2015-09-21 Steven G. Kargl <kargl@gcc.gnu.org>
[official-gcc.git] / gcc / cp / parser.c
blob013418941b11007916d9774279147e97847c3704
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2015 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 "alias.h"
28 #include "tree.h"
29 #include "print-tree.h"
30 #include "stringpool.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "cp-tree.h"
34 #include "intl.h"
35 #include "c-family/c-pragma.h"
36 #include "decl.h"
37 #include "flags.h"
38 #include "diagnostic-core.h"
39 #include "target.h"
40 #include "hard-reg-set.h"
41 #include "function.h"
42 #include "cgraph.h"
43 #include "c-family/c-common.h"
44 #include "c-family/c-objc.h"
45 #include "plugin.h"
46 #include "tree-pretty-print.h"
47 #include "parser.h"
48 #include "type-utils.h"
49 #include "omp-low.h"
50 #include "gomp-constants.h"
51 #include "c-family/c-indentation.h"
54 /* The lexer. */
56 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
57 and c-lex.c) and the C++ parser. */
59 static cp_token eof_token =
61 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
64 /* The various kinds of non integral constant we encounter. */
65 enum non_integral_constant {
66 NIC_NONE,
67 /* floating-point literal */
68 NIC_FLOAT,
69 /* %<this%> */
70 NIC_THIS,
71 /* %<__FUNCTION__%> */
72 NIC_FUNC_NAME,
73 /* %<__PRETTY_FUNCTION__%> */
74 NIC_PRETTY_FUNC,
75 /* %<__func__%> */
76 NIC_C99_FUNC,
77 /* "%<va_arg%> */
78 NIC_VA_ARG,
79 /* a cast */
80 NIC_CAST,
81 /* %<typeid%> operator */
82 NIC_TYPEID,
83 /* non-constant compound literals */
84 NIC_NCC,
85 /* a function call */
86 NIC_FUNC_CALL,
87 /* an increment */
88 NIC_INC,
89 /* an decrement */
90 NIC_DEC,
91 /* an array reference */
92 NIC_ARRAY_REF,
93 /* %<->%> */
94 NIC_ARROW,
95 /* %<.%> */
96 NIC_POINT,
97 /* the address of a label */
98 NIC_ADDR_LABEL,
99 /* %<*%> */
100 NIC_STAR,
101 /* %<&%> */
102 NIC_ADDR,
103 /* %<++%> */
104 NIC_PREINCREMENT,
105 /* %<--%> */
106 NIC_PREDECREMENT,
107 /* %<new%> */
108 NIC_NEW,
109 /* %<delete%> */
110 NIC_DEL,
111 /* calls to overloaded operators */
112 NIC_OVERLOADED,
113 /* an assignment */
114 NIC_ASSIGNMENT,
115 /* a comma operator */
116 NIC_COMMA,
117 /* a call to a constructor */
118 NIC_CONSTRUCTOR,
119 /* a transaction expression */
120 NIC_TRANSACTION
123 /* The various kinds of errors about name-lookup failing. */
124 enum name_lookup_error {
125 /* NULL */
126 NLE_NULL,
127 /* is not a type */
128 NLE_TYPE,
129 /* is not a class or namespace */
130 NLE_CXX98,
131 /* is not a class, namespace, or enumeration */
132 NLE_NOT_CXX98
135 /* The various kinds of required token */
136 enum required_token {
137 RT_NONE,
138 RT_SEMICOLON, /* ';' */
139 RT_OPEN_PAREN, /* '(' */
140 RT_CLOSE_BRACE, /* '}' */
141 RT_OPEN_BRACE, /* '{' */
142 RT_CLOSE_SQUARE, /* ']' */
143 RT_OPEN_SQUARE, /* '[' */
144 RT_COMMA, /* ',' */
145 RT_SCOPE, /* '::' */
146 RT_LESS, /* '<' */
147 RT_GREATER, /* '>' */
148 RT_EQ, /* '=' */
149 RT_ELLIPSIS, /* '...' */
150 RT_MULT, /* '*' */
151 RT_COMPL, /* '~' */
152 RT_COLON, /* ':' */
153 RT_COLON_SCOPE, /* ':' or '::' */
154 RT_CLOSE_PAREN, /* ')' */
155 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
156 RT_PRAGMA_EOL, /* end of line */
157 RT_NAME, /* identifier */
159 /* The type is CPP_KEYWORD */
160 RT_NEW, /* new */
161 RT_DELETE, /* delete */
162 RT_RETURN, /* return */
163 RT_WHILE, /* while */
164 RT_EXTERN, /* extern */
165 RT_STATIC_ASSERT, /* static_assert */
166 RT_DECLTYPE, /* decltype */
167 RT_OPERATOR, /* operator */
168 RT_CLASS, /* class */
169 RT_TEMPLATE, /* template */
170 RT_NAMESPACE, /* namespace */
171 RT_USING, /* using */
172 RT_ASM, /* asm */
173 RT_TRY, /* try */
174 RT_CATCH, /* catch */
175 RT_THROW, /* throw */
176 RT_LABEL, /* __label__ */
177 RT_AT_TRY, /* @try */
178 RT_AT_SYNCHRONIZED, /* @synchronized */
179 RT_AT_THROW, /* @throw */
181 RT_SELECT, /* selection-statement */
182 RT_INTERATION, /* iteration-statement */
183 RT_JUMP, /* jump-statement */
184 RT_CLASS_KEY, /* class-key */
185 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
186 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
187 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
188 RT_TRANSACTION_CANCEL /* __transaction_cancel */
191 /* Prototypes. */
193 static cp_lexer *cp_lexer_new_main
194 (void);
195 static cp_lexer *cp_lexer_new_from_tokens
196 (cp_token_cache *tokens);
197 static void cp_lexer_destroy
198 (cp_lexer *);
199 static int cp_lexer_saving_tokens
200 (const cp_lexer *);
201 static cp_token *cp_lexer_token_at
202 (cp_lexer *, cp_token_position);
203 static void cp_lexer_get_preprocessor_token
204 (cp_lexer *, cp_token *);
205 static inline cp_token *cp_lexer_peek_token
206 (cp_lexer *);
207 static cp_token *cp_lexer_peek_nth_token
208 (cp_lexer *, size_t);
209 static inline bool cp_lexer_next_token_is
210 (cp_lexer *, enum cpp_ttype);
211 static bool cp_lexer_next_token_is_not
212 (cp_lexer *, enum cpp_ttype);
213 static bool cp_lexer_next_token_is_keyword
214 (cp_lexer *, enum rid);
215 static cp_token *cp_lexer_consume_token
216 (cp_lexer *);
217 static void cp_lexer_purge_token
218 (cp_lexer *);
219 static void cp_lexer_purge_tokens_after
220 (cp_lexer *, cp_token_position);
221 static void cp_lexer_save_tokens
222 (cp_lexer *);
223 static void cp_lexer_commit_tokens
224 (cp_lexer *);
225 static void cp_lexer_rollback_tokens
226 (cp_lexer *);
227 static void cp_lexer_print_token
228 (FILE *, cp_token *);
229 static inline bool cp_lexer_debugging_p
230 (cp_lexer *);
231 static void cp_lexer_start_debugging
232 (cp_lexer *) ATTRIBUTE_UNUSED;
233 static void cp_lexer_stop_debugging
234 (cp_lexer *) ATTRIBUTE_UNUSED;
236 static cp_token_cache *cp_token_cache_new
237 (cp_token *, cp_token *);
239 static void cp_parser_initial_pragma
240 (cp_token *);
242 static tree cp_literal_operator_id
243 (const char *);
245 static void cp_parser_cilk_simd
246 (cp_parser *, cp_token *);
247 static tree cp_parser_cilk_for
248 (cp_parser *, tree);
249 static bool cp_parser_omp_declare_reduction_exprs
250 (tree, cp_parser *);
251 static tree cp_parser_cilk_simd_vectorlength
252 (cp_parser *, tree, bool);
254 /* Manifest constants. */
255 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
256 #define CP_SAVED_TOKEN_STACK 5
258 /* Variables. */
260 /* The stream to which debugging output should be written. */
261 static FILE *cp_lexer_debug_stream;
263 /* Nonzero if we are parsing an unevaluated operand: an operand to
264 sizeof, typeof, or alignof. */
265 int cp_unevaluated_operand;
267 /* Dump up to NUM tokens in BUFFER to FILE starting with token
268 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
269 first token in BUFFER. If NUM is 0, dump all the tokens. If
270 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
271 highlighted by surrounding it in [[ ]]. */
273 static void
274 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
275 cp_token *start_token, unsigned num,
276 cp_token *curr_token)
278 unsigned i, nprinted;
279 cp_token *token;
280 bool do_print;
282 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
284 if (buffer == NULL)
285 return;
287 if (num == 0)
288 num = buffer->length ();
290 if (start_token == NULL)
291 start_token = buffer->address ();
293 if (start_token > buffer->address ())
295 cp_lexer_print_token (file, &(*buffer)[0]);
296 fprintf (file, " ... ");
299 do_print = false;
300 nprinted = 0;
301 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
303 if (token == start_token)
304 do_print = true;
306 if (!do_print)
307 continue;
309 nprinted++;
310 if (token == curr_token)
311 fprintf (file, "[[");
313 cp_lexer_print_token (file, token);
315 if (token == curr_token)
316 fprintf (file, "]]");
318 switch (token->type)
320 case CPP_SEMICOLON:
321 case CPP_OPEN_BRACE:
322 case CPP_CLOSE_BRACE:
323 case CPP_EOF:
324 fputc ('\n', file);
325 break;
327 default:
328 fputc (' ', file);
332 if (i == num && i < buffer->length ())
334 fprintf (file, " ... ");
335 cp_lexer_print_token (file, &buffer->last ());
338 fprintf (file, "\n");
342 /* Dump all tokens in BUFFER to stderr. */
344 void
345 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
347 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
350 DEBUG_FUNCTION void
351 debug (vec<cp_token, va_gc> &ref)
353 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
356 DEBUG_FUNCTION void
357 debug (vec<cp_token, va_gc> *ptr)
359 if (ptr)
360 debug (*ptr);
361 else
362 fprintf (stderr, "<nil>\n");
366 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
367 description for T. */
369 static void
370 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
372 if (t)
374 fprintf (file, "%s: ", desc);
375 print_node_brief (file, "", t, 0);
380 /* Dump parser context C to FILE. */
382 static void
383 cp_debug_print_context (FILE *file, cp_parser_context *c)
385 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
386 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
387 print_node_brief (file, "", c->object_type, 0);
388 fprintf (file, "}\n");
392 /* Print the stack of parsing contexts to FILE starting with FIRST. */
394 static void
395 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
397 unsigned i;
398 cp_parser_context *c;
400 fprintf (file, "Parsing context stack:\n");
401 for (i = 0, c = first; c; c = c->next, i++)
403 fprintf (file, "\t#%u: ", i);
404 cp_debug_print_context (file, c);
409 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
411 static void
412 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
414 if (flag)
415 fprintf (file, "%s: true\n", desc);
419 /* Print an unparsed function entry UF to FILE. */
421 static void
422 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
424 unsigned i;
425 cp_default_arg_entry *default_arg_fn;
426 tree fn;
428 fprintf (file, "\tFunctions with default args:\n");
429 for (i = 0;
430 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
431 i++)
433 fprintf (file, "\t\tClass type: ");
434 print_node_brief (file, "", default_arg_fn->class_type, 0);
435 fprintf (file, "\t\tDeclaration: ");
436 print_node_brief (file, "", default_arg_fn->decl, 0);
437 fprintf (file, "\n");
440 fprintf (file, "\n\tFunctions with definitions that require "
441 "post-processing\n\t\t");
442 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
444 print_node_brief (file, "", fn, 0);
445 fprintf (file, " ");
447 fprintf (file, "\n");
449 fprintf (file, "\n\tNon-static data members with initializers that require "
450 "post-processing\n\t\t");
451 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
453 print_node_brief (file, "", fn, 0);
454 fprintf (file, " ");
456 fprintf (file, "\n");
460 /* Print the stack of unparsed member functions S to FILE. */
462 static void
463 cp_debug_print_unparsed_queues (FILE *file,
464 vec<cp_unparsed_functions_entry, va_gc> *s)
466 unsigned i;
467 cp_unparsed_functions_entry *uf;
469 fprintf (file, "Unparsed functions\n");
470 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
472 fprintf (file, "#%u:\n", i);
473 cp_debug_print_unparsed_function (file, uf);
478 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
479 the given PARSER. If FILE is NULL, the output is printed on stderr. */
481 static void
482 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
484 cp_token *next_token, *first_token, *start_token;
486 if (file == NULL)
487 file = stderr;
489 next_token = parser->lexer->next_token;
490 first_token = parser->lexer->buffer->address ();
491 start_token = (next_token > first_token + window_size / 2)
492 ? next_token - window_size / 2
493 : first_token;
494 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
495 next_token);
499 /* Dump debugging information for the given PARSER. If FILE is NULL,
500 the output is printed on stderr. */
502 void
503 cp_debug_parser (FILE *file, cp_parser *parser)
505 const size_t window_size = 20;
506 cp_token *token;
507 expanded_location eloc;
509 if (file == NULL)
510 file = stderr;
512 fprintf (file, "Parser state\n\n");
513 fprintf (file, "Number of tokens: %u\n",
514 vec_safe_length (parser->lexer->buffer));
515 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
516 cp_debug_print_tree_if_set (file, "Object scope",
517 parser->object_scope);
518 cp_debug_print_tree_if_set (file, "Qualifying scope",
519 parser->qualifying_scope);
520 cp_debug_print_context_stack (file, parser->context);
521 cp_debug_print_flag (file, "Allow GNU extensions",
522 parser->allow_gnu_extensions_p);
523 cp_debug_print_flag (file, "'>' token is greater-than",
524 parser->greater_than_is_operator_p);
525 cp_debug_print_flag (file, "Default args allowed in current "
526 "parameter list", parser->default_arg_ok_p);
527 cp_debug_print_flag (file, "Parsing integral constant-expression",
528 parser->integral_constant_expression_p);
529 cp_debug_print_flag (file, "Allow non-constant expression in current "
530 "constant-expression",
531 parser->allow_non_integral_constant_expression_p);
532 cp_debug_print_flag (file, "Seen non-constant expression",
533 parser->non_integral_constant_expression_p);
534 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
535 "current context",
536 parser->local_variables_forbidden_p);
537 cp_debug_print_flag (file, "In unbraced linkage specification",
538 parser->in_unbraced_linkage_specification_p);
539 cp_debug_print_flag (file, "Parsing a declarator",
540 parser->in_declarator_p);
541 cp_debug_print_flag (file, "In template argument list",
542 parser->in_template_argument_list_p);
543 cp_debug_print_flag (file, "Parsing an iteration statement",
544 parser->in_statement & IN_ITERATION_STMT);
545 cp_debug_print_flag (file, "Parsing a switch statement",
546 parser->in_statement & IN_SWITCH_STMT);
547 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
548 parser->in_statement & IN_OMP_BLOCK);
549 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
550 parser->in_statement & IN_CILK_SIMD_FOR);
551 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
552 parser->in_statement & IN_OMP_FOR);
553 cp_debug_print_flag (file, "Parsing an if statement",
554 parser->in_statement & IN_IF_STMT);
555 cp_debug_print_flag (file, "Parsing a type-id in an expression "
556 "context", parser->in_type_id_in_expr_p);
557 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
558 parser->implicit_extern_c);
559 cp_debug_print_flag (file, "String expressions should be translated "
560 "to execution character set",
561 parser->translate_strings_p);
562 cp_debug_print_flag (file, "Parsing function body outside of a "
563 "local class", parser->in_function_body);
564 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
565 parser->colon_corrects_to_scope_p);
566 cp_debug_print_flag (file, "Colon doesn't start a class definition",
567 parser->colon_doesnt_start_class_def_p);
568 if (parser->type_definition_forbidden_message)
569 fprintf (file, "Error message for forbidden type definitions: %s\n",
570 parser->type_definition_forbidden_message);
571 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
572 fprintf (file, "Number of class definitions in progress: %u\n",
573 parser->num_classes_being_defined);
574 fprintf (file, "Number of template parameter lists for the current "
575 "declaration: %u\n", parser->num_template_parameter_lists);
576 cp_debug_parser_tokens (file, parser, window_size);
577 token = parser->lexer->next_token;
578 fprintf (file, "Next token to parse:\n");
579 fprintf (file, "\tToken: ");
580 cp_lexer_print_token (file, token);
581 eloc = expand_location (token->location);
582 fprintf (file, "\n\tFile: %s\n", eloc.file);
583 fprintf (file, "\tLine: %d\n", eloc.line);
584 fprintf (file, "\tColumn: %d\n", eloc.column);
587 DEBUG_FUNCTION void
588 debug (cp_parser &ref)
590 cp_debug_parser (stderr, &ref);
593 DEBUG_FUNCTION void
594 debug (cp_parser *ptr)
596 if (ptr)
597 debug (*ptr);
598 else
599 fprintf (stderr, "<nil>\n");
602 /* Allocate memory for a new lexer object and return it. */
604 static cp_lexer *
605 cp_lexer_alloc (void)
607 cp_lexer *lexer;
609 c_common_no_more_pch ();
611 /* Allocate the memory. */
612 lexer = ggc_cleared_alloc<cp_lexer> ();
614 /* Initially we are not debugging. */
615 lexer->debugging_p = false;
617 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
619 /* Create the buffer. */
620 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
622 return lexer;
626 /* Create a new main C++ lexer, the lexer that gets tokens from the
627 preprocessor. */
629 static cp_lexer *
630 cp_lexer_new_main (void)
632 cp_lexer *lexer;
633 cp_token token;
635 /* It's possible that parsing the first pragma will load a PCH file,
636 which is a GC collection point. So we have to do that before
637 allocating any memory. */
638 cp_parser_initial_pragma (&token);
640 lexer = cp_lexer_alloc ();
642 /* Put the first token in the buffer. */
643 lexer->buffer->quick_push (token);
645 /* Get the remaining tokens from the preprocessor. */
646 while (token.type != CPP_EOF)
648 cp_lexer_get_preprocessor_token (lexer, &token);
649 vec_safe_push (lexer->buffer, token);
652 lexer->last_token = lexer->buffer->address ()
653 + lexer->buffer->length ()
654 - 1;
655 lexer->next_token = lexer->buffer->length ()
656 ? lexer->buffer->address ()
657 : &eof_token;
659 /* Subsequent preprocessor diagnostics should use compiler
660 diagnostic functions to get the compiler source location. */
661 done_lexing = true;
663 gcc_assert (!lexer->next_token->purged_p);
664 return lexer;
667 /* Create a new lexer whose token stream is primed with the tokens in
668 CACHE. When these tokens are exhausted, no new tokens will be read. */
670 static cp_lexer *
671 cp_lexer_new_from_tokens (cp_token_cache *cache)
673 cp_token *first = cache->first;
674 cp_token *last = cache->last;
675 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
677 /* We do not own the buffer. */
678 lexer->buffer = NULL;
679 lexer->next_token = first == last ? &eof_token : first;
680 lexer->last_token = last;
682 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
684 /* Initially we are not debugging. */
685 lexer->debugging_p = false;
687 gcc_assert (!lexer->next_token->purged_p);
688 return lexer;
691 /* Frees all resources associated with LEXER. */
693 static void
694 cp_lexer_destroy (cp_lexer *lexer)
696 vec_free (lexer->buffer);
697 lexer->saved_tokens.release ();
698 ggc_free (lexer);
701 /* Returns nonzero if debugging information should be output. */
703 static inline bool
704 cp_lexer_debugging_p (cp_lexer *lexer)
706 return lexer->debugging_p;
710 static inline cp_token_position
711 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
713 gcc_assert (!previous_p || lexer->next_token != &eof_token);
715 return lexer->next_token - previous_p;
718 static inline cp_token *
719 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
721 return pos;
724 static inline void
725 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
727 lexer->next_token = cp_lexer_token_at (lexer, pos);
730 static inline cp_token_position
731 cp_lexer_previous_token_position (cp_lexer *lexer)
733 if (lexer->next_token == &eof_token)
734 return lexer->last_token - 1;
735 else
736 return cp_lexer_token_position (lexer, true);
739 static inline cp_token *
740 cp_lexer_previous_token (cp_lexer *lexer)
742 cp_token_position tp = cp_lexer_previous_token_position (lexer);
744 return cp_lexer_token_at (lexer, tp);
747 /* nonzero if we are presently saving tokens. */
749 static inline int
750 cp_lexer_saving_tokens (const cp_lexer* lexer)
752 return lexer->saved_tokens.length () != 0;
755 /* Store the next token from the preprocessor in *TOKEN. Return true
756 if we reach EOF. If LEXER is NULL, assume we are handling an
757 initial #pragma pch_preprocess, and thus want the lexer to return
758 processed strings. */
760 static void
761 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
763 static int is_extern_c = 0;
765 /* Get a new token from the preprocessor. */
766 token->type
767 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
768 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
769 token->keyword = RID_MAX;
770 token->pragma_kind = PRAGMA_NONE;
771 token->purged_p = false;
772 token->error_reported = false;
774 /* On some systems, some header files are surrounded by an
775 implicit extern "C" block. Set a flag in the token if it
776 comes from such a header. */
777 is_extern_c += pending_lang_change;
778 pending_lang_change = 0;
779 token->implicit_extern_c = is_extern_c > 0;
781 /* Check to see if this token is a keyword. */
782 if (token->type == CPP_NAME)
784 if (C_IS_RESERVED_WORD (token->u.value))
786 /* Mark this token as a keyword. */
787 token->type = CPP_KEYWORD;
788 /* Record which keyword. */
789 token->keyword = C_RID_CODE (token->u.value);
791 else
793 if (warn_cxx11_compat
794 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
795 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
797 /* Warn about the C++0x keyword (but still treat it as
798 an identifier). */
799 warning (OPT_Wc__11_compat,
800 "identifier %qE is a keyword in C++11",
801 token->u.value);
803 /* Clear out the C_RID_CODE so we don't warn about this
804 particular identifier-turned-keyword again. */
805 C_SET_RID_CODE (token->u.value, RID_MAX);
808 token->keyword = RID_MAX;
811 else if (token->type == CPP_AT_NAME)
813 /* This only happens in Objective-C++; it must be a keyword. */
814 token->type = CPP_KEYWORD;
815 switch (C_RID_CODE (token->u.value))
817 /* Replace 'class' with '@class', 'private' with '@private',
818 etc. This prevents confusion with the C++ keyword
819 'class', and makes the tokens consistent with other
820 Objective-C 'AT' keywords. For example '@class' is
821 reported as RID_AT_CLASS which is consistent with
822 '@synchronized', which is reported as
823 RID_AT_SYNCHRONIZED.
825 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
826 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
827 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
828 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
829 case RID_THROW: token->keyword = RID_AT_THROW; break;
830 case RID_TRY: token->keyword = RID_AT_TRY; break;
831 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
832 default: token->keyword = C_RID_CODE (token->u.value);
835 else if (token->type == CPP_PRAGMA)
837 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
838 token->pragma_kind = ((enum pragma_kind)
839 TREE_INT_CST_LOW (token->u.value));
840 token->u.value = NULL_TREE;
844 /* Update the globals input_location and the input file stack from TOKEN. */
845 static inline void
846 cp_lexer_set_source_position_from_token (cp_token *token)
848 if (token->type != CPP_EOF)
850 input_location = token->location;
854 /* Update the globals input_location and the input file stack from LEXER. */
855 static inline void
856 cp_lexer_set_source_position (cp_lexer *lexer)
858 cp_token *token = cp_lexer_peek_token (lexer);
859 cp_lexer_set_source_position_from_token (token);
862 /* Return a pointer to the next token in the token stream, but do not
863 consume it. */
865 static inline cp_token *
866 cp_lexer_peek_token (cp_lexer *lexer)
868 if (cp_lexer_debugging_p (lexer))
870 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
871 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
872 putc ('\n', cp_lexer_debug_stream);
874 return lexer->next_token;
877 /* Return true if the next token has the indicated TYPE. */
879 static inline bool
880 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
882 return cp_lexer_peek_token (lexer)->type == type;
885 /* Return true if the next token does not have the indicated TYPE. */
887 static inline bool
888 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
890 return !cp_lexer_next_token_is (lexer, type);
893 /* Return true if the next token is the indicated KEYWORD. */
895 static inline bool
896 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
898 return cp_lexer_peek_token (lexer)->keyword == keyword;
901 static inline bool
902 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
904 return cp_lexer_peek_nth_token (lexer, n)->type == type;
907 static inline bool
908 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
910 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
913 /* Return true if the next token is not the indicated KEYWORD. */
915 static inline bool
916 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
918 return cp_lexer_peek_token (lexer)->keyword != keyword;
921 /* Return true if the next token is a keyword for a decl-specifier. */
923 static bool
924 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
926 cp_token *token;
928 token = cp_lexer_peek_token (lexer);
929 switch (token->keyword)
931 /* auto specifier: storage-class-specifier in C++,
932 simple-type-specifier in C++0x. */
933 case RID_AUTO:
934 /* Storage classes. */
935 case RID_REGISTER:
936 case RID_STATIC:
937 case RID_EXTERN:
938 case RID_MUTABLE:
939 case RID_THREAD:
940 /* Elaborated type specifiers. */
941 case RID_ENUM:
942 case RID_CLASS:
943 case RID_STRUCT:
944 case RID_UNION:
945 case RID_TYPENAME:
946 /* Simple type specifiers. */
947 case RID_CHAR:
948 case RID_CHAR16:
949 case RID_CHAR32:
950 case RID_WCHAR:
951 case RID_BOOL:
952 case RID_SHORT:
953 case RID_INT:
954 case RID_LONG:
955 case RID_SIGNED:
956 case RID_UNSIGNED:
957 case RID_FLOAT:
958 case RID_DOUBLE:
959 case RID_VOID:
960 /* GNU extensions. */
961 case RID_ATTRIBUTE:
962 case RID_TYPEOF:
963 /* C++0x extensions. */
964 case RID_DECLTYPE:
965 case RID_UNDERLYING_TYPE:
966 return true;
968 default:
969 if (token->keyword >= RID_FIRST_INT_N
970 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
971 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
972 return true;
973 return false;
977 /* Returns TRUE iff the token T begins a decltype type. */
979 static bool
980 token_is_decltype (cp_token *t)
982 return (t->keyword == RID_DECLTYPE
983 || t->type == CPP_DECLTYPE);
986 /* Returns TRUE iff the next token begins a decltype type. */
988 static bool
989 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
991 cp_token *t = cp_lexer_peek_token (lexer);
992 return token_is_decltype (t);
995 /* Return a pointer to the Nth token in the token stream. If N is 1,
996 then this is precisely equivalent to cp_lexer_peek_token (except
997 that it is not inline). One would like to disallow that case, but
998 there is one case (cp_parser_nth_token_starts_template_id) where
999 the caller passes a variable for N and it might be 1. */
1001 static cp_token *
1002 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1004 cp_token *token;
1006 /* N is 1-based, not zero-based. */
1007 gcc_assert (n > 0);
1009 if (cp_lexer_debugging_p (lexer))
1010 fprintf (cp_lexer_debug_stream,
1011 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1013 --n;
1014 token = lexer->next_token;
1015 gcc_assert (!n || token != &eof_token);
1016 while (n != 0)
1018 ++token;
1019 if (token == lexer->last_token)
1021 token = &eof_token;
1022 break;
1025 if (!token->purged_p)
1026 --n;
1029 if (cp_lexer_debugging_p (lexer))
1031 cp_lexer_print_token (cp_lexer_debug_stream, token);
1032 putc ('\n', cp_lexer_debug_stream);
1035 return token;
1038 /* Return the next token, and advance the lexer's next_token pointer
1039 to point to the next non-purged token. */
1041 static cp_token *
1042 cp_lexer_consume_token (cp_lexer* lexer)
1044 cp_token *token = lexer->next_token;
1046 gcc_assert (token != &eof_token);
1047 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1051 lexer->next_token++;
1052 if (lexer->next_token == lexer->last_token)
1054 lexer->next_token = &eof_token;
1055 break;
1059 while (lexer->next_token->purged_p);
1061 cp_lexer_set_source_position_from_token (token);
1063 /* Provide debugging output. */
1064 if (cp_lexer_debugging_p (lexer))
1066 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1067 cp_lexer_print_token (cp_lexer_debug_stream, token);
1068 putc ('\n', cp_lexer_debug_stream);
1071 return token;
1074 /* Permanently remove the next token from the token stream, and
1075 advance the next_token pointer to refer to the next non-purged
1076 token. */
1078 static void
1079 cp_lexer_purge_token (cp_lexer *lexer)
1081 cp_token *tok = lexer->next_token;
1083 gcc_assert (tok != &eof_token);
1084 tok->purged_p = true;
1085 tok->location = UNKNOWN_LOCATION;
1086 tok->u.value = NULL_TREE;
1087 tok->keyword = RID_MAX;
1091 tok++;
1092 if (tok == lexer->last_token)
1094 tok = &eof_token;
1095 break;
1098 while (tok->purged_p);
1099 lexer->next_token = tok;
1102 /* Permanently remove all tokens after TOK, up to, but not
1103 including, the token that will be returned next by
1104 cp_lexer_peek_token. */
1106 static void
1107 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1109 cp_token *peek = lexer->next_token;
1111 if (peek == &eof_token)
1112 peek = lexer->last_token;
1114 gcc_assert (tok < peek);
1116 for ( tok += 1; tok != peek; tok += 1)
1118 tok->purged_p = true;
1119 tok->location = UNKNOWN_LOCATION;
1120 tok->u.value = NULL_TREE;
1121 tok->keyword = RID_MAX;
1125 /* Begin saving tokens. All tokens consumed after this point will be
1126 preserved. */
1128 static void
1129 cp_lexer_save_tokens (cp_lexer* lexer)
1131 /* Provide debugging output. */
1132 if (cp_lexer_debugging_p (lexer))
1133 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1135 lexer->saved_tokens.safe_push (lexer->next_token);
1138 /* Commit to the portion of the token stream most recently saved. */
1140 static void
1141 cp_lexer_commit_tokens (cp_lexer* lexer)
1143 /* Provide debugging output. */
1144 if (cp_lexer_debugging_p (lexer))
1145 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1147 lexer->saved_tokens.pop ();
1150 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1151 to the token stream. Stop saving tokens. */
1153 static void
1154 cp_lexer_rollback_tokens (cp_lexer* lexer)
1156 /* Provide debugging output. */
1157 if (cp_lexer_debugging_p (lexer))
1158 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1160 lexer->next_token = lexer->saved_tokens.pop ();
1163 /* RAII wrapper around the above functions, with sanity checking. Creating
1164 a variable saves tokens, which are committed when the variable is
1165 destroyed unless they are explicitly rolled back by calling the rollback
1166 member function. */
1168 struct saved_token_sentinel
1170 cp_lexer *lexer;
1171 unsigned len;
1172 bool commit;
1173 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1175 len = lexer->saved_tokens.length ();
1176 cp_lexer_save_tokens (lexer);
1178 void rollback ()
1180 cp_lexer_rollback_tokens (lexer);
1181 commit = false;
1183 ~saved_token_sentinel()
1185 if (commit)
1186 cp_lexer_commit_tokens (lexer);
1187 gcc_assert (lexer->saved_tokens.length () == len);
1191 /* Print a representation of the TOKEN on the STREAM. */
1193 static void
1194 cp_lexer_print_token (FILE * stream, cp_token *token)
1196 /* We don't use cpp_type2name here because the parser defines
1197 a few tokens of its own. */
1198 static const char *const token_names[] = {
1199 /* cpplib-defined token types */
1200 #define OP(e, s) #e,
1201 #define TK(e, s) #e,
1202 TTYPE_TABLE
1203 #undef OP
1204 #undef TK
1205 /* C++ parser token types - see "Manifest constants", above. */
1206 "KEYWORD",
1207 "TEMPLATE_ID",
1208 "NESTED_NAME_SPECIFIER",
1211 /* For some tokens, print the associated data. */
1212 switch (token->type)
1214 case CPP_KEYWORD:
1215 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1216 For example, `struct' is mapped to an INTEGER_CST. */
1217 if (!identifier_p (token->u.value))
1218 break;
1219 /* else fall through */
1220 case CPP_NAME:
1221 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1222 break;
1224 case CPP_STRING:
1225 case CPP_STRING16:
1226 case CPP_STRING32:
1227 case CPP_WSTRING:
1228 case CPP_UTF8STRING:
1229 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1230 break;
1232 case CPP_NUMBER:
1233 print_generic_expr (stream, token->u.value, 0);
1234 break;
1236 default:
1237 /* If we have a name for the token, print it out. Otherwise, we
1238 simply give the numeric code. */
1239 if (token->type < ARRAY_SIZE(token_names))
1240 fputs (token_names[token->type], stream);
1241 else
1242 fprintf (stream, "[%d]", token->type);
1243 break;
1247 DEBUG_FUNCTION void
1248 debug (cp_token &ref)
1250 cp_lexer_print_token (stderr, &ref);
1251 fprintf (stderr, "\n");
1254 DEBUG_FUNCTION void
1255 debug (cp_token *ptr)
1257 if (ptr)
1258 debug (*ptr);
1259 else
1260 fprintf (stderr, "<nil>\n");
1264 /* Start emitting debugging information. */
1266 static void
1267 cp_lexer_start_debugging (cp_lexer* lexer)
1269 lexer->debugging_p = true;
1270 cp_lexer_debug_stream = stderr;
1273 /* Stop emitting debugging information. */
1275 static void
1276 cp_lexer_stop_debugging (cp_lexer* lexer)
1278 lexer->debugging_p = false;
1279 cp_lexer_debug_stream = NULL;
1282 /* Create a new cp_token_cache, representing a range of tokens. */
1284 static cp_token_cache *
1285 cp_token_cache_new (cp_token *first, cp_token *last)
1287 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1288 cache->first = first;
1289 cache->last = last;
1290 return cache;
1293 /* Diagnose if #pragma omp declare simd isn't followed immediately
1294 by function declaration or definition. */
1296 static inline void
1297 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1299 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1301 error ("%<#pragma omp declare simd%> not immediately followed by "
1302 "function declaration or definition");
1303 parser->omp_declare_simd = NULL;
1307 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1308 and put that into "omp declare simd" attribute. */
1310 static inline void
1311 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1313 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1315 if (fndecl == error_mark_node)
1317 parser->omp_declare_simd = NULL;
1318 return;
1320 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1322 cp_ensure_no_omp_declare_simd (parser);
1323 return;
1328 /* Decl-specifiers. */
1330 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1332 static void
1333 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1335 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1338 /* Declarators. */
1340 /* Nothing other than the parser should be creating declarators;
1341 declarators are a semi-syntactic representation of C++ entities.
1342 Other parts of the front end that need to create entities (like
1343 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1345 static cp_declarator *make_call_declarator
1346 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree);
1347 static cp_declarator *make_array_declarator
1348 (cp_declarator *, tree);
1349 static cp_declarator *make_pointer_declarator
1350 (cp_cv_quals, cp_declarator *, tree);
1351 static cp_declarator *make_reference_declarator
1352 (cp_cv_quals, cp_declarator *, bool, tree);
1353 static cp_declarator *make_ptrmem_declarator
1354 (cp_cv_quals, tree, cp_declarator *, tree);
1356 /* An erroneous declarator. */
1357 static cp_declarator *cp_error_declarator;
1359 /* The obstack on which declarators and related data structures are
1360 allocated. */
1361 static struct obstack declarator_obstack;
1363 /* Alloc BYTES from the declarator memory pool. */
1365 static inline void *
1366 alloc_declarator (size_t bytes)
1368 return obstack_alloc (&declarator_obstack, bytes);
1371 /* Allocate a declarator of the indicated KIND. Clear fields that are
1372 common to all declarators. */
1374 static cp_declarator *
1375 make_declarator (cp_declarator_kind kind)
1377 cp_declarator *declarator;
1379 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1380 declarator->kind = kind;
1381 declarator->attributes = NULL_TREE;
1382 declarator->std_attributes = NULL_TREE;
1383 declarator->declarator = NULL;
1384 declarator->parameter_pack_p = false;
1385 declarator->id_loc = UNKNOWN_LOCATION;
1387 return declarator;
1390 /* Make a declarator for a generalized identifier. If
1391 QUALIFYING_SCOPE is non-NULL, the identifier is
1392 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1393 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1394 is, if any. */
1396 static cp_declarator *
1397 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1398 special_function_kind sfk)
1400 cp_declarator *declarator;
1402 /* It is valid to write:
1404 class C { void f(); };
1405 typedef C D;
1406 void D::f();
1408 The standard is not clear about whether `typedef const C D' is
1409 legal; as of 2002-09-15 the committee is considering that
1410 question. EDG 3.0 allows that syntax. Therefore, we do as
1411 well. */
1412 if (qualifying_scope && TYPE_P (qualifying_scope))
1413 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1415 gcc_assert (identifier_p (unqualified_name)
1416 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1417 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1419 declarator = make_declarator (cdk_id);
1420 declarator->u.id.qualifying_scope = qualifying_scope;
1421 declarator->u.id.unqualified_name = unqualified_name;
1422 declarator->u.id.sfk = sfk;
1424 return declarator;
1427 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1428 of modifiers such as const or volatile to apply to the pointer
1429 type, represented as identifiers. ATTRIBUTES represent the attributes that
1430 appertain to the pointer or reference. */
1432 cp_declarator *
1433 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1434 tree attributes)
1436 cp_declarator *declarator;
1438 declarator = make_declarator (cdk_pointer);
1439 declarator->declarator = target;
1440 declarator->u.pointer.qualifiers = cv_qualifiers;
1441 declarator->u.pointer.class_type = NULL_TREE;
1442 if (target)
1444 declarator->id_loc = target->id_loc;
1445 declarator->parameter_pack_p = target->parameter_pack_p;
1446 target->parameter_pack_p = false;
1448 else
1449 declarator->parameter_pack_p = false;
1451 declarator->std_attributes = attributes;
1453 return declarator;
1456 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1457 represent the attributes that appertain to the pointer or
1458 reference. */
1460 cp_declarator *
1461 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1462 bool rvalue_ref, tree attributes)
1464 cp_declarator *declarator;
1466 declarator = make_declarator (cdk_reference);
1467 declarator->declarator = target;
1468 declarator->u.reference.qualifiers = cv_qualifiers;
1469 declarator->u.reference.rvalue_ref = rvalue_ref;
1470 if (target)
1472 declarator->id_loc = target->id_loc;
1473 declarator->parameter_pack_p = target->parameter_pack_p;
1474 target->parameter_pack_p = false;
1476 else
1477 declarator->parameter_pack_p = false;
1479 declarator->std_attributes = attributes;
1481 return declarator;
1484 /* Like make_pointer_declarator -- but for a pointer to a non-static
1485 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1486 appertain to the pointer or reference. */
1488 cp_declarator *
1489 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1490 cp_declarator *pointee,
1491 tree attributes)
1493 cp_declarator *declarator;
1495 declarator = make_declarator (cdk_ptrmem);
1496 declarator->declarator = pointee;
1497 declarator->u.pointer.qualifiers = cv_qualifiers;
1498 declarator->u.pointer.class_type = class_type;
1500 if (pointee)
1502 declarator->parameter_pack_p = pointee->parameter_pack_p;
1503 pointee->parameter_pack_p = false;
1505 else
1506 declarator->parameter_pack_p = false;
1508 declarator->std_attributes = attributes;
1510 return declarator;
1513 /* Make a declarator for the function given by TARGET, with the
1514 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1515 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1516 indicates what exceptions can be thrown. */
1518 cp_declarator *
1519 make_call_declarator (cp_declarator *target,
1520 tree parms,
1521 cp_cv_quals cv_qualifiers,
1522 cp_virt_specifiers virt_specifiers,
1523 cp_ref_qualifier ref_qualifier,
1524 tree exception_specification,
1525 tree late_return_type,
1526 tree requires_clause)
1528 cp_declarator *declarator;
1530 declarator = make_declarator (cdk_function);
1531 declarator->declarator = target;
1532 declarator->u.function.parameters = parms;
1533 declarator->u.function.qualifiers = cv_qualifiers;
1534 declarator->u.function.virt_specifiers = virt_specifiers;
1535 declarator->u.function.ref_qualifier = ref_qualifier;
1536 declarator->u.function.exception_specification = exception_specification;
1537 declarator->u.function.late_return_type = late_return_type;
1538 declarator->u.function.requires_clause = requires_clause;
1539 if (target)
1541 declarator->id_loc = target->id_loc;
1542 declarator->parameter_pack_p = target->parameter_pack_p;
1543 target->parameter_pack_p = false;
1545 else
1546 declarator->parameter_pack_p = false;
1548 return declarator;
1551 /* Make a declarator for an array of BOUNDS elements, each of which is
1552 defined by ELEMENT. */
1554 cp_declarator *
1555 make_array_declarator (cp_declarator *element, tree bounds)
1557 cp_declarator *declarator;
1559 declarator = make_declarator (cdk_array);
1560 declarator->declarator = element;
1561 declarator->u.array.bounds = bounds;
1562 if (element)
1564 declarator->id_loc = element->id_loc;
1565 declarator->parameter_pack_p = element->parameter_pack_p;
1566 element->parameter_pack_p = false;
1568 else
1569 declarator->parameter_pack_p = false;
1571 return declarator;
1574 /* Determine whether the declarator we've seen so far can be a
1575 parameter pack, when followed by an ellipsis. */
1576 static bool
1577 declarator_can_be_parameter_pack (cp_declarator *declarator)
1579 if (declarator && declarator->parameter_pack_p)
1580 /* We already saw an ellipsis. */
1581 return false;
1583 /* Search for a declarator name, or any other declarator that goes
1584 after the point where the ellipsis could appear in a parameter
1585 pack. If we find any of these, then this declarator can not be
1586 made into a parameter pack. */
1587 bool found = false;
1588 while (declarator && !found)
1590 switch ((int)declarator->kind)
1592 case cdk_id:
1593 case cdk_array:
1594 found = true;
1595 break;
1597 case cdk_error:
1598 return true;
1600 default:
1601 declarator = declarator->declarator;
1602 break;
1606 return !found;
1609 cp_parameter_declarator *no_parameters;
1611 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1612 DECLARATOR and DEFAULT_ARGUMENT. */
1614 cp_parameter_declarator *
1615 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1616 cp_declarator *declarator,
1617 tree default_argument,
1618 bool template_parameter_pack_p = false)
1620 cp_parameter_declarator *parameter;
1622 parameter = ((cp_parameter_declarator *)
1623 alloc_declarator (sizeof (cp_parameter_declarator)));
1624 parameter->next = NULL;
1625 if (decl_specifiers)
1626 parameter->decl_specifiers = *decl_specifiers;
1627 else
1628 clear_decl_specs (&parameter->decl_specifiers);
1629 parameter->declarator = declarator;
1630 parameter->default_argument = default_argument;
1631 parameter->template_parameter_pack_p = template_parameter_pack_p;
1633 return parameter;
1636 /* Returns true iff DECLARATOR is a declaration for a function. */
1638 static bool
1639 function_declarator_p (const cp_declarator *declarator)
1641 while (declarator)
1643 if (declarator->kind == cdk_function
1644 && declarator->declarator->kind == cdk_id)
1645 return true;
1646 if (declarator->kind == cdk_id
1647 || declarator->kind == cdk_error)
1648 return false;
1649 declarator = declarator->declarator;
1651 return false;
1654 /* The parser. */
1656 /* Overview
1657 --------
1659 A cp_parser parses the token stream as specified by the C++
1660 grammar. Its job is purely parsing, not semantic analysis. For
1661 example, the parser breaks the token stream into declarators,
1662 expressions, statements, and other similar syntactic constructs.
1663 It does not check that the types of the expressions on either side
1664 of an assignment-statement are compatible, or that a function is
1665 not declared with a parameter of type `void'.
1667 The parser invokes routines elsewhere in the compiler to perform
1668 semantic analysis and to build up the abstract syntax tree for the
1669 code processed.
1671 The parser (and the template instantiation code, which is, in a
1672 way, a close relative of parsing) are the only parts of the
1673 compiler that should be calling push_scope and pop_scope, or
1674 related functions. The parser (and template instantiation code)
1675 keeps track of what scope is presently active; everything else
1676 should simply honor that. (The code that generates static
1677 initializers may also need to set the scope, in order to check
1678 access control correctly when emitting the initializers.)
1680 Methodology
1681 -----------
1683 The parser is of the standard recursive-descent variety. Upcoming
1684 tokens in the token stream are examined in order to determine which
1685 production to use when parsing a non-terminal. Some C++ constructs
1686 require arbitrary look ahead to disambiguate. For example, it is
1687 impossible, in the general case, to tell whether a statement is an
1688 expression or declaration without scanning the entire statement.
1689 Therefore, the parser is capable of "parsing tentatively." When the
1690 parser is not sure what construct comes next, it enters this mode.
1691 Then, while we attempt to parse the construct, the parser queues up
1692 error messages, rather than issuing them immediately, and saves the
1693 tokens it consumes. If the construct is parsed successfully, the
1694 parser "commits", i.e., it issues any queued error messages and
1695 the tokens that were being preserved are permanently discarded.
1696 If, however, the construct is not parsed successfully, the parser
1697 rolls back its state completely so that it can resume parsing using
1698 a different alternative.
1700 Future Improvements
1701 -------------------
1703 The performance of the parser could probably be improved substantially.
1704 We could often eliminate the need to parse tentatively by looking ahead
1705 a little bit. In some places, this approach might not entirely eliminate
1706 the need to parse tentatively, but it might still speed up the average
1707 case. */
1709 /* Flags that are passed to some parsing functions. These values can
1710 be bitwise-ored together. */
1712 enum
1714 /* No flags. */
1715 CP_PARSER_FLAGS_NONE = 0x0,
1716 /* The construct is optional. If it is not present, then no error
1717 should be issued. */
1718 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1719 /* When parsing a type-specifier, treat user-defined type-names
1720 as non-type identifiers. */
1721 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1722 /* When parsing a type-specifier, do not try to parse a class-specifier
1723 or enum-specifier. */
1724 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1725 /* When parsing a decl-specifier-seq, only allow type-specifier or
1726 constexpr. */
1727 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1730 /* This type is used for parameters and variables which hold
1731 combinations of the above flags. */
1732 typedef int cp_parser_flags;
1734 /* The different kinds of declarators we want to parse. */
1736 enum cp_parser_declarator_kind
1738 /* We want an abstract declarator. */
1739 CP_PARSER_DECLARATOR_ABSTRACT,
1740 /* We want a named declarator. */
1741 CP_PARSER_DECLARATOR_NAMED,
1742 /* We don't mind, but the name must be an unqualified-id. */
1743 CP_PARSER_DECLARATOR_EITHER
1746 /* The precedence values used to parse binary expressions. The minimum value
1747 of PREC must be 1, because zero is reserved to quickly discriminate
1748 binary operators from other tokens. */
1750 enum cp_parser_prec
1752 PREC_NOT_OPERATOR,
1753 PREC_LOGICAL_OR_EXPRESSION,
1754 PREC_LOGICAL_AND_EXPRESSION,
1755 PREC_INCLUSIVE_OR_EXPRESSION,
1756 PREC_EXCLUSIVE_OR_EXPRESSION,
1757 PREC_AND_EXPRESSION,
1758 PREC_EQUALITY_EXPRESSION,
1759 PREC_RELATIONAL_EXPRESSION,
1760 PREC_SHIFT_EXPRESSION,
1761 PREC_ADDITIVE_EXPRESSION,
1762 PREC_MULTIPLICATIVE_EXPRESSION,
1763 PREC_PM_EXPRESSION,
1764 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1767 /* A mapping from a token type to a corresponding tree node type, with a
1768 precedence value. */
1770 struct cp_parser_binary_operations_map_node
1772 /* The token type. */
1773 enum cpp_ttype token_type;
1774 /* The corresponding tree code. */
1775 enum tree_code tree_type;
1776 /* The precedence of this operator. */
1777 enum cp_parser_prec prec;
1780 struct cp_parser_expression_stack_entry
1782 /* Left hand side of the binary operation we are currently
1783 parsing. */
1784 tree lhs;
1785 /* Original tree code for left hand side, if it was a binary
1786 expression itself (used for -Wparentheses). */
1787 enum tree_code lhs_type;
1788 /* Tree code for the binary operation we are parsing. */
1789 enum tree_code tree_type;
1790 /* Precedence of the binary operation we are parsing. */
1791 enum cp_parser_prec prec;
1792 /* Location of the binary operation we are parsing. */
1793 location_t loc;
1796 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1797 entries because precedence levels on the stack are monotonically
1798 increasing. */
1799 typedef struct cp_parser_expression_stack_entry
1800 cp_parser_expression_stack[NUM_PREC_VALUES];
1802 /* Prototypes. */
1804 /* Constructors and destructors. */
1806 static cp_parser_context *cp_parser_context_new
1807 (cp_parser_context *);
1809 /* Class variables. */
1811 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1813 /* The operator-precedence table used by cp_parser_binary_expression.
1814 Transformed into an associative array (binops_by_token) by
1815 cp_parser_new. */
1817 static const cp_parser_binary_operations_map_node binops[] = {
1818 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1819 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1821 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1822 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1823 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1825 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1826 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1828 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1829 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1831 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1832 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1833 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1834 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1836 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1837 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1839 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1841 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1843 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1845 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1847 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1850 /* The same as binops, but initialized by cp_parser_new so that
1851 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1852 for speed. */
1853 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1855 /* Constructors and destructors. */
1857 /* Construct a new context. The context below this one on the stack
1858 is given by NEXT. */
1860 static cp_parser_context *
1861 cp_parser_context_new (cp_parser_context* next)
1863 cp_parser_context *context;
1865 /* Allocate the storage. */
1866 if (cp_parser_context_free_list != NULL)
1868 /* Pull the first entry from the free list. */
1869 context = cp_parser_context_free_list;
1870 cp_parser_context_free_list = context->next;
1871 memset (context, 0, sizeof (*context));
1873 else
1874 context = ggc_cleared_alloc<cp_parser_context> ();
1876 /* No errors have occurred yet in this context. */
1877 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1878 /* If this is not the bottommost context, copy information that we
1879 need from the previous context. */
1880 if (next)
1882 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1883 expression, then we are parsing one in this context, too. */
1884 context->object_type = next->object_type;
1885 /* Thread the stack. */
1886 context->next = next;
1889 return context;
1892 /* Managing the unparsed function queues. */
1894 #define unparsed_funs_with_default_args \
1895 parser->unparsed_queues->last ().funs_with_default_args
1896 #define unparsed_funs_with_definitions \
1897 parser->unparsed_queues->last ().funs_with_definitions
1898 #define unparsed_nsdmis \
1899 parser->unparsed_queues->last ().nsdmis
1900 #define unparsed_classes \
1901 parser->unparsed_queues->last ().classes
1903 static void
1904 push_unparsed_function_queues (cp_parser *parser)
1906 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1907 vec_safe_push (parser->unparsed_queues, e);
1910 static void
1911 pop_unparsed_function_queues (cp_parser *parser)
1913 release_tree_vector (unparsed_funs_with_definitions);
1914 parser->unparsed_queues->pop ();
1917 /* Prototypes. */
1919 /* Constructors and destructors. */
1921 static cp_parser *cp_parser_new
1922 (void);
1924 /* Routines to parse various constructs.
1926 Those that return `tree' will return the error_mark_node (rather
1927 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1928 Sometimes, they will return an ordinary node if error-recovery was
1929 attempted, even though a parse error occurred. So, to check
1930 whether or not a parse error occurred, you should always use
1931 cp_parser_error_occurred. If the construct is optional (indicated
1932 either by an `_opt' in the name of the function that does the
1933 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1934 the construct is not present. */
1936 /* Lexical conventions [gram.lex] */
1938 static tree cp_parser_identifier
1939 (cp_parser *);
1940 static tree cp_parser_string_literal
1941 (cp_parser *, bool, bool, bool);
1942 static tree cp_parser_userdef_char_literal
1943 (cp_parser *);
1944 static tree cp_parser_userdef_string_literal
1945 (tree);
1946 static tree cp_parser_userdef_numeric_literal
1947 (cp_parser *);
1949 /* Basic concepts [gram.basic] */
1951 static bool cp_parser_translation_unit
1952 (cp_parser *);
1954 /* Expressions [gram.expr] */
1956 static tree cp_parser_primary_expression
1957 (cp_parser *, bool, bool, bool, cp_id_kind *);
1958 static tree cp_parser_id_expression
1959 (cp_parser *, bool, bool, bool *, bool, bool);
1960 static tree cp_parser_unqualified_id
1961 (cp_parser *, bool, bool, bool, bool);
1962 static tree cp_parser_nested_name_specifier_opt
1963 (cp_parser *, bool, bool, bool, bool);
1964 static tree cp_parser_nested_name_specifier
1965 (cp_parser *, bool, bool, bool, bool);
1966 static tree cp_parser_qualifying_entity
1967 (cp_parser *, bool, bool, bool, bool, bool);
1968 static tree cp_parser_postfix_expression
1969 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1970 static tree cp_parser_postfix_open_square_expression
1971 (cp_parser *, tree, bool, bool);
1972 static tree cp_parser_postfix_dot_deref_expression
1973 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1974 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1975 (cp_parser *, int, bool, bool, bool *, bool = false);
1976 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1977 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1978 static void cp_parser_pseudo_destructor_name
1979 (cp_parser *, tree, tree *, tree *);
1980 static tree cp_parser_unary_expression
1981 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
1982 static enum tree_code cp_parser_unary_operator
1983 (cp_token *);
1984 static tree cp_parser_new_expression
1985 (cp_parser *);
1986 static vec<tree, va_gc> *cp_parser_new_placement
1987 (cp_parser *);
1988 static tree cp_parser_new_type_id
1989 (cp_parser *, tree *);
1990 static cp_declarator *cp_parser_new_declarator_opt
1991 (cp_parser *);
1992 static cp_declarator *cp_parser_direct_new_declarator
1993 (cp_parser *);
1994 static vec<tree, va_gc> *cp_parser_new_initializer
1995 (cp_parser *);
1996 static tree cp_parser_delete_expression
1997 (cp_parser *);
1998 static tree cp_parser_cast_expression
1999 (cp_parser *, bool, bool, bool, cp_id_kind *);
2000 static tree cp_parser_binary_expression
2001 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2002 static tree cp_parser_question_colon_clause
2003 (cp_parser *, tree);
2004 static tree cp_parser_assignment_expression
2005 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2006 static enum tree_code cp_parser_assignment_operator_opt
2007 (cp_parser *);
2008 static tree cp_parser_expression
2009 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2010 static tree cp_parser_constant_expression
2011 (cp_parser *, bool = false, bool * = NULL);
2012 static tree cp_parser_builtin_offsetof
2013 (cp_parser *);
2014 static tree cp_parser_lambda_expression
2015 (cp_parser *);
2016 static void cp_parser_lambda_introducer
2017 (cp_parser *, tree);
2018 static bool cp_parser_lambda_declarator_opt
2019 (cp_parser *, tree);
2020 static void cp_parser_lambda_body
2021 (cp_parser *, tree);
2023 /* Statements [gram.stmt.stmt] */
2025 static void cp_parser_statement
2026 (cp_parser *, tree, bool, bool *);
2027 static void cp_parser_label_for_labeled_statement
2028 (cp_parser *, tree);
2029 static tree cp_parser_expression_statement
2030 (cp_parser *, tree);
2031 static tree cp_parser_compound_statement
2032 (cp_parser *, tree, bool, bool);
2033 static void cp_parser_statement_seq_opt
2034 (cp_parser *, tree);
2035 static tree cp_parser_selection_statement
2036 (cp_parser *, bool *);
2037 static tree cp_parser_condition
2038 (cp_parser *);
2039 static tree cp_parser_iteration_statement
2040 (cp_parser *, bool);
2041 static bool cp_parser_for_init_statement
2042 (cp_parser *, tree *decl);
2043 static tree cp_parser_for
2044 (cp_parser *, bool);
2045 static tree cp_parser_c_for
2046 (cp_parser *, tree, tree, bool);
2047 static tree cp_parser_range_for
2048 (cp_parser *, tree, tree, tree, bool);
2049 static void do_range_for_auto_deduction
2050 (tree, tree);
2051 static tree cp_parser_perform_range_for_lookup
2052 (tree, tree *, tree *);
2053 static tree cp_parser_range_for_member_function
2054 (tree, tree);
2055 static tree cp_parser_jump_statement
2056 (cp_parser *);
2057 static void cp_parser_declaration_statement
2058 (cp_parser *);
2060 static tree cp_parser_implicitly_scoped_statement
2061 (cp_parser *, bool *, const token_indent_info &);
2062 static void cp_parser_already_scoped_statement
2063 (cp_parser *, const token_indent_info &);
2065 /* Declarations [gram.dcl.dcl] */
2067 static void cp_parser_declaration_seq_opt
2068 (cp_parser *);
2069 static void cp_parser_declaration
2070 (cp_parser *);
2071 static void cp_parser_block_declaration
2072 (cp_parser *, bool);
2073 static void cp_parser_simple_declaration
2074 (cp_parser *, bool, tree *);
2075 static void cp_parser_decl_specifier_seq
2076 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2077 static tree cp_parser_storage_class_specifier_opt
2078 (cp_parser *);
2079 static tree cp_parser_function_specifier_opt
2080 (cp_parser *, cp_decl_specifier_seq *);
2081 static tree cp_parser_type_specifier
2082 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2083 int *, bool *);
2084 static tree cp_parser_simple_type_specifier
2085 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2086 static tree cp_parser_type_name
2087 (cp_parser *, bool);
2088 static tree cp_parser_type_name
2089 (cp_parser *);
2090 static tree cp_parser_nonclass_name
2091 (cp_parser* parser);
2092 static tree cp_parser_elaborated_type_specifier
2093 (cp_parser *, bool, bool);
2094 static tree cp_parser_enum_specifier
2095 (cp_parser *);
2096 static void cp_parser_enumerator_list
2097 (cp_parser *, tree);
2098 static void cp_parser_enumerator_definition
2099 (cp_parser *, tree);
2100 static tree cp_parser_namespace_name
2101 (cp_parser *);
2102 static void cp_parser_namespace_definition
2103 (cp_parser *);
2104 static void cp_parser_namespace_body
2105 (cp_parser *);
2106 static tree cp_parser_qualified_namespace_specifier
2107 (cp_parser *);
2108 static void cp_parser_namespace_alias_definition
2109 (cp_parser *);
2110 static bool cp_parser_using_declaration
2111 (cp_parser *, bool);
2112 static void cp_parser_using_directive
2113 (cp_parser *);
2114 static tree cp_parser_alias_declaration
2115 (cp_parser *);
2116 static void cp_parser_asm_definition
2117 (cp_parser *);
2118 static void cp_parser_linkage_specification
2119 (cp_parser *);
2120 static void cp_parser_static_assert
2121 (cp_parser *, bool);
2122 static tree cp_parser_decltype
2123 (cp_parser *);
2125 /* Declarators [gram.dcl.decl] */
2127 static tree cp_parser_init_declarator
2128 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2129 bool, bool, int, bool *, tree *, location_t *);
2130 static cp_declarator *cp_parser_declarator
2131 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2132 static cp_declarator *cp_parser_direct_declarator
2133 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2134 static enum tree_code cp_parser_ptr_operator
2135 (cp_parser *, tree *, cp_cv_quals *, tree *);
2136 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2137 (cp_parser *);
2138 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2139 (cp_parser *);
2140 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2141 (cp_parser *);
2142 static tree cp_parser_late_return_type_opt
2143 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2144 static tree cp_parser_declarator_id
2145 (cp_parser *, bool);
2146 static tree cp_parser_type_id
2147 (cp_parser *);
2148 static tree cp_parser_template_type_arg
2149 (cp_parser *);
2150 static tree cp_parser_trailing_type_id (cp_parser *);
2151 static tree cp_parser_type_id_1
2152 (cp_parser *, bool, bool);
2153 static void cp_parser_type_specifier_seq
2154 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2155 static tree cp_parser_parameter_declaration_clause
2156 (cp_parser *);
2157 static tree cp_parser_parameter_declaration_list
2158 (cp_parser *, bool *);
2159 static cp_parameter_declarator *cp_parser_parameter_declaration
2160 (cp_parser *, bool, bool *);
2161 static tree cp_parser_default_argument
2162 (cp_parser *, bool);
2163 static void cp_parser_function_body
2164 (cp_parser *, bool);
2165 static tree cp_parser_initializer
2166 (cp_parser *, bool *, bool *);
2167 static tree cp_parser_initializer_clause
2168 (cp_parser *, bool *);
2169 static tree cp_parser_braced_list
2170 (cp_parser*, bool*);
2171 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2172 (cp_parser *, bool *);
2174 static bool cp_parser_ctor_initializer_opt_and_function_body
2175 (cp_parser *, bool);
2177 static tree cp_parser_late_parsing_omp_declare_simd
2178 (cp_parser *, tree);
2180 static tree cp_parser_late_parsing_cilk_simd_fn_info
2181 (cp_parser *, tree);
2183 static tree synthesize_implicit_template_parm
2184 (cp_parser *, tree);
2185 static tree finish_fully_implicit_template
2186 (cp_parser *, tree);
2188 /* Classes [gram.class] */
2190 static tree cp_parser_class_name
2191 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2192 static tree cp_parser_class_specifier
2193 (cp_parser *);
2194 static tree cp_parser_class_head
2195 (cp_parser *, bool *);
2196 static enum tag_types cp_parser_class_key
2197 (cp_parser *);
2198 static void cp_parser_type_parameter_key
2199 (cp_parser* parser);
2200 static void cp_parser_member_specification_opt
2201 (cp_parser *);
2202 static void cp_parser_member_declaration
2203 (cp_parser *);
2204 static tree cp_parser_pure_specifier
2205 (cp_parser *);
2206 static tree cp_parser_constant_initializer
2207 (cp_parser *);
2209 /* Derived classes [gram.class.derived] */
2211 static tree cp_parser_base_clause
2212 (cp_parser *);
2213 static tree cp_parser_base_specifier
2214 (cp_parser *);
2216 /* Special member functions [gram.special] */
2218 static tree cp_parser_conversion_function_id
2219 (cp_parser *);
2220 static tree cp_parser_conversion_type_id
2221 (cp_parser *);
2222 static cp_declarator *cp_parser_conversion_declarator_opt
2223 (cp_parser *);
2224 static bool cp_parser_ctor_initializer_opt
2225 (cp_parser *);
2226 static void cp_parser_mem_initializer_list
2227 (cp_parser *);
2228 static tree cp_parser_mem_initializer
2229 (cp_parser *);
2230 static tree cp_parser_mem_initializer_id
2231 (cp_parser *);
2233 /* Overloading [gram.over] */
2235 static tree cp_parser_operator_function_id
2236 (cp_parser *);
2237 static tree cp_parser_operator
2238 (cp_parser *);
2240 /* Templates [gram.temp] */
2242 static void cp_parser_template_declaration
2243 (cp_parser *, bool);
2244 static tree cp_parser_template_parameter_list
2245 (cp_parser *);
2246 static tree cp_parser_template_parameter
2247 (cp_parser *, bool *, bool *);
2248 static tree cp_parser_type_parameter
2249 (cp_parser *, bool *);
2250 static tree cp_parser_template_id
2251 (cp_parser *, bool, bool, enum tag_types, bool);
2252 static tree cp_parser_template_name
2253 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2254 static tree cp_parser_template_argument_list
2255 (cp_parser *);
2256 static tree cp_parser_template_argument
2257 (cp_parser *);
2258 static void cp_parser_explicit_instantiation
2259 (cp_parser *);
2260 static void cp_parser_explicit_specialization
2261 (cp_parser *);
2263 /* Exception handling [gram.exception] */
2265 static tree cp_parser_try_block
2266 (cp_parser *);
2267 static bool cp_parser_function_try_block
2268 (cp_parser *);
2269 static void cp_parser_handler_seq
2270 (cp_parser *);
2271 static void cp_parser_handler
2272 (cp_parser *);
2273 static tree cp_parser_exception_declaration
2274 (cp_parser *);
2275 static tree cp_parser_throw_expression
2276 (cp_parser *);
2277 static tree cp_parser_exception_specification_opt
2278 (cp_parser *);
2279 static tree cp_parser_type_id_list
2280 (cp_parser *);
2282 /* GNU Extensions */
2284 static tree cp_parser_asm_specification_opt
2285 (cp_parser *);
2286 static tree cp_parser_asm_operand_list
2287 (cp_parser *);
2288 static tree cp_parser_asm_clobber_list
2289 (cp_parser *);
2290 static tree cp_parser_asm_label_list
2291 (cp_parser *);
2292 static bool cp_next_tokens_can_be_attribute_p
2293 (cp_parser *);
2294 static bool cp_next_tokens_can_be_gnu_attribute_p
2295 (cp_parser *);
2296 static bool cp_next_tokens_can_be_std_attribute_p
2297 (cp_parser *);
2298 static bool cp_nth_tokens_can_be_std_attribute_p
2299 (cp_parser *, size_t);
2300 static bool cp_nth_tokens_can_be_gnu_attribute_p
2301 (cp_parser *, size_t);
2302 static bool cp_nth_tokens_can_be_attribute_p
2303 (cp_parser *, size_t);
2304 static tree cp_parser_attributes_opt
2305 (cp_parser *);
2306 static tree cp_parser_gnu_attributes_opt
2307 (cp_parser *);
2308 static tree cp_parser_gnu_attribute_list
2309 (cp_parser *);
2310 static tree cp_parser_std_attribute
2311 (cp_parser *);
2312 static tree cp_parser_std_attribute_spec
2313 (cp_parser *);
2314 static tree cp_parser_std_attribute_spec_seq
2315 (cp_parser *);
2316 static bool cp_parser_extension_opt
2317 (cp_parser *, int *);
2318 static void cp_parser_label_declaration
2319 (cp_parser *);
2321 /* Concept Extensions */
2323 static tree cp_parser_requires_clause
2324 (cp_parser *);
2325 static tree cp_parser_requires_clause_opt
2326 (cp_parser *);
2327 static tree cp_parser_requires_expression
2328 (cp_parser *);
2329 static tree cp_parser_requirement_parameter_list
2330 (cp_parser *);
2331 static tree cp_parser_requirement_body
2332 (cp_parser *);
2333 static tree cp_parser_requirement_list
2334 (cp_parser *);
2335 static tree cp_parser_requirement
2336 (cp_parser *);
2337 static tree cp_parser_simple_requirement
2338 (cp_parser *);
2339 static tree cp_parser_compound_requirement
2340 (cp_parser *);
2341 static tree cp_parser_type_requirement
2342 (cp_parser *);
2343 static tree cp_parser_nested_requirement
2344 (cp_parser *);
2346 /* Transactional Memory Extensions */
2348 static tree cp_parser_transaction
2349 (cp_parser *, enum rid);
2350 static tree cp_parser_transaction_expression
2351 (cp_parser *, enum rid);
2352 static bool cp_parser_function_transaction
2353 (cp_parser *, enum rid);
2354 static tree cp_parser_transaction_cancel
2355 (cp_parser *);
2357 enum pragma_context {
2358 pragma_external,
2359 pragma_member,
2360 pragma_objc_icode,
2361 pragma_stmt,
2362 pragma_compound
2364 static bool cp_parser_pragma
2365 (cp_parser *, enum pragma_context);
2367 /* Objective-C++ Productions */
2369 static tree cp_parser_objc_message_receiver
2370 (cp_parser *);
2371 static tree cp_parser_objc_message_args
2372 (cp_parser *);
2373 static tree cp_parser_objc_message_expression
2374 (cp_parser *);
2375 static tree cp_parser_objc_encode_expression
2376 (cp_parser *);
2377 static tree cp_parser_objc_defs_expression
2378 (cp_parser *);
2379 static tree cp_parser_objc_protocol_expression
2380 (cp_parser *);
2381 static tree cp_parser_objc_selector_expression
2382 (cp_parser *);
2383 static tree cp_parser_objc_expression
2384 (cp_parser *);
2385 static bool cp_parser_objc_selector_p
2386 (enum cpp_ttype);
2387 static tree cp_parser_objc_selector
2388 (cp_parser *);
2389 static tree cp_parser_objc_protocol_refs_opt
2390 (cp_parser *);
2391 static void cp_parser_objc_declaration
2392 (cp_parser *, tree);
2393 static tree cp_parser_objc_statement
2394 (cp_parser *);
2395 static bool cp_parser_objc_valid_prefix_attributes
2396 (cp_parser *, tree *);
2397 static void cp_parser_objc_at_property_declaration
2398 (cp_parser *) ;
2399 static void cp_parser_objc_at_synthesize_declaration
2400 (cp_parser *) ;
2401 static void cp_parser_objc_at_dynamic_declaration
2402 (cp_parser *) ;
2403 static tree cp_parser_objc_struct_declaration
2404 (cp_parser *) ;
2406 /* Utility Routines */
2408 static tree cp_parser_lookup_name
2409 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2410 static tree cp_parser_lookup_name_simple
2411 (cp_parser *, tree, location_t);
2412 static tree cp_parser_maybe_treat_template_as_class
2413 (tree, bool);
2414 static bool cp_parser_check_declarator_template_parameters
2415 (cp_parser *, cp_declarator *, location_t);
2416 static bool cp_parser_check_template_parameters
2417 (cp_parser *, unsigned, location_t, cp_declarator *);
2418 static tree cp_parser_simple_cast_expression
2419 (cp_parser *);
2420 static tree cp_parser_global_scope_opt
2421 (cp_parser *, bool);
2422 static bool cp_parser_constructor_declarator_p
2423 (cp_parser *, bool);
2424 static tree cp_parser_function_definition_from_specifiers_and_declarator
2425 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2426 static tree cp_parser_function_definition_after_declarator
2427 (cp_parser *, bool);
2428 static bool cp_parser_template_declaration_after_export
2429 (cp_parser *, bool);
2430 static void cp_parser_perform_template_parameter_access_checks
2431 (vec<deferred_access_check, va_gc> *);
2432 static tree cp_parser_single_declaration
2433 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2434 static tree cp_parser_functional_cast
2435 (cp_parser *, tree);
2436 static tree cp_parser_save_member_function_body
2437 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2438 static tree cp_parser_save_nsdmi
2439 (cp_parser *);
2440 static tree cp_parser_enclosed_template_argument_list
2441 (cp_parser *);
2442 static void cp_parser_save_default_args
2443 (cp_parser *, tree);
2444 static void cp_parser_late_parsing_for_member
2445 (cp_parser *, tree);
2446 static tree cp_parser_late_parse_one_default_arg
2447 (cp_parser *, tree, tree, tree);
2448 static void cp_parser_late_parsing_nsdmi
2449 (cp_parser *, tree);
2450 static void cp_parser_late_parsing_default_args
2451 (cp_parser *, tree);
2452 static tree cp_parser_sizeof_operand
2453 (cp_parser *, enum rid);
2454 static tree cp_parser_trait_expr
2455 (cp_parser *, enum rid);
2456 static bool cp_parser_declares_only_class_p
2457 (cp_parser *);
2458 static void cp_parser_set_storage_class
2459 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2460 static void cp_parser_set_decl_spec_type
2461 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2462 static void set_and_check_decl_spec_loc
2463 (cp_decl_specifier_seq *decl_specs,
2464 cp_decl_spec ds, cp_token *);
2465 static bool cp_parser_friend_p
2466 (const cp_decl_specifier_seq *);
2467 static void cp_parser_required_error
2468 (cp_parser *, required_token, bool);
2469 static cp_token *cp_parser_require
2470 (cp_parser *, enum cpp_ttype, required_token);
2471 static cp_token *cp_parser_require_keyword
2472 (cp_parser *, enum rid, required_token);
2473 static bool cp_parser_token_starts_function_definition_p
2474 (cp_token *);
2475 static bool cp_parser_next_token_starts_class_definition_p
2476 (cp_parser *);
2477 static bool cp_parser_next_token_ends_template_argument_p
2478 (cp_parser *);
2479 static bool cp_parser_nth_token_starts_template_argument_list_p
2480 (cp_parser *, size_t);
2481 static enum tag_types cp_parser_token_is_class_key
2482 (cp_token *);
2483 static enum tag_types cp_parser_token_is_type_parameter_key
2484 (cp_token *);
2485 static void cp_parser_check_class_key
2486 (enum tag_types, tree type);
2487 static void cp_parser_check_access_in_redeclaration
2488 (tree type, location_t location);
2489 static bool cp_parser_optional_template_keyword
2490 (cp_parser *);
2491 static void cp_parser_pre_parsed_nested_name_specifier
2492 (cp_parser *);
2493 static bool cp_parser_cache_group
2494 (cp_parser *, enum cpp_ttype, unsigned);
2495 static tree cp_parser_cache_defarg
2496 (cp_parser *parser, bool nsdmi);
2497 static void cp_parser_parse_tentatively
2498 (cp_parser *);
2499 static void cp_parser_commit_to_tentative_parse
2500 (cp_parser *);
2501 static void cp_parser_commit_to_topmost_tentative_parse
2502 (cp_parser *);
2503 static void cp_parser_abort_tentative_parse
2504 (cp_parser *);
2505 static bool cp_parser_parse_definitely
2506 (cp_parser *);
2507 static inline bool cp_parser_parsing_tentatively
2508 (cp_parser *);
2509 static bool cp_parser_uncommitted_to_tentative_parse_p
2510 (cp_parser *);
2511 static void cp_parser_error
2512 (cp_parser *, const char *);
2513 static void cp_parser_name_lookup_error
2514 (cp_parser *, tree, tree, name_lookup_error, location_t);
2515 static bool cp_parser_simulate_error
2516 (cp_parser *);
2517 static bool cp_parser_check_type_definition
2518 (cp_parser *);
2519 static void cp_parser_check_for_definition_in_return_type
2520 (cp_declarator *, tree, location_t type_location);
2521 static void cp_parser_check_for_invalid_template_id
2522 (cp_parser *, tree, enum tag_types, location_t location);
2523 static bool cp_parser_non_integral_constant_expression
2524 (cp_parser *, non_integral_constant);
2525 static void cp_parser_diagnose_invalid_type_name
2526 (cp_parser *, tree, location_t);
2527 static bool cp_parser_parse_and_diagnose_invalid_type_name
2528 (cp_parser *);
2529 static int cp_parser_skip_to_closing_parenthesis
2530 (cp_parser *, bool, bool, bool);
2531 static void cp_parser_skip_to_end_of_statement
2532 (cp_parser *);
2533 static void cp_parser_consume_semicolon_at_end_of_statement
2534 (cp_parser *);
2535 static void cp_parser_skip_to_end_of_block_or_statement
2536 (cp_parser *);
2537 static bool cp_parser_skip_to_closing_brace
2538 (cp_parser *);
2539 static void cp_parser_skip_to_end_of_template_parameter_list
2540 (cp_parser *);
2541 static void cp_parser_skip_to_pragma_eol
2542 (cp_parser*, cp_token *);
2543 static bool cp_parser_error_occurred
2544 (cp_parser *);
2545 static bool cp_parser_allow_gnu_extensions_p
2546 (cp_parser *);
2547 static bool cp_parser_is_pure_string_literal
2548 (cp_token *);
2549 static bool cp_parser_is_string_literal
2550 (cp_token *);
2551 static bool cp_parser_is_keyword
2552 (cp_token *, enum rid);
2553 static tree cp_parser_make_typename_type
2554 (cp_parser *, tree, location_t location);
2555 static cp_declarator * cp_parser_make_indirect_declarator
2556 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2557 static bool cp_parser_compound_literal_p
2558 (cp_parser *);
2559 static bool cp_parser_array_designator_p
2560 (cp_parser *);
2561 static bool cp_parser_skip_to_closing_square_bracket
2562 (cp_parser *);
2564 /* Concept-related syntactic transformations */
2566 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2567 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2569 // -------------------------------------------------------------------------- //
2570 // Unevaluated Operand Guard
2572 // Implementation of an RAII helper for unevaluated operand parsing.
2573 cp_unevaluated::cp_unevaluated ()
2575 ++cp_unevaluated_operand;
2576 ++c_inhibit_evaluation_warnings;
2579 cp_unevaluated::~cp_unevaluated ()
2581 --c_inhibit_evaluation_warnings;
2582 --cp_unevaluated_operand;
2585 // -------------------------------------------------------------------------- //
2586 // Tentative Parsing
2588 /* Returns nonzero if we are parsing tentatively. */
2590 static inline bool
2591 cp_parser_parsing_tentatively (cp_parser* parser)
2593 return parser->context->next != NULL;
2596 /* Returns nonzero if TOKEN is a string literal. */
2598 static bool
2599 cp_parser_is_pure_string_literal (cp_token* token)
2601 return (token->type == CPP_STRING ||
2602 token->type == CPP_STRING16 ||
2603 token->type == CPP_STRING32 ||
2604 token->type == CPP_WSTRING ||
2605 token->type == CPP_UTF8STRING);
2608 /* Returns nonzero if TOKEN is a string literal
2609 of a user-defined string literal. */
2611 static bool
2612 cp_parser_is_string_literal (cp_token* token)
2614 return (cp_parser_is_pure_string_literal (token) ||
2615 token->type == CPP_STRING_USERDEF ||
2616 token->type == CPP_STRING16_USERDEF ||
2617 token->type == CPP_STRING32_USERDEF ||
2618 token->type == CPP_WSTRING_USERDEF ||
2619 token->type == CPP_UTF8STRING_USERDEF);
2622 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2624 static bool
2625 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2627 return token->keyword == keyword;
2630 /* If not parsing tentatively, issue a diagnostic of the form
2631 FILE:LINE: MESSAGE before TOKEN
2632 where TOKEN is the next token in the input stream. MESSAGE
2633 (specified by the caller) is usually of the form "expected
2634 OTHER-TOKEN". */
2636 static void
2637 cp_parser_error (cp_parser* parser, const char* gmsgid)
2639 if (!cp_parser_simulate_error (parser))
2641 cp_token *token = cp_lexer_peek_token (parser->lexer);
2642 /* This diagnostic makes more sense if it is tagged to the line
2643 of the token we just peeked at. */
2644 cp_lexer_set_source_position_from_token (token);
2646 if (token->type == CPP_PRAGMA)
2648 error_at (token->location,
2649 "%<#pragma%> is not allowed here");
2650 cp_parser_skip_to_pragma_eol (parser, token);
2651 return;
2654 c_parse_error (gmsgid,
2655 /* Because c_parser_error does not understand
2656 CPP_KEYWORD, keywords are treated like
2657 identifiers. */
2658 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2659 token->u.value, token->flags);
2663 /* Issue an error about name-lookup failing. NAME is the
2664 IDENTIFIER_NODE DECL is the result of
2665 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2666 the thing that we hoped to find. */
2668 static void
2669 cp_parser_name_lookup_error (cp_parser* parser,
2670 tree name,
2671 tree decl,
2672 name_lookup_error desired,
2673 location_t location)
2675 /* If name lookup completely failed, tell the user that NAME was not
2676 declared. */
2677 if (decl == error_mark_node)
2679 if (parser->scope && parser->scope != global_namespace)
2680 error_at (location, "%<%E::%E%> has not been declared",
2681 parser->scope, name);
2682 else if (parser->scope == global_namespace)
2683 error_at (location, "%<::%E%> has not been declared", name);
2684 else if (parser->object_scope
2685 && !CLASS_TYPE_P (parser->object_scope))
2686 error_at (location, "request for member %qE in non-class type %qT",
2687 name, parser->object_scope);
2688 else if (parser->object_scope)
2689 error_at (location, "%<%T::%E%> has not been declared",
2690 parser->object_scope, name);
2691 else
2692 error_at (location, "%qE has not been declared", name);
2694 else if (parser->scope && parser->scope != global_namespace)
2696 switch (desired)
2698 case NLE_TYPE:
2699 error_at (location, "%<%E::%E%> is not a type",
2700 parser->scope, name);
2701 break;
2702 case NLE_CXX98:
2703 error_at (location, "%<%E::%E%> is not a class or namespace",
2704 parser->scope, name);
2705 break;
2706 case NLE_NOT_CXX98:
2707 error_at (location,
2708 "%<%E::%E%> is not a class, namespace, or enumeration",
2709 parser->scope, name);
2710 break;
2711 default:
2712 gcc_unreachable ();
2716 else if (parser->scope == global_namespace)
2718 switch (desired)
2720 case NLE_TYPE:
2721 error_at (location, "%<::%E%> is not a type", name);
2722 break;
2723 case NLE_CXX98:
2724 error_at (location, "%<::%E%> is not a class or namespace", name);
2725 break;
2726 case NLE_NOT_CXX98:
2727 error_at (location,
2728 "%<::%E%> is not a class, namespace, or enumeration",
2729 name);
2730 break;
2731 default:
2732 gcc_unreachable ();
2735 else
2737 switch (desired)
2739 case NLE_TYPE:
2740 error_at (location, "%qE is not a type", name);
2741 break;
2742 case NLE_CXX98:
2743 error_at (location, "%qE is not a class or namespace", name);
2744 break;
2745 case NLE_NOT_CXX98:
2746 error_at (location,
2747 "%qE is not a class, namespace, or enumeration", name);
2748 break;
2749 default:
2750 gcc_unreachable ();
2755 /* If we are parsing tentatively, remember that an error has occurred
2756 during this tentative parse. Returns true if the error was
2757 simulated; false if a message should be issued by the caller. */
2759 static bool
2760 cp_parser_simulate_error (cp_parser* parser)
2762 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2764 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2765 return true;
2767 return false;
2770 /* This function is called when a type is defined. If type
2771 definitions are forbidden at this point, an error message is
2772 issued. */
2774 static bool
2775 cp_parser_check_type_definition (cp_parser* parser)
2777 /* If types are forbidden here, issue a message. */
2778 if (parser->type_definition_forbidden_message)
2780 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2781 in the message need to be interpreted. */
2782 error (parser->type_definition_forbidden_message);
2783 return false;
2785 return true;
2788 /* This function is called when the DECLARATOR is processed. The TYPE
2789 was a type defined in the decl-specifiers. If it is invalid to
2790 define a type in the decl-specifiers for DECLARATOR, an error is
2791 issued. TYPE_LOCATION is the location of TYPE and is used
2792 for error reporting. */
2794 static void
2795 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2796 tree type, location_t type_location)
2798 /* [dcl.fct] forbids type definitions in return types.
2799 Unfortunately, it's not easy to know whether or not we are
2800 processing a return type until after the fact. */
2801 while (declarator
2802 && (declarator->kind == cdk_pointer
2803 || declarator->kind == cdk_reference
2804 || declarator->kind == cdk_ptrmem))
2805 declarator = declarator->declarator;
2806 if (declarator
2807 && declarator->kind == cdk_function)
2809 error_at (type_location,
2810 "new types may not be defined in a return type");
2811 inform (type_location,
2812 "(perhaps a semicolon is missing after the definition of %qT)",
2813 type);
2817 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2818 "<" in any valid C++ program. If the next token is indeed "<",
2819 issue a message warning the user about what appears to be an
2820 invalid attempt to form a template-id. LOCATION is the location
2821 of the type-specifier (TYPE) */
2823 static void
2824 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2825 tree type,
2826 enum tag_types tag_type,
2827 location_t location)
2829 cp_token_position start = 0;
2831 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2833 if (TYPE_P (type))
2834 error_at (location, "%qT is not a template", type);
2835 else if (identifier_p (type))
2837 if (tag_type != none_type)
2838 error_at (location, "%qE is not a class template", type);
2839 else
2840 error_at (location, "%qE is not a template", type);
2842 else
2843 error_at (location, "invalid template-id");
2844 /* Remember the location of the invalid "<". */
2845 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2846 start = cp_lexer_token_position (parser->lexer, true);
2847 /* Consume the "<". */
2848 cp_lexer_consume_token (parser->lexer);
2849 /* Parse the template arguments. */
2850 cp_parser_enclosed_template_argument_list (parser);
2851 /* Permanently remove the invalid template arguments so that
2852 this error message is not issued again. */
2853 if (start)
2854 cp_lexer_purge_tokens_after (parser->lexer, start);
2858 /* If parsing an integral constant-expression, issue an error message
2859 about the fact that THING appeared and return true. Otherwise,
2860 return false. In either case, set
2861 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2863 static bool
2864 cp_parser_non_integral_constant_expression (cp_parser *parser,
2865 non_integral_constant thing)
2867 parser->non_integral_constant_expression_p = true;
2868 if (parser->integral_constant_expression_p)
2870 if (!parser->allow_non_integral_constant_expression_p)
2872 const char *msg = NULL;
2873 switch (thing)
2875 case NIC_FLOAT:
2876 error ("floating-point literal "
2877 "cannot appear in a constant-expression");
2878 return true;
2879 case NIC_CAST:
2880 error ("a cast to a type other than an integral or "
2881 "enumeration type cannot appear in a "
2882 "constant-expression");
2883 return true;
2884 case NIC_TYPEID:
2885 error ("%<typeid%> operator "
2886 "cannot appear in a constant-expression");
2887 return true;
2888 case NIC_NCC:
2889 error ("non-constant compound literals "
2890 "cannot appear in a constant-expression");
2891 return true;
2892 case NIC_FUNC_CALL:
2893 error ("a function call "
2894 "cannot appear in a constant-expression");
2895 return true;
2896 case NIC_INC:
2897 error ("an increment "
2898 "cannot appear in a constant-expression");
2899 return true;
2900 case NIC_DEC:
2901 error ("an decrement "
2902 "cannot appear in a constant-expression");
2903 return true;
2904 case NIC_ARRAY_REF:
2905 error ("an array reference "
2906 "cannot appear in a constant-expression");
2907 return true;
2908 case NIC_ADDR_LABEL:
2909 error ("the address of a label "
2910 "cannot appear in a constant-expression");
2911 return true;
2912 case NIC_OVERLOADED:
2913 error ("calls to overloaded operators "
2914 "cannot appear in a constant-expression");
2915 return true;
2916 case NIC_ASSIGNMENT:
2917 error ("an assignment cannot appear in a constant-expression");
2918 return true;
2919 case NIC_COMMA:
2920 error ("a comma operator "
2921 "cannot appear in a constant-expression");
2922 return true;
2923 case NIC_CONSTRUCTOR:
2924 error ("a call to a constructor "
2925 "cannot appear in a constant-expression");
2926 return true;
2927 case NIC_TRANSACTION:
2928 error ("a transaction expression "
2929 "cannot appear in a constant-expression");
2930 return true;
2931 case NIC_THIS:
2932 msg = "this";
2933 break;
2934 case NIC_FUNC_NAME:
2935 msg = "__FUNCTION__";
2936 break;
2937 case NIC_PRETTY_FUNC:
2938 msg = "__PRETTY_FUNCTION__";
2939 break;
2940 case NIC_C99_FUNC:
2941 msg = "__func__";
2942 break;
2943 case NIC_VA_ARG:
2944 msg = "va_arg";
2945 break;
2946 case NIC_ARROW:
2947 msg = "->";
2948 break;
2949 case NIC_POINT:
2950 msg = ".";
2951 break;
2952 case NIC_STAR:
2953 msg = "*";
2954 break;
2955 case NIC_ADDR:
2956 msg = "&";
2957 break;
2958 case NIC_PREINCREMENT:
2959 msg = "++";
2960 break;
2961 case NIC_PREDECREMENT:
2962 msg = "--";
2963 break;
2964 case NIC_NEW:
2965 msg = "new";
2966 break;
2967 case NIC_DEL:
2968 msg = "delete";
2969 break;
2970 default:
2971 gcc_unreachable ();
2973 if (msg)
2974 error ("%qs cannot appear in a constant-expression", msg);
2975 return true;
2978 return false;
2981 /* Emit a diagnostic for an invalid type name. This function commits
2982 to the current active tentative parse, if any. (Otherwise, the
2983 problematic construct might be encountered again later, resulting
2984 in duplicate error messages.) LOCATION is the location of ID. */
2986 static void
2987 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
2988 location_t location)
2990 tree decl, ambiguous_decls;
2991 cp_parser_commit_to_tentative_parse (parser);
2992 /* Try to lookup the identifier. */
2993 decl = cp_parser_lookup_name (parser, id, none_type,
2994 /*is_template=*/false,
2995 /*is_namespace=*/false,
2996 /*check_dependency=*/true,
2997 &ambiguous_decls, location);
2998 if (ambiguous_decls)
2999 /* If the lookup was ambiguous, an error will already have
3000 been issued. */
3001 return;
3002 /* If the lookup found a template-name, it means that the user forgot
3003 to specify an argument list. Emit a useful error message. */
3004 if (DECL_TYPE_TEMPLATE_P (decl))
3006 error_at (location,
3007 "invalid use of template-name %qE without an argument list",
3008 decl);
3009 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3011 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3012 error_at (location, "invalid use of destructor %qD as a type", id);
3013 else if (TREE_CODE (decl) == TYPE_DECL)
3014 /* Something like 'unsigned A a;' */
3015 error_at (location, "invalid combination of multiple type-specifiers");
3016 else if (!parser->scope)
3018 /* Issue an error message. */
3019 error_at (location, "%qE does not name a type", id);
3020 /* If we're in a template class, it's possible that the user was
3021 referring to a type from a base class. For example:
3023 template <typename T> struct A { typedef T X; };
3024 template <typename T> struct B : public A<T> { X x; };
3026 The user should have said "typename A<T>::X". */
3027 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3028 inform (location, "C++11 %<constexpr%> only available with "
3029 "-std=c++11 or -std=gnu++11");
3030 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3031 inform (location, "C++11 %<noexcept%> only available with "
3032 "-std=c++11 or -std=gnu++11");
3033 else if (cxx_dialect < cxx11
3034 && TREE_CODE (id) == IDENTIFIER_NODE
3035 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
3036 inform (location, "C++11 %<thread_local%> only available with "
3037 "-std=c++11 or -std=gnu++11");
3038 else if (processing_template_decl && current_class_type
3039 && TYPE_BINFO (current_class_type))
3041 tree b;
3043 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3045 b = TREE_CHAIN (b))
3047 tree base_type = BINFO_TYPE (b);
3048 if (CLASS_TYPE_P (base_type)
3049 && dependent_type_p (base_type))
3051 tree field;
3052 /* Go from a particular instantiation of the
3053 template (which will have an empty TYPE_FIELDs),
3054 to the main version. */
3055 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3056 for (field = TYPE_FIELDS (base_type);
3057 field;
3058 field = DECL_CHAIN (field))
3059 if (TREE_CODE (field) == TYPE_DECL
3060 && DECL_NAME (field) == id)
3062 inform (location,
3063 "(perhaps %<typename %T::%E%> was intended)",
3064 BINFO_TYPE (b), id);
3065 break;
3067 if (field)
3068 break;
3073 /* Here we diagnose qualified-ids where the scope is actually correct,
3074 but the identifier does not resolve to a valid type name. */
3075 else if (parser->scope != error_mark_node)
3077 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3079 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3080 error_at (location_of (id),
3081 "%qE in namespace %qE does not name a template type",
3082 id, parser->scope);
3083 else
3084 error_at (location_of (id),
3085 "%qE in namespace %qE does not name a type",
3086 id, parser->scope);
3087 if (DECL_P (decl))
3088 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3090 else if (CLASS_TYPE_P (parser->scope)
3091 && constructor_name_p (id, parser->scope))
3093 /* A<T>::A<T>() */
3094 error_at (location, "%<%T::%E%> names the constructor, not"
3095 " the type", parser->scope, id);
3096 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3097 error_at (location, "and %qT has no template constructors",
3098 parser->scope);
3100 else if (TYPE_P (parser->scope)
3101 && dependent_scope_p (parser->scope))
3102 error_at (location, "need %<typename%> before %<%T::%E%> because "
3103 "%qT is a dependent scope",
3104 parser->scope, id, parser->scope);
3105 else if (TYPE_P (parser->scope))
3107 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3108 error_at (location_of (id),
3109 "%qE in %q#T does not name a template type",
3110 id, parser->scope);
3111 else
3112 error_at (location_of (id),
3113 "%qE in %q#T does not name a type",
3114 id, parser->scope);
3115 if (DECL_P (decl))
3116 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3118 else
3119 gcc_unreachable ();
3123 /* Check for a common situation where a type-name should be present,
3124 but is not, and issue a sensible error message. Returns true if an
3125 invalid type-name was detected.
3127 The situation handled by this function are variable declarations of the
3128 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3129 Usually, `ID' should name a type, but if we got here it means that it
3130 does not. We try to emit the best possible error message depending on
3131 how exactly the id-expression looks like. */
3133 static bool
3134 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3136 tree id;
3137 cp_token *token = cp_lexer_peek_token (parser->lexer);
3139 /* Avoid duplicate error about ambiguous lookup. */
3140 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3142 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3143 if (next->type == CPP_NAME && next->error_reported)
3144 goto out;
3147 cp_parser_parse_tentatively (parser);
3148 id = cp_parser_id_expression (parser,
3149 /*template_keyword_p=*/false,
3150 /*check_dependency_p=*/true,
3151 /*template_p=*/NULL,
3152 /*declarator_p=*/true,
3153 /*optional_p=*/false);
3154 /* If the next token is a (, this is a function with no explicit return
3155 type, i.e. constructor, destructor or conversion op. */
3156 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3157 || TREE_CODE (id) == TYPE_DECL)
3159 cp_parser_abort_tentative_parse (parser);
3160 return false;
3162 if (!cp_parser_parse_definitely (parser))
3163 return false;
3165 /* Emit a diagnostic for the invalid type. */
3166 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3167 out:
3168 /* If we aren't in the middle of a declarator (i.e. in a
3169 parameter-declaration-clause), skip to the end of the declaration;
3170 there's no point in trying to process it. */
3171 if (!parser->in_declarator_p)
3172 cp_parser_skip_to_end_of_block_or_statement (parser);
3173 return true;
3176 /* Consume tokens up to, and including, the next non-nested closing `)'.
3177 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3178 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3179 found an unnested token of that type. */
3181 static int
3182 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3183 bool recovering,
3184 cpp_ttype or_ttype,
3185 bool consume_paren)
3187 unsigned paren_depth = 0;
3188 unsigned brace_depth = 0;
3189 unsigned square_depth = 0;
3191 if (recovering && or_ttype == CPP_EOF
3192 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3193 return 0;
3195 while (true)
3197 cp_token * token = cp_lexer_peek_token (parser->lexer);
3199 /* Have we found what we're looking for before the closing paren? */
3200 if (token->type == or_ttype && or_ttype != CPP_EOF
3201 && !brace_depth && !paren_depth && !square_depth)
3202 return -1;
3204 switch (token->type)
3206 case CPP_EOF:
3207 case CPP_PRAGMA_EOL:
3208 /* If we've run out of tokens, then there is no closing `)'. */
3209 return 0;
3211 /* This is good for lambda expression capture-lists. */
3212 case CPP_OPEN_SQUARE:
3213 ++square_depth;
3214 break;
3215 case CPP_CLOSE_SQUARE:
3216 if (!square_depth--)
3217 return 0;
3218 break;
3220 case CPP_SEMICOLON:
3221 /* This matches the processing in skip_to_end_of_statement. */
3222 if (!brace_depth)
3223 return 0;
3224 break;
3226 case CPP_OPEN_BRACE:
3227 ++brace_depth;
3228 break;
3229 case CPP_CLOSE_BRACE:
3230 if (!brace_depth--)
3231 return 0;
3232 break;
3234 case CPP_OPEN_PAREN:
3235 if (!brace_depth)
3236 ++paren_depth;
3237 break;
3239 case CPP_CLOSE_PAREN:
3240 if (!brace_depth && !paren_depth--)
3242 if (consume_paren)
3243 cp_lexer_consume_token (parser->lexer);
3244 return 1;
3246 break;
3248 default:
3249 break;
3252 /* Consume the token. */
3253 cp_lexer_consume_token (parser->lexer);
3257 /* Consume tokens up to, and including, the next non-nested closing `)'.
3258 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3259 are doing error recovery. Returns -1 if OR_COMMA is true and we
3260 found an unnested token of that type. */
3262 static int
3263 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3264 bool recovering,
3265 bool or_comma,
3266 bool consume_paren)
3268 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3269 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3270 ttype, consume_paren);
3273 /* Consume tokens until we reach the end of the current statement.
3274 Normally, that will be just before consuming a `;'. However, if a
3275 non-nested `}' comes first, then we stop before consuming that. */
3277 static void
3278 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3280 unsigned nesting_depth = 0;
3282 /* Unwind generic function template scope if necessary. */
3283 if (parser->fully_implicit_function_template_p)
3284 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3286 while (true)
3288 cp_token *token = cp_lexer_peek_token (parser->lexer);
3290 switch (token->type)
3292 case CPP_EOF:
3293 case CPP_PRAGMA_EOL:
3294 /* If we've run out of tokens, stop. */
3295 return;
3297 case CPP_SEMICOLON:
3298 /* If the next token is a `;', we have reached the end of the
3299 statement. */
3300 if (!nesting_depth)
3301 return;
3302 break;
3304 case CPP_CLOSE_BRACE:
3305 /* If this is a non-nested '}', stop before consuming it.
3306 That way, when confronted with something like:
3308 { 3 + }
3310 we stop before consuming the closing '}', even though we
3311 have not yet reached a `;'. */
3312 if (nesting_depth == 0)
3313 return;
3315 /* If it is the closing '}' for a block that we have
3316 scanned, stop -- but only after consuming the token.
3317 That way given:
3319 void f g () { ... }
3320 typedef int I;
3322 we will stop after the body of the erroneously declared
3323 function, but before consuming the following `typedef'
3324 declaration. */
3325 if (--nesting_depth == 0)
3327 cp_lexer_consume_token (parser->lexer);
3328 return;
3331 case CPP_OPEN_BRACE:
3332 ++nesting_depth;
3333 break;
3335 default:
3336 break;
3339 /* Consume the token. */
3340 cp_lexer_consume_token (parser->lexer);
3344 /* This function is called at the end of a statement or declaration.
3345 If the next token is a semicolon, it is consumed; otherwise, error
3346 recovery is attempted. */
3348 static void
3349 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3351 /* Look for the trailing `;'. */
3352 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3354 /* If there is additional (erroneous) input, skip to the end of
3355 the statement. */
3356 cp_parser_skip_to_end_of_statement (parser);
3357 /* If the next token is now a `;', consume it. */
3358 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3359 cp_lexer_consume_token (parser->lexer);
3363 /* Skip tokens until we have consumed an entire block, or until we
3364 have consumed a non-nested `;'. */
3366 static void
3367 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3369 int nesting_depth = 0;
3371 /* Unwind generic function template scope if necessary. */
3372 if (parser->fully_implicit_function_template_p)
3373 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3375 while (nesting_depth >= 0)
3377 cp_token *token = cp_lexer_peek_token (parser->lexer);
3379 switch (token->type)
3381 case CPP_EOF:
3382 case CPP_PRAGMA_EOL:
3383 /* If we've run out of tokens, stop. */
3384 return;
3386 case CPP_SEMICOLON:
3387 /* Stop if this is an unnested ';'. */
3388 if (!nesting_depth)
3389 nesting_depth = -1;
3390 break;
3392 case CPP_CLOSE_BRACE:
3393 /* Stop if this is an unnested '}', or closes the outermost
3394 nesting level. */
3395 nesting_depth--;
3396 if (nesting_depth < 0)
3397 return;
3398 if (!nesting_depth)
3399 nesting_depth = -1;
3400 break;
3402 case CPP_OPEN_BRACE:
3403 /* Nest. */
3404 nesting_depth++;
3405 break;
3407 default:
3408 break;
3411 /* Consume the token. */
3412 cp_lexer_consume_token (parser->lexer);
3416 /* Skip tokens until a non-nested closing curly brace is the next
3417 token, or there are no more tokens. Return true in the first case,
3418 false otherwise. */
3420 static bool
3421 cp_parser_skip_to_closing_brace (cp_parser *parser)
3423 unsigned nesting_depth = 0;
3425 while (true)
3427 cp_token *token = cp_lexer_peek_token (parser->lexer);
3429 switch (token->type)
3431 case CPP_EOF:
3432 case CPP_PRAGMA_EOL:
3433 /* If we've run out of tokens, stop. */
3434 return false;
3436 case CPP_CLOSE_BRACE:
3437 /* If the next token is a non-nested `}', then we have reached
3438 the end of the current block. */
3439 if (nesting_depth-- == 0)
3440 return true;
3441 break;
3443 case CPP_OPEN_BRACE:
3444 /* If it the next token is a `{', then we are entering a new
3445 block. Consume the entire block. */
3446 ++nesting_depth;
3447 break;
3449 default:
3450 break;
3453 /* Consume the token. */
3454 cp_lexer_consume_token (parser->lexer);
3458 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3459 parameter is the PRAGMA token, allowing us to purge the entire pragma
3460 sequence. */
3462 static void
3463 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3465 cp_token *token;
3467 parser->lexer->in_pragma = false;
3470 token = cp_lexer_consume_token (parser->lexer);
3471 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3473 /* Ensure that the pragma is not parsed again. */
3474 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3477 /* Require pragma end of line, resyncing with it as necessary. The
3478 arguments are as for cp_parser_skip_to_pragma_eol. */
3480 static void
3481 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3483 parser->lexer->in_pragma = false;
3484 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3485 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3488 /* This is a simple wrapper around make_typename_type. When the id is
3489 an unresolved identifier node, we can provide a superior diagnostic
3490 using cp_parser_diagnose_invalid_type_name. */
3492 static tree
3493 cp_parser_make_typename_type (cp_parser *parser, tree id,
3494 location_t id_location)
3496 tree result;
3497 if (identifier_p (id))
3499 result = make_typename_type (parser->scope, id, typename_type,
3500 /*complain=*/tf_none);
3501 if (result == error_mark_node)
3502 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3503 return result;
3505 return make_typename_type (parser->scope, id, typename_type, tf_error);
3508 /* This is a wrapper around the
3509 make_{pointer,ptrmem,reference}_declarator functions that decides
3510 which one to call based on the CODE and CLASS_TYPE arguments. The
3511 CODE argument should be one of the values returned by
3512 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3513 appertain to the pointer or reference. */
3515 static cp_declarator *
3516 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3517 cp_cv_quals cv_qualifiers,
3518 cp_declarator *target,
3519 tree attributes)
3521 if (code == ERROR_MARK)
3522 return cp_error_declarator;
3524 if (code == INDIRECT_REF)
3525 if (class_type == NULL_TREE)
3526 return make_pointer_declarator (cv_qualifiers, target, attributes);
3527 else
3528 return make_ptrmem_declarator (cv_qualifiers, class_type,
3529 target, attributes);
3530 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3531 return make_reference_declarator (cv_qualifiers, target,
3532 false, attributes);
3533 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3534 return make_reference_declarator (cv_qualifiers, target,
3535 true, attributes);
3536 gcc_unreachable ();
3539 /* Create a new C++ parser. */
3541 static cp_parser *
3542 cp_parser_new (void)
3544 cp_parser *parser;
3545 cp_lexer *lexer;
3546 unsigned i;
3548 /* cp_lexer_new_main is called before doing GC allocation because
3549 cp_lexer_new_main might load a PCH file. */
3550 lexer = cp_lexer_new_main ();
3552 /* Initialize the binops_by_token so that we can get the tree
3553 directly from the token. */
3554 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3555 binops_by_token[binops[i].token_type] = binops[i];
3557 parser = ggc_cleared_alloc<cp_parser> ();
3558 parser->lexer = lexer;
3559 parser->context = cp_parser_context_new (NULL);
3561 /* For now, we always accept GNU extensions. */
3562 parser->allow_gnu_extensions_p = 1;
3564 /* The `>' token is a greater-than operator, not the end of a
3565 template-id. */
3566 parser->greater_than_is_operator_p = true;
3568 parser->default_arg_ok_p = true;
3570 /* We are not parsing a constant-expression. */
3571 parser->integral_constant_expression_p = false;
3572 parser->allow_non_integral_constant_expression_p = false;
3573 parser->non_integral_constant_expression_p = false;
3575 /* Local variable names are not forbidden. */
3576 parser->local_variables_forbidden_p = false;
3578 /* We are not processing an `extern "C"' declaration. */
3579 parser->in_unbraced_linkage_specification_p = false;
3581 /* We are not processing a declarator. */
3582 parser->in_declarator_p = false;
3584 /* We are not processing a template-argument-list. */
3585 parser->in_template_argument_list_p = false;
3587 /* We are not in an iteration statement. */
3588 parser->in_statement = 0;
3590 /* We are not in a switch statement. */
3591 parser->in_switch_statement_p = false;
3593 /* We are not parsing a type-id inside an expression. */
3594 parser->in_type_id_in_expr_p = false;
3596 /* Declarations aren't implicitly extern "C". */
3597 parser->implicit_extern_c = false;
3599 /* String literals should be translated to the execution character set. */
3600 parser->translate_strings_p = true;
3602 /* We are not parsing a function body. */
3603 parser->in_function_body = false;
3605 /* We can correct until told otherwise. */
3606 parser->colon_corrects_to_scope_p = true;
3608 /* The unparsed function queue is empty. */
3609 push_unparsed_function_queues (parser);
3611 /* There are no classes being defined. */
3612 parser->num_classes_being_defined = 0;
3614 /* No template parameters apply. */
3615 parser->num_template_parameter_lists = 0;
3617 /* Not declaring an implicit function template. */
3618 parser->auto_is_implicit_function_template_parm_p = false;
3619 parser->fully_implicit_function_template_p = false;
3620 parser->implicit_template_parms = 0;
3621 parser->implicit_template_scope = 0;
3623 /* Allow constrained-type-specifiers. */
3624 parser->prevent_constrained_type_specifiers = 0;
3626 return parser;
3629 /* Create a cp_lexer structure which will emit the tokens in CACHE
3630 and push it onto the parser's lexer stack. This is used for delayed
3631 parsing of in-class method bodies and default arguments, and should
3632 not be confused with tentative parsing. */
3633 static void
3634 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3636 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3637 lexer->next = parser->lexer;
3638 parser->lexer = lexer;
3640 /* Move the current source position to that of the first token in the
3641 new lexer. */
3642 cp_lexer_set_source_position_from_token (lexer->next_token);
3645 /* Pop the top lexer off the parser stack. This is never used for the
3646 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3647 static void
3648 cp_parser_pop_lexer (cp_parser *parser)
3650 cp_lexer *lexer = parser->lexer;
3651 parser->lexer = lexer->next;
3652 cp_lexer_destroy (lexer);
3654 /* Put the current source position back where it was before this
3655 lexer was pushed. */
3656 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3659 /* Lexical conventions [gram.lex] */
3661 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3662 identifier. */
3664 static tree
3665 cp_parser_identifier (cp_parser* parser)
3667 cp_token *token;
3669 /* Look for the identifier. */
3670 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3671 /* Return the value. */
3672 return token ? token->u.value : error_mark_node;
3675 /* Parse a sequence of adjacent string constants. Returns a
3676 TREE_STRING representing the combined, nul-terminated string
3677 constant. If TRANSLATE is true, translate the string to the
3678 execution character set. If WIDE_OK is true, a wide string is
3679 invalid here.
3681 C++98 [lex.string] says that if a narrow string literal token is
3682 adjacent to a wide string literal token, the behavior is undefined.
3683 However, C99 6.4.5p4 says that this results in a wide string literal.
3684 We follow C99 here, for consistency with the C front end.
3686 This code is largely lifted from lex_string() in c-lex.c.
3688 FUTURE: ObjC++ will need to handle @-strings here. */
3689 static tree
3690 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3691 bool lookup_udlit = true)
3693 tree value;
3694 size_t count;
3695 struct obstack str_ob;
3696 cpp_string str, istr, *strs;
3697 cp_token *tok;
3698 enum cpp_ttype type, curr_type;
3699 int have_suffix_p = 0;
3700 tree string_tree;
3701 tree suffix_id = NULL_TREE;
3702 bool curr_tok_is_userdef_p = false;
3704 tok = cp_lexer_peek_token (parser->lexer);
3705 if (!cp_parser_is_string_literal (tok))
3707 cp_parser_error (parser, "expected string-literal");
3708 return error_mark_node;
3711 if (cpp_userdef_string_p (tok->type))
3713 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3714 curr_type = cpp_userdef_string_remove_type (tok->type);
3715 curr_tok_is_userdef_p = true;
3717 else
3719 string_tree = tok->u.value;
3720 curr_type = tok->type;
3722 type = curr_type;
3724 /* Try to avoid the overhead of creating and destroying an obstack
3725 for the common case of just one string. */
3726 if (!cp_parser_is_string_literal
3727 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3729 cp_lexer_consume_token (parser->lexer);
3731 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3732 str.len = TREE_STRING_LENGTH (string_tree);
3733 count = 1;
3735 if (curr_tok_is_userdef_p)
3737 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3738 have_suffix_p = 1;
3739 curr_type = cpp_userdef_string_remove_type (tok->type);
3741 else
3742 curr_type = tok->type;
3744 strs = &str;
3746 else
3748 gcc_obstack_init (&str_ob);
3749 count = 0;
3753 cp_lexer_consume_token (parser->lexer);
3754 count++;
3755 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3756 str.len = TREE_STRING_LENGTH (string_tree);
3758 if (curr_tok_is_userdef_p)
3760 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3761 if (have_suffix_p == 0)
3763 suffix_id = curr_suffix_id;
3764 have_suffix_p = 1;
3766 else if (have_suffix_p == 1
3767 && curr_suffix_id != suffix_id)
3769 error ("inconsistent user-defined literal suffixes"
3770 " %qD and %qD in string literal",
3771 suffix_id, curr_suffix_id);
3772 have_suffix_p = -1;
3774 curr_type = cpp_userdef_string_remove_type (tok->type);
3776 else
3777 curr_type = tok->type;
3779 if (type != curr_type)
3781 if (type == CPP_STRING)
3782 type = curr_type;
3783 else if (curr_type != CPP_STRING)
3784 error_at (tok->location,
3785 "unsupported non-standard concatenation "
3786 "of string literals");
3789 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3791 tok = cp_lexer_peek_token (parser->lexer);
3792 if (cpp_userdef_string_p (tok->type))
3794 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3795 curr_type = cpp_userdef_string_remove_type (tok->type);
3796 curr_tok_is_userdef_p = true;
3798 else
3800 string_tree = tok->u.value;
3801 curr_type = tok->type;
3802 curr_tok_is_userdef_p = false;
3805 while (cp_parser_is_string_literal (tok));
3807 strs = (cpp_string *) obstack_finish (&str_ob);
3810 if (type != CPP_STRING && !wide_ok)
3812 cp_parser_error (parser, "a wide string is invalid in this context");
3813 type = CPP_STRING;
3816 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3817 (parse_in, strs, count, &istr, type))
3819 value = build_string (istr.len, (const char *)istr.text);
3820 free (CONST_CAST (unsigned char *, istr.text));
3822 switch (type)
3824 default:
3825 case CPP_STRING:
3826 case CPP_UTF8STRING:
3827 TREE_TYPE (value) = char_array_type_node;
3828 break;
3829 case CPP_STRING16:
3830 TREE_TYPE (value) = char16_array_type_node;
3831 break;
3832 case CPP_STRING32:
3833 TREE_TYPE (value) = char32_array_type_node;
3834 break;
3835 case CPP_WSTRING:
3836 TREE_TYPE (value) = wchar_array_type_node;
3837 break;
3840 value = fix_string_type (value);
3842 if (have_suffix_p)
3844 tree literal = build_userdef_literal (suffix_id, value,
3845 OT_NONE, NULL_TREE);
3846 if (lookup_udlit)
3847 value = cp_parser_userdef_string_literal (literal);
3848 else
3849 value = literal;
3852 else
3853 /* cpp_interpret_string has issued an error. */
3854 value = error_mark_node;
3856 if (count > 1)
3857 obstack_free (&str_ob, 0);
3859 return value;
3862 /* Look up a literal operator with the name and the exact arguments. */
3864 static tree
3865 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3867 tree decl, fns;
3868 decl = lookup_name (name);
3869 if (!decl || !is_overloaded_fn (decl))
3870 return error_mark_node;
3872 for (fns = decl; fns; fns = OVL_NEXT (fns))
3874 unsigned int ix;
3875 bool found = true;
3876 tree fn = OVL_CURRENT (fns);
3877 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3878 if (parmtypes != NULL_TREE)
3880 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3881 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3883 tree tparm = TREE_VALUE (parmtypes);
3884 tree targ = TREE_TYPE ((*args)[ix]);
3885 bool ptr = TYPE_PTR_P (tparm);
3886 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3887 if ((ptr || arr || !same_type_p (tparm, targ))
3888 && (!ptr || !arr
3889 || !same_type_p (TREE_TYPE (tparm),
3890 TREE_TYPE (targ))))
3891 found = false;
3893 if (found
3894 && ix == vec_safe_length (args)
3895 /* May be this should be sufficient_parms_p instead,
3896 depending on how exactly should user-defined literals
3897 work in presence of default arguments on the literal
3898 operator parameters. */
3899 && parmtypes == void_list_node)
3900 return decl;
3904 return error_mark_node;
3907 /* Parse a user-defined char constant. Returns a call to a user-defined
3908 literal operator taking the character as an argument. */
3910 static tree
3911 cp_parser_userdef_char_literal (cp_parser *parser)
3913 cp_token *token = cp_lexer_consume_token (parser->lexer);
3914 tree literal = token->u.value;
3915 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3916 tree value = USERDEF_LITERAL_VALUE (literal);
3917 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3918 tree decl, result;
3920 /* Build up a call to the user-defined operator */
3921 /* Lookup the name we got back from the id-expression. */
3922 vec<tree, va_gc> *args = make_tree_vector ();
3923 vec_safe_push (args, value);
3924 decl = lookup_literal_operator (name, args);
3925 if (!decl || decl == error_mark_node)
3927 error ("unable to find character literal operator %qD with %qT argument",
3928 name, TREE_TYPE (value));
3929 release_tree_vector (args);
3930 return error_mark_node;
3932 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3933 release_tree_vector (args);
3934 return result;
3937 /* A subroutine of cp_parser_userdef_numeric_literal to
3938 create a char... template parameter pack from a string node. */
3940 static tree
3941 make_char_string_pack (tree value)
3943 tree charvec;
3944 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3945 const char *str = TREE_STRING_POINTER (value);
3946 int i, len = TREE_STRING_LENGTH (value) - 1;
3947 tree argvec = make_tree_vec (1);
3949 /* Fill in CHARVEC with all of the parameters. */
3950 charvec = make_tree_vec (len);
3951 for (i = 0; i < len; ++i)
3952 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3954 /* Build the argument packs. */
3955 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3956 TREE_TYPE (argpack) = char_type_node;
3958 TREE_VEC_ELT (argvec, 0) = argpack;
3960 return argvec;
3963 /* A subroutine of cp_parser_userdef_numeric_literal to
3964 create a char... template parameter pack from a string node. */
3966 static tree
3967 make_string_pack (tree value)
3969 tree charvec;
3970 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3971 const unsigned char *str
3972 = (const unsigned char *) TREE_STRING_POINTER (value);
3973 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3974 int len = TREE_STRING_LENGTH (value) / sz - 1;
3975 tree argvec = make_tree_vec (2);
3977 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3978 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3980 /* First template parm is character type. */
3981 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3983 /* Fill in CHARVEC with all of the parameters. */
3984 charvec = make_tree_vec (len);
3985 for (int i = 0; i < len; ++i)
3986 TREE_VEC_ELT (charvec, i)
3987 = double_int_to_tree (str_char_type_node,
3988 double_int::from_buffer (str + i * sz, sz));
3990 /* Build the argument packs. */
3991 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3992 TREE_TYPE (argpack) = str_char_type_node;
3994 TREE_VEC_ELT (argvec, 1) = argpack;
3996 return argvec;
3999 /* Parse a user-defined numeric constant. returns a call to a user-defined
4000 literal operator. */
4002 static tree
4003 cp_parser_userdef_numeric_literal (cp_parser *parser)
4005 cp_token *token = cp_lexer_consume_token (parser->lexer);
4006 tree literal = token->u.value;
4007 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4008 tree value = USERDEF_LITERAL_VALUE (literal);
4009 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4010 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4011 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4012 tree decl, result;
4013 vec<tree, va_gc> *args;
4015 /* Look for a literal operator taking the exact type of numeric argument
4016 as the literal value. */
4017 args = make_tree_vector ();
4018 vec_safe_push (args, value);
4019 decl = lookup_literal_operator (name, args);
4020 if (decl && decl != error_mark_node)
4022 result = finish_call_expr (decl, &args, false, true,
4023 tf_warning_or_error);
4025 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4027 warning_at (token->location, OPT_Woverflow,
4028 "integer literal exceeds range of %qT type",
4029 long_long_unsigned_type_node);
4031 else
4033 if (overflow > 0)
4034 warning_at (token->location, OPT_Woverflow,
4035 "floating literal exceeds range of %qT type",
4036 long_double_type_node);
4037 else if (overflow < 0)
4038 warning_at (token->location, OPT_Woverflow,
4039 "floating literal truncated to zero");
4042 release_tree_vector (args);
4043 return result;
4045 release_tree_vector (args);
4047 /* If the numeric argument didn't work, look for a raw literal
4048 operator taking a const char* argument consisting of the number
4049 in string format. */
4050 args = make_tree_vector ();
4051 vec_safe_push (args, num_string);
4052 decl = lookup_literal_operator (name, args);
4053 if (decl && decl != error_mark_node)
4055 result = finish_call_expr (decl, &args, false, true,
4056 tf_warning_or_error);
4057 release_tree_vector (args);
4058 return result;
4060 release_tree_vector (args);
4062 /* If the raw literal didn't work, look for a non-type template
4063 function with parameter pack char.... Call the function with
4064 template parameter characters representing the number. */
4065 args = make_tree_vector ();
4066 decl = lookup_literal_operator (name, args);
4067 if (decl && decl != error_mark_node)
4069 tree tmpl_args = make_char_string_pack (num_string);
4070 decl = lookup_template_function (decl, tmpl_args);
4071 result = finish_call_expr (decl, &args, false, true,
4072 tf_warning_or_error);
4073 release_tree_vector (args);
4074 return result;
4077 release_tree_vector (args);
4079 error ("unable to find numeric literal operator %qD", name);
4080 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4081 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4082 "to enable more built-in suffixes");
4083 return error_mark_node;
4086 /* Parse a user-defined string constant. Returns a call to a user-defined
4087 literal operator taking a character pointer and the length of the string
4088 as arguments. */
4090 static tree
4091 cp_parser_userdef_string_literal (tree literal)
4093 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4094 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4095 tree value = USERDEF_LITERAL_VALUE (literal);
4096 int len = TREE_STRING_LENGTH (value)
4097 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4098 tree decl, result;
4099 vec<tree, va_gc> *args;
4101 /* Build up a call to the user-defined operator. */
4102 /* Lookup the name we got back from the id-expression. */
4103 args = make_tree_vector ();
4104 vec_safe_push (args, value);
4105 vec_safe_push (args, build_int_cst (size_type_node, len));
4106 decl = lookup_literal_operator (name, args);
4108 if (decl && decl != error_mark_node)
4110 result = finish_call_expr (decl, &args, false, true,
4111 tf_warning_or_error);
4112 release_tree_vector (args);
4113 return result;
4115 release_tree_vector (args);
4117 /* Look for a template function with typename parameter CharT
4118 and parameter pack CharT... Call the function with
4119 template parameter characters representing the string. */
4120 args = make_tree_vector ();
4121 decl = lookup_literal_operator (name, args);
4122 if (decl && decl != error_mark_node)
4124 tree tmpl_args = make_string_pack (value);
4125 decl = lookup_template_function (decl, tmpl_args);
4126 result = finish_call_expr (decl, &args, false, true,
4127 tf_warning_or_error);
4128 release_tree_vector (args);
4129 return result;
4131 release_tree_vector (args);
4133 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4134 name, TREE_TYPE (value), size_type_node);
4135 return error_mark_node;
4139 /* Basic concepts [gram.basic] */
4141 /* Parse a translation-unit.
4143 translation-unit:
4144 declaration-seq [opt]
4146 Returns TRUE if all went well. */
4148 static bool
4149 cp_parser_translation_unit (cp_parser* parser)
4151 /* The address of the first non-permanent object on the declarator
4152 obstack. */
4153 static void *declarator_obstack_base;
4155 bool success;
4157 /* Create the declarator obstack, if necessary. */
4158 if (!cp_error_declarator)
4160 gcc_obstack_init (&declarator_obstack);
4161 /* Create the error declarator. */
4162 cp_error_declarator = make_declarator (cdk_error);
4163 /* Create the empty parameter list. */
4164 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4165 /* Remember where the base of the declarator obstack lies. */
4166 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4169 cp_parser_declaration_seq_opt (parser);
4171 /* If there are no tokens left then all went well. */
4172 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4174 /* Get rid of the token array; we don't need it any more. */
4175 cp_lexer_destroy (parser->lexer);
4176 parser->lexer = NULL;
4178 /* This file might have been a context that's implicitly extern
4179 "C". If so, pop the lang context. (Only relevant for PCH.) */
4180 if (parser->implicit_extern_c)
4182 pop_lang_context ();
4183 parser->implicit_extern_c = false;
4186 /* Finish up. */
4187 finish_translation_unit ();
4189 success = true;
4191 else
4193 cp_parser_error (parser, "expected declaration");
4194 success = false;
4197 /* Make sure the declarator obstack was fully cleaned up. */
4198 gcc_assert (obstack_next_free (&declarator_obstack)
4199 == declarator_obstack_base);
4201 /* All went well. */
4202 return success;
4205 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4206 decltype context. */
4208 static inline tsubst_flags_t
4209 complain_flags (bool decltype_p)
4211 tsubst_flags_t complain = tf_warning_or_error;
4212 if (decltype_p)
4213 complain |= tf_decltype;
4214 return complain;
4217 /* We're about to parse a collection of statements. If we're currently
4218 parsing tentatively, set up a firewall so that any nested
4219 cp_parser_commit_to_tentative_parse won't affect the current context. */
4221 static cp_token_position
4222 cp_parser_start_tentative_firewall (cp_parser *parser)
4224 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4225 return 0;
4227 cp_parser_parse_tentatively (parser);
4228 cp_parser_commit_to_topmost_tentative_parse (parser);
4229 return cp_lexer_token_position (parser->lexer, false);
4232 /* We've finished parsing the collection of statements. Wrap up the
4233 firewall and replace the relevant tokens with the parsed form. */
4235 static void
4236 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4237 tree expr)
4239 if (!start)
4240 return;
4242 /* Finish the firewall level. */
4243 cp_parser_parse_definitely (parser);
4244 /* And remember the result of the parse for when we try again. */
4245 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4246 token->type = CPP_PREPARSED_EXPR;
4247 token->u.value = expr;
4248 token->keyword = RID_MAX;
4249 cp_lexer_purge_tokens_after (parser->lexer, start);
4252 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4253 enclosing parentheses. */
4255 static tree
4256 cp_parser_statement_expr (cp_parser *parser)
4258 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4260 /* Consume the '('. */
4261 cp_lexer_consume_token (parser->lexer);
4262 /* Start the statement-expression. */
4263 tree expr = begin_stmt_expr ();
4264 /* Parse the compound-statement. */
4265 cp_parser_compound_statement (parser, expr, false, false);
4266 /* Finish up. */
4267 expr = finish_stmt_expr (expr, false);
4268 /* Consume the ')'. */
4269 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4270 cp_parser_skip_to_end_of_statement (parser);
4272 cp_parser_end_tentative_firewall (parser, start, expr);
4273 return expr;
4276 /* Expressions [gram.expr] */
4278 /* Parse a fold-operator.
4280 fold-operator:
4281 - * / % ^ & | = < > << >>
4282 = -= *= /= %= ^= &= |= <<= >>=
4283 == != <= >= && || , .* ->*
4285 This returns the tree code corresponding to the matched operator
4286 as an int. When the current token matches a compound assignment
4287 opertor, the resulting tree code is the negative value of the
4288 non-assignment operator. */
4290 static int
4291 cp_parser_fold_operator (cp_token *token)
4293 switch (token->type)
4295 case CPP_PLUS: return PLUS_EXPR;
4296 case CPP_MINUS: return MINUS_EXPR;
4297 case CPP_MULT: return MULT_EXPR;
4298 case CPP_DIV: return TRUNC_DIV_EXPR;
4299 case CPP_MOD: return TRUNC_MOD_EXPR;
4300 case CPP_XOR: return BIT_XOR_EXPR;
4301 case CPP_AND: return BIT_AND_EXPR;
4302 case CPP_OR: return BIT_IOR_EXPR;
4303 case CPP_LSHIFT: return LSHIFT_EXPR;
4304 case CPP_RSHIFT: return RSHIFT_EXPR;
4306 case CPP_EQ: return -NOP_EXPR;
4307 case CPP_PLUS_EQ: return -PLUS_EXPR;
4308 case CPP_MINUS_EQ: return -MINUS_EXPR;
4309 case CPP_MULT_EQ: return -MULT_EXPR;
4310 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4311 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4312 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4313 case CPP_AND_EQ: return -BIT_AND_EXPR;
4314 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4315 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4316 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4318 case CPP_EQ_EQ: return EQ_EXPR;
4319 case CPP_NOT_EQ: return NE_EXPR;
4320 case CPP_LESS: return LT_EXPR;
4321 case CPP_GREATER: return GT_EXPR;
4322 case CPP_LESS_EQ: return LE_EXPR;
4323 case CPP_GREATER_EQ: return GE_EXPR;
4325 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4326 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4328 case CPP_COMMA: return COMPOUND_EXPR;
4330 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4331 case CPP_DEREF_STAR: return MEMBER_REF;
4333 default: return ERROR_MARK;
4337 /* If the next token is a suitable fold operator, consume it and return as
4338 the function above. */
4340 static int
4341 cp_parser_fold_operator (cp_parser *parser)
4343 cp_token* token = cp_lexer_peek_token (parser->lexer);
4344 int code = cp_parser_fold_operator (token);
4345 if (code != ERROR_MARK)
4346 cp_lexer_consume_token (parser->lexer);
4347 return code;
4350 /* Returns true iff we're at the beginning of an N4191 fold-expression, after
4351 the left parenthesis. Rather than do tentative parsing, we scan the tokens
4352 up to the matching right paren for an ellipsis next to a binary
4353 operator. */
4355 static bool
4356 cp_parser_fold_expr_p (cp_parser *parser)
4358 /* An ellipsis right after the left paren always indicates a
4359 fold-expression. */
4360 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4362 /* But if there isn't a fold operator after the ellipsis,
4363 give a different error. */
4364 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, 2);
4365 return (cp_parser_fold_operator (token) != ERROR_MARK);
4368 /* Otherwise, look for an ellipsis. */
4369 cp_lexer_save_tokens (parser->lexer);
4370 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser, false,
4371 CPP_ELLIPSIS, false);
4372 bool found = (ret == -1);
4373 if (found)
4375 /* We found an ellipsis, is the previous token an operator? */
4376 cp_token *token = cp_lexer_peek_token (parser->lexer);
4377 --token;
4378 if (cp_parser_fold_operator (token) == ERROR_MARK)
4379 found = false;
4381 cp_lexer_rollback_tokens (parser->lexer);
4382 return found;
4385 /* Parse a fold-expression.
4387 fold-expression:
4388 ( ... folding-operator cast-expression)
4389 ( cast-expression folding-operator ... )
4390 ( cast-expression folding operator ... folding-operator cast-expression)
4392 Note that the '(' and ')' are matched in primary expression. */
4394 static tree
4395 cp_parser_fold_expression (cp_parser *parser)
4397 cp_id_kind pidk;
4399 if (cxx_dialect < cxx1z && !in_system_header_at (input_location))
4400 pedwarn (input_location, 0, "fold-expressions only available with "
4401 "-std=c++1z or -std=gnu++1z");
4403 // Left fold.
4404 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4406 cp_lexer_consume_token (parser->lexer);
4407 int op = cp_parser_fold_operator (parser);
4408 if (op == ERROR_MARK)
4410 cp_parser_error (parser, "expected binary operator");
4411 return error_mark_node;
4414 tree expr = cp_parser_cast_expression (parser, false, false,
4415 false, &pidk);
4416 if (expr == error_mark_node)
4417 return error_mark_node;
4418 return finish_left_unary_fold_expr (expr, op);
4421 tree expr1 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4422 if (expr1 == error_mark_node)
4423 return error_mark_node;
4425 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4426 int op = cp_parser_fold_operator (parser);
4427 if (op == ERROR_MARK)
4429 cp_parser_error (parser, "expected binary operator");
4430 return error_mark_node;
4433 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4435 cp_parser_error (parser, "expected ...");
4436 return error_mark_node;
4438 cp_lexer_consume_token (parser->lexer);
4440 // Right fold.
4441 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4442 return finish_right_unary_fold_expr (expr1, op);
4444 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4446 cp_parser_error (parser, "mismatched operator in fold-expression");
4447 return error_mark_node;
4449 cp_lexer_consume_token (parser->lexer);
4451 // Binary left or right fold.
4452 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4453 if (expr2 == error_mark_node)
4454 return error_mark_node;
4455 return finish_binary_fold_expr (expr1, expr2, op);
4458 /* Parse a primary-expression.
4460 primary-expression:
4461 literal
4462 this
4463 ( expression )
4464 id-expression
4465 lambda-expression (C++11)
4467 GNU Extensions:
4469 primary-expression:
4470 ( compound-statement )
4471 __builtin_va_arg ( assignment-expression , type-id )
4472 __builtin_offsetof ( type-id , offsetof-expression )
4474 C++ Extensions:
4475 __has_nothrow_assign ( type-id )
4476 __has_nothrow_constructor ( type-id )
4477 __has_nothrow_copy ( type-id )
4478 __has_trivial_assign ( type-id )
4479 __has_trivial_constructor ( type-id )
4480 __has_trivial_copy ( type-id )
4481 __has_trivial_destructor ( type-id )
4482 __has_virtual_destructor ( type-id )
4483 __is_abstract ( type-id )
4484 __is_base_of ( type-id , type-id )
4485 __is_class ( type-id )
4486 __is_empty ( type-id )
4487 __is_enum ( type-id )
4488 __is_final ( type-id )
4489 __is_literal_type ( type-id )
4490 __is_pod ( type-id )
4491 __is_polymorphic ( type-id )
4492 __is_std_layout ( type-id )
4493 __is_trivial ( type-id )
4494 __is_union ( type-id )
4496 Objective-C++ Extension:
4498 primary-expression:
4499 objc-expression
4501 literal:
4502 __null
4504 ADDRESS_P is true iff this expression was immediately preceded by
4505 "&" and therefore might denote a pointer-to-member. CAST_P is true
4506 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4507 true iff this expression is a template argument.
4509 Returns a representation of the expression. Upon return, *IDK
4510 indicates what kind of id-expression (if any) was present. */
4512 static tree
4513 cp_parser_primary_expression (cp_parser *parser,
4514 bool address_p,
4515 bool cast_p,
4516 bool template_arg_p,
4517 bool decltype_p,
4518 cp_id_kind *idk)
4520 cp_token *token = NULL;
4522 /* Assume the primary expression is not an id-expression. */
4523 *idk = CP_ID_KIND_NONE;
4525 /* Peek at the next token. */
4526 token = cp_lexer_peek_token (parser->lexer);
4527 switch ((int) token->type)
4529 /* literal:
4530 integer-literal
4531 character-literal
4532 floating-literal
4533 string-literal
4534 boolean-literal
4535 pointer-literal
4536 user-defined-literal */
4537 case CPP_CHAR:
4538 case CPP_CHAR16:
4539 case CPP_CHAR32:
4540 case CPP_WCHAR:
4541 case CPP_UTF8CHAR:
4542 case CPP_NUMBER:
4543 case CPP_PREPARSED_EXPR:
4544 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4545 return cp_parser_userdef_numeric_literal (parser);
4546 token = cp_lexer_consume_token (parser->lexer);
4547 if (TREE_CODE (token->u.value) == FIXED_CST)
4549 error_at (token->location,
4550 "fixed-point types not supported in C++");
4551 return error_mark_node;
4553 /* Floating-point literals are only allowed in an integral
4554 constant expression if they are cast to an integral or
4555 enumeration type. */
4556 if (TREE_CODE (token->u.value) == REAL_CST
4557 && parser->integral_constant_expression_p
4558 && pedantic)
4560 /* CAST_P will be set even in invalid code like "int(2.7 +
4561 ...)". Therefore, we have to check that the next token
4562 is sure to end the cast. */
4563 if (cast_p)
4565 cp_token *next_token;
4567 next_token = cp_lexer_peek_token (parser->lexer);
4568 if (/* The comma at the end of an
4569 enumerator-definition. */
4570 next_token->type != CPP_COMMA
4571 /* The curly brace at the end of an enum-specifier. */
4572 && next_token->type != CPP_CLOSE_BRACE
4573 /* The end of a statement. */
4574 && next_token->type != CPP_SEMICOLON
4575 /* The end of the cast-expression. */
4576 && next_token->type != CPP_CLOSE_PAREN
4577 /* The end of an array bound. */
4578 && next_token->type != CPP_CLOSE_SQUARE
4579 /* The closing ">" in a template-argument-list. */
4580 && (next_token->type != CPP_GREATER
4581 || parser->greater_than_is_operator_p)
4582 /* C++0x only: A ">>" treated like two ">" tokens,
4583 in a template-argument-list. */
4584 && (next_token->type != CPP_RSHIFT
4585 || (cxx_dialect == cxx98)
4586 || parser->greater_than_is_operator_p))
4587 cast_p = false;
4590 /* If we are within a cast, then the constraint that the
4591 cast is to an integral or enumeration type will be
4592 checked at that point. If we are not within a cast, then
4593 this code is invalid. */
4594 if (!cast_p)
4595 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4597 return token->u.value;
4599 case CPP_CHAR_USERDEF:
4600 case CPP_CHAR16_USERDEF:
4601 case CPP_CHAR32_USERDEF:
4602 case CPP_WCHAR_USERDEF:
4603 case CPP_UTF8CHAR_USERDEF:
4604 return cp_parser_userdef_char_literal (parser);
4606 case CPP_STRING:
4607 case CPP_STRING16:
4608 case CPP_STRING32:
4609 case CPP_WSTRING:
4610 case CPP_UTF8STRING:
4611 case CPP_STRING_USERDEF:
4612 case CPP_STRING16_USERDEF:
4613 case CPP_STRING32_USERDEF:
4614 case CPP_WSTRING_USERDEF:
4615 case CPP_UTF8STRING_USERDEF:
4616 /* ??? Should wide strings be allowed when parser->translate_strings_p
4617 is false (i.e. in attributes)? If not, we can kill the third
4618 argument to cp_parser_string_literal. */
4619 return cp_parser_string_literal (parser,
4620 parser->translate_strings_p,
4621 true);
4623 case CPP_OPEN_PAREN:
4624 /* If we see `( { ' then we are looking at the beginning of
4625 a GNU statement-expression. */
4626 if (cp_parser_allow_gnu_extensions_p (parser)
4627 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4629 /* Statement-expressions are not allowed by the standard. */
4630 pedwarn (token->location, OPT_Wpedantic,
4631 "ISO C++ forbids braced-groups within expressions");
4633 /* And they're not allowed outside of a function-body; you
4634 cannot, for example, write:
4636 int i = ({ int j = 3; j + 1; });
4638 at class or namespace scope. */
4639 if (!parser->in_function_body
4640 || parser->in_template_argument_list_p)
4642 error_at (token->location,
4643 "statement-expressions are not allowed outside "
4644 "functions nor in template-argument lists");
4645 cp_parser_skip_to_end_of_block_or_statement (parser);
4646 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4647 cp_lexer_consume_token (parser->lexer);
4648 return error_mark_node;
4650 else
4651 return cp_parser_statement_expr (parser);
4653 /* Otherwise it's a normal parenthesized expression. */
4655 tree expr;
4656 bool saved_greater_than_is_operator_p;
4658 /* Consume the `('. */
4659 cp_lexer_consume_token (parser->lexer);
4660 /* Within a parenthesized expression, a `>' token is always
4661 the greater-than operator. */
4662 saved_greater_than_is_operator_p
4663 = parser->greater_than_is_operator_p;
4664 parser->greater_than_is_operator_p = true;
4666 // Handle a fold-expression.
4667 if (cp_parser_fold_expr_p (parser))
4669 tree fold = cp_parser_fold_expression (parser);
4670 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4671 return fold;
4674 /* Parse the parenthesized expression. */
4675 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4676 /* Let the front end know that this expression was
4677 enclosed in parentheses. This matters in case, for
4678 example, the expression is of the form `A::B', since
4679 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4680 not. */
4681 expr = finish_parenthesized_expr (expr);
4682 /* DR 705: Wrapping an unqualified name in parentheses
4683 suppresses arg-dependent lookup. We want to pass back
4684 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4685 (c++/37862), but none of the others. */
4686 if (*idk != CP_ID_KIND_QUALIFIED)
4687 *idk = CP_ID_KIND_NONE;
4689 /* The `>' token might be the end of a template-id or
4690 template-parameter-list now. */
4691 parser->greater_than_is_operator_p
4692 = saved_greater_than_is_operator_p;
4693 /* Consume the `)'. */
4694 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)
4695 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4696 cp_parser_skip_to_end_of_statement (parser);
4698 return expr;
4701 case CPP_OPEN_SQUARE:
4703 if (c_dialect_objc ())
4705 /* We might have an Objective-C++ message. */
4706 cp_parser_parse_tentatively (parser);
4707 tree msg = cp_parser_objc_message_expression (parser);
4708 /* If that works out, we're done ... */
4709 if (cp_parser_parse_definitely (parser))
4710 return msg;
4711 /* ... else, fall though to see if it's a lambda. */
4713 tree lam = cp_parser_lambda_expression (parser);
4714 /* Don't warn about a failed tentative parse. */
4715 if (cp_parser_error_occurred (parser))
4716 return error_mark_node;
4717 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4718 return lam;
4721 case CPP_OBJC_STRING:
4722 if (c_dialect_objc ())
4723 /* We have an Objective-C++ string literal. */
4724 return cp_parser_objc_expression (parser);
4725 cp_parser_error (parser, "expected primary-expression");
4726 return error_mark_node;
4728 case CPP_KEYWORD:
4729 switch (token->keyword)
4731 /* These two are the boolean literals. */
4732 case RID_TRUE:
4733 cp_lexer_consume_token (parser->lexer);
4734 return boolean_true_node;
4735 case RID_FALSE:
4736 cp_lexer_consume_token (parser->lexer);
4737 return boolean_false_node;
4739 /* The `__null' literal. */
4740 case RID_NULL:
4741 cp_lexer_consume_token (parser->lexer);
4742 return null_node;
4744 /* The `nullptr' literal. */
4745 case RID_NULLPTR:
4746 cp_lexer_consume_token (parser->lexer);
4747 return nullptr_node;
4749 /* Recognize the `this' keyword. */
4750 case RID_THIS:
4751 cp_lexer_consume_token (parser->lexer);
4752 if (parser->local_variables_forbidden_p)
4754 error_at (token->location,
4755 "%<this%> may not be used in this context");
4756 return error_mark_node;
4758 /* Pointers cannot appear in constant-expressions. */
4759 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4760 return error_mark_node;
4761 return finish_this_expr ();
4763 /* The `operator' keyword can be the beginning of an
4764 id-expression. */
4765 case RID_OPERATOR:
4766 goto id_expression;
4768 case RID_FUNCTION_NAME:
4769 case RID_PRETTY_FUNCTION_NAME:
4770 case RID_C99_FUNCTION_NAME:
4772 non_integral_constant name;
4774 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4775 __func__ are the names of variables -- but they are
4776 treated specially. Therefore, they are handled here,
4777 rather than relying on the generic id-expression logic
4778 below. Grammatically, these names are id-expressions.
4780 Consume the token. */
4781 token = cp_lexer_consume_token (parser->lexer);
4783 switch (token->keyword)
4785 case RID_FUNCTION_NAME:
4786 name = NIC_FUNC_NAME;
4787 break;
4788 case RID_PRETTY_FUNCTION_NAME:
4789 name = NIC_PRETTY_FUNC;
4790 break;
4791 case RID_C99_FUNCTION_NAME:
4792 name = NIC_C99_FUNC;
4793 break;
4794 default:
4795 gcc_unreachable ();
4798 if (cp_parser_non_integral_constant_expression (parser, name))
4799 return error_mark_node;
4801 /* Look up the name. */
4802 return finish_fname (token->u.value);
4805 case RID_VA_ARG:
4807 tree expression;
4808 tree type;
4809 source_location type_location;
4811 /* The `__builtin_va_arg' construct is used to handle
4812 `va_arg'. Consume the `__builtin_va_arg' token. */
4813 cp_lexer_consume_token (parser->lexer);
4814 /* Look for the opening `('. */
4815 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4816 /* Now, parse the assignment-expression. */
4817 expression = cp_parser_assignment_expression (parser);
4818 /* Look for the `,'. */
4819 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4820 type_location = cp_lexer_peek_token (parser->lexer)->location;
4821 /* Parse the type-id. */
4822 type = cp_parser_type_id (parser);
4823 /* Look for the closing `)'. */
4824 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4825 /* Using `va_arg' in a constant-expression is not
4826 allowed. */
4827 if (cp_parser_non_integral_constant_expression (parser,
4828 NIC_VA_ARG))
4829 return error_mark_node;
4830 return build_x_va_arg (type_location, expression, type);
4833 case RID_OFFSETOF:
4834 return cp_parser_builtin_offsetof (parser);
4836 case RID_HAS_NOTHROW_ASSIGN:
4837 case RID_HAS_NOTHROW_CONSTRUCTOR:
4838 case RID_HAS_NOTHROW_COPY:
4839 case RID_HAS_TRIVIAL_ASSIGN:
4840 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4841 case RID_HAS_TRIVIAL_COPY:
4842 case RID_HAS_TRIVIAL_DESTRUCTOR:
4843 case RID_HAS_VIRTUAL_DESTRUCTOR:
4844 case RID_IS_ABSTRACT:
4845 case RID_IS_BASE_OF:
4846 case RID_IS_CLASS:
4847 case RID_IS_EMPTY:
4848 case RID_IS_ENUM:
4849 case RID_IS_FINAL:
4850 case RID_IS_LITERAL_TYPE:
4851 case RID_IS_POD:
4852 case RID_IS_POLYMORPHIC:
4853 case RID_IS_SAME_AS:
4854 case RID_IS_STD_LAYOUT:
4855 case RID_IS_TRIVIAL:
4856 case RID_IS_TRIVIALLY_ASSIGNABLE:
4857 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
4858 case RID_IS_TRIVIALLY_COPYABLE:
4859 case RID_IS_UNION:
4860 return cp_parser_trait_expr (parser, token->keyword);
4862 // C++ concepts
4863 case RID_REQUIRES:
4864 return cp_parser_requires_expression (parser);
4866 /* Objective-C++ expressions. */
4867 case RID_AT_ENCODE:
4868 case RID_AT_PROTOCOL:
4869 case RID_AT_SELECTOR:
4870 return cp_parser_objc_expression (parser);
4872 case RID_TEMPLATE:
4873 if (parser->in_function_body
4874 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4875 == CPP_LESS))
4877 error_at (token->location,
4878 "a template declaration cannot appear at block scope");
4879 cp_parser_skip_to_end_of_block_or_statement (parser);
4880 return error_mark_node;
4882 default:
4883 cp_parser_error (parser, "expected primary-expression");
4884 return error_mark_node;
4887 /* An id-expression can start with either an identifier, a
4888 `::' as the beginning of a qualified-id, or the "operator"
4889 keyword. */
4890 case CPP_NAME:
4891 case CPP_SCOPE:
4892 case CPP_TEMPLATE_ID:
4893 case CPP_NESTED_NAME_SPECIFIER:
4895 tree id_expression;
4896 tree decl;
4897 const char *error_msg;
4898 bool template_p;
4899 bool done;
4900 cp_token *id_expr_token;
4902 id_expression:
4903 /* Parse the id-expression. */
4904 id_expression
4905 = cp_parser_id_expression (parser,
4906 /*template_keyword_p=*/false,
4907 /*check_dependency_p=*/true,
4908 &template_p,
4909 /*declarator_p=*/false,
4910 /*optional_p=*/false);
4911 if (id_expression == error_mark_node)
4912 return error_mark_node;
4913 id_expr_token = token;
4914 token = cp_lexer_peek_token (parser->lexer);
4915 done = (token->type != CPP_OPEN_SQUARE
4916 && token->type != CPP_OPEN_PAREN
4917 && token->type != CPP_DOT
4918 && token->type != CPP_DEREF
4919 && token->type != CPP_PLUS_PLUS
4920 && token->type != CPP_MINUS_MINUS);
4921 /* If we have a template-id, then no further lookup is
4922 required. If the template-id was for a template-class, we
4923 will sometimes have a TYPE_DECL at this point. */
4924 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4925 || TREE_CODE (id_expression) == TYPE_DECL)
4926 decl = id_expression;
4927 /* Look up the name. */
4928 else
4930 tree ambiguous_decls;
4932 /* If we already know that this lookup is ambiguous, then
4933 we've already issued an error message; there's no reason
4934 to check again. */
4935 if (id_expr_token->type == CPP_NAME
4936 && id_expr_token->error_reported)
4938 cp_parser_simulate_error (parser);
4939 return error_mark_node;
4942 decl = cp_parser_lookup_name (parser, id_expression,
4943 none_type,
4944 template_p,
4945 /*is_namespace=*/false,
4946 /*check_dependency=*/true,
4947 &ambiguous_decls,
4948 id_expr_token->location);
4949 /* If the lookup was ambiguous, an error will already have
4950 been issued. */
4951 if (ambiguous_decls)
4952 return error_mark_node;
4954 /* In Objective-C++, we may have an Objective-C 2.0
4955 dot-syntax for classes here. */
4956 if (c_dialect_objc ()
4957 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4958 && TREE_CODE (decl) == TYPE_DECL
4959 && objc_is_class_name (decl))
4961 tree component;
4962 cp_lexer_consume_token (parser->lexer);
4963 component = cp_parser_identifier (parser);
4964 if (component == error_mark_node)
4965 return error_mark_node;
4967 return objc_build_class_component_ref (id_expression, component);
4970 /* In Objective-C++, an instance variable (ivar) may be preferred
4971 to whatever cp_parser_lookup_name() found. */
4972 decl = objc_lookup_ivar (decl, id_expression);
4974 /* If name lookup gives us a SCOPE_REF, then the
4975 qualifying scope was dependent. */
4976 if (TREE_CODE (decl) == SCOPE_REF)
4978 /* At this point, we do not know if DECL is a valid
4979 integral constant expression. We assume that it is
4980 in fact such an expression, so that code like:
4982 template <int N> struct A {
4983 int a[B<N>::i];
4986 is accepted. At template-instantiation time, we
4987 will check that B<N>::i is actually a constant. */
4988 return decl;
4990 /* Check to see if DECL is a local variable in a context
4991 where that is forbidden. */
4992 if (parser->local_variables_forbidden_p
4993 && local_variable_p (decl))
4995 /* It might be that we only found DECL because we are
4996 trying to be generous with pre-ISO scoping rules.
4997 For example, consider:
4999 int i;
5000 void g() {
5001 for (int i = 0; i < 10; ++i) {}
5002 extern void f(int j = i);
5005 Here, name look up will originally find the out
5006 of scope `i'. We need to issue a warning message,
5007 but then use the global `i'. */
5008 decl = check_for_out_of_scope_variable (decl);
5009 if (local_variable_p (decl))
5011 error_at (id_expr_token->location,
5012 "local variable %qD may not appear in this context",
5013 decl);
5014 return error_mark_node;
5019 decl = (finish_id_expression
5020 (id_expression, decl, parser->scope,
5021 idk,
5022 parser->integral_constant_expression_p,
5023 parser->allow_non_integral_constant_expression_p,
5024 &parser->non_integral_constant_expression_p,
5025 template_p, done, address_p,
5026 template_arg_p,
5027 &error_msg,
5028 id_expr_token->location));
5029 if (error_msg)
5030 cp_parser_error (parser, error_msg);
5031 return decl;
5034 /* Anything else is an error. */
5035 default:
5036 cp_parser_error (parser, "expected primary-expression");
5037 return error_mark_node;
5041 static inline tree
5042 cp_parser_primary_expression (cp_parser *parser,
5043 bool address_p,
5044 bool cast_p,
5045 bool template_arg_p,
5046 cp_id_kind *idk)
5048 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5049 /*decltype*/false, idk);
5052 /* Parse an id-expression.
5054 id-expression:
5055 unqualified-id
5056 qualified-id
5058 qualified-id:
5059 :: [opt] nested-name-specifier template [opt] unqualified-id
5060 :: identifier
5061 :: operator-function-id
5062 :: template-id
5064 Return a representation of the unqualified portion of the
5065 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5066 a `::' or nested-name-specifier.
5068 Often, if the id-expression was a qualified-id, the caller will
5069 want to make a SCOPE_REF to represent the qualified-id. This
5070 function does not do this in order to avoid wastefully creating
5071 SCOPE_REFs when they are not required.
5073 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5074 `template' keyword.
5076 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5077 uninstantiated templates.
5079 If *TEMPLATE_P is non-NULL, it is set to true iff the
5080 `template' keyword is used to explicitly indicate that the entity
5081 named is a template.
5083 If DECLARATOR_P is true, the id-expression is appearing as part of
5084 a declarator, rather than as part of an expression. */
5086 static tree
5087 cp_parser_id_expression (cp_parser *parser,
5088 bool template_keyword_p,
5089 bool check_dependency_p,
5090 bool *template_p,
5091 bool declarator_p,
5092 bool optional_p)
5094 bool global_scope_p;
5095 bool nested_name_specifier_p;
5097 /* Assume the `template' keyword was not used. */
5098 if (template_p)
5099 *template_p = template_keyword_p;
5101 /* Look for the optional `::' operator. */
5102 global_scope_p
5103 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
5104 != NULL_TREE);
5105 /* Look for the optional nested-name-specifier. */
5106 nested_name_specifier_p
5107 = (cp_parser_nested_name_specifier_opt (parser,
5108 /*typename_keyword_p=*/false,
5109 check_dependency_p,
5110 /*type_p=*/false,
5111 declarator_p)
5112 != NULL_TREE);
5113 /* If there is a nested-name-specifier, then we are looking at
5114 the first qualified-id production. */
5115 if (nested_name_specifier_p)
5117 tree saved_scope;
5118 tree saved_object_scope;
5119 tree saved_qualifying_scope;
5120 tree unqualified_id;
5121 bool is_template;
5123 /* See if the next token is the `template' keyword. */
5124 if (!template_p)
5125 template_p = &is_template;
5126 *template_p = cp_parser_optional_template_keyword (parser);
5127 /* Name lookup we do during the processing of the
5128 unqualified-id might obliterate SCOPE. */
5129 saved_scope = parser->scope;
5130 saved_object_scope = parser->object_scope;
5131 saved_qualifying_scope = parser->qualifying_scope;
5132 /* Process the final unqualified-id. */
5133 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5134 check_dependency_p,
5135 declarator_p,
5136 /*optional_p=*/false);
5137 /* Restore the SAVED_SCOPE for our caller. */
5138 parser->scope = saved_scope;
5139 parser->object_scope = saved_object_scope;
5140 parser->qualifying_scope = saved_qualifying_scope;
5142 return unqualified_id;
5144 /* Otherwise, if we are in global scope, then we are looking at one
5145 of the other qualified-id productions. */
5146 else if (global_scope_p)
5148 cp_token *token;
5149 tree id;
5151 /* Peek at the next token. */
5152 token = cp_lexer_peek_token (parser->lexer);
5154 /* If it's an identifier, and the next token is not a "<", then
5155 we can avoid the template-id case. This is an optimization
5156 for this common case. */
5157 if (token->type == CPP_NAME
5158 && !cp_parser_nth_token_starts_template_argument_list_p
5159 (parser, 2))
5160 return cp_parser_identifier (parser);
5162 cp_parser_parse_tentatively (parser);
5163 /* Try a template-id. */
5164 id = cp_parser_template_id (parser,
5165 /*template_keyword_p=*/false,
5166 /*check_dependency_p=*/true,
5167 none_type,
5168 declarator_p);
5169 /* If that worked, we're done. */
5170 if (cp_parser_parse_definitely (parser))
5171 return id;
5173 /* Peek at the next token. (Changes in the token buffer may
5174 have invalidated the pointer obtained above.) */
5175 token = cp_lexer_peek_token (parser->lexer);
5177 switch (token->type)
5179 case CPP_NAME:
5180 return cp_parser_identifier (parser);
5182 case CPP_KEYWORD:
5183 if (token->keyword == RID_OPERATOR)
5184 return cp_parser_operator_function_id (parser);
5185 /* Fall through. */
5187 default:
5188 cp_parser_error (parser, "expected id-expression");
5189 return error_mark_node;
5192 else
5193 return cp_parser_unqualified_id (parser, template_keyword_p,
5194 /*check_dependency_p=*/true,
5195 declarator_p,
5196 optional_p);
5199 /* Parse an unqualified-id.
5201 unqualified-id:
5202 identifier
5203 operator-function-id
5204 conversion-function-id
5205 ~ class-name
5206 template-id
5208 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5209 keyword, in a construct like `A::template ...'.
5211 Returns a representation of unqualified-id. For the `identifier'
5212 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5213 production a BIT_NOT_EXPR is returned; the operand of the
5214 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5215 other productions, see the documentation accompanying the
5216 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5217 names are looked up in uninstantiated templates. If DECLARATOR_P
5218 is true, the unqualified-id is appearing as part of a declarator,
5219 rather than as part of an expression. */
5221 static tree
5222 cp_parser_unqualified_id (cp_parser* parser,
5223 bool template_keyword_p,
5224 bool check_dependency_p,
5225 bool declarator_p,
5226 bool optional_p)
5228 cp_token *token;
5230 /* Peek at the next token. */
5231 token = cp_lexer_peek_token (parser->lexer);
5233 switch ((int) token->type)
5235 case CPP_NAME:
5237 tree id;
5239 /* We don't know yet whether or not this will be a
5240 template-id. */
5241 cp_parser_parse_tentatively (parser);
5242 /* Try a template-id. */
5243 id = cp_parser_template_id (parser, template_keyword_p,
5244 check_dependency_p,
5245 none_type,
5246 declarator_p);
5247 /* If it worked, we're done. */
5248 if (cp_parser_parse_definitely (parser))
5249 return id;
5250 /* Otherwise, it's an ordinary identifier. */
5251 return cp_parser_identifier (parser);
5254 case CPP_TEMPLATE_ID:
5255 return cp_parser_template_id (parser, template_keyword_p,
5256 check_dependency_p,
5257 none_type,
5258 declarator_p);
5260 case CPP_COMPL:
5262 tree type_decl;
5263 tree qualifying_scope;
5264 tree object_scope;
5265 tree scope;
5266 bool done;
5268 /* Consume the `~' token. */
5269 cp_lexer_consume_token (parser->lexer);
5270 /* Parse the class-name. The standard, as written, seems to
5271 say that:
5273 template <typename T> struct S { ~S (); };
5274 template <typename T> S<T>::~S() {}
5276 is invalid, since `~' must be followed by a class-name, but
5277 `S<T>' is dependent, and so not known to be a class.
5278 That's not right; we need to look in uninstantiated
5279 templates. A further complication arises from:
5281 template <typename T> void f(T t) {
5282 t.T::~T();
5285 Here, it is not possible to look up `T' in the scope of `T'
5286 itself. We must look in both the current scope, and the
5287 scope of the containing complete expression.
5289 Yet another issue is:
5291 struct S {
5292 int S;
5293 ~S();
5296 S::~S() {}
5298 The standard does not seem to say that the `S' in `~S'
5299 should refer to the type `S' and not the data member
5300 `S::S'. */
5302 /* DR 244 says that we look up the name after the "~" in the
5303 same scope as we looked up the qualifying name. That idea
5304 isn't fully worked out; it's more complicated than that. */
5305 scope = parser->scope;
5306 object_scope = parser->object_scope;
5307 qualifying_scope = parser->qualifying_scope;
5309 /* Check for invalid scopes. */
5310 if (scope == error_mark_node)
5312 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5313 cp_lexer_consume_token (parser->lexer);
5314 return error_mark_node;
5316 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5318 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5319 error_at (token->location,
5320 "scope %qT before %<~%> is not a class-name",
5321 scope);
5322 cp_parser_simulate_error (parser);
5323 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5324 cp_lexer_consume_token (parser->lexer);
5325 return error_mark_node;
5327 gcc_assert (!scope || TYPE_P (scope));
5329 /* If the name is of the form "X::~X" it's OK even if X is a
5330 typedef. */
5331 token = cp_lexer_peek_token (parser->lexer);
5332 if (scope
5333 && token->type == CPP_NAME
5334 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5335 != CPP_LESS)
5336 && (token->u.value == TYPE_IDENTIFIER (scope)
5337 || (CLASS_TYPE_P (scope)
5338 && constructor_name_p (token->u.value, scope))))
5340 cp_lexer_consume_token (parser->lexer);
5341 return build_nt (BIT_NOT_EXPR, scope);
5344 /* ~auto means the destructor of whatever the object is. */
5345 if (cp_parser_is_keyword (token, RID_AUTO))
5347 if (cxx_dialect < cxx14)
5348 pedwarn (input_location, 0,
5349 "%<~auto%> only available with "
5350 "-std=c++14 or -std=gnu++14");
5351 cp_lexer_consume_token (parser->lexer);
5352 return build_nt (BIT_NOT_EXPR, make_auto ());
5355 /* If there was an explicit qualification (S::~T), first look
5356 in the scope given by the qualification (i.e., S).
5358 Note: in the calls to cp_parser_class_name below we pass
5359 typename_type so that lookup finds the injected-class-name
5360 rather than the constructor. */
5361 done = false;
5362 type_decl = NULL_TREE;
5363 if (scope)
5365 cp_parser_parse_tentatively (parser);
5366 type_decl = cp_parser_class_name (parser,
5367 /*typename_keyword_p=*/false,
5368 /*template_keyword_p=*/false,
5369 typename_type,
5370 /*check_dependency=*/false,
5371 /*class_head_p=*/false,
5372 declarator_p);
5373 if (cp_parser_parse_definitely (parser))
5374 done = true;
5376 /* In "N::S::~S", look in "N" as well. */
5377 if (!done && scope && qualifying_scope)
5379 cp_parser_parse_tentatively (parser);
5380 parser->scope = qualifying_scope;
5381 parser->object_scope = NULL_TREE;
5382 parser->qualifying_scope = NULL_TREE;
5383 type_decl
5384 = cp_parser_class_name (parser,
5385 /*typename_keyword_p=*/false,
5386 /*template_keyword_p=*/false,
5387 typename_type,
5388 /*check_dependency=*/false,
5389 /*class_head_p=*/false,
5390 declarator_p);
5391 if (cp_parser_parse_definitely (parser))
5392 done = true;
5394 /* In "p->S::~T", look in the scope given by "*p" as well. */
5395 else if (!done && object_scope)
5397 cp_parser_parse_tentatively (parser);
5398 parser->scope = object_scope;
5399 parser->object_scope = NULL_TREE;
5400 parser->qualifying_scope = NULL_TREE;
5401 type_decl
5402 = cp_parser_class_name (parser,
5403 /*typename_keyword_p=*/false,
5404 /*template_keyword_p=*/false,
5405 typename_type,
5406 /*check_dependency=*/false,
5407 /*class_head_p=*/false,
5408 declarator_p);
5409 if (cp_parser_parse_definitely (parser))
5410 done = true;
5412 /* Look in the surrounding context. */
5413 if (!done)
5415 parser->scope = NULL_TREE;
5416 parser->object_scope = NULL_TREE;
5417 parser->qualifying_scope = NULL_TREE;
5418 if (processing_template_decl)
5419 cp_parser_parse_tentatively (parser);
5420 type_decl
5421 = cp_parser_class_name (parser,
5422 /*typename_keyword_p=*/false,
5423 /*template_keyword_p=*/false,
5424 typename_type,
5425 /*check_dependency=*/false,
5426 /*class_head_p=*/false,
5427 declarator_p);
5428 if (processing_template_decl
5429 && ! cp_parser_parse_definitely (parser))
5431 /* We couldn't find a type with this name. If we're parsing
5432 tentatively, fail and try something else. */
5433 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5435 cp_parser_simulate_error (parser);
5436 return error_mark_node;
5438 /* Otherwise, accept it and check for a match at instantiation
5439 time. */
5440 type_decl = cp_parser_identifier (parser);
5441 if (type_decl != error_mark_node)
5442 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5443 return type_decl;
5446 /* If an error occurred, assume that the name of the
5447 destructor is the same as the name of the qualifying
5448 class. That allows us to keep parsing after running
5449 into ill-formed destructor names. */
5450 if (type_decl == error_mark_node && scope)
5451 return build_nt (BIT_NOT_EXPR, scope);
5452 else if (type_decl == error_mark_node)
5453 return error_mark_node;
5455 /* Check that destructor name and scope match. */
5456 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5458 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5459 error_at (token->location,
5460 "declaration of %<~%T%> as member of %qT",
5461 type_decl, scope);
5462 cp_parser_simulate_error (parser);
5463 return error_mark_node;
5466 /* [class.dtor]
5468 A typedef-name that names a class shall not be used as the
5469 identifier in the declarator for a destructor declaration. */
5470 if (declarator_p
5471 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5472 && !DECL_SELF_REFERENCE_P (type_decl)
5473 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5474 error_at (token->location,
5475 "typedef-name %qD used as destructor declarator",
5476 type_decl);
5478 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5481 case CPP_KEYWORD:
5482 if (token->keyword == RID_OPERATOR)
5484 tree id;
5486 /* This could be a template-id, so we try that first. */
5487 cp_parser_parse_tentatively (parser);
5488 /* Try a template-id. */
5489 id = cp_parser_template_id (parser, template_keyword_p,
5490 /*check_dependency_p=*/true,
5491 none_type,
5492 declarator_p);
5493 /* If that worked, we're done. */
5494 if (cp_parser_parse_definitely (parser))
5495 return id;
5496 /* We still don't know whether we're looking at an
5497 operator-function-id or a conversion-function-id. */
5498 cp_parser_parse_tentatively (parser);
5499 /* Try an operator-function-id. */
5500 id = cp_parser_operator_function_id (parser);
5501 /* If that didn't work, try a conversion-function-id. */
5502 if (!cp_parser_parse_definitely (parser))
5503 id = cp_parser_conversion_function_id (parser);
5504 else if (UDLIT_OPER_P (id))
5506 /* 17.6.3.3.5 */
5507 const char *name = UDLIT_OP_SUFFIX (id);
5508 if (name[0] != '_' && !in_system_header_at (input_location)
5509 && declarator_p)
5510 warning (0, "literal operator suffixes not preceded by %<_%>"
5511 " are reserved for future standardization");
5514 return id;
5516 /* Fall through. */
5518 default:
5519 if (optional_p)
5520 return NULL_TREE;
5521 cp_parser_error (parser, "expected unqualified-id");
5522 return error_mark_node;
5526 /* Parse an (optional) nested-name-specifier.
5528 nested-name-specifier: [C++98]
5529 class-or-namespace-name :: nested-name-specifier [opt]
5530 class-or-namespace-name :: template nested-name-specifier [opt]
5532 nested-name-specifier: [C++0x]
5533 type-name ::
5534 namespace-name ::
5535 nested-name-specifier identifier ::
5536 nested-name-specifier template [opt] simple-template-id ::
5538 PARSER->SCOPE should be set appropriately before this function is
5539 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5540 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5541 in name lookups.
5543 Sets PARSER->SCOPE to the class (TYPE) or namespace
5544 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5545 it unchanged if there is no nested-name-specifier. Returns the new
5546 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5548 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5549 part of a declaration and/or decl-specifier. */
5551 static tree
5552 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5553 bool typename_keyword_p,
5554 bool check_dependency_p,
5555 bool type_p,
5556 bool is_declaration)
5558 bool success = false;
5559 cp_token_position start = 0;
5560 cp_token *token;
5562 /* Remember where the nested-name-specifier starts. */
5563 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5565 start = cp_lexer_token_position (parser->lexer, false);
5566 push_deferring_access_checks (dk_deferred);
5569 while (true)
5571 tree new_scope;
5572 tree old_scope;
5573 tree saved_qualifying_scope;
5574 bool template_keyword_p;
5576 /* Spot cases that cannot be the beginning of a
5577 nested-name-specifier. */
5578 token = cp_lexer_peek_token (parser->lexer);
5580 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5581 the already parsed nested-name-specifier. */
5582 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5584 /* Grab the nested-name-specifier and continue the loop. */
5585 cp_parser_pre_parsed_nested_name_specifier (parser);
5586 /* If we originally encountered this nested-name-specifier
5587 with IS_DECLARATION set to false, we will not have
5588 resolved TYPENAME_TYPEs, so we must do so here. */
5589 if (is_declaration
5590 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5592 new_scope = resolve_typename_type (parser->scope,
5593 /*only_current_p=*/false);
5594 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5595 parser->scope = new_scope;
5597 success = true;
5598 continue;
5601 /* Spot cases that cannot be the beginning of a
5602 nested-name-specifier. On the second and subsequent times
5603 through the loop, we look for the `template' keyword. */
5604 if (success && token->keyword == RID_TEMPLATE)
5606 /* A template-id can start a nested-name-specifier. */
5607 else if (token->type == CPP_TEMPLATE_ID)
5609 /* DR 743: decltype can be used in a nested-name-specifier. */
5610 else if (token_is_decltype (token))
5612 else
5614 /* If the next token is not an identifier, then it is
5615 definitely not a type-name or namespace-name. */
5616 if (token->type != CPP_NAME)
5617 break;
5618 /* If the following token is neither a `<' (to begin a
5619 template-id), nor a `::', then we are not looking at a
5620 nested-name-specifier. */
5621 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5623 if (token->type == CPP_COLON
5624 && parser->colon_corrects_to_scope_p
5625 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5627 error_at (token->location,
5628 "found %<:%> in nested-name-specifier, expected %<::%>");
5629 token->type = CPP_SCOPE;
5632 if (token->type != CPP_SCOPE
5633 && !cp_parser_nth_token_starts_template_argument_list_p
5634 (parser, 2))
5635 break;
5638 /* The nested-name-specifier is optional, so we parse
5639 tentatively. */
5640 cp_parser_parse_tentatively (parser);
5642 /* Look for the optional `template' keyword, if this isn't the
5643 first time through the loop. */
5644 if (success)
5645 template_keyword_p = cp_parser_optional_template_keyword (parser);
5646 else
5647 template_keyword_p = false;
5649 /* Save the old scope since the name lookup we are about to do
5650 might destroy it. */
5651 old_scope = parser->scope;
5652 saved_qualifying_scope = parser->qualifying_scope;
5653 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5654 look up names in "X<T>::I" in order to determine that "Y" is
5655 a template. So, if we have a typename at this point, we make
5656 an effort to look through it. */
5657 if (is_declaration
5658 && !typename_keyword_p
5659 && parser->scope
5660 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5661 parser->scope = resolve_typename_type (parser->scope,
5662 /*only_current_p=*/false);
5663 /* Parse the qualifying entity. */
5664 new_scope
5665 = cp_parser_qualifying_entity (parser,
5666 typename_keyword_p,
5667 template_keyword_p,
5668 check_dependency_p,
5669 type_p,
5670 is_declaration);
5671 /* Look for the `::' token. */
5672 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5674 /* If we found what we wanted, we keep going; otherwise, we're
5675 done. */
5676 if (!cp_parser_parse_definitely (parser))
5678 bool error_p = false;
5680 /* Restore the OLD_SCOPE since it was valid before the
5681 failed attempt at finding the last
5682 class-or-namespace-name. */
5683 parser->scope = old_scope;
5684 parser->qualifying_scope = saved_qualifying_scope;
5686 /* If the next token is a decltype, and the one after that is a
5687 `::', then the decltype has failed to resolve to a class or
5688 enumeration type. Give this error even when parsing
5689 tentatively since it can't possibly be valid--and we're going
5690 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5691 won't get another chance.*/
5692 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5693 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5694 == CPP_SCOPE))
5696 token = cp_lexer_consume_token (parser->lexer);
5697 error_at (token->location, "decltype evaluates to %qT, "
5698 "which is not a class or enumeration type",
5699 token->u.value);
5700 parser->scope = error_mark_node;
5701 error_p = true;
5702 /* As below. */
5703 success = true;
5704 cp_lexer_consume_token (parser->lexer);
5707 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
5708 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
5710 /* If we have a non-type template-id followed by ::, it can't
5711 possibly be valid. */
5712 token = cp_lexer_peek_token (parser->lexer);
5713 tree tid = token->u.tree_check_value->value;
5714 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
5715 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
5717 tree tmpl = NULL_TREE;
5718 if (is_overloaded_fn (tid))
5720 tree fns = get_fns (tid);
5721 if (!OVL_CHAIN (fns))
5722 tmpl = OVL_CURRENT (fns);
5723 error_at (token->location, "function template-id %qD "
5724 "in nested-name-specifier", tid);
5726 else
5728 /* Variable template. */
5729 tmpl = TREE_OPERAND (tid, 0);
5730 gcc_assert (variable_template_p (tmpl));
5731 error_at (token->location, "variable template-id %qD "
5732 "in nested-name-specifier", tid);
5734 if (tmpl)
5735 inform (DECL_SOURCE_LOCATION (tmpl),
5736 "%qD declared here", tmpl);
5738 parser->scope = error_mark_node;
5739 error_p = true;
5740 /* As below. */
5741 success = true;
5742 cp_lexer_consume_token (parser->lexer);
5743 cp_lexer_consume_token (parser->lexer);
5747 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5748 break;
5749 /* If the next token is an identifier, and the one after
5750 that is a `::', then any valid interpretation would have
5751 found a class-or-namespace-name. */
5752 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5753 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5754 == CPP_SCOPE)
5755 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5756 != CPP_COMPL))
5758 token = cp_lexer_consume_token (parser->lexer);
5759 if (!error_p)
5761 if (!token->error_reported)
5763 tree decl;
5764 tree ambiguous_decls;
5766 decl = cp_parser_lookup_name (parser, token->u.value,
5767 none_type,
5768 /*is_template=*/false,
5769 /*is_namespace=*/false,
5770 /*check_dependency=*/true,
5771 &ambiguous_decls,
5772 token->location);
5773 if (TREE_CODE (decl) == TEMPLATE_DECL)
5774 error_at (token->location,
5775 "%qD used without template parameters",
5776 decl);
5777 else if (ambiguous_decls)
5779 // cp_parser_lookup_name has the same diagnostic,
5780 // thus make sure to emit it at most once.
5781 if (cp_parser_uncommitted_to_tentative_parse_p
5782 (parser))
5784 error_at (token->location,
5785 "reference to %qD is ambiguous",
5786 token->u.value);
5787 print_candidates (ambiguous_decls);
5789 decl = error_mark_node;
5791 else
5793 if (cxx_dialect != cxx98)
5794 cp_parser_name_lookup_error
5795 (parser, token->u.value, decl, NLE_NOT_CXX98,
5796 token->location);
5797 else
5798 cp_parser_name_lookup_error
5799 (parser, token->u.value, decl, NLE_CXX98,
5800 token->location);
5803 parser->scope = error_mark_node;
5804 error_p = true;
5805 /* Treat this as a successful nested-name-specifier
5806 due to:
5808 [basic.lookup.qual]
5810 If the name found is not a class-name (clause
5811 _class_) or namespace-name (_namespace.def_), the
5812 program is ill-formed. */
5813 success = true;
5815 cp_lexer_consume_token (parser->lexer);
5817 break;
5819 /* We've found one valid nested-name-specifier. */
5820 success = true;
5821 /* Name lookup always gives us a DECL. */
5822 if (TREE_CODE (new_scope) == TYPE_DECL)
5823 new_scope = TREE_TYPE (new_scope);
5824 /* Uses of "template" must be followed by actual templates. */
5825 if (template_keyword_p
5826 && !(CLASS_TYPE_P (new_scope)
5827 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5828 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5829 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5830 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5831 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5832 == TEMPLATE_ID_EXPR)))
5833 permerror (input_location, TYPE_P (new_scope)
5834 ? G_("%qT is not a template")
5835 : G_("%qD is not a template"),
5836 new_scope);
5837 /* If it is a class scope, try to complete it; we are about to
5838 be looking up names inside the class. */
5839 if (TYPE_P (new_scope)
5840 /* Since checking types for dependency can be expensive,
5841 avoid doing it if the type is already complete. */
5842 && !COMPLETE_TYPE_P (new_scope)
5843 /* Do not try to complete dependent types. */
5844 && !dependent_type_p (new_scope))
5846 new_scope = complete_type (new_scope);
5847 /* If it is a typedef to current class, use the current
5848 class instead, as the typedef won't have any names inside
5849 it yet. */
5850 if (!COMPLETE_TYPE_P (new_scope)
5851 && currently_open_class (new_scope))
5852 new_scope = TYPE_MAIN_VARIANT (new_scope);
5854 /* Make sure we look in the right scope the next time through
5855 the loop. */
5856 parser->scope = new_scope;
5859 /* If parsing tentatively, replace the sequence of tokens that makes
5860 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5861 token. That way, should we re-parse the token stream, we will
5862 not have to repeat the effort required to do the parse, nor will
5863 we issue duplicate error messages. */
5864 if (success && start)
5866 cp_token *token;
5868 token = cp_lexer_token_at (parser->lexer, start);
5869 /* Reset the contents of the START token. */
5870 token->type = CPP_NESTED_NAME_SPECIFIER;
5871 /* Retrieve any deferred checks. Do not pop this access checks yet
5872 so the memory will not be reclaimed during token replacing below. */
5873 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
5874 token->u.tree_check_value->value = parser->scope;
5875 token->u.tree_check_value->checks = get_deferred_access_checks ();
5876 token->u.tree_check_value->qualifying_scope =
5877 parser->qualifying_scope;
5878 token->keyword = RID_MAX;
5880 /* Purge all subsequent tokens. */
5881 cp_lexer_purge_tokens_after (parser->lexer, start);
5884 if (start)
5885 pop_to_parent_deferring_access_checks ();
5887 return success ? parser->scope : NULL_TREE;
5890 /* Parse a nested-name-specifier. See
5891 cp_parser_nested_name_specifier_opt for details. This function
5892 behaves identically, except that it will an issue an error if no
5893 nested-name-specifier is present. */
5895 static tree
5896 cp_parser_nested_name_specifier (cp_parser *parser,
5897 bool typename_keyword_p,
5898 bool check_dependency_p,
5899 bool type_p,
5900 bool is_declaration)
5902 tree scope;
5904 /* Look for the nested-name-specifier. */
5905 scope = cp_parser_nested_name_specifier_opt (parser,
5906 typename_keyword_p,
5907 check_dependency_p,
5908 type_p,
5909 is_declaration);
5910 /* If it was not present, issue an error message. */
5911 if (!scope)
5913 cp_parser_error (parser, "expected nested-name-specifier");
5914 parser->scope = NULL_TREE;
5917 return scope;
5920 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5921 this is either a class-name or a namespace-name (which corresponds
5922 to the class-or-namespace-name production in the grammar). For
5923 C++0x, it can also be a type-name that refers to an enumeration
5924 type or a simple-template-id.
5926 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5927 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5928 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5929 TYPE_P is TRUE iff the next name should be taken as a class-name,
5930 even the same name is declared to be another entity in the same
5931 scope.
5933 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5934 specified by the class-or-namespace-name. If neither is found the
5935 ERROR_MARK_NODE is returned. */
5937 static tree
5938 cp_parser_qualifying_entity (cp_parser *parser,
5939 bool typename_keyword_p,
5940 bool template_keyword_p,
5941 bool check_dependency_p,
5942 bool type_p,
5943 bool is_declaration)
5945 tree saved_scope;
5946 tree saved_qualifying_scope;
5947 tree saved_object_scope;
5948 tree scope;
5949 bool only_class_p;
5950 bool successful_parse_p;
5952 /* DR 743: decltype can appear in a nested-name-specifier. */
5953 if (cp_lexer_next_token_is_decltype (parser->lexer))
5955 scope = cp_parser_decltype (parser);
5956 if (TREE_CODE (scope) != ENUMERAL_TYPE
5957 && !MAYBE_CLASS_TYPE_P (scope))
5959 cp_parser_simulate_error (parser);
5960 return error_mark_node;
5962 if (TYPE_NAME (scope))
5963 scope = TYPE_NAME (scope);
5964 return scope;
5967 /* Before we try to parse the class-name, we must save away the
5968 current PARSER->SCOPE since cp_parser_class_name will destroy
5969 it. */
5970 saved_scope = parser->scope;
5971 saved_qualifying_scope = parser->qualifying_scope;
5972 saved_object_scope = parser->object_scope;
5973 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5974 there is no need to look for a namespace-name. */
5975 only_class_p = template_keyword_p
5976 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5977 if (!only_class_p)
5978 cp_parser_parse_tentatively (parser);
5979 scope = cp_parser_class_name (parser,
5980 typename_keyword_p,
5981 template_keyword_p,
5982 type_p ? class_type : none_type,
5983 check_dependency_p,
5984 /*class_head_p=*/false,
5985 is_declaration,
5986 /*enum_ok=*/cxx_dialect > cxx98);
5987 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5988 /* If that didn't work, try for a namespace-name. */
5989 if (!only_class_p && !successful_parse_p)
5991 /* Restore the saved scope. */
5992 parser->scope = saved_scope;
5993 parser->qualifying_scope = saved_qualifying_scope;
5994 parser->object_scope = saved_object_scope;
5995 /* If we are not looking at an identifier followed by the scope
5996 resolution operator, then this is not part of a
5997 nested-name-specifier. (Note that this function is only used
5998 to parse the components of a nested-name-specifier.) */
5999 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6000 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6001 return error_mark_node;
6002 scope = cp_parser_namespace_name (parser);
6005 return scope;
6008 /* Return true if we are looking at a compound-literal, false otherwise. */
6010 static bool
6011 cp_parser_compound_literal_p (cp_parser *parser)
6013 /* Consume the `('. */
6014 cp_lexer_consume_token (parser->lexer);
6016 cp_lexer_save_tokens (parser->lexer);
6018 /* Skip tokens until the next token is a closing parenthesis.
6019 If we find the closing `)', and the next token is a `{', then
6020 we are looking at a compound-literal. */
6021 bool compound_literal_p
6022 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6023 /*consume_paren=*/true)
6024 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6026 /* Roll back the tokens we skipped. */
6027 cp_lexer_rollback_tokens (parser->lexer);
6029 return compound_literal_p;
6032 /* Parse a postfix-expression.
6034 postfix-expression:
6035 primary-expression
6036 postfix-expression [ expression ]
6037 postfix-expression ( expression-list [opt] )
6038 simple-type-specifier ( expression-list [opt] )
6039 typename :: [opt] nested-name-specifier identifier
6040 ( expression-list [opt] )
6041 typename :: [opt] nested-name-specifier template [opt] template-id
6042 ( expression-list [opt] )
6043 postfix-expression . template [opt] id-expression
6044 postfix-expression -> template [opt] id-expression
6045 postfix-expression . pseudo-destructor-name
6046 postfix-expression -> pseudo-destructor-name
6047 postfix-expression ++
6048 postfix-expression --
6049 dynamic_cast < type-id > ( expression )
6050 static_cast < type-id > ( expression )
6051 reinterpret_cast < type-id > ( expression )
6052 const_cast < type-id > ( expression )
6053 typeid ( expression )
6054 typeid ( type-id )
6056 GNU Extension:
6058 postfix-expression:
6059 ( type-id ) { initializer-list , [opt] }
6061 This extension is a GNU version of the C99 compound-literal
6062 construct. (The C99 grammar uses `type-name' instead of `type-id',
6063 but they are essentially the same concept.)
6065 If ADDRESS_P is true, the postfix expression is the operand of the
6066 `&' operator. CAST_P is true if this expression is the target of a
6067 cast.
6069 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6070 class member access expressions [expr.ref].
6072 Returns a representation of the expression. */
6074 static tree
6075 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6076 bool member_access_only_p, bool decltype_p,
6077 cp_id_kind * pidk_return)
6079 cp_token *token;
6080 location_t loc;
6081 enum rid keyword;
6082 cp_id_kind idk = CP_ID_KIND_NONE;
6083 tree postfix_expression = NULL_TREE;
6084 bool is_member_access = false;
6085 int saved_in_statement = -1;
6087 /* Peek at the next token. */
6088 token = cp_lexer_peek_token (parser->lexer);
6089 loc = token->location;
6090 /* Some of the productions are determined by keywords. */
6091 keyword = token->keyword;
6092 switch (keyword)
6094 case RID_DYNCAST:
6095 case RID_STATCAST:
6096 case RID_REINTCAST:
6097 case RID_CONSTCAST:
6099 tree type;
6100 tree expression;
6101 const char *saved_message;
6102 bool saved_in_type_id_in_expr_p;
6104 /* All of these can be handled in the same way from the point
6105 of view of parsing. Begin by consuming the token
6106 identifying the cast. */
6107 cp_lexer_consume_token (parser->lexer);
6109 /* New types cannot be defined in the cast. */
6110 saved_message = parser->type_definition_forbidden_message;
6111 parser->type_definition_forbidden_message
6112 = G_("types may not be defined in casts");
6114 /* Look for the opening `<'. */
6115 cp_parser_require (parser, CPP_LESS, RT_LESS);
6116 /* Parse the type to which we are casting. */
6117 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6118 parser->in_type_id_in_expr_p = true;
6119 type = cp_parser_type_id (parser);
6120 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6121 /* Look for the closing `>'. */
6122 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6123 /* Restore the old message. */
6124 parser->type_definition_forbidden_message = saved_message;
6126 bool saved_greater_than_is_operator_p
6127 = parser->greater_than_is_operator_p;
6128 parser->greater_than_is_operator_p = true;
6130 /* And the expression which is being cast. */
6131 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6132 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6133 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6135 parser->greater_than_is_operator_p
6136 = saved_greater_than_is_operator_p;
6138 /* Only type conversions to integral or enumeration types
6139 can be used in constant-expressions. */
6140 if (!cast_valid_in_integral_constant_expression_p (type)
6141 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6142 return error_mark_node;
6144 switch (keyword)
6146 case RID_DYNCAST:
6147 postfix_expression
6148 = build_dynamic_cast (type, expression, tf_warning_or_error);
6149 break;
6150 case RID_STATCAST:
6151 postfix_expression
6152 = build_static_cast (type, expression, tf_warning_or_error);
6153 break;
6154 case RID_REINTCAST:
6155 postfix_expression
6156 = build_reinterpret_cast (type, expression,
6157 tf_warning_or_error);
6158 break;
6159 case RID_CONSTCAST:
6160 postfix_expression
6161 = build_const_cast (type, expression, tf_warning_or_error);
6162 break;
6163 default:
6164 gcc_unreachable ();
6167 break;
6169 case RID_TYPEID:
6171 tree type;
6172 const char *saved_message;
6173 bool saved_in_type_id_in_expr_p;
6175 /* Consume the `typeid' token. */
6176 cp_lexer_consume_token (parser->lexer);
6177 /* Look for the `(' token. */
6178 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6179 /* Types cannot be defined in a `typeid' expression. */
6180 saved_message = parser->type_definition_forbidden_message;
6181 parser->type_definition_forbidden_message
6182 = G_("types may not be defined in a %<typeid%> expression");
6183 /* We can't be sure yet whether we're looking at a type-id or an
6184 expression. */
6185 cp_parser_parse_tentatively (parser);
6186 /* Try a type-id first. */
6187 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6188 parser->in_type_id_in_expr_p = true;
6189 type = cp_parser_type_id (parser);
6190 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6191 /* Look for the `)' token. Otherwise, we can't be sure that
6192 we're not looking at an expression: consider `typeid (int
6193 (3))', for example. */
6194 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6195 /* If all went well, simply lookup the type-id. */
6196 if (cp_parser_parse_definitely (parser))
6197 postfix_expression = get_typeid (type, tf_warning_or_error);
6198 /* Otherwise, fall back to the expression variant. */
6199 else
6201 tree expression;
6203 /* Look for an expression. */
6204 expression = cp_parser_expression (parser, & idk);
6205 /* Compute its typeid. */
6206 postfix_expression = build_typeid (expression, tf_warning_or_error);
6207 /* Look for the `)' token. */
6208 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6210 /* Restore the saved message. */
6211 parser->type_definition_forbidden_message = saved_message;
6212 /* `typeid' may not appear in an integral constant expression. */
6213 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6214 return error_mark_node;
6216 break;
6218 case RID_TYPENAME:
6220 tree type;
6221 /* The syntax permitted here is the same permitted for an
6222 elaborated-type-specifier. */
6223 ++parser->prevent_constrained_type_specifiers;
6224 type = cp_parser_elaborated_type_specifier (parser,
6225 /*is_friend=*/false,
6226 /*is_declaration=*/false);
6227 --parser->prevent_constrained_type_specifiers;
6228 postfix_expression = cp_parser_functional_cast (parser, type);
6230 break;
6232 case RID_CILK_SPAWN:
6234 cp_lexer_consume_token (parser->lexer);
6235 token = cp_lexer_peek_token (parser->lexer);
6236 if (token->type == CPP_SEMICOLON)
6238 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6239 "an expression");
6240 postfix_expression = error_mark_node;
6241 break;
6243 else if (!current_function_decl)
6245 error_at (token->location, "%<_Cilk_spawn%> may only be used "
6246 "inside a function");
6247 postfix_expression = error_mark_node;
6248 break;
6250 else
6252 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6253 saved_in_statement = parser->in_statement;
6254 parser->in_statement |= IN_CILK_SPAWN;
6256 cfun->calls_cilk_spawn = 1;
6257 postfix_expression =
6258 cp_parser_postfix_expression (parser, false, false,
6259 false, false, &idk);
6260 if (!flag_cilkplus)
6262 error_at (token->location, "-fcilkplus must be enabled to use"
6263 " %<_Cilk_spawn%>");
6264 cfun->calls_cilk_spawn = 0;
6266 else if (saved_in_statement & IN_CILK_SPAWN)
6268 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6269 "are not permitted");
6270 postfix_expression = error_mark_node;
6271 cfun->calls_cilk_spawn = 0;
6273 else
6275 postfix_expression = build_cilk_spawn (token->location,
6276 postfix_expression);
6277 if (postfix_expression != error_mark_node)
6278 SET_EXPR_LOCATION (postfix_expression, input_location);
6279 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6281 break;
6284 case RID_BUILTIN_SHUFFLE:
6286 vec<tree, va_gc> *vec;
6287 unsigned int i;
6288 tree p;
6290 cp_lexer_consume_token (parser->lexer);
6291 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6292 /*cast_p=*/false, /*allow_expansion_p=*/true,
6293 /*non_constant_p=*/NULL);
6294 if (vec == NULL)
6295 return error_mark_node;
6297 FOR_EACH_VEC_ELT (*vec, i, p)
6298 mark_exp_read (p);
6300 if (vec->length () == 2)
6301 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
6302 tf_warning_or_error);
6303 else if (vec->length () == 3)
6304 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6305 tf_warning_or_error);
6306 else
6308 error_at (loc, "wrong number of arguments to "
6309 "%<__builtin_shuffle%>");
6310 return error_mark_node;
6312 break;
6315 default:
6317 tree type;
6319 /* If the next thing is a simple-type-specifier, we may be
6320 looking at a functional cast. We could also be looking at
6321 an id-expression. So, we try the functional cast, and if
6322 that doesn't work we fall back to the primary-expression. */
6323 cp_parser_parse_tentatively (parser);
6324 /* Look for the simple-type-specifier. */
6325 ++parser->prevent_constrained_type_specifiers;
6326 type = cp_parser_simple_type_specifier (parser,
6327 /*decl_specs=*/NULL,
6328 CP_PARSER_FLAGS_NONE);
6329 --parser->prevent_constrained_type_specifiers;
6330 /* Parse the cast itself. */
6331 if (!cp_parser_error_occurred (parser))
6332 postfix_expression
6333 = cp_parser_functional_cast (parser, type);
6334 /* If that worked, we're done. */
6335 if (cp_parser_parse_definitely (parser))
6336 break;
6338 /* If the functional-cast didn't work out, try a
6339 compound-literal. */
6340 if (cp_parser_allow_gnu_extensions_p (parser)
6341 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6343 tree initializer = NULL_TREE;
6345 cp_parser_parse_tentatively (parser);
6347 /* Avoid calling cp_parser_type_id pointlessly, see comment
6348 in cp_parser_cast_expression about c++/29234. */
6349 if (!cp_parser_compound_literal_p (parser))
6350 cp_parser_simulate_error (parser);
6351 else
6353 /* Parse the type. */
6354 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6355 parser->in_type_id_in_expr_p = true;
6356 type = cp_parser_type_id (parser);
6357 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6358 /* Look for the `)'. */
6359 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6362 /* If things aren't going well, there's no need to
6363 keep going. */
6364 if (!cp_parser_error_occurred (parser))
6366 bool non_constant_p;
6367 /* Parse the brace-enclosed initializer list. */
6368 initializer = cp_parser_braced_list (parser,
6369 &non_constant_p);
6371 /* If that worked, we're definitely looking at a
6372 compound-literal expression. */
6373 if (cp_parser_parse_definitely (parser))
6375 /* Warn the user that a compound literal is not
6376 allowed in standard C++. */
6377 pedwarn (input_location, OPT_Wpedantic,
6378 "ISO C++ forbids compound-literals");
6379 /* For simplicity, we disallow compound literals in
6380 constant-expressions. We could
6381 allow compound literals of integer type, whose
6382 initializer was a constant, in constant
6383 expressions. Permitting that usage, as a further
6384 extension, would not change the meaning of any
6385 currently accepted programs. (Of course, as
6386 compound literals are not part of ISO C++, the
6387 standard has nothing to say.) */
6388 if (cp_parser_non_integral_constant_expression (parser,
6389 NIC_NCC))
6391 postfix_expression = error_mark_node;
6392 break;
6394 /* Form the representation of the compound-literal. */
6395 postfix_expression
6396 = finish_compound_literal (type, initializer,
6397 tf_warning_or_error);
6398 break;
6402 /* It must be a primary-expression. */
6403 postfix_expression
6404 = cp_parser_primary_expression (parser, address_p, cast_p,
6405 /*template_arg_p=*/false,
6406 decltype_p,
6407 &idk);
6409 break;
6412 /* Note that we don't need to worry about calling build_cplus_new on a
6413 class-valued CALL_EXPR in decltype when it isn't the end of the
6414 postfix-expression; unary_complex_lvalue will take care of that for
6415 all these cases. */
6417 /* Keep looping until the postfix-expression is complete. */
6418 while (true)
6420 if (idk == CP_ID_KIND_UNQUALIFIED
6421 && identifier_p (postfix_expression)
6422 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6423 /* It is not a Koenig lookup function call. */
6424 postfix_expression
6425 = unqualified_name_lookup_error (postfix_expression);
6427 /* Peek at the next token. */
6428 token = cp_lexer_peek_token (parser->lexer);
6430 switch (token->type)
6432 case CPP_OPEN_SQUARE:
6433 if (cp_next_tokens_can_be_std_attribute_p (parser))
6435 cp_parser_error (parser,
6436 "two consecutive %<[%> shall "
6437 "only introduce an attribute");
6438 return error_mark_node;
6440 postfix_expression
6441 = cp_parser_postfix_open_square_expression (parser,
6442 postfix_expression,
6443 false,
6444 decltype_p);
6445 idk = CP_ID_KIND_NONE;
6446 is_member_access = false;
6447 break;
6449 case CPP_OPEN_PAREN:
6450 /* postfix-expression ( expression-list [opt] ) */
6452 bool koenig_p;
6453 bool is_builtin_constant_p;
6454 bool saved_integral_constant_expression_p = false;
6455 bool saved_non_integral_constant_expression_p = false;
6456 tsubst_flags_t complain = complain_flags (decltype_p);
6457 vec<tree, va_gc> *args;
6459 is_member_access = false;
6461 is_builtin_constant_p
6462 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6463 if (is_builtin_constant_p)
6465 /* The whole point of __builtin_constant_p is to allow
6466 non-constant expressions to appear as arguments. */
6467 saved_integral_constant_expression_p
6468 = parser->integral_constant_expression_p;
6469 saved_non_integral_constant_expression_p
6470 = parser->non_integral_constant_expression_p;
6471 parser->integral_constant_expression_p = false;
6473 args = (cp_parser_parenthesized_expression_list
6474 (parser, non_attr,
6475 /*cast_p=*/false, /*allow_expansion_p=*/true,
6476 /*non_constant_p=*/NULL,
6477 /*want_literal_zero_p=*/warn_memset_transposed_args));
6478 if (is_builtin_constant_p)
6480 parser->integral_constant_expression_p
6481 = saved_integral_constant_expression_p;
6482 parser->non_integral_constant_expression_p
6483 = saved_non_integral_constant_expression_p;
6486 if (args == NULL)
6488 postfix_expression = error_mark_node;
6489 break;
6492 /* Function calls are not permitted in
6493 constant-expressions. */
6494 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6495 && cp_parser_non_integral_constant_expression (parser,
6496 NIC_FUNC_CALL))
6498 postfix_expression = error_mark_node;
6499 release_tree_vector (args);
6500 break;
6503 koenig_p = false;
6504 if (idk == CP_ID_KIND_UNQUALIFIED
6505 || idk == CP_ID_KIND_TEMPLATE_ID)
6507 if (identifier_p (postfix_expression))
6509 if (!args->is_empty ())
6511 koenig_p = true;
6512 if (!any_type_dependent_arguments_p (args))
6513 postfix_expression
6514 = perform_koenig_lookup (postfix_expression, args,
6515 complain);
6517 else
6518 postfix_expression
6519 = unqualified_fn_lookup_error (postfix_expression);
6521 /* We do not perform argument-dependent lookup if
6522 normal lookup finds a non-function, in accordance
6523 with the expected resolution of DR 218. */
6524 else if (!args->is_empty ()
6525 && is_overloaded_fn (postfix_expression))
6527 tree fn = get_first_fn (postfix_expression);
6528 fn = STRIP_TEMPLATE (fn);
6530 /* Do not do argument dependent lookup if regular
6531 lookup finds a member function or a block-scope
6532 function declaration. [basic.lookup.argdep]/3 */
6533 if (!DECL_FUNCTION_MEMBER_P (fn)
6534 && !DECL_LOCAL_FUNCTION_P (fn))
6536 koenig_p = true;
6537 if (!any_type_dependent_arguments_p (args))
6538 postfix_expression
6539 = perform_koenig_lookup (postfix_expression, args,
6540 complain);
6545 if (warn_memset_transposed_args)
6547 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6548 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6549 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6550 && vec_safe_length (args) == 3
6551 && integer_zerop ((*args)[2])
6552 && LITERAL_ZERO_P ((*args)[2])
6553 && !(integer_zerop ((*args)[1])
6554 && LITERAL_ZERO_P ((*args)[1])))
6555 warning (OPT_Wmemset_transposed_args,
6556 "%<memset%> used with constant zero length "
6557 "parameter; this could be due to transposed "
6558 "parameters");
6560 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6561 to avoid leaking those into folder and middle-end. */
6562 unsigned int i;
6563 tree arg;
6564 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6565 if (TREE_CODE (arg) == INTEGER_CST && LITERAL_ZERO_P (arg))
6566 (*args)[i] = build_int_cst (TREE_TYPE (arg), 0);
6569 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6571 tree instance = TREE_OPERAND (postfix_expression, 0);
6572 tree fn = TREE_OPERAND (postfix_expression, 1);
6574 if (processing_template_decl
6575 && (type_dependent_expression_p (instance)
6576 || (!BASELINK_P (fn)
6577 && TREE_CODE (fn) != FIELD_DECL)
6578 || type_dependent_expression_p (fn)
6579 || any_type_dependent_arguments_p (args)))
6581 postfix_expression
6582 = build_nt_call_vec (postfix_expression, args);
6583 release_tree_vector (args);
6584 break;
6587 if (BASELINK_P (fn))
6589 postfix_expression
6590 = (build_new_method_call
6591 (instance, fn, &args, NULL_TREE,
6592 (idk == CP_ID_KIND_QUALIFIED
6593 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6594 : LOOKUP_NORMAL),
6595 /*fn_p=*/NULL,
6596 complain));
6598 else
6599 postfix_expression
6600 = finish_call_expr (postfix_expression, &args,
6601 /*disallow_virtual=*/false,
6602 /*koenig_p=*/false,
6603 complain);
6605 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6606 || TREE_CODE (postfix_expression) == MEMBER_REF
6607 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6608 postfix_expression = (build_offset_ref_call_from_tree
6609 (postfix_expression, &args,
6610 complain));
6611 else if (idk == CP_ID_KIND_QUALIFIED)
6612 /* A call to a static class member, or a namespace-scope
6613 function. */
6614 postfix_expression
6615 = finish_call_expr (postfix_expression, &args,
6616 /*disallow_virtual=*/true,
6617 koenig_p,
6618 complain);
6619 else
6620 /* All other function calls. */
6621 postfix_expression
6622 = finish_call_expr (postfix_expression, &args,
6623 /*disallow_virtual=*/false,
6624 koenig_p,
6625 complain);
6627 protected_set_expr_location (postfix_expression, token->location);
6629 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6630 idk = CP_ID_KIND_NONE;
6632 release_tree_vector (args);
6634 break;
6636 case CPP_DOT:
6637 case CPP_DEREF:
6638 /* postfix-expression . template [opt] id-expression
6639 postfix-expression . pseudo-destructor-name
6640 postfix-expression -> template [opt] id-expression
6641 postfix-expression -> pseudo-destructor-name */
6643 /* Consume the `.' or `->' operator. */
6644 cp_lexer_consume_token (parser->lexer);
6646 postfix_expression
6647 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6648 postfix_expression,
6649 false, &idk, loc);
6651 is_member_access = true;
6652 break;
6654 case CPP_PLUS_PLUS:
6655 /* postfix-expression ++ */
6656 /* Consume the `++' token. */
6657 cp_lexer_consume_token (parser->lexer);
6658 /* Generate a representation for the complete expression. */
6659 postfix_expression
6660 = finish_increment_expr (postfix_expression,
6661 POSTINCREMENT_EXPR);
6662 /* Increments may not appear in constant-expressions. */
6663 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6664 postfix_expression = error_mark_node;
6665 idk = CP_ID_KIND_NONE;
6666 is_member_access = false;
6667 break;
6669 case CPP_MINUS_MINUS:
6670 /* postfix-expression -- */
6671 /* Consume the `--' token. */
6672 cp_lexer_consume_token (parser->lexer);
6673 /* Generate a representation for the complete expression. */
6674 postfix_expression
6675 = finish_increment_expr (postfix_expression,
6676 POSTDECREMENT_EXPR);
6677 /* Decrements may not appear in constant-expressions. */
6678 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6679 postfix_expression = error_mark_node;
6680 idk = CP_ID_KIND_NONE;
6681 is_member_access = false;
6682 break;
6684 default:
6685 if (pidk_return != NULL)
6686 * pidk_return = idk;
6687 if (member_access_only_p)
6688 return is_member_access? postfix_expression : error_mark_node;
6689 else
6690 return postfix_expression;
6694 /* We should never get here. */
6695 gcc_unreachable ();
6696 return error_mark_node;
6699 /* This function parses Cilk Plus array notations. If a normal array expr. is
6700 parsed then the array index is passed back to the caller through *INIT_INDEX
6701 and the function returns a NULL_TREE. If array notation expr. is parsed,
6702 then *INIT_INDEX is ignored by the caller and the function returns
6703 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6704 error_mark_node. */
6706 static tree
6707 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6708 tree array_value)
6710 cp_token *token = NULL;
6711 tree length_index, stride = NULL_TREE, value_tree, array_type;
6712 if (!array_value || array_value == error_mark_node)
6714 cp_parser_skip_to_end_of_statement (parser);
6715 return error_mark_node;
6718 array_type = TREE_TYPE (array_value);
6720 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6721 parser->colon_corrects_to_scope_p = false;
6722 token = cp_lexer_peek_token (parser->lexer);
6724 if (!token)
6726 cp_parser_error (parser, "expected %<:%> or numeral");
6727 return error_mark_node;
6729 else if (token->type == CPP_COLON)
6731 /* Consume the ':'. */
6732 cp_lexer_consume_token (parser->lexer);
6734 /* If we are here, then we have a case like this A[:]. */
6735 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6737 cp_parser_error (parser, "expected %<]%>");
6738 cp_parser_skip_to_end_of_statement (parser);
6739 return error_mark_node;
6741 *init_index = NULL_TREE;
6742 stride = NULL_TREE;
6743 length_index = NULL_TREE;
6745 else
6747 /* If we are here, then there are three valid possibilities:
6748 1. ARRAY [ EXP ]
6749 2. ARRAY [ EXP : EXP ]
6750 3. ARRAY [ EXP : EXP : EXP ] */
6752 *init_index = cp_parser_expression (parser);
6753 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6755 /* This indicates that we have a normal array expression. */
6756 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6757 return NULL_TREE;
6760 /* Consume the ':'. */
6761 cp_lexer_consume_token (parser->lexer);
6762 length_index = cp_parser_expression (parser);
6763 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6765 cp_lexer_consume_token (parser->lexer);
6766 stride = cp_parser_expression (parser);
6769 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6771 if (*init_index == error_mark_node || length_index == error_mark_node
6772 || stride == error_mark_node || array_type == error_mark_node)
6774 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6775 cp_lexer_consume_token (parser->lexer);
6776 return error_mark_node;
6778 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6780 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6781 length_index, stride, array_type);
6782 return value_tree;
6785 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6786 by cp_parser_builtin_offsetof. We're looking for
6788 postfix-expression [ expression ]
6789 postfix-expression [ braced-init-list ] (C++11)
6791 FOR_OFFSETOF is set if we're being called in that context, which
6792 changes how we deal with integer constant expressions. */
6794 static tree
6795 cp_parser_postfix_open_square_expression (cp_parser *parser,
6796 tree postfix_expression,
6797 bool for_offsetof,
6798 bool decltype_p)
6800 tree index = NULL_TREE;
6801 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6802 bool saved_greater_than_is_operator_p;
6804 /* Consume the `[' token. */
6805 cp_lexer_consume_token (parser->lexer);
6807 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6808 parser->greater_than_is_operator_p = true;
6810 /* Parse the index expression. */
6811 /* ??? For offsetof, there is a question of what to allow here. If
6812 offsetof is not being used in an integral constant expression context,
6813 then we *could* get the right answer by computing the value at runtime.
6814 If we are in an integral constant expression context, then we might
6815 could accept any constant expression; hard to say without analysis.
6816 Rather than open the barn door too wide right away, allow only integer
6817 constant expressions here. */
6818 if (for_offsetof)
6819 index = cp_parser_constant_expression (parser);
6820 else
6822 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6824 bool expr_nonconst_p;
6825 cp_lexer_set_source_position (parser->lexer);
6826 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6827 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6828 if (flag_cilkplus
6829 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6831 error_at (cp_lexer_peek_token (parser->lexer)->location,
6832 "braced list index is not allowed with array "
6833 "notation");
6834 cp_parser_skip_to_end_of_statement (parser);
6835 return error_mark_node;
6838 else if (flag_cilkplus)
6840 /* Here are have these two options:
6841 ARRAY[EXP : EXP] - Array notation expr with default
6842 stride of 1.
6843 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6844 stride. */
6845 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6846 postfix_expression);
6847 if (an_exp)
6848 return an_exp;
6850 else
6851 index = cp_parser_expression (parser);
6854 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6856 /* Look for the closing `]'. */
6857 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6859 /* Build the ARRAY_REF. */
6860 postfix_expression = grok_array_decl (loc, postfix_expression,
6861 index, decltype_p);
6863 /* When not doing offsetof, array references are not permitted in
6864 constant-expressions. */
6865 if (!for_offsetof
6866 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6867 postfix_expression = error_mark_node;
6869 return postfix_expression;
6872 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6873 by cp_parser_builtin_offsetof. We're looking for
6875 postfix-expression . template [opt] id-expression
6876 postfix-expression . pseudo-destructor-name
6877 postfix-expression -> template [opt] id-expression
6878 postfix-expression -> pseudo-destructor-name
6880 FOR_OFFSETOF is set if we're being called in that context. That sorta
6881 limits what of the above we'll actually accept, but nevermind.
6882 TOKEN_TYPE is the "." or "->" token, which will already have been
6883 removed from the stream. */
6885 static tree
6886 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6887 enum cpp_ttype token_type,
6888 tree postfix_expression,
6889 bool for_offsetof, cp_id_kind *idk,
6890 location_t location)
6892 tree name;
6893 bool dependent_p;
6894 bool pseudo_destructor_p;
6895 tree scope = NULL_TREE;
6897 /* If this is a `->' operator, dereference the pointer. */
6898 if (token_type == CPP_DEREF)
6899 postfix_expression = build_x_arrow (location, postfix_expression,
6900 tf_warning_or_error);
6901 /* Check to see whether or not the expression is type-dependent. */
6902 dependent_p = type_dependent_expression_p (postfix_expression);
6903 /* The identifier following the `->' or `.' is not qualified. */
6904 parser->scope = NULL_TREE;
6905 parser->qualifying_scope = NULL_TREE;
6906 parser->object_scope = NULL_TREE;
6907 *idk = CP_ID_KIND_NONE;
6909 /* Enter the scope corresponding to the type of the object
6910 given by the POSTFIX_EXPRESSION. */
6911 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6913 scope = TREE_TYPE (postfix_expression);
6914 /* According to the standard, no expression should ever have
6915 reference type. Unfortunately, we do not currently match
6916 the standard in this respect in that our internal representation
6917 of an expression may have reference type even when the standard
6918 says it does not. Therefore, we have to manually obtain the
6919 underlying type here. */
6920 scope = non_reference (scope);
6921 /* The type of the POSTFIX_EXPRESSION must be complete. */
6922 if (scope == unknown_type_node)
6924 error_at (location, "%qE does not have class type",
6925 postfix_expression);
6926 scope = NULL_TREE;
6928 /* Unlike the object expression in other contexts, *this is not
6929 required to be of complete type for purposes of class member
6930 access (5.2.5) outside the member function body. */
6931 else if (postfix_expression != current_class_ref
6932 && !(processing_template_decl && scope == current_class_type))
6933 scope = complete_type_or_else (scope, NULL_TREE);
6934 /* Let the name lookup machinery know that we are processing a
6935 class member access expression. */
6936 parser->context->object_type = scope;
6937 /* If something went wrong, we want to be able to discern that case,
6938 as opposed to the case where there was no SCOPE due to the type
6939 of expression being dependent. */
6940 if (!scope)
6941 scope = error_mark_node;
6942 /* If the SCOPE was erroneous, make the various semantic analysis
6943 functions exit quickly -- and without issuing additional error
6944 messages. */
6945 if (scope == error_mark_node)
6946 postfix_expression = error_mark_node;
6949 /* Assume this expression is not a pseudo-destructor access. */
6950 pseudo_destructor_p = false;
6952 /* If the SCOPE is a scalar type, then, if this is a valid program,
6953 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6954 is type dependent, it can be pseudo-destructor-name or something else.
6955 Try to parse it as pseudo-destructor-name first. */
6956 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6958 tree s;
6959 tree type;
6961 cp_parser_parse_tentatively (parser);
6962 /* Parse the pseudo-destructor-name. */
6963 s = NULL_TREE;
6964 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6965 &s, &type);
6966 if (dependent_p
6967 && (cp_parser_error_occurred (parser)
6968 || !SCALAR_TYPE_P (type)))
6969 cp_parser_abort_tentative_parse (parser);
6970 else if (cp_parser_parse_definitely (parser))
6972 pseudo_destructor_p = true;
6973 postfix_expression
6974 = finish_pseudo_destructor_expr (postfix_expression,
6975 s, type, location);
6979 if (!pseudo_destructor_p)
6981 /* If the SCOPE is not a scalar type, we are looking at an
6982 ordinary class member access expression, rather than a
6983 pseudo-destructor-name. */
6984 bool template_p;
6985 cp_token *token = cp_lexer_peek_token (parser->lexer);
6986 /* Parse the id-expression. */
6987 name = (cp_parser_id_expression
6988 (parser,
6989 cp_parser_optional_template_keyword (parser),
6990 /*check_dependency_p=*/true,
6991 &template_p,
6992 /*declarator_p=*/false,
6993 /*optional_p=*/false));
6994 /* In general, build a SCOPE_REF if the member name is qualified.
6995 However, if the name was not dependent and has already been
6996 resolved; there is no need to build the SCOPE_REF. For example;
6998 struct X { void f(); };
6999 template <typename T> void f(T* t) { t->X::f(); }
7001 Even though "t" is dependent, "X::f" is not and has been resolved
7002 to a BASELINK; there is no need to include scope information. */
7004 /* But we do need to remember that there was an explicit scope for
7005 virtual function calls. */
7006 if (parser->scope)
7007 *idk = CP_ID_KIND_QUALIFIED;
7009 /* If the name is a template-id that names a type, we will get a
7010 TYPE_DECL here. That is invalid code. */
7011 if (TREE_CODE (name) == TYPE_DECL)
7013 error_at (token->location, "invalid use of %qD", name);
7014 postfix_expression = error_mark_node;
7016 else
7018 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7020 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7022 error_at (token->location, "%<%D::%D%> is not a class member",
7023 parser->scope, name);
7024 postfix_expression = error_mark_node;
7026 else
7027 name = build_qualified_name (/*type=*/NULL_TREE,
7028 parser->scope,
7029 name,
7030 template_p);
7031 parser->scope = NULL_TREE;
7032 parser->qualifying_scope = NULL_TREE;
7033 parser->object_scope = NULL_TREE;
7035 if (parser->scope && name && BASELINK_P (name))
7036 adjust_result_of_qualified_name_lookup
7037 (name, parser->scope, scope);
7038 postfix_expression
7039 = finish_class_member_access_expr (postfix_expression, name,
7040 template_p,
7041 tf_warning_or_error);
7045 /* We no longer need to look up names in the scope of the object on
7046 the left-hand side of the `.' or `->' operator. */
7047 parser->context->object_type = NULL_TREE;
7049 /* Outside of offsetof, these operators may not appear in
7050 constant-expressions. */
7051 if (!for_offsetof
7052 && (cp_parser_non_integral_constant_expression
7053 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7054 postfix_expression = error_mark_node;
7056 return postfix_expression;
7059 /* Cache of LITERAL_ZERO_P constants. */
7061 static GTY(()) tree literal_zeros[itk_none];
7063 /* Parse a parenthesized expression-list.
7065 expression-list:
7066 assignment-expression
7067 expression-list, assignment-expression
7069 attribute-list:
7070 expression-list
7071 identifier
7072 identifier, expression-list
7074 CAST_P is true if this expression is the target of a cast.
7076 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7077 argument pack.
7079 Returns a vector of trees. Each element is a representation of an
7080 assignment-expression. NULL is returned if the ( and or ) are
7081 missing. An empty, but allocated, vector is returned on no
7082 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7083 if we are parsing an attribute list for an attribute that wants a
7084 plain identifier argument, normal_attr for an attribute that wants
7085 an expression, or non_attr if we aren't parsing an attribute list. If
7086 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7087 not all of the expressions in the list were constant.
7088 WANT_LITERAL_ZERO_P is true if the caller is interested in
7089 LITERAL_ZERO_P INTEGER_CSTs. FIXME: once we don't fold everything
7090 immediately, this can be removed. */
7092 static vec<tree, va_gc> *
7093 cp_parser_parenthesized_expression_list (cp_parser* parser,
7094 int is_attribute_list,
7095 bool cast_p,
7096 bool allow_expansion_p,
7097 bool *non_constant_p,
7098 bool want_literal_zero_p)
7100 vec<tree, va_gc> *expression_list;
7101 bool fold_expr_p = is_attribute_list != non_attr;
7102 tree identifier = NULL_TREE;
7103 bool saved_greater_than_is_operator_p;
7105 /* Assume all the expressions will be constant. */
7106 if (non_constant_p)
7107 *non_constant_p = false;
7109 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
7110 return NULL;
7112 expression_list = make_tree_vector ();
7114 /* Within a parenthesized expression, a `>' token is always
7115 the greater-than operator. */
7116 saved_greater_than_is_operator_p
7117 = parser->greater_than_is_operator_p;
7118 parser->greater_than_is_operator_p = true;
7120 /* Consume expressions until there are no more. */
7121 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7122 while (true)
7124 tree expr;
7126 /* At the beginning of attribute lists, check to see if the
7127 next token is an identifier. */
7128 if (is_attribute_list == id_attr
7129 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7131 cp_token *token;
7133 /* Consume the identifier. */
7134 token = cp_lexer_consume_token (parser->lexer);
7135 /* Save the identifier. */
7136 identifier = token->u.value;
7138 else
7140 bool expr_non_constant_p;
7142 /* Parse the next assignment-expression. */
7143 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7145 /* A braced-init-list. */
7146 cp_lexer_set_source_position (parser->lexer);
7147 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7148 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7149 if (non_constant_p && expr_non_constant_p)
7150 *non_constant_p = true;
7152 else if (non_constant_p)
7154 expr = (cp_parser_constant_expression
7155 (parser, /*allow_non_constant_p=*/true,
7156 &expr_non_constant_p));
7157 if (expr_non_constant_p)
7158 *non_constant_p = true;
7160 else
7162 expr = NULL_TREE;
7163 cp_token *tok = cp_lexer_peek_token (parser->lexer);
7164 switch (tok->type)
7166 case CPP_NUMBER:
7167 case CPP_CHAR:
7168 case CPP_WCHAR:
7169 case CPP_CHAR16:
7170 case CPP_CHAR32:
7171 case CPP_UTF8CHAR:
7172 /* If a parameter is literal zero alone, remember it
7173 for -Wmemset-transposed-args warning. */
7174 if (integer_zerop (tok->u.value)
7175 && !TREE_OVERFLOW (tok->u.value)
7176 && want_literal_zero_p
7177 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
7178 == CPP_COMMA
7179 || cp_lexer_peek_nth_token (parser->lexer, 2)->type
7180 == CPP_CLOSE_PAREN))
7182 unsigned int i;
7183 for (i = 0; i < itk_none; ++i)
7184 if (TREE_TYPE (tok->u.value) == integer_types[i])
7185 break;
7186 if (i < itk_none && literal_zeros[i])
7187 expr = literal_zeros[i];
7188 else
7190 expr = copy_node (tok->u.value);
7191 LITERAL_ZERO_P (expr) = 1;
7192 if (i < itk_none)
7193 literal_zeros[i] = expr;
7195 /* Consume the 0 token (or '\0', 0LL etc.). */
7196 cp_lexer_consume_token (parser->lexer);
7198 break;
7199 default:
7200 break;
7202 if (expr == NULL_TREE)
7203 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7204 cast_p);
7207 if (fold_expr_p)
7208 expr = instantiate_non_dependent_expr (expr);
7210 /* If we have an ellipsis, then this is an expression
7211 expansion. */
7212 if (allow_expansion_p
7213 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7215 /* Consume the `...'. */
7216 cp_lexer_consume_token (parser->lexer);
7218 /* Build the argument pack. */
7219 expr = make_pack_expansion (expr);
7222 /* Add it to the list. We add error_mark_node
7223 expressions to the list, so that we can still tell if
7224 the correct form for a parenthesized expression-list
7225 is found. That gives better errors. */
7226 vec_safe_push (expression_list, expr);
7228 if (expr == error_mark_node)
7229 goto skip_comma;
7232 /* After the first item, attribute lists look the same as
7233 expression lists. */
7234 is_attribute_list = non_attr;
7236 get_comma:;
7237 /* If the next token isn't a `,', then we are done. */
7238 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7239 break;
7241 /* Otherwise, consume the `,' and keep going. */
7242 cp_lexer_consume_token (parser->lexer);
7245 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7247 int ending;
7249 skip_comma:;
7250 /* We try and resync to an unnested comma, as that will give the
7251 user better diagnostics. */
7252 ending = cp_parser_skip_to_closing_parenthesis (parser,
7253 /*recovering=*/true,
7254 /*or_comma=*/true,
7255 /*consume_paren=*/true);
7256 if (ending < 0)
7257 goto get_comma;
7258 if (!ending)
7260 parser->greater_than_is_operator_p
7261 = saved_greater_than_is_operator_p;
7262 return NULL;
7266 parser->greater_than_is_operator_p
7267 = saved_greater_than_is_operator_p;
7269 if (identifier)
7270 vec_safe_insert (expression_list, 0, identifier);
7272 return expression_list;
7275 /* Parse a pseudo-destructor-name.
7277 pseudo-destructor-name:
7278 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7279 :: [opt] nested-name-specifier template template-id :: ~ type-name
7280 :: [opt] nested-name-specifier [opt] ~ type-name
7282 If either of the first two productions is used, sets *SCOPE to the
7283 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7284 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7285 or ERROR_MARK_NODE if the parse fails. */
7287 static void
7288 cp_parser_pseudo_destructor_name (cp_parser* parser,
7289 tree object,
7290 tree* scope,
7291 tree* type)
7293 bool nested_name_specifier_p;
7295 /* Handle ~auto. */
7296 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7297 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7298 && !type_dependent_expression_p (object))
7300 if (cxx_dialect < cxx14)
7301 pedwarn (input_location, 0,
7302 "%<~auto%> only available with "
7303 "-std=c++14 or -std=gnu++14");
7304 cp_lexer_consume_token (parser->lexer);
7305 cp_lexer_consume_token (parser->lexer);
7306 *scope = NULL_TREE;
7307 *type = TREE_TYPE (object);
7308 return;
7311 /* Assume that things will not work out. */
7312 *type = error_mark_node;
7314 /* Look for the optional `::' operator. */
7315 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7316 /* Look for the optional nested-name-specifier. */
7317 nested_name_specifier_p
7318 = (cp_parser_nested_name_specifier_opt (parser,
7319 /*typename_keyword_p=*/false,
7320 /*check_dependency_p=*/true,
7321 /*type_p=*/false,
7322 /*is_declaration=*/false)
7323 != NULL_TREE);
7324 /* Now, if we saw a nested-name-specifier, we might be doing the
7325 second production. */
7326 if (nested_name_specifier_p
7327 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7329 /* Consume the `template' keyword. */
7330 cp_lexer_consume_token (parser->lexer);
7331 /* Parse the template-id. */
7332 cp_parser_template_id (parser,
7333 /*template_keyword_p=*/true,
7334 /*check_dependency_p=*/false,
7335 class_type,
7336 /*is_declaration=*/true);
7337 /* Look for the `::' token. */
7338 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7340 /* If the next token is not a `~', then there might be some
7341 additional qualification. */
7342 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7344 /* At this point, we're looking for "type-name :: ~". The type-name
7345 must not be a class-name, since this is a pseudo-destructor. So,
7346 it must be either an enum-name, or a typedef-name -- both of which
7347 are just identifiers. So, we peek ahead to check that the "::"
7348 and "~" tokens are present; if they are not, then we can avoid
7349 calling type_name. */
7350 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7351 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7352 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7354 cp_parser_error (parser, "non-scalar type");
7355 return;
7358 /* Look for the type-name. */
7359 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7360 if (*scope == error_mark_node)
7361 return;
7363 /* Look for the `::' token. */
7364 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7366 else
7367 *scope = NULL_TREE;
7369 /* Look for the `~'. */
7370 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7372 /* Once we see the ~, this has to be a pseudo-destructor. */
7373 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7374 cp_parser_commit_to_topmost_tentative_parse (parser);
7376 /* Look for the type-name again. We are not responsible for
7377 checking that it matches the first type-name. */
7378 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7381 /* Parse a unary-expression.
7383 unary-expression:
7384 postfix-expression
7385 ++ cast-expression
7386 -- cast-expression
7387 unary-operator cast-expression
7388 sizeof unary-expression
7389 sizeof ( type-id )
7390 alignof ( type-id ) [C++0x]
7391 new-expression
7392 delete-expression
7394 GNU Extensions:
7396 unary-expression:
7397 __extension__ cast-expression
7398 __alignof__ unary-expression
7399 __alignof__ ( type-id )
7400 alignof unary-expression [C++0x]
7401 __real__ cast-expression
7402 __imag__ cast-expression
7403 && identifier
7404 sizeof ( type-id ) { initializer-list , [opt] }
7405 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7406 __alignof__ ( type-id ) { initializer-list , [opt] }
7408 ADDRESS_P is true iff the unary-expression is appearing as the
7409 operand of the `&' operator. CAST_P is true if this expression is
7410 the target of a cast.
7412 Returns a representation of the expression. */
7414 static tree
7415 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7416 bool address_p, bool cast_p, bool decltype_p)
7418 cp_token *token;
7419 enum tree_code unary_operator;
7421 /* Peek at the next token. */
7422 token = cp_lexer_peek_token (parser->lexer);
7423 /* Some keywords give away the kind of expression. */
7424 if (token->type == CPP_KEYWORD)
7426 enum rid keyword = token->keyword;
7428 switch (keyword)
7430 case RID_ALIGNOF:
7431 case RID_SIZEOF:
7433 tree operand, ret;
7434 enum tree_code op;
7435 location_t first_loc;
7437 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7438 /* Consume the token. */
7439 cp_lexer_consume_token (parser->lexer);
7440 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7441 /* Parse the operand. */
7442 operand = cp_parser_sizeof_operand (parser, keyword);
7444 if (TYPE_P (operand))
7445 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7446 else
7448 /* ISO C++ defines alignof only with types, not with
7449 expressions. So pedwarn if alignof is used with a non-
7450 type expression. However, __alignof__ is ok. */
7451 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7452 pedwarn (token->location, OPT_Wpedantic,
7453 "ISO C++ does not allow %<alignof%> "
7454 "with a non-type");
7456 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7458 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7459 SIZEOF_EXPR with the original operand. */
7460 if (op == SIZEOF_EXPR && ret != error_mark_node)
7462 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7464 if (!processing_template_decl && TYPE_P (operand))
7466 ret = build_min (SIZEOF_EXPR, size_type_node,
7467 build1 (NOP_EXPR, operand,
7468 error_mark_node));
7469 SIZEOF_EXPR_TYPE_P (ret) = 1;
7471 else
7472 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7473 TREE_SIDE_EFFECTS (ret) = 0;
7474 TREE_READONLY (ret) = 1;
7476 SET_EXPR_LOCATION (ret, first_loc);
7478 return ret;
7481 case RID_NEW:
7482 return cp_parser_new_expression (parser);
7484 case RID_DELETE:
7485 return cp_parser_delete_expression (parser);
7487 case RID_EXTENSION:
7489 /* The saved value of the PEDANTIC flag. */
7490 int saved_pedantic;
7491 tree expr;
7493 /* Save away the PEDANTIC flag. */
7494 cp_parser_extension_opt (parser, &saved_pedantic);
7495 /* Parse the cast-expression. */
7496 expr = cp_parser_simple_cast_expression (parser);
7497 /* Restore the PEDANTIC flag. */
7498 pedantic = saved_pedantic;
7500 return expr;
7503 case RID_REALPART:
7504 case RID_IMAGPART:
7506 tree expression;
7508 /* Consume the `__real__' or `__imag__' token. */
7509 cp_lexer_consume_token (parser->lexer);
7510 /* Parse the cast-expression. */
7511 expression = cp_parser_simple_cast_expression (parser);
7512 /* Create the complete representation. */
7513 return build_x_unary_op (token->location,
7514 (keyword == RID_REALPART
7515 ? REALPART_EXPR : IMAGPART_EXPR),
7516 expression,
7517 tf_warning_or_error);
7519 break;
7521 case RID_TRANSACTION_ATOMIC:
7522 case RID_TRANSACTION_RELAXED:
7523 return cp_parser_transaction_expression (parser, keyword);
7525 case RID_NOEXCEPT:
7527 tree expr;
7528 const char *saved_message;
7529 bool saved_integral_constant_expression_p;
7530 bool saved_non_integral_constant_expression_p;
7531 bool saved_greater_than_is_operator_p;
7533 cp_lexer_consume_token (parser->lexer);
7534 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7536 saved_message = parser->type_definition_forbidden_message;
7537 parser->type_definition_forbidden_message
7538 = G_("types may not be defined in %<noexcept%> expressions");
7540 saved_integral_constant_expression_p
7541 = parser->integral_constant_expression_p;
7542 saved_non_integral_constant_expression_p
7543 = parser->non_integral_constant_expression_p;
7544 parser->integral_constant_expression_p = false;
7546 saved_greater_than_is_operator_p
7547 = parser->greater_than_is_operator_p;
7548 parser->greater_than_is_operator_p = true;
7550 ++cp_unevaluated_operand;
7551 ++c_inhibit_evaluation_warnings;
7552 ++cp_noexcept_operand;
7553 expr = cp_parser_expression (parser);
7554 --cp_noexcept_operand;
7555 --c_inhibit_evaluation_warnings;
7556 --cp_unevaluated_operand;
7558 parser->greater_than_is_operator_p
7559 = saved_greater_than_is_operator_p;
7561 parser->integral_constant_expression_p
7562 = saved_integral_constant_expression_p;
7563 parser->non_integral_constant_expression_p
7564 = saved_non_integral_constant_expression_p;
7566 parser->type_definition_forbidden_message = saved_message;
7568 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7569 return finish_noexcept_expr (expr, tf_warning_or_error);
7572 default:
7573 break;
7577 /* Look for the `:: new' and `:: delete', which also signal the
7578 beginning of a new-expression, or delete-expression,
7579 respectively. If the next token is `::', then it might be one of
7580 these. */
7581 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7583 enum rid keyword;
7585 /* See if the token after the `::' is one of the keywords in
7586 which we're interested. */
7587 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7588 /* If it's `new', we have a new-expression. */
7589 if (keyword == RID_NEW)
7590 return cp_parser_new_expression (parser);
7591 /* Similarly, for `delete'. */
7592 else if (keyword == RID_DELETE)
7593 return cp_parser_delete_expression (parser);
7596 /* Look for a unary operator. */
7597 unary_operator = cp_parser_unary_operator (token);
7598 /* The `++' and `--' operators can be handled similarly, even though
7599 they are not technically unary-operators in the grammar. */
7600 if (unary_operator == ERROR_MARK)
7602 if (token->type == CPP_PLUS_PLUS)
7603 unary_operator = PREINCREMENT_EXPR;
7604 else if (token->type == CPP_MINUS_MINUS)
7605 unary_operator = PREDECREMENT_EXPR;
7606 /* Handle the GNU address-of-label extension. */
7607 else if (cp_parser_allow_gnu_extensions_p (parser)
7608 && token->type == CPP_AND_AND)
7610 tree identifier;
7611 tree expression;
7612 location_t loc = token->location;
7614 /* Consume the '&&' token. */
7615 cp_lexer_consume_token (parser->lexer);
7616 /* Look for the identifier. */
7617 identifier = cp_parser_identifier (parser);
7618 /* Create an expression representing the address. */
7619 expression = finish_label_address_expr (identifier, loc);
7620 if (cp_parser_non_integral_constant_expression (parser,
7621 NIC_ADDR_LABEL))
7622 expression = error_mark_node;
7623 return expression;
7626 if (unary_operator != ERROR_MARK)
7628 tree cast_expression;
7629 tree expression = error_mark_node;
7630 non_integral_constant non_constant_p = NIC_NONE;
7631 location_t loc = token->location;
7632 tsubst_flags_t complain = complain_flags (decltype_p);
7634 /* Consume the operator token. */
7635 token = cp_lexer_consume_token (parser->lexer);
7636 /* Parse the cast-expression. */
7637 cast_expression
7638 = cp_parser_cast_expression (parser,
7639 unary_operator == ADDR_EXPR,
7640 /*cast_p=*/false,
7641 /*decltype*/false,
7642 pidk);
7643 /* Now, build an appropriate representation. */
7644 switch (unary_operator)
7646 case INDIRECT_REF:
7647 non_constant_p = NIC_STAR;
7648 expression = build_x_indirect_ref (loc, cast_expression,
7649 RO_UNARY_STAR,
7650 complain);
7651 break;
7653 case ADDR_EXPR:
7654 non_constant_p = NIC_ADDR;
7655 /* Fall through. */
7656 case BIT_NOT_EXPR:
7657 expression = build_x_unary_op (loc, unary_operator,
7658 cast_expression,
7659 complain);
7660 break;
7662 case PREINCREMENT_EXPR:
7663 case PREDECREMENT_EXPR:
7664 non_constant_p = unary_operator == PREINCREMENT_EXPR
7665 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7666 /* Fall through. */
7667 case UNARY_PLUS_EXPR:
7668 case NEGATE_EXPR:
7669 case TRUTH_NOT_EXPR:
7670 expression = finish_unary_op_expr (loc, unary_operator,
7671 cast_expression, complain);
7672 break;
7674 default:
7675 gcc_unreachable ();
7678 if (non_constant_p != NIC_NONE
7679 && cp_parser_non_integral_constant_expression (parser,
7680 non_constant_p))
7681 expression = error_mark_node;
7683 return expression;
7686 return cp_parser_postfix_expression (parser, address_p, cast_p,
7687 /*member_access_only_p=*/false,
7688 decltype_p,
7689 pidk);
7692 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7693 unary-operator, the corresponding tree code is returned. */
7695 static enum tree_code
7696 cp_parser_unary_operator (cp_token* token)
7698 switch (token->type)
7700 case CPP_MULT:
7701 return INDIRECT_REF;
7703 case CPP_AND:
7704 return ADDR_EXPR;
7706 case CPP_PLUS:
7707 return UNARY_PLUS_EXPR;
7709 case CPP_MINUS:
7710 return NEGATE_EXPR;
7712 case CPP_NOT:
7713 return TRUTH_NOT_EXPR;
7715 case CPP_COMPL:
7716 return BIT_NOT_EXPR;
7718 default:
7719 return ERROR_MARK;
7723 /* Parse a new-expression.
7725 new-expression:
7726 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7727 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7729 Returns a representation of the expression. */
7731 static tree
7732 cp_parser_new_expression (cp_parser* parser)
7734 bool global_scope_p;
7735 vec<tree, va_gc> *placement;
7736 tree type;
7737 vec<tree, va_gc> *initializer;
7738 tree nelts = NULL_TREE;
7739 tree ret;
7741 /* Look for the optional `::' operator. */
7742 global_scope_p
7743 = (cp_parser_global_scope_opt (parser,
7744 /*current_scope_valid_p=*/false)
7745 != NULL_TREE);
7746 /* Look for the `new' operator. */
7747 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7748 /* There's no easy way to tell a new-placement from the
7749 `( type-id )' construct. */
7750 cp_parser_parse_tentatively (parser);
7751 /* Look for a new-placement. */
7752 placement = cp_parser_new_placement (parser);
7753 /* If that didn't work out, there's no new-placement. */
7754 if (!cp_parser_parse_definitely (parser))
7756 if (placement != NULL)
7757 release_tree_vector (placement);
7758 placement = NULL;
7761 /* If the next token is a `(', then we have a parenthesized
7762 type-id. */
7763 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7765 cp_token *token;
7766 const char *saved_message = parser->type_definition_forbidden_message;
7768 /* Consume the `('. */
7769 cp_lexer_consume_token (parser->lexer);
7771 /* Parse the type-id. */
7772 parser->type_definition_forbidden_message
7773 = G_("types may not be defined in a new-expression");
7774 type = cp_parser_type_id (parser);
7775 parser->type_definition_forbidden_message = saved_message;
7777 /* Look for the closing `)'. */
7778 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7779 token = cp_lexer_peek_token (parser->lexer);
7780 /* There should not be a direct-new-declarator in this production,
7781 but GCC used to allowed this, so we check and emit a sensible error
7782 message for this case. */
7783 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7785 error_at (token->location,
7786 "array bound forbidden after parenthesized type-id");
7787 inform (token->location,
7788 "try removing the parentheses around the type-id");
7789 cp_parser_direct_new_declarator (parser);
7792 /* Otherwise, there must be a new-type-id. */
7793 else
7794 type = cp_parser_new_type_id (parser, &nelts);
7796 /* If the next token is a `(' or '{', then we have a new-initializer. */
7797 cp_token *token = cp_lexer_peek_token (parser->lexer);
7798 if (token->type == CPP_OPEN_PAREN
7799 || token->type == CPP_OPEN_BRACE)
7800 initializer = cp_parser_new_initializer (parser);
7801 else
7802 initializer = NULL;
7804 /* A new-expression may not appear in an integral constant
7805 expression. */
7806 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7807 ret = error_mark_node;
7808 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
7809 of a new-type-id or type-id of a new-expression, the new-expression shall
7810 contain a new-initializer of the form ( assignment-expression )".
7811 Additionally, consistently with the spirit of DR 1467, we want to accept
7812 'new auto { 2 }' too. */
7813 else if (type_uses_auto (type)
7814 && (vec_safe_length (initializer) != 1
7815 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
7816 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
7818 error_at (token->location,
7819 "initialization of new-expression for type %<auto%> "
7820 "requires exactly one element");
7821 ret = error_mark_node;
7823 else
7825 /* Create a representation of the new-expression. */
7826 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7827 tf_warning_or_error);
7830 if (placement != NULL)
7831 release_tree_vector (placement);
7832 if (initializer != NULL)
7833 release_tree_vector (initializer);
7835 return ret;
7838 /* Parse a new-placement.
7840 new-placement:
7841 ( expression-list )
7843 Returns the same representation as for an expression-list. */
7845 static vec<tree, va_gc> *
7846 cp_parser_new_placement (cp_parser* parser)
7848 vec<tree, va_gc> *expression_list;
7850 /* Parse the expression-list. */
7851 expression_list = (cp_parser_parenthesized_expression_list
7852 (parser, non_attr, /*cast_p=*/false,
7853 /*allow_expansion_p=*/true,
7854 /*non_constant_p=*/NULL));
7856 if (expression_list && expression_list->is_empty ())
7857 error ("expected expression-list or type-id");
7859 return expression_list;
7862 /* Parse a new-type-id.
7864 new-type-id:
7865 type-specifier-seq new-declarator [opt]
7867 Returns the TYPE allocated. If the new-type-id indicates an array
7868 type, *NELTS is set to the number of elements in the last array
7869 bound; the TYPE will not include the last array bound. */
7871 static tree
7872 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7874 cp_decl_specifier_seq type_specifier_seq;
7875 cp_declarator *new_declarator;
7876 cp_declarator *declarator;
7877 cp_declarator *outer_declarator;
7878 const char *saved_message;
7880 /* The type-specifier sequence must not contain type definitions.
7881 (It cannot contain declarations of new types either, but if they
7882 are not definitions we will catch that because they are not
7883 complete.) */
7884 saved_message = parser->type_definition_forbidden_message;
7885 parser->type_definition_forbidden_message
7886 = G_("types may not be defined in a new-type-id");
7887 /* Parse the type-specifier-seq. */
7888 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7889 /*is_trailing_return=*/false,
7890 &type_specifier_seq);
7891 /* Restore the old message. */
7892 parser->type_definition_forbidden_message = saved_message;
7894 if (type_specifier_seq.type == error_mark_node)
7895 return error_mark_node;
7897 /* Parse the new-declarator. */
7898 new_declarator = cp_parser_new_declarator_opt (parser);
7900 /* Determine the number of elements in the last array dimension, if
7901 any. */
7902 *nelts = NULL_TREE;
7903 /* Skip down to the last array dimension. */
7904 declarator = new_declarator;
7905 outer_declarator = NULL;
7906 while (declarator && (declarator->kind == cdk_pointer
7907 || declarator->kind == cdk_ptrmem))
7909 outer_declarator = declarator;
7910 declarator = declarator->declarator;
7912 while (declarator
7913 && declarator->kind == cdk_array
7914 && declarator->declarator
7915 && declarator->declarator->kind == cdk_array)
7917 outer_declarator = declarator;
7918 declarator = declarator->declarator;
7921 if (declarator && declarator->kind == cdk_array)
7923 *nelts = declarator->u.array.bounds;
7924 if (*nelts == error_mark_node)
7925 *nelts = integer_one_node;
7927 if (outer_declarator)
7928 outer_declarator->declarator = declarator->declarator;
7929 else
7930 new_declarator = NULL;
7933 return groktypename (&type_specifier_seq, new_declarator, false);
7936 /* Parse an (optional) new-declarator.
7938 new-declarator:
7939 ptr-operator new-declarator [opt]
7940 direct-new-declarator
7942 Returns the declarator. */
7944 static cp_declarator *
7945 cp_parser_new_declarator_opt (cp_parser* parser)
7947 enum tree_code code;
7948 tree type, std_attributes = NULL_TREE;
7949 cp_cv_quals cv_quals;
7951 /* We don't know if there's a ptr-operator next, or not. */
7952 cp_parser_parse_tentatively (parser);
7953 /* Look for a ptr-operator. */
7954 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7955 /* If that worked, look for more new-declarators. */
7956 if (cp_parser_parse_definitely (parser))
7958 cp_declarator *declarator;
7960 /* Parse another optional declarator. */
7961 declarator = cp_parser_new_declarator_opt (parser);
7963 declarator = cp_parser_make_indirect_declarator
7964 (code, type, cv_quals, declarator, std_attributes);
7966 return declarator;
7969 /* If the next token is a `[', there is a direct-new-declarator. */
7970 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7971 return cp_parser_direct_new_declarator (parser);
7973 return NULL;
7976 /* Parse a direct-new-declarator.
7978 direct-new-declarator:
7979 [ expression ]
7980 direct-new-declarator [constant-expression]
7984 static cp_declarator *
7985 cp_parser_direct_new_declarator (cp_parser* parser)
7987 cp_declarator *declarator = NULL;
7989 while (true)
7991 tree expression;
7992 cp_token *token;
7994 /* Look for the opening `['. */
7995 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7997 token = cp_lexer_peek_token (parser->lexer);
7998 expression = cp_parser_expression (parser);
7999 /* The standard requires that the expression have integral
8000 type. DR 74 adds enumeration types. We believe that the
8001 real intent is that these expressions be handled like the
8002 expression in a `switch' condition, which also allows
8003 classes with a single conversion to integral or
8004 enumeration type. */
8005 if (!processing_template_decl)
8007 expression
8008 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8009 expression,
8010 /*complain=*/true);
8011 if (!expression)
8013 error_at (token->location,
8014 "expression in new-declarator must have integral "
8015 "or enumeration type");
8016 expression = error_mark_node;
8020 /* Look for the closing `]'. */
8021 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8023 /* Add this bound to the declarator. */
8024 declarator = make_array_declarator (declarator, expression);
8026 /* If the next token is not a `[', then there are no more
8027 bounds. */
8028 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8029 break;
8032 return declarator;
8035 /* Parse a new-initializer.
8037 new-initializer:
8038 ( expression-list [opt] )
8039 braced-init-list
8041 Returns a representation of the expression-list. */
8043 static vec<tree, va_gc> *
8044 cp_parser_new_initializer (cp_parser* parser)
8046 vec<tree, va_gc> *expression_list;
8048 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8050 tree t;
8051 bool expr_non_constant_p;
8052 cp_lexer_set_source_position (parser->lexer);
8053 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8054 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8055 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8056 expression_list = make_tree_vector_single (t);
8058 else
8059 expression_list = (cp_parser_parenthesized_expression_list
8060 (parser, non_attr, /*cast_p=*/false,
8061 /*allow_expansion_p=*/true,
8062 /*non_constant_p=*/NULL));
8064 return expression_list;
8067 /* Parse a delete-expression.
8069 delete-expression:
8070 :: [opt] delete cast-expression
8071 :: [opt] delete [ ] cast-expression
8073 Returns a representation of the expression. */
8075 static tree
8076 cp_parser_delete_expression (cp_parser* parser)
8078 bool global_scope_p;
8079 bool array_p;
8080 tree expression;
8082 /* Look for the optional `::' operator. */
8083 global_scope_p
8084 = (cp_parser_global_scope_opt (parser,
8085 /*current_scope_valid_p=*/false)
8086 != NULL_TREE);
8087 /* Look for the `delete' keyword. */
8088 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8089 /* See if the array syntax is in use. */
8090 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8092 /* Consume the `[' token. */
8093 cp_lexer_consume_token (parser->lexer);
8094 /* Look for the `]' token. */
8095 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8096 /* Remember that this is the `[]' construct. */
8097 array_p = true;
8099 else
8100 array_p = false;
8102 /* Parse the cast-expression. */
8103 expression = cp_parser_simple_cast_expression (parser);
8105 /* A delete-expression may not appear in an integral constant
8106 expression. */
8107 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8108 return error_mark_node;
8110 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8111 tf_warning_or_error);
8114 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8115 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8116 0 otherwise. */
8118 static int
8119 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8121 cp_token *token = cp_lexer_peek_token (parser->lexer);
8122 switch (token->type)
8124 case CPP_COMMA:
8125 case CPP_SEMICOLON:
8126 case CPP_QUERY:
8127 case CPP_COLON:
8128 case CPP_CLOSE_SQUARE:
8129 case CPP_CLOSE_PAREN:
8130 case CPP_CLOSE_BRACE:
8131 case CPP_OPEN_BRACE:
8132 case CPP_DOT:
8133 case CPP_DOT_STAR:
8134 case CPP_DEREF:
8135 case CPP_DEREF_STAR:
8136 case CPP_DIV:
8137 case CPP_MOD:
8138 case CPP_LSHIFT:
8139 case CPP_RSHIFT:
8140 case CPP_LESS:
8141 case CPP_GREATER:
8142 case CPP_LESS_EQ:
8143 case CPP_GREATER_EQ:
8144 case CPP_EQ_EQ:
8145 case CPP_NOT_EQ:
8146 case CPP_EQ:
8147 case CPP_MULT_EQ:
8148 case CPP_DIV_EQ:
8149 case CPP_MOD_EQ:
8150 case CPP_PLUS_EQ:
8151 case CPP_MINUS_EQ:
8152 case CPP_RSHIFT_EQ:
8153 case CPP_LSHIFT_EQ:
8154 case CPP_AND_EQ:
8155 case CPP_XOR_EQ:
8156 case CPP_OR_EQ:
8157 case CPP_XOR:
8158 case CPP_OR:
8159 case CPP_OR_OR:
8160 case CPP_EOF:
8161 case CPP_ELLIPSIS:
8162 return 0;
8164 case CPP_OPEN_PAREN:
8165 /* In ((type ()) () the last () isn't a valid cast-expression,
8166 so the whole must be parsed as postfix-expression. */
8167 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8168 != CPP_CLOSE_PAREN;
8170 case CPP_OPEN_SQUARE:
8171 /* '[' may start a primary-expression in obj-c++ and in C++11,
8172 as a lambda-expression, eg, '(void)[]{}'. */
8173 if (cxx_dialect >= cxx11)
8174 return -1;
8175 return c_dialect_objc ();
8177 case CPP_PLUS_PLUS:
8178 case CPP_MINUS_MINUS:
8179 /* '++' and '--' may or may not start a cast-expression:
8181 struct T { void operator++(int); };
8182 void f() { (T())++; }
8186 int a;
8187 (int)++a; */
8188 return -1;
8190 default:
8191 return 1;
8195 /* Parse a cast-expression.
8197 cast-expression:
8198 unary-expression
8199 ( type-id ) cast-expression
8201 ADDRESS_P is true iff the unary-expression is appearing as the
8202 operand of the `&' operator. CAST_P is true if this expression is
8203 the target of a cast.
8205 Returns a representation of the expression. */
8207 static tree
8208 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8209 bool decltype_p, cp_id_kind * pidk)
8211 /* If it's a `(', then we might be looking at a cast. */
8212 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8214 tree type = NULL_TREE;
8215 tree expr = NULL_TREE;
8216 int cast_expression = 0;
8217 const char *saved_message;
8219 /* There's no way to know yet whether or not this is a cast.
8220 For example, `(int (3))' is a unary-expression, while `(int)
8221 3' is a cast. So, we resort to parsing tentatively. */
8222 cp_parser_parse_tentatively (parser);
8223 /* Types may not be defined in a cast. */
8224 saved_message = parser->type_definition_forbidden_message;
8225 parser->type_definition_forbidden_message
8226 = G_("types may not be defined in casts");
8227 /* Consume the `('. */
8228 cp_lexer_consume_token (parser->lexer);
8229 /* A very tricky bit is that `(struct S) { 3 }' is a
8230 compound-literal (which we permit in C++ as an extension).
8231 But, that construct is not a cast-expression -- it is a
8232 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8233 is legal; if the compound-literal were a cast-expression,
8234 you'd need an extra set of parentheses.) But, if we parse
8235 the type-id, and it happens to be a class-specifier, then we
8236 will commit to the parse at that point, because we cannot
8237 undo the action that is done when creating a new class. So,
8238 then we cannot back up and do a postfix-expression.
8240 Another tricky case is the following (c++/29234):
8242 struct S { void operator () (); };
8244 void foo ()
8246 ( S()() );
8249 As a type-id we parse the parenthesized S()() as a function
8250 returning a function, groktypename complains and we cannot
8251 back up in this case either.
8253 Therefore, we scan ahead to the closing `)', and check to see
8254 if the tokens after the `)' can start a cast-expression. Otherwise
8255 we are dealing with an unary-expression, a postfix-expression
8256 or something else.
8258 Yet another tricky case, in C++11, is the following (c++/54891):
8260 (void)[]{};
8262 The issue is that usually, besides the case of lambda-expressions,
8263 the parenthesized type-id cannot be followed by '[', and, eg, we
8264 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8265 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8266 we don't commit, we try a cast-expression, then an unary-expression.
8268 Save tokens so that we can put them back. */
8269 cp_lexer_save_tokens (parser->lexer);
8271 /* We may be looking at a cast-expression. */
8272 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8273 /*consume_paren=*/true))
8274 cast_expression
8275 = cp_parser_tokens_start_cast_expression (parser);
8277 /* Roll back the tokens we skipped. */
8278 cp_lexer_rollback_tokens (parser->lexer);
8279 /* If we aren't looking at a cast-expression, simulate an error so
8280 that the call to cp_parser_error_occurred below returns true. */
8281 if (!cast_expression)
8282 cp_parser_simulate_error (parser);
8283 else
8285 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8286 parser->in_type_id_in_expr_p = true;
8287 /* Look for the type-id. */
8288 type = cp_parser_type_id (parser);
8289 /* Look for the closing `)'. */
8290 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8291 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8294 /* Restore the saved message. */
8295 parser->type_definition_forbidden_message = saved_message;
8297 /* At this point this can only be either a cast or a
8298 parenthesized ctor such as `(T ())' that looks like a cast to
8299 function returning T. */
8300 if (!cp_parser_error_occurred (parser))
8302 /* Only commit if the cast-expression doesn't start with
8303 '++', '--', or '[' in C++11. */
8304 if (cast_expression > 0)
8305 cp_parser_commit_to_topmost_tentative_parse (parser);
8307 expr = cp_parser_cast_expression (parser,
8308 /*address_p=*/false,
8309 /*cast_p=*/true,
8310 /*decltype_p=*/false,
8311 pidk);
8313 if (cp_parser_parse_definitely (parser))
8315 /* Warn about old-style casts, if so requested. */
8316 if (warn_old_style_cast
8317 && !in_system_header_at (input_location)
8318 && !VOID_TYPE_P (type)
8319 && current_lang_name != lang_name_c)
8320 warning (OPT_Wold_style_cast, "use of old-style cast");
8322 /* Only type conversions to integral or enumeration types
8323 can be used in constant-expressions. */
8324 if (!cast_valid_in_integral_constant_expression_p (type)
8325 && cp_parser_non_integral_constant_expression (parser,
8326 NIC_CAST))
8327 return error_mark_node;
8329 /* Perform the cast. */
8330 expr = build_c_cast (input_location, type, expr);
8331 return expr;
8334 else
8335 cp_parser_abort_tentative_parse (parser);
8338 /* If we get here, then it's not a cast, so it must be a
8339 unary-expression. */
8340 return cp_parser_unary_expression (parser, pidk, address_p,
8341 cast_p, decltype_p);
8344 /* Parse a binary expression of the general form:
8346 pm-expression:
8347 cast-expression
8348 pm-expression .* cast-expression
8349 pm-expression ->* cast-expression
8351 multiplicative-expression:
8352 pm-expression
8353 multiplicative-expression * pm-expression
8354 multiplicative-expression / pm-expression
8355 multiplicative-expression % pm-expression
8357 additive-expression:
8358 multiplicative-expression
8359 additive-expression + multiplicative-expression
8360 additive-expression - multiplicative-expression
8362 shift-expression:
8363 additive-expression
8364 shift-expression << additive-expression
8365 shift-expression >> additive-expression
8367 relational-expression:
8368 shift-expression
8369 relational-expression < shift-expression
8370 relational-expression > shift-expression
8371 relational-expression <= shift-expression
8372 relational-expression >= shift-expression
8374 GNU Extension:
8376 relational-expression:
8377 relational-expression <? shift-expression
8378 relational-expression >? shift-expression
8380 equality-expression:
8381 relational-expression
8382 equality-expression == relational-expression
8383 equality-expression != relational-expression
8385 and-expression:
8386 equality-expression
8387 and-expression & equality-expression
8389 exclusive-or-expression:
8390 and-expression
8391 exclusive-or-expression ^ and-expression
8393 inclusive-or-expression:
8394 exclusive-or-expression
8395 inclusive-or-expression | exclusive-or-expression
8397 logical-and-expression:
8398 inclusive-or-expression
8399 logical-and-expression && inclusive-or-expression
8401 logical-or-expression:
8402 logical-and-expression
8403 logical-or-expression || logical-and-expression
8405 All these are implemented with a single function like:
8407 binary-expression:
8408 simple-cast-expression
8409 binary-expression <token> binary-expression
8411 CAST_P is true if this expression is the target of a cast.
8413 The binops_by_token map is used to get the tree codes for each <token> type.
8414 binary-expressions are associated according to a precedence table. */
8416 #define TOKEN_PRECEDENCE(token) \
8417 (((token->type == CPP_GREATER \
8418 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8419 && !parser->greater_than_is_operator_p) \
8420 ? PREC_NOT_OPERATOR \
8421 : binops_by_token[token->type].prec)
8423 static tree
8424 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8425 bool no_toplevel_fold_p,
8426 bool decltype_p,
8427 enum cp_parser_prec prec,
8428 cp_id_kind * pidk)
8430 cp_parser_expression_stack stack;
8431 cp_parser_expression_stack_entry *sp = &stack[0];
8432 cp_parser_expression_stack_entry current;
8433 tree rhs;
8434 cp_token *token;
8435 enum tree_code rhs_type;
8436 enum cp_parser_prec new_prec, lookahead_prec;
8437 tree overload;
8439 /* Parse the first expression. */
8440 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8441 ? TRUTH_NOT_EXPR : ERROR_MARK);
8442 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8443 cast_p, decltype_p, pidk);
8444 current.prec = prec;
8446 if (cp_parser_error_occurred (parser))
8447 return error_mark_node;
8449 for (;;)
8451 /* Get an operator token. */
8452 token = cp_lexer_peek_token (parser->lexer);
8454 if (warn_cxx11_compat
8455 && token->type == CPP_RSHIFT
8456 && !parser->greater_than_is_operator_p)
8458 if (warning_at (token->location, OPT_Wc__11_compat,
8459 "%<>>%> operator is treated"
8460 " as two right angle brackets in C++11"))
8461 inform (token->location,
8462 "suggest parentheses around %<>>%> expression");
8465 new_prec = TOKEN_PRECEDENCE (token);
8467 /* Popping an entry off the stack means we completed a subexpression:
8468 - either we found a token which is not an operator (`>' where it is not
8469 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8470 will happen repeatedly;
8471 - or, we found an operator which has lower priority. This is the case
8472 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8473 parsing `3 * 4'. */
8474 if (new_prec <= current.prec)
8476 if (sp == stack)
8477 break;
8478 else
8479 goto pop;
8482 get_rhs:
8483 current.tree_type = binops_by_token[token->type].tree_type;
8484 current.loc = token->location;
8486 /* We used the operator token. */
8487 cp_lexer_consume_token (parser->lexer);
8489 /* For "false && x" or "true || x", x will never be executed;
8490 disable warnings while evaluating it. */
8491 if (current.tree_type == TRUTH_ANDIF_EXPR)
8492 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
8493 else if (current.tree_type == TRUTH_ORIF_EXPR)
8494 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
8496 /* Extract another operand. It may be the RHS of this expression
8497 or the LHS of a new, higher priority expression. */
8498 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8499 ? TRUTH_NOT_EXPR : ERROR_MARK);
8500 rhs = cp_parser_simple_cast_expression (parser);
8502 /* Get another operator token. Look up its precedence to avoid
8503 building a useless (immediately popped) stack entry for common
8504 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8505 token = cp_lexer_peek_token (parser->lexer);
8506 lookahead_prec = TOKEN_PRECEDENCE (token);
8507 if (lookahead_prec > new_prec)
8509 /* ... and prepare to parse the RHS of the new, higher priority
8510 expression. Since precedence levels on the stack are
8511 monotonically increasing, we do not have to care about
8512 stack overflows. */
8513 *sp = current;
8514 ++sp;
8515 current.lhs = rhs;
8516 current.lhs_type = rhs_type;
8517 current.prec = new_prec;
8518 new_prec = lookahead_prec;
8519 goto get_rhs;
8521 pop:
8522 lookahead_prec = new_prec;
8523 /* If the stack is not empty, we have parsed into LHS the right side
8524 (`4' in the example above) of an expression we had suspended.
8525 We can use the information on the stack to recover the LHS (`3')
8526 from the stack together with the tree code (`MULT_EXPR'), and
8527 the precedence of the higher level subexpression
8528 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8529 which will be used to actually build the additive expression. */
8530 rhs = current.lhs;
8531 rhs_type = current.lhs_type;
8532 --sp;
8533 current = *sp;
8536 /* Undo the disabling of warnings done above. */
8537 if (current.tree_type == TRUTH_ANDIF_EXPR)
8538 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
8539 else if (current.tree_type == TRUTH_ORIF_EXPR)
8540 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
8542 if (warn_logical_not_paren
8543 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
8544 && current.lhs_type == TRUTH_NOT_EXPR
8545 /* Avoid warning for !!x == y. */
8546 && (TREE_CODE (current.lhs) != NE_EXPR
8547 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
8548 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
8549 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
8550 /* Avoid warning for !b == y where b is boolean. */
8551 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
8552 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
8553 != BOOLEAN_TYPE))))
8554 /* Avoid warning for !!b == y where b is boolean. */
8555 && (!DECL_P (current.lhs)
8556 || TREE_TYPE (current.lhs) == NULL_TREE
8557 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
8558 warn_logical_not_parentheses (current.loc, current.tree_type,
8559 maybe_constant_value (rhs));
8561 overload = NULL;
8562 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8563 ERROR_MARK for everything that is not a binary expression.
8564 This makes warn_about_parentheses miss some warnings that
8565 involve unary operators. For unary expressions we should
8566 pass the correct tree_code unless the unary expression was
8567 surrounded by parentheses.
8569 if (no_toplevel_fold_p
8570 && lookahead_prec <= current.prec
8571 && sp == stack)
8572 current.lhs = build2 (current.tree_type,
8573 TREE_CODE_CLASS (current.tree_type)
8574 == tcc_comparison
8575 ? boolean_type_node : TREE_TYPE (current.lhs),
8576 current.lhs, rhs);
8577 else
8578 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8579 current.lhs, current.lhs_type,
8580 rhs, rhs_type, &overload,
8581 complain_flags (decltype_p));
8582 current.lhs_type = current.tree_type;
8583 if (EXPR_P (current.lhs))
8584 SET_EXPR_LOCATION (current.lhs, current.loc);
8586 /* If the binary operator required the use of an overloaded operator,
8587 then this expression cannot be an integral constant-expression.
8588 An overloaded operator can be used even if both operands are
8589 otherwise permissible in an integral constant-expression if at
8590 least one of the operands is of enumeration type. */
8592 if (overload
8593 && cp_parser_non_integral_constant_expression (parser,
8594 NIC_OVERLOADED))
8595 return error_mark_node;
8598 return current.lhs;
8601 static tree
8602 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8603 bool no_toplevel_fold_p,
8604 enum cp_parser_prec prec,
8605 cp_id_kind * pidk)
8607 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8608 /*decltype*/false, prec, pidk);
8611 /* Parse the `? expression : assignment-expression' part of a
8612 conditional-expression. The LOGICAL_OR_EXPR is the
8613 logical-or-expression that started the conditional-expression.
8614 Returns a representation of the entire conditional-expression.
8616 This routine is used by cp_parser_assignment_expression.
8618 ? expression : assignment-expression
8620 GNU Extensions:
8622 ? : assignment-expression */
8624 static tree
8625 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8627 tree expr;
8628 tree assignment_expr;
8629 struct cp_token *token;
8630 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8632 /* Consume the `?' token. */
8633 cp_lexer_consume_token (parser->lexer);
8634 token = cp_lexer_peek_token (parser->lexer);
8635 if (cp_parser_allow_gnu_extensions_p (parser)
8636 && token->type == CPP_COLON)
8638 pedwarn (token->location, OPT_Wpedantic,
8639 "ISO C++ does not allow ?: with omitted middle operand");
8640 /* Implicit true clause. */
8641 expr = NULL_TREE;
8642 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8643 warn_for_omitted_condop (token->location, logical_or_expr);
8645 else
8647 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8648 parser->colon_corrects_to_scope_p = false;
8649 /* Parse the expression. */
8650 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8651 expr = cp_parser_expression (parser);
8652 c_inhibit_evaluation_warnings +=
8653 ((logical_or_expr == truthvalue_true_node)
8654 - (logical_or_expr == truthvalue_false_node));
8655 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8658 /* The next token should be a `:'. */
8659 cp_parser_require (parser, CPP_COLON, RT_COLON);
8660 /* Parse the assignment-expression. */
8661 assignment_expr = cp_parser_assignment_expression (parser);
8662 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8664 /* Build the conditional-expression. */
8665 return build_x_conditional_expr (loc, logical_or_expr,
8666 expr,
8667 assignment_expr,
8668 tf_warning_or_error);
8671 /* Parse an assignment-expression.
8673 assignment-expression:
8674 conditional-expression
8675 logical-or-expression assignment-operator assignment_expression
8676 throw-expression
8678 CAST_P is true if this expression is the target of a cast.
8679 DECLTYPE_P is true if this expression is the operand of decltype.
8681 Returns a representation for the expression. */
8683 static tree
8684 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
8685 bool cast_p, bool decltype_p)
8687 tree expr;
8689 /* If the next token is the `throw' keyword, then we're looking at
8690 a throw-expression. */
8691 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8692 expr = cp_parser_throw_expression (parser);
8693 /* Otherwise, it must be that we are looking at a
8694 logical-or-expression. */
8695 else
8697 /* Parse the binary expressions (logical-or-expression). */
8698 expr = cp_parser_binary_expression (parser, cast_p, false,
8699 decltype_p,
8700 PREC_NOT_OPERATOR, pidk);
8701 /* If the next token is a `?' then we're actually looking at a
8702 conditional-expression. */
8703 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8704 return cp_parser_question_colon_clause (parser, expr);
8705 else
8707 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8709 /* If it's an assignment-operator, we're using the second
8710 production. */
8711 enum tree_code assignment_operator
8712 = cp_parser_assignment_operator_opt (parser);
8713 if (assignment_operator != ERROR_MARK)
8715 bool non_constant_p;
8716 location_t saved_input_location;
8718 /* Parse the right-hand side of the assignment. */
8719 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8721 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8722 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8724 /* An assignment may not appear in a
8725 constant-expression. */
8726 if (cp_parser_non_integral_constant_expression (parser,
8727 NIC_ASSIGNMENT))
8728 return error_mark_node;
8729 /* Build the assignment expression. Its default
8730 location is the location of the '=' token. */
8731 saved_input_location = input_location;
8732 input_location = loc;
8733 expr = build_x_modify_expr (loc, expr,
8734 assignment_operator,
8735 rhs,
8736 complain_flags (decltype_p));
8737 input_location = saved_input_location;
8742 return expr;
8745 /* Parse an (optional) assignment-operator.
8747 assignment-operator: one of
8748 = *= /= %= += -= >>= <<= &= ^= |=
8750 GNU Extension:
8752 assignment-operator: one of
8753 <?= >?=
8755 If the next token is an assignment operator, the corresponding tree
8756 code is returned, and the token is consumed. For example, for
8757 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8758 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8759 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8760 operator, ERROR_MARK is returned. */
8762 static enum tree_code
8763 cp_parser_assignment_operator_opt (cp_parser* parser)
8765 enum tree_code op;
8766 cp_token *token;
8768 /* Peek at the next token. */
8769 token = cp_lexer_peek_token (parser->lexer);
8771 switch (token->type)
8773 case CPP_EQ:
8774 op = NOP_EXPR;
8775 break;
8777 case CPP_MULT_EQ:
8778 op = MULT_EXPR;
8779 break;
8781 case CPP_DIV_EQ:
8782 op = TRUNC_DIV_EXPR;
8783 break;
8785 case CPP_MOD_EQ:
8786 op = TRUNC_MOD_EXPR;
8787 break;
8789 case CPP_PLUS_EQ:
8790 op = PLUS_EXPR;
8791 break;
8793 case CPP_MINUS_EQ:
8794 op = MINUS_EXPR;
8795 break;
8797 case CPP_RSHIFT_EQ:
8798 op = RSHIFT_EXPR;
8799 break;
8801 case CPP_LSHIFT_EQ:
8802 op = LSHIFT_EXPR;
8803 break;
8805 case CPP_AND_EQ:
8806 op = BIT_AND_EXPR;
8807 break;
8809 case CPP_XOR_EQ:
8810 op = BIT_XOR_EXPR;
8811 break;
8813 case CPP_OR_EQ:
8814 op = BIT_IOR_EXPR;
8815 break;
8817 default:
8818 /* Nothing else is an assignment operator. */
8819 op = ERROR_MARK;
8822 /* If it was an assignment operator, consume it. */
8823 if (op != ERROR_MARK)
8824 cp_lexer_consume_token (parser->lexer);
8826 return op;
8829 /* Parse an expression.
8831 expression:
8832 assignment-expression
8833 expression , assignment-expression
8835 CAST_P is true if this expression is the target of a cast.
8836 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8837 except possibly parenthesized or on the RHS of a comma (N3276).
8839 Returns a representation of the expression. */
8841 static tree
8842 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
8843 bool cast_p, bool decltype_p)
8845 tree expression = NULL_TREE;
8846 location_t loc = UNKNOWN_LOCATION;
8848 while (true)
8850 tree assignment_expression;
8852 /* Parse the next assignment-expression. */
8853 assignment_expression
8854 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
8856 /* We don't create a temporary for a call that is the immediate operand
8857 of decltype or on the RHS of a comma. But when we see a comma, we
8858 need to create a temporary for a call on the LHS. */
8859 if (decltype_p && !processing_template_decl
8860 && TREE_CODE (assignment_expression) == CALL_EXPR
8861 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8862 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8863 assignment_expression
8864 = build_cplus_new (TREE_TYPE (assignment_expression),
8865 assignment_expression, tf_warning_or_error);
8867 /* If this is the first assignment-expression, we can just
8868 save it away. */
8869 if (!expression)
8870 expression = assignment_expression;
8871 else
8872 expression = build_x_compound_expr (loc, expression,
8873 assignment_expression,
8874 complain_flags (decltype_p));
8875 /* If the next token is not a comma, then we are done with the
8876 expression. */
8877 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8878 break;
8879 /* Consume the `,'. */
8880 loc = cp_lexer_peek_token (parser->lexer)->location;
8881 cp_lexer_consume_token (parser->lexer);
8882 /* A comma operator cannot appear in a constant-expression. */
8883 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8884 expression = error_mark_node;
8887 return expression;
8890 /* Parse a constant-expression.
8892 constant-expression:
8893 conditional-expression
8895 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8896 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8897 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8898 is false, NON_CONSTANT_P should be NULL. */
8900 static tree
8901 cp_parser_constant_expression (cp_parser* parser,
8902 bool allow_non_constant_p,
8903 bool *non_constant_p)
8905 bool saved_integral_constant_expression_p;
8906 bool saved_allow_non_integral_constant_expression_p;
8907 bool saved_non_integral_constant_expression_p;
8908 tree expression;
8910 /* It might seem that we could simply parse the
8911 conditional-expression, and then check to see if it were
8912 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8913 one that the compiler can figure out is constant, possibly after
8914 doing some simplifications or optimizations. The standard has a
8915 precise definition of constant-expression, and we must honor
8916 that, even though it is somewhat more restrictive.
8918 For example:
8920 int i[(2, 3)];
8922 is not a legal declaration, because `(2, 3)' is not a
8923 constant-expression. The `,' operator is forbidden in a
8924 constant-expression. However, GCC's constant-folding machinery
8925 will fold this operation to an INTEGER_CST for `3'. */
8927 /* Save the old settings. */
8928 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8929 saved_allow_non_integral_constant_expression_p
8930 = parser->allow_non_integral_constant_expression_p;
8931 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8932 /* We are now parsing a constant-expression. */
8933 parser->integral_constant_expression_p = true;
8934 parser->allow_non_integral_constant_expression_p
8935 = (allow_non_constant_p || cxx_dialect >= cxx11);
8936 parser->non_integral_constant_expression_p = false;
8937 /* Although the grammar says "conditional-expression", we parse an
8938 "assignment-expression", which also permits "throw-expression"
8939 and the use of assignment operators. In the case that
8940 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8941 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8942 actually essential that we look for an assignment-expression.
8943 For example, cp_parser_initializer_clauses uses this function to
8944 determine whether a particular assignment-expression is in fact
8945 constant. */
8946 expression = cp_parser_assignment_expression (parser);
8947 /* Restore the old settings. */
8948 parser->integral_constant_expression_p
8949 = saved_integral_constant_expression_p;
8950 parser->allow_non_integral_constant_expression_p
8951 = saved_allow_non_integral_constant_expression_p;
8952 if (cxx_dialect >= cxx11)
8954 /* Require an rvalue constant expression here; that's what our
8955 callers expect. Reference constant expressions are handled
8956 separately in e.g. cp_parser_template_argument. */
8957 bool is_const = potential_rvalue_constant_expression (expression);
8958 parser->non_integral_constant_expression_p = !is_const;
8959 if (!is_const && !allow_non_constant_p)
8960 require_potential_rvalue_constant_expression (expression);
8962 if (allow_non_constant_p)
8963 *non_constant_p = parser->non_integral_constant_expression_p;
8964 parser->non_integral_constant_expression_p
8965 = saved_non_integral_constant_expression_p;
8967 return expression;
8970 /* Parse __builtin_offsetof.
8972 offsetof-expression:
8973 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8975 offsetof-member-designator:
8976 id-expression
8977 | offsetof-member-designator "." id-expression
8978 | offsetof-member-designator "[" expression "]"
8979 | offsetof-member-designator "->" id-expression */
8981 static tree
8982 cp_parser_builtin_offsetof (cp_parser *parser)
8984 int save_ice_p, save_non_ice_p;
8985 tree type, expr;
8986 cp_id_kind dummy;
8987 cp_token *token;
8989 /* We're about to accept non-integral-constant things, but will
8990 definitely yield an integral constant expression. Save and
8991 restore these values around our local parsing. */
8992 save_ice_p = parser->integral_constant_expression_p;
8993 save_non_ice_p = parser->non_integral_constant_expression_p;
8995 /* Consume the "__builtin_offsetof" token. */
8996 cp_lexer_consume_token (parser->lexer);
8997 /* Consume the opening `('. */
8998 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8999 /* Parse the type-id. */
9000 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9001 type = cp_parser_type_id (parser);
9002 /* Look for the `,'. */
9003 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9004 token = cp_lexer_peek_token (parser->lexer);
9006 /* Build the (type *)null that begins the traditional offsetof macro. */
9007 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
9008 tf_warning_or_error);
9010 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9011 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
9012 true, &dummy, token->location);
9013 while (true)
9015 token = cp_lexer_peek_token (parser->lexer);
9016 switch (token->type)
9018 case CPP_OPEN_SQUARE:
9019 /* offsetof-member-designator "[" expression "]" */
9020 expr = cp_parser_postfix_open_square_expression (parser, expr,
9021 true, false);
9022 break;
9024 case CPP_DEREF:
9025 /* offsetof-member-designator "->" identifier */
9026 expr = grok_array_decl (token->location, expr,
9027 integer_zero_node, false);
9028 /* FALLTHRU */
9030 case CPP_DOT:
9031 /* offsetof-member-designator "." identifier */
9032 cp_lexer_consume_token (parser->lexer);
9033 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9034 expr, true, &dummy,
9035 token->location);
9036 break;
9038 case CPP_CLOSE_PAREN:
9039 /* Consume the ")" token. */
9040 cp_lexer_consume_token (parser->lexer);
9041 goto success;
9043 default:
9044 /* Error. We know the following require will fail, but
9045 that gives the proper error message. */
9046 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9047 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9048 expr = error_mark_node;
9049 goto failure;
9053 success:
9054 expr = finish_offsetof (expr, loc);
9056 failure:
9057 parser->integral_constant_expression_p = save_ice_p;
9058 parser->non_integral_constant_expression_p = save_non_ice_p;
9060 return expr;
9063 /* Parse a trait expression.
9065 Returns a representation of the expression, the underlying type
9066 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9068 static tree
9069 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9071 cp_trait_kind kind;
9072 tree type1, type2 = NULL_TREE;
9073 bool binary = false;
9074 bool variadic = false;
9076 switch (keyword)
9078 case RID_HAS_NOTHROW_ASSIGN:
9079 kind = CPTK_HAS_NOTHROW_ASSIGN;
9080 break;
9081 case RID_HAS_NOTHROW_CONSTRUCTOR:
9082 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9083 break;
9084 case RID_HAS_NOTHROW_COPY:
9085 kind = CPTK_HAS_NOTHROW_COPY;
9086 break;
9087 case RID_HAS_TRIVIAL_ASSIGN:
9088 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9089 break;
9090 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9091 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9092 break;
9093 case RID_HAS_TRIVIAL_COPY:
9094 kind = CPTK_HAS_TRIVIAL_COPY;
9095 break;
9096 case RID_HAS_TRIVIAL_DESTRUCTOR:
9097 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9098 break;
9099 case RID_HAS_VIRTUAL_DESTRUCTOR:
9100 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9101 break;
9102 case RID_IS_ABSTRACT:
9103 kind = CPTK_IS_ABSTRACT;
9104 break;
9105 case RID_IS_BASE_OF:
9106 kind = CPTK_IS_BASE_OF;
9107 binary = true;
9108 break;
9109 case RID_IS_CLASS:
9110 kind = CPTK_IS_CLASS;
9111 break;
9112 case RID_IS_EMPTY:
9113 kind = CPTK_IS_EMPTY;
9114 break;
9115 case RID_IS_ENUM:
9116 kind = CPTK_IS_ENUM;
9117 break;
9118 case RID_IS_FINAL:
9119 kind = CPTK_IS_FINAL;
9120 break;
9121 case RID_IS_LITERAL_TYPE:
9122 kind = CPTK_IS_LITERAL_TYPE;
9123 break;
9124 case RID_IS_POD:
9125 kind = CPTK_IS_POD;
9126 break;
9127 case RID_IS_POLYMORPHIC:
9128 kind = CPTK_IS_POLYMORPHIC;
9129 break;
9130 case RID_IS_SAME_AS:
9131 kind = CPTK_IS_SAME_AS;
9132 binary = true;
9133 break;
9134 case RID_IS_STD_LAYOUT:
9135 kind = CPTK_IS_STD_LAYOUT;
9136 break;
9137 case RID_IS_TRIVIAL:
9138 kind = CPTK_IS_TRIVIAL;
9139 break;
9140 case RID_IS_TRIVIALLY_ASSIGNABLE:
9141 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9142 binary = true;
9143 break;
9144 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9145 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9146 variadic = true;
9147 break;
9148 case RID_IS_TRIVIALLY_COPYABLE:
9149 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9150 break;
9151 case RID_IS_UNION:
9152 kind = CPTK_IS_UNION;
9153 break;
9154 case RID_UNDERLYING_TYPE:
9155 kind = CPTK_UNDERLYING_TYPE;
9156 break;
9157 case RID_BASES:
9158 kind = CPTK_BASES;
9159 break;
9160 case RID_DIRECT_BASES:
9161 kind = CPTK_DIRECT_BASES;
9162 break;
9163 default:
9164 gcc_unreachable ();
9167 /* Consume the token. */
9168 cp_lexer_consume_token (parser->lexer);
9170 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9172 type1 = cp_parser_type_id (parser);
9174 if (type1 == error_mark_node)
9175 return error_mark_node;
9177 if (binary)
9179 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9181 type2 = cp_parser_type_id (parser);
9183 if (type2 == error_mark_node)
9184 return error_mark_node;
9186 else if (variadic)
9188 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9190 cp_lexer_consume_token (parser->lexer);
9191 tree elt = cp_parser_type_id (parser);
9192 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9194 cp_lexer_consume_token (parser->lexer);
9195 elt = make_pack_expansion (elt);
9197 if (elt == error_mark_node)
9198 return error_mark_node;
9199 type2 = tree_cons (NULL_TREE, elt, type2);
9203 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9205 /* Complete the trait expression, which may mean either processing
9206 the trait expr now or saving it for template instantiation. */
9207 switch(kind)
9209 case CPTK_UNDERLYING_TYPE:
9210 return finish_underlying_type (type1);
9211 case CPTK_BASES:
9212 return finish_bases (type1, false);
9213 case CPTK_DIRECT_BASES:
9214 return finish_bases (type1, true);
9215 default:
9216 return finish_trait_expr (kind, type1, type2);
9220 /* Lambdas that appear in variable initializer or default argument scope
9221 get that in their mangling, so we need to record it. We might as well
9222 use the count for function and namespace scopes as well. */
9223 static GTY(()) tree lambda_scope;
9224 static GTY(()) int lambda_count;
9225 struct GTY(()) tree_int
9227 tree t;
9228 int i;
9230 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
9232 static void
9233 start_lambda_scope (tree decl)
9235 tree_int ti;
9236 gcc_assert (decl);
9237 /* Once we're inside a function, we ignore other scopes and just push
9238 the function again so that popping works properly. */
9239 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
9240 decl = current_function_decl;
9241 ti.t = lambda_scope;
9242 ti.i = lambda_count;
9243 vec_safe_push (lambda_scope_stack, ti);
9244 if (lambda_scope != decl)
9246 /* Don't reset the count if we're still in the same function. */
9247 lambda_scope = decl;
9248 lambda_count = 0;
9252 static void
9253 record_lambda_scope (tree lambda)
9255 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
9256 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
9259 static void
9260 finish_lambda_scope (void)
9262 tree_int *p = &lambda_scope_stack->last ();
9263 if (lambda_scope != p->t)
9265 lambda_scope = p->t;
9266 lambda_count = p->i;
9268 lambda_scope_stack->pop ();
9271 /* Parse a lambda expression.
9273 lambda-expression:
9274 lambda-introducer lambda-declarator [opt] compound-statement
9276 Returns a representation of the expression. */
9278 static tree
9279 cp_parser_lambda_expression (cp_parser* parser)
9281 tree lambda_expr = build_lambda_expr ();
9282 tree type;
9283 bool ok = true;
9284 cp_token *token = cp_lexer_peek_token (parser->lexer);
9285 cp_token_position start = 0;
9287 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9289 if (cp_unevaluated_operand)
9291 if (!token->error_reported)
9293 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9294 "lambda-expression in unevaluated context");
9295 token->error_reported = true;
9297 ok = false;
9299 else if (parser->in_template_argument_list_p)
9301 if (!token->error_reported)
9303 error_at (token->location, "lambda-expression in template-argument");
9304 token->error_reported = true;
9306 ok = false;
9309 /* We may be in the middle of deferred access check. Disable
9310 it now. */
9311 push_deferring_access_checks (dk_no_deferred);
9313 cp_parser_lambda_introducer (parser, lambda_expr);
9315 type = begin_lambda_type (lambda_expr);
9316 if (type == error_mark_node)
9317 return error_mark_node;
9319 record_lambda_scope (lambda_expr);
9321 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9322 determine_visibility (TYPE_NAME (type));
9324 /* Now that we've started the type, add the capture fields for any
9325 explicit captures. */
9326 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9329 /* Inside the class, surrounding template-parameter-lists do not apply. */
9330 unsigned int saved_num_template_parameter_lists
9331 = parser->num_template_parameter_lists;
9332 unsigned char in_statement = parser->in_statement;
9333 bool in_switch_statement_p = parser->in_switch_statement_p;
9334 bool fully_implicit_function_template_p
9335 = parser->fully_implicit_function_template_p;
9336 tree implicit_template_parms = parser->implicit_template_parms;
9337 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9338 bool auto_is_implicit_function_template_parm_p
9339 = parser->auto_is_implicit_function_template_parm_p;
9341 parser->num_template_parameter_lists = 0;
9342 parser->in_statement = 0;
9343 parser->in_switch_statement_p = false;
9344 parser->fully_implicit_function_template_p = false;
9345 parser->implicit_template_parms = 0;
9346 parser->implicit_template_scope = 0;
9347 parser->auto_is_implicit_function_template_parm_p = false;
9349 /* By virtue of defining a local class, a lambda expression has access to
9350 the private variables of enclosing classes. */
9352 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9354 if (ok)
9356 if (!cp_parser_error_occurred (parser)
9357 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9358 && cp_parser_start_tentative_firewall (parser))
9359 start = token;
9360 cp_parser_lambda_body (parser, lambda_expr);
9362 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9364 if (cp_parser_skip_to_closing_brace (parser))
9365 cp_lexer_consume_token (parser->lexer);
9368 /* The capture list was built up in reverse order; fix that now. */
9369 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9370 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9372 if (ok)
9373 maybe_add_lambda_conv_op (type);
9375 type = finish_struct (type, /*attributes=*/NULL_TREE);
9377 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9378 parser->in_statement = in_statement;
9379 parser->in_switch_statement_p = in_switch_statement_p;
9380 parser->fully_implicit_function_template_p
9381 = fully_implicit_function_template_p;
9382 parser->implicit_template_parms = implicit_template_parms;
9383 parser->implicit_template_scope = implicit_template_scope;
9384 parser->auto_is_implicit_function_template_parm_p
9385 = auto_is_implicit_function_template_parm_p;
9388 pop_deferring_access_checks ();
9390 /* This field is only used during parsing of the lambda. */
9391 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9393 /* This lambda shouldn't have any proxies left at this point. */
9394 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9395 /* And now that we're done, push proxies for an enclosing lambda. */
9396 insert_pending_capture_proxies ();
9398 if (ok)
9399 lambda_expr = build_lambda_object (lambda_expr);
9400 else
9401 lambda_expr = error_mark_node;
9403 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9405 return lambda_expr;
9408 /* Parse the beginning of a lambda expression.
9410 lambda-introducer:
9411 [ lambda-capture [opt] ]
9413 LAMBDA_EXPR is the current representation of the lambda expression. */
9415 static void
9416 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9418 /* Need commas after the first capture. */
9419 bool first = true;
9421 /* Eat the leading `['. */
9422 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9424 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9425 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9426 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9427 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9428 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9429 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9431 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9433 cp_lexer_consume_token (parser->lexer);
9434 first = false;
9437 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9439 cp_token* capture_token;
9440 tree capture_id;
9441 tree capture_init_expr;
9442 cp_id_kind idk = CP_ID_KIND_NONE;
9443 bool explicit_init_p = false;
9445 enum capture_kind_type
9447 BY_COPY,
9448 BY_REFERENCE
9450 enum capture_kind_type capture_kind = BY_COPY;
9452 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9454 error ("expected end of capture-list");
9455 return;
9458 if (first)
9459 first = false;
9460 else
9461 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9463 /* Possibly capture `this'. */
9464 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9466 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9467 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9468 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9469 "with by-copy capture default");
9470 cp_lexer_consume_token (parser->lexer);
9471 add_capture (lambda_expr,
9472 /*id=*/this_identifier,
9473 /*initializer=*/finish_this_expr(),
9474 /*by_reference_p=*/false,
9475 explicit_init_p);
9476 continue;
9479 /* Remember whether we want to capture as a reference or not. */
9480 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9482 capture_kind = BY_REFERENCE;
9483 cp_lexer_consume_token (parser->lexer);
9486 /* Get the identifier. */
9487 capture_token = cp_lexer_peek_token (parser->lexer);
9488 capture_id = cp_parser_identifier (parser);
9490 if (capture_id == error_mark_node)
9491 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9492 delimiters, but I modified this to stop on unnested ']' as well. It
9493 was already changed to stop on unnested '}', so the
9494 "closing_parenthesis" name is no more misleading with my change. */
9496 cp_parser_skip_to_closing_parenthesis (parser,
9497 /*recovering=*/true,
9498 /*or_comma=*/true,
9499 /*consume_paren=*/true);
9500 break;
9503 /* Find the initializer for this capture. */
9504 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9505 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9506 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9508 bool direct, non_constant;
9509 /* An explicit initializer exists. */
9510 if (cxx_dialect < cxx14)
9511 pedwarn (input_location, 0,
9512 "lambda capture initializers "
9513 "only available with -std=c++14 or -std=gnu++14");
9514 capture_init_expr = cp_parser_initializer (parser, &direct,
9515 &non_constant);
9516 explicit_init_p = true;
9517 if (capture_init_expr == NULL_TREE)
9519 error ("empty initializer for lambda init-capture");
9520 capture_init_expr = error_mark_node;
9523 else
9525 const char* error_msg;
9527 /* Turn the identifier into an id-expression. */
9528 capture_init_expr
9529 = cp_parser_lookup_name_simple (parser, capture_id,
9530 capture_token->location);
9532 if (capture_init_expr == error_mark_node)
9534 unqualified_name_lookup_error (capture_id);
9535 continue;
9537 else if (DECL_P (capture_init_expr)
9538 && (!VAR_P (capture_init_expr)
9539 && TREE_CODE (capture_init_expr) != PARM_DECL))
9541 error_at (capture_token->location,
9542 "capture of non-variable %qD ",
9543 capture_init_expr);
9544 inform (DECL_SOURCE_LOCATION (capture_init_expr),
9545 "%q#D declared here", capture_init_expr);
9546 continue;
9548 if (VAR_P (capture_init_expr)
9549 && decl_storage_duration (capture_init_expr) != dk_auto)
9551 if (pedwarn (capture_token->location, 0, "capture of variable "
9552 "%qD with non-automatic storage duration",
9553 capture_init_expr))
9554 inform (DECL_SOURCE_LOCATION (capture_init_expr),
9555 "%q#D declared here", capture_init_expr);
9556 continue;
9559 capture_init_expr
9560 = finish_id_expression
9561 (capture_id,
9562 capture_init_expr,
9563 parser->scope,
9564 &idk,
9565 /*integral_constant_expression_p=*/false,
9566 /*allow_non_integral_constant_expression_p=*/false,
9567 /*non_integral_constant_expression_p=*/NULL,
9568 /*template_p=*/false,
9569 /*done=*/true,
9570 /*address_p=*/false,
9571 /*template_arg_p=*/false,
9572 &error_msg,
9573 capture_token->location);
9575 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9577 cp_lexer_consume_token (parser->lexer);
9578 capture_init_expr = make_pack_expansion (capture_init_expr);
9580 else
9581 check_for_bare_parameter_packs (capture_init_expr);
9584 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9585 && !explicit_init_p)
9587 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9588 && capture_kind == BY_COPY)
9589 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9590 "of %qD redundant with by-copy capture default",
9591 capture_id);
9592 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9593 && capture_kind == BY_REFERENCE)
9594 pedwarn (capture_token->location, 0, "explicit by-reference "
9595 "capture of %qD redundant with by-reference capture "
9596 "default", capture_id);
9599 add_capture (lambda_expr,
9600 capture_id,
9601 capture_init_expr,
9602 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9603 explicit_init_p);
9606 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9609 /* Parse the (optional) middle of a lambda expression.
9611 lambda-declarator:
9612 < template-parameter-list [opt] >
9613 ( parameter-declaration-clause [opt] )
9614 attribute-specifier [opt]
9615 mutable [opt]
9616 exception-specification [opt]
9617 lambda-return-type-clause [opt]
9619 LAMBDA_EXPR is the current representation of the lambda expression. */
9621 static bool
9622 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9624 /* 5.1.1.4 of the standard says:
9625 If a lambda-expression does not include a lambda-declarator, it is as if
9626 the lambda-declarator were ().
9627 This means an empty parameter list, no attributes, and no exception
9628 specification. */
9629 tree param_list = void_list_node;
9630 tree attributes = NULL_TREE;
9631 tree exception_spec = NULL_TREE;
9632 tree template_param_list = NULL_TREE;
9634 /* The template-parameter-list is optional, but must begin with
9635 an opening angle if present. */
9636 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9638 if (cxx_dialect < cxx14)
9639 pedwarn (parser->lexer->next_token->location, 0,
9640 "lambda templates are only available with "
9641 "-std=c++14 or -std=gnu++14");
9643 cp_lexer_consume_token (parser->lexer);
9645 template_param_list = cp_parser_template_parameter_list (parser);
9647 cp_parser_skip_to_end_of_template_parameter_list (parser);
9649 /* We just processed one more parameter list. */
9650 ++parser->num_template_parameter_lists;
9653 /* The parameter-declaration-clause is optional (unless
9654 template-parameter-list was given), but must begin with an
9655 opening parenthesis if present. */
9656 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9658 cp_lexer_consume_token (parser->lexer);
9660 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9662 /* Parse parameters. */
9663 param_list = cp_parser_parameter_declaration_clause (parser);
9665 /* Default arguments shall not be specified in the
9666 parameter-declaration-clause of a lambda-declarator. */
9667 for (tree t = param_list; t; t = TREE_CHAIN (t))
9668 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
9669 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9670 "default argument specified for lambda parameter");
9672 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9674 attributes = cp_parser_attributes_opt (parser);
9676 /* Parse optional `mutable' keyword. */
9677 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9679 cp_lexer_consume_token (parser->lexer);
9680 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9683 /* Parse optional exception specification. */
9684 exception_spec = cp_parser_exception_specification_opt (parser);
9686 /* Parse optional trailing return type. */
9687 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9689 cp_lexer_consume_token (parser->lexer);
9690 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9691 = cp_parser_trailing_type_id (parser);
9694 /* The function parameters must be in scope all the way until after the
9695 trailing-return-type in case of decltype. */
9696 pop_bindings_and_leave_scope ();
9698 else if (template_param_list != NULL_TREE) // generate diagnostic
9699 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9701 /* Create the function call operator.
9703 Messing with declarators like this is no uglier than building up the
9704 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9705 other code. */
9707 cp_decl_specifier_seq return_type_specs;
9708 cp_declarator* declarator;
9709 tree fco;
9710 int quals;
9711 void *p;
9713 clear_decl_specs (&return_type_specs);
9714 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9715 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9716 else
9717 /* Maybe we will deduce the return type later. */
9718 return_type_specs.type = make_auto ();
9720 p = obstack_alloc (&declarator_obstack, 0);
9722 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9723 sfk_none);
9725 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9726 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9727 declarator = make_call_declarator (declarator, param_list, quals,
9728 VIRT_SPEC_UNSPECIFIED,
9729 REF_QUAL_NONE,
9730 exception_spec,
9731 /*late_return_type=*/NULL_TREE,
9732 /*requires_clause*/NULL_TREE);
9733 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9735 fco = grokmethod (&return_type_specs,
9736 declarator,
9737 attributes);
9738 if (fco != error_mark_node)
9740 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9741 DECL_ARTIFICIAL (fco) = 1;
9742 /* Give the object parameter a different name. */
9743 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9744 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9745 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9747 if (template_param_list)
9749 fco = finish_member_template_decl (fco);
9750 finish_template_decl (template_param_list);
9751 --parser->num_template_parameter_lists;
9753 else if (parser->fully_implicit_function_template_p)
9754 fco = finish_fully_implicit_template (parser, fco);
9756 finish_member_declaration (fco);
9758 obstack_free (&declarator_obstack, p);
9760 return (fco != error_mark_node);
9764 /* Parse the body of a lambda expression, which is simply
9766 compound-statement
9768 but which requires special handling.
9769 LAMBDA_EXPR is the current representation of the lambda expression. */
9771 static void
9772 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9774 bool nested = (current_function_decl != NULL_TREE);
9775 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9776 if (nested)
9777 push_function_context ();
9778 else
9779 /* Still increment function_depth so that we don't GC in the
9780 middle of an expression. */
9781 ++function_depth;
9782 /* Clear this in case we're in the middle of a default argument. */
9783 parser->local_variables_forbidden_p = false;
9785 /* Finish the function call operator
9786 - class_specifier
9787 + late_parsing_for_member
9788 + function_definition_after_declarator
9789 + ctor_initializer_opt_and_function_body */
9791 tree fco = lambda_function (lambda_expr);
9792 tree body;
9793 bool done = false;
9794 tree compound_stmt;
9795 tree cap;
9797 /* Let the front end know that we are going to be defining this
9798 function. */
9799 start_preparsed_function (fco,
9800 NULL_TREE,
9801 SF_PRE_PARSED | SF_INCLASS_INLINE);
9803 start_lambda_scope (fco);
9804 body = begin_function_body ();
9806 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9807 goto out;
9809 /* Push the proxies for any explicit captures. */
9810 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9811 cap = TREE_CHAIN (cap))
9812 build_capture_proxy (TREE_PURPOSE (cap));
9814 compound_stmt = begin_compound_stmt (0);
9816 /* 5.1.1.4 of the standard says:
9817 If a lambda-expression does not include a trailing-return-type, it
9818 is as if the trailing-return-type denotes the following type:
9819 * if the compound-statement is of the form
9820 { return attribute-specifier [opt] expression ; }
9821 the type of the returned expression after lvalue-to-rvalue
9822 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9823 (_conv.array_ 4.2), and function-to-pointer conversion
9824 (_conv.func_ 4.3);
9825 * otherwise, void. */
9827 /* In a lambda that has neither a lambda-return-type-clause
9828 nor a deducible form, errors should be reported for return statements
9829 in the body. Since we used void as the placeholder return type, parsing
9830 the body as usual will give such desired behavior. */
9831 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9832 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9833 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9835 tree expr = NULL_TREE;
9836 cp_id_kind idk = CP_ID_KIND_NONE;
9838 /* Parse tentatively in case there's more after the initial return
9839 statement. */
9840 cp_parser_parse_tentatively (parser);
9842 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9844 expr = cp_parser_expression (parser, &idk);
9846 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9847 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9849 if (cp_parser_parse_definitely (parser))
9851 if (!processing_template_decl)
9852 apply_deduced_return_type (fco, lambda_return_type (expr));
9854 /* Will get error here if type not deduced yet. */
9855 finish_return_stmt (expr);
9857 done = true;
9861 if (!done)
9863 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9864 cp_parser_label_declaration (parser);
9865 cp_parser_statement_seq_opt (parser, NULL_TREE);
9866 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9869 finish_compound_stmt (compound_stmt);
9871 out:
9872 finish_function_body (body);
9873 finish_lambda_scope ();
9875 /* Finish the function and generate code for it if necessary. */
9876 tree fn = finish_function (/*inline*/2);
9878 /* Only expand if the call op is not a template. */
9879 if (!DECL_TEMPLATE_INFO (fco))
9880 expand_or_defer_fn (fn);
9883 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9884 if (nested)
9885 pop_function_context();
9886 else
9887 --function_depth;
9890 /* Statements [gram.stmt.stmt] */
9892 /* Parse a statement.
9894 statement:
9895 labeled-statement
9896 expression-statement
9897 compound-statement
9898 selection-statement
9899 iteration-statement
9900 jump-statement
9901 declaration-statement
9902 try-block
9904 C++11:
9906 statement:
9907 labeled-statement
9908 attribute-specifier-seq (opt) expression-statement
9909 attribute-specifier-seq (opt) compound-statement
9910 attribute-specifier-seq (opt) selection-statement
9911 attribute-specifier-seq (opt) iteration-statement
9912 attribute-specifier-seq (opt) jump-statement
9913 declaration-statement
9914 attribute-specifier-seq (opt) try-block
9916 TM Extension:
9918 statement:
9919 atomic-statement
9921 IN_COMPOUND is true when the statement is nested inside a
9922 cp_parser_compound_statement; this matters for certain pragmas.
9924 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9925 is a (possibly labeled) if statement which is not enclosed in braces
9926 and has an else clause. This is used to implement -Wparentheses. */
9928 static void
9929 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9930 bool in_compound, bool *if_p)
9932 tree statement, std_attrs = NULL_TREE;
9933 cp_token *token;
9934 location_t statement_location, attrs_location;
9936 restart:
9937 if (if_p != NULL)
9938 *if_p = false;
9939 /* There is no statement yet. */
9940 statement = NULL_TREE;
9942 saved_token_sentinel saved_tokens (parser->lexer);
9943 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9944 if (c_dialect_objc ())
9945 /* In obj-c++, seeing '[[' might be the either the beginning of
9946 c++11 attributes, or a nested objc-message-expression. So
9947 let's parse the c++11 attributes tentatively. */
9948 cp_parser_parse_tentatively (parser);
9949 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9950 if (c_dialect_objc ())
9952 if (!cp_parser_parse_definitely (parser))
9953 std_attrs = NULL_TREE;
9956 /* Peek at the next token. */
9957 token = cp_lexer_peek_token (parser->lexer);
9958 /* Remember the location of the first token in the statement. */
9959 statement_location = token->location;
9960 /* If this is a keyword, then that will often determine what kind of
9961 statement we have. */
9962 if (token->type == CPP_KEYWORD)
9964 enum rid keyword = token->keyword;
9966 switch (keyword)
9968 case RID_CASE:
9969 case RID_DEFAULT:
9970 /* Looks like a labeled-statement with a case label.
9971 Parse the label, and then use tail recursion to parse
9972 the statement. */
9973 cp_parser_label_for_labeled_statement (parser, std_attrs);
9974 goto restart;
9976 case RID_IF:
9977 case RID_SWITCH:
9978 statement = cp_parser_selection_statement (parser, if_p);
9979 break;
9981 case RID_WHILE:
9982 case RID_DO:
9983 case RID_FOR:
9984 statement = cp_parser_iteration_statement (parser, false);
9985 break;
9987 case RID_CILK_FOR:
9988 if (!flag_cilkplus)
9990 error_at (cp_lexer_peek_token (parser->lexer)->location,
9991 "-fcilkplus must be enabled to use %<_Cilk_for%>");
9992 cp_lexer_consume_token (parser->lexer);
9993 statement = error_mark_node;
9995 else
9996 statement = cp_parser_cilk_for (parser, integer_zero_node);
9997 break;
9999 case RID_BREAK:
10000 case RID_CONTINUE:
10001 case RID_RETURN:
10002 case RID_GOTO:
10003 statement = cp_parser_jump_statement (parser);
10004 break;
10006 case RID_CILK_SYNC:
10007 cp_lexer_consume_token (parser->lexer);
10008 if (flag_cilkplus)
10010 tree sync_expr = build_cilk_sync ();
10011 SET_EXPR_LOCATION (sync_expr,
10012 token->location);
10013 statement = finish_expr_stmt (sync_expr);
10015 else
10017 error_at (token->location, "-fcilkplus must be enabled to use"
10018 " %<_Cilk_sync%>");
10019 statement = error_mark_node;
10021 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10022 break;
10024 /* Objective-C++ exception-handling constructs. */
10025 case RID_AT_TRY:
10026 case RID_AT_CATCH:
10027 case RID_AT_FINALLY:
10028 case RID_AT_SYNCHRONIZED:
10029 case RID_AT_THROW:
10030 statement = cp_parser_objc_statement (parser);
10031 break;
10033 case RID_TRY:
10034 statement = cp_parser_try_block (parser);
10035 break;
10037 case RID_NAMESPACE:
10038 /* This must be a namespace alias definition. */
10039 cp_parser_declaration_statement (parser);
10040 return;
10042 case RID_TRANSACTION_ATOMIC:
10043 case RID_TRANSACTION_RELAXED:
10044 statement = cp_parser_transaction (parser, keyword);
10045 break;
10046 case RID_TRANSACTION_CANCEL:
10047 statement = cp_parser_transaction_cancel (parser);
10048 break;
10050 default:
10051 /* It might be a keyword like `int' that can start a
10052 declaration-statement. */
10053 break;
10056 else if (token->type == CPP_NAME)
10058 /* If the next token is a `:', then we are looking at a
10059 labeled-statement. */
10060 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10061 if (token->type == CPP_COLON)
10063 /* Looks like a labeled-statement with an ordinary label.
10064 Parse the label, and then use tail recursion to parse
10065 the statement. */
10067 cp_parser_label_for_labeled_statement (parser, std_attrs);
10068 goto restart;
10071 /* Anything that starts with a `{' must be a compound-statement. */
10072 else if (token->type == CPP_OPEN_BRACE)
10073 statement = cp_parser_compound_statement (parser, NULL, false, false);
10074 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10075 a statement all its own. */
10076 else if (token->type == CPP_PRAGMA)
10078 /* Only certain OpenMP pragmas are attached to statements, and thus
10079 are considered statements themselves. All others are not. In
10080 the context of a compound, accept the pragma as a "statement" and
10081 return so that we can check for a close brace. Otherwise we
10082 require a real statement and must go back and read one. */
10083 if (in_compound)
10084 cp_parser_pragma (parser, pragma_compound);
10085 else if (!cp_parser_pragma (parser, pragma_stmt))
10086 goto restart;
10087 return;
10089 else if (token->type == CPP_EOF)
10091 cp_parser_error (parser, "expected statement");
10092 return;
10095 /* Everything else must be a declaration-statement or an
10096 expression-statement. Try for the declaration-statement
10097 first, unless we are looking at a `;', in which case we know that
10098 we have an expression-statement. */
10099 if (!statement)
10101 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10103 if (std_attrs != NULL_TREE)
10105 /* Attributes should be parsed as part of the the
10106 declaration, so let's un-parse them. */
10107 saved_tokens.rollback();
10108 std_attrs = NULL_TREE;
10111 cp_parser_parse_tentatively (parser);
10112 /* Try to parse the declaration-statement. */
10113 cp_parser_declaration_statement (parser);
10114 /* If that worked, we're done. */
10115 if (cp_parser_parse_definitely (parser))
10116 return;
10118 /* Look for an expression-statement instead. */
10119 statement = cp_parser_expression_statement (parser, in_statement_expr);
10122 /* Set the line number for the statement. */
10123 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10124 SET_EXPR_LOCATION (statement, statement_location);
10126 /* Note that for now, we don't do anything with c++11 statements
10127 parsed at this level. */
10128 if (std_attrs != NULL_TREE)
10129 warning_at (attrs_location,
10130 OPT_Wattributes,
10131 "attributes at the beginning of statement are ignored");
10134 /* Parse the label for a labeled-statement, i.e.
10136 identifier :
10137 case constant-expression :
10138 default :
10140 GNU Extension:
10141 case constant-expression ... constant-expression : statement
10143 When a label is parsed without errors, the label is added to the
10144 parse tree by the finish_* functions, so this function doesn't
10145 have to return the label. */
10147 static void
10148 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10150 cp_token *token;
10151 tree label = NULL_TREE;
10152 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10154 /* The next token should be an identifier. */
10155 token = cp_lexer_peek_token (parser->lexer);
10156 if (token->type != CPP_NAME
10157 && token->type != CPP_KEYWORD)
10159 cp_parser_error (parser, "expected labeled-statement");
10160 return;
10163 parser->colon_corrects_to_scope_p = false;
10164 switch (token->keyword)
10166 case RID_CASE:
10168 tree expr, expr_hi;
10169 cp_token *ellipsis;
10171 /* Consume the `case' token. */
10172 cp_lexer_consume_token (parser->lexer);
10173 /* Parse the constant-expression. */
10174 expr = cp_parser_constant_expression (parser);
10175 if (check_for_bare_parameter_packs (expr))
10176 expr = error_mark_node;
10178 ellipsis = cp_lexer_peek_token (parser->lexer);
10179 if (ellipsis->type == CPP_ELLIPSIS)
10181 /* Consume the `...' token. */
10182 cp_lexer_consume_token (parser->lexer);
10183 expr_hi = cp_parser_constant_expression (parser);
10184 if (check_for_bare_parameter_packs (expr_hi))
10185 expr_hi = error_mark_node;
10187 /* We don't need to emit warnings here, as the common code
10188 will do this for us. */
10190 else
10191 expr_hi = NULL_TREE;
10193 if (parser->in_switch_statement_p)
10194 finish_case_label (token->location, expr, expr_hi);
10195 else
10196 error_at (token->location,
10197 "case label %qE not within a switch statement",
10198 expr);
10200 break;
10202 case RID_DEFAULT:
10203 /* Consume the `default' token. */
10204 cp_lexer_consume_token (parser->lexer);
10206 if (parser->in_switch_statement_p)
10207 finish_case_label (token->location, NULL_TREE, NULL_TREE);
10208 else
10209 error_at (token->location, "case label not within a switch statement");
10210 break;
10212 default:
10213 /* Anything else must be an ordinary label. */
10214 label = finish_label_stmt (cp_parser_identifier (parser));
10215 break;
10218 /* Require the `:' token. */
10219 cp_parser_require (parser, CPP_COLON, RT_COLON);
10221 /* An ordinary label may optionally be followed by attributes.
10222 However, this is only permitted if the attributes are then
10223 followed by a semicolon. This is because, for backward
10224 compatibility, when parsing
10225 lab: __attribute__ ((unused)) int i;
10226 we want the attribute to attach to "i", not "lab". */
10227 if (label != NULL_TREE
10228 && cp_next_tokens_can_be_gnu_attribute_p (parser))
10230 tree attrs;
10231 cp_parser_parse_tentatively (parser);
10232 attrs = cp_parser_gnu_attributes_opt (parser);
10233 if (attrs == NULL_TREE
10234 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10235 cp_parser_abort_tentative_parse (parser);
10236 else if (!cp_parser_parse_definitely (parser))
10238 else
10239 attributes = chainon (attributes, attrs);
10242 if (attributes != NULL_TREE)
10243 cplus_decl_attributes (&label, attributes, 0);
10245 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10248 /* Parse an expression-statement.
10250 expression-statement:
10251 expression [opt] ;
10253 Returns the new EXPR_STMT -- or NULL_TREE if the expression
10254 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
10255 indicates whether this expression-statement is part of an
10256 expression statement. */
10258 static tree
10259 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
10261 tree statement = NULL_TREE;
10262 cp_token *token = cp_lexer_peek_token (parser->lexer);
10264 /* If the next token is a ';', then there is no expression
10265 statement. */
10266 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10268 statement = cp_parser_expression (parser);
10269 if (statement == error_mark_node
10270 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10272 cp_parser_skip_to_end_of_block_or_statement (parser);
10273 return error_mark_node;
10277 /* Give a helpful message for "A<T>::type t;" and the like. */
10278 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
10279 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10281 if (TREE_CODE (statement) == SCOPE_REF)
10282 error_at (token->location, "need %<typename%> before %qE because "
10283 "%qT is a dependent scope",
10284 statement, TREE_OPERAND (statement, 0));
10285 else if (is_overloaded_fn (statement)
10286 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
10288 /* A::A a; */
10289 tree fn = get_first_fn (statement);
10290 error_at (token->location,
10291 "%<%T::%D%> names the constructor, not the type",
10292 DECL_CONTEXT (fn), DECL_NAME (fn));
10296 /* Consume the final `;'. */
10297 cp_parser_consume_semicolon_at_end_of_statement (parser);
10299 if (in_statement_expr
10300 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10301 /* This is the final expression statement of a statement
10302 expression. */
10303 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10304 else if (statement)
10305 statement = finish_expr_stmt (statement);
10307 return statement;
10310 /* Parse a compound-statement.
10312 compound-statement:
10313 { statement-seq [opt] }
10315 GNU extension:
10317 compound-statement:
10318 { label-declaration-seq [opt] statement-seq [opt] }
10320 label-declaration-seq:
10321 label-declaration
10322 label-declaration-seq label-declaration
10324 Returns a tree representing the statement. */
10326 static tree
10327 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10328 bool in_try, bool function_body)
10330 tree compound_stmt;
10332 /* Consume the `{'. */
10333 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10334 return error_mark_node;
10335 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10336 && !function_body && cxx_dialect < cxx14)
10337 pedwarn (input_location, OPT_Wpedantic,
10338 "compound-statement in constexpr function");
10339 /* Begin the compound-statement. */
10340 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
10341 /* If the next keyword is `__label__' we have a label declaration. */
10342 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10343 cp_parser_label_declaration (parser);
10344 /* Parse an (optional) statement-seq. */
10345 cp_parser_statement_seq_opt (parser, in_statement_expr);
10346 /* Finish the compound-statement. */
10347 finish_compound_stmt (compound_stmt);
10348 /* Consume the `}'. */
10349 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10351 return compound_stmt;
10354 /* Parse an (optional) statement-seq.
10356 statement-seq:
10357 statement
10358 statement-seq [opt] statement */
10360 static void
10361 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10363 /* Scan statements until there aren't any more. */
10364 while (true)
10366 cp_token *token = cp_lexer_peek_token (parser->lexer);
10368 /* If we are looking at a `}', then we have run out of
10369 statements; the same is true if we have reached the end
10370 of file, or have stumbled upon a stray '@end'. */
10371 if (token->type == CPP_CLOSE_BRACE
10372 || token->type == CPP_EOF
10373 || token->type == CPP_PRAGMA_EOL
10374 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10375 break;
10377 /* If we are in a compound statement and find 'else' then
10378 something went wrong. */
10379 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10381 if (parser->in_statement & IN_IF_STMT)
10382 break;
10383 else
10385 token = cp_lexer_consume_token (parser->lexer);
10386 error_at (token->location, "%<else%> without a previous %<if%>");
10390 /* Parse the statement. */
10391 cp_parser_statement (parser, in_statement_expr, true, NULL);
10395 /* Parse a selection-statement.
10397 selection-statement:
10398 if ( condition ) statement
10399 if ( condition ) statement else statement
10400 switch ( condition ) statement
10402 Returns the new IF_STMT or SWITCH_STMT.
10404 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10405 is a (possibly labeled) if statement which is not enclosed in
10406 braces and has an else clause. This is used to implement
10407 -Wparentheses. */
10409 static tree
10410 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
10412 cp_token *token;
10413 enum rid keyword;
10414 token_indent_info guard_tinfo;
10416 if (if_p != NULL)
10417 *if_p = false;
10419 /* Peek at the next token. */
10420 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10421 guard_tinfo = get_token_indent_info (token);
10423 /* See what kind of keyword it is. */
10424 keyword = token->keyword;
10425 switch (keyword)
10427 case RID_IF:
10428 case RID_SWITCH:
10430 tree statement;
10431 tree condition;
10433 /* Look for the `('. */
10434 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10436 cp_parser_skip_to_end_of_statement (parser);
10437 return error_mark_node;
10440 /* Begin the selection-statement. */
10441 if (keyword == RID_IF)
10442 statement = begin_if_stmt ();
10443 else
10444 statement = begin_switch_stmt ();
10446 /* Parse the condition. */
10447 condition = cp_parser_condition (parser);
10448 /* Look for the `)'. */
10449 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10450 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10451 /*consume_paren=*/true);
10453 if (keyword == RID_IF)
10455 bool nested_if;
10456 unsigned char in_statement;
10458 /* Add the condition. */
10459 finish_if_stmt_cond (condition, statement);
10461 /* Parse the then-clause. */
10462 in_statement = parser->in_statement;
10463 parser->in_statement |= IN_IF_STMT;
10464 cp_parser_implicitly_scoped_statement (parser, &nested_if,
10465 guard_tinfo);
10466 parser->in_statement = in_statement;
10468 finish_then_clause (statement);
10470 /* If the next token is `else', parse the else-clause. */
10471 if (cp_lexer_next_token_is_keyword (parser->lexer,
10472 RID_ELSE))
10474 guard_tinfo
10475 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
10476 /* Consume the `else' keyword. */
10477 cp_lexer_consume_token (parser->lexer);
10478 begin_else_clause (statement);
10479 /* Parse the else-clause. */
10480 cp_parser_implicitly_scoped_statement (parser, NULL,
10481 guard_tinfo);
10483 finish_else_clause (statement);
10485 /* If we are currently parsing a then-clause, then
10486 IF_P will not be NULL. We set it to true to
10487 indicate that this if statement has an else clause.
10488 This may trigger the Wparentheses warning below
10489 when we get back up to the parent if statement. */
10490 if (if_p != NULL)
10491 *if_p = true;
10493 else
10495 /* This if statement does not have an else clause. If
10496 NESTED_IF is true, then the then-clause is an if
10497 statement which does have an else clause. We warn
10498 about the potential ambiguity. */
10499 if (nested_if)
10500 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10501 "suggest explicit braces to avoid ambiguous"
10502 " %<else%>");
10505 /* Now we're all done with the if-statement. */
10506 finish_if_stmt (statement);
10508 else
10510 bool in_switch_statement_p;
10511 unsigned char in_statement;
10513 /* Add the condition. */
10514 finish_switch_cond (condition, statement);
10516 /* Parse the body of the switch-statement. */
10517 in_switch_statement_p = parser->in_switch_statement_p;
10518 in_statement = parser->in_statement;
10519 parser->in_switch_statement_p = true;
10520 parser->in_statement |= IN_SWITCH_STMT;
10521 cp_parser_implicitly_scoped_statement (parser, NULL,
10522 guard_tinfo);
10523 parser->in_switch_statement_p = in_switch_statement_p;
10524 parser->in_statement = in_statement;
10526 /* Now we're all done with the switch-statement. */
10527 finish_switch_stmt (statement);
10530 return statement;
10532 break;
10534 default:
10535 cp_parser_error (parser, "expected selection-statement");
10536 return error_mark_node;
10540 /* Parse a condition.
10542 condition:
10543 expression
10544 type-specifier-seq declarator = initializer-clause
10545 type-specifier-seq declarator braced-init-list
10547 GNU Extension:
10549 condition:
10550 type-specifier-seq declarator asm-specification [opt]
10551 attributes [opt] = assignment-expression
10553 Returns the expression that should be tested. */
10555 static tree
10556 cp_parser_condition (cp_parser* parser)
10558 cp_decl_specifier_seq type_specifiers;
10559 const char *saved_message;
10560 int declares_class_or_enum;
10562 /* Try the declaration first. */
10563 cp_parser_parse_tentatively (parser);
10564 /* New types are not allowed in the type-specifier-seq for a
10565 condition. */
10566 saved_message = parser->type_definition_forbidden_message;
10567 parser->type_definition_forbidden_message
10568 = G_("types may not be defined in conditions");
10569 /* Parse the type-specifier-seq. */
10570 cp_parser_decl_specifier_seq (parser,
10571 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
10572 &type_specifiers,
10573 &declares_class_or_enum);
10574 /* Restore the saved message. */
10575 parser->type_definition_forbidden_message = saved_message;
10576 /* If all is well, we might be looking at a declaration. */
10577 if (!cp_parser_error_occurred (parser))
10579 tree decl;
10580 tree asm_specification;
10581 tree attributes;
10582 cp_declarator *declarator;
10583 tree initializer = NULL_TREE;
10585 /* Parse the declarator. */
10586 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10587 /*ctor_dtor_or_conv_p=*/NULL,
10588 /*parenthesized_p=*/NULL,
10589 /*member_p=*/false,
10590 /*friend_p=*/false);
10591 /* Parse the attributes. */
10592 attributes = cp_parser_attributes_opt (parser);
10593 /* Parse the asm-specification. */
10594 asm_specification = cp_parser_asm_specification_opt (parser);
10595 /* If the next token is not an `=' or '{', then we might still be
10596 looking at an expression. For example:
10598 if (A(a).x)
10600 looks like a decl-specifier-seq and a declarator -- but then
10601 there is no `=', so this is an expression. */
10602 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10603 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10604 cp_parser_simulate_error (parser);
10606 /* If we did see an `=' or '{', then we are looking at a declaration
10607 for sure. */
10608 if (cp_parser_parse_definitely (parser))
10610 tree pushed_scope;
10611 bool non_constant_p;
10612 bool flags = LOOKUP_ONLYCONVERTING;
10614 /* Create the declaration. */
10615 decl = start_decl (declarator, &type_specifiers,
10616 /*initialized_p=*/true,
10617 attributes, /*prefix_attributes=*/NULL_TREE,
10618 &pushed_scope);
10620 /* Parse the initializer. */
10621 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10623 initializer = cp_parser_braced_list (parser, &non_constant_p);
10624 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10625 flags = 0;
10627 else
10629 /* Consume the `='. */
10630 cp_parser_require (parser, CPP_EQ, RT_EQ);
10631 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10633 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10634 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10636 /* Process the initializer. */
10637 cp_finish_decl (decl,
10638 initializer, !non_constant_p,
10639 asm_specification,
10640 flags);
10642 if (pushed_scope)
10643 pop_scope (pushed_scope);
10645 return convert_from_reference (decl);
10648 /* If we didn't even get past the declarator successfully, we are
10649 definitely not looking at a declaration. */
10650 else
10651 cp_parser_abort_tentative_parse (parser);
10653 /* Otherwise, we are looking at an expression. */
10654 return cp_parser_expression (parser);
10657 /* Parses a for-statement or range-for-statement until the closing ')',
10658 not included. */
10660 static tree
10661 cp_parser_for (cp_parser *parser, bool ivdep)
10663 tree init, scope, decl;
10664 bool is_range_for;
10666 /* Begin the for-statement. */
10667 scope = begin_for_scope (&init);
10669 /* Parse the initialization. */
10670 is_range_for = cp_parser_for_init_statement (parser, &decl);
10672 if (is_range_for)
10673 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10674 else
10675 return cp_parser_c_for (parser, scope, init, ivdep);
10678 static tree
10679 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10681 /* Normal for loop */
10682 tree condition = NULL_TREE;
10683 tree expression = NULL_TREE;
10684 tree stmt;
10686 stmt = begin_for_stmt (scope, init);
10687 /* The for-init-statement has already been parsed in
10688 cp_parser_for_init_statement, so no work is needed here. */
10689 finish_for_init_stmt (stmt);
10691 /* If there's a condition, process it. */
10692 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10693 condition = cp_parser_condition (parser);
10694 else if (ivdep)
10696 cp_parser_error (parser, "missing loop condition in loop with "
10697 "%<GCC ivdep%> pragma");
10698 condition = error_mark_node;
10700 finish_for_cond (condition, stmt, ivdep);
10701 /* Look for the `;'. */
10702 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10704 /* If there's an expression, process it. */
10705 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10706 expression = cp_parser_expression (parser);
10707 finish_for_expr (expression, stmt);
10709 return stmt;
10712 /* Tries to parse a range-based for-statement:
10714 range-based-for:
10715 decl-specifier-seq declarator : expression
10717 The decl-specifier-seq declarator and the `:' are already parsed by
10718 cp_parser_for_init_statement. If processing_template_decl it returns a
10719 newly created RANGE_FOR_STMT; if not, it is converted to a
10720 regular FOR_STMT. */
10722 static tree
10723 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10724 bool ivdep)
10726 tree stmt, range_expr;
10728 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10730 bool expr_non_constant_p;
10731 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10733 else
10734 range_expr = cp_parser_expression (parser);
10736 /* If in template, STMT is converted to a normal for-statement
10737 at instantiation. If not, it is done just ahead. */
10738 if (processing_template_decl)
10740 if (check_for_bare_parameter_packs (range_expr))
10741 range_expr = error_mark_node;
10742 stmt = begin_range_for_stmt (scope, init);
10743 if (ivdep)
10744 RANGE_FOR_IVDEP (stmt) = 1;
10745 finish_range_for_decl (stmt, range_decl, range_expr);
10746 if (!type_dependent_expression_p (range_expr)
10747 /* do_auto_deduction doesn't mess with template init-lists. */
10748 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10749 do_range_for_auto_deduction (range_decl, range_expr);
10751 else
10753 stmt = begin_for_stmt (scope, init);
10754 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10756 return stmt;
10759 /* Subroutine of cp_convert_range_for: given the initializer expression,
10760 builds up the range temporary. */
10762 static tree
10763 build_range_temp (tree range_expr)
10765 tree range_type, range_temp;
10767 /* Find out the type deduced by the declaration
10768 `auto &&__range = range_expr'. */
10769 range_type = cp_build_reference_type (make_auto (), true);
10770 range_type = do_auto_deduction (range_type, range_expr,
10771 type_uses_auto (range_type));
10773 /* Create the __range variable. */
10774 range_temp = build_decl (input_location, VAR_DECL,
10775 get_identifier ("__for_range"), range_type);
10776 TREE_USED (range_temp) = 1;
10777 DECL_ARTIFICIAL (range_temp) = 1;
10779 return range_temp;
10782 /* Used by cp_parser_range_for in template context: we aren't going to
10783 do a full conversion yet, but we still need to resolve auto in the
10784 type of the for-range-declaration if present. This is basically
10785 a shortcut version of cp_convert_range_for. */
10787 static void
10788 do_range_for_auto_deduction (tree decl, tree range_expr)
10790 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10791 if (auto_node)
10793 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10794 range_temp = convert_from_reference (build_range_temp (range_expr));
10795 iter_type = (cp_parser_perform_range_for_lookup
10796 (range_temp, &begin_dummy, &end_dummy));
10797 if (iter_type)
10799 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10800 iter_type);
10801 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10802 tf_warning_or_error);
10803 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10804 iter_decl, auto_node);
10809 /* Converts a range-based for-statement into a normal
10810 for-statement, as per the definition.
10812 for (RANGE_DECL : RANGE_EXPR)
10813 BLOCK
10815 should be equivalent to:
10818 auto &&__range = RANGE_EXPR;
10819 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10820 __begin != __end;
10821 ++__begin)
10823 RANGE_DECL = *__begin;
10824 BLOCK
10828 If RANGE_EXPR is an array:
10829 BEGIN_EXPR = __range
10830 END_EXPR = __range + ARRAY_SIZE(__range)
10831 Else if RANGE_EXPR has a member 'begin' or 'end':
10832 BEGIN_EXPR = __range.begin()
10833 END_EXPR = __range.end()
10834 Else:
10835 BEGIN_EXPR = begin(__range)
10836 END_EXPR = end(__range);
10838 If __range has a member 'begin' but not 'end', or vice versa, we must
10839 still use the second alternative (it will surely fail, however).
10840 When calling begin()/end() in the third alternative we must use
10841 argument dependent lookup, but always considering 'std' as an associated
10842 namespace. */
10844 tree
10845 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10846 bool ivdep)
10848 tree begin, end;
10849 tree iter_type, begin_expr, end_expr;
10850 tree condition, expression;
10852 if (range_decl == error_mark_node || range_expr == error_mark_node)
10853 /* If an error happened previously do nothing or else a lot of
10854 unhelpful errors would be issued. */
10855 begin_expr = end_expr = iter_type = error_mark_node;
10856 else
10858 tree range_temp;
10860 if (VAR_P (range_expr)
10861 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10862 /* Can't bind a reference to an array of runtime bound. */
10863 range_temp = range_expr;
10864 else
10866 range_temp = build_range_temp (range_expr);
10867 pushdecl (range_temp);
10868 cp_finish_decl (range_temp, range_expr,
10869 /*is_constant_init*/false, NULL_TREE,
10870 LOOKUP_ONLYCONVERTING);
10871 range_temp = convert_from_reference (range_temp);
10873 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10874 &begin_expr, &end_expr);
10877 /* The new for initialization statement. */
10878 begin = build_decl (input_location, VAR_DECL,
10879 get_identifier ("__for_begin"), iter_type);
10880 TREE_USED (begin) = 1;
10881 DECL_ARTIFICIAL (begin) = 1;
10882 pushdecl (begin);
10883 cp_finish_decl (begin, begin_expr,
10884 /*is_constant_init*/false, NULL_TREE,
10885 LOOKUP_ONLYCONVERTING);
10887 end = build_decl (input_location, VAR_DECL,
10888 get_identifier ("__for_end"), iter_type);
10889 TREE_USED (end) = 1;
10890 DECL_ARTIFICIAL (end) = 1;
10891 pushdecl (end);
10892 cp_finish_decl (end, end_expr,
10893 /*is_constant_init*/false, NULL_TREE,
10894 LOOKUP_ONLYCONVERTING);
10896 finish_for_init_stmt (statement);
10898 /* The new for condition. */
10899 condition = build_x_binary_op (input_location, NE_EXPR,
10900 begin, ERROR_MARK,
10901 end, ERROR_MARK,
10902 NULL, tf_warning_or_error);
10903 finish_for_cond (condition, statement, ivdep);
10905 /* The new increment expression. */
10906 expression = finish_unary_op_expr (input_location,
10907 PREINCREMENT_EXPR, begin,
10908 tf_warning_or_error);
10909 finish_for_expr (expression, statement);
10911 /* The declaration is initialized with *__begin inside the loop body. */
10912 cp_finish_decl (range_decl,
10913 build_x_indirect_ref (input_location, begin, RO_NULL,
10914 tf_warning_or_error),
10915 /*is_constant_init*/false, NULL_TREE,
10916 LOOKUP_ONLYCONVERTING);
10918 return statement;
10921 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10922 We need to solve both at the same time because the method used
10923 depends on the existence of members begin or end.
10924 Returns the type deduced for the iterator expression. */
10926 static tree
10927 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10929 if (error_operand_p (range))
10931 *begin = *end = error_mark_node;
10932 return error_mark_node;
10935 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10937 error ("range-based %<for%> expression of type %qT "
10938 "has incomplete type", TREE_TYPE (range));
10939 *begin = *end = error_mark_node;
10940 return error_mark_node;
10942 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10944 /* If RANGE is an array, we will use pointer arithmetic. */
10945 *begin = range;
10946 *end = build_binary_op (input_location, PLUS_EXPR,
10947 range,
10948 array_type_nelts_top (TREE_TYPE (range)),
10950 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10952 else
10954 /* If it is not an array, we must do a bit of magic. */
10955 tree id_begin, id_end;
10956 tree member_begin, member_end;
10958 *begin = *end = error_mark_node;
10960 id_begin = get_identifier ("begin");
10961 id_end = get_identifier ("end");
10962 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10963 /*protect=*/2, /*want_type=*/false,
10964 tf_warning_or_error);
10965 member_end = lookup_member (TREE_TYPE (range), id_end,
10966 /*protect=*/2, /*want_type=*/false,
10967 tf_warning_or_error);
10969 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10971 /* Use the member functions. */
10972 if (member_begin != NULL_TREE)
10973 *begin = cp_parser_range_for_member_function (range, id_begin);
10974 else
10975 error ("range-based %<for%> expression of type %qT has an "
10976 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10978 if (member_end != NULL_TREE)
10979 *end = cp_parser_range_for_member_function (range, id_end);
10980 else
10981 error ("range-based %<for%> expression of type %qT has a "
10982 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10984 else
10986 /* Use global functions with ADL. */
10987 vec<tree, va_gc> *vec;
10988 vec = make_tree_vector ();
10990 vec_safe_push (vec, range);
10992 member_begin = perform_koenig_lookup (id_begin, vec,
10993 tf_warning_or_error);
10994 *begin = finish_call_expr (member_begin, &vec, false, true,
10995 tf_warning_or_error);
10996 member_end = perform_koenig_lookup (id_end, vec,
10997 tf_warning_or_error);
10998 *end = finish_call_expr (member_end, &vec, false, true,
10999 tf_warning_or_error);
11001 release_tree_vector (vec);
11004 /* Last common checks. */
11005 if (*begin == error_mark_node || *end == error_mark_node)
11007 /* If one of the expressions is an error do no more checks. */
11008 *begin = *end = error_mark_node;
11009 return error_mark_node;
11011 else if (type_dependent_expression_p (*begin)
11012 || type_dependent_expression_p (*end))
11013 /* Can happen, when, eg, in a template context, Koenig lookup
11014 can't resolve begin/end (c++/58503). */
11015 return NULL_TREE;
11016 else
11018 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
11019 /* The unqualified type of the __begin and __end temporaries should
11020 be the same, as required by the multiple auto declaration. */
11021 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
11022 error ("inconsistent begin/end types in range-based %<for%> "
11023 "statement: %qT and %qT",
11024 TREE_TYPE (*begin), TREE_TYPE (*end));
11025 return iter_type;
11030 /* Helper function for cp_parser_perform_range_for_lookup.
11031 Builds a tree for RANGE.IDENTIFIER(). */
11033 static tree
11034 cp_parser_range_for_member_function (tree range, tree identifier)
11036 tree member, res;
11037 vec<tree, va_gc> *vec;
11039 member = finish_class_member_access_expr (range, identifier,
11040 false, tf_warning_or_error);
11041 if (member == error_mark_node)
11042 return error_mark_node;
11044 vec = make_tree_vector ();
11045 res = finish_call_expr (member, &vec,
11046 /*disallow_virtual=*/false,
11047 /*koenig_p=*/false,
11048 tf_warning_or_error);
11049 release_tree_vector (vec);
11050 return res;
11053 /* Parse an iteration-statement.
11055 iteration-statement:
11056 while ( condition ) statement
11057 do statement while ( expression ) ;
11058 for ( for-init-statement condition [opt] ; expression [opt] )
11059 statement
11061 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
11063 static tree
11064 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
11066 cp_token *token;
11067 enum rid keyword;
11068 tree statement;
11069 unsigned char in_statement;
11070 token_indent_info guard_tinfo;
11072 /* Peek at the next token. */
11073 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
11074 if (!token)
11075 return error_mark_node;
11077 guard_tinfo = get_token_indent_info (token);
11079 /* Remember whether or not we are already within an iteration
11080 statement. */
11081 in_statement = parser->in_statement;
11083 /* See what kind of keyword it is. */
11084 keyword = token->keyword;
11085 switch (keyword)
11087 case RID_WHILE:
11089 tree condition;
11091 /* Begin the while-statement. */
11092 statement = begin_while_stmt ();
11093 /* Look for the `('. */
11094 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11095 /* Parse the condition. */
11096 condition = cp_parser_condition (parser);
11097 finish_while_stmt_cond (condition, statement, ivdep);
11098 /* Look for the `)'. */
11099 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11100 /* Parse the dependent statement. */
11101 parser->in_statement = IN_ITERATION_STMT;
11102 cp_parser_already_scoped_statement (parser, guard_tinfo);
11103 parser->in_statement = in_statement;
11104 /* We're done with the while-statement. */
11105 finish_while_stmt (statement);
11107 break;
11109 case RID_DO:
11111 tree expression;
11113 /* Begin the do-statement. */
11114 statement = begin_do_stmt ();
11115 /* Parse the body of the do-statement. */
11116 parser->in_statement = IN_ITERATION_STMT;
11117 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
11118 parser->in_statement = in_statement;
11119 finish_do_body (statement);
11120 /* Look for the `while' keyword. */
11121 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
11122 /* Look for the `('. */
11123 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11124 /* Parse the expression. */
11125 expression = cp_parser_expression (parser);
11126 /* We're done with the do-statement. */
11127 finish_do_stmt (expression, statement, ivdep);
11128 /* Look for the `)'. */
11129 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11130 /* Look for the `;'. */
11131 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11133 break;
11135 case RID_FOR:
11137 /* Look for the `('. */
11138 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11140 statement = cp_parser_for (parser, ivdep);
11142 /* Look for the `)'. */
11143 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11145 /* Parse the body of the for-statement. */
11146 parser->in_statement = IN_ITERATION_STMT;
11147 cp_parser_already_scoped_statement (parser, guard_tinfo);
11148 parser->in_statement = in_statement;
11150 /* We're done with the for-statement. */
11151 finish_for_stmt (statement);
11153 break;
11155 default:
11156 cp_parser_error (parser, "expected iteration-statement");
11157 statement = error_mark_node;
11158 break;
11161 return statement;
11164 /* Parse a for-init-statement or the declarator of a range-based-for.
11165 Returns true if a range-based-for declaration is seen.
11167 for-init-statement:
11168 expression-statement
11169 simple-declaration */
11171 static bool
11172 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
11174 /* If the next token is a `;', then we have an empty
11175 expression-statement. Grammatically, this is also a
11176 simple-declaration, but an invalid one, because it does not
11177 declare anything. Therefore, if we did not handle this case
11178 specially, we would issue an error message about an invalid
11179 declaration. */
11180 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11182 bool is_range_for = false;
11183 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11185 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11186 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
11188 /* N3994 -- for (id : init) ... */
11189 if (cxx_dialect < cxx1z)
11190 pedwarn (input_location, 0, "range-based for loop without a "
11191 "type-specifier only available with "
11192 "-std=c++1z or -std=gnu++1z");
11193 tree name = cp_parser_identifier (parser);
11194 tree type = cp_build_reference_type (make_auto (), /*rval*/true);
11195 *decl = build_decl (input_location, VAR_DECL, name, type);
11196 pushdecl (*decl);
11197 cp_lexer_consume_token (parser->lexer);
11198 return true;
11201 /* A colon is used in range-based for. */
11202 parser->colon_corrects_to_scope_p = false;
11204 /* We're going to speculatively look for a declaration, falling back
11205 to an expression, if necessary. */
11206 cp_parser_parse_tentatively (parser);
11207 /* Parse the declaration. */
11208 cp_parser_simple_declaration (parser,
11209 /*function_definition_allowed_p=*/false,
11210 decl);
11211 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11212 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11214 /* It is a range-for, consume the ':' */
11215 cp_lexer_consume_token (parser->lexer);
11216 is_range_for = true;
11217 if (cxx_dialect < cxx11)
11219 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11220 "range-based %<for%> loops only available with "
11221 "-std=c++11 or -std=gnu++11");
11222 *decl = error_mark_node;
11225 else
11226 /* The ';' is not consumed yet because we told
11227 cp_parser_simple_declaration not to. */
11228 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11230 if (cp_parser_parse_definitely (parser))
11231 return is_range_for;
11232 /* If the tentative parse failed, then we shall need to look for an
11233 expression-statement. */
11235 /* If we are here, it is an expression-statement. */
11236 cp_parser_expression_statement (parser, NULL_TREE);
11237 return false;
11240 /* Parse a jump-statement.
11242 jump-statement:
11243 break ;
11244 continue ;
11245 return expression [opt] ;
11246 return braced-init-list ;
11247 goto identifier ;
11249 GNU extension:
11251 jump-statement:
11252 goto * expression ;
11254 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
11256 static tree
11257 cp_parser_jump_statement (cp_parser* parser)
11259 tree statement = error_mark_node;
11260 cp_token *token;
11261 enum rid keyword;
11262 unsigned char in_statement;
11264 /* Peek at the next token. */
11265 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
11266 if (!token)
11267 return error_mark_node;
11269 /* See what kind of keyword it is. */
11270 keyword = token->keyword;
11271 switch (keyword)
11273 case RID_BREAK:
11274 in_statement = parser->in_statement & ~IN_IF_STMT;
11275 switch (in_statement)
11277 case 0:
11278 error_at (token->location, "break statement not within loop or switch");
11279 break;
11280 default:
11281 gcc_assert ((in_statement & IN_SWITCH_STMT)
11282 || in_statement == IN_ITERATION_STMT);
11283 statement = finish_break_stmt ();
11284 if (in_statement == IN_ITERATION_STMT)
11285 break_maybe_infinite_loop ();
11286 break;
11287 case IN_OMP_BLOCK:
11288 error_at (token->location, "invalid exit from OpenMP structured block");
11289 break;
11290 case IN_OMP_FOR:
11291 error_at (token->location, "break statement used with OpenMP for loop");
11292 break;
11293 case IN_CILK_SIMD_FOR:
11294 error_at (token->location, "break statement used with Cilk Plus for loop");
11295 break;
11297 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11298 break;
11300 case RID_CONTINUE:
11301 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
11303 case 0:
11304 error_at (token->location, "continue statement not within a loop");
11305 break;
11306 case IN_CILK_SIMD_FOR:
11307 error_at (token->location,
11308 "continue statement within %<#pragma simd%> loop body");
11309 /* Fall through. */
11310 case IN_ITERATION_STMT:
11311 case IN_OMP_FOR:
11312 statement = finish_continue_stmt ();
11313 break;
11314 case IN_OMP_BLOCK:
11315 error_at (token->location, "invalid exit from OpenMP structured block");
11316 break;
11317 default:
11318 gcc_unreachable ();
11320 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11321 break;
11323 case RID_RETURN:
11325 tree expr;
11326 bool expr_non_constant_p;
11328 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11330 cp_lexer_set_source_position (parser->lexer);
11331 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11332 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11334 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11335 expr = cp_parser_expression (parser);
11336 else
11337 /* If the next token is a `;', then there is no
11338 expression. */
11339 expr = NULL_TREE;
11340 /* Build the return-statement. */
11341 statement = finish_return_stmt (expr);
11342 /* Look for the final `;'. */
11343 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11345 break;
11347 case RID_GOTO:
11348 if (parser->in_function_body
11349 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11351 error ("%<goto%> in %<constexpr%> function");
11352 cp_function_chain->invalid_constexpr = true;
11355 /* Create the goto-statement. */
11356 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11358 /* Issue a warning about this use of a GNU extension. */
11359 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11360 /* Consume the '*' token. */
11361 cp_lexer_consume_token (parser->lexer);
11362 /* Parse the dependent expression. */
11363 finish_goto_stmt (cp_parser_expression (parser));
11365 else
11366 finish_goto_stmt (cp_parser_identifier (parser));
11367 /* Look for the final `;'. */
11368 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11369 break;
11371 default:
11372 cp_parser_error (parser, "expected jump-statement");
11373 break;
11376 return statement;
11379 /* Parse a declaration-statement.
11381 declaration-statement:
11382 block-declaration */
11384 static void
11385 cp_parser_declaration_statement (cp_parser* parser)
11387 void *p;
11389 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11390 p = obstack_alloc (&declarator_obstack, 0);
11392 /* Parse the block-declaration. */
11393 cp_parser_block_declaration (parser, /*statement_p=*/true);
11395 /* Free any declarators allocated. */
11396 obstack_free (&declarator_obstack, p);
11399 /* Some dependent statements (like `if (cond) statement'), are
11400 implicitly in their own scope. In other words, if the statement is
11401 a single statement (as opposed to a compound-statement), it is
11402 none-the-less treated as if it were enclosed in braces. Any
11403 declarations appearing in the dependent statement are out of scope
11404 after control passes that point. This function parses a statement,
11405 but ensures that is in its own scope, even if it is not a
11406 compound-statement.
11408 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11409 is a (possibly labeled) if statement which is not enclosed in
11410 braces and has an else clause. This is used to implement
11411 -Wparentheses.
11413 Returns the new statement. */
11415 static tree
11416 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
11417 const token_indent_info &guard_tinfo)
11419 tree statement;
11420 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
11421 token_indent_info body_tinfo
11422 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11424 if (if_p != NULL)
11425 *if_p = false;
11427 /* Mark if () ; with a special NOP_EXPR. */
11428 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11430 cp_lexer_consume_token (parser->lexer);
11431 statement = add_stmt (build_empty_stmt (body_loc));
11433 if (guard_tinfo.keyword == RID_IF
11434 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
11435 warning_at (body_loc, OPT_Wempty_body,
11436 "suggest braces around empty body in an %<if%> statement");
11437 else if (guard_tinfo.keyword == RID_ELSE)
11438 warning_at (body_loc, OPT_Wempty_body,
11439 "suggest braces around empty body in an %<else%> statement");
11441 /* if a compound is opened, we simply parse the statement directly. */
11442 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11443 statement = cp_parser_compound_statement (parser, NULL, false, false);
11444 /* If the token is not a `{', then we must take special action. */
11445 else
11447 /* Create a compound-statement. */
11448 statement = begin_compound_stmt (0);
11449 /* Parse the dependent-statement. */
11450 cp_parser_statement (parser, NULL_TREE, false, if_p);
11451 /* Finish the dummy compound-statement. */
11452 finish_compound_stmt (statement);
11455 token_indent_info next_tinfo
11456 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11457 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
11459 /* Return the statement. */
11460 return statement;
11463 /* For some dependent statements (like `while (cond) statement'), we
11464 have already created a scope. Therefore, even if the dependent
11465 statement is a compound-statement, we do not want to create another
11466 scope. */
11468 static void
11469 cp_parser_already_scoped_statement (cp_parser* parser,
11470 const token_indent_info &guard_tinfo)
11472 /* If the token is a `{', then we must take special action. */
11473 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11475 token_indent_info body_tinfo
11476 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11478 cp_parser_statement (parser, NULL_TREE, false, NULL);
11479 token_indent_info next_tinfo
11480 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11481 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
11483 else
11485 /* Avoid calling cp_parser_compound_statement, so that we
11486 don't create a new scope. Do everything else by hand. */
11487 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11488 /* If the next keyword is `__label__' we have a label declaration. */
11489 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11490 cp_parser_label_declaration (parser);
11491 /* Parse an (optional) statement-seq. */
11492 cp_parser_statement_seq_opt (parser, NULL_TREE);
11493 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11497 /* Declarations [gram.dcl.dcl] */
11499 /* Parse an optional declaration-sequence.
11501 declaration-seq:
11502 declaration
11503 declaration-seq declaration */
11505 static void
11506 cp_parser_declaration_seq_opt (cp_parser* parser)
11508 while (true)
11510 cp_token *token;
11512 token = cp_lexer_peek_token (parser->lexer);
11514 if (token->type == CPP_CLOSE_BRACE
11515 || token->type == CPP_EOF
11516 || token->type == CPP_PRAGMA_EOL)
11517 break;
11519 if (token->type == CPP_SEMICOLON)
11521 /* A declaration consisting of a single semicolon is
11522 invalid. Allow it unless we're being pedantic. */
11523 cp_lexer_consume_token (parser->lexer);
11524 if (!in_system_header_at (input_location))
11525 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11526 continue;
11529 /* If we're entering or exiting a region that's implicitly
11530 extern "C", modify the lang context appropriately. */
11531 if (!parser->implicit_extern_c && token->implicit_extern_c)
11533 push_lang_context (lang_name_c);
11534 parser->implicit_extern_c = true;
11536 else if (parser->implicit_extern_c && !token->implicit_extern_c)
11538 pop_lang_context ();
11539 parser->implicit_extern_c = false;
11542 if (token->type == CPP_PRAGMA)
11544 /* A top-level declaration can consist solely of a #pragma.
11545 A nested declaration cannot, so this is done here and not
11546 in cp_parser_declaration. (A #pragma at block scope is
11547 handled in cp_parser_statement.) */
11548 cp_parser_pragma (parser, pragma_external);
11549 continue;
11552 /* Parse the declaration itself. */
11553 cp_parser_declaration (parser);
11557 /* Parse a declaration.
11559 declaration:
11560 block-declaration
11561 function-definition
11562 template-declaration
11563 explicit-instantiation
11564 explicit-specialization
11565 linkage-specification
11566 namespace-definition
11568 GNU extension:
11570 declaration:
11571 __extension__ declaration */
11573 static void
11574 cp_parser_declaration (cp_parser* parser)
11576 cp_token token1;
11577 cp_token token2;
11578 int saved_pedantic;
11579 void *p;
11580 tree attributes = NULL_TREE;
11582 /* Check for the `__extension__' keyword. */
11583 if (cp_parser_extension_opt (parser, &saved_pedantic))
11585 /* Parse the qualified declaration. */
11586 cp_parser_declaration (parser);
11587 /* Restore the PEDANTIC flag. */
11588 pedantic = saved_pedantic;
11590 return;
11593 /* Try to figure out what kind of declaration is present. */
11594 token1 = *cp_lexer_peek_token (parser->lexer);
11596 if (token1.type != CPP_EOF)
11597 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
11598 else
11600 token2.type = CPP_EOF;
11601 token2.keyword = RID_MAX;
11604 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11605 p = obstack_alloc (&declarator_obstack, 0);
11607 /* If the next token is `extern' and the following token is a string
11608 literal, then we have a linkage specification. */
11609 if (token1.keyword == RID_EXTERN
11610 && cp_parser_is_pure_string_literal (&token2))
11611 cp_parser_linkage_specification (parser);
11612 /* If the next token is `template', then we have either a template
11613 declaration, an explicit instantiation, or an explicit
11614 specialization. */
11615 else if (token1.keyword == RID_TEMPLATE)
11617 /* `template <>' indicates a template specialization. */
11618 if (token2.type == CPP_LESS
11619 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
11620 cp_parser_explicit_specialization (parser);
11621 /* `template <' indicates a template declaration. */
11622 else if (token2.type == CPP_LESS)
11623 cp_parser_template_declaration (parser, /*member_p=*/false);
11624 /* Anything else must be an explicit instantiation. */
11625 else
11626 cp_parser_explicit_instantiation (parser);
11628 /* If the next token is `export', then we have a template
11629 declaration. */
11630 else if (token1.keyword == RID_EXPORT)
11631 cp_parser_template_declaration (parser, /*member_p=*/false);
11632 /* If the next token is `extern', 'static' or 'inline' and the one
11633 after that is `template', we have a GNU extended explicit
11634 instantiation directive. */
11635 else if (cp_parser_allow_gnu_extensions_p (parser)
11636 && (token1.keyword == RID_EXTERN
11637 || token1.keyword == RID_STATIC
11638 || token1.keyword == RID_INLINE)
11639 && token2.keyword == RID_TEMPLATE)
11640 cp_parser_explicit_instantiation (parser);
11641 /* If the next token is `namespace', check for a named or unnamed
11642 namespace definition. */
11643 else if (token1.keyword == RID_NAMESPACE
11644 && (/* A named namespace definition. */
11645 (token2.type == CPP_NAME
11646 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11647 != CPP_EQ))
11648 || (token2.type == CPP_OPEN_SQUARE
11649 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
11650 == CPP_OPEN_SQUARE)
11651 /* An unnamed namespace definition. */
11652 || token2.type == CPP_OPEN_BRACE
11653 || token2.keyword == RID_ATTRIBUTE))
11654 cp_parser_namespace_definition (parser);
11655 /* An inline (associated) namespace definition. */
11656 else if (token1.keyword == RID_INLINE
11657 && token2.keyword == RID_NAMESPACE)
11658 cp_parser_namespace_definition (parser);
11659 /* Objective-C++ declaration/definition. */
11660 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11661 cp_parser_objc_declaration (parser, NULL_TREE);
11662 else if (c_dialect_objc ()
11663 && token1.keyword == RID_ATTRIBUTE
11664 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11665 cp_parser_objc_declaration (parser, attributes);
11666 /* At this point we may have a template declared by a concept
11667 introduction. */
11668 else if (flag_concepts
11669 && cp_parser_template_declaration_after_export (parser,
11670 /*member_p=*/false))
11671 /* We did. */;
11672 else
11673 /* Try to parse a block-declaration, or a function-definition. */
11674 cp_parser_block_declaration (parser, /*statement_p=*/false);
11676 /* Free any declarators allocated. */
11677 obstack_free (&declarator_obstack, p);
11680 /* Parse a block-declaration.
11682 block-declaration:
11683 simple-declaration
11684 asm-definition
11685 namespace-alias-definition
11686 using-declaration
11687 using-directive
11689 GNU Extension:
11691 block-declaration:
11692 __extension__ block-declaration
11694 C++0x Extension:
11696 block-declaration:
11697 static_assert-declaration
11699 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11700 part of a declaration-statement. */
11702 static void
11703 cp_parser_block_declaration (cp_parser *parser,
11704 bool statement_p)
11706 cp_token *token1;
11707 int saved_pedantic;
11709 /* Check for the `__extension__' keyword. */
11710 if (cp_parser_extension_opt (parser, &saved_pedantic))
11712 /* Parse the qualified declaration. */
11713 cp_parser_block_declaration (parser, statement_p);
11714 /* Restore the PEDANTIC flag. */
11715 pedantic = saved_pedantic;
11717 return;
11720 /* Peek at the next token to figure out which kind of declaration is
11721 present. */
11722 token1 = cp_lexer_peek_token (parser->lexer);
11724 /* If the next keyword is `asm', we have an asm-definition. */
11725 if (token1->keyword == RID_ASM)
11727 if (statement_p)
11728 cp_parser_commit_to_tentative_parse (parser);
11729 cp_parser_asm_definition (parser);
11731 /* If the next keyword is `namespace', we have a
11732 namespace-alias-definition. */
11733 else if (token1->keyword == RID_NAMESPACE)
11734 cp_parser_namespace_alias_definition (parser);
11735 /* If the next keyword is `using', we have a
11736 using-declaration, a using-directive, or an alias-declaration. */
11737 else if (token1->keyword == RID_USING)
11739 cp_token *token2;
11741 if (statement_p)
11742 cp_parser_commit_to_tentative_parse (parser);
11743 /* If the token after `using' is `namespace', then we have a
11744 using-directive. */
11745 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11746 if (token2->keyword == RID_NAMESPACE)
11747 cp_parser_using_directive (parser);
11748 /* If the second token after 'using' is '=', then we have an
11749 alias-declaration. */
11750 else if (cxx_dialect >= cxx11
11751 && token2->type == CPP_NAME
11752 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11753 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11754 cp_parser_alias_declaration (parser);
11755 /* Otherwise, it's a using-declaration. */
11756 else
11757 cp_parser_using_declaration (parser,
11758 /*access_declaration_p=*/false);
11760 /* If the next keyword is `__label__' we have a misplaced label
11761 declaration. */
11762 else if (token1->keyword == RID_LABEL)
11764 cp_lexer_consume_token (parser->lexer);
11765 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11766 cp_parser_skip_to_end_of_statement (parser);
11767 /* If the next token is now a `;', consume it. */
11768 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11769 cp_lexer_consume_token (parser->lexer);
11771 /* If the next token is `static_assert' we have a static assertion. */
11772 else if (token1->keyword == RID_STATIC_ASSERT)
11773 cp_parser_static_assert (parser, /*member_p=*/false);
11774 /* Anything else must be a simple-declaration. */
11775 else
11776 cp_parser_simple_declaration (parser, !statement_p,
11777 /*maybe_range_for_decl*/NULL);
11780 /* Parse a simple-declaration.
11782 simple-declaration:
11783 decl-specifier-seq [opt] init-declarator-list [opt] ;
11785 init-declarator-list:
11786 init-declarator
11787 init-declarator-list , init-declarator
11789 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11790 function-definition as a simple-declaration.
11792 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11793 parsed declaration if it is an uninitialized single declarator not followed
11794 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11795 if present, will not be consumed. */
11797 static void
11798 cp_parser_simple_declaration (cp_parser* parser,
11799 bool function_definition_allowed_p,
11800 tree *maybe_range_for_decl)
11802 cp_decl_specifier_seq decl_specifiers;
11803 int declares_class_or_enum;
11804 bool saw_declarator;
11805 location_t comma_loc = UNKNOWN_LOCATION;
11806 location_t init_loc = UNKNOWN_LOCATION;
11808 if (maybe_range_for_decl)
11809 *maybe_range_for_decl = NULL_TREE;
11811 /* Defer access checks until we know what is being declared; the
11812 checks for names appearing in the decl-specifier-seq should be
11813 done as if we were in the scope of the thing being declared. */
11814 push_deferring_access_checks (dk_deferred);
11816 /* Parse the decl-specifier-seq. We have to keep track of whether
11817 or not the decl-specifier-seq declares a named class or
11818 enumeration type, since that is the only case in which the
11819 init-declarator-list is allowed to be empty.
11821 [dcl.dcl]
11823 In a simple-declaration, the optional init-declarator-list can be
11824 omitted only when declaring a class or enumeration, that is when
11825 the decl-specifier-seq contains either a class-specifier, an
11826 elaborated-type-specifier, or an enum-specifier. */
11827 cp_parser_decl_specifier_seq (parser,
11828 CP_PARSER_FLAGS_OPTIONAL,
11829 &decl_specifiers,
11830 &declares_class_or_enum);
11831 /* We no longer need to defer access checks. */
11832 stop_deferring_access_checks ();
11834 /* In a block scope, a valid declaration must always have a
11835 decl-specifier-seq. By not trying to parse declarators, we can
11836 resolve the declaration/expression ambiguity more quickly. */
11837 if (!function_definition_allowed_p
11838 && !decl_specifiers.any_specifiers_p)
11840 cp_parser_error (parser, "expected declaration");
11841 goto done;
11844 /* If the next two tokens are both identifiers, the code is
11845 erroneous. The usual cause of this situation is code like:
11847 T t;
11849 where "T" should name a type -- but does not. */
11850 if (!decl_specifiers.any_type_specifiers_p
11851 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11853 /* If parsing tentatively, we should commit; we really are
11854 looking at a declaration. */
11855 cp_parser_commit_to_tentative_parse (parser);
11856 /* Give up. */
11857 goto done;
11860 /* If we have seen at least one decl-specifier, and the next token
11861 is not a parenthesis, then we must be looking at a declaration.
11862 (After "int (" we might be looking at a functional cast.) */
11863 if (decl_specifiers.any_specifiers_p
11864 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11865 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11866 && !cp_parser_error_occurred (parser))
11867 cp_parser_commit_to_tentative_parse (parser);
11869 /* Keep going until we hit the `;' at the end of the simple
11870 declaration. */
11871 saw_declarator = false;
11872 while (cp_lexer_next_token_is_not (parser->lexer,
11873 CPP_SEMICOLON))
11875 cp_token *token;
11876 bool function_definition_p;
11877 tree decl;
11879 if (saw_declarator)
11881 /* If we are processing next declarator, comma is expected */
11882 token = cp_lexer_peek_token (parser->lexer);
11883 gcc_assert (token->type == CPP_COMMA);
11884 cp_lexer_consume_token (parser->lexer);
11885 if (maybe_range_for_decl)
11887 *maybe_range_for_decl = error_mark_node;
11888 if (comma_loc == UNKNOWN_LOCATION)
11889 comma_loc = token->location;
11892 else
11893 saw_declarator = true;
11895 /* Parse the init-declarator. */
11896 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11897 /*checks=*/NULL,
11898 function_definition_allowed_p,
11899 /*member_p=*/false,
11900 declares_class_or_enum,
11901 &function_definition_p,
11902 maybe_range_for_decl,
11903 &init_loc);
11904 /* If an error occurred while parsing tentatively, exit quickly.
11905 (That usually happens when in the body of a function; each
11906 statement is treated as a declaration-statement until proven
11907 otherwise.) */
11908 if (cp_parser_error_occurred (parser))
11909 goto done;
11910 /* Handle function definitions specially. */
11911 if (function_definition_p)
11913 /* If the next token is a `,', then we are probably
11914 processing something like:
11916 void f() {}, *p;
11918 which is erroneous. */
11919 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11921 cp_token *token = cp_lexer_peek_token (parser->lexer);
11922 error_at (token->location,
11923 "mixing"
11924 " declarations and function-definitions is forbidden");
11926 /* Otherwise, we're done with the list of declarators. */
11927 else
11929 pop_deferring_access_checks ();
11930 return;
11933 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11934 *maybe_range_for_decl = decl;
11935 /* The next token should be either a `,' or a `;'. */
11936 token = cp_lexer_peek_token (parser->lexer);
11937 /* If it's a `,', there are more declarators to come. */
11938 if (token->type == CPP_COMMA)
11939 /* will be consumed next time around */;
11940 /* If it's a `;', we are done. */
11941 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11942 break;
11943 /* Anything else is an error. */
11944 else
11946 /* If we have already issued an error message we don't need
11947 to issue another one. */
11948 if ((decl != error_mark_node
11949 && DECL_INITIAL (decl) != error_mark_node)
11950 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11951 cp_parser_error (parser, "expected %<,%> or %<;%>");
11952 /* Skip tokens until we reach the end of the statement. */
11953 cp_parser_skip_to_end_of_statement (parser);
11954 /* If the next token is now a `;', consume it. */
11955 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11956 cp_lexer_consume_token (parser->lexer);
11957 goto done;
11959 /* After the first time around, a function-definition is not
11960 allowed -- even if it was OK at first. For example:
11962 int i, f() {}
11964 is not valid. */
11965 function_definition_allowed_p = false;
11968 /* Issue an error message if no declarators are present, and the
11969 decl-specifier-seq does not itself declare a class or
11970 enumeration: [dcl.dcl]/3. */
11971 if (!saw_declarator)
11973 if (cp_parser_declares_only_class_p (parser))
11975 if (!declares_class_or_enum
11976 && decl_specifiers.type
11977 && OVERLOAD_TYPE_P (decl_specifiers.type))
11978 /* Ensure an error is issued anyway when finish_decltype_type,
11979 called via cp_parser_decl_specifier_seq, returns a class or
11980 an enumeration (c++/51786). */
11981 decl_specifiers.type = NULL_TREE;
11982 shadow_tag (&decl_specifiers);
11984 /* Perform any deferred access checks. */
11985 perform_deferred_access_checks (tf_warning_or_error);
11988 /* Consume the `;'. */
11989 if (!maybe_range_for_decl)
11990 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11991 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11993 if (init_loc != UNKNOWN_LOCATION)
11994 error_at (init_loc, "initializer in range-based %<for%> loop");
11995 if (comma_loc != UNKNOWN_LOCATION)
11996 error_at (comma_loc,
11997 "multiple declarations in range-based %<for%> loop");
12000 done:
12001 pop_deferring_access_checks ();
12004 /* Parse a decl-specifier-seq.
12006 decl-specifier-seq:
12007 decl-specifier-seq [opt] decl-specifier
12008 decl-specifier attribute-specifier-seq [opt] (C++11)
12010 decl-specifier:
12011 storage-class-specifier
12012 type-specifier
12013 function-specifier
12014 friend
12015 typedef
12017 GNU Extension:
12019 decl-specifier:
12020 attributes
12022 Concepts Extension:
12024 decl-specifier:
12025 concept
12027 Set *DECL_SPECS to a representation of the decl-specifier-seq.
12029 The parser flags FLAGS is used to control type-specifier parsing.
12031 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
12032 flags:
12034 1: one of the decl-specifiers is an elaborated-type-specifier
12035 (i.e., a type declaration)
12036 2: one of the decl-specifiers is an enum-specifier or a
12037 class-specifier (i.e., a type definition)
12041 static void
12042 cp_parser_decl_specifier_seq (cp_parser* parser,
12043 cp_parser_flags flags,
12044 cp_decl_specifier_seq *decl_specs,
12045 int* declares_class_or_enum)
12047 bool constructor_possible_p = !parser->in_declarator_p;
12048 bool found_decl_spec = false;
12049 cp_token *start_token = NULL;
12050 cp_decl_spec ds;
12052 /* Clear DECL_SPECS. */
12053 clear_decl_specs (decl_specs);
12055 /* Assume no class or enumeration type is declared. */
12056 *declares_class_or_enum = 0;
12058 /* Keep reading specifiers until there are no more to read. */
12059 while (true)
12061 bool constructor_p;
12062 cp_token *token;
12063 ds = ds_last;
12065 /* Peek at the next token. */
12066 token = cp_lexer_peek_token (parser->lexer);
12068 /* Save the first token of the decl spec list for error
12069 reporting. */
12070 if (!start_token)
12071 start_token = token;
12072 /* Handle attributes. */
12073 if (cp_next_tokens_can_be_attribute_p (parser))
12075 /* Parse the attributes. */
12076 tree attrs = cp_parser_attributes_opt (parser);
12078 /* In a sequence of declaration specifiers, c++11 attributes
12079 appertain to the type that precede them. In that case
12080 [dcl.spec]/1 says:
12082 The attribute-specifier-seq affects the type only for
12083 the declaration it appears in, not other declarations
12084 involving the same type.
12086 But for now let's force the user to position the
12087 attribute either at the beginning of the declaration or
12088 after the declarator-id, which would clearly mean that it
12089 applies to the declarator. */
12090 if (cxx11_attribute_p (attrs))
12092 if (!found_decl_spec)
12093 /* The c++11 attribute is at the beginning of the
12094 declaration. It appertains to the entity being
12095 declared. */;
12096 else
12098 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
12100 /* This is an attribute following a
12101 class-specifier. */
12102 if (decl_specs->type_definition_p)
12103 warn_misplaced_attr_for_class_type (token->location,
12104 decl_specs->type);
12105 attrs = NULL_TREE;
12107 else
12109 decl_specs->std_attributes
12110 = chainon (decl_specs->std_attributes,
12111 attrs);
12112 if (decl_specs->locations[ds_std_attribute] == 0)
12113 decl_specs->locations[ds_std_attribute] = token->location;
12115 continue;
12119 decl_specs->attributes
12120 = chainon (decl_specs->attributes,
12121 attrs);
12122 if (decl_specs->locations[ds_attribute] == 0)
12123 decl_specs->locations[ds_attribute] = token->location;
12124 continue;
12126 /* Assume we will find a decl-specifier keyword. */
12127 found_decl_spec = true;
12128 /* If the next token is an appropriate keyword, we can simply
12129 add it to the list. */
12130 switch (token->keyword)
12132 /* decl-specifier:
12133 friend
12134 constexpr */
12135 case RID_FRIEND:
12136 if (!at_class_scope_p ())
12138 error_at (token->location, "%<friend%> used outside of class");
12139 cp_lexer_purge_token (parser->lexer);
12141 else
12143 ds = ds_friend;
12144 /* Consume the token. */
12145 cp_lexer_consume_token (parser->lexer);
12147 break;
12149 case RID_CONSTEXPR:
12150 ds = ds_constexpr;
12151 cp_lexer_consume_token (parser->lexer);
12152 break;
12154 case RID_CONCEPT:
12155 ds = ds_concept;
12156 cp_lexer_consume_token (parser->lexer);
12157 break;
12159 /* function-specifier:
12160 inline
12161 virtual
12162 explicit */
12163 case RID_INLINE:
12164 case RID_VIRTUAL:
12165 case RID_EXPLICIT:
12166 cp_parser_function_specifier_opt (parser, decl_specs);
12167 break;
12169 /* decl-specifier:
12170 typedef */
12171 case RID_TYPEDEF:
12172 ds = ds_typedef;
12173 /* Consume the token. */
12174 cp_lexer_consume_token (parser->lexer);
12175 /* A constructor declarator cannot appear in a typedef. */
12176 constructor_possible_p = false;
12177 /* The "typedef" keyword can only occur in a declaration; we
12178 may as well commit at this point. */
12179 cp_parser_commit_to_tentative_parse (parser);
12181 if (decl_specs->storage_class != sc_none)
12182 decl_specs->conflicting_specifiers_p = true;
12183 break;
12185 /* storage-class-specifier:
12186 auto
12187 register
12188 static
12189 extern
12190 mutable
12192 GNU Extension:
12193 thread */
12194 case RID_AUTO:
12195 if (cxx_dialect == cxx98)
12197 /* Consume the token. */
12198 cp_lexer_consume_token (parser->lexer);
12200 /* Complain about `auto' as a storage specifier, if
12201 we're complaining about C++0x compatibility. */
12202 warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
12203 " changes meaning in C++11; please remove it");
12205 /* Set the storage class anyway. */
12206 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
12207 token);
12209 else
12210 /* C++0x auto type-specifier. */
12211 found_decl_spec = false;
12212 break;
12214 case RID_REGISTER:
12215 case RID_STATIC:
12216 case RID_EXTERN:
12217 case RID_MUTABLE:
12218 /* Consume the token. */
12219 cp_lexer_consume_token (parser->lexer);
12220 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
12221 token);
12222 break;
12223 case RID_THREAD:
12224 /* Consume the token. */
12225 ds = ds_thread;
12226 cp_lexer_consume_token (parser->lexer);
12227 break;
12229 default:
12230 /* We did not yet find a decl-specifier yet. */
12231 found_decl_spec = false;
12232 break;
12235 if (found_decl_spec
12236 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
12237 && token->keyword != RID_CONSTEXPR)
12238 error ("decl-specifier invalid in condition");
12240 if (ds != ds_last)
12241 set_and_check_decl_spec_loc (decl_specs, ds, token);
12243 /* Constructors are a special case. The `S' in `S()' is not a
12244 decl-specifier; it is the beginning of the declarator. */
12245 constructor_p
12246 = (!found_decl_spec
12247 && constructor_possible_p
12248 && (cp_parser_constructor_declarator_p
12249 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
12251 /* If we don't have a DECL_SPEC yet, then we must be looking at
12252 a type-specifier. */
12253 if (!found_decl_spec && !constructor_p)
12255 int decl_spec_declares_class_or_enum;
12256 bool is_cv_qualifier;
12257 tree type_spec;
12259 type_spec
12260 = cp_parser_type_specifier (parser, flags,
12261 decl_specs,
12262 /*is_declaration=*/true,
12263 &decl_spec_declares_class_or_enum,
12264 &is_cv_qualifier);
12265 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
12267 /* If this type-specifier referenced a user-defined type
12268 (a typedef, class-name, etc.), then we can't allow any
12269 more such type-specifiers henceforth.
12271 [dcl.spec]
12273 The longest sequence of decl-specifiers that could
12274 possibly be a type name is taken as the
12275 decl-specifier-seq of a declaration. The sequence shall
12276 be self-consistent as described below.
12278 [dcl.type]
12280 As a general rule, at most one type-specifier is allowed
12281 in the complete decl-specifier-seq of a declaration. The
12282 only exceptions are the following:
12284 -- const or volatile can be combined with any other
12285 type-specifier.
12287 -- signed or unsigned can be combined with char, long,
12288 short, or int.
12290 -- ..
12292 Example:
12294 typedef char* Pc;
12295 void g (const int Pc);
12297 Here, Pc is *not* part of the decl-specifier seq; it's
12298 the declarator. Therefore, once we see a type-specifier
12299 (other than a cv-qualifier), we forbid any additional
12300 user-defined types. We *do* still allow things like `int
12301 int' to be considered a decl-specifier-seq, and issue the
12302 error message later. */
12303 if (type_spec && !is_cv_qualifier)
12304 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12305 /* A constructor declarator cannot follow a type-specifier. */
12306 if (type_spec)
12308 constructor_possible_p = false;
12309 found_decl_spec = true;
12310 if (!is_cv_qualifier)
12311 decl_specs->any_type_specifiers_p = true;
12315 /* If we still do not have a DECL_SPEC, then there are no more
12316 decl-specifiers. */
12317 if (!found_decl_spec)
12318 break;
12320 decl_specs->any_specifiers_p = true;
12321 /* After we see one decl-specifier, further decl-specifiers are
12322 always optional. */
12323 flags |= CP_PARSER_FLAGS_OPTIONAL;
12326 /* Don't allow a friend specifier with a class definition. */
12327 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
12328 && (*declares_class_or_enum & 2))
12329 error_at (decl_specs->locations[ds_friend],
12330 "class definition may not be declared a friend");
12333 /* Parse an (optional) storage-class-specifier.
12335 storage-class-specifier:
12336 auto
12337 register
12338 static
12339 extern
12340 mutable
12342 GNU Extension:
12344 storage-class-specifier:
12345 thread
12347 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
12349 static tree
12350 cp_parser_storage_class_specifier_opt (cp_parser* parser)
12352 switch (cp_lexer_peek_token (parser->lexer)->keyword)
12354 case RID_AUTO:
12355 if (cxx_dialect != cxx98)
12356 return NULL_TREE;
12357 /* Fall through for C++98. */
12359 case RID_REGISTER:
12360 case RID_STATIC:
12361 case RID_EXTERN:
12362 case RID_MUTABLE:
12363 case RID_THREAD:
12364 /* Consume the token. */
12365 return cp_lexer_consume_token (parser->lexer)->u.value;
12367 default:
12368 return NULL_TREE;
12372 /* Parse an (optional) function-specifier.
12374 function-specifier:
12375 inline
12376 virtual
12377 explicit
12379 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12380 Updates DECL_SPECS, if it is non-NULL. */
12382 static tree
12383 cp_parser_function_specifier_opt (cp_parser* parser,
12384 cp_decl_specifier_seq *decl_specs)
12386 cp_token *token = cp_lexer_peek_token (parser->lexer);
12387 switch (token->keyword)
12389 case RID_INLINE:
12390 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12391 break;
12393 case RID_VIRTUAL:
12394 /* 14.5.2.3 [temp.mem]
12396 A member function template shall not be virtual. */
12397 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12398 error_at (token->location, "templates may not be %<virtual%>");
12399 else
12400 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12401 break;
12403 case RID_EXPLICIT:
12404 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12405 break;
12407 default:
12408 return NULL_TREE;
12411 /* Consume the token. */
12412 return cp_lexer_consume_token (parser->lexer)->u.value;
12415 /* Parse a linkage-specification.
12417 linkage-specification:
12418 extern string-literal { declaration-seq [opt] }
12419 extern string-literal declaration */
12421 static void
12422 cp_parser_linkage_specification (cp_parser* parser)
12424 tree linkage;
12426 /* Look for the `extern' keyword. */
12427 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12429 /* Look for the string-literal. */
12430 linkage = cp_parser_string_literal (parser, false, false);
12432 /* Transform the literal into an identifier. If the literal is a
12433 wide-character string, or contains embedded NULs, then we can't
12434 handle it as the user wants. */
12435 if (strlen (TREE_STRING_POINTER (linkage))
12436 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12438 cp_parser_error (parser, "invalid linkage-specification");
12439 /* Assume C++ linkage. */
12440 linkage = lang_name_cplusplus;
12442 else
12443 linkage = get_identifier (TREE_STRING_POINTER (linkage));
12445 /* We're now using the new linkage. */
12446 push_lang_context (linkage);
12448 /* If the next token is a `{', then we're using the first
12449 production. */
12450 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12452 cp_ensure_no_omp_declare_simd (parser);
12454 /* Consume the `{' token. */
12455 cp_lexer_consume_token (parser->lexer);
12456 /* Parse the declarations. */
12457 cp_parser_declaration_seq_opt (parser);
12458 /* Look for the closing `}'. */
12459 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12461 /* Otherwise, there's just one declaration. */
12462 else
12464 bool saved_in_unbraced_linkage_specification_p;
12466 saved_in_unbraced_linkage_specification_p
12467 = parser->in_unbraced_linkage_specification_p;
12468 parser->in_unbraced_linkage_specification_p = true;
12469 cp_parser_declaration (parser);
12470 parser->in_unbraced_linkage_specification_p
12471 = saved_in_unbraced_linkage_specification_p;
12474 /* We're done with the linkage-specification. */
12475 pop_lang_context ();
12478 /* Parse a static_assert-declaration.
12480 static_assert-declaration:
12481 static_assert ( constant-expression , string-literal ) ;
12482 static_assert ( constant-expression ) ; (C++1Z)
12484 If MEMBER_P, this static_assert is a class member. */
12486 static void
12487 cp_parser_static_assert(cp_parser *parser, bool member_p)
12489 tree condition;
12490 tree message;
12491 cp_token *token;
12492 location_t saved_loc;
12493 bool dummy;
12495 /* Peek at the `static_assert' token so we can keep track of exactly
12496 where the static assertion started. */
12497 token = cp_lexer_peek_token (parser->lexer);
12498 saved_loc = token->location;
12500 /* Look for the `static_assert' keyword. */
12501 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
12502 RT_STATIC_ASSERT))
12503 return;
12505 /* We know we are in a static assertion; commit to any tentative
12506 parse. */
12507 if (cp_parser_parsing_tentatively (parser))
12508 cp_parser_commit_to_tentative_parse (parser);
12510 /* Parse the `(' starting the static assertion condition. */
12511 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12513 /* Parse the constant-expression. Allow a non-constant expression
12514 here in order to give better diagnostics in finish_static_assert. */
12515 condition =
12516 cp_parser_constant_expression (parser,
12517 /*allow_non_constant_p=*/true,
12518 /*non_constant_p=*/&dummy);
12520 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12522 if (cxx_dialect < cxx1z)
12523 pedwarn (input_location, OPT_Wpedantic,
12524 "static_assert without a message "
12525 "only available with -std=c++1z or -std=gnu++1z");
12526 /* Eat the ')' */
12527 cp_lexer_consume_token (parser->lexer);
12528 message = build_string (1, "");
12529 TREE_TYPE (message) = char_array_type_node;
12530 fix_string_type (message);
12532 else
12534 /* Parse the separating `,'. */
12535 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
12537 /* Parse the string-literal message. */
12538 message = cp_parser_string_literal (parser,
12539 /*translate=*/false,
12540 /*wide_ok=*/true);
12542 /* A `)' completes the static assertion. */
12543 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12544 cp_parser_skip_to_closing_parenthesis (parser,
12545 /*recovering=*/true,
12546 /*or_comma=*/false,
12547 /*consume_paren=*/true);
12550 /* A semicolon terminates the declaration. */
12551 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12553 /* Complete the static assertion, which may mean either processing
12554 the static assert now or saving it for template instantiation. */
12555 finish_static_assert (condition, message, saved_loc, member_p);
12558 /* Parse the expression in decltype ( expression ). */
12560 static tree
12561 cp_parser_decltype_expr (cp_parser *parser,
12562 bool &id_expression_or_member_access_p)
12564 cp_token *id_expr_start_token;
12565 tree expr;
12567 /* First, try parsing an id-expression. */
12568 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
12569 cp_parser_parse_tentatively (parser);
12570 expr = cp_parser_id_expression (parser,
12571 /*template_keyword_p=*/false,
12572 /*check_dependency_p=*/true,
12573 /*template_p=*/NULL,
12574 /*declarator_p=*/false,
12575 /*optional_p=*/false);
12577 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
12579 bool non_integral_constant_expression_p = false;
12580 tree id_expression = expr;
12581 cp_id_kind idk;
12582 const char *error_msg;
12584 if (identifier_p (expr))
12585 /* Lookup the name we got back from the id-expression. */
12586 expr = cp_parser_lookup_name_simple (parser, expr,
12587 id_expr_start_token->location);
12589 if (expr
12590 && expr != error_mark_node
12591 && TREE_CODE (expr) != TYPE_DECL
12592 && (TREE_CODE (expr) != BIT_NOT_EXPR
12593 || !TYPE_P (TREE_OPERAND (expr, 0)))
12594 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12596 /* Complete lookup of the id-expression. */
12597 expr = (finish_id_expression
12598 (id_expression, expr, parser->scope, &idk,
12599 /*integral_constant_expression_p=*/false,
12600 /*allow_non_integral_constant_expression_p=*/true,
12601 &non_integral_constant_expression_p,
12602 /*template_p=*/false,
12603 /*done=*/true,
12604 /*address_p=*/false,
12605 /*template_arg_p=*/false,
12606 &error_msg,
12607 id_expr_start_token->location));
12609 if (expr == error_mark_node)
12610 /* We found an id-expression, but it was something that we
12611 should not have found. This is an error, not something
12612 we can recover from, so note that we found an
12613 id-expression and we'll recover as gracefully as
12614 possible. */
12615 id_expression_or_member_access_p = true;
12618 if (expr
12619 && expr != error_mark_node
12620 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12621 /* We have an id-expression. */
12622 id_expression_or_member_access_p = true;
12625 if (!id_expression_or_member_access_p)
12627 /* Abort the id-expression parse. */
12628 cp_parser_abort_tentative_parse (parser);
12630 /* Parsing tentatively, again. */
12631 cp_parser_parse_tentatively (parser);
12633 /* Parse a class member access. */
12634 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
12635 /*cast_p=*/false, /*decltype*/true,
12636 /*member_access_only_p=*/true, NULL);
12638 if (expr
12639 && expr != error_mark_node
12640 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12641 /* We have an id-expression. */
12642 id_expression_or_member_access_p = true;
12645 if (id_expression_or_member_access_p)
12646 /* We have parsed the complete id-expression or member access. */
12647 cp_parser_parse_definitely (parser);
12648 else
12650 /* Abort our attempt to parse an id-expression or member access
12651 expression. */
12652 cp_parser_abort_tentative_parse (parser);
12654 /* Parse a full expression. */
12655 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
12656 /*decltype_p=*/true);
12659 return expr;
12662 /* Parse a `decltype' type. Returns the type.
12664 simple-type-specifier:
12665 decltype ( expression )
12666 C++14 proposal:
12667 decltype ( auto ) */
12669 static tree
12670 cp_parser_decltype (cp_parser *parser)
12672 tree expr;
12673 bool id_expression_or_member_access_p = false;
12674 const char *saved_message;
12675 bool saved_integral_constant_expression_p;
12676 bool saved_non_integral_constant_expression_p;
12677 bool saved_greater_than_is_operator_p;
12678 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12680 if (start_token->type == CPP_DECLTYPE)
12682 /* Already parsed. */
12683 cp_lexer_consume_token (parser->lexer);
12684 return start_token->u.value;
12687 /* Look for the `decltype' token. */
12688 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12689 return error_mark_node;
12691 /* Parse the opening `('. */
12692 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12693 return error_mark_node;
12695 /* decltype (auto) */
12696 if (cxx_dialect >= cxx14
12697 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12699 cp_lexer_consume_token (parser->lexer);
12700 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12701 return error_mark_node;
12702 expr = make_decltype_auto ();
12703 AUTO_IS_DECLTYPE (expr) = true;
12704 goto rewrite;
12707 /* Types cannot be defined in a `decltype' expression. Save away the
12708 old message. */
12709 saved_message = parser->type_definition_forbidden_message;
12711 /* And create the new one. */
12712 parser->type_definition_forbidden_message
12713 = G_("types may not be defined in %<decltype%> expressions");
12715 /* The restrictions on constant-expressions do not apply inside
12716 decltype expressions. */
12717 saved_integral_constant_expression_p
12718 = parser->integral_constant_expression_p;
12719 saved_non_integral_constant_expression_p
12720 = parser->non_integral_constant_expression_p;
12721 parser->integral_constant_expression_p = false;
12723 /* Within a parenthesized expression, a `>' token is always
12724 the greater-than operator. */
12725 saved_greater_than_is_operator_p
12726 = parser->greater_than_is_operator_p;
12727 parser->greater_than_is_operator_p = true;
12729 /* Do not actually evaluate the expression. */
12730 ++cp_unevaluated_operand;
12732 /* Do not warn about problems with the expression. */
12733 ++c_inhibit_evaluation_warnings;
12735 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12737 /* Go back to evaluating expressions. */
12738 --cp_unevaluated_operand;
12739 --c_inhibit_evaluation_warnings;
12741 /* The `>' token might be the end of a template-id or
12742 template-parameter-list now. */
12743 parser->greater_than_is_operator_p
12744 = saved_greater_than_is_operator_p;
12746 /* Restore the old message and the integral constant expression
12747 flags. */
12748 parser->type_definition_forbidden_message = saved_message;
12749 parser->integral_constant_expression_p
12750 = saved_integral_constant_expression_p;
12751 parser->non_integral_constant_expression_p
12752 = saved_non_integral_constant_expression_p;
12754 /* Parse to the closing `)'. */
12755 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12757 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12758 /*consume_paren=*/true);
12759 return error_mark_node;
12762 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12763 tf_warning_or_error);
12765 rewrite:
12766 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12767 it again. */
12768 start_token->type = CPP_DECLTYPE;
12769 start_token->u.value = expr;
12770 start_token->keyword = RID_MAX;
12771 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12773 return expr;
12776 /* Special member functions [gram.special] */
12778 /* Parse a conversion-function-id.
12780 conversion-function-id:
12781 operator conversion-type-id
12783 Returns an IDENTIFIER_NODE representing the operator. */
12785 static tree
12786 cp_parser_conversion_function_id (cp_parser* parser)
12788 tree type;
12789 tree saved_scope;
12790 tree saved_qualifying_scope;
12791 tree saved_object_scope;
12792 tree pushed_scope = NULL_TREE;
12794 /* Look for the `operator' token. */
12795 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12796 return error_mark_node;
12797 /* When we parse the conversion-type-id, the current scope will be
12798 reset. However, we need that information in able to look up the
12799 conversion function later, so we save it here. */
12800 saved_scope = parser->scope;
12801 saved_qualifying_scope = parser->qualifying_scope;
12802 saved_object_scope = parser->object_scope;
12803 /* We must enter the scope of the class so that the names of
12804 entities declared within the class are available in the
12805 conversion-type-id. For example, consider:
12807 struct S {
12808 typedef int I;
12809 operator I();
12812 S::operator I() { ... }
12814 In order to see that `I' is a type-name in the definition, we
12815 must be in the scope of `S'. */
12816 if (saved_scope)
12817 pushed_scope = push_scope (saved_scope);
12818 /* Parse the conversion-type-id. */
12819 type = cp_parser_conversion_type_id (parser);
12820 /* Leave the scope of the class, if any. */
12821 if (pushed_scope)
12822 pop_scope (pushed_scope);
12823 /* Restore the saved scope. */
12824 parser->scope = saved_scope;
12825 parser->qualifying_scope = saved_qualifying_scope;
12826 parser->object_scope = saved_object_scope;
12827 /* If the TYPE is invalid, indicate failure. */
12828 if (type == error_mark_node)
12829 return error_mark_node;
12830 return mangle_conv_op_name_for_type (type);
12833 /* Parse a conversion-type-id:
12835 conversion-type-id:
12836 type-specifier-seq conversion-declarator [opt]
12838 Returns the TYPE specified. */
12840 static tree
12841 cp_parser_conversion_type_id (cp_parser* parser)
12843 tree attributes;
12844 cp_decl_specifier_seq type_specifiers;
12845 cp_declarator *declarator;
12846 tree type_specified;
12847 const char *saved_message;
12849 /* Parse the attributes. */
12850 attributes = cp_parser_attributes_opt (parser);
12852 saved_message = parser->type_definition_forbidden_message;
12853 parser->type_definition_forbidden_message
12854 = G_("types may not be defined in a conversion-type-id");
12856 /* Parse the type-specifiers. */
12857 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12858 /*is_trailing_return=*/false,
12859 &type_specifiers);
12861 parser->type_definition_forbidden_message = saved_message;
12863 /* If that didn't work, stop. */
12864 if (type_specifiers.type == error_mark_node)
12865 return error_mark_node;
12866 /* Parse the conversion-declarator. */
12867 declarator = cp_parser_conversion_declarator_opt (parser);
12869 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12870 /*initialized=*/0, &attributes);
12871 if (attributes)
12872 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12874 /* Don't give this error when parsing tentatively. This happens to
12875 work because we always parse this definitively once. */
12876 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12877 && type_uses_auto (type_specified))
12879 if (cxx_dialect < cxx14)
12881 error ("invalid use of %<auto%> in conversion operator");
12882 return error_mark_node;
12884 else if (template_parm_scope_p ())
12885 warning (0, "use of %<auto%> in member template "
12886 "conversion operator can never be deduced");
12889 return type_specified;
12892 /* Parse an (optional) conversion-declarator.
12894 conversion-declarator:
12895 ptr-operator conversion-declarator [opt]
12899 static cp_declarator *
12900 cp_parser_conversion_declarator_opt (cp_parser* parser)
12902 enum tree_code code;
12903 tree class_type, std_attributes = NULL_TREE;
12904 cp_cv_quals cv_quals;
12906 /* We don't know if there's a ptr-operator next, or not. */
12907 cp_parser_parse_tentatively (parser);
12908 /* Try the ptr-operator. */
12909 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12910 &std_attributes);
12911 /* If it worked, look for more conversion-declarators. */
12912 if (cp_parser_parse_definitely (parser))
12914 cp_declarator *declarator;
12916 /* Parse another optional declarator. */
12917 declarator = cp_parser_conversion_declarator_opt (parser);
12919 declarator = cp_parser_make_indirect_declarator
12920 (code, class_type, cv_quals, declarator, std_attributes);
12922 return declarator;
12925 return NULL;
12928 /* Parse an (optional) ctor-initializer.
12930 ctor-initializer:
12931 : mem-initializer-list
12933 Returns TRUE iff the ctor-initializer was actually present. */
12935 static bool
12936 cp_parser_ctor_initializer_opt (cp_parser* parser)
12938 /* If the next token is not a `:', then there is no
12939 ctor-initializer. */
12940 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12942 /* Do default initialization of any bases and members. */
12943 if (DECL_CONSTRUCTOR_P (current_function_decl))
12944 finish_mem_initializers (NULL_TREE);
12946 return false;
12949 /* Consume the `:' token. */
12950 cp_lexer_consume_token (parser->lexer);
12951 /* And the mem-initializer-list. */
12952 cp_parser_mem_initializer_list (parser);
12954 return true;
12957 /* Parse a mem-initializer-list.
12959 mem-initializer-list:
12960 mem-initializer ... [opt]
12961 mem-initializer ... [opt] , mem-initializer-list */
12963 static void
12964 cp_parser_mem_initializer_list (cp_parser* parser)
12966 tree mem_initializer_list = NULL_TREE;
12967 tree target_ctor = error_mark_node;
12968 cp_token *token = cp_lexer_peek_token (parser->lexer);
12970 /* Let the semantic analysis code know that we are starting the
12971 mem-initializer-list. */
12972 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12973 error_at (token->location,
12974 "only constructors take member initializers");
12976 /* Loop through the list. */
12977 while (true)
12979 tree mem_initializer;
12981 token = cp_lexer_peek_token (parser->lexer);
12982 /* Parse the mem-initializer. */
12983 mem_initializer = cp_parser_mem_initializer (parser);
12984 /* If the next token is a `...', we're expanding member initializers. */
12985 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12987 /* Consume the `...'. */
12988 cp_lexer_consume_token (parser->lexer);
12990 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12991 can be expanded but members cannot. */
12992 if (mem_initializer != error_mark_node
12993 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12995 error_at (token->location,
12996 "cannot expand initializer for member %<%D%>",
12997 TREE_PURPOSE (mem_initializer));
12998 mem_initializer = error_mark_node;
13001 /* Construct the pack expansion type. */
13002 if (mem_initializer != error_mark_node)
13003 mem_initializer = make_pack_expansion (mem_initializer);
13005 if (target_ctor != error_mark_node
13006 && mem_initializer != error_mark_node)
13008 error ("mem-initializer for %qD follows constructor delegation",
13009 TREE_PURPOSE (mem_initializer));
13010 mem_initializer = error_mark_node;
13012 /* Look for a target constructor. */
13013 if (mem_initializer != error_mark_node
13014 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
13015 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
13017 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
13018 if (mem_initializer_list)
13020 error ("constructor delegation follows mem-initializer for %qD",
13021 TREE_PURPOSE (mem_initializer_list));
13022 mem_initializer = error_mark_node;
13024 target_ctor = mem_initializer;
13026 /* Add it to the list, unless it was erroneous. */
13027 if (mem_initializer != error_mark_node)
13029 TREE_CHAIN (mem_initializer) = mem_initializer_list;
13030 mem_initializer_list = mem_initializer;
13032 /* If the next token is not a `,', we're done. */
13033 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13034 break;
13035 /* Consume the `,' token. */
13036 cp_lexer_consume_token (parser->lexer);
13039 /* Perform semantic analysis. */
13040 if (DECL_CONSTRUCTOR_P (current_function_decl))
13041 finish_mem_initializers (mem_initializer_list);
13044 /* Parse a mem-initializer.
13046 mem-initializer:
13047 mem-initializer-id ( expression-list [opt] )
13048 mem-initializer-id braced-init-list
13050 GNU extension:
13052 mem-initializer:
13053 ( expression-list [opt] )
13055 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
13056 class) or FIELD_DECL (for a non-static data member) to initialize;
13057 the TREE_VALUE is the expression-list. An empty initialization
13058 list is represented by void_list_node. */
13060 static tree
13061 cp_parser_mem_initializer (cp_parser* parser)
13063 tree mem_initializer_id;
13064 tree expression_list;
13065 tree member;
13066 cp_token *token = cp_lexer_peek_token (parser->lexer);
13068 /* Find out what is being initialized. */
13069 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
13071 permerror (token->location,
13072 "anachronistic old-style base class initializer");
13073 mem_initializer_id = NULL_TREE;
13075 else
13077 mem_initializer_id = cp_parser_mem_initializer_id (parser);
13078 if (mem_initializer_id == error_mark_node)
13079 return mem_initializer_id;
13081 member = expand_member_init (mem_initializer_id);
13082 if (member && !DECL_P (member))
13083 in_base_initializer = 1;
13085 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13087 bool expr_non_constant_p;
13088 cp_lexer_set_source_position (parser->lexer);
13089 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13090 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
13091 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
13092 expression_list = build_tree_list (NULL_TREE, expression_list);
13094 else
13096 vec<tree, va_gc> *vec;
13097 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
13098 /*cast_p=*/false,
13099 /*allow_expansion_p=*/true,
13100 /*non_constant_p=*/NULL);
13101 if (vec == NULL)
13102 return error_mark_node;
13103 expression_list = build_tree_list_vec (vec);
13104 release_tree_vector (vec);
13107 if (expression_list == error_mark_node)
13108 return error_mark_node;
13109 if (!expression_list)
13110 expression_list = void_type_node;
13112 in_base_initializer = 0;
13114 return member ? build_tree_list (member, expression_list) : error_mark_node;
13117 /* Parse a mem-initializer-id.
13119 mem-initializer-id:
13120 :: [opt] nested-name-specifier [opt] class-name
13121 decltype-specifier (C++11)
13122 identifier
13124 Returns a TYPE indicating the class to be initialized for the first
13125 production (and the second in C++11). Returns an IDENTIFIER_NODE
13126 indicating the data member to be initialized for the last production. */
13128 static tree
13129 cp_parser_mem_initializer_id (cp_parser* parser)
13131 bool global_scope_p;
13132 bool nested_name_specifier_p;
13133 bool template_p = false;
13134 tree id;
13136 cp_token *token = cp_lexer_peek_token (parser->lexer);
13138 /* `typename' is not allowed in this context ([temp.res]). */
13139 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13141 error_at (token->location,
13142 "keyword %<typename%> not allowed in this context (a qualified "
13143 "member initializer is implicitly a type)");
13144 cp_lexer_consume_token (parser->lexer);
13146 /* Look for the optional `::' operator. */
13147 global_scope_p
13148 = (cp_parser_global_scope_opt (parser,
13149 /*current_scope_valid_p=*/false)
13150 != NULL_TREE);
13151 /* Look for the optional nested-name-specifier. The simplest way to
13152 implement:
13154 [temp.res]
13156 The keyword `typename' is not permitted in a base-specifier or
13157 mem-initializer; in these contexts a qualified name that
13158 depends on a template-parameter is implicitly assumed to be a
13159 type name.
13161 is to assume that we have seen the `typename' keyword at this
13162 point. */
13163 nested_name_specifier_p
13164 = (cp_parser_nested_name_specifier_opt (parser,
13165 /*typename_keyword_p=*/true,
13166 /*check_dependency_p=*/true,
13167 /*type_p=*/true,
13168 /*is_declaration=*/true)
13169 != NULL_TREE);
13170 if (nested_name_specifier_p)
13171 template_p = cp_parser_optional_template_keyword (parser);
13172 /* If there is a `::' operator or a nested-name-specifier, then we
13173 are definitely looking for a class-name. */
13174 if (global_scope_p || nested_name_specifier_p)
13175 return cp_parser_class_name (parser,
13176 /*typename_keyword_p=*/true,
13177 /*template_keyword_p=*/template_p,
13178 typename_type,
13179 /*check_dependency_p=*/true,
13180 /*class_head_p=*/false,
13181 /*is_declaration=*/true);
13182 /* Otherwise, we could also be looking for an ordinary identifier. */
13183 cp_parser_parse_tentatively (parser);
13184 if (cp_lexer_next_token_is_decltype (parser->lexer))
13185 /* Try a decltype-specifier. */
13186 id = cp_parser_decltype (parser);
13187 else
13188 /* Otherwise, try a class-name. */
13189 id = cp_parser_class_name (parser,
13190 /*typename_keyword_p=*/true,
13191 /*template_keyword_p=*/false,
13192 none_type,
13193 /*check_dependency_p=*/true,
13194 /*class_head_p=*/false,
13195 /*is_declaration=*/true);
13196 /* If we found one, we're done. */
13197 if (cp_parser_parse_definitely (parser))
13198 return id;
13199 /* Otherwise, look for an ordinary identifier. */
13200 return cp_parser_identifier (parser);
13203 /* Overloading [gram.over] */
13205 /* Parse an operator-function-id.
13207 operator-function-id:
13208 operator operator
13210 Returns an IDENTIFIER_NODE for the operator which is a
13211 human-readable spelling of the identifier, e.g., `operator +'. */
13213 static tree
13214 cp_parser_operator_function_id (cp_parser* parser)
13216 /* Look for the `operator' keyword. */
13217 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13218 return error_mark_node;
13219 /* And then the name of the operator itself. */
13220 return cp_parser_operator (parser);
13223 /* Return an identifier node for a user-defined literal operator.
13224 The suffix identifier is chained to the operator name identifier. */
13226 static tree
13227 cp_literal_operator_id (const char* name)
13229 tree identifier;
13230 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
13231 + strlen (name) + 10);
13232 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
13233 identifier = get_identifier (buffer);
13235 return identifier;
13238 /* Parse an operator.
13240 operator:
13241 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
13242 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
13243 || ++ -- , ->* -> () []
13245 GNU Extensions:
13247 operator:
13248 <? >? <?= >?=
13250 Returns an IDENTIFIER_NODE for the operator which is a
13251 human-readable spelling of the identifier, e.g., `operator +'. */
13253 static tree
13254 cp_parser_operator (cp_parser* parser)
13256 tree id = NULL_TREE;
13257 cp_token *token;
13258 bool utf8 = false;
13260 /* Peek at the next token. */
13261 token = cp_lexer_peek_token (parser->lexer);
13262 /* Figure out which operator we have. */
13263 switch (token->type)
13265 case CPP_KEYWORD:
13267 enum tree_code op;
13269 /* The keyword should be either `new' or `delete'. */
13270 if (token->keyword == RID_NEW)
13271 op = NEW_EXPR;
13272 else if (token->keyword == RID_DELETE)
13273 op = DELETE_EXPR;
13274 else
13275 break;
13277 /* Consume the `new' or `delete' token. */
13278 cp_lexer_consume_token (parser->lexer);
13280 /* Peek at the next token. */
13281 token = cp_lexer_peek_token (parser->lexer);
13282 /* If it's a `[' token then this is the array variant of the
13283 operator. */
13284 if (token->type == CPP_OPEN_SQUARE)
13286 /* Consume the `[' token. */
13287 cp_lexer_consume_token (parser->lexer);
13288 /* Look for the `]' token. */
13289 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13290 id = ansi_opname (op == NEW_EXPR
13291 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
13293 /* Otherwise, we have the non-array variant. */
13294 else
13295 id = ansi_opname (op);
13297 return id;
13300 case CPP_PLUS:
13301 id = ansi_opname (PLUS_EXPR);
13302 break;
13304 case CPP_MINUS:
13305 id = ansi_opname (MINUS_EXPR);
13306 break;
13308 case CPP_MULT:
13309 id = ansi_opname (MULT_EXPR);
13310 break;
13312 case CPP_DIV:
13313 id = ansi_opname (TRUNC_DIV_EXPR);
13314 break;
13316 case CPP_MOD:
13317 id = ansi_opname (TRUNC_MOD_EXPR);
13318 break;
13320 case CPP_XOR:
13321 id = ansi_opname (BIT_XOR_EXPR);
13322 break;
13324 case CPP_AND:
13325 id = ansi_opname (BIT_AND_EXPR);
13326 break;
13328 case CPP_OR:
13329 id = ansi_opname (BIT_IOR_EXPR);
13330 break;
13332 case CPP_COMPL:
13333 id = ansi_opname (BIT_NOT_EXPR);
13334 break;
13336 case CPP_NOT:
13337 id = ansi_opname (TRUTH_NOT_EXPR);
13338 break;
13340 case CPP_EQ:
13341 id = ansi_assopname (NOP_EXPR);
13342 break;
13344 case CPP_LESS:
13345 id = ansi_opname (LT_EXPR);
13346 break;
13348 case CPP_GREATER:
13349 id = ansi_opname (GT_EXPR);
13350 break;
13352 case CPP_PLUS_EQ:
13353 id = ansi_assopname (PLUS_EXPR);
13354 break;
13356 case CPP_MINUS_EQ:
13357 id = ansi_assopname (MINUS_EXPR);
13358 break;
13360 case CPP_MULT_EQ:
13361 id = ansi_assopname (MULT_EXPR);
13362 break;
13364 case CPP_DIV_EQ:
13365 id = ansi_assopname (TRUNC_DIV_EXPR);
13366 break;
13368 case CPP_MOD_EQ:
13369 id = ansi_assopname (TRUNC_MOD_EXPR);
13370 break;
13372 case CPP_XOR_EQ:
13373 id = ansi_assopname (BIT_XOR_EXPR);
13374 break;
13376 case CPP_AND_EQ:
13377 id = ansi_assopname (BIT_AND_EXPR);
13378 break;
13380 case CPP_OR_EQ:
13381 id = ansi_assopname (BIT_IOR_EXPR);
13382 break;
13384 case CPP_LSHIFT:
13385 id = ansi_opname (LSHIFT_EXPR);
13386 break;
13388 case CPP_RSHIFT:
13389 id = ansi_opname (RSHIFT_EXPR);
13390 break;
13392 case CPP_LSHIFT_EQ:
13393 id = ansi_assopname (LSHIFT_EXPR);
13394 break;
13396 case CPP_RSHIFT_EQ:
13397 id = ansi_assopname (RSHIFT_EXPR);
13398 break;
13400 case CPP_EQ_EQ:
13401 id = ansi_opname (EQ_EXPR);
13402 break;
13404 case CPP_NOT_EQ:
13405 id = ansi_opname (NE_EXPR);
13406 break;
13408 case CPP_LESS_EQ:
13409 id = ansi_opname (LE_EXPR);
13410 break;
13412 case CPP_GREATER_EQ:
13413 id = ansi_opname (GE_EXPR);
13414 break;
13416 case CPP_AND_AND:
13417 id = ansi_opname (TRUTH_ANDIF_EXPR);
13418 break;
13420 case CPP_OR_OR:
13421 id = ansi_opname (TRUTH_ORIF_EXPR);
13422 break;
13424 case CPP_PLUS_PLUS:
13425 id = ansi_opname (POSTINCREMENT_EXPR);
13426 break;
13428 case CPP_MINUS_MINUS:
13429 id = ansi_opname (PREDECREMENT_EXPR);
13430 break;
13432 case CPP_COMMA:
13433 id = ansi_opname (COMPOUND_EXPR);
13434 break;
13436 case CPP_DEREF_STAR:
13437 id = ansi_opname (MEMBER_REF);
13438 break;
13440 case CPP_DEREF:
13441 id = ansi_opname (COMPONENT_REF);
13442 break;
13444 case CPP_OPEN_PAREN:
13445 /* Consume the `('. */
13446 cp_lexer_consume_token (parser->lexer);
13447 /* Look for the matching `)'. */
13448 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13449 return ansi_opname (CALL_EXPR);
13451 case CPP_OPEN_SQUARE:
13452 /* Consume the `['. */
13453 cp_lexer_consume_token (parser->lexer);
13454 /* Look for the matching `]'. */
13455 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13456 return ansi_opname (ARRAY_REF);
13458 case CPP_UTF8STRING:
13459 case CPP_UTF8STRING_USERDEF:
13460 utf8 = true;
13461 case CPP_STRING:
13462 case CPP_WSTRING:
13463 case CPP_STRING16:
13464 case CPP_STRING32:
13465 case CPP_STRING_USERDEF:
13466 case CPP_WSTRING_USERDEF:
13467 case CPP_STRING16_USERDEF:
13468 case CPP_STRING32_USERDEF:
13470 tree str, string_tree;
13471 int sz, len;
13473 if (cxx_dialect == cxx98)
13474 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
13476 /* Consume the string. */
13477 str = cp_parser_string_literal (parser, /*translate=*/true,
13478 /*wide_ok=*/true, /*lookup_udlit=*/false);
13479 if (str == error_mark_node)
13480 return error_mark_node;
13481 else if (TREE_CODE (str) == USERDEF_LITERAL)
13483 string_tree = USERDEF_LITERAL_VALUE (str);
13484 id = USERDEF_LITERAL_SUFFIX_ID (str);
13486 else
13488 string_tree = str;
13489 /* Look for the suffix identifier. */
13490 token = cp_lexer_peek_token (parser->lexer);
13491 if (token->type == CPP_NAME)
13492 id = cp_parser_identifier (parser);
13493 else if (token->type == CPP_KEYWORD)
13495 error ("unexpected keyword;"
13496 " remove space between quotes and suffix identifier");
13497 return error_mark_node;
13499 else
13501 error ("expected suffix identifier");
13502 return error_mark_node;
13505 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13506 (TREE_TYPE (TREE_TYPE (string_tree))));
13507 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
13508 if (len != 0)
13510 error ("expected empty string after %<operator%> keyword");
13511 return error_mark_node;
13513 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
13514 != char_type_node)
13516 error ("invalid encoding prefix in literal operator");
13517 return error_mark_node;
13519 if (id != error_mark_node)
13521 const char *name = IDENTIFIER_POINTER (id);
13522 id = cp_literal_operator_id (name);
13524 return id;
13527 default:
13528 /* Anything else is an error. */
13529 break;
13532 /* If we have selected an identifier, we need to consume the
13533 operator token. */
13534 if (id)
13535 cp_lexer_consume_token (parser->lexer);
13536 /* Otherwise, no valid operator name was present. */
13537 else
13539 cp_parser_error (parser, "expected operator");
13540 id = error_mark_node;
13543 return id;
13546 /* Parse a template-declaration.
13548 template-declaration:
13549 export [opt] template < template-parameter-list > declaration
13551 If MEMBER_P is TRUE, this template-declaration occurs within a
13552 class-specifier.
13554 The grammar rule given by the standard isn't correct. What
13555 is really meant is:
13557 template-declaration:
13558 export [opt] template-parameter-list-seq
13559 decl-specifier-seq [opt] init-declarator [opt] ;
13560 export [opt] template-parameter-list-seq
13561 function-definition
13563 template-parameter-list-seq:
13564 template-parameter-list-seq [opt]
13565 template < template-parameter-list >
13567 Concept Extensions:
13569 template-parameter-list-seq:
13570 template < template-parameter-list > requires-clause [opt]
13572 requires-clause:
13573 requires logical-or-expression */
13575 static void
13576 cp_parser_template_declaration (cp_parser* parser, bool member_p)
13578 /* Check for `export'. */
13579 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
13581 /* Consume the `export' token. */
13582 cp_lexer_consume_token (parser->lexer);
13583 /* Warn that we do not support `export'. */
13584 warning (0, "keyword %<export%> not implemented, and will be ignored");
13587 cp_parser_template_declaration_after_export (parser, member_p);
13590 /* Parse a template-parameter-list.
13592 template-parameter-list:
13593 template-parameter
13594 template-parameter-list , template-parameter
13596 Returns a TREE_LIST. Each node represents a template parameter.
13597 The nodes are connected via their TREE_CHAINs. */
13599 static tree
13600 cp_parser_template_parameter_list (cp_parser* parser)
13602 tree parameter_list = NULL_TREE;
13604 begin_template_parm_list ();
13606 /* The loop below parses the template parms. We first need to know
13607 the total number of template parms to be able to compute proper
13608 canonical types of each dependent type. So after the loop, when
13609 we know the total number of template parms,
13610 end_template_parm_list computes the proper canonical types and
13611 fixes up the dependent types accordingly. */
13612 while (true)
13614 tree parameter;
13615 bool is_non_type;
13616 bool is_parameter_pack;
13617 location_t parm_loc;
13619 /* Parse the template-parameter. */
13620 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
13621 parameter = cp_parser_template_parameter (parser,
13622 &is_non_type,
13623 &is_parameter_pack);
13624 /* Add it to the list. */
13625 if (parameter != error_mark_node)
13626 parameter_list = process_template_parm (parameter_list,
13627 parm_loc,
13628 parameter,
13629 is_non_type,
13630 is_parameter_pack);
13631 else
13633 tree err_parm = build_tree_list (parameter, parameter);
13634 parameter_list = chainon (parameter_list, err_parm);
13637 /* If the next token is not a `,', we're done. */
13638 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13639 break;
13640 /* Otherwise, consume the `,' token. */
13641 cp_lexer_consume_token (parser->lexer);
13644 return end_template_parm_list (parameter_list);
13647 /* Parse a introduction-list.
13649 introduction-list:
13650 introduced-parameter
13651 introduction-list , introduced-parameter
13653 introduced-parameter:
13654 ...[opt] identifier
13656 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
13657 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
13658 WILDCARD_DECL will also have DECL_NAME set and token location in
13659 DECL_SOURCE_LOCATION. */
13661 static tree
13662 cp_parser_introduction_list (cp_parser *parser)
13664 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
13666 while (true)
13668 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
13669 if (is_pack)
13670 cp_lexer_consume_token (parser->lexer);
13672 /* Build placeholder. */
13673 tree parm = build_nt (WILDCARD_DECL);
13674 DECL_SOURCE_LOCATION (parm)
13675 = cp_lexer_peek_token (parser->lexer)->location;
13676 DECL_NAME (parm) = cp_parser_identifier (parser);
13677 WILDCARD_PACK_P (parm) = is_pack;
13678 vec_safe_push (introduction_vec, parm);
13680 /* If the next token is not a `,', we're done. */
13681 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13682 break;
13683 /* Otherwise, consume the `,' token. */
13684 cp_lexer_consume_token (parser->lexer);
13687 /* Convert the vec into a TREE_VEC. */
13688 tree introduction_list = make_tree_vec (introduction_vec->length ());
13689 unsigned int n;
13690 tree parm;
13691 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
13692 TREE_VEC_ELT (introduction_list, n) = parm;
13694 release_tree_vector (introduction_vec);
13695 return introduction_list;
13698 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
13699 is an abstract declarator. */
13701 static inline cp_declarator*
13702 get_id_declarator (cp_declarator *declarator)
13704 cp_declarator *d = declarator;
13705 while (d && d->kind != cdk_id)
13706 d = d->declarator;
13707 return d;
13710 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
13711 is an abstract declarator. */
13713 static inline tree
13714 get_unqualified_id (cp_declarator *declarator)
13716 declarator = get_id_declarator (declarator);
13717 if (declarator)
13718 return declarator->u.id.unqualified_name;
13719 else
13720 return NULL_TREE;
13723 /* Returns true if DECL represents a constrained-parameter. */
13725 static inline bool
13726 is_constrained_parameter (tree decl)
13728 return (decl
13729 && TREE_CODE (decl) == TYPE_DECL
13730 && CONSTRAINED_PARM_CONCEPT (decl)
13731 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
13734 /* Returns true if PARM declares a constrained-parameter. */
13736 static inline bool
13737 is_constrained_parameter (cp_parameter_declarator *parm)
13739 return is_constrained_parameter (parm->decl_specifiers.type);
13742 /* Check that the type parameter is only a declarator-id, and that its
13743 type is not cv-qualified. */
13745 bool
13746 cp_parser_check_constrained_type_parm (cp_parser *parser,
13747 cp_parameter_declarator *parm)
13749 if (!parm->declarator)
13750 return true;
13752 if (parm->declarator->kind != cdk_id)
13754 cp_parser_error (parser, "invalid constrained type parameter");
13755 return false;
13758 /* Don't allow cv-qualified type parameters. */
13759 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
13760 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
13762 cp_parser_error (parser, "cv-qualified type parameter");
13763 return false;
13766 return true;
13769 /* Finish parsing/processing a template type parameter and checking
13770 various restrictions. */
13772 static inline tree
13773 cp_parser_constrained_type_template_parm (cp_parser *parser,
13774 tree id,
13775 cp_parameter_declarator* parmdecl)
13777 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
13778 return finish_template_type_parm (class_type_node, id);
13779 else
13780 return error_mark_node;
13783 /* Finish parsing/processing a template template parameter by borrowing
13784 the template parameter list from the prototype parameter. */
13786 static tree
13787 cp_parser_constrained_template_template_parm (cp_parser *parser,
13788 tree proto,
13789 tree id,
13790 cp_parameter_declarator *parmdecl)
13792 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
13793 return error_mark_node;
13795 /* FIXME: This should probably be copied, and we may need to adjust
13796 the template parameter depths. */
13797 tree saved_parms = current_template_parms;
13798 begin_template_parm_list ();
13799 current_template_parms = DECL_TEMPLATE_PARMS (proto);
13800 end_template_parm_list ();
13802 tree parm = finish_template_template_parm (class_type_node, id);
13803 current_template_parms = saved_parms;
13805 return parm;
13808 /* Create a new non-type template parameter from the given PARM
13809 declarator. */
13811 static tree
13812 constrained_non_type_template_parm (bool *is_non_type,
13813 cp_parameter_declarator *parm)
13815 *is_non_type = true;
13816 cp_declarator *decl = parm->declarator;
13817 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
13818 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
13819 return grokdeclarator (decl, specs, TPARM, 0, NULL);
13822 /* Build a constrained template parameter based on the PARMDECL
13823 declarator. The type of PARMDECL is the constrained type, which
13824 refers to the prototype template parameter that ultimately
13825 specifies the type of the declared parameter. */
13827 static tree
13828 finish_constrained_parameter (cp_parser *parser,
13829 cp_parameter_declarator *parmdecl,
13830 bool *is_non_type,
13831 bool *is_parameter_pack)
13833 tree decl = parmdecl->decl_specifiers.type;
13834 tree id = get_unqualified_id (parmdecl->declarator);
13835 tree def = parmdecl->default_argument;
13836 tree proto = DECL_INITIAL (decl);
13838 /* A template parameter constrained by a variadic concept shall also
13839 be declared as a template parameter pack. */
13840 bool is_variadic = template_parameter_pack_p (proto);
13841 if (is_variadic && !*is_parameter_pack)
13842 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
13844 /* Build the parameter. Return an error if the declarator was invalid. */
13845 tree parm;
13846 if (TREE_CODE (proto) == TYPE_DECL)
13847 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
13848 else if (TREE_CODE (proto) == TEMPLATE_DECL)
13849 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
13850 parmdecl);
13851 else
13852 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
13853 if (parm == error_mark_node)
13854 return error_mark_node;
13856 /* Finish the parameter decl and create a node attaching the
13857 default argument and constraint. */
13858 parm = build_tree_list (def, parm);
13859 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
13861 return parm;
13864 /* Returns true if the parsed type actually represents the declaration
13865 of a type template-parameter. */
13867 static inline bool
13868 declares_constrained_type_template_parameter (tree type)
13870 return (is_constrained_parameter (type)
13871 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
13875 /* Returns true if the parsed type actually represents the declaration of
13876 a template template-parameter. */
13878 static bool
13879 declares_constrained_template_template_parameter (tree type)
13881 return (is_constrained_parameter (type)
13882 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
13885 /* Parse a default argument for a type template-parameter.
13886 Note that diagnostics are handled in cp_parser_template_parameter. */
13888 static tree
13889 cp_parser_default_type_template_argument (cp_parser *parser)
13891 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
13893 /* Consume the `=' token. */
13894 cp_lexer_consume_token (parser->lexer);
13896 /* Parse the default-argument. */
13897 push_deferring_access_checks (dk_no_deferred);
13898 tree default_argument = cp_parser_type_id (parser);
13899 pop_deferring_access_checks ();
13901 return default_argument;
13904 /* Parse a default argument for a template template-parameter. */
13906 static tree
13907 cp_parser_default_template_template_argument (cp_parser *parser)
13909 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
13911 bool is_template;
13913 /* Consume the `='. */
13914 cp_lexer_consume_token (parser->lexer);
13915 /* Parse the id-expression. */
13916 push_deferring_access_checks (dk_no_deferred);
13917 /* save token before parsing the id-expression, for error
13918 reporting */
13919 const cp_token* token = cp_lexer_peek_token (parser->lexer);
13920 tree default_argument
13921 = cp_parser_id_expression (parser,
13922 /*template_keyword_p=*/false,
13923 /*check_dependency_p=*/true,
13924 /*template_p=*/&is_template,
13925 /*declarator_p=*/false,
13926 /*optional_p=*/false);
13927 if (TREE_CODE (default_argument) == TYPE_DECL)
13928 /* If the id-expression was a template-id that refers to
13929 a template-class, we already have the declaration here,
13930 so no further lookup is needed. */
13932 else
13933 /* Look up the name. */
13934 default_argument
13935 = cp_parser_lookup_name (parser, default_argument,
13936 none_type,
13937 /*is_template=*/is_template,
13938 /*is_namespace=*/false,
13939 /*check_dependency=*/true,
13940 /*ambiguous_decls=*/NULL,
13941 token->location);
13942 /* See if the default argument is valid. */
13943 default_argument = check_template_template_default_arg (default_argument);
13944 pop_deferring_access_checks ();
13945 return default_argument;
13948 /* Parse a template-parameter.
13950 template-parameter:
13951 type-parameter
13952 parameter-declaration
13954 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13955 the parameter. The TREE_PURPOSE is the default value, if any.
13956 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13957 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13958 set to true iff this parameter is a parameter pack. */
13960 static tree
13961 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
13962 bool *is_parameter_pack)
13964 cp_token *token;
13965 cp_parameter_declarator *parameter_declarator;
13966 tree parm;
13968 /* Assume it is a type parameter or a template parameter. */
13969 *is_non_type = false;
13970 /* Assume it not a parameter pack. */
13971 *is_parameter_pack = false;
13972 /* Peek at the next token. */
13973 token = cp_lexer_peek_token (parser->lexer);
13974 /* If it is `class' or `template', we have a type-parameter. */
13975 if (token->keyword == RID_TEMPLATE)
13976 return cp_parser_type_parameter (parser, is_parameter_pack);
13977 /* If it is `class' or `typename' we do not know yet whether it is a
13978 type parameter or a non-type parameter. Consider:
13980 template <typename T, typename T::X X> ...
13984 template <class C, class D*> ...
13986 Here, the first parameter is a type parameter, and the second is
13987 a non-type parameter. We can tell by looking at the token after
13988 the identifier -- if it is a `,', `=', or `>' then we have a type
13989 parameter. */
13990 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13992 /* Peek at the token after `class' or `typename'. */
13993 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13994 /* If it's an ellipsis, we have a template type parameter
13995 pack. */
13996 if (token->type == CPP_ELLIPSIS)
13997 return cp_parser_type_parameter (parser, is_parameter_pack);
13998 /* If it's an identifier, skip it. */
13999 if (token->type == CPP_NAME)
14000 token = cp_lexer_peek_nth_token (parser->lexer, 3);
14001 /* Now, see if the token looks like the end of a template
14002 parameter. */
14003 if (token->type == CPP_COMMA
14004 || token->type == CPP_EQ
14005 || token->type == CPP_GREATER)
14006 return cp_parser_type_parameter (parser, is_parameter_pack);
14009 /* Otherwise, it is a non-type parameter or a constrained parameter.
14011 [temp.param]
14013 When parsing a default template-argument for a non-type
14014 template-parameter, the first non-nested `>' is taken as the end
14015 of the template parameter-list rather than a greater-than
14016 operator. */
14017 parameter_declarator
14018 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
14019 /*parenthesized_p=*/NULL);
14021 if (!parameter_declarator)
14022 return error_mark_node;
14024 /* If the parameter declaration is marked as a parameter pack, set
14025 *IS_PARAMETER_PACK to notify the caller. */
14026 if (parameter_declarator->template_parameter_pack_p)
14027 *is_parameter_pack = true;
14029 if (parameter_declarator->default_argument)
14031 /* Can happen in some cases of erroneous input (c++/34892). */
14032 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14033 /* Consume the `...' for better error recovery. */
14034 cp_lexer_consume_token (parser->lexer);
14037 // The parameter may have been constrained.
14038 if (is_constrained_parameter (parameter_declarator))
14039 return finish_constrained_parameter (parser,
14040 parameter_declarator,
14041 is_non_type,
14042 is_parameter_pack);
14044 // Now we're sure that the parameter is a non-type parameter.
14045 *is_non_type = true;
14047 parm = grokdeclarator (parameter_declarator->declarator,
14048 &parameter_declarator->decl_specifiers,
14049 TPARM, /*initialized=*/0,
14050 /*attrlist=*/NULL);
14051 if (parm == error_mark_node)
14052 return error_mark_node;
14054 return build_tree_list (parameter_declarator->default_argument, parm);
14057 /* Parse a type-parameter.
14059 type-parameter:
14060 class identifier [opt]
14061 class identifier [opt] = type-id
14062 typename identifier [opt]
14063 typename identifier [opt] = type-id
14064 template < template-parameter-list > class identifier [opt]
14065 template < template-parameter-list > class identifier [opt]
14066 = id-expression
14068 GNU Extension (variadic templates):
14070 type-parameter:
14071 class ... identifier [opt]
14072 typename ... identifier [opt]
14074 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
14075 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
14076 the declaration of the parameter.
14078 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
14080 static tree
14081 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
14083 cp_token *token;
14084 tree parameter;
14086 /* Look for a keyword to tell us what kind of parameter this is. */
14087 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
14088 if (!token)
14089 return error_mark_node;
14091 switch (token->keyword)
14093 case RID_CLASS:
14094 case RID_TYPENAME:
14096 tree identifier;
14097 tree default_argument;
14099 /* If the next token is an ellipsis, we have a template
14100 argument pack. */
14101 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14103 /* Consume the `...' token. */
14104 cp_lexer_consume_token (parser->lexer);
14105 maybe_warn_variadic_templates ();
14107 *is_parameter_pack = true;
14110 /* If the next token is an identifier, then it names the
14111 parameter. */
14112 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14113 identifier = cp_parser_identifier (parser);
14114 else
14115 identifier = NULL_TREE;
14117 /* Create the parameter. */
14118 parameter = finish_template_type_parm (class_type_node, identifier);
14120 /* If the next token is an `=', we have a default argument. */
14121 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14123 default_argument
14124 = cp_parser_default_type_template_argument (parser);
14126 /* Template parameter packs cannot have default
14127 arguments. */
14128 if (*is_parameter_pack)
14130 if (identifier)
14131 error_at (token->location,
14132 "template parameter pack %qD cannot have a "
14133 "default argument", identifier);
14134 else
14135 error_at (token->location,
14136 "template parameter packs cannot have "
14137 "default arguments");
14138 default_argument = NULL_TREE;
14140 else if (check_for_bare_parameter_packs (default_argument))
14141 default_argument = error_mark_node;
14143 else
14144 default_argument = NULL_TREE;
14146 /* Create the combined representation of the parameter and the
14147 default argument. */
14148 parameter = build_tree_list (default_argument, parameter);
14150 break;
14152 case RID_TEMPLATE:
14154 tree identifier;
14155 tree default_argument;
14157 /* Look for the `<'. */
14158 cp_parser_require (parser, CPP_LESS, RT_LESS);
14159 /* Parse the template-parameter-list. */
14160 cp_parser_template_parameter_list (parser);
14161 /* Look for the `>'. */
14162 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14164 // If template requirements are present, parse them.
14165 if (flag_concepts)
14167 tree reqs = get_shorthand_constraints (current_template_parms);
14168 if (tree r = cp_parser_requires_clause_opt (parser))
14169 reqs = conjoin_constraints (reqs, make_predicate_constraint (r));
14170 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
14173 /* Look for the `class' or 'typename' keywords. */
14174 cp_parser_type_parameter_key (parser);
14175 /* If the next token is an ellipsis, we have a template
14176 argument pack. */
14177 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14179 /* Consume the `...' token. */
14180 cp_lexer_consume_token (parser->lexer);
14181 maybe_warn_variadic_templates ();
14183 *is_parameter_pack = true;
14185 /* If the next token is an `=', then there is a
14186 default-argument. If the next token is a `>', we are at
14187 the end of the parameter-list. If the next token is a `,',
14188 then we are at the end of this parameter. */
14189 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
14190 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
14191 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14193 identifier = cp_parser_identifier (parser);
14194 /* Treat invalid names as if the parameter were nameless. */
14195 if (identifier == error_mark_node)
14196 identifier = NULL_TREE;
14198 else
14199 identifier = NULL_TREE;
14201 /* Create the template parameter. */
14202 parameter = finish_template_template_parm (class_type_node,
14203 identifier);
14205 /* If the next token is an `=', then there is a
14206 default-argument. */
14207 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14209 default_argument
14210 = cp_parser_default_template_template_argument (parser);
14212 /* Template parameter packs cannot have default
14213 arguments. */
14214 if (*is_parameter_pack)
14216 if (identifier)
14217 error_at (token->location,
14218 "template parameter pack %qD cannot "
14219 "have a default argument",
14220 identifier);
14221 else
14222 error_at (token->location, "template parameter packs cannot "
14223 "have default arguments");
14224 default_argument = NULL_TREE;
14227 else
14228 default_argument = NULL_TREE;
14230 /* Create the combined representation of the parameter and the
14231 default argument. */
14232 parameter = build_tree_list (default_argument, parameter);
14234 break;
14236 default:
14237 gcc_unreachable ();
14238 break;
14241 return parameter;
14244 /* Parse a template-id.
14246 template-id:
14247 template-name < template-argument-list [opt] >
14249 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
14250 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
14251 returned. Otherwise, if the template-name names a function, or set
14252 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
14253 names a class, returns a TYPE_DECL for the specialization.
14255 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
14256 uninstantiated templates. */
14258 static tree
14259 cp_parser_template_id (cp_parser *parser,
14260 bool template_keyword_p,
14261 bool check_dependency_p,
14262 enum tag_types tag_type,
14263 bool is_declaration)
14265 int i;
14266 tree templ;
14267 tree arguments;
14268 tree template_id;
14269 cp_token_position start_of_id = 0;
14270 deferred_access_check *chk;
14271 vec<deferred_access_check, va_gc> *access_check;
14272 cp_token *next_token = NULL, *next_token_2 = NULL;
14273 bool is_identifier;
14275 /* If the next token corresponds to a template-id, there is no need
14276 to reparse it. */
14277 next_token = cp_lexer_peek_token (parser->lexer);
14278 if (next_token->type == CPP_TEMPLATE_ID)
14280 struct tree_check *check_value;
14282 /* Get the stored value. */
14283 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
14284 /* Perform any access checks that were deferred. */
14285 access_check = check_value->checks;
14286 if (access_check)
14288 FOR_EACH_VEC_ELT (*access_check, i, chk)
14289 perform_or_defer_access_check (chk->binfo,
14290 chk->decl,
14291 chk->diag_decl,
14292 tf_warning_or_error);
14294 /* Return the stored value. */
14295 return check_value->value;
14298 /* Avoid performing name lookup if there is no possibility of
14299 finding a template-id. */
14300 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
14301 || (next_token->type == CPP_NAME
14302 && !cp_parser_nth_token_starts_template_argument_list_p
14303 (parser, 2)))
14305 cp_parser_error (parser, "expected template-id");
14306 return error_mark_node;
14309 /* Remember where the template-id starts. */
14310 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
14311 start_of_id = cp_lexer_token_position (parser->lexer, false);
14313 push_deferring_access_checks (dk_deferred);
14315 /* Parse the template-name. */
14316 is_identifier = false;
14317 templ = cp_parser_template_name (parser, template_keyword_p,
14318 check_dependency_p,
14319 is_declaration,
14320 tag_type,
14321 &is_identifier);
14322 if (templ == error_mark_node || is_identifier)
14324 pop_deferring_access_checks ();
14325 return templ;
14328 /* If we find the sequence `[:' after a template-name, it's probably
14329 a digraph-typo for `< ::'. Substitute the tokens and check if we can
14330 parse correctly the argument list. */
14331 next_token = cp_lexer_peek_token (parser->lexer);
14332 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14333 if (next_token->type == CPP_OPEN_SQUARE
14334 && next_token->flags & DIGRAPH
14335 && next_token_2->type == CPP_COLON
14336 && !(next_token_2->flags & PREV_WHITE))
14338 cp_parser_parse_tentatively (parser);
14339 /* Change `:' into `::'. */
14340 next_token_2->type = CPP_SCOPE;
14341 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
14342 CPP_LESS. */
14343 cp_lexer_consume_token (parser->lexer);
14345 /* Parse the arguments. */
14346 arguments = cp_parser_enclosed_template_argument_list (parser);
14347 if (!cp_parser_parse_definitely (parser))
14349 /* If we couldn't parse an argument list, then we revert our changes
14350 and return simply an error. Maybe this is not a template-id
14351 after all. */
14352 next_token_2->type = CPP_COLON;
14353 cp_parser_error (parser, "expected %<<%>");
14354 pop_deferring_access_checks ();
14355 return error_mark_node;
14357 /* Otherwise, emit an error about the invalid digraph, but continue
14358 parsing because we got our argument list. */
14359 if (permerror (next_token->location,
14360 "%<<::%> cannot begin a template-argument list"))
14362 static bool hint = false;
14363 inform (next_token->location,
14364 "%<<:%> is an alternate spelling for %<[%>."
14365 " Insert whitespace between %<<%> and %<::%>");
14366 if (!hint && !flag_permissive)
14368 inform (next_token->location, "(if you use %<-fpermissive%> "
14369 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
14370 "accept your code)");
14371 hint = true;
14375 else
14377 /* Look for the `<' that starts the template-argument-list. */
14378 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
14380 pop_deferring_access_checks ();
14381 return error_mark_node;
14383 /* Parse the arguments. */
14384 arguments = cp_parser_enclosed_template_argument_list (parser);
14387 /* Build a representation of the specialization. */
14388 if (identifier_p (templ))
14389 template_id = build_min_nt_loc (next_token->location,
14390 TEMPLATE_ID_EXPR,
14391 templ, arguments);
14392 else if (DECL_TYPE_TEMPLATE_P (templ)
14393 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
14395 bool entering_scope;
14396 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
14397 template (rather than some instantiation thereof) only if
14398 is not nested within some other construct. For example, in
14399 "template <typename T> void f(T) { A<T>::", A<T> is just an
14400 instantiation of A. */
14401 entering_scope = (template_parm_scope_p ()
14402 && cp_lexer_next_token_is (parser->lexer,
14403 CPP_SCOPE));
14404 template_id
14405 = finish_template_type (templ, arguments, entering_scope);
14407 /* A template-like identifier may be a partial concept id. */
14408 else if (flag_concepts
14409 && (template_id = (cp_parser_maybe_partial_concept_id
14410 (parser, templ, arguments))))
14411 return template_id;
14412 else if (variable_template_p (templ))
14414 template_id = lookup_template_variable (templ, arguments);
14415 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
14416 SET_EXPR_LOCATION (template_id, next_token->location);
14418 else
14420 /* If it's not a class-template or a template-template, it should be
14421 a function-template. */
14422 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
14423 || TREE_CODE (templ) == OVERLOAD
14424 || BASELINK_P (templ)));
14426 template_id = lookup_template_function (templ, arguments);
14427 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
14428 SET_EXPR_LOCATION (template_id, next_token->location);
14431 /* If parsing tentatively, replace the sequence of tokens that makes
14432 up the template-id with a CPP_TEMPLATE_ID token. That way,
14433 should we re-parse the token stream, we will not have to repeat
14434 the effort required to do the parse, nor will we issue duplicate
14435 error messages about problems during instantiation of the
14436 template. */
14437 if (start_of_id
14438 /* Don't do this if we had a parse error in a declarator; re-parsing
14439 might succeed if a name changes meaning (60361). */
14440 && !(cp_parser_error_occurred (parser)
14441 && cp_parser_parsing_tentatively (parser)
14442 && parser->in_declarator_p))
14444 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
14446 /* Reset the contents of the START_OF_ID token. */
14447 token->type = CPP_TEMPLATE_ID;
14448 /* Retrieve any deferred checks. Do not pop this access checks yet
14449 so the memory will not be reclaimed during token replacing below. */
14450 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14451 token->u.tree_check_value->value = template_id;
14452 token->u.tree_check_value->checks = get_deferred_access_checks ();
14453 token->keyword = RID_MAX;
14455 /* Purge all subsequent tokens. */
14456 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
14458 /* ??? Can we actually assume that, if template_id ==
14459 error_mark_node, we will have issued a diagnostic to the
14460 user, as opposed to simply marking the tentative parse as
14461 failed? */
14462 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
14463 error_at (token->location, "parse error in template argument list");
14466 pop_to_parent_deferring_access_checks ();
14467 return template_id;
14470 /* Parse a template-name.
14472 template-name:
14473 identifier
14475 The standard should actually say:
14477 template-name:
14478 identifier
14479 operator-function-id
14481 A defect report has been filed about this issue.
14483 A conversion-function-id cannot be a template name because they cannot
14484 be part of a template-id. In fact, looking at this code:
14486 a.operator K<int>()
14488 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
14489 It is impossible to call a templated conversion-function-id with an
14490 explicit argument list, since the only allowed template parameter is
14491 the type to which it is converting.
14493 If TEMPLATE_KEYWORD_P is true, then we have just seen the
14494 `template' keyword, in a construction like:
14496 T::template f<3>()
14498 In that case `f' is taken to be a template-name, even though there
14499 is no way of knowing for sure.
14501 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
14502 name refers to a set of overloaded functions, at least one of which
14503 is a template, or an IDENTIFIER_NODE with the name of the template,
14504 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
14505 names are looked up inside uninstantiated templates. */
14507 static tree
14508 cp_parser_template_name (cp_parser* parser,
14509 bool template_keyword_p,
14510 bool check_dependency_p,
14511 bool is_declaration,
14512 enum tag_types tag_type,
14513 bool *is_identifier)
14515 tree identifier;
14516 tree decl;
14517 tree fns;
14518 cp_token *token = cp_lexer_peek_token (parser->lexer);
14520 /* If the next token is `operator', then we have either an
14521 operator-function-id or a conversion-function-id. */
14522 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
14524 /* We don't know whether we're looking at an
14525 operator-function-id or a conversion-function-id. */
14526 cp_parser_parse_tentatively (parser);
14527 /* Try an operator-function-id. */
14528 identifier = cp_parser_operator_function_id (parser);
14529 /* If that didn't work, try a conversion-function-id. */
14530 if (!cp_parser_parse_definitely (parser))
14532 cp_parser_error (parser, "expected template-name");
14533 return error_mark_node;
14536 /* Look for the identifier. */
14537 else
14538 identifier = cp_parser_identifier (parser);
14540 /* If we didn't find an identifier, we don't have a template-id. */
14541 if (identifier == error_mark_node)
14542 return error_mark_node;
14544 /* If the name immediately followed the `template' keyword, then it
14545 is a template-name. However, if the next token is not `<', then
14546 we do not treat it as a template-name, since it is not being used
14547 as part of a template-id. This enables us to handle constructs
14548 like:
14550 template <typename T> struct S { S(); };
14551 template <typename T> S<T>::S();
14553 correctly. We would treat `S' as a template -- if it were `S<T>'
14554 -- but we do not if there is no `<'. */
14556 if (processing_template_decl
14557 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
14559 /* In a declaration, in a dependent context, we pretend that the
14560 "template" keyword was present in order to improve error
14561 recovery. For example, given:
14563 template <typename T> void f(T::X<int>);
14565 we want to treat "X<int>" as a template-id. */
14566 if (is_declaration
14567 && !template_keyword_p
14568 && parser->scope && TYPE_P (parser->scope)
14569 && check_dependency_p
14570 && dependent_scope_p (parser->scope)
14571 /* Do not do this for dtors (or ctors), since they never
14572 need the template keyword before their name. */
14573 && !constructor_name_p (identifier, parser->scope))
14575 cp_token_position start = 0;
14577 /* Explain what went wrong. */
14578 error_at (token->location, "non-template %qD used as template",
14579 identifier);
14580 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
14581 parser->scope, identifier);
14582 /* If parsing tentatively, find the location of the "<" token. */
14583 if (cp_parser_simulate_error (parser))
14584 start = cp_lexer_token_position (parser->lexer, true);
14585 /* Parse the template arguments so that we can issue error
14586 messages about them. */
14587 cp_lexer_consume_token (parser->lexer);
14588 cp_parser_enclosed_template_argument_list (parser);
14589 /* Skip tokens until we find a good place from which to
14590 continue parsing. */
14591 cp_parser_skip_to_closing_parenthesis (parser,
14592 /*recovering=*/true,
14593 /*or_comma=*/true,
14594 /*consume_paren=*/false);
14595 /* If parsing tentatively, permanently remove the
14596 template argument list. That will prevent duplicate
14597 error messages from being issued about the missing
14598 "template" keyword. */
14599 if (start)
14600 cp_lexer_purge_tokens_after (parser->lexer, start);
14601 if (is_identifier)
14602 *is_identifier = true;
14603 return identifier;
14606 /* If the "template" keyword is present, then there is generally
14607 no point in doing name-lookup, so we just return IDENTIFIER.
14608 But, if the qualifying scope is non-dependent then we can
14609 (and must) do name-lookup normally. */
14610 if (template_keyword_p
14611 && (!parser->scope
14612 || (TYPE_P (parser->scope)
14613 && dependent_type_p (parser->scope))))
14614 return identifier;
14617 /* Look up the name. */
14618 decl = cp_parser_lookup_name (parser, identifier,
14619 tag_type,
14620 /*is_template=*/true,
14621 /*is_namespace=*/false,
14622 check_dependency_p,
14623 /*ambiguous_decls=*/NULL,
14624 token->location);
14626 decl = strip_using_decl (decl);
14628 /* If DECL is a template, then the name was a template-name. */
14629 if (TREE_CODE (decl) == TEMPLATE_DECL)
14631 if (TREE_DEPRECATED (decl)
14632 && deprecated_state != DEPRECATED_SUPPRESS)
14633 warn_deprecated_use (decl, NULL_TREE);
14635 else
14637 tree fn = NULL_TREE;
14639 /* The standard does not explicitly indicate whether a name that
14640 names a set of overloaded declarations, some of which are
14641 templates, is a template-name. However, such a name should
14642 be a template-name; otherwise, there is no way to form a
14643 template-id for the overloaded templates. */
14644 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
14645 if (TREE_CODE (fns) == OVERLOAD)
14646 for (fn = fns; fn; fn = OVL_NEXT (fn))
14647 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
14648 break;
14650 if (!fn)
14652 /* The name does not name a template. */
14653 cp_parser_error (parser, "expected template-name");
14654 return error_mark_node;
14658 /* If DECL is dependent, and refers to a function, then just return
14659 its name; we will look it up again during template instantiation. */
14660 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
14662 tree scope = ovl_scope (decl);
14663 if (TYPE_P (scope) && dependent_type_p (scope))
14664 return identifier;
14667 return decl;
14670 /* Parse a template-argument-list.
14672 template-argument-list:
14673 template-argument ... [opt]
14674 template-argument-list , template-argument ... [opt]
14676 Returns a TREE_VEC containing the arguments. */
14678 static tree
14679 cp_parser_template_argument_list (cp_parser* parser)
14681 tree fixed_args[10];
14682 unsigned n_args = 0;
14683 unsigned alloced = 10;
14684 tree *arg_ary = fixed_args;
14685 tree vec;
14686 bool saved_in_template_argument_list_p;
14687 bool saved_ice_p;
14688 bool saved_non_ice_p;
14690 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
14691 parser->in_template_argument_list_p = true;
14692 /* Even if the template-id appears in an integral
14693 constant-expression, the contents of the argument list do
14694 not. */
14695 saved_ice_p = parser->integral_constant_expression_p;
14696 parser->integral_constant_expression_p = false;
14697 saved_non_ice_p = parser->non_integral_constant_expression_p;
14698 parser->non_integral_constant_expression_p = false;
14700 /* Parse the arguments. */
14703 tree argument;
14705 if (n_args)
14706 /* Consume the comma. */
14707 cp_lexer_consume_token (parser->lexer);
14709 /* Parse the template-argument. */
14710 argument = cp_parser_template_argument (parser);
14712 /* If the next token is an ellipsis, we're expanding a template
14713 argument pack. */
14714 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14716 if (argument == error_mark_node)
14718 cp_token *token = cp_lexer_peek_token (parser->lexer);
14719 error_at (token->location,
14720 "expected parameter pack before %<...%>");
14722 /* Consume the `...' token. */
14723 cp_lexer_consume_token (parser->lexer);
14725 /* Make the argument into a TYPE_PACK_EXPANSION or
14726 EXPR_PACK_EXPANSION. */
14727 argument = make_pack_expansion (argument);
14730 if (n_args == alloced)
14732 alloced *= 2;
14734 if (arg_ary == fixed_args)
14736 arg_ary = XNEWVEC (tree, alloced);
14737 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
14739 else
14740 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
14742 arg_ary[n_args++] = argument;
14744 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14746 vec = make_tree_vec (n_args);
14748 while (n_args--)
14749 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
14751 if (arg_ary != fixed_args)
14752 free (arg_ary);
14753 parser->non_integral_constant_expression_p = saved_non_ice_p;
14754 parser->integral_constant_expression_p = saved_ice_p;
14755 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
14756 #ifdef ENABLE_CHECKING
14757 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
14758 #endif
14759 return vec;
14762 /* Parse a template-argument.
14764 template-argument:
14765 assignment-expression
14766 type-id
14767 id-expression
14769 The representation is that of an assignment-expression, type-id, or
14770 id-expression -- except that the qualified id-expression is
14771 evaluated, so that the value returned is either a DECL or an
14772 OVERLOAD.
14774 Although the standard says "assignment-expression", it forbids
14775 throw-expressions or assignments in the template argument.
14776 Therefore, we use "conditional-expression" instead. */
14778 static tree
14779 cp_parser_template_argument (cp_parser* parser)
14781 tree argument;
14782 bool template_p;
14783 bool address_p;
14784 bool maybe_type_id = false;
14785 cp_token *token = NULL, *argument_start_token = NULL;
14786 location_t loc = 0;
14787 cp_id_kind idk;
14789 /* There's really no way to know what we're looking at, so we just
14790 try each alternative in order.
14792 [temp.arg]
14794 In a template-argument, an ambiguity between a type-id and an
14795 expression is resolved to a type-id, regardless of the form of
14796 the corresponding template-parameter.
14798 Therefore, we try a type-id first. */
14799 cp_parser_parse_tentatively (parser);
14800 argument = cp_parser_template_type_arg (parser);
14801 /* If there was no error parsing the type-id but the next token is a
14802 '>>', our behavior depends on which dialect of C++ we're
14803 parsing. In C++98, we probably found a typo for '> >'. But there
14804 are type-id which are also valid expressions. For instance:
14806 struct X { int operator >> (int); };
14807 template <int V> struct Foo {};
14808 Foo<X () >> 5> r;
14810 Here 'X()' is a valid type-id of a function type, but the user just
14811 wanted to write the expression "X() >> 5". Thus, we remember that we
14812 found a valid type-id, but we still try to parse the argument as an
14813 expression to see what happens.
14815 In C++0x, the '>>' will be considered two separate '>'
14816 tokens. */
14817 if (!cp_parser_error_occurred (parser)
14818 && cxx_dialect == cxx98
14819 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14821 maybe_type_id = true;
14822 cp_parser_abort_tentative_parse (parser);
14824 else
14826 /* If the next token isn't a `,' or a `>', then this argument wasn't
14827 really finished. This means that the argument is not a valid
14828 type-id. */
14829 if (!cp_parser_next_token_ends_template_argument_p (parser))
14830 cp_parser_error (parser, "expected template-argument");
14831 /* If that worked, we're done. */
14832 if (cp_parser_parse_definitely (parser))
14833 return argument;
14835 /* We're still not sure what the argument will be. */
14836 cp_parser_parse_tentatively (parser);
14837 /* Try a template. */
14838 argument_start_token = cp_lexer_peek_token (parser->lexer);
14839 argument = cp_parser_id_expression (parser,
14840 /*template_keyword_p=*/false,
14841 /*check_dependency_p=*/true,
14842 &template_p,
14843 /*declarator_p=*/false,
14844 /*optional_p=*/false);
14845 /* If the next token isn't a `,' or a `>', then this argument wasn't
14846 really finished. */
14847 if (!cp_parser_next_token_ends_template_argument_p (parser))
14848 cp_parser_error (parser, "expected template-argument");
14849 if (!cp_parser_error_occurred (parser))
14851 /* Figure out what is being referred to. If the id-expression
14852 was for a class template specialization, then we will have a
14853 TYPE_DECL at this point. There is no need to do name lookup
14854 at this point in that case. */
14855 if (TREE_CODE (argument) != TYPE_DECL)
14856 argument = cp_parser_lookup_name (parser, argument,
14857 none_type,
14858 /*is_template=*/template_p,
14859 /*is_namespace=*/false,
14860 /*check_dependency=*/true,
14861 /*ambiguous_decls=*/NULL,
14862 argument_start_token->location);
14863 if (TREE_CODE (argument) != TEMPLATE_DECL
14864 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14865 cp_parser_error (parser, "expected template-name");
14867 if (cp_parser_parse_definitely (parser))
14869 if (TREE_DEPRECATED (argument))
14870 warn_deprecated_use (argument, NULL_TREE);
14871 return argument;
14873 /* It must be a non-type argument. There permitted cases are given
14874 in [temp.arg.nontype]:
14876 -- an integral constant-expression of integral or enumeration
14877 type; or
14879 -- the name of a non-type template-parameter; or
14881 -- the name of an object or function with external linkage...
14883 -- the address of an object or function with external linkage...
14885 -- a pointer to member... */
14886 /* Look for a non-type template parameter. */
14887 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14889 cp_parser_parse_tentatively (parser);
14890 argument = cp_parser_primary_expression (parser,
14891 /*address_p=*/false,
14892 /*cast_p=*/false,
14893 /*template_arg_p=*/true,
14894 &idk);
14895 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14896 || !cp_parser_next_token_ends_template_argument_p (parser))
14897 cp_parser_simulate_error (parser);
14898 if (cp_parser_parse_definitely (parser))
14899 return argument;
14902 /* If the next token is "&", the argument must be the address of an
14903 object or function with external linkage. */
14904 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14905 if (address_p)
14907 loc = cp_lexer_peek_token (parser->lexer)->location;
14908 cp_lexer_consume_token (parser->lexer);
14910 /* See if we might have an id-expression. */
14911 token = cp_lexer_peek_token (parser->lexer);
14912 if (token->type == CPP_NAME
14913 || token->keyword == RID_OPERATOR
14914 || token->type == CPP_SCOPE
14915 || token->type == CPP_TEMPLATE_ID
14916 || token->type == CPP_NESTED_NAME_SPECIFIER)
14918 cp_parser_parse_tentatively (parser);
14919 argument = cp_parser_primary_expression (parser,
14920 address_p,
14921 /*cast_p=*/false,
14922 /*template_arg_p=*/true,
14923 &idk);
14924 if (cp_parser_error_occurred (parser)
14925 || !cp_parser_next_token_ends_template_argument_p (parser))
14926 cp_parser_abort_tentative_parse (parser);
14927 else
14929 tree probe;
14931 if (INDIRECT_REF_P (argument))
14933 /* Strip the dereference temporarily. */
14934 gcc_assert (REFERENCE_REF_P (argument));
14935 argument = TREE_OPERAND (argument, 0);
14938 /* If we're in a template, we represent a qualified-id referring
14939 to a static data member as a SCOPE_REF even if the scope isn't
14940 dependent so that we can check access control later. */
14941 probe = argument;
14942 if (TREE_CODE (probe) == SCOPE_REF)
14943 probe = TREE_OPERAND (probe, 1);
14944 if (VAR_P (probe))
14946 /* A variable without external linkage might still be a
14947 valid constant-expression, so no error is issued here
14948 if the external-linkage check fails. */
14949 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14950 cp_parser_simulate_error (parser);
14952 else if (is_overloaded_fn (argument))
14953 /* All overloaded functions are allowed; if the external
14954 linkage test does not pass, an error will be issued
14955 later. */
14957 else if (address_p
14958 && (TREE_CODE (argument) == OFFSET_REF
14959 || TREE_CODE (argument) == SCOPE_REF))
14960 /* A pointer-to-member. */
14962 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14964 else
14965 cp_parser_simulate_error (parser);
14967 if (cp_parser_parse_definitely (parser))
14969 if (address_p)
14970 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14971 tf_warning_or_error);
14972 else
14973 argument = convert_from_reference (argument);
14974 return argument;
14978 /* If the argument started with "&", there are no other valid
14979 alternatives at this point. */
14980 if (address_p)
14982 cp_parser_error (parser, "invalid non-type template argument");
14983 return error_mark_node;
14986 /* If the argument wasn't successfully parsed as a type-id followed
14987 by '>>', the argument can only be a constant expression now.
14988 Otherwise, we try parsing the constant-expression tentatively,
14989 because the argument could really be a type-id. */
14990 if (maybe_type_id)
14991 cp_parser_parse_tentatively (parser);
14992 argument = cp_parser_constant_expression (parser);
14994 if (!maybe_type_id)
14995 return argument;
14996 if (!cp_parser_next_token_ends_template_argument_p (parser))
14997 cp_parser_error (parser, "expected template-argument");
14998 if (cp_parser_parse_definitely (parser))
14999 return argument;
15000 /* We did our best to parse the argument as a non type-id, but that
15001 was the only alternative that matched (albeit with a '>' after
15002 it). We can assume it's just a typo from the user, and a
15003 diagnostic will then be issued. */
15004 return cp_parser_template_type_arg (parser);
15007 /* Parse an explicit-instantiation.
15009 explicit-instantiation:
15010 template declaration
15012 Although the standard says `declaration', what it really means is:
15014 explicit-instantiation:
15015 template decl-specifier-seq [opt] declarator [opt] ;
15017 Things like `template int S<int>::i = 5, int S<double>::j;' are not
15018 supposed to be allowed. A defect report has been filed about this
15019 issue.
15021 GNU Extension:
15023 explicit-instantiation:
15024 storage-class-specifier template
15025 decl-specifier-seq [opt] declarator [opt] ;
15026 function-specifier template
15027 decl-specifier-seq [opt] declarator [opt] ; */
15029 static void
15030 cp_parser_explicit_instantiation (cp_parser* parser)
15032 int declares_class_or_enum;
15033 cp_decl_specifier_seq decl_specifiers;
15034 tree extension_specifier = NULL_TREE;
15036 timevar_push (TV_TEMPLATE_INST);
15038 /* Look for an (optional) storage-class-specifier or
15039 function-specifier. */
15040 if (cp_parser_allow_gnu_extensions_p (parser))
15042 extension_specifier
15043 = cp_parser_storage_class_specifier_opt (parser);
15044 if (!extension_specifier)
15045 extension_specifier
15046 = cp_parser_function_specifier_opt (parser,
15047 /*decl_specs=*/NULL);
15050 /* Look for the `template' keyword. */
15051 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15052 /* Let the front end know that we are processing an explicit
15053 instantiation. */
15054 begin_explicit_instantiation ();
15055 /* [temp.explicit] says that we are supposed to ignore access
15056 control while processing explicit instantiation directives. */
15057 push_deferring_access_checks (dk_no_check);
15058 /* Parse a decl-specifier-seq. */
15059 cp_parser_decl_specifier_seq (parser,
15060 CP_PARSER_FLAGS_OPTIONAL,
15061 &decl_specifiers,
15062 &declares_class_or_enum);
15063 /* If there was exactly one decl-specifier, and it declared a class,
15064 and there's no declarator, then we have an explicit type
15065 instantiation. */
15066 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
15068 tree type;
15070 type = check_tag_decl (&decl_specifiers,
15071 /*explicit_type_instantiation_p=*/true);
15072 /* Turn access control back on for names used during
15073 template instantiation. */
15074 pop_deferring_access_checks ();
15075 if (type)
15076 do_type_instantiation (type, extension_specifier,
15077 /*complain=*/tf_error);
15079 else
15081 cp_declarator *declarator;
15082 tree decl;
15084 /* Parse the declarator. */
15085 declarator
15086 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15087 /*ctor_dtor_or_conv_p=*/NULL,
15088 /*parenthesized_p=*/NULL,
15089 /*member_p=*/false,
15090 /*friend_p=*/false);
15091 if (declares_class_or_enum & 2)
15092 cp_parser_check_for_definition_in_return_type (declarator,
15093 decl_specifiers.type,
15094 decl_specifiers.locations[ds_type_spec]);
15095 if (declarator != cp_error_declarator)
15097 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
15098 permerror (decl_specifiers.locations[ds_inline],
15099 "explicit instantiation shall not use"
15100 " %<inline%> specifier");
15101 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
15102 permerror (decl_specifiers.locations[ds_constexpr],
15103 "explicit instantiation shall not use"
15104 " %<constexpr%> specifier");
15106 decl = grokdeclarator (declarator, &decl_specifiers,
15107 NORMAL, 0, &decl_specifiers.attributes);
15108 /* Turn access control back on for names used during
15109 template instantiation. */
15110 pop_deferring_access_checks ();
15111 /* Do the explicit instantiation. */
15112 do_decl_instantiation (decl, extension_specifier);
15114 else
15116 pop_deferring_access_checks ();
15117 /* Skip the body of the explicit instantiation. */
15118 cp_parser_skip_to_end_of_statement (parser);
15121 /* We're done with the instantiation. */
15122 end_explicit_instantiation ();
15124 cp_parser_consume_semicolon_at_end_of_statement (parser);
15126 timevar_pop (TV_TEMPLATE_INST);
15129 /* Parse an explicit-specialization.
15131 explicit-specialization:
15132 template < > declaration
15134 Although the standard says `declaration', what it really means is:
15136 explicit-specialization:
15137 template <> decl-specifier [opt] init-declarator [opt] ;
15138 template <> function-definition
15139 template <> explicit-specialization
15140 template <> template-declaration */
15142 static void
15143 cp_parser_explicit_specialization (cp_parser* parser)
15145 bool need_lang_pop;
15146 cp_token *token = cp_lexer_peek_token (parser->lexer);
15148 /* Look for the `template' keyword. */
15149 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15150 /* Look for the `<'. */
15151 cp_parser_require (parser, CPP_LESS, RT_LESS);
15152 /* Look for the `>'. */
15153 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15154 /* We have processed another parameter list. */
15155 ++parser->num_template_parameter_lists;
15156 /* [temp]
15158 A template ... explicit specialization ... shall not have C
15159 linkage. */
15160 if (current_lang_name == lang_name_c)
15162 error_at (token->location, "template specialization with C linkage");
15163 /* Give it C++ linkage to avoid confusing other parts of the
15164 front end. */
15165 push_lang_context (lang_name_cplusplus);
15166 need_lang_pop = true;
15168 else
15169 need_lang_pop = false;
15170 /* Let the front end know that we are beginning a specialization. */
15171 if (!begin_specialization ())
15173 end_specialization ();
15174 return;
15177 /* If the next keyword is `template', we need to figure out whether
15178 or not we're looking a template-declaration. */
15179 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15181 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15182 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
15183 cp_parser_template_declaration_after_export (parser,
15184 /*member_p=*/false);
15185 else
15186 cp_parser_explicit_specialization (parser);
15188 else
15189 /* Parse the dependent declaration. */
15190 cp_parser_single_declaration (parser,
15191 /*checks=*/NULL,
15192 /*member_p=*/false,
15193 /*explicit_specialization_p=*/true,
15194 /*friend_p=*/NULL);
15195 /* We're done with the specialization. */
15196 end_specialization ();
15197 /* For the erroneous case of a template with C linkage, we pushed an
15198 implicit C++ linkage scope; exit that scope now. */
15199 if (need_lang_pop)
15200 pop_lang_context ();
15201 /* We're done with this parameter list. */
15202 --parser->num_template_parameter_lists;
15205 /* Parse a type-specifier.
15207 type-specifier:
15208 simple-type-specifier
15209 class-specifier
15210 enum-specifier
15211 elaborated-type-specifier
15212 cv-qualifier
15214 GNU Extension:
15216 type-specifier:
15217 __complex__
15219 Returns a representation of the type-specifier. For a
15220 class-specifier, enum-specifier, or elaborated-type-specifier, a
15221 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
15223 The parser flags FLAGS is used to control type-specifier parsing.
15225 If IS_DECLARATION is TRUE, then this type-specifier is appearing
15226 in a decl-specifier-seq.
15228 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
15229 class-specifier, enum-specifier, or elaborated-type-specifier, then
15230 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
15231 if a type is declared; 2 if it is defined. Otherwise, it is set to
15232 zero.
15234 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
15235 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
15236 is set to FALSE. */
15238 static tree
15239 cp_parser_type_specifier (cp_parser* parser,
15240 cp_parser_flags flags,
15241 cp_decl_specifier_seq *decl_specs,
15242 bool is_declaration,
15243 int* declares_class_or_enum,
15244 bool* is_cv_qualifier)
15246 tree type_spec = NULL_TREE;
15247 cp_token *token;
15248 enum rid keyword;
15249 cp_decl_spec ds = ds_last;
15251 /* Assume this type-specifier does not declare a new type. */
15252 if (declares_class_or_enum)
15253 *declares_class_or_enum = 0;
15254 /* And that it does not specify a cv-qualifier. */
15255 if (is_cv_qualifier)
15256 *is_cv_qualifier = false;
15257 /* Peek at the next token. */
15258 token = cp_lexer_peek_token (parser->lexer);
15260 /* If we're looking at a keyword, we can use that to guide the
15261 production we choose. */
15262 keyword = token->keyword;
15263 switch (keyword)
15265 case RID_ENUM:
15266 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15267 goto elaborated_type_specifier;
15269 /* Look for the enum-specifier. */
15270 type_spec = cp_parser_enum_specifier (parser);
15271 /* If that worked, we're done. */
15272 if (type_spec)
15274 if (declares_class_or_enum)
15275 *declares_class_or_enum = 2;
15276 if (decl_specs)
15277 cp_parser_set_decl_spec_type (decl_specs,
15278 type_spec,
15279 token,
15280 /*type_definition_p=*/true);
15281 return type_spec;
15283 else
15284 goto elaborated_type_specifier;
15286 /* Any of these indicate either a class-specifier, or an
15287 elaborated-type-specifier. */
15288 case RID_CLASS:
15289 case RID_STRUCT:
15290 case RID_UNION:
15291 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15292 goto elaborated_type_specifier;
15294 /* Parse tentatively so that we can back up if we don't find a
15295 class-specifier. */
15296 cp_parser_parse_tentatively (parser);
15297 /* Look for the class-specifier. */
15298 type_spec = cp_parser_class_specifier (parser);
15299 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
15300 /* If that worked, we're done. */
15301 if (cp_parser_parse_definitely (parser))
15303 if (declares_class_or_enum)
15304 *declares_class_or_enum = 2;
15305 if (decl_specs)
15306 cp_parser_set_decl_spec_type (decl_specs,
15307 type_spec,
15308 token,
15309 /*type_definition_p=*/true);
15310 return type_spec;
15313 /* Fall through. */
15314 elaborated_type_specifier:
15315 /* We're declaring (not defining) a class or enum. */
15316 if (declares_class_or_enum)
15317 *declares_class_or_enum = 1;
15319 /* Fall through. */
15320 case RID_TYPENAME:
15321 /* Look for an elaborated-type-specifier. */
15322 type_spec
15323 = (cp_parser_elaborated_type_specifier
15324 (parser,
15325 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
15326 is_declaration));
15327 if (decl_specs)
15328 cp_parser_set_decl_spec_type (decl_specs,
15329 type_spec,
15330 token,
15331 /*type_definition_p=*/false);
15332 return type_spec;
15334 case RID_CONST:
15335 ds = ds_const;
15336 if (is_cv_qualifier)
15337 *is_cv_qualifier = true;
15338 break;
15340 case RID_VOLATILE:
15341 ds = ds_volatile;
15342 if (is_cv_qualifier)
15343 *is_cv_qualifier = true;
15344 break;
15346 case RID_RESTRICT:
15347 ds = ds_restrict;
15348 if (is_cv_qualifier)
15349 *is_cv_qualifier = true;
15350 break;
15352 case RID_COMPLEX:
15353 /* The `__complex__' keyword is a GNU extension. */
15354 ds = ds_complex;
15355 break;
15357 default:
15358 break;
15361 /* Handle simple keywords. */
15362 if (ds != ds_last)
15364 if (decl_specs)
15366 set_and_check_decl_spec_loc (decl_specs, ds, token);
15367 decl_specs->any_specifiers_p = true;
15369 return cp_lexer_consume_token (parser->lexer)->u.value;
15372 /* If we do not already have a type-specifier, assume we are looking
15373 at a simple-type-specifier. */
15374 type_spec = cp_parser_simple_type_specifier (parser,
15375 decl_specs,
15376 flags);
15378 /* If we didn't find a type-specifier, and a type-specifier was not
15379 optional in this context, issue an error message. */
15380 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15382 cp_parser_error (parser, "expected type specifier");
15383 return error_mark_node;
15386 return type_spec;
15389 /* Parse a simple-type-specifier.
15391 simple-type-specifier:
15392 :: [opt] nested-name-specifier [opt] type-name
15393 :: [opt] nested-name-specifier template template-id
15394 char
15395 wchar_t
15396 bool
15397 short
15399 long
15400 signed
15401 unsigned
15402 float
15403 double
15404 void
15406 C++0x Extension:
15408 simple-type-specifier:
15409 auto
15410 decltype ( expression )
15411 char16_t
15412 char32_t
15413 __underlying_type ( type-id )
15415 GNU Extension:
15417 simple-type-specifier:
15418 __int128
15419 __typeof__ unary-expression
15420 __typeof__ ( type-id )
15421 __typeof__ ( type-id ) { initializer-list , [opt] }
15423 Concepts Extension:
15425 simple-type-specifier:
15426 constrained-type-specifier
15428 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
15429 appropriately updated. */
15431 static tree
15432 cp_parser_simple_type_specifier (cp_parser* parser,
15433 cp_decl_specifier_seq *decl_specs,
15434 cp_parser_flags flags)
15436 tree type = NULL_TREE;
15437 cp_token *token;
15438 int idx;
15440 /* Peek at the next token. */
15441 token = cp_lexer_peek_token (parser->lexer);
15443 /* If we're looking at a keyword, things are easy. */
15444 switch (token->keyword)
15446 case RID_CHAR:
15447 if (decl_specs)
15448 decl_specs->explicit_char_p = true;
15449 type = char_type_node;
15450 break;
15451 case RID_CHAR16:
15452 type = char16_type_node;
15453 break;
15454 case RID_CHAR32:
15455 type = char32_type_node;
15456 break;
15457 case RID_WCHAR:
15458 type = wchar_type_node;
15459 break;
15460 case RID_BOOL:
15461 type = boolean_type_node;
15462 break;
15463 case RID_SHORT:
15464 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
15465 type = short_integer_type_node;
15466 break;
15467 case RID_INT:
15468 if (decl_specs)
15469 decl_specs->explicit_int_p = true;
15470 type = integer_type_node;
15471 break;
15472 case RID_INT_N_0:
15473 case RID_INT_N_1:
15474 case RID_INT_N_2:
15475 case RID_INT_N_3:
15476 idx = token->keyword - RID_INT_N_0;
15477 if (! int_n_enabled_p [idx])
15478 break;
15479 if (decl_specs)
15481 decl_specs->explicit_intN_p = true;
15482 decl_specs->int_n_idx = idx;
15484 type = int_n_trees [idx].signed_type;
15485 break;
15486 case RID_LONG:
15487 if (decl_specs)
15488 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
15489 type = long_integer_type_node;
15490 break;
15491 case RID_SIGNED:
15492 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
15493 type = integer_type_node;
15494 break;
15495 case RID_UNSIGNED:
15496 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
15497 type = unsigned_type_node;
15498 break;
15499 case RID_FLOAT:
15500 type = float_type_node;
15501 break;
15502 case RID_DOUBLE:
15503 type = double_type_node;
15504 break;
15505 case RID_VOID:
15506 type = void_type_node;
15507 break;
15509 case RID_AUTO:
15510 maybe_warn_cpp0x (CPP0X_AUTO);
15511 if (parser->auto_is_implicit_function_template_parm_p)
15513 /* The 'auto' might be the placeholder return type for a function decl
15514 with trailing return type. */
15515 bool have_trailing_return_fn_decl = false;
15516 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15517 == CPP_OPEN_PAREN)
15519 cp_parser_parse_tentatively (parser);
15520 cp_lexer_consume_token (parser->lexer);
15521 cp_lexer_consume_token (parser->lexer);
15522 if (cp_parser_skip_to_closing_parenthesis (parser,
15523 /*recovering*/false,
15524 /*or_comma*/false,
15525 /*consume_paren*/true))
15526 have_trailing_return_fn_decl
15527 = cp_lexer_next_token_is (parser->lexer, CPP_DEREF);
15528 cp_parser_abort_tentative_parse (parser);
15531 if (have_trailing_return_fn_decl)
15533 type = make_auto ();
15534 break;
15537 if (cxx_dialect >= cxx14)
15538 type = synthesize_implicit_template_parm (parser, NULL_TREE);
15539 else
15540 type = error_mark_node;
15542 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
15544 if (cxx_dialect < cxx14)
15545 error_at (token->location,
15546 "use of %<auto%> in lambda parameter declaration "
15547 "only available with "
15548 "-std=c++14 or -std=gnu++14");
15550 else if (cxx_dialect < cxx14)
15551 error_at (token->location,
15552 "use of %<auto%> in parameter declaration "
15553 "only available with "
15554 "-std=c++14 or -std=gnu++14");
15555 else
15556 pedwarn (token->location, OPT_Wpedantic,
15557 "ISO C++ forbids use of %<auto%> in parameter "
15558 "declaration");
15560 else
15561 type = make_auto ();
15562 break;
15564 case RID_DECLTYPE:
15565 /* Since DR 743, decltype can either be a simple-type-specifier by
15566 itself or begin a nested-name-specifier. Parsing it will replace
15567 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
15568 handling below decide what to do. */
15569 cp_parser_decltype (parser);
15570 cp_lexer_set_token_position (parser->lexer, token);
15571 break;
15573 case RID_TYPEOF:
15574 /* Consume the `typeof' token. */
15575 cp_lexer_consume_token (parser->lexer);
15576 /* Parse the operand to `typeof'. */
15577 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
15578 /* If it is not already a TYPE, take its type. */
15579 if (!TYPE_P (type))
15580 type = finish_typeof (type);
15582 if (decl_specs)
15583 cp_parser_set_decl_spec_type (decl_specs, type,
15584 token,
15585 /*type_definition_p=*/false);
15587 return type;
15589 case RID_UNDERLYING_TYPE:
15590 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
15591 if (decl_specs)
15592 cp_parser_set_decl_spec_type (decl_specs, type,
15593 token,
15594 /*type_definition_p=*/false);
15596 return type;
15598 case RID_BASES:
15599 case RID_DIRECT_BASES:
15600 type = cp_parser_trait_expr (parser, token->keyword);
15601 if (decl_specs)
15602 cp_parser_set_decl_spec_type (decl_specs, type,
15603 token,
15604 /*type_definition_p=*/false);
15605 return type;
15606 default:
15607 break;
15610 /* If token is an already-parsed decltype not followed by ::,
15611 it's a simple-type-specifier. */
15612 if (token->type == CPP_DECLTYPE
15613 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
15615 type = token->u.value;
15616 if (decl_specs)
15618 cp_parser_set_decl_spec_type (decl_specs, type,
15619 token,
15620 /*type_definition_p=*/false);
15621 /* Remember that we are handling a decltype in order to
15622 implement the resolution of DR 1510 when the argument
15623 isn't instantiation dependent. */
15624 decl_specs->decltype_p = true;
15626 cp_lexer_consume_token (parser->lexer);
15627 return type;
15630 /* If the type-specifier was for a built-in type, we're done. */
15631 if (type)
15633 /* Record the type. */
15634 if (decl_specs
15635 && (token->keyword != RID_SIGNED
15636 && token->keyword != RID_UNSIGNED
15637 && token->keyword != RID_SHORT
15638 && token->keyword != RID_LONG))
15639 cp_parser_set_decl_spec_type (decl_specs,
15640 type,
15641 token,
15642 /*type_definition_p=*/false);
15643 if (decl_specs)
15644 decl_specs->any_specifiers_p = true;
15646 /* Consume the token. */
15647 cp_lexer_consume_token (parser->lexer);
15649 if (type == error_mark_node)
15650 return error_mark_node;
15652 /* There is no valid C++ program where a non-template type is
15653 followed by a "<". That usually indicates that the user thought
15654 that the type was a template. */
15655 cp_parser_check_for_invalid_template_id (parser, type, none_type,
15656 token->location);
15658 return TYPE_NAME (type);
15661 /* The type-specifier must be a user-defined type. */
15662 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
15664 bool qualified_p;
15665 bool global_p;
15667 /* Don't gobble tokens or issue error messages if this is an
15668 optional type-specifier. */
15669 if (flags & CP_PARSER_FLAGS_OPTIONAL)
15670 cp_parser_parse_tentatively (parser);
15672 /* Look for the optional `::' operator. */
15673 global_p
15674 = (cp_parser_global_scope_opt (parser,
15675 /*current_scope_valid_p=*/false)
15676 != NULL_TREE);
15677 /* Look for the nested-name specifier. */
15678 qualified_p
15679 = (cp_parser_nested_name_specifier_opt (parser,
15680 /*typename_keyword_p=*/false,
15681 /*check_dependency_p=*/true,
15682 /*type_p=*/false,
15683 /*is_declaration=*/false)
15684 != NULL_TREE);
15685 token = cp_lexer_peek_token (parser->lexer);
15686 /* If we have seen a nested-name-specifier, and the next token
15687 is `template', then we are using the template-id production. */
15688 if (parser->scope
15689 && cp_parser_optional_template_keyword (parser))
15691 /* Look for the template-id. */
15692 type = cp_parser_template_id (parser,
15693 /*template_keyword_p=*/true,
15694 /*check_dependency_p=*/true,
15695 none_type,
15696 /*is_declaration=*/false);
15697 /* If the template-id did not name a type, we are out of
15698 luck. */
15699 if (TREE_CODE (type) != TYPE_DECL)
15701 cp_parser_error (parser, "expected template-id for type");
15702 type = NULL_TREE;
15705 /* Otherwise, look for a type-name. */
15706 else
15707 type = cp_parser_type_name (parser);
15708 /* Keep track of all name-lookups performed in class scopes. */
15709 if (type
15710 && !global_p
15711 && !qualified_p
15712 && TREE_CODE (type) == TYPE_DECL
15713 && identifier_p (DECL_NAME (type)))
15714 maybe_note_name_used_in_class (DECL_NAME (type), type);
15715 /* If it didn't work out, we don't have a TYPE. */
15716 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
15717 && !cp_parser_parse_definitely (parser))
15718 type = NULL_TREE;
15719 if (type && decl_specs)
15720 cp_parser_set_decl_spec_type (decl_specs, type,
15721 token,
15722 /*type_definition_p=*/false);
15725 /* If we didn't get a type-name, issue an error message. */
15726 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15728 cp_parser_error (parser, "expected type-name");
15729 return error_mark_node;
15732 if (type && type != error_mark_node)
15734 /* See if TYPE is an Objective-C type, and if so, parse and
15735 accept any protocol references following it. Do this before
15736 the cp_parser_check_for_invalid_template_id() call, because
15737 Objective-C types can be followed by '<...>' which would
15738 enclose protocol names rather than template arguments, and so
15739 everything is fine. */
15740 if (c_dialect_objc () && !parser->scope
15741 && (objc_is_id (type) || objc_is_class_name (type)))
15743 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15744 tree qual_type = objc_get_protocol_qualified_type (type, protos);
15746 /* Clobber the "unqualified" type previously entered into
15747 DECL_SPECS with the new, improved protocol-qualified version. */
15748 if (decl_specs)
15749 decl_specs->type = qual_type;
15751 return qual_type;
15754 /* There is no valid C++ program where a non-template type is
15755 followed by a "<". That usually indicates that the user
15756 thought that the type was a template. */
15757 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
15758 none_type,
15759 token->location);
15762 return type;
15765 /* Parse a type-name.
15767 type-name:
15768 class-name
15769 enum-name
15770 typedef-name
15771 simple-template-id [in c++0x]
15773 enum-name:
15774 identifier
15776 typedef-name:
15777 identifier
15779 Concepts:
15781 type-name:
15782 concept-name
15783 partial-concept-id
15785 concept-name:
15786 identifier
15788 Returns a TYPE_DECL for the type. */
15790 static tree
15791 cp_parser_type_name (cp_parser* parser)
15793 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
15796 /* See above. */
15797 static tree
15798 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
15800 tree type_decl;
15802 /* We can't know yet whether it is a class-name or not. */
15803 cp_parser_parse_tentatively (parser);
15804 /* Try a class-name. */
15805 type_decl = cp_parser_class_name (parser,
15806 typename_keyword_p,
15807 /*template_keyword_p=*/false,
15808 none_type,
15809 /*check_dependency_p=*/true,
15810 /*class_head_p=*/false,
15811 /*is_declaration=*/false);
15812 /* If it's not a class-name, keep looking. */
15813 if (!cp_parser_parse_definitely (parser))
15815 if (cxx_dialect < cxx11)
15816 /* It must be a typedef-name or an enum-name. */
15817 return cp_parser_nonclass_name (parser);
15819 cp_parser_parse_tentatively (parser);
15820 /* It is either a simple-template-id representing an
15821 instantiation of an alias template... */
15822 type_decl = cp_parser_template_id (parser,
15823 /*template_keyword_p=*/false,
15824 /*check_dependency_p=*/true,
15825 none_type,
15826 /*is_declaration=*/false);
15827 /* Note that this must be an instantiation of an alias template
15828 because [temp.names]/6 says:
15830 A template-id that names an alias template specialization
15831 is a type-name.
15833 Whereas [temp.names]/7 says:
15835 A simple-template-id that names a class template
15836 specialization is a class-name.
15838 With concepts, this could also be a partial-concept-id that
15839 declares a non-type template parameter. */
15840 if (type_decl != NULL_TREE
15841 && TREE_CODE (type_decl) == TYPE_DECL
15842 && TYPE_DECL_ALIAS_P (type_decl))
15843 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
15844 else if (is_constrained_parameter (type_decl))
15845 /* Don't do anything. */ ;
15846 else
15847 cp_parser_simulate_error (parser);
15849 if (!cp_parser_parse_definitely (parser))
15850 /* ... Or a typedef-name or an enum-name. */
15851 return cp_parser_nonclass_name (parser);
15854 return type_decl;
15857 /* Returns true if proto is a type parameter, but not a template
15858 template parameter. */
15859 static bool
15860 check_type_concept (tree fn, tree proto)
15862 if (TREE_CODE (proto) != TYPE_DECL)
15864 error ("invalid use of non-type concept %qD", fn);
15865 return false;
15867 return true;
15870 /* Check if DECL and ARGS can form a constrained-type-specifier.
15871 If ARGS is non-null, we try to form a concept check of the
15872 form DECL<?, ARGS> where ? is a wildcard that matches any
15873 kind of template argument. If ARGS is NULL, then we try to
15874 form a concept check of the form DECL<?>. */
15876 static tree
15877 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
15878 tree decl, tree args)
15880 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
15882 /* If we a constrained-type-specifier cannot be deduced. */
15883 if (parser->prevent_constrained_type_specifiers)
15884 return NULL_TREE;
15886 /* A constrained type specifier can only be found in an
15887 overload set or as a reference to a template declaration.
15889 FIXME: This might be masking a bug. It's possible that
15890 that the deduction below is causing template specializations
15891 to be formed with the wildcard as an argument. */
15892 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
15893 return NULL_TREE;
15895 /* Try to build a call expression that evaluates the
15896 concept. This can fail if the overload set refers
15897 only to non-templates. */
15898 tree placeholder = build_nt (WILDCARD_DECL);
15899 tree check = build_concept_check (decl, placeholder, args);
15900 if (check == error_mark_node)
15901 return NULL_TREE;
15903 /* Deduce the checked constraint and the prototype parameter.
15905 FIXME: In certain cases, failure to deduce should be a
15906 diagnosable error. */
15907 tree conc;
15908 tree proto;
15909 if (!deduce_constrained_parameter (check, conc, proto))
15910 return NULL_TREE;
15912 /* In template parameter scope, this results in a constrained
15913 parameter. Return a descriptor of that parm. */
15914 if (processing_template_parmlist)
15915 return build_constrained_parameter (conc, proto, args);
15917 /* In any other context, a concept must be a type concept.
15919 FIXME: A constrained-type-specifier can be a placeholder
15920 of any kind. */
15921 if (!check_type_concept (conc, proto))
15922 return error_mark_node;
15924 /* In a parameter-declaration-clause, constrained-type
15925 specifiers result in invented template parameters. */
15926 if (parser->auto_is_implicit_function_template_parm_p)
15928 tree x = build_constrained_parameter (conc, proto, args);
15929 return synthesize_implicit_template_parm (parser, x);
15931 else
15933 /* Otherwise, we're in a context where the constrained
15934 type name is deduced and the constraint applies
15935 after deduction. */
15936 return make_constrained_auto (conc, args);
15939 return NULL_TREE;
15942 /* If DECL refers to a concept, return a TYPE_DECL representing
15943 the result of using the constrained type specifier in the
15944 current context. DECL refers to a concept if
15946 - it is an overload set containing a function concept taking a single
15947 type argument, or
15949 - it is a variable concept taking a single type argument. */
15951 static tree
15952 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
15954 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
15957 /* Check if DECL and ARGS form a partial-concept-id. If so,
15958 assign ID to the resulting constrained placeholder.
15960 Returns true if the partial-concept-id designates a placeholder
15961 and false otherwise. Note that *id is set to NULL_TREE in
15962 this case. */
15964 static tree
15965 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
15967 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
15970 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
15971 or a concept-name.
15973 enum-name:
15974 identifier
15976 typedef-name:
15977 identifier
15979 concept-name:
15980 identifier
15982 Returns a TYPE_DECL for the type. */
15984 static tree
15985 cp_parser_nonclass_name (cp_parser* parser)
15987 tree type_decl;
15988 tree identifier;
15990 cp_token *token = cp_lexer_peek_token (parser->lexer);
15991 identifier = cp_parser_identifier (parser);
15992 if (identifier == error_mark_node)
15993 return error_mark_node;
15995 /* Look up the type-name. */
15996 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
15998 type_decl = strip_using_decl (type_decl);
16000 /* If we found an overload set, then it may refer to a concept-name. */
16001 if (flag_concepts
16002 && (TREE_CODE (type_decl) == OVERLOAD
16003 || BASELINK_P (type_decl)
16004 || variable_concept_p (type_decl)))
16006 /* Determine whether the overload refers to a concept. */
16007 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
16008 return decl;
16011 if (TREE_CODE (type_decl) != TYPE_DECL
16012 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
16014 /* See if this is an Objective-C type. */
16015 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16016 tree type = objc_get_protocol_qualified_type (identifier, protos);
16017 if (type)
16018 type_decl = TYPE_NAME (type);
16021 /* Issue an error if we did not find a type-name. */
16022 if (TREE_CODE (type_decl) != TYPE_DECL
16023 /* In Objective-C, we have the complication that class names are
16024 normally type names and start declarations (eg, the
16025 "NSObject" in "NSObject *object;"), but can be used in an
16026 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
16027 is an expression. So, a classname followed by a dot is not a
16028 valid type-name. */
16029 || (objc_is_class_name (TREE_TYPE (type_decl))
16030 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
16032 if (!cp_parser_simulate_error (parser))
16033 cp_parser_name_lookup_error (parser, identifier, type_decl,
16034 NLE_TYPE, token->location);
16035 return error_mark_node;
16037 /* Remember that the name was used in the definition of the
16038 current class so that we can check later to see if the
16039 meaning would have been different after the class was
16040 entirely defined. */
16041 else if (type_decl != error_mark_node
16042 && !parser->scope)
16043 maybe_note_name_used_in_class (identifier, type_decl);
16045 return type_decl;
16048 /* Parse an elaborated-type-specifier. Note that the grammar given
16049 here incorporates the resolution to DR68.
16051 elaborated-type-specifier:
16052 class-key :: [opt] nested-name-specifier [opt] identifier
16053 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
16054 enum-key :: [opt] nested-name-specifier [opt] identifier
16055 typename :: [opt] nested-name-specifier identifier
16056 typename :: [opt] nested-name-specifier template [opt]
16057 template-id
16059 GNU extension:
16061 elaborated-type-specifier:
16062 class-key attributes :: [opt] nested-name-specifier [opt] identifier
16063 class-key attributes :: [opt] nested-name-specifier [opt]
16064 template [opt] template-id
16065 enum attributes :: [opt] nested-name-specifier [opt] identifier
16067 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
16068 declared `friend'. If IS_DECLARATION is TRUE, then this
16069 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
16070 something is being declared.
16072 Returns the TYPE specified. */
16074 static tree
16075 cp_parser_elaborated_type_specifier (cp_parser* parser,
16076 bool is_friend,
16077 bool is_declaration)
16079 enum tag_types tag_type;
16080 tree identifier;
16081 tree type = NULL_TREE;
16082 tree attributes = NULL_TREE;
16083 tree globalscope;
16084 cp_token *token = NULL;
16086 /* See if we're looking at the `enum' keyword. */
16087 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
16089 /* Consume the `enum' token. */
16090 cp_lexer_consume_token (parser->lexer);
16091 /* Remember that it's an enumeration type. */
16092 tag_type = enum_type;
16093 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
16094 enums) is used here. */
16095 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
16096 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
16098 pedwarn (input_location, 0, "elaborated-type-specifier "
16099 "for a scoped enum must not use the %<%D%> keyword",
16100 cp_lexer_peek_token (parser->lexer)->u.value);
16101 /* Consume the `struct' or `class' and parse it anyway. */
16102 cp_lexer_consume_token (parser->lexer);
16104 /* Parse the attributes. */
16105 attributes = cp_parser_attributes_opt (parser);
16107 /* Or, it might be `typename'. */
16108 else if (cp_lexer_next_token_is_keyword (parser->lexer,
16109 RID_TYPENAME))
16111 /* Consume the `typename' token. */
16112 cp_lexer_consume_token (parser->lexer);
16113 /* Remember that it's a `typename' type. */
16114 tag_type = typename_type;
16116 /* Otherwise it must be a class-key. */
16117 else
16119 tag_type = cp_parser_class_key (parser);
16120 if (tag_type == none_type)
16121 return error_mark_node;
16122 /* Parse the attributes. */
16123 attributes = cp_parser_attributes_opt (parser);
16126 /* Look for the `::' operator. */
16127 globalscope = cp_parser_global_scope_opt (parser,
16128 /*current_scope_valid_p=*/false);
16129 /* Look for the nested-name-specifier. */
16130 if (tag_type == typename_type && !globalscope)
16132 if (!cp_parser_nested_name_specifier (parser,
16133 /*typename_keyword_p=*/true,
16134 /*check_dependency_p=*/true,
16135 /*type_p=*/true,
16136 is_declaration))
16137 return error_mark_node;
16139 else
16140 /* Even though `typename' is not present, the proposed resolution
16141 to Core Issue 180 says that in `class A<T>::B', `B' should be
16142 considered a type-name, even if `A<T>' is dependent. */
16143 cp_parser_nested_name_specifier_opt (parser,
16144 /*typename_keyword_p=*/true,
16145 /*check_dependency_p=*/true,
16146 /*type_p=*/true,
16147 is_declaration);
16148 /* For everything but enumeration types, consider a template-id.
16149 For an enumeration type, consider only a plain identifier. */
16150 if (tag_type != enum_type)
16152 bool template_p = false;
16153 tree decl;
16155 /* Allow the `template' keyword. */
16156 template_p = cp_parser_optional_template_keyword (parser);
16157 /* If we didn't see `template', we don't know if there's a
16158 template-id or not. */
16159 if (!template_p)
16160 cp_parser_parse_tentatively (parser);
16161 /* Parse the template-id. */
16162 token = cp_lexer_peek_token (parser->lexer);
16163 decl = cp_parser_template_id (parser, template_p,
16164 /*check_dependency_p=*/true,
16165 tag_type,
16166 is_declaration);
16167 /* If we didn't find a template-id, look for an ordinary
16168 identifier. */
16169 if (!template_p && !cp_parser_parse_definitely (parser))
16171 /* We can get here when cp_parser_template_id, called by
16172 cp_parser_class_name with tag_type == none_type, succeeds
16173 and caches a BASELINK. Then, when called again here,
16174 instead of failing and returning an error_mark_node
16175 returns it (see template/typename17.C in C++11).
16176 ??? Could we diagnose this earlier? */
16177 else if (tag_type == typename_type && BASELINK_P (decl))
16179 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
16180 type = error_mark_node;
16182 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
16183 in effect, then we must assume that, upon instantiation, the
16184 template will correspond to a class. */
16185 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16186 && tag_type == typename_type)
16187 type = make_typename_type (parser->scope, decl,
16188 typename_type,
16189 /*complain=*/tf_error);
16190 /* If the `typename' keyword is in effect and DECL is not a type
16191 decl, then type is non existent. */
16192 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
16194 else if (TREE_CODE (decl) == TYPE_DECL)
16195 type = check_elaborated_type_specifier (tag_type, decl,
16196 /*allow_template_p=*/true);
16197 else if (decl == error_mark_node)
16198 type = error_mark_node;
16201 if (!type)
16203 token = cp_lexer_peek_token (parser->lexer);
16204 identifier = cp_parser_identifier (parser);
16206 if (identifier == error_mark_node)
16208 parser->scope = NULL_TREE;
16209 return error_mark_node;
16212 /* For a `typename', we needn't call xref_tag. */
16213 if (tag_type == typename_type
16214 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
16215 return cp_parser_make_typename_type (parser, identifier,
16216 token->location);
16218 /* Template parameter lists apply only if we are not within a
16219 function parameter list. */
16220 bool template_parm_lists_apply
16221 = parser->num_template_parameter_lists;
16222 if (template_parm_lists_apply)
16223 for (cp_binding_level *s = current_binding_level;
16224 s && s->kind != sk_template_parms;
16225 s = s->level_chain)
16226 if (s->kind == sk_function_parms)
16227 template_parm_lists_apply = false;
16229 /* Look up a qualified name in the usual way. */
16230 if (parser->scope)
16232 tree decl;
16233 tree ambiguous_decls;
16235 decl = cp_parser_lookup_name (parser, identifier,
16236 tag_type,
16237 /*is_template=*/false,
16238 /*is_namespace=*/false,
16239 /*check_dependency=*/true,
16240 &ambiguous_decls,
16241 token->location);
16243 /* If the lookup was ambiguous, an error will already have been
16244 issued. */
16245 if (ambiguous_decls)
16246 return error_mark_node;
16248 /* If we are parsing friend declaration, DECL may be a
16249 TEMPLATE_DECL tree node here. However, we need to check
16250 whether this TEMPLATE_DECL results in valid code. Consider
16251 the following example:
16253 namespace N {
16254 template <class T> class C {};
16256 class X {
16257 template <class T> friend class N::C; // #1, valid code
16259 template <class T> class Y {
16260 friend class N::C; // #2, invalid code
16263 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
16264 name lookup of `N::C'. We see that friend declaration must
16265 be template for the code to be valid. Note that
16266 processing_template_decl does not work here since it is
16267 always 1 for the above two cases. */
16269 decl = (cp_parser_maybe_treat_template_as_class
16270 (decl, /*tag_name_p=*/is_friend
16271 && template_parm_lists_apply));
16273 if (TREE_CODE (decl) != TYPE_DECL)
16275 cp_parser_diagnose_invalid_type_name (parser,
16276 identifier,
16277 token->location);
16278 return error_mark_node;
16281 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
16283 bool allow_template = (template_parm_lists_apply
16284 || DECL_SELF_REFERENCE_P (decl));
16285 type = check_elaborated_type_specifier (tag_type, decl,
16286 allow_template);
16288 if (type == error_mark_node)
16289 return error_mark_node;
16292 /* Forward declarations of nested types, such as
16294 class C1::C2;
16295 class C1::C2::C3;
16297 are invalid unless all components preceding the final '::'
16298 are complete. If all enclosing types are complete, these
16299 declarations become merely pointless.
16301 Invalid forward declarations of nested types are errors
16302 caught elsewhere in parsing. Those that are pointless arrive
16303 here. */
16305 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16306 && !is_friend && !processing_explicit_instantiation)
16307 warning (0, "declaration %qD does not declare anything", decl);
16309 type = TREE_TYPE (decl);
16311 else
16313 /* An elaborated-type-specifier sometimes introduces a new type and
16314 sometimes names an existing type. Normally, the rule is that it
16315 introduces a new type only if there is not an existing type of
16316 the same name already in scope. For example, given:
16318 struct S {};
16319 void f() { struct S s; }
16321 the `struct S' in the body of `f' is the same `struct S' as in
16322 the global scope; the existing definition is used. However, if
16323 there were no global declaration, this would introduce a new
16324 local class named `S'.
16326 An exception to this rule applies to the following code:
16328 namespace N { struct S; }
16330 Here, the elaborated-type-specifier names a new type
16331 unconditionally; even if there is already an `S' in the
16332 containing scope this declaration names a new type.
16333 This exception only applies if the elaborated-type-specifier
16334 forms the complete declaration:
16336 [class.name]
16338 A declaration consisting solely of `class-key identifier ;' is
16339 either a redeclaration of the name in the current scope or a
16340 forward declaration of the identifier as a class name. It
16341 introduces the name into the current scope.
16343 We are in this situation precisely when the next token is a `;'.
16345 An exception to the exception is that a `friend' declaration does
16346 *not* name a new type; i.e., given:
16348 struct S { friend struct T; };
16350 `T' is not a new type in the scope of `S'.
16352 Also, `new struct S' or `sizeof (struct S)' never results in the
16353 definition of a new type; a new type can only be declared in a
16354 declaration context. */
16356 tag_scope ts;
16357 bool template_p;
16359 if (is_friend)
16360 /* Friends have special name lookup rules. */
16361 ts = ts_within_enclosing_non_class;
16362 else if (is_declaration
16363 && cp_lexer_next_token_is (parser->lexer,
16364 CPP_SEMICOLON))
16365 /* This is a `class-key identifier ;' */
16366 ts = ts_current;
16367 else
16368 ts = ts_global;
16370 template_p =
16371 (template_parm_lists_apply
16372 && (cp_parser_next_token_starts_class_definition_p (parser)
16373 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
16374 /* An unqualified name was used to reference this type, so
16375 there were no qualifying templates. */
16376 if (template_parm_lists_apply
16377 && !cp_parser_check_template_parameters (parser,
16378 /*num_templates=*/0,
16379 token->location,
16380 /*declarator=*/NULL))
16381 return error_mark_node;
16382 type = xref_tag (tag_type, identifier, ts, template_p);
16386 if (type == error_mark_node)
16387 return error_mark_node;
16389 /* Allow attributes on forward declarations of classes. */
16390 if (attributes)
16392 if (TREE_CODE (type) == TYPENAME_TYPE)
16393 warning (OPT_Wattributes,
16394 "attributes ignored on uninstantiated type");
16395 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
16396 && ! processing_explicit_instantiation)
16397 warning (OPT_Wattributes,
16398 "attributes ignored on template instantiation");
16399 else if (is_declaration && cp_parser_declares_only_class_p (parser))
16400 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
16401 else
16402 warning (OPT_Wattributes,
16403 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
16406 if (tag_type != enum_type)
16408 /* Indicate whether this class was declared as a `class' or as a
16409 `struct'. */
16410 if (TREE_CODE (type) == RECORD_TYPE)
16411 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
16412 cp_parser_check_class_key (tag_type, type);
16415 /* A "<" cannot follow an elaborated type specifier. If that
16416 happens, the user was probably trying to form a template-id. */
16417 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
16418 token->location);
16420 return type;
16423 /* Parse an enum-specifier.
16425 enum-specifier:
16426 enum-head { enumerator-list [opt] }
16427 enum-head { enumerator-list , } [C++0x]
16429 enum-head:
16430 enum-key identifier [opt] enum-base [opt]
16431 enum-key nested-name-specifier identifier enum-base [opt]
16433 enum-key:
16434 enum
16435 enum class [C++0x]
16436 enum struct [C++0x]
16438 enum-base: [C++0x]
16439 : type-specifier-seq
16441 opaque-enum-specifier:
16442 enum-key identifier enum-base [opt] ;
16444 GNU Extensions:
16445 enum-key attributes[opt] identifier [opt] enum-base [opt]
16446 { enumerator-list [opt] }attributes[opt]
16447 enum-key attributes[opt] identifier [opt] enum-base [opt]
16448 { enumerator-list, }attributes[opt] [C++0x]
16450 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
16451 if the token stream isn't an enum-specifier after all. */
16453 static tree
16454 cp_parser_enum_specifier (cp_parser* parser)
16456 tree identifier;
16457 tree type = NULL_TREE;
16458 tree prev_scope;
16459 tree nested_name_specifier = NULL_TREE;
16460 tree attributes;
16461 bool scoped_enum_p = false;
16462 bool has_underlying_type = false;
16463 bool nested_being_defined = false;
16464 bool new_value_list = false;
16465 bool is_new_type = false;
16466 bool is_anonymous = false;
16467 tree underlying_type = NULL_TREE;
16468 cp_token *type_start_token = NULL;
16469 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
16471 parser->colon_corrects_to_scope_p = false;
16473 /* Parse tentatively so that we can back up if we don't find a
16474 enum-specifier. */
16475 cp_parser_parse_tentatively (parser);
16477 /* Caller guarantees that the current token is 'enum', an identifier
16478 possibly follows, and the token after that is an opening brace.
16479 If we don't have an identifier, fabricate an anonymous name for
16480 the enumeration being defined. */
16481 cp_lexer_consume_token (parser->lexer);
16483 /* Parse the "class" or "struct", which indicates a scoped
16484 enumeration type in C++0x. */
16485 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
16486 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
16488 if (cxx_dialect < cxx11)
16489 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
16491 /* Consume the `struct' or `class' token. */
16492 cp_lexer_consume_token (parser->lexer);
16494 scoped_enum_p = true;
16497 attributes = cp_parser_attributes_opt (parser);
16499 /* Clear the qualification. */
16500 parser->scope = NULL_TREE;
16501 parser->qualifying_scope = NULL_TREE;
16502 parser->object_scope = NULL_TREE;
16504 /* Figure out in what scope the declaration is being placed. */
16505 prev_scope = current_scope ();
16507 type_start_token = cp_lexer_peek_token (parser->lexer);
16509 push_deferring_access_checks (dk_no_check);
16510 nested_name_specifier
16511 = cp_parser_nested_name_specifier_opt (parser,
16512 /*typename_keyword_p=*/true,
16513 /*check_dependency_p=*/false,
16514 /*type_p=*/false,
16515 /*is_declaration=*/false);
16517 if (nested_name_specifier)
16519 tree name;
16521 identifier = cp_parser_identifier (parser);
16522 name = cp_parser_lookup_name (parser, identifier,
16523 enum_type,
16524 /*is_template=*/false,
16525 /*is_namespace=*/false,
16526 /*check_dependency=*/true,
16527 /*ambiguous_decls=*/NULL,
16528 input_location);
16529 if (name && name != error_mark_node)
16531 type = TREE_TYPE (name);
16532 if (TREE_CODE (type) == TYPENAME_TYPE)
16534 /* Are template enums allowed in ISO? */
16535 if (template_parm_scope_p ())
16536 pedwarn (type_start_token->location, OPT_Wpedantic,
16537 "%qD is an enumeration template", name);
16538 /* ignore a typename reference, for it will be solved by name
16539 in start_enum. */
16540 type = NULL_TREE;
16543 else if (nested_name_specifier == error_mark_node)
16544 /* We already issued an error. */;
16545 else
16546 error_at (type_start_token->location,
16547 "%qD is not an enumerator-name", identifier);
16549 else
16551 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16552 identifier = cp_parser_identifier (parser);
16553 else
16555 identifier = make_anon_name ();
16556 is_anonymous = true;
16557 if (scoped_enum_p)
16558 error_at (type_start_token->location,
16559 "anonymous scoped enum is not allowed");
16562 pop_deferring_access_checks ();
16564 /* Check for the `:' that denotes a specified underlying type in C++0x.
16565 Note that a ':' could also indicate a bitfield width, however. */
16566 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16568 cp_decl_specifier_seq type_specifiers;
16570 /* Consume the `:'. */
16571 cp_lexer_consume_token (parser->lexer);
16573 /* Parse the type-specifier-seq. */
16574 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
16575 /*is_trailing_return=*/false,
16576 &type_specifiers);
16578 /* At this point this is surely not elaborated type specifier. */
16579 if (!cp_parser_parse_definitely (parser))
16580 return NULL_TREE;
16582 if (cxx_dialect < cxx11)
16583 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
16585 has_underlying_type = true;
16587 /* If that didn't work, stop. */
16588 if (type_specifiers.type != error_mark_node)
16590 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
16591 /*initialized=*/0, NULL);
16592 if (underlying_type == error_mark_node
16593 || check_for_bare_parameter_packs (underlying_type))
16594 underlying_type = NULL_TREE;
16598 /* Look for the `{' but don't consume it yet. */
16599 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16601 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
16603 cp_parser_error (parser, "expected %<{%>");
16604 if (has_underlying_type)
16606 type = NULL_TREE;
16607 goto out;
16610 /* An opaque-enum-specifier must have a ';' here. */
16611 if ((scoped_enum_p || underlying_type)
16612 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16614 cp_parser_error (parser, "expected %<;%> or %<{%>");
16615 if (has_underlying_type)
16617 type = NULL_TREE;
16618 goto out;
16623 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
16624 return NULL_TREE;
16626 if (nested_name_specifier)
16628 if (CLASS_TYPE_P (nested_name_specifier))
16630 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
16631 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
16632 push_scope (nested_name_specifier);
16634 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
16636 push_nested_namespace (nested_name_specifier);
16640 /* Issue an error message if type-definitions are forbidden here. */
16641 if (!cp_parser_check_type_definition (parser))
16642 type = error_mark_node;
16643 else
16644 /* Create the new type. We do this before consuming the opening
16645 brace so the enum will be recorded as being on the line of its
16646 tag (or the 'enum' keyword, if there is no tag). */
16647 type = start_enum (identifier, type, underlying_type,
16648 scoped_enum_p, &is_new_type);
16650 /* If the next token is not '{' it is an opaque-enum-specifier or an
16651 elaborated-type-specifier. */
16652 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16654 timevar_push (TV_PARSE_ENUM);
16655 if (nested_name_specifier
16656 && nested_name_specifier != error_mark_node)
16658 /* The following catches invalid code such as:
16659 enum class S<int>::E { A, B, C }; */
16660 if (!processing_specialization
16661 && CLASS_TYPE_P (nested_name_specifier)
16662 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
16663 error_at (type_start_token->location, "cannot add an enumerator "
16664 "list to a template instantiation");
16666 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
16668 error_at (type_start_token->location,
16669 "%<%T::%E%> has not been declared",
16670 TYPE_CONTEXT (nested_name_specifier),
16671 nested_name_specifier);
16672 type = error_mark_node;
16674 /* If that scope does not contain the scope in which the
16675 class was originally declared, the program is invalid. */
16676 else if (prev_scope && !is_ancestor (prev_scope,
16677 nested_name_specifier))
16679 if (at_namespace_scope_p ())
16680 error_at (type_start_token->location,
16681 "declaration of %qD in namespace %qD which does not "
16682 "enclose %qD",
16683 type, prev_scope, nested_name_specifier);
16684 else
16685 error_at (type_start_token->location,
16686 "declaration of %qD in %qD which does not "
16687 "enclose %qD",
16688 type, prev_scope, nested_name_specifier);
16689 type = error_mark_node;
16693 if (scoped_enum_p)
16694 begin_scope (sk_scoped_enum, type);
16696 /* Consume the opening brace. */
16697 cp_lexer_consume_token (parser->lexer);
16699 if (type == error_mark_node)
16700 ; /* Nothing to add */
16701 else if (OPAQUE_ENUM_P (type)
16702 || (cxx_dialect > cxx98 && processing_specialization))
16704 new_value_list = true;
16705 SET_OPAQUE_ENUM_P (type, false);
16706 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
16708 else
16710 error_at (type_start_token->location,
16711 "multiple definition of %q#T", type);
16712 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
16713 "previous definition here");
16714 type = error_mark_node;
16717 if (type == error_mark_node)
16718 cp_parser_skip_to_end_of_block_or_statement (parser);
16719 /* If the next token is not '}', then there are some enumerators. */
16720 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16722 if (is_anonymous && !scoped_enum_p)
16723 pedwarn (type_start_token->location, OPT_Wpedantic,
16724 "ISO C++ forbids empty anonymous enum");
16726 else
16727 cp_parser_enumerator_list (parser, type);
16729 /* Consume the final '}'. */
16730 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16732 if (scoped_enum_p)
16733 finish_scope ();
16734 timevar_pop (TV_PARSE_ENUM);
16736 else
16738 /* If a ';' follows, then it is an opaque-enum-specifier
16739 and additional restrictions apply. */
16740 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16742 if (is_anonymous)
16743 error_at (type_start_token->location,
16744 "opaque-enum-specifier without name");
16745 else if (nested_name_specifier)
16746 error_at (type_start_token->location,
16747 "opaque-enum-specifier must use a simple identifier");
16751 /* Look for trailing attributes to apply to this enumeration, and
16752 apply them if appropriate. */
16753 if (cp_parser_allow_gnu_extensions_p (parser))
16755 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
16756 trailing_attr = chainon (trailing_attr, attributes);
16757 cplus_decl_attributes (&type,
16758 trailing_attr,
16759 (int) ATTR_FLAG_TYPE_IN_PLACE);
16762 /* Finish up the enumeration. */
16763 if (type != error_mark_node)
16765 if (new_value_list)
16766 finish_enum_value_list (type);
16767 if (is_new_type)
16768 finish_enum (type);
16771 if (nested_name_specifier)
16773 if (CLASS_TYPE_P (nested_name_specifier))
16775 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
16776 pop_scope (nested_name_specifier);
16778 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
16780 pop_nested_namespace (nested_name_specifier);
16783 out:
16784 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
16785 return type;
16788 /* Parse an enumerator-list. The enumerators all have the indicated
16789 TYPE.
16791 enumerator-list:
16792 enumerator-definition
16793 enumerator-list , enumerator-definition */
16795 static void
16796 cp_parser_enumerator_list (cp_parser* parser, tree type)
16798 while (true)
16800 /* Parse an enumerator-definition. */
16801 cp_parser_enumerator_definition (parser, type);
16803 /* If the next token is not a ',', we've reached the end of
16804 the list. */
16805 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16806 break;
16807 /* Otherwise, consume the `,' and keep going. */
16808 cp_lexer_consume_token (parser->lexer);
16809 /* If the next token is a `}', there is a trailing comma. */
16810 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16812 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
16813 pedwarn (input_location, OPT_Wpedantic,
16814 "comma at end of enumerator list");
16815 break;
16820 /* Parse an enumerator-definition. The enumerator has the indicated
16821 TYPE.
16823 enumerator-definition:
16824 enumerator
16825 enumerator = constant-expression
16827 enumerator:
16828 identifier
16830 GNU Extensions:
16832 enumerator-definition:
16833 enumerator attributes [opt]
16834 enumerator attributes [opt] = constant-expression */
16836 static void
16837 cp_parser_enumerator_definition (cp_parser* parser, tree type)
16839 tree identifier;
16840 tree value;
16841 location_t loc;
16843 /* Save the input location because we are interested in the location
16844 of the identifier and not the location of the explicit value. */
16845 loc = cp_lexer_peek_token (parser->lexer)->location;
16847 /* Look for the identifier. */
16848 identifier = cp_parser_identifier (parser);
16849 if (identifier == error_mark_node)
16850 return;
16852 /* Parse any specified attributes. */
16853 tree attrs = cp_parser_attributes_opt (parser);
16855 /* If the next token is an '=', then there is an explicit value. */
16856 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16858 /* Consume the `=' token. */
16859 cp_lexer_consume_token (parser->lexer);
16860 /* Parse the value. */
16861 value = cp_parser_constant_expression (parser);
16863 else
16864 value = NULL_TREE;
16866 /* If we are processing a template, make sure the initializer of the
16867 enumerator doesn't contain any bare template parameter pack. */
16868 if (check_for_bare_parameter_packs (value))
16869 value = error_mark_node;
16871 /* Create the enumerator. */
16872 build_enumerator (identifier, value, type, attrs, loc);
16875 /* Parse a namespace-name.
16877 namespace-name:
16878 original-namespace-name
16879 namespace-alias
16881 Returns the NAMESPACE_DECL for the namespace. */
16883 static tree
16884 cp_parser_namespace_name (cp_parser* parser)
16886 tree identifier;
16887 tree namespace_decl;
16889 cp_token *token = cp_lexer_peek_token (parser->lexer);
16891 /* Get the name of the namespace. */
16892 identifier = cp_parser_identifier (parser);
16893 if (identifier == error_mark_node)
16894 return error_mark_node;
16896 /* Look up the identifier in the currently active scope. Look only
16897 for namespaces, due to:
16899 [basic.lookup.udir]
16901 When looking up a namespace-name in a using-directive or alias
16902 definition, only namespace names are considered.
16904 And:
16906 [basic.lookup.qual]
16908 During the lookup of a name preceding the :: scope resolution
16909 operator, object, function, and enumerator names are ignored.
16911 (Note that cp_parser_qualifying_entity only calls this
16912 function if the token after the name is the scope resolution
16913 operator.) */
16914 namespace_decl = cp_parser_lookup_name (parser, identifier,
16915 none_type,
16916 /*is_template=*/false,
16917 /*is_namespace=*/true,
16918 /*check_dependency=*/true,
16919 /*ambiguous_decls=*/NULL,
16920 token->location);
16921 /* If it's not a namespace, issue an error. */
16922 if (namespace_decl == error_mark_node
16923 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
16925 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16926 error_at (token->location, "%qD is not a namespace-name", identifier);
16927 cp_parser_error (parser, "expected namespace-name");
16928 namespace_decl = error_mark_node;
16931 return namespace_decl;
16934 /* Parse a namespace-definition.
16936 namespace-definition:
16937 named-namespace-definition
16938 unnamed-namespace-definition
16940 named-namespace-definition:
16941 original-namespace-definition
16942 extension-namespace-definition
16944 original-namespace-definition:
16945 namespace identifier { namespace-body }
16947 extension-namespace-definition:
16948 namespace original-namespace-name { namespace-body }
16950 unnamed-namespace-definition:
16951 namespace { namespace-body } */
16953 static void
16954 cp_parser_namespace_definition (cp_parser* parser)
16956 tree identifier, attribs;
16957 bool has_visibility;
16958 bool is_inline;
16959 cp_token* token;
16960 int nested_definition_count = 0;
16962 cp_ensure_no_omp_declare_simd (parser);
16963 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
16965 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
16966 is_inline = true;
16967 cp_lexer_consume_token (parser->lexer);
16969 else
16970 is_inline = false;
16972 /* Look for the `namespace' keyword. */
16973 token = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16975 /* Parse any specified attributes before the identifier. */
16976 attribs = cp_parser_attributes_opt (parser);
16978 /* Get the name of the namespace. We do not attempt to distinguish
16979 between an original-namespace-definition and an
16980 extension-namespace-definition at this point. The semantic
16981 analysis routines are responsible for that. */
16982 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16983 identifier = cp_parser_identifier (parser);
16984 else
16985 identifier = NULL_TREE;
16987 /* Parse any specified attributes after the identifier. */
16988 tree post_ident_attribs = cp_parser_attributes_opt (parser);
16989 if (post_ident_attribs)
16991 if (attribs)
16992 attribs = chainon (attribs, post_ident_attribs);
16993 else
16994 attribs = post_ident_attribs;
16997 /* Start the namespace. */
16998 push_namespace (identifier);
17000 /* Parse any nested namespace definition. */
17001 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17003 if (attribs)
17004 error_at (token->location, "a nested namespace definition cannot have attributes");
17005 if (cxx_dialect < cxx1z)
17006 pedwarn (input_location, OPT_Wpedantic,
17007 "nested namespace definitions only available with "
17008 "-std=c++1z or -std=gnu++1z");
17009 if (is_inline)
17010 error_at (token->location, "a nested namespace definition cannot be inline");
17011 while (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17013 cp_lexer_consume_token (parser->lexer);
17014 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17015 identifier = cp_parser_identifier (parser);
17016 else
17018 cp_parser_error (parser, "nested identifier required");
17019 break;
17021 ++nested_definition_count;
17022 push_namespace (identifier);
17026 /* Look for the `{' to validate starting the namespace. */
17027 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
17029 /* "inline namespace" is equivalent to a stub namespace definition
17030 followed by a strong using directive. */
17031 if (is_inline)
17033 tree name_space = current_namespace;
17034 /* Set up namespace association. */
17035 DECL_NAMESPACE_ASSOCIATIONS (name_space)
17036 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
17037 DECL_NAMESPACE_ASSOCIATIONS (name_space));
17038 /* Import the contents of the inline namespace. */
17039 pop_namespace ();
17040 do_using_directive (name_space);
17041 push_namespace (identifier);
17044 has_visibility = handle_namespace_attrs (current_namespace, attribs);
17046 /* Parse the body of the namespace. */
17047 cp_parser_namespace_body (parser);
17049 if (has_visibility)
17050 pop_visibility (1);
17052 /* Finish the nested namespace definitions. */
17053 while (nested_definition_count--)
17054 pop_namespace ();
17056 /* Finish the namespace. */
17057 pop_namespace ();
17058 /* Look for the final `}'. */
17059 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17062 /* Parse a namespace-body.
17064 namespace-body:
17065 declaration-seq [opt] */
17067 static void
17068 cp_parser_namespace_body (cp_parser* parser)
17070 cp_parser_declaration_seq_opt (parser);
17073 /* Parse a namespace-alias-definition.
17075 namespace-alias-definition:
17076 namespace identifier = qualified-namespace-specifier ; */
17078 static void
17079 cp_parser_namespace_alias_definition (cp_parser* parser)
17081 tree identifier;
17082 tree namespace_specifier;
17084 cp_token *token = cp_lexer_peek_token (parser->lexer);
17086 /* Look for the `namespace' keyword. */
17087 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17088 /* Look for the identifier. */
17089 identifier = cp_parser_identifier (parser);
17090 if (identifier == error_mark_node)
17091 return;
17092 /* Look for the `=' token. */
17093 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
17094 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17096 error_at (token->location, "%<namespace%> definition is not allowed here");
17097 /* Skip the definition. */
17098 cp_lexer_consume_token (parser->lexer);
17099 if (cp_parser_skip_to_closing_brace (parser))
17100 cp_lexer_consume_token (parser->lexer);
17101 return;
17103 cp_parser_require (parser, CPP_EQ, RT_EQ);
17104 /* Look for the qualified-namespace-specifier. */
17105 namespace_specifier
17106 = cp_parser_qualified_namespace_specifier (parser);
17107 /* Look for the `;' token. */
17108 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17110 /* Register the alias in the symbol table. */
17111 do_namespace_alias (identifier, namespace_specifier);
17114 /* Parse a qualified-namespace-specifier.
17116 qualified-namespace-specifier:
17117 :: [opt] nested-name-specifier [opt] namespace-name
17119 Returns a NAMESPACE_DECL corresponding to the specified
17120 namespace. */
17122 static tree
17123 cp_parser_qualified_namespace_specifier (cp_parser* parser)
17125 /* Look for the optional `::'. */
17126 cp_parser_global_scope_opt (parser,
17127 /*current_scope_valid_p=*/false);
17129 /* Look for the optional nested-name-specifier. */
17130 cp_parser_nested_name_specifier_opt (parser,
17131 /*typename_keyword_p=*/false,
17132 /*check_dependency_p=*/true,
17133 /*type_p=*/false,
17134 /*is_declaration=*/true);
17136 return cp_parser_namespace_name (parser);
17139 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
17140 access declaration.
17142 using-declaration:
17143 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
17144 using :: unqualified-id ;
17146 access-declaration:
17147 qualified-id ;
17151 static bool
17152 cp_parser_using_declaration (cp_parser* parser,
17153 bool access_declaration_p)
17155 cp_token *token;
17156 bool typename_p = false;
17157 bool global_scope_p;
17158 tree decl;
17159 tree identifier;
17160 tree qscope;
17161 int oldcount = errorcount;
17162 cp_token *diag_token = NULL;
17164 if (access_declaration_p)
17166 diag_token = cp_lexer_peek_token (parser->lexer);
17167 cp_parser_parse_tentatively (parser);
17169 else
17171 /* Look for the `using' keyword. */
17172 cp_parser_require_keyword (parser, RID_USING, RT_USING);
17174 /* Peek at the next token. */
17175 token = cp_lexer_peek_token (parser->lexer);
17176 /* See if it's `typename'. */
17177 if (token->keyword == RID_TYPENAME)
17179 /* Remember that we've seen it. */
17180 typename_p = true;
17181 /* Consume the `typename' token. */
17182 cp_lexer_consume_token (parser->lexer);
17186 /* Look for the optional global scope qualification. */
17187 global_scope_p
17188 = (cp_parser_global_scope_opt (parser,
17189 /*current_scope_valid_p=*/false)
17190 != NULL_TREE);
17192 /* If we saw `typename', or didn't see `::', then there must be a
17193 nested-name-specifier present. */
17194 if (typename_p || !global_scope_p)
17196 qscope = cp_parser_nested_name_specifier (parser, typename_p,
17197 /*check_dependency_p=*/true,
17198 /*type_p=*/false,
17199 /*is_declaration=*/true);
17200 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
17202 cp_parser_skip_to_end_of_block_or_statement (parser);
17203 return false;
17206 /* Otherwise, we could be in either of the two productions. In that
17207 case, treat the nested-name-specifier as optional. */
17208 else
17209 qscope = cp_parser_nested_name_specifier_opt (parser,
17210 /*typename_keyword_p=*/false,
17211 /*check_dependency_p=*/true,
17212 /*type_p=*/false,
17213 /*is_declaration=*/true);
17214 if (!qscope)
17215 qscope = global_namespace;
17216 else if (UNSCOPED_ENUM_P (qscope))
17217 qscope = CP_TYPE_CONTEXT (qscope);
17219 if (access_declaration_p && cp_parser_error_occurred (parser))
17220 /* Something has already gone wrong; there's no need to parse
17221 further. Since an error has occurred, the return value of
17222 cp_parser_parse_definitely will be false, as required. */
17223 return cp_parser_parse_definitely (parser);
17225 token = cp_lexer_peek_token (parser->lexer);
17226 /* Parse the unqualified-id. */
17227 identifier = cp_parser_unqualified_id (parser,
17228 /*template_keyword_p=*/false,
17229 /*check_dependency_p=*/true,
17230 /*declarator_p=*/true,
17231 /*optional_p=*/false);
17233 if (access_declaration_p)
17235 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17236 cp_parser_simulate_error (parser);
17237 if (!cp_parser_parse_definitely (parser))
17238 return false;
17241 /* The function we call to handle a using-declaration is different
17242 depending on what scope we are in. */
17243 if (qscope == error_mark_node || identifier == error_mark_node)
17245 else if (!identifier_p (identifier)
17246 && TREE_CODE (identifier) != BIT_NOT_EXPR)
17247 /* [namespace.udecl]
17249 A using declaration shall not name a template-id. */
17250 error_at (token->location,
17251 "a template-id may not appear in a using-declaration");
17252 else
17254 if (at_class_scope_p ())
17256 /* Create the USING_DECL. */
17257 decl = do_class_using_decl (parser->scope, identifier);
17259 if (decl && typename_p)
17260 USING_DECL_TYPENAME_P (decl) = 1;
17262 if (check_for_bare_parameter_packs (decl))
17264 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17265 return false;
17267 else
17268 /* Add it to the list of members in this class. */
17269 finish_member_declaration (decl);
17271 else
17273 decl = cp_parser_lookup_name_simple (parser,
17274 identifier,
17275 token->location);
17276 if (decl == error_mark_node)
17277 cp_parser_name_lookup_error (parser, identifier,
17278 decl, NLE_NULL,
17279 token->location);
17280 else if (check_for_bare_parameter_packs (decl))
17282 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17283 return false;
17285 else if (!at_namespace_scope_p ())
17286 do_local_using_decl (decl, qscope, identifier);
17287 else
17288 do_toplevel_using_decl (decl, qscope, identifier);
17292 /* Look for the final `;'. */
17293 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17295 if (access_declaration_p && errorcount == oldcount)
17296 warning_at (diag_token->location, OPT_Wdeprecated,
17297 "access declarations are deprecated "
17298 "in favour of using-declarations; "
17299 "suggestion: add the %<using%> keyword");
17301 return true;
17304 /* Parse an alias-declaration.
17306 alias-declaration:
17307 using identifier attribute-specifier-seq [opt] = type-id */
17309 static tree
17310 cp_parser_alias_declaration (cp_parser* parser)
17312 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
17313 location_t id_location;
17314 cp_declarator *declarator;
17315 cp_decl_specifier_seq decl_specs;
17316 bool member_p;
17317 const char *saved_message = NULL;
17319 /* Look for the `using' keyword. */
17320 cp_token *using_token
17321 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
17322 if (using_token == NULL)
17323 return error_mark_node;
17325 id_location = cp_lexer_peek_token (parser->lexer)->location;
17326 id = cp_parser_identifier (parser);
17327 if (id == error_mark_node)
17328 return error_mark_node;
17330 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
17331 attributes = cp_parser_attributes_opt (parser);
17332 if (attributes == error_mark_node)
17333 return error_mark_node;
17335 cp_parser_require (parser, CPP_EQ, RT_EQ);
17337 if (cp_parser_error_occurred (parser))
17338 return error_mark_node;
17340 cp_parser_commit_to_tentative_parse (parser);
17342 /* Now we are going to parse the type-id of the declaration. */
17345 [dcl.type]/3 says:
17347 "A type-specifier-seq shall not define a class or enumeration
17348 unless it appears in the type-id of an alias-declaration (7.1.3) that
17349 is not the declaration of a template-declaration."
17351 In other words, if we currently are in an alias template, the
17352 type-id should not define a type.
17354 So let's set parser->type_definition_forbidden_message in that
17355 case; cp_parser_check_type_definition (called by
17356 cp_parser_class_specifier) will then emit an error if a type is
17357 defined in the type-id. */
17358 if (parser->num_template_parameter_lists)
17360 saved_message = parser->type_definition_forbidden_message;
17361 parser->type_definition_forbidden_message =
17362 G_("types may not be defined in alias template declarations");
17365 type = cp_parser_type_id (parser);
17367 /* Restore the error message if need be. */
17368 if (parser->num_template_parameter_lists)
17369 parser->type_definition_forbidden_message = saved_message;
17371 if (type == error_mark_node
17372 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
17374 cp_parser_skip_to_end_of_block_or_statement (parser);
17375 return error_mark_node;
17378 /* A typedef-name can also be introduced by an alias-declaration. The
17379 identifier following the using keyword becomes a typedef-name. It has
17380 the same semantics as if it were introduced by the typedef
17381 specifier. In particular, it does not define a new type and it shall
17382 not appear in the type-id. */
17384 clear_decl_specs (&decl_specs);
17385 decl_specs.type = type;
17386 if (attributes != NULL_TREE)
17388 decl_specs.attributes = attributes;
17389 set_and_check_decl_spec_loc (&decl_specs,
17390 ds_attribute,
17391 attrs_token);
17393 set_and_check_decl_spec_loc (&decl_specs,
17394 ds_typedef,
17395 using_token);
17396 set_and_check_decl_spec_loc (&decl_specs,
17397 ds_alias,
17398 using_token);
17400 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
17401 declarator->id_loc = id_location;
17403 member_p = at_class_scope_p ();
17404 if (member_p)
17405 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
17406 NULL_TREE, attributes);
17407 else
17408 decl = start_decl (declarator, &decl_specs, 0,
17409 attributes, NULL_TREE, &pushed_scope);
17410 if (decl == error_mark_node)
17411 return decl;
17413 // Attach constraints to the alias declaration.
17414 if (flag_concepts && current_template_parms)
17416 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
17417 tree constr = build_constraints (reqs, NULL_TREE);
17418 set_constraints (decl, constr);
17421 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
17423 if (pushed_scope)
17424 pop_scope (pushed_scope);
17426 /* If decl is a template, return its TEMPLATE_DECL so that it gets
17427 added into the symbol table; otherwise, return the TYPE_DECL. */
17428 if (DECL_LANG_SPECIFIC (decl)
17429 && DECL_TEMPLATE_INFO (decl)
17430 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
17432 decl = DECL_TI_TEMPLATE (decl);
17433 if (member_p)
17434 check_member_template (decl);
17437 return decl;
17440 /* Parse a using-directive.
17442 using-directive:
17443 using namespace :: [opt] nested-name-specifier [opt]
17444 namespace-name ; */
17446 static void
17447 cp_parser_using_directive (cp_parser* parser)
17449 tree namespace_decl;
17450 tree attribs;
17452 /* Look for the `using' keyword. */
17453 cp_parser_require_keyword (parser, RID_USING, RT_USING);
17454 /* And the `namespace' keyword. */
17455 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17456 /* Look for the optional `::' operator. */
17457 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17458 /* And the optional nested-name-specifier. */
17459 cp_parser_nested_name_specifier_opt (parser,
17460 /*typename_keyword_p=*/false,
17461 /*check_dependency_p=*/true,
17462 /*type_p=*/false,
17463 /*is_declaration=*/true);
17464 /* Get the namespace being used. */
17465 namespace_decl = cp_parser_namespace_name (parser);
17466 /* And any specified attributes. */
17467 attribs = cp_parser_attributes_opt (parser);
17468 /* Update the symbol table. */
17469 parse_using_directive (namespace_decl, attribs);
17470 /* Look for the final `;'. */
17471 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17474 /* Parse an asm-definition.
17476 asm-definition:
17477 asm ( string-literal ) ;
17479 GNU Extension:
17481 asm-definition:
17482 asm volatile [opt] ( string-literal ) ;
17483 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
17484 asm volatile [opt] ( string-literal : asm-operand-list [opt]
17485 : asm-operand-list [opt] ) ;
17486 asm volatile [opt] ( string-literal : asm-operand-list [opt]
17487 : asm-operand-list [opt]
17488 : asm-clobber-list [opt] ) ;
17489 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
17490 : asm-clobber-list [opt]
17491 : asm-goto-list ) ; */
17493 static void
17494 cp_parser_asm_definition (cp_parser* parser)
17496 tree string;
17497 tree outputs = NULL_TREE;
17498 tree inputs = NULL_TREE;
17499 tree clobbers = NULL_TREE;
17500 tree labels = NULL_TREE;
17501 tree asm_stmt;
17502 bool volatile_p = false;
17503 bool extended_p = false;
17504 bool invalid_inputs_p = false;
17505 bool invalid_outputs_p = false;
17506 bool goto_p = false;
17507 required_token missing = RT_NONE;
17509 /* Look for the `asm' keyword. */
17510 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
17512 if (parser->in_function_body
17513 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
17515 error ("%<asm%> in %<constexpr%> function");
17516 cp_function_chain->invalid_constexpr = true;
17519 /* See if the next token is `volatile'. */
17520 if (cp_parser_allow_gnu_extensions_p (parser)
17521 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
17523 /* Remember that we saw the `volatile' keyword. */
17524 volatile_p = true;
17525 /* Consume the token. */
17526 cp_lexer_consume_token (parser->lexer);
17528 if (cp_parser_allow_gnu_extensions_p (parser)
17529 && parser->in_function_body
17530 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
17532 /* Remember that we saw the `goto' keyword. */
17533 goto_p = true;
17534 /* Consume the token. */
17535 cp_lexer_consume_token (parser->lexer);
17537 /* Look for the opening `('. */
17538 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
17539 return;
17540 /* Look for the string. */
17541 string = cp_parser_string_literal (parser, false, false);
17542 if (string == error_mark_node)
17544 cp_parser_skip_to_closing_parenthesis (parser, true, false,
17545 /*consume_paren=*/true);
17546 return;
17549 /* If we're allowing GNU extensions, check for the extended assembly
17550 syntax. Unfortunately, the `:' tokens need not be separated by
17551 a space in C, and so, for compatibility, we tolerate that here
17552 too. Doing that means that we have to treat the `::' operator as
17553 two `:' tokens. */
17554 if (cp_parser_allow_gnu_extensions_p (parser)
17555 && parser->in_function_body
17556 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
17557 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
17559 bool inputs_p = false;
17560 bool clobbers_p = false;
17561 bool labels_p = false;
17563 /* The extended syntax was used. */
17564 extended_p = true;
17566 /* Look for outputs. */
17567 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17569 /* Consume the `:'. */
17570 cp_lexer_consume_token (parser->lexer);
17571 /* Parse the output-operands. */
17572 if (cp_lexer_next_token_is_not (parser->lexer,
17573 CPP_COLON)
17574 && cp_lexer_next_token_is_not (parser->lexer,
17575 CPP_SCOPE)
17576 && cp_lexer_next_token_is_not (parser->lexer,
17577 CPP_CLOSE_PAREN)
17578 && !goto_p)
17580 outputs = cp_parser_asm_operand_list (parser);
17581 if (outputs == error_mark_node)
17582 invalid_outputs_p = true;
17585 /* If the next token is `::', there are no outputs, and the
17586 next token is the beginning of the inputs. */
17587 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17588 /* The inputs are coming next. */
17589 inputs_p = true;
17591 /* Look for inputs. */
17592 if (inputs_p
17593 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17595 /* Consume the `:' or `::'. */
17596 cp_lexer_consume_token (parser->lexer);
17597 /* Parse the output-operands. */
17598 if (cp_lexer_next_token_is_not (parser->lexer,
17599 CPP_COLON)
17600 && cp_lexer_next_token_is_not (parser->lexer,
17601 CPP_SCOPE)
17602 && cp_lexer_next_token_is_not (parser->lexer,
17603 CPP_CLOSE_PAREN))
17605 inputs = cp_parser_asm_operand_list (parser);
17606 if (inputs == error_mark_node)
17607 invalid_inputs_p = true;
17610 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17611 /* The clobbers are coming next. */
17612 clobbers_p = true;
17614 /* Look for clobbers. */
17615 if (clobbers_p
17616 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17618 clobbers_p = true;
17619 /* Consume the `:' or `::'. */
17620 cp_lexer_consume_token (parser->lexer);
17621 /* Parse the clobbers. */
17622 if (cp_lexer_next_token_is_not (parser->lexer,
17623 CPP_COLON)
17624 && cp_lexer_next_token_is_not (parser->lexer,
17625 CPP_CLOSE_PAREN))
17626 clobbers = cp_parser_asm_clobber_list (parser);
17628 else if (goto_p
17629 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17630 /* The labels are coming next. */
17631 labels_p = true;
17633 /* Look for labels. */
17634 if (labels_p
17635 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
17637 labels_p = true;
17638 /* Consume the `:' or `::'. */
17639 cp_lexer_consume_token (parser->lexer);
17640 /* Parse the labels. */
17641 labels = cp_parser_asm_label_list (parser);
17644 if (goto_p && !labels_p)
17645 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
17647 else if (goto_p)
17648 missing = RT_COLON_SCOPE;
17650 /* Look for the closing `)'. */
17651 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
17652 missing ? missing : RT_CLOSE_PAREN))
17653 cp_parser_skip_to_closing_parenthesis (parser, true, false,
17654 /*consume_paren=*/true);
17655 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17657 if (!invalid_inputs_p && !invalid_outputs_p)
17659 /* Create the ASM_EXPR. */
17660 if (parser->in_function_body)
17662 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
17663 inputs, clobbers, labels);
17664 /* If the extended syntax was not used, mark the ASM_EXPR. */
17665 if (!extended_p)
17667 tree temp = asm_stmt;
17668 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
17669 temp = TREE_OPERAND (temp, 0);
17671 ASM_INPUT_P (temp) = 1;
17674 else
17675 symtab->finalize_toplevel_asm (string);
17679 /* Declarators [gram.dcl.decl] */
17681 /* Parse an init-declarator.
17683 init-declarator:
17684 declarator initializer [opt]
17686 GNU Extension:
17688 init-declarator:
17689 declarator asm-specification [opt] attributes [opt] initializer [opt]
17691 function-definition:
17692 decl-specifier-seq [opt] declarator ctor-initializer [opt]
17693 function-body
17694 decl-specifier-seq [opt] declarator function-try-block
17696 GNU Extension:
17698 function-definition:
17699 __extension__ function-definition
17701 TM Extension:
17703 function-definition:
17704 decl-specifier-seq [opt] declarator function-transaction-block
17706 The DECL_SPECIFIERS apply to this declarator. Returns a
17707 representation of the entity declared. If MEMBER_P is TRUE, then
17708 this declarator appears in a class scope. The new DECL created by
17709 this declarator is returned.
17711 The CHECKS are access checks that should be performed once we know
17712 what entity is being declared (and, therefore, what classes have
17713 befriended it).
17715 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
17716 for a function-definition here as well. If the declarator is a
17717 declarator for a function-definition, *FUNCTION_DEFINITION_P will
17718 be TRUE upon return. By that point, the function-definition will
17719 have been completely parsed.
17721 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
17722 is FALSE.
17724 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
17725 parsed declaration if it is an uninitialized single declarator not followed
17726 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
17727 if present, will not be consumed. If returned, this declarator will be
17728 created with SD_INITIALIZED but will not call cp_finish_decl.
17730 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
17731 and there is an initializer, the pointed location_t is set to the
17732 location of the '=' or `(', or '{' in C++11 token introducing the
17733 initializer. */
17735 static tree
17736 cp_parser_init_declarator (cp_parser* parser,
17737 cp_decl_specifier_seq *decl_specifiers,
17738 vec<deferred_access_check, va_gc> *checks,
17739 bool function_definition_allowed_p,
17740 bool member_p,
17741 int declares_class_or_enum,
17742 bool* function_definition_p,
17743 tree* maybe_range_for_decl,
17744 location_t* init_loc)
17746 cp_token *token = NULL, *asm_spec_start_token = NULL,
17747 *attributes_start_token = NULL;
17748 cp_declarator *declarator;
17749 tree prefix_attributes;
17750 tree attributes = NULL;
17751 tree asm_specification;
17752 tree initializer;
17753 tree decl = NULL_TREE;
17754 tree scope;
17755 int is_initialized;
17756 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
17757 initialized with "= ..", CPP_OPEN_PAREN if initialized with
17758 "(...)". */
17759 enum cpp_ttype initialization_kind;
17760 bool is_direct_init = false;
17761 bool is_non_constant_init;
17762 int ctor_dtor_or_conv_p;
17763 bool friend_p = cp_parser_friend_p (decl_specifiers);
17764 tree pushed_scope = NULL_TREE;
17765 bool range_for_decl_p = false;
17766 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17767 location_t tmp_init_loc = UNKNOWN_LOCATION;
17769 /* Gather the attributes that were provided with the
17770 decl-specifiers. */
17771 prefix_attributes = decl_specifiers->attributes;
17773 /* Assume that this is not the declarator for a function
17774 definition. */
17775 if (function_definition_p)
17776 *function_definition_p = false;
17778 /* Default arguments are only permitted for function parameters. */
17779 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
17780 parser->default_arg_ok_p = false;
17782 /* Defer access checks while parsing the declarator; we cannot know
17783 what names are accessible until we know what is being
17784 declared. */
17785 resume_deferring_access_checks ();
17787 /* Parse the declarator. */
17788 token = cp_lexer_peek_token (parser->lexer);
17789 declarator
17790 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17791 &ctor_dtor_or_conv_p,
17792 /*parenthesized_p=*/NULL,
17793 member_p, friend_p);
17794 /* Gather up the deferred checks. */
17795 stop_deferring_access_checks ();
17797 parser->default_arg_ok_p = saved_default_arg_ok_p;
17799 /* If the DECLARATOR was erroneous, there's no need to go
17800 further. */
17801 if (declarator == cp_error_declarator)
17802 return error_mark_node;
17804 /* Check that the number of template-parameter-lists is OK. */
17805 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
17806 token->location))
17807 return error_mark_node;
17809 if (declares_class_or_enum & 2)
17810 cp_parser_check_for_definition_in_return_type (declarator,
17811 decl_specifiers->type,
17812 decl_specifiers->locations[ds_type_spec]);
17814 /* Figure out what scope the entity declared by the DECLARATOR is
17815 located in. `grokdeclarator' sometimes changes the scope, so
17816 we compute it now. */
17817 scope = get_scope_of_declarator (declarator);
17819 /* Perform any lookups in the declared type which were thought to be
17820 dependent, but are not in the scope of the declarator. */
17821 decl_specifiers->type
17822 = maybe_update_decl_type (decl_specifiers->type, scope);
17824 /* If we're allowing GNU extensions, look for an
17825 asm-specification. */
17826 if (cp_parser_allow_gnu_extensions_p (parser))
17828 /* Look for an asm-specification. */
17829 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
17830 asm_specification = cp_parser_asm_specification_opt (parser);
17832 else
17833 asm_specification = NULL_TREE;
17835 /* Look for attributes. */
17836 attributes_start_token = cp_lexer_peek_token (parser->lexer);
17837 attributes = cp_parser_attributes_opt (parser);
17839 /* Peek at the next token. */
17840 token = cp_lexer_peek_token (parser->lexer);
17842 bool bogus_implicit_tmpl = false;
17844 if (function_declarator_p (declarator))
17846 /* Check to see if the token indicates the start of a
17847 function-definition. */
17848 if (cp_parser_token_starts_function_definition_p (token))
17850 if (!function_definition_allowed_p)
17852 /* If a function-definition should not appear here, issue an
17853 error message. */
17854 cp_parser_error (parser,
17855 "a function-definition is not allowed here");
17856 return error_mark_node;
17859 location_t func_brace_location
17860 = cp_lexer_peek_token (parser->lexer)->location;
17862 /* Neither attributes nor an asm-specification are allowed
17863 on a function-definition. */
17864 if (asm_specification)
17865 error_at (asm_spec_start_token->location,
17866 "an asm-specification is not allowed "
17867 "on a function-definition");
17868 if (attributes)
17869 error_at (attributes_start_token->location,
17870 "attributes are not allowed "
17871 "on a function-definition");
17872 /* This is a function-definition. */
17873 *function_definition_p = true;
17875 /* Parse the function definition. */
17876 if (member_p)
17877 decl = cp_parser_save_member_function_body (parser,
17878 decl_specifiers,
17879 declarator,
17880 prefix_attributes);
17881 else
17882 decl =
17883 (cp_parser_function_definition_from_specifiers_and_declarator
17884 (parser, decl_specifiers, prefix_attributes, declarator));
17886 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
17888 /* This is where the prologue starts... */
17889 DECL_STRUCT_FUNCTION (decl)->function_start_locus
17890 = func_brace_location;
17893 return decl;
17896 else if (parser->fully_implicit_function_template_p)
17898 /* A non-template declaration involving a function parameter list
17899 containing an implicit template parameter will be made into a
17900 template. If the resulting declaration is not going to be an
17901 actual function then finish the template scope here to prevent it.
17902 An error message will be issued once we have a decl to talk about.
17904 FIXME probably we should do type deduction rather than create an
17905 implicit template, but the standard currently doesn't allow it. */
17906 bogus_implicit_tmpl = true;
17907 finish_fully_implicit_template (parser, NULL_TREE);
17910 /* [dcl.dcl]
17912 Only in function declarations for constructors, destructors, and
17913 type conversions can the decl-specifier-seq be omitted.
17915 We explicitly postpone this check past the point where we handle
17916 function-definitions because we tolerate function-definitions
17917 that are missing their return types in some modes. */
17918 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
17920 cp_parser_error (parser,
17921 "expected constructor, destructor, or type conversion");
17922 return error_mark_node;
17925 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
17926 if (token->type == CPP_EQ
17927 || token->type == CPP_OPEN_PAREN
17928 || token->type == CPP_OPEN_BRACE)
17930 is_initialized = SD_INITIALIZED;
17931 initialization_kind = token->type;
17932 if (maybe_range_for_decl)
17933 *maybe_range_for_decl = error_mark_node;
17934 tmp_init_loc = token->location;
17935 if (init_loc && *init_loc == UNKNOWN_LOCATION)
17936 *init_loc = tmp_init_loc;
17938 if (token->type == CPP_EQ
17939 && function_declarator_p (declarator))
17941 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
17942 if (t2->keyword == RID_DEFAULT)
17943 is_initialized = SD_DEFAULTED;
17944 else if (t2->keyword == RID_DELETE)
17945 is_initialized = SD_DELETED;
17948 else
17950 /* If the init-declarator isn't initialized and isn't followed by a
17951 `,' or `;', it's not a valid init-declarator. */
17952 if (token->type != CPP_COMMA
17953 && token->type != CPP_SEMICOLON)
17955 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
17956 range_for_decl_p = true;
17957 else
17959 if (!maybe_range_for_decl)
17960 cp_parser_error (parser, "expected initializer");
17961 return error_mark_node;
17964 is_initialized = SD_UNINITIALIZED;
17965 initialization_kind = CPP_EOF;
17968 /* Because start_decl has side-effects, we should only call it if we
17969 know we're going ahead. By this point, we know that we cannot
17970 possibly be looking at any other construct. */
17971 cp_parser_commit_to_tentative_parse (parser);
17973 /* Enter the newly declared entry in the symbol table. If we're
17974 processing a declaration in a class-specifier, we wait until
17975 after processing the initializer. */
17976 if (!member_p)
17978 if (parser->in_unbraced_linkage_specification_p)
17979 decl_specifiers->storage_class = sc_extern;
17980 decl = start_decl (declarator, decl_specifiers,
17981 range_for_decl_p? SD_INITIALIZED : is_initialized,
17982 attributes, prefix_attributes, &pushed_scope);
17983 cp_finalize_omp_declare_simd (parser, decl);
17984 /* Adjust location of decl if declarator->id_loc is more appropriate:
17985 set, and decl wasn't merged with another decl, in which case its
17986 location would be different from input_location, and more accurate. */
17987 if (DECL_P (decl)
17988 && declarator->id_loc != UNKNOWN_LOCATION
17989 && DECL_SOURCE_LOCATION (decl) == input_location)
17990 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
17992 else if (scope)
17993 /* Enter the SCOPE. That way unqualified names appearing in the
17994 initializer will be looked up in SCOPE. */
17995 pushed_scope = push_scope (scope);
17997 /* Perform deferred access control checks, now that we know in which
17998 SCOPE the declared entity resides. */
17999 if (!member_p && decl)
18001 tree saved_current_function_decl = NULL_TREE;
18003 /* If the entity being declared is a function, pretend that we
18004 are in its scope. If it is a `friend', it may have access to
18005 things that would not otherwise be accessible. */
18006 if (TREE_CODE (decl) == FUNCTION_DECL)
18008 saved_current_function_decl = current_function_decl;
18009 current_function_decl = decl;
18012 /* Perform access checks for template parameters. */
18013 cp_parser_perform_template_parameter_access_checks (checks);
18015 /* Perform the access control checks for the declarator and the
18016 decl-specifiers. */
18017 perform_deferred_access_checks (tf_warning_or_error);
18019 /* Restore the saved value. */
18020 if (TREE_CODE (decl) == FUNCTION_DECL)
18021 current_function_decl = saved_current_function_decl;
18024 /* Parse the initializer. */
18025 initializer = NULL_TREE;
18026 is_direct_init = false;
18027 is_non_constant_init = true;
18028 if (is_initialized)
18030 if (function_declarator_p (declarator))
18032 if (initialization_kind == CPP_EQ)
18033 initializer = cp_parser_pure_specifier (parser);
18034 else
18036 /* If the declaration was erroneous, we don't really
18037 know what the user intended, so just silently
18038 consume the initializer. */
18039 if (decl != error_mark_node)
18040 error_at (tmp_init_loc, "initializer provided for function");
18041 cp_parser_skip_to_closing_parenthesis (parser,
18042 /*recovering=*/true,
18043 /*or_comma=*/false,
18044 /*consume_paren=*/true);
18047 else
18049 /* We want to record the extra mangling scope for in-class
18050 initializers of class members and initializers of static data
18051 member templates. The former involves deferring
18052 parsing of the initializer until end of class as with default
18053 arguments. So right here we only handle the latter. */
18054 if (!member_p && processing_template_decl)
18055 start_lambda_scope (decl);
18056 initializer = cp_parser_initializer (parser,
18057 &is_direct_init,
18058 &is_non_constant_init);
18059 if (!member_p && processing_template_decl)
18060 finish_lambda_scope ();
18061 if (initializer == error_mark_node)
18062 cp_parser_skip_to_end_of_statement (parser);
18066 /* The old parser allows attributes to appear after a parenthesized
18067 initializer. Mark Mitchell proposed removing this functionality
18068 on the GCC mailing lists on 2002-08-13. This parser accepts the
18069 attributes -- but ignores them. */
18070 if (cp_parser_allow_gnu_extensions_p (parser)
18071 && initialization_kind == CPP_OPEN_PAREN)
18072 if (cp_parser_attributes_opt (parser))
18073 warning (OPT_Wattributes,
18074 "attributes after parenthesized initializer ignored");
18076 /* And now complain about a non-function implicit template. */
18077 if (bogus_implicit_tmpl)
18078 error_at (DECL_SOURCE_LOCATION (decl),
18079 "non-function %qD declared as implicit template", decl);
18081 /* For an in-class declaration, use `grokfield' to create the
18082 declaration. */
18083 if (member_p)
18085 if (pushed_scope)
18087 pop_scope (pushed_scope);
18088 pushed_scope = NULL_TREE;
18090 decl = grokfield (declarator, decl_specifiers,
18091 initializer, !is_non_constant_init,
18092 /*asmspec=*/NULL_TREE,
18093 chainon (attributes, prefix_attributes));
18094 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
18095 cp_parser_save_default_args (parser, decl);
18096 cp_finalize_omp_declare_simd (parser, decl);
18099 /* Finish processing the declaration. But, skip member
18100 declarations. */
18101 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
18103 cp_finish_decl (decl,
18104 initializer, !is_non_constant_init,
18105 asm_specification,
18106 /* If the initializer is in parentheses, then this is
18107 a direct-initialization, which means that an
18108 `explicit' constructor is OK. Otherwise, an
18109 `explicit' constructor cannot be used. */
18110 ((is_direct_init || !is_initialized)
18111 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
18113 else if ((cxx_dialect != cxx98) && friend_p
18114 && decl && TREE_CODE (decl) == FUNCTION_DECL)
18115 /* Core issue #226 (C++0x only): A default template-argument
18116 shall not be specified in a friend class template
18117 declaration. */
18118 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
18119 /*is_partial=*/false, /*is_friend_decl=*/1);
18121 if (!friend_p && pushed_scope)
18122 pop_scope (pushed_scope);
18124 if (function_declarator_p (declarator)
18125 && parser->fully_implicit_function_template_p)
18127 if (member_p)
18128 decl = finish_fully_implicit_template (parser, decl);
18129 else
18130 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
18133 return decl;
18136 /* Parse a declarator.
18138 declarator:
18139 direct-declarator
18140 ptr-operator declarator
18142 abstract-declarator:
18143 ptr-operator abstract-declarator [opt]
18144 direct-abstract-declarator
18146 GNU Extensions:
18148 declarator:
18149 attributes [opt] direct-declarator
18150 attributes [opt] ptr-operator declarator
18152 abstract-declarator:
18153 attributes [opt] ptr-operator abstract-declarator [opt]
18154 attributes [opt] direct-abstract-declarator
18156 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
18157 detect constructor, destructor or conversion operators. It is set
18158 to -1 if the declarator is a name, and +1 if it is a
18159 function. Otherwise it is set to zero. Usually you just want to
18160 test for >0, but internally the negative value is used.
18162 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
18163 a decl-specifier-seq unless it declares a constructor, destructor,
18164 or conversion. It might seem that we could check this condition in
18165 semantic analysis, rather than parsing, but that makes it difficult
18166 to handle something like `f()'. We want to notice that there are
18167 no decl-specifiers, and therefore realize that this is an
18168 expression, not a declaration.)
18170 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
18171 the declarator is a direct-declarator of the form "(...)".
18173 MEMBER_P is true iff this declarator is a member-declarator.
18175 FRIEND_P is true iff this declarator is a friend. */
18177 static cp_declarator *
18178 cp_parser_declarator (cp_parser* parser,
18179 cp_parser_declarator_kind dcl_kind,
18180 int* ctor_dtor_or_conv_p,
18181 bool* parenthesized_p,
18182 bool member_p, bool friend_p)
18184 cp_declarator *declarator;
18185 enum tree_code code;
18186 cp_cv_quals cv_quals;
18187 tree class_type;
18188 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
18190 /* Assume this is not a constructor, destructor, or type-conversion
18191 operator. */
18192 if (ctor_dtor_or_conv_p)
18193 *ctor_dtor_or_conv_p = 0;
18195 if (cp_parser_allow_gnu_extensions_p (parser))
18196 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
18198 /* Check for the ptr-operator production. */
18199 cp_parser_parse_tentatively (parser);
18200 /* Parse the ptr-operator. */
18201 code = cp_parser_ptr_operator (parser,
18202 &class_type,
18203 &cv_quals,
18204 &std_attributes);
18206 /* If that worked, then we have a ptr-operator. */
18207 if (cp_parser_parse_definitely (parser))
18209 /* If a ptr-operator was found, then this declarator was not
18210 parenthesized. */
18211 if (parenthesized_p)
18212 *parenthesized_p = true;
18213 /* The dependent declarator is optional if we are parsing an
18214 abstract-declarator. */
18215 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18216 cp_parser_parse_tentatively (parser);
18218 /* Parse the dependent declarator. */
18219 declarator = cp_parser_declarator (parser, dcl_kind,
18220 /*ctor_dtor_or_conv_p=*/NULL,
18221 /*parenthesized_p=*/NULL,
18222 /*member_p=*/false,
18223 friend_p);
18225 /* If we are parsing an abstract-declarator, we must handle the
18226 case where the dependent declarator is absent. */
18227 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
18228 && !cp_parser_parse_definitely (parser))
18229 declarator = NULL;
18231 declarator = cp_parser_make_indirect_declarator
18232 (code, class_type, cv_quals, declarator, std_attributes);
18234 /* Everything else is a direct-declarator. */
18235 else
18237 if (parenthesized_p)
18238 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
18239 CPP_OPEN_PAREN);
18240 declarator = cp_parser_direct_declarator (parser, dcl_kind,
18241 ctor_dtor_or_conv_p,
18242 member_p, friend_p);
18245 if (gnu_attributes && declarator && declarator != cp_error_declarator)
18246 declarator->attributes = gnu_attributes;
18247 return declarator;
18250 /* Parse a direct-declarator or direct-abstract-declarator.
18252 direct-declarator:
18253 declarator-id
18254 direct-declarator ( parameter-declaration-clause )
18255 cv-qualifier-seq [opt]
18256 ref-qualifier [opt]
18257 exception-specification [opt]
18258 direct-declarator [ constant-expression [opt] ]
18259 ( declarator )
18261 direct-abstract-declarator:
18262 direct-abstract-declarator [opt]
18263 ( parameter-declaration-clause )
18264 cv-qualifier-seq [opt]
18265 ref-qualifier [opt]
18266 exception-specification [opt]
18267 direct-abstract-declarator [opt] [ constant-expression [opt] ]
18268 ( abstract-declarator )
18270 Returns a representation of the declarator. DCL_KIND is
18271 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
18272 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
18273 we are parsing a direct-declarator. It is
18274 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
18275 of ambiguity we prefer an abstract declarator, as per
18276 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
18277 as for cp_parser_declarator. */
18279 static cp_declarator *
18280 cp_parser_direct_declarator (cp_parser* parser,
18281 cp_parser_declarator_kind dcl_kind,
18282 int* ctor_dtor_or_conv_p,
18283 bool member_p, bool friend_p)
18285 cp_token *token;
18286 cp_declarator *declarator = NULL;
18287 tree scope = NULL_TREE;
18288 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18289 bool saved_in_declarator_p = parser->in_declarator_p;
18290 bool first = true;
18291 tree pushed_scope = NULL_TREE;
18293 while (true)
18295 /* Peek at the next token. */
18296 token = cp_lexer_peek_token (parser->lexer);
18297 if (token->type == CPP_OPEN_PAREN)
18299 /* This is either a parameter-declaration-clause, or a
18300 parenthesized declarator. When we know we are parsing a
18301 named declarator, it must be a parenthesized declarator
18302 if FIRST is true. For instance, `(int)' is a
18303 parameter-declaration-clause, with an omitted
18304 direct-abstract-declarator. But `((*))', is a
18305 parenthesized abstract declarator. Finally, when T is a
18306 template parameter `(T)' is a
18307 parameter-declaration-clause, and not a parenthesized
18308 named declarator.
18310 We first try and parse a parameter-declaration-clause,
18311 and then try a nested declarator (if FIRST is true).
18313 It is not an error for it not to be a
18314 parameter-declaration-clause, even when FIRST is
18315 false. Consider,
18317 int i (int);
18318 int i (3);
18320 The first is the declaration of a function while the
18321 second is the definition of a variable, including its
18322 initializer.
18324 Having seen only the parenthesis, we cannot know which of
18325 these two alternatives should be selected. Even more
18326 complex are examples like:
18328 int i (int (a));
18329 int i (int (3));
18331 The former is a function-declaration; the latter is a
18332 variable initialization.
18334 Thus again, we try a parameter-declaration-clause, and if
18335 that fails, we back out and return. */
18337 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18339 tree params;
18340 bool is_declarator = false;
18342 /* In a member-declarator, the only valid interpretation
18343 of a parenthesis is the start of a
18344 parameter-declaration-clause. (It is invalid to
18345 initialize a static data member with a parenthesized
18346 initializer; only the "=" form of initialization is
18347 permitted.) */
18348 if (!member_p)
18349 cp_parser_parse_tentatively (parser);
18351 /* Consume the `('. */
18352 cp_lexer_consume_token (parser->lexer);
18353 if (first)
18355 /* If this is going to be an abstract declarator, we're
18356 in a declarator and we can't have default args. */
18357 parser->default_arg_ok_p = false;
18358 parser->in_declarator_p = true;
18361 begin_scope (sk_function_parms, NULL_TREE);
18363 /* Parse the parameter-declaration-clause. */
18364 params = cp_parser_parameter_declaration_clause (parser);
18366 /* Consume the `)'. */
18367 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18369 /* If all went well, parse the cv-qualifier-seq,
18370 ref-qualifier and the exception-specification. */
18371 if (member_p || cp_parser_parse_definitely (parser))
18373 cp_cv_quals cv_quals;
18374 cp_virt_specifiers virt_specifiers;
18375 cp_ref_qualifier ref_qual;
18376 tree exception_specification;
18377 tree late_return;
18378 tree attrs;
18379 bool memfn = (member_p || (pushed_scope
18380 && CLASS_TYPE_P (pushed_scope)));
18382 is_declarator = true;
18384 if (ctor_dtor_or_conv_p)
18385 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
18386 first = false;
18388 /* Parse the cv-qualifier-seq. */
18389 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18390 /* Parse the ref-qualifier. */
18391 ref_qual = cp_parser_ref_qualifier_opt (parser);
18392 /* And the exception-specification. */
18393 exception_specification
18394 = cp_parser_exception_specification_opt (parser);
18396 attrs = cp_parser_std_attribute_spec_seq (parser);
18398 /* In here, we handle cases where attribute is used after
18399 the function declaration. For example:
18400 void func (int x) __attribute__((vector(..))); */
18401 if (flag_cilkplus
18402 && cp_next_tokens_can_be_gnu_attribute_p (parser))
18404 cp_parser_parse_tentatively (parser);
18405 tree attr = cp_parser_gnu_attributes_opt (parser);
18406 if (cp_lexer_next_token_is_not (parser->lexer,
18407 CPP_SEMICOLON)
18408 && cp_lexer_next_token_is_not (parser->lexer,
18409 CPP_OPEN_BRACE))
18410 cp_parser_abort_tentative_parse (parser);
18411 else if (!cp_parser_parse_definitely (parser))
18413 else
18414 attrs = chainon (attr, attrs);
18416 tree requires_clause = NULL_TREE;
18417 late_return = (cp_parser_late_return_type_opt
18418 (parser, declarator, requires_clause,
18419 memfn ? cv_quals : -1));
18421 /* Parse the virt-specifier-seq. */
18422 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18424 /* Create the function-declarator. */
18425 declarator = make_call_declarator (declarator,
18426 params,
18427 cv_quals,
18428 virt_specifiers,
18429 ref_qual,
18430 exception_specification,
18431 late_return,
18432 requires_clause);
18433 declarator->std_attributes = attrs;
18434 /* Any subsequent parameter lists are to do with
18435 return type, so are not those of the declared
18436 function. */
18437 parser->default_arg_ok_p = false;
18440 /* Remove the function parms from scope. */
18441 pop_bindings_and_leave_scope ();
18443 if (is_declarator)
18444 /* Repeat the main loop. */
18445 continue;
18448 /* If this is the first, we can try a parenthesized
18449 declarator. */
18450 if (first)
18452 bool saved_in_type_id_in_expr_p;
18454 parser->default_arg_ok_p = saved_default_arg_ok_p;
18455 parser->in_declarator_p = saved_in_declarator_p;
18457 /* Consume the `('. */
18458 cp_lexer_consume_token (parser->lexer);
18459 /* Parse the nested declarator. */
18460 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18461 parser->in_type_id_in_expr_p = true;
18462 declarator
18463 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
18464 /*parenthesized_p=*/NULL,
18465 member_p, friend_p);
18466 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18467 first = false;
18468 /* Expect a `)'. */
18469 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
18470 declarator = cp_error_declarator;
18471 if (declarator == cp_error_declarator)
18472 break;
18474 goto handle_declarator;
18476 /* Otherwise, we must be done. */
18477 else
18478 break;
18480 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18481 && token->type == CPP_OPEN_SQUARE
18482 && !cp_next_tokens_can_be_attribute_p (parser))
18484 /* Parse an array-declarator. */
18485 tree bounds, attrs;
18487 if (ctor_dtor_or_conv_p)
18488 *ctor_dtor_or_conv_p = 0;
18490 first = false;
18491 parser->default_arg_ok_p = false;
18492 parser->in_declarator_p = true;
18493 /* Consume the `['. */
18494 cp_lexer_consume_token (parser->lexer);
18495 /* Peek at the next token. */
18496 token = cp_lexer_peek_token (parser->lexer);
18497 /* If the next token is `]', then there is no
18498 constant-expression. */
18499 if (token->type != CPP_CLOSE_SQUARE)
18501 bool non_constant_p;
18502 bounds
18503 = cp_parser_constant_expression (parser,
18504 /*allow_non_constant=*/true,
18505 &non_constant_p);
18506 if (!non_constant_p)
18507 /* OK */;
18508 else if (error_operand_p (bounds))
18509 /* Already gave an error. */;
18510 else if (!parser->in_function_body
18511 || current_binding_level->kind == sk_function_parms)
18513 /* Normally, the array bound must be an integral constant
18514 expression. However, as an extension, we allow VLAs
18515 in function scopes as long as they aren't part of a
18516 parameter declaration. */
18517 cp_parser_error (parser,
18518 "array bound is not an integer constant");
18519 bounds = error_mark_node;
18521 else if (processing_template_decl
18522 && !type_dependent_expression_p (bounds))
18524 /* Remember this wasn't a constant-expression. */
18525 bounds = build_nop (TREE_TYPE (bounds), bounds);
18526 TREE_SIDE_EFFECTS (bounds) = 1;
18529 else
18530 bounds = NULL_TREE;
18531 /* Look for the closing `]'. */
18532 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
18534 declarator = cp_error_declarator;
18535 break;
18538 attrs = cp_parser_std_attribute_spec_seq (parser);
18539 declarator = make_array_declarator (declarator, bounds);
18540 declarator->std_attributes = attrs;
18542 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
18545 tree qualifying_scope;
18546 tree unqualified_name;
18547 tree attrs;
18548 special_function_kind sfk;
18549 bool abstract_ok;
18550 bool pack_expansion_p = false;
18551 cp_token *declarator_id_start_token;
18553 /* Parse a declarator-id */
18554 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
18555 if (abstract_ok)
18557 cp_parser_parse_tentatively (parser);
18559 /* If we see an ellipsis, we should be looking at a
18560 parameter pack. */
18561 if (token->type == CPP_ELLIPSIS)
18563 /* Consume the `...' */
18564 cp_lexer_consume_token (parser->lexer);
18566 pack_expansion_p = true;
18570 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
18571 unqualified_name
18572 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
18573 qualifying_scope = parser->scope;
18574 if (abstract_ok)
18576 bool okay = false;
18578 if (!unqualified_name && pack_expansion_p)
18580 /* Check whether an error occurred. */
18581 okay = !cp_parser_error_occurred (parser);
18583 /* We already consumed the ellipsis to mark a
18584 parameter pack, but we have no way to report it,
18585 so abort the tentative parse. We will be exiting
18586 immediately anyway. */
18587 cp_parser_abort_tentative_parse (parser);
18589 else
18590 okay = cp_parser_parse_definitely (parser);
18592 if (!okay)
18593 unqualified_name = error_mark_node;
18594 else if (unqualified_name
18595 && (qualifying_scope
18596 || (!identifier_p (unqualified_name))))
18598 cp_parser_error (parser, "expected unqualified-id");
18599 unqualified_name = error_mark_node;
18603 if (!unqualified_name)
18604 return NULL;
18605 if (unqualified_name == error_mark_node)
18607 declarator = cp_error_declarator;
18608 pack_expansion_p = false;
18609 declarator->parameter_pack_p = false;
18610 break;
18613 attrs = cp_parser_std_attribute_spec_seq (parser);
18615 if (qualifying_scope && at_namespace_scope_p ()
18616 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
18618 /* In the declaration of a member of a template class
18619 outside of the class itself, the SCOPE will sometimes
18620 be a TYPENAME_TYPE. For example, given:
18622 template <typename T>
18623 int S<T>::R::i = 3;
18625 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
18626 this context, we must resolve S<T>::R to an ordinary
18627 type, rather than a typename type.
18629 The reason we normally avoid resolving TYPENAME_TYPEs
18630 is that a specialization of `S' might render
18631 `S<T>::R' not a type. However, if `S' is
18632 specialized, then this `i' will not be used, so there
18633 is no harm in resolving the types here. */
18634 tree type;
18636 /* Resolve the TYPENAME_TYPE. */
18637 type = resolve_typename_type (qualifying_scope,
18638 /*only_current_p=*/false);
18639 /* If that failed, the declarator is invalid. */
18640 if (TREE_CODE (type) == TYPENAME_TYPE)
18642 if (typedef_variant_p (type))
18643 error_at (declarator_id_start_token->location,
18644 "cannot define member of dependent typedef "
18645 "%qT", type);
18646 else
18647 error_at (declarator_id_start_token->location,
18648 "%<%T::%E%> is not a type",
18649 TYPE_CONTEXT (qualifying_scope),
18650 TYPE_IDENTIFIER (qualifying_scope));
18652 qualifying_scope = type;
18655 sfk = sfk_none;
18657 if (unqualified_name)
18659 tree class_type;
18661 if (qualifying_scope
18662 && CLASS_TYPE_P (qualifying_scope))
18663 class_type = qualifying_scope;
18664 else
18665 class_type = current_class_type;
18667 if (TREE_CODE (unqualified_name) == TYPE_DECL)
18669 tree name_type = TREE_TYPE (unqualified_name);
18670 if (class_type && same_type_p (name_type, class_type))
18672 if (qualifying_scope
18673 && CLASSTYPE_USE_TEMPLATE (name_type))
18675 error_at (declarator_id_start_token->location,
18676 "invalid use of constructor as a template");
18677 inform (declarator_id_start_token->location,
18678 "use %<%T::%D%> instead of %<%T::%D%> to "
18679 "name the constructor in a qualified name",
18680 class_type,
18681 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
18682 class_type, name_type);
18683 declarator = cp_error_declarator;
18684 break;
18686 else
18687 unqualified_name = constructor_name (class_type);
18689 else
18691 /* We do not attempt to print the declarator
18692 here because we do not have enough
18693 information about its original syntactic
18694 form. */
18695 cp_parser_error (parser, "invalid declarator");
18696 declarator = cp_error_declarator;
18697 break;
18701 if (class_type)
18703 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
18704 sfk = sfk_destructor;
18705 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
18706 sfk = sfk_conversion;
18707 else if (/* There's no way to declare a constructor
18708 for an anonymous type, even if the type
18709 got a name for linkage purposes. */
18710 !TYPE_WAS_ANONYMOUS (class_type)
18711 /* Handle correctly (c++/19200):
18713 struct S {
18714 struct T{};
18715 friend void S(T);
18718 and also:
18720 namespace N {
18721 void S();
18724 struct S {
18725 friend void N::S();
18726 }; */
18727 && !(friend_p
18728 && class_type != qualifying_scope)
18729 && constructor_name_p (unqualified_name,
18730 class_type))
18732 unqualified_name = constructor_name (class_type);
18733 sfk = sfk_constructor;
18735 else if (is_overloaded_fn (unqualified_name)
18736 && DECL_CONSTRUCTOR_P (get_first_fn
18737 (unqualified_name)))
18738 sfk = sfk_constructor;
18740 if (ctor_dtor_or_conv_p && sfk != sfk_none)
18741 *ctor_dtor_or_conv_p = -1;
18744 declarator = make_id_declarator (qualifying_scope,
18745 unqualified_name,
18746 sfk);
18747 declarator->std_attributes = attrs;
18748 declarator->id_loc = token->location;
18749 declarator->parameter_pack_p = pack_expansion_p;
18751 if (pack_expansion_p)
18752 maybe_warn_variadic_templates ();
18755 handle_declarator:;
18756 scope = get_scope_of_declarator (declarator);
18757 if (scope)
18759 /* Any names that appear after the declarator-id for a
18760 member are looked up in the containing scope. */
18761 if (at_function_scope_p ())
18763 /* But declarations with qualified-ids can't appear in a
18764 function. */
18765 cp_parser_error (parser, "qualified-id in declaration");
18766 declarator = cp_error_declarator;
18767 break;
18769 pushed_scope = push_scope (scope);
18771 parser->in_declarator_p = true;
18772 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
18773 || (declarator && declarator->kind == cdk_id))
18774 /* Default args are only allowed on function
18775 declarations. */
18776 parser->default_arg_ok_p = saved_default_arg_ok_p;
18777 else
18778 parser->default_arg_ok_p = false;
18780 first = false;
18782 /* We're done. */
18783 else
18784 break;
18787 /* For an abstract declarator, we might wind up with nothing at this
18788 point. That's an error; the declarator is not optional. */
18789 if (!declarator)
18790 cp_parser_error (parser, "expected declarator");
18792 /* If we entered a scope, we must exit it now. */
18793 if (pushed_scope)
18794 pop_scope (pushed_scope);
18796 parser->default_arg_ok_p = saved_default_arg_ok_p;
18797 parser->in_declarator_p = saved_in_declarator_p;
18799 return declarator;
18802 /* Parse a ptr-operator.
18804 ptr-operator:
18805 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18806 * cv-qualifier-seq [opt]
18808 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
18809 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18811 GNU Extension:
18813 ptr-operator:
18814 & cv-qualifier-seq [opt]
18816 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
18817 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
18818 an rvalue reference. In the case of a pointer-to-member, *TYPE is
18819 filled in with the TYPE containing the member. *CV_QUALS is
18820 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
18821 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
18822 Note that the tree codes returned by this function have nothing
18823 to do with the types of trees that will be eventually be created
18824 to represent the pointer or reference type being parsed. They are
18825 just constants with suggestive names. */
18826 static enum tree_code
18827 cp_parser_ptr_operator (cp_parser* parser,
18828 tree* type,
18829 cp_cv_quals *cv_quals,
18830 tree *attributes)
18832 enum tree_code code = ERROR_MARK;
18833 cp_token *token;
18834 tree attrs = NULL_TREE;
18836 /* Assume that it's not a pointer-to-member. */
18837 *type = NULL_TREE;
18838 /* And that there are no cv-qualifiers. */
18839 *cv_quals = TYPE_UNQUALIFIED;
18841 /* Peek at the next token. */
18842 token = cp_lexer_peek_token (parser->lexer);
18844 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
18845 if (token->type == CPP_MULT)
18846 code = INDIRECT_REF;
18847 else if (token->type == CPP_AND)
18848 code = ADDR_EXPR;
18849 else if ((cxx_dialect != cxx98) &&
18850 token->type == CPP_AND_AND) /* C++0x only */
18851 code = NON_LVALUE_EXPR;
18853 if (code != ERROR_MARK)
18855 /* Consume the `*', `&' or `&&'. */
18856 cp_lexer_consume_token (parser->lexer);
18858 /* A `*' can be followed by a cv-qualifier-seq, and so can a
18859 `&', if we are allowing GNU extensions. (The only qualifier
18860 that can legally appear after `&' is `restrict', but that is
18861 enforced during semantic analysis. */
18862 if (code == INDIRECT_REF
18863 || cp_parser_allow_gnu_extensions_p (parser))
18864 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18866 attrs = cp_parser_std_attribute_spec_seq (parser);
18867 if (attributes != NULL)
18868 *attributes = attrs;
18870 else
18872 /* Try the pointer-to-member case. */
18873 cp_parser_parse_tentatively (parser);
18874 /* Look for the optional `::' operator. */
18875 cp_parser_global_scope_opt (parser,
18876 /*current_scope_valid_p=*/false);
18877 /* Look for the nested-name specifier. */
18878 token = cp_lexer_peek_token (parser->lexer);
18879 cp_parser_nested_name_specifier (parser,
18880 /*typename_keyword_p=*/false,
18881 /*check_dependency_p=*/true,
18882 /*type_p=*/false,
18883 /*is_declaration=*/false);
18884 /* If we found it, and the next token is a `*', then we are
18885 indeed looking at a pointer-to-member operator. */
18886 if (!cp_parser_error_occurred (parser)
18887 && cp_parser_require (parser, CPP_MULT, RT_MULT))
18889 /* Indicate that the `*' operator was used. */
18890 code = INDIRECT_REF;
18892 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
18893 error_at (token->location, "%qD is a namespace", parser->scope);
18894 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
18895 error_at (token->location, "cannot form pointer to member of "
18896 "non-class %q#T", parser->scope);
18897 else
18899 /* The type of which the member is a member is given by the
18900 current SCOPE. */
18901 *type = parser->scope;
18902 /* The next name will not be qualified. */
18903 parser->scope = NULL_TREE;
18904 parser->qualifying_scope = NULL_TREE;
18905 parser->object_scope = NULL_TREE;
18906 /* Look for optional c++11 attributes. */
18907 attrs = cp_parser_std_attribute_spec_seq (parser);
18908 if (attributes != NULL)
18909 *attributes = attrs;
18910 /* Look for the optional cv-qualifier-seq. */
18911 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18914 /* If that didn't work we don't have a ptr-operator. */
18915 if (!cp_parser_parse_definitely (parser))
18916 cp_parser_error (parser, "expected ptr-operator");
18919 return code;
18922 /* Parse an (optional) cv-qualifier-seq.
18924 cv-qualifier-seq:
18925 cv-qualifier cv-qualifier-seq [opt]
18927 cv-qualifier:
18928 const
18929 volatile
18931 GNU Extension:
18933 cv-qualifier:
18934 __restrict__
18936 Returns a bitmask representing the cv-qualifiers. */
18938 static cp_cv_quals
18939 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
18941 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
18943 while (true)
18945 cp_token *token;
18946 cp_cv_quals cv_qualifier;
18948 /* Peek at the next token. */
18949 token = cp_lexer_peek_token (parser->lexer);
18950 /* See if it's a cv-qualifier. */
18951 switch (token->keyword)
18953 case RID_CONST:
18954 cv_qualifier = TYPE_QUAL_CONST;
18955 break;
18957 case RID_VOLATILE:
18958 cv_qualifier = TYPE_QUAL_VOLATILE;
18959 break;
18961 case RID_RESTRICT:
18962 cv_qualifier = TYPE_QUAL_RESTRICT;
18963 break;
18965 default:
18966 cv_qualifier = TYPE_UNQUALIFIED;
18967 break;
18970 if (!cv_qualifier)
18971 break;
18973 if (cv_quals & cv_qualifier)
18975 error_at (token->location, "duplicate cv-qualifier");
18976 cp_lexer_purge_token (parser->lexer);
18978 else
18980 cp_lexer_consume_token (parser->lexer);
18981 cv_quals |= cv_qualifier;
18985 return cv_quals;
18988 /* Parse an (optional) ref-qualifier
18990 ref-qualifier:
18994 Returns cp_ref_qualifier representing ref-qualifier. */
18996 static cp_ref_qualifier
18997 cp_parser_ref_qualifier_opt (cp_parser* parser)
18999 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
19001 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
19002 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
19003 return ref_qual;
19005 while (true)
19007 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
19008 cp_token *token = cp_lexer_peek_token (parser->lexer);
19010 switch (token->type)
19012 case CPP_AND:
19013 curr_ref_qual = REF_QUAL_LVALUE;
19014 break;
19016 case CPP_AND_AND:
19017 curr_ref_qual = REF_QUAL_RVALUE;
19018 break;
19020 default:
19021 curr_ref_qual = REF_QUAL_NONE;
19022 break;
19025 if (!curr_ref_qual)
19026 break;
19027 else if (ref_qual)
19029 error_at (token->location, "multiple ref-qualifiers");
19030 cp_lexer_purge_token (parser->lexer);
19032 else
19034 ref_qual = curr_ref_qual;
19035 cp_lexer_consume_token (parser->lexer);
19039 return ref_qual;
19042 /* Parse an (optional) virt-specifier-seq.
19044 virt-specifier-seq:
19045 virt-specifier virt-specifier-seq [opt]
19047 virt-specifier:
19048 override
19049 final
19051 Returns a bitmask representing the virt-specifiers. */
19053 static cp_virt_specifiers
19054 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
19056 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19058 while (true)
19060 cp_token *token;
19061 cp_virt_specifiers virt_specifier;
19063 /* Peek at the next token. */
19064 token = cp_lexer_peek_token (parser->lexer);
19065 /* See if it's a virt-specifier-qualifier. */
19066 if (token->type != CPP_NAME)
19067 break;
19068 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
19070 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19071 virt_specifier = VIRT_SPEC_OVERRIDE;
19073 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
19075 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19076 virt_specifier = VIRT_SPEC_FINAL;
19078 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
19080 virt_specifier = VIRT_SPEC_FINAL;
19082 else
19083 break;
19085 if (virt_specifiers & virt_specifier)
19087 error_at (token->location, "duplicate virt-specifier");
19088 cp_lexer_purge_token (parser->lexer);
19090 else
19092 cp_lexer_consume_token (parser->lexer);
19093 virt_specifiers |= virt_specifier;
19096 return virt_specifiers;
19099 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
19100 is in scope even though it isn't real. */
19102 void
19103 inject_this_parameter (tree ctype, cp_cv_quals quals)
19105 tree this_parm;
19107 if (current_class_ptr)
19109 /* We don't clear this between NSDMIs. Is it already what we want? */
19110 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
19111 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
19112 && cp_type_quals (type) == quals)
19113 return;
19116 this_parm = build_this_parm (ctype, quals);
19117 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
19118 current_class_ptr = NULL_TREE;
19119 current_class_ref
19120 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
19121 current_class_ptr = this_parm;
19124 /* Return true iff our current scope is a non-static data member
19125 initializer. */
19127 bool
19128 parsing_nsdmi (void)
19130 /* We recognize NSDMI context by the context-less 'this' pointer set up
19131 by the function above. */
19132 if (current_class_ptr
19133 && TREE_CODE (current_class_ptr) == PARM_DECL
19134 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
19135 return true;
19136 return false;
19139 /* Parse a late-specified return type, if any. This is not a separate
19140 non-terminal, but part of a function declarator, which looks like
19142 -> trailing-type-specifier-seq abstract-declarator(opt)
19144 Returns the type indicated by the type-id.
19146 In addition to this, parse any queued up omp declare simd
19147 clauses and Cilk Plus SIMD-enabled function's vector attributes.
19149 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
19150 function. */
19152 static tree
19153 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
19154 tree& requires_clause, cp_cv_quals quals)
19156 cp_token *token;
19157 tree type = NULL_TREE;
19158 bool declare_simd_p = (parser->omp_declare_simd
19159 && declarator
19160 && declarator->kind == cdk_id);
19162 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
19163 && declarator && declarator->kind == cdk_id);
19165 /* Peek at the next token. */
19166 token = cp_lexer_peek_token (parser->lexer);
19167 /* A late-specified return type is indicated by an initial '->'. */
19168 if (token->type != CPP_DEREF
19169 && token->keyword != RID_REQUIRES
19170 && !(declare_simd_p || cilk_simd_fn_vector_p))
19171 return NULL_TREE;
19173 tree save_ccp = current_class_ptr;
19174 tree save_ccr = current_class_ref;
19175 if (quals >= 0)
19177 /* DR 1207: 'this' is in scope in the trailing return type. */
19178 inject_this_parameter (current_class_type, quals);
19181 if (token->type == CPP_DEREF)
19183 /* Consume the ->. */
19184 cp_lexer_consume_token (parser->lexer);
19186 type = cp_parser_trailing_type_id (parser);
19189 /* Function declarations may be followed by a trailing
19190 requires-clause. */
19191 requires_clause = cp_parser_requires_clause_opt (parser);
19193 if (cilk_simd_fn_vector_p)
19194 declarator->std_attributes
19195 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
19196 declarator->std_attributes);
19197 if (declare_simd_p)
19198 declarator->std_attributes
19199 = cp_parser_late_parsing_omp_declare_simd (parser,
19200 declarator->std_attributes);
19202 if (quals >= 0)
19204 current_class_ptr = save_ccp;
19205 current_class_ref = save_ccr;
19208 return type;
19211 /* Parse a declarator-id.
19213 declarator-id:
19214 id-expression
19215 :: [opt] nested-name-specifier [opt] type-name
19217 In the `id-expression' case, the value returned is as for
19218 cp_parser_id_expression if the id-expression was an unqualified-id.
19219 If the id-expression was a qualified-id, then a SCOPE_REF is
19220 returned. The first operand is the scope (either a NAMESPACE_DECL
19221 or TREE_TYPE), but the second is still just a representation of an
19222 unqualified-id. */
19224 static tree
19225 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
19227 tree id;
19228 /* The expression must be an id-expression. Assume that qualified
19229 names are the names of types so that:
19231 template <class T>
19232 int S<T>::R::i = 3;
19234 will work; we must treat `S<T>::R' as the name of a type.
19235 Similarly, assume that qualified names are templates, where
19236 required, so that:
19238 template <class T>
19239 int S<T>::R<T>::i = 3;
19241 will work, too. */
19242 id = cp_parser_id_expression (parser,
19243 /*template_keyword_p=*/false,
19244 /*check_dependency_p=*/false,
19245 /*template_p=*/NULL,
19246 /*declarator_p=*/true,
19247 optional_p);
19248 if (id && BASELINK_P (id))
19249 id = BASELINK_FUNCTIONS (id);
19250 return id;
19253 /* Parse a type-id.
19255 type-id:
19256 type-specifier-seq abstract-declarator [opt]
19258 Returns the TYPE specified. */
19260 static tree
19261 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
19262 bool is_trailing_return)
19264 cp_decl_specifier_seq type_specifier_seq;
19265 cp_declarator *abstract_declarator;
19267 /* Parse the type-specifier-seq. */
19268 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
19269 is_trailing_return,
19270 &type_specifier_seq);
19271 if (type_specifier_seq.type == error_mark_node)
19272 return error_mark_node;
19274 /* There might or might not be an abstract declarator. */
19275 cp_parser_parse_tentatively (parser);
19276 /* Look for the declarator. */
19277 abstract_declarator
19278 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
19279 /*parenthesized_p=*/NULL,
19280 /*member_p=*/false,
19281 /*friend_p=*/false);
19282 /* Check to see if there really was a declarator. */
19283 if (!cp_parser_parse_definitely (parser))
19284 abstract_declarator = NULL;
19286 if (type_specifier_seq.type
19287 /* None of the valid uses of 'auto' in C++14 involve the type-id
19288 nonterminal, but it is valid in a trailing-return-type. */
19289 && !(cxx_dialect >= cxx14 && is_trailing_return)
19290 && type_uses_auto (type_specifier_seq.type))
19292 /* A type-id with type 'auto' is only ok if the abstract declarator
19293 is a function declarator with a late-specified return type.
19295 A type-id with 'auto' is also valid in a trailing-return-type
19296 in a compound-requirement. */
19297 if (abstract_declarator
19298 && abstract_declarator->kind == cdk_function
19299 && abstract_declarator->u.function.late_return_type)
19300 /* OK */;
19301 else if (parser->in_result_type_constraint_p)
19302 /* OK */;
19303 else
19305 error ("invalid use of %<auto%>");
19306 return error_mark_node;
19310 return groktypename (&type_specifier_seq, abstract_declarator,
19311 is_template_arg);
19314 static tree cp_parser_type_id (cp_parser *parser)
19316 return cp_parser_type_id_1 (parser, false, false);
19319 static tree cp_parser_template_type_arg (cp_parser *parser)
19321 tree r;
19322 const char *saved_message = parser->type_definition_forbidden_message;
19323 parser->type_definition_forbidden_message
19324 = G_("types may not be defined in template arguments");
19325 r = cp_parser_type_id_1 (parser, true, false);
19326 parser->type_definition_forbidden_message = saved_message;
19327 if (cxx_dialect >= cxx14 && type_uses_auto (r))
19329 error ("invalid use of %<auto%> in template argument");
19330 r = error_mark_node;
19332 return r;
19335 static tree
19336 cp_parser_trailing_type_id (cp_parser *parser)
19338 return cp_parser_type_id_1 (parser, false, true);
19341 /* Parse a type-specifier-seq.
19343 type-specifier-seq:
19344 type-specifier type-specifier-seq [opt]
19346 GNU extension:
19348 type-specifier-seq:
19349 attributes type-specifier-seq [opt]
19351 If IS_DECLARATION is true, we are at the start of a "condition" or
19352 exception-declaration, so we might be followed by a declarator-id.
19354 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
19355 i.e. we've just seen "->".
19357 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
19359 static void
19360 cp_parser_type_specifier_seq (cp_parser* parser,
19361 bool is_declaration,
19362 bool is_trailing_return,
19363 cp_decl_specifier_seq *type_specifier_seq)
19365 bool seen_type_specifier = false;
19366 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
19367 cp_token *start_token = NULL;
19369 /* Clear the TYPE_SPECIFIER_SEQ. */
19370 clear_decl_specs (type_specifier_seq);
19372 /* In the context of a trailing return type, enum E { } is an
19373 elaborated-type-specifier followed by a function-body, not an
19374 enum-specifier. */
19375 if (is_trailing_return)
19376 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
19378 /* Parse the type-specifiers and attributes. */
19379 while (true)
19381 tree type_specifier;
19382 bool is_cv_qualifier;
19384 /* Check for attributes first. */
19385 if (cp_next_tokens_can_be_attribute_p (parser))
19387 type_specifier_seq->attributes =
19388 chainon (type_specifier_seq->attributes,
19389 cp_parser_attributes_opt (parser));
19390 continue;
19393 /* record the token of the beginning of the type specifier seq,
19394 for error reporting purposes*/
19395 if (!start_token)
19396 start_token = cp_lexer_peek_token (parser->lexer);
19398 /* Look for the type-specifier. */
19399 type_specifier = cp_parser_type_specifier (parser,
19400 flags,
19401 type_specifier_seq,
19402 /*is_declaration=*/false,
19403 NULL,
19404 &is_cv_qualifier);
19405 if (!type_specifier)
19407 /* If the first type-specifier could not be found, this is not a
19408 type-specifier-seq at all. */
19409 if (!seen_type_specifier)
19411 /* Set in_declarator_p to avoid skipping to the semicolon. */
19412 int in_decl = parser->in_declarator_p;
19413 parser->in_declarator_p = true;
19415 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
19416 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
19417 cp_parser_error (parser, "expected type-specifier");
19419 parser->in_declarator_p = in_decl;
19421 type_specifier_seq->type = error_mark_node;
19422 return;
19424 /* If subsequent type-specifiers could not be found, the
19425 type-specifier-seq is complete. */
19426 break;
19429 seen_type_specifier = true;
19430 /* The standard says that a condition can be:
19432 type-specifier-seq declarator = assignment-expression
19434 However, given:
19436 struct S {};
19437 if (int S = ...)
19439 we should treat the "S" as a declarator, not as a
19440 type-specifier. The standard doesn't say that explicitly for
19441 type-specifier-seq, but it does say that for
19442 decl-specifier-seq in an ordinary declaration. Perhaps it
19443 would be clearer just to allow a decl-specifier-seq here, and
19444 then add a semantic restriction that if any decl-specifiers
19445 that are not type-specifiers appear, the program is invalid. */
19446 if (is_declaration && !is_cv_qualifier)
19447 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
19451 /* Return whether the function currently being declared has an associated
19452 template parameter list. */
19454 static bool
19455 function_being_declared_is_template_p (cp_parser* parser)
19457 if (!current_template_parms || processing_template_parmlist)
19458 return false;
19460 if (parser->implicit_template_scope)
19461 return true;
19463 if (at_class_scope_p ()
19464 && TYPE_BEING_DEFINED (current_class_type))
19465 return parser->num_template_parameter_lists != 0;
19467 return ((int) parser->num_template_parameter_lists > template_class_depth
19468 (current_class_type));
19471 /* Parse a parameter-declaration-clause.
19473 parameter-declaration-clause:
19474 parameter-declaration-list [opt] ... [opt]
19475 parameter-declaration-list , ...
19477 Returns a representation for the parameter declarations. A return
19478 value of NULL indicates a parameter-declaration-clause consisting
19479 only of an ellipsis. */
19481 static tree
19482 cp_parser_parameter_declaration_clause (cp_parser* parser)
19484 tree parameters;
19485 cp_token *token;
19486 bool ellipsis_p;
19487 bool is_error;
19489 struct cleanup {
19490 cp_parser* parser;
19491 int auto_is_implicit_function_template_parm_p;
19492 ~cleanup() {
19493 parser->auto_is_implicit_function_template_parm_p
19494 = auto_is_implicit_function_template_parm_p;
19496 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
19498 (void) cleanup;
19500 if (!processing_specialization
19501 && !processing_template_parmlist
19502 && !processing_explicit_instantiation)
19503 if (!current_function_decl
19504 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
19505 parser->auto_is_implicit_function_template_parm_p = true;
19507 /* Peek at the next token. */
19508 token = cp_lexer_peek_token (parser->lexer);
19509 /* Check for trivial parameter-declaration-clauses. */
19510 if (token->type == CPP_ELLIPSIS)
19512 /* Consume the `...' token. */
19513 cp_lexer_consume_token (parser->lexer);
19514 return NULL_TREE;
19516 else if (token->type == CPP_CLOSE_PAREN)
19517 /* There are no parameters. */
19519 #ifndef NO_IMPLICIT_EXTERN_C
19520 if (in_system_header_at (input_location)
19521 && current_class_type == NULL
19522 && current_lang_name == lang_name_c)
19523 return NULL_TREE;
19524 else
19525 #endif
19526 return void_list_node;
19528 /* Check for `(void)', too, which is a special case. */
19529 else if (token->keyword == RID_VOID
19530 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19531 == CPP_CLOSE_PAREN))
19533 /* Consume the `void' token. */
19534 cp_lexer_consume_token (parser->lexer);
19535 /* There are no parameters. */
19536 return void_list_node;
19539 /* Parse the parameter-declaration-list. */
19540 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
19541 /* If a parse error occurred while parsing the
19542 parameter-declaration-list, then the entire
19543 parameter-declaration-clause is erroneous. */
19544 if (is_error)
19545 return NULL;
19547 /* Peek at the next token. */
19548 token = cp_lexer_peek_token (parser->lexer);
19549 /* If it's a `,', the clause should terminate with an ellipsis. */
19550 if (token->type == CPP_COMMA)
19552 /* Consume the `,'. */
19553 cp_lexer_consume_token (parser->lexer);
19554 /* Expect an ellipsis. */
19555 ellipsis_p
19556 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
19558 /* It might also be `...' if the optional trailing `,' was
19559 omitted. */
19560 else if (token->type == CPP_ELLIPSIS)
19562 /* Consume the `...' token. */
19563 cp_lexer_consume_token (parser->lexer);
19564 /* And remember that we saw it. */
19565 ellipsis_p = true;
19567 else
19568 ellipsis_p = false;
19570 /* Finish the parameter list. */
19571 if (!ellipsis_p)
19572 parameters = chainon (parameters, void_list_node);
19574 return parameters;
19577 /* Parse a parameter-declaration-list.
19579 parameter-declaration-list:
19580 parameter-declaration
19581 parameter-declaration-list , parameter-declaration
19583 Returns a representation of the parameter-declaration-list, as for
19584 cp_parser_parameter_declaration_clause. However, the
19585 `void_list_node' is never appended to the list. Upon return,
19586 *IS_ERROR will be true iff an error occurred. */
19588 static tree
19589 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
19591 tree parameters = NULL_TREE;
19592 tree *tail = &parameters;
19593 bool saved_in_unbraced_linkage_specification_p;
19594 int index = 0;
19596 /* Assume all will go well. */
19597 *is_error = false;
19598 /* The special considerations that apply to a function within an
19599 unbraced linkage specifications do not apply to the parameters
19600 to the function. */
19601 saved_in_unbraced_linkage_specification_p
19602 = parser->in_unbraced_linkage_specification_p;
19603 parser->in_unbraced_linkage_specification_p = false;
19605 /* Look for more parameters. */
19606 while (true)
19608 cp_parameter_declarator *parameter;
19609 tree decl = error_mark_node;
19610 bool parenthesized_p = false;
19611 int template_parm_idx = (function_being_declared_is_template_p (parser)?
19612 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
19613 (current_template_parms)) : 0);
19615 /* Parse the parameter. */
19616 parameter
19617 = cp_parser_parameter_declaration (parser,
19618 /*template_parm_p=*/false,
19619 &parenthesized_p);
19621 /* We don't know yet if the enclosing context is deprecated, so wait
19622 and warn in grokparms if appropriate. */
19623 deprecated_state = DEPRECATED_SUPPRESS;
19625 if (parameter)
19627 /* If a function parameter pack was specified and an implicit template
19628 parameter was introduced during cp_parser_parameter_declaration,
19629 change any implicit parameters introduced into packs. */
19630 if (parser->implicit_template_parms
19631 && parameter->declarator
19632 && parameter->declarator->parameter_pack_p)
19634 int latest_template_parm_idx = TREE_VEC_LENGTH
19635 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
19637 if (latest_template_parm_idx != template_parm_idx)
19638 parameter->decl_specifiers.type = convert_generic_types_to_packs
19639 (parameter->decl_specifiers.type,
19640 template_parm_idx, latest_template_parm_idx);
19643 decl = grokdeclarator (parameter->declarator,
19644 &parameter->decl_specifiers,
19645 PARM,
19646 parameter->default_argument != NULL_TREE,
19647 &parameter->decl_specifiers.attributes);
19650 deprecated_state = DEPRECATED_NORMAL;
19652 /* If a parse error occurred parsing the parameter declaration,
19653 then the entire parameter-declaration-list is erroneous. */
19654 if (decl == error_mark_node)
19656 *is_error = true;
19657 parameters = error_mark_node;
19658 break;
19661 if (parameter->decl_specifiers.attributes)
19662 cplus_decl_attributes (&decl,
19663 parameter->decl_specifiers.attributes,
19665 if (DECL_NAME (decl))
19666 decl = pushdecl (decl);
19668 if (decl != error_mark_node)
19670 retrofit_lang_decl (decl);
19671 DECL_PARM_INDEX (decl) = ++index;
19672 DECL_PARM_LEVEL (decl) = function_parm_depth ();
19675 /* Add the new parameter to the list. */
19676 *tail = build_tree_list (parameter->default_argument, decl);
19677 tail = &TREE_CHAIN (*tail);
19679 /* Peek at the next token. */
19680 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
19681 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
19682 /* These are for Objective-C++ */
19683 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19684 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19685 /* The parameter-declaration-list is complete. */
19686 break;
19687 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19689 cp_token *token;
19691 /* Peek at the next token. */
19692 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19693 /* If it's an ellipsis, then the list is complete. */
19694 if (token->type == CPP_ELLIPSIS)
19695 break;
19696 /* Otherwise, there must be more parameters. Consume the
19697 `,'. */
19698 cp_lexer_consume_token (parser->lexer);
19699 /* When parsing something like:
19701 int i(float f, double d)
19703 we can tell after seeing the declaration for "f" that we
19704 are not looking at an initialization of a variable "i",
19705 but rather at the declaration of a function "i".
19707 Due to the fact that the parsing of template arguments
19708 (as specified to a template-id) requires backtracking we
19709 cannot use this technique when inside a template argument
19710 list. */
19711 if (!parser->in_template_argument_list_p
19712 && !parser->in_type_id_in_expr_p
19713 && cp_parser_uncommitted_to_tentative_parse_p (parser)
19714 /* However, a parameter-declaration of the form
19715 "float(f)" (which is a valid declaration of a
19716 parameter "f") can also be interpreted as an
19717 expression (the conversion of "f" to "float"). */
19718 && !parenthesized_p)
19719 cp_parser_commit_to_tentative_parse (parser);
19721 else
19723 cp_parser_error (parser, "expected %<,%> or %<...%>");
19724 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19725 cp_parser_skip_to_closing_parenthesis (parser,
19726 /*recovering=*/true,
19727 /*or_comma=*/false,
19728 /*consume_paren=*/false);
19729 break;
19733 parser->in_unbraced_linkage_specification_p
19734 = saved_in_unbraced_linkage_specification_p;
19736 /* Reset implicit_template_scope if we are about to leave the function
19737 parameter list that introduced it. Note that for out-of-line member
19738 definitions, there will be one or more class scopes before we get to
19739 the template parameter scope. */
19741 if (cp_binding_level *its = parser->implicit_template_scope)
19742 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
19744 while (maybe_its->kind == sk_class)
19745 maybe_its = maybe_its->level_chain;
19746 if (maybe_its == its)
19748 parser->implicit_template_parms = 0;
19749 parser->implicit_template_scope = 0;
19753 return parameters;
19756 /* Parse a parameter declaration.
19758 parameter-declaration:
19759 decl-specifier-seq ... [opt] declarator
19760 decl-specifier-seq declarator = assignment-expression
19761 decl-specifier-seq ... [opt] abstract-declarator [opt]
19762 decl-specifier-seq abstract-declarator [opt] = assignment-expression
19764 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
19765 declares a template parameter. (In that case, a non-nested `>'
19766 token encountered during the parsing of the assignment-expression
19767 is not interpreted as a greater-than operator.)
19769 Returns a representation of the parameter, or NULL if an error
19770 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
19771 true iff the declarator is of the form "(p)". */
19773 static cp_parameter_declarator *
19774 cp_parser_parameter_declaration (cp_parser *parser,
19775 bool template_parm_p,
19776 bool *parenthesized_p)
19778 int declares_class_or_enum;
19779 cp_decl_specifier_seq decl_specifiers;
19780 cp_declarator *declarator;
19781 tree default_argument;
19782 cp_token *token = NULL, *declarator_token_start = NULL;
19783 const char *saved_message;
19784 bool template_parameter_pack_p = false;
19786 /* In a template parameter, `>' is not an operator.
19788 [temp.param]
19790 When parsing a default template-argument for a non-type
19791 template-parameter, the first non-nested `>' is taken as the end
19792 of the template parameter-list rather than a greater-than
19793 operator. */
19795 /* Type definitions may not appear in parameter types. */
19796 saved_message = parser->type_definition_forbidden_message;
19797 parser->type_definition_forbidden_message
19798 = G_("types may not be defined in parameter types");
19800 /* Parse the declaration-specifiers. */
19801 cp_parser_decl_specifier_seq (parser,
19802 CP_PARSER_FLAGS_NONE,
19803 &decl_specifiers,
19804 &declares_class_or_enum);
19806 /* Complain about missing 'typename' or other invalid type names. */
19807 if (!decl_specifiers.any_type_specifiers_p
19808 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
19809 decl_specifiers.type = error_mark_node;
19811 /* If an error occurred, there's no reason to attempt to parse the
19812 rest of the declaration. */
19813 if (cp_parser_error_occurred (parser))
19815 parser->type_definition_forbidden_message = saved_message;
19816 return NULL;
19819 /* Peek at the next token. */
19820 token = cp_lexer_peek_token (parser->lexer);
19822 /* If the next token is a `)', `,', `=', `>', or `...', then there
19823 is no declarator. However, when variadic templates are enabled,
19824 there may be a declarator following `...'. */
19825 if (token->type == CPP_CLOSE_PAREN
19826 || token->type == CPP_COMMA
19827 || token->type == CPP_EQ
19828 || token->type == CPP_GREATER)
19830 declarator = NULL;
19831 if (parenthesized_p)
19832 *parenthesized_p = false;
19834 /* Otherwise, there should be a declarator. */
19835 else
19837 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19838 parser->default_arg_ok_p = false;
19840 /* After seeing a decl-specifier-seq, if the next token is not a
19841 "(", there is no possibility that the code is a valid
19842 expression. Therefore, if parsing tentatively, we commit at
19843 this point. */
19844 if (!parser->in_template_argument_list_p
19845 /* In an expression context, having seen:
19847 (int((char ...
19849 we cannot be sure whether we are looking at a
19850 function-type (taking a "char" as a parameter) or a cast
19851 of some object of type "char" to "int". */
19852 && !parser->in_type_id_in_expr_p
19853 && cp_parser_uncommitted_to_tentative_parse_p (parser)
19854 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19855 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
19856 cp_parser_commit_to_tentative_parse (parser);
19857 /* Parse the declarator. */
19858 declarator_token_start = token;
19859 declarator = cp_parser_declarator (parser,
19860 CP_PARSER_DECLARATOR_EITHER,
19861 /*ctor_dtor_or_conv_p=*/NULL,
19862 parenthesized_p,
19863 /*member_p=*/false,
19864 /*friend_p=*/false);
19865 parser->default_arg_ok_p = saved_default_arg_ok_p;
19866 /* After the declarator, allow more attributes. */
19867 decl_specifiers.attributes
19868 = chainon (decl_specifiers.attributes,
19869 cp_parser_attributes_opt (parser));
19871 /* If the declarator is a template parameter pack, remember that and
19872 clear the flag in the declarator itself so we don't get errors
19873 from grokdeclarator. */
19874 if (template_parm_p && declarator && declarator->parameter_pack_p)
19876 declarator->parameter_pack_p = false;
19877 template_parameter_pack_p = true;
19881 /* If the next token is an ellipsis, and we have not seen a declarator
19882 name, and if either the type of the declarator contains parameter
19883 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
19884 for, eg, abbreviated integral type names), then we actually have a
19885 parameter pack expansion expression. Otherwise, leave the ellipsis
19886 for a C-style variadic function. */
19887 token = cp_lexer_peek_token (parser->lexer);
19888 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19890 tree type = decl_specifiers.type;
19892 if (type && DECL_P (type))
19893 type = TREE_TYPE (type);
19895 if (((type
19896 && TREE_CODE (type) != TYPE_PACK_EXPANSION
19897 && (template_parm_p || uses_parameter_packs (type)))
19898 || (!type && template_parm_p))
19899 && declarator_can_be_parameter_pack (declarator))
19901 /* Consume the `...'. */
19902 cp_lexer_consume_token (parser->lexer);
19903 maybe_warn_variadic_templates ();
19905 /* Build a pack expansion type */
19906 if (template_parm_p)
19907 template_parameter_pack_p = true;
19908 else if (declarator)
19909 declarator->parameter_pack_p = true;
19910 else
19911 decl_specifiers.type = make_pack_expansion (type);
19915 /* The restriction on defining new types applies only to the type
19916 of the parameter, not to the default argument. */
19917 parser->type_definition_forbidden_message = saved_message;
19919 /* If the next token is `=', then process a default argument. */
19920 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19922 tree type = decl_specifiers.type;
19923 token = cp_lexer_peek_token (parser->lexer);
19924 /* If we are defining a class, then the tokens that make up the
19925 default argument must be saved and processed later. */
19926 if (!template_parm_p && at_class_scope_p ()
19927 && TYPE_BEING_DEFINED (current_class_type)
19928 && !LAMBDA_TYPE_P (current_class_type))
19929 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
19931 // A constrained-type-specifier may declare a type template-parameter.
19932 else if (declares_constrained_type_template_parameter (type))
19933 default_argument
19934 = cp_parser_default_type_template_argument (parser);
19936 // A constrained-type-specifier may declare a template-template-parameter.
19937 else if (declares_constrained_template_template_parameter (type))
19938 default_argument
19939 = cp_parser_default_template_template_argument (parser);
19941 /* Outside of a class definition, we can just parse the
19942 assignment-expression. */
19943 else
19944 default_argument
19945 = cp_parser_default_argument (parser, template_parm_p);
19947 if (!parser->default_arg_ok_p)
19949 permerror (token->location,
19950 "default arguments are only "
19951 "permitted for function parameters");
19953 else if ((declarator && declarator->parameter_pack_p)
19954 || template_parameter_pack_p
19955 || (decl_specifiers.type
19956 && PACK_EXPANSION_P (decl_specifiers.type)))
19958 /* Find the name of the parameter pack. */
19959 cp_declarator *id_declarator = declarator;
19960 while (id_declarator && id_declarator->kind != cdk_id)
19961 id_declarator = id_declarator->declarator;
19963 if (id_declarator && id_declarator->kind == cdk_id)
19964 error_at (declarator_token_start->location,
19965 template_parm_p
19966 ? G_("template parameter pack %qD "
19967 "cannot have a default argument")
19968 : G_("parameter pack %qD cannot have "
19969 "a default argument"),
19970 id_declarator->u.id.unqualified_name);
19971 else
19972 error_at (declarator_token_start->location,
19973 template_parm_p
19974 ? G_("template parameter pack cannot have "
19975 "a default argument")
19976 : G_("parameter pack cannot have a "
19977 "default argument"));
19979 default_argument = NULL_TREE;
19982 else
19983 default_argument = NULL_TREE;
19985 return make_parameter_declarator (&decl_specifiers,
19986 declarator,
19987 default_argument,
19988 template_parameter_pack_p);
19991 /* Parse a default argument and return it.
19993 TEMPLATE_PARM_P is true if this is a default argument for a
19994 non-type template parameter. */
19995 static tree
19996 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
19998 tree default_argument = NULL_TREE;
19999 bool saved_greater_than_is_operator_p;
20000 bool saved_local_variables_forbidden_p;
20001 bool non_constant_p, is_direct_init;
20003 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
20004 set correctly. */
20005 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
20006 parser->greater_than_is_operator_p = !template_parm_p;
20007 /* Local variable names (and the `this' keyword) may not
20008 appear in a default argument. */
20009 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20010 parser->local_variables_forbidden_p = true;
20011 /* Parse the assignment-expression. */
20012 if (template_parm_p)
20013 push_deferring_access_checks (dk_no_deferred);
20014 tree saved_class_ptr = NULL_TREE;
20015 tree saved_class_ref = NULL_TREE;
20016 /* The "this" pointer is not valid in a default argument. */
20017 if (cfun)
20019 saved_class_ptr = current_class_ptr;
20020 cp_function_chain->x_current_class_ptr = NULL_TREE;
20021 saved_class_ref = current_class_ref;
20022 cp_function_chain->x_current_class_ref = NULL_TREE;
20024 default_argument
20025 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
20026 /* Restore the "this" pointer. */
20027 if (cfun)
20029 cp_function_chain->x_current_class_ptr = saved_class_ptr;
20030 cp_function_chain->x_current_class_ref = saved_class_ref;
20032 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
20033 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20034 if (template_parm_p)
20035 pop_deferring_access_checks ();
20036 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
20037 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20039 return default_argument;
20042 /* Parse a function-body.
20044 function-body:
20045 compound_statement */
20047 static void
20048 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
20050 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
20053 /* Parse a ctor-initializer-opt followed by a function-body. Return
20054 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
20055 is true we are parsing a function-try-block. */
20057 static bool
20058 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
20059 bool in_function_try_block)
20061 tree body, list;
20062 bool ctor_initializer_p;
20063 const bool check_body_p =
20064 DECL_CONSTRUCTOR_P (current_function_decl)
20065 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
20066 tree last = NULL;
20068 /* Begin the function body. */
20069 body = begin_function_body ();
20070 /* Parse the optional ctor-initializer. */
20071 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
20073 /* If we're parsing a constexpr constructor definition, we need
20074 to check that the constructor body is indeed empty. However,
20075 before we get to cp_parser_function_body lot of junk has been
20076 generated, so we can't just check that we have an empty block.
20077 Rather we take a snapshot of the outermost block, and check whether
20078 cp_parser_function_body changed its state. */
20079 if (check_body_p)
20081 list = cur_stmt_list;
20082 if (STATEMENT_LIST_TAIL (list))
20083 last = STATEMENT_LIST_TAIL (list)->stmt;
20085 /* Parse the function-body. */
20086 cp_parser_function_body (parser, in_function_try_block);
20087 if (check_body_p)
20088 check_constexpr_ctor_body (last, list, /*complain=*/true);
20089 /* Finish the function body. */
20090 finish_function_body (body);
20092 return ctor_initializer_p;
20095 /* Parse an initializer.
20097 initializer:
20098 = initializer-clause
20099 ( expression-list )
20101 Returns an expression representing the initializer. If no
20102 initializer is present, NULL_TREE is returned.
20104 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
20105 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
20106 set to TRUE if there is no initializer present. If there is an
20107 initializer, and it is not a constant-expression, *NON_CONSTANT_P
20108 is set to true; otherwise it is set to false. */
20110 static tree
20111 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
20112 bool* non_constant_p)
20114 cp_token *token;
20115 tree init;
20117 /* Peek at the next token. */
20118 token = cp_lexer_peek_token (parser->lexer);
20120 /* Let our caller know whether or not this initializer was
20121 parenthesized. */
20122 *is_direct_init = (token->type != CPP_EQ);
20123 /* Assume that the initializer is constant. */
20124 *non_constant_p = false;
20126 if (token->type == CPP_EQ)
20128 /* Consume the `='. */
20129 cp_lexer_consume_token (parser->lexer);
20130 /* Parse the initializer-clause. */
20131 init = cp_parser_initializer_clause (parser, non_constant_p);
20133 else if (token->type == CPP_OPEN_PAREN)
20135 vec<tree, va_gc> *vec;
20136 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20137 /*cast_p=*/false,
20138 /*allow_expansion_p=*/true,
20139 non_constant_p);
20140 if (vec == NULL)
20141 return error_mark_node;
20142 init = build_tree_list_vec (vec);
20143 release_tree_vector (vec);
20145 else if (token->type == CPP_OPEN_BRACE)
20147 cp_lexer_set_source_position (parser->lexer);
20148 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20149 init = cp_parser_braced_list (parser, non_constant_p);
20150 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
20152 else
20154 /* Anything else is an error. */
20155 cp_parser_error (parser, "expected initializer");
20156 init = error_mark_node;
20159 return init;
20162 /* Parse an initializer-clause.
20164 initializer-clause:
20165 assignment-expression
20166 braced-init-list
20168 Returns an expression representing the initializer.
20170 If the `assignment-expression' production is used the value
20171 returned is simply a representation for the expression.
20173 Otherwise, calls cp_parser_braced_list. */
20175 static tree
20176 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
20178 tree initializer;
20180 /* Assume the expression is constant. */
20181 *non_constant_p = false;
20183 /* If it is not a `{', then we are looking at an
20184 assignment-expression. */
20185 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
20187 initializer
20188 = cp_parser_constant_expression (parser,
20189 /*allow_non_constant_p=*/true,
20190 non_constant_p);
20192 else
20193 initializer = cp_parser_braced_list (parser, non_constant_p);
20195 return initializer;
20198 /* Parse a brace-enclosed initializer list.
20200 braced-init-list:
20201 { initializer-list , [opt] }
20204 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
20205 the elements of the initializer-list (or NULL, if the last
20206 production is used). The TREE_TYPE for the CONSTRUCTOR will be
20207 NULL_TREE. There is no way to detect whether or not the optional
20208 trailing `,' was provided. NON_CONSTANT_P is as for
20209 cp_parser_initializer. */
20211 static tree
20212 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
20214 tree initializer;
20216 /* Consume the `{' token. */
20217 cp_lexer_consume_token (parser->lexer);
20218 /* Create a CONSTRUCTOR to represent the braced-initializer. */
20219 initializer = make_node (CONSTRUCTOR);
20220 /* If it's not a `}', then there is a non-trivial initializer. */
20221 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
20223 /* Parse the initializer list. */
20224 CONSTRUCTOR_ELTS (initializer)
20225 = cp_parser_initializer_list (parser, non_constant_p);
20226 /* A trailing `,' token is allowed. */
20227 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20228 cp_lexer_consume_token (parser->lexer);
20230 else
20231 *non_constant_p = false;
20232 /* Now, there should be a trailing `}'. */
20233 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20234 TREE_TYPE (initializer) = init_list_type_node;
20235 return initializer;
20238 /* Consume tokens up to, and including, the next non-nested closing `]'.
20239 Returns true iff we found a closing `]'. */
20241 static bool
20242 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
20244 unsigned square_depth = 0;
20246 while (true)
20248 cp_token * token = cp_lexer_peek_token (parser->lexer);
20250 switch (token->type)
20252 case CPP_EOF:
20253 case CPP_PRAGMA_EOL:
20254 /* If we've run out of tokens, then there is no closing `]'. */
20255 return false;
20257 case CPP_OPEN_SQUARE:
20258 ++square_depth;
20259 break;
20261 case CPP_CLOSE_SQUARE:
20262 if (!square_depth--)
20264 cp_lexer_consume_token (parser->lexer);
20265 return true;
20267 break;
20269 default:
20270 break;
20273 /* Consume the token. */
20274 cp_lexer_consume_token (parser->lexer);
20278 /* Return true if we are looking at an array-designator, false otherwise. */
20280 static bool
20281 cp_parser_array_designator_p (cp_parser *parser)
20283 /* Consume the `['. */
20284 cp_lexer_consume_token (parser->lexer);
20286 cp_lexer_save_tokens (parser->lexer);
20288 /* Skip tokens until the next token is a closing square bracket.
20289 If we find the closing `]', and the next token is a `=', then
20290 we are looking at an array designator. */
20291 bool array_designator_p
20292 = (cp_parser_skip_to_closing_square_bracket (parser)
20293 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
20295 /* Roll back the tokens we skipped. */
20296 cp_lexer_rollback_tokens (parser->lexer);
20298 return array_designator_p;
20301 /* Parse an initializer-list.
20303 initializer-list:
20304 initializer-clause ... [opt]
20305 initializer-list , initializer-clause ... [opt]
20307 GNU Extension:
20309 initializer-list:
20310 designation initializer-clause ...[opt]
20311 initializer-list , designation initializer-clause ...[opt]
20313 designation:
20314 . identifier =
20315 identifier :
20316 [ constant-expression ] =
20318 Returns a vec of constructor_elt. The VALUE of each elt is an expression
20319 for the initializer. If the INDEX of the elt is non-NULL, it is the
20320 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
20321 as for cp_parser_initializer. */
20323 static vec<constructor_elt, va_gc> *
20324 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
20326 vec<constructor_elt, va_gc> *v = NULL;
20328 /* Assume all of the expressions are constant. */
20329 *non_constant_p = false;
20331 /* Parse the rest of the list. */
20332 while (true)
20334 cp_token *token;
20335 tree designator;
20336 tree initializer;
20337 bool clause_non_constant_p;
20339 /* If the next token is an identifier and the following one is a
20340 colon, we are looking at the GNU designated-initializer
20341 syntax. */
20342 if (cp_parser_allow_gnu_extensions_p (parser)
20343 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
20344 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
20346 /* Warn the user that they are using an extension. */
20347 pedwarn (input_location, OPT_Wpedantic,
20348 "ISO C++ does not allow designated initializers");
20349 /* Consume the identifier. */
20350 designator = cp_lexer_consume_token (parser->lexer)->u.value;
20351 /* Consume the `:'. */
20352 cp_lexer_consume_token (parser->lexer);
20354 /* Also handle the C99 syntax, '. id ='. */
20355 else if (cp_parser_allow_gnu_extensions_p (parser)
20356 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
20357 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
20358 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
20360 /* Warn the user that they are using an extension. */
20361 pedwarn (input_location, OPT_Wpedantic,
20362 "ISO C++ does not allow C99 designated initializers");
20363 /* Consume the `.'. */
20364 cp_lexer_consume_token (parser->lexer);
20365 /* Consume the identifier. */
20366 designator = cp_lexer_consume_token (parser->lexer)->u.value;
20367 /* Consume the `='. */
20368 cp_lexer_consume_token (parser->lexer);
20370 /* Also handle C99 array designators, '[ const ] ='. */
20371 else if (cp_parser_allow_gnu_extensions_p (parser)
20372 && !c_dialect_objc ()
20373 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
20375 /* In C++11, [ could start a lambda-introducer. */
20376 bool non_const = false;
20378 cp_parser_parse_tentatively (parser);
20380 if (!cp_parser_array_designator_p (parser))
20382 cp_parser_simulate_error (parser);
20383 designator = NULL_TREE;
20385 else
20387 designator = cp_parser_constant_expression (parser, true,
20388 &non_const);
20389 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
20390 cp_parser_require (parser, CPP_EQ, RT_EQ);
20393 if (!cp_parser_parse_definitely (parser))
20394 designator = NULL_TREE;
20395 else if (non_const)
20396 require_potential_rvalue_constant_expression (designator);
20398 else
20399 designator = NULL_TREE;
20401 /* Parse the initializer. */
20402 initializer = cp_parser_initializer_clause (parser,
20403 &clause_non_constant_p);
20404 /* If any clause is non-constant, so is the entire initializer. */
20405 if (clause_non_constant_p)
20406 *non_constant_p = true;
20408 /* If we have an ellipsis, this is an initializer pack
20409 expansion. */
20410 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20412 /* Consume the `...'. */
20413 cp_lexer_consume_token (parser->lexer);
20415 /* Turn the initializer into an initializer expansion. */
20416 initializer = make_pack_expansion (initializer);
20419 /* Add it to the vector. */
20420 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
20422 /* If the next token is not a comma, we have reached the end of
20423 the list. */
20424 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20425 break;
20427 /* Peek at the next token. */
20428 token = cp_lexer_peek_nth_token (parser->lexer, 2);
20429 /* If the next token is a `}', then we're still done. An
20430 initializer-clause can have a trailing `,' after the
20431 initializer-list and before the closing `}'. */
20432 if (token->type == CPP_CLOSE_BRACE)
20433 break;
20435 /* Consume the `,' token. */
20436 cp_lexer_consume_token (parser->lexer);
20439 return v;
20442 /* Classes [gram.class] */
20444 /* Parse a class-name.
20446 class-name:
20447 identifier
20448 template-id
20450 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
20451 to indicate that names looked up in dependent types should be
20452 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
20453 keyword has been used to indicate that the name that appears next
20454 is a template. TAG_TYPE indicates the explicit tag given before
20455 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
20456 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
20457 is the class being defined in a class-head. If ENUM_OK is TRUE,
20458 enum-names are also accepted.
20460 Returns the TYPE_DECL representing the class. */
20462 static tree
20463 cp_parser_class_name (cp_parser *parser,
20464 bool typename_keyword_p,
20465 bool template_keyword_p,
20466 enum tag_types tag_type,
20467 bool check_dependency_p,
20468 bool class_head_p,
20469 bool is_declaration,
20470 bool enum_ok)
20472 tree decl;
20473 tree scope;
20474 bool typename_p;
20475 cp_token *token;
20476 tree identifier = NULL_TREE;
20478 /* All class-names start with an identifier. */
20479 token = cp_lexer_peek_token (parser->lexer);
20480 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
20482 cp_parser_error (parser, "expected class-name");
20483 return error_mark_node;
20486 /* PARSER->SCOPE can be cleared when parsing the template-arguments
20487 to a template-id, so we save it here. */
20488 scope = parser->scope;
20489 if (scope == error_mark_node)
20490 return error_mark_node;
20492 /* Any name names a type if we're following the `typename' keyword
20493 in a qualified name where the enclosing scope is type-dependent. */
20494 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
20495 && dependent_type_p (scope));
20496 /* Handle the common case (an identifier, but not a template-id)
20497 efficiently. */
20498 if (token->type == CPP_NAME
20499 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
20501 cp_token *identifier_token;
20502 bool ambiguous_p;
20504 /* Look for the identifier. */
20505 identifier_token = cp_lexer_peek_token (parser->lexer);
20506 ambiguous_p = identifier_token->error_reported;
20507 identifier = cp_parser_identifier (parser);
20508 /* If the next token isn't an identifier, we are certainly not
20509 looking at a class-name. */
20510 if (identifier == error_mark_node)
20511 decl = error_mark_node;
20512 /* If we know this is a type-name, there's no need to look it
20513 up. */
20514 else if (typename_p)
20515 decl = identifier;
20516 else
20518 tree ambiguous_decls;
20519 /* If we already know that this lookup is ambiguous, then
20520 we've already issued an error message; there's no reason
20521 to check again. */
20522 if (ambiguous_p)
20524 cp_parser_simulate_error (parser);
20525 return error_mark_node;
20527 /* If the next token is a `::', then the name must be a type
20528 name.
20530 [basic.lookup.qual]
20532 During the lookup for a name preceding the :: scope
20533 resolution operator, object, function, and enumerator
20534 names are ignored. */
20535 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
20536 tag_type = typename_type;
20537 /* Look up the name. */
20538 decl = cp_parser_lookup_name (parser, identifier,
20539 tag_type,
20540 /*is_template=*/false,
20541 /*is_namespace=*/false,
20542 check_dependency_p,
20543 &ambiguous_decls,
20544 identifier_token->location);
20545 if (ambiguous_decls)
20547 if (cp_parser_parsing_tentatively (parser))
20548 cp_parser_simulate_error (parser);
20549 return error_mark_node;
20553 else
20555 /* Try a template-id. */
20556 decl = cp_parser_template_id (parser, template_keyword_p,
20557 check_dependency_p,
20558 tag_type,
20559 is_declaration);
20560 if (decl == error_mark_node)
20561 return error_mark_node;
20564 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
20566 /* If this is a typename, create a TYPENAME_TYPE. */
20567 if (typename_p && decl != error_mark_node)
20569 decl = make_typename_type (scope, decl, typename_type,
20570 /*complain=*/tf_error);
20571 if (decl != error_mark_node)
20572 decl = TYPE_NAME (decl);
20575 decl = strip_using_decl (decl);
20577 /* Check to see that it is really the name of a class. */
20578 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
20579 && identifier_p (TREE_OPERAND (decl, 0))
20580 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
20581 /* Situations like this:
20583 template <typename T> struct A {
20584 typename T::template X<int>::I i;
20587 are problematic. Is `T::template X<int>' a class-name? The
20588 standard does not seem to be definitive, but there is no other
20589 valid interpretation of the following `::'. Therefore, those
20590 names are considered class-names. */
20592 decl = make_typename_type (scope, decl, tag_type, tf_error);
20593 if (decl != error_mark_node)
20594 decl = TYPE_NAME (decl);
20596 else if (TREE_CODE (decl) != TYPE_DECL
20597 || TREE_TYPE (decl) == error_mark_node
20598 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
20599 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
20600 /* In Objective-C 2.0, a classname followed by '.' starts a
20601 dot-syntax expression, and it's not a type-name. */
20602 || (c_dialect_objc ()
20603 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
20604 && objc_is_class_name (decl)))
20605 decl = error_mark_node;
20607 if (decl == error_mark_node)
20608 cp_parser_error (parser, "expected class-name");
20609 else if (identifier && !parser->scope)
20610 maybe_note_name_used_in_class (identifier, decl);
20612 return decl;
20615 /* Parse a class-specifier.
20617 class-specifier:
20618 class-head { member-specification [opt] }
20620 Returns the TREE_TYPE representing the class. */
20622 static tree
20623 cp_parser_class_specifier_1 (cp_parser* parser)
20625 tree type;
20626 tree attributes = NULL_TREE;
20627 bool nested_name_specifier_p;
20628 unsigned saved_num_template_parameter_lists;
20629 bool saved_in_function_body;
20630 unsigned char in_statement;
20631 bool in_switch_statement_p;
20632 bool saved_in_unbraced_linkage_specification_p;
20633 tree old_scope = NULL_TREE;
20634 tree scope = NULL_TREE;
20635 cp_token *closing_brace;
20637 push_deferring_access_checks (dk_no_deferred);
20639 /* Parse the class-head. */
20640 type = cp_parser_class_head (parser,
20641 &nested_name_specifier_p);
20642 /* If the class-head was a semantic disaster, skip the entire body
20643 of the class. */
20644 if (!type)
20646 cp_parser_skip_to_end_of_block_or_statement (parser);
20647 pop_deferring_access_checks ();
20648 return error_mark_node;
20651 /* Look for the `{'. */
20652 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
20654 pop_deferring_access_checks ();
20655 return error_mark_node;
20658 cp_ensure_no_omp_declare_simd (parser);
20660 /* Issue an error message if type-definitions are forbidden here. */
20661 cp_parser_check_type_definition (parser);
20662 /* Remember that we are defining one more class. */
20663 ++parser->num_classes_being_defined;
20664 /* Inside the class, surrounding template-parameter-lists do not
20665 apply. */
20666 saved_num_template_parameter_lists
20667 = parser->num_template_parameter_lists;
20668 parser->num_template_parameter_lists = 0;
20669 /* We are not in a function body. */
20670 saved_in_function_body = parser->in_function_body;
20671 parser->in_function_body = false;
20672 /* Or in a loop. */
20673 in_statement = parser->in_statement;
20674 parser->in_statement = 0;
20675 /* Or in a switch. */
20676 in_switch_statement_p = parser->in_switch_statement_p;
20677 parser->in_switch_statement_p = false;
20678 /* We are not immediately inside an extern "lang" block. */
20679 saved_in_unbraced_linkage_specification_p
20680 = parser->in_unbraced_linkage_specification_p;
20681 parser->in_unbraced_linkage_specification_p = false;
20683 // Associate constraints with the type.
20684 if (flag_concepts)
20685 type = associate_classtype_constraints (type);
20687 /* Start the class. */
20688 if (nested_name_specifier_p)
20690 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
20691 old_scope = push_inner_scope (scope);
20693 type = begin_class_definition (type);
20695 if (type == error_mark_node)
20696 /* If the type is erroneous, skip the entire body of the class. */
20697 cp_parser_skip_to_closing_brace (parser);
20698 else
20699 /* Parse the member-specification. */
20700 cp_parser_member_specification_opt (parser);
20702 /* Look for the trailing `}'. */
20703 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20704 /* Look for trailing attributes to apply to this class. */
20705 if (cp_parser_allow_gnu_extensions_p (parser))
20706 attributes = cp_parser_gnu_attributes_opt (parser);
20707 if (type != error_mark_node)
20708 type = finish_struct (type, attributes);
20709 if (nested_name_specifier_p)
20710 pop_inner_scope (old_scope, scope);
20712 /* We've finished a type definition. Check for the common syntax
20713 error of forgetting a semicolon after the definition. We need to
20714 be careful, as we can't just check for not-a-semicolon and be done
20715 with it; the user might have typed:
20717 class X { } c = ...;
20718 class X { } *p = ...;
20720 and so forth. Instead, enumerate all the possible tokens that
20721 might follow this production; if we don't see one of them, then
20722 complain and silently insert the semicolon. */
20724 cp_token *token = cp_lexer_peek_token (parser->lexer);
20725 bool want_semicolon = true;
20727 if (cp_next_tokens_can_be_std_attribute_p (parser))
20728 /* Don't try to parse c++11 attributes here. As per the
20729 grammar, that should be a task for
20730 cp_parser_decl_specifier_seq. */
20731 want_semicolon = false;
20733 switch (token->type)
20735 case CPP_NAME:
20736 case CPP_SEMICOLON:
20737 case CPP_MULT:
20738 case CPP_AND:
20739 case CPP_OPEN_PAREN:
20740 case CPP_CLOSE_PAREN:
20741 case CPP_COMMA:
20742 want_semicolon = false;
20743 break;
20745 /* While it's legal for type qualifiers and storage class
20746 specifiers to follow type definitions in the grammar, only
20747 compiler testsuites contain code like that. Assume that if
20748 we see such code, then what we're really seeing is a case
20749 like:
20751 class X { }
20752 const <type> var = ...;
20756 class Y { }
20757 static <type> func (...) ...
20759 i.e. the qualifier or specifier applies to the next
20760 declaration. To do so, however, we need to look ahead one
20761 more token to see if *that* token is a type specifier.
20763 This code could be improved to handle:
20765 class Z { }
20766 static const <type> var = ...; */
20767 case CPP_KEYWORD:
20768 if (keyword_is_decl_specifier (token->keyword))
20770 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
20772 /* Handling user-defined types here would be nice, but very
20773 tricky. */
20774 want_semicolon
20775 = (lookahead->type == CPP_KEYWORD
20776 && keyword_begins_type_specifier (lookahead->keyword));
20778 break;
20779 default:
20780 break;
20783 /* If we don't have a type, then something is very wrong and we
20784 shouldn't try to do anything clever. Likewise for not seeing the
20785 closing brace. */
20786 if (closing_brace && TYPE_P (type) && want_semicolon)
20788 cp_token_position prev
20789 = cp_lexer_previous_token_position (parser->lexer);
20790 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
20791 location_t loc = prev_token->location;
20793 if (CLASSTYPE_DECLARED_CLASS (type))
20794 error_at (loc, "expected %<;%> after class definition");
20795 else if (TREE_CODE (type) == RECORD_TYPE)
20796 error_at (loc, "expected %<;%> after struct definition");
20797 else if (TREE_CODE (type) == UNION_TYPE)
20798 error_at (loc, "expected %<;%> after union definition");
20799 else
20800 gcc_unreachable ();
20802 /* Unget one token and smash it to look as though we encountered
20803 a semicolon in the input stream. */
20804 cp_lexer_set_token_position (parser->lexer, prev);
20805 token = cp_lexer_peek_token (parser->lexer);
20806 token->type = CPP_SEMICOLON;
20807 token->keyword = RID_MAX;
20811 /* If this class is not itself within the scope of another class,
20812 then we need to parse the bodies of all of the queued function
20813 definitions. Note that the queued functions defined in a class
20814 are not always processed immediately following the
20815 class-specifier for that class. Consider:
20817 struct A {
20818 struct B { void f() { sizeof (A); } };
20821 If `f' were processed before the processing of `A' were
20822 completed, there would be no way to compute the size of `A'.
20823 Note that the nesting we are interested in here is lexical --
20824 not the semantic nesting given by TYPE_CONTEXT. In particular,
20825 for:
20827 struct A { struct B; };
20828 struct A::B { void f() { } };
20830 there is no need to delay the parsing of `A::B::f'. */
20831 if (--parser->num_classes_being_defined == 0)
20833 tree decl;
20834 tree class_type = NULL_TREE;
20835 tree pushed_scope = NULL_TREE;
20836 unsigned ix;
20837 cp_default_arg_entry *e;
20838 tree save_ccp, save_ccr;
20840 /* In a first pass, parse default arguments to the functions.
20841 Then, in a second pass, parse the bodies of the functions.
20842 This two-phased approach handles cases like:
20844 struct S {
20845 void f() { g(); }
20846 void g(int i = 3);
20850 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
20852 decl = e->decl;
20853 /* If there are default arguments that have not yet been processed,
20854 take care of them now. */
20855 if (class_type != e->class_type)
20857 if (pushed_scope)
20858 pop_scope (pushed_scope);
20859 class_type = e->class_type;
20860 pushed_scope = push_scope (class_type);
20862 /* Make sure that any template parameters are in scope. */
20863 maybe_begin_member_template_processing (decl);
20864 /* Parse the default argument expressions. */
20865 cp_parser_late_parsing_default_args (parser, decl);
20866 /* Remove any template parameters from the symbol table. */
20867 maybe_end_member_template_processing ();
20869 vec_safe_truncate (unparsed_funs_with_default_args, 0);
20870 /* Now parse any NSDMIs. */
20871 save_ccp = current_class_ptr;
20872 save_ccr = current_class_ref;
20873 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
20875 if (class_type != DECL_CONTEXT (decl))
20877 if (pushed_scope)
20878 pop_scope (pushed_scope);
20879 class_type = DECL_CONTEXT (decl);
20880 pushed_scope = push_scope (class_type);
20882 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
20883 cp_parser_late_parsing_nsdmi (parser, decl);
20885 vec_safe_truncate (unparsed_nsdmis, 0);
20886 current_class_ptr = save_ccp;
20887 current_class_ref = save_ccr;
20888 if (pushed_scope)
20889 pop_scope (pushed_scope);
20891 /* Now do some post-NSDMI bookkeeping. */
20892 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
20893 after_nsdmi_defaulted_late_checks (class_type);
20894 vec_safe_truncate (unparsed_classes, 0);
20895 after_nsdmi_defaulted_late_checks (type);
20897 /* Now parse the body of the functions. */
20898 if (flag_openmp)
20900 /* OpenMP UDRs need to be parsed before all other functions. */
20901 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20902 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
20903 cp_parser_late_parsing_for_member (parser, decl);
20904 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20905 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
20906 cp_parser_late_parsing_for_member (parser, decl);
20908 else
20909 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20910 cp_parser_late_parsing_for_member (parser, decl);
20911 vec_safe_truncate (unparsed_funs_with_definitions, 0);
20913 else
20914 vec_safe_push (unparsed_classes, type);
20916 /* Put back any saved access checks. */
20917 pop_deferring_access_checks ();
20919 /* Restore saved state. */
20920 parser->in_switch_statement_p = in_switch_statement_p;
20921 parser->in_statement = in_statement;
20922 parser->in_function_body = saved_in_function_body;
20923 parser->num_template_parameter_lists
20924 = saved_num_template_parameter_lists;
20925 parser->in_unbraced_linkage_specification_p
20926 = saved_in_unbraced_linkage_specification_p;
20928 return type;
20931 static tree
20932 cp_parser_class_specifier (cp_parser* parser)
20934 tree ret;
20935 timevar_push (TV_PARSE_STRUCT);
20936 ret = cp_parser_class_specifier_1 (parser);
20937 timevar_pop (TV_PARSE_STRUCT);
20938 return ret;
20941 /* Parse a class-head.
20943 class-head:
20944 class-key identifier [opt] base-clause [opt]
20945 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
20946 class-key nested-name-specifier [opt] template-id
20947 base-clause [opt]
20949 class-virt-specifier:
20950 final
20952 GNU Extensions:
20953 class-key attributes identifier [opt] base-clause [opt]
20954 class-key attributes nested-name-specifier identifier base-clause [opt]
20955 class-key attributes nested-name-specifier [opt] template-id
20956 base-clause [opt]
20958 Upon return BASES is initialized to the list of base classes (or
20959 NULL, if there are none) in the same form returned by
20960 cp_parser_base_clause.
20962 Returns the TYPE of the indicated class. Sets
20963 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
20964 involving a nested-name-specifier was used, and FALSE otherwise.
20966 Returns error_mark_node if this is not a class-head.
20968 Returns NULL_TREE if the class-head is syntactically valid, but
20969 semantically invalid in a way that means we should skip the entire
20970 body of the class. */
20972 static tree
20973 cp_parser_class_head (cp_parser* parser,
20974 bool* nested_name_specifier_p)
20976 tree nested_name_specifier;
20977 enum tag_types class_key;
20978 tree id = NULL_TREE;
20979 tree type = NULL_TREE;
20980 tree attributes;
20981 tree bases;
20982 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20983 bool template_id_p = false;
20984 bool qualified_p = false;
20985 bool invalid_nested_name_p = false;
20986 bool invalid_explicit_specialization_p = false;
20987 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20988 tree pushed_scope = NULL_TREE;
20989 unsigned num_templates;
20990 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
20991 /* Assume no nested-name-specifier will be present. */
20992 *nested_name_specifier_p = false;
20993 /* Assume no template parameter lists will be used in defining the
20994 type. */
20995 num_templates = 0;
20996 parser->colon_corrects_to_scope_p = false;
20998 /* Look for the class-key. */
20999 class_key = cp_parser_class_key (parser);
21000 if (class_key == none_type)
21001 return error_mark_node;
21003 /* Parse the attributes. */
21004 attributes = cp_parser_attributes_opt (parser);
21006 /* If the next token is `::', that is invalid -- but sometimes
21007 people do try to write:
21009 struct ::S {};
21011 Handle this gracefully by accepting the extra qualifier, and then
21012 issuing an error about it later if this really is a
21013 class-head. If it turns out just to be an elaborated type
21014 specifier, remain silent. */
21015 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
21016 qualified_p = true;
21018 push_deferring_access_checks (dk_no_check);
21020 /* Determine the name of the class. Begin by looking for an
21021 optional nested-name-specifier. */
21022 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
21023 nested_name_specifier
21024 = cp_parser_nested_name_specifier_opt (parser,
21025 /*typename_keyword_p=*/false,
21026 /*check_dependency_p=*/false,
21027 /*type_p=*/true,
21028 /*is_declaration=*/false);
21029 /* If there was a nested-name-specifier, then there *must* be an
21030 identifier. */
21031 if (nested_name_specifier)
21033 type_start_token = cp_lexer_peek_token (parser->lexer);
21034 /* Although the grammar says `identifier', it really means
21035 `class-name' or `template-name'. You are only allowed to
21036 define a class that has already been declared with this
21037 syntax.
21039 The proposed resolution for Core Issue 180 says that wherever
21040 you see `class T::X' you should treat `X' as a type-name.
21042 It is OK to define an inaccessible class; for example:
21044 class A { class B; };
21045 class A::B {};
21047 We do not know if we will see a class-name, or a
21048 template-name. We look for a class-name first, in case the
21049 class-name is a template-id; if we looked for the
21050 template-name first we would stop after the template-name. */
21051 cp_parser_parse_tentatively (parser);
21052 type = cp_parser_class_name (parser,
21053 /*typename_keyword_p=*/false,
21054 /*template_keyword_p=*/false,
21055 class_type,
21056 /*check_dependency_p=*/false,
21057 /*class_head_p=*/true,
21058 /*is_declaration=*/false);
21059 /* If that didn't work, ignore the nested-name-specifier. */
21060 if (!cp_parser_parse_definitely (parser))
21062 invalid_nested_name_p = true;
21063 type_start_token = cp_lexer_peek_token (parser->lexer);
21064 id = cp_parser_identifier (parser);
21065 if (id == error_mark_node)
21066 id = NULL_TREE;
21068 /* If we could not find a corresponding TYPE, treat this
21069 declaration like an unqualified declaration. */
21070 if (type == error_mark_node)
21071 nested_name_specifier = NULL_TREE;
21072 /* Otherwise, count the number of templates used in TYPE and its
21073 containing scopes. */
21074 else
21076 tree scope;
21078 for (scope = TREE_TYPE (type);
21079 scope && TREE_CODE (scope) != NAMESPACE_DECL;
21080 scope = get_containing_scope (scope))
21081 if (TYPE_P (scope)
21082 && CLASS_TYPE_P (scope)
21083 && CLASSTYPE_TEMPLATE_INFO (scope)
21084 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
21085 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
21086 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
21087 ++num_templates;
21090 /* Otherwise, the identifier is optional. */
21091 else
21093 /* We don't know whether what comes next is a template-id,
21094 an identifier, or nothing at all. */
21095 cp_parser_parse_tentatively (parser);
21096 /* Check for a template-id. */
21097 type_start_token = cp_lexer_peek_token (parser->lexer);
21098 id = cp_parser_template_id (parser,
21099 /*template_keyword_p=*/false,
21100 /*check_dependency_p=*/true,
21101 class_key,
21102 /*is_declaration=*/true);
21103 /* If that didn't work, it could still be an identifier. */
21104 if (!cp_parser_parse_definitely (parser))
21106 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21108 type_start_token = cp_lexer_peek_token (parser->lexer);
21109 id = cp_parser_identifier (parser);
21111 else
21112 id = NULL_TREE;
21114 else
21116 template_id_p = true;
21117 ++num_templates;
21121 pop_deferring_access_checks ();
21123 if (id)
21125 cp_parser_check_for_invalid_template_id (parser, id,
21126 class_key,
21127 type_start_token->location);
21129 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
21131 /* If it's not a `:' or a `{' then we can't really be looking at a
21132 class-head, since a class-head only appears as part of a
21133 class-specifier. We have to detect this situation before calling
21134 xref_tag, since that has irreversible side-effects. */
21135 if (!cp_parser_next_token_starts_class_definition_p (parser))
21137 cp_parser_error (parser, "expected %<{%> or %<:%>");
21138 type = error_mark_node;
21139 goto out;
21142 /* At this point, we're going ahead with the class-specifier, even
21143 if some other problem occurs. */
21144 cp_parser_commit_to_tentative_parse (parser);
21145 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
21147 cp_parser_error (parser,
21148 "cannot specify %<override%> for a class");
21149 type = error_mark_node;
21150 goto out;
21152 /* Issue the error about the overly-qualified name now. */
21153 if (qualified_p)
21155 cp_parser_error (parser,
21156 "global qualification of class name is invalid");
21157 type = error_mark_node;
21158 goto out;
21160 else if (invalid_nested_name_p)
21162 cp_parser_error (parser,
21163 "qualified name does not name a class");
21164 type = error_mark_node;
21165 goto out;
21167 else if (nested_name_specifier)
21169 tree scope;
21171 /* Reject typedef-names in class heads. */
21172 if (!DECL_IMPLICIT_TYPEDEF_P (type))
21174 error_at (type_start_token->location,
21175 "invalid class name in declaration of %qD",
21176 type);
21177 type = NULL_TREE;
21178 goto done;
21181 /* Figure out in what scope the declaration is being placed. */
21182 scope = current_scope ();
21183 /* If that scope does not contain the scope in which the
21184 class was originally declared, the program is invalid. */
21185 if (scope && !is_ancestor (scope, nested_name_specifier))
21187 if (at_namespace_scope_p ())
21188 error_at (type_start_token->location,
21189 "declaration of %qD in namespace %qD which does not "
21190 "enclose %qD",
21191 type, scope, nested_name_specifier);
21192 else
21193 error_at (type_start_token->location,
21194 "declaration of %qD in %qD which does not enclose %qD",
21195 type, scope, nested_name_specifier);
21196 type = NULL_TREE;
21197 goto done;
21199 /* [dcl.meaning]
21201 A declarator-id shall not be qualified except for the
21202 definition of a ... nested class outside of its class
21203 ... [or] the definition or explicit instantiation of a
21204 class member of a namespace outside of its namespace. */
21205 if (scope == nested_name_specifier)
21207 permerror (nested_name_specifier_token_start->location,
21208 "extra qualification not allowed");
21209 nested_name_specifier = NULL_TREE;
21210 num_templates = 0;
21213 /* An explicit-specialization must be preceded by "template <>". If
21214 it is not, try to recover gracefully. */
21215 if (at_namespace_scope_p ()
21216 && parser->num_template_parameter_lists == 0
21217 && template_id_p)
21219 error_at (type_start_token->location,
21220 "an explicit specialization must be preceded by %<template <>%>");
21221 invalid_explicit_specialization_p = true;
21222 /* Take the same action that would have been taken by
21223 cp_parser_explicit_specialization. */
21224 ++parser->num_template_parameter_lists;
21225 begin_specialization ();
21227 /* There must be no "return" statements between this point and the
21228 end of this function; set "type "to the correct return value and
21229 use "goto done;" to return. */
21230 /* Make sure that the right number of template parameters were
21231 present. */
21232 if (!cp_parser_check_template_parameters (parser, num_templates,
21233 type_start_token->location,
21234 /*declarator=*/NULL))
21236 /* If something went wrong, there is no point in even trying to
21237 process the class-definition. */
21238 type = NULL_TREE;
21239 goto done;
21242 /* Look up the type. */
21243 if (template_id_p)
21245 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
21246 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
21247 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
21249 error_at (type_start_token->location,
21250 "function template %qD redeclared as a class template", id);
21251 type = error_mark_node;
21253 else
21255 type = TREE_TYPE (id);
21256 type = maybe_process_partial_specialization (type);
21258 if (nested_name_specifier)
21259 pushed_scope = push_scope (nested_name_specifier);
21261 else if (nested_name_specifier)
21263 tree class_type;
21265 /* Given:
21267 template <typename T> struct S { struct T };
21268 template <typename T> struct S<T>::T { };
21270 we will get a TYPENAME_TYPE when processing the definition of
21271 `S::T'. We need to resolve it to the actual type before we
21272 try to define it. */
21273 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
21275 class_type = resolve_typename_type (TREE_TYPE (type),
21276 /*only_current_p=*/false);
21277 if (TREE_CODE (class_type) != TYPENAME_TYPE)
21278 type = TYPE_NAME (class_type);
21279 else
21281 cp_parser_error (parser, "could not resolve typename type");
21282 type = error_mark_node;
21286 if (maybe_process_partial_specialization (TREE_TYPE (type))
21287 == error_mark_node)
21289 type = NULL_TREE;
21290 goto done;
21293 class_type = current_class_type;
21294 /* Enter the scope indicated by the nested-name-specifier. */
21295 pushed_scope = push_scope (nested_name_specifier);
21296 /* Get the canonical version of this type. */
21297 type = TYPE_MAIN_DECL (TREE_TYPE (type));
21298 /* Call push_template_decl if it seems like we should be defining a
21299 template either from the template headers or the type we're
21300 defining, so that we diagnose both extra and missing headers. */
21301 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
21302 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
21303 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
21305 type = push_template_decl (type);
21306 if (type == error_mark_node)
21308 type = NULL_TREE;
21309 goto done;
21313 type = TREE_TYPE (type);
21314 *nested_name_specifier_p = true;
21316 else /* The name is not a nested name. */
21318 /* If the class was unnamed, create a dummy name. */
21319 if (!id)
21320 id = make_anon_name ();
21321 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
21322 parser->num_template_parameter_lists);
21325 /* Indicate whether this class was declared as a `class' or as a
21326 `struct'. */
21327 if (TREE_CODE (type) == RECORD_TYPE)
21328 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
21329 cp_parser_check_class_key (class_key, type);
21331 /* If this type was already complete, and we see another definition,
21332 that's an error. */
21333 if (type != error_mark_node && COMPLETE_TYPE_P (type))
21335 error_at (type_start_token->location, "redefinition of %q#T",
21336 type);
21337 error_at (type_start_token->location, "previous definition of %q+#T",
21338 type);
21339 type = NULL_TREE;
21340 goto done;
21342 else if (type == error_mark_node)
21343 type = NULL_TREE;
21345 if (type)
21347 /* Apply attributes now, before any use of the class as a template
21348 argument in its base list. */
21349 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
21350 fixup_attribute_variants (type);
21353 /* We will have entered the scope containing the class; the names of
21354 base classes should be looked up in that context. For example:
21356 struct A { struct B {}; struct C; };
21357 struct A::C : B {};
21359 is valid. */
21361 /* Get the list of base-classes, if there is one. */
21362 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21364 /* PR59482: enter the class scope so that base-specifiers are looked
21365 up correctly. */
21366 if (type)
21367 pushclass (type);
21368 bases = cp_parser_base_clause (parser);
21369 /* PR59482: get out of the previously pushed class scope so that the
21370 subsequent pops pop the right thing. */
21371 if (type)
21372 popclass ();
21374 else
21375 bases = NULL_TREE;
21377 /* If we're really defining a class, process the base classes.
21378 If they're invalid, fail. */
21379 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
21380 && !xref_basetypes (type, bases))
21381 type = NULL_TREE;
21383 done:
21384 /* Leave the scope given by the nested-name-specifier. We will
21385 enter the class scope itself while processing the members. */
21386 if (pushed_scope)
21387 pop_scope (pushed_scope);
21389 if (invalid_explicit_specialization_p)
21391 end_specialization ();
21392 --parser->num_template_parameter_lists;
21395 if (type)
21396 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
21397 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
21398 CLASSTYPE_FINAL (type) = 1;
21399 out:
21400 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
21401 return type;
21404 /* Parse a class-key.
21406 class-key:
21407 class
21408 struct
21409 union
21411 Returns the kind of class-key specified, or none_type to indicate
21412 error. */
21414 static enum tag_types
21415 cp_parser_class_key (cp_parser* parser)
21417 cp_token *token;
21418 enum tag_types tag_type;
21420 /* Look for the class-key. */
21421 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
21422 if (!token)
21423 return none_type;
21425 /* Check to see if the TOKEN is a class-key. */
21426 tag_type = cp_parser_token_is_class_key (token);
21427 if (!tag_type)
21428 cp_parser_error (parser, "expected class-key");
21429 return tag_type;
21432 /* Parse a type-parameter-key.
21434 type-parameter-key:
21435 class
21436 typename
21439 static void
21440 cp_parser_type_parameter_key (cp_parser* parser)
21442 /* Look for the type-parameter-key. */
21443 enum tag_types tag_type = none_type;
21444 cp_token *token = cp_lexer_peek_token (parser->lexer);
21445 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
21447 cp_lexer_consume_token (parser->lexer);
21448 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
21449 /* typename is not allowed in a template template parameter
21450 by the standard until C++1Z. */
21451 pedwarn (token->location, OPT_Wpedantic,
21452 "ISO C++ forbids typename key in template template parameter;"
21453 " use -std=c++1z or -std=gnu++1z");
21455 else
21456 cp_parser_error (parser, "expected %<class%> or %<typename%>");
21458 return;
21461 /* Parse an (optional) member-specification.
21463 member-specification:
21464 member-declaration member-specification [opt]
21465 access-specifier : member-specification [opt] */
21467 static void
21468 cp_parser_member_specification_opt (cp_parser* parser)
21470 while (true)
21472 cp_token *token;
21473 enum rid keyword;
21475 /* Peek at the next token. */
21476 token = cp_lexer_peek_token (parser->lexer);
21477 /* If it's a `}', or EOF then we've seen all the members. */
21478 if (token->type == CPP_CLOSE_BRACE
21479 || token->type == CPP_EOF
21480 || token->type == CPP_PRAGMA_EOL)
21481 break;
21483 /* See if this token is a keyword. */
21484 keyword = token->keyword;
21485 switch (keyword)
21487 case RID_PUBLIC:
21488 case RID_PROTECTED:
21489 case RID_PRIVATE:
21490 /* Consume the access-specifier. */
21491 cp_lexer_consume_token (parser->lexer);
21492 /* Remember which access-specifier is active. */
21493 current_access_specifier = token->u.value;
21494 /* Look for the `:'. */
21495 cp_parser_require (parser, CPP_COLON, RT_COLON);
21496 break;
21498 default:
21499 /* Accept #pragmas at class scope. */
21500 if (token->type == CPP_PRAGMA)
21502 cp_parser_pragma (parser, pragma_member);
21503 break;
21506 /* Otherwise, the next construction must be a
21507 member-declaration. */
21508 cp_parser_member_declaration (parser);
21513 /* Parse a member-declaration.
21515 member-declaration:
21516 decl-specifier-seq [opt] member-declarator-list [opt] ;
21517 function-definition ; [opt]
21518 :: [opt] nested-name-specifier template [opt] unqualified-id ;
21519 using-declaration
21520 template-declaration
21521 alias-declaration
21523 member-declarator-list:
21524 member-declarator
21525 member-declarator-list , member-declarator
21527 member-declarator:
21528 declarator pure-specifier [opt]
21529 declarator constant-initializer [opt]
21530 identifier [opt] : constant-expression
21532 GNU Extensions:
21534 member-declaration:
21535 __extension__ member-declaration
21537 member-declarator:
21538 declarator attributes [opt] pure-specifier [opt]
21539 declarator attributes [opt] constant-initializer [opt]
21540 identifier [opt] attributes [opt] : constant-expression
21542 C++0x Extensions:
21544 member-declaration:
21545 static_assert-declaration */
21547 static void
21548 cp_parser_member_declaration (cp_parser* parser)
21550 cp_decl_specifier_seq decl_specifiers;
21551 tree prefix_attributes;
21552 tree decl;
21553 int declares_class_or_enum;
21554 bool friend_p;
21555 cp_token *token = NULL;
21556 cp_token *decl_spec_token_start = NULL;
21557 cp_token *initializer_token_start = NULL;
21558 int saved_pedantic;
21559 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
21561 /* Check for the `__extension__' keyword. */
21562 if (cp_parser_extension_opt (parser, &saved_pedantic))
21564 /* Recurse. */
21565 cp_parser_member_declaration (parser);
21566 /* Restore the old value of the PEDANTIC flag. */
21567 pedantic = saved_pedantic;
21569 return;
21572 /* Check for a template-declaration. */
21573 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
21575 /* An explicit specialization here is an error condition, and we
21576 expect the specialization handler to detect and report this. */
21577 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
21578 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
21579 cp_parser_explicit_specialization (parser);
21580 else
21581 cp_parser_template_declaration (parser, /*member_p=*/true);
21583 return;
21585 /* Check for a template introduction. */
21586 else if (cp_parser_template_declaration_after_export (parser, true))
21587 return;
21589 /* Check for a using-declaration. */
21590 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
21592 if (cxx_dialect < cxx11)
21594 /* Parse the using-declaration. */
21595 cp_parser_using_declaration (parser,
21596 /*access_declaration_p=*/false);
21597 return;
21599 else
21601 tree decl;
21602 bool alias_decl_expected;
21603 cp_parser_parse_tentatively (parser);
21604 decl = cp_parser_alias_declaration (parser);
21605 /* Note that if we actually see the '=' token after the
21606 identifier, cp_parser_alias_declaration commits the
21607 tentative parse. In that case, we really expect an
21608 alias-declaration. Otherwise, we expect a using
21609 declaration. */
21610 alias_decl_expected =
21611 !cp_parser_uncommitted_to_tentative_parse_p (parser);
21612 cp_parser_parse_definitely (parser);
21614 if (alias_decl_expected)
21615 finish_member_declaration (decl);
21616 else
21617 cp_parser_using_declaration (parser,
21618 /*access_declaration_p=*/false);
21619 return;
21623 /* Check for @defs. */
21624 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
21626 tree ivar, member;
21627 tree ivar_chains = cp_parser_objc_defs_expression (parser);
21628 ivar = ivar_chains;
21629 while (ivar)
21631 member = ivar;
21632 ivar = TREE_CHAIN (member);
21633 TREE_CHAIN (member) = NULL_TREE;
21634 finish_member_declaration (member);
21636 return;
21639 /* If the next token is `static_assert' we have a static assertion. */
21640 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
21642 cp_parser_static_assert (parser, /*member_p=*/true);
21643 return;
21646 parser->colon_corrects_to_scope_p = false;
21648 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
21649 goto out;
21651 /* Parse the decl-specifier-seq. */
21652 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21653 cp_parser_decl_specifier_seq (parser,
21654 CP_PARSER_FLAGS_OPTIONAL,
21655 &decl_specifiers,
21656 &declares_class_or_enum);
21657 /* Check for an invalid type-name. */
21658 if (!decl_specifiers.any_type_specifiers_p
21659 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21660 goto out;
21661 /* If there is no declarator, then the decl-specifier-seq should
21662 specify a type. */
21663 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21665 /* If there was no decl-specifier-seq, and the next token is a
21666 `;', then we have something like:
21668 struct S { ; };
21670 [class.mem]
21672 Each member-declaration shall declare at least one member
21673 name of the class. */
21674 if (!decl_specifiers.any_specifiers_p)
21676 cp_token *token = cp_lexer_peek_token (parser->lexer);
21677 if (!in_system_header_at (token->location))
21678 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
21680 else
21682 tree type;
21684 /* See if this declaration is a friend. */
21685 friend_p = cp_parser_friend_p (&decl_specifiers);
21686 /* If there were decl-specifiers, check to see if there was
21687 a class-declaration. */
21688 type = check_tag_decl (&decl_specifiers,
21689 /*explicit_type_instantiation_p=*/false);
21690 /* Nested classes have already been added to the class, but
21691 a `friend' needs to be explicitly registered. */
21692 if (friend_p)
21694 /* If the `friend' keyword was present, the friend must
21695 be introduced with a class-key. */
21696 if (!declares_class_or_enum && cxx_dialect < cxx11)
21697 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
21698 "in C++03 a class-key must be used "
21699 "when declaring a friend");
21700 /* In this case:
21702 template <typename T> struct A {
21703 friend struct A<T>::B;
21706 A<T>::B will be represented by a TYPENAME_TYPE, and
21707 therefore not recognized by check_tag_decl. */
21708 if (!type)
21710 type = decl_specifiers.type;
21711 if (type && TREE_CODE (type) == TYPE_DECL)
21712 type = TREE_TYPE (type);
21714 if (!type || !TYPE_P (type))
21715 error_at (decl_spec_token_start->location,
21716 "friend declaration does not name a class or "
21717 "function");
21718 else
21719 make_friend_class (current_class_type, type,
21720 /*complain=*/true);
21722 /* If there is no TYPE, an error message will already have
21723 been issued. */
21724 else if (!type || type == error_mark_node)
21726 /* An anonymous aggregate has to be handled specially; such
21727 a declaration really declares a data member (with a
21728 particular type), as opposed to a nested class. */
21729 else if (ANON_AGGR_TYPE_P (type))
21731 /* C++11 9.5/6. */
21732 if (decl_specifiers.storage_class != sc_none)
21733 error_at (decl_spec_token_start->location,
21734 "a storage class on an anonymous aggregate "
21735 "in class scope is not allowed");
21737 /* Remove constructors and such from TYPE, now that we
21738 know it is an anonymous aggregate. */
21739 fixup_anonymous_aggr (type);
21740 /* And make the corresponding data member. */
21741 decl = build_decl (decl_spec_token_start->location,
21742 FIELD_DECL, NULL_TREE, type);
21743 /* Add it to the class. */
21744 finish_member_declaration (decl);
21746 else
21747 cp_parser_check_access_in_redeclaration
21748 (TYPE_NAME (type),
21749 decl_spec_token_start->location);
21752 else
21754 bool assume_semicolon = false;
21756 /* Clear attributes from the decl_specifiers but keep them
21757 around as prefix attributes that apply them to the entity
21758 being declared. */
21759 prefix_attributes = decl_specifiers.attributes;
21760 decl_specifiers.attributes = NULL_TREE;
21762 /* See if these declarations will be friends. */
21763 friend_p = cp_parser_friend_p (&decl_specifiers);
21765 /* Keep going until we hit the `;' at the end of the
21766 declaration. */
21767 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21769 tree attributes = NULL_TREE;
21770 tree first_attribute;
21772 /* Peek at the next token. */
21773 token = cp_lexer_peek_token (parser->lexer);
21775 /* Check for a bitfield declaration. */
21776 if (token->type == CPP_COLON
21777 || (token->type == CPP_NAME
21778 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
21779 == CPP_COLON))
21781 tree identifier;
21782 tree width;
21784 /* Get the name of the bitfield. Note that we cannot just
21785 check TOKEN here because it may have been invalidated by
21786 the call to cp_lexer_peek_nth_token above. */
21787 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
21788 identifier = cp_parser_identifier (parser);
21789 else
21790 identifier = NULL_TREE;
21792 /* Consume the `:' token. */
21793 cp_lexer_consume_token (parser->lexer);
21794 /* Get the width of the bitfield. */
21795 width
21796 = cp_parser_constant_expression (parser);
21798 /* Look for attributes that apply to the bitfield. */
21799 attributes = cp_parser_attributes_opt (parser);
21800 /* Remember which attributes are prefix attributes and
21801 which are not. */
21802 first_attribute = attributes;
21803 /* Combine the attributes. */
21804 attributes = chainon (prefix_attributes, attributes);
21806 /* Create the bitfield declaration. */
21807 decl = grokbitfield (identifier
21808 ? make_id_declarator (NULL_TREE,
21809 identifier,
21810 sfk_none)
21811 : NULL,
21812 &decl_specifiers,
21813 width,
21814 attributes);
21816 else
21818 cp_declarator *declarator;
21819 tree initializer;
21820 tree asm_specification;
21821 int ctor_dtor_or_conv_p;
21823 /* Parse the declarator. */
21824 declarator
21825 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
21826 &ctor_dtor_or_conv_p,
21827 /*parenthesized_p=*/NULL,
21828 /*member_p=*/true,
21829 friend_p);
21831 /* If something went wrong parsing the declarator, make sure
21832 that we at least consume some tokens. */
21833 if (declarator == cp_error_declarator)
21835 /* Skip to the end of the statement. */
21836 cp_parser_skip_to_end_of_statement (parser);
21837 /* If the next token is not a semicolon, that is
21838 probably because we just skipped over the body of
21839 a function. So, we consume a semicolon if
21840 present, but do not issue an error message if it
21841 is not present. */
21842 if (cp_lexer_next_token_is (parser->lexer,
21843 CPP_SEMICOLON))
21844 cp_lexer_consume_token (parser->lexer);
21845 goto out;
21848 if (declares_class_or_enum & 2)
21849 cp_parser_check_for_definition_in_return_type
21850 (declarator, decl_specifiers.type,
21851 decl_specifiers.locations[ds_type_spec]);
21853 /* Look for an asm-specification. */
21854 asm_specification = cp_parser_asm_specification_opt (parser);
21855 /* Look for attributes that apply to the declaration. */
21856 attributes = cp_parser_attributes_opt (parser);
21857 /* Remember which attributes are prefix attributes and
21858 which are not. */
21859 first_attribute = attributes;
21860 /* Combine the attributes. */
21861 attributes = chainon (prefix_attributes, attributes);
21863 /* If it's an `=', then we have a constant-initializer or a
21864 pure-specifier. It is not correct to parse the
21865 initializer before registering the member declaration
21866 since the member declaration should be in scope while
21867 its initializer is processed. However, the rest of the
21868 front end does not yet provide an interface that allows
21869 us to handle this correctly. */
21870 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21872 /* In [class.mem]:
21874 A pure-specifier shall be used only in the declaration of
21875 a virtual function.
21877 A member-declarator can contain a constant-initializer
21878 only if it declares a static member of integral or
21879 enumeration type.
21881 Therefore, if the DECLARATOR is for a function, we look
21882 for a pure-specifier; otherwise, we look for a
21883 constant-initializer. When we call `grokfield', it will
21884 perform more stringent semantics checks. */
21885 initializer_token_start = cp_lexer_peek_token (parser->lexer);
21886 if (function_declarator_p (declarator)
21887 || (decl_specifiers.type
21888 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
21889 && declarator->kind == cdk_id
21890 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
21891 == FUNCTION_TYPE)))
21892 initializer = cp_parser_pure_specifier (parser);
21893 else if (decl_specifiers.storage_class != sc_static)
21894 initializer = cp_parser_save_nsdmi (parser);
21895 else if (cxx_dialect >= cxx11)
21897 bool nonconst;
21898 /* Don't require a constant rvalue in C++11, since we
21899 might want a reference constant. We'll enforce
21900 constancy later. */
21901 cp_lexer_consume_token (parser->lexer);
21902 /* Parse the initializer. */
21903 initializer = cp_parser_initializer_clause (parser,
21904 &nonconst);
21906 else
21907 /* Parse the initializer. */
21908 initializer = cp_parser_constant_initializer (parser);
21910 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
21911 && !function_declarator_p (declarator))
21913 bool x;
21914 if (decl_specifiers.storage_class != sc_static)
21915 initializer = cp_parser_save_nsdmi (parser);
21916 else
21917 initializer = cp_parser_initializer (parser, &x, &x);
21919 /* Otherwise, there is no initializer. */
21920 else
21921 initializer = NULL_TREE;
21923 /* See if we are probably looking at a function
21924 definition. We are certainly not looking at a
21925 member-declarator. Calling `grokfield' has
21926 side-effects, so we must not do it unless we are sure
21927 that we are looking at a member-declarator. */
21928 if (cp_parser_token_starts_function_definition_p
21929 (cp_lexer_peek_token (parser->lexer)))
21931 /* The grammar does not allow a pure-specifier to be
21932 used when a member function is defined. (It is
21933 possible that this fact is an oversight in the
21934 standard, since a pure function may be defined
21935 outside of the class-specifier. */
21936 if (initializer && initializer_token_start)
21937 error_at (initializer_token_start->location,
21938 "pure-specifier on function-definition");
21939 decl = cp_parser_save_member_function_body (parser,
21940 &decl_specifiers,
21941 declarator,
21942 attributes);
21943 if (parser->fully_implicit_function_template_p)
21944 decl = finish_fully_implicit_template (parser, decl);
21945 /* If the member was not a friend, declare it here. */
21946 if (!friend_p)
21947 finish_member_declaration (decl);
21948 /* Peek at the next token. */
21949 token = cp_lexer_peek_token (parser->lexer);
21950 /* If the next token is a semicolon, consume it. */
21951 if (token->type == CPP_SEMICOLON)
21952 cp_lexer_consume_token (parser->lexer);
21953 goto out;
21955 else
21956 if (declarator->kind == cdk_function)
21957 declarator->id_loc = token->location;
21958 /* Create the declaration. */
21959 decl = grokfield (declarator, &decl_specifiers,
21960 initializer, /*init_const_expr_p=*/true,
21961 asm_specification, attributes);
21962 if (parser->fully_implicit_function_template_p)
21964 if (friend_p)
21965 finish_fully_implicit_template (parser, 0);
21966 else
21967 decl = finish_fully_implicit_template (parser, decl);
21971 cp_finalize_omp_declare_simd (parser, decl);
21973 /* Reset PREFIX_ATTRIBUTES. */
21974 while (attributes && TREE_CHAIN (attributes) != first_attribute)
21975 attributes = TREE_CHAIN (attributes);
21976 if (attributes)
21977 TREE_CHAIN (attributes) = NULL_TREE;
21979 /* If there is any qualification still in effect, clear it
21980 now; we will be starting fresh with the next declarator. */
21981 parser->scope = NULL_TREE;
21982 parser->qualifying_scope = NULL_TREE;
21983 parser->object_scope = NULL_TREE;
21984 /* If it's a `,', then there are more declarators. */
21985 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21987 cp_lexer_consume_token (parser->lexer);
21988 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21990 cp_token *token = cp_lexer_previous_token (parser->lexer);
21991 error_at (token->location,
21992 "stray %<,%> at end of member declaration");
21995 /* If the next token isn't a `;', then we have a parse error. */
21996 else if (cp_lexer_next_token_is_not (parser->lexer,
21997 CPP_SEMICOLON))
21999 /* The next token might be a ways away from where the
22000 actual semicolon is missing. Find the previous token
22001 and use that for our error position. */
22002 cp_token *token = cp_lexer_previous_token (parser->lexer);
22003 error_at (token->location,
22004 "expected %<;%> at end of member declaration");
22006 /* Assume that the user meant to provide a semicolon. If
22007 we were to cp_parser_skip_to_end_of_statement, we might
22008 skip to a semicolon inside a member function definition
22009 and issue nonsensical error messages. */
22010 assume_semicolon = true;
22013 if (decl)
22015 /* Add DECL to the list of members. */
22016 if (!friend_p
22017 /* Explicitly include, eg, NSDMIs, for better error
22018 recovery (c++/58650). */
22019 || !DECL_DECLARES_FUNCTION_P (decl))
22020 finish_member_declaration (decl);
22022 if (TREE_CODE (decl) == FUNCTION_DECL)
22023 cp_parser_save_default_args (parser, decl);
22024 else if (TREE_CODE (decl) == FIELD_DECL
22025 && !DECL_C_BIT_FIELD (decl)
22026 && DECL_INITIAL (decl))
22027 /* Add DECL to the queue of NSDMI to be parsed later. */
22028 vec_safe_push (unparsed_nsdmis, decl);
22031 if (assume_semicolon)
22032 goto out;
22036 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22037 out:
22038 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22041 /* Parse a pure-specifier.
22043 pure-specifier:
22046 Returns INTEGER_ZERO_NODE if a pure specifier is found.
22047 Otherwise, ERROR_MARK_NODE is returned. */
22049 static tree
22050 cp_parser_pure_specifier (cp_parser* parser)
22052 cp_token *token;
22054 /* Look for the `=' token. */
22055 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
22056 return error_mark_node;
22057 /* Look for the `0' token. */
22058 token = cp_lexer_peek_token (parser->lexer);
22060 if (token->type == CPP_EOF
22061 || token->type == CPP_PRAGMA_EOL)
22062 return error_mark_node;
22064 cp_lexer_consume_token (parser->lexer);
22066 /* Accept = default or = delete in c++0x mode. */
22067 if (token->keyword == RID_DEFAULT
22068 || token->keyword == RID_DELETE)
22070 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
22071 return token->u.value;
22074 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
22075 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
22077 cp_parser_error (parser,
22078 "invalid pure specifier (only %<= 0%> is allowed)");
22079 cp_parser_skip_to_end_of_statement (parser);
22080 return error_mark_node;
22082 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
22084 error_at (token->location, "templates may not be %<virtual%>");
22085 return error_mark_node;
22088 return integer_zero_node;
22091 /* Parse a constant-initializer.
22093 constant-initializer:
22094 = constant-expression
22096 Returns a representation of the constant-expression. */
22098 static tree
22099 cp_parser_constant_initializer (cp_parser* parser)
22101 /* Look for the `=' token. */
22102 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
22103 return error_mark_node;
22105 /* It is invalid to write:
22107 struct S { static const int i = { 7 }; };
22110 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22112 cp_parser_error (parser,
22113 "a brace-enclosed initializer is not allowed here");
22114 /* Consume the opening brace. */
22115 cp_lexer_consume_token (parser->lexer);
22116 /* Skip the initializer. */
22117 cp_parser_skip_to_closing_brace (parser);
22118 /* Look for the trailing `}'. */
22119 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
22121 return error_mark_node;
22124 return cp_parser_constant_expression (parser);
22127 /* Derived classes [gram.class.derived] */
22129 /* Parse a base-clause.
22131 base-clause:
22132 : base-specifier-list
22134 base-specifier-list:
22135 base-specifier ... [opt]
22136 base-specifier-list , base-specifier ... [opt]
22138 Returns a TREE_LIST representing the base-classes, in the order in
22139 which they were declared. The representation of each node is as
22140 described by cp_parser_base_specifier.
22142 In the case that no bases are specified, this function will return
22143 NULL_TREE, not ERROR_MARK_NODE. */
22145 static tree
22146 cp_parser_base_clause (cp_parser* parser)
22148 tree bases = NULL_TREE;
22150 /* Look for the `:' that begins the list. */
22151 cp_parser_require (parser, CPP_COLON, RT_COLON);
22153 /* Scan the base-specifier-list. */
22154 while (true)
22156 cp_token *token;
22157 tree base;
22158 bool pack_expansion_p = false;
22160 /* Look for the base-specifier. */
22161 base = cp_parser_base_specifier (parser);
22162 /* Look for the (optional) ellipsis. */
22163 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22165 /* Consume the `...'. */
22166 cp_lexer_consume_token (parser->lexer);
22168 pack_expansion_p = true;
22171 /* Add BASE to the front of the list. */
22172 if (base && base != error_mark_node)
22174 if (pack_expansion_p)
22175 /* Make this a pack expansion type. */
22176 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
22178 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
22180 TREE_CHAIN (base) = bases;
22181 bases = base;
22184 /* Peek at the next token. */
22185 token = cp_lexer_peek_token (parser->lexer);
22186 /* If it's not a comma, then the list is complete. */
22187 if (token->type != CPP_COMMA)
22188 break;
22189 /* Consume the `,'. */
22190 cp_lexer_consume_token (parser->lexer);
22193 /* PARSER->SCOPE may still be non-NULL at this point, if the last
22194 base class had a qualified name. However, the next name that
22195 appears is certainly not qualified. */
22196 parser->scope = NULL_TREE;
22197 parser->qualifying_scope = NULL_TREE;
22198 parser->object_scope = NULL_TREE;
22200 return nreverse (bases);
22203 /* Parse a base-specifier.
22205 base-specifier:
22206 :: [opt] nested-name-specifier [opt] class-name
22207 virtual access-specifier [opt] :: [opt] nested-name-specifier
22208 [opt] class-name
22209 access-specifier virtual [opt] :: [opt] nested-name-specifier
22210 [opt] class-name
22212 Returns a TREE_LIST. The TREE_PURPOSE will be one of
22213 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
22214 indicate the specifiers provided. The TREE_VALUE will be a TYPE
22215 (or the ERROR_MARK_NODE) indicating the type that was specified. */
22217 static tree
22218 cp_parser_base_specifier (cp_parser* parser)
22220 cp_token *token;
22221 bool done = false;
22222 bool virtual_p = false;
22223 bool duplicate_virtual_error_issued_p = false;
22224 bool duplicate_access_error_issued_p = false;
22225 bool class_scope_p, template_p;
22226 tree access = access_default_node;
22227 tree type;
22229 /* Process the optional `virtual' and `access-specifier'. */
22230 while (!done)
22232 /* Peek at the next token. */
22233 token = cp_lexer_peek_token (parser->lexer);
22234 /* Process `virtual'. */
22235 switch (token->keyword)
22237 case RID_VIRTUAL:
22238 /* If `virtual' appears more than once, issue an error. */
22239 if (virtual_p && !duplicate_virtual_error_issued_p)
22241 cp_parser_error (parser,
22242 "%<virtual%> specified more than once in base-specified");
22243 duplicate_virtual_error_issued_p = true;
22246 virtual_p = true;
22248 /* Consume the `virtual' token. */
22249 cp_lexer_consume_token (parser->lexer);
22251 break;
22253 case RID_PUBLIC:
22254 case RID_PROTECTED:
22255 case RID_PRIVATE:
22256 /* If more than one access specifier appears, issue an
22257 error. */
22258 if (access != access_default_node
22259 && !duplicate_access_error_issued_p)
22261 cp_parser_error (parser,
22262 "more than one access specifier in base-specified");
22263 duplicate_access_error_issued_p = true;
22266 access = ridpointers[(int) token->keyword];
22268 /* Consume the access-specifier. */
22269 cp_lexer_consume_token (parser->lexer);
22271 break;
22273 default:
22274 done = true;
22275 break;
22278 /* It is not uncommon to see programs mechanically, erroneously, use
22279 the 'typename' keyword to denote (dependent) qualified types
22280 as base classes. */
22281 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
22283 token = cp_lexer_peek_token (parser->lexer);
22284 if (!processing_template_decl)
22285 error_at (token->location,
22286 "keyword %<typename%> not allowed outside of templates");
22287 else
22288 error_at (token->location,
22289 "keyword %<typename%> not allowed in this context "
22290 "(the base class is implicitly a type)");
22291 cp_lexer_consume_token (parser->lexer);
22294 /* Look for the optional `::' operator. */
22295 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
22296 /* Look for the nested-name-specifier. The simplest way to
22297 implement:
22299 [temp.res]
22301 The keyword `typename' is not permitted in a base-specifier or
22302 mem-initializer; in these contexts a qualified name that
22303 depends on a template-parameter is implicitly assumed to be a
22304 type name.
22306 is to pretend that we have seen the `typename' keyword at this
22307 point. */
22308 cp_parser_nested_name_specifier_opt (parser,
22309 /*typename_keyword_p=*/true,
22310 /*check_dependency_p=*/true,
22311 typename_type,
22312 /*is_declaration=*/true);
22313 /* If the base class is given by a qualified name, assume that names
22314 we see are type names or templates, as appropriate. */
22315 class_scope_p = (parser->scope && TYPE_P (parser->scope));
22316 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
22318 if (!parser->scope
22319 && cp_lexer_next_token_is_decltype (parser->lexer))
22320 /* DR 950 allows decltype as a base-specifier. */
22321 type = cp_parser_decltype (parser);
22322 else
22324 /* Otherwise, look for the class-name. */
22325 type = cp_parser_class_name (parser,
22326 class_scope_p,
22327 template_p,
22328 typename_type,
22329 /*check_dependency_p=*/true,
22330 /*class_head_p=*/false,
22331 /*is_declaration=*/true);
22332 type = TREE_TYPE (type);
22335 if (type == error_mark_node)
22336 return error_mark_node;
22338 return finish_base_specifier (type, access, virtual_p);
22341 /* Exception handling [gram.exception] */
22343 /* Parse an (optional) noexcept-specification.
22345 noexcept-specification:
22346 noexcept ( constant-expression ) [opt]
22348 If no noexcept-specification is present, returns NULL_TREE.
22349 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
22350 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
22351 there are no parentheses. CONSUMED_EXPR will be set accordingly.
22352 Otherwise, returns a noexcept specification unless RETURN_COND is true,
22353 in which case a boolean condition is returned instead. */
22355 static tree
22356 cp_parser_noexcept_specification_opt (cp_parser* parser,
22357 bool require_constexpr,
22358 bool* consumed_expr,
22359 bool return_cond)
22361 cp_token *token;
22362 const char *saved_message;
22364 /* Peek at the next token. */
22365 token = cp_lexer_peek_token (parser->lexer);
22367 /* Is it a noexcept-specification? */
22368 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
22370 tree expr;
22371 cp_lexer_consume_token (parser->lexer);
22373 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
22375 cp_lexer_consume_token (parser->lexer);
22377 if (require_constexpr)
22379 /* Types may not be defined in an exception-specification. */
22380 saved_message = parser->type_definition_forbidden_message;
22381 parser->type_definition_forbidden_message
22382 = G_("types may not be defined in an exception-specification");
22384 expr = cp_parser_constant_expression (parser);
22386 /* Restore the saved message. */
22387 parser->type_definition_forbidden_message = saved_message;
22389 else
22391 expr = cp_parser_expression (parser);
22392 *consumed_expr = true;
22395 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22397 else
22399 expr = boolean_true_node;
22400 if (!require_constexpr)
22401 *consumed_expr = false;
22404 /* We cannot build a noexcept-spec right away because this will check
22405 that expr is a constexpr. */
22406 if (!return_cond)
22407 return build_noexcept_spec (expr, tf_warning_or_error);
22408 else
22409 return expr;
22411 else
22412 return NULL_TREE;
22415 /* Parse an (optional) exception-specification.
22417 exception-specification:
22418 throw ( type-id-list [opt] )
22420 Returns a TREE_LIST representing the exception-specification. The
22421 TREE_VALUE of each node is a type. */
22423 static tree
22424 cp_parser_exception_specification_opt (cp_parser* parser)
22426 cp_token *token;
22427 tree type_id_list;
22428 const char *saved_message;
22430 /* Peek at the next token. */
22431 token = cp_lexer_peek_token (parser->lexer);
22433 /* Is it a noexcept-specification? */
22434 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
22435 false);
22436 if (type_id_list != NULL_TREE)
22437 return type_id_list;
22439 /* If it's not `throw', then there's no exception-specification. */
22440 if (!cp_parser_is_keyword (token, RID_THROW))
22441 return NULL_TREE;
22443 #if 0
22444 /* Enable this once a lot of code has transitioned to noexcept? */
22445 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
22446 warning (OPT_Wdeprecated, "dynamic exception specifications are "
22447 "deprecated in C++0x; use %<noexcept%> instead");
22448 #endif
22450 /* Consume the `throw'. */
22451 cp_lexer_consume_token (parser->lexer);
22453 /* Look for the `('. */
22454 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22456 /* Peek at the next token. */
22457 token = cp_lexer_peek_token (parser->lexer);
22458 /* If it's not a `)', then there is a type-id-list. */
22459 if (token->type != CPP_CLOSE_PAREN)
22461 /* Types may not be defined in an exception-specification. */
22462 saved_message = parser->type_definition_forbidden_message;
22463 parser->type_definition_forbidden_message
22464 = G_("types may not be defined in an exception-specification");
22465 /* Parse the type-id-list. */
22466 type_id_list = cp_parser_type_id_list (parser);
22467 /* Restore the saved message. */
22468 parser->type_definition_forbidden_message = saved_message;
22470 else
22471 type_id_list = empty_except_spec;
22473 /* Look for the `)'. */
22474 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22476 return type_id_list;
22479 /* Parse an (optional) type-id-list.
22481 type-id-list:
22482 type-id ... [opt]
22483 type-id-list , type-id ... [opt]
22485 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
22486 in the order that the types were presented. */
22488 static tree
22489 cp_parser_type_id_list (cp_parser* parser)
22491 tree types = NULL_TREE;
22493 while (true)
22495 cp_token *token;
22496 tree type;
22498 /* Get the next type-id. */
22499 type = cp_parser_type_id (parser);
22500 /* Parse the optional ellipsis. */
22501 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22503 /* Consume the `...'. */
22504 cp_lexer_consume_token (parser->lexer);
22506 /* Turn the type into a pack expansion expression. */
22507 type = make_pack_expansion (type);
22509 /* Add it to the list. */
22510 types = add_exception_specifier (types, type, /*complain=*/1);
22511 /* Peek at the next token. */
22512 token = cp_lexer_peek_token (parser->lexer);
22513 /* If it is not a `,', we are done. */
22514 if (token->type != CPP_COMMA)
22515 break;
22516 /* Consume the `,'. */
22517 cp_lexer_consume_token (parser->lexer);
22520 return nreverse (types);
22523 /* Parse a try-block.
22525 try-block:
22526 try compound-statement handler-seq */
22528 static tree
22529 cp_parser_try_block (cp_parser* parser)
22531 tree try_block;
22533 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
22534 if (parser->in_function_body
22535 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
22536 error ("%<try%> in %<constexpr%> function");
22538 try_block = begin_try_block ();
22539 cp_parser_compound_statement (parser, NULL, true, false);
22540 finish_try_block (try_block);
22541 cp_parser_handler_seq (parser);
22542 finish_handler_sequence (try_block);
22544 return try_block;
22547 /* Parse a function-try-block.
22549 function-try-block:
22550 try ctor-initializer [opt] function-body handler-seq */
22552 static bool
22553 cp_parser_function_try_block (cp_parser* parser)
22555 tree compound_stmt;
22556 tree try_block;
22557 bool ctor_initializer_p;
22559 /* Look for the `try' keyword. */
22560 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
22561 return false;
22562 /* Let the rest of the front end know where we are. */
22563 try_block = begin_function_try_block (&compound_stmt);
22564 /* Parse the function-body. */
22565 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
22566 (parser, /*in_function_try_block=*/true);
22567 /* We're done with the `try' part. */
22568 finish_function_try_block (try_block);
22569 /* Parse the handlers. */
22570 cp_parser_handler_seq (parser);
22571 /* We're done with the handlers. */
22572 finish_function_handler_sequence (try_block, compound_stmt);
22574 return ctor_initializer_p;
22577 /* Parse a handler-seq.
22579 handler-seq:
22580 handler handler-seq [opt] */
22582 static void
22583 cp_parser_handler_seq (cp_parser* parser)
22585 while (true)
22587 cp_token *token;
22589 /* Parse the handler. */
22590 cp_parser_handler (parser);
22591 /* Peek at the next token. */
22592 token = cp_lexer_peek_token (parser->lexer);
22593 /* If it's not `catch' then there are no more handlers. */
22594 if (!cp_parser_is_keyword (token, RID_CATCH))
22595 break;
22599 /* Parse a handler.
22601 handler:
22602 catch ( exception-declaration ) compound-statement */
22604 static void
22605 cp_parser_handler (cp_parser* parser)
22607 tree handler;
22608 tree declaration;
22610 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
22611 handler = begin_handler ();
22612 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22613 declaration = cp_parser_exception_declaration (parser);
22614 finish_handler_parms (declaration, handler);
22615 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22616 cp_parser_compound_statement (parser, NULL, false, false);
22617 finish_handler (handler);
22620 /* Parse an exception-declaration.
22622 exception-declaration:
22623 type-specifier-seq declarator
22624 type-specifier-seq abstract-declarator
22625 type-specifier-seq
22628 Returns a VAR_DECL for the declaration, or NULL_TREE if the
22629 ellipsis variant is used. */
22631 static tree
22632 cp_parser_exception_declaration (cp_parser* parser)
22634 cp_decl_specifier_seq type_specifiers;
22635 cp_declarator *declarator;
22636 const char *saved_message;
22638 /* If it's an ellipsis, it's easy to handle. */
22639 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22641 /* Consume the `...' token. */
22642 cp_lexer_consume_token (parser->lexer);
22643 return NULL_TREE;
22646 /* Types may not be defined in exception-declarations. */
22647 saved_message = parser->type_definition_forbidden_message;
22648 parser->type_definition_forbidden_message
22649 = G_("types may not be defined in exception-declarations");
22651 /* Parse the type-specifier-seq. */
22652 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
22653 /*is_trailing_return=*/false,
22654 &type_specifiers);
22655 /* If it's a `)', then there is no declarator. */
22656 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22657 declarator = NULL;
22658 else
22659 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
22660 /*ctor_dtor_or_conv_p=*/NULL,
22661 /*parenthesized_p=*/NULL,
22662 /*member_p=*/false,
22663 /*friend_p=*/false);
22665 /* Restore the saved message. */
22666 parser->type_definition_forbidden_message = saved_message;
22668 if (!type_specifiers.any_specifiers_p)
22669 return error_mark_node;
22671 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
22674 /* Parse a throw-expression.
22676 throw-expression:
22677 throw assignment-expression [opt]
22679 Returns a THROW_EXPR representing the throw-expression. */
22681 static tree
22682 cp_parser_throw_expression (cp_parser* parser)
22684 tree expression;
22685 cp_token* token;
22687 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
22688 token = cp_lexer_peek_token (parser->lexer);
22689 /* Figure out whether or not there is an assignment-expression
22690 following the "throw" keyword. */
22691 if (token->type == CPP_COMMA
22692 || token->type == CPP_SEMICOLON
22693 || token->type == CPP_CLOSE_PAREN
22694 || token->type == CPP_CLOSE_SQUARE
22695 || token->type == CPP_CLOSE_BRACE
22696 || token->type == CPP_COLON)
22697 expression = NULL_TREE;
22698 else
22699 expression = cp_parser_assignment_expression (parser);
22701 return build_throw (expression);
22704 /* GNU Extensions */
22706 /* Parse an (optional) asm-specification.
22708 asm-specification:
22709 asm ( string-literal )
22711 If the asm-specification is present, returns a STRING_CST
22712 corresponding to the string-literal. Otherwise, returns
22713 NULL_TREE. */
22715 static tree
22716 cp_parser_asm_specification_opt (cp_parser* parser)
22718 cp_token *token;
22719 tree asm_specification;
22721 /* Peek at the next token. */
22722 token = cp_lexer_peek_token (parser->lexer);
22723 /* If the next token isn't the `asm' keyword, then there's no
22724 asm-specification. */
22725 if (!cp_parser_is_keyword (token, RID_ASM))
22726 return NULL_TREE;
22728 /* Consume the `asm' token. */
22729 cp_lexer_consume_token (parser->lexer);
22730 /* Look for the `('. */
22731 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22733 /* Look for the string-literal. */
22734 asm_specification = cp_parser_string_literal (parser, false, false);
22736 /* Look for the `)'. */
22737 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22739 return asm_specification;
22742 /* Parse an asm-operand-list.
22744 asm-operand-list:
22745 asm-operand
22746 asm-operand-list , asm-operand
22748 asm-operand:
22749 string-literal ( expression )
22750 [ string-literal ] string-literal ( expression )
22752 Returns a TREE_LIST representing the operands. The TREE_VALUE of
22753 each node is the expression. The TREE_PURPOSE is itself a
22754 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
22755 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
22756 is a STRING_CST for the string literal before the parenthesis. Returns
22757 ERROR_MARK_NODE if any of the operands are invalid. */
22759 static tree
22760 cp_parser_asm_operand_list (cp_parser* parser)
22762 tree asm_operands = NULL_TREE;
22763 bool invalid_operands = false;
22765 while (true)
22767 tree string_literal;
22768 tree expression;
22769 tree name;
22771 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22773 /* Consume the `[' token. */
22774 cp_lexer_consume_token (parser->lexer);
22775 /* Read the operand name. */
22776 name = cp_parser_identifier (parser);
22777 if (name != error_mark_node)
22778 name = build_string (IDENTIFIER_LENGTH (name),
22779 IDENTIFIER_POINTER (name));
22780 /* Look for the closing `]'. */
22781 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22783 else
22784 name = NULL_TREE;
22785 /* Look for the string-literal. */
22786 string_literal = cp_parser_string_literal (parser, false, false);
22788 /* Look for the `('. */
22789 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22790 /* Parse the expression. */
22791 expression = cp_parser_expression (parser);
22792 /* Look for the `)'. */
22793 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22795 if (name == error_mark_node
22796 || string_literal == error_mark_node
22797 || expression == error_mark_node)
22798 invalid_operands = true;
22800 /* Add this operand to the list. */
22801 asm_operands = tree_cons (build_tree_list (name, string_literal),
22802 expression,
22803 asm_operands);
22804 /* If the next token is not a `,', there are no more
22805 operands. */
22806 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22807 break;
22808 /* Consume the `,'. */
22809 cp_lexer_consume_token (parser->lexer);
22812 return invalid_operands ? error_mark_node : nreverse (asm_operands);
22815 /* Parse an asm-clobber-list.
22817 asm-clobber-list:
22818 string-literal
22819 asm-clobber-list , string-literal
22821 Returns a TREE_LIST, indicating the clobbers in the order that they
22822 appeared. The TREE_VALUE of each node is a STRING_CST. */
22824 static tree
22825 cp_parser_asm_clobber_list (cp_parser* parser)
22827 tree clobbers = NULL_TREE;
22829 while (true)
22831 tree string_literal;
22833 /* Look for the string literal. */
22834 string_literal = cp_parser_string_literal (parser, false, false);
22835 /* Add it to the list. */
22836 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
22837 /* If the next token is not a `,', then the list is
22838 complete. */
22839 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22840 break;
22841 /* Consume the `,' token. */
22842 cp_lexer_consume_token (parser->lexer);
22845 return clobbers;
22848 /* Parse an asm-label-list.
22850 asm-label-list:
22851 identifier
22852 asm-label-list , identifier
22854 Returns a TREE_LIST, indicating the labels in the order that they
22855 appeared. The TREE_VALUE of each node is a label. */
22857 static tree
22858 cp_parser_asm_label_list (cp_parser* parser)
22860 tree labels = NULL_TREE;
22862 while (true)
22864 tree identifier, label, name;
22866 /* Look for the identifier. */
22867 identifier = cp_parser_identifier (parser);
22868 if (!error_operand_p (identifier))
22870 label = lookup_label (identifier);
22871 if (TREE_CODE (label) == LABEL_DECL)
22873 TREE_USED (label) = 1;
22874 check_goto (label);
22875 name = build_string (IDENTIFIER_LENGTH (identifier),
22876 IDENTIFIER_POINTER (identifier));
22877 labels = tree_cons (name, label, labels);
22880 /* If the next token is not a `,', then the list is
22881 complete. */
22882 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22883 break;
22884 /* Consume the `,' token. */
22885 cp_lexer_consume_token (parser->lexer);
22888 return nreverse (labels);
22891 /* Return TRUE iff the next tokens in the stream are possibly the
22892 beginning of a GNU extension attribute. */
22894 static bool
22895 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
22897 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
22900 /* Return TRUE iff the next tokens in the stream are possibly the
22901 beginning of a standard C++-11 attribute specifier. */
22903 static bool
22904 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
22906 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
22909 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22910 beginning of a standard C++-11 attribute specifier. */
22912 static bool
22913 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
22915 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22917 return (cxx_dialect >= cxx11
22918 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
22919 || (token->type == CPP_OPEN_SQUARE
22920 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
22921 && token->type == CPP_OPEN_SQUARE)));
22924 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22925 beginning of a GNU extension attribute. */
22927 static bool
22928 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
22930 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22932 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
22935 /* Return true iff the next tokens can be the beginning of either a
22936 GNU attribute list, or a standard C++11 attribute sequence. */
22938 static bool
22939 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
22941 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
22942 || cp_next_tokens_can_be_std_attribute_p (parser));
22945 /* Return true iff the next Nth tokens can be the beginning of either
22946 a GNU attribute list, or a standard C++11 attribute sequence. */
22948 static bool
22949 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
22951 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
22952 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
22955 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
22956 of GNU attributes, or return NULL. */
22958 static tree
22959 cp_parser_attributes_opt (cp_parser *parser)
22961 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
22962 return cp_parser_gnu_attributes_opt (parser);
22963 return cp_parser_std_attribute_spec_seq (parser);
22966 #define CILK_SIMD_FN_CLAUSE_MASK \
22967 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
22968 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
22969 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
22970 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
22971 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
22973 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
22974 vector [(<clauses>)] */
22976 static void
22977 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
22979 bool first_p = parser->cilk_simd_fn_info == NULL;
22980 cp_token *token = v_token;
22981 if (first_p)
22983 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
22984 parser->cilk_simd_fn_info->error_seen = false;
22985 parser->cilk_simd_fn_info->fndecl_seen = false;
22986 parser->cilk_simd_fn_info->tokens = vNULL;
22988 int paren_scope = 0;
22989 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22991 cp_lexer_consume_token (parser->lexer);
22992 v_token = cp_lexer_peek_token (parser->lexer);
22993 paren_scope++;
22995 while (paren_scope > 0)
22997 token = cp_lexer_peek_token (parser->lexer);
22998 if (token->type == CPP_OPEN_PAREN)
22999 paren_scope++;
23000 else if (token->type == CPP_CLOSE_PAREN)
23001 paren_scope--;
23002 /* Do not push the last ')' */
23003 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
23004 cp_lexer_consume_token (parser->lexer);
23007 token->type = CPP_PRAGMA_EOL;
23008 parser->lexer->next_token = token;
23009 cp_lexer_consume_token (parser->lexer);
23011 struct cp_token_cache *cp
23012 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
23013 parser->cilk_simd_fn_info->tokens.safe_push (cp);
23016 /* Parse an (optional) series of attributes.
23018 attributes:
23019 attributes attribute
23021 attribute:
23022 __attribute__ (( attribute-list [opt] ))
23024 The return value is as for cp_parser_gnu_attribute_list. */
23026 static tree
23027 cp_parser_gnu_attributes_opt (cp_parser* parser)
23029 tree attributes = NULL_TREE;
23031 while (true)
23033 cp_token *token;
23034 tree attribute_list;
23035 bool ok = true;
23037 /* Peek at the next token. */
23038 token = cp_lexer_peek_token (parser->lexer);
23039 /* If it's not `__attribute__', then we're done. */
23040 if (token->keyword != RID_ATTRIBUTE)
23041 break;
23043 /* Consume the `__attribute__' keyword. */
23044 cp_lexer_consume_token (parser->lexer);
23045 /* Look for the two `(' tokens. */
23046 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23047 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23049 /* Peek at the next token. */
23050 token = cp_lexer_peek_token (parser->lexer);
23051 if (token->type != CPP_CLOSE_PAREN)
23052 /* Parse the attribute-list. */
23053 attribute_list = cp_parser_gnu_attribute_list (parser);
23054 else
23055 /* If the next token is a `)', then there is no attribute
23056 list. */
23057 attribute_list = NULL;
23059 /* Look for the two `)' tokens. */
23060 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23061 ok = false;
23062 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23063 ok = false;
23064 if (!ok)
23065 cp_parser_skip_to_end_of_statement (parser);
23067 /* Add these new attributes to the list. */
23068 attributes = chainon (attributes, attribute_list);
23071 return attributes;
23074 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
23075 "__vector" or "__vector__." */
23077 static inline bool
23078 is_cilkplus_vector_p (tree name)
23080 if (flag_cilkplus && is_attribute_p ("vector", name))
23081 return true;
23082 return false;
23085 /* Parse a GNU attribute-list.
23087 attribute-list:
23088 attribute
23089 attribute-list , attribute
23091 attribute:
23092 identifier
23093 identifier ( identifier )
23094 identifier ( identifier , expression-list )
23095 identifier ( expression-list )
23097 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
23098 to an attribute. The TREE_PURPOSE of each node is the identifier
23099 indicating which attribute is in use. The TREE_VALUE represents
23100 the arguments, if any. */
23102 static tree
23103 cp_parser_gnu_attribute_list (cp_parser* parser)
23105 tree attribute_list = NULL_TREE;
23106 bool save_translate_strings_p = parser->translate_strings_p;
23108 parser->translate_strings_p = false;
23109 while (true)
23111 cp_token *token;
23112 tree identifier;
23113 tree attribute;
23115 /* Look for the identifier. We also allow keywords here; for
23116 example `__attribute__ ((const))' is legal. */
23117 token = cp_lexer_peek_token (parser->lexer);
23118 if (token->type == CPP_NAME
23119 || token->type == CPP_KEYWORD)
23121 tree arguments = NULL_TREE;
23123 /* Consume the token, but save it since we need it for the
23124 SIMD enabled function parsing. */
23125 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
23127 /* Save away the identifier that indicates which attribute
23128 this is. */
23129 identifier = (token->type == CPP_KEYWORD)
23130 /* For keywords, use the canonical spelling, not the
23131 parsed identifier. */
23132 ? ridpointers[(int) token->keyword]
23133 : id_token->u.value;
23135 attribute = build_tree_list (identifier, NULL_TREE);
23137 /* Peek at the next token. */
23138 token = cp_lexer_peek_token (parser->lexer);
23139 /* If it's an `(', then parse the attribute arguments. */
23140 if (token->type == CPP_OPEN_PAREN)
23142 vec<tree, va_gc> *vec;
23143 int attr_flag = (attribute_takes_identifier_p (identifier)
23144 ? id_attr : normal_attr);
23145 if (is_cilkplus_vector_p (identifier))
23147 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
23148 continue;
23150 else
23151 vec = cp_parser_parenthesized_expression_list
23152 (parser, attr_flag, /*cast_p=*/false,
23153 /*allow_expansion_p=*/false,
23154 /*non_constant_p=*/NULL);
23155 if (vec == NULL)
23156 arguments = error_mark_node;
23157 else
23159 arguments = build_tree_list_vec (vec);
23160 release_tree_vector (vec);
23162 /* Save the arguments away. */
23163 TREE_VALUE (attribute) = arguments;
23165 else if (is_cilkplus_vector_p (identifier))
23167 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
23168 continue;
23171 if (arguments != error_mark_node)
23173 /* Add this attribute to the list. */
23174 TREE_CHAIN (attribute) = attribute_list;
23175 attribute_list = attribute;
23178 token = cp_lexer_peek_token (parser->lexer);
23180 /* Now, look for more attributes. If the next token isn't a
23181 `,', we're done. */
23182 if (token->type != CPP_COMMA)
23183 break;
23185 /* Consume the comma and keep going. */
23186 cp_lexer_consume_token (parser->lexer);
23188 parser->translate_strings_p = save_translate_strings_p;
23190 /* We built up the list in reverse order. */
23191 return nreverse (attribute_list);
23194 /* Parse a standard C++11 attribute.
23196 The returned representation is a TREE_LIST which TREE_PURPOSE is
23197 the scoped name of the attribute, and the TREE_VALUE is its
23198 arguments list.
23200 Note that the scoped name of the attribute is itself a TREE_LIST
23201 which TREE_PURPOSE is the namespace of the attribute, and
23202 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
23203 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
23204 and which TREE_PURPOSE is directly the attribute name.
23206 Clients of the attribute code should use get_attribute_namespace
23207 and get_attribute_name to get the actual namespace and name of
23208 attributes, regardless of their being GNU or C++11 attributes.
23210 attribute:
23211 attribute-token attribute-argument-clause [opt]
23213 attribute-token:
23214 identifier
23215 attribute-scoped-token
23217 attribute-scoped-token:
23218 attribute-namespace :: identifier
23220 attribute-namespace:
23221 identifier
23223 attribute-argument-clause:
23224 ( balanced-token-seq )
23226 balanced-token-seq:
23227 balanced-token [opt]
23228 balanced-token-seq balanced-token
23230 balanced-token:
23231 ( balanced-token-seq )
23232 [ balanced-token-seq ]
23233 { balanced-token-seq }. */
23235 static tree
23236 cp_parser_std_attribute (cp_parser *parser)
23238 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
23239 cp_token *token;
23241 /* First, parse name of the attribute, a.k.a attribute-token. */
23243 token = cp_lexer_peek_token (parser->lexer);
23244 if (token->type == CPP_NAME)
23245 attr_id = token->u.value;
23246 else if (token->type == CPP_KEYWORD)
23247 attr_id = ridpointers[(int) token->keyword];
23248 else if (token->flags & NAMED_OP)
23249 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
23251 if (attr_id == NULL_TREE)
23252 return NULL_TREE;
23254 cp_lexer_consume_token (parser->lexer);
23256 token = cp_lexer_peek_token (parser->lexer);
23257 if (token->type == CPP_SCOPE)
23259 /* We are seeing a scoped attribute token. */
23261 cp_lexer_consume_token (parser->lexer);
23262 attr_ns = attr_id;
23264 token = cp_lexer_consume_token (parser->lexer);
23265 if (token->type == CPP_NAME)
23266 attr_id = token->u.value;
23267 else if (token->type == CPP_KEYWORD)
23268 attr_id = ridpointers[(int) token->keyword];
23269 else
23271 error_at (token->location,
23272 "expected an identifier for the attribute name");
23273 return error_mark_node;
23275 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
23276 NULL_TREE);
23277 token = cp_lexer_peek_token (parser->lexer);
23279 else
23281 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
23282 NULL_TREE);
23283 /* C++11 noreturn attribute is equivalent to GNU's. */
23284 if (is_attribute_p ("noreturn", attr_id))
23285 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
23286 /* C++14 deprecated attribute is equivalent to GNU's. */
23287 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
23289 if (cxx_dialect == cxx11)
23290 pedwarn (token->location, OPT_Wpedantic,
23291 "%<deprecated%> is a C++14 feature;"
23292 " use %<gnu::deprecated%>");
23293 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
23297 /* Now parse the optional argument clause of the attribute. */
23299 if (token->type != CPP_OPEN_PAREN)
23300 return attribute;
23303 vec<tree, va_gc> *vec;
23304 int attr_flag = normal_attr;
23306 if (attr_ns == get_identifier ("gnu")
23307 && attribute_takes_identifier_p (attr_id))
23308 /* A GNU attribute that takes an identifier in parameter. */
23309 attr_flag = id_attr;
23311 vec = cp_parser_parenthesized_expression_list
23312 (parser, attr_flag, /*cast_p=*/false,
23313 /*allow_expansion_p=*/true,
23314 /*non_constant_p=*/NULL);
23315 if (vec == NULL)
23316 arguments = error_mark_node;
23317 else
23319 arguments = build_tree_list_vec (vec);
23320 release_tree_vector (vec);
23323 if (arguments == error_mark_node)
23324 attribute = error_mark_node;
23325 else
23326 TREE_VALUE (attribute) = arguments;
23329 return attribute;
23332 /* Check that the attribute ATTRIBUTE appears at most once in the
23333 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
23334 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
23335 isn't implemented yet in GCC. */
23337 static void
23338 cp_parser_check_std_attribute (tree attributes, tree attribute)
23340 if (attributes)
23342 tree name = get_attribute_name (attribute);
23343 if (is_attribute_p ("noreturn", name)
23344 && lookup_attribute ("noreturn", attributes))
23345 error ("attribute noreturn can appear at most once "
23346 "in an attribute-list");
23347 else if (is_attribute_p ("deprecated", name)
23348 && lookup_attribute ("deprecated", attributes))
23349 error ("attribute deprecated can appear at most once "
23350 "in an attribute-list");
23354 /* Parse a list of standard C++-11 attributes.
23356 attribute-list:
23357 attribute [opt]
23358 attribute-list , attribute[opt]
23359 attribute ...
23360 attribute-list , attribute ...
23363 static tree
23364 cp_parser_std_attribute_list (cp_parser *parser)
23366 tree attributes = NULL_TREE, attribute = NULL_TREE;
23367 cp_token *token = NULL;
23369 while (true)
23371 attribute = cp_parser_std_attribute (parser);
23372 if (attribute == error_mark_node)
23373 break;
23374 if (attribute != NULL_TREE)
23376 cp_parser_check_std_attribute (attributes, attribute);
23377 TREE_CHAIN (attribute) = attributes;
23378 attributes = attribute;
23380 token = cp_lexer_peek_token (parser->lexer);
23381 if (token->type == CPP_ELLIPSIS)
23383 cp_lexer_consume_token (parser->lexer);
23384 TREE_VALUE (attribute)
23385 = make_pack_expansion (TREE_VALUE (attribute));
23386 token = cp_lexer_peek_token (parser->lexer);
23388 if (token->type != CPP_COMMA)
23389 break;
23390 cp_lexer_consume_token (parser->lexer);
23392 attributes = nreverse (attributes);
23393 return attributes;
23396 /* Parse a standard C++-11 attribute specifier.
23398 attribute-specifier:
23399 [ [ attribute-list ] ]
23400 alignment-specifier
23402 alignment-specifier:
23403 alignas ( type-id ... [opt] )
23404 alignas ( alignment-expression ... [opt] ). */
23406 static tree
23407 cp_parser_std_attribute_spec (cp_parser *parser)
23409 tree attributes = NULL_TREE;
23410 cp_token *token = cp_lexer_peek_token (parser->lexer);
23412 if (token->type == CPP_OPEN_SQUARE
23413 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
23415 cp_lexer_consume_token (parser->lexer);
23416 cp_lexer_consume_token (parser->lexer);
23418 attributes = cp_parser_std_attribute_list (parser);
23420 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
23421 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
23422 cp_parser_skip_to_end_of_statement (parser);
23423 else
23424 /* Warn about parsing c++11 attribute in non-c++1 mode, only
23425 when we are sure that we have actually parsed them. */
23426 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
23428 else
23430 tree alignas_expr;
23432 /* Look for an alignment-specifier. */
23434 token = cp_lexer_peek_token (parser->lexer);
23436 if (token->type != CPP_KEYWORD
23437 || token->keyword != RID_ALIGNAS)
23438 return NULL_TREE;
23440 cp_lexer_consume_token (parser->lexer);
23441 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
23443 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
23445 cp_parser_error (parser, "expected %<(%>");
23446 return error_mark_node;
23449 cp_parser_parse_tentatively (parser);
23450 alignas_expr = cp_parser_type_id (parser);
23452 if (!cp_parser_parse_definitely (parser))
23454 gcc_assert (alignas_expr == error_mark_node
23455 || alignas_expr == NULL_TREE);
23457 alignas_expr =
23458 cp_parser_assignment_expression (parser);
23459 if (alignas_expr == error_mark_node)
23460 cp_parser_skip_to_end_of_statement (parser);
23461 if (alignas_expr == NULL_TREE
23462 || alignas_expr == error_mark_node)
23463 return alignas_expr;
23466 alignas_expr = cxx_alignas_expr (alignas_expr);
23467 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
23469 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23471 cp_lexer_consume_token (parser->lexer);
23472 alignas_expr = make_pack_expansion (alignas_expr);
23475 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
23477 cp_parser_error (parser, "expected %<)%>");
23478 return error_mark_node;
23481 /* Build the C++-11 representation of an 'aligned'
23482 attribute. */
23483 attributes =
23484 build_tree_list (build_tree_list (get_identifier ("gnu"),
23485 get_identifier ("aligned")),
23486 alignas_expr);
23489 return attributes;
23492 /* Parse a standard C++-11 attribute-specifier-seq.
23494 attribute-specifier-seq:
23495 attribute-specifier-seq [opt] attribute-specifier
23498 static tree
23499 cp_parser_std_attribute_spec_seq (cp_parser *parser)
23501 tree attr_specs = NULL;
23503 while (true)
23505 tree attr_spec = cp_parser_std_attribute_spec (parser);
23506 if (attr_spec == NULL_TREE)
23507 break;
23508 if (attr_spec == error_mark_node)
23509 return error_mark_node;
23511 TREE_CHAIN (attr_spec) = attr_specs;
23512 attr_specs = attr_spec;
23515 attr_specs = nreverse (attr_specs);
23516 return attr_specs;
23519 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
23520 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
23521 current value of the PEDANTIC flag, regardless of whether or not
23522 the `__extension__' keyword is present. The caller is responsible
23523 for restoring the value of the PEDANTIC flag. */
23525 static bool
23526 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
23528 /* Save the old value of the PEDANTIC flag. */
23529 *saved_pedantic = pedantic;
23531 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
23533 /* Consume the `__extension__' token. */
23534 cp_lexer_consume_token (parser->lexer);
23535 /* We're not being pedantic while the `__extension__' keyword is
23536 in effect. */
23537 pedantic = 0;
23539 return true;
23542 return false;
23545 /* Parse a label declaration.
23547 label-declaration:
23548 __label__ label-declarator-seq ;
23550 label-declarator-seq:
23551 identifier , label-declarator-seq
23552 identifier */
23554 static void
23555 cp_parser_label_declaration (cp_parser* parser)
23557 /* Look for the `__label__' keyword. */
23558 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
23560 while (true)
23562 tree identifier;
23564 /* Look for an identifier. */
23565 identifier = cp_parser_identifier (parser);
23566 /* If we failed, stop. */
23567 if (identifier == error_mark_node)
23568 break;
23569 /* Declare it as a label. */
23570 finish_label_decl (identifier);
23571 /* If the next token is a `;', stop. */
23572 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23573 break;
23574 /* Look for the `,' separating the label declarations. */
23575 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
23578 /* Look for the final `;'. */
23579 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23582 // -------------------------------------------------------------------------- //
23583 // Requires Clause
23585 // Parse a requires clause.
23587 // requires-clause:
23588 // 'requires' logical-or-expression
23590 // The required logical-or-expression must be a constant expression. Note
23591 // that we don't check that the expression is constepxr here. We defer until
23592 // we analyze constraints and then, we only check atomic constraints.
23593 static tree
23594 cp_parser_requires_clause (cp_parser *parser)
23596 // Parse the requires clause so that it is not automatically folded.
23597 ++processing_template_decl;
23598 tree expr = cp_parser_binary_expression (parser, false, false,
23599 PREC_NOT_OPERATOR, NULL);
23600 --processing_template_decl;
23601 return expr;
23604 // Optionally parse a requires clause:
23605 static tree
23606 cp_parser_requires_clause_opt (cp_parser *parser)
23608 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
23609 return NULL_TREE;
23610 cp_lexer_consume_token (parser->lexer);
23611 return cp_parser_requires_clause (parser);
23615 /*---------------------------------------------------------------------------
23616 Requires expressions
23617 ---------------------------------------------------------------------------*/
23619 /* Parse a requires expression
23621 requirement-expression:
23622 'requires' requirement-parameter-list [opt] requirement-body */
23623 static tree
23624 cp_parser_requires_expression (cp_parser *parser)
23626 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
23627 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
23629 /* A requires-expression shall appear only within a concept
23630 definition or a requires-clause.
23632 TODO: Implement this diagnostic correctly. */
23633 if (!processing_template_decl)
23635 error_at (loc, "a requires expression cannot appear outside a template");
23636 cp_parser_skip_to_end_of_statement (parser);
23637 return error_mark_node;
23640 tree parms, reqs;
23642 /* Local parameters are delared as variables within the scope
23643 of the expression. They are not visible past the end of
23644 the expression. Expressions within the requires-expression
23645 are unevaluated. */
23646 struct scope_sentinel
23648 scope_sentinel ()
23650 ++cp_unevaluated_operand;
23651 begin_scope (sk_block, NULL_TREE);
23654 ~scope_sentinel ()
23656 pop_bindings_and_leave_scope ();
23657 --cp_unevaluated_operand;
23659 } s;
23661 /* Parse the optional parameter list. */
23662 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23664 parms = cp_parser_requirement_parameter_list (parser);
23665 if (parms == error_mark_node)
23666 return error_mark_node;
23668 else
23669 parms = NULL_TREE;
23671 /* Parse the requirement body. */
23672 reqs = cp_parser_requirement_body (parser);
23673 if (reqs == error_mark_node)
23674 return error_mark_node;
23677 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
23678 the parm chain. */
23679 grokparms (parms, &parms);
23680 return finish_requires_expr (parms, reqs);
23683 /* Parse a parameterized requirement.
23685 requirement-parameter-list:
23686 '(' parameter-declaration-clause ')' */
23687 static tree
23688 cp_parser_requirement_parameter_list (cp_parser *parser)
23690 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23691 return error_mark_node;
23693 tree parms = cp_parser_parameter_declaration_clause (parser);
23695 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23696 return error_mark_node;
23698 return parms;
23701 /* Parse the body of a requirement.
23703 requirement-body:
23704 '{' requirement-list '}' */
23705 static tree
23706 cp_parser_requirement_body (cp_parser *parser)
23708 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
23709 return error_mark_node;
23711 tree reqs = cp_parser_requirement_list (parser);
23713 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
23714 return error_mark_node;
23716 return reqs;
23719 /* Parse a list of requirements.
23721 requirement-list:
23722 requirement
23723 requirement-list ';' requirement[opt] */
23724 static tree
23725 cp_parser_requirement_list (cp_parser *parser)
23727 tree result = NULL_TREE;
23728 while (true)
23730 tree req = cp_parser_requirement (parser);
23731 if (req == error_mark_node)
23732 return error_mark_node;
23734 result = tree_cons (NULL_TREE, req, result);
23736 /* If we see a semi-colon, consume it. */
23737 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23738 cp_lexer_consume_token (parser->lexer);
23740 /* Stop processing at the end of the list. */
23741 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
23742 break;
23745 /* Reverse the order of requirements so they are analyzed in
23746 declaration order. */
23747 return nreverse (result);
23750 /* Parse a syntactic requirement or type requirement.
23752 requirement:
23753 simple-requirement
23754 compound-requirement
23755 type-requirement
23756 nested-requirement */
23757 static tree
23758 cp_parser_requirement (cp_parser *parser)
23760 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23761 return cp_parser_compound_requirement (parser);
23762 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
23763 return cp_parser_type_requirement (parser);
23764 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
23765 return cp_parser_nested_requirement (parser);
23766 else
23767 return cp_parser_simple_requirement (parser);
23770 /* Parse a simple requirement.
23772 simple-requirement:
23773 expression ';' */
23774 static tree
23775 cp_parser_simple_requirement (cp_parser *parser)
23777 tree expr = cp_parser_expression (parser, NULL, false, false);
23778 if (!expr || expr == error_mark_node)
23779 return error_mark_node;
23781 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
23782 return error_mark_node;
23784 return finish_simple_requirement (expr);
23787 /* Parse a type requirement
23789 type-requirement
23790 nested-name-specifier [opt] required-type-name ';'
23792 required-type-name:
23793 type-name
23794 'template' [opt] simple-template-id */
23795 static tree
23796 cp_parser_type_requirement (cp_parser *parser)
23798 cp_lexer_consume_token (parser->lexer);
23800 // Save the scope before parsing name specifiers.
23801 tree saved_scope = parser->scope;
23802 tree saved_object_scope = parser->object_scope;
23803 tree saved_qualifying_scope = parser->qualifying_scope;
23804 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
23805 cp_parser_nested_name_specifier_opt (parser,
23806 /*typename_keyword_p=*/true,
23807 /*check_dependency_p=*/false,
23808 /*type_p=*/true,
23809 /*is_declaration=*/false);
23811 tree type;
23812 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23814 cp_lexer_consume_token (parser->lexer);
23815 type = cp_parser_template_id (parser,
23816 /*template_keyword_p=*/true,
23817 /*check_dependency=*/false,
23818 /*tag_type=*/none_type,
23819 /*is_declaration=*/false);
23820 type = make_typename_type (parser->scope, type, typename_type,
23821 /*complain=*/tf_error);
23823 else
23824 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
23826 if (TREE_CODE (type) == TYPE_DECL)
23827 type = TREE_TYPE (type);
23829 parser->scope = saved_scope;
23830 parser->object_scope = saved_object_scope;
23831 parser->qualifying_scope = saved_qualifying_scope;
23833 if (type == error_mark_node)
23834 cp_parser_skip_to_end_of_statement (parser);
23836 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
23837 return error_mark_node;
23838 if (type == error_mark_node)
23839 return error_mark_node;
23841 return finish_type_requirement (type);
23844 /* Parse a compound requirement
23846 compound-requirement:
23847 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
23848 static tree
23849 cp_parser_compound_requirement (cp_parser *parser)
23851 /* Parse an expression enclosed in '{ }'s. */
23852 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
23853 return error_mark_node;
23855 tree expr = cp_parser_expression (parser, NULL, false, false);
23856 if (!expr || expr == error_mark_node)
23857 return error_mark_node;
23859 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
23860 return error_mark_node;
23862 /* Parse the optional noexcept. */
23863 bool noexcept_p = false;
23864 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
23866 cp_lexer_consume_token (parser->lexer);
23867 noexcept_p = true;
23870 /* Parse the optional trailing return type. */
23871 tree type = NULL_TREE;
23872 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
23874 cp_lexer_consume_token (parser->lexer);
23875 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
23876 parser->in_result_type_constraint_p = true;
23877 type = cp_parser_trailing_type_id (parser);
23878 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
23879 if (type == error_mark_node)
23880 return error_mark_node;
23883 return finish_compound_requirement (expr, type, noexcept_p);
23886 /* Parse a nested requirement. This is the same as a requires clause.
23888 nested-requirement:
23889 requires-clause */
23890 static tree
23891 cp_parser_nested_requirement (cp_parser *parser)
23893 cp_lexer_consume_token (parser->lexer);
23894 tree req = cp_parser_requires_clause (parser);
23895 if (req == error_mark_node)
23896 return error_mark_node;
23897 return finish_nested_requirement (req);
23900 /* Support Functions */
23902 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
23903 NAME should have one of the representations used for an
23904 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
23905 is returned. If PARSER->SCOPE is a dependent type, then a
23906 SCOPE_REF is returned.
23908 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
23909 returned; the name was already resolved when the TEMPLATE_ID_EXPR
23910 was formed. Abstractly, such entities should not be passed to this
23911 function, because they do not need to be looked up, but it is
23912 simpler to check for this special case here, rather than at the
23913 call-sites.
23915 In cases not explicitly covered above, this function returns a
23916 DECL, OVERLOAD, or baselink representing the result of the lookup.
23917 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
23918 is returned.
23920 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
23921 (e.g., "struct") that was used. In that case bindings that do not
23922 refer to types are ignored.
23924 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
23925 ignored.
23927 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
23928 are ignored.
23930 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
23931 types.
23933 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
23934 TREE_LIST of candidates if name-lookup results in an ambiguity, and
23935 NULL_TREE otherwise. */
23937 static tree
23938 cp_parser_lookup_name (cp_parser *parser, tree name,
23939 enum tag_types tag_type,
23940 bool is_template,
23941 bool is_namespace,
23942 bool check_dependency,
23943 tree *ambiguous_decls,
23944 location_t name_location)
23946 tree decl;
23947 tree object_type = parser->context->object_type;
23949 /* Assume that the lookup will be unambiguous. */
23950 if (ambiguous_decls)
23951 *ambiguous_decls = NULL_TREE;
23953 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
23954 no longer valid. Note that if we are parsing tentatively, and
23955 the parse fails, OBJECT_TYPE will be automatically restored. */
23956 parser->context->object_type = NULL_TREE;
23958 if (name == error_mark_node)
23959 return error_mark_node;
23961 /* A template-id has already been resolved; there is no lookup to
23962 do. */
23963 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
23964 return name;
23965 if (BASELINK_P (name))
23967 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
23968 == TEMPLATE_ID_EXPR);
23969 return name;
23972 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
23973 it should already have been checked to make sure that the name
23974 used matches the type being destroyed. */
23975 if (TREE_CODE (name) == BIT_NOT_EXPR)
23977 tree type;
23979 /* Figure out to which type this destructor applies. */
23980 if (parser->scope)
23981 type = parser->scope;
23982 else if (object_type)
23983 type = object_type;
23984 else
23985 type = current_class_type;
23986 /* If that's not a class type, there is no destructor. */
23987 if (!type || !CLASS_TYPE_P (type))
23988 return error_mark_node;
23989 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
23990 lazily_declare_fn (sfk_destructor, type);
23991 if (!CLASSTYPE_DESTRUCTORS (type))
23992 return error_mark_node;
23993 /* If it was a class type, return the destructor. */
23994 return CLASSTYPE_DESTRUCTORS (type);
23997 /* By this point, the NAME should be an ordinary identifier. If
23998 the id-expression was a qualified name, the qualifying scope is
23999 stored in PARSER->SCOPE at this point. */
24000 gcc_assert (identifier_p (name));
24002 /* Perform the lookup. */
24003 if (parser->scope)
24005 bool dependent_p;
24007 if (parser->scope == error_mark_node)
24008 return error_mark_node;
24010 /* If the SCOPE is dependent, the lookup must be deferred until
24011 the template is instantiated -- unless we are explicitly
24012 looking up names in uninstantiated templates. Even then, we
24013 cannot look up the name if the scope is not a class type; it
24014 might, for example, be a template type parameter. */
24015 dependent_p = (TYPE_P (parser->scope)
24016 && dependent_scope_p (parser->scope));
24017 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
24018 && dependent_p)
24019 /* Defer lookup. */
24020 decl = error_mark_node;
24021 else
24023 tree pushed_scope = NULL_TREE;
24025 /* If PARSER->SCOPE is a dependent type, then it must be a
24026 class type, and we must not be checking dependencies;
24027 otherwise, we would have processed this lookup above. So
24028 that PARSER->SCOPE is not considered a dependent base by
24029 lookup_member, we must enter the scope here. */
24030 if (dependent_p)
24031 pushed_scope = push_scope (parser->scope);
24033 /* If the PARSER->SCOPE is a template specialization, it
24034 may be instantiated during name lookup. In that case,
24035 errors may be issued. Even if we rollback the current
24036 tentative parse, those errors are valid. */
24037 decl = lookup_qualified_name (parser->scope, name,
24038 tag_type != none_type,
24039 /*complain=*/true);
24041 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
24042 lookup result and the nested-name-specifier nominates a class C:
24043 * if the name specified after the nested-name-specifier, when
24044 looked up in C, is the injected-class-name of C (Clause 9), or
24045 * if the name specified after the nested-name-specifier is the
24046 same as the identifier or the simple-template-id's template-
24047 name in the last component of the nested-name-specifier,
24048 the name is instead considered to name the constructor of
24049 class C. [ Note: for example, the constructor is not an
24050 acceptable lookup result in an elaborated-type-specifier so
24051 the constructor would not be used in place of the
24052 injected-class-name. --end note ] Such a constructor name
24053 shall be used only in the declarator-id of a declaration that
24054 names a constructor or in a using-declaration. */
24055 if (tag_type == none_type
24056 && DECL_SELF_REFERENCE_P (decl)
24057 && same_type_p (DECL_CONTEXT (decl), parser->scope))
24058 decl = lookup_qualified_name (parser->scope, ctor_identifier,
24059 tag_type != none_type,
24060 /*complain=*/true);
24062 /* If we have a single function from a using decl, pull it out. */
24063 if (TREE_CODE (decl) == OVERLOAD
24064 && !really_overloaded_fn (decl))
24065 decl = OVL_FUNCTION (decl);
24067 if (pushed_scope)
24068 pop_scope (pushed_scope);
24071 /* If the scope is a dependent type and either we deferred lookup or
24072 we did lookup but didn't find the name, rememeber the name. */
24073 if (decl == error_mark_node && TYPE_P (parser->scope)
24074 && dependent_type_p (parser->scope))
24076 if (tag_type)
24078 tree type;
24080 /* The resolution to Core Issue 180 says that `struct
24081 A::B' should be considered a type-name, even if `A'
24082 is dependent. */
24083 type = make_typename_type (parser->scope, name, tag_type,
24084 /*complain=*/tf_error);
24085 if (type != error_mark_node)
24086 decl = TYPE_NAME (type);
24088 else if (is_template
24089 && (cp_parser_next_token_ends_template_argument_p (parser)
24090 || cp_lexer_next_token_is (parser->lexer,
24091 CPP_CLOSE_PAREN)))
24092 decl = make_unbound_class_template (parser->scope,
24093 name, NULL_TREE,
24094 /*complain=*/tf_error);
24095 else
24096 decl = build_qualified_name (/*type=*/NULL_TREE,
24097 parser->scope, name,
24098 is_template);
24100 parser->qualifying_scope = parser->scope;
24101 parser->object_scope = NULL_TREE;
24103 else if (object_type)
24105 /* Look up the name in the scope of the OBJECT_TYPE, unless the
24106 OBJECT_TYPE is not a class. */
24107 if (CLASS_TYPE_P (object_type))
24108 /* If the OBJECT_TYPE is a template specialization, it may
24109 be instantiated during name lookup. In that case, errors
24110 may be issued. Even if we rollback the current tentative
24111 parse, those errors are valid. */
24112 decl = lookup_member (object_type,
24113 name,
24114 /*protect=*/0,
24115 tag_type != none_type,
24116 tf_warning_or_error);
24117 else
24118 decl = NULL_TREE;
24120 if (!decl)
24121 /* Look it up in the enclosing context. */
24122 decl = lookup_name_real (name, tag_type != none_type,
24123 /*nonclass=*/0,
24124 /*block_p=*/true, is_namespace, 0);
24125 parser->object_scope = object_type;
24126 parser->qualifying_scope = NULL_TREE;
24128 else
24130 decl = lookup_name_real (name, tag_type != none_type,
24131 /*nonclass=*/0,
24132 /*block_p=*/true, is_namespace, 0);
24133 parser->qualifying_scope = NULL_TREE;
24134 parser->object_scope = NULL_TREE;
24137 /* If the lookup failed, let our caller know. */
24138 if (!decl || decl == error_mark_node)
24139 return error_mark_node;
24141 /* Pull out the template from an injected-class-name (or multiple). */
24142 if (is_template)
24143 decl = maybe_get_template_decl_from_type_decl (decl);
24145 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
24146 if (TREE_CODE (decl) == TREE_LIST)
24148 if (ambiguous_decls)
24149 *ambiguous_decls = decl;
24150 /* The error message we have to print is too complicated for
24151 cp_parser_error, so we incorporate its actions directly. */
24152 if (!cp_parser_simulate_error (parser))
24154 error_at (name_location, "reference to %qD is ambiguous",
24155 name);
24156 print_candidates (decl);
24158 return error_mark_node;
24161 gcc_assert (DECL_P (decl)
24162 || TREE_CODE (decl) == OVERLOAD
24163 || TREE_CODE (decl) == SCOPE_REF
24164 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
24165 || BASELINK_P (decl));
24167 /* If we have resolved the name of a member declaration, check to
24168 see if the declaration is accessible. When the name resolves to
24169 set of overloaded functions, accessibility is checked when
24170 overload resolution is done.
24172 During an explicit instantiation, access is not checked at all,
24173 as per [temp.explicit]. */
24174 if (DECL_P (decl))
24175 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
24177 maybe_record_typedef_use (decl);
24179 return decl;
24182 /* Like cp_parser_lookup_name, but for use in the typical case where
24183 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
24184 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
24186 static tree
24187 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
24189 return cp_parser_lookup_name (parser, name,
24190 none_type,
24191 /*is_template=*/false,
24192 /*is_namespace=*/false,
24193 /*check_dependency=*/true,
24194 /*ambiguous_decls=*/NULL,
24195 location);
24198 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
24199 the current context, return the TYPE_DECL. If TAG_NAME_P is
24200 true, the DECL indicates the class being defined in a class-head,
24201 or declared in an elaborated-type-specifier.
24203 Otherwise, return DECL. */
24205 static tree
24206 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
24208 /* If the TEMPLATE_DECL is being declared as part of a class-head,
24209 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
24211 struct A {
24212 template <typename T> struct B;
24215 template <typename T> struct A::B {};
24217 Similarly, in an elaborated-type-specifier:
24219 namespace N { struct X{}; }
24221 struct A {
24222 template <typename T> friend struct N::X;
24225 However, if the DECL refers to a class type, and we are in
24226 the scope of the class, then the name lookup automatically
24227 finds the TYPE_DECL created by build_self_reference rather
24228 than a TEMPLATE_DECL. For example, in:
24230 template <class T> struct S {
24231 S s;
24234 there is no need to handle such case. */
24236 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
24237 return DECL_TEMPLATE_RESULT (decl);
24239 return decl;
24242 /* If too many, or too few, template-parameter lists apply to the
24243 declarator, issue an error message. Returns TRUE if all went well,
24244 and FALSE otherwise. */
24246 static bool
24247 cp_parser_check_declarator_template_parameters (cp_parser* parser,
24248 cp_declarator *declarator,
24249 location_t declarator_location)
24251 switch (declarator->kind)
24253 case cdk_id:
24255 unsigned num_templates = 0;
24256 tree scope = declarator->u.id.qualifying_scope;
24258 if (scope)
24259 num_templates = num_template_headers_for_class (scope);
24260 else if (TREE_CODE (declarator->u.id.unqualified_name)
24261 == TEMPLATE_ID_EXPR)
24262 /* If the DECLARATOR has the form `X<y>' then it uses one
24263 additional level of template parameters. */
24264 ++num_templates;
24266 return cp_parser_check_template_parameters
24267 (parser, num_templates, declarator_location, declarator);
24270 case cdk_function:
24271 case cdk_array:
24272 case cdk_pointer:
24273 case cdk_reference:
24274 case cdk_ptrmem:
24275 return (cp_parser_check_declarator_template_parameters
24276 (parser, declarator->declarator, declarator_location));
24278 case cdk_error:
24279 return true;
24281 default:
24282 gcc_unreachable ();
24284 return false;
24287 /* NUM_TEMPLATES were used in the current declaration. If that is
24288 invalid, return FALSE and issue an error messages. Otherwise,
24289 return TRUE. If DECLARATOR is non-NULL, then we are checking a
24290 declarator and we can print more accurate diagnostics. */
24292 static bool
24293 cp_parser_check_template_parameters (cp_parser* parser,
24294 unsigned num_templates,
24295 location_t location,
24296 cp_declarator *declarator)
24298 /* If there are the same number of template classes and parameter
24299 lists, that's OK. */
24300 if (parser->num_template_parameter_lists == num_templates)
24301 return true;
24302 /* If there are more, but only one more, then we are referring to a
24303 member template. That's OK too. */
24304 if (parser->num_template_parameter_lists == num_templates + 1)
24305 return true;
24306 /* If there are more template classes than parameter lists, we have
24307 something like:
24309 template <class T> void S<T>::R<T>::f (); */
24310 if (parser->num_template_parameter_lists < num_templates)
24312 if (declarator && !current_function_decl)
24313 error_at (location, "specializing member %<%T::%E%> "
24314 "requires %<template<>%> syntax",
24315 declarator->u.id.qualifying_scope,
24316 declarator->u.id.unqualified_name);
24317 else if (declarator)
24318 error_at (location, "invalid declaration of %<%T::%E%>",
24319 declarator->u.id.qualifying_scope,
24320 declarator->u.id.unqualified_name);
24321 else
24322 error_at (location, "too few template-parameter-lists");
24323 return false;
24325 /* Otherwise, there are too many template parameter lists. We have
24326 something like:
24328 template <class T> template <class U> void S::f(); */
24329 error_at (location, "too many template-parameter-lists");
24330 return false;
24333 /* Parse an optional `::' token indicating that the following name is
24334 from the global namespace. If so, PARSER->SCOPE is set to the
24335 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
24336 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
24337 Returns the new value of PARSER->SCOPE, if the `::' token is
24338 present, and NULL_TREE otherwise. */
24340 static tree
24341 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
24343 cp_token *token;
24345 /* Peek at the next token. */
24346 token = cp_lexer_peek_token (parser->lexer);
24347 /* If we're looking at a `::' token then we're starting from the
24348 global namespace, not our current location. */
24349 if (token->type == CPP_SCOPE)
24351 /* Consume the `::' token. */
24352 cp_lexer_consume_token (parser->lexer);
24353 /* Set the SCOPE so that we know where to start the lookup. */
24354 parser->scope = global_namespace;
24355 parser->qualifying_scope = global_namespace;
24356 parser->object_scope = NULL_TREE;
24358 return parser->scope;
24360 else if (!current_scope_valid_p)
24362 parser->scope = NULL_TREE;
24363 parser->qualifying_scope = NULL_TREE;
24364 parser->object_scope = NULL_TREE;
24367 return NULL_TREE;
24370 /* Returns TRUE if the upcoming token sequence is the start of a
24371 constructor declarator. If FRIEND_P is true, the declarator is
24372 preceded by the `friend' specifier. */
24374 static bool
24375 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
24377 bool constructor_p;
24378 bool outside_class_specifier_p;
24379 tree nested_name_specifier;
24380 cp_token *next_token;
24382 /* The common case is that this is not a constructor declarator, so
24383 try to avoid doing lots of work if at all possible. It's not
24384 valid declare a constructor at function scope. */
24385 if (parser->in_function_body)
24386 return false;
24387 /* And only certain tokens can begin a constructor declarator. */
24388 next_token = cp_lexer_peek_token (parser->lexer);
24389 if (next_token->type != CPP_NAME
24390 && next_token->type != CPP_SCOPE
24391 && next_token->type != CPP_NESTED_NAME_SPECIFIER
24392 && next_token->type != CPP_TEMPLATE_ID)
24393 return false;
24395 /* Parse tentatively; we are going to roll back all of the tokens
24396 consumed here. */
24397 cp_parser_parse_tentatively (parser);
24398 /* Assume that we are looking at a constructor declarator. */
24399 constructor_p = true;
24401 /* Look for the optional `::' operator. */
24402 cp_parser_global_scope_opt (parser,
24403 /*current_scope_valid_p=*/false);
24404 /* Look for the nested-name-specifier. */
24405 nested_name_specifier
24406 = (cp_parser_nested_name_specifier_opt (parser,
24407 /*typename_keyword_p=*/false,
24408 /*check_dependency_p=*/false,
24409 /*type_p=*/false,
24410 /*is_declaration=*/false));
24412 outside_class_specifier_p = (!at_class_scope_p ()
24413 || !TYPE_BEING_DEFINED (current_class_type)
24414 || friend_p);
24416 /* Outside of a class-specifier, there must be a
24417 nested-name-specifier. */
24418 if (!nested_name_specifier && outside_class_specifier_p)
24419 constructor_p = false;
24420 else if (nested_name_specifier == error_mark_node)
24421 constructor_p = false;
24423 /* If we have a class scope, this is easy; DR 147 says that S::S always
24424 names the constructor, and no other qualified name could. */
24425 if (constructor_p && nested_name_specifier
24426 && CLASS_TYPE_P (nested_name_specifier))
24428 tree id = cp_parser_unqualified_id (parser,
24429 /*template_keyword_p=*/false,
24430 /*check_dependency_p=*/false,
24431 /*declarator_p=*/true,
24432 /*optional_p=*/false);
24433 if (is_overloaded_fn (id))
24434 id = DECL_NAME (get_first_fn (id));
24435 if (!constructor_name_p (id, nested_name_specifier))
24436 constructor_p = false;
24438 /* If we still think that this might be a constructor-declarator,
24439 look for a class-name. */
24440 else if (constructor_p)
24442 /* If we have:
24444 template <typename T> struct S {
24445 S();
24448 we must recognize that the nested `S' names a class. */
24449 tree type_decl;
24450 type_decl = cp_parser_class_name (parser,
24451 /*typename_keyword_p=*/false,
24452 /*template_keyword_p=*/false,
24453 none_type,
24454 /*check_dependency_p=*/false,
24455 /*class_head_p=*/false,
24456 /*is_declaration=*/false);
24457 /* If there was no class-name, then this is not a constructor.
24458 Otherwise, if we are in a class-specifier and we aren't
24459 handling a friend declaration, check that its type matches
24460 current_class_type (c++/38313). Note: error_mark_node
24461 is left alone for error recovery purposes. */
24462 constructor_p = (!cp_parser_error_occurred (parser)
24463 && (outside_class_specifier_p
24464 || type_decl == error_mark_node
24465 || same_type_p (current_class_type,
24466 TREE_TYPE (type_decl))));
24468 /* If we're still considering a constructor, we have to see a `(',
24469 to begin the parameter-declaration-clause, followed by either a
24470 `)', an `...', or a decl-specifier. We need to check for a
24471 type-specifier to avoid being fooled into thinking that:
24473 S (f) (int);
24475 is a constructor. (It is actually a function named `f' that
24476 takes one parameter (of type `int') and returns a value of type
24477 `S'. */
24478 if (constructor_p
24479 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24480 constructor_p = false;
24482 if (constructor_p
24483 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
24484 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
24485 /* A parameter declaration begins with a decl-specifier,
24486 which is either the "attribute" keyword, a storage class
24487 specifier, or (usually) a type-specifier. */
24488 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
24490 tree type;
24491 tree pushed_scope = NULL_TREE;
24492 unsigned saved_num_template_parameter_lists;
24494 /* Names appearing in the type-specifier should be looked up
24495 in the scope of the class. */
24496 if (current_class_type)
24497 type = NULL_TREE;
24498 else
24500 type = TREE_TYPE (type_decl);
24501 if (TREE_CODE (type) == TYPENAME_TYPE)
24503 type = resolve_typename_type (type,
24504 /*only_current_p=*/false);
24505 if (TREE_CODE (type) == TYPENAME_TYPE)
24507 cp_parser_abort_tentative_parse (parser);
24508 return false;
24511 pushed_scope = push_scope (type);
24514 /* Inside the constructor parameter list, surrounding
24515 template-parameter-lists do not apply. */
24516 saved_num_template_parameter_lists
24517 = parser->num_template_parameter_lists;
24518 parser->num_template_parameter_lists = 0;
24520 /* Look for the type-specifier. */
24521 cp_parser_type_specifier (parser,
24522 CP_PARSER_FLAGS_NONE,
24523 /*decl_specs=*/NULL,
24524 /*is_declarator=*/true,
24525 /*declares_class_or_enum=*/NULL,
24526 /*is_cv_qualifier=*/NULL);
24528 parser->num_template_parameter_lists
24529 = saved_num_template_parameter_lists;
24531 /* Leave the scope of the class. */
24532 if (pushed_scope)
24533 pop_scope (pushed_scope);
24535 constructor_p = !cp_parser_error_occurred (parser);
24539 /* We did not really want to consume any tokens. */
24540 cp_parser_abort_tentative_parse (parser);
24542 return constructor_p;
24545 /* Parse the definition of the function given by the DECL_SPECIFIERS,
24546 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
24547 they must be performed once we are in the scope of the function.
24549 Returns the function defined. */
24551 static tree
24552 cp_parser_function_definition_from_specifiers_and_declarator
24553 (cp_parser* parser,
24554 cp_decl_specifier_seq *decl_specifiers,
24555 tree attributes,
24556 const cp_declarator *declarator)
24558 tree fn;
24559 bool success_p;
24561 /* Begin the function-definition. */
24562 success_p = start_function (decl_specifiers, declarator, attributes);
24564 /* The things we're about to see are not directly qualified by any
24565 template headers we've seen thus far. */
24566 reset_specialization ();
24568 /* If there were names looked up in the decl-specifier-seq that we
24569 did not check, check them now. We must wait until we are in the
24570 scope of the function to perform the checks, since the function
24571 might be a friend. */
24572 perform_deferred_access_checks (tf_warning_or_error);
24574 if (success_p)
24576 cp_finalize_omp_declare_simd (parser, current_function_decl);
24577 parser->omp_declare_simd = NULL;
24580 if (!success_p)
24582 /* Skip the entire function. */
24583 cp_parser_skip_to_end_of_block_or_statement (parser);
24584 fn = error_mark_node;
24586 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
24588 /* Seen already, skip it. An error message has already been output. */
24589 cp_parser_skip_to_end_of_block_or_statement (parser);
24590 fn = current_function_decl;
24591 current_function_decl = NULL_TREE;
24592 /* If this is a function from a class, pop the nested class. */
24593 if (current_class_name)
24594 pop_nested_class ();
24596 else
24598 timevar_id_t tv;
24599 if (DECL_DECLARED_INLINE_P (current_function_decl))
24600 tv = TV_PARSE_INLINE;
24601 else
24602 tv = TV_PARSE_FUNC;
24603 timevar_push (tv);
24604 fn = cp_parser_function_definition_after_declarator (parser,
24605 /*inline_p=*/false);
24606 timevar_pop (tv);
24609 return fn;
24612 /* Parse the part of a function-definition that follows the
24613 declarator. INLINE_P is TRUE iff this function is an inline
24614 function defined within a class-specifier.
24616 Returns the function defined. */
24618 static tree
24619 cp_parser_function_definition_after_declarator (cp_parser* parser,
24620 bool inline_p)
24622 tree fn;
24623 bool ctor_initializer_p = false;
24624 bool saved_in_unbraced_linkage_specification_p;
24625 bool saved_in_function_body;
24626 unsigned saved_num_template_parameter_lists;
24627 cp_token *token;
24628 bool fully_implicit_function_template_p
24629 = parser->fully_implicit_function_template_p;
24630 parser->fully_implicit_function_template_p = false;
24631 tree implicit_template_parms
24632 = parser->implicit_template_parms;
24633 parser->implicit_template_parms = 0;
24634 cp_binding_level* implicit_template_scope
24635 = parser->implicit_template_scope;
24636 parser->implicit_template_scope = 0;
24638 saved_in_function_body = parser->in_function_body;
24639 parser->in_function_body = true;
24640 /* If the next token is `return', then the code may be trying to
24641 make use of the "named return value" extension that G++ used to
24642 support. */
24643 token = cp_lexer_peek_token (parser->lexer);
24644 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
24646 /* Consume the `return' keyword. */
24647 cp_lexer_consume_token (parser->lexer);
24648 /* Look for the identifier that indicates what value is to be
24649 returned. */
24650 cp_parser_identifier (parser);
24651 /* Issue an error message. */
24652 error_at (token->location,
24653 "named return values are no longer supported");
24654 /* Skip tokens until we reach the start of the function body. */
24655 while (true)
24657 cp_token *token = cp_lexer_peek_token (parser->lexer);
24658 if (token->type == CPP_OPEN_BRACE
24659 || token->type == CPP_EOF
24660 || token->type == CPP_PRAGMA_EOL)
24661 break;
24662 cp_lexer_consume_token (parser->lexer);
24665 /* The `extern' in `extern "C" void f () { ... }' does not apply to
24666 anything declared inside `f'. */
24667 saved_in_unbraced_linkage_specification_p
24668 = parser->in_unbraced_linkage_specification_p;
24669 parser->in_unbraced_linkage_specification_p = false;
24670 /* Inside the function, surrounding template-parameter-lists do not
24671 apply. */
24672 saved_num_template_parameter_lists
24673 = parser->num_template_parameter_lists;
24674 parser->num_template_parameter_lists = 0;
24676 start_lambda_scope (current_function_decl);
24678 /* If the next token is `try', `__transaction_atomic', or
24679 `__transaction_relaxed`, then we are looking at either function-try-block
24680 or function-transaction-block. Note that all of these include the
24681 function-body. */
24682 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
24683 ctor_initializer_p = cp_parser_function_transaction (parser,
24684 RID_TRANSACTION_ATOMIC);
24685 else if (cp_lexer_next_token_is_keyword (parser->lexer,
24686 RID_TRANSACTION_RELAXED))
24687 ctor_initializer_p = cp_parser_function_transaction (parser,
24688 RID_TRANSACTION_RELAXED);
24689 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
24690 ctor_initializer_p = cp_parser_function_try_block (parser);
24691 else
24692 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
24693 (parser, /*in_function_try_block=*/false);
24695 finish_lambda_scope ();
24697 /* Finish the function. */
24698 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
24699 (inline_p ? 2 : 0));
24700 /* Generate code for it, if necessary. */
24701 expand_or_defer_fn (fn);
24702 /* Restore the saved values. */
24703 parser->in_unbraced_linkage_specification_p
24704 = saved_in_unbraced_linkage_specification_p;
24705 parser->num_template_parameter_lists
24706 = saved_num_template_parameter_lists;
24707 parser->in_function_body = saved_in_function_body;
24709 parser->fully_implicit_function_template_p
24710 = fully_implicit_function_template_p;
24711 parser->implicit_template_parms
24712 = implicit_template_parms;
24713 parser->implicit_template_scope
24714 = implicit_template_scope;
24716 if (parser->fully_implicit_function_template_p)
24717 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
24719 return fn;
24722 /* Parse a template-declaration body (following argument list). */
24724 static void
24725 cp_parser_template_declaration_after_parameters (cp_parser* parser,
24726 tree parameter_list,
24727 bool member_p)
24729 tree decl = NULL_TREE;
24730 bool friend_p = false;
24732 /* We just processed one more parameter list. */
24733 ++parser->num_template_parameter_lists;
24735 /* Get the deferred access checks from the parameter list. These
24736 will be checked once we know what is being declared, as for a
24737 member template the checks must be performed in the scope of the
24738 class containing the member. */
24739 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
24741 /* Tentatively parse for a new template parameter list, which can either be
24742 the template keyword or a template introduction. */
24743 if (cp_parser_template_declaration_after_export (parser, member_p))
24744 /* OK */;
24745 else if (cxx_dialect >= cxx11
24746 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
24747 decl = cp_parser_alias_declaration (parser);
24748 else
24750 /* There are no access checks when parsing a template, as we do not
24751 know if a specialization will be a friend. */
24752 push_deferring_access_checks (dk_no_check);
24753 cp_token *token = cp_lexer_peek_token (parser->lexer);
24754 decl = cp_parser_single_declaration (parser,
24755 checks,
24756 member_p,
24757 /*explicit_specialization_p=*/false,
24758 &friend_p);
24759 pop_deferring_access_checks ();
24761 /* If this is a member template declaration, let the front
24762 end know. */
24763 if (member_p && !friend_p && decl)
24765 if (TREE_CODE (decl) == TYPE_DECL)
24766 cp_parser_check_access_in_redeclaration (decl, token->location);
24768 decl = finish_member_template_decl (decl);
24770 else if (friend_p && decl
24771 && DECL_DECLARES_TYPE_P (decl))
24772 make_friend_class (current_class_type, TREE_TYPE (decl),
24773 /*complain=*/true);
24775 /* We are done with the current parameter list. */
24776 --parser->num_template_parameter_lists;
24778 pop_deferring_access_checks ();
24780 /* Finish up. */
24781 finish_template_decl (parameter_list);
24783 /* Check the template arguments for a literal operator template. */
24784 if (decl
24785 && DECL_DECLARES_FUNCTION_P (decl)
24786 && UDLIT_OPER_P (DECL_NAME (decl)))
24788 bool ok = true;
24789 if (parameter_list == NULL_TREE)
24790 ok = false;
24791 else
24793 int num_parms = TREE_VEC_LENGTH (parameter_list);
24794 if (num_parms == 1)
24796 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
24797 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
24798 if (TREE_TYPE (parm) != char_type_node
24799 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
24800 ok = false;
24802 else if (num_parms == 2 && cxx_dialect >= cxx14)
24804 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
24805 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
24806 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
24807 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
24808 if (TREE_TYPE (parm) != TREE_TYPE (type)
24809 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
24810 ok = false;
24812 else
24813 ok = false;
24815 if (!ok)
24817 if (cxx_dialect >= cxx14)
24818 error ("literal operator template %qD has invalid parameter list."
24819 " Expected non-type template argument pack <char...>"
24820 " or <typename CharT, CharT...>",
24821 decl);
24822 else
24823 error ("literal operator template %qD has invalid parameter list."
24824 " Expected non-type template argument pack <char...>",
24825 decl);
24829 /* Register member declarations. */
24830 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
24831 finish_member_declaration (decl);
24832 /* If DECL is a function template, we must return to parse it later.
24833 (Even though there is no definition, there might be default
24834 arguments that need handling.) */
24835 if (member_p && decl
24836 && DECL_DECLARES_FUNCTION_P (decl))
24837 vec_safe_push (unparsed_funs_with_definitions, decl);
24840 /* Parse a template introduction header for a template-declaration. Returns
24841 false if tentative parse fails. */
24843 static bool
24844 cp_parser_template_introduction (cp_parser* parser, bool member_p)
24846 cp_parser_parse_tentatively (parser);
24848 tree saved_scope = parser->scope;
24849 tree saved_object_scope = parser->object_scope;
24850 tree saved_qualifying_scope = parser->qualifying_scope;
24852 /* Look for the optional `::' operator. */
24853 cp_parser_global_scope_opt (parser,
24854 /*current_scope_valid_p=*/false);
24855 /* Look for the nested-name-specifier. */
24856 cp_parser_nested_name_specifier_opt (parser,
24857 /*typename_keyword_p=*/false,
24858 /*check_dependency_p=*/true,
24859 /*type_p=*/false,
24860 /*is_declaration=*/false);
24862 cp_token *token = cp_lexer_peek_token (parser->lexer);
24863 tree concept_name = cp_parser_identifier (parser);
24865 /* Look up the concept for which we will be matching
24866 template parameters. */
24867 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
24868 token->location);
24869 parser->scope = saved_scope;
24870 parser->object_scope = saved_object_scope;
24871 parser->qualifying_scope = saved_qualifying_scope;
24873 if (concept_name == error_mark_node)
24874 cp_parser_simulate_error (parser);
24876 /* Look for opening brace for introduction. */
24877 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
24879 if (!cp_parser_parse_definitely (parser))
24880 return false;
24882 push_deferring_access_checks (dk_deferred);
24884 /* Build vector of placeholder parameters and grab
24885 matching identifiers. */
24886 tree introduction_list = cp_parser_introduction_list (parser);
24888 /* The introduction-list shall not be empty. */
24889 int nargs = TREE_VEC_LENGTH (introduction_list);
24890 if (nargs == 0)
24892 error ("empty introduction-list");
24893 return true;
24896 /* Look for closing brace for introduction. */
24897 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24898 return true;
24900 if (tmpl_decl == error_mark_node)
24902 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
24903 token->location);
24904 return true;
24907 /* Build and associate the constraint. */
24908 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
24909 if (parms && parms != error_mark_node)
24911 cp_parser_template_declaration_after_parameters (parser, parms,
24912 member_p);
24913 return true;
24916 error_at (token->location, "no matching concept for template-introduction");
24917 return true;
24920 /* Parse a normal template-declaration following the template keyword. */
24922 static void
24923 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
24925 tree parameter_list;
24926 bool need_lang_pop;
24927 location_t location = input_location;
24929 /* Look for the `<' token. */
24930 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
24931 return;
24932 if (at_class_scope_p () && current_function_decl)
24934 /* 14.5.2.2 [temp.mem]
24936 A local class shall not have member templates. */
24937 error_at (location,
24938 "invalid declaration of member template in local class");
24939 cp_parser_skip_to_end_of_block_or_statement (parser);
24940 return;
24942 /* [temp]
24944 A template ... shall not have C linkage. */
24945 if (current_lang_name == lang_name_c)
24947 error_at (location, "template with C linkage");
24948 /* Give it C++ linkage to avoid confusing other parts of the
24949 front end. */
24950 push_lang_context (lang_name_cplusplus);
24951 need_lang_pop = true;
24953 else
24954 need_lang_pop = false;
24956 /* We cannot perform access checks on the template parameter
24957 declarations until we know what is being declared, just as we
24958 cannot check the decl-specifier list. */
24959 push_deferring_access_checks (dk_deferred);
24961 /* If the next token is `>', then we have an invalid
24962 specialization. Rather than complain about an invalid template
24963 parameter, issue an error message here. */
24964 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
24966 cp_parser_error (parser, "invalid explicit specialization");
24967 begin_specialization ();
24968 parameter_list = NULL_TREE;
24970 else
24972 /* Parse the template parameters. */
24973 parameter_list = cp_parser_template_parameter_list (parser);
24976 /* Look for the `>'. */
24977 cp_parser_skip_to_end_of_template_parameter_list (parser);
24979 /* Manage template requirements */
24980 if (flag_concepts)
24982 tree reqs = get_shorthand_constraints (current_template_parms);
24983 if (tree r = cp_parser_requires_clause_opt (parser))
24984 reqs = conjoin_constraints (reqs, make_predicate_constraint (r));
24985 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
24988 cp_parser_template_declaration_after_parameters (parser, parameter_list,
24989 member_p);
24991 /* For the erroneous case of a template with C linkage, we pushed an
24992 implicit C++ linkage scope; exit that scope now. */
24993 if (need_lang_pop)
24994 pop_lang_context ();
24997 /* Parse a template-declaration, assuming that the `export' (and
24998 `extern') keywords, if present, has already been scanned. MEMBER_P
24999 is as for cp_parser_template_declaration. */
25001 static bool
25002 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
25004 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25006 cp_lexer_consume_token (parser->lexer);
25007 cp_parser_explicit_template_declaration (parser, member_p);
25008 return true;
25010 else if (flag_concepts)
25011 return cp_parser_template_introduction (parser, member_p);
25013 return false;
25016 /* Perform the deferred access checks from a template-parameter-list.
25017 CHECKS is a TREE_LIST of access checks, as returned by
25018 get_deferred_access_checks. */
25020 static void
25021 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
25023 ++processing_template_parmlist;
25024 perform_access_checks (checks, tf_warning_or_error);
25025 --processing_template_parmlist;
25028 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
25029 `function-definition' sequence that follows a template header.
25030 If MEMBER_P is true, this declaration appears in a class scope.
25032 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
25033 *FRIEND_P is set to TRUE iff the declaration is a friend. */
25035 static tree
25036 cp_parser_single_declaration (cp_parser* parser,
25037 vec<deferred_access_check, va_gc> *checks,
25038 bool member_p,
25039 bool explicit_specialization_p,
25040 bool* friend_p)
25042 int declares_class_or_enum;
25043 tree decl = NULL_TREE;
25044 cp_decl_specifier_seq decl_specifiers;
25045 bool function_definition_p = false;
25046 cp_token *decl_spec_token_start;
25048 /* This function is only used when processing a template
25049 declaration. */
25050 gcc_assert (innermost_scope_kind () == sk_template_parms
25051 || innermost_scope_kind () == sk_template_spec);
25053 /* Defer access checks until we know what is being declared. */
25054 push_deferring_access_checks (dk_deferred);
25056 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
25057 alternative. */
25058 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
25059 cp_parser_decl_specifier_seq (parser,
25060 CP_PARSER_FLAGS_OPTIONAL,
25061 &decl_specifiers,
25062 &declares_class_or_enum);
25063 if (friend_p)
25064 *friend_p = cp_parser_friend_p (&decl_specifiers);
25066 /* There are no template typedefs. */
25067 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
25069 error_at (decl_spec_token_start->location,
25070 "template declaration of %<typedef%>");
25071 decl = error_mark_node;
25074 /* Gather up the access checks that occurred the
25075 decl-specifier-seq. */
25076 stop_deferring_access_checks ();
25078 /* Check for the declaration of a template class. */
25079 if (declares_class_or_enum)
25081 if (cp_parser_declares_only_class_p (parser))
25083 // If this is a declaration, but not a definition, associate
25084 // any constraints with the type declaration. Constraints
25085 // are associated with definitions in cp_parser_class_specifier.
25086 if (declares_class_or_enum == 1)
25087 associate_classtype_constraints (decl_specifiers.type);
25089 decl = shadow_tag (&decl_specifiers);
25091 /* In this case:
25093 struct C {
25094 friend template <typename T> struct A<T>::B;
25097 A<T>::B will be represented by a TYPENAME_TYPE, and
25098 therefore not recognized by shadow_tag. */
25099 if (friend_p && *friend_p
25100 && !decl
25101 && decl_specifiers.type
25102 && TYPE_P (decl_specifiers.type))
25103 decl = decl_specifiers.type;
25105 if (decl && decl != error_mark_node)
25106 decl = TYPE_NAME (decl);
25107 else
25108 decl = error_mark_node;
25110 /* Perform access checks for template parameters. */
25111 cp_parser_perform_template_parameter_access_checks (checks);
25115 /* Complain about missing 'typename' or other invalid type names. */
25116 if (!decl_specifiers.any_type_specifiers_p
25117 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
25119 /* cp_parser_parse_and_diagnose_invalid_type_name calls
25120 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
25121 the rest of this declaration. */
25122 decl = error_mark_node;
25123 goto out;
25126 /* If it's not a template class, try for a template function. If
25127 the next token is a `;', then this declaration does not declare
25128 anything. But, if there were errors in the decl-specifiers, then
25129 the error might well have come from an attempted class-specifier.
25130 In that case, there's no need to warn about a missing declarator. */
25131 if (!decl
25132 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
25133 || decl_specifiers.type != error_mark_node))
25135 decl = cp_parser_init_declarator (parser,
25136 &decl_specifiers,
25137 checks,
25138 /*function_definition_allowed_p=*/true,
25139 member_p,
25140 declares_class_or_enum,
25141 &function_definition_p,
25142 NULL, NULL);
25144 /* 7.1.1-1 [dcl.stc]
25146 A storage-class-specifier shall not be specified in an explicit
25147 specialization... */
25148 if (decl
25149 && explicit_specialization_p
25150 && decl_specifiers.storage_class != sc_none)
25152 error_at (decl_spec_token_start->location,
25153 "explicit template specialization cannot have a storage class");
25154 decl = error_mark_node;
25157 if (decl && VAR_P (decl))
25158 check_template_variable (decl);
25161 /* Look for a trailing `;' after the declaration. */
25162 if (!function_definition_p
25163 && (decl == error_mark_node
25164 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
25165 cp_parser_skip_to_end_of_block_or_statement (parser);
25167 out:
25168 pop_deferring_access_checks ();
25170 /* Clear any current qualification; whatever comes next is the start
25171 of something new. */
25172 parser->scope = NULL_TREE;
25173 parser->qualifying_scope = NULL_TREE;
25174 parser->object_scope = NULL_TREE;
25176 return decl;
25179 /* Parse a cast-expression that is not the operand of a unary "&". */
25181 static tree
25182 cp_parser_simple_cast_expression (cp_parser *parser)
25184 return cp_parser_cast_expression (parser, /*address_p=*/false,
25185 /*cast_p=*/false, /*decltype*/false, NULL);
25188 /* Parse a functional cast to TYPE. Returns an expression
25189 representing the cast. */
25191 static tree
25192 cp_parser_functional_cast (cp_parser* parser, tree type)
25194 vec<tree, va_gc> *vec;
25195 tree expression_list;
25196 tree cast;
25197 bool nonconst_p;
25199 if (!type)
25200 type = error_mark_node;
25202 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25204 cp_lexer_set_source_position (parser->lexer);
25205 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25206 expression_list = cp_parser_braced_list (parser, &nonconst_p);
25207 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
25208 if (TREE_CODE (type) == TYPE_DECL)
25209 type = TREE_TYPE (type);
25210 return finish_compound_literal (type, expression_list,
25211 tf_warning_or_error);
25215 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
25216 /*cast_p=*/true,
25217 /*allow_expansion_p=*/true,
25218 /*non_constant_p=*/NULL);
25219 if (vec == NULL)
25220 expression_list = error_mark_node;
25221 else
25223 expression_list = build_tree_list_vec (vec);
25224 release_tree_vector (vec);
25227 cast = build_functional_cast (type, expression_list,
25228 tf_warning_or_error);
25229 /* [expr.const]/1: In an integral constant expression "only type
25230 conversions to integral or enumeration type can be used". */
25231 if (TREE_CODE (type) == TYPE_DECL)
25232 type = TREE_TYPE (type);
25233 if (cast != error_mark_node
25234 && !cast_valid_in_integral_constant_expression_p (type)
25235 && cp_parser_non_integral_constant_expression (parser,
25236 NIC_CONSTRUCTOR))
25237 return error_mark_node;
25238 return cast;
25241 /* Save the tokens that make up the body of a member function defined
25242 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
25243 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
25244 specifiers applied to the declaration. Returns the FUNCTION_DECL
25245 for the member function. */
25247 static tree
25248 cp_parser_save_member_function_body (cp_parser* parser,
25249 cp_decl_specifier_seq *decl_specifiers,
25250 cp_declarator *declarator,
25251 tree attributes)
25253 cp_token *first;
25254 cp_token *last;
25255 tree fn;
25257 /* Create the FUNCTION_DECL. */
25258 fn = grokmethod (decl_specifiers, declarator, attributes);
25259 cp_finalize_omp_declare_simd (parser, fn);
25260 /* If something went badly wrong, bail out now. */
25261 if (fn == error_mark_node)
25263 /* If there's a function-body, skip it. */
25264 if (cp_parser_token_starts_function_definition_p
25265 (cp_lexer_peek_token (parser->lexer)))
25266 cp_parser_skip_to_end_of_block_or_statement (parser);
25267 return error_mark_node;
25270 /* Remember it, if there default args to post process. */
25271 cp_parser_save_default_args (parser, fn);
25273 /* Save away the tokens that make up the body of the
25274 function. */
25275 first = parser->lexer->next_token;
25276 /* Handle function try blocks. */
25277 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
25278 cp_lexer_consume_token (parser->lexer);
25279 /* We can have braced-init-list mem-initializers before the fn body. */
25280 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
25282 cp_lexer_consume_token (parser->lexer);
25283 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
25285 /* cache_group will stop after an un-nested { } pair, too. */
25286 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
25287 break;
25289 /* variadic mem-inits have ... after the ')'. */
25290 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25291 cp_lexer_consume_token (parser->lexer);
25294 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25295 /* Handle function try blocks. */
25296 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
25297 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25298 last = parser->lexer->next_token;
25300 /* Save away the inline definition; we will process it when the
25301 class is complete. */
25302 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
25303 DECL_PENDING_INLINE_P (fn) = 1;
25305 /* We need to know that this was defined in the class, so that
25306 friend templates are handled correctly. */
25307 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
25309 /* Add FN to the queue of functions to be parsed later. */
25310 vec_safe_push (unparsed_funs_with_definitions, fn);
25312 return fn;
25315 /* Save the tokens that make up the in-class initializer for a non-static
25316 data member. Returns a DEFAULT_ARG. */
25318 static tree
25319 cp_parser_save_nsdmi (cp_parser* parser)
25321 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
25324 /* Parse a template-argument-list, as well as the trailing ">" (but
25325 not the opening "<"). See cp_parser_template_argument_list for the
25326 return value. */
25328 static tree
25329 cp_parser_enclosed_template_argument_list (cp_parser* parser)
25331 tree arguments;
25332 tree saved_scope;
25333 tree saved_qualifying_scope;
25334 tree saved_object_scope;
25335 bool saved_greater_than_is_operator_p;
25336 int saved_unevaluated_operand;
25337 int saved_inhibit_evaluation_warnings;
25339 /* [temp.names]
25341 When parsing a template-id, the first non-nested `>' is taken as
25342 the end of the template-argument-list rather than a greater-than
25343 operator. */
25344 saved_greater_than_is_operator_p
25345 = parser->greater_than_is_operator_p;
25346 parser->greater_than_is_operator_p = false;
25347 /* Parsing the argument list may modify SCOPE, so we save it
25348 here. */
25349 saved_scope = parser->scope;
25350 saved_qualifying_scope = parser->qualifying_scope;
25351 saved_object_scope = parser->object_scope;
25352 /* We need to evaluate the template arguments, even though this
25353 template-id may be nested within a "sizeof". */
25354 saved_unevaluated_operand = cp_unevaluated_operand;
25355 cp_unevaluated_operand = 0;
25356 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
25357 c_inhibit_evaluation_warnings = 0;
25358 /* Parse the template-argument-list itself. */
25359 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
25360 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
25361 arguments = NULL_TREE;
25362 else
25363 arguments = cp_parser_template_argument_list (parser);
25364 /* Look for the `>' that ends the template-argument-list. If we find
25365 a '>>' instead, it's probably just a typo. */
25366 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
25368 if (cxx_dialect != cxx98)
25370 /* In C++0x, a `>>' in a template argument list or cast
25371 expression is considered to be two separate `>'
25372 tokens. So, change the current token to a `>', but don't
25373 consume it: it will be consumed later when the outer
25374 template argument list (or cast expression) is parsed.
25375 Note that this replacement of `>' for `>>' is necessary
25376 even if we are parsing tentatively: in the tentative
25377 case, after calling
25378 cp_parser_enclosed_template_argument_list we will always
25379 throw away all of the template arguments and the first
25380 closing `>', either because the template argument list
25381 was erroneous or because we are replacing those tokens
25382 with a CPP_TEMPLATE_ID token. The second `>' (which will
25383 not have been thrown away) is needed either to close an
25384 outer template argument list or to complete a new-style
25385 cast. */
25386 cp_token *token = cp_lexer_peek_token (parser->lexer);
25387 token->type = CPP_GREATER;
25389 else if (!saved_greater_than_is_operator_p)
25391 /* If we're in a nested template argument list, the '>>' has
25392 to be a typo for '> >'. We emit the error message, but we
25393 continue parsing and we push a '>' as next token, so that
25394 the argument list will be parsed correctly. Note that the
25395 global source location is still on the token before the
25396 '>>', so we need to say explicitly where we want it. */
25397 cp_token *token = cp_lexer_peek_token (parser->lexer);
25398 error_at (token->location, "%<>>%> should be %<> >%> "
25399 "within a nested template argument list");
25401 token->type = CPP_GREATER;
25403 else
25405 /* If this is not a nested template argument list, the '>>'
25406 is a typo for '>'. Emit an error message and continue.
25407 Same deal about the token location, but here we can get it
25408 right by consuming the '>>' before issuing the diagnostic. */
25409 cp_token *token = cp_lexer_consume_token (parser->lexer);
25410 error_at (token->location,
25411 "spurious %<>>%>, use %<>%> to terminate "
25412 "a template argument list");
25415 else
25416 cp_parser_skip_to_end_of_template_parameter_list (parser);
25417 /* The `>' token might be a greater-than operator again now. */
25418 parser->greater_than_is_operator_p
25419 = saved_greater_than_is_operator_p;
25420 /* Restore the SAVED_SCOPE. */
25421 parser->scope = saved_scope;
25422 parser->qualifying_scope = saved_qualifying_scope;
25423 parser->object_scope = saved_object_scope;
25424 cp_unevaluated_operand = saved_unevaluated_operand;
25425 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
25427 return arguments;
25430 /* MEMBER_FUNCTION is a member function, or a friend. If default
25431 arguments, or the body of the function have not yet been parsed,
25432 parse them now. */
25434 static void
25435 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
25437 timevar_push (TV_PARSE_INMETH);
25438 /* If this member is a template, get the underlying
25439 FUNCTION_DECL. */
25440 if (DECL_FUNCTION_TEMPLATE_P (member_function))
25441 member_function = DECL_TEMPLATE_RESULT (member_function);
25443 /* There should not be any class definitions in progress at this
25444 point; the bodies of members are only parsed outside of all class
25445 definitions. */
25446 gcc_assert (parser->num_classes_being_defined == 0);
25447 /* While we're parsing the member functions we might encounter more
25448 classes. We want to handle them right away, but we don't want
25449 them getting mixed up with functions that are currently in the
25450 queue. */
25451 push_unparsed_function_queues (parser);
25453 /* Make sure that any template parameters are in scope. */
25454 maybe_begin_member_template_processing (member_function);
25456 /* If the body of the function has not yet been parsed, parse it
25457 now. */
25458 if (DECL_PENDING_INLINE_P (member_function))
25460 tree function_scope;
25461 cp_token_cache *tokens;
25463 /* The function is no longer pending; we are processing it. */
25464 tokens = DECL_PENDING_INLINE_INFO (member_function);
25465 DECL_PENDING_INLINE_INFO (member_function) = NULL;
25466 DECL_PENDING_INLINE_P (member_function) = 0;
25468 /* If this is a local class, enter the scope of the containing
25469 function. */
25470 function_scope = current_function_decl;
25471 if (function_scope)
25472 push_function_context ();
25474 /* Push the body of the function onto the lexer stack. */
25475 cp_parser_push_lexer_for_tokens (parser, tokens);
25477 /* Let the front end know that we going to be defining this
25478 function. */
25479 start_preparsed_function (member_function, NULL_TREE,
25480 SF_PRE_PARSED | SF_INCLASS_INLINE);
25482 /* Don't do access checking if it is a templated function. */
25483 if (processing_template_decl)
25484 push_deferring_access_checks (dk_no_check);
25486 /* #pragma omp declare reduction needs special parsing. */
25487 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
25489 parser->lexer->in_pragma = true;
25490 cp_parser_omp_declare_reduction_exprs (member_function, parser);
25491 finish_function (/*inline*/2);
25492 cp_check_omp_declare_reduction (member_function);
25494 else
25495 /* Now, parse the body of the function. */
25496 cp_parser_function_definition_after_declarator (parser,
25497 /*inline_p=*/true);
25499 if (processing_template_decl)
25500 pop_deferring_access_checks ();
25502 /* Leave the scope of the containing function. */
25503 if (function_scope)
25504 pop_function_context ();
25505 cp_parser_pop_lexer (parser);
25508 /* Remove any template parameters from the symbol table. */
25509 maybe_end_member_template_processing ();
25511 /* Restore the queue. */
25512 pop_unparsed_function_queues (parser);
25513 timevar_pop (TV_PARSE_INMETH);
25516 /* If DECL contains any default args, remember it on the unparsed
25517 functions queue. */
25519 static void
25520 cp_parser_save_default_args (cp_parser* parser, tree decl)
25522 tree probe;
25524 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
25525 probe;
25526 probe = TREE_CHAIN (probe))
25527 if (TREE_PURPOSE (probe))
25529 cp_default_arg_entry entry = {current_class_type, decl};
25530 vec_safe_push (unparsed_funs_with_default_args, entry);
25531 break;
25535 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
25536 which is either a FIELD_DECL or PARM_DECL. Parse it and return
25537 the result. For a PARM_DECL, PARMTYPE is the corresponding type
25538 from the parameter-type-list. */
25540 static tree
25541 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
25542 tree default_arg, tree parmtype)
25544 cp_token_cache *tokens;
25545 tree parsed_arg;
25546 bool dummy;
25548 if (default_arg == error_mark_node)
25549 return error_mark_node;
25551 /* Push the saved tokens for the default argument onto the parser's
25552 lexer stack. */
25553 tokens = DEFARG_TOKENS (default_arg);
25554 cp_parser_push_lexer_for_tokens (parser, tokens);
25556 start_lambda_scope (decl);
25558 /* Parse the default argument. */
25559 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
25560 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
25561 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25563 finish_lambda_scope ();
25565 if (parsed_arg == error_mark_node)
25566 cp_parser_skip_to_end_of_statement (parser);
25568 if (!processing_template_decl)
25570 /* In a non-template class, check conversions now. In a template,
25571 we'll wait and instantiate these as needed. */
25572 if (TREE_CODE (decl) == PARM_DECL)
25573 parsed_arg = check_default_argument (parmtype, parsed_arg,
25574 tf_warning_or_error);
25575 else
25576 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
25579 /* If the token stream has not been completely used up, then
25580 there was extra junk after the end of the default
25581 argument. */
25582 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
25584 if (TREE_CODE (decl) == PARM_DECL)
25585 cp_parser_error (parser, "expected %<,%>");
25586 else
25587 cp_parser_error (parser, "expected %<;%>");
25590 /* Revert to the main lexer. */
25591 cp_parser_pop_lexer (parser);
25593 return parsed_arg;
25596 /* FIELD is a non-static data member with an initializer which we saved for
25597 later; parse it now. */
25599 static void
25600 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
25602 tree def;
25604 maybe_begin_member_template_processing (field);
25606 push_unparsed_function_queues (parser);
25607 def = cp_parser_late_parse_one_default_arg (parser, field,
25608 DECL_INITIAL (field),
25609 NULL_TREE);
25610 pop_unparsed_function_queues (parser);
25612 maybe_end_member_template_processing ();
25614 DECL_INITIAL (field) = def;
25617 /* FN is a FUNCTION_DECL which may contains a parameter with an
25618 unparsed DEFAULT_ARG. Parse the default args now. This function
25619 assumes that the current scope is the scope in which the default
25620 argument should be processed. */
25622 static void
25623 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
25625 bool saved_local_variables_forbidden_p;
25626 tree parm, parmdecl;
25628 /* While we're parsing the default args, we might (due to the
25629 statement expression extension) encounter more classes. We want
25630 to handle them right away, but we don't want them getting mixed
25631 up with default args that are currently in the queue. */
25632 push_unparsed_function_queues (parser);
25634 /* Local variable names (and the `this' keyword) may not appear
25635 in a default argument. */
25636 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
25637 parser->local_variables_forbidden_p = true;
25639 push_defarg_context (fn);
25641 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
25642 parmdecl = DECL_ARGUMENTS (fn);
25643 parm && parm != void_list_node;
25644 parm = TREE_CHAIN (parm),
25645 parmdecl = DECL_CHAIN (parmdecl))
25647 tree default_arg = TREE_PURPOSE (parm);
25648 tree parsed_arg;
25649 vec<tree, va_gc> *insts;
25650 tree copy;
25651 unsigned ix;
25653 if (!default_arg)
25654 continue;
25656 if (TREE_CODE (default_arg) != DEFAULT_ARG)
25657 /* This can happen for a friend declaration for a function
25658 already declared with default arguments. */
25659 continue;
25661 parsed_arg
25662 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
25663 default_arg,
25664 TREE_VALUE (parm));
25665 if (parsed_arg == error_mark_node)
25667 continue;
25670 TREE_PURPOSE (parm) = parsed_arg;
25672 /* Update any instantiations we've already created. */
25673 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
25674 vec_safe_iterate (insts, ix, &copy); ix++)
25675 TREE_PURPOSE (copy) = parsed_arg;
25678 pop_defarg_context ();
25680 /* Make sure no default arg is missing. */
25681 check_default_args (fn);
25683 /* Restore the state of local_variables_forbidden_p. */
25684 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
25686 /* Restore the queue. */
25687 pop_unparsed_function_queues (parser);
25690 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
25692 sizeof ... ( identifier )
25694 where the 'sizeof' token has already been consumed. */
25696 static tree
25697 cp_parser_sizeof_pack (cp_parser *parser)
25699 /* Consume the `...'. */
25700 cp_lexer_consume_token (parser->lexer);
25701 maybe_warn_variadic_templates ();
25703 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
25704 if (paren)
25705 cp_lexer_consume_token (parser->lexer);
25706 else
25707 permerror (cp_lexer_peek_token (parser->lexer)->location,
25708 "%<sizeof...%> argument must be surrounded by parentheses");
25710 cp_token *token = cp_lexer_peek_token (parser->lexer);
25711 tree name = cp_parser_identifier (parser);
25712 if (name == error_mark_node)
25713 return error_mark_node;
25714 /* The name is not qualified. */
25715 parser->scope = NULL_TREE;
25716 parser->qualifying_scope = NULL_TREE;
25717 parser->object_scope = NULL_TREE;
25718 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
25719 if (expr == error_mark_node)
25720 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
25721 token->location);
25722 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
25723 expr = TREE_TYPE (expr);
25724 else if (TREE_CODE (expr) == CONST_DECL)
25725 expr = DECL_INITIAL (expr);
25726 expr = make_pack_expansion (expr);
25728 if (paren)
25729 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25731 return expr;
25734 /* Parse the operand of `sizeof' (or a similar operator). Returns
25735 either a TYPE or an expression, depending on the form of the
25736 input. The KEYWORD indicates which kind of expression we have
25737 encountered. */
25739 static tree
25740 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
25742 tree expr = NULL_TREE;
25743 const char *saved_message;
25744 char *tmp;
25745 bool saved_integral_constant_expression_p;
25746 bool saved_non_integral_constant_expression_p;
25748 /* If it's a `...', then we are computing the length of a parameter
25749 pack. */
25750 if (keyword == RID_SIZEOF
25751 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25752 return cp_parser_sizeof_pack (parser);
25754 /* Types cannot be defined in a `sizeof' expression. Save away the
25755 old message. */
25756 saved_message = parser->type_definition_forbidden_message;
25757 /* And create the new one. */
25758 tmp = concat ("types may not be defined in %<",
25759 IDENTIFIER_POINTER (ridpointers[keyword]),
25760 "%> expressions", NULL);
25761 parser->type_definition_forbidden_message = tmp;
25763 /* The restrictions on constant-expressions do not apply inside
25764 sizeof expressions. */
25765 saved_integral_constant_expression_p
25766 = parser->integral_constant_expression_p;
25767 saved_non_integral_constant_expression_p
25768 = parser->non_integral_constant_expression_p;
25769 parser->integral_constant_expression_p = false;
25771 /* Do not actually evaluate the expression. */
25772 ++cp_unevaluated_operand;
25773 ++c_inhibit_evaluation_warnings;
25774 /* If it's a `(', then we might be looking at the type-id
25775 construction. */
25776 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25778 tree type = NULL_TREE;
25780 /* We can't be sure yet whether we're looking at a type-id or an
25781 expression. */
25782 cp_parser_parse_tentatively (parser);
25783 /* Note: as a GNU Extension, compound literals are considered
25784 postfix-expressions as they are in C99, so they are valid
25785 arguments to sizeof. See comment in cp_parser_cast_expression
25786 for details. */
25787 if (cp_parser_compound_literal_p (parser))
25788 cp_parser_simulate_error (parser);
25789 else
25791 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
25792 parser->in_type_id_in_expr_p = true;
25793 /* Look for the type-id. */
25794 type = cp_parser_type_id (parser);
25795 /* Look for the closing `)'. */
25796 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25797 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
25800 /* If all went well, then we're done. */
25801 if (cp_parser_parse_definitely (parser))
25803 cp_decl_specifier_seq decl_specs;
25805 /* Build a trivial decl-specifier-seq. */
25806 clear_decl_specs (&decl_specs);
25807 decl_specs.type = type;
25809 /* Call grokdeclarator to figure out what type this is. */
25810 expr = grokdeclarator (NULL,
25811 &decl_specs,
25812 TYPENAME,
25813 /*initialized=*/0,
25814 /*attrlist=*/NULL);
25818 /* If the type-id production did not work out, then we must be
25819 looking at the unary-expression production. */
25820 if (!expr)
25821 expr = cp_parser_unary_expression (parser);
25823 /* Go back to evaluating expressions. */
25824 --cp_unevaluated_operand;
25825 --c_inhibit_evaluation_warnings;
25827 /* Free the message we created. */
25828 free (tmp);
25829 /* And restore the old one. */
25830 parser->type_definition_forbidden_message = saved_message;
25831 parser->integral_constant_expression_p
25832 = saved_integral_constant_expression_p;
25833 parser->non_integral_constant_expression_p
25834 = saved_non_integral_constant_expression_p;
25836 return expr;
25839 /* If the current declaration has no declarator, return true. */
25841 static bool
25842 cp_parser_declares_only_class_p (cp_parser *parser)
25844 /* If the next token is a `;' or a `,' then there is no
25845 declarator. */
25846 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25847 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
25850 /* Update the DECL_SPECS to reflect the storage class indicated by
25851 KEYWORD. */
25853 static void
25854 cp_parser_set_storage_class (cp_parser *parser,
25855 cp_decl_specifier_seq *decl_specs,
25856 enum rid keyword,
25857 cp_token *token)
25859 cp_storage_class storage_class;
25861 if (parser->in_unbraced_linkage_specification_p)
25863 error_at (token->location, "invalid use of %qD in linkage specification",
25864 ridpointers[keyword]);
25865 return;
25867 else if (decl_specs->storage_class != sc_none)
25869 decl_specs->conflicting_specifiers_p = true;
25870 return;
25873 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
25874 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
25875 && decl_specs->gnu_thread_keyword_p)
25877 pedwarn (decl_specs->locations[ds_thread], 0,
25878 "%<__thread%> before %qD", ridpointers[keyword]);
25881 switch (keyword)
25883 case RID_AUTO:
25884 storage_class = sc_auto;
25885 break;
25886 case RID_REGISTER:
25887 storage_class = sc_register;
25888 break;
25889 case RID_STATIC:
25890 storage_class = sc_static;
25891 break;
25892 case RID_EXTERN:
25893 storage_class = sc_extern;
25894 break;
25895 case RID_MUTABLE:
25896 storage_class = sc_mutable;
25897 break;
25898 default:
25899 gcc_unreachable ();
25901 decl_specs->storage_class = storage_class;
25902 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
25904 /* A storage class specifier cannot be applied alongside a typedef
25905 specifier. If there is a typedef specifier present then set
25906 conflicting_specifiers_p which will trigger an error later
25907 on in grokdeclarator. */
25908 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
25909 decl_specs->conflicting_specifiers_p = true;
25912 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
25913 is true, the type is a class or enum definition. */
25915 static void
25916 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
25917 tree type_spec,
25918 cp_token *token,
25919 bool type_definition_p)
25921 decl_specs->any_specifiers_p = true;
25923 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
25924 (with, for example, in "typedef int wchar_t;") we remember that
25925 this is what happened. In system headers, we ignore these
25926 declarations so that G++ can work with system headers that are not
25927 C++-safe. */
25928 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
25929 && !type_definition_p
25930 && (type_spec == boolean_type_node
25931 || type_spec == char16_type_node
25932 || type_spec == char32_type_node
25933 || type_spec == wchar_type_node)
25934 && (decl_specs->type
25935 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
25936 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
25937 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
25938 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
25940 decl_specs->redefined_builtin_type = type_spec;
25941 set_and_check_decl_spec_loc (decl_specs,
25942 ds_redefined_builtin_type_spec,
25943 token);
25944 if (!decl_specs->type)
25946 decl_specs->type = type_spec;
25947 decl_specs->type_definition_p = false;
25948 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
25951 else if (decl_specs->type)
25952 decl_specs->multiple_types_p = true;
25953 else
25955 decl_specs->type = type_spec;
25956 decl_specs->type_definition_p = type_definition_p;
25957 decl_specs->redefined_builtin_type = NULL_TREE;
25958 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
25962 /* True iff TOKEN is the GNU keyword __thread. */
25964 static bool
25965 token_is__thread (cp_token *token)
25967 gcc_assert (token->keyword == RID_THREAD);
25968 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
25971 /* Set the location for a declarator specifier and check if it is
25972 duplicated.
25974 DECL_SPECS is the sequence of declarator specifiers onto which to
25975 set the location.
25977 DS is the single declarator specifier to set which location is to
25978 be set onto the existing sequence of declarators.
25980 LOCATION is the location for the declarator specifier to
25981 consider. */
25983 static void
25984 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
25985 cp_decl_spec ds, cp_token *token)
25987 gcc_assert (ds < ds_last);
25989 if (decl_specs == NULL)
25990 return;
25992 source_location location = token->location;
25994 if (decl_specs->locations[ds] == 0)
25996 decl_specs->locations[ds] = location;
25997 if (ds == ds_thread)
25998 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
26000 else
26002 if (ds == ds_long)
26004 if (decl_specs->locations[ds_long_long] != 0)
26005 error_at (location,
26006 "%<long long long%> is too long for GCC");
26007 else
26009 decl_specs->locations[ds_long_long] = location;
26010 pedwarn_cxx98 (location,
26011 OPT_Wlong_long,
26012 "ISO C++ 1998 does not support %<long long%>");
26015 else if (ds == ds_thread)
26017 bool gnu = token_is__thread (token);
26018 if (gnu != decl_specs->gnu_thread_keyword_p)
26019 error_at (location,
26020 "both %<__thread%> and %<thread_local%> specified");
26021 else
26022 error_at (location, "duplicate %qD", token->u.value);
26024 else
26026 static const char *const decl_spec_names[] = {
26027 "signed",
26028 "unsigned",
26029 "short",
26030 "long",
26031 "const",
26032 "volatile",
26033 "restrict",
26034 "inline",
26035 "virtual",
26036 "explicit",
26037 "friend",
26038 "typedef",
26039 "using",
26040 "constexpr",
26041 "__complex"
26043 error_at (location,
26044 "duplicate %qs", decl_spec_names[ds]);
26049 /* Return true iff the declarator specifier DS is present in the
26050 sequence of declarator specifiers DECL_SPECS. */
26052 bool
26053 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
26054 cp_decl_spec ds)
26056 gcc_assert (ds < ds_last);
26058 if (decl_specs == NULL)
26059 return false;
26061 return decl_specs->locations[ds] != 0;
26064 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
26065 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
26067 static bool
26068 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
26070 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
26073 /* Issue an error message indicating that TOKEN_DESC was expected.
26074 If KEYWORD is true, it indicated this function is called by
26075 cp_parser_require_keword and the required token can only be
26076 a indicated keyword. */
26078 static void
26079 cp_parser_required_error (cp_parser *parser,
26080 required_token token_desc,
26081 bool keyword)
26083 switch (token_desc)
26085 case RT_NEW:
26086 cp_parser_error (parser, "expected %<new%>");
26087 return;
26088 case RT_DELETE:
26089 cp_parser_error (parser, "expected %<delete%>");
26090 return;
26091 case RT_RETURN:
26092 cp_parser_error (parser, "expected %<return%>");
26093 return;
26094 case RT_WHILE:
26095 cp_parser_error (parser, "expected %<while%>");
26096 return;
26097 case RT_EXTERN:
26098 cp_parser_error (parser, "expected %<extern%>");
26099 return;
26100 case RT_STATIC_ASSERT:
26101 cp_parser_error (parser, "expected %<static_assert%>");
26102 return;
26103 case RT_DECLTYPE:
26104 cp_parser_error (parser, "expected %<decltype%>");
26105 return;
26106 case RT_OPERATOR:
26107 cp_parser_error (parser, "expected %<operator%>");
26108 return;
26109 case RT_CLASS:
26110 cp_parser_error (parser, "expected %<class%>");
26111 return;
26112 case RT_TEMPLATE:
26113 cp_parser_error (parser, "expected %<template%>");
26114 return;
26115 case RT_NAMESPACE:
26116 cp_parser_error (parser, "expected %<namespace%>");
26117 return;
26118 case RT_USING:
26119 cp_parser_error (parser, "expected %<using%>");
26120 return;
26121 case RT_ASM:
26122 cp_parser_error (parser, "expected %<asm%>");
26123 return;
26124 case RT_TRY:
26125 cp_parser_error (parser, "expected %<try%>");
26126 return;
26127 case RT_CATCH:
26128 cp_parser_error (parser, "expected %<catch%>");
26129 return;
26130 case RT_THROW:
26131 cp_parser_error (parser, "expected %<throw%>");
26132 return;
26133 case RT_LABEL:
26134 cp_parser_error (parser, "expected %<__label__%>");
26135 return;
26136 case RT_AT_TRY:
26137 cp_parser_error (parser, "expected %<@try%>");
26138 return;
26139 case RT_AT_SYNCHRONIZED:
26140 cp_parser_error (parser, "expected %<@synchronized%>");
26141 return;
26142 case RT_AT_THROW:
26143 cp_parser_error (parser, "expected %<@throw%>");
26144 return;
26145 case RT_TRANSACTION_ATOMIC:
26146 cp_parser_error (parser, "expected %<__transaction_atomic%>");
26147 return;
26148 case RT_TRANSACTION_RELAXED:
26149 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
26150 return;
26151 default:
26152 break;
26154 if (!keyword)
26156 switch (token_desc)
26158 case RT_SEMICOLON:
26159 cp_parser_error (parser, "expected %<;%>");
26160 return;
26161 case RT_OPEN_PAREN:
26162 cp_parser_error (parser, "expected %<(%>");
26163 return;
26164 case RT_CLOSE_BRACE:
26165 cp_parser_error (parser, "expected %<}%>");
26166 return;
26167 case RT_OPEN_BRACE:
26168 cp_parser_error (parser, "expected %<{%>");
26169 return;
26170 case RT_CLOSE_SQUARE:
26171 cp_parser_error (parser, "expected %<]%>");
26172 return;
26173 case RT_OPEN_SQUARE:
26174 cp_parser_error (parser, "expected %<[%>");
26175 return;
26176 case RT_COMMA:
26177 cp_parser_error (parser, "expected %<,%>");
26178 return;
26179 case RT_SCOPE:
26180 cp_parser_error (parser, "expected %<::%>");
26181 return;
26182 case RT_LESS:
26183 cp_parser_error (parser, "expected %<<%>");
26184 return;
26185 case RT_GREATER:
26186 cp_parser_error (parser, "expected %<>%>");
26187 return;
26188 case RT_EQ:
26189 cp_parser_error (parser, "expected %<=%>");
26190 return;
26191 case RT_ELLIPSIS:
26192 cp_parser_error (parser, "expected %<...%>");
26193 return;
26194 case RT_MULT:
26195 cp_parser_error (parser, "expected %<*%>");
26196 return;
26197 case RT_COMPL:
26198 cp_parser_error (parser, "expected %<~%>");
26199 return;
26200 case RT_COLON:
26201 cp_parser_error (parser, "expected %<:%>");
26202 return;
26203 case RT_COLON_SCOPE:
26204 cp_parser_error (parser, "expected %<:%> or %<::%>");
26205 return;
26206 case RT_CLOSE_PAREN:
26207 cp_parser_error (parser, "expected %<)%>");
26208 return;
26209 case RT_COMMA_CLOSE_PAREN:
26210 cp_parser_error (parser, "expected %<,%> or %<)%>");
26211 return;
26212 case RT_PRAGMA_EOL:
26213 cp_parser_error (parser, "expected end of line");
26214 return;
26215 case RT_NAME:
26216 cp_parser_error (parser, "expected identifier");
26217 return;
26218 case RT_SELECT:
26219 cp_parser_error (parser, "expected selection-statement");
26220 return;
26221 case RT_INTERATION:
26222 cp_parser_error (parser, "expected iteration-statement");
26223 return;
26224 case RT_JUMP:
26225 cp_parser_error (parser, "expected jump-statement");
26226 return;
26227 case RT_CLASS_KEY:
26228 cp_parser_error (parser, "expected class-key");
26229 return;
26230 case RT_CLASS_TYPENAME_TEMPLATE:
26231 cp_parser_error (parser,
26232 "expected %<class%>, %<typename%>, or %<template%>");
26233 return;
26234 default:
26235 gcc_unreachable ();
26238 else
26239 gcc_unreachable ();
26244 /* If the next token is of the indicated TYPE, consume it. Otherwise,
26245 issue an error message indicating that TOKEN_DESC was expected.
26247 Returns the token consumed, if the token had the appropriate type.
26248 Otherwise, returns NULL. */
26250 static cp_token *
26251 cp_parser_require (cp_parser* parser,
26252 enum cpp_ttype type,
26253 required_token token_desc)
26255 if (cp_lexer_next_token_is (parser->lexer, type))
26256 return cp_lexer_consume_token (parser->lexer);
26257 else
26259 /* Output the MESSAGE -- unless we're parsing tentatively. */
26260 if (!cp_parser_simulate_error (parser))
26261 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
26262 return NULL;
26266 /* An error message is produced if the next token is not '>'.
26267 All further tokens are skipped until the desired token is
26268 found or '{', '}', ';' or an unbalanced ')' or ']'. */
26270 static void
26271 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
26273 /* Current level of '< ... >'. */
26274 unsigned level = 0;
26275 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
26276 unsigned nesting_depth = 0;
26278 /* Are we ready, yet? If not, issue error message. */
26279 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
26280 return;
26282 /* Skip tokens until the desired token is found. */
26283 while (true)
26285 /* Peek at the next token. */
26286 switch (cp_lexer_peek_token (parser->lexer)->type)
26288 case CPP_LESS:
26289 if (!nesting_depth)
26290 ++level;
26291 break;
26293 case CPP_RSHIFT:
26294 if (cxx_dialect == cxx98)
26295 /* C++0x views the `>>' operator as two `>' tokens, but
26296 C++98 does not. */
26297 break;
26298 else if (!nesting_depth && level-- == 0)
26300 /* We've hit a `>>' where the first `>' closes the
26301 template argument list, and the second `>' is
26302 spurious. Just consume the `>>' and stop; we've
26303 already produced at least one error. */
26304 cp_lexer_consume_token (parser->lexer);
26305 return;
26307 /* Fall through for C++0x, so we handle the second `>' in
26308 the `>>'. */
26310 case CPP_GREATER:
26311 if (!nesting_depth && level-- == 0)
26313 /* We've reached the token we want, consume it and stop. */
26314 cp_lexer_consume_token (parser->lexer);
26315 return;
26317 break;
26319 case CPP_OPEN_PAREN:
26320 case CPP_OPEN_SQUARE:
26321 ++nesting_depth;
26322 break;
26324 case CPP_CLOSE_PAREN:
26325 case CPP_CLOSE_SQUARE:
26326 if (nesting_depth-- == 0)
26327 return;
26328 break;
26330 case CPP_EOF:
26331 case CPP_PRAGMA_EOL:
26332 case CPP_SEMICOLON:
26333 case CPP_OPEN_BRACE:
26334 case CPP_CLOSE_BRACE:
26335 /* The '>' was probably forgotten, don't look further. */
26336 return;
26338 default:
26339 break;
26342 /* Consume this token. */
26343 cp_lexer_consume_token (parser->lexer);
26347 /* If the next token is the indicated keyword, consume it. Otherwise,
26348 issue an error message indicating that TOKEN_DESC was expected.
26350 Returns the token consumed, if the token had the appropriate type.
26351 Otherwise, returns NULL. */
26353 static cp_token *
26354 cp_parser_require_keyword (cp_parser* parser,
26355 enum rid keyword,
26356 required_token token_desc)
26358 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
26360 if (token && token->keyword != keyword)
26362 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
26363 return NULL;
26366 return token;
26369 /* Returns TRUE iff TOKEN is a token that can begin the body of a
26370 function-definition. */
26372 static bool
26373 cp_parser_token_starts_function_definition_p (cp_token* token)
26375 return (/* An ordinary function-body begins with an `{'. */
26376 token->type == CPP_OPEN_BRACE
26377 /* A ctor-initializer begins with a `:'. */
26378 || token->type == CPP_COLON
26379 /* A function-try-block begins with `try'. */
26380 || token->keyword == RID_TRY
26381 /* A function-transaction-block begins with `__transaction_atomic'
26382 or `__transaction_relaxed'. */
26383 || token->keyword == RID_TRANSACTION_ATOMIC
26384 || token->keyword == RID_TRANSACTION_RELAXED
26385 /* The named return value extension begins with `return'. */
26386 || token->keyword == RID_RETURN);
26389 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
26390 definition. */
26392 static bool
26393 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
26395 cp_token *token;
26397 token = cp_lexer_peek_token (parser->lexer);
26398 return (token->type == CPP_OPEN_BRACE
26399 || (token->type == CPP_COLON
26400 && !parser->colon_doesnt_start_class_def_p));
26403 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
26404 C++0x) ending a template-argument. */
26406 static bool
26407 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
26409 cp_token *token;
26411 token = cp_lexer_peek_token (parser->lexer);
26412 return (token->type == CPP_COMMA
26413 || token->type == CPP_GREATER
26414 || token->type == CPP_ELLIPSIS
26415 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
26418 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
26419 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
26421 static bool
26422 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
26423 size_t n)
26425 cp_token *token;
26427 token = cp_lexer_peek_nth_token (parser->lexer, n);
26428 if (token->type == CPP_LESS)
26429 return true;
26430 /* Check for the sequence `<::' in the original code. It would be lexed as
26431 `[:', where `[' is a digraph, and there is no whitespace before
26432 `:'. */
26433 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
26435 cp_token *token2;
26436 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
26437 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
26438 return true;
26440 return false;
26443 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
26444 or none_type otherwise. */
26446 static enum tag_types
26447 cp_parser_token_is_class_key (cp_token* token)
26449 switch (token->keyword)
26451 case RID_CLASS:
26452 return class_type;
26453 case RID_STRUCT:
26454 return record_type;
26455 case RID_UNION:
26456 return union_type;
26458 default:
26459 return none_type;
26463 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
26464 or none_type otherwise or if the token is null. */
26466 static enum tag_types
26467 cp_parser_token_is_type_parameter_key (cp_token* token)
26469 if (!token)
26470 return none_type;
26472 switch (token->keyword)
26474 case RID_CLASS:
26475 return class_type;
26476 case RID_TYPENAME:
26477 return typename_type;
26479 default:
26480 return none_type;
26484 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
26486 static void
26487 cp_parser_check_class_key (enum tag_types class_key, tree type)
26489 if (type == error_mark_node)
26490 return;
26491 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
26493 if (permerror (input_location, "%qs tag used in naming %q#T",
26494 class_key == union_type ? "union"
26495 : class_key == record_type ? "struct" : "class",
26496 type))
26497 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
26498 "%q#T was previously declared here", type);
26502 /* Issue an error message if DECL is redeclared with different
26503 access than its original declaration [class.access.spec/3].
26504 This applies to nested classes and nested class templates.
26505 [class.mem/1]. */
26507 static void
26508 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
26510 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
26511 return;
26513 if ((TREE_PRIVATE (decl)
26514 != (current_access_specifier == access_private_node))
26515 || (TREE_PROTECTED (decl)
26516 != (current_access_specifier == access_protected_node)))
26517 error_at (location, "%qD redeclared with different access", decl);
26520 /* Look for the `template' keyword, as a syntactic disambiguator.
26521 Return TRUE iff it is present, in which case it will be
26522 consumed. */
26524 static bool
26525 cp_parser_optional_template_keyword (cp_parser *parser)
26527 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26529 /* In C++98 the `template' keyword can only be used within templates;
26530 outside templates the parser can always figure out what is a
26531 template and what is not. In C++11, per the resolution of DR 468,
26532 `template' is allowed in cases where it is not strictly necessary. */
26533 if (!processing_template_decl
26534 && pedantic && cxx_dialect == cxx98)
26536 cp_token *token = cp_lexer_peek_token (parser->lexer);
26537 pedwarn (token->location, OPT_Wpedantic,
26538 "in C++98 %<template%> (as a disambiguator) is only "
26539 "allowed within templates");
26540 /* If this part of the token stream is rescanned, the same
26541 error message would be generated. So, we purge the token
26542 from the stream. */
26543 cp_lexer_purge_token (parser->lexer);
26544 return false;
26546 else
26548 /* Consume the `template' keyword. */
26549 cp_lexer_consume_token (parser->lexer);
26550 return true;
26553 return false;
26556 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
26557 set PARSER->SCOPE, and perform other related actions. */
26559 static void
26560 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
26562 int i;
26563 struct tree_check *check_value;
26564 deferred_access_check *chk;
26565 vec<deferred_access_check, va_gc> *checks;
26567 /* Get the stored value. */
26568 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
26569 /* Perform any access checks that were deferred. */
26570 checks = check_value->checks;
26571 if (checks)
26573 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
26574 perform_or_defer_access_check (chk->binfo,
26575 chk->decl,
26576 chk->diag_decl, tf_warning_or_error);
26578 /* Set the scope from the stored value. */
26579 parser->scope = check_value->value;
26580 parser->qualifying_scope = check_value->qualifying_scope;
26581 parser->object_scope = NULL_TREE;
26584 /* Consume tokens up through a non-nested END token. Returns TRUE if we
26585 encounter the end of a block before what we were looking for. */
26587 static bool
26588 cp_parser_cache_group (cp_parser *parser,
26589 enum cpp_ttype end,
26590 unsigned depth)
26592 while (true)
26594 cp_token *token = cp_lexer_peek_token (parser->lexer);
26596 /* Abort a parenthesized expression if we encounter a semicolon. */
26597 if ((end == CPP_CLOSE_PAREN || depth == 0)
26598 && token->type == CPP_SEMICOLON)
26599 return true;
26600 /* If we've reached the end of the file, stop. */
26601 if (token->type == CPP_EOF
26602 || (end != CPP_PRAGMA_EOL
26603 && token->type == CPP_PRAGMA_EOL))
26604 return true;
26605 if (token->type == CPP_CLOSE_BRACE && depth == 0)
26606 /* We've hit the end of an enclosing block, so there's been some
26607 kind of syntax error. */
26608 return true;
26610 /* Consume the token. */
26611 cp_lexer_consume_token (parser->lexer);
26612 /* See if it starts a new group. */
26613 if (token->type == CPP_OPEN_BRACE)
26615 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
26616 /* In theory this should probably check end == '}', but
26617 cp_parser_save_member_function_body needs it to exit
26618 after either '}' or ')' when called with ')'. */
26619 if (depth == 0)
26620 return false;
26622 else if (token->type == CPP_OPEN_PAREN)
26624 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
26625 if (depth == 0 && end == CPP_CLOSE_PAREN)
26626 return false;
26628 else if (token->type == CPP_PRAGMA)
26629 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
26630 else if (token->type == end)
26631 return false;
26635 /* Like above, for caching a default argument or NSDMI. Both of these are
26636 terminated by a non-nested comma, but it can be unclear whether or not a
26637 comma is nested in a template argument list unless we do more parsing.
26638 In order to handle this ambiguity, when we encounter a ',' after a '<'
26639 we try to parse what follows as a parameter-declaration-list (in the
26640 case of a default argument) or a member-declarator (in the case of an
26641 NSDMI). If that succeeds, then we stop caching. */
26643 static tree
26644 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
26646 unsigned depth = 0;
26647 int maybe_template_id = 0;
26648 cp_token *first_token;
26649 cp_token *token;
26650 tree default_argument;
26652 /* Add tokens until we have processed the entire default
26653 argument. We add the range [first_token, token). */
26654 first_token = cp_lexer_peek_token (parser->lexer);
26655 if (first_token->type == CPP_OPEN_BRACE)
26657 /* For list-initialization, this is straightforward. */
26658 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26659 token = cp_lexer_peek_token (parser->lexer);
26661 else while (true)
26663 bool done = false;
26665 /* Peek at the next token. */
26666 token = cp_lexer_peek_token (parser->lexer);
26667 /* What we do depends on what token we have. */
26668 switch (token->type)
26670 /* In valid code, a default argument must be
26671 immediately followed by a `,' `)', or `...'. */
26672 case CPP_COMMA:
26673 if (depth == 0 && maybe_template_id)
26675 /* If we've seen a '<', we might be in a
26676 template-argument-list. Until Core issue 325 is
26677 resolved, we don't know how this situation ought
26678 to be handled, so try to DTRT. We check whether
26679 what comes after the comma is a valid parameter
26680 declaration list. If it is, then the comma ends
26681 the default argument; otherwise the default
26682 argument continues. */
26683 bool error = false;
26684 cp_token *peek;
26686 /* Set ITALP so cp_parser_parameter_declaration_list
26687 doesn't decide to commit to this parse. */
26688 bool saved_italp = parser->in_template_argument_list_p;
26689 parser->in_template_argument_list_p = true;
26691 cp_parser_parse_tentatively (parser);
26693 if (nsdmi)
26695 /* Parse declarators until we reach a non-comma or
26696 somthing that cannot be an initializer.
26697 Just checking whether we're looking at a single
26698 declarator is insufficient. Consider:
26699 int var = tuple<T,U>::x;
26700 The template parameter 'U' looks exactly like a
26701 declarator. */
26704 int ctor_dtor_or_conv_p;
26705 cp_lexer_consume_token (parser->lexer);
26706 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26707 &ctor_dtor_or_conv_p,
26708 /*parenthesized_p=*/NULL,
26709 /*member_p=*/true,
26710 /*friend_p=*/false);
26711 peek = cp_lexer_peek_token (parser->lexer);
26712 if (cp_parser_error_occurred (parser))
26713 break;
26715 while (peek->type == CPP_COMMA);
26716 /* If we met an '=' or ';' then the original comma
26717 was the end of the NSDMI. Otherwise assume
26718 we're still in the NSDMI. */
26719 error = (peek->type != CPP_EQ
26720 && peek->type != CPP_SEMICOLON);
26722 else
26724 cp_lexer_consume_token (parser->lexer);
26725 begin_scope (sk_function_parms, NULL_TREE);
26726 cp_parser_parameter_declaration_list (parser, &error);
26727 pop_bindings_and_leave_scope ();
26729 if (!cp_parser_error_occurred (parser) && !error)
26730 done = true;
26731 cp_parser_abort_tentative_parse (parser);
26733 parser->in_template_argument_list_p = saved_italp;
26734 break;
26736 case CPP_CLOSE_PAREN:
26737 case CPP_ELLIPSIS:
26738 /* If we run into a non-nested `;', `}', or `]',
26739 then the code is invalid -- but the default
26740 argument is certainly over. */
26741 case CPP_SEMICOLON:
26742 case CPP_CLOSE_BRACE:
26743 case CPP_CLOSE_SQUARE:
26744 if (depth == 0
26745 /* Handle correctly int n = sizeof ... ( p ); */
26746 && token->type != CPP_ELLIPSIS)
26747 done = true;
26748 /* Update DEPTH, if necessary. */
26749 else if (token->type == CPP_CLOSE_PAREN
26750 || token->type == CPP_CLOSE_BRACE
26751 || token->type == CPP_CLOSE_SQUARE)
26752 --depth;
26753 break;
26755 case CPP_OPEN_PAREN:
26756 case CPP_OPEN_SQUARE:
26757 case CPP_OPEN_BRACE:
26758 ++depth;
26759 break;
26761 case CPP_LESS:
26762 if (depth == 0)
26763 /* This might be the comparison operator, or it might
26764 start a template argument list. */
26765 ++maybe_template_id;
26766 break;
26768 case CPP_RSHIFT:
26769 if (cxx_dialect == cxx98)
26770 break;
26771 /* Fall through for C++0x, which treats the `>>'
26772 operator like two `>' tokens in certain
26773 cases. */
26775 case CPP_GREATER:
26776 if (depth == 0)
26778 /* This might be an operator, or it might close a
26779 template argument list. But if a previous '<'
26780 started a template argument list, this will have
26781 closed it, so we can't be in one anymore. */
26782 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
26783 if (maybe_template_id < 0)
26784 maybe_template_id = 0;
26786 break;
26788 /* If we run out of tokens, issue an error message. */
26789 case CPP_EOF:
26790 case CPP_PRAGMA_EOL:
26791 error_at (token->location, "file ends in default argument");
26792 done = true;
26793 break;
26795 case CPP_NAME:
26796 case CPP_SCOPE:
26797 /* In these cases, we should look for template-ids.
26798 For example, if the default argument is
26799 `X<int, double>()', we need to do name lookup to
26800 figure out whether or not `X' is a template; if
26801 so, the `,' does not end the default argument.
26803 That is not yet done. */
26804 break;
26806 default:
26807 break;
26810 /* If we've reached the end, stop. */
26811 if (done)
26812 break;
26814 /* Add the token to the token block. */
26815 token = cp_lexer_consume_token (parser->lexer);
26818 /* Create a DEFAULT_ARG to represent the unparsed default
26819 argument. */
26820 default_argument = make_node (DEFAULT_ARG);
26821 DEFARG_TOKENS (default_argument)
26822 = cp_token_cache_new (first_token, token);
26823 DEFARG_INSTANTIATIONS (default_argument) = NULL;
26825 return default_argument;
26828 /* Begin parsing tentatively. We always save tokens while parsing
26829 tentatively so that if the tentative parsing fails we can restore the
26830 tokens. */
26832 static void
26833 cp_parser_parse_tentatively (cp_parser* parser)
26835 /* Enter a new parsing context. */
26836 parser->context = cp_parser_context_new (parser->context);
26837 /* Begin saving tokens. */
26838 cp_lexer_save_tokens (parser->lexer);
26839 /* In order to avoid repetitive access control error messages,
26840 access checks are queued up until we are no longer parsing
26841 tentatively. */
26842 push_deferring_access_checks (dk_deferred);
26845 /* Commit to the currently active tentative parse. */
26847 static void
26848 cp_parser_commit_to_tentative_parse (cp_parser* parser)
26850 cp_parser_context *context;
26851 cp_lexer *lexer;
26853 /* Mark all of the levels as committed. */
26854 lexer = parser->lexer;
26855 for (context = parser->context; context->next; context = context->next)
26857 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
26858 break;
26859 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
26860 while (!cp_lexer_saving_tokens (lexer))
26861 lexer = lexer->next;
26862 cp_lexer_commit_tokens (lexer);
26866 /* Commit to the topmost currently active tentative parse.
26868 Note that this function shouldn't be called when there are
26869 irreversible side-effects while in a tentative state. For
26870 example, we shouldn't create a permanent entry in the symbol
26871 table, or issue an error message that might not apply if the
26872 tentative parse is aborted. */
26874 static void
26875 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
26877 cp_parser_context *context = parser->context;
26878 cp_lexer *lexer = parser->lexer;
26880 if (context)
26882 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
26883 return;
26884 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
26886 while (!cp_lexer_saving_tokens (lexer))
26887 lexer = lexer->next;
26888 cp_lexer_commit_tokens (lexer);
26892 /* Abort the currently active tentative parse. All consumed tokens
26893 will be rolled back, and no diagnostics will be issued. */
26895 static void
26896 cp_parser_abort_tentative_parse (cp_parser* parser)
26898 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
26899 || errorcount > 0);
26900 cp_parser_simulate_error (parser);
26901 /* Now, pretend that we want to see if the construct was
26902 successfully parsed. */
26903 cp_parser_parse_definitely (parser);
26906 /* Stop parsing tentatively. If a parse error has occurred, restore the
26907 token stream. Otherwise, commit to the tokens we have consumed.
26908 Returns true if no error occurred; false otherwise. */
26910 static bool
26911 cp_parser_parse_definitely (cp_parser* parser)
26913 bool error_occurred;
26914 cp_parser_context *context;
26916 /* Remember whether or not an error occurred, since we are about to
26917 destroy that information. */
26918 error_occurred = cp_parser_error_occurred (parser);
26919 /* Remove the topmost context from the stack. */
26920 context = parser->context;
26921 parser->context = context->next;
26922 /* If no parse errors occurred, commit to the tentative parse. */
26923 if (!error_occurred)
26925 /* Commit to the tokens read tentatively, unless that was
26926 already done. */
26927 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
26928 cp_lexer_commit_tokens (parser->lexer);
26930 pop_to_parent_deferring_access_checks ();
26932 /* Otherwise, if errors occurred, roll back our state so that things
26933 are just as they were before we began the tentative parse. */
26934 else
26936 cp_lexer_rollback_tokens (parser->lexer);
26937 pop_deferring_access_checks ();
26939 /* Add the context to the front of the free list. */
26940 context->next = cp_parser_context_free_list;
26941 cp_parser_context_free_list = context;
26943 return !error_occurred;
26946 /* Returns true if we are parsing tentatively and are not committed to
26947 this tentative parse. */
26949 static bool
26950 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
26952 return (cp_parser_parsing_tentatively (parser)
26953 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
26956 /* Returns nonzero iff an error has occurred during the most recent
26957 tentative parse. */
26959 static bool
26960 cp_parser_error_occurred (cp_parser* parser)
26962 return (cp_parser_parsing_tentatively (parser)
26963 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
26966 /* Returns nonzero if GNU extensions are allowed. */
26968 static bool
26969 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
26971 return parser->allow_gnu_extensions_p;
26974 /* Objective-C++ Productions */
26977 /* Parse an Objective-C expression, which feeds into a primary-expression
26978 above.
26980 objc-expression:
26981 objc-message-expression
26982 objc-string-literal
26983 objc-encode-expression
26984 objc-protocol-expression
26985 objc-selector-expression
26987 Returns a tree representation of the expression. */
26989 static tree
26990 cp_parser_objc_expression (cp_parser* parser)
26992 /* Try to figure out what kind of declaration is present. */
26993 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26995 switch (kwd->type)
26997 case CPP_OPEN_SQUARE:
26998 return cp_parser_objc_message_expression (parser);
27000 case CPP_OBJC_STRING:
27001 kwd = cp_lexer_consume_token (parser->lexer);
27002 return objc_build_string_object (kwd->u.value);
27004 case CPP_KEYWORD:
27005 switch (kwd->keyword)
27007 case RID_AT_ENCODE:
27008 return cp_parser_objc_encode_expression (parser);
27010 case RID_AT_PROTOCOL:
27011 return cp_parser_objc_protocol_expression (parser);
27013 case RID_AT_SELECTOR:
27014 return cp_parser_objc_selector_expression (parser);
27016 default:
27017 break;
27019 default:
27020 error_at (kwd->location,
27021 "misplaced %<@%D%> Objective-C++ construct",
27022 kwd->u.value);
27023 cp_parser_skip_to_end_of_block_or_statement (parser);
27026 return error_mark_node;
27029 /* Parse an Objective-C message expression.
27031 objc-message-expression:
27032 [ objc-message-receiver objc-message-args ]
27034 Returns a representation of an Objective-C message. */
27036 static tree
27037 cp_parser_objc_message_expression (cp_parser* parser)
27039 tree receiver, messageargs;
27041 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
27042 receiver = cp_parser_objc_message_receiver (parser);
27043 messageargs = cp_parser_objc_message_args (parser);
27044 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27046 return objc_build_message_expr (receiver, messageargs);
27049 /* Parse an objc-message-receiver.
27051 objc-message-receiver:
27052 expression
27053 simple-type-specifier
27055 Returns a representation of the type or expression. */
27057 static tree
27058 cp_parser_objc_message_receiver (cp_parser* parser)
27060 tree rcv;
27062 /* An Objective-C message receiver may be either (1) a type
27063 or (2) an expression. */
27064 cp_parser_parse_tentatively (parser);
27065 rcv = cp_parser_expression (parser);
27067 /* If that worked out, fine. */
27068 if (cp_parser_parse_definitely (parser))
27069 return rcv;
27071 cp_parser_parse_tentatively (parser);
27072 rcv = cp_parser_simple_type_specifier (parser,
27073 /*decl_specs=*/NULL,
27074 CP_PARSER_FLAGS_NONE);
27076 if (cp_parser_parse_definitely (parser))
27077 return objc_get_class_reference (rcv);
27079 cp_parser_error (parser, "objective-c++ message receiver expected");
27080 return error_mark_node;
27083 /* Parse the arguments and selectors comprising an Objective-C message.
27085 objc-message-args:
27086 objc-selector
27087 objc-selector-args
27088 objc-selector-args , objc-comma-args
27090 objc-selector-args:
27091 objc-selector [opt] : assignment-expression
27092 objc-selector-args objc-selector [opt] : assignment-expression
27094 objc-comma-args:
27095 assignment-expression
27096 objc-comma-args , assignment-expression
27098 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
27099 selector arguments and TREE_VALUE containing a list of comma
27100 arguments. */
27102 static tree
27103 cp_parser_objc_message_args (cp_parser* parser)
27105 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
27106 bool maybe_unary_selector_p = true;
27107 cp_token *token = cp_lexer_peek_token (parser->lexer);
27109 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
27111 tree selector = NULL_TREE, arg;
27113 if (token->type != CPP_COLON)
27114 selector = cp_parser_objc_selector (parser);
27116 /* Detect if we have a unary selector. */
27117 if (maybe_unary_selector_p
27118 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27119 return build_tree_list (selector, NULL_TREE);
27121 maybe_unary_selector_p = false;
27122 cp_parser_require (parser, CPP_COLON, RT_COLON);
27123 arg = cp_parser_assignment_expression (parser);
27125 sel_args
27126 = chainon (sel_args,
27127 build_tree_list (selector, arg));
27129 token = cp_lexer_peek_token (parser->lexer);
27132 /* Handle non-selector arguments, if any. */
27133 while (token->type == CPP_COMMA)
27135 tree arg;
27137 cp_lexer_consume_token (parser->lexer);
27138 arg = cp_parser_assignment_expression (parser);
27140 addl_args
27141 = chainon (addl_args,
27142 build_tree_list (NULL_TREE, arg));
27144 token = cp_lexer_peek_token (parser->lexer);
27147 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
27149 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
27150 return build_tree_list (error_mark_node, error_mark_node);
27153 return build_tree_list (sel_args, addl_args);
27156 /* Parse an Objective-C encode expression.
27158 objc-encode-expression:
27159 @encode objc-typename
27161 Returns an encoded representation of the type argument. */
27163 static tree
27164 cp_parser_objc_encode_expression (cp_parser* parser)
27166 tree type;
27167 cp_token *token;
27169 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
27170 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27171 token = cp_lexer_peek_token (parser->lexer);
27172 type = complete_type (cp_parser_type_id (parser));
27173 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27175 if (!type)
27177 error_at (token->location,
27178 "%<@encode%> must specify a type as an argument");
27179 return error_mark_node;
27182 /* This happens if we find @encode(T) (where T is a template
27183 typename or something dependent on a template typename) when
27184 parsing a template. In that case, we can't compile it
27185 immediately, but we rather create an AT_ENCODE_EXPR which will
27186 need to be instantiated when the template is used.
27188 if (dependent_type_p (type))
27190 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
27191 TREE_READONLY (value) = 1;
27192 return value;
27195 return objc_build_encode_expr (type);
27198 /* Parse an Objective-C @defs expression. */
27200 static tree
27201 cp_parser_objc_defs_expression (cp_parser *parser)
27203 tree name;
27205 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
27206 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27207 name = cp_parser_identifier (parser);
27208 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27210 return objc_get_class_ivars (name);
27213 /* Parse an Objective-C protocol expression.
27215 objc-protocol-expression:
27216 @protocol ( identifier )
27218 Returns a representation of the protocol expression. */
27220 static tree
27221 cp_parser_objc_protocol_expression (cp_parser* parser)
27223 tree proto;
27225 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
27226 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27227 proto = cp_parser_identifier (parser);
27228 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27230 return objc_build_protocol_expr (proto);
27233 /* Parse an Objective-C selector expression.
27235 objc-selector-expression:
27236 @selector ( objc-method-signature )
27238 objc-method-signature:
27239 objc-selector
27240 objc-selector-seq
27242 objc-selector-seq:
27243 objc-selector :
27244 objc-selector-seq objc-selector :
27246 Returns a representation of the method selector. */
27248 static tree
27249 cp_parser_objc_selector_expression (cp_parser* parser)
27251 tree sel_seq = NULL_TREE;
27252 bool maybe_unary_selector_p = true;
27253 cp_token *token;
27254 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27256 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
27257 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27258 token = cp_lexer_peek_token (parser->lexer);
27260 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
27261 || token->type == CPP_SCOPE)
27263 tree selector = NULL_TREE;
27265 if (token->type != CPP_COLON
27266 || token->type == CPP_SCOPE)
27267 selector = cp_parser_objc_selector (parser);
27269 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
27270 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
27272 /* Detect if we have a unary selector. */
27273 if (maybe_unary_selector_p)
27275 sel_seq = selector;
27276 goto finish_selector;
27278 else
27280 cp_parser_error (parser, "expected %<:%>");
27283 maybe_unary_selector_p = false;
27284 token = cp_lexer_consume_token (parser->lexer);
27286 if (token->type == CPP_SCOPE)
27288 sel_seq
27289 = chainon (sel_seq,
27290 build_tree_list (selector, NULL_TREE));
27291 sel_seq
27292 = chainon (sel_seq,
27293 build_tree_list (NULL_TREE, NULL_TREE));
27295 else
27296 sel_seq
27297 = chainon (sel_seq,
27298 build_tree_list (selector, NULL_TREE));
27300 token = cp_lexer_peek_token (parser->lexer);
27303 finish_selector:
27304 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27306 return objc_build_selector_expr (loc, sel_seq);
27309 /* Parse a list of identifiers.
27311 objc-identifier-list:
27312 identifier
27313 objc-identifier-list , identifier
27315 Returns a TREE_LIST of identifier nodes. */
27317 static tree
27318 cp_parser_objc_identifier_list (cp_parser* parser)
27320 tree identifier;
27321 tree list;
27322 cp_token *sep;
27324 identifier = cp_parser_identifier (parser);
27325 if (identifier == error_mark_node)
27326 return error_mark_node;
27328 list = build_tree_list (NULL_TREE, identifier);
27329 sep = cp_lexer_peek_token (parser->lexer);
27331 while (sep->type == CPP_COMMA)
27333 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27334 identifier = cp_parser_identifier (parser);
27335 if (identifier == error_mark_node)
27336 return list;
27338 list = chainon (list, build_tree_list (NULL_TREE,
27339 identifier));
27340 sep = cp_lexer_peek_token (parser->lexer);
27343 return list;
27346 /* Parse an Objective-C alias declaration.
27348 objc-alias-declaration:
27349 @compatibility_alias identifier identifier ;
27351 This function registers the alias mapping with the Objective-C front end.
27352 It returns nothing. */
27354 static void
27355 cp_parser_objc_alias_declaration (cp_parser* parser)
27357 tree alias, orig;
27359 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
27360 alias = cp_parser_identifier (parser);
27361 orig = cp_parser_identifier (parser);
27362 objc_declare_alias (alias, orig);
27363 cp_parser_consume_semicolon_at_end_of_statement (parser);
27366 /* Parse an Objective-C class forward-declaration.
27368 objc-class-declaration:
27369 @class objc-identifier-list ;
27371 The function registers the forward declarations with the Objective-C
27372 front end. It returns nothing. */
27374 static void
27375 cp_parser_objc_class_declaration (cp_parser* parser)
27377 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
27378 while (true)
27380 tree id;
27382 id = cp_parser_identifier (parser);
27383 if (id == error_mark_node)
27384 break;
27386 objc_declare_class (id);
27388 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27389 cp_lexer_consume_token (parser->lexer);
27390 else
27391 break;
27393 cp_parser_consume_semicolon_at_end_of_statement (parser);
27396 /* Parse a list of Objective-C protocol references.
27398 objc-protocol-refs-opt:
27399 objc-protocol-refs [opt]
27401 objc-protocol-refs:
27402 < objc-identifier-list >
27404 Returns a TREE_LIST of identifiers, if any. */
27406 static tree
27407 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
27409 tree protorefs = NULL_TREE;
27411 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
27413 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
27414 protorefs = cp_parser_objc_identifier_list (parser);
27415 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
27418 return protorefs;
27421 /* Parse a Objective-C visibility specification. */
27423 static void
27424 cp_parser_objc_visibility_spec (cp_parser* parser)
27426 cp_token *vis = cp_lexer_peek_token (parser->lexer);
27428 switch (vis->keyword)
27430 case RID_AT_PRIVATE:
27431 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
27432 break;
27433 case RID_AT_PROTECTED:
27434 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
27435 break;
27436 case RID_AT_PUBLIC:
27437 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
27438 break;
27439 case RID_AT_PACKAGE:
27440 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
27441 break;
27442 default:
27443 return;
27446 /* Eat '@private'/'@protected'/'@public'. */
27447 cp_lexer_consume_token (parser->lexer);
27450 /* Parse an Objective-C method type. Return 'true' if it is a class
27451 (+) method, and 'false' if it is an instance (-) method. */
27453 static inline bool
27454 cp_parser_objc_method_type (cp_parser* parser)
27456 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
27457 return true;
27458 else
27459 return false;
27462 /* Parse an Objective-C protocol qualifier. */
27464 static tree
27465 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
27467 tree quals = NULL_TREE, node;
27468 cp_token *token = cp_lexer_peek_token (parser->lexer);
27470 node = token->u.value;
27472 while (node && identifier_p (node)
27473 && (node == ridpointers [(int) RID_IN]
27474 || node == ridpointers [(int) RID_OUT]
27475 || node == ridpointers [(int) RID_INOUT]
27476 || node == ridpointers [(int) RID_BYCOPY]
27477 || node == ridpointers [(int) RID_BYREF]
27478 || node == ridpointers [(int) RID_ONEWAY]))
27480 quals = tree_cons (NULL_TREE, node, quals);
27481 cp_lexer_consume_token (parser->lexer);
27482 token = cp_lexer_peek_token (parser->lexer);
27483 node = token->u.value;
27486 return quals;
27489 /* Parse an Objective-C typename. */
27491 static tree
27492 cp_parser_objc_typename (cp_parser* parser)
27494 tree type_name = NULL_TREE;
27496 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27498 tree proto_quals, cp_type = NULL_TREE;
27500 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
27501 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
27503 /* An ObjC type name may consist of just protocol qualifiers, in which
27504 case the type shall default to 'id'. */
27505 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
27507 cp_type = cp_parser_type_id (parser);
27509 /* If the type could not be parsed, an error has already
27510 been produced. For error recovery, behave as if it had
27511 not been specified, which will use the default type
27512 'id'. */
27513 if (cp_type == error_mark_node)
27515 cp_type = NULL_TREE;
27516 /* We need to skip to the closing parenthesis as
27517 cp_parser_type_id() does not seem to do it for
27518 us. */
27519 cp_parser_skip_to_closing_parenthesis (parser,
27520 /*recovering=*/true,
27521 /*or_comma=*/false,
27522 /*consume_paren=*/false);
27526 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27527 type_name = build_tree_list (proto_quals, cp_type);
27530 return type_name;
27533 /* Check to see if TYPE refers to an Objective-C selector name. */
27535 static bool
27536 cp_parser_objc_selector_p (enum cpp_ttype type)
27538 return (type == CPP_NAME || type == CPP_KEYWORD
27539 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
27540 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
27541 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
27542 || type == CPP_XOR || type == CPP_XOR_EQ);
27545 /* Parse an Objective-C selector. */
27547 static tree
27548 cp_parser_objc_selector (cp_parser* parser)
27550 cp_token *token = cp_lexer_consume_token (parser->lexer);
27552 if (!cp_parser_objc_selector_p (token->type))
27554 error_at (token->location, "invalid Objective-C++ selector name");
27555 return error_mark_node;
27558 /* C++ operator names are allowed to appear in ObjC selectors. */
27559 switch (token->type)
27561 case CPP_AND_AND: return get_identifier ("and");
27562 case CPP_AND_EQ: return get_identifier ("and_eq");
27563 case CPP_AND: return get_identifier ("bitand");
27564 case CPP_OR: return get_identifier ("bitor");
27565 case CPP_COMPL: return get_identifier ("compl");
27566 case CPP_NOT: return get_identifier ("not");
27567 case CPP_NOT_EQ: return get_identifier ("not_eq");
27568 case CPP_OR_OR: return get_identifier ("or");
27569 case CPP_OR_EQ: return get_identifier ("or_eq");
27570 case CPP_XOR: return get_identifier ("xor");
27571 case CPP_XOR_EQ: return get_identifier ("xor_eq");
27572 default: return token->u.value;
27576 /* Parse an Objective-C params list. */
27578 static tree
27579 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
27581 tree params = NULL_TREE;
27582 bool maybe_unary_selector_p = true;
27583 cp_token *token = cp_lexer_peek_token (parser->lexer);
27585 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
27587 tree selector = NULL_TREE, type_name, identifier;
27588 tree parm_attr = NULL_TREE;
27590 if (token->keyword == RID_ATTRIBUTE)
27591 break;
27593 if (token->type != CPP_COLON)
27594 selector = cp_parser_objc_selector (parser);
27596 /* Detect if we have a unary selector. */
27597 if (maybe_unary_selector_p
27598 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27600 params = selector; /* Might be followed by attributes. */
27601 break;
27604 maybe_unary_selector_p = false;
27605 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27607 /* Something went quite wrong. There should be a colon
27608 here, but there is not. Stop parsing parameters. */
27609 break;
27611 type_name = cp_parser_objc_typename (parser);
27612 /* New ObjC allows attributes on parameters too. */
27613 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
27614 parm_attr = cp_parser_attributes_opt (parser);
27615 identifier = cp_parser_identifier (parser);
27617 params
27618 = chainon (params,
27619 objc_build_keyword_decl (selector,
27620 type_name,
27621 identifier,
27622 parm_attr));
27624 token = cp_lexer_peek_token (parser->lexer);
27627 if (params == NULL_TREE)
27629 cp_parser_error (parser, "objective-c++ method declaration is expected");
27630 return error_mark_node;
27633 /* We allow tail attributes for the method. */
27634 if (token->keyword == RID_ATTRIBUTE)
27636 *attributes = cp_parser_attributes_opt (parser);
27637 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27638 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27639 return params;
27640 cp_parser_error (parser,
27641 "method attributes must be specified at the end");
27642 return error_mark_node;
27645 if (params == NULL_TREE)
27647 cp_parser_error (parser, "objective-c++ method declaration is expected");
27648 return error_mark_node;
27650 return params;
27653 /* Parse the non-keyword Objective-C params. */
27655 static tree
27656 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
27657 tree* attributes)
27659 tree params = make_node (TREE_LIST);
27660 cp_token *token = cp_lexer_peek_token (parser->lexer);
27661 *ellipsisp = false; /* Initially, assume no ellipsis. */
27663 while (token->type == CPP_COMMA)
27665 cp_parameter_declarator *parmdecl;
27666 tree parm;
27668 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27669 token = cp_lexer_peek_token (parser->lexer);
27671 if (token->type == CPP_ELLIPSIS)
27673 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
27674 *ellipsisp = true;
27675 token = cp_lexer_peek_token (parser->lexer);
27676 break;
27679 /* TODO: parse attributes for tail parameters. */
27680 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
27681 parm = grokdeclarator (parmdecl->declarator,
27682 &parmdecl->decl_specifiers,
27683 PARM, /*initialized=*/0,
27684 /*attrlist=*/NULL);
27686 chainon (params, build_tree_list (NULL_TREE, parm));
27687 token = cp_lexer_peek_token (parser->lexer);
27690 /* We allow tail attributes for the method. */
27691 if (token->keyword == RID_ATTRIBUTE)
27693 if (*attributes == NULL_TREE)
27695 *attributes = cp_parser_attributes_opt (parser);
27696 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27697 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27698 return params;
27700 else
27701 /* We have an error, but parse the attributes, so that we can
27702 carry on. */
27703 *attributes = cp_parser_attributes_opt (parser);
27705 cp_parser_error (parser,
27706 "method attributes must be specified at the end");
27707 return error_mark_node;
27710 return params;
27713 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
27715 static void
27716 cp_parser_objc_interstitial_code (cp_parser* parser)
27718 cp_token *token = cp_lexer_peek_token (parser->lexer);
27720 /* If the next token is `extern' and the following token is a string
27721 literal, then we have a linkage specification. */
27722 if (token->keyword == RID_EXTERN
27723 && cp_parser_is_pure_string_literal
27724 (cp_lexer_peek_nth_token (parser->lexer, 2)))
27725 cp_parser_linkage_specification (parser);
27726 /* Handle #pragma, if any. */
27727 else if (token->type == CPP_PRAGMA)
27728 cp_parser_pragma (parser, pragma_objc_icode);
27729 /* Allow stray semicolons. */
27730 else if (token->type == CPP_SEMICOLON)
27731 cp_lexer_consume_token (parser->lexer);
27732 /* Mark methods as optional or required, when building protocols. */
27733 else if (token->keyword == RID_AT_OPTIONAL)
27735 cp_lexer_consume_token (parser->lexer);
27736 objc_set_method_opt (true);
27738 else if (token->keyword == RID_AT_REQUIRED)
27740 cp_lexer_consume_token (parser->lexer);
27741 objc_set_method_opt (false);
27743 else if (token->keyword == RID_NAMESPACE)
27744 cp_parser_namespace_definition (parser);
27745 /* Other stray characters must generate errors. */
27746 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
27748 cp_lexer_consume_token (parser->lexer);
27749 error ("stray %qs between Objective-C++ methods",
27750 token->type == CPP_OPEN_BRACE ? "{" : "}");
27752 /* Finally, try to parse a block-declaration, or a function-definition. */
27753 else
27754 cp_parser_block_declaration (parser, /*statement_p=*/false);
27757 /* Parse a method signature. */
27759 static tree
27760 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
27762 tree rettype, kwdparms, optparms;
27763 bool ellipsis = false;
27764 bool is_class_method;
27766 is_class_method = cp_parser_objc_method_type (parser);
27767 rettype = cp_parser_objc_typename (parser);
27768 *attributes = NULL_TREE;
27769 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
27770 if (kwdparms == error_mark_node)
27771 return error_mark_node;
27772 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
27773 if (optparms == error_mark_node)
27774 return error_mark_node;
27776 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
27779 static bool
27780 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
27782 tree tattr;
27783 cp_lexer_save_tokens (parser->lexer);
27784 tattr = cp_parser_attributes_opt (parser);
27785 gcc_assert (tattr) ;
27787 /* If the attributes are followed by a method introducer, this is not allowed.
27788 Dump the attributes and flag the situation. */
27789 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
27790 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
27791 return true;
27793 /* Otherwise, the attributes introduce some interstitial code, possibly so
27794 rewind to allow that check. */
27795 cp_lexer_rollback_tokens (parser->lexer);
27796 return false;
27799 /* Parse an Objective-C method prototype list. */
27801 static void
27802 cp_parser_objc_method_prototype_list (cp_parser* parser)
27804 cp_token *token = cp_lexer_peek_token (parser->lexer);
27806 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
27808 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
27810 tree attributes, sig;
27811 bool is_class_method;
27812 if (token->type == CPP_PLUS)
27813 is_class_method = true;
27814 else
27815 is_class_method = false;
27816 sig = cp_parser_objc_method_signature (parser, &attributes);
27817 if (sig == error_mark_node)
27819 cp_parser_skip_to_end_of_block_or_statement (parser);
27820 token = cp_lexer_peek_token (parser->lexer);
27821 continue;
27823 objc_add_method_declaration (is_class_method, sig, attributes);
27824 cp_parser_consume_semicolon_at_end_of_statement (parser);
27826 else if (token->keyword == RID_AT_PROPERTY)
27827 cp_parser_objc_at_property_declaration (parser);
27828 else if (token->keyword == RID_ATTRIBUTE
27829 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
27830 warning_at (cp_lexer_peek_token (parser->lexer)->location,
27831 OPT_Wattributes,
27832 "prefix attributes are ignored for methods");
27833 else
27834 /* Allow for interspersed non-ObjC++ code. */
27835 cp_parser_objc_interstitial_code (parser);
27837 token = cp_lexer_peek_token (parser->lexer);
27840 if (token->type != CPP_EOF)
27841 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
27842 else
27843 cp_parser_error (parser, "expected %<@end%>");
27845 objc_finish_interface ();
27848 /* Parse an Objective-C method definition list. */
27850 static void
27851 cp_parser_objc_method_definition_list (cp_parser* parser)
27853 cp_token *token = cp_lexer_peek_token (parser->lexer);
27855 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
27857 tree meth;
27859 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
27861 cp_token *ptk;
27862 tree sig, attribute;
27863 bool is_class_method;
27864 if (token->type == CPP_PLUS)
27865 is_class_method = true;
27866 else
27867 is_class_method = false;
27868 push_deferring_access_checks (dk_deferred);
27869 sig = cp_parser_objc_method_signature (parser, &attribute);
27870 if (sig == error_mark_node)
27872 cp_parser_skip_to_end_of_block_or_statement (parser);
27873 token = cp_lexer_peek_token (parser->lexer);
27874 continue;
27876 objc_start_method_definition (is_class_method, sig, attribute,
27877 NULL_TREE);
27879 /* For historical reasons, we accept an optional semicolon. */
27880 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27881 cp_lexer_consume_token (parser->lexer);
27883 ptk = cp_lexer_peek_token (parser->lexer);
27884 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
27885 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
27887 perform_deferred_access_checks (tf_warning_or_error);
27888 stop_deferring_access_checks ();
27889 meth = cp_parser_function_definition_after_declarator (parser,
27890 false);
27891 pop_deferring_access_checks ();
27892 objc_finish_method_definition (meth);
27895 /* The following case will be removed once @synthesize is
27896 completely implemented. */
27897 else if (token->keyword == RID_AT_PROPERTY)
27898 cp_parser_objc_at_property_declaration (parser);
27899 else if (token->keyword == RID_AT_SYNTHESIZE)
27900 cp_parser_objc_at_synthesize_declaration (parser);
27901 else if (token->keyword == RID_AT_DYNAMIC)
27902 cp_parser_objc_at_dynamic_declaration (parser);
27903 else if (token->keyword == RID_ATTRIBUTE
27904 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
27905 warning_at (token->location, OPT_Wattributes,
27906 "prefix attributes are ignored for methods");
27907 else
27908 /* Allow for interspersed non-ObjC++ code. */
27909 cp_parser_objc_interstitial_code (parser);
27911 token = cp_lexer_peek_token (parser->lexer);
27914 if (token->type != CPP_EOF)
27915 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
27916 else
27917 cp_parser_error (parser, "expected %<@end%>");
27919 objc_finish_implementation ();
27922 /* Parse Objective-C ivars. */
27924 static void
27925 cp_parser_objc_class_ivars (cp_parser* parser)
27927 cp_token *token = cp_lexer_peek_token (parser->lexer);
27929 if (token->type != CPP_OPEN_BRACE)
27930 return; /* No ivars specified. */
27932 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
27933 token = cp_lexer_peek_token (parser->lexer);
27935 while (token->type != CPP_CLOSE_BRACE
27936 && token->keyword != RID_AT_END && token->type != CPP_EOF)
27938 cp_decl_specifier_seq declspecs;
27939 int decl_class_or_enum_p;
27940 tree prefix_attributes;
27942 cp_parser_objc_visibility_spec (parser);
27944 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
27945 break;
27947 cp_parser_decl_specifier_seq (parser,
27948 CP_PARSER_FLAGS_OPTIONAL,
27949 &declspecs,
27950 &decl_class_or_enum_p);
27952 /* auto, register, static, extern, mutable. */
27953 if (declspecs.storage_class != sc_none)
27955 cp_parser_error (parser, "invalid type for instance variable");
27956 declspecs.storage_class = sc_none;
27959 /* thread_local. */
27960 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
27962 cp_parser_error (parser, "invalid type for instance variable");
27963 declspecs.locations[ds_thread] = 0;
27966 /* typedef. */
27967 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
27969 cp_parser_error (parser, "invalid type for instance variable");
27970 declspecs.locations[ds_typedef] = 0;
27973 prefix_attributes = declspecs.attributes;
27974 declspecs.attributes = NULL_TREE;
27976 /* Keep going until we hit the `;' at the end of the
27977 declaration. */
27978 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27980 tree width = NULL_TREE, attributes, first_attribute, decl;
27981 cp_declarator *declarator = NULL;
27982 int ctor_dtor_or_conv_p;
27984 /* Check for a (possibly unnamed) bitfield declaration. */
27985 token = cp_lexer_peek_token (parser->lexer);
27986 if (token->type == CPP_COLON)
27987 goto eat_colon;
27989 if (token->type == CPP_NAME
27990 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
27991 == CPP_COLON))
27993 /* Get the name of the bitfield. */
27994 declarator = make_id_declarator (NULL_TREE,
27995 cp_parser_identifier (parser),
27996 sfk_none);
27998 eat_colon:
27999 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
28000 /* Get the width of the bitfield. */
28001 width
28002 = cp_parser_constant_expression (parser);
28004 else
28006 /* Parse the declarator. */
28007 declarator
28008 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28009 &ctor_dtor_or_conv_p,
28010 /*parenthesized_p=*/NULL,
28011 /*member_p=*/false,
28012 /*friend_p=*/false);
28015 /* Look for attributes that apply to the ivar. */
28016 attributes = cp_parser_attributes_opt (parser);
28017 /* Remember which attributes are prefix attributes and
28018 which are not. */
28019 first_attribute = attributes;
28020 /* Combine the attributes. */
28021 attributes = chainon (prefix_attributes, attributes);
28023 if (width)
28024 /* Create the bitfield declaration. */
28025 decl = grokbitfield (declarator, &declspecs,
28026 width,
28027 attributes);
28028 else
28029 decl = grokfield (declarator, &declspecs,
28030 NULL_TREE, /*init_const_expr_p=*/false,
28031 NULL_TREE, attributes);
28033 /* Add the instance variable. */
28034 if (decl != error_mark_node && decl != NULL_TREE)
28035 objc_add_instance_variable (decl);
28037 /* Reset PREFIX_ATTRIBUTES. */
28038 while (attributes && TREE_CHAIN (attributes) != first_attribute)
28039 attributes = TREE_CHAIN (attributes);
28040 if (attributes)
28041 TREE_CHAIN (attributes) = NULL_TREE;
28043 token = cp_lexer_peek_token (parser->lexer);
28045 if (token->type == CPP_COMMA)
28047 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28048 continue;
28050 break;
28053 cp_parser_consume_semicolon_at_end_of_statement (parser);
28054 token = cp_lexer_peek_token (parser->lexer);
28057 if (token->keyword == RID_AT_END)
28058 cp_parser_error (parser, "expected %<}%>");
28060 /* Do not consume the RID_AT_END, so it will be read again as terminating
28061 the @interface of @implementation. */
28062 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
28063 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
28065 /* For historical reasons, we accept an optional semicolon. */
28066 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28067 cp_lexer_consume_token (parser->lexer);
28070 /* Parse an Objective-C protocol declaration. */
28072 static void
28073 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
28075 tree proto, protorefs;
28076 cp_token *tok;
28078 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
28079 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
28081 tok = cp_lexer_peek_token (parser->lexer);
28082 error_at (tok->location, "identifier expected after %<@protocol%>");
28083 cp_parser_consume_semicolon_at_end_of_statement (parser);
28084 return;
28087 /* See if we have a forward declaration or a definition. */
28088 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
28090 /* Try a forward declaration first. */
28091 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
28093 while (true)
28095 tree id;
28097 id = cp_parser_identifier (parser);
28098 if (id == error_mark_node)
28099 break;
28101 objc_declare_protocol (id, attributes);
28103 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28104 cp_lexer_consume_token (parser->lexer);
28105 else
28106 break;
28108 cp_parser_consume_semicolon_at_end_of_statement (parser);
28111 /* Ok, we got a full-fledged definition (or at least should). */
28112 else
28114 proto = cp_parser_identifier (parser);
28115 protorefs = cp_parser_objc_protocol_refs_opt (parser);
28116 objc_start_protocol (proto, protorefs, attributes);
28117 cp_parser_objc_method_prototype_list (parser);
28121 /* Parse an Objective-C superclass or category. */
28123 static void
28124 cp_parser_objc_superclass_or_category (cp_parser *parser,
28125 bool iface_p,
28126 tree *super,
28127 tree *categ, bool *is_class_extension)
28129 cp_token *next = cp_lexer_peek_token (parser->lexer);
28131 *super = *categ = NULL_TREE;
28132 *is_class_extension = false;
28133 if (next->type == CPP_COLON)
28135 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
28136 *super = cp_parser_identifier (parser);
28138 else if (next->type == CPP_OPEN_PAREN)
28140 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
28142 /* If there is no category name, and this is an @interface, we
28143 have a class extension. */
28144 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
28146 *categ = NULL_TREE;
28147 *is_class_extension = true;
28149 else
28150 *categ = cp_parser_identifier (parser);
28152 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28156 /* Parse an Objective-C class interface. */
28158 static void
28159 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
28161 tree name, super, categ, protos;
28162 bool is_class_extension;
28164 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
28165 name = cp_parser_identifier (parser);
28166 if (name == error_mark_node)
28168 /* It's hard to recover because even if valid @interface stuff
28169 is to follow, we can't compile it (or validate it) if we
28170 don't even know which class it refers to. Let's assume this
28171 was a stray '@interface' token in the stream and skip it.
28173 return;
28175 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
28176 &is_class_extension);
28177 protos = cp_parser_objc_protocol_refs_opt (parser);
28179 /* We have either a class or a category on our hands. */
28180 if (categ || is_class_extension)
28181 objc_start_category_interface (name, categ, protos, attributes);
28182 else
28184 objc_start_class_interface (name, super, protos, attributes);
28185 /* Handle instance variable declarations, if any. */
28186 cp_parser_objc_class_ivars (parser);
28187 objc_continue_interface ();
28190 cp_parser_objc_method_prototype_list (parser);
28193 /* Parse an Objective-C class implementation. */
28195 static void
28196 cp_parser_objc_class_implementation (cp_parser* parser)
28198 tree name, super, categ;
28199 bool is_class_extension;
28201 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
28202 name = cp_parser_identifier (parser);
28203 if (name == error_mark_node)
28205 /* It's hard to recover because even if valid @implementation
28206 stuff is to follow, we can't compile it (or validate it) if
28207 we don't even know which class it refers to. Let's assume
28208 this was a stray '@implementation' token in the stream and
28209 skip it.
28211 return;
28213 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
28214 &is_class_extension);
28216 /* We have either a class or a category on our hands. */
28217 if (categ)
28218 objc_start_category_implementation (name, categ);
28219 else
28221 objc_start_class_implementation (name, super);
28222 /* Handle instance variable declarations, if any. */
28223 cp_parser_objc_class_ivars (parser);
28224 objc_continue_implementation ();
28227 cp_parser_objc_method_definition_list (parser);
28230 /* Consume the @end token and finish off the implementation. */
28232 static void
28233 cp_parser_objc_end_implementation (cp_parser* parser)
28235 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
28236 objc_finish_implementation ();
28239 /* Parse an Objective-C declaration. */
28241 static void
28242 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
28244 /* Try to figure out what kind of declaration is present. */
28245 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
28247 if (attributes)
28248 switch (kwd->keyword)
28250 case RID_AT_ALIAS:
28251 case RID_AT_CLASS:
28252 case RID_AT_END:
28253 error_at (kwd->location, "attributes may not be specified before"
28254 " the %<@%D%> Objective-C++ keyword",
28255 kwd->u.value);
28256 attributes = NULL;
28257 break;
28258 case RID_AT_IMPLEMENTATION:
28259 warning_at (kwd->location, OPT_Wattributes,
28260 "prefix attributes are ignored before %<@%D%>",
28261 kwd->u.value);
28262 attributes = NULL;
28263 default:
28264 break;
28267 switch (kwd->keyword)
28269 case RID_AT_ALIAS:
28270 cp_parser_objc_alias_declaration (parser);
28271 break;
28272 case RID_AT_CLASS:
28273 cp_parser_objc_class_declaration (parser);
28274 break;
28275 case RID_AT_PROTOCOL:
28276 cp_parser_objc_protocol_declaration (parser, attributes);
28277 break;
28278 case RID_AT_INTERFACE:
28279 cp_parser_objc_class_interface (parser, attributes);
28280 break;
28281 case RID_AT_IMPLEMENTATION:
28282 cp_parser_objc_class_implementation (parser);
28283 break;
28284 case RID_AT_END:
28285 cp_parser_objc_end_implementation (parser);
28286 break;
28287 default:
28288 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
28289 kwd->u.value);
28290 cp_parser_skip_to_end_of_block_or_statement (parser);
28294 /* Parse an Objective-C try-catch-finally statement.
28296 objc-try-catch-finally-stmt:
28297 @try compound-statement objc-catch-clause-seq [opt]
28298 objc-finally-clause [opt]
28300 objc-catch-clause-seq:
28301 objc-catch-clause objc-catch-clause-seq [opt]
28303 objc-catch-clause:
28304 @catch ( objc-exception-declaration ) compound-statement
28306 objc-finally-clause:
28307 @finally compound-statement
28309 objc-exception-declaration:
28310 parameter-declaration
28311 '...'
28313 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
28315 Returns NULL_TREE.
28317 PS: This function is identical to c_parser_objc_try_catch_finally_statement
28318 for C. Keep them in sync. */
28320 static tree
28321 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
28323 location_t location;
28324 tree stmt;
28326 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
28327 location = cp_lexer_peek_token (parser->lexer)->location;
28328 objc_maybe_warn_exceptions (location);
28329 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
28330 node, lest it get absorbed into the surrounding block. */
28331 stmt = push_stmt_list ();
28332 cp_parser_compound_statement (parser, NULL, false, false);
28333 objc_begin_try_stmt (location, pop_stmt_list (stmt));
28335 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
28337 cp_parameter_declarator *parm;
28338 tree parameter_declaration = error_mark_node;
28339 bool seen_open_paren = false;
28341 cp_lexer_consume_token (parser->lexer);
28342 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28343 seen_open_paren = true;
28344 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28346 /* We have "@catch (...)" (where the '...' are literally
28347 what is in the code). Skip the '...'.
28348 parameter_declaration is set to NULL_TREE, and
28349 objc_being_catch_clauses() knows that that means
28350 '...'. */
28351 cp_lexer_consume_token (parser->lexer);
28352 parameter_declaration = NULL_TREE;
28354 else
28356 /* We have "@catch (NSException *exception)" or something
28357 like that. Parse the parameter declaration. */
28358 parm = cp_parser_parameter_declaration (parser, false, NULL);
28359 if (parm == NULL)
28360 parameter_declaration = error_mark_node;
28361 else
28362 parameter_declaration = grokdeclarator (parm->declarator,
28363 &parm->decl_specifiers,
28364 PARM, /*initialized=*/0,
28365 /*attrlist=*/NULL);
28367 if (seen_open_paren)
28368 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28369 else
28371 /* If there was no open parenthesis, we are recovering from
28372 an error, and we are trying to figure out what mistake
28373 the user has made. */
28375 /* If there is an immediate closing parenthesis, the user
28376 probably forgot the opening one (ie, they typed "@catch
28377 NSException *e)". Parse the closing parenthesis and keep
28378 going. */
28379 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
28380 cp_lexer_consume_token (parser->lexer);
28382 /* If these is no immediate closing parenthesis, the user
28383 probably doesn't know that parenthesis are required at
28384 all (ie, they typed "@catch NSException *e"). So, just
28385 forget about the closing parenthesis and keep going. */
28387 objc_begin_catch_clause (parameter_declaration);
28388 cp_parser_compound_statement (parser, NULL, false, false);
28389 objc_finish_catch_clause ();
28391 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
28393 cp_lexer_consume_token (parser->lexer);
28394 location = cp_lexer_peek_token (parser->lexer)->location;
28395 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
28396 node, lest it get absorbed into the surrounding block. */
28397 stmt = push_stmt_list ();
28398 cp_parser_compound_statement (parser, NULL, false, false);
28399 objc_build_finally_clause (location, pop_stmt_list (stmt));
28402 return objc_finish_try_stmt ();
28405 /* Parse an Objective-C synchronized statement.
28407 objc-synchronized-stmt:
28408 @synchronized ( expression ) compound-statement
28410 Returns NULL_TREE. */
28412 static tree
28413 cp_parser_objc_synchronized_statement (cp_parser *parser)
28415 location_t location;
28416 tree lock, stmt;
28418 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
28420 location = cp_lexer_peek_token (parser->lexer)->location;
28421 objc_maybe_warn_exceptions (location);
28422 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28423 lock = cp_parser_expression (parser);
28424 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28426 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
28427 node, lest it get absorbed into the surrounding block. */
28428 stmt = push_stmt_list ();
28429 cp_parser_compound_statement (parser, NULL, false, false);
28431 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
28434 /* Parse an Objective-C throw statement.
28436 objc-throw-stmt:
28437 @throw assignment-expression [opt] ;
28439 Returns a constructed '@throw' statement. */
28441 static tree
28442 cp_parser_objc_throw_statement (cp_parser *parser)
28444 tree expr = NULL_TREE;
28445 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28447 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
28449 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28450 expr = cp_parser_expression (parser);
28452 cp_parser_consume_semicolon_at_end_of_statement (parser);
28454 return objc_build_throw_stmt (loc, expr);
28457 /* Parse an Objective-C statement. */
28459 static tree
28460 cp_parser_objc_statement (cp_parser * parser)
28462 /* Try to figure out what kind of declaration is present. */
28463 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
28465 switch (kwd->keyword)
28467 case RID_AT_TRY:
28468 return cp_parser_objc_try_catch_finally_statement (parser);
28469 case RID_AT_SYNCHRONIZED:
28470 return cp_parser_objc_synchronized_statement (parser);
28471 case RID_AT_THROW:
28472 return cp_parser_objc_throw_statement (parser);
28473 default:
28474 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
28475 kwd->u.value);
28476 cp_parser_skip_to_end_of_block_or_statement (parser);
28479 return error_mark_node;
28482 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
28483 look ahead to see if an objc keyword follows the attributes. This
28484 is to detect the use of prefix attributes on ObjC @interface and
28485 @protocol. */
28487 static bool
28488 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
28490 cp_lexer_save_tokens (parser->lexer);
28491 *attrib = cp_parser_attributes_opt (parser);
28492 gcc_assert (*attrib);
28493 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
28495 cp_lexer_commit_tokens (parser->lexer);
28496 return true;
28498 cp_lexer_rollback_tokens (parser->lexer);
28499 return false;
28502 /* This routine is a minimal replacement for
28503 c_parser_struct_declaration () used when parsing the list of
28504 types/names or ObjC++ properties. For example, when parsing the
28505 code
28507 @property (readonly) int a, b, c;
28509 this function is responsible for parsing "int a, int b, int c" and
28510 returning the declarations as CHAIN of DECLs.
28512 TODO: Share this code with cp_parser_objc_class_ivars. It's very
28513 similar parsing. */
28514 static tree
28515 cp_parser_objc_struct_declaration (cp_parser *parser)
28517 tree decls = NULL_TREE;
28518 cp_decl_specifier_seq declspecs;
28519 int decl_class_or_enum_p;
28520 tree prefix_attributes;
28522 cp_parser_decl_specifier_seq (parser,
28523 CP_PARSER_FLAGS_NONE,
28524 &declspecs,
28525 &decl_class_or_enum_p);
28527 if (declspecs.type == error_mark_node)
28528 return error_mark_node;
28530 /* auto, register, static, extern, mutable. */
28531 if (declspecs.storage_class != sc_none)
28533 cp_parser_error (parser, "invalid type for property");
28534 declspecs.storage_class = sc_none;
28537 /* thread_local. */
28538 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
28540 cp_parser_error (parser, "invalid type for property");
28541 declspecs.locations[ds_thread] = 0;
28544 /* typedef. */
28545 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
28547 cp_parser_error (parser, "invalid type for property");
28548 declspecs.locations[ds_typedef] = 0;
28551 prefix_attributes = declspecs.attributes;
28552 declspecs.attributes = NULL_TREE;
28554 /* Keep going until we hit the `;' at the end of the declaration. */
28555 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28557 tree attributes, first_attribute, decl;
28558 cp_declarator *declarator;
28559 cp_token *token;
28561 /* Parse the declarator. */
28562 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28563 NULL, NULL, false, false);
28565 /* Look for attributes that apply to the ivar. */
28566 attributes = cp_parser_attributes_opt (parser);
28567 /* Remember which attributes are prefix attributes and
28568 which are not. */
28569 first_attribute = attributes;
28570 /* Combine the attributes. */
28571 attributes = chainon (prefix_attributes, attributes);
28573 decl = grokfield (declarator, &declspecs,
28574 NULL_TREE, /*init_const_expr_p=*/false,
28575 NULL_TREE, attributes);
28577 if (decl == error_mark_node || decl == NULL_TREE)
28578 return error_mark_node;
28580 /* Reset PREFIX_ATTRIBUTES. */
28581 while (attributes && TREE_CHAIN (attributes) != first_attribute)
28582 attributes = TREE_CHAIN (attributes);
28583 if (attributes)
28584 TREE_CHAIN (attributes) = NULL_TREE;
28586 DECL_CHAIN (decl) = decls;
28587 decls = decl;
28589 token = cp_lexer_peek_token (parser->lexer);
28590 if (token->type == CPP_COMMA)
28592 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28593 continue;
28595 else
28596 break;
28598 return decls;
28601 /* Parse an Objective-C @property declaration. The syntax is:
28603 objc-property-declaration:
28604 '@property' objc-property-attributes[opt] struct-declaration ;
28606 objc-property-attributes:
28607 '(' objc-property-attribute-list ')'
28609 objc-property-attribute-list:
28610 objc-property-attribute
28611 objc-property-attribute-list, objc-property-attribute
28613 objc-property-attribute
28614 'getter' = identifier
28615 'setter' = identifier
28616 'readonly'
28617 'readwrite'
28618 'assign'
28619 'retain'
28620 'copy'
28621 'nonatomic'
28623 For example:
28624 @property NSString *name;
28625 @property (readonly) id object;
28626 @property (retain, nonatomic, getter=getTheName) id name;
28627 @property int a, b, c;
28629 PS: This function is identical to
28630 c_parser_objc_at_property_declaration for C. Keep them in sync. */
28631 static void
28632 cp_parser_objc_at_property_declaration (cp_parser *parser)
28634 /* The following variables hold the attributes of the properties as
28635 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
28636 seen. When we see an attribute, we set them to 'true' (if they
28637 are boolean properties) or to the identifier (if they have an
28638 argument, ie, for getter and setter). Note that here we only
28639 parse the list of attributes, check the syntax and accumulate the
28640 attributes that we find. objc_add_property_declaration() will
28641 then process the information. */
28642 bool property_assign = false;
28643 bool property_copy = false;
28644 tree property_getter_ident = NULL_TREE;
28645 bool property_nonatomic = false;
28646 bool property_readonly = false;
28647 bool property_readwrite = false;
28648 bool property_retain = false;
28649 tree property_setter_ident = NULL_TREE;
28651 /* 'properties' is the list of properties that we read. Usually a
28652 single one, but maybe more (eg, in "@property int a, b, c;" there
28653 are three). */
28654 tree properties;
28655 location_t loc;
28657 loc = cp_lexer_peek_token (parser->lexer)->location;
28659 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
28661 /* Parse the optional attribute list... */
28662 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28664 /* Eat the '('. */
28665 cp_lexer_consume_token (parser->lexer);
28667 while (true)
28669 bool syntax_error = false;
28670 cp_token *token = cp_lexer_peek_token (parser->lexer);
28671 enum rid keyword;
28673 if (token->type != CPP_NAME)
28675 cp_parser_error (parser, "expected identifier");
28676 break;
28678 keyword = C_RID_CODE (token->u.value);
28679 cp_lexer_consume_token (parser->lexer);
28680 switch (keyword)
28682 case RID_ASSIGN: property_assign = true; break;
28683 case RID_COPY: property_copy = true; break;
28684 case RID_NONATOMIC: property_nonatomic = true; break;
28685 case RID_READONLY: property_readonly = true; break;
28686 case RID_READWRITE: property_readwrite = true; break;
28687 case RID_RETAIN: property_retain = true; break;
28689 case RID_GETTER:
28690 case RID_SETTER:
28691 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
28693 if (keyword == RID_GETTER)
28694 cp_parser_error (parser,
28695 "missing %<=%> (after %<getter%> attribute)");
28696 else
28697 cp_parser_error (parser,
28698 "missing %<=%> (after %<setter%> attribute)");
28699 syntax_error = true;
28700 break;
28702 cp_lexer_consume_token (parser->lexer); /* eat the = */
28703 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
28705 cp_parser_error (parser, "expected identifier");
28706 syntax_error = true;
28707 break;
28709 if (keyword == RID_SETTER)
28711 if (property_setter_ident != NULL_TREE)
28713 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
28714 cp_lexer_consume_token (parser->lexer);
28716 else
28717 property_setter_ident = cp_parser_objc_selector (parser);
28718 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28719 cp_parser_error (parser, "setter name must terminate with %<:%>");
28720 else
28721 cp_lexer_consume_token (parser->lexer);
28723 else
28725 if (property_getter_ident != NULL_TREE)
28727 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
28728 cp_lexer_consume_token (parser->lexer);
28730 else
28731 property_getter_ident = cp_parser_objc_selector (parser);
28733 break;
28734 default:
28735 cp_parser_error (parser, "unknown property attribute");
28736 syntax_error = true;
28737 break;
28740 if (syntax_error)
28741 break;
28743 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28744 cp_lexer_consume_token (parser->lexer);
28745 else
28746 break;
28749 /* FIXME: "@property (setter, assign);" will generate a spurious
28750 "error: expected ‘)’ before ‘,’ token". This is because
28751 cp_parser_require, unlike the C counterpart, will produce an
28752 error even if we are in error recovery. */
28753 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28755 cp_parser_skip_to_closing_parenthesis (parser,
28756 /*recovering=*/true,
28757 /*or_comma=*/false,
28758 /*consume_paren=*/true);
28762 /* ... and the property declaration(s). */
28763 properties = cp_parser_objc_struct_declaration (parser);
28765 if (properties == error_mark_node)
28767 cp_parser_skip_to_end_of_statement (parser);
28768 /* If the next token is now a `;', consume it. */
28769 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28770 cp_lexer_consume_token (parser->lexer);
28771 return;
28774 if (properties == NULL_TREE)
28775 cp_parser_error (parser, "expected identifier");
28776 else
28778 /* Comma-separated properties are chained together in
28779 reverse order; add them one by one. */
28780 properties = nreverse (properties);
28782 for (; properties; properties = TREE_CHAIN (properties))
28783 objc_add_property_declaration (loc, copy_node (properties),
28784 property_readonly, property_readwrite,
28785 property_assign, property_retain,
28786 property_copy, property_nonatomic,
28787 property_getter_ident, property_setter_ident);
28790 cp_parser_consume_semicolon_at_end_of_statement (parser);
28793 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
28795 objc-synthesize-declaration:
28796 @synthesize objc-synthesize-identifier-list ;
28798 objc-synthesize-identifier-list:
28799 objc-synthesize-identifier
28800 objc-synthesize-identifier-list, objc-synthesize-identifier
28802 objc-synthesize-identifier
28803 identifier
28804 identifier = identifier
28806 For example:
28807 @synthesize MyProperty;
28808 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
28810 PS: This function is identical to c_parser_objc_at_synthesize_declaration
28811 for C. Keep them in sync.
28813 static void
28814 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
28816 tree list = NULL_TREE;
28817 location_t loc;
28818 loc = cp_lexer_peek_token (parser->lexer)->location;
28820 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
28821 while (true)
28823 tree property, ivar;
28824 property = cp_parser_identifier (parser);
28825 if (property == error_mark_node)
28827 cp_parser_consume_semicolon_at_end_of_statement (parser);
28828 return;
28830 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
28832 cp_lexer_consume_token (parser->lexer);
28833 ivar = cp_parser_identifier (parser);
28834 if (ivar == error_mark_node)
28836 cp_parser_consume_semicolon_at_end_of_statement (parser);
28837 return;
28840 else
28841 ivar = NULL_TREE;
28842 list = chainon (list, build_tree_list (ivar, property));
28843 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28844 cp_lexer_consume_token (parser->lexer);
28845 else
28846 break;
28848 cp_parser_consume_semicolon_at_end_of_statement (parser);
28849 objc_add_synthesize_declaration (loc, list);
28852 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
28854 objc-dynamic-declaration:
28855 @dynamic identifier-list ;
28857 For example:
28858 @dynamic MyProperty;
28859 @dynamic MyProperty, AnotherProperty;
28861 PS: This function is identical to c_parser_objc_at_dynamic_declaration
28862 for C. Keep them in sync.
28864 static void
28865 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
28867 tree list = NULL_TREE;
28868 location_t loc;
28869 loc = cp_lexer_peek_token (parser->lexer)->location;
28871 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
28872 while (true)
28874 tree property;
28875 property = cp_parser_identifier (parser);
28876 if (property == error_mark_node)
28878 cp_parser_consume_semicolon_at_end_of_statement (parser);
28879 return;
28881 list = chainon (list, build_tree_list (NULL, property));
28882 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28883 cp_lexer_consume_token (parser->lexer);
28884 else
28885 break;
28887 cp_parser_consume_semicolon_at_end_of_statement (parser);
28888 objc_add_dynamic_declaration (loc, list);
28892 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
28894 /* Returns name of the next clause.
28895 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
28896 the token is not consumed. Otherwise appropriate pragma_omp_clause is
28897 returned and the token is consumed. */
28899 static pragma_omp_clause
28900 cp_parser_omp_clause_name (cp_parser *parser)
28902 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
28904 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
28905 result = PRAGMA_OMP_CLAUSE_IF;
28906 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
28907 result = PRAGMA_OMP_CLAUSE_DEFAULT;
28908 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
28909 result = PRAGMA_OACC_CLAUSE_DELETE;
28910 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
28911 result = PRAGMA_OMP_CLAUSE_PRIVATE;
28912 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
28913 result = PRAGMA_OMP_CLAUSE_FOR;
28914 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28916 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28917 const char *p = IDENTIFIER_POINTER (id);
28919 switch (p[0])
28921 case 'a':
28922 if (!strcmp ("aligned", p))
28923 result = PRAGMA_OMP_CLAUSE_ALIGNED;
28924 else if (!strcmp ("async", p))
28925 result = PRAGMA_OACC_CLAUSE_ASYNC;
28926 break;
28927 case 'c':
28928 if (!strcmp ("collapse", p))
28929 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
28930 else if (!strcmp ("copy", p))
28931 result = PRAGMA_OACC_CLAUSE_COPY;
28932 else if (!strcmp ("copyin", p))
28933 result = PRAGMA_OMP_CLAUSE_COPYIN;
28934 else if (!strcmp ("copyout", p))
28935 result = PRAGMA_OACC_CLAUSE_COPYOUT;
28936 else if (!strcmp ("copyprivate", p))
28937 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
28938 else if (!strcmp ("create", p))
28939 result = PRAGMA_OACC_CLAUSE_CREATE;
28940 break;
28941 case 'd':
28942 if (!strcmp ("depend", p))
28943 result = PRAGMA_OMP_CLAUSE_DEPEND;
28944 else if (!strcmp ("device", p))
28945 result = PRAGMA_OMP_CLAUSE_DEVICE;
28946 else if (!strcmp ("deviceptr", p))
28947 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
28948 else if (!strcmp ("dist_schedule", p))
28949 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
28950 break;
28951 case 'f':
28952 if (!strcmp ("final", p))
28953 result = PRAGMA_OMP_CLAUSE_FINAL;
28954 else if (!strcmp ("firstprivate", p))
28955 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
28956 else if (!strcmp ("from", p))
28957 result = PRAGMA_OMP_CLAUSE_FROM;
28958 break;
28959 case 'h':
28960 if (!strcmp ("host", p))
28961 result = PRAGMA_OACC_CLAUSE_HOST;
28962 break;
28963 case 'i':
28964 if (!strcmp ("inbranch", p))
28965 result = PRAGMA_OMP_CLAUSE_INBRANCH;
28966 break;
28967 case 'l':
28968 if (!strcmp ("lastprivate", p))
28969 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
28970 else if (!strcmp ("linear", p))
28971 result = PRAGMA_OMP_CLAUSE_LINEAR;
28972 break;
28973 case 'm':
28974 if (!strcmp ("map", p))
28975 result = PRAGMA_OMP_CLAUSE_MAP;
28976 else if (!strcmp ("mergeable", p))
28977 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
28978 else if (flag_cilkplus && !strcmp ("mask", p))
28979 result = PRAGMA_CILK_CLAUSE_MASK;
28980 break;
28981 case 'n':
28982 if (!strcmp ("notinbranch", p))
28983 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
28984 else if (!strcmp ("nowait", p))
28985 result = PRAGMA_OMP_CLAUSE_NOWAIT;
28986 else if (flag_cilkplus && !strcmp ("nomask", p))
28987 result = PRAGMA_CILK_CLAUSE_NOMASK;
28988 else if (!strcmp ("num_gangs", p))
28989 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
28990 else if (!strcmp ("num_teams", p))
28991 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
28992 else if (!strcmp ("num_threads", p))
28993 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
28994 else if (!strcmp ("num_workers", p))
28995 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
28996 break;
28997 case 'o':
28998 if (!strcmp ("ordered", p))
28999 result = PRAGMA_OMP_CLAUSE_ORDERED;
29000 break;
29001 case 'p':
29002 if (!strcmp ("parallel", p))
29003 result = PRAGMA_OMP_CLAUSE_PARALLEL;
29004 else if (!strcmp ("present", p))
29005 result = PRAGMA_OACC_CLAUSE_PRESENT;
29006 else if (!strcmp ("present_or_copy", p)
29007 || !strcmp ("pcopy", p))
29008 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
29009 else if (!strcmp ("present_or_copyin", p)
29010 || !strcmp ("pcopyin", p))
29011 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
29012 else if (!strcmp ("present_or_copyout", p)
29013 || !strcmp ("pcopyout", p))
29014 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
29015 else if (!strcmp ("present_or_create", p)
29016 || !strcmp ("pcreate", p))
29017 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
29018 else if (!strcmp ("proc_bind", p))
29019 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
29020 break;
29021 case 'r':
29022 if (!strcmp ("reduction", p))
29023 result = PRAGMA_OMP_CLAUSE_REDUCTION;
29024 break;
29025 case 's':
29026 if (!strcmp ("safelen", p))
29027 result = PRAGMA_OMP_CLAUSE_SAFELEN;
29028 else if (!strcmp ("schedule", p))
29029 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
29030 else if (!strcmp ("sections", p))
29031 result = PRAGMA_OMP_CLAUSE_SECTIONS;
29032 else if (!strcmp ("self", p))
29033 result = PRAGMA_OACC_CLAUSE_SELF;
29034 else if (!strcmp ("shared", p))
29035 result = PRAGMA_OMP_CLAUSE_SHARED;
29036 else if (!strcmp ("simdlen", p))
29037 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
29038 break;
29039 case 't':
29040 if (!strcmp ("taskgroup", p))
29041 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
29042 else if (!strcmp ("thread_limit", p))
29043 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
29044 else if (!strcmp ("to", p))
29045 result = PRAGMA_OMP_CLAUSE_TO;
29046 break;
29047 case 'u':
29048 if (!strcmp ("uniform", p))
29049 result = PRAGMA_OMP_CLAUSE_UNIFORM;
29050 else if (!strcmp ("untied", p))
29051 result = PRAGMA_OMP_CLAUSE_UNTIED;
29052 break;
29053 case 'v':
29054 if (!strcmp ("vector_length", p))
29055 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
29056 else if (flag_cilkplus && !strcmp ("vectorlength", p))
29057 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
29058 break;
29059 case 'w':
29060 if (!strcmp ("wait", p))
29061 result = PRAGMA_OACC_CLAUSE_WAIT;
29062 break;
29066 if (result != PRAGMA_OMP_CLAUSE_NONE)
29067 cp_lexer_consume_token (parser->lexer);
29069 return result;
29072 /* Validate that a clause of the given type does not already exist. */
29074 static void
29075 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
29076 const char *name, location_t location)
29078 tree c;
29080 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29081 if (OMP_CLAUSE_CODE (c) == code)
29083 error_at (location, "too many %qs clauses", name);
29084 break;
29088 /* OpenMP 2.5:
29089 variable-list:
29090 identifier
29091 variable-list , identifier
29093 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
29094 colon). An opening parenthesis will have been consumed by the caller.
29096 If KIND is nonzero, create the appropriate node and install the decl
29097 in OMP_CLAUSE_DECL and add the node to the head of the list.
29099 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
29100 return the list created.
29102 COLON can be NULL if only closing parenthesis should end the list,
29103 or pointer to bool which will receive false if the list is terminated
29104 by closing parenthesis or true if the list is terminated by colon. */
29106 static tree
29107 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
29108 tree list, bool *colon)
29110 cp_token *token;
29111 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
29112 if (colon)
29114 parser->colon_corrects_to_scope_p = false;
29115 *colon = false;
29117 while (1)
29119 tree name, decl;
29121 token = cp_lexer_peek_token (parser->lexer);
29122 name = cp_parser_id_expression (parser, /*template_p=*/false,
29123 /*check_dependency_p=*/true,
29124 /*template_p=*/NULL,
29125 /*declarator_p=*/false,
29126 /*optional_p=*/false);
29127 if (name == error_mark_node)
29128 goto skip_comma;
29130 decl = cp_parser_lookup_name_simple (parser, name, token->location);
29131 if (decl == error_mark_node)
29132 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
29133 token->location);
29134 else if (kind != 0)
29136 switch (kind)
29138 case OMP_CLAUSE__CACHE_:
29139 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
29141 error_at (token->location, "expected %<[%>");
29142 decl = error_mark_node;
29143 break;
29145 /* FALL THROUGH. */
29146 case OMP_CLAUSE_MAP:
29147 case OMP_CLAUSE_FROM:
29148 case OMP_CLAUSE_TO:
29149 case OMP_CLAUSE_DEPEND:
29150 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
29152 tree low_bound = NULL_TREE, length = NULL_TREE;
29154 parser->colon_corrects_to_scope_p = false;
29155 cp_lexer_consume_token (parser->lexer);
29156 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
29157 low_bound = cp_parser_expression (parser);
29158 if (!colon)
29159 parser->colon_corrects_to_scope_p
29160 = saved_colon_corrects_to_scope_p;
29161 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
29162 length = integer_one_node;
29163 else
29165 /* Look for `:'. */
29166 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29167 goto skip_comma;
29168 if (!cp_lexer_next_token_is (parser->lexer,
29169 CPP_CLOSE_SQUARE))
29170 length = cp_parser_expression (parser);
29172 /* Look for the closing `]'. */
29173 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
29174 RT_CLOSE_SQUARE))
29175 goto skip_comma;
29177 if (kind == OMP_CLAUSE__CACHE_)
29179 if (TREE_CODE (low_bound) != INTEGER_CST
29180 && !TREE_READONLY (low_bound))
29182 error_at (token->location,
29183 "%qD is not a constant", low_bound);
29184 decl = error_mark_node;
29187 if (TREE_CODE (length) != INTEGER_CST
29188 && !TREE_READONLY (length))
29190 error_at (token->location,
29191 "%qD is not a constant", length);
29192 decl = error_mark_node;
29196 decl = tree_cons (low_bound, length, decl);
29198 break;
29199 default:
29200 break;
29203 tree u = build_omp_clause (token->location, kind);
29204 OMP_CLAUSE_DECL (u) = decl;
29205 OMP_CLAUSE_CHAIN (u) = list;
29206 list = u;
29208 else
29209 list = tree_cons (decl, NULL_TREE, list);
29211 get_comma:
29212 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29213 break;
29214 cp_lexer_consume_token (parser->lexer);
29217 if (colon)
29218 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
29220 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
29222 *colon = true;
29223 cp_parser_require (parser, CPP_COLON, RT_COLON);
29224 return list;
29227 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29229 int ending;
29231 /* Try to resync to an unnested comma. Copied from
29232 cp_parser_parenthesized_expression_list. */
29233 skip_comma:
29234 if (colon)
29235 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
29236 ending = cp_parser_skip_to_closing_parenthesis (parser,
29237 /*recovering=*/true,
29238 /*or_comma=*/true,
29239 /*consume_paren=*/true);
29240 if (ending < 0)
29241 goto get_comma;
29244 return list;
29247 /* Similarly, but expect leading and trailing parenthesis. This is a very
29248 common case for omp clauses. */
29250 static tree
29251 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
29253 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29254 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
29255 return list;
29258 /* OpenACC 2.0:
29259 copy ( variable-list )
29260 copyin ( variable-list )
29261 copyout ( variable-list )
29262 create ( variable-list )
29263 delete ( variable-list )
29264 present ( variable-list )
29265 present_or_copy ( variable-list )
29266 pcopy ( variable-list )
29267 present_or_copyin ( variable-list )
29268 pcopyin ( variable-list )
29269 present_or_copyout ( variable-list )
29270 pcopyout ( variable-list )
29271 present_or_create ( variable-list )
29272 pcreate ( variable-list ) */
29274 static tree
29275 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
29276 tree list)
29278 enum gomp_map_kind kind;
29279 switch (c_kind)
29281 case PRAGMA_OACC_CLAUSE_COPY:
29282 kind = GOMP_MAP_FORCE_TOFROM;
29283 break;
29284 case PRAGMA_OACC_CLAUSE_COPYIN:
29285 kind = GOMP_MAP_FORCE_TO;
29286 break;
29287 case PRAGMA_OACC_CLAUSE_COPYOUT:
29288 kind = GOMP_MAP_FORCE_FROM;
29289 break;
29290 case PRAGMA_OACC_CLAUSE_CREATE:
29291 kind = GOMP_MAP_FORCE_ALLOC;
29292 break;
29293 case PRAGMA_OACC_CLAUSE_DELETE:
29294 kind = GOMP_MAP_FORCE_DEALLOC;
29295 break;
29296 case PRAGMA_OACC_CLAUSE_DEVICE:
29297 kind = GOMP_MAP_FORCE_TO;
29298 break;
29299 case PRAGMA_OACC_CLAUSE_HOST:
29300 case PRAGMA_OACC_CLAUSE_SELF:
29301 kind = GOMP_MAP_FORCE_FROM;
29302 break;
29303 case PRAGMA_OACC_CLAUSE_PRESENT:
29304 kind = GOMP_MAP_FORCE_PRESENT;
29305 break;
29306 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
29307 kind = GOMP_MAP_TOFROM;
29308 break;
29309 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
29310 kind = GOMP_MAP_TO;
29311 break;
29312 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
29313 kind = GOMP_MAP_FROM;
29314 break;
29315 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
29316 kind = GOMP_MAP_ALLOC;
29317 break;
29318 default:
29319 gcc_unreachable ();
29321 tree nl, c;
29322 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
29324 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
29325 OMP_CLAUSE_SET_MAP_KIND (c, kind);
29327 return nl;
29330 /* OpenACC 2.0:
29331 deviceptr ( variable-list ) */
29333 static tree
29334 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
29336 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29337 tree vars, t;
29339 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
29340 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
29341 variable-list must only allow for pointer variables. */
29342 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
29343 for (t = vars; t; t = TREE_CHAIN (t))
29345 tree v = TREE_PURPOSE (t);
29347 /* FIXME diagnostics: Ideally we should keep individual
29348 locations for all the variables in the var list to make the
29349 following errors more precise. Perhaps
29350 c_parser_omp_var_list_parens should construct a list of
29351 locations to go along with the var list. */
29353 if (!VAR_P (v))
29354 error_at (loc, "%qD is not a variable", v);
29355 else if (TREE_TYPE (v) == error_mark_node)
29357 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
29358 error_at (loc, "%qD is not a pointer variable", v);
29360 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
29361 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
29362 OMP_CLAUSE_DECL (u) = v;
29363 OMP_CLAUSE_CHAIN (u) = list;
29364 list = u;
29367 return list;
29370 /* OpenACC:
29371 vector_length ( expression ) */
29373 static tree
29374 cp_parser_oacc_clause_vector_length (cp_parser *parser, tree list)
29376 tree t, c;
29377 location_t location = cp_lexer_peek_token (parser->lexer)->location;
29378 bool error = false;
29380 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29381 return list;
29383 t = cp_parser_condition (parser);
29384 if (t == error_mark_node || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
29386 error_at (location, "expected positive integer expression");
29387 error = true;
29390 if (error || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29392 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29393 /*or_comma=*/false,
29394 /*consume_paren=*/true);
29395 return list;
29398 check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH, "vector_length",
29399 location);
29401 c = build_omp_clause (location, OMP_CLAUSE_VECTOR_LENGTH);
29402 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
29403 OMP_CLAUSE_CHAIN (c) = list;
29404 list = c;
29406 return list;
29409 /* OpenACC 2.0
29410 Parse wait clause or directive parameters. */
29412 static tree
29413 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
29415 vec<tree, va_gc> *args;
29416 tree t, args_tree;
29418 args = cp_parser_parenthesized_expression_list (parser, non_attr,
29419 /*cast_p=*/false,
29420 /*allow_expansion_p=*/true,
29421 /*non_constant_p=*/NULL);
29423 if (args == NULL || args->length () == 0)
29425 cp_parser_error (parser, "expected integer expression before ')'");
29426 if (args != NULL)
29427 release_tree_vector (args);
29428 return list;
29431 args_tree = build_tree_list_vec (args);
29433 release_tree_vector (args);
29435 for (t = args_tree; t; t = TREE_CHAIN (t))
29437 tree targ = TREE_VALUE (t);
29439 if (targ != error_mark_node)
29441 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
29442 error ("%<wait%> expression must be integral");
29443 else
29445 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
29447 mark_rvalue_use (targ);
29448 OMP_CLAUSE_DECL (c) = targ;
29449 OMP_CLAUSE_CHAIN (c) = list;
29450 list = c;
29455 return list;
29458 /* OpenACC:
29459 wait ( int-expr-list ) */
29461 static tree
29462 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
29464 location_t location = cp_lexer_peek_token (parser->lexer)->location;
29466 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
29467 return list;
29469 list = cp_parser_oacc_wait_list (parser, location, list);
29471 return list;
29474 /* OpenMP 3.0:
29475 collapse ( constant-expression ) */
29477 static tree
29478 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
29480 tree c, num;
29481 location_t loc;
29482 HOST_WIDE_INT n;
29484 loc = cp_lexer_peek_token (parser->lexer)->location;
29485 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29486 return list;
29488 num = cp_parser_constant_expression (parser);
29490 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29491 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29492 /*or_comma=*/false,
29493 /*consume_paren=*/true);
29495 if (num == error_mark_node)
29496 return list;
29497 num = fold_non_dependent_expr (num);
29498 if (!tree_fits_shwi_p (num)
29499 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
29500 || (n = tree_to_shwi (num)) <= 0
29501 || (int) n != n)
29503 error_at (loc, "collapse argument needs positive constant integer expression");
29504 return list;
29507 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
29508 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
29509 OMP_CLAUSE_CHAIN (c) = list;
29510 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
29512 return c;
29515 /* OpenMP 2.5:
29516 default ( shared | none ) */
29518 static tree
29519 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
29521 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
29522 tree c;
29524 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29525 return list;
29526 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29528 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29529 const char *p = IDENTIFIER_POINTER (id);
29531 switch (p[0])
29533 case 'n':
29534 if (strcmp ("none", p) != 0)
29535 goto invalid_kind;
29536 kind = OMP_CLAUSE_DEFAULT_NONE;
29537 break;
29539 case 's':
29540 if (strcmp ("shared", p) != 0)
29541 goto invalid_kind;
29542 kind = OMP_CLAUSE_DEFAULT_SHARED;
29543 break;
29545 default:
29546 goto invalid_kind;
29549 cp_lexer_consume_token (parser->lexer);
29551 else
29553 invalid_kind:
29554 cp_parser_error (parser, "expected %<none%> or %<shared%>");
29557 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29558 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29559 /*or_comma=*/false,
29560 /*consume_paren=*/true);
29562 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
29563 return list;
29565 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
29566 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
29567 OMP_CLAUSE_CHAIN (c) = list;
29568 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
29570 return c;
29573 /* OpenMP 3.1:
29574 final ( expression ) */
29576 static tree
29577 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
29579 tree t, c;
29581 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29582 return list;
29584 t = cp_parser_condition (parser);
29586 if (t == error_mark_node
29587 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29588 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29589 /*or_comma=*/false,
29590 /*consume_paren=*/true);
29592 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
29594 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
29595 OMP_CLAUSE_FINAL_EXPR (c) = t;
29596 OMP_CLAUSE_CHAIN (c) = list;
29598 return c;
29601 /* OpenMP 2.5:
29602 if ( expression ) */
29604 static tree
29605 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
29607 tree t, c;
29609 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29610 return list;
29612 t = cp_parser_condition (parser);
29614 if (t == error_mark_node
29615 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29616 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29617 /*or_comma=*/false,
29618 /*consume_paren=*/true);
29620 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
29622 c = build_omp_clause (location, OMP_CLAUSE_IF);
29623 OMP_CLAUSE_IF_EXPR (c) = t;
29624 OMP_CLAUSE_CHAIN (c) = list;
29626 return c;
29629 /* OpenMP 3.1:
29630 mergeable */
29632 static tree
29633 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
29634 tree list, location_t location)
29636 tree c;
29638 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
29639 location);
29641 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
29642 OMP_CLAUSE_CHAIN (c) = list;
29643 return c;
29646 /* OpenMP 2.5:
29647 nowait */
29649 static tree
29650 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
29651 tree list, location_t location)
29653 tree c;
29655 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
29657 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
29658 OMP_CLAUSE_CHAIN (c) = list;
29659 return c;
29662 /* OpenACC:
29663 num_gangs ( expression ) */
29665 static tree
29666 cp_parser_omp_clause_num_gangs (cp_parser *parser, tree list)
29668 tree t, c;
29669 location_t location = cp_lexer_peek_token (parser->lexer)->location;
29671 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29672 return list;
29674 t = cp_parser_condition (parser);
29676 if (t == error_mark_node
29677 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29678 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29679 /*or_comma=*/false,
29680 /*consume_paren=*/true);
29682 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
29684 error_at (location, "expected positive integer expression");
29685 return list;
29688 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_GANGS, "num_gangs", location);
29690 c = build_omp_clause (location, OMP_CLAUSE_NUM_GANGS);
29691 OMP_CLAUSE_NUM_GANGS_EXPR (c) = t;
29692 OMP_CLAUSE_CHAIN (c) = list;
29693 list = c;
29695 return list;
29698 /* OpenMP 2.5:
29699 num_threads ( expression ) */
29701 static tree
29702 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
29703 location_t location)
29705 tree t, c;
29707 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29708 return list;
29710 t = cp_parser_expression (parser);
29712 if (t == error_mark_node
29713 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29714 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29715 /*or_comma=*/false,
29716 /*consume_paren=*/true);
29718 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
29719 "num_threads", location);
29721 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
29722 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
29723 OMP_CLAUSE_CHAIN (c) = list;
29725 return c;
29728 /* OpenACC:
29729 num_workers ( expression ) */
29731 static tree
29732 cp_parser_omp_clause_num_workers (cp_parser *parser, tree list)
29734 tree t, c;
29735 location_t location = cp_lexer_peek_token (parser->lexer)->location;
29737 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29738 return list;
29740 t = cp_parser_condition (parser);
29742 if (t == error_mark_node
29743 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29744 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29745 /*or_comma=*/false,
29746 /*consume_paren=*/true);
29748 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
29750 error_at (location, "expected positive integer expression");
29751 return list;
29754 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_WORKERS, "num_gangs",
29755 location);
29757 c = build_omp_clause (location, OMP_CLAUSE_NUM_WORKERS);
29758 OMP_CLAUSE_NUM_WORKERS_EXPR (c) = t;
29759 OMP_CLAUSE_CHAIN (c) = list;
29760 list = c;
29762 return list;
29765 /* OpenMP 2.5:
29766 ordered */
29768 static tree
29769 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
29770 tree list, location_t location)
29772 tree c;
29774 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
29775 "ordered", location);
29777 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
29778 OMP_CLAUSE_CHAIN (c) = list;
29779 return c;
29782 /* OpenMP 2.5:
29783 reduction ( reduction-operator : variable-list )
29785 reduction-operator:
29786 One of: + * - & ^ | && ||
29788 OpenMP 3.1:
29790 reduction-operator:
29791 One of: + * - & ^ | && || min max
29793 OpenMP 4.0:
29795 reduction-operator:
29796 One of: + * - & ^ | && ||
29797 id-expression */
29799 static tree
29800 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
29802 enum tree_code code = ERROR_MARK;
29803 tree nlist, c, id = NULL_TREE;
29805 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29806 return list;
29808 switch (cp_lexer_peek_token (parser->lexer)->type)
29810 case CPP_PLUS: code = PLUS_EXPR; break;
29811 case CPP_MULT: code = MULT_EXPR; break;
29812 case CPP_MINUS: code = MINUS_EXPR; break;
29813 case CPP_AND: code = BIT_AND_EXPR; break;
29814 case CPP_XOR: code = BIT_XOR_EXPR; break;
29815 case CPP_OR: code = BIT_IOR_EXPR; break;
29816 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
29817 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
29818 default: break;
29821 if (code != ERROR_MARK)
29822 cp_lexer_consume_token (parser->lexer);
29823 else
29825 bool saved_colon_corrects_to_scope_p;
29826 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
29827 parser->colon_corrects_to_scope_p = false;
29828 id = cp_parser_id_expression (parser, /*template_p=*/false,
29829 /*check_dependency_p=*/true,
29830 /*template_p=*/NULL,
29831 /*declarator_p=*/false,
29832 /*optional_p=*/false);
29833 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
29834 if (identifier_p (id))
29836 const char *p = IDENTIFIER_POINTER (id);
29838 if (strcmp (p, "min") == 0)
29839 code = MIN_EXPR;
29840 else if (strcmp (p, "max") == 0)
29841 code = MAX_EXPR;
29842 else if (id == ansi_opname (PLUS_EXPR))
29843 code = PLUS_EXPR;
29844 else if (id == ansi_opname (MULT_EXPR))
29845 code = MULT_EXPR;
29846 else if (id == ansi_opname (MINUS_EXPR))
29847 code = MINUS_EXPR;
29848 else if (id == ansi_opname (BIT_AND_EXPR))
29849 code = BIT_AND_EXPR;
29850 else if (id == ansi_opname (BIT_IOR_EXPR))
29851 code = BIT_IOR_EXPR;
29852 else if (id == ansi_opname (BIT_XOR_EXPR))
29853 code = BIT_XOR_EXPR;
29854 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
29855 code = TRUTH_ANDIF_EXPR;
29856 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
29857 code = TRUTH_ORIF_EXPR;
29858 id = omp_reduction_id (code, id, NULL_TREE);
29859 tree scope = parser->scope;
29860 if (scope)
29861 id = build_qualified_name (NULL_TREE, scope, id, false);
29862 parser->scope = NULL_TREE;
29863 parser->qualifying_scope = NULL_TREE;
29864 parser->object_scope = NULL_TREE;
29866 else
29868 error ("invalid reduction-identifier");
29869 resync_fail:
29870 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29871 /*or_comma=*/false,
29872 /*consume_paren=*/true);
29873 return list;
29877 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29878 goto resync_fail;
29880 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
29881 NULL);
29882 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
29884 OMP_CLAUSE_REDUCTION_CODE (c) = code;
29885 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
29888 return nlist;
29891 /* OpenMP 2.5:
29892 schedule ( schedule-kind )
29893 schedule ( schedule-kind , expression )
29895 schedule-kind:
29896 static | dynamic | guided | runtime | auto */
29898 static tree
29899 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
29901 tree c, t;
29903 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29904 return list;
29906 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
29908 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29910 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29911 const char *p = IDENTIFIER_POINTER (id);
29913 switch (p[0])
29915 case 'd':
29916 if (strcmp ("dynamic", p) != 0)
29917 goto invalid_kind;
29918 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
29919 break;
29921 case 'g':
29922 if (strcmp ("guided", p) != 0)
29923 goto invalid_kind;
29924 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
29925 break;
29927 case 'r':
29928 if (strcmp ("runtime", p) != 0)
29929 goto invalid_kind;
29930 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
29931 break;
29933 default:
29934 goto invalid_kind;
29937 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
29938 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
29939 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
29940 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
29941 else
29942 goto invalid_kind;
29943 cp_lexer_consume_token (parser->lexer);
29945 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29947 cp_token *token;
29948 cp_lexer_consume_token (parser->lexer);
29950 token = cp_lexer_peek_token (parser->lexer);
29951 t = cp_parser_assignment_expression (parser);
29953 if (t == error_mark_node)
29954 goto resync_fail;
29955 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
29956 error_at (token->location, "schedule %<runtime%> does not take "
29957 "a %<chunk_size%> parameter");
29958 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
29959 error_at (token->location, "schedule %<auto%> does not take "
29960 "a %<chunk_size%> parameter");
29961 else
29962 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
29964 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29965 goto resync_fail;
29967 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
29968 goto resync_fail;
29970 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
29971 OMP_CLAUSE_CHAIN (c) = list;
29972 return c;
29974 invalid_kind:
29975 cp_parser_error (parser, "invalid schedule kind");
29976 resync_fail:
29977 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29978 /*or_comma=*/false,
29979 /*consume_paren=*/true);
29980 return list;
29983 /* OpenMP 3.0:
29984 untied */
29986 static tree
29987 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
29988 tree list, location_t location)
29990 tree c;
29992 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
29994 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
29995 OMP_CLAUSE_CHAIN (c) = list;
29996 return c;
29999 /* OpenMP 4.0:
30000 inbranch
30001 notinbranch */
30003 static tree
30004 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
30005 tree list, location_t location)
30007 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
30008 tree c = build_omp_clause (location, code);
30009 OMP_CLAUSE_CHAIN (c) = list;
30010 return c;
30013 /* OpenMP 4.0:
30014 parallel
30016 sections
30017 taskgroup */
30019 static tree
30020 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
30021 enum omp_clause_code code,
30022 tree list, location_t location)
30024 tree c = build_omp_clause (location, code);
30025 OMP_CLAUSE_CHAIN (c) = list;
30026 return c;
30029 /* OpenMP 4.0:
30030 num_teams ( expression ) */
30032 static tree
30033 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
30034 location_t location)
30036 tree t, c;
30038 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30039 return list;
30041 t = cp_parser_expression (parser);
30043 if (t == error_mark_node
30044 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30045 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30046 /*or_comma=*/false,
30047 /*consume_paren=*/true);
30049 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
30050 "num_teams", location);
30052 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
30053 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
30054 OMP_CLAUSE_CHAIN (c) = list;
30056 return c;
30059 /* OpenMP 4.0:
30060 thread_limit ( expression ) */
30062 static tree
30063 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
30064 location_t location)
30066 tree t, c;
30068 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30069 return list;
30071 t = cp_parser_expression (parser);
30073 if (t == error_mark_node
30074 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30075 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30076 /*or_comma=*/false,
30077 /*consume_paren=*/true);
30079 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
30080 "thread_limit", location);
30082 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
30083 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
30084 OMP_CLAUSE_CHAIN (c) = list;
30086 return c;
30089 /* OpenMP 4.0:
30090 aligned ( variable-list )
30091 aligned ( variable-list : constant-expression ) */
30093 static tree
30094 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
30096 tree nlist, c, alignment = NULL_TREE;
30097 bool colon;
30099 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30100 return list;
30102 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
30103 &colon);
30105 if (colon)
30107 alignment = cp_parser_constant_expression (parser);
30109 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30110 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30111 /*or_comma=*/false,
30112 /*consume_paren=*/true);
30114 if (alignment == error_mark_node)
30115 alignment = NULL_TREE;
30118 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
30119 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
30121 return nlist;
30124 /* OpenMP 4.0:
30125 linear ( variable-list )
30126 linear ( variable-list : expression ) */
30128 static tree
30129 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
30130 bool is_cilk_simd_fn)
30132 tree nlist, c, step = integer_one_node;
30133 bool colon;
30135 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30136 return list;
30138 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
30139 &colon);
30141 if (colon)
30143 step = cp_parser_expression (parser);
30145 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
30147 sorry ("using parameters for %<linear%> step is not supported yet");
30148 step = integer_one_node;
30150 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30151 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30152 /*or_comma=*/false,
30153 /*consume_paren=*/true);
30155 if (step == error_mark_node)
30156 return list;
30159 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
30160 OMP_CLAUSE_LINEAR_STEP (c) = step;
30162 return nlist;
30165 /* OpenMP 4.0:
30166 safelen ( constant-expression ) */
30168 static tree
30169 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
30170 location_t location)
30172 tree t, c;
30174 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30175 return list;
30177 t = cp_parser_constant_expression (parser);
30179 if (t == error_mark_node
30180 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30181 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30182 /*or_comma=*/false,
30183 /*consume_paren=*/true);
30185 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
30187 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
30188 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
30189 OMP_CLAUSE_CHAIN (c) = list;
30191 return c;
30194 /* OpenMP 4.0:
30195 simdlen ( constant-expression ) */
30197 static tree
30198 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
30199 location_t location)
30201 tree t, c;
30203 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30204 return list;
30206 t = cp_parser_constant_expression (parser);
30208 if (t == error_mark_node
30209 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30210 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30211 /*or_comma=*/false,
30212 /*consume_paren=*/true);
30214 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
30216 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
30217 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
30218 OMP_CLAUSE_CHAIN (c) = list;
30220 return c;
30223 /* OpenMP 4.0:
30224 depend ( depend-kind : variable-list )
30226 depend-kind:
30227 in | out | inout */
30229 static tree
30230 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
30232 tree nlist, c;
30233 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
30235 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30236 return list;
30238 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30240 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30241 const char *p = IDENTIFIER_POINTER (id);
30243 if (strcmp ("in", p) == 0)
30244 kind = OMP_CLAUSE_DEPEND_IN;
30245 else if (strcmp ("inout", p) == 0)
30246 kind = OMP_CLAUSE_DEPEND_INOUT;
30247 else if (strcmp ("out", p) == 0)
30248 kind = OMP_CLAUSE_DEPEND_OUT;
30249 else
30250 goto invalid_kind;
30252 else
30253 goto invalid_kind;
30255 cp_lexer_consume_token (parser->lexer);
30256 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30257 goto resync_fail;
30259 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
30260 NULL);
30262 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
30263 OMP_CLAUSE_DEPEND_KIND (c) = kind;
30265 return nlist;
30267 invalid_kind:
30268 cp_parser_error (parser, "invalid depend kind");
30269 resync_fail:
30270 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30271 /*or_comma=*/false,
30272 /*consume_paren=*/true);
30273 return list;
30276 /* OpenMP 4.0:
30277 map ( map-kind : variable-list )
30278 map ( variable-list )
30280 map-kind:
30281 alloc | to | from | tofrom */
30283 static tree
30284 cp_parser_omp_clause_map (cp_parser *parser, tree list)
30286 tree nlist, c;
30287 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
30289 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30290 return list;
30292 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
30293 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
30295 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30296 const char *p = IDENTIFIER_POINTER (id);
30298 if (strcmp ("alloc", p) == 0)
30299 kind = GOMP_MAP_ALLOC;
30300 else if (strcmp ("to", p) == 0)
30301 kind = GOMP_MAP_TO;
30302 else if (strcmp ("from", p) == 0)
30303 kind = GOMP_MAP_FROM;
30304 else if (strcmp ("tofrom", p) == 0)
30305 kind = GOMP_MAP_TOFROM;
30306 else
30308 cp_parser_error (parser, "invalid map kind");
30309 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30310 /*or_comma=*/false,
30311 /*consume_paren=*/true);
30312 return list;
30314 cp_lexer_consume_token (parser->lexer);
30315 cp_lexer_consume_token (parser->lexer);
30318 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
30319 NULL);
30321 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
30322 OMP_CLAUSE_SET_MAP_KIND (c, kind);
30324 return nlist;
30327 /* OpenMP 4.0:
30328 device ( expression ) */
30330 static tree
30331 cp_parser_omp_clause_device (cp_parser *parser, tree list,
30332 location_t location)
30334 tree t, c;
30336 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30337 return list;
30339 t = cp_parser_expression (parser);
30341 if (t == error_mark_node
30342 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30343 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30344 /*or_comma=*/false,
30345 /*consume_paren=*/true);
30347 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
30348 "device", location);
30350 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
30351 OMP_CLAUSE_DEVICE_ID (c) = t;
30352 OMP_CLAUSE_CHAIN (c) = list;
30354 return c;
30357 /* OpenMP 4.0:
30358 dist_schedule ( static )
30359 dist_schedule ( static , expression ) */
30361 static tree
30362 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
30363 location_t location)
30365 tree c, t;
30367 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30368 return list;
30370 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
30372 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
30373 goto invalid_kind;
30374 cp_lexer_consume_token (parser->lexer);
30376 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30378 cp_lexer_consume_token (parser->lexer);
30380 t = cp_parser_assignment_expression (parser);
30382 if (t == error_mark_node)
30383 goto resync_fail;
30384 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
30386 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30387 goto resync_fail;
30389 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
30390 goto resync_fail;
30392 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
30393 location);
30394 OMP_CLAUSE_CHAIN (c) = list;
30395 return c;
30397 invalid_kind:
30398 cp_parser_error (parser, "invalid dist_schedule kind");
30399 resync_fail:
30400 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30401 /*or_comma=*/false,
30402 /*consume_paren=*/true);
30403 return list;
30406 /* OpenMP 4.0:
30407 proc_bind ( proc-bind-kind )
30409 proc-bind-kind:
30410 master | close | spread */
30412 static tree
30413 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
30414 location_t location)
30416 tree c;
30417 enum omp_clause_proc_bind_kind kind;
30419 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30420 return list;
30422 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30424 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30425 const char *p = IDENTIFIER_POINTER (id);
30427 if (strcmp ("master", p) == 0)
30428 kind = OMP_CLAUSE_PROC_BIND_MASTER;
30429 else if (strcmp ("close", p) == 0)
30430 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
30431 else if (strcmp ("spread", p) == 0)
30432 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
30433 else
30434 goto invalid_kind;
30436 else
30437 goto invalid_kind;
30439 cp_lexer_consume_token (parser->lexer);
30440 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
30441 goto resync_fail;
30443 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
30444 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
30445 location);
30446 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
30447 OMP_CLAUSE_CHAIN (c) = list;
30448 return c;
30450 invalid_kind:
30451 cp_parser_error (parser, "invalid depend kind");
30452 resync_fail:
30453 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30454 /*or_comma=*/false,
30455 /*consume_paren=*/true);
30456 return list;
30459 /* OpenACC:
30460 async [( int-expr )] */
30462 static tree
30463 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
30465 tree c, t;
30466 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30468 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
30470 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
30472 cp_lexer_consume_token (parser->lexer);
30474 t = cp_parser_expression (parser);
30475 if (t == error_mark_node
30476 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30477 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30478 /*or_comma=*/false,
30479 /*consume_paren=*/true);
30482 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
30484 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
30485 OMP_CLAUSE_ASYNC_EXPR (c) = t;
30486 OMP_CLAUSE_CHAIN (c) = list;
30487 list = c;
30489 return list;
30492 /* Parse all OpenACC clauses. The set clauses allowed by the directive
30493 is a bitmask in MASK. Return the list of clauses found. */
30495 static tree
30496 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
30497 const char *where, cp_token *pragma_tok,
30498 bool finish_p = true)
30500 tree clauses = NULL;
30501 bool first = true;
30503 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30505 location_t here;
30506 pragma_omp_clause c_kind;
30507 const char *c_name;
30508 tree prev = clauses;
30510 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30511 cp_lexer_consume_token (parser->lexer);
30513 here = cp_lexer_peek_token (parser->lexer)->location;
30514 c_kind = cp_parser_omp_clause_name (parser);
30516 switch (c_kind)
30518 case PRAGMA_OACC_CLAUSE_ASYNC:
30519 clauses = cp_parser_oacc_clause_async (parser, clauses);
30520 c_name = "async";
30521 break;
30522 case PRAGMA_OACC_CLAUSE_COLLAPSE:
30523 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
30524 c_name = "collapse";
30525 break;
30526 case PRAGMA_OACC_CLAUSE_COPY:
30527 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30528 c_name = "copy";
30529 break;
30530 case PRAGMA_OACC_CLAUSE_COPYIN:
30531 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30532 c_name = "copyin";
30533 break;
30534 case PRAGMA_OACC_CLAUSE_COPYOUT:
30535 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30536 c_name = "copyout";
30537 break;
30538 case PRAGMA_OACC_CLAUSE_CREATE:
30539 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30540 c_name = "create";
30541 break;
30542 case PRAGMA_OACC_CLAUSE_DELETE:
30543 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30544 c_name = "delete";
30545 break;
30546 case PRAGMA_OACC_CLAUSE_DEVICE:
30547 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30548 c_name = "device";
30549 break;
30550 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
30551 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
30552 c_name = "deviceptr";
30553 break;
30554 case PRAGMA_OACC_CLAUSE_HOST:
30555 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30556 c_name = "host";
30557 break;
30558 case PRAGMA_OACC_CLAUSE_IF:
30559 clauses = cp_parser_omp_clause_if (parser, clauses, here);
30560 c_name = "if";
30561 break;
30562 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
30563 clauses = cp_parser_omp_clause_num_gangs (parser, clauses);
30564 c_name = "num_gangs";
30565 break;
30566 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
30567 clauses = cp_parser_omp_clause_num_workers (parser, clauses);
30568 c_name = "num_workers";
30569 break;
30570 case PRAGMA_OACC_CLAUSE_PRESENT:
30571 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30572 c_name = "present";
30573 break;
30574 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
30575 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30576 c_name = "present_or_copy";
30577 break;
30578 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
30579 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30580 c_name = "present_or_copyin";
30581 break;
30582 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
30583 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30584 c_name = "present_or_copyout";
30585 break;
30586 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
30587 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30588 c_name = "present_or_create";
30589 break;
30590 case PRAGMA_OACC_CLAUSE_REDUCTION:
30591 clauses = cp_parser_omp_clause_reduction (parser, clauses);
30592 c_name = "reduction";
30593 break;
30594 case PRAGMA_OACC_CLAUSE_SELF:
30595 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
30596 c_name = "self";
30597 break;
30598 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
30599 clauses = cp_parser_oacc_clause_vector_length (parser, clauses);
30600 c_name = "vector_length";
30601 break;
30602 case PRAGMA_OACC_CLAUSE_WAIT:
30603 clauses = cp_parser_oacc_clause_wait (parser, clauses);
30604 c_name = "wait";
30605 break;
30606 default:
30607 cp_parser_error (parser, "expected %<#pragma acc%> clause");
30608 goto saw_error;
30611 first = false;
30613 if (((mask >> c_kind) & 1) == 0)
30615 /* Remove the invalid clause(s) from the list to avoid
30616 confusing the rest of the compiler. */
30617 clauses = prev;
30618 error_at (here, "%qs is not valid for %qs", c_name, where);
30622 saw_error:
30623 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30625 if (finish_p)
30626 return finish_omp_clauses (clauses);
30628 return clauses;
30631 /* Parse all OpenMP clauses. The set clauses allowed by the directive
30632 is a bitmask in MASK. Return the list of clauses found; the result
30633 of clause default goes in *pdefault. */
30635 static tree
30636 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
30637 const char *where, cp_token *pragma_tok,
30638 bool finish_p = true)
30640 tree clauses = NULL;
30641 bool first = true;
30642 cp_token *token = NULL;
30643 bool cilk_simd_fn = false;
30645 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30647 pragma_omp_clause c_kind;
30648 const char *c_name;
30649 tree prev = clauses;
30651 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30652 cp_lexer_consume_token (parser->lexer);
30654 token = cp_lexer_peek_token (parser->lexer);
30655 c_kind = cp_parser_omp_clause_name (parser);
30657 switch (c_kind)
30659 case PRAGMA_OMP_CLAUSE_COLLAPSE:
30660 clauses = cp_parser_omp_clause_collapse (parser, clauses,
30661 token->location);
30662 c_name = "collapse";
30663 break;
30664 case PRAGMA_OMP_CLAUSE_COPYIN:
30665 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
30666 c_name = "copyin";
30667 break;
30668 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
30669 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
30670 clauses);
30671 c_name = "copyprivate";
30672 break;
30673 case PRAGMA_OMP_CLAUSE_DEFAULT:
30674 clauses = cp_parser_omp_clause_default (parser, clauses,
30675 token->location);
30676 c_name = "default";
30677 break;
30678 case PRAGMA_OMP_CLAUSE_FINAL:
30679 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
30680 c_name = "final";
30681 break;
30682 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
30683 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
30684 clauses);
30685 c_name = "firstprivate";
30686 break;
30687 case PRAGMA_OMP_CLAUSE_IF:
30688 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
30689 c_name = "if";
30690 break;
30691 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
30692 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
30693 clauses);
30694 c_name = "lastprivate";
30695 break;
30696 case PRAGMA_OMP_CLAUSE_MERGEABLE:
30697 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
30698 token->location);
30699 c_name = "mergeable";
30700 break;
30701 case PRAGMA_OMP_CLAUSE_NOWAIT:
30702 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
30703 c_name = "nowait";
30704 break;
30705 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
30706 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
30707 token->location);
30708 c_name = "num_threads";
30709 break;
30710 case PRAGMA_OMP_CLAUSE_ORDERED:
30711 clauses = cp_parser_omp_clause_ordered (parser, clauses,
30712 token->location);
30713 c_name = "ordered";
30714 break;
30715 case PRAGMA_OMP_CLAUSE_PRIVATE:
30716 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
30717 clauses);
30718 c_name = "private";
30719 break;
30720 case PRAGMA_OMP_CLAUSE_REDUCTION:
30721 clauses = cp_parser_omp_clause_reduction (parser, clauses);
30722 c_name = "reduction";
30723 break;
30724 case PRAGMA_OMP_CLAUSE_SCHEDULE:
30725 clauses = cp_parser_omp_clause_schedule (parser, clauses,
30726 token->location);
30727 c_name = "schedule";
30728 break;
30729 case PRAGMA_OMP_CLAUSE_SHARED:
30730 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
30731 clauses);
30732 c_name = "shared";
30733 break;
30734 case PRAGMA_OMP_CLAUSE_UNTIED:
30735 clauses = cp_parser_omp_clause_untied (parser, clauses,
30736 token->location);
30737 c_name = "untied";
30738 break;
30739 case PRAGMA_OMP_CLAUSE_INBRANCH:
30740 case PRAGMA_CILK_CLAUSE_MASK:
30741 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
30742 clauses, token->location);
30743 c_name = "inbranch";
30744 break;
30745 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
30746 case PRAGMA_CILK_CLAUSE_NOMASK:
30747 clauses = cp_parser_omp_clause_branch (parser,
30748 OMP_CLAUSE_NOTINBRANCH,
30749 clauses, token->location);
30750 c_name = "notinbranch";
30751 break;
30752 case PRAGMA_OMP_CLAUSE_PARALLEL:
30753 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
30754 clauses, token->location);
30755 c_name = "parallel";
30756 if (!first)
30758 clause_not_first:
30759 error_at (token->location, "%qs must be the first clause of %qs",
30760 c_name, where);
30761 clauses = prev;
30763 break;
30764 case PRAGMA_OMP_CLAUSE_FOR:
30765 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
30766 clauses, token->location);
30767 c_name = "for";
30768 if (!first)
30769 goto clause_not_first;
30770 break;
30771 case PRAGMA_OMP_CLAUSE_SECTIONS:
30772 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
30773 clauses, token->location);
30774 c_name = "sections";
30775 if (!first)
30776 goto clause_not_first;
30777 break;
30778 case PRAGMA_OMP_CLAUSE_TASKGROUP:
30779 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
30780 clauses, token->location);
30781 c_name = "taskgroup";
30782 if (!first)
30783 goto clause_not_first;
30784 break;
30785 case PRAGMA_OMP_CLAUSE_TO:
30786 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
30787 clauses);
30788 c_name = "to";
30789 break;
30790 case PRAGMA_OMP_CLAUSE_FROM:
30791 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
30792 clauses);
30793 c_name = "from";
30794 break;
30795 case PRAGMA_OMP_CLAUSE_UNIFORM:
30796 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
30797 clauses);
30798 c_name = "uniform";
30799 break;
30800 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
30801 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
30802 token->location);
30803 c_name = "num_teams";
30804 break;
30805 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
30806 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
30807 token->location);
30808 c_name = "thread_limit";
30809 break;
30810 case PRAGMA_OMP_CLAUSE_ALIGNED:
30811 clauses = cp_parser_omp_clause_aligned (parser, clauses);
30812 c_name = "aligned";
30813 break;
30814 case PRAGMA_OMP_CLAUSE_LINEAR:
30815 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
30816 cilk_simd_fn = true;
30817 clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
30818 c_name = "linear";
30819 break;
30820 case PRAGMA_OMP_CLAUSE_DEPEND:
30821 clauses = cp_parser_omp_clause_depend (parser, clauses);
30822 c_name = "depend";
30823 break;
30824 case PRAGMA_OMP_CLAUSE_MAP:
30825 clauses = cp_parser_omp_clause_map (parser, clauses);
30826 c_name = "map";
30827 break;
30828 case PRAGMA_OMP_CLAUSE_DEVICE:
30829 clauses = cp_parser_omp_clause_device (parser, clauses,
30830 token->location);
30831 c_name = "device";
30832 break;
30833 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
30834 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
30835 token->location);
30836 c_name = "dist_schedule";
30837 break;
30838 case PRAGMA_OMP_CLAUSE_PROC_BIND:
30839 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
30840 token->location);
30841 c_name = "proc_bind";
30842 break;
30843 case PRAGMA_OMP_CLAUSE_SAFELEN:
30844 clauses = cp_parser_omp_clause_safelen (parser, clauses,
30845 token->location);
30846 c_name = "safelen";
30847 break;
30848 case PRAGMA_OMP_CLAUSE_SIMDLEN:
30849 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
30850 token->location);
30851 c_name = "simdlen";
30852 break;
30853 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
30854 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
30855 c_name = "simdlen";
30856 break;
30857 default:
30858 cp_parser_error (parser, "expected %<#pragma omp%> clause");
30859 goto saw_error;
30862 first = false;
30864 if (((mask >> c_kind) & 1) == 0)
30866 /* Remove the invalid clause(s) from the list to avoid
30867 confusing the rest of the compiler. */
30868 clauses = prev;
30869 error_at (token->location, "%qs is not valid for %qs", c_name, where);
30872 saw_error:
30873 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
30874 no reason to skip to the end. */
30875 if (!(flag_cilkplus && pragma_tok == NULL))
30876 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30877 if (finish_p)
30878 return finish_omp_clauses (clauses);
30879 return clauses;
30882 /* OpenMP 2.5:
30883 structured-block:
30884 statement
30886 In practice, we're also interested in adding the statement to an
30887 outer node. So it is convenient if we work around the fact that
30888 cp_parser_statement calls add_stmt. */
30890 static unsigned
30891 cp_parser_begin_omp_structured_block (cp_parser *parser)
30893 unsigned save = parser->in_statement;
30895 /* Only move the values to IN_OMP_BLOCK if they weren't false.
30896 This preserves the "not within loop or switch" style error messages
30897 for nonsense cases like
30898 void foo() {
30899 #pragma omp single
30900 break;
30903 if (parser->in_statement)
30904 parser->in_statement = IN_OMP_BLOCK;
30906 return save;
30909 static void
30910 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
30912 parser->in_statement = save;
30915 static tree
30916 cp_parser_omp_structured_block (cp_parser *parser)
30918 tree stmt = begin_omp_structured_block ();
30919 unsigned int save = cp_parser_begin_omp_structured_block (parser);
30921 cp_parser_statement (parser, NULL_TREE, false, NULL);
30923 cp_parser_end_omp_structured_block (parser, save);
30924 return finish_omp_structured_block (stmt);
30927 /* OpenMP 2.5:
30928 # pragma omp atomic new-line
30929 expression-stmt
30931 expression-stmt:
30932 x binop= expr | x++ | ++x | x-- | --x
30933 binop:
30934 +, *, -, /, &, ^, |, <<, >>
30936 where x is an lvalue expression with scalar type.
30938 OpenMP 3.1:
30939 # pragma omp atomic new-line
30940 update-stmt
30942 # pragma omp atomic read new-line
30943 read-stmt
30945 # pragma omp atomic write new-line
30946 write-stmt
30948 # pragma omp atomic update new-line
30949 update-stmt
30951 # pragma omp atomic capture new-line
30952 capture-stmt
30954 # pragma omp atomic capture new-line
30955 capture-block
30957 read-stmt:
30958 v = x
30959 write-stmt:
30960 x = expr
30961 update-stmt:
30962 expression-stmt | x = x binop expr
30963 capture-stmt:
30964 v = expression-stmt
30965 capture-block:
30966 { v = x; update-stmt; } | { update-stmt; v = x; }
30968 OpenMP 4.0:
30969 update-stmt:
30970 expression-stmt | x = x binop expr | x = expr binop x
30971 capture-stmt:
30972 v = update-stmt
30973 capture-block:
30974 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
30976 where x and v are lvalue expressions with scalar type. */
30978 static void
30979 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
30981 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
30982 tree rhs1 = NULL_TREE, orig_lhs;
30983 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
30984 bool structured_block = false;
30985 bool seq_cst = false;
30987 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30989 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30990 const char *p = IDENTIFIER_POINTER (id);
30992 if (!strcmp (p, "seq_cst"))
30994 seq_cst = true;
30995 cp_lexer_consume_token (parser->lexer);
30996 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
30997 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
30998 cp_lexer_consume_token (parser->lexer);
31001 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31003 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31004 const char *p = IDENTIFIER_POINTER (id);
31006 if (!strcmp (p, "read"))
31007 code = OMP_ATOMIC_READ;
31008 else if (!strcmp (p, "write"))
31009 code = NOP_EXPR;
31010 else if (!strcmp (p, "update"))
31011 code = OMP_ATOMIC;
31012 else if (!strcmp (p, "capture"))
31013 code = OMP_ATOMIC_CAPTURE_NEW;
31014 else
31015 p = NULL;
31016 if (p)
31017 cp_lexer_consume_token (parser->lexer);
31019 if (!seq_cst)
31021 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
31022 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
31023 cp_lexer_consume_token (parser->lexer);
31025 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31027 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31028 const char *p = IDENTIFIER_POINTER (id);
31030 if (!strcmp (p, "seq_cst"))
31032 seq_cst = true;
31033 cp_lexer_consume_token (parser->lexer);
31037 cp_parser_require_pragma_eol (parser, pragma_tok);
31039 switch (code)
31041 case OMP_ATOMIC_READ:
31042 case NOP_EXPR: /* atomic write */
31043 v = cp_parser_unary_expression (parser);
31044 if (v == error_mark_node)
31045 goto saw_error;
31046 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
31047 goto saw_error;
31048 if (code == NOP_EXPR)
31049 lhs = cp_parser_expression (parser);
31050 else
31051 lhs = cp_parser_unary_expression (parser);
31052 if (lhs == error_mark_node)
31053 goto saw_error;
31054 if (code == NOP_EXPR)
31056 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
31057 opcode. */
31058 code = OMP_ATOMIC;
31059 rhs = lhs;
31060 lhs = v;
31061 v = NULL_TREE;
31063 goto done;
31064 case OMP_ATOMIC_CAPTURE_NEW:
31065 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
31067 cp_lexer_consume_token (parser->lexer);
31068 structured_block = true;
31070 else
31072 v = cp_parser_unary_expression (parser);
31073 if (v == error_mark_node)
31074 goto saw_error;
31075 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
31076 goto saw_error;
31078 default:
31079 break;
31082 restart:
31083 lhs = cp_parser_unary_expression (parser);
31084 orig_lhs = lhs;
31085 switch (TREE_CODE (lhs))
31087 case ERROR_MARK:
31088 goto saw_error;
31090 case POSTINCREMENT_EXPR:
31091 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
31092 code = OMP_ATOMIC_CAPTURE_OLD;
31093 /* FALLTHROUGH */
31094 case PREINCREMENT_EXPR:
31095 lhs = TREE_OPERAND (lhs, 0);
31096 opcode = PLUS_EXPR;
31097 rhs = integer_one_node;
31098 break;
31100 case POSTDECREMENT_EXPR:
31101 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
31102 code = OMP_ATOMIC_CAPTURE_OLD;
31103 /* FALLTHROUGH */
31104 case PREDECREMENT_EXPR:
31105 lhs = TREE_OPERAND (lhs, 0);
31106 opcode = MINUS_EXPR;
31107 rhs = integer_one_node;
31108 break;
31110 case COMPOUND_EXPR:
31111 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
31112 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
31113 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
31114 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
31115 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
31116 (TREE_OPERAND (lhs, 1), 0), 0)))
31117 == BOOLEAN_TYPE)
31118 /* Undo effects of boolean_increment for post {in,de}crement. */
31119 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
31120 /* FALLTHRU */
31121 case MODIFY_EXPR:
31122 if (TREE_CODE (lhs) == MODIFY_EXPR
31123 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
31125 /* Undo effects of boolean_increment. */
31126 if (integer_onep (TREE_OPERAND (lhs, 1)))
31128 /* This is pre or post increment. */
31129 rhs = TREE_OPERAND (lhs, 1);
31130 lhs = TREE_OPERAND (lhs, 0);
31131 opcode = NOP_EXPR;
31132 if (code == OMP_ATOMIC_CAPTURE_NEW
31133 && !structured_block
31134 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
31135 code = OMP_ATOMIC_CAPTURE_OLD;
31136 break;
31139 /* FALLTHRU */
31140 default:
31141 switch (cp_lexer_peek_token (parser->lexer)->type)
31143 case CPP_MULT_EQ:
31144 opcode = MULT_EXPR;
31145 break;
31146 case CPP_DIV_EQ:
31147 opcode = TRUNC_DIV_EXPR;
31148 break;
31149 case CPP_PLUS_EQ:
31150 opcode = PLUS_EXPR;
31151 break;
31152 case CPP_MINUS_EQ:
31153 opcode = MINUS_EXPR;
31154 break;
31155 case CPP_LSHIFT_EQ:
31156 opcode = LSHIFT_EXPR;
31157 break;
31158 case CPP_RSHIFT_EQ:
31159 opcode = RSHIFT_EXPR;
31160 break;
31161 case CPP_AND_EQ:
31162 opcode = BIT_AND_EXPR;
31163 break;
31164 case CPP_OR_EQ:
31165 opcode = BIT_IOR_EXPR;
31166 break;
31167 case CPP_XOR_EQ:
31168 opcode = BIT_XOR_EXPR;
31169 break;
31170 case CPP_EQ:
31171 enum cp_parser_prec oprec;
31172 cp_token *token;
31173 cp_lexer_consume_token (parser->lexer);
31174 cp_parser_parse_tentatively (parser);
31175 rhs1 = cp_parser_simple_cast_expression (parser);
31176 if (rhs1 == error_mark_node)
31178 cp_parser_abort_tentative_parse (parser);
31179 cp_parser_simple_cast_expression (parser);
31180 goto saw_error;
31182 token = cp_lexer_peek_token (parser->lexer);
31183 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
31185 cp_parser_abort_tentative_parse (parser);
31186 cp_parser_parse_tentatively (parser);
31187 rhs = cp_parser_binary_expression (parser, false, true,
31188 PREC_NOT_OPERATOR, NULL);
31189 if (rhs == error_mark_node)
31191 cp_parser_abort_tentative_parse (parser);
31192 cp_parser_binary_expression (parser, false, true,
31193 PREC_NOT_OPERATOR, NULL);
31194 goto saw_error;
31196 switch (TREE_CODE (rhs))
31198 case MULT_EXPR:
31199 case TRUNC_DIV_EXPR:
31200 case RDIV_EXPR:
31201 case PLUS_EXPR:
31202 case MINUS_EXPR:
31203 case LSHIFT_EXPR:
31204 case RSHIFT_EXPR:
31205 case BIT_AND_EXPR:
31206 case BIT_IOR_EXPR:
31207 case BIT_XOR_EXPR:
31208 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
31210 if (cp_parser_parse_definitely (parser))
31212 opcode = TREE_CODE (rhs);
31213 rhs1 = TREE_OPERAND (rhs, 0);
31214 rhs = TREE_OPERAND (rhs, 1);
31215 goto stmt_done;
31217 else
31218 goto saw_error;
31220 break;
31221 default:
31222 break;
31224 cp_parser_abort_tentative_parse (parser);
31225 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
31227 rhs = cp_parser_expression (parser);
31228 if (rhs == error_mark_node)
31229 goto saw_error;
31230 opcode = NOP_EXPR;
31231 rhs1 = NULL_TREE;
31232 goto stmt_done;
31234 cp_parser_error (parser,
31235 "invalid form of %<#pragma omp atomic%>");
31236 goto saw_error;
31238 if (!cp_parser_parse_definitely (parser))
31239 goto saw_error;
31240 switch (token->type)
31242 case CPP_SEMICOLON:
31243 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
31245 code = OMP_ATOMIC_CAPTURE_OLD;
31246 v = lhs;
31247 lhs = NULL_TREE;
31248 lhs1 = rhs1;
31249 rhs1 = NULL_TREE;
31250 cp_lexer_consume_token (parser->lexer);
31251 goto restart;
31253 else if (structured_block)
31255 opcode = NOP_EXPR;
31256 rhs = rhs1;
31257 rhs1 = NULL_TREE;
31258 goto stmt_done;
31260 cp_parser_error (parser,
31261 "invalid form of %<#pragma omp atomic%>");
31262 goto saw_error;
31263 case CPP_MULT:
31264 opcode = MULT_EXPR;
31265 break;
31266 case CPP_DIV:
31267 opcode = TRUNC_DIV_EXPR;
31268 break;
31269 case CPP_PLUS:
31270 opcode = PLUS_EXPR;
31271 break;
31272 case CPP_MINUS:
31273 opcode = MINUS_EXPR;
31274 break;
31275 case CPP_LSHIFT:
31276 opcode = LSHIFT_EXPR;
31277 break;
31278 case CPP_RSHIFT:
31279 opcode = RSHIFT_EXPR;
31280 break;
31281 case CPP_AND:
31282 opcode = BIT_AND_EXPR;
31283 break;
31284 case CPP_OR:
31285 opcode = BIT_IOR_EXPR;
31286 break;
31287 case CPP_XOR:
31288 opcode = BIT_XOR_EXPR;
31289 break;
31290 default:
31291 cp_parser_error (parser,
31292 "invalid operator for %<#pragma omp atomic%>");
31293 goto saw_error;
31295 oprec = TOKEN_PRECEDENCE (token);
31296 gcc_assert (oprec != PREC_NOT_OPERATOR);
31297 if (commutative_tree_code (opcode))
31298 oprec = (enum cp_parser_prec) (oprec - 1);
31299 cp_lexer_consume_token (parser->lexer);
31300 rhs = cp_parser_binary_expression (parser, false, false,
31301 oprec, NULL);
31302 if (rhs == error_mark_node)
31303 goto saw_error;
31304 goto stmt_done;
31305 /* FALLTHROUGH */
31306 default:
31307 cp_parser_error (parser,
31308 "invalid operator for %<#pragma omp atomic%>");
31309 goto saw_error;
31311 cp_lexer_consume_token (parser->lexer);
31313 rhs = cp_parser_expression (parser);
31314 if (rhs == error_mark_node)
31315 goto saw_error;
31316 break;
31318 stmt_done:
31319 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
31321 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
31322 goto saw_error;
31323 v = cp_parser_unary_expression (parser);
31324 if (v == error_mark_node)
31325 goto saw_error;
31326 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
31327 goto saw_error;
31328 lhs1 = cp_parser_unary_expression (parser);
31329 if (lhs1 == error_mark_node)
31330 goto saw_error;
31332 if (structured_block)
31334 cp_parser_consume_semicolon_at_end_of_statement (parser);
31335 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
31337 done:
31338 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
31339 if (!structured_block)
31340 cp_parser_consume_semicolon_at_end_of_statement (parser);
31341 return;
31343 saw_error:
31344 cp_parser_skip_to_end_of_block_or_statement (parser);
31345 if (structured_block)
31347 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
31348 cp_lexer_consume_token (parser->lexer);
31349 else if (code == OMP_ATOMIC_CAPTURE_NEW)
31351 cp_parser_skip_to_end_of_block_or_statement (parser);
31352 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
31353 cp_lexer_consume_token (parser->lexer);
31359 /* OpenMP 2.5:
31360 # pragma omp barrier new-line */
31362 static void
31363 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
31365 cp_parser_require_pragma_eol (parser, pragma_tok);
31366 finish_omp_barrier ();
31369 /* OpenMP 2.5:
31370 # pragma omp critical [(name)] new-line
31371 structured-block */
31373 static tree
31374 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
31376 tree stmt, name = NULL;
31378 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31380 cp_lexer_consume_token (parser->lexer);
31382 name = cp_parser_identifier (parser);
31384 if (name == error_mark_node
31385 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31386 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31387 /*or_comma=*/false,
31388 /*consume_paren=*/true);
31389 if (name == error_mark_node)
31390 name = NULL;
31392 cp_parser_require_pragma_eol (parser, pragma_tok);
31394 stmt = cp_parser_omp_structured_block (parser);
31395 return c_finish_omp_critical (input_location, stmt, name);
31398 /* OpenMP 2.5:
31399 # pragma omp flush flush-vars[opt] new-line
31401 flush-vars:
31402 ( variable-list ) */
31404 static void
31405 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
31407 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31408 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31409 cp_parser_require_pragma_eol (parser, pragma_tok);
31411 finish_omp_flush ();
31414 /* Helper function, to parse omp for increment expression. */
31416 static tree
31417 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
31419 tree cond = cp_parser_binary_expression (parser, false, true,
31420 PREC_NOT_OPERATOR, NULL);
31421 if (cond == error_mark_node
31422 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31424 cp_parser_skip_to_end_of_statement (parser);
31425 return error_mark_node;
31428 switch (TREE_CODE (cond))
31430 case GT_EXPR:
31431 case GE_EXPR:
31432 case LT_EXPR:
31433 case LE_EXPR:
31434 break;
31435 case NE_EXPR:
31436 if (code == CILK_SIMD || code == CILK_FOR)
31437 break;
31438 /* Fall through: OpenMP disallows NE_EXPR. */
31439 default:
31440 return error_mark_node;
31443 /* If decl is an iterator, preserve LHS and RHS of the relational
31444 expr until finish_omp_for. */
31445 if (decl
31446 && (type_dependent_expression_p (decl)
31447 || CLASS_TYPE_P (TREE_TYPE (decl))))
31448 return cond;
31450 return build_x_binary_op (input_location, TREE_CODE (cond),
31451 TREE_OPERAND (cond, 0), ERROR_MARK,
31452 TREE_OPERAND (cond, 1), ERROR_MARK,
31453 /*overload=*/NULL, tf_warning_or_error);
31456 /* Helper function, to parse omp for increment expression. */
31458 static tree
31459 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
31461 cp_token *token = cp_lexer_peek_token (parser->lexer);
31462 enum tree_code op;
31463 tree lhs, rhs;
31464 cp_id_kind idk;
31465 bool decl_first;
31467 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
31469 op = (token->type == CPP_PLUS_PLUS
31470 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
31471 cp_lexer_consume_token (parser->lexer);
31472 lhs = cp_parser_simple_cast_expression (parser);
31473 if (lhs != decl)
31474 return error_mark_node;
31475 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
31478 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
31479 if (lhs != decl)
31480 return error_mark_node;
31482 token = cp_lexer_peek_token (parser->lexer);
31483 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
31485 op = (token->type == CPP_PLUS_PLUS
31486 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
31487 cp_lexer_consume_token (parser->lexer);
31488 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
31491 op = cp_parser_assignment_operator_opt (parser);
31492 if (op == ERROR_MARK)
31493 return error_mark_node;
31495 if (op != NOP_EXPR)
31497 rhs = cp_parser_assignment_expression (parser);
31498 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
31499 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
31502 lhs = cp_parser_binary_expression (parser, false, false,
31503 PREC_ADDITIVE_EXPRESSION, NULL);
31504 token = cp_lexer_peek_token (parser->lexer);
31505 decl_first = lhs == decl;
31506 if (decl_first)
31507 lhs = NULL_TREE;
31508 if (token->type != CPP_PLUS
31509 && token->type != CPP_MINUS)
31510 return error_mark_node;
31514 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
31515 cp_lexer_consume_token (parser->lexer);
31516 rhs = cp_parser_binary_expression (parser, false, false,
31517 PREC_ADDITIVE_EXPRESSION, NULL);
31518 token = cp_lexer_peek_token (parser->lexer);
31519 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
31521 if (lhs == NULL_TREE)
31523 if (op == PLUS_EXPR)
31524 lhs = rhs;
31525 else
31526 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
31527 tf_warning_or_error);
31529 else
31530 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
31531 ERROR_MARK, NULL, tf_warning_or_error);
31534 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
31536 if (!decl_first)
31538 if (rhs != decl || op == MINUS_EXPR)
31539 return error_mark_node;
31540 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
31542 else
31543 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
31545 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
31548 /* Parse the initialization statement of either an OpenMP for loop or
31549 a Cilk Plus for loop.
31551 Return true if the resulting construct should have an
31552 OMP_CLAUSE_PRIVATE added to it. */
31554 static bool
31555 cp_parser_omp_for_loop_init (cp_parser *parser,
31556 enum tree_code code,
31557 tree &this_pre_body,
31558 vec<tree, va_gc> *for_block,
31559 tree &init,
31560 tree &decl,
31561 tree &real_decl)
31563 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31564 return false;
31566 bool add_private_clause = false;
31568 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
31570 init-expr:
31571 var = lb
31572 integer-type var = lb
31573 random-access-iterator-type var = lb
31574 pointer-type var = lb
31576 cp_decl_specifier_seq type_specifiers;
31578 /* First, try to parse as an initialized declaration. See
31579 cp_parser_condition, from whence the bulk of this is copied. */
31581 cp_parser_parse_tentatively (parser);
31582 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
31583 /*is_trailing_return=*/false,
31584 &type_specifiers);
31585 if (cp_parser_parse_definitely (parser))
31587 /* If parsing a type specifier seq succeeded, then this
31588 MUST be a initialized declaration. */
31589 tree asm_specification, attributes;
31590 cp_declarator *declarator;
31592 declarator = cp_parser_declarator (parser,
31593 CP_PARSER_DECLARATOR_NAMED,
31594 /*ctor_dtor_or_conv_p=*/NULL,
31595 /*parenthesized_p=*/NULL,
31596 /*member_p=*/false,
31597 /*friend_p=*/false);
31598 attributes = cp_parser_attributes_opt (parser);
31599 asm_specification = cp_parser_asm_specification_opt (parser);
31601 if (declarator == cp_error_declarator)
31602 cp_parser_skip_to_end_of_statement (parser);
31604 else
31606 tree pushed_scope, auto_node;
31608 decl = start_decl (declarator, &type_specifiers,
31609 SD_INITIALIZED, attributes,
31610 /*prefix_attributes=*/NULL_TREE,
31611 &pushed_scope);
31613 auto_node = type_uses_auto (TREE_TYPE (decl));
31614 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31616 if (cp_lexer_next_token_is (parser->lexer,
31617 CPP_OPEN_PAREN))
31619 if (code != CILK_SIMD && code != CILK_FOR)
31620 error ("parenthesized initialization is not allowed in "
31621 "OpenMP %<for%> loop");
31622 else
31623 error ("parenthesized initialization is "
31624 "not allowed in for-loop");
31626 else
31627 /* Trigger an error. */
31628 cp_parser_require (parser, CPP_EQ, RT_EQ);
31630 init = error_mark_node;
31631 cp_parser_skip_to_end_of_statement (parser);
31633 else if (CLASS_TYPE_P (TREE_TYPE (decl))
31634 || type_dependent_expression_p (decl)
31635 || auto_node)
31637 bool is_direct_init, is_non_constant_init;
31639 init = cp_parser_initializer (parser,
31640 &is_direct_init,
31641 &is_non_constant_init);
31643 if (auto_node)
31645 TREE_TYPE (decl)
31646 = do_auto_deduction (TREE_TYPE (decl), init,
31647 auto_node);
31649 if (!CLASS_TYPE_P (TREE_TYPE (decl))
31650 && !type_dependent_expression_p (decl))
31651 goto non_class;
31654 cp_finish_decl (decl, init, !is_non_constant_init,
31655 asm_specification,
31656 LOOKUP_ONLYCONVERTING);
31657 if (CLASS_TYPE_P (TREE_TYPE (decl)))
31659 vec_safe_push (for_block, this_pre_body);
31660 init = NULL_TREE;
31662 else
31663 init = pop_stmt_list (this_pre_body);
31664 this_pre_body = NULL_TREE;
31666 else
31668 /* Consume '='. */
31669 cp_lexer_consume_token (parser->lexer);
31670 init = cp_parser_assignment_expression (parser);
31672 non_class:
31673 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
31674 init = error_mark_node;
31675 else
31676 cp_finish_decl (decl, NULL_TREE,
31677 /*init_const_expr_p=*/false,
31678 asm_specification,
31679 LOOKUP_ONLYCONVERTING);
31682 if (pushed_scope)
31683 pop_scope (pushed_scope);
31686 else
31688 cp_id_kind idk;
31689 /* If parsing a type specifier sequence failed, then
31690 this MUST be a simple expression. */
31691 if (code == CILK_FOR)
31692 error ("%<_Cilk_for%> allows expression instead of declaration only "
31693 "in C, not in C++");
31694 cp_parser_parse_tentatively (parser);
31695 decl = cp_parser_primary_expression (parser, false, false,
31696 false, &idk);
31697 if (!cp_parser_error_occurred (parser)
31698 && decl
31699 && DECL_P (decl)
31700 && CLASS_TYPE_P (TREE_TYPE (decl)))
31702 tree rhs;
31704 cp_parser_parse_definitely (parser);
31705 cp_parser_require (parser, CPP_EQ, RT_EQ);
31706 rhs = cp_parser_assignment_expression (parser);
31707 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
31708 decl, NOP_EXPR,
31709 rhs,
31710 tf_warning_or_error));
31711 add_private_clause = true;
31713 else
31715 decl = NULL;
31716 cp_parser_abort_tentative_parse (parser);
31717 init = cp_parser_expression (parser);
31718 if (init)
31720 if (TREE_CODE (init) == MODIFY_EXPR
31721 || TREE_CODE (init) == MODOP_EXPR)
31722 real_decl = TREE_OPERAND (init, 0);
31726 return add_private_clause;
31729 /* Parse the restricted form of the for statement allowed by OpenMP. */
31731 static tree
31732 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
31733 tree *cclauses)
31735 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
31736 tree real_decl, initv, condv, incrv, declv;
31737 tree this_pre_body, cl;
31738 location_t loc_first;
31739 bool collapse_err = false;
31740 int i, collapse = 1, nbraces = 0;
31741 vec<tree, va_gc> *for_block = make_tree_vector ();
31743 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
31744 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
31745 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
31747 gcc_assert (collapse >= 1);
31749 declv = make_tree_vec (collapse);
31750 initv = make_tree_vec (collapse);
31751 condv = make_tree_vec (collapse);
31752 incrv = make_tree_vec (collapse);
31754 loc_first = cp_lexer_peek_token (parser->lexer)->location;
31756 for (i = 0; i < collapse; i++)
31758 int bracecount = 0;
31759 bool add_private_clause = false;
31760 location_t loc;
31762 if (code != CILK_FOR
31763 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31765 cp_parser_error (parser, "for statement expected");
31766 return NULL;
31768 if (code == CILK_FOR
31769 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
31771 cp_parser_error (parser, "_Cilk_for statement expected");
31772 return NULL;
31774 loc = cp_lexer_consume_token (parser->lexer)->location;
31776 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31777 return NULL;
31779 init = decl = real_decl = NULL;
31780 this_pre_body = push_stmt_list ();
31782 add_private_clause
31783 |= cp_parser_omp_for_loop_init (parser, code,
31784 this_pre_body, for_block,
31785 init, decl, real_decl);
31787 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
31788 if (this_pre_body)
31790 this_pre_body = pop_stmt_list (this_pre_body);
31791 if (pre_body)
31793 tree t = pre_body;
31794 pre_body = push_stmt_list ();
31795 add_stmt (t);
31796 add_stmt (this_pre_body);
31797 pre_body = pop_stmt_list (pre_body);
31799 else
31800 pre_body = this_pre_body;
31803 if (decl)
31804 real_decl = decl;
31805 if (cclauses != NULL
31806 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
31807 && real_decl != NULL_TREE)
31809 tree *c;
31810 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
31811 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
31812 && OMP_CLAUSE_DECL (*c) == real_decl)
31814 error_at (loc, "iteration variable %qD"
31815 " should not be firstprivate", real_decl);
31816 *c = OMP_CLAUSE_CHAIN (*c);
31818 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
31819 && OMP_CLAUSE_DECL (*c) == real_decl)
31821 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
31822 tree l = *c;
31823 *c = OMP_CLAUSE_CHAIN (*c);
31824 if (code == OMP_SIMD)
31826 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
31827 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
31829 else
31831 OMP_CLAUSE_CHAIN (l) = clauses;
31832 clauses = l;
31834 add_private_clause = false;
31836 else
31838 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
31839 && OMP_CLAUSE_DECL (*c) == real_decl)
31840 add_private_clause = false;
31841 c = &OMP_CLAUSE_CHAIN (*c);
31845 if (add_private_clause)
31847 tree c;
31848 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31850 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
31851 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
31852 && OMP_CLAUSE_DECL (c) == decl)
31853 break;
31854 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
31855 && OMP_CLAUSE_DECL (c) == decl)
31856 error_at (loc, "iteration variable %qD "
31857 "should not be firstprivate",
31858 decl);
31859 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
31860 && OMP_CLAUSE_DECL (c) == decl)
31861 error_at (loc, "iteration variable %qD should not be reduction",
31862 decl);
31864 if (c == NULL)
31866 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
31867 OMP_CLAUSE_DECL (c) = decl;
31868 c = finish_omp_clauses (c);
31869 if (c)
31871 OMP_CLAUSE_CHAIN (c) = clauses;
31872 clauses = c;
31877 cond = NULL;
31878 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31879 cond = cp_parser_omp_for_cond (parser, decl, code);
31880 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
31882 incr = NULL;
31883 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
31885 /* If decl is an iterator, preserve the operator on decl
31886 until finish_omp_for. */
31887 if (real_decl
31888 && ((processing_template_decl
31889 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
31890 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
31891 incr = cp_parser_omp_for_incr (parser, real_decl);
31892 else
31893 incr = cp_parser_expression (parser);
31894 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
31895 SET_EXPR_LOCATION (incr, input_location);
31898 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31899 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31900 /*or_comma=*/false,
31901 /*consume_paren=*/true);
31903 TREE_VEC_ELT (declv, i) = decl;
31904 TREE_VEC_ELT (initv, i) = init;
31905 TREE_VEC_ELT (condv, i) = cond;
31906 TREE_VEC_ELT (incrv, i) = incr;
31908 if (i == collapse - 1)
31909 break;
31911 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
31912 in between the collapsed for loops to be still considered perfectly
31913 nested. Hopefully the final version clarifies this.
31914 For now handle (multiple) {'s and empty statements. */
31915 cp_parser_parse_tentatively (parser);
31918 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31919 break;
31920 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
31922 cp_lexer_consume_token (parser->lexer);
31923 bracecount++;
31925 else if (bracecount
31926 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31927 cp_lexer_consume_token (parser->lexer);
31928 else
31930 loc = cp_lexer_peek_token (parser->lexer)->location;
31931 error_at (loc, "not enough collapsed for loops");
31932 collapse_err = true;
31933 cp_parser_abort_tentative_parse (parser);
31934 declv = NULL_TREE;
31935 break;
31938 while (1);
31940 if (declv)
31942 cp_parser_parse_definitely (parser);
31943 nbraces += bracecount;
31947 /* Note that we saved the original contents of this flag when we entered
31948 the structured block, and so we don't need to re-save it here. */
31949 if (code == CILK_SIMD || code == CILK_FOR)
31950 parser->in_statement = IN_CILK_SIMD_FOR;
31951 else
31952 parser->in_statement = IN_OMP_FOR;
31954 /* Note that the grammar doesn't call for a structured block here,
31955 though the loop as a whole is a structured block. */
31956 body = push_stmt_list ();
31957 cp_parser_statement (parser, NULL_TREE, false, NULL);
31958 body = pop_stmt_list (body);
31960 if (declv == NULL_TREE)
31961 ret = NULL_TREE;
31962 else
31963 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
31964 pre_body, clauses);
31966 while (nbraces)
31968 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
31970 cp_lexer_consume_token (parser->lexer);
31971 nbraces--;
31973 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31974 cp_lexer_consume_token (parser->lexer);
31975 else
31977 if (!collapse_err)
31979 error_at (cp_lexer_peek_token (parser->lexer)->location,
31980 "collapsed loops not perfectly nested");
31982 collapse_err = true;
31983 cp_parser_statement_seq_opt (parser, NULL);
31984 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
31985 break;
31989 while (!for_block->is_empty ())
31990 add_stmt (pop_stmt_list (for_block->pop ()));
31991 release_tree_vector (for_block);
31993 return ret;
31996 /* Helper function for OpenMP parsing, split clauses and call
31997 finish_omp_clauses on each of the set of clauses afterwards. */
31999 static void
32000 cp_omp_split_clauses (location_t loc, enum tree_code code,
32001 omp_clause_mask mask, tree clauses, tree *cclauses)
32003 int i;
32004 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
32005 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
32006 if (cclauses[i])
32007 cclauses[i] = finish_omp_clauses (cclauses[i]);
32010 /* OpenMP 4.0:
32011 #pragma omp simd simd-clause[optseq] new-line
32012 for-loop */
32014 #define OMP_SIMD_CLAUSE_MASK \
32015 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
32016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
32017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
32018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
32019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
32020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
32021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
32023 static tree
32024 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
32025 char *p_name, omp_clause_mask mask, tree *cclauses)
32027 tree clauses, sb, ret;
32028 unsigned int save;
32029 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32031 strcat (p_name, " simd");
32032 mask |= OMP_SIMD_CLAUSE_MASK;
32033 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
32035 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
32036 cclauses == NULL);
32037 if (cclauses)
32039 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
32040 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
32043 sb = begin_omp_structured_block ();
32044 save = cp_parser_begin_omp_structured_block (parser);
32046 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
32048 cp_parser_end_omp_structured_block (parser, save);
32049 add_stmt (finish_omp_structured_block (sb));
32051 return ret;
32054 /* OpenMP 2.5:
32055 #pragma omp for for-clause[optseq] new-line
32056 for-loop
32058 OpenMP 4.0:
32059 #pragma omp for simd for-simd-clause[optseq] new-line
32060 for-loop */
32062 #define OMP_FOR_CLAUSE_MASK \
32063 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
32064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
32065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
32066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
32067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
32068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
32069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
32070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
32072 static tree
32073 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
32074 char *p_name, omp_clause_mask mask, tree *cclauses)
32076 tree clauses, sb, ret;
32077 unsigned int save;
32078 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32080 strcat (p_name, " for");
32081 mask |= OMP_FOR_CLAUSE_MASK;
32082 if (cclauses)
32083 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
32085 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32087 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32088 const char *p = IDENTIFIER_POINTER (id);
32090 if (strcmp (p, "simd") == 0)
32092 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
32093 if (cclauses == NULL)
32094 cclauses = cclauses_buf;
32096 cp_lexer_consume_token (parser->lexer);
32097 if (!flag_openmp) /* flag_openmp_simd */
32098 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
32099 cclauses);
32100 sb = begin_omp_structured_block ();
32101 save = cp_parser_begin_omp_structured_block (parser);
32102 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
32103 cclauses);
32104 cp_parser_end_omp_structured_block (parser, save);
32105 tree body = finish_omp_structured_block (sb);
32106 if (ret == NULL)
32107 return ret;
32108 ret = make_node (OMP_FOR);
32109 TREE_TYPE (ret) = void_type_node;
32110 OMP_FOR_BODY (ret) = body;
32111 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
32112 SET_EXPR_LOCATION (ret, loc);
32113 add_stmt (ret);
32114 return ret;
32117 if (!flag_openmp) /* flag_openmp_simd */
32119 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32120 return NULL_TREE;
32123 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
32124 cclauses == NULL);
32125 if (cclauses)
32127 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
32128 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
32131 sb = begin_omp_structured_block ();
32132 save = cp_parser_begin_omp_structured_block (parser);
32134 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
32136 cp_parser_end_omp_structured_block (parser, save);
32137 add_stmt (finish_omp_structured_block (sb));
32139 return ret;
32142 /* OpenMP 2.5:
32143 # pragma omp master new-line
32144 structured-block */
32146 static tree
32147 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
32149 cp_parser_require_pragma_eol (parser, pragma_tok);
32150 return c_finish_omp_master (input_location,
32151 cp_parser_omp_structured_block (parser));
32154 /* OpenMP 2.5:
32155 # pragma omp ordered new-line
32156 structured-block */
32158 static tree
32159 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
32161 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32162 cp_parser_require_pragma_eol (parser, pragma_tok);
32163 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
32166 /* OpenMP 2.5:
32168 section-scope:
32169 { section-sequence }
32171 section-sequence:
32172 section-directive[opt] structured-block
32173 section-sequence section-directive structured-block */
32175 static tree
32176 cp_parser_omp_sections_scope (cp_parser *parser)
32178 tree stmt, substmt;
32179 bool error_suppress = false;
32180 cp_token *tok;
32182 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
32183 return NULL_TREE;
32185 stmt = push_stmt_list ();
32187 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
32189 substmt = cp_parser_omp_structured_block (parser);
32190 substmt = build1 (OMP_SECTION, void_type_node, substmt);
32191 add_stmt (substmt);
32194 while (1)
32196 tok = cp_lexer_peek_token (parser->lexer);
32197 if (tok->type == CPP_CLOSE_BRACE)
32198 break;
32199 if (tok->type == CPP_EOF)
32200 break;
32202 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
32204 cp_lexer_consume_token (parser->lexer);
32205 cp_parser_require_pragma_eol (parser, tok);
32206 error_suppress = false;
32208 else if (!error_suppress)
32210 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
32211 error_suppress = true;
32214 substmt = cp_parser_omp_structured_block (parser);
32215 substmt = build1 (OMP_SECTION, void_type_node, substmt);
32216 add_stmt (substmt);
32218 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
32220 substmt = pop_stmt_list (stmt);
32222 stmt = make_node (OMP_SECTIONS);
32223 TREE_TYPE (stmt) = void_type_node;
32224 OMP_SECTIONS_BODY (stmt) = substmt;
32226 add_stmt (stmt);
32227 return stmt;
32230 /* OpenMP 2.5:
32231 # pragma omp sections sections-clause[optseq] newline
32232 sections-scope */
32234 #define OMP_SECTIONS_CLAUSE_MASK \
32235 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
32236 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
32237 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
32238 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
32239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
32241 static tree
32242 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
32243 char *p_name, omp_clause_mask mask, tree *cclauses)
32245 tree clauses, ret;
32246 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32248 strcat (p_name, " sections");
32249 mask |= OMP_SECTIONS_CLAUSE_MASK;
32250 if (cclauses)
32251 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
32253 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
32254 cclauses == NULL);
32255 if (cclauses)
32257 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
32258 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
32261 ret = cp_parser_omp_sections_scope (parser);
32262 if (ret)
32263 OMP_SECTIONS_CLAUSES (ret) = clauses;
32265 return ret;
32268 /* OpenMP 2.5:
32269 # pragma omp parallel parallel-clause[optseq] new-line
32270 structured-block
32271 # pragma omp parallel for parallel-for-clause[optseq] new-line
32272 structured-block
32273 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
32274 structured-block
32276 OpenMP 4.0:
32277 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
32278 structured-block */
32280 #define OMP_PARALLEL_CLAUSE_MASK \
32281 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
32282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
32283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
32284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
32285 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
32286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
32287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
32288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
32289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
32291 static tree
32292 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
32293 char *p_name, omp_clause_mask mask, tree *cclauses)
32295 tree stmt, clauses, block;
32296 unsigned int save;
32297 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32299 strcat (p_name, " parallel");
32300 mask |= OMP_PARALLEL_CLAUSE_MASK;
32302 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
32304 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
32305 if (cclauses == NULL)
32306 cclauses = cclauses_buf;
32308 cp_lexer_consume_token (parser->lexer);
32309 if (!flag_openmp) /* flag_openmp_simd */
32310 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
32311 block = begin_omp_parallel ();
32312 save = cp_parser_begin_omp_structured_block (parser);
32313 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
32314 cp_parser_end_omp_structured_block (parser, save);
32315 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
32316 block);
32317 if (ret == NULL_TREE)
32318 return ret;
32319 OMP_PARALLEL_COMBINED (stmt) = 1;
32320 return stmt;
32322 else if (cclauses)
32324 error_at (loc, "expected %<for%> after %qs", p_name);
32325 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32326 return NULL_TREE;
32328 else if (!flag_openmp) /* flag_openmp_simd */
32330 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32331 return NULL_TREE;
32333 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32335 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32336 const char *p = IDENTIFIER_POINTER (id);
32337 if (strcmp (p, "sections") == 0)
32339 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
32340 cclauses = cclauses_buf;
32342 cp_lexer_consume_token (parser->lexer);
32343 block = begin_omp_parallel ();
32344 save = cp_parser_begin_omp_structured_block (parser);
32345 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
32346 cp_parser_end_omp_structured_block (parser, save);
32347 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
32348 block);
32349 OMP_PARALLEL_COMBINED (stmt) = 1;
32350 return stmt;
32354 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
32356 block = begin_omp_parallel ();
32357 save = cp_parser_begin_omp_structured_block (parser);
32358 cp_parser_statement (parser, NULL_TREE, false, NULL);
32359 cp_parser_end_omp_structured_block (parser, save);
32360 stmt = finish_omp_parallel (clauses, block);
32361 return stmt;
32364 /* OpenMP 2.5:
32365 # pragma omp single single-clause[optseq] new-line
32366 structured-block */
32368 #define OMP_SINGLE_CLAUSE_MASK \
32369 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
32370 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
32371 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
32372 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
32374 static tree
32375 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
32377 tree stmt = make_node (OMP_SINGLE);
32378 TREE_TYPE (stmt) = void_type_node;
32380 OMP_SINGLE_CLAUSES (stmt)
32381 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
32382 "#pragma omp single", pragma_tok);
32383 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
32385 return add_stmt (stmt);
32388 /* OpenMP 3.0:
32389 # pragma omp task task-clause[optseq] new-line
32390 structured-block */
32392 #define OMP_TASK_CLAUSE_MASK \
32393 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
32394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
32395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
32396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
32397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
32398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
32399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
32400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
32401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
32403 static tree
32404 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
32406 tree clauses, block;
32407 unsigned int save;
32409 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
32410 "#pragma omp task", pragma_tok);
32411 block = begin_omp_task ();
32412 save = cp_parser_begin_omp_structured_block (parser);
32413 cp_parser_statement (parser, NULL_TREE, false, NULL);
32414 cp_parser_end_omp_structured_block (parser, save);
32415 return finish_omp_task (clauses, block);
32418 /* OpenMP 3.0:
32419 # pragma omp taskwait new-line */
32421 static void
32422 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
32424 cp_parser_require_pragma_eol (parser, pragma_tok);
32425 finish_omp_taskwait ();
32428 /* OpenMP 3.1:
32429 # pragma omp taskyield new-line */
32431 static void
32432 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
32434 cp_parser_require_pragma_eol (parser, pragma_tok);
32435 finish_omp_taskyield ();
32438 /* OpenMP 4.0:
32439 # pragma omp taskgroup new-line
32440 structured-block */
32442 static tree
32443 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
32445 cp_parser_require_pragma_eol (parser, pragma_tok);
32446 return c_finish_omp_taskgroup (input_location,
32447 cp_parser_omp_structured_block (parser));
32451 /* OpenMP 2.5:
32452 # pragma omp threadprivate (variable-list) */
32454 static void
32455 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
32457 tree vars;
32459 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
32460 cp_parser_require_pragma_eol (parser, pragma_tok);
32462 finish_omp_threadprivate (vars);
32465 /* OpenMP 4.0:
32466 # pragma omp cancel cancel-clause[optseq] new-line */
32468 #define OMP_CANCEL_CLAUSE_MASK \
32469 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
32470 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
32471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
32472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
32473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
32475 static void
32476 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
32478 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
32479 "#pragma omp cancel", pragma_tok);
32480 finish_omp_cancel (clauses);
32483 /* OpenMP 4.0:
32484 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
32486 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
32487 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
32488 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
32489 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
32490 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
32492 static void
32493 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
32495 tree clauses;
32496 bool point_seen = false;
32498 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32500 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32501 const char *p = IDENTIFIER_POINTER (id);
32503 if (strcmp (p, "point") == 0)
32505 cp_lexer_consume_token (parser->lexer);
32506 point_seen = true;
32509 if (!point_seen)
32511 cp_parser_error (parser, "expected %<point%>");
32512 cp_parser_require_pragma_eol (parser, pragma_tok);
32513 return;
32516 clauses = cp_parser_omp_all_clauses (parser,
32517 OMP_CANCELLATION_POINT_CLAUSE_MASK,
32518 "#pragma omp cancellation point",
32519 pragma_tok);
32520 finish_omp_cancellation_point (clauses);
32523 /* OpenMP 4.0:
32524 #pragma omp distribute distribute-clause[optseq] new-line
32525 for-loop */
32527 #define OMP_DISTRIBUTE_CLAUSE_MASK \
32528 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
32529 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
32530 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
32531 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
32533 static tree
32534 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
32535 char *p_name, omp_clause_mask mask, tree *cclauses)
32537 tree clauses, sb, ret;
32538 unsigned int save;
32539 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32541 strcat (p_name, " distribute");
32542 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
32544 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32546 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32547 const char *p = IDENTIFIER_POINTER (id);
32548 bool simd = false;
32549 bool parallel = false;
32551 if (strcmp (p, "simd") == 0)
32552 simd = true;
32553 else
32554 parallel = strcmp (p, "parallel") == 0;
32555 if (parallel || simd)
32557 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
32558 if (cclauses == NULL)
32559 cclauses = cclauses_buf;
32560 cp_lexer_consume_token (parser->lexer);
32561 if (!flag_openmp) /* flag_openmp_simd */
32563 if (simd)
32564 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
32565 cclauses);
32566 else
32567 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
32568 cclauses);
32570 sb = begin_omp_structured_block ();
32571 save = cp_parser_begin_omp_structured_block (parser);
32572 if (simd)
32573 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
32574 cclauses);
32575 else
32576 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
32577 cclauses);
32578 cp_parser_end_omp_structured_block (parser, save);
32579 tree body = finish_omp_structured_block (sb);
32580 if (ret == NULL)
32581 return ret;
32582 ret = make_node (OMP_DISTRIBUTE);
32583 TREE_TYPE (ret) = void_type_node;
32584 OMP_FOR_BODY (ret) = body;
32585 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
32586 SET_EXPR_LOCATION (ret, loc);
32587 add_stmt (ret);
32588 return ret;
32591 if (!flag_openmp) /* flag_openmp_simd */
32593 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32594 return NULL_TREE;
32597 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
32598 cclauses == NULL);
32599 if (cclauses)
32601 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
32602 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
32605 sb = begin_omp_structured_block ();
32606 save = cp_parser_begin_omp_structured_block (parser);
32608 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
32610 cp_parser_end_omp_structured_block (parser, save);
32611 add_stmt (finish_omp_structured_block (sb));
32613 return ret;
32616 /* OpenMP 4.0:
32617 # pragma omp teams teams-clause[optseq] new-line
32618 structured-block */
32620 #define OMP_TEAMS_CLAUSE_MASK \
32621 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
32622 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
32623 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
32624 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
32625 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
32626 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
32627 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
32629 static tree
32630 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
32631 char *p_name, omp_clause_mask mask, tree *cclauses)
32633 tree clauses, sb, ret;
32634 unsigned int save;
32635 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32637 strcat (p_name, " teams");
32638 mask |= OMP_TEAMS_CLAUSE_MASK;
32640 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32642 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32643 const char *p = IDENTIFIER_POINTER (id);
32644 if (strcmp (p, "distribute") == 0)
32646 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
32647 if (cclauses == NULL)
32648 cclauses = cclauses_buf;
32650 cp_lexer_consume_token (parser->lexer);
32651 if (!flag_openmp) /* flag_openmp_simd */
32652 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
32653 cclauses);
32654 sb = begin_omp_structured_block ();
32655 save = cp_parser_begin_omp_structured_block (parser);
32656 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
32657 cclauses);
32658 cp_parser_end_omp_structured_block (parser, save);
32659 tree body = finish_omp_structured_block (sb);
32660 if (ret == NULL)
32661 return ret;
32662 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
32663 ret = make_node (OMP_TEAMS);
32664 TREE_TYPE (ret) = void_type_node;
32665 OMP_TEAMS_CLAUSES (ret) = clauses;
32666 OMP_TEAMS_BODY (ret) = body;
32667 OMP_TEAMS_COMBINED (ret) = 1;
32668 return add_stmt (ret);
32671 if (!flag_openmp) /* flag_openmp_simd */
32673 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32674 return NULL_TREE;
32677 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
32678 cclauses == NULL);
32679 if (cclauses)
32681 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
32682 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
32685 tree stmt = make_node (OMP_TEAMS);
32686 TREE_TYPE (stmt) = void_type_node;
32687 OMP_TEAMS_CLAUSES (stmt) = clauses;
32688 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
32690 return add_stmt (stmt);
32693 /* OpenMP 4.0:
32694 # pragma omp target data target-data-clause[optseq] new-line
32695 structured-block */
32697 #define OMP_TARGET_DATA_CLAUSE_MASK \
32698 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
32699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
32700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
32702 static tree
32703 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
32705 tree stmt = make_node (OMP_TARGET_DATA);
32706 TREE_TYPE (stmt) = void_type_node;
32708 OMP_TARGET_DATA_CLAUSES (stmt)
32709 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
32710 "#pragma omp target data", pragma_tok);
32711 keep_next_level (true);
32712 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
32714 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32715 return add_stmt (stmt);
32718 /* OpenMP 4.0:
32719 # pragma omp target update target-update-clause[optseq] new-line */
32721 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
32722 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
32723 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
32724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
32725 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
32727 static bool
32728 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
32729 enum pragma_context context)
32731 if (context == pragma_stmt)
32733 error_at (pragma_tok->location,
32734 "%<#pragma omp target update%> may only be "
32735 "used in compound statements");
32736 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32737 return false;
32740 tree clauses
32741 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
32742 "#pragma omp target update", pragma_tok);
32743 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
32744 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
32746 error_at (pragma_tok->location,
32747 "%<#pragma omp target update%> must contain at least one "
32748 "%<from%> or %<to%> clauses");
32749 return false;
32752 tree stmt = make_node (OMP_TARGET_UPDATE);
32753 TREE_TYPE (stmt) = void_type_node;
32754 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
32755 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32756 add_stmt (stmt);
32757 return false;
32760 /* OpenMP 4.0:
32761 # pragma omp target target-clause[optseq] new-line
32762 structured-block */
32764 #define OMP_TARGET_CLAUSE_MASK \
32765 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
32766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
32767 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
32769 static bool
32770 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
32771 enum pragma_context context)
32773 if (context != pragma_stmt && context != pragma_compound)
32775 cp_parser_error (parser, "expected declaration specifiers");
32776 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32777 return false;
32780 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32782 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32783 const char *p = IDENTIFIER_POINTER (id);
32785 if (strcmp (p, "teams") == 0)
32787 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
32788 char p_name[sizeof ("#pragma omp target teams distribute "
32789 "parallel for simd")];
32791 cp_lexer_consume_token (parser->lexer);
32792 strcpy (p_name, "#pragma omp target");
32793 if (!flag_openmp) /* flag_openmp_simd */
32795 tree stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
32796 OMP_TARGET_CLAUSE_MASK,
32797 cclauses);
32798 return stmt != NULL_TREE;
32800 keep_next_level (true);
32801 tree sb = begin_omp_structured_block ();
32802 unsigned save = cp_parser_begin_omp_structured_block (parser);
32803 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
32804 OMP_TARGET_CLAUSE_MASK, cclauses);
32805 cp_parser_end_omp_structured_block (parser, save);
32806 tree body = finish_omp_structured_block (sb);
32807 if (ret == NULL_TREE)
32808 return false;
32809 tree stmt = make_node (OMP_TARGET);
32810 TREE_TYPE (stmt) = void_type_node;
32811 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
32812 OMP_TARGET_BODY (stmt) = body;
32813 add_stmt (stmt);
32814 return true;
32816 else if (!flag_openmp) /* flag_openmp_simd */
32818 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32819 return false;
32821 else if (strcmp (p, "data") == 0)
32823 cp_lexer_consume_token (parser->lexer);
32824 cp_parser_omp_target_data (parser, pragma_tok);
32825 return true;
32827 else if (strcmp (p, "update") == 0)
32829 cp_lexer_consume_token (parser->lexer);
32830 return cp_parser_omp_target_update (parser, pragma_tok, context);
32834 tree stmt = make_node (OMP_TARGET);
32835 TREE_TYPE (stmt) = void_type_node;
32837 OMP_TARGET_CLAUSES (stmt)
32838 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
32839 "#pragma omp target", pragma_tok);
32840 keep_next_level (true);
32841 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
32843 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32844 add_stmt (stmt);
32845 return true;
32848 /* OpenACC 2.0:
32849 # pragma acc cache (variable-list) new-line
32852 static tree
32853 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
32855 tree stmt, clauses;
32857 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
32858 clauses = finish_omp_clauses (clauses);
32860 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
32862 stmt = make_node (OACC_CACHE);
32863 TREE_TYPE (stmt) = void_type_node;
32864 OACC_CACHE_CLAUSES (stmt) = clauses;
32865 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32866 add_stmt (stmt);
32868 return stmt;
32871 /* OpenACC 2.0:
32872 # pragma acc data oacc-data-clause[optseq] new-line
32873 structured-block */
32875 #define OACC_DATA_CLAUSE_MASK \
32876 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
32877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
32878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
32879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
32880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
32881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
32882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
32883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
32884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
32885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
32886 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
32888 static tree
32889 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok)
32891 tree stmt, clauses, block;
32892 unsigned int save;
32894 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
32895 "#pragma acc data", pragma_tok);
32897 block = begin_omp_parallel ();
32898 save = cp_parser_begin_omp_structured_block (parser);
32899 cp_parser_statement (parser, NULL_TREE, false, NULL);
32900 cp_parser_end_omp_structured_block (parser, save);
32901 stmt = finish_oacc_data (clauses, block);
32902 return stmt;
32905 /* OpenACC 2.0:
32906 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
32910 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
32912 LOC is the location of the #pragma token.
32915 #define OACC_ENTER_DATA_CLAUSE_MASK \
32916 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
32917 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
32918 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
32919 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
32920 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
32921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
32922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
32924 #define OACC_EXIT_DATA_CLAUSE_MASK \
32925 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
32926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
32927 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
32928 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
32929 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
32931 static tree
32932 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
32933 bool enter)
32935 tree stmt, clauses;
32937 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
32938 || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32940 cp_parser_error (parser, enter
32941 ? "expected %<data%> in %<#pragma acc enter data%>"
32942 : "expected %<data%> in %<#pragma acc exit data%>");
32943 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32944 return NULL_TREE;
32947 const char *p =
32948 IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
32949 if (strcmp (p, "data") != 0)
32951 cp_parser_error (parser, "invalid pragma");
32952 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32953 return NULL_TREE;
32956 cp_lexer_consume_token (parser->lexer);
32958 if (enter)
32959 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
32960 "#pragma acc enter data", pragma_tok);
32961 else
32962 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
32963 "#pragma acc exit data", pragma_tok);
32965 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
32967 error_at (pragma_tok->location,
32968 "%<#pragma acc enter data%> has no data movement clause");
32969 return NULL_TREE;
32972 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
32973 TREE_TYPE (stmt) = void_type_node;
32974 OMP_STANDALONE_CLAUSES (stmt) = clauses;
32975 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32976 add_stmt (stmt);
32977 return stmt;
32980 /* OpenACC 2.0:
32981 # pragma acc kernels oacc-kernels-clause[optseq] new-line
32982 structured-block */
32984 #define OACC_KERNELS_CLAUSE_MASK \
32985 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
32986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
32987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
32988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
32989 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
32990 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
32991 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
32992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
32993 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
32994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
32995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
32996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
32997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
32999 static tree
33000 cp_parser_oacc_kernels (cp_parser *parser, cp_token *pragma_tok)
33002 tree stmt, clauses, block;
33003 unsigned int save;
33005 clauses = cp_parser_oacc_all_clauses (parser, OACC_KERNELS_CLAUSE_MASK,
33006 "#pragma acc kernels", pragma_tok);
33008 block = begin_omp_parallel ();
33009 save = cp_parser_begin_omp_structured_block (parser);
33010 cp_parser_statement (parser, NULL_TREE, false, NULL);
33011 cp_parser_end_omp_structured_block (parser, save);
33012 stmt = finish_oacc_kernels (clauses, block);
33013 return stmt;
33016 /* OpenACC 2.0:
33017 # pragma acc loop oacc-loop-clause[optseq] new-line
33018 structured-block */
33020 #define OACC_LOOP_CLAUSE_MASK \
33021 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
33022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION))
33024 static tree
33025 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok)
33027 tree stmt, clauses, block;
33028 int save;
33030 clauses = cp_parser_oacc_all_clauses (parser, OACC_LOOP_CLAUSE_MASK,
33031 "#pragma acc loop", pragma_tok);
33033 block = begin_omp_structured_block ();
33034 save = cp_parser_begin_omp_structured_block (parser);
33035 stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL);
33036 cp_parser_end_omp_structured_block (parser, save);
33037 add_stmt (finish_omp_structured_block (block));
33038 return stmt;
33041 /* OpenACC 2.0:
33042 # pragma acc parallel oacc-parallel-clause[optseq] new-line
33043 structured-block */
33045 #define OACC_PARALLEL_CLAUSE_MASK \
33046 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
33047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
33048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
33049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
33050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
33051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
33052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
33053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
33054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
33055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
33056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
33057 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
33058 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
33059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
33060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
33061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
33062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
33064 static tree
33065 cp_parser_oacc_parallel (cp_parser *parser, cp_token *pragma_tok)
33067 tree stmt, clauses, block;
33068 unsigned int save;
33070 clauses = cp_parser_oacc_all_clauses (parser, OACC_PARALLEL_CLAUSE_MASK,
33071 "#pragma acc parallel", pragma_tok);
33073 block = begin_omp_parallel ();
33074 save = cp_parser_begin_omp_structured_block (parser);
33075 cp_parser_statement (parser, NULL_TREE, false, NULL);
33076 cp_parser_end_omp_structured_block (parser, save);
33077 stmt = finish_oacc_parallel (clauses, block);
33078 return stmt;
33081 /* OpenACC 2.0:
33082 # pragma acc update oacc-update-clause[optseq] new-line
33085 #define OACC_UPDATE_CLAUSE_MASK \
33086 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
33087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
33088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
33089 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
33090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
33091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
33093 static tree
33094 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
33096 tree stmt, clauses;
33098 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
33099 "#pragma acc update", pragma_tok);
33101 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
33103 error_at (pragma_tok->location,
33104 "%<#pragma acc update%> must contain at least one "
33105 "%<device%> or %<host/self%> clause");
33106 return NULL_TREE;
33109 stmt = make_node (OACC_UPDATE);
33110 TREE_TYPE (stmt) = void_type_node;
33111 OACC_UPDATE_CLAUSES (stmt) = clauses;
33112 SET_EXPR_LOCATION (stmt, pragma_tok->location);
33113 add_stmt (stmt);
33114 return stmt;
33117 /* OpenACC 2.0:
33118 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
33120 LOC is the location of the #pragma token.
33123 #define OACC_WAIT_CLAUSE_MASK \
33124 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
33126 static tree
33127 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
33129 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
33130 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33132 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33133 list = cp_parser_oacc_wait_list (parser, loc, list);
33135 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
33136 "#pragma acc wait", pragma_tok);
33138 stmt = c_finish_oacc_wait (loc, list, clauses);
33140 return stmt;
33143 /* OpenMP 4.0:
33144 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
33146 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
33147 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
33148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
33149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
33150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
33151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
33152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
33154 static void
33155 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
33156 enum pragma_context context)
33158 bool first_p = parser->omp_declare_simd == NULL;
33159 cp_omp_declare_simd_data data;
33160 if (first_p)
33162 data.error_seen = false;
33163 data.fndecl_seen = false;
33164 data.tokens = vNULL;
33165 parser->omp_declare_simd = &data;
33167 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
33168 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
33169 cp_lexer_consume_token (parser->lexer);
33170 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33171 parser->omp_declare_simd->error_seen = true;
33172 cp_parser_require_pragma_eol (parser, pragma_tok);
33173 struct cp_token_cache *cp
33174 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
33175 parser->omp_declare_simd->tokens.safe_push (cp);
33176 if (first_p)
33178 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
33179 cp_parser_pragma (parser, context);
33180 switch (context)
33182 case pragma_external:
33183 cp_parser_declaration (parser);
33184 break;
33185 case pragma_member:
33186 cp_parser_member_declaration (parser);
33187 break;
33188 case pragma_objc_icode:
33189 cp_parser_block_declaration (parser, /*statement_p=*/false);
33190 break;
33191 default:
33192 cp_parser_declaration_statement (parser);
33193 break;
33195 if (parser->omp_declare_simd
33196 && !parser->omp_declare_simd->error_seen
33197 && !parser->omp_declare_simd->fndecl_seen)
33198 error_at (pragma_tok->location,
33199 "%<#pragma omp declare simd%> not immediately followed by "
33200 "function declaration or definition");
33201 data.tokens.release ();
33202 parser->omp_declare_simd = NULL;
33206 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
33207 This function is modelled similar to the late parsing of omp declare
33208 simd. */
33210 static tree
33211 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
33213 struct cp_token_cache *ce;
33214 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
33215 int ii = 0;
33217 if (parser->omp_declare_simd != NULL)
33219 error ("%<#pragma omp declare simd%> cannot be used in the same function"
33220 " marked as a Cilk Plus SIMD-enabled function");
33221 XDELETE (parser->cilk_simd_fn_info);
33222 parser->cilk_simd_fn_info = NULL;
33223 return attrs;
33225 if (!info->error_seen && info->fndecl_seen)
33227 error ("vector attribute not immediately followed by a single function"
33228 " declaration or definition");
33229 info->error_seen = true;
33231 if (info->error_seen)
33232 return attrs;
33234 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
33236 tree c, cl;
33238 cp_parser_push_lexer_for_tokens (parser, ce);
33239 parser->lexer->in_pragma = true;
33240 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
33241 "SIMD-enabled functions attribute",
33242 NULL);
33243 cp_parser_pop_lexer (parser);
33244 if (cl)
33245 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
33247 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
33248 TREE_CHAIN (c) = attrs;
33249 attrs = c;
33251 c = build_tree_list (get_identifier ("omp declare simd"), cl);
33252 TREE_CHAIN (c) = attrs;
33253 if (processing_template_decl)
33254 ATTR_IS_DEPENDENT (c) = 1;
33255 attrs = c;
33257 info->fndecl_seen = true;
33258 XDELETE (parser->cilk_simd_fn_info);
33259 parser->cilk_simd_fn_info = NULL;
33260 return attrs;
33263 /* Finalize #pragma omp declare simd clauses after direct declarator has
33264 been parsed, and put that into "omp declare simd" attribute. */
33266 static tree
33267 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
33269 struct cp_token_cache *ce;
33270 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
33271 int i;
33273 if (!data->error_seen && data->fndecl_seen)
33275 error ("%<#pragma omp declare simd%> not immediately followed by "
33276 "a single function declaration or definition");
33277 data->error_seen = true;
33278 return attrs;
33280 if (data->error_seen)
33281 return attrs;
33283 FOR_EACH_VEC_ELT (data->tokens, i, ce)
33285 tree c, cl;
33287 cp_parser_push_lexer_for_tokens (parser, ce);
33288 parser->lexer->in_pragma = true;
33289 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
33290 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
33291 cp_lexer_consume_token (parser->lexer);
33292 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
33293 "#pragma omp declare simd", pragma_tok);
33294 cp_parser_pop_lexer (parser);
33295 if (cl)
33296 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
33297 c = build_tree_list (get_identifier ("omp declare simd"), cl);
33298 TREE_CHAIN (c) = attrs;
33299 if (processing_template_decl)
33300 ATTR_IS_DEPENDENT (c) = 1;
33301 attrs = c;
33304 data->fndecl_seen = true;
33305 return attrs;
33309 /* OpenMP 4.0:
33310 # pragma omp declare target new-line
33311 declarations and definitions
33312 # pragma omp end declare target new-line */
33314 static void
33315 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
33317 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33318 scope_chain->omp_declare_target_attribute++;
33321 static void
33322 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
33324 const char *p = "";
33325 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33327 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33328 p = IDENTIFIER_POINTER (id);
33330 if (strcmp (p, "declare") == 0)
33332 cp_lexer_consume_token (parser->lexer);
33333 p = "";
33334 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33336 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33337 p = IDENTIFIER_POINTER (id);
33339 if (strcmp (p, "target") == 0)
33340 cp_lexer_consume_token (parser->lexer);
33341 else
33343 cp_parser_error (parser, "expected %<target%>");
33344 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33345 return;
33348 else
33350 cp_parser_error (parser, "expected %<declare%>");
33351 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33352 return;
33354 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33355 if (!scope_chain->omp_declare_target_attribute)
33356 error_at (pragma_tok->location,
33357 "%<#pragma omp end declare target%> without corresponding "
33358 "%<#pragma omp declare target%>");
33359 else
33360 scope_chain->omp_declare_target_attribute--;
33363 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
33364 expression and optional initializer clause of
33365 #pragma omp declare reduction. We store the expression(s) as
33366 either 3, 6 or 7 special statements inside of the artificial function's
33367 body. The first two statements are DECL_EXPRs for the artificial
33368 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
33369 expression that uses those variables.
33370 If there was any INITIALIZER clause, this is followed by further statements,
33371 the fourth and fifth statements are DECL_EXPRs for the artificial
33372 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
33373 constructor variant (first token after open paren is not omp_priv),
33374 then the sixth statement is a statement with the function call expression
33375 that uses the OMP_PRIV and optionally OMP_ORIG variable.
33376 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
33377 to initialize the OMP_PRIV artificial variable and there is seventh
33378 statement, a DECL_EXPR of the OMP_PRIV statement again. */
33380 static bool
33381 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
33383 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
33384 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
33385 type = TREE_TYPE (type);
33386 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
33387 DECL_ARTIFICIAL (omp_out) = 1;
33388 pushdecl (omp_out);
33389 add_decl_expr (omp_out);
33390 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
33391 DECL_ARTIFICIAL (omp_in) = 1;
33392 pushdecl (omp_in);
33393 add_decl_expr (omp_in);
33394 tree combiner;
33395 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
33397 keep_next_level (true);
33398 tree block = begin_omp_structured_block ();
33399 combiner = cp_parser_expression (parser);
33400 finish_expr_stmt (combiner);
33401 block = finish_omp_structured_block (block);
33402 add_stmt (block);
33404 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33405 return false;
33407 const char *p = "";
33408 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33410 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33411 p = IDENTIFIER_POINTER (id);
33414 if (strcmp (p, "initializer") == 0)
33416 cp_lexer_consume_token (parser->lexer);
33417 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33418 return false;
33420 p = "";
33421 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33423 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33424 p = IDENTIFIER_POINTER (id);
33427 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
33428 DECL_ARTIFICIAL (omp_priv) = 1;
33429 pushdecl (omp_priv);
33430 add_decl_expr (omp_priv);
33431 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
33432 DECL_ARTIFICIAL (omp_orig) = 1;
33433 pushdecl (omp_orig);
33434 add_decl_expr (omp_orig);
33436 keep_next_level (true);
33437 block = begin_omp_structured_block ();
33439 bool ctor = false;
33440 if (strcmp (p, "omp_priv") == 0)
33442 bool is_direct_init, is_non_constant_init;
33443 ctor = true;
33444 cp_lexer_consume_token (parser->lexer);
33445 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
33446 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
33447 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
33448 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
33449 == CPP_CLOSE_PAREN
33450 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
33451 == CPP_CLOSE_PAREN))
33453 finish_omp_structured_block (block);
33454 error ("invalid initializer clause");
33455 return false;
33457 initializer = cp_parser_initializer (parser, &is_direct_init,
33458 &is_non_constant_init);
33459 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
33460 NULL_TREE, LOOKUP_ONLYCONVERTING);
33462 else
33464 cp_parser_parse_tentatively (parser);
33465 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
33466 /*check_dependency_p=*/true,
33467 /*template_p=*/NULL,
33468 /*declarator_p=*/false,
33469 /*optional_p=*/false);
33470 vec<tree, va_gc> *args;
33471 if (fn_name == error_mark_node
33472 || cp_parser_error_occurred (parser)
33473 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
33474 || ((args = cp_parser_parenthesized_expression_list
33475 (parser, non_attr, /*cast_p=*/false,
33476 /*allow_expansion_p=*/true,
33477 /*non_constant_p=*/NULL)),
33478 cp_parser_error_occurred (parser)))
33480 finish_omp_structured_block (block);
33481 cp_parser_abort_tentative_parse (parser);
33482 cp_parser_error (parser, "expected id-expression (arguments)");
33483 return false;
33485 unsigned int i;
33486 tree arg;
33487 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
33488 if (arg == omp_priv
33489 || (TREE_CODE (arg) == ADDR_EXPR
33490 && TREE_OPERAND (arg, 0) == omp_priv))
33491 break;
33492 cp_parser_abort_tentative_parse (parser);
33493 if (arg == NULL_TREE)
33494 error ("one of the initializer call arguments should be %<omp_priv%>"
33495 " or %<&omp_priv%>");
33496 initializer = cp_parser_postfix_expression (parser, false, false, false,
33497 false, NULL);
33498 finish_expr_stmt (initializer);
33501 block = finish_omp_structured_block (block);
33502 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
33503 add_stmt (block);
33505 if (ctor)
33506 add_decl_expr (omp_orig);
33508 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33509 return false;
33512 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
33513 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
33515 return true;
33518 /* OpenMP 4.0
33519 #pragma omp declare reduction (reduction-id : typename-list : expression) \
33520 initializer-clause[opt] new-line
33522 initializer-clause:
33523 initializer (omp_priv initializer)
33524 initializer (function-name (argument-list)) */
33526 static void
33527 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
33528 enum pragma_context)
33530 auto_vec<tree> types;
33531 enum tree_code reduc_code = ERROR_MARK;
33532 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
33533 unsigned int i;
33534 cp_token *first_token;
33535 cp_token_cache *cp;
33536 int errs;
33537 void *p;
33539 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
33540 p = obstack_alloc (&declarator_obstack, 0);
33542 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33543 goto fail;
33545 switch (cp_lexer_peek_token (parser->lexer)->type)
33547 case CPP_PLUS:
33548 reduc_code = PLUS_EXPR;
33549 break;
33550 case CPP_MULT:
33551 reduc_code = MULT_EXPR;
33552 break;
33553 case CPP_MINUS:
33554 reduc_code = MINUS_EXPR;
33555 break;
33556 case CPP_AND:
33557 reduc_code = BIT_AND_EXPR;
33558 break;
33559 case CPP_XOR:
33560 reduc_code = BIT_XOR_EXPR;
33561 break;
33562 case CPP_OR:
33563 reduc_code = BIT_IOR_EXPR;
33564 break;
33565 case CPP_AND_AND:
33566 reduc_code = TRUTH_ANDIF_EXPR;
33567 break;
33568 case CPP_OR_OR:
33569 reduc_code = TRUTH_ORIF_EXPR;
33570 break;
33571 case CPP_NAME:
33572 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
33573 break;
33574 default:
33575 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
33576 "%<|%>, %<&&%>, %<||%> or identifier");
33577 goto fail;
33580 if (reduc_code != ERROR_MARK)
33581 cp_lexer_consume_token (parser->lexer);
33583 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
33584 if (reduc_id == error_mark_node)
33585 goto fail;
33587 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33588 goto fail;
33590 /* Types may not be defined in declare reduction type list. */
33591 const char *saved_message;
33592 saved_message = parser->type_definition_forbidden_message;
33593 parser->type_definition_forbidden_message
33594 = G_("types may not be defined in declare reduction type list");
33595 bool saved_colon_corrects_to_scope_p;
33596 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33597 parser->colon_corrects_to_scope_p = false;
33598 bool saved_colon_doesnt_start_class_def_p;
33599 saved_colon_doesnt_start_class_def_p
33600 = parser->colon_doesnt_start_class_def_p;
33601 parser->colon_doesnt_start_class_def_p = true;
33603 while (true)
33605 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33606 type = cp_parser_type_id (parser);
33607 if (type == error_mark_node)
33609 else if (ARITHMETIC_TYPE_P (type)
33610 && (orig_reduc_id == NULL_TREE
33611 || (TREE_CODE (type) != COMPLEX_TYPE
33612 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
33613 "min") == 0
33614 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
33615 "max") == 0))))
33616 error_at (loc, "predeclared arithmetic type %qT in "
33617 "%<#pragma omp declare reduction%>", type);
33618 else if (TREE_CODE (type) == FUNCTION_TYPE
33619 || TREE_CODE (type) == METHOD_TYPE
33620 || TREE_CODE (type) == ARRAY_TYPE)
33621 error_at (loc, "function or array type %qT in "
33622 "%<#pragma omp declare reduction%>", type);
33623 else if (TREE_CODE (type) == REFERENCE_TYPE)
33624 error_at (loc, "reference type %qT in "
33625 "%<#pragma omp declare reduction%>", type);
33626 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
33627 error_at (loc, "const, volatile or __restrict qualified type %qT in "
33628 "%<#pragma omp declare reduction%>", type);
33629 else
33630 types.safe_push (type);
33632 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33633 cp_lexer_consume_token (parser->lexer);
33634 else
33635 break;
33638 /* Restore the saved message. */
33639 parser->type_definition_forbidden_message = saved_message;
33640 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33641 parser->colon_doesnt_start_class_def_p
33642 = saved_colon_doesnt_start_class_def_p;
33644 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
33645 || types.is_empty ())
33647 fail:
33648 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33649 goto done;
33652 first_token = cp_lexer_peek_token (parser->lexer);
33653 cp = NULL;
33654 errs = errorcount;
33655 FOR_EACH_VEC_ELT (types, i, type)
33657 tree fntype
33658 = build_function_type_list (void_type_node,
33659 cp_build_reference_type (type, false),
33660 NULL_TREE);
33661 tree this_reduc_id = reduc_id;
33662 if (!dependent_type_p (type))
33663 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
33664 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
33665 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
33666 DECL_ARTIFICIAL (fndecl) = 1;
33667 DECL_EXTERNAL (fndecl) = 1;
33668 DECL_DECLARED_INLINE_P (fndecl) = 1;
33669 DECL_IGNORED_P (fndecl) = 1;
33670 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
33671 DECL_ATTRIBUTES (fndecl)
33672 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
33673 DECL_ATTRIBUTES (fndecl));
33674 if (processing_template_decl)
33675 fndecl = push_template_decl (fndecl);
33676 bool block_scope = false;
33677 tree block = NULL_TREE;
33678 if (current_function_decl)
33680 block_scope = true;
33681 DECL_CONTEXT (fndecl) = global_namespace;
33682 if (!processing_template_decl)
33683 pushdecl (fndecl);
33685 else if (current_class_type)
33687 if (cp == NULL)
33689 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
33690 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
33691 cp_lexer_consume_token (parser->lexer);
33692 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33693 goto fail;
33694 cp = cp_token_cache_new (first_token,
33695 cp_lexer_peek_nth_token (parser->lexer,
33696 2));
33698 DECL_STATIC_FUNCTION_P (fndecl) = 1;
33699 finish_member_declaration (fndecl);
33700 DECL_PENDING_INLINE_INFO (fndecl) = cp;
33701 DECL_PENDING_INLINE_P (fndecl) = 1;
33702 vec_safe_push (unparsed_funs_with_definitions, fndecl);
33703 continue;
33705 else
33707 DECL_CONTEXT (fndecl) = current_namespace;
33708 pushdecl (fndecl);
33710 if (!block_scope)
33711 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
33712 else
33713 block = begin_omp_structured_block ();
33714 if (cp)
33716 cp_parser_push_lexer_for_tokens (parser, cp);
33717 parser->lexer->in_pragma = true;
33719 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
33721 if (!block_scope)
33722 finish_function (0);
33723 else
33724 DECL_CONTEXT (fndecl) = current_function_decl;
33725 if (cp)
33726 cp_parser_pop_lexer (parser);
33727 goto fail;
33729 if (cp)
33730 cp_parser_pop_lexer (parser);
33731 if (!block_scope)
33732 finish_function (0);
33733 else
33735 DECL_CONTEXT (fndecl) = current_function_decl;
33736 block = finish_omp_structured_block (block);
33737 if (TREE_CODE (block) == BIND_EXPR)
33738 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
33739 else if (TREE_CODE (block) == STATEMENT_LIST)
33740 DECL_SAVED_TREE (fndecl) = block;
33741 if (processing_template_decl)
33742 add_decl_expr (fndecl);
33744 cp_check_omp_declare_reduction (fndecl);
33745 if (cp == NULL && types.length () > 1)
33746 cp = cp_token_cache_new (first_token,
33747 cp_lexer_peek_nth_token (parser->lexer, 2));
33748 if (errs != errorcount)
33749 break;
33752 cp_parser_require_pragma_eol (parser, pragma_tok);
33754 done:
33755 /* Free any declarators allocated. */
33756 obstack_free (&declarator_obstack, p);
33759 /* OpenMP 4.0
33760 #pragma omp declare simd declare-simd-clauses[optseq] new-line
33761 #pragma omp declare reduction (reduction-id : typename-list : expression) \
33762 initializer-clause[opt] new-line
33763 #pragma omp declare target new-line */
33765 static void
33766 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
33767 enum pragma_context context)
33769 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33771 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33772 const char *p = IDENTIFIER_POINTER (id);
33774 if (strcmp (p, "simd") == 0)
33776 cp_lexer_consume_token (parser->lexer);
33777 cp_parser_omp_declare_simd (parser, pragma_tok,
33778 context);
33779 return;
33781 cp_ensure_no_omp_declare_simd (parser);
33782 if (strcmp (p, "reduction") == 0)
33784 cp_lexer_consume_token (parser->lexer);
33785 cp_parser_omp_declare_reduction (parser, pragma_tok,
33786 context);
33787 return;
33789 if (!flag_openmp) /* flag_openmp_simd */
33791 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33792 return;
33794 if (strcmp (p, "target") == 0)
33796 cp_lexer_consume_token (parser->lexer);
33797 cp_parser_omp_declare_target (parser, pragma_tok);
33798 return;
33801 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
33802 "or %<target%>");
33803 cp_parser_require_pragma_eol (parser, pragma_tok);
33806 /* Main entry point to OpenMP statement pragmas. */
33808 static void
33809 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
33811 tree stmt;
33812 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
33813 omp_clause_mask mask (0);
33815 switch (pragma_tok->pragma_kind)
33817 case PRAGMA_OACC_CACHE:
33818 stmt = cp_parser_oacc_cache (parser, pragma_tok);
33819 break;
33820 case PRAGMA_OACC_DATA:
33821 stmt = cp_parser_oacc_data (parser, pragma_tok);
33822 break;
33823 case PRAGMA_OACC_ENTER_DATA:
33824 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
33825 break;
33826 case PRAGMA_OACC_EXIT_DATA:
33827 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
33828 break;
33829 case PRAGMA_OACC_KERNELS:
33830 stmt = cp_parser_oacc_kernels (parser, pragma_tok);
33831 break;
33832 case PRAGMA_OACC_LOOP:
33833 stmt = cp_parser_oacc_loop (parser, pragma_tok);
33834 break;
33835 case PRAGMA_OACC_PARALLEL:
33836 stmt = cp_parser_oacc_parallel (parser, pragma_tok);
33837 break;
33838 case PRAGMA_OACC_UPDATE:
33839 stmt = cp_parser_oacc_update (parser, pragma_tok);
33840 break;
33841 case PRAGMA_OACC_WAIT:
33842 stmt = cp_parser_oacc_wait (parser, pragma_tok);
33843 break;
33844 case PRAGMA_OMP_ATOMIC:
33845 cp_parser_omp_atomic (parser, pragma_tok);
33846 return;
33847 case PRAGMA_OMP_CRITICAL:
33848 stmt = cp_parser_omp_critical (parser, pragma_tok);
33849 break;
33850 case PRAGMA_OMP_DISTRIBUTE:
33851 strcpy (p_name, "#pragma omp");
33852 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
33853 break;
33854 case PRAGMA_OMP_FOR:
33855 strcpy (p_name, "#pragma omp");
33856 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
33857 break;
33858 case PRAGMA_OMP_MASTER:
33859 stmt = cp_parser_omp_master (parser, pragma_tok);
33860 break;
33861 case PRAGMA_OMP_ORDERED:
33862 stmt = cp_parser_omp_ordered (parser, pragma_tok);
33863 break;
33864 case PRAGMA_OMP_PARALLEL:
33865 strcpy (p_name, "#pragma omp");
33866 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
33867 break;
33868 case PRAGMA_OMP_SECTIONS:
33869 strcpy (p_name, "#pragma omp");
33870 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
33871 break;
33872 case PRAGMA_OMP_SIMD:
33873 strcpy (p_name, "#pragma omp");
33874 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
33875 break;
33876 case PRAGMA_OMP_SINGLE:
33877 stmt = cp_parser_omp_single (parser, pragma_tok);
33878 break;
33879 case PRAGMA_OMP_TASK:
33880 stmt = cp_parser_omp_task (parser, pragma_tok);
33881 break;
33882 case PRAGMA_OMP_TASKGROUP:
33883 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
33884 break;
33885 case PRAGMA_OMP_TEAMS:
33886 strcpy (p_name, "#pragma omp");
33887 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
33888 break;
33889 default:
33890 gcc_unreachable ();
33893 if (stmt)
33894 SET_EXPR_LOCATION (stmt, pragma_tok->location);
33897 /* Transactional Memory parsing routines. */
33899 /* Parse a transaction attribute.
33901 txn-attribute:
33902 attribute
33903 [ [ identifier ] ]
33905 ??? Simplify this when C++0x bracket attributes are
33906 implemented properly. */
33908 static tree
33909 cp_parser_txn_attribute_opt (cp_parser *parser)
33911 cp_token *token;
33912 tree attr_name, attr = NULL;
33914 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
33915 return cp_parser_attributes_opt (parser);
33917 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
33918 return NULL_TREE;
33919 cp_lexer_consume_token (parser->lexer);
33920 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
33921 goto error1;
33923 token = cp_lexer_peek_token (parser->lexer);
33924 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
33926 token = cp_lexer_consume_token (parser->lexer);
33928 attr_name = (token->type == CPP_KEYWORD
33929 /* For keywords, use the canonical spelling,
33930 not the parsed identifier. */
33931 ? ridpointers[(int) token->keyword]
33932 : token->u.value);
33933 attr = build_tree_list (attr_name, NULL_TREE);
33935 else
33936 cp_parser_error (parser, "expected identifier");
33938 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
33939 error1:
33940 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
33941 return attr;
33944 /* Parse a __transaction_atomic or __transaction_relaxed statement.
33946 transaction-statement:
33947 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
33948 compound-statement
33949 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
33952 static tree
33953 cp_parser_transaction (cp_parser *parser, enum rid keyword)
33955 unsigned char old_in = parser->in_transaction;
33956 unsigned char this_in = 1, new_in;
33957 cp_token *token;
33958 tree stmt, attrs, noex;
33960 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
33961 || keyword == RID_TRANSACTION_RELAXED);
33962 token = cp_parser_require_keyword (parser, keyword,
33963 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
33964 : RT_TRANSACTION_RELAXED));
33965 gcc_assert (token != NULL);
33967 if (keyword == RID_TRANSACTION_RELAXED)
33968 this_in |= TM_STMT_ATTR_RELAXED;
33969 else
33971 attrs = cp_parser_txn_attribute_opt (parser);
33972 if (attrs)
33973 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
33976 /* Parse a noexcept specification. */
33977 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
33979 /* Keep track if we're in the lexical scope of an outer transaction. */
33980 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
33982 stmt = begin_transaction_stmt (token->location, NULL, this_in);
33984 parser->in_transaction = new_in;
33985 cp_parser_compound_statement (parser, NULL, false, false);
33986 parser->in_transaction = old_in;
33988 finish_transaction_stmt (stmt, NULL, this_in, noex);
33990 return stmt;
33993 /* Parse a __transaction_atomic or __transaction_relaxed expression.
33995 transaction-expression:
33996 __transaction_atomic txn-noexcept-spec[opt] ( expression )
33997 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
34000 static tree
34001 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
34003 unsigned char old_in = parser->in_transaction;
34004 unsigned char this_in = 1;
34005 cp_token *token;
34006 tree expr, noex;
34007 bool noex_expr;
34009 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
34010 || keyword == RID_TRANSACTION_RELAXED);
34012 if (!flag_tm)
34013 error (keyword == RID_TRANSACTION_RELAXED
34014 ? G_("%<__transaction_relaxed%> without transactional memory "
34015 "support enabled")
34016 : G_("%<__transaction_atomic%> without transactional memory "
34017 "support enabled"));
34019 token = cp_parser_require_keyword (parser, keyword,
34020 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
34021 : RT_TRANSACTION_RELAXED));
34022 gcc_assert (token != NULL);
34024 if (keyword == RID_TRANSACTION_RELAXED)
34025 this_in |= TM_STMT_ATTR_RELAXED;
34027 /* Set this early. This might mean that we allow transaction_cancel in
34028 an expression that we find out later actually has to be a constexpr.
34029 However, we expect that cxx_constant_value will be able to deal with
34030 this; also, if the noexcept has no constexpr, then what we parse next
34031 really is a transaction's body. */
34032 parser->in_transaction = this_in;
34034 /* Parse a noexcept specification. */
34035 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
34036 true);
34038 if (!noex || !noex_expr
34039 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
34041 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
34043 expr = cp_parser_expression (parser);
34044 expr = finish_parenthesized_expr (expr);
34046 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
34048 else
34050 /* The only expression that is available got parsed for the noexcept
34051 already. noexcept is true then. */
34052 expr = noex;
34053 noex = boolean_true_node;
34056 expr = build_transaction_expr (token->location, expr, this_in, noex);
34057 parser->in_transaction = old_in;
34059 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
34060 return error_mark_node;
34062 return (flag_tm ? expr : error_mark_node);
34065 /* Parse a function-transaction-block.
34067 function-transaction-block:
34068 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
34069 function-body
34070 __transaction_atomic txn-attribute[opt] function-try-block
34071 __transaction_relaxed ctor-initializer[opt] function-body
34072 __transaction_relaxed function-try-block
34075 static bool
34076 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
34078 unsigned char old_in = parser->in_transaction;
34079 unsigned char new_in = 1;
34080 tree compound_stmt, stmt, attrs;
34081 bool ctor_initializer_p;
34082 cp_token *token;
34084 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
34085 || keyword == RID_TRANSACTION_RELAXED);
34086 token = cp_parser_require_keyword (parser, keyword,
34087 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
34088 : RT_TRANSACTION_RELAXED));
34089 gcc_assert (token != NULL);
34091 if (keyword == RID_TRANSACTION_RELAXED)
34092 new_in |= TM_STMT_ATTR_RELAXED;
34093 else
34095 attrs = cp_parser_txn_attribute_opt (parser);
34096 if (attrs)
34097 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
34100 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
34102 parser->in_transaction = new_in;
34104 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
34105 ctor_initializer_p = cp_parser_function_try_block (parser);
34106 else
34107 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
34108 (parser, /*in_function_try_block=*/false);
34110 parser->in_transaction = old_in;
34112 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
34114 return ctor_initializer_p;
34117 /* Parse a __transaction_cancel statement.
34119 cancel-statement:
34120 __transaction_cancel txn-attribute[opt] ;
34121 __transaction_cancel txn-attribute[opt] throw-expression ;
34123 ??? Cancel and throw is not yet implemented. */
34125 static tree
34126 cp_parser_transaction_cancel (cp_parser *parser)
34128 cp_token *token;
34129 bool is_outer = false;
34130 tree stmt, attrs;
34132 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
34133 RT_TRANSACTION_CANCEL);
34134 gcc_assert (token != NULL);
34136 attrs = cp_parser_txn_attribute_opt (parser);
34137 if (attrs)
34138 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
34140 /* ??? Parse cancel-and-throw here. */
34142 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34144 if (!flag_tm)
34146 error_at (token->location, "%<__transaction_cancel%> without "
34147 "transactional memory support enabled");
34148 return error_mark_node;
34150 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
34152 error_at (token->location, "%<__transaction_cancel%> within a "
34153 "%<__transaction_relaxed%>");
34154 return error_mark_node;
34156 else if (is_outer)
34158 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
34159 && !is_tm_may_cancel_outer (current_function_decl))
34161 error_at (token->location, "outer %<__transaction_cancel%> not "
34162 "within outer %<__transaction_atomic%>");
34163 error_at (token->location,
34164 " or a %<transaction_may_cancel_outer%> function");
34165 return error_mark_node;
34168 else if (parser->in_transaction == 0)
34170 error_at (token->location, "%<__transaction_cancel%> not within "
34171 "%<__transaction_atomic%>");
34172 return error_mark_node;
34175 stmt = build_tm_abort_call (token->location, is_outer);
34176 add_stmt (stmt);
34178 return stmt;
34181 /* The parser. */
34183 static GTY (()) cp_parser *the_parser;
34186 /* Special handling for the first token or line in the file. The first
34187 thing in the file might be #pragma GCC pch_preprocess, which loads a
34188 PCH file, which is a GC collection point. So we need to handle this
34189 first pragma without benefit of an existing lexer structure.
34191 Always returns one token to the caller in *FIRST_TOKEN. This is
34192 either the true first token of the file, or the first token after
34193 the initial pragma. */
34195 static void
34196 cp_parser_initial_pragma (cp_token *first_token)
34198 tree name = NULL;
34200 cp_lexer_get_preprocessor_token (NULL, first_token);
34201 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
34202 return;
34204 cp_lexer_get_preprocessor_token (NULL, first_token);
34205 if (first_token->type == CPP_STRING)
34207 name = first_token->u.value;
34209 cp_lexer_get_preprocessor_token (NULL, first_token);
34210 if (first_token->type != CPP_PRAGMA_EOL)
34211 error_at (first_token->location,
34212 "junk at end of %<#pragma GCC pch_preprocess%>");
34214 else
34215 error_at (first_token->location, "expected string literal");
34217 /* Skip to the end of the pragma. */
34218 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
34219 cp_lexer_get_preprocessor_token (NULL, first_token);
34221 /* Now actually load the PCH file. */
34222 if (name)
34223 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
34225 /* Read one more token to return to our caller. We have to do this
34226 after reading the PCH file in, since its pointers have to be
34227 live. */
34228 cp_lexer_get_preprocessor_token (NULL, first_token);
34231 /* Parses the grainsize pragma for the _Cilk_for statement.
34232 Syntax:
34233 #pragma cilk grainsize = <VALUE>. */
34235 static void
34236 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok)
34238 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
34240 tree exp = cp_parser_binary_expression (parser, false, false,
34241 PREC_NOT_OPERATOR, NULL);
34242 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34243 if (!exp || exp == error_mark_node)
34245 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
34246 return;
34249 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
34250 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
34251 cp_parser_cilk_for (parser, exp);
34252 else
34253 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
34254 "%<#pragma cilk grainsize%> is not followed by "
34255 "%<_Cilk_for%>");
34256 return;
34258 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34261 /* Normal parsing of a pragma token. Here we can (and must) use the
34262 regular lexer. */
34264 static bool
34265 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
34267 cp_token *pragma_tok;
34268 unsigned int id;
34270 pragma_tok = cp_lexer_consume_token (parser->lexer);
34271 gcc_assert (pragma_tok->type == CPP_PRAGMA);
34272 parser->lexer->in_pragma = true;
34274 id = pragma_tok->pragma_kind;
34275 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
34276 cp_ensure_no_omp_declare_simd (parser);
34277 switch (id)
34279 case PRAGMA_GCC_PCH_PREPROCESS:
34280 error_at (pragma_tok->location,
34281 "%<#pragma GCC pch_preprocess%> must be first");
34282 break;
34284 case PRAGMA_OMP_BARRIER:
34285 switch (context)
34287 case pragma_compound:
34288 cp_parser_omp_barrier (parser, pragma_tok);
34289 return false;
34290 case pragma_stmt:
34291 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
34292 "used in compound statements");
34293 break;
34294 default:
34295 goto bad_stmt;
34297 break;
34299 case PRAGMA_OMP_FLUSH:
34300 switch (context)
34302 case pragma_compound:
34303 cp_parser_omp_flush (parser, pragma_tok);
34304 return false;
34305 case pragma_stmt:
34306 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
34307 "used in compound statements");
34308 break;
34309 default:
34310 goto bad_stmt;
34312 break;
34314 case PRAGMA_OMP_TASKWAIT:
34315 switch (context)
34317 case pragma_compound:
34318 cp_parser_omp_taskwait (parser, pragma_tok);
34319 return false;
34320 case pragma_stmt:
34321 error_at (pragma_tok->location,
34322 "%<#pragma omp taskwait%> may only be "
34323 "used in compound statements");
34324 break;
34325 default:
34326 goto bad_stmt;
34328 break;
34330 case PRAGMA_OMP_TASKYIELD:
34331 switch (context)
34333 case pragma_compound:
34334 cp_parser_omp_taskyield (parser, pragma_tok);
34335 return false;
34336 case pragma_stmt:
34337 error_at (pragma_tok->location,
34338 "%<#pragma omp taskyield%> may only be "
34339 "used in compound statements");
34340 break;
34341 default:
34342 goto bad_stmt;
34344 break;
34346 case PRAGMA_OMP_CANCEL:
34347 switch (context)
34349 case pragma_compound:
34350 cp_parser_omp_cancel (parser, pragma_tok);
34351 return false;
34352 case pragma_stmt:
34353 error_at (pragma_tok->location,
34354 "%<#pragma omp cancel%> may only be "
34355 "used in compound statements");
34356 break;
34357 default:
34358 goto bad_stmt;
34360 break;
34362 case PRAGMA_OMP_CANCELLATION_POINT:
34363 switch (context)
34365 case pragma_compound:
34366 cp_parser_omp_cancellation_point (parser, pragma_tok);
34367 return false;
34368 case pragma_stmt:
34369 error_at (pragma_tok->location,
34370 "%<#pragma omp cancellation point%> may only be "
34371 "used in compound statements");
34372 break;
34373 default:
34374 goto bad_stmt;
34376 break;
34378 case PRAGMA_OMP_THREADPRIVATE:
34379 cp_parser_omp_threadprivate (parser, pragma_tok);
34380 return false;
34382 case PRAGMA_OMP_DECLARE_REDUCTION:
34383 cp_parser_omp_declare (parser, pragma_tok, context);
34384 return false;
34386 case PRAGMA_OACC_CACHE:
34387 case PRAGMA_OACC_DATA:
34388 case PRAGMA_OACC_ENTER_DATA:
34389 case PRAGMA_OACC_EXIT_DATA:
34390 case PRAGMA_OACC_KERNELS:
34391 case PRAGMA_OACC_PARALLEL:
34392 case PRAGMA_OACC_LOOP:
34393 case PRAGMA_OACC_UPDATE:
34394 case PRAGMA_OACC_WAIT:
34395 case PRAGMA_OMP_ATOMIC:
34396 case PRAGMA_OMP_CRITICAL:
34397 case PRAGMA_OMP_DISTRIBUTE:
34398 case PRAGMA_OMP_FOR:
34399 case PRAGMA_OMP_MASTER:
34400 case PRAGMA_OMP_ORDERED:
34401 case PRAGMA_OMP_PARALLEL:
34402 case PRAGMA_OMP_SECTIONS:
34403 case PRAGMA_OMP_SIMD:
34404 case PRAGMA_OMP_SINGLE:
34405 case PRAGMA_OMP_TASK:
34406 case PRAGMA_OMP_TASKGROUP:
34407 case PRAGMA_OMP_TEAMS:
34408 if (context != pragma_stmt && context != pragma_compound)
34409 goto bad_stmt;
34410 cp_parser_omp_construct (parser, pragma_tok);
34411 return true;
34413 case PRAGMA_OMP_TARGET:
34414 return cp_parser_omp_target (parser, pragma_tok, context);
34416 case PRAGMA_OMP_END_DECLARE_TARGET:
34417 cp_parser_omp_end_declare_target (parser, pragma_tok);
34418 return false;
34420 case PRAGMA_OMP_SECTION:
34421 error_at (pragma_tok->location,
34422 "%<#pragma omp section%> may only be used in "
34423 "%<#pragma omp sections%> construct");
34424 break;
34426 case PRAGMA_IVDEP:
34428 if (context == pragma_external)
34430 error_at (pragma_tok->location,
34431 "%<#pragma GCC ivdep%> must be inside a function");
34432 break;
34434 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34435 cp_token *tok;
34436 tok = cp_lexer_peek_token (the_parser->lexer);
34437 if (tok->type != CPP_KEYWORD
34438 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
34439 && tok->keyword != RID_DO))
34441 cp_parser_error (parser, "for, while or do statement expected");
34442 return false;
34444 cp_parser_iteration_statement (parser, true);
34445 return true;
34448 case PRAGMA_CILK_SIMD:
34449 if (context == pragma_external)
34451 error_at (pragma_tok->location,
34452 "%<#pragma simd%> must be inside a function");
34453 break;
34455 cp_parser_cilk_simd (parser, pragma_tok);
34456 return true;
34458 case PRAGMA_CILK_GRAINSIZE:
34459 if (context == pragma_external)
34461 error_at (pragma_tok->location,
34462 "%<#pragma cilk grainsize%> must be inside a function");
34463 break;
34466 /* Ignore the pragma if Cilk Plus is not enabled. */
34467 if (flag_cilkplus)
34469 cp_parser_cilk_grainsize (parser, pragma_tok);
34470 return true;
34472 else
34474 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
34475 "%<#pragma cilk grainsize%>");
34476 break;
34479 default:
34480 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
34481 c_invoke_pragma_handler (id);
34482 break;
34484 bad_stmt:
34485 cp_parser_error (parser, "expected declaration specifiers");
34486 break;
34489 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34490 return false;
34493 /* The interface the pragma parsers have to the lexer. */
34495 enum cpp_ttype
34496 pragma_lex (tree *value, location_t *loc)
34498 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
34499 enum cpp_ttype ret = tok->type;
34501 *value = tok->u.value;
34502 if (loc)
34503 *loc = tok->location;
34505 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
34506 ret = CPP_EOF;
34507 else if (ret == CPP_STRING)
34508 *value = cp_parser_string_literal (the_parser, false, false);
34509 else
34511 if (ret == CPP_KEYWORD)
34512 ret = CPP_NAME;
34513 cp_lexer_consume_token (the_parser->lexer);
34516 return ret;
34520 /* External interface. */
34522 /* Parse one entire translation unit. */
34524 void
34525 c_parse_file (void)
34527 static bool already_called = false;
34529 if (already_called)
34530 fatal_error (input_location,
34531 "inter-module optimizations not implemented for C++");
34532 already_called = true;
34534 the_parser = cp_parser_new ();
34535 push_deferring_access_checks (flag_access_control
34536 ? dk_no_deferred : dk_no_check);
34537 cp_parser_translation_unit (the_parser);
34538 the_parser = NULL;
34541 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
34542 vectorlength clause:
34543 Syntax:
34544 vectorlength ( constant-expression ) */
34546 static tree
34547 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
34548 bool is_simd_fn)
34550 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34551 tree expr;
34552 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
34553 safelen clause. Thus, vectorlength is represented as OMP 4.0
34554 safelen. For SIMD-enabled function it is represented by OMP 4.0
34555 simdlen. */
34556 if (!is_simd_fn)
34557 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
34558 loc);
34559 else
34560 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
34561 loc);
34563 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34564 return error_mark_node;
34566 expr = cp_parser_constant_expression (parser);
34567 expr = maybe_constant_value (expr);
34569 /* If expr == error_mark_node, then don't emit any errors nor
34570 create a clause. if any of the above functions returns
34571 error mark node then they would have emitted an error message. */
34572 if (expr == error_mark_node)
34574 else if (!TREE_TYPE (expr)
34575 || !TREE_CONSTANT (expr)
34576 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
34577 error_at (loc, "vectorlength must be an integer constant");
34578 else if (TREE_CONSTANT (expr)
34579 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
34580 error_at (loc, "vectorlength must be a power of 2");
34581 else
34583 tree c;
34584 if (!is_simd_fn)
34586 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
34587 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
34588 OMP_CLAUSE_CHAIN (c) = clauses;
34589 clauses = c;
34591 else
34593 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
34594 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
34595 OMP_CLAUSE_CHAIN (c) = clauses;
34596 clauses = c;
34600 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
34601 return error_mark_node;
34602 return clauses;
34605 /* Handles the Cilk Plus #pragma simd linear clause.
34606 Syntax:
34607 linear ( simd-linear-variable-list )
34609 simd-linear-variable-list:
34610 simd-linear-variable
34611 simd-linear-variable-list , simd-linear-variable
34613 simd-linear-variable:
34614 id-expression
34615 id-expression : simd-linear-step
34617 simd-linear-step:
34618 conditional-expression */
34620 static tree
34621 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
34623 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34625 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34626 return clauses;
34627 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34629 cp_parser_error (parser, "expected identifier");
34630 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
34631 return error_mark_node;
34634 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
34635 parser->colon_corrects_to_scope_p = false;
34636 while (1)
34638 cp_token *token = cp_lexer_peek_token (parser->lexer);
34639 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34641 cp_parser_error (parser, "expected variable-name");
34642 clauses = error_mark_node;
34643 break;
34646 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
34647 false, false);
34648 tree decl = cp_parser_lookup_name_simple (parser, var_name,
34649 token->location);
34650 if (decl == error_mark_node)
34652 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
34653 token->location);
34654 clauses = error_mark_node;
34656 else
34658 tree e = NULL_TREE;
34659 tree step_size = integer_one_node;
34661 /* If present, parse the linear step. Otherwise, assume the default
34662 value of 1. */
34663 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
34665 cp_lexer_consume_token (parser->lexer);
34667 e = cp_parser_assignment_expression (parser);
34668 e = maybe_constant_value (e);
34670 if (e == error_mark_node)
34672 /* If an error has occurred, then the whole pragma is
34673 considered ill-formed. Thus, no reason to keep
34674 parsing. */
34675 clauses = error_mark_node;
34676 break;
34678 else if (type_dependent_expression_p (e)
34679 || value_dependent_expression_p (e)
34680 || (TREE_TYPE (e)
34681 && INTEGRAL_TYPE_P (TREE_TYPE (e))
34682 && (TREE_CONSTANT (e)
34683 || DECL_P (e))))
34684 step_size = e;
34685 else
34686 cp_parser_error (parser,
34687 "step size must be an integer constant "
34688 "expression or an integer variable");
34691 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
34692 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
34693 OMP_CLAUSE_DECL (l) = decl;
34694 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
34695 OMP_CLAUSE_CHAIN (l) = clauses;
34696 clauses = l;
34698 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34699 cp_lexer_consume_token (parser->lexer);
34700 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
34701 break;
34702 else
34704 error_at (cp_lexer_peek_token (parser->lexer)->location,
34705 "expected %<,%> or %<)%> after %qE", decl);
34706 clauses = error_mark_node;
34707 break;
34710 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
34711 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
34712 return clauses;
34715 /* Returns the name of the next clause. If the clause is not
34716 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
34717 token is not consumed. Otherwise, the appropriate enum from the
34718 pragma_simd_clause is returned and the token is consumed. */
34720 static pragma_omp_clause
34721 cp_parser_cilk_simd_clause_name (cp_parser *parser)
34723 pragma_omp_clause clause_type;
34724 cp_token *token = cp_lexer_peek_token (parser->lexer);
34726 if (token->keyword == RID_PRIVATE)
34727 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
34728 else if (!token->u.value || token->type != CPP_NAME)
34729 return PRAGMA_CILK_CLAUSE_NONE;
34730 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
34731 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
34732 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
34733 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
34734 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
34735 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
34736 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
34737 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
34738 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
34739 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
34740 else
34741 return PRAGMA_CILK_CLAUSE_NONE;
34743 cp_lexer_consume_token (parser->lexer);
34744 return clause_type;
34747 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
34749 static tree
34750 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
34752 tree clauses = NULL_TREE;
34754 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
34755 && clauses != error_mark_node)
34757 pragma_omp_clause c_kind;
34758 c_kind = cp_parser_cilk_simd_clause_name (parser);
34759 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
34760 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
34761 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
34762 clauses = cp_parser_cilk_simd_linear (parser, clauses);
34763 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
34764 /* Use the OpenMP 4.0 equivalent function. */
34765 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
34766 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
34767 /* Use the OpenMP 4.0 equivalent function. */
34768 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
34769 clauses);
34770 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
34771 /* Use the OMP 4.0 equivalent function. */
34772 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
34773 clauses);
34774 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
34775 /* Use the OMP 4.0 equivalent function. */
34776 clauses = cp_parser_omp_clause_reduction (parser, clauses);
34777 else
34779 clauses = error_mark_node;
34780 cp_parser_error (parser, "expected %<#pragma simd%> clause");
34781 break;
34785 cp_parser_skip_to_pragma_eol (parser, pragma_token);
34787 if (clauses == error_mark_node)
34788 return error_mark_node;
34789 else
34790 return c_finish_cilk_clauses (clauses);
34793 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
34795 static void
34796 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
34798 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
34800 if (clauses == error_mark_node)
34801 return;
34803 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
34805 error_at (cp_lexer_peek_token (parser->lexer)->location,
34806 "for statement expected");
34807 return;
34810 tree sb = begin_omp_structured_block ();
34811 int save = cp_parser_begin_omp_structured_block (parser);
34812 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
34813 if (ret)
34814 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
34815 cp_parser_end_omp_structured_block (parser, save);
34816 add_stmt (finish_omp_structured_block (sb));
34819 /* Main entry-point for parsing Cilk Plus _Cilk_for
34820 loops. The return value is error_mark_node
34821 when errors happen and CILK_FOR tree on success. */
34823 static tree
34824 cp_parser_cilk_for (cp_parser *parser, tree grain)
34826 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
34827 gcc_unreachable ();
34829 tree sb = begin_omp_structured_block ();
34830 int save = cp_parser_begin_omp_structured_block (parser);
34832 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
34833 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
34834 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
34835 clauses = finish_omp_clauses (clauses);
34837 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL);
34838 if (ret)
34839 cpp_validate_cilk_plus_loop (ret);
34840 else
34841 ret = error_mark_node;
34843 cp_parser_end_omp_structured_block (parser, save);
34844 add_stmt (finish_omp_structured_block (sb));
34845 return ret;
34848 /* Create an identifier for a generic parameter type (a synthesized
34849 template parameter implied by `auto' or a concept identifier). */
34851 static GTY(()) int generic_parm_count;
34852 static tree
34853 make_generic_type_name ()
34855 char buf[32];
34856 sprintf (buf, "auto:%d", ++generic_parm_count);
34857 return get_identifier (buf);
34860 /* Predicate that behaves as is_auto_or_concept but matches the parent
34861 node of the generic type rather than the generic type itself. This
34862 allows for type transformation in add_implicit_template_parms. */
34864 static inline bool
34865 tree_type_is_auto_or_concept (const_tree t)
34867 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
34870 /* Returns the template declaration being called or evaluated as
34871 part of the constraint check. Note that T must be a predicate
34872 constraint (it can't be any other kind of constraint). */
34873 static tree
34874 get_concept_from_constraint (tree t)
34876 gcc_assert (TREE_CODE (t) == PRED_CONSTR);
34877 t = PRED_CONSTR_EXPR (t);
34878 gcc_assert (TREE_CODE (t) == CALL_EXPR
34879 || TREE_CODE (t) == TEMPLATE_ID_EXPR
34880 || VAR_P (t));
34882 if (TREE_CODE (t) == TEMPLATE_ID_EXPR)
34883 return DECL_TEMPLATE_RESULT (TREE_OPERAND (t, 0));
34884 if (VAR_P (t))
34885 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (t));
34886 else
34888 tree fn = CALL_EXPR_FN (t);
34889 tree ovl = TREE_OPERAND (fn, 0);
34890 tree tmpl = OVL_FUNCTION (ovl);
34891 return DECL_TEMPLATE_RESULT (tmpl);
34895 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
34896 (creating a new template parameter list if necessary). Returns the newly
34897 created template type parm. */
34899 tree
34900 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
34902 gcc_assert (current_binding_level->kind == sk_function_parms);
34904 /* Before committing to modifying any scope, if we're in an
34905 implicit template scope, and we're trying to synthesize a
34906 constrained parameter, try to find a previous parameter with
34907 the same name. This is the same-type rule for abbreviated
34908 function templates. */
34909 if (parser->implicit_template_scope && constr)
34911 tree t = parser->implicit_template_parms;
34912 while (t)
34914 tree c = get_concept_from_constraint (TREE_TYPE (t));
34915 if (c == CONSTRAINED_PARM_CONCEPT (constr))
34916 return TREE_VALUE (t);
34917 t = TREE_CHAIN (t);
34921 /* We are either continuing a function template that already contains implicit
34922 template parameters, creating a new fully-implicit function template, or
34923 extending an existing explicit function template with implicit template
34924 parameters. */
34926 cp_binding_level *const entry_scope = current_binding_level;
34928 bool become_template = false;
34929 cp_binding_level *parent_scope = 0;
34931 if (parser->implicit_template_scope)
34933 gcc_assert (parser->implicit_template_parms);
34935 current_binding_level = parser->implicit_template_scope;
34937 else
34939 /* Roll back to the existing template parameter scope (in the case of
34940 extending an explicit function template) or introduce a new template
34941 parameter scope ahead of the function parameter scope (or class scope
34942 in the case of out-of-line member definitions). The function scope is
34943 added back after template parameter synthesis below. */
34945 cp_binding_level *scope = entry_scope;
34947 while (scope->kind == sk_function_parms)
34949 parent_scope = scope;
34950 scope = scope->level_chain;
34952 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
34954 /* If not defining a class, then any class scope is a scope level in
34955 an out-of-line member definition. In this case simply wind back
34956 beyond the first such scope to inject the template parameter list.
34957 Otherwise wind back to the class being defined. The latter can
34958 occur in class member friend declarations such as:
34960 class A {
34961 void foo (auto);
34963 class B {
34964 friend void A::foo (auto);
34967 The template parameter list synthesized for the friend declaration
34968 must be injected in the scope of 'B'. This can also occur in
34969 erroneous cases such as:
34971 struct A {
34972 struct B {
34973 void foo (auto);
34975 void B::foo (auto) {}
34978 Here the attempted definition of 'B::foo' within 'A' is ill-formed
34979 but, nevertheless, the template parameter list synthesized for the
34980 declarator should be injected into the scope of 'A' as if the
34981 ill-formed template was specified explicitly. */
34983 while (scope->kind == sk_class && !scope->defining_class_p)
34985 parent_scope = scope;
34986 scope = scope->level_chain;
34990 current_binding_level = scope;
34992 if (scope->kind != sk_template_parms
34993 || !function_being_declared_is_template_p (parser))
34995 /* Introduce a new template parameter list for implicit template
34996 parameters. */
34998 become_template = true;
35000 parser->implicit_template_scope
35001 = begin_scope (sk_template_parms, NULL);
35003 ++processing_template_decl;
35005 parser->fully_implicit_function_template_p = true;
35006 ++parser->num_template_parameter_lists;
35008 else
35010 /* Synthesize implicit template parameters at the end of the explicit
35011 template parameter list. */
35013 gcc_assert (current_template_parms);
35015 parser->implicit_template_scope = scope;
35017 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
35018 parser->implicit_template_parms
35019 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
35023 /* Synthesize a new template parameter and track the current template
35024 parameter chain with implicit_template_parms. */
35026 tree synth_id = make_generic_type_name ();
35027 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
35028 synth_id);
35030 // Attach the constraint to the parm before processing.
35031 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
35032 TREE_TYPE (node) = constr;
35033 tree new_parm
35034 = process_template_parm (parser->implicit_template_parms,
35035 input_location,
35036 node,
35037 /*non_type=*/false,
35038 /*param_pack=*/false);
35040 // Chain the new parameter to the list of implicit parameters.
35041 if (parser->implicit_template_parms)
35042 parser->implicit_template_parms
35043 = TREE_CHAIN (parser->implicit_template_parms);
35044 else
35045 parser->implicit_template_parms = new_parm;
35047 tree new_type = TREE_TYPE (getdecls ());
35049 /* If creating a fully implicit function template, start the new implicit
35050 template parameter list with this synthesized type, otherwise grow the
35051 current template parameter list. */
35053 if (become_template)
35055 parent_scope->level_chain = current_binding_level;
35057 tree new_parms = make_tree_vec (1);
35058 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
35059 current_template_parms = tree_cons (size_int (processing_template_decl),
35060 new_parms, current_template_parms);
35062 else
35064 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
35065 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
35066 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
35067 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
35070 // If the new parameter was constrained, we need to add that to the
35071 // constraints in the template parameter list.
35072 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
35074 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
35075 reqs = conjoin_constraints (reqs, req);
35076 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
35079 current_binding_level = entry_scope;
35081 return new_type;
35084 /* Finish the declaration of a fully implicit function template. Such a
35085 template has no explicit template parameter list so has not been through the
35086 normal template head and tail processing. synthesize_implicit_template_parm
35087 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
35088 provided if the declaration is a class member such that its template
35089 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
35090 form is returned. Otherwise NULL_TREE is returned. */
35092 tree
35093 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
35095 gcc_assert (parser->fully_implicit_function_template_p);
35097 if (member_decl_opt && member_decl_opt != error_mark_node
35098 && DECL_VIRTUAL_P (member_decl_opt))
35100 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
35101 "implicit templates may not be %<virtual%>");
35102 DECL_VIRTUAL_P (member_decl_opt) = false;
35105 if (member_decl_opt)
35106 member_decl_opt = finish_member_template_decl (member_decl_opt);
35107 end_template_decl ();
35109 parser->fully_implicit_function_template_p = false;
35110 --parser->num_template_parameter_lists;
35112 return member_decl_opt;
35115 #include "gt-cp-parser.h"