Fix PR c++/69139 (deduction failure with trailing return type)
[official-gcc.git] / gcc / cp / parser.c
blob6f47edf9bd0abec6d22406e185716cc1d35640a5
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2016 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 "cp-tree.h"
25 #include "c-family/c-common.h"
26 #include "timevar.h"
27 #include "stringpool.h"
28 #include "cgraph.h"
29 #include "print-tree.h"
30 #include "attribs.h"
31 #include "trans-mem.h"
32 #include "intl.h"
33 #include "decl.h"
34 #include "c-family/c-objc.h"
35 #include "plugin.h"
36 #include "tree-pretty-print.h"
37 #include "parser.h"
38 #include "omp-low.h"
39 #include "gomp-constants.h"
40 #include "c-family/c-indentation.h"
41 #include "context.h"
44 /* The lexer. */
46 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
47 and c-lex.c) and the C++ parser. */
49 static cp_token eof_token =
51 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
54 /* The various kinds of non integral constant we encounter. */
55 enum non_integral_constant {
56 NIC_NONE,
57 /* floating-point literal */
58 NIC_FLOAT,
59 /* %<this%> */
60 NIC_THIS,
61 /* %<__FUNCTION__%> */
62 NIC_FUNC_NAME,
63 /* %<__PRETTY_FUNCTION__%> */
64 NIC_PRETTY_FUNC,
65 /* %<__func__%> */
66 NIC_C99_FUNC,
67 /* "%<va_arg%> */
68 NIC_VA_ARG,
69 /* a cast */
70 NIC_CAST,
71 /* %<typeid%> operator */
72 NIC_TYPEID,
73 /* non-constant compound literals */
74 NIC_NCC,
75 /* a function call */
76 NIC_FUNC_CALL,
77 /* an increment */
78 NIC_INC,
79 /* an decrement */
80 NIC_DEC,
81 /* an array reference */
82 NIC_ARRAY_REF,
83 /* %<->%> */
84 NIC_ARROW,
85 /* %<.%> */
86 NIC_POINT,
87 /* the address of a label */
88 NIC_ADDR_LABEL,
89 /* %<*%> */
90 NIC_STAR,
91 /* %<&%> */
92 NIC_ADDR,
93 /* %<++%> */
94 NIC_PREINCREMENT,
95 /* %<--%> */
96 NIC_PREDECREMENT,
97 /* %<new%> */
98 NIC_NEW,
99 /* %<delete%> */
100 NIC_DEL,
101 /* calls to overloaded operators */
102 NIC_OVERLOADED,
103 /* an assignment */
104 NIC_ASSIGNMENT,
105 /* a comma operator */
106 NIC_COMMA,
107 /* a call to a constructor */
108 NIC_CONSTRUCTOR,
109 /* a transaction expression */
110 NIC_TRANSACTION
113 /* The various kinds of errors about name-lookup failing. */
114 enum name_lookup_error {
115 /* NULL */
116 NLE_NULL,
117 /* is not a type */
118 NLE_TYPE,
119 /* is not a class or namespace */
120 NLE_CXX98,
121 /* is not a class, namespace, or enumeration */
122 NLE_NOT_CXX98
125 /* The various kinds of required token */
126 enum required_token {
127 RT_NONE,
128 RT_SEMICOLON, /* ';' */
129 RT_OPEN_PAREN, /* '(' */
130 RT_CLOSE_BRACE, /* '}' */
131 RT_OPEN_BRACE, /* '{' */
132 RT_CLOSE_SQUARE, /* ']' */
133 RT_OPEN_SQUARE, /* '[' */
134 RT_COMMA, /* ',' */
135 RT_SCOPE, /* '::' */
136 RT_LESS, /* '<' */
137 RT_GREATER, /* '>' */
138 RT_EQ, /* '=' */
139 RT_ELLIPSIS, /* '...' */
140 RT_MULT, /* '*' */
141 RT_COMPL, /* '~' */
142 RT_COLON, /* ':' */
143 RT_COLON_SCOPE, /* ':' or '::' */
144 RT_CLOSE_PAREN, /* ')' */
145 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
146 RT_PRAGMA_EOL, /* end of line */
147 RT_NAME, /* identifier */
149 /* The type is CPP_KEYWORD */
150 RT_NEW, /* new */
151 RT_DELETE, /* delete */
152 RT_RETURN, /* return */
153 RT_WHILE, /* while */
154 RT_EXTERN, /* extern */
155 RT_STATIC_ASSERT, /* static_assert */
156 RT_DECLTYPE, /* decltype */
157 RT_OPERATOR, /* operator */
158 RT_CLASS, /* class */
159 RT_TEMPLATE, /* template */
160 RT_NAMESPACE, /* namespace */
161 RT_USING, /* using */
162 RT_ASM, /* asm */
163 RT_TRY, /* try */
164 RT_CATCH, /* catch */
165 RT_THROW, /* throw */
166 RT_LABEL, /* __label__ */
167 RT_AT_TRY, /* @try */
168 RT_AT_SYNCHRONIZED, /* @synchronized */
169 RT_AT_THROW, /* @throw */
171 RT_SELECT, /* selection-statement */
172 RT_INTERATION, /* iteration-statement */
173 RT_JUMP, /* jump-statement */
174 RT_CLASS_KEY, /* class-key */
175 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
176 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
177 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
178 RT_TRANSACTION_CANCEL /* __transaction_cancel */
181 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
182 reverting it on destruction. */
184 class type_id_in_expr_sentinel
186 cp_parser *parser;
187 bool saved;
188 public:
189 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
190 : parser (parser),
191 saved (parser->in_type_id_in_expr_p)
192 { parser->in_type_id_in_expr_p = set; }
193 ~type_id_in_expr_sentinel ()
194 { parser->in_type_id_in_expr_p = saved; }
197 /* Prototypes. */
199 static cp_lexer *cp_lexer_new_main
200 (void);
201 static cp_lexer *cp_lexer_new_from_tokens
202 (cp_token_cache *tokens);
203 static void cp_lexer_destroy
204 (cp_lexer *);
205 static int cp_lexer_saving_tokens
206 (const cp_lexer *);
207 static cp_token *cp_lexer_token_at
208 (cp_lexer *, cp_token_position);
209 static void cp_lexer_get_preprocessor_token
210 (cp_lexer *, cp_token *);
211 static inline cp_token *cp_lexer_peek_token
212 (cp_lexer *);
213 static cp_token *cp_lexer_peek_nth_token
214 (cp_lexer *, size_t);
215 static inline bool cp_lexer_next_token_is
216 (cp_lexer *, enum cpp_ttype);
217 static bool cp_lexer_next_token_is_not
218 (cp_lexer *, enum cpp_ttype);
219 static bool cp_lexer_next_token_is_keyword
220 (cp_lexer *, enum rid);
221 static cp_token *cp_lexer_consume_token
222 (cp_lexer *);
223 static void cp_lexer_purge_token
224 (cp_lexer *);
225 static void cp_lexer_purge_tokens_after
226 (cp_lexer *, cp_token_position);
227 static void cp_lexer_save_tokens
228 (cp_lexer *);
229 static void cp_lexer_commit_tokens
230 (cp_lexer *);
231 static void cp_lexer_rollback_tokens
232 (cp_lexer *);
233 static void cp_lexer_print_token
234 (FILE *, cp_token *);
235 static inline bool cp_lexer_debugging_p
236 (cp_lexer *);
237 static void cp_lexer_start_debugging
238 (cp_lexer *) ATTRIBUTE_UNUSED;
239 static void cp_lexer_stop_debugging
240 (cp_lexer *) ATTRIBUTE_UNUSED;
242 static cp_token_cache *cp_token_cache_new
243 (cp_token *, cp_token *);
245 static void cp_parser_initial_pragma
246 (cp_token *);
248 static tree cp_literal_operator_id
249 (const char *);
251 static void cp_parser_cilk_simd
252 (cp_parser *, cp_token *);
253 static tree cp_parser_cilk_for
254 (cp_parser *, tree);
255 static bool cp_parser_omp_declare_reduction_exprs
256 (tree, cp_parser *);
257 static tree cp_parser_cilk_simd_vectorlength
258 (cp_parser *, tree, bool);
259 static void cp_finalize_oacc_routine
260 (cp_parser *, tree, bool);
262 /* Manifest constants. */
263 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
264 #define CP_SAVED_TOKEN_STACK 5
266 /* Variables. */
268 /* The stream to which debugging output should be written. */
269 static FILE *cp_lexer_debug_stream;
271 /* Nonzero if we are parsing an unevaluated operand: an operand to
272 sizeof, typeof, or alignof. */
273 int cp_unevaluated_operand;
275 /* Dump up to NUM tokens in BUFFER to FILE starting with token
276 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
277 first token in BUFFER. If NUM is 0, dump all the tokens. If
278 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
279 highlighted by surrounding it in [[ ]]. */
281 static void
282 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
283 cp_token *start_token, unsigned num,
284 cp_token *curr_token)
286 unsigned i, nprinted;
287 cp_token *token;
288 bool do_print;
290 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
292 if (buffer == NULL)
293 return;
295 if (num == 0)
296 num = buffer->length ();
298 if (start_token == NULL)
299 start_token = buffer->address ();
301 if (start_token > buffer->address ())
303 cp_lexer_print_token (file, &(*buffer)[0]);
304 fprintf (file, " ... ");
307 do_print = false;
308 nprinted = 0;
309 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
311 if (token == start_token)
312 do_print = true;
314 if (!do_print)
315 continue;
317 nprinted++;
318 if (token == curr_token)
319 fprintf (file, "[[");
321 cp_lexer_print_token (file, token);
323 if (token == curr_token)
324 fprintf (file, "]]");
326 switch (token->type)
328 case CPP_SEMICOLON:
329 case CPP_OPEN_BRACE:
330 case CPP_CLOSE_BRACE:
331 case CPP_EOF:
332 fputc ('\n', file);
333 break;
335 default:
336 fputc (' ', file);
340 if (i == num && i < buffer->length ())
342 fprintf (file, " ... ");
343 cp_lexer_print_token (file, &buffer->last ());
346 fprintf (file, "\n");
350 /* Dump all tokens in BUFFER to stderr. */
352 void
353 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
355 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
358 DEBUG_FUNCTION void
359 debug (vec<cp_token, va_gc> &ref)
361 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
364 DEBUG_FUNCTION void
365 debug (vec<cp_token, va_gc> *ptr)
367 if (ptr)
368 debug (*ptr);
369 else
370 fprintf (stderr, "<nil>\n");
374 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
375 description for T. */
377 static void
378 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
380 if (t)
382 fprintf (file, "%s: ", desc);
383 print_node_brief (file, "", t, 0);
388 /* Dump parser context C to FILE. */
390 static void
391 cp_debug_print_context (FILE *file, cp_parser_context *c)
393 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
394 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
395 print_node_brief (file, "", c->object_type, 0);
396 fprintf (file, "}\n");
400 /* Print the stack of parsing contexts to FILE starting with FIRST. */
402 static void
403 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
405 unsigned i;
406 cp_parser_context *c;
408 fprintf (file, "Parsing context stack:\n");
409 for (i = 0, c = first; c; c = c->next, i++)
411 fprintf (file, "\t#%u: ", i);
412 cp_debug_print_context (file, c);
417 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
419 static void
420 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
422 if (flag)
423 fprintf (file, "%s: true\n", desc);
427 /* Print an unparsed function entry UF to FILE. */
429 static void
430 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
432 unsigned i;
433 cp_default_arg_entry *default_arg_fn;
434 tree fn;
436 fprintf (file, "\tFunctions with default args:\n");
437 for (i = 0;
438 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
439 i++)
441 fprintf (file, "\t\tClass type: ");
442 print_node_brief (file, "", default_arg_fn->class_type, 0);
443 fprintf (file, "\t\tDeclaration: ");
444 print_node_brief (file, "", default_arg_fn->decl, 0);
445 fprintf (file, "\n");
448 fprintf (file, "\n\tFunctions with definitions that require "
449 "post-processing\n\t\t");
450 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
452 print_node_brief (file, "", fn, 0);
453 fprintf (file, " ");
455 fprintf (file, "\n");
457 fprintf (file, "\n\tNon-static data members with initializers that require "
458 "post-processing\n\t\t");
459 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
461 print_node_brief (file, "", fn, 0);
462 fprintf (file, " ");
464 fprintf (file, "\n");
468 /* Print the stack of unparsed member functions S to FILE. */
470 static void
471 cp_debug_print_unparsed_queues (FILE *file,
472 vec<cp_unparsed_functions_entry, va_gc> *s)
474 unsigned i;
475 cp_unparsed_functions_entry *uf;
477 fprintf (file, "Unparsed functions\n");
478 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
480 fprintf (file, "#%u:\n", i);
481 cp_debug_print_unparsed_function (file, uf);
486 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
487 the given PARSER. If FILE is NULL, the output is printed on stderr. */
489 static void
490 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
492 cp_token *next_token, *first_token, *start_token;
494 if (file == NULL)
495 file = stderr;
497 next_token = parser->lexer->next_token;
498 first_token = parser->lexer->buffer->address ();
499 start_token = (next_token > first_token + window_size / 2)
500 ? next_token - window_size / 2
501 : first_token;
502 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
503 next_token);
507 /* Dump debugging information for the given PARSER. If FILE is NULL,
508 the output is printed on stderr. */
510 void
511 cp_debug_parser (FILE *file, cp_parser *parser)
513 const size_t window_size = 20;
514 cp_token *token;
515 expanded_location eloc;
517 if (file == NULL)
518 file = stderr;
520 fprintf (file, "Parser state\n\n");
521 fprintf (file, "Number of tokens: %u\n",
522 vec_safe_length (parser->lexer->buffer));
523 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
524 cp_debug_print_tree_if_set (file, "Object scope",
525 parser->object_scope);
526 cp_debug_print_tree_if_set (file, "Qualifying scope",
527 parser->qualifying_scope);
528 cp_debug_print_context_stack (file, parser->context);
529 cp_debug_print_flag (file, "Allow GNU extensions",
530 parser->allow_gnu_extensions_p);
531 cp_debug_print_flag (file, "'>' token is greater-than",
532 parser->greater_than_is_operator_p);
533 cp_debug_print_flag (file, "Default args allowed in current "
534 "parameter list", parser->default_arg_ok_p);
535 cp_debug_print_flag (file, "Parsing integral constant-expression",
536 parser->integral_constant_expression_p);
537 cp_debug_print_flag (file, "Allow non-constant expression in current "
538 "constant-expression",
539 parser->allow_non_integral_constant_expression_p);
540 cp_debug_print_flag (file, "Seen non-constant expression",
541 parser->non_integral_constant_expression_p);
542 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
543 "current context",
544 parser->local_variables_forbidden_p);
545 cp_debug_print_flag (file, "In unbraced linkage specification",
546 parser->in_unbraced_linkage_specification_p);
547 cp_debug_print_flag (file, "Parsing a declarator",
548 parser->in_declarator_p);
549 cp_debug_print_flag (file, "In template argument list",
550 parser->in_template_argument_list_p);
551 cp_debug_print_flag (file, "Parsing an iteration statement",
552 parser->in_statement & IN_ITERATION_STMT);
553 cp_debug_print_flag (file, "Parsing a switch statement",
554 parser->in_statement & IN_SWITCH_STMT);
555 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
556 parser->in_statement & IN_OMP_BLOCK);
557 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
558 parser->in_statement & IN_CILK_SIMD_FOR);
559 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
560 parser->in_statement & IN_OMP_FOR);
561 cp_debug_print_flag (file, "Parsing an if statement",
562 parser->in_statement & IN_IF_STMT);
563 cp_debug_print_flag (file, "Parsing a type-id in an expression "
564 "context", parser->in_type_id_in_expr_p);
565 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
566 parser->implicit_extern_c);
567 cp_debug_print_flag (file, "String expressions should be translated "
568 "to execution character set",
569 parser->translate_strings_p);
570 cp_debug_print_flag (file, "Parsing function body outside of a "
571 "local class", parser->in_function_body);
572 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
573 parser->colon_corrects_to_scope_p);
574 cp_debug_print_flag (file, "Colon doesn't start a class definition",
575 parser->colon_doesnt_start_class_def_p);
576 if (parser->type_definition_forbidden_message)
577 fprintf (file, "Error message for forbidden type definitions: %s\n",
578 parser->type_definition_forbidden_message);
579 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
580 fprintf (file, "Number of class definitions in progress: %u\n",
581 parser->num_classes_being_defined);
582 fprintf (file, "Number of template parameter lists for the current "
583 "declaration: %u\n", parser->num_template_parameter_lists);
584 cp_debug_parser_tokens (file, parser, window_size);
585 token = parser->lexer->next_token;
586 fprintf (file, "Next token to parse:\n");
587 fprintf (file, "\tToken: ");
588 cp_lexer_print_token (file, token);
589 eloc = expand_location (token->location);
590 fprintf (file, "\n\tFile: %s\n", eloc.file);
591 fprintf (file, "\tLine: %d\n", eloc.line);
592 fprintf (file, "\tColumn: %d\n", eloc.column);
595 DEBUG_FUNCTION void
596 debug (cp_parser &ref)
598 cp_debug_parser (stderr, &ref);
601 DEBUG_FUNCTION void
602 debug (cp_parser *ptr)
604 if (ptr)
605 debug (*ptr);
606 else
607 fprintf (stderr, "<nil>\n");
610 /* Allocate memory for a new lexer object and return it. */
612 static cp_lexer *
613 cp_lexer_alloc (void)
615 cp_lexer *lexer;
617 c_common_no_more_pch ();
619 /* Allocate the memory. */
620 lexer = ggc_cleared_alloc<cp_lexer> ();
622 /* Initially we are not debugging. */
623 lexer->debugging_p = false;
625 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
627 /* Create the buffer. */
628 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
630 return lexer;
634 /* Create a new main C++ lexer, the lexer that gets tokens from the
635 preprocessor. */
637 static cp_lexer *
638 cp_lexer_new_main (void)
640 cp_lexer *lexer;
641 cp_token token;
643 /* It's possible that parsing the first pragma will load a PCH file,
644 which is a GC collection point. So we have to do that before
645 allocating any memory. */
646 cp_parser_initial_pragma (&token);
648 lexer = cp_lexer_alloc ();
650 /* Put the first token in the buffer. */
651 lexer->buffer->quick_push (token);
653 /* Get the remaining tokens from the preprocessor. */
654 while (token.type != CPP_EOF)
656 cp_lexer_get_preprocessor_token (lexer, &token);
657 vec_safe_push (lexer->buffer, token);
660 lexer->last_token = lexer->buffer->address ()
661 + lexer->buffer->length ()
662 - 1;
663 lexer->next_token = lexer->buffer->length ()
664 ? lexer->buffer->address ()
665 : &eof_token;
667 /* Subsequent preprocessor diagnostics should use compiler
668 diagnostic functions to get the compiler source location. */
669 done_lexing = true;
671 gcc_assert (!lexer->next_token->purged_p);
672 return lexer;
675 /* Create a new lexer whose token stream is primed with the tokens in
676 CACHE. When these tokens are exhausted, no new tokens will be read. */
678 static cp_lexer *
679 cp_lexer_new_from_tokens (cp_token_cache *cache)
681 cp_token *first = cache->first;
682 cp_token *last = cache->last;
683 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
685 /* We do not own the buffer. */
686 lexer->buffer = NULL;
687 lexer->next_token = first == last ? &eof_token : first;
688 lexer->last_token = last;
690 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
692 /* Initially we are not debugging. */
693 lexer->debugging_p = false;
695 gcc_assert (!lexer->next_token->purged_p);
696 return lexer;
699 /* Frees all resources associated with LEXER. */
701 static void
702 cp_lexer_destroy (cp_lexer *lexer)
704 vec_free (lexer->buffer);
705 lexer->saved_tokens.release ();
706 ggc_free (lexer);
709 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
710 be used. The point of this flag is to help the compiler to fold away calls
711 to cp_lexer_debugging_p within this source file at compile time, when the
712 lexer is not being debugged. */
714 #define LEXER_DEBUGGING_ENABLED_P false
716 /* Returns nonzero if debugging information should be output. */
718 static inline bool
719 cp_lexer_debugging_p (cp_lexer *lexer)
721 if (!LEXER_DEBUGGING_ENABLED_P)
722 return false;
724 return lexer->debugging_p;
728 static inline cp_token_position
729 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
731 gcc_assert (!previous_p || lexer->next_token != &eof_token);
733 return lexer->next_token - previous_p;
736 static inline cp_token *
737 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
739 return pos;
742 static inline void
743 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
745 lexer->next_token = cp_lexer_token_at (lexer, pos);
748 static inline cp_token_position
749 cp_lexer_previous_token_position (cp_lexer *lexer)
751 if (lexer->next_token == &eof_token)
752 return lexer->last_token - 1;
753 else
754 return cp_lexer_token_position (lexer, true);
757 static inline cp_token *
758 cp_lexer_previous_token (cp_lexer *lexer)
760 cp_token_position tp = cp_lexer_previous_token_position (lexer);
762 /* Skip past purged tokens. */
763 while (tp->purged_p)
765 gcc_assert (tp != lexer->buffer->address ());
766 tp--;
769 return cp_lexer_token_at (lexer, tp);
772 /* nonzero if we are presently saving tokens. */
774 static inline int
775 cp_lexer_saving_tokens (const cp_lexer* lexer)
777 return lexer->saved_tokens.length () != 0;
780 /* Store the next token from the preprocessor in *TOKEN. Return true
781 if we reach EOF. If LEXER is NULL, assume we are handling an
782 initial #pragma pch_preprocess, and thus want the lexer to return
783 processed strings. */
785 static void
786 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
788 static int is_extern_c = 0;
790 /* Get a new token from the preprocessor. */
791 token->type
792 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
793 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
794 token->keyword = RID_MAX;
795 token->purged_p = false;
796 token->error_reported = false;
798 /* On some systems, some header files are surrounded by an
799 implicit extern "C" block. Set a flag in the token if it
800 comes from such a header. */
801 is_extern_c += pending_lang_change;
802 pending_lang_change = 0;
803 token->implicit_extern_c = is_extern_c > 0;
805 /* Check to see if this token is a keyword. */
806 if (token->type == CPP_NAME)
808 if (C_IS_RESERVED_WORD (token->u.value))
810 /* Mark this token as a keyword. */
811 token->type = CPP_KEYWORD;
812 /* Record which keyword. */
813 token->keyword = C_RID_CODE (token->u.value);
815 else
817 if (warn_cxx11_compat
818 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
819 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
821 /* Warn about the C++0x keyword (but still treat it as
822 an identifier). */
823 warning (OPT_Wc__11_compat,
824 "identifier %qE is a keyword in C++11",
825 token->u.value);
827 /* Clear out the C_RID_CODE so we don't warn about this
828 particular identifier-turned-keyword again. */
829 C_SET_RID_CODE (token->u.value, RID_MAX);
832 token->keyword = RID_MAX;
835 else if (token->type == CPP_AT_NAME)
837 /* This only happens in Objective-C++; it must be a keyword. */
838 token->type = CPP_KEYWORD;
839 switch (C_RID_CODE (token->u.value))
841 /* Replace 'class' with '@class', 'private' with '@private',
842 etc. This prevents confusion with the C++ keyword
843 'class', and makes the tokens consistent with other
844 Objective-C 'AT' keywords. For example '@class' is
845 reported as RID_AT_CLASS which is consistent with
846 '@synchronized', which is reported as
847 RID_AT_SYNCHRONIZED.
849 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
850 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
851 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
852 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
853 case RID_THROW: token->keyword = RID_AT_THROW; break;
854 case RID_TRY: token->keyword = RID_AT_TRY; break;
855 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
856 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
857 default: token->keyword = C_RID_CODE (token->u.value);
862 /* Update the globals input_location and the input file stack from TOKEN. */
863 static inline void
864 cp_lexer_set_source_position_from_token (cp_token *token)
866 if (token->type != CPP_EOF)
868 input_location = token->location;
872 /* Update the globals input_location and the input file stack from LEXER. */
873 static inline void
874 cp_lexer_set_source_position (cp_lexer *lexer)
876 cp_token *token = cp_lexer_peek_token (lexer);
877 cp_lexer_set_source_position_from_token (token);
880 /* Return a pointer to the next token in the token stream, but do not
881 consume it. */
883 static inline cp_token *
884 cp_lexer_peek_token (cp_lexer *lexer)
886 if (cp_lexer_debugging_p (lexer))
888 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
889 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
890 putc ('\n', cp_lexer_debug_stream);
892 return lexer->next_token;
895 /* Return true if the next token has the indicated TYPE. */
897 static inline bool
898 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
900 return cp_lexer_peek_token (lexer)->type == type;
903 /* Return true if the next token does not have the indicated TYPE. */
905 static inline bool
906 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
908 return !cp_lexer_next_token_is (lexer, type);
911 /* Return true if the next token is the indicated KEYWORD. */
913 static inline bool
914 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
916 return cp_lexer_peek_token (lexer)->keyword == keyword;
919 static inline bool
920 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
922 return cp_lexer_peek_nth_token (lexer, n)->type == type;
925 static inline bool
926 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
928 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
931 /* Return true if the next token is not the indicated KEYWORD. */
933 static inline bool
934 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
936 return cp_lexer_peek_token (lexer)->keyword != keyword;
939 /* Return true if the next token is a keyword for a decl-specifier. */
941 static bool
942 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
944 cp_token *token;
946 token = cp_lexer_peek_token (lexer);
947 switch (token->keyword)
949 /* auto specifier: storage-class-specifier in C++,
950 simple-type-specifier in C++0x. */
951 case RID_AUTO:
952 /* Storage classes. */
953 case RID_REGISTER:
954 case RID_STATIC:
955 case RID_EXTERN:
956 case RID_MUTABLE:
957 case RID_THREAD:
958 /* Elaborated type specifiers. */
959 case RID_ENUM:
960 case RID_CLASS:
961 case RID_STRUCT:
962 case RID_UNION:
963 case RID_TYPENAME:
964 /* Simple type specifiers. */
965 case RID_CHAR:
966 case RID_CHAR16:
967 case RID_CHAR32:
968 case RID_WCHAR:
969 case RID_BOOL:
970 case RID_SHORT:
971 case RID_INT:
972 case RID_LONG:
973 case RID_SIGNED:
974 case RID_UNSIGNED:
975 case RID_FLOAT:
976 case RID_DOUBLE:
977 case RID_VOID:
978 /* GNU extensions. */
979 case RID_ATTRIBUTE:
980 case RID_TYPEOF:
981 /* C++0x extensions. */
982 case RID_DECLTYPE:
983 case RID_UNDERLYING_TYPE:
984 return true;
986 default:
987 if (token->keyword >= RID_FIRST_INT_N
988 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
989 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
990 return true;
991 return false;
995 /* Returns TRUE iff the token T begins a decltype type. */
997 static bool
998 token_is_decltype (cp_token *t)
1000 return (t->keyword == RID_DECLTYPE
1001 || t->type == CPP_DECLTYPE);
1004 /* Returns TRUE iff the next token begins a decltype type. */
1006 static bool
1007 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1009 cp_token *t = cp_lexer_peek_token (lexer);
1010 return token_is_decltype (t);
1013 /* Called when processing a token with tree_check_value; perform or defer the
1014 associated checks and return the value. */
1016 static tree
1017 saved_checks_value (struct tree_check *check_value)
1019 /* Perform any access checks that were deferred. */
1020 vec<deferred_access_check, va_gc> *checks;
1021 deferred_access_check *chk;
1022 checks = check_value->checks;
1023 if (checks)
1025 int i;
1026 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1027 perform_or_defer_access_check (chk->binfo,
1028 chk->decl,
1029 chk->diag_decl, tf_warning_or_error);
1031 /* Return the stored value. */
1032 return check_value->value;
1035 /* Return a pointer to the Nth token in the token stream. If N is 1,
1036 then this is precisely equivalent to cp_lexer_peek_token (except
1037 that it is not inline). One would like to disallow that case, but
1038 there is one case (cp_parser_nth_token_starts_template_id) where
1039 the caller passes a variable for N and it might be 1. */
1041 static cp_token *
1042 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1044 cp_token *token;
1046 /* N is 1-based, not zero-based. */
1047 gcc_assert (n > 0);
1049 if (cp_lexer_debugging_p (lexer))
1050 fprintf (cp_lexer_debug_stream,
1051 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1053 --n;
1054 token = lexer->next_token;
1055 gcc_assert (!n || token != &eof_token);
1056 while (n != 0)
1058 ++token;
1059 if (token == lexer->last_token)
1061 token = &eof_token;
1062 break;
1065 if (!token->purged_p)
1066 --n;
1069 if (cp_lexer_debugging_p (lexer))
1071 cp_lexer_print_token (cp_lexer_debug_stream, token);
1072 putc ('\n', cp_lexer_debug_stream);
1075 return token;
1078 /* Return the next token, and advance the lexer's next_token pointer
1079 to point to the next non-purged token. */
1081 static cp_token *
1082 cp_lexer_consume_token (cp_lexer* lexer)
1084 cp_token *token = lexer->next_token;
1086 gcc_assert (token != &eof_token);
1087 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1091 lexer->next_token++;
1092 if (lexer->next_token == lexer->last_token)
1094 lexer->next_token = &eof_token;
1095 break;
1099 while (lexer->next_token->purged_p);
1101 cp_lexer_set_source_position_from_token (token);
1103 /* Provide debugging output. */
1104 if (cp_lexer_debugging_p (lexer))
1106 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1107 cp_lexer_print_token (cp_lexer_debug_stream, token);
1108 putc ('\n', cp_lexer_debug_stream);
1111 return token;
1114 /* Permanently remove the next token from the token stream, and
1115 advance the next_token pointer to refer to the next non-purged
1116 token. */
1118 static void
1119 cp_lexer_purge_token (cp_lexer *lexer)
1121 cp_token *tok = lexer->next_token;
1123 gcc_assert (tok != &eof_token);
1124 tok->purged_p = true;
1125 tok->location = UNKNOWN_LOCATION;
1126 tok->u.value = NULL_TREE;
1127 tok->keyword = RID_MAX;
1131 tok++;
1132 if (tok == lexer->last_token)
1134 tok = &eof_token;
1135 break;
1138 while (tok->purged_p);
1139 lexer->next_token = tok;
1142 /* Permanently remove all tokens after TOK, up to, but not
1143 including, the token that will be returned next by
1144 cp_lexer_peek_token. */
1146 static void
1147 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1149 cp_token *peek = lexer->next_token;
1151 if (peek == &eof_token)
1152 peek = lexer->last_token;
1154 gcc_assert (tok < peek);
1156 for ( tok += 1; tok != peek; tok += 1)
1158 tok->purged_p = true;
1159 tok->location = UNKNOWN_LOCATION;
1160 tok->u.value = NULL_TREE;
1161 tok->keyword = RID_MAX;
1165 /* Begin saving tokens. All tokens consumed after this point will be
1166 preserved. */
1168 static void
1169 cp_lexer_save_tokens (cp_lexer* lexer)
1171 /* Provide debugging output. */
1172 if (cp_lexer_debugging_p (lexer))
1173 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1175 lexer->saved_tokens.safe_push (lexer->next_token);
1178 /* Commit to the portion of the token stream most recently saved. */
1180 static void
1181 cp_lexer_commit_tokens (cp_lexer* lexer)
1183 /* Provide debugging output. */
1184 if (cp_lexer_debugging_p (lexer))
1185 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1187 lexer->saved_tokens.pop ();
1190 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1191 to the token stream. Stop saving tokens. */
1193 static void
1194 cp_lexer_rollback_tokens (cp_lexer* lexer)
1196 /* Provide debugging output. */
1197 if (cp_lexer_debugging_p (lexer))
1198 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1200 lexer->next_token = lexer->saved_tokens.pop ();
1203 /* RAII wrapper around the above functions, with sanity checking. Creating
1204 a variable saves tokens, which are committed when the variable is
1205 destroyed unless they are explicitly rolled back by calling the rollback
1206 member function. */
1208 struct saved_token_sentinel
1210 cp_lexer *lexer;
1211 unsigned len;
1212 bool commit;
1213 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1215 len = lexer->saved_tokens.length ();
1216 cp_lexer_save_tokens (lexer);
1218 void rollback ()
1220 cp_lexer_rollback_tokens (lexer);
1221 commit = false;
1223 ~saved_token_sentinel()
1225 if (commit)
1226 cp_lexer_commit_tokens (lexer);
1227 gcc_assert (lexer->saved_tokens.length () == len);
1231 /* Print a representation of the TOKEN on the STREAM. */
1233 static void
1234 cp_lexer_print_token (FILE * stream, cp_token *token)
1236 /* We don't use cpp_type2name here because the parser defines
1237 a few tokens of its own. */
1238 static const char *const token_names[] = {
1239 /* cpplib-defined token types */
1240 #define OP(e, s) #e,
1241 #define TK(e, s) #e,
1242 TTYPE_TABLE
1243 #undef OP
1244 #undef TK
1245 /* C++ parser token types - see "Manifest constants", above. */
1246 "KEYWORD",
1247 "TEMPLATE_ID",
1248 "NESTED_NAME_SPECIFIER",
1251 /* For some tokens, print the associated data. */
1252 switch (token->type)
1254 case CPP_KEYWORD:
1255 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1256 For example, `struct' is mapped to an INTEGER_CST. */
1257 if (!identifier_p (token->u.value))
1258 break;
1259 /* else fall through */
1260 case CPP_NAME:
1261 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1262 break;
1264 case CPP_STRING:
1265 case CPP_STRING16:
1266 case CPP_STRING32:
1267 case CPP_WSTRING:
1268 case CPP_UTF8STRING:
1269 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1270 break;
1272 case CPP_NUMBER:
1273 print_generic_expr (stream, token->u.value, 0);
1274 break;
1276 default:
1277 /* If we have a name for the token, print it out. Otherwise, we
1278 simply give the numeric code. */
1279 if (token->type < ARRAY_SIZE(token_names))
1280 fputs (token_names[token->type], stream);
1281 else
1282 fprintf (stream, "[%d]", token->type);
1283 break;
1287 DEBUG_FUNCTION void
1288 debug (cp_token &ref)
1290 cp_lexer_print_token (stderr, &ref);
1291 fprintf (stderr, "\n");
1294 DEBUG_FUNCTION void
1295 debug (cp_token *ptr)
1297 if (ptr)
1298 debug (*ptr);
1299 else
1300 fprintf (stderr, "<nil>\n");
1304 /* Start emitting debugging information. */
1306 static void
1307 cp_lexer_start_debugging (cp_lexer* lexer)
1309 if (!LEXER_DEBUGGING_ENABLED_P)
1310 fatal_error (input_location,
1311 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1313 lexer->debugging_p = true;
1314 cp_lexer_debug_stream = stderr;
1317 /* Stop emitting debugging information. */
1319 static void
1320 cp_lexer_stop_debugging (cp_lexer* lexer)
1322 if (!LEXER_DEBUGGING_ENABLED_P)
1323 fatal_error (input_location,
1324 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1326 lexer->debugging_p = false;
1327 cp_lexer_debug_stream = NULL;
1330 /* Create a new cp_token_cache, representing a range of tokens. */
1332 static cp_token_cache *
1333 cp_token_cache_new (cp_token *first, cp_token *last)
1335 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1336 cache->first = first;
1337 cache->last = last;
1338 return cache;
1341 /* Diagnose if #pragma omp declare simd isn't followed immediately
1342 by function declaration or definition. */
1344 static inline void
1345 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1347 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1349 error ("%<#pragma omp declare simd%> not immediately followed by "
1350 "function declaration or definition");
1351 parser->omp_declare_simd = NULL;
1355 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1356 and put that into "omp declare simd" attribute. */
1358 static inline void
1359 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1361 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1363 if (fndecl == error_mark_node)
1365 parser->omp_declare_simd = NULL;
1366 return;
1368 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1370 cp_ensure_no_omp_declare_simd (parser);
1371 return;
1376 /* Diagnose if #pragma acc routine isn't followed immediately by function
1377 declaration or definition. */
1379 static inline void
1380 cp_ensure_no_oacc_routine (cp_parser *parser)
1382 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1384 tree clauses = parser->oacc_routine->clauses;
1385 location_t loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE (clauses));
1387 error_at (loc, "%<#pragma acc routine%> not followed by a function "
1388 "declaration or definition");
1389 parser->oacc_routine = NULL;
1393 /* Decl-specifiers. */
1395 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1397 static void
1398 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1400 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1403 /* Declarators. */
1405 /* Nothing other than the parser should be creating declarators;
1406 declarators are a semi-syntactic representation of C++ entities.
1407 Other parts of the front end that need to create entities (like
1408 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1410 static cp_declarator *make_call_declarator
1411 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1412 static cp_declarator *make_array_declarator
1413 (cp_declarator *, tree);
1414 static cp_declarator *make_pointer_declarator
1415 (cp_cv_quals, cp_declarator *, tree);
1416 static cp_declarator *make_reference_declarator
1417 (cp_cv_quals, cp_declarator *, bool, tree);
1418 static cp_declarator *make_ptrmem_declarator
1419 (cp_cv_quals, tree, cp_declarator *, tree);
1421 /* An erroneous declarator. */
1422 static cp_declarator *cp_error_declarator;
1424 /* The obstack on which declarators and related data structures are
1425 allocated. */
1426 static struct obstack declarator_obstack;
1428 /* Alloc BYTES from the declarator memory pool. */
1430 static inline void *
1431 alloc_declarator (size_t bytes)
1433 return obstack_alloc (&declarator_obstack, bytes);
1436 /* Allocate a declarator of the indicated KIND. Clear fields that are
1437 common to all declarators. */
1439 static cp_declarator *
1440 make_declarator (cp_declarator_kind kind)
1442 cp_declarator *declarator;
1444 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1445 declarator->kind = kind;
1446 declarator->attributes = NULL_TREE;
1447 declarator->std_attributes = NULL_TREE;
1448 declarator->declarator = NULL;
1449 declarator->parameter_pack_p = false;
1450 declarator->id_loc = UNKNOWN_LOCATION;
1452 return declarator;
1455 /* Make a declarator for a generalized identifier. If
1456 QUALIFYING_SCOPE is non-NULL, the identifier is
1457 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1458 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1459 is, if any. */
1461 static cp_declarator *
1462 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1463 special_function_kind sfk)
1465 cp_declarator *declarator;
1467 /* It is valid to write:
1469 class C { void f(); };
1470 typedef C D;
1471 void D::f();
1473 The standard is not clear about whether `typedef const C D' is
1474 legal; as of 2002-09-15 the committee is considering that
1475 question. EDG 3.0 allows that syntax. Therefore, we do as
1476 well. */
1477 if (qualifying_scope && TYPE_P (qualifying_scope))
1478 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1480 gcc_assert (identifier_p (unqualified_name)
1481 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1482 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1484 declarator = make_declarator (cdk_id);
1485 declarator->u.id.qualifying_scope = qualifying_scope;
1486 declarator->u.id.unqualified_name = unqualified_name;
1487 declarator->u.id.sfk = sfk;
1489 return declarator;
1492 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1493 of modifiers such as const or volatile to apply to the pointer
1494 type, represented as identifiers. ATTRIBUTES represent the attributes that
1495 appertain to the pointer or reference. */
1497 cp_declarator *
1498 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1499 tree attributes)
1501 cp_declarator *declarator;
1503 declarator = make_declarator (cdk_pointer);
1504 declarator->declarator = target;
1505 declarator->u.pointer.qualifiers = cv_qualifiers;
1506 declarator->u.pointer.class_type = NULL_TREE;
1507 if (target)
1509 declarator->id_loc = target->id_loc;
1510 declarator->parameter_pack_p = target->parameter_pack_p;
1511 target->parameter_pack_p = false;
1513 else
1514 declarator->parameter_pack_p = false;
1516 declarator->std_attributes = attributes;
1518 return declarator;
1521 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1522 represent the attributes that appertain to the pointer or
1523 reference. */
1525 cp_declarator *
1526 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1527 bool rvalue_ref, tree attributes)
1529 cp_declarator *declarator;
1531 declarator = make_declarator (cdk_reference);
1532 declarator->declarator = target;
1533 declarator->u.reference.qualifiers = cv_qualifiers;
1534 declarator->u.reference.rvalue_ref = rvalue_ref;
1535 if (target)
1537 declarator->id_loc = target->id_loc;
1538 declarator->parameter_pack_p = target->parameter_pack_p;
1539 target->parameter_pack_p = false;
1541 else
1542 declarator->parameter_pack_p = false;
1544 declarator->std_attributes = attributes;
1546 return declarator;
1549 /* Like make_pointer_declarator -- but for a pointer to a non-static
1550 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1551 appertain to the pointer or reference. */
1553 cp_declarator *
1554 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1555 cp_declarator *pointee,
1556 tree attributes)
1558 cp_declarator *declarator;
1560 declarator = make_declarator (cdk_ptrmem);
1561 declarator->declarator = pointee;
1562 declarator->u.pointer.qualifiers = cv_qualifiers;
1563 declarator->u.pointer.class_type = class_type;
1565 if (pointee)
1567 declarator->parameter_pack_p = pointee->parameter_pack_p;
1568 pointee->parameter_pack_p = false;
1570 else
1571 declarator->parameter_pack_p = false;
1573 declarator->std_attributes = attributes;
1575 return declarator;
1578 /* Make a declarator for the function given by TARGET, with the
1579 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1580 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1581 indicates what exceptions can be thrown. */
1583 cp_declarator *
1584 make_call_declarator (cp_declarator *target,
1585 tree parms,
1586 cp_cv_quals cv_qualifiers,
1587 cp_virt_specifiers virt_specifiers,
1588 cp_ref_qualifier ref_qualifier,
1589 tree tx_qualifier,
1590 tree exception_specification,
1591 tree late_return_type,
1592 tree requires_clause)
1594 cp_declarator *declarator;
1596 declarator = make_declarator (cdk_function);
1597 declarator->declarator = target;
1598 declarator->u.function.parameters = parms;
1599 declarator->u.function.qualifiers = cv_qualifiers;
1600 declarator->u.function.virt_specifiers = virt_specifiers;
1601 declarator->u.function.ref_qualifier = ref_qualifier;
1602 declarator->u.function.tx_qualifier = tx_qualifier;
1603 declarator->u.function.exception_specification = exception_specification;
1604 declarator->u.function.late_return_type = late_return_type;
1605 declarator->u.function.requires_clause = requires_clause;
1606 if (target)
1608 declarator->id_loc = target->id_loc;
1609 declarator->parameter_pack_p = target->parameter_pack_p;
1610 target->parameter_pack_p = false;
1612 else
1613 declarator->parameter_pack_p = false;
1615 return declarator;
1618 /* Make a declarator for an array of BOUNDS elements, each of which is
1619 defined by ELEMENT. */
1621 cp_declarator *
1622 make_array_declarator (cp_declarator *element, tree bounds)
1624 cp_declarator *declarator;
1626 declarator = make_declarator (cdk_array);
1627 declarator->declarator = element;
1628 declarator->u.array.bounds = bounds;
1629 if (element)
1631 declarator->id_loc = element->id_loc;
1632 declarator->parameter_pack_p = element->parameter_pack_p;
1633 element->parameter_pack_p = false;
1635 else
1636 declarator->parameter_pack_p = false;
1638 return declarator;
1641 /* Determine whether the declarator we've seen so far can be a
1642 parameter pack, when followed by an ellipsis. */
1643 static bool
1644 declarator_can_be_parameter_pack (cp_declarator *declarator)
1646 if (declarator && declarator->parameter_pack_p)
1647 /* We already saw an ellipsis. */
1648 return false;
1650 /* Search for a declarator name, or any other declarator that goes
1651 after the point where the ellipsis could appear in a parameter
1652 pack. If we find any of these, then this declarator can not be
1653 made into a parameter pack. */
1654 bool found = false;
1655 while (declarator && !found)
1657 switch ((int)declarator->kind)
1659 case cdk_id:
1660 case cdk_array:
1661 found = true;
1662 break;
1664 case cdk_error:
1665 return true;
1667 default:
1668 declarator = declarator->declarator;
1669 break;
1673 return !found;
1676 cp_parameter_declarator *no_parameters;
1678 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1679 DECLARATOR and DEFAULT_ARGUMENT. */
1681 cp_parameter_declarator *
1682 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1683 cp_declarator *declarator,
1684 tree default_argument,
1685 bool template_parameter_pack_p = false)
1687 cp_parameter_declarator *parameter;
1689 parameter = ((cp_parameter_declarator *)
1690 alloc_declarator (sizeof (cp_parameter_declarator)));
1691 parameter->next = NULL;
1692 if (decl_specifiers)
1693 parameter->decl_specifiers = *decl_specifiers;
1694 else
1695 clear_decl_specs (&parameter->decl_specifiers);
1696 parameter->declarator = declarator;
1697 parameter->default_argument = default_argument;
1698 parameter->template_parameter_pack_p = template_parameter_pack_p;
1700 return parameter;
1703 /* Returns true iff DECLARATOR is a declaration for a function. */
1705 static bool
1706 function_declarator_p (const cp_declarator *declarator)
1708 while (declarator)
1710 if (declarator->kind == cdk_function
1711 && declarator->declarator->kind == cdk_id)
1712 return true;
1713 if (declarator->kind == cdk_id
1714 || declarator->kind == cdk_error)
1715 return false;
1716 declarator = declarator->declarator;
1718 return false;
1721 /* The parser. */
1723 /* Overview
1724 --------
1726 A cp_parser parses the token stream as specified by the C++
1727 grammar. Its job is purely parsing, not semantic analysis. For
1728 example, the parser breaks the token stream into declarators,
1729 expressions, statements, and other similar syntactic constructs.
1730 It does not check that the types of the expressions on either side
1731 of an assignment-statement are compatible, or that a function is
1732 not declared with a parameter of type `void'.
1734 The parser invokes routines elsewhere in the compiler to perform
1735 semantic analysis and to build up the abstract syntax tree for the
1736 code processed.
1738 The parser (and the template instantiation code, which is, in a
1739 way, a close relative of parsing) are the only parts of the
1740 compiler that should be calling push_scope and pop_scope, or
1741 related functions. The parser (and template instantiation code)
1742 keeps track of what scope is presently active; everything else
1743 should simply honor that. (The code that generates static
1744 initializers may also need to set the scope, in order to check
1745 access control correctly when emitting the initializers.)
1747 Methodology
1748 -----------
1750 The parser is of the standard recursive-descent variety. Upcoming
1751 tokens in the token stream are examined in order to determine which
1752 production to use when parsing a non-terminal. Some C++ constructs
1753 require arbitrary look ahead to disambiguate. For example, it is
1754 impossible, in the general case, to tell whether a statement is an
1755 expression or declaration without scanning the entire statement.
1756 Therefore, the parser is capable of "parsing tentatively." When the
1757 parser is not sure what construct comes next, it enters this mode.
1758 Then, while we attempt to parse the construct, the parser queues up
1759 error messages, rather than issuing them immediately, and saves the
1760 tokens it consumes. If the construct is parsed successfully, the
1761 parser "commits", i.e., it issues any queued error messages and
1762 the tokens that were being preserved are permanently discarded.
1763 If, however, the construct is not parsed successfully, the parser
1764 rolls back its state completely so that it can resume parsing using
1765 a different alternative.
1767 Future Improvements
1768 -------------------
1770 The performance of the parser could probably be improved substantially.
1771 We could often eliminate the need to parse tentatively by looking ahead
1772 a little bit. In some places, this approach might not entirely eliminate
1773 the need to parse tentatively, but it might still speed up the average
1774 case. */
1776 /* Flags that are passed to some parsing functions. These values can
1777 be bitwise-ored together. */
1779 enum
1781 /* No flags. */
1782 CP_PARSER_FLAGS_NONE = 0x0,
1783 /* The construct is optional. If it is not present, then no error
1784 should be issued. */
1785 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1786 /* When parsing a type-specifier, treat user-defined type-names
1787 as non-type identifiers. */
1788 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1789 /* When parsing a type-specifier, do not try to parse a class-specifier
1790 or enum-specifier. */
1791 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1792 /* When parsing a decl-specifier-seq, only allow type-specifier or
1793 constexpr. */
1794 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1797 /* This type is used for parameters and variables which hold
1798 combinations of the above flags. */
1799 typedef int cp_parser_flags;
1801 /* The different kinds of declarators we want to parse. */
1803 enum cp_parser_declarator_kind
1805 /* We want an abstract declarator. */
1806 CP_PARSER_DECLARATOR_ABSTRACT,
1807 /* We want a named declarator. */
1808 CP_PARSER_DECLARATOR_NAMED,
1809 /* We don't mind, but the name must be an unqualified-id. */
1810 CP_PARSER_DECLARATOR_EITHER
1813 /* The precedence values used to parse binary expressions. The minimum value
1814 of PREC must be 1, because zero is reserved to quickly discriminate
1815 binary operators from other tokens. */
1817 enum cp_parser_prec
1819 PREC_NOT_OPERATOR,
1820 PREC_LOGICAL_OR_EXPRESSION,
1821 PREC_LOGICAL_AND_EXPRESSION,
1822 PREC_INCLUSIVE_OR_EXPRESSION,
1823 PREC_EXCLUSIVE_OR_EXPRESSION,
1824 PREC_AND_EXPRESSION,
1825 PREC_EQUALITY_EXPRESSION,
1826 PREC_RELATIONAL_EXPRESSION,
1827 PREC_SHIFT_EXPRESSION,
1828 PREC_ADDITIVE_EXPRESSION,
1829 PREC_MULTIPLICATIVE_EXPRESSION,
1830 PREC_PM_EXPRESSION,
1831 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1834 /* A mapping from a token type to a corresponding tree node type, with a
1835 precedence value. */
1837 struct cp_parser_binary_operations_map_node
1839 /* The token type. */
1840 enum cpp_ttype token_type;
1841 /* The corresponding tree code. */
1842 enum tree_code tree_type;
1843 /* The precedence of this operator. */
1844 enum cp_parser_prec prec;
1847 struct cp_parser_expression_stack_entry
1849 /* Left hand side of the binary operation we are currently
1850 parsing. */
1851 cp_expr lhs;
1852 /* Original tree code for left hand side, if it was a binary
1853 expression itself (used for -Wparentheses). */
1854 enum tree_code lhs_type;
1855 /* Tree code for the binary operation we are parsing. */
1856 enum tree_code tree_type;
1857 /* Precedence of the binary operation we are parsing. */
1858 enum cp_parser_prec prec;
1859 /* Location of the binary operation we are parsing. */
1860 location_t loc;
1863 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1864 entries because precedence levels on the stack are monotonically
1865 increasing. */
1866 typedef struct cp_parser_expression_stack_entry
1867 cp_parser_expression_stack[NUM_PREC_VALUES];
1869 /* Prototypes. */
1871 /* Constructors and destructors. */
1873 static cp_parser_context *cp_parser_context_new
1874 (cp_parser_context *);
1876 /* Class variables. */
1878 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1880 /* The operator-precedence table used by cp_parser_binary_expression.
1881 Transformed into an associative array (binops_by_token) by
1882 cp_parser_new. */
1884 static const cp_parser_binary_operations_map_node binops[] = {
1885 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1886 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1888 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1889 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1890 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1892 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1893 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1895 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1896 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1898 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1899 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1900 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1901 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1903 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1904 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1906 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1908 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1910 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1912 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1914 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1917 /* The same as binops, but initialized by cp_parser_new so that
1918 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1919 for speed. */
1920 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1922 /* Constructors and destructors. */
1924 /* Construct a new context. The context below this one on the stack
1925 is given by NEXT. */
1927 static cp_parser_context *
1928 cp_parser_context_new (cp_parser_context* next)
1930 cp_parser_context *context;
1932 /* Allocate the storage. */
1933 if (cp_parser_context_free_list != NULL)
1935 /* Pull the first entry from the free list. */
1936 context = cp_parser_context_free_list;
1937 cp_parser_context_free_list = context->next;
1938 memset (context, 0, sizeof (*context));
1940 else
1941 context = ggc_cleared_alloc<cp_parser_context> ();
1943 /* No errors have occurred yet in this context. */
1944 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1945 /* If this is not the bottommost context, copy information that we
1946 need from the previous context. */
1947 if (next)
1949 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1950 expression, then we are parsing one in this context, too. */
1951 context->object_type = next->object_type;
1952 /* Thread the stack. */
1953 context->next = next;
1956 return context;
1959 /* Managing the unparsed function queues. */
1961 #define unparsed_funs_with_default_args \
1962 parser->unparsed_queues->last ().funs_with_default_args
1963 #define unparsed_funs_with_definitions \
1964 parser->unparsed_queues->last ().funs_with_definitions
1965 #define unparsed_nsdmis \
1966 parser->unparsed_queues->last ().nsdmis
1967 #define unparsed_classes \
1968 parser->unparsed_queues->last ().classes
1970 static void
1971 push_unparsed_function_queues (cp_parser *parser)
1973 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1974 vec_safe_push (parser->unparsed_queues, e);
1977 static void
1978 pop_unparsed_function_queues (cp_parser *parser)
1980 release_tree_vector (unparsed_funs_with_definitions);
1981 parser->unparsed_queues->pop ();
1984 /* Prototypes. */
1986 /* Constructors and destructors. */
1988 static cp_parser *cp_parser_new
1989 (void);
1991 /* Routines to parse various constructs.
1993 Those that return `tree' will return the error_mark_node (rather
1994 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1995 Sometimes, they will return an ordinary node if error-recovery was
1996 attempted, even though a parse error occurred. So, to check
1997 whether or not a parse error occurred, you should always use
1998 cp_parser_error_occurred. If the construct is optional (indicated
1999 either by an `_opt' in the name of the function that does the
2000 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2001 the construct is not present. */
2003 /* Lexical conventions [gram.lex] */
2005 static cp_expr cp_parser_identifier
2006 (cp_parser *);
2007 static cp_expr cp_parser_string_literal
2008 (cp_parser *, bool, bool, bool);
2009 static cp_expr cp_parser_userdef_char_literal
2010 (cp_parser *);
2011 static tree cp_parser_userdef_string_literal
2012 (tree);
2013 static cp_expr cp_parser_userdef_numeric_literal
2014 (cp_parser *);
2016 /* Basic concepts [gram.basic] */
2018 static bool cp_parser_translation_unit
2019 (cp_parser *);
2021 /* Expressions [gram.expr] */
2023 static cp_expr cp_parser_primary_expression
2024 (cp_parser *, bool, bool, bool, cp_id_kind *);
2025 static cp_expr cp_parser_id_expression
2026 (cp_parser *, bool, bool, bool *, bool, bool);
2027 static cp_expr cp_parser_unqualified_id
2028 (cp_parser *, bool, bool, bool, bool);
2029 static tree cp_parser_nested_name_specifier_opt
2030 (cp_parser *, bool, bool, bool, bool);
2031 static tree cp_parser_nested_name_specifier
2032 (cp_parser *, bool, bool, bool, bool);
2033 static tree cp_parser_qualifying_entity
2034 (cp_parser *, bool, bool, bool, bool, bool);
2035 static cp_expr cp_parser_postfix_expression
2036 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2037 static tree cp_parser_postfix_open_square_expression
2038 (cp_parser *, tree, bool, bool);
2039 static tree cp_parser_postfix_dot_deref_expression
2040 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2041 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2042 (cp_parser *, int, bool, bool, bool *, location_t * = NULL);
2043 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2044 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2045 static void cp_parser_pseudo_destructor_name
2046 (cp_parser *, tree, tree *, tree *);
2047 static cp_expr cp_parser_unary_expression
2048 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2049 static enum tree_code cp_parser_unary_operator
2050 (cp_token *);
2051 static tree cp_parser_new_expression
2052 (cp_parser *);
2053 static vec<tree, va_gc> *cp_parser_new_placement
2054 (cp_parser *);
2055 static tree cp_parser_new_type_id
2056 (cp_parser *, tree *);
2057 static cp_declarator *cp_parser_new_declarator_opt
2058 (cp_parser *);
2059 static cp_declarator *cp_parser_direct_new_declarator
2060 (cp_parser *);
2061 static vec<tree, va_gc> *cp_parser_new_initializer
2062 (cp_parser *);
2063 static tree cp_parser_delete_expression
2064 (cp_parser *);
2065 static cp_expr cp_parser_cast_expression
2066 (cp_parser *, bool, bool, bool, cp_id_kind *);
2067 static cp_expr cp_parser_binary_expression
2068 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2069 static tree cp_parser_question_colon_clause
2070 (cp_parser *, cp_expr);
2071 static cp_expr cp_parser_assignment_expression
2072 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2073 static enum tree_code cp_parser_assignment_operator_opt
2074 (cp_parser *);
2075 static cp_expr cp_parser_expression
2076 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2077 static cp_expr cp_parser_constant_expression
2078 (cp_parser *, bool = false, bool * = NULL);
2079 static cp_expr cp_parser_builtin_offsetof
2080 (cp_parser *);
2081 static cp_expr cp_parser_lambda_expression
2082 (cp_parser *);
2083 static void cp_parser_lambda_introducer
2084 (cp_parser *, tree);
2085 static bool cp_parser_lambda_declarator_opt
2086 (cp_parser *, tree);
2087 static void cp_parser_lambda_body
2088 (cp_parser *, tree);
2090 /* Statements [gram.stmt.stmt] */
2092 static void cp_parser_statement
2093 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL);
2094 static void cp_parser_label_for_labeled_statement
2095 (cp_parser *, tree);
2096 static tree cp_parser_expression_statement
2097 (cp_parser *, tree);
2098 static tree cp_parser_compound_statement
2099 (cp_parser *, tree, int, bool);
2100 static void cp_parser_statement_seq_opt
2101 (cp_parser *, tree);
2102 static tree cp_parser_selection_statement
2103 (cp_parser *, bool *, vec<tree> *);
2104 static tree cp_parser_condition
2105 (cp_parser *);
2106 static tree cp_parser_iteration_statement
2107 (cp_parser *, bool);
2108 static bool cp_parser_for_init_statement
2109 (cp_parser *, tree *decl);
2110 static tree cp_parser_for
2111 (cp_parser *, bool);
2112 static tree cp_parser_c_for
2113 (cp_parser *, tree, tree, bool);
2114 static tree cp_parser_range_for
2115 (cp_parser *, tree, tree, tree, bool);
2116 static void do_range_for_auto_deduction
2117 (tree, tree);
2118 static tree cp_parser_perform_range_for_lookup
2119 (tree, tree *, tree *);
2120 static tree cp_parser_range_for_member_function
2121 (tree, tree);
2122 static tree cp_parser_jump_statement
2123 (cp_parser *);
2124 static void cp_parser_declaration_statement
2125 (cp_parser *);
2127 static tree cp_parser_implicitly_scoped_statement
2128 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2129 static void cp_parser_already_scoped_statement
2130 (cp_parser *, const token_indent_info &);
2132 /* Declarations [gram.dcl.dcl] */
2134 static void cp_parser_declaration_seq_opt
2135 (cp_parser *);
2136 static void cp_parser_declaration
2137 (cp_parser *);
2138 static void cp_parser_block_declaration
2139 (cp_parser *, bool);
2140 static void cp_parser_simple_declaration
2141 (cp_parser *, bool, tree *);
2142 static void cp_parser_decl_specifier_seq
2143 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2144 static tree cp_parser_storage_class_specifier_opt
2145 (cp_parser *);
2146 static tree cp_parser_function_specifier_opt
2147 (cp_parser *, cp_decl_specifier_seq *);
2148 static tree cp_parser_type_specifier
2149 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2150 int *, bool *);
2151 static tree cp_parser_simple_type_specifier
2152 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2153 static tree cp_parser_type_name
2154 (cp_parser *, bool);
2155 static tree cp_parser_type_name
2156 (cp_parser *);
2157 static tree cp_parser_nonclass_name
2158 (cp_parser* parser);
2159 static tree cp_parser_elaborated_type_specifier
2160 (cp_parser *, bool, bool);
2161 static tree cp_parser_enum_specifier
2162 (cp_parser *);
2163 static void cp_parser_enumerator_list
2164 (cp_parser *, tree);
2165 static void cp_parser_enumerator_definition
2166 (cp_parser *, tree);
2167 static tree cp_parser_namespace_name
2168 (cp_parser *);
2169 static void cp_parser_namespace_definition
2170 (cp_parser *);
2171 static void cp_parser_namespace_body
2172 (cp_parser *);
2173 static tree cp_parser_qualified_namespace_specifier
2174 (cp_parser *);
2175 static void cp_parser_namespace_alias_definition
2176 (cp_parser *);
2177 static bool cp_parser_using_declaration
2178 (cp_parser *, bool);
2179 static void cp_parser_using_directive
2180 (cp_parser *);
2181 static tree cp_parser_alias_declaration
2182 (cp_parser *);
2183 static void cp_parser_asm_definition
2184 (cp_parser *);
2185 static void cp_parser_linkage_specification
2186 (cp_parser *);
2187 static void cp_parser_static_assert
2188 (cp_parser *, bool);
2189 static tree cp_parser_decltype
2190 (cp_parser *);
2192 /* Declarators [gram.dcl.decl] */
2194 static tree cp_parser_init_declarator
2195 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2196 bool, bool, int, bool *, tree *, location_t *);
2197 static cp_declarator *cp_parser_declarator
2198 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2199 static cp_declarator *cp_parser_direct_declarator
2200 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2201 static enum tree_code cp_parser_ptr_operator
2202 (cp_parser *, tree *, cp_cv_quals *, tree *);
2203 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2204 (cp_parser *);
2205 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2206 (cp_parser *);
2207 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2208 (cp_parser *);
2209 static tree cp_parser_tx_qualifier_opt
2210 (cp_parser *);
2211 static tree cp_parser_late_return_type_opt
2212 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2213 static tree cp_parser_declarator_id
2214 (cp_parser *, bool);
2215 static tree cp_parser_type_id
2216 (cp_parser *);
2217 static tree cp_parser_template_type_arg
2218 (cp_parser *);
2219 static tree cp_parser_trailing_type_id (cp_parser *);
2220 static tree cp_parser_type_id_1
2221 (cp_parser *, bool, bool);
2222 static void cp_parser_type_specifier_seq
2223 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2224 static tree cp_parser_parameter_declaration_clause
2225 (cp_parser *);
2226 static tree cp_parser_parameter_declaration_list
2227 (cp_parser *, bool *);
2228 static cp_parameter_declarator *cp_parser_parameter_declaration
2229 (cp_parser *, bool, bool *);
2230 static tree cp_parser_default_argument
2231 (cp_parser *, bool);
2232 static void cp_parser_function_body
2233 (cp_parser *, bool);
2234 static tree cp_parser_initializer
2235 (cp_parser *, bool *, bool *);
2236 static cp_expr cp_parser_initializer_clause
2237 (cp_parser *, bool *);
2238 static cp_expr cp_parser_braced_list
2239 (cp_parser*, bool*);
2240 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2241 (cp_parser *, bool *);
2243 static bool cp_parser_ctor_initializer_opt_and_function_body
2244 (cp_parser *, bool);
2246 static tree cp_parser_late_parsing_omp_declare_simd
2247 (cp_parser *, tree);
2249 static tree cp_parser_late_parsing_cilk_simd_fn_info
2250 (cp_parser *, tree);
2252 static tree cp_parser_late_parsing_oacc_routine
2253 (cp_parser *, tree);
2255 static tree synthesize_implicit_template_parm
2256 (cp_parser *, tree);
2257 static tree finish_fully_implicit_template
2258 (cp_parser *, tree);
2260 /* Classes [gram.class] */
2262 static tree cp_parser_class_name
2263 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2264 static tree cp_parser_class_specifier
2265 (cp_parser *);
2266 static tree cp_parser_class_head
2267 (cp_parser *, bool *);
2268 static enum tag_types cp_parser_class_key
2269 (cp_parser *);
2270 static void cp_parser_type_parameter_key
2271 (cp_parser* parser);
2272 static void cp_parser_member_specification_opt
2273 (cp_parser *);
2274 static void cp_parser_member_declaration
2275 (cp_parser *);
2276 static tree cp_parser_pure_specifier
2277 (cp_parser *);
2278 static tree cp_parser_constant_initializer
2279 (cp_parser *);
2281 /* Derived classes [gram.class.derived] */
2283 static tree cp_parser_base_clause
2284 (cp_parser *);
2285 static tree cp_parser_base_specifier
2286 (cp_parser *);
2288 /* Special member functions [gram.special] */
2290 static tree cp_parser_conversion_function_id
2291 (cp_parser *);
2292 static tree cp_parser_conversion_type_id
2293 (cp_parser *);
2294 static cp_declarator *cp_parser_conversion_declarator_opt
2295 (cp_parser *);
2296 static bool cp_parser_ctor_initializer_opt
2297 (cp_parser *);
2298 static void cp_parser_mem_initializer_list
2299 (cp_parser *);
2300 static tree cp_parser_mem_initializer
2301 (cp_parser *);
2302 static tree cp_parser_mem_initializer_id
2303 (cp_parser *);
2305 /* Overloading [gram.over] */
2307 static cp_expr cp_parser_operator_function_id
2308 (cp_parser *);
2309 static cp_expr cp_parser_operator
2310 (cp_parser *);
2312 /* Templates [gram.temp] */
2314 static void cp_parser_template_declaration
2315 (cp_parser *, bool);
2316 static tree cp_parser_template_parameter_list
2317 (cp_parser *);
2318 static tree cp_parser_template_parameter
2319 (cp_parser *, bool *, bool *);
2320 static tree cp_parser_type_parameter
2321 (cp_parser *, bool *);
2322 static tree cp_parser_template_id
2323 (cp_parser *, bool, bool, enum tag_types, bool);
2324 static tree cp_parser_template_name
2325 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2326 static tree cp_parser_template_argument_list
2327 (cp_parser *);
2328 static tree cp_parser_template_argument
2329 (cp_parser *);
2330 static void cp_parser_explicit_instantiation
2331 (cp_parser *);
2332 static void cp_parser_explicit_specialization
2333 (cp_parser *);
2335 /* Exception handling [gram.exception] */
2337 static tree cp_parser_try_block
2338 (cp_parser *);
2339 static bool cp_parser_function_try_block
2340 (cp_parser *);
2341 static void cp_parser_handler_seq
2342 (cp_parser *);
2343 static void cp_parser_handler
2344 (cp_parser *);
2345 static tree cp_parser_exception_declaration
2346 (cp_parser *);
2347 static tree cp_parser_throw_expression
2348 (cp_parser *);
2349 static tree cp_parser_exception_specification_opt
2350 (cp_parser *);
2351 static tree cp_parser_type_id_list
2352 (cp_parser *);
2354 /* GNU Extensions */
2356 static tree cp_parser_asm_specification_opt
2357 (cp_parser *);
2358 static tree cp_parser_asm_operand_list
2359 (cp_parser *);
2360 static tree cp_parser_asm_clobber_list
2361 (cp_parser *);
2362 static tree cp_parser_asm_label_list
2363 (cp_parser *);
2364 static bool cp_next_tokens_can_be_attribute_p
2365 (cp_parser *);
2366 static bool cp_next_tokens_can_be_gnu_attribute_p
2367 (cp_parser *);
2368 static bool cp_next_tokens_can_be_std_attribute_p
2369 (cp_parser *);
2370 static bool cp_nth_tokens_can_be_std_attribute_p
2371 (cp_parser *, size_t);
2372 static bool cp_nth_tokens_can_be_gnu_attribute_p
2373 (cp_parser *, size_t);
2374 static bool cp_nth_tokens_can_be_attribute_p
2375 (cp_parser *, size_t);
2376 static tree cp_parser_attributes_opt
2377 (cp_parser *);
2378 static tree cp_parser_gnu_attributes_opt
2379 (cp_parser *);
2380 static tree cp_parser_gnu_attribute_list
2381 (cp_parser *);
2382 static tree cp_parser_std_attribute
2383 (cp_parser *);
2384 static tree cp_parser_std_attribute_spec
2385 (cp_parser *);
2386 static tree cp_parser_std_attribute_spec_seq
2387 (cp_parser *);
2388 static bool cp_parser_extension_opt
2389 (cp_parser *, int *);
2390 static void cp_parser_label_declaration
2391 (cp_parser *);
2393 /* Concept Extensions */
2395 static tree cp_parser_requires_clause
2396 (cp_parser *);
2397 static tree cp_parser_requires_clause_opt
2398 (cp_parser *);
2399 static tree cp_parser_requires_expression
2400 (cp_parser *);
2401 static tree cp_parser_requirement_parameter_list
2402 (cp_parser *);
2403 static tree cp_parser_requirement_body
2404 (cp_parser *);
2405 static tree cp_parser_requirement_list
2406 (cp_parser *);
2407 static tree cp_parser_requirement
2408 (cp_parser *);
2409 static tree cp_parser_simple_requirement
2410 (cp_parser *);
2411 static tree cp_parser_compound_requirement
2412 (cp_parser *);
2413 static tree cp_parser_type_requirement
2414 (cp_parser *);
2415 static tree cp_parser_nested_requirement
2416 (cp_parser *);
2418 /* Transactional Memory Extensions */
2420 static tree cp_parser_transaction
2421 (cp_parser *, cp_token *);
2422 static tree cp_parser_transaction_expression
2423 (cp_parser *, enum rid);
2424 static bool cp_parser_function_transaction
2425 (cp_parser *, enum rid);
2426 static tree cp_parser_transaction_cancel
2427 (cp_parser *);
2429 enum pragma_context {
2430 pragma_external,
2431 pragma_member,
2432 pragma_objc_icode,
2433 pragma_stmt,
2434 pragma_compound
2436 static bool cp_parser_pragma
2437 (cp_parser *, enum pragma_context);
2439 /* Objective-C++ Productions */
2441 static tree cp_parser_objc_message_receiver
2442 (cp_parser *);
2443 static tree cp_parser_objc_message_args
2444 (cp_parser *);
2445 static tree cp_parser_objc_message_expression
2446 (cp_parser *);
2447 static cp_expr cp_parser_objc_encode_expression
2448 (cp_parser *);
2449 static tree cp_parser_objc_defs_expression
2450 (cp_parser *);
2451 static tree cp_parser_objc_protocol_expression
2452 (cp_parser *);
2453 static tree cp_parser_objc_selector_expression
2454 (cp_parser *);
2455 static cp_expr cp_parser_objc_expression
2456 (cp_parser *);
2457 static bool cp_parser_objc_selector_p
2458 (enum cpp_ttype);
2459 static tree cp_parser_objc_selector
2460 (cp_parser *);
2461 static tree cp_parser_objc_protocol_refs_opt
2462 (cp_parser *);
2463 static void cp_parser_objc_declaration
2464 (cp_parser *, tree);
2465 static tree cp_parser_objc_statement
2466 (cp_parser *);
2467 static bool cp_parser_objc_valid_prefix_attributes
2468 (cp_parser *, tree *);
2469 static void cp_parser_objc_at_property_declaration
2470 (cp_parser *) ;
2471 static void cp_parser_objc_at_synthesize_declaration
2472 (cp_parser *) ;
2473 static void cp_parser_objc_at_dynamic_declaration
2474 (cp_parser *) ;
2475 static tree cp_parser_objc_struct_declaration
2476 (cp_parser *) ;
2478 /* Utility Routines */
2480 static cp_expr cp_parser_lookup_name
2481 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2482 static tree cp_parser_lookup_name_simple
2483 (cp_parser *, tree, location_t);
2484 static tree cp_parser_maybe_treat_template_as_class
2485 (tree, bool);
2486 static bool cp_parser_check_declarator_template_parameters
2487 (cp_parser *, cp_declarator *, location_t);
2488 static bool cp_parser_check_template_parameters
2489 (cp_parser *, unsigned, location_t, cp_declarator *);
2490 static cp_expr cp_parser_simple_cast_expression
2491 (cp_parser *);
2492 static tree cp_parser_global_scope_opt
2493 (cp_parser *, bool);
2494 static bool cp_parser_constructor_declarator_p
2495 (cp_parser *, bool);
2496 static tree cp_parser_function_definition_from_specifiers_and_declarator
2497 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2498 static tree cp_parser_function_definition_after_declarator
2499 (cp_parser *, bool);
2500 static bool cp_parser_template_declaration_after_export
2501 (cp_parser *, bool);
2502 static void cp_parser_perform_template_parameter_access_checks
2503 (vec<deferred_access_check, va_gc> *);
2504 static tree cp_parser_single_declaration
2505 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2506 static cp_expr cp_parser_functional_cast
2507 (cp_parser *, tree);
2508 static tree cp_parser_save_member_function_body
2509 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2510 static tree cp_parser_save_nsdmi
2511 (cp_parser *);
2512 static tree cp_parser_enclosed_template_argument_list
2513 (cp_parser *);
2514 static void cp_parser_save_default_args
2515 (cp_parser *, tree);
2516 static void cp_parser_late_parsing_for_member
2517 (cp_parser *, tree);
2518 static tree cp_parser_late_parse_one_default_arg
2519 (cp_parser *, tree, tree, tree);
2520 static void cp_parser_late_parsing_nsdmi
2521 (cp_parser *, tree);
2522 static void cp_parser_late_parsing_default_args
2523 (cp_parser *, tree);
2524 static tree cp_parser_sizeof_operand
2525 (cp_parser *, enum rid);
2526 static tree cp_parser_trait_expr
2527 (cp_parser *, enum rid);
2528 static bool cp_parser_declares_only_class_p
2529 (cp_parser *);
2530 static void cp_parser_set_storage_class
2531 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2532 static void cp_parser_set_decl_spec_type
2533 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2534 static void set_and_check_decl_spec_loc
2535 (cp_decl_specifier_seq *decl_specs,
2536 cp_decl_spec ds, cp_token *);
2537 static bool cp_parser_friend_p
2538 (const cp_decl_specifier_seq *);
2539 static void cp_parser_required_error
2540 (cp_parser *, required_token, bool);
2541 static cp_token *cp_parser_require
2542 (cp_parser *, enum cpp_ttype, required_token);
2543 static cp_token *cp_parser_require_keyword
2544 (cp_parser *, enum rid, required_token);
2545 static bool cp_parser_token_starts_function_definition_p
2546 (cp_token *);
2547 static bool cp_parser_next_token_starts_class_definition_p
2548 (cp_parser *);
2549 static bool cp_parser_next_token_ends_template_argument_p
2550 (cp_parser *);
2551 static bool cp_parser_nth_token_starts_template_argument_list_p
2552 (cp_parser *, size_t);
2553 static enum tag_types cp_parser_token_is_class_key
2554 (cp_token *);
2555 static enum tag_types cp_parser_token_is_type_parameter_key
2556 (cp_token *);
2557 static void cp_parser_check_class_key
2558 (enum tag_types, tree type);
2559 static void cp_parser_check_access_in_redeclaration
2560 (tree type, location_t location);
2561 static bool cp_parser_optional_template_keyword
2562 (cp_parser *);
2563 static void cp_parser_pre_parsed_nested_name_specifier
2564 (cp_parser *);
2565 static bool cp_parser_cache_group
2566 (cp_parser *, enum cpp_ttype, unsigned);
2567 static tree cp_parser_cache_defarg
2568 (cp_parser *parser, bool nsdmi);
2569 static void cp_parser_parse_tentatively
2570 (cp_parser *);
2571 static void cp_parser_commit_to_tentative_parse
2572 (cp_parser *);
2573 static void cp_parser_commit_to_topmost_tentative_parse
2574 (cp_parser *);
2575 static void cp_parser_abort_tentative_parse
2576 (cp_parser *);
2577 static bool cp_parser_parse_definitely
2578 (cp_parser *);
2579 static inline bool cp_parser_parsing_tentatively
2580 (cp_parser *);
2581 static bool cp_parser_uncommitted_to_tentative_parse_p
2582 (cp_parser *);
2583 static void cp_parser_error
2584 (cp_parser *, const char *);
2585 static void cp_parser_name_lookup_error
2586 (cp_parser *, tree, tree, name_lookup_error, location_t);
2587 static bool cp_parser_simulate_error
2588 (cp_parser *);
2589 static bool cp_parser_check_type_definition
2590 (cp_parser *);
2591 static void cp_parser_check_for_definition_in_return_type
2592 (cp_declarator *, tree, location_t type_location);
2593 static void cp_parser_check_for_invalid_template_id
2594 (cp_parser *, tree, enum tag_types, location_t location);
2595 static bool cp_parser_non_integral_constant_expression
2596 (cp_parser *, non_integral_constant);
2597 static void cp_parser_diagnose_invalid_type_name
2598 (cp_parser *, tree, location_t);
2599 static bool cp_parser_parse_and_diagnose_invalid_type_name
2600 (cp_parser *);
2601 static int cp_parser_skip_to_closing_parenthesis
2602 (cp_parser *, bool, bool, bool);
2603 static void cp_parser_skip_to_end_of_statement
2604 (cp_parser *);
2605 static void cp_parser_consume_semicolon_at_end_of_statement
2606 (cp_parser *);
2607 static void cp_parser_skip_to_end_of_block_or_statement
2608 (cp_parser *);
2609 static bool cp_parser_skip_to_closing_brace
2610 (cp_parser *);
2611 static void cp_parser_skip_to_end_of_template_parameter_list
2612 (cp_parser *);
2613 static void cp_parser_skip_to_pragma_eol
2614 (cp_parser*, cp_token *);
2615 static bool cp_parser_error_occurred
2616 (cp_parser *);
2617 static bool cp_parser_allow_gnu_extensions_p
2618 (cp_parser *);
2619 static bool cp_parser_is_pure_string_literal
2620 (cp_token *);
2621 static bool cp_parser_is_string_literal
2622 (cp_token *);
2623 static bool cp_parser_is_keyword
2624 (cp_token *, enum rid);
2625 static tree cp_parser_make_typename_type
2626 (cp_parser *, tree, location_t location);
2627 static cp_declarator * cp_parser_make_indirect_declarator
2628 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2629 static bool cp_parser_compound_literal_p
2630 (cp_parser *);
2631 static bool cp_parser_array_designator_p
2632 (cp_parser *);
2633 static bool cp_parser_skip_to_closing_square_bracket
2634 (cp_parser *);
2636 /* Concept-related syntactic transformations */
2638 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2639 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2641 // -------------------------------------------------------------------------- //
2642 // Unevaluated Operand Guard
2644 // Implementation of an RAII helper for unevaluated operand parsing.
2645 cp_unevaluated::cp_unevaluated ()
2647 ++cp_unevaluated_operand;
2648 ++c_inhibit_evaluation_warnings;
2651 cp_unevaluated::~cp_unevaluated ()
2653 --c_inhibit_evaluation_warnings;
2654 --cp_unevaluated_operand;
2657 // -------------------------------------------------------------------------- //
2658 // Tentative Parsing
2660 /* Returns nonzero if we are parsing tentatively. */
2662 static inline bool
2663 cp_parser_parsing_tentatively (cp_parser* parser)
2665 return parser->context->next != NULL;
2668 /* Returns nonzero if TOKEN is a string literal. */
2670 static bool
2671 cp_parser_is_pure_string_literal (cp_token* token)
2673 return (token->type == CPP_STRING ||
2674 token->type == CPP_STRING16 ||
2675 token->type == CPP_STRING32 ||
2676 token->type == CPP_WSTRING ||
2677 token->type == CPP_UTF8STRING);
2680 /* Returns nonzero if TOKEN is a string literal
2681 of a user-defined string literal. */
2683 static bool
2684 cp_parser_is_string_literal (cp_token* token)
2686 return (cp_parser_is_pure_string_literal (token) ||
2687 token->type == CPP_STRING_USERDEF ||
2688 token->type == CPP_STRING16_USERDEF ||
2689 token->type == CPP_STRING32_USERDEF ||
2690 token->type == CPP_WSTRING_USERDEF ||
2691 token->type == CPP_UTF8STRING_USERDEF);
2694 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2696 static bool
2697 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2699 return token->keyword == keyword;
2702 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2703 PRAGMA_NONE. */
2705 static enum pragma_kind
2706 cp_parser_pragma_kind (cp_token *token)
2708 if (token->type != CPP_PRAGMA)
2709 return PRAGMA_NONE;
2710 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2711 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2714 /* Helper function for cp_parser_error.
2715 Having peeked a token of kind TOK1_KIND that might signify
2716 a conflict marker, peek successor tokens to determine
2717 if we actually do have a conflict marker.
2718 Specifically, we consider a run of 7 '<', '=' or '>' characters
2719 at the start of a line as a conflict marker.
2720 These come through the lexer as three pairs and a single,
2721 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2722 If it returns true, *OUT_LOC is written to with the location/range
2723 of the marker. */
2725 static bool
2726 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2727 location_t *out_loc)
2729 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2730 if (token2->type != tok1_kind)
2731 return false;
2732 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2733 if (token3->type != tok1_kind)
2734 return false;
2735 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2736 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2737 return false;
2739 /* It must be at the start of the line. */
2740 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2741 if (LOCATION_COLUMN (start_loc) != 1)
2742 return false;
2744 /* We have a conflict marker. Construct a location of the form:
2745 <<<<<<<
2746 ^~~~~~~
2747 with start == caret, finishing at the end of the marker. */
2748 location_t finish_loc = get_finish (token4->location);
2749 *out_loc = make_location (start_loc, start_loc, finish_loc);
2751 return true;
2754 /* If not parsing tentatively, issue a diagnostic of the form
2755 FILE:LINE: MESSAGE before TOKEN
2756 where TOKEN is the next token in the input stream. MESSAGE
2757 (specified by the caller) is usually of the form "expected
2758 OTHER-TOKEN". */
2760 static void
2761 cp_parser_error (cp_parser* parser, const char* gmsgid)
2763 if (!cp_parser_simulate_error (parser))
2765 cp_token *token = cp_lexer_peek_token (parser->lexer);
2766 /* This diagnostic makes more sense if it is tagged to the line
2767 of the token we just peeked at. */
2768 cp_lexer_set_source_position_from_token (token);
2770 if (token->type == CPP_PRAGMA)
2772 error_at (token->location,
2773 "%<#pragma%> is not allowed here");
2774 cp_parser_skip_to_pragma_eol (parser, token);
2775 return;
2778 /* If this is actually a conflict marker, report it as such. */
2779 if (token->type == CPP_LSHIFT
2780 || token->type == CPP_RSHIFT
2781 || token->type == CPP_EQ_EQ)
2783 location_t loc;
2784 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2786 error_at (loc, "version control conflict marker in file");
2787 return;
2791 c_parse_error (gmsgid,
2792 /* Because c_parser_error does not understand
2793 CPP_KEYWORD, keywords are treated like
2794 identifiers. */
2795 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2796 token->u.value, token->flags);
2800 /* Issue an error about name-lookup failing. NAME is the
2801 IDENTIFIER_NODE DECL is the result of
2802 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2803 the thing that we hoped to find. */
2805 static void
2806 cp_parser_name_lookup_error (cp_parser* parser,
2807 tree name,
2808 tree decl,
2809 name_lookup_error desired,
2810 location_t location)
2812 /* If name lookup completely failed, tell the user that NAME was not
2813 declared. */
2814 if (decl == error_mark_node)
2816 if (parser->scope && parser->scope != global_namespace)
2817 error_at (location, "%<%E::%E%> has not been declared",
2818 parser->scope, name);
2819 else if (parser->scope == global_namespace)
2820 error_at (location, "%<::%E%> has not been declared", name);
2821 else if (parser->object_scope
2822 && !CLASS_TYPE_P (parser->object_scope))
2823 error_at (location, "request for member %qE in non-class type %qT",
2824 name, parser->object_scope);
2825 else if (parser->object_scope)
2826 error_at (location, "%<%T::%E%> has not been declared",
2827 parser->object_scope, name);
2828 else
2829 error_at (location, "%qE has not been declared", name);
2831 else if (parser->scope && parser->scope != global_namespace)
2833 switch (desired)
2835 case NLE_TYPE:
2836 error_at (location, "%<%E::%E%> is not a type",
2837 parser->scope, name);
2838 break;
2839 case NLE_CXX98:
2840 error_at (location, "%<%E::%E%> is not a class or namespace",
2841 parser->scope, name);
2842 break;
2843 case NLE_NOT_CXX98:
2844 error_at (location,
2845 "%<%E::%E%> is not a class, namespace, or enumeration",
2846 parser->scope, name);
2847 break;
2848 default:
2849 gcc_unreachable ();
2853 else if (parser->scope == global_namespace)
2855 switch (desired)
2857 case NLE_TYPE:
2858 error_at (location, "%<::%E%> is not a type", name);
2859 break;
2860 case NLE_CXX98:
2861 error_at (location, "%<::%E%> is not a class or namespace", name);
2862 break;
2863 case NLE_NOT_CXX98:
2864 error_at (location,
2865 "%<::%E%> is not a class, namespace, or enumeration",
2866 name);
2867 break;
2868 default:
2869 gcc_unreachable ();
2872 else
2874 switch (desired)
2876 case NLE_TYPE:
2877 error_at (location, "%qE is not a type", name);
2878 break;
2879 case NLE_CXX98:
2880 error_at (location, "%qE is not a class or namespace", name);
2881 break;
2882 case NLE_NOT_CXX98:
2883 error_at (location,
2884 "%qE is not a class, namespace, or enumeration", name);
2885 break;
2886 default:
2887 gcc_unreachable ();
2892 /* If we are parsing tentatively, remember that an error has occurred
2893 during this tentative parse. Returns true if the error was
2894 simulated; false if a message should be issued by the caller. */
2896 static bool
2897 cp_parser_simulate_error (cp_parser* parser)
2899 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2901 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2902 return true;
2904 return false;
2907 /* This function is called when a type is defined. If type
2908 definitions are forbidden at this point, an error message is
2909 issued. */
2911 static bool
2912 cp_parser_check_type_definition (cp_parser* parser)
2914 /* If types are forbidden here, issue a message. */
2915 if (parser->type_definition_forbidden_message)
2917 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2918 in the message need to be interpreted. */
2919 error (parser->type_definition_forbidden_message);
2920 return false;
2922 return true;
2925 /* This function is called when the DECLARATOR is processed. The TYPE
2926 was a type defined in the decl-specifiers. If it is invalid to
2927 define a type in the decl-specifiers for DECLARATOR, an error is
2928 issued. TYPE_LOCATION is the location of TYPE and is used
2929 for error reporting. */
2931 static void
2932 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2933 tree type, location_t type_location)
2935 /* [dcl.fct] forbids type definitions in return types.
2936 Unfortunately, it's not easy to know whether or not we are
2937 processing a return type until after the fact. */
2938 while (declarator
2939 && (declarator->kind == cdk_pointer
2940 || declarator->kind == cdk_reference
2941 || declarator->kind == cdk_ptrmem))
2942 declarator = declarator->declarator;
2943 if (declarator
2944 && declarator->kind == cdk_function)
2946 error_at (type_location,
2947 "new types may not be defined in a return type");
2948 inform (type_location,
2949 "(perhaps a semicolon is missing after the definition of %qT)",
2950 type);
2954 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2955 "<" in any valid C++ program. If the next token is indeed "<",
2956 issue a message warning the user about what appears to be an
2957 invalid attempt to form a template-id. LOCATION is the location
2958 of the type-specifier (TYPE) */
2960 static void
2961 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2962 tree type,
2963 enum tag_types tag_type,
2964 location_t location)
2966 cp_token_position start = 0;
2968 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2970 if (TYPE_P (type))
2971 error_at (location, "%qT is not a template", type);
2972 else if (identifier_p (type))
2974 if (tag_type != none_type)
2975 error_at (location, "%qE is not a class template", type);
2976 else
2977 error_at (location, "%qE is not a template", type);
2979 else
2980 error_at (location, "invalid template-id");
2981 /* Remember the location of the invalid "<". */
2982 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2983 start = cp_lexer_token_position (parser->lexer, true);
2984 /* Consume the "<". */
2985 cp_lexer_consume_token (parser->lexer);
2986 /* Parse the template arguments. */
2987 cp_parser_enclosed_template_argument_list (parser);
2988 /* Permanently remove the invalid template arguments so that
2989 this error message is not issued again. */
2990 if (start)
2991 cp_lexer_purge_tokens_after (parser->lexer, start);
2995 /* If parsing an integral constant-expression, issue an error message
2996 about the fact that THING appeared and return true. Otherwise,
2997 return false. In either case, set
2998 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3000 static bool
3001 cp_parser_non_integral_constant_expression (cp_parser *parser,
3002 non_integral_constant thing)
3004 parser->non_integral_constant_expression_p = true;
3005 if (parser->integral_constant_expression_p)
3007 if (!parser->allow_non_integral_constant_expression_p)
3009 const char *msg = NULL;
3010 switch (thing)
3012 case NIC_FLOAT:
3013 error ("floating-point literal "
3014 "cannot appear in a constant-expression");
3015 return true;
3016 case NIC_CAST:
3017 error ("a cast to a type other than an integral or "
3018 "enumeration type cannot appear in a "
3019 "constant-expression");
3020 return true;
3021 case NIC_TYPEID:
3022 error ("%<typeid%> operator "
3023 "cannot appear in a constant-expression");
3024 return true;
3025 case NIC_NCC:
3026 error ("non-constant compound literals "
3027 "cannot appear in a constant-expression");
3028 return true;
3029 case NIC_FUNC_CALL:
3030 error ("a function call "
3031 "cannot appear in a constant-expression");
3032 return true;
3033 case NIC_INC:
3034 error ("an increment "
3035 "cannot appear in a constant-expression");
3036 return true;
3037 case NIC_DEC:
3038 error ("an decrement "
3039 "cannot appear in a constant-expression");
3040 return true;
3041 case NIC_ARRAY_REF:
3042 error ("an array reference "
3043 "cannot appear in a constant-expression");
3044 return true;
3045 case NIC_ADDR_LABEL:
3046 error ("the address of a label "
3047 "cannot appear in a constant-expression");
3048 return true;
3049 case NIC_OVERLOADED:
3050 error ("calls to overloaded operators "
3051 "cannot appear in a constant-expression");
3052 return true;
3053 case NIC_ASSIGNMENT:
3054 error ("an assignment cannot appear in a constant-expression");
3055 return true;
3056 case NIC_COMMA:
3057 error ("a comma operator "
3058 "cannot appear in a constant-expression");
3059 return true;
3060 case NIC_CONSTRUCTOR:
3061 error ("a call to a constructor "
3062 "cannot appear in a constant-expression");
3063 return true;
3064 case NIC_TRANSACTION:
3065 error ("a transaction expression "
3066 "cannot appear in a constant-expression");
3067 return true;
3068 case NIC_THIS:
3069 msg = "this";
3070 break;
3071 case NIC_FUNC_NAME:
3072 msg = "__FUNCTION__";
3073 break;
3074 case NIC_PRETTY_FUNC:
3075 msg = "__PRETTY_FUNCTION__";
3076 break;
3077 case NIC_C99_FUNC:
3078 msg = "__func__";
3079 break;
3080 case NIC_VA_ARG:
3081 msg = "va_arg";
3082 break;
3083 case NIC_ARROW:
3084 msg = "->";
3085 break;
3086 case NIC_POINT:
3087 msg = ".";
3088 break;
3089 case NIC_STAR:
3090 msg = "*";
3091 break;
3092 case NIC_ADDR:
3093 msg = "&";
3094 break;
3095 case NIC_PREINCREMENT:
3096 msg = "++";
3097 break;
3098 case NIC_PREDECREMENT:
3099 msg = "--";
3100 break;
3101 case NIC_NEW:
3102 msg = "new";
3103 break;
3104 case NIC_DEL:
3105 msg = "delete";
3106 break;
3107 default:
3108 gcc_unreachable ();
3110 if (msg)
3111 error ("%qs cannot appear in a constant-expression", msg);
3112 return true;
3115 return false;
3118 /* Emit a diagnostic for an invalid type name. This function commits
3119 to the current active tentative parse, if any. (Otherwise, the
3120 problematic construct might be encountered again later, resulting
3121 in duplicate error messages.) LOCATION is the location of ID. */
3123 static void
3124 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3125 location_t location)
3127 tree decl, ambiguous_decls;
3128 cp_parser_commit_to_tentative_parse (parser);
3129 /* Try to lookup the identifier. */
3130 decl = cp_parser_lookup_name (parser, id, none_type,
3131 /*is_template=*/false,
3132 /*is_namespace=*/false,
3133 /*check_dependency=*/true,
3134 &ambiguous_decls, location);
3135 if (ambiguous_decls)
3136 /* If the lookup was ambiguous, an error will already have
3137 been issued. */
3138 return;
3139 /* If the lookup found a template-name, it means that the user forgot
3140 to specify an argument list. Emit a useful error message. */
3141 if (DECL_TYPE_TEMPLATE_P (decl))
3143 error_at (location,
3144 "invalid use of template-name %qE without an argument list",
3145 decl);
3146 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3148 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3149 error_at (location, "invalid use of destructor %qD as a type", id);
3150 else if (TREE_CODE (decl) == TYPE_DECL)
3151 /* Something like 'unsigned A a;' */
3152 error_at (location, "invalid combination of multiple type-specifiers");
3153 else if (!parser->scope)
3155 /* Issue an error message. */
3156 error_at (location, "%qE does not name a type", id);
3157 /* If we're in a template class, it's possible that the user was
3158 referring to a type from a base class. For example:
3160 template <typename T> struct A { typedef T X; };
3161 template <typename T> struct B : public A<T> { X x; };
3163 The user should have said "typename A<T>::X". */
3164 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3165 inform (location, "C++11 %<constexpr%> only available with "
3166 "-std=c++11 or -std=gnu++11");
3167 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3168 inform (location, "C++11 %<noexcept%> only available with "
3169 "-std=c++11 or -std=gnu++11");
3170 else if (cxx_dialect < cxx11
3171 && TREE_CODE (id) == IDENTIFIER_NODE
3172 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
3173 inform (location, "C++11 %<thread_local%> only available with "
3174 "-std=c++11 or -std=gnu++11");
3175 else if (processing_template_decl && current_class_type
3176 && TYPE_BINFO (current_class_type))
3178 tree b;
3180 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3182 b = TREE_CHAIN (b))
3184 tree base_type = BINFO_TYPE (b);
3185 if (CLASS_TYPE_P (base_type)
3186 && dependent_type_p (base_type))
3188 tree field;
3189 /* Go from a particular instantiation of the
3190 template (which will have an empty TYPE_FIELDs),
3191 to the main version. */
3192 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3193 for (field = TYPE_FIELDS (base_type);
3194 field;
3195 field = DECL_CHAIN (field))
3196 if (TREE_CODE (field) == TYPE_DECL
3197 && DECL_NAME (field) == id)
3199 inform (location,
3200 "(perhaps %<typename %T::%E%> was intended)",
3201 BINFO_TYPE (b), id);
3202 break;
3204 if (field)
3205 break;
3210 /* Here we diagnose qualified-ids where the scope is actually correct,
3211 but the identifier does not resolve to a valid type name. */
3212 else if (parser->scope != error_mark_node)
3214 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3216 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3217 error_at (location_of (id),
3218 "%qE in namespace %qE does not name a template type",
3219 id, parser->scope);
3220 else
3221 error_at (location_of (id),
3222 "%qE in namespace %qE does not name a type",
3223 id, parser->scope);
3224 if (DECL_P (decl))
3225 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3227 else if (CLASS_TYPE_P (parser->scope)
3228 && constructor_name_p (id, parser->scope))
3230 /* A<T>::A<T>() */
3231 error_at (location, "%<%T::%E%> names the constructor, not"
3232 " the type", parser->scope, id);
3233 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3234 error_at (location, "and %qT has no template constructors",
3235 parser->scope);
3237 else if (TYPE_P (parser->scope)
3238 && dependent_scope_p (parser->scope))
3239 error_at (location, "need %<typename%> before %<%T::%E%> because "
3240 "%qT is a dependent scope",
3241 parser->scope, id, parser->scope);
3242 else if (TYPE_P (parser->scope))
3244 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3245 error_at (location_of (id),
3246 "%qE in %q#T does not name a template type",
3247 id, parser->scope);
3248 else
3249 error_at (location_of (id),
3250 "%qE in %q#T does not name a type",
3251 id, parser->scope);
3252 if (DECL_P (decl))
3253 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3255 else
3256 gcc_unreachable ();
3260 /* Check for a common situation where a type-name should be present,
3261 but is not, and issue a sensible error message. Returns true if an
3262 invalid type-name was detected.
3264 The situation handled by this function are variable declarations of the
3265 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3266 Usually, `ID' should name a type, but if we got here it means that it
3267 does not. We try to emit the best possible error message depending on
3268 how exactly the id-expression looks like. */
3270 static bool
3271 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3273 tree id;
3274 cp_token *token = cp_lexer_peek_token (parser->lexer);
3276 /* Avoid duplicate error about ambiguous lookup. */
3277 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3279 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3280 if (next->type == CPP_NAME && next->error_reported)
3281 goto out;
3284 cp_parser_parse_tentatively (parser);
3285 id = cp_parser_id_expression (parser,
3286 /*template_keyword_p=*/false,
3287 /*check_dependency_p=*/true,
3288 /*template_p=*/NULL,
3289 /*declarator_p=*/true,
3290 /*optional_p=*/false);
3291 /* If the next token is a (, this is a function with no explicit return
3292 type, i.e. constructor, destructor or conversion op. */
3293 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3294 || TREE_CODE (id) == TYPE_DECL)
3296 cp_parser_abort_tentative_parse (parser);
3297 return false;
3299 if (!cp_parser_parse_definitely (parser))
3300 return false;
3302 /* Emit a diagnostic for the invalid type. */
3303 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3304 out:
3305 /* If we aren't in the middle of a declarator (i.e. in a
3306 parameter-declaration-clause), skip to the end of the declaration;
3307 there's no point in trying to process it. */
3308 if (!parser->in_declarator_p)
3309 cp_parser_skip_to_end_of_block_or_statement (parser);
3310 return true;
3313 /* Consume tokens up to, and including, the next non-nested closing `)'.
3314 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3315 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3316 found an unnested token of that type. */
3318 static int
3319 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3320 bool recovering,
3321 cpp_ttype or_ttype,
3322 bool consume_paren)
3324 unsigned paren_depth = 0;
3325 unsigned brace_depth = 0;
3326 unsigned square_depth = 0;
3328 if (recovering && or_ttype == CPP_EOF
3329 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3330 return 0;
3332 while (true)
3334 cp_token * token = cp_lexer_peek_token (parser->lexer);
3336 /* Have we found what we're looking for before the closing paren? */
3337 if (token->type == or_ttype && or_ttype != CPP_EOF
3338 && !brace_depth && !paren_depth && !square_depth)
3339 return -1;
3341 switch (token->type)
3343 case CPP_EOF:
3344 case CPP_PRAGMA_EOL:
3345 /* If we've run out of tokens, then there is no closing `)'. */
3346 return 0;
3348 /* This is good for lambda expression capture-lists. */
3349 case CPP_OPEN_SQUARE:
3350 ++square_depth;
3351 break;
3352 case CPP_CLOSE_SQUARE:
3353 if (!square_depth--)
3354 return 0;
3355 break;
3357 case CPP_SEMICOLON:
3358 /* This matches the processing in skip_to_end_of_statement. */
3359 if (!brace_depth)
3360 return 0;
3361 break;
3363 case CPP_OPEN_BRACE:
3364 ++brace_depth;
3365 break;
3366 case CPP_CLOSE_BRACE:
3367 if (!brace_depth--)
3368 return 0;
3369 break;
3371 case CPP_OPEN_PAREN:
3372 if (!brace_depth)
3373 ++paren_depth;
3374 break;
3376 case CPP_CLOSE_PAREN:
3377 if (!brace_depth && !paren_depth--)
3379 if (consume_paren)
3380 cp_lexer_consume_token (parser->lexer);
3381 return 1;
3383 break;
3385 default:
3386 break;
3389 /* Consume the token. */
3390 cp_lexer_consume_token (parser->lexer);
3394 /* Consume tokens up to, and including, the next non-nested closing `)'.
3395 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3396 are doing error recovery. Returns -1 if OR_COMMA is true and we
3397 found an unnested token of that type. */
3399 static int
3400 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3401 bool recovering,
3402 bool or_comma,
3403 bool consume_paren)
3405 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3406 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3407 ttype, consume_paren);
3410 /* Consume tokens until we reach the end of the current statement.
3411 Normally, that will be just before consuming a `;'. However, if a
3412 non-nested `}' comes first, then we stop before consuming that. */
3414 static void
3415 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3417 unsigned nesting_depth = 0;
3419 /* Unwind generic function template scope if necessary. */
3420 if (parser->fully_implicit_function_template_p)
3421 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3423 while (true)
3425 cp_token *token = cp_lexer_peek_token (parser->lexer);
3427 switch (token->type)
3429 case CPP_EOF:
3430 case CPP_PRAGMA_EOL:
3431 /* If we've run out of tokens, stop. */
3432 return;
3434 case CPP_SEMICOLON:
3435 /* If the next token is a `;', we have reached the end of the
3436 statement. */
3437 if (!nesting_depth)
3438 return;
3439 break;
3441 case CPP_CLOSE_BRACE:
3442 /* If this is a non-nested '}', stop before consuming it.
3443 That way, when confronted with something like:
3445 { 3 + }
3447 we stop before consuming the closing '}', even though we
3448 have not yet reached a `;'. */
3449 if (nesting_depth == 0)
3450 return;
3452 /* If it is the closing '}' for a block that we have
3453 scanned, stop -- but only after consuming the token.
3454 That way given:
3456 void f g () { ... }
3457 typedef int I;
3459 we will stop after the body of the erroneously declared
3460 function, but before consuming the following `typedef'
3461 declaration. */
3462 if (--nesting_depth == 0)
3464 cp_lexer_consume_token (parser->lexer);
3465 return;
3468 case CPP_OPEN_BRACE:
3469 ++nesting_depth;
3470 break;
3472 default:
3473 break;
3476 /* Consume the token. */
3477 cp_lexer_consume_token (parser->lexer);
3481 /* This function is called at the end of a statement or declaration.
3482 If the next token is a semicolon, it is consumed; otherwise, error
3483 recovery is attempted. */
3485 static void
3486 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3488 /* Look for the trailing `;'. */
3489 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3491 /* If there is additional (erroneous) input, skip to the end of
3492 the statement. */
3493 cp_parser_skip_to_end_of_statement (parser);
3494 /* If the next token is now a `;', consume it. */
3495 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3496 cp_lexer_consume_token (parser->lexer);
3500 /* Skip tokens until we have consumed an entire block, or until we
3501 have consumed a non-nested `;'. */
3503 static void
3504 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3506 int nesting_depth = 0;
3508 /* Unwind generic function template scope if necessary. */
3509 if (parser->fully_implicit_function_template_p)
3510 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3512 while (nesting_depth >= 0)
3514 cp_token *token = cp_lexer_peek_token (parser->lexer);
3516 switch (token->type)
3518 case CPP_EOF:
3519 case CPP_PRAGMA_EOL:
3520 /* If we've run out of tokens, stop. */
3521 return;
3523 case CPP_SEMICOLON:
3524 /* Stop if this is an unnested ';'. */
3525 if (!nesting_depth)
3526 nesting_depth = -1;
3527 break;
3529 case CPP_CLOSE_BRACE:
3530 /* Stop if this is an unnested '}', or closes the outermost
3531 nesting level. */
3532 nesting_depth--;
3533 if (nesting_depth < 0)
3534 return;
3535 if (!nesting_depth)
3536 nesting_depth = -1;
3537 break;
3539 case CPP_OPEN_BRACE:
3540 /* Nest. */
3541 nesting_depth++;
3542 break;
3544 default:
3545 break;
3548 /* Consume the token. */
3549 cp_lexer_consume_token (parser->lexer);
3553 /* Skip tokens until a non-nested closing curly brace is the next
3554 token, or there are no more tokens. Return true in the first case,
3555 false otherwise. */
3557 static bool
3558 cp_parser_skip_to_closing_brace (cp_parser *parser)
3560 unsigned nesting_depth = 0;
3562 while (true)
3564 cp_token *token = cp_lexer_peek_token (parser->lexer);
3566 switch (token->type)
3568 case CPP_EOF:
3569 case CPP_PRAGMA_EOL:
3570 /* If we've run out of tokens, stop. */
3571 return false;
3573 case CPP_CLOSE_BRACE:
3574 /* If the next token is a non-nested `}', then we have reached
3575 the end of the current block. */
3576 if (nesting_depth-- == 0)
3577 return true;
3578 break;
3580 case CPP_OPEN_BRACE:
3581 /* If it the next token is a `{', then we are entering a new
3582 block. Consume the entire block. */
3583 ++nesting_depth;
3584 break;
3586 default:
3587 break;
3590 /* Consume the token. */
3591 cp_lexer_consume_token (parser->lexer);
3595 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3596 parameter is the PRAGMA token, allowing us to purge the entire pragma
3597 sequence. */
3599 static void
3600 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3602 cp_token *token;
3604 parser->lexer->in_pragma = false;
3607 token = cp_lexer_consume_token (parser->lexer);
3608 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3610 /* Ensure that the pragma is not parsed again. */
3611 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3614 /* Require pragma end of line, resyncing with it as necessary. The
3615 arguments are as for cp_parser_skip_to_pragma_eol. */
3617 static void
3618 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3620 parser->lexer->in_pragma = false;
3621 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3622 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3625 /* This is a simple wrapper around make_typename_type. When the id is
3626 an unresolved identifier node, we can provide a superior diagnostic
3627 using cp_parser_diagnose_invalid_type_name. */
3629 static tree
3630 cp_parser_make_typename_type (cp_parser *parser, tree id,
3631 location_t id_location)
3633 tree result;
3634 if (identifier_p (id))
3636 result = make_typename_type (parser->scope, id, typename_type,
3637 /*complain=*/tf_none);
3638 if (result == error_mark_node)
3639 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3640 return result;
3642 return make_typename_type (parser->scope, id, typename_type, tf_error);
3645 /* This is a wrapper around the
3646 make_{pointer,ptrmem,reference}_declarator functions that decides
3647 which one to call based on the CODE and CLASS_TYPE arguments. The
3648 CODE argument should be one of the values returned by
3649 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3650 appertain to the pointer or reference. */
3652 static cp_declarator *
3653 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3654 cp_cv_quals cv_qualifiers,
3655 cp_declarator *target,
3656 tree attributes)
3658 if (code == ERROR_MARK)
3659 return cp_error_declarator;
3661 if (code == INDIRECT_REF)
3662 if (class_type == NULL_TREE)
3663 return make_pointer_declarator (cv_qualifiers, target, attributes);
3664 else
3665 return make_ptrmem_declarator (cv_qualifiers, class_type,
3666 target, attributes);
3667 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3668 return make_reference_declarator (cv_qualifiers, target,
3669 false, attributes);
3670 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3671 return make_reference_declarator (cv_qualifiers, target,
3672 true, attributes);
3673 gcc_unreachable ();
3676 /* Create a new C++ parser. */
3678 static cp_parser *
3679 cp_parser_new (void)
3681 cp_parser *parser;
3682 cp_lexer *lexer;
3683 unsigned i;
3685 /* cp_lexer_new_main is called before doing GC allocation because
3686 cp_lexer_new_main might load a PCH file. */
3687 lexer = cp_lexer_new_main ();
3689 /* Initialize the binops_by_token so that we can get the tree
3690 directly from the token. */
3691 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3692 binops_by_token[binops[i].token_type] = binops[i];
3694 parser = ggc_cleared_alloc<cp_parser> ();
3695 parser->lexer = lexer;
3696 parser->context = cp_parser_context_new (NULL);
3698 /* For now, we always accept GNU extensions. */
3699 parser->allow_gnu_extensions_p = 1;
3701 /* The `>' token is a greater-than operator, not the end of a
3702 template-id. */
3703 parser->greater_than_is_operator_p = true;
3705 parser->default_arg_ok_p = true;
3707 /* We are not parsing a constant-expression. */
3708 parser->integral_constant_expression_p = false;
3709 parser->allow_non_integral_constant_expression_p = false;
3710 parser->non_integral_constant_expression_p = false;
3712 /* Local variable names are not forbidden. */
3713 parser->local_variables_forbidden_p = false;
3715 /* We are not processing an `extern "C"' declaration. */
3716 parser->in_unbraced_linkage_specification_p = false;
3718 /* We are not processing a declarator. */
3719 parser->in_declarator_p = false;
3721 /* We are not processing a template-argument-list. */
3722 parser->in_template_argument_list_p = false;
3724 /* We are not in an iteration statement. */
3725 parser->in_statement = 0;
3727 /* We are not in a switch statement. */
3728 parser->in_switch_statement_p = false;
3730 /* We are not parsing a type-id inside an expression. */
3731 parser->in_type_id_in_expr_p = false;
3733 /* Declarations aren't implicitly extern "C". */
3734 parser->implicit_extern_c = false;
3736 /* String literals should be translated to the execution character set. */
3737 parser->translate_strings_p = true;
3739 /* We are not parsing a function body. */
3740 parser->in_function_body = false;
3742 /* We can correct until told otherwise. */
3743 parser->colon_corrects_to_scope_p = true;
3745 /* The unparsed function queue is empty. */
3746 push_unparsed_function_queues (parser);
3748 /* There are no classes being defined. */
3749 parser->num_classes_being_defined = 0;
3751 /* No template parameters apply. */
3752 parser->num_template_parameter_lists = 0;
3754 /* Not declaring an implicit function template. */
3755 parser->auto_is_implicit_function_template_parm_p = false;
3756 parser->fully_implicit_function_template_p = false;
3757 parser->implicit_template_parms = 0;
3758 parser->implicit_template_scope = 0;
3760 /* Active OpenACC routine clauses. */
3761 parser->oacc_routine = NULL;
3763 /* Allow constrained-type-specifiers. */
3764 parser->prevent_constrained_type_specifiers = 0;
3766 return parser;
3769 /* Create a cp_lexer structure which will emit the tokens in CACHE
3770 and push it onto the parser's lexer stack. This is used for delayed
3771 parsing of in-class method bodies and default arguments, and should
3772 not be confused with tentative parsing. */
3773 static void
3774 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3776 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3777 lexer->next = parser->lexer;
3778 parser->lexer = lexer;
3780 /* Move the current source position to that of the first token in the
3781 new lexer. */
3782 cp_lexer_set_source_position_from_token (lexer->next_token);
3785 /* Pop the top lexer off the parser stack. This is never used for the
3786 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3787 static void
3788 cp_parser_pop_lexer (cp_parser *parser)
3790 cp_lexer *lexer = parser->lexer;
3791 parser->lexer = lexer->next;
3792 cp_lexer_destroy (lexer);
3794 /* Put the current source position back where it was before this
3795 lexer was pushed. */
3796 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3799 /* Lexical conventions [gram.lex] */
3801 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3802 identifier. */
3804 static cp_expr
3805 cp_parser_identifier (cp_parser* parser)
3807 cp_token *token;
3809 /* Look for the identifier. */
3810 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3811 /* Return the value. */
3812 if (token)
3813 return cp_expr (token->u.value, token->location);
3814 else
3815 return error_mark_node;
3818 /* Parse a sequence of adjacent string constants. Returns a
3819 TREE_STRING representing the combined, nul-terminated string
3820 constant. If TRANSLATE is true, translate the string to the
3821 execution character set. If WIDE_OK is true, a wide string is
3822 invalid here.
3824 C++98 [lex.string] says that if a narrow string literal token is
3825 adjacent to a wide string literal token, the behavior is undefined.
3826 However, C99 6.4.5p4 says that this results in a wide string literal.
3827 We follow C99 here, for consistency with the C front end.
3829 This code is largely lifted from lex_string() in c-lex.c.
3831 FUTURE: ObjC++ will need to handle @-strings here. */
3832 static cp_expr
3833 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3834 bool lookup_udlit = true)
3836 tree value;
3837 size_t count;
3838 struct obstack str_ob;
3839 cpp_string str, istr, *strs;
3840 cp_token *tok;
3841 enum cpp_ttype type, curr_type;
3842 int have_suffix_p = 0;
3843 tree string_tree;
3844 tree suffix_id = NULL_TREE;
3845 bool curr_tok_is_userdef_p = false;
3847 tok = cp_lexer_peek_token (parser->lexer);
3848 if (!cp_parser_is_string_literal (tok))
3850 cp_parser_error (parser, "expected string-literal");
3851 return error_mark_node;
3854 location_t loc = tok->location;
3856 if (cpp_userdef_string_p (tok->type))
3858 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3859 curr_type = cpp_userdef_string_remove_type (tok->type);
3860 curr_tok_is_userdef_p = true;
3862 else
3864 string_tree = tok->u.value;
3865 curr_type = tok->type;
3867 type = curr_type;
3869 /* Try to avoid the overhead of creating and destroying an obstack
3870 for the common case of just one string. */
3871 if (!cp_parser_is_string_literal
3872 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3874 cp_lexer_consume_token (parser->lexer);
3876 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3877 str.len = TREE_STRING_LENGTH (string_tree);
3878 count = 1;
3880 if (curr_tok_is_userdef_p)
3882 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3883 have_suffix_p = 1;
3884 curr_type = cpp_userdef_string_remove_type (tok->type);
3886 else
3887 curr_type = tok->type;
3889 strs = &str;
3891 else
3893 location_t last_tok_loc;
3894 gcc_obstack_init (&str_ob);
3895 count = 0;
3899 last_tok_loc = tok->location;
3900 cp_lexer_consume_token (parser->lexer);
3901 count++;
3902 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3903 str.len = TREE_STRING_LENGTH (string_tree);
3905 if (curr_tok_is_userdef_p)
3907 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3908 if (have_suffix_p == 0)
3910 suffix_id = curr_suffix_id;
3911 have_suffix_p = 1;
3913 else if (have_suffix_p == 1
3914 && curr_suffix_id != suffix_id)
3916 error ("inconsistent user-defined literal suffixes"
3917 " %qD and %qD in string literal",
3918 suffix_id, curr_suffix_id);
3919 have_suffix_p = -1;
3921 curr_type = cpp_userdef_string_remove_type (tok->type);
3923 else
3924 curr_type = tok->type;
3926 if (type != curr_type)
3928 if (type == CPP_STRING)
3929 type = curr_type;
3930 else if (curr_type != CPP_STRING)
3931 error_at (tok->location,
3932 "unsupported non-standard concatenation "
3933 "of string literals");
3936 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3938 tok = cp_lexer_peek_token (parser->lexer);
3939 if (cpp_userdef_string_p (tok->type))
3941 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3942 curr_type = cpp_userdef_string_remove_type (tok->type);
3943 curr_tok_is_userdef_p = true;
3945 else
3947 string_tree = tok->u.value;
3948 curr_type = tok->type;
3949 curr_tok_is_userdef_p = false;
3952 while (cp_parser_is_string_literal (tok));
3954 /* A string literal built by concatenation has its caret=start at
3955 the start of the initial string, and its finish at the finish of
3956 the final string literal. */
3957 loc = make_location (loc, loc, get_finish (last_tok_loc));
3959 strs = (cpp_string *) obstack_finish (&str_ob);
3962 if (type != CPP_STRING && !wide_ok)
3964 cp_parser_error (parser, "a wide string is invalid in this context");
3965 type = CPP_STRING;
3968 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3969 (parse_in, strs, count, &istr, type))
3971 value = build_string (istr.len, (const char *)istr.text);
3972 free (CONST_CAST (unsigned char *, istr.text));
3974 switch (type)
3976 default:
3977 case CPP_STRING:
3978 case CPP_UTF8STRING:
3979 TREE_TYPE (value) = char_array_type_node;
3980 break;
3981 case CPP_STRING16:
3982 TREE_TYPE (value) = char16_array_type_node;
3983 break;
3984 case CPP_STRING32:
3985 TREE_TYPE (value) = char32_array_type_node;
3986 break;
3987 case CPP_WSTRING:
3988 TREE_TYPE (value) = wchar_array_type_node;
3989 break;
3992 value = fix_string_type (value);
3994 if (have_suffix_p)
3996 tree literal = build_userdef_literal (suffix_id, value,
3997 OT_NONE, NULL_TREE);
3998 if (lookup_udlit)
3999 value = cp_parser_userdef_string_literal (literal);
4000 else
4001 value = literal;
4004 else
4005 /* cpp_interpret_string has issued an error. */
4006 value = error_mark_node;
4008 if (count > 1)
4009 obstack_free (&str_ob, 0);
4011 return cp_expr (value, loc);
4014 /* Look up a literal operator with the name and the exact arguments. */
4016 static tree
4017 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4019 tree decl, fns;
4020 decl = lookup_name (name);
4021 if (!decl || !is_overloaded_fn (decl))
4022 return error_mark_node;
4024 for (fns = decl; fns; fns = OVL_NEXT (fns))
4026 unsigned int ix;
4027 bool found = true;
4028 tree fn = OVL_CURRENT (fns);
4029 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4030 if (parmtypes != NULL_TREE)
4032 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4033 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4035 tree tparm = TREE_VALUE (parmtypes);
4036 tree targ = TREE_TYPE ((*args)[ix]);
4037 bool ptr = TYPE_PTR_P (tparm);
4038 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4039 if ((ptr || arr || !same_type_p (tparm, targ))
4040 && (!ptr || !arr
4041 || !same_type_p (TREE_TYPE (tparm),
4042 TREE_TYPE (targ))))
4043 found = false;
4045 if (found
4046 && ix == vec_safe_length (args)
4047 /* May be this should be sufficient_parms_p instead,
4048 depending on how exactly should user-defined literals
4049 work in presence of default arguments on the literal
4050 operator parameters. */
4051 && parmtypes == void_list_node)
4052 return decl;
4056 return error_mark_node;
4059 /* Parse a user-defined char constant. Returns a call to a user-defined
4060 literal operator taking the character as an argument. */
4062 static cp_expr
4063 cp_parser_userdef_char_literal (cp_parser *parser)
4065 cp_token *token = cp_lexer_consume_token (parser->lexer);
4066 tree literal = token->u.value;
4067 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4068 tree value = USERDEF_LITERAL_VALUE (literal);
4069 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4070 tree decl, result;
4072 /* Build up a call to the user-defined operator */
4073 /* Lookup the name we got back from the id-expression. */
4074 vec<tree, va_gc> *args = make_tree_vector ();
4075 vec_safe_push (args, value);
4076 decl = lookup_literal_operator (name, args);
4077 if (!decl || decl == error_mark_node)
4079 error ("unable to find character literal operator %qD with %qT argument",
4080 name, TREE_TYPE (value));
4081 release_tree_vector (args);
4082 return error_mark_node;
4084 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4085 release_tree_vector (args);
4086 return result;
4089 /* A subroutine of cp_parser_userdef_numeric_literal to
4090 create a char... template parameter pack from a string node. */
4092 static tree
4093 make_char_string_pack (tree value)
4095 tree charvec;
4096 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4097 const char *str = TREE_STRING_POINTER (value);
4098 int i, len = TREE_STRING_LENGTH (value) - 1;
4099 tree argvec = make_tree_vec (1);
4101 /* Fill in CHARVEC with all of the parameters. */
4102 charvec = make_tree_vec (len);
4103 for (i = 0; i < len; ++i)
4104 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4106 /* Build the argument packs. */
4107 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4108 TREE_TYPE (argpack) = char_type_node;
4110 TREE_VEC_ELT (argvec, 0) = argpack;
4112 return argvec;
4115 /* A subroutine of cp_parser_userdef_numeric_literal to
4116 create a char... template parameter pack from a string node. */
4118 static tree
4119 make_string_pack (tree value)
4121 tree charvec;
4122 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4123 const unsigned char *str
4124 = (const unsigned char *) TREE_STRING_POINTER (value);
4125 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4126 int len = TREE_STRING_LENGTH (value) / sz - 1;
4127 tree argvec = make_tree_vec (2);
4129 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4130 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4132 /* First template parm is character type. */
4133 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4135 /* Fill in CHARVEC with all of the parameters. */
4136 charvec = make_tree_vec (len);
4137 for (int i = 0; i < len; ++i)
4138 TREE_VEC_ELT (charvec, i)
4139 = double_int_to_tree (str_char_type_node,
4140 double_int::from_buffer (str + i * sz, sz));
4142 /* Build the argument packs. */
4143 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4144 TREE_TYPE (argpack) = str_char_type_node;
4146 TREE_VEC_ELT (argvec, 1) = argpack;
4148 return argvec;
4151 /* Parse a user-defined numeric constant. returns a call to a user-defined
4152 literal operator. */
4154 static cp_expr
4155 cp_parser_userdef_numeric_literal (cp_parser *parser)
4157 cp_token *token = cp_lexer_consume_token (parser->lexer);
4158 tree literal = token->u.value;
4159 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4160 tree value = USERDEF_LITERAL_VALUE (literal);
4161 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4162 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4163 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4164 tree decl, result;
4165 vec<tree, va_gc> *args;
4167 /* Look for a literal operator taking the exact type of numeric argument
4168 as the literal value. */
4169 args = make_tree_vector ();
4170 vec_safe_push (args, value);
4171 decl = lookup_literal_operator (name, args);
4172 if (decl && decl != error_mark_node)
4174 result = finish_call_expr (decl, &args, false, true,
4175 tf_warning_or_error);
4177 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4179 warning_at (token->location, OPT_Woverflow,
4180 "integer literal exceeds range of %qT type",
4181 long_long_unsigned_type_node);
4183 else
4185 if (overflow > 0)
4186 warning_at (token->location, OPT_Woverflow,
4187 "floating literal exceeds range of %qT type",
4188 long_double_type_node);
4189 else if (overflow < 0)
4190 warning_at (token->location, OPT_Woverflow,
4191 "floating literal truncated to zero");
4194 release_tree_vector (args);
4195 return result;
4197 release_tree_vector (args);
4199 /* If the numeric argument didn't work, look for a raw literal
4200 operator taking a const char* argument consisting of the number
4201 in string format. */
4202 args = make_tree_vector ();
4203 vec_safe_push (args, num_string);
4204 decl = lookup_literal_operator (name, args);
4205 if (decl && decl != error_mark_node)
4207 result = finish_call_expr (decl, &args, false, true,
4208 tf_warning_or_error);
4209 release_tree_vector (args);
4210 return result;
4212 release_tree_vector (args);
4214 /* If the raw literal didn't work, look for a non-type template
4215 function with parameter pack char.... Call the function with
4216 template parameter characters representing the number. */
4217 args = make_tree_vector ();
4218 decl = lookup_literal_operator (name, args);
4219 if (decl && decl != error_mark_node)
4221 tree tmpl_args = make_char_string_pack (num_string);
4222 decl = lookup_template_function (decl, tmpl_args);
4223 result = finish_call_expr (decl, &args, false, true,
4224 tf_warning_or_error);
4225 release_tree_vector (args);
4226 return result;
4229 release_tree_vector (args);
4231 error ("unable to find numeric literal operator %qD", name);
4232 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4233 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4234 "to enable more built-in suffixes");
4235 return error_mark_node;
4238 /* Parse a user-defined string constant. Returns a call to a user-defined
4239 literal operator taking a character pointer and the length of the string
4240 as arguments. */
4242 static tree
4243 cp_parser_userdef_string_literal (tree literal)
4245 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4246 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4247 tree value = USERDEF_LITERAL_VALUE (literal);
4248 int len = TREE_STRING_LENGTH (value)
4249 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4250 tree decl, result;
4251 vec<tree, va_gc> *args;
4253 /* Build up a call to the user-defined operator. */
4254 /* Lookup the name we got back from the id-expression. */
4255 args = make_tree_vector ();
4256 vec_safe_push (args, value);
4257 vec_safe_push (args, build_int_cst (size_type_node, len));
4258 decl = lookup_literal_operator (name, args);
4260 if (decl && decl != error_mark_node)
4262 result = finish_call_expr (decl, &args, false, true,
4263 tf_warning_or_error);
4264 release_tree_vector (args);
4265 return result;
4267 release_tree_vector (args);
4269 /* Look for a template function with typename parameter CharT
4270 and parameter pack CharT... Call the function with
4271 template parameter characters representing the string. */
4272 args = make_tree_vector ();
4273 decl = lookup_literal_operator (name, args);
4274 if (decl && decl != error_mark_node)
4276 tree tmpl_args = make_string_pack (value);
4277 decl = lookup_template_function (decl, tmpl_args);
4278 result = finish_call_expr (decl, &args, false, true,
4279 tf_warning_or_error);
4280 release_tree_vector (args);
4281 return result;
4283 release_tree_vector (args);
4285 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4286 name, TREE_TYPE (value), size_type_node);
4287 return error_mark_node;
4291 /* Basic concepts [gram.basic] */
4293 /* Parse a translation-unit.
4295 translation-unit:
4296 declaration-seq [opt]
4298 Returns TRUE if all went well. */
4300 static bool
4301 cp_parser_translation_unit (cp_parser* parser)
4303 /* The address of the first non-permanent object on the declarator
4304 obstack. */
4305 static void *declarator_obstack_base;
4307 bool success;
4309 /* Create the declarator obstack, if necessary. */
4310 if (!cp_error_declarator)
4312 gcc_obstack_init (&declarator_obstack);
4313 /* Create the error declarator. */
4314 cp_error_declarator = make_declarator (cdk_error);
4315 /* Create the empty parameter list. */
4316 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4317 /* Remember where the base of the declarator obstack lies. */
4318 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4321 cp_parser_declaration_seq_opt (parser);
4323 /* If there are no tokens left then all went well. */
4324 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4326 /* Get rid of the token array; we don't need it any more. */
4327 cp_lexer_destroy (parser->lexer);
4328 parser->lexer = NULL;
4330 /* This file might have been a context that's implicitly extern
4331 "C". If so, pop the lang context. (Only relevant for PCH.) */
4332 if (parser->implicit_extern_c)
4334 pop_lang_context ();
4335 parser->implicit_extern_c = false;
4338 /* Finish up. */
4339 finish_translation_unit ();
4341 success = true;
4343 else
4345 cp_parser_error (parser, "expected declaration");
4346 success = false;
4349 /* Make sure the declarator obstack was fully cleaned up. */
4350 gcc_assert (obstack_next_free (&declarator_obstack)
4351 == declarator_obstack_base);
4353 /* All went well. */
4354 return success;
4357 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4358 decltype context. */
4360 static inline tsubst_flags_t
4361 complain_flags (bool decltype_p)
4363 tsubst_flags_t complain = tf_warning_or_error;
4364 if (decltype_p)
4365 complain |= tf_decltype;
4366 return complain;
4369 /* We're about to parse a collection of statements. If we're currently
4370 parsing tentatively, set up a firewall so that any nested
4371 cp_parser_commit_to_tentative_parse won't affect the current context. */
4373 static cp_token_position
4374 cp_parser_start_tentative_firewall (cp_parser *parser)
4376 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4377 return 0;
4379 cp_parser_parse_tentatively (parser);
4380 cp_parser_commit_to_topmost_tentative_parse (parser);
4381 return cp_lexer_token_position (parser->lexer, false);
4384 /* We've finished parsing the collection of statements. Wrap up the
4385 firewall and replace the relevant tokens with the parsed form. */
4387 static void
4388 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4389 tree expr)
4391 if (!start)
4392 return;
4394 /* Finish the firewall level. */
4395 cp_parser_parse_definitely (parser);
4396 /* And remember the result of the parse for when we try again. */
4397 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4398 token->type = CPP_PREPARSED_EXPR;
4399 token->u.value = expr;
4400 token->keyword = RID_MAX;
4401 cp_lexer_purge_tokens_after (parser->lexer, start);
4404 /* Like the above functions, but let the user modify the tokens. Used by
4405 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4406 later parses, so it makes sense to localize the effects of
4407 cp_parser_commit_to_tentative_parse. */
4409 struct tentative_firewall
4411 cp_parser *parser;
4412 bool set;
4414 tentative_firewall (cp_parser *p): parser(p)
4416 /* If we're currently parsing tentatively, start a committed level as a
4417 firewall and then an inner tentative parse. */
4418 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4420 cp_parser_parse_tentatively (parser);
4421 cp_parser_commit_to_topmost_tentative_parse (parser);
4422 cp_parser_parse_tentatively (parser);
4426 ~tentative_firewall()
4428 if (set)
4430 /* Finish the inner tentative parse and the firewall, propagating any
4431 uncommitted error state to the outer tentative parse. */
4432 bool err = cp_parser_error_occurred (parser);
4433 cp_parser_parse_definitely (parser);
4434 cp_parser_parse_definitely (parser);
4435 if (err)
4436 cp_parser_simulate_error (parser);
4441 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4442 enclosing parentheses. */
4444 static cp_expr
4445 cp_parser_statement_expr (cp_parser *parser)
4447 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4449 /* Consume the '('. */
4450 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4451 cp_lexer_consume_token (parser->lexer);
4452 /* Start the statement-expression. */
4453 tree expr = begin_stmt_expr ();
4454 /* Parse the compound-statement. */
4455 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4456 /* Finish up. */
4457 expr = finish_stmt_expr (expr, false);
4458 /* Consume the ')'. */
4459 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4460 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4461 cp_parser_skip_to_end_of_statement (parser);
4463 cp_parser_end_tentative_firewall (parser, start, expr);
4464 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4465 return cp_expr (expr, combined_loc);
4468 /* Expressions [gram.expr] */
4470 /* Parse a fold-operator.
4472 fold-operator:
4473 - * / % ^ & | = < > << >>
4474 = -= *= /= %= ^= &= |= <<= >>=
4475 == != <= >= && || , .* ->*
4477 This returns the tree code corresponding to the matched operator
4478 as an int. When the current token matches a compound assignment
4479 opertor, the resulting tree code is the negative value of the
4480 non-assignment operator. */
4482 static int
4483 cp_parser_fold_operator (cp_token *token)
4485 switch (token->type)
4487 case CPP_PLUS: return PLUS_EXPR;
4488 case CPP_MINUS: return MINUS_EXPR;
4489 case CPP_MULT: return MULT_EXPR;
4490 case CPP_DIV: return TRUNC_DIV_EXPR;
4491 case CPP_MOD: return TRUNC_MOD_EXPR;
4492 case CPP_XOR: return BIT_XOR_EXPR;
4493 case CPP_AND: return BIT_AND_EXPR;
4494 case CPP_OR: return BIT_IOR_EXPR;
4495 case CPP_LSHIFT: return LSHIFT_EXPR;
4496 case CPP_RSHIFT: return RSHIFT_EXPR;
4498 case CPP_EQ: return -NOP_EXPR;
4499 case CPP_PLUS_EQ: return -PLUS_EXPR;
4500 case CPP_MINUS_EQ: return -MINUS_EXPR;
4501 case CPP_MULT_EQ: return -MULT_EXPR;
4502 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4503 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4504 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4505 case CPP_AND_EQ: return -BIT_AND_EXPR;
4506 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4507 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4508 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4510 case CPP_EQ_EQ: return EQ_EXPR;
4511 case CPP_NOT_EQ: return NE_EXPR;
4512 case CPP_LESS: return LT_EXPR;
4513 case CPP_GREATER: return GT_EXPR;
4514 case CPP_LESS_EQ: return LE_EXPR;
4515 case CPP_GREATER_EQ: return GE_EXPR;
4517 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4518 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4520 case CPP_COMMA: return COMPOUND_EXPR;
4522 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4523 case CPP_DEREF_STAR: return MEMBER_REF;
4525 default: return ERROR_MARK;
4529 /* Returns true if CODE indicates a binary expression, which is not allowed in
4530 the LHS of a fold-expression. More codes will need to be added to use this
4531 function in other contexts. */
4533 static bool
4534 is_binary_op (tree_code code)
4536 switch (code)
4538 case PLUS_EXPR:
4539 case POINTER_PLUS_EXPR:
4540 case MINUS_EXPR:
4541 case MULT_EXPR:
4542 case TRUNC_DIV_EXPR:
4543 case TRUNC_MOD_EXPR:
4544 case BIT_XOR_EXPR:
4545 case BIT_AND_EXPR:
4546 case BIT_IOR_EXPR:
4547 case LSHIFT_EXPR:
4548 case RSHIFT_EXPR:
4550 case MODOP_EXPR:
4552 case EQ_EXPR:
4553 case NE_EXPR:
4554 case LE_EXPR:
4555 case GE_EXPR:
4556 case LT_EXPR:
4557 case GT_EXPR:
4559 case TRUTH_ANDIF_EXPR:
4560 case TRUTH_ORIF_EXPR:
4562 case COMPOUND_EXPR:
4564 case DOTSTAR_EXPR:
4565 case MEMBER_REF:
4566 return true;
4568 default:
4569 return false;
4573 /* If the next token is a suitable fold operator, consume it and return as
4574 the function above. */
4576 static int
4577 cp_parser_fold_operator (cp_parser *parser)
4579 cp_token* token = cp_lexer_peek_token (parser->lexer);
4580 int code = cp_parser_fold_operator (token);
4581 if (code != ERROR_MARK)
4582 cp_lexer_consume_token (parser->lexer);
4583 return code;
4586 /* Parse a fold-expression.
4588 fold-expression:
4589 ( ... folding-operator cast-expression)
4590 ( cast-expression folding-operator ... )
4591 ( cast-expression folding operator ... folding-operator cast-expression)
4593 Note that the '(' and ')' are matched in primary expression. */
4595 static cp_expr
4596 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4598 cp_id_kind pidk;
4600 // Left fold.
4601 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4603 cp_lexer_consume_token (parser->lexer);
4604 int op = cp_parser_fold_operator (parser);
4605 if (op == ERROR_MARK)
4607 cp_parser_error (parser, "expected binary operator");
4608 return error_mark_node;
4611 tree expr = cp_parser_cast_expression (parser, false, false,
4612 false, &pidk);
4613 if (expr == error_mark_node)
4614 return error_mark_node;
4615 return finish_left_unary_fold_expr (expr, op);
4618 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4619 int op = cp_parser_fold_operator (parser);
4620 if (op == ERROR_MARK)
4622 cp_parser_error (parser, "expected binary operator");
4623 return error_mark_node;
4626 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4628 cp_parser_error (parser, "expected ...");
4629 return error_mark_node;
4631 cp_lexer_consume_token (parser->lexer);
4633 /* The operands of a fold-expression are cast-expressions, so binary or
4634 conditional expressions are not allowed. We check this here to avoid
4635 tentative parsing. */
4636 if (is_binary_op (TREE_CODE (expr1)))
4637 error_at (location_of (expr1),
4638 "binary expression in operand of fold-expression");
4639 else if (TREE_CODE (expr1) == COND_EXPR)
4640 error_at (location_of (expr1),
4641 "conditional expression in operand of fold-expression");
4643 // Right fold.
4644 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4645 return finish_right_unary_fold_expr (expr1, op);
4647 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4649 cp_parser_error (parser, "mismatched operator in fold-expression");
4650 return error_mark_node;
4652 cp_lexer_consume_token (parser->lexer);
4654 // Binary left or right fold.
4655 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4656 if (expr2 == error_mark_node)
4657 return error_mark_node;
4658 return finish_binary_fold_expr (expr1, expr2, op);
4661 /* Parse a primary-expression.
4663 primary-expression:
4664 literal
4665 this
4666 ( expression )
4667 id-expression
4668 lambda-expression (C++11)
4670 GNU Extensions:
4672 primary-expression:
4673 ( compound-statement )
4674 __builtin_va_arg ( assignment-expression , type-id )
4675 __builtin_offsetof ( type-id , offsetof-expression )
4677 C++ Extensions:
4678 __has_nothrow_assign ( type-id )
4679 __has_nothrow_constructor ( type-id )
4680 __has_nothrow_copy ( type-id )
4681 __has_trivial_assign ( type-id )
4682 __has_trivial_constructor ( type-id )
4683 __has_trivial_copy ( type-id )
4684 __has_trivial_destructor ( type-id )
4685 __has_virtual_destructor ( type-id )
4686 __is_abstract ( type-id )
4687 __is_base_of ( type-id , type-id )
4688 __is_class ( type-id )
4689 __is_empty ( type-id )
4690 __is_enum ( type-id )
4691 __is_final ( type-id )
4692 __is_literal_type ( type-id )
4693 __is_pod ( type-id )
4694 __is_polymorphic ( type-id )
4695 __is_std_layout ( type-id )
4696 __is_trivial ( type-id )
4697 __is_union ( type-id )
4699 Objective-C++ Extension:
4701 primary-expression:
4702 objc-expression
4704 literal:
4705 __null
4707 ADDRESS_P is true iff this expression was immediately preceded by
4708 "&" and therefore might denote a pointer-to-member. CAST_P is true
4709 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4710 true iff this expression is a template argument.
4712 Returns a representation of the expression. Upon return, *IDK
4713 indicates what kind of id-expression (if any) was present. */
4715 static cp_expr
4716 cp_parser_primary_expression (cp_parser *parser,
4717 bool address_p,
4718 bool cast_p,
4719 bool template_arg_p,
4720 bool decltype_p,
4721 cp_id_kind *idk)
4723 cp_token *token = NULL;
4725 /* Assume the primary expression is not an id-expression. */
4726 *idk = CP_ID_KIND_NONE;
4728 /* Peek at the next token. */
4729 token = cp_lexer_peek_token (parser->lexer);
4730 switch ((int) token->type)
4732 /* literal:
4733 integer-literal
4734 character-literal
4735 floating-literal
4736 string-literal
4737 boolean-literal
4738 pointer-literal
4739 user-defined-literal */
4740 case CPP_CHAR:
4741 case CPP_CHAR16:
4742 case CPP_CHAR32:
4743 case CPP_WCHAR:
4744 case CPP_UTF8CHAR:
4745 case CPP_NUMBER:
4746 case CPP_PREPARSED_EXPR:
4747 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4748 return cp_parser_userdef_numeric_literal (parser);
4749 token = cp_lexer_consume_token (parser->lexer);
4750 if (TREE_CODE (token->u.value) == FIXED_CST)
4752 error_at (token->location,
4753 "fixed-point types not supported in C++");
4754 return error_mark_node;
4756 /* Floating-point literals are only allowed in an integral
4757 constant expression if they are cast to an integral or
4758 enumeration type. */
4759 if (TREE_CODE (token->u.value) == REAL_CST
4760 && parser->integral_constant_expression_p
4761 && pedantic)
4763 /* CAST_P will be set even in invalid code like "int(2.7 +
4764 ...)". Therefore, we have to check that the next token
4765 is sure to end the cast. */
4766 if (cast_p)
4768 cp_token *next_token;
4770 next_token = cp_lexer_peek_token (parser->lexer);
4771 if (/* The comma at the end of an
4772 enumerator-definition. */
4773 next_token->type != CPP_COMMA
4774 /* The curly brace at the end of an enum-specifier. */
4775 && next_token->type != CPP_CLOSE_BRACE
4776 /* The end of a statement. */
4777 && next_token->type != CPP_SEMICOLON
4778 /* The end of the cast-expression. */
4779 && next_token->type != CPP_CLOSE_PAREN
4780 /* The end of an array bound. */
4781 && next_token->type != CPP_CLOSE_SQUARE
4782 /* The closing ">" in a template-argument-list. */
4783 && (next_token->type != CPP_GREATER
4784 || parser->greater_than_is_operator_p)
4785 /* C++0x only: A ">>" treated like two ">" tokens,
4786 in a template-argument-list. */
4787 && (next_token->type != CPP_RSHIFT
4788 || (cxx_dialect == cxx98)
4789 || parser->greater_than_is_operator_p))
4790 cast_p = false;
4793 /* If we are within a cast, then the constraint that the
4794 cast is to an integral or enumeration type will be
4795 checked at that point. If we are not within a cast, then
4796 this code is invalid. */
4797 if (!cast_p)
4798 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4800 return cp_expr (token->u.value, token->location);
4802 case CPP_CHAR_USERDEF:
4803 case CPP_CHAR16_USERDEF:
4804 case CPP_CHAR32_USERDEF:
4805 case CPP_WCHAR_USERDEF:
4806 case CPP_UTF8CHAR_USERDEF:
4807 return cp_parser_userdef_char_literal (parser);
4809 case CPP_STRING:
4810 case CPP_STRING16:
4811 case CPP_STRING32:
4812 case CPP_WSTRING:
4813 case CPP_UTF8STRING:
4814 case CPP_STRING_USERDEF:
4815 case CPP_STRING16_USERDEF:
4816 case CPP_STRING32_USERDEF:
4817 case CPP_WSTRING_USERDEF:
4818 case CPP_UTF8STRING_USERDEF:
4819 /* ??? Should wide strings be allowed when parser->translate_strings_p
4820 is false (i.e. in attributes)? If not, we can kill the third
4821 argument to cp_parser_string_literal. */
4822 return cp_parser_string_literal (parser,
4823 parser->translate_strings_p,
4824 true);
4826 case CPP_OPEN_PAREN:
4827 /* If we see `( { ' then we are looking at the beginning of
4828 a GNU statement-expression. */
4829 if (cp_parser_allow_gnu_extensions_p (parser)
4830 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4832 /* Statement-expressions are not allowed by the standard. */
4833 pedwarn (token->location, OPT_Wpedantic,
4834 "ISO C++ forbids braced-groups within expressions");
4836 /* And they're not allowed outside of a function-body; you
4837 cannot, for example, write:
4839 int i = ({ int j = 3; j + 1; });
4841 at class or namespace scope. */
4842 if (!parser->in_function_body
4843 || parser->in_template_argument_list_p)
4845 error_at (token->location,
4846 "statement-expressions are not allowed outside "
4847 "functions nor in template-argument lists");
4848 cp_parser_skip_to_end_of_block_or_statement (parser);
4849 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4850 cp_lexer_consume_token (parser->lexer);
4851 return error_mark_node;
4853 else
4854 return cp_parser_statement_expr (parser);
4856 /* Otherwise it's a normal parenthesized expression. */
4858 cp_expr expr;
4859 bool saved_greater_than_is_operator_p;
4861 location_t open_paren_loc = token->location;
4863 /* Consume the `('. */
4864 cp_lexer_consume_token (parser->lexer);
4865 /* Within a parenthesized expression, a `>' token is always
4866 the greater-than operator. */
4867 saved_greater_than_is_operator_p
4868 = parser->greater_than_is_operator_p;
4869 parser->greater_than_is_operator_p = true;
4871 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4872 /* Left fold expression. */
4873 expr = NULL_TREE;
4874 else
4875 /* Parse the parenthesized expression. */
4876 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4878 token = cp_lexer_peek_token (parser->lexer);
4879 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
4881 expr = cp_parser_fold_expression (parser, expr);
4882 if (expr != error_mark_node
4883 && cxx_dialect < cxx1z
4884 && !in_system_header_at (input_location))
4885 pedwarn (input_location, 0, "fold-expressions only available "
4886 "with -std=c++1z or -std=gnu++1z");
4888 else
4889 /* Let the front end know that this expression was
4890 enclosed in parentheses. This matters in case, for
4891 example, the expression is of the form `A::B', since
4892 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4893 not. */
4894 expr = finish_parenthesized_expr (expr);
4896 /* DR 705: Wrapping an unqualified name in parentheses
4897 suppresses arg-dependent lookup. We want to pass back
4898 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4899 (c++/37862), but none of the others. */
4900 if (*idk != CP_ID_KIND_QUALIFIED)
4901 *idk = CP_ID_KIND_NONE;
4903 /* The `>' token might be the end of a template-id or
4904 template-parameter-list now. */
4905 parser->greater_than_is_operator_p
4906 = saved_greater_than_is_operator_p;
4908 /* Consume the `)'. */
4909 token = cp_lexer_peek_token (parser->lexer);
4910 location_t close_paren_loc = token->location;
4911 expr.set_range (open_paren_loc, close_paren_loc);
4912 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)
4913 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4914 cp_parser_skip_to_end_of_statement (parser);
4916 return expr;
4919 case CPP_OPEN_SQUARE:
4921 if (c_dialect_objc ())
4923 /* We might have an Objective-C++ message. */
4924 cp_parser_parse_tentatively (parser);
4925 tree msg = cp_parser_objc_message_expression (parser);
4926 /* If that works out, we're done ... */
4927 if (cp_parser_parse_definitely (parser))
4928 return msg;
4929 /* ... else, fall though to see if it's a lambda. */
4931 cp_expr lam = cp_parser_lambda_expression (parser);
4932 /* Don't warn about a failed tentative parse. */
4933 if (cp_parser_error_occurred (parser))
4934 return error_mark_node;
4935 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4936 return lam;
4939 case CPP_OBJC_STRING:
4940 if (c_dialect_objc ())
4941 /* We have an Objective-C++ string literal. */
4942 return cp_parser_objc_expression (parser);
4943 cp_parser_error (parser, "expected primary-expression");
4944 return error_mark_node;
4946 case CPP_KEYWORD:
4947 switch (token->keyword)
4949 /* These two are the boolean literals. */
4950 case RID_TRUE:
4951 cp_lexer_consume_token (parser->lexer);
4952 return cp_expr (boolean_true_node, token->location);
4953 case RID_FALSE:
4954 cp_lexer_consume_token (parser->lexer);
4955 return cp_expr (boolean_false_node, token->location);
4957 /* The `__null' literal. */
4958 case RID_NULL:
4959 cp_lexer_consume_token (parser->lexer);
4960 return cp_expr (null_node, token->location);
4962 /* The `nullptr' literal. */
4963 case RID_NULLPTR:
4964 cp_lexer_consume_token (parser->lexer);
4965 return cp_expr (nullptr_node, token->location);
4967 /* Recognize the `this' keyword. */
4968 case RID_THIS:
4969 cp_lexer_consume_token (parser->lexer);
4970 if (parser->local_variables_forbidden_p)
4972 error_at (token->location,
4973 "%<this%> may not be used in this context");
4974 return error_mark_node;
4976 /* Pointers cannot appear in constant-expressions. */
4977 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4978 return error_mark_node;
4979 return cp_expr (finish_this_expr (), token->location);
4981 /* The `operator' keyword can be the beginning of an
4982 id-expression. */
4983 case RID_OPERATOR:
4984 goto id_expression;
4986 case RID_FUNCTION_NAME:
4987 case RID_PRETTY_FUNCTION_NAME:
4988 case RID_C99_FUNCTION_NAME:
4990 non_integral_constant name;
4992 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4993 __func__ are the names of variables -- but they are
4994 treated specially. Therefore, they are handled here,
4995 rather than relying on the generic id-expression logic
4996 below. Grammatically, these names are id-expressions.
4998 Consume the token. */
4999 token = cp_lexer_consume_token (parser->lexer);
5001 switch (token->keyword)
5003 case RID_FUNCTION_NAME:
5004 name = NIC_FUNC_NAME;
5005 break;
5006 case RID_PRETTY_FUNCTION_NAME:
5007 name = NIC_PRETTY_FUNC;
5008 break;
5009 case RID_C99_FUNCTION_NAME:
5010 name = NIC_C99_FUNC;
5011 break;
5012 default:
5013 gcc_unreachable ();
5016 if (cp_parser_non_integral_constant_expression (parser, name))
5017 return error_mark_node;
5019 /* Look up the name. */
5020 return finish_fname (token->u.value);
5023 case RID_VA_ARG:
5025 tree expression;
5026 tree type;
5027 source_location type_location;
5028 location_t start_loc
5029 = cp_lexer_peek_token (parser->lexer)->location;
5030 /* The `__builtin_va_arg' construct is used to handle
5031 `va_arg'. Consume the `__builtin_va_arg' token. */
5032 cp_lexer_consume_token (parser->lexer);
5033 /* Look for the opening `('. */
5034 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5035 /* Now, parse the assignment-expression. */
5036 expression = cp_parser_assignment_expression (parser);
5037 /* Look for the `,'. */
5038 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5039 type_location = cp_lexer_peek_token (parser->lexer)->location;
5040 /* Parse the type-id. */
5042 type_id_in_expr_sentinel s (parser);
5043 type = cp_parser_type_id (parser);
5045 /* Look for the closing `)'. */
5046 location_t finish_loc
5047 = cp_lexer_peek_token (parser->lexer)->location;
5048 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5049 /* Using `va_arg' in a constant-expression is not
5050 allowed. */
5051 if (cp_parser_non_integral_constant_expression (parser,
5052 NIC_VA_ARG))
5053 return error_mark_node;
5054 /* Construct a location of the form:
5055 __builtin_va_arg (v, int)
5056 ~~~~~~~~~~~~~~~~~~~~~^~~~
5057 with the caret at the type, ranging from the start of the
5058 "__builtin_va_arg" token to the close paren. */
5059 location_t combined_loc
5060 = make_location (type_location, start_loc, finish_loc);
5061 return build_x_va_arg (combined_loc, expression, type);
5064 case RID_OFFSETOF:
5065 return cp_parser_builtin_offsetof (parser);
5067 case RID_HAS_NOTHROW_ASSIGN:
5068 case RID_HAS_NOTHROW_CONSTRUCTOR:
5069 case RID_HAS_NOTHROW_COPY:
5070 case RID_HAS_TRIVIAL_ASSIGN:
5071 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5072 case RID_HAS_TRIVIAL_COPY:
5073 case RID_HAS_TRIVIAL_DESTRUCTOR:
5074 case RID_HAS_VIRTUAL_DESTRUCTOR:
5075 case RID_IS_ABSTRACT:
5076 case RID_IS_BASE_OF:
5077 case RID_IS_CLASS:
5078 case RID_IS_EMPTY:
5079 case RID_IS_ENUM:
5080 case RID_IS_FINAL:
5081 case RID_IS_LITERAL_TYPE:
5082 case RID_IS_POD:
5083 case RID_IS_POLYMORPHIC:
5084 case RID_IS_SAME_AS:
5085 case RID_IS_STD_LAYOUT:
5086 case RID_IS_TRIVIAL:
5087 case RID_IS_TRIVIALLY_ASSIGNABLE:
5088 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5089 case RID_IS_TRIVIALLY_COPYABLE:
5090 case RID_IS_UNION:
5091 return cp_parser_trait_expr (parser, token->keyword);
5093 // C++ concepts
5094 case RID_REQUIRES:
5095 return cp_parser_requires_expression (parser);
5097 /* Objective-C++ expressions. */
5098 case RID_AT_ENCODE:
5099 case RID_AT_PROTOCOL:
5100 case RID_AT_SELECTOR:
5101 return cp_parser_objc_expression (parser);
5103 case RID_TEMPLATE:
5104 if (parser->in_function_body
5105 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5106 == CPP_LESS))
5108 error_at (token->location,
5109 "a template declaration cannot appear at block scope");
5110 cp_parser_skip_to_end_of_block_or_statement (parser);
5111 return error_mark_node;
5113 default:
5114 cp_parser_error (parser, "expected primary-expression");
5115 return error_mark_node;
5118 /* An id-expression can start with either an identifier, a
5119 `::' as the beginning of a qualified-id, or the "operator"
5120 keyword. */
5121 case CPP_NAME:
5122 case CPP_SCOPE:
5123 case CPP_TEMPLATE_ID:
5124 case CPP_NESTED_NAME_SPECIFIER:
5126 id_expression:
5127 cp_expr id_expression;
5128 cp_expr decl;
5129 const char *error_msg;
5130 bool template_p;
5131 bool done;
5132 cp_token *id_expr_token;
5134 /* Parse the id-expression. */
5135 id_expression
5136 = cp_parser_id_expression (parser,
5137 /*template_keyword_p=*/false,
5138 /*check_dependency_p=*/true,
5139 &template_p,
5140 /*declarator_p=*/false,
5141 /*optional_p=*/false);
5142 if (id_expression == error_mark_node)
5143 return error_mark_node;
5144 id_expr_token = token;
5145 token = cp_lexer_peek_token (parser->lexer);
5146 done = (token->type != CPP_OPEN_SQUARE
5147 && token->type != CPP_OPEN_PAREN
5148 && token->type != CPP_DOT
5149 && token->type != CPP_DEREF
5150 && token->type != CPP_PLUS_PLUS
5151 && token->type != CPP_MINUS_MINUS);
5152 /* If we have a template-id, then no further lookup is
5153 required. If the template-id was for a template-class, we
5154 will sometimes have a TYPE_DECL at this point. */
5155 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5156 || TREE_CODE (id_expression) == TYPE_DECL)
5157 decl = id_expression;
5158 /* Look up the name. */
5159 else
5161 tree ambiguous_decls;
5163 /* If we already know that this lookup is ambiguous, then
5164 we've already issued an error message; there's no reason
5165 to check again. */
5166 if (id_expr_token->type == CPP_NAME
5167 && id_expr_token->error_reported)
5169 cp_parser_simulate_error (parser);
5170 return error_mark_node;
5173 decl = cp_parser_lookup_name (parser, id_expression,
5174 none_type,
5175 template_p,
5176 /*is_namespace=*/false,
5177 /*check_dependency=*/true,
5178 &ambiguous_decls,
5179 id_expr_token->location);
5180 /* If the lookup was ambiguous, an error will already have
5181 been issued. */
5182 if (ambiguous_decls)
5183 return error_mark_node;
5185 /* In Objective-C++, we may have an Objective-C 2.0
5186 dot-syntax for classes here. */
5187 if (c_dialect_objc ()
5188 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5189 && TREE_CODE (decl) == TYPE_DECL
5190 && objc_is_class_name (decl))
5192 tree component;
5193 cp_lexer_consume_token (parser->lexer);
5194 component = cp_parser_identifier (parser);
5195 if (component == error_mark_node)
5196 return error_mark_node;
5198 tree result = objc_build_class_component_ref (id_expression,
5199 component);
5200 /* Build a location of the form:
5201 expr.component
5202 ~~~~~^~~~~~~~~
5203 with caret at the start of the component name (at
5204 input_location), ranging from the start of the id_expression
5205 to the end of the component name. */
5206 location_t combined_loc
5207 = make_location (input_location, id_expression.get_start (),
5208 get_finish (input_location));
5209 protected_set_expr_location (result, combined_loc);
5210 return result;
5213 /* In Objective-C++, an instance variable (ivar) may be preferred
5214 to whatever cp_parser_lookup_name() found.
5215 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5216 rest of c-family, we have to do a little extra work to preserve
5217 any location information in cp_expr "decl". Given that
5218 objc_lookup_ivar is implemented in "c-family" and "objc", we
5219 have a trip through the pure "tree" type, rather than cp_expr.
5220 Naively copying it back to "decl" would implicitly give the
5221 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5222 store an EXPR_LOCATION. Hence we only update "decl" (and
5223 hence its location_t) if we get back a different tree node. */
5224 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5225 id_expression);
5226 if (decl_tree != decl.get_value ())
5227 decl = cp_expr (decl_tree);
5229 /* If name lookup gives us a SCOPE_REF, then the
5230 qualifying scope was dependent. */
5231 if (TREE_CODE (decl) == SCOPE_REF)
5233 /* At this point, we do not know if DECL is a valid
5234 integral constant expression. We assume that it is
5235 in fact such an expression, so that code like:
5237 template <int N> struct A {
5238 int a[B<N>::i];
5241 is accepted. At template-instantiation time, we
5242 will check that B<N>::i is actually a constant. */
5243 return decl;
5245 /* Check to see if DECL is a local variable in a context
5246 where that is forbidden. */
5247 if (parser->local_variables_forbidden_p
5248 && local_variable_p (decl))
5250 /* It might be that we only found DECL because we are
5251 trying to be generous with pre-ISO scoping rules.
5252 For example, consider:
5254 int i;
5255 void g() {
5256 for (int i = 0; i < 10; ++i) {}
5257 extern void f(int j = i);
5260 Here, name look up will originally find the out
5261 of scope `i'. We need to issue a warning message,
5262 but then use the global `i'. */
5263 decl = check_for_out_of_scope_variable (decl);
5264 if (local_variable_p (decl))
5266 error_at (id_expr_token->location,
5267 "local variable %qD may not appear in this context",
5268 decl.get_value ());
5269 return error_mark_node;
5274 decl = (finish_id_expression
5275 (id_expression, decl, parser->scope,
5276 idk,
5277 parser->integral_constant_expression_p,
5278 parser->allow_non_integral_constant_expression_p,
5279 &parser->non_integral_constant_expression_p,
5280 template_p, done, address_p,
5281 template_arg_p,
5282 &error_msg,
5283 id_expr_token->location));
5284 if (error_msg)
5285 cp_parser_error (parser, error_msg);
5286 decl.set_location (id_expr_token->location);
5287 return decl;
5290 /* Anything else is an error. */
5291 default:
5292 cp_parser_error (parser, "expected primary-expression");
5293 return error_mark_node;
5297 static inline cp_expr
5298 cp_parser_primary_expression (cp_parser *parser,
5299 bool address_p,
5300 bool cast_p,
5301 bool template_arg_p,
5302 cp_id_kind *idk)
5304 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5305 /*decltype*/false, idk);
5308 /* Parse an id-expression.
5310 id-expression:
5311 unqualified-id
5312 qualified-id
5314 qualified-id:
5315 :: [opt] nested-name-specifier template [opt] unqualified-id
5316 :: identifier
5317 :: operator-function-id
5318 :: template-id
5320 Return a representation of the unqualified portion of the
5321 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5322 a `::' or nested-name-specifier.
5324 Often, if the id-expression was a qualified-id, the caller will
5325 want to make a SCOPE_REF to represent the qualified-id. This
5326 function does not do this in order to avoid wastefully creating
5327 SCOPE_REFs when they are not required.
5329 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5330 `template' keyword.
5332 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5333 uninstantiated templates.
5335 If *TEMPLATE_P is non-NULL, it is set to true iff the
5336 `template' keyword is used to explicitly indicate that the entity
5337 named is a template.
5339 If DECLARATOR_P is true, the id-expression is appearing as part of
5340 a declarator, rather than as part of an expression. */
5342 static cp_expr
5343 cp_parser_id_expression (cp_parser *parser,
5344 bool template_keyword_p,
5345 bool check_dependency_p,
5346 bool *template_p,
5347 bool declarator_p,
5348 bool optional_p)
5350 bool global_scope_p;
5351 bool nested_name_specifier_p;
5353 /* Assume the `template' keyword was not used. */
5354 if (template_p)
5355 *template_p = template_keyword_p;
5357 /* Look for the optional `::' operator. */
5358 global_scope_p
5359 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
5360 != NULL_TREE);
5361 /* Look for the optional nested-name-specifier. */
5362 nested_name_specifier_p
5363 = (cp_parser_nested_name_specifier_opt (parser,
5364 /*typename_keyword_p=*/false,
5365 check_dependency_p,
5366 /*type_p=*/false,
5367 declarator_p)
5368 != NULL_TREE);
5369 /* If there is a nested-name-specifier, then we are looking at
5370 the first qualified-id production. */
5371 if (nested_name_specifier_p)
5373 tree saved_scope;
5374 tree saved_object_scope;
5375 tree saved_qualifying_scope;
5376 tree unqualified_id;
5377 bool is_template;
5379 /* See if the next token is the `template' keyword. */
5380 if (!template_p)
5381 template_p = &is_template;
5382 *template_p = cp_parser_optional_template_keyword (parser);
5383 /* Name lookup we do during the processing of the
5384 unqualified-id might obliterate SCOPE. */
5385 saved_scope = parser->scope;
5386 saved_object_scope = parser->object_scope;
5387 saved_qualifying_scope = parser->qualifying_scope;
5388 /* Process the final unqualified-id. */
5389 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5390 check_dependency_p,
5391 declarator_p,
5392 /*optional_p=*/false);
5393 /* Restore the SAVED_SCOPE for our caller. */
5394 parser->scope = saved_scope;
5395 parser->object_scope = saved_object_scope;
5396 parser->qualifying_scope = saved_qualifying_scope;
5398 return unqualified_id;
5400 /* Otherwise, if we are in global scope, then we are looking at one
5401 of the other qualified-id productions. */
5402 else if (global_scope_p)
5404 cp_token *token;
5405 tree id;
5407 /* Peek at the next token. */
5408 token = cp_lexer_peek_token (parser->lexer);
5410 /* If it's an identifier, and the next token is not a "<", then
5411 we can avoid the template-id case. This is an optimization
5412 for this common case. */
5413 if (token->type == CPP_NAME
5414 && !cp_parser_nth_token_starts_template_argument_list_p
5415 (parser, 2))
5416 return cp_parser_identifier (parser);
5418 cp_parser_parse_tentatively (parser);
5419 /* Try a template-id. */
5420 id = cp_parser_template_id (parser,
5421 /*template_keyword_p=*/false,
5422 /*check_dependency_p=*/true,
5423 none_type,
5424 declarator_p);
5425 /* If that worked, we're done. */
5426 if (cp_parser_parse_definitely (parser))
5427 return id;
5429 /* Peek at the next token. (Changes in the token buffer may
5430 have invalidated the pointer obtained above.) */
5431 token = cp_lexer_peek_token (parser->lexer);
5433 switch (token->type)
5435 case CPP_NAME:
5436 return cp_parser_identifier (parser);
5438 case CPP_KEYWORD:
5439 if (token->keyword == RID_OPERATOR)
5440 return cp_parser_operator_function_id (parser);
5441 /* Fall through. */
5443 default:
5444 cp_parser_error (parser, "expected id-expression");
5445 return error_mark_node;
5448 else
5449 return cp_parser_unqualified_id (parser, template_keyword_p,
5450 /*check_dependency_p=*/true,
5451 declarator_p,
5452 optional_p);
5455 /* Parse an unqualified-id.
5457 unqualified-id:
5458 identifier
5459 operator-function-id
5460 conversion-function-id
5461 ~ class-name
5462 template-id
5464 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5465 keyword, in a construct like `A::template ...'.
5467 Returns a representation of unqualified-id. For the `identifier'
5468 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5469 production a BIT_NOT_EXPR is returned; the operand of the
5470 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5471 other productions, see the documentation accompanying the
5472 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5473 names are looked up in uninstantiated templates. If DECLARATOR_P
5474 is true, the unqualified-id is appearing as part of a declarator,
5475 rather than as part of an expression. */
5477 static cp_expr
5478 cp_parser_unqualified_id (cp_parser* parser,
5479 bool template_keyword_p,
5480 bool check_dependency_p,
5481 bool declarator_p,
5482 bool optional_p)
5484 cp_token *token;
5486 /* Peek at the next token. */
5487 token = cp_lexer_peek_token (parser->lexer);
5489 switch ((int) token->type)
5491 case CPP_NAME:
5493 tree id;
5495 /* We don't know yet whether or not this will be a
5496 template-id. */
5497 cp_parser_parse_tentatively (parser);
5498 /* Try a template-id. */
5499 id = cp_parser_template_id (parser, template_keyword_p,
5500 check_dependency_p,
5501 none_type,
5502 declarator_p);
5503 /* If it worked, we're done. */
5504 if (cp_parser_parse_definitely (parser))
5505 return id;
5506 /* Otherwise, it's an ordinary identifier. */
5507 return cp_parser_identifier (parser);
5510 case CPP_TEMPLATE_ID:
5511 return cp_parser_template_id (parser, template_keyword_p,
5512 check_dependency_p,
5513 none_type,
5514 declarator_p);
5516 case CPP_COMPL:
5518 tree type_decl;
5519 tree qualifying_scope;
5520 tree object_scope;
5521 tree scope;
5522 bool done;
5524 /* Consume the `~' token. */
5525 cp_lexer_consume_token (parser->lexer);
5526 /* Parse the class-name. The standard, as written, seems to
5527 say that:
5529 template <typename T> struct S { ~S (); };
5530 template <typename T> S<T>::~S() {}
5532 is invalid, since `~' must be followed by a class-name, but
5533 `S<T>' is dependent, and so not known to be a class.
5534 That's not right; we need to look in uninstantiated
5535 templates. A further complication arises from:
5537 template <typename T> void f(T t) {
5538 t.T::~T();
5541 Here, it is not possible to look up `T' in the scope of `T'
5542 itself. We must look in both the current scope, and the
5543 scope of the containing complete expression.
5545 Yet another issue is:
5547 struct S {
5548 int S;
5549 ~S();
5552 S::~S() {}
5554 The standard does not seem to say that the `S' in `~S'
5555 should refer to the type `S' and not the data member
5556 `S::S'. */
5558 /* DR 244 says that we look up the name after the "~" in the
5559 same scope as we looked up the qualifying name. That idea
5560 isn't fully worked out; it's more complicated than that. */
5561 scope = parser->scope;
5562 object_scope = parser->object_scope;
5563 qualifying_scope = parser->qualifying_scope;
5565 /* Check for invalid scopes. */
5566 if (scope == error_mark_node)
5568 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5569 cp_lexer_consume_token (parser->lexer);
5570 return error_mark_node;
5572 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5574 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5575 error_at (token->location,
5576 "scope %qT before %<~%> is not a class-name",
5577 scope);
5578 cp_parser_simulate_error (parser);
5579 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5580 cp_lexer_consume_token (parser->lexer);
5581 return error_mark_node;
5583 gcc_assert (!scope || TYPE_P (scope));
5585 /* If the name is of the form "X::~X" it's OK even if X is a
5586 typedef. */
5587 token = cp_lexer_peek_token (parser->lexer);
5588 if (scope
5589 && token->type == CPP_NAME
5590 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5591 != CPP_LESS)
5592 && (token->u.value == TYPE_IDENTIFIER (scope)
5593 || (CLASS_TYPE_P (scope)
5594 && constructor_name_p (token->u.value, scope))))
5596 cp_lexer_consume_token (parser->lexer);
5597 return build_nt (BIT_NOT_EXPR, scope);
5600 /* ~auto means the destructor of whatever the object is. */
5601 if (cp_parser_is_keyword (token, RID_AUTO))
5603 if (cxx_dialect < cxx14)
5604 pedwarn (input_location, 0,
5605 "%<~auto%> only available with "
5606 "-std=c++14 or -std=gnu++14");
5607 cp_lexer_consume_token (parser->lexer);
5608 return build_nt (BIT_NOT_EXPR, make_auto ());
5611 /* If there was an explicit qualification (S::~T), first look
5612 in the scope given by the qualification (i.e., S).
5614 Note: in the calls to cp_parser_class_name below we pass
5615 typename_type so that lookup finds the injected-class-name
5616 rather than the constructor. */
5617 done = false;
5618 type_decl = NULL_TREE;
5619 if (scope)
5621 cp_parser_parse_tentatively (parser);
5622 type_decl = cp_parser_class_name (parser,
5623 /*typename_keyword_p=*/false,
5624 /*template_keyword_p=*/false,
5625 typename_type,
5626 /*check_dependency=*/false,
5627 /*class_head_p=*/false,
5628 declarator_p);
5629 if (cp_parser_parse_definitely (parser))
5630 done = true;
5632 /* In "N::S::~S", look in "N" as well. */
5633 if (!done && scope && qualifying_scope)
5635 cp_parser_parse_tentatively (parser);
5636 parser->scope = qualifying_scope;
5637 parser->object_scope = NULL_TREE;
5638 parser->qualifying_scope = NULL_TREE;
5639 type_decl
5640 = cp_parser_class_name (parser,
5641 /*typename_keyword_p=*/false,
5642 /*template_keyword_p=*/false,
5643 typename_type,
5644 /*check_dependency=*/false,
5645 /*class_head_p=*/false,
5646 declarator_p);
5647 if (cp_parser_parse_definitely (parser))
5648 done = true;
5650 /* In "p->S::~T", look in the scope given by "*p" as well. */
5651 else if (!done && object_scope)
5653 cp_parser_parse_tentatively (parser);
5654 parser->scope = object_scope;
5655 parser->object_scope = NULL_TREE;
5656 parser->qualifying_scope = NULL_TREE;
5657 type_decl
5658 = cp_parser_class_name (parser,
5659 /*typename_keyword_p=*/false,
5660 /*template_keyword_p=*/false,
5661 typename_type,
5662 /*check_dependency=*/false,
5663 /*class_head_p=*/false,
5664 declarator_p);
5665 if (cp_parser_parse_definitely (parser))
5666 done = true;
5668 /* Look in the surrounding context. */
5669 if (!done)
5671 parser->scope = NULL_TREE;
5672 parser->object_scope = NULL_TREE;
5673 parser->qualifying_scope = NULL_TREE;
5674 if (processing_template_decl)
5675 cp_parser_parse_tentatively (parser);
5676 type_decl
5677 = cp_parser_class_name (parser,
5678 /*typename_keyword_p=*/false,
5679 /*template_keyword_p=*/false,
5680 typename_type,
5681 /*check_dependency=*/false,
5682 /*class_head_p=*/false,
5683 declarator_p);
5684 if (processing_template_decl
5685 && ! cp_parser_parse_definitely (parser))
5687 /* We couldn't find a type with this name. If we're parsing
5688 tentatively, fail and try something else. */
5689 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5691 cp_parser_simulate_error (parser);
5692 return error_mark_node;
5694 /* Otherwise, accept it and check for a match at instantiation
5695 time. */
5696 type_decl = cp_parser_identifier (parser);
5697 if (type_decl != error_mark_node)
5698 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5699 return type_decl;
5702 /* If an error occurred, assume that the name of the
5703 destructor is the same as the name of the qualifying
5704 class. That allows us to keep parsing after running
5705 into ill-formed destructor names. */
5706 if (type_decl == error_mark_node && scope)
5707 return build_nt (BIT_NOT_EXPR, scope);
5708 else if (type_decl == error_mark_node)
5709 return error_mark_node;
5711 /* Check that destructor name and scope match. */
5712 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5714 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5715 error_at (token->location,
5716 "declaration of %<~%T%> as member of %qT",
5717 type_decl, scope);
5718 cp_parser_simulate_error (parser);
5719 return error_mark_node;
5722 /* [class.dtor]
5724 A typedef-name that names a class shall not be used as the
5725 identifier in the declarator for a destructor declaration. */
5726 if (declarator_p
5727 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5728 && !DECL_SELF_REFERENCE_P (type_decl)
5729 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5730 error_at (token->location,
5731 "typedef-name %qD used as destructor declarator",
5732 type_decl);
5734 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5737 case CPP_KEYWORD:
5738 if (token->keyword == RID_OPERATOR)
5740 cp_expr id;
5742 /* This could be a template-id, so we try that first. */
5743 cp_parser_parse_tentatively (parser);
5744 /* Try a template-id. */
5745 id = cp_parser_template_id (parser, template_keyword_p,
5746 /*check_dependency_p=*/true,
5747 none_type,
5748 declarator_p);
5749 /* If that worked, we're done. */
5750 if (cp_parser_parse_definitely (parser))
5751 return id;
5752 /* We still don't know whether we're looking at an
5753 operator-function-id or a conversion-function-id. */
5754 cp_parser_parse_tentatively (parser);
5755 /* Try an operator-function-id. */
5756 id = cp_parser_operator_function_id (parser);
5757 /* If that didn't work, try a conversion-function-id. */
5758 if (!cp_parser_parse_definitely (parser))
5759 id = cp_parser_conversion_function_id (parser);
5760 else if (UDLIT_OPER_P (id))
5762 /* 17.6.3.3.5 */
5763 const char *name = UDLIT_OP_SUFFIX (id);
5764 if (name[0] != '_' && !in_system_header_at (input_location)
5765 && declarator_p)
5766 warning (0, "literal operator suffixes not preceded by %<_%>"
5767 " are reserved for future standardization");
5770 return id;
5772 /* Fall through. */
5774 default:
5775 if (optional_p)
5776 return NULL_TREE;
5777 cp_parser_error (parser, "expected unqualified-id");
5778 return error_mark_node;
5782 /* Parse an (optional) nested-name-specifier.
5784 nested-name-specifier: [C++98]
5785 class-or-namespace-name :: nested-name-specifier [opt]
5786 class-or-namespace-name :: template nested-name-specifier [opt]
5788 nested-name-specifier: [C++0x]
5789 type-name ::
5790 namespace-name ::
5791 nested-name-specifier identifier ::
5792 nested-name-specifier template [opt] simple-template-id ::
5794 PARSER->SCOPE should be set appropriately before this function is
5795 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5796 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5797 in name lookups.
5799 Sets PARSER->SCOPE to the class (TYPE) or namespace
5800 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5801 it unchanged if there is no nested-name-specifier. Returns the new
5802 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5804 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5805 part of a declaration and/or decl-specifier. */
5807 static tree
5808 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5809 bool typename_keyword_p,
5810 bool check_dependency_p,
5811 bool type_p,
5812 bool is_declaration)
5814 bool success = false;
5815 cp_token_position start = 0;
5816 cp_token *token;
5818 /* Remember where the nested-name-specifier starts. */
5819 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5821 start = cp_lexer_token_position (parser->lexer, false);
5822 push_deferring_access_checks (dk_deferred);
5825 while (true)
5827 tree new_scope;
5828 tree old_scope;
5829 tree saved_qualifying_scope;
5830 bool template_keyword_p;
5832 /* Spot cases that cannot be the beginning of a
5833 nested-name-specifier. */
5834 token = cp_lexer_peek_token (parser->lexer);
5836 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5837 the already parsed nested-name-specifier. */
5838 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5840 /* Grab the nested-name-specifier and continue the loop. */
5841 cp_parser_pre_parsed_nested_name_specifier (parser);
5842 /* If we originally encountered this nested-name-specifier
5843 with IS_DECLARATION set to false, we will not have
5844 resolved TYPENAME_TYPEs, so we must do so here. */
5845 if (is_declaration
5846 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5848 new_scope = resolve_typename_type (parser->scope,
5849 /*only_current_p=*/false);
5850 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5851 parser->scope = new_scope;
5853 success = true;
5854 continue;
5857 /* Spot cases that cannot be the beginning of a
5858 nested-name-specifier. On the second and subsequent times
5859 through the loop, we look for the `template' keyword. */
5860 if (success && token->keyword == RID_TEMPLATE)
5862 /* A template-id can start a nested-name-specifier. */
5863 else if (token->type == CPP_TEMPLATE_ID)
5865 /* DR 743: decltype can be used in a nested-name-specifier. */
5866 else if (token_is_decltype (token))
5868 else
5870 /* If the next token is not an identifier, then it is
5871 definitely not a type-name or namespace-name. */
5872 if (token->type != CPP_NAME)
5873 break;
5874 /* If the following token is neither a `<' (to begin a
5875 template-id), nor a `::', then we are not looking at a
5876 nested-name-specifier. */
5877 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5879 if (token->type == CPP_COLON
5880 && parser->colon_corrects_to_scope_p
5881 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5883 error_at (token->location,
5884 "found %<:%> in nested-name-specifier, expected %<::%>");
5885 token->type = CPP_SCOPE;
5888 if (token->type != CPP_SCOPE
5889 && !cp_parser_nth_token_starts_template_argument_list_p
5890 (parser, 2))
5891 break;
5894 /* The nested-name-specifier is optional, so we parse
5895 tentatively. */
5896 cp_parser_parse_tentatively (parser);
5898 /* Look for the optional `template' keyword, if this isn't the
5899 first time through the loop. */
5900 if (success)
5901 template_keyword_p = cp_parser_optional_template_keyword (parser);
5902 else
5903 template_keyword_p = false;
5905 /* Save the old scope since the name lookup we are about to do
5906 might destroy it. */
5907 old_scope = parser->scope;
5908 saved_qualifying_scope = parser->qualifying_scope;
5909 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5910 look up names in "X<T>::I" in order to determine that "Y" is
5911 a template. So, if we have a typename at this point, we make
5912 an effort to look through it. */
5913 if (is_declaration
5914 && !typename_keyword_p
5915 && parser->scope
5916 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5917 parser->scope = resolve_typename_type (parser->scope,
5918 /*only_current_p=*/false);
5919 /* Parse the qualifying entity. */
5920 new_scope
5921 = cp_parser_qualifying_entity (parser,
5922 typename_keyword_p,
5923 template_keyword_p,
5924 check_dependency_p,
5925 type_p,
5926 is_declaration);
5927 /* Look for the `::' token. */
5928 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5930 /* If we found what we wanted, we keep going; otherwise, we're
5931 done. */
5932 if (!cp_parser_parse_definitely (parser))
5934 bool error_p = false;
5936 /* Restore the OLD_SCOPE since it was valid before the
5937 failed attempt at finding the last
5938 class-or-namespace-name. */
5939 parser->scope = old_scope;
5940 parser->qualifying_scope = saved_qualifying_scope;
5942 /* If the next token is a decltype, and the one after that is a
5943 `::', then the decltype has failed to resolve to a class or
5944 enumeration type. Give this error even when parsing
5945 tentatively since it can't possibly be valid--and we're going
5946 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5947 won't get another chance.*/
5948 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5949 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5950 == CPP_SCOPE))
5952 token = cp_lexer_consume_token (parser->lexer);
5953 error_at (token->location, "decltype evaluates to %qT, "
5954 "which is not a class or enumeration type",
5955 token->u.tree_check_value->value);
5956 parser->scope = error_mark_node;
5957 error_p = true;
5958 /* As below. */
5959 success = true;
5960 cp_lexer_consume_token (parser->lexer);
5963 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
5964 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
5966 /* If we have a non-type template-id followed by ::, it can't
5967 possibly be valid. */
5968 token = cp_lexer_peek_token (parser->lexer);
5969 tree tid = token->u.tree_check_value->value;
5970 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
5971 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
5973 tree tmpl = NULL_TREE;
5974 if (is_overloaded_fn (tid))
5976 tree fns = get_fns (tid);
5977 if (!OVL_CHAIN (fns))
5978 tmpl = OVL_CURRENT (fns);
5979 error_at (token->location, "function template-id %qD "
5980 "in nested-name-specifier", tid);
5982 else
5984 /* Variable template. */
5985 tmpl = TREE_OPERAND (tid, 0);
5986 gcc_assert (variable_template_p (tmpl));
5987 error_at (token->location, "variable template-id %qD "
5988 "in nested-name-specifier", tid);
5990 if (tmpl)
5991 inform (DECL_SOURCE_LOCATION (tmpl),
5992 "%qD declared here", tmpl);
5994 parser->scope = error_mark_node;
5995 error_p = true;
5996 /* As below. */
5997 success = true;
5998 cp_lexer_consume_token (parser->lexer);
5999 cp_lexer_consume_token (parser->lexer);
6003 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6004 break;
6005 /* If the next token is an identifier, and the one after
6006 that is a `::', then any valid interpretation would have
6007 found a class-or-namespace-name. */
6008 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6009 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6010 == CPP_SCOPE)
6011 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6012 != CPP_COMPL))
6014 token = cp_lexer_consume_token (parser->lexer);
6015 if (!error_p)
6017 if (!token->error_reported)
6019 tree decl;
6020 tree ambiguous_decls;
6022 decl = cp_parser_lookup_name (parser, token->u.value,
6023 none_type,
6024 /*is_template=*/false,
6025 /*is_namespace=*/false,
6026 /*check_dependency=*/true,
6027 &ambiguous_decls,
6028 token->location);
6029 if (TREE_CODE (decl) == TEMPLATE_DECL)
6030 error_at (token->location,
6031 "%qD used without template parameters",
6032 decl);
6033 else if (ambiguous_decls)
6035 // cp_parser_lookup_name has the same diagnostic,
6036 // thus make sure to emit it at most once.
6037 if (cp_parser_uncommitted_to_tentative_parse_p
6038 (parser))
6040 error_at (token->location,
6041 "reference to %qD is ambiguous",
6042 token->u.value);
6043 print_candidates (ambiguous_decls);
6045 decl = error_mark_node;
6047 else
6049 if (cxx_dialect != cxx98)
6050 cp_parser_name_lookup_error
6051 (parser, token->u.value, decl, NLE_NOT_CXX98,
6052 token->location);
6053 else
6054 cp_parser_name_lookup_error
6055 (parser, token->u.value, decl, NLE_CXX98,
6056 token->location);
6059 parser->scope = error_mark_node;
6060 error_p = true;
6061 /* Treat this as a successful nested-name-specifier
6062 due to:
6064 [basic.lookup.qual]
6066 If the name found is not a class-name (clause
6067 _class_) or namespace-name (_namespace.def_), the
6068 program is ill-formed. */
6069 success = true;
6071 cp_lexer_consume_token (parser->lexer);
6073 break;
6075 /* We've found one valid nested-name-specifier. */
6076 success = true;
6077 /* Name lookup always gives us a DECL. */
6078 if (TREE_CODE (new_scope) == TYPE_DECL)
6079 new_scope = TREE_TYPE (new_scope);
6080 /* Uses of "template" must be followed by actual templates. */
6081 if (template_keyword_p
6082 && !(CLASS_TYPE_P (new_scope)
6083 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6084 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6085 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6086 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6087 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6088 == TEMPLATE_ID_EXPR)))
6089 permerror (input_location, TYPE_P (new_scope)
6090 ? G_("%qT is not a template")
6091 : G_("%qD is not a template"),
6092 new_scope);
6093 /* If it is a class scope, try to complete it; we are about to
6094 be looking up names inside the class. */
6095 if (TYPE_P (new_scope)
6096 /* Since checking types for dependency can be expensive,
6097 avoid doing it if the type is already complete. */
6098 && !COMPLETE_TYPE_P (new_scope)
6099 /* Do not try to complete dependent types. */
6100 && !dependent_type_p (new_scope))
6102 new_scope = complete_type (new_scope);
6103 /* If it is a typedef to current class, use the current
6104 class instead, as the typedef won't have any names inside
6105 it yet. */
6106 if (!COMPLETE_TYPE_P (new_scope)
6107 && currently_open_class (new_scope))
6108 new_scope = TYPE_MAIN_VARIANT (new_scope);
6110 /* Make sure we look in the right scope the next time through
6111 the loop. */
6112 parser->scope = new_scope;
6115 /* If parsing tentatively, replace the sequence of tokens that makes
6116 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6117 token. That way, should we re-parse the token stream, we will
6118 not have to repeat the effort required to do the parse, nor will
6119 we issue duplicate error messages. */
6120 if (success && start)
6122 cp_token *token;
6124 token = cp_lexer_token_at (parser->lexer, start);
6125 /* Reset the contents of the START token. */
6126 token->type = CPP_NESTED_NAME_SPECIFIER;
6127 /* Retrieve any deferred checks. Do not pop this access checks yet
6128 so the memory will not be reclaimed during token replacing below. */
6129 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6130 token->u.tree_check_value->value = parser->scope;
6131 token->u.tree_check_value->checks = get_deferred_access_checks ();
6132 token->u.tree_check_value->qualifying_scope =
6133 parser->qualifying_scope;
6134 token->keyword = RID_MAX;
6136 /* Purge all subsequent tokens. */
6137 cp_lexer_purge_tokens_after (parser->lexer, start);
6140 if (start)
6141 pop_to_parent_deferring_access_checks ();
6143 return success ? parser->scope : NULL_TREE;
6146 /* Parse a nested-name-specifier. See
6147 cp_parser_nested_name_specifier_opt for details. This function
6148 behaves identically, except that it will an issue an error if no
6149 nested-name-specifier is present. */
6151 static tree
6152 cp_parser_nested_name_specifier (cp_parser *parser,
6153 bool typename_keyword_p,
6154 bool check_dependency_p,
6155 bool type_p,
6156 bool is_declaration)
6158 tree scope;
6160 /* Look for the nested-name-specifier. */
6161 scope = cp_parser_nested_name_specifier_opt (parser,
6162 typename_keyword_p,
6163 check_dependency_p,
6164 type_p,
6165 is_declaration);
6166 /* If it was not present, issue an error message. */
6167 if (!scope)
6169 cp_parser_error (parser, "expected nested-name-specifier");
6170 parser->scope = NULL_TREE;
6173 return scope;
6176 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6177 this is either a class-name or a namespace-name (which corresponds
6178 to the class-or-namespace-name production in the grammar). For
6179 C++0x, it can also be a type-name that refers to an enumeration
6180 type or a simple-template-id.
6182 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6183 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6184 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6185 TYPE_P is TRUE iff the next name should be taken as a class-name,
6186 even the same name is declared to be another entity in the same
6187 scope.
6189 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6190 specified by the class-or-namespace-name. If neither is found the
6191 ERROR_MARK_NODE is returned. */
6193 static tree
6194 cp_parser_qualifying_entity (cp_parser *parser,
6195 bool typename_keyword_p,
6196 bool template_keyword_p,
6197 bool check_dependency_p,
6198 bool type_p,
6199 bool is_declaration)
6201 tree saved_scope;
6202 tree saved_qualifying_scope;
6203 tree saved_object_scope;
6204 tree scope;
6205 bool only_class_p;
6206 bool successful_parse_p;
6208 /* DR 743: decltype can appear in a nested-name-specifier. */
6209 if (cp_lexer_next_token_is_decltype (parser->lexer))
6211 scope = cp_parser_decltype (parser);
6212 if (TREE_CODE (scope) != ENUMERAL_TYPE
6213 && !MAYBE_CLASS_TYPE_P (scope))
6215 cp_parser_simulate_error (parser);
6216 return error_mark_node;
6218 if (TYPE_NAME (scope))
6219 scope = TYPE_NAME (scope);
6220 return scope;
6223 /* Before we try to parse the class-name, we must save away the
6224 current PARSER->SCOPE since cp_parser_class_name will destroy
6225 it. */
6226 saved_scope = parser->scope;
6227 saved_qualifying_scope = parser->qualifying_scope;
6228 saved_object_scope = parser->object_scope;
6229 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6230 there is no need to look for a namespace-name. */
6231 only_class_p = template_keyword_p
6232 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6233 if (!only_class_p)
6234 cp_parser_parse_tentatively (parser);
6235 scope = cp_parser_class_name (parser,
6236 typename_keyword_p,
6237 template_keyword_p,
6238 type_p ? class_type : none_type,
6239 check_dependency_p,
6240 /*class_head_p=*/false,
6241 is_declaration,
6242 /*enum_ok=*/cxx_dialect > cxx98);
6243 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6244 /* If that didn't work, try for a namespace-name. */
6245 if (!only_class_p && !successful_parse_p)
6247 /* Restore the saved scope. */
6248 parser->scope = saved_scope;
6249 parser->qualifying_scope = saved_qualifying_scope;
6250 parser->object_scope = saved_object_scope;
6251 /* If we are not looking at an identifier followed by the scope
6252 resolution operator, then this is not part of a
6253 nested-name-specifier. (Note that this function is only used
6254 to parse the components of a nested-name-specifier.) */
6255 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6256 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6257 return error_mark_node;
6258 scope = cp_parser_namespace_name (parser);
6261 return scope;
6264 /* Return true if we are looking at a compound-literal, false otherwise. */
6266 static bool
6267 cp_parser_compound_literal_p (cp_parser *parser)
6269 /* Consume the `('. */
6270 cp_lexer_consume_token (parser->lexer);
6272 cp_lexer_save_tokens (parser->lexer);
6274 /* Skip tokens until the next token is a closing parenthesis.
6275 If we find the closing `)', and the next token is a `{', then
6276 we are looking at a compound-literal. */
6277 bool compound_literal_p
6278 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6279 /*consume_paren=*/true)
6280 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6282 /* Roll back the tokens we skipped. */
6283 cp_lexer_rollback_tokens (parser->lexer);
6285 return compound_literal_p;
6288 /* Parse a postfix-expression.
6290 postfix-expression:
6291 primary-expression
6292 postfix-expression [ expression ]
6293 postfix-expression ( expression-list [opt] )
6294 simple-type-specifier ( expression-list [opt] )
6295 typename :: [opt] nested-name-specifier identifier
6296 ( expression-list [opt] )
6297 typename :: [opt] nested-name-specifier template [opt] template-id
6298 ( expression-list [opt] )
6299 postfix-expression . template [opt] id-expression
6300 postfix-expression -> template [opt] id-expression
6301 postfix-expression . pseudo-destructor-name
6302 postfix-expression -> pseudo-destructor-name
6303 postfix-expression ++
6304 postfix-expression --
6305 dynamic_cast < type-id > ( expression )
6306 static_cast < type-id > ( expression )
6307 reinterpret_cast < type-id > ( expression )
6308 const_cast < type-id > ( expression )
6309 typeid ( expression )
6310 typeid ( type-id )
6312 GNU Extension:
6314 postfix-expression:
6315 ( type-id ) { initializer-list , [opt] }
6317 This extension is a GNU version of the C99 compound-literal
6318 construct. (The C99 grammar uses `type-name' instead of `type-id',
6319 but they are essentially the same concept.)
6321 If ADDRESS_P is true, the postfix expression is the operand of the
6322 `&' operator. CAST_P is true if this expression is the target of a
6323 cast.
6325 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6326 class member access expressions [expr.ref].
6328 Returns a representation of the expression. */
6330 static cp_expr
6331 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6332 bool member_access_only_p, bool decltype_p,
6333 cp_id_kind * pidk_return)
6335 cp_token *token;
6336 location_t loc;
6337 enum rid keyword;
6338 cp_id_kind idk = CP_ID_KIND_NONE;
6339 cp_expr postfix_expression = NULL_TREE;
6340 bool is_member_access = false;
6341 int saved_in_statement = -1;
6343 /* Peek at the next token. */
6344 token = cp_lexer_peek_token (parser->lexer);
6345 loc = token->location;
6346 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6348 /* Some of the productions are determined by keywords. */
6349 keyword = token->keyword;
6350 switch (keyword)
6352 case RID_DYNCAST:
6353 case RID_STATCAST:
6354 case RID_REINTCAST:
6355 case RID_CONSTCAST:
6357 tree type;
6358 cp_expr expression;
6359 const char *saved_message;
6360 bool saved_in_type_id_in_expr_p;
6362 /* All of these can be handled in the same way from the point
6363 of view of parsing. Begin by consuming the token
6364 identifying the cast. */
6365 cp_lexer_consume_token (parser->lexer);
6367 /* New types cannot be defined in the cast. */
6368 saved_message = parser->type_definition_forbidden_message;
6369 parser->type_definition_forbidden_message
6370 = G_("types may not be defined in casts");
6372 /* Look for the opening `<'. */
6373 cp_parser_require (parser, CPP_LESS, RT_LESS);
6374 /* Parse the type to which we are casting. */
6375 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6376 parser->in_type_id_in_expr_p = true;
6377 type = cp_parser_type_id (parser);
6378 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6379 /* Look for the closing `>'. */
6380 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6381 /* Restore the old message. */
6382 parser->type_definition_forbidden_message = saved_message;
6384 bool saved_greater_than_is_operator_p
6385 = parser->greater_than_is_operator_p;
6386 parser->greater_than_is_operator_p = true;
6388 /* And the expression which is being cast. */
6389 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6390 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6391 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6392 RT_CLOSE_PAREN);
6393 location_t end_loc = close_paren ?
6394 close_paren->location : UNKNOWN_LOCATION;
6396 parser->greater_than_is_operator_p
6397 = saved_greater_than_is_operator_p;
6399 /* Only type conversions to integral or enumeration types
6400 can be used in constant-expressions. */
6401 if (!cast_valid_in_integral_constant_expression_p (type)
6402 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6403 return error_mark_node;
6405 switch (keyword)
6407 case RID_DYNCAST:
6408 postfix_expression
6409 = build_dynamic_cast (type, expression, tf_warning_or_error);
6410 break;
6411 case RID_STATCAST:
6412 postfix_expression
6413 = build_static_cast (type, expression, tf_warning_or_error);
6414 break;
6415 case RID_REINTCAST:
6416 postfix_expression
6417 = build_reinterpret_cast (type, expression,
6418 tf_warning_or_error);
6419 break;
6420 case RID_CONSTCAST:
6421 postfix_expression
6422 = build_const_cast (type, expression, tf_warning_or_error);
6423 break;
6424 default:
6425 gcc_unreachable ();
6428 /* Construct a location e.g. :
6429 reinterpret_cast <int *> (expr)
6430 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6431 ranging from the start of the "*_cast" token to the final closing
6432 paren, with the caret at the start. */
6433 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6434 postfix_expression.set_location (cp_cast_loc);
6436 break;
6438 case RID_TYPEID:
6440 tree type;
6441 const char *saved_message;
6442 bool saved_in_type_id_in_expr_p;
6444 /* Consume the `typeid' token. */
6445 cp_lexer_consume_token (parser->lexer);
6446 /* Look for the `(' token. */
6447 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6448 /* Types cannot be defined in a `typeid' expression. */
6449 saved_message = parser->type_definition_forbidden_message;
6450 parser->type_definition_forbidden_message
6451 = G_("types may not be defined in a %<typeid%> expression");
6452 /* We can't be sure yet whether we're looking at a type-id or an
6453 expression. */
6454 cp_parser_parse_tentatively (parser);
6455 /* Try a type-id first. */
6456 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6457 parser->in_type_id_in_expr_p = true;
6458 type = cp_parser_type_id (parser);
6459 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6460 /* Look for the `)' token. Otherwise, we can't be sure that
6461 we're not looking at an expression: consider `typeid (int
6462 (3))', for example. */
6463 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6464 /* If all went well, simply lookup the type-id. */
6465 if (cp_parser_parse_definitely (parser))
6466 postfix_expression = get_typeid (type, tf_warning_or_error);
6467 /* Otherwise, fall back to the expression variant. */
6468 else
6470 tree expression;
6472 /* Look for an expression. */
6473 expression = cp_parser_expression (parser, & idk);
6474 /* Compute its typeid. */
6475 postfix_expression = build_typeid (expression, tf_warning_or_error);
6476 /* Look for the `)' token. */
6477 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6479 /* Restore the saved message. */
6480 parser->type_definition_forbidden_message = saved_message;
6481 /* `typeid' may not appear in an integral constant expression. */
6482 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6483 return error_mark_node;
6485 break;
6487 case RID_TYPENAME:
6489 tree type;
6490 /* The syntax permitted here is the same permitted for an
6491 elaborated-type-specifier. */
6492 ++parser->prevent_constrained_type_specifiers;
6493 type = cp_parser_elaborated_type_specifier (parser,
6494 /*is_friend=*/false,
6495 /*is_declaration=*/false);
6496 --parser->prevent_constrained_type_specifiers;
6497 postfix_expression = cp_parser_functional_cast (parser, type);
6499 break;
6501 case RID_CILK_SPAWN:
6503 location_t cilk_spawn_loc
6504 = cp_lexer_peek_token (parser->lexer)->location;
6505 cp_lexer_consume_token (parser->lexer);
6506 token = cp_lexer_peek_token (parser->lexer);
6507 if (token->type == CPP_SEMICOLON)
6509 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6510 "an expression");
6511 postfix_expression = error_mark_node;
6512 break;
6514 else if (!current_function_decl)
6516 error_at (token->location, "%<_Cilk_spawn%> may only be used "
6517 "inside a function");
6518 postfix_expression = error_mark_node;
6519 break;
6521 else
6523 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6524 saved_in_statement = parser->in_statement;
6525 parser->in_statement |= IN_CILK_SPAWN;
6527 cfun->calls_cilk_spawn = 1;
6528 postfix_expression =
6529 cp_parser_postfix_expression (parser, false, false,
6530 false, false, &idk);
6531 if (!flag_cilkplus)
6533 error_at (token->location, "-fcilkplus must be enabled to use"
6534 " %<_Cilk_spawn%>");
6535 cfun->calls_cilk_spawn = 0;
6537 else if (saved_in_statement & IN_CILK_SPAWN)
6539 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6540 "are not permitted");
6541 postfix_expression = error_mark_node;
6542 cfun->calls_cilk_spawn = 0;
6544 else
6546 location_t loc = postfix_expression.get_location ();
6547 postfix_expression = build_cilk_spawn (token->location,
6548 postfix_expression);
6549 /* Build a location of the form:
6550 _Cilk_spawn expr
6551 ~~~~~~~~~~~~^~~~
6552 with caret at the expr, ranging from the start of the
6553 _Cilk_spawn token to the end of the expression. */
6554 location_t combined_loc =
6555 make_location (loc, cilk_spawn_loc, get_finish (loc));
6556 postfix_expression.set_location (combined_loc);
6557 if (postfix_expression != error_mark_node)
6558 SET_EXPR_LOCATION (postfix_expression, input_location);
6559 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6561 break;
6564 case RID_BUILTIN_SHUFFLE:
6566 vec<tree, va_gc> *vec;
6567 unsigned int i;
6568 tree p;
6570 cp_lexer_consume_token (parser->lexer);
6571 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6572 /*cast_p=*/false, /*allow_expansion_p=*/true,
6573 /*non_constant_p=*/NULL);
6574 if (vec == NULL)
6575 return error_mark_node;
6577 FOR_EACH_VEC_ELT (*vec, i, p)
6578 mark_exp_read (p);
6580 if (vec->length () == 2)
6581 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
6582 tf_warning_or_error);
6583 else if (vec->length () == 3)
6584 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6585 tf_warning_or_error);
6586 else
6588 error_at (loc, "wrong number of arguments to "
6589 "%<__builtin_shuffle%>");
6590 return error_mark_node;
6592 break;
6595 default:
6597 tree type;
6599 /* If the next thing is a simple-type-specifier, we may be
6600 looking at a functional cast. We could also be looking at
6601 an id-expression. So, we try the functional cast, and if
6602 that doesn't work we fall back to the primary-expression. */
6603 cp_parser_parse_tentatively (parser);
6604 /* Look for the simple-type-specifier. */
6605 ++parser->prevent_constrained_type_specifiers;
6606 type = cp_parser_simple_type_specifier (parser,
6607 /*decl_specs=*/NULL,
6608 CP_PARSER_FLAGS_NONE);
6609 --parser->prevent_constrained_type_specifiers;
6610 /* Parse the cast itself. */
6611 if (!cp_parser_error_occurred (parser))
6612 postfix_expression
6613 = cp_parser_functional_cast (parser, type);
6614 /* If that worked, we're done. */
6615 if (cp_parser_parse_definitely (parser))
6616 break;
6618 /* If the functional-cast didn't work out, try a
6619 compound-literal. */
6620 if (cp_parser_allow_gnu_extensions_p (parser)
6621 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6623 cp_expr initializer = NULL_TREE;
6625 cp_parser_parse_tentatively (parser);
6627 /* Avoid calling cp_parser_type_id pointlessly, see comment
6628 in cp_parser_cast_expression about c++/29234. */
6629 if (!cp_parser_compound_literal_p (parser))
6630 cp_parser_simulate_error (parser);
6631 else
6633 /* Parse the type. */
6634 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6635 parser->in_type_id_in_expr_p = true;
6636 type = cp_parser_type_id (parser);
6637 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6638 /* Look for the `)'. */
6639 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6642 /* If things aren't going well, there's no need to
6643 keep going. */
6644 if (!cp_parser_error_occurred (parser))
6646 bool non_constant_p;
6647 /* Parse the brace-enclosed initializer list. */
6648 initializer = cp_parser_braced_list (parser,
6649 &non_constant_p);
6651 /* If that worked, we're definitely looking at a
6652 compound-literal expression. */
6653 if (cp_parser_parse_definitely (parser))
6655 /* Warn the user that a compound literal is not
6656 allowed in standard C++. */
6657 pedwarn (input_location, OPT_Wpedantic,
6658 "ISO C++ forbids compound-literals");
6659 /* For simplicity, we disallow compound literals in
6660 constant-expressions. We could
6661 allow compound literals of integer type, whose
6662 initializer was a constant, in constant
6663 expressions. Permitting that usage, as a further
6664 extension, would not change the meaning of any
6665 currently accepted programs. (Of course, as
6666 compound literals are not part of ISO C++, the
6667 standard has nothing to say.) */
6668 if (cp_parser_non_integral_constant_expression (parser,
6669 NIC_NCC))
6671 postfix_expression = error_mark_node;
6672 break;
6674 /* Form the representation of the compound-literal. */
6675 postfix_expression
6676 = finish_compound_literal (type, initializer,
6677 tf_warning_or_error);
6678 postfix_expression.set_location (initializer.get_location ());
6679 break;
6683 /* It must be a primary-expression. */
6684 postfix_expression
6685 = cp_parser_primary_expression (parser, address_p, cast_p,
6686 /*template_arg_p=*/false,
6687 decltype_p,
6688 &idk);
6690 break;
6693 /* Note that we don't need to worry about calling build_cplus_new on a
6694 class-valued CALL_EXPR in decltype when it isn't the end of the
6695 postfix-expression; unary_complex_lvalue will take care of that for
6696 all these cases. */
6698 /* Keep looping until the postfix-expression is complete. */
6699 while (true)
6701 if (idk == CP_ID_KIND_UNQUALIFIED
6702 && identifier_p (postfix_expression)
6703 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6704 /* It is not a Koenig lookup function call. */
6705 postfix_expression
6706 = unqualified_name_lookup_error (postfix_expression);
6708 /* Peek at the next token. */
6709 token = cp_lexer_peek_token (parser->lexer);
6711 switch (token->type)
6713 case CPP_OPEN_SQUARE:
6714 if (cp_next_tokens_can_be_std_attribute_p (parser))
6716 cp_parser_error (parser,
6717 "two consecutive %<[%> shall "
6718 "only introduce an attribute");
6719 return error_mark_node;
6721 postfix_expression
6722 = cp_parser_postfix_open_square_expression (parser,
6723 postfix_expression,
6724 false,
6725 decltype_p);
6726 postfix_expression.set_range (start_loc,
6727 postfix_expression.get_location ());
6729 idk = CP_ID_KIND_NONE;
6730 is_member_access = false;
6731 break;
6733 case CPP_OPEN_PAREN:
6734 /* postfix-expression ( expression-list [opt] ) */
6736 bool koenig_p;
6737 bool is_builtin_constant_p;
6738 bool saved_integral_constant_expression_p = false;
6739 bool saved_non_integral_constant_expression_p = false;
6740 tsubst_flags_t complain = complain_flags (decltype_p);
6741 vec<tree, va_gc> *args;
6742 location_t close_paren_loc = UNKNOWN_LOCATION;
6744 is_member_access = false;
6746 is_builtin_constant_p
6747 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6748 if (is_builtin_constant_p)
6750 /* The whole point of __builtin_constant_p is to allow
6751 non-constant expressions to appear as arguments. */
6752 saved_integral_constant_expression_p
6753 = parser->integral_constant_expression_p;
6754 saved_non_integral_constant_expression_p
6755 = parser->non_integral_constant_expression_p;
6756 parser->integral_constant_expression_p = false;
6758 args = (cp_parser_parenthesized_expression_list
6759 (parser, non_attr,
6760 /*cast_p=*/false, /*allow_expansion_p=*/true,
6761 /*non_constant_p=*/NULL,
6762 /*close_paren_loc=*/&close_paren_loc));
6763 if (is_builtin_constant_p)
6765 parser->integral_constant_expression_p
6766 = saved_integral_constant_expression_p;
6767 parser->non_integral_constant_expression_p
6768 = saved_non_integral_constant_expression_p;
6771 if (args == NULL)
6773 postfix_expression = error_mark_node;
6774 break;
6777 /* Function calls are not permitted in
6778 constant-expressions. */
6779 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6780 && cp_parser_non_integral_constant_expression (parser,
6781 NIC_FUNC_CALL))
6783 postfix_expression = error_mark_node;
6784 release_tree_vector (args);
6785 break;
6788 koenig_p = false;
6789 if (idk == CP_ID_KIND_UNQUALIFIED
6790 || idk == CP_ID_KIND_TEMPLATE_ID)
6792 if (identifier_p (postfix_expression))
6794 if (!args->is_empty ())
6796 koenig_p = true;
6797 if (!any_type_dependent_arguments_p (args))
6798 postfix_expression
6799 = perform_koenig_lookup (postfix_expression, args,
6800 complain);
6802 else
6803 postfix_expression
6804 = unqualified_fn_lookup_error (postfix_expression);
6806 /* We do not perform argument-dependent lookup if
6807 normal lookup finds a non-function, in accordance
6808 with the expected resolution of DR 218. */
6809 else if (!args->is_empty ()
6810 && is_overloaded_fn (postfix_expression))
6812 tree fn = get_first_fn (postfix_expression);
6813 fn = STRIP_TEMPLATE (fn);
6815 /* Do not do argument dependent lookup if regular
6816 lookup finds a member function or a block-scope
6817 function declaration. [basic.lookup.argdep]/3 */
6818 if (!DECL_FUNCTION_MEMBER_P (fn)
6819 && !DECL_LOCAL_FUNCTION_P (fn))
6821 koenig_p = true;
6822 if (!any_type_dependent_arguments_p (args))
6823 postfix_expression
6824 = perform_koenig_lookup (postfix_expression, args,
6825 complain);
6830 if (warn_memset_transposed_args)
6832 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6833 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6834 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6835 && vec_safe_length (args) == 3
6836 && TREE_CODE ((*args)[2]) == INTEGER_CST
6837 && integer_zerop ((*args)[2])
6838 && !(TREE_CODE ((*args)[1]) == INTEGER_CST
6839 && integer_zerop ((*args)[1])))
6840 warning (OPT_Wmemset_transposed_args,
6841 "%<memset%> used with constant zero length "
6842 "parameter; this could be due to transposed "
6843 "parameters");
6846 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6848 tree instance = TREE_OPERAND (postfix_expression, 0);
6849 tree fn = TREE_OPERAND (postfix_expression, 1);
6851 if (processing_template_decl
6852 && (type_dependent_expression_p (instance)
6853 || (!BASELINK_P (fn)
6854 && TREE_CODE (fn) != FIELD_DECL)
6855 || type_dependent_expression_p (fn)
6856 || any_type_dependent_arguments_p (args)))
6858 postfix_expression
6859 = build_nt_call_vec (postfix_expression, args);
6860 release_tree_vector (args);
6861 break;
6864 if (BASELINK_P (fn))
6866 postfix_expression
6867 = (build_new_method_call
6868 (instance, fn, &args, NULL_TREE,
6869 (idk == CP_ID_KIND_QUALIFIED
6870 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6871 : LOOKUP_NORMAL),
6872 /*fn_p=*/NULL,
6873 complain));
6875 else
6876 postfix_expression
6877 = finish_call_expr (postfix_expression, &args,
6878 /*disallow_virtual=*/false,
6879 /*koenig_p=*/false,
6880 complain);
6882 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6883 || TREE_CODE (postfix_expression) == MEMBER_REF
6884 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6885 postfix_expression = (build_offset_ref_call_from_tree
6886 (postfix_expression, &args,
6887 complain));
6888 else if (idk == CP_ID_KIND_QUALIFIED)
6889 /* A call to a static class member, or a namespace-scope
6890 function. */
6891 postfix_expression
6892 = finish_call_expr (postfix_expression, &args,
6893 /*disallow_virtual=*/true,
6894 koenig_p,
6895 complain);
6896 else
6897 /* All other function calls. */
6898 postfix_expression
6899 = finish_call_expr (postfix_expression, &args,
6900 /*disallow_virtual=*/false,
6901 koenig_p,
6902 complain);
6904 if (close_paren_loc != UNKNOWN_LOCATION)
6906 location_t combined_loc = make_location (token->location,
6907 start_loc,
6908 close_paren_loc);
6909 postfix_expression.set_location (combined_loc);
6912 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6913 idk = CP_ID_KIND_NONE;
6915 release_tree_vector (args);
6917 break;
6919 case CPP_DOT:
6920 case CPP_DEREF:
6921 /* postfix-expression . template [opt] id-expression
6922 postfix-expression . pseudo-destructor-name
6923 postfix-expression -> template [opt] id-expression
6924 postfix-expression -> pseudo-destructor-name */
6926 /* Consume the `.' or `->' operator. */
6927 cp_lexer_consume_token (parser->lexer);
6929 postfix_expression
6930 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6931 postfix_expression,
6932 false, &idk, loc);
6934 is_member_access = true;
6935 break;
6937 case CPP_PLUS_PLUS:
6938 /* postfix-expression ++ */
6939 /* Consume the `++' token. */
6940 cp_lexer_consume_token (parser->lexer);
6941 /* Generate a representation for the complete expression. */
6942 postfix_expression
6943 = finish_increment_expr (postfix_expression,
6944 POSTINCREMENT_EXPR);
6945 /* Increments may not appear in constant-expressions. */
6946 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6947 postfix_expression = error_mark_node;
6948 idk = CP_ID_KIND_NONE;
6949 is_member_access = false;
6950 break;
6952 case CPP_MINUS_MINUS:
6953 /* postfix-expression -- */
6954 /* Consume the `--' token. */
6955 cp_lexer_consume_token (parser->lexer);
6956 /* Generate a representation for the complete expression. */
6957 postfix_expression
6958 = finish_increment_expr (postfix_expression,
6959 POSTDECREMENT_EXPR);
6960 /* Decrements may not appear in constant-expressions. */
6961 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6962 postfix_expression = error_mark_node;
6963 idk = CP_ID_KIND_NONE;
6964 is_member_access = false;
6965 break;
6967 default:
6968 if (pidk_return != NULL)
6969 * pidk_return = idk;
6970 if (member_access_only_p)
6971 return is_member_access
6972 ? postfix_expression
6973 : cp_expr (error_mark_node);
6974 else
6975 return postfix_expression;
6979 /* We should never get here. */
6980 gcc_unreachable ();
6981 return error_mark_node;
6984 /* This function parses Cilk Plus array notations. If a normal array expr. is
6985 parsed then the array index is passed back to the caller through *INIT_INDEX
6986 and the function returns a NULL_TREE. If array notation expr. is parsed,
6987 then *INIT_INDEX is ignored by the caller and the function returns
6988 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6989 error_mark_node. */
6991 static tree
6992 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6993 tree array_value)
6995 cp_token *token = NULL;
6996 tree length_index, stride = NULL_TREE, value_tree, array_type;
6997 if (!array_value || array_value == error_mark_node)
6999 cp_parser_skip_to_end_of_statement (parser);
7000 return error_mark_node;
7003 array_type = TREE_TYPE (array_value);
7005 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
7006 parser->colon_corrects_to_scope_p = false;
7007 token = cp_lexer_peek_token (parser->lexer);
7009 if (!token)
7011 cp_parser_error (parser, "expected %<:%> or numeral");
7012 return error_mark_node;
7014 else if (token->type == CPP_COLON)
7016 /* Consume the ':'. */
7017 cp_lexer_consume_token (parser->lexer);
7019 /* If we are here, then we have a case like this A[:]. */
7020 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
7022 cp_parser_error (parser, "expected %<]%>");
7023 cp_parser_skip_to_end_of_statement (parser);
7024 return error_mark_node;
7026 *init_index = NULL_TREE;
7027 stride = NULL_TREE;
7028 length_index = NULL_TREE;
7030 else
7032 /* If we are here, then there are three valid possibilities:
7033 1. ARRAY [ EXP ]
7034 2. ARRAY [ EXP : EXP ]
7035 3. ARRAY [ EXP : EXP : EXP ] */
7037 *init_index = cp_parser_expression (parser);
7038 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
7040 /* This indicates that we have a normal array expression. */
7041 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7042 return NULL_TREE;
7045 /* Consume the ':'. */
7046 cp_lexer_consume_token (parser->lexer);
7047 length_index = cp_parser_expression (parser);
7048 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7050 cp_lexer_consume_token (parser->lexer);
7051 stride = cp_parser_expression (parser);
7054 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7056 if (*init_index == error_mark_node || length_index == error_mark_node
7057 || stride == error_mark_node || array_type == error_mark_node)
7059 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
7060 cp_lexer_consume_token (parser->lexer);
7061 return error_mark_node;
7063 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7065 value_tree = build_array_notation_ref (loc, array_value, *init_index,
7066 length_index, stride, array_type);
7067 return value_tree;
7070 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7071 by cp_parser_builtin_offsetof. We're looking for
7073 postfix-expression [ expression ]
7074 postfix-expression [ braced-init-list ] (C++11)
7076 FOR_OFFSETOF is set if we're being called in that context, which
7077 changes how we deal with integer constant expressions. */
7079 static tree
7080 cp_parser_postfix_open_square_expression (cp_parser *parser,
7081 tree postfix_expression,
7082 bool for_offsetof,
7083 bool decltype_p)
7085 tree index = NULL_TREE;
7086 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7087 bool saved_greater_than_is_operator_p;
7089 /* Consume the `[' token. */
7090 cp_lexer_consume_token (parser->lexer);
7092 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7093 parser->greater_than_is_operator_p = true;
7095 /* Parse the index expression. */
7096 /* ??? For offsetof, there is a question of what to allow here. If
7097 offsetof is not being used in an integral constant expression context,
7098 then we *could* get the right answer by computing the value at runtime.
7099 If we are in an integral constant expression context, then we might
7100 could accept any constant expression; hard to say without analysis.
7101 Rather than open the barn door too wide right away, allow only integer
7102 constant expressions here. */
7103 if (for_offsetof)
7104 index = cp_parser_constant_expression (parser);
7105 else
7107 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7109 bool expr_nonconst_p;
7110 cp_lexer_set_source_position (parser->lexer);
7111 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7112 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7113 if (flag_cilkplus
7114 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7116 error_at (cp_lexer_peek_token (parser->lexer)->location,
7117 "braced list index is not allowed with array "
7118 "notation");
7119 cp_parser_skip_to_end_of_statement (parser);
7120 return error_mark_node;
7123 else if (flag_cilkplus)
7125 /* Here are have these two options:
7126 ARRAY[EXP : EXP] - Array notation expr with default
7127 stride of 1.
7128 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7129 stride. */
7130 tree an_exp = cp_parser_array_notation (loc, parser, &index,
7131 postfix_expression);
7132 if (an_exp)
7133 return an_exp;
7135 else
7136 index = cp_parser_expression (parser);
7139 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7141 /* Look for the closing `]'. */
7142 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7144 /* Build the ARRAY_REF. */
7145 postfix_expression = grok_array_decl (loc, postfix_expression,
7146 index, decltype_p);
7148 /* When not doing offsetof, array references are not permitted in
7149 constant-expressions. */
7150 if (!for_offsetof
7151 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7152 postfix_expression = error_mark_node;
7154 return postfix_expression;
7157 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7158 by cp_parser_builtin_offsetof. We're looking for
7160 postfix-expression . template [opt] id-expression
7161 postfix-expression . pseudo-destructor-name
7162 postfix-expression -> template [opt] id-expression
7163 postfix-expression -> pseudo-destructor-name
7165 FOR_OFFSETOF is set if we're being called in that context. That sorta
7166 limits what of the above we'll actually accept, but nevermind.
7167 TOKEN_TYPE is the "." or "->" token, which will already have been
7168 removed from the stream. */
7170 static tree
7171 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7172 enum cpp_ttype token_type,
7173 cp_expr postfix_expression,
7174 bool for_offsetof, cp_id_kind *idk,
7175 location_t location)
7177 tree name;
7178 bool dependent_p;
7179 bool pseudo_destructor_p;
7180 tree scope = NULL_TREE;
7181 location_t start_loc = postfix_expression.get_start ();
7183 /* If this is a `->' operator, dereference the pointer. */
7184 if (token_type == CPP_DEREF)
7185 postfix_expression = build_x_arrow (location, postfix_expression,
7186 tf_warning_or_error);
7187 /* Check to see whether or not the expression is type-dependent. */
7188 dependent_p = type_dependent_expression_p (postfix_expression);
7189 /* The identifier following the `->' or `.' is not qualified. */
7190 parser->scope = NULL_TREE;
7191 parser->qualifying_scope = NULL_TREE;
7192 parser->object_scope = NULL_TREE;
7193 *idk = CP_ID_KIND_NONE;
7195 /* Enter the scope corresponding to the type of the object
7196 given by the POSTFIX_EXPRESSION. */
7197 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
7199 scope = TREE_TYPE (postfix_expression);
7200 /* According to the standard, no expression should ever have
7201 reference type. Unfortunately, we do not currently match
7202 the standard in this respect in that our internal representation
7203 of an expression may have reference type even when the standard
7204 says it does not. Therefore, we have to manually obtain the
7205 underlying type here. */
7206 scope = non_reference (scope);
7207 /* The type of the POSTFIX_EXPRESSION must be complete. */
7208 if (scope == unknown_type_node)
7210 error_at (location, "%qE does not have class type",
7211 postfix_expression.get_value ());
7212 scope = NULL_TREE;
7214 /* Unlike the object expression in other contexts, *this is not
7215 required to be of complete type for purposes of class member
7216 access (5.2.5) outside the member function body. */
7217 else if (postfix_expression != current_class_ref
7218 && !(processing_template_decl && scope == current_class_type))
7219 scope = complete_type_or_else (scope, NULL_TREE);
7220 /* Let the name lookup machinery know that we are processing a
7221 class member access expression. */
7222 parser->context->object_type = scope;
7223 /* If something went wrong, we want to be able to discern that case,
7224 as opposed to the case where there was no SCOPE due to the type
7225 of expression being dependent. */
7226 if (!scope)
7227 scope = error_mark_node;
7228 /* If the SCOPE was erroneous, make the various semantic analysis
7229 functions exit quickly -- and without issuing additional error
7230 messages. */
7231 if (scope == error_mark_node)
7232 postfix_expression = error_mark_node;
7235 /* Assume this expression is not a pseudo-destructor access. */
7236 pseudo_destructor_p = false;
7238 /* If the SCOPE is a scalar type, then, if this is a valid program,
7239 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7240 is type dependent, it can be pseudo-destructor-name or something else.
7241 Try to parse it as pseudo-destructor-name first. */
7242 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7244 tree s;
7245 tree type;
7247 cp_parser_parse_tentatively (parser);
7248 /* Parse the pseudo-destructor-name. */
7249 s = NULL_TREE;
7250 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7251 &s, &type);
7252 if (dependent_p
7253 && (cp_parser_error_occurred (parser)
7254 || !SCALAR_TYPE_P (type)))
7255 cp_parser_abort_tentative_parse (parser);
7256 else if (cp_parser_parse_definitely (parser))
7258 pseudo_destructor_p = true;
7259 postfix_expression
7260 = finish_pseudo_destructor_expr (postfix_expression,
7261 s, type, location);
7265 if (!pseudo_destructor_p)
7267 /* If the SCOPE is not a scalar type, we are looking at an
7268 ordinary class member access expression, rather than a
7269 pseudo-destructor-name. */
7270 bool template_p;
7271 cp_token *token = cp_lexer_peek_token (parser->lexer);
7272 /* Parse the id-expression. */
7273 name = (cp_parser_id_expression
7274 (parser,
7275 cp_parser_optional_template_keyword (parser),
7276 /*check_dependency_p=*/true,
7277 &template_p,
7278 /*declarator_p=*/false,
7279 /*optional_p=*/false));
7280 /* In general, build a SCOPE_REF if the member name is qualified.
7281 However, if the name was not dependent and has already been
7282 resolved; there is no need to build the SCOPE_REF. For example;
7284 struct X { void f(); };
7285 template <typename T> void f(T* t) { t->X::f(); }
7287 Even though "t" is dependent, "X::f" is not and has been resolved
7288 to a BASELINK; there is no need to include scope information. */
7290 /* But we do need to remember that there was an explicit scope for
7291 virtual function calls. */
7292 if (parser->scope)
7293 *idk = CP_ID_KIND_QUALIFIED;
7295 /* If the name is a template-id that names a type, we will get a
7296 TYPE_DECL here. That is invalid code. */
7297 if (TREE_CODE (name) == TYPE_DECL)
7299 error_at (token->location, "invalid use of %qD", name);
7300 postfix_expression = error_mark_node;
7302 else
7304 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7306 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7308 error_at (token->location, "%<%D::%D%> is not a class member",
7309 parser->scope, name);
7310 postfix_expression = error_mark_node;
7312 else
7313 name = build_qualified_name (/*type=*/NULL_TREE,
7314 parser->scope,
7315 name,
7316 template_p);
7317 parser->scope = NULL_TREE;
7318 parser->qualifying_scope = NULL_TREE;
7319 parser->object_scope = NULL_TREE;
7321 if (parser->scope && name && BASELINK_P (name))
7322 adjust_result_of_qualified_name_lookup
7323 (name, parser->scope, scope);
7324 postfix_expression
7325 = finish_class_member_access_expr (postfix_expression, name,
7326 template_p,
7327 tf_warning_or_error);
7328 /* Build a location e.g.:
7329 ptr->access_expr
7330 ~~~^~~~~~~~~~~~~
7331 where the caret is at the deref token, ranging from
7332 the start of postfix_expression to the end of the access expr. */
7333 location_t end_loc
7334 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7335 location_t combined_loc
7336 = make_location (input_location, start_loc, end_loc);
7337 protected_set_expr_location (postfix_expression, combined_loc);
7341 /* We no longer need to look up names in the scope of the object on
7342 the left-hand side of the `.' or `->' operator. */
7343 parser->context->object_type = NULL_TREE;
7345 /* Outside of offsetof, these operators may not appear in
7346 constant-expressions. */
7347 if (!for_offsetof
7348 && (cp_parser_non_integral_constant_expression
7349 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7350 postfix_expression = error_mark_node;
7352 return postfix_expression;
7355 /* Parse a parenthesized expression-list.
7357 expression-list:
7358 assignment-expression
7359 expression-list, assignment-expression
7361 attribute-list:
7362 expression-list
7363 identifier
7364 identifier, expression-list
7366 CAST_P is true if this expression is the target of a cast.
7368 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7369 argument pack.
7371 Returns a vector of trees. Each element is a representation of an
7372 assignment-expression. NULL is returned if the ( and or ) are
7373 missing. An empty, but allocated, vector is returned on no
7374 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7375 if we are parsing an attribute list for an attribute that wants a
7376 plain identifier argument, normal_attr for an attribute that wants
7377 an expression, or non_attr if we aren't parsing an attribute list. If
7378 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7379 not all of the expressions in the list were constant.
7380 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7381 will be written to with the location of the closing parenthesis. If
7382 an error occurs, it may or may not be written to. */
7384 static vec<tree, va_gc> *
7385 cp_parser_parenthesized_expression_list (cp_parser* parser,
7386 int is_attribute_list,
7387 bool cast_p,
7388 bool allow_expansion_p,
7389 bool *non_constant_p,
7390 location_t *close_paren_loc)
7392 vec<tree, va_gc> *expression_list;
7393 bool fold_expr_p = is_attribute_list != non_attr;
7394 tree identifier = NULL_TREE;
7395 bool saved_greater_than_is_operator_p;
7397 /* Assume all the expressions will be constant. */
7398 if (non_constant_p)
7399 *non_constant_p = false;
7401 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
7402 return NULL;
7404 expression_list = make_tree_vector ();
7406 /* Within a parenthesized expression, a `>' token is always
7407 the greater-than operator. */
7408 saved_greater_than_is_operator_p
7409 = parser->greater_than_is_operator_p;
7410 parser->greater_than_is_operator_p = true;
7412 /* Consume expressions until there are no more. */
7413 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7414 while (true)
7416 tree expr;
7418 /* At the beginning of attribute lists, check to see if the
7419 next token is an identifier. */
7420 if (is_attribute_list == id_attr
7421 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7423 cp_token *token;
7425 /* Consume the identifier. */
7426 token = cp_lexer_consume_token (parser->lexer);
7427 /* Save the identifier. */
7428 identifier = token->u.value;
7430 else
7432 bool expr_non_constant_p;
7434 /* Parse the next assignment-expression. */
7435 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7437 /* A braced-init-list. */
7438 cp_lexer_set_source_position (parser->lexer);
7439 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7440 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7441 if (non_constant_p && expr_non_constant_p)
7442 *non_constant_p = true;
7444 else if (non_constant_p)
7446 expr = (cp_parser_constant_expression
7447 (parser, /*allow_non_constant_p=*/true,
7448 &expr_non_constant_p));
7449 if (expr_non_constant_p)
7450 *non_constant_p = true;
7452 else
7453 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7454 cast_p);
7456 if (fold_expr_p)
7457 expr = instantiate_non_dependent_expr (expr);
7459 /* If we have an ellipsis, then this is an expression
7460 expansion. */
7461 if (allow_expansion_p
7462 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7464 /* Consume the `...'. */
7465 cp_lexer_consume_token (parser->lexer);
7467 /* Build the argument pack. */
7468 expr = make_pack_expansion (expr);
7471 /* Add it to the list. We add error_mark_node
7472 expressions to the list, so that we can still tell if
7473 the correct form for a parenthesized expression-list
7474 is found. That gives better errors. */
7475 vec_safe_push (expression_list, expr);
7477 if (expr == error_mark_node)
7478 goto skip_comma;
7481 /* After the first item, attribute lists look the same as
7482 expression lists. */
7483 is_attribute_list = non_attr;
7485 get_comma:;
7486 /* If the next token isn't a `,', then we are done. */
7487 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7488 break;
7490 /* Otherwise, consume the `,' and keep going. */
7491 cp_lexer_consume_token (parser->lexer);
7494 if (close_paren_loc)
7495 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7497 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7499 int ending;
7501 skip_comma:;
7502 /* We try and resync to an unnested comma, as that will give the
7503 user better diagnostics. */
7504 ending = cp_parser_skip_to_closing_parenthesis (parser,
7505 /*recovering=*/true,
7506 /*or_comma=*/true,
7507 /*consume_paren=*/true);
7508 if (ending < 0)
7509 goto get_comma;
7510 if (!ending)
7512 parser->greater_than_is_operator_p
7513 = saved_greater_than_is_operator_p;
7514 return NULL;
7518 parser->greater_than_is_operator_p
7519 = saved_greater_than_is_operator_p;
7521 if (identifier)
7522 vec_safe_insert (expression_list, 0, identifier);
7524 return expression_list;
7527 /* Parse a pseudo-destructor-name.
7529 pseudo-destructor-name:
7530 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7531 :: [opt] nested-name-specifier template template-id :: ~ type-name
7532 :: [opt] nested-name-specifier [opt] ~ type-name
7534 If either of the first two productions is used, sets *SCOPE to the
7535 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7536 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7537 or ERROR_MARK_NODE if the parse fails. */
7539 static void
7540 cp_parser_pseudo_destructor_name (cp_parser* parser,
7541 tree object,
7542 tree* scope,
7543 tree* type)
7545 bool nested_name_specifier_p;
7547 /* Handle ~auto. */
7548 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7549 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7550 && !type_dependent_expression_p (object))
7552 if (cxx_dialect < cxx14)
7553 pedwarn (input_location, 0,
7554 "%<~auto%> only available with "
7555 "-std=c++14 or -std=gnu++14");
7556 cp_lexer_consume_token (parser->lexer);
7557 cp_lexer_consume_token (parser->lexer);
7558 *scope = NULL_TREE;
7559 *type = TREE_TYPE (object);
7560 return;
7563 /* Assume that things will not work out. */
7564 *type = error_mark_node;
7566 /* Look for the optional `::' operator. */
7567 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7568 /* Look for the optional nested-name-specifier. */
7569 nested_name_specifier_p
7570 = (cp_parser_nested_name_specifier_opt (parser,
7571 /*typename_keyword_p=*/false,
7572 /*check_dependency_p=*/true,
7573 /*type_p=*/false,
7574 /*is_declaration=*/false)
7575 != NULL_TREE);
7576 /* Now, if we saw a nested-name-specifier, we might be doing the
7577 second production. */
7578 if (nested_name_specifier_p
7579 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7581 /* Consume the `template' keyword. */
7582 cp_lexer_consume_token (parser->lexer);
7583 /* Parse the template-id. */
7584 cp_parser_template_id (parser,
7585 /*template_keyword_p=*/true,
7586 /*check_dependency_p=*/false,
7587 class_type,
7588 /*is_declaration=*/true);
7589 /* Look for the `::' token. */
7590 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7592 /* If the next token is not a `~', then there might be some
7593 additional qualification. */
7594 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7596 /* At this point, we're looking for "type-name :: ~". The type-name
7597 must not be a class-name, since this is a pseudo-destructor. So,
7598 it must be either an enum-name, or a typedef-name -- both of which
7599 are just identifiers. So, we peek ahead to check that the "::"
7600 and "~" tokens are present; if they are not, then we can avoid
7601 calling type_name. */
7602 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7603 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7604 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7606 cp_parser_error (parser, "non-scalar type");
7607 return;
7610 /* Look for the type-name. */
7611 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7612 if (*scope == error_mark_node)
7613 return;
7615 /* Look for the `::' token. */
7616 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7618 else
7619 *scope = NULL_TREE;
7621 /* Look for the `~'. */
7622 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7624 /* Once we see the ~, this has to be a pseudo-destructor. */
7625 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7626 cp_parser_commit_to_topmost_tentative_parse (parser);
7628 /* Look for the type-name again. We are not responsible for
7629 checking that it matches the first type-name. */
7630 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7633 /* Parse a unary-expression.
7635 unary-expression:
7636 postfix-expression
7637 ++ cast-expression
7638 -- cast-expression
7639 unary-operator cast-expression
7640 sizeof unary-expression
7641 sizeof ( type-id )
7642 alignof ( type-id ) [C++0x]
7643 new-expression
7644 delete-expression
7646 GNU Extensions:
7648 unary-expression:
7649 __extension__ cast-expression
7650 __alignof__ unary-expression
7651 __alignof__ ( type-id )
7652 alignof unary-expression [C++0x]
7653 __real__ cast-expression
7654 __imag__ cast-expression
7655 && identifier
7656 sizeof ( type-id ) { initializer-list , [opt] }
7657 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7658 __alignof__ ( type-id ) { initializer-list , [opt] }
7660 ADDRESS_P is true iff the unary-expression is appearing as the
7661 operand of the `&' operator. CAST_P is true if this expression is
7662 the target of a cast.
7664 Returns a representation of the expression. */
7666 static cp_expr
7667 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7668 bool address_p, bool cast_p, bool decltype_p)
7670 cp_token *token;
7671 enum tree_code unary_operator;
7673 /* Peek at the next token. */
7674 token = cp_lexer_peek_token (parser->lexer);
7675 /* Some keywords give away the kind of expression. */
7676 if (token->type == CPP_KEYWORD)
7678 enum rid keyword = token->keyword;
7680 switch (keyword)
7682 case RID_ALIGNOF:
7683 case RID_SIZEOF:
7685 tree operand, ret;
7686 enum tree_code op;
7687 location_t first_loc;
7689 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7690 /* Consume the token. */
7691 cp_lexer_consume_token (parser->lexer);
7692 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7693 /* Parse the operand. */
7694 operand = cp_parser_sizeof_operand (parser, keyword);
7696 if (TYPE_P (operand))
7697 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7698 else
7700 /* ISO C++ defines alignof only with types, not with
7701 expressions. So pedwarn if alignof is used with a non-
7702 type expression. However, __alignof__ is ok. */
7703 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7704 pedwarn (token->location, OPT_Wpedantic,
7705 "ISO C++ does not allow %<alignof%> "
7706 "with a non-type");
7708 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7710 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7711 SIZEOF_EXPR with the original operand. */
7712 if (op == SIZEOF_EXPR && ret != error_mark_node)
7714 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7716 if (!processing_template_decl && TYPE_P (operand))
7718 ret = build_min (SIZEOF_EXPR, size_type_node,
7719 build1 (NOP_EXPR, operand,
7720 error_mark_node));
7721 SIZEOF_EXPR_TYPE_P (ret) = 1;
7723 else
7724 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7725 TREE_SIDE_EFFECTS (ret) = 0;
7726 TREE_READONLY (ret) = 1;
7728 SET_EXPR_LOCATION (ret, first_loc);
7730 return ret;
7733 case RID_NEW:
7734 return cp_parser_new_expression (parser);
7736 case RID_DELETE:
7737 return cp_parser_delete_expression (parser);
7739 case RID_EXTENSION:
7741 /* The saved value of the PEDANTIC flag. */
7742 int saved_pedantic;
7743 tree expr;
7745 /* Save away the PEDANTIC flag. */
7746 cp_parser_extension_opt (parser, &saved_pedantic);
7747 /* Parse the cast-expression. */
7748 expr = cp_parser_simple_cast_expression (parser);
7749 /* Restore the PEDANTIC flag. */
7750 pedantic = saved_pedantic;
7752 return expr;
7755 case RID_REALPART:
7756 case RID_IMAGPART:
7758 tree expression;
7760 /* Consume the `__real__' or `__imag__' token. */
7761 cp_lexer_consume_token (parser->lexer);
7762 /* Parse the cast-expression. */
7763 expression = cp_parser_simple_cast_expression (parser);
7764 /* Create the complete representation. */
7765 return build_x_unary_op (token->location,
7766 (keyword == RID_REALPART
7767 ? REALPART_EXPR : IMAGPART_EXPR),
7768 expression,
7769 tf_warning_or_error);
7771 break;
7773 case RID_TRANSACTION_ATOMIC:
7774 case RID_TRANSACTION_RELAXED:
7775 return cp_parser_transaction_expression (parser, keyword);
7777 case RID_NOEXCEPT:
7779 tree expr;
7780 const char *saved_message;
7781 bool saved_integral_constant_expression_p;
7782 bool saved_non_integral_constant_expression_p;
7783 bool saved_greater_than_is_operator_p;
7785 cp_lexer_consume_token (parser->lexer);
7786 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7788 saved_message = parser->type_definition_forbidden_message;
7789 parser->type_definition_forbidden_message
7790 = G_("types may not be defined in %<noexcept%> expressions");
7792 saved_integral_constant_expression_p
7793 = parser->integral_constant_expression_p;
7794 saved_non_integral_constant_expression_p
7795 = parser->non_integral_constant_expression_p;
7796 parser->integral_constant_expression_p = false;
7798 saved_greater_than_is_operator_p
7799 = parser->greater_than_is_operator_p;
7800 parser->greater_than_is_operator_p = true;
7802 ++cp_unevaluated_operand;
7803 ++c_inhibit_evaluation_warnings;
7804 ++cp_noexcept_operand;
7805 expr = cp_parser_expression (parser);
7806 --cp_noexcept_operand;
7807 --c_inhibit_evaluation_warnings;
7808 --cp_unevaluated_operand;
7810 parser->greater_than_is_operator_p
7811 = saved_greater_than_is_operator_p;
7813 parser->integral_constant_expression_p
7814 = saved_integral_constant_expression_p;
7815 parser->non_integral_constant_expression_p
7816 = saved_non_integral_constant_expression_p;
7818 parser->type_definition_forbidden_message = saved_message;
7820 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7821 return finish_noexcept_expr (expr, tf_warning_or_error);
7824 default:
7825 break;
7829 /* Look for the `:: new' and `:: delete', which also signal the
7830 beginning of a new-expression, or delete-expression,
7831 respectively. If the next token is `::', then it might be one of
7832 these. */
7833 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7835 enum rid keyword;
7837 /* See if the token after the `::' is one of the keywords in
7838 which we're interested. */
7839 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7840 /* If it's `new', we have a new-expression. */
7841 if (keyword == RID_NEW)
7842 return cp_parser_new_expression (parser);
7843 /* Similarly, for `delete'. */
7844 else if (keyword == RID_DELETE)
7845 return cp_parser_delete_expression (parser);
7848 /* Look for a unary operator. */
7849 unary_operator = cp_parser_unary_operator (token);
7850 /* The `++' and `--' operators can be handled similarly, even though
7851 they are not technically unary-operators in the grammar. */
7852 if (unary_operator == ERROR_MARK)
7854 if (token->type == CPP_PLUS_PLUS)
7855 unary_operator = PREINCREMENT_EXPR;
7856 else if (token->type == CPP_MINUS_MINUS)
7857 unary_operator = PREDECREMENT_EXPR;
7858 /* Handle the GNU address-of-label extension. */
7859 else if (cp_parser_allow_gnu_extensions_p (parser)
7860 && token->type == CPP_AND_AND)
7862 tree identifier;
7863 tree expression;
7864 location_t start_loc = token->location;
7866 /* Consume the '&&' token. */
7867 cp_lexer_consume_token (parser->lexer);
7868 /* Look for the identifier. */
7869 location_t finish_loc
7870 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
7871 identifier = cp_parser_identifier (parser);
7872 /* Construct a location of the form:
7873 &&label
7874 ^~~~~~~
7875 with caret==start at the "&&", finish at the end of the label. */
7876 location_t combined_loc
7877 = make_location (start_loc, start_loc, finish_loc);
7878 /* Create an expression representing the address. */
7879 expression = finish_label_address_expr (identifier, combined_loc);
7880 if (cp_parser_non_integral_constant_expression (parser,
7881 NIC_ADDR_LABEL))
7882 expression = error_mark_node;
7883 return expression;
7886 if (unary_operator != ERROR_MARK)
7888 cp_expr cast_expression;
7889 cp_expr expression = error_mark_node;
7890 non_integral_constant non_constant_p = NIC_NONE;
7891 location_t loc = token->location;
7892 tsubst_flags_t complain = complain_flags (decltype_p);
7894 /* Consume the operator token. */
7895 token = cp_lexer_consume_token (parser->lexer);
7896 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
7898 /* Parse the cast-expression. */
7899 cast_expression
7900 = cp_parser_cast_expression (parser,
7901 unary_operator == ADDR_EXPR,
7902 /*cast_p=*/false,
7903 /*decltype*/false,
7904 pidk);
7906 /* Make a location:
7907 OP_TOKEN CAST_EXPRESSION
7908 ^~~~~~~~~~~~~~~~~~~~~~~~~
7909 with start==caret at the operator token, and
7910 extending to the end of the cast_expression. */
7911 loc = make_location (loc, loc, cast_expression.get_finish ());
7913 /* Now, build an appropriate representation. */
7914 switch (unary_operator)
7916 case INDIRECT_REF:
7917 non_constant_p = NIC_STAR;
7918 expression = build_x_indirect_ref (loc, cast_expression,
7919 RO_UNARY_STAR,
7920 complain);
7921 /* TODO: build_x_indirect_ref does not always honor the
7922 location, so ensure it is set. */
7923 expression.set_location (loc);
7924 break;
7926 case ADDR_EXPR:
7927 non_constant_p = NIC_ADDR;
7928 /* Fall through. */
7929 case BIT_NOT_EXPR:
7930 expression = build_x_unary_op (loc, unary_operator,
7931 cast_expression,
7932 complain);
7933 /* TODO: build_x_unary_op does not always honor the location,
7934 so ensure it is set. */
7935 expression.set_location (loc);
7936 break;
7938 case PREINCREMENT_EXPR:
7939 case PREDECREMENT_EXPR:
7940 non_constant_p = unary_operator == PREINCREMENT_EXPR
7941 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7942 /* Fall through. */
7943 case NEGATE_EXPR:
7944 /* Immediately fold negation of a constant, unless the constant is 0
7945 (since -0 == 0) or it would overflow. */
7946 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
7947 && CONSTANT_CLASS_P (cast_expression)
7948 && !integer_zerop (cast_expression)
7949 && !TREE_OVERFLOW (cast_expression))
7951 tree folded = fold_build1 (unary_operator,
7952 TREE_TYPE (cast_expression),
7953 cast_expression);
7954 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
7956 expression = cp_expr (folded, loc);
7957 break;
7960 /* Fall through. */
7961 case UNARY_PLUS_EXPR:
7962 case TRUTH_NOT_EXPR:
7963 expression = finish_unary_op_expr (loc, unary_operator,
7964 cast_expression, complain);
7965 break;
7967 default:
7968 gcc_unreachable ();
7971 if (non_constant_p != NIC_NONE
7972 && cp_parser_non_integral_constant_expression (parser,
7973 non_constant_p))
7974 expression = error_mark_node;
7976 return expression;
7979 return cp_parser_postfix_expression (parser, address_p, cast_p,
7980 /*member_access_only_p=*/false,
7981 decltype_p,
7982 pidk);
7985 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7986 unary-operator, the corresponding tree code is returned. */
7988 static enum tree_code
7989 cp_parser_unary_operator (cp_token* token)
7991 switch (token->type)
7993 case CPP_MULT:
7994 return INDIRECT_REF;
7996 case CPP_AND:
7997 return ADDR_EXPR;
7999 case CPP_PLUS:
8000 return UNARY_PLUS_EXPR;
8002 case CPP_MINUS:
8003 return NEGATE_EXPR;
8005 case CPP_NOT:
8006 return TRUTH_NOT_EXPR;
8008 case CPP_COMPL:
8009 return BIT_NOT_EXPR;
8011 default:
8012 return ERROR_MARK;
8016 /* Parse a new-expression.
8018 new-expression:
8019 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8020 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8022 Returns a representation of the expression. */
8024 static tree
8025 cp_parser_new_expression (cp_parser* parser)
8027 bool global_scope_p;
8028 vec<tree, va_gc> *placement;
8029 tree type;
8030 vec<tree, va_gc> *initializer;
8031 tree nelts = NULL_TREE;
8032 tree ret;
8034 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8036 /* Look for the optional `::' operator. */
8037 global_scope_p
8038 = (cp_parser_global_scope_opt (parser,
8039 /*current_scope_valid_p=*/false)
8040 != NULL_TREE);
8041 /* Look for the `new' operator. */
8042 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8043 /* There's no easy way to tell a new-placement from the
8044 `( type-id )' construct. */
8045 cp_parser_parse_tentatively (parser);
8046 /* Look for a new-placement. */
8047 placement = cp_parser_new_placement (parser);
8048 /* If that didn't work out, there's no new-placement. */
8049 if (!cp_parser_parse_definitely (parser))
8051 if (placement != NULL)
8052 release_tree_vector (placement);
8053 placement = NULL;
8056 /* If the next token is a `(', then we have a parenthesized
8057 type-id. */
8058 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8060 cp_token *token;
8061 const char *saved_message = parser->type_definition_forbidden_message;
8063 /* Consume the `('. */
8064 cp_lexer_consume_token (parser->lexer);
8066 /* Parse the type-id. */
8067 parser->type_definition_forbidden_message
8068 = G_("types may not be defined in a new-expression");
8070 type_id_in_expr_sentinel s (parser);
8071 type = cp_parser_type_id (parser);
8073 parser->type_definition_forbidden_message = saved_message;
8075 /* Look for the closing `)'. */
8076 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8077 token = cp_lexer_peek_token (parser->lexer);
8078 /* There should not be a direct-new-declarator in this production,
8079 but GCC used to allowed this, so we check and emit a sensible error
8080 message for this case. */
8081 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8083 error_at (token->location,
8084 "array bound forbidden after parenthesized type-id");
8085 inform (token->location,
8086 "try removing the parentheses around the type-id");
8087 cp_parser_direct_new_declarator (parser);
8090 /* Otherwise, there must be a new-type-id. */
8091 else
8092 type = cp_parser_new_type_id (parser, &nelts);
8094 /* If the next token is a `(' or '{', then we have a new-initializer. */
8095 cp_token *token = cp_lexer_peek_token (parser->lexer);
8096 if (token->type == CPP_OPEN_PAREN
8097 || token->type == CPP_OPEN_BRACE)
8098 initializer = cp_parser_new_initializer (parser);
8099 else
8100 initializer = NULL;
8102 /* A new-expression may not appear in an integral constant
8103 expression. */
8104 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8105 ret = error_mark_node;
8106 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8107 of a new-type-id or type-id of a new-expression, the new-expression shall
8108 contain a new-initializer of the form ( assignment-expression )".
8109 Additionally, consistently with the spirit of DR 1467, we want to accept
8110 'new auto { 2 }' too. */
8111 else if (type_uses_auto (type)
8112 && (vec_safe_length (initializer) != 1
8113 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8114 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8116 error_at (token->location,
8117 "initialization of new-expression for type %<auto%> "
8118 "requires exactly one element");
8119 ret = error_mark_node;
8121 else
8123 /* Construct a location e.g.:
8124 ptr = new int[100]
8125 ^~~~~~~~~~~~
8126 with caret == start at the start of the "new" token, and the end
8127 at the end of the final token we consumed. */
8128 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8129 location_t end_loc = get_finish (end_tok->location);
8130 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8132 /* Create a representation of the new-expression. */
8133 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8134 tf_warning_or_error);
8135 protected_set_expr_location (ret, combined_loc);
8138 if (placement != NULL)
8139 release_tree_vector (placement);
8140 if (initializer != NULL)
8141 release_tree_vector (initializer);
8143 return ret;
8146 /* Parse a new-placement.
8148 new-placement:
8149 ( expression-list )
8151 Returns the same representation as for an expression-list. */
8153 static vec<tree, va_gc> *
8154 cp_parser_new_placement (cp_parser* parser)
8156 vec<tree, va_gc> *expression_list;
8158 /* Parse the expression-list. */
8159 expression_list = (cp_parser_parenthesized_expression_list
8160 (parser, non_attr, /*cast_p=*/false,
8161 /*allow_expansion_p=*/true,
8162 /*non_constant_p=*/NULL));
8164 if (expression_list && expression_list->is_empty ())
8165 error ("expected expression-list or type-id");
8167 return expression_list;
8170 /* Parse a new-type-id.
8172 new-type-id:
8173 type-specifier-seq new-declarator [opt]
8175 Returns the TYPE allocated. If the new-type-id indicates an array
8176 type, *NELTS is set to the number of elements in the last array
8177 bound; the TYPE will not include the last array bound. */
8179 static tree
8180 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8182 cp_decl_specifier_seq type_specifier_seq;
8183 cp_declarator *new_declarator;
8184 cp_declarator *declarator;
8185 cp_declarator *outer_declarator;
8186 const char *saved_message;
8188 /* The type-specifier sequence must not contain type definitions.
8189 (It cannot contain declarations of new types either, but if they
8190 are not definitions we will catch that because they are not
8191 complete.) */
8192 saved_message = parser->type_definition_forbidden_message;
8193 parser->type_definition_forbidden_message
8194 = G_("types may not be defined in a new-type-id");
8195 /* Parse the type-specifier-seq. */
8196 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8197 /*is_trailing_return=*/false,
8198 &type_specifier_seq);
8199 /* Restore the old message. */
8200 parser->type_definition_forbidden_message = saved_message;
8202 if (type_specifier_seq.type == error_mark_node)
8203 return error_mark_node;
8205 /* Parse the new-declarator. */
8206 new_declarator = cp_parser_new_declarator_opt (parser);
8208 /* Determine the number of elements in the last array dimension, if
8209 any. */
8210 *nelts = NULL_TREE;
8211 /* Skip down to the last array dimension. */
8212 declarator = new_declarator;
8213 outer_declarator = NULL;
8214 while (declarator && (declarator->kind == cdk_pointer
8215 || declarator->kind == cdk_ptrmem))
8217 outer_declarator = declarator;
8218 declarator = declarator->declarator;
8220 while (declarator
8221 && declarator->kind == cdk_array
8222 && declarator->declarator
8223 && declarator->declarator->kind == cdk_array)
8225 outer_declarator = declarator;
8226 declarator = declarator->declarator;
8229 if (declarator && declarator->kind == cdk_array)
8231 *nelts = declarator->u.array.bounds;
8232 if (*nelts == error_mark_node)
8233 *nelts = integer_one_node;
8235 if (outer_declarator)
8236 outer_declarator->declarator = declarator->declarator;
8237 else
8238 new_declarator = NULL;
8241 return groktypename (&type_specifier_seq, new_declarator, false);
8244 /* Parse an (optional) new-declarator.
8246 new-declarator:
8247 ptr-operator new-declarator [opt]
8248 direct-new-declarator
8250 Returns the declarator. */
8252 static cp_declarator *
8253 cp_parser_new_declarator_opt (cp_parser* parser)
8255 enum tree_code code;
8256 tree type, std_attributes = NULL_TREE;
8257 cp_cv_quals cv_quals;
8259 /* We don't know if there's a ptr-operator next, or not. */
8260 cp_parser_parse_tentatively (parser);
8261 /* Look for a ptr-operator. */
8262 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8263 /* If that worked, look for more new-declarators. */
8264 if (cp_parser_parse_definitely (parser))
8266 cp_declarator *declarator;
8268 /* Parse another optional declarator. */
8269 declarator = cp_parser_new_declarator_opt (parser);
8271 declarator = cp_parser_make_indirect_declarator
8272 (code, type, cv_quals, declarator, std_attributes);
8274 return declarator;
8277 /* If the next token is a `[', there is a direct-new-declarator. */
8278 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8279 return cp_parser_direct_new_declarator (parser);
8281 return NULL;
8284 /* Parse a direct-new-declarator.
8286 direct-new-declarator:
8287 [ expression ]
8288 direct-new-declarator [constant-expression]
8292 static cp_declarator *
8293 cp_parser_direct_new_declarator (cp_parser* parser)
8295 cp_declarator *declarator = NULL;
8297 while (true)
8299 tree expression;
8300 cp_token *token;
8302 /* Look for the opening `['. */
8303 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8305 token = cp_lexer_peek_token (parser->lexer);
8306 expression = cp_parser_expression (parser);
8307 /* The standard requires that the expression have integral
8308 type. DR 74 adds enumeration types. We believe that the
8309 real intent is that these expressions be handled like the
8310 expression in a `switch' condition, which also allows
8311 classes with a single conversion to integral or
8312 enumeration type. */
8313 if (!processing_template_decl)
8315 expression
8316 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8317 expression,
8318 /*complain=*/true);
8319 if (!expression)
8321 error_at (token->location,
8322 "expression in new-declarator must have integral "
8323 "or enumeration type");
8324 expression = error_mark_node;
8328 /* Look for the closing `]'. */
8329 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8331 /* Add this bound to the declarator. */
8332 declarator = make_array_declarator (declarator, expression);
8334 /* If the next token is not a `[', then there are no more
8335 bounds. */
8336 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8337 break;
8340 return declarator;
8343 /* Parse a new-initializer.
8345 new-initializer:
8346 ( expression-list [opt] )
8347 braced-init-list
8349 Returns a representation of the expression-list. */
8351 static vec<tree, va_gc> *
8352 cp_parser_new_initializer (cp_parser* parser)
8354 vec<tree, va_gc> *expression_list;
8356 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8358 tree t;
8359 bool expr_non_constant_p;
8360 cp_lexer_set_source_position (parser->lexer);
8361 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8362 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8363 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8364 expression_list = make_tree_vector_single (t);
8366 else
8367 expression_list = (cp_parser_parenthesized_expression_list
8368 (parser, non_attr, /*cast_p=*/false,
8369 /*allow_expansion_p=*/true,
8370 /*non_constant_p=*/NULL));
8372 return expression_list;
8375 /* Parse a delete-expression.
8377 delete-expression:
8378 :: [opt] delete cast-expression
8379 :: [opt] delete [ ] cast-expression
8381 Returns a representation of the expression. */
8383 static tree
8384 cp_parser_delete_expression (cp_parser* parser)
8386 bool global_scope_p;
8387 bool array_p;
8388 tree expression;
8390 /* Look for the optional `::' operator. */
8391 global_scope_p
8392 = (cp_parser_global_scope_opt (parser,
8393 /*current_scope_valid_p=*/false)
8394 != NULL_TREE);
8395 /* Look for the `delete' keyword. */
8396 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8397 /* See if the array syntax is in use. */
8398 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8400 /* Consume the `[' token. */
8401 cp_lexer_consume_token (parser->lexer);
8402 /* Look for the `]' token. */
8403 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8404 /* Remember that this is the `[]' construct. */
8405 array_p = true;
8407 else
8408 array_p = false;
8410 /* Parse the cast-expression. */
8411 expression = cp_parser_simple_cast_expression (parser);
8413 /* A delete-expression may not appear in an integral constant
8414 expression. */
8415 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8416 return error_mark_node;
8418 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8419 tf_warning_or_error);
8422 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8423 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8424 0 otherwise. */
8426 static int
8427 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8429 cp_token *token = cp_lexer_peek_token (parser->lexer);
8430 switch (token->type)
8432 case CPP_COMMA:
8433 case CPP_SEMICOLON:
8434 case CPP_QUERY:
8435 case CPP_COLON:
8436 case CPP_CLOSE_SQUARE:
8437 case CPP_CLOSE_PAREN:
8438 case CPP_CLOSE_BRACE:
8439 case CPP_OPEN_BRACE:
8440 case CPP_DOT:
8441 case CPP_DOT_STAR:
8442 case CPP_DEREF:
8443 case CPP_DEREF_STAR:
8444 case CPP_DIV:
8445 case CPP_MOD:
8446 case CPP_LSHIFT:
8447 case CPP_RSHIFT:
8448 case CPP_LESS:
8449 case CPP_GREATER:
8450 case CPP_LESS_EQ:
8451 case CPP_GREATER_EQ:
8452 case CPP_EQ_EQ:
8453 case CPP_NOT_EQ:
8454 case CPP_EQ:
8455 case CPP_MULT_EQ:
8456 case CPP_DIV_EQ:
8457 case CPP_MOD_EQ:
8458 case CPP_PLUS_EQ:
8459 case CPP_MINUS_EQ:
8460 case CPP_RSHIFT_EQ:
8461 case CPP_LSHIFT_EQ:
8462 case CPP_AND_EQ:
8463 case CPP_XOR_EQ:
8464 case CPP_OR_EQ:
8465 case CPP_XOR:
8466 case CPP_OR:
8467 case CPP_OR_OR:
8468 case CPP_EOF:
8469 case CPP_ELLIPSIS:
8470 return 0;
8472 case CPP_OPEN_PAREN:
8473 /* In ((type ()) () the last () isn't a valid cast-expression,
8474 so the whole must be parsed as postfix-expression. */
8475 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8476 != CPP_CLOSE_PAREN;
8478 case CPP_OPEN_SQUARE:
8479 /* '[' may start a primary-expression in obj-c++ and in C++11,
8480 as a lambda-expression, eg, '(void)[]{}'. */
8481 if (cxx_dialect >= cxx11)
8482 return -1;
8483 return c_dialect_objc ();
8485 case CPP_PLUS_PLUS:
8486 case CPP_MINUS_MINUS:
8487 /* '++' and '--' may or may not start a cast-expression:
8489 struct T { void operator++(int); };
8490 void f() { (T())++; }
8494 int a;
8495 (int)++a; */
8496 return -1;
8498 default:
8499 return 1;
8503 /* Parse a cast-expression.
8505 cast-expression:
8506 unary-expression
8507 ( type-id ) cast-expression
8509 ADDRESS_P is true iff the unary-expression is appearing as the
8510 operand of the `&' operator. CAST_P is true if this expression is
8511 the target of a cast.
8513 Returns a representation of the expression. */
8515 static cp_expr
8516 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8517 bool decltype_p, cp_id_kind * pidk)
8519 /* If it's a `(', then we might be looking at a cast. */
8520 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8522 tree type = NULL_TREE;
8523 cp_expr expr (NULL_TREE);
8524 int cast_expression = 0;
8525 const char *saved_message;
8527 /* There's no way to know yet whether or not this is a cast.
8528 For example, `(int (3))' is a unary-expression, while `(int)
8529 3' is a cast. So, we resort to parsing tentatively. */
8530 cp_parser_parse_tentatively (parser);
8531 /* Types may not be defined in a cast. */
8532 saved_message = parser->type_definition_forbidden_message;
8533 parser->type_definition_forbidden_message
8534 = G_("types may not be defined in casts");
8535 /* Consume the `('. */
8536 cp_token *open_paren = cp_lexer_consume_token (parser->lexer);
8537 location_t open_paren_loc = open_paren->location;
8539 /* A very tricky bit is that `(struct S) { 3 }' is a
8540 compound-literal (which we permit in C++ as an extension).
8541 But, that construct is not a cast-expression -- it is a
8542 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8543 is legal; if the compound-literal were a cast-expression,
8544 you'd need an extra set of parentheses.) But, if we parse
8545 the type-id, and it happens to be a class-specifier, then we
8546 will commit to the parse at that point, because we cannot
8547 undo the action that is done when creating a new class. So,
8548 then we cannot back up and do a postfix-expression.
8550 Another tricky case is the following (c++/29234):
8552 struct S { void operator () (); };
8554 void foo ()
8556 ( S()() );
8559 As a type-id we parse the parenthesized S()() as a function
8560 returning a function, groktypename complains and we cannot
8561 back up in this case either.
8563 Therefore, we scan ahead to the closing `)', and check to see
8564 if the tokens after the `)' can start a cast-expression. Otherwise
8565 we are dealing with an unary-expression, a postfix-expression
8566 or something else.
8568 Yet another tricky case, in C++11, is the following (c++/54891):
8570 (void)[]{};
8572 The issue is that usually, besides the case of lambda-expressions,
8573 the parenthesized type-id cannot be followed by '[', and, eg, we
8574 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8575 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8576 we don't commit, we try a cast-expression, then an unary-expression.
8578 Save tokens so that we can put them back. */
8579 cp_lexer_save_tokens (parser->lexer);
8581 /* We may be looking at a cast-expression. */
8582 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8583 /*consume_paren=*/true))
8584 cast_expression
8585 = cp_parser_tokens_start_cast_expression (parser);
8587 /* Roll back the tokens we skipped. */
8588 cp_lexer_rollback_tokens (parser->lexer);
8589 /* If we aren't looking at a cast-expression, simulate an error so
8590 that the call to cp_parser_error_occurred below returns true. */
8591 if (!cast_expression)
8592 cp_parser_simulate_error (parser);
8593 else
8595 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8596 parser->in_type_id_in_expr_p = true;
8597 /* Look for the type-id. */
8598 type = cp_parser_type_id (parser);
8599 /* Look for the closing `)'. */
8600 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8601 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8604 /* Restore the saved message. */
8605 parser->type_definition_forbidden_message = saved_message;
8607 /* At this point this can only be either a cast or a
8608 parenthesized ctor such as `(T ())' that looks like a cast to
8609 function returning T. */
8610 if (!cp_parser_error_occurred (parser))
8612 /* Only commit if the cast-expression doesn't start with
8613 '++', '--', or '[' in C++11. */
8614 if (cast_expression > 0)
8615 cp_parser_commit_to_topmost_tentative_parse (parser);
8617 expr = cp_parser_cast_expression (parser,
8618 /*address_p=*/false,
8619 /*cast_p=*/true,
8620 /*decltype_p=*/false,
8621 pidk);
8623 if (cp_parser_parse_definitely (parser))
8625 /* Warn about old-style casts, if so requested. */
8626 if (warn_old_style_cast
8627 && !in_system_header_at (input_location)
8628 && !VOID_TYPE_P (type)
8629 && current_lang_name != lang_name_c)
8630 warning (OPT_Wold_style_cast, "use of old-style cast");
8632 /* Only type conversions to integral or enumeration types
8633 can be used in constant-expressions. */
8634 if (!cast_valid_in_integral_constant_expression_p (type)
8635 && cp_parser_non_integral_constant_expression (parser,
8636 NIC_CAST))
8637 return error_mark_node;
8639 /* Perform the cast. */
8640 /* Make a location:
8641 (TYPE) EXPR
8642 ^~~~~~~~~~~
8643 with start==caret at the open paren, extending to the
8644 end of "expr". */
8645 location_t cast_loc = make_location (open_paren_loc,
8646 open_paren_loc,
8647 expr.get_finish ());
8648 expr = build_c_cast (cast_loc, type, expr);
8649 return expr;
8652 else
8653 cp_parser_abort_tentative_parse (parser);
8656 /* If we get here, then it's not a cast, so it must be a
8657 unary-expression. */
8658 return cp_parser_unary_expression (parser, pidk, address_p,
8659 cast_p, decltype_p);
8662 /* Parse a binary expression of the general form:
8664 pm-expression:
8665 cast-expression
8666 pm-expression .* cast-expression
8667 pm-expression ->* cast-expression
8669 multiplicative-expression:
8670 pm-expression
8671 multiplicative-expression * pm-expression
8672 multiplicative-expression / pm-expression
8673 multiplicative-expression % pm-expression
8675 additive-expression:
8676 multiplicative-expression
8677 additive-expression + multiplicative-expression
8678 additive-expression - multiplicative-expression
8680 shift-expression:
8681 additive-expression
8682 shift-expression << additive-expression
8683 shift-expression >> additive-expression
8685 relational-expression:
8686 shift-expression
8687 relational-expression < shift-expression
8688 relational-expression > shift-expression
8689 relational-expression <= shift-expression
8690 relational-expression >= shift-expression
8692 GNU Extension:
8694 relational-expression:
8695 relational-expression <? shift-expression
8696 relational-expression >? shift-expression
8698 equality-expression:
8699 relational-expression
8700 equality-expression == relational-expression
8701 equality-expression != relational-expression
8703 and-expression:
8704 equality-expression
8705 and-expression & equality-expression
8707 exclusive-or-expression:
8708 and-expression
8709 exclusive-or-expression ^ and-expression
8711 inclusive-or-expression:
8712 exclusive-or-expression
8713 inclusive-or-expression | exclusive-or-expression
8715 logical-and-expression:
8716 inclusive-or-expression
8717 logical-and-expression && inclusive-or-expression
8719 logical-or-expression:
8720 logical-and-expression
8721 logical-or-expression || logical-and-expression
8723 All these are implemented with a single function like:
8725 binary-expression:
8726 simple-cast-expression
8727 binary-expression <token> binary-expression
8729 CAST_P is true if this expression is the target of a cast.
8731 The binops_by_token map is used to get the tree codes for each <token> type.
8732 binary-expressions are associated according to a precedence table. */
8734 #define TOKEN_PRECEDENCE(token) \
8735 (((token->type == CPP_GREATER \
8736 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8737 && !parser->greater_than_is_operator_p) \
8738 ? PREC_NOT_OPERATOR \
8739 : binops_by_token[token->type].prec)
8741 static cp_expr
8742 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8743 bool no_toplevel_fold_p,
8744 bool decltype_p,
8745 enum cp_parser_prec prec,
8746 cp_id_kind * pidk)
8748 cp_parser_expression_stack stack;
8749 cp_parser_expression_stack_entry *sp = &stack[0];
8750 cp_parser_expression_stack_entry current;
8751 cp_expr rhs;
8752 cp_token *token;
8753 enum tree_code rhs_type;
8754 enum cp_parser_prec new_prec, lookahead_prec;
8755 tree overload;
8757 /* Parse the first expression. */
8758 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8759 ? TRUTH_NOT_EXPR : ERROR_MARK);
8760 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8761 cast_p, decltype_p, pidk);
8762 current.prec = prec;
8764 if (cp_parser_error_occurred (parser))
8765 return error_mark_node;
8767 for (;;)
8769 /* Get an operator token. */
8770 token = cp_lexer_peek_token (parser->lexer);
8772 if (warn_cxx11_compat
8773 && token->type == CPP_RSHIFT
8774 && !parser->greater_than_is_operator_p)
8776 if (warning_at (token->location, OPT_Wc__11_compat,
8777 "%<>>%> operator is treated"
8778 " as two right angle brackets in C++11"))
8779 inform (token->location,
8780 "suggest parentheses around %<>>%> expression");
8783 new_prec = TOKEN_PRECEDENCE (token);
8784 if (new_prec != PREC_NOT_OPERATOR
8785 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8786 /* This is a fold-expression; handle it later. */
8787 new_prec = PREC_NOT_OPERATOR;
8789 /* Popping an entry off the stack means we completed a subexpression:
8790 - either we found a token which is not an operator (`>' where it is not
8791 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8792 will happen repeatedly;
8793 - or, we found an operator which has lower priority. This is the case
8794 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8795 parsing `3 * 4'. */
8796 if (new_prec <= current.prec)
8798 if (sp == stack)
8799 break;
8800 else
8801 goto pop;
8804 get_rhs:
8805 current.tree_type = binops_by_token[token->type].tree_type;
8806 current.loc = token->location;
8808 /* We used the operator token. */
8809 cp_lexer_consume_token (parser->lexer);
8811 /* For "false && x" or "true || x", x will never be executed;
8812 disable warnings while evaluating it. */
8813 if (current.tree_type == TRUTH_ANDIF_EXPR)
8814 c_inhibit_evaluation_warnings +=
8815 cp_fully_fold (current.lhs) == truthvalue_false_node;
8816 else if (current.tree_type == TRUTH_ORIF_EXPR)
8817 c_inhibit_evaluation_warnings +=
8818 cp_fully_fold (current.lhs) == truthvalue_true_node;
8820 /* Extract another operand. It may be the RHS of this expression
8821 or the LHS of a new, higher priority expression. */
8822 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8823 ? TRUTH_NOT_EXPR : ERROR_MARK);
8824 rhs = cp_parser_simple_cast_expression (parser);
8826 /* Get another operator token. Look up its precedence to avoid
8827 building a useless (immediately popped) stack entry for common
8828 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8829 token = cp_lexer_peek_token (parser->lexer);
8830 lookahead_prec = TOKEN_PRECEDENCE (token);
8831 if (lookahead_prec != PREC_NOT_OPERATOR
8832 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8833 lookahead_prec = PREC_NOT_OPERATOR;
8834 if (lookahead_prec > new_prec)
8836 /* ... and prepare to parse the RHS of the new, higher priority
8837 expression. Since precedence levels on the stack are
8838 monotonically increasing, we do not have to care about
8839 stack overflows. */
8840 *sp = current;
8841 ++sp;
8842 current.lhs = rhs;
8843 current.lhs_type = rhs_type;
8844 current.prec = new_prec;
8845 new_prec = lookahead_prec;
8846 goto get_rhs;
8848 pop:
8849 lookahead_prec = new_prec;
8850 /* If the stack is not empty, we have parsed into LHS the right side
8851 (`4' in the example above) of an expression we had suspended.
8852 We can use the information on the stack to recover the LHS (`3')
8853 from the stack together with the tree code (`MULT_EXPR'), and
8854 the precedence of the higher level subexpression
8855 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8856 which will be used to actually build the additive expression. */
8857 rhs = current.lhs;
8858 rhs_type = current.lhs_type;
8859 --sp;
8860 current = *sp;
8863 /* Undo the disabling of warnings done above. */
8864 if (current.tree_type == TRUTH_ANDIF_EXPR)
8865 c_inhibit_evaluation_warnings -=
8866 cp_fully_fold (current.lhs) == truthvalue_false_node;
8867 else if (current.tree_type == TRUTH_ORIF_EXPR)
8868 c_inhibit_evaluation_warnings -=
8869 cp_fully_fold (current.lhs) == truthvalue_true_node;
8871 if (warn_logical_not_paren
8872 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
8873 && current.lhs_type == TRUTH_NOT_EXPR
8874 /* Avoid warning for !!x == y. */
8875 && (TREE_CODE (current.lhs) != NE_EXPR
8876 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
8877 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
8878 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
8879 /* Avoid warning for !b == y where b is boolean. */
8880 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
8881 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
8882 != BOOLEAN_TYPE))))
8883 /* Avoid warning for !!b == y where b is boolean. */
8884 && (!DECL_P (current.lhs)
8885 || TREE_TYPE (current.lhs) == NULL_TREE
8886 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
8887 warn_logical_not_parentheses (current.loc, current.tree_type,
8888 maybe_constant_value (rhs));
8890 overload = NULL;
8892 location_t combined_loc = make_location (current.loc,
8893 current.lhs.get_start (),
8894 rhs.get_finish ());
8896 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8897 ERROR_MARK for everything that is not a binary expression.
8898 This makes warn_about_parentheses miss some warnings that
8899 involve unary operators. For unary expressions we should
8900 pass the correct tree_code unless the unary expression was
8901 surrounded by parentheses.
8903 if (no_toplevel_fold_p
8904 && lookahead_prec <= current.prec
8905 && sp == stack)
8906 current.lhs = build2_loc (combined_loc,
8907 current.tree_type,
8908 TREE_CODE_CLASS (current.tree_type)
8909 == tcc_comparison
8910 ? boolean_type_node : TREE_TYPE (current.lhs),
8911 current.lhs, rhs);
8912 else
8914 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
8915 current.lhs, current.lhs_type,
8916 rhs, rhs_type, &overload,
8917 complain_flags (decltype_p));
8918 /* TODO: build_x_binary_op doesn't always honor the location. */
8919 current.lhs.set_location (combined_loc);
8921 current.lhs_type = current.tree_type;
8923 /* If the binary operator required the use of an overloaded operator,
8924 then this expression cannot be an integral constant-expression.
8925 An overloaded operator can be used even if both operands are
8926 otherwise permissible in an integral constant-expression if at
8927 least one of the operands is of enumeration type. */
8929 if (overload
8930 && cp_parser_non_integral_constant_expression (parser,
8931 NIC_OVERLOADED))
8932 return error_mark_node;
8935 return current.lhs;
8938 static cp_expr
8939 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8940 bool no_toplevel_fold_p,
8941 enum cp_parser_prec prec,
8942 cp_id_kind * pidk)
8944 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8945 /*decltype*/false, prec, pidk);
8948 /* Parse the `? expression : assignment-expression' part of a
8949 conditional-expression. The LOGICAL_OR_EXPR is the
8950 logical-or-expression that started the conditional-expression.
8951 Returns a representation of the entire conditional-expression.
8953 This routine is used by cp_parser_assignment_expression.
8955 ? expression : assignment-expression
8957 GNU Extensions:
8959 ? : assignment-expression */
8961 static tree
8962 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
8964 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
8965 cp_expr assignment_expr;
8966 struct cp_token *token;
8967 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8969 /* Consume the `?' token. */
8970 cp_lexer_consume_token (parser->lexer);
8971 token = cp_lexer_peek_token (parser->lexer);
8972 if (cp_parser_allow_gnu_extensions_p (parser)
8973 && token->type == CPP_COLON)
8975 pedwarn (token->location, OPT_Wpedantic,
8976 "ISO C++ does not allow ?: with omitted middle operand");
8977 /* Implicit true clause. */
8978 expr = NULL_TREE;
8979 c_inhibit_evaluation_warnings +=
8980 folded_logical_or_expr == truthvalue_true_node;
8981 warn_for_omitted_condop (token->location, logical_or_expr);
8983 else
8985 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8986 parser->colon_corrects_to_scope_p = false;
8987 /* Parse the expression. */
8988 c_inhibit_evaluation_warnings +=
8989 folded_logical_or_expr == truthvalue_false_node;
8990 expr = cp_parser_expression (parser);
8991 c_inhibit_evaluation_warnings +=
8992 ((folded_logical_or_expr == truthvalue_true_node)
8993 - (folded_logical_or_expr == truthvalue_false_node));
8994 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8997 /* The next token should be a `:'. */
8998 cp_parser_require (parser, CPP_COLON, RT_COLON);
8999 /* Parse the assignment-expression. */
9000 assignment_expr = cp_parser_assignment_expression (parser);
9001 c_inhibit_evaluation_warnings -=
9002 folded_logical_or_expr == truthvalue_true_node;
9004 /* Make a location:
9005 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9006 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9007 with the caret at the "?", ranging from the start of
9008 the logical_or_expr to the end of the assignment_expr. */
9009 loc = make_location (loc,
9010 logical_or_expr.get_start (),
9011 assignment_expr.get_finish ());
9013 /* Build the conditional-expression. */
9014 return build_x_conditional_expr (loc, logical_or_expr,
9015 expr,
9016 assignment_expr,
9017 tf_warning_or_error);
9020 /* Parse an assignment-expression.
9022 assignment-expression:
9023 conditional-expression
9024 logical-or-expression assignment-operator assignment_expression
9025 throw-expression
9027 CAST_P is true if this expression is the target of a cast.
9028 DECLTYPE_P is true if this expression is the operand of decltype.
9030 Returns a representation for the expression. */
9032 static cp_expr
9033 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9034 bool cast_p, bool decltype_p)
9036 cp_expr expr;
9038 /* If the next token is the `throw' keyword, then we're looking at
9039 a throw-expression. */
9040 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9041 expr = cp_parser_throw_expression (parser);
9042 /* Otherwise, it must be that we are looking at a
9043 logical-or-expression. */
9044 else
9046 /* Parse the binary expressions (logical-or-expression). */
9047 expr = cp_parser_binary_expression (parser, cast_p, false,
9048 decltype_p,
9049 PREC_NOT_OPERATOR, pidk);
9050 /* If the next token is a `?' then we're actually looking at a
9051 conditional-expression. */
9052 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9053 return cp_parser_question_colon_clause (parser, expr);
9054 else
9056 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9058 /* If it's an assignment-operator, we're using the second
9059 production. */
9060 enum tree_code assignment_operator
9061 = cp_parser_assignment_operator_opt (parser);
9062 if (assignment_operator != ERROR_MARK)
9064 bool non_constant_p;
9066 /* Parse the right-hand side of the assignment. */
9067 cp_expr rhs = cp_parser_initializer_clause (parser,
9068 &non_constant_p);
9070 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9071 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9073 /* An assignment may not appear in a
9074 constant-expression. */
9075 if (cp_parser_non_integral_constant_expression (parser,
9076 NIC_ASSIGNMENT))
9077 return error_mark_node;
9078 /* Build the assignment expression. Its default
9079 location:
9080 LHS = RHS
9081 ~~~~^~~~~
9082 is the location of the '=' token as the
9083 caret, ranging from the start of the lhs to the
9084 end of the rhs. */
9085 loc = make_location (loc,
9086 expr.get_start (),
9087 rhs.get_finish ());
9088 expr = build_x_modify_expr (loc, expr,
9089 assignment_operator,
9090 rhs,
9091 complain_flags (decltype_p));
9092 /* TODO: build_x_modify_expr doesn't honor the location,
9093 so we must set it here. */
9094 expr.set_location (loc);
9099 return expr;
9102 /* Parse an (optional) assignment-operator.
9104 assignment-operator: one of
9105 = *= /= %= += -= >>= <<= &= ^= |=
9107 GNU Extension:
9109 assignment-operator: one of
9110 <?= >?=
9112 If the next token is an assignment operator, the corresponding tree
9113 code is returned, and the token is consumed. For example, for
9114 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9115 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9116 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9117 operator, ERROR_MARK is returned. */
9119 static enum tree_code
9120 cp_parser_assignment_operator_opt (cp_parser* parser)
9122 enum tree_code op;
9123 cp_token *token;
9125 /* Peek at the next token. */
9126 token = cp_lexer_peek_token (parser->lexer);
9128 switch (token->type)
9130 case CPP_EQ:
9131 op = NOP_EXPR;
9132 break;
9134 case CPP_MULT_EQ:
9135 op = MULT_EXPR;
9136 break;
9138 case CPP_DIV_EQ:
9139 op = TRUNC_DIV_EXPR;
9140 break;
9142 case CPP_MOD_EQ:
9143 op = TRUNC_MOD_EXPR;
9144 break;
9146 case CPP_PLUS_EQ:
9147 op = PLUS_EXPR;
9148 break;
9150 case CPP_MINUS_EQ:
9151 op = MINUS_EXPR;
9152 break;
9154 case CPP_RSHIFT_EQ:
9155 op = RSHIFT_EXPR;
9156 break;
9158 case CPP_LSHIFT_EQ:
9159 op = LSHIFT_EXPR;
9160 break;
9162 case CPP_AND_EQ:
9163 op = BIT_AND_EXPR;
9164 break;
9166 case CPP_XOR_EQ:
9167 op = BIT_XOR_EXPR;
9168 break;
9170 case CPP_OR_EQ:
9171 op = BIT_IOR_EXPR;
9172 break;
9174 default:
9175 /* Nothing else is an assignment operator. */
9176 op = ERROR_MARK;
9179 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9180 if (op != ERROR_MARK
9181 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9182 op = ERROR_MARK;
9184 /* If it was an assignment operator, consume it. */
9185 if (op != ERROR_MARK)
9186 cp_lexer_consume_token (parser->lexer);
9188 return op;
9191 /* Parse an expression.
9193 expression:
9194 assignment-expression
9195 expression , assignment-expression
9197 CAST_P is true if this expression is the target of a cast.
9198 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9199 except possibly parenthesized or on the RHS of a comma (N3276).
9201 Returns a representation of the expression. */
9203 static cp_expr
9204 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9205 bool cast_p, bool decltype_p)
9207 cp_expr expression = NULL_TREE;
9208 location_t loc = UNKNOWN_LOCATION;
9210 while (true)
9212 cp_expr assignment_expression;
9214 /* Parse the next assignment-expression. */
9215 assignment_expression
9216 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9218 /* We don't create a temporary for a call that is the immediate operand
9219 of decltype or on the RHS of a comma. But when we see a comma, we
9220 need to create a temporary for a call on the LHS. */
9221 if (decltype_p && !processing_template_decl
9222 && TREE_CODE (assignment_expression) == CALL_EXPR
9223 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9224 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9225 assignment_expression
9226 = build_cplus_new (TREE_TYPE (assignment_expression),
9227 assignment_expression, tf_warning_or_error);
9229 /* If this is the first assignment-expression, we can just
9230 save it away. */
9231 if (!expression)
9232 expression = assignment_expression;
9233 else
9235 /* Create a location with caret at the comma, ranging
9236 from the start of the LHS to the end of the RHS. */
9237 loc = make_location (loc,
9238 expression.get_start (),
9239 assignment_expression.get_finish ());
9240 expression = build_x_compound_expr (loc, expression,
9241 assignment_expression,
9242 complain_flags (decltype_p));
9243 expression.set_location (loc);
9245 /* If the next token is not a comma, or we're in a fold-expression, then
9246 we are done with the expression. */
9247 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9248 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9249 break;
9250 /* Consume the `,'. */
9251 loc = cp_lexer_peek_token (parser->lexer)->location;
9252 cp_lexer_consume_token (parser->lexer);
9253 /* A comma operator cannot appear in a constant-expression. */
9254 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9255 expression = error_mark_node;
9258 return expression;
9261 /* Parse a constant-expression.
9263 constant-expression:
9264 conditional-expression
9266 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9267 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9268 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9269 is false, NON_CONSTANT_P should be NULL. */
9271 static cp_expr
9272 cp_parser_constant_expression (cp_parser* parser,
9273 bool allow_non_constant_p,
9274 bool *non_constant_p)
9276 bool saved_integral_constant_expression_p;
9277 bool saved_allow_non_integral_constant_expression_p;
9278 bool saved_non_integral_constant_expression_p;
9279 cp_expr expression;
9281 /* It might seem that we could simply parse the
9282 conditional-expression, and then check to see if it were
9283 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9284 one that the compiler can figure out is constant, possibly after
9285 doing some simplifications or optimizations. The standard has a
9286 precise definition of constant-expression, and we must honor
9287 that, even though it is somewhat more restrictive.
9289 For example:
9291 int i[(2, 3)];
9293 is not a legal declaration, because `(2, 3)' is not a
9294 constant-expression. The `,' operator is forbidden in a
9295 constant-expression. However, GCC's constant-folding machinery
9296 will fold this operation to an INTEGER_CST for `3'. */
9298 /* Save the old settings. */
9299 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9300 saved_allow_non_integral_constant_expression_p
9301 = parser->allow_non_integral_constant_expression_p;
9302 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9303 /* We are now parsing a constant-expression. */
9304 parser->integral_constant_expression_p = true;
9305 parser->allow_non_integral_constant_expression_p
9306 = (allow_non_constant_p || cxx_dialect >= cxx11);
9307 parser->non_integral_constant_expression_p = false;
9308 /* Although the grammar says "conditional-expression", we parse an
9309 "assignment-expression", which also permits "throw-expression"
9310 and the use of assignment operators. In the case that
9311 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9312 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9313 actually essential that we look for an assignment-expression.
9314 For example, cp_parser_initializer_clauses uses this function to
9315 determine whether a particular assignment-expression is in fact
9316 constant. */
9317 expression = cp_parser_assignment_expression (parser);
9318 /* Restore the old settings. */
9319 parser->integral_constant_expression_p
9320 = saved_integral_constant_expression_p;
9321 parser->allow_non_integral_constant_expression_p
9322 = saved_allow_non_integral_constant_expression_p;
9323 if (cxx_dialect >= cxx11)
9325 /* Require an rvalue constant expression here; that's what our
9326 callers expect. Reference constant expressions are handled
9327 separately in e.g. cp_parser_template_argument. */
9328 bool is_const = potential_rvalue_constant_expression (expression);
9329 parser->non_integral_constant_expression_p = !is_const;
9330 if (!is_const && !allow_non_constant_p)
9331 require_potential_rvalue_constant_expression (expression);
9333 if (allow_non_constant_p)
9334 *non_constant_p = parser->non_integral_constant_expression_p;
9335 parser->non_integral_constant_expression_p
9336 = saved_non_integral_constant_expression_p;
9338 return expression;
9341 /* Parse __builtin_offsetof.
9343 offsetof-expression:
9344 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9346 offsetof-member-designator:
9347 id-expression
9348 | offsetof-member-designator "." id-expression
9349 | offsetof-member-designator "[" expression "]"
9350 | offsetof-member-designator "->" id-expression */
9352 static cp_expr
9353 cp_parser_builtin_offsetof (cp_parser *parser)
9355 int save_ice_p, save_non_ice_p;
9356 tree type;
9357 cp_expr expr;
9358 cp_id_kind dummy;
9359 cp_token *token;
9360 location_t finish_loc;
9362 /* We're about to accept non-integral-constant things, but will
9363 definitely yield an integral constant expression. Save and
9364 restore these values around our local parsing. */
9365 save_ice_p = parser->integral_constant_expression_p;
9366 save_non_ice_p = parser->non_integral_constant_expression_p;
9368 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9370 /* Consume the "__builtin_offsetof" token. */
9371 cp_lexer_consume_token (parser->lexer);
9372 /* Consume the opening `('. */
9373 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9374 /* Parse the type-id. */
9375 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9376 type = cp_parser_type_id (parser);
9377 /* Look for the `,'. */
9378 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9379 token = cp_lexer_peek_token (parser->lexer);
9381 /* Build the (type *)null that begins the traditional offsetof macro. */
9382 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
9383 tf_warning_or_error);
9385 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9386 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
9387 true, &dummy, token->location);
9388 while (true)
9390 token = cp_lexer_peek_token (parser->lexer);
9391 switch (token->type)
9393 case CPP_OPEN_SQUARE:
9394 /* offsetof-member-designator "[" expression "]" */
9395 expr = cp_parser_postfix_open_square_expression (parser, expr,
9396 true, false);
9397 break;
9399 case CPP_DEREF:
9400 /* offsetof-member-designator "->" identifier */
9401 expr = grok_array_decl (token->location, expr,
9402 integer_zero_node, false);
9403 /* FALLTHRU */
9405 case CPP_DOT:
9406 /* offsetof-member-designator "." identifier */
9407 cp_lexer_consume_token (parser->lexer);
9408 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9409 expr, true, &dummy,
9410 token->location);
9411 break;
9413 case CPP_CLOSE_PAREN:
9414 /* Consume the ")" token. */
9415 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9416 cp_lexer_consume_token (parser->lexer);
9417 goto success;
9419 default:
9420 /* Error. We know the following require will fail, but
9421 that gives the proper error message. */
9422 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9423 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9424 expr = error_mark_node;
9425 goto failure;
9429 success:
9430 /* Make a location of the form:
9431 __builtin_offsetof (struct s, f)
9432 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9433 with caret at the type-id, ranging from the start of the
9434 "_builtin_offsetof" token to the close paren. */
9435 loc = make_location (loc, start_loc, finish_loc);
9436 /* The result will be an INTEGER_CST, so we need to explicitly
9437 preserve the location. */
9438 expr = cp_expr (finish_offsetof (expr, loc), loc);
9440 failure:
9441 parser->integral_constant_expression_p = save_ice_p;
9442 parser->non_integral_constant_expression_p = save_non_ice_p;
9444 return expr;
9447 /* Parse a trait expression.
9449 Returns a representation of the expression, the underlying type
9450 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9452 static tree
9453 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9455 cp_trait_kind kind;
9456 tree type1, type2 = NULL_TREE;
9457 bool binary = false;
9458 bool variadic = false;
9460 switch (keyword)
9462 case RID_HAS_NOTHROW_ASSIGN:
9463 kind = CPTK_HAS_NOTHROW_ASSIGN;
9464 break;
9465 case RID_HAS_NOTHROW_CONSTRUCTOR:
9466 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9467 break;
9468 case RID_HAS_NOTHROW_COPY:
9469 kind = CPTK_HAS_NOTHROW_COPY;
9470 break;
9471 case RID_HAS_TRIVIAL_ASSIGN:
9472 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9473 break;
9474 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9475 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9476 break;
9477 case RID_HAS_TRIVIAL_COPY:
9478 kind = CPTK_HAS_TRIVIAL_COPY;
9479 break;
9480 case RID_HAS_TRIVIAL_DESTRUCTOR:
9481 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9482 break;
9483 case RID_HAS_VIRTUAL_DESTRUCTOR:
9484 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9485 break;
9486 case RID_IS_ABSTRACT:
9487 kind = CPTK_IS_ABSTRACT;
9488 break;
9489 case RID_IS_BASE_OF:
9490 kind = CPTK_IS_BASE_OF;
9491 binary = true;
9492 break;
9493 case RID_IS_CLASS:
9494 kind = CPTK_IS_CLASS;
9495 break;
9496 case RID_IS_EMPTY:
9497 kind = CPTK_IS_EMPTY;
9498 break;
9499 case RID_IS_ENUM:
9500 kind = CPTK_IS_ENUM;
9501 break;
9502 case RID_IS_FINAL:
9503 kind = CPTK_IS_FINAL;
9504 break;
9505 case RID_IS_LITERAL_TYPE:
9506 kind = CPTK_IS_LITERAL_TYPE;
9507 break;
9508 case RID_IS_POD:
9509 kind = CPTK_IS_POD;
9510 break;
9511 case RID_IS_POLYMORPHIC:
9512 kind = CPTK_IS_POLYMORPHIC;
9513 break;
9514 case RID_IS_SAME_AS:
9515 kind = CPTK_IS_SAME_AS;
9516 binary = true;
9517 break;
9518 case RID_IS_STD_LAYOUT:
9519 kind = CPTK_IS_STD_LAYOUT;
9520 break;
9521 case RID_IS_TRIVIAL:
9522 kind = CPTK_IS_TRIVIAL;
9523 break;
9524 case RID_IS_TRIVIALLY_ASSIGNABLE:
9525 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9526 binary = true;
9527 break;
9528 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9529 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9530 variadic = true;
9531 break;
9532 case RID_IS_TRIVIALLY_COPYABLE:
9533 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9534 break;
9535 case RID_IS_UNION:
9536 kind = CPTK_IS_UNION;
9537 break;
9538 case RID_UNDERLYING_TYPE:
9539 kind = CPTK_UNDERLYING_TYPE;
9540 break;
9541 case RID_BASES:
9542 kind = CPTK_BASES;
9543 break;
9544 case RID_DIRECT_BASES:
9545 kind = CPTK_DIRECT_BASES;
9546 break;
9547 default:
9548 gcc_unreachable ();
9551 /* Consume the token. */
9552 cp_lexer_consume_token (parser->lexer);
9554 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9557 type_id_in_expr_sentinel s (parser);
9558 type1 = cp_parser_type_id (parser);
9561 if (type1 == error_mark_node)
9562 return error_mark_node;
9564 if (binary)
9566 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9569 type_id_in_expr_sentinel s (parser);
9570 type2 = cp_parser_type_id (parser);
9573 if (type2 == error_mark_node)
9574 return error_mark_node;
9576 else if (variadic)
9578 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9580 cp_lexer_consume_token (parser->lexer);
9581 tree elt = cp_parser_type_id (parser);
9582 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9584 cp_lexer_consume_token (parser->lexer);
9585 elt = make_pack_expansion (elt);
9587 if (elt == error_mark_node)
9588 return error_mark_node;
9589 type2 = tree_cons (NULL_TREE, elt, type2);
9593 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9595 /* Complete the trait expression, which may mean either processing
9596 the trait expr now or saving it for template instantiation. */
9597 switch(kind)
9599 case CPTK_UNDERLYING_TYPE:
9600 return finish_underlying_type (type1);
9601 case CPTK_BASES:
9602 return finish_bases (type1, false);
9603 case CPTK_DIRECT_BASES:
9604 return finish_bases (type1, true);
9605 default:
9606 return finish_trait_expr (kind, type1, type2);
9610 /* Lambdas that appear in variable initializer or default argument scope
9611 get that in their mangling, so we need to record it. We might as well
9612 use the count for function and namespace scopes as well. */
9613 static GTY(()) tree lambda_scope;
9614 static GTY(()) int lambda_count;
9615 struct GTY(()) tree_int
9617 tree t;
9618 int i;
9620 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
9622 static void
9623 start_lambda_scope (tree decl)
9625 tree_int ti;
9626 gcc_assert (decl);
9627 /* Once we're inside a function, we ignore other scopes and just push
9628 the function again so that popping works properly. */
9629 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
9630 decl = current_function_decl;
9631 ti.t = lambda_scope;
9632 ti.i = lambda_count;
9633 vec_safe_push (lambda_scope_stack, ti);
9634 if (lambda_scope != decl)
9636 /* Don't reset the count if we're still in the same function. */
9637 lambda_scope = decl;
9638 lambda_count = 0;
9642 static void
9643 record_lambda_scope (tree lambda)
9645 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
9646 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
9649 static void
9650 finish_lambda_scope (void)
9652 tree_int *p = &lambda_scope_stack->last ();
9653 if (lambda_scope != p->t)
9655 lambda_scope = p->t;
9656 lambda_count = p->i;
9658 lambda_scope_stack->pop ();
9661 /* Parse a lambda expression.
9663 lambda-expression:
9664 lambda-introducer lambda-declarator [opt] compound-statement
9666 Returns a representation of the expression. */
9668 static cp_expr
9669 cp_parser_lambda_expression (cp_parser* parser)
9671 tree lambda_expr = build_lambda_expr ();
9672 tree type;
9673 bool ok = true;
9674 cp_token *token = cp_lexer_peek_token (parser->lexer);
9675 cp_token_position start = 0;
9677 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9679 if (cp_unevaluated_operand)
9681 if (!token->error_reported)
9683 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9684 "lambda-expression in unevaluated context");
9685 token->error_reported = true;
9687 ok = false;
9689 else if (parser->in_template_argument_list_p)
9691 if (!token->error_reported)
9693 error_at (token->location, "lambda-expression in template-argument");
9694 token->error_reported = true;
9696 ok = false;
9699 /* We may be in the middle of deferred access check. Disable
9700 it now. */
9701 push_deferring_access_checks (dk_no_deferred);
9703 cp_parser_lambda_introducer (parser, lambda_expr);
9705 type = begin_lambda_type (lambda_expr);
9706 if (type == error_mark_node)
9707 return error_mark_node;
9709 record_lambda_scope (lambda_expr);
9711 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9712 determine_visibility (TYPE_NAME (type));
9714 /* Now that we've started the type, add the capture fields for any
9715 explicit captures. */
9716 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9719 /* Inside the class, surrounding template-parameter-lists do not apply. */
9720 unsigned int saved_num_template_parameter_lists
9721 = parser->num_template_parameter_lists;
9722 unsigned char in_statement = parser->in_statement;
9723 bool in_switch_statement_p = parser->in_switch_statement_p;
9724 bool fully_implicit_function_template_p
9725 = parser->fully_implicit_function_template_p;
9726 tree implicit_template_parms = parser->implicit_template_parms;
9727 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9728 bool auto_is_implicit_function_template_parm_p
9729 = parser->auto_is_implicit_function_template_parm_p;
9731 parser->num_template_parameter_lists = 0;
9732 parser->in_statement = 0;
9733 parser->in_switch_statement_p = false;
9734 parser->fully_implicit_function_template_p = false;
9735 parser->implicit_template_parms = 0;
9736 parser->implicit_template_scope = 0;
9737 parser->auto_is_implicit_function_template_parm_p = false;
9739 /* By virtue of defining a local class, a lambda expression has access to
9740 the private variables of enclosing classes. */
9742 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9744 if (ok)
9746 if (!cp_parser_error_occurred (parser)
9747 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9748 && cp_parser_start_tentative_firewall (parser))
9749 start = token;
9750 cp_parser_lambda_body (parser, lambda_expr);
9752 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9754 if (cp_parser_skip_to_closing_brace (parser))
9755 cp_lexer_consume_token (parser->lexer);
9758 /* The capture list was built up in reverse order; fix that now. */
9759 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9760 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9762 if (ok)
9763 maybe_add_lambda_conv_op (type);
9765 type = finish_struct (type, /*attributes=*/NULL_TREE);
9767 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9768 parser->in_statement = in_statement;
9769 parser->in_switch_statement_p = in_switch_statement_p;
9770 parser->fully_implicit_function_template_p
9771 = fully_implicit_function_template_p;
9772 parser->implicit_template_parms = implicit_template_parms;
9773 parser->implicit_template_scope = implicit_template_scope;
9774 parser->auto_is_implicit_function_template_parm_p
9775 = auto_is_implicit_function_template_parm_p;
9778 pop_deferring_access_checks ();
9780 /* This field is only used during parsing of the lambda. */
9781 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9783 /* This lambda shouldn't have any proxies left at this point. */
9784 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9785 /* And now that we're done, push proxies for an enclosing lambda. */
9786 insert_pending_capture_proxies ();
9788 if (ok)
9789 lambda_expr = build_lambda_object (lambda_expr);
9790 else
9791 lambda_expr = error_mark_node;
9793 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9795 return lambda_expr;
9798 /* Parse the beginning of a lambda expression.
9800 lambda-introducer:
9801 [ lambda-capture [opt] ]
9803 LAMBDA_EXPR is the current representation of the lambda expression. */
9805 static void
9806 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9808 /* Need commas after the first capture. */
9809 bool first = true;
9811 /* Eat the leading `['. */
9812 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9814 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9815 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9816 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9817 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9818 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9819 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9821 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9823 cp_lexer_consume_token (parser->lexer);
9824 first = false;
9827 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9829 cp_token* capture_token;
9830 tree capture_id;
9831 tree capture_init_expr;
9832 cp_id_kind idk = CP_ID_KIND_NONE;
9833 bool explicit_init_p = false;
9835 enum capture_kind_type
9837 BY_COPY,
9838 BY_REFERENCE
9840 enum capture_kind_type capture_kind = BY_COPY;
9842 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9844 error ("expected end of capture-list");
9845 return;
9848 if (first)
9849 first = false;
9850 else
9851 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9853 /* Possibly capture `this'. */
9854 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9856 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9857 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9858 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9859 "with by-copy capture default");
9860 cp_lexer_consume_token (parser->lexer);
9861 add_capture (lambda_expr,
9862 /*id=*/this_identifier,
9863 /*initializer=*/finish_this_expr(),
9864 /*by_reference_p=*/false,
9865 explicit_init_p);
9866 continue;
9869 /* Remember whether we want to capture as a reference or not. */
9870 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9872 capture_kind = BY_REFERENCE;
9873 cp_lexer_consume_token (parser->lexer);
9876 /* Get the identifier. */
9877 capture_token = cp_lexer_peek_token (parser->lexer);
9878 capture_id = cp_parser_identifier (parser);
9880 if (capture_id == error_mark_node)
9881 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9882 delimiters, but I modified this to stop on unnested ']' as well. It
9883 was already changed to stop on unnested '}', so the
9884 "closing_parenthesis" name is no more misleading with my change. */
9886 cp_parser_skip_to_closing_parenthesis (parser,
9887 /*recovering=*/true,
9888 /*or_comma=*/true,
9889 /*consume_paren=*/true);
9890 break;
9893 /* Find the initializer for this capture. */
9894 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9895 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9896 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9898 bool direct, non_constant;
9899 /* An explicit initializer exists. */
9900 if (cxx_dialect < cxx14)
9901 pedwarn (input_location, 0,
9902 "lambda capture initializers "
9903 "only available with -std=c++14 or -std=gnu++14");
9904 capture_init_expr = cp_parser_initializer (parser, &direct,
9905 &non_constant);
9906 explicit_init_p = true;
9907 if (capture_init_expr == NULL_TREE)
9909 error ("empty initializer for lambda init-capture");
9910 capture_init_expr = error_mark_node;
9913 else
9915 const char* error_msg;
9917 /* Turn the identifier into an id-expression. */
9918 capture_init_expr
9919 = cp_parser_lookup_name_simple (parser, capture_id,
9920 capture_token->location);
9922 if (capture_init_expr == error_mark_node)
9924 unqualified_name_lookup_error (capture_id);
9925 continue;
9927 else if (DECL_P (capture_init_expr)
9928 && (!VAR_P (capture_init_expr)
9929 && TREE_CODE (capture_init_expr) != PARM_DECL))
9931 error_at (capture_token->location,
9932 "capture of non-variable %qD ",
9933 capture_init_expr);
9934 inform (DECL_SOURCE_LOCATION (capture_init_expr),
9935 "%q#D declared here", capture_init_expr);
9936 continue;
9938 if (VAR_P (capture_init_expr)
9939 && decl_storage_duration (capture_init_expr) != dk_auto)
9941 if (pedwarn (capture_token->location, 0, "capture of variable "
9942 "%qD with non-automatic storage duration",
9943 capture_init_expr))
9944 inform (DECL_SOURCE_LOCATION (capture_init_expr),
9945 "%q#D declared here", capture_init_expr);
9946 continue;
9949 capture_init_expr
9950 = finish_id_expression
9951 (capture_id,
9952 capture_init_expr,
9953 parser->scope,
9954 &idk,
9955 /*integral_constant_expression_p=*/false,
9956 /*allow_non_integral_constant_expression_p=*/false,
9957 /*non_integral_constant_expression_p=*/NULL,
9958 /*template_p=*/false,
9959 /*done=*/true,
9960 /*address_p=*/false,
9961 /*template_arg_p=*/false,
9962 &error_msg,
9963 capture_token->location);
9965 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9967 cp_lexer_consume_token (parser->lexer);
9968 capture_init_expr = make_pack_expansion (capture_init_expr);
9970 else
9971 check_for_bare_parameter_packs (capture_init_expr);
9974 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9975 && !explicit_init_p)
9977 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9978 && capture_kind == BY_COPY)
9979 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9980 "of %qD redundant with by-copy capture default",
9981 capture_id);
9982 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9983 && capture_kind == BY_REFERENCE)
9984 pedwarn (capture_token->location, 0, "explicit by-reference "
9985 "capture of %qD redundant with by-reference capture "
9986 "default", capture_id);
9989 add_capture (lambda_expr,
9990 capture_id,
9991 capture_init_expr,
9992 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9993 explicit_init_p);
9996 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9999 /* Parse the (optional) middle of a lambda expression.
10001 lambda-declarator:
10002 < template-parameter-list [opt] >
10003 ( parameter-declaration-clause [opt] )
10004 attribute-specifier [opt]
10005 mutable [opt]
10006 exception-specification [opt]
10007 lambda-return-type-clause [opt]
10009 LAMBDA_EXPR is the current representation of the lambda expression. */
10011 static bool
10012 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10014 /* 5.1.1.4 of the standard says:
10015 If a lambda-expression does not include a lambda-declarator, it is as if
10016 the lambda-declarator were ().
10017 This means an empty parameter list, no attributes, and no exception
10018 specification. */
10019 tree param_list = void_list_node;
10020 tree attributes = NULL_TREE;
10021 tree exception_spec = NULL_TREE;
10022 tree template_param_list = NULL_TREE;
10023 tree tx_qual = NULL_TREE;
10025 /* The template-parameter-list is optional, but must begin with
10026 an opening angle if present. */
10027 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10029 if (cxx_dialect < cxx14)
10030 pedwarn (parser->lexer->next_token->location, 0,
10031 "lambda templates are only available with "
10032 "-std=c++14 or -std=gnu++14");
10034 cp_lexer_consume_token (parser->lexer);
10036 template_param_list = cp_parser_template_parameter_list (parser);
10038 cp_parser_skip_to_end_of_template_parameter_list (parser);
10040 /* We just processed one more parameter list. */
10041 ++parser->num_template_parameter_lists;
10044 /* The parameter-declaration-clause is optional (unless
10045 template-parameter-list was given), but must begin with an
10046 opening parenthesis if present. */
10047 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10049 cp_lexer_consume_token (parser->lexer);
10051 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10053 /* Parse parameters. */
10054 param_list = cp_parser_parameter_declaration_clause (parser);
10056 /* Default arguments shall not be specified in the
10057 parameter-declaration-clause of a lambda-declarator. */
10058 for (tree t = param_list; t; t = TREE_CHAIN (t))
10059 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
10060 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10061 "default argument specified for lambda parameter");
10063 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10065 attributes = cp_parser_attributes_opt (parser);
10067 /* Parse optional `mutable' keyword. */
10068 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
10070 cp_lexer_consume_token (parser->lexer);
10071 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10074 tx_qual = cp_parser_tx_qualifier_opt (parser);
10076 /* Parse optional exception specification. */
10077 exception_spec = cp_parser_exception_specification_opt (parser);
10079 /* Parse optional trailing return type. */
10080 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10082 cp_lexer_consume_token (parser->lexer);
10083 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10084 = cp_parser_trailing_type_id (parser);
10087 /* The function parameters must be in scope all the way until after the
10088 trailing-return-type in case of decltype. */
10089 pop_bindings_and_leave_scope ();
10091 else if (template_param_list != NULL_TREE) // generate diagnostic
10092 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10094 /* Create the function call operator.
10096 Messing with declarators like this is no uglier than building up the
10097 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10098 other code. */
10100 cp_decl_specifier_seq return_type_specs;
10101 cp_declarator* declarator;
10102 tree fco;
10103 int quals;
10104 void *p;
10106 clear_decl_specs (&return_type_specs);
10107 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10108 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
10109 else
10110 /* Maybe we will deduce the return type later. */
10111 return_type_specs.type = make_auto ();
10113 p = obstack_alloc (&declarator_obstack, 0);
10115 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
10116 sfk_none);
10118 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10119 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10120 declarator = make_call_declarator (declarator, param_list, quals,
10121 VIRT_SPEC_UNSPECIFIED,
10122 REF_QUAL_NONE,
10123 tx_qual,
10124 exception_spec,
10125 /*late_return_type=*/NULL_TREE,
10126 /*requires_clause*/NULL_TREE);
10127 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10129 fco = grokmethod (&return_type_specs,
10130 declarator,
10131 attributes);
10132 if (fco != error_mark_node)
10134 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10135 DECL_ARTIFICIAL (fco) = 1;
10136 /* Give the object parameter a different name. */
10137 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10138 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10139 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10141 if (template_param_list)
10143 fco = finish_member_template_decl (fco);
10144 finish_template_decl (template_param_list);
10145 --parser->num_template_parameter_lists;
10147 else if (parser->fully_implicit_function_template_p)
10148 fco = finish_fully_implicit_template (parser, fco);
10150 finish_member_declaration (fco);
10152 obstack_free (&declarator_obstack, p);
10154 return (fco != error_mark_node);
10158 /* Parse the body of a lambda expression, which is simply
10160 compound-statement
10162 but which requires special handling.
10163 LAMBDA_EXPR is the current representation of the lambda expression. */
10165 static void
10166 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10168 bool nested = (current_function_decl != NULL_TREE);
10169 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10170 if (nested)
10171 push_function_context ();
10172 else
10173 /* Still increment function_depth so that we don't GC in the
10174 middle of an expression. */
10175 ++function_depth;
10176 vec<tree> omp_privatization_save;
10177 save_omp_privatization_clauses (omp_privatization_save);
10178 /* Clear this in case we're in the middle of a default argument. */
10179 parser->local_variables_forbidden_p = false;
10181 /* Finish the function call operator
10182 - class_specifier
10183 + late_parsing_for_member
10184 + function_definition_after_declarator
10185 + ctor_initializer_opt_and_function_body */
10187 tree fco = lambda_function (lambda_expr);
10188 tree body;
10189 bool done = false;
10190 tree compound_stmt;
10191 tree cap;
10193 /* Let the front end know that we are going to be defining this
10194 function. */
10195 start_preparsed_function (fco,
10196 NULL_TREE,
10197 SF_PRE_PARSED | SF_INCLASS_INLINE);
10199 start_lambda_scope (fco);
10200 body = begin_function_body ();
10202 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10203 goto out;
10205 /* Push the proxies for any explicit captures. */
10206 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
10207 cap = TREE_CHAIN (cap))
10208 build_capture_proxy (TREE_PURPOSE (cap));
10210 compound_stmt = begin_compound_stmt (0);
10212 /* 5.1.1.4 of the standard says:
10213 If a lambda-expression does not include a trailing-return-type, it
10214 is as if the trailing-return-type denotes the following type:
10215 * if the compound-statement is of the form
10216 { return attribute-specifier [opt] expression ; }
10217 the type of the returned expression after lvalue-to-rvalue
10218 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10219 (_conv.array_ 4.2), and function-to-pointer conversion
10220 (_conv.func_ 4.3);
10221 * otherwise, void. */
10223 /* In a lambda that has neither a lambda-return-type-clause
10224 nor a deducible form, errors should be reported for return statements
10225 in the body. Since we used void as the placeholder return type, parsing
10226 the body as usual will give such desired behavior. */
10227 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10228 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
10229 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
10231 tree expr = NULL_TREE;
10232 cp_id_kind idk = CP_ID_KIND_NONE;
10234 /* Parse tentatively in case there's more after the initial return
10235 statement. */
10236 cp_parser_parse_tentatively (parser);
10238 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
10240 expr = cp_parser_expression (parser, &idk);
10242 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10243 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10245 if (cp_parser_parse_definitely (parser))
10247 if (!processing_template_decl)
10249 tree type = lambda_return_type (expr);
10250 apply_deduced_return_type (fco, type);
10251 if (type == error_mark_node)
10252 expr = error_mark_node;
10255 /* Will get error here if type not deduced yet. */
10256 finish_return_stmt (expr);
10258 done = true;
10262 if (!done)
10264 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10265 cp_parser_label_declaration (parser);
10266 cp_parser_statement_seq_opt (parser, NULL_TREE);
10267 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10270 finish_compound_stmt (compound_stmt);
10272 out:
10273 finish_function_body (body);
10274 finish_lambda_scope ();
10276 /* Finish the function and generate code for it if necessary. */
10277 tree fn = finish_function (/*inline*/2);
10279 /* Only expand if the call op is not a template. */
10280 if (!DECL_TEMPLATE_INFO (fco))
10281 expand_or_defer_fn (fn);
10284 restore_omp_privatization_clauses (omp_privatization_save);
10285 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10286 if (nested)
10287 pop_function_context();
10288 else
10289 --function_depth;
10292 /* Statements [gram.stmt.stmt] */
10294 /* Parse a statement.
10296 statement:
10297 labeled-statement
10298 expression-statement
10299 compound-statement
10300 selection-statement
10301 iteration-statement
10302 jump-statement
10303 declaration-statement
10304 try-block
10306 C++11:
10308 statement:
10309 labeled-statement
10310 attribute-specifier-seq (opt) expression-statement
10311 attribute-specifier-seq (opt) compound-statement
10312 attribute-specifier-seq (opt) selection-statement
10313 attribute-specifier-seq (opt) iteration-statement
10314 attribute-specifier-seq (opt) jump-statement
10315 declaration-statement
10316 attribute-specifier-seq (opt) try-block
10318 TM Extension:
10320 statement:
10321 atomic-statement
10323 IN_COMPOUND is true when the statement is nested inside a
10324 cp_parser_compound_statement; this matters for certain pragmas.
10326 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10327 is a (possibly labeled) if statement which is not enclosed in braces
10328 and has an else clause. This is used to implement -Wparentheses.
10330 CHAIN is a vector of if-else-if conditions. */
10332 static void
10333 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10334 bool in_compound, bool *if_p, vec<tree> *chain)
10336 tree statement, std_attrs = NULL_TREE;
10337 cp_token *token;
10338 location_t statement_location, attrs_location;
10340 restart:
10341 if (if_p != NULL)
10342 *if_p = false;
10343 /* There is no statement yet. */
10344 statement = NULL_TREE;
10346 saved_token_sentinel saved_tokens (parser->lexer);
10347 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10348 if (c_dialect_objc ())
10349 /* In obj-c++, seeing '[[' might be the either the beginning of
10350 c++11 attributes, or a nested objc-message-expression. So
10351 let's parse the c++11 attributes tentatively. */
10352 cp_parser_parse_tentatively (parser);
10353 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10354 if (c_dialect_objc ())
10356 if (!cp_parser_parse_definitely (parser))
10357 std_attrs = NULL_TREE;
10360 /* Peek at the next token. */
10361 token = cp_lexer_peek_token (parser->lexer);
10362 /* Remember the location of the first token in the statement. */
10363 statement_location = token->location;
10364 /* If this is a keyword, then that will often determine what kind of
10365 statement we have. */
10366 if (token->type == CPP_KEYWORD)
10368 enum rid keyword = token->keyword;
10370 switch (keyword)
10372 case RID_CASE:
10373 case RID_DEFAULT:
10374 /* Looks like a labeled-statement with a case label.
10375 Parse the label, and then use tail recursion to parse
10376 the statement. */
10377 cp_parser_label_for_labeled_statement (parser, std_attrs);
10378 in_compound = false;
10379 goto restart;
10381 case RID_IF:
10382 case RID_SWITCH:
10383 statement = cp_parser_selection_statement (parser, if_p, chain);
10384 break;
10386 case RID_WHILE:
10387 case RID_DO:
10388 case RID_FOR:
10389 statement = cp_parser_iteration_statement (parser, false);
10390 break;
10392 case RID_CILK_FOR:
10393 if (!flag_cilkplus)
10395 error_at (cp_lexer_peek_token (parser->lexer)->location,
10396 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10397 cp_lexer_consume_token (parser->lexer);
10398 statement = error_mark_node;
10400 else
10401 statement = cp_parser_cilk_for (parser, integer_zero_node);
10402 break;
10404 case RID_BREAK:
10405 case RID_CONTINUE:
10406 case RID_RETURN:
10407 case RID_GOTO:
10408 statement = cp_parser_jump_statement (parser);
10409 break;
10411 case RID_CILK_SYNC:
10412 cp_lexer_consume_token (parser->lexer);
10413 if (flag_cilkplus)
10415 tree sync_expr = build_cilk_sync ();
10416 SET_EXPR_LOCATION (sync_expr,
10417 token->location);
10418 statement = finish_expr_stmt (sync_expr);
10420 else
10422 error_at (token->location, "-fcilkplus must be enabled to use"
10423 " %<_Cilk_sync%>");
10424 statement = error_mark_node;
10426 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10427 break;
10429 /* Objective-C++ exception-handling constructs. */
10430 case RID_AT_TRY:
10431 case RID_AT_CATCH:
10432 case RID_AT_FINALLY:
10433 case RID_AT_SYNCHRONIZED:
10434 case RID_AT_THROW:
10435 statement = cp_parser_objc_statement (parser);
10436 break;
10438 case RID_TRY:
10439 statement = cp_parser_try_block (parser);
10440 break;
10442 case RID_NAMESPACE:
10443 /* This must be a namespace alias definition. */
10444 cp_parser_declaration_statement (parser);
10445 return;
10447 case RID_TRANSACTION_ATOMIC:
10448 case RID_TRANSACTION_RELAXED:
10449 case RID_SYNCHRONIZED:
10450 case RID_ATOMIC_NOEXCEPT:
10451 case RID_ATOMIC_CANCEL:
10452 statement = cp_parser_transaction (parser, token);
10453 break;
10454 case RID_TRANSACTION_CANCEL:
10455 statement = cp_parser_transaction_cancel (parser);
10456 break;
10458 default:
10459 /* It might be a keyword like `int' that can start a
10460 declaration-statement. */
10461 break;
10464 else if (token->type == CPP_NAME)
10466 /* If the next token is a `:', then we are looking at a
10467 labeled-statement. */
10468 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10469 if (token->type == CPP_COLON)
10471 /* Looks like a labeled-statement with an ordinary label.
10472 Parse the label, and then use tail recursion to parse
10473 the statement. */
10475 cp_parser_label_for_labeled_statement (parser, std_attrs);
10476 in_compound = false;
10477 goto restart;
10480 /* Anything that starts with a `{' must be a compound-statement. */
10481 else if (token->type == CPP_OPEN_BRACE)
10482 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10483 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10484 a statement all its own. */
10485 else if (token->type == CPP_PRAGMA)
10487 /* Only certain OpenMP pragmas are attached to statements, and thus
10488 are considered statements themselves. All others are not. In
10489 the context of a compound, accept the pragma as a "statement" and
10490 return so that we can check for a close brace. Otherwise we
10491 require a real statement and must go back and read one. */
10492 if (in_compound)
10493 cp_parser_pragma (parser, pragma_compound);
10494 else if (!cp_parser_pragma (parser, pragma_stmt))
10495 goto restart;
10496 return;
10498 else if (token->type == CPP_EOF)
10500 cp_parser_error (parser, "expected statement");
10501 return;
10504 /* Everything else must be a declaration-statement or an
10505 expression-statement. Try for the declaration-statement
10506 first, unless we are looking at a `;', in which case we know that
10507 we have an expression-statement. */
10508 if (!statement)
10510 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10512 if (std_attrs != NULL_TREE)
10514 /* Attributes should be parsed as part of the the
10515 declaration, so let's un-parse them. */
10516 saved_tokens.rollback();
10517 std_attrs = NULL_TREE;
10520 cp_parser_parse_tentatively (parser);
10521 /* Try to parse the declaration-statement. */
10522 cp_parser_declaration_statement (parser);
10523 /* If that worked, we're done. */
10524 if (cp_parser_parse_definitely (parser))
10525 return;
10527 /* Look for an expression-statement instead. */
10528 statement = cp_parser_expression_statement (parser, in_statement_expr);
10531 /* Set the line number for the statement. */
10532 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10533 SET_EXPR_LOCATION (statement, statement_location);
10535 /* Note that for now, we don't do anything with c++11 statements
10536 parsed at this level. */
10537 if (std_attrs != NULL_TREE)
10538 warning_at (attrs_location,
10539 OPT_Wattributes,
10540 "attributes at the beginning of statement are ignored");
10543 /* Parse the label for a labeled-statement, i.e.
10545 identifier :
10546 case constant-expression :
10547 default :
10549 GNU Extension:
10550 case constant-expression ... constant-expression : statement
10552 When a label is parsed without errors, the label is added to the
10553 parse tree by the finish_* functions, so this function doesn't
10554 have to return the label. */
10556 static void
10557 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10559 cp_token *token;
10560 tree label = NULL_TREE;
10561 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10563 /* The next token should be an identifier. */
10564 token = cp_lexer_peek_token (parser->lexer);
10565 if (token->type != CPP_NAME
10566 && token->type != CPP_KEYWORD)
10568 cp_parser_error (parser, "expected labeled-statement");
10569 return;
10572 parser->colon_corrects_to_scope_p = false;
10573 switch (token->keyword)
10575 case RID_CASE:
10577 tree expr, expr_hi;
10578 cp_token *ellipsis;
10580 /* Consume the `case' token. */
10581 cp_lexer_consume_token (parser->lexer);
10582 /* Parse the constant-expression. */
10583 expr = cp_parser_constant_expression (parser);
10584 if (check_for_bare_parameter_packs (expr))
10585 expr = error_mark_node;
10587 ellipsis = cp_lexer_peek_token (parser->lexer);
10588 if (ellipsis->type == CPP_ELLIPSIS)
10590 /* Consume the `...' token. */
10591 cp_lexer_consume_token (parser->lexer);
10592 expr_hi = cp_parser_constant_expression (parser);
10593 if (check_for_bare_parameter_packs (expr_hi))
10594 expr_hi = error_mark_node;
10596 /* We don't need to emit warnings here, as the common code
10597 will do this for us. */
10599 else
10600 expr_hi = NULL_TREE;
10602 if (parser->in_switch_statement_p)
10603 finish_case_label (token->location, expr, expr_hi);
10604 else
10605 error_at (token->location,
10606 "case label %qE not within a switch statement",
10607 expr);
10609 break;
10611 case RID_DEFAULT:
10612 /* Consume the `default' token. */
10613 cp_lexer_consume_token (parser->lexer);
10615 if (parser->in_switch_statement_p)
10616 finish_case_label (token->location, NULL_TREE, NULL_TREE);
10617 else
10618 error_at (token->location, "case label not within a switch statement");
10619 break;
10621 default:
10622 /* Anything else must be an ordinary label. */
10623 label = finish_label_stmt (cp_parser_identifier (parser));
10624 break;
10627 /* Require the `:' token. */
10628 cp_parser_require (parser, CPP_COLON, RT_COLON);
10630 /* An ordinary label may optionally be followed by attributes.
10631 However, this is only permitted if the attributes are then
10632 followed by a semicolon. This is because, for backward
10633 compatibility, when parsing
10634 lab: __attribute__ ((unused)) int i;
10635 we want the attribute to attach to "i", not "lab". */
10636 if (label != NULL_TREE
10637 && cp_next_tokens_can_be_gnu_attribute_p (parser))
10639 tree attrs;
10640 cp_parser_parse_tentatively (parser);
10641 attrs = cp_parser_gnu_attributes_opt (parser);
10642 if (attrs == NULL_TREE
10643 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10644 cp_parser_abort_tentative_parse (parser);
10645 else if (!cp_parser_parse_definitely (parser))
10647 else
10648 attributes = chainon (attributes, attrs);
10651 if (attributes != NULL_TREE)
10652 cplus_decl_attributes (&label, attributes, 0);
10654 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10657 /* Parse an expression-statement.
10659 expression-statement:
10660 expression [opt] ;
10662 Returns the new EXPR_STMT -- or NULL_TREE if the expression
10663 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
10664 indicates whether this expression-statement is part of an
10665 expression statement. */
10667 static tree
10668 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
10670 tree statement = NULL_TREE;
10671 cp_token *token = cp_lexer_peek_token (parser->lexer);
10673 /* If the next token is a ';', then there is no expression
10674 statement. */
10675 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10677 statement = cp_parser_expression (parser);
10678 if (statement == error_mark_node
10679 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10681 cp_parser_skip_to_end_of_block_or_statement (parser);
10682 return error_mark_node;
10686 /* Give a helpful message for "A<T>::type t;" and the like. */
10687 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
10688 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10690 if (TREE_CODE (statement) == SCOPE_REF)
10691 error_at (token->location, "need %<typename%> before %qE because "
10692 "%qT is a dependent scope",
10693 statement, TREE_OPERAND (statement, 0));
10694 else if (is_overloaded_fn (statement)
10695 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
10697 /* A::A a; */
10698 tree fn = get_first_fn (statement);
10699 error_at (token->location,
10700 "%<%T::%D%> names the constructor, not the type",
10701 DECL_CONTEXT (fn), DECL_NAME (fn));
10705 /* Consume the final `;'. */
10706 cp_parser_consume_semicolon_at_end_of_statement (parser);
10708 if (in_statement_expr
10709 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10710 /* This is the final expression statement of a statement
10711 expression. */
10712 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10713 else if (statement)
10714 statement = finish_expr_stmt (statement);
10716 return statement;
10719 /* Parse a compound-statement.
10721 compound-statement:
10722 { statement-seq [opt] }
10724 GNU extension:
10726 compound-statement:
10727 { label-declaration-seq [opt] statement-seq [opt] }
10729 label-declaration-seq:
10730 label-declaration
10731 label-declaration-seq label-declaration
10733 Returns a tree representing the statement. */
10735 static tree
10736 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10737 int bcs_flags, bool function_body)
10739 tree compound_stmt;
10741 /* Consume the `{'. */
10742 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10743 return error_mark_node;
10744 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10745 && !function_body && cxx_dialect < cxx14)
10746 pedwarn (input_location, OPT_Wpedantic,
10747 "compound-statement in constexpr function");
10748 /* Begin the compound-statement. */
10749 compound_stmt = begin_compound_stmt (bcs_flags);
10750 /* If the next keyword is `__label__' we have a label declaration. */
10751 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10752 cp_parser_label_declaration (parser);
10753 /* Parse an (optional) statement-seq. */
10754 cp_parser_statement_seq_opt (parser, in_statement_expr);
10755 /* Finish the compound-statement. */
10756 finish_compound_stmt (compound_stmt);
10757 /* Consume the `}'. */
10758 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10760 return compound_stmt;
10763 /* Parse an (optional) statement-seq.
10765 statement-seq:
10766 statement
10767 statement-seq [opt] statement */
10769 static void
10770 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10772 /* Scan statements until there aren't any more. */
10773 while (true)
10775 cp_token *token = cp_lexer_peek_token (parser->lexer);
10777 /* If we are looking at a `}', then we have run out of
10778 statements; the same is true if we have reached the end
10779 of file, or have stumbled upon a stray '@end'. */
10780 if (token->type == CPP_CLOSE_BRACE
10781 || token->type == CPP_EOF
10782 || token->type == CPP_PRAGMA_EOL
10783 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10784 break;
10786 /* If we are in a compound statement and find 'else' then
10787 something went wrong. */
10788 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10790 if (parser->in_statement & IN_IF_STMT)
10791 break;
10792 else
10794 token = cp_lexer_consume_token (parser->lexer);
10795 error_at (token->location, "%<else%> without a previous %<if%>");
10799 /* Parse the statement. */
10800 cp_parser_statement (parser, in_statement_expr, true, NULL);
10804 /* Parse a selection-statement.
10806 selection-statement:
10807 if ( condition ) statement
10808 if ( condition ) statement else statement
10809 switch ( condition ) statement
10811 Returns the new IF_STMT or SWITCH_STMT.
10813 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10814 is a (possibly labeled) if statement which is not enclosed in
10815 braces and has an else clause. This is used to implement
10816 -Wparentheses.
10818 CHAIN is a vector of if-else-if conditions. This is used to implement
10819 -Wduplicated-cond. */
10821 static tree
10822 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
10823 vec<tree> *chain)
10825 cp_token *token;
10826 enum rid keyword;
10827 token_indent_info guard_tinfo;
10829 if (if_p != NULL)
10830 *if_p = false;
10832 /* Peek at the next token. */
10833 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10834 guard_tinfo = get_token_indent_info (token);
10836 /* See what kind of keyword it is. */
10837 keyword = token->keyword;
10838 switch (keyword)
10840 case RID_IF:
10841 case RID_SWITCH:
10843 tree statement;
10844 tree condition;
10846 /* Look for the `('. */
10847 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10849 cp_parser_skip_to_end_of_statement (parser);
10850 return error_mark_node;
10853 /* Begin the selection-statement. */
10854 if (keyword == RID_IF)
10855 statement = begin_if_stmt ();
10856 else
10857 statement = begin_switch_stmt ();
10859 /* Parse the condition. */
10860 condition = cp_parser_condition (parser);
10861 /* Look for the `)'. */
10862 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10863 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10864 /*consume_paren=*/true);
10866 if (keyword == RID_IF)
10868 bool nested_if;
10869 unsigned char in_statement;
10871 /* Add the condition. */
10872 finish_if_stmt_cond (condition, statement);
10874 if (warn_duplicated_cond)
10875 warn_duplicated_cond_add_or_warn (token->location, condition,
10876 &chain);
10878 /* Parse the then-clause. */
10879 in_statement = parser->in_statement;
10880 parser->in_statement |= IN_IF_STMT;
10881 cp_parser_implicitly_scoped_statement (parser, &nested_if,
10882 guard_tinfo);
10883 parser->in_statement = in_statement;
10885 finish_then_clause (statement);
10887 /* If the next token is `else', parse the else-clause. */
10888 if (cp_lexer_next_token_is_keyword (parser->lexer,
10889 RID_ELSE))
10891 guard_tinfo
10892 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
10893 /* Consume the `else' keyword. */
10894 cp_lexer_consume_token (parser->lexer);
10895 if (warn_duplicated_cond)
10897 if (cp_lexer_next_token_is_keyword (parser->lexer,
10898 RID_IF)
10899 && chain == NULL)
10901 /* We've got "if (COND) else if (COND2)". Start
10902 the condition chain and add COND as the first
10903 element. */
10904 chain = new vec<tree> ();
10905 if (!CONSTANT_CLASS_P (condition)
10906 && !TREE_SIDE_EFFECTS (condition))
10908 /* Wrap it in a NOP_EXPR so that we can set the
10909 location of the condition. */
10910 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
10911 condition);
10912 SET_EXPR_LOCATION (e, token->location);
10913 chain->safe_push (e);
10916 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
10917 RID_IF))
10919 /* This is if-else without subsequent if. Zap the
10920 condition chain; we would have already warned at
10921 this point. */
10922 delete chain;
10923 chain = NULL;
10926 begin_else_clause (statement);
10927 /* Parse the else-clause. */
10928 cp_parser_implicitly_scoped_statement (parser, NULL,
10929 guard_tinfo, chain);
10931 finish_else_clause (statement);
10933 /* If we are currently parsing a then-clause, then
10934 IF_P will not be NULL. We set it to true to
10935 indicate that this if statement has an else clause.
10936 This may trigger the Wparentheses warning below
10937 when we get back up to the parent if statement. */
10938 if (if_p != NULL)
10939 *if_p = true;
10941 else
10943 /* This if statement does not have an else clause. If
10944 NESTED_IF is true, then the then-clause is an if
10945 statement which does have an else clause. We warn
10946 about the potential ambiguity. */
10947 if (nested_if)
10948 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10949 "suggest explicit braces to avoid ambiguous"
10950 " %<else%>");
10951 if (warn_duplicated_cond)
10953 /* We don't need the condition chain anymore. */
10954 delete chain;
10955 chain = NULL;
10959 /* Now we're all done with the if-statement. */
10960 finish_if_stmt (statement);
10962 else
10964 bool in_switch_statement_p;
10965 unsigned char in_statement;
10967 /* Add the condition. */
10968 finish_switch_cond (condition, statement);
10970 /* Parse the body of the switch-statement. */
10971 in_switch_statement_p = parser->in_switch_statement_p;
10972 in_statement = parser->in_statement;
10973 parser->in_switch_statement_p = true;
10974 parser->in_statement |= IN_SWITCH_STMT;
10975 cp_parser_implicitly_scoped_statement (parser, NULL,
10976 guard_tinfo);
10977 parser->in_switch_statement_p = in_switch_statement_p;
10978 parser->in_statement = in_statement;
10980 /* Now we're all done with the switch-statement. */
10981 finish_switch_stmt (statement);
10984 return statement;
10986 break;
10988 default:
10989 cp_parser_error (parser, "expected selection-statement");
10990 return error_mark_node;
10994 /* Parse a condition.
10996 condition:
10997 expression
10998 type-specifier-seq declarator = initializer-clause
10999 type-specifier-seq declarator braced-init-list
11001 GNU Extension:
11003 condition:
11004 type-specifier-seq declarator asm-specification [opt]
11005 attributes [opt] = assignment-expression
11007 Returns the expression that should be tested. */
11009 static tree
11010 cp_parser_condition (cp_parser* parser)
11012 cp_decl_specifier_seq type_specifiers;
11013 const char *saved_message;
11014 int declares_class_or_enum;
11016 /* Try the declaration first. */
11017 cp_parser_parse_tentatively (parser);
11018 /* New types are not allowed in the type-specifier-seq for a
11019 condition. */
11020 saved_message = parser->type_definition_forbidden_message;
11021 parser->type_definition_forbidden_message
11022 = G_("types may not be defined in conditions");
11023 /* Parse the type-specifier-seq. */
11024 cp_parser_decl_specifier_seq (parser,
11025 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11026 &type_specifiers,
11027 &declares_class_or_enum);
11028 /* Restore the saved message. */
11029 parser->type_definition_forbidden_message = saved_message;
11030 /* If all is well, we might be looking at a declaration. */
11031 if (!cp_parser_error_occurred (parser))
11033 tree decl;
11034 tree asm_specification;
11035 tree attributes;
11036 cp_declarator *declarator;
11037 tree initializer = NULL_TREE;
11039 /* Parse the declarator. */
11040 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11041 /*ctor_dtor_or_conv_p=*/NULL,
11042 /*parenthesized_p=*/NULL,
11043 /*member_p=*/false,
11044 /*friend_p=*/false);
11045 /* Parse the attributes. */
11046 attributes = cp_parser_attributes_opt (parser);
11047 /* Parse the asm-specification. */
11048 asm_specification = cp_parser_asm_specification_opt (parser);
11049 /* If the next token is not an `=' or '{', then we might still be
11050 looking at an expression. For example:
11052 if (A(a).x)
11054 looks like a decl-specifier-seq and a declarator -- but then
11055 there is no `=', so this is an expression. */
11056 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11057 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11058 cp_parser_simulate_error (parser);
11060 /* If we did see an `=' or '{', then we are looking at a declaration
11061 for sure. */
11062 if (cp_parser_parse_definitely (parser))
11064 tree pushed_scope;
11065 bool non_constant_p;
11066 bool flags = LOOKUP_ONLYCONVERTING;
11068 /* Create the declaration. */
11069 decl = start_decl (declarator, &type_specifiers,
11070 /*initialized_p=*/true,
11071 attributes, /*prefix_attributes=*/NULL_TREE,
11072 &pushed_scope);
11074 /* Parse the initializer. */
11075 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11077 initializer = cp_parser_braced_list (parser, &non_constant_p);
11078 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11079 flags = 0;
11081 else
11083 /* Consume the `='. */
11084 cp_parser_require (parser, CPP_EQ, RT_EQ);
11085 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11087 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11088 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11090 /* Process the initializer. */
11091 cp_finish_decl (decl,
11092 initializer, !non_constant_p,
11093 asm_specification,
11094 flags);
11096 if (pushed_scope)
11097 pop_scope (pushed_scope);
11099 return convert_from_reference (decl);
11102 /* If we didn't even get past the declarator successfully, we are
11103 definitely not looking at a declaration. */
11104 else
11105 cp_parser_abort_tentative_parse (parser);
11107 /* Otherwise, we are looking at an expression. */
11108 return cp_parser_expression (parser);
11111 /* Parses a for-statement or range-for-statement until the closing ')',
11112 not included. */
11114 static tree
11115 cp_parser_for (cp_parser *parser, bool ivdep)
11117 tree init, scope, decl;
11118 bool is_range_for;
11120 /* Begin the for-statement. */
11121 scope = begin_for_scope (&init);
11123 /* Parse the initialization. */
11124 is_range_for = cp_parser_for_init_statement (parser, &decl);
11126 if (is_range_for)
11127 return cp_parser_range_for (parser, scope, init, decl, ivdep);
11128 else
11129 return cp_parser_c_for (parser, scope, init, ivdep);
11132 static tree
11133 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11135 /* Normal for loop */
11136 tree condition = NULL_TREE;
11137 tree expression = NULL_TREE;
11138 tree stmt;
11140 stmt = begin_for_stmt (scope, init);
11141 /* The for-init-statement has already been parsed in
11142 cp_parser_for_init_statement, so no work is needed here. */
11143 finish_for_init_stmt (stmt);
11145 /* If there's a condition, process it. */
11146 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11147 condition = cp_parser_condition (parser);
11148 else if (ivdep)
11150 cp_parser_error (parser, "missing loop condition in loop with "
11151 "%<GCC ivdep%> pragma");
11152 condition = error_mark_node;
11154 finish_for_cond (condition, stmt, ivdep);
11155 /* Look for the `;'. */
11156 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11158 /* If there's an expression, process it. */
11159 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11160 expression = cp_parser_expression (parser);
11161 finish_for_expr (expression, stmt);
11163 return stmt;
11166 /* Tries to parse a range-based for-statement:
11168 range-based-for:
11169 decl-specifier-seq declarator : expression
11171 The decl-specifier-seq declarator and the `:' are already parsed by
11172 cp_parser_for_init_statement. If processing_template_decl it returns a
11173 newly created RANGE_FOR_STMT; if not, it is converted to a
11174 regular FOR_STMT. */
11176 static tree
11177 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11178 bool ivdep)
11180 tree stmt, range_expr;
11182 /* Get the range declaration momentarily out of the way so that
11183 the range expression doesn't clash with it. */
11184 if (range_decl != error_mark_node)
11185 pop_binding (DECL_NAME (range_decl), range_decl);
11187 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11189 bool expr_non_constant_p;
11190 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11192 else
11193 range_expr = cp_parser_expression (parser);
11195 /* Put the range declaration back into scope. */
11196 if (range_decl != error_mark_node)
11197 push_binding (DECL_NAME (range_decl), range_decl, current_binding_level);
11199 /* If in template, STMT is converted to a normal for-statement
11200 at instantiation. If not, it is done just ahead. */
11201 if (processing_template_decl)
11203 if (check_for_bare_parameter_packs (range_expr))
11204 range_expr = error_mark_node;
11205 stmt = begin_range_for_stmt (scope, init);
11206 if (ivdep)
11207 RANGE_FOR_IVDEP (stmt) = 1;
11208 finish_range_for_decl (stmt, range_decl, range_expr);
11209 if (!type_dependent_expression_p (range_expr)
11210 /* do_auto_deduction doesn't mess with template init-lists. */
11211 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11212 do_range_for_auto_deduction (range_decl, range_expr);
11214 else
11216 stmt = begin_for_stmt (scope, init);
11217 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
11219 return stmt;
11222 /* Subroutine of cp_convert_range_for: given the initializer expression,
11223 builds up the range temporary. */
11225 static tree
11226 build_range_temp (tree range_expr)
11228 tree range_type, range_temp;
11230 /* Find out the type deduced by the declaration
11231 `auto &&__range = range_expr'. */
11232 range_type = cp_build_reference_type (make_auto (), true);
11233 range_type = do_auto_deduction (range_type, range_expr,
11234 type_uses_auto (range_type));
11236 /* Create the __range variable. */
11237 range_temp = build_decl (input_location, VAR_DECL,
11238 get_identifier ("__for_range"), range_type);
11239 TREE_USED (range_temp) = 1;
11240 DECL_ARTIFICIAL (range_temp) = 1;
11242 return range_temp;
11245 /* Used by cp_parser_range_for in template context: we aren't going to
11246 do a full conversion yet, but we still need to resolve auto in the
11247 type of the for-range-declaration if present. This is basically
11248 a shortcut version of cp_convert_range_for. */
11250 static void
11251 do_range_for_auto_deduction (tree decl, tree range_expr)
11253 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11254 if (auto_node)
11256 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11257 range_temp = convert_from_reference (build_range_temp (range_expr));
11258 iter_type = (cp_parser_perform_range_for_lookup
11259 (range_temp, &begin_dummy, &end_dummy));
11260 if (iter_type)
11262 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11263 iter_type);
11264 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
11265 tf_warning_or_error);
11266 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11267 iter_decl, auto_node);
11272 /* Converts a range-based for-statement into a normal
11273 for-statement, as per the definition.
11275 for (RANGE_DECL : RANGE_EXPR)
11276 BLOCK
11278 should be equivalent to:
11281 auto &&__range = RANGE_EXPR;
11282 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11283 __begin != __end;
11284 ++__begin)
11286 RANGE_DECL = *__begin;
11287 BLOCK
11291 If RANGE_EXPR is an array:
11292 BEGIN_EXPR = __range
11293 END_EXPR = __range + ARRAY_SIZE(__range)
11294 Else if RANGE_EXPR has a member 'begin' or 'end':
11295 BEGIN_EXPR = __range.begin()
11296 END_EXPR = __range.end()
11297 Else:
11298 BEGIN_EXPR = begin(__range)
11299 END_EXPR = end(__range);
11301 If __range has a member 'begin' but not 'end', or vice versa, we must
11302 still use the second alternative (it will surely fail, however).
11303 When calling begin()/end() in the third alternative we must use
11304 argument dependent lookup, but always considering 'std' as an associated
11305 namespace. */
11307 tree
11308 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11309 bool ivdep)
11311 tree begin, end;
11312 tree iter_type, begin_expr, end_expr;
11313 tree condition, expression;
11315 if (range_decl == error_mark_node || range_expr == error_mark_node)
11316 /* If an error happened previously do nothing or else a lot of
11317 unhelpful errors would be issued. */
11318 begin_expr = end_expr = iter_type = error_mark_node;
11319 else
11321 tree range_temp;
11323 if (VAR_P (range_expr)
11324 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11325 /* Can't bind a reference to an array of runtime bound. */
11326 range_temp = range_expr;
11327 else
11329 range_temp = build_range_temp (range_expr);
11330 pushdecl (range_temp);
11331 cp_finish_decl (range_temp, range_expr,
11332 /*is_constant_init*/false, NULL_TREE,
11333 LOOKUP_ONLYCONVERTING);
11334 range_temp = convert_from_reference (range_temp);
11336 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11337 &begin_expr, &end_expr);
11340 /* The new for initialization statement. */
11341 begin = build_decl (input_location, VAR_DECL,
11342 get_identifier ("__for_begin"), iter_type);
11343 TREE_USED (begin) = 1;
11344 DECL_ARTIFICIAL (begin) = 1;
11345 pushdecl (begin);
11346 cp_finish_decl (begin, begin_expr,
11347 /*is_constant_init*/false, NULL_TREE,
11348 LOOKUP_ONLYCONVERTING);
11350 end = build_decl (input_location, VAR_DECL,
11351 get_identifier ("__for_end"), iter_type);
11352 TREE_USED (end) = 1;
11353 DECL_ARTIFICIAL (end) = 1;
11354 pushdecl (end);
11355 cp_finish_decl (end, end_expr,
11356 /*is_constant_init*/false, NULL_TREE,
11357 LOOKUP_ONLYCONVERTING);
11359 finish_for_init_stmt (statement);
11361 /* The new for condition. */
11362 condition = build_x_binary_op (input_location, NE_EXPR,
11363 begin, ERROR_MARK,
11364 end, ERROR_MARK,
11365 NULL, tf_warning_or_error);
11366 finish_for_cond (condition, statement, ivdep);
11368 /* The new increment expression. */
11369 expression = finish_unary_op_expr (input_location,
11370 PREINCREMENT_EXPR, begin,
11371 tf_warning_or_error);
11372 finish_for_expr (expression, statement);
11374 /* The declaration is initialized with *__begin inside the loop body. */
11375 cp_finish_decl (range_decl,
11376 build_x_indirect_ref (input_location, begin, RO_NULL,
11377 tf_warning_or_error),
11378 /*is_constant_init*/false, NULL_TREE,
11379 LOOKUP_ONLYCONVERTING);
11381 return statement;
11384 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11385 We need to solve both at the same time because the method used
11386 depends on the existence of members begin or end.
11387 Returns the type deduced for the iterator expression. */
11389 static tree
11390 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11392 if (error_operand_p (range))
11394 *begin = *end = error_mark_node;
11395 return error_mark_node;
11398 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11400 error ("range-based %<for%> expression of type %qT "
11401 "has incomplete type", TREE_TYPE (range));
11402 *begin = *end = error_mark_node;
11403 return error_mark_node;
11405 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11407 /* If RANGE is an array, we will use pointer arithmetic. */
11408 *begin = range;
11409 *end = build_binary_op (input_location, PLUS_EXPR,
11410 range,
11411 array_type_nelts_top (TREE_TYPE (range)),
11413 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
11415 else
11417 /* If it is not an array, we must do a bit of magic. */
11418 tree id_begin, id_end;
11419 tree member_begin, member_end;
11421 *begin = *end = error_mark_node;
11423 id_begin = get_identifier ("begin");
11424 id_end = get_identifier ("end");
11425 member_begin = lookup_member (TREE_TYPE (range), id_begin,
11426 /*protect=*/2, /*want_type=*/false,
11427 tf_warning_or_error);
11428 member_end = lookup_member (TREE_TYPE (range), id_end,
11429 /*protect=*/2, /*want_type=*/false,
11430 tf_warning_or_error);
11432 if (member_begin != NULL_TREE || member_end != NULL_TREE)
11434 /* Use the member functions. */
11435 if (member_begin != NULL_TREE)
11436 *begin = cp_parser_range_for_member_function (range, id_begin);
11437 else
11438 error ("range-based %<for%> expression of type %qT has an "
11439 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11441 if (member_end != NULL_TREE)
11442 *end = cp_parser_range_for_member_function (range, id_end);
11443 else
11444 error ("range-based %<for%> expression of type %qT has a "
11445 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
11447 else
11449 /* Use global functions with ADL. */
11450 vec<tree, va_gc> *vec;
11451 vec = make_tree_vector ();
11453 vec_safe_push (vec, range);
11455 member_begin = perform_koenig_lookup (id_begin, vec,
11456 tf_warning_or_error);
11457 *begin = finish_call_expr (member_begin, &vec, false, true,
11458 tf_warning_or_error);
11459 member_end = perform_koenig_lookup (id_end, vec,
11460 tf_warning_or_error);
11461 *end = finish_call_expr (member_end, &vec, false, true,
11462 tf_warning_or_error);
11464 release_tree_vector (vec);
11467 /* Last common checks. */
11468 if (*begin == error_mark_node || *end == error_mark_node)
11470 /* If one of the expressions is an error do no more checks. */
11471 *begin = *end = error_mark_node;
11472 return error_mark_node;
11474 else if (type_dependent_expression_p (*begin)
11475 || type_dependent_expression_p (*end))
11476 /* Can happen, when, eg, in a template context, Koenig lookup
11477 can't resolve begin/end (c++/58503). */
11478 return NULL_TREE;
11479 else
11481 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
11482 /* The unqualified type of the __begin and __end temporaries should
11483 be the same, as required by the multiple auto declaration. */
11484 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
11485 error ("inconsistent begin/end types in range-based %<for%> "
11486 "statement: %qT and %qT",
11487 TREE_TYPE (*begin), TREE_TYPE (*end));
11488 return iter_type;
11493 /* Helper function for cp_parser_perform_range_for_lookup.
11494 Builds a tree for RANGE.IDENTIFIER(). */
11496 static tree
11497 cp_parser_range_for_member_function (tree range, tree identifier)
11499 tree member, res;
11500 vec<tree, va_gc> *vec;
11502 member = finish_class_member_access_expr (range, identifier,
11503 false, tf_warning_or_error);
11504 if (member == error_mark_node)
11505 return error_mark_node;
11507 vec = make_tree_vector ();
11508 res = finish_call_expr (member, &vec,
11509 /*disallow_virtual=*/false,
11510 /*koenig_p=*/false,
11511 tf_warning_or_error);
11512 release_tree_vector (vec);
11513 return res;
11516 /* Parse an iteration-statement.
11518 iteration-statement:
11519 while ( condition ) statement
11520 do statement while ( expression ) ;
11521 for ( for-init-statement condition [opt] ; expression [opt] )
11522 statement
11524 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
11526 static tree
11527 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
11529 cp_token *token;
11530 enum rid keyword;
11531 tree statement;
11532 unsigned char in_statement;
11533 token_indent_info guard_tinfo;
11535 /* Peek at the next token. */
11536 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
11537 if (!token)
11538 return error_mark_node;
11540 guard_tinfo = get_token_indent_info (token);
11542 /* Remember whether or not we are already within an iteration
11543 statement. */
11544 in_statement = parser->in_statement;
11546 /* See what kind of keyword it is. */
11547 keyword = token->keyword;
11548 switch (keyword)
11550 case RID_WHILE:
11552 tree condition;
11554 /* Begin the while-statement. */
11555 statement = begin_while_stmt ();
11556 /* Look for the `('. */
11557 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11558 /* Parse the condition. */
11559 condition = cp_parser_condition (parser);
11560 finish_while_stmt_cond (condition, statement, ivdep);
11561 /* Look for the `)'. */
11562 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11563 /* Parse the dependent statement. */
11564 parser->in_statement = IN_ITERATION_STMT;
11565 cp_parser_already_scoped_statement (parser, guard_tinfo);
11566 parser->in_statement = in_statement;
11567 /* We're done with the while-statement. */
11568 finish_while_stmt (statement);
11570 break;
11572 case RID_DO:
11574 tree expression;
11576 /* Begin the do-statement. */
11577 statement = begin_do_stmt ();
11578 /* Parse the body of the do-statement. */
11579 parser->in_statement = IN_ITERATION_STMT;
11580 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
11581 parser->in_statement = in_statement;
11582 finish_do_body (statement);
11583 /* Look for the `while' keyword. */
11584 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
11585 /* Look for the `('. */
11586 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11587 /* Parse the expression. */
11588 expression = cp_parser_expression (parser);
11589 /* We're done with the do-statement. */
11590 finish_do_stmt (expression, statement, ivdep);
11591 /* Look for the `)'. */
11592 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11593 /* Look for the `;'. */
11594 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11596 break;
11598 case RID_FOR:
11600 /* Look for the `('. */
11601 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11603 statement = cp_parser_for (parser, ivdep);
11605 /* Look for the `)'. */
11606 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11608 /* Parse the body of the for-statement. */
11609 parser->in_statement = IN_ITERATION_STMT;
11610 cp_parser_already_scoped_statement (parser, guard_tinfo);
11611 parser->in_statement = in_statement;
11613 /* We're done with the for-statement. */
11614 finish_for_stmt (statement);
11616 break;
11618 default:
11619 cp_parser_error (parser, "expected iteration-statement");
11620 statement = error_mark_node;
11621 break;
11624 return statement;
11627 /* Parse a for-init-statement or the declarator of a range-based-for.
11628 Returns true if a range-based-for declaration is seen.
11630 for-init-statement:
11631 expression-statement
11632 simple-declaration */
11634 static bool
11635 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
11637 /* If the next token is a `;', then we have an empty
11638 expression-statement. Grammatically, this is also a
11639 simple-declaration, but an invalid one, because it does not
11640 declare anything. Therefore, if we did not handle this case
11641 specially, we would issue an error message about an invalid
11642 declaration. */
11643 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11645 bool is_range_for = false;
11646 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11648 /* A colon is used in range-based for. */
11649 parser->colon_corrects_to_scope_p = false;
11651 /* We're going to speculatively look for a declaration, falling back
11652 to an expression, if necessary. */
11653 cp_parser_parse_tentatively (parser);
11654 /* Parse the declaration. */
11655 cp_parser_simple_declaration (parser,
11656 /*function_definition_allowed_p=*/false,
11657 decl);
11658 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11659 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11661 /* It is a range-for, consume the ':' */
11662 cp_lexer_consume_token (parser->lexer);
11663 is_range_for = true;
11664 if (cxx_dialect < cxx11)
11666 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11667 "range-based %<for%> loops only available with "
11668 "-std=c++11 or -std=gnu++11");
11669 *decl = error_mark_node;
11672 else
11673 /* The ';' is not consumed yet because we told
11674 cp_parser_simple_declaration not to. */
11675 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11677 if (cp_parser_parse_definitely (parser))
11678 return is_range_for;
11679 /* If the tentative parse failed, then we shall need to look for an
11680 expression-statement. */
11682 /* If we are here, it is an expression-statement. */
11683 cp_parser_expression_statement (parser, NULL_TREE);
11684 return false;
11687 /* Parse a jump-statement.
11689 jump-statement:
11690 break ;
11691 continue ;
11692 return expression [opt] ;
11693 return braced-init-list ;
11694 goto identifier ;
11696 GNU extension:
11698 jump-statement:
11699 goto * expression ;
11701 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
11703 static tree
11704 cp_parser_jump_statement (cp_parser* parser)
11706 tree statement = error_mark_node;
11707 cp_token *token;
11708 enum rid keyword;
11709 unsigned char in_statement;
11711 /* Peek at the next token. */
11712 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
11713 if (!token)
11714 return error_mark_node;
11716 /* See what kind of keyword it is. */
11717 keyword = token->keyword;
11718 switch (keyword)
11720 case RID_BREAK:
11721 in_statement = parser->in_statement & ~IN_IF_STMT;
11722 switch (in_statement)
11724 case 0:
11725 error_at (token->location, "break statement not within loop or switch");
11726 break;
11727 default:
11728 gcc_assert ((in_statement & IN_SWITCH_STMT)
11729 || in_statement == IN_ITERATION_STMT);
11730 statement = finish_break_stmt ();
11731 if (in_statement == IN_ITERATION_STMT)
11732 break_maybe_infinite_loop ();
11733 break;
11734 case IN_OMP_BLOCK:
11735 error_at (token->location, "invalid exit from OpenMP structured block");
11736 break;
11737 case IN_OMP_FOR:
11738 error_at (token->location, "break statement used with OpenMP for loop");
11739 break;
11740 case IN_CILK_SIMD_FOR:
11741 error_at (token->location, "break statement used with Cilk Plus for loop");
11742 break;
11744 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11745 break;
11747 case RID_CONTINUE:
11748 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
11750 case 0:
11751 error_at (token->location, "continue statement not within a loop");
11752 break;
11753 case IN_CILK_SIMD_FOR:
11754 error_at (token->location,
11755 "continue statement within %<#pragma simd%> loop body");
11756 /* Fall through. */
11757 case IN_ITERATION_STMT:
11758 case IN_OMP_FOR:
11759 statement = finish_continue_stmt ();
11760 break;
11761 case IN_OMP_BLOCK:
11762 error_at (token->location, "invalid exit from OpenMP structured block");
11763 break;
11764 default:
11765 gcc_unreachable ();
11767 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11768 break;
11770 case RID_RETURN:
11772 tree expr;
11773 bool expr_non_constant_p;
11775 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11777 cp_lexer_set_source_position (parser->lexer);
11778 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11779 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11781 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11782 expr = cp_parser_expression (parser);
11783 else
11784 /* If the next token is a `;', then there is no
11785 expression. */
11786 expr = NULL_TREE;
11787 /* Build the return-statement. */
11788 statement = finish_return_stmt (expr);
11789 /* Look for the final `;'. */
11790 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11792 break;
11794 case RID_GOTO:
11795 if (parser->in_function_body
11796 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11798 error ("%<goto%> in %<constexpr%> function");
11799 cp_function_chain->invalid_constexpr = true;
11802 /* Create the goto-statement. */
11803 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11805 /* Issue a warning about this use of a GNU extension. */
11806 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11807 /* Consume the '*' token. */
11808 cp_lexer_consume_token (parser->lexer);
11809 /* Parse the dependent expression. */
11810 finish_goto_stmt (cp_parser_expression (parser));
11812 else
11813 finish_goto_stmt (cp_parser_identifier (parser));
11814 /* Look for the final `;'. */
11815 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11816 break;
11818 default:
11819 cp_parser_error (parser, "expected jump-statement");
11820 break;
11823 return statement;
11826 /* Parse a declaration-statement.
11828 declaration-statement:
11829 block-declaration */
11831 static void
11832 cp_parser_declaration_statement (cp_parser* parser)
11834 void *p;
11836 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11837 p = obstack_alloc (&declarator_obstack, 0);
11839 /* Parse the block-declaration. */
11840 cp_parser_block_declaration (parser, /*statement_p=*/true);
11842 /* Free any declarators allocated. */
11843 obstack_free (&declarator_obstack, p);
11846 /* Some dependent statements (like `if (cond) statement'), are
11847 implicitly in their own scope. In other words, if the statement is
11848 a single statement (as opposed to a compound-statement), it is
11849 none-the-less treated as if it were enclosed in braces. Any
11850 declarations appearing in the dependent statement are out of scope
11851 after control passes that point. This function parses a statement,
11852 but ensures that is in its own scope, even if it is not a
11853 compound-statement.
11855 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11856 is a (possibly labeled) if statement which is not enclosed in
11857 braces and has an else clause. This is used to implement
11858 -Wparentheses.
11860 CHAIN is a vector of if-else-if conditions. This is used to implement
11861 -Wduplicated-cond.
11863 Returns the new statement. */
11865 static tree
11866 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
11867 const token_indent_info &guard_tinfo,
11868 vec<tree> *chain)
11870 tree statement;
11871 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
11872 token_indent_info body_tinfo
11873 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11875 if (if_p != NULL)
11876 *if_p = false;
11878 /* Mark if () ; with a special NOP_EXPR. */
11879 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11881 cp_lexer_consume_token (parser->lexer);
11882 statement = add_stmt (build_empty_stmt (body_loc));
11884 if (guard_tinfo.keyword == RID_IF
11885 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
11886 warning_at (body_loc, OPT_Wempty_body,
11887 "suggest braces around empty body in an %<if%> statement");
11888 else if (guard_tinfo.keyword == RID_ELSE)
11889 warning_at (body_loc, OPT_Wempty_body,
11890 "suggest braces around empty body in an %<else%> statement");
11892 /* if a compound is opened, we simply parse the statement directly. */
11893 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11894 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
11895 /* If the token is not a `{', then we must take special action. */
11896 else
11898 /* Create a compound-statement. */
11899 statement = begin_compound_stmt (0);
11900 /* Parse the dependent-statement. */
11901 cp_parser_statement (parser, NULL_TREE, false, if_p, chain);
11902 /* Finish the dummy compound-statement. */
11903 finish_compound_stmt (statement);
11906 token_indent_info next_tinfo
11907 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11908 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
11910 /* Return the statement. */
11911 return statement;
11914 /* For some dependent statements (like `while (cond) statement'), we
11915 have already created a scope. Therefore, even if the dependent
11916 statement is a compound-statement, we do not want to create another
11917 scope. */
11919 static void
11920 cp_parser_already_scoped_statement (cp_parser* parser,
11921 const token_indent_info &guard_tinfo)
11923 /* If the token is a `{', then we must take special action. */
11924 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11926 token_indent_info body_tinfo
11927 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11929 cp_parser_statement (parser, NULL_TREE, false, NULL);
11930 token_indent_info next_tinfo
11931 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11932 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
11934 else
11936 /* Avoid calling cp_parser_compound_statement, so that we
11937 don't create a new scope. Do everything else by hand. */
11938 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11939 /* If the next keyword is `__label__' we have a label declaration. */
11940 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11941 cp_parser_label_declaration (parser);
11942 /* Parse an (optional) statement-seq. */
11943 cp_parser_statement_seq_opt (parser, NULL_TREE);
11944 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11948 /* Declarations [gram.dcl.dcl] */
11950 /* Parse an optional declaration-sequence.
11952 declaration-seq:
11953 declaration
11954 declaration-seq declaration */
11956 static void
11957 cp_parser_declaration_seq_opt (cp_parser* parser)
11959 while (true)
11961 cp_token *token;
11963 token = cp_lexer_peek_token (parser->lexer);
11965 if (token->type == CPP_CLOSE_BRACE
11966 || token->type == CPP_EOF
11967 || token->type == CPP_PRAGMA_EOL)
11968 break;
11970 if (token->type == CPP_SEMICOLON)
11972 /* A declaration consisting of a single semicolon is
11973 invalid. Allow it unless we're being pedantic. */
11974 cp_lexer_consume_token (parser->lexer);
11975 if (!in_system_header_at (input_location))
11976 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11977 continue;
11980 /* If we're entering or exiting a region that's implicitly
11981 extern "C", modify the lang context appropriately. */
11982 if (!parser->implicit_extern_c && token->implicit_extern_c)
11984 push_lang_context (lang_name_c);
11985 parser->implicit_extern_c = true;
11987 else if (parser->implicit_extern_c && !token->implicit_extern_c)
11989 pop_lang_context ();
11990 parser->implicit_extern_c = false;
11993 if (token->type == CPP_PRAGMA)
11995 /* A top-level declaration can consist solely of a #pragma.
11996 A nested declaration cannot, so this is done here and not
11997 in cp_parser_declaration. (A #pragma at block scope is
11998 handled in cp_parser_statement.) */
11999 cp_parser_pragma (parser, pragma_external);
12000 continue;
12003 /* Parse the declaration itself. */
12004 cp_parser_declaration (parser);
12008 /* Parse a declaration.
12010 declaration:
12011 block-declaration
12012 function-definition
12013 template-declaration
12014 explicit-instantiation
12015 explicit-specialization
12016 linkage-specification
12017 namespace-definition
12019 GNU extension:
12021 declaration:
12022 __extension__ declaration */
12024 static void
12025 cp_parser_declaration (cp_parser* parser)
12027 cp_token token1;
12028 cp_token token2;
12029 int saved_pedantic;
12030 void *p;
12031 tree attributes = NULL_TREE;
12033 /* Check for the `__extension__' keyword. */
12034 if (cp_parser_extension_opt (parser, &saved_pedantic))
12036 /* Parse the qualified declaration. */
12037 cp_parser_declaration (parser);
12038 /* Restore the PEDANTIC flag. */
12039 pedantic = saved_pedantic;
12041 return;
12044 /* Try to figure out what kind of declaration is present. */
12045 token1 = *cp_lexer_peek_token (parser->lexer);
12047 if (token1.type != CPP_EOF)
12048 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12049 else
12051 token2.type = CPP_EOF;
12052 token2.keyword = RID_MAX;
12055 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12056 p = obstack_alloc (&declarator_obstack, 0);
12058 /* If the next token is `extern' and the following token is a string
12059 literal, then we have a linkage specification. */
12060 if (token1.keyword == RID_EXTERN
12061 && cp_parser_is_pure_string_literal (&token2))
12062 cp_parser_linkage_specification (parser);
12063 /* If the next token is `template', then we have either a template
12064 declaration, an explicit instantiation, or an explicit
12065 specialization. */
12066 else if (token1.keyword == RID_TEMPLATE)
12068 /* `template <>' indicates a template specialization. */
12069 if (token2.type == CPP_LESS
12070 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12071 cp_parser_explicit_specialization (parser);
12072 /* `template <' indicates a template declaration. */
12073 else if (token2.type == CPP_LESS)
12074 cp_parser_template_declaration (parser, /*member_p=*/false);
12075 /* Anything else must be an explicit instantiation. */
12076 else
12077 cp_parser_explicit_instantiation (parser);
12079 /* If the next token is `export', then we have a template
12080 declaration. */
12081 else if (token1.keyword == RID_EXPORT)
12082 cp_parser_template_declaration (parser, /*member_p=*/false);
12083 /* If the next token is `extern', 'static' or 'inline' and the one
12084 after that is `template', we have a GNU extended explicit
12085 instantiation directive. */
12086 else if (cp_parser_allow_gnu_extensions_p (parser)
12087 && (token1.keyword == RID_EXTERN
12088 || token1.keyword == RID_STATIC
12089 || token1.keyword == RID_INLINE)
12090 && token2.keyword == RID_TEMPLATE)
12091 cp_parser_explicit_instantiation (parser);
12092 /* If the next token is `namespace', check for a named or unnamed
12093 namespace definition. */
12094 else if (token1.keyword == RID_NAMESPACE
12095 && (/* A named namespace definition. */
12096 (token2.type == CPP_NAME
12097 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12098 != CPP_EQ))
12099 || (token2.type == CPP_OPEN_SQUARE
12100 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12101 == CPP_OPEN_SQUARE)
12102 /* An unnamed namespace definition. */
12103 || token2.type == CPP_OPEN_BRACE
12104 || token2.keyword == RID_ATTRIBUTE))
12105 cp_parser_namespace_definition (parser);
12106 /* An inline (associated) namespace definition. */
12107 else if (token1.keyword == RID_INLINE
12108 && token2.keyword == RID_NAMESPACE)
12109 cp_parser_namespace_definition (parser);
12110 /* Objective-C++ declaration/definition. */
12111 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12112 cp_parser_objc_declaration (parser, NULL_TREE);
12113 else if (c_dialect_objc ()
12114 && token1.keyword == RID_ATTRIBUTE
12115 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12116 cp_parser_objc_declaration (parser, attributes);
12117 /* At this point we may have a template declared by a concept
12118 introduction. */
12119 else if (flag_concepts
12120 && cp_parser_template_declaration_after_export (parser,
12121 /*member_p=*/false))
12122 /* We did. */;
12123 else
12124 /* Try to parse a block-declaration, or a function-definition. */
12125 cp_parser_block_declaration (parser, /*statement_p=*/false);
12127 /* Free any declarators allocated. */
12128 obstack_free (&declarator_obstack, p);
12131 /* Parse a block-declaration.
12133 block-declaration:
12134 simple-declaration
12135 asm-definition
12136 namespace-alias-definition
12137 using-declaration
12138 using-directive
12140 GNU Extension:
12142 block-declaration:
12143 __extension__ block-declaration
12145 C++0x Extension:
12147 block-declaration:
12148 static_assert-declaration
12150 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12151 part of a declaration-statement. */
12153 static void
12154 cp_parser_block_declaration (cp_parser *parser,
12155 bool statement_p)
12157 cp_token *token1;
12158 int saved_pedantic;
12160 /* Check for the `__extension__' keyword. */
12161 if (cp_parser_extension_opt (parser, &saved_pedantic))
12163 /* Parse the qualified declaration. */
12164 cp_parser_block_declaration (parser, statement_p);
12165 /* Restore the PEDANTIC flag. */
12166 pedantic = saved_pedantic;
12168 return;
12171 /* Peek at the next token to figure out which kind of declaration is
12172 present. */
12173 token1 = cp_lexer_peek_token (parser->lexer);
12175 /* If the next keyword is `asm', we have an asm-definition. */
12176 if (token1->keyword == RID_ASM)
12178 if (statement_p)
12179 cp_parser_commit_to_tentative_parse (parser);
12180 cp_parser_asm_definition (parser);
12182 /* If the next keyword is `namespace', we have a
12183 namespace-alias-definition. */
12184 else if (token1->keyword == RID_NAMESPACE)
12185 cp_parser_namespace_alias_definition (parser);
12186 /* If the next keyword is `using', we have a
12187 using-declaration, a using-directive, or an alias-declaration. */
12188 else if (token1->keyword == RID_USING)
12190 cp_token *token2;
12192 if (statement_p)
12193 cp_parser_commit_to_tentative_parse (parser);
12194 /* If the token after `using' is `namespace', then we have a
12195 using-directive. */
12196 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12197 if (token2->keyword == RID_NAMESPACE)
12198 cp_parser_using_directive (parser);
12199 /* If the second token after 'using' is '=', then we have an
12200 alias-declaration. */
12201 else if (cxx_dialect >= cxx11
12202 && token2->type == CPP_NAME
12203 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12204 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12205 cp_parser_alias_declaration (parser);
12206 /* Otherwise, it's a using-declaration. */
12207 else
12208 cp_parser_using_declaration (parser,
12209 /*access_declaration_p=*/false);
12211 /* If the next keyword is `__label__' we have a misplaced label
12212 declaration. */
12213 else if (token1->keyword == RID_LABEL)
12215 cp_lexer_consume_token (parser->lexer);
12216 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12217 cp_parser_skip_to_end_of_statement (parser);
12218 /* If the next token is now a `;', consume it. */
12219 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12220 cp_lexer_consume_token (parser->lexer);
12222 /* If the next token is `static_assert' we have a static assertion. */
12223 else if (token1->keyword == RID_STATIC_ASSERT)
12224 cp_parser_static_assert (parser, /*member_p=*/false);
12225 /* Anything else must be a simple-declaration. */
12226 else
12227 cp_parser_simple_declaration (parser, !statement_p,
12228 /*maybe_range_for_decl*/NULL);
12231 /* Parse a simple-declaration.
12233 simple-declaration:
12234 decl-specifier-seq [opt] init-declarator-list [opt] ;
12236 init-declarator-list:
12237 init-declarator
12238 init-declarator-list , init-declarator
12240 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12241 function-definition as a simple-declaration.
12243 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12244 parsed declaration if it is an uninitialized single declarator not followed
12245 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12246 if present, will not be consumed. */
12248 static void
12249 cp_parser_simple_declaration (cp_parser* parser,
12250 bool function_definition_allowed_p,
12251 tree *maybe_range_for_decl)
12253 cp_decl_specifier_seq decl_specifiers;
12254 int declares_class_or_enum;
12255 bool saw_declarator;
12256 location_t comma_loc = UNKNOWN_LOCATION;
12257 location_t init_loc = UNKNOWN_LOCATION;
12259 if (maybe_range_for_decl)
12260 *maybe_range_for_decl = NULL_TREE;
12262 /* Defer access checks until we know what is being declared; the
12263 checks for names appearing in the decl-specifier-seq should be
12264 done as if we were in the scope of the thing being declared. */
12265 push_deferring_access_checks (dk_deferred);
12267 /* Parse the decl-specifier-seq. We have to keep track of whether
12268 or not the decl-specifier-seq declares a named class or
12269 enumeration type, since that is the only case in which the
12270 init-declarator-list is allowed to be empty.
12272 [dcl.dcl]
12274 In a simple-declaration, the optional init-declarator-list can be
12275 omitted only when declaring a class or enumeration, that is when
12276 the decl-specifier-seq contains either a class-specifier, an
12277 elaborated-type-specifier, or an enum-specifier. */
12278 cp_parser_decl_specifier_seq (parser,
12279 CP_PARSER_FLAGS_OPTIONAL,
12280 &decl_specifiers,
12281 &declares_class_or_enum);
12282 /* We no longer need to defer access checks. */
12283 stop_deferring_access_checks ();
12285 /* In a block scope, a valid declaration must always have a
12286 decl-specifier-seq. By not trying to parse declarators, we can
12287 resolve the declaration/expression ambiguity more quickly. */
12288 if (!function_definition_allowed_p
12289 && !decl_specifiers.any_specifiers_p)
12291 cp_parser_error (parser, "expected declaration");
12292 goto done;
12295 /* If the next two tokens are both identifiers, the code is
12296 erroneous. The usual cause of this situation is code like:
12298 T t;
12300 where "T" should name a type -- but does not. */
12301 if (!decl_specifiers.any_type_specifiers_p
12302 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12304 /* If parsing tentatively, we should commit; we really are
12305 looking at a declaration. */
12306 cp_parser_commit_to_tentative_parse (parser);
12307 /* Give up. */
12308 goto done;
12311 /* If we have seen at least one decl-specifier, and the next token
12312 is not a parenthesis, then we must be looking at a declaration.
12313 (After "int (" we might be looking at a functional cast.) */
12314 if (decl_specifiers.any_specifiers_p
12315 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12316 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12317 && !cp_parser_error_occurred (parser))
12318 cp_parser_commit_to_tentative_parse (parser);
12320 tree last_type, auto_node;
12322 last_type = NULL_TREE;
12323 auto_node = type_uses_auto (decl_specifiers.type);
12325 /* Keep going until we hit the `;' at the end of the simple
12326 declaration. */
12327 saw_declarator = false;
12328 while (cp_lexer_next_token_is_not (parser->lexer,
12329 CPP_SEMICOLON))
12331 cp_token *token;
12332 bool function_definition_p;
12333 tree decl;
12335 if (saw_declarator)
12337 /* If we are processing next declarator, comma is expected */
12338 token = cp_lexer_peek_token (parser->lexer);
12339 gcc_assert (token->type == CPP_COMMA);
12340 cp_lexer_consume_token (parser->lexer);
12341 if (maybe_range_for_decl)
12343 *maybe_range_for_decl = error_mark_node;
12344 if (comma_loc == UNKNOWN_LOCATION)
12345 comma_loc = token->location;
12348 else
12349 saw_declarator = true;
12351 /* Parse the init-declarator. */
12352 decl = cp_parser_init_declarator (parser, &decl_specifiers,
12353 /*checks=*/NULL,
12354 function_definition_allowed_p,
12355 /*member_p=*/false,
12356 declares_class_or_enum,
12357 &function_definition_p,
12358 maybe_range_for_decl,
12359 &init_loc);
12360 /* If an error occurred while parsing tentatively, exit quickly.
12361 (That usually happens when in the body of a function; each
12362 statement is treated as a declaration-statement until proven
12363 otherwise.) */
12364 if (cp_parser_error_occurred (parser))
12365 goto done;
12367 if (auto_node)
12369 tree type = TREE_TYPE (decl);
12370 if (last_type && !same_type_p (type, last_type))
12372 /* If the list of declarators contains more than one declarator,
12373 the type of each declared variable is determined as described
12374 above. If the type deduced for the template parameter U is not
12375 the same in each deduction, the program is ill-formed. */
12376 error_at (decl_specifiers.locations[ds_type_spec],
12377 "inconsistent deduction for %qT: %qT and then %qT",
12378 decl_specifiers.type, last_type, type);
12379 auto_node = NULL_TREE;
12381 last_type = type;
12384 /* Handle function definitions specially. */
12385 if (function_definition_p)
12387 /* If the next token is a `,', then we are probably
12388 processing something like:
12390 void f() {}, *p;
12392 which is erroneous. */
12393 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12395 cp_token *token = cp_lexer_peek_token (parser->lexer);
12396 error_at (token->location,
12397 "mixing"
12398 " declarations and function-definitions is forbidden");
12400 /* Otherwise, we're done with the list of declarators. */
12401 else
12403 pop_deferring_access_checks ();
12404 return;
12407 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
12408 *maybe_range_for_decl = decl;
12409 /* The next token should be either a `,' or a `;'. */
12410 token = cp_lexer_peek_token (parser->lexer);
12411 /* If it's a `,', there are more declarators to come. */
12412 if (token->type == CPP_COMMA)
12413 /* will be consumed next time around */;
12414 /* If it's a `;', we are done. */
12415 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12416 break;
12417 /* Anything else is an error. */
12418 else
12420 /* If we have already issued an error message we don't need
12421 to issue another one. */
12422 if ((decl != error_mark_node
12423 && DECL_INITIAL (decl) != error_mark_node)
12424 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12425 cp_parser_error (parser, "expected %<,%> or %<;%>");
12426 /* Skip tokens until we reach the end of the statement. */
12427 cp_parser_skip_to_end_of_statement (parser);
12428 /* If the next token is now a `;', consume it. */
12429 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12430 cp_lexer_consume_token (parser->lexer);
12431 goto done;
12433 /* After the first time around, a function-definition is not
12434 allowed -- even if it was OK at first. For example:
12436 int i, f() {}
12438 is not valid. */
12439 function_definition_allowed_p = false;
12442 /* Issue an error message if no declarators are present, and the
12443 decl-specifier-seq does not itself declare a class or
12444 enumeration: [dcl.dcl]/3. */
12445 if (!saw_declarator)
12447 if (cp_parser_declares_only_class_p (parser))
12449 if (!declares_class_or_enum
12450 && decl_specifiers.type
12451 && OVERLOAD_TYPE_P (decl_specifiers.type))
12452 /* Ensure an error is issued anyway when finish_decltype_type,
12453 called via cp_parser_decl_specifier_seq, returns a class or
12454 an enumeration (c++/51786). */
12455 decl_specifiers.type = NULL_TREE;
12456 shadow_tag (&decl_specifiers);
12458 /* Perform any deferred access checks. */
12459 perform_deferred_access_checks (tf_warning_or_error);
12462 /* Consume the `;'. */
12463 if (!maybe_range_for_decl)
12464 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12465 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12467 if (init_loc != UNKNOWN_LOCATION)
12468 error_at (init_loc, "initializer in range-based %<for%> loop");
12469 if (comma_loc != UNKNOWN_LOCATION)
12470 error_at (comma_loc,
12471 "multiple declarations in range-based %<for%> loop");
12474 done:
12475 pop_deferring_access_checks ();
12478 /* Parse a decl-specifier-seq.
12480 decl-specifier-seq:
12481 decl-specifier-seq [opt] decl-specifier
12482 decl-specifier attribute-specifier-seq [opt] (C++11)
12484 decl-specifier:
12485 storage-class-specifier
12486 type-specifier
12487 function-specifier
12488 friend
12489 typedef
12491 GNU Extension:
12493 decl-specifier:
12494 attributes
12496 Concepts Extension:
12498 decl-specifier:
12499 concept
12501 Set *DECL_SPECS to a representation of the decl-specifier-seq.
12503 The parser flags FLAGS is used to control type-specifier parsing.
12505 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
12506 flags:
12508 1: one of the decl-specifiers is an elaborated-type-specifier
12509 (i.e., a type declaration)
12510 2: one of the decl-specifiers is an enum-specifier or a
12511 class-specifier (i.e., a type definition)
12515 static void
12516 cp_parser_decl_specifier_seq (cp_parser* parser,
12517 cp_parser_flags flags,
12518 cp_decl_specifier_seq *decl_specs,
12519 int* declares_class_or_enum)
12521 bool constructor_possible_p = !parser->in_declarator_p;
12522 bool found_decl_spec = false;
12523 cp_token *start_token = NULL;
12524 cp_decl_spec ds;
12526 /* Clear DECL_SPECS. */
12527 clear_decl_specs (decl_specs);
12529 /* Assume no class or enumeration type is declared. */
12530 *declares_class_or_enum = 0;
12532 /* Keep reading specifiers until there are no more to read. */
12533 while (true)
12535 bool constructor_p;
12536 cp_token *token;
12537 ds = ds_last;
12539 /* Peek at the next token. */
12540 token = cp_lexer_peek_token (parser->lexer);
12542 /* Save the first token of the decl spec list for error
12543 reporting. */
12544 if (!start_token)
12545 start_token = token;
12546 /* Handle attributes. */
12547 if (cp_next_tokens_can_be_attribute_p (parser))
12549 /* Parse the attributes. */
12550 tree attrs = cp_parser_attributes_opt (parser);
12552 /* In a sequence of declaration specifiers, c++11 attributes
12553 appertain to the type that precede them. In that case
12554 [dcl.spec]/1 says:
12556 The attribute-specifier-seq affects the type only for
12557 the declaration it appears in, not other declarations
12558 involving the same type.
12560 But for now let's force the user to position the
12561 attribute either at the beginning of the declaration or
12562 after the declarator-id, which would clearly mean that it
12563 applies to the declarator. */
12564 if (cxx11_attribute_p (attrs))
12566 if (!found_decl_spec)
12567 /* The c++11 attribute is at the beginning of the
12568 declaration. It appertains to the entity being
12569 declared. */;
12570 else
12572 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
12574 /* This is an attribute following a
12575 class-specifier. */
12576 if (decl_specs->type_definition_p)
12577 warn_misplaced_attr_for_class_type (token->location,
12578 decl_specs->type);
12579 attrs = NULL_TREE;
12581 else
12583 decl_specs->std_attributes
12584 = chainon (decl_specs->std_attributes,
12585 attrs);
12586 if (decl_specs->locations[ds_std_attribute] == 0)
12587 decl_specs->locations[ds_std_attribute] = token->location;
12589 continue;
12593 decl_specs->attributes
12594 = chainon (decl_specs->attributes,
12595 attrs);
12596 if (decl_specs->locations[ds_attribute] == 0)
12597 decl_specs->locations[ds_attribute] = token->location;
12598 continue;
12600 /* Assume we will find a decl-specifier keyword. */
12601 found_decl_spec = true;
12602 /* If the next token is an appropriate keyword, we can simply
12603 add it to the list. */
12604 switch (token->keyword)
12606 /* decl-specifier:
12607 friend
12608 constexpr */
12609 case RID_FRIEND:
12610 if (!at_class_scope_p ())
12612 error_at (token->location, "%<friend%> used outside of class");
12613 cp_lexer_purge_token (parser->lexer);
12615 else
12617 ds = ds_friend;
12618 /* Consume the token. */
12619 cp_lexer_consume_token (parser->lexer);
12621 break;
12623 case RID_CONSTEXPR:
12624 ds = ds_constexpr;
12625 cp_lexer_consume_token (parser->lexer);
12626 break;
12628 case RID_CONCEPT:
12629 ds = ds_concept;
12630 cp_lexer_consume_token (parser->lexer);
12631 break;
12633 /* function-specifier:
12634 inline
12635 virtual
12636 explicit */
12637 case RID_INLINE:
12638 case RID_VIRTUAL:
12639 case RID_EXPLICIT:
12640 cp_parser_function_specifier_opt (parser, decl_specs);
12641 break;
12643 /* decl-specifier:
12644 typedef */
12645 case RID_TYPEDEF:
12646 ds = ds_typedef;
12647 /* Consume the token. */
12648 cp_lexer_consume_token (parser->lexer);
12649 /* A constructor declarator cannot appear in a typedef. */
12650 constructor_possible_p = false;
12651 /* The "typedef" keyword can only occur in a declaration; we
12652 may as well commit at this point. */
12653 cp_parser_commit_to_tentative_parse (parser);
12655 if (decl_specs->storage_class != sc_none)
12656 decl_specs->conflicting_specifiers_p = true;
12657 break;
12659 /* storage-class-specifier:
12660 auto
12661 register
12662 static
12663 extern
12664 mutable
12666 GNU Extension:
12667 thread */
12668 case RID_AUTO:
12669 if (cxx_dialect == cxx98)
12671 /* Consume the token. */
12672 cp_lexer_consume_token (parser->lexer);
12674 /* Complain about `auto' as a storage specifier, if
12675 we're complaining about C++0x compatibility. */
12676 warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
12677 " changes meaning in C++11; please remove it");
12679 /* Set the storage class anyway. */
12680 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
12681 token);
12683 else
12684 /* C++0x auto type-specifier. */
12685 found_decl_spec = false;
12686 break;
12688 case RID_REGISTER:
12689 case RID_STATIC:
12690 case RID_EXTERN:
12691 case RID_MUTABLE:
12692 /* Consume the token. */
12693 cp_lexer_consume_token (parser->lexer);
12694 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
12695 token);
12696 break;
12697 case RID_THREAD:
12698 /* Consume the token. */
12699 ds = ds_thread;
12700 cp_lexer_consume_token (parser->lexer);
12701 break;
12703 default:
12704 /* We did not yet find a decl-specifier yet. */
12705 found_decl_spec = false;
12706 break;
12709 if (found_decl_spec
12710 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
12711 && token->keyword != RID_CONSTEXPR)
12712 error ("decl-specifier invalid in condition");
12714 if (ds != ds_last)
12715 set_and_check_decl_spec_loc (decl_specs, ds, token);
12717 /* Constructors are a special case. The `S' in `S()' is not a
12718 decl-specifier; it is the beginning of the declarator. */
12719 constructor_p
12720 = (!found_decl_spec
12721 && constructor_possible_p
12722 && (cp_parser_constructor_declarator_p
12723 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
12725 /* If we don't have a DECL_SPEC yet, then we must be looking at
12726 a type-specifier. */
12727 if (!found_decl_spec && !constructor_p)
12729 int decl_spec_declares_class_or_enum;
12730 bool is_cv_qualifier;
12731 tree type_spec;
12733 type_spec
12734 = cp_parser_type_specifier (parser, flags,
12735 decl_specs,
12736 /*is_declaration=*/true,
12737 &decl_spec_declares_class_or_enum,
12738 &is_cv_qualifier);
12739 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
12741 /* If this type-specifier referenced a user-defined type
12742 (a typedef, class-name, etc.), then we can't allow any
12743 more such type-specifiers henceforth.
12745 [dcl.spec]
12747 The longest sequence of decl-specifiers that could
12748 possibly be a type name is taken as the
12749 decl-specifier-seq of a declaration. The sequence shall
12750 be self-consistent as described below.
12752 [dcl.type]
12754 As a general rule, at most one type-specifier is allowed
12755 in the complete decl-specifier-seq of a declaration. The
12756 only exceptions are the following:
12758 -- const or volatile can be combined with any other
12759 type-specifier.
12761 -- signed or unsigned can be combined with char, long,
12762 short, or int.
12764 -- ..
12766 Example:
12768 typedef char* Pc;
12769 void g (const int Pc);
12771 Here, Pc is *not* part of the decl-specifier seq; it's
12772 the declarator. Therefore, once we see a type-specifier
12773 (other than a cv-qualifier), we forbid any additional
12774 user-defined types. We *do* still allow things like `int
12775 int' to be considered a decl-specifier-seq, and issue the
12776 error message later. */
12777 if (type_spec && !is_cv_qualifier)
12778 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12779 /* A constructor declarator cannot follow a type-specifier. */
12780 if (type_spec)
12782 constructor_possible_p = false;
12783 found_decl_spec = true;
12784 if (!is_cv_qualifier)
12785 decl_specs->any_type_specifiers_p = true;
12789 /* If we still do not have a DECL_SPEC, then there are no more
12790 decl-specifiers. */
12791 if (!found_decl_spec)
12792 break;
12794 decl_specs->any_specifiers_p = true;
12795 /* After we see one decl-specifier, further decl-specifiers are
12796 always optional. */
12797 flags |= CP_PARSER_FLAGS_OPTIONAL;
12800 /* Don't allow a friend specifier with a class definition. */
12801 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
12802 && (*declares_class_or_enum & 2))
12803 error_at (decl_specs->locations[ds_friend],
12804 "class definition may not be declared a friend");
12807 /* Parse an (optional) storage-class-specifier.
12809 storage-class-specifier:
12810 auto
12811 register
12812 static
12813 extern
12814 mutable
12816 GNU Extension:
12818 storage-class-specifier:
12819 thread
12821 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
12823 static tree
12824 cp_parser_storage_class_specifier_opt (cp_parser* parser)
12826 switch (cp_lexer_peek_token (parser->lexer)->keyword)
12828 case RID_AUTO:
12829 if (cxx_dialect != cxx98)
12830 return NULL_TREE;
12831 /* Fall through for C++98. */
12833 case RID_REGISTER:
12834 case RID_STATIC:
12835 case RID_EXTERN:
12836 case RID_MUTABLE:
12837 case RID_THREAD:
12838 /* Consume the token. */
12839 return cp_lexer_consume_token (parser->lexer)->u.value;
12841 default:
12842 return NULL_TREE;
12846 /* Parse an (optional) function-specifier.
12848 function-specifier:
12849 inline
12850 virtual
12851 explicit
12853 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12854 Updates DECL_SPECS, if it is non-NULL. */
12856 static tree
12857 cp_parser_function_specifier_opt (cp_parser* parser,
12858 cp_decl_specifier_seq *decl_specs)
12860 cp_token *token = cp_lexer_peek_token (parser->lexer);
12861 switch (token->keyword)
12863 case RID_INLINE:
12864 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12865 break;
12867 case RID_VIRTUAL:
12868 /* 14.5.2.3 [temp.mem]
12870 A member function template shall not be virtual. */
12871 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12872 error_at (token->location, "templates may not be %<virtual%>");
12873 else
12874 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12875 break;
12877 case RID_EXPLICIT:
12878 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12879 break;
12881 default:
12882 return NULL_TREE;
12885 /* Consume the token. */
12886 return cp_lexer_consume_token (parser->lexer)->u.value;
12889 /* Parse a linkage-specification.
12891 linkage-specification:
12892 extern string-literal { declaration-seq [opt] }
12893 extern string-literal declaration */
12895 static void
12896 cp_parser_linkage_specification (cp_parser* parser)
12898 tree linkage;
12900 /* Look for the `extern' keyword. */
12901 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12903 /* Look for the string-literal. */
12904 linkage = cp_parser_string_literal (parser, false, false);
12906 /* Transform the literal into an identifier. If the literal is a
12907 wide-character string, or contains embedded NULs, then we can't
12908 handle it as the user wants. */
12909 if (strlen (TREE_STRING_POINTER (linkage))
12910 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12912 cp_parser_error (parser, "invalid linkage-specification");
12913 /* Assume C++ linkage. */
12914 linkage = lang_name_cplusplus;
12916 else
12917 linkage = get_identifier (TREE_STRING_POINTER (linkage));
12919 /* We're now using the new linkage. */
12920 push_lang_context (linkage);
12922 /* If the next token is a `{', then we're using the first
12923 production. */
12924 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12926 cp_ensure_no_omp_declare_simd (parser);
12927 cp_ensure_no_oacc_routine (parser);
12929 /* Consume the `{' token. */
12930 cp_lexer_consume_token (parser->lexer);
12931 /* Parse the declarations. */
12932 cp_parser_declaration_seq_opt (parser);
12933 /* Look for the closing `}'. */
12934 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12936 /* Otherwise, there's just one declaration. */
12937 else
12939 bool saved_in_unbraced_linkage_specification_p;
12941 saved_in_unbraced_linkage_specification_p
12942 = parser->in_unbraced_linkage_specification_p;
12943 parser->in_unbraced_linkage_specification_p = true;
12944 cp_parser_declaration (parser);
12945 parser->in_unbraced_linkage_specification_p
12946 = saved_in_unbraced_linkage_specification_p;
12949 /* We're done with the linkage-specification. */
12950 pop_lang_context ();
12953 /* Parse a static_assert-declaration.
12955 static_assert-declaration:
12956 static_assert ( constant-expression , string-literal ) ;
12957 static_assert ( constant-expression ) ; (C++1Z)
12959 If MEMBER_P, this static_assert is a class member. */
12961 static void
12962 cp_parser_static_assert(cp_parser *parser, bool member_p)
12964 tree condition;
12965 tree message;
12966 cp_token *token;
12967 location_t saved_loc;
12968 bool dummy;
12970 /* Peek at the `static_assert' token so we can keep track of exactly
12971 where the static assertion started. */
12972 token = cp_lexer_peek_token (parser->lexer);
12973 saved_loc = token->location;
12975 /* Look for the `static_assert' keyword. */
12976 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
12977 RT_STATIC_ASSERT))
12978 return;
12980 /* We know we are in a static assertion; commit to any tentative
12981 parse. */
12982 if (cp_parser_parsing_tentatively (parser))
12983 cp_parser_commit_to_tentative_parse (parser);
12985 /* Parse the `(' starting the static assertion condition. */
12986 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12988 /* Parse the constant-expression. Allow a non-constant expression
12989 here in order to give better diagnostics in finish_static_assert. */
12990 condition =
12991 cp_parser_constant_expression (parser,
12992 /*allow_non_constant_p=*/true,
12993 /*non_constant_p=*/&dummy);
12995 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12997 if (cxx_dialect < cxx1z)
12998 pedwarn (input_location, OPT_Wpedantic,
12999 "static_assert without a message "
13000 "only available with -std=c++1z or -std=gnu++1z");
13001 /* Eat the ')' */
13002 cp_lexer_consume_token (parser->lexer);
13003 message = build_string (1, "");
13004 TREE_TYPE (message) = char_array_type_node;
13005 fix_string_type (message);
13007 else
13009 /* Parse the separating `,'. */
13010 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13012 /* Parse the string-literal message. */
13013 message = cp_parser_string_literal (parser,
13014 /*translate=*/false,
13015 /*wide_ok=*/true);
13017 /* A `)' completes the static assertion. */
13018 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13019 cp_parser_skip_to_closing_parenthesis (parser,
13020 /*recovering=*/true,
13021 /*or_comma=*/false,
13022 /*consume_paren=*/true);
13025 /* A semicolon terminates the declaration. */
13026 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13028 /* Complete the static assertion, which may mean either processing
13029 the static assert now or saving it for template instantiation. */
13030 finish_static_assert (condition, message, saved_loc, member_p);
13033 /* Parse the expression in decltype ( expression ). */
13035 static tree
13036 cp_parser_decltype_expr (cp_parser *parser,
13037 bool &id_expression_or_member_access_p)
13039 cp_token *id_expr_start_token;
13040 tree expr;
13042 /* Since we're going to preserve any side-effects from this parse, set up a
13043 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13044 in the expression. */
13045 tentative_firewall firewall (parser);
13047 /* First, try parsing an id-expression. */
13048 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13049 cp_parser_parse_tentatively (parser);
13050 expr = cp_parser_id_expression (parser,
13051 /*template_keyword_p=*/false,
13052 /*check_dependency_p=*/true,
13053 /*template_p=*/NULL,
13054 /*declarator_p=*/false,
13055 /*optional_p=*/false);
13057 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13059 bool non_integral_constant_expression_p = false;
13060 tree id_expression = expr;
13061 cp_id_kind idk;
13062 const char *error_msg;
13064 if (identifier_p (expr))
13065 /* Lookup the name we got back from the id-expression. */
13066 expr = cp_parser_lookup_name_simple (parser, expr,
13067 id_expr_start_token->location);
13069 if (expr
13070 && expr != error_mark_node
13071 && TREE_CODE (expr) != TYPE_DECL
13072 && (TREE_CODE (expr) != BIT_NOT_EXPR
13073 || !TYPE_P (TREE_OPERAND (expr, 0)))
13074 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13076 /* Complete lookup of the id-expression. */
13077 expr = (finish_id_expression
13078 (id_expression, expr, parser->scope, &idk,
13079 /*integral_constant_expression_p=*/false,
13080 /*allow_non_integral_constant_expression_p=*/true,
13081 &non_integral_constant_expression_p,
13082 /*template_p=*/false,
13083 /*done=*/true,
13084 /*address_p=*/false,
13085 /*template_arg_p=*/false,
13086 &error_msg,
13087 id_expr_start_token->location));
13089 if (expr == error_mark_node)
13090 /* We found an id-expression, but it was something that we
13091 should not have found. This is an error, not something
13092 we can recover from, so note that we found an
13093 id-expression and we'll recover as gracefully as
13094 possible. */
13095 id_expression_or_member_access_p = true;
13098 if (expr
13099 && expr != error_mark_node
13100 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13101 /* We have an id-expression. */
13102 id_expression_or_member_access_p = true;
13105 if (!id_expression_or_member_access_p)
13107 /* Abort the id-expression parse. */
13108 cp_parser_abort_tentative_parse (parser);
13110 /* Parsing tentatively, again. */
13111 cp_parser_parse_tentatively (parser);
13113 /* Parse a class member access. */
13114 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13115 /*cast_p=*/false, /*decltype*/true,
13116 /*member_access_only_p=*/true, NULL);
13118 if (expr
13119 && expr != error_mark_node
13120 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13121 /* We have an id-expression. */
13122 id_expression_or_member_access_p = true;
13125 if (id_expression_or_member_access_p)
13126 /* We have parsed the complete id-expression or member access. */
13127 cp_parser_parse_definitely (parser);
13128 else
13130 /* Abort our attempt to parse an id-expression or member access
13131 expression. */
13132 cp_parser_abort_tentative_parse (parser);
13134 /* Parse a full expression. */
13135 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
13136 /*decltype_p=*/true);
13139 return expr;
13142 /* Parse a `decltype' type. Returns the type.
13144 simple-type-specifier:
13145 decltype ( expression )
13146 C++14 proposal:
13147 decltype ( auto ) */
13149 static tree
13150 cp_parser_decltype (cp_parser *parser)
13152 tree expr;
13153 bool id_expression_or_member_access_p = false;
13154 const char *saved_message;
13155 bool saved_integral_constant_expression_p;
13156 bool saved_non_integral_constant_expression_p;
13157 bool saved_greater_than_is_operator_p;
13158 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13160 if (start_token->type == CPP_DECLTYPE)
13162 /* Already parsed. */
13163 cp_lexer_consume_token (parser->lexer);
13164 return saved_checks_value (start_token->u.tree_check_value);
13167 /* Look for the `decltype' token. */
13168 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
13169 return error_mark_node;
13171 /* Parse the opening `('. */
13172 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13173 return error_mark_node;
13175 /* decltype (auto) */
13176 if (cxx_dialect >= cxx14
13177 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
13179 cp_lexer_consume_token (parser->lexer);
13180 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13181 return error_mark_node;
13182 expr = make_decltype_auto ();
13183 AUTO_IS_DECLTYPE (expr) = true;
13184 goto rewrite;
13187 /* Types cannot be defined in a `decltype' expression. Save away the
13188 old message. */
13189 saved_message = parser->type_definition_forbidden_message;
13191 /* And create the new one. */
13192 parser->type_definition_forbidden_message
13193 = G_("types may not be defined in %<decltype%> expressions");
13195 /* The restrictions on constant-expressions do not apply inside
13196 decltype expressions. */
13197 saved_integral_constant_expression_p
13198 = parser->integral_constant_expression_p;
13199 saved_non_integral_constant_expression_p
13200 = parser->non_integral_constant_expression_p;
13201 parser->integral_constant_expression_p = false;
13203 /* Within a parenthesized expression, a `>' token is always
13204 the greater-than operator. */
13205 saved_greater_than_is_operator_p
13206 = parser->greater_than_is_operator_p;
13207 parser->greater_than_is_operator_p = true;
13209 /* Do not actually evaluate the expression. */
13210 ++cp_unevaluated_operand;
13212 /* Do not warn about problems with the expression. */
13213 ++c_inhibit_evaluation_warnings;
13215 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
13217 /* Go back to evaluating expressions. */
13218 --cp_unevaluated_operand;
13219 --c_inhibit_evaluation_warnings;
13221 /* The `>' token might be the end of a template-id or
13222 template-parameter-list now. */
13223 parser->greater_than_is_operator_p
13224 = saved_greater_than_is_operator_p;
13226 /* Restore the old message and the integral constant expression
13227 flags. */
13228 parser->type_definition_forbidden_message = saved_message;
13229 parser->integral_constant_expression_p
13230 = saved_integral_constant_expression_p;
13231 parser->non_integral_constant_expression_p
13232 = saved_non_integral_constant_expression_p;
13234 /* Parse to the closing `)'. */
13235 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13237 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13238 /*consume_paren=*/true);
13239 return error_mark_node;
13242 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
13243 tf_warning_or_error);
13245 rewrite:
13246 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
13247 it again. */
13248 start_token->type = CPP_DECLTYPE;
13249 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13250 start_token->u.tree_check_value->value = expr;
13251 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
13252 start_token->keyword = RID_MAX;
13253 cp_lexer_purge_tokens_after (parser->lexer, start_token);
13255 return expr;
13258 /* Special member functions [gram.special] */
13260 /* Parse a conversion-function-id.
13262 conversion-function-id:
13263 operator conversion-type-id
13265 Returns an IDENTIFIER_NODE representing the operator. */
13267 static tree
13268 cp_parser_conversion_function_id (cp_parser* parser)
13270 tree type;
13271 tree saved_scope;
13272 tree saved_qualifying_scope;
13273 tree saved_object_scope;
13274 tree pushed_scope = NULL_TREE;
13276 /* Look for the `operator' token. */
13277 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13278 return error_mark_node;
13279 /* When we parse the conversion-type-id, the current scope will be
13280 reset. However, we need that information in able to look up the
13281 conversion function later, so we save it here. */
13282 saved_scope = parser->scope;
13283 saved_qualifying_scope = parser->qualifying_scope;
13284 saved_object_scope = parser->object_scope;
13285 /* We must enter the scope of the class so that the names of
13286 entities declared within the class are available in the
13287 conversion-type-id. For example, consider:
13289 struct S {
13290 typedef int I;
13291 operator I();
13294 S::operator I() { ... }
13296 In order to see that `I' is a type-name in the definition, we
13297 must be in the scope of `S'. */
13298 if (saved_scope)
13299 pushed_scope = push_scope (saved_scope);
13300 /* Parse the conversion-type-id. */
13301 type = cp_parser_conversion_type_id (parser);
13302 /* Leave the scope of the class, if any. */
13303 if (pushed_scope)
13304 pop_scope (pushed_scope);
13305 /* Restore the saved scope. */
13306 parser->scope = saved_scope;
13307 parser->qualifying_scope = saved_qualifying_scope;
13308 parser->object_scope = saved_object_scope;
13309 /* If the TYPE is invalid, indicate failure. */
13310 if (type == error_mark_node)
13311 return error_mark_node;
13312 return mangle_conv_op_name_for_type (type);
13315 /* Parse a conversion-type-id:
13317 conversion-type-id:
13318 type-specifier-seq conversion-declarator [opt]
13320 Returns the TYPE specified. */
13322 static tree
13323 cp_parser_conversion_type_id (cp_parser* parser)
13325 tree attributes;
13326 cp_decl_specifier_seq type_specifiers;
13327 cp_declarator *declarator;
13328 tree type_specified;
13329 const char *saved_message;
13331 /* Parse the attributes. */
13332 attributes = cp_parser_attributes_opt (parser);
13334 saved_message = parser->type_definition_forbidden_message;
13335 parser->type_definition_forbidden_message
13336 = G_("types may not be defined in a conversion-type-id");
13338 /* Parse the type-specifiers. */
13339 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13340 /*is_trailing_return=*/false,
13341 &type_specifiers);
13343 parser->type_definition_forbidden_message = saved_message;
13345 /* If that didn't work, stop. */
13346 if (type_specifiers.type == error_mark_node)
13347 return error_mark_node;
13348 /* Parse the conversion-declarator. */
13349 declarator = cp_parser_conversion_declarator_opt (parser);
13351 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
13352 /*initialized=*/0, &attributes);
13353 if (attributes)
13354 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
13356 /* Don't give this error when parsing tentatively. This happens to
13357 work because we always parse this definitively once. */
13358 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
13359 && type_uses_auto (type_specified))
13361 if (cxx_dialect < cxx14)
13363 error ("invalid use of %<auto%> in conversion operator");
13364 return error_mark_node;
13366 else if (template_parm_scope_p ())
13367 warning (0, "use of %<auto%> in member template "
13368 "conversion operator can never be deduced");
13371 return type_specified;
13374 /* Parse an (optional) conversion-declarator.
13376 conversion-declarator:
13377 ptr-operator conversion-declarator [opt]
13381 static cp_declarator *
13382 cp_parser_conversion_declarator_opt (cp_parser* parser)
13384 enum tree_code code;
13385 tree class_type, std_attributes = NULL_TREE;
13386 cp_cv_quals cv_quals;
13388 /* We don't know if there's a ptr-operator next, or not. */
13389 cp_parser_parse_tentatively (parser);
13390 /* Try the ptr-operator. */
13391 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
13392 &std_attributes);
13393 /* If it worked, look for more conversion-declarators. */
13394 if (cp_parser_parse_definitely (parser))
13396 cp_declarator *declarator;
13398 /* Parse another optional declarator. */
13399 declarator = cp_parser_conversion_declarator_opt (parser);
13401 declarator = cp_parser_make_indirect_declarator
13402 (code, class_type, cv_quals, declarator, std_attributes);
13404 return declarator;
13407 return NULL;
13410 /* Parse an (optional) ctor-initializer.
13412 ctor-initializer:
13413 : mem-initializer-list
13415 Returns TRUE iff the ctor-initializer was actually present. */
13417 static bool
13418 cp_parser_ctor_initializer_opt (cp_parser* parser)
13420 /* If the next token is not a `:', then there is no
13421 ctor-initializer. */
13422 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13424 /* Do default initialization of any bases and members. */
13425 if (DECL_CONSTRUCTOR_P (current_function_decl))
13426 finish_mem_initializers (NULL_TREE);
13428 return false;
13431 /* Consume the `:' token. */
13432 cp_lexer_consume_token (parser->lexer);
13433 /* And the mem-initializer-list. */
13434 cp_parser_mem_initializer_list (parser);
13436 return true;
13439 /* Parse a mem-initializer-list.
13441 mem-initializer-list:
13442 mem-initializer ... [opt]
13443 mem-initializer ... [opt] , mem-initializer-list */
13445 static void
13446 cp_parser_mem_initializer_list (cp_parser* parser)
13448 tree mem_initializer_list = NULL_TREE;
13449 tree target_ctor = error_mark_node;
13450 cp_token *token = cp_lexer_peek_token (parser->lexer);
13452 /* Let the semantic analysis code know that we are starting the
13453 mem-initializer-list. */
13454 if (!DECL_CONSTRUCTOR_P (current_function_decl))
13455 error_at (token->location,
13456 "only constructors take member initializers");
13458 /* Loop through the list. */
13459 while (true)
13461 tree mem_initializer;
13463 token = cp_lexer_peek_token (parser->lexer);
13464 /* Parse the mem-initializer. */
13465 mem_initializer = cp_parser_mem_initializer (parser);
13466 /* If the next token is a `...', we're expanding member initializers. */
13467 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13469 /* Consume the `...'. */
13470 cp_lexer_consume_token (parser->lexer);
13472 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
13473 can be expanded but members cannot. */
13474 if (mem_initializer != error_mark_node
13475 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
13477 error_at (token->location,
13478 "cannot expand initializer for member %<%D%>",
13479 TREE_PURPOSE (mem_initializer));
13480 mem_initializer = error_mark_node;
13483 /* Construct the pack expansion type. */
13484 if (mem_initializer != error_mark_node)
13485 mem_initializer = make_pack_expansion (mem_initializer);
13487 if (target_ctor != error_mark_node
13488 && mem_initializer != error_mark_node)
13490 error ("mem-initializer for %qD follows constructor delegation",
13491 TREE_PURPOSE (mem_initializer));
13492 mem_initializer = error_mark_node;
13494 /* Look for a target constructor. */
13495 if (mem_initializer != error_mark_node
13496 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
13497 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
13499 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
13500 if (mem_initializer_list)
13502 error ("constructor delegation follows mem-initializer for %qD",
13503 TREE_PURPOSE (mem_initializer_list));
13504 mem_initializer = error_mark_node;
13506 target_ctor = mem_initializer;
13508 /* Add it to the list, unless it was erroneous. */
13509 if (mem_initializer != error_mark_node)
13511 TREE_CHAIN (mem_initializer) = mem_initializer_list;
13512 mem_initializer_list = mem_initializer;
13514 /* If the next token is not a `,', we're done. */
13515 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13516 break;
13517 /* Consume the `,' token. */
13518 cp_lexer_consume_token (parser->lexer);
13521 /* Perform semantic analysis. */
13522 if (DECL_CONSTRUCTOR_P (current_function_decl))
13523 finish_mem_initializers (mem_initializer_list);
13526 /* Parse a mem-initializer.
13528 mem-initializer:
13529 mem-initializer-id ( expression-list [opt] )
13530 mem-initializer-id braced-init-list
13532 GNU extension:
13534 mem-initializer:
13535 ( expression-list [opt] )
13537 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
13538 class) or FIELD_DECL (for a non-static data member) to initialize;
13539 the TREE_VALUE is the expression-list. An empty initialization
13540 list is represented by void_list_node. */
13542 static tree
13543 cp_parser_mem_initializer (cp_parser* parser)
13545 tree mem_initializer_id;
13546 tree expression_list;
13547 tree member;
13548 cp_token *token = cp_lexer_peek_token (parser->lexer);
13550 /* Find out what is being initialized. */
13551 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
13553 permerror (token->location,
13554 "anachronistic old-style base class initializer");
13555 mem_initializer_id = NULL_TREE;
13557 else
13559 mem_initializer_id = cp_parser_mem_initializer_id (parser);
13560 if (mem_initializer_id == error_mark_node)
13561 return mem_initializer_id;
13563 member = expand_member_init (mem_initializer_id);
13564 if (member && !DECL_P (member))
13565 in_base_initializer = 1;
13567 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13569 bool expr_non_constant_p;
13570 cp_lexer_set_source_position (parser->lexer);
13571 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13572 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
13573 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
13574 expression_list = build_tree_list (NULL_TREE, expression_list);
13576 else
13578 vec<tree, va_gc> *vec;
13579 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
13580 /*cast_p=*/false,
13581 /*allow_expansion_p=*/true,
13582 /*non_constant_p=*/NULL);
13583 if (vec == NULL)
13584 return error_mark_node;
13585 expression_list = build_tree_list_vec (vec);
13586 release_tree_vector (vec);
13589 if (expression_list == error_mark_node)
13590 return error_mark_node;
13591 if (!expression_list)
13592 expression_list = void_type_node;
13594 in_base_initializer = 0;
13596 return member ? build_tree_list (member, expression_list) : error_mark_node;
13599 /* Parse a mem-initializer-id.
13601 mem-initializer-id:
13602 :: [opt] nested-name-specifier [opt] class-name
13603 decltype-specifier (C++11)
13604 identifier
13606 Returns a TYPE indicating the class to be initialized for the first
13607 production (and the second in C++11). Returns an IDENTIFIER_NODE
13608 indicating the data member to be initialized for the last production. */
13610 static tree
13611 cp_parser_mem_initializer_id (cp_parser* parser)
13613 bool global_scope_p;
13614 bool nested_name_specifier_p;
13615 bool template_p = false;
13616 tree id;
13618 cp_token *token = cp_lexer_peek_token (parser->lexer);
13620 /* `typename' is not allowed in this context ([temp.res]). */
13621 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13623 error_at (token->location,
13624 "keyword %<typename%> not allowed in this context (a qualified "
13625 "member initializer is implicitly a type)");
13626 cp_lexer_consume_token (parser->lexer);
13628 /* Look for the optional `::' operator. */
13629 global_scope_p
13630 = (cp_parser_global_scope_opt (parser,
13631 /*current_scope_valid_p=*/false)
13632 != NULL_TREE);
13633 /* Look for the optional nested-name-specifier. The simplest way to
13634 implement:
13636 [temp.res]
13638 The keyword `typename' is not permitted in a base-specifier or
13639 mem-initializer; in these contexts a qualified name that
13640 depends on a template-parameter is implicitly assumed to be a
13641 type name.
13643 is to assume that we have seen the `typename' keyword at this
13644 point. */
13645 nested_name_specifier_p
13646 = (cp_parser_nested_name_specifier_opt (parser,
13647 /*typename_keyword_p=*/true,
13648 /*check_dependency_p=*/true,
13649 /*type_p=*/true,
13650 /*is_declaration=*/true)
13651 != NULL_TREE);
13652 if (nested_name_specifier_p)
13653 template_p = cp_parser_optional_template_keyword (parser);
13654 /* If there is a `::' operator or a nested-name-specifier, then we
13655 are definitely looking for a class-name. */
13656 if (global_scope_p || nested_name_specifier_p)
13657 return cp_parser_class_name (parser,
13658 /*typename_keyword_p=*/true,
13659 /*template_keyword_p=*/template_p,
13660 typename_type,
13661 /*check_dependency_p=*/true,
13662 /*class_head_p=*/false,
13663 /*is_declaration=*/true);
13664 /* Otherwise, we could also be looking for an ordinary identifier. */
13665 cp_parser_parse_tentatively (parser);
13666 if (cp_lexer_next_token_is_decltype (parser->lexer))
13667 /* Try a decltype-specifier. */
13668 id = cp_parser_decltype (parser);
13669 else
13670 /* Otherwise, try a class-name. */
13671 id = cp_parser_class_name (parser,
13672 /*typename_keyword_p=*/true,
13673 /*template_keyword_p=*/false,
13674 none_type,
13675 /*check_dependency_p=*/true,
13676 /*class_head_p=*/false,
13677 /*is_declaration=*/true);
13678 /* If we found one, we're done. */
13679 if (cp_parser_parse_definitely (parser))
13680 return id;
13681 /* Otherwise, look for an ordinary identifier. */
13682 return cp_parser_identifier (parser);
13685 /* Overloading [gram.over] */
13687 /* Parse an operator-function-id.
13689 operator-function-id:
13690 operator operator
13692 Returns an IDENTIFIER_NODE for the operator which is a
13693 human-readable spelling of the identifier, e.g., `operator +'. */
13695 static cp_expr
13696 cp_parser_operator_function_id (cp_parser* parser)
13698 /* Look for the `operator' keyword. */
13699 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13700 return error_mark_node;
13701 /* And then the name of the operator itself. */
13702 return cp_parser_operator (parser);
13705 /* Return an identifier node for a user-defined literal operator.
13706 The suffix identifier is chained to the operator name identifier. */
13708 static tree
13709 cp_literal_operator_id (const char* name)
13711 tree identifier;
13712 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
13713 + strlen (name) + 10);
13714 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
13715 identifier = get_identifier (buffer);
13717 return identifier;
13720 /* Parse an operator.
13722 operator:
13723 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
13724 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
13725 || ++ -- , ->* -> () []
13727 GNU Extensions:
13729 operator:
13730 <? >? <?= >?=
13732 Returns an IDENTIFIER_NODE for the operator which is a
13733 human-readable spelling of the identifier, e.g., `operator +'. */
13735 static cp_expr
13736 cp_parser_operator (cp_parser* parser)
13738 tree id = NULL_TREE;
13739 cp_token *token;
13740 bool utf8 = false;
13742 /* Peek at the next token. */
13743 token = cp_lexer_peek_token (parser->lexer);
13745 location_t start_loc = token->location;
13747 /* Figure out which operator we have. */
13748 switch (token->type)
13750 case CPP_KEYWORD:
13752 enum tree_code op;
13754 /* The keyword should be either `new' or `delete'. */
13755 if (token->keyword == RID_NEW)
13756 op = NEW_EXPR;
13757 else if (token->keyword == RID_DELETE)
13758 op = DELETE_EXPR;
13759 else
13760 break;
13762 /* Consume the `new' or `delete' token. */
13763 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
13765 /* Peek at the next token. */
13766 token = cp_lexer_peek_token (parser->lexer);
13767 /* If it's a `[' token then this is the array variant of the
13768 operator. */
13769 if (token->type == CPP_OPEN_SQUARE)
13771 /* Consume the `[' token. */
13772 cp_lexer_consume_token (parser->lexer);
13773 /* Look for the `]' token. */
13774 end_loc = cp_parser_require (parser, CPP_CLOSE_SQUARE,
13775 RT_CLOSE_SQUARE)->location;
13776 id = ansi_opname (op == NEW_EXPR
13777 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
13779 /* Otherwise, we have the non-array variant. */
13780 else
13781 id = ansi_opname (op);
13783 location_t loc = make_location (start_loc, start_loc, end_loc);
13785 return cp_expr (id, loc);
13788 case CPP_PLUS:
13789 id = ansi_opname (PLUS_EXPR);
13790 break;
13792 case CPP_MINUS:
13793 id = ansi_opname (MINUS_EXPR);
13794 break;
13796 case CPP_MULT:
13797 id = ansi_opname (MULT_EXPR);
13798 break;
13800 case CPP_DIV:
13801 id = ansi_opname (TRUNC_DIV_EXPR);
13802 break;
13804 case CPP_MOD:
13805 id = ansi_opname (TRUNC_MOD_EXPR);
13806 break;
13808 case CPP_XOR:
13809 id = ansi_opname (BIT_XOR_EXPR);
13810 break;
13812 case CPP_AND:
13813 id = ansi_opname (BIT_AND_EXPR);
13814 break;
13816 case CPP_OR:
13817 id = ansi_opname (BIT_IOR_EXPR);
13818 break;
13820 case CPP_COMPL:
13821 id = ansi_opname (BIT_NOT_EXPR);
13822 break;
13824 case CPP_NOT:
13825 id = ansi_opname (TRUTH_NOT_EXPR);
13826 break;
13828 case CPP_EQ:
13829 id = ansi_assopname (NOP_EXPR);
13830 break;
13832 case CPP_LESS:
13833 id = ansi_opname (LT_EXPR);
13834 break;
13836 case CPP_GREATER:
13837 id = ansi_opname (GT_EXPR);
13838 break;
13840 case CPP_PLUS_EQ:
13841 id = ansi_assopname (PLUS_EXPR);
13842 break;
13844 case CPP_MINUS_EQ:
13845 id = ansi_assopname (MINUS_EXPR);
13846 break;
13848 case CPP_MULT_EQ:
13849 id = ansi_assopname (MULT_EXPR);
13850 break;
13852 case CPP_DIV_EQ:
13853 id = ansi_assopname (TRUNC_DIV_EXPR);
13854 break;
13856 case CPP_MOD_EQ:
13857 id = ansi_assopname (TRUNC_MOD_EXPR);
13858 break;
13860 case CPP_XOR_EQ:
13861 id = ansi_assopname (BIT_XOR_EXPR);
13862 break;
13864 case CPP_AND_EQ:
13865 id = ansi_assopname (BIT_AND_EXPR);
13866 break;
13868 case CPP_OR_EQ:
13869 id = ansi_assopname (BIT_IOR_EXPR);
13870 break;
13872 case CPP_LSHIFT:
13873 id = ansi_opname (LSHIFT_EXPR);
13874 break;
13876 case CPP_RSHIFT:
13877 id = ansi_opname (RSHIFT_EXPR);
13878 break;
13880 case CPP_LSHIFT_EQ:
13881 id = ansi_assopname (LSHIFT_EXPR);
13882 break;
13884 case CPP_RSHIFT_EQ:
13885 id = ansi_assopname (RSHIFT_EXPR);
13886 break;
13888 case CPP_EQ_EQ:
13889 id = ansi_opname (EQ_EXPR);
13890 break;
13892 case CPP_NOT_EQ:
13893 id = ansi_opname (NE_EXPR);
13894 break;
13896 case CPP_LESS_EQ:
13897 id = ansi_opname (LE_EXPR);
13898 break;
13900 case CPP_GREATER_EQ:
13901 id = ansi_opname (GE_EXPR);
13902 break;
13904 case CPP_AND_AND:
13905 id = ansi_opname (TRUTH_ANDIF_EXPR);
13906 break;
13908 case CPP_OR_OR:
13909 id = ansi_opname (TRUTH_ORIF_EXPR);
13910 break;
13912 case CPP_PLUS_PLUS:
13913 id = ansi_opname (POSTINCREMENT_EXPR);
13914 break;
13916 case CPP_MINUS_MINUS:
13917 id = ansi_opname (PREDECREMENT_EXPR);
13918 break;
13920 case CPP_COMMA:
13921 id = ansi_opname (COMPOUND_EXPR);
13922 break;
13924 case CPP_DEREF_STAR:
13925 id = ansi_opname (MEMBER_REF);
13926 break;
13928 case CPP_DEREF:
13929 id = ansi_opname (COMPONENT_REF);
13930 break;
13932 case CPP_OPEN_PAREN:
13933 /* Consume the `('. */
13934 cp_lexer_consume_token (parser->lexer);
13935 /* Look for the matching `)'. */
13936 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13937 return ansi_opname (CALL_EXPR);
13939 case CPP_OPEN_SQUARE:
13940 /* Consume the `['. */
13941 cp_lexer_consume_token (parser->lexer);
13942 /* Look for the matching `]'. */
13943 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13944 return ansi_opname (ARRAY_REF);
13946 case CPP_UTF8STRING:
13947 case CPP_UTF8STRING_USERDEF:
13948 utf8 = true;
13949 case CPP_STRING:
13950 case CPP_WSTRING:
13951 case CPP_STRING16:
13952 case CPP_STRING32:
13953 case CPP_STRING_USERDEF:
13954 case CPP_WSTRING_USERDEF:
13955 case CPP_STRING16_USERDEF:
13956 case CPP_STRING32_USERDEF:
13958 tree str, string_tree;
13959 int sz, len;
13961 if (cxx_dialect == cxx98)
13962 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
13964 /* Consume the string. */
13965 str = cp_parser_string_literal (parser, /*translate=*/true,
13966 /*wide_ok=*/true, /*lookup_udlit=*/false);
13967 if (str == error_mark_node)
13968 return error_mark_node;
13969 else if (TREE_CODE (str) == USERDEF_LITERAL)
13971 string_tree = USERDEF_LITERAL_VALUE (str);
13972 id = USERDEF_LITERAL_SUFFIX_ID (str);
13974 else
13976 string_tree = str;
13977 /* Look for the suffix identifier. */
13978 token = cp_lexer_peek_token (parser->lexer);
13979 if (token->type == CPP_NAME)
13980 id = cp_parser_identifier (parser);
13981 else if (token->type == CPP_KEYWORD)
13983 error ("unexpected keyword;"
13984 " remove space between quotes and suffix identifier");
13985 return error_mark_node;
13987 else
13989 error ("expected suffix identifier");
13990 return error_mark_node;
13993 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13994 (TREE_TYPE (TREE_TYPE (string_tree))));
13995 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
13996 if (len != 0)
13998 error ("expected empty string after %<operator%> keyword");
13999 return error_mark_node;
14001 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14002 != char_type_node)
14004 error ("invalid encoding prefix in literal operator");
14005 return error_mark_node;
14007 if (id != error_mark_node)
14009 const char *name = IDENTIFIER_POINTER (id);
14010 id = cp_literal_operator_id (name);
14012 return id;
14015 default:
14016 /* Anything else is an error. */
14017 break;
14020 /* If we have selected an identifier, we need to consume the
14021 operator token. */
14022 if (id)
14023 cp_lexer_consume_token (parser->lexer);
14024 /* Otherwise, no valid operator name was present. */
14025 else
14027 cp_parser_error (parser, "expected operator");
14028 id = error_mark_node;
14031 return cp_expr (id, start_loc);
14034 /* Parse a template-declaration.
14036 template-declaration:
14037 export [opt] template < template-parameter-list > declaration
14039 If MEMBER_P is TRUE, this template-declaration occurs within a
14040 class-specifier.
14042 The grammar rule given by the standard isn't correct. What
14043 is really meant is:
14045 template-declaration:
14046 export [opt] template-parameter-list-seq
14047 decl-specifier-seq [opt] init-declarator [opt] ;
14048 export [opt] template-parameter-list-seq
14049 function-definition
14051 template-parameter-list-seq:
14052 template-parameter-list-seq [opt]
14053 template < template-parameter-list >
14055 Concept Extensions:
14057 template-parameter-list-seq:
14058 template < template-parameter-list > requires-clause [opt]
14060 requires-clause:
14061 requires logical-or-expression */
14063 static void
14064 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14066 /* Check for `export'. */
14067 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14069 /* Consume the `export' token. */
14070 cp_lexer_consume_token (parser->lexer);
14071 /* Warn that we do not support `export'. */
14072 warning (0, "keyword %<export%> not implemented, and will be ignored");
14075 cp_parser_template_declaration_after_export (parser, member_p);
14078 /* Parse a template-parameter-list.
14080 template-parameter-list:
14081 template-parameter
14082 template-parameter-list , template-parameter
14084 Returns a TREE_LIST. Each node represents a template parameter.
14085 The nodes are connected via their TREE_CHAINs. */
14087 static tree
14088 cp_parser_template_parameter_list (cp_parser* parser)
14090 tree parameter_list = NULL_TREE;
14092 begin_template_parm_list ();
14094 /* The loop below parses the template parms. We first need to know
14095 the total number of template parms to be able to compute proper
14096 canonical types of each dependent type. So after the loop, when
14097 we know the total number of template parms,
14098 end_template_parm_list computes the proper canonical types and
14099 fixes up the dependent types accordingly. */
14100 while (true)
14102 tree parameter;
14103 bool is_non_type;
14104 bool is_parameter_pack;
14105 location_t parm_loc;
14107 /* Parse the template-parameter. */
14108 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
14109 parameter = cp_parser_template_parameter (parser,
14110 &is_non_type,
14111 &is_parameter_pack);
14112 /* Add it to the list. */
14113 if (parameter != error_mark_node)
14114 parameter_list = process_template_parm (parameter_list,
14115 parm_loc,
14116 parameter,
14117 is_non_type,
14118 is_parameter_pack);
14119 else
14121 tree err_parm = build_tree_list (parameter, parameter);
14122 parameter_list = chainon (parameter_list, err_parm);
14125 /* If the next token is not a `,', we're done. */
14126 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14127 break;
14128 /* Otherwise, consume the `,' token. */
14129 cp_lexer_consume_token (parser->lexer);
14132 return end_template_parm_list (parameter_list);
14135 /* Parse a introduction-list.
14137 introduction-list:
14138 introduced-parameter
14139 introduction-list , introduced-parameter
14141 introduced-parameter:
14142 ...[opt] identifier
14144 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
14145 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
14146 WILDCARD_DECL will also have DECL_NAME set and token location in
14147 DECL_SOURCE_LOCATION. */
14149 static tree
14150 cp_parser_introduction_list (cp_parser *parser)
14152 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
14154 while (true)
14156 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14157 if (is_pack)
14158 cp_lexer_consume_token (parser->lexer);
14160 /* Build placeholder. */
14161 tree parm = build_nt (WILDCARD_DECL);
14162 DECL_SOURCE_LOCATION (parm)
14163 = cp_lexer_peek_token (parser->lexer)->location;
14164 DECL_NAME (parm) = cp_parser_identifier (parser);
14165 WILDCARD_PACK_P (parm) = is_pack;
14166 vec_safe_push (introduction_vec, parm);
14168 /* If the next token is not a `,', we're done. */
14169 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14170 break;
14171 /* Otherwise, consume the `,' token. */
14172 cp_lexer_consume_token (parser->lexer);
14175 /* Convert the vec into a TREE_VEC. */
14176 tree introduction_list = make_tree_vec (introduction_vec->length ());
14177 unsigned int n;
14178 tree parm;
14179 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
14180 TREE_VEC_ELT (introduction_list, n) = parm;
14182 release_tree_vector (introduction_vec);
14183 return introduction_list;
14186 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
14187 is an abstract declarator. */
14189 static inline cp_declarator*
14190 get_id_declarator (cp_declarator *declarator)
14192 cp_declarator *d = declarator;
14193 while (d && d->kind != cdk_id)
14194 d = d->declarator;
14195 return d;
14198 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
14199 is an abstract declarator. */
14201 static inline tree
14202 get_unqualified_id (cp_declarator *declarator)
14204 declarator = get_id_declarator (declarator);
14205 if (declarator)
14206 return declarator->u.id.unqualified_name;
14207 else
14208 return NULL_TREE;
14211 /* Returns true if DECL represents a constrained-parameter. */
14213 static inline bool
14214 is_constrained_parameter (tree decl)
14216 return (decl
14217 && TREE_CODE (decl) == TYPE_DECL
14218 && CONSTRAINED_PARM_CONCEPT (decl)
14219 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
14222 /* Returns true if PARM declares a constrained-parameter. */
14224 static inline bool
14225 is_constrained_parameter (cp_parameter_declarator *parm)
14227 return is_constrained_parameter (parm->decl_specifiers.type);
14230 /* Check that the type parameter is only a declarator-id, and that its
14231 type is not cv-qualified. */
14233 bool
14234 cp_parser_check_constrained_type_parm (cp_parser *parser,
14235 cp_parameter_declarator *parm)
14237 if (!parm->declarator)
14238 return true;
14240 if (parm->declarator->kind != cdk_id)
14242 cp_parser_error (parser, "invalid constrained type parameter");
14243 return false;
14246 /* Don't allow cv-qualified type parameters. */
14247 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
14248 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
14250 cp_parser_error (parser, "cv-qualified type parameter");
14251 return false;
14254 return true;
14257 /* Finish parsing/processing a template type parameter and checking
14258 various restrictions. */
14260 static inline tree
14261 cp_parser_constrained_type_template_parm (cp_parser *parser,
14262 tree id,
14263 cp_parameter_declarator* parmdecl)
14265 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
14266 return finish_template_type_parm (class_type_node, id);
14267 else
14268 return error_mark_node;
14271 static tree
14272 finish_constrained_template_template_parm (tree proto, tree id)
14274 /* FIXME: This should probably be copied, and we may need to adjust
14275 the template parameter depths. */
14276 tree saved_parms = current_template_parms;
14277 begin_template_parm_list ();
14278 current_template_parms = DECL_TEMPLATE_PARMS (proto);
14279 end_template_parm_list ();
14281 tree parm = finish_template_template_parm (class_type_node, id);
14282 current_template_parms = saved_parms;
14284 return parm;
14287 /* Finish parsing/processing a template template parameter by borrowing
14288 the template parameter list from the prototype parameter. */
14290 static tree
14291 cp_parser_constrained_template_template_parm (cp_parser *parser,
14292 tree proto,
14293 tree id,
14294 cp_parameter_declarator *parmdecl)
14296 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
14297 return error_mark_node;
14298 return finish_constrained_template_template_parm (proto, id);
14301 /* Create a new non-type template parameter from the given PARM
14302 declarator. */
14304 static tree
14305 constrained_non_type_template_parm (bool *is_non_type,
14306 cp_parameter_declarator *parm)
14308 *is_non_type = true;
14309 cp_declarator *decl = parm->declarator;
14310 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
14311 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
14312 return grokdeclarator (decl, specs, TPARM, 0, NULL);
14315 /* Build a constrained template parameter based on the PARMDECL
14316 declarator. The type of PARMDECL is the constrained type, which
14317 refers to the prototype template parameter that ultimately
14318 specifies the type of the declared parameter. */
14320 static tree
14321 finish_constrained_parameter (cp_parser *parser,
14322 cp_parameter_declarator *parmdecl,
14323 bool *is_non_type,
14324 bool *is_parameter_pack)
14326 tree decl = parmdecl->decl_specifiers.type;
14327 tree id = get_unqualified_id (parmdecl->declarator);
14328 tree def = parmdecl->default_argument;
14329 tree proto = DECL_INITIAL (decl);
14331 /* A template parameter constrained by a variadic concept shall also
14332 be declared as a template parameter pack. */
14333 bool is_variadic = template_parameter_pack_p (proto);
14334 if (is_variadic && !*is_parameter_pack)
14335 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
14337 /* Build the parameter. Return an error if the declarator was invalid. */
14338 tree parm;
14339 if (TREE_CODE (proto) == TYPE_DECL)
14340 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
14341 else if (TREE_CODE (proto) == TEMPLATE_DECL)
14342 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
14343 parmdecl);
14344 else
14345 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
14346 if (parm == error_mark_node)
14347 return error_mark_node;
14349 /* Finish the parameter decl and create a node attaching the
14350 default argument and constraint. */
14351 parm = build_tree_list (def, parm);
14352 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
14354 return parm;
14357 /* Returns true if the parsed type actually represents the declaration
14358 of a type template-parameter. */
14360 static inline bool
14361 declares_constrained_type_template_parameter (tree type)
14363 return (is_constrained_parameter (type)
14364 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
14368 /* Returns true if the parsed type actually represents the declaration of
14369 a template template-parameter. */
14371 static bool
14372 declares_constrained_template_template_parameter (tree type)
14374 return (is_constrained_parameter (type)
14375 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
14378 /* Parse a default argument for a type template-parameter.
14379 Note that diagnostics are handled in cp_parser_template_parameter. */
14381 static tree
14382 cp_parser_default_type_template_argument (cp_parser *parser)
14384 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14386 /* Consume the `=' token. */
14387 cp_lexer_consume_token (parser->lexer);
14389 cp_token *token = cp_lexer_peek_token (parser->lexer);
14391 /* Parse the default-argument. */
14392 push_deferring_access_checks (dk_no_deferred);
14393 tree default_argument = cp_parser_type_id (parser);
14394 pop_deferring_access_checks ();
14396 if (flag_concepts && type_uses_auto (default_argument))
14398 error_at (token->location,
14399 "invalid use of %<auto%> in default template argument");
14400 return error_mark_node;
14403 return default_argument;
14406 /* Parse a default argument for a template template-parameter. */
14408 static tree
14409 cp_parser_default_template_template_argument (cp_parser *parser)
14411 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14413 bool is_template;
14415 /* Consume the `='. */
14416 cp_lexer_consume_token (parser->lexer);
14417 /* Parse the id-expression. */
14418 push_deferring_access_checks (dk_no_deferred);
14419 /* save token before parsing the id-expression, for error
14420 reporting */
14421 const cp_token* token = cp_lexer_peek_token (parser->lexer);
14422 tree default_argument
14423 = cp_parser_id_expression (parser,
14424 /*template_keyword_p=*/false,
14425 /*check_dependency_p=*/true,
14426 /*template_p=*/&is_template,
14427 /*declarator_p=*/false,
14428 /*optional_p=*/false);
14429 if (TREE_CODE (default_argument) == TYPE_DECL)
14430 /* If the id-expression was a template-id that refers to
14431 a template-class, we already have the declaration here,
14432 so no further lookup is needed. */
14434 else
14435 /* Look up the name. */
14436 default_argument
14437 = cp_parser_lookup_name (parser, default_argument,
14438 none_type,
14439 /*is_template=*/is_template,
14440 /*is_namespace=*/false,
14441 /*check_dependency=*/true,
14442 /*ambiguous_decls=*/NULL,
14443 token->location);
14444 /* See if the default argument is valid. */
14445 default_argument = check_template_template_default_arg (default_argument);
14446 pop_deferring_access_checks ();
14447 return default_argument;
14450 /* Parse a template-parameter.
14452 template-parameter:
14453 type-parameter
14454 parameter-declaration
14456 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
14457 the parameter. The TREE_PURPOSE is the default value, if any.
14458 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
14459 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
14460 set to true iff this parameter is a parameter pack. */
14462 static tree
14463 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
14464 bool *is_parameter_pack)
14466 cp_token *token;
14467 cp_parameter_declarator *parameter_declarator;
14468 tree parm;
14470 /* Assume it is a type parameter or a template parameter. */
14471 *is_non_type = false;
14472 /* Assume it not a parameter pack. */
14473 *is_parameter_pack = false;
14474 /* Peek at the next token. */
14475 token = cp_lexer_peek_token (parser->lexer);
14476 /* If it is `class' or `template', we have a type-parameter. */
14477 if (token->keyword == RID_TEMPLATE)
14478 return cp_parser_type_parameter (parser, is_parameter_pack);
14479 /* If it is `class' or `typename' we do not know yet whether it is a
14480 type parameter or a non-type parameter. Consider:
14482 template <typename T, typename T::X X> ...
14486 template <class C, class D*> ...
14488 Here, the first parameter is a type parameter, and the second is
14489 a non-type parameter. We can tell by looking at the token after
14490 the identifier -- if it is a `,', `=', or `>' then we have a type
14491 parameter. */
14492 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
14494 /* Peek at the token after `class' or `typename'. */
14495 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14496 /* If it's an ellipsis, we have a template type parameter
14497 pack. */
14498 if (token->type == CPP_ELLIPSIS)
14499 return cp_parser_type_parameter (parser, is_parameter_pack);
14500 /* If it's an identifier, skip it. */
14501 if (token->type == CPP_NAME)
14502 token = cp_lexer_peek_nth_token (parser->lexer, 3);
14503 /* Now, see if the token looks like the end of a template
14504 parameter. */
14505 if (token->type == CPP_COMMA
14506 || token->type == CPP_EQ
14507 || token->type == CPP_GREATER)
14508 return cp_parser_type_parameter (parser, is_parameter_pack);
14511 /* Otherwise, it is a non-type parameter or a constrained parameter.
14513 [temp.param]
14515 When parsing a default template-argument for a non-type
14516 template-parameter, the first non-nested `>' is taken as the end
14517 of the template parameter-list rather than a greater-than
14518 operator. */
14519 parameter_declarator
14520 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
14521 /*parenthesized_p=*/NULL);
14523 if (!parameter_declarator)
14524 return error_mark_node;
14526 /* If the parameter declaration is marked as a parameter pack, set
14527 *IS_PARAMETER_PACK to notify the caller. */
14528 if (parameter_declarator->template_parameter_pack_p)
14529 *is_parameter_pack = true;
14531 if (parameter_declarator->default_argument)
14533 /* Can happen in some cases of erroneous input (c++/34892). */
14534 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14535 /* Consume the `...' for better error recovery. */
14536 cp_lexer_consume_token (parser->lexer);
14539 // The parameter may have been constrained.
14540 if (is_constrained_parameter (parameter_declarator))
14541 return finish_constrained_parameter (parser,
14542 parameter_declarator,
14543 is_non_type,
14544 is_parameter_pack);
14546 // Now we're sure that the parameter is a non-type parameter.
14547 *is_non_type = true;
14549 parm = grokdeclarator (parameter_declarator->declarator,
14550 &parameter_declarator->decl_specifiers,
14551 TPARM, /*initialized=*/0,
14552 /*attrlist=*/NULL);
14553 if (parm == error_mark_node)
14554 return error_mark_node;
14556 return build_tree_list (parameter_declarator->default_argument, parm);
14559 /* Parse a type-parameter.
14561 type-parameter:
14562 class identifier [opt]
14563 class identifier [opt] = type-id
14564 typename identifier [opt]
14565 typename identifier [opt] = type-id
14566 template < template-parameter-list > class identifier [opt]
14567 template < template-parameter-list > class identifier [opt]
14568 = id-expression
14570 GNU Extension (variadic templates):
14572 type-parameter:
14573 class ... identifier [opt]
14574 typename ... identifier [opt]
14576 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
14577 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
14578 the declaration of the parameter.
14580 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
14582 static tree
14583 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
14585 cp_token *token;
14586 tree parameter;
14588 /* Look for a keyword to tell us what kind of parameter this is. */
14589 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
14590 if (!token)
14591 return error_mark_node;
14593 switch (token->keyword)
14595 case RID_CLASS:
14596 case RID_TYPENAME:
14598 tree identifier;
14599 tree default_argument;
14601 /* If the next token is an ellipsis, we have a template
14602 argument pack. */
14603 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14605 /* Consume the `...' token. */
14606 cp_lexer_consume_token (parser->lexer);
14607 maybe_warn_variadic_templates ();
14609 *is_parameter_pack = true;
14612 /* If the next token is an identifier, then it names the
14613 parameter. */
14614 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14615 identifier = cp_parser_identifier (parser);
14616 else
14617 identifier = NULL_TREE;
14619 /* Create the parameter. */
14620 parameter = finish_template_type_parm (class_type_node, identifier);
14622 /* If the next token is an `=', we have a default argument. */
14623 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14625 default_argument
14626 = cp_parser_default_type_template_argument (parser);
14628 /* Template parameter packs cannot have default
14629 arguments. */
14630 if (*is_parameter_pack)
14632 if (identifier)
14633 error_at (token->location,
14634 "template parameter pack %qD cannot have a "
14635 "default argument", identifier);
14636 else
14637 error_at (token->location,
14638 "template parameter packs cannot have "
14639 "default arguments");
14640 default_argument = NULL_TREE;
14642 else if (check_for_bare_parameter_packs (default_argument))
14643 default_argument = error_mark_node;
14645 else
14646 default_argument = NULL_TREE;
14648 /* Create the combined representation of the parameter and the
14649 default argument. */
14650 parameter = build_tree_list (default_argument, parameter);
14652 break;
14654 case RID_TEMPLATE:
14656 tree identifier;
14657 tree default_argument;
14659 /* Look for the `<'. */
14660 cp_parser_require (parser, CPP_LESS, RT_LESS);
14661 /* Parse the template-parameter-list. */
14662 cp_parser_template_parameter_list (parser);
14663 /* Look for the `>'. */
14664 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14666 // If template requirements are present, parse them.
14667 if (flag_concepts)
14669 tree reqs = get_shorthand_constraints (current_template_parms);
14670 if (tree r = cp_parser_requires_clause_opt (parser))
14671 reqs = conjoin_constraints (reqs, make_predicate_constraint (r));
14672 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
14675 /* Look for the `class' or 'typename' keywords. */
14676 cp_parser_type_parameter_key (parser);
14677 /* If the next token is an ellipsis, we have a template
14678 argument pack. */
14679 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14681 /* Consume the `...' token. */
14682 cp_lexer_consume_token (parser->lexer);
14683 maybe_warn_variadic_templates ();
14685 *is_parameter_pack = true;
14687 /* If the next token is an `=', then there is a
14688 default-argument. If the next token is a `>', we are at
14689 the end of the parameter-list. If the next token is a `,',
14690 then we are at the end of this parameter. */
14691 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
14692 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
14693 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14695 identifier = cp_parser_identifier (parser);
14696 /* Treat invalid names as if the parameter were nameless. */
14697 if (identifier == error_mark_node)
14698 identifier = NULL_TREE;
14700 else
14701 identifier = NULL_TREE;
14703 /* Create the template parameter. */
14704 parameter = finish_template_template_parm (class_type_node,
14705 identifier);
14707 /* If the next token is an `=', then there is a
14708 default-argument. */
14709 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14711 default_argument
14712 = cp_parser_default_template_template_argument (parser);
14714 /* Template parameter packs cannot have default
14715 arguments. */
14716 if (*is_parameter_pack)
14718 if (identifier)
14719 error_at (token->location,
14720 "template parameter pack %qD cannot "
14721 "have a default argument",
14722 identifier);
14723 else
14724 error_at (token->location, "template parameter packs cannot "
14725 "have default arguments");
14726 default_argument = NULL_TREE;
14729 else
14730 default_argument = NULL_TREE;
14732 /* Create the combined representation of the parameter and the
14733 default argument. */
14734 parameter = build_tree_list (default_argument, parameter);
14736 break;
14738 default:
14739 gcc_unreachable ();
14740 break;
14743 return parameter;
14746 /* Parse a template-id.
14748 template-id:
14749 template-name < template-argument-list [opt] >
14751 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
14752 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
14753 returned. Otherwise, if the template-name names a function, or set
14754 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
14755 names a class, returns a TYPE_DECL for the specialization.
14757 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
14758 uninstantiated templates. */
14760 static tree
14761 cp_parser_template_id (cp_parser *parser,
14762 bool template_keyword_p,
14763 bool check_dependency_p,
14764 enum tag_types tag_type,
14765 bool is_declaration)
14767 tree templ;
14768 tree arguments;
14769 tree template_id;
14770 cp_token_position start_of_id = 0;
14771 cp_token *next_token = NULL, *next_token_2 = NULL;
14772 bool is_identifier;
14774 /* If the next token corresponds to a template-id, there is no need
14775 to reparse it. */
14776 next_token = cp_lexer_peek_token (parser->lexer);
14777 if (next_token->type == CPP_TEMPLATE_ID)
14779 cp_lexer_consume_token (parser->lexer);
14780 return saved_checks_value (next_token->u.tree_check_value);
14783 /* Avoid performing name lookup if there is no possibility of
14784 finding a template-id. */
14785 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
14786 || (next_token->type == CPP_NAME
14787 && !cp_parser_nth_token_starts_template_argument_list_p
14788 (parser, 2)))
14790 cp_parser_error (parser, "expected template-id");
14791 return error_mark_node;
14794 /* Remember where the template-id starts. */
14795 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
14796 start_of_id = cp_lexer_token_position (parser->lexer, false);
14798 push_deferring_access_checks (dk_deferred);
14800 /* Parse the template-name. */
14801 is_identifier = false;
14802 templ = cp_parser_template_name (parser, template_keyword_p,
14803 check_dependency_p,
14804 is_declaration,
14805 tag_type,
14806 &is_identifier);
14807 if (templ == error_mark_node || is_identifier)
14809 pop_deferring_access_checks ();
14810 return templ;
14813 /* Since we're going to preserve any side-effects from this parse, set up a
14814 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14815 in the template arguments. */
14816 tentative_firewall firewall (parser);
14818 /* If we find the sequence `[:' after a template-name, it's probably
14819 a digraph-typo for `< ::'. Substitute the tokens and check if we can
14820 parse correctly the argument list. */
14821 next_token = cp_lexer_peek_token (parser->lexer);
14822 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14823 if (next_token->type == CPP_OPEN_SQUARE
14824 && next_token->flags & DIGRAPH
14825 && next_token_2->type == CPP_COLON
14826 && !(next_token_2->flags & PREV_WHITE))
14828 cp_parser_parse_tentatively (parser);
14829 /* Change `:' into `::'. */
14830 next_token_2->type = CPP_SCOPE;
14831 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
14832 CPP_LESS. */
14833 cp_lexer_consume_token (parser->lexer);
14835 /* Parse the arguments. */
14836 arguments = cp_parser_enclosed_template_argument_list (parser);
14837 if (!cp_parser_parse_definitely (parser))
14839 /* If we couldn't parse an argument list, then we revert our changes
14840 and return simply an error. Maybe this is not a template-id
14841 after all. */
14842 next_token_2->type = CPP_COLON;
14843 cp_parser_error (parser, "expected %<<%>");
14844 pop_deferring_access_checks ();
14845 return error_mark_node;
14847 /* Otherwise, emit an error about the invalid digraph, but continue
14848 parsing because we got our argument list. */
14849 if (permerror (next_token->location,
14850 "%<<::%> cannot begin a template-argument list"))
14852 static bool hint = false;
14853 inform (next_token->location,
14854 "%<<:%> is an alternate spelling for %<[%>."
14855 " Insert whitespace between %<<%> and %<::%>");
14856 if (!hint && !flag_permissive)
14858 inform (next_token->location, "(if you use %<-fpermissive%> "
14859 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
14860 "accept your code)");
14861 hint = true;
14865 else
14867 /* Look for the `<' that starts the template-argument-list. */
14868 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
14870 pop_deferring_access_checks ();
14871 return error_mark_node;
14873 /* Parse the arguments. */
14874 arguments = cp_parser_enclosed_template_argument_list (parser);
14877 /* Build a representation of the specialization. */
14878 if (identifier_p (templ))
14879 template_id = build_min_nt_loc (next_token->location,
14880 TEMPLATE_ID_EXPR,
14881 templ, arguments);
14882 else if (DECL_TYPE_TEMPLATE_P (templ)
14883 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
14885 bool entering_scope;
14886 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
14887 template (rather than some instantiation thereof) only if
14888 is not nested within some other construct. For example, in
14889 "template <typename T> void f(T) { A<T>::", A<T> is just an
14890 instantiation of A. */
14891 entering_scope = (template_parm_scope_p ()
14892 && cp_lexer_next_token_is (parser->lexer,
14893 CPP_SCOPE));
14894 template_id
14895 = finish_template_type (templ, arguments, entering_scope);
14897 /* A template-like identifier may be a partial concept id. */
14898 else if (flag_concepts
14899 && (template_id = (cp_parser_maybe_partial_concept_id
14900 (parser, templ, arguments))))
14901 return template_id;
14902 else if (variable_template_p (templ))
14904 template_id = lookup_template_variable (templ, arguments);
14905 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
14906 SET_EXPR_LOCATION (template_id, next_token->location);
14908 else
14910 /* If it's not a class-template or a template-template, it should be
14911 a function-template. */
14912 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
14913 || TREE_CODE (templ) == OVERLOAD
14914 || BASELINK_P (templ)));
14916 template_id = lookup_template_function (templ, arguments);
14917 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
14918 SET_EXPR_LOCATION (template_id, next_token->location);
14921 /* If parsing tentatively, replace the sequence of tokens that makes
14922 up the template-id with a CPP_TEMPLATE_ID token. That way,
14923 should we re-parse the token stream, we will not have to repeat
14924 the effort required to do the parse, nor will we issue duplicate
14925 error messages about problems during instantiation of the
14926 template. */
14927 if (start_of_id
14928 /* Don't do this if we had a parse error in a declarator; re-parsing
14929 might succeed if a name changes meaning (60361). */
14930 && !(cp_parser_error_occurred (parser)
14931 && cp_parser_parsing_tentatively (parser)
14932 && parser->in_declarator_p))
14934 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
14936 /* Reset the contents of the START_OF_ID token. */
14937 token->type = CPP_TEMPLATE_ID;
14939 /* Update the location to be of the form:
14940 template-name < template-argument-list [opt] >
14941 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14942 with caret == start at the start of the template-name,
14943 ranging until the closing '>'. */
14944 location_t finish_loc
14945 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
14946 location_t combined_loc
14947 = make_location (token->location, token->location, finish_loc);
14948 token->location = combined_loc;
14950 /* Retrieve any deferred checks. Do not pop this access checks yet
14951 so the memory will not be reclaimed during token replacing below. */
14952 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14953 token->u.tree_check_value->value = template_id;
14954 token->u.tree_check_value->checks = get_deferred_access_checks ();
14955 token->keyword = RID_MAX;
14957 /* Purge all subsequent tokens. */
14958 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
14960 /* ??? Can we actually assume that, if template_id ==
14961 error_mark_node, we will have issued a diagnostic to the
14962 user, as opposed to simply marking the tentative parse as
14963 failed? */
14964 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
14965 error_at (token->location, "parse error in template argument list");
14968 pop_to_parent_deferring_access_checks ();
14969 return template_id;
14972 /* Parse a template-name.
14974 template-name:
14975 identifier
14977 The standard should actually say:
14979 template-name:
14980 identifier
14981 operator-function-id
14983 A defect report has been filed about this issue.
14985 A conversion-function-id cannot be a template name because they cannot
14986 be part of a template-id. In fact, looking at this code:
14988 a.operator K<int>()
14990 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
14991 It is impossible to call a templated conversion-function-id with an
14992 explicit argument list, since the only allowed template parameter is
14993 the type to which it is converting.
14995 If TEMPLATE_KEYWORD_P is true, then we have just seen the
14996 `template' keyword, in a construction like:
14998 T::template f<3>()
15000 In that case `f' is taken to be a template-name, even though there
15001 is no way of knowing for sure.
15003 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15004 name refers to a set of overloaded functions, at least one of which
15005 is a template, or an IDENTIFIER_NODE with the name of the template,
15006 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15007 names are looked up inside uninstantiated templates. */
15009 static tree
15010 cp_parser_template_name (cp_parser* parser,
15011 bool template_keyword_p,
15012 bool check_dependency_p,
15013 bool is_declaration,
15014 enum tag_types tag_type,
15015 bool *is_identifier)
15017 tree identifier;
15018 tree decl;
15019 tree fns;
15020 cp_token *token = cp_lexer_peek_token (parser->lexer);
15022 /* If the next token is `operator', then we have either an
15023 operator-function-id or a conversion-function-id. */
15024 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15026 /* We don't know whether we're looking at an
15027 operator-function-id or a conversion-function-id. */
15028 cp_parser_parse_tentatively (parser);
15029 /* Try an operator-function-id. */
15030 identifier = cp_parser_operator_function_id (parser);
15031 /* If that didn't work, try a conversion-function-id. */
15032 if (!cp_parser_parse_definitely (parser))
15034 cp_parser_error (parser, "expected template-name");
15035 return error_mark_node;
15038 /* Look for the identifier. */
15039 else
15040 identifier = cp_parser_identifier (parser);
15042 /* If we didn't find an identifier, we don't have a template-id. */
15043 if (identifier == error_mark_node)
15044 return error_mark_node;
15046 /* If the name immediately followed the `template' keyword, then it
15047 is a template-name. However, if the next token is not `<', then
15048 we do not treat it as a template-name, since it is not being used
15049 as part of a template-id. This enables us to handle constructs
15050 like:
15052 template <typename T> struct S { S(); };
15053 template <typename T> S<T>::S();
15055 correctly. We would treat `S' as a template -- if it were `S<T>'
15056 -- but we do not if there is no `<'. */
15058 if (processing_template_decl
15059 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15061 /* In a declaration, in a dependent context, we pretend that the
15062 "template" keyword was present in order to improve error
15063 recovery. For example, given:
15065 template <typename T> void f(T::X<int>);
15067 we want to treat "X<int>" as a template-id. */
15068 if (is_declaration
15069 && !template_keyword_p
15070 && parser->scope && TYPE_P (parser->scope)
15071 && check_dependency_p
15072 && dependent_scope_p (parser->scope)
15073 /* Do not do this for dtors (or ctors), since they never
15074 need the template keyword before their name. */
15075 && !constructor_name_p (identifier, parser->scope))
15077 cp_token_position start = 0;
15079 /* Explain what went wrong. */
15080 error_at (token->location, "non-template %qD used as template",
15081 identifier);
15082 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15083 parser->scope, identifier);
15084 /* If parsing tentatively, find the location of the "<" token. */
15085 if (cp_parser_simulate_error (parser))
15086 start = cp_lexer_token_position (parser->lexer, true);
15087 /* Parse the template arguments so that we can issue error
15088 messages about them. */
15089 cp_lexer_consume_token (parser->lexer);
15090 cp_parser_enclosed_template_argument_list (parser);
15091 /* Skip tokens until we find a good place from which to
15092 continue parsing. */
15093 cp_parser_skip_to_closing_parenthesis (parser,
15094 /*recovering=*/true,
15095 /*or_comma=*/true,
15096 /*consume_paren=*/false);
15097 /* If parsing tentatively, permanently remove the
15098 template argument list. That will prevent duplicate
15099 error messages from being issued about the missing
15100 "template" keyword. */
15101 if (start)
15102 cp_lexer_purge_tokens_after (parser->lexer, start);
15103 if (is_identifier)
15104 *is_identifier = true;
15105 return identifier;
15108 /* If the "template" keyword is present, then there is generally
15109 no point in doing name-lookup, so we just return IDENTIFIER.
15110 But, if the qualifying scope is non-dependent then we can
15111 (and must) do name-lookup normally. */
15112 if (template_keyword_p
15113 && (!parser->scope
15114 || (TYPE_P (parser->scope)
15115 && dependent_type_p (parser->scope))))
15116 return identifier;
15119 /* Look up the name. */
15120 decl = cp_parser_lookup_name (parser, identifier,
15121 tag_type,
15122 /*is_template=*/true,
15123 /*is_namespace=*/false,
15124 check_dependency_p,
15125 /*ambiguous_decls=*/NULL,
15126 token->location);
15128 decl = strip_using_decl (decl);
15130 /* If DECL is a template, then the name was a template-name. */
15131 if (TREE_CODE (decl) == TEMPLATE_DECL)
15133 if (TREE_DEPRECATED (decl)
15134 && deprecated_state != DEPRECATED_SUPPRESS)
15135 warn_deprecated_use (decl, NULL_TREE);
15137 else
15139 tree fn = NULL_TREE;
15141 /* The standard does not explicitly indicate whether a name that
15142 names a set of overloaded declarations, some of which are
15143 templates, is a template-name. However, such a name should
15144 be a template-name; otherwise, there is no way to form a
15145 template-id for the overloaded templates. */
15146 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
15147 if (TREE_CODE (fns) == OVERLOAD)
15148 for (fn = fns; fn; fn = OVL_NEXT (fn))
15149 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
15150 break;
15152 if (!fn)
15154 /* The name does not name a template. */
15155 cp_parser_error (parser, "expected template-name");
15156 return error_mark_node;
15160 /* If DECL is dependent, and refers to a function, then just return
15161 its name; we will look it up again during template instantiation. */
15162 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
15164 tree scope = ovl_scope (decl);
15165 if (TYPE_P (scope) && dependent_type_p (scope))
15166 return identifier;
15169 return decl;
15172 /* Parse a template-argument-list.
15174 template-argument-list:
15175 template-argument ... [opt]
15176 template-argument-list , template-argument ... [opt]
15178 Returns a TREE_VEC containing the arguments. */
15180 static tree
15181 cp_parser_template_argument_list (cp_parser* parser)
15183 tree fixed_args[10];
15184 unsigned n_args = 0;
15185 unsigned alloced = 10;
15186 tree *arg_ary = fixed_args;
15187 tree vec;
15188 bool saved_in_template_argument_list_p;
15189 bool saved_ice_p;
15190 bool saved_non_ice_p;
15192 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
15193 parser->in_template_argument_list_p = true;
15194 /* Even if the template-id appears in an integral
15195 constant-expression, the contents of the argument list do
15196 not. */
15197 saved_ice_p = parser->integral_constant_expression_p;
15198 parser->integral_constant_expression_p = false;
15199 saved_non_ice_p = parser->non_integral_constant_expression_p;
15200 parser->non_integral_constant_expression_p = false;
15202 /* Parse the arguments. */
15205 tree argument;
15207 if (n_args)
15208 /* Consume the comma. */
15209 cp_lexer_consume_token (parser->lexer);
15211 /* Parse the template-argument. */
15212 argument = cp_parser_template_argument (parser);
15214 /* If the next token is an ellipsis, we're expanding a template
15215 argument pack. */
15216 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15218 if (argument == error_mark_node)
15220 cp_token *token = cp_lexer_peek_token (parser->lexer);
15221 error_at (token->location,
15222 "expected parameter pack before %<...%>");
15224 /* Consume the `...' token. */
15225 cp_lexer_consume_token (parser->lexer);
15227 /* Make the argument into a TYPE_PACK_EXPANSION or
15228 EXPR_PACK_EXPANSION. */
15229 argument = make_pack_expansion (argument);
15232 if (n_args == alloced)
15234 alloced *= 2;
15236 if (arg_ary == fixed_args)
15238 arg_ary = XNEWVEC (tree, alloced);
15239 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
15241 else
15242 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
15244 arg_ary[n_args++] = argument;
15246 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15248 vec = make_tree_vec (n_args);
15250 while (n_args--)
15251 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
15253 if (arg_ary != fixed_args)
15254 free (arg_ary);
15255 parser->non_integral_constant_expression_p = saved_non_ice_p;
15256 parser->integral_constant_expression_p = saved_ice_p;
15257 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
15258 if (CHECKING_P)
15259 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
15260 return vec;
15263 /* Parse a template-argument.
15265 template-argument:
15266 assignment-expression
15267 type-id
15268 id-expression
15270 The representation is that of an assignment-expression, type-id, or
15271 id-expression -- except that the qualified id-expression is
15272 evaluated, so that the value returned is either a DECL or an
15273 OVERLOAD.
15275 Although the standard says "assignment-expression", it forbids
15276 throw-expressions or assignments in the template argument.
15277 Therefore, we use "conditional-expression" instead. */
15279 static tree
15280 cp_parser_template_argument (cp_parser* parser)
15282 tree argument;
15283 bool template_p;
15284 bool address_p;
15285 bool maybe_type_id = false;
15286 cp_token *token = NULL, *argument_start_token = NULL;
15287 location_t loc = 0;
15288 cp_id_kind idk;
15290 /* There's really no way to know what we're looking at, so we just
15291 try each alternative in order.
15293 [temp.arg]
15295 In a template-argument, an ambiguity between a type-id and an
15296 expression is resolved to a type-id, regardless of the form of
15297 the corresponding template-parameter.
15299 Therefore, we try a type-id first. */
15300 cp_parser_parse_tentatively (parser);
15301 argument = cp_parser_template_type_arg (parser);
15302 /* If there was no error parsing the type-id but the next token is a
15303 '>>', our behavior depends on which dialect of C++ we're
15304 parsing. In C++98, we probably found a typo for '> >'. But there
15305 are type-id which are also valid expressions. For instance:
15307 struct X { int operator >> (int); };
15308 template <int V> struct Foo {};
15309 Foo<X () >> 5> r;
15311 Here 'X()' is a valid type-id of a function type, but the user just
15312 wanted to write the expression "X() >> 5". Thus, we remember that we
15313 found a valid type-id, but we still try to parse the argument as an
15314 expression to see what happens.
15316 In C++0x, the '>>' will be considered two separate '>'
15317 tokens. */
15318 if (!cp_parser_error_occurred (parser)
15319 && cxx_dialect == cxx98
15320 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15322 maybe_type_id = true;
15323 cp_parser_abort_tentative_parse (parser);
15325 else
15327 /* If the next token isn't a `,' or a `>', then this argument wasn't
15328 really finished. This means that the argument is not a valid
15329 type-id. */
15330 if (!cp_parser_next_token_ends_template_argument_p (parser))
15331 cp_parser_error (parser, "expected template-argument");
15332 /* If that worked, we're done. */
15333 if (cp_parser_parse_definitely (parser))
15334 return argument;
15336 /* We're still not sure what the argument will be. */
15337 cp_parser_parse_tentatively (parser);
15338 /* Try a template. */
15339 argument_start_token = cp_lexer_peek_token (parser->lexer);
15340 argument = cp_parser_id_expression (parser,
15341 /*template_keyword_p=*/false,
15342 /*check_dependency_p=*/true,
15343 &template_p,
15344 /*declarator_p=*/false,
15345 /*optional_p=*/false);
15346 /* If the next token isn't a `,' or a `>', then this argument wasn't
15347 really finished. */
15348 if (!cp_parser_next_token_ends_template_argument_p (parser))
15349 cp_parser_error (parser, "expected template-argument");
15350 if (!cp_parser_error_occurred (parser))
15352 /* Figure out what is being referred to. If the id-expression
15353 was for a class template specialization, then we will have a
15354 TYPE_DECL at this point. There is no need to do name lookup
15355 at this point in that case. */
15356 if (TREE_CODE (argument) != TYPE_DECL)
15357 argument = cp_parser_lookup_name (parser, argument,
15358 none_type,
15359 /*is_template=*/template_p,
15360 /*is_namespace=*/false,
15361 /*check_dependency=*/true,
15362 /*ambiguous_decls=*/NULL,
15363 argument_start_token->location);
15364 /* Handle a constrained-type-specifier for a non-type template
15365 parameter. */
15366 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
15367 argument = decl;
15368 else if (TREE_CODE (argument) != TEMPLATE_DECL
15369 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
15370 cp_parser_error (parser, "expected template-name");
15372 if (cp_parser_parse_definitely (parser))
15374 if (TREE_DEPRECATED (argument))
15375 warn_deprecated_use (argument, NULL_TREE);
15376 return argument;
15378 /* It must be a non-type argument. In C++17 any constant-expression is
15379 allowed. */
15380 if (cxx_dialect > cxx14)
15381 goto general_expr;
15383 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
15385 -- an integral constant-expression of integral or enumeration
15386 type; or
15388 -- the name of a non-type template-parameter; or
15390 -- the name of an object or function with external linkage...
15392 -- the address of an object or function with external linkage...
15394 -- a pointer to member... */
15395 /* Look for a non-type template parameter. */
15396 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15398 cp_parser_parse_tentatively (parser);
15399 argument = cp_parser_primary_expression (parser,
15400 /*address_p=*/false,
15401 /*cast_p=*/false,
15402 /*template_arg_p=*/true,
15403 &idk);
15404 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
15405 || !cp_parser_next_token_ends_template_argument_p (parser))
15406 cp_parser_simulate_error (parser);
15407 if (cp_parser_parse_definitely (parser))
15408 return argument;
15411 /* If the next token is "&", the argument must be the address of an
15412 object or function with external linkage. */
15413 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
15414 if (address_p)
15416 loc = cp_lexer_peek_token (parser->lexer)->location;
15417 cp_lexer_consume_token (parser->lexer);
15419 /* See if we might have an id-expression. */
15420 token = cp_lexer_peek_token (parser->lexer);
15421 if (token->type == CPP_NAME
15422 || token->keyword == RID_OPERATOR
15423 || token->type == CPP_SCOPE
15424 || token->type == CPP_TEMPLATE_ID
15425 || token->type == CPP_NESTED_NAME_SPECIFIER)
15427 cp_parser_parse_tentatively (parser);
15428 argument = cp_parser_primary_expression (parser,
15429 address_p,
15430 /*cast_p=*/false,
15431 /*template_arg_p=*/true,
15432 &idk);
15433 if (cp_parser_error_occurred (parser)
15434 || !cp_parser_next_token_ends_template_argument_p (parser))
15435 cp_parser_abort_tentative_parse (parser);
15436 else
15438 tree probe;
15440 if (INDIRECT_REF_P (argument))
15442 /* Strip the dereference temporarily. */
15443 gcc_assert (REFERENCE_REF_P (argument));
15444 argument = TREE_OPERAND (argument, 0);
15447 /* If we're in a template, we represent a qualified-id referring
15448 to a static data member as a SCOPE_REF even if the scope isn't
15449 dependent so that we can check access control later. */
15450 probe = argument;
15451 if (TREE_CODE (probe) == SCOPE_REF)
15452 probe = TREE_OPERAND (probe, 1);
15453 if (VAR_P (probe))
15455 /* A variable without external linkage might still be a
15456 valid constant-expression, so no error is issued here
15457 if the external-linkage check fails. */
15458 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
15459 cp_parser_simulate_error (parser);
15461 else if (is_overloaded_fn (argument))
15462 /* All overloaded functions are allowed; if the external
15463 linkage test does not pass, an error will be issued
15464 later. */
15466 else if (address_p
15467 && (TREE_CODE (argument) == OFFSET_REF
15468 || TREE_CODE (argument) == SCOPE_REF))
15469 /* A pointer-to-member. */
15471 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
15473 else
15474 cp_parser_simulate_error (parser);
15476 if (cp_parser_parse_definitely (parser))
15478 if (address_p)
15479 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
15480 tf_warning_or_error);
15481 else
15482 argument = convert_from_reference (argument);
15483 return argument;
15487 /* If the argument started with "&", there are no other valid
15488 alternatives at this point. */
15489 if (address_p)
15491 cp_parser_error (parser, "invalid non-type template argument");
15492 return error_mark_node;
15495 general_expr:
15496 /* If the argument wasn't successfully parsed as a type-id followed
15497 by '>>', the argument can only be a constant expression now.
15498 Otherwise, we try parsing the constant-expression tentatively,
15499 because the argument could really be a type-id. */
15500 if (maybe_type_id)
15501 cp_parser_parse_tentatively (parser);
15503 if (cxx_dialect <= cxx14)
15504 argument = cp_parser_constant_expression (parser);
15505 else
15507 /* With C++17 generalized non-type template arguments we need to handle
15508 lvalue constant expressions, too. */
15509 argument = cp_parser_assignment_expression (parser);
15510 require_potential_constant_expression (argument);
15513 if (!maybe_type_id)
15514 return argument;
15515 if (!cp_parser_next_token_ends_template_argument_p (parser))
15516 cp_parser_error (parser, "expected template-argument");
15517 if (cp_parser_parse_definitely (parser))
15518 return argument;
15519 /* We did our best to parse the argument as a non type-id, but that
15520 was the only alternative that matched (albeit with a '>' after
15521 it). We can assume it's just a typo from the user, and a
15522 diagnostic will then be issued. */
15523 return cp_parser_template_type_arg (parser);
15526 /* Parse an explicit-instantiation.
15528 explicit-instantiation:
15529 template declaration
15531 Although the standard says `declaration', what it really means is:
15533 explicit-instantiation:
15534 template decl-specifier-seq [opt] declarator [opt] ;
15536 Things like `template int S<int>::i = 5, int S<double>::j;' are not
15537 supposed to be allowed. A defect report has been filed about this
15538 issue.
15540 GNU Extension:
15542 explicit-instantiation:
15543 storage-class-specifier template
15544 decl-specifier-seq [opt] declarator [opt] ;
15545 function-specifier template
15546 decl-specifier-seq [opt] declarator [opt] ; */
15548 static void
15549 cp_parser_explicit_instantiation (cp_parser* parser)
15551 int declares_class_or_enum;
15552 cp_decl_specifier_seq decl_specifiers;
15553 tree extension_specifier = NULL_TREE;
15555 timevar_push (TV_TEMPLATE_INST);
15557 /* Look for an (optional) storage-class-specifier or
15558 function-specifier. */
15559 if (cp_parser_allow_gnu_extensions_p (parser))
15561 extension_specifier
15562 = cp_parser_storage_class_specifier_opt (parser);
15563 if (!extension_specifier)
15564 extension_specifier
15565 = cp_parser_function_specifier_opt (parser,
15566 /*decl_specs=*/NULL);
15569 /* Look for the `template' keyword. */
15570 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15571 /* Let the front end know that we are processing an explicit
15572 instantiation. */
15573 begin_explicit_instantiation ();
15574 /* [temp.explicit] says that we are supposed to ignore access
15575 control while processing explicit instantiation directives. */
15576 push_deferring_access_checks (dk_no_check);
15577 /* Parse a decl-specifier-seq. */
15578 cp_parser_decl_specifier_seq (parser,
15579 CP_PARSER_FLAGS_OPTIONAL,
15580 &decl_specifiers,
15581 &declares_class_or_enum);
15582 /* If there was exactly one decl-specifier, and it declared a class,
15583 and there's no declarator, then we have an explicit type
15584 instantiation. */
15585 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
15587 tree type;
15589 type = check_tag_decl (&decl_specifiers,
15590 /*explicit_type_instantiation_p=*/true);
15591 /* Turn access control back on for names used during
15592 template instantiation. */
15593 pop_deferring_access_checks ();
15594 if (type)
15595 do_type_instantiation (type, extension_specifier,
15596 /*complain=*/tf_error);
15598 else
15600 cp_declarator *declarator;
15601 tree decl;
15603 /* Parse the declarator. */
15604 declarator
15605 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15606 /*ctor_dtor_or_conv_p=*/NULL,
15607 /*parenthesized_p=*/NULL,
15608 /*member_p=*/false,
15609 /*friend_p=*/false);
15610 if (declares_class_or_enum & 2)
15611 cp_parser_check_for_definition_in_return_type (declarator,
15612 decl_specifiers.type,
15613 decl_specifiers.locations[ds_type_spec]);
15614 if (declarator != cp_error_declarator)
15616 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
15617 permerror (decl_specifiers.locations[ds_inline],
15618 "explicit instantiation shall not use"
15619 " %<inline%> specifier");
15620 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
15621 permerror (decl_specifiers.locations[ds_constexpr],
15622 "explicit instantiation shall not use"
15623 " %<constexpr%> specifier");
15625 decl = grokdeclarator (declarator, &decl_specifiers,
15626 NORMAL, 0, &decl_specifiers.attributes);
15627 /* Turn access control back on for names used during
15628 template instantiation. */
15629 pop_deferring_access_checks ();
15630 /* Do the explicit instantiation. */
15631 do_decl_instantiation (decl, extension_specifier);
15633 else
15635 pop_deferring_access_checks ();
15636 /* Skip the body of the explicit instantiation. */
15637 cp_parser_skip_to_end_of_statement (parser);
15640 /* We're done with the instantiation. */
15641 end_explicit_instantiation ();
15643 cp_parser_consume_semicolon_at_end_of_statement (parser);
15645 timevar_pop (TV_TEMPLATE_INST);
15648 /* Parse an explicit-specialization.
15650 explicit-specialization:
15651 template < > declaration
15653 Although the standard says `declaration', what it really means is:
15655 explicit-specialization:
15656 template <> decl-specifier [opt] init-declarator [opt] ;
15657 template <> function-definition
15658 template <> explicit-specialization
15659 template <> template-declaration */
15661 static void
15662 cp_parser_explicit_specialization (cp_parser* parser)
15664 bool need_lang_pop;
15665 cp_token *token = cp_lexer_peek_token (parser->lexer);
15667 /* Look for the `template' keyword. */
15668 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15669 /* Look for the `<'. */
15670 cp_parser_require (parser, CPP_LESS, RT_LESS);
15671 /* Look for the `>'. */
15672 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15673 /* We have processed another parameter list. */
15674 ++parser->num_template_parameter_lists;
15675 /* [temp]
15677 A template ... explicit specialization ... shall not have C
15678 linkage. */
15679 if (current_lang_name == lang_name_c)
15681 error_at (token->location, "template specialization with C linkage");
15682 /* Give it C++ linkage to avoid confusing other parts of the
15683 front end. */
15684 push_lang_context (lang_name_cplusplus);
15685 need_lang_pop = true;
15687 else
15688 need_lang_pop = false;
15689 /* Let the front end know that we are beginning a specialization. */
15690 if (!begin_specialization ())
15692 end_specialization ();
15693 return;
15696 /* If the next keyword is `template', we need to figure out whether
15697 or not we're looking a template-declaration. */
15698 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15700 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15701 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
15702 cp_parser_template_declaration_after_export (parser,
15703 /*member_p=*/false);
15704 else
15705 cp_parser_explicit_specialization (parser);
15707 else
15708 /* Parse the dependent declaration. */
15709 cp_parser_single_declaration (parser,
15710 /*checks=*/NULL,
15711 /*member_p=*/false,
15712 /*explicit_specialization_p=*/true,
15713 /*friend_p=*/NULL);
15714 /* We're done with the specialization. */
15715 end_specialization ();
15716 /* For the erroneous case of a template with C linkage, we pushed an
15717 implicit C++ linkage scope; exit that scope now. */
15718 if (need_lang_pop)
15719 pop_lang_context ();
15720 /* We're done with this parameter list. */
15721 --parser->num_template_parameter_lists;
15724 /* Parse a type-specifier.
15726 type-specifier:
15727 simple-type-specifier
15728 class-specifier
15729 enum-specifier
15730 elaborated-type-specifier
15731 cv-qualifier
15733 GNU Extension:
15735 type-specifier:
15736 __complex__
15738 Returns a representation of the type-specifier. For a
15739 class-specifier, enum-specifier, or elaborated-type-specifier, a
15740 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
15742 The parser flags FLAGS is used to control type-specifier parsing.
15744 If IS_DECLARATION is TRUE, then this type-specifier is appearing
15745 in a decl-specifier-seq.
15747 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
15748 class-specifier, enum-specifier, or elaborated-type-specifier, then
15749 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
15750 if a type is declared; 2 if it is defined. Otherwise, it is set to
15751 zero.
15753 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
15754 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
15755 is set to FALSE. */
15757 static tree
15758 cp_parser_type_specifier (cp_parser* parser,
15759 cp_parser_flags flags,
15760 cp_decl_specifier_seq *decl_specs,
15761 bool is_declaration,
15762 int* declares_class_or_enum,
15763 bool* is_cv_qualifier)
15765 tree type_spec = NULL_TREE;
15766 cp_token *token;
15767 enum rid keyword;
15768 cp_decl_spec ds = ds_last;
15770 /* Assume this type-specifier does not declare a new type. */
15771 if (declares_class_or_enum)
15772 *declares_class_or_enum = 0;
15773 /* And that it does not specify a cv-qualifier. */
15774 if (is_cv_qualifier)
15775 *is_cv_qualifier = false;
15776 /* Peek at the next token. */
15777 token = cp_lexer_peek_token (parser->lexer);
15779 /* If we're looking at a keyword, we can use that to guide the
15780 production we choose. */
15781 keyword = token->keyword;
15782 switch (keyword)
15784 case RID_ENUM:
15785 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15786 goto elaborated_type_specifier;
15788 /* Look for the enum-specifier. */
15789 type_spec = cp_parser_enum_specifier (parser);
15790 /* If that worked, we're done. */
15791 if (type_spec)
15793 if (declares_class_or_enum)
15794 *declares_class_or_enum = 2;
15795 if (decl_specs)
15796 cp_parser_set_decl_spec_type (decl_specs,
15797 type_spec,
15798 token,
15799 /*type_definition_p=*/true);
15800 return type_spec;
15802 else
15803 goto elaborated_type_specifier;
15805 /* Any of these indicate either a class-specifier, or an
15806 elaborated-type-specifier. */
15807 case RID_CLASS:
15808 case RID_STRUCT:
15809 case RID_UNION:
15810 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15811 goto elaborated_type_specifier;
15813 /* Parse tentatively so that we can back up if we don't find a
15814 class-specifier. */
15815 cp_parser_parse_tentatively (parser);
15816 /* Look for the class-specifier. */
15817 type_spec = cp_parser_class_specifier (parser);
15818 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
15819 /* If that worked, we're done. */
15820 if (cp_parser_parse_definitely (parser))
15822 if (declares_class_or_enum)
15823 *declares_class_or_enum = 2;
15824 if (decl_specs)
15825 cp_parser_set_decl_spec_type (decl_specs,
15826 type_spec,
15827 token,
15828 /*type_definition_p=*/true);
15829 return type_spec;
15832 /* Fall through. */
15833 elaborated_type_specifier:
15834 /* We're declaring (not defining) a class or enum. */
15835 if (declares_class_or_enum)
15836 *declares_class_or_enum = 1;
15838 /* Fall through. */
15839 case RID_TYPENAME:
15840 /* Look for an elaborated-type-specifier. */
15841 type_spec
15842 = (cp_parser_elaborated_type_specifier
15843 (parser,
15844 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
15845 is_declaration));
15846 if (decl_specs)
15847 cp_parser_set_decl_spec_type (decl_specs,
15848 type_spec,
15849 token,
15850 /*type_definition_p=*/false);
15851 return type_spec;
15853 case RID_CONST:
15854 ds = ds_const;
15855 if (is_cv_qualifier)
15856 *is_cv_qualifier = true;
15857 break;
15859 case RID_VOLATILE:
15860 ds = ds_volatile;
15861 if (is_cv_qualifier)
15862 *is_cv_qualifier = true;
15863 break;
15865 case RID_RESTRICT:
15866 ds = ds_restrict;
15867 if (is_cv_qualifier)
15868 *is_cv_qualifier = true;
15869 break;
15871 case RID_COMPLEX:
15872 /* The `__complex__' keyword is a GNU extension. */
15873 ds = ds_complex;
15874 break;
15876 default:
15877 break;
15880 /* Handle simple keywords. */
15881 if (ds != ds_last)
15883 if (decl_specs)
15885 set_and_check_decl_spec_loc (decl_specs, ds, token);
15886 decl_specs->any_specifiers_p = true;
15888 return cp_lexer_consume_token (parser->lexer)->u.value;
15891 /* If we do not already have a type-specifier, assume we are looking
15892 at a simple-type-specifier. */
15893 type_spec = cp_parser_simple_type_specifier (parser,
15894 decl_specs,
15895 flags);
15897 /* If we didn't find a type-specifier, and a type-specifier was not
15898 optional in this context, issue an error message. */
15899 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15901 cp_parser_error (parser, "expected type specifier");
15902 return error_mark_node;
15905 return type_spec;
15908 /* Parse a simple-type-specifier.
15910 simple-type-specifier:
15911 :: [opt] nested-name-specifier [opt] type-name
15912 :: [opt] nested-name-specifier template template-id
15913 char
15914 wchar_t
15915 bool
15916 short
15918 long
15919 signed
15920 unsigned
15921 float
15922 double
15923 void
15925 C++0x Extension:
15927 simple-type-specifier:
15928 auto
15929 decltype ( expression )
15930 char16_t
15931 char32_t
15932 __underlying_type ( type-id )
15934 GNU Extension:
15936 simple-type-specifier:
15937 __int128
15938 __typeof__ unary-expression
15939 __typeof__ ( type-id )
15940 __typeof__ ( type-id ) { initializer-list , [opt] }
15942 Concepts Extension:
15944 simple-type-specifier:
15945 constrained-type-specifier
15947 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
15948 appropriately updated. */
15950 static tree
15951 cp_parser_simple_type_specifier (cp_parser* parser,
15952 cp_decl_specifier_seq *decl_specs,
15953 cp_parser_flags flags)
15955 tree type = NULL_TREE;
15956 cp_token *token;
15957 int idx;
15959 /* Peek at the next token. */
15960 token = cp_lexer_peek_token (parser->lexer);
15962 /* If we're looking at a keyword, things are easy. */
15963 switch (token->keyword)
15965 case RID_CHAR:
15966 if (decl_specs)
15967 decl_specs->explicit_char_p = true;
15968 type = char_type_node;
15969 break;
15970 case RID_CHAR16:
15971 type = char16_type_node;
15972 break;
15973 case RID_CHAR32:
15974 type = char32_type_node;
15975 break;
15976 case RID_WCHAR:
15977 type = wchar_type_node;
15978 break;
15979 case RID_BOOL:
15980 type = boolean_type_node;
15981 break;
15982 case RID_SHORT:
15983 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
15984 type = short_integer_type_node;
15985 break;
15986 case RID_INT:
15987 if (decl_specs)
15988 decl_specs->explicit_int_p = true;
15989 type = integer_type_node;
15990 break;
15991 case RID_INT_N_0:
15992 case RID_INT_N_1:
15993 case RID_INT_N_2:
15994 case RID_INT_N_3:
15995 idx = token->keyword - RID_INT_N_0;
15996 if (! int_n_enabled_p [idx])
15997 break;
15998 if (decl_specs)
16000 decl_specs->explicit_intN_p = true;
16001 decl_specs->int_n_idx = idx;
16003 type = int_n_trees [idx].signed_type;
16004 break;
16005 case RID_LONG:
16006 if (decl_specs)
16007 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16008 type = long_integer_type_node;
16009 break;
16010 case RID_SIGNED:
16011 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16012 type = integer_type_node;
16013 break;
16014 case RID_UNSIGNED:
16015 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16016 type = unsigned_type_node;
16017 break;
16018 case RID_FLOAT:
16019 type = float_type_node;
16020 break;
16021 case RID_DOUBLE:
16022 type = double_type_node;
16023 break;
16024 case RID_VOID:
16025 type = void_type_node;
16026 break;
16028 case RID_AUTO:
16029 maybe_warn_cpp0x (CPP0X_AUTO);
16030 if (parser->auto_is_implicit_function_template_parm_p)
16032 /* The 'auto' might be the placeholder return type for a function decl
16033 with trailing return type. */
16034 bool have_trailing_return_fn_decl = false;
16036 cp_parser_parse_tentatively (parser);
16037 cp_lexer_consume_token (parser->lexer);
16038 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16039 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16040 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16041 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16043 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16045 cp_lexer_consume_token (parser->lexer);
16046 cp_parser_skip_to_closing_parenthesis (parser,
16047 /*recovering*/false,
16048 /*or_comma*/false,
16049 /*consume_paren*/true);
16050 continue;
16053 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16055 have_trailing_return_fn_decl = true;
16056 break;
16059 cp_lexer_consume_token (parser->lexer);
16061 cp_parser_abort_tentative_parse (parser);
16063 if (have_trailing_return_fn_decl)
16065 type = make_auto ();
16066 break;
16069 if (cxx_dialect >= cxx14)
16071 type = synthesize_implicit_template_parm (parser, NULL_TREE);
16072 type = TREE_TYPE (type);
16074 else
16075 type = error_mark_node;
16077 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16079 if (cxx_dialect < cxx14)
16080 error_at (token->location,
16081 "use of %<auto%> in lambda parameter declaration "
16082 "only available with "
16083 "-std=c++14 or -std=gnu++14");
16085 else if (cxx_dialect < cxx14)
16086 error_at (token->location,
16087 "use of %<auto%> in parameter declaration "
16088 "only available with "
16089 "-std=c++14 or -std=gnu++14");
16090 else if (!flag_concepts)
16091 pedwarn (token->location, OPT_Wpedantic,
16092 "ISO C++ forbids use of %<auto%> in parameter "
16093 "declaration");
16095 else
16096 type = make_auto ();
16097 break;
16099 case RID_DECLTYPE:
16100 /* Since DR 743, decltype can either be a simple-type-specifier by
16101 itself or begin a nested-name-specifier. Parsing it will replace
16102 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16103 handling below decide what to do. */
16104 cp_parser_decltype (parser);
16105 cp_lexer_set_token_position (parser->lexer, token);
16106 break;
16108 case RID_TYPEOF:
16109 /* Consume the `typeof' token. */
16110 cp_lexer_consume_token (parser->lexer);
16111 /* Parse the operand to `typeof'. */
16112 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
16113 /* If it is not already a TYPE, take its type. */
16114 if (!TYPE_P (type))
16115 type = finish_typeof (type);
16117 if (decl_specs)
16118 cp_parser_set_decl_spec_type (decl_specs, type,
16119 token,
16120 /*type_definition_p=*/false);
16122 return type;
16124 case RID_UNDERLYING_TYPE:
16125 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
16126 if (decl_specs)
16127 cp_parser_set_decl_spec_type (decl_specs, type,
16128 token,
16129 /*type_definition_p=*/false);
16131 return type;
16133 case RID_BASES:
16134 case RID_DIRECT_BASES:
16135 type = cp_parser_trait_expr (parser, token->keyword);
16136 if (decl_specs)
16137 cp_parser_set_decl_spec_type (decl_specs, type,
16138 token,
16139 /*type_definition_p=*/false);
16140 return type;
16141 default:
16142 break;
16145 /* If token is an already-parsed decltype not followed by ::,
16146 it's a simple-type-specifier. */
16147 if (token->type == CPP_DECLTYPE
16148 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
16150 type = saved_checks_value (token->u.tree_check_value);
16151 if (decl_specs)
16153 cp_parser_set_decl_spec_type (decl_specs, type,
16154 token,
16155 /*type_definition_p=*/false);
16156 /* Remember that we are handling a decltype in order to
16157 implement the resolution of DR 1510 when the argument
16158 isn't instantiation dependent. */
16159 decl_specs->decltype_p = true;
16161 cp_lexer_consume_token (parser->lexer);
16162 return type;
16165 /* If the type-specifier was for a built-in type, we're done. */
16166 if (type)
16168 /* Record the type. */
16169 if (decl_specs
16170 && (token->keyword != RID_SIGNED
16171 && token->keyword != RID_UNSIGNED
16172 && token->keyword != RID_SHORT
16173 && token->keyword != RID_LONG))
16174 cp_parser_set_decl_spec_type (decl_specs,
16175 type,
16176 token,
16177 /*type_definition_p=*/false);
16178 if (decl_specs)
16179 decl_specs->any_specifiers_p = true;
16181 /* Consume the token. */
16182 cp_lexer_consume_token (parser->lexer);
16184 if (type == error_mark_node)
16185 return error_mark_node;
16187 /* There is no valid C++ program where a non-template type is
16188 followed by a "<". That usually indicates that the user thought
16189 that the type was a template. */
16190 cp_parser_check_for_invalid_template_id (parser, type, none_type,
16191 token->location);
16193 return TYPE_NAME (type);
16196 /* The type-specifier must be a user-defined type. */
16197 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
16199 bool qualified_p;
16200 bool global_p;
16202 /* Don't gobble tokens or issue error messages if this is an
16203 optional type-specifier. */
16204 if (flags & CP_PARSER_FLAGS_OPTIONAL)
16205 cp_parser_parse_tentatively (parser);
16207 /* Look for the optional `::' operator. */
16208 global_p
16209 = (cp_parser_global_scope_opt (parser,
16210 /*current_scope_valid_p=*/false)
16211 != NULL_TREE);
16212 /* Look for the nested-name specifier. */
16213 qualified_p
16214 = (cp_parser_nested_name_specifier_opt (parser,
16215 /*typename_keyword_p=*/false,
16216 /*check_dependency_p=*/true,
16217 /*type_p=*/false,
16218 /*is_declaration=*/false)
16219 != NULL_TREE);
16220 token = cp_lexer_peek_token (parser->lexer);
16221 /* If we have seen a nested-name-specifier, and the next token
16222 is `template', then we are using the template-id production. */
16223 if (parser->scope
16224 && cp_parser_optional_template_keyword (parser))
16226 /* Look for the template-id. */
16227 type = cp_parser_template_id (parser,
16228 /*template_keyword_p=*/true,
16229 /*check_dependency_p=*/true,
16230 none_type,
16231 /*is_declaration=*/false);
16232 /* If the template-id did not name a type, we are out of
16233 luck. */
16234 if (TREE_CODE (type) != TYPE_DECL)
16236 cp_parser_error (parser, "expected template-id for type");
16237 type = NULL_TREE;
16240 /* Otherwise, look for a type-name. */
16241 else
16242 type = cp_parser_type_name (parser);
16243 /* Keep track of all name-lookups performed in class scopes. */
16244 if (type
16245 && !global_p
16246 && !qualified_p
16247 && TREE_CODE (type) == TYPE_DECL
16248 && identifier_p (DECL_NAME (type)))
16249 maybe_note_name_used_in_class (DECL_NAME (type), type);
16250 /* If it didn't work out, we don't have a TYPE. */
16251 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
16252 && !cp_parser_parse_definitely (parser))
16253 type = NULL_TREE;
16254 if (type && decl_specs)
16255 cp_parser_set_decl_spec_type (decl_specs, type,
16256 token,
16257 /*type_definition_p=*/false);
16260 /* If we didn't get a type-name, issue an error message. */
16261 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16263 cp_parser_error (parser, "expected type-name");
16264 return error_mark_node;
16267 if (type && type != error_mark_node)
16269 /* See if TYPE is an Objective-C type, and if so, parse and
16270 accept any protocol references following it. Do this before
16271 the cp_parser_check_for_invalid_template_id() call, because
16272 Objective-C types can be followed by '<...>' which would
16273 enclose protocol names rather than template arguments, and so
16274 everything is fine. */
16275 if (c_dialect_objc () && !parser->scope
16276 && (objc_is_id (type) || objc_is_class_name (type)))
16278 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16279 tree qual_type = objc_get_protocol_qualified_type (type, protos);
16281 /* Clobber the "unqualified" type previously entered into
16282 DECL_SPECS with the new, improved protocol-qualified version. */
16283 if (decl_specs)
16284 decl_specs->type = qual_type;
16286 return qual_type;
16289 /* There is no valid C++ program where a non-template type is
16290 followed by a "<". That usually indicates that the user
16291 thought that the type was a template. */
16292 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
16293 none_type,
16294 token->location);
16297 return type;
16300 /* Parse a type-name.
16302 type-name:
16303 class-name
16304 enum-name
16305 typedef-name
16306 simple-template-id [in c++0x]
16308 enum-name:
16309 identifier
16311 typedef-name:
16312 identifier
16314 Concepts:
16316 type-name:
16317 concept-name
16318 partial-concept-id
16320 concept-name:
16321 identifier
16323 Returns a TYPE_DECL for the type. */
16325 static tree
16326 cp_parser_type_name (cp_parser* parser)
16328 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
16331 /* See above. */
16332 static tree
16333 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
16335 tree type_decl;
16337 /* We can't know yet whether it is a class-name or not. */
16338 cp_parser_parse_tentatively (parser);
16339 /* Try a class-name. */
16340 type_decl = cp_parser_class_name (parser,
16341 typename_keyword_p,
16342 /*template_keyword_p=*/false,
16343 none_type,
16344 /*check_dependency_p=*/true,
16345 /*class_head_p=*/false,
16346 /*is_declaration=*/false);
16347 /* If it's not a class-name, keep looking. */
16348 if (!cp_parser_parse_definitely (parser))
16350 if (cxx_dialect < cxx11)
16351 /* It must be a typedef-name or an enum-name. */
16352 return cp_parser_nonclass_name (parser);
16354 cp_parser_parse_tentatively (parser);
16355 /* It is either a simple-template-id representing an
16356 instantiation of an alias template... */
16357 type_decl = cp_parser_template_id (parser,
16358 /*template_keyword_p=*/false,
16359 /*check_dependency_p=*/true,
16360 none_type,
16361 /*is_declaration=*/false);
16362 /* Note that this must be an instantiation of an alias template
16363 because [temp.names]/6 says:
16365 A template-id that names an alias template specialization
16366 is a type-name.
16368 Whereas [temp.names]/7 says:
16370 A simple-template-id that names a class template
16371 specialization is a class-name.
16373 With concepts, this could also be a partial-concept-id that
16374 declares a non-type template parameter. */
16375 if (type_decl != NULL_TREE
16376 && TREE_CODE (type_decl) == TYPE_DECL
16377 && TYPE_DECL_ALIAS_P (type_decl))
16378 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
16379 else if (is_constrained_parameter (type_decl))
16380 /* Don't do anything. */ ;
16381 else
16382 cp_parser_simulate_error (parser);
16384 if (!cp_parser_parse_definitely (parser))
16385 /* ... Or a typedef-name or an enum-name. */
16386 return cp_parser_nonclass_name (parser);
16389 return type_decl;
16392 /* Check if DECL and ARGS can form a constrained-type-specifier.
16393 If ARGS is non-null, we try to form a concept check of the
16394 form DECL<?, ARGS> where ? is a wildcard that matches any
16395 kind of template argument. If ARGS is NULL, then we try to
16396 form a concept check of the form DECL<?>. */
16398 static tree
16399 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
16400 tree decl, tree args)
16402 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
16404 /* If we a constrained-type-specifier cannot be deduced. */
16405 if (parser->prevent_constrained_type_specifiers)
16406 return NULL_TREE;
16408 /* A constrained type specifier can only be found in an
16409 overload set or as a reference to a template declaration.
16411 FIXME: This might be masking a bug. It's possible that
16412 that the deduction below is causing template specializations
16413 to be formed with the wildcard as an argument. */
16414 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
16415 return NULL_TREE;
16417 /* Try to build a call expression that evaluates the
16418 concept. This can fail if the overload set refers
16419 only to non-templates. */
16420 tree placeholder = build_nt (WILDCARD_DECL);
16421 tree check = build_concept_check (decl, placeholder, args);
16422 if (check == error_mark_node)
16423 return NULL_TREE;
16425 /* Deduce the checked constraint and the prototype parameter.
16427 FIXME: In certain cases, failure to deduce should be a
16428 diagnosable error. */
16429 tree conc;
16430 tree proto;
16431 if (!deduce_constrained_parameter (check, conc, proto))
16432 return NULL_TREE;
16434 /* In template parameter scope, this results in a constrained
16435 parameter. Return a descriptor of that parm. */
16436 if (processing_template_parmlist)
16437 return build_constrained_parameter (conc, proto, args);
16439 /* In a parameter-declaration-clause, constrained-type
16440 specifiers result in invented template parameters. */
16441 if (parser->auto_is_implicit_function_template_parm_p)
16443 tree x = build_constrained_parameter (conc, proto, args);
16444 return synthesize_implicit_template_parm (parser, x);
16446 else
16448 /* Otherwise, we're in a context where the constrained
16449 type name is deduced and the constraint applies
16450 after deduction. */
16451 return make_constrained_auto (conc, args);
16454 return NULL_TREE;
16457 /* If DECL refers to a concept, return a TYPE_DECL representing
16458 the result of using the constrained type specifier in the
16459 current context. DECL refers to a concept if
16461 - it is an overload set containing a function concept taking a single
16462 type argument, or
16464 - it is a variable concept taking a single type argument. */
16466 static tree
16467 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
16469 if (flag_concepts
16470 && (TREE_CODE (decl) == OVERLOAD
16471 || BASELINK_P (decl)
16472 || variable_concept_p (decl)))
16473 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
16474 else
16475 return NULL_TREE;
16478 /* Check if DECL and ARGS form a partial-concept-id. If so,
16479 assign ID to the resulting constrained placeholder.
16481 Returns true if the partial-concept-id designates a placeholder
16482 and false otherwise. Note that *id is set to NULL_TREE in
16483 this case. */
16485 static tree
16486 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
16488 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
16491 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
16492 or a concept-name.
16494 enum-name:
16495 identifier
16497 typedef-name:
16498 identifier
16500 concept-name:
16501 identifier
16503 Returns a TYPE_DECL for the type. */
16505 static tree
16506 cp_parser_nonclass_name (cp_parser* parser)
16508 tree type_decl;
16509 tree identifier;
16511 cp_token *token = cp_lexer_peek_token (parser->lexer);
16512 identifier = cp_parser_identifier (parser);
16513 if (identifier == error_mark_node)
16514 return error_mark_node;
16516 /* Look up the type-name. */
16517 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
16519 type_decl = strip_using_decl (type_decl);
16521 /* If we found an overload set, then it may refer to a concept-name. */
16522 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
16523 type_decl = decl;
16525 if (TREE_CODE (type_decl) != TYPE_DECL
16526 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
16528 /* See if this is an Objective-C type. */
16529 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16530 tree type = objc_get_protocol_qualified_type (identifier, protos);
16531 if (type)
16532 type_decl = TYPE_NAME (type);
16535 /* Issue an error if we did not find a type-name. */
16536 if (TREE_CODE (type_decl) != TYPE_DECL
16537 /* In Objective-C, we have the complication that class names are
16538 normally type names and start declarations (eg, the
16539 "NSObject" in "NSObject *object;"), but can be used in an
16540 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
16541 is an expression. So, a classname followed by a dot is not a
16542 valid type-name. */
16543 || (objc_is_class_name (TREE_TYPE (type_decl))
16544 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
16546 if (!cp_parser_simulate_error (parser))
16547 cp_parser_name_lookup_error (parser, identifier, type_decl,
16548 NLE_TYPE, token->location);
16549 return error_mark_node;
16551 /* Remember that the name was used in the definition of the
16552 current class so that we can check later to see if the
16553 meaning would have been different after the class was
16554 entirely defined. */
16555 else if (type_decl != error_mark_node
16556 && !parser->scope)
16557 maybe_note_name_used_in_class (identifier, type_decl);
16559 return type_decl;
16562 /* Parse an elaborated-type-specifier. Note that the grammar given
16563 here incorporates the resolution to DR68.
16565 elaborated-type-specifier:
16566 class-key :: [opt] nested-name-specifier [opt] identifier
16567 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
16568 enum-key :: [opt] nested-name-specifier [opt] identifier
16569 typename :: [opt] nested-name-specifier identifier
16570 typename :: [opt] nested-name-specifier template [opt]
16571 template-id
16573 GNU extension:
16575 elaborated-type-specifier:
16576 class-key attributes :: [opt] nested-name-specifier [opt] identifier
16577 class-key attributes :: [opt] nested-name-specifier [opt]
16578 template [opt] template-id
16579 enum attributes :: [opt] nested-name-specifier [opt] identifier
16581 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
16582 declared `friend'. If IS_DECLARATION is TRUE, then this
16583 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
16584 something is being declared.
16586 Returns the TYPE specified. */
16588 static tree
16589 cp_parser_elaborated_type_specifier (cp_parser* parser,
16590 bool is_friend,
16591 bool is_declaration)
16593 enum tag_types tag_type;
16594 tree identifier;
16595 tree type = NULL_TREE;
16596 tree attributes = NULL_TREE;
16597 tree globalscope;
16598 cp_token *token = NULL;
16600 /* See if we're looking at the `enum' keyword. */
16601 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
16603 /* Consume the `enum' token. */
16604 cp_lexer_consume_token (parser->lexer);
16605 /* Remember that it's an enumeration type. */
16606 tag_type = enum_type;
16607 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
16608 enums) is used here. */
16609 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
16610 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
16612 pedwarn (input_location, 0, "elaborated-type-specifier "
16613 "for a scoped enum must not use the %<%D%> keyword",
16614 cp_lexer_peek_token (parser->lexer)->u.value);
16615 /* Consume the `struct' or `class' and parse it anyway. */
16616 cp_lexer_consume_token (parser->lexer);
16618 /* Parse the attributes. */
16619 attributes = cp_parser_attributes_opt (parser);
16621 /* Or, it might be `typename'. */
16622 else if (cp_lexer_next_token_is_keyword (parser->lexer,
16623 RID_TYPENAME))
16625 /* Consume the `typename' token. */
16626 cp_lexer_consume_token (parser->lexer);
16627 /* Remember that it's a `typename' type. */
16628 tag_type = typename_type;
16630 /* Otherwise it must be a class-key. */
16631 else
16633 tag_type = cp_parser_class_key (parser);
16634 if (tag_type == none_type)
16635 return error_mark_node;
16636 /* Parse the attributes. */
16637 attributes = cp_parser_attributes_opt (parser);
16640 /* Look for the `::' operator. */
16641 globalscope = cp_parser_global_scope_opt (parser,
16642 /*current_scope_valid_p=*/false);
16643 /* Look for the nested-name-specifier. */
16644 if (tag_type == typename_type && !globalscope)
16646 if (!cp_parser_nested_name_specifier (parser,
16647 /*typename_keyword_p=*/true,
16648 /*check_dependency_p=*/true,
16649 /*type_p=*/true,
16650 is_declaration))
16651 return error_mark_node;
16653 else
16654 /* Even though `typename' is not present, the proposed resolution
16655 to Core Issue 180 says that in `class A<T>::B', `B' should be
16656 considered a type-name, even if `A<T>' is dependent. */
16657 cp_parser_nested_name_specifier_opt (parser,
16658 /*typename_keyword_p=*/true,
16659 /*check_dependency_p=*/true,
16660 /*type_p=*/true,
16661 is_declaration);
16662 /* For everything but enumeration types, consider a template-id.
16663 For an enumeration type, consider only a plain identifier. */
16664 if (tag_type != enum_type)
16666 bool template_p = false;
16667 tree decl;
16669 /* Allow the `template' keyword. */
16670 template_p = cp_parser_optional_template_keyword (parser);
16671 /* If we didn't see `template', we don't know if there's a
16672 template-id or not. */
16673 if (!template_p)
16674 cp_parser_parse_tentatively (parser);
16675 /* Parse the template-id. */
16676 token = cp_lexer_peek_token (parser->lexer);
16677 decl = cp_parser_template_id (parser, template_p,
16678 /*check_dependency_p=*/true,
16679 tag_type,
16680 is_declaration);
16681 /* If we didn't find a template-id, look for an ordinary
16682 identifier. */
16683 if (!template_p && !cp_parser_parse_definitely (parser))
16685 /* We can get here when cp_parser_template_id, called by
16686 cp_parser_class_name with tag_type == none_type, succeeds
16687 and caches a BASELINK. Then, when called again here,
16688 instead of failing and returning an error_mark_node
16689 returns it (see template/typename17.C in C++11).
16690 ??? Could we diagnose this earlier? */
16691 else if (tag_type == typename_type && BASELINK_P (decl))
16693 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
16694 type = error_mark_node;
16696 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
16697 in effect, then we must assume that, upon instantiation, the
16698 template will correspond to a class. */
16699 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16700 && tag_type == typename_type)
16701 type = make_typename_type (parser->scope, decl,
16702 typename_type,
16703 /*complain=*/tf_error);
16704 /* If the `typename' keyword is in effect and DECL is not a type
16705 decl, then type is non existent. */
16706 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
16708 else if (TREE_CODE (decl) == TYPE_DECL)
16709 type = check_elaborated_type_specifier (tag_type, decl,
16710 /*allow_template_p=*/true);
16711 else if (decl == error_mark_node)
16712 type = error_mark_node;
16715 if (!type)
16717 token = cp_lexer_peek_token (parser->lexer);
16718 identifier = cp_parser_identifier (parser);
16720 if (identifier == error_mark_node)
16722 parser->scope = NULL_TREE;
16723 return error_mark_node;
16726 /* For a `typename', we needn't call xref_tag. */
16727 if (tag_type == typename_type
16728 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
16729 return cp_parser_make_typename_type (parser, identifier,
16730 token->location);
16732 /* Template parameter lists apply only if we are not within a
16733 function parameter list. */
16734 bool template_parm_lists_apply
16735 = parser->num_template_parameter_lists;
16736 if (template_parm_lists_apply)
16737 for (cp_binding_level *s = current_binding_level;
16738 s && s->kind != sk_template_parms;
16739 s = s->level_chain)
16740 if (s->kind == sk_function_parms)
16741 template_parm_lists_apply = false;
16743 /* Look up a qualified name in the usual way. */
16744 if (parser->scope)
16746 tree decl;
16747 tree ambiguous_decls;
16749 decl = cp_parser_lookup_name (parser, identifier,
16750 tag_type,
16751 /*is_template=*/false,
16752 /*is_namespace=*/false,
16753 /*check_dependency=*/true,
16754 &ambiguous_decls,
16755 token->location);
16757 /* If the lookup was ambiguous, an error will already have been
16758 issued. */
16759 if (ambiguous_decls)
16760 return error_mark_node;
16762 /* If we are parsing friend declaration, DECL may be a
16763 TEMPLATE_DECL tree node here. However, we need to check
16764 whether this TEMPLATE_DECL results in valid code. Consider
16765 the following example:
16767 namespace N {
16768 template <class T> class C {};
16770 class X {
16771 template <class T> friend class N::C; // #1, valid code
16773 template <class T> class Y {
16774 friend class N::C; // #2, invalid code
16777 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
16778 name lookup of `N::C'. We see that friend declaration must
16779 be template for the code to be valid. Note that
16780 processing_template_decl does not work here since it is
16781 always 1 for the above two cases. */
16783 decl = (cp_parser_maybe_treat_template_as_class
16784 (decl, /*tag_name_p=*/is_friend
16785 && template_parm_lists_apply));
16787 if (TREE_CODE (decl) != TYPE_DECL)
16789 cp_parser_diagnose_invalid_type_name (parser,
16790 identifier,
16791 token->location);
16792 return error_mark_node;
16795 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
16797 bool allow_template = (template_parm_lists_apply
16798 || DECL_SELF_REFERENCE_P (decl));
16799 type = check_elaborated_type_specifier (tag_type, decl,
16800 allow_template);
16802 if (type == error_mark_node)
16803 return error_mark_node;
16806 /* Forward declarations of nested types, such as
16808 class C1::C2;
16809 class C1::C2::C3;
16811 are invalid unless all components preceding the final '::'
16812 are complete. If all enclosing types are complete, these
16813 declarations become merely pointless.
16815 Invalid forward declarations of nested types are errors
16816 caught elsewhere in parsing. Those that are pointless arrive
16817 here. */
16819 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16820 && !is_friend && !processing_explicit_instantiation)
16821 warning (0, "declaration %qD does not declare anything", decl);
16823 type = TREE_TYPE (decl);
16825 else
16827 /* An elaborated-type-specifier sometimes introduces a new type and
16828 sometimes names an existing type. Normally, the rule is that it
16829 introduces a new type only if there is not an existing type of
16830 the same name already in scope. For example, given:
16832 struct S {};
16833 void f() { struct S s; }
16835 the `struct S' in the body of `f' is the same `struct S' as in
16836 the global scope; the existing definition is used. However, if
16837 there were no global declaration, this would introduce a new
16838 local class named `S'.
16840 An exception to this rule applies to the following code:
16842 namespace N { struct S; }
16844 Here, the elaborated-type-specifier names a new type
16845 unconditionally; even if there is already an `S' in the
16846 containing scope this declaration names a new type.
16847 This exception only applies if the elaborated-type-specifier
16848 forms the complete declaration:
16850 [class.name]
16852 A declaration consisting solely of `class-key identifier ;' is
16853 either a redeclaration of the name in the current scope or a
16854 forward declaration of the identifier as a class name. It
16855 introduces the name into the current scope.
16857 We are in this situation precisely when the next token is a `;'.
16859 An exception to the exception is that a `friend' declaration does
16860 *not* name a new type; i.e., given:
16862 struct S { friend struct T; };
16864 `T' is not a new type in the scope of `S'.
16866 Also, `new struct S' or `sizeof (struct S)' never results in the
16867 definition of a new type; a new type can only be declared in a
16868 declaration context. */
16870 tag_scope ts;
16871 bool template_p;
16873 if (is_friend)
16874 /* Friends have special name lookup rules. */
16875 ts = ts_within_enclosing_non_class;
16876 else if (is_declaration
16877 && cp_lexer_next_token_is (parser->lexer,
16878 CPP_SEMICOLON))
16879 /* This is a `class-key identifier ;' */
16880 ts = ts_current;
16881 else
16882 ts = ts_global;
16884 template_p =
16885 (template_parm_lists_apply
16886 && (cp_parser_next_token_starts_class_definition_p (parser)
16887 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
16888 /* An unqualified name was used to reference this type, so
16889 there were no qualifying templates. */
16890 if (template_parm_lists_apply
16891 && !cp_parser_check_template_parameters (parser,
16892 /*num_templates=*/0,
16893 token->location,
16894 /*declarator=*/NULL))
16895 return error_mark_node;
16896 type = xref_tag (tag_type, identifier, ts, template_p);
16900 if (type == error_mark_node)
16901 return error_mark_node;
16903 /* Allow attributes on forward declarations of classes. */
16904 if (attributes)
16906 if (TREE_CODE (type) == TYPENAME_TYPE)
16907 warning (OPT_Wattributes,
16908 "attributes ignored on uninstantiated type");
16909 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
16910 && ! processing_explicit_instantiation)
16911 warning (OPT_Wattributes,
16912 "attributes ignored on template instantiation");
16913 else if (is_declaration && cp_parser_declares_only_class_p (parser))
16914 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
16915 else
16916 warning (OPT_Wattributes,
16917 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
16920 if (tag_type != enum_type)
16922 /* Indicate whether this class was declared as a `class' or as a
16923 `struct'. */
16924 if (CLASS_TYPE_P (type))
16925 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
16926 cp_parser_check_class_key (tag_type, type);
16929 /* A "<" cannot follow an elaborated type specifier. If that
16930 happens, the user was probably trying to form a template-id. */
16931 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
16932 token->location);
16934 return type;
16937 /* Parse an enum-specifier.
16939 enum-specifier:
16940 enum-head { enumerator-list [opt] }
16941 enum-head { enumerator-list , } [C++0x]
16943 enum-head:
16944 enum-key identifier [opt] enum-base [opt]
16945 enum-key nested-name-specifier identifier enum-base [opt]
16947 enum-key:
16948 enum
16949 enum class [C++0x]
16950 enum struct [C++0x]
16952 enum-base: [C++0x]
16953 : type-specifier-seq
16955 opaque-enum-specifier:
16956 enum-key identifier enum-base [opt] ;
16958 GNU Extensions:
16959 enum-key attributes[opt] identifier [opt] enum-base [opt]
16960 { enumerator-list [opt] }attributes[opt]
16961 enum-key attributes[opt] identifier [opt] enum-base [opt]
16962 { enumerator-list, }attributes[opt] [C++0x]
16964 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
16965 if the token stream isn't an enum-specifier after all. */
16967 static tree
16968 cp_parser_enum_specifier (cp_parser* parser)
16970 tree identifier;
16971 tree type = NULL_TREE;
16972 tree prev_scope;
16973 tree nested_name_specifier = NULL_TREE;
16974 tree attributes;
16975 bool scoped_enum_p = false;
16976 bool has_underlying_type = false;
16977 bool nested_being_defined = false;
16978 bool new_value_list = false;
16979 bool is_new_type = false;
16980 bool is_anonymous = false;
16981 tree underlying_type = NULL_TREE;
16982 cp_token *type_start_token = NULL;
16983 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
16985 parser->colon_corrects_to_scope_p = false;
16987 /* Parse tentatively so that we can back up if we don't find a
16988 enum-specifier. */
16989 cp_parser_parse_tentatively (parser);
16991 /* Caller guarantees that the current token is 'enum', an identifier
16992 possibly follows, and the token after that is an opening brace.
16993 If we don't have an identifier, fabricate an anonymous name for
16994 the enumeration being defined. */
16995 cp_lexer_consume_token (parser->lexer);
16997 /* Parse the "class" or "struct", which indicates a scoped
16998 enumeration type in C++0x. */
16999 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17000 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17002 if (cxx_dialect < cxx11)
17003 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17005 /* Consume the `struct' or `class' token. */
17006 cp_lexer_consume_token (parser->lexer);
17008 scoped_enum_p = true;
17011 attributes = cp_parser_attributes_opt (parser);
17013 /* Clear the qualification. */
17014 parser->scope = NULL_TREE;
17015 parser->qualifying_scope = NULL_TREE;
17016 parser->object_scope = NULL_TREE;
17018 /* Figure out in what scope the declaration is being placed. */
17019 prev_scope = current_scope ();
17021 type_start_token = cp_lexer_peek_token (parser->lexer);
17023 push_deferring_access_checks (dk_no_check);
17024 nested_name_specifier
17025 = cp_parser_nested_name_specifier_opt (parser,
17026 /*typename_keyword_p=*/true,
17027 /*check_dependency_p=*/false,
17028 /*type_p=*/false,
17029 /*is_declaration=*/false);
17031 if (nested_name_specifier)
17033 tree name;
17035 identifier = cp_parser_identifier (parser);
17036 name = cp_parser_lookup_name (parser, identifier,
17037 enum_type,
17038 /*is_template=*/false,
17039 /*is_namespace=*/false,
17040 /*check_dependency=*/true,
17041 /*ambiguous_decls=*/NULL,
17042 input_location);
17043 if (name && name != error_mark_node)
17045 type = TREE_TYPE (name);
17046 if (TREE_CODE (type) == TYPENAME_TYPE)
17048 /* Are template enums allowed in ISO? */
17049 if (template_parm_scope_p ())
17050 pedwarn (type_start_token->location, OPT_Wpedantic,
17051 "%qD is an enumeration template", name);
17052 /* ignore a typename reference, for it will be solved by name
17053 in start_enum. */
17054 type = NULL_TREE;
17057 else if (nested_name_specifier == error_mark_node)
17058 /* We already issued an error. */;
17059 else
17061 error_at (type_start_token->location,
17062 "%qD does not name an enumeration in %qT",
17063 identifier, nested_name_specifier);
17064 nested_name_specifier = error_mark_node;
17067 else
17069 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17070 identifier = cp_parser_identifier (parser);
17071 else
17073 identifier = make_anon_name ();
17074 is_anonymous = true;
17075 if (scoped_enum_p)
17076 error_at (type_start_token->location,
17077 "anonymous scoped enum is not allowed");
17080 pop_deferring_access_checks ();
17082 /* Check for the `:' that denotes a specified underlying type in C++0x.
17083 Note that a ':' could also indicate a bitfield width, however. */
17084 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17086 cp_decl_specifier_seq type_specifiers;
17088 /* Consume the `:'. */
17089 cp_lexer_consume_token (parser->lexer);
17091 /* Parse the type-specifier-seq. */
17092 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17093 /*is_trailing_return=*/false,
17094 &type_specifiers);
17096 /* At this point this is surely not elaborated type specifier. */
17097 if (!cp_parser_parse_definitely (parser))
17098 return NULL_TREE;
17100 if (cxx_dialect < cxx11)
17101 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17103 has_underlying_type = true;
17105 /* If that didn't work, stop. */
17106 if (type_specifiers.type != error_mark_node)
17108 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
17109 /*initialized=*/0, NULL);
17110 if (underlying_type == error_mark_node
17111 || check_for_bare_parameter_packs (underlying_type))
17112 underlying_type = NULL_TREE;
17116 /* Look for the `{' but don't consume it yet. */
17117 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17119 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
17121 cp_parser_error (parser, "expected %<{%>");
17122 if (has_underlying_type)
17124 type = NULL_TREE;
17125 goto out;
17128 /* An opaque-enum-specifier must have a ';' here. */
17129 if ((scoped_enum_p || underlying_type)
17130 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17132 cp_parser_error (parser, "expected %<;%> or %<{%>");
17133 if (has_underlying_type)
17135 type = NULL_TREE;
17136 goto out;
17141 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
17142 return NULL_TREE;
17144 if (nested_name_specifier)
17146 if (CLASS_TYPE_P (nested_name_specifier))
17148 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
17149 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
17150 push_scope (nested_name_specifier);
17152 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17154 push_nested_namespace (nested_name_specifier);
17158 /* Issue an error message if type-definitions are forbidden here. */
17159 if (!cp_parser_check_type_definition (parser))
17160 type = error_mark_node;
17161 else
17162 /* Create the new type. We do this before consuming the opening
17163 brace so the enum will be recorded as being on the line of its
17164 tag (or the 'enum' keyword, if there is no tag). */
17165 type = start_enum (identifier, type, underlying_type,
17166 attributes, scoped_enum_p, &is_new_type);
17168 /* If the next token is not '{' it is an opaque-enum-specifier or an
17169 elaborated-type-specifier. */
17170 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17172 timevar_push (TV_PARSE_ENUM);
17173 if (nested_name_specifier
17174 && nested_name_specifier != error_mark_node)
17176 /* The following catches invalid code such as:
17177 enum class S<int>::E { A, B, C }; */
17178 if (!processing_specialization
17179 && CLASS_TYPE_P (nested_name_specifier)
17180 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
17181 error_at (type_start_token->location, "cannot add an enumerator "
17182 "list to a template instantiation");
17184 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
17186 error_at (type_start_token->location,
17187 "%<%T::%E%> has not been declared",
17188 TYPE_CONTEXT (nested_name_specifier),
17189 nested_name_specifier);
17190 type = error_mark_node;
17192 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
17193 && !CLASS_TYPE_P (nested_name_specifier))
17195 error_at (type_start_token->location, "nested name specifier "
17196 "%qT for enum declaration does not name a class "
17197 "or namespace", nested_name_specifier);
17198 type = error_mark_node;
17200 /* If that scope does not contain the scope in which the
17201 class was originally declared, the program is invalid. */
17202 else if (prev_scope && !is_ancestor (prev_scope,
17203 nested_name_specifier))
17205 if (at_namespace_scope_p ())
17206 error_at (type_start_token->location,
17207 "declaration of %qD in namespace %qD which does not "
17208 "enclose %qD",
17209 type, prev_scope, nested_name_specifier);
17210 else
17211 error_at (type_start_token->location,
17212 "declaration of %qD in %qD which does not "
17213 "enclose %qD",
17214 type, prev_scope, nested_name_specifier);
17215 type = error_mark_node;
17219 if (scoped_enum_p)
17220 begin_scope (sk_scoped_enum, type);
17222 /* Consume the opening brace. */
17223 cp_lexer_consume_token (parser->lexer);
17225 if (type == error_mark_node)
17226 ; /* Nothing to add */
17227 else if (OPAQUE_ENUM_P (type)
17228 || (cxx_dialect > cxx98 && processing_specialization))
17230 new_value_list = true;
17231 SET_OPAQUE_ENUM_P (type, false);
17232 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17234 else
17236 error_at (type_start_token->location,
17237 "multiple definition of %q#T", type);
17238 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
17239 "previous definition here");
17240 type = error_mark_node;
17243 if (type == error_mark_node)
17244 cp_parser_skip_to_end_of_block_or_statement (parser);
17245 /* If the next token is not '}', then there are some enumerators. */
17246 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17248 if (is_anonymous && !scoped_enum_p)
17249 pedwarn (type_start_token->location, OPT_Wpedantic,
17250 "ISO C++ forbids empty anonymous enum");
17252 else
17253 cp_parser_enumerator_list (parser, type);
17255 /* Consume the final '}'. */
17256 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17258 if (scoped_enum_p)
17259 finish_scope ();
17260 timevar_pop (TV_PARSE_ENUM);
17262 else
17264 /* If a ';' follows, then it is an opaque-enum-specifier
17265 and additional restrictions apply. */
17266 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17268 if (is_anonymous)
17269 error_at (type_start_token->location,
17270 "opaque-enum-specifier without name");
17271 else if (nested_name_specifier)
17272 error_at (type_start_token->location,
17273 "opaque-enum-specifier must use a simple identifier");
17277 /* Look for trailing attributes to apply to this enumeration, and
17278 apply them if appropriate. */
17279 if (cp_parser_allow_gnu_extensions_p (parser))
17281 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
17282 cplus_decl_attributes (&type,
17283 trailing_attr,
17284 (int) ATTR_FLAG_TYPE_IN_PLACE);
17287 /* Finish up the enumeration. */
17288 if (type != error_mark_node)
17290 if (new_value_list)
17291 finish_enum_value_list (type);
17292 if (is_new_type)
17293 finish_enum (type);
17296 if (nested_name_specifier)
17298 if (CLASS_TYPE_P (nested_name_specifier))
17300 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
17301 pop_scope (nested_name_specifier);
17303 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17305 pop_nested_namespace (nested_name_specifier);
17308 out:
17309 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17310 return type;
17313 /* Parse an enumerator-list. The enumerators all have the indicated
17314 TYPE.
17316 enumerator-list:
17317 enumerator-definition
17318 enumerator-list , enumerator-definition */
17320 static void
17321 cp_parser_enumerator_list (cp_parser* parser, tree type)
17323 while (true)
17325 /* Parse an enumerator-definition. */
17326 cp_parser_enumerator_definition (parser, type);
17328 /* If the next token is not a ',', we've reached the end of
17329 the list. */
17330 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17331 break;
17332 /* Otherwise, consume the `,' and keep going. */
17333 cp_lexer_consume_token (parser->lexer);
17334 /* If the next token is a `}', there is a trailing comma. */
17335 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17337 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
17338 pedwarn (input_location, OPT_Wpedantic,
17339 "comma at end of enumerator list");
17340 break;
17345 /* Parse an enumerator-definition. The enumerator has the indicated
17346 TYPE.
17348 enumerator-definition:
17349 enumerator
17350 enumerator = constant-expression
17352 enumerator:
17353 identifier
17355 GNU Extensions:
17357 enumerator-definition:
17358 enumerator attributes [opt]
17359 enumerator attributes [opt] = constant-expression */
17361 static void
17362 cp_parser_enumerator_definition (cp_parser* parser, tree type)
17364 tree identifier;
17365 tree value;
17366 location_t loc;
17368 /* Save the input location because we are interested in the location
17369 of the identifier and not the location of the explicit value. */
17370 loc = cp_lexer_peek_token (parser->lexer)->location;
17372 /* Look for the identifier. */
17373 identifier = cp_parser_identifier (parser);
17374 if (identifier == error_mark_node)
17375 return;
17377 /* Parse any specified attributes. */
17378 tree attrs = cp_parser_attributes_opt (parser);
17380 /* If the next token is an '=', then there is an explicit value. */
17381 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17383 /* Consume the `=' token. */
17384 cp_lexer_consume_token (parser->lexer);
17385 /* Parse the value. */
17386 value = cp_parser_constant_expression (parser);
17388 else
17389 value = NULL_TREE;
17391 /* If we are processing a template, make sure the initializer of the
17392 enumerator doesn't contain any bare template parameter pack. */
17393 if (check_for_bare_parameter_packs (value))
17394 value = error_mark_node;
17396 /* Create the enumerator. */
17397 build_enumerator (identifier, value, type, attrs, loc);
17400 /* Parse a namespace-name.
17402 namespace-name:
17403 original-namespace-name
17404 namespace-alias
17406 Returns the NAMESPACE_DECL for the namespace. */
17408 static tree
17409 cp_parser_namespace_name (cp_parser* parser)
17411 tree identifier;
17412 tree namespace_decl;
17414 cp_token *token = cp_lexer_peek_token (parser->lexer);
17416 /* Get the name of the namespace. */
17417 identifier = cp_parser_identifier (parser);
17418 if (identifier == error_mark_node)
17419 return error_mark_node;
17421 /* Look up the identifier in the currently active scope. Look only
17422 for namespaces, due to:
17424 [basic.lookup.udir]
17426 When looking up a namespace-name in a using-directive or alias
17427 definition, only namespace names are considered.
17429 And:
17431 [basic.lookup.qual]
17433 During the lookup of a name preceding the :: scope resolution
17434 operator, object, function, and enumerator names are ignored.
17436 (Note that cp_parser_qualifying_entity only calls this
17437 function if the token after the name is the scope resolution
17438 operator.) */
17439 namespace_decl = cp_parser_lookup_name (parser, identifier,
17440 none_type,
17441 /*is_template=*/false,
17442 /*is_namespace=*/true,
17443 /*check_dependency=*/true,
17444 /*ambiguous_decls=*/NULL,
17445 token->location);
17446 /* If it's not a namespace, issue an error. */
17447 if (namespace_decl == error_mark_node
17448 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
17450 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17451 error_at (token->location, "%qD is not a namespace-name", identifier);
17452 cp_parser_error (parser, "expected namespace-name");
17453 namespace_decl = error_mark_node;
17456 return namespace_decl;
17459 /* Parse a namespace-definition.
17461 namespace-definition:
17462 named-namespace-definition
17463 unnamed-namespace-definition
17465 named-namespace-definition:
17466 original-namespace-definition
17467 extension-namespace-definition
17469 original-namespace-definition:
17470 namespace identifier { namespace-body }
17472 extension-namespace-definition:
17473 namespace original-namespace-name { namespace-body }
17475 unnamed-namespace-definition:
17476 namespace { namespace-body } */
17478 static void
17479 cp_parser_namespace_definition (cp_parser* parser)
17481 tree identifier, attribs;
17482 bool has_visibility;
17483 bool is_inline;
17484 cp_token* token;
17485 int nested_definition_count = 0;
17487 cp_ensure_no_omp_declare_simd (parser);
17488 cp_ensure_no_oacc_routine (parser);
17489 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
17491 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
17492 is_inline = true;
17493 cp_lexer_consume_token (parser->lexer);
17495 else
17496 is_inline = false;
17498 /* Look for the `namespace' keyword. */
17499 token = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17501 /* Parse any specified attributes before the identifier. */
17502 attribs = cp_parser_attributes_opt (parser);
17504 /* Get the name of the namespace. We do not attempt to distinguish
17505 between an original-namespace-definition and an
17506 extension-namespace-definition at this point. The semantic
17507 analysis routines are responsible for that. */
17508 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17509 identifier = cp_parser_identifier (parser);
17510 else
17511 identifier = NULL_TREE;
17513 /* Parse any specified attributes after the identifier. */
17514 tree post_ident_attribs = cp_parser_attributes_opt (parser);
17515 if (post_ident_attribs)
17517 if (attribs)
17518 attribs = chainon (attribs, post_ident_attribs);
17519 else
17520 attribs = post_ident_attribs;
17523 /* Start the namespace. */
17524 push_namespace (identifier);
17526 /* Parse any nested namespace definition. */
17527 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17529 if (attribs)
17530 error_at (token->location, "a nested namespace definition cannot have attributes");
17531 if (cxx_dialect < cxx1z)
17532 pedwarn (input_location, OPT_Wpedantic,
17533 "nested namespace definitions only available with "
17534 "-std=c++1z or -std=gnu++1z");
17535 if (is_inline)
17536 error_at (token->location, "a nested namespace definition cannot be inline");
17537 while (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17539 cp_lexer_consume_token (parser->lexer);
17540 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17541 identifier = cp_parser_identifier (parser);
17542 else
17544 cp_parser_error (parser, "nested identifier required");
17545 break;
17547 ++nested_definition_count;
17548 push_namespace (identifier);
17552 /* Look for the `{' to validate starting the namespace. */
17553 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
17555 /* "inline namespace" is equivalent to a stub namespace definition
17556 followed by a strong using directive. */
17557 if (is_inline)
17559 tree name_space = current_namespace;
17560 /* Set up namespace association. */
17561 DECL_NAMESPACE_ASSOCIATIONS (name_space)
17562 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
17563 DECL_NAMESPACE_ASSOCIATIONS (name_space));
17564 /* Import the contents of the inline namespace. */
17565 pop_namespace ();
17566 do_using_directive (name_space);
17567 push_namespace (identifier);
17570 has_visibility = handle_namespace_attrs (current_namespace, attribs);
17572 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
17574 /* Parse the body of the namespace. */
17575 cp_parser_namespace_body (parser);
17577 if (has_visibility)
17578 pop_visibility (1);
17580 /* Finish the nested namespace definitions. */
17581 while (nested_definition_count--)
17582 pop_namespace ();
17584 /* Finish the namespace. */
17585 pop_namespace ();
17586 /* Look for the final `}'. */
17587 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17590 /* Parse a namespace-body.
17592 namespace-body:
17593 declaration-seq [opt] */
17595 static void
17596 cp_parser_namespace_body (cp_parser* parser)
17598 cp_parser_declaration_seq_opt (parser);
17601 /* Parse a namespace-alias-definition.
17603 namespace-alias-definition:
17604 namespace identifier = qualified-namespace-specifier ; */
17606 static void
17607 cp_parser_namespace_alias_definition (cp_parser* parser)
17609 tree identifier;
17610 tree namespace_specifier;
17612 cp_token *token = cp_lexer_peek_token (parser->lexer);
17614 /* Look for the `namespace' keyword. */
17615 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17616 /* Look for the identifier. */
17617 identifier = cp_parser_identifier (parser);
17618 if (identifier == error_mark_node)
17619 return;
17620 /* Look for the `=' token. */
17621 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
17622 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17624 error_at (token->location, "%<namespace%> definition is not allowed here");
17625 /* Skip the definition. */
17626 cp_lexer_consume_token (parser->lexer);
17627 if (cp_parser_skip_to_closing_brace (parser))
17628 cp_lexer_consume_token (parser->lexer);
17629 return;
17631 cp_parser_require (parser, CPP_EQ, RT_EQ);
17632 /* Look for the qualified-namespace-specifier. */
17633 namespace_specifier
17634 = cp_parser_qualified_namespace_specifier (parser);
17635 /* Look for the `;' token. */
17636 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17638 /* Register the alias in the symbol table. */
17639 do_namespace_alias (identifier, namespace_specifier);
17642 /* Parse a qualified-namespace-specifier.
17644 qualified-namespace-specifier:
17645 :: [opt] nested-name-specifier [opt] namespace-name
17647 Returns a NAMESPACE_DECL corresponding to the specified
17648 namespace. */
17650 static tree
17651 cp_parser_qualified_namespace_specifier (cp_parser* parser)
17653 /* Look for the optional `::'. */
17654 cp_parser_global_scope_opt (parser,
17655 /*current_scope_valid_p=*/false);
17657 /* Look for the optional nested-name-specifier. */
17658 cp_parser_nested_name_specifier_opt (parser,
17659 /*typename_keyword_p=*/false,
17660 /*check_dependency_p=*/true,
17661 /*type_p=*/false,
17662 /*is_declaration=*/true);
17664 return cp_parser_namespace_name (parser);
17667 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
17668 access declaration.
17670 using-declaration:
17671 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
17672 using :: unqualified-id ;
17674 access-declaration:
17675 qualified-id ;
17679 static bool
17680 cp_parser_using_declaration (cp_parser* parser,
17681 bool access_declaration_p)
17683 cp_token *token;
17684 bool typename_p = false;
17685 bool global_scope_p;
17686 tree decl;
17687 tree identifier;
17688 tree qscope;
17689 int oldcount = errorcount;
17690 cp_token *diag_token = NULL;
17692 if (access_declaration_p)
17694 diag_token = cp_lexer_peek_token (parser->lexer);
17695 cp_parser_parse_tentatively (parser);
17697 else
17699 /* Look for the `using' keyword. */
17700 cp_parser_require_keyword (parser, RID_USING, RT_USING);
17702 /* Peek at the next token. */
17703 token = cp_lexer_peek_token (parser->lexer);
17704 /* See if it's `typename'. */
17705 if (token->keyword == RID_TYPENAME)
17707 /* Remember that we've seen it. */
17708 typename_p = true;
17709 /* Consume the `typename' token. */
17710 cp_lexer_consume_token (parser->lexer);
17714 /* Look for the optional global scope qualification. */
17715 global_scope_p
17716 = (cp_parser_global_scope_opt (parser,
17717 /*current_scope_valid_p=*/false)
17718 != NULL_TREE);
17720 /* If we saw `typename', or didn't see `::', then there must be a
17721 nested-name-specifier present. */
17722 if (typename_p || !global_scope_p)
17724 qscope = cp_parser_nested_name_specifier (parser, typename_p,
17725 /*check_dependency_p=*/true,
17726 /*type_p=*/false,
17727 /*is_declaration=*/true);
17728 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
17730 cp_parser_skip_to_end_of_block_or_statement (parser);
17731 return false;
17734 /* Otherwise, we could be in either of the two productions. In that
17735 case, treat the nested-name-specifier as optional. */
17736 else
17737 qscope = cp_parser_nested_name_specifier_opt (parser,
17738 /*typename_keyword_p=*/false,
17739 /*check_dependency_p=*/true,
17740 /*type_p=*/false,
17741 /*is_declaration=*/true);
17742 if (!qscope)
17743 qscope = global_namespace;
17744 else if (UNSCOPED_ENUM_P (qscope))
17745 qscope = CP_TYPE_CONTEXT (qscope);
17747 if (access_declaration_p && cp_parser_error_occurred (parser))
17748 /* Something has already gone wrong; there's no need to parse
17749 further. Since an error has occurred, the return value of
17750 cp_parser_parse_definitely will be false, as required. */
17751 return cp_parser_parse_definitely (parser);
17753 token = cp_lexer_peek_token (parser->lexer);
17754 /* Parse the unqualified-id. */
17755 identifier = cp_parser_unqualified_id (parser,
17756 /*template_keyword_p=*/false,
17757 /*check_dependency_p=*/true,
17758 /*declarator_p=*/true,
17759 /*optional_p=*/false);
17761 if (access_declaration_p)
17763 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17764 cp_parser_simulate_error (parser);
17765 if (!cp_parser_parse_definitely (parser))
17766 return false;
17769 /* The function we call to handle a using-declaration is different
17770 depending on what scope we are in. */
17771 if (qscope == error_mark_node || identifier == error_mark_node)
17773 else if (!identifier_p (identifier)
17774 && TREE_CODE (identifier) != BIT_NOT_EXPR)
17775 /* [namespace.udecl]
17777 A using declaration shall not name a template-id. */
17778 error_at (token->location,
17779 "a template-id may not appear in a using-declaration");
17780 else
17782 if (at_class_scope_p ())
17784 /* Create the USING_DECL. */
17785 decl = do_class_using_decl (parser->scope, identifier);
17787 if (decl && typename_p)
17788 USING_DECL_TYPENAME_P (decl) = 1;
17790 if (check_for_bare_parameter_packs (decl))
17792 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17793 return false;
17795 else
17796 /* Add it to the list of members in this class. */
17797 finish_member_declaration (decl);
17799 else
17801 decl = cp_parser_lookup_name_simple (parser,
17802 identifier,
17803 token->location);
17804 if (decl == error_mark_node)
17805 cp_parser_name_lookup_error (parser, identifier,
17806 decl, NLE_NULL,
17807 token->location);
17808 else if (check_for_bare_parameter_packs (decl))
17810 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17811 return false;
17813 else if (!at_namespace_scope_p ())
17814 do_local_using_decl (decl, qscope, identifier);
17815 else
17816 do_toplevel_using_decl (decl, qscope, identifier);
17820 /* Look for the final `;'. */
17821 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17823 if (access_declaration_p && errorcount == oldcount)
17824 warning_at (diag_token->location, OPT_Wdeprecated,
17825 "access declarations are deprecated "
17826 "in favour of using-declarations; "
17827 "suggestion: add the %<using%> keyword");
17829 return true;
17832 /* Parse an alias-declaration.
17834 alias-declaration:
17835 using identifier attribute-specifier-seq [opt] = type-id */
17837 static tree
17838 cp_parser_alias_declaration (cp_parser* parser)
17840 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
17841 location_t id_location;
17842 cp_declarator *declarator;
17843 cp_decl_specifier_seq decl_specs;
17844 bool member_p;
17845 const char *saved_message = NULL;
17847 /* Look for the `using' keyword. */
17848 cp_token *using_token
17849 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
17850 if (using_token == NULL)
17851 return error_mark_node;
17853 id_location = cp_lexer_peek_token (parser->lexer)->location;
17854 id = cp_parser_identifier (parser);
17855 if (id == error_mark_node)
17856 return error_mark_node;
17858 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
17859 attributes = cp_parser_attributes_opt (parser);
17860 if (attributes == error_mark_node)
17861 return error_mark_node;
17863 cp_parser_require (parser, CPP_EQ, RT_EQ);
17865 if (cp_parser_error_occurred (parser))
17866 return error_mark_node;
17868 cp_parser_commit_to_tentative_parse (parser);
17870 /* Now we are going to parse the type-id of the declaration. */
17873 [dcl.type]/3 says:
17875 "A type-specifier-seq shall not define a class or enumeration
17876 unless it appears in the type-id of an alias-declaration (7.1.3) that
17877 is not the declaration of a template-declaration."
17879 In other words, if we currently are in an alias template, the
17880 type-id should not define a type.
17882 So let's set parser->type_definition_forbidden_message in that
17883 case; cp_parser_check_type_definition (called by
17884 cp_parser_class_specifier) will then emit an error if a type is
17885 defined in the type-id. */
17886 if (parser->num_template_parameter_lists)
17888 saved_message = parser->type_definition_forbidden_message;
17889 parser->type_definition_forbidden_message =
17890 G_("types may not be defined in alias template declarations");
17893 type = cp_parser_type_id (parser);
17895 /* Restore the error message if need be. */
17896 if (parser->num_template_parameter_lists)
17897 parser->type_definition_forbidden_message = saved_message;
17899 if (type == error_mark_node
17900 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
17902 cp_parser_skip_to_end_of_block_or_statement (parser);
17903 return error_mark_node;
17906 /* A typedef-name can also be introduced by an alias-declaration. The
17907 identifier following the using keyword becomes a typedef-name. It has
17908 the same semantics as if it were introduced by the typedef
17909 specifier. In particular, it does not define a new type and it shall
17910 not appear in the type-id. */
17912 clear_decl_specs (&decl_specs);
17913 decl_specs.type = type;
17914 if (attributes != NULL_TREE)
17916 decl_specs.attributes = attributes;
17917 set_and_check_decl_spec_loc (&decl_specs,
17918 ds_attribute,
17919 attrs_token);
17921 set_and_check_decl_spec_loc (&decl_specs,
17922 ds_typedef,
17923 using_token);
17924 set_and_check_decl_spec_loc (&decl_specs,
17925 ds_alias,
17926 using_token);
17928 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
17929 declarator->id_loc = id_location;
17931 member_p = at_class_scope_p ();
17932 if (member_p)
17933 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
17934 NULL_TREE, attributes);
17935 else
17936 decl = start_decl (declarator, &decl_specs, 0,
17937 attributes, NULL_TREE, &pushed_scope);
17938 if (decl == error_mark_node)
17939 return decl;
17941 // Attach constraints to the alias declaration.
17942 if (flag_concepts && current_template_parms)
17944 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
17945 tree constr = build_constraints (reqs, NULL_TREE);
17946 set_constraints (decl, constr);
17949 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
17951 if (pushed_scope)
17952 pop_scope (pushed_scope);
17954 /* If decl is a template, return its TEMPLATE_DECL so that it gets
17955 added into the symbol table; otherwise, return the TYPE_DECL. */
17956 if (DECL_LANG_SPECIFIC (decl)
17957 && DECL_TEMPLATE_INFO (decl)
17958 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
17960 decl = DECL_TI_TEMPLATE (decl);
17961 if (member_p)
17962 check_member_template (decl);
17965 return decl;
17968 /* Parse a using-directive.
17970 using-directive:
17971 using namespace :: [opt] nested-name-specifier [opt]
17972 namespace-name ; */
17974 static void
17975 cp_parser_using_directive (cp_parser* parser)
17977 tree namespace_decl;
17978 tree attribs;
17980 /* Look for the `using' keyword. */
17981 cp_parser_require_keyword (parser, RID_USING, RT_USING);
17982 /* And the `namespace' keyword. */
17983 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17984 /* Look for the optional `::' operator. */
17985 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17986 /* And the optional nested-name-specifier. */
17987 cp_parser_nested_name_specifier_opt (parser,
17988 /*typename_keyword_p=*/false,
17989 /*check_dependency_p=*/true,
17990 /*type_p=*/false,
17991 /*is_declaration=*/true);
17992 /* Get the namespace being used. */
17993 namespace_decl = cp_parser_namespace_name (parser);
17994 /* And any specified attributes. */
17995 attribs = cp_parser_attributes_opt (parser);
17996 /* Update the symbol table. */
17997 parse_using_directive (namespace_decl, attribs);
17998 /* Look for the final `;'. */
17999 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18002 /* Parse an asm-definition.
18004 asm-definition:
18005 asm ( string-literal ) ;
18007 GNU Extension:
18009 asm-definition:
18010 asm volatile [opt] ( string-literal ) ;
18011 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18012 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18013 : asm-operand-list [opt] ) ;
18014 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18015 : asm-operand-list [opt]
18016 : asm-clobber-list [opt] ) ;
18017 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18018 : asm-clobber-list [opt]
18019 : asm-goto-list ) ; */
18021 static void
18022 cp_parser_asm_definition (cp_parser* parser)
18024 tree string;
18025 tree outputs = NULL_TREE;
18026 tree inputs = NULL_TREE;
18027 tree clobbers = NULL_TREE;
18028 tree labels = NULL_TREE;
18029 tree asm_stmt;
18030 bool volatile_p = false;
18031 bool extended_p = false;
18032 bool invalid_inputs_p = false;
18033 bool invalid_outputs_p = false;
18034 bool goto_p = false;
18035 required_token missing = RT_NONE;
18037 /* Look for the `asm' keyword. */
18038 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
18040 if (parser->in_function_body
18041 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
18043 error ("%<asm%> in %<constexpr%> function");
18044 cp_function_chain->invalid_constexpr = true;
18047 /* See if the next token is `volatile'. */
18048 if (cp_parser_allow_gnu_extensions_p (parser)
18049 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
18051 /* Remember that we saw the `volatile' keyword. */
18052 volatile_p = true;
18053 /* Consume the token. */
18054 cp_lexer_consume_token (parser->lexer);
18056 if (cp_parser_allow_gnu_extensions_p (parser)
18057 && parser->in_function_body
18058 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
18060 /* Remember that we saw the `goto' keyword. */
18061 goto_p = true;
18062 /* Consume the token. */
18063 cp_lexer_consume_token (parser->lexer);
18065 /* Look for the opening `('. */
18066 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18067 return;
18068 /* Look for the string. */
18069 string = cp_parser_string_literal (parser, false, false);
18070 if (string == error_mark_node)
18072 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18073 /*consume_paren=*/true);
18074 return;
18077 /* If we're allowing GNU extensions, check for the extended assembly
18078 syntax. Unfortunately, the `:' tokens need not be separated by
18079 a space in C, and so, for compatibility, we tolerate that here
18080 too. Doing that means that we have to treat the `::' operator as
18081 two `:' tokens. */
18082 if (cp_parser_allow_gnu_extensions_p (parser)
18083 && parser->in_function_body
18084 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
18085 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
18087 bool inputs_p = false;
18088 bool clobbers_p = false;
18089 bool labels_p = false;
18091 /* The extended syntax was used. */
18092 extended_p = true;
18094 /* Look for outputs. */
18095 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18097 /* Consume the `:'. */
18098 cp_lexer_consume_token (parser->lexer);
18099 /* Parse the output-operands. */
18100 if (cp_lexer_next_token_is_not (parser->lexer,
18101 CPP_COLON)
18102 && cp_lexer_next_token_is_not (parser->lexer,
18103 CPP_SCOPE)
18104 && cp_lexer_next_token_is_not (parser->lexer,
18105 CPP_CLOSE_PAREN)
18106 && !goto_p)
18108 outputs = cp_parser_asm_operand_list (parser);
18109 if (outputs == error_mark_node)
18110 invalid_outputs_p = true;
18113 /* If the next token is `::', there are no outputs, and the
18114 next token is the beginning of the inputs. */
18115 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18116 /* The inputs are coming next. */
18117 inputs_p = true;
18119 /* Look for inputs. */
18120 if (inputs_p
18121 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18123 /* Consume the `:' or `::'. */
18124 cp_lexer_consume_token (parser->lexer);
18125 /* Parse the output-operands. */
18126 if (cp_lexer_next_token_is_not (parser->lexer,
18127 CPP_COLON)
18128 && cp_lexer_next_token_is_not (parser->lexer,
18129 CPP_SCOPE)
18130 && cp_lexer_next_token_is_not (parser->lexer,
18131 CPP_CLOSE_PAREN))
18133 inputs = cp_parser_asm_operand_list (parser);
18134 if (inputs == error_mark_node)
18135 invalid_inputs_p = true;
18138 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18139 /* The clobbers are coming next. */
18140 clobbers_p = true;
18142 /* Look for clobbers. */
18143 if (clobbers_p
18144 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18146 clobbers_p = true;
18147 /* Consume the `:' or `::'. */
18148 cp_lexer_consume_token (parser->lexer);
18149 /* Parse the clobbers. */
18150 if (cp_lexer_next_token_is_not (parser->lexer,
18151 CPP_COLON)
18152 && cp_lexer_next_token_is_not (parser->lexer,
18153 CPP_CLOSE_PAREN))
18154 clobbers = cp_parser_asm_clobber_list (parser);
18156 else if (goto_p
18157 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18158 /* The labels are coming next. */
18159 labels_p = true;
18161 /* Look for labels. */
18162 if (labels_p
18163 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
18165 labels_p = true;
18166 /* Consume the `:' or `::'. */
18167 cp_lexer_consume_token (parser->lexer);
18168 /* Parse the labels. */
18169 labels = cp_parser_asm_label_list (parser);
18172 if (goto_p && !labels_p)
18173 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
18175 else if (goto_p)
18176 missing = RT_COLON_SCOPE;
18178 /* Look for the closing `)'. */
18179 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
18180 missing ? missing : RT_CLOSE_PAREN))
18181 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18182 /*consume_paren=*/true);
18183 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18185 if (!invalid_inputs_p && !invalid_outputs_p)
18187 /* Create the ASM_EXPR. */
18188 if (parser->in_function_body)
18190 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
18191 inputs, clobbers, labels);
18192 /* If the extended syntax was not used, mark the ASM_EXPR. */
18193 if (!extended_p)
18195 tree temp = asm_stmt;
18196 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
18197 temp = TREE_OPERAND (temp, 0);
18199 ASM_INPUT_P (temp) = 1;
18202 else
18203 symtab->finalize_toplevel_asm (string);
18207 /* Declarators [gram.dcl.decl] */
18209 /* Parse an init-declarator.
18211 init-declarator:
18212 declarator initializer [opt]
18214 GNU Extension:
18216 init-declarator:
18217 declarator asm-specification [opt] attributes [opt] initializer [opt]
18219 function-definition:
18220 decl-specifier-seq [opt] declarator ctor-initializer [opt]
18221 function-body
18222 decl-specifier-seq [opt] declarator function-try-block
18224 GNU Extension:
18226 function-definition:
18227 __extension__ function-definition
18229 TM Extension:
18231 function-definition:
18232 decl-specifier-seq [opt] declarator function-transaction-block
18234 The DECL_SPECIFIERS apply to this declarator. Returns a
18235 representation of the entity declared. If MEMBER_P is TRUE, then
18236 this declarator appears in a class scope. The new DECL created by
18237 this declarator is returned.
18239 The CHECKS are access checks that should be performed once we know
18240 what entity is being declared (and, therefore, what classes have
18241 befriended it).
18243 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
18244 for a function-definition here as well. If the declarator is a
18245 declarator for a function-definition, *FUNCTION_DEFINITION_P will
18246 be TRUE upon return. By that point, the function-definition will
18247 have been completely parsed.
18249 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
18250 is FALSE.
18252 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
18253 parsed declaration if it is an uninitialized single declarator not followed
18254 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
18255 if present, will not be consumed. If returned, this declarator will be
18256 created with SD_INITIALIZED but will not call cp_finish_decl.
18258 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
18259 and there is an initializer, the pointed location_t is set to the
18260 location of the '=' or `(', or '{' in C++11 token introducing the
18261 initializer. */
18263 static tree
18264 cp_parser_init_declarator (cp_parser* parser,
18265 cp_decl_specifier_seq *decl_specifiers,
18266 vec<deferred_access_check, va_gc> *checks,
18267 bool function_definition_allowed_p,
18268 bool member_p,
18269 int declares_class_or_enum,
18270 bool* function_definition_p,
18271 tree* maybe_range_for_decl,
18272 location_t* init_loc)
18274 cp_token *token = NULL, *asm_spec_start_token = NULL,
18275 *attributes_start_token = NULL;
18276 cp_declarator *declarator;
18277 tree prefix_attributes;
18278 tree attributes = NULL;
18279 tree asm_specification;
18280 tree initializer;
18281 tree decl = NULL_TREE;
18282 tree scope;
18283 int is_initialized;
18284 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
18285 initialized with "= ..", CPP_OPEN_PAREN if initialized with
18286 "(...)". */
18287 enum cpp_ttype initialization_kind;
18288 bool is_direct_init = false;
18289 bool is_non_constant_init;
18290 int ctor_dtor_or_conv_p;
18291 bool friend_p = cp_parser_friend_p (decl_specifiers);
18292 tree pushed_scope = NULL_TREE;
18293 bool range_for_decl_p = false;
18294 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18295 location_t tmp_init_loc = UNKNOWN_LOCATION;
18297 /* Gather the attributes that were provided with the
18298 decl-specifiers. */
18299 prefix_attributes = decl_specifiers->attributes;
18301 /* Assume that this is not the declarator for a function
18302 definition. */
18303 if (function_definition_p)
18304 *function_definition_p = false;
18306 /* Default arguments are only permitted for function parameters. */
18307 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
18308 parser->default_arg_ok_p = false;
18310 /* Defer access checks while parsing the declarator; we cannot know
18311 what names are accessible until we know what is being
18312 declared. */
18313 resume_deferring_access_checks ();
18315 /* Parse the declarator. */
18316 token = cp_lexer_peek_token (parser->lexer);
18317 declarator
18318 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18319 &ctor_dtor_or_conv_p,
18320 /*parenthesized_p=*/NULL,
18321 member_p, friend_p);
18322 /* Gather up the deferred checks. */
18323 stop_deferring_access_checks ();
18325 parser->default_arg_ok_p = saved_default_arg_ok_p;
18327 /* If the DECLARATOR was erroneous, there's no need to go
18328 further. */
18329 if (declarator == cp_error_declarator)
18330 return error_mark_node;
18332 /* Check that the number of template-parameter-lists is OK. */
18333 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
18334 token->location))
18335 return error_mark_node;
18337 if (declares_class_or_enum & 2)
18338 cp_parser_check_for_definition_in_return_type (declarator,
18339 decl_specifiers->type,
18340 decl_specifiers->locations[ds_type_spec]);
18342 /* Figure out what scope the entity declared by the DECLARATOR is
18343 located in. `grokdeclarator' sometimes changes the scope, so
18344 we compute it now. */
18345 scope = get_scope_of_declarator (declarator);
18347 /* Perform any lookups in the declared type which were thought to be
18348 dependent, but are not in the scope of the declarator. */
18349 decl_specifiers->type
18350 = maybe_update_decl_type (decl_specifiers->type, scope);
18352 /* If we're allowing GNU extensions, look for an
18353 asm-specification. */
18354 if (cp_parser_allow_gnu_extensions_p (parser))
18356 /* Look for an asm-specification. */
18357 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
18358 asm_specification = cp_parser_asm_specification_opt (parser);
18360 else
18361 asm_specification = NULL_TREE;
18363 /* Look for attributes. */
18364 attributes_start_token = cp_lexer_peek_token (parser->lexer);
18365 attributes = cp_parser_attributes_opt (parser);
18367 /* Peek at the next token. */
18368 token = cp_lexer_peek_token (parser->lexer);
18370 bool bogus_implicit_tmpl = false;
18372 if (function_declarator_p (declarator))
18374 /* Check to see if the token indicates the start of a
18375 function-definition. */
18376 if (cp_parser_token_starts_function_definition_p (token))
18378 if (!function_definition_allowed_p)
18380 /* If a function-definition should not appear here, issue an
18381 error message. */
18382 cp_parser_error (parser,
18383 "a function-definition is not allowed here");
18384 return error_mark_node;
18387 location_t func_brace_location
18388 = cp_lexer_peek_token (parser->lexer)->location;
18390 /* Neither attributes nor an asm-specification are allowed
18391 on a function-definition. */
18392 if (asm_specification)
18393 error_at (asm_spec_start_token->location,
18394 "an asm-specification is not allowed "
18395 "on a function-definition");
18396 if (attributes)
18397 error_at (attributes_start_token->location,
18398 "attributes are not allowed "
18399 "on a function-definition");
18400 /* This is a function-definition. */
18401 *function_definition_p = true;
18403 /* Parse the function definition. */
18404 if (member_p)
18405 decl = cp_parser_save_member_function_body (parser,
18406 decl_specifiers,
18407 declarator,
18408 prefix_attributes);
18409 else
18410 decl =
18411 (cp_parser_function_definition_from_specifiers_and_declarator
18412 (parser, decl_specifiers, prefix_attributes, declarator));
18414 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
18416 /* This is where the prologue starts... */
18417 DECL_STRUCT_FUNCTION (decl)->function_start_locus
18418 = func_brace_location;
18421 return decl;
18424 else if (parser->fully_implicit_function_template_p)
18426 /* A non-template declaration involving a function parameter list
18427 containing an implicit template parameter will be made into a
18428 template. If the resulting declaration is not going to be an
18429 actual function then finish the template scope here to prevent it.
18430 An error message will be issued once we have a decl to talk about.
18432 FIXME probably we should do type deduction rather than create an
18433 implicit template, but the standard currently doesn't allow it. */
18434 bogus_implicit_tmpl = true;
18435 finish_fully_implicit_template (parser, NULL_TREE);
18438 /* [dcl.dcl]
18440 Only in function declarations for constructors, destructors, and
18441 type conversions can the decl-specifier-seq be omitted.
18443 We explicitly postpone this check past the point where we handle
18444 function-definitions because we tolerate function-definitions
18445 that are missing their return types in some modes. */
18446 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
18448 cp_parser_error (parser,
18449 "expected constructor, destructor, or type conversion");
18450 return error_mark_node;
18453 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
18454 if (token->type == CPP_EQ
18455 || token->type == CPP_OPEN_PAREN
18456 || token->type == CPP_OPEN_BRACE)
18458 is_initialized = SD_INITIALIZED;
18459 initialization_kind = token->type;
18460 if (maybe_range_for_decl)
18461 *maybe_range_for_decl = error_mark_node;
18462 tmp_init_loc = token->location;
18463 if (init_loc && *init_loc == UNKNOWN_LOCATION)
18464 *init_loc = tmp_init_loc;
18466 if (token->type == CPP_EQ
18467 && function_declarator_p (declarator))
18469 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
18470 if (t2->keyword == RID_DEFAULT)
18471 is_initialized = SD_DEFAULTED;
18472 else if (t2->keyword == RID_DELETE)
18473 is_initialized = SD_DELETED;
18476 else
18478 /* If the init-declarator isn't initialized and isn't followed by a
18479 `,' or `;', it's not a valid init-declarator. */
18480 if (token->type != CPP_COMMA
18481 && token->type != CPP_SEMICOLON)
18483 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
18484 range_for_decl_p = true;
18485 else
18487 if (!maybe_range_for_decl)
18488 cp_parser_error (parser, "expected initializer");
18489 return error_mark_node;
18492 is_initialized = SD_UNINITIALIZED;
18493 initialization_kind = CPP_EOF;
18496 /* Because start_decl has side-effects, we should only call it if we
18497 know we're going ahead. By this point, we know that we cannot
18498 possibly be looking at any other construct. */
18499 cp_parser_commit_to_tentative_parse (parser);
18501 /* Enter the newly declared entry in the symbol table. If we're
18502 processing a declaration in a class-specifier, we wait until
18503 after processing the initializer. */
18504 if (!member_p)
18506 if (parser->in_unbraced_linkage_specification_p)
18507 decl_specifiers->storage_class = sc_extern;
18508 decl = start_decl (declarator, decl_specifiers,
18509 range_for_decl_p? SD_INITIALIZED : is_initialized,
18510 attributes, prefix_attributes, &pushed_scope);
18511 cp_finalize_omp_declare_simd (parser, decl);
18512 cp_finalize_oacc_routine (parser, decl, false);
18513 /* Adjust location of decl if declarator->id_loc is more appropriate:
18514 set, and decl wasn't merged with another decl, in which case its
18515 location would be different from input_location, and more accurate. */
18516 if (DECL_P (decl)
18517 && declarator->id_loc != UNKNOWN_LOCATION
18518 && DECL_SOURCE_LOCATION (decl) == input_location)
18519 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
18521 else if (scope)
18522 /* Enter the SCOPE. That way unqualified names appearing in the
18523 initializer will be looked up in SCOPE. */
18524 pushed_scope = push_scope (scope);
18526 /* Perform deferred access control checks, now that we know in which
18527 SCOPE the declared entity resides. */
18528 if (!member_p && decl)
18530 tree saved_current_function_decl = NULL_TREE;
18532 /* If the entity being declared is a function, pretend that we
18533 are in its scope. If it is a `friend', it may have access to
18534 things that would not otherwise be accessible. */
18535 if (TREE_CODE (decl) == FUNCTION_DECL)
18537 saved_current_function_decl = current_function_decl;
18538 current_function_decl = decl;
18541 /* Perform access checks for template parameters. */
18542 cp_parser_perform_template_parameter_access_checks (checks);
18544 /* Perform the access control checks for the declarator and the
18545 decl-specifiers. */
18546 perform_deferred_access_checks (tf_warning_or_error);
18548 /* Restore the saved value. */
18549 if (TREE_CODE (decl) == FUNCTION_DECL)
18550 current_function_decl = saved_current_function_decl;
18553 /* Parse the initializer. */
18554 initializer = NULL_TREE;
18555 is_direct_init = false;
18556 is_non_constant_init = true;
18557 if (is_initialized)
18559 if (function_declarator_p (declarator))
18561 if (initialization_kind == CPP_EQ)
18562 initializer = cp_parser_pure_specifier (parser);
18563 else
18565 /* If the declaration was erroneous, we don't really
18566 know what the user intended, so just silently
18567 consume the initializer. */
18568 if (decl != error_mark_node)
18569 error_at (tmp_init_loc, "initializer provided for function");
18570 cp_parser_skip_to_closing_parenthesis (parser,
18571 /*recovering=*/true,
18572 /*or_comma=*/false,
18573 /*consume_paren=*/true);
18576 else
18578 /* We want to record the extra mangling scope for in-class
18579 initializers of class members and initializers of static data
18580 member templates. The former involves deferring
18581 parsing of the initializer until end of class as with default
18582 arguments. So right here we only handle the latter. */
18583 if (!member_p && processing_template_decl)
18584 start_lambda_scope (decl);
18585 initializer = cp_parser_initializer (parser,
18586 &is_direct_init,
18587 &is_non_constant_init);
18588 if (!member_p && processing_template_decl)
18589 finish_lambda_scope ();
18590 if (initializer == error_mark_node)
18591 cp_parser_skip_to_end_of_statement (parser);
18595 /* The old parser allows attributes to appear after a parenthesized
18596 initializer. Mark Mitchell proposed removing this functionality
18597 on the GCC mailing lists on 2002-08-13. This parser accepts the
18598 attributes -- but ignores them. */
18599 if (cp_parser_allow_gnu_extensions_p (parser)
18600 && initialization_kind == CPP_OPEN_PAREN)
18601 if (cp_parser_attributes_opt (parser))
18602 warning (OPT_Wattributes,
18603 "attributes after parenthesized initializer ignored");
18605 /* And now complain about a non-function implicit template. */
18606 if (bogus_implicit_tmpl && decl != error_mark_node)
18607 error_at (DECL_SOURCE_LOCATION (decl),
18608 "non-function %qD declared as implicit template", decl);
18610 /* For an in-class declaration, use `grokfield' to create the
18611 declaration. */
18612 if (member_p)
18614 if (pushed_scope)
18616 pop_scope (pushed_scope);
18617 pushed_scope = NULL_TREE;
18619 decl = grokfield (declarator, decl_specifiers,
18620 initializer, !is_non_constant_init,
18621 /*asmspec=*/NULL_TREE,
18622 chainon (attributes, prefix_attributes));
18623 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
18624 cp_parser_save_default_args (parser, decl);
18625 cp_finalize_omp_declare_simd (parser, decl);
18626 cp_finalize_oacc_routine (parser, decl, false);
18629 /* Finish processing the declaration. But, skip member
18630 declarations. */
18631 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
18633 cp_finish_decl (decl,
18634 initializer, !is_non_constant_init,
18635 asm_specification,
18636 /* If the initializer is in parentheses, then this is
18637 a direct-initialization, which means that an
18638 `explicit' constructor is OK. Otherwise, an
18639 `explicit' constructor cannot be used. */
18640 ((is_direct_init || !is_initialized)
18641 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
18643 else if ((cxx_dialect != cxx98) && friend_p
18644 && decl && TREE_CODE (decl) == FUNCTION_DECL)
18645 /* Core issue #226 (C++0x only): A default template-argument
18646 shall not be specified in a friend class template
18647 declaration. */
18648 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
18649 /*is_partial=*/false, /*is_friend_decl=*/1);
18651 if (!friend_p && pushed_scope)
18652 pop_scope (pushed_scope);
18654 if (function_declarator_p (declarator)
18655 && parser->fully_implicit_function_template_p)
18657 if (member_p)
18658 decl = finish_fully_implicit_template (parser, decl);
18659 else
18660 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
18663 return decl;
18666 /* Parse a declarator.
18668 declarator:
18669 direct-declarator
18670 ptr-operator declarator
18672 abstract-declarator:
18673 ptr-operator abstract-declarator [opt]
18674 direct-abstract-declarator
18676 GNU Extensions:
18678 declarator:
18679 attributes [opt] direct-declarator
18680 attributes [opt] ptr-operator declarator
18682 abstract-declarator:
18683 attributes [opt] ptr-operator abstract-declarator [opt]
18684 attributes [opt] direct-abstract-declarator
18686 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
18687 detect constructor, destructor or conversion operators. It is set
18688 to -1 if the declarator is a name, and +1 if it is a
18689 function. Otherwise it is set to zero. Usually you just want to
18690 test for >0, but internally the negative value is used.
18692 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
18693 a decl-specifier-seq unless it declares a constructor, destructor,
18694 or conversion. It might seem that we could check this condition in
18695 semantic analysis, rather than parsing, but that makes it difficult
18696 to handle something like `f()'. We want to notice that there are
18697 no decl-specifiers, and therefore realize that this is an
18698 expression, not a declaration.)
18700 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
18701 the declarator is a direct-declarator of the form "(...)".
18703 MEMBER_P is true iff this declarator is a member-declarator.
18705 FRIEND_P is true iff this declarator is a friend. */
18707 static cp_declarator *
18708 cp_parser_declarator (cp_parser* parser,
18709 cp_parser_declarator_kind dcl_kind,
18710 int* ctor_dtor_or_conv_p,
18711 bool* parenthesized_p,
18712 bool member_p, bool friend_p)
18714 cp_declarator *declarator;
18715 enum tree_code code;
18716 cp_cv_quals cv_quals;
18717 tree class_type;
18718 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
18720 /* Assume this is not a constructor, destructor, or type-conversion
18721 operator. */
18722 if (ctor_dtor_or_conv_p)
18723 *ctor_dtor_or_conv_p = 0;
18725 if (cp_parser_allow_gnu_extensions_p (parser))
18726 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
18728 /* Check for the ptr-operator production. */
18729 cp_parser_parse_tentatively (parser);
18730 /* Parse the ptr-operator. */
18731 code = cp_parser_ptr_operator (parser,
18732 &class_type,
18733 &cv_quals,
18734 &std_attributes);
18736 /* If that worked, then we have a ptr-operator. */
18737 if (cp_parser_parse_definitely (parser))
18739 /* If a ptr-operator was found, then this declarator was not
18740 parenthesized. */
18741 if (parenthesized_p)
18742 *parenthesized_p = true;
18743 /* The dependent declarator is optional if we are parsing an
18744 abstract-declarator. */
18745 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18746 cp_parser_parse_tentatively (parser);
18748 /* Parse the dependent declarator. */
18749 declarator = cp_parser_declarator (parser, dcl_kind,
18750 /*ctor_dtor_or_conv_p=*/NULL,
18751 /*parenthesized_p=*/NULL,
18752 /*member_p=*/false,
18753 friend_p);
18755 /* If we are parsing an abstract-declarator, we must handle the
18756 case where the dependent declarator is absent. */
18757 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
18758 && !cp_parser_parse_definitely (parser))
18759 declarator = NULL;
18761 declarator = cp_parser_make_indirect_declarator
18762 (code, class_type, cv_quals, declarator, std_attributes);
18764 /* Everything else is a direct-declarator. */
18765 else
18767 if (parenthesized_p)
18768 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
18769 CPP_OPEN_PAREN);
18770 declarator = cp_parser_direct_declarator (parser, dcl_kind,
18771 ctor_dtor_or_conv_p,
18772 member_p, friend_p);
18775 if (gnu_attributes && declarator && declarator != cp_error_declarator)
18776 declarator->attributes = gnu_attributes;
18777 return declarator;
18780 /* Parse a direct-declarator or direct-abstract-declarator.
18782 direct-declarator:
18783 declarator-id
18784 direct-declarator ( parameter-declaration-clause )
18785 cv-qualifier-seq [opt]
18786 ref-qualifier [opt]
18787 exception-specification [opt]
18788 direct-declarator [ constant-expression [opt] ]
18789 ( declarator )
18791 direct-abstract-declarator:
18792 direct-abstract-declarator [opt]
18793 ( parameter-declaration-clause )
18794 cv-qualifier-seq [opt]
18795 ref-qualifier [opt]
18796 exception-specification [opt]
18797 direct-abstract-declarator [opt] [ constant-expression [opt] ]
18798 ( abstract-declarator )
18800 Returns a representation of the declarator. DCL_KIND is
18801 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
18802 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
18803 we are parsing a direct-declarator. It is
18804 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
18805 of ambiguity we prefer an abstract declarator, as per
18806 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
18807 as for cp_parser_declarator. */
18809 static cp_declarator *
18810 cp_parser_direct_declarator (cp_parser* parser,
18811 cp_parser_declarator_kind dcl_kind,
18812 int* ctor_dtor_or_conv_p,
18813 bool member_p, bool friend_p)
18815 cp_token *token;
18816 cp_declarator *declarator = NULL;
18817 tree scope = NULL_TREE;
18818 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18819 bool saved_in_declarator_p = parser->in_declarator_p;
18820 bool first = true;
18821 tree pushed_scope = NULL_TREE;
18823 while (true)
18825 /* Peek at the next token. */
18826 token = cp_lexer_peek_token (parser->lexer);
18827 if (token->type == CPP_OPEN_PAREN)
18829 /* This is either a parameter-declaration-clause, or a
18830 parenthesized declarator. When we know we are parsing a
18831 named declarator, it must be a parenthesized declarator
18832 if FIRST is true. For instance, `(int)' is a
18833 parameter-declaration-clause, with an omitted
18834 direct-abstract-declarator. But `((*))', is a
18835 parenthesized abstract declarator. Finally, when T is a
18836 template parameter `(T)' is a
18837 parameter-declaration-clause, and not a parenthesized
18838 named declarator.
18840 We first try and parse a parameter-declaration-clause,
18841 and then try a nested declarator (if FIRST is true).
18843 It is not an error for it not to be a
18844 parameter-declaration-clause, even when FIRST is
18845 false. Consider,
18847 int i (int);
18848 int i (3);
18850 The first is the declaration of a function while the
18851 second is the definition of a variable, including its
18852 initializer.
18854 Having seen only the parenthesis, we cannot know which of
18855 these two alternatives should be selected. Even more
18856 complex are examples like:
18858 int i (int (a));
18859 int i (int (3));
18861 The former is a function-declaration; the latter is a
18862 variable initialization.
18864 Thus again, we try a parameter-declaration-clause, and if
18865 that fails, we back out and return. */
18867 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18869 tree params;
18870 bool is_declarator = false;
18872 /* In a member-declarator, the only valid interpretation
18873 of a parenthesis is the start of a
18874 parameter-declaration-clause. (It is invalid to
18875 initialize a static data member with a parenthesized
18876 initializer; only the "=" form of initialization is
18877 permitted.) */
18878 if (!member_p)
18879 cp_parser_parse_tentatively (parser);
18881 /* Consume the `('. */
18882 cp_lexer_consume_token (parser->lexer);
18883 if (first)
18885 /* If this is going to be an abstract declarator, we're
18886 in a declarator and we can't have default args. */
18887 parser->default_arg_ok_p = false;
18888 parser->in_declarator_p = true;
18891 begin_scope (sk_function_parms, NULL_TREE);
18893 /* Parse the parameter-declaration-clause. */
18894 params = cp_parser_parameter_declaration_clause (parser);
18896 /* Consume the `)'. */
18897 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18899 /* If all went well, parse the cv-qualifier-seq,
18900 ref-qualifier and the exception-specification. */
18901 if (member_p || cp_parser_parse_definitely (parser))
18903 cp_cv_quals cv_quals;
18904 cp_virt_specifiers virt_specifiers;
18905 cp_ref_qualifier ref_qual;
18906 tree exception_specification;
18907 tree late_return;
18908 tree attrs;
18909 bool memfn = (member_p || (pushed_scope
18910 && CLASS_TYPE_P (pushed_scope)));
18912 is_declarator = true;
18914 if (ctor_dtor_or_conv_p)
18915 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
18916 first = false;
18918 /* Parse the cv-qualifier-seq. */
18919 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18920 /* Parse the ref-qualifier. */
18921 ref_qual = cp_parser_ref_qualifier_opt (parser);
18922 /* Parse the tx-qualifier. */
18923 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
18924 /* And the exception-specification. */
18925 exception_specification
18926 = cp_parser_exception_specification_opt (parser);
18928 attrs = cp_parser_std_attribute_spec_seq (parser);
18930 /* In here, we handle cases where attribute is used after
18931 the function declaration. For example:
18932 void func (int x) __attribute__((vector(..))); */
18933 tree gnu_attrs = NULL_TREE;
18934 if (flag_cilkplus
18935 && cp_next_tokens_can_be_gnu_attribute_p (parser))
18937 cp_parser_parse_tentatively (parser);
18938 tree attr = cp_parser_gnu_attributes_opt (parser);
18939 if (cp_lexer_next_token_is_not (parser->lexer,
18940 CPP_SEMICOLON)
18941 && cp_lexer_next_token_is_not (parser->lexer,
18942 CPP_OPEN_BRACE))
18943 cp_parser_abort_tentative_parse (parser);
18944 else if (!cp_parser_parse_definitely (parser))
18946 else
18947 gnu_attrs = attr;
18949 tree requires_clause = NULL_TREE;
18950 late_return = (cp_parser_late_return_type_opt
18951 (parser, declarator, requires_clause,
18952 memfn ? cv_quals : -1));
18954 /* Parse the virt-specifier-seq. */
18955 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18957 /* Create the function-declarator. */
18958 declarator = make_call_declarator (declarator,
18959 params,
18960 cv_quals,
18961 virt_specifiers,
18962 ref_qual,
18963 tx_qual,
18964 exception_specification,
18965 late_return,
18966 requires_clause);
18967 declarator->std_attributes = attrs;
18968 declarator->attributes = gnu_attrs;
18969 /* Any subsequent parameter lists are to do with
18970 return type, so are not those of the declared
18971 function. */
18972 parser->default_arg_ok_p = false;
18975 /* Remove the function parms from scope. */
18976 pop_bindings_and_leave_scope ();
18978 if (is_declarator)
18979 /* Repeat the main loop. */
18980 continue;
18983 /* If this is the first, we can try a parenthesized
18984 declarator. */
18985 if (first)
18987 bool saved_in_type_id_in_expr_p;
18989 parser->default_arg_ok_p = saved_default_arg_ok_p;
18990 parser->in_declarator_p = saved_in_declarator_p;
18992 /* Consume the `('. */
18993 cp_lexer_consume_token (parser->lexer);
18994 /* Parse the nested declarator. */
18995 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18996 parser->in_type_id_in_expr_p = true;
18997 declarator
18998 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
18999 /*parenthesized_p=*/NULL,
19000 member_p, friend_p);
19001 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19002 first = false;
19003 /* Expect a `)'. */
19004 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
19005 declarator = cp_error_declarator;
19006 if (declarator == cp_error_declarator)
19007 break;
19009 goto handle_declarator;
19011 /* Otherwise, we must be done. */
19012 else
19013 break;
19015 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19016 && token->type == CPP_OPEN_SQUARE
19017 && !cp_next_tokens_can_be_attribute_p (parser))
19019 /* Parse an array-declarator. */
19020 tree bounds, attrs;
19022 if (ctor_dtor_or_conv_p)
19023 *ctor_dtor_or_conv_p = 0;
19025 first = false;
19026 parser->default_arg_ok_p = false;
19027 parser->in_declarator_p = true;
19028 /* Consume the `['. */
19029 cp_lexer_consume_token (parser->lexer);
19030 /* Peek at the next token. */
19031 token = cp_lexer_peek_token (parser->lexer);
19032 /* If the next token is `]', then there is no
19033 constant-expression. */
19034 if (token->type != CPP_CLOSE_SQUARE)
19036 bool non_constant_p;
19037 bounds
19038 = cp_parser_constant_expression (parser,
19039 /*allow_non_constant=*/true,
19040 &non_constant_p);
19041 if (!non_constant_p)
19042 /* OK */;
19043 else if (error_operand_p (bounds))
19044 /* Already gave an error. */;
19045 else if (!parser->in_function_body
19046 || current_binding_level->kind == sk_function_parms)
19048 /* Normally, the array bound must be an integral constant
19049 expression. However, as an extension, we allow VLAs
19050 in function scopes as long as they aren't part of a
19051 parameter declaration. */
19052 cp_parser_error (parser,
19053 "array bound is not an integer constant");
19054 bounds = error_mark_node;
19056 else if (processing_template_decl
19057 && !type_dependent_expression_p (bounds))
19059 /* Remember this wasn't a constant-expression. */
19060 bounds = build_nop (TREE_TYPE (bounds), bounds);
19061 TREE_SIDE_EFFECTS (bounds) = 1;
19064 else
19065 bounds = NULL_TREE;
19066 /* Look for the closing `]'. */
19067 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
19069 declarator = cp_error_declarator;
19070 break;
19073 attrs = cp_parser_std_attribute_spec_seq (parser);
19074 declarator = make_array_declarator (declarator, bounds);
19075 declarator->std_attributes = attrs;
19077 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
19080 tree qualifying_scope;
19081 tree unqualified_name;
19082 tree attrs;
19083 special_function_kind sfk;
19084 bool abstract_ok;
19085 bool pack_expansion_p = false;
19086 cp_token *declarator_id_start_token;
19088 /* Parse a declarator-id */
19089 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
19090 if (abstract_ok)
19092 cp_parser_parse_tentatively (parser);
19094 /* If we see an ellipsis, we should be looking at a
19095 parameter pack. */
19096 if (token->type == CPP_ELLIPSIS)
19098 /* Consume the `...' */
19099 cp_lexer_consume_token (parser->lexer);
19101 pack_expansion_p = true;
19105 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
19106 unqualified_name
19107 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
19108 qualifying_scope = parser->scope;
19109 if (abstract_ok)
19111 bool okay = false;
19113 if (!unqualified_name && pack_expansion_p)
19115 /* Check whether an error occurred. */
19116 okay = !cp_parser_error_occurred (parser);
19118 /* We already consumed the ellipsis to mark a
19119 parameter pack, but we have no way to report it,
19120 so abort the tentative parse. We will be exiting
19121 immediately anyway. */
19122 cp_parser_abort_tentative_parse (parser);
19124 else
19125 okay = cp_parser_parse_definitely (parser);
19127 if (!okay)
19128 unqualified_name = error_mark_node;
19129 else if (unqualified_name
19130 && (qualifying_scope
19131 || (!identifier_p (unqualified_name))))
19133 cp_parser_error (parser, "expected unqualified-id");
19134 unqualified_name = error_mark_node;
19138 if (!unqualified_name)
19139 return NULL;
19140 if (unqualified_name == error_mark_node)
19142 declarator = cp_error_declarator;
19143 pack_expansion_p = false;
19144 declarator->parameter_pack_p = false;
19145 break;
19148 attrs = cp_parser_std_attribute_spec_seq (parser);
19150 if (qualifying_scope && at_namespace_scope_p ()
19151 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
19153 /* In the declaration of a member of a template class
19154 outside of the class itself, the SCOPE will sometimes
19155 be a TYPENAME_TYPE. For example, given:
19157 template <typename T>
19158 int S<T>::R::i = 3;
19160 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
19161 this context, we must resolve S<T>::R to an ordinary
19162 type, rather than a typename type.
19164 The reason we normally avoid resolving TYPENAME_TYPEs
19165 is that a specialization of `S' might render
19166 `S<T>::R' not a type. However, if `S' is
19167 specialized, then this `i' will not be used, so there
19168 is no harm in resolving the types here. */
19169 tree type;
19171 /* Resolve the TYPENAME_TYPE. */
19172 type = resolve_typename_type (qualifying_scope,
19173 /*only_current_p=*/false);
19174 /* If that failed, the declarator is invalid. */
19175 if (TREE_CODE (type) == TYPENAME_TYPE)
19177 if (typedef_variant_p (type))
19178 error_at (declarator_id_start_token->location,
19179 "cannot define member of dependent typedef "
19180 "%qT", type);
19181 else
19182 error_at (declarator_id_start_token->location,
19183 "%<%T::%E%> is not a type",
19184 TYPE_CONTEXT (qualifying_scope),
19185 TYPE_IDENTIFIER (qualifying_scope));
19187 qualifying_scope = type;
19190 sfk = sfk_none;
19192 if (unqualified_name)
19194 tree class_type;
19196 if (qualifying_scope
19197 && CLASS_TYPE_P (qualifying_scope))
19198 class_type = qualifying_scope;
19199 else
19200 class_type = current_class_type;
19202 if (TREE_CODE (unqualified_name) == TYPE_DECL)
19204 tree name_type = TREE_TYPE (unqualified_name);
19205 if (class_type && same_type_p (name_type, class_type))
19207 if (qualifying_scope
19208 && CLASSTYPE_USE_TEMPLATE (name_type))
19210 error_at (declarator_id_start_token->location,
19211 "invalid use of constructor as a template");
19212 inform (declarator_id_start_token->location,
19213 "use %<%T::%D%> instead of %<%T::%D%> to "
19214 "name the constructor in a qualified name",
19215 class_type,
19216 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
19217 class_type, name_type);
19218 declarator = cp_error_declarator;
19219 break;
19221 else
19222 unqualified_name = constructor_name (class_type);
19224 else
19226 /* We do not attempt to print the declarator
19227 here because we do not have enough
19228 information about its original syntactic
19229 form. */
19230 cp_parser_error (parser, "invalid declarator");
19231 declarator = cp_error_declarator;
19232 break;
19236 if (class_type)
19238 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
19239 sfk = sfk_destructor;
19240 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
19241 sfk = sfk_conversion;
19242 else if (/* There's no way to declare a constructor
19243 for an anonymous type, even if the type
19244 got a name for linkage purposes. */
19245 !TYPE_WAS_ANONYMOUS (class_type)
19246 /* Handle correctly (c++/19200):
19248 struct S {
19249 struct T{};
19250 friend void S(T);
19253 and also:
19255 namespace N {
19256 void S();
19259 struct S {
19260 friend void N::S();
19261 }; */
19262 && !(friend_p
19263 && class_type != qualifying_scope)
19264 && constructor_name_p (unqualified_name,
19265 class_type))
19267 unqualified_name = constructor_name (class_type);
19268 sfk = sfk_constructor;
19270 else if (is_overloaded_fn (unqualified_name)
19271 && DECL_CONSTRUCTOR_P (get_first_fn
19272 (unqualified_name)))
19273 sfk = sfk_constructor;
19275 if (ctor_dtor_or_conv_p && sfk != sfk_none)
19276 *ctor_dtor_or_conv_p = -1;
19279 declarator = make_id_declarator (qualifying_scope,
19280 unqualified_name,
19281 sfk);
19282 declarator->std_attributes = attrs;
19283 declarator->id_loc = token->location;
19284 declarator->parameter_pack_p = pack_expansion_p;
19286 if (pack_expansion_p)
19287 maybe_warn_variadic_templates ();
19290 handle_declarator:;
19291 scope = get_scope_of_declarator (declarator);
19292 if (scope)
19294 /* Any names that appear after the declarator-id for a
19295 member are looked up in the containing scope. */
19296 if (at_function_scope_p ())
19298 /* But declarations with qualified-ids can't appear in a
19299 function. */
19300 cp_parser_error (parser, "qualified-id in declaration");
19301 declarator = cp_error_declarator;
19302 break;
19304 pushed_scope = push_scope (scope);
19306 parser->in_declarator_p = true;
19307 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
19308 || (declarator && declarator->kind == cdk_id))
19309 /* Default args are only allowed on function
19310 declarations. */
19311 parser->default_arg_ok_p = saved_default_arg_ok_p;
19312 else
19313 parser->default_arg_ok_p = false;
19315 first = false;
19317 /* We're done. */
19318 else
19319 break;
19322 /* For an abstract declarator, we might wind up with nothing at this
19323 point. That's an error; the declarator is not optional. */
19324 if (!declarator)
19325 cp_parser_error (parser, "expected declarator");
19327 /* If we entered a scope, we must exit it now. */
19328 if (pushed_scope)
19329 pop_scope (pushed_scope);
19331 parser->default_arg_ok_p = saved_default_arg_ok_p;
19332 parser->in_declarator_p = saved_in_declarator_p;
19334 return declarator;
19337 /* Parse a ptr-operator.
19339 ptr-operator:
19340 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
19341 * cv-qualifier-seq [opt]
19343 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
19344 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
19346 GNU Extension:
19348 ptr-operator:
19349 & cv-qualifier-seq [opt]
19351 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
19352 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
19353 an rvalue reference. In the case of a pointer-to-member, *TYPE is
19354 filled in with the TYPE containing the member. *CV_QUALS is
19355 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
19356 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
19357 Note that the tree codes returned by this function have nothing
19358 to do with the types of trees that will be eventually be created
19359 to represent the pointer or reference type being parsed. They are
19360 just constants with suggestive names. */
19361 static enum tree_code
19362 cp_parser_ptr_operator (cp_parser* parser,
19363 tree* type,
19364 cp_cv_quals *cv_quals,
19365 tree *attributes)
19367 enum tree_code code = ERROR_MARK;
19368 cp_token *token;
19369 tree attrs = NULL_TREE;
19371 /* Assume that it's not a pointer-to-member. */
19372 *type = NULL_TREE;
19373 /* And that there are no cv-qualifiers. */
19374 *cv_quals = TYPE_UNQUALIFIED;
19376 /* Peek at the next token. */
19377 token = cp_lexer_peek_token (parser->lexer);
19379 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
19380 if (token->type == CPP_MULT)
19381 code = INDIRECT_REF;
19382 else if (token->type == CPP_AND)
19383 code = ADDR_EXPR;
19384 else if ((cxx_dialect != cxx98) &&
19385 token->type == CPP_AND_AND) /* C++0x only */
19386 code = NON_LVALUE_EXPR;
19388 if (code != ERROR_MARK)
19390 /* Consume the `*', `&' or `&&'. */
19391 cp_lexer_consume_token (parser->lexer);
19393 /* A `*' can be followed by a cv-qualifier-seq, and so can a
19394 `&', if we are allowing GNU extensions. (The only qualifier
19395 that can legally appear after `&' is `restrict', but that is
19396 enforced during semantic analysis. */
19397 if (code == INDIRECT_REF
19398 || cp_parser_allow_gnu_extensions_p (parser))
19399 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19401 attrs = cp_parser_std_attribute_spec_seq (parser);
19402 if (attributes != NULL)
19403 *attributes = attrs;
19405 else
19407 /* Try the pointer-to-member case. */
19408 cp_parser_parse_tentatively (parser);
19409 /* Look for the optional `::' operator. */
19410 cp_parser_global_scope_opt (parser,
19411 /*current_scope_valid_p=*/false);
19412 /* Look for the nested-name specifier. */
19413 token = cp_lexer_peek_token (parser->lexer);
19414 cp_parser_nested_name_specifier (parser,
19415 /*typename_keyword_p=*/false,
19416 /*check_dependency_p=*/true,
19417 /*type_p=*/false,
19418 /*is_declaration=*/false);
19419 /* If we found it, and the next token is a `*', then we are
19420 indeed looking at a pointer-to-member operator. */
19421 if (!cp_parser_error_occurred (parser)
19422 && cp_parser_require (parser, CPP_MULT, RT_MULT))
19424 /* Indicate that the `*' operator was used. */
19425 code = INDIRECT_REF;
19427 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
19428 error_at (token->location, "%qD is a namespace", parser->scope);
19429 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
19430 error_at (token->location, "cannot form pointer to member of "
19431 "non-class %q#T", parser->scope);
19432 else
19434 /* The type of which the member is a member is given by the
19435 current SCOPE. */
19436 *type = parser->scope;
19437 /* The next name will not be qualified. */
19438 parser->scope = NULL_TREE;
19439 parser->qualifying_scope = NULL_TREE;
19440 parser->object_scope = NULL_TREE;
19441 /* Look for optional c++11 attributes. */
19442 attrs = cp_parser_std_attribute_spec_seq (parser);
19443 if (attributes != NULL)
19444 *attributes = attrs;
19445 /* Look for the optional cv-qualifier-seq. */
19446 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19449 /* If that didn't work we don't have a ptr-operator. */
19450 if (!cp_parser_parse_definitely (parser))
19451 cp_parser_error (parser, "expected ptr-operator");
19454 return code;
19457 /* Parse an (optional) cv-qualifier-seq.
19459 cv-qualifier-seq:
19460 cv-qualifier cv-qualifier-seq [opt]
19462 cv-qualifier:
19463 const
19464 volatile
19466 GNU Extension:
19468 cv-qualifier:
19469 __restrict__
19471 Returns a bitmask representing the cv-qualifiers. */
19473 static cp_cv_quals
19474 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
19476 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
19478 while (true)
19480 cp_token *token;
19481 cp_cv_quals cv_qualifier;
19483 /* Peek at the next token. */
19484 token = cp_lexer_peek_token (parser->lexer);
19485 /* See if it's a cv-qualifier. */
19486 switch (token->keyword)
19488 case RID_CONST:
19489 cv_qualifier = TYPE_QUAL_CONST;
19490 break;
19492 case RID_VOLATILE:
19493 cv_qualifier = TYPE_QUAL_VOLATILE;
19494 break;
19496 case RID_RESTRICT:
19497 cv_qualifier = TYPE_QUAL_RESTRICT;
19498 break;
19500 default:
19501 cv_qualifier = TYPE_UNQUALIFIED;
19502 break;
19505 if (!cv_qualifier)
19506 break;
19508 if (cv_quals & cv_qualifier)
19510 error_at (token->location, "duplicate cv-qualifier");
19511 cp_lexer_purge_token (parser->lexer);
19513 else
19515 cp_lexer_consume_token (parser->lexer);
19516 cv_quals |= cv_qualifier;
19520 return cv_quals;
19523 /* Parse an (optional) ref-qualifier
19525 ref-qualifier:
19529 Returns cp_ref_qualifier representing ref-qualifier. */
19531 static cp_ref_qualifier
19532 cp_parser_ref_qualifier_opt (cp_parser* parser)
19534 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
19536 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
19537 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
19538 return ref_qual;
19540 while (true)
19542 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
19543 cp_token *token = cp_lexer_peek_token (parser->lexer);
19545 switch (token->type)
19547 case CPP_AND:
19548 curr_ref_qual = REF_QUAL_LVALUE;
19549 break;
19551 case CPP_AND_AND:
19552 curr_ref_qual = REF_QUAL_RVALUE;
19553 break;
19555 default:
19556 curr_ref_qual = REF_QUAL_NONE;
19557 break;
19560 if (!curr_ref_qual)
19561 break;
19562 else if (ref_qual)
19564 error_at (token->location, "multiple ref-qualifiers");
19565 cp_lexer_purge_token (parser->lexer);
19567 else
19569 ref_qual = curr_ref_qual;
19570 cp_lexer_consume_token (parser->lexer);
19574 return ref_qual;
19577 /* Parse an optional tx-qualifier.
19579 tx-qualifier:
19580 transaction_safe
19581 transaction_safe_dynamic */
19583 static tree
19584 cp_parser_tx_qualifier_opt (cp_parser *parser)
19586 cp_token *token = cp_lexer_peek_token (parser->lexer);
19587 if (token->type == CPP_NAME)
19589 tree name = token->u.value;
19590 const char *p = IDENTIFIER_POINTER (name);
19591 const int len = strlen ("transaction_safe");
19592 if (!strncmp (p, "transaction_safe", len))
19594 p += len;
19595 if (*p == '\0'
19596 || !strcmp (p, "_dynamic"))
19598 cp_lexer_consume_token (parser->lexer);
19599 if (!flag_tm)
19601 error ("%E requires %<-fgnu-tm%>", name);
19602 return NULL_TREE;
19604 else
19605 return name;
19609 return NULL_TREE;
19612 /* Parse an (optional) virt-specifier-seq.
19614 virt-specifier-seq:
19615 virt-specifier virt-specifier-seq [opt]
19617 virt-specifier:
19618 override
19619 final
19621 Returns a bitmask representing the virt-specifiers. */
19623 static cp_virt_specifiers
19624 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
19626 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19628 while (true)
19630 cp_token *token;
19631 cp_virt_specifiers virt_specifier;
19633 /* Peek at the next token. */
19634 token = cp_lexer_peek_token (parser->lexer);
19635 /* See if it's a virt-specifier-qualifier. */
19636 if (token->type != CPP_NAME)
19637 break;
19638 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
19640 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19641 virt_specifier = VIRT_SPEC_OVERRIDE;
19643 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
19645 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19646 virt_specifier = VIRT_SPEC_FINAL;
19648 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
19650 virt_specifier = VIRT_SPEC_FINAL;
19652 else
19653 break;
19655 if (virt_specifiers & virt_specifier)
19657 error_at (token->location, "duplicate virt-specifier");
19658 cp_lexer_purge_token (parser->lexer);
19660 else
19662 cp_lexer_consume_token (parser->lexer);
19663 virt_specifiers |= virt_specifier;
19666 return virt_specifiers;
19669 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
19670 is in scope even though it isn't real. */
19672 void
19673 inject_this_parameter (tree ctype, cp_cv_quals quals)
19675 tree this_parm;
19677 if (current_class_ptr)
19679 /* We don't clear this between NSDMIs. Is it already what we want? */
19680 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
19681 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
19682 && cp_type_quals (type) == quals)
19683 return;
19686 this_parm = build_this_parm (ctype, quals);
19687 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
19688 current_class_ptr = NULL_TREE;
19689 current_class_ref
19690 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
19691 current_class_ptr = this_parm;
19694 /* Return true iff our current scope is a non-static data member
19695 initializer. */
19697 bool
19698 parsing_nsdmi (void)
19700 /* We recognize NSDMI context by the context-less 'this' pointer set up
19701 by the function above. */
19702 if (current_class_ptr
19703 && TREE_CODE (current_class_ptr) == PARM_DECL
19704 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
19705 return true;
19706 return false;
19709 /* Parse a late-specified return type, if any. This is not a separate
19710 non-terminal, but part of a function declarator, which looks like
19712 -> trailing-type-specifier-seq abstract-declarator(opt)
19714 Returns the type indicated by the type-id.
19716 In addition to this, parse any queued up omp declare simd
19717 clauses and Cilk Plus SIMD-enabled function's vector attributes.
19719 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
19720 function. */
19722 static tree
19723 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
19724 tree& requires_clause, cp_cv_quals quals)
19726 cp_token *token;
19727 tree type = NULL_TREE;
19728 bool declare_simd_p = (parser->omp_declare_simd
19729 && declarator
19730 && declarator->kind == cdk_id);
19732 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
19733 && declarator && declarator->kind == cdk_id);
19735 bool oacc_routine_p = (parser->oacc_routine
19736 && declarator
19737 && declarator->kind == cdk_id);
19739 /* Peek at the next token. */
19740 token = cp_lexer_peek_token (parser->lexer);
19741 /* A late-specified return type is indicated by an initial '->'. */
19742 if (token->type != CPP_DEREF
19743 && token->keyword != RID_REQUIRES
19744 && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
19745 return NULL_TREE;
19747 tree save_ccp = current_class_ptr;
19748 tree save_ccr = current_class_ref;
19749 if (quals >= 0)
19751 /* DR 1207: 'this' is in scope in the trailing return type. */
19752 inject_this_parameter (current_class_type, quals);
19755 if (token->type == CPP_DEREF)
19757 /* Consume the ->. */
19758 cp_lexer_consume_token (parser->lexer);
19760 type = cp_parser_trailing_type_id (parser);
19763 /* Function declarations may be followed by a trailing
19764 requires-clause. */
19765 requires_clause = cp_parser_requires_clause_opt (parser);
19767 if (cilk_simd_fn_vector_p)
19768 declarator->attributes
19769 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
19770 declarator->attributes);
19771 if (declare_simd_p)
19772 declarator->attributes
19773 = cp_parser_late_parsing_omp_declare_simd (parser,
19774 declarator->attributes);
19775 if (oacc_routine_p)
19776 declarator->attributes
19777 = cp_parser_late_parsing_oacc_routine (parser,
19778 declarator->attributes);
19780 if (quals >= 0)
19782 current_class_ptr = save_ccp;
19783 current_class_ref = save_ccr;
19786 return type;
19789 /* Parse a declarator-id.
19791 declarator-id:
19792 id-expression
19793 :: [opt] nested-name-specifier [opt] type-name
19795 In the `id-expression' case, the value returned is as for
19796 cp_parser_id_expression if the id-expression was an unqualified-id.
19797 If the id-expression was a qualified-id, then a SCOPE_REF is
19798 returned. The first operand is the scope (either a NAMESPACE_DECL
19799 or TREE_TYPE), but the second is still just a representation of an
19800 unqualified-id. */
19802 static tree
19803 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
19805 tree id;
19806 /* The expression must be an id-expression. Assume that qualified
19807 names are the names of types so that:
19809 template <class T>
19810 int S<T>::R::i = 3;
19812 will work; we must treat `S<T>::R' as the name of a type.
19813 Similarly, assume that qualified names are templates, where
19814 required, so that:
19816 template <class T>
19817 int S<T>::R<T>::i = 3;
19819 will work, too. */
19820 id = cp_parser_id_expression (parser,
19821 /*template_keyword_p=*/false,
19822 /*check_dependency_p=*/false,
19823 /*template_p=*/NULL,
19824 /*declarator_p=*/true,
19825 optional_p);
19826 if (id && BASELINK_P (id))
19827 id = BASELINK_FUNCTIONS (id);
19828 return id;
19831 /* Parse a type-id.
19833 type-id:
19834 type-specifier-seq abstract-declarator [opt]
19836 Returns the TYPE specified. */
19838 static tree
19839 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
19840 bool is_trailing_return)
19842 cp_decl_specifier_seq type_specifier_seq;
19843 cp_declarator *abstract_declarator;
19845 /* Parse the type-specifier-seq. */
19846 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
19847 is_trailing_return,
19848 &type_specifier_seq);
19849 if (type_specifier_seq.type == error_mark_node)
19850 return error_mark_node;
19852 /* There might or might not be an abstract declarator. */
19853 cp_parser_parse_tentatively (parser);
19854 /* Look for the declarator. */
19855 abstract_declarator
19856 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
19857 /*parenthesized_p=*/NULL,
19858 /*member_p=*/false,
19859 /*friend_p=*/false);
19860 /* Check to see if there really was a declarator. */
19861 if (!cp_parser_parse_definitely (parser))
19862 abstract_declarator = NULL;
19864 if (type_specifier_seq.type
19865 /* The concepts TS allows 'auto' as a type-id. */
19866 && (!flag_concepts || parser->in_type_id_in_expr_p)
19867 /* None of the valid uses of 'auto' in C++14 involve the type-id
19868 nonterminal, but it is valid in a trailing-return-type. */
19869 && !(cxx_dialect >= cxx14 && is_trailing_return)
19870 && type_uses_auto (type_specifier_seq.type))
19872 /* A type-id with type 'auto' is only ok if the abstract declarator
19873 is a function declarator with a late-specified return type.
19875 A type-id with 'auto' is also valid in a trailing-return-type
19876 in a compound-requirement. */
19877 if (abstract_declarator
19878 && abstract_declarator->kind == cdk_function
19879 && abstract_declarator->u.function.late_return_type)
19880 /* OK */;
19881 else if (parser->in_result_type_constraint_p)
19882 /* OK */;
19883 else
19885 error ("invalid use of %<auto%>");
19886 return error_mark_node;
19890 return groktypename (&type_specifier_seq, abstract_declarator,
19891 is_template_arg);
19894 static tree
19895 cp_parser_type_id (cp_parser *parser)
19897 return cp_parser_type_id_1 (parser, false, false);
19900 static tree
19901 cp_parser_template_type_arg (cp_parser *parser)
19903 tree r;
19904 const char *saved_message = parser->type_definition_forbidden_message;
19905 parser->type_definition_forbidden_message
19906 = G_("types may not be defined in template arguments");
19907 r = cp_parser_type_id_1 (parser, true, false);
19908 parser->type_definition_forbidden_message = saved_message;
19909 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
19911 error ("invalid use of %<auto%> in template argument");
19912 r = error_mark_node;
19914 return r;
19917 static tree
19918 cp_parser_trailing_type_id (cp_parser *parser)
19920 return cp_parser_type_id_1 (parser, false, true);
19923 /* Parse a type-specifier-seq.
19925 type-specifier-seq:
19926 type-specifier type-specifier-seq [opt]
19928 GNU extension:
19930 type-specifier-seq:
19931 attributes type-specifier-seq [opt]
19933 If IS_DECLARATION is true, we are at the start of a "condition" or
19934 exception-declaration, so we might be followed by a declarator-id.
19936 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
19937 i.e. we've just seen "->".
19939 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
19941 static void
19942 cp_parser_type_specifier_seq (cp_parser* parser,
19943 bool is_declaration,
19944 bool is_trailing_return,
19945 cp_decl_specifier_seq *type_specifier_seq)
19947 bool seen_type_specifier = false;
19948 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
19949 cp_token *start_token = NULL;
19951 /* Clear the TYPE_SPECIFIER_SEQ. */
19952 clear_decl_specs (type_specifier_seq);
19954 /* In the context of a trailing return type, enum E { } is an
19955 elaborated-type-specifier followed by a function-body, not an
19956 enum-specifier. */
19957 if (is_trailing_return)
19958 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
19960 /* Parse the type-specifiers and attributes. */
19961 while (true)
19963 tree type_specifier;
19964 bool is_cv_qualifier;
19966 /* Check for attributes first. */
19967 if (cp_next_tokens_can_be_attribute_p (parser))
19969 type_specifier_seq->attributes =
19970 chainon (type_specifier_seq->attributes,
19971 cp_parser_attributes_opt (parser));
19972 continue;
19975 /* record the token of the beginning of the type specifier seq,
19976 for error reporting purposes*/
19977 if (!start_token)
19978 start_token = cp_lexer_peek_token (parser->lexer);
19980 /* Look for the type-specifier. */
19981 type_specifier = cp_parser_type_specifier (parser,
19982 flags,
19983 type_specifier_seq,
19984 /*is_declaration=*/false,
19985 NULL,
19986 &is_cv_qualifier);
19987 if (!type_specifier)
19989 /* If the first type-specifier could not be found, this is not a
19990 type-specifier-seq at all. */
19991 if (!seen_type_specifier)
19993 /* Set in_declarator_p to avoid skipping to the semicolon. */
19994 int in_decl = parser->in_declarator_p;
19995 parser->in_declarator_p = true;
19997 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
19998 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
19999 cp_parser_error (parser, "expected type-specifier");
20001 parser->in_declarator_p = in_decl;
20003 type_specifier_seq->type = error_mark_node;
20004 return;
20006 /* If subsequent type-specifiers could not be found, the
20007 type-specifier-seq is complete. */
20008 break;
20011 seen_type_specifier = true;
20012 /* The standard says that a condition can be:
20014 type-specifier-seq declarator = assignment-expression
20016 However, given:
20018 struct S {};
20019 if (int S = ...)
20021 we should treat the "S" as a declarator, not as a
20022 type-specifier. The standard doesn't say that explicitly for
20023 type-specifier-seq, but it does say that for
20024 decl-specifier-seq in an ordinary declaration. Perhaps it
20025 would be clearer just to allow a decl-specifier-seq here, and
20026 then add a semantic restriction that if any decl-specifiers
20027 that are not type-specifiers appear, the program is invalid. */
20028 if (is_declaration && !is_cv_qualifier)
20029 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
20033 /* Return whether the function currently being declared has an associated
20034 template parameter list. */
20036 static bool
20037 function_being_declared_is_template_p (cp_parser* parser)
20039 if (!current_template_parms || processing_template_parmlist)
20040 return false;
20042 if (parser->implicit_template_scope)
20043 return true;
20045 if (at_class_scope_p ()
20046 && TYPE_BEING_DEFINED (current_class_type))
20047 return parser->num_template_parameter_lists != 0;
20049 return ((int) parser->num_template_parameter_lists > template_class_depth
20050 (current_class_type));
20053 /* Parse a parameter-declaration-clause.
20055 parameter-declaration-clause:
20056 parameter-declaration-list [opt] ... [opt]
20057 parameter-declaration-list , ...
20059 Returns a representation for the parameter declarations. A return
20060 value of NULL indicates a parameter-declaration-clause consisting
20061 only of an ellipsis. */
20063 static tree
20064 cp_parser_parameter_declaration_clause (cp_parser* parser)
20066 tree parameters;
20067 cp_token *token;
20068 bool ellipsis_p;
20069 bool is_error;
20071 struct cleanup {
20072 cp_parser* parser;
20073 int auto_is_implicit_function_template_parm_p;
20074 ~cleanup() {
20075 parser->auto_is_implicit_function_template_parm_p
20076 = auto_is_implicit_function_template_parm_p;
20078 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
20080 (void) cleanup;
20082 if (!processing_specialization
20083 && !processing_template_parmlist
20084 && !processing_explicit_instantiation)
20085 if (!current_function_decl
20086 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
20087 parser->auto_is_implicit_function_template_parm_p = true;
20089 /* Peek at the next token. */
20090 token = cp_lexer_peek_token (parser->lexer);
20091 /* Check for trivial parameter-declaration-clauses. */
20092 if (token->type == CPP_ELLIPSIS)
20094 /* Consume the `...' token. */
20095 cp_lexer_consume_token (parser->lexer);
20096 return NULL_TREE;
20098 else if (token->type == CPP_CLOSE_PAREN)
20099 /* There are no parameters. */
20101 #ifndef NO_IMPLICIT_EXTERN_C
20102 if (in_system_header_at (input_location)
20103 && current_class_type == NULL
20104 && current_lang_name == lang_name_c)
20105 return NULL_TREE;
20106 else
20107 #endif
20108 return void_list_node;
20110 /* Check for `(void)', too, which is a special case. */
20111 else if (token->keyword == RID_VOID
20112 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20113 == CPP_CLOSE_PAREN))
20115 /* Consume the `void' token. */
20116 cp_lexer_consume_token (parser->lexer);
20117 /* There are no parameters. */
20118 return void_list_node;
20121 /* Parse the parameter-declaration-list. */
20122 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
20123 /* If a parse error occurred while parsing the
20124 parameter-declaration-list, then the entire
20125 parameter-declaration-clause is erroneous. */
20126 if (is_error)
20127 return NULL;
20129 /* Peek at the next token. */
20130 token = cp_lexer_peek_token (parser->lexer);
20131 /* If it's a `,', the clause should terminate with an ellipsis. */
20132 if (token->type == CPP_COMMA)
20134 /* Consume the `,'. */
20135 cp_lexer_consume_token (parser->lexer);
20136 /* Expect an ellipsis. */
20137 ellipsis_p
20138 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
20140 /* It might also be `...' if the optional trailing `,' was
20141 omitted. */
20142 else if (token->type == CPP_ELLIPSIS)
20144 /* Consume the `...' token. */
20145 cp_lexer_consume_token (parser->lexer);
20146 /* And remember that we saw it. */
20147 ellipsis_p = true;
20149 else
20150 ellipsis_p = false;
20152 /* Finish the parameter list. */
20153 if (!ellipsis_p)
20154 parameters = chainon (parameters, void_list_node);
20156 return parameters;
20159 /* Parse a parameter-declaration-list.
20161 parameter-declaration-list:
20162 parameter-declaration
20163 parameter-declaration-list , parameter-declaration
20165 Returns a representation of the parameter-declaration-list, as for
20166 cp_parser_parameter_declaration_clause. However, the
20167 `void_list_node' is never appended to the list. Upon return,
20168 *IS_ERROR will be true iff an error occurred. */
20170 static tree
20171 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
20173 tree parameters = NULL_TREE;
20174 tree *tail = &parameters;
20175 bool saved_in_unbraced_linkage_specification_p;
20176 int index = 0;
20178 /* Assume all will go well. */
20179 *is_error = false;
20180 /* The special considerations that apply to a function within an
20181 unbraced linkage specifications do not apply to the parameters
20182 to the function. */
20183 saved_in_unbraced_linkage_specification_p
20184 = parser->in_unbraced_linkage_specification_p;
20185 parser->in_unbraced_linkage_specification_p = false;
20187 /* Look for more parameters. */
20188 while (true)
20190 cp_parameter_declarator *parameter;
20191 tree decl = error_mark_node;
20192 bool parenthesized_p = false;
20193 int template_parm_idx = (function_being_declared_is_template_p (parser)?
20194 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
20195 (current_template_parms)) : 0);
20197 /* Parse the parameter. */
20198 parameter
20199 = cp_parser_parameter_declaration (parser,
20200 /*template_parm_p=*/false,
20201 &parenthesized_p);
20203 /* We don't know yet if the enclosing context is deprecated, so wait
20204 and warn in grokparms if appropriate. */
20205 deprecated_state = DEPRECATED_SUPPRESS;
20207 if (parameter)
20209 /* If a function parameter pack was specified and an implicit template
20210 parameter was introduced during cp_parser_parameter_declaration,
20211 change any implicit parameters introduced into packs. */
20212 if (parser->implicit_template_parms
20213 && parameter->declarator
20214 && parameter->declarator->parameter_pack_p)
20216 int latest_template_parm_idx = TREE_VEC_LENGTH
20217 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
20219 if (latest_template_parm_idx != template_parm_idx)
20220 parameter->decl_specifiers.type = convert_generic_types_to_packs
20221 (parameter->decl_specifiers.type,
20222 template_parm_idx, latest_template_parm_idx);
20225 decl = grokdeclarator (parameter->declarator,
20226 &parameter->decl_specifiers,
20227 PARM,
20228 parameter->default_argument != NULL_TREE,
20229 &parameter->decl_specifiers.attributes);
20232 deprecated_state = DEPRECATED_NORMAL;
20234 /* If a parse error occurred parsing the parameter declaration,
20235 then the entire parameter-declaration-list is erroneous. */
20236 if (decl == error_mark_node)
20238 *is_error = true;
20239 parameters = error_mark_node;
20240 break;
20243 if (parameter->decl_specifiers.attributes)
20244 cplus_decl_attributes (&decl,
20245 parameter->decl_specifiers.attributes,
20247 if (DECL_NAME (decl))
20248 decl = pushdecl (decl);
20250 if (decl != error_mark_node)
20252 retrofit_lang_decl (decl);
20253 DECL_PARM_INDEX (decl) = ++index;
20254 DECL_PARM_LEVEL (decl) = function_parm_depth ();
20257 /* Add the new parameter to the list. */
20258 *tail = build_tree_list (parameter->default_argument, decl);
20259 tail = &TREE_CHAIN (*tail);
20261 /* Peek at the next token. */
20262 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
20263 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
20264 /* These are for Objective-C++ */
20265 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20266 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20267 /* The parameter-declaration-list is complete. */
20268 break;
20269 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20271 cp_token *token;
20273 /* Peek at the next token. */
20274 token = cp_lexer_peek_nth_token (parser->lexer, 2);
20275 /* If it's an ellipsis, then the list is complete. */
20276 if (token->type == CPP_ELLIPSIS)
20277 break;
20278 /* Otherwise, there must be more parameters. Consume the
20279 `,'. */
20280 cp_lexer_consume_token (parser->lexer);
20281 /* When parsing something like:
20283 int i(float f, double d)
20285 we can tell after seeing the declaration for "f" that we
20286 are not looking at an initialization of a variable "i",
20287 but rather at the declaration of a function "i".
20289 Due to the fact that the parsing of template arguments
20290 (as specified to a template-id) requires backtracking we
20291 cannot use this technique when inside a template argument
20292 list. */
20293 if (!parser->in_template_argument_list_p
20294 && !parser->in_type_id_in_expr_p
20295 && cp_parser_uncommitted_to_tentative_parse_p (parser)
20296 /* However, a parameter-declaration of the form
20297 "float(f)" (which is a valid declaration of a
20298 parameter "f") can also be interpreted as an
20299 expression (the conversion of "f" to "float"). */
20300 && !parenthesized_p)
20301 cp_parser_commit_to_tentative_parse (parser);
20303 else
20305 cp_parser_error (parser, "expected %<,%> or %<...%>");
20306 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
20307 cp_parser_skip_to_closing_parenthesis (parser,
20308 /*recovering=*/true,
20309 /*or_comma=*/false,
20310 /*consume_paren=*/false);
20311 break;
20315 parser->in_unbraced_linkage_specification_p
20316 = saved_in_unbraced_linkage_specification_p;
20318 /* Reset implicit_template_scope if we are about to leave the function
20319 parameter list that introduced it. Note that for out-of-line member
20320 definitions, there will be one or more class scopes before we get to
20321 the template parameter scope. */
20323 if (cp_binding_level *its = parser->implicit_template_scope)
20324 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
20326 while (maybe_its->kind == sk_class)
20327 maybe_its = maybe_its->level_chain;
20328 if (maybe_its == its)
20330 parser->implicit_template_parms = 0;
20331 parser->implicit_template_scope = 0;
20335 return parameters;
20338 /* Parse a parameter declaration.
20340 parameter-declaration:
20341 decl-specifier-seq ... [opt] declarator
20342 decl-specifier-seq declarator = assignment-expression
20343 decl-specifier-seq ... [opt] abstract-declarator [opt]
20344 decl-specifier-seq abstract-declarator [opt] = assignment-expression
20346 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
20347 declares a template parameter. (In that case, a non-nested `>'
20348 token encountered during the parsing of the assignment-expression
20349 is not interpreted as a greater-than operator.)
20351 Returns a representation of the parameter, or NULL if an error
20352 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
20353 true iff the declarator is of the form "(p)". */
20355 static cp_parameter_declarator *
20356 cp_parser_parameter_declaration (cp_parser *parser,
20357 bool template_parm_p,
20358 bool *parenthesized_p)
20360 int declares_class_or_enum;
20361 cp_decl_specifier_seq decl_specifiers;
20362 cp_declarator *declarator;
20363 tree default_argument;
20364 cp_token *token = NULL, *declarator_token_start = NULL;
20365 const char *saved_message;
20366 bool template_parameter_pack_p = false;
20368 /* In a template parameter, `>' is not an operator.
20370 [temp.param]
20372 When parsing a default template-argument for a non-type
20373 template-parameter, the first non-nested `>' is taken as the end
20374 of the template parameter-list rather than a greater-than
20375 operator. */
20377 /* Type definitions may not appear in parameter types. */
20378 saved_message = parser->type_definition_forbidden_message;
20379 parser->type_definition_forbidden_message
20380 = G_("types may not be defined in parameter types");
20382 /* Parse the declaration-specifiers. */
20383 cp_parser_decl_specifier_seq (parser,
20384 CP_PARSER_FLAGS_NONE,
20385 &decl_specifiers,
20386 &declares_class_or_enum);
20388 /* Complain about missing 'typename' or other invalid type names. */
20389 if (!decl_specifiers.any_type_specifiers_p
20390 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20391 decl_specifiers.type = error_mark_node;
20393 /* If an error occurred, there's no reason to attempt to parse the
20394 rest of the declaration. */
20395 if (cp_parser_error_occurred (parser))
20397 parser->type_definition_forbidden_message = saved_message;
20398 return NULL;
20401 /* Peek at the next token. */
20402 token = cp_lexer_peek_token (parser->lexer);
20404 /* If the next token is a `)', `,', `=', `>', or `...', then there
20405 is no declarator. However, when variadic templates are enabled,
20406 there may be a declarator following `...'. */
20407 if (token->type == CPP_CLOSE_PAREN
20408 || token->type == CPP_COMMA
20409 || token->type == CPP_EQ
20410 || token->type == CPP_GREATER)
20412 declarator = NULL;
20413 if (parenthesized_p)
20414 *parenthesized_p = false;
20416 /* Otherwise, there should be a declarator. */
20417 else
20419 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20420 parser->default_arg_ok_p = false;
20422 /* After seeing a decl-specifier-seq, if the next token is not a
20423 "(", there is no possibility that the code is a valid
20424 expression. Therefore, if parsing tentatively, we commit at
20425 this point. */
20426 if (!parser->in_template_argument_list_p
20427 /* In an expression context, having seen:
20429 (int((char ...
20431 we cannot be sure whether we are looking at a
20432 function-type (taking a "char" as a parameter) or a cast
20433 of some object of type "char" to "int". */
20434 && !parser->in_type_id_in_expr_p
20435 && cp_parser_uncommitted_to_tentative_parse_p (parser)
20436 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20437 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
20438 cp_parser_commit_to_tentative_parse (parser);
20439 /* Parse the declarator. */
20440 declarator_token_start = token;
20441 declarator = cp_parser_declarator (parser,
20442 CP_PARSER_DECLARATOR_EITHER,
20443 /*ctor_dtor_or_conv_p=*/NULL,
20444 parenthesized_p,
20445 /*member_p=*/false,
20446 /*friend_p=*/false);
20447 parser->default_arg_ok_p = saved_default_arg_ok_p;
20448 /* After the declarator, allow more attributes. */
20449 decl_specifiers.attributes
20450 = chainon (decl_specifiers.attributes,
20451 cp_parser_attributes_opt (parser));
20453 /* If the declarator is a template parameter pack, remember that and
20454 clear the flag in the declarator itself so we don't get errors
20455 from grokdeclarator. */
20456 if (template_parm_p && declarator && declarator->parameter_pack_p)
20458 declarator->parameter_pack_p = false;
20459 template_parameter_pack_p = true;
20463 /* If the next token is an ellipsis, and we have not seen a declarator
20464 name, and if either the type of the declarator contains parameter
20465 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
20466 for, eg, abbreviated integral type names), then we actually have a
20467 parameter pack expansion expression. Otherwise, leave the ellipsis
20468 for a C-style variadic function. */
20469 token = cp_lexer_peek_token (parser->lexer);
20470 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20472 tree type = decl_specifiers.type;
20474 if (type && DECL_P (type))
20475 type = TREE_TYPE (type);
20477 if (((type
20478 && TREE_CODE (type) != TYPE_PACK_EXPANSION
20479 && (template_parm_p || uses_parameter_packs (type)))
20480 || (!type && template_parm_p))
20481 && declarator_can_be_parameter_pack (declarator))
20483 /* Consume the `...'. */
20484 cp_lexer_consume_token (parser->lexer);
20485 maybe_warn_variadic_templates ();
20487 /* Build a pack expansion type */
20488 if (template_parm_p)
20489 template_parameter_pack_p = true;
20490 else if (declarator)
20491 declarator->parameter_pack_p = true;
20492 else
20493 decl_specifiers.type = make_pack_expansion (type);
20497 /* The restriction on defining new types applies only to the type
20498 of the parameter, not to the default argument. */
20499 parser->type_definition_forbidden_message = saved_message;
20501 /* If the next token is `=', then process a default argument. */
20502 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20504 tree type = decl_specifiers.type;
20505 token = cp_lexer_peek_token (parser->lexer);
20506 /* If we are defining a class, then the tokens that make up the
20507 default argument must be saved and processed later. */
20508 if (!template_parm_p && at_class_scope_p ()
20509 && TYPE_BEING_DEFINED (current_class_type)
20510 && !LAMBDA_TYPE_P (current_class_type))
20511 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
20513 // A constrained-type-specifier may declare a type template-parameter.
20514 else if (declares_constrained_type_template_parameter (type))
20515 default_argument
20516 = cp_parser_default_type_template_argument (parser);
20518 // A constrained-type-specifier may declare a template-template-parameter.
20519 else if (declares_constrained_template_template_parameter (type))
20520 default_argument
20521 = cp_parser_default_template_template_argument (parser);
20523 /* Outside of a class definition, we can just parse the
20524 assignment-expression. */
20525 else
20526 default_argument
20527 = cp_parser_default_argument (parser, template_parm_p);
20529 if (!parser->default_arg_ok_p)
20531 permerror (token->location,
20532 "default arguments are only "
20533 "permitted for function parameters");
20535 else if ((declarator && declarator->parameter_pack_p)
20536 || template_parameter_pack_p
20537 || (decl_specifiers.type
20538 && PACK_EXPANSION_P (decl_specifiers.type)))
20540 /* Find the name of the parameter pack. */
20541 cp_declarator *id_declarator = declarator;
20542 while (id_declarator && id_declarator->kind != cdk_id)
20543 id_declarator = id_declarator->declarator;
20545 if (id_declarator && id_declarator->kind == cdk_id)
20546 error_at (declarator_token_start->location,
20547 template_parm_p
20548 ? G_("template parameter pack %qD "
20549 "cannot have a default argument")
20550 : G_("parameter pack %qD cannot have "
20551 "a default argument"),
20552 id_declarator->u.id.unqualified_name);
20553 else
20554 error_at (declarator_token_start->location,
20555 template_parm_p
20556 ? G_("template parameter pack cannot have "
20557 "a default argument")
20558 : G_("parameter pack cannot have a "
20559 "default argument"));
20561 default_argument = NULL_TREE;
20564 else
20565 default_argument = NULL_TREE;
20567 return make_parameter_declarator (&decl_specifiers,
20568 declarator,
20569 default_argument,
20570 template_parameter_pack_p);
20573 /* Parse a default argument and return it.
20575 TEMPLATE_PARM_P is true if this is a default argument for a
20576 non-type template parameter. */
20577 static tree
20578 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
20580 tree default_argument = NULL_TREE;
20581 bool saved_greater_than_is_operator_p;
20582 bool saved_local_variables_forbidden_p;
20583 bool non_constant_p, is_direct_init;
20585 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
20586 set correctly. */
20587 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
20588 parser->greater_than_is_operator_p = !template_parm_p;
20589 /* Local variable names (and the `this' keyword) may not
20590 appear in a default argument. */
20591 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20592 parser->local_variables_forbidden_p = true;
20593 /* Parse the assignment-expression. */
20594 if (template_parm_p)
20595 push_deferring_access_checks (dk_no_deferred);
20596 tree saved_class_ptr = NULL_TREE;
20597 tree saved_class_ref = NULL_TREE;
20598 /* The "this" pointer is not valid in a default argument. */
20599 if (cfun)
20601 saved_class_ptr = current_class_ptr;
20602 cp_function_chain->x_current_class_ptr = NULL_TREE;
20603 saved_class_ref = current_class_ref;
20604 cp_function_chain->x_current_class_ref = NULL_TREE;
20606 default_argument
20607 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
20608 /* Restore the "this" pointer. */
20609 if (cfun)
20611 cp_function_chain->x_current_class_ptr = saved_class_ptr;
20612 cp_function_chain->x_current_class_ref = saved_class_ref;
20614 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
20615 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20616 if (template_parm_p)
20617 pop_deferring_access_checks ();
20618 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
20619 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20621 return default_argument;
20624 /* Parse a function-body.
20626 function-body:
20627 compound_statement */
20629 static void
20630 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
20632 cp_parser_compound_statement (parser, NULL, (in_function_try_block
20633 ? BCS_TRY_BLOCK : BCS_NORMAL),
20634 true);
20637 /* Parse a ctor-initializer-opt followed by a function-body. Return
20638 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
20639 is true we are parsing a function-try-block. */
20641 static bool
20642 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
20643 bool in_function_try_block)
20645 tree body, list;
20646 bool ctor_initializer_p;
20647 const bool check_body_p =
20648 DECL_CONSTRUCTOR_P (current_function_decl)
20649 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
20650 tree last = NULL;
20652 /* Begin the function body. */
20653 body = begin_function_body ();
20654 /* Parse the optional ctor-initializer. */
20655 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
20657 /* If we're parsing a constexpr constructor definition, we need
20658 to check that the constructor body is indeed empty. However,
20659 before we get to cp_parser_function_body lot of junk has been
20660 generated, so we can't just check that we have an empty block.
20661 Rather we take a snapshot of the outermost block, and check whether
20662 cp_parser_function_body changed its state. */
20663 if (check_body_p)
20665 list = cur_stmt_list;
20666 if (STATEMENT_LIST_TAIL (list))
20667 last = STATEMENT_LIST_TAIL (list)->stmt;
20669 /* Parse the function-body. */
20670 cp_parser_function_body (parser, in_function_try_block);
20671 if (check_body_p)
20672 check_constexpr_ctor_body (last, list, /*complain=*/true);
20673 /* Finish the function body. */
20674 finish_function_body (body);
20676 return ctor_initializer_p;
20679 /* Parse an initializer.
20681 initializer:
20682 = initializer-clause
20683 ( expression-list )
20685 Returns an expression representing the initializer. If no
20686 initializer is present, NULL_TREE is returned.
20688 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
20689 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
20690 set to TRUE if there is no initializer present. If there is an
20691 initializer, and it is not a constant-expression, *NON_CONSTANT_P
20692 is set to true; otherwise it is set to false. */
20694 static tree
20695 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
20696 bool* non_constant_p)
20698 cp_token *token;
20699 tree init;
20701 /* Peek at the next token. */
20702 token = cp_lexer_peek_token (parser->lexer);
20704 /* Let our caller know whether or not this initializer was
20705 parenthesized. */
20706 *is_direct_init = (token->type != CPP_EQ);
20707 /* Assume that the initializer is constant. */
20708 *non_constant_p = false;
20710 if (token->type == CPP_EQ)
20712 /* Consume the `='. */
20713 cp_lexer_consume_token (parser->lexer);
20714 /* Parse the initializer-clause. */
20715 init = cp_parser_initializer_clause (parser, non_constant_p);
20717 else if (token->type == CPP_OPEN_PAREN)
20719 vec<tree, va_gc> *vec;
20720 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20721 /*cast_p=*/false,
20722 /*allow_expansion_p=*/true,
20723 non_constant_p);
20724 if (vec == NULL)
20725 return error_mark_node;
20726 init = build_tree_list_vec (vec);
20727 release_tree_vector (vec);
20729 else if (token->type == CPP_OPEN_BRACE)
20731 cp_lexer_set_source_position (parser->lexer);
20732 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20733 init = cp_parser_braced_list (parser, non_constant_p);
20734 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
20736 else
20738 /* Anything else is an error. */
20739 cp_parser_error (parser, "expected initializer");
20740 init = error_mark_node;
20743 return init;
20746 /* Parse an initializer-clause.
20748 initializer-clause:
20749 assignment-expression
20750 braced-init-list
20752 Returns an expression representing the initializer.
20754 If the `assignment-expression' production is used the value
20755 returned is simply a representation for the expression.
20757 Otherwise, calls cp_parser_braced_list. */
20759 static cp_expr
20760 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
20762 cp_expr initializer;
20764 /* Assume the expression is constant. */
20765 *non_constant_p = false;
20767 /* If it is not a `{', then we are looking at an
20768 assignment-expression. */
20769 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
20771 initializer
20772 = cp_parser_constant_expression (parser,
20773 /*allow_non_constant_p=*/true,
20774 non_constant_p);
20776 else
20777 initializer = cp_parser_braced_list (parser, non_constant_p);
20779 return initializer;
20782 /* Parse a brace-enclosed initializer list.
20784 braced-init-list:
20785 { initializer-list , [opt] }
20788 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
20789 the elements of the initializer-list (or NULL, if the last
20790 production is used). The TREE_TYPE for the CONSTRUCTOR will be
20791 NULL_TREE. There is no way to detect whether or not the optional
20792 trailing `,' was provided. NON_CONSTANT_P is as for
20793 cp_parser_initializer. */
20795 static cp_expr
20796 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
20798 tree initializer;
20799 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
20801 /* Consume the `{' token. */
20802 cp_lexer_consume_token (parser->lexer);
20803 /* Create a CONSTRUCTOR to represent the braced-initializer. */
20804 initializer = make_node (CONSTRUCTOR);
20805 /* If it's not a `}', then there is a non-trivial initializer. */
20806 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
20808 /* Parse the initializer list. */
20809 CONSTRUCTOR_ELTS (initializer)
20810 = cp_parser_initializer_list (parser, non_constant_p);
20811 /* A trailing `,' token is allowed. */
20812 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20813 cp_lexer_consume_token (parser->lexer);
20815 else
20816 *non_constant_p = false;
20817 /* Now, there should be a trailing `}'. */
20818 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
20819 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20820 TREE_TYPE (initializer) = init_list_type_node;
20822 cp_expr result (initializer);
20823 /* Build a location of the form:
20824 { ... }
20825 ^~~~~~~
20826 with caret==start at the open brace, finish at the close brace. */
20827 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
20828 result.set_location (combined_loc);
20829 return result;
20832 /* Consume tokens up to, and including, the next non-nested closing `]'.
20833 Returns true iff we found a closing `]'. */
20835 static bool
20836 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
20838 unsigned square_depth = 0;
20840 while (true)
20842 cp_token * token = cp_lexer_peek_token (parser->lexer);
20844 switch (token->type)
20846 case CPP_EOF:
20847 case CPP_PRAGMA_EOL:
20848 /* If we've run out of tokens, then there is no closing `]'. */
20849 return false;
20851 case CPP_OPEN_SQUARE:
20852 ++square_depth;
20853 break;
20855 case CPP_CLOSE_SQUARE:
20856 if (!square_depth--)
20858 cp_lexer_consume_token (parser->lexer);
20859 return true;
20861 break;
20863 default:
20864 break;
20867 /* Consume the token. */
20868 cp_lexer_consume_token (parser->lexer);
20872 /* Return true if we are looking at an array-designator, false otherwise. */
20874 static bool
20875 cp_parser_array_designator_p (cp_parser *parser)
20877 /* Consume the `['. */
20878 cp_lexer_consume_token (parser->lexer);
20880 cp_lexer_save_tokens (parser->lexer);
20882 /* Skip tokens until the next token is a closing square bracket.
20883 If we find the closing `]', and the next token is a `=', then
20884 we are looking at an array designator. */
20885 bool array_designator_p
20886 = (cp_parser_skip_to_closing_square_bracket (parser)
20887 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
20889 /* Roll back the tokens we skipped. */
20890 cp_lexer_rollback_tokens (parser->lexer);
20892 return array_designator_p;
20895 /* Parse an initializer-list.
20897 initializer-list:
20898 initializer-clause ... [opt]
20899 initializer-list , initializer-clause ... [opt]
20901 GNU Extension:
20903 initializer-list:
20904 designation initializer-clause ...[opt]
20905 initializer-list , designation initializer-clause ...[opt]
20907 designation:
20908 . identifier =
20909 identifier :
20910 [ constant-expression ] =
20912 Returns a vec of constructor_elt. The VALUE of each elt is an expression
20913 for the initializer. If the INDEX of the elt is non-NULL, it is the
20914 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
20915 as for cp_parser_initializer. */
20917 static vec<constructor_elt, va_gc> *
20918 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
20920 vec<constructor_elt, va_gc> *v = NULL;
20922 /* Assume all of the expressions are constant. */
20923 *non_constant_p = false;
20925 /* Parse the rest of the list. */
20926 while (true)
20928 cp_token *token;
20929 tree designator;
20930 tree initializer;
20931 bool clause_non_constant_p;
20933 /* If the next token is an identifier and the following one is a
20934 colon, we are looking at the GNU designated-initializer
20935 syntax. */
20936 if (cp_parser_allow_gnu_extensions_p (parser)
20937 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
20938 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
20940 /* Warn the user that they are using an extension. */
20941 pedwarn (input_location, OPT_Wpedantic,
20942 "ISO C++ does not allow designated initializers");
20943 /* Consume the identifier. */
20944 designator = cp_lexer_consume_token (parser->lexer)->u.value;
20945 /* Consume the `:'. */
20946 cp_lexer_consume_token (parser->lexer);
20948 /* Also handle the C99 syntax, '. id ='. */
20949 else if (cp_parser_allow_gnu_extensions_p (parser)
20950 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
20951 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
20952 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
20954 /* Warn the user that they are using an extension. */
20955 pedwarn (input_location, OPT_Wpedantic,
20956 "ISO C++ does not allow C99 designated initializers");
20957 /* Consume the `.'. */
20958 cp_lexer_consume_token (parser->lexer);
20959 /* Consume the identifier. */
20960 designator = cp_lexer_consume_token (parser->lexer)->u.value;
20961 /* Consume the `='. */
20962 cp_lexer_consume_token (parser->lexer);
20964 /* Also handle C99 array designators, '[ const ] ='. */
20965 else if (cp_parser_allow_gnu_extensions_p (parser)
20966 && !c_dialect_objc ()
20967 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
20969 /* In C++11, [ could start a lambda-introducer. */
20970 bool non_const = false;
20972 cp_parser_parse_tentatively (parser);
20974 if (!cp_parser_array_designator_p (parser))
20976 cp_parser_simulate_error (parser);
20977 designator = NULL_TREE;
20979 else
20981 designator = cp_parser_constant_expression (parser, true,
20982 &non_const);
20983 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
20984 cp_parser_require (parser, CPP_EQ, RT_EQ);
20987 if (!cp_parser_parse_definitely (parser))
20988 designator = NULL_TREE;
20989 else if (non_const)
20990 require_potential_rvalue_constant_expression (designator);
20992 else
20993 designator = NULL_TREE;
20995 /* Parse the initializer. */
20996 initializer = cp_parser_initializer_clause (parser,
20997 &clause_non_constant_p);
20998 /* If any clause is non-constant, so is the entire initializer. */
20999 if (clause_non_constant_p)
21000 *non_constant_p = true;
21002 /* If we have an ellipsis, this is an initializer pack
21003 expansion. */
21004 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21006 /* Consume the `...'. */
21007 cp_lexer_consume_token (parser->lexer);
21009 /* Turn the initializer into an initializer expansion. */
21010 initializer = make_pack_expansion (initializer);
21013 /* Add it to the vector. */
21014 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
21016 /* If the next token is not a comma, we have reached the end of
21017 the list. */
21018 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21019 break;
21021 /* Peek at the next token. */
21022 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21023 /* If the next token is a `}', then we're still done. An
21024 initializer-clause can have a trailing `,' after the
21025 initializer-list and before the closing `}'. */
21026 if (token->type == CPP_CLOSE_BRACE)
21027 break;
21029 /* Consume the `,' token. */
21030 cp_lexer_consume_token (parser->lexer);
21033 return v;
21036 /* Classes [gram.class] */
21038 /* Parse a class-name.
21040 class-name:
21041 identifier
21042 template-id
21044 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
21045 to indicate that names looked up in dependent types should be
21046 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
21047 keyword has been used to indicate that the name that appears next
21048 is a template. TAG_TYPE indicates the explicit tag given before
21049 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
21050 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
21051 is the class being defined in a class-head. If ENUM_OK is TRUE,
21052 enum-names are also accepted.
21054 Returns the TYPE_DECL representing the class. */
21056 static tree
21057 cp_parser_class_name (cp_parser *parser,
21058 bool typename_keyword_p,
21059 bool template_keyword_p,
21060 enum tag_types tag_type,
21061 bool check_dependency_p,
21062 bool class_head_p,
21063 bool is_declaration,
21064 bool enum_ok)
21066 tree decl;
21067 tree scope;
21068 bool typename_p;
21069 cp_token *token;
21070 tree identifier = NULL_TREE;
21072 /* All class-names start with an identifier. */
21073 token = cp_lexer_peek_token (parser->lexer);
21074 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
21076 cp_parser_error (parser, "expected class-name");
21077 return error_mark_node;
21080 /* PARSER->SCOPE can be cleared when parsing the template-arguments
21081 to a template-id, so we save it here. */
21082 scope = parser->scope;
21083 if (scope == error_mark_node)
21084 return error_mark_node;
21086 /* Any name names a type if we're following the `typename' keyword
21087 in a qualified name where the enclosing scope is type-dependent. */
21088 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
21089 && dependent_type_p (scope));
21090 /* Handle the common case (an identifier, but not a template-id)
21091 efficiently. */
21092 if (token->type == CPP_NAME
21093 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
21095 cp_token *identifier_token;
21096 bool ambiguous_p;
21098 /* Look for the identifier. */
21099 identifier_token = cp_lexer_peek_token (parser->lexer);
21100 ambiguous_p = identifier_token->error_reported;
21101 identifier = cp_parser_identifier (parser);
21102 /* If the next token isn't an identifier, we are certainly not
21103 looking at a class-name. */
21104 if (identifier == error_mark_node)
21105 decl = error_mark_node;
21106 /* If we know this is a type-name, there's no need to look it
21107 up. */
21108 else if (typename_p)
21109 decl = identifier;
21110 else
21112 tree ambiguous_decls;
21113 /* If we already know that this lookup is ambiguous, then
21114 we've already issued an error message; there's no reason
21115 to check again. */
21116 if (ambiguous_p)
21118 cp_parser_simulate_error (parser);
21119 return error_mark_node;
21121 /* If the next token is a `::', then the name must be a type
21122 name.
21124 [basic.lookup.qual]
21126 During the lookup for a name preceding the :: scope
21127 resolution operator, object, function, and enumerator
21128 names are ignored. */
21129 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21130 tag_type = typename_type;
21131 /* Look up the name. */
21132 decl = cp_parser_lookup_name (parser, identifier,
21133 tag_type,
21134 /*is_template=*/false,
21135 /*is_namespace=*/false,
21136 check_dependency_p,
21137 &ambiguous_decls,
21138 identifier_token->location);
21139 if (ambiguous_decls)
21141 if (cp_parser_parsing_tentatively (parser))
21142 cp_parser_simulate_error (parser);
21143 return error_mark_node;
21147 else
21149 /* Try a template-id. */
21150 decl = cp_parser_template_id (parser, template_keyword_p,
21151 check_dependency_p,
21152 tag_type,
21153 is_declaration);
21154 if (decl == error_mark_node)
21155 return error_mark_node;
21158 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
21160 /* If this is a typename, create a TYPENAME_TYPE. */
21161 if (typename_p && decl != error_mark_node)
21163 decl = make_typename_type (scope, decl, typename_type,
21164 /*complain=*/tf_error);
21165 if (decl != error_mark_node)
21166 decl = TYPE_NAME (decl);
21169 decl = strip_using_decl (decl);
21171 /* Check to see that it is really the name of a class. */
21172 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
21173 && identifier_p (TREE_OPERAND (decl, 0))
21174 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21175 /* Situations like this:
21177 template <typename T> struct A {
21178 typename T::template X<int>::I i;
21181 are problematic. Is `T::template X<int>' a class-name? The
21182 standard does not seem to be definitive, but there is no other
21183 valid interpretation of the following `::'. Therefore, those
21184 names are considered class-names. */
21186 decl = make_typename_type (scope, decl, tag_type, tf_error);
21187 if (decl != error_mark_node)
21188 decl = TYPE_NAME (decl);
21190 else if (TREE_CODE (decl) != TYPE_DECL
21191 || TREE_TYPE (decl) == error_mark_node
21192 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
21193 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
21194 /* In Objective-C 2.0, a classname followed by '.' starts a
21195 dot-syntax expression, and it's not a type-name. */
21196 || (c_dialect_objc ()
21197 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
21198 && objc_is_class_name (decl)))
21199 decl = error_mark_node;
21201 if (decl == error_mark_node)
21202 cp_parser_error (parser, "expected class-name");
21203 else if (identifier && !parser->scope)
21204 maybe_note_name_used_in_class (identifier, decl);
21206 return decl;
21209 /* Parse a class-specifier.
21211 class-specifier:
21212 class-head { member-specification [opt] }
21214 Returns the TREE_TYPE representing the class. */
21216 static tree
21217 cp_parser_class_specifier_1 (cp_parser* parser)
21219 tree type;
21220 tree attributes = NULL_TREE;
21221 bool nested_name_specifier_p;
21222 unsigned saved_num_template_parameter_lists;
21223 bool saved_in_function_body;
21224 unsigned char in_statement;
21225 bool in_switch_statement_p;
21226 bool saved_in_unbraced_linkage_specification_p;
21227 tree old_scope = NULL_TREE;
21228 tree scope = NULL_TREE;
21229 cp_token *closing_brace;
21231 push_deferring_access_checks (dk_no_deferred);
21233 /* Parse the class-head. */
21234 type = cp_parser_class_head (parser,
21235 &nested_name_specifier_p);
21236 /* If the class-head was a semantic disaster, skip the entire body
21237 of the class. */
21238 if (!type)
21240 cp_parser_skip_to_end_of_block_or_statement (parser);
21241 pop_deferring_access_checks ();
21242 return error_mark_node;
21245 /* Look for the `{'. */
21246 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
21248 pop_deferring_access_checks ();
21249 return error_mark_node;
21252 cp_ensure_no_omp_declare_simd (parser);
21253 cp_ensure_no_oacc_routine (parser);
21255 /* Issue an error message if type-definitions are forbidden here. */
21256 cp_parser_check_type_definition (parser);
21257 /* Remember that we are defining one more class. */
21258 ++parser->num_classes_being_defined;
21259 /* Inside the class, surrounding template-parameter-lists do not
21260 apply. */
21261 saved_num_template_parameter_lists
21262 = parser->num_template_parameter_lists;
21263 parser->num_template_parameter_lists = 0;
21264 /* We are not in a function body. */
21265 saved_in_function_body = parser->in_function_body;
21266 parser->in_function_body = false;
21267 /* Or in a loop. */
21268 in_statement = parser->in_statement;
21269 parser->in_statement = 0;
21270 /* Or in a switch. */
21271 in_switch_statement_p = parser->in_switch_statement_p;
21272 parser->in_switch_statement_p = false;
21273 /* We are not immediately inside an extern "lang" block. */
21274 saved_in_unbraced_linkage_specification_p
21275 = parser->in_unbraced_linkage_specification_p;
21276 parser->in_unbraced_linkage_specification_p = false;
21278 // Associate constraints with the type.
21279 if (flag_concepts)
21280 type = associate_classtype_constraints (type);
21282 /* Start the class. */
21283 if (nested_name_specifier_p)
21285 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
21286 old_scope = push_inner_scope (scope);
21288 type = begin_class_definition (type);
21290 if (type == error_mark_node)
21291 /* If the type is erroneous, skip the entire body of the class. */
21292 cp_parser_skip_to_closing_brace (parser);
21293 else
21294 /* Parse the member-specification. */
21295 cp_parser_member_specification_opt (parser);
21297 /* Look for the trailing `}'. */
21298 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21299 /* Look for trailing attributes to apply to this class. */
21300 if (cp_parser_allow_gnu_extensions_p (parser))
21301 attributes = cp_parser_gnu_attributes_opt (parser);
21302 if (type != error_mark_node)
21303 type = finish_struct (type, attributes);
21304 if (nested_name_specifier_p)
21305 pop_inner_scope (old_scope, scope);
21307 /* We've finished a type definition. Check for the common syntax
21308 error of forgetting a semicolon after the definition. We need to
21309 be careful, as we can't just check for not-a-semicolon and be done
21310 with it; the user might have typed:
21312 class X { } c = ...;
21313 class X { } *p = ...;
21315 and so forth. Instead, enumerate all the possible tokens that
21316 might follow this production; if we don't see one of them, then
21317 complain and silently insert the semicolon. */
21319 cp_token *token = cp_lexer_peek_token (parser->lexer);
21320 bool want_semicolon = true;
21322 if (cp_next_tokens_can_be_std_attribute_p (parser))
21323 /* Don't try to parse c++11 attributes here. As per the
21324 grammar, that should be a task for
21325 cp_parser_decl_specifier_seq. */
21326 want_semicolon = false;
21328 switch (token->type)
21330 case CPP_NAME:
21331 case CPP_SEMICOLON:
21332 case CPP_MULT:
21333 case CPP_AND:
21334 case CPP_OPEN_PAREN:
21335 case CPP_CLOSE_PAREN:
21336 case CPP_COMMA:
21337 want_semicolon = false;
21338 break;
21340 /* While it's legal for type qualifiers and storage class
21341 specifiers to follow type definitions in the grammar, only
21342 compiler testsuites contain code like that. Assume that if
21343 we see such code, then what we're really seeing is a case
21344 like:
21346 class X { }
21347 const <type> var = ...;
21351 class Y { }
21352 static <type> func (...) ...
21354 i.e. the qualifier or specifier applies to the next
21355 declaration. To do so, however, we need to look ahead one
21356 more token to see if *that* token is a type specifier.
21358 This code could be improved to handle:
21360 class Z { }
21361 static const <type> var = ...; */
21362 case CPP_KEYWORD:
21363 if (keyword_is_decl_specifier (token->keyword))
21365 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
21367 /* Handling user-defined types here would be nice, but very
21368 tricky. */
21369 want_semicolon
21370 = (lookahead->type == CPP_KEYWORD
21371 && keyword_begins_type_specifier (lookahead->keyword));
21373 break;
21374 default:
21375 break;
21378 /* If we don't have a type, then something is very wrong and we
21379 shouldn't try to do anything clever. Likewise for not seeing the
21380 closing brace. */
21381 if (closing_brace && TYPE_P (type) && want_semicolon)
21383 cp_token_position prev
21384 = cp_lexer_previous_token_position (parser->lexer);
21385 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
21386 location_t loc = prev_token->location;
21388 if (CLASSTYPE_DECLARED_CLASS (type))
21389 error_at (loc, "expected %<;%> after class definition");
21390 else if (TREE_CODE (type) == RECORD_TYPE)
21391 error_at (loc, "expected %<;%> after struct definition");
21392 else if (TREE_CODE (type) == UNION_TYPE)
21393 error_at (loc, "expected %<;%> after union definition");
21394 else
21395 gcc_unreachable ();
21397 /* Unget one token and smash it to look as though we encountered
21398 a semicolon in the input stream. */
21399 cp_lexer_set_token_position (parser->lexer, prev);
21400 token = cp_lexer_peek_token (parser->lexer);
21401 token->type = CPP_SEMICOLON;
21402 token->keyword = RID_MAX;
21406 /* If this class is not itself within the scope of another class,
21407 then we need to parse the bodies of all of the queued function
21408 definitions. Note that the queued functions defined in a class
21409 are not always processed immediately following the
21410 class-specifier for that class. Consider:
21412 struct A {
21413 struct B { void f() { sizeof (A); } };
21416 If `f' were processed before the processing of `A' were
21417 completed, there would be no way to compute the size of `A'.
21418 Note that the nesting we are interested in here is lexical --
21419 not the semantic nesting given by TYPE_CONTEXT. In particular,
21420 for:
21422 struct A { struct B; };
21423 struct A::B { void f() { } };
21425 there is no need to delay the parsing of `A::B::f'. */
21426 if (--parser->num_classes_being_defined == 0)
21428 tree decl;
21429 tree class_type = NULL_TREE;
21430 tree pushed_scope = NULL_TREE;
21431 unsigned ix;
21432 cp_default_arg_entry *e;
21433 tree save_ccp, save_ccr;
21435 /* In a first pass, parse default arguments to the functions.
21436 Then, in a second pass, parse the bodies of the functions.
21437 This two-phased approach handles cases like:
21439 struct S {
21440 void f() { g(); }
21441 void g(int i = 3);
21445 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
21447 decl = e->decl;
21448 /* If there are default arguments that have not yet been processed,
21449 take care of them now. */
21450 if (class_type != e->class_type)
21452 if (pushed_scope)
21453 pop_scope (pushed_scope);
21454 class_type = e->class_type;
21455 pushed_scope = push_scope (class_type);
21457 /* Make sure that any template parameters are in scope. */
21458 maybe_begin_member_template_processing (decl);
21459 /* Parse the default argument expressions. */
21460 cp_parser_late_parsing_default_args (parser, decl);
21461 /* Remove any template parameters from the symbol table. */
21462 maybe_end_member_template_processing ();
21464 vec_safe_truncate (unparsed_funs_with_default_args, 0);
21465 /* Now parse any NSDMIs. */
21466 save_ccp = current_class_ptr;
21467 save_ccr = current_class_ref;
21468 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
21470 if (class_type != DECL_CONTEXT (decl))
21472 if (pushed_scope)
21473 pop_scope (pushed_scope);
21474 class_type = DECL_CONTEXT (decl);
21475 pushed_scope = push_scope (class_type);
21477 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
21478 cp_parser_late_parsing_nsdmi (parser, decl);
21480 vec_safe_truncate (unparsed_nsdmis, 0);
21481 current_class_ptr = save_ccp;
21482 current_class_ref = save_ccr;
21483 if (pushed_scope)
21484 pop_scope (pushed_scope);
21486 /* Now do some post-NSDMI bookkeeping. */
21487 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
21488 after_nsdmi_defaulted_late_checks (class_type);
21489 vec_safe_truncate (unparsed_classes, 0);
21490 after_nsdmi_defaulted_late_checks (type);
21492 /* Now parse the body of the functions. */
21493 if (flag_openmp)
21495 /* OpenMP UDRs need to be parsed before all other functions. */
21496 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21497 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
21498 cp_parser_late_parsing_for_member (parser, decl);
21499 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21500 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
21501 cp_parser_late_parsing_for_member (parser, decl);
21503 else
21504 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21505 cp_parser_late_parsing_for_member (parser, decl);
21506 vec_safe_truncate (unparsed_funs_with_definitions, 0);
21508 else
21509 vec_safe_push (unparsed_classes, type);
21511 /* Put back any saved access checks. */
21512 pop_deferring_access_checks ();
21514 /* Restore saved state. */
21515 parser->in_switch_statement_p = in_switch_statement_p;
21516 parser->in_statement = in_statement;
21517 parser->in_function_body = saved_in_function_body;
21518 parser->num_template_parameter_lists
21519 = saved_num_template_parameter_lists;
21520 parser->in_unbraced_linkage_specification_p
21521 = saved_in_unbraced_linkage_specification_p;
21523 return type;
21526 static tree
21527 cp_parser_class_specifier (cp_parser* parser)
21529 tree ret;
21530 timevar_push (TV_PARSE_STRUCT);
21531 ret = cp_parser_class_specifier_1 (parser);
21532 timevar_pop (TV_PARSE_STRUCT);
21533 return ret;
21536 /* Parse a class-head.
21538 class-head:
21539 class-key identifier [opt] base-clause [opt]
21540 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
21541 class-key nested-name-specifier [opt] template-id
21542 base-clause [opt]
21544 class-virt-specifier:
21545 final
21547 GNU Extensions:
21548 class-key attributes identifier [opt] base-clause [opt]
21549 class-key attributes nested-name-specifier identifier base-clause [opt]
21550 class-key attributes nested-name-specifier [opt] template-id
21551 base-clause [opt]
21553 Upon return BASES is initialized to the list of base classes (or
21554 NULL, if there are none) in the same form returned by
21555 cp_parser_base_clause.
21557 Returns the TYPE of the indicated class. Sets
21558 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
21559 involving a nested-name-specifier was used, and FALSE otherwise.
21561 Returns error_mark_node if this is not a class-head.
21563 Returns NULL_TREE if the class-head is syntactically valid, but
21564 semantically invalid in a way that means we should skip the entire
21565 body of the class. */
21567 static tree
21568 cp_parser_class_head (cp_parser* parser,
21569 bool* nested_name_specifier_p)
21571 tree nested_name_specifier;
21572 enum tag_types class_key;
21573 tree id = NULL_TREE;
21574 tree type = NULL_TREE;
21575 tree attributes;
21576 tree bases;
21577 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21578 bool template_id_p = false;
21579 bool qualified_p = false;
21580 bool invalid_nested_name_p = false;
21581 bool invalid_explicit_specialization_p = false;
21582 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
21583 tree pushed_scope = NULL_TREE;
21584 unsigned num_templates;
21585 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
21586 /* Assume no nested-name-specifier will be present. */
21587 *nested_name_specifier_p = false;
21588 /* Assume no template parameter lists will be used in defining the
21589 type. */
21590 num_templates = 0;
21591 parser->colon_corrects_to_scope_p = false;
21593 /* Look for the class-key. */
21594 class_key = cp_parser_class_key (parser);
21595 if (class_key == none_type)
21596 return error_mark_node;
21598 /* Parse the attributes. */
21599 attributes = cp_parser_attributes_opt (parser);
21601 /* If the next token is `::', that is invalid -- but sometimes
21602 people do try to write:
21604 struct ::S {};
21606 Handle this gracefully by accepting the extra qualifier, and then
21607 issuing an error about it later if this really is a
21608 class-head. If it turns out just to be an elaborated type
21609 specifier, remain silent. */
21610 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
21611 qualified_p = true;
21613 push_deferring_access_checks (dk_no_check);
21615 /* Determine the name of the class. Begin by looking for an
21616 optional nested-name-specifier. */
21617 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
21618 nested_name_specifier
21619 = cp_parser_nested_name_specifier_opt (parser,
21620 /*typename_keyword_p=*/false,
21621 /*check_dependency_p=*/false,
21622 /*type_p=*/true,
21623 /*is_declaration=*/false);
21624 /* If there was a nested-name-specifier, then there *must* be an
21625 identifier. */
21626 if (nested_name_specifier)
21628 type_start_token = cp_lexer_peek_token (parser->lexer);
21629 /* Although the grammar says `identifier', it really means
21630 `class-name' or `template-name'. You are only allowed to
21631 define a class that has already been declared with this
21632 syntax.
21634 The proposed resolution for Core Issue 180 says that wherever
21635 you see `class T::X' you should treat `X' as a type-name.
21637 It is OK to define an inaccessible class; for example:
21639 class A { class B; };
21640 class A::B {};
21642 We do not know if we will see a class-name, or a
21643 template-name. We look for a class-name first, in case the
21644 class-name is a template-id; if we looked for the
21645 template-name first we would stop after the template-name. */
21646 cp_parser_parse_tentatively (parser);
21647 type = cp_parser_class_name (parser,
21648 /*typename_keyword_p=*/false,
21649 /*template_keyword_p=*/false,
21650 class_type,
21651 /*check_dependency_p=*/false,
21652 /*class_head_p=*/true,
21653 /*is_declaration=*/false);
21654 /* If that didn't work, ignore the nested-name-specifier. */
21655 if (!cp_parser_parse_definitely (parser))
21657 invalid_nested_name_p = true;
21658 type_start_token = cp_lexer_peek_token (parser->lexer);
21659 id = cp_parser_identifier (parser);
21660 if (id == error_mark_node)
21661 id = NULL_TREE;
21663 /* If we could not find a corresponding TYPE, treat this
21664 declaration like an unqualified declaration. */
21665 if (type == error_mark_node)
21666 nested_name_specifier = NULL_TREE;
21667 /* Otherwise, count the number of templates used in TYPE and its
21668 containing scopes. */
21669 else
21671 tree scope;
21673 for (scope = TREE_TYPE (type);
21674 scope && TREE_CODE (scope) != NAMESPACE_DECL;
21675 scope = get_containing_scope (scope))
21676 if (TYPE_P (scope)
21677 && CLASS_TYPE_P (scope)
21678 && CLASSTYPE_TEMPLATE_INFO (scope)
21679 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
21680 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
21681 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
21682 ++num_templates;
21685 /* Otherwise, the identifier is optional. */
21686 else
21688 /* We don't know whether what comes next is a template-id,
21689 an identifier, or nothing at all. */
21690 cp_parser_parse_tentatively (parser);
21691 /* Check for a template-id. */
21692 type_start_token = cp_lexer_peek_token (parser->lexer);
21693 id = cp_parser_template_id (parser,
21694 /*template_keyword_p=*/false,
21695 /*check_dependency_p=*/true,
21696 class_key,
21697 /*is_declaration=*/true);
21698 /* If that didn't work, it could still be an identifier. */
21699 if (!cp_parser_parse_definitely (parser))
21701 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21703 type_start_token = cp_lexer_peek_token (parser->lexer);
21704 id = cp_parser_identifier (parser);
21706 else
21707 id = NULL_TREE;
21709 else
21711 template_id_p = true;
21712 ++num_templates;
21716 pop_deferring_access_checks ();
21718 if (id)
21720 cp_parser_check_for_invalid_template_id (parser, id,
21721 class_key,
21722 type_start_token->location);
21724 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
21726 /* If it's not a `:' or a `{' then we can't really be looking at a
21727 class-head, since a class-head only appears as part of a
21728 class-specifier. We have to detect this situation before calling
21729 xref_tag, since that has irreversible side-effects. */
21730 if (!cp_parser_next_token_starts_class_definition_p (parser))
21732 cp_parser_error (parser, "expected %<{%> or %<:%>");
21733 type = error_mark_node;
21734 goto out;
21737 /* At this point, we're going ahead with the class-specifier, even
21738 if some other problem occurs. */
21739 cp_parser_commit_to_tentative_parse (parser);
21740 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
21742 cp_parser_error (parser,
21743 "cannot specify %<override%> for a class");
21744 type = error_mark_node;
21745 goto out;
21747 /* Issue the error about the overly-qualified name now. */
21748 if (qualified_p)
21750 cp_parser_error (parser,
21751 "global qualification of class name is invalid");
21752 type = error_mark_node;
21753 goto out;
21755 else if (invalid_nested_name_p)
21757 cp_parser_error (parser,
21758 "qualified name does not name a class");
21759 type = error_mark_node;
21760 goto out;
21762 else if (nested_name_specifier)
21764 tree scope;
21766 /* Reject typedef-names in class heads. */
21767 if (!DECL_IMPLICIT_TYPEDEF_P (type))
21769 error_at (type_start_token->location,
21770 "invalid class name in declaration of %qD",
21771 type);
21772 type = NULL_TREE;
21773 goto done;
21776 /* Figure out in what scope the declaration is being placed. */
21777 scope = current_scope ();
21778 /* If that scope does not contain the scope in which the
21779 class was originally declared, the program is invalid. */
21780 if (scope && !is_ancestor (scope, nested_name_specifier))
21782 if (at_namespace_scope_p ())
21783 error_at (type_start_token->location,
21784 "declaration of %qD in namespace %qD which does not "
21785 "enclose %qD",
21786 type, scope, nested_name_specifier);
21787 else
21788 error_at (type_start_token->location,
21789 "declaration of %qD in %qD which does not enclose %qD",
21790 type, scope, nested_name_specifier);
21791 type = NULL_TREE;
21792 goto done;
21794 /* [dcl.meaning]
21796 A declarator-id shall not be qualified except for the
21797 definition of a ... nested class outside of its class
21798 ... [or] the definition or explicit instantiation of a
21799 class member of a namespace outside of its namespace. */
21800 if (scope == nested_name_specifier)
21802 permerror (nested_name_specifier_token_start->location,
21803 "extra qualification not allowed");
21804 nested_name_specifier = NULL_TREE;
21805 num_templates = 0;
21808 /* An explicit-specialization must be preceded by "template <>". If
21809 it is not, try to recover gracefully. */
21810 if (at_namespace_scope_p ()
21811 && parser->num_template_parameter_lists == 0
21812 && template_id_p)
21814 error_at (type_start_token->location,
21815 "an explicit specialization must be preceded by %<template <>%>");
21816 invalid_explicit_specialization_p = true;
21817 /* Take the same action that would have been taken by
21818 cp_parser_explicit_specialization. */
21819 ++parser->num_template_parameter_lists;
21820 begin_specialization ();
21822 /* There must be no "return" statements between this point and the
21823 end of this function; set "type "to the correct return value and
21824 use "goto done;" to return. */
21825 /* Make sure that the right number of template parameters were
21826 present. */
21827 if (!cp_parser_check_template_parameters (parser, num_templates,
21828 type_start_token->location,
21829 /*declarator=*/NULL))
21831 /* If something went wrong, there is no point in even trying to
21832 process the class-definition. */
21833 type = NULL_TREE;
21834 goto done;
21837 /* Look up the type. */
21838 if (template_id_p)
21840 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
21841 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
21842 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
21844 error_at (type_start_token->location,
21845 "function template %qD redeclared as a class template", id);
21846 type = error_mark_node;
21848 else
21850 type = TREE_TYPE (id);
21851 type = maybe_process_partial_specialization (type);
21853 if (nested_name_specifier)
21854 pushed_scope = push_scope (nested_name_specifier);
21856 else if (nested_name_specifier)
21858 tree class_type;
21860 /* Given:
21862 template <typename T> struct S { struct T };
21863 template <typename T> struct S<T>::T { };
21865 we will get a TYPENAME_TYPE when processing the definition of
21866 `S::T'. We need to resolve it to the actual type before we
21867 try to define it. */
21868 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
21870 class_type = resolve_typename_type (TREE_TYPE (type),
21871 /*only_current_p=*/false);
21872 if (TREE_CODE (class_type) != TYPENAME_TYPE)
21873 type = TYPE_NAME (class_type);
21874 else
21876 cp_parser_error (parser, "could not resolve typename type");
21877 type = error_mark_node;
21881 if (maybe_process_partial_specialization (TREE_TYPE (type))
21882 == error_mark_node)
21884 type = NULL_TREE;
21885 goto done;
21888 class_type = current_class_type;
21889 /* Enter the scope indicated by the nested-name-specifier. */
21890 pushed_scope = push_scope (nested_name_specifier);
21891 /* Get the canonical version of this type. */
21892 type = TYPE_MAIN_DECL (TREE_TYPE (type));
21893 /* Call push_template_decl if it seems like we should be defining a
21894 template either from the template headers or the type we're
21895 defining, so that we diagnose both extra and missing headers. */
21896 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
21897 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
21898 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
21900 type = push_template_decl (type);
21901 if (type == error_mark_node)
21903 type = NULL_TREE;
21904 goto done;
21908 type = TREE_TYPE (type);
21909 *nested_name_specifier_p = true;
21911 else /* The name is not a nested name. */
21913 /* If the class was unnamed, create a dummy name. */
21914 if (!id)
21915 id = make_anon_name ();
21916 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
21917 parser->num_template_parameter_lists);
21920 /* Indicate whether this class was declared as a `class' or as a
21921 `struct'. */
21922 if (TREE_CODE (type) == RECORD_TYPE)
21923 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
21924 cp_parser_check_class_key (class_key, type);
21926 /* If this type was already complete, and we see another definition,
21927 that's an error. */
21928 if (type != error_mark_node && COMPLETE_TYPE_P (type))
21930 error_at (type_start_token->location, "redefinition of %q#T",
21931 type);
21932 error_at (type_start_token->location, "previous definition of %q+#T",
21933 type);
21934 type = NULL_TREE;
21935 goto done;
21937 else if (type == error_mark_node)
21938 type = NULL_TREE;
21940 if (type)
21942 /* Apply attributes now, before any use of the class as a template
21943 argument in its base list. */
21944 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
21945 fixup_attribute_variants (type);
21948 /* We will have entered the scope containing the class; the names of
21949 base classes should be looked up in that context. For example:
21951 struct A { struct B {}; struct C; };
21952 struct A::C : B {};
21954 is valid. */
21956 /* Get the list of base-classes, if there is one. */
21957 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21959 /* PR59482: enter the class scope so that base-specifiers are looked
21960 up correctly. */
21961 if (type)
21962 pushclass (type);
21963 bases = cp_parser_base_clause (parser);
21964 /* PR59482: get out of the previously pushed class scope so that the
21965 subsequent pops pop the right thing. */
21966 if (type)
21967 popclass ();
21969 else
21970 bases = NULL_TREE;
21972 /* If we're really defining a class, process the base classes.
21973 If they're invalid, fail. */
21974 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
21975 && !xref_basetypes (type, bases))
21976 type = NULL_TREE;
21978 done:
21979 /* Leave the scope given by the nested-name-specifier. We will
21980 enter the class scope itself while processing the members. */
21981 if (pushed_scope)
21982 pop_scope (pushed_scope);
21984 if (invalid_explicit_specialization_p)
21986 end_specialization ();
21987 --parser->num_template_parameter_lists;
21990 if (type)
21991 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
21992 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
21993 CLASSTYPE_FINAL (type) = 1;
21994 out:
21995 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
21996 return type;
21999 /* Parse a class-key.
22001 class-key:
22002 class
22003 struct
22004 union
22006 Returns the kind of class-key specified, or none_type to indicate
22007 error. */
22009 static enum tag_types
22010 cp_parser_class_key (cp_parser* parser)
22012 cp_token *token;
22013 enum tag_types tag_type;
22015 /* Look for the class-key. */
22016 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
22017 if (!token)
22018 return none_type;
22020 /* Check to see if the TOKEN is a class-key. */
22021 tag_type = cp_parser_token_is_class_key (token);
22022 if (!tag_type)
22023 cp_parser_error (parser, "expected class-key");
22024 return tag_type;
22027 /* Parse a type-parameter-key.
22029 type-parameter-key:
22030 class
22031 typename
22034 static void
22035 cp_parser_type_parameter_key (cp_parser* parser)
22037 /* Look for the type-parameter-key. */
22038 enum tag_types tag_type = none_type;
22039 cp_token *token = cp_lexer_peek_token (parser->lexer);
22040 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
22042 cp_lexer_consume_token (parser->lexer);
22043 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
22044 /* typename is not allowed in a template template parameter
22045 by the standard until C++1Z. */
22046 pedwarn (token->location, OPT_Wpedantic,
22047 "ISO C++ forbids typename key in template template parameter;"
22048 " use -std=c++1z or -std=gnu++1z");
22050 else
22051 cp_parser_error (parser, "expected %<class%> or %<typename%>");
22053 return;
22056 /* Parse an (optional) member-specification.
22058 member-specification:
22059 member-declaration member-specification [opt]
22060 access-specifier : member-specification [opt] */
22062 static void
22063 cp_parser_member_specification_opt (cp_parser* parser)
22065 while (true)
22067 cp_token *token;
22068 enum rid keyword;
22070 /* Peek at the next token. */
22071 token = cp_lexer_peek_token (parser->lexer);
22072 /* If it's a `}', or EOF then we've seen all the members. */
22073 if (token->type == CPP_CLOSE_BRACE
22074 || token->type == CPP_EOF
22075 || token->type == CPP_PRAGMA_EOL)
22076 break;
22078 /* See if this token is a keyword. */
22079 keyword = token->keyword;
22080 switch (keyword)
22082 case RID_PUBLIC:
22083 case RID_PROTECTED:
22084 case RID_PRIVATE:
22085 /* Consume the access-specifier. */
22086 cp_lexer_consume_token (parser->lexer);
22087 /* Remember which access-specifier is active. */
22088 current_access_specifier = token->u.value;
22089 /* Look for the `:'. */
22090 cp_parser_require (parser, CPP_COLON, RT_COLON);
22091 break;
22093 default:
22094 /* Accept #pragmas at class scope. */
22095 if (token->type == CPP_PRAGMA)
22097 cp_parser_pragma (parser, pragma_member);
22098 break;
22101 /* Otherwise, the next construction must be a
22102 member-declaration. */
22103 cp_parser_member_declaration (parser);
22108 /* Parse a member-declaration.
22110 member-declaration:
22111 decl-specifier-seq [opt] member-declarator-list [opt] ;
22112 function-definition ; [opt]
22113 :: [opt] nested-name-specifier template [opt] unqualified-id ;
22114 using-declaration
22115 template-declaration
22116 alias-declaration
22118 member-declarator-list:
22119 member-declarator
22120 member-declarator-list , member-declarator
22122 member-declarator:
22123 declarator pure-specifier [opt]
22124 declarator constant-initializer [opt]
22125 identifier [opt] : constant-expression
22127 GNU Extensions:
22129 member-declaration:
22130 __extension__ member-declaration
22132 member-declarator:
22133 declarator attributes [opt] pure-specifier [opt]
22134 declarator attributes [opt] constant-initializer [opt]
22135 identifier [opt] attributes [opt] : constant-expression
22137 C++0x Extensions:
22139 member-declaration:
22140 static_assert-declaration */
22142 static void
22143 cp_parser_member_declaration (cp_parser* parser)
22145 cp_decl_specifier_seq decl_specifiers;
22146 tree prefix_attributes;
22147 tree decl;
22148 int declares_class_or_enum;
22149 bool friend_p;
22150 cp_token *token = NULL;
22151 cp_token *decl_spec_token_start = NULL;
22152 cp_token *initializer_token_start = NULL;
22153 int saved_pedantic;
22154 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22156 /* Check for the `__extension__' keyword. */
22157 if (cp_parser_extension_opt (parser, &saved_pedantic))
22159 /* Recurse. */
22160 cp_parser_member_declaration (parser);
22161 /* Restore the old value of the PEDANTIC flag. */
22162 pedantic = saved_pedantic;
22164 return;
22167 /* Check for a template-declaration. */
22168 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22170 /* An explicit specialization here is an error condition, and we
22171 expect the specialization handler to detect and report this. */
22172 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
22173 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
22174 cp_parser_explicit_specialization (parser);
22175 else
22176 cp_parser_template_declaration (parser, /*member_p=*/true);
22178 return;
22180 /* Check for a template introduction. */
22181 else if (cp_parser_template_declaration_after_export (parser, true))
22182 return;
22184 /* Check for a using-declaration. */
22185 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
22187 if (cxx_dialect < cxx11)
22189 /* Parse the using-declaration. */
22190 cp_parser_using_declaration (parser,
22191 /*access_declaration_p=*/false);
22192 return;
22194 else
22196 tree decl;
22197 bool alias_decl_expected;
22198 cp_parser_parse_tentatively (parser);
22199 decl = cp_parser_alias_declaration (parser);
22200 /* Note that if we actually see the '=' token after the
22201 identifier, cp_parser_alias_declaration commits the
22202 tentative parse. In that case, we really expect an
22203 alias-declaration. Otherwise, we expect a using
22204 declaration. */
22205 alias_decl_expected =
22206 !cp_parser_uncommitted_to_tentative_parse_p (parser);
22207 cp_parser_parse_definitely (parser);
22209 if (alias_decl_expected)
22210 finish_member_declaration (decl);
22211 else
22212 cp_parser_using_declaration (parser,
22213 /*access_declaration_p=*/false);
22214 return;
22218 /* Check for @defs. */
22219 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
22221 tree ivar, member;
22222 tree ivar_chains = cp_parser_objc_defs_expression (parser);
22223 ivar = ivar_chains;
22224 while (ivar)
22226 member = ivar;
22227 ivar = TREE_CHAIN (member);
22228 TREE_CHAIN (member) = NULL_TREE;
22229 finish_member_declaration (member);
22231 return;
22234 /* If the next token is `static_assert' we have a static assertion. */
22235 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
22237 cp_parser_static_assert (parser, /*member_p=*/true);
22238 return;
22241 parser->colon_corrects_to_scope_p = false;
22243 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
22244 goto out;
22246 /* Parse the decl-specifier-seq. */
22247 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22248 cp_parser_decl_specifier_seq (parser,
22249 CP_PARSER_FLAGS_OPTIONAL,
22250 &decl_specifiers,
22251 &declares_class_or_enum);
22252 /* Check for an invalid type-name. */
22253 if (!decl_specifiers.any_type_specifiers_p
22254 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22255 goto out;
22256 /* If there is no declarator, then the decl-specifier-seq should
22257 specify a type. */
22258 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22260 /* If there was no decl-specifier-seq, and the next token is a
22261 `;', then we have something like:
22263 struct S { ; };
22265 [class.mem]
22267 Each member-declaration shall declare at least one member
22268 name of the class. */
22269 if (!decl_specifiers.any_specifiers_p)
22271 cp_token *token = cp_lexer_peek_token (parser->lexer);
22272 if (!in_system_header_at (token->location))
22273 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
22275 else
22277 tree type;
22279 /* See if this declaration is a friend. */
22280 friend_p = cp_parser_friend_p (&decl_specifiers);
22281 /* If there were decl-specifiers, check to see if there was
22282 a class-declaration. */
22283 type = check_tag_decl (&decl_specifiers,
22284 /*explicit_type_instantiation_p=*/false);
22285 /* Nested classes have already been added to the class, but
22286 a `friend' needs to be explicitly registered. */
22287 if (friend_p)
22289 /* If the `friend' keyword was present, the friend must
22290 be introduced with a class-key. */
22291 if (!declares_class_or_enum && cxx_dialect < cxx11)
22292 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
22293 "in C++03 a class-key must be used "
22294 "when declaring a friend");
22295 /* In this case:
22297 template <typename T> struct A {
22298 friend struct A<T>::B;
22301 A<T>::B will be represented by a TYPENAME_TYPE, and
22302 therefore not recognized by check_tag_decl. */
22303 if (!type)
22305 type = decl_specifiers.type;
22306 if (type && TREE_CODE (type) == TYPE_DECL)
22307 type = TREE_TYPE (type);
22309 if (!type || !TYPE_P (type))
22310 error_at (decl_spec_token_start->location,
22311 "friend declaration does not name a class or "
22312 "function");
22313 else
22314 make_friend_class (current_class_type, type,
22315 /*complain=*/true);
22317 /* If there is no TYPE, an error message will already have
22318 been issued. */
22319 else if (!type || type == error_mark_node)
22321 /* An anonymous aggregate has to be handled specially; such
22322 a declaration really declares a data member (with a
22323 particular type), as opposed to a nested class. */
22324 else if (ANON_AGGR_TYPE_P (type))
22326 /* C++11 9.5/6. */
22327 if (decl_specifiers.storage_class != sc_none)
22328 error_at (decl_spec_token_start->location,
22329 "a storage class on an anonymous aggregate "
22330 "in class scope is not allowed");
22332 /* Remove constructors and such from TYPE, now that we
22333 know it is an anonymous aggregate. */
22334 fixup_anonymous_aggr (type);
22335 /* And make the corresponding data member. */
22336 decl = build_decl (decl_spec_token_start->location,
22337 FIELD_DECL, NULL_TREE, type);
22338 /* Add it to the class. */
22339 finish_member_declaration (decl);
22341 else
22342 cp_parser_check_access_in_redeclaration
22343 (TYPE_NAME (type),
22344 decl_spec_token_start->location);
22347 else
22349 bool assume_semicolon = false;
22351 /* Clear attributes from the decl_specifiers but keep them
22352 around as prefix attributes that apply them to the entity
22353 being declared. */
22354 prefix_attributes = decl_specifiers.attributes;
22355 decl_specifiers.attributes = NULL_TREE;
22357 /* See if these declarations will be friends. */
22358 friend_p = cp_parser_friend_p (&decl_specifiers);
22360 /* Keep going until we hit the `;' at the end of the
22361 declaration. */
22362 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22364 tree attributes = NULL_TREE;
22365 tree first_attribute;
22367 /* Peek at the next token. */
22368 token = cp_lexer_peek_token (parser->lexer);
22370 /* Check for a bitfield declaration. */
22371 if (token->type == CPP_COLON
22372 || (token->type == CPP_NAME
22373 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
22374 == CPP_COLON))
22376 tree identifier;
22377 tree width;
22379 /* Get the name of the bitfield. Note that we cannot just
22380 check TOKEN here because it may have been invalidated by
22381 the call to cp_lexer_peek_nth_token above. */
22382 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
22383 identifier = cp_parser_identifier (parser);
22384 else
22385 identifier = NULL_TREE;
22387 /* Consume the `:' token. */
22388 cp_lexer_consume_token (parser->lexer);
22389 /* Get the width of the bitfield. */
22390 width
22391 = cp_parser_constant_expression (parser);
22393 /* Look for attributes that apply to the bitfield. */
22394 attributes = cp_parser_attributes_opt (parser);
22395 /* Remember which attributes are prefix attributes and
22396 which are not. */
22397 first_attribute = attributes;
22398 /* Combine the attributes. */
22399 attributes = chainon (prefix_attributes, attributes);
22401 /* Create the bitfield declaration. */
22402 decl = grokbitfield (identifier
22403 ? make_id_declarator (NULL_TREE,
22404 identifier,
22405 sfk_none)
22406 : NULL,
22407 &decl_specifiers,
22408 width,
22409 attributes);
22411 else
22413 cp_declarator *declarator;
22414 tree initializer;
22415 tree asm_specification;
22416 int ctor_dtor_or_conv_p;
22418 /* Parse the declarator. */
22419 declarator
22420 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22421 &ctor_dtor_or_conv_p,
22422 /*parenthesized_p=*/NULL,
22423 /*member_p=*/true,
22424 friend_p);
22426 /* If something went wrong parsing the declarator, make sure
22427 that we at least consume some tokens. */
22428 if (declarator == cp_error_declarator)
22430 /* Skip to the end of the statement. */
22431 cp_parser_skip_to_end_of_statement (parser);
22432 /* If the next token is not a semicolon, that is
22433 probably because we just skipped over the body of
22434 a function. So, we consume a semicolon if
22435 present, but do not issue an error message if it
22436 is not present. */
22437 if (cp_lexer_next_token_is (parser->lexer,
22438 CPP_SEMICOLON))
22439 cp_lexer_consume_token (parser->lexer);
22440 goto out;
22443 if (declares_class_or_enum & 2)
22444 cp_parser_check_for_definition_in_return_type
22445 (declarator, decl_specifiers.type,
22446 decl_specifiers.locations[ds_type_spec]);
22448 /* Look for an asm-specification. */
22449 asm_specification = cp_parser_asm_specification_opt (parser);
22450 /* Look for attributes that apply to the declaration. */
22451 attributes = cp_parser_attributes_opt (parser);
22452 /* Remember which attributes are prefix attributes and
22453 which are not. */
22454 first_attribute = attributes;
22455 /* Combine the attributes. */
22456 attributes = chainon (prefix_attributes, attributes);
22458 /* If it's an `=', then we have a constant-initializer or a
22459 pure-specifier. It is not correct to parse the
22460 initializer before registering the member declaration
22461 since the member declaration should be in scope while
22462 its initializer is processed. However, the rest of the
22463 front end does not yet provide an interface that allows
22464 us to handle this correctly. */
22465 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22467 /* In [class.mem]:
22469 A pure-specifier shall be used only in the declaration of
22470 a virtual function.
22472 A member-declarator can contain a constant-initializer
22473 only if it declares a static member of integral or
22474 enumeration type.
22476 Therefore, if the DECLARATOR is for a function, we look
22477 for a pure-specifier; otherwise, we look for a
22478 constant-initializer. When we call `grokfield', it will
22479 perform more stringent semantics checks. */
22480 initializer_token_start = cp_lexer_peek_token (parser->lexer);
22481 if (function_declarator_p (declarator)
22482 || (decl_specifiers.type
22483 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
22484 && declarator->kind == cdk_id
22485 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
22486 == FUNCTION_TYPE)))
22487 initializer = cp_parser_pure_specifier (parser);
22488 else if (decl_specifiers.storage_class != sc_static)
22489 initializer = cp_parser_save_nsdmi (parser);
22490 else if (cxx_dialect >= cxx11)
22492 bool nonconst;
22493 /* Don't require a constant rvalue in C++11, since we
22494 might want a reference constant. We'll enforce
22495 constancy later. */
22496 cp_lexer_consume_token (parser->lexer);
22497 /* Parse the initializer. */
22498 initializer = cp_parser_initializer_clause (parser,
22499 &nonconst);
22501 else
22502 /* Parse the initializer. */
22503 initializer = cp_parser_constant_initializer (parser);
22505 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
22506 && !function_declarator_p (declarator))
22508 bool x;
22509 if (decl_specifiers.storage_class != sc_static)
22510 initializer = cp_parser_save_nsdmi (parser);
22511 else
22512 initializer = cp_parser_initializer (parser, &x, &x);
22514 /* Otherwise, there is no initializer. */
22515 else
22516 initializer = NULL_TREE;
22518 /* See if we are probably looking at a function
22519 definition. We are certainly not looking at a
22520 member-declarator. Calling `grokfield' has
22521 side-effects, so we must not do it unless we are sure
22522 that we are looking at a member-declarator. */
22523 if (cp_parser_token_starts_function_definition_p
22524 (cp_lexer_peek_token (parser->lexer)))
22526 /* The grammar does not allow a pure-specifier to be
22527 used when a member function is defined. (It is
22528 possible that this fact is an oversight in the
22529 standard, since a pure function may be defined
22530 outside of the class-specifier. */
22531 if (initializer && initializer_token_start)
22532 error_at (initializer_token_start->location,
22533 "pure-specifier on function-definition");
22534 decl = cp_parser_save_member_function_body (parser,
22535 &decl_specifiers,
22536 declarator,
22537 attributes);
22538 if (parser->fully_implicit_function_template_p)
22539 decl = finish_fully_implicit_template (parser, decl);
22540 /* If the member was not a friend, declare it here. */
22541 if (!friend_p)
22542 finish_member_declaration (decl);
22543 /* Peek at the next token. */
22544 token = cp_lexer_peek_token (parser->lexer);
22545 /* If the next token is a semicolon, consume it. */
22546 if (token->type == CPP_SEMICOLON)
22547 cp_lexer_consume_token (parser->lexer);
22548 goto out;
22550 else
22551 if (declarator->kind == cdk_function)
22552 declarator->id_loc = token->location;
22553 /* Create the declaration. */
22554 decl = grokfield (declarator, &decl_specifiers,
22555 initializer, /*init_const_expr_p=*/true,
22556 asm_specification, attributes);
22557 if (parser->fully_implicit_function_template_p)
22559 if (friend_p)
22560 finish_fully_implicit_template (parser, 0);
22561 else
22562 decl = finish_fully_implicit_template (parser, decl);
22566 cp_finalize_omp_declare_simd (parser, decl);
22567 cp_finalize_oacc_routine (parser, decl, false);
22569 /* Reset PREFIX_ATTRIBUTES. */
22570 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22571 attributes = TREE_CHAIN (attributes);
22572 if (attributes)
22573 TREE_CHAIN (attributes) = NULL_TREE;
22575 /* If there is any qualification still in effect, clear it
22576 now; we will be starting fresh with the next declarator. */
22577 parser->scope = NULL_TREE;
22578 parser->qualifying_scope = NULL_TREE;
22579 parser->object_scope = NULL_TREE;
22580 /* If it's a `,', then there are more declarators. */
22581 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22583 cp_lexer_consume_token (parser->lexer);
22584 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22586 cp_token *token = cp_lexer_previous_token (parser->lexer);
22587 error_at (token->location,
22588 "stray %<,%> at end of member declaration");
22591 /* If the next token isn't a `;', then we have a parse error. */
22592 else if (cp_lexer_next_token_is_not (parser->lexer,
22593 CPP_SEMICOLON))
22595 /* The next token might be a ways away from where the
22596 actual semicolon is missing. Find the previous token
22597 and use that for our error position. */
22598 cp_token *token = cp_lexer_previous_token (parser->lexer);
22599 error_at (token->location,
22600 "expected %<;%> at end of member declaration");
22602 /* Assume that the user meant to provide a semicolon. If
22603 we were to cp_parser_skip_to_end_of_statement, we might
22604 skip to a semicolon inside a member function definition
22605 and issue nonsensical error messages. */
22606 assume_semicolon = true;
22609 if (decl)
22611 /* Add DECL to the list of members. */
22612 if (!friend_p
22613 /* Explicitly include, eg, NSDMIs, for better error
22614 recovery (c++/58650). */
22615 || !DECL_DECLARES_FUNCTION_P (decl))
22616 finish_member_declaration (decl);
22618 if (TREE_CODE (decl) == FUNCTION_DECL)
22619 cp_parser_save_default_args (parser, decl);
22620 else if (TREE_CODE (decl) == FIELD_DECL
22621 && !DECL_C_BIT_FIELD (decl)
22622 && DECL_INITIAL (decl))
22623 /* Add DECL to the queue of NSDMI to be parsed later. */
22624 vec_safe_push (unparsed_nsdmis, decl);
22627 if (assume_semicolon)
22628 goto out;
22632 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22633 out:
22634 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22637 /* Parse a pure-specifier.
22639 pure-specifier:
22642 Returns INTEGER_ZERO_NODE if a pure specifier is found.
22643 Otherwise, ERROR_MARK_NODE is returned. */
22645 static tree
22646 cp_parser_pure_specifier (cp_parser* parser)
22648 cp_token *token;
22650 /* Look for the `=' token. */
22651 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
22652 return error_mark_node;
22653 /* Look for the `0' token. */
22654 token = cp_lexer_peek_token (parser->lexer);
22656 if (token->type == CPP_EOF
22657 || token->type == CPP_PRAGMA_EOL)
22658 return error_mark_node;
22660 cp_lexer_consume_token (parser->lexer);
22662 /* Accept = default or = delete in c++0x mode. */
22663 if (token->keyword == RID_DEFAULT
22664 || token->keyword == RID_DELETE)
22666 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
22667 return token->u.value;
22670 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
22671 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
22673 cp_parser_error (parser,
22674 "invalid pure specifier (only %<= 0%> is allowed)");
22675 cp_parser_skip_to_end_of_statement (parser);
22676 return error_mark_node;
22678 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
22680 error_at (token->location, "templates may not be %<virtual%>");
22681 return error_mark_node;
22684 return integer_zero_node;
22687 /* Parse a constant-initializer.
22689 constant-initializer:
22690 = constant-expression
22692 Returns a representation of the constant-expression. */
22694 static tree
22695 cp_parser_constant_initializer (cp_parser* parser)
22697 /* Look for the `=' token. */
22698 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
22699 return error_mark_node;
22701 /* It is invalid to write:
22703 struct S { static const int i = { 7 }; };
22706 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22708 cp_parser_error (parser,
22709 "a brace-enclosed initializer is not allowed here");
22710 /* Consume the opening brace. */
22711 cp_lexer_consume_token (parser->lexer);
22712 /* Skip the initializer. */
22713 cp_parser_skip_to_closing_brace (parser);
22714 /* Look for the trailing `}'. */
22715 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
22717 return error_mark_node;
22720 return cp_parser_constant_expression (parser);
22723 /* Derived classes [gram.class.derived] */
22725 /* Parse a base-clause.
22727 base-clause:
22728 : base-specifier-list
22730 base-specifier-list:
22731 base-specifier ... [opt]
22732 base-specifier-list , base-specifier ... [opt]
22734 Returns a TREE_LIST representing the base-classes, in the order in
22735 which they were declared. The representation of each node is as
22736 described by cp_parser_base_specifier.
22738 In the case that no bases are specified, this function will return
22739 NULL_TREE, not ERROR_MARK_NODE. */
22741 static tree
22742 cp_parser_base_clause (cp_parser* parser)
22744 tree bases = NULL_TREE;
22746 /* Look for the `:' that begins the list. */
22747 cp_parser_require (parser, CPP_COLON, RT_COLON);
22749 /* Scan the base-specifier-list. */
22750 while (true)
22752 cp_token *token;
22753 tree base;
22754 bool pack_expansion_p = false;
22756 /* Look for the base-specifier. */
22757 base = cp_parser_base_specifier (parser);
22758 /* Look for the (optional) ellipsis. */
22759 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22761 /* Consume the `...'. */
22762 cp_lexer_consume_token (parser->lexer);
22764 pack_expansion_p = true;
22767 /* Add BASE to the front of the list. */
22768 if (base && base != error_mark_node)
22770 if (pack_expansion_p)
22771 /* Make this a pack expansion type. */
22772 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
22774 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
22776 TREE_CHAIN (base) = bases;
22777 bases = base;
22780 /* Peek at the next token. */
22781 token = cp_lexer_peek_token (parser->lexer);
22782 /* If it's not a comma, then the list is complete. */
22783 if (token->type != CPP_COMMA)
22784 break;
22785 /* Consume the `,'. */
22786 cp_lexer_consume_token (parser->lexer);
22789 /* PARSER->SCOPE may still be non-NULL at this point, if the last
22790 base class had a qualified name. However, the next name that
22791 appears is certainly not qualified. */
22792 parser->scope = NULL_TREE;
22793 parser->qualifying_scope = NULL_TREE;
22794 parser->object_scope = NULL_TREE;
22796 return nreverse (bases);
22799 /* Parse a base-specifier.
22801 base-specifier:
22802 :: [opt] nested-name-specifier [opt] class-name
22803 virtual access-specifier [opt] :: [opt] nested-name-specifier
22804 [opt] class-name
22805 access-specifier virtual [opt] :: [opt] nested-name-specifier
22806 [opt] class-name
22808 Returns a TREE_LIST. The TREE_PURPOSE will be one of
22809 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
22810 indicate the specifiers provided. The TREE_VALUE will be a TYPE
22811 (or the ERROR_MARK_NODE) indicating the type that was specified. */
22813 static tree
22814 cp_parser_base_specifier (cp_parser* parser)
22816 cp_token *token;
22817 bool done = false;
22818 bool virtual_p = false;
22819 bool duplicate_virtual_error_issued_p = false;
22820 bool duplicate_access_error_issued_p = false;
22821 bool class_scope_p, template_p;
22822 tree access = access_default_node;
22823 tree type;
22825 /* Process the optional `virtual' and `access-specifier'. */
22826 while (!done)
22828 /* Peek at the next token. */
22829 token = cp_lexer_peek_token (parser->lexer);
22830 /* Process `virtual'. */
22831 switch (token->keyword)
22833 case RID_VIRTUAL:
22834 /* If `virtual' appears more than once, issue an error. */
22835 if (virtual_p && !duplicate_virtual_error_issued_p)
22837 cp_parser_error (parser,
22838 "%<virtual%> specified more than once in base-specified");
22839 duplicate_virtual_error_issued_p = true;
22842 virtual_p = true;
22844 /* Consume the `virtual' token. */
22845 cp_lexer_consume_token (parser->lexer);
22847 break;
22849 case RID_PUBLIC:
22850 case RID_PROTECTED:
22851 case RID_PRIVATE:
22852 /* If more than one access specifier appears, issue an
22853 error. */
22854 if (access != access_default_node
22855 && !duplicate_access_error_issued_p)
22857 cp_parser_error (parser,
22858 "more than one access specifier in base-specified");
22859 duplicate_access_error_issued_p = true;
22862 access = ridpointers[(int) token->keyword];
22864 /* Consume the access-specifier. */
22865 cp_lexer_consume_token (parser->lexer);
22867 break;
22869 default:
22870 done = true;
22871 break;
22874 /* It is not uncommon to see programs mechanically, erroneously, use
22875 the 'typename' keyword to denote (dependent) qualified types
22876 as base classes. */
22877 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
22879 token = cp_lexer_peek_token (parser->lexer);
22880 if (!processing_template_decl)
22881 error_at (token->location,
22882 "keyword %<typename%> not allowed outside of templates");
22883 else
22884 error_at (token->location,
22885 "keyword %<typename%> not allowed in this context "
22886 "(the base class is implicitly a type)");
22887 cp_lexer_consume_token (parser->lexer);
22890 /* Look for the optional `::' operator. */
22891 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
22892 /* Look for the nested-name-specifier. The simplest way to
22893 implement:
22895 [temp.res]
22897 The keyword `typename' is not permitted in a base-specifier or
22898 mem-initializer; in these contexts a qualified name that
22899 depends on a template-parameter is implicitly assumed to be a
22900 type name.
22902 is to pretend that we have seen the `typename' keyword at this
22903 point. */
22904 cp_parser_nested_name_specifier_opt (parser,
22905 /*typename_keyword_p=*/true,
22906 /*check_dependency_p=*/true,
22907 typename_type,
22908 /*is_declaration=*/true);
22909 /* If the base class is given by a qualified name, assume that names
22910 we see are type names or templates, as appropriate. */
22911 class_scope_p = (parser->scope && TYPE_P (parser->scope));
22912 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
22914 if (!parser->scope
22915 && cp_lexer_next_token_is_decltype (parser->lexer))
22916 /* DR 950 allows decltype as a base-specifier. */
22917 type = cp_parser_decltype (parser);
22918 else
22920 /* Otherwise, look for the class-name. */
22921 type = cp_parser_class_name (parser,
22922 class_scope_p,
22923 template_p,
22924 typename_type,
22925 /*check_dependency_p=*/true,
22926 /*class_head_p=*/false,
22927 /*is_declaration=*/true);
22928 type = TREE_TYPE (type);
22931 if (type == error_mark_node)
22932 return error_mark_node;
22934 return finish_base_specifier (type, access, virtual_p);
22937 /* Exception handling [gram.exception] */
22939 /* Parse an (optional) noexcept-specification.
22941 noexcept-specification:
22942 noexcept ( constant-expression ) [opt]
22944 If no noexcept-specification is present, returns NULL_TREE.
22945 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
22946 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
22947 there are no parentheses. CONSUMED_EXPR will be set accordingly.
22948 Otherwise, returns a noexcept specification unless RETURN_COND is true,
22949 in which case a boolean condition is returned instead. */
22951 static tree
22952 cp_parser_noexcept_specification_opt (cp_parser* parser,
22953 bool require_constexpr,
22954 bool* consumed_expr,
22955 bool return_cond)
22957 cp_token *token;
22958 const char *saved_message;
22960 /* Peek at the next token. */
22961 token = cp_lexer_peek_token (parser->lexer);
22963 /* Is it a noexcept-specification? */
22964 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
22966 tree expr;
22967 cp_lexer_consume_token (parser->lexer);
22969 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
22971 cp_lexer_consume_token (parser->lexer);
22973 if (require_constexpr)
22975 /* Types may not be defined in an exception-specification. */
22976 saved_message = parser->type_definition_forbidden_message;
22977 parser->type_definition_forbidden_message
22978 = G_("types may not be defined in an exception-specification");
22980 expr = cp_parser_constant_expression (parser);
22982 /* Restore the saved message. */
22983 parser->type_definition_forbidden_message = saved_message;
22985 else
22987 expr = cp_parser_expression (parser);
22988 *consumed_expr = true;
22991 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22993 else
22995 expr = boolean_true_node;
22996 if (!require_constexpr)
22997 *consumed_expr = false;
23000 /* We cannot build a noexcept-spec right away because this will check
23001 that expr is a constexpr. */
23002 if (!return_cond)
23003 return build_noexcept_spec (expr, tf_warning_or_error);
23004 else
23005 return expr;
23007 else
23008 return NULL_TREE;
23011 /* Parse an (optional) exception-specification.
23013 exception-specification:
23014 throw ( type-id-list [opt] )
23016 Returns a TREE_LIST representing the exception-specification. The
23017 TREE_VALUE of each node is a type. */
23019 static tree
23020 cp_parser_exception_specification_opt (cp_parser* parser)
23022 cp_token *token;
23023 tree type_id_list;
23024 const char *saved_message;
23026 /* Peek at the next token. */
23027 token = cp_lexer_peek_token (parser->lexer);
23029 /* Is it a noexcept-specification? */
23030 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
23031 false);
23032 if (type_id_list != NULL_TREE)
23033 return type_id_list;
23035 /* If it's not `throw', then there's no exception-specification. */
23036 if (!cp_parser_is_keyword (token, RID_THROW))
23037 return NULL_TREE;
23039 #if 0
23040 /* Enable this once a lot of code has transitioned to noexcept? */
23041 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
23042 warning (OPT_Wdeprecated, "dynamic exception specifications are "
23043 "deprecated in C++0x; use %<noexcept%> instead");
23044 #endif
23046 /* Consume the `throw'. */
23047 cp_lexer_consume_token (parser->lexer);
23049 /* Look for the `('. */
23050 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23052 /* Peek at the next token. */
23053 token = cp_lexer_peek_token (parser->lexer);
23054 /* If it's not a `)', then there is a type-id-list. */
23055 if (token->type != CPP_CLOSE_PAREN)
23057 /* Types may not be defined in an exception-specification. */
23058 saved_message = parser->type_definition_forbidden_message;
23059 parser->type_definition_forbidden_message
23060 = G_("types may not be defined in an exception-specification");
23061 /* Parse the type-id-list. */
23062 type_id_list = cp_parser_type_id_list (parser);
23063 /* Restore the saved message. */
23064 parser->type_definition_forbidden_message = saved_message;
23066 else
23067 type_id_list = empty_except_spec;
23069 /* Look for the `)'. */
23070 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23072 return type_id_list;
23075 /* Parse an (optional) type-id-list.
23077 type-id-list:
23078 type-id ... [opt]
23079 type-id-list , type-id ... [opt]
23081 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
23082 in the order that the types were presented. */
23084 static tree
23085 cp_parser_type_id_list (cp_parser* parser)
23087 tree types = NULL_TREE;
23089 while (true)
23091 cp_token *token;
23092 tree type;
23094 token = cp_lexer_peek_token (parser->lexer);
23096 /* Get the next type-id. */
23097 type = cp_parser_type_id (parser);
23098 /* Check for invalid 'auto'. */
23099 if (flag_concepts && type_uses_auto (type))
23101 error_at (token->location,
23102 "invalid use of %<auto%> in exception-specification");
23103 type = error_mark_node;
23105 /* Parse the optional ellipsis. */
23106 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23108 /* Consume the `...'. */
23109 cp_lexer_consume_token (parser->lexer);
23111 /* Turn the type into a pack expansion expression. */
23112 type = make_pack_expansion (type);
23114 /* Add it to the list. */
23115 types = add_exception_specifier (types, type, /*complain=*/1);
23116 /* Peek at the next token. */
23117 token = cp_lexer_peek_token (parser->lexer);
23118 /* If it is not a `,', we are done. */
23119 if (token->type != CPP_COMMA)
23120 break;
23121 /* Consume the `,'. */
23122 cp_lexer_consume_token (parser->lexer);
23125 return nreverse (types);
23128 /* Parse a try-block.
23130 try-block:
23131 try compound-statement handler-seq */
23133 static tree
23134 cp_parser_try_block (cp_parser* parser)
23136 tree try_block;
23138 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
23139 if (parser->in_function_body
23140 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
23141 error ("%<try%> in %<constexpr%> function");
23143 try_block = begin_try_block ();
23144 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
23145 finish_try_block (try_block);
23146 cp_parser_handler_seq (parser);
23147 finish_handler_sequence (try_block);
23149 return try_block;
23152 /* Parse a function-try-block.
23154 function-try-block:
23155 try ctor-initializer [opt] function-body handler-seq */
23157 static bool
23158 cp_parser_function_try_block (cp_parser* parser)
23160 tree compound_stmt;
23161 tree try_block;
23162 bool ctor_initializer_p;
23164 /* Look for the `try' keyword. */
23165 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
23166 return false;
23167 /* Let the rest of the front end know where we are. */
23168 try_block = begin_function_try_block (&compound_stmt);
23169 /* Parse the function-body. */
23170 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23171 (parser, /*in_function_try_block=*/true);
23172 /* We're done with the `try' part. */
23173 finish_function_try_block (try_block);
23174 /* Parse the handlers. */
23175 cp_parser_handler_seq (parser);
23176 /* We're done with the handlers. */
23177 finish_function_handler_sequence (try_block, compound_stmt);
23179 return ctor_initializer_p;
23182 /* Parse a handler-seq.
23184 handler-seq:
23185 handler handler-seq [opt] */
23187 static void
23188 cp_parser_handler_seq (cp_parser* parser)
23190 while (true)
23192 cp_token *token;
23194 /* Parse the handler. */
23195 cp_parser_handler (parser);
23196 /* Peek at the next token. */
23197 token = cp_lexer_peek_token (parser->lexer);
23198 /* If it's not `catch' then there are no more handlers. */
23199 if (!cp_parser_is_keyword (token, RID_CATCH))
23200 break;
23204 /* Parse a handler.
23206 handler:
23207 catch ( exception-declaration ) compound-statement */
23209 static void
23210 cp_parser_handler (cp_parser* parser)
23212 tree handler;
23213 tree declaration;
23215 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
23216 handler = begin_handler ();
23217 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23218 declaration = cp_parser_exception_declaration (parser);
23219 finish_handler_parms (declaration, handler);
23220 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23221 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
23222 finish_handler (handler);
23225 /* Parse an exception-declaration.
23227 exception-declaration:
23228 type-specifier-seq declarator
23229 type-specifier-seq abstract-declarator
23230 type-specifier-seq
23233 Returns a VAR_DECL for the declaration, or NULL_TREE if the
23234 ellipsis variant is used. */
23236 static tree
23237 cp_parser_exception_declaration (cp_parser* parser)
23239 cp_decl_specifier_seq type_specifiers;
23240 cp_declarator *declarator;
23241 const char *saved_message;
23243 /* If it's an ellipsis, it's easy to handle. */
23244 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23246 /* Consume the `...' token. */
23247 cp_lexer_consume_token (parser->lexer);
23248 return NULL_TREE;
23251 /* Types may not be defined in exception-declarations. */
23252 saved_message = parser->type_definition_forbidden_message;
23253 parser->type_definition_forbidden_message
23254 = G_("types may not be defined in exception-declarations");
23256 /* Parse the type-specifier-seq. */
23257 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
23258 /*is_trailing_return=*/false,
23259 &type_specifiers);
23260 /* If it's a `)', then there is no declarator. */
23261 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
23262 declarator = NULL;
23263 else
23264 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
23265 /*ctor_dtor_or_conv_p=*/NULL,
23266 /*parenthesized_p=*/NULL,
23267 /*member_p=*/false,
23268 /*friend_p=*/false);
23270 /* Restore the saved message. */
23271 parser->type_definition_forbidden_message = saved_message;
23273 if (!type_specifiers.any_specifiers_p)
23274 return error_mark_node;
23276 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
23279 /* Parse a throw-expression.
23281 throw-expression:
23282 throw assignment-expression [opt]
23284 Returns a THROW_EXPR representing the throw-expression. */
23286 static tree
23287 cp_parser_throw_expression (cp_parser* parser)
23289 tree expression;
23290 cp_token* token;
23292 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
23293 token = cp_lexer_peek_token (parser->lexer);
23294 /* Figure out whether or not there is an assignment-expression
23295 following the "throw" keyword. */
23296 if (token->type == CPP_COMMA
23297 || token->type == CPP_SEMICOLON
23298 || token->type == CPP_CLOSE_PAREN
23299 || token->type == CPP_CLOSE_SQUARE
23300 || token->type == CPP_CLOSE_BRACE
23301 || token->type == CPP_COLON)
23302 expression = NULL_TREE;
23303 else
23304 expression = cp_parser_assignment_expression (parser);
23306 return build_throw (expression);
23309 /* GNU Extensions */
23311 /* Parse an (optional) asm-specification.
23313 asm-specification:
23314 asm ( string-literal )
23316 If the asm-specification is present, returns a STRING_CST
23317 corresponding to the string-literal. Otherwise, returns
23318 NULL_TREE. */
23320 static tree
23321 cp_parser_asm_specification_opt (cp_parser* parser)
23323 cp_token *token;
23324 tree asm_specification;
23326 /* Peek at the next token. */
23327 token = cp_lexer_peek_token (parser->lexer);
23328 /* If the next token isn't the `asm' keyword, then there's no
23329 asm-specification. */
23330 if (!cp_parser_is_keyword (token, RID_ASM))
23331 return NULL_TREE;
23333 /* Consume the `asm' token. */
23334 cp_lexer_consume_token (parser->lexer);
23335 /* Look for the `('. */
23336 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23338 /* Look for the string-literal. */
23339 asm_specification = cp_parser_string_literal (parser, false, false);
23341 /* Look for the `)'. */
23342 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23344 return asm_specification;
23347 /* Parse an asm-operand-list.
23349 asm-operand-list:
23350 asm-operand
23351 asm-operand-list , asm-operand
23353 asm-operand:
23354 string-literal ( expression )
23355 [ string-literal ] string-literal ( expression )
23357 Returns a TREE_LIST representing the operands. The TREE_VALUE of
23358 each node is the expression. The TREE_PURPOSE is itself a
23359 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
23360 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
23361 is a STRING_CST for the string literal before the parenthesis. Returns
23362 ERROR_MARK_NODE if any of the operands are invalid. */
23364 static tree
23365 cp_parser_asm_operand_list (cp_parser* parser)
23367 tree asm_operands = NULL_TREE;
23368 bool invalid_operands = false;
23370 while (true)
23372 tree string_literal;
23373 tree expression;
23374 tree name;
23376 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
23378 /* Consume the `[' token. */
23379 cp_lexer_consume_token (parser->lexer);
23380 /* Read the operand name. */
23381 name = cp_parser_identifier (parser);
23382 if (name != error_mark_node)
23383 name = build_string (IDENTIFIER_LENGTH (name),
23384 IDENTIFIER_POINTER (name));
23385 /* Look for the closing `]'. */
23386 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23388 else
23389 name = NULL_TREE;
23390 /* Look for the string-literal. */
23391 string_literal = cp_parser_string_literal (parser, false, false);
23393 /* Look for the `('. */
23394 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23395 /* Parse the expression. */
23396 expression = cp_parser_expression (parser);
23397 /* Look for the `)'. */
23398 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23400 if (name == error_mark_node
23401 || string_literal == error_mark_node
23402 || expression == error_mark_node)
23403 invalid_operands = true;
23405 /* Add this operand to the list. */
23406 asm_operands = tree_cons (build_tree_list (name, string_literal),
23407 expression,
23408 asm_operands);
23409 /* If the next token is not a `,', there are no more
23410 operands. */
23411 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23412 break;
23413 /* Consume the `,'. */
23414 cp_lexer_consume_token (parser->lexer);
23417 return invalid_operands ? error_mark_node : nreverse (asm_operands);
23420 /* Parse an asm-clobber-list.
23422 asm-clobber-list:
23423 string-literal
23424 asm-clobber-list , string-literal
23426 Returns a TREE_LIST, indicating the clobbers in the order that they
23427 appeared. The TREE_VALUE of each node is a STRING_CST. */
23429 static tree
23430 cp_parser_asm_clobber_list (cp_parser* parser)
23432 tree clobbers = NULL_TREE;
23434 while (true)
23436 tree string_literal;
23438 /* Look for the string literal. */
23439 string_literal = cp_parser_string_literal (parser, false, false);
23440 /* Add it to the list. */
23441 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
23442 /* If the next token is not a `,', then the list is
23443 complete. */
23444 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23445 break;
23446 /* Consume the `,' token. */
23447 cp_lexer_consume_token (parser->lexer);
23450 return clobbers;
23453 /* Parse an asm-label-list.
23455 asm-label-list:
23456 identifier
23457 asm-label-list , identifier
23459 Returns a TREE_LIST, indicating the labels in the order that they
23460 appeared. The TREE_VALUE of each node is a label. */
23462 static tree
23463 cp_parser_asm_label_list (cp_parser* parser)
23465 tree labels = NULL_TREE;
23467 while (true)
23469 tree identifier, label, name;
23471 /* Look for the identifier. */
23472 identifier = cp_parser_identifier (parser);
23473 if (!error_operand_p (identifier))
23475 label = lookup_label (identifier);
23476 if (TREE_CODE (label) == LABEL_DECL)
23478 TREE_USED (label) = 1;
23479 check_goto (label);
23480 name = build_string (IDENTIFIER_LENGTH (identifier),
23481 IDENTIFIER_POINTER (identifier));
23482 labels = tree_cons (name, label, labels);
23485 /* If the next token is not a `,', then the list is
23486 complete. */
23487 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23488 break;
23489 /* Consume the `,' token. */
23490 cp_lexer_consume_token (parser->lexer);
23493 return nreverse (labels);
23496 /* Return TRUE iff the next tokens in the stream are possibly the
23497 beginning of a GNU extension attribute. */
23499 static bool
23500 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
23502 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
23505 /* Return TRUE iff the next tokens in the stream are possibly the
23506 beginning of a standard C++-11 attribute specifier. */
23508 static bool
23509 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
23511 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
23514 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23515 beginning of a standard C++-11 attribute specifier. */
23517 static bool
23518 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
23520 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23522 return (cxx_dialect >= cxx11
23523 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
23524 || (token->type == CPP_OPEN_SQUARE
23525 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
23526 && token->type == CPP_OPEN_SQUARE)));
23529 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23530 beginning of a GNU extension attribute. */
23532 static bool
23533 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
23535 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23537 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
23540 /* Return true iff the next tokens can be the beginning of either a
23541 GNU attribute list, or a standard C++11 attribute sequence. */
23543 static bool
23544 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
23546 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
23547 || cp_next_tokens_can_be_std_attribute_p (parser));
23550 /* Return true iff the next Nth tokens can be the beginning of either
23551 a GNU attribute list, or a standard C++11 attribute sequence. */
23553 static bool
23554 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
23556 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
23557 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
23560 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
23561 of GNU attributes, or return NULL. */
23563 static tree
23564 cp_parser_attributes_opt (cp_parser *parser)
23566 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
23567 return cp_parser_gnu_attributes_opt (parser);
23568 return cp_parser_std_attribute_spec_seq (parser);
23571 #define CILK_SIMD_FN_CLAUSE_MASK \
23572 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
23573 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
23574 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
23575 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
23576 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
23578 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
23579 vector [(<clauses>)] */
23581 static void
23582 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
23584 bool first_p = parser->cilk_simd_fn_info == NULL;
23585 cp_token *token = v_token;
23586 if (first_p)
23588 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
23589 parser->cilk_simd_fn_info->error_seen = false;
23590 parser->cilk_simd_fn_info->fndecl_seen = false;
23591 parser->cilk_simd_fn_info->tokens = vNULL;
23593 int paren_scope = 0;
23594 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23596 cp_lexer_consume_token (parser->lexer);
23597 v_token = cp_lexer_peek_token (parser->lexer);
23598 paren_scope++;
23600 while (paren_scope > 0)
23602 token = cp_lexer_peek_token (parser->lexer);
23603 if (token->type == CPP_OPEN_PAREN)
23604 paren_scope++;
23605 else if (token->type == CPP_CLOSE_PAREN)
23606 paren_scope--;
23607 /* Do not push the last ')' */
23608 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
23609 cp_lexer_consume_token (parser->lexer);
23612 token->type = CPP_PRAGMA_EOL;
23613 parser->lexer->next_token = token;
23614 cp_lexer_consume_token (parser->lexer);
23616 struct cp_token_cache *cp
23617 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
23618 parser->cilk_simd_fn_info->tokens.safe_push (cp);
23621 /* Parse an (optional) series of attributes.
23623 attributes:
23624 attributes attribute
23626 attribute:
23627 __attribute__ (( attribute-list [opt] ))
23629 The return value is as for cp_parser_gnu_attribute_list. */
23631 static tree
23632 cp_parser_gnu_attributes_opt (cp_parser* parser)
23634 tree attributes = NULL_TREE;
23636 while (true)
23638 cp_token *token;
23639 tree attribute_list;
23640 bool ok = true;
23642 /* Peek at the next token. */
23643 token = cp_lexer_peek_token (parser->lexer);
23644 /* If it's not `__attribute__', then we're done. */
23645 if (token->keyword != RID_ATTRIBUTE)
23646 break;
23648 /* Consume the `__attribute__' keyword. */
23649 cp_lexer_consume_token (parser->lexer);
23650 /* Look for the two `(' tokens. */
23651 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23652 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23654 /* Peek at the next token. */
23655 token = cp_lexer_peek_token (parser->lexer);
23656 if (token->type != CPP_CLOSE_PAREN)
23657 /* Parse the attribute-list. */
23658 attribute_list = cp_parser_gnu_attribute_list (parser);
23659 else
23660 /* If the next token is a `)', then there is no attribute
23661 list. */
23662 attribute_list = NULL;
23664 /* Look for the two `)' tokens. */
23665 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23666 ok = false;
23667 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23668 ok = false;
23669 if (!ok)
23670 cp_parser_skip_to_end_of_statement (parser);
23672 /* Add these new attributes to the list. */
23673 attributes = chainon (attributes, attribute_list);
23676 return attributes;
23679 /* Parse a GNU attribute-list.
23681 attribute-list:
23682 attribute
23683 attribute-list , attribute
23685 attribute:
23686 identifier
23687 identifier ( identifier )
23688 identifier ( identifier , expression-list )
23689 identifier ( expression-list )
23691 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
23692 to an attribute. The TREE_PURPOSE of each node is the identifier
23693 indicating which attribute is in use. The TREE_VALUE represents
23694 the arguments, if any. */
23696 static tree
23697 cp_parser_gnu_attribute_list (cp_parser* parser)
23699 tree attribute_list = NULL_TREE;
23700 bool save_translate_strings_p = parser->translate_strings_p;
23702 parser->translate_strings_p = false;
23703 while (true)
23705 cp_token *token;
23706 tree identifier;
23707 tree attribute;
23709 /* Look for the identifier. We also allow keywords here; for
23710 example `__attribute__ ((const))' is legal. */
23711 token = cp_lexer_peek_token (parser->lexer);
23712 if (token->type == CPP_NAME
23713 || token->type == CPP_KEYWORD)
23715 tree arguments = NULL_TREE;
23717 /* Consume the token, but save it since we need it for the
23718 SIMD enabled function parsing. */
23719 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
23721 /* Save away the identifier that indicates which attribute
23722 this is. */
23723 identifier = (token->type == CPP_KEYWORD)
23724 /* For keywords, use the canonical spelling, not the
23725 parsed identifier. */
23726 ? ridpointers[(int) token->keyword]
23727 : id_token->u.value;
23729 attribute = build_tree_list (identifier, NULL_TREE);
23731 /* Peek at the next token. */
23732 token = cp_lexer_peek_token (parser->lexer);
23733 /* If it's an `(', then parse the attribute arguments. */
23734 if (token->type == CPP_OPEN_PAREN)
23736 vec<tree, va_gc> *vec;
23737 int attr_flag = (attribute_takes_identifier_p (identifier)
23738 ? id_attr : normal_attr);
23739 if (is_cilkplus_vector_p (identifier))
23741 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
23742 continue;
23744 else
23745 vec = cp_parser_parenthesized_expression_list
23746 (parser, attr_flag, /*cast_p=*/false,
23747 /*allow_expansion_p=*/false,
23748 /*non_constant_p=*/NULL);
23749 if (vec == NULL)
23750 arguments = error_mark_node;
23751 else
23753 arguments = build_tree_list_vec (vec);
23754 release_tree_vector (vec);
23756 /* Save the arguments away. */
23757 TREE_VALUE (attribute) = arguments;
23759 else if (is_cilkplus_vector_p (identifier))
23761 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
23762 continue;
23765 if (arguments != error_mark_node)
23767 /* Add this attribute to the list. */
23768 TREE_CHAIN (attribute) = attribute_list;
23769 attribute_list = attribute;
23772 token = cp_lexer_peek_token (parser->lexer);
23774 /* Now, look for more attributes. If the next token isn't a
23775 `,', we're done. */
23776 if (token->type != CPP_COMMA)
23777 break;
23779 /* Consume the comma and keep going. */
23780 cp_lexer_consume_token (parser->lexer);
23782 parser->translate_strings_p = save_translate_strings_p;
23784 /* We built up the list in reverse order. */
23785 return nreverse (attribute_list);
23788 /* Parse a standard C++11 attribute.
23790 The returned representation is a TREE_LIST which TREE_PURPOSE is
23791 the scoped name of the attribute, and the TREE_VALUE is its
23792 arguments list.
23794 Note that the scoped name of the attribute is itself a TREE_LIST
23795 which TREE_PURPOSE is the namespace of the attribute, and
23796 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
23797 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
23798 and which TREE_PURPOSE is directly the attribute name.
23800 Clients of the attribute code should use get_attribute_namespace
23801 and get_attribute_name to get the actual namespace and name of
23802 attributes, regardless of their being GNU or C++11 attributes.
23804 attribute:
23805 attribute-token attribute-argument-clause [opt]
23807 attribute-token:
23808 identifier
23809 attribute-scoped-token
23811 attribute-scoped-token:
23812 attribute-namespace :: identifier
23814 attribute-namespace:
23815 identifier
23817 attribute-argument-clause:
23818 ( balanced-token-seq )
23820 balanced-token-seq:
23821 balanced-token [opt]
23822 balanced-token-seq balanced-token
23824 balanced-token:
23825 ( balanced-token-seq )
23826 [ balanced-token-seq ]
23827 { balanced-token-seq }. */
23829 static tree
23830 cp_parser_std_attribute (cp_parser *parser)
23832 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
23833 cp_token *token;
23835 /* First, parse name of the attribute, a.k.a attribute-token. */
23837 token = cp_lexer_peek_token (parser->lexer);
23838 if (token->type == CPP_NAME)
23839 attr_id = token->u.value;
23840 else if (token->type == CPP_KEYWORD)
23841 attr_id = ridpointers[(int) token->keyword];
23842 else if (token->flags & NAMED_OP)
23843 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
23845 if (attr_id == NULL_TREE)
23846 return NULL_TREE;
23848 cp_lexer_consume_token (parser->lexer);
23850 token = cp_lexer_peek_token (parser->lexer);
23851 if (token->type == CPP_SCOPE)
23853 /* We are seeing a scoped attribute token. */
23855 cp_lexer_consume_token (parser->lexer);
23856 attr_ns = attr_id;
23858 token = cp_lexer_consume_token (parser->lexer);
23859 if (token->type == CPP_NAME)
23860 attr_id = token->u.value;
23861 else if (token->type == CPP_KEYWORD)
23862 attr_id = ridpointers[(int) token->keyword];
23863 else
23865 error_at (token->location,
23866 "expected an identifier for the attribute name");
23867 return error_mark_node;
23869 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
23870 NULL_TREE);
23871 token = cp_lexer_peek_token (parser->lexer);
23873 else
23875 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
23876 NULL_TREE);
23877 /* C++11 noreturn attribute is equivalent to GNU's. */
23878 if (is_attribute_p ("noreturn", attr_id))
23879 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
23880 /* C++14 deprecated attribute is equivalent to GNU's. */
23881 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
23883 if (cxx_dialect == cxx11)
23884 pedwarn (token->location, OPT_Wpedantic,
23885 "%<deprecated%> is a C++14 feature;"
23886 " use %<gnu::deprecated%>");
23887 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
23889 /* Transactional Memory TS optimize_for_synchronized attribute is
23890 equivalent to GNU transaction_callable. */
23891 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
23892 TREE_PURPOSE (attribute)
23893 = get_identifier ("transaction_callable");
23894 /* Transactional Memory attributes are GNU attributes. */
23895 else if (tm_attr_to_mask (attr_id))
23896 TREE_PURPOSE (attribute) = attr_id;
23899 /* Now parse the optional argument clause of the attribute. */
23901 if (token->type != CPP_OPEN_PAREN)
23902 return attribute;
23905 vec<tree, va_gc> *vec;
23906 int attr_flag = normal_attr;
23908 if (attr_ns == get_identifier ("gnu")
23909 && attribute_takes_identifier_p (attr_id))
23910 /* A GNU attribute that takes an identifier in parameter. */
23911 attr_flag = id_attr;
23913 vec = cp_parser_parenthesized_expression_list
23914 (parser, attr_flag, /*cast_p=*/false,
23915 /*allow_expansion_p=*/true,
23916 /*non_constant_p=*/NULL);
23917 if (vec == NULL)
23918 arguments = error_mark_node;
23919 else
23921 arguments = build_tree_list_vec (vec);
23922 release_tree_vector (vec);
23925 if (arguments == error_mark_node)
23926 attribute = error_mark_node;
23927 else
23928 TREE_VALUE (attribute) = arguments;
23931 return attribute;
23934 /* Check that the attribute ATTRIBUTE appears at most once in the
23935 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
23936 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
23937 isn't implemented yet in GCC. */
23939 static void
23940 cp_parser_check_std_attribute (tree attributes, tree attribute)
23942 if (attributes)
23944 tree name = get_attribute_name (attribute);
23945 if (is_attribute_p ("noreturn", name)
23946 && lookup_attribute ("noreturn", attributes))
23947 error ("attribute noreturn can appear at most once "
23948 "in an attribute-list");
23949 else if (is_attribute_p ("deprecated", name)
23950 && lookup_attribute ("deprecated", attributes))
23951 error ("attribute deprecated can appear at most once "
23952 "in an attribute-list");
23956 /* Parse a list of standard C++-11 attributes.
23958 attribute-list:
23959 attribute [opt]
23960 attribute-list , attribute[opt]
23961 attribute ...
23962 attribute-list , attribute ...
23965 static tree
23966 cp_parser_std_attribute_list (cp_parser *parser)
23968 tree attributes = NULL_TREE, attribute = NULL_TREE;
23969 cp_token *token = NULL;
23971 while (true)
23973 attribute = cp_parser_std_attribute (parser);
23974 if (attribute == error_mark_node)
23975 break;
23976 if (attribute != NULL_TREE)
23978 cp_parser_check_std_attribute (attributes, attribute);
23979 TREE_CHAIN (attribute) = attributes;
23980 attributes = attribute;
23982 token = cp_lexer_peek_token (parser->lexer);
23983 if (token->type == CPP_ELLIPSIS)
23985 cp_lexer_consume_token (parser->lexer);
23986 TREE_VALUE (attribute)
23987 = make_pack_expansion (TREE_VALUE (attribute));
23988 token = cp_lexer_peek_token (parser->lexer);
23990 if (token->type != CPP_COMMA)
23991 break;
23992 cp_lexer_consume_token (parser->lexer);
23994 attributes = nreverse (attributes);
23995 return attributes;
23998 /* Parse a standard C++-11 attribute specifier.
24000 attribute-specifier:
24001 [ [ attribute-list ] ]
24002 alignment-specifier
24004 alignment-specifier:
24005 alignas ( type-id ... [opt] )
24006 alignas ( alignment-expression ... [opt] ). */
24008 static tree
24009 cp_parser_std_attribute_spec (cp_parser *parser)
24011 tree attributes = NULL_TREE;
24012 cp_token *token = cp_lexer_peek_token (parser->lexer);
24014 if (token->type == CPP_OPEN_SQUARE
24015 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
24017 cp_lexer_consume_token (parser->lexer);
24018 cp_lexer_consume_token (parser->lexer);
24020 attributes = cp_parser_std_attribute_list (parser);
24022 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
24023 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
24024 cp_parser_skip_to_end_of_statement (parser);
24025 else
24026 /* Warn about parsing c++11 attribute in non-c++1 mode, only
24027 when we are sure that we have actually parsed them. */
24028 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24030 else
24032 tree alignas_expr;
24034 /* Look for an alignment-specifier. */
24036 token = cp_lexer_peek_token (parser->lexer);
24038 if (token->type != CPP_KEYWORD
24039 || token->keyword != RID_ALIGNAS)
24040 return NULL_TREE;
24042 cp_lexer_consume_token (parser->lexer);
24043 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24045 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
24047 cp_parser_error (parser, "expected %<(%>");
24048 return error_mark_node;
24051 cp_parser_parse_tentatively (parser);
24052 alignas_expr = cp_parser_type_id (parser);
24054 if (!cp_parser_parse_definitely (parser))
24056 gcc_assert (alignas_expr == error_mark_node
24057 || alignas_expr == NULL_TREE);
24059 alignas_expr =
24060 cp_parser_assignment_expression (parser);
24061 if (alignas_expr == error_mark_node)
24062 cp_parser_skip_to_end_of_statement (parser);
24063 if (alignas_expr == NULL_TREE
24064 || alignas_expr == error_mark_node)
24065 return alignas_expr;
24068 alignas_expr = cxx_alignas_expr (alignas_expr);
24069 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
24071 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24073 cp_lexer_consume_token (parser->lexer);
24074 alignas_expr = make_pack_expansion (alignas_expr);
24077 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
24079 cp_parser_error (parser, "expected %<)%>");
24080 return error_mark_node;
24083 /* Build the C++-11 representation of an 'aligned'
24084 attribute. */
24085 attributes =
24086 build_tree_list (build_tree_list (get_identifier ("gnu"),
24087 get_identifier ("aligned")),
24088 alignas_expr);
24091 return attributes;
24094 /* Parse a standard C++-11 attribute-specifier-seq.
24096 attribute-specifier-seq:
24097 attribute-specifier-seq [opt] attribute-specifier
24100 static tree
24101 cp_parser_std_attribute_spec_seq (cp_parser *parser)
24103 tree attr_specs = NULL;
24105 while (true)
24107 tree attr_spec = cp_parser_std_attribute_spec (parser);
24108 if (attr_spec == NULL_TREE)
24109 break;
24110 if (attr_spec == error_mark_node)
24111 return error_mark_node;
24113 TREE_CHAIN (attr_spec) = attr_specs;
24114 attr_specs = attr_spec;
24117 attr_specs = nreverse (attr_specs);
24118 return attr_specs;
24121 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
24122 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
24123 current value of the PEDANTIC flag, regardless of whether or not
24124 the `__extension__' keyword is present. The caller is responsible
24125 for restoring the value of the PEDANTIC flag. */
24127 static bool
24128 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
24130 /* Save the old value of the PEDANTIC flag. */
24131 *saved_pedantic = pedantic;
24133 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
24135 /* Consume the `__extension__' token. */
24136 cp_lexer_consume_token (parser->lexer);
24137 /* We're not being pedantic while the `__extension__' keyword is
24138 in effect. */
24139 pedantic = 0;
24141 return true;
24144 return false;
24147 /* Parse a label declaration.
24149 label-declaration:
24150 __label__ label-declarator-seq ;
24152 label-declarator-seq:
24153 identifier , label-declarator-seq
24154 identifier */
24156 static void
24157 cp_parser_label_declaration (cp_parser* parser)
24159 /* Look for the `__label__' keyword. */
24160 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
24162 while (true)
24164 tree identifier;
24166 /* Look for an identifier. */
24167 identifier = cp_parser_identifier (parser);
24168 /* If we failed, stop. */
24169 if (identifier == error_mark_node)
24170 break;
24171 /* Declare it as a label. */
24172 finish_label_decl (identifier);
24173 /* If the next token is a `;', stop. */
24174 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24175 break;
24176 /* Look for the `,' separating the label declarations. */
24177 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
24180 /* Look for the final `;'. */
24181 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24184 // -------------------------------------------------------------------------- //
24185 // Requires Clause
24187 // Parse a requires clause.
24189 // requires-clause:
24190 // 'requires' logical-or-expression
24192 // The required logical-or-expression must be a constant expression. Note
24193 // that we don't check that the expression is constepxr here. We defer until
24194 // we analyze constraints and then, we only check atomic constraints.
24195 static tree
24196 cp_parser_requires_clause (cp_parser *parser)
24198 // Parse the requires clause so that it is not automatically folded.
24199 ++processing_template_decl;
24200 tree expr = cp_parser_binary_expression (parser, false, false,
24201 PREC_NOT_OPERATOR, NULL);
24202 --processing_template_decl;
24203 return expr;
24206 // Optionally parse a requires clause:
24207 static tree
24208 cp_parser_requires_clause_opt (cp_parser *parser)
24210 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
24211 return NULL_TREE;
24212 cp_lexer_consume_token (parser->lexer);
24213 return cp_parser_requires_clause (parser);
24217 /*---------------------------------------------------------------------------
24218 Requires expressions
24219 ---------------------------------------------------------------------------*/
24221 /* Parse a requires expression
24223 requirement-expression:
24224 'requires' requirement-parameter-list [opt] requirement-body */
24225 static tree
24226 cp_parser_requires_expression (cp_parser *parser)
24228 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
24229 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
24231 /* A requires-expression shall appear only within a concept
24232 definition or a requires-clause.
24234 TODO: Implement this diagnostic correctly. */
24235 if (!processing_template_decl)
24237 error_at (loc, "a requires expression cannot appear outside a template");
24238 cp_parser_skip_to_end_of_statement (parser);
24239 return error_mark_node;
24242 tree parms, reqs;
24244 /* Local parameters are delared as variables within the scope
24245 of the expression. They are not visible past the end of
24246 the expression. Expressions within the requires-expression
24247 are unevaluated. */
24248 struct scope_sentinel
24250 scope_sentinel ()
24252 ++cp_unevaluated_operand;
24253 begin_scope (sk_block, NULL_TREE);
24256 ~scope_sentinel ()
24258 pop_bindings_and_leave_scope ();
24259 --cp_unevaluated_operand;
24261 } s;
24263 /* Parse the optional parameter list. */
24264 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24266 parms = cp_parser_requirement_parameter_list (parser);
24267 if (parms == error_mark_node)
24268 return error_mark_node;
24270 else
24271 parms = NULL_TREE;
24273 /* Parse the requirement body. */
24274 reqs = cp_parser_requirement_body (parser);
24275 if (reqs == error_mark_node)
24276 return error_mark_node;
24279 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
24280 the parm chain. */
24281 grokparms (parms, &parms);
24282 return finish_requires_expr (parms, reqs);
24285 /* Parse a parameterized requirement.
24287 requirement-parameter-list:
24288 '(' parameter-declaration-clause ')' */
24289 static tree
24290 cp_parser_requirement_parameter_list (cp_parser *parser)
24292 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24293 return error_mark_node;
24295 tree parms = cp_parser_parameter_declaration_clause (parser);
24297 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24298 return error_mark_node;
24300 return parms;
24303 /* Parse the body of a requirement.
24305 requirement-body:
24306 '{' requirement-list '}' */
24307 static tree
24308 cp_parser_requirement_body (cp_parser *parser)
24310 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24311 return error_mark_node;
24313 tree reqs = cp_parser_requirement_list (parser);
24315 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24316 return error_mark_node;
24318 return reqs;
24321 /* Parse a list of requirements.
24323 requirement-list:
24324 requirement
24325 requirement-list ';' requirement[opt] */
24326 static tree
24327 cp_parser_requirement_list (cp_parser *parser)
24329 tree result = NULL_TREE;
24330 while (true)
24332 tree req = cp_parser_requirement (parser);
24333 if (req == error_mark_node)
24334 return error_mark_node;
24336 result = tree_cons (NULL_TREE, req, result);
24338 /* If we see a semi-colon, consume it. */
24339 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24340 cp_lexer_consume_token (parser->lexer);
24342 /* Stop processing at the end of the list. */
24343 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24344 break;
24347 /* Reverse the order of requirements so they are analyzed in
24348 declaration order. */
24349 return nreverse (result);
24352 /* Parse a syntactic requirement or type requirement.
24354 requirement:
24355 simple-requirement
24356 compound-requirement
24357 type-requirement
24358 nested-requirement */
24359 static tree
24360 cp_parser_requirement (cp_parser *parser)
24362 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24363 return cp_parser_compound_requirement (parser);
24364 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24365 return cp_parser_type_requirement (parser);
24366 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
24367 return cp_parser_nested_requirement (parser);
24368 else
24369 return cp_parser_simple_requirement (parser);
24372 /* Parse a simple requirement.
24374 simple-requirement:
24375 expression ';' */
24376 static tree
24377 cp_parser_simple_requirement (cp_parser *parser)
24379 tree expr = cp_parser_expression (parser, NULL, false, false);
24380 if (!expr || expr == error_mark_node)
24381 return error_mark_node;
24383 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
24384 return error_mark_node;
24386 return finish_simple_requirement (expr);
24389 /* Parse a type requirement
24391 type-requirement
24392 nested-name-specifier [opt] required-type-name ';'
24394 required-type-name:
24395 type-name
24396 'template' [opt] simple-template-id */
24397 static tree
24398 cp_parser_type_requirement (cp_parser *parser)
24400 cp_lexer_consume_token (parser->lexer);
24402 // Save the scope before parsing name specifiers.
24403 tree saved_scope = parser->scope;
24404 tree saved_object_scope = parser->object_scope;
24405 tree saved_qualifying_scope = parser->qualifying_scope;
24406 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
24407 cp_parser_nested_name_specifier_opt (parser,
24408 /*typename_keyword_p=*/true,
24409 /*check_dependency_p=*/false,
24410 /*type_p=*/true,
24411 /*is_declaration=*/false);
24413 tree type;
24414 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24416 cp_lexer_consume_token (parser->lexer);
24417 type = cp_parser_template_id (parser,
24418 /*template_keyword_p=*/true,
24419 /*check_dependency=*/false,
24420 /*tag_type=*/none_type,
24421 /*is_declaration=*/false);
24422 type = make_typename_type (parser->scope, type, typename_type,
24423 /*complain=*/tf_error);
24425 else
24426 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
24428 if (TREE_CODE (type) == TYPE_DECL)
24429 type = TREE_TYPE (type);
24431 parser->scope = saved_scope;
24432 parser->object_scope = saved_object_scope;
24433 parser->qualifying_scope = saved_qualifying_scope;
24435 if (type == error_mark_node)
24436 cp_parser_skip_to_end_of_statement (parser);
24438 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
24439 return error_mark_node;
24440 if (type == error_mark_node)
24441 return error_mark_node;
24443 return finish_type_requirement (type);
24446 /* Parse a compound requirement
24448 compound-requirement:
24449 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
24450 static tree
24451 cp_parser_compound_requirement (cp_parser *parser)
24453 /* Parse an expression enclosed in '{ }'s. */
24454 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24455 return error_mark_node;
24457 tree expr = cp_parser_expression (parser, NULL, false, false);
24458 if (!expr || expr == error_mark_node)
24459 return error_mark_node;
24461 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24462 return error_mark_node;
24464 /* Parse the optional noexcept. */
24465 bool noexcept_p = false;
24466 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
24468 cp_lexer_consume_token (parser->lexer);
24469 noexcept_p = true;
24472 /* Parse the optional trailing return type. */
24473 tree type = NULL_TREE;
24474 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
24476 cp_lexer_consume_token (parser->lexer);
24477 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
24478 parser->in_result_type_constraint_p = true;
24479 type = cp_parser_trailing_type_id (parser);
24480 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
24481 if (type == error_mark_node)
24482 return error_mark_node;
24485 return finish_compound_requirement (expr, type, noexcept_p);
24488 /* Parse a nested requirement. This is the same as a requires clause.
24490 nested-requirement:
24491 requires-clause */
24492 static tree
24493 cp_parser_nested_requirement (cp_parser *parser)
24495 cp_lexer_consume_token (parser->lexer);
24496 tree req = cp_parser_requires_clause (parser);
24497 if (req == error_mark_node)
24498 return error_mark_node;
24499 return finish_nested_requirement (req);
24502 /* Support Functions */
24504 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
24505 NAME should have one of the representations used for an
24506 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
24507 is returned. If PARSER->SCOPE is a dependent type, then a
24508 SCOPE_REF is returned.
24510 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
24511 returned; the name was already resolved when the TEMPLATE_ID_EXPR
24512 was formed. Abstractly, such entities should not be passed to this
24513 function, because they do not need to be looked up, but it is
24514 simpler to check for this special case here, rather than at the
24515 call-sites.
24517 In cases not explicitly covered above, this function returns a
24518 DECL, OVERLOAD, or baselink representing the result of the lookup.
24519 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
24520 is returned.
24522 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
24523 (e.g., "struct") that was used. In that case bindings that do not
24524 refer to types are ignored.
24526 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
24527 ignored.
24529 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
24530 are ignored.
24532 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
24533 types.
24535 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
24536 TREE_LIST of candidates if name-lookup results in an ambiguity, and
24537 NULL_TREE otherwise. */
24539 static cp_expr
24540 cp_parser_lookup_name (cp_parser *parser, tree name,
24541 enum tag_types tag_type,
24542 bool is_template,
24543 bool is_namespace,
24544 bool check_dependency,
24545 tree *ambiguous_decls,
24546 location_t name_location)
24548 tree decl;
24549 tree object_type = parser->context->object_type;
24551 /* Assume that the lookup will be unambiguous. */
24552 if (ambiguous_decls)
24553 *ambiguous_decls = NULL_TREE;
24555 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
24556 no longer valid. Note that if we are parsing tentatively, and
24557 the parse fails, OBJECT_TYPE will be automatically restored. */
24558 parser->context->object_type = NULL_TREE;
24560 if (name == error_mark_node)
24561 return error_mark_node;
24563 /* A template-id has already been resolved; there is no lookup to
24564 do. */
24565 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
24566 return name;
24567 if (BASELINK_P (name))
24569 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
24570 == TEMPLATE_ID_EXPR);
24571 return name;
24574 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
24575 it should already have been checked to make sure that the name
24576 used matches the type being destroyed. */
24577 if (TREE_CODE (name) == BIT_NOT_EXPR)
24579 tree type;
24581 /* Figure out to which type this destructor applies. */
24582 if (parser->scope)
24583 type = parser->scope;
24584 else if (object_type)
24585 type = object_type;
24586 else
24587 type = current_class_type;
24588 /* If that's not a class type, there is no destructor. */
24589 if (!type || !CLASS_TYPE_P (type))
24590 return error_mark_node;
24591 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
24592 lazily_declare_fn (sfk_destructor, type);
24593 if (!CLASSTYPE_DESTRUCTORS (type))
24594 return error_mark_node;
24595 /* If it was a class type, return the destructor. */
24596 return CLASSTYPE_DESTRUCTORS (type);
24599 /* By this point, the NAME should be an ordinary identifier. If
24600 the id-expression was a qualified name, the qualifying scope is
24601 stored in PARSER->SCOPE at this point. */
24602 gcc_assert (identifier_p (name));
24604 /* Perform the lookup. */
24605 if (parser->scope)
24607 bool dependent_p;
24609 if (parser->scope == error_mark_node)
24610 return error_mark_node;
24612 /* If the SCOPE is dependent, the lookup must be deferred until
24613 the template is instantiated -- unless we are explicitly
24614 looking up names in uninstantiated templates. Even then, we
24615 cannot look up the name if the scope is not a class type; it
24616 might, for example, be a template type parameter. */
24617 dependent_p = (TYPE_P (parser->scope)
24618 && dependent_scope_p (parser->scope));
24619 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
24620 && dependent_p)
24621 /* Defer lookup. */
24622 decl = error_mark_node;
24623 else
24625 tree pushed_scope = NULL_TREE;
24627 /* If PARSER->SCOPE is a dependent type, then it must be a
24628 class type, and we must not be checking dependencies;
24629 otherwise, we would have processed this lookup above. So
24630 that PARSER->SCOPE is not considered a dependent base by
24631 lookup_member, we must enter the scope here. */
24632 if (dependent_p)
24633 pushed_scope = push_scope (parser->scope);
24635 /* If the PARSER->SCOPE is a template specialization, it
24636 may be instantiated during name lookup. In that case,
24637 errors may be issued. Even if we rollback the current
24638 tentative parse, those errors are valid. */
24639 decl = lookup_qualified_name (parser->scope, name,
24640 tag_type != none_type,
24641 /*complain=*/true);
24643 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
24644 lookup result and the nested-name-specifier nominates a class C:
24645 * if the name specified after the nested-name-specifier, when
24646 looked up in C, is the injected-class-name of C (Clause 9), or
24647 * if the name specified after the nested-name-specifier is the
24648 same as the identifier or the simple-template-id's template-
24649 name in the last component of the nested-name-specifier,
24650 the name is instead considered to name the constructor of
24651 class C. [ Note: for example, the constructor is not an
24652 acceptable lookup result in an elaborated-type-specifier so
24653 the constructor would not be used in place of the
24654 injected-class-name. --end note ] Such a constructor name
24655 shall be used only in the declarator-id of a declaration that
24656 names a constructor or in a using-declaration. */
24657 if (tag_type == none_type
24658 && DECL_SELF_REFERENCE_P (decl)
24659 && same_type_p (DECL_CONTEXT (decl), parser->scope))
24660 decl = lookup_qualified_name (parser->scope, ctor_identifier,
24661 tag_type != none_type,
24662 /*complain=*/true);
24664 /* If we have a single function from a using decl, pull it out. */
24665 if (TREE_CODE (decl) == OVERLOAD
24666 && !really_overloaded_fn (decl))
24667 decl = OVL_FUNCTION (decl);
24669 if (pushed_scope)
24670 pop_scope (pushed_scope);
24673 /* If the scope is a dependent type and either we deferred lookup or
24674 we did lookup but didn't find the name, rememeber the name. */
24675 if (decl == error_mark_node && TYPE_P (parser->scope)
24676 && dependent_type_p (parser->scope))
24678 if (tag_type)
24680 tree type;
24682 /* The resolution to Core Issue 180 says that `struct
24683 A::B' should be considered a type-name, even if `A'
24684 is dependent. */
24685 type = make_typename_type (parser->scope, name, tag_type,
24686 /*complain=*/tf_error);
24687 if (type != error_mark_node)
24688 decl = TYPE_NAME (type);
24690 else if (is_template
24691 && (cp_parser_next_token_ends_template_argument_p (parser)
24692 || cp_lexer_next_token_is (parser->lexer,
24693 CPP_CLOSE_PAREN)))
24694 decl = make_unbound_class_template (parser->scope,
24695 name, NULL_TREE,
24696 /*complain=*/tf_error);
24697 else
24698 decl = build_qualified_name (/*type=*/NULL_TREE,
24699 parser->scope, name,
24700 is_template);
24702 parser->qualifying_scope = parser->scope;
24703 parser->object_scope = NULL_TREE;
24705 else if (object_type)
24707 /* Look up the name in the scope of the OBJECT_TYPE, unless the
24708 OBJECT_TYPE is not a class. */
24709 if (CLASS_TYPE_P (object_type))
24710 /* If the OBJECT_TYPE is a template specialization, it may
24711 be instantiated during name lookup. In that case, errors
24712 may be issued. Even if we rollback the current tentative
24713 parse, those errors are valid. */
24714 decl = lookup_member (object_type,
24715 name,
24716 /*protect=*/0,
24717 tag_type != none_type,
24718 tf_warning_or_error);
24719 else
24720 decl = NULL_TREE;
24722 if (!decl)
24723 /* Look it up in the enclosing context. */
24724 decl = lookup_name_real (name, tag_type != none_type,
24725 /*nonclass=*/0,
24726 /*block_p=*/true, is_namespace, 0);
24727 parser->object_scope = object_type;
24728 parser->qualifying_scope = NULL_TREE;
24730 else
24732 decl = lookup_name_real (name, tag_type != none_type,
24733 /*nonclass=*/0,
24734 /*block_p=*/true, is_namespace, 0);
24735 parser->qualifying_scope = NULL_TREE;
24736 parser->object_scope = NULL_TREE;
24739 /* If the lookup failed, let our caller know. */
24740 if (!decl || decl == error_mark_node)
24741 return error_mark_node;
24743 /* Pull out the template from an injected-class-name (or multiple). */
24744 if (is_template)
24745 decl = maybe_get_template_decl_from_type_decl (decl);
24747 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
24748 if (TREE_CODE (decl) == TREE_LIST)
24750 if (ambiguous_decls)
24751 *ambiguous_decls = decl;
24752 /* The error message we have to print is too complicated for
24753 cp_parser_error, so we incorporate its actions directly. */
24754 if (!cp_parser_simulate_error (parser))
24756 error_at (name_location, "reference to %qD is ambiguous",
24757 name);
24758 print_candidates (decl);
24760 return error_mark_node;
24763 gcc_assert (DECL_P (decl)
24764 || TREE_CODE (decl) == OVERLOAD
24765 || TREE_CODE (decl) == SCOPE_REF
24766 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
24767 || BASELINK_P (decl));
24769 /* If we have resolved the name of a member declaration, check to
24770 see if the declaration is accessible. When the name resolves to
24771 set of overloaded functions, accessibility is checked when
24772 overload resolution is done.
24774 During an explicit instantiation, access is not checked at all,
24775 as per [temp.explicit]. */
24776 if (DECL_P (decl))
24777 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
24779 maybe_record_typedef_use (decl);
24781 return cp_expr (decl, name_location);
24784 /* Like cp_parser_lookup_name, but for use in the typical case where
24785 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
24786 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
24788 static tree
24789 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
24791 return cp_parser_lookup_name (parser, name,
24792 none_type,
24793 /*is_template=*/false,
24794 /*is_namespace=*/false,
24795 /*check_dependency=*/true,
24796 /*ambiguous_decls=*/NULL,
24797 location);
24800 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
24801 the current context, return the TYPE_DECL. If TAG_NAME_P is
24802 true, the DECL indicates the class being defined in a class-head,
24803 or declared in an elaborated-type-specifier.
24805 Otherwise, return DECL. */
24807 static tree
24808 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
24810 /* If the TEMPLATE_DECL is being declared as part of a class-head,
24811 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
24813 struct A {
24814 template <typename T> struct B;
24817 template <typename T> struct A::B {};
24819 Similarly, in an elaborated-type-specifier:
24821 namespace N { struct X{}; }
24823 struct A {
24824 template <typename T> friend struct N::X;
24827 However, if the DECL refers to a class type, and we are in
24828 the scope of the class, then the name lookup automatically
24829 finds the TYPE_DECL created by build_self_reference rather
24830 than a TEMPLATE_DECL. For example, in:
24832 template <class T> struct S {
24833 S s;
24836 there is no need to handle such case. */
24838 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
24839 return DECL_TEMPLATE_RESULT (decl);
24841 return decl;
24844 /* If too many, or too few, template-parameter lists apply to the
24845 declarator, issue an error message. Returns TRUE if all went well,
24846 and FALSE otherwise. */
24848 static bool
24849 cp_parser_check_declarator_template_parameters (cp_parser* parser,
24850 cp_declarator *declarator,
24851 location_t declarator_location)
24853 switch (declarator->kind)
24855 case cdk_id:
24857 unsigned num_templates = 0;
24858 tree scope = declarator->u.id.qualifying_scope;
24860 if (scope)
24861 num_templates = num_template_headers_for_class (scope);
24862 else if (TREE_CODE (declarator->u.id.unqualified_name)
24863 == TEMPLATE_ID_EXPR)
24864 /* If the DECLARATOR has the form `X<y>' then it uses one
24865 additional level of template parameters. */
24866 ++num_templates;
24868 return cp_parser_check_template_parameters
24869 (parser, num_templates, declarator_location, declarator);
24872 case cdk_function:
24873 case cdk_array:
24874 case cdk_pointer:
24875 case cdk_reference:
24876 case cdk_ptrmem:
24877 return (cp_parser_check_declarator_template_parameters
24878 (parser, declarator->declarator, declarator_location));
24880 case cdk_error:
24881 return true;
24883 default:
24884 gcc_unreachable ();
24886 return false;
24889 /* NUM_TEMPLATES were used in the current declaration. If that is
24890 invalid, return FALSE and issue an error messages. Otherwise,
24891 return TRUE. If DECLARATOR is non-NULL, then we are checking a
24892 declarator and we can print more accurate diagnostics. */
24894 static bool
24895 cp_parser_check_template_parameters (cp_parser* parser,
24896 unsigned num_templates,
24897 location_t location,
24898 cp_declarator *declarator)
24900 /* If there are the same number of template classes and parameter
24901 lists, that's OK. */
24902 if (parser->num_template_parameter_lists == num_templates)
24903 return true;
24904 /* If there are more, but only one more, then we are referring to a
24905 member template. That's OK too. */
24906 if (parser->num_template_parameter_lists == num_templates + 1)
24907 return true;
24908 /* If there are more template classes than parameter lists, we have
24909 something like:
24911 template <class T> void S<T>::R<T>::f (); */
24912 if (parser->num_template_parameter_lists < num_templates)
24914 if (declarator && !current_function_decl)
24915 error_at (location, "specializing member %<%T::%E%> "
24916 "requires %<template<>%> syntax",
24917 declarator->u.id.qualifying_scope,
24918 declarator->u.id.unqualified_name);
24919 else if (declarator)
24920 error_at (location, "invalid declaration of %<%T::%E%>",
24921 declarator->u.id.qualifying_scope,
24922 declarator->u.id.unqualified_name);
24923 else
24924 error_at (location, "too few template-parameter-lists");
24925 return false;
24927 /* Otherwise, there are too many template parameter lists. We have
24928 something like:
24930 template <class T> template <class U> void S::f(); */
24931 error_at (location, "too many template-parameter-lists");
24932 return false;
24935 /* Parse an optional `::' token indicating that the following name is
24936 from the global namespace. If so, PARSER->SCOPE is set to the
24937 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
24938 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
24939 Returns the new value of PARSER->SCOPE, if the `::' token is
24940 present, and NULL_TREE otherwise. */
24942 static tree
24943 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
24945 cp_token *token;
24947 /* Peek at the next token. */
24948 token = cp_lexer_peek_token (parser->lexer);
24949 /* If we're looking at a `::' token then we're starting from the
24950 global namespace, not our current location. */
24951 if (token->type == CPP_SCOPE)
24953 /* Consume the `::' token. */
24954 cp_lexer_consume_token (parser->lexer);
24955 /* Set the SCOPE so that we know where to start the lookup. */
24956 parser->scope = global_namespace;
24957 parser->qualifying_scope = global_namespace;
24958 parser->object_scope = NULL_TREE;
24960 return parser->scope;
24962 else if (!current_scope_valid_p)
24964 parser->scope = NULL_TREE;
24965 parser->qualifying_scope = NULL_TREE;
24966 parser->object_scope = NULL_TREE;
24969 return NULL_TREE;
24972 /* Returns TRUE if the upcoming token sequence is the start of a
24973 constructor declarator. If FRIEND_P is true, the declarator is
24974 preceded by the `friend' specifier. */
24976 static bool
24977 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
24979 bool constructor_p;
24980 bool outside_class_specifier_p;
24981 tree nested_name_specifier;
24982 cp_token *next_token;
24984 /* The common case is that this is not a constructor declarator, so
24985 try to avoid doing lots of work if at all possible. It's not
24986 valid declare a constructor at function scope. */
24987 if (parser->in_function_body)
24988 return false;
24989 /* And only certain tokens can begin a constructor declarator. */
24990 next_token = cp_lexer_peek_token (parser->lexer);
24991 if (next_token->type != CPP_NAME
24992 && next_token->type != CPP_SCOPE
24993 && next_token->type != CPP_NESTED_NAME_SPECIFIER
24994 && next_token->type != CPP_TEMPLATE_ID)
24995 return false;
24997 /* Parse tentatively; we are going to roll back all of the tokens
24998 consumed here. */
24999 cp_parser_parse_tentatively (parser);
25000 /* Assume that we are looking at a constructor declarator. */
25001 constructor_p = true;
25003 /* Look for the optional `::' operator. */
25004 cp_parser_global_scope_opt (parser,
25005 /*current_scope_valid_p=*/false);
25006 /* Look for the nested-name-specifier. */
25007 nested_name_specifier
25008 = (cp_parser_nested_name_specifier_opt (parser,
25009 /*typename_keyword_p=*/false,
25010 /*check_dependency_p=*/false,
25011 /*type_p=*/false,
25012 /*is_declaration=*/false));
25014 outside_class_specifier_p = (!at_class_scope_p ()
25015 || !TYPE_BEING_DEFINED (current_class_type)
25016 || friend_p);
25018 /* Outside of a class-specifier, there must be a
25019 nested-name-specifier. */
25020 if (!nested_name_specifier && outside_class_specifier_p)
25021 constructor_p = false;
25022 else if (nested_name_specifier == error_mark_node)
25023 constructor_p = false;
25025 /* If we have a class scope, this is easy; DR 147 says that S::S always
25026 names the constructor, and no other qualified name could. */
25027 if (constructor_p && nested_name_specifier
25028 && CLASS_TYPE_P (nested_name_specifier))
25030 tree id = cp_parser_unqualified_id (parser,
25031 /*template_keyword_p=*/false,
25032 /*check_dependency_p=*/false,
25033 /*declarator_p=*/true,
25034 /*optional_p=*/false);
25035 if (is_overloaded_fn (id))
25036 id = DECL_NAME (get_first_fn (id));
25037 if (!constructor_name_p (id, nested_name_specifier))
25038 constructor_p = false;
25040 /* If we still think that this might be a constructor-declarator,
25041 look for a class-name. */
25042 else if (constructor_p)
25044 /* If we have:
25046 template <typename T> struct S {
25047 S();
25050 we must recognize that the nested `S' names a class. */
25051 tree type_decl;
25052 type_decl = cp_parser_class_name (parser,
25053 /*typename_keyword_p=*/false,
25054 /*template_keyword_p=*/false,
25055 none_type,
25056 /*check_dependency_p=*/false,
25057 /*class_head_p=*/false,
25058 /*is_declaration=*/false);
25059 /* If there was no class-name, then this is not a constructor.
25060 Otherwise, if we are in a class-specifier and we aren't
25061 handling a friend declaration, check that its type matches
25062 current_class_type (c++/38313). Note: error_mark_node
25063 is left alone for error recovery purposes. */
25064 constructor_p = (!cp_parser_error_occurred (parser)
25065 && (outside_class_specifier_p
25066 || type_decl == error_mark_node
25067 || same_type_p (current_class_type,
25068 TREE_TYPE (type_decl))));
25070 /* If we're still considering a constructor, we have to see a `(',
25071 to begin the parameter-declaration-clause, followed by either a
25072 `)', an `...', or a decl-specifier. We need to check for a
25073 type-specifier to avoid being fooled into thinking that:
25075 S (f) (int);
25077 is a constructor. (It is actually a function named `f' that
25078 takes one parameter (of type `int') and returns a value of type
25079 `S'. */
25080 if (constructor_p
25081 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25082 constructor_p = false;
25084 if (constructor_p
25085 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
25086 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
25087 /* A parameter declaration begins with a decl-specifier,
25088 which is either the "attribute" keyword, a storage class
25089 specifier, or (usually) a type-specifier. */
25090 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
25092 tree type;
25093 tree pushed_scope = NULL_TREE;
25094 unsigned saved_num_template_parameter_lists;
25096 /* Names appearing in the type-specifier should be looked up
25097 in the scope of the class. */
25098 if (current_class_type)
25099 type = NULL_TREE;
25100 else
25102 type = TREE_TYPE (type_decl);
25103 if (TREE_CODE (type) == TYPENAME_TYPE)
25105 type = resolve_typename_type (type,
25106 /*only_current_p=*/false);
25107 if (TREE_CODE (type) == TYPENAME_TYPE)
25109 cp_parser_abort_tentative_parse (parser);
25110 return false;
25113 pushed_scope = push_scope (type);
25116 /* Inside the constructor parameter list, surrounding
25117 template-parameter-lists do not apply. */
25118 saved_num_template_parameter_lists
25119 = parser->num_template_parameter_lists;
25120 parser->num_template_parameter_lists = 0;
25122 /* Look for the type-specifier. */
25123 cp_parser_type_specifier (parser,
25124 CP_PARSER_FLAGS_NONE,
25125 /*decl_specs=*/NULL,
25126 /*is_declarator=*/true,
25127 /*declares_class_or_enum=*/NULL,
25128 /*is_cv_qualifier=*/NULL);
25130 parser->num_template_parameter_lists
25131 = saved_num_template_parameter_lists;
25133 /* Leave the scope of the class. */
25134 if (pushed_scope)
25135 pop_scope (pushed_scope);
25137 constructor_p = !cp_parser_error_occurred (parser);
25141 /* We did not really want to consume any tokens. */
25142 cp_parser_abort_tentative_parse (parser);
25144 return constructor_p;
25147 /* Parse the definition of the function given by the DECL_SPECIFIERS,
25148 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
25149 they must be performed once we are in the scope of the function.
25151 Returns the function defined. */
25153 static tree
25154 cp_parser_function_definition_from_specifiers_and_declarator
25155 (cp_parser* parser,
25156 cp_decl_specifier_seq *decl_specifiers,
25157 tree attributes,
25158 const cp_declarator *declarator)
25160 tree fn;
25161 bool success_p;
25163 /* Begin the function-definition. */
25164 success_p = start_function (decl_specifiers, declarator, attributes);
25166 /* The things we're about to see are not directly qualified by any
25167 template headers we've seen thus far. */
25168 reset_specialization ();
25170 /* If there were names looked up in the decl-specifier-seq that we
25171 did not check, check them now. We must wait until we are in the
25172 scope of the function to perform the checks, since the function
25173 might be a friend. */
25174 perform_deferred_access_checks (tf_warning_or_error);
25176 if (success_p)
25178 cp_finalize_omp_declare_simd (parser, current_function_decl);
25179 parser->omp_declare_simd = NULL;
25180 cp_finalize_oacc_routine (parser, current_function_decl, true);
25181 parser->oacc_routine = NULL;
25184 if (!success_p)
25186 /* Skip the entire function. */
25187 cp_parser_skip_to_end_of_block_or_statement (parser);
25188 fn = error_mark_node;
25190 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
25192 /* Seen already, skip it. An error message has already been output. */
25193 cp_parser_skip_to_end_of_block_or_statement (parser);
25194 fn = current_function_decl;
25195 current_function_decl = NULL_TREE;
25196 /* If this is a function from a class, pop the nested class. */
25197 if (current_class_name)
25198 pop_nested_class ();
25200 else
25202 timevar_id_t tv;
25203 if (DECL_DECLARED_INLINE_P (current_function_decl))
25204 tv = TV_PARSE_INLINE;
25205 else
25206 tv = TV_PARSE_FUNC;
25207 timevar_push (tv);
25208 fn = cp_parser_function_definition_after_declarator (parser,
25209 /*inline_p=*/false);
25210 timevar_pop (tv);
25213 return fn;
25216 /* Parse the part of a function-definition that follows the
25217 declarator. INLINE_P is TRUE iff this function is an inline
25218 function defined within a class-specifier.
25220 Returns the function defined. */
25222 static tree
25223 cp_parser_function_definition_after_declarator (cp_parser* parser,
25224 bool inline_p)
25226 tree fn;
25227 bool ctor_initializer_p = false;
25228 bool saved_in_unbraced_linkage_specification_p;
25229 bool saved_in_function_body;
25230 unsigned saved_num_template_parameter_lists;
25231 cp_token *token;
25232 bool fully_implicit_function_template_p
25233 = parser->fully_implicit_function_template_p;
25234 parser->fully_implicit_function_template_p = false;
25235 tree implicit_template_parms
25236 = parser->implicit_template_parms;
25237 parser->implicit_template_parms = 0;
25238 cp_binding_level* implicit_template_scope
25239 = parser->implicit_template_scope;
25240 parser->implicit_template_scope = 0;
25242 saved_in_function_body = parser->in_function_body;
25243 parser->in_function_body = true;
25244 /* If the next token is `return', then the code may be trying to
25245 make use of the "named return value" extension that G++ used to
25246 support. */
25247 token = cp_lexer_peek_token (parser->lexer);
25248 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
25250 /* Consume the `return' keyword. */
25251 cp_lexer_consume_token (parser->lexer);
25252 /* Look for the identifier that indicates what value is to be
25253 returned. */
25254 cp_parser_identifier (parser);
25255 /* Issue an error message. */
25256 error_at (token->location,
25257 "named return values are no longer supported");
25258 /* Skip tokens until we reach the start of the function body. */
25259 while (true)
25261 cp_token *token = cp_lexer_peek_token (parser->lexer);
25262 if (token->type == CPP_OPEN_BRACE
25263 || token->type == CPP_EOF
25264 || token->type == CPP_PRAGMA_EOL)
25265 break;
25266 cp_lexer_consume_token (parser->lexer);
25269 /* The `extern' in `extern "C" void f () { ... }' does not apply to
25270 anything declared inside `f'. */
25271 saved_in_unbraced_linkage_specification_p
25272 = parser->in_unbraced_linkage_specification_p;
25273 parser->in_unbraced_linkage_specification_p = false;
25274 /* Inside the function, surrounding template-parameter-lists do not
25275 apply. */
25276 saved_num_template_parameter_lists
25277 = parser->num_template_parameter_lists;
25278 parser->num_template_parameter_lists = 0;
25280 start_lambda_scope (current_function_decl);
25282 /* If the next token is `try', `__transaction_atomic', or
25283 `__transaction_relaxed`, then we are looking at either function-try-block
25284 or function-transaction-block. Note that all of these include the
25285 function-body. */
25286 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
25287 ctor_initializer_p = cp_parser_function_transaction (parser,
25288 RID_TRANSACTION_ATOMIC);
25289 else if (cp_lexer_next_token_is_keyword (parser->lexer,
25290 RID_TRANSACTION_RELAXED))
25291 ctor_initializer_p = cp_parser_function_transaction (parser,
25292 RID_TRANSACTION_RELAXED);
25293 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
25294 ctor_initializer_p = cp_parser_function_try_block (parser);
25295 else
25296 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
25297 (parser, /*in_function_try_block=*/false);
25299 finish_lambda_scope ();
25301 /* Finish the function. */
25302 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
25303 (inline_p ? 2 : 0));
25304 /* Generate code for it, if necessary. */
25305 expand_or_defer_fn (fn);
25306 /* Restore the saved values. */
25307 parser->in_unbraced_linkage_specification_p
25308 = saved_in_unbraced_linkage_specification_p;
25309 parser->num_template_parameter_lists
25310 = saved_num_template_parameter_lists;
25311 parser->in_function_body = saved_in_function_body;
25313 parser->fully_implicit_function_template_p
25314 = fully_implicit_function_template_p;
25315 parser->implicit_template_parms
25316 = implicit_template_parms;
25317 parser->implicit_template_scope
25318 = implicit_template_scope;
25320 if (parser->fully_implicit_function_template_p)
25321 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
25323 return fn;
25326 /* Parse a template-declaration body (following argument list). */
25328 static void
25329 cp_parser_template_declaration_after_parameters (cp_parser* parser,
25330 tree parameter_list,
25331 bool member_p)
25333 tree decl = NULL_TREE;
25334 bool friend_p = false;
25336 /* We just processed one more parameter list. */
25337 ++parser->num_template_parameter_lists;
25339 /* Get the deferred access checks from the parameter list. These
25340 will be checked once we know what is being declared, as for a
25341 member template the checks must be performed in the scope of the
25342 class containing the member. */
25343 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
25345 /* Tentatively parse for a new template parameter list, which can either be
25346 the template keyword or a template introduction. */
25347 if (cp_parser_template_declaration_after_export (parser, member_p))
25348 /* OK */;
25349 else if (cxx_dialect >= cxx11
25350 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25351 decl = cp_parser_alias_declaration (parser);
25352 else
25354 /* There are no access checks when parsing a template, as we do not
25355 know if a specialization will be a friend. */
25356 push_deferring_access_checks (dk_no_check);
25357 cp_token *token = cp_lexer_peek_token (parser->lexer);
25358 decl = cp_parser_single_declaration (parser,
25359 checks,
25360 member_p,
25361 /*explicit_specialization_p=*/false,
25362 &friend_p);
25363 pop_deferring_access_checks ();
25365 /* If this is a member template declaration, let the front
25366 end know. */
25367 if (member_p && !friend_p && decl)
25369 if (TREE_CODE (decl) == TYPE_DECL)
25370 cp_parser_check_access_in_redeclaration (decl, token->location);
25372 decl = finish_member_template_decl (decl);
25374 else if (friend_p && decl
25375 && DECL_DECLARES_TYPE_P (decl))
25376 make_friend_class (current_class_type, TREE_TYPE (decl),
25377 /*complain=*/true);
25379 /* We are done with the current parameter list. */
25380 --parser->num_template_parameter_lists;
25382 pop_deferring_access_checks ();
25384 /* Finish up. */
25385 finish_template_decl (parameter_list);
25387 /* Check the template arguments for a literal operator template. */
25388 if (decl
25389 && DECL_DECLARES_FUNCTION_P (decl)
25390 && UDLIT_OPER_P (DECL_NAME (decl)))
25392 bool ok = true;
25393 if (parameter_list == NULL_TREE)
25394 ok = false;
25395 else
25397 int num_parms = TREE_VEC_LENGTH (parameter_list);
25398 if (num_parms == 1)
25400 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
25401 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
25402 if (TREE_TYPE (parm) != char_type_node
25403 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
25404 ok = false;
25406 else if (num_parms == 2 && cxx_dialect >= cxx14)
25408 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
25409 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
25410 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
25411 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
25412 if (TREE_TYPE (parm) != TREE_TYPE (type)
25413 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
25414 ok = false;
25416 else
25417 ok = false;
25419 if (!ok)
25421 if (cxx_dialect >= cxx14)
25422 error ("literal operator template %qD has invalid parameter list."
25423 " Expected non-type template argument pack <char...>"
25424 " or <typename CharT, CharT...>",
25425 decl);
25426 else
25427 error ("literal operator template %qD has invalid parameter list."
25428 " Expected non-type template argument pack <char...>",
25429 decl);
25433 /* Register member declarations. */
25434 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
25435 finish_member_declaration (decl);
25436 /* If DECL is a function template, we must return to parse it later.
25437 (Even though there is no definition, there might be default
25438 arguments that need handling.) */
25439 if (member_p && decl
25440 && DECL_DECLARES_FUNCTION_P (decl))
25441 vec_safe_push (unparsed_funs_with_definitions, decl);
25444 /* Parse a template introduction header for a template-declaration. Returns
25445 false if tentative parse fails. */
25447 static bool
25448 cp_parser_template_introduction (cp_parser* parser, bool member_p)
25450 cp_parser_parse_tentatively (parser);
25452 tree saved_scope = parser->scope;
25453 tree saved_object_scope = parser->object_scope;
25454 tree saved_qualifying_scope = parser->qualifying_scope;
25456 /* Look for the optional `::' operator. */
25457 cp_parser_global_scope_opt (parser,
25458 /*current_scope_valid_p=*/false);
25459 /* Look for the nested-name-specifier. */
25460 cp_parser_nested_name_specifier_opt (parser,
25461 /*typename_keyword_p=*/false,
25462 /*check_dependency_p=*/true,
25463 /*type_p=*/false,
25464 /*is_declaration=*/false);
25466 cp_token *token = cp_lexer_peek_token (parser->lexer);
25467 tree concept_name = cp_parser_identifier (parser);
25469 /* Look up the concept for which we will be matching
25470 template parameters. */
25471 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
25472 token->location);
25473 parser->scope = saved_scope;
25474 parser->object_scope = saved_object_scope;
25475 parser->qualifying_scope = saved_qualifying_scope;
25477 if (concept_name == error_mark_node)
25478 cp_parser_simulate_error (parser);
25480 /* Look for opening brace for introduction. */
25481 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
25483 if (!cp_parser_parse_definitely (parser))
25484 return false;
25486 push_deferring_access_checks (dk_deferred);
25488 /* Build vector of placeholder parameters and grab
25489 matching identifiers. */
25490 tree introduction_list = cp_parser_introduction_list (parser);
25492 /* The introduction-list shall not be empty. */
25493 int nargs = TREE_VEC_LENGTH (introduction_list);
25494 if (nargs == 0)
25496 error ("empty introduction-list");
25497 return true;
25500 /* Look for closing brace for introduction. */
25501 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25502 return true;
25504 if (tmpl_decl == error_mark_node)
25506 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
25507 token->location);
25508 return true;
25511 /* Build and associate the constraint. */
25512 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
25513 if (parms && parms != error_mark_node)
25515 cp_parser_template_declaration_after_parameters (parser, parms,
25516 member_p);
25517 return true;
25520 error_at (token->location, "no matching concept for template-introduction");
25521 return true;
25524 /* Parse a normal template-declaration following the template keyword. */
25526 static void
25527 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
25529 tree parameter_list;
25530 bool need_lang_pop;
25531 location_t location = input_location;
25533 /* Look for the `<' token. */
25534 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
25535 return;
25536 if (at_class_scope_p () && current_function_decl)
25538 /* 14.5.2.2 [temp.mem]
25540 A local class shall not have member templates. */
25541 error_at (location,
25542 "invalid declaration of member template in local class");
25543 cp_parser_skip_to_end_of_block_or_statement (parser);
25544 return;
25546 /* [temp]
25548 A template ... shall not have C linkage. */
25549 if (current_lang_name == lang_name_c)
25551 error_at (location, "template with C linkage");
25552 /* Give it C++ linkage to avoid confusing other parts of the
25553 front end. */
25554 push_lang_context (lang_name_cplusplus);
25555 need_lang_pop = true;
25557 else
25558 need_lang_pop = false;
25560 /* We cannot perform access checks on the template parameter
25561 declarations until we know what is being declared, just as we
25562 cannot check the decl-specifier list. */
25563 push_deferring_access_checks (dk_deferred);
25565 /* If the next token is `>', then we have an invalid
25566 specialization. Rather than complain about an invalid template
25567 parameter, issue an error message here. */
25568 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
25570 cp_parser_error (parser, "invalid explicit specialization");
25571 begin_specialization ();
25572 parameter_list = NULL_TREE;
25574 else
25576 /* Parse the template parameters. */
25577 parameter_list = cp_parser_template_parameter_list (parser);
25580 /* Look for the `>'. */
25581 cp_parser_skip_to_end_of_template_parameter_list (parser);
25583 /* Manage template requirements */
25584 if (flag_concepts)
25586 tree reqs = get_shorthand_constraints (current_template_parms);
25587 if (tree r = cp_parser_requires_clause_opt (parser))
25588 reqs = conjoin_constraints (reqs, make_predicate_constraint (r));
25589 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
25592 cp_parser_template_declaration_after_parameters (parser, parameter_list,
25593 member_p);
25595 /* For the erroneous case of a template with C linkage, we pushed an
25596 implicit C++ linkage scope; exit that scope now. */
25597 if (need_lang_pop)
25598 pop_lang_context ();
25601 /* Parse a template-declaration, assuming that the `export' (and
25602 `extern') keywords, if present, has already been scanned. MEMBER_P
25603 is as for cp_parser_template_declaration. */
25605 static bool
25606 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
25608 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25610 cp_lexer_consume_token (parser->lexer);
25611 cp_parser_explicit_template_declaration (parser, member_p);
25612 return true;
25614 else if (flag_concepts)
25615 return cp_parser_template_introduction (parser, member_p);
25617 return false;
25620 /* Perform the deferred access checks from a template-parameter-list.
25621 CHECKS is a TREE_LIST of access checks, as returned by
25622 get_deferred_access_checks. */
25624 static void
25625 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
25627 ++processing_template_parmlist;
25628 perform_access_checks (checks, tf_warning_or_error);
25629 --processing_template_parmlist;
25632 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
25633 `function-definition' sequence that follows a template header.
25634 If MEMBER_P is true, this declaration appears in a class scope.
25636 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
25637 *FRIEND_P is set to TRUE iff the declaration is a friend. */
25639 static tree
25640 cp_parser_single_declaration (cp_parser* parser,
25641 vec<deferred_access_check, va_gc> *checks,
25642 bool member_p,
25643 bool explicit_specialization_p,
25644 bool* friend_p)
25646 int declares_class_or_enum;
25647 tree decl = NULL_TREE;
25648 cp_decl_specifier_seq decl_specifiers;
25649 bool function_definition_p = false;
25650 cp_token *decl_spec_token_start;
25652 /* This function is only used when processing a template
25653 declaration. */
25654 gcc_assert (innermost_scope_kind () == sk_template_parms
25655 || innermost_scope_kind () == sk_template_spec);
25657 /* Defer access checks until we know what is being declared. */
25658 push_deferring_access_checks (dk_deferred);
25660 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
25661 alternative. */
25662 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
25663 cp_parser_decl_specifier_seq (parser,
25664 CP_PARSER_FLAGS_OPTIONAL,
25665 &decl_specifiers,
25666 &declares_class_or_enum);
25667 if (friend_p)
25668 *friend_p = cp_parser_friend_p (&decl_specifiers);
25670 /* There are no template typedefs. */
25671 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
25673 error_at (decl_spec_token_start->location,
25674 "template declaration of %<typedef%>");
25675 decl = error_mark_node;
25678 /* Gather up the access checks that occurred the
25679 decl-specifier-seq. */
25680 stop_deferring_access_checks ();
25682 /* Check for the declaration of a template class. */
25683 if (declares_class_or_enum)
25685 if (cp_parser_declares_only_class_p (parser)
25686 || (declares_class_or_enum & 2))
25688 // If this is a declaration, but not a definition, associate
25689 // any constraints with the type declaration. Constraints
25690 // are associated with definitions in cp_parser_class_specifier.
25691 if (declares_class_or_enum == 1)
25692 associate_classtype_constraints (decl_specifiers.type);
25694 decl = shadow_tag (&decl_specifiers);
25696 /* In this case:
25698 struct C {
25699 friend template <typename T> struct A<T>::B;
25702 A<T>::B will be represented by a TYPENAME_TYPE, and
25703 therefore not recognized by shadow_tag. */
25704 if (friend_p && *friend_p
25705 && !decl
25706 && decl_specifiers.type
25707 && TYPE_P (decl_specifiers.type))
25708 decl = decl_specifiers.type;
25710 if (decl && decl != error_mark_node)
25711 decl = TYPE_NAME (decl);
25712 else
25713 decl = error_mark_node;
25715 /* Perform access checks for template parameters. */
25716 cp_parser_perform_template_parameter_access_checks (checks);
25718 /* Give a helpful diagnostic for
25719 template <class T> struct A { } a;
25720 if we aren't already recovering from an error. */
25721 if (!cp_parser_declares_only_class_p (parser)
25722 && !seen_error ())
25724 error_at (cp_lexer_peek_token (parser->lexer)->location,
25725 "a class template declaration must not declare "
25726 "anything else");
25727 cp_parser_skip_to_end_of_block_or_statement (parser);
25728 goto out;
25733 /* Complain about missing 'typename' or other invalid type names. */
25734 if (!decl_specifiers.any_type_specifiers_p
25735 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
25737 /* cp_parser_parse_and_diagnose_invalid_type_name calls
25738 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
25739 the rest of this declaration. */
25740 decl = error_mark_node;
25741 goto out;
25744 /* If it's not a template class, try for a template function. If
25745 the next token is a `;', then this declaration does not declare
25746 anything. But, if there were errors in the decl-specifiers, then
25747 the error might well have come from an attempted class-specifier.
25748 In that case, there's no need to warn about a missing declarator. */
25749 if (!decl
25750 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
25751 || decl_specifiers.type != error_mark_node))
25753 decl = cp_parser_init_declarator (parser,
25754 &decl_specifiers,
25755 checks,
25756 /*function_definition_allowed_p=*/true,
25757 member_p,
25758 declares_class_or_enum,
25759 &function_definition_p,
25760 NULL, NULL);
25762 /* 7.1.1-1 [dcl.stc]
25764 A storage-class-specifier shall not be specified in an explicit
25765 specialization... */
25766 if (decl
25767 && explicit_specialization_p
25768 && decl_specifiers.storage_class != sc_none)
25770 error_at (decl_spec_token_start->location,
25771 "explicit template specialization cannot have a storage class");
25772 decl = error_mark_node;
25775 if (decl && VAR_P (decl))
25776 check_template_variable (decl);
25779 /* Look for a trailing `;' after the declaration. */
25780 if (!function_definition_p
25781 && (decl == error_mark_node
25782 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
25783 cp_parser_skip_to_end_of_block_or_statement (parser);
25785 out:
25786 pop_deferring_access_checks ();
25788 /* Clear any current qualification; whatever comes next is the start
25789 of something new. */
25790 parser->scope = NULL_TREE;
25791 parser->qualifying_scope = NULL_TREE;
25792 parser->object_scope = NULL_TREE;
25794 return decl;
25797 /* Parse a cast-expression that is not the operand of a unary "&". */
25799 static cp_expr
25800 cp_parser_simple_cast_expression (cp_parser *parser)
25802 return cp_parser_cast_expression (parser, /*address_p=*/false,
25803 /*cast_p=*/false, /*decltype*/false, NULL);
25806 /* Parse a functional cast to TYPE. Returns an expression
25807 representing the cast. */
25809 static cp_expr
25810 cp_parser_functional_cast (cp_parser* parser, tree type)
25812 vec<tree, va_gc> *vec;
25813 tree expression_list;
25814 cp_expr cast;
25815 bool nonconst_p;
25817 location_t start_loc = input_location;
25819 if (!type)
25820 type = error_mark_node;
25822 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25824 cp_lexer_set_source_position (parser->lexer);
25825 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25826 expression_list = cp_parser_braced_list (parser, &nonconst_p);
25827 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
25828 if (TREE_CODE (type) == TYPE_DECL)
25829 type = TREE_TYPE (type);
25831 cast = finish_compound_literal (type, expression_list,
25832 tf_warning_or_error);
25833 /* Create a location of the form:
25834 type_name{i, f}
25835 ^~~~~~~~~~~~~~~
25836 with caret == start at the start of the type name,
25837 finishing at the closing brace. */
25838 location_t finish_loc
25839 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
25840 location_t combined_loc = make_location (start_loc, start_loc,
25841 finish_loc);
25842 cast.set_location (combined_loc);
25843 return cast;
25847 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
25848 /*cast_p=*/true,
25849 /*allow_expansion_p=*/true,
25850 /*non_constant_p=*/NULL);
25851 if (vec == NULL)
25852 expression_list = error_mark_node;
25853 else
25855 expression_list = build_tree_list_vec (vec);
25856 release_tree_vector (vec);
25859 cast = build_functional_cast (type, expression_list,
25860 tf_warning_or_error);
25861 /* [expr.const]/1: In an integral constant expression "only type
25862 conversions to integral or enumeration type can be used". */
25863 if (TREE_CODE (type) == TYPE_DECL)
25864 type = TREE_TYPE (type);
25865 if (cast != error_mark_node
25866 && !cast_valid_in_integral_constant_expression_p (type)
25867 && cp_parser_non_integral_constant_expression (parser,
25868 NIC_CONSTRUCTOR))
25869 return error_mark_node;
25871 /* Create a location of the form:
25872 float(i)
25873 ^~~~~~~~
25874 with caret == start at the start of the type name,
25875 finishing at the closing paren. */
25876 location_t finish_loc
25877 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
25878 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
25879 cast.set_location (combined_loc);
25880 return cast;
25883 /* Save the tokens that make up the body of a member function defined
25884 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
25885 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
25886 specifiers applied to the declaration. Returns the FUNCTION_DECL
25887 for the member function. */
25889 static tree
25890 cp_parser_save_member_function_body (cp_parser* parser,
25891 cp_decl_specifier_seq *decl_specifiers,
25892 cp_declarator *declarator,
25893 tree attributes)
25895 cp_token *first;
25896 cp_token *last;
25897 tree fn;
25899 /* Create the FUNCTION_DECL. */
25900 fn = grokmethod (decl_specifiers, declarator, attributes);
25901 cp_finalize_omp_declare_simd (parser, fn);
25902 cp_finalize_oacc_routine (parser, fn, true);
25903 /* If something went badly wrong, bail out now. */
25904 if (fn == error_mark_node)
25906 /* If there's a function-body, skip it. */
25907 if (cp_parser_token_starts_function_definition_p
25908 (cp_lexer_peek_token (parser->lexer)))
25909 cp_parser_skip_to_end_of_block_or_statement (parser);
25910 return error_mark_node;
25913 /* Remember it, if there default args to post process. */
25914 cp_parser_save_default_args (parser, fn);
25916 /* Save away the tokens that make up the body of the
25917 function. */
25918 first = parser->lexer->next_token;
25919 /* Handle function try blocks. */
25920 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
25921 cp_lexer_consume_token (parser->lexer);
25922 /* We can have braced-init-list mem-initializers before the fn body. */
25923 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
25925 cp_lexer_consume_token (parser->lexer);
25926 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
25928 /* cache_group will stop after an un-nested { } pair, too. */
25929 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
25930 break;
25932 /* variadic mem-inits have ... after the ')'. */
25933 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25934 cp_lexer_consume_token (parser->lexer);
25937 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25938 /* Handle function try blocks. */
25939 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
25940 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25941 last = parser->lexer->next_token;
25943 /* Save away the inline definition; we will process it when the
25944 class is complete. */
25945 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
25946 DECL_PENDING_INLINE_P (fn) = 1;
25948 /* We need to know that this was defined in the class, so that
25949 friend templates are handled correctly. */
25950 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
25952 /* Add FN to the queue of functions to be parsed later. */
25953 vec_safe_push (unparsed_funs_with_definitions, fn);
25955 return fn;
25958 /* Save the tokens that make up the in-class initializer for a non-static
25959 data member. Returns a DEFAULT_ARG. */
25961 static tree
25962 cp_parser_save_nsdmi (cp_parser* parser)
25964 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
25967 /* Parse a template-argument-list, as well as the trailing ">" (but
25968 not the opening "<"). See cp_parser_template_argument_list for the
25969 return value. */
25971 static tree
25972 cp_parser_enclosed_template_argument_list (cp_parser* parser)
25974 tree arguments;
25975 tree saved_scope;
25976 tree saved_qualifying_scope;
25977 tree saved_object_scope;
25978 bool saved_greater_than_is_operator_p;
25979 int saved_unevaluated_operand;
25980 int saved_inhibit_evaluation_warnings;
25982 /* [temp.names]
25984 When parsing a template-id, the first non-nested `>' is taken as
25985 the end of the template-argument-list rather than a greater-than
25986 operator. */
25987 saved_greater_than_is_operator_p
25988 = parser->greater_than_is_operator_p;
25989 parser->greater_than_is_operator_p = false;
25990 /* Parsing the argument list may modify SCOPE, so we save it
25991 here. */
25992 saved_scope = parser->scope;
25993 saved_qualifying_scope = parser->qualifying_scope;
25994 saved_object_scope = parser->object_scope;
25995 /* We need to evaluate the template arguments, even though this
25996 template-id may be nested within a "sizeof". */
25997 saved_unevaluated_operand = cp_unevaluated_operand;
25998 cp_unevaluated_operand = 0;
25999 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
26000 c_inhibit_evaluation_warnings = 0;
26001 /* Parse the template-argument-list itself. */
26002 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
26003 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26004 arguments = NULL_TREE;
26005 else
26006 arguments = cp_parser_template_argument_list (parser);
26007 /* Look for the `>' that ends the template-argument-list. If we find
26008 a '>>' instead, it's probably just a typo. */
26009 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26011 if (cxx_dialect != cxx98)
26013 /* In C++0x, a `>>' in a template argument list or cast
26014 expression is considered to be two separate `>'
26015 tokens. So, change the current token to a `>', but don't
26016 consume it: it will be consumed later when the outer
26017 template argument list (or cast expression) is parsed.
26018 Note that this replacement of `>' for `>>' is necessary
26019 even if we are parsing tentatively: in the tentative
26020 case, after calling
26021 cp_parser_enclosed_template_argument_list we will always
26022 throw away all of the template arguments and the first
26023 closing `>', either because the template argument list
26024 was erroneous or because we are replacing those tokens
26025 with a CPP_TEMPLATE_ID token. The second `>' (which will
26026 not have been thrown away) is needed either to close an
26027 outer template argument list or to complete a new-style
26028 cast. */
26029 cp_token *token = cp_lexer_peek_token (parser->lexer);
26030 token->type = CPP_GREATER;
26032 else if (!saved_greater_than_is_operator_p)
26034 /* If we're in a nested template argument list, the '>>' has
26035 to be a typo for '> >'. We emit the error message, but we
26036 continue parsing and we push a '>' as next token, so that
26037 the argument list will be parsed correctly. Note that the
26038 global source location is still on the token before the
26039 '>>', so we need to say explicitly where we want it. */
26040 cp_token *token = cp_lexer_peek_token (parser->lexer);
26041 error_at (token->location, "%<>>%> should be %<> >%> "
26042 "within a nested template argument list");
26044 token->type = CPP_GREATER;
26046 else
26048 /* If this is not a nested template argument list, the '>>'
26049 is a typo for '>'. Emit an error message and continue.
26050 Same deal about the token location, but here we can get it
26051 right by consuming the '>>' before issuing the diagnostic. */
26052 cp_token *token = cp_lexer_consume_token (parser->lexer);
26053 error_at (token->location,
26054 "spurious %<>>%>, use %<>%> to terminate "
26055 "a template argument list");
26058 else
26059 cp_parser_skip_to_end_of_template_parameter_list (parser);
26060 /* The `>' token might be a greater-than operator again now. */
26061 parser->greater_than_is_operator_p
26062 = saved_greater_than_is_operator_p;
26063 /* Restore the SAVED_SCOPE. */
26064 parser->scope = saved_scope;
26065 parser->qualifying_scope = saved_qualifying_scope;
26066 parser->object_scope = saved_object_scope;
26067 cp_unevaluated_operand = saved_unevaluated_operand;
26068 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
26070 return arguments;
26073 /* MEMBER_FUNCTION is a member function, or a friend. If default
26074 arguments, or the body of the function have not yet been parsed,
26075 parse them now. */
26077 static void
26078 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
26080 timevar_push (TV_PARSE_INMETH);
26081 /* If this member is a template, get the underlying
26082 FUNCTION_DECL. */
26083 if (DECL_FUNCTION_TEMPLATE_P (member_function))
26084 member_function = DECL_TEMPLATE_RESULT (member_function);
26086 /* There should not be any class definitions in progress at this
26087 point; the bodies of members are only parsed outside of all class
26088 definitions. */
26089 gcc_assert (parser->num_classes_being_defined == 0);
26090 /* While we're parsing the member functions we might encounter more
26091 classes. We want to handle them right away, but we don't want
26092 them getting mixed up with functions that are currently in the
26093 queue. */
26094 push_unparsed_function_queues (parser);
26096 /* Make sure that any template parameters are in scope. */
26097 maybe_begin_member_template_processing (member_function);
26099 /* If the body of the function has not yet been parsed, parse it
26100 now. */
26101 if (DECL_PENDING_INLINE_P (member_function))
26103 tree function_scope;
26104 cp_token_cache *tokens;
26106 /* The function is no longer pending; we are processing it. */
26107 tokens = DECL_PENDING_INLINE_INFO (member_function);
26108 DECL_PENDING_INLINE_INFO (member_function) = NULL;
26109 DECL_PENDING_INLINE_P (member_function) = 0;
26111 /* If this is a local class, enter the scope of the containing
26112 function. */
26113 function_scope = current_function_decl;
26114 if (function_scope)
26115 push_function_context ();
26117 /* Push the body of the function onto the lexer stack. */
26118 cp_parser_push_lexer_for_tokens (parser, tokens);
26120 /* Let the front end know that we going to be defining this
26121 function. */
26122 start_preparsed_function (member_function, NULL_TREE,
26123 SF_PRE_PARSED | SF_INCLASS_INLINE);
26125 /* Don't do access checking if it is a templated function. */
26126 if (processing_template_decl)
26127 push_deferring_access_checks (dk_no_check);
26129 /* #pragma omp declare reduction needs special parsing. */
26130 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
26132 parser->lexer->in_pragma = true;
26133 cp_parser_omp_declare_reduction_exprs (member_function, parser);
26134 finish_function (/*inline*/2);
26135 cp_check_omp_declare_reduction (member_function);
26137 else
26138 /* Now, parse the body of the function. */
26139 cp_parser_function_definition_after_declarator (parser,
26140 /*inline_p=*/true);
26142 if (processing_template_decl)
26143 pop_deferring_access_checks ();
26145 /* Leave the scope of the containing function. */
26146 if (function_scope)
26147 pop_function_context ();
26148 cp_parser_pop_lexer (parser);
26151 /* Remove any template parameters from the symbol table. */
26152 maybe_end_member_template_processing ();
26154 /* Restore the queue. */
26155 pop_unparsed_function_queues (parser);
26156 timevar_pop (TV_PARSE_INMETH);
26159 /* If DECL contains any default args, remember it on the unparsed
26160 functions queue. */
26162 static void
26163 cp_parser_save_default_args (cp_parser* parser, tree decl)
26165 tree probe;
26167 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
26168 probe;
26169 probe = TREE_CHAIN (probe))
26170 if (TREE_PURPOSE (probe))
26172 cp_default_arg_entry entry = {current_class_type, decl};
26173 vec_safe_push (unparsed_funs_with_default_args, entry);
26174 break;
26178 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
26179 which is either a FIELD_DECL or PARM_DECL. Parse it and return
26180 the result. For a PARM_DECL, PARMTYPE is the corresponding type
26181 from the parameter-type-list. */
26183 static tree
26184 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
26185 tree default_arg, tree parmtype)
26187 cp_token_cache *tokens;
26188 tree parsed_arg;
26189 bool dummy;
26191 if (default_arg == error_mark_node)
26192 return error_mark_node;
26194 /* Push the saved tokens for the default argument onto the parser's
26195 lexer stack. */
26196 tokens = DEFARG_TOKENS (default_arg);
26197 cp_parser_push_lexer_for_tokens (parser, tokens);
26199 start_lambda_scope (decl);
26201 /* Parse the default argument. */
26202 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
26203 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
26204 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26206 finish_lambda_scope ();
26208 if (parsed_arg == error_mark_node)
26209 cp_parser_skip_to_end_of_statement (parser);
26211 if (!processing_template_decl)
26213 /* In a non-template class, check conversions now. In a template,
26214 we'll wait and instantiate these as needed. */
26215 if (TREE_CODE (decl) == PARM_DECL)
26216 parsed_arg = check_default_argument (parmtype, parsed_arg,
26217 tf_warning_or_error);
26218 else
26219 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
26222 /* If the token stream has not been completely used up, then
26223 there was extra junk after the end of the default
26224 argument. */
26225 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26227 if (TREE_CODE (decl) == PARM_DECL)
26228 cp_parser_error (parser, "expected %<,%>");
26229 else
26230 cp_parser_error (parser, "expected %<;%>");
26233 /* Revert to the main lexer. */
26234 cp_parser_pop_lexer (parser);
26236 return parsed_arg;
26239 /* FIELD is a non-static data member with an initializer which we saved for
26240 later; parse it now. */
26242 static void
26243 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
26245 tree def;
26247 maybe_begin_member_template_processing (field);
26249 push_unparsed_function_queues (parser);
26250 def = cp_parser_late_parse_one_default_arg (parser, field,
26251 DECL_INITIAL (field),
26252 NULL_TREE);
26253 pop_unparsed_function_queues (parser);
26255 maybe_end_member_template_processing ();
26257 DECL_INITIAL (field) = def;
26260 /* FN is a FUNCTION_DECL which may contains a parameter with an
26261 unparsed DEFAULT_ARG. Parse the default args now. This function
26262 assumes that the current scope is the scope in which the default
26263 argument should be processed. */
26265 static void
26266 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
26268 bool saved_local_variables_forbidden_p;
26269 tree parm, parmdecl;
26271 /* While we're parsing the default args, we might (due to the
26272 statement expression extension) encounter more classes. We want
26273 to handle them right away, but we don't want them getting mixed
26274 up with default args that are currently in the queue. */
26275 push_unparsed_function_queues (parser);
26277 /* Local variable names (and the `this' keyword) may not appear
26278 in a default argument. */
26279 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
26280 parser->local_variables_forbidden_p = true;
26282 push_defarg_context (fn);
26284 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
26285 parmdecl = DECL_ARGUMENTS (fn);
26286 parm && parm != void_list_node;
26287 parm = TREE_CHAIN (parm),
26288 parmdecl = DECL_CHAIN (parmdecl))
26290 tree default_arg = TREE_PURPOSE (parm);
26291 tree parsed_arg;
26292 vec<tree, va_gc> *insts;
26293 tree copy;
26294 unsigned ix;
26296 if (!default_arg)
26297 continue;
26299 if (TREE_CODE (default_arg) != DEFAULT_ARG)
26300 /* This can happen for a friend declaration for a function
26301 already declared with default arguments. */
26302 continue;
26304 parsed_arg
26305 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
26306 default_arg,
26307 TREE_VALUE (parm));
26308 if (parsed_arg == error_mark_node)
26310 continue;
26313 TREE_PURPOSE (parm) = parsed_arg;
26315 /* Update any instantiations we've already created. */
26316 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
26317 vec_safe_iterate (insts, ix, &copy); ix++)
26318 TREE_PURPOSE (copy) = parsed_arg;
26321 pop_defarg_context ();
26323 /* Make sure no default arg is missing. */
26324 check_default_args (fn);
26326 /* Restore the state of local_variables_forbidden_p. */
26327 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
26329 /* Restore the queue. */
26330 pop_unparsed_function_queues (parser);
26333 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
26335 sizeof ... ( identifier )
26337 where the 'sizeof' token has already been consumed. */
26339 static tree
26340 cp_parser_sizeof_pack (cp_parser *parser)
26342 /* Consume the `...'. */
26343 cp_lexer_consume_token (parser->lexer);
26344 maybe_warn_variadic_templates ();
26346 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
26347 if (paren)
26348 cp_lexer_consume_token (parser->lexer);
26349 else
26350 permerror (cp_lexer_peek_token (parser->lexer)->location,
26351 "%<sizeof...%> argument must be surrounded by parentheses");
26353 cp_token *token = cp_lexer_peek_token (parser->lexer);
26354 tree name = cp_parser_identifier (parser);
26355 if (name == error_mark_node)
26356 return error_mark_node;
26357 /* The name is not qualified. */
26358 parser->scope = NULL_TREE;
26359 parser->qualifying_scope = NULL_TREE;
26360 parser->object_scope = NULL_TREE;
26361 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
26362 if (expr == error_mark_node)
26363 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
26364 token->location);
26365 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
26366 expr = TREE_TYPE (expr);
26367 else if (TREE_CODE (expr) == CONST_DECL)
26368 expr = DECL_INITIAL (expr);
26369 expr = make_pack_expansion (expr);
26370 PACK_EXPANSION_SIZEOF_P (expr) = true;
26372 if (paren)
26373 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26375 return expr;
26378 /* Parse the operand of `sizeof' (or a similar operator). Returns
26379 either a TYPE or an expression, depending on the form of the
26380 input. The KEYWORD indicates which kind of expression we have
26381 encountered. */
26383 static tree
26384 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
26386 tree expr = NULL_TREE;
26387 const char *saved_message;
26388 char *tmp;
26389 bool saved_integral_constant_expression_p;
26390 bool saved_non_integral_constant_expression_p;
26392 /* If it's a `...', then we are computing the length of a parameter
26393 pack. */
26394 if (keyword == RID_SIZEOF
26395 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26396 return cp_parser_sizeof_pack (parser);
26398 /* Types cannot be defined in a `sizeof' expression. Save away the
26399 old message. */
26400 saved_message = parser->type_definition_forbidden_message;
26401 /* And create the new one. */
26402 tmp = concat ("types may not be defined in %<",
26403 IDENTIFIER_POINTER (ridpointers[keyword]),
26404 "%> expressions", NULL);
26405 parser->type_definition_forbidden_message = tmp;
26407 /* The restrictions on constant-expressions do not apply inside
26408 sizeof expressions. */
26409 saved_integral_constant_expression_p
26410 = parser->integral_constant_expression_p;
26411 saved_non_integral_constant_expression_p
26412 = parser->non_integral_constant_expression_p;
26413 parser->integral_constant_expression_p = false;
26415 /* Do not actually evaluate the expression. */
26416 ++cp_unevaluated_operand;
26417 ++c_inhibit_evaluation_warnings;
26418 /* If it's a `(', then we might be looking at the type-id
26419 construction. */
26420 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26422 tree type = NULL_TREE;
26424 /* We can't be sure yet whether we're looking at a type-id or an
26425 expression. */
26426 cp_parser_parse_tentatively (parser);
26427 /* Note: as a GNU Extension, compound literals are considered
26428 postfix-expressions as they are in C99, so they are valid
26429 arguments to sizeof. See comment in cp_parser_cast_expression
26430 for details. */
26431 if (cp_parser_compound_literal_p (parser))
26432 cp_parser_simulate_error (parser);
26433 else
26435 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
26436 parser->in_type_id_in_expr_p = true;
26437 /* Look for the type-id. */
26438 type = cp_parser_type_id (parser);
26439 /* Look for the closing `)'. */
26440 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26441 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
26444 /* If all went well, then we're done. */
26445 if (cp_parser_parse_definitely (parser))
26447 cp_decl_specifier_seq decl_specs;
26449 /* Build a trivial decl-specifier-seq. */
26450 clear_decl_specs (&decl_specs);
26451 decl_specs.type = type;
26453 /* Call grokdeclarator to figure out what type this is. */
26454 expr = grokdeclarator (NULL,
26455 &decl_specs,
26456 TYPENAME,
26457 /*initialized=*/0,
26458 /*attrlist=*/NULL);
26462 /* If the type-id production did not work out, then we must be
26463 looking at the unary-expression production. */
26464 if (!expr)
26465 expr = cp_parser_unary_expression (parser);
26467 /* Go back to evaluating expressions. */
26468 --cp_unevaluated_operand;
26469 --c_inhibit_evaluation_warnings;
26471 /* Free the message we created. */
26472 free (tmp);
26473 /* And restore the old one. */
26474 parser->type_definition_forbidden_message = saved_message;
26475 parser->integral_constant_expression_p
26476 = saved_integral_constant_expression_p;
26477 parser->non_integral_constant_expression_p
26478 = saved_non_integral_constant_expression_p;
26480 return expr;
26483 /* If the current declaration has no declarator, return true. */
26485 static bool
26486 cp_parser_declares_only_class_p (cp_parser *parser)
26488 /* If the next token is a `;' or a `,' then there is no
26489 declarator. */
26490 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26491 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
26494 /* Update the DECL_SPECS to reflect the storage class indicated by
26495 KEYWORD. */
26497 static void
26498 cp_parser_set_storage_class (cp_parser *parser,
26499 cp_decl_specifier_seq *decl_specs,
26500 enum rid keyword,
26501 cp_token *token)
26503 cp_storage_class storage_class;
26505 if (parser->in_unbraced_linkage_specification_p)
26507 error_at (token->location, "invalid use of %qD in linkage specification",
26508 ridpointers[keyword]);
26509 return;
26511 else if (decl_specs->storage_class != sc_none)
26513 decl_specs->conflicting_specifiers_p = true;
26514 return;
26517 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
26518 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
26519 && decl_specs->gnu_thread_keyword_p)
26521 pedwarn (decl_specs->locations[ds_thread], 0,
26522 "%<__thread%> before %qD", ridpointers[keyword]);
26525 switch (keyword)
26527 case RID_AUTO:
26528 storage_class = sc_auto;
26529 break;
26530 case RID_REGISTER:
26531 storage_class = sc_register;
26532 break;
26533 case RID_STATIC:
26534 storage_class = sc_static;
26535 break;
26536 case RID_EXTERN:
26537 storage_class = sc_extern;
26538 break;
26539 case RID_MUTABLE:
26540 storage_class = sc_mutable;
26541 break;
26542 default:
26543 gcc_unreachable ();
26545 decl_specs->storage_class = storage_class;
26546 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
26548 /* A storage class specifier cannot be applied alongside a typedef
26549 specifier. If there is a typedef specifier present then set
26550 conflicting_specifiers_p which will trigger an error later
26551 on in grokdeclarator. */
26552 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
26553 decl_specs->conflicting_specifiers_p = true;
26556 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
26557 is true, the type is a class or enum definition. */
26559 static void
26560 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
26561 tree type_spec,
26562 cp_token *token,
26563 bool type_definition_p)
26565 decl_specs->any_specifiers_p = true;
26567 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
26568 (with, for example, in "typedef int wchar_t;") we remember that
26569 this is what happened. In system headers, we ignore these
26570 declarations so that G++ can work with system headers that are not
26571 C++-safe. */
26572 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
26573 && !type_definition_p
26574 && (type_spec == boolean_type_node
26575 || type_spec == char16_type_node
26576 || type_spec == char32_type_node
26577 || type_spec == wchar_type_node)
26578 && (decl_specs->type
26579 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
26580 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
26581 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
26582 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
26584 decl_specs->redefined_builtin_type = type_spec;
26585 set_and_check_decl_spec_loc (decl_specs,
26586 ds_redefined_builtin_type_spec,
26587 token);
26588 if (!decl_specs->type)
26590 decl_specs->type = type_spec;
26591 decl_specs->type_definition_p = false;
26592 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
26595 else if (decl_specs->type)
26596 decl_specs->multiple_types_p = true;
26597 else
26599 decl_specs->type = type_spec;
26600 decl_specs->type_definition_p = type_definition_p;
26601 decl_specs->redefined_builtin_type = NULL_TREE;
26602 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
26606 /* True iff TOKEN is the GNU keyword __thread. */
26608 static bool
26609 token_is__thread (cp_token *token)
26611 gcc_assert (token->keyword == RID_THREAD);
26612 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
26615 /* Set the location for a declarator specifier and check if it is
26616 duplicated.
26618 DECL_SPECS is the sequence of declarator specifiers onto which to
26619 set the location.
26621 DS is the single declarator specifier to set which location is to
26622 be set onto the existing sequence of declarators.
26624 LOCATION is the location for the declarator specifier to
26625 consider. */
26627 static void
26628 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
26629 cp_decl_spec ds, cp_token *token)
26631 gcc_assert (ds < ds_last);
26633 if (decl_specs == NULL)
26634 return;
26636 source_location location = token->location;
26638 if (decl_specs->locations[ds] == 0)
26640 decl_specs->locations[ds] = location;
26641 if (ds == ds_thread)
26642 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
26644 else
26646 if (ds == ds_long)
26648 if (decl_specs->locations[ds_long_long] != 0)
26649 error_at (location,
26650 "%<long long long%> is too long for GCC");
26651 else
26653 decl_specs->locations[ds_long_long] = location;
26654 pedwarn_cxx98 (location,
26655 OPT_Wlong_long,
26656 "ISO C++ 1998 does not support %<long long%>");
26659 else if (ds == ds_thread)
26661 bool gnu = token_is__thread (token);
26662 if (gnu != decl_specs->gnu_thread_keyword_p)
26663 error_at (location,
26664 "both %<__thread%> and %<thread_local%> specified");
26665 else
26666 error_at (location, "duplicate %qD", token->u.value);
26668 else
26670 static const char *const decl_spec_names[] = {
26671 "signed",
26672 "unsigned",
26673 "short",
26674 "long",
26675 "const",
26676 "volatile",
26677 "restrict",
26678 "inline",
26679 "virtual",
26680 "explicit",
26681 "friend",
26682 "typedef",
26683 "using",
26684 "constexpr",
26685 "__complex"
26687 error_at (location,
26688 "duplicate %qs", decl_spec_names[ds]);
26693 /* Return true iff the declarator specifier DS is present in the
26694 sequence of declarator specifiers DECL_SPECS. */
26696 bool
26697 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
26698 cp_decl_spec ds)
26700 gcc_assert (ds < ds_last);
26702 if (decl_specs == NULL)
26703 return false;
26705 return decl_specs->locations[ds] != 0;
26708 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
26709 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
26711 static bool
26712 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
26714 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
26717 /* Issue an error message indicating that TOKEN_DESC was expected.
26718 If KEYWORD is true, it indicated this function is called by
26719 cp_parser_require_keword and the required token can only be
26720 a indicated keyword. */
26722 static void
26723 cp_parser_required_error (cp_parser *parser,
26724 required_token token_desc,
26725 bool keyword)
26727 switch (token_desc)
26729 case RT_NEW:
26730 cp_parser_error (parser, "expected %<new%>");
26731 return;
26732 case RT_DELETE:
26733 cp_parser_error (parser, "expected %<delete%>");
26734 return;
26735 case RT_RETURN:
26736 cp_parser_error (parser, "expected %<return%>");
26737 return;
26738 case RT_WHILE:
26739 cp_parser_error (parser, "expected %<while%>");
26740 return;
26741 case RT_EXTERN:
26742 cp_parser_error (parser, "expected %<extern%>");
26743 return;
26744 case RT_STATIC_ASSERT:
26745 cp_parser_error (parser, "expected %<static_assert%>");
26746 return;
26747 case RT_DECLTYPE:
26748 cp_parser_error (parser, "expected %<decltype%>");
26749 return;
26750 case RT_OPERATOR:
26751 cp_parser_error (parser, "expected %<operator%>");
26752 return;
26753 case RT_CLASS:
26754 cp_parser_error (parser, "expected %<class%>");
26755 return;
26756 case RT_TEMPLATE:
26757 cp_parser_error (parser, "expected %<template%>");
26758 return;
26759 case RT_NAMESPACE:
26760 cp_parser_error (parser, "expected %<namespace%>");
26761 return;
26762 case RT_USING:
26763 cp_parser_error (parser, "expected %<using%>");
26764 return;
26765 case RT_ASM:
26766 cp_parser_error (parser, "expected %<asm%>");
26767 return;
26768 case RT_TRY:
26769 cp_parser_error (parser, "expected %<try%>");
26770 return;
26771 case RT_CATCH:
26772 cp_parser_error (parser, "expected %<catch%>");
26773 return;
26774 case RT_THROW:
26775 cp_parser_error (parser, "expected %<throw%>");
26776 return;
26777 case RT_LABEL:
26778 cp_parser_error (parser, "expected %<__label__%>");
26779 return;
26780 case RT_AT_TRY:
26781 cp_parser_error (parser, "expected %<@try%>");
26782 return;
26783 case RT_AT_SYNCHRONIZED:
26784 cp_parser_error (parser, "expected %<@synchronized%>");
26785 return;
26786 case RT_AT_THROW:
26787 cp_parser_error (parser, "expected %<@throw%>");
26788 return;
26789 case RT_TRANSACTION_ATOMIC:
26790 cp_parser_error (parser, "expected %<__transaction_atomic%>");
26791 return;
26792 case RT_TRANSACTION_RELAXED:
26793 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
26794 return;
26795 default:
26796 break;
26798 if (!keyword)
26800 switch (token_desc)
26802 case RT_SEMICOLON:
26803 cp_parser_error (parser, "expected %<;%>");
26804 return;
26805 case RT_OPEN_PAREN:
26806 cp_parser_error (parser, "expected %<(%>");
26807 return;
26808 case RT_CLOSE_BRACE:
26809 cp_parser_error (parser, "expected %<}%>");
26810 return;
26811 case RT_OPEN_BRACE:
26812 cp_parser_error (parser, "expected %<{%>");
26813 return;
26814 case RT_CLOSE_SQUARE:
26815 cp_parser_error (parser, "expected %<]%>");
26816 return;
26817 case RT_OPEN_SQUARE:
26818 cp_parser_error (parser, "expected %<[%>");
26819 return;
26820 case RT_COMMA:
26821 cp_parser_error (parser, "expected %<,%>");
26822 return;
26823 case RT_SCOPE:
26824 cp_parser_error (parser, "expected %<::%>");
26825 return;
26826 case RT_LESS:
26827 cp_parser_error (parser, "expected %<<%>");
26828 return;
26829 case RT_GREATER:
26830 cp_parser_error (parser, "expected %<>%>");
26831 return;
26832 case RT_EQ:
26833 cp_parser_error (parser, "expected %<=%>");
26834 return;
26835 case RT_ELLIPSIS:
26836 cp_parser_error (parser, "expected %<...%>");
26837 return;
26838 case RT_MULT:
26839 cp_parser_error (parser, "expected %<*%>");
26840 return;
26841 case RT_COMPL:
26842 cp_parser_error (parser, "expected %<~%>");
26843 return;
26844 case RT_COLON:
26845 cp_parser_error (parser, "expected %<:%>");
26846 return;
26847 case RT_COLON_SCOPE:
26848 cp_parser_error (parser, "expected %<:%> or %<::%>");
26849 return;
26850 case RT_CLOSE_PAREN:
26851 cp_parser_error (parser, "expected %<)%>");
26852 return;
26853 case RT_COMMA_CLOSE_PAREN:
26854 cp_parser_error (parser, "expected %<,%> or %<)%>");
26855 return;
26856 case RT_PRAGMA_EOL:
26857 cp_parser_error (parser, "expected end of line");
26858 return;
26859 case RT_NAME:
26860 cp_parser_error (parser, "expected identifier");
26861 return;
26862 case RT_SELECT:
26863 cp_parser_error (parser, "expected selection-statement");
26864 return;
26865 case RT_INTERATION:
26866 cp_parser_error (parser, "expected iteration-statement");
26867 return;
26868 case RT_JUMP:
26869 cp_parser_error (parser, "expected jump-statement");
26870 return;
26871 case RT_CLASS_KEY:
26872 cp_parser_error (parser, "expected class-key");
26873 return;
26874 case RT_CLASS_TYPENAME_TEMPLATE:
26875 cp_parser_error (parser,
26876 "expected %<class%>, %<typename%>, or %<template%>");
26877 return;
26878 default:
26879 gcc_unreachable ();
26882 else
26883 gcc_unreachable ();
26888 /* If the next token is of the indicated TYPE, consume it. Otherwise,
26889 issue an error message indicating that TOKEN_DESC was expected.
26891 Returns the token consumed, if the token had the appropriate type.
26892 Otherwise, returns NULL. */
26894 static cp_token *
26895 cp_parser_require (cp_parser* parser,
26896 enum cpp_ttype type,
26897 required_token token_desc)
26899 if (cp_lexer_next_token_is (parser->lexer, type))
26900 return cp_lexer_consume_token (parser->lexer);
26901 else
26903 /* Output the MESSAGE -- unless we're parsing tentatively. */
26904 if (!cp_parser_simulate_error (parser))
26905 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
26906 return NULL;
26910 /* An error message is produced if the next token is not '>'.
26911 All further tokens are skipped until the desired token is
26912 found or '{', '}', ';' or an unbalanced ')' or ']'. */
26914 static void
26915 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
26917 /* Current level of '< ... >'. */
26918 unsigned level = 0;
26919 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
26920 unsigned nesting_depth = 0;
26922 /* Are we ready, yet? If not, issue error message. */
26923 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
26924 return;
26926 /* Skip tokens until the desired token is found. */
26927 while (true)
26929 /* Peek at the next token. */
26930 switch (cp_lexer_peek_token (parser->lexer)->type)
26932 case CPP_LESS:
26933 if (!nesting_depth)
26934 ++level;
26935 break;
26937 case CPP_RSHIFT:
26938 if (cxx_dialect == cxx98)
26939 /* C++0x views the `>>' operator as two `>' tokens, but
26940 C++98 does not. */
26941 break;
26942 else if (!nesting_depth && level-- == 0)
26944 /* We've hit a `>>' where the first `>' closes the
26945 template argument list, and the second `>' is
26946 spurious. Just consume the `>>' and stop; we've
26947 already produced at least one error. */
26948 cp_lexer_consume_token (parser->lexer);
26949 return;
26951 /* Fall through for C++0x, so we handle the second `>' in
26952 the `>>'. */
26954 case CPP_GREATER:
26955 if (!nesting_depth && level-- == 0)
26957 /* We've reached the token we want, consume it and stop. */
26958 cp_lexer_consume_token (parser->lexer);
26959 return;
26961 break;
26963 case CPP_OPEN_PAREN:
26964 case CPP_OPEN_SQUARE:
26965 ++nesting_depth;
26966 break;
26968 case CPP_CLOSE_PAREN:
26969 case CPP_CLOSE_SQUARE:
26970 if (nesting_depth-- == 0)
26971 return;
26972 break;
26974 case CPP_EOF:
26975 case CPP_PRAGMA_EOL:
26976 case CPP_SEMICOLON:
26977 case CPP_OPEN_BRACE:
26978 case CPP_CLOSE_BRACE:
26979 /* The '>' was probably forgotten, don't look further. */
26980 return;
26982 default:
26983 break;
26986 /* Consume this token. */
26987 cp_lexer_consume_token (parser->lexer);
26991 /* If the next token is the indicated keyword, consume it. Otherwise,
26992 issue an error message indicating that TOKEN_DESC was expected.
26994 Returns the token consumed, if the token had the appropriate type.
26995 Otherwise, returns NULL. */
26997 static cp_token *
26998 cp_parser_require_keyword (cp_parser* parser,
26999 enum rid keyword,
27000 required_token token_desc)
27002 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
27004 if (token && token->keyword != keyword)
27006 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
27007 return NULL;
27010 return token;
27013 /* Returns TRUE iff TOKEN is a token that can begin the body of a
27014 function-definition. */
27016 static bool
27017 cp_parser_token_starts_function_definition_p (cp_token* token)
27019 return (/* An ordinary function-body begins with an `{'. */
27020 token->type == CPP_OPEN_BRACE
27021 /* A ctor-initializer begins with a `:'. */
27022 || token->type == CPP_COLON
27023 /* A function-try-block begins with `try'. */
27024 || token->keyword == RID_TRY
27025 /* A function-transaction-block begins with `__transaction_atomic'
27026 or `__transaction_relaxed'. */
27027 || token->keyword == RID_TRANSACTION_ATOMIC
27028 || token->keyword == RID_TRANSACTION_RELAXED
27029 /* The named return value extension begins with `return'. */
27030 || token->keyword == RID_RETURN);
27033 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
27034 definition. */
27036 static bool
27037 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
27039 cp_token *token;
27041 token = cp_lexer_peek_token (parser->lexer);
27042 return (token->type == CPP_OPEN_BRACE
27043 || (token->type == CPP_COLON
27044 && !parser->colon_doesnt_start_class_def_p));
27047 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
27048 C++0x) ending a template-argument. */
27050 static bool
27051 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
27053 cp_token *token;
27055 token = cp_lexer_peek_token (parser->lexer);
27056 return (token->type == CPP_COMMA
27057 || token->type == CPP_GREATER
27058 || token->type == CPP_ELLIPSIS
27059 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
27062 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
27063 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
27065 static bool
27066 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
27067 size_t n)
27069 cp_token *token;
27071 token = cp_lexer_peek_nth_token (parser->lexer, n);
27072 if (token->type == CPP_LESS)
27073 return true;
27074 /* Check for the sequence `<::' in the original code. It would be lexed as
27075 `[:', where `[' is a digraph, and there is no whitespace before
27076 `:'. */
27077 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
27079 cp_token *token2;
27080 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
27081 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
27082 return true;
27084 return false;
27087 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
27088 or none_type otherwise. */
27090 static enum tag_types
27091 cp_parser_token_is_class_key (cp_token* token)
27093 switch (token->keyword)
27095 case RID_CLASS:
27096 return class_type;
27097 case RID_STRUCT:
27098 return record_type;
27099 case RID_UNION:
27100 return union_type;
27102 default:
27103 return none_type;
27107 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
27108 or none_type otherwise or if the token is null. */
27110 static enum tag_types
27111 cp_parser_token_is_type_parameter_key (cp_token* token)
27113 if (!token)
27114 return none_type;
27116 switch (token->keyword)
27118 case RID_CLASS:
27119 return class_type;
27120 case RID_TYPENAME:
27121 return typename_type;
27123 default:
27124 return none_type;
27128 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
27130 static void
27131 cp_parser_check_class_key (enum tag_types class_key, tree type)
27133 if (type == error_mark_node)
27134 return;
27135 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
27137 if (permerror (input_location, "%qs tag used in naming %q#T",
27138 class_key == union_type ? "union"
27139 : class_key == record_type ? "struct" : "class",
27140 type))
27141 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
27142 "%q#T was previously declared here", type);
27146 /* Issue an error message if DECL is redeclared with different
27147 access than its original declaration [class.access.spec/3].
27148 This applies to nested classes and nested class templates.
27149 [class.mem/1]. */
27151 static void
27152 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
27154 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
27155 return;
27157 if ((TREE_PRIVATE (decl)
27158 != (current_access_specifier == access_private_node))
27159 || (TREE_PROTECTED (decl)
27160 != (current_access_specifier == access_protected_node)))
27161 error_at (location, "%qD redeclared with different access", decl);
27164 /* Look for the `template' keyword, as a syntactic disambiguator.
27165 Return TRUE iff it is present, in which case it will be
27166 consumed. */
27168 static bool
27169 cp_parser_optional_template_keyword (cp_parser *parser)
27171 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27173 /* In C++98 the `template' keyword can only be used within templates;
27174 outside templates the parser can always figure out what is a
27175 template and what is not. In C++11, per the resolution of DR 468,
27176 `template' is allowed in cases where it is not strictly necessary. */
27177 if (!processing_template_decl
27178 && pedantic && cxx_dialect == cxx98)
27180 cp_token *token = cp_lexer_peek_token (parser->lexer);
27181 pedwarn (token->location, OPT_Wpedantic,
27182 "in C++98 %<template%> (as a disambiguator) is only "
27183 "allowed within templates");
27184 /* If this part of the token stream is rescanned, the same
27185 error message would be generated. So, we purge the token
27186 from the stream. */
27187 cp_lexer_purge_token (parser->lexer);
27188 return false;
27190 else
27192 /* Consume the `template' keyword. */
27193 cp_lexer_consume_token (parser->lexer);
27194 return true;
27197 return false;
27200 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
27201 set PARSER->SCOPE, and perform other related actions. */
27203 static void
27204 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
27206 struct tree_check *check_value;
27208 /* Get the stored value. */
27209 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
27210 /* Set the scope from the stored value. */
27211 parser->scope = saved_checks_value (check_value);
27212 parser->qualifying_scope = check_value->qualifying_scope;
27213 parser->object_scope = NULL_TREE;
27216 /* Consume tokens up through a non-nested END token. Returns TRUE if we
27217 encounter the end of a block before what we were looking for. */
27219 static bool
27220 cp_parser_cache_group (cp_parser *parser,
27221 enum cpp_ttype end,
27222 unsigned depth)
27224 while (true)
27226 cp_token *token = cp_lexer_peek_token (parser->lexer);
27228 /* Abort a parenthesized expression if we encounter a semicolon. */
27229 if ((end == CPP_CLOSE_PAREN || depth == 0)
27230 && token->type == CPP_SEMICOLON)
27231 return true;
27232 /* If we've reached the end of the file, stop. */
27233 if (token->type == CPP_EOF
27234 || (end != CPP_PRAGMA_EOL
27235 && token->type == CPP_PRAGMA_EOL))
27236 return true;
27237 if (token->type == CPP_CLOSE_BRACE && depth == 0)
27238 /* We've hit the end of an enclosing block, so there's been some
27239 kind of syntax error. */
27240 return true;
27242 /* Consume the token. */
27243 cp_lexer_consume_token (parser->lexer);
27244 /* See if it starts a new group. */
27245 if (token->type == CPP_OPEN_BRACE)
27247 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
27248 /* In theory this should probably check end == '}', but
27249 cp_parser_save_member_function_body needs it to exit
27250 after either '}' or ')' when called with ')'. */
27251 if (depth == 0)
27252 return false;
27254 else if (token->type == CPP_OPEN_PAREN)
27256 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
27257 if (depth == 0 && end == CPP_CLOSE_PAREN)
27258 return false;
27260 else if (token->type == CPP_PRAGMA)
27261 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
27262 else if (token->type == end)
27263 return false;
27267 /* Like above, for caching a default argument or NSDMI. Both of these are
27268 terminated by a non-nested comma, but it can be unclear whether or not a
27269 comma is nested in a template argument list unless we do more parsing.
27270 In order to handle this ambiguity, when we encounter a ',' after a '<'
27271 we try to parse what follows as a parameter-declaration-list (in the
27272 case of a default argument) or a member-declarator (in the case of an
27273 NSDMI). If that succeeds, then we stop caching. */
27275 static tree
27276 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
27278 unsigned depth = 0;
27279 int maybe_template_id = 0;
27280 cp_token *first_token;
27281 cp_token *token;
27282 tree default_argument;
27284 /* Add tokens until we have processed the entire default
27285 argument. We add the range [first_token, token). */
27286 first_token = cp_lexer_peek_token (parser->lexer);
27287 if (first_token->type == CPP_OPEN_BRACE)
27289 /* For list-initialization, this is straightforward. */
27290 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27291 token = cp_lexer_peek_token (parser->lexer);
27293 else while (true)
27295 bool done = false;
27297 /* Peek at the next token. */
27298 token = cp_lexer_peek_token (parser->lexer);
27299 /* What we do depends on what token we have. */
27300 switch (token->type)
27302 /* In valid code, a default argument must be
27303 immediately followed by a `,' `)', or `...'. */
27304 case CPP_COMMA:
27305 if (depth == 0 && maybe_template_id)
27307 /* If we've seen a '<', we might be in a
27308 template-argument-list. Until Core issue 325 is
27309 resolved, we don't know how this situation ought
27310 to be handled, so try to DTRT. We check whether
27311 what comes after the comma is a valid parameter
27312 declaration list. If it is, then the comma ends
27313 the default argument; otherwise the default
27314 argument continues. */
27315 bool error = false;
27316 cp_token *peek;
27318 /* Set ITALP so cp_parser_parameter_declaration_list
27319 doesn't decide to commit to this parse. */
27320 bool saved_italp = parser->in_template_argument_list_p;
27321 parser->in_template_argument_list_p = true;
27323 cp_parser_parse_tentatively (parser);
27325 if (nsdmi)
27327 /* Parse declarators until we reach a non-comma or
27328 somthing that cannot be an initializer.
27329 Just checking whether we're looking at a single
27330 declarator is insufficient. Consider:
27331 int var = tuple<T,U>::x;
27332 The template parameter 'U' looks exactly like a
27333 declarator. */
27336 int ctor_dtor_or_conv_p;
27337 cp_lexer_consume_token (parser->lexer);
27338 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27339 &ctor_dtor_or_conv_p,
27340 /*parenthesized_p=*/NULL,
27341 /*member_p=*/true,
27342 /*friend_p=*/false);
27343 peek = cp_lexer_peek_token (parser->lexer);
27344 if (cp_parser_error_occurred (parser))
27345 break;
27347 while (peek->type == CPP_COMMA);
27348 /* If we met an '=' or ';' then the original comma
27349 was the end of the NSDMI. Otherwise assume
27350 we're still in the NSDMI. */
27351 error = (peek->type != CPP_EQ
27352 && peek->type != CPP_SEMICOLON);
27354 else
27356 cp_lexer_consume_token (parser->lexer);
27357 begin_scope (sk_function_parms, NULL_TREE);
27358 cp_parser_parameter_declaration_list (parser, &error);
27359 pop_bindings_and_leave_scope ();
27361 if (!cp_parser_error_occurred (parser) && !error)
27362 done = true;
27363 cp_parser_abort_tentative_parse (parser);
27365 parser->in_template_argument_list_p = saved_italp;
27366 break;
27368 case CPP_CLOSE_PAREN:
27369 case CPP_ELLIPSIS:
27370 /* If we run into a non-nested `;', `}', or `]',
27371 then the code is invalid -- but the default
27372 argument is certainly over. */
27373 case CPP_SEMICOLON:
27374 case CPP_CLOSE_BRACE:
27375 case CPP_CLOSE_SQUARE:
27376 if (depth == 0
27377 /* Handle correctly int n = sizeof ... ( p ); */
27378 && token->type != CPP_ELLIPSIS)
27379 done = true;
27380 /* Update DEPTH, if necessary. */
27381 else if (token->type == CPP_CLOSE_PAREN
27382 || token->type == CPP_CLOSE_BRACE
27383 || token->type == CPP_CLOSE_SQUARE)
27384 --depth;
27385 break;
27387 case CPP_OPEN_PAREN:
27388 case CPP_OPEN_SQUARE:
27389 case CPP_OPEN_BRACE:
27390 ++depth;
27391 break;
27393 case CPP_LESS:
27394 if (depth == 0)
27395 /* This might be the comparison operator, or it might
27396 start a template argument list. */
27397 ++maybe_template_id;
27398 break;
27400 case CPP_RSHIFT:
27401 if (cxx_dialect == cxx98)
27402 break;
27403 /* Fall through for C++0x, which treats the `>>'
27404 operator like two `>' tokens in certain
27405 cases. */
27407 case CPP_GREATER:
27408 if (depth == 0)
27410 /* This might be an operator, or it might close a
27411 template argument list. But if a previous '<'
27412 started a template argument list, this will have
27413 closed it, so we can't be in one anymore. */
27414 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
27415 if (maybe_template_id < 0)
27416 maybe_template_id = 0;
27418 break;
27420 /* If we run out of tokens, issue an error message. */
27421 case CPP_EOF:
27422 case CPP_PRAGMA_EOL:
27423 error_at (token->location, "file ends in default argument");
27424 done = true;
27425 break;
27427 case CPP_NAME:
27428 case CPP_SCOPE:
27429 /* In these cases, we should look for template-ids.
27430 For example, if the default argument is
27431 `X<int, double>()', we need to do name lookup to
27432 figure out whether or not `X' is a template; if
27433 so, the `,' does not end the default argument.
27435 That is not yet done. */
27436 break;
27438 default:
27439 break;
27442 /* If we've reached the end, stop. */
27443 if (done)
27444 break;
27446 /* Add the token to the token block. */
27447 token = cp_lexer_consume_token (parser->lexer);
27450 /* Create a DEFAULT_ARG to represent the unparsed default
27451 argument. */
27452 default_argument = make_node (DEFAULT_ARG);
27453 DEFARG_TOKENS (default_argument)
27454 = cp_token_cache_new (first_token, token);
27455 DEFARG_INSTANTIATIONS (default_argument) = NULL;
27457 return default_argument;
27460 /* Begin parsing tentatively. We always save tokens while parsing
27461 tentatively so that if the tentative parsing fails we can restore the
27462 tokens. */
27464 static void
27465 cp_parser_parse_tentatively (cp_parser* parser)
27467 /* Enter a new parsing context. */
27468 parser->context = cp_parser_context_new (parser->context);
27469 /* Begin saving tokens. */
27470 cp_lexer_save_tokens (parser->lexer);
27471 /* In order to avoid repetitive access control error messages,
27472 access checks are queued up until we are no longer parsing
27473 tentatively. */
27474 push_deferring_access_checks (dk_deferred);
27477 /* Commit to the currently active tentative parse. */
27479 static void
27480 cp_parser_commit_to_tentative_parse (cp_parser* parser)
27482 cp_parser_context *context;
27483 cp_lexer *lexer;
27485 /* Mark all of the levels as committed. */
27486 lexer = parser->lexer;
27487 for (context = parser->context; context->next; context = context->next)
27489 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
27490 break;
27491 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
27492 while (!cp_lexer_saving_tokens (lexer))
27493 lexer = lexer->next;
27494 cp_lexer_commit_tokens (lexer);
27498 /* Commit to the topmost currently active tentative parse.
27500 Note that this function shouldn't be called when there are
27501 irreversible side-effects while in a tentative state. For
27502 example, we shouldn't create a permanent entry in the symbol
27503 table, or issue an error message that might not apply if the
27504 tentative parse is aborted. */
27506 static void
27507 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
27509 cp_parser_context *context = parser->context;
27510 cp_lexer *lexer = parser->lexer;
27512 if (context)
27514 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
27515 return;
27516 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
27518 while (!cp_lexer_saving_tokens (lexer))
27519 lexer = lexer->next;
27520 cp_lexer_commit_tokens (lexer);
27524 /* Abort the currently active tentative parse. All consumed tokens
27525 will be rolled back, and no diagnostics will be issued. */
27527 static void
27528 cp_parser_abort_tentative_parse (cp_parser* parser)
27530 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
27531 || errorcount > 0);
27532 cp_parser_simulate_error (parser);
27533 /* Now, pretend that we want to see if the construct was
27534 successfully parsed. */
27535 cp_parser_parse_definitely (parser);
27538 /* Stop parsing tentatively. If a parse error has occurred, restore the
27539 token stream. Otherwise, commit to the tokens we have consumed.
27540 Returns true if no error occurred; false otherwise. */
27542 static bool
27543 cp_parser_parse_definitely (cp_parser* parser)
27545 bool error_occurred;
27546 cp_parser_context *context;
27548 /* Remember whether or not an error occurred, since we are about to
27549 destroy that information. */
27550 error_occurred = cp_parser_error_occurred (parser);
27551 /* Remove the topmost context from the stack. */
27552 context = parser->context;
27553 parser->context = context->next;
27554 /* If no parse errors occurred, commit to the tentative parse. */
27555 if (!error_occurred)
27557 /* Commit to the tokens read tentatively, unless that was
27558 already done. */
27559 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
27560 cp_lexer_commit_tokens (parser->lexer);
27562 pop_to_parent_deferring_access_checks ();
27564 /* Otherwise, if errors occurred, roll back our state so that things
27565 are just as they were before we began the tentative parse. */
27566 else
27568 cp_lexer_rollback_tokens (parser->lexer);
27569 pop_deferring_access_checks ();
27571 /* Add the context to the front of the free list. */
27572 context->next = cp_parser_context_free_list;
27573 cp_parser_context_free_list = context;
27575 return !error_occurred;
27578 /* Returns true if we are parsing tentatively and are not committed to
27579 this tentative parse. */
27581 static bool
27582 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
27584 return (cp_parser_parsing_tentatively (parser)
27585 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
27588 /* Returns nonzero iff an error has occurred during the most recent
27589 tentative parse. */
27591 static bool
27592 cp_parser_error_occurred (cp_parser* parser)
27594 return (cp_parser_parsing_tentatively (parser)
27595 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
27598 /* Returns nonzero if GNU extensions are allowed. */
27600 static bool
27601 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
27603 return parser->allow_gnu_extensions_p;
27606 /* Objective-C++ Productions */
27609 /* Parse an Objective-C expression, which feeds into a primary-expression
27610 above.
27612 objc-expression:
27613 objc-message-expression
27614 objc-string-literal
27615 objc-encode-expression
27616 objc-protocol-expression
27617 objc-selector-expression
27619 Returns a tree representation of the expression. */
27621 static cp_expr
27622 cp_parser_objc_expression (cp_parser* parser)
27624 /* Try to figure out what kind of declaration is present. */
27625 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27627 switch (kwd->type)
27629 case CPP_OPEN_SQUARE:
27630 return cp_parser_objc_message_expression (parser);
27632 case CPP_OBJC_STRING:
27633 kwd = cp_lexer_consume_token (parser->lexer);
27634 return objc_build_string_object (kwd->u.value);
27636 case CPP_KEYWORD:
27637 switch (kwd->keyword)
27639 case RID_AT_ENCODE:
27640 return cp_parser_objc_encode_expression (parser);
27642 case RID_AT_PROTOCOL:
27643 return cp_parser_objc_protocol_expression (parser);
27645 case RID_AT_SELECTOR:
27646 return cp_parser_objc_selector_expression (parser);
27648 default:
27649 break;
27651 default:
27652 error_at (kwd->location,
27653 "misplaced %<@%D%> Objective-C++ construct",
27654 kwd->u.value);
27655 cp_parser_skip_to_end_of_block_or_statement (parser);
27658 return error_mark_node;
27661 /* Parse an Objective-C message expression.
27663 objc-message-expression:
27664 [ objc-message-receiver objc-message-args ]
27666 Returns a representation of an Objective-C message. */
27668 static tree
27669 cp_parser_objc_message_expression (cp_parser* parser)
27671 tree receiver, messageargs;
27673 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
27674 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
27675 receiver = cp_parser_objc_message_receiver (parser);
27676 messageargs = cp_parser_objc_message_args (parser);
27677 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
27678 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27680 tree result = objc_build_message_expr (receiver, messageargs);
27682 /* Construct a location e.g.
27683 [self func1:5]
27684 ^~~~~~~~~~~~~~
27685 ranging from the '[' to the ']', with the caret at the start. */
27686 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
27687 protected_set_expr_location (result, combined_loc);
27689 return result;
27692 /* Parse an objc-message-receiver.
27694 objc-message-receiver:
27695 expression
27696 simple-type-specifier
27698 Returns a representation of the type or expression. */
27700 static tree
27701 cp_parser_objc_message_receiver (cp_parser* parser)
27703 tree rcv;
27705 /* An Objective-C message receiver may be either (1) a type
27706 or (2) an expression. */
27707 cp_parser_parse_tentatively (parser);
27708 rcv = cp_parser_expression (parser);
27710 /* If that worked out, fine. */
27711 if (cp_parser_parse_definitely (parser))
27712 return rcv;
27714 cp_parser_parse_tentatively (parser);
27715 rcv = cp_parser_simple_type_specifier (parser,
27716 /*decl_specs=*/NULL,
27717 CP_PARSER_FLAGS_NONE);
27719 if (cp_parser_parse_definitely (parser))
27720 return objc_get_class_reference (rcv);
27722 cp_parser_error (parser, "objective-c++ message receiver expected");
27723 return error_mark_node;
27726 /* Parse the arguments and selectors comprising an Objective-C message.
27728 objc-message-args:
27729 objc-selector
27730 objc-selector-args
27731 objc-selector-args , objc-comma-args
27733 objc-selector-args:
27734 objc-selector [opt] : assignment-expression
27735 objc-selector-args objc-selector [opt] : assignment-expression
27737 objc-comma-args:
27738 assignment-expression
27739 objc-comma-args , assignment-expression
27741 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
27742 selector arguments and TREE_VALUE containing a list of comma
27743 arguments. */
27745 static tree
27746 cp_parser_objc_message_args (cp_parser* parser)
27748 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
27749 bool maybe_unary_selector_p = true;
27750 cp_token *token = cp_lexer_peek_token (parser->lexer);
27752 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
27754 tree selector = NULL_TREE, arg;
27756 if (token->type != CPP_COLON)
27757 selector = cp_parser_objc_selector (parser);
27759 /* Detect if we have a unary selector. */
27760 if (maybe_unary_selector_p
27761 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27762 return build_tree_list (selector, NULL_TREE);
27764 maybe_unary_selector_p = false;
27765 cp_parser_require (parser, CPP_COLON, RT_COLON);
27766 arg = cp_parser_assignment_expression (parser);
27768 sel_args
27769 = chainon (sel_args,
27770 build_tree_list (selector, arg));
27772 token = cp_lexer_peek_token (parser->lexer);
27775 /* Handle non-selector arguments, if any. */
27776 while (token->type == CPP_COMMA)
27778 tree arg;
27780 cp_lexer_consume_token (parser->lexer);
27781 arg = cp_parser_assignment_expression (parser);
27783 addl_args
27784 = chainon (addl_args,
27785 build_tree_list (NULL_TREE, arg));
27787 token = cp_lexer_peek_token (parser->lexer);
27790 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
27792 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
27793 return build_tree_list (error_mark_node, error_mark_node);
27796 return build_tree_list (sel_args, addl_args);
27799 /* Parse an Objective-C encode expression.
27801 objc-encode-expression:
27802 @encode objc-typename
27804 Returns an encoded representation of the type argument. */
27806 static cp_expr
27807 cp_parser_objc_encode_expression (cp_parser* parser)
27809 tree type;
27810 cp_token *token;
27811 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
27813 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
27814 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27815 token = cp_lexer_peek_token (parser->lexer);
27816 type = complete_type (cp_parser_type_id (parser));
27817 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27819 if (!type)
27821 error_at (token->location,
27822 "%<@encode%> must specify a type as an argument");
27823 return error_mark_node;
27826 /* This happens if we find @encode(T) (where T is a template
27827 typename or something dependent on a template typename) when
27828 parsing a template. In that case, we can't compile it
27829 immediately, but we rather create an AT_ENCODE_EXPR which will
27830 need to be instantiated when the template is used.
27832 if (dependent_type_p (type))
27834 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
27835 TREE_READONLY (value) = 1;
27836 return value;
27840 /* Build a location of the form:
27841 @encode(int)
27842 ^~~~~~~~~~~~
27843 with caret==start at the @ token, finishing at the close paren. */
27844 location_t combined_loc
27845 = make_location (start_loc, start_loc,
27846 cp_lexer_previous_token (parser->lexer)->location);
27848 return cp_expr (objc_build_encode_expr (type), combined_loc);
27851 /* Parse an Objective-C @defs expression. */
27853 static tree
27854 cp_parser_objc_defs_expression (cp_parser *parser)
27856 tree name;
27858 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
27859 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27860 name = cp_parser_identifier (parser);
27861 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27863 return objc_get_class_ivars (name);
27866 /* Parse an Objective-C protocol expression.
27868 objc-protocol-expression:
27869 @protocol ( identifier )
27871 Returns a representation of the protocol expression. */
27873 static tree
27874 cp_parser_objc_protocol_expression (cp_parser* parser)
27876 tree proto;
27877 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
27879 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
27880 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27881 proto = cp_parser_identifier (parser);
27882 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27884 /* Build a location of the form:
27885 @protocol(prot)
27886 ^~~~~~~~~~~~~~~
27887 with caret==start at the @ token, finishing at the close paren. */
27888 location_t combined_loc
27889 = make_location (start_loc, start_loc,
27890 cp_lexer_previous_token (parser->lexer)->location);
27891 tree result = objc_build_protocol_expr (proto);
27892 protected_set_expr_location (result, combined_loc);
27893 return result;
27896 /* Parse an Objective-C selector expression.
27898 objc-selector-expression:
27899 @selector ( objc-method-signature )
27901 objc-method-signature:
27902 objc-selector
27903 objc-selector-seq
27905 objc-selector-seq:
27906 objc-selector :
27907 objc-selector-seq objc-selector :
27909 Returns a representation of the method selector. */
27911 static tree
27912 cp_parser_objc_selector_expression (cp_parser* parser)
27914 tree sel_seq = NULL_TREE;
27915 bool maybe_unary_selector_p = true;
27916 cp_token *token;
27917 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27919 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
27920 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27921 token = cp_lexer_peek_token (parser->lexer);
27923 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
27924 || token->type == CPP_SCOPE)
27926 tree selector = NULL_TREE;
27928 if (token->type != CPP_COLON
27929 || token->type == CPP_SCOPE)
27930 selector = cp_parser_objc_selector (parser);
27932 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
27933 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
27935 /* Detect if we have a unary selector. */
27936 if (maybe_unary_selector_p)
27938 sel_seq = selector;
27939 goto finish_selector;
27941 else
27943 cp_parser_error (parser, "expected %<:%>");
27946 maybe_unary_selector_p = false;
27947 token = cp_lexer_consume_token (parser->lexer);
27949 if (token->type == CPP_SCOPE)
27951 sel_seq
27952 = chainon (sel_seq,
27953 build_tree_list (selector, NULL_TREE));
27954 sel_seq
27955 = chainon (sel_seq,
27956 build_tree_list (NULL_TREE, NULL_TREE));
27958 else
27959 sel_seq
27960 = chainon (sel_seq,
27961 build_tree_list (selector, NULL_TREE));
27963 token = cp_lexer_peek_token (parser->lexer);
27966 finish_selector:
27967 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27970 /* Build a location of the form:
27971 @selector(func)
27972 ^~~~~~~~~~~~~~~
27973 with caret==start at the @ token, finishing at the close paren. */
27974 location_t combined_loc
27975 = make_location (loc, loc,
27976 cp_lexer_previous_token (parser->lexer)->location);
27977 tree result = objc_build_selector_expr (combined_loc, sel_seq);
27978 /* TODO: objc_build_selector_expr doesn't always honor the location. */
27979 protected_set_expr_location (result, combined_loc);
27980 return result;
27983 /* Parse a list of identifiers.
27985 objc-identifier-list:
27986 identifier
27987 objc-identifier-list , identifier
27989 Returns a TREE_LIST of identifier nodes. */
27991 static tree
27992 cp_parser_objc_identifier_list (cp_parser* parser)
27994 tree identifier;
27995 tree list;
27996 cp_token *sep;
27998 identifier = cp_parser_identifier (parser);
27999 if (identifier == error_mark_node)
28000 return error_mark_node;
28002 list = build_tree_list (NULL_TREE, identifier);
28003 sep = cp_lexer_peek_token (parser->lexer);
28005 while (sep->type == CPP_COMMA)
28007 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28008 identifier = cp_parser_identifier (parser);
28009 if (identifier == error_mark_node)
28010 return list;
28012 list = chainon (list, build_tree_list (NULL_TREE,
28013 identifier));
28014 sep = cp_lexer_peek_token (parser->lexer);
28017 return list;
28020 /* Parse an Objective-C alias declaration.
28022 objc-alias-declaration:
28023 @compatibility_alias identifier identifier ;
28025 This function registers the alias mapping with the Objective-C front end.
28026 It returns nothing. */
28028 static void
28029 cp_parser_objc_alias_declaration (cp_parser* parser)
28031 tree alias, orig;
28033 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
28034 alias = cp_parser_identifier (parser);
28035 orig = cp_parser_identifier (parser);
28036 objc_declare_alias (alias, orig);
28037 cp_parser_consume_semicolon_at_end_of_statement (parser);
28040 /* Parse an Objective-C class forward-declaration.
28042 objc-class-declaration:
28043 @class objc-identifier-list ;
28045 The function registers the forward declarations with the Objective-C
28046 front end. It returns nothing. */
28048 static void
28049 cp_parser_objc_class_declaration (cp_parser* parser)
28051 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
28052 while (true)
28054 tree id;
28056 id = cp_parser_identifier (parser);
28057 if (id == error_mark_node)
28058 break;
28060 objc_declare_class (id);
28062 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28063 cp_lexer_consume_token (parser->lexer);
28064 else
28065 break;
28067 cp_parser_consume_semicolon_at_end_of_statement (parser);
28070 /* Parse a list of Objective-C protocol references.
28072 objc-protocol-refs-opt:
28073 objc-protocol-refs [opt]
28075 objc-protocol-refs:
28076 < objc-identifier-list >
28078 Returns a TREE_LIST of identifiers, if any. */
28080 static tree
28081 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
28083 tree protorefs = NULL_TREE;
28085 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
28087 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
28088 protorefs = cp_parser_objc_identifier_list (parser);
28089 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
28092 return protorefs;
28095 /* Parse a Objective-C visibility specification. */
28097 static void
28098 cp_parser_objc_visibility_spec (cp_parser* parser)
28100 cp_token *vis = cp_lexer_peek_token (parser->lexer);
28102 switch (vis->keyword)
28104 case RID_AT_PRIVATE:
28105 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
28106 break;
28107 case RID_AT_PROTECTED:
28108 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
28109 break;
28110 case RID_AT_PUBLIC:
28111 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
28112 break;
28113 case RID_AT_PACKAGE:
28114 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
28115 break;
28116 default:
28117 return;
28120 /* Eat '@private'/'@protected'/'@public'. */
28121 cp_lexer_consume_token (parser->lexer);
28124 /* Parse an Objective-C method type. Return 'true' if it is a class
28125 (+) method, and 'false' if it is an instance (-) method. */
28127 static inline bool
28128 cp_parser_objc_method_type (cp_parser* parser)
28130 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
28131 return true;
28132 else
28133 return false;
28136 /* Parse an Objective-C protocol qualifier. */
28138 static tree
28139 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
28141 tree quals = NULL_TREE, node;
28142 cp_token *token = cp_lexer_peek_token (parser->lexer);
28144 node = token->u.value;
28146 while (node && identifier_p (node)
28147 && (node == ridpointers [(int) RID_IN]
28148 || node == ridpointers [(int) RID_OUT]
28149 || node == ridpointers [(int) RID_INOUT]
28150 || node == ridpointers [(int) RID_BYCOPY]
28151 || node == ridpointers [(int) RID_BYREF]
28152 || node == ridpointers [(int) RID_ONEWAY]))
28154 quals = tree_cons (NULL_TREE, node, quals);
28155 cp_lexer_consume_token (parser->lexer);
28156 token = cp_lexer_peek_token (parser->lexer);
28157 node = token->u.value;
28160 return quals;
28163 /* Parse an Objective-C typename. */
28165 static tree
28166 cp_parser_objc_typename (cp_parser* parser)
28168 tree type_name = NULL_TREE;
28170 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28172 tree proto_quals, cp_type = NULL_TREE;
28174 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
28175 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
28177 /* An ObjC type name may consist of just protocol qualifiers, in which
28178 case the type shall default to 'id'. */
28179 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
28181 cp_type = cp_parser_type_id (parser);
28183 /* If the type could not be parsed, an error has already
28184 been produced. For error recovery, behave as if it had
28185 not been specified, which will use the default type
28186 'id'. */
28187 if (cp_type == error_mark_node)
28189 cp_type = NULL_TREE;
28190 /* We need to skip to the closing parenthesis as
28191 cp_parser_type_id() does not seem to do it for
28192 us. */
28193 cp_parser_skip_to_closing_parenthesis (parser,
28194 /*recovering=*/true,
28195 /*or_comma=*/false,
28196 /*consume_paren=*/false);
28200 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28201 type_name = build_tree_list (proto_quals, cp_type);
28204 return type_name;
28207 /* Check to see if TYPE refers to an Objective-C selector name. */
28209 static bool
28210 cp_parser_objc_selector_p (enum cpp_ttype type)
28212 return (type == CPP_NAME || type == CPP_KEYWORD
28213 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
28214 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
28215 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
28216 || type == CPP_XOR || type == CPP_XOR_EQ);
28219 /* Parse an Objective-C selector. */
28221 static tree
28222 cp_parser_objc_selector (cp_parser* parser)
28224 cp_token *token = cp_lexer_consume_token (parser->lexer);
28226 if (!cp_parser_objc_selector_p (token->type))
28228 error_at (token->location, "invalid Objective-C++ selector name");
28229 return error_mark_node;
28232 /* C++ operator names are allowed to appear in ObjC selectors. */
28233 switch (token->type)
28235 case CPP_AND_AND: return get_identifier ("and");
28236 case CPP_AND_EQ: return get_identifier ("and_eq");
28237 case CPP_AND: return get_identifier ("bitand");
28238 case CPP_OR: return get_identifier ("bitor");
28239 case CPP_COMPL: return get_identifier ("compl");
28240 case CPP_NOT: return get_identifier ("not");
28241 case CPP_NOT_EQ: return get_identifier ("not_eq");
28242 case CPP_OR_OR: return get_identifier ("or");
28243 case CPP_OR_EQ: return get_identifier ("or_eq");
28244 case CPP_XOR: return get_identifier ("xor");
28245 case CPP_XOR_EQ: return get_identifier ("xor_eq");
28246 default: return token->u.value;
28250 /* Parse an Objective-C params list. */
28252 static tree
28253 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
28255 tree params = NULL_TREE;
28256 bool maybe_unary_selector_p = true;
28257 cp_token *token = cp_lexer_peek_token (parser->lexer);
28259 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
28261 tree selector = NULL_TREE, type_name, identifier;
28262 tree parm_attr = NULL_TREE;
28264 if (token->keyword == RID_ATTRIBUTE)
28265 break;
28267 if (token->type != CPP_COLON)
28268 selector = cp_parser_objc_selector (parser);
28270 /* Detect if we have a unary selector. */
28271 if (maybe_unary_selector_p
28272 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28274 params = selector; /* Might be followed by attributes. */
28275 break;
28278 maybe_unary_selector_p = false;
28279 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28281 /* Something went quite wrong. There should be a colon
28282 here, but there is not. Stop parsing parameters. */
28283 break;
28285 type_name = cp_parser_objc_typename (parser);
28286 /* New ObjC allows attributes on parameters too. */
28287 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
28288 parm_attr = cp_parser_attributes_opt (parser);
28289 identifier = cp_parser_identifier (parser);
28291 params
28292 = chainon (params,
28293 objc_build_keyword_decl (selector,
28294 type_name,
28295 identifier,
28296 parm_attr));
28298 token = cp_lexer_peek_token (parser->lexer);
28301 if (params == NULL_TREE)
28303 cp_parser_error (parser, "objective-c++ method declaration is expected");
28304 return error_mark_node;
28307 /* We allow tail attributes for the method. */
28308 if (token->keyword == RID_ATTRIBUTE)
28310 *attributes = cp_parser_attributes_opt (parser);
28311 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28312 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28313 return params;
28314 cp_parser_error (parser,
28315 "method attributes must be specified at the end");
28316 return error_mark_node;
28319 if (params == NULL_TREE)
28321 cp_parser_error (parser, "objective-c++ method declaration is expected");
28322 return error_mark_node;
28324 return params;
28327 /* Parse the non-keyword Objective-C params. */
28329 static tree
28330 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
28331 tree* attributes)
28333 tree params = make_node (TREE_LIST);
28334 cp_token *token = cp_lexer_peek_token (parser->lexer);
28335 *ellipsisp = false; /* Initially, assume no ellipsis. */
28337 while (token->type == CPP_COMMA)
28339 cp_parameter_declarator *parmdecl;
28340 tree parm;
28342 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28343 token = cp_lexer_peek_token (parser->lexer);
28345 if (token->type == CPP_ELLIPSIS)
28347 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
28348 *ellipsisp = true;
28349 token = cp_lexer_peek_token (parser->lexer);
28350 break;
28353 /* TODO: parse attributes for tail parameters. */
28354 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
28355 parm = grokdeclarator (parmdecl->declarator,
28356 &parmdecl->decl_specifiers,
28357 PARM, /*initialized=*/0,
28358 /*attrlist=*/NULL);
28360 chainon (params, build_tree_list (NULL_TREE, parm));
28361 token = cp_lexer_peek_token (parser->lexer);
28364 /* We allow tail attributes for the method. */
28365 if (token->keyword == RID_ATTRIBUTE)
28367 if (*attributes == NULL_TREE)
28369 *attributes = cp_parser_attributes_opt (parser);
28370 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28371 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28372 return params;
28374 else
28375 /* We have an error, but parse the attributes, so that we can
28376 carry on. */
28377 *attributes = cp_parser_attributes_opt (parser);
28379 cp_parser_error (parser,
28380 "method attributes must be specified at the end");
28381 return error_mark_node;
28384 return params;
28387 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
28389 static void
28390 cp_parser_objc_interstitial_code (cp_parser* parser)
28392 cp_token *token = cp_lexer_peek_token (parser->lexer);
28394 /* If the next token is `extern' and the following token is a string
28395 literal, then we have a linkage specification. */
28396 if (token->keyword == RID_EXTERN
28397 && cp_parser_is_pure_string_literal
28398 (cp_lexer_peek_nth_token (parser->lexer, 2)))
28399 cp_parser_linkage_specification (parser);
28400 /* Handle #pragma, if any. */
28401 else if (token->type == CPP_PRAGMA)
28402 cp_parser_pragma (parser, pragma_objc_icode);
28403 /* Allow stray semicolons. */
28404 else if (token->type == CPP_SEMICOLON)
28405 cp_lexer_consume_token (parser->lexer);
28406 /* Mark methods as optional or required, when building protocols. */
28407 else if (token->keyword == RID_AT_OPTIONAL)
28409 cp_lexer_consume_token (parser->lexer);
28410 objc_set_method_opt (true);
28412 else if (token->keyword == RID_AT_REQUIRED)
28414 cp_lexer_consume_token (parser->lexer);
28415 objc_set_method_opt (false);
28417 else if (token->keyword == RID_NAMESPACE)
28418 cp_parser_namespace_definition (parser);
28419 /* Other stray characters must generate errors. */
28420 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
28422 cp_lexer_consume_token (parser->lexer);
28423 error ("stray %qs between Objective-C++ methods",
28424 token->type == CPP_OPEN_BRACE ? "{" : "}");
28426 /* Finally, try to parse a block-declaration, or a function-definition. */
28427 else
28428 cp_parser_block_declaration (parser, /*statement_p=*/false);
28431 /* Parse a method signature. */
28433 static tree
28434 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
28436 tree rettype, kwdparms, optparms;
28437 bool ellipsis = false;
28438 bool is_class_method;
28440 is_class_method = cp_parser_objc_method_type (parser);
28441 rettype = cp_parser_objc_typename (parser);
28442 *attributes = NULL_TREE;
28443 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
28444 if (kwdparms == error_mark_node)
28445 return error_mark_node;
28446 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
28447 if (optparms == error_mark_node)
28448 return error_mark_node;
28450 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
28453 static bool
28454 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
28456 tree tattr;
28457 cp_lexer_save_tokens (parser->lexer);
28458 tattr = cp_parser_attributes_opt (parser);
28459 gcc_assert (tattr) ;
28461 /* If the attributes are followed by a method introducer, this is not allowed.
28462 Dump the attributes and flag the situation. */
28463 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
28464 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
28465 return true;
28467 /* Otherwise, the attributes introduce some interstitial code, possibly so
28468 rewind to allow that check. */
28469 cp_lexer_rollback_tokens (parser->lexer);
28470 return false;
28473 /* Parse an Objective-C method prototype list. */
28475 static void
28476 cp_parser_objc_method_prototype_list (cp_parser* parser)
28478 cp_token *token = cp_lexer_peek_token (parser->lexer);
28480 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
28482 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
28484 tree attributes, sig;
28485 bool is_class_method;
28486 if (token->type == CPP_PLUS)
28487 is_class_method = true;
28488 else
28489 is_class_method = false;
28490 sig = cp_parser_objc_method_signature (parser, &attributes);
28491 if (sig == error_mark_node)
28493 cp_parser_skip_to_end_of_block_or_statement (parser);
28494 token = cp_lexer_peek_token (parser->lexer);
28495 continue;
28497 objc_add_method_declaration (is_class_method, sig, attributes);
28498 cp_parser_consume_semicolon_at_end_of_statement (parser);
28500 else if (token->keyword == RID_AT_PROPERTY)
28501 cp_parser_objc_at_property_declaration (parser);
28502 else if (token->keyword == RID_ATTRIBUTE
28503 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
28504 warning_at (cp_lexer_peek_token (parser->lexer)->location,
28505 OPT_Wattributes,
28506 "prefix attributes are ignored for methods");
28507 else
28508 /* Allow for interspersed non-ObjC++ code. */
28509 cp_parser_objc_interstitial_code (parser);
28511 token = cp_lexer_peek_token (parser->lexer);
28514 if (token->type != CPP_EOF)
28515 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
28516 else
28517 cp_parser_error (parser, "expected %<@end%>");
28519 objc_finish_interface ();
28522 /* Parse an Objective-C method definition list. */
28524 static void
28525 cp_parser_objc_method_definition_list (cp_parser* parser)
28527 cp_token *token = cp_lexer_peek_token (parser->lexer);
28529 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
28531 tree meth;
28533 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
28535 cp_token *ptk;
28536 tree sig, attribute;
28537 bool is_class_method;
28538 if (token->type == CPP_PLUS)
28539 is_class_method = true;
28540 else
28541 is_class_method = false;
28542 push_deferring_access_checks (dk_deferred);
28543 sig = cp_parser_objc_method_signature (parser, &attribute);
28544 if (sig == error_mark_node)
28546 cp_parser_skip_to_end_of_block_or_statement (parser);
28547 token = cp_lexer_peek_token (parser->lexer);
28548 continue;
28550 objc_start_method_definition (is_class_method, sig, attribute,
28551 NULL_TREE);
28553 /* For historical reasons, we accept an optional semicolon. */
28554 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28555 cp_lexer_consume_token (parser->lexer);
28557 ptk = cp_lexer_peek_token (parser->lexer);
28558 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
28559 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
28561 perform_deferred_access_checks (tf_warning_or_error);
28562 stop_deferring_access_checks ();
28563 meth = cp_parser_function_definition_after_declarator (parser,
28564 false);
28565 pop_deferring_access_checks ();
28566 objc_finish_method_definition (meth);
28569 /* The following case will be removed once @synthesize is
28570 completely implemented. */
28571 else if (token->keyword == RID_AT_PROPERTY)
28572 cp_parser_objc_at_property_declaration (parser);
28573 else if (token->keyword == RID_AT_SYNTHESIZE)
28574 cp_parser_objc_at_synthesize_declaration (parser);
28575 else if (token->keyword == RID_AT_DYNAMIC)
28576 cp_parser_objc_at_dynamic_declaration (parser);
28577 else if (token->keyword == RID_ATTRIBUTE
28578 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
28579 warning_at (token->location, OPT_Wattributes,
28580 "prefix attributes are ignored for methods");
28581 else
28582 /* Allow for interspersed non-ObjC++ code. */
28583 cp_parser_objc_interstitial_code (parser);
28585 token = cp_lexer_peek_token (parser->lexer);
28588 if (token->type != CPP_EOF)
28589 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
28590 else
28591 cp_parser_error (parser, "expected %<@end%>");
28593 objc_finish_implementation ();
28596 /* Parse Objective-C ivars. */
28598 static void
28599 cp_parser_objc_class_ivars (cp_parser* parser)
28601 cp_token *token = cp_lexer_peek_token (parser->lexer);
28603 if (token->type != CPP_OPEN_BRACE)
28604 return; /* No ivars specified. */
28606 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
28607 token = cp_lexer_peek_token (parser->lexer);
28609 while (token->type != CPP_CLOSE_BRACE
28610 && token->keyword != RID_AT_END && token->type != CPP_EOF)
28612 cp_decl_specifier_seq declspecs;
28613 int decl_class_or_enum_p;
28614 tree prefix_attributes;
28616 cp_parser_objc_visibility_spec (parser);
28618 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28619 break;
28621 cp_parser_decl_specifier_seq (parser,
28622 CP_PARSER_FLAGS_OPTIONAL,
28623 &declspecs,
28624 &decl_class_or_enum_p);
28626 /* auto, register, static, extern, mutable. */
28627 if (declspecs.storage_class != sc_none)
28629 cp_parser_error (parser, "invalid type for instance variable");
28630 declspecs.storage_class = sc_none;
28633 /* thread_local. */
28634 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
28636 cp_parser_error (parser, "invalid type for instance variable");
28637 declspecs.locations[ds_thread] = 0;
28640 /* typedef. */
28641 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
28643 cp_parser_error (parser, "invalid type for instance variable");
28644 declspecs.locations[ds_typedef] = 0;
28647 prefix_attributes = declspecs.attributes;
28648 declspecs.attributes = NULL_TREE;
28650 /* Keep going until we hit the `;' at the end of the
28651 declaration. */
28652 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28654 tree width = NULL_TREE, attributes, first_attribute, decl;
28655 cp_declarator *declarator = NULL;
28656 int ctor_dtor_or_conv_p;
28658 /* Check for a (possibly unnamed) bitfield declaration. */
28659 token = cp_lexer_peek_token (parser->lexer);
28660 if (token->type == CPP_COLON)
28661 goto eat_colon;
28663 if (token->type == CPP_NAME
28664 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
28665 == CPP_COLON))
28667 /* Get the name of the bitfield. */
28668 declarator = make_id_declarator (NULL_TREE,
28669 cp_parser_identifier (parser),
28670 sfk_none);
28672 eat_colon:
28673 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
28674 /* Get the width of the bitfield. */
28675 width
28676 = cp_parser_constant_expression (parser);
28678 else
28680 /* Parse the declarator. */
28681 declarator
28682 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28683 &ctor_dtor_or_conv_p,
28684 /*parenthesized_p=*/NULL,
28685 /*member_p=*/false,
28686 /*friend_p=*/false);
28689 /* Look for attributes that apply to the ivar. */
28690 attributes = cp_parser_attributes_opt (parser);
28691 /* Remember which attributes are prefix attributes and
28692 which are not. */
28693 first_attribute = attributes;
28694 /* Combine the attributes. */
28695 attributes = chainon (prefix_attributes, attributes);
28697 if (width)
28698 /* Create the bitfield declaration. */
28699 decl = grokbitfield (declarator, &declspecs,
28700 width,
28701 attributes);
28702 else
28703 decl = grokfield (declarator, &declspecs,
28704 NULL_TREE, /*init_const_expr_p=*/false,
28705 NULL_TREE, attributes);
28707 /* Add the instance variable. */
28708 if (decl != error_mark_node && decl != NULL_TREE)
28709 objc_add_instance_variable (decl);
28711 /* Reset PREFIX_ATTRIBUTES. */
28712 while (attributes && TREE_CHAIN (attributes) != first_attribute)
28713 attributes = TREE_CHAIN (attributes);
28714 if (attributes)
28715 TREE_CHAIN (attributes) = NULL_TREE;
28717 token = cp_lexer_peek_token (parser->lexer);
28719 if (token->type == CPP_COMMA)
28721 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28722 continue;
28724 break;
28727 cp_parser_consume_semicolon_at_end_of_statement (parser);
28728 token = cp_lexer_peek_token (parser->lexer);
28731 if (token->keyword == RID_AT_END)
28732 cp_parser_error (parser, "expected %<}%>");
28734 /* Do not consume the RID_AT_END, so it will be read again as terminating
28735 the @interface of @implementation. */
28736 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
28737 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
28739 /* For historical reasons, we accept an optional semicolon. */
28740 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28741 cp_lexer_consume_token (parser->lexer);
28744 /* Parse an Objective-C protocol declaration. */
28746 static void
28747 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
28749 tree proto, protorefs;
28750 cp_token *tok;
28752 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
28753 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
28755 tok = cp_lexer_peek_token (parser->lexer);
28756 error_at (tok->location, "identifier expected after %<@protocol%>");
28757 cp_parser_consume_semicolon_at_end_of_statement (parser);
28758 return;
28761 /* See if we have a forward declaration or a definition. */
28762 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
28764 /* Try a forward declaration first. */
28765 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
28767 while (true)
28769 tree id;
28771 id = cp_parser_identifier (parser);
28772 if (id == error_mark_node)
28773 break;
28775 objc_declare_protocol (id, attributes);
28777 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28778 cp_lexer_consume_token (parser->lexer);
28779 else
28780 break;
28782 cp_parser_consume_semicolon_at_end_of_statement (parser);
28785 /* Ok, we got a full-fledged definition (or at least should). */
28786 else
28788 proto = cp_parser_identifier (parser);
28789 protorefs = cp_parser_objc_protocol_refs_opt (parser);
28790 objc_start_protocol (proto, protorefs, attributes);
28791 cp_parser_objc_method_prototype_list (parser);
28795 /* Parse an Objective-C superclass or category. */
28797 static void
28798 cp_parser_objc_superclass_or_category (cp_parser *parser,
28799 bool iface_p,
28800 tree *super,
28801 tree *categ, bool *is_class_extension)
28803 cp_token *next = cp_lexer_peek_token (parser->lexer);
28805 *super = *categ = NULL_TREE;
28806 *is_class_extension = false;
28807 if (next->type == CPP_COLON)
28809 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
28810 *super = cp_parser_identifier (parser);
28812 else if (next->type == CPP_OPEN_PAREN)
28814 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
28816 /* If there is no category name, and this is an @interface, we
28817 have a class extension. */
28818 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
28820 *categ = NULL_TREE;
28821 *is_class_extension = true;
28823 else
28824 *categ = cp_parser_identifier (parser);
28826 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28830 /* Parse an Objective-C class interface. */
28832 static void
28833 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
28835 tree name, super, categ, protos;
28836 bool is_class_extension;
28838 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
28839 name = cp_parser_identifier (parser);
28840 if (name == error_mark_node)
28842 /* It's hard to recover because even if valid @interface stuff
28843 is to follow, we can't compile it (or validate it) if we
28844 don't even know which class it refers to. Let's assume this
28845 was a stray '@interface' token in the stream and skip it.
28847 return;
28849 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
28850 &is_class_extension);
28851 protos = cp_parser_objc_protocol_refs_opt (parser);
28853 /* We have either a class or a category on our hands. */
28854 if (categ || is_class_extension)
28855 objc_start_category_interface (name, categ, protos, attributes);
28856 else
28858 objc_start_class_interface (name, super, protos, attributes);
28859 /* Handle instance variable declarations, if any. */
28860 cp_parser_objc_class_ivars (parser);
28861 objc_continue_interface ();
28864 cp_parser_objc_method_prototype_list (parser);
28867 /* Parse an Objective-C class implementation. */
28869 static void
28870 cp_parser_objc_class_implementation (cp_parser* parser)
28872 tree name, super, categ;
28873 bool is_class_extension;
28875 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
28876 name = cp_parser_identifier (parser);
28877 if (name == error_mark_node)
28879 /* It's hard to recover because even if valid @implementation
28880 stuff is to follow, we can't compile it (or validate it) if
28881 we don't even know which class it refers to. Let's assume
28882 this was a stray '@implementation' token in the stream and
28883 skip it.
28885 return;
28887 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
28888 &is_class_extension);
28890 /* We have either a class or a category on our hands. */
28891 if (categ)
28892 objc_start_category_implementation (name, categ);
28893 else
28895 objc_start_class_implementation (name, super);
28896 /* Handle instance variable declarations, if any. */
28897 cp_parser_objc_class_ivars (parser);
28898 objc_continue_implementation ();
28901 cp_parser_objc_method_definition_list (parser);
28904 /* Consume the @end token and finish off the implementation. */
28906 static void
28907 cp_parser_objc_end_implementation (cp_parser* parser)
28909 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
28910 objc_finish_implementation ();
28913 /* Parse an Objective-C declaration. */
28915 static void
28916 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
28918 /* Try to figure out what kind of declaration is present. */
28919 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
28921 if (attributes)
28922 switch (kwd->keyword)
28924 case RID_AT_ALIAS:
28925 case RID_AT_CLASS:
28926 case RID_AT_END:
28927 error_at (kwd->location, "attributes may not be specified before"
28928 " the %<@%D%> Objective-C++ keyword",
28929 kwd->u.value);
28930 attributes = NULL;
28931 break;
28932 case RID_AT_IMPLEMENTATION:
28933 warning_at (kwd->location, OPT_Wattributes,
28934 "prefix attributes are ignored before %<@%D%>",
28935 kwd->u.value);
28936 attributes = NULL;
28937 default:
28938 break;
28941 switch (kwd->keyword)
28943 case RID_AT_ALIAS:
28944 cp_parser_objc_alias_declaration (parser);
28945 break;
28946 case RID_AT_CLASS:
28947 cp_parser_objc_class_declaration (parser);
28948 break;
28949 case RID_AT_PROTOCOL:
28950 cp_parser_objc_protocol_declaration (parser, attributes);
28951 break;
28952 case RID_AT_INTERFACE:
28953 cp_parser_objc_class_interface (parser, attributes);
28954 break;
28955 case RID_AT_IMPLEMENTATION:
28956 cp_parser_objc_class_implementation (parser);
28957 break;
28958 case RID_AT_END:
28959 cp_parser_objc_end_implementation (parser);
28960 break;
28961 default:
28962 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
28963 kwd->u.value);
28964 cp_parser_skip_to_end_of_block_or_statement (parser);
28968 /* Parse an Objective-C try-catch-finally statement.
28970 objc-try-catch-finally-stmt:
28971 @try compound-statement objc-catch-clause-seq [opt]
28972 objc-finally-clause [opt]
28974 objc-catch-clause-seq:
28975 objc-catch-clause objc-catch-clause-seq [opt]
28977 objc-catch-clause:
28978 @catch ( objc-exception-declaration ) compound-statement
28980 objc-finally-clause:
28981 @finally compound-statement
28983 objc-exception-declaration:
28984 parameter-declaration
28985 '...'
28987 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
28989 Returns NULL_TREE.
28991 PS: This function is identical to c_parser_objc_try_catch_finally_statement
28992 for C. Keep them in sync. */
28994 static tree
28995 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
28997 location_t location;
28998 tree stmt;
29000 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
29001 location = cp_lexer_peek_token (parser->lexer)->location;
29002 objc_maybe_warn_exceptions (location);
29003 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
29004 node, lest it get absorbed into the surrounding block. */
29005 stmt = push_stmt_list ();
29006 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29007 objc_begin_try_stmt (location, pop_stmt_list (stmt));
29009 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
29011 cp_parameter_declarator *parm;
29012 tree parameter_declaration = error_mark_node;
29013 bool seen_open_paren = false;
29015 cp_lexer_consume_token (parser->lexer);
29016 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29017 seen_open_paren = true;
29018 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29020 /* We have "@catch (...)" (where the '...' are literally
29021 what is in the code). Skip the '...'.
29022 parameter_declaration is set to NULL_TREE, and
29023 objc_being_catch_clauses() knows that that means
29024 '...'. */
29025 cp_lexer_consume_token (parser->lexer);
29026 parameter_declaration = NULL_TREE;
29028 else
29030 /* We have "@catch (NSException *exception)" or something
29031 like that. Parse the parameter declaration. */
29032 parm = cp_parser_parameter_declaration (parser, false, NULL);
29033 if (parm == NULL)
29034 parameter_declaration = error_mark_node;
29035 else
29036 parameter_declaration = grokdeclarator (parm->declarator,
29037 &parm->decl_specifiers,
29038 PARM, /*initialized=*/0,
29039 /*attrlist=*/NULL);
29041 if (seen_open_paren)
29042 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29043 else
29045 /* If there was no open parenthesis, we are recovering from
29046 an error, and we are trying to figure out what mistake
29047 the user has made. */
29049 /* If there is an immediate closing parenthesis, the user
29050 probably forgot the opening one (ie, they typed "@catch
29051 NSException *e)". Parse the closing parenthesis and keep
29052 going. */
29053 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29054 cp_lexer_consume_token (parser->lexer);
29056 /* If these is no immediate closing parenthesis, the user
29057 probably doesn't know that parenthesis are required at
29058 all (ie, they typed "@catch NSException *e"). So, just
29059 forget about the closing parenthesis and keep going. */
29061 objc_begin_catch_clause (parameter_declaration);
29062 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29063 objc_finish_catch_clause ();
29065 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
29067 cp_lexer_consume_token (parser->lexer);
29068 location = cp_lexer_peek_token (parser->lexer)->location;
29069 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
29070 node, lest it get absorbed into the surrounding block. */
29071 stmt = push_stmt_list ();
29072 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29073 objc_build_finally_clause (location, pop_stmt_list (stmt));
29076 return objc_finish_try_stmt ();
29079 /* Parse an Objective-C synchronized statement.
29081 objc-synchronized-stmt:
29082 @synchronized ( expression ) compound-statement
29084 Returns NULL_TREE. */
29086 static tree
29087 cp_parser_objc_synchronized_statement (cp_parser *parser)
29089 location_t location;
29090 tree lock, stmt;
29092 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
29094 location = cp_lexer_peek_token (parser->lexer)->location;
29095 objc_maybe_warn_exceptions (location);
29096 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
29097 lock = cp_parser_expression (parser);
29098 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29100 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
29101 node, lest it get absorbed into the surrounding block. */
29102 stmt = push_stmt_list ();
29103 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29105 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
29108 /* Parse an Objective-C throw statement.
29110 objc-throw-stmt:
29111 @throw assignment-expression [opt] ;
29113 Returns a constructed '@throw' statement. */
29115 static tree
29116 cp_parser_objc_throw_statement (cp_parser *parser)
29118 tree expr = NULL_TREE;
29119 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29121 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
29123 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29124 expr = cp_parser_expression (parser);
29126 cp_parser_consume_semicolon_at_end_of_statement (parser);
29128 return objc_build_throw_stmt (loc, expr);
29131 /* Parse an Objective-C statement. */
29133 static tree
29134 cp_parser_objc_statement (cp_parser * parser)
29136 /* Try to figure out what kind of declaration is present. */
29137 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29139 switch (kwd->keyword)
29141 case RID_AT_TRY:
29142 return cp_parser_objc_try_catch_finally_statement (parser);
29143 case RID_AT_SYNCHRONIZED:
29144 return cp_parser_objc_synchronized_statement (parser);
29145 case RID_AT_THROW:
29146 return cp_parser_objc_throw_statement (parser);
29147 default:
29148 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
29149 kwd->u.value);
29150 cp_parser_skip_to_end_of_block_or_statement (parser);
29153 return error_mark_node;
29156 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
29157 look ahead to see if an objc keyword follows the attributes. This
29158 is to detect the use of prefix attributes on ObjC @interface and
29159 @protocol. */
29161 static bool
29162 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
29164 cp_lexer_save_tokens (parser->lexer);
29165 *attrib = cp_parser_attributes_opt (parser);
29166 gcc_assert (*attrib);
29167 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
29169 cp_lexer_commit_tokens (parser->lexer);
29170 return true;
29172 cp_lexer_rollback_tokens (parser->lexer);
29173 return false;
29176 /* This routine is a minimal replacement for
29177 c_parser_struct_declaration () used when parsing the list of
29178 types/names or ObjC++ properties. For example, when parsing the
29179 code
29181 @property (readonly) int a, b, c;
29183 this function is responsible for parsing "int a, int b, int c" and
29184 returning the declarations as CHAIN of DECLs.
29186 TODO: Share this code with cp_parser_objc_class_ivars. It's very
29187 similar parsing. */
29188 static tree
29189 cp_parser_objc_struct_declaration (cp_parser *parser)
29191 tree decls = NULL_TREE;
29192 cp_decl_specifier_seq declspecs;
29193 int decl_class_or_enum_p;
29194 tree prefix_attributes;
29196 cp_parser_decl_specifier_seq (parser,
29197 CP_PARSER_FLAGS_NONE,
29198 &declspecs,
29199 &decl_class_or_enum_p);
29201 if (declspecs.type == error_mark_node)
29202 return error_mark_node;
29204 /* auto, register, static, extern, mutable. */
29205 if (declspecs.storage_class != sc_none)
29207 cp_parser_error (parser, "invalid type for property");
29208 declspecs.storage_class = sc_none;
29211 /* thread_local. */
29212 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
29214 cp_parser_error (parser, "invalid type for property");
29215 declspecs.locations[ds_thread] = 0;
29218 /* typedef. */
29219 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
29221 cp_parser_error (parser, "invalid type for property");
29222 declspecs.locations[ds_typedef] = 0;
29225 prefix_attributes = declspecs.attributes;
29226 declspecs.attributes = NULL_TREE;
29228 /* Keep going until we hit the `;' at the end of the declaration. */
29229 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29231 tree attributes, first_attribute, decl;
29232 cp_declarator *declarator;
29233 cp_token *token;
29235 /* Parse the declarator. */
29236 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29237 NULL, NULL, false, false);
29239 /* Look for attributes that apply to the ivar. */
29240 attributes = cp_parser_attributes_opt (parser);
29241 /* Remember which attributes are prefix attributes and
29242 which are not. */
29243 first_attribute = attributes;
29244 /* Combine the attributes. */
29245 attributes = chainon (prefix_attributes, attributes);
29247 decl = grokfield (declarator, &declspecs,
29248 NULL_TREE, /*init_const_expr_p=*/false,
29249 NULL_TREE, attributes);
29251 if (decl == error_mark_node || decl == NULL_TREE)
29252 return error_mark_node;
29254 /* Reset PREFIX_ATTRIBUTES. */
29255 while (attributes && TREE_CHAIN (attributes) != first_attribute)
29256 attributes = TREE_CHAIN (attributes);
29257 if (attributes)
29258 TREE_CHAIN (attributes) = NULL_TREE;
29260 DECL_CHAIN (decl) = decls;
29261 decls = decl;
29263 token = cp_lexer_peek_token (parser->lexer);
29264 if (token->type == CPP_COMMA)
29266 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29267 continue;
29269 else
29270 break;
29272 return decls;
29275 /* Parse an Objective-C @property declaration. The syntax is:
29277 objc-property-declaration:
29278 '@property' objc-property-attributes[opt] struct-declaration ;
29280 objc-property-attributes:
29281 '(' objc-property-attribute-list ')'
29283 objc-property-attribute-list:
29284 objc-property-attribute
29285 objc-property-attribute-list, objc-property-attribute
29287 objc-property-attribute
29288 'getter' = identifier
29289 'setter' = identifier
29290 'readonly'
29291 'readwrite'
29292 'assign'
29293 'retain'
29294 'copy'
29295 'nonatomic'
29297 For example:
29298 @property NSString *name;
29299 @property (readonly) id object;
29300 @property (retain, nonatomic, getter=getTheName) id name;
29301 @property int a, b, c;
29303 PS: This function is identical to
29304 c_parser_objc_at_property_declaration for C. Keep them in sync. */
29305 static void
29306 cp_parser_objc_at_property_declaration (cp_parser *parser)
29308 /* The following variables hold the attributes of the properties as
29309 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
29310 seen. When we see an attribute, we set them to 'true' (if they
29311 are boolean properties) or to the identifier (if they have an
29312 argument, ie, for getter and setter). Note that here we only
29313 parse the list of attributes, check the syntax and accumulate the
29314 attributes that we find. objc_add_property_declaration() will
29315 then process the information. */
29316 bool property_assign = false;
29317 bool property_copy = false;
29318 tree property_getter_ident = NULL_TREE;
29319 bool property_nonatomic = false;
29320 bool property_readonly = false;
29321 bool property_readwrite = false;
29322 bool property_retain = false;
29323 tree property_setter_ident = NULL_TREE;
29325 /* 'properties' is the list of properties that we read. Usually a
29326 single one, but maybe more (eg, in "@property int a, b, c;" there
29327 are three). */
29328 tree properties;
29329 location_t loc;
29331 loc = cp_lexer_peek_token (parser->lexer)->location;
29333 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
29335 /* Parse the optional attribute list... */
29336 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29338 /* Eat the '('. */
29339 cp_lexer_consume_token (parser->lexer);
29341 while (true)
29343 bool syntax_error = false;
29344 cp_token *token = cp_lexer_peek_token (parser->lexer);
29345 enum rid keyword;
29347 if (token->type != CPP_NAME)
29349 cp_parser_error (parser, "expected identifier");
29350 break;
29352 keyword = C_RID_CODE (token->u.value);
29353 cp_lexer_consume_token (parser->lexer);
29354 switch (keyword)
29356 case RID_ASSIGN: property_assign = true; break;
29357 case RID_COPY: property_copy = true; break;
29358 case RID_NONATOMIC: property_nonatomic = true; break;
29359 case RID_READONLY: property_readonly = true; break;
29360 case RID_READWRITE: property_readwrite = true; break;
29361 case RID_RETAIN: property_retain = true; break;
29363 case RID_GETTER:
29364 case RID_SETTER:
29365 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29367 if (keyword == RID_GETTER)
29368 cp_parser_error (parser,
29369 "missing %<=%> (after %<getter%> attribute)");
29370 else
29371 cp_parser_error (parser,
29372 "missing %<=%> (after %<setter%> attribute)");
29373 syntax_error = true;
29374 break;
29376 cp_lexer_consume_token (parser->lexer); /* eat the = */
29377 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
29379 cp_parser_error (parser, "expected identifier");
29380 syntax_error = true;
29381 break;
29383 if (keyword == RID_SETTER)
29385 if (property_setter_ident != NULL_TREE)
29387 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
29388 cp_lexer_consume_token (parser->lexer);
29390 else
29391 property_setter_ident = cp_parser_objc_selector (parser);
29392 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29393 cp_parser_error (parser, "setter name must terminate with %<:%>");
29394 else
29395 cp_lexer_consume_token (parser->lexer);
29397 else
29399 if (property_getter_ident != NULL_TREE)
29401 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
29402 cp_lexer_consume_token (parser->lexer);
29404 else
29405 property_getter_ident = cp_parser_objc_selector (parser);
29407 break;
29408 default:
29409 cp_parser_error (parser, "unknown property attribute");
29410 syntax_error = true;
29411 break;
29414 if (syntax_error)
29415 break;
29417 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29418 cp_lexer_consume_token (parser->lexer);
29419 else
29420 break;
29423 /* FIXME: "@property (setter, assign);" will generate a spurious
29424 "error: expected ‘)’ before ‘,’ token". This is because
29425 cp_parser_require, unlike the C counterpart, will produce an
29426 error even if we are in error recovery. */
29427 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29429 cp_parser_skip_to_closing_parenthesis (parser,
29430 /*recovering=*/true,
29431 /*or_comma=*/false,
29432 /*consume_paren=*/true);
29436 /* ... and the property declaration(s). */
29437 properties = cp_parser_objc_struct_declaration (parser);
29439 if (properties == error_mark_node)
29441 cp_parser_skip_to_end_of_statement (parser);
29442 /* If the next token is now a `;', consume it. */
29443 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29444 cp_lexer_consume_token (parser->lexer);
29445 return;
29448 if (properties == NULL_TREE)
29449 cp_parser_error (parser, "expected identifier");
29450 else
29452 /* Comma-separated properties are chained together in
29453 reverse order; add them one by one. */
29454 properties = nreverse (properties);
29456 for (; properties; properties = TREE_CHAIN (properties))
29457 objc_add_property_declaration (loc, copy_node (properties),
29458 property_readonly, property_readwrite,
29459 property_assign, property_retain,
29460 property_copy, property_nonatomic,
29461 property_getter_ident, property_setter_ident);
29464 cp_parser_consume_semicolon_at_end_of_statement (parser);
29467 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
29469 objc-synthesize-declaration:
29470 @synthesize objc-synthesize-identifier-list ;
29472 objc-synthesize-identifier-list:
29473 objc-synthesize-identifier
29474 objc-synthesize-identifier-list, objc-synthesize-identifier
29476 objc-synthesize-identifier
29477 identifier
29478 identifier = identifier
29480 For example:
29481 @synthesize MyProperty;
29482 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
29484 PS: This function is identical to c_parser_objc_at_synthesize_declaration
29485 for C. Keep them in sync.
29487 static void
29488 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
29490 tree list = NULL_TREE;
29491 location_t loc;
29492 loc = cp_lexer_peek_token (parser->lexer)->location;
29494 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
29495 while (true)
29497 tree property, ivar;
29498 property = cp_parser_identifier (parser);
29499 if (property == error_mark_node)
29501 cp_parser_consume_semicolon_at_end_of_statement (parser);
29502 return;
29504 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
29506 cp_lexer_consume_token (parser->lexer);
29507 ivar = cp_parser_identifier (parser);
29508 if (ivar == error_mark_node)
29510 cp_parser_consume_semicolon_at_end_of_statement (parser);
29511 return;
29514 else
29515 ivar = NULL_TREE;
29516 list = chainon (list, build_tree_list (ivar, property));
29517 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29518 cp_lexer_consume_token (parser->lexer);
29519 else
29520 break;
29522 cp_parser_consume_semicolon_at_end_of_statement (parser);
29523 objc_add_synthesize_declaration (loc, list);
29526 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
29528 objc-dynamic-declaration:
29529 @dynamic identifier-list ;
29531 For example:
29532 @dynamic MyProperty;
29533 @dynamic MyProperty, AnotherProperty;
29535 PS: This function is identical to c_parser_objc_at_dynamic_declaration
29536 for C. Keep them in sync.
29538 static void
29539 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
29541 tree list = NULL_TREE;
29542 location_t loc;
29543 loc = cp_lexer_peek_token (parser->lexer)->location;
29545 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
29546 while (true)
29548 tree property;
29549 property = cp_parser_identifier (parser);
29550 if (property == error_mark_node)
29552 cp_parser_consume_semicolon_at_end_of_statement (parser);
29553 return;
29555 list = chainon (list, build_tree_list (NULL, property));
29556 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29557 cp_lexer_consume_token (parser->lexer);
29558 else
29559 break;
29561 cp_parser_consume_semicolon_at_end_of_statement (parser);
29562 objc_add_dynamic_declaration (loc, list);
29566 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
29568 /* Returns name of the next clause.
29569 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
29570 the token is not consumed. Otherwise appropriate pragma_omp_clause is
29571 returned and the token is consumed. */
29573 static pragma_omp_clause
29574 cp_parser_omp_clause_name (cp_parser *parser)
29576 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
29578 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
29579 result = PRAGMA_OACC_CLAUSE_AUTO;
29580 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
29581 result = PRAGMA_OMP_CLAUSE_IF;
29582 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
29583 result = PRAGMA_OMP_CLAUSE_DEFAULT;
29584 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
29585 result = PRAGMA_OACC_CLAUSE_DELETE;
29586 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
29587 result = PRAGMA_OMP_CLAUSE_PRIVATE;
29588 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29589 result = PRAGMA_OMP_CLAUSE_FOR;
29590 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29592 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29593 const char *p = IDENTIFIER_POINTER (id);
29595 switch (p[0])
29597 case 'a':
29598 if (!strcmp ("aligned", p))
29599 result = PRAGMA_OMP_CLAUSE_ALIGNED;
29600 else if (!strcmp ("async", p))
29601 result = PRAGMA_OACC_CLAUSE_ASYNC;
29602 break;
29603 case 'c':
29604 if (!strcmp ("collapse", p))
29605 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
29606 else if (!strcmp ("copy", p))
29607 result = PRAGMA_OACC_CLAUSE_COPY;
29608 else if (!strcmp ("copyin", p))
29609 result = PRAGMA_OMP_CLAUSE_COPYIN;
29610 else if (!strcmp ("copyout", p))
29611 result = PRAGMA_OACC_CLAUSE_COPYOUT;
29612 else if (!strcmp ("copyprivate", p))
29613 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
29614 else if (!strcmp ("create", p))
29615 result = PRAGMA_OACC_CLAUSE_CREATE;
29616 break;
29617 case 'd':
29618 if (!strcmp ("defaultmap", p))
29619 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
29620 else if (!strcmp ("depend", p))
29621 result = PRAGMA_OMP_CLAUSE_DEPEND;
29622 else if (!strcmp ("device", p))
29623 result = PRAGMA_OMP_CLAUSE_DEVICE;
29624 else if (!strcmp ("deviceptr", p))
29625 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
29626 else if (!strcmp ("device_resident", p))
29627 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
29628 else if (!strcmp ("dist_schedule", p))
29629 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
29630 break;
29631 case 'f':
29632 if (!strcmp ("final", p))
29633 result = PRAGMA_OMP_CLAUSE_FINAL;
29634 else if (!strcmp ("firstprivate", p))
29635 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
29636 else if (!strcmp ("from", p))
29637 result = PRAGMA_OMP_CLAUSE_FROM;
29638 break;
29639 case 'g':
29640 if (!strcmp ("gang", p))
29641 result = PRAGMA_OACC_CLAUSE_GANG;
29642 else if (!strcmp ("grainsize", p))
29643 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
29644 break;
29645 case 'h':
29646 if (!strcmp ("hint", p))
29647 result = PRAGMA_OMP_CLAUSE_HINT;
29648 else if (!strcmp ("host", p))
29649 result = PRAGMA_OACC_CLAUSE_HOST;
29650 break;
29651 case 'i':
29652 if (!strcmp ("inbranch", p))
29653 result = PRAGMA_OMP_CLAUSE_INBRANCH;
29654 else if (!strcmp ("independent", p))
29655 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
29656 else if (!strcmp ("is_device_ptr", p))
29657 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
29658 break;
29659 case 'l':
29660 if (!strcmp ("lastprivate", p))
29661 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
29662 else if (!strcmp ("linear", p))
29663 result = PRAGMA_OMP_CLAUSE_LINEAR;
29664 else if (!strcmp ("link", p))
29665 result = PRAGMA_OMP_CLAUSE_LINK;
29666 break;
29667 case 'm':
29668 if (!strcmp ("map", p))
29669 result = PRAGMA_OMP_CLAUSE_MAP;
29670 else if (!strcmp ("mergeable", p))
29671 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
29672 else if (flag_cilkplus && !strcmp ("mask", p))
29673 result = PRAGMA_CILK_CLAUSE_MASK;
29674 break;
29675 case 'n':
29676 if (!strcmp ("nogroup", p))
29677 result = PRAGMA_OMP_CLAUSE_NOGROUP;
29678 else if (!strcmp ("notinbranch", p))
29679 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
29680 else if (!strcmp ("nowait", p))
29681 result = PRAGMA_OMP_CLAUSE_NOWAIT;
29682 else if (flag_cilkplus && !strcmp ("nomask", p))
29683 result = PRAGMA_CILK_CLAUSE_NOMASK;
29684 else if (!strcmp ("num_gangs", p))
29685 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
29686 else if (!strcmp ("num_tasks", p))
29687 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
29688 else if (!strcmp ("num_teams", p))
29689 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
29690 else if (!strcmp ("num_threads", p))
29691 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
29692 else if (!strcmp ("num_workers", p))
29693 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
29694 break;
29695 case 'o':
29696 if (!strcmp ("ordered", p))
29697 result = PRAGMA_OMP_CLAUSE_ORDERED;
29698 break;
29699 case 'p':
29700 if (!strcmp ("parallel", p))
29701 result = PRAGMA_OMP_CLAUSE_PARALLEL;
29702 else if (!strcmp ("present", p))
29703 result = PRAGMA_OACC_CLAUSE_PRESENT;
29704 else if (!strcmp ("present_or_copy", p)
29705 || !strcmp ("pcopy", p))
29706 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
29707 else if (!strcmp ("present_or_copyin", p)
29708 || !strcmp ("pcopyin", p))
29709 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
29710 else if (!strcmp ("present_or_copyout", p)
29711 || !strcmp ("pcopyout", p))
29712 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
29713 else if (!strcmp ("present_or_create", p)
29714 || !strcmp ("pcreate", p))
29715 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
29716 else if (!strcmp ("priority", p))
29717 result = PRAGMA_OMP_CLAUSE_PRIORITY;
29718 else if (!strcmp ("proc_bind", p))
29719 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
29720 break;
29721 case 'r':
29722 if (!strcmp ("reduction", p))
29723 result = PRAGMA_OMP_CLAUSE_REDUCTION;
29724 break;
29725 case 's':
29726 if (!strcmp ("safelen", p))
29727 result = PRAGMA_OMP_CLAUSE_SAFELEN;
29728 else if (!strcmp ("schedule", p))
29729 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
29730 else if (!strcmp ("sections", p))
29731 result = PRAGMA_OMP_CLAUSE_SECTIONS;
29732 else if (!strcmp ("self", p))
29733 result = PRAGMA_OACC_CLAUSE_SELF;
29734 else if (!strcmp ("seq", p))
29735 result = PRAGMA_OACC_CLAUSE_SEQ;
29736 else if (!strcmp ("shared", p))
29737 result = PRAGMA_OMP_CLAUSE_SHARED;
29738 else if (!strcmp ("simd", p))
29739 result = PRAGMA_OMP_CLAUSE_SIMD;
29740 else if (!strcmp ("simdlen", p))
29741 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
29742 break;
29743 case 't':
29744 if (!strcmp ("taskgroup", p))
29745 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
29746 else if (!strcmp ("thread_limit", p))
29747 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
29748 else if (!strcmp ("threads", p))
29749 result = PRAGMA_OMP_CLAUSE_THREADS;
29750 else if (!strcmp ("tile", p))
29751 result = PRAGMA_OACC_CLAUSE_TILE;
29752 else if (!strcmp ("to", p))
29753 result = PRAGMA_OMP_CLAUSE_TO;
29754 break;
29755 case 'u':
29756 if (!strcmp ("uniform", p))
29757 result = PRAGMA_OMP_CLAUSE_UNIFORM;
29758 else if (!strcmp ("untied", p))
29759 result = PRAGMA_OMP_CLAUSE_UNTIED;
29760 else if (!strcmp ("use_device", p))
29761 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
29762 else if (!strcmp ("use_device_ptr", p))
29763 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
29764 break;
29765 case 'v':
29766 if (!strcmp ("vector", p))
29767 result = PRAGMA_OACC_CLAUSE_VECTOR;
29768 else if (!strcmp ("vector_length", p))
29769 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
29770 else if (flag_cilkplus && !strcmp ("vectorlength", p))
29771 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
29772 break;
29773 case 'w':
29774 if (!strcmp ("wait", p))
29775 result = PRAGMA_OACC_CLAUSE_WAIT;
29776 else if (!strcmp ("worker", p))
29777 result = PRAGMA_OACC_CLAUSE_WORKER;
29778 break;
29782 if (result != PRAGMA_OMP_CLAUSE_NONE)
29783 cp_lexer_consume_token (parser->lexer);
29785 return result;
29788 /* Validate that a clause of the given type does not already exist. */
29790 static void
29791 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
29792 const char *name, location_t location)
29794 tree c;
29796 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29797 if (OMP_CLAUSE_CODE (c) == code)
29799 error_at (location, "too many %qs clauses", name);
29800 break;
29804 /* OpenMP 2.5:
29805 variable-list:
29806 identifier
29807 variable-list , identifier
29809 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
29810 colon). An opening parenthesis will have been consumed by the caller.
29812 If KIND is nonzero, create the appropriate node and install the decl
29813 in OMP_CLAUSE_DECL and add the node to the head of the list.
29815 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
29816 return the list created.
29818 COLON can be NULL if only closing parenthesis should end the list,
29819 or pointer to bool which will receive false if the list is terminated
29820 by closing parenthesis or true if the list is terminated by colon. */
29822 static tree
29823 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
29824 tree list, bool *colon)
29826 cp_token *token;
29827 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
29828 if (colon)
29830 parser->colon_corrects_to_scope_p = false;
29831 *colon = false;
29833 while (1)
29835 tree name, decl;
29837 token = cp_lexer_peek_token (parser->lexer);
29838 if (kind != 0
29839 && current_class_ptr
29840 && cp_parser_is_keyword (token, RID_THIS))
29842 decl = finish_this_expr ();
29843 if (TREE_CODE (decl) == NON_LVALUE_EXPR
29844 || CONVERT_EXPR_P (decl))
29845 decl = TREE_OPERAND (decl, 0);
29846 cp_lexer_consume_token (parser->lexer);
29848 else
29850 name = cp_parser_id_expression (parser, /*template_p=*/false,
29851 /*check_dependency_p=*/true,
29852 /*template_p=*/NULL,
29853 /*declarator_p=*/false,
29854 /*optional_p=*/false);
29855 if (name == error_mark_node)
29856 goto skip_comma;
29858 decl = cp_parser_lookup_name_simple (parser, name, token->location);
29859 if (decl == error_mark_node)
29860 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
29861 token->location);
29863 if (decl == error_mark_node)
29865 else if (kind != 0)
29867 switch (kind)
29869 case OMP_CLAUSE__CACHE_:
29870 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
29872 error_at (token->location, "expected %<[%>");
29873 decl = error_mark_node;
29874 break;
29876 /* FALLTHROUGH. */
29877 case OMP_CLAUSE_MAP:
29878 case OMP_CLAUSE_FROM:
29879 case OMP_CLAUSE_TO:
29880 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
29882 location_t loc
29883 = cp_lexer_peek_token (parser->lexer)->location;
29884 cp_id_kind idk = CP_ID_KIND_NONE;
29885 cp_lexer_consume_token (parser->lexer);
29886 decl
29887 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
29888 decl, false,
29889 &idk, loc);
29891 /* FALLTHROUGH. */
29892 case OMP_CLAUSE_DEPEND:
29893 case OMP_CLAUSE_REDUCTION:
29894 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
29896 tree low_bound = NULL_TREE, length = NULL_TREE;
29898 parser->colon_corrects_to_scope_p = false;
29899 cp_lexer_consume_token (parser->lexer);
29900 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
29901 low_bound = cp_parser_expression (parser);
29902 if (!colon)
29903 parser->colon_corrects_to_scope_p
29904 = saved_colon_corrects_to_scope_p;
29905 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
29906 length = integer_one_node;
29907 else
29909 /* Look for `:'. */
29910 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29911 goto skip_comma;
29912 if (!cp_lexer_next_token_is (parser->lexer,
29913 CPP_CLOSE_SQUARE))
29914 length = cp_parser_expression (parser);
29916 /* Look for the closing `]'. */
29917 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
29918 RT_CLOSE_SQUARE))
29919 goto skip_comma;
29921 if (kind == OMP_CLAUSE__CACHE_)
29923 if (TREE_CODE (low_bound) != INTEGER_CST
29924 && !TREE_READONLY (low_bound))
29926 error_at (token->location,
29927 "%qD is not a constant", low_bound);
29928 decl = error_mark_node;
29931 if (TREE_CODE (length) != INTEGER_CST
29932 && !TREE_READONLY (length))
29934 error_at (token->location,
29935 "%qD is not a constant", length);
29936 decl = error_mark_node;
29940 decl = tree_cons (low_bound, length, decl);
29942 break;
29943 default:
29944 break;
29947 tree u = build_omp_clause (token->location, kind);
29948 OMP_CLAUSE_DECL (u) = decl;
29949 OMP_CLAUSE_CHAIN (u) = list;
29950 list = u;
29952 else
29953 list = tree_cons (decl, NULL_TREE, list);
29955 get_comma:
29956 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29957 break;
29958 cp_lexer_consume_token (parser->lexer);
29961 if (colon)
29962 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
29964 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
29966 *colon = true;
29967 cp_parser_require (parser, CPP_COLON, RT_COLON);
29968 return list;
29971 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29973 int ending;
29975 /* Try to resync to an unnested comma. Copied from
29976 cp_parser_parenthesized_expression_list. */
29977 skip_comma:
29978 if (colon)
29979 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
29980 ending = cp_parser_skip_to_closing_parenthesis (parser,
29981 /*recovering=*/true,
29982 /*or_comma=*/true,
29983 /*consume_paren=*/true);
29984 if (ending < 0)
29985 goto get_comma;
29988 return list;
29991 /* Similarly, but expect leading and trailing parenthesis. This is a very
29992 common case for omp clauses. */
29994 static tree
29995 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
29997 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29998 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
29999 return list;
30002 /* OpenACC 2.0:
30003 copy ( variable-list )
30004 copyin ( variable-list )
30005 copyout ( variable-list )
30006 create ( variable-list )
30007 delete ( variable-list )
30008 present ( variable-list )
30009 present_or_copy ( variable-list )
30010 pcopy ( variable-list )
30011 present_or_copyin ( variable-list )
30012 pcopyin ( variable-list )
30013 present_or_copyout ( variable-list )
30014 pcopyout ( variable-list )
30015 present_or_create ( variable-list )
30016 pcreate ( variable-list ) */
30018 static tree
30019 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
30020 tree list)
30022 enum gomp_map_kind kind;
30023 switch (c_kind)
30025 case PRAGMA_OACC_CLAUSE_COPY:
30026 kind = GOMP_MAP_FORCE_TOFROM;
30027 break;
30028 case PRAGMA_OACC_CLAUSE_COPYIN:
30029 kind = GOMP_MAP_FORCE_TO;
30030 break;
30031 case PRAGMA_OACC_CLAUSE_COPYOUT:
30032 kind = GOMP_MAP_FORCE_FROM;
30033 break;
30034 case PRAGMA_OACC_CLAUSE_CREATE:
30035 kind = GOMP_MAP_FORCE_ALLOC;
30036 break;
30037 case PRAGMA_OACC_CLAUSE_DELETE:
30038 kind = GOMP_MAP_FORCE_DEALLOC;
30039 break;
30040 case PRAGMA_OACC_CLAUSE_DEVICE:
30041 kind = GOMP_MAP_FORCE_TO;
30042 break;
30043 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
30044 kind = GOMP_MAP_DEVICE_RESIDENT;
30045 break;
30046 case PRAGMA_OACC_CLAUSE_HOST:
30047 case PRAGMA_OACC_CLAUSE_SELF:
30048 kind = GOMP_MAP_FORCE_FROM;
30049 break;
30050 case PRAGMA_OACC_CLAUSE_LINK:
30051 kind = GOMP_MAP_LINK;
30052 break;
30053 case PRAGMA_OACC_CLAUSE_PRESENT:
30054 kind = GOMP_MAP_FORCE_PRESENT;
30055 break;
30056 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
30057 kind = GOMP_MAP_TOFROM;
30058 break;
30059 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
30060 kind = GOMP_MAP_TO;
30061 break;
30062 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
30063 kind = GOMP_MAP_FROM;
30064 break;
30065 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
30066 kind = GOMP_MAP_ALLOC;
30067 break;
30068 default:
30069 gcc_unreachable ();
30071 tree nl, c;
30072 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
30074 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
30075 OMP_CLAUSE_SET_MAP_KIND (c, kind);
30077 return nl;
30080 /* OpenACC 2.0:
30081 deviceptr ( variable-list ) */
30083 static tree
30084 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
30086 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30087 tree vars, t;
30089 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
30090 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
30091 variable-list must only allow for pointer variables. */
30092 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30093 for (t = vars; t; t = TREE_CHAIN (t))
30095 tree v = TREE_PURPOSE (t);
30097 /* FIXME diagnostics: Ideally we should keep individual
30098 locations for all the variables in the var list to make the
30099 following errors more precise. Perhaps
30100 c_parser_omp_var_list_parens should construct a list of
30101 locations to go along with the var list. */
30103 if (!VAR_P (v))
30104 error_at (loc, "%qD is not a variable", v);
30105 else if (TREE_TYPE (v) == error_mark_node)
30107 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
30108 error_at (loc, "%qD is not a pointer variable", v);
30110 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
30111 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
30112 OMP_CLAUSE_DECL (u) = v;
30113 OMP_CLAUSE_CHAIN (u) = list;
30114 list = u;
30117 return list;
30120 /* OpenACC 2.0:
30121 auto
30122 independent
30123 nohost
30124 seq */
30126 static tree
30127 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
30128 enum omp_clause_code code,
30129 tree list, location_t location)
30131 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
30132 tree c = build_omp_clause (location, code);
30133 OMP_CLAUSE_CHAIN (c) = list;
30134 return c;
30137 /* OpenACC:
30138 num_gangs ( expression )
30139 num_workers ( expression )
30140 vector_length ( expression ) */
30142 static tree
30143 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
30144 const char *str, tree list)
30146 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30148 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30149 return list;
30151 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
30153 if (t == error_mark_node
30154 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30156 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30157 /*or_comma=*/false,
30158 /*consume_paren=*/true);
30159 return list;
30162 check_no_duplicate_clause (list, code, str, loc);
30164 tree c = build_omp_clause (loc, code);
30165 OMP_CLAUSE_OPERAND (c, 0) = t;
30166 OMP_CLAUSE_CHAIN (c) = list;
30167 return c;
30170 /* OpenACC:
30172 gang [( gang-arg-list )]
30173 worker [( [num:] int-expr )]
30174 vector [( [length:] int-expr )]
30176 where gang-arg is one of:
30178 [num:] int-expr
30179 static: size-expr
30181 and size-expr may be:
30184 int-expr
30187 static tree
30188 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
30189 const char *str, tree list)
30191 const char *id = "num";
30192 cp_lexer *lexer = parser->lexer;
30193 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
30194 location_t loc = cp_lexer_peek_token (lexer)->location;
30196 if (kind == OMP_CLAUSE_VECTOR)
30197 id = "length";
30199 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
30201 cp_lexer_consume_token (lexer);
30205 cp_token *next = cp_lexer_peek_token (lexer);
30206 int idx = 0;
30208 /* Gang static argument. */
30209 if (kind == OMP_CLAUSE_GANG
30210 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
30212 cp_lexer_consume_token (lexer);
30214 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30215 goto cleanup_error;
30217 idx = 1;
30218 if (ops[idx] != NULL)
30220 cp_parser_error (parser, "too many %<static%> arguments");
30221 goto cleanup_error;
30224 /* Check for the '*' argument. */
30225 if (cp_lexer_next_token_is (lexer, CPP_MULT)
30226 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
30227 || cp_lexer_nth_token_is (parser->lexer, 2,
30228 CPP_CLOSE_PAREN)))
30230 cp_lexer_consume_token (lexer);
30231 ops[idx] = integer_minus_one_node;
30233 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
30235 cp_lexer_consume_token (lexer);
30236 continue;
30238 else break;
30241 /* Worker num: argument and vector length: arguments. */
30242 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
30243 && strcmp (id, IDENTIFIER_POINTER (next->u.value)) == 0
30244 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
30246 cp_lexer_consume_token (lexer); /* id */
30247 cp_lexer_consume_token (lexer); /* ':' */
30250 /* Now collect the actual argument. */
30251 if (ops[idx] != NULL_TREE)
30253 cp_parser_error (parser, "unexpected argument");
30254 goto cleanup_error;
30257 tree expr = cp_parser_assignment_expression (parser, NULL, false,
30258 false);
30259 if (expr == error_mark_node)
30260 goto cleanup_error;
30262 mark_exp_read (expr);
30263 ops[idx] = expr;
30265 if (kind == OMP_CLAUSE_GANG
30266 && cp_lexer_next_token_is (lexer, CPP_COMMA))
30268 cp_lexer_consume_token (lexer);
30269 continue;
30271 break;
30273 while (1);
30275 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30276 goto cleanup_error;
30279 check_no_duplicate_clause (list, kind, str, loc);
30281 c = build_omp_clause (loc, kind);
30283 if (ops[1])
30284 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
30286 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
30287 OMP_CLAUSE_CHAIN (c) = list;
30289 return c;
30291 cleanup_error:
30292 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
30293 return list;
30296 /* OpenACC 2.0:
30297 tile ( size-expr-list ) */
30299 static tree
30300 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
30302 tree c, expr = error_mark_node;
30303 tree tile = NULL_TREE;
30305 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
30307 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30308 return list;
30312 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
30313 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
30314 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
30316 cp_lexer_consume_token (parser->lexer);
30317 expr = integer_minus_one_node;
30319 else
30320 expr = cp_parser_assignment_expression (parser, NULL, false, false);
30322 if (expr == error_mark_node)
30323 return list;
30325 tile = tree_cons (NULL_TREE, expr, tile);
30327 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30328 cp_lexer_consume_token (parser->lexer);
30330 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
30332 /* Consume the trailing ')'. */
30333 cp_lexer_consume_token (parser->lexer);
30335 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
30336 tile = nreverse (tile);
30337 OMP_CLAUSE_TILE_LIST (c) = tile;
30338 OMP_CLAUSE_CHAIN (c) = list;
30339 return c;
30342 /* OpenACC 2.0
30343 Parse wait clause or directive parameters. */
30345 static tree
30346 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
30348 vec<tree, va_gc> *args;
30349 tree t, args_tree;
30351 args = cp_parser_parenthesized_expression_list (parser, non_attr,
30352 /*cast_p=*/false,
30353 /*allow_expansion_p=*/true,
30354 /*non_constant_p=*/NULL);
30356 if (args == NULL || args->length () == 0)
30358 cp_parser_error (parser, "expected integer expression before ')'");
30359 if (args != NULL)
30360 release_tree_vector (args);
30361 return list;
30364 args_tree = build_tree_list_vec (args);
30366 release_tree_vector (args);
30368 for (t = args_tree; t; t = TREE_CHAIN (t))
30370 tree targ = TREE_VALUE (t);
30372 if (targ != error_mark_node)
30374 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
30375 error ("%<wait%> expression must be integral");
30376 else
30378 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
30380 mark_rvalue_use (targ);
30381 OMP_CLAUSE_DECL (c) = targ;
30382 OMP_CLAUSE_CHAIN (c) = list;
30383 list = c;
30388 return list;
30391 /* OpenACC:
30392 wait ( int-expr-list ) */
30394 static tree
30395 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
30397 location_t location = cp_lexer_peek_token (parser->lexer)->location;
30399 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
30400 return list;
30402 list = cp_parser_oacc_wait_list (parser, location, list);
30404 return list;
30407 /* OpenMP 3.0:
30408 collapse ( constant-expression ) */
30410 static tree
30411 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
30413 tree c, num;
30414 location_t loc;
30415 HOST_WIDE_INT n;
30417 loc = cp_lexer_peek_token (parser->lexer)->location;
30418 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30419 return list;
30421 num = cp_parser_constant_expression (parser);
30423 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30424 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30425 /*or_comma=*/false,
30426 /*consume_paren=*/true);
30428 if (num == error_mark_node)
30429 return list;
30430 num = fold_non_dependent_expr (num);
30431 if (!tree_fits_shwi_p (num)
30432 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
30433 || (n = tree_to_shwi (num)) <= 0
30434 || (int) n != n)
30436 error_at (loc, "collapse argument needs positive constant integer expression");
30437 return list;
30440 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
30441 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
30442 OMP_CLAUSE_CHAIN (c) = list;
30443 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
30445 return c;
30448 /* OpenMP 2.5:
30449 default ( shared | none )
30451 OpenACC 2.0
30452 default (none) */
30454 static tree
30455 cp_parser_omp_clause_default (cp_parser *parser, tree list,
30456 location_t location, bool is_oacc)
30458 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
30459 tree c;
30461 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30462 return list;
30463 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30465 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30466 const char *p = IDENTIFIER_POINTER (id);
30468 switch (p[0])
30470 case 'n':
30471 if (strcmp ("none", p) != 0)
30472 goto invalid_kind;
30473 kind = OMP_CLAUSE_DEFAULT_NONE;
30474 break;
30476 case 's':
30477 if (strcmp ("shared", p) != 0 || is_oacc)
30478 goto invalid_kind;
30479 kind = OMP_CLAUSE_DEFAULT_SHARED;
30480 break;
30482 default:
30483 goto invalid_kind;
30486 cp_lexer_consume_token (parser->lexer);
30488 else
30490 invalid_kind:
30491 if (is_oacc)
30492 cp_parser_error (parser, "expected %<none%>");
30493 else
30494 cp_parser_error (parser, "expected %<none%> or %<shared%>");
30497 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30498 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30499 /*or_comma=*/false,
30500 /*consume_paren=*/true);
30502 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
30503 return list;
30505 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
30506 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
30507 OMP_CLAUSE_CHAIN (c) = list;
30508 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
30510 return c;
30513 /* OpenMP 3.1:
30514 final ( expression ) */
30516 static tree
30517 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
30519 tree t, c;
30521 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30522 return list;
30524 t = cp_parser_condition (parser);
30526 if (t == error_mark_node
30527 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30528 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30529 /*or_comma=*/false,
30530 /*consume_paren=*/true);
30532 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
30534 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
30535 OMP_CLAUSE_FINAL_EXPR (c) = t;
30536 OMP_CLAUSE_CHAIN (c) = list;
30538 return c;
30541 /* OpenMP 2.5:
30542 if ( expression )
30544 OpenMP 4.5:
30545 if ( directive-name-modifier : expression )
30547 directive-name-modifier:
30548 parallel | task | taskloop | target data | target | target update
30549 | target enter data | target exit data */
30551 static tree
30552 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
30553 bool is_omp)
30555 tree t, c;
30556 enum tree_code if_modifier = ERROR_MARK;
30558 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30559 return list;
30561 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30563 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30564 const char *p = IDENTIFIER_POINTER (id);
30565 int n = 2;
30567 if (strcmp ("parallel", p) == 0)
30568 if_modifier = OMP_PARALLEL;
30569 else if (strcmp ("task", p) == 0)
30570 if_modifier = OMP_TASK;
30571 else if (strcmp ("taskloop", p) == 0)
30572 if_modifier = OMP_TASKLOOP;
30573 else if (strcmp ("target", p) == 0)
30575 if_modifier = OMP_TARGET;
30576 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
30578 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
30579 p = IDENTIFIER_POINTER (id);
30580 if (strcmp ("data", p) == 0)
30581 if_modifier = OMP_TARGET_DATA;
30582 else if (strcmp ("update", p) == 0)
30583 if_modifier = OMP_TARGET_UPDATE;
30584 else if (strcmp ("enter", p) == 0)
30585 if_modifier = OMP_TARGET_ENTER_DATA;
30586 else if (strcmp ("exit", p) == 0)
30587 if_modifier = OMP_TARGET_EXIT_DATA;
30588 if (if_modifier != OMP_TARGET)
30589 n = 3;
30590 else
30592 location_t loc
30593 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
30594 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
30595 "or %<exit%>");
30596 if_modifier = ERROR_MARK;
30598 if (if_modifier == OMP_TARGET_ENTER_DATA
30599 || if_modifier == OMP_TARGET_EXIT_DATA)
30601 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
30603 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
30604 p = IDENTIFIER_POINTER (id);
30605 if (strcmp ("data", p) == 0)
30606 n = 4;
30608 if (n != 4)
30610 location_t loc
30611 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
30612 error_at (loc, "expected %<data%>");
30613 if_modifier = ERROR_MARK;
30618 if (if_modifier != ERROR_MARK)
30620 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
30622 while (n-- > 0)
30623 cp_lexer_consume_token (parser->lexer);
30625 else
30627 if (n > 2)
30629 location_t loc
30630 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
30631 error_at (loc, "expected %<:%>");
30633 if_modifier = ERROR_MARK;
30638 t = cp_parser_condition (parser);
30640 if (t == error_mark_node
30641 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30642 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30643 /*or_comma=*/false,
30644 /*consume_paren=*/true);
30646 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
30647 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
30649 if (if_modifier != ERROR_MARK
30650 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
30652 const char *p = NULL;
30653 switch (if_modifier)
30655 case OMP_PARALLEL: p = "parallel"; break;
30656 case OMP_TASK: p = "task"; break;
30657 case OMP_TASKLOOP: p = "taskloop"; break;
30658 case OMP_TARGET_DATA: p = "target data"; break;
30659 case OMP_TARGET: p = "target"; break;
30660 case OMP_TARGET_UPDATE: p = "target update"; break;
30661 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
30662 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
30663 default: gcc_unreachable ();
30665 error_at (location, "too many %<if%> clauses with %qs modifier",
30667 return list;
30669 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
30671 if (!is_omp)
30672 error_at (location, "too many %<if%> clauses");
30673 else
30674 error_at (location, "too many %<if%> clauses without modifier");
30675 return list;
30677 else if (if_modifier == ERROR_MARK
30678 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
30680 error_at (location, "if any %<if%> clause has modifier, then all "
30681 "%<if%> clauses have to use modifier");
30682 return list;
30686 c = build_omp_clause (location, OMP_CLAUSE_IF);
30687 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
30688 OMP_CLAUSE_IF_EXPR (c) = t;
30689 OMP_CLAUSE_CHAIN (c) = list;
30691 return c;
30694 /* OpenMP 3.1:
30695 mergeable */
30697 static tree
30698 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
30699 tree list, location_t location)
30701 tree c;
30703 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
30704 location);
30706 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
30707 OMP_CLAUSE_CHAIN (c) = list;
30708 return c;
30711 /* OpenMP 2.5:
30712 nowait */
30714 static tree
30715 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
30716 tree list, location_t location)
30718 tree c;
30720 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
30722 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
30723 OMP_CLAUSE_CHAIN (c) = list;
30724 return c;
30727 /* OpenMP 2.5:
30728 num_threads ( expression ) */
30730 static tree
30731 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
30732 location_t location)
30734 tree t, c;
30736 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30737 return list;
30739 t = cp_parser_expression (parser);
30741 if (t == error_mark_node
30742 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30743 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30744 /*or_comma=*/false,
30745 /*consume_paren=*/true);
30747 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
30748 "num_threads", location);
30750 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
30751 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
30752 OMP_CLAUSE_CHAIN (c) = list;
30754 return c;
30757 /* OpenMP 4.5:
30758 num_tasks ( expression ) */
30760 static tree
30761 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
30762 location_t location)
30764 tree t, c;
30766 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30767 return list;
30769 t = cp_parser_expression (parser);
30771 if (t == error_mark_node
30772 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30773 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30774 /*or_comma=*/false,
30775 /*consume_paren=*/true);
30777 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
30778 "num_tasks", location);
30780 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
30781 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
30782 OMP_CLAUSE_CHAIN (c) = list;
30784 return c;
30787 /* OpenMP 4.5:
30788 grainsize ( expression ) */
30790 static tree
30791 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
30792 location_t location)
30794 tree t, c;
30796 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30797 return list;
30799 t = cp_parser_expression (parser);
30801 if (t == error_mark_node
30802 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30803 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30804 /*or_comma=*/false,
30805 /*consume_paren=*/true);
30807 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
30808 "grainsize", location);
30810 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
30811 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
30812 OMP_CLAUSE_CHAIN (c) = list;
30814 return c;
30817 /* OpenMP 4.5:
30818 priority ( expression ) */
30820 static tree
30821 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
30822 location_t location)
30824 tree t, c;
30826 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30827 return list;
30829 t = cp_parser_expression (parser);
30831 if (t == error_mark_node
30832 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30833 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30834 /*or_comma=*/false,
30835 /*consume_paren=*/true);
30837 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
30838 "priority", location);
30840 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
30841 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
30842 OMP_CLAUSE_CHAIN (c) = list;
30844 return c;
30847 /* OpenMP 4.5:
30848 hint ( expression ) */
30850 static tree
30851 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
30852 location_t location)
30854 tree t, c;
30856 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30857 return list;
30859 t = cp_parser_expression (parser);
30861 if (t == error_mark_node
30862 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30863 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30864 /*or_comma=*/false,
30865 /*consume_paren=*/true);
30867 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
30869 c = build_omp_clause (location, OMP_CLAUSE_HINT);
30870 OMP_CLAUSE_HINT_EXPR (c) = t;
30871 OMP_CLAUSE_CHAIN (c) = list;
30873 return c;
30876 /* OpenMP 4.5:
30877 defaultmap ( tofrom : scalar ) */
30879 static tree
30880 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
30881 location_t location)
30883 tree c, id;
30884 const char *p;
30886 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30887 return list;
30889 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30891 cp_parser_error (parser, "expected %<tofrom%>");
30892 goto out_err;
30894 id = cp_lexer_peek_token (parser->lexer)->u.value;
30895 p = IDENTIFIER_POINTER (id);
30896 if (strcmp (p, "tofrom") != 0)
30898 cp_parser_error (parser, "expected %<tofrom%>");
30899 goto out_err;
30901 cp_lexer_consume_token (parser->lexer);
30902 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30903 goto out_err;
30905 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30907 cp_parser_error (parser, "expected %<scalar%>");
30908 goto out_err;
30910 id = cp_lexer_peek_token (parser->lexer)->u.value;
30911 p = IDENTIFIER_POINTER (id);
30912 if (strcmp (p, "scalar") != 0)
30914 cp_parser_error (parser, "expected %<scalar%>");
30915 goto out_err;
30917 cp_lexer_consume_token (parser->lexer);
30918 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30919 goto out_err;
30921 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
30922 location);
30924 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
30925 OMP_CLAUSE_CHAIN (c) = list;
30926 return c;
30928 out_err:
30929 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30930 /*or_comma=*/false,
30931 /*consume_paren=*/true);
30932 return list;
30935 /* OpenMP 2.5:
30936 ordered
30938 OpenMP 4.5:
30939 ordered ( constant-expression ) */
30941 static tree
30942 cp_parser_omp_clause_ordered (cp_parser *parser,
30943 tree list, location_t location)
30945 tree c, num = NULL_TREE;
30946 HOST_WIDE_INT n;
30948 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
30949 "ordered", location);
30951 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30953 cp_lexer_consume_token (parser->lexer);
30955 num = cp_parser_constant_expression (parser);
30957 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30958 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30959 /*or_comma=*/false,
30960 /*consume_paren=*/true);
30962 if (num == error_mark_node)
30963 return list;
30964 num = fold_non_dependent_expr (num);
30965 if (!tree_fits_shwi_p (num)
30966 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
30967 || (n = tree_to_shwi (num)) <= 0
30968 || (int) n != n)
30970 error_at (location,
30971 "ordered argument needs positive constant integer "
30972 "expression");
30973 return list;
30977 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
30978 OMP_CLAUSE_ORDERED_EXPR (c) = num;
30979 OMP_CLAUSE_CHAIN (c) = list;
30980 return c;
30983 /* OpenMP 2.5:
30984 reduction ( reduction-operator : variable-list )
30986 reduction-operator:
30987 One of: + * - & ^ | && ||
30989 OpenMP 3.1:
30991 reduction-operator:
30992 One of: + * - & ^ | && || min max
30994 OpenMP 4.0:
30996 reduction-operator:
30997 One of: + * - & ^ | && ||
30998 id-expression */
31000 static tree
31001 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
31003 enum tree_code code = ERROR_MARK;
31004 tree nlist, c, id = NULL_TREE;
31006 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31007 return list;
31009 switch (cp_lexer_peek_token (parser->lexer)->type)
31011 case CPP_PLUS: code = PLUS_EXPR; break;
31012 case CPP_MULT: code = MULT_EXPR; break;
31013 case CPP_MINUS: code = MINUS_EXPR; break;
31014 case CPP_AND: code = BIT_AND_EXPR; break;
31015 case CPP_XOR: code = BIT_XOR_EXPR; break;
31016 case CPP_OR: code = BIT_IOR_EXPR; break;
31017 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
31018 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
31019 default: break;
31022 if (code != ERROR_MARK)
31023 cp_lexer_consume_token (parser->lexer);
31024 else
31026 bool saved_colon_corrects_to_scope_p;
31027 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31028 parser->colon_corrects_to_scope_p = false;
31029 id = cp_parser_id_expression (parser, /*template_p=*/false,
31030 /*check_dependency_p=*/true,
31031 /*template_p=*/NULL,
31032 /*declarator_p=*/false,
31033 /*optional_p=*/false);
31034 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31035 if (identifier_p (id))
31037 const char *p = IDENTIFIER_POINTER (id);
31039 if (strcmp (p, "min") == 0)
31040 code = MIN_EXPR;
31041 else if (strcmp (p, "max") == 0)
31042 code = MAX_EXPR;
31043 else if (id == ansi_opname (PLUS_EXPR))
31044 code = PLUS_EXPR;
31045 else if (id == ansi_opname (MULT_EXPR))
31046 code = MULT_EXPR;
31047 else if (id == ansi_opname (MINUS_EXPR))
31048 code = MINUS_EXPR;
31049 else if (id == ansi_opname (BIT_AND_EXPR))
31050 code = BIT_AND_EXPR;
31051 else if (id == ansi_opname (BIT_IOR_EXPR))
31052 code = BIT_IOR_EXPR;
31053 else if (id == ansi_opname (BIT_XOR_EXPR))
31054 code = BIT_XOR_EXPR;
31055 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
31056 code = TRUTH_ANDIF_EXPR;
31057 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
31058 code = TRUTH_ORIF_EXPR;
31059 id = omp_reduction_id (code, id, NULL_TREE);
31060 tree scope = parser->scope;
31061 if (scope)
31062 id = build_qualified_name (NULL_TREE, scope, id, false);
31063 parser->scope = NULL_TREE;
31064 parser->qualifying_scope = NULL_TREE;
31065 parser->object_scope = NULL_TREE;
31067 else
31069 error ("invalid reduction-identifier");
31070 resync_fail:
31071 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31072 /*or_comma=*/false,
31073 /*consume_paren=*/true);
31074 return list;
31078 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31079 goto resync_fail;
31081 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
31082 NULL);
31083 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31085 OMP_CLAUSE_REDUCTION_CODE (c) = code;
31086 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
31089 return nlist;
31092 /* OpenMP 2.5:
31093 schedule ( schedule-kind )
31094 schedule ( schedule-kind , expression )
31096 schedule-kind:
31097 static | dynamic | guided | runtime | auto
31099 OpenMP 4.5:
31100 schedule ( schedule-modifier : schedule-kind )
31101 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
31103 schedule-modifier:
31104 simd
31105 monotonic
31106 nonmonotonic */
31108 static tree
31109 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
31111 tree c, t;
31112 int modifiers = 0, nmodifiers = 0;
31114 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31115 return list;
31117 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
31119 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31121 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31122 const char *p = IDENTIFIER_POINTER (id);
31123 if (strcmp ("simd", p) == 0)
31124 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
31125 else if (strcmp ("monotonic", p) == 0)
31126 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
31127 else if (strcmp ("nonmonotonic", p) == 0)
31128 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
31129 else
31130 break;
31131 cp_lexer_consume_token (parser->lexer);
31132 if (nmodifiers++ == 0
31133 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31134 cp_lexer_consume_token (parser->lexer);
31135 else
31137 cp_parser_require (parser, CPP_COLON, RT_COLON);
31138 break;
31142 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31144 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31145 const char *p = IDENTIFIER_POINTER (id);
31147 switch (p[0])
31149 case 'd':
31150 if (strcmp ("dynamic", p) != 0)
31151 goto invalid_kind;
31152 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
31153 break;
31155 case 'g':
31156 if (strcmp ("guided", p) != 0)
31157 goto invalid_kind;
31158 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
31159 break;
31161 case 'r':
31162 if (strcmp ("runtime", p) != 0)
31163 goto invalid_kind;
31164 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
31165 break;
31167 default:
31168 goto invalid_kind;
31171 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
31172 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
31173 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31174 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
31175 else
31176 goto invalid_kind;
31177 cp_lexer_consume_token (parser->lexer);
31179 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
31180 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
31181 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
31182 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
31184 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
31185 "specified");
31186 modifiers = 0;
31189 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31191 cp_token *token;
31192 cp_lexer_consume_token (parser->lexer);
31194 token = cp_lexer_peek_token (parser->lexer);
31195 t = cp_parser_assignment_expression (parser);
31197 if (t == error_mark_node)
31198 goto resync_fail;
31199 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
31200 error_at (token->location, "schedule %<runtime%> does not take "
31201 "a %<chunk_size%> parameter");
31202 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
31203 error_at (token->location, "schedule %<auto%> does not take "
31204 "a %<chunk_size%> parameter");
31205 else
31206 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
31208 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31209 goto resync_fail;
31211 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
31212 goto resync_fail;
31214 OMP_CLAUSE_SCHEDULE_KIND (c)
31215 = (enum omp_clause_schedule_kind)
31216 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
31218 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
31219 OMP_CLAUSE_CHAIN (c) = list;
31220 return c;
31222 invalid_kind:
31223 cp_parser_error (parser, "invalid schedule kind");
31224 resync_fail:
31225 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31226 /*or_comma=*/false,
31227 /*consume_paren=*/true);
31228 return list;
31231 /* OpenMP 3.0:
31232 untied */
31234 static tree
31235 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
31236 tree list, location_t location)
31238 tree c;
31240 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
31242 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
31243 OMP_CLAUSE_CHAIN (c) = list;
31244 return c;
31247 /* OpenMP 4.0:
31248 inbranch
31249 notinbranch */
31251 static tree
31252 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
31253 tree list, location_t location)
31255 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31256 tree c = build_omp_clause (location, code);
31257 OMP_CLAUSE_CHAIN (c) = list;
31258 return c;
31261 /* OpenMP 4.0:
31262 parallel
31264 sections
31265 taskgroup */
31267 static tree
31268 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
31269 enum omp_clause_code code,
31270 tree list, location_t location)
31272 tree c = build_omp_clause (location, code);
31273 OMP_CLAUSE_CHAIN (c) = list;
31274 return c;
31277 /* OpenMP 4.5:
31278 nogroup */
31280 static tree
31281 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
31282 tree list, location_t location)
31284 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
31285 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
31286 OMP_CLAUSE_CHAIN (c) = list;
31287 return c;
31290 /* OpenMP 4.5:
31291 simd
31292 threads */
31294 static tree
31295 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
31296 enum omp_clause_code code,
31297 tree list, location_t location)
31299 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31300 tree c = build_omp_clause (location, code);
31301 OMP_CLAUSE_CHAIN (c) = list;
31302 return c;
31305 /* OpenMP 4.0:
31306 num_teams ( expression ) */
31308 static tree
31309 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
31310 location_t location)
31312 tree t, c;
31314 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31315 return list;
31317 t = cp_parser_expression (parser);
31319 if (t == error_mark_node
31320 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31321 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31322 /*or_comma=*/false,
31323 /*consume_paren=*/true);
31325 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
31326 "num_teams", location);
31328 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
31329 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
31330 OMP_CLAUSE_CHAIN (c) = list;
31332 return c;
31335 /* OpenMP 4.0:
31336 thread_limit ( expression ) */
31338 static tree
31339 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
31340 location_t location)
31342 tree t, c;
31344 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31345 return list;
31347 t = cp_parser_expression (parser);
31349 if (t == error_mark_node
31350 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31351 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31352 /*or_comma=*/false,
31353 /*consume_paren=*/true);
31355 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
31356 "thread_limit", location);
31358 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
31359 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
31360 OMP_CLAUSE_CHAIN (c) = list;
31362 return c;
31365 /* OpenMP 4.0:
31366 aligned ( variable-list )
31367 aligned ( variable-list : constant-expression ) */
31369 static tree
31370 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
31372 tree nlist, c, alignment = NULL_TREE;
31373 bool colon;
31375 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31376 return list;
31378 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
31379 &colon);
31381 if (colon)
31383 alignment = cp_parser_constant_expression (parser);
31385 if (!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);
31390 if (alignment == error_mark_node)
31391 alignment = NULL_TREE;
31394 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31395 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
31397 return nlist;
31400 /* OpenMP 4.0:
31401 linear ( variable-list )
31402 linear ( variable-list : expression )
31404 OpenMP 4.5:
31405 linear ( modifier ( variable-list ) )
31406 linear ( modifier ( variable-list ) : expression ) */
31408 static tree
31409 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
31410 bool is_cilk_simd_fn, bool declare_simd)
31412 tree nlist, c, step = integer_one_node;
31413 bool colon;
31414 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
31416 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31417 return list;
31419 if (!is_cilk_simd_fn
31420 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31422 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31423 const char *p = IDENTIFIER_POINTER (id);
31425 if (strcmp ("ref", p) == 0)
31426 kind = OMP_CLAUSE_LINEAR_REF;
31427 else if (strcmp ("val", p) == 0)
31428 kind = OMP_CLAUSE_LINEAR_VAL;
31429 else if (strcmp ("uval", p) == 0)
31430 kind = OMP_CLAUSE_LINEAR_UVAL;
31431 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
31432 cp_lexer_consume_token (parser->lexer);
31433 else
31434 kind = OMP_CLAUSE_LINEAR_DEFAULT;
31437 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
31438 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
31439 &colon);
31440 else
31442 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
31443 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
31444 if (colon)
31445 cp_parser_require (parser, CPP_COLON, RT_COLON);
31446 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31447 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31448 /*or_comma=*/false,
31449 /*consume_paren=*/true);
31452 if (colon)
31454 step = NULL_TREE;
31455 if (declare_simd
31456 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
31457 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
31459 cp_token *token = cp_lexer_peek_token (parser->lexer);
31460 cp_parser_parse_tentatively (parser);
31461 step = cp_parser_id_expression (parser, /*template_p=*/false,
31462 /*check_dependency_p=*/true,
31463 /*template_p=*/NULL,
31464 /*declarator_p=*/false,
31465 /*optional_p=*/false);
31466 if (step != error_mark_node)
31467 step = cp_parser_lookup_name_simple (parser, step, token->location);
31468 if (step == error_mark_node)
31470 step = NULL_TREE;
31471 cp_parser_abort_tentative_parse (parser);
31473 else if (!cp_parser_parse_definitely (parser))
31474 step = NULL_TREE;
31476 if (!step)
31477 step = cp_parser_expression (parser);
31479 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
31481 sorry ("using parameters for %<linear%> step is not supported yet");
31482 step = integer_one_node;
31484 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31485 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31486 /*or_comma=*/false,
31487 /*consume_paren=*/true);
31489 if (step == error_mark_node)
31490 return list;
31493 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31495 OMP_CLAUSE_LINEAR_STEP (c) = step;
31496 OMP_CLAUSE_LINEAR_KIND (c) = kind;
31499 return nlist;
31502 /* OpenMP 4.0:
31503 safelen ( constant-expression ) */
31505 static tree
31506 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
31507 location_t location)
31509 tree t, c;
31511 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31512 return list;
31514 t = cp_parser_constant_expression (parser);
31516 if (t == error_mark_node
31517 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31518 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31519 /*or_comma=*/false,
31520 /*consume_paren=*/true);
31522 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
31524 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
31525 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
31526 OMP_CLAUSE_CHAIN (c) = list;
31528 return c;
31531 /* OpenMP 4.0:
31532 simdlen ( constant-expression ) */
31534 static tree
31535 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
31536 location_t location)
31538 tree t, c;
31540 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31541 return list;
31543 t = cp_parser_constant_expression (parser);
31545 if (t == error_mark_node
31546 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31547 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31548 /*or_comma=*/false,
31549 /*consume_paren=*/true);
31551 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
31553 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
31554 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
31555 OMP_CLAUSE_CHAIN (c) = list;
31557 return c;
31560 /* OpenMP 4.5:
31561 vec:
31562 identifier [+/- integer]
31563 vec , identifier [+/- integer]
31566 static tree
31567 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
31568 tree list)
31570 tree vec = NULL;
31572 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31574 cp_parser_error (parser, "expected identifier");
31575 return list;
31578 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31580 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
31581 tree t, identifier = cp_parser_identifier (parser);
31582 tree addend = NULL;
31584 if (identifier == error_mark_node)
31585 t = error_mark_node;
31586 else
31588 t = cp_parser_lookup_name_simple
31589 (parser, identifier,
31590 cp_lexer_peek_token (parser->lexer)->location);
31591 if (t == error_mark_node)
31592 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
31593 id_loc);
31596 bool neg = false;
31597 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
31598 neg = true;
31599 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
31601 addend = integer_zero_node;
31602 goto add_to_vector;
31604 cp_lexer_consume_token (parser->lexer);
31606 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
31608 cp_parser_error (parser, "expected integer");
31609 return list;
31612 addend = cp_lexer_peek_token (parser->lexer)->u.value;
31613 if (TREE_CODE (addend) != INTEGER_CST)
31615 cp_parser_error (parser, "expected integer");
31616 return list;
31618 cp_lexer_consume_token (parser->lexer);
31620 add_to_vector:
31621 if (t != error_mark_node)
31623 vec = tree_cons (addend, t, vec);
31624 if (neg)
31625 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
31628 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31629 break;
31631 cp_lexer_consume_token (parser->lexer);
31634 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
31636 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
31637 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
31638 OMP_CLAUSE_DECL (u) = nreverse (vec);
31639 OMP_CLAUSE_CHAIN (u) = list;
31640 return u;
31642 return list;
31645 /* OpenMP 4.0:
31646 depend ( depend-kind : variable-list )
31648 depend-kind:
31649 in | out | inout
31651 OpenMP 4.5:
31652 depend ( source )
31654 depend ( sink : vec ) */
31656 static tree
31657 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
31659 tree nlist, c;
31660 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
31662 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31663 return list;
31665 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31667 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31668 const char *p = IDENTIFIER_POINTER (id);
31670 if (strcmp ("in", p) == 0)
31671 kind = OMP_CLAUSE_DEPEND_IN;
31672 else if (strcmp ("inout", p) == 0)
31673 kind = OMP_CLAUSE_DEPEND_INOUT;
31674 else if (strcmp ("out", p) == 0)
31675 kind = OMP_CLAUSE_DEPEND_OUT;
31676 else if (strcmp ("source", p) == 0)
31677 kind = OMP_CLAUSE_DEPEND_SOURCE;
31678 else if (strcmp ("sink", p) == 0)
31679 kind = OMP_CLAUSE_DEPEND_SINK;
31680 else
31681 goto invalid_kind;
31683 else
31684 goto invalid_kind;
31686 cp_lexer_consume_token (parser->lexer);
31688 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
31690 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
31691 OMP_CLAUSE_DEPEND_KIND (c) = kind;
31692 OMP_CLAUSE_DECL (c) = NULL_TREE;
31693 OMP_CLAUSE_CHAIN (c) = list;
31694 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31695 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31696 /*or_comma=*/false,
31697 /*consume_paren=*/true);
31698 return c;
31701 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31702 goto resync_fail;
31704 if (kind == OMP_CLAUSE_DEPEND_SINK)
31705 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
31706 else
31708 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
31709 list, NULL);
31711 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31712 OMP_CLAUSE_DEPEND_KIND (c) = kind;
31714 return nlist;
31716 invalid_kind:
31717 cp_parser_error (parser, "invalid depend kind");
31718 resync_fail:
31719 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31720 /*or_comma=*/false,
31721 /*consume_paren=*/true);
31722 return list;
31725 /* OpenMP 4.0:
31726 map ( map-kind : variable-list )
31727 map ( variable-list )
31729 map-kind:
31730 alloc | to | from | tofrom
31732 OpenMP 4.5:
31733 map-kind:
31734 alloc | to | from | tofrom | release | delete
31736 map ( always [,] map-kind: variable-list ) */
31738 static tree
31739 cp_parser_omp_clause_map (cp_parser *parser, tree list)
31741 tree nlist, c;
31742 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
31743 bool always = false;
31745 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31746 return list;
31748 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31750 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31751 const char *p = IDENTIFIER_POINTER (id);
31753 if (strcmp ("always", p) == 0)
31755 int nth = 2;
31756 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
31757 nth++;
31758 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
31759 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
31760 == RID_DELETE))
31761 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
31762 == CPP_COLON))
31764 always = true;
31765 cp_lexer_consume_token (parser->lexer);
31766 if (nth == 3)
31767 cp_lexer_consume_token (parser->lexer);
31772 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
31773 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
31775 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31776 const char *p = IDENTIFIER_POINTER (id);
31778 if (strcmp ("alloc", p) == 0)
31779 kind = GOMP_MAP_ALLOC;
31780 else if (strcmp ("to", p) == 0)
31781 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
31782 else if (strcmp ("from", p) == 0)
31783 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
31784 else if (strcmp ("tofrom", p) == 0)
31785 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
31786 else if (strcmp ("release", p) == 0)
31787 kind = GOMP_MAP_RELEASE;
31788 else
31790 cp_parser_error (parser, "invalid map kind");
31791 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31792 /*or_comma=*/false,
31793 /*consume_paren=*/true);
31794 return list;
31796 cp_lexer_consume_token (parser->lexer);
31797 cp_lexer_consume_token (parser->lexer);
31799 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
31800 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
31802 kind = GOMP_MAP_DELETE;
31803 cp_lexer_consume_token (parser->lexer);
31804 cp_lexer_consume_token (parser->lexer);
31807 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
31808 NULL);
31810 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31811 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31813 return nlist;
31816 /* OpenMP 4.0:
31817 device ( expression ) */
31819 static tree
31820 cp_parser_omp_clause_device (cp_parser *parser, tree list,
31821 location_t location)
31823 tree t, c;
31825 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31826 return list;
31828 t = cp_parser_expression (parser);
31830 if (t == error_mark_node
31831 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31832 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31833 /*or_comma=*/false,
31834 /*consume_paren=*/true);
31836 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
31837 "device", location);
31839 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
31840 OMP_CLAUSE_DEVICE_ID (c) = t;
31841 OMP_CLAUSE_CHAIN (c) = list;
31843 return c;
31846 /* OpenMP 4.0:
31847 dist_schedule ( static )
31848 dist_schedule ( static , expression ) */
31850 static tree
31851 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
31852 location_t location)
31854 tree c, t;
31856 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31857 return list;
31859 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
31861 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
31862 goto invalid_kind;
31863 cp_lexer_consume_token (parser->lexer);
31865 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31867 cp_lexer_consume_token (parser->lexer);
31869 t = cp_parser_assignment_expression (parser);
31871 if (t == error_mark_node)
31872 goto resync_fail;
31873 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
31875 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31876 goto resync_fail;
31878 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
31879 goto resync_fail;
31881 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
31882 location);
31883 OMP_CLAUSE_CHAIN (c) = list;
31884 return c;
31886 invalid_kind:
31887 cp_parser_error (parser, "invalid dist_schedule kind");
31888 resync_fail:
31889 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31890 /*or_comma=*/false,
31891 /*consume_paren=*/true);
31892 return list;
31895 /* OpenMP 4.0:
31896 proc_bind ( proc-bind-kind )
31898 proc-bind-kind:
31899 master | close | spread */
31901 static tree
31902 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
31903 location_t location)
31905 tree c;
31906 enum omp_clause_proc_bind_kind kind;
31908 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31909 return list;
31911 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31913 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31914 const char *p = IDENTIFIER_POINTER (id);
31916 if (strcmp ("master", p) == 0)
31917 kind = OMP_CLAUSE_PROC_BIND_MASTER;
31918 else if (strcmp ("close", p) == 0)
31919 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
31920 else if (strcmp ("spread", p) == 0)
31921 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
31922 else
31923 goto invalid_kind;
31925 else
31926 goto invalid_kind;
31928 cp_lexer_consume_token (parser->lexer);
31929 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
31930 goto resync_fail;
31932 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
31933 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
31934 location);
31935 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
31936 OMP_CLAUSE_CHAIN (c) = list;
31937 return c;
31939 invalid_kind:
31940 cp_parser_error (parser, "invalid depend kind");
31941 resync_fail:
31942 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31943 /*or_comma=*/false,
31944 /*consume_paren=*/true);
31945 return list;
31948 /* OpenACC:
31949 async [( int-expr )] */
31951 static tree
31952 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
31954 tree c, t;
31955 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31957 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
31959 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31961 cp_lexer_consume_token (parser->lexer);
31963 t = cp_parser_expression (parser);
31964 if (t == error_mark_node
31965 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31966 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31967 /*or_comma=*/false,
31968 /*consume_paren=*/true);
31971 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
31973 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
31974 OMP_CLAUSE_ASYNC_EXPR (c) = t;
31975 OMP_CLAUSE_CHAIN (c) = list;
31976 list = c;
31978 return list;
31981 /* Parse all OpenACC clauses. The set clauses allowed by the directive
31982 is a bitmask in MASK. Return the list of clauses found. */
31984 static tree
31985 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
31986 const char *where, cp_token *pragma_tok,
31987 bool finish_p = true)
31989 tree clauses = NULL;
31990 bool first = true;
31992 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31994 location_t here;
31995 pragma_omp_clause c_kind;
31996 omp_clause_code code;
31997 const char *c_name;
31998 tree prev = clauses;
32000 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32001 cp_lexer_consume_token (parser->lexer);
32003 here = cp_lexer_peek_token (parser->lexer)->location;
32004 c_kind = cp_parser_omp_clause_name (parser);
32006 switch (c_kind)
32008 case PRAGMA_OACC_CLAUSE_ASYNC:
32009 clauses = cp_parser_oacc_clause_async (parser, clauses);
32010 c_name = "async";
32011 break;
32012 case PRAGMA_OACC_CLAUSE_AUTO:
32013 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
32014 clauses, here);
32015 c_name = "auto";
32016 break;
32017 case PRAGMA_OACC_CLAUSE_COLLAPSE:
32018 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
32019 c_name = "collapse";
32020 break;
32021 case PRAGMA_OACC_CLAUSE_COPY:
32022 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32023 c_name = "copy";
32024 break;
32025 case PRAGMA_OACC_CLAUSE_COPYIN:
32026 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32027 c_name = "copyin";
32028 break;
32029 case PRAGMA_OACC_CLAUSE_COPYOUT:
32030 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32031 c_name = "copyout";
32032 break;
32033 case PRAGMA_OACC_CLAUSE_CREATE:
32034 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32035 c_name = "create";
32036 break;
32037 case PRAGMA_OACC_CLAUSE_DELETE:
32038 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32039 c_name = "delete";
32040 break;
32041 case PRAGMA_OMP_CLAUSE_DEFAULT:
32042 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
32043 c_name = "default";
32044 break;
32045 case PRAGMA_OACC_CLAUSE_DEVICE:
32046 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32047 c_name = "device";
32048 break;
32049 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
32050 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
32051 c_name = "deviceptr";
32052 break;
32053 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
32054 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32055 c_name = "device_resident";
32056 break;
32057 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
32058 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32059 clauses);
32060 c_name = "firstprivate";
32061 break;
32062 case PRAGMA_OACC_CLAUSE_GANG:
32063 c_name = "gang";
32064 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
32065 c_name, clauses);
32066 break;
32067 case PRAGMA_OACC_CLAUSE_HOST:
32068 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32069 c_name = "host";
32070 break;
32071 case PRAGMA_OACC_CLAUSE_IF:
32072 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
32073 c_name = "if";
32074 break;
32075 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
32076 clauses = cp_parser_oacc_simple_clause (parser,
32077 OMP_CLAUSE_INDEPENDENT,
32078 clauses, here);
32079 c_name = "independent";
32080 break;
32081 case PRAGMA_OACC_CLAUSE_LINK:
32082 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32083 c_name = "link";
32084 break;
32085 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
32086 code = OMP_CLAUSE_NUM_GANGS;
32087 c_name = "num_gangs";
32088 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32089 clauses);
32090 break;
32091 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
32092 c_name = "num_workers";
32093 code = OMP_CLAUSE_NUM_WORKERS;
32094 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32095 clauses);
32096 break;
32097 case PRAGMA_OACC_CLAUSE_PRESENT:
32098 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32099 c_name = "present";
32100 break;
32101 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
32102 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32103 c_name = "present_or_copy";
32104 break;
32105 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
32106 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32107 c_name = "present_or_copyin";
32108 break;
32109 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
32110 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32111 c_name = "present_or_copyout";
32112 break;
32113 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
32114 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32115 c_name = "present_or_create";
32116 break;
32117 case PRAGMA_OACC_CLAUSE_PRIVATE:
32118 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
32119 clauses);
32120 c_name = "private";
32121 break;
32122 case PRAGMA_OACC_CLAUSE_REDUCTION:
32123 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32124 c_name = "reduction";
32125 break;
32126 case PRAGMA_OACC_CLAUSE_SELF:
32127 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32128 c_name = "self";
32129 break;
32130 case PRAGMA_OACC_CLAUSE_SEQ:
32131 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
32132 clauses, here);
32133 c_name = "seq";
32134 break;
32135 case PRAGMA_OACC_CLAUSE_TILE:
32136 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
32137 c_name = "tile";
32138 break;
32139 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
32140 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
32141 clauses);
32142 c_name = "use_device";
32143 break;
32144 case PRAGMA_OACC_CLAUSE_VECTOR:
32145 c_name = "vector";
32146 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
32147 c_name, clauses);
32148 break;
32149 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
32150 c_name = "vector_length";
32151 code = OMP_CLAUSE_VECTOR_LENGTH;
32152 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32153 clauses);
32154 break;
32155 case PRAGMA_OACC_CLAUSE_WAIT:
32156 clauses = cp_parser_oacc_clause_wait (parser, clauses);
32157 c_name = "wait";
32158 break;
32159 case PRAGMA_OACC_CLAUSE_WORKER:
32160 c_name = "worker";
32161 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
32162 c_name, clauses);
32163 break;
32164 default:
32165 cp_parser_error (parser, "expected %<#pragma acc%> clause");
32166 goto saw_error;
32169 first = false;
32171 if (((mask >> c_kind) & 1) == 0)
32173 /* Remove the invalid clause(s) from the list to avoid
32174 confusing the rest of the compiler. */
32175 clauses = prev;
32176 error_at (here, "%qs is not valid for %qs", c_name, where);
32180 saw_error:
32181 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32183 if (finish_p)
32184 return finish_omp_clauses (clauses, false);
32186 return clauses;
32189 /* Parse all OpenMP clauses. The set clauses allowed by the directive
32190 is a bitmask in MASK. Return the list of clauses found; the result
32191 of clause default goes in *pdefault. */
32193 static tree
32194 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
32195 const char *where, cp_token *pragma_tok,
32196 bool finish_p = true)
32198 tree clauses = NULL;
32199 bool first = true;
32200 cp_token *token = NULL;
32202 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32204 pragma_omp_clause c_kind;
32205 const char *c_name;
32206 tree prev = clauses;
32208 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32209 cp_lexer_consume_token (parser->lexer);
32211 token = cp_lexer_peek_token (parser->lexer);
32212 c_kind = cp_parser_omp_clause_name (parser);
32214 switch (c_kind)
32216 case PRAGMA_OMP_CLAUSE_COLLAPSE:
32217 clauses = cp_parser_omp_clause_collapse (parser, clauses,
32218 token->location);
32219 c_name = "collapse";
32220 break;
32221 case PRAGMA_OMP_CLAUSE_COPYIN:
32222 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
32223 c_name = "copyin";
32224 break;
32225 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
32226 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
32227 clauses);
32228 c_name = "copyprivate";
32229 break;
32230 case PRAGMA_OMP_CLAUSE_DEFAULT:
32231 clauses = cp_parser_omp_clause_default (parser, clauses,
32232 token->location, false);
32233 c_name = "default";
32234 break;
32235 case PRAGMA_OMP_CLAUSE_FINAL:
32236 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
32237 c_name = "final";
32238 break;
32239 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
32240 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32241 clauses);
32242 c_name = "firstprivate";
32243 break;
32244 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
32245 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
32246 token->location);
32247 c_name = "grainsize";
32248 break;
32249 case PRAGMA_OMP_CLAUSE_HINT:
32250 clauses = cp_parser_omp_clause_hint (parser, clauses,
32251 token->location);
32252 c_name = "hint";
32253 break;
32254 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
32255 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
32256 token->location);
32257 c_name = "defaultmap";
32258 break;
32259 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
32260 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
32261 clauses);
32262 c_name = "use_device_ptr";
32263 break;
32264 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
32265 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
32266 clauses);
32267 c_name = "is_device_ptr";
32268 break;
32269 case PRAGMA_OMP_CLAUSE_IF:
32270 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
32271 true);
32272 c_name = "if";
32273 break;
32274 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
32275 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
32276 clauses);
32277 c_name = "lastprivate";
32278 break;
32279 case PRAGMA_OMP_CLAUSE_MERGEABLE:
32280 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
32281 token->location);
32282 c_name = "mergeable";
32283 break;
32284 case PRAGMA_OMP_CLAUSE_NOWAIT:
32285 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
32286 c_name = "nowait";
32287 break;
32288 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
32289 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
32290 token->location);
32291 c_name = "num_tasks";
32292 break;
32293 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
32294 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
32295 token->location);
32296 c_name = "num_threads";
32297 break;
32298 case PRAGMA_OMP_CLAUSE_ORDERED:
32299 clauses = cp_parser_omp_clause_ordered (parser, clauses,
32300 token->location);
32301 c_name = "ordered";
32302 break;
32303 case PRAGMA_OMP_CLAUSE_PRIORITY:
32304 clauses = cp_parser_omp_clause_priority (parser, clauses,
32305 token->location);
32306 c_name = "priority";
32307 break;
32308 case PRAGMA_OMP_CLAUSE_PRIVATE:
32309 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
32310 clauses);
32311 c_name = "private";
32312 break;
32313 case PRAGMA_OMP_CLAUSE_REDUCTION:
32314 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32315 c_name = "reduction";
32316 break;
32317 case PRAGMA_OMP_CLAUSE_SCHEDULE:
32318 clauses = cp_parser_omp_clause_schedule (parser, clauses,
32319 token->location);
32320 c_name = "schedule";
32321 break;
32322 case PRAGMA_OMP_CLAUSE_SHARED:
32323 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
32324 clauses);
32325 c_name = "shared";
32326 break;
32327 case PRAGMA_OMP_CLAUSE_UNTIED:
32328 clauses = cp_parser_omp_clause_untied (parser, clauses,
32329 token->location);
32330 c_name = "untied";
32331 break;
32332 case PRAGMA_OMP_CLAUSE_INBRANCH:
32333 case PRAGMA_CILK_CLAUSE_MASK:
32334 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
32335 clauses, token->location);
32336 c_name = "inbranch";
32337 break;
32338 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
32339 case PRAGMA_CILK_CLAUSE_NOMASK:
32340 clauses = cp_parser_omp_clause_branch (parser,
32341 OMP_CLAUSE_NOTINBRANCH,
32342 clauses, token->location);
32343 c_name = "notinbranch";
32344 break;
32345 case PRAGMA_OMP_CLAUSE_PARALLEL:
32346 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
32347 clauses, token->location);
32348 c_name = "parallel";
32349 if (!first)
32351 clause_not_first:
32352 error_at (token->location, "%qs must be the first clause of %qs",
32353 c_name, where);
32354 clauses = prev;
32356 break;
32357 case PRAGMA_OMP_CLAUSE_FOR:
32358 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
32359 clauses, token->location);
32360 c_name = "for";
32361 if (!first)
32362 goto clause_not_first;
32363 break;
32364 case PRAGMA_OMP_CLAUSE_SECTIONS:
32365 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
32366 clauses, token->location);
32367 c_name = "sections";
32368 if (!first)
32369 goto clause_not_first;
32370 break;
32371 case PRAGMA_OMP_CLAUSE_TASKGROUP:
32372 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
32373 clauses, token->location);
32374 c_name = "taskgroup";
32375 if (!first)
32376 goto clause_not_first;
32377 break;
32378 case PRAGMA_OMP_CLAUSE_LINK:
32379 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
32380 c_name = "to";
32381 break;
32382 case PRAGMA_OMP_CLAUSE_TO:
32383 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
32384 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
32385 clauses);
32386 else
32387 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
32388 c_name = "to";
32389 break;
32390 case PRAGMA_OMP_CLAUSE_FROM:
32391 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
32392 c_name = "from";
32393 break;
32394 case PRAGMA_OMP_CLAUSE_UNIFORM:
32395 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
32396 clauses);
32397 c_name = "uniform";
32398 break;
32399 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
32400 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
32401 token->location);
32402 c_name = "num_teams";
32403 break;
32404 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
32405 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
32406 token->location);
32407 c_name = "thread_limit";
32408 break;
32409 case PRAGMA_OMP_CLAUSE_ALIGNED:
32410 clauses = cp_parser_omp_clause_aligned (parser, clauses);
32411 c_name = "aligned";
32412 break;
32413 case PRAGMA_OMP_CLAUSE_LINEAR:
32415 bool cilk_simd_fn = false, declare_simd = false;
32416 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
32417 cilk_simd_fn = true;
32418 else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
32419 declare_simd = true;
32420 clauses = cp_parser_omp_clause_linear (parser, clauses,
32421 cilk_simd_fn, declare_simd);
32423 c_name = "linear";
32424 break;
32425 case PRAGMA_OMP_CLAUSE_DEPEND:
32426 clauses = cp_parser_omp_clause_depend (parser, clauses,
32427 token->location);
32428 c_name = "depend";
32429 break;
32430 case PRAGMA_OMP_CLAUSE_MAP:
32431 clauses = cp_parser_omp_clause_map (parser, clauses);
32432 c_name = "map";
32433 break;
32434 case PRAGMA_OMP_CLAUSE_DEVICE:
32435 clauses = cp_parser_omp_clause_device (parser, clauses,
32436 token->location);
32437 c_name = "device";
32438 break;
32439 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
32440 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
32441 token->location);
32442 c_name = "dist_schedule";
32443 break;
32444 case PRAGMA_OMP_CLAUSE_PROC_BIND:
32445 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
32446 token->location);
32447 c_name = "proc_bind";
32448 break;
32449 case PRAGMA_OMP_CLAUSE_SAFELEN:
32450 clauses = cp_parser_omp_clause_safelen (parser, clauses,
32451 token->location);
32452 c_name = "safelen";
32453 break;
32454 case PRAGMA_OMP_CLAUSE_SIMDLEN:
32455 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
32456 token->location);
32457 c_name = "simdlen";
32458 break;
32459 case PRAGMA_OMP_CLAUSE_NOGROUP:
32460 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
32461 token->location);
32462 c_name = "nogroup";
32463 break;
32464 case PRAGMA_OMP_CLAUSE_THREADS:
32465 clauses
32466 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
32467 clauses, token->location);
32468 c_name = "threads";
32469 break;
32470 case PRAGMA_OMP_CLAUSE_SIMD:
32471 clauses
32472 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
32473 clauses, token->location);
32474 c_name = "simd";
32475 break;
32476 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
32477 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
32478 c_name = "simdlen";
32479 break;
32480 default:
32481 cp_parser_error (parser, "expected %<#pragma omp%> clause");
32482 goto saw_error;
32485 first = false;
32487 if (((mask >> c_kind) & 1) == 0)
32489 /* Remove the invalid clause(s) from the list to avoid
32490 confusing the rest of the compiler. */
32491 clauses = prev;
32492 error_at (token->location, "%qs is not valid for %qs", c_name, where);
32495 saw_error:
32496 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
32497 no reason to skip to the end. */
32498 if (!(flag_cilkplus && pragma_tok == NULL))
32499 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32500 if (finish_p)
32502 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
32503 return finish_omp_clauses (clauses, false, true);
32504 else
32505 return finish_omp_clauses (clauses, true);
32507 return clauses;
32510 /* OpenMP 2.5:
32511 structured-block:
32512 statement
32514 In practice, we're also interested in adding the statement to an
32515 outer node. So it is convenient if we work around the fact that
32516 cp_parser_statement calls add_stmt. */
32518 static unsigned
32519 cp_parser_begin_omp_structured_block (cp_parser *parser)
32521 unsigned save = parser->in_statement;
32523 /* Only move the values to IN_OMP_BLOCK if they weren't false.
32524 This preserves the "not within loop or switch" style error messages
32525 for nonsense cases like
32526 void foo() {
32527 #pragma omp single
32528 break;
32531 if (parser->in_statement)
32532 parser->in_statement = IN_OMP_BLOCK;
32534 return save;
32537 static void
32538 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
32540 parser->in_statement = save;
32543 static tree
32544 cp_parser_omp_structured_block (cp_parser *parser)
32546 tree stmt = begin_omp_structured_block ();
32547 unsigned int save = cp_parser_begin_omp_structured_block (parser);
32549 cp_parser_statement (parser, NULL_TREE, false, NULL);
32551 cp_parser_end_omp_structured_block (parser, save);
32552 return finish_omp_structured_block (stmt);
32555 /* OpenMP 2.5:
32556 # pragma omp atomic new-line
32557 expression-stmt
32559 expression-stmt:
32560 x binop= expr | x++ | ++x | x-- | --x
32561 binop:
32562 +, *, -, /, &, ^, |, <<, >>
32564 where x is an lvalue expression with scalar type.
32566 OpenMP 3.1:
32567 # pragma omp atomic new-line
32568 update-stmt
32570 # pragma omp atomic read new-line
32571 read-stmt
32573 # pragma omp atomic write new-line
32574 write-stmt
32576 # pragma omp atomic update new-line
32577 update-stmt
32579 # pragma omp atomic capture new-line
32580 capture-stmt
32582 # pragma omp atomic capture new-line
32583 capture-block
32585 read-stmt:
32586 v = x
32587 write-stmt:
32588 x = expr
32589 update-stmt:
32590 expression-stmt | x = x binop expr
32591 capture-stmt:
32592 v = expression-stmt
32593 capture-block:
32594 { v = x; update-stmt; } | { update-stmt; v = x; }
32596 OpenMP 4.0:
32597 update-stmt:
32598 expression-stmt | x = x binop expr | x = expr binop x
32599 capture-stmt:
32600 v = update-stmt
32601 capture-block:
32602 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
32604 where x and v are lvalue expressions with scalar type. */
32606 static void
32607 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
32609 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
32610 tree rhs1 = NULL_TREE, orig_lhs;
32611 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
32612 bool structured_block = false;
32613 bool seq_cst = false;
32615 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32617 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32618 const char *p = IDENTIFIER_POINTER (id);
32620 if (!strcmp (p, "seq_cst"))
32622 seq_cst = true;
32623 cp_lexer_consume_token (parser->lexer);
32624 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
32625 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
32626 cp_lexer_consume_token (parser->lexer);
32629 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32631 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32632 const char *p = IDENTIFIER_POINTER (id);
32634 if (!strcmp (p, "read"))
32635 code = OMP_ATOMIC_READ;
32636 else if (!strcmp (p, "write"))
32637 code = NOP_EXPR;
32638 else if (!strcmp (p, "update"))
32639 code = OMP_ATOMIC;
32640 else if (!strcmp (p, "capture"))
32641 code = OMP_ATOMIC_CAPTURE_NEW;
32642 else
32643 p = NULL;
32644 if (p)
32645 cp_lexer_consume_token (parser->lexer);
32647 if (!seq_cst)
32649 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
32650 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
32651 cp_lexer_consume_token (parser->lexer);
32653 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32655 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32656 const char *p = IDENTIFIER_POINTER (id);
32658 if (!strcmp (p, "seq_cst"))
32660 seq_cst = true;
32661 cp_lexer_consume_token (parser->lexer);
32665 cp_parser_require_pragma_eol (parser, pragma_tok);
32667 switch (code)
32669 case OMP_ATOMIC_READ:
32670 case NOP_EXPR: /* atomic write */
32671 v = cp_parser_unary_expression (parser);
32672 if (v == error_mark_node)
32673 goto saw_error;
32674 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32675 goto saw_error;
32676 if (code == NOP_EXPR)
32677 lhs = cp_parser_expression (parser);
32678 else
32679 lhs = cp_parser_unary_expression (parser);
32680 if (lhs == error_mark_node)
32681 goto saw_error;
32682 if (code == NOP_EXPR)
32684 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
32685 opcode. */
32686 code = OMP_ATOMIC;
32687 rhs = lhs;
32688 lhs = v;
32689 v = NULL_TREE;
32691 goto done;
32692 case OMP_ATOMIC_CAPTURE_NEW:
32693 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
32695 cp_lexer_consume_token (parser->lexer);
32696 structured_block = true;
32698 else
32700 v = cp_parser_unary_expression (parser);
32701 if (v == error_mark_node)
32702 goto saw_error;
32703 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32704 goto saw_error;
32706 default:
32707 break;
32710 restart:
32711 lhs = cp_parser_unary_expression (parser);
32712 orig_lhs = lhs;
32713 switch (TREE_CODE (lhs))
32715 case ERROR_MARK:
32716 goto saw_error;
32718 case POSTINCREMENT_EXPR:
32719 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
32720 code = OMP_ATOMIC_CAPTURE_OLD;
32721 /* FALLTHROUGH */
32722 case PREINCREMENT_EXPR:
32723 lhs = TREE_OPERAND (lhs, 0);
32724 opcode = PLUS_EXPR;
32725 rhs = integer_one_node;
32726 break;
32728 case POSTDECREMENT_EXPR:
32729 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
32730 code = OMP_ATOMIC_CAPTURE_OLD;
32731 /* FALLTHROUGH */
32732 case PREDECREMENT_EXPR:
32733 lhs = TREE_OPERAND (lhs, 0);
32734 opcode = MINUS_EXPR;
32735 rhs = integer_one_node;
32736 break;
32738 case COMPOUND_EXPR:
32739 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
32740 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
32741 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
32742 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
32743 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
32744 (TREE_OPERAND (lhs, 1), 0), 0)))
32745 == BOOLEAN_TYPE)
32746 /* Undo effects of boolean_increment for post {in,de}crement. */
32747 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
32748 /* FALLTHRU */
32749 case MODIFY_EXPR:
32750 if (TREE_CODE (lhs) == MODIFY_EXPR
32751 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
32753 /* Undo effects of boolean_increment. */
32754 if (integer_onep (TREE_OPERAND (lhs, 1)))
32756 /* This is pre or post increment. */
32757 rhs = TREE_OPERAND (lhs, 1);
32758 lhs = TREE_OPERAND (lhs, 0);
32759 opcode = NOP_EXPR;
32760 if (code == OMP_ATOMIC_CAPTURE_NEW
32761 && !structured_block
32762 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
32763 code = OMP_ATOMIC_CAPTURE_OLD;
32764 break;
32767 /* FALLTHRU */
32768 default:
32769 switch (cp_lexer_peek_token (parser->lexer)->type)
32771 case CPP_MULT_EQ:
32772 opcode = MULT_EXPR;
32773 break;
32774 case CPP_DIV_EQ:
32775 opcode = TRUNC_DIV_EXPR;
32776 break;
32777 case CPP_PLUS_EQ:
32778 opcode = PLUS_EXPR;
32779 break;
32780 case CPP_MINUS_EQ:
32781 opcode = MINUS_EXPR;
32782 break;
32783 case CPP_LSHIFT_EQ:
32784 opcode = LSHIFT_EXPR;
32785 break;
32786 case CPP_RSHIFT_EQ:
32787 opcode = RSHIFT_EXPR;
32788 break;
32789 case CPP_AND_EQ:
32790 opcode = BIT_AND_EXPR;
32791 break;
32792 case CPP_OR_EQ:
32793 opcode = BIT_IOR_EXPR;
32794 break;
32795 case CPP_XOR_EQ:
32796 opcode = BIT_XOR_EXPR;
32797 break;
32798 case CPP_EQ:
32799 enum cp_parser_prec oprec;
32800 cp_token *token;
32801 cp_lexer_consume_token (parser->lexer);
32802 cp_parser_parse_tentatively (parser);
32803 rhs1 = cp_parser_simple_cast_expression (parser);
32804 if (rhs1 == error_mark_node)
32806 cp_parser_abort_tentative_parse (parser);
32807 cp_parser_simple_cast_expression (parser);
32808 goto saw_error;
32810 token = cp_lexer_peek_token (parser->lexer);
32811 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
32813 cp_parser_abort_tentative_parse (parser);
32814 cp_parser_parse_tentatively (parser);
32815 rhs = cp_parser_binary_expression (parser, false, true,
32816 PREC_NOT_OPERATOR, NULL);
32817 if (rhs == error_mark_node)
32819 cp_parser_abort_tentative_parse (parser);
32820 cp_parser_binary_expression (parser, false, true,
32821 PREC_NOT_OPERATOR, NULL);
32822 goto saw_error;
32824 switch (TREE_CODE (rhs))
32826 case MULT_EXPR:
32827 case TRUNC_DIV_EXPR:
32828 case RDIV_EXPR:
32829 case PLUS_EXPR:
32830 case MINUS_EXPR:
32831 case LSHIFT_EXPR:
32832 case RSHIFT_EXPR:
32833 case BIT_AND_EXPR:
32834 case BIT_IOR_EXPR:
32835 case BIT_XOR_EXPR:
32836 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
32838 if (cp_parser_parse_definitely (parser))
32840 opcode = TREE_CODE (rhs);
32841 rhs1 = TREE_OPERAND (rhs, 0);
32842 rhs = TREE_OPERAND (rhs, 1);
32843 goto stmt_done;
32845 else
32846 goto saw_error;
32848 break;
32849 default:
32850 break;
32852 cp_parser_abort_tentative_parse (parser);
32853 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
32855 rhs = cp_parser_expression (parser);
32856 if (rhs == error_mark_node)
32857 goto saw_error;
32858 opcode = NOP_EXPR;
32859 rhs1 = NULL_TREE;
32860 goto stmt_done;
32862 cp_parser_error (parser,
32863 "invalid form of %<#pragma omp atomic%>");
32864 goto saw_error;
32866 if (!cp_parser_parse_definitely (parser))
32867 goto saw_error;
32868 switch (token->type)
32870 case CPP_SEMICOLON:
32871 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
32873 code = OMP_ATOMIC_CAPTURE_OLD;
32874 v = lhs;
32875 lhs = NULL_TREE;
32876 lhs1 = rhs1;
32877 rhs1 = NULL_TREE;
32878 cp_lexer_consume_token (parser->lexer);
32879 goto restart;
32881 else if (structured_block)
32883 opcode = NOP_EXPR;
32884 rhs = rhs1;
32885 rhs1 = NULL_TREE;
32886 goto stmt_done;
32888 cp_parser_error (parser,
32889 "invalid form of %<#pragma omp atomic%>");
32890 goto saw_error;
32891 case CPP_MULT:
32892 opcode = MULT_EXPR;
32893 break;
32894 case CPP_DIV:
32895 opcode = TRUNC_DIV_EXPR;
32896 break;
32897 case CPP_PLUS:
32898 opcode = PLUS_EXPR;
32899 break;
32900 case CPP_MINUS:
32901 opcode = MINUS_EXPR;
32902 break;
32903 case CPP_LSHIFT:
32904 opcode = LSHIFT_EXPR;
32905 break;
32906 case CPP_RSHIFT:
32907 opcode = RSHIFT_EXPR;
32908 break;
32909 case CPP_AND:
32910 opcode = BIT_AND_EXPR;
32911 break;
32912 case CPP_OR:
32913 opcode = BIT_IOR_EXPR;
32914 break;
32915 case CPP_XOR:
32916 opcode = BIT_XOR_EXPR;
32917 break;
32918 default:
32919 cp_parser_error (parser,
32920 "invalid operator for %<#pragma omp atomic%>");
32921 goto saw_error;
32923 oprec = TOKEN_PRECEDENCE (token);
32924 gcc_assert (oprec != PREC_NOT_OPERATOR);
32925 if (commutative_tree_code (opcode))
32926 oprec = (enum cp_parser_prec) (oprec - 1);
32927 cp_lexer_consume_token (parser->lexer);
32928 rhs = cp_parser_binary_expression (parser, false, false,
32929 oprec, NULL);
32930 if (rhs == error_mark_node)
32931 goto saw_error;
32932 goto stmt_done;
32933 /* FALLTHROUGH */
32934 default:
32935 cp_parser_error (parser,
32936 "invalid operator for %<#pragma omp atomic%>");
32937 goto saw_error;
32939 cp_lexer_consume_token (parser->lexer);
32941 rhs = cp_parser_expression (parser);
32942 if (rhs == error_mark_node)
32943 goto saw_error;
32944 break;
32946 stmt_done:
32947 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
32949 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
32950 goto saw_error;
32951 v = cp_parser_unary_expression (parser);
32952 if (v == error_mark_node)
32953 goto saw_error;
32954 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32955 goto saw_error;
32956 lhs1 = cp_parser_unary_expression (parser);
32957 if (lhs1 == error_mark_node)
32958 goto saw_error;
32960 if (structured_block)
32962 cp_parser_consume_semicolon_at_end_of_statement (parser);
32963 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
32965 done:
32966 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
32967 if (!structured_block)
32968 cp_parser_consume_semicolon_at_end_of_statement (parser);
32969 return;
32971 saw_error:
32972 cp_parser_skip_to_end_of_block_or_statement (parser);
32973 if (structured_block)
32975 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
32976 cp_lexer_consume_token (parser->lexer);
32977 else if (code == OMP_ATOMIC_CAPTURE_NEW)
32979 cp_parser_skip_to_end_of_block_or_statement (parser);
32980 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
32981 cp_lexer_consume_token (parser->lexer);
32987 /* OpenMP 2.5:
32988 # pragma omp barrier new-line */
32990 static void
32991 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
32993 cp_parser_require_pragma_eol (parser, pragma_tok);
32994 finish_omp_barrier ();
32997 /* OpenMP 2.5:
32998 # pragma omp critical [(name)] new-line
32999 structured-block
33001 OpenMP 4.5:
33002 # pragma omp critical [(name) [hint(expression)]] new-line
33003 structured-block */
33005 #define OMP_CRITICAL_CLAUSE_MASK \
33006 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
33008 static tree
33009 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
33011 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
33013 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33015 cp_lexer_consume_token (parser->lexer);
33017 name = cp_parser_identifier (parser);
33019 if (name == error_mark_node
33020 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33021 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33022 /*or_comma=*/false,
33023 /*consume_paren=*/true);
33024 if (name == error_mark_node)
33025 name = NULL;
33027 clauses = cp_parser_omp_all_clauses (parser,
33028 OMP_CRITICAL_CLAUSE_MASK,
33029 "#pragma omp critical", pragma_tok);
33031 else
33032 cp_parser_require_pragma_eol (parser, pragma_tok);
33034 stmt = cp_parser_omp_structured_block (parser);
33035 return c_finish_omp_critical (input_location, stmt, name, clauses);
33038 /* OpenMP 2.5:
33039 # pragma omp flush flush-vars[opt] new-line
33041 flush-vars:
33042 ( variable-list ) */
33044 static void
33045 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
33047 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33048 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
33049 cp_parser_require_pragma_eol (parser, pragma_tok);
33051 finish_omp_flush ();
33054 /* Helper function, to parse omp for increment expression. */
33056 static tree
33057 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
33059 tree cond = cp_parser_binary_expression (parser, false, true,
33060 PREC_NOT_OPERATOR, NULL);
33061 if (cond == error_mark_node
33062 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33064 cp_parser_skip_to_end_of_statement (parser);
33065 return error_mark_node;
33068 switch (TREE_CODE (cond))
33070 case GT_EXPR:
33071 case GE_EXPR:
33072 case LT_EXPR:
33073 case LE_EXPR:
33074 break;
33075 case NE_EXPR:
33076 if (code == CILK_SIMD || code == CILK_FOR)
33077 break;
33078 /* Fall through: OpenMP disallows NE_EXPR. */
33079 default:
33080 return error_mark_node;
33083 /* If decl is an iterator, preserve LHS and RHS of the relational
33084 expr until finish_omp_for. */
33085 if (decl
33086 && (type_dependent_expression_p (decl)
33087 || CLASS_TYPE_P (TREE_TYPE (decl))))
33088 return cond;
33090 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
33091 TREE_CODE (cond),
33092 TREE_OPERAND (cond, 0), ERROR_MARK,
33093 TREE_OPERAND (cond, 1), ERROR_MARK,
33094 /*overload=*/NULL, tf_warning_or_error);
33097 /* Helper function, to parse omp for increment expression. */
33099 static tree
33100 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
33102 cp_token *token = cp_lexer_peek_token (parser->lexer);
33103 enum tree_code op;
33104 tree lhs, rhs;
33105 cp_id_kind idk;
33106 bool decl_first;
33108 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
33110 op = (token->type == CPP_PLUS_PLUS
33111 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
33112 cp_lexer_consume_token (parser->lexer);
33113 lhs = cp_parser_simple_cast_expression (parser);
33114 if (lhs != decl
33115 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
33116 return error_mark_node;
33117 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
33120 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
33121 if (lhs != decl
33122 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
33123 return error_mark_node;
33125 token = cp_lexer_peek_token (parser->lexer);
33126 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
33128 op = (token->type == CPP_PLUS_PLUS
33129 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
33130 cp_lexer_consume_token (parser->lexer);
33131 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
33134 op = cp_parser_assignment_operator_opt (parser);
33135 if (op == ERROR_MARK)
33136 return error_mark_node;
33138 if (op != NOP_EXPR)
33140 rhs = cp_parser_assignment_expression (parser);
33141 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
33142 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
33145 lhs = cp_parser_binary_expression (parser, false, false,
33146 PREC_ADDITIVE_EXPRESSION, NULL);
33147 token = cp_lexer_peek_token (parser->lexer);
33148 decl_first = (lhs == decl
33149 || (processing_template_decl && cp_tree_equal (lhs, decl)));
33150 if (decl_first)
33151 lhs = NULL_TREE;
33152 if (token->type != CPP_PLUS
33153 && token->type != CPP_MINUS)
33154 return error_mark_node;
33158 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
33159 cp_lexer_consume_token (parser->lexer);
33160 rhs = cp_parser_binary_expression (parser, false, false,
33161 PREC_ADDITIVE_EXPRESSION, NULL);
33162 token = cp_lexer_peek_token (parser->lexer);
33163 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
33165 if (lhs == NULL_TREE)
33167 if (op == PLUS_EXPR)
33168 lhs = rhs;
33169 else
33170 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
33171 tf_warning_or_error);
33173 else
33174 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
33175 ERROR_MARK, NULL, tf_warning_or_error);
33178 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
33180 if (!decl_first)
33182 if ((rhs != decl
33183 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
33184 || op == MINUS_EXPR)
33185 return error_mark_node;
33186 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
33188 else
33189 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
33191 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
33194 /* Parse the initialization statement of either an OpenMP for loop or
33195 a Cilk Plus for loop.
33197 Return true if the resulting construct should have an
33198 OMP_CLAUSE_PRIVATE added to it. */
33200 static tree
33201 cp_parser_omp_for_loop_init (cp_parser *parser,
33202 enum tree_code code,
33203 tree &this_pre_body,
33204 vec<tree, va_gc> *for_block,
33205 tree &init,
33206 tree &orig_init,
33207 tree &decl,
33208 tree &real_decl)
33210 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33211 return NULL_TREE;
33213 tree add_private_clause = NULL_TREE;
33215 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
33217 init-expr:
33218 var = lb
33219 integer-type var = lb
33220 random-access-iterator-type var = lb
33221 pointer-type var = lb
33223 cp_decl_specifier_seq type_specifiers;
33225 /* First, try to parse as an initialized declaration. See
33226 cp_parser_condition, from whence the bulk of this is copied. */
33228 cp_parser_parse_tentatively (parser);
33229 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
33230 /*is_trailing_return=*/false,
33231 &type_specifiers);
33232 if (cp_parser_parse_definitely (parser))
33234 /* If parsing a type specifier seq succeeded, then this
33235 MUST be a initialized declaration. */
33236 tree asm_specification, attributes;
33237 cp_declarator *declarator;
33239 declarator = cp_parser_declarator (parser,
33240 CP_PARSER_DECLARATOR_NAMED,
33241 /*ctor_dtor_or_conv_p=*/NULL,
33242 /*parenthesized_p=*/NULL,
33243 /*member_p=*/false,
33244 /*friend_p=*/false);
33245 attributes = cp_parser_attributes_opt (parser);
33246 asm_specification = cp_parser_asm_specification_opt (parser);
33248 if (declarator == cp_error_declarator)
33249 cp_parser_skip_to_end_of_statement (parser);
33251 else
33253 tree pushed_scope, auto_node;
33255 decl = start_decl (declarator, &type_specifiers,
33256 SD_INITIALIZED, attributes,
33257 /*prefix_attributes=*/NULL_TREE,
33258 &pushed_scope);
33260 auto_node = type_uses_auto (TREE_TYPE (decl));
33261 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
33263 if (cp_lexer_next_token_is (parser->lexer,
33264 CPP_OPEN_PAREN))
33266 if (code != CILK_SIMD && code != CILK_FOR)
33267 error ("parenthesized initialization is not allowed in "
33268 "OpenMP %<for%> loop");
33269 else
33270 error ("parenthesized initialization is "
33271 "not allowed in for-loop");
33273 else
33274 /* Trigger an error. */
33275 cp_parser_require (parser, CPP_EQ, RT_EQ);
33277 init = error_mark_node;
33278 cp_parser_skip_to_end_of_statement (parser);
33280 else if (CLASS_TYPE_P (TREE_TYPE (decl))
33281 || type_dependent_expression_p (decl)
33282 || auto_node)
33284 bool is_direct_init, is_non_constant_init;
33286 init = cp_parser_initializer (parser,
33287 &is_direct_init,
33288 &is_non_constant_init);
33290 if (auto_node)
33292 TREE_TYPE (decl)
33293 = do_auto_deduction (TREE_TYPE (decl), init,
33294 auto_node);
33296 if (!CLASS_TYPE_P (TREE_TYPE (decl))
33297 && !type_dependent_expression_p (decl))
33298 goto non_class;
33301 cp_finish_decl (decl, init, !is_non_constant_init,
33302 asm_specification,
33303 LOOKUP_ONLYCONVERTING);
33304 orig_init = init;
33305 if (CLASS_TYPE_P (TREE_TYPE (decl)))
33307 vec_safe_push (for_block, this_pre_body);
33308 init = NULL_TREE;
33310 else
33311 init = pop_stmt_list (this_pre_body);
33312 this_pre_body = NULL_TREE;
33314 else
33316 /* Consume '='. */
33317 cp_lexer_consume_token (parser->lexer);
33318 init = cp_parser_assignment_expression (parser);
33320 non_class:
33321 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
33322 init = error_mark_node;
33323 else
33324 cp_finish_decl (decl, NULL_TREE,
33325 /*init_const_expr_p=*/false,
33326 asm_specification,
33327 LOOKUP_ONLYCONVERTING);
33330 if (pushed_scope)
33331 pop_scope (pushed_scope);
33334 else
33336 cp_id_kind idk;
33337 /* If parsing a type specifier sequence failed, then
33338 this MUST be a simple expression. */
33339 if (code == CILK_FOR)
33340 error ("%<_Cilk_for%> allows expression instead of declaration only "
33341 "in C, not in C++");
33342 cp_parser_parse_tentatively (parser);
33343 decl = cp_parser_primary_expression (parser, false, false,
33344 false, &idk);
33345 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
33346 if (!cp_parser_error_occurred (parser)
33347 && decl
33348 && (TREE_CODE (decl) == COMPONENT_REF
33349 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
33351 cp_parser_abort_tentative_parse (parser);
33352 cp_parser_parse_tentatively (parser);
33353 cp_token *token = cp_lexer_peek_token (parser->lexer);
33354 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
33355 /*check_dependency_p=*/true,
33356 /*template_p=*/NULL,
33357 /*declarator_p=*/false,
33358 /*optional_p=*/false);
33359 if (name != error_mark_node
33360 && last_tok == cp_lexer_peek_token (parser->lexer))
33362 decl = cp_parser_lookup_name_simple (parser, name,
33363 token->location);
33364 if (TREE_CODE (decl) == FIELD_DECL)
33365 add_private_clause = omp_privatize_field (decl, false);
33367 cp_parser_abort_tentative_parse (parser);
33368 cp_parser_parse_tentatively (parser);
33369 decl = cp_parser_primary_expression (parser, false, false,
33370 false, &idk);
33372 if (!cp_parser_error_occurred (parser)
33373 && decl
33374 && DECL_P (decl)
33375 && CLASS_TYPE_P (TREE_TYPE (decl)))
33377 tree rhs;
33379 cp_parser_parse_definitely (parser);
33380 cp_parser_require (parser, CPP_EQ, RT_EQ);
33381 rhs = cp_parser_assignment_expression (parser);
33382 orig_init = rhs;
33383 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
33384 decl, NOP_EXPR,
33385 rhs,
33386 tf_warning_or_error));
33387 if (!add_private_clause)
33388 add_private_clause = decl;
33390 else
33392 decl = NULL;
33393 cp_parser_abort_tentative_parse (parser);
33394 init = cp_parser_expression (parser);
33395 if (init)
33397 if (TREE_CODE (init) == MODIFY_EXPR
33398 || TREE_CODE (init) == MODOP_EXPR)
33399 real_decl = TREE_OPERAND (init, 0);
33403 return add_private_clause;
33406 /* Parse the restricted form of the for statement allowed by OpenMP. */
33408 static tree
33409 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
33410 tree *cclauses)
33412 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
33413 tree real_decl, initv, condv, incrv, declv;
33414 tree this_pre_body, cl, ordered_cl = NULL_TREE;
33415 location_t loc_first;
33416 bool collapse_err = false;
33417 int i, collapse = 1, ordered = 0, count, nbraces = 0;
33418 vec<tree, va_gc> *for_block = make_tree_vector ();
33419 auto_vec<tree, 4> orig_inits;
33421 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
33422 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
33423 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
33424 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
33425 && OMP_CLAUSE_ORDERED_EXPR (cl))
33427 ordered_cl = cl;
33428 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
33431 if (ordered && ordered < collapse)
33433 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
33434 "%<ordered%> clause parameter is less than %<collapse%>");
33435 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
33436 = build_int_cst (NULL_TREE, collapse);
33437 ordered = collapse;
33439 if (ordered)
33441 for (tree *pc = &clauses; *pc; )
33442 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
33444 error_at (OMP_CLAUSE_LOCATION (*pc),
33445 "%<linear%> clause may not be specified together "
33446 "with %<ordered%> clause with a parameter");
33447 *pc = OMP_CLAUSE_CHAIN (*pc);
33449 else
33450 pc = &OMP_CLAUSE_CHAIN (*pc);
33453 gcc_assert (collapse >= 1 && ordered >= 0);
33454 count = ordered ? ordered : collapse;
33456 declv = make_tree_vec (count);
33457 initv = make_tree_vec (count);
33458 condv = make_tree_vec (count);
33459 incrv = make_tree_vec (count);
33461 loc_first = cp_lexer_peek_token (parser->lexer)->location;
33463 for (i = 0; i < count; i++)
33465 int bracecount = 0;
33466 tree add_private_clause = NULL_TREE;
33467 location_t loc;
33469 if (code != CILK_FOR
33470 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
33472 cp_parser_error (parser, "for statement expected");
33473 return NULL;
33475 if (code == CILK_FOR
33476 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
33478 cp_parser_error (parser, "_Cilk_for statement expected");
33479 return NULL;
33481 loc = cp_lexer_consume_token (parser->lexer)->location;
33483 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33484 return NULL;
33486 init = orig_init = decl = real_decl = NULL;
33487 this_pre_body = push_stmt_list ();
33489 add_private_clause
33490 = cp_parser_omp_for_loop_init (parser, code,
33491 this_pre_body, for_block,
33492 init, orig_init, decl, real_decl);
33494 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
33495 if (this_pre_body)
33497 this_pre_body = pop_stmt_list (this_pre_body);
33498 if (pre_body)
33500 tree t = pre_body;
33501 pre_body = push_stmt_list ();
33502 add_stmt (t);
33503 add_stmt (this_pre_body);
33504 pre_body = pop_stmt_list (pre_body);
33506 else
33507 pre_body = this_pre_body;
33510 if (decl)
33511 real_decl = decl;
33512 if (cclauses != NULL
33513 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
33514 && real_decl != NULL_TREE)
33516 tree *c;
33517 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
33518 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
33519 && OMP_CLAUSE_DECL (*c) == real_decl)
33521 error_at (loc, "iteration variable %qD"
33522 " should not be firstprivate", real_decl);
33523 *c = OMP_CLAUSE_CHAIN (*c);
33525 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
33526 && OMP_CLAUSE_DECL (*c) == real_decl)
33528 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
33529 tree l = *c;
33530 *c = OMP_CLAUSE_CHAIN (*c);
33531 if (code == OMP_SIMD)
33533 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33534 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
33536 else
33538 OMP_CLAUSE_CHAIN (l) = clauses;
33539 clauses = l;
33541 add_private_clause = NULL_TREE;
33543 else
33545 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
33546 && OMP_CLAUSE_DECL (*c) == real_decl)
33547 add_private_clause = NULL_TREE;
33548 c = &OMP_CLAUSE_CHAIN (*c);
33552 if (add_private_clause)
33554 tree c;
33555 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
33557 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
33558 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
33559 && OMP_CLAUSE_DECL (c) == decl)
33560 break;
33561 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
33562 && OMP_CLAUSE_DECL (c) == decl)
33563 error_at (loc, "iteration variable %qD "
33564 "should not be firstprivate",
33565 decl);
33566 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
33567 && OMP_CLAUSE_DECL (c) == decl)
33568 error_at (loc, "iteration variable %qD should not be reduction",
33569 decl);
33571 if (c == NULL)
33573 if (code != OMP_SIMD)
33574 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
33575 else if (collapse == 1)
33576 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
33577 else
33578 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
33579 OMP_CLAUSE_DECL (c) = add_private_clause;
33580 c = finish_omp_clauses (c, true);
33581 if (c)
33583 OMP_CLAUSE_CHAIN (c) = clauses;
33584 clauses = c;
33585 /* For linear, signal that we need to fill up
33586 the so far unknown linear step. */
33587 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
33588 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
33593 cond = NULL;
33594 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33595 cond = cp_parser_omp_for_cond (parser, decl, code);
33596 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
33598 incr = NULL;
33599 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
33601 /* If decl is an iterator, preserve the operator on decl
33602 until finish_omp_for. */
33603 if (real_decl
33604 && ((processing_template_decl
33605 && (TREE_TYPE (real_decl) == NULL_TREE
33606 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
33607 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
33608 incr = cp_parser_omp_for_incr (parser, real_decl);
33609 else
33610 incr = cp_parser_expression (parser);
33611 if (!EXPR_HAS_LOCATION (incr))
33612 protected_set_expr_location (incr, input_location);
33615 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33616 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33617 /*or_comma=*/false,
33618 /*consume_paren=*/true);
33620 TREE_VEC_ELT (declv, i) = decl;
33621 TREE_VEC_ELT (initv, i) = init;
33622 TREE_VEC_ELT (condv, i) = cond;
33623 TREE_VEC_ELT (incrv, i) = incr;
33624 if (orig_init)
33626 orig_inits.safe_grow_cleared (i + 1);
33627 orig_inits[i] = orig_init;
33630 if (i == count - 1)
33631 break;
33633 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
33634 in between the collapsed for loops to be still considered perfectly
33635 nested. Hopefully the final version clarifies this.
33636 For now handle (multiple) {'s and empty statements. */
33637 cp_parser_parse_tentatively (parser);
33640 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
33641 break;
33642 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33644 cp_lexer_consume_token (parser->lexer);
33645 bracecount++;
33647 else if (bracecount
33648 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33649 cp_lexer_consume_token (parser->lexer);
33650 else
33652 loc = cp_lexer_peek_token (parser->lexer)->location;
33653 error_at (loc, "not enough collapsed for loops");
33654 collapse_err = true;
33655 cp_parser_abort_tentative_parse (parser);
33656 declv = NULL_TREE;
33657 break;
33660 while (1);
33662 if (declv)
33664 cp_parser_parse_definitely (parser);
33665 nbraces += bracecount;
33669 /* Note that we saved the original contents of this flag when we entered
33670 the structured block, and so we don't need to re-save it here. */
33671 if (code == CILK_SIMD || code == CILK_FOR)
33672 parser->in_statement = IN_CILK_SIMD_FOR;
33673 else
33674 parser->in_statement = IN_OMP_FOR;
33676 /* Note that the grammar doesn't call for a structured block here,
33677 though the loop as a whole is a structured block. */
33678 body = push_stmt_list ();
33679 cp_parser_statement (parser, NULL_TREE, false, NULL);
33680 body = pop_stmt_list (body);
33682 if (declv == NULL_TREE)
33683 ret = NULL_TREE;
33684 else
33685 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
33686 body, pre_body, &orig_inits, clauses);
33688 while (nbraces)
33690 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33692 cp_lexer_consume_token (parser->lexer);
33693 nbraces--;
33695 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33696 cp_lexer_consume_token (parser->lexer);
33697 else
33699 if (!collapse_err)
33701 error_at (cp_lexer_peek_token (parser->lexer)->location,
33702 "collapsed loops not perfectly nested");
33704 collapse_err = true;
33705 cp_parser_statement_seq_opt (parser, NULL);
33706 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
33707 break;
33711 while (!for_block->is_empty ())
33712 add_stmt (pop_stmt_list (for_block->pop ()));
33713 release_tree_vector (for_block);
33715 return ret;
33718 /* Helper function for OpenMP parsing, split clauses and call
33719 finish_omp_clauses on each of the set of clauses afterwards. */
33721 static void
33722 cp_omp_split_clauses (location_t loc, enum tree_code code,
33723 omp_clause_mask mask, tree clauses, tree *cclauses)
33725 int i;
33726 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
33727 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
33728 if (cclauses[i])
33729 cclauses[i] = finish_omp_clauses (cclauses[i], true);
33732 /* OpenMP 4.0:
33733 #pragma omp simd simd-clause[optseq] new-line
33734 for-loop */
33736 #define OMP_SIMD_CLAUSE_MASK \
33737 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
33738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
33739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
33740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
33741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33742 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33746 static tree
33747 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
33748 char *p_name, omp_clause_mask mask, tree *cclauses)
33750 tree clauses, sb, ret;
33751 unsigned int save;
33752 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33754 strcat (p_name, " simd");
33755 mask |= OMP_SIMD_CLAUSE_MASK;
33757 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33758 cclauses == NULL);
33759 if (cclauses)
33761 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
33762 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
33763 tree c = find_omp_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
33764 OMP_CLAUSE_ORDERED);
33765 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
33767 error_at (OMP_CLAUSE_LOCATION (c),
33768 "%<ordered%> clause with parameter may not be specified "
33769 "on %qs construct", p_name);
33770 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
33774 sb = begin_omp_structured_block ();
33775 save = cp_parser_begin_omp_structured_block (parser);
33777 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
33779 cp_parser_end_omp_structured_block (parser, save);
33780 add_stmt (finish_omp_structured_block (sb));
33782 return ret;
33785 /* OpenMP 2.5:
33786 #pragma omp for for-clause[optseq] new-line
33787 for-loop
33789 OpenMP 4.0:
33790 #pragma omp for simd for-simd-clause[optseq] new-line
33791 for-loop */
33793 #define OMP_FOR_CLAUSE_MASK \
33794 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
33798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33799 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
33800 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
33801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
33802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33804 static tree
33805 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
33806 char *p_name, omp_clause_mask mask, tree *cclauses)
33808 tree clauses, sb, ret;
33809 unsigned int save;
33810 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33812 strcat (p_name, " for");
33813 mask |= OMP_FOR_CLAUSE_MASK;
33814 if (cclauses)
33815 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
33816 /* Composite distribute parallel for{, simd} disallows ordered clause. */
33817 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
33818 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
33820 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33822 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33823 const char *p = IDENTIFIER_POINTER (id);
33825 if (strcmp (p, "simd") == 0)
33827 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
33828 if (cclauses == NULL)
33829 cclauses = cclauses_buf;
33831 cp_lexer_consume_token (parser->lexer);
33832 if (!flag_openmp) /* flag_openmp_simd */
33833 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33834 cclauses);
33835 sb = begin_omp_structured_block ();
33836 save = cp_parser_begin_omp_structured_block (parser);
33837 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33838 cclauses);
33839 cp_parser_end_omp_structured_block (parser, save);
33840 tree body = finish_omp_structured_block (sb);
33841 if (ret == NULL)
33842 return ret;
33843 ret = make_node (OMP_FOR);
33844 TREE_TYPE (ret) = void_type_node;
33845 OMP_FOR_BODY (ret) = body;
33846 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33847 SET_EXPR_LOCATION (ret, loc);
33848 add_stmt (ret);
33849 return ret;
33852 if (!flag_openmp) /* flag_openmp_simd */
33854 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33855 return NULL_TREE;
33858 /* Composite distribute parallel for disallows linear clause. */
33859 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
33860 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
33862 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33863 cclauses == NULL);
33864 if (cclauses)
33866 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
33867 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33870 sb = begin_omp_structured_block ();
33871 save = cp_parser_begin_omp_structured_block (parser);
33873 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
33875 cp_parser_end_omp_structured_block (parser, save);
33876 add_stmt (finish_omp_structured_block (sb));
33878 return ret;
33881 /* OpenMP 2.5:
33882 # pragma omp master new-line
33883 structured-block */
33885 static tree
33886 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
33888 cp_parser_require_pragma_eol (parser, pragma_tok);
33889 return c_finish_omp_master (input_location,
33890 cp_parser_omp_structured_block (parser));
33893 /* OpenMP 2.5:
33894 # pragma omp ordered new-line
33895 structured-block
33897 OpenMP 4.5:
33898 # pragma omp ordered ordered-clauses new-line
33899 structured-block */
33901 #define OMP_ORDERED_CLAUSE_MASK \
33902 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
33903 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
33905 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
33906 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
33908 static bool
33909 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
33910 enum pragma_context context)
33912 location_t loc = pragma_tok->location;
33914 if (context != pragma_stmt && context != pragma_compound)
33916 cp_parser_error (parser, "expected declaration specifiers");
33917 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33918 return false;
33921 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33923 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33924 const char *p = IDENTIFIER_POINTER (id);
33926 if (strcmp (p, "depend") == 0)
33928 if (context == pragma_stmt)
33930 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
33931 "%<depend%> clause may only be used in compound "
33932 "statements");
33933 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33934 return false;
33936 tree clauses
33937 = cp_parser_omp_all_clauses (parser,
33938 OMP_ORDERED_DEPEND_CLAUSE_MASK,
33939 "#pragma omp ordered", pragma_tok);
33940 c_finish_omp_ordered (loc, clauses, NULL_TREE);
33941 return false;
33945 tree clauses
33946 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
33947 "#pragma omp ordered", pragma_tok);
33948 c_finish_omp_ordered (loc, clauses,
33949 cp_parser_omp_structured_block (parser));
33950 return true;
33953 /* OpenMP 2.5:
33955 section-scope:
33956 { section-sequence }
33958 section-sequence:
33959 section-directive[opt] structured-block
33960 section-sequence section-directive structured-block */
33962 static tree
33963 cp_parser_omp_sections_scope (cp_parser *parser)
33965 tree stmt, substmt;
33966 bool error_suppress = false;
33967 cp_token *tok;
33969 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
33970 return NULL_TREE;
33972 stmt = push_stmt_list ();
33974 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
33975 != PRAGMA_OMP_SECTION)
33977 substmt = cp_parser_omp_structured_block (parser);
33978 substmt = build1 (OMP_SECTION, void_type_node, substmt);
33979 add_stmt (substmt);
33982 while (1)
33984 tok = cp_lexer_peek_token (parser->lexer);
33985 if (tok->type == CPP_CLOSE_BRACE)
33986 break;
33987 if (tok->type == CPP_EOF)
33988 break;
33990 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
33992 cp_lexer_consume_token (parser->lexer);
33993 cp_parser_require_pragma_eol (parser, tok);
33994 error_suppress = false;
33996 else if (!error_suppress)
33998 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
33999 error_suppress = true;
34002 substmt = cp_parser_omp_structured_block (parser);
34003 substmt = build1 (OMP_SECTION, void_type_node, substmt);
34004 add_stmt (substmt);
34006 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34008 substmt = pop_stmt_list (stmt);
34010 stmt = make_node (OMP_SECTIONS);
34011 TREE_TYPE (stmt) = void_type_node;
34012 OMP_SECTIONS_BODY (stmt) = substmt;
34014 add_stmt (stmt);
34015 return stmt;
34018 /* OpenMP 2.5:
34019 # pragma omp sections sections-clause[optseq] newline
34020 sections-scope */
34022 #define OMP_SECTIONS_CLAUSE_MASK \
34023 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34024 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34025 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34029 static tree
34030 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
34031 char *p_name, omp_clause_mask mask, tree *cclauses)
34033 tree clauses, ret;
34034 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34036 strcat (p_name, " sections");
34037 mask |= OMP_SECTIONS_CLAUSE_MASK;
34038 if (cclauses)
34039 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
34041 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34042 cclauses == NULL);
34043 if (cclauses)
34045 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
34046 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
34049 ret = cp_parser_omp_sections_scope (parser);
34050 if (ret)
34051 OMP_SECTIONS_CLAUSES (ret) = clauses;
34053 return ret;
34056 /* OpenMP 2.5:
34057 # pragma omp parallel parallel-clause[optseq] new-line
34058 structured-block
34059 # pragma omp parallel for parallel-for-clause[optseq] new-line
34060 structured-block
34061 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
34062 structured-block
34064 OpenMP 4.0:
34065 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
34066 structured-block */
34068 #define OMP_PARALLEL_CLAUSE_MASK \
34069 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
34073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34074 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
34075 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34076 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
34077 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
34079 static tree
34080 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
34081 char *p_name, omp_clause_mask mask, tree *cclauses)
34083 tree stmt, clauses, block;
34084 unsigned int save;
34085 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34087 strcat (p_name, " parallel");
34088 mask |= OMP_PARALLEL_CLAUSE_MASK;
34089 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
34090 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
34091 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
34092 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
34094 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34096 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34097 if (cclauses == NULL)
34098 cclauses = cclauses_buf;
34100 cp_lexer_consume_token (parser->lexer);
34101 if (!flag_openmp) /* flag_openmp_simd */
34102 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
34103 block = begin_omp_parallel ();
34104 save = cp_parser_begin_omp_structured_block (parser);
34105 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
34106 cp_parser_end_omp_structured_block (parser, save);
34107 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
34108 block);
34109 if (ret == NULL_TREE)
34110 return ret;
34111 OMP_PARALLEL_COMBINED (stmt) = 1;
34112 return stmt;
34114 /* When combined with distribute, parallel has to be followed by for.
34115 #pragma omp target parallel is allowed though. */
34116 else if (cclauses
34117 && (mask & (OMP_CLAUSE_MASK_1
34118 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34120 error_at (loc, "expected %<for%> after %qs", p_name);
34121 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34122 return NULL_TREE;
34124 else if (!flag_openmp) /* flag_openmp_simd */
34126 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34127 return NULL_TREE;
34129 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34131 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34132 const char *p = IDENTIFIER_POINTER (id);
34133 if (strcmp (p, "sections") == 0)
34135 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34136 cclauses = cclauses_buf;
34138 cp_lexer_consume_token (parser->lexer);
34139 block = begin_omp_parallel ();
34140 save = cp_parser_begin_omp_structured_block (parser);
34141 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
34142 cp_parser_end_omp_structured_block (parser, save);
34143 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
34144 block);
34145 OMP_PARALLEL_COMBINED (stmt) = 1;
34146 return stmt;
34150 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
34151 if (cclauses)
34153 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
34154 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
34157 block = begin_omp_parallel ();
34158 save = cp_parser_begin_omp_structured_block (parser);
34159 cp_parser_statement (parser, NULL_TREE, false, NULL);
34160 cp_parser_end_omp_structured_block (parser, save);
34161 stmt = finish_omp_parallel (clauses, block);
34162 return stmt;
34165 /* OpenMP 2.5:
34166 # pragma omp single single-clause[optseq] new-line
34167 structured-block */
34169 #define OMP_SINGLE_CLAUSE_MASK \
34170 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34171 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34172 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
34173 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34175 static tree
34176 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
34178 tree stmt = make_node (OMP_SINGLE);
34179 TREE_TYPE (stmt) = void_type_node;
34181 OMP_SINGLE_CLAUSES (stmt)
34182 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
34183 "#pragma omp single", pragma_tok);
34184 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
34186 return add_stmt (stmt);
34189 /* OpenMP 3.0:
34190 # pragma omp task task-clause[optseq] new-line
34191 structured-block */
34193 #define OMP_TASK_CLAUSE_MASK \
34194 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34195 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
34196 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
34197 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34198 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34199 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34200 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
34201 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
34202 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34203 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
34205 static tree
34206 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
34208 tree clauses, block;
34209 unsigned int save;
34211 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
34212 "#pragma omp task", pragma_tok);
34213 block = begin_omp_task ();
34214 save = cp_parser_begin_omp_structured_block (parser);
34215 cp_parser_statement (parser, NULL_TREE, false, NULL);
34216 cp_parser_end_omp_structured_block (parser, save);
34217 return finish_omp_task (clauses, block);
34220 /* OpenMP 3.0:
34221 # pragma omp taskwait new-line */
34223 static void
34224 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
34226 cp_parser_require_pragma_eol (parser, pragma_tok);
34227 finish_omp_taskwait ();
34230 /* OpenMP 3.1:
34231 # pragma omp taskyield new-line */
34233 static void
34234 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
34236 cp_parser_require_pragma_eol (parser, pragma_tok);
34237 finish_omp_taskyield ();
34240 /* OpenMP 4.0:
34241 # pragma omp taskgroup new-line
34242 structured-block */
34244 static tree
34245 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
34247 cp_parser_require_pragma_eol (parser, pragma_tok);
34248 return c_finish_omp_taskgroup (input_location,
34249 cp_parser_omp_structured_block (parser));
34253 /* OpenMP 2.5:
34254 # pragma omp threadprivate (variable-list) */
34256 static void
34257 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
34259 tree vars;
34261 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34262 cp_parser_require_pragma_eol (parser, pragma_tok);
34264 finish_omp_threadprivate (vars);
34267 /* OpenMP 4.0:
34268 # pragma omp cancel cancel-clause[optseq] new-line */
34270 #define OMP_CANCEL_CLAUSE_MASK \
34271 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
34272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
34273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
34274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
34275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
34277 static void
34278 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
34280 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
34281 "#pragma omp cancel", pragma_tok);
34282 finish_omp_cancel (clauses);
34285 /* OpenMP 4.0:
34286 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
34288 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
34289 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
34290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
34291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
34292 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
34294 static void
34295 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
34297 tree clauses;
34298 bool point_seen = false;
34300 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34302 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34303 const char *p = IDENTIFIER_POINTER (id);
34305 if (strcmp (p, "point") == 0)
34307 cp_lexer_consume_token (parser->lexer);
34308 point_seen = true;
34311 if (!point_seen)
34313 cp_parser_error (parser, "expected %<point%>");
34314 cp_parser_require_pragma_eol (parser, pragma_tok);
34315 return;
34318 clauses = cp_parser_omp_all_clauses (parser,
34319 OMP_CANCELLATION_POINT_CLAUSE_MASK,
34320 "#pragma omp cancellation point",
34321 pragma_tok);
34322 finish_omp_cancellation_point (clauses);
34325 /* OpenMP 4.0:
34326 #pragma omp distribute distribute-clause[optseq] new-line
34327 for-loop */
34329 #define OMP_DISTRIBUTE_CLAUSE_MASK \
34330 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
34334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34336 static tree
34337 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
34338 char *p_name, omp_clause_mask mask, tree *cclauses)
34340 tree clauses, sb, ret;
34341 unsigned int save;
34342 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34344 strcat (p_name, " distribute");
34345 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
34347 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34349 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34350 const char *p = IDENTIFIER_POINTER (id);
34351 bool simd = false;
34352 bool parallel = false;
34354 if (strcmp (p, "simd") == 0)
34355 simd = true;
34356 else
34357 parallel = strcmp (p, "parallel") == 0;
34358 if (parallel || simd)
34360 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34361 if (cclauses == NULL)
34362 cclauses = cclauses_buf;
34363 cp_lexer_consume_token (parser->lexer);
34364 if (!flag_openmp) /* flag_openmp_simd */
34366 if (simd)
34367 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34368 cclauses);
34369 else
34370 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
34371 cclauses);
34373 sb = begin_omp_structured_block ();
34374 save = cp_parser_begin_omp_structured_block (parser);
34375 if (simd)
34376 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34377 cclauses);
34378 else
34379 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
34380 cclauses);
34381 cp_parser_end_omp_structured_block (parser, save);
34382 tree body = finish_omp_structured_block (sb);
34383 if (ret == NULL)
34384 return ret;
34385 ret = make_node (OMP_DISTRIBUTE);
34386 TREE_TYPE (ret) = void_type_node;
34387 OMP_FOR_BODY (ret) = body;
34388 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
34389 SET_EXPR_LOCATION (ret, loc);
34390 add_stmt (ret);
34391 return ret;
34394 if (!flag_openmp) /* flag_openmp_simd */
34396 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34397 return NULL_TREE;
34400 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34401 cclauses == NULL);
34402 if (cclauses)
34404 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
34405 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
34408 sb = begin_omp_structured_block ();
34409 save = cp_parser_begin_omp_structured_block (parser);
34411 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
34413 cp_parser_end_omp_structured_block (parser, save);
34414 add_stmt (finish_omp_structured_block (sb));
34416 return ret;
34419 /* OpenMP 4.0:
34420 # pragma omp teams teams-clause[optseq] new-line
34421 structured-block */
34423 #define OMP_TEAMS_CLAUSE_MASK \
34424 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34427 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34428 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
34429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
34430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
34432 static tree
34433 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
34434 char *p_name, omp_clause_mask mask, tree *cclauses)
34436 tree clauses, sb, ret;
34437 unsigned int save;
34438 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34440 strcat (p_name, " teams");
34441 mask |= OMP_TEAMS_CLAUSE_MASK;
34443 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34445 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34446 const char *p = IDENTIFIER_POINTER (id);
34447 if (strcmp (p, "distribute") == 0)
34449 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34450 if (cclauses == NULL)
34451 cclauses = cclauses_buf;
34453 cp_lexer_consume_token (parser->lexer);
34454 if (!flag_openmp) /* flag_openmp_simd */
34455 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
34456 cclauses);
34457 sb = begin_omp_structured_block ();
34458 save = cp_parser_begin_omp_structured_block (parser);
34459 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
34460 cclauses);
34461 cp_parser_end_omp_structured_block (parser, save);
34462 tree body = finish_omp_structured_block (sb);
34463 if (ret == NULL)
34464 return ret;
34465 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34466 ret = make_node (OMP_TEAMS);
34467 TREE_TYPE (ret) = void_type_node;
34468 OMP_TEAMS_CLAUSES (ret) = clauses;
34469 OMP_TEAMS_BODY (ret) = body;
34470 OMP_TEAMS_COMBINED (ret) = 1;
34471 return add_stmt (ret);
34474 if (!flag_openmp) /* flag_openmp_simd */
34476 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34477 return NULL_TREE;
34480 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34481 cclauses == NULL);
34482 if (cclauses)
34484 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
34485 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34488 tree stmt = make_node (OMP_TEAMS);
34489 TREE_TYPE (stmt) = void_type_node;
34490 OMP_TEAMS_CLAUSES (stmt) = clauses;
34491 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
34493 return add_stmt (stmt);
34496 /* OpenMP 4.0:
34497 # pragma omp target data target-data-clause[optseq] new-line
34498 structured-block */
34500 #define OMP_TARGET_DATA_CLAUSE_MASK \
34501 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34502 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
34506 static tree
34507 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
34509 tree clauses
34510 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
34511 "#pragma omp target data", pragma_tok);
34512 int map_seen = 0;
34513 for (tree *pc = &clauses; *pc;)
34515 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34516 switch (OMP_CLAUSE_MAP_KIND (*pc))
34518 case GOMP_MAP_TO:
34519 case GOMP_MAP_ALWAYS_TO:
34520 case GOMP_MAP_FROM:
34521 case GOMP_MAP_ALWAYS_FROM:
34522 case GOMP_MAP_TOFROM:
34523 case GOMP_MAP_ALWAYS_TOFROM:
34524 case GOMP_MAP_ALLOC:
34525 map_seen = 3;
34526 break;
34527 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34528 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34529 case GOMP_MAP_ALWAYS_POINTER:
34530 break;
34531 default:
34532 map_seen |= 1;
34533 error_at (OMP_CLAUSE_LOCATION (*pc),
34534 "%<#pragma omp target data%> with map-type other "
34535 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
34536 "on %<map%> clause");
34537 *pc = OMP_CLAUSE_CHAIN (*pc);
34538 continue;
34540 pc = &OMP_CLAUSE_CHAIN (*pc);
34543 if (map_seen != 3)
34545 if (map_seen == 0)
34546 error_at (pragma_tok->location,
34547 "%<#pragma omp target data%> must contain at least "
34548 "one %<map%> clause");
34549 return NULL_TREE;
34552 tree stmt = make_node (OMP_TARGET_DATA);
34553 TREE_TYPE (stmt) = void_type_node;
34554 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
34556 keep_next_level (true);
34557 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
34559 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34560 return add_stmt (stmt);
34563 /* OpenMP 4.5:
34564 # pragma omp target enter data target-enter-data-clause[optseq] new-line
34565 structured-block */
34567 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
34568 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34569 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34570 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34574 static tree
34575 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
34576 enum pragma_context context)
34578 bool data_seen = false;
34579 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34581 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34582 const char *p = IDENTIFIER_POINTER (id);
34584 if (strcmp (p, "data") == 0)
34586 cp_lexer_consume_token (parser->lexer);
34587 data_seen = true;
34590 if (!data_seen)
34592 cp_parser_error (parser, "expected %<data%>");
34593 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34594 return NULL_TREE;
34597 if (context == pragma_stmt)
34599 error_at (pragma_tok->location,
34600 "%<#pragma omp target enter data%> may only be "
34601 "used in compound statements");
34602 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34603 return NULL_TREE;
34606 tree clauses
34607 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
34608 "#pragma omp target enter data", pragma_tok);
34609 int map_seen = 0;
34610 for (tree *pc = &clauses; *pc;)
34612 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34613 switch (OMP_CLAUSE_MAP_KIND (*pc))
34615 case GOMP_MAP_TO:
34616 case GOMP_MAP_ALWAYS_TO:
34617 case GOMP_MAP_ALLOC:
34618 map_seen = 3;
34619 break;
34620 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34621 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34622 case GOMP_MAP_ALWAYS_POINTER:
34623 break;
34624 default:
34625 map_seen |= 1;
34626 error_at (OMP_CLAUSE_LOCATION (*pc),
34627 "%<#pragma omp target enter data%> with map-type other "
34628 "than %<to%> or %<alloc%> on %<map%> clause");
34629 *pc = OMP_CLAUSE_CHAIN (*pc);
34630 continue;
34632 pc = &OMP_CLAUSE_CHAIN (*pc);
34635 if (map_seen != 3)
34637 if (map_seen == 0)
34638 error_at (pragma_tok->location,
34639 "%<#pragma omp target enter data%> must contain at least "
34640 "one %<map%> clause");
34641 return NULL_TREE;
34644 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
34645 TREE_TYPE (stmt) = void_type_node;
34646 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
34647 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34648 return add_stmt (stmt);
34651 /* OpenMP 4.5:
34652 # pragma omp target exit data target-enter-data-clause[optseq] new-line
34653 structured-block */
34655 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
34656 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34657 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34658 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34659 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34662 static tree
34663 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
34664 enum pragma_context context)
34666 bool data_seen = false;
34667 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34669 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34670 const char *p = IDENTIFIER_POINTER (id);
34672 if (strcmp (p, "data") == 0)
34674 cp_lexer_consume_token (parser->lexer);
34675 data_seen = true;
34678 if (!data_seen)
34680 cp_parser_error (parser, "expected %<data%>");
34681 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34682 return NULL_TREE;
34685 if (context == pragma_stmt)
34687 error_at (pragma_tok->location,
34688 "%<#pragma omp target exit data%> may only be "
34689 "used in compound statements");
34690 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34691 return NULL_TREE;
34694 tree clauses
34695 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
34696 "#pragma omp target exit data", pragma_tok);
34697 int map_seen = 0;
34698 for (tree *pc = &clauses; *pc;)
34700 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34701 switch (OMP_CLAUSE_MAP_KIND (*pc))
34703 case GOMP_MAP_FROM:
34704 case GOMP_MAP_ALWAYS_FROM:
34705 case GOMP_MAP_RELEASE:
34706 case GOMP_MAP_DELETE:
34707 map_seen = 3;
34708 break;
34709 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34710 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34711 case GOMP_MAP_ALWAYS_POINTER:
34712 break;
34713 default:
34714 map_seen |= 1;
34715 error_at (OMP_CLAUSE_LOCATION (*pc),
34716 "%<#pragma omp target exit data%> with map-type other "
34717 "than %<from%>, %<release%> or %<delete%> on %<map%>"
34718 " clause");
34719 *pc = OMP_CLAUSE_CHAIN (*pc);
34720 continue;
34722 pc = &OMP_CLAUSE_CHAIN (*pc);
34725 if (map_seen != 3)
34727 if (map_seen == 0)
34728 error_at (pragma_tok->location,
34729 "%<#pragma omp target exit data%> must contain at least "
34730 "one %<map%> clause");
34731 return NULL_TREE;
34734 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
34735 TREE_TYPE (stmt) = void_type_node;
34736 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
34737 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34738 return add_stmt (stmt);
34741 /* OpenMP 4.0:
34742 # pragma omp target update target-update-clause[optseq] new-line */
34744 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
34745 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
34746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
34747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34752 static bool
34753 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
34754 enum pragma_context context)
34756 if (context == pragma_stmt)
34758 error_at (pragma_tok->location,
34759 "%<#pragma omp target update%> may only be "
34760 "used in compound statements");
34761 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34762 return false;
34765 tree clauses
34766 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
34767 "#pragma omp target update", pragma_tok);
34768 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
34769 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
34771 error_at (pragma_tok->location,
34772 "%<#pragma omp target update%> must contain at least one "
34773 "%<from%> or %<to%> clauses");
34774 return false;
34777 tree stmt = make_node (OMP_TARGET_UPDATE);
34778 TREE_TYPE (stmt) = void_type_node;
34779 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
34780 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34781 add_stmt (stmt);
34782 return false;
34785 /* OpenMP 4.0:
34786 # pragma omp target target-clause[optseq] new-line
34787 structured-block */
34789 #define OMP_TARGET_CLAUSE_MASK \
34790 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34791 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34792 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
34795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
34798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
34800 static bool
34801 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
34802 enum pragma_context context)
34804 tree *pc = NULL, stmt;
34806 if (context != pragma_stmt && context != pragma_compound)
34808 cp_parser_error (parser, "expected declaration specifiers");
34809 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34810 return false;
34813 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34815 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34816 const char *p = IDENTIFIER_POINTER (id);
34817 enum tree_code ccode = ERROR_MARK;
34819 if (strcmp (p, "teams") == 0)
34820 ccode = OMP_TEAMS;
34821 else if (strcmp (p, "parallel") == 0)
34822 ccode = OMP_PARALLEL;
34823 else if (strcmp (p, "simd") == 0)
34824 ccode = OMP_SIMD;
34825 if (ccode != ERROR_MARK)
34827 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
34828 char p_name[sizeof ("#pragma omp target teams distribute "
34829 "parallel for simd")];
34831 cp_lexer_consume_token (parser->lexer);
34832 strcpy (p_name, "#pragma omp target");
34833 if (!flag_openmp) /* flag_openmp_simd */
34835 tree stmt;
34836 switch (ccode)
34838 case OMP_TEAMS:
34839 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
34840 OMP_TARGET_CLAUSE_MASK,
34841 cclauses);
34842 break;
34843 case OMP_PARALLEL:
34844 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
34845 OMP_TARGET_CLAUSE_MASK,
34846 cclauses);
34847 break;
34848 case OMP_SIMD:
34849 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
34850 OMP_TARGET_CLAUSE_MASK,
34851 cclauses);
34852 break;
34853 default:
34854 gcc_unreachable ();
34856 return stmt != NULL_TREE;
34858 keep_next_level (true);
34859 tree sb = begin_omp_structured_block (), ret;
34860 unsigned save = cp_parser_begin_omp_structured_block (parser);
34861 switch (ccode)
34863 case OMP_TEAMS:
34864 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
34865 OMP_TARGET_CLAUSE_MASK, cclauses);
34866 break;
34867 case OMP_PARALLEL:
34868 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
34869 OMP_TARGET_CLAUSE_MASK, cclauses);
34870 break;
34871 case OMP_SIMD:
34872 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
34873 OMP_TARGET_CLAUSE_MASK, cclauses);
34874 break;
34875 default:
34876 gcc_unreachable ();
34878 cp_parser_end_omp_structured_block (parser, save);
34879 tree body = finish_omp_structured_block (sb);
34880 if (ret == NULL_TREE)
34881 return false;
34882 if (ccode == OMP_TEAMS && !processing_template_decl)
34884 /* For combined target teams, ensure the num_teams and
34885 thread_limit clause expressions are evaluated on the host,
34886 before entering the target construct. */
34887 tree c;
34888 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34889 c; c = OMP_CLAUSE_CHAIN (c))
34890 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
34891 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
34892 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
34894 tree expr = OMP_CLAUSE_OPERAND (c, 0);
34895 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
34896 if (expr == error_mark_node)
34897 continue;
34898 tree tmp = TARGET_EXPR_SLOT (expr);
34899 add_stmt (expr);
34900 OMP_CLAUSE_OPERAND (c, 0) = expr;
34901 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
34902 OMP_CLAUSE_FIRSTPRIVATE);
34903 OMP_CLAUSE_DECL (tc) = tmp;
34904 OMP_CLAUSE_CHAIN (tc)
34905 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
34906 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
34909 tree stmt = make_node (OMP_TARGET);
34910 TREE_TYPE (stmt) = void_type_node;
34911 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
34912 OMP_TARGET_BODY (stmt) = body;
34913 OMP_TARGET_COMBINED (stmt) = 1;
34914 add_stmt (stmt);
34915 pc = &OMP_TARGET_CLAUSES (stmt);
34916 goto check_clauses;
34918 else if (!flag_openmp) /* flag_openmp_simd */
34920 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34921 return false;
34923 else if (strcmp (p, "data") == 0)
34925 cp_lexer_consume_token (parser->lexer);
34926 cp_parser_omp_target_data (parser, pragma_tok);
34927 return true;
34929 else if (strcmp (p, "enter") == 0)
34931 cp_lexer_consume_token (parser->lexer);
34932 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
34933 return false;
34935 else if (strcmp (p, "exit") == 0)
34937 cp_lexer_consume_token (parser->lexer);
34938 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
34939 return false;
34941 else if (strcmp (p, "update") == 0)
34943 cp_lexer_consume_token (parser->lexer);
34944 return cp_parser_omp_target_update (parser, pragma_tok, context);
34948 stmt = make_node (OMP_TARGET);
34949 TREE_TYPE (stmt) = void_type_node;
34951 OMP_TARGET_CLAUSES (stmt)
34952 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
34953 "#pragma omp target", pragma_tok);
34954 pc = &OMP_TARGET_CLAUSES (stmt);
34955 keep_next_level (true);
34956 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
34958 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34959 add_stmt (stmt);
34961 check_clauses:
34962 while (*pc)
34964 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34965 switch (OMP_CLAUSE_MAP_KIND (*pc))
34967 case GOMP_MAP_TO:
34968 case GOMP_MAP_ALWAYS_TO:
34969 case GOMP_MAP_FROM:
34970 case GOMP_MAP_ALWAYS_FROM:
34971 case GOMP_MAP_TOFROM:
34972 case GOMP_MAP_ALWAYS_TOFROM:
34973 case GOMP_MAP_ALLOC:
34974 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34975 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34976 case GOMP_MAP_ALWAYS_POINTER:
34977 break;
34978 default:
34979 error_at (OMP_CLAUSE_LOCATION (*pc),
34980 "%<#pragma omp target%> with map-type other "
34981 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
34982 "on %<map%> clause");
34983 *pc = OMP_CLAUSE_CHAIN (*pc);
34984 continue;
34986 pc = &OMP_CLAUSE_CHAIN (*pc);
34988 return true;
34991 /* OpenACC 2.0:
34992 # pragma acc cache (variable-list) new-line
34995 static tree
34996 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
34998 tree stmt, clauses;
35000 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
35001 clauses = finish_omp_clauses (clauses, false);
35003 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
35005 stmt = make_node (OACC_CACHE);
35006 TREE_TYPE (stmt) = void_type_node;
35007 OACC_CACHE_CLAUSES (stmt) = clauses;
35008 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35009 add_stmt (stmt);
35011 return stmt;
35014 /* OpenACC 2.0:
35015 # pragma acc data oacc-data-clause[optseq] new-line
35016 structured-block */
35018 #define OACC_DATA_CLAUSE_MASK \
35019 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35024 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35025 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
35031 static tree
35032 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok)
35034 tree stmt, clauses, block;
35035 unsigned int save;
35037 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
35038 "#pragma acc data", pragma_tok);
35040 block = begin_omp_parallel ();
35041 save = cp_parser_begin_omp_structured_block (parser);
35042 cp_parser_statement (parser, NULL_TREE, false, NULL);
35043 cp_parser_end_omp_structured_block (parser, save);
35044 stmt = finish_oacc_data (clauses, block);
35045 return stmt;
35048 /* OpenACC 2.0:
35049 # pragma acc host_data <clauses> new-line
35050 structured-block */
35052 #define OACC_HOST_DATA_CLAUSE_MASK \
35053 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
35055 static tree
35056 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok)
35058 tree stmt, clauses, block;
35059 unsigned int save;
35061 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
35062 "#pragma acc host_data", pragma_tok);
35064 block = begin_omp_parallel ();
35065 save = cp_parser_begin_omp_structured_block (parser);
35066 cp_parser_statement (parser, NULL_TREE, false, NULL);
35067 cp_parser_end_omp_structured_block (parser, save);
35068 stmt = finish_oacc_host_data (clauses, block);
35069 return stmt;
35072 /* OpenACC 2.0:
35073 # pragma acc declare oacc-data-clause[optseq] new-line
35076 #define OACC_DECLARE_CLAUSE_MASK \
35077 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35081 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
35083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
35084 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
35090 static tree
35091 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
35093 tree clauses, stmt;
35094 bool error = false;
35096 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
35097 "#pragma acc declare", pragma_tok, true);
35100 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35102 error_at (pragma_tok->location,
35103 "no valid clauses specified in %<#pragma acc declare%>");
35104 return NULL_TREE;
35107 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
35109 location_t loc = OMP_CLAUSE_LOCATION (t);
35110 tree decl = OMP_CLAUSE_DECL (t);
35111 if (!DECL_P (decl))
35113 error_at (loc, "array section in %<#pragma acc declare%>");
35114 error = true;
35115 continue;
35117 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
35118 switch (OMP_CLAUSE_MAP_KIND (t))
35120 case GOMP_MAP_FORCE_ALLOC:
35121 case GOMP_MAP_FORCE_TO:
35122 case GOMP_MAP_FORCE_DEVICEPTR:
35123 case GOMP_MAP_DEVICE_RESIDENT:
35124 break;
35126 case GOMP_MAP_POINTER:
35127 /* Generated by c_finish_omp_clauses from array sections;
35128 avoid spurious diagnostics. */
35129 break;
35131 case GOMP_MAP_LINK:
35132 if (!global_bindings_p ()
35133 && (TREE_STATIC (decl)
35134 || !DECL_EXTERNAL (decl)))
35136 error_at (loc,
35137 "%qD must be a global variable in"
35138 "%<#pragma acc declare link%>",
35139 decl);
35140 error = true;
35141 continue;
35143 break;
35145 default:
35146 if (global_bindings_p ())
35148 error_at (loc, "invalid OpenACC clause at file scope");
35149 error = true;
35150 continue;
35152 if (DECL_EXTERNAL (decl))
35154 error_at (loc,
35155 "invalid use of %<extern%> variable %qD "
35156 "in %<#pragma acc declare%>", decl);
35157 error = true;
35158 continue;
35160 else if (TREE_PUBLIC (decl))
35162 error_at (loc,
35163 "invalid use of %<global%> variable %qD "
35164 "in %<#pragma acc declare%>", decl);
35165 error = true;
35166 continue;
35168 break;
35171 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
35172 || lookup_attribute ("omp declare target link",
35173 DECL_ATTRIBUTES (decl)))
35175 error_at (loc, "variable %qD used more than once with "
35176 "%<#pragma acc declare%>", decl);
35177 error = true;
35178 continue;
35181 if (!error)
35183 tree id;
35185 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
35186 id = get_identifier ("omp declare target link");
35187 else
35188 id = get_identifier ("omp declare target");
35190 DECL_ATTRIBUTES (decl)
35191 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
35192 if (global_bindings_p ())
35194 symtab_node *node = symtab_node::get (decl);
35195 if (node != NULL)
35197 node->offloadable = 1;
35198 if (ENABLE_OFFLOADING)
35200 g->have_offload = true;
35201 if (is_a <varpool_node *> (node))
35202 vec_safe_push (offload_vars, decl);
35209 if (error || global_bindings_p ())
35210 return NULL_TREE;
35212 stmt = make_node (OACC_DECLARE);
35213 TREE_TYPE (stmt) = void_type_node;
35214 OACC_DECLARE_CLAUSES (stmt) = clauses;
35215 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35217 add_stmt (stmt);
35219 return NULL_TREE;
35222 /* OpenACC 2.0:
35223 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
35227 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
35229 LOC is the location of the #pragma token.
35232 #define OACC_ENTER_DATA_CLAUSE_MASK \
35233 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35234 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35235 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35236 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35237 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35238 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35241 #define OACC_EXIT_DATA_CLAUSE_MASK \
35242 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35243 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35244 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35245 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
35246 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35248 static tree
35249 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
35250 bool enter)
35252 tree stmt, clauses;
35254 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
35255 || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
35257 cp_parser_error (parser, enter
35258 ? "expected %<data%> in %<#pragma acc enter data%>"
35259 : "expected %<data%> in %<#pragma acc exit data%>");
35260 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35261 return NULL_TREE;
35264 const char *p =
35265 IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
35266 if (strcmp (p, "data") != 0)
35268 cp_parser_error (parser, "invalid pragma");
35269 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35270 return NULL_TREE;
35273 cp_lexer_consume_token (parser->lexer);
35275 if (enter)
35276 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
35277 "#pragma acc enter data", pragma_tok);
35278 else
35279 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
35280 "#pragma acc exit data", pragma_tok);
35282 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35284 error_at (pragma_tok->location,
35285 "%<#pragma acc enter data%> has no data movement clause");
35286 return NULL_TREE;
35289 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
35290 TREE_TYPE (stmt) = void_type_node;
35291 OMP_STANDALONE_CLAUSES (stmt) = clauses;
35292 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35293 add_stmt (stmt);
35294 return stmt;
35297 /* OpenACC 2.0:
35298 # pragma acc loop oacc-loop-clause[optseq] new-line
35299 structured-block */
35301 #define OACC_LOOP_CLAUSE_MASK \
35302 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
35303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
35304 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
35305 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
35306 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
35307 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
35308 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
35309 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
35310 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
35311 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
35313 static tree
35314 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
35315 omp_clause_mask mask, tree *cclauses)
35317 strcat (p_name, " loop");
35318 mask |= OACC_LOOP_CLAUSE_MASK;
35320 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
35321 cclauses == NULL);
35322 if (cclauses)
35324 clauses = c_oacc_split_loop_clauses (clauses, cclauses);
35325 if (*cclauses)
35326 finish_omp_clauses (*cclauses, false);
35327 if (clauses)
35328 finish_omp_clauses (clauses, false);
35331 tree block = begin_omp_structured_block ();
35332 int save = cp_parser_begin_omp_structured_block (parser);
35333 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL);
35334 cp_parser_end_omp_structured_block (parser, save);
35335 add_stmt (finish_omp_structured_block (block));
35337 return stmt;
35340 /* OpenACC 2.0:
35341 # pragma acc kernels oacc-kernels-clause[optseq] new-line
35342 structured-block
35346 # pragma acc parallel oacc-parallel-clause[optseq] new-line
35347 structured-block
35350 #define OACC_KERNELS_CLAUSE_MASK \
35351 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35353 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35354 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35355 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
35357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35358 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35359 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35360 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35361 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35362 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35363 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35364 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35366 #define OACC_PARALLEL_CLAUSE_MASK \
35367 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35368 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35369 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35370 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35371 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35372 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
35373 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35374 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
35375 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35376 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
35377 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
35378 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35379 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35382 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35383 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
35384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
35385 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
35386 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35388 static tree
35389 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
35390 char *p_name)
35392 omp_clause_mask mask;
35393 enum tree_code code;
35394 switch (cp_parser_pragma_kind (pragma_tok))
35396 case PRAGMA_OACC_KERNELS:
35397 strcat (p_name, " kernels");
35398 mask = OACC_KERNELS_CLAUSE_MASK;
35399 code = OACC_KERNELS;
35400 break;
35401 case PRAGMA_OACC_PARALLEL:
35402 strcat (p_name, " parallel");
35403 mask = OACC_PARALLEL_CLAUSE_MASK;
35404 code = OACC_PARALLEL;
35405 break;
35406 default:
35407 gcc_unreachable ();
35410 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35412 const char *p
35413 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
35414 if (strcmp (p, "loop") == 0)
35416 cp_lexer_consume_token (parser->lexer);
35417 mask |= OACC_LOOP_CLAUSE_MASK;
35419 tree block = begin_omp_parallel ();
35420 tree clauses;
35421 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses);
35422 return finish_omp_construct (code, block, clauses);
35426 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
35428 tree block = begin_omp_parallel ();
35429 unsigned int save = cp_parser_begin_omp_structured_block (parser);
35430 cp_parser_statement (parser, NULL_TREE, false, NULL);
35431 cp_parser_end_omp_structured_block (parser, save);
35432 return finish_omp_construct (code, block, clauses);
35435 /* OpenACC 2.0:
35436 # pragma acc update oacc-update-clause[optseq] new-line
35439 #define OACC_UPDATE_CLAUSE_MASK \
35440 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
35442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
35443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
35445 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
35447 static tree
35448 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
35450 tree stmt, clauses;
35452 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
35453 "#pragma acc update", pragma_tok);
35455 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35457 error_at (pragma_tok->location,
35458 "%<#pragma acc update%> must contain at least one "
35459 "%<device%> or %<host%> or %<self%> clause");
35460 return NULL_TREE;
35463 stmt = make_node (OACC_UPDATE);
35464 TREE_TYPE (stmt) = void_type_node;
35465 OACC_UPDATE_CLAUSES (stmt) = clauses;
35466 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35467 add_stmt (stmt);
35468 return stmt;
35471 /* OpenACC 2.0:
35472 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
35474 LOC is the location of the #pragma token.
35477 #define OACC_WAIT_CLAUSE_MASK \
35478 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
35480 static tree
35481 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
35483 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
35484 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35486 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
35487 list = cp_parser_oacc_wait_list (parser, loc, list);
35489 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
35490 "#pragma acc wait", pragma_tok);
35492 stmt = c_finish_oacc_wait (loc, list, clauses);
35493 stmt = finish_expr_stmt (stmt);
35495 return stmt;
35498 /* OpenMP 4.0:
35499 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
35501 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
35502 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
35506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
35507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
35509 static void
35510 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
35511 enum pragma_context context)
35513 bool first_p = parser->omp_declare_simd == NULL;
35514 cp_omp_declare_simd_data data;
35515 if (first_p)
35517 data.error_seen = false;
35518 data.fndecl_seen = false;
35519 data.tokens = vNULL;
35520 parser->omp_declare_simd = &data;
35522 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
35523 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
35524 cp_lexer_consume_token (parser->lexer);
35525 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35526 parser->omp_declare_simd->error_seen = true;
35527 cp_parser_require_pragma_eol (parser, pragma_tok);
35528 struct cp_token_cache *cp
35529 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
35530 parser->omp_declare_simd->tokens.safe_push (cp);
35531 if (first_p)
35533 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
35534 cp_parser_pragma (parser, context);
35535 switch (context)
35537 case pragma_external:
35538 cp_parser_declaration (parser);
35539 break;
35540 case pragma_member:
35541 cp_parser_member_declaration (parser);
35542 break;
35543 case pragma_objc_icode:
35544 cp_parser_block_declaration (parser, /*statement_p=*/false);
35545 break;
35546 default:
35547 cp_parser_declaration_statement (parser);
35548 break;
35550 if (parser->omp_declare_simd
35551 && !parser->omp_declare_simd->error_seen
35552 && !parser->omp_declare_simd->fndecl_seen)
35553 error_at (pragma_tok->location,
35554 "%<#pragma omp declare simd%> not immediately followed by "
35555 "function declaration or definition");
35556 data.tokens.release ();
35557 parser->omp_declare_simd = NULL;
35561 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
35562 This function is modelled similar to the late parsing of omp declare
35563 simd. */
35565 static tree
35566 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
35568 struct cp_token_cache *ce;
35569 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
35570 int ii = 0;
35572 if (parser->omp_declare_simd != NULL
35573 || lookup_attribute ("simd", attrs))
35575 error ("%<#pragma omp declare simd%> of %<simd%> attribute cannot be "
35576 "used in the same function marked as a Cilk Plus SIMD-enabled "
35577 " function");
35578 parser->cilk_simd_fn_info->tokens.release ();
35579 XDELETE (parser->cilk_simd_fn_info);
35580 parser->cilk_simd_fn_info = NULL;
35581 return attrs;
35583 if (!info->error_seen && info->fndecl_seen)
35585 error ("vector attribute not immediately followed by a single function"
35586 " declaration or definition");
35587 info->error_seen = true;
35589 if (info->error_seen)
35590 return attrs;
35592 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
35594 tree c, cl;
35596 cp_parser_push_lexer_for_tokens (parser, ce);
35597 parser->lexer->in_pragma = true;
35598 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
35599 "SIMD-enabled functions attribute",
35600 NULL);
35601 cp_parser_pop_lexer (parser);
35602 if (cl)
35603 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
35605 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
35606 TREE_CHAIN (c) = attrs;
35607 attrs = c;
35609 c = build_tree_list (get_identifier ("omp declare simd"), cl);
35610 TREE_CHAIN (c) = attrs;
35611 if (processing_template_decl)
35612 ATTR_IS_DEPENDENT (c) = 1;
35613 attrs = c;
35615 info->fndecl_seen = true;
35616 parser->cilk_simd_fn_info->tokens.release ();
35617 XDELETE (parser->cilk_simd_fn_info);
35618 parser->cilk_simd_fn_info = NULL;
35619 return attrs;
35622 /* Finalize #pragma omp declare simd clauses after direct declarator has
35623 been parsed, and put that into "omp declare simd" attribute. */
35625 static tree
35626 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
35628 struct cp_token_cache *ce;
35629 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
35630 int i;
35632 if (!data->error_seen && data->fndecl_seen)
35634 error ("%<#pragma omp declare simd%> not immediately followed by "
35635 "a single function declaration or definition");
35636 data->error_seen = true;
35637 return attrs;
35639 if (data->error_seen)
35640 return attrs;
35642 FOR_EACH_VEC_ELT (data->tokens, i, ce)
35644 tree c, cl;
35646 cp_parser_push_lexer_for_tokens (parser, ce);
35647 parser->lexer->in_pragma = true;
35648 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
35649 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
35650 cp_lexer_consume_token (parser->lexer);
35651 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
35652 "#pragma omp declare simd", pragma_tok);
35653 cp_parser_pop_lexer (parser);
35654 if (cl)
35655 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
35656 c = build_tree_list (get_identifier ("omp declare simd"), cl);
35657 TREE_CHAIN (c) = attrs;
35658 if (processing_template_decl)
35659 ATTR_IS_DEPENDENT (c) = 1;
35660 attrs = c;
35663 data->fndecl_seen = true;
35664 return attrs;
35668 /* OpenMP 4.0:
35669 # pragma omp declare target new-line
35670 declarations and definitions
35671 # pragma omp end declare target new-line
35673 OpenMP 4.5:
35674 # pragma omp declare target ( extended-list ) new-line
35676 # pragma omp declare target declare-target-clauses[seq] new-line */
35678 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
35679 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
35680 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
35682 static void
35683 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
35685 tree clauses = NULL_TREE;
35686 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35687 clauses
35688 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
35689 "#pragma omp declare target", pragma_tok);
35690 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35692 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
35693 clauses);
35694 clauses = finish_omp_clauses (clauses, true);
35695 cp_parser_require_pragma_eol (parser, pragma_tok);
35697 else
35699 cp_parser_require_pragma_eol (parser, pragma_tok);
35700 scope_chain->omp_declare_target_attribute++;
35701 return;
35703 if (scope_chain->omp_declare_target_attribute)
35704 error_at (pragma_tok->location,
35705 "%<#pragma omp declare target%> with clauses in between "
35706 "%<#pragma omp declare target%> without clauses and "
35707 "%<#pragma omp end declare target%>");
35708 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
35710 tree t = OMP_CLAUSE_DECL (c), id;
35711 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
35712 tree at2 = lookup_attribute ("omp declare target link",
35713 DECL_ATTRIBUTES (t));
35714 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
35716 id = get_identifier ("omp declare target link");
35717 std::swap (at1, at2);
35719 else
35720 id = get_identifier ("omp declare target");
35721 if (at2)
35723 error_at (OMP_CLAUSE_LOCATION (c),
35724 "%qD specified both in declare target %<link%> and %<to%>"
35725 " clauses", t);
35726 continue;
35728 if (!at1)
35730 symtab_node *node = symtab_node::get (t);
35731 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
35732 if (node != NULL)
35734 node->offloadable = 1;
35735 if (ENABLE_OFFLOADING)
35737 g->have_offload = true;
35738 if (is_a <varpool_node *> (node))
35739 vec_safe_push (offload_vars, t);
35746 static void
35747 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
35749 const char *p = "";
35750 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35752 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35753 p = IDENTIFIER_POINTER (id);
35755 if (strcmp (p, "declare") == 0)
35757 cp_lexer_consume_token (parser->lexer);
35758 p = "";
35759 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35761 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35762 p = IDENTIFIER_POINTER (id);
35764 if (strcmp (p, "target") == 0)
35765 cp_lexer_consume_token (parser->lexer);
35766 else
35768 cp_parser_error (parser, "expected %<target%>");
35769 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35770 return;
35773 else
35775 cp_parser_error (parser, "expected %<declare%>");
35776 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35777 return;
35779 cp_parser_require_pragma_eol (parser, pragma_tok);
35780 if (!scope_chain->omp_declare_target_attribute)
35781 error_at (pragma_tok->location,
35782 "%<#pragma omp end declare target%> without corresponding "
35783 "%<#pragma omp declare target%>");
35784 else
35785 scope_chain->omp_declare_target_attribute--;
35788 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
35789 expression and optional initializer clause of
35790 #pragma omp declare reduction. We store the expression(s) as
35791 either 3, 6 or 7 special statements inside of the artificial function's
35792 body. The first two statements are DECL_EXPRs for the artificial
35793 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
35794 expression that uses those variables.
35795 If there was any INITIALIZER clause, this is followed by further statements,
35796 the fourth and fifth statements are DECL_EXPRs for the artificial
35797 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
35798 constructor variant (first token after open paren is not omp_priv),
35799 then the sixth statement is a statement with the function call expression
35800 that uses the OMP_PRIV and optionally OMP_ORIG variable.
35801 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
35802 to initialize the OMP_PRIV artificial variable and there is seventh
35803 statement, a DECL_EXPR of the OMP_PRIV statement again. */
35805 static bool
35806 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
35808 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
35809 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
35810 type = TREE_TYPE (type);
35811 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
35812 DECL_ARTIFICIAL (omp_out) = 1;
35813 pushdecl (omp_out);
35814 add_decl_expr (omp_out);
35815 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
35816 DECL_ARTIFICIAL (omp_in) = 1;
35817 pushdecl (omp_in);
35818 add_decl_expr (omp_in);
35819 tree combiner;
35820 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
35822 keep_next_level (true);
35823 tree block = begin_omp_structured_block ();
35824 combiner = cp_parser_expression (parser);
35825 finish_expr_stmt (combiner);
35826 block = finish_omp_structured_block (block);
35827 add_stmt (block);
35829 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
35830 return false;
35832 const char *p = "";
35833 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35835 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35836 p = IDENTIFIER_POINTER (id);
35839 if (strcmp (p, "initializer") == 0)
35841 cp_lexer_consume_token (parser->lexer);
35842 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35843 return false;
35845 p = "";
35846 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35848 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35849 p = IDENTIFIER_POINTER (id);
35852 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
35853 DECL_ARTIFICIAL (omp_priv) = 1;
35854 pushdecl (omp_priv);
35855 add_decl_expr (omp_priv);
35856 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
35857 DECL_ARTIFICIAL (omp_orig) = 1;
35858 pushdecl (omp_orig);
35859 add_decl_expr (omp_orig);
35861 keep_next_level (true);
35862 block = begin_omp_structured_block ();
35864 bool ctor = false;
35865 if (strcmp (p, "omp_priv") == 0)
35867 bool is_direct_init, is_non_constant_init;
35868 ctor = true;
35869 cp_lexer_consume_token (parser->lexer);
35870 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
35871 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
35872 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
35873 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
35874 == CPP_CLOSE_PAREN
35875 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
35876 == CPP_CLOSE_PAREN))
35878 finish_omp_structured_block (block);
35879 error ("invalid initializer clause");
35880 return false;
35882 initializer = cp_parser_initializer (parser, &is_direct_init,
35883 &is_non_constant_init);
35884 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
35885 NULL_TREE, LOOKUP_ONLYCONVERTING);
35887 else
35889 cp_parser_parse_tentatively (parser);
35890 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
35891 /*check_dependency_p=*/true,
35892 /*template_p=*/NULL,
35893 /*declarator_p=*/false,
35894 /*optional_p=*/false);
35895 vec<tree, va_gc> *args;
35896 if (fn_name == error_mark_node
35897 || cp_parser_error_occurred (parser)
35898 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
35899 || ((args = cp_parser_parenthesized_expression_list
35900 (parser, non_attr, /*cast_p=*/false,
35901 /*allow_expansion_p=*/true,
35902 /*non_constant_p=*/NULL)),
35903 cp_parser_error_occurred (parser)))
35905 finish_omp_structured_block (block);
35906 cp_parser_abort_tentative_parse (parser);
35907 cp_parser_error (parser, "expected id-expression (arguments)");
35908 return false;
35910 unsigned int i;
35911 tree arg;
35912 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
35913 if (arg == omp_priv
35914 || (TREE_CODE (arg) == ADDR_EXPR
35915 && TREE_OPERAND (arg, 0) == omp_priv))
35916 break;
35917 cp_parser_abort_tentative_parse (parser);
35918 if (arg == NULL_TREE)
35919 error ("one of the initializer call arguments should be %<omp_priv%>"
35920 " or %<&omp_priv%>");
35921 initializer = cp_parser_postfix_expression (parser, false, false, false,
35922 false, NULL);
35923 finish_expr_stmt (initializer);
35926 block = finish_omp_structured_block (block);
35927 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
35928 add_stmt (block);
35930 if (ctor)
35931 add_decl_expr (omp_orig);
35933 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
35934 return false;
35937 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
35938 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
35940 return true;
35943 /* OpenMP 4.0
35944 #pragma omp declare reduction (reduction-id : typename-list : expression) \
35945 initializer-clause[opt] new-line
35947 initializer-clause:
35948 initializer (omp_priv initializer)
35949 initializer (function-name (argument-list)) */
35951 static void
35952 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
35953 enum pragma_context)
35955 auto_vec<tree> types;
35956 enum tree_code reduc_code = ERROR_MARK;
35957 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
35958 unsigned int i;
35959 cp_token *first_token;
35960 cp_token_cache *cp;
35961 int errs;
35962 void *p;
35964 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
35965 p = obstack_alloc (&declarator_obstack, 0);
35967 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35968 goto fail;
35970 switch (cp_lexer_peek_token (parser->lexer)->type)
35972 case CPP_PLUS:
35973 reduc_code = PLUS_EXPR;
35974 break;
35975 case CPP_MULT:
35976 reduc_code = MULT_EXPR;
35977 break;
35978 case CPP_MINUS:
35979 reduc_code = MINUS_EXPR;
35980 break;
35981 case CPP_AND:
35982 reduc_code = BIT_AND_EXPR;
35983 break;
35984 case CPP_XOR:
35985 reduc_code = BIT_XOR_EXPR;
35986 break;
35987 case CPP_OR:
35988 reduc_code = BIT_IOR_EXPR;
35989 break;
35990 case CPP_AND_AND:
35991 reduc_code = TRUTH_ANDIF_EXPR;
35992 break;
35993 case CPP_OR_OR:
35994 reduc_code = TRUTH_ORIF_EXPR;
35995 break;
35996 case CPP_NAME:
35997 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
35998 break;
35999 default:
36000 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
36001 "%<|%>, %<&&%>, %<||%> or identifier");
36002 goto fail;
36005 if (reduc_code != ERROR_MARK)
36006 cp_lexer_consume_token (parser->lexer);
36008 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
36009 if (reduc_id == error_mark_node)
36010 goto fail;
36012 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36013 goto fail;
36015 /* Types may not be defined in declare reduction type list. */
36016 const char *saved_message;
36017 saved_message = parser->type_definition_forbidden_message;
36018 parser->type_definition_forbidden_message
36019 = G_("types may not be defined in declare reduction type list");
36020 bool saved_colon_corrects_to_scope_p;
36021 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
36022 parser->colon_corrects_to_scope_p = false;
36023 bool saved_colon_doesnt_start_class_def_p;
36024 saved_colon_doesnt_start_class_def_p
36025 = parser->colon_doesnt_start_class_def_p;
36026 parser->colon_doesnt_start_class_def_p = true;
36028 while (true)
36030 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36031 type = cp_parser_type_id (parser);
36032 if (type == error_mark_node)
36034 else if (ARITHMETIC_TYPE_P (type)
36035 && (orig_reduc_id == NULL_TREE
36036 || (TREE_CODE (type) != COMPLEX_TYPE
36037 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
36038 "min") == 0
36039 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
36040 "max") == 0))))
36041 error_at (loc, "predeclared arithmetic type %qT in "
36042 "%<#pragma omp declare reduction%>", type);
36043 else if (TREE_CODE (type) == FUNCTION_TYPE
36044 || TREE_CODE (type) == METHOD_TYPE
36045 || TREE_CODE (type) == ARRAY_TYPE)
36046 error_at (loc, "function or array type %qT in "
36047 "%<#pragma omp declare reduction%>", type);
36048 else if (TREE_CODE (type) == REFERENCE_TYPE)
36049 error_at (loc, "reference type %qT in "
36050 "%<#pragma omp declare reduction%>", type);
36051 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
36052 error_at (loc, "const, volatile or __restrict qualified type %qT in "
36053 "%<#pragma omp declare reduction%>", type);
36054 else
36055 types.safe_push (type);
36057 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36058 cp_lexer_consume_token (parser->lexer);
36059 else
36060 break;
36063 /* Restore the saved message. */
36064 parser->type_definition_forbidden_message = saved_message;
36065 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36066 parser->colon_doesnt_start_class_def_p
36067 = saved_colon_doesnt_start_class_def_p;
36069 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
36070 || types.is_empty ())
36072 fail:
36073 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36074 goto done;
36077 first_token = cp_lexer_peek_token (parser->lexer);
36078 cp = NULL;
36079 errs = errorcount;
36080 FOR_EACH_VEC_ELT (types, i, type)
36082 tree fntype
36083 = build_function_type_list (void_type_node,
36084 cp_build_reference_type (type, false),
36085 NULL_TREE);
36086 tree this_reduc_id = reduc_id;
36087 if (!dependent_type_p (type))
36088 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
36089 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
36090 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
36091 DECL_ARTIFICIAL (fndecl) = 1;
36092 DECL_EXTERNAL (fndecl) = 1;
36093 DECL_DECLARED_INLINE_P (fndecl) = 1;
36094 DECL_IGNORED_P (fndecl) = 1;
36095 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
36096 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
36097 DECL_ATTRIBUTES (fndecl)
36098 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
36099 DECL_ATTRIBUTES (fndecl));
36100 if (processing_template_decl)
36101 fndecl = push_template_decl (fndecl);
36102 bool block_scope = false;
36103 tree block = NULL_TREE;
36104 if (current_function_decl)
36106 block_scope = true;
36107 DECL_CONTEXT (fndecl) = global_namespace;
36108 if (!processing_template_decl)
36109 pushdecl (fndecl);
36111 else if (current_class_type)
36113 if (cp == NULL)
36115 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36116 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36117 cp_lexer_consume_token (parser->lexer);
36118 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36119 goto fail;
36120 cp = cp_token_cache_new (first_token,
36121 cp_lexer_peek_nth_token (parser->lexer,
36122 2));
36124 DECL_STATIC_FUNCTION_P (fndecl) = 1;
36125 finish_member_declaration (fndecl);
36126 DECL_PENDING_INLINE_INFO (fndecl) = cp;
36127 DECL_PENDING_INLINE_P (fndecl) = 1;
36128 vec_safe_push (unparsed_funs_with_definitions, fndecl);
36129 continue;
36131 else
36133 DECL_CONTEXT (fndecl) = current_namespace;
36134 pushdecl (fndecl);
36136 if (!block_scope)
36137 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
36138 else
36139 block = begin_omp_structured_block ();
36140 if (cp)
36142 cp_parser_push_lexer_for_tokens (parser, cp);
36143 parser->lexer->in_pragma = true;
36145 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
36147 if (!block_scope)
36148 finish_function (0);
36149 else
36150 DECL_CONTEXT (fndecl) = current_function_decl;
36151 if (cp)
36152 cp_parser_pop_lexer (parser);
36153 goto fail;
36155 if (cp)
36156 cp_parser_pop_lexer (parser);
36157 if (!block_scope)
36158 finish_function (0);
36159 else
36161 DECL_CONTEXT (fndecl) = current_function_decl;
36162 block = finish_omp_structured_block (block);
36163 if (TREE_CODE (block) == BIND_EXPR)
36164 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
36165 else if (TREE_CODE (block) == STATEMENT_LIST)
36166 DECL_SAVED_TREE (fndecl) = block;
36167 if (processing_template_decl)
36168 add_decl_expr (fndecl);
36170 cp_check_omp_declare_reduction (fndecl);
36171 if (cp == NULL && types.length () > 1)
36172 cp = cp_token_cache_new (first_token,
36173 cp_lexer_peek_nth_token (parser->lexer, 2));
36174 if (errs != errorcount)
36175 break;
36178 cp_parser_require_pragma_eol (parser, pragma_tok);
36180 done:
36181 /* Free any declarators allocated. */
36182 obstack_free (&declarator_obstack, p);
36185 /* OpenMP 4.0
36186 #pragma omp declare simd declare-simd-clauses[optseq] new-line
36187 #pragma omp declare reduction (reduction-id : typename-list : expression) \
36188 initializer-clause[opt] new-line
36189 #pragma omp declare target new-line */
36191 static void
36192 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
36193 enum pragma_context context)
36195 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36197 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36198 const char *p = IDENTIFIER_POINTER (id);
36200 if (strcmp (p, "simd") == 0)
36202 cp_lexer_consume_token (parser->lexer);
36203 cp_parser_omp_declare_simd (parser, pragma_tok,
36204 context);
36205 return;
36207 cp_ensure_no_omp_declare_simd (parser);
36208 if (strcmp (p, "reduction") == 0)
36210 cp_lexer_consume_token (parser->lexer);
36211 cp_parser_omp_declare_reduction (parser, pragma_tok,
36212 context);
36213 return;
36215 if (!flag_openmp) /* flag_openmp_simd */
36217 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36218 return;
36220 if (strcmp (p, "target") == 0)
36222 cp_lexer_consume_token (parser->lexer);
36223 cp_parser_omp_declare_target (parser, pragma_tok);
36224 return;
36227 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
36228 "or %<target%>");
36229 cp_parser_require_pragma_eol (parser, pragma_tok);
36232 /* OpenMP 4.5:
36233 #pragma omp taskloop taskloop-clause[optseq] new-line
36234 for-loop
36236 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
36237 for-loop */
36239 #define OMP_TASKLOOP_CLAUSE_MASK \
36240 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36241 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36242 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36243 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36244 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
36245 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
36246 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
36247 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
36248 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
36249 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36250 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
36251 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
36252 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
36253 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
36255 static tree
36256 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
36257 char *p_name, omp_clause_mask mask, tree *cclauses)
36259 tree clauses, sb, ret;
36260 unsigned int save;
36261 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36263 strcat (p_name, " taskloop");
36264 mask |= OMP_TASKLOOP_CLAUSE_MASK;
36266 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36268 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36269 const char *p = IDENTIFIER_POINTER (id);
36271 if (strcmp (p, "simd") == 0)
36273 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36274 if (cclauses == NULL)
36275 cclauses = cclauses_buf;
36277 cp_lexer_consume_token (parser->lexer);
36278 if (!flag_openmp) /* flag_openmp_simd */
36279 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36280 cclauses);
36281 sb = begin_omp_structured_block ();
36282 save = cp_parser_begin_omp_structured_block (parser);
36283 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36284 cclauses);
36285 cp_parser_end_omp_structured_block (parser, save);
36286 tree body = finish_omp_structured_block (sb);
36287 if (ret == NULL)
36288 return ret;
36289 ret = make_node (OMP_TASKLOOP);
36290 TREE_TYPE (ret) = void_type_node;
36291 OMP_FOR_BODY (ret) = body;
36292 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
36293 SET_EXPR_LOCATION (ret, loc);
36294 add_stmt (ret);
36295 return ret;
36298 if (!flag_openmp) /* flag_openmp_simd */
36300 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36301 return NULL_TREE;
36304 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36305 cclauses == NULL);
36306 if (cclauses)
36308 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
36309 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
36312 sb = begin_omp_structured_block ();
36313 save = cp_parser_begin_omp_structured_block (parser);
36315 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses);
36317 cp_parser_end_omp_structured_block (parser, save);
36318 add_stmt (finish_omp_structured_block (sb));
36320 return ret;
36324 /* OpenACC 2.0:
36325 # pragma acc routine oacc-routine-clause[optseq] new-line
36326 function-definition
36328 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
36331 #define OACC_ROUTINE_CLAUSE_MASK \
36332 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
36338 /* Parse the OpenACC routine pragma. This has an optional '( name )'
36339 component, which must resolve to a declared namespace-scope
36340 function. The clauses are either processed directly (for a named
36341 function), or defered until the immediatley following declaration
36342 is parsed. */
36344 static void
36345 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
36346 enum pragma_context context)
36348 bool first_p = parser->oacc_routine == NULL;
36349 location_t loc = pragma_tok->location;
36350 cp_omp_declare_simd_data data;
36351 if (first_p)
36353 data.error_seen = false;
36354 data.fndecl_seen = false;
36355 data.tokens = vNULL;
36356 data.clauses = NULL_TREE;
36357 parser->oacc_routine = &data;
36360 tree decl = NULL_TREE;
36361 /* Create a dummy claue, to record location. */
36362 tree c_head = build_omp_clause (pragma_tok->location, OMP_CLAUSE_SEQ);
36364 if (context != pragma_external)
36366 cp_parser_error (parser, "%<#pragma acc routine%> not at file scope");
36367 parser->oacc_routine->error_seen = true;
36368 parser->oacc_routine = NULL;
36369 return;
36372 /* Look for optional '( name )'. */
36373 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36375 if (!first_p)
36377 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36378 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36379 cp_lexer_consume_token (parser->lexer);
36380 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36381 parser->oacc_routine->error_seen = true;
36382 cp_parser_require_pragma_eol (parser, pragma_tok);
36384 error_at (OMP_CLAUSE_LOCATION (parser->oacc_routine->clauses),
36385 "%<#pragma acc routine%> not followed by a "
36386 "function declaration or definition");
36388 parser->oacc_routine->error_seen = true;
36389 return;
36392 cp_lexer_consume_token (parser->lexer);
36393 cp_token *token = cp_lexer_peek_token (parser->lexer);
36395 /* We parse the name as an id-expression. If it resolves to
36396 anything other than a non-overloaded function at namespace
36397 scope, it's an error. */
36398 tree id = cp_parser_id_expression (parser,
36399 /*template_keyword_p=*/false,
36400 /*check_dependency_p=*/false,
36401 /*template_p=*/NULL,
36402 /*declarator_p=*/false,
36403 /*optional_p=*/false);
36404 decl = cp_parser_lookup_name_simple (parser, id, token->location);
36405 if (id != error_mark_node && decl == error_mark_node)
36406 cp_parser_name_lookup_error (parser, id, decl, NLE_NULL,
36407 token->location);
36409 if (decl == error_mark_node
36410 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36412 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36413 parser->oacc_routine = NULL;
36414 return;
36417 /* Build a chain of clauses. */
36418 parser->lexer->in_pragma = true;
36419 tree clauses = NULL_TREE;
36420 clauses = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
36421 "#pragma acc routine",
36422 cp_lexer_peek_token
36423 (parser->lexer));
36425 /* Force clauses to be non-null, by attaching context to it. */
36426 clauses = tree_cons (c_head, clauses, NULL_TREE);
36428 if (decl && is_overloaded_fn (decl)
36429 && (TREE_CODE (decl) != FUNCTION_DECL
36430 || DECL_FUNCTION_TEMPLATE_P (decl)))
36432 error_at (loc, "%<#pragma acc routine%> names a set of overloads");
36433 parser->oacc_routine = NULL;
36434 return;
36437 /* Perhaps we should use the same rule as declarations in different
36438 namespaces? */
36439 if (!DECL_NAMESPACE_SCOPE_P (decl))
36441 error_at (loc, "%<#pragma acc routine%> does not refer to a "
36442 "namespace scope function");
36443 parser->oacc_routine = NULL;
36444 return;
36447 if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
36449 error_at (loc,
36450 "%<#pragma acc routine%> does not refer to a function");
36451 parser->oacc_routine = NULL;
36452 return;
36455 data.clauses = clauses;
36457 cp_finalize_oacc_routine (parser, decl, false);
36458 data.tokens.release ();
36459 parser->oacc_routine = NULL;
36461 else
36463 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36464 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36465 cp_lexer_consume_token (parser->lexer);
36466 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36467 parser->oacc_routine->error_seen = true;
36468 cp_parser_require_pragma_eol (parser, pragma_tok);
36470 struct cp_token_cache *cp
36471 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
36472 parser->oacc_routine->tokens.safe_push (cp);
36474 if (first_p)
36475 parser->oacc_routine->clauses = c_head;
36477 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
36478 cp_parser_pragma (parser, context);
36480 if (first_p)
36482 /* Create an empty list of clauses. */
36483 parser->oacc_routine->clauses = tree_cons (c_head, NULL_TREE,
36484 NULL_TREE);
36485 cp_parser_declaration (parser);
36487 if (parser->oacc_routine
36488 && !parser->oacc_routine->error_seen
36489 && !parser->oacc_routine->fndecl_seen)
36490 error_at (loc, "%<#pragma acc routine%> not followed by a "
36491 "function declaration or definition");
36493 data.tokens.release ();
36494 parser->oacc_routine = NULL;
36499 /* Finalize #pragma acc routine clauses after direct declarator has
36500 been parsed, and put that into "oacc function" attribute. */
36502 static tree
36503 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
36505 struct cp_token_cache *ce;
36506 cp_omp_declare_simd_data *data = parser->oacc_routine;
36507 tree cl, clauses = parser->oacc_routine->clauses;
36508 location_t loc;
36510 loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE(clauses));
36512 if ((!data->error_seen && data->fndecl_seen)
36513 || data->tokens.length () != 1)
36515 error_at (loc, "%<#pragma acc routine%> not followed by a "
36516 "function declaration or definition");
36517 data->error_seen = true;
36518 return attrs;
36520 if (data->error_seen)
36521 return attrs;
36523 ce = data->tokens[0];
36525 cp_parser_push_lexer_for_tokens (parser, ce);
36526 parser->lexer->in_pragma = true;
36527 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
36529 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
36530 cl = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
36531 "#pragma acc routine", pragma_tok);
36532 cp_parser_pop_lexer (parser);
36534 tree c_head = build_omp_clause (loc, OMP_CLAUSE_SEQ);
36536 /* Force clauses to be non-null, by attaching context to it. */
36537 parser->oacc_routine->clauses = tree_cons (c_head, cl, NULL_TREE);
36539 data->fndecl_seen = true;
36540 return attrs;
36543 /* Apply any saved OpenACC routine clauses to a just-parsed
36544 declaration. */
36546 static void
36547 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
36549 if (__builtin_expect (parser->oacc_routine != NULL, 0))
36551 tree clauses = parser->oacc_routine->clauses;
36552 location_t loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE(clauses));
36554 if (parser->oacc_routine->error_seen)
36555 return;
36557 if (fndecl == error_mark_node)
36559 parser->oacc_routine = NULL;
36560 return;
36563 if (TREE_CODE (fndecl) != FUNCTION_DECL)
36565 cp_ensure_no_oacc_routine (parser);
36566 return;
36569 if (!fndecl || TREE_CODE (fndecl) != FUNCTION_DECL)
36571 error_at (loc,
36572 "%<#pragma acc routine%> not followed by a function "
36573 "declaration or definition");
36574 parser->oacc_routine = NULL;
36577 if (get_oacc_fn_attrib (fndecl))
36579 error_at (loc, "%<#pragma acc routine%> already applied to %D",
36580 fndecl);
36581 parser->oacc_routine = NULL;
36584 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
36586 error_at (loc, "%<#pragma acc routine%> must be applied before %s",
36587 TREE_USED (fndecl) ? "use" : "definition");
36588 parser->oacc_routine = NULL;
36591 /* Process for function attrib */
36592 tree dims = build_oacc_routine_dims (TREE_VALUE (clauses));
36593 replace_oacc_fn_attrib (fndecl, dims);
36595 /* Add an "omp target" attribute. */
36596 DECL_ATTRIBUTES (fndecl)
36597 = tree_cons (get_identifier ("omp declare target"),
36598 NULL_TREE, DECL_ATTRIBUTES (fndecl));
36602 /* Main entry point to OpenMP statement pragmas. */
36604 static void
36605 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
36607 tree stmt;
36608 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
36609 omp_clause_mask mask (0);
36611 switch (cp_parser_pragma_kind (pragma_tok))
36613 case PRAGMA_OACC_ATOMIC:
36614 cp_parser_omp_atomic (parser, pragma_tok);
36615 return;
36616 case PRAGMA_OACC_CACHE:
36617 stmt = cp_parser_oacc_cache (parser, pragma_tok);
36618 break;
36619 case PRAGMA_OACC_DATA:
36620 stmt = cp_parser_oacc_data (parser, pragma_tok);
36621 break;
36622 case PRAGMA_OACC_ENTER_DATA:
36623 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
36624 break;
36625 case PRAGMA_OACC_EXIT_DATA:
36626 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
36627 break;
36628 case PRAGMA_OACC_HOST_DATA:
36629 stmt = cp_parser_oacc_host_data (parser, pragma_tok);
36630 break;
36631 case PRAGMA_OACC_KERNELS:
36632 case PRAGMA_OACC_PARALLEL:
36633 strcpy (p_name, "#pragma acc");
36634 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name);
36635 break;
36636 case PRAGMA_OACC_LOOP:
36637 strcpy (p_name, "#pragma acc");
36638 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL);
36639 break;
36640 case PRAGMA_OACC_UPDATE:
36641 stmt = cp_parser_oacc_update (parser, pragma_tok);
36642 break;
36643 case PRAGMA_OACC_WAIT:
36644 stmt = cp_parser_oacc_wait (parser, pragma_tok);
36645 break;
36646 case PRAGMA_OMP_ATOMIC:
36647 cp_parser_omp_atomic (parser, pragma_tok);
36648 return;
36649 case PRAGMA_OMP_CRITICAL:
36650 stmt = cp_parser_omp_critical (parser, pragma_tok);
36651 break;
36652 case PRAGMA_OMP_DISTRIBUTE:
36653 strcpy (p_name, "#pragma omp");
36654 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
36655 break;
36656 case PRAGMA_OMP_FOR:
36657 strcpy (p_name, "#pragma omp");
36658 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
36659 break;
36660 case PRAGMA_OMP_MASTER:
36661 stmt = cp_parser_omp_master (parser, pragma_tok);
36662 break;
36663 case PRAGMA_OMP_PARALLEL:
36664 strcpy (p_name, "#pragma omp");
36665 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
36666 break;
36667 case PRAGMA_OMP_SECTIONS:
36668 strcpy (p_name, "#pragma omp");
36669 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
36670 break;
36671 case PRAGMA_OMP_SIMD:
36672 strcpy (p_name, "#pragma omp");
36673 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
36674 break;
36675 case PRAGMA_OMP_SINGLE:
36676 stmt = cp_parser_omp_single (parser, pragma_tok);
36677 break;
36678 case PRAGMA_OMP_TASK:
36679 stmt = cp_parser_omp_task (parser, pragma_tok);
36680 break;
36681 case PRAGMA_OMP_TASKGROUP:
36682 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
36683 break;
36684 case PRAGMA_OMP_TASKLOOP:
36685 strcpy (p_name, "#pragma omp");
36686 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL);
36687 break;
36688 case PRAGMA_OMP_TEAMS:
36689 strcpy (p_name, "#pragma omp");
36690 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
36691 break;
36692 default:
36693 gcc_unreachable ();
36696 protected_set_expr_location (stmt, pragma_tok->location);
36699 /* Transactional Memory parsing routines. */
36701 /* Parse a transaction attribute.
36703 txn-attribute:
36704 attribute
36705 [ [ identifier ] ]
36707 We use this instead of cp_parser_attributes_opt for transactions to avoid
36708 the pedwarn in C++98 mode. */
36710 static tree
36711 cp_parser_txn_attribute_opt (cp_parser *parser)
36713 cp_token *token;
36714 tree attr_name, attr = NULL;
36716 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
36717 return cp_parser_attributes_opt (parser);
36719 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
36720 return NULL_TREE;
36721 cp_lexer_consume_token (parser->lexer);
36722 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
36723 goto error1;
36725 token = cp_lexer_peek_token (parser->lexer);
36726 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
36728 token = cp_lexer_consume_token (parser->lexer);
36730 attr_name = (token->type == CPP_KEYWORD
36731 /* For keywords, use the canonical spelling,
36732 not the parsed identifier. */
36733 ? ridpointers[(int) token->keyword]
36734 : token->u.value);
36735 attr = build_tree_list (attr_name, NULL_TREE);
36737 else
36738 cp_parser_error (parser, "expected identifier");
36740 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
36741 error1:
36742 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
36743 return attr;
36746 /* Parse a __transaction_atomic or __transaction_relaxed statement.
36748 transaction-statement:
36749 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
36750 compound-statement
36751 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
36754 static tree
36755 cp_parser_transaction (cp_parser *parser, cp_token *token)
36757 unsigned char old_in = parser->in_transaction;
36758 unsigned char this_in = 1, new_in;
36759 enum rid keyword = token->keyword;
36760 tree stmt, attrs, noex;
36762 cp_lexer_consume_token (parser->lexer);
36764 if (keyword == RID_TRANSACTION_RELAXED
36765 || keyword == RID_SYNCHRONIZED)
36766 this_in |= TM_STMT_ATTR_RELAXED;
36767 else
36769 attrs = cp_parser_txn_attribute_opt (parser);
36770 if (attrs)
36771 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
36774 /* Parse a noexcept specification. */
36775 if (keyword == RID_ATOMIC_NOEXCEPT)
36776 noex = boolean_true_node;
36777 else if (keyword == RID_ATOMIC_CANCEL)
36779 /* cancel-and-throw is unimplemented. */
36780 sorry ("atomic_cancel");
36781 noex = NULL_TREE;
36783 else
36784 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
36786 /* Keep track if we're in the lexical scope of an outer transaction. */
36787 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
36789 stmt = begin_transaction_stmt (token->location, NULL, this_in);
36791 parser->in_transaction = new_in;
36792 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
36793 parser->in_transaction = old_in;
36795 finish_transaction_stmt (stmt, NULL, this_in, noex);
36797 return stmt;
36800 /* Parse a __transaction_atomic or __transaction_relaxed expression.
36802 transaction-expression:
36803 __transaction_atomic txn-noexcept-spec[opt] ( expression )
36804 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
36807 static tree
36808 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
36810 unsigned char old_in = parser->in_transaction;
36811 unsigned char this_in = 1;
36812 cp_token *token;
36813 tree expr, noex;
36814 bool noex_expr;
36815 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36817 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
36818 || keyword == RID_TRANSACTION_RELAXED);
36820 if (!flag_tm)
36821 error_at (loc,
36822 keyword == RID_TRANSACTION_RELAXED
36823 ? G_("%<__transaction_relaxed%> without transactional memory "
36824 "support enabled")
36825 : G_("%<__transaction_atomic%> without transactional memory "
36826 "support enabled"));
36828 token = cp_parser_require_keyword (parser, keyword,
36829 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
36830 : RT_TRANSACTION_RELAXED));
36831 gcc_assert (token != NULL);
36833 if (keyword == RID_TRANSACTION_RELAXED)
36834 this_in |= TM_STMT_ATTR_RELAXED;
36836 /* Set this early. This might mean that we allow transaction_cancel in
36837 an expression that we find out later actually has to be a constexpr.
36838 However, we expect that cxx_constant_value will be able to deal with
36839 this; also, if the noexcept has no constexpr, then what we parse next
36840 really is a transaction's body. */
36841 parser->in_transaction = this_in;
36843 /* Parse a noexcept specification. */
36844 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
36845 true);
36847 if (!noex || !noex_expr
36848 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36850 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
36852 expr = cp_parser_expression (parser);
36853 expr = finish_parenthesized_expr (expr);
36855 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
36857 else
36859 /* The only expression that is available got parsed for the noexcept
36860 already. noexcept is true then. */
36861 expr = noex;
36862 noex = boolean_true_node;
36865 expr = build_transaction_expr (token->location, expr, this_in, noex);
36866 parser->in_transaction = old_in;
36868 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
36869 return error_mark_node;
36871 return (flag_tm ? expr : error_mark_node);
36874 /* Parse a function-transaction-block.
36876 function-transaction-block:
36877 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
36878 function-body
36879 __transaction_atomic txn-attribute[opt] function-try-block
36880 __transaction_relaxed ctor-initializer[opt] function-body
36881 __transaction_relaxed function-try-block
36884 static bool
36885 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
36887 unsigned char old_in = parser->in_transaction;
36888 unsigned char new_in = 1;
36889 tree compound_stmt, stmt, attrs;
36890 bool ctor_initializer_p;
36891 cp_token *token;
36893 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
36894 || keyword == RID_TRANSACTION_RELAXED);
36895 token = cp_parser_require_keyword (parser, keyword,
36896 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
36897 : RT_TRANSACTION_RELAXED));
36898 gcc_assert (token != NULL);
36900 if (keyword == RID_TRANSACTION_RELAXED)
36901 new_in |= TM_STMT_ATTR_RELAXED;
36902 else
36904 attrs = cp_parser_txn_attribute_opt (parser);
36905 if (attrs)
36906 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
36909 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
36911 parser->in_transaction = new_in;
36913 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
36914 ctor_initializer_p = cp_parser_function_try_block (parser);
36915 else
36916 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
36917 (parser, /*in_function_try_block=*/false);
36919 parser->in_transaction = old_in;
36921 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
36923 return ctor_initializer_p;
36926 /* Parse a __transaction_cancel statement.
36928 cancel-statement:
36929 __transaction_cancel txn-attribute[opt] ;
36930 __transaction_cancel txn-attribute[opt] throw-expression ;
36932 ??? Cancel and throw is not yet implemented. */
36934 static tree
36935 cp_parser_transaction_cancel (cp_parser *parser)
36937 cp_token *token;
36938 bool is_outer = false;
36939 tree stmt, attrs;
36941 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
36942 RT_TRANSACTION_CANCEL);
36943 gcc_assert (token != NULL);
36945 attrs = cp_parser_txn_attribute_opt (parser);
36946 if (attrs)
36947 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
36949 /* ??? Parse cancel-and-throw here. */
36951 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
36953 if (!flag_tm)
36955 error_at (token->location, "%<__transaction_cancel%> without "
36956 "transactional memory support enabled");
36957 return error_mark_node;
36959 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
36961 error_at (token->location, "%<__transaction_cancel%> within a "
36962 "%<__transaction_relaxed%>");
36963 return error_mark_node;
36965 else if (is_outer)
36967 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
36968 && !is_tm_may_cancel_outer (current_function_decl))
36970 error_at (token->location, "outer %<__transaction_cancel%> not "
36971 "within outer %<__transaction_atomic%>");
36972 error_at (token->location,
36973 " or a %<transaction_may_cancel_outer%> function");
36974 return error_mark_node;
36977 else if (parser->in_transaction == 0)
36979 error_at (token->location, "%<__transaction_cancel%> not within "
36980 "%<__transaction_atomic%>");
36981 return error_mark_node;
36984 stmt = build_tm_abort_call (token->location, is_outer);
36985 add_stmt (stmt);
36987 return stmt;
36990 /* The parser. */
36992 static GTY (()) cp_parser *the_parser;
36995 /* Special handling for the first token or line in the file. The first
36996 thing in the file might be #pragma GCC pch_preprocess, which loads a
36997 PCH file, which is a GC collection point. So we need to handle this
36998 first pragma without benefit of an existing lexer structure.
37000 Always returns one token to the caller in *FIRST_TOKEN. This is
37001 either the true first token of the file, or the first token after
37002 the initial pragma. */
37004 static void
37005 cp_parser_initial_pragma (cp_token *first_token)
37007 tree name = NULL;
37009 cp_lexer_get_preprocessor_token (NULL, first_token);
37010 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
37011 return;
37013 cp_lexer_get_preprocessor_token (NULL, first_token);
37014 if (first_token->type == CPP_STRING)
37016 name = first_token->u.value;
37018 cp_lexer_get_preprocessor_token (NULL, first_token);
37019 if (first_token->type != CPP_PRAGMA_EOL)
37020 error_at (first_token->location,
37021 "junk at end of %<#pragma GCC pch_preprocess%>");
37023 else
37024 error_at (first_token->location, "expected string literal");
37026 /* Skip to the end of the pragma. */
37027 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
37028 cp_lexer_get_preprocessor_token (NULL, first_token);
37030 /* Now actually load the PCH file. */
37031 if (name)
37032 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
37034 /* Read one more token to return to our caller. We have to do this
37035 after reading the PCH file in, since its pointers have to be
37036 live. */
37037 cp_lexer_get_preprocessor_token (NULL, first_token);
37040 /* Parses the grainsize pragma for the _Cilk_for statement.
37041 Syntax:
37042 #pragma cilk grainsize = <VALUE>. */
37044 static void
37045 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok)
37047 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
37049 tree exp = cp_parser_binary_expression (parser, false, false,
37050 PREC_NOT_OPERATOR, NULL);
37051 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37052 if (!exp || exp == error_mark_node)
37054 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
37055 return;
37058 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
37059 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
37060 cp_parser_cilk_for (parser, exp);
37061 else
37062 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
37063 "%<#pragma cilk grainsize%> is not followed by "
37064 "%<_Cilk_for%>");
37065 return;
37067 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37070 /* Normal parsing of a pragma token. Here we can (and must) use the
37071 regular lexer. */
37073 static bool
37074 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
37076 cp_token *pragma_tok;
37077 unsigned int id;
37078 tree stmt;
37079 bool ret;
37081 pragma_tok = cp_lexer_consume_token (parser->lexer);
37082 gcc_assert (pragma_tok->type == CPP_PRAGMA);
37083 parser->lexer->in_pragma = true;
37085 id = cp_parser_pragma_kind (pragma_tok);
37086 if (id != PRAGMA_OMP_DECLARE_REDUCTION && id != PRAGMA_OACC_ROUTINE)
37087 cp_ensure_no_omp_declare_simd (parser);
37088 switch (id)
37090 case PRAGMA_GCC_PCH_PREPROCESS:
37091 error_at (pragma_tok->location,
37092 "%<#pragma GCC pch_preprocess%> must be first");
37093 break;
37095 case PRAGMA_OMP_BARRIER:
37096 switch (context)
37098 case pragma_compound:
37099 cp_parser_omp_barrier (parser, pragma_tok);
37100 return false;
37101 case pragma_stmt:
37102 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
37103 "used in compound statements");
37104 break;
37105 default:
37106 goto bad_stmt;
37108 break;
37110 case PRAGMA_OMP_FLUSH:
37111 switch (context)
37113 case pragma_compound:
37114 cp_parser_omp_flush (parser, pragma_tok);
37115 return false;
37116 case pragma_stmt:
37117 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
37118 "used in compound statements");
37119 break;
37120 default:
37121 goto bad_stmt;
37123 break;
37125 case PRAGMA_OMP_TASKWAIT:
37126 switch (context)
37128 case pragma_compound:
37129 cp_parser_omp_taskwait (parser, pragma_tok);
37130 return false;
37131 case pragma_stmt:
37132 error_at (pragma_tok->location,
37133 "%<#pragma omp taskwait%> may only be "
37134 "used in compound statements");
37135 break;
37136 default:
37137 goto bad_stmt;
37139 break;
37141 case PRAGMA_OMP_TASKYIELD:
37142 switch (context)
37144 case pragma_compound:
37145 cp_parser_omp_taskyield (parser, pragma_tok);
37146 return false;
37147 case pragma_stmt:
37148 error_at (pragma_tok->location,
37149 "%<#pragma omp taskyield%> may only be "
37150 "used in compound statements");
37151 break;
37152 default:
37153 goto bad_stmt;
37155 break;
37157 case PRAGMA_OMP_CANCEL:
37158 switch (context)
37160 case pragma_compound:
37161 cp_parser_omp_cancel (parser, pragma_tok);
37162 return false;
37163 case pragma_stmt:
37164 error_at (pragma_tok->location,
37165 "%<#pragma omp cancel%> may only be "
37166 "used in compound statements");
37167 break;
37168 default:
37169 goto bad_stmt;
37171 break;
37173 case PRAGMA_OMP_CANCELLATION_POINT:
37174 switch (context)
37176 case pragma_compound:
37177 cp_parser_omp_cancellation_point (parser, pragma_tok);
37178 return false;
37179 case pragma_stmt:
37180 error_at (pragma_tok->location,
37181 "%<#pragma omp cancellation point%> may only be "
37182 "used in compound statements");
37183 break;
37184 default:
37185 goto bad_stmt;
37187 break;
37189 case PRAGMA_OMP_THREADPRIVATE:
37190 cp_parser_omp_threadprivate (parser, pragma_tok);
37191 return false;
37193 case PRAGMA_OMP_DECLARE_REDUCTION:
37194 cp_parser_omp_declare (parser, pragma_tok, context);
37195 return false;
37197 case PRAGMA_OACC_DECLARE:
37198 cp_parser_oacc_declare (parser, pragma_tok);
37199 return false;
37201 case PRAGMA_OACC_ROUTINE:
37202 cp_parser_oacc_routine (parser, pragma_tok, context);
37203 return false;
37205 case PRAGMA_OACC_ATOMIC:
37206 case PRAGMA_OACC_CACHE:
37207 case PRAGMA_OACC_DATA:
37208 case PRAGMA_OACC_ENTER_DATA:
37209 case PRAGMA_OACC_EXIT_DATA:
37210 case PRAGMA_OACC_HOST_DATA:
37211 case PRAGMA_OACC_KERNELS:
37212 case PRAGMA_OACC_PARALLEL:
37213 case PRAGMA_OACC_LOOP:
37214 case PRAGMA_OACC_UPDATE:
37215 case PRAGMA_OACC_WAIT:
37216 case PRAGMA_OMP_ATOMIC:
37217 case PRAGMA_OMP_CRITICAL:
37218 case PRAGMA_OMP_DISTRIBUTE:
37219 case PRAGMA_OMP_FOR:
37220 case PRAGMA_OMP_MASTER:
37221 case PRAGMA_OMP_PARALLEL:
37222 case PRAGMA_OMP_SECTIONS:
37223 case PRAGMA_OMP_SIMD:
37224 case PRAGMA_OMP_SINGLE:
37225 case PRAGMA_OMP_TASK:
37226 case PRAGMA_OMP_TASKGROUP:
37227 case PRAGMA_OMP_TASKLOOP:
37228 case PRAGMA_OMP_TEAMS:
37229 if (context != pragma_stmt && context != pragma_compound)
37230 goto bad_stmt;
37231 stmt = push_omp_privatization_clauses (false);
37232 cp_parser_omp_construct (parser, pragma_tok);
37233 pop_omp_privatization_clauses (stmt);
37234 return true;
37236 case PRAGMA_OMP_ORDERED:
37237 stmt = push_omp_privatization_clauses (false);
37238 ret = cp_parser_omp_ordered (parser, pragma_tok, context);
37239 pop_omp_privatization_clauses (stmt);
37240 return ret;
37242 case PRAGMA_OMP_TARGET:
37243 stmt = push_omp_privatization_clauses (false);
37244 ret = cp_parser_omp_target (parser, pragma_tok, context);
37245 pop_omp_privatization_clauses (stmt);
37246 return ret;
37248 case PRAGMA_OMP_END_DECLARE_TARGET:
37249 cp_parser_omp_end_declare_target (parser, pragma_tok);
37250 return false;
37252 case PRAGMA_OMP_SECTION:
37253 error_at (pragma_tok->location,
37254 "%<#pragma omp section%> may only be used in "
37255 "%<#pragma omp sections%> construct");
37256 break;
37258 case PRAGMA_IVDEP:
37260 if (context == pragma_external)
37262 error_at (pragma_tok->location,
37263 "%<#pragma GCC ivdep%> must be inside a function");
37264 break;
37266 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37267 cp_token *tok;
37268 tok = cp_lexer_peek_token (the_parser->lexer);
37269 if (tok->type != CPP_KEYWORD
37270 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
37271 && tok->keyword != RID_DO))
37273 cp_parser_error (parser, "for, while or do statement expected");
37274 return false;
37276 cp_parser_iteration_statement (parser, true);
37277 return true;
37280 case PRAGMA_CILK_SIMD:
37281 if (context == pragma_external)
37283 error_at (pragma_tok->location,
37284 "%<#pragma simd%> must be inside a function");
37285 break;
37287 stmt = push_omp_privatization_clauses (false);
37288 cp_parser_cilk_simd (parser, pragma_tok);
37289 pop_omp_privatization_clauses (stmt);
37290 return true;
37292 case PRAGMA_CILK_GRAINSIZE:
37293 if (context == pragma_external)
37295 error_at (pragma_tok->location,
37296 "%<#pragma cilk grainsize%> must be inside a function");
37297 break;
37300 /* Ignore the pragma if Cilk Plus is not enabled. */
37301 if (flag_cilkplus)
37303 cp_parser_cilk_grainsize (parser, pragma_tok);
37304 return true;
37306 else
37308 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
37309 "%<#pragma cilk grainsize%>");
37310 break;
37313 default:
37314 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
37315 c_invoke_pragma_handler (id);
37316 break;
37318 bad_stmt:
37319 cp_parser_error (parser, "expected declaration specifiers");
37320 break;
37323 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37324 return false;
37327 /* The interface the pragma parsers have to the lexer. */
37329 enum cpp_ttype
37330 pragma_lex (tree *value, location_t *loc)
37332 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
37333 enum cpp_ttype ret = tok->type;
37335 *value = tok->u.value;
37336 if (loc)
37337 *loc = tok->location;
37339 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
37340 ret = CPP_EOF;
37341 else if (ret == CPP_STRING)
37342 *value = cp_parser_string_literal (the_parser, false, false);
37343 else
37345 if (ret == CPP_KEYWORD)
37346 ret = CPP_NAME;
37347 cp_lexer_consume_token (the_parser->lexer);
37350 return ret;
37354 /* External interface. */
37356 /* Parse one entire translation unit. */
37358 void
37359 c_parse_file (void)
37361 static bool already_called = false;
37363 if (already_called)
37364 fatal_error (input_location,
37365 "inter-module optimizations not implemented for C++");
37366 already_called = true;
37368 the_parser = cp_parser_new ();
37369 push_deferring_access_checks (flag_access_control
37370 ? dk_no_deferred : dk_no_check);
37371 cp_parser_translation_unit (the_parser);
37372 the_parser = NULL;
37375 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
37376 vectorlength clause:
37377 Syntax:
37378 vectorlength ( constant-expression ) */
37380 static tree
37381 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
37382 bool is_simd_fn)
37384 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37385 tree expr;
37386 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
37387 safelen clause. Thus, vectorlength is represented as OMP 4.0
37388 safelen. For SIMD-enabled function it is represented by OMP 4.0
37389 simdlen. */
37390 if (!is_simd_fn)
37391 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
37392 loc);
37393 else
37394 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
37395 loc);
37397 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37398 return error_mark_node;
37400 expr = cp_parser_constant_expression (parser);
37401 expr = maybe_constant_value (expr);
37403 /* If expr == error_mark_node, then don't emit any errors nor
37404 create a clause. if any of the above functions returns
37405 error mark node then they would have emitted an error message. */
37406 if (expr == error_mark_node)
37408 else if (!TREE_TYPE (expr)
37409 || !TREE_CONSTANT (expr)
37410 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
37411 error_at (loc, "vectorlength must be an integer constant");
37412 else if (TREE_CONSTANT (expr)
37413 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
37414 error_at (loc, "vectorlength must be a power of 2");
37415 else
37417 tree c;
37418 if (!is_simd_fn)
37420 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
37421 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
37422 OMP_CLAUSE_CHAIN (c) = clauses;
37423 clauses = c;
37425 else
37427 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
37428 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
37429 OMP_CLAUSE_CHAIN (c) = clauses;
37430 clauses = c;
37434 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37435 return error_mark_node;
37436 return clauses;
37439 /* Handles the Cilk Plus #pragma simd linear clause.
37440 Syntax:
37441 linear ( simd-linear-variable-list )
37443 simd-linear-variable-list:
37444 simd-linear-variable
37445 simd-linear-variable-list , simd-linear-variable
37447 simd-linear-variable:
37448 id-expression
37449 id-expression : simd-linear-step
37451 simd-linear-step:
37452 conditional-expression */
37454 static tree
37455 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
37457 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37459 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37460 return clauses;
37461 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37463 cp_parser_error (parser, "expected identifier");
37464 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
37465 return error_mark_node;
37468 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37469 parser->colon_corrects_to_scope_p = false;
37470 while (1)
37472 cp_token *token = cp_lexer_peek_token (parser->lexer);
37473 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37475 cp_parser_error (parser, "expected variable-name");
37476 clauses = error_mark_node;
37477 break;
37480 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
37481 false, false);
37482 tree decl = cp_parser_lookup_name_simple (parser, var_name,
37483 token->location);
37484 if (decl == error_mark_node)
37486 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
37487 token->location);
37488 clauses = error_mark_node;
37490 else
37492 tree e = NULL_TREE;
37493 tree step_size = integer_one_node;
37495 /* If present, parse the linear step. Otherwise, assume the default
37496 value of 1. */
37497 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
37499 cp_lexer_consume_token (parser->lexer);
37501 e = cp_parser_assignment_expression (parser);
37502 e = maybe_constant_value (e);
37504 if (e == error_mark_node)
37506 /* If an error has occurred, then the whole pragma is
37507 considered ill-formed. Thus, no reason to keep
37508 parsing. */
37509 clauses = error_mark_node;
37510 break;
37512 else if (type_dependent_expression_p (e)
37513 || value_dependent_expression_p (e)
37514 || (TREE_TYPE (e)
37515 && INTEGRAL_TYPE_P (TREE_TYPE (e))
37516 && (TREE_CONSTANT (e)
37517 || DECL_P (e))))
37518 step_size = e;
37519 else
37520 cp_parser_error (parser,
37521 "step size must be an integer constant "
37522 "expression or an integer variable");
37525 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
37526 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
37527 OMP_CLAUSE_DECL (l) = decl;
37528 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
37529 OMP_CLAUSE_CHAIN (l) = clauses;
37530 clauses = l;
37532 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37533 cp_lexer_consume_token (parser->lexer);
37534 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37535 break;
37536 else
37538 error_at (cp_lexer_peek_token (parser->lexer)->location,
37539 "expected %<,%> or %<)%> after %qE", decl);
37540 clauses = error_mark_node;
37541 break;
37544 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37545 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
37546 return clauses;
37549 /* Returns the name of the next clause. If the clause is not
37550 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
37551 token is not consumed. Otherwise, the appropriate enum from the
37552 pragma_simd_clause is returned and the token is consumed. */
37554 static pragma_omp_clause
37555 cp_parser_cilk_simd_clause_name (cp_parser *parser)
37557 pragma_omp_clause clause_type;
37558 cp_token *token = cp_lexer_peek_token (parser->lexer);
37560 if (token->keyword == RID_PRIVATE)
37561 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
37562 else if (!token->u.value || token->type != CPP_NAME)
37563 return PRAGMA_CILK_CLAUSE_NONE;
37564 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
37565 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
37566 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
37567 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
37568 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
37569 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
37570 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
37571 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
37572 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
37573 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
37574 else
37575 return PRAGMA_CILK_CLAUSE_NONE;
37577 cp_lexer_consume_token (parser->lexer);
37578 return clause_type;
37581 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
37583 static tree
37584 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
37586 tree clauses = NULL_TREE;
37588 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37589 && clauses != error_mark_node)
37591 pragma_omp_clause c_kind;
37592 c_kind = cp_parser_cilk_simd_clause_name (parser);
37593 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
37594 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
37595 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
37596 clauses = cp_parser_cilk_simd_linear (parser, clauses);
37597 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
37598 /* Use the OpenMP 4.0 equivalent function. */
37599 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
37600 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
37601 /* Use the OpenMP 4.0 equivalent function. */
37602 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
37603 clauses);
37604 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
37605 /* Use the OMP 4.0 equivalent function. */
37606 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
37607 clauses);
37608 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
37609 /* Use the OMP 4.0 equivalent function. */
37610 clauses = cp_parser_omp_clause_reduction (parser, clauses);
37611 else
37613 clauses = error_mark_node;
37614 cp_parser_error (parser, "expected %<#pragma simd%> clause");
37615 break;
37619 cp_parser_skip_to_pragma_eol (parser, pragma_token);
37621 if (clauses == error_mark_node)
37622 return error_mark_node;
37623 else
37624 return c_finish_cilk_clauses (clauses);
37627 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
37629 static void
37630 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
37632 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
37634 if (clauses == error_mark_node)
37635 return;
37637 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
37639 error_at (cp_lexer_peek_token (parser->lexer)->location,
37640 "for statement expected");
37641 return;
37644 tree sb = begin_omp_structured_block ();
37645 int save = cp_parser_begin_omp_structured_block (parser);
37646 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
37647 if (ret)
37648 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
37649 cp_parser_end_omp_structured_block (parser, save);
37650 add_stmt (finish_omp_structured_block (sb));
37653 /* Main entry-point for parsing Cilk Plus _Cilk_for
37654 loops. The return value is error_mark_node
37655 when errors happen and CILK_FOR tree on success. */
37657 static tree
37658 cp_parser_cilk_for (cp_parser *parser, tree grain)
37660 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
37661 gcc_unreachable ();
37663 tree sb = begin_omp_structured_block ();
37664 int save = cp_parser_begin_omp_structured_block (parser);
37666 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
37667 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
37668 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
37669 clauses = finish_omp_clauses (clauses, false);
37671 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL);
37672 if (ret)
37673 cpp_validate_cilk_plus_loop (ret);
37674 else
37675 ret = error_mark_node;
37677 cp_parser_end_omp_structured_block (parser, save);
37678 add_stmt (finish_omp_structured_block (sb));
37679 return ret;
37682 /* Create an identifier for a generic parameter type (a synthesized
37683 template parameter implied by `auto' or a concept identifier). */
37685 static GTY(()) int generic_parm_count;
37686 static tree
37687 make_generic_type_name ()
37689 char buf[32];
37690 sprintf (buf, "auto:%d", ++generic_parm_count);
37691 return get_identifier (buf);
37694 /* Predicate that behaves as is_auto_or_concept but matches the parent
37695 node of the generic type rather than the generic type itself. This
37696 allows for type transformation in add_implicit_template_parms. */
37698 static inline bool
37699 tree_type_is_auto_or_concept (const_tree t)
37701 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
37704 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
37705 (creating a new template parameter list if necessary). Returns the newly
37706 created template type parm. */
37708 static tree
37709 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
37711 gcc_assert (current_binding_level->kind == sk_function_parms);
37713 /* Before committing to modifying any scope, if we're in an
37714 implicit template scope, and we're trying to synthesize a
37715 constrained parameter, try to find a previous parameter with
37716 the same name. This is the same-type rule for abbreviated
37717 function templates. */
37718 if (parser->implicit_template_scope && constr)
37720 tree t = parser->implicit_template_parms;
37721 while (t)
37723 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
37725 tree d = TREE_VALUE (t);
37726 if (TREE_CODE (d) == PARM_DECL)
37727 /* Return the TEMPLATE_PARM_INDEX. */
37728 d = DECL_INITIAL (d);
37729 return d;
37731 t = TREE_CHAIN (t);
37735 /* We are either continuing a function template that already contains implicit
37736 template parameters, creating a new fully-implicit function template, or
37737 extending an existing explicit function template with implicit template
37738 parameters. */
37740 cp_binding_level *const entry_scope = current_binding_level;
37742 bool become_template = false;
37743 cp_binding_level *parent_scope = 0;
37745 if (parser->implicit_template_scope)
37747 gcc_assert (parser->implicit_template_parms);
37749 current_binding_level = parser->implicit_template_scope;
37751 else
37753 /* Roll back to the existing template parameter scope (in the case of
37754 extending an explicit function template) or introduce a new template
37755 parameter scope ahead of the function parameter scope (or class scope
37756 in the case of out-of-line member definitions). The function scope is
37757 added back after template parameter synthesis below. */
37759 cp_binding_level *scope = entry_scope;
37761 while (scope->kind == sk_function_parms)
37763 parent_scope = scope;
37764 scope = scope->level_chain;
37766 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
37768 /* If not defining a class, then any class scope is a scope level in
37769 an out-of-line member definition. In this case simply wind back
37770 beyond the first such scope to inject the template parameter list.
37771 Otherwise wind back to the class being defined. The latter can
37772 occur in class member friend declarations such as:
37774 class A {
37775 void foo (auto);
37777 class B {
37778 friend void A::foo (auto);
37781 The template parameter list synthesized for the friend declaration
37782 must be injected in the scope of 'B'. This can also occur in
37783 erroneous cases such as:
37785 struct A {
37786 struct B {
37787 void foo (auto);
37789 void B::foo (auto) {}
37792 Here the attempted definition of 'B::foo' within 'A' is ill-formed
37793 but, nevertheless, the template parameter list synthesized for the
37794 declarator should be injected into the scope of 'A' as if the
37795 ill-formed template was specified explicitly. */
37797 while (scope->kind == sk_class && !scope->defining_class_p)
37799 parent_scope = scope;
37800 scope = scope->level_chain;
37804 current_binding_level = scope;
37806 if (scope->kind != sk_template_parms
37807 || !function_being_declared_is_template_p (parser))
37809 /* Introduce a new template parameter list for implicit template
37810 parameters. */
37812 become_template = true;
37814 parser->implicit_template_scope
37815 = begin_scope (sk_template_parms, NULL);
37817 ++processing_template_decl;
37819 parser->fully_implicit_function_template_p = true;
37820 ++parser->num_template_parameter_lists;
37822 else
37824 /* Synthesize implicit template parameters at the end of the explicit
37825 template parameter list. */
37827 gcc_assert (current_template_parms);
37829 parser->implicit_template_scope = scope;
37831 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
37832 parser->implicit_template_parms
37833 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
37837 /* Synthesize a new template parameter and track the current template
37838 parameter chain with implicit_template_parms. */
37840 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
37841 tree synth_id = make_generic_type_name ();
37842 tree synth_tmpl_parm;
37843 bool non_type = false;
37845 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
37846 synth_tmpl_parm
37847 = finish_template_type_parm (class_type_node, synth_id);
37848 else if (TREE_CODE (proto) == TEMPLATE_DECL)
37849 synth_tmpl_parm
37850 = finish_constrained_template_template_parm (proto, synth_id);
37851 else
37853 synth_tmpl_parm = copy_decl (proto);
37854 DECL_NAME (synth_tmpl_parm) = synth_id;
37855 non_type = true;
37858 // Attach the constraint to the parm before processing.
37859 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
37860 TREE_TYPE (node) = constr;
37861 tree new_parm
37862 = process_template_parm (parser->implicit_template_parms,
37863 input_location,
37864 node,
37865 /*non_type=*/non_type,
37866 /*param_pack=*/false);
37868 // Chain the new parameter to the list of implicit parameters.
37869 if (parser->implicit_template_parms)
37870 parser->implicit_template_parms
37871 = TREE_CHAIN (parser->implicit_template_parms);
37872 else
37873 parser->implicit_template_parms = new_parm;
37875 tree new_decl = getdecls ();
37876 if (non_type)
37877 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
37878 new_decl = DECL_INITIAL (new_decl);
37880 /* If creating a fully implicit function template, start the new implicit
37881 template parameter list with this synthesized type, otherwise grow the
37882 current template parameter list. */
37884 if (become_template)
37886 parent_scope->level_chain = current_binding_level;
37888 tree new_parms = make_tree_vec (1);
37889 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
37890 current_template_parms = tree_cons (size_int (processing_template_decl),
37891 new_parms, current_template_parms);
37893 else
37895 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
37896 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
37897 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
37898 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
37901 // If the new parameter was constrained, we need to add that to the
37902 // constraints in the template parameter list.
37903 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
37905 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
37906 reqs = conjoin_constraints (reqs, req);
37907 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
37910 current_binding_level = entry_scope;
37912 return new_decl;
37915 /* Finish the declaration of a fully implicit function template. Such a
37916 template has no explicit template parameter list so has not been through the
37917 normal template head and tail processing. synthesize_implicit_template_parm
37918 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
37919 provided if the declaration is a class member such that its template
37920 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
37921 form is returned. Otherwise NULL_TREE is returned. */
37923 static tree
37924 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
37926 gcc_assert (parser->fully_implicit_function_template_p);
37928 if (member_decl_opt && member_decl_opt != error_mark_node
37929 && DECL_VIRTUAL_P (member_decl_opt))
37931 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
37932 "implicit templates may not be %<virtual%>");
37933 DECL_VIRTUAL_P (member_decl_opt) = false;
37936 if (member_decl_opt)
37937 member_decl_opt = finish_member_template_decl (member_decl_opt);
37938 end_template_decl ();
37940 parser->fully_implicit_function_template_p = false;
37941 --parser->num_template_parameter_lists;
37943 return member_decl_opt;
37946 #include "gt-cp-parser.h"