c-parser.c (c_parser_oacc_loop): Update cclauses and clauses when calling c_finish_om...
[official-gcc.git] / gcc / cp / parser.c
blob6ae45b0ca1add9b05c92c00e945e2116d6f2688f
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 (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3176 inform (location, "%<concept%> only available with -fconcepts");
3177 else if (processing_template_decl && current_class_type
3178 && TYPE_BINFO (current_class_type))
3180 tree b;
3182 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3184 b = TREE_CHAIN (b))
3186 tree base_type = BINFO_TYPE (b);
3187 if (CLASS_TYPE_P (base_type)
3188 && dependent_type_p (base_type))
3190 tree field;
3191 /* Go from a particular instantiation of the
3192 template (which will have an empty TYPE_FIELDs),
3193 to the main version. */
3194 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3195 for (field = TYPE_FIELDS (base_type);
3196 field;
3197 field = DECL_CHAIN (field))
3198 if (TREE_CODE (field) == TYPE_DECL
3199 && DECL_NAME (field) == id)
3201 inform (location,
3202 "(perhaps %<typename %T::%E%> was intended)",
3203 BINFO_TYPE (b), id);
3204 break;
3206 if (field)
3207 break;
3212 /* Here we diagnose qualified-ids where the scope is actually correct,
3213 but the identifier does not resolve to a valid type name. */
3214 else if (parser->scope != error_mark_node)
3216 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3218 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3219 error_at (location_of (id),
3220 "%qE in namespace %qE does not name a template type",
3221 id, parser->scope);
3222 else
3223 error_at (location_of (id),
3224 "%qE in namespace %qE does not name a type",
3225 id, parser->scope);
3226 if (DECL_P (decl))
3227 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3229 else if (CLASS_TYPE_P (parser->scope)
3230 && constructor_name_p (id, parser->scope))
3232 /* A<T>::A<T>() */
3233 error_at (location, "%<%T::%E%> names the constructor, not"
3234 " the type", parser->scope, id);
3235 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3236 error_at (location, "and %qT has no template constructors",
3237 parser->scope);
3239 else if (TYPE_P (parser->scope)
3240 && dependent_scope_p (parser->scope))
3241 error_at (location, "need %<typename%> before %<%T::%E%> because "
3242 "%qT is a dependent scope",
3243 parser->scope, id, parser->scope);
3244 else if (TYPE_P (parser->scope))
3246 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3247 error_at (location_of (id),
3248 "%qE in %q#T does not name a template type",
3249 id, parser->scope);
3250 else
3251 error_at (location_of (id),
3252 "%qE in %q#T does not name a type",
3253 id, parser->scope);
3254 if (DECL_P (decl))
3255 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3257 else
3258 gcc_unreachable ();
3262 /* Check for a common situation where a type-name should be present,
3263 but is not, and issue a sensible error message. Returns true if an
3264 invalid type-name was detected.
3266 The situation handled by this function are variable declarations of the
3267 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3268 Usually, `ID' should name a type, but if we got here it means that it
3269 does not. We try to emit the best possible error message depending on
3270 how exactly the id-expression looks like. */
3272 static bool
3273 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3275 tree id;
3276 cp_token *token = cp_lexer_peek_token (parser->lexer);
3278 /* Avoid duplicate error about ambiguous lookup. */
3279 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3281 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3282 if (next->type == CPP_NAME && next->error_reported)
3283 goto out;
3286 cp_parser_parse_tentatively (parser);
3287 id = cp_parser_id_expression (parser,
3288 /*template_keyword_p=*/false,
3289 /*check_dependency_p=*/true,
3290 /*template_p=*/NULL,
3291 /*declarator_p=*/true,
3292 /*optional_p=*/false);
3293 /* If the next token is a (, this is a function with no explicit return
3294 type, i.e. constructor, destructor or conversion op. */
3295 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3296 || TREE_CODE (id) == TYPE_DECL)
3298 cp_parser_abort_tentative_parse (parser);
3299 return false;
3301 if (!cp_parser_parse_definitely (parser))
3302 return false;
3304 /* Emit a diagnostic for the invalid type. */
3305 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3306 out:
3307 /* If we aren't in the middle of a declarator (i.e. in a
3308 parameter-declaration-clause), skip to the end of the declaration;
3309 there's no point in trying to process it. */
3310 if (!parser->in_declarator_p)
3311 cp_parser_skip_to_end_of_block_or_statement (parser);
3312 return true;
3315 /* Consume tokens up to, and including, the next non-nested closing `)'.
3316 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3317 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3318 found an unnested token of that type. */
3320 static int
3321 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3322 bool recovering,
3323 cpp_ttype or_ttype,
3324 bool consume_paren)
3326 unsigned paren_depth = 0;
3327 unsigned brace_depth = 0;
3328 unsigned square_depth = 0;
3330 if (recovering && or_ttype == CPP_EOF
3331 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3332 return 0;
3334 while (true)
3336 cp_token * token = cp_lexer_peek_token (parser->lexer);
3338 /* Have we found what we're looking for before the closing paren? */
3339 if (token->type == or_ttype && or_ttype != CPP_EOF
3340 && !brace_depth && !paren_depth && !square_depth)
3341 return -1;
3343 switch (token->type)
3345 case CPP_EOF:
3346 case CPP_PRAGMA_EOL:
3347 /* If we've run out of tokens, then there is no closing `)'. */
3348 return 0;
3350 /* This is good for lambda expression capture-lists. */
3351 case CPP_OPEN_SQUARE:
3352 ++square_depth;
3353 break;
3354 case CPP_CLOSE_SQUARE:
3355 if (!square_depth--)
3356 return 0;
3357 break;
3359 case CPP_SEMICOLON:
3360 /* This matches the processing in skip_to_end_of_statement. */
3361 if (!brace_depth)
3362 return 0;
3363 break;
3365 case CPP_OPEN_BRACE:
3366 ++brace_depth;
3367 break;
3368 case CPP_CLOSE_BRACE:
3369 if (!brace_depth--)
3370 return 0;
3371 break;
3373 case CPP_OPEN_PAREN:
3374 if (!brace_depth)
3375 ++paren_depth;
3376 break;
3378 case CPP_CLOSE_PAREN:
3379 if (!brace_depth && !paren_depth--)
3381 if (consume_paren)
3382 cp_lexer_consume_token (parser->lexer);
3383 return 1;
3385 break;
3387 default:
3388 break;
3391 /* Consume the token. */
3392 cp_lexer_consume_token (parser->lexer);
3396 /* Consume tokens up to, and including, the next non-nested closing `)'.
3397 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3398 are doing error recovery. Returns -1 if OR_COMMA is true and we
3399 found an unnested token of that type. */
3401 static int
3402 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3403 bool recovering,
3404 bool or_comma,
3405 bool consume_paren)
3407 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3408 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3409 ttype, consume_paren);
3412 /* Consume tokens until we reach the end of the current statement.
3413 Normally, that will be just before consuming a `;'. However, if a
3414 non-nested `}' comes first, then we stop before consuming that. */
3416 static void
3417 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3419 unsigned nesting_depth = 0;
3421 /* Unwind generic function template scope if necessary. */
3422 if (parser->fully_implicit_function_template_p)
3423 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3425 while (true)
3427 cp_token *token = cp_lexer_peek_token (parser->lexer);
3429 switch (token->type)
3431 case CPP_EOF:
3432 case CPP_PRAGMA_EOL:
3433 /* If we've run out of tokens, stop. */
3434 return;
3436 case CPP_SEMICOLON:
3437 /* If the next token is a `;', we have reached the end of the
3438 statement. */
3439 if (!nesting_depth)
3440 return;
3441 break;
3443 case CPP_CLOSE_BRACE:
3444 /* If this is a non-nested '}', stop before consuming it.
3445 That way, when confronted with something like:
3447 { 3 + }
3449 we stop before consuming the closing '}', even though we
3450 have not yet reached a `;'. */
3451 if (nesting_depth == 0)
3452 return;
3454 /* If it is the closing '}' for a block that we have
3455 scanned, stop -- but only after consuming the token.
3456 That way given:
3458 void f g () { ... }
3459 typedef int I;
3461 we will stop after the body of the erroneously declared
3462 function, but before consuming the following `typedef'
3463 declaration. */
3464 if (--nesting_depth == 0)
3466 cp_lexer_consume_token (parser->lexer);
3467 return;
3470 case CPP_OPEN_BRACE:
3471 ++nesting_depth;
3472 break;
3474 default:
3475 break;
3478 /* Consume the token. */
3479 cp_lexer_consume_token (parser->lexer);
3483 /* This function is called at the end of a statement or declaration.
3484 If the next token is a semicolon, it is consumed; otherwise, error
3485 recovery is attempted. */
3487 static void
3488 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3490 /* Look for the trailing `;'. */
3491 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3493 /* If there is additional (erroneous) input, skip to the end of
3494 the statement. */
3495 cp_parser_skip_to_end_of_statement (parser);
3496 /* If the next token is now a `;', consume it. */
3497 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3498 cp_lexer_consume_token (parser->lexer);
3502 /* Skip tokens until we have consumed an entire block, or until we
3503 have consumed a non-nested `;'. */
3505 static void
3506 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3508 int nesting_depth = 0;
3510 /* Unwind generic function template scope if necessary. */
3511 if (parser->fully_implicit_function_template_p)
3512 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3514 while (nesting_depth >= 0)
3516 cp_token *token = cp_lexer_peek_token (parser->lexer);
3518 switch (token->type)
3520 case CPP_EOF:
3521 case CPP_PRAGMA_EOL:
3522 /* If we've run out of tokens, stop. */
3523 return;
3525 case CPP_SEMICOLON:
3526 /* Stop if this is an unnested ';'. */
3527 if (!nesting_depth)
3528 nesting_depth = -1;
3529 break;
3531 case CPP_CLOSE_BRACE:
3532 /* Stop if this is an unnested '}', or closes the outermost
3533 nesting level. */
3534 nesting_depth--;
3535 if (nesting_depth < 0)
3536 return;
3537 if (!nesting_depth)
3538 nesting_depth = -1;
3539 break;
3541 case CPP_OPEN_BRACE:
3542 /* Nest. */
3543 nesting_depth++;
3544 break;
3546 default:
3547 break;
3550 /* Consume the token. */
3551 cp_lexer_consume_token (parser->lexer);
3555 /* Skip tokens until a non-nested closing curly brace is the next
3556 token, or there are no more tokens. Return true in the first case,
3557 false otherwise. */
3559 static bool
3560 cp_parser_skip_to_closing_brace (cp_parser *parser)
3562 unsigned nesting_depth = 0;
3564 while (true)
3566 cp_token *token = cp_lexer_peek_token (parser->lexer);
3568 switch (token->type)
3570 case CPP_EOF:
3571 case CPP_PRAGMA_EOL:
3572 /* If we've run out of tokens, stop. */
3573 return false;
3575 case CPP_CLOSE_BRACE:
3576 /* If the next token is a non-nested `}', then we have reached
3577 the end of the current block. */
3578 if (nesting_depth-- == 0)
3579 return true;
3580 break;
3582 case CPP_OPEN_BRACE:
3583 /* If it the next token is a `{', then we are entering a new
3584 block. Consume the entire block. */
3585 ++nesting_depth;
3586 break;
3588 default:
3589 break;
3592 /* Consume the token. */
3593 cp_lexer_consume_token (parser->lexer);
3597 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3598 parameter is the PRAGMA token, allowing us to purge the entire pragma
3599 sequence. */
3601 static void
3602 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3604 cp_token *token;
3606 parser->lexer->in_pragma = false;
3609 token = cp_lexer_consume_token (parser->lexer);
3610 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3612 /* Ensure that the pragma is not parsed again. */
3613 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3616 /* Require pragma end of line, resyncing with it as necessary. The
3617 arguments are as for cp_parser_skip_to_pragma_eol. */
3619 static void
3620 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3622 parser->lexer->in_pragma = false;
3623 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3624 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3627 /* This is a simple wrapper around make_typename_type. When the id is
3628 an unresolved identifier node, we can provide a superior diagnostic
3629 using cp_parser_diagnose_invalid_type_name. */
3631 static tree
3632 cp_parser_make_typename_type (cp_parser *parser, tree id,
3633 location_t id_location)
3635 tree result;
3636 if (identifier_p (id))
3638 result = make_typename_type (parser->scope, id, typename_type,
3639 /*complain=*/tf_none);
3640 if (result == error_mark_node)
3641 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3642 return result;
3644 return make_typename_type (parser->scope, id, typename_type, tf_error);
3647 /* This is a wrapper around the
3648 make_{pointer,ptrmem,reference}_declarator functions that decides
3649 which one to call based on the CODE and CLASS_TYPE arguments. The
3650 CODE argument should be one of the values returned by
3651 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3652 appertain to the pointer or reference. */
3654 static cp_declarator *
3655 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3656 cp_cv_quals cv_qualifiers,
3657 cp_declarator *target,
3658 tree attributes)
3660 if (code == ERROR_MARK)
3661 return cp_error_declarator;
3663 if (code == INDIRECT_REF)
3664 if (class_type == NULL_TREE)
3665 return make_pointer_declarator (cv_qualifiers, target, attributes);
3666 else
3667 return make_ptrmem_declarator (cv_qualifiers, class_type,
3668 target, attributes);
3669 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3670 return make_reference_declarator (cv_qualifiers, target,
3671 false, attributes);
3672 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3673 return make_reference_declarator (cv_qualifiers, target,
3674 true, attributes);
3675 gcc_unreachable ();
3678 /* Create a new C++ parser. */
3680 static cp_parser *
3681 cp_parser_new (void)
3683 cp_parser *parser;
3684 cp_lexer *lexer;
3685 unsigned i;
3687 /* cp_lexer_new_main is called before doing GC allocation because
3688 cp_lexer_new_main might load a PCH file. */
3689 lexer = cp_lexer_new_main ();
3691 /* Initialize the binops_by_token so that we can get the tree
3692 directly from the token. */
3693 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3694 binops_by_token[binops[i].token_type] = binops[i];
3696 parser = ggc_cleared_alloc<cp_parser> ();
3697 parser->lexer = lexer;
3698 parser->context = cp_parser_context_new (NULL);
3700 /* For now, we always accept GNU extensions. */
3701 parser->allow_gnu_extensions_p = 1;
3703 /* The `>' token is a greater-than operator, not the end of a
3704 template-id. */
3705 parser->greater_than_is_operator_p = true;
3707 parser->default_arg_ok_p = true;
3709 /* We are not parsing a constant-expression. */
3710 parser->integral_constant_expression_p = false;
3711 parser->allow_non_integral_constant_expression_p = false;
3712 parser->non_integral_constant_expression_p = false;
3714 /* Local variable names are not forbidden. */
3715 parser->local_variables_forbidden_p = false;
3717 /* We are not processing an `extern "C"' declaration. */
3718 parser->in_unbraced_linkage_specification_p = false;
3720 /* We are not processing a declarator. */
3721 parser->in_declarator_p = false;
3723 /* We are not processing a template-argument-list. */
3724 parser->in_template_argument_list_p = false;
3726 /* We are not in an iteration statement. */
3727 parser->in_statement = 0;
3729 /* We are not in a switch statement. */
3730 parser->in_switch_statement_p = false;
3732 /* We are not parsing a type-id inside an expression. */
3733 parser->in_type_id_in_expr_p = false;
3735 /* Declarations aren't implicitly extern "C". */
3736 parser->implicit_extern_c = false;
3738 /* String literals should be translated to the execution character set. */
3739 parser->translate_strings_p = true;
3741 /* We are not parsing a function body. */
3742 parser->in_function_body = false;
3744 /* We can correct until told otherwise. */
3745 parser->colon_corrects_to_scope_p = true;
3747 /* The unparsed function queue is empty. */
3748 push_unparsed_function_queues (parser);
3750 /* There are no classes being defined. */
3751 parser->num_classes_being_defined = 0;
3753 /* No template parameters apply. */
3754 parser->num_template_parameter_lists = 0;
3756 /* Not declaring an implicit function template. */
3757 parser->auto_is_implicit_function_template_parm_p = false;
3758 parser->fully_implicit_function_template_p = false;
3759 parser->implicit_template_parms = 0;
3760 parser->implicit_template_scope = 0;
3762 /* Active OpenACC routine clauses. */
3763 parser->oacc_routine = NULL;
3765 /* Allow constrained-type-specifiers. */
3766 parser->prevent_constrained_type_specifiers = 0;
3768 return parser;
3771 /* Create a cp_lexer structure which will emit the tokens in CACHE
3772 and push it onto the parser's lexer stack. This is used for delayed
3773 parsing of in-class method bodies and default arguments, and should
3774 not be confused with tentative parsing. */
3775 static void
3776 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3778 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3779 lexer->next = parser->lexer;
3780 parser->lexer = lexer;
3782 /* Move the current source position to that of the first token in the
3783 new lexer. */
3784 cp_lexer_set_source_position_from_token (lexer->next_token);
3787 /* Pop the top lexer off the parser stack. This is never used for the
3788 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3789 static void
3790 cp_parser_pop_lexer (cp_parser *parser)
3792 cp_lexer *lexer = parser->lexer;
3793 parser->lexer = lexer->next;
3794 cp_lexer_destroy (lexer);
3796 /* Put the current source position back where it was before this
3797 lexer was pushed. */
3798 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3801 /* Lexical conventions [gram.lex] */
3803 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3804 identifier. */
3806 static cp_expr
3807 cp_parser_identifier (cp_parser* parser)
3809 cp_token *token;
3811 /* Look for the identifier. */
3812 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3813 /* Return the value. */
3814 if (token)
3815 return cp_expr (token->u.value, token->location);
3816 else
3817 return error_mark_node;
3820 /* Parse a sequence of adjacent string constants. Returns a
3821 TREE_STRING representing the combined, nul-terminated string
3822 constant. If TRANSLATE is true, translate the string to the
3823 execution character set. If WIDE_OK is true, a wide string is
3824 invalid here.
3826 C++98 [lex.string] says that if a narrow string literal token is
3827 adjacent to a wide string literal token, the behavior is undefined.
3828 However, C99 6.4.5p4 says that this results in a wide string literal.
3829 We follow C99 here, for consistency with the C front end.
3831 This code is largely lifted from lex_string() in c-lex.c.
3833 FUTURE: ObjC++ will need to handle @-strings here. */
3834 static cp_expr
3835 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3836 bool lookup_udlit = true)
3838 tree value;
3839 size_t count;
3840 struct obstack str_ob;
3841 cpp_string str, istr, *strs;
3842 cp_token *tok;
3843 enum cpp_ttype type, curr_type;
3844 int have_suffix_p = 0;
3845 tree string_tree;
3846 tree suffix_id = NULL_TREE;
3847 bool curr_tok_is_userdef_p = false;
3849 tok = cp_lexer_peek_token (parser->lexer);
3850 if (!cp_parser_is_string_literal (tok))
3852 cp_parser_error (parser, "expected string-literal");
3853 return error_mark_node;
3856 location_t loc = tok->location;
3858 if (cpp_userdef_string_p (tok->type))
3860 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3861 curr_type = cpp_userdef_string_remove_type (tok->type);
3862 curr_tok_is_userdef_p = true;
3864 else
3866 string_tree = tok->u.value;
3867 curr_type = tok->type;
3869 type = curr_type;
3871 /* Try to avoid the overhead of creating and destroying an obstack
3872 for the common case of just one string. */
3873 if (!cp_parser_is_string_literal
3874 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3876 cp_lexer_consume_token (parser->lexer);
3878 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3879 str.len = TREE_STRING_LENGTH (string_tree);
3880 count = 1;
3882 if (curr_tok_is_userdef_p)
3884 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3885 have_suffix_p = 1;
3886 curr_type = cpp_userdef_string_remove_type (tok->type);
3888 else
3889 curr_type = tok->type;
3891 strs = &str;
3893 else
3895 location_t last_tok_loc;
3896 gcc_obstack_init (&str_ob);
3897 count = 0;
3901 last_tok_loc = tok->location;
3902 cp_lexer_consume_token (parser->lexer);
3903 count++;
3904 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3905 str.len = TREE_STRING_LENGTH (string_tree);
3907 if (curr_tok_is_userdef_p)
3909 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3910 if (have_suffix_p == 0)
3912 suffix_id = curr_suffix_id;
3913 have_suffix_p = 1;
3915 else if (have_suffix_p == 1
3916 && curr_suffix_id != suffix_id)
3918 error ("inconsistent user-defined literal suffixes"
3919 " %qD and %qD in string literal",
3920 suffix_id, curr_suffix_id);
3921 have_suffix_p = -1;
3923 curr_type = cpp_userdef_string_remove_type (tok->type);
3925 else
3926 curr_type = tok->type;
3928 if (type != curr_type)
3930 if (type == CPP_STRING)
3931 type = curr_type;
3932 else if (curr_type != CPP_STRING)
3933 error_at (tok->location,
3934 "unsupported non-standard concatenation "
3935 "of string literals");
3938 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3940 tok = cp_lexer_peek_token (parser->lexer);
3941 if (cpp_userdef_string_p (tok->type))
3943 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3944 curr_type = cpp_userdef_string_remove_type (tok->type);
3945 curr_tok_is_userdef_p = true;
3947 else
3949 string_tree = tok->u.value;
3950 curr_type = tok->type;
3951 curr_tok_is_userdef_p = false;
3954 while (cp_parser_is_string_literal (tok));
3956 /* A string literal built by concatenation has its caret=start at
3957 the start of the initial string, and its finish at the finish of
3958 the final string literal. */
3959 loc = make_location (loc, loc, get_finish (last_tok_loc));
3961 strs = (cpp_string *) obstack_finish (&str_ob);
3964 if (type != CPP_STRING && !wide_ok)
3966 cp_parser_error (parser, "a wide string is invalid in this context");
3967 type = CPP_STRING;
3970 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3971 (parse_in, strs, count, &istr, type))
3973 value = build_string (istr.len, (const char *)istr.text);
3974 free (CONST_CAST (unsigned char *, istr.text));
3976 switch (type)
3978 default:
3979 case CPP_STRING:
3980 case CPP_UTF8STRING:
3981 TREE_TYPE (value) = char_array_type_node;
3982 break;
3983 case CPP_STRING16:
3984 TREE_TYPE (value) = char16_array_type_node;
3985 break;
3986 case CPP_STRING32:
3987 TREE_TYPE (value) = char32_array_type_node;
3988 break;
3989 case CPP_WSTRING:
3990 TREE_TYPE (value) = wchar_array_type_node;
3991 break;
3994 value = fix_string_type (value);
3996 if (have_suffix_p)
3998 tree literal = build_userdef_literal (suffix_id, value,
3999 OT_NONE, NULL_TREE);
4000 if (lookup_udlit)
4001 value = cp_parser_userdef_string_literal (literal);
4002 else
4003 value = literal;
4006 else
4007 /* cpp_interpret_string has issued an error. */
4008 value = error_mark_node;
4010 if (count > 1)
4011 obstack_free (&str_ob, 0);
4013 return cp_expr (value, loc);
4016 /* Look up a literal operator with the name and the exact arguments. */
4018 static tree
4019 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4021 tree decl, fns;
4022 decl = lookup_name (name);
4023 if (!decl || !is_overloaded_fn (decl))
4024 return error_mark_node;
4026 for (fns = decl; fns; fns = OVL_NEXT (fns))
4028 unsigned int ix;
4029 bool found = true;
4030 tree fn = OVL_CURRENT (fns);
4031 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4032 if (parmtypes != NULL_TREE)
4034 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4035 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4037 tree tparm = TREE_VALUE (parmtypes);
4038 tree targ = TREE_TYPE ((*args)[ix]);
4039 bool ptr = TYPE_PTR_P (tparm);
4040 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4041 if ((ptr || arr || !same_type_p (tparm, targ))
4042 && (!ptr || !arr
4043 || !same_type_p (TREE_TYPE (tparm),
4044 TREE_TYPE (targ))))
4045 found = false;
4047 if (found
4048 && ix == vec_safe_length (args)
4049 /* May be this should be sufficient_parms_p instead,
4050 depending on how exactly should user-defined literals
4051 work in presence of default arguments on the literal
4052 operator parameters. */
4053 && parmtypes == void_list_node)
4054 return decl;
4058 return error_mark_node;
4061 /* Parse a user-defined char constant. Returns a call to a user-defined
4062 literal operator taking the character as an argument. */
4064 static cp_expr
4065 cp_parser_userdef_char_literal (cp_parser *parser)
4067 cp_token *token = cp_lexer_consume_token (parser->lexer);
4068 tree literal = token->u.value;
4069 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4070 tree value = USERDEF_LITERAL_VALUE (literal);
4071 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4072 tree decl, result;
4074 /* Build up a call to the user-defined operator */
4075 /* Lookup the name we got back from the id-expression. */
4076 vec<tree, va_gc> *args = make_tree_vector ();
4077 vec_safe_push (args, value);
4078 decl = lookup_literal_operator (name, args);
4079 if (!decl || decl == error_mark_node)
4081 error ("unable to find character literal operator %qD with %qT argument",
4082 name, TREE_TYPE (value));
4083 release_tree_vector (args);
4084 return error_mark_node;
4086 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4087 release_tree_vector (args);
4088 return result;
4091 /* A subroutine of cp_parser_userdef_numeric_literal to
4092 create a char... template parameter pack from a string node. */
4094 static tree
4095 make_char_string_pack (tree value)
4097 tree charvec;
4098 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4099 const char *str = TREE_STRING_POINTER (value);
4100 int i, len = TREE_STRING_LENGTH (value) - 1;
4101 tree argvec = make_tree_vec (1);
4103 /* Fill in CHARVEC with all of the parameters. */
4104 charvec = make_tree_vec (len);
4105 for (i = 0; i < len; ++i)
4106 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4108 /* Build the argument packs. */
4109 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4110 TREE_TYPE (argpack) = char_type_node;
4112 TREE_VEC_ELT (argvec, 0) = argpack;
4114 return argvec;
4117 /* A subroutine of cp_parser_userdef_numeric_literal to
4118 create a char... template parameter pack from a string node. */
4120 static tree
4121 make_string_pack (tree value)
4123 tree charvec;
4124 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4125 const unsigned char *str
4126 = (const unsigned char *) TREE_STRING_POINTER (value);
4127 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4128 int len = TREE_STRING_LENGTH (value) / sz - 1;
4129 tree argvec = make_tree_vec (2);
4131 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4132 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4134 /* First template parm is character type. */
4135 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4137 /* Fill in CHARVEC with all of the parameters. */
4138 charvec = make_tree_vec (len);
4139 for (int i = 0; i < len; ++i)
4140 TREE_VEC_ELT (charvec, i)
4141 = double_int_to_tree (str_char_type_node,
4142 double_int::from_buffer (str + i * sz, sz));
4144 /* Build the argument packs. */
4145 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4146 TREE_TYPE (argpack) = str_char_type_node;
4148 TREE_VEC_ELT (argvec, 1) = argpack;
4150 return argvec;
4153 /* Parse a user-defined numeric constant. returns a call to a user-defined
4154 literal operator. */
4156 static cp_expr
4157 cp_parser_userdef_numeric_literal (cp_parser *parser)
4159 cp_token *token = cp_lexer_consume_token (parser->lexer);
4160 tree literal = token->u.value;
4161 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4162 tree value = USERDEF_LITERAL_VALUE (literal);
4163 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4164 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4165 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4166 tree decl, result;
4167 vec<tree, va_gc> *args;
4169 /* Look for a literal operator taking the exact type of numeric argument
4170 as the literal value. */
4171 args = make_tree_vector ();
4172 vec_safe_push (args, value);
4173 decl = lookup_literal_operator (name, args);
4174 if (decl && decl != error_mark_node)
4176 result = finish_call_expr (decl, &args, false, true,
4177 tf_warning_or_error);
4179 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4181 warning_at (token->location, OPT_Woverflow,
4182 "integer literal exceeds range of %qT type",
4183 long_long_unsigned_type_node);
4185 else
4187 if (overflow > 0)
4188 warning_at (token->location, OPT_Woverflow,
4189 "floating literal exceeds range of %qT type",
4190 long_double_type_node);
4191 else if (overflow < 0)
4192 warning_at (token->location, OPT_Woverflow,
4193 "floating literal truncated to zero");
4196 release_tree_vector (args);
4197 return result;
4199 release_tree_vector (args);
4201 /* If the numeric argument didn't work, look for a raw literal
4202 operator taking a const char* argument consisting of the number
4203 in string format. */
4204 args = make_tree_vector ();
4205 vec_safe_push (args, num_string);
4206 decl = lookup_literal_operator (name, args);
4207 if (decl && decl != error_mark_node)
4209 result = finish_call_expr (decl, &args, false, true,
4210 tf_warning_or_error);
4211 release_tree_vector (args);
4212 return result;
4214 release_tree_vector (args);
4216 /* If the raw literal didn't work, look for a non-type template
4217 function with parameter pack char.... Call the function with
4218 template parameter characters representing the number. */
4219 args = make_tree_vector ();
4220 decl = lookup_literal_operator (name, args);
4221 if (decl && decl != error_mark_node)
4223 tree tmpl_args = make_char_string_pack (num_string);
4224 decl = lookup_template_function (decl, tmpl_args);
4225 result = finish_call_expr (decl, &args, false, true,
4226 tf_warning_or_error);
4227 release_tree_vector (args);
4228 return result;
4231 release_tree_vector (args);
4233 error ("unable to find numeric literal operator %qD", name);
4234 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4235 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4236 "to enable more built-in suffixes");
4237 return error_mark_node;
4240 /* Parse a user-defined string constant. Returns a call to a user-defined
4241 literal operator taking a character pointer and the length of the string
4242 as arguments. */
4244 static tree
4245 cp_parser_userdef_string_literal (tree literal)
4247 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4248 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4249 tree value = USERDEF_LITERAL_VALUE (literal);
4250 int len = TREE_STRING_LENGTH (value)
4251 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4252 tree decl, result;
4253 vec<tree, va_gc> *args;
4255 /* Build up a call to the user-defined operator. */
4256 /* Lookup the name we got back from the id-expression. */
4257 args = make_tree_vector ();
4258 vec_safe_push (args, value);
4259 vec_safe_push (args, build_int_cst (size_type_node, len));
4260 decl = lookup_literal_operator (name, args);
4262 if (decl && decl != error_mark_node)
4264 result = finish_call_expr (decl, &args, false, true,
4265 tf_warning_or_error);
4266 release_tree_vector (args);
4267 return result;
4269 release_tree_vector (args);
4271 /* Look for a template function with typename parameter CharT
4272 and parameter pack CharT... Call the function with
4273 template parameter characters representing the string. */
4274 args = make_tree_vector ();
4275 decl = lookup_literal_operator (name, args);
4276 if (decl && decl != error_mark_node)
4278 tree tmpl_args = make_string_pack (value);
4279 decl = lookup_template_function (decl, tmpl_args);
4280 result = finish_call_expr (decl, &args, false, true,
4281 tf_warning_or_error);
4282 release_tree_vector (args);
4283 return result;
4285 release_tree_vector (args);
4287 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4288 name, TREE_TYPE (value), size_type_node);
4289 return error_mark_node;
4293 /* Basic concepts [gram.basic] */
4295 /* Parse a translation-unit.
4297 translation-unit:
4298 declaration-seq [opt]
4300 Returns TRUE if all went well. */
4302 static bool
4303 cp_parser_translation_unit (cp_parser* parser)
4305 /* The address of the first non-permanent object on the declarator
4306 obstack. */
4307 static void *declarator_obstack_base;
4309 bool success;
4311 /* Create the declarator obstack, if necessary. */
4312 if (!cp_error_declarator)
4314 gcc_obstack_init (&declarator_obstack);
4315 /* Create the error declarator. */
4316 cp_error_declarator = make_declarator (cdk_error);
4317 /* Create the empty parameter list. */
4318 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4319 /* Remember where the base of the declarator obstack lies. */
4320 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4323 cp_parser_declaration_seq_opt (parser);
4325 /* If there are no tokens left then all went well. */
4326 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4328 /* Get rid of the token array; we don't need it any more. */
4329 cp_lexer_destroy (parser->lexer);
4330 parser->lexer = NULL;
4332 /* This file might have been a context that's implicitly extern
4333 "C". If so, pop the lang context. (Only relevant for PCH.) */
4334 if (parser->implicit_extern_c)
4336 pop_lang_context ();
4337 parser->implicit_extern_c = false;
4340 /* Finish up. */
4341 finish_translation_unit ();
4343 success = true;
4345 else
4347 cp_parser_error (parser, "expected declaration");
4348 success = false;
4351 /* Make sure the declarator obstack was fully cleaned up. */
4352 gcc_assert (obstack_next_free (&declarator_obstack)
4353 == declarator_obstack_base);
4355 /* All went well. */
4356 return success;
4359 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4360 decltype context. */
4362 static inline tsubst_flags_t
4363 complain_flags (bool decltype_p)
4365 tsubst_flags_t complain = tf_warning_or_error;
4366 if (decltype_p)
4367 complain |= tf_decltype;
4368 return complain;
4371 /* We're about to parse a collection of statements. If we're currently
4372 parsing tentatively, set up a firewall so that any nested
4373 cp_parser_commit_to_tentative_parse won't affect the current context. */
4375 static cp_token_position
4376 cp_parser_start_tentative_firewall (cp_parser *parser)
4378 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4379 return 0;
4381 cp_parser_parse_tentatively (parser);
4382 cp_parser_commit_to_topmost_tentative_parse (parser);
4383 return cp_lexer_token_position (parser->lexer, false);
4386 /* We've finished parsing the collection of statements. Wrap up the
4387 firewall and replace the relevant tokens with the parsed form. */
4389 static void
4390 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4391 tree expr)
4393 if (!start)
4394 return;
4396 /* Finish the firewall level. */
4397 cp_parser_parse_definitely (parser);
4398 /* And remember the result of the parse for when we try again. */
4399 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4400 token->type = CPP_PREPARSED_EXPR;
4401 token->u.value = expr;
4402 token->keyword = RID_MAX;
4403 cp_lexer_purge_tokens_after (parser->lexer, start);
4406 /* Like the above functions, but let the user modify the tokens. Used by
4407 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4408 later parses, so it makes sense to localize the effects of
4409 cp_parser_commit_to_tentative_parse. */
4411 struct tentative_firewall
4413 cp_parser *parser;
4414 bool set;
4416 tentative_firewall (cp_parser *p): parser(p)
4418 /* If we're currently parsing tentatively, start a committed level as a
4419 firewall and then an inner tentative parse. */
4420 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4422 cp_parser_parse_tentatively (parser);
4423 cp_parser_commit_to_topmost_tentative_parse (parser);
4424 cp_parser_parse_tentatively (parser);
4428 ~tentative_firewall()
4430 if (set)
4432 /* Finish the inner tentative parse and the firewall, propagating any
4433 uncommitted error state to the outer tentative parse. */
4434 bool err = cp_parser_error_occurred (parser);
4435 cp_parser_parse_definitely (parser);
4436 cp_parser_parse_definitely (parser);
4437 if (err)
4438 cp_parser_simulate_error (parser);
4443 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4444 enclosing parentheses. */
4446 static cp_expr
4447 cp_parser_statement_expr (cp_parser *parser)
4449 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4451 /* Consume the '('. */
4452 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4453 cp_lexer_consume_token (parser->lexer);
4454 /* Start the statement-expression. */
4455 tree expr = begin_stmt_expr ();
4456 /* Parse the compound-statement. */
4457 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4458 /* Finish up. */
4459 expr = finish_stmt_expr (expr, false);
4460 /* Consume the ')'. */
4461 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4462 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4463 cp_parser_skip_to_end_of_statement (parser);
4465 cp_parser_end_tentative_firewall (parser, start, expr);
4466 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4467 return cp_expr (expr, combined_loc);
4470 /* Expressions [gram.expr] */
4472 /* Parse a fold-operator.
4474 fold-operator:
4475 - * / % ^ & | = < > << >>
4476 = -= *= /= %= ^= &= |= <<= >>=
4477 == != <= >= && || , .* ->*
4479 This returns the tree code corresponding to the matched operator
4480 as an int. When the current token matches a compound assignment
4481 opertor, the resulting tree code is the negative value of the
4482 non-assignment operator. */
4484 static int
4485 cp_parser_fold_operator (cp_token *token)
4487 switch (token->type)
4489 case CPP_PLUS: return PLUS_EXPR;
4490 case CPP_MINUS: return MINUS_EXPR;
4491 case CPP_MULT: return MULT_EXPR;
4492 case CPP_DIV: return TRUNC_DIV_EXPR;
4493 case CPP_MOD: return TRUNC_MOD_EXPR;
4494 case CPP_XOR: return BIT_XOR_EXPR;
4495 case CPP_AND: return BIT_AND_EXPR;
4496 case CPP_OR: return BIT_IOR_EXPR;
4497 case CPP_LSHIFT: return LSHIFT_EXPR;
4498 case CPP_RSHIFT: return RSHIFT_EXPR;
4500 case CPP_EQ: return -NOP_EXPR;
4501 case CPP_PLUS_EQ: return -PLUS_EXPR;
4502 case CPP_MINUS_EQ: return -MINUS_EXPR;
4503 case CPP_MULT_EQ: return -MULT_EXPR;
4504 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4505 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4506 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4507 case CPP_AND_EQ: return -BIT_AND_EXPR;
4508 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4509 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4510 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4512 case CPP_EQ_EQ: return EQ_EXPR;
4513 case CPP_NOT_EQ: return NE_EXPR;
4514 case CPP_LESS: return LT_EXPR;
4515 case CPP_GREATER: return GT_EXPR;
4516 case CPP_LESS_EQ: return LE_EXPR;
4517 case CPP_GREATER_EQ: return GE_EXPR;
4519 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4520 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4522 case CPP_COMMA: return COMPOUND_EXPR;
4524 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4525 case CPP_DEREF_STAR: return MEMBER_REF;
4527 default: return ERROR_MARK;
4531 /* Returns true if CODE indicates a binary expression, which is not allowed in
4532 the LHS of a fold-expression. More codes will need to be added to use this
4533 function in other contexts. */
4535 static bool
4536 is_binary_op (tree_code code)
4538 switch (code)
4540 case PLUS_EXPR:
4541 case POINTER_PLUS_EXPR:
4542 case MINUS_EXPR:
4543 case MULT_EXPR:
4544 case TRUNC_DIV_EXPR:
4545 case TRUNC_MOD_EXPR:
4546 case BIT_XOR_EXPR:
4547 case BIT_AND_EXPR:
4548 case BIT_IOR_EXPR:
4549 case LSHIFT_EXPR:
4550 case RSHIFT_EXPR:
4552 case MODOP_EXPR:
4554 case EQ_EXPR:
4555 case NE_EXPR:
4556 case LE_EXPR:
4557 case GE_EXPR:
4558 case LT_EXPR:
4559 case GT_EXPR:
4561 case TRUTH_ANDIF_EXPR:
4562 case TRUTH_ORIF_EXPR:
4564 case COMPOUND_EXPR:
4566 case DOTSTAR_EXPR:
4567 case MEMBER_REF:
4568 return true;
4570 default:
4571 return false;
4575 /* If the next token is a suitable fold operator, consume it and return as
4576 the function above. */
4578 static int
4579 cp_parser_fold_operator (cp_parser *parser)
4581 cp_token* token = cp_lexer_peek_token (parser->lexer);
4582 int code = cp_parser_fold_operator (token);
4583 if (code != ERROR_MARK)
4584 cp_lexer_consume_token (parser->lexer);
4585 return code;
4588 /* Parse a fold-expression.
4590 fold-expression:
4591 ( ... folding-operator cast-expression)
4592 ( cast-expression folding-operator ... )
4593 ( cast-expression folding operator ... folding-operator cast-expression)
4595 Note that the '(' and ')' are matched in primary expression. */
4597 static cp_expr
4598 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4600 cp_id_kind pidk;
4602 // Left fold.
4603 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4605 cp_lexer_consume_token (parser->lexer);
4606 int op = cp_parser_fold_operator (parser);
4607 if (op == ERROR_MARK)
4609 cp_parser_error (parser, "expected binary operator");
4610 return error_mark_node;
4613 tree expr = cp_parser_cast_expression (parser, false, false,
4614 false, &pidk);
4615 if (expr == error_mark_node)
4616 return error_mark_node;
4617 return finish_left_unary_fold_expr (expr, op);
4620 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4621 int op = cp_parser_fold_operator (parser);
4622 if (op == ERROR_MARK)
4624 cp_parser_error (parser, "expected binary operator");
4625 return error_mark_node;
4628 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4630 cp_parser_error (parser, "expected ...");
4631 return error_mark_node;
4633 cp_lexer_consume_token (parser->lexer);
4635 /* The operands of a fold-expression are cast-expressions, so binary or
4636 conditional expressions are not allowed. We check this here to avoid
4637 tentative parsing. */
4638 if (is_binary_op (TREE_CODE (expr1)))
4639 error_at (location_of (expr1),
4640 "binary expression in operand of fold-expression");
4641 else if (TREE_CODE (expr1) == COND_EXPR)
4642 error_at (location_of (expr1),
4643 "conditional expression in operand of fold-expression");
4645 // Right fold.
4646 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4647 return finish_right_unary_fold_expr (expr1, op);
4649 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4651 cp_parser_error (parser, "mismatched operator in fold-expression");
4652 return error_mark_node;
4654 cp_lexer_consume_token (parser->lexer);
4656 // Binary left or right fold.
4657 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4658 if (expr2 == error_mark_node)
4659 return error_mark_node;
4660 return finish_binary_fold_expr (expr1, expr2, op);
4663 /* Parse a primary-expression.
4665 primary-expression:
4666 literal
4667 this
4668 ( expression )
4669 id-expression
4670 lambda-expression (C++11)
4672 GNU Extensions:
4674 primary-expression:
4675 ( compound-statement )
4676 __builtin_va_arg ( assignment-expression , type-id )
4677 __builtin_offsetof ( type-id , offsetof-expression )
4679 C++ Extensions:
4680 __has_nothrow_assign ( type-id )
4681 __has_nothrow_constructor ( type-id )
4682 __has_nothrow_copy ( type-id )
4683 __has_trivial_assign ( type-id )
4684 __has_trivial_constructor ( type-id )
4685 __has_trivial_copy ( type-id )
4686 __has_trivial_destructor ( type-id )
4687 __has_virtual_destructor ( type-id )
4688 __is_abstract ( type-id )
4689 __is_base_of ( type-id , type-id )
4690 __is_class ( type-id )
4691 __is_empty ( type-id )
4692 __is_enum ( type-id )
4693 __is_final ( type-id )
4694 __is_literal_type ( type-id )
4695 __is_pod ( type-id )
4696 __is_polymorphic ( type-id )
4697 __is_std_layout ( type-id )
4698 __is_trivial ( type-id )
4699 __is_union ( type-id )
4701 Objective-C++ Extension:
4703 primary-expression:
4704 objc-expression
4706 literal:
4707 __null
4709 ADDRESS_P is true iff this expression was immediately preceded by
4710 "&" and therefore might denote a pointer-to-member. CAST_P is true
4711 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4712 true iff this expression is a template argument.
4714 Returns a representation of the expression. Upon return, *IDK
4715 indicates what kind of id-expression (if any) was present. */
4717 static cp_expr
4718 cp_parser_primary_expression (cp_parser *parser,
4719 bool address_p,
4720 bool cast_p,
4721 bool template_arg_p,
4722 bool decltype_p,
4723 cp_id_kind *idk)
4725 cp_token *token = NULL;
4727 /* Assume the primary expression is not an id-expression. */
4728 *idk = CP_ID_KIND_NONE;
4730 /* Peek at the next token. */
4731 token = cp_lexer_peek_token (parser->lexer);
4732 switch ((int) token->type)
4734 /* literal:
4735 integer-literal
4736 character-literal
4737 floating-literal
4738 string-literal
4739 boolean-literal
4740 pointer-literal
4741 user-defined-literal */
4742 case CPP_CHAR:
4743 case CPP_CHAR16:
4744 case CPP_CHAR32:
4745 case CPP_WCHAR:
4746 case CPP_UTF8CHAR:
4747 case CPP_NUMBER:
4748 case CPP_PREPARSED_EXPR:
4749 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4750 return cp_parser_userdef_numeric_literal (parser);
4751 token = cp_lexer_consume_token (parser->lexer);
4752 if (TREE_CODE (token->u.value) == FIXED_CST)
4754 error_at (token->location,
4755 "fixed-point types not supported in C++");
4756 return error_mark_node;
4758 /* Floating-point literals are only allowed in an integral
4759 constant expression if they are cast to an integral or
4760 enumeration type. */
4761 if (TREE_CODE (token->u.value) == REAL_CST
4762 && parser->integral_constant_expression_p
4763 && pedantic)
4765 /* CAST_P will be set even in invalid code like "int(2.7 +
4766 ...)". Therefore, we have to check that the next token
4767 is sure to end the cast. */
4768 if (cast_p)
4770 cp_token *next_token;
4772 next_token = cp_lexer_peek_token (parser->lexer);
4773 if (/* The comma at the end of an
4774 enumerator-definition. */
4775 next_token->type != CPP_COMMA
4776 /* The curly brace at the end of an enum-specifier. */
4777 && next_token->type != CPP_CLOSE_BRACE
4778 /* The end of a statement. */
4779 && next_token->type != CPP_SEMICOLON
4780 /* The end of the cast-expression. */
4781 && next_token->type != CPP_CLOSE_PAREN
4782 /* The end of an array bound. */
4783 && next_token->type != CPP_CLOSE_SQUARE
4784 /* The closing ">" in a template-argument-list. */
4785 && (next_token->type != CPP_GREATER
4786 || parser->greater_than_is_operator_p)
4787 /* C++0x only: A ">>" treated like two ">" tokens,
4788 in a template-argument-list. */
4789 && (next_token->type != CPP_RSHIFT
4790 || (cxx_dialect == cxx98)
4791 || parser->greater_than_is_operator_p))
4792 cast_p = false;
4795 /* If we are within a cast, then the constraint that the
4796 cast is to an integral or enumeration type will be
4797 checked at that point. If we are not within a cast, then
4798 this code is invalid. */
4799 if (!cast_p)
4800 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4802 return cp_expr (token->u.value, token->location);
4804 case CPP_CHAR_USERDEF:
4805 case CPP_CHAR16_USERDEF:
4806 case CPP_CHAR32_USERDEF:
4807 case CPP_WCHAR_USERDEF:
4808 case CPP_UTF8CHAR_USERDEF:
4809 return cp_parser_userdef_char_literal (parser);
4811 case CPP_STRING:
4812 case CPP_STRING16:
4813 case CPP_STRING32:
4814 case CPP_WSTRING:
4815 case CPP_UTF8STRING:
4816 case CPP_STRING_USERDEF:
4817 case CPP_STRING16_USERDEF:
4818 case CPP_STRING32_USERDEF:
4819 case CPP_WSTRING_USERDEF:
4820 case CPP_UTF8STRING_USERDEF:
4821 /* ??? Should wide strings be allowed when parser->translate_strings_p
4822 is false (i.e. in attributes)? If not, we can kill the third
4823 argument to cp_parser_string_literal. */
4824 return cp_parser_string_literal (parser,
4825 parser->translate_strings_p,
4826 true);
4828 case CPP_OPEN_PAREN:
4829 /* If we see `( { ' then we are looking at the beginning of
4830 a GNU statement-expression. */
4831 if (cp_parser_allow_gnu_extensions_p (parser)
4832 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4834 /* Statement-expressions are not allowed by the standard. */
4835 pedwarn (token->location, OPT_Wpedantic,
4836 "ISO C++ forbids braced-groups within expressions");
4838 /* And they're not allowed outside of a function-body; you
4839 cannot, for example, write:
4841 int i = ({ int j = 3; j + 1; });
4843 at class or namespace scope. */
4844 if (!parser->in_function_body
4845 || parser->in_template_argument_list_p)
4847 error_at (token->location,
4848 "statement-expressions are not allowed outside "
4849 "functions nor in template-argument lists");
4850 cp_parser_skip_to_end_of_block_or_statement (parser);
4851 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4852 cp_lexer_consume_token (parser->lexer);
4853 return error_mark_node;
4855 else
4856 return cp_parser_statement_expr (parser);
4858 /* Otherwise it's a normal parenthesized expression. */
4860 cp_expr expr;
4861 bool saved_greater_than_is_operator_p;
4863 location_t open_paren_loc = token->location;
4865 /* Consume the `('. */
4866 cp_lexer_consume_token (parser->lexer);
4867 /* Within a parenthesized expression, a `>' token is always
4868 the greater-than operator. */
4869 saved_greater_than_is_operator_p
4870 = parser->greater_than_is_operator_p;
4871 parser->greater_than_is_operator_p = true;
4873 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4874 /* Left fold expression. */
4875 expr = NULL_TREE;
4876 else
4877 /* Parse the parenthesized expression. */
4878 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4880 token = cp_lexer_peek_token (parser->lexer);
4881 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
4883 expr = cp_parser_fold_expression (parser, expr);
4884 if (expr != error_mark_node
4885 && cxx_dialect < cxx1z
4886 && !in_system_header_at (input_location))
4887 pedwarn (input_location, 0, "fold-expressions only available "
4888 "with -std=c++1z or -std=gnu++1z");
4890 else
4891 /* Let the front end know that this expression was
4892 enclosed in parentheses. This matters in case, for
4893 example, the expression is of the form `A::B', since
4894 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4895 not. */
4896 expr = finish_parenthesized_expr (expr);
4898 /* DR 705: Wrapping an unqualified name in parentheses
4899 suppresses arg-dependent lookup. We want to pass back
4900 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4901 (c++/37862), but none of the others. */
4902 if (*idk != CP_ID_KIND_QUALIFIED)
4903 *idk = CP_ID_KIND_NONE;
4905 /* The `>' token might be the end of a template-id or
4906 template-parameter-list now. */
4907 parser->greater_than_is_operator_p
4908 = saved_greater_than_is_operator_p;
4910 /* Consume the `)'. */
4911 token = cp_lexer_peek_token (parser->lexer);
4912 location_t close_paren_loc = token->location;
4913 expr.set_range (open_paren_loc, close_paren_loc);
4914 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)
4915 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4916 cp_parser_skip_to_end_of_statement (parser);
4918 return expr;
4921 case CPP_OPEN_SQUARE:
4923 if (c_dialect_objc ())
4925 /* We might have an Objective-C++ message. */
4926 cp_parser_parse_tentatively (parser);
4927 tree msg = cp_parser_objc_message_expression (parser);
4928 /* If that works out, we're done ... */
4929 if (cp_parser_parse_definitely (parser))
4930 return msg;
4931 /* ... else, fall though to see if it's a lambda. */
4933 cp_expr lam = cp_parser_lambda_expression (parser);
4934 /* Don't warn about a failed tentative parse. */
4935 if (cp_parser_error_occurred (parser))
4936 return error_mark_node;
4937 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4938 return lam;
4941 case CPP_OBJC_STRING:
4942 if (c_dialect_objc ())
4943 /* We have an Objective-C++ string literal. */
4944 return cp_parser_objc_expression (parser);
4945 cp_parser_error (parser, "expected primary-expression");
4946 return error_mark_node;
4948 case CPP_KEYWORD:
4949 switch (token->keyword)
4951 /* These two are the boolean literals. */
4952 case RID_TRUE:
4953 cp_lexer_consume_token (parser->lexer);
4954 return cp_expr (boolean_true_node, token->location);
4955 case RID_FALSE:
4956 cp_lexer_consume_token (parser->lexer);
4957 return cp_expr (boolean_false_node, token->location);
4959 /* The `__null' literal. */
4960 case RID_NULL:
4961 cp_lexer_consume_token (parser->lexer);
4962 return cp_expr (null_node, token->location);
4964 /* The `nullptr' literal. */
4965 case RID_NULLPTR:
4966 cp_lexer_consume_token (parser->lexer);
4967 return cp_expr (nullptr_node, token->location);
4969 /* Recognize the `this' keyword. */
4970 case RID_THIS:
4971 cp_lexer_consume_token (parser->lexer);
4972 if (parser->local_variables_forbidden_p)
4974 error_at (token->location,
4975 "%<this%> may not be used in this context");
4976 return error_mark_node;
4978 /* Pointers cannot appear in constant-expressions. */
4979 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4980 return error_mark_node;
4981 return cp_expr (finish_this_expr (), token->location);
4983 /* The `operator' keyword can be the beginning of an
4984 id-expression. */
4985 case RID_OPERATOR:
4986 goto id_expression;
4988 case RID_FUNCTION_NAME:
4989 case RID_PRETTY_FUNCTION_NAME:
4990 case RID_C99_FUNCTION_NAME:
4992 non_integral_constant name;
4994 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4995 __func__ are the names of variables -- but they are
4996 treated specially. Therefore, they are handled here,
4997 rather than relying on the generic id-expression logic
4998 below. Grammatically, these names are id-expressions.
5000 Consume the token. */
5001 token = cp_lexer_consume_token (parser->lexer);
5003 switch (token->keyword)
5005 case RID_FUNCTION_NAME:
5006 name = NIC_FUNC_NAME;
5007 break;
5008 case RID_PRETTY_FUNCTION_NAME:
5009 name = NIC_PRETTY_FUNC;
5010 break;
5011 case RID_C99_FUNCTION_NAME:
5012 name = NIC_C99_FUNC;
5013 break;
5014 default:
5015 gcc_unreachable ();
5018 if (cp_parser_non_integral_constant_expression (parser, name))
5019 return error_mark_node;
5021 /* Look up the name. */
5022 return finish_fname (token->u.value);
5025 case RID_VA_ARG:
5027 tree expression;
5028 tree type;
5029 source_location type_location;
5030 location_t start_loc
5031 = cp_lexer_peek_token (parser->lexer)->location;
5032 /* The `__builtin_va_arg' construct is used to handle
5033 `va_arg'. Consume the `__builtin_va_arg' token. */
5034 cp_lexer_consume_token (parser->lexer);
5035 /* Look for the opening `('. */
5036 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5037 /* Now, parse the assignment-expression. */
5038 expression = cp_parser_assignment_expression (parser);
5039 /* Look for the `,'. */
5040 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5041 type_location = cp_lexer_peek_token (parser->lexer)->location;
5042 /* Parse the type-id. */
5044 type_id_in_expr_sentinel s (parser);
5045 type = cp_parser_type_id (parser);
5047 /* Look for the closing `)'. */
5048 location_t finish_loc
5049 = cp_lexer_peek_token (parser->lexer)->location;
5050 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5051 /* Using `va_arg' in a constant-expression is not
5052 allowed. */
5053 if (cp_parser_non_integral_constant_expression (parser,
5054 NIC_VA_ARG))
5055 return error_mark_node;
5056 /* Construct a location of the form:
5057 __builtin_va_arg (v, int)
5058 ~~~~~~~~~~~~~~~~~~~~~^~~~
5059 with the caret at the type, ranging from the start of the
5060 "__builtin_va_arg" token to the close paren. */
5061 location_t combined_loc
5062 = make_location (type_location, start_loc, finish_loc);
5063 return build_x_va_arg (combined_loc, expression, type);
5066 case RID_OFFSETOF:
5067 return cp_parser_builtin_offsetof (parser);
5069 case RID_HAS_NOTHROW_ASSIGN:
5070 case RID_HAS_NOTHROW_CONSTRUCTOR:
5071 case RID_HAS_NOTHROW_COPY:
5072 case RID_HAS_TRIVIAL_ASSIGN:
5073 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5074 case RID_HAS_TRIVIAL_COPY:
5075 case RID_HAS_TRIVIAL_DESTRUCTOR:
5076 case RID_HAS_VIRTUAL_DESTRUCTOR:
5077 case RID_IS_ABSTRACT:
5078 case RID_IS_BASE_OF:
5079 case RID_IS_CLASS:
5080 case RID_IS_EMPTY:
5081 case RID_IS_ENUM:
5082 case RID_IS_FINAL:
5083 case RID_IS_LITERAL_TYPE:
5084 case RID_IS_POD:
5085 case RID_IS_POLYMORPHIC:
5086 case RID_IS_SAME_AS:
5087 case RID_IS_STD_LAYOUT:
5088 case RID_IS_TRIVIAL:
5089 case RID_IS_TRIVIALLY_ASSIGNABLE:
5090 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5091 case RID_IS_TRIVIALLY_COPYABLE:
5092 case RID_IS_UNION:
5093 return cp_parser_trait_expr (parser, token->keyword);
5095 // C++ concepts
5096 case RID_REQUIRES:
5097 return cp_parser_requires_expression (parser);
5099 /* Objective-C++ expressions. */
5100 case RID_AT_ENCODE:
5101 case RID_AT_PROTOCOL:
5102 case RID_AT_SELECTOR:
5103 return cp_parser_objc_expression (parser);
5105 case RID_TEMPLATE:
5106 if (parser->in_function_body
5107 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5108 == CPP_LESS))
5110 error_at (token->location,
5111 "a template declaration cannot appear at block scope");
5112 cp_parser_skip_to_end_of_block_or_statement (parser);
5113 return error_mark_node;
5115 default:
5116 cp_parser_error (parser, "expected primary-expression");
5117 return error_mark_node;
5120 /* An id-expression can start with either an identifier, a
5121 `::' as the beginning of a qualified-id, or the "operator"
5122 keyword. */
5123 case CPP_NAME:
5124 case CPP_SCOPE:
5125 case CPP_TEMPLATE_ID:
5126 case CPP_NESTED_NAME_SPECIFIER:
5128 id_expression:
5129 cp_expr id_expression;
5130 cp_expr decl;
5131 const char *error_msg;
5132 bool template_p;
5133 bool done;
5134 cp_token *id_expr_token;
5136 /* Parse the id-expression. */
5137 id_expression
5138 = cp_parser_id_expression (parser,
5139 /*template_keyword_p=*/false,
5140 /*check_dependency_p=*/true,
5141 &template_p,
5142 /*declarator_p=*/false,
5143 /*optional_p=*/false);
5144 if (id_expression == error_mark_node)
5145 return error_mark_node;
5146 id_expr_token = token;
5147 token = cp_lexer_peek_token (parser->lexer);
5148 done = (token->type != CPP_OPEN_SQUARE
5149 && token->type != CPP_OPEN_PAREN
5150 && token->type != CPP_DOT
5151 && token->type != CPP_DEREF
5152 && token->type != CPP_PLUS_PLUS
5153 && token->type != CPP_MINUS_MINUS);
5154 /* If we have a template-id, then no further lookup is
5155 required. If the template-id was for a template-class, we
5156 will sometimes have a TYPE_DECL at this point. */
5157 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5158 || TREE_CODE (id_expression) == TYPE_DECL)
5159 decl = id_expression;
5160 /* Look up the name. */
5161 else
5163 tree ambiguous_decls;
5165 /* If we already know that this lookup is ambiguous, then
5166 we've already issued an error message; there's no reason
5167 to check again. */
5168 if (id_expr_token->type == CPP_NAME
5169 && id_expr_token->error_reported)
5171 cp_parser_simulate_error (parser);
5172 return error_mark_node;
5175 decl = cp_parser_lookup_name (parser, id_expression,
5176 none_type,
5177 template_p,
5178 /*is_namespace=*/false,
5179 /*check_dependency=*/true,
5180 &ambiguous_decls,
5181 id_expr_token->location);
5182 /* If the lookup was ambiguous, an error will already have
5183 been issued. */
5184 if (ambiguous_decls)
5185 return error_mark_node;
5187 /* In Objective-C++, we may have an Objective-C 2.0
5188 dot-syntax for classes here. */
5189 if (c_dialect_objc ()
5190 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5191 && TREE_CODE (decl) == TYPE_DECL
5192 && objc_is_class_name (decl))
5194 tree component;
5195 cp_lexer_consume_token (parser->lexer);
5196 component = cp_parser_identifier (parser);
5197 if (component == error_mark_node)
5198 return error_mark_node;
5200 tree result = objc_build_class_component_ref (id_expression,
5201 component);
5202 /* Build a location of the form:
5203 expr.component
5204 ~~~~~^~~~~~~~~
5205 with caret at the start of the component name (at
5206 input_location), ranging from the start of the id_expression
5207 to the end of the component name. */
5208 location_t combined_loc
5209 = make_location (input_location, id_expression.get_start (),
5210 get_finish (input_location));
5211 protected_set_expr_location (result, combined_loc);
5212 return result;
5215 /* In Objective-C++, an instance variable (ivar) may be preferred
5216 to whatever cp_parser_lookup_name() found.
5217 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5218 rest of c-family, we have to do a little extra work to preserve
5219 any location information in cp_expr "decl". Given that
5220 objc_lookup_ivar is implemented in "c-family" and "objc", we
5221 have a trip through the pure "tree" type, rather than cp_expr.
5222 Naively copying it back to "decl" would implicitly give the
5223 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5224 store an EXPR_LOCATION. Hence we only update "decl" (and
5225 hence its location_t) if we get back a different tree node. */
5226 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5227 id_expression);
5228 if (decl_tree != decl.get_value ())
5229 decl = cp_expr (decl_tree);
5231 /* If name lookup gives us a SCOPE_REF, then the
5232 qualifying scope was dependent. */
5233 if (TREE_CODE (decl) == SCOPE_REF)
5235 /* At this point, we do not know if DECL is a valid
5236 integral constant expression. We assume that it is
5237 in fact such an expression, so that code like:
5239 template <int N> struct A {
5240 int a[B<N>::i];
5243 is accepted. At template-instantiation time, we
5244 will check that B<N>::i is actually a constant. */
5245 return decl;
5247 /* Check to see if DECL is a local variable in a context
5248 where that is forbidden. */
5249 if (parser->local_variables_forbidden_p
5250 && local_variable_p (decl))
5252 /* It might be that we only found DECL because we are
5253 trying to be generous with pre-ISO scoping rules.
5254 For example, consider:
5256 int i;
5257 void g() {
5258 for (int i = 0; i < 10; ++i) {}
5259 extern void f(int j = i);
5262 Here, name look up will originally find the out
5263 of scope `i'. We need to issue a warning message,
5264 but then use the global `i'. */
5265 decl = check_for_out_of_scope_variable (decl);
5266 if (local_variable_p (decl))
5268 error_at (id_expr_token->location,
5269 "local variable %qD may not appear in this context",
5270 decl.get_value ());
5271 return error_mark_node;
5276 decl = (finish_id_expression
5277 (id_expression, decl, parser->scope,
5278 idk,
5279 parser->integral_constant_expression_p,
5280 parser->allow_non_integral_constant_expression_p,
5281 &parser->non_integral_constant_expression_p,
5282 template_p, done, address_p,
5283 template_arg_p,
5284 &error_msg,
5285 id_expr_token->location));
5286 if (error_msg)
5287 cp_parser_error (parser, error_msg);
5288 decl.set_location (id_expr_token->location);
5289 return decl;
5292 /* Anything else is an error. */
5293 default:
5294 cp_parser_error (parser, "expected primary-expression");
5295 return error_mark_node;
5299 static inline cp_expr
5300 cp_parser_primary_expression (cp_parser *parser,
5301 bool address_p,
5302 bool cast_p,
5303 bool template_arg_p,
5304 cp_id_kind *idk)
5306 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5307 /*decltype*/false, idk);
5310 /* Parse an id-expression.
5312 id-expression:
5313 unqualified-id
5314 qualified-id
5316 qualified-id:
5317 :: [opt] nested-name-specifier template [opt] unqualified-id
5318 :: identifier
5319 :: operator-function-id
5320 :: template-id
5322 Return a representation of the unqualified portion of the
5323 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5324 a `::' or nested-name-specifier.
5326 Often, if the id-expression was a qualified-id, the caller will
5327 want to make a SCOPE_REF to represent the qualified-id. This
5328 function does not do this in order to avoid wastefully creating
5329 SCOPE_REFs when they are not required.
5331 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5332 `template' keyword.
5334 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5335 uninstantiated templates.
5337 If *TEMPLATE_P is non-NULL, it is set to true iff the
5338 `template' keyword is used to explicitly indicate that the entity
5339 named is a template.
5341 If DECLARATOR_P is true, the id-expression is appearing as part of
5342 a declarator, rather than as part of an expression. */
5344 static cp_expr
5345 cp_parser_id_expression (cp_parser *parser,
5346 bool template_keyword_p,
5347 bool check_dependency_p,
5348 bool *template_p,
5349 bool declarator_p,
5350 bool optional_p)
5352 bool global_scope_p;
5353 bool nested_name_specifier_p;
5355 /* Assume the `template' keyword was not used. */
5356 if (template_p)
5357 *template_p = template_keyword_p;
5359 /* Look for the optional `::' operator. */
5360 global_scope_p
5361 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
5362 != NULL_TREE);
5363 /* Look for the optional nested-name-specifier. */
5364 nested_name_specifier_p
5365 = (cp_parser_nested_name_specifier_opt (parser,
5366 /*typename_keyword_p=*/false,
5367 check_dependency_p,
5368 /*type_p=*/false,
5369 declarator_p)
5370 != NULL_TREE);
5371 /* If there is a nested-name-specifier, then we are looking at
5372 the first qualified-id production. */
5373 if (nested_name_specifier_p)
5375 tree saved_scope;
5376 tree saved_object_scope;
5377 tree saved_qualifying_scope;
5378 tree unqualified_id;
5379 bool is_template;
5381 /* See if the next token is the `template' keyword. */
5382 if (!template_p)
5383 template_p = &is_template;
5384 *template_p = cp_parser_optional_template_keyword (parser);
5385 /* Name lookup we do during the processing of the
5386 unqualified-id might obliterate SCOPE. */
5387 saved_scope = parser->scope;
5388 saved_object_scope = parser->object_scope;
5389 saved_qualifying_scope = parser->qualifying_scope;
5390 /* Process the final unqualified-id. */
5391 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5392 check_dependency_p,
5393 declarator_p,
5394 /*optional_p=*/false);
5395 /* Restore the SAVED_SCOPE for our caller. */
5396 parser->scope = saved_scope;
5397 parser->object_scope = saved_object_scope;
5398 parser->qualifying_scope = saved_qualifying_scope;
5400 return unqualified_id;
5402 /* Otherwise, if we are in global scope, then we are looking at one
5403 of the other qualified-id productions. */
5404 else if (global_scope_p)
5406 cp_token *token;
5407 tree id;
5409 /* Peek at the next token. */
5410 token = cp_lexer_peek_token (parser->lexer);
5412 /* If it's an identifier, and the next token is not a "<", then
5413 we can avoid the template-id case. This is an optimization
5414 for this common case. */
5415 if (token->type == CPP_NAME
5416 && !cp_parser_nth_token_starts_template_argument_list_p
5417 (parser, 2))
5418 return cp_parser_identifier (parser);
5420 cp_parser_parse_tentatively (parser);
5421 /* Try a template-id. */
5422 id = cp_parser_template_id (parser,
5423 /*template_keyword_p=*/false,
5424 /*check_dependency_p=*/true,
5425 none_type,
5426 declarator_p);
5427 /* If that worked, we're done. */
5428 if (cp_parser_parse_definitely (parser))
5429 return id;
5431 /* Peek at the next token. (Changes in the token buffer may
5432 have invalidated the pointer obtained above.) */
5433 token = cp_lexer_peek_token (parser->lexer);
5435 switch (token->type)
5437 case CPP_NAME:
5438 return cp_parser_identifier (parser);
5440 case CPP_KEYWORD:
5441 if (token->keyword == RID_OPERATOR)
5442 return cp_parser_operator_function_id (parser);
5443 /* Fall through. */
5445 default:
5446 cp_parser_error (parser, "expected id-expression");
5447 return error_mark_node;
5450 else
5451 return cp_parser_unqualified_id (parser, template_keyword_p,
5452 /*check_dependency_p=*/true,
5453 declarator_p,
5454 optional_p);
5457 /* Parse an unqualified-id.
5459 unqualified-id:
5460 identifier
5461 operator-function-id
5462 conversion-function-id
5463 ~ class-name
5464 template-id
5466 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5467 keyword, in a construct like `A::template ...'.
5469 Returns a representation of unqualified-id. For the `identifier'
5470 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5471 production a BIT_NOT_EXPR is returned; the operand of the
5472 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5473 other productions, see the documentation accompanying the
5474 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5475 names are looked up in uninstantiated templates. If DECLARATOR_P
5476 is true, the unqualified-id is appearing as part of a declarator,
5477 rather than as part of an expression. */
5479 static cp_expr
5480 cp_parser_unqualified_id (cp_parser* parser,
5481 bool template_keyword_p,
5482 bool check_dependency_p,
5483 bool declarator_p,
5484 bool optional_p)
5486 cp_token *token;
5488 /* Peek at the next token. */
5489 token = cp_lexer_peek_token (parser->lexer);
5491 switch ((int) token->type)
5493 case CPP_NAME:
5495 tree id;
5497 /* We don't know yet whether or not this will be a
5498 template-id. */
5499 cp_parser_parse_tentatively (parser);
5500 /* Try a template-id. */
5501 id = cp_parser_template_id (parser, template_keyword_p,
5502 check_dependency_p,
5503 none_type,
5504 declarator_p);
5505 /* If it worked, we're done. */
5506 if (cp_parser_parse_definitely (parser))
5507 return id;
5508 /* Otherwise, it's an ordinary identifier. */
5509 return cp_parser_identifier (parser);
5512 case CPP_TEMPLATE_ID:
5513 return cp_parser_template_id (parser, template_keyword_p,
5514 check_dependency_p,
5515 none_type,
5516 declarator_p);
5518 case CPP_COMPL:
5520 tree type_decl;
5521 tree qualifying_scope;
5522 tree object_scope;
5523 tree scope;
5524 bool done;
5526 /* Consume the `~' token. */
5527 cp_lexer_consume_token (parser->lexer);
5528 /* Parse the class-name. The standard, as written, seems to
5529 say that:
5531 template <typename T> struct S { ~S (); };
5532 template <typename T> S<T>::~S() {}
5534 is invalid, since `~' must be followed by a class-name, but
5535 `S<T>' is dependent, and so not known to be a class.
5536 That's not right; we need to look in uninstantiated
5537 templates. A further complication arises from:
5539 template <typename T> void f(T t) {
5540 t.T::~T();
5543 Here, it is not possible to look up `T' in the scope of `T'
5544 itself. We must look in both the current scope, and the
5545 scope of the containing complete expression.
5547 Yet another issue is:
5549 struct S {
5550 int S;
5551 ~S();
5554 S::~S() {}
5556 The standard does not seem to say that the `S' in `~S'
5557 should refer to the type `S' and not the data member
5558 `S::S'. */
5560 /* DR 244 says that we look up the name after the "~" in the
5561 same scope as we looked up the qualifying name. That idea
5562 isn't fully worked out; it's more complicated than that. */
5563 scope = parser->scope;
5564 object_scope = parser->object_scope;
5565 qualifying_scope = parser->qualifying_scope;
5567 /* Check for invalid scopes. */
5568 if (scope == error_mark_node)
5570 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5571 cp_lexer_consume_token (parser->lexer);
5572 return error_mark_node;
5574 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5576 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5577 error_at (token->location,
5578 "scope %qT before %<~%> is not a class-name",
5579 scope);
5580 cp_parser_simulate_error (parser);
5581 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5582 cp_lexer_consume_token (parser->lexer);
5583 return error_mark_node;
5585 gcc_assert (!scope || TYPE_P (scope));
5587 /* If the name is of the form "X::~X" it's OK even if X is a
5588 typedef. */
5589 token = cp_lexer_peek_token (parser->lexer);
5590 if (scope
5591 && token->type == CPP_NAME
5592 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5593 != CPP_LESS)
5594 && (token->u.value == TYPE_IDENTIFIER (scope)
5595 || (CLASS_TYPE_P (scope)
5596 && constructor_name_p (token->u.value, scope))))
5598 cp_lexer_consume_token (parser->lexer);
5599 return build_nt (BIT_NOT_EXPR, scope);
5602 /* ~auto means the destructor of whatever the object is. */
5603 if (cp_parser_is_keyword (token, RID_AUTO))
5605 if (cxx_dialect < cxx14)
5606 pedwarn (input_location, 0,
5607 "%<~auto%> only available with "
5608 "-std=c++14 or -std=gnu++14");
5609 cp_lexer_consume_token (parser->lexer);
5610 return build_nt (BIT_NOT_EXPR, make_auto ());
5613 /* If there was an explicit qualification (S::~T), first look
5614 in the scope given by the qualification (i.e., S).
5616 Note: in the calls to cp_parser_class_name below we pass
5617 typename_type so that lookup finds the injected-class-name
5618 rather than the constructor. */
5619 done = false;
5620 type_decl = NULL_TREE;
5621 if (scope)
5623 cp_parser_parse_tentatively (parser);
5624 type_decl = cp_parser_class_name (parser,
5625 /*typename_keyword_p=*/false,
5626 /*template_keyword_p=*/false,
5627 typename_type,
5628 /*check_dependency=*/false,
5629 /*class_head_p=*/false,
5630 declarator_p);
5631 if (cp_parser_parse_definitely (parser))
5632 done = true;
5634 /* In "N::S::~S", look in "N" as well. */
5635 if (!done && scope && qualifying_scope)
5637 cp_parser_parse_tentatively (parser);
5638 parser->scope = qualifying_scope;
5639 parser->object_scope = NULL_TREE;
5640 parser->qualifying_scope = NULL_TREE;
5641 type_decl
5642 = cp_parser_class_name (parser,
5643 /*typename_keyword_p=*/false,
5644 /*template_keyword_p=*/false,
5645 typename_type,
5646 /*check_dependency=*/false,
5647 /*class_head_p=*/false,
5648 declarator_p);
5649 if (cp_parser_parse_definitely (parser))
5650 done = true;
5652 /* In "p->S::~T", look in the scope given by "*p" as well. */
5653 else if (!done && object_scope)
5655 cp_parser_parse_tentatively (parser);
5656 parser->scope = object_scope;
5657 parser->object_scope = NULL_TREE;
5658 parser->qualifying_scope = NULL_TREE;
5659 type_decl
5660 = cp_parser_class_name (parser,
5661 /*typename_keyword_p=*/false,
5662 /*template_keyword_p=*/false,
5663 typename_type,
5664 /*check_dependency=*/false,
5665 /*class_head_p=*/false,
5666 declarator_p);
5667 if (cp_parser_parse_definitely (parser))
5668 done = true;
5670 /* Look in the surrounding context. */
5671 if (!done)
5673 parser->scope = NULL_TREE;
5674 parser->object_scope = NULL_TREE;
5675 parser->qualifying_scope = NULL_TREE;
5676 if (processing_template_decl)
5677 cp_parser_parse_tentatively (parser);
5678 type_decl
5679 = cp_parser_class_name (parser,
5680 /*typename_keyword_p=*/false,
5681 /*template_keyword_p=*/false,
5682 typename_type,
5683 /*check_dependency=*/false,
5684 /*class_head_p=*/false,
5685 declarator_p);
5686 if (processing_template_decl
5687 && ! cp_parser_parse_definitely (parser))
5689 /* We couldn't find a type with this name. If we're parsing
5690 tentatively, fail and try something else. */
5691 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5693 cp_parser_simulate_error (parser);
5694 return error_mark_node;
5696 /* Otherwise, accept it and check for a match at instantiation
5697 time. */
5698 type_decl = cp_parser_identifier (parser);
5699 if (type_decl != error_mark_node)
5700 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5701 return type_decl;
5704 /* If an error occurred, assume that the name of the
5705 destructor is the same as the name of the qualifying
5706 class. That allows us to keep parsing after running
5707 into ill-formed destructor names. */
5708 if (type_decl == error_mark_node && scope)
5709 return build_nt (BIT_NOT_EXPR, scope);
5710 else if (type_decl == error_mark_node)
5711 return error_mark_node;
5713 /* Check that destructor name and scope match. */
5714 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5716 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5717 error_at (token->location,
5718 "declaration of %<~%T%> as member of %qT",
5719 type_decl, scope);
5720 cp_parser_simulate_error (parser);
5721 return error_mark_node;
5724 /* [class.dtor]
5726 A typedef-name that names a class shall not be used as the
5727 identifier in the declarator for a destructor declaration. */
5728 if (declarator_p
5729 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5730 && !DECL_SELF_REFERENCE_P (type_decl)
5731 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5732 error_at (token->location,
5733 "typedef-name %qD used as destructor declarator",
5734 type_decl);
5736 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5739 case CPP_KEYWORD:
5740 if (token->keyword == RID_OPERATOR)
5742 cp_expr id;
5744 /* This could be a template-id, so we try that first. */
5745 cp_parser_parse_tentatively (parser);
5746 /* Try a template-id. */
5747 id = cp_parser_template_id (parser, template_keyword_p,
5748 /*check_dependency_p=*/true,
5749 none_type,
5750 declarator_p);
5751 /* If that worked, we're done. */
5752 if (cp_parser_parse_definitely (parser))
5753 return id;
5754 /* We still don't know whether we're looking at an
5755 operator-function-id or a conversion-function-id. */
5756 cp_parser_parse_tentatively (parser);
5757 /* Try an operator-function-id. */
5758 id = cp_parser_operator_function_id (parser);
5759 /* If that didn't work, try a conversion-function-id. */
5760 if (!cp_parser_parse_definitely (parser))
5761 id = cp_parser_conversion_function_id (parser);
5762 else if (UDLIT_OPER_P (id))
5764 /* 17.6.3.3.5 */
5765 const char *name = UDLIT_OP_SUFFIX (id);
5766 if (name[0] != '_' && !in_system_header_at (input_location)
5767 && declarator_p)
5768 warning (0, "literal operator suffixes not preceded by %<_%>"
5769 " are reserved for future standardization");
5772 return id;
5774 /* Fall through. */
5776 default:
5777 if (optional_p)
5778 return NULL_TREE;
5779 cp_parser_error (parser, "expected unqualified-id");
5780 return error_mark_node;
5784 /* Parse an (optional) nested-name-specifier.
5786 nested-name-specifier: [C++98]
5787 class-or-namespace-name :: nested-name-specifier [opt]
5788 class-or-namespace-name :: template nested-name-specifier [opt]
5790 nested-name-specifier: [C++0x]
5791 type-name ::
5792 namespace-name ::
5793 nested-name-specifier identifier ::
5794 nested-name-specifier template [opt] simple-template-id ::
5796 PARSER->SCOPE should be set appropriately before this function is
5797 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5798 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5799 in name lookups.
5801 Sets PARSER->SCOPE to the class (TYPE) or namespace
5802 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5803 it unchanged if there is no nested-name-specifier. Returns the new
5804 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5806 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5807 part of a declaration and/or decl-specifier. */
5809 static tree
5810 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5811 bool typename_keyword_p,
5812 bool check_dependency_p,
5813 bool type_p,
5814 bool is_declaration)
5816 bool success = false;
5817 cp_token_position start = 0;
5818 cp_token *token;
5820 /* Remember where the nested-name-specifier starts. */
5821 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5823 start = cp_lexer_token_position (parser->lexer, false);
5824 push_deferring_access_checks (dk_deferred);
5827 while (true)
5829 tree new_scope;
5830 tree old_scope;
5831 tree saved_qualifying_scope;
5832 bool template_keyword_p;
5834 /* Spot cases that cannot be the beginning of a
5835 nested-name-specifier. */
5836 token = cp_lexer_peek_token (parser->lexer);
5838 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5839 the already parsed nested-name-specifier. */
5840 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5842 /* Grab the nested-name-specifier and continue the loop. */
5843 cp_parser_pre_parsed_nested_name_specifier (parser);
5844 /* If we originally encountered this nested-name-specifier
5845 with IS_DECLARATION set to false, we will not have
5846 resolved TYPENAME_TYPEs, so we must do so here. */
5847 if (is_declaration
5848 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5850 new_scope = resolve_typename_type (parser->scope,
5851 /*only_current_p=*/false);
5852 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5853 parser->scope = new_scope;
5855 success = true;
5856 continue;
5859 /* Spot cases that cannot be the beginning of a
5860 nested-name-specifier. On the second and subsequent times
5861 through the loop, we look for the `template' keyword. */
5862 if (success && token->keyword == RID_TEMPLATE)
5864 /* A template-id can start a nested-name-specifier. */
5865 else if (token->type == CPP_TEMPLATE_ID)
5867 /* DR 743: decltype can be used in a nested-name-specifier. */
5868 else if (token_is_decltype (token))
5870 else
5872 /* If the next token is not an identifier, then it is
5873 definitely not a type-name or namespace-name. */
5874 if (token->type != CPP_NAME)
5875 break;
5876 /* If the following token is neither a `<' (to begin a
5877 template-id), nor a `::', then we are not looking at a
5878 nested-name-specifier. */
5879 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5881 if (token->type == CPP_COLON
5882 && parser->colon_corrects_to_scope_p
5883 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5885 error_at (token->location,
5886 "found %<:%> in nested-name-specifier, expected %<::%>");
5887 token->type = CPP_SCOPE;
5890 if (token->type != CPP_SCOPE
5891 && !cp_parser_nth_token_starts_template_argument_list_p
5892 (parser, 2))
5893 break;
5896 /* The nested-name-specifier is optional, so we parse
5897 tentatively. */
5898 cp_parser_parse_tentatively (parser);
5900 /* Look for the optional `template' keyword, if this isn't the
5901 first time through the loop. */
5902 if (success)
5903 template_keyword_p = cp_parser_optional_template_keyword (parser);
5904 else
5905 template_keyword_p = false;
5907 /* Save the old scope since the name lookup we are about to do
5908 might destroy it. */
5909 old_scope = parser->scope;
5910 saved_qualifying_scope = parser->qualifying_scope;
5911 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5912 look up names in "X<T>::I" in order to determine that "Y" is
5913 a template. So, if we have a typename at this point, we make
5914 an effort to look through it. */
5915 if (is_declaration
5916 && !typename_keyword_p
5917 && parser->scope
5918 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5919 parser->scope = resolve_typename_type (parser->scope,
5920 /*only_current_p=*/false);
5921 /* Parse the qualifying entity. */
5922 new_scope
5923 = cp_parser_qualifying_entity (parser,
5924 typename_keyword_p,
5925 template_keyword_p,
5926 check_dependency_p,
5927 type_p,
5928 is_declaration);
5929 /* Look for the `::' token. */
5930 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5932 /* If we found what we wanted, we keep going; otherwise, we're
5933 done. */
5934 if (!cp_parser_parse_definitely (parser))
5936 bool error_p = false;
5938 /* Restore the OLD_SCOPE since it was valid before the
5939 failed attempt at finding the last
5940 class-or-namespace-name. */
5941 parser->scope = old_scope;
5942 parser->qualifying_scope = saved_qualifying_scope;
5944 /* If the next token is a decltype, and the one after that is a
5945 `::', then the decltype has failed to resolve to a class or
5946 enumeration type. Give this error even when parsing
5947 tentatively since it can't possibly be valid--and we're going
5948 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5949 won't get another chance.*/
5950 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5951 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5952 == CPP_SCOPE))
5954 token = cp_lexer_consume_token (parser->lexer);
5955 error_at (token->location, "decltype evaluates to %qT, "
5956 "which is not a class or enumeration type",
5957 token->u.tree_check_value->value);
5958 parser->scope = error_mark_node;
5959 error_p = true;
5960 /* As below. */
5961 success = true;
5962 cp_lexer_consume_token (parser->lexer);
5965 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
5966 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
5968 /* If we have a non-type template-id followed by ::, it can't
5969 possibly be valid. */
5970 token = cp_lexer_peek_token (parser->lexer);
5971 tree tid = token->u.tree_check_value->value;
5972 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
5973 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
5975 tree tmpl = NULL_TREE;
5976 if (is_overloaded_fn (tid))
5978 tree fns = get_fns (tid);
5979 if (!OVL_CHAIN (fns))
5980 tmpl = OVL_CURRENT (fns);
5981 error_at (token->location, "function template-id %qD "
5982 "in nested-name-specifier", tid);
5984 else
5986 /* Variable template. */
5987 tmpl = TREE_OPERAND (tid, 0);
5988 gcc_assert (variable_template_p (tmpl));
5989 error_at (token->location, "variable template-id %qD "
5990 "in nested-name-specifier", tid);
5992 if (tmpl)
5993 inform (DECL_SOURCE_LOCATION (tmpl),
5994 "%qD declared here", tmpl);
5996 parser->scope = error_mark_node;
5997 error_p = true;
5998 /* As below. */
5999 success = true;
6000 cp_lexer_consume_token (parser->lexer);
6001 cp_lexer_consume_token (parser->lexer);
6005 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6006 break;
6007 /* If the next token is an identifier, and the one after
6008 that is a `::', then any valid interpretation would have
6009 found a class-or-namespace-name. */
6010 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6011 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6012 == CPP_SCOPE)
6013 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6014 != CPP_COMPL))
6016 token = cp_lexer_consume_token (parser->lexer);
6017 if (!error_p)
6019 if (!token->error_reported)
6021 tree decl;
6022 tree ambiguous_decls;
6024 decl = cp_parser_lookup_name (parser, token->u.value,
6025 none_type,
6026 /*is_template=*/false,
6027 /*is_namespace=*/false,
6028 /*check_dependency=*/true,
6029 &ambiguous_decls,
6030 token->location);
6031 if (TREE_CODE (decl) == TEMPLATE_DECL)
6032 error_at (token->location,
6033 "%qD used without template parameters",
6034 decl);
6035 else if (ambiguous_decls)
6037 // cp_parser_lookup_name has the same diagnostic,
6038 // thus make sure to emit it at most once.
6039 if (cp_parser_uncommitted_to_tentative_parse_p
6040 (parser))
6042 error_at (token->location,
6043 "reference to %qD is ambiguous",
6044 token->u.value);
6045 print_candidates (ambiguous_decls);
6047 decl = error_mark_node;
6049 else
6051 if (cxx_dialect != cxx98)
6052 cp_parser_name_lookup_error
6053 (parser, token->u.value, decl, NLE_NOT_CXX98,
6054 token->location);
6055 else
6056 cp_parser_name_lookup_error
6057 (parser, token->u.value, decl, NLE_CXX98,
6058 token->location);
6061 parser->scope = error_mark_node;
6062 error_p = true;
6063 /* Treat this as a successful nested-name-specifier
6064 due to:
6066 [basic.lookup.qual]
6068 If the name found is not a class-name (clause
6069 _class_) or namespace-name (_namespace.def_), the
6070 program is ill-formed. */
6071 success = true;
6073 cp_lexer_consume_token (parser->lexer);
6075 break;
6077 /* We've found one valid nested-name-specifier. */
6078 success = true;
6079 /* Name lookup always gives us a DECL. */
6080 if (TREE_CODE (new_scope) == TYPE_DECL)
6081 new_scope = TREE_TYPE (new_scope);
6082 /* Uses of "template" must be followed by actual templates. */
6083 if (template_keyword_p
6084 && !(CLASS_TYPE_P (new_scope)
6085 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6086 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6087 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6088 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6089 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6090 == TEMPLATE_ID_EXPR)))
6091 permerror (input_location, TYPE_P (new_scope)
6092 ? G_("%qT is not a template")
6093 : G_("%qD is not a template"),
6094 new_scope);
6095 /* If it is a class scope, try to complete it; we are about to
6096 be looking up names inside the class. */
6097 if (TYPE_P (new_scope)
6098 /* Since checking types for dependency can be expensive,
6099 avoid doing it if the type is already complete. */
6100 && !COMPLETE_TYPE_P (new_scope)
6101 /* Do not try to complete dependent types. */
6102 && !dependent_type_p (new_scope))
6104 new_scope = complete_type (new_scope);
6105 /* If it is a typedef to current class, use the current
6106 class instead, as the typedef won't have any names inside
6107 it yet. */
6108 if (!COMPLETE_TYPE_P (new_scope)
6109 && currently_open_class (new_scope))
6110 new_scope = TYPE_MAIN_VARIANT (new_scope);
6112 /* Make sure we look in the right scope the next time through
6113 the loop. */
6114 parser->scope = new_scope;
6117 /* If parsing tentatively, replace the sequence of tokens that makes
6118 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6119 token. That way, should we re-parse the token stream, we will
6120 not have to repeat the effort required to do the parse, nor will
6121 we issue duplicate error messages. */
6122 if (success && start)
6124 cp_token *token;
6126 token = cp_lexer_token_at (parser->lexer, start);
6127 /* Reset the contents of the START token. */
6128 token->type = CPP_NESTED_NAME_SPECIFIER;
6129 /* Retrieve any deferred checks. Do not pop this access checks yet
6130 so the memory will not be reclaimed during token replacing below. */
6131 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6132 token->u.tree_check_value->value = parser->scope;
6133 token->u.tree_check_value->checks = get_deferred_access_checks ();
6134 token->u.tree_check_value->qualifying_scope =
6135 parser->qualifying_scope;
6136 token->keyword = RID_MAX;
6138 /* Purge all subsequent tokens. */
6139 cp_lexer_purge_tokens_after (parser->lexer, start);
6142 if (start)
6143 pop_to_parent_deferring_access_checks ();
6145 return success ? parser->scope : NULL_TREE;
6148 /* Parse a nested-name-specifier. See
6149 cp_parser_nested_name_specifier_opt for details. This function
6150 behaves identically, except that it will an issue an error if no
6151 nested-name-specifier is present. */
6153 static tree
6154 cp_parser_nested_name_specifier (cp_parser *parser,
6155 bool typename_keyword_p,
6156 bool check_dependency_p,
6157 bool type_p,
6158 bool is_declaration)
6160 tree scope;
6162 /* Look for the nested-name-specifier. */
6163 scope = cp_parser_nested_name_specifier_opt (parser,
6164 typename_keyword_p,
6165 check_dependency_p,
6166 type_p,
6167 is_declaration);
6168 /* If it was not present, issue an error message. */
6169 if (!scope)
6171 cp_parser_error (parser, "expected nested-name-specifier");
6172 parser->scope = NULL_TREE;
6175 return scope;
6178 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6179 this is either a class-name or a namespace-name (which corresponds
6180 to the class-or-namespace-name production in the grammar). For
6181 C++0x, it can also be a type-name that refers to an enumeration
6182 type or a simple-template-id.
6184 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6185 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6186 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6187 TYPE_P is TRUE iff the next name should be taken as a class-name,
6188 even the same name is declared to be another entity in the same
6189 scope.
6191 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6192 specified by the class-or-namespace-name. If neither is found the
6193 ERROR_MARK_NODE is returned. */
6195 static tree
6196 cp_parser_qualifying_entity (cp_parser *parser,
6197 bool typename_keyword_p,
6198 bool template_keyword_p,
6199 bool check_dependency_p,
6200 bool type_p,
6201 bool is_declaration)
6203 tree saved_scope;
6204 tree saved_qualifying_scope;
6205 tree saved_object_scope;
6206 tree scope;
6207 bool only_class_p;
6208 bool successful_parse_p;
6210 /* DR 743: decltype can appear in a nested-name-specifier. */
6211 if (cp_lexer_next_token_is_decltype (parser->lexer))
6213 scope = cp_parser_decltype (parser);
6214 if (TREE_CODE (scope) != ENUMERAL_TYPE
6215 && !MAYBE_CLASS_TYPE_P (scope))
6217 cp_parser_simulate_error (parser);
6218 return error_mark_node;
6220 if (TYPE_NAME (scope))
6221 scope = TYPE_NAME (scope);
6222 return scope;
6225 /* Before we try to parse the class-name, we must save away the
6226 current PARSER->SCOPE since cp_parser_class_name will destroy
6227 it. */
6228 saved_scope = parser->scope;
6229 saved_qualifying_scope = parser->qualifying_scope;
6230 saved_object_scope = parser->object_scope;
6231 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6232 there is no need to look for a namespace-name. */
6233 only_class_p = template_keyword_p
6234 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6235 if (!only_class_p)
6236 cp_parser_parse_tentatively (parser);
6237 scope = cp_parser_class_name (parser,
6238 typename_keyword_p,
6239 template_keyword_p,
6240 type_p ? class_type : none_type,
6241 check_dependency_p,
6242 /*class_head_p=*/false,
6243 is_declaration,
6244 /*enum_ok=*/cxx_dialect > cxx98);
6245 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6246 /* If that didn't work, try for a namespace-name. */
6247 if (!only_class_p && !successful_parse_p)
6249 /* Restore the saved scope. */
6250 parser->scope = saved_scope;
6251 parser->qualifying_scope = saved_qualifying_scope;
6252 parser->object_scope = saved_object_scope;
6253 /* If we are not looking at an identifier followed by the scope
6254 resolution operator, then this is not part of a
6255 nested-name-specifier. (Note that this function is only used
6256 to parse the components of a nested-name-specifier.) */
6257 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6258 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6259 return error_mark_node;
6260 scope = cp_parser_namespace_name (parser);
6263 return scope;
6266 /* Return true if we are looking at a compound-literal, false otherwise. */
6268 static bool
6269 cp_parser_compound_literal_p (cp_parser *parser)
6271 /* Consume the `('. */
6272 cp_lexer_consume_token (parser->lexer);
6274 cp_lexer_save_tokens (parser->lexer);
6276 /* Skip tokens until the next token is a closing parenthesis.
6277 If we find the closing `)', and the next token is a `{', then
6278 we are looking at a compound-literal. */
6279 bool compound_literal_p
6280 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6281 /*consume_paren=*/true)
6282 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6284 /* Roll back the tokens we skipped. */
6285 cp_lexer_rollback_tokens (parser->lexer);
6287 return compound_literal_p;
6290 /* Parse a postfix-expression.
6292 postfix-expression:
6293 primary-expression
6294 postfix-expression [ expression ]
6295 postfix-expression ( expression-list [opt] )
6296 simple-type-specifier ( expression-list [opt] )
6297 typename :: [opt] nested-name-specifier identifier
6298 ( expression-list [opt] )
6299 typename :: [opt] nested-name-specifier template [opt] template-id
6300 ( expression-list [opt] )
6301 postfix-expression . template [opt] id-expression
6302 postfix-expression -> template [opt] id-expression
6303 postfix-expression . pseudo-destructor-name
6304 postfix-expression -> pseudo-destructor-name
6305 postfix-expression ++
6306 postfix-expression --
6307 dynamic_cast < type-id > ( expression )
6308 static_cast < type-id > ( expression )
6309 reinterpret_cast < type-id > ( expression )
6310 const_cast < type-id > ( expression )
6311 typeid ( expression )
6312 typeid ( type-id )
6314 GNU Extension:
6316 postfix-expression:
6317 ( type-id ) { initializer-list , [opt] }
6319 This extension is a GNU version of the C99 compound-literal
6320 construct. (The C99 grammar uses `type-name' instead of `type-id',
6321 but they are essentially the same concept.)
6323 If ADDRESS_P is true, the postfix expression is the operand of the
6324 `&' operator. CAST_P is true if this expression is the target of a
6325 cast.
6327 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6328 class member access expressions [expr.ref].
6330 Returns a representation of the expression. */
6332 static cp_expr
6333 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6334 bool member_access_only_p, bool decltype_p,
6335 cp_id_kind * pidk_return)
6337 cp_token *token;
6338 location_t loc;
6339 enum rid keyword;
6340 cp_id_kind idk = CP_ID_KIND_NONE;
6341 cp_expr postfix_expression = NULL_TREE;
6342 bool is_member_access = false;
6343 int saved_in_statement = -1;
6345 /* Peek at the next token. */
6346 token = cp_lexer_peek_token (parser->lexer);
6347 loc = token->location;
6348 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6350 /* Some of the productions are determined by keywords. */
6351 keyword = token->keyword;
6352 switch (keyword)
6354 case RID_DYNCAST:
6355 case RID_STATCAST:
6356 case RID_REINTCAST:
6357 case RID_CONSTCAST:
6359 tree type;
6360 cp_expr expression;
6361 const char *saved_message;
6362 bool saved_in_type_id_in_expr_p;
6364 /* All of these can be handled in the same way from the point
6365 of view of parsing. Begin by consuming the token
6366 identifying the cast. */
6367 cp_lexer_consume_token (parser->lexer);
6369 /* New types cannot be defined in the cast. */
6370 saved_message = parser->type_definition_forbidden_message;
6371 parser->type_definition_forbidden_message
6372 = G_("types may not be defined in casts");
6374 /* Look for the opening `<'. */
6375 cp_parser_require (parser, CPP_LESS, RT_LESS);
6376 /* Parse the type to which we are casting. */
6377 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6378 parser->in_type_id_in_expr_p = true;
6379 type = cp_parser_type_id (parser);
6380 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6381 /* Look for the closing `>'. */
6382 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6383 /* Restore the old message. */
6384 parser->type_definition_forbidden_message = saved_message;
6386 bool saved_greater_than_is_operator_p
6387 = parser->greater_than_is_operator_p;
6388 parser->greater_than_is_operator_p = true;
6390 /* And the expression which is being cast. */
6391 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6392 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6393 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6394 RT_CLOSE_PAREN);
6395 location_t end_loc = close_paren ?
6396 close_paren->location : UNKNOWN_LOCATION;
6398 parser->greater_than_is_operator_p
6399 = saved_greater_than_is_operator_p;
6401 /* Only type conversions to integral or enumeration types
6402 can be used in constant-expressions. */
6403 if (!cast_valid_in_integral_constant_expression_p (type)
6404 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6405 return error_mark_node;
6407 switch (keyword)
6409 case RID_DYNCAST:
6410 postfix_expression
6411 = build_dynamic_cast (type, expression, tf_warning_or_error);
6412 break;
6413 case RID_STATCAST:
6414 postfix_expression
6415 = build_static_cast (type, expression, tf_warning_or_error);
6416 break;
6417 case RID_REINTCAST:
6418 postfix_expression
6419 = build_reinterpret_cast (type, expression,
6420 tf_warning_or_error);
6421 break;
6422 case RID_CONSTCAST:
6423 postfix_expression
6424 = build_const_cast (type, expression, tf_warning_or_error);
6425 break;
6426 default:
6427 gcc_unreachable ();
6430 /* Construct a location e.g. :
6431 reinterpret_cast <int *> (expr)
6432 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6433 ranging from the start of the "*_cast" token to the final closing
6434 paren, with the caret at the start. */
6435 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6436 postfix_expression.set_location (cp_cast_loc);
6438 break;
6440 case RID_TYPEID:
6442 tree type;
6443 const char *saved_message;
6444 bool saved_in_type_id_in_expr_p;
6446 /* Consume the `typeid' token. */
6447 cp_lexer_consume_token (parser->lexer);
6448 /* Look for the `(' token. */
6449 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6450 /* Types cannot be defined in a `typeid' expression. */
6451 saved_message = parser->type_definition_forbidden_message;
6452 parser->type_definition_forbidden_message
6453 = G_("types may not be defined in a %<typeid%> expression");
6454 /* We can't be sure yet whether we're looking at a type-id or an
6455 expression. */
6456 cp_parser_parse_tentatively (parser);
6457 /* Try a type-id first. */
6458 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6459 parser->in_type_id_in_expr_p = true;
6460 type = cp_parser_type_id (parser);
6461 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6462 /* Look for the `)' token. Otherwise, we can't be sure that
6463 we're not looking at an expression: consider `typeid (int
6464 (3))', for example. */
6465 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6466 /* If all went well, simply lookup the type-id. */
6467 if (cp_parser_parse_definitely (parser))
6468 postfix_expression = get_typeid (type, tf_warning_or_error);
6469 /* Otherwise, fall back to the expression variant. */
6470 else
6472 tree expression;
6474 /* Look for an expression. */
6475 expression = cp_parser_expression (parser, & idk);
6476 /* Compute its typeid. */
6477 postfix_expression = build_typeid (expression, tf_warning_or_error);
6478 /* Look for the `)' token. */
6479 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6481 /* Restore the saved message. */
6482 parser->type_definition_forbidden_message = saved_message;
6483 /* `typeid' may not appear in an integral constant expression. */
6484 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6485 return error_mark_node;
6487 break;
6489 case RID_TYPENAME:
6491 tree type;
6492 /* The syntax permitted here is the same permitted for an
6493 elaborated-type-specifier. */
6494 ++parser->prevent_constrained_type_specifiers;
6495 type = cp_parser_elaborated_type_specifier (parser,
6496 /*is_friend=*/false,
6497 /*is_declaration=*/false);
6498 --parser->prevent_constrained_type_specifiers;
6499 postfix_expression = cp_parser_functional_cast (parser, type);
6501 break;
6503 case RID_CILK_SPAWN:
6505 location_t cilk_spawn_loc
6506 = cp_lexer_peek_token (parser->lexer)->location;
6507 cp_lexer_consume_token (parser->lexer);
6508 token = cp_lexer_peek_token (parser->lexer);
6509 if (token->type == CPP_SEMICOLON)
6511 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6512 "an expression");
6513 postfix_expression = error_mark_node;
6514 break;
6516 else if (!current_function_decl)
6518 error_at (token->location, "%<_Cilk_spawn%> may only be used "
6519 "inside a function");
6520 postfix_expression = error_mark_node;
6521 break;
6523 else
6525 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6526 saved_in_statement = parser->in_statement;
6527 parser->in_statement |= IN_CILK_SPAWN;
6529 cfun->calls_cilk_spawn = 1;
6530 postfix_expression =
6531 cp_parser_postfix_expression (parser, false, false,
6532 false, false, &idk);
6533 if (!flag_cilkplus)
6535 error_at (token->location, "-fcilkplus must be enabled to use"
6536 " %<_Cilk_spawn%>");
6537 cfun->calls_cilk_spawn = 0;
6539 else if (saved_in_statement & IN_CILK_SPAWN)
6541 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6542 "are not permitted");
6543 postfix_expression = error_mark_node;
6544 cfun->calls_cilk_spawn = 0;
6546 else
6548 location_t loc = postfix_expression.get_location ();
6549 postfix_expression = build_cilk_spawn (token->location,
6550 postfix_expression);
6551 /* Build a location of the form:
6552 _Cilk_spawn expr
6553 ~~~~~~~~~~~~^~~~
6554 with caret at the expr, ranging from the start of the
6555 _Cilk_spawn token to the end of the expression. */
6556 location_t combined_loc =
6557 make_location (loc, cilk_spawn_loc, get_finish (loc));
6558 postfix_expression.set_location (combined_loc);
6559 if (postfix_expression != error_mark_node)
6560 SET_EXPR_LOCATION (postfix_expression, input_location);
6561 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6563 break;
6566 case RID_BUILTIN_SHUFFLE:
6568 vec<tree, va_gc> *vec;
6569 unsigned int i;
6570 tree p;
6572 cp_lexer_consume_token (parser->lexer);
6573 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6574 /*cast_p=*/false, /*allow_expansion_p=*/true,
6575 /*non_constant_p=*/NULL);
6576 if (vec == NULL)
6577 return error_mark_node;
6579 FOR_EACH_VEC_ELT (*vec, i, p)
6580 mark_exp_read (p);
6582 if (vec->length () == 2)
6583 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
6584 tf_warning_or_error);
6585 else if (vec->length () == 3)
6586 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6587 tf_warning_or_error);
6588 else
6590 error_at (loc, "wrong number of arguments to "
6591 "%<__builtin_shuffle%>");
6592 return error_mark_node;
6594 break;
6597 default:
6599 tree type;
6601 /* If the next thing is a simple-type-specifier, we may be
6602 looking at a functional cast. We could also be looking at
6603 an id-expression. So, we try the functional cast, and if
6604 that doesn't work we fall back to the primary-expression. */
6605 cp_parser_parse_tentatively (parser);
6606 /* Look for the simple-type-specifier. */
6607 ++parser->prevent_constrained_type_specifiers;
6608 type = cp_parser_simple_type_specifier (parser,
6609 /*decl_specs=*/NULL,
6610 CP_PARSER_FLAGS_NONE);
6611 --parser->prevent_constrained_type_specifiers;
6612 /* Parse the cast itself. */
6613 if (!cp_parser_error_occurred (parser))
6614 postfix_expression
6615 = cp_parser_functional_cast (parser, type);
6616 /* If that worked, we're done. */
6617 if (cp_parser_parse_definitely (parser))
6618 break;
6620 /* If the functional-cast didn't work out, try a
6621 compound-literal. */
6622 if (cp_parser_allow_gnu_extensions_p (parser)
6623 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6625 cp_expr initializer = NULL_TREE;
6627 cp_parser_parse_tentatively (parser);
6629 /* Avoid calling cp_parser_type_id pointlessly, see comment
6630 in cp_parser_cast_expression about c++/29234. */
6631 if (!cp_parser_compound_literal_p (parser))
6632 cp_parser_simulate_error (parser);
6633 else
6635 /* Parse the type. */
6636 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6637 parser->in_type_id_in_expr_p = true;
6638 type = cp_parser_type_id (parser);
6639 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6640 /* Look for the `)'. */
6641 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6644 /* If things aren't going well, there's no need to
6645 keep going. */
6646 if (!cp_parser_error_occurred (parser))
6648 bool non_constant_p;
6649 /* Parse the brace-enclosed initializer list. */
6650 initializer = cp_parser_braced_list (parser,
6651 &non_constant_p);
6653 /* If that worked, we're definitely looking at a
6654 compound-literal expression. */
6655 if (cp_parser_parse_definitely (parser))
6657 /* Warn the user that a compound literal is not
6658 allowed in standard C++. */
6659 pedwarn (input_location, OPT_Wpedantic,
6660 "ISO C++ forbids compound-literals");
6661 /* For simplicity, we disallow compound literals in
6662 constant-expressions. We could
6663 allow compound literals of integer type, whose
6664 initializer was a constant, in constant
6665 expressions. Permitting that usage, as a further
6666 extension, would not change the meaning of any
6667 currently accepted programs. (Of course, as
6668 compound literals are not part of ISO C++, the
6669 standard has nothing to say.) */
6670 if (cp_parser_non_integral_constant_expression (parser,
6671 NIC_NCC))
6673 postfix_expression = error_mark_node;
6674 break;
6676 /* Form the representation of the compound-literal. */
6677 postfix_expression
6678 = finish_compound_literal (type, initializer,
6679 tf_warning_or_error);
6680 postfix_expression.set_location (initializer.get_location ());
6681 break;
6685 /* It must be a primary-expression. */
6686 postfix_expression
6687 = cp_parser_primary_expression (parser, address_p, cast_p,
6688 /*template_arg_p=*/false,
6689 decltype_p,
6690 &idk);
6692 break;
6695 /* Note that we don't need to worry about calling build_cplus_new on a
6696 class-valued CALL_EXPR in decltype when it isn't the end of the
6697 postfix-expression; unary_complex_lvalue will take care of that for
6698 all these cases. */
6700 /* Keep looping until the postfix-expression is complete. */
6701 while (true)
6703 if (idk == CP_ID_KIND_UNQUALIFIED
6704 && identifier_p (postfix_expression)
6705 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6706 /* It is not a Koenig lookup function call. */
6707 postfix_expression
6708 = unqualified_name_lookup_error (postfix_expression);
6710 /* Peek at the next token. */
6711 token = cp_lexer_peek_token (parser->lexer);
6713 switch (token->type)
6715 case CPP_OPEN_SQUARE:
6716 if (cp_next_tokens_can_be_std_attribute_p (parser))
6718 cp_parser_error (parser,
6719 "two consecutive %<[%> shall "
6720 "only introduce an attribute");
6721 return error_mark_node;
6723 postfix_expression
6724 = cp_parser_postfix_open_square_expression (parser,
6725 postfix_expression,
6726 false,
6727 decltype_p);
6728 postfix_expression.set_range (start_loc,
6729 postfix_expression.get_location ());
6731 idk = CP_ID_KIND_NONE;
6732 is_member_access = false;
6733 break;
6735 case CPP_OPEN_PAREN:
6736 /* postfix-expression ( expression-list [opt] ) */
6738 bool koenig_p;
6739 bool is_builtin_constant_p;
6740 bool saved_integral_constant_expression_p = false;
6741 bool saved_non_integral_constant_expression_p = false;
6742 tsubst_flags_t complain = complain_flags (decltype_p);
6743 vec<tree, va_gc> *args;
6744 location_t close_paren_loc = UNKNOWN_LOCATION;
6746 is_member_access = false;
6748 is_builtin_constant_p
6749 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6750 if (is_builtin_constant_p)
6752 /* The whole point of __builtin_constant_p is to allow
6753 non-constant expressions to appear as arguments. */
6754 saved_integral_constant_expression_p
6755 = parser->integral_constant_expression_p;
6756 saved_non_integral_constant_expression_p
6757 = parser->non_integral_constant_expression_p;
6758 parser->integral_constant_expression_p = false;
6760 args = (cp_parser_parenthesized_expression_list
6761 (parser, non_attr,
6762 /*cast_p=*/false, /*allow_expansion_p=*/true,
6763 /*non_constant_p=*/NULL,
6764 /*close_paren_loc=*/&close_paren_loc));
6765 if (is_builtin_constant_p)
6767 parser->integral_constant_expression_p
6768 = saved_integral_constant_expression_p;
6769 parser->non_integral_constant_expression_p
6770 = saved_non_integral_constant_expression_p;
6773 if (args == NULL)
6775 postfix_expression = error_mark_node;
6776 break;
6779 /* Function calls are not permitted in
6780 constant-expressions. */
6781 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6782 && cp_parser_non_integral_constant_expression (parser,
6783 NIC_FUNC_CALL))
6785 postfix_expression = error_mark_node;
6786 release_tree_vector (args);
6787 break;
6790 koenig_p = false;
6791 if (idk == CP_ID_KIND_UNQUALIFIED
6792 || idk == CP_ID_KIND_TEMPLATE_ID)
6794 if (identifier_p (postfix_expression))
6796 if (!args->is_empty ())
6798 koenig_p = true;
6799 if (!any_type_dependent_arguments_p (args))
6800 postfix_expression
6801 = perform_koenig_lookup (postfix_expression, args,
6802 complain);
6804 else
6805 postfix_expression
6806 = unqualified_fn_lookup_error (postfix_expression);
6808 /* We do not perform argument-dependent lookup if
6809 normal lookup finds a non-function, in accordance
6810 with the expected resolution of DR 218. */
6811 else if (!args->is_empty ()
6812 && is_overloaded_fn (postfix_expression))
6814 tree fn = get_first_fn (postfix_expression);
6815 fn = STRIP_TEMPLATE (fn);
6817 /* Do not do argument dependent lookup if regular
6818 lookup finds a member function or a block-scope
6819 function declaration. [basic.lookup.argdep]/3 */
6820 if (!DECL_FUNCTION_MEMBER_P (fn)
6821 && !DECL_LOCAL_FUNCTION_P (fn))
6823 koenig_p = true;
6824 if (!any_type_dependent_arguments_p (args))
6825 postfix_expression
6826 = perform_koenig_lookup (postfix_expression, args,
6827 complain);
6832 if (warn_memset_transposed_args)
6834 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6835 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6836 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6837 && vec_safe_length (args) == 3
6838 && TREE_CODE ((*args)[2]) == INTEGER_CST
6839 && integer_zerop ((*args)[2])
6840 && !(TREE_CODE ((*args)[1]) == INTEGER_CST
6841 && integer_zerop ((*args)[1])))
6842 warning (OPT_Wmemset_transposed_args,
6843 "%<memset%> used with constant zero length "
6844 "parameter; this could be due to transposed "
6845 "parameters");
6848 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6850 tree instance = TREE_OPERAND (postfix_expression, 0);
6851 tree fn = TREE_OPERAND (postfix_expression, 1);
6853 if (processing_template_decl
6854 && (type_dependent_expression_p (instance)
6855 || (!BASELINK_P (fn)
6856 && TREE_CODE (fn) != FIELD_DECL)
6857 || type_dependent_expression_p (fn)
6858 || any_type_dependent_arguments_p (args)))
6860 postfix_expression
6861 = build_nt_call_vec (postfix_expression, args);
6862 release_tree_vector (args);
6863 break;
6866 if (BASELINK_P (fn))
6868 postfix_expression
6869 = (build_new_method_call
6870 (instance, fn, &args, NULL_TREE,
6871 (idk == CP_ID_KIND_QUALIFIED
6872 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6873 : LOOKUP_NORMAL),
6874 /*fn_p=*/NULL,
6875 complain));
6877 else
6878 postfix_expression
6879 = finish_call_expr (postfix_expression, &args,
6880 /*disallow_virtual=*/false,
6881 /*koenig_p=*/false,
6882 complain);
6884 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6885 || TREE_CODE (postfix_expression) == MEMBER_REF
6886 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6887 postfix_expression = (build_offset_ref_call_from_tree
6888 (postfix_expression, &args,
6889 complain));
6890 else if (idk == CP_ID_KIND_QUALIFIED)
6891 /* A call to a static class member, or a namespace-scope
6892 function. */
6893 postfix_expression
6894 = finish_call_expr (postfix_expression, &args,
6895 /*disallow_virtual=*/true,
6896 koenig_p,
6897 complain);
6898 else
6899 /* All other function calls. */
6900 postfix_expression
6901 = finish_call_expr (postfix_expression, &args,
6902 /*disallow_virtual=*/false,
6903 koenig_p,
6904 complain);
6906 if (close_paren_loc != UNKNOWN_LOCATION)
6908 location_t combined_loc = make_location (token->location,
6909 start_loc,
6910 close_paren_loc);
6911 postfix_expression.set_location (combined_loc);
6914 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6915 idk = CP_ID_KIND_NONE;
6917 release_tree_vector (args);
6919 break;
6921 case CPP_DOT:
6922 case CPP_DEREF:
6923 /* postfix-expression . template [opt] id-expression
6924 postfix-expression . pseudo-destructor-name
6925 postfix-expression -> template [opt] id-expression
6926 postfix-expression -> pseudo-destructor-name */
6928 /* Consume the `.' or `->' operator. */
6929 cp_lexer_consume_token (parser->lexer);
6931 postfix_expression
6932 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6933 postfix_expression,
6934 false, &idk, loc);
6936 is_member_access = true;
6937 break;
6939 case CPP_PLUS_PLUS:
6940 /* postfix-expression ++ */
6941 /* Consume the `++' token. */
6942 cp_lexer_consume_token (parser->lexer);
6943 /* Generate a representation for the complete expression. */
6944 postfix_expression
6945 = finish_increment_expr (postfix_expression,
6946 POSTINCREMENT_EXPR);
6947 /* Increments may not appear in constant-expressions. */
6948 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6949 postfix_expression = error_mark_node;
6950 idk = CP_ID_KIND_NONE;
6951 is_member_access = false;
6952 break;
6954 case CPP_MINUS_MINUS:
6955 /* postfix-expression -- */
6956 /* Consume the `--' token. */
6957 cp_lexer_consume_token (parser->lexer);
6958 /* Generate a representation for the complete expression. */
6959 postfix_expression
6960 = finish_increment_expr (postfix_expression,
6961 POSTDECREMENT_EXPR);
6962 /* Decrements may not appear in constant-expressions. */
6963 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6964 postfix_expression = error_mark_node;
6965 idk = CP_ID_KIND_NONE;
6966 is_member_access = false;
6967 break;
6969 default:
6970 if (pidk_return != NULL)
6971 * pidk_return = idk;
6972 if (member_access_only_p)
6973 return is_member_access
6974 ? postfix_expression
6975 : cp_expr (error_mark_node);
6976 else
6977 return postfix_expression;
6981 /* We should never get here. */
6982 gcc_unreachable ();
6983 return error_mark_node;
6986 /* This function parses Cilk Plus array notations. If a normal array expr. is
6987 parsed then the array index is passed back to the caller through *INIT_INDEX
6988 and the function returns a NULL_TREE. If array notation expr. is parsed,
6989 then *INIT_INDEX is ignored by the caller and the function returns
6990 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6991 error_mark_node. */
6993 static tree
6994 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6995 tree array_value)
6997 cp_token *token = NULL;
6998 tree length_index, stride = NULL_TREE, value_tree, array_type;
6999 if (!array_value || array_value == error_mark_node)
7001 cp_parser_skip_to_end_of_statement (parser);
7002 return error_mark_node;
7005 array_type = TREE_TYPE (array_value);
7007 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
7008 parser->colon_corrects_to_scope_p = false;
7009 token = cp_lexer_peek_token (parser->lexer);
7011 if (!token)
7013 cp_parser_error (parser, "expected %<:%> or numeral");
7014 return error_mark_node;
7016 else if (token->type == CPP_COLON)
7018 /* Consume the ':'. */
7019 cp_lexer_consume_token (parser->lexer);
7021 /* If we are here, then we have a case like this A[:]. */
7022 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
7024 cp_parser_error (parser, "expected %<]%>");
7025 cp_parser_skip_to_end_of_statement (parser);
7026 return error_mark_node;
7028 *init_index = NULL_TREE;
7029 stride = NULL_TREE;
7030 length_index = NULL_TREE;
7032 else
7034 /* If we are here, then there are three valid possibilities:
7035 1. ARRAY [ EXP ]
7036 2. ARRAY [ EXP : EXP ]
7037 3. ARRAY [ EXP : EXP : EXP ] */
7039 *init_index = cp_parser_expression (parser);
7040 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
7042 /* This indicates that we have a normal array expression. */
7043 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7044 return NULL_TREE;
7047 /* Consume the ':'. */
7048 cp_lexer_consume_token (parser->lexer);
7049 length_index = cp_parser_expression (parser);
7050 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7052 cp_lexer_consume_token (parser->lexer);
7053 stride = cp_parser_expression (parser);
7056 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7058 if (*init_index == error_mark_node || length_index == error_mark_node
7059 || stride == error_mark_node || array_type == error_mark_node)
7061 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
7062 cp_lexer_consume_token (parser->lexer);
7063 return error_mark_node;
7065 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7067 value_tree = build_array_notation_ref (loc, array_value, *init_index,
7068 length_index, stride, array_type);
7069 return value_tree;
7072 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7073 by cp_parser_builtin_offsetof. We're looking for
7075 postfix-expression [ expression ]
7076 postfix-expression [ braced-init-list ] (C++11)
7078 FOR_OFFSETOF is set if we're being called in that context, which
7079 changes how we deal with integer constant expressions. */
7081 static tree
7082 cp_parser_postfix_open_square_expression (cp_parser *parser,
7083 tree postfix_expression,
7084 bool for_offsetof,
7085 bool decltype_p)
7087 tree index = NULL_TREE;
7088 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7089 bool saved_greater_than_is_operator_p;
7091 /* Consume the `[' token. */
7092 cp_lexer_consume_token (parser->lexer);
7094 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7095 parser->greater_than_is_operator_p = true;
7097 /* Parse the index expression. */
7098 /* ??? For offsetof, there is a question of what to allow here. If
7099 offsetof is not being used in an integral constant expression context,
7100 then we *could* get the right answer by computing the value at runtime.
7101 If we are in an integral constant expression context, then we might
7102 could accept any constant expression; hard to say without analysis.
7103 Rather than open the barn door too wide right away, allow only integer
7104 constant expressions here. */
7105 if (for_offsetof)
7106 index = cp_parser_constant_expression (parser);
7107 else
7109 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7111 bool expr_nonconst_p;
7112 cp_lexer_set_source_position (parser->lexer);
7113 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7114 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7115 if (flag_cilkplus
7116 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7118 error_at (cp_lexer_peek_token (parser->lexer)->location,
7119 "braced list index is not allowed with array "
7120 "notation");
7121 cp_parser_skip_to_end_of_statement (parser);
7122 return error_mark_node;
7125 else if (flag_cilkplus)
7127 /* Here are have these two options:
7128 ARRAY[EXP : EXP] - Array notation expr with default
7129 stride of 1.
7130 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7131 stride. */
7132 tree an_exp = cp_parser_array_notation (loc, parser, &index,
7133 postfix_expression);
7134 if (an_exp)
7135 return an_exp;
7137 else
7138 index = cp_parser_expression (parser);
7141 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7143 /* Look for the closing `]'. */
7144 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7146 /* Build the ARRAY_REF. */
7147 postfix_expression = grok_array_decl (loc, postfix_expression,
7148 index, decltype_p);
7150 /* When not doing offsetof, array references are not permitted in
7151 constant-expressions. */
7152 if (!for_offsetof
7153 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7154 postfix_expression = error_mark_node;
7156 return postfix_expression;
7159 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7160 by cp_parser_builtin_offsetof. We're looking for
7162 postfix-expression . template [opt] id-expression
7163 postfix-expression . pseudo-destructor-name
7164 postfix-expression -> template [opt] id-expression
7165 postfix-expression -> pseudo-destructor-name
7167 FOR_OFFSETOF is set if we're being called in that context. That sorta
7168 limits what of the above we'll actually accept, but nevermind.
7169 TOKEN_TYPE is the "." or "->" token, which will already have been
7170 removed from the stream. */
7172 static tree
7173 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7174 enum cpp_ttype token_type,
7175 cp_expr postfix_expression,
7176 bool for_offsetof, cp_id_kind *idk,
7177 location_t location)
7179 tree name;
7180 bool dependent_p;
7181 bool pseudo_destructor_p;
7182 tree scope = NULL_TREE;
7183 location_t start_loc = postfix_expression.get_start ();
7185 /* If this is a `->' operator, dereference the pointer. */
7186 if (token_type == CPP_DEREF)
7187 postfix_expression = build_x_arrow (location, postfix_expression,
7188 tf_warning_or_error);
7189 /* Check to see whether or not the expression is type-dependent. */
7190 dependent_p = type_dependent_expression_p (postfix_expression);
7191 /* The identifier following the `->' or `.' is not qualified. */
7192 parser->scope = NULL_TREE;
7193 parser->qualifying_scope = NULL_TREE;
7194 parser->object_scope = NULL_TREE;
7195 *idk = CP_ID_KIND_NONE;
7197 /* Enter the scope corresponding to the type of the object
7198 given by the POSTFIX_EXPRESSION. */
7199 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
7201 scope = TREE_TYPE (postfix_expression);
7202 /* According to the standard, no expression should ever have
7203 reference type. Unfortunately, we do not currently match
7204 the standard in this respect in that our internal representation
7205 of an expression may have reference type even when the standard
7206 says it does not. Therefore, we have to manually obtain the
7207 underlying type here. */
7208 scope = non_reference (scope);
7209 /* The type of the POSTFIX_EXPRESSION must be complete. */
7210 if (scope == unknown_type_node)
7212 error_at (location, "%qE does not have class type",
7213 postfix_expression.get_value ());
7214 scope = NULL_TREE;
7216 /* Unlike the object expression in other contexts, *this is not
7217 required to be of complete type for purposes of class member
7218 access (5.2.5) outside the member function body. */
7219 else if (postfix_expression != current_class_ref
7220 && !(processing_template_decl && scope == current_class_type))
7221 scope = complete_type_or_else (scope, NULL_TREE);
7222 /* Let the name lookup machinery know that we are processing a
7223 class member access expression. */
7224 parser->context->object_type = scope;
7225 /* If something went wrong, we want to be able to discern that case,
7226 as opposed to the case where there was no SCOPE due to the type
7227 of expression being dependent. */
7228 if (!scope)
7229 scope = error_mark_node;
7230 /* If the SCOPE was erroneous, make the various semantic analysis
7231 functions exit quickly -- and without issuing additional error
7232 messages. */
7233 if (scope == error_mark_node)
7234 postfix_expression = error_mark_node;
7236 else
7237 /* Tell cp_parser_lookup_name that there was an object, even though it's
7238 type-dependent. */
7239 parser->context->object_type = unknown_type_node;
7241 /* Assume this expression is not a pseudo-destructor access. */
7242 pseudo_destructor_p = false;
7244 /* If the SCOPE is a scalar type, then, if this is a valid program,
7245 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7246 is type dependent, it can be pseudo-destructor-name or something else.
7247 Try to parse it as pseudo-destructor-name first. */
7248 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7250 tree s;
7251 tree type;
7253 cp_parser_parse_tentatively (parser);
7254 /* Parse the pseudo-destructor-name. */
7255 s = NULL_TREE;
7256 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7257 &s, &type);
7258 if (dependent_p
7259 && (cp_parser_error_occurred (parser)
7260 || !SCALAR_TYPE_P (type)))
7261 cp_parser_abort_tentative_parse (parser);
7262 else if (cp_parser_parse_definitely (parser))
7264 pseudo_destructor_p = true;
7265 postfix_expression
7266 = finish_pseudo_destructor_expr (postfix_expression,
7267 s, type, location);
7271 if (!pseudo_destructor_p)
7273 /* If the SCOPE is not a scalar type, we are looking at an
7274 ordinary class member access expression, rather than a
7275 pseudo-destructor-name. */
7276 bool template_p;
7277 cp_token *token = cp_lexer_peek_token (parser->lexer);
7278 /* Parse the id-expression. */
7279 name = (cp_parser_id_expression
7280 (parser,
7281 cp_parser_optional_template_keyword (parser),
7282 /*check_dependency_p=*/true,
7283 &template_p,
7284 /*declarator_p=*/false,
7285 /*optional_p=*/false));
7286 /* In general, build a SCOPE_REF if the member name is qualified.
7287 However, if the name was not dependent and has already been
7288 resolved; there is no need to build the SCOPE_REF. For example;
7290 struct X { void f(); };
7291 template <typename T> void f(T* t) { t->X::f(); }
7293 Even though "t" is dependent, "X::f" is not and has been resolved
7294 to a BASELINK; there is no need to include scope information. */
7296 /* But we do need to remember that there was an explicit scope for
7297 virtual function calls. */
7298 if (parser->scope)
7299 *idk = CP_ID_KIND_QUALIFIED;
7301 /* If the name is a template-id that names a type, we will get a
7302 TYPE_DECL here. That is invalid code. */
7303 if (TREE_CODE (name) == TYPE_DECL)
7305 error_at (token->location, "invalid use of %qD", name);
7306 postfix_expression = error_mark_node;
7308 else
7310 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7312 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7314 error_at (token->location, "%<%D::%D%> is not a class member",
7315 parser->scope, name);
7316 postfix_expression = error_mark_node;
7318 else
7319 name = build_qualified_name (/*type=*/NULL_TREE,
7320 parser->scope,
7321 name,
7322 template_p);
7323 parser->scope = NULL_TREE;
7324 parser->qualifying_scope = NULL_TREE;
7325 parser->object_scope = NULL_TREE;
7327 if (parser->scope && name && BASELINK_P (name))
7328 adjust_result_of_qualified_name_lookup
7329 (name, parser->scope, scope);
7330 postfix_expression
7331 = finish_class_member_access_expr (postfix_expression, name,
7332 template_p,
7333 tf_warning_or_error);
7334 /* Build a location e.g.:
7335 ptr->access_expr
7336 ~~~^~~~~~~~~~~~~
7337 where the caret is at the deref token, ranging from
7338 the start of postfix_expression to the end of the access expr. */
7339 location_t end_loc
7340 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7341 location_t combined_loc
7342 = make_location (input_location, start_loc, end_loc);
7343 protected_set_expr_location (postfix_expression, combined_loc);
7347 /* We no longer need to look up names in the scope of the object on
7348 the left-hand side of the `.' or `->' operator. */
7349 parser->context->object_type = NULL_TREE;
7351 /* Outside of offsetof, these operators may not appear in
7352 constant-expressions. */
7353 if (!for_offsetof
7354 && (cp_parser_non_integral_constant_expression
7355 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7356 postfix_expression = error_mark_node;
7358 return postfix_expression;
7361 /* Parse a parenthesized expression-list.
7363 expression-list:
7364 assignment-expression
7365 expression-list, assignment-expression
7367 attribute-list:
7368 expression-list
7369 identifier
7370 identifier, expression-list
7372 CAST_P is true if this expression is the target of a cast.
7374 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7375 argument pack.
7377 Returns a vector of trees. Each element is a representation of an
7378 assignment-expression. NULL is returned if the ( and or ) are
7379 missing. An empty, but allocated, vector is returned on no
7380 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7381 if we are parsing an attribute list for an attribute that wants a
7382 plain identifier argument, normal_attr for an attribute that wants
7383 an expression, or non_attr if we aren't parsing an attribute list. If
7384 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7385 not all of the expressions in the list were constant.
7386 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7387 will be written to with the location of the closing parenthesis. If
7388 an error occurs, it may or may not be written to. */
7390 static vec<tree, va_gc> *
7391 cp_parser_parenthesized_expression_list (cp_parser* parser,
7392 int is_attribute_list,
7393 bool cast_p,
7394 bool allow_expansion_p,
7395 bool *non_constant_p,
7396 location_t *close_paren_loc)
7398 vec<tree, va_gc> *expression_list;
7399 bool fold_expr_p = is_attribute_list != non_attr;
7400 tree identifier = NULL_TREE;
7401 bool saved_greater_than_is_operator_p;
7403 /* Assume all the expressions will be constant. */
7404 if (non_constant_p)
7405 *non_constant_p = false;
7407 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
7408 return NULL;
7410 expression_list = make_tree_vector ();
7412 /* Within a parenthesized expression, a `>' token is always
7413 the greater-than operator. */
7414 saved_greater_than_is_operator_p
7415 = parser->greater_than_is_operator_p;
7416 parser->greater_than_is_operator_p = true;
7418 /* Consume expressions until there are no more. */
7419 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7420 while (true)
7422 tree expr;
7424 /* At the beginning of attribute lists, check to see if the
7425 next token is an identifier. */
7426 if (is_attribute_list == id_attr
7427 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7429 cp_token *token;
7431 /* Consume the identifier. */
7432 token = cp_lexer_consume_token (parser->lexer);
7433 /* Save the identifier. */
7434 identifier = token->u.value;
7436 else
7438 bool expr_non_constant_p;
7440 /* Parse the next assignment-expression. */
7441 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7443 /* A braced-init-list. */
7444 cp_lexer_set_source_position (parser->lexer);
7445 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7446 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7447 if (non_constant_p && expr_non_constant_p)
7448 *non_constant_p = true;
7450 else if (non_constant_p)
7452 expr = (cp_parser_constant_expression
7453 (parser, /*allow_non_constant_p=*/true,
7454 &expr_non_constant_p));
7455 if (expr_non_constant_p)
7456 *non_constant_p = true;
7458 else
7459 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7460 cast_p);
7462 if (fold_expr_p)
7463 expr = instantiate_non_dependent_expr (expr);
7465 /* If we have an ellipsis, then this is an expression
7466 expansion. */
7467 if (allow_expansion_p
7468 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7470 /* Consume the `...'. */
7471 cp_lexer_consume_token (parser->lexer);
7473 /* Build the argument pack. */
7474 expr = make_pack_expansion (expr);
7477 /* Add it to the list. We add error_mark_node
7478 expressions to the list, so that we can still tell if
7479 the correct form for a parenthesized expression-list
7480 is found. That gives better errors. */
7481 vec_safe_push (expression_list, expr);
7483 if (expr == error_mark_node)
7484 goto skip_comma;
7487 /* After the first item, attribute lists look the same as
7488 expression lists. */
7489 is_attribute_list = non_attr;
7491 get_comma:;
7492 /* If the next token isn't a `,', then we are done. */
7493 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7494 break;
7496 /* Otherwise, consume the `,' and keep going. */
7497 cp_lexer_consume_token (parser->lexer);
7500 if (close_paren_loc)
7501 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7503 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7505 int ending;
7507 skip_comma:;
7508 /* We try and resync to an unnested comma, as that will give the
7509 user better diagnostics. */
7510 ending = cp_parser_skip_to_closing_parenthesis (parser,
7511 /*recovering=*/true,
7512 /*or_comma=*/true,
7513 /*consume_paren=*/true);
7514 if (ending < 0)
7515 goto get_comma;
7516 if (!ending)
7518 parser->greater_than_is_operator_p
7519 = saved_greater_than_is_operator_p;
7520 return NULL;
7524 parser->greater_than_is_operator_p
7525 = saved_greater_than_is_operator_p;
7527 if (identifier)
7528 vec_safe_insert (expression_list, 0, identifier);
7530 return expression_list;
7533 /* Parse a pseudo-destructor-name.
7535 pseudo-destructor-name:
7536 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7537 :: [opt] nested-name-specifier template template-id :: ~ type-name
7538 :: [opt] nested-name-specifier [opt] ~ type-name
7540 If either of the first two productions is used, sets *SCOPE to the
7541 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7542 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7543 or ERROR_MARK_NODE if the parse fails. */
7545 static void
7546 cp_parser_pseudo_destructor_name (cp_parser* parser,
7547 tree object,
7548 tree* scope,
7549 tree* type)
7551 bool nested_name_specifier_p;
7553 /* Handle ~auto. */
7554 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7555 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7556 && !type_dependent_expression_p (object))
7558 if (cxx_dialect < cxx14)
7559 pedwarn (input_location, 0,
7560 "%<~auto%> only available with "
7561 "-std=c++14 or -std=gnu++14");
7562 cp_lexer_consume_token (parser->lexer);
7563 cp_lexer_consume_token (parser->lexer);
7564 *scope = NULL_TREE;
7565 *type = TREE_TYPE (object);
7566 return;
7569 /* Assume that things will not work out. */
7570 *type = error_mark_node;
7572 /* Look for the optional `::' operator. */
7573 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7574 /* Look for the optional nested-name-specifier. */
7575 nested_name_specifier_p
7576 = (cp_parser_nested_name_specifier_opt (parser,
7577 /*typename_keyword_p=*/false,
7578 /*check_dependency_p=*/true,
7579 /*type_p=*/false,
7580 /*is_declaration=*/false)
7581 != NULL_TREE);
7582 /* Now, if we saw a nested-name-specifier, we might be doing the
7583 second production. */
7584 if (nested_name_specifier_p
7585 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7587 /* Consume the `template' keyword. */
7588 cp_lexer_consume_token (parser->lexer);
7589 /* Parse the template-id. */
7590 cp_parser_template_id (parser,
7591 /*template_keyword_p=*/true,
7592 /*check_dependency_p=*/false,
7593 class_type,
7594 /*is_declaration=*/true);
7595 /* Look for the `::' token. */
7596 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7598 /* If the next token is not a `~', then there might be some
7599 additional qualification. */
7600 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7602 /* At this point, we're looking for "type-name :: ~". The type-name
7603 must not be a class-name, since this is a pseudo-destructor. So,
7604 it must be either an enum-name, or a typedef-name -- both of which
7605 are just identifiers. So, we peek ahead to check that the "::"
7606 and "~" tokens are present; if they are not, then we can avoid
7607 calling type_name. */
7608 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7609 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7610 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7612 cp_parser_error (parser, "non-scalar type");
7613 return;
7616 /* Look for the type-name. */
7617 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7618 if (*scope == error_mark_node)
7619 return;
7621 /* Look for the `::' token. */
7622 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7624 else
7625 *scope = NULL_TREE;
7627 /* Look for the `~'. */
7628 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7630 /* Once we see the ~, this has to be a pseudo-destructor. */
7631 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7632 cp_parser_commit_to_topmost_tentative_parse (parser);
7634 /* Look for the type-name again. We are not responsible for
7635 checking that it matches the first type-name. */
7636 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7639 /* Parse a unary-expression.
7641 unary-expression:
7642 postfix-expression
7643 ++ cast-expression
7644 -- cast-expression
7645 unary-operator cast-expression
7646 sizeof unary-expression
7647 sizeof ( type-id )
7648 alignof ( type-id ) [C++0x]
7649 new-expression
7650 delete-expression
7652 GNU Extensions:
7654 unary-expression:
7655 __extension__ cast-expression
7656 __alignof__ unary-expression
7657 __alignof__ ( type-id )
7658 alignof unary-expression [C++0x]
7659 __real__ cast-expression
7660 __imag__ cast-expression
7661 && identifier
7662 sizeof ( type-id ) { initializer-list , [opt] }
7663 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7664 __alignof__ ( type-id ) { initializer-list , [opt] }
7666 ADDRESS_P is true iff the unary-expression is appearing as the
7667 operand of the `&' operator. CAST_P is true if this expression is
7668 the target of a cast.
7670 Returns a representation of the expression. */
7672 static cp_expr
7673 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7674 bool address_p, bool cast_p, bool decltype_p)
7676 cp_token *token;
7677 enum tree_code unary_operator;
7679 /* Peek at the next token. */
7680 token = cp_lexer_peek_token (parser->lexer);
7681 /* Some keywords give away the kind of expression. */
7682 if (token->type == CPP_KEYWORD)
7684 enum rid keyword = token->keyword;
7686 switch (keyword)
7688 case RID_ALIGNOF:
7689 case RID_SIZEOF:
7691 tree operand, ret;
7692 enum tree_code op;
7693 location_t first_loc;
7695 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7696 /* Consume the token. */
7697 cp_lexer_consume_token (parser->lexer);
7698 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7699 /* Parse the operand. */
7700 operand = cp_parser_sizeof_operand (parser, keyword);
7702 if (TYPE_P (operand))
7703 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7704 else
7706 /* ISO C++ defines alignof only with types, not with
7707 expressions. So pedwarn if alignof is used with a non-
7708 type expression. However, __alignof__ is ok. */
7709 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7710 pedwarn (token->location, OPT_Wpedantic,
7711 "ISO C++ does not allow %<alignof%> "
7712 "with a non-type");
7714 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7716 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7717 SIZEOF_EXPR with the original operand. */
7718 if (op == SIZEOF_EXPR && ret != error_mark_node)
7720 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7722 if (!processing_template_decl && TYPE_P (operand))
7724 ret = build_min (SIZEOF_EXPR, size_type_node,
7725 build1 (NOP_EXPR, operand,
7726 error_mark_node));
7727 SIZEOF_EXPR_TYPE_P (ret) = 1;
7729 else
7730 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7731 TREE_SIDE_EFFECTS (ret) = 0;
7732 TREE_READONLY (ret) = 1;
7734 SET_EXPR_LOCATION (ret, first_loc);
7736 return ret;
7739 case RID_NEW:
7740 return cp_parser_new_expression (parser);
7742 case RID_DELETE:
7743 return cp_parser_delete_expression (parser);
7745 case RID_EXTENSION:
7747 /* The saved value of the PEDANTIC flag. */
7748 int saved_pedantic;
7749 tree expr;
7751 /* Save away the PEDANTIC flag. */
7752 cp_parser_extension_opt (parser, &saved_pedantic);
7753 /* Parse the cast-expression. */
7754 expr = cp_parser_simple_cast_expression (parser);
7755 /* Restore the PEDANTIC flag. */
7756 pedantic = saved_pedantic;
7758 return expr;
7761 case RID_REALPART:
7762 case RID_IMAGPART:
7764 tree expression;
7766 /* Consume the `__real__' or `__imag__' token. */
7767 cp_lexer_consume_token (parser->lexer);
7768 /* Parse the cast-expression. */
7769 expression = cp_parser_simple_cast_expression (parser);
7770 /* Create the complete representation. */
7771 return build_x_unary_op (token->location,
7772 (keyword == RID_REALPART
7773 ? REALPART_EXPR : IMAGPART_EXPR),
7774 expression,
7775 tf_warning_or_error);
7777 break;
7779 case RID_TRANSACTION_ATOMIC:
7780 case RID_TRANSACTION_RELAXED:
7781 return cp_parser_transaction_expression (parser, keyword);
7783 case RID_NOEXCEPT:
7785 tree expr;
7786 const char *saved_message;
7787 bool saved_integral_constant_expression_p;
7788 bool saved_non_integral_constant_expression_p;
7789 bool saved_greater_than_is_operator_p;
7791 cp_lexer_consume_token (parser->lexer);
7792 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7794 saved_message = parser->type_definition_forbidden_message;
7795 parser->type_definition_forbidden_message
7796 = G_("types may not be defined in %<noexcept%> expressions");
7798 saved_integral_constant_expression_p
7799 = parser->integral_constant_expression_p;
7800 saved_non_integral_constant_expression_p
7801 = parser->non_integral_constant_expression_p;
7802 parser->integral_constant_expression_p = false;
7804 saved_greater_than_is_operator_p
7805 = parser->greater_than_is_operator_p;
7806 parser->greater_than_is_operator_p = true;
7808 ++cp_unevaluated_operand;
7809 ++c_inhibit_evaluation_warnings;
7810 ++cp_noexcept_operand;
7811 expr = cp_parser_expression (parser);
7812 --cp_noexcept_operand;
7813 --c_inhibit_evaluation_warnings;
7814 --cp_unevaluated_operand;
7816 parser->greater_than_is_operator_p
7817 = saved_greater_than_is_operator_p;
7819 parser->integral_constant_expression_p
7820 = saved_integral_constant_expression_p;
7821 parser->non_integral_constant_expression_p
7822 = saved_non_integral_constant_expression_p;
7824 parser->type_definition_forbidden_message = saved_message;
7826 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7827 return finish_noexcept_expr (expr, tf_warning_or_error);
7830 default:
7831 break;
7835 /* Look for the `:: new' and `:: delete', which also signal the
7836 beginning of a new-expression, or delete-expression,
7837 respectively. If the next token is `::', then it might be one of
7838 these. */
7839 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7841 enum rid keyword;
7843 /* See if the token after the `::' is one of the keywords in
7844 which we're interested. */
7845 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7846 /* If it's `new', we have a new-expression. */
7847 if (keyword == RID_NEW)
7848 return cp_parser_new_expression (parser);
7849 /* Similarly, for `delete'. */
7850 else if (keyword == RID_DELETE)
7851 return cp_parser_delete_expression (parser);
7854 /* Look for a unary operator. */
7855 unary_operator = cp_parser_unary_operator (token);
7856 /* The `++' and `--' operators can be handled similarly, even though
7857 they are not technically unary-operators in the grammar. */
7858 if (unary_operator == ERROR_MARK)
7860 if (token->type == CPP_PLUS_PLUS)
7861 unary_operator = PREINCREMENT_EXPR;
7862 else if (token->type == CPP_MINUS_MINUS)
7863 unary_operator = PREDECREMENT_EXPR;
7864 /* Handle the GNU address-of-label extension. */
7865 else if (cp_parser_allow_gnu_extensions_p (parser)
7866 && token->type == CPP_AND_AND)
7868 tree identifier;
7869 tree expression;
7870 location_t start_loc = token->location;
7872 /* Consume the '&&' token. */
7873 cp_lexer_consume_token (parser->lexer);
7874 /* Look for the identifier. */
7875 location_t finish_loc
7876 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
7877 identifier = cp_parser_identifier (parser);
7878 /* Construct a location of the form:
7879 &&label
7880 ^~~~~~~
7881 with caret==start at the "&&", finish at the end of the label. */
7882 location_t combined_loc
7883 = make_location (start_loc, start_loc, finish_loc);
7884 /* Create an expression representing the address. */
7885 expression = finish_label_address_expr (identifier, combined_loc);
7886 if (cp_parser_non_integral_constant_expression (parser,
7887 NIC_ADDR_LABEL))
7888 expression = error_mark_node;
7889 return expression;
7892 if (unary_operator != ERROR_MARK)
7894 cp_expr cast_expression;
7895 cp_expr expression = error_mark_node;
7896 non_integral_constant non_constant_p = NIC_NONE;
7897 location_t loc = token->location;
7898 tsubst_flags_t complain = complain_flags (decltype_p);
7900 /* Consume the operator token. */
7901 token = cp_lexer_consume_token (parser->lexer);
7902 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
7904 /* Parse the cast-expression. */
7905 cast_expression
7906 = cp_parser_cast_expression (parser,
7907 unary_operator == ADDR_EXPR,
7908 /*cast_p=*/false,
7909 /*decltype*/false,
7910 pidk);
7912 /* Make a location:
7913 OP_TOKEN CAST_EXPRESSION
7914 ^~~~~~~~~~~~~~~~~~~~~~~~~
7915 with start==caret at the operator token, and
7916 extending to the end of the cast_expression. */
7917 loc = make_location (loc, loc, cast_expression.get_finish ());
7919 /* Now, build an appropriate representation. */
7920 switch (unary_operator)
7922 case INDIRECT_REF:
7923 non_constant_p = NIC_STAR;
7924 expression = build_x_indirect_ref (loc, cast_expression,
7925 RO_UNARY_STAR,
7926 complain);
7927 /* TODO: build_x_indirect_ref does not always honor the
7928 location, so ensure it is set. */
7929 expression.set_location (loc);
7930 break;
7932 case ADDR_EXPR:
7933 non_constant_p = NIC_ADDR;
7934 /* Fall through. */
7935 case BIT_NOT_EXPR:
7936 expression = build_x_unary_op (loc, unary_operator,
7937 cast_expression,
7938 complain);
7939 /* TODO: build_x_unary_op does not always honor the location,
7940 so ensure it is set. */
7941 expression.set_location (loc);
7942 break;
7944 case PREINCREMENT_EXPR:
7945 case PREDECREMENT_EXPR:
7946 non_constant_p = unary_operator == PREINCREMENT_EXPR
7947 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7948 /* Fall through. */
7949 case NEGATE_EXPR:
7950 /* Immediately fold negation of a constant, unless the constant is 0
7951 (since -0 == 0) or it would overflow. */
7952 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
7953 && CONSTANT_CLASS_P (cast_expression)
7954 && !integer_zerop (cast_expression)
7955 && !TREE_OVERFLOW (cast_expression))
7957 tree folded = fold_build1 (unary_operator,
7958 TREE_TYPE (cast_expression),
7959 cast_expression);
7960 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
7962 expression = cp_expr (folded, loc);
7963 break;
7966 /* Fall through. */
7967 case UNARY_PLUS_EXPR:
7968 case TRUTH_NOT_EXPR:
7969 expression = finish_unary_op_expr (loc, unary_operator,
7970 cast_expression, complain);
7971 break;
7973 default:
7974 gcc_unreachable ();
7977 if (non_constant_p != NIC_NONE
7978 && cp_parser_non_integral_constant_expression (parser,
7979 non_constant_p))
7980 expression = error_mark_node;
7982 return expression;
7985 return cp_parser_postfix_expression (parser, address_p, cast_p,
7986 /*member_access_only_p=*/false,
7987 decltype_p,
7988 pidk);
7991 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7992 unary-operator, the corresponding tree code is returned. */
7994 static enum tree_code
7995 cp_parser_unary_operator (cp_token* token)
7997 switch (token->type)
7999 case CPP_MULT:
8000 return INDIRECT_REF;
8002 case CPP_AND:
8003 return ADDR_EXPR;
8005 case CPP_PLUS:
8006 return UNARY_PLUS_EXPR;
8008 case CPP_MINUS:
8009 return NEGATE_EXPR;
8011 case CPP_NOT:
8012 return TRUTH_NOT_EXPR;
8014 case CPP_COMPL:
8015 return BIT_NOT_EXPR;
8017 default:
8018 return ERROR_MARK;
8022 /* Parse a new-expression.
8024 new-expression:
8025 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8026 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8028 Returns a representation of the expression. */
8030 static tree
8031 cp_parser_new_expression (cp_parser* parser)
8033 bool global_scope_p;
8034 vec<tree, va_gc> *placement;
8035 tree type;
8036 vec<tree, va_gc> *initializer;
8037 tree nelts = NULL_TREE;
8038 tree ret;
8040 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8042 /* Look for the optional `::' operator. */
8043 global_scope_p
8044 = (cp_parser_global_scope_opt (parser,
8045 /*current_scope_valid_p=*/false)
8046 != NULL_TREE);
8047 /* Look for the `new' operator. */
8048 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8049 /* There's no easy way to tell a new-placement from the
8050 `( type-id )' construct. */
8051 cp_parser_parse_tentatively (parser);
8052 /* Look for a new-placement. */
8053 placement = cp_parser_new_placement (parser);
8054 /* If that didn't work out, there's no new-placement. */
8055 if (!cp_parser_parse_definitely (parser))
8057 if (placement != NULL)
8058 release_tree_vector (placement);
8059 placement = NULL;
8062 /* If the next token is a `(', then we have a parenthesized
8063 type-id. */
8064 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8066 cp_token *token;
8067 const char *saved_message = parser->type_definition_forbidden_message;
8069 /* Consume the `('. */
8070 cp_lexer_consume_token (parser->lexer);
8072 /* Parse the type-id. */
8073 parser->type_definition_forbidden_message
8074 = G_("types may not be defined in a new-expression");
8076 type_id_in_expr_sentinel s (parser);
8077 type = cp_parser_type_id (parser);
8079 parser->type_definition_forbidden_message = saved_message;
8081 /* Look for the closing `)'. */
8082 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8083 token = cp_lexer_peek_token (parser->lexer);
8084 /* There should not be a direct-new-declarator in this production,
8085 but GCC used to allowed this, so we check and emit a sensible error
8086 message for this case. */
8087 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8089 error_at (token->location,
8090 "array bound forbidden after parenthesized type-id");
8091 inform (token->location,
8092 "try removing the parentheses around the type-id");
8093 cp_parser_direct_new_declarator (parser);
8096 /* Otherwise, there must be a new-type-id. */
8097 else
8098 type = cp_parser_new_type_id (parser, &nelts);
8100 /* If the next token is a `(' or '{', then we have a new-initializer. */
8101 cp_token *token = cp_lexer_peek_token (parser->lexer);
8102 if (token->type == CPP_OPEN_PAREN
8103 || token->type == CPP_OPEN_BRACE)
8104 initializer = cp_parser_new_initializer (parser);
8105 else
8106 initializer = NULL;
8108 /* A new-expression may not appear in an integral constant
8109 expression. */
8110 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8111 ret = error_mark_node;
8112 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8113 of a new-type-id or type-id of a new-expression, the new-expression shall
8114 contain a new-initializer of the form ( assignment-expression )".
8115 Additionally, consistently with the spirit of DR 1467, we want to accept
8116 'new auto { 2 }' too. */
8117 else if (type_uses_auto (type)
8118 && (vec_safe_length (initializer) != 1
8119 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8120 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8122 error_at (token->location,
8123 "initialization of new-expression for type %<auto%> "
8124 "requires exactly one element");
8125 ret = error_mark_node;
8127 else
8129 /* Construct a location e.g.:
8130 ptr = new int[100]
8131 ^~~~~~~~~~~~
8132 with caret == start at the start of the "new" token, and the end
8133 at the end of the final token we consumed. */
8134 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8135 location_t end_loc = get_finish (end_tok->location);
8136 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8138 /* Create a representation of the new-expression. */
8139 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8140 tf_warning_or_error);
8141 protected_set_expr_location (ret, combined_loc);
8144 if (placement != NULL)
8145 release_tree_vector (placement);
8146 if (initializer != NULL)
8147 release_tree_vector (initializer);
8149 return ret;
8152 /* Parse a new-placement.
8154 new-placement:
8155 ( expression-list )
8157 Returns the same representation as for an expression-list. */
8159 static vec<tree, va_gc> *
8160 cp_parser_new_placement (cp_parser* parser)
8162 vec<tree, va_gc> *expression_list;
8164 /* Parse the expression-list. */
8165 expression_list = (cp_parser_parenthesized_expression_list
8166 (parser, non_attr, /*cast_p=*/false,
8167 /*allow_expansion_p=*/true,
8168 /*non_constant_p=*/NULL));
8170 if (expression_list && expression_list->is_empty ())
8171 error ("expected expression-list or type-id");
8173 return expression_list;
8176 /* Parse a new-type-id.
8178 new-type-id:
8179 type-specifier-seq new-declarator [opt]
8181 Returns the TYPE allocated. If the new-type-id indicates an array
8182 type, *NELTS is set to the number of elements in the last array
8183 bound; the TYPE will not include the last array bound. */
8185 static tree
8186 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8188 cp_decl_specifier_seq type_specifier_seq;
8189 cp_declarator *new_declarator;
8190 cp_declarator *declarator;
8191 cp_declarator *outer_declarator;
8192 const char *saved_message;
8194 /* The type-specifier sequence must not contain type definitions.
8195 (It cannot contain declarations of new types either, but if they
8196 are not definitions we will catch that because they are not
8197 complete.) */
8198 saved_message = parser->type_definition_forbidden_message;
8199 parser->type_definition_forbidden_message
8200 = G_("types may not be defined in a new-type-id");
8201 /* Parse the type-specifier-seq. */
8202 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8203 /*is_trailing_return=*/false,
8204 &type_specifier_seq);
8205 /* Restore the old message. */
8206 parser->type_definition_forbidden_message = saved_message;
8208 if (type_specifier_seq.type == error_mark_node)
8209 return error_mark_node;
8211 /* Parse the new-declarator. */
8212 new_declarator = cp_parser_new_declarator_opt (parser);
8214 /* Determine the number of elements in the last array dimension, if
8215 any. */
8216 *nelts = NULL_TREE;
8217 /* Skip down to the last array dimension. */
8218 declarator = new_declarator;
8219 outer_declarator = NULL;
8220 while (declarator && (declarator->kind == cdk_pointer
8221 || declarator->kind == cdk_ptrmem))
8223 outer_declarator = declarator;
8224 declarator = declarator->declarator;
8226 while (declarator
8227 && declarator->kind == cdk_array
8228 && declarator->declarator
8229 && declarator->declarator->kind == cdk_array)
8231 outer_declarator = declarator;
8232 declarator = declarator->declarator;
8235 if (declarator && declarator->kind == cdk_array)
8237 *nelts = declarator->u.array.bounds;
8238 if (*nelts == error_mark_node)
8239 *nelts = integer_one_node;
8241 if (outer_declarator)
8242 outer_declarator->declarator = declarator->declarator;
8243 else
8244 new_declarator = NULL;
8247 return groktypename (&type_specifier_seq, new_declarator, false);
8250 /* Parse an (optional) new-declarator.
8252 new-declarator:
8253 ptr-operator new-declarator [opt]
8254 direct-new-declarator
8256 Returns the declarator. */
8258 static cp_declarator *
8259 cp_parser_new_declarator_opt (cp_parser* parser)
8261 enum tree_code code;
8262 tree type, std_attributes = NULL_TREE;
8263 cp_cv_quals cv_quals;
8265 /* We don't know if there's a ptr-operator next, or not. */
8266 cp_parser_parse_tentatively (parser);
8267 /* Look for a ptr-operator. */
8268 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8269 /* If that worked, look for more new-declarators. */
8270 if (cp_parser_parse_definitely (parser))
8272 cp_declarator *declarator;
8274 /* Parse another optional declarator. */
8275 declarator = cp_parser_new_declarator_opt (parser);
8277 declarator = cp_parser_make_indirect_declarator
8278 (code, type, cv_quals, declarator, std_attributes);
8280 return declarator;
8283 /* If the next token is a `[', there is a direct-new-declarator. */
8284 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8285 return cp_parser_direct_new_declarator (parser);
8287 return NULL;
8290 /* Parse a direct-new-declarator.
8292 direct-new-declarator:
8293 [ expression ]
8294 direct-new-declarator [constant-expression]
8298 static cp_declarator *
8299 cp_parser_direct_new_declarator (cp_parser* parser)
8301 cp_declarator *declarator = NULL;
8303 while (true)
8305 tree expression;
8306 cp_token *token;
8308 /* Look for the opening `['. */
8309 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8311 token = cp_lexer_peek_token (parser->lexer);
8312 expression = cp_parser_expression (parser);
8313 /* The standard requires that the expression have integral
8314 type. DR 74 adds enumeration types. We believe that the
8315 real intent is that these expressions be handled like the
8316 expression in a `switch' condition, which also allows
8317 classes with a single conversion to integral or
8318 enumeration type. */
8319 if (!processing_template_decl)
8321 expression
8322 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8323 expression,
8324 /*complain=*/true);
8325 if (!expression)
8327 error_at (token->location,
8328 "expression in new-declarator must have integral "
8329 "or enumeration type");
8330 expression = error_mark_node;
8334 /* Look for the closing `]'. */
8335 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8337 /* Add this bound to the declarator. */
8338 declarator = make_array_declarator (declarator, expression);
8340 /* If the next token is not a `[', then there are no more
8341 bounds. */
8342 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8343 break;
8346 return declarator;
8349 /* Parse a new-initializer.
8351 new-initializer:
8352 ( expression-list [opt] )
8353 braced-init-list
8355 Returns a representation of the expression-list. */
8357 static vec<tree, va_gc> *
8358 cp_parser_new_initializer (cp_parser* parser)
8360 vec<tree, va_gc> *expression_list;
8362 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8364 tree t;
8365 bool expr_non_constant_p;
8366 cp_lexer_set_source_position (parser->lexer);
8367 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8368 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8369 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8370 expression_list = make_tree_vector_single (t);
8372 else
8373 expression_list = (cp_parser_parenthesized_expression_list
8374 (parser, non_attr, /*cast_p=*/false,
8375 /*allow_expansion_p=*/true,
8376 /*non_constant_p=*/NULL));
8378 return expression_list;
8381 /* Parse a delete-expression.
8383 delete-expression:
8384 :: [opt] delete cast-expression
8385 :: [opt] delete [ ] cast-expression
8387 Returns a representation of the expression. */
8389 static tree
8390 cp_parser_delete_expression (cp_parser* parser)
8392 bool global_scope_p;
8393 bool array_p;
8394 tree expression;
8396 /* Look for the optional `::' operator. */
8397 global_scope_p
8398 = (cp_parser_global_scope_opt (parser,
8399 /*current_scope_valid_p=*/false)
8400 != NULL_TREE);
8401 /* Look for the `delete' keyword. */
8402 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8403 /* See if the array syntax is in use. */
8404 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8406 /* Consume the `[' token. */
8407 cp_lexer_consume_token (parser->lexer);
8408 /* Look for the `]' token. */
8409 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8410 /* Remember that this is the `[]' construct. */
8411 array_p = true;
8413 else
8414 array_p = false;
8416 /* Parse the cast-expression. */
8417 expression = cp_parser_simple_cast_expression (parser);
8419 /* A delete-expression may not appear in an integral constant
8420 expression. */
8421 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8422 return error_mark_node;
8424 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8425 tf_warning_or_error);
8428 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8429 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8430 0 otherwise. */
8432 static int
8433 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8435 cp_token *token = cp_lexer_peek_token (parser->lexer);
8436 switch (token->type)
8438 case CPP_COMMA:
8439 case CPP_SEMICOLON:
8440 case CPP_QUERY:
8441 case CPP_COLON:
8442 case CPP_CLOSE_SQUARE:
8443 case CPP_CLOSE_PAREN:
8444 case CPP_CLOSE_BRACE:
8445 case CPP_OPEN_BRACE:
8446 case CPP_DOT:
8447 case CPP_DOT_STAR:
8448 case CPP_DEREF:
8449 case CPP_DEREF_STAR:
8450 case CPP_DIV:
8451 case CPP_MOD:
8452 case CPP_LSHIFT:
8453 case CPP_RSHIFT:
8454 case CPP_LESS:
8455 case CPP_GREATER:
8456 case CPP_LESS_EQ:
8457 case CPP_GREATER_EQ:
8458 case CPP_EQ_EQ:
8459 case CPP_NOT_EQ:
8460 case CPP_EQ:
8461 case CPP_MULT_EQ:
8462 case CPP_DIV_EQ:
8463 case CPP_MOD_EQ:
8464 case CPP_PLUS_EQ:
8465 case CPP_MINUS_EQ:
8466 case CPP_RSHIFT_EQ:
8467 case CPP_LSHIFT_EQ:
8468 case CPP_AND_EQ:
8469 case CPP_XOR_EQ:
8470 case CPP_OR_EQ:
8471 case CPP_XOR:
8472 case CPP_OR:
8473 case CPP_OR_OR:
8474 case CPP_EOF:
8475 case CPP_ELLIPSIS:
8476 return 0;
8478 case CPP_OPEN_PAREN:
8479 /* In ((type ()) () the last () isn't a valid cast-expression,
8480 so the whole must be parsed as postfix-expression. */
8481 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8482 != CPP_CLOSE_PAREN;
8484 case CPP_OPEN_SQUARE:
8485 /* '[' may start a primary-expression in obj-c++ and in C++11,
8486 as a lambda-expression, eg, '(void)[]{}'. */
8487 if (cxx_dialect >= cxx11)
8488 return -1;
8489 return c_dialect_objc ();
8491 case CPP_PLUS_PLUS:
8492 case CPP_MINUS_MINUS:
8493 /* '++' and '--' may or may not start a cast-expression:
8495 struct T { void operator++(int); };
8496 void f() { (T())++; }
8500 int a;
8501 (int)++a; */
8502 return -1;
8504 default:
8505 return 1;
8509 /* Parse a cast-expression.
8511 cast-expression:
8512 unary-expression
8513 ( type-id ) cast-expression
8515 ADDRESS_P is true iff the unary-expression is appearing as the
8516 operand of the `&' operator. CAST_P is true if this expression is
8517 the target of a cast.
8519 Returns a representation of the expression. */
8521 static cp_expr
8522 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8523 bool decltype_p, cp_id_kind * pidk)
8525 /* If it's a `(', then we might be looking at a cast. */
8526 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8528 tree type = NULL_TREE;
8529 cp_expr expr (NULL_TREE);
8530 int cast_expression = 0;
8531 const char *saved_message;
8533 /* There's no way to know yet whether or not this is a cast.
8534 For example, `(int (3))' is a unary-expression, while `(int)
8535 3' is a cast. So, we resort to parsing tentatively. */
8536 cp_parser_parse_tentatively (parser);
8537 /* Types may not be defined in a cast. */
8538 saved_message = parser->type_definition_forbidden_message;
8539 parser->type_definition_forbidden_message
8540 = G_("types may not be defined in casts");
8541 /* Consume the `('. */
8542 cp_token *open_paren = cp_lexer_consume_token (parser->lexer);
8543 location_t open_paren_loc = open_paren->location;
8545 /* A very tricky bit is that `(struct S) { 3 }' is a
8546 compound-literal (which we permit in C++ as an extension).
8547 But, that construct is not a cast-expression -- it is a
8548 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8549 is legal; if the compound-literal were a cast-expression,
8550 you'd need an extra set of parentheses.) But, if we parse
8551 the type-id, and it happens to be a class-specifier, then we
8552 will commit to the parse at that point, because we cannot
8553 undo the action that is done when creating a new class. So,
8554 then we cannot back up and do a postfix-expression.
8556 Another tricky case is the following (c++/29234):
8558 struct S { void operator () (); };
8560 void foo ()
8562 ( S()() );
8565 As a type-id we parse the parenthesized S()() as a function
8566 returning a function, groktypename complains and we cannot
8567 back up in this case either.
8569 Therefore, we scan ahead to the closing `)', and check to see
8570 if the tokens after the `)' can start a cast-expression. Otherwise
8571 we are dealing with an unary-expression, a postfix-expression
8572 or something else.
8574 Yet another tricky case, in C++11, is the following (c++/54891):
8576 (void)[]{};
8578 The issue is that usually, besides the case of lambda-expressions,
8579 the parenthesized type-id cannot be followed by '[', and, eg, we
8580 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8581 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8582 we don't commit, we try a cast-expression, then an unary-expression.
8584 Save tokens so that we can put them back. */
8585 cp_lexer_save_tokens (parser->lexer);
8587 /* We may be looking at a cast-expression. */
8588 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8589 /*consume_paren=*/true))
8590 cast_expression
8591 = cp_parser_tokens_start_cast_expression (parser);
8593 /* Roll back the tokens we skipped. */
8594 cp_lexer_rollback_tokens (parser->lexer);
8595 /* If we aren't looking at a cast-expression, simulate an error so
8596 that the call to cp_parser_error_occurred below returns true. */
8597 if (!cast_expression)
8598 cp_parser_simulate_error (parser);
8599 else
8601 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8602 parser->in_type_id_in_expr_p = true;
8603 /* Look for the type-id. */
8604 type = cp_parser_type_id (parser);
8605 /* Look for the closing `)'. */
8606 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8607 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8610 /* Restore the saved message. */
8611 parser->type_definition_forbidden_message = saved_message;
8613 /* At this point this can only be either a cast or a
8614 parenthesized ctor such as `(T ())' that looks like a cast to
8615 function returning T. */
8616 if (!cp_parser_error_occurred (parser))
8618 /* Only commit if the cast-expression doesn't start with
8619 '++', '--', or '[' in C++11. */
8620 if (cast_expression > 0)
8621 cp_parser_commit_to_topmost_tentative_parse (parser);
8623 expr = cp_parser_cast_expression (parser,
8624 /*address_p=*/false,
8625 /*cast_p=*/true,
8626 /*decltype_p=*/false,
8627 pidk);
8629 if (cp_parser_parse_definitely (parser))
8631 /* Warn about old-style casts, if so requested. */
8632 if (warn_old_style_cast
8633 && !in_system_header_at (input_location)
8634 && !VOID_TYPE_P (type)
8635 && current_lang_name != lang_name_c)
8636 warning (OPT_Wold_style_cast, "use of old-style cast");
8638 /* Only type conversions to integral or enumeration types
8639 can be used in constant-expressions. */
8640 if (!cast_valid_in_integral_constant_expression_p (type)
8641 && cp_parser_non_integral_constant_expression (parser,
8642 NIC_CAST))
8643 return error_mark_node;
8645 /* Perform the cast. */
8646 /* Make a location:
8647 (TYPE) EXPR
8648 ^~~~~~~~~~~
8649 with start==caret at the open paren, extending to the
8650 end of "expr". */
8651 location_t cast_loc = make_location (open_paren_loc,
8652 open_paren_loc,
8653 expr.get_finish ());
8654 expr = build_c_cast (cast_loc, type, expr);
8655 return expr;
8658 else
8659 cp_parser_abort_tentative_parse (parser);
8662 /* If we get here, then it's not a cast, so it must be a
8663 unary-expression. */
8664 return cp_parser_unary_expression (parser, pidk, address_p,
8665 cast_p, decltype_p);
8668 /* Parse a binary expression of the general form:
8670 pm-expression:
8671 cast-expression
8672 pm-expression .* cast-expression
8673 pm-expression ->* cast-expression
8675 multiplicative-expression:
8676 pm-expression
8677 multiplicative-expression * pm-expression
8678 multiplicative-expression / pm-expression
8679 multiplicative-expression % pm-expression
8681 additive-expression:
8682 multiplicative-expression
8683 additive-expression + multiplicative-expression
8684 additive-expression - multiplicative-expression
8686 shift-expression:
8687 additive-expression
8688 shift-expression << additive-expression
8689 shift-expression >> additive-expression
8691 relational-expression:
8692 shift-expression
8693 relational-expression < shift-expression
8694 relational-expression > shift-expression
8695 relational-expression <= shift-expression
8696 relational-expression >= shift-expression
8698 GNU Extension:
8700 relational-expression:
8701 relational-expression <? shift-expression
8702 relational-expression >? shift-expression
8704 equality-expression:
8705 relational-expression
8706 equality-expression == relational-expression
8707 equality-expression != relational-expression
8709 and-expression:
8710 equality-expression
8711 and-expression & equality-expression
8713 exclusive-or-expression:
8714 and-expression
8715 exclusive-or-expression ^ and-expression
8717 inclusive-or-expression:
8718 exclusive-or-expression
8719 inclusive-or-expression | exclusive-or-expression
8721 logical-and-expression:
8722 inclusive-or-expression
8723 logical-and-expression && inclusive-or-expression
8725 logical-or-expression:
8726 logical-and-expression
8727 logical-or-expression || logical-and-expression
8729 All these are implemented with a single function like:
8731 binary-expression:
8732 simple-cast-expression
8733 binary-expression <token> binary-expression
8735 CAST_P is true if this expression is the target of a cast.
8737 The binops_by_token map is used to get the tree codes for each <token> type.
8738 binary-expressions are associated according to a precedence table. */
8740 #define TOKEN_PRECEDENCE(token) \
8741 (((token->type == CPP_GREATER \
8742 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8743 && !parser->greater_than_is_operator_p) \
8744 ? PREC_NOT_OPERATOR \
8745 : binops_by_token[token->type].prec)
8747 static cp_expr
8748 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8749 bool no_toplevel_fold_p,
8750 bool decltype_p,
8751 enum cp_parser_prec prec,
8752 cp_id_kind * pidk)
8754 cp_parser_expression_stack stack;
8755 cp_parser_expression_stack_entry *sp = &stack[0];
8756 cp_parser_expression_stack_entry current;
8757 cp_expr rhs;
8758 cp_token *token;
8759 enum tree_code rhs_type;
8760 enum cp_parser_prec new_prec, lookahead_prec;
8761 tree overload;
8763 /* Parse the first expression. */
8764 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8765 ? TRUTH_NOT_EXPR : ERROR_MARK);
8766 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8767 cast_p, decltype_p, pidk);
8768 current.prec = prec;
8770 if (cp_parser_error_occurred (parser))
8771 return error_mark_node;
8773 for (;;)
8775 /* Get an operator token. */
8776 token = cp_lexer_peek_token (parser->lexer);
8778 if (warn_cxx11_compat
8779 && token->type == CPP_RSHIFT
8780 && !parser->greater_than_is_operator_p)
8782 if (warning_at (token->location, OPT_Wc__11_compat,
8783 "%<>>%> operator is treated"
8784 " as two right angle brackets in C++11"))
8785 inform (token->location,
8786 "suggest parentheses around %<>>%> expression");
8789 new_prec = TOKEN_PRECEDENCE (token);
8790 if (new_prec != PREC_NOT_OPERATOR
8791 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8792 /* This is a fold-expression; handle it later. */
8793 new_prec = PREC_NOT_OPERATOR;
8795 /* Popping an entry off the stack means we completed a subexpression:
8796 - either we found a token which is not an operator (`>' where it is not
8797 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8798 will happen repeatedly;
8799 - or, we found an operator which has lower priority. This is the case
8800 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8801 parsing `3 * 4'. */
8802 if (new_prec <= current.prec)
8804 if (sp == stack)
8805 break;
8806 else
8807 goto pop;
8810 get_rhs:
8811 current.tree_type = binops_by_token[token->type].tree_type;
8812 current.loc = token->location;
8814 /* We used the operator token. */
8815 cp_lexer_consume_token (parser->lexer);
8817 /* For "false && x" or "true || x", x will never be executed;
8818 disable warnings while evaluating it. */
8819 if (current.tree_type == TRUTH_ANDIF_EXPR)
8820 c_inhibit_evaluation_warnings +=
8821 cp_fully_fold (current.lhs) == truthvalue_false_node;
8822 else if (current.tree_type == TRUTH_ORIF_EXPR)
8823 c_inhibit_evaluation_warnings +=
8824 cp_fully_fold (current.lhs) == truthvalue_true_node;
8826 /* Extract another operand. It may be the RHS of this expression
8827 or the LHS of a new, higher priority expression. */
8828 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8829 ? TRUTH_NOT_EXPR : ERROR_MARK);
8830 rhs = cp_parser_simple_cast_expression (parser);
8832 /* Get another operator token. Look up its precedence to avoid
8833 building a useless (immediately popped) stack entry for common
8834 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8835 token = cp_lexer_peek_token (parser->lexer);
8836 lookahead_prec = TOKEN_PRECEDENCE (token);
8837 if (lookahead_prec != PREC_NOT_OPERATOR
8838 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8839 lookahead_prec = PREC_NOT_OPERATOR;
8840 if (lookahead_prec > new_prec)
8842 /* ... and prepare to parse the RHS of the new, higher priority
8843 expression. Since precedence levels on the stack are
8844 monotonically increasing, we do not have to care about
8845 stack overflows. */
8846 *sp = current;
8847 ++sp;
8848 current.lhs = rhs;
8849 current.lhs_type = rhs_type;
8850 current.prec = new_prec;
8851 new_prec = lookahead_prec;
8852 goto get_rhs;
8854 pop:
8855 lookahead_prec = new_prec;
8856 /* If the stack is not empty, we have parsed into LHS the right side
8857 (`4' in the example above) of an expression we had suspended.
8858 We can use the information on the stack to recover the LHS (`3')
8859 from the stack together with the tree code (`MULT_EXPR'), and
8860 the precedence of the higher level subexpression
8861 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8862 which will be used to actually build the additive expression. */
8863 rhs = current.lhs;
8864 rhs_type = current.lhs_type;
8865 --sp;
8866 current = *sp;
8869 /* Undo the disabling of warnings done above. */
8870 if (current.tree_type == TRUTH_ANDIF_EXPR)
8871 c_inhibit_evaluation_warnings -=
8872 cp_fully_fold (current.lhs) == truthvalue_false_node;
8873 else if (current.tree_type == TRUTH_ORIF_EXPR)
8874 c_inhibit_evaluation_warnings -=
8875 cp_fully_fold (current.lhs) == truthvalue_true_node;
8877 if (warn_logical_not_paren
8878 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
8879 && current.lhs_type == TRUTH_NOT_EXPR
8880 /* Avoid warning for !!x == y. */
8881 && (TREE_CODE (current.lhs) != NE_EXPR
8882 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
8883 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
8884 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
8885 /* Avoid warning for !b == y where b is boolean. */
8886 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
8887 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
8888 != BOOLEAN_TYPE))))
8889 /* Avoid warning for !!b == y where b is boolean. */
8890 && (!DECL_P (current.lhs)
8891 || TREE_TYPE (current.lhs) == NULL_TREE
8892 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
8893 warn_logical_not_parentheses (current.loc, current.tree_type,
8894 maybe_constant_value (rhs));
8896 overload = NULL;
8898 location_t combined_loc = make_location (current.loc,
8899 current.lhs.get_start (),
8900 rhs.get_finish ());
8902 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8903 ERROR_MARK for everything that is not a binary expression.
8904 This makes warn_about_parentheses miss some warnings that
8905 involve unary operators. For unary expressions we should
8906 pass the correct tree_code unless the unary expression was
8907 surrounded by parentheses.
8909 if (no_toplevel_fold_p
8910 && lookahead_prec <= current.prec
8911 && sp == stack)
8912 current.lhs = build2_loc (combined_loc,
8913 current.tree_type,
8914 TREE_CODE_CLASS (current.tree_type)
8915 == tcc_comparison
8916 ? boolean_type_node : TREE_TYPE (current.lhs),
8917 current.lhs, rhs);
8918 else
8920 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
8921 current.lhs, current.lhs_type,
8922 rhs, rhs_type, &overload,
8923 complain_flags (decltype_p));
8924 /* TODO: build_x_binary_op doesn't always honor the location. */
8925 current.lhs.set_location (combined_loc);
8927 current.lhs_type = current.tree_type;
8929 /* If the binary operator required the use of an overloaded operator,
8930 then this expression cannot be an integral constant-expression.
8931 An overloaded operator can be used even if both operands are
8932 otherwise permissible in an integral constant-expression if at
8933 least one of the operands is of enumeration type. */
8935 if (overload
8936 && cp_parser_non_integral_constant_expression (parser,
8937 NIC_OVERLOADED))
8938 return error_mark_node;
8941 return current.lhs;
8944 static cp_expr
8945 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8946 bool no_toplevel_fold_p,
8947 enum cp_parser_prec prec,
8948 cp_id_kind * pidk)
8950 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8951 /*decltype*/false, prec, pidk);
8954 /* Parse the `? expression : assignment-expression' part of a
8955 conditional-expression. The LOGICAL_OR_EXPR is the
8956 logical-or-expression that started the conditional-expression.
8957 Returns a representation of the entire conditional-expression.
8959 This routine is used by cp_parser_assignment_expression.
8961 ? expression : assignment-expression
8963 GNU Extensions:
8965 ? : assignment-expression */
8967 static tree
8968 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
8970 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
8971 cp_expr assignment_expr;
8972 struct cp_token *token;
8973 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8975 /* Consume the `?' token. */
8976 cp_lexer_consume_token (parser->lexer);
8977 token = cp_lexer_peek_token (parser->lexer);
8978 if (cp_parser_allow_gnu_extensions_p (parser)
8979 && token->type == CPP_COLON)
8981 pedwarn (token->location, OPT_Wpedantic,
8982 "ISO C++ does not allow ?: with omitted middle operand");
8983 /* Implicit true clause. */
8984 expr = NULL_TREE;
8985 c_inhibit_evaluation_warnings +=
8986 folded_logical_or_expr == truthvalue_true_node;
8987 warn_for_omitted_condop (token->location, logical_or_expr);
8989 else
8991 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8992 parser->colon_corrects_to_scope_p = false;
8993 /* Parse the expression. */
8994 c_inhibit_evaluation_warnings +=
8995 folded_logical_or_expr == truthvalue_false_node;
8996 expr = cp_parser_expression (parser);
8997 c_inhibit_evaluation_warnings +=
8998 ((folded_logical_or_expr == truthvalue_true_node)
8999 - (folded_logical_or_expr == truthvalue_false_node));
9000 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9003 /* The next token should be a `:'. */
9004 cp_parser_require (parser, CPP_COLON, RT_COLON);
9005 /* Parse the assignment-expression. */
9006 assignment_expr = cp_parser_assignment_expression (parser);
9007 c_inhibit_evaluation_warnings -=
9008 folded_logical_or_expr == truthvalue_true_node;
9010 /* Make a location:
9011 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9012 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9013 with the caret at the "?", ranging from the start of
9014 the logical_or_expr to the end of the assignment_expr. */
9015 loc = make_location (loc,
9016 logical_or_expr.get_start (),
9017 assignment_expr.get_finish ());
9019 /* Build the conditional-expression. */
9020 return build_x_conditional_expr (loc, logical_or_expr,
9021 expr,
9022 assignment_expr,
9023 tf_warning_or_error);
9026 /* Parse an assignment-expression.
9028 assignment-expression:
9029 conditional-expression
9030 logical-or-expression assignment-operator assignment_expression
9031 throw-expression
9033 CAST_P is true if this expression is the target of a cast.
9034 DECLTYPE_P is true if this expression is the operand of decltype.
9036 Returns a representation for the expression. */
9038 static cp_expr
9039 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9040 bool cast_p, bool decltype_p)
9042 cp_expr expr;
9044 /* If the next token is the `throw' keyword, then we're looking at
9045 a throw-expression. */
9046 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9047 expr = cp_parser_throw_expression (parser);
9048 /* Otherwise, it must be that we are looking at a
9049 logical-or-expression. */
9050 else
9052 /* Parse the binary expressions (logical-or-expression). */
9053 expr = cp_parser_binary_expression (parser, cast_p, false,
9054 decltype_p,
9055 PREC_NOT_OPERATOR, pidk);
9056 /* If the next token is a `?' then we're actually looking at a
9057 conditional-expression. */
9058 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9059 return cp_parser_question_colon_clause (parser, expr);
9060 else
9062 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9064 /* If it's an assignment-operator, we're using the second
9065 production. */
9066 enum tree_code assignment_operator
9067 = cp_parser_assignment_operator_opt (parser);
9068 if (assignment_operator != ERROR_MARK)
9070 bool non_constant_p;
9072 /* Parse the right-hand side of the assignment. */
9073 cp_expr rhs = cp_parser_initializer_clause (parser,
9074 &non_constant_p);
9076 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9077 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9079 /* An assignment may not appear in a
9080 constant-expression. */
9081 if (cp_parser_non_integral_constant_expression (parser,
9082 NIC_ASSIGNMENT))
9083 return error_mark_node;
9084 /* Build the assignment expression. Its default
9085 location:
9086 LHS = RHS
9087 ~~~~^~~~~
9088 is the location of the '=' token as the
9089 caret, ranging from the start of the lhs to the
9090 end of the rhs. */
9091 loc = make_location (loc,
9092 expr.get_start (),
9093 rhs.get_finish ());
9094 expr = build_x_modify_expr (loc, expr,
9095 assignment_operator,
9096 rhs,
9097 complain_flags (decltype_p));
9098 /* TODO: build_x_modify_expr doesn't honor the location,
9099 so we must set it here. */
9100 expr.set_location (loc);
9105 return expr;
9108 /* Parse an (optional) assignment-operator.
9110 assignment-operator: one of
9111 = *= /= %= += -= >>= <<= &= ^= |=
9113 GNU Extension:
9115 assignment-operator: one of
9116 <?= >?=
9118 If the next token is an assignment operator, the corresponding tree
9119 code is returned, and the token is consumed. For example, for
9120 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9121 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9122 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9123 operator, ERROR_MARK is returned. */
9125 static enum tree_code
9126 cp_parser_assignment_operator_opt (cp_parser* parser)
9128 enum tree_code op;
9129 cp_token *token;
9131 /* Peek at the next token. */
9132 token = cp_lexer_peek_token (parser->lexer);
9134 switch (token->type)
9136 case CPP_EQ:
9137 op = NOP_EXPR;
9138 break;
9140 case CPP_MULT_EQ:
9141 op = MULT_EXPR;
9142 break;
9144 case CPP_DIV_EQ:
9145 op = TRUNC_DIV_EXPR;
9146 break;
9148 case CPP_MOD_EQ:
9149 op = TRUNC_MOD_EXPR;
9150 break;
9152 case CPP_PLUS_EQ:
9153 op = PLUS_EXPR;
9154 break;
9156 case CPP_MINUS_EQ:
9157 op = MINUS_EXPR;
9158 break;
9160 case CPP_RSHIFT_EQ:
9161 op = RSHIFT_EXPR;
9162 break;
9164 case CPP_LSHIFT_EQ:
9165 op = LSHIFT_EXPR;
9166 break;
9168 case CPP_AND_EQ:
9169 op = BIT_AND_EXPR;
9170 break;
9172 case CPP_XOR_EQ:
9173 op = BIT_XOR_EXPR;
9174 break;
9176 case CPP_OR_EQ:
9177 op = BIT_IOR_EXPR;
9178 break;
9180 default:
9181 /* Nothing else is an assignment operator. */
9182 op = ERROR_MARK;
9185 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9186 if (op != ERROR_MARK
9187 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9188 op = ERROR_MARK;
9190 /* If it was an assignment operator, consume it. */
9191 if (op != ERROR_MARK)
9192 cp_lexer_consume_token (parser->lexer);
9194 return op;
9197 /* Parse an expression.
9199 expression:
9200 assignment-expression
9201 expression , assignment-expression
9203 CAST_P is true if this expression is the target of a cast.
9204 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9205 except possibly parenthesized or on the RHS of a comma (N3276).
9207 Returns a representation of the expression. */
9209 static cp_expr
9210 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9211 bool cast_p, bool decltype_p)
9213 cp_expr expression = NULL_TREE;
9214 location_t loc = UNKNOWN_LOCATION;
9216 while (true)
9218 cp_expr assignment_expression;
9220 /* Parse the next assignment-expression. */
9221 assignment_expression
9222 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9224 /* We don't create a temporary for a call that is the immediate operand
9225 of decltype or on the RHS of a comma. But when we see a comma, we
9226 need to create a temporary for a call on the LHS. */
9227 if (decltype_p && !processing_template_decl
9228 && TREE_CODE (assignment_expression) == CALL_EXPR
9229 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9230 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9231 assignment_expression
9232 = build_cplus_new (TREE_TYPE (assignment_expression),
9233 assignment_expression, tf_warning_or_error);
9235 /* If this is the first assignment-expression, we can just
9236 save it away. */
9237 if (!expression)
9238 expression = assignment_expression;
9239 else
9241 /* Create a location with caret at the comma, ranging
9242 from the start of the LHS to the end of the RHS. */
9243 loc = make_location (loc,
9244 expression.get_start (),
9245 assignment_expression.get_finish ());
9246 expression = build_x_compound_expr (loc, expression,
9247 assignment_expression,
9248 complain_flags (decltype_p));
9249 expression.set_location (loc);
9251 /* If the next token is not a comma, or we're in a fold-expression, then
9252 we are done with the expression. */
9253 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9254 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9255 break;
9256 /* Consume the `,'. */
9257 loc = cp_lexer_peek_token (parser->lexer)->location;
9258 cp_lexer_consume_token (parser->lexer);
9259 /* A comma operator cannot appear in a constant-expression. */
9260 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9261 expression = error_mark_node;
9264 return expression;
9267 /* Parse a constant-expression.
9269 constant-expression:
9270 conditional-expression
9272 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9273 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9274 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9275 is false, NON_CONSTANT_P should be NULL. */
9277 static cp_expr
9278 cp_parser_constant_expression (cp_parser* parser,
9279 bool allow_non_constant_p,
9280 bool *non_constant_p)
9282 bool saved_integral_constant_expression_p;
9283 bool saved_allow_non_integral_constant_expression_p;
9284 bool saved_non_integral_constant_expression_p;
9285 cp_expr expression;
9287 /* It might seem that we could simply parse the
9288 conditional-expression, and then check to see if it were
9289 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9290 one that the compiler can figure out is constant, possibly after
9291 doing some simplifications or optimizations. The standard has a
9292 precise definition of constant-expression, and we must honor
9293 that, even though it is somewhat more restrictive.
9295 For example:
9297 int i[(2, 3)];
9299 is not a legal declaration, because `(2, 3)' is not a
9300 constant-expression. The `,' operator is forbidden in a
9301 constant-expression. However, GCC's constant-folding machinery
9302 will fold this operation to an INTEGER_CST for `3'. */
9304 /* Save the old settings. */
9305 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9306 saved_allow_non_integral_constant_expression_p
9307 = parser->allow_non_integral_constant_expression_p;
9308 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9309 /* We are now parsing a constant-expression. */
9310 parser->integral_constant_expression_p = true;
9311 parser->allow_non_integral_constant_expression_p
9312 = (allow_non_constant_p || cxx_dialect >= cxx11);
9313 parser->non_integral_constant_expression_p = false;
9314 /* Although the grammar says "conditional-expression", we parse an
9315 "assignment-expression", which also permits "throw-expression"
9316 and the use of assignment operators. In the case that
9317 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9318 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9319 actually essential that we look for an assignment-expression.
9320 For example, cp_parser_initializer_clauses uses this function to
9321 determine whether a particular assignment-expression is in fact
9322 constant. */
9323 expression = cp_parser_assignment_expression (parser);
9324 /* Restore the old settings. */
9325 parser->integral_constant_expression_p
9326 = saved_integral_constant_expression_p;
9327 parser->allow_non_integral_constant_expression_p
9328 = saved_allow_non_integral_constant_expression_p;
9329 if (cxx_dialect >= cxx11)
9331 /* Require an rvalue constant expression here; that's what our
9332 callers expect. Reference constant expressions are handled
9333 separately in e.g. cp_parser_template_argument. */
9334 bool is_const = potential_rvalue_constant_expression (expression);
9335 parser->non_integral_constant_expression_p = !is_const;
9336 if (!is_const && !allow_non_constant_p)
9337 require_potential_rvalue_constant_expression (expression);
9339 if (allow_non_constant_p)
9340 *non_constant_p = parser->non_integral_constant_expression_p;
9341 parser->non_integral_constant_expression_p
9342 = saved_non_integral_constant_expression_p;
9344 return expression;
9347 /* Parse __builtin_offsetof.
9349 offsetof-expression:
9350 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9352 offsetof-member-designator:
9353 id-expression
9354 | offsetof-member-designator "." id-expression
9355 | offsetof-member-designator "[" expression "]"
9356 | offsetof-member-designator "->" id-expression */
9358 static cp_expr
9359 cp_parser_builtin_offsetof (cp_parser *parser)
9361 int save_ice_p, save_non_ice_p;
9362 tree type;
9363 cp_expr expr;
9364 cp_id_kind dummy;
9365 cp_token *token;
9366 location_t finish_loc;
9368 /* We're about to accept non-integral-constant things, but will
9369 definitely yield an integral constant expression. Save and
9370 restore these values around our local parsing. */
9371 save_ice_p = parser->integral_constant_expression_p;
9372 save_non_ice_p = parser->non_integral_constant_expression_p;
9374 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9376 /* Consume the "__builtin_offsetof" token. */
9377 cp_lexer_consume_token (parser->lexer);
9378 /* Consume the opening `('. */
9379 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9380 /* Parse the type-id. */
9381 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9382 type = cp_parser_type_id (parser);
9383 /* Look for the `,'. */
9384 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9385 token = cp_lexer_peek_token (parser->lexer);
9387 /* Build the (type *)null that begins the traditional offsetof macro. */
9388 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
9389 tf_warning_or_error);
9391 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9392 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
9393 true, &dummy, token->location);
9394 while (true)
9396 token = cp_lexer_peek_token (parser->lexer);
9397 switch (token->type)
9399 case CPP_OPEN_SQUARE:
9400 /* offsetof-member-designator "[" expression "]" */
9401 expr = cp_parser_postfix_open_square_expression (parser, expr,
9402 true, false);
9403 break;
9405 case CPP_DEREF:
9406 /* offsetof-member-designator "->" identifier */
9407 expr = grok_array_decl (token->location, expr,
9408 integer_zero_node, false);
9409 /* FALLTHRU */
9411 case CPP_DOT:
9412 /* offsetof-member-designator "." identifier */
9413 cp_lexer_consume_token (parser->lexer);
9414 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9415 expr, true, &dummy,
9416 token->location);
9417 break;
9419 case CPP_CLOSE_PAREN:
9420 /* Consume the ")" token. */
9421 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9422 cp_lexer_consume_token (parser->lexer);
9423 goto success;
9425 default:
9426 /* Error. We know the following require will fail, but
9427 that gives the proper error message. */
9428 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9429 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9430 expr = error_mark_node;
9431 goto failure;
9435 success:
9436 /* Make a location of the form:
9437 __builtin_offsetof (struct s, f)
9438 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9439 with caret at the type-id, ranging from the start of the
9440 "_builtin_offsetof" token to the close paren. */
9441 loc = make_location (loc, start_loc, finish_loc);
9442 /* The result will be an INTEGER_CST, so we need to explicitly
9443 preserve the location. */
9444 expr = cp_expr (finish_offsetof (expr, loc), loc);
9446 failure:
9447 parser->integral_constant_expression_p = save_ice_p;
9448 parser->non_integral_constant_expression_p = save_non_ice_p;
9450 return expr;
9453 /* Parse a trait expression.
9455 Returns a representation of the expression, the underlying type
9456 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9458 static tree
9459 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9461 cp_trait_kind kind;
9462 tree type1, type2 = NULL_TREE;
9463 bool binary = false;
9464 bool variadic = false;
9466 switch (keyword)
9468 case RID_HAS_NOTHROW_ASSIGN:
9469 kind = CPTK_HAS_NOTHROW_ASSIGN;
9470 break;
9471 case RID_HAS_NOTHROW_CONSTRUCTOR:
9472 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9473 break;
9474 case RID_HAS_NOTHROW_COPY:
9475 kind = CPTK_HAS_NOTHROW_COPY;
9476 break;
9477 case RID_HAS_TRIVIAL_ASSIGN:
9478 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9479 break;
9480 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9481 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9482 break;
9483 case RID_HAS_TRIVIAL_COPY:
9484 kind = CPTK_HAS_TRIVIAL_COPY;
9485 break;
9486 case RID_HAS_TRIVIAL_DESTRUCTOR:
9487 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9488 break;
9489 case RID_HAS_VIRTUAL_DESTRUCTOR:
9490 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9491 break;
9492 case RID_IS_ABSTRACT:
9493 kind = CPTK_IS_ABSTRACT;
9494 break;
9495 case RID_IS_BASE_OF:
9496 kind = CPTK_IS_BASE_OF;
9497 binary = true;
9498 break;
9499 case RID_IS_CLASS:
9500 kind = CPTK_IS_CLASS;
9501 break;
9502 case RID_IS_EMPTY:
9503 kind = CPTK_IS_EMPTY;
9504 break;
9505 case RID_IS_ENUM:
9506 kind = CPTK_IS_ENUM;
9507 break;
9508 case RID_IS_FINAL:
9509 kind = CPTK_IS_FINAL;
9510 break;
9511 case RID_IS_LITERAL_TYPE:
9512 kind = CPTK_IS_LITERAL_TYPE;
9513 break;
9514 case RID_IS_POD:
9515 kind = CPTK_IS_POD;
9516 break;
9517 case RID_IS_POLYMORPHIC:
9518 kind = CPTK_IS_POLYMORPHIC;
9519 break;
9520 case RID_IS_SAME_AS:
9521 kind = CPTK_IS_SAME_AS;
9522 binary = true;
9523 break;
9524 case RID_IS_STD_LAYOUT:
9525 kind = CPTK_IS_STD_LAYOUT;
9526 break;
9527 case RID_IS_TRIVIAL:
9528 kind = CPTK_IS_TRIVIAL;
9529 break;
9530 case RID_IS_TRIVIALLY_ASSIGNABLE:
9531 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9532 binary = true;
9533 break;
9534 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9535 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9536 variadic = true;
9537 break;
9538 case RID_IS_TRIVIALLY_COPYABLE:
9539 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9540 break;
9541 case RID_IS_UNION:
9542 kind = CPTK_IS_UNION;
9543 break;
9544 case RID_UNDERLYING_TYPE:
9545 kind = CPTK_UNDERLYING_TYPE;
9546 break;
9547 case RID_BASES:
9548 kind = CPTK_BASES;
9549 break;
9550 case RID_DIRECT_BASES:
9551 kind = CPTK_DIRECT_BASES;
9552 break;
9553 default:
9554 gcc_unreachable ();
9557 /* Consume the token. */
9558 cp_lexer_consume_token (parser->lexer);
9560 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9563 type_id_in_expr_sentinel s (parser);
9564 type1 = cp_parser_type_id (parser);
9567 if (type1 == error_mark_node)
9568 return error_mark_node;
9570 if (binary)
9572 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9575 type_id_in_expr_sentinel s (parser);
9576 type2 = cp_parser_type_id (parser);
9579 if (type2 == error_mark_node)
9580 return error_mark_node;
9582 else if (variadic)
9584 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9586 cp_lexer_consume_token (parser->lexer);
9587 tree elt = cp_parser_type_id (parser);
9588 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9590 cp_lexer_consume_token (parser->lexer);
9591 elt = make_pack_expansion (elt);
9593 if (elt == error_mark_node)
9594 return error_mark_node;
9595 type2 = tree_cons (NULL_TREE, elt, type2);
9599 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9601 /* Complete the trait expression, which may mean either processing
9602 the trait expr now or saving it for template instantiation. */
9603 switch(kind)
9605 case CPTK_UNDERLYING_TYPE:
9606 return finish_underlying_type (type1);
9607 case CPTK_BASES:
9608 return finish_bases (type1, false);
9609 case CPTK_DIRECT_BASES:
9610 return finish_bases (type1, true);
9611 default:
9612 return finish_trait_expr (kind, type1, type2);
9616 /* Lambdas that appear in variable initializer or default argument scope
9617 get that in their mangling, so we need to record it. We might as well
9618 use the count for function and namespace scopes as well. */
9619 static GTY(()) tree lambda_scope;
9620 static GTY(()) int lambda_count;
9621 struct GTY(()) tree_int
9623 tree t;
9624 int i;
9626 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
9628 static void
9629 start_lambda_scope (tree decl)
9631 tree_int ti;
9632 gcc_assert (decl);
9633 /* Once we're inside a function, we ignore other scopes and just push
9634 the function again so that popping works properly. */
9635 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
9636 decl = current_function_decl;
9637 ti.t = lambda_scope;
9638 ti.i = lambda_count;
9639 vec_safe_push (lambda_scope_stack, ti);
9640 if (lambda_scope != decl)
9642 /* Don't reset the count if we're still in the same function. */
9643 lambda_scope = decl;
9644 lambda_count = 0;
9648 static void
9649 record_lambda_scope (tree lambda)
9651 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
9652 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
9655 static void
9656 finish_lambda_scope (void)
9658 tree_int *p = &lambda_scope_stack->last ();
9659 if (lambda_scope != p->t)
9661 lambda_scope = p->t;
9662 lambda_count = p->i;
9664 lambda_scope_stack->pop ();
9667 /* Parse a lambda expression.
9669 lambda-expression:
9670 lambda-introducer lambda-declarator [opt] compound-statement
9672 Returns a representation of the expression. */
9674 static cp_expr
9675 cp_parser_lambda_expression (cp_parser* parser)
9677 tree lambda_expr = build_lambda_expr ();
9678 tree type;
9679 bool ok = true;
9680 cp_token *token = cp_lexer_peek_token (parser->lexer);
9681 cp_token_position start = 0;
9683 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9685 if (cp_unevaluated_operand)
9687 if (!token->error_reported)
9689 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9690 "lambda-expression in unevaluated context");
9691 token->error_reported = true;
9693 ok = false;
9695 else if (parser->in_template_argument_list_p)
9697 if (!token->error_reported)
9699 error_at (token->location, "lambda-expression in template-argument");
9700 token->error_reported = true;
9702 ok = false;
9705 /* We may be in the middle of deferred access check. Disable
9706 it now. */
9707 push_deferring_access_checks (dk_no_deferred);
9709 cp_parser_lambda_introducer (parser, lambda_expr);
9711 type = begin_lambda_type (lambda_expr);
9712 if (type == error_mark_node)
9713 return error_mark_node;
9715 record_lambda_scope (lambda_expr);
9717 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9718 determine_visibility (TYPE_NAME (type));
9720 /* Now that we've started the type, add the capture fields for any
9721 explicit captures. */
9722 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9725 /* Inside the class, surrounding template-parameter-lists do not apply. */
9726 unsigned int saved_num_template_parameter_lists
9727 = parser->num_template_parameter_lists;
9728 unsigned char in_statement = parser->in_statement;
9729 bool in_switch_statement_p = parser->in_switch_statement_p;
9730 bool fully_implicit_function_template_p
9731 = parser->fully_implicit_function_template_p;
9732 tree implicit_template_parms = parser->implicit_template_parms;
9733 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9734 bool auto_is_implicit_function_template_parm_p
9735 = parser->auto_is_implicit_function_template_parm_p;
9737 parser->num_template_parameter_lists = 0;
9738 parser->in_statement = 0;
9739 parser->in_switch_statement_p = false;
9740 parser->fully_implicit_function_template_p = false;
9741 parser->implicit_template_parms = 0;
9742 parser->implicit_template_scope = 0;
9743 parser->auto_is_implicit_function_template_parm_p = false;
9745 /* By virtue of defining a local class, a lambda expression has access to
9746 the private variables of enclosing classes. */
9748 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9750 if (ok)
9752 if (!cp_parser_error_occurred (parser)
9753 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9754 && cp_parser_start_tentative_firewall (parser))
9755 start = token;
9756 cp_parser_lambda_body (parser, lambda_expr);
9758 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9760 if (cp_parser_skip_to_closing_brace (parser))
9761 cp_lexer_consume_token (parser->lexer);
9764 /* The capture list was built up in reverse order; fix that now. */
9765 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9766 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9768 if (ok)
9769 maybe_add_lambda_conv_op (type);
9771 type = finish_struct (type, /*attributes=*/NULL_TREE);
9773 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9774 parser->in_statement = in_statement;
9775 parser->in_switch_statement_p = in_switch_statement_p;
9776 parser->fully_implicit_function_template_p
9777 = fully_implicit_function_template_p;
9778 parser->implicit_template_parms = implicit_template_parms;
9779 parser->implicit_template_scope = implicit_template_scope;
9780 parser->auto_is_implicit_function_template_parm_p
9781 = auto_is_implicit_function_template_parm_p;
9784 pop_deferring_access_checks ();
9786 /* This field is only used during parsing of the lambda. */
9787 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9789 /* This lambda shouldn't have any proxies left at this point. */
9790 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9791 /* And now that we're done, push proxies for an enclosing lambda. */
9792 insert_pending_capture_proxies ();
9794 if (ok)
9795 lambda_expr = build_lambda_object (lambda_expr);
9796 else
9797 lambda_expr = error_mark_node;
9799 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9801 return lambda_expr;
9804 /* Parse the beginning of a lambda expression.
9806 lambda-introducer:
9807 [ lambda-capture [opt] ]
9809 LAMBDA_EXPR is the current representation of the lambda expression. */
9811 static void
9812 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9814 /* Need commas after the first capture. */
9815 bool first = true;
9817 /* Eat the leading `['. */
9818 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9820 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9821 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9822 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9823 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9824 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9825 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9827 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9829 cp_lexer_consume_token (parser->lexer);
9830 first = false;
9833 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9835 cp_token* capture_token;
9836 tree capture_id;
9837 tree capture_init_expr;
9838 cp_id_kind idk = CP_ID_KIND_NONE;
9839 bool explicit_init_p = false;
9841 enum capture_kind_type
9843 BY_COPY,
9844 BY_REFERENCE
9846 enum capture_kind_type capture_kind = BY_COPY;
9848 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9850 error ("expected end of capture-list");
9851 return;
9854 if (first)
9855 first = false;
9856 else
9857 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9859 /* Possibly capture `this'. */
9860 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9862 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9863 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9864 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9865 "with by-copy capture default");
9866 cp_lexer_consume_token (parser->lexer);
9867 add_capture (lambda_expr,
9868 /*id=*/this_identifier,
9869 /*initializer=*/finish_this_expr(),
9870 /*by_reference_p=*/false,
9871 explicit_init_p);
9872 continue;
9875 /* Remember whether we want to capture as a reference or not. */
9876 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9878 capture_kind = BY_REFERENCE;
9879 cp_lexer_consume_token (parser->lexer);
9882 /* Get the identifier. */
9883 capture_token = cp_lexer_peek_token (parser->lexer);
9884 capture_id = cp_parser_identifier (parser);
9886 if (capture_id == error_mark_node)
9887 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9888 delimiters, but I modified this to stop on unnested ']' as well. It
9889 was already changed to stop on unnested '}', so the
9890 "closing_parenthesis" name is no more misleading with my change. */
9892 cp_parser_skip_to_closing_parenthesis (parser,
9893 /*recovering=*/true,
9894 /*or_comma=*/true,
9895 /*consume_paren=*/true);
9896 break;
9899 /* Find the initializer for this capture. */
9900 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9901 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9902 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9904 bool direct, non_constant;
9905 /* An explicit initializer exists. */
9906 if (cxx_dialect < cxx14)
9907 pedwarn (input_location, 0,
9908 "lambda capture initializers "
9909 "only available with -std=c++14 or -std=gnu++14");
9910 capture_init_expr = cp_parser_initializer (parser, &direct,
9911 &non_constant);
9912 explicit_init_p = true;
9913 if (capture_init_expr == NULL_TREE)
9915 error ("empty initializer for lambda init-capture");
9916 capture_init_expr = error_mark_node;
9919 else
9921 const char* error_msg;
9923 /* Turn the identifier into an id-expression. */
9924 capture_init_expr
9925 = cp_parser_lookup_name_simple (parser, capture_id,
9926 capture_token->location);
9928 if (capture_init_expr == error_mark_node)
9930 unqualified_name_lookup_error (capture_id);
9931 continue;
9933 else if (DECL_P (capture_init_expr)
9934 && (!VAR_P (capture_init_expr)
9935 && TREE_CODE (capture_init_expr) != PARM_DECL))
9937 error_at (capture_token->location,
9938 "capture of non-variable %qD ",
9939 capture_init_expr);
9940 inform (DECL_SOURCE_LOCATION (capture_init_expr),
9941 "%q#D declared here", capture_init_expr);
9942 continue;
9944 if (VAR_P (capture_init_expr)
9945 && decl_storage_duration (capture_init_expr) != dk_auto)
9947 if (pedwarn (capture_token->location, 0, "capture of variable "
9948 "%qD with non-automatic storage duration",
9949 capture_init_expr))
9950 inform (DECL_SOURCE_LOCATION (capture_init_expr),
9951 "%q#D declared here", capture_init_expr);
9952 continue;
9955 capture_init_expr
9956 = finish_id_expression
9957 (capture_id,
9958 capture_init_expr,
9959 parser->scope,
9960 &idk,
9961 /*integral_constant_expression_p=*/false,
9962 /*allow_non_integral_constant_expression_p=*/false,
9963 /*non_integral_constant_expression_p=*/NULL,
9964 /*template_p=*/false,
9965 /*done=*/true,
9966 /*address_p=*/false,
9967 /*template_arg_p=*/false,
9968 &error_msg,
9969 capture_token->location);
9971 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9973 cp_lexer_consume_token (parser->lexer);
9974 capture_init_expr = make_pack_expansion (capture_init_expr);
9976 else
9977 check_for_bare_parameter_packs (capture_init_expr);
9980 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9981 && !explicit_init_p)
9983 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9984 && capture_kind == BY_COPY)
9985 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9986 "of %qD redundant with by-copy capture default",
9987 capture_id);
9988 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9989 && capture_kind == BY_REFERENCE)
9990 pedwarn (capture_token->location, 0, "explicit by-reference "
9991 "capture of %qD redundant with by-reference capture "
9992 "default", capture_id);
9995 add_capture (lambda_expr,
9996 capture_id,
9997 capture_init_expr,
9998 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9999 explicit_init_p);
10002 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10005 /* Parse the (optional) middle of a lambda expression.
10007 lambda-declarator:
10008 < template-parameter-list [opt] >
10009 ( parameter-declaration-clause [opt] )
10010 attribute-specifier [opt]
10011 mutable [opt]
10012 exception-specification [opt]
10013 lambda-return-type-clause [opt]
10015 LAMBDA_EXPR is the current representation of the lambda expression. */
10017 static bool
10018 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10020 /* 5.1.1.4 of the standard says:
10021 If a lambda-expression does not include a lambda-declarator, it is as if
10022 the lambda-declarator were ().
10023 This means an empty parameter list, no attributes, and no exception
10024 specification. */
10025 tree param_list = void_list_node;
10026 tree attributes = NULL_TREE;
10027 tree exception_spec = NULL_TREE;
10028 tree template_param_list = NULL_TREE;
10029 tree tx_qual = NULL_TREE;
10031 /* The template-parameter-list is optional, but must begin with
10032 an opening angle if present. */
10033 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10035 if (cxx_dialect < cxx14)
10036 pedwarn (parser->lexer->next_token->location, 0,
10037 "lambda templates are only available with "
10038 "-std=c++14 or -std=gnu++14");
10040 cp_lexer_consume_token (parser->lexer);
10042 template_param_list = cp_parser_template_parameter_list (parser);
10044 cp_parser_skip_to_end_of_template_parameter_list (parser);
10046 /* We just processed one more parameter list. */
10047 ++parser->num_template_parameter_lists;
10050 /* The parameter-declaration-clause is optional (unless
10051 template-parameter-list was given), but must begin with an
10052 opening parenthesis if present. */
10053 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10055 cp_lexer_consume_token (parser->lexer);
10057 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10059 /* Parse parameters. */
10060 param_list = cp_parser_parameter_declaration_clause (parser);
10062 /* Default arguments shall not be specified in the
10063 parameter-declaration-clause of a lambda-declarator. */
10064 for (tree t = param_list; t; t = TREE_CHAIN (t))
10065 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
10066 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10067 "default argument specified for lambda parameter");
10069 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10071 attributes = cp_parser_attributes_opt (parser);
10073 /* Parse optional `mutable' keyword. */
10074 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
10076 cp_lexer_consume_token (parser->lexer);
10077 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10080 tx_qual = cp_parser_tx_qualifier_opt (parser);
10082 /* Parse optional exception specification. */
10083 exception_spec = cp_parser_exception_specification_opt (parser);
10085 /* Parse optional trailing return type. */
10086 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10088 cp_lexer_consume_token (parser->lexer);
10089 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10090 = cp_parser_trailing_type_id (parser);
10093 /* The function parameters must be in scope all the way until after the
10094 trailing-return-type in case of decltype. */
10095 pop_bindings_and_leave_scope ();
10097 else if (template_param_list != NULL_TREE) // generate diagnostic
10098 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10100 /* Create the function call operator.
10102 Messing with declarators like this is no uglier than building up the
10103 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10104 other code. */
10106 cp_decl_specifier_seq return_type_specs;
10107 cp_declarator* declarator;
10108 tree fco;
10109 int quals;
10110 void *p;
10112 clear_decl_specs (&return_type_specs);
10113 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10114 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
10115 else
10116 /* Maybe we will deduce the return type later. */
10117 return_type_specs.type = make_auto ();
10119 p = obstack_alloc (&declarator_obstack, 0);
10121 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
10122 sfk_none);
10124 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10125 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10126 declarator = make_call_declarator (declarator, param_list, quals,
10127 VIRT_SPEC_UNSPECIFIED,
10128 REF_QUAL_NONE,
10129 tx_qual,
10130 exception_spec,
10131 /*late_return_type=*/NULL_TREE,
10132 /*requires_clause*/NULL_TREE);
10133 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10135 fco = grokmethod (&return_type_specs,
10136 declarator,
10137 attributes);
10138 if (fco != error_mark_node)
10140 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10141 DECL_ARTIFICIAL (fco) = 1;
10142 /* Give the object parameter a different name. */
10143 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10144 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10145 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10147 if (template_param_list)
10149 fco = finish_member_template_decl (fco);
10150 finish_template_decl (template_param_list);
10151 --parser->num_template_parameter_lists;
10153 else if (parser->fully_implicit_function_template_p)
10154 fco = finish_fully_implicit_template (parser, fco);
10156 finish_member_declaration (fco);
10158 obstack_free (&declarator_obstack, p);
10160 return (fco != error_mark_node);
10164 /* Parse the body of a lambda expression, which is simply
10166 compound-statement
10168 but which requires special handling.
10169 LAMBDA_EXPR is the current representation of the lambda expression. */
10171 static void
10172 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10174 bool nested = (current_function_decl != NULL_TREE);
10175 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10176 if (nested)
10177 push_function_context ();
10178 else
10179 /* Still increment function_depth so that we don't GC in the
10180 middle of an expression. */
10181 ++function_depth;
10182 vec<tree> omp_privatization_save;
10183 save_omp_privatization_clauses (omp_privatization_save);
10184 /* Clear this in case we're in the middle of a default argument. */
10185 parser->local_variables_forbidden_p = false;
10187 /* Finish the function call operator
10188 - class_specifier
10189 + late_parsing_for_member
10190 + function_definition_after_declarator
10191 + ctor_initializer_opt_and_function_body */
10193 tree fco = lambda_function (lambda_expr);
10194 tree body;
10195 bool done = false;
10196 tree compound_stmt;
10197 tree cap;
10199 /* Let the front end know that we are going to be defining this
10200 function. */
10201 start_preparsed_function (fco,
10202 NULL_TREE,
10203 SF_PRE_PARSED | SF_INCLASS_INLINE);
10205 start_lambda_scope (fco);
10206 body = begin_function_body ();
10208 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10209 goto out;
10211 /* Push the proxies for any explicit captures. */
10212 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
10213 cap = TREE_CHAIN (cap))
10214 build_capture_proxy (TREE_PURPOSE (cap));
10216 compound_stmt = begin_compound_stmt (0);
10218 /* 5.1.1.4 of the standard says:
10219 If a lambda-expression does not include a trailing-return-type, it
10220 is as if the trailing-return-type denotes the following type:
10221 * if the compound-statement is of the form
10222 { return attribute-specifier [opt] expression ; }
10223 the type of the returned expression after lvalue-to-rvalue
10224 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10225 (_conv.array_ 4.2), and function-to-pointer conversion
10226 (_conv.func_ 4.3);
10227 * otherwise, void. */
10229 /* In a lambda that has neither a lambda-return-type-clause
10230 nor a deducible form, errors should be reported for return statements
10231 in the body. Since we used void as the placeholder return type, parsing
10232 the body as usual will give such desired behavior. */
10233 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10234 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
10235 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
10237 tree expr = NULL_TREE;
10238 cp_id_kind idk = CP_ID_KIND_NONE;
10240 /* Parse tentatively in case there's more after the initial return
10241 statement. */
10242 cp_parser_parse_tentatively (parser);
10244 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
10246 expr = cp_parser_expression (parser, &idk);
10248 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10249 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10251 if (cp_parser_parse_definitely (parser))
10253 if (!processing_template_decl)
10255 tree type = lambda_return_type (expr);
10256 apply_deduced_return_type (fco, type);
10257 if (type == error_mark_node)
10258 expr = error_mark_node;
10261 /* Will get error here if type not deduced yet. */
10262 finish_return_stmt (expr);
10264 done = true;
10268 if (!done)
10270 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10271 cp_parser_label_declaration (parser);
10272 cp_parser_statement_seq_opt (parser, NULL_TREE);
10273 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10276 finish_compound_stmt (compound_stmt);
10278 out:
10279 finish_function_body (body);
10280 finish_lambda_scope ();
10282 /* Finish the function and generate code for it if necessary. */
10283 tree fn = finish_function (/*inline*/2);
10285 /* Only expand if the call op is not a template. */
10286 if (!DECL_TEMPLATE_INFO (fco))
10287 expand_or_defer_fn (fn);
10290 restore_omp_privatization_clauses (omp_privatization_save);
10291 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10292 if (nested)
10293 pop_function_context();
10294 else
10295 --function_depth;
10298 /* Statements [gram.stmt.stmt] */
10300 /* Parse a statement.
10302 statement:
10303 labeled-statement
10304 expression-statement
10305 compound-statement
10306 selection-statement
10307 iteration-statement
10308 jump-statement
10309 declaration-statement
10310 try-block
10312 C++11:
10314 statement:
10315 labeled-statement
10316 attribute-specifier-seq (opt) expression-statement
10317 attribute-specifier-seq (opt) compound-statement
10318 attribute-specifier-seq (opt) selection-statement
10319 attribute-specifier-seq (opt) iteration-statement
10320 attribute-specifier-seq (opt) jump-statement
10321 declaration-statement
10322 attribute-specifier-seq (opt) try-block
10324 TM Extension:
10326 statement:
10327 atomic-statement
10329 IN_COMPOUND is true when the statement is nested inside a
10330 cp_parser_compound_statement; this matters for certain pragmas.
10332 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10333 is a (possibly labeled) if statement which is not enclosed in braces
10334 and has an else clause. This is used to implement -Wparentheses.
10336 CHAIN is a vector of if-else-if conditions. */
10338 static void
10339 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10340 bool in_compound, bool *if_p, vec<tree> *chain)
10342 tree statement, std_attrs = NULL_TREE;
10343 cp_token *token;
10344 location_t statement_location, attrs_location;
10346 restart:
10347 if (if_p != NULL)
10348 *if_p = false;
10349 /* There is no statement yet. */
10350 statement = NULL_TREE;
10352 saved_token_sentinel saved_tokens (parser->lexer);
10353 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10354 if (c_dialect_objc ())
10355 /* In obj-c++, seeing '[[' might be the either the beginning of
10356 c++11 attributes, or a nested objc-message-expression. So
10357 let's parse the c++11 attributes tentatively. */
10358 cp_parser_parse_tentatively (parser);
10359 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10360 if (c_dialect_objc ())
10362 if (!cp_parser_parse_definitely (parser))
10363 std_attrs = NULL_TREE;
10366 /* Peek at the next token. */
10367 token = cp_lexer_peek_token (parser->lexer);
10368 /* Remember the location of the first token in the statement. */
10369 statement_location = token->location;
10370 /* If this is a keyword, then that will often determine what kind of
10371 statement we have. */
10372 if (token->type == CPP_KEYWORD)
10374 enum rid keyword = token->keyword;
10376 switch (keyword)
10378 case RID_CASE:
10379 case RID_DEFAULT:
10380 /* Looks like a labeled-statement with a case label.
10381 Parse the label, and then use tail recursion to parse
10382 the statement. */
10383 cp_parser_label_for_labeled_statement (parser, std_attrs);
10384 in_compound = false;
10385 goto restart;
10387 case RID_IF:
10388 case RID_SWITCH:
10389 statement = cp_parser_selection_statement (parser, if_p, chain);
10390 break;
10392 case RID_WHILE:
10393 case RID_DO:
10394 case RID_FOR:
10395 statement = cp_parser_iteration_statement (parser, false);
10396 break;
10398 case RID_CILK_FOR:
10399 if (!flag_cilkplus)
10401 error_at (cp_lexer_peek_token (parser->lexer)->location,
10402 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10403 cp_lexer_consume_token (parser->lexer);
10404 statement = error_mark_node;
10406 else
10407 statement = cp_parser_cilk_for (parser, integer_zero_node);
10408 break;
10410 case RID_BREAK:
10411 case RID_CONTINUE:
10412 case RID_RETURN:
10413 case RID_GOTO:
10414 statement = cp_parser_jump_statement (parser);
10415 break;
10417 case RID_CILK_SYNC:
10418 cp_lexer_consume_token (parser->lexer);
10419 if (flag_cilkplus)
10421 tree sync_expr = build_cilk_sync ();
10422 SET_EXPR_LOCATION (sync_expr,
10423 token->location);
10424 statement = finish_expr_stmt (sync_expr);
10426 else
10428 error_at (token->location, "-fcilkplus must be enabled to use"
10429 " %<_Cilk_sync%>");
10430 statement = error_mark_node;
10432 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10433 break;
10435 /* Objective-C++ exception-handling constructs. */
10436 case RID_AT_TRY:
10437 case RID_AT_CATCH:
10438 case RID_AT_FINALLY:
10439 case RID_AT_SYNCHRONIZED:
10440 case RID_AT_THROW:
10441 statement = cp_parser_objc_statement (parser);
10442 break;
10444 case RID_TRY:
10445 statement = cp_parser_try_block (parser);
10446 break;
10448 case RID_NAMESPACE:
10449 /* This must be a namespace alias definition. */
10450 cp_parser_declaration_statement (parser);
10451 return;
10453 case RID_TRANSACTION_ATOMIC:
10454 case RID_TRANSACTION_RELAXED:
10455 case RID_SYNCHRONIZED:
10456 case RID_ATOMIC_NOEXCEPT:
10457 case RID_ATOMIC_CANCEL:
10458 statement = cp_parser_transaction (parser, token);
10459 break;
10460 case RID_TRANSACTION_CANCEL:
10461 statement = cp_parser_transaction_cancel (parser);
10462 break;
10464 default:
10465 /* It might be a keyword like `int' that can start a
10466 declaration-statement. */
10467 break;
10470 else if (token->type == CPP_NAME)
10472 /* If the next token is a `:', then we are looking at a
10473 labeled-statement. */
10474 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10475 if (token->type == CPP_COLON)
10477 /* Looks like a labeled-statement with an ordinary label.
10478 Parse the label, and then use tail recursion to parse
10479 the statement. */
10481 cp_parser_label_for_labeled_statement (parser, std_attrs);
10482 in_compound = false;
10483 goto restart;
10486 /* Anything that starts with a `{' must be a compound-statement. */
10487 else if (token->type == CPP_OPEN_BRACE)
10488 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10489 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10490 a statement all its own. */
10491 else if (token->type == CPP_PRAGMA)
10493 /* Only certain OpenMP pragmas are attached to statements, and thus
10494 are considered statements themselves. All others are not. In
10495 the context of a compound, accept the pragma as a "statement" and
10496 return so that we can check for a close brace. Otherwise we
10497 require a real statement and must go back and read one. */
10498 if (in_compound)
10499 cp_parser_pragma (parser, pragma_compound);
10500 else if (!cp_parser_pragma (parser, pragma_stmt))
10501 goto restart;
10502 return;
10504 else if (token->type == CPP_EOF)
10506 cp_parser_error (parser, "expected statement");
10507 return;
10510 /* Everything else must be a declaration-statement or an
10511 expression-statement. Try for the declaration-statement
10512 first, unless we are looking at a `;', in which case we know that
10513 we have an expression-statement. */
10514 if (!statement)
10516 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10518 if (std_attrs != NULL_TREE)
10520 /* Attributes should be parsed as part of the the
10521 declaration, so let's un-parse them. */
10522 saved_tokens.rollback();
10523 std_attrs = NULL_TREE;
10526 cp_parser_parse_tentatively (parser);
10527 /* Try to parse the declaration-statement. */
10528 cp_parser_declaration_statement (parser);
10529 /* If that worked, we're done. */
10530 if (cp_parser_parse_definitely (parser))
10531 return;
10533 /* Look for an expression-statement instead. */
10534 statement = cp_parser_expression_statement (parser, in_statement_expr);
10537 /* Set the line number for the statement. */
10538 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10539 SET_EXPR_LOCATION (statement, statement_location);
10541 /* Note that for now, we don't do anything with c++11 statements
10542 parsed at this level. */
10543 if (std_attrs != NULL_TREE)
10544 warning_at (attrs_location,
10545 OPT_Wattributes,
10546 "attributes at the beginning of statement are ignored");
10549 /* Parse the label for a labeled-statement, i.e.
10551 identifier :
10552 case constant-expression :
10553 default :
10555 GNU Extension:
10556 case constant-expression ... constant-expression : statement
10558 When a label is parsed without errors, the label is added to the
10559 parse tree by the finish_* functions, so this function doesn't
10560 have to return the label. */
10562 static void
10563 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10565 cp_token *token;
10566 tree label = NULL_TREE;
10567 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10569 /* The next token should be an identifier. */
10570 token = cp_lexer_peek_token (parser->lexer);
10571 if (token->type != CPP_NAME
10572 && token->type != CPP_KEYWORD)
10574 cp_parser_error (parser, "expected labeled-statement");
10575 return;
10578 parser->colon_corrects_to_scope_p = false;
10579 switch (token->keyword)
10581 case RID_CASE:
10583 tree expr, expr_hi;
10584 cp_token *ellipsis;
10586 /* Consume the `case' token. */
10587 cp_lexer_consume_token (parser->lexer);
10588 /* Parse the constant-expression. */
10589 expr = cp_parser_constant_expression (parser);
10590 if (check_for_bare_parameter_packs (expr))
10591 expr = error_mark_node;
10593 ellipsis = cp_lexer_peek_token (parser->lexer);
10594 if (ellipsis->type == CPP_ELLIPSIS)
10596 /* Consume the `...' token. */
10597 cp_lexer_consume_token (parser->lexer);
10598 expr_hi = cp_parser_constant_expression (parser);
10599 if (check_for_bare_parameter_packs (expr_hi))
10600 expr_hi = error_mark_node;
10602 /* We don't need to emit warnings here, as the common code
10603 will do this for us. */
10605 else
10606 expr_hi = NULL_TREE;
10608 if (parser->in_switch_statement_p)
10609 finish_case_label (token->location, expr, expr_hi);
10610 else
10611 error_at (token->location,
10612 "case label %qE not within a switch statement",
10613 expr);
10615 break;
10617 case RID_DEFAULT:
10618 /* Consume the `default' token. */
10619 cp_lexer_consume_token (parser->lexer);
10621 if (parser->in_switch_statement_p)
10622 finish_case_label (token->location, NULL_TREE, NULL_TREE);
10623 else
10624 error_at (token->location, "case label not within a switch statement");
10625 break;
10627 default:
10628 /* Anything else must be an ordinary label. */
10629 label = finish_label_stmt (cp_parser_identifier (parser));
10630 break;
10633 /* Require the `:' token. */
10634 cp_parser_require (parser, CPP_COLON, RT_COLON);
10636 /* An ordinary label may optionally be followed by attributes.
10637 However, this is only permitted if the attributes are then
10638 followed by a semicolon. This is because, for backward
10639 compatibility, when parsing
10640 lab: __attribute__ ((unused)) int i;
10641 we want the attribute to attach to "i", not "lab". */
10642 if (label != NULL_TREE
10643 && cp_next_tokens_can_be_gnu_attribute_p (parser))
10645 tree attrs;
10646 cp_parser_parse_tentatively (parser);
10647 attrs = cp_parser_gnu_attributes_opt (parser);
10648 if (attrs == NULL_TREE
10649 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10650 cp_parser_abort_tentative_parse (parser);
10651 else if (!cp_parser_parse_definitely (parser))
10653 else
10654 attributes = chainon (attributes, attrs);
10657 if (attributes != NULL_TREE)
10658 cplus_decl_attributes (&label, attributes, 0);
10660 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10663 /* Parse an expression-statement.
10665 expression-statement:
10666 expression [opt] ;
10668 Returns the new EXPR_STMT -- or NULL_TREE if the expression
10669 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
10670 indicates whether this expression-statement is part of an
10671 expression statement. */
10673 static tree
10674 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
10676 tree statement = NULL_TREE;
10677 cp_token *token = cp_lexer_peek_token (parser->lexer);
10679 /* If the next token is a ';', then there is no expression
10680 statement. */
10681 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10683 statement = cp_parser_expression (parser);
10684 if (statement == error_mark_node
10685 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10687 cp_parser_skip_to_end_of_block_or_statement (parser);
10688 return error_mark_node;
10692 /* Give a helpful message for "A<T>::type t;" and the like. */
10693 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
10694 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10696 if (TREE_CODE (statement) == SCOPE_REF)
10697 error_at (token->location, "need %<typename%> before %qE because "
10698 "%qT is a dependent scope",
10699 statement, TREE_OPERAND (statement, 0));
10700 else if (is_overloaded_fn (statement)
10701 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
10703 /* A::A a; */
10704 tree fn = get_first_fn (statement);
10705 error_at (token->location,
10706 "%<%T::%D%> names the constructor, not the type",
10707 DECL_CONTEXT (fn), DECL_NAME (fn));
10711 /* Consume the final `;'. */
10712 cp_parser_consume_semicolon_at_end_of_statement (parser);
10714 if (in_statement_expr
10715 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10716 /* This is the final expression statement of a statement
10717 expression. */
10718 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10719 else if (statement)
10720 statement = finish_expr_stmt (statement);
10722 return statement;
10725 /* Parse a compound-statement.
10727 compound-statement:
10728 { statement-seq [opt] }
10730 GNU extension:
10732 compound-statement:
10733 { label-declaration-seq [opt] statement-seq [opt] }
10735 label-declaration-seq:
10736 label-declaration
10737 label-declaration-seq label-declaration
10739 Returns a tree representing the statement. */
10741 static tree
10742 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10743 int bcs_flags, bool function_body)
10745 tree compound_stmt;
10747 /* Consume the `{'. */
10748 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10749 return error_mark_node;
10750 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10751 && !function_body && cxx_dialect < cxx14)
10752 pedwarn (input_location, OPT_Wpedantic,
10753 "compound-statement in constexpr function");
10754 /* Begin the compound-statement. */
10755 compound_stmt = begin_compound_stmt (bcs_flags);
10756 /* If the next keyword is `__label__' we have a label declaration. */
10757 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10758 cp_parser_label_declaration (parser);
10759 /* Parse an (optional) statement-seq. */
10760 cp_parser_statement_seq_opt (parser, in_statement_expr);
10761 /* Finish the compound-statement. */
10762 finish_compound_stmt (compound_stmt);
10763 /* Consume the `}'. */
10764 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10766 return compound_stmt;
10769 /* Parse an (optional) statement-seq.
10771 statement-seq:
10772 statement
10773 statement-seq [opt] statement */
10775 static void
10776 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10778 /* Scan statements until there aren't any more. */
10779 while (true)
10781 cp_token *token = cp_lexer_peek_token (parser->lexer);
10783 /* If we are looking at a `}', then we have run out of
10784 statements; the same is true if we have reached the end
10785 of file, or have stumbled upon a stray '@end'. */
10786 if (token->type == CPP_CLOSE_BRACE
10787 || token->type == CPP_EOF
10788 || token->type == CPP_PRAGMA_EOL
10789 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10790 break;
10792 /* If we are in a compound statement and find 'else' then
10793 something went wrong. */
10794 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10796 if (parser->in_statement & IN_IF_STMT)
10797 break;
10798 else
10800 token = cp_lexer_consume_token (parser->lexer);
10801 error_at (token->location, "%<else%> without a previous %<if%>");
10805 /* Parse the statement. */
10806 cp_parser_statement (parser, in_statement_expr, true, NULL);
10810 /* Parse a selection-statement.
10812 selection-statement:
10813 if ( condition ) statement
10814 if ( condition ) statement else statement
10815 switch ( condition ) statement
10817 Returns the new IF_STMT or SWITCH_STMT.
10819 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10820 is a (possibly labeled) if statement which is not enclosed in
10821 braces and has an else clause. This is used to implement
10822 -Wparentheses.
10824 CHAIN is a vector of if-else-if conditions. This is used to implement
10825 -Wduplicated-cond. */
10827 static tree
10828 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
10829 vec<tree> *chain)
10831 cp_token *token;
10832 enum rid keyword;
10833 token_indent_info guard_tinfo;
10835 if (if_p != NULL)
10836 *if_p = false;
10838 /* Peek at the next token. */
10839 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10840 guard_tinfo = get_token_indent_info (token);
10842 /* See what kind of keyword it is. */
10843 keyword = token->keyword;
10844 switch (keyword)
10846 case RID_IF:
10847 case RID_SWITCH:
10849 tree statement;
10850 tree condition;
10852 /* Look for the `('. */
10853 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10855 cp_parser_skip_to_end_of_statement (parser);
10856 return error_mark_node;
10859 /* Begin the selection-statement. */
10860 if (keyword == RID_IF)
10861 statement = begin_if_stmt ();
10862 else
10863 statement = begin_switch_stmt ();
10865 /* Parse the condition. */
10866 condition = cp_parser_condition (parser);
10867 /* Look for the `)'. */
10868 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10869 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10870 /*consume_paren=*/true);
10872 if (keyword == RID_IF)
10874 bool nested_if;
10875 unsigned char in_statement;
10877 /* Add the condition. */
10878 finish_if_stmt_cond (condition, statement);
10880 if (warn_duplicated_cond)
10881 warn_duplicated_cond_add_or_warn (token->location, condition,
10882 &chain);
10884 /* Parse the then-clause. */
10885 in_statement = parser->in_statement;
10886 parser->in_statement |= IN_IF_STMT;
10887 cp_parser_implicitly_scoped_statement (parser, &nested_if,
10888 guard_tinfo);
10889 parser->in_statement = in_statement;
10891 finish_then_clause (statement);
10893 /* If the next token is `else', parse the else-clause. */
10894 if (cp_lexer_next_token_is_keyword (parser->lexer,
10895 RID_ELSE))
10897 guard_tinfo
10898 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
10899 /* Consume the `else' keyword. */
10900 cp_lexer_consume_token (parser->lexer);
10901 if (warn_duplicated_cond)
10903 if (cp_lexer_next_token_is_keyword (parser->lexer,
10904 RID_IF)
10905 && chain == NULL)
10907 /* We've got "if (COND) else if (COND2)". Start
10908 the condition chain and add COND as the first
10909 element. */
10910 chain = new vec<tree> ();
10911 if (!CONSTANT_CLASS_P (condition)
10912 && !TREE_SIDE_EFFECTS (condition))
10914 /* Wrap it in a NOP_EXPR so that we can set the
10915 location of the condition. */
10916 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
10917 condition);
10918 SET_EXPR_LOCATION (e, token->location);
10919 chain->safe_push (e);
10922 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
10923 RID_IF))
10925 /* This is if-else without subsequent if. Zap the
10926 condition chain; we would have already warned at
10927 this point. */
10928 delete chain;
10929 chain = NULL;
10932 begin_else_clause (statement);
10933 /* Parse the else-clause. */
10934 cp_parser_implicitly_scoped_statement (parser, NULL,
10935 guard_tinfo, chain);
10937 finish_else_clause (statement);
10939 /* If we are currently parsing a then-clause, then
10940 IF_P will not be NULL. We set it to true to
10941 indicate that this if statement has an else clause.
10942 This may trigger the Wparentheses warning below
10943 when we get back up to the parent if statement. */
10944 if (if_p != NULL)
10945 *if_p = true;
10947 else
10949 /* This if statement does not have an else clause. If
10950 NESTED_IF is true, then the then-clause is an if
10951 statement which does have an else clause. We warn
10952 about the potential ambiguity. */
10953 if (nested_if)
10954 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10955 "suggest explicit braces to avoid ambiguous"
10956 " %<else%>");
10957 if (warn_duplicated_cond)
10959 /* We don't need the condition chain anymore. */
10960 delete chain;
10961 chain = NULL;
10965 /* Now we're all done with the if-statement. */
10966 finish_if_stmt (statement);
10968 else
10970 bool in_switch_statement_p;
10971 unsigned char in_statement;
10973 /* Add the condition. */
10974 finish_switch_cond (condition, statement);
10976 /* Parse the body of the switch-statement. */
10977 in_switch_statement_p = parser->in_switch_statement_p;
10978 in_statement = parser->in_statement;
10979 parser->in_switch_statement_p = true;
10980 parser->in_statement |= IN_SWITCH_STMT;
10981 cp_parser_implicitly_scoped_statement (parser, NULL,
10982 guard_tinfo);
10983 parser->in_switch_statement_p = in_switch_statement_p;
10984 parser->in_statement = in_statement;
10986 /* Now we're all done with the switch-statement. */
10987 finish_switch_stmt (statement);
10990 return statement;
10992 break;
10994 default:
10995 cp_parser_error (parser, "expected selection-statement");
10996 return error_mark_node;
11000 /* Parse a condition.
11002 condition:
11003 expression
11004 type-specifier-seq declarator = initializer-clause
11005 type-specifier-seq declarator braced-init-list
11007 GNU Extension:
11009 condition:
11010 type-specifier-seq declarator asm-specification [opt]
11011 attributes [opt] = assignment-expression
11013 Returns the expression that should be tested. */
11015 static tree
11016 cp_parser_condition (cp_parser* parser)
11018 cp_decl_specifier_seq type_specifiers;
11019 const char *saved_message;
11020 int declares_class_or_enum;
11022 /* Try the declaration first. */
11023 cp_parser_parse_tentatively (parser);
11024 /* New types are not allowed in the type-specifier-seq for a
11025 condition. */
11026 saved_message = parser->type_definition_forbidden_message;
11027 parser->type_definition_forbidden_message
11028 = G_("types may not be defined in conditions");
11029 /* Parse the type-specifier-seq. */
11030 cp_parser_decl_specifier_seq (parser,
11031 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11032 &type_specifiers,
11033 &declares_class_or_enum);
11034 /* Restore the saved message. */
11035 parser->type_definition_forbidden_message = saved_message;
11036 /* If all is well, we might be looking at a declaration. */
11037 if (!cp_parser_error_occurred (parser))
11039 tree decl;
11040 tree asm_specification;
11041 tree attributes;
11042 cp_declarator *declarator;
11043 tree initializer = NULL_TREE;
11045 /* Parse the declarator. */
11046 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11047 /*ctor_dtor_or_conv_p=*/NULL,
11048 /*parenthesized_p=*/NULL,
11049 /*member_p=*/false,
11050 /*friend_p=*/false);
11051 /* Parse the attributes. */
11052 attributes = cp_parser_attributes_opt (parser);
11053 /* Parse the asm-specification. */
11054 asm_specification = cp_parser_asm_specification_opt (parser);
11055 /* If the next token is not an `=' or '{', then we might still be
11056 looking at an expression. For example:
11058 if (A(a).x)
11060 looks like a decl-specifier-seq and a declarator -- but then
11061 there is no `=', so this is an expression. */
11062 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11063 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11064 cp_parser_simulate_error (parser);
11066 /* If we did see an `=' or '{', then we are looking at a declaration
11067 for sure. */
11068 if (cp_parser_parse_definitely (parser))
11070 tree pushed_scope;
11071 bool non_constant_p;
11072 bool flags = LOOKUP_ONLYCONVERTING;
11074 /* Create the declaration. */
11075 decl = start_decl (declarator, &type_specifiers,
11076 /*initialized_p=*/true,
11077 attributes, /*prefix_attributes=*/NULL_TREE,
11078 &pushed_scope);
11080 /* Parse the initializer. */
11081 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11083 initializer = cp_parser_braced_list (parser, &non_constant_p);
11084 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11085 flags = 0;
11087 else
11089 /* Consume the `='. */
11090 cp_parser_require (parser, CPP_EQ, RT_EQ);
11091 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11093 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11094 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11096 /* Process the initializer. */
11097 cp_finish_decl (decl,
11098 initializer, !non_constant_p,
11099 asm_specification,
11100 flags);
11102 if (pushed_scope)
11103 pop_scope (pushed_scope);
11105 return convert_from_reference (decl);
11108 /* If we didn't even get past the declarator successfully, we are
11109 definitely not looking at a declaration. */
11110 else
11111 cp_parser_abort_tentative_parse (parser);
11113 /* Otherwise, we are looking at an expression. */
11114 return cp_parser_expression (parser);
11117 /* Parses a for-statement or range-for-statement until the closing ')',
11118 not included. */
11120 static tree
11121 cp_parser_for (cp_parser *parser, bool ivdep)
11123 tree init, scope, decl;
11124 bool is_range_for;
11126 /* Begin the for-statement. */
11127 scope = begin_for_scope (&init);
11129 /* Parse the initialization. */
11130 is_range_for = cp_parser_for_init_statement (parser, &decl);
11132 if (is_range_for)
11133 return cp_parser_range_for (parser, scope, init, decl, ivdep);
11134 else
11135 return cp_parser_c_for (parser, scope, init, ivdep);
11138 static tree
11139 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11141 /* Normal for loop */
11142 tree condition = NULL_TREE;
11143 tree expression = NULL_TREE;
11144 tree stmt;
11146 stmt = begin_for_stmt (scope, init);
11147 /* The for-init-statement has already been parsed in
11148 cp_parser_for_init_statement, so no work is needed here. */
11149 finish_for_init_stmt (stmt);
11151 /* If there's a condition, process it. */
11152 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11153 condition = cp_parser_condition (parser);
11154 else if (ivdep)
11156 cp_parser_error (parser, "missing loop condition in loop with "
11157 "%<GCC ivdep%> pragma");
11158 condition = error_mark_node;
11160 finish_for_cond (condition, stmt, ivdep);
11161 /* Look for the `;'. */
11162 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11164 /* If there's an expression, process it. */
11165 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11166 expression = cp_parser_expression (parser);
11167 finish_for_expr (expression, stmt);
11169 return stmt;
11172 /* Tries to parse a range-based for-statement:
11174 range-based-for:
11175 decl-specifier-seq declarator : expression
11177 The decl-specifier-seq declarator and the `:' are already parsed by
11178 cp_parser_for_init_statement. If processing_template_decl it returns a
11179 newly created RANGE_FOR_STMT; if not, it is converted to a
11180 regular FOR_STMT. */
11182 static tree
11183 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11184 bool ivdep)
11186 tree stmt, range_expr;
11188 /* Get the range declaration momentarily out of the way so that
11189 the range expression doesn't clash with it. */
11190 if (range_decl != error_mark_node)
11191 pop_binding (DECL_NAME (range_decl), range_decl);
11193 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11195 bool expr_non_constant_p;
11196 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11198 else
11199 range_expr = cp_parser_expression (parser);
11201 /* Put the range declaration back into scope. */
11202 if (range_decl != error_mark_node)
11203 push_binding (DECL_NAME (range_decl), range_decl, current_binding_level);
11205 /* If in template, STMT is converted to a normal for-statement
11206 at instantiation. If not, it is done just ahead. */
11207 if (processing_template_decl)
11209 if (check_for_bare_parameter_packs (range_expr))
11210 range_expr = error_mark_node;
11211 stmt = begin_range_for_stmt (scope, init);
11212 if (ivdep)
11213 RANGE_FOR_IVDEP (stmt) = 1;
11214 finish_range_for_decl (stmt, range_decl, range_expr);
11215 if (!type_dependent_expression_p (range_expr)
11216 /* do_auto_deduction doesn't mess with template init-lists. */
11217 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11218 do_range_for_auto_deduction (range_decl, range_expr);
11220 else
11222 stmt = begin_for_stmt (scope, init);
11223 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
11225 return stmt;
11228 /* Subroutine of cp_convert_range_for: given the initializer expression,
11229 builds up the range temporary. */
11231 static tree
11232 build_range_temp (tree range_expr)
11234 tree range_type, range_temp;
11236 /* Find out the type deduced by the declaration
11237 `auto &&__range = range_expr'. */
11238 range_type = cp_build_reference_type (make_auto (), true);
11239 range_type = do_auto_deduction (range_type, range_expr,
11240 type_uses_auto (range_type));
11242 /* Create the __range variable. */
11243 range_temp = build_decl (input_location, VAR_DECL,
11244 get_identifier ("__for_range"), range_type);
11245 TREE_USED (range_temp) = 1;
11246 DECL_ARTIFICIAL (range_temp) = 1;
11248 return range_temp;
11251 /* Used by cp_parser_range_for in template context: we aren't going to
11252 do a full conversion yet, but we still need to resolve auto in the
11253 type of the for-range-declaration if present. This is basically
11254 a shortcut version of cp_convert_range_for. */
11256 static void
11257 do_range_for_auto_deduction (tree decl, tree range_expr)
11259 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11260 if (auto_node)
11262 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11263 range_temp = convert_from_reference (build_range_temp (range_expr));
11264 iter_type = (cp_parser_perform_range_for_lookup
11265 (range_temp, &begin_dummy, &end_dummy));
11266 if (iter_type)
11268 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11269 iter_type);
11270 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
11271 tf_warning_or_error);
11272 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11273 iter_decl, auto_node);
11278 /* Converts a range-based for-statement into a normal
11279 for-statement, as per the definition.
11281 for (RANGE_DECL : RANGE_EXPR)
11282 BLOCK
11284 should be equivalent to:
11287 auto &&__range = RANGE_EXPR;
11288 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11289 __begin != __end;
11290 ++__begin)
11292 RANGE_DECL = *__begin;
11293 BLOCK
11297 If RANGE_EXPR is an array:
11298 BEGIN_EXPR = __range
11299 END_EXPR = __range + ARRAY_SIZE(__range)
11300 Else if RANGE_EXPR has a member 'begin' or 'end':
11301 BEGIN_EXPR = __range.begin()
11302 END_EXPR = __range.end()
11303 Else:
11304 BEGIN_EXPR = begin(__range)
11305 END_EXPR = end(__range);
11307 If __range has a member 'begin' but not 'end', or vice versa, we must
11308 still use the second alternative (it will surely fail, however).
11309 When calling begin()/end() in the third alternative we must use
11310 argument dependent lookup, but always considering 'std' as an associated
11311 namespace. */
11313 tree
11314 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11315 bool ivdep)
11317 tree begin, end;
11318 tree iter_type, begin_expr, end_expr;
11319 tree condition, expression;
11321 if (range_decl == error_mark_node || range_expr == error_mark_node)
11322 /* If an error happened previously do nothing or else a lot of
11323 unhelpful errors would be issued. */
11324 begin_expr = end_expr = iter_type = error_mark_node;
11325 else
11327 tree range_temp;
11329 if (VAR_P (range_expr)
11330 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11331 /* Can't bind a reference to an array of runtime bound. */
11332 range_temp = range_expr;
11333 else
11335 range_temp = build_range_temp (range_expr);
11336 pushdecl (range_temp);
11337 cp_finish_decl (range_temp, range_expr,
11338 /*is_constant_init*/false, NULL_TREE,
11339 LOOKUP_ONLYCONVERTING);
11340 range_temp = convert_from_reference (range_temp);
11342 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11343 &begin_expr, &end_expr);
11346 /* The new for initialization statement. */
11347 begin = build_decl (input_location, VAR_DECL,
11348 get_identifier ("__for_begin"), iter_type);
11349 TREE_USED (begin) = 1;
11350 DECL_ARTIFICIAL (begin) = 1;
11351 pushdecl (begin);
11352 cp_finish_decl (begin, begin_expr,
11353 /*is_constant_init*/false, NULL_TREE,
11354 LOOKUP_ONLYCONVERTING);
11356 end = build_decl (input_location, VAR_DECL,
11357 get_identifier ("__for_end"), iter_type);
11358 TREE_USED (end) = 1;
11359 DECL_ARTIFICIAL (end) = 1;
11360 pushdecl (end);
11361 cp_finish_decl (end, end_expr,
11362 /*is_constant_init*/false, NULL_TREE,
11363 LOOKUP_ONLYCONVERTING);
11365 finish_for_init_stmt (statement);
11367 /* The new for condition. */
11368 condition = build_x_binary_op (input_location, NE_EXPR,
11369 begin, ERROR_MARK,
11370 end, ERROR_MARK,
11371 NULL, tf_warning_or_error);
11372 finish_for_cond (condition, statement, ivdep);
11374 /* The new increment expression. */
11375 expression = finish_unary_op_expr (input_location,
11376 PREINCREMENT_EXPR, begin,
11377 tf_warning_or_error);
11378 finish_for_expr (expression, statement);
11380 /* The declaration is initialized with *__begin inside the loop body. */
11381 cp_finish_decl (range_decl,
11382 build_x_indirect_ref (input_location, begin, RO_NULL,
11383 tf_warning_or_error),
11384 /*is_constant_init*/false, NULL_TREE,
11385 LOOKUP_ONLYCONVERTING);
11387 return statement;
11390 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11391 We need to solve both at the same time because the method used
11392 depends on the existence of members begin or end.
11393 Returns the type deduced for the iterator expression. */
11395 static tree
11396 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11398 if (error_operand_p (range))
11400 *begin = *end = error_mark_node;
11401 return error_mark_node;
11404 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11406 error ("range-based %<for%> expression of type %qT "
11407 "has incomplete type", TREE_TYPE (range));
11408 *begin = *end = error_mark_node;
11409 return error_mark_node;
11411 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11413 /* If RANGE is an array, we will use pointer arithmetic. */
11414 *begin = range;
11415 *end = build_binary_op (input_location, PLUS_EXPR,
11416 range,
11417 array_type_nelts_top (TREE_TYPE (range)),
11419 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
11421 else
11423 /* If it is not an array, we must do a bit of magic. */
11424 tree id_begin, id_end;
11425 tree member_begin, member_end;
11427 *begin = *end = error_mark_node;
11429 id_begin = get_identifier ("begin");
11430 id_end = get_identifier ("end");
11431 member_begin = lookup_member (TREE_TYPE (range), id_begin,
11432 /*protect=*/2, /*want_type=*/false,
11433 tf_warning_or_error);
11434 member_end = lookup_member (TREE_TYPE (range), id_end,
11435 /*protect=*/2, /*want_type=*/false,
11436 tf_warning_or_error);
11438 if (member_begin != NULL_TREE || member_end != NULL_TREE)
11440 /* Use the member functions. */
11441 if (member_begin != NULL_TREE)
11442 *begin = cp_parser_range_for_member_function (range, id_begin);
11443 else
11444 error ("range-based %<for%> expression of type %qT has an "
11445 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11447 if (member_end != NULL_TREE)
11448 *end = cp_parser_range_for_member_function (range, id_end);
11449 else
11450 error ("range-based %<for%> expression of type %qT has a "
11451 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
11453 else
11455 /* Use global functions with ADL. */
11456 vec<tree, va_gc> *vec;
11457 vec = make_tree_vector ();
11459 vec_safe_push (vec, range);
11461 member_begin = perform_koenig_lookup (id_begin, vec,
11462 tf_warning_or_error);
11463 *begin = finish_call_expr (member_begin, &vec, false, true,
11464 tf_warning_or_error);
11465 member_end = perform_koenig_lookup (id_end, vec,
11466 tf_warning_or_error);
11467 *end = finish_call_expr (member_end, &vec, false, true,
11468 tf_warning_or_error);
11470 release_tree_vector (vec);
11473 /* Last common checks. */
11474 if (*begin == error_mark_node || *end == error_mark_node)
11476 /* If one of the expressions is an error do no more checks. */
11477 *begin = *end = error_mark_node;
11478 return error_mark_node;
11480 else if (type_dependent_expression_p (*begin)
11481 || type_dependent_expression_p (*end))
11482 /* Can happen, when, eg, in a template context, Koenig lookup
11483 can't resolve begin/end (c++/58503). */
11484 return NULL_TREE;
11485 else
11487 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
11488 /* The unqualified type of the __begin and __end temporaries should
11489 be the same, as required by the multiple auto declaration. */
11490 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
11491 error ("inconsistent begin/end types in range-based %<for%> "
11492 "statement: %qT and %qT",
11493 TREE_TYPE (*begin), TREE_TYPE (*end));
11494 return iter_type;
11499 /* Helper function for cp_parser_perform_range_for_lookup.
11500 Builds a tree for RANGE.IDENTIFIER(). */
11502 static tree
11503 cp_parser_range_for_member_function (tree range, tree identifier)
11505 tree member, res;
11506 vec<tree, va_gc> *vec;
11508 member = finish_class_member_access_expr (range, identifier,
11509 false, tf_warning_or_error);
11510 if (member == error_mark_node)
11511 return error_mark_node;
11513 vec = make_tree_vector ();
11514 res = finish_call_expr (member, &vec,
11515 /*disallow_virtual=*/false,
11516 /*koenig_p=*/false,
11517 tf_warning_or_error);
11518 release_tree_vector (vec);
11519 return res;
11522 /* Parse an iteration-statement.
11524 iteration-statement:
11525 while ( condition ) statement
11526 do statement while ( expression ) ;
11527 for ( for-init-statement condition [opt] ; expression [opt] )
11528 statement
11530 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
11532 static tree
11533 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
11535 cp_token *token;
11536 enum rid keyword;
11537 tree statement;
11538 unsigned char in_statement;
11539 token_indent_info guard_tinfo;
11541 /* Peek at the next token. */
11542 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
11543 if (!token)
11544 return error_mark_node;
11546 guard_tinfo = get_token_indent_info (token);
11548 /* Remember whether or not we are already within an iteration
11549 statement. */
11550 in_statement = parser->in_statement;
11552 /* See what kind of keyword it is. */
11553 keyword = token->keyword;
11554 switch (keyword)
11556 case RID_WHILE:
11558 tree condition;
11560 /* Begin the while-statement. */
11561 statement = begin_while_stmt ();
11562 /* Look for the `('. */
11563 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11564 /* Parse the condition. */
11565 condition = cp_parser_condition (parser);
11566 finish_while_stmt_cond (condition, statement, ivdep);
11567 /* Look for the `)'. */
11568 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11569 /* Parse the dependent statement. */
11570 parser->in_statement = IN_ITERATION_STMT;
11571 cp_parser_already_scoped_statement (parser, guard_tinfo);
11572 parser->in_statement = in_statement;
11573 /* We're done with the while-statement. */
11574 finish_while_stmt (statement);
11576 break;
11578 case RID_DO:
11580 tree expression;
11582 /* Begin the do-statement. */
11583 statement = begin_do_stmt ();
11584 /* Parse the body of the do-statement. */
11585 parser->in_statement = IN_ITERATION_STMT;
11586 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
11587 parser->in_statement = in_statement;
11588 finish_do_body (statement);
11589 /* Look for the `while' keyword. */
11590 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
11591 /* Look for the `('. */
11592 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11593 /* Parse the expression. */
11594 expression = cp_parser_expression (parser);
11595 /* We're done with the do-statement. */
11596 finish_do_stmt (expression, statement, ivdep);
11597 /* Look for the `)'. */
11598 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11599 /* Look for the `;'. */
11600 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11602 break;
11604 case RID_FOR:
11606 /* Look for the `('. */
11607 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11609 statement = cp_parser_for (parser, ivdep);
11611 /* Look for the `)'. */
11612 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11614 /* Parse the body of the for-statement. */
11615 parser->in_statement = IN_ITERATION_STMT;
11616 cp_parser_already_scoped_statement (parser, guard_tinfo);
11617 parser->in_statement = in_statement;
11619 /* We're done with the for-statement. */
11620 finish_for_stmt (statement);
11622 break;
11624 default:
11625 cp_parser_error (parser, "expected iteration-statement");
11626 statement = error_mark_node;
11627 break;
11630 return statement;
11633 /* Parse a for-init-statement or the declarator of a range-based-for.
11634 Returns true if a range-based-for declaration is seen.
11636 for-init-statement:
11637 expression-statement
11638 simple-declaration */
11640 static bool
11641 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
11643 /* If the next token is a `;', then we have an empty
11644 expression-statement. Grammatically, this is also a
11645 simple-declaration, but an invalid one, because it does not
11646 declare anything. Therefore, if we did not handle this case
11647 specially, we would issue an error message about an invalid
11648 declaration. */
11649 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11651 bool is_range_for = false;
11652 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11654 /* A colon is used in range-based for. */
11655 parser->colon_corrects_to_scope_p = false;
11657 /* We're going to speculatively look for a declaration, falling back
11658 to an expression, if necessary. */
11659 cp_parser_parse_tentatively (parser);
11660 /* Parse the declaration. */
11661 cp_parser_simple_declaration (parser,
11662 /*function_definition_allowed_p=*/false,
11663 decl);
11664 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11665 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11667 /* It is a range-for, consume the ':' */
11668 cp_lexer_consume_token (parser->lexer);
11669 is_range_for = true;
11670 if (cxx_dialect < cxx11)
11672 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11673 "range-based %<for%> loops only available with "
11674 "-std=c++11 or -std=gnu++11");
11675 *decl = error_mark_node;
11678 else
11679 /* The ';' is not consumed yet because we told
11680 cp_parser_simple_declaration not to. */
11681 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11683 if (cp_parser_parse_definitely (parser))
11684 return is_range_for;
11685 /* If the tentative parse failed, then we shall need to look for an
11686 expression-statement. */
11688 /* If we are here, it is an expression-statement. */
11689 cp_parser_expression_statement (parser, NULL_TREE);
11690 return false;
11693 /* Parse a jump-statement.
11695 jump-statement:
11696 break ;
11697 continue ;
11698 return expression [opt] ;
11699 return braced-init-list ;
11700 goto identifier ;
11702 GNU extension:
11704 jump-statement:
11705 goto * expression ;
11707 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
11709 static tree
11710 cp_parser_jump_statement (cp_parser* parser)
11712 tree statement = error_mark_node;
11713 cp_token *token;
11714 enum rid keyword;
11715 unsigned char in_statement;
11717 /* Peek at the next token. */
11718 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
11719 if (!token)
11720 return error_mark_node;
11722 /* See what kind of keyword it is. */
11723 keyword = token->keyword;
11724 switch (keyword)
11726 case RID_BREAK:
11727 in_statement = parser->in_statement & ~IN_IF_STMT;
11728 switch (in_statement)
11730 case 0:
11731 error_at (token->location, "break statement not within loop or switch");
11732 break;
11733 default:
11734 gcc_assert ((in_statement & IN_SWITCH_STMT)
11735 || in_statement == IN_ITERATION_STMT);
11736 statement = finish_break_stmt ();
11737 if (in_statement == IN_ITERATION_STMT)
11738 break_maybe_infinite_loop ();
11739 break;
11740 case IN_OMP_BLOCK:
11741 error_at (token->location, "invalid exit from OpenMP structured block");
11742 break;
11743 case IN_OMP_FOR:
11744 error_at (token->location, "break statement used with OpenMP for loop");
11745 break;
11746 case IN_CILK_SIMD_FOR:
11747 error_at (token->location, "break statement used with Cilk Plus for loop");
11748 break;
11750 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11751 break;
11753 case RID_CONTINUE:
11754 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
11756 case 0:
11757 error_at (token->location, "continue statement not within a loop");
11758 break;
11759 case IN_CILK_SIMD_FOR:
11760 error_at (token->location,
11761 "continue statement within %<#pragma simd%> loop body");
11762 /* Fall through. */
11763 case IN_ITERATION_STMT:
11764 case IN_OMP_FOR:
11765 statement = finish_continue_stmt ();
11766 break;
11767 case IN_OMP_BLOCK:
11768 error_at (token->location, "invalid exit from OpenMP structured block");
11769 break;
11770 default:
11771 gcc_unreachable ();
11773 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11774 break;
11776 case RID_RETURN:
11778 tree expr;
11779 bool expr_non_constant_p;
11781 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11783 cp_lexer_set_source_position (parser->lexer);
11784 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11785 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11787 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11788 expr = cp_parser_expression (parser);
11789 else
11790 /* If the next token is a `;', then there is no
11791 expression. */
11792 expr = NULL_TREE;
11793 /* Build the return-statement. */
11794 statement = finish_return_stmt (expr);
11795 /* Look for the final `;'. */
11796 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11798 break;
11800 case RID_GOTO:
11801 if (parser->in_function_body
11802 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11804 error ("%<goto%> in %<constexpr%> function");
11805 cp_function_chain->invalid_constexpr = true;
11808 /* Create the goto-statement. */
11809 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11811 /* Issue a warning about this use of a GNU extension. */
11812 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11813 /* Consume the '*' token. */
11814 cp_lexer_consume_token (parser->lexer);
11815 /* Parse the dependent expression. */
11816 finish_goto_stmt (cp_parser_expression (parser));
11818 else
11819 finish_goto_stmt (cp_parser_identifier (parser));
11820 /* Look for the final `;'. */
11821 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11822 break;
11824 default:
11825 cp_parser_error (parser, "expected jump-statement");
11826 break;
11829 return statement;
11832 /* Parse a declaration-statement.
11834 declaration-statement:
11835 block-declaration */
11837 static void
11838 cp_parser_declaration_statement (cp_parser* parser)
11840 void *p;
11842 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11843 p = obstack_alloc (&declarator_obstack, 0);
11845 /* Parse the block-declaration. */
11846 cp_parser_block_declaration (parser, /*statement_p=*/true);
11848 /* Free any declarators allocated. */
11849 obstack_free (&declarator_obstack, p);
11852 /* Some dependent statements (like `if (cond) statement'), are
11853 implicitly in their own scope. In other words, if the statement is
11854 a single statement (as opposed to a compound-statement), it is
11855 none-the-less treated as if it were enclosed in braces. Any
11856 declarations appearing in the dependent statement are out of scope
11857 after control passes that point. This function parses a statement,
11858 but ensures that is in its own scope, even if it is not a
11859 compound-statement.
11861 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11862 is a (possibly labeled) if statement which is not enclosed in
11863 braces and has an else clause. This is used to implement
11864 -Wparentheses.
11866 CHAIN is a vector of if-else-if conditions. This is used to implement
11867 -Wduplicated-cond.
11869 Returns the new statement. */
11871 static tree
11872 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
11873 const token_indent_info &guard_tinfo,
11874 vec<tree> *chain)
11876 tree statement;
11877 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
11878 token_indent_info body_tinfo
11879 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11881 if (if_p != NULL)
11882 *if_p = false;
11884 /* Mark if () ; with a special NOP_EXPR. */
11885 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11887 cp_lexer_consume_token (parser->lexer);
11888 statement = add_stmt (build_empty_stmt (body_loc));
11890 if (guard_tinfo.keyword == RID_IF
11891 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
11892 warning_at (body_loc, OPT_Wempty_body,
11893 "suggest braces around empty body in an %<if%> statement");
11894 else if (guard_tinfo.keyword == RID_ELSE)
11895 warning_at (body_loc, OPT_Wempty_body,
11896 "suggest braces around empty body in an %<else%> statement");
11898 /* if a compound is opened, we simply parse the statement directly. */
11899 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11900 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
11901 /* If the token is not a `{', then we must take special action. */
11902 else
11904 /* Create a compound-statement. */
11905 statement = begin_compound_stmt (0);
11906 /* Parse the dependent-statement. */
11907 cp_parser_statement (parser, NULL_TREE, false, if_p, chain);
11908 /* Finish the dummy compound-statement. */
11909 finish_compound_stmt (statement);
11912 token_indent_info next_tinfo
11913 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11914 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
11916 /* Return the statement. */
11917 return statement;
11920 /* For some dependent statements (like `while (cond) statement'), we
11921 have already created a scope. Therefore, even if the dependent
11922 statement is a compound-statement, we do not want to create another
11923 scope. */
11925 static void
11926 cp_parser_already_scoped_statement (cp_parser* parser,
11927 const token_indent_info &guard_tinfo)
11929 /* If the token is a `{', then we must take special action. */
11930 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11932 token_indent_info body_tinfo
11933 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11935 cp_parser_statement (parser, NULL_TREE, false, NULL);
11936 token_indent_info next_tinfo
11937 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11938 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
11940 else
11942 /* Avoid calling cp_parser_compound_statement, so that we
11943 don't create a new scope. Do everything else by hand. */
11944 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11945 /* If the next keyword is `__label__' we have a label declaration. */
11946 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11947 cp_parser_label_declaration (parser);
11948 /* Parse an (optional) statement-seq. */
11949 cp_parser_statement_seq_opt (parser, NULL_TREE);
11950 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11954 /* Declarations [gram.dcl.dcl] */
11956 /* Parse an optional declaration-sequence.
11958 declaration-seq:
11959 declaration
11960 declaration-seq declaration */
11962 static void
11963 cp_parser_declaration_seq_opt (cp_parser* parser)
11965 while (true)
11967 cp_token *token;
11969 token = cp_lexer_peek_token (parser->lexer);
11971 if (token->type == CPP_CLOSE_BRACE
11972 || token->type == CPP_EOF
11973 || token->type == CPP_PRAGMA_EOL)
11974 break;
11976 if (token->type == CPP_SEMICOLON)
11978 /* A declaration consisting of a single semicolon is
11979 invalid. Allow it unless we're being pedantic. */
11980 cp_lexer_consume_token (parser->lexer);
11981 if (!in_system_header_at (input_location))
11982 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11983 continue;
11986 /* If we're entering or exiting a region that's implicitly
11987 extern "C", modify the lang context appropriately. */
11988 if (!parser->implicit_extern_c && token->implicit_extern_c)
11990 push_lang_context (lang_name_c);
11991 parser->implicit_extern_c = true;
11993 else if (parser->implicit_extern_c && !token->implicit_extern_c)
11995 pop_lang_context ();
11996 parser->implicit_extern_c = false;
11999 if (token->type == CPP_PRAGMA)
12001 /* A top-level declaration can consist solely of a #pragma.
12002 A nested declaration cannot, so this is done here and not
12003 in cp_parser_declaration. (A #pragma at block scope is
12004 handled in cp_parser_statement.) */
12005 cp_parser_pragma (parser, pragma_external);
12006 continue;
12009 /* Parse the declaration itself. */
12010 cp_parser_declaration (parser);
12014 /* Parse a declaration.
12016 declaration:
12017 block-declaration
12018 function-definition
12019 template-declaration
12020 explicit-instantiation
12021 explicit-specialization
12022 linkage-specification
12023 namespace-definition
12025 GNU extension:
12027 declaration:
12028 __extension__ declaration */
12030 static void
12031 cp_parser_declaration (cp_parser* parser)
12033 cp_token token1;
12034 cp_token token2;
12035 int saved_pedantic;
12036 void *p;
12037 tree attributes = NULL_TREE;
12039 /* Check for the `__extension__' keyword. */
12040 if (cp_parser_extension_opt (parser, &saved_pedantic))
12042 /* Parse the qualified declaration. */
12043 cp_parser_declaration (parser);
12044 /* Restore the PEDANTIC flag. */
12045 pedantic = saved_pedantic;
12047 return;
12050 /* Try to figure out what kind of declaration is present. */
12051 token1 = *cp_lexer_peek_token (parser->lexer);
12053 if (token1.type != CPP_EOF)
12054 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12055 else
12057 token2.type = CPP_EOF;
12058 token2.keyword = RID_MAX;
12061 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12062 p = obstack_alloc (&declarator_obstack, 0);
12064 /* If the next token is `extern' and the following token is a string
12065 literal, then we have a linkage specification. */
12066 if (token1.keyword == RID_EXTERN
12067 && cp_parser_is_pure_string_literal (&token2))
12068 cp_parser_linkage_specification (parser);
12069 /* If the next token is `template', then we have either a template
12070 declaration, an explicit instantiation, or an explicit
12071 specialization. */
12072 else if (token1.keyword == RID_TEMPLATE)
12074 /* `template <>' indicates a template specialization. */
12075 if (token2.type == CPP_LESS
12076 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12077 cp_parser_explicit_specialization (parser);
12078 /* `template <' indicates a template declaration. */
12079 else if (token2.type == CPP_LESS)
12080 cp_parser_template_declaration (parser, /*member_p=*/false);
12081 /* Anything else must be an explicit instantiation. */
12082 else
12083 cp_parser_explicit_instantiation (parser);
12085 /* If the next token is `export', then we have a template
12086 declaration. */
12087 else if (token1.keyword == RID_EXPORT)
12088 cp_parser_template_declaration (parser, /*member_p=*/false);
12089 /* If the next token is `extern', 'static' or 'inline' and the one
12090 after that is `template', we have a GNU extended explicit
12091 instantiation directive. */
12092 else if (cp_parser_allow_gnu_extensions_p (parser)
12093 && (token1.keyword == RID_EXTERN
12094 || token1.keyword == RID_STATIC
12095 || token1.keyword == RID_INLINE)
12096 && token2.keyword == RID_TEMPLATE)
12097 cp_parser_explicit_instantiation (parser);
12098 /* If the next token is `namespace', check for a named or unnamed
12099 namespace definition. */
12100 else if (token1.keyword == RID_NAMESPACE
12101 && (/* A named namespace definition. */
12102 (token2.type == CPP_NAME
12103 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12104 != CPP_EQ))
12105 || (token2.type == CPP_OPEN_SQUARE
12106 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12107 == CPP_OPEN_SQUARE)
12108 /* An unnamed namespace definition. */
12109 || token2.type == CPP_OPEN_BRACE
12110 || token2.keyword == RID_ATTRIBUTE))
12111 cp_parser_namespace_definition (parser);
12112 /* An inline (associated) namespace definition. */
12113 else if (token1.keyword == RID_INLINE
12114 && token2.keyword == RID_NAMESPACE)
12115 cp_parser_namespace_definition (parser);
12116 /* Objective-C++ declaration/definition. */
12117 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12118 cp_parser_objc_declaration (parser, NULL_TREE);
12119 else if (c_dialect_objc ()
12120 && token1.keyword == RID_ATTRIBUTE
12121 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12122 cp_parser_objc_declaration (parser, attributes);
12123 /* At this point we may have a template declared by a concept
12124 introduction. */
12125 else if (flag_concepts
12126 && cp_parser_template_declaration_after_export (parser,
12127 /*member_p=*/false))
12128 /* We did. */;
12129 else
12130 /* Try to parse a block-declaration, or a function-definition. */
12131 cp_parser_block_declaration (parser, /*statement_p=*/false);
12133 /* Free any declarators allocated. */
12134 obstack_free (&declarator_obstack, p);
12137 /* Parse a block-declaration.
12139 block-declaration:
12140 simple-declaration
12141 asm-definition
12142 namespace-alias-definition
12143 using-declaration
12144 using-directive
12146 GNU Extension:
12148 block-declaration:
12149 __extension__ block-declaration
12151 C++0x Extension:
12153 block-declaration:
12154 static_assert-declaration
12156 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12157 part of a declaration-statement. */
12159 static void
12160 cp_parser_block_declaration (cp_parser *parser,
12161 bool statement_p)
12163 cp_token *token1;
12164 int saved_pedantic;
12166 /* Check for the `__extension__' keyword. */
12167 if (cp_parser_extension_opt (parser, &saved_pedantic))
12169 /* Parse the qualified declaration. */
12170 cp_parser_block_declaration (parser, statement_p);
12171 /* Restore the PEDANTIC flag. */
12172 pedantic = saved_pedantic;
12174 return;
12177 /* Peek at the next token to figure out which kind of declaration is
12178 present. */
12179 token1 = cp_lexer_peek_token (parser->lexer);
12181 /* If the next keyword is `asm', we have an asm-definition. */
12182 if (token1->keyword == RID_ASM)
12184 if (statement_p)
12185 cp_parser_commit_to_tentative_parse (parser);
12186 cp_parser_asm_definition (parser);
12188 /* If the next keyword is `namespace', we have a
12189 namespace-alias-definition. */
12190 else if (token1->keyword == RID_NAMESPACE)
12191 cp_parser_namespace_alias_definition (parser);
12192 /* If the next keyword is `using', we have a
12193 using-declaration, a using-directive, or an alias-declaration. */
12194 else if (token1->keyword == RID_USING)
12196 cp_token *token2;
12198 if (statement_p)
12199 cp_parser_commit_to_tentative_parse (parser);
12200 /* If the token after `using' is `namespace', then we have a
12201 using-directive. */
12202 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12203 if (token2->keyword == RID_NAMESPACE)
12204 cp_parser_using_directive (parser);
12205 /* If the second token after 'using' is '=', then we have an
12206 alias-declaration. */
12207 else if (cxx_dialect >= cxx11
12208 && token2->type == CPP_NAME
12209 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12210 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12211 cp_parser_alias_declaration (parser);
12212 /* Otherwise, it's a using-declaration. */
12213 else
12214 cp_parser_using_declaration (parser,
12215 /*access_declaration_p=*/false);
12217 /* If the next keyword is `__label__' we have a misplaced label
12218 declaration. */
12219 else if (token1->keyword == RID_LABEL)
12221 cp_lexer_consume_token (parser->lexer);
12222 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12223 cp_parser_skip_to_end_of_statement (parser);
12224 /* If the next token is now a `;', consume it. */
12225 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12226 cp_lexer_consume_token (parser->lexer);
12228 /* If the next token is `static_assert' we have a static assertion. */
12229 else if (token1->keyword == RID_STATIC_ASSERT)
12230 cp_parser_static_assert (parser, /*member_p=*/false);
12231 /* Anything else must be a simple-declaration. */
12232 else
12233 cp_parser_simple_declaration (parser, !statement_p,
12234 /*maybe_range_for_decl*/NULL);
12237 /* Parse a simple-declaration.
12239 simple-declaration:
12240 decl-specifier-seq [opt] init-declarator-list [opt] ;
12242 init-declarator-list:
12243 init-declarator
12244 init-declarator-list , init-declarator
12246 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12247 function-definition as a simple-declaration.
12249 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12250 parsed declaration if it is an uninitialized single declarator not followed
12251 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12252 if present, will not be consumed. */
12254 static void
12255 cp_parser_simple_declaration (cp_parser* parser,
12256 bool function_definition_allowed_p,
12257 tree *maybe_range_for_decl)
12259 cp_decl_specifier_seq decl_specifiers;
12260 int declares_class_or_enum;
12261 bool saw_declarator;
12262 location_t comma_loc = UNKNOWN_LOCATION;
12263 location_t init_loc = UNKNOWN_LOCATION;
12265 if (maybe_range_for_decl)
12266 *maybe_range_for_decl = NULL_TREE;
12268 /* Defer access checks until we know what is being declared; the
12269 checks for names appearing in the decl-specifier-seq should be
12270 done as if we were in the scope of the thing being declared. */
12271 push_deferring_access_checks (dk_deferred);
12273 /* Parse the decl-specifier-seq. We have to keep track of whether
12274 or not the decl-specifier-seq declares a named class or
12275 enumeration type, since that is the only case in which the
12276 init-declarator-list is allowed to be empty.
12278 [dcl.dcl]
12280 In a simple-declaration, the optional init-declarator-list can be
12281 omitted only when declaring a class or enumeration, that is when
12282 the decl-specifier-seq contains either a class-specifier, an
12283 elaborated-type-specifier, or an enum-specifier. */
12284 cp_parser_decl_specifier_seq (parser,
12285 CP_PARSER_FLAGS_OPTIONAL,
12286 &decl_specifiers,
12287 &declares_class_or_enum);
12288 /* We no longer need to defer access checks. */
12289 stop_deferring_access_checks ();
12291 /* In a block scope, a valid declaration must always have a
12292 decl-specifier-seq. By not trying to parse declarators, we can
12293 resolve the declaration/expression ambiguity more quickly. */
12294 if (!function_definition_allowed_p
12295 && !decl_specifiers.any_specifiers_p)
12297 cp_parser_error (parser, "expected declaration");
12298 goto done;
12301 /* If the next two tokens are both identifiers, the code is
12302 erroneous. The usual cause of this situation is code like:
12304 T t;
12306 where "T" should name a type -- but does not. */
12307 if (!decl_specifiers.any_type_specifiers_p
12308 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12310 /* If parsing tentatively, we should commit; we really are
12311 looking at a declaration. */
12312 cp_parser_commit_to_tentative_parse (parser);
12313 /* Give up. */
12314 goto done;
12317 /* If we have seen at least one decl-specifier, and the next token
12318 is not a parenthesis, then we must be looking at a declaration.
12319 (After "int (" we might be looking at a functional cast.) */
12320 if (decl_specifiers.any_specifiers_p
12321 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12322 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12323 && !cp_parser_error_occurred (parser))
12324 cp_parser_commit_to_tentative_parse (parser);
12326 tree last_type, auto_node;
12328 last_type = NULL_TREE;
12329 auto_node = type_uses_auto (decl_specifiers.type);
12331 /* Keep going until we hit the `;' at the end of the simple
12332 declaration. */
12333 saw_declarator = false;
12334 while (cp_lexer_next_token_is_not (parser->lexer,
12335 CPP_SEMICOLON))
12337 cp_token *token;
12338 bool function_definition_p;
12339 tree decl;
12341 if (saw_declarator)
12343 /* If we are processing next declarator, comma is expected */
12344 token = cp_lexer_peek_token (parser->lexer);
12345 gcc_assert (token->type == CPP_COMMA);
12346 cp_lexer_consume_token (parser->lexer);
12347 if (maybe_range_for_decl)
12349 *maybe_range_for_decl = error_mark_node;
12350 if (comma_loc == UNKNOWN_LOCATION)
12351 comma_loc = token->location;
12354 else
12355 saw_declarator = true;
12357 /* Parse the init-declarator. */
12358 decl = cp_parser_init_declarator (parser, &decl_specifiers,
12359 /*checks=*/NULL,
12360 function_definition_allowed_p,
12361 /*member_p=*/false,
12362 declares_class_or_enum,
12363 &function_definition_p,
12364 maybe_range_for_decl,
12365 &init_loc);
12366 /* If an error occurred while parsing tentatively, exit quickly.
12367 (That usually happens when in the body of a function; each
12368 statement is treated as a declaration-statement until proven
12369 otherwise.) */
12370 if (cp_parser_error_occurred (parser))
12371 goto done;
12373 if (auto_node)
12375 tree type = TREE_TYPE (decl);
12376 if (last_type && !same_type_p (type, last_type))
12378 /* If the list of declarators contains more than one declarator,
12379 the type of each declared variable is determined as described
12380 above. If the type deduced for the template parameter U is not
12381 the same in each deduction, the program is ill-formed. */
12382 error_at (decl_specifiers.locations[ds_type_spec],
12383 "inconsistent deduction for %qT: %qT and then %qT",
12384 decl_specifiers.type, last_type, type);
12385 auto_node = NULL_TREE;
12387 last_type = type;
12390 /* Handle function definitions specially. */
12391 if (function_definition_p)
12393 /* If the next token is a `,', then we are probably
12394 processing something like:
12396 void f() {}, *p;
12398 which is erroneous. */
12399 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12401 cp_token *token = cp_lexer_peek_token (parser->lexer);
12402 error_at (token->location,
12403 "mixing"
12404 " declarations and function-definitions is forbidden");
12406 /* Otherwise, we're done with the list of declarators. */
12407 else
12409 pop_deferring_access_checks ();
12410 return;
12413 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
12414 *maybe_range_for_decl = decl;
12415 /* The next token should be either a `,' or a `;'. */
12416 token = cp_lexer_peek_token (parser->lexer);
12417 /* If it's a `,', there are more declarators to come. */
12418 if (token->type == CPP_COMMA)
12419 /* will be consumed next time around */;
12420 /* If it's a `;', we are done. */
12421 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12422 break;
12423 /* Anything else is an error. */
12424 else
12426 /* If we have already issued an error message we don't need
12427 to issue another one. */
12428 if ((decl != error_mark_node
12429 && DECL_INITIAL (decl) != error_mark_node)
12430 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12431 cp_parser_error (parser, "expected %<,%> or %<;%>");
12432 /* Skip tokens until we reach the end of the statement. */
12433 cp_parser_skip_to_end_of_statement (parser);
12434 /* If the next token is now a `;', consume it. */
12435 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12436 cp_lexer_consume_token (parser->lexer);
12437 goto done;
12439 /* After the first time around, a function-definition is not
12440 allowed -- even if it was OK at first. For example:
12442 int i, f() {}
12444 is not valid. */
12445 function_definition_allowed_p = false;
12448 /* Issue an error message if no declarators are present, and the
12449 decl-specifier-seq does not itself declare a class or
12450 enumeration: [dcl.dcl]/3. */
12451 if (!saw_declarator)
12453 if (cp_parser_declares_only_class_p (parser))
12455 if (!declares_class_or_enum
12456 && decl_specifiers.type
12457 && OVERLOAD_TYPE_P (decl_specifiers.type))
12458 /* Ensure an error is issued anyway when finish_decltype_type,
12459 called via cp_parser_decl_specifier_seq, returns a class or
12460 an enumeration (c++/51786). */
12461 decl_specifiers.type = NULL_TREE;
12462 shadow_tag (&decl_specifiers);
12464 /* Perform any deferred access checks. */
12465 perform_deferred_access_checks (tf_warning_or_error);
12468 /* Consume the `;'. */
12469 if (!maybe_range_for_decl)
12470 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12471 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12473 if (init_loc != UNKNOWN_LOCATION)
12474 error_at (init_loc, "initializer in range-based %<for%> loop");
12475 if (comma_loc != UNKNOWN_LOCATION)
12476 error_at (comma_loc,
12477 "multiple declarations in range-based %<for%> loop");
12480 done:
12481 pop_deferring_access_checks ();
12484 /* Parse a decl-specifier-seq.
12486 decl-specifier-seq:
12487 decl-specifier-seq [opt] decl-specifier
12488 decl-specifier attribute-specifier-seq [opt] (C++11)
12490 decl-specifier:
12491 storage-class-specifier
12492 type-specifier
12493 function-specifier
12494 friend
12495 typedef
12497 GNU Extension:
12499 decl-specifier:
12500 attributes
12502 Concepts Extension:
12504 decl-specifier:
12505 concept
12507 Set *DECL_SPECS to a representation of the decl-specifier-seq.
12509 The parser flags FLAGS is used to control type-specifier parsing.
12511 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
12512 flags:
12514 1: one of the decl-specifiers is an elaborated-type-specifier
12515 (i.e., a type declaration)
12516 2: one of the decl-specifiers is an enum-specifier or a
12517 class-specifier (i.e., a type definition)
12521 static void
12522 cp_parser_decl_specifier_seq (cp_parser* parser,
12523 cp_parser_flags flags,
12524 cp_decl_specifier_seq *decl_specs,
12525 int* declares_class_or_enum)
12527 bool constructor_possible_p = !parser->in_declarator_p;
12528 bool found_decl_spec = false;
12529 cp_token *start_token = NULL;
12530 cp_decl_spec ds;
12532 /* Clear DECL_SPECS. */
12533 clear_decl_specs (decl_specs);
12535 /* Assume no class or enumeration type is declared. */
12536 *declares_class_or_enum = 0;
12538 /* Keep reading specifiers until there are no more to read. */
12539 while (true)
12541 bool constructor_p;
12542 cp_token *token;
12543 ds = ds_last;
12545 /* Peek at the next token. */
12546 token = cp_lexer_peek_token (parser->lexer);
12548 /* Save the first token of the decl spec list for error
12549 reporting. */
12550 if (!start_token)
12551 start_token = token;
12552 /* Handle attributes. */
12553 if (cp_next_tokens_can_be_attribute_p (parser))
12555 /* Parse the attributes. */
12556 tree attrs = cp_parser_attributes_opt (parser);
12558 /* In a sequence of declaration specifiers, c++11 attributes
12559 appertain to the type that precede them. In that case
12560 [dcl.spec]/1 says:
12562 The attribute-specifier-seq affects the type only for
12563 the declaration it appears in, not other declarations
12564 involving the same type.
12566 But for now let's force the user to position the
12567 attribute either at the beginning of the declaration or
12568 after the declarator-id, which would clearly mean that it
12569 applies to the declarator. */
12570 if (cxx11_attribute_p (attrs))
12572 if (!found_decl_spec)
12573 /* The c++11 attribute is at the beginning of the
12574 declaration. It appertains to the entity being
12575 declared. */;
12576 else
12578 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
12580 /* This is an attribute following a
12581 class-specifier. */
12582 if (decl_specs->type_definition_p)
12583 warn_misplaced_attr_for_class_type (token->location,
12584 decl_specs->type);
12585 attrs = NULL_TREE;
12587 else
12589 decl_specs->std_attributes
12590 = chainon (decl_specs->std_attributes,
12591 attrs);
12592 if (decl_specs->locations[ds_std_attribute] == 0)
12593 decl_specs->locations[ds_std_attribute] = token->location;
12595 continue;
12599 decl_specs->attributes
12600 = chainon (decl_specs->attributes,
12601 attrs);
12602 if (decl_specs->locations[ds_attribute] == 0)
12603 decl_specs->locations[ds_attribute] = token->location;
12604 continue;
12606 /* Assume we will find a decl-specifier keyword. */
12607 found_decl_spec = true;
12608 /* If the next token is an appropriate keyword, we can simply
12609 add it to the list. */
12610 switch (token->keyword)
12612 /* decl-specifier:
12613 friend
12614 constexpr */
12615 case RID_FRIEND:
12616 if (!at_class_scope_p ())
12618 error_at (token->location, "%<friend%> used outside of class");
12619 cp_lexer_purge_token (parser->lexer);
12621 else
12623 ds = ds_friend;
12624 /* Consume the token. */
12625 cp_lexer_consume_token (parser->lexer);
12627 break;
12629 case RID_CONSTEXPR:
12630 ds = ds_constexpr;
12631 cp_lexer_consume_token (parser->lexer);
12632 break;
12634 case RID_CONCEPT:
12635 ds = ds_concept;
12636 cp_lexer_consume_token (parser->lexer);
12637 break;
12639 /* function-specifier:
12640 inline
12641 virtual
12642 explicit */
12643 case RID_INLINE:
12644 case RID_VIRTUAL:
12645 case RID_EXPLICIT:
12646 cp_parser_function_specifier_opt (parser, decl_specs);
12647 break;
12649 /* decl-specifier:
12650 typedef */
12651 case RID_TYPEDEF:
12652 ds = ds_typedef;
12653 /* Consume the token. */
12654 cp_lexer_consume_token (parser->lexer);
12655 /* A constructor declarator cannot appear in a typedef. */
12656 constructor_possible_p = false;
12657 /* The "typedef" keyword can only occur in a declaration; we
12658 may as well commit at this point. */
12659 cp_parser_commit_to_tentative_parse (parser);
12661 if (decl_specs->storage_class != sc_none)
12662 decl_specs->conflicting_specifiers_p = true;
12663 break;
12665 /* storage-class-specifier:
12666 auto
12667 register
12668 static
12669 extern
12670 mutable
12672 GNU Extension:
12673 thread */
12674 case RID_AUTO:
12675 if (cxx_dialect == cxx98)
12677 /* Consume the token. */
12678 cp_lexer_consume_token (parser->lexer);
12680 /* Complain about `auto' as a storage specifier, if
12681 we're complaining about C++0x compatibility. */
12682 warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
12683 " changes meaning in C++11; please remove it");
12685 /* Set the storage class anyway. */
12686 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
12687 token);
12689 else
12690 /* C++0x auto type-specifier. */
12691 found_decl_spec = false;
12692 break;
12694 case RID_REGISTER:
12695 case RID_STATIC:
12696 case RID_EXTERN:
12697 case RID_MUTABLE:
12698 /* Consume the token. */
12699 cp_lexer_consume_token (parser->lexer);
12700 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
12701 token);
12702 break;
12703 case RID_THREAD:
12704 /* Consume the token. */
12705 ds = ds_thread;
12706 cp_lexer_consume_token (parser->lexer);
12707 break;
12709 default:
12710 /* We did not yet find a decl-specifier yet. */
12711 found_decl_spec = false;
12712 break;
12715 if (found_decl_spec
12716 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
12717 && token->keyword != RID_CONSTEXPR)
12718 error ("decl-specifier invalid in condition");
12720 if (ds != ds_last)
12721 set_and_check_decl_spec_loc (decl_specs, ds, token);
12723 /* Constructors are a special case. The `S' in `S()' is not a
12724 decl-specifier; it is the beginning of the declarator. */
12725 constructor_p
12726 = (!found_decl_spec
12727 && constructor_possible_p
12728 && (cp_parser_constructor_declarator_p
12729 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
12731 /* If we don't have a DECL_SPEC yet, then we must be looking at
12732 a type-specifier. */
12733 if (!found_decl_spec && !constructor_p)
12735 int decl_spec_declares_class_or_enum;
12736 bool is_cv_qualifier;
12737 tree type_spec;
12739 type_spec
12740 = cp_parser_type_specifier (parser, flags,
12741 decl_specs,
12742 /*is_declaration=*/true,
12743 &decl_spec_declares_class_or_enum,
12744 &is_cv_qualifier);
12745 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
12747 /* If this type-specifier referenced a user-defined type
12748 (a typedef, class-name, etc.), then we can't allow any
12749 more such type-specifiers henceforth.
12751 [dcl.spec]
12753 The longest sequence of decl-specifiers that could
12754 possibly be a type name is taken as the
12755 decl-specifier-seq of a declaration. The sequence shall
12756 be self-consistent as described below.
12758 [dcl.type]
12760 As a general rule, at most one type-specifier is allowed
12761 in the complete decl-specifier-seq of a declaration. The
12762 only exceptions are the following:
12764 -- const or volatile can be combined with any other
12765 type-specifier.
12767 -- signed or unsigned can be combined with char, long,
12768 short, or int.
12770 -- ..
12772 Example:
12774 typedef char* Pc;
12775 void g (const int Pc);
12777 Here, Pc is *not* part of the decl-specifier seq; it's
12778 the declarator. Therefore, once we see a type-specifier
12779 (other than a cv-qualifier), we forbid any additional
12780 user-defined types. We *do* still allow things like `int
12781 int' to be considered a decl-specifier-seq, and issue the
12782 error message later. */
12783 if (type_spec && !is_cv_qualifier)
12784 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12785 /* A constructor declarator cannot follow a type-specifier. */
12786 if (type_spec)
12788 constructor_possible_p = false;
12789 found_decl_spec = true;
12790 if (!is_cv_qualifier)
12791 decl_specs->any_type_specifiers_p = true;
12795 /* If we still do not have a DECL_SPEC, then there are no more
12796 decl-specifiers. */
12797 if (!found_decl_spec)
12798 break;
12800 decl_specs->any_specifiers_p = true;
12801 /* After we see one decl-specifier, further decl-specifiers are
12802 always optional. */
12803 flags |= CP_PARSER_FLAGS_OPTIONAL;
12806 /* Don't allow a friend specifier with a class definition. */
12807 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
12808 && (*declares_class_or_enum & 2))
12809 error_at (decl_specs->locations[ds_friend],
12810 "class definition may not be declared a friend");
12813 /* Parse an (optional) storage-class-specifier.
12815 storage-class-specifier:
12816 auto
12817 register
12818 static
12819 extern
12820 mutable
12822 GNU Extension:
12824 storage-class-specifier:
12825 thread
12827 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
12829 static tree
12830 cp_parser_storage_class_specifier_opt (cp_parser* parser)
12832 switch (cp_lexer_peek_token (parser->lexer)->keyword)
12834 case RID_AUTO:
12835 if (cxx_dialect != cxx98)
12836 return NULL_TREE;
12837 /* Fall through for C++98. */
12839 case RID_REGISTER:
12840 case RID_STATIC:
12841 case RID_EXTERN:
12842 case RID_MUTABLE:
12843 case RID_THREAD:
12844 /* Consume the token. */
12845 return cp_lexer_consume_token (parser->lexer)->u.value;
12847 default:
12848 return NULL_TREE;
12852 /* Parse an (optional) function-specifier.
12854 function-specifier:
12855 inline
12856 virtual
12857 explicit
12859 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12860 Updates DECL_SPECS, if it is non-NULL. */
12862 static tree
12863 cp_parser_function_specifier_opt (cp_parser* parser,
12864 cp_decl_specifier_seq *decl_specs)
12866 cp_token *token = cp_lexer_peek_token (parser->lexer);
12867 switch (token->keyword)
12869 case RID_INLINE:
12870 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12871 break;
12873 case RID_VIRTUAL:
12874 /* 14.5.2.3 [temp.mem]
12876 A member function template shall not be virtual. */
12877 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12878 error_at (token->location, "templates may not be %<virtual%>");
12879 else
12880 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12881 break;
12883 case RID_EXPLICIT:
12884 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12885 break;
12887 default:
12888 return NULL_TREE;
12891 /* Consume the token. */
12892 return cp_lexer_consume_token (parser->lexer)->u.value;
12895 /* Parse a linkage-specification.
12897 linkage-specification:
12898 extern string-literal { declaration-seq [opt] }
12899 extern string-literal declaration */
12901 static void
12902 cp_parser_linkage_specification (cp_parser* parser)
12904 tree linkage;
12906 /* Look for the `extern' keyword. */
12907 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12909 /* Look for the string-literal. */
12910 linkage = cp_parser_string_literal (parser, false, false);
12912 /* Transform the literal into an identifier. If the literal is a
12913 wide-character string, or contains embedded NULs, then we can't
12914 handle it as the user wants. */
12915 if (strlen (TREE_STRING_POINTER (linkage))
12916 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12918 cp_parser_error (parser, "invalid linkage-specification");
12919 /* Assume C++ linkage. */
12920 linkage = lang_name_cplusplus;
12922 else
12923 linkage = get_identifier (TREE_STRING_POINTER (linkage));
12925 /* We're now using the new linkage. */
12926 push_lang_context (linkage);
12928 /* If the next token is a `{', then we're using the first
12929 production. */
12930 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12932 cp_ensure_no_omp_declare_simd (parser);
12933 cp_ensure_no_oacc_routine (parser);
12935 /* Consume the `{' token. */
12936 cp_lexer_consume_token (parser->lexer);
12937 /* Parse the declarations. */
12938 cp_parser_declaration_seq_opt (parser);
12939 /* Look for the closing `}'. */
12940 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12942 /* Otherwise, there's just one declaration. */
12943 else
12945 bool saved_in_unbraced_linkage_specification_p;
12947 saved_in_unbraced_linkage_specification_p
12948 = parser->in_unbraced_linkage_specification_p;
12949 parser->in_unbraced_linkage_specification_p = true;
12950 cp_parser_declaration (parser);
12951 parser->in_unbraced_linkage_specification_p
12952 = saved_in_unbraced_linkage_specification_p;
12955 /* We're done with the linkage-specification. */
12956 pop_lang_context ();
12959 /* Parse a static_assert-declaration.
12961 static_assert-declaration:
12962 static_assert ( constant-expression , string-literal ) ;
12963 static_assert ( constant-expression ) ; (C++1Z)
12965 If MEMBER_P, this static_assert is a class member. */
12967 static void
12968 cp_parser_static_assert(cp_parser *parser, bool member_p)
12970 tree condition;
12971 tree message;
12972 cp_token *token;
12973 location_t saved_loc;
12974 bool dummy;
12976 /* Peek at the `static_assert' token so we can keep track of exactly
12977 where the static assertion started. */
12978 token = cp_lexer_peek_token (parser->lexer);
12979 saved_loc = token->location;
12981 /* Look for the `static_assert' keyword. */
12982 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
12983 RT_STATIC_ASSERT))
12984 return;
12986 /* We know we are in a static assertion; commit to any tentative
12987 parse. */
12988 if (cp_parser_parsing_tentatively (parser))
12989 cp_parser_commit_to_tentative_parse (parser);
12991 /* Parse the `(' starting the static assertion condition. */
12992 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12994 /* Parse the constant-expression. Allow a non-constant expression
12995 here in order to give better diagnostics in finish_static_assert. */
12996 condition =
12997 cp_parser_constant_expression (parser,
12998 /*allow_non_constant_p=*/true,
12999 /*non_constant_p=*/&dummy);
13001 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13003 if (cxx_dialect < cxx1z)
13004 pedwarn (input_location, OPT_Wpedantic,
13005 "static_assert without a message "
13006 "only available with -std=c++1z or -std=gnu++1z");
13007 /* Eat the ')' */
13008 cp_lexer_consume_token (parser->lexer);
13009 message = build_string (1, "");
13010 TREE_TYPE (message) = char_array_type_node;
13011 fix_string_type (message);
13013 else
13015 /* Parse the separating `,'. */
13016 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13018 /* Parse the string-literal message. */
13019 message = cp_parser_string_literal (parser,
13020 /*translate=*/false,
13021 /*wide_ok=*/true);
13023 /* A `)' completes the static assertion. */
13024 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13025 cp_parser_skip_to_closing_parenthesis (parser,
13026 /*recovering=*/true,
13027 /*or_comma=*/false,
13028 /*consume_paren=*/true);
13031 /* A semicolon terminates the declaration. */
13032 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13034 /* Complete the static assertion, which may mean either processing
13035 the static assert now or saving it for template instantiation. */
13036 finish_static_assert (condition, message, saved_loc, member_p);
13039 /* Parse the expression in decltype ( expression ). */
13041 static tree
13042 cp_parser_decltype_expr (cp_parser *parser,
13043 bool &id_expression_or_member_access_p)
13045 cp_token *id_expr_start_token;
13046 tree expr;
13048 /* Since we're going to preserve any side-effects from this parse, set up a
13049 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13050 in the expression. */
13051 tentative_firewall firewall (parser);
13053 /* First, try parsing an id-expression. */
13054 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13055 cp_parser_parse_tentatively (parser);
13056 expr = cp_parser_id_expression (parser,
13057 /*template_keyword_p=*/false,
13058 /*check_dependency_p=*/true,
13059 /*template_p=*/NULL,
13060 /*declarator_p=*/false,
13061 /*optional_p=*/false);
13063 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13065 bool non_integral_constant_expression_p = false;
13066 tree id_expression = expr;
13067 cp_id_kind idk;
13068 const char *error_msg;
13070 if (identifier_p (expr))
13071 /* Lookup the name we got back from the id-expression. */
13072 expr = cp_parser_lookup_name_simple (parser, expr,
13073 id_expr_start_token->location);
13075 if (expr
13076 && expr != error_mark_node
13077 && TREE_CODE (expr) != TYPE_DECL
13078 && (TREE_CODE (expr) != BIT_NOT_EXPR
13079 || !TYPE_P (TREE_OPERAND (expr, 0)))
13080 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13082 /* Complete lookup of the id-expression. */
13083 expr = (finish_id_expression
13084 (id_expression, expr, parser->scope, &idk,
13085 /*integral_constant_expression_p=*/false,
13086 /*allow_non_integral_constant_expression_p=*/true,
13087 &non_integral_constant_expression_p,
13088 /*template_p=*/false,
13089 /*done=*/true,
13090 /*address_p=*/false,
13091 /*template_arg_p=*/false,
13092 &error_msg,
13093 id_expr_start_token->location));
13095 if (expr == error_mark_node)
13096 /* We found an id-expression, but it was something that we
13097 should not have found. This is an error, not something
13098 we can recover from, so note that we found an
13099 id-expression and we'll recover as gracefully as
13100 possible. */
13101 id_expression_or_member_access_p = true;
13104 if (expr
13105 && expr != error_mark_node
13106 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13107 /* We have an id-expression. */
13108 id_expression_or_member_access_p = true;
13111 if (!id_expression_or_member_access_p)
13113 /* Abort the id-expression parse. */
13114 cp_parser_abort_tentative_parse (parser);
13116 /* Parsing tentatively, again. */
13117 cp_parser_parse_tentatively (parser);
13119 /* Parse a class member access. */
13120 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13121 /*cast_p=*/false, /*decltype*/true,
13122 /*member_access_only_p=*/true, NULL);
13124 if (expr
13125 && expr != error_mark_node
13126 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13127 /* We have an id-expression. */
13128 id_expression_or_member_access_p = true;
13131 if (id_expression_or_member_access_p)
13132 /* We have parsed the complete id-expression or member access. */
13133 cp_parser_parse_definitely (parser);
13134 else
13136 /* Abort our attempt to parse an id-expression or member access
13137 expression. */
13138 cp_parser_abort_tentative_parse (parser);
13140 /* Parse a full expression. */
13141 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
13142 /*decltype_p=*/true);
13145 return expr;
13148 /* Parse a `decltype' type. Returns the type.
13150 simple-type-specifier:
13151 decltype ( expression )
13152 C++14 proposal:
13153 decltype ( auto ) */
13155 static tree
13156 cp_parser_decltype (cp_parser *parser)
13158 tree expr;
13159 bool id_expression_or_member_access_p = false;
13160 const char *saved_message;
13161 bool saved_integral_constant_expression_p;
13162 bool saved_non_integral_constant_expression_p;
13163 bool saved_greater_than_is_operator_p;
13164 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13166 if (start_token->type == CPP_DECLTYPE)
13168 /* Already parsed. */
13169 cp_lexer_consume_token (parser->lexer);
13170 return saved_checks_value (start_token->u.tree_check_value);
13173 /* Look for the `decltype' token. */
13174 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
13175 return error_mark_node;
13177 /* Parse the opening `('. */
13178 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13179 return error_mark_node;
13181 /* decltype (auto) */
13182 if (cxx_dialect >= cxx14
13183 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
13185 cp_lexer_consume_token (parser->lexer);
13186 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13187 return error_mark_node;
13188 expr = make_decltype_auto ();
13189 AUTO_IS_DECLTYPE (expr) = true;
13190 goto rewrite;
13193 /* Types cannot be defined in a `decltype' expression. Save away the
13194 old message. */
13195 saved_message = parser->type_definition_forbidden_message;
13197 /* And create the new one. */
13198 parser->type_definition_forbidden_message
13199 = G_("types may not be defined in %<decltype%> expressions");
13201 /* The restrictions on constant-expressions do not apply inside
13202 decltype expressions. */
13203 saved_integral_constant_expression_p
13204 = parser->integral_constant_expression_p;
13205 saved_non_integral_constant_expression_p
13206 = parser->non_integral_constant_expression_p;
13207 parser->integral_constant_expression_p = false;
13209 /* Within a parenthesized expression, a `>' token is always
13210 the greater-than operator. */
13211 saved_greater_than_is_operator_p
13212 = parser->greater_than_is_operator_p;
13213 parser->greater_than_is_operator_p = true;
13215 /* Do not actually evaluate the expression. */
13216 ++cp_unevaluated_operand;
13218 /* Do not warn about problems with the expression. */
13219 ++c_inhibit_evaluation_warnings;
13221 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
13223 /* Go back to evaluating expressions. */
13224 --cp_unevaluated_operand;
13225 --c_inhibit_evaluation_warnings;
13227 /* The `>' token might be the end of a template-id or
13228 template-parameter-list now. */
13229 parser->greater_than_is_operator_p
13230 = saved_greater_than_is_operator_p;
13232 /* Restore the old message and the integral constant expression
13233 flags. */
13234 parser->type_definition_forbidden_message = saved_message;
13235 parser->integral_constant_expression_p
13236 = saved_integral_constant_expression_p;
13237 parser->non_integral_constant_expression_p
13238 = saved_non_integral_constant_expression_p;
13240 /* Parse to the closing `)'. */
13241 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13243 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13244 /*consume_paren=*/true);
13245 return error_mark_node;
13248 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
13249 tf_warning_or_error);
13251 rewrite:
13252 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
13253 it again. */
13254 start_token->type = CPP_DECLTYPE;
13255 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13256 start_token->u.tree_check_value->value = expr;
13257 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
13258 start_token->keyword = RID_MAX;
13259 cp_lexer_purge_tokens_after (parser->lexer, start_token);
13261 return expr;
13264 /* Special member functions [gram.special] */
13266 /* Parse a conversion-function-id.
13268 conversion-function-id:
13269 operator conversion-type-id
13271 Returns an IDENTIFIER_NODE representing the operator. */
13273 static tree
13274 cp_parser_conversion_function_id (cp_parser* parser)
13276 tree type;
13277 tree saved_scope;
13278 tree saved_qualifying_scope;
13279 tree saved_object_scope;
13280 tree pushed_scope = NULL_TREE;
13282 /* Look for the `operator' token. */
13283 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13284 return error_mark_node;
13285 /* When we parse the conversion-type-id, the current scope will be
13286 reset. However, we need that information in able to look up the
13287 conversion function later, so we save it here. */
13288 saved_scope = parser->scope;
13289 saved_qualifying_scope = parser->qualifying_scope;
13290 saved_object_scope = parser->object_scope;
13291 /* We must enter the scope of the class so that the names of
13292 entities declared within the class are available in the
13293 conversion-type-id. For example, consider:
13295 struct S {
13296 typedef int I;
13297 operator I();
13300 S::operator I() { ... }
13302 In order to see that `I' is a type-name in the definition, we
13303 must be in the scope of `S'. */
13304 if (saved_scope)
13305 pushed_scope = push_scope (saved_scope);
13306 /* Parse the conversion-type-id. */
13307 type = cp_parser_conversion_type_id (parser);
13308 /* Leave the scope of the class, if any. */
13309 if (pushed_scope)
13310 pop_scope (pushed_scope);
13311 /* Restore the saved scope. */
13312 parser->scope = saved_scope;
13313 parser->qualifying_scope = saved_qualifying_scope;
13314 parser->object_scope = saved_object_scope;
13315 /* If the TYPE is invalid, indicate failure. */
13316 if (type == error_mark_node)
13317 return error_mark_node;
13318 return mangle_conv_op_name_for_type (type);
13321 /* Parse a conversion-type-id:
13323 conversion-type-id:
13324 type-specifier-seq conversion-declarator [opt]
13326 Returns the TYPE specified. */
13328 static tree
13329 cp_parser_conversion_type_id (cp_parser* parser)
13331 tree attributes;
13332 cp_decl_specifier_seq type_specifiers;
13333 cp_declarator *declarator;
13334 tree type_specified;
13335 const char *saved_message;
13337 /* Parse the attributes. */
13338 attributes = cp_parser_attributes_opt (parser);
13340 saved_message = parser->type_definition_forbidden_message;
13341 parser->type_definition_forbidden_message
13342 = G_("types may not be defined in a conversion-type-id");
13344 /* Parse the type-specifiers. */
13345 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13346 /*is_trailing_return=*/false,
13347 &type_specifiers);
13349 parser->type_definition_forbidden_message = saved_message;
13351 /* If that didn't work, stop. */
13352 if (type_specifiers.type == error_mark_node)
13353 return error_mark_node;
13354 /* Parse the conversion-declarator. */
13355 declarator = cp_parser_conversion_declarator_opt (parser);
13357 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
13358 /*initialized=*/0, &attributes);
13359 if (attributes)
13360 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
13362 /* Don't give this error when parsing tentatively. This happens to
13363 work because we always parse this definitively once. */
13364 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
13365 && type_uses_auto (type_specified))
13367 if (cxx_dialect < cxx14)
13369 error ("invalid use of %<auto%> in conversion operator");
13370 return error_mark_node;
13372 else if (template_parm_scope_p ())
13373 warning (0, "use of %<auto%> in member template "
13374 "conversion operator can never be deduced");
13377 return type_specified;
13380 /* Parse an (optional) conversion-declarator.
13382 conversion-declarator:
13383 ptr-operator conversion-declarator [opt]
13387 static cp_declarator *
13388 cp_parser_conversion_declarator_opt (cp_parser* parser)
13390 enum tree_code code;
13391 tree class_type, std_attributes = NULL_TREE;
13392 cp_cv_quals cv_quals;
13394 /* We don't know if there's a ptr-operator next, or not. */
13395 cp_parser_parse_tentatively (parser);
13396 /* Try the ptr-operator. */
13397 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
13398 &std_attributes);
13399 /* If it worked, look for more conversion-declarators. */
13400 if (cp_parser_parse_definitely (parser))
13402 cp_declarator *declarator;
13404 /* Parse another optional declarator. */
13405 declarator = cp_parser_conversion_declarator_opt (parser);
13407 declarator = cp_parser_make_indirect_declarator
13408 (code, class_type, cv_quals, declarator, std_attributes);
13410 return declarator;
13413 return NULL;
13416 /* Parse an (optional) ctor-initializer.
13418 ctor-initializer:
13419 : mem-initializer-list
13421 Returns TRUE iff the ctor-initializer was actually present. */
13423 static bool
13424 cp_parser_ctor_initializer_opt (cp_parser* parser)
13426 /* If the next token is not a `:', then there is no
13427 ctor-initializer. */
13428 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13430 /* Do default initialization of any bases and members. */
13431 if (DECL_CONSTRUCTOR_P (current_function_decl))
13432 finish_mem_initializers (NULL_TREE);
13434 return false;
13437 /* Consume the `:' token. */
13438 cp_lexer_consume_token (parser->lexer);
13439 /* And the mem-initializer-list. */
13440 cp_parser_mem_initializer_list (parser);
13442 return true;
13445 /* Parse a mem-initializer-list.
13447 mem-initializer-list:
13448 mem-initializer ... [opt]
13449 mem-initializer ... [opt] , mem-initializer-list */
13451 static void
13452 cp_parser_mem_initializer_list (cp_parser* parser)
13454 tree mem_initializer_list = NULL_TREE;
13455 tree target_ctor = error_mark_node;
13456 cp_token *token = cp_lexer_peek_token (parser->lexer);
13458 /* Let the semantic analysis code know that we are starting the
13459 mem-initializer-list. */
13460 if (!DECL_CONSTRUCTOR_P (current_function_decl))
13461 error_at (token->location,
13462 "only constructors take member initializers");
13464 /* Loop through the list. */
13465 while (true)
13467 tree mem_initializer;
13469 token = cp_lexer_peek_token (parser->lexer);
13470 /* Parse the mem-initializer. */
13471 mem_initializer = cp_parser_mem_initializer (parser);
13472 /* If the next token is a `...', we're expanding member initializers. */
13473 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13475 /* Consume the `...'. */
13476 cp_lexer_consume_token (parser->lexer);
13478 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
13479 can be expanded but members cannot. */
13480 if (mem_initializer != error_mark_node
13481 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
13483 error_at (token->location,
13484 "cannot expand initializer for member %<%D%>",
13485 TREE_PURPOSE (mem_initializer));
13486 mem_initializer = error_mark_node;
13489 /* Construct the pack expansion type. */
13490 if (mem_initializer != error_mark_node)
13491 mem_initializer = make_pack_expansion (mem_initializer);
13493 if (target_ctor != error_mark_node
13494 && mem_initializer != error_mark_node)
13496 error ("mem-initializer for %qD follows constructor delegation",
13497 TREE_PURPOSE (mem_initializer));
13498 mem_initializer = error_mark_node;
13500 /* Look for a target constructor. */
13501 if (mem_initializer != error_mark_node
13502 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
13503 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
13505 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
13506 if (mem_initializer_list)
13508 error ("constructor delegation follows mem-initializer for %qD",
13509 TREE_PURPOSE (mem_initializer_list));
13510 mem_initializer = error_mark_node;
13512 target_ctor = mem_initializer;
13514 /* Add it to the list, unless it was erroneous. */
13515 if (mem_initializer != error_mark_node)
13517 TREE_CHAIN (mem_initializer) = mem_initializer_list;
13518 mem_initializer_list = mem_initializer;
13520 /* If the next token is not a `,', we're done. */
13521 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13522 break;
13523 /* Consume the `,' token. */
13524 cp_lexer_consume_token (parser->lexer);
13527 /* Perform semantic analysis. */
13528 if (DECL_CONSTRUCTOR_P (current_function_decl))
13529 finish_mem_initializers (mem_initializer_list);
13532 /* Parse a mem-initializer.
13534 mem-initializer:
13535 mem-initializer-id ( expression-list [opt] )
13536 mem-initializer-id braced-init-list
13538 GNU extension:
13540 mem-initializer:
13541 ( expression-list [opt] )
13543 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
13544 class) or FIELD_DECL (for a non-static data member) to initialize;
13545 the TREE_VALUE is the expression-list. An empty initialization
13546 list is represented by void_list_node. */
13548 static tree
13549 cp_parser_mem_initializer (cp_parser* parser)
13551 tree mem_initializer_id;
13552 tree expression_list;
13553 tree member;
13554 cp_token *token = cp_lexer_peek_token (parser->lexer);
13556 /* Find out what is being initialized. */
13557 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
13559 permerror (token->location,
13560 "anachronistic old-style base class initializer");
13561 mem_initializer_id = NULL_TREE;
13563 else
13565 mem_initializer_id = cp_parser_mem_initializer_id (parser);
13566 if (mem_initializer_id == error_mark_node)
13567 return mem_initializer_id;
13569 member = expand_member_init (mem_initializer_id);
13570 if (member && !DECL_P (member))
13571 in_base_initializer = 1;
13573 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13575 bool expr_non_constant_p;
13576 cp_lexer_set_source_position (parser->lexer);
13577 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13578 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
13579 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
13580 expression_list = build_tree_list (NULL_TREE, expression_list);
13582 else
13584 vec<tree, va_gc> *vec;
13585 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
13586 /*cast_p=*/false,
13587 /*allow_expansion_p=*/true,
13588 /*non_constant_p=*/NULL);
13589 if (vec == NULL)
13590 return error_mark_node;
13591 expression_list = build_tree_list_vec (vec);
13592 release_tree_vector (vec);
13595 if (expression_list == error_mark_node)
13596 return error_mark_node;
13597 if (!expression_list)
13598 expression_list = void_type_node;
13600 in_base_initializer = 0;
13602 return member ? build_tree_list (member, expression_list) : error_mark_node;
13605 /* Parse a mem-initializer-id.
13607 mem-initializer-id:
13608 :: [opt] nested-name-specifier [opt] class-name
13609 decltype-specifier (C++11)
13610 identifier
13612 Returns a TYPE indicating the class to be initialized for the first
13613 production (and the second in C++11). Returns an IDENTIFIER_NODE
13614 indicating the data member to be initialized for the last production. */
13616 static tree
13617 cp_parser_mem_initializer_id (cp_parser* parser)
13619 bool global_scope_p;
13620 bool nested_name_specifier_p;
13621 bool template_p = false;
13622 tree id;
13624 cp_token *token = cp_lexer_peek_token (parser->lexer);
13626 /* `typename' is not allowed in this context ([temp.res]). */
13627 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13629 error_at (token->location,
13630 "keyword %<typename%> not allowed in this context (a qualified "
13631 "member initializer is implicitly a type)");
13632 cp_lexer_consume_token (parser->lexer);
13634 /* Look for the optional `::' operator. */
13635 global_scope_p
13636 = (cp_parser_global_scope_opt (parser,
13637 /*current_scope_valid_p=*/false)
13638 != NULL_TREE);
13639 /* Look for the optional nested-name-specifier. The simplest way to
13640 implement:
13642 [temp.res]
13644 The keyword `typename' is not permitted in a base-specifier or
13645 mem-initializer; in these contexts a qualified name that
13646 depends on a template-parameter is implicitly assumed to be a
13647 type name.
13649 is to assume that we have seen the `typename' keyword at this
13650 point. */
13651 nested_name_specifier_p
13652 = (cp_parser_nested_name_specifier_opt (parser,
13653 /*typename_keyword_p=*/true,
13654 /*check_dependency_p=*/true,
13655 /*type_p=*/true,
13656 /*is_declaration=*/true)
13657 != NULL_TREE);
13658 if (nested_name_specifier_p)
13659 template_p = cp_parser_optional_template_keyword (parser);
13660 /* If there is a `::' operator or a nested-name-specifier, then we
13661 are definitely looking for a class-name. */
13662 if (global_scope_p || nested_name_specifier_p)
13663 return cp_parser_class_name (parser,
13664 /*typename_keyword_p=*/true,
13665 /*template_keyword_p=*/template_p,
13666 typename_type,
13667 /*check_dependency_p=*/true,
13668 /*class_head_p=*/false,
13669 /*is_declaration=*/true);
13670 /* Otherwise, we could also be looking for an ordinary identifier. */
13671 cp_parser_parse_tentatively (parser);
13672 if (cp_lexer_next_token_is_decltype (parser->lexer))
13673 /* Try a decltype-specifier. */
13674 id = cp_parser_decltype (parser);
13675 else
13676 /* Otherwise, try a class-name. */
13677 id = cp_parser_class_name (parser,
13678 /*typename_keyword_p=*/true,
13679 /*template_keyword_p=*/false,
13680 none_type,
13681 /*check_dependency_p=*/true,
13682 /*class_head_p=*/false,
13683 /*is_declaration=*/true);
13684 /* If we found one, we're done. */
13685 if (cp_parser_parse_definitely (parser))
13686 return id;
13687 /* Otherwise, look for an ordinary identifier. */
13688 return cp_parser_identifier (parser);
13691 /* Overloading [gram.over] */
13693 /* Parse an operator-function-id.
13695 operator-function-id:
13696 operator operator
13698 Returns an IDENTIFIER_NODE for the operator which is a
13699 human-readable spelling of the identifier, e.g., `operator +'. */
13701 static cp_expr
13702 cp_parser_operator_function_id (cp_parser* parser)
13704 /* Look for the `operator' keyword. */
13705 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13706 return error_mark_node;
13707 /* And then the name of the operator itself. */
13708 return cp_parser_operator (parser);
13711 /* Return an identifier node for a user-defined literal operator.
13712 The suffix identifier is chained to the operator name identifier. */
13714 static tree
13715 cp_literal_operator_id (const char* name)
13717 tree identifier;
13718 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
13719 + strlen (name) + 10);
13720 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
13721 identifier = get_identifier (buffer);
13723 return identifier;
13726 /* Parse an operator.
13728 operator:
13729 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
13730 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
13731 || ++ -- , ->* -> () []
13733 GNU Extensions:
13735 operator:
13736 <? >? <?= >?=
13738 Returns an IDENTIFIER_NODE for the operator which is a
13739 human-readable spelling of the identifier, e.g., `operator +'. */
13741 static cp_expr
13742 cp_parser_operator (cp_parser* parser)
13744 tree id = NULL_TREE;
13745 cp_token *token;
13746 bool utf8 = false;
13748 /* Peek at the next token. */
13749 token = cp_lexer_peek_token (parser->lexer);
13751 location_t start_loc = token->location;
13753 /* Figure out which operator we have. */
13754 switch (token->type)
13756 case CPP_KEYWORD:
13758 enum tree_code op;
13760 /* The keyword should be either `new' or `delete'. */
13761 if (token->keyword == RID_NEW)
13762 op = NEW_EXPR;
13763 else if (token->keyword == RID_DELETE)
13764 op = DELETE_EXPR;
13765 else
13766 break;
13768 /* Consume the `new' or `delete' token. */
13769 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
13771 /* Peek at the next token. */
13772 token = cp_lexer_peek_token (parser->lexer);
13773 /* If it's a `[' token then this is the array variant of the
13774 operator. */
13775 if (token->type == CPP_OPEN_SQUARE)
13777 /* Consume the `[' token. */
13778 cp_lexer_consume_token (parser->lexer);
13779 /* Look for the `]' token. */
13780 end_loc = cp_parser_require (parser, CPP_CLOSE_SQUARE,
13781 RT_CLOSE_SQUARE)->location;
13782 id = ansi_opname (op == NEW_EXPR
13783 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
13785 /* Otherwise, we have the non-array variant. */
13786 else
13787 id = ansi_opname (op);
13789 location_t loc = make_location (start_loc, start_loc, end_loc);
13791 return cp_expr (id, loc);
13794 case CPP_PLUS:
13795 id = ansi_opname (PLUS_EXPR);
13796 break;
13798 case CPP_MINUS:
13799 id = ansi_opname (MINUS_EXPR);
13800 break;
13802 case CPP_MULT:
13803 id = ansi_opname (MULT_EXPR);
13804 break;
13806 case CPP_DIV:
13807 id = ansi_opname (TRUNC_DIV_EXPR);
13808 break;
13810 case CPP_MOD:
13811 id = ansi_opname (TRUNC_MOD_EXPR);
13812 break;
13814 case CPP_XOR:
13815 id = ansi_opname (BIT_XOR_EXPR);
13816 break;
13818 case CPP_AND:
13819 id = ansi_opname (BIT_AND_EXPR);
13820 break;
13822 case CPP_OR:
13823 id = ansi_opname (BIT_IOR_EXPR);
13824 break;
13826 case CPP_COMPL:
13827 id = ansi_opname (BIT_NOT_EXPR);
13828 break;
13830 case CPP_NOT:
13831 id = ansi_opname (TRUTH_NOT_EXPR);
13832 break;
13834 case CPP_EQ:
13835 id = ansi_assopname (NOP_EXPR);
13836 break;
13838 case CPP_LESS:
13839 id = ansi_opname (LT_EXPR);
13840 break;
13842 case CPP_GREATER:
13843 id = ansi_opname (GT_EXPR);
13844 break;
13846 case CPP_PLUS_EQ:
13847 id = ansi_assopname (PLUS_EXPR);
13848 break;
13850 case CPP_MINUS_EQ:
13851 id = ansi_assopname (MINUS_EXPR);
13852 break;
13854 case CPP_MULT_EQ:
13855 id = ansi_assopname (MULT_EXPR);
13856 break;
13858 case CPP_DIV_EQ:
13859 id = ansi_assopname (TRUNC_DIV_EXPR);
13860 break;
13862 case CPP_MOD_EQ:
13863 id = ansi_assopname (TRUNC_MOD_EXPR);
13864 break;
13866 case CPP_XOR_EQ:
13867 id = ansi_assopname (BIT_XOR_EXPR);
13868 break;
13870 case CPP_AND_EQ:
13871 id = ansi_assopname (BIT_AND_EXPR);
13872 break;
13874 case CPP_OR_EQ:
13875 id = ansi_assopname (BIT_IOR_EXPR);
13876 break;
13878 case CPP_LSHIFT:
13879 id = ansi_opname (LSHIFT_EXPR);
13880 break;
13882 case CPP_RSHIFT:
13883 id = ansi_opname (RSHIFT_EXPR);
13884 break;
13886 case CPP_LSHIFT_EQ:
13887 id = ansi_assopname (LSHIFT_EXPR);
13888 break;
13890 case CPP_RSHIFT_EQ:
13891 id = ansi_assopname (RSHIFT_EXPR);
13892 break;
13894 case CPP_EQ_EQ:
13895 id = ansi_opname (EQ_EXPR);
13896 break;
13898 case CPP_NOT_EQ:
13899 id = ansi_opname (NE_EXPR);
13900 break;
13902 case CPP_LESS_EQ:
13903 id = ansi_opname (LE_EXPR);
13904 break;
13906 case CPP_GREATER_EQ:
13907 id = ansi_opname (GE_EXPR);
13908 break;
13910 case CPP_AND_AND:
13911 id = ansi_opname (TRUTH_ANDIF_EXPR);
13912 break;
13914 case CPP_OR_OR:
13915 id = ansi_opname (TRUTH_ORIF_EXPR);
13916 break;
13918 case CPP_PLUS_PLUS:
13919 id = ansi_opname (POSTINCREMENT_EXPR);
13920 break;
13922 case CPP_MINUS_MINUS:
13923 id = ansi_opname (PREDECREMENT_EXPR);
13924 break;
13926 case CPP_COMMA:
13927 id = ansi_opname (COMPOUND_EXPR);
13928 break;
13930 case CPP_DEREF_STAR:
13931 id = ansi_opname (MEMBER_REF);
13932 break;
13934 case CPP_DEREF:
13935 id = ansi_opname (COMPONENT_REF);
13936 break;
13938 case CPP_OPEN_PAREN:
13939 /* Consume the `('. */
13940 cp_lexer_consume_token (parser->lexer);
13941 /* Look for the matching `)'. */
13942 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13943 return ansi_opname (CALL_EXPR);
13945 case CPP_OPEN_SQUARE:
13946 /* Consume the `['. */
13947 cp_lexer_consume_token (parser->lexer);
13948 /* Look for the matching `]'. */
13949 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13950 return ansi_opname (ARRAY_REF);
13952 case CPP_UTF8STRING:
13953 case CPP_UTF8STRING_USERDEF:
13954 utf8 = true;
13955 case CPP_STRING:
13956 case CPP_WSTRING:
13957 case CPP_STRING16:
13958 case CPP_STRING32:
13959 case CPP_STRING_USERDEF:
13960 case CPP_WSTRING_USERDEF:
13961 case CPP_STRING16_USERDEF:
13962 case CPP_STRING32_USERDEF:
13964 tree str, string_tree;
13965 int sz, len;
13967 if (cxx_dialect == cxx98)
13968 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
13970 /* Consume the string. */
13971 str = cp_parser_string_literal (parser, /*translate=*/true,
13972 /*wide_ok=*/true, /*lookup_udlit=*/false);
13973 if (str == error_mark_node)
13974 return error_mark_node;
13975 else if (TREE_CODE (str) == USERDEF_LITERAL)
13977 string_tree = USERDEF_LITERAL_VALUE (str);
13978 id = USERDEF_LITERAL_SUFFIX_ID (str);
13980 else
13982 string_tree = str;
13983 /* Look for the suffix identifier. */
13984 token = cp_lexer_peek_token (parser->lexer);
13985 if (token->type == CPP_NAME)
13986 id = cp_parser_identifier (parser);
13987 else if (token->type == CPP_KEYWORD)
13989 error ("unexpected keyword;"
13990 " remove space between quotes and suffix identifier");
13991 return error_mark_node;
13993 else
13995 error ("expected suffix identifier");
13996 return error_mark_node;
13999 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14000 (TREE_TYPE (TREE_TYPE (string_tree))));
14001 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14002 if (len != 0)
14004 error ("expected empty string after %<operator%> keyword");
14005 return error_mark_node;
14007 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14008 != char_type_node)
14010 error ("invalid encoding prefix in literal operator");
14011 return error_mark_node;
14013 if (id != error_mark_node)
14015 const char *name = IDENTIFIER_POINTER (id);
14016 id = cp_literal_operator_id (name);
14018 return id;
14021 default:
14022 /* Anything else is an error. */
14023 break;
14026 /* If we have selected an identifier, we need to consume the
14027 operator token. */
14028 if (id)
14029 cp_lexer_consume_token (parser->lexer);
14030 /* Otherwise, no valid operator name was present. */
14031 else
14033 cp_parser_error (parser, "expected operator");
14034 id = error_mark_node;
14037 return cp_expr (id, start_loc);
14040 /* Parse a template-declaration.
14042 template-declaration:
14043 export [opt] template < template-parameter-list > declaration
14045 If MEMBER_P is TRUE, this template-declaration occurs within a
14046 class-specifier.
14048 The grammar rule given by the standard isn't correct. What
14049 is really meant is:
14051 template-declaration:
14052 export [opt] template-parameter-list-seq
14053 decl-specifier-seq [opt] init-declarator [opt] ;
14054 export [opt] template-parameter-list-seq
14055 function-definition
14057 template-parameter-list-seq:
14058 template-parameter-list-seq [opt]
14059 template < template-parameter-list >
14061 Concept Extensions:
14063 template-parameter-list-seq:
14064 template < template-parameter-list > requires-clause [opt]
14066 requires-clause:
14067 requires logical-or-expression */
14069 static void
14070 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14072 /* Check for `export'. */
14073 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14075 /* Consume the `export' token. */
14076 cp_lexer_consume_token (parser->lexer);
14077 /* Warn that we do not support `export'. */
14078 warning (0, "keyword %<export%> not implemented, and will be ignored");
14081 cp_parser_template_declaration_after_export (parser, member_p);
14084 /* Parse a template-parameter-list.
14086 template-parameter-list:
14087 template-parameter
14088 template-parameter-list , template-parameter
14090 Returns a TREE_LIST. Each node represents a template parameter.
14091 The nodes are connected via their TREE_CHAINs. */
14093 static tree
14094 cp_parser_template_parameter_list (cp_parser* parser)
14096 tree parameter_list = NULL_TREE;
14098 begin_template_parm_list ();
14100 /* The loop below parses the template parms. We first need to know
14101 the total number of template parms to be able to compute proper
14102 canonical types of each dependent type. So after the loop, when
14103 we know the total number of template parms,
14104 end_template_parm_list computes the proper canonical types and
14105 fixes up the dependent types accordingly. */
14106 while (true)
14108 tree parameter;
14109 bool is_non_type;
14110 bool is_parameter_pack;
14111 location_t parm_loc;
14113 /* Parse the template-parameter. */
14114 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
14115 parameter = cp_parser_template_parameter (parser,
14116 &is_non_type,
14117 &is_parameter_pack);
14118 /* Add it to the list. */
14119 if (parameter != error_mark_node)
14120 parameter_list = process_template_parm (parameter_list,
14121 parm_loc,
14122 parameter,
14123 is_non_type,
14124 is_parameter_pack);
14125 else
14127 tree err_parm = build_tree_list (parameter, parameter);
14128 parameter_list = chainon (parameter_list, err_parm);
14131 /* If the next token is not a `,', we're done. */
14132 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14133 break;
14134 /* Otherwise, consume the `,' token. */
14135 cp_lexer_consume_token (parser->lexer);
14138 return end_template_parm_list (parameter_list);
14141 /* Parse a introduction-list.
14143 introduction-list:
14144 introduced-parameter
14145 introduction-list , introduced-parameter
14147 introduced-parameter:
14148 ...[opt] identifier
14150 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
14151 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
14152 WILDCARD_DECL will also have DECL_NAME set and token location in
14153 DECL_SOURCE_LOCATION. */
14155 static tree
14156 cp_parser_introduction_list (cp_parser *parser)
14158 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
14160 while (true)
14162 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14163 if (is_pack)
14164 cp_lexer_consume_token (parser->lexer);
14166 /* Build placeholder. */
14167 tree parm = build_nt (WILDCARD_DECL);
14168 DECL_SOURCE_LOCATION (parm)
14169 = cp_lexer_peek_token (parser->lexer)->location;
14170 DECL_NAME (parm) = cp_parser_identifier (parser);
14171 WILDCARD_PACK_P (parm) = is_pack;
14172 vec_safe_push (introduction_vec, parm);
14174 /* If the next token is not a `,', we're done. */
14175 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14176 break;
14177 /* Otherwise, consume the `,' token. */
14178 cp_lexer_consume_token (parser->lexer);
14181 /* Convert the vec into a TREE_VEC. */
14182 tree introduction_list = make_tree_vec (introduction_vec->length ());
14183 unsigned int n;
14184 tree parm;
14185 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
14186 TREE_VEC_ELT (introduction_list, n) = parm;
14188 release_tree_vector (introduction_vec);
14189 return introduction_list;
14192 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
14193 is an abstract declarator. */
14195 static inline cp_declarator*
14196 get_id_declarator (cp_declarator *declarator)
14198 cp_declarator *d = declarator;
14199 while (d && d->kind != cdk_id)
14200 d = d->declarator;
14201 return d;
14204 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
14205 is an abstract declarator. */
14207 static inline tree
14208 get_unqualified_id (cp_declarator *declarator)
14210 declarator = get_id_declarator (declarator);
14211 if (declarator)
14212 return declarator->u.id.unqualified_name;
14213 else
14214 return NULL_TREE;
14217 /* Returns true if DECL represents a constrained-parameter. */
14219 static inline bool
14220 is_constrained_parameter (tree decl)
14222 return (decl
14223 && TREE_CODE (decl) == TYPE_DECL
14224 && CONSTRAINED_PARM_CONCEPT (decl)
14225 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
14228 /* Returns true if PARM declares a constrained-parameter. */
14230 static inline bool
14231 is_constrained_parameter (cp_parameter_declarator *parm)
14233 return is_constrained_parameter (parm->decl_specifiers.type);
14236 /* Check that the type parameter is only a declarator-id, and that its
14237 type is not cv-qualified. */
14239 bool
14240 cp_parser_check_constrained_type_parm (cp_parser *parser,
14241 cp_parameter_declarator *parm)
14243 if (!parm->declarator)
14244 return true;
14246 if (parm->declarator->kind != cdk_id)
14248 cp_parser_error (parser, "invalid constrained type parameter");
14249 return false;
14252 /* Don't allow cv-qualified type parameters. */
14253 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
14254 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
14256 cp_parser_error (parser, "cv-qualified type parameter");
14257 return false;
14260 return true;
14263 /* Finish parsing/processing a template type parameter and checking
14264 various restrictions. */
14266 static inline tree
14267 cp_parser_constrained_type_template_parm (cp_parser *parser,
14268 tree id,
14269 cp_parameter_declarator* parmdecl)
14271 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
14272 return finish_template_type_parm (class_type_node, id);
14273 else
14274 return error_mark_node;
14277 static tree
14278 finish_constrained_template_template_parm (tree proto, tree id)
14280 /* FIXME: This should probably be copied, and we may need to adjust
14281 the template parameter depths. */
14282 tree saved_parms = current_template_parms;
14283 begin_template_parm_list ();
14284 current_template_parms = DECL_TEMPLATE_PARMS (proto);
14285 end_template_parm_list ();
14287 tree parm = finish_template_template_parm (class_type_node, id);
14288 current_template_parms = saved_parms;
14290 return parm;
14293 /* Finish parsing/processing a template template parameter by borrowing
14294 the template parameter list from the prototype parameter. */
14296 static tree
14297 cp_parser_constrained_template_template_parm (cp_parser *parser,
14298 tree proto,
14299 tree id,
14300 cp_parameter_declarator *parmdecl)
14302 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
14303 return error_mark_node;
14304 return finish_constrained_template_template_parm (proto, id);
14307 /* Create a new non-type template parameter from the given PARM
14308 declarator. */
14310 static tree
14311 constrained_non_type_template_parm (bool *is_non_type,
14312 cp_parameter_declarator *parm)
14314 *is_non_type = true;
14315 cp_declarator *decl = parm->declarator;
14316 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
14317 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
14318 return grokdeclarator (decl, specs, TPARM, 0, NULL);
14321 /* Build a constrained template parameter based on the PARMDECL
14322 declarator. The type of PARMDECL is the constrained type, which
14323 refers to the prototype template parameter that ultimately
14324 specifies the type of the declared parameter. */
14326 static tree
14327 finish_constrained_parameter (cp_parser *parser,
14328 cp_parameter_declarator *parmdecl,
14329 bool *is_non_type,
14330 bool *is_parameter_pack)
14332 tree decl = parmdecl->decl_specifiers.type;
14333 tree id = get_unqualified_id (parmdecl->declarator);
14334 tree def = parmdecl->default_argument;
14335 tree proto = DECL_INITIAL (decl);
14337 /* A template parameter constrained by a variadic concept shall also
14338 be declared as a template parameter pack. */
14339 bool is_variadic = template_parameter_pack_p (proto);
14340 if (is_variadic && !*is_parameter_pack)
14341 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
14343 /* Build the parameter. Return an error if the declarator was invalid. */
14344 tree parm;
14345 if (TREE_CODE (proto) == TYPE_DECL)
14346 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
14347 else if (TREE_CODE (proto) == TEMPLATE_DECL)
14348 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
14349 parmdecl);
14350 else
14351 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
14352 if (parm == error_mark_node)
14353 return error_mark_node;
14355 /* Finish the parameter decl and create a node attaching the
14356 default argument and constraint. */
14357 parm = build_tree_list (def, parm);
14358 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
14360 return parm;
14363 /* Returns true if the parsed type actually represents the declaration
14364 of a type template-parameter. */
14366 static inline bool
14367 declares_constrained_type_template_parameter (tree type)
14369 return (is_constrained_parameter (type)
14370 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
14374 /* Returns true if the parsed type actually represents the declaration of
14375 a template template-parameter. */
14377 static bool
14378 declares_constrained_template_template_parameter (tree type)
14380 return (is_constrained_parameter (type)
14381 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
14384 /* Parse a default argument for a type template-parameter.
14385 Note that diagnostics are handled in cp_parser_template_parameter. */
14387 static tree
14388 cp_parser_default_type_template_argument (cp_parser *parser)
14390 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14392 /* Consume the `=' token. */
14393 cp_lexer_consume_token (parser->lexer);
14395 cp_token *token = cp_lexer_peek_token (parser->lexer);
14397 /* Parse the default-argument. */
14398 push_deferring_access_checks (dk_no_deferred);
14399 tree default_argument = cp_parser_type_id (parser);
14400 pop_deferring_access_checks ();
14402 if (flag_concepts && type_uses_auto (default_argument))
14404 error_at (token->location,
14405 "invalid use of %<auto%> in default template argument");
14406 return error_mark_node;
14409 return default_argument;
14412 /* Parse a default argument for a template template-parameter. */
14414 static tree
14415 cp_parser_default_template_template_argument (cp_parser *parser)
14417 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14419 bool is_template;
14421 /* Consume the `='. */
14422 cp_lexer_consume_token (parser->lexer);
14423 /* Parse the id-expression. */
14424 push_deferring_access_checks (dk_no_deferred);
14425 /* save token before parsing the id-expression, for error
14426 reporting */
14427 const cp_token* token = cp_lexer_peek_token (parser->lexer);
14428 tree default_argument
14429 = cp_parser_id_expression (parser,
14430 /*template_keyword_p=*/false,
14431 /*check_dependency_p=*/true,
14432 /*template_p=*/&is_template,
14433 /*declarator_p=*/false,
14434 /*optional_p=*/false);
14435 if (TREE_CODE (default_argument) == TYPE_DECL)
14436 /* If the id-expression was a template-id that refers to
14437 a template-class, we already have the declaration here,
14438 so no further lookup is needed. */
14440 else
14441 /* Look up the name. */
14442 default_argument
14443 = cp_parser_lookup_name (parser, default_argument,
14444 none_type,
14445 /*is_template=*/is_template,
14446 /*is_namespace=*/false,
14447 /*check_dependency=*/true,
14448 /*ambiguous_decls=*/NULL,
14449 token->location);
14450 /* See if the default argument is valid. */
14451 default_argument = check_template_template_default_arg (default_argument);
14452 pop_deferring_access_checks ();
14453 return default_argument;
14456 /* Parse a template-parameter.
14458 template-parameter:
14459 type-parameter
14460 parameter-declaration
14462 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
14463 the parameter. The TREE_PURPOSE is the default value, if any.
14464 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
14465 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
14466 set to true iff this parameter is a parameter pack. */
14468 static tree
14469 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
14470 bool *is_parameter_pack)
14472 cp_token *token;
14473 cp_parameter_declarator *parameter_declarator;
14474 tree parm;
14476 /* Assume it is a type parameter or a template parameter. */
14477 *is_non_type = false;
14478 /* Assume it not a parameter pack. */
14479 *is_parameter_pack = false;
14480 /* Peek at the next token. */
14481 token = cp_lexer_peek_token (parser->lexer);
14482 /* If it is `class' or `template', we have a type-parameter. */
14483 if (token->keyword == RID_TEMPLATE)
14484 return cp_parser_type_parameter (parser, is_parameter_pack);
14485 /* If it is `class' or `typename' we do not know yet whether it is a
14486 type parameter or a non-type parameter. Consider:
14488 template <typename T, typename T::X X> ...
14492 template <class C, class D*> ...
14494 Here, the first parameter is a type parameter, and the second is
14495 a non-type parameter. We can tell by looking at the token after
14496 the identifier -- if it is a `,', `=', or `>' then we have a type
14497 parameter. */
14498 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
14500 /* Peek at the token after `class' or `typename'. */
14501 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14502 /* If it's an ellipsis, we have a template type parameter
14503 pack. */
14504 if (token->type == CPP_ELLIPSIS)
14505 return cp_parser_type_parameter (parser, is_parameter_pack);
14506 /* If it's an identifier, skip it. */
14507 if (token->type == CPP_NAME)
14508 token = cp_lexer_peek_nth_token (parser->lexer, 3);
14509 /* Now, see if the token looks like the end of a template
14510 parameter. */
14511 if (token->type == CPP_COMMA
14512 || token->type == CPP_EQ
14513 || token->type == CPP_GREATER)
14514 return cp_parser_type_parameter (parser, is_parameter_pack);
14517 /* Otherwise, it is a non-type parameter or a constrained parameter.
14519 [temp.param]
14521 When parsing a default template-argument for a non-type
14522 template-parameter, the first non-nested `>' is taken as the end
14523 of the template parameter-list rather than a greater-than
14524 operator. */
14525 parameter_declarator
14526 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
14527 /*parenthesized_p=*/NULL);
14529 if (!parameter_declarator)
14530 return error_mark_node;
14532 /* If the parameter declaration is marked as a parameter pack, set
14533 *IS_PARAMETER_PACK to notify the caller. */
14534 if (parameter_declarator->template_parameter_pack_p)
14535 *is_parameter_pack = true;
14537 if (parameter_declarator->default_argument)
14539 /* Can happen in some cases of erroneous input (c++/34892). */
14540 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14541 /* Consume the `...' for better error recovery. */
14542 cp_lexer_consume_token (parser->lexer);
14545 // The parameter may have been constrained.
14546 if (is_constrained_parameter (parameter_declarator))
14547 return finish_constrained_parameter (parser,
14548 parameter_declarator,
14549 is_non_type,
14550 is_parameter_pack);
14552 // Now we're sure that the parameter is a non-type parameter.
14553 *is_non_type = true;
14555 parm = grokdeclarator (parameter_declarator->declarator,
14556 &parameter_declarator->decl_specifiers,
14557 TPARM, /*initialized=*/0,
14558 /*attrlist=*/NULL);
14559 if (parm == error_mark_node)
14560 return error_mark_node;
14562 return build_tree_list (parameter_declarator->default_argument, parm);
14565 /* Parse a type-parameter.
14567 type-parameter:
14568 class identifier [opt]
14569 class identifier [opt] = type-id
14570 typename identifier [opt]
14571 typename identifier [opt] = type-id
14572 template < template-parameter-list > class identifier [opt]
14573 template < template-parameter-list > class identifier [opt]
14574 = id-expression
14576 GNU Extension (variadic templates):
14578 type-parameter:
14579 class ... identifier [opt]
14580 typename ... identifier [opt]
14582 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
14583 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
14584 the declaration of the parameter.
14586 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
14588 static tree
14589 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
14591 cp_token *token;
14592 tree parameter;
14594 /* Look for a keyword to tell us what kind of parameter this is. */
14595 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
14596 if (!token)
14597 return error_mark_node;
14599 switch (token->keyword)
14601 case RID_CLASS:
14602 case RID_TYPENAME:
14604 tree identifier;
14605 tree default_argument;
14607 /* If the next token is an ellipsis, we have a template
14608 argument pack. */
14609 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14611 /* Consume the `...' token. */
14612 cp_lexer_consume_token (parser->lexer);
14613 maybe_warn_variadic_templates ();
14615 *is_parameter_pack = true;
14618 /* If the next token is an identifier, then it names the
14619 parameter. */
14620 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14621 identifier = cp_parser_identifier (parser);
14622 else
14623 identifier = NULL_TREE;
14625 /* Create the parameter. */
14626 parameter = finish_template_type_parm (class_type_node, identifier);
14628 /* If the next token is an `=', we have a default argument. */
14629 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14631 default_argument
14632 = cp_parser_default_type_template_argument (parser);
14634 /* Template parameter packs cannot have default
14635 arguments. */
14636 if (*is_parameter_pack)
14638 if (identifier)
14639 error_at (token->location,
14640 "template parameter pack %qD cannot have a "
14641 "default argument", identifier);
14642 else
14643 error_at (token->location,
14644 "template parameter packs cannot have "
14645 "default arguments");
14646 default_argument = NULL_TREE;
14648 else if (check_for_bare_parameter_packs (default_argument))
14649 default_argument = error_mark_node;
14651 else
14652 default_argument = NULL_TREE;
14654 /* Create the combined representation of the parameter and the
14655 default argument. */
14656 parameter = build_tree_list (default_argument, parameter);
14658 break;
14660 case RID_TEMPLATE:
14662 tree identifier;
14663 tree default_argument;
14665 /* Look for the `<'. */
14666 cp_parser_require (parser, CPP_LESS, RT_LESS);
14667 /* Parse the template-parameter-list. */
14668 cp_parser_template_parameter_list (parser);
14669 /* Look for the `>'. */
14670 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14672 // If template requirements are present, parse them.
14673 tree reqs = get_shorthand_constraints (current_template_parms);
14674 if (tree r = cp_parser_requires_clause_opt (parser))
14675 reqs = conjoin_constraints (reqs, make_predicate_constraint (r));
14676 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
14678 /* Look for the `class' or 'typename' keywords. */
14679 cp_parser_type_parameter_key (parser);
14680 /* If the next token is an ellipsis, we have a template
14681 argument pack. */
14682 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14684 /* Consume the `...' token. */
14685 cp_lexer_consume_token (parser->lexer);
14686 maybe_warn_variadic_templates ();
14688 *is_parameter_pack = true;
14690 /* If the next token is an `=', then there is a
14691 default-argument. If the next token is a `>', we are at
14692 the end of the parameter-list. If the next token is a `,',
14693 then we are at the end of this parameter. */
14694 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
14695 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
14696 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14698 identifier = cp_parser_identifier (parser);
14699 /* Treat invalid names as if the parameter were nameless. */
14700 if (identifier == error_mark_node)
14701 identifier = NULL_TREE;
14703 else
14704 identifier = NULL_TREE;
14706 /* Create the template parameter. */
14707 parameter = finish_template_template_parm (class_type_node,
14708 identifier);
14710 /* If the next token is an `=', then there is a
14711 default-argument. */
14712 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14714 default_argument
14715 = cp_parser_default_template_template_argument (parser);
14717 /* Template parameter packs cannot have default
14718 arguments. */
14719 if (*is_parameter_pack)
14721 if (identifier)
14722 error_at (token->location,
14723 "template parameter pack %qD cannot "
14724 "have a default argument",
14725 identifier);
14726 else
14727 error_at (token->location, "template parameter packs cannot "
14728 "have default arguments");
14729 default_argument = NULL_TREE;
14732 else
14733 default_argument = NULL_TREE;
14735 /* Create the combined representation of the parameter and the
14736 default argument. */
14737 parameter = build_tree_list (default_argument, parameter);
14739 break;
14741 default:
14742 gcc_unreachable ();
14743 break;
14746 return parameter;
14749 /* Parse a template-id.
14751 template-id:
14752 template-name < template-argument-list [opt] >
14754 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
14755 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
14756 returned. Otherwise, if the template-name names a function, or set
14757 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
14758 names a class, returns a TYPE_DECL for the specialization.
14760 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
14761 uninstantiated templates. */
14763 static tree
14764 cp_parser_template_id (cp_parser *parser,
14765 bool template_keyword_p,
14766 bool check_dependency_p,
14767 enum tag_types tag_type,
14768 bool is_declaration)
14770 tree templ;
14771 tree arguments;
14772 tree template_id;
14773 cp_token_position start_of_id = 0;
14774 cp_token *next_token = NULL, *next_token_2 = NULL;
14775 bool is_identifier;
14777 /* If the next token corresponds to a template-id, there is no need
14778 to reparse it. */
14779 next_token = cp_lexer_peek_token (parser->lexer);
14780 if (next_token->type == CPP_TEMPLATE_ID)
14782 cp_lexer_consume_token (parser->lexer);
14783 return saved_checks_value (next_token->u.tree_check_value);
14786 /* Avoid performing name lookup if there is no possibility of
14787 finding a template-id. */
14788 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
14789 || (next_token->type == CPP_NAME
14790 && !cp_parser_nth_token_starts_template_argument_list_p
14791 (parser, 2)))
14793 cp_parser_error (parser, "expected template-id");
14794 return error_mark_node;
14797 /* Remember where the template-id starts. */
14798 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
14799 start_of_id = cp_lexer_token_position (parser->lexer, false);
14801 push_deferring_access_checks (dk_deferred);
14803 /* Parse the template-name. */
14804 is_identifier = false;
14805 templ = cp_parser_template_name (parser, template_keyword_p,
14806 check_dependency_p,
14807 is_declaration,
14808 tag_type,
14809 &is_identifier);
14810 if (templ == error_mark_node || is_identifier)
14812 pop_deferring_access_checks ();
14813 return templ;
14816 /* Since we're going to preserve any side-effects from this parse, set up a
14817 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14818 in the template arguments. */
14819 tentative_firewall firewall (parser);
14821 /* If we find the sequence `[:' after a template-name, it's probably
14822 a digraph-typo for `< ::'. Substitute the tokens and check if we can
14823 parse correctly the argument list. */
14824 next_token = cp_lexer_peek_token (parser->lexer);
14825 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14826 if (next_token->type == CPP_OPEN_SQUARE
14827 && next_token->flags & DIGRAPH
14828 && next_token_2->type == CPP_COLON
14829 && !(next_token_2->flags & PREV_WHITE))
14831 cp_parser_parse_tentatively (parser);
14832 /* Change `:' into `::'. */
14833 next_token_2->type = CPP_SCOPE;
14834 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
14835 CPP_LESS. */
14836 cp_lexer_consume_token (parser->lexer);
14838 /* Parse the arguments. */
14839 arguments = cp_parser_enclosed_template_argument_list (parser);
14840 if (!cp_parser_parse_definitely (parser))
14842 /* If we couldn't parse an argument list, then we revert our changes
14843 and return simply an error. Maybe this is not a template-id
14844 after all. */
14845 next_token_2->type = CPP_COLON;
14846 cp_parser_error (parser, "expected %<<%>");
14847 pop_deferring_access_checks ();
14848 return error_mark_node;
14850 /* Otherwise, emit an error about the invalid digraph, but continue
14851 parsing because we got our argument list. */
14852 if (permerror (next_token->location,
14853 "%<<::%> cannot begin a template-argument list"))
14855 static bool hint = false;
14856 inform (next_token->location,
14857 "%<<:%> is an alternate spelling for %<[%>."
14858 " Insert whitespace between %<<%> and %<::%>");
14859 if (!hint && !flag_permissive)
14861 inform (next_token->location, "(if you use %<-fpermissive%> "
14862 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
14863 "accept your code)");
14864 hint = true;
14868 else
14870 /* Look for the `<' that starts the template-argument-list. */
14871 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
14873 pop_deferring_access_checks ();
14874 return error_mark_node;
14876 /* Parse the arguments. */
14877 arguments = cp_parser_enclosed_template_argument_list (parser);
14880 /* Build a representation of the specialization. */
14881 if (identifier_p (templ))
14882 template_id = build_min_nt_loc (next_token->location,
14883 TEMPLATE_ID_EXPR,
14884 templ, arguments);
14885 else if (DECL_TYPE_TEMPLATE_P (templ)
14886 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
14888 bool entering_scope;
14889 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
14890 template (rather than some instantiation thereof) only if
14891 is not nested within some other construct. For example, in
14892 "template <typename T> void f(T) { A<T>::", A<T> is just an
14893 instantiation of A. */
14894 entering_scope = (template_parm_scope_p ()
14895 && cp_lexer_next_token_is (parser->lexer,
14896 CPP_SCOPE));
14897 template_id
14898 = finish_template_type (templ, arguments, entering_scope);
14900 /* A template-like identifier may be a partial concept id. */
14901 else if (flag_concepts
14902 && (template_id = (cp_parser_maybe_partial_concept_id
14903 (parser, templ, arguments))))
14904 return template_id;
14905 else if (variable_template_p (templ))
14907 template_id = lookup_template_variable (templ, arguments);
14908 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
14909 SET_EXPR_LOCATION (template_id, next_token->location);
14911 else
14913 /* If it's not a class-template or a template-template, it should be
14914 a function-template. */
14915 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
14916 || TREE_CODE (templ) == OVERLOAD
14917 || BASELINK_P (templ)));
14919 template_id = lookup_template_function (templ, arguments);
14920 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
14921 SET_EXPR_LOCATION (template_id, next_token->location);
14924 /* If parsing tentatively, replace the sequence of tokens that makes
14925 up the template-id with a CPP_TEMPLATE_ID token. That way,
14926 should we re-parse the token stream, we will not have to repeat
14927 the effort required to do the parse, nor will we issue duplicate
14928 error messages about problems during instantiation of the
14929 template. */
14930 if (start_of_id
14931 /* Don't do this if we had a parse error in a declarator; re-parsing
14932 might succeed if a name changes meaning (60361). */
14933 && !(cp_parser_error_occurred (parser)
14934 && cp_parser_parsing_tentatively (parser)
14935 && parser->in_declarator_p))
14937 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
14939 /* Reset the contents of the START_OF_ID token. */
14940 token->type = CPP_TEMPLATE_ID;
14942 /* Update the location to be of the form:
14943 template-name < template-argument-list [opt] >
14944 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14945 with caret == start at the start of the template-name,
14946 ranging until the closing '>'. */
14947 location_t finish_loc
14948 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
14949 location_t combined_loc
14950 = make_location (token->location, token->location, finish_loc);
14951 token->location = combined_loc;
14953 /* Retrieve any deferred checks. Do not pop this access checks yet
14954 so the memory will not be reclaimed during token replacing below. */
14955 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14956 token->u.tree_check_value->value = template_id;
14957 token->u.tree_check_value->checks = get_deferred_access_checks ();
14958 token->keyword = RID_MAX;
14960 /* Purge all subsequent tokens. */
14961 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
14963 /* ??? Can we actually assume that, if template_id ==
14964 error_mark_node, we will have issued a diagnostic to the
14965 user, as opposed to simply marking the tentative parse as
14966 failed? */
14967 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
14968 error_at (token->location, "parse error in template argument list");
14971 pop_to_parent_deferring_access_checks ();
14972 return template_id;
14975 /* Parse a template-name.
14977 template-name:
14978 identifier
14980 The standard should actually say:
14982 template-name:
14983 identifier
14984 operator-function-id
14986 A defect report has been filed about this issue.
14988 A conversion-function-id cannot be a template name because they cannot
14989 be part of a template-id. In fact, looking at this code:
14991 a.operator K<int>()
14993 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
14994 It is impossible to call a templated conversion-function-id with an
14995 explicit argument list, since the only allowed template parameter is
14996 the type to which it is converting.
14998 If TEMPLATE_KEYWORD_P is true, then we have just seen the
14999 `template' keyword, in a construction like:
15001 T::template f<3>()
15003 In that case `f' is taken to be a template-name, even though there
15004 is no way of knowing for sure.
15006 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15007 name refers to a set of overloaded functions, at least one of which
15008 is a template, or an IDENTIFIER_NODE with the name of the template,
15009 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15010 names are looked up inside uninstantiated templates. */
15012 static tree
15013 cp_parser_template_name (cp_parser* parser,
15014 bool template_keyword_p,
15015 bool check_dependency_p,
15016 bool is_declaration,
15017 enum tag_types tag_type,
15018 bool *is_identifier)
15020 tree identifier;
15021 tree decl;
15022 tree fns;
15023 cp_token *token = cp_lexer_peek_token (parser->lexer);
15025 /* If the next token is `operator', then we have either an
15026 operator-function-id or a conversion-function-id. */
15027 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15029 /* We don't know whether we're looking at an
15030 operator-function-id or a conversion-function-id. */
15031 cp_parser_parse_tentatively (parser);
15032 /* Try an operator-function-id. */
15033 identifier = cp_parser_operator_function_id (parser);
15034 /* If that didn't work, try a conversion-function-id. */
15035 if (!cp_parser_parse_definitely (parser))
15037 cp_parser_error (parser, "expected template-name");
15038 return error_mark_node;
15041 /* Look for the identifier. */
15042 else
15043 identifier = cp_parser_identifier (parser);
15045 /* If we didn't find an identifier, we don't have a template-id. */
15046 if (identifier == error_mark_node)
15047 return error_mark_node;
15049 /* If the name immediately followed the `template' keyword, then it
15050 is a template-name. However, if the next token is not `<', then
15051 we do not treat it as a template-name, since it is not being used
15052 as part of a template-id. This enables us to handle constructs
15053 like:
15055 template <typename T> struct S { S(); };
15056 template <typename T> S<T>::S();
15058 correctly. We would treat `S' as a template -- if it were `S<T>'
15059 -- but we do not if there is no `<'. */
15061 if (processing_template_decl
15062 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15064 /* In a declaration, in a dependent context, we pretend that the
15065 "template" keyword was present in order to improve error
15066 recovery. For example, given:
15068 template <typename T> void f(T::X<int>);
15070 we want to treat "X<int>" as a template-id. */
15071 if (is_declaration
15072 && !template_keyword_p
15073 && parser->scope && TYPE_P (parser->scope)
15074 && check_dependency_p
15075 && dependent_scope_p (parser->scope)
15076 /* Do not do this for dtors (or ctors), since they never
15077 need the template keyword before their name. */
15078 && !constructor_name_p (identifier, parser->scope))
15080 cp_token_position start = 0;
15082 /* Explain what went wrong. */
15083 error_at (token->location, "non-template %qD used as template",
15084 identifier);
15085 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15086 parser->scope, identifier);
15087 /* If parsing tentatively, find the location of the "<" token. */
15088 if (cp_parser_simulate_error (parser))
15089 start = cp_lexer_token_position (parser->lexer, true);
15090 /* Parse the template arguments so that we can issue error
15091 messages about them. */
15092 cp_lexer_consume_token (parser->lexer);
15093 cp_parser_enclosed_template_argument_list (parser);
15094 /* Skip tokens until we find a good place from which to
15095 continue parsing. */
15096 cp_parser_skip_to_closing_parenthesis (parser,
15097 /*recovering=*/true,
15098 /*or_comma=*/true,
15099 /*consume_paren=*/false);
15100 /* If parsing tentatively, permanently remove the
15101 template argument list. That will prevent duplicate
15102 error messages from being issued about the missing
15103 "template" keyword. */
15104 if (start)
15105 cp_lexer_purge_tokens_after (parser->lexer, start);
15106 if (is_identifier)
15107 *is_identifier = true;
15108 return identifier;
15111 /* If the "template" keyword is present, then there is generally
15112 no point in doing name-lookup, so we just return IDENTIFIER.
15113 But, if the qualifying scope is non-dependent then we can
15114 (and must) do name-lookup normally. */
15115 if (template_keyword_p
15116 && (!parser->scope
15117 || (TYPE_P (parser->scope)
15118 && dependent_type_p (parser->scope))))
15119 return identifier;
15122 /* Look up the name. */
15123 decl = cp_parser_lookup_name (parser, identifier,
15124 tag_type,
15125 /*is_template=*/true,
15126 /*is_namespace=*/false,
15127 check_dependency_p,
15128 /*ambiguous_decls=*/NULL,
15129 token->location);
15131 decl = strip_using_decl (decl);
15133 /* If DECL is a template, then the name was a template-name. */
15134 if (TREE_CODE (decl) == TEMPLATE_DECL)
15136 if (TREE_DEPRECATED (decl)
15137 && deprecated_state != DEPRECATED_SUPPRESS)
15138 warn_deprecated_use (decl, NULL_TREE);
15140 else
15142 tree fn = NULL_TREE;
15144 /* The standard does not explicitly indicate whether a name that
15145 names a set of overloaded declarations, some of which are
15146 templates, is a template-name. However, such a name should
15147 be a template-name; otherwise, there is no way to form a
15148 template-id for the overloaded templates. */
15149 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
15150 if (TREE_CODE (fns) == OVERLOAD)
15151 for (fn = fns; fn; fn = OVL_NEXT (fn))
15152 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
15153 break;
15155 if (!fn)
15157 /* The name does not name a template. */
15158 cp_parser_error (parser, "expected template-name");
15159 return error_mark_node;
15163 /* If DECL is dependent, and refers to a function, then just return
15164 its name; we will look it up again during template instantiation. */
15165 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
15167 tree scope = ovl_scope (decl);
15168 if (TYPE_P (scope) && dependent_type_p (scope))
15169 return identifier;
15172 return decl;
15175 /* Parse a template-argument-list.
15177 template-argument-list:
15178 template-argument ... [opt]
15179 template-argument-list , template-argument ... [opt]
15181 Returns a TREE_VEC containing the arguments. */
15183 static tree
15184 cp_parser_template_argument_list (cp_parser* parser)
15186 tree fixed_args[10];
15187 unsigned n_args = 0;
15188 unsigned alloced = 10;
15189 tree *arg_ary = fixed_args;
15190 tree vec;
15191 bool saved_in_template_argument_list_p;
15192 bool saved_ice_p;
15193 bool saved_non_ice_p;
15195 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
15196 parser->in_template_argument_list_p = true;
15197 /* Even if the template-id appears in an integral
15198 constant-expression, the contents of the argument list do
15199 not. */
15200 saved_ice_p = parser->integral_constant_expression_p;
15201 parser->integral_constant_expression_p = false;
15202 saved_non_ice_p = parser->non_integral_constant_expression_p;
15203 parser->non_integral_constant_expression_p = false;
15205 /* Parse the arguments. */
15208 tree argument;
15210 if (n_args)
15211 /* Consume the comma. */
15212 cp_lexer_consume_token (parser->lexer);
15214 /* Parse the template-argument. */
15215 argument = cp_parser_template_argument (parser);
15217 /* If the next token is an ellipsis, we're expanding a template
15218 argument pack. */
15219 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15221 if (argument == error_mark_node)
15223 cp_token *token = cp_lexer_peek_token (parser->lexer);
15224 error_at (token->location,
15225 "expected parameter pack before %<...%>");
15227 /* Consume the `...' token. */
15228 cp_lexer_consume_token (parser->lexer);
15230 /* Make the argument into a TYPE_PACK_EXPANSION or
15231 EXPR_PACK_EXPANSION. */
15232 argument = make_pack_expansion (argument);
15235 if (n_args == alloced)
15237 alloced *= 2;
15239 if (arg_ary == fixed_args)
15241 arg_ary = XNEWVEC (tree, alloced);
15242 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
15244 else
15245 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
15247 arg_ary[n_args++] = argument;
15249 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15251 vec = make_tree_vec (n_args);
15253 while (n_args--)
15254 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
15256 if (arg_ary != fixed_args)
15257 free (arg_ary);
15258 parser->non_integral_constant_expression_p = saved_non_ice_p;
15259 parser->integral_constant_expression_p = saved_ice_p;
15260 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
15261 if (CHECKING_P)
15262 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
15263 return vec;
15266 /* Parse a template-argument.
15268 template-argument:
15269 assignment-expression
15270 type-id
15271 id-expression
15273 The representation is that of an assignment-expression, type-id, or
15274 id-expression -- except that the qualified id-expression is
15275 evaluated, so that the value returned is either a DECL or an
15276 OVERLOAD.
15278 Although the standard says "assignment-expression", it forbids
15279 throw-expressions or assignments in the template argument.
15280 Therefore, we use "conditional-expression" instead. */
15282 static tree
15283 cp_parser_template_argument (cp_parser* parser)
15285 tree argument;
15286 bool template_p;
15287 bool address_p;
15288 bool maybe_type_id = false;
15289 cp_token *token = NULL, *argument_start_token = NULL;
15290 location_t loc = 0;
15291 cp_id_kind idk;
15293 /* There's really no way to know what we're looking at, so we just
15294 try each alternative in order.
15296 [temp.arg]
15298 In a template-argument, an ambiguity between a type-id and an
15299 expression is resolved to a type-id, regardless of the form of
15300 the corresponding template-parameter.
15302 Therefore, we try a type-id first. */
15303 cp_parser_parse_tentatively (parser);
15304 argument = cp_parser_template_type_arg (parser);
15305 /* If there was no error parsing the type-id but the next token is a
15306 '>>', our behavior depends on which dialect of C++ we're
15307 parsing. In C++98, we probably found a typo for '> >'. But there
15308 are type-id which are also valid expressions. For instance:
15310 struct X { int operator >> (int); };
15311 template <int V> struct Foo {};
15312 Foo<X () >> 5> r;
15314 Here 'X()' is a valid type-id of a function type, but the user just
15315 wanted to write the expression "X() >> 5". Thus, we remember that we
15316 found a valid type-id, but we still try to parse the argument as an
15317 expression to see what happens.
15319 In C++0x, the '>>' will be considered two separate '>'
15320 tokens. */
15321 if (!cp_parser_error_occurred (parser)
15322 && cxx_dialect == cxx98
15323 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15325 maybe_type_id = true;
15326 cp_parser_abort_tentative_parse (parser);
15328 else
15330 /* If the next token isn't a `,' or a `>', then this argument wasn't
15331 really finished. This means that the argument is not a valid
15332 type-id. */
15333 if (!cp_parser_next_token_ends_template_argument_p (parser))
15334 cp_parser_error (parser, "expected template-argument");
15335 /* If that worked, we're done. */
15336 if (cp_parser_parse_definitely (parser))
15337 return argument;
15339 /* We're still not sure what the argument will be. */
15340 cp_parser_parse_tentatively (parser);
15341 /* Try a template. */
15342 argument_start_token = cp_lexer_peek_token (parser->lexer);
15343 argument = cp_parser_id_expression (parser,
15344 /*template_keyword_p=*/false,
15345 /*check_dependency_p=*/true,
15346 &template_p,
15347 /*declarator_p=*/false,
15348 /*optional_p=*/false);
15349 /* If the next token isn't a `,' or a `>', then this argument wasn't
15350 really finished. */
15351 if (!cp_parser_next_token_ends_template_argument_p (parser))
15352 cp_parser_error (parser, "expected template-argument");
15353 if (!cp_parser_error_occurred (parser))
15355 /* Figure out what is being referred to. If the id-expression
15356 was for a class template specialization, then we will have a
15357 TYPE_DECL at this point. There is no need to do name lookup
15358 at this point in that case. */
15359 if (TREE_CODE (argument) != TYPE_DECL)
15360 argument = cp_parser_lookup_name (parser, argument,
15361 none_type,
15362 /*is_template=*/template_p,
15363 /*is_namespace=*/false,
15364 /*check_dependency=*/true,
15365 /*ambiguous_decls=*/NULL,
15366 argument_start_token->location);
15367 /* Handle a constrained-type-specifier for a non-type template
15368 parameter. */
15369 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
15370 argument = decl;
15371 else if (TREE_CODE (argument) != TEMPLATE_DECL
15372 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
15373 cp_parser_error (parser, "expected template-name");
15375 if (cp_parser_parse_definitely (parser))
15377 if (TREE_DEPRECATED (argument))
15378 warn_deprecated_use (argument, NULL_TREE);
15379 return argument;
15381 /* It must be a non-type argument. In C++17 any constant-expression is
15382 allowed. */
15383 if (cxx_dialect > cxx14)
15384 goto general_expr;
15386 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
15388 -- an integral constant-expression of integral or enumeration
15389 type; or
15391 -- the name of a non-type template-parameter; or
15393 -- the name of an object or function with external linkage...
15395 -- the address of an object or function with external linkage...
15397 -- a pointer to member... */
15398 /* Look for a non-type template parameter. */
15399 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15401 cp_parser_parse_tentatively (parser);
15402 argument = cp_parser_primary_expression (parser,
15403 /*address_p=*/false,
15404 /*cast_p=*/false,
15405 /*template_arg_p=*/true,
15406 &idk);
15407 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
15408 || !cp_parser_next_token_ends_template_argument_p (parser))
15409 cp_parser_simulate_error (parser);
15410 if (cp_parser_parse_definitely (parser))
15411 return argument;
15414 /* If the next token is "&", the argument must be the address of an
15415 object or function with external linkage. */
15416 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
15417 if (address_p)
15419 loc = cp_lexer_peek_token (parser->lexer)->location;
15420 cp_lexer_consume_token (parser->lexer);
15422 /* See if we might have an id-expression. */
15423 token = cp_lexer_peek_token (parser->lexer);
15424 if (token->type == CPP_NAME
15425 || token->keyword == RID_OPERATOR
15426 || token->type == CPP_SCOPE
15427 || token->type == CPP_TEMPLATE_ID
15428 || token->type == CPP_NESTED_NAME_SPECIFIER)
15430 cp_parser_parse_tentatively (parser);
15431 argument = cp_parser_primary_expression (parser,
15432 address_p,
15433 /*cast_p=*/false,
15434 /*template_arg_p=*/true,
15435 &idk);
15436 if (cp_parser_error_occurred (parser)
15437 || !cp_parser_next_token_ends_template_argument_p (parser))
15438 cp_parser_abort_tentative_parse (parser);
15439 else
15441 tree probe;
15443 if (INDIRECT_REF_P (argument))
15445 /* Strip the dereference temporarily. */
15446 gcc_assert (REFERENCE_REF_P (argument));
15447 argument = TREE_OPERAND (argument, 0);
15450 /* If we're in a template, we represent a qualified-id referring
15451 to a static data member as a SCOPE_REF even if the scope isn't
15452 dependent so that we can check access control later. */
15453 probe = argument;
15454 if (TREE_CODE (probe) == SCOPE_REF)
15455 probe = TREE_OPERAND (probe, 1);
15456 if (VAR_P (probe))
15458 /* A variable without external linkage might still be a
15459 valid constant-expression, so no error is issued here
15460 if the external-linkage check fails. */
15461 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
15462 cp_parser_simulate_error (parser);
15464 else if (is_overloaded_fn (argument))
15465 /* All overloaded functions are allowed; if the external
15466 linkage test does not pass, an error will be issued
15467 later. */
15469 else if (address_p
15470 && (TREE_CODE (argument) == OFFSET_REF
15471 || TREE_CODE (argument) == SCOPE_REF))
15472 /* A pointer-to-member. */
15474 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
15476 else
15477 cp_parser_simulate_error (parser);
15479 if (cp_parser_parse_definitely (parser))
15481 if (address_p)
15482 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
15483 tf_warning_or_error);
15484 else
15485 argument = convert_from_reference (argument);
15486 return argument;
15490 /* If the argument started with "&", there are no other valid
15491 alternatives at this point. */
15492 if (address_p)
15494 cp_parser_error (parser, "invalid non-type template argument");
15495 return error_mark_node;
15498 general_expr:
15499 /* If the argument wasn't successfully parsed as a type-id followed
15500 by '>>', the argument can only be a constant expression now.
15501 Otherwise, we try parsing the constant-expression tentatively,
15502 because the argument could really be a type-id. */
15503 if (maybe_type_id)
15504 cp_parser_parse_tentatively (parser);
15506 if (cxx_dialect <= cxx14)
15507 argument = cp_parser_constant_expression (parser);
15508 else
15510 /* With C++17 generalized non-type template arguments we need to handle
15511 lvalue constant expressions, too. */
15512 argument = cp_parser_assignment_expression (parser);
15513 require_potential_constant_expression (argument);
15516 if (!maybe_type_id)
15517 return argument;
15518 if (!cp_parser_next_token_ends_template_argument_p (parser))
15519 cp_parser_error (parser, "expected template-argument");
15520 if (cp_parser_parse_definitely (parser))
15521 return argument;
15522 /* We did our best to parse the argument as a non type-id, but that
15523 was the only alternative that matched (albeit with a '>' after
15524 it). We can assume it's just a typo from the user, and a
15525 diagnostic will then be issued. */
15526 return cp_parser_template_type_arg (parser);
15529 /* Parse an explicit-instantiation.
15531 explicit-instantiation:
15532 template declaration
15534 Although the standard says `declaration', what it really means is:
15536 explicit-instantiation:
15537 template decl-specifier-seq [opt] declarator [opt] ;
15539 Things like `template int S<int>::i = 5, int S<double>::j;' are not
15540 supposed to be allowed. A defect report has been filed about this
15541 issue.
15543 GNU Extension:
15545 explicit-instantiation:
15546 storage-class-specifier template
15547 decl-specifier-seq [opt] declarator [opt] ;
15548 function-specifier template
15549 decl-specifier-seq [opt] declarator [opt] ; */
15551 static void
15552 cp_parser_explicit_instantiation (cp_parser* parser)
15554 int declares_class_or_enum;
15555 cp_decl_specifier_seq decl_specifiers;
15556 tree extension_specifier = NULL_TREE;
15558 timevar_push (TV_TEMPLATE_INST);
15560 /* Look for an (optional) storage-class-specifier or
15561 function-specifier. */
15562 if (cp_parser_allow_gnu_extensions_p (parser))
15564 extension_specifier
15565 = cp_parser_storage_class_specifier_opt (parser);
15566 if (!extension_specifier)
15567 extension_specifier
15568 = cp_parser_function_specifier_opt (parser,
15569 /*decl_specs=*/NULL);
15572 /* Look for the `template' keyword. */
15573 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15574 /* Let the front end know that we are processing an explicit
15575 instantiation. */
15576 begin_explicit_instantiation ();
15577 /* [temp.explicit] says that we are supposed to ignore access
15578 control while processing explicit instantiation directives. */
15579 push_deferring_access_checks (dk_no_check);
15580 /* Parse a decl-specifier-seq. */
15581 cp_parser_decl_specifier_seq (parser,
15582 CP_PARSER_FLAGS_OPTIONAL,
15583 &decl_specifiers,
15584 &declares_class_or_enum);
15585 /* If there was exactly one decl-specifier, and it declared a class,
15586 and there's no declarator, then we have an explicit type
15587 instantiation. */
15588 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
15590 tree type;
15592 type = check_tag_decl (&decl_specifiers,
15593 /*explicit_type_instantiation_p=*/true);
15594 /* Turn access control back on for names used during
15595 template instantiation. */
15596 pop_deferring_access_checks ();
15597 if (type)
15598 do_type_instantiation (type, extension_specifier,
15599 /*complain=*/tf_error);
15601 else
15603 cp_declarator *declarator;
15604 tree decl;
15606 /* Parse the declarator. */
15607 declarator
15608 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15609 /*ctor_dtor_or_conv_p=*/NULL,
15610 /*parenthesized_p=*/NULL,
15611 /*member_p=*/false,
15612 /*friend_p=*/false);
15613 if (declares_class_or_enum & 2)
15614 cp_parser_check_for_definition_in_return_type (declarator,
15615 decl_specifiers.type,
15616 decl_specifiers.locations[ds_type_spec]);
15617 if (declarator != cp_error_declarator)
15619 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
15620 permerror (decl_specifiers.locations[ds_inline],
15621 "explicit instantiation shall not use"
15622 " %<inline%> specifier");
15623 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
15624 permerror (decl_specifiers.locations[ds_constexpr],
15625 "explicit instantiation shall not use"
15626 " %<constexpr%> specifier");
15628 decl = grokdeclarator (declarator, &decl_specifiers,
15629 NORMAL, 0, &decl_specifiers.attributes);
15630 /* Turn access control back on for names used during
15631 template instantiation. */
15632 pop_deferring_access_checks ();
15633 /* Do the explicit instantiation. */
15634 do_decl_instantiation (decl, extension_specifier);
15636 else
15638 pop_deferring_access_checks ();
15639 /* Skip the body of the explicit instantiation. */
15640 cp_parser_skip_to_end_of_statement (parser);
15643 /* We're done with the instantiation. */
15644 end_explicit_instantiation ();
15646 cp_parser_consume_semicolon_at_end_of_statement (parser);
15648 timevar_pop (TV_TEMPLATE_INST);
15651 /* Parse an explicit-specialization.
15653 explicit-specialization:
15654 template < > declaration
15656 Although the standard says `declaration', what it really means is:
15658 explicit-specialization:
15659 template <> decl-specifier [opt] init-declarator [opt] ;
15660 template <> function-definition
15661 template <> explicit-specialization
15662 template <> template-declaration */
15664 static void
15665 cp_parser_explicit_specialization (cp_parser* parser)
15667 bool need_lang_pop;
15668 cp_token *token = cp_lexer_peek_token (parser->lexer);
15670 /* Look for the `template' keyword. */
15671 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15672 /* Look for the `<'. */
15673 cp_parser_require (parser, CPP_LESS, RT_LESS);
15674 /* Look for the `>'. */
15675 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15676 /* We have processed another parameter list. */
15677 ++parser->num_template_parameter_lists;
15678 /* [temp]
15680 A template ... explicit specialization ... shall not have C
15681 linkage. */
15682 if (current_lang_name == lang_name_c)
15684 error_at (token->location, "template specialization with C linkage");
15685 /* Give it C++ linkage to avoid confusing other parts of the
15686 front end. */
15687 push_lang_context (lang_name_cplusplus);
15688 need_lang_pop = true;
15690 else
15691 need_lang_pop = false;
15692 /* Let the front end know that we are beginning a specialization. */
15693 if (!begin_specialization ())
15695 end_specialization ();
15696 return;
15699 /* If the next keyword is `template', we need to figure out whether
15700 or not we're looking a template-declaration. */
15701 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15703 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15704 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
15705 cp_parser_template_declaration_after_export (parser,
15706 /*member_p=*/false);
15707 else
15708 cp_parser_explicit_specialization (parser);
15710 else
15711 /* Parse the dependent declaration. */
15712 cp_parser_single_declaration (parser,
15713 /*checks=*/NULL,
15714 /*member_p=*/false,
15715 /*explicit_specialization_p=*/true,
15716 /*friend_p=*/NULL);
15717 /* We're done with the specialization. */
15718 end_specialization ();
15719 /* For the erroneous case of a template with C linkage, we pushed an
15720 implicit C++ linkage scope; exit that scope now. */
15721 if (need_lang_pop)
15722 pop_lang_context ();
15723 /* We're done with this parameter list. */
15724 --parser->num_template_parameter_lists;
15727 /* Parse a type-specifier.
15729 type-specifier:
15730 simple-type-specifier
15731 class-specifier
15732 enum-specifier
15733 elaborated-type-specifier
15734 cv-qualifier
15736 GNU Extension:
15738 type-specifier:
15739 __complex__
15741 Returns a representation of the type-specifier. For a
15742 class-specifier, enum-specifier, or elaborated-type-specifier, a
15743 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
15745 The parser flags FLAGS is used to control type-specifier parsing.
15747 If IS_DECLARATION is TRUE, then this type-specifier is appearing
15748 in a decl-specifier-seq.
15750 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
15751 class-specifier, enum-specifier, or elaborated-type-specifier, then
15752 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
15753 if a type is declared; 2 if it is defined. Otherwise, it is set to
15754 zero.
15756 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
15757 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
15758 is set to FALSE. */
15760 static tree
15761 cp_parser_type_specifier (cp_parser* parser,
15762 cp_parser_flags flags,
15763 cp_decl_specifier_seq *decl_specs,
15764 bool is_declaration,
15765 int* declares_class_or_enum,
15766 bool* is_cv_qualifier)
15768 tree type_spec = NULL_TREE;
15769 cp_token *token;
15770 enum rid keyword;
15771 cp_decl_spec ds = ds_last;
15773 /* Assume this type-specifier does not declare a new type. */
15774 if (declares_class_or_enum)
15775 *declares_class_or_enum = 0;
15776 /* And that it does not specify a cv-qualifier. */
15777 if (is_cv_qualifier)
15778 *is_cv_qualifier = false;
15779 /* Peek at the next token. */
15780 token = cp_lexer_peek_token (parser->lexer);
15782 /* If we're looking at a keyword, we can use that to guide the
15783 production we choose. */
15784 keyword = token->keyword;
15785 switch (keyword)
15787 case RID_ENUM:
15788 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15789 goto elaborated_type_specifier;
15791 /* Look for the enum-specifier. */
15792 type_spec = cp_parser_enum_specifier (parser);
15793 /* If that worked, we're done. */
15794 if (type_spec)
15796 if (declares_class_or_enum)
15797 *declares_class_or_enum = 2;
15798 if (decl_specs)
15799 cp_parser_set_decl_spec_type (decl_specs,
15800 type_spec,
15801 token,
15802 /*type_definition_p=*/true);
15803 return type_spec;
15805 else
15806 goto elaborated_type_specifier;
15808 /* Any of these indicate either a class-specifier, or an
15809 elaborated-type-specifier. */
15810 case RID_CLASS:
15811 case RID_STRUCT:
15812 case RID_UNION:
15813 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15814 goto elaborated_type_specifier;
15816 /* Parse tentatively so that we can back up if we don't find a
15817 class-specifier. */
15818 cp_parser_parse_tentatively (parser);
15819 /* Look for the class-specifier. */
15820 type_spec = cp_parser_class_specifier (parser);
15821 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
15822 /* If that worked, we're done. */
15823 if (cp_parser_parse_definitely (parser))
15825 if (declares_class_or_enum)
15826 *declares_class_or_enum = 2;
15827 if (decl_specs)
15828 cp_parser_set_decl_spec_type (decl_specs,
15829 type_spec,
15830 token,
15831 /*type_definition_p=*/true);
15832 return type_spec;
15835 /* Fall through. */
15836 elaborated_type_specifier:
15837 /* We're declaring (not defining) a class or enum. */
15838 if (declares_class_or_enum)
15839 *declares_class_or_enum = 1;
15841 /* Fall through. */
15842 case RID_TYPENAME:
15843 /* Look for an elaborated-type-specifier. */
15844 type_spec
15845 = (cp_parser_elaborated_type_specifier
15846 (parser,
15847 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
15848 is_declaration));
15849 if (decl_specs)
15850 cp_parser_set_decl_spec_type (decl_specs,
15851 type_spec,
15852 token,
15853 /*type_definition_p=*/false);
15854 return type_spec;
15856 case RID_CONST:
15857 ds = ds_const;
15858 if (is_cv_qualifier)
15859 *is_cv_qualifier = true;
15860 break;
15862 case RID_VOLATILE:
15863 ds = ds_volatile;
15864 if (is_cv_qualifier)
15865 *is_cv_qualifier = true;
15866 break;
15868 case RID_RESTRICT:
15869 ds = ds_restrict;
15870 if (is_cv_qualifier)
15871 *is_cv_qualifier = true;
15872 break;
15874 case RID_COMPLEX:
15875 /* The `__complex__' keyword is a GNU extension. */
15876 ds = ds_complex;
15877 break;
15879 default:
15880 break;
15883 /* Handle simple keywords. */
15884 if (ds != ds_last)
15886 if (decl_specs)
15888 set_and_check_decl_spec_loc (decl_specs, ds, token);
15889 decl_specs->any_specifiers_p = true;
15891 return cp_lexer_consume_token (parser->lexer)->u.value;
15894 /* If we do not already have a type-specifier, assume we are looking
15895 at a simple-type-specifier. */
15896 type_spec = cp_parser_simple_type_specifier (parser,
15897 decl_specs,
15898 flags);
15900 /* If we didn't find a type-specifier, and a type-specifier was not
15901 optional in this context, issue an error message. */
15902 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15904 cp_parser_error (parser, "expected type specifier");
15905 return error_mark_node;
15908 return type_spec;
15911 /* Parse a simple-type-specifier.
15913 simple-type-specifier:
15914 :: [opt] nested-name-specifier [opt] type-name
15915 :: [opt] nested-name-specifier template template-id
15916 char
15917 wchar_t
15918 bool
15919 short
15921 long
15922 signed
15923 unsigned
15924 float
15925 double
15926 void
15928 C++0x Extension:
15930 simple-type-specifier:
15931 auto
15932 decltype ( expression )
15933 char16_t
15934 char32_t
15935 __underlying_type ( type-id )
15937 GNU Extension:
15939 simple-type-specifier:
15940 __int128
15941 __typeof__ unary-expression
15942 __typeof__ ( type-id )
15943 __typeof__ ( type-id ) { initializer-list , [opt] }
15945 Concepts Extension:
15947 simple-type-specifier:
15948 constrained-type-specifier
15950 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
15951 appropriately updated. */
15953 static tree
15954 cp_parser_simple_type_specifier (cp_parser* parser,
15955 cp_decl_specifier_seq *decl_specs,
15956 cp_parser_flags flags)
15958 tree type = NULL_TREE;
15959 cp_token *token;
15960 int idx;
15962 /* Peek at the next token. */
15963 token = cp_lexer_peek_token (parser->lexer);
15965 /* If we're looking at a keyword, things are easy. */
15966 switch (token->keyword)
15968 case RID_CHAR:
15969 if (decl_specs)
15970 decl_specs->explicit_char_p = true;
15971 type = char_type_node;
15972 break;
15973 case RID_CHAR16:
15974 type = char16_type_node;
15975 break;
15976 case RID_CHAR32:
15977 type = char32_type_node;
15978 break;
15979 case RID_WCHAR:
15980 type = wchar_type_node;
15981 break;
15982 case RID_BOOL:
15983 type = boolean_type_node;
15984 break;
15985 case RID_SHORT:
15986 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
15987 type = short_integer_type_node;
15988 break;
15989 case RID_INT:
15990 if (decl_specs)
15991 decl_specs->explicit_int_p = true;
15992 type = integer_type_node;
15993 break;
15994 case RID_INT_N_0:
15995 case RID_INT_N_1:
15996 case RID_INT_N_2:
15997 case RID_INT_N_3:
15998 idx = token->keyword - RID_INT_N_0;
15999 if (! int_n_enabled_p [idx])
16000 break;
16001 if (decl_specs)
16003 decl_specs->explicit_intN_p = true;
16004 decl_specs->int_n_idx = idx;
16006 type = int_n_trees [idx].signed_type;
16007 break;
16008 case RID_LONG:
16009 if (decl_specs)
16010 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16011 type = long_integer_type_node;
16012 break;
16013 case RID_SIGNED:
16014 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16015 type = integer_type_node;
16016 break;
16017 case RID_UNSIGNED:
16018 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16019 type = unsigned_type_node;
16020 break;
16021 case RID_FLOAT:
16022 type = float_type_node;
16023 break;
16024 case RID_DOUBLE:
16025 type = double_type_node;
16026 break;
16027 case RID_VOID:
16028 type = void_type_node;
16029 break;
16031 case RID_AUTO:
16032 maybe_warn_cpp0x (CPP0X_AUTO);
16033 if (parser->auto_is_implicit_function_template_parm_p)
16035 /* The 'auto' might be the placeholder return type for a function decl
16036 with trailing return type. */
16037 bool have_trailing_return_fn_decl = false;
16039 cp_parser_parse_tentatively (parser);
16040 cp_lexer_consume_token (parser->lexer);
16041 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16042 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16043 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16044 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16046 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16048 cp_lexer_consume_token (parser->lexer);
16049 cp_parser_skip_to_closing_parenthesis (parser,
16050 /*recovering*/false,
16051 /*or_comma*/false,
16052 /*consume_paren*/true);
16053 continue;
16056 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16058 have_trailing_return_fn_decl = true;
16059 break;
16062 cp_lexer_consume_token (parser->lexer);
16064 cp_parser_abort_tentative_parse (parser);
16066 if (have_trailing_return_fn_decl)
16068 type = make_auto ();
16069 break;
16072 if (cxx_dialect >= cxx14)
16074 type = synthesize_implicit_template_parm (parser, NULL_TREE);
16075 type = TREE_TYPE (type);
16077 else
16078 type = error_mark_node;
16080 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16082 if (cxx_dialect < cxx14)
16083 error_at (token->location,
16084 "use of %<auto%> in lambda parameter declaration "
16085 "only available with "
16086 "-std=c++14 or -std=gnu++14");
16088 else if (cxx_dialect < cxx14)
16089 error_at (token->location,
16090 "use of %<auto%> in parameter declaration "
16091 "only available with "
16092 "-std=c++14 or -std=gnu++14");
16093 else if (!flag_concepts)
16094 pedwarn (token->location, OPT_Wpedantic,
16095 "ISO C++ forbids use of %<auto%> in parameter "
16096 "declaration");
16098 else
16099 type = make_auto ();
16100 break;
16102 case RID_DECLTYPE:
16103 /* Since DR 743, decltype can either be a simple-type-specifier by
16104 itself or begin a nested-name-specifier. Parsing it will replace
16105 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16106 handling below decide what to do. */
16107 cp_parser_decltype (parser);
16108 cp_lexer_set_token_position (parser->lexer, token);
16109 break;
16111 case RID_TYPEOF:
16112 /* Consume the `typeof' token. */
16113 cp_lexer_consume_token (parser->lexer);
16114 /* Parse the operand to `typeof'. */
16115 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
16116 /* If it is not already a TYPE, take its type. */
16117 if (!TYPE_P (type))
16118 type = finish_typeof (type);
16120 if (decl_specs)
16121 cp_parser_set_decl_spec_type (decl_specs, type,
16122 token,
16123 /*type_definition_p=*/false);
16125 return type;
16127 case RID_UNDERLYING_TYPE:
16128 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
16129 if (decl_specs)
16130 cp_parser_set_decl_spec_type (decl_specs, type,
16131 token,
16132 /*type_definition_p=*/false);
16134 return type;
16136 case RID_BASES:
16137 case RID_DIRECT_BASES:
16138 type = cp_parser_trait_expr (parser, token->keyword);
16139 if (decl_specs)
16140 cp_parser_set_decl_spec_type (decl_specs, type,
16141 token,
16142 /*type_definition_p=*/false);
16143 return type;
16144 default:
16145 break;
16148 /* If token is an already-parsed decltype not followed by ::,
16149 it's a simple-type-specifier. */
16150 if (token->type == CPP_DECLTYPE
16151 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
16153 type = saved_checks_value (token->u.tree_check_value);
16154 if (decl_specs)
16156 cp_parser_set_decl_spec_type (decl_specs, type,
16157 token,
16158 /*type_definition_p=*/false);
16159 /* Remember that we are handling a decltype in order to
16160 implement the resolution of DR 1510 when the argument
16161 isn't instantiation dependent. */
16162 decl_specs->decltype_p = true;
16164 cp_lexer_consume_token (parser->lexer);
16165 return type;
16168 /* If the type-specifier was for a built-in type, we're done. */
16169 if (type)
16171 /* Record the type. */
16172 if (decl_specs
16173 && (token->keyword != RID_SIGNED
16174 && token->keyword != RID_UNSIGNED
16175 && token->keyword != RID_SHORT
16176 && token->keyword != RID_LONG))
16177 cp_parser_set_decl_spec_type (decl_specs,
16178 type,
16179 token,
16180 /*type_definition_p=*/false);
16181 if (decl_specs)
16182 decl_specs->any_specifiers_p = true;
16184 /* Consume the token. */
16185 cp_lexer_consume_token (parser->lexer);
16187 if (type == error_mark_node)
16188 return error_mark_node;
16190 /* There is no valid C++ program where a non-template type is
16191 followed by a "<". That usually indicates that the user thought
16192 that the type was a template. */
16193 cp_parser_check_for_invalid_template_id (parser, type, none_type,
16194 token->location);
16196 return TYPE_NAME (type);
16199 /* The type-specifier must be a user-defined type. */
16200 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
16202 bool qualified_p;
16203 bool global_p;
16205 /* Don't gobble tokens or issue error messages if this is an
16206 optional type-specifier. */
16207 if (flags & CP_PARSER_FLAGS_OPTIONAL)
16208 cp_parser_parse_tentatively (parser);
16210 /* Look for the optional `::' operator. */
16211 global_p
16212 = (cp_parser_global_scope_opt (parser,
16213 /*current_scope_valid_p=*/false)
16214 != NULL_TREE);
16215 /* Look for the nested-name specifier. */
16216 qualified_p
16217 = (cp_parser_nested_name_specifier_opt (parser,
16218 /*typename_keyword_p=*/false,
16219 /*check_dependency_p=*/true,
16220 /*type_p=*/false,
16221 /*is_declaration=*/false)
16222 != NULL_TREE);
16223 token = cp_lexer_peek_token (parser->lexer);
16224 /* If we have seen a nested-name-specifier, and the next token
16225 is `template', then we are using the template-id production. */
16226 if (parser->scope
16227 && cp_parser_optional_template_keyword (parser))
16229 /* Look for the template-id. */
16230 type = cp_parser_template_id (parser,
16231 /*template_keyword_p=*/true,
16232 /*check_dependency_p=*/true,
16233 none_type,
16234 /*is_declaration=*/false);
16235 /* If the template-id did not name a type, we are out of
16236 luck. */
16237 if (TREE_CODE (type) != TYPE_DECL)
16239 cp_parser_error (parser, "expected template-id for type");
16240 type = NULL_TREE;
16243 /* Otherwise, look for a type-name. */
16244 else
16245 type = cp_parser_type_name (parser);
16246 /* Keep track of all name-lookups performed in class scopes. */
16247 if (type
16248 && !global_p
16249 && !qualified_p
16250 && TREE_CODE (type) == TYPE_DECL
16251 && identifier_p (DECL_NAME (type)))
16252 maybe_note_name_used_in_class (DECL_NAME (type), type);
16253 /* If it didn't work out, we don't have a TYPE. */
16254 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
16255 && !cp_parser_parse_definitely (parser))
16256 type = NULL_TREE;
16257 if (type && decl_specs)
16258 cp_parser_set_decl_spec_type (decl_specs, type,
16259 token,
16260 /*type_definition_p=*/false);
16263 /* If we didn't get a type-name, issue an error message. */
16264 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16266 cp_parser_error (parser, "expected type-name");
16267 return error_mark_node;
16270 if (type && type != error_mark_node)
16272 /* See if TYPE is an Objective-C type, and if so, parse and
16273 accept any protocol references following it. Do this before
16274 the cp_parser_check_for_invalid_template_id() call, because
16275 Objective-C types can be followed by '<...>' which would
16276 enclose protocol names rather than template arguments, and so
16277 everything is fine. */
16278 if (c_dialect_objc () && !parser->scope
16279 && (objc_is_id (type) || objc_is_class_name (type)))
16281 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16282 tree qual_type = objc_get_protocol_qualified_type (type, protos);
16284 /* Clobber the "unqualified" type previously entered into
16285 DECL_SPECS with the new, improved protocol-qualified version. */
16286 if (decl_specs)
16287 decl_specs->type = qual_type;
16289 return qual_type;
16292 /* There is no valid C++ program where a non-template type is
16293 followed by a "<". That usually indicates that the user
16294 thought that the type was a template. */
16295 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
16296 none_type,
16297 token->location);
16300 return type;
16303 /* Parse a type-name.
16305 type-name:
16306 class-name
16307 enum-name
16308 typedef-name
16309 simple-template-id [in c++0x]
16311 enum-name:
16312 identifier
16314 typedef-name:
16315 identifier
16317 Concepts:
16319 type-name:
16320 concept-name
16321 partial-concept-id
16323 concept-name:
16324 identifier
16326 Returns a TYPE_DECL for the type. */
16328 static tree
16329 cp_parser_type_name (cp_parser* parser)
16331 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
16334 /* See above. */
16335 static tree
16336 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
16338 tree type_decl;
16340 /* We can't know yet whether it is a class-name or not. */
16341 cp_parser_parse_tentatively (parser);
16342 /* Try a class-name. */
16343 type_decl = cp_parser_class_name (parser,
16344 typename_keyword_p,
16345 /*template_keyword_p=*/false,
16346 none_type,
16347 /*check_dependency_p=*/true,
16348 /*class_head_p=*/false,
16349 /*is_declaration=*/false);
16350 /* If it's not a class-name, keep looking. */
16351 if (!cp_parser_parse_definitely (parser))
16353 if (cxx_dialect < cxx11)
16354 /* It must be a typedef-name or an enum-name. */
16355 return cp_parser_nonclass_name (parser);
16357 cp_parser_parse_tentatively (parser);
16358 /* It is either a simple-template-id representing an
16359 instantiation of an alias template... */
16360 type_decl = cp_parser_template_id (parser,
16361 /*template_keyword_p=*/false,
16362 /*check_dependency_p=*/true,
16363 none_type,
16364 /*is_declaration=*/false);
16365 /* Note that this must be an instantiation of an alias template
16366 because [temp.names]/6 says:
16368 A template-id that names an alias template specialization
16369 is a type-name.
16371 Whereas [temp.names]/7 says:
16373 A simple-template-id that names a class template
16374 specialization is a class-name.
16376 With concepts, this could also be a partial-concept-id that
16377 declares a non-type template parameter. */
16378 if (type_decl != NULL_TREE
16379 && TREE_CODE (type_decl) == TYPE_DECL
16380 && TYPE_DECL_ALIAS_P (type_decl))
16381 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
16382 else if (is_constrained_parameter (type_decl))
16383 /* Don't do anything. */ ;
16384 else
16385 cp_parser_simulate_error (parser);
16387 if (!cp_parser_parse_definitely (parser))
16388 /* ... Or a typedef-name or an enum-name. */
16389 return cp_parser_nonclass_name (parser);
16392 return type_decl;
16395 /* Check if DECL and ARGS can form a constrained-type-specifier.
16396 If ARGS is non-null, we try to form a concept check of the
16397 form DECL<?, ARGS> where ? is a wildcard that matches any
16398 kind of template argument. If ARGS is NULL, then we try to
16399 form a concept check of the form DECL<?>. */
16401 static tree
16402 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
16403 tree decl, tree args)
16405 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
16407 /* If we a constrained-type-specifier cannot be deduced. */
16408 if (parser->prevent_constrained_type_specifiers)
16409 return NULL_TREE;
16411 /* A constrained type specifier can only be found in an
16412 overload set or as a reference to a template declaration.
16414 FIXME: This might be masking a bug. It's possible that
16415 that the deduction below is causing template specializations
16416 to be formed with the wildcard as an argument. */
16417 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
16418 return NULL_TREE;
16420 /* Try to build a call expression that evaluates the
16421 concept. This can fail if the overload set refers
16422 only to non-templates. */
16423 tree placeholder = build_nt (WILDCARD_DECL);
16424 tree check = build_concept_check (decl, placeholder, args);
16425 if (check == error_mark_node)
16426 return NULL_TREE;
16428 /* Deduce the checked constraint and the prototype parameter.
16430 FIXME: In certain cases, failure to deduce should be a
16431 diagnosable error. */
16432 tree conc;
16433 tree proto;
16434 if (!deduce_constrained_parameter (check, conc, proto))
16435 return NULL_TREE;
16437 /* In template parameter scope, this results in a constrained
16438 parameter. Return a descriptor of that parm. */
16439 if (processing_template_parmlist)
16440 return build_constrained_parameter (conc, proto, args);
16442 /* In a parameter-declaration-clause, constrained-type
16443 specifiers result in invented template parameters. */
16444 if (parser->auto_is_implicit_function_template_parm_p)
16446 tree x = build_constrained_parameter (conc, proto, args);
16447 return synthesize_implicit_template_parm (parser, x);
16449 else
16451 /* Otherwise, we're in a context where the constrained
16452 type name is deduced and the constraint applies
16453 after deduction. */
16454 return make_constrained_auto (conc, args);
16457 return NULL_TREE;
16460 /* If DECL refers to a concept, return a TYPE_DECL representing
16461 the result of using the constrained type specifier in the
16462 current context. DECL refers to a concept if
16464 - it is an overload set containing a function concept taking a single
16465 type argument, or
16467 - it is a variable concept taking a single type argument. */
16469 static tree
16470 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
16472 if (flag_concepts
16473 && (TREE_CODE (decl) == OVERLOAD
16474 || BASELINK_P (decl)
16475 || variable_concept_p (decl)))
16476 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
16477 else
16478 return NULL_TREE;
16481 /* Check if DECL and ARGS form a partial-concept-id. If so,
16482 assign ID to the resulting constrained placeholder.
16484 Returns true if the partial-concept-id designates a placeholder
16485 and false otherwise. Note that *id is set to NULL_TREE in
16486 this case. */
16488 static tree
16489 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
16491 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
16494 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
16495 or a concept-name.
16497 enum-name:
16498 identifier
16500 typedef-name:
16501 identifier
16503 concept-name:
16504 identifier
16506 Returns a TYPE_DECL for the type. */
16508 static tree
16509 cp_parser_nonclass_name (cp_parser* parser)
16511 tree type_decl;
16512 tree identifier;
16514 cp_token *token = cp_lexer_peek_token (parser->lexer);
16515 identifier = cp_parser_identifier (parser);
16516 if (identifier == error_mark_node)
16517 return error_mark_node;
16519 /* Look up the type-name. */
16520 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
16522 type_decl = strip_using_decl (type_decl);
16524 /* If we found an overload set, then it may refer to a concept-name. */
16525 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
16526 type_decl = decl;
16528 if (TREE_CODE (type_decl) != TYPE_DECL
16529 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
16531 /* See if this is an Objective-C type. */
16532 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16533 tree type = objc_get_protocol_qualified_type (identifier, protos);
16534 if (type)
16535 type_decl = TYPE_NAME (type);
16538 /* Issue an error if we did not find a type-name. */
16539 if (TREE_CODE (type_decl) != TYPE_DECL
16540 /* In Objective-C, we have the complication that class names are
16541 normally type names and start declarations (eg, the
16542 "NSObject" in "NSObject *object;"), but can be used in an
16543 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
16544 is an expression. So, a classname followed by a dot is not a
16545 valid type-name. */
16546 || (objc_is_class_name (TREE_TYPE (type_decl))
16547 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
16549 if (!cp_parser_simulate_error (parser))
16550 cp_parser_name_lookup_error (parser, identifier, type_decl,
16551 NLE_TYPE, token->location);
16552 return error_mark_node;
16554 /* Remember that the name was used in the definition of the
16555 current class so that we can check later to see if the
16556 meaning would have been different after the class was
16557 entirely defined. */
16558 else if (type_decl != error_mark_node
16559 && !parser->scope)
16560 maybe_note_name_used_in_class (identifier, type_decl);
16562 return type_decl;
16565 /* Parse an elaborated-type-specifier. Note that the grammar given
16566 here incorporates the resolution to DR68.
16568 elaborated-type-specifier:
16569 class-key :: [opt] nested-name-specifier [opt] identifier
16570 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
16571 enum-key :: [opt] nested-name-specifier [opt] identifier
16572 typename :: [opt] nested-name-specifier identifier
16573 typename :: [opt] nested-name-specifier template [opt]
16574 template-id
16576 GNU extension:
16578 elaborated-type-specifier:
16579 class-key attributes :: [opt] nested-name-specifier [opt] identifier
16580 class-key attributes :: [opt] nested-name-specifier [opt]
16581 template [opt] template-id
16582 enum attributes :: [opt] nested-name-specifier [opt] identifier
16584 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
16585 declared `friend'. If IS_DECLARATION is TRUE, then this
16586 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
16587 something is being declared.
16589 Returns the TYPE specified. */
16591 static tree
16592 cp_parser_elaborated_type_specifier (cp_parser* parser,
16593 bool is_friend,
16594 bool is_declaration)
16596 enum tag_types tag_type;
16597 tree identifier;
16598 tree type = NULL_TREE;
16599 tree attributes = NULL_TREE;
16600 tree globalscope;
16601 cp_token *token = NULL;
16603 /* See if we're looking at the `enum' keyword. */
16604 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
16606 /* Consume the `enum' token. */
16607 cp_lexer_consume_token (parser->lexer);
16608 /* Remember that it's an enumeration type. */
16609 tag_type = enum_type;
16610 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
16611 enums) is used here. */
16612 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
16613 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
16615 pedwarn (input_location, 0, "elaborated-type-specifier "
16616 "for a scoped enum must not use the %<%D%> keyword",
16617 cp_lexer_peek_token (parser->lexer)->u.value);
16618 /* Consume the `struct' or `class' and parse it anyway. */
16619 cp_lexer_consume_token (parser->lexer);
16621 /* Parse the attributes. */
16622 attributes = cp_parser_attributes_opt (parser);
16624 /* Or, it might be `typename'. */
16625 else if (cp_lexer_next_token_is_keyword (parser->lexer,
16626 RID_TYPENAME))
16628 /* Consume the `typename' token. */
16629 cp_lexer_consume_token (parser->lexer);
16630 /* Remember that it's a `typename' type. */
16631 tag_type = typename_type;
16633 /* Otherwise it must be a class-key. */
16634 else
16636 tag_type = cp_parser_class_key (parser);
16637 if (tag_type == none_type)
16638 return error_mark_node;
16639 /* Parse the attributes. */
16640 attributes = cp_parser_attributes_opt (parser);
16643 /* Look for the `::' operator. */
16644 globalscope = cp_parser_global_scope_opt (parser,
16645 /*current_scope_valid_p=*/false);
16646 /* Look for the nested-name-specifier. */
16647 if (tag_type == typename_type && !globalscope)
16649 if (!cp_parser_nested_name_specifier (parser,
16650 /*typename_keyword_p=*/true,
16651 /*check_dependency_p=*/true,
16652 /*type_p=*/true,
16653 is_declaration))
16654 return error_mark_node;
16656 else
16657 /* Even though `typename' is not present, the proposed resolution
16658 to Core Issue 180 says that in `class A<T>::B', `B' should be
16659 considered a type-name, even if `A<T>' is dependent. */
16660 cp_parser_nested_name_specifier_opt (parser,
16661 /*typename_keyword_p=*/true,
16662 /*check_dependency_p=*/true,
16663 /*type_p=*/true,
16664 is_declaration);
16665 /* For everything but enumeration types, consider a template-id.
16666 For an enumeration type, consider only a plain identifier. */
16667 if (tag_type != enum_type)
16669 bool template_p = false;
16670 tree decl;
16672 /* Allow the `template' keyword. */
16673 template_p = cp_parser_optional_template_keyword (parser);
16674 /* If we didn't see `template', we don't know if there's a
16675 template-id or not. */
16676 if (!template_p)
16677 cp_parser_parse_tentatively (parser);
16678 /* Parse the template-id. */
16679 token = cp_lexer_peek_token (parser->lexer);
16680 decl = cp_parser_template_id (parser, template_p,
16681 /*check_dependency_p=*/true,
16682 tag_type,
16683 is_declaration);
16684 /* If we didn't find a template-id, look for an ordinary
16685 identifier. */
16686 if (!template_p && !cp_parser_parse_definitely (parser))
16688 /* We can get here when cp_parser_template_id, called by
16689 cp_parser_class_name with tag_type == none_type, succeeds
16690 and caches a BASELINK. Then, when called again here,
16691 instead of failing and returning an error_mark_node
16692 returns it (see template/typename17.C in C++11).
16693 ??? Could we diagnose this earlier? */
16694 else if (tag_type == typename_type && BASELINK_P (decl))
16696 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
16697 type = error_mark_node;
16699 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
16700 in effect, then we must assume that, upon instantiation, the
16701 template will correspond to a class. */
16702 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16703 && tag_type == typename_type)
16704 type = make_typename_type (parser->scope, decl,
16705 typename_type,
16706 /*complain=*/tf_error);
16707 /* If the `typename' keyword is in effect and DECL is not a type
16708 decl, then type is non existent. */
16709 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
16711 else if (TREE_CODE (decl) == TYPE_DECL)
16712 type = check_elaborated_type_specifier (tag_type, decl,
16713 /*allow_template_p=*/true);
16714 else if (decl == error_mark_node)
16715 type = error_mark_node;
16718 if (!type)
16720 token = cp_lexer_peek_token (parser->lexer);
16721 identifier = cp_parser_identifier (parser);
16723 if (identifier == error_mark_node)
16725 parser->scope = NULL_TREE;
16726 return error_mark_node;
16729 /* For a `typename', we needn't call xref_tag. */
16730 if (tag_type == typename_type
16731 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
16732 return cp_parser_make_typename_type (parser, identifier,
16733 token->location);
16735 /* Template parameter lists apply only if we are not within a
16736 function parameter list. */
16737 bool template_parm_lists_apply
16738 = parser->num_template_parameter_lists;
16739 if (template_parm_lists_apply)
16740 for (cp_binding_level *s = current_binding_level;
16741 s && s->kind != sk_template_parms;
16742 s = s->level_chain)
16743 if (s->kind == sk_function_parms)
16744 template_parm_lists_apply = false;
16746 /* Look up a qualified name in the usual way. */
16747 if (parser->scope)
16749 tree decl;
16750 tree ambiguous_decls;
16752 decl = cp_parser_lookup_name (parser, identifier,
16753 tag_type,
16754 /*is_template=*/false,
16755 /*is_namespace=*/false,
16756 /*check_dependency=*/true,
16757 &ambiguous_decls,
16758 token->location);
16760 /* If the lookup was ambiguous, an error will already have been
16761 issued. */
16762 if (ambiguous_decls)
16763 return error_mark_node;
16765 /* If we are parsing friend declaration, DECL may be a
16766 TEMPLATE_DECL tree node here. However, we need to check
16767 whether this TEMPLATE_DECL results in valid code. Consider
16768 the following example:
16770 namespace N {
16771 template <class T> class C {};
16773 class X {
16774 template <class T> friend class N::C; // #1, valid code
16776 template <class T> class Y {
16777 friend class N::C; // #2, invalid code
16780 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
16781 name lookup of `N::C'. We see that friend declaration must
16782 be template for the code to be valid. Note that
16783 processing_template_decl does not work here since it is
16784 always 1 for the above two cases. */
16786 decl = (cp_parser_maybe_treat_template_as_class
16787 (decl, /*tag_name_p=*/is_friend
16788 && template_parm_lists_apply));
16790 if (TREE_CODE (decl) != TYPE_DECL)
16792 cp_parser_diagnose_invalid_type_name (parser,
16793 identifier,
16794 token->location);
16795 return error_mark_node;
16798 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
16800 bool allow_template = (template_parm_lists_apply
16801 || DECL_SELF_REFERENCE_P (decl));
16802 type = check_elaborated_type_specifier (tag_type, decl,
16803 allow_template);
16805 if (type == error_mark_node)
16806 return error_mark_node;
16809 /* Forward declarations of nested types, such as
16811 class C1::C2;
16812 class C1::C2::C3;
16814 are invalid unless all components preceding the final '::'
16815 are complete. If all enclosing types are complete, these
16816 declarations become merely pointless.
16818 Invalid forward declarations of nested types are errors
16819 caught elsewhere in parsing. Those that are pointless arrive
16820 here. */
16822 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16823 && !is_friend && !processing_explicit_instantiation)
16824 warning (0, "declaration %qD does not declare anything", decl);
16826 type = TREE_TYPE (decl);
16828 else
16830 /* An elaborated-type-specifier sometimes introduces a new type and
16831 sometimes names an existing type. Normally, the rule is that it
16832 introduces a new type only if there is not an existing type of
16833 the same name already in scope. For example, given:
16835 struct S {};
16836 void f() { struct S s; }
16838 the `struct S' in the body of `f' is the same `struct S' as in
16839 the global scope; the existing definition is used. However, if
16840 there were no global declaration, this would introduce a new
16841 local class named `S'.
16843 An exception to this rule applies to the following code:
16845 namespace N { struct S; }
16847 Here, the elaborated-type-specifier names a new type
16848 unconditionally; even if there is already an `S' in the
16849 containing scope this declaration names a new type.
16850 This exception only applies if the elaborated-type-specifier
16851 forms the complete declaration:
16853 [class.name]
16855 A declaration consisting solely of `class-key identifier ;' is
16856 either a redeclaration of the name in the current scope or a
16857 forward declaration of the identifier as a class name. It
16858 introduces the name into the current scope.
16860 We are in this situation precisely when the next token is a `;'.
16862 An exception to the exception is that a `friend' declaration does
16863 *not* name a new type; i.e., given:
16865 struct S { friend struct T; };
16867 `T' is not a new type in the scope of `S'.
16869 Also, `new struct S' or `sizeof (struct S)' never results in the
16870 definition of a new type; a new type can only be declared in a
16871 declaration context. */
16873 tag_scope ts;
16874 bool template_p;
16876 if (is_friend)
16877 /* Friends have special name lookup rules. */
16878 ts = ts_within_enclosing_non_class;
16879 else if (is_declaration
16880 && cp_lexer_next_token_is (parser->lexer,
16881 CPP_SEMICOLON))
16882 /* This is a `class-key identifier ;' */
16883 ts = ts_current;
16884 else
16885 ts = ts_global;
16887 template_p =
16888 (template_parm_lists_apply
16889 && (cp_parser_next_token_starts_class_definition_p (parser)
16890 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
16891 /* An unqualified name was used to reference this type, so
16892 there were no qualifying templates. */
16893 if (template_parm_lists_apply
16894 && !cp_parser_check_template_parameters (parser,
16895 /*num_templates=*/0,
16896 token->location,
16897 /*declarator=*/NULL))
16898 return error_mark_node;
16899 type = xref_tag (tag_type, identifier, ts, template_p);
16903 if (type == error_mark_node)
16904 return error_mark_node;
16906 /* Allow attributes on forward declarations of classes. */
16907 if (attributes)
16909 if (TREE_CODE (type) == TYPENAME_TYPE)
16910 warning (OPT_Wattributes,
16911 "attributes ignored on uninstantiated type");
16912 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
16913 && ! processing_explicit_instantiation)
16914 warning (OPT_Wattributes,
16915 "attributes ignored on template instantiation");
16916 else if (is_declaration && cp_parser_declares_only_class_p (parser))
16917 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
16918 else
16919 warning (OPT_Wattributes,
16920 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
16923 if (tag_type != enum_type)
16925 /* Indicate whether this class was declared as a `class' or as a
16926 `struct'. */
16927 if (CLASS_TYPE_P (type))
16928 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
16929 cp_parser_check_class_key (tag_type, type);
16932 /* A "<" cannot follow an elaborated type specifier. If that
16933 happens, the user was probably trying to form a template-id. */
16934 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
16935 token->location);
16937 return type;
16940 /* Parse an enum-specifier.
16942 enum-specifier:
16943 enum-head { enumerator-list [opt] }
16944 enum-head { enumerator-list , } [C++0x]
16946 enum-head:
16947 enum-key identifier [opt] enum-base [opt]
16948 enum-key nested-name-specifier identifier enum-base [opt]
16950 enum-key:
16951 enum
16952 enum class [C++0x]
16953 enum struct [C++0x]
16955 enum-base: [C++0x]
16956 : type-specifier-seq
16958 opaque-enum-specifier:
16959 enum-key identifier enum-base [opt] ;
16961 GNU Extensions:
16962 enum-key attributes[opt] identifier [opt] enum-base [opt]
16963 { enumerator-list [opt] }attributes[opt]
16964 enum-key attributes[opt] identifier [opt] enum-base [opt]
16965 { enumerator-list, }attributes[opt] [C++0x]
16967 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
16968 if the token stream isn't an enum-specifier after all. */
16970 static tree
16971 cp_parser_enum_specifier (cp_parser* parser)
16973 tree identifier;
16974 tree type = NULL_TREE;
16975 tree prev_scope;
16976 tree nested_name_specifier = NULL_TREE;
16977 tree attributes;
16978 bool scoped_enum_p = false;
16979 bool has_underlying_type = false;
16980 bool nested_being_defined = false;
16981 bool new_value_list = false;
16982 bool is_new_type = false;
16983 bool is_anonymous = false;
16984 tree underlying_type = NULL_TREE;
16985 cp_token *type_start_token = NULL;
16986 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
16988 parser->colon_corrects_to_scope_p = false;
16990 /* Parse tentatively so that we can back up if we don't find a
16991 enum-specifier. */
16992 cp_parser_parse_tentatively (parser);
16994 /* Caller guarantees that the current token is 'enum', an identifier
16995 possibly follows, and the token after that is an opening brace.
16996 If we don't have an identifier, fabricate an anonymous name for
16997 the enumeration being defined. */
16998 cp_lexer_consume_token (parser->lexer);
17000 /* Parse the "class" or "struct", which indicates a scoped
17001 enumeration type in C++0x. */
17002 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17003 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17005 if (cxx_dialect < cxx11)
17006 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17008 /* Consume the `struct' or `class' token. */
17009 cp_lexer_consume_token (parser->lexer);
17011 scoped_enum_p = true;
17014 attributes = cp_parser_attributes_opt (parser);
17016 /* Clear the qualification. */
17017 parser->scope = NULL_TREE;
17018 parser->qualifying_scope = NULL_TREE;
17019 parser->object_scope = NULL_TREE;
17021 /* Figure out in what scope the declaration is being placed. */
17022 prev_scope = current_scope ();
17024 type_start_token = cp_lexer_peek_token (parser->lexer);
17026 push_deferring_access_checks (dk_no_check);
17027 nested_name_specifier
17028 = cp_parser_nested_name_specifier_opt (parser,
17029 /*typename_keyword_p=*/true,
17030 /*check_dependency_p=*/false,
17031 /*type_p=*/false,
17032 /*is_declaration=*/false);
17034 if (nested_name_specifier)
17036 tree name;
17038 identifier = cp_parser_identifier (parser);
17039 name = cp_parser_lookup_name (parser, identifier,
17040 enum_type,
17041 /*is_template=*/false,
17042 /*is_namespace=*/false,
17043 /*check_dependency=*/true,
17044 /*ambiguous_decls=*/NULL,
17045 input_location);
17046 if (name && name != error_mark_node)
17048 type = TREE_TYPE (name);
17049 if (TREE_CODE (type) == TYPENAME_TYPE)
17051 /* Are template enums allowed in ISO? */
17052 if (template_parm_scope_p ())
17053 pedwarn (type_start_token->location, OPT_Wpedantic,
17054 "%qD is an enumeration template", name);
17055 /* ignore a typename reference, for it will be solved by name
17056 in start_enum. */
17057 type = NULL_TREE;
17060 else if (nested_name_specifier == error_mark_node)
17061 /* We already issued an error. */;
17062 else
17064 error_at (type_start_token->location,
17065 "%qD does not name an enumeration in %qT",
17066 identifier, nested_name_specifier);
17067 nested_name_specifier = error_mark_node;
17070 else
17072 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17073 identifier = cp_parser_identifier (parser);
17074 else
17076 identifier = make_anon_name ();
17077 is_anonymous = true;
17078 if (scoped_enum_p)
17079 error_at (type_start_token->location,
17080 "anonymous scoped enum is not allowed");
17083 pop_deferring_access_checks ();
17085 /* Check for the `:' that denotes a specified underlying type in C++0x.
17086 Note that a ':' could also indicate a bitfield width, however. */
17087 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17089 cp_decl_specifier_seq type_specifiers;
17091 /* Consume the `:'. */
17092 cp_lexer_consume_token (parser->lexer);
17094 /* Parse the type-specifier-seq. */
17095 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17096 /*is_trailing_return=*/false,
17097 &type_specifiers);
17099 /* At this point this is surely not elaborated type specifier. */
17100 if (!cp_parser_parse_definitely (parser))
17101 return NULL_TREE;
17103 if (cxx_dialect < cxx11)
17104 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17106 has_underlying_type = true;
17108 /* If that didn't work, stop. */
17109 if (type_specifiers.type != error_mark_node)
17111 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
17112 /*initialized=*/0, NULL);
17113 if (underlying_type == error_mark_node
17114 || check_for_bare_parameter_packs (underlying_type))
17115 underlying_type = NULL_TREE;
17119 /* Look for the `{' but don't consume it yet. */
17120 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17122 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
17124 cp_parser_error (parser, "expected %<{%>");
17125 if (has_underlying_type)
17127 type = NULL_TREE;
17128 goto out;
17131 /* An opaque-enum-specifier must have a ';' here. */
17132 if ((scoped_enum_p || underlying_type)
17133 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17135 cp_parser_error (parser, "expected %<;%> or %<{%>");
17136 if (has_underlying_type)
17138 type = NULL_TREE;
17139 goto out;
17144 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
17145 return NULL_TREE;
17147 if (nested_name_specifier)
17149 if (CLASS_TYPE_P (nested_name_specifier))
17151 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
17152 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
17153 push_scope (nested_name_specifier);
17155 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17157 push_nested_namespace (nested_name_specifier);
17161 /* Issue an error message if type-definitions are forbidden here. */
17162 if (!cp_parser_check_type_definition (parser))
17163 type = error_mark_node;
17164 else
17165 /* Create the new type. We do this before consuming the opening
17166 brace so the enum will be recorded as being on the line of its
17167 tag (or the 'enum' keyword, if there is no tag). */
17168 type = start_enum (identifier, type, underlying_type,
17169 attributes, scoped_enum_p, &is_new_type);
17171 /* If the next token is not '{' it is an opaque-enum-specifier or an
17172 elaborated-type-specifier. */
17173 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17175 timevar_push (TV_PARSE_ENUM);
17176 if (nested_name_specifier
17177 && nested_name_specifier != error_mark_node)
17179 /* The following catches invalid code such as:
17180 enum class S<int>::E { A, B, C }; */
17181 if (!processing_specialization
17182 && CLASS_TYPE_P (nested_name_specifier)
17183 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
17184 error_at (type_start_token->location, "cannot add an enumerator "
17185 "list to a template instantiation");
17187 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
17189 error_at (type_start_token->location,
17190 "%<%T::%E%> has not been declared",
17191 TYPE_CONTEXT (nested_name_specifier),
17192 nested_name_specifier);
17193 type = error_mark_node;
17195 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
17196 && !CLASS_TYPE_P (nested_name_specifier))
17198 error_at (type_start_token->location, "nested name specifier "
17199 "%qT for enum declaration does not name a class "
17200 "or namespace", nested_name_specifier);
17201 type = error_mark_node;
17203 /* If that scope does not contain the scope in which the
17204 class was originally declared, the program is invalid. */
17205 else if (prev_scope && !is_ancestor (prev_scope,
17206 nested_name_specifier))
17208 if (at_namespace_scope_p ())
17209 error_at (type_start_token->location,
17210 "declaration of %qD in namespace %qD which does not "
17211 "enclose %qD",
17212 type, prev_scope, nested_name_specifier);
17213 else
17214 error_at (type_start_token->location,
17215 "declaration of %qD in %qD which does not "
17216 "enclose %qD",
17217 type, prev_scope, nested_name_specifier);
17218 type = error_mark_node;
17222 if (scoped_enum_p)
17223 begin_scope (sk_scoped_enum, type);
17225 /* Consume the opening brace. */
17226 cp_lexer_consume_token (parser->lexer);
17228 if (type == error_mark_node)
17229 ; /* Nothing to add */
17230 else if (OPAQUE_ENUM_P (type)
17231 || (cxx_dialect > cxx98 && processing_specialization))
17233 new_value_list = true;
17234 SET_OPAQUE_ENUM_P (type, false);
17235 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17237 else
17239 error_at (type_start_token->location,
17240 "multiple definition of %q#T", type);
17241 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
17242 "previous definition here");
17243 type = error_mark_node;
17246 if (type == error_mark_node)
17247 cp_parser_skip_to_end_of_block_or_statement (parser);
17248 /* If the next token is not '}', then there are some enumerators. */
17249 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17251 if (is_anonymous && !scoped_enum_p)
17252 pedwarn (type_start_token->location, OPT_Wpedantic,
17253 "ISO C++ forbids empty anonymous enum");
17255 else
17256 cp_parser_enumerator_list (parser, type);
17258 /* Consume the final '}'. */
17259 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17261 if (scoped_enum_p)
17262 finish_scope ();
17263 timevar_pop (TV_PARSE_ENUM);
17265 else
17267 /* If a ';' follows, then it is an opaque-enum-specifier
17268 and additional restrictions apply. */
17269 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17271 if (is_anonymous)
17272 error_at (type_start_token->location,
17273 "opaque-enum-specifier without name");
17274 else if (nested_name_specifier)
17275 error_at (type_start_token->location,
17276 "opaque-enum-specifier must use a simple identifier");
17280 /* Look for trailing attributes to apply to this enumeration, and
17281 apply them if appropriate. */
17282 if (cp_parser_allow_gnu_extensions_p (parser))
17284 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
17285 cplus_decl_attributes (&type,
17286 trailing_attr,
17287 (int) ATTR_FLAG_TYPE_IN_PLACE);
17290 /* Finish up the enumeration. */
17291 if (type != error_mark_node)
17293 if (new_value_list)
17294 finish_enum_value_list (type);
17295 if (is_new_type)
17296 finish_enum (type);
17299 if (nested_name_specifier)
17301 if (CLASS_TYPE_P (nested_name_specifier))
17303 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
17304 pop_scope (nested_name_specifier);
17306 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17308 pop_nested_namespace (nested_name_specifier);
17311 out:
17312 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17313 return type;
17316 /* Parse an enumerator-list. The enumerators all have the indicated
17317 TYPE.
17319 enumerator-list:
17320 enumerator-definition
17321 enumerator-list , enumerator-definition */
17323 static void
17324 cp_parser_enumerator_list (cp_parser* parser, tree type)
17326 while (true)
17328 /* Parse an enumerator-definition. */
17329 cp_parser_enumerator_definition (parser, type);
17331 /* If the next token is not a ',', we've reached the end of
17332 the list. */
17333 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17334 break;
17335 /* Otherwise, consume the `,' and keep going. */
17336 cp_lexer_consume_token (parser->lexer);
17337 /* If the next token is a `}', there is a trailing comma. */
17338 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17340 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
17341 pedwarn (input_location, OPT_Wpedantic,
17342 "comma at end of enumerator list");
17343 break;
17348 /* Parse an enumerator-definition. The enumerator has the indicated
17349 TYPE.
17351 enumerator-definition:
17352 enumerator
17353 enumerator = constant-expression
17355 enumerator:
17356 identifier
17358 GNU Extensions:
17360 enumerator-definition:
17361 enumerator attributes [opt]
17362 enumerator attributes [opt] = constant-expression */
17364 static void
17365 cp_parser_enumerator_definition (cp_parser* parser, tree type)
17367 tree identifier;
17368 tree value;
17369 location_t loc;
17371 /* Save the input location because we are interested in the location
17372 of the identifier and not the location of the explicit value. */
17373 loc = cp_lexer_peek_token (parser->lexer)->location;
17375 /* Look for the identifier. */
17376 identifier = cp_parser_identifier (parser);
17377 if (identifier == error_mark_node)
17378 return;
17380 /* Parse any specified attributes. */
17381 tree attrs = cp_parser_attributes_opt (parser);
17383 /* If the next token is an '=', then there is an explicit value. */
17384 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17386 /* Consume the `=' token. */
17387 cp_lexer_consume_token (parser->lexer);
17388 /* Parse the value. */
17389 value = cp_parser_constant_expression (parser);
17391 else
17392 value = NULL_TREE;
17394 /* If we are processing a template, make sure the initializer of the
17395 enumerator doesn't contain any bare template parameter pack. */
17396 if (check_for_bare_parameter_packs (value))
17397 value = error_mark_node;
17399 /* Create the enumerator. */
17400 build_enumerator (identifier, value, type, attrs, loc);
17403 /* Parse a namespace-name.
17405 namespace-name:
17406 original-namespace-name
17407 namespace-alias
17409 Returns the NAMESPACE_DECL for the namespace. */
17411 static tree
17412 cp_parser_namespace_name (cp_parser* parser)
17414 tree identifier;
17415 tree namespace_decl;
17417 cp_token *token = cp_lexer_peek_token (parser->lexer);
17419 /* Get the name of the namespace. */
17420 identifier = cp_parser_identifier (parser);
17421 if (identifier == error_mark_node)
17422 return error_mark_node;
17424 /* Look up the identifier in the currently active scope. Look only
17425 for namespaces, due to:
17427 [basic.lookup.udir]
17429 When looking up a namespace-name in a using-directive or alias
17430 definition, only namespace names are considered.
17432 And:
17434 [basic.lookup.qual]
17436 During the lookup of a name preceding the :: scope resolution
17437 operator, object, function, and enumerator names are ignored.
17439 (Note that cp_parser_qualifying_entity only calls this
17440 function if the token after the name is the scope resolution
17441 operator.) */
17442 namespace_decl = cp_parser_lookup_name (parser, identifier,
17443 none_type,
17444 /*is_template=*/false,
17445 /*is_namespace=*/true,
17446 /*check_dependency=*/true,
17447 /*ambiguous_decls=*/NULL,
17448 token->location);
17449 /* If it's not a namespace, issue an error. */
17450 if (namespace_decl == error_mark_node
17451 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
17453 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17454 error_at (token->location, "%qD is not a namespace-name", identifier);
17455 cp_parser_error (parser, "expected namespace-name");
17456 namespace_decl = error_mark_node;
17459 return namespace_decl;
17462 /* Parse a namespace-definition.
17464 namespace-definition:
17465 named-namespace-definition
17466 unnamed-namespace-definition
17468 named-namespace-definition:
17469 original-namespace-definition
17470 extension-namespace-definition
17472 original-namespace-definition:
17473 namespace identifier { namespace-body }
17475 extension-namespace-definition:
17476 namespace original-namespace-name { namespace-body }
17478 unnamed-namespace-definition:
17479 namespace { namespace-body } */
17481 static void
17482 cp_parser_namespace_definition (cp_parser* parser)
17484 tree identifier, attribs;
17485 bool has_visibility;
17486 bool is_inline;
17487 cp_token* token;
17488 int nested_definition_count = 0;
17490 cp_ensure_no_omp_declare_simd (parser);
17491 cp_ensure_no_oacc_routine (parser);
17492 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
17494 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
17495 is_inline = true;
17496 cp_lexer_consume_token (parser->lexer);
17498 else
17499 is_inline = false;
17501 /* Look for the `namespace' keyword. */
17502 token = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17504 /* Parse any specified attributes before the identifier. */
17505 attribs = cp_parser_attributes_opt (parser);
17507 /* Get the name of the namespace. We do not attempt to distinguish
17508 between an original-namespace-definition and an
17509 extension-namespace-definition at this point. The semantic
17510 analysis routines are responsible for that. */
17511 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17512 identifier = cp_parser_identifier (parser);
17513 else
17514 identifier = NULL_TREE;
17516 /* Parse any specified attributes after the identifier. */
17517 tree post_ident_attribs = cp_parser_attributes_opt (parser);
17518 if (post_ident_attribs)
17520 if (attribs)
17521 attribs = chainon (attribs, post_ident_attribs);
17522 else
17523 attribs = post_ident_attribs;
17526 /* Start the namespace. */
17527 push_namespace (identifier);
17529 /* Parse any nested namespace definition. */
17530 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17532 if (attribs)
17533 error_at (token->location, "a nested namespace definition cannot have attributes");
17534 if (cxx_dialect < cxx1z)
17535 pedwarn (input_location, OPT_Wpedantic,
17536 "nested namespace definitions only available with "
17537 "-std=c++1z or -std=gnu++1z");
17538 if (is_inline)
17539 error_at (token->location, "a nested namespace definition cannot be inline");
17540 while (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17542 cp_lexer_consume_token (parser->lexer);
17543 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17544 identifier = cp_parser_identifier (parser);
17545 else
17547 cp_parser_error (parser, "nested identifier required");
17548 break;
17550 ++nested_definition_count;
17551 push_namespace (identifier);
17555 /* Look for the `{' to validate starting the namespace. */
17556 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
17558 /* "inline namespace" is equivalent to a stub namespace definition
17559 followed by a strong using directive. */
17560 if (is_inline)
17562 tree name_space = current_namespace;
17563 /* Set up namespace association. */
17564 DECL_NAMESPACE_ASSOCIATIONS (name_space)
17565 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
17566 DECL_NAMESPACE_ASSOCIATIONS (name_space));
17567 /* Import the contents of the inline namespace. */
17568 pop_namespace ();
17569 do_using_directive (name_space);
17570 push_namespace (identifier);
17573 has_visibility = handle_namespace_attrs (current_namespace, attribs);
17575 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
17577 /* Parse the body of the namespace. */
17578 cp_parser_namespace_body (parser);
17580 if (has_visibility)
17581 pop_visibility (1);
17583 /* Finish the nested namespace definitions. */
17584 while (nested_definition_count--)
17585 pop_namespace ();
17587 /* Finish the namespace. */
17588 pop_namespace ();
17589 /* Look for the final `}'. */
17590 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17593 /* Parse a namespace-body.
17595 namespace-body:
17596 declaration-seq [opt] */
17598 static void
17599 cp_parser_namespace_body (cp_parser* parser)
17601 cp_parser_declaration_seq_opt (parser);
17604 /* Parse a namespace-alias-definition.
17606 namespace-alias-definition:
17607 namespace identifier = qualified-namespace-specifier ; */
17609 static void
17610 cp_parser_namespace_alias_definition (cp_parser* parser)
17612 tree identifier;
17613 tree namespace_specifier;
17615 cp_token *token = cp_lexer_peek_token (parser->lexer);
17617 /* Look for the `namespace' keyword. */
17618 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17619 /* Look for the identifier. */
17620 identifier = cp_parser_identifier (parser);
17621 if (identifier == error_mark_node)
17622 return;
17623 /* Look for the `=' token. */
17624 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
17625 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17627 error_at (token->location, "%<namespace%> definition is not allowed here");
17628 /* Skip the definition. */
17629 cp_lexer_consume_token (parser->lexer);
17630 if (cp_parser_skip_to_closing_brace (parser))
17631 cp_lexer_consume_token (parser->lexer);
17632 return;
17634 cp_parser_require (parser, CPP_EQ, RT_EQ);
17635 /* Look for the qualified-namespace-specifier. */
17636 namespace_specifier
17637 = cp_parser_qualified_namespace_specifier (parser);
17638 /* Look for the `;' token. */
17639 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17641 /* Register the alias in the symbol table. */
17642 do_namespace_alias (identifier, namespace_specifier);
17645 /* Parse a qualified-namespace-specifier.
17647 qualified-namespace-specifier:
17648 :: [opt] nested-name-specifier [opt] namespace-name
17650 Returns a NAMESPACE_DECL corresponding to the specified
17651 namespace. */
17653 static tree
17654 cp_parser_qualified_namespace_specifier (cp_parser* parser)
17656 /* Look for the optional `::'. */
17657 cp_parser_global_scope_opt (parser,
17658 /*current_scope_valid_p=*/false);
17660 /* Look for the optional nested-name-specifier. */
17661 cp_parser_nested_name_specifier_opt (parser,
17662 /*typename_keyword_p=*/false,
17663 /*check_dependency_p=*/true,
17664 /*type_p=*/false,
17665 /*is_declaration=*/true);
17667 return cp_parser_namespace_name (parser);
17670 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
17671 access declaration.
17673 using-declaration:
17674 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
17675 using :: unqualified-id ;
17677 access-declaration:
17678 qualified-id ;
17682 static bool
17683 cp_parser_using_declaration (cp_parser* parser,
17684 bool access_declaration_p)
17686 cp_token *token;
17687 bool typename_p = false;
17688 bool global_scope_p;
17689 tree decl;
17690 tree identifier;
17691 tree qscope;
17692 int oldcount = errorcount;
17693 cp_token *diag_token = NULL;
17695 if (access_declaration_p)
17697 diag_token = cp_lexer_peek_token (parser->lexer);
17698 cp_parser_parse_tentatively (parser);
17700 else
17702 /* Look for the `using' keyword. */
17703 cp_parser_require_keyword (parser, RID_USING, RT_USING);
17705 /* Peek at the next token. */
17706 token = cp_lexer_peek_token (parser->lexer);
17707 /* See if it's `typename'. */
17708 if (token->keyword == RID_TYPENAME)
17710 /* Remember that we've seen it. */
17711 typename_p = true;
17712 /* Consume the `typename' token. */
17713 cp_lexer_consume_token (parser->lexer);
17717 /* Look for the optional global scope qualification. */
17718 global_scope_p
17719 = (cp_parser_global_scope_opt (parser,
17720 /*current_scope_valid_p=*/false)
17721 != NULL_TREE);
17723 /* If we saw `typename', or didn't see `::', then there must be a
17724 nested-name-specifier present. */
17725 if (typename_p || !global_scope_p)
17727 qscope = cp_parser_nested_name_specifier (parser, typename_p,
17728 /*check_dependency_p=*/true,
17729 /*type_p=*/false,
17730 /*is_declaration=*/true);
17731 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
17733 cp_parser_skip_to_end_of_block_or_statement (parser);
17734 return false;
17737 /* Otherwise, we could be in either of the two productions. In that
17738 case, treat the nested-name-specifier as optional. */
17739 else
17740 qscope = cp_parser_nested_name_specifier_opt (parser,
17741 /*typename_keyword_p=*/false,
17742 /*check_dependency_p=*/true,
17743 /*type_p=*/false,
17744 /*is_declaration=*/true);
17745 if (!qscope)
17746 qscope = global_namespace;
17747 else if (UNSCOPED_ENUM_P (qscope))
17748 qscope = CP_TYPE_CONTEXT (qscope);
17750 if (access_declaration_p && cp_parser_error_occurred (parser))
17751 /* Something has already gone wrong; there's no need to parse
17752 further. Since an error has occurred, the return value of
17753 cp_parser_parse_definitely will be false, as required. */
17754 return cp_parser_parse_definitely (parser);
17756 token = cp_lexer_peek_token (parser->lexer);
17757 /* Parse the unqualified-id. */
17758 identifier = cp_parser_unqualified_id (parser,
17759 /*template_keyword_p=*/false,
17760 /*check_dependency_p=*/true,
17761 /*declarator_p=*/true,
17762 /*optional_p=*/false);
17764 if (access_declaration_p)
17766 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17767 cp_parser_simulate_error (parser);
17768 if (!cp_parser_parse_definitely (parser))
17769 return false;
17772 /* The function we call to handle a using-declaration is different
17773 depending on what scope we are in. */
17774 if (qscope == error_mark_node || identifier == error_mark_node)
17776 else if (!identifier_p (identifier)
17777 && TREE_CODE (identifier) != BIT_NOT_EXPR)
17778 /* [namespace.udecl]
17780 A using declaration shall not name a template-id. */
17781 error_at (token->location,
17782 "a template-id may not appear in a using-declaration");
17783 else
17785 if (at_class_scope_p ())
17787 /* Create the USING_DECL. */
17788 decl = do_class_using_decl (parser->scope, identifier);
17790 if (decl && typename_p)
17791 USING_DECL_TYPENAME_P (decl) = 1;
17793 if (check_for_bare_parameter_packs (decl))
17795 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17796 return false;
17798 else
17799 /* Add it to the list of members in this class. */
17800 finish_member_declaration (decl);
17802 else
17804 decl = cp_parser_lookup_name_simple (parser,
17805 identifier,
17806 token->location);
17807 if (decl == error_mark_node)
17808 cp_parser_name_lookup_error (parser, identifier,
17809 decl, NLE_NULL,
17810 token->location);
17811 else if (check_for_bare_parameter_packs (decl))
17813 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17814 return false;
17816 else if (!at_namespace_scope_p ())
17817 do_local_using_decl (decl, qscope, identifier);
17818 else
17819 do_toplevel_using_decl (decl, qscope, identifier);
17823 /* Look for the final `;'. */
17824 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17826 if (access_declaration_p && errorcount == oldcount)
17827 warning_at (diag_token->location, OPT_Wdeprecated,
17828 "access declarations are deprecated "
17829 "in favour of using-declarations; "
17830 "suggestion: add the %<using%> keyword");
17832 return true;
17835 /* Parse an alias-declaration.
17837 alias-declaration:
17838 using identifier attribute-specifier-seq [opt] = type-id */
17840 static tree
17841 cp_parser_alias_declaration (cp_parser* parser)
17843 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
17844 location_t id_location;
17845 cp_declarator *declarator;
17846 cp_decl_specifier_seq decl_specs;
17847 bool member_p;
17848 const char *saved_message = NULL;
17850 /* Look for the `using' keyword. */
17851 cp_token *using_token
17852 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
17853 if (using_token == NULL)
17854 return error_mark_node;
17856 id_location = cp_lexer_peek_token (parser->lexer)->location;
17857 id = cp_parser_identifier (parser);
17858 if (id == error_mark_node)
17859 return error_mark_node;
17861 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
17862 attributes = cp_parser_attributes_opt (parser);
17863 if (attributes == error_mark_node)
17864 return error_mark_node;
17866 cp_parser_require (parser, CPP_EQ, RT_EQ);
17868 if (cp_parser_error_occurred (parser))
17869 return error_mark_node;
17871 cp_parser_commit_to_tentative_parse (parser);
17873 /* Now we are going to parse the type-id of the declaration. */
17876 [dcl.type]/3 says:
17878 "A type-specifier-seq shall not define a class or enumeration
17879 unless it appears in the type-id of an alias-declaration (7.1.3) that
17880 is not the declaration of a template-declaration."
17882 In other words, if we currently are in an alias template, the
17883 type-id should not define a type.
17885 So let's set parser->type_definition_forbidden_message in that
17886 case; cp_parser_check_type_definition (called by
17887 cp_parser_class_specifier) will then emit an error if a type is
17888 defined in the type-id. */
17889 if (parser->num_template_parameter_lists)
17891 saved_message = parser->type_definition_forbidden_message;
17892 parser->type_definition_forbidden_message =
17893 G_("types may not be defined in alias template declarations");
17896 type = cp_parser_type_id (parser);
17898 /* Restore the error message if need be. */
17899 if (parser->num_template_parameter_lists)
17900 parser->type_definition_forbidden_message = saved_message;
17902 if (type == error_mark_node
17903 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
17905 cp_parser_skip_to_end_of_block_or_statement (parser);
17906 return error_mark_node;
17909 /* A typedef-name can also be introduced by an alias-declaration. The
17910 identifier following the using keyword becomes a typedef-name. It has
17911 the same semantics as if it were introduced by the typedef
17912 specifier. In particular, it does not define a new type and it shall
17913 not appear in the type-id. */
17915 clear_decl_specs (&decl_specs);
17916 decl_specs.type = type;
17917 if (attributes != NULL_TREE)
17919 decl_specs.attributes = attributes;
17920 set_and_check_decl_spec_loc (&decl_specs,
17921 ds_attribute,
17922 attrs_token);
17924 set_and_check_decl_spec_loc (&decl_specs,
17925 ds_typedef,
17926 using_token);
17927 set_and_check_decl_spec_loc (&decl_specs,
17928 ds_alias,
17929 using_token);
17931 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
17932 declarator->id_loc = id_location;
17934 member_p = at_class_scope_p ();
17935 if (member_p)
17936 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
17937 NULL_TREE, attributes);
17938 else
17939 decl = start_decl (declarator, &decl_specs, 0,
17940 attributes, NULL_TREE, &pushed_scope);
17941 if (decl == error_mark_node)
17942 return decl;
17944 // Attach constraints to the alias declaration.
17945 if (flag_concepts && current_template_parms)
17947 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
17948 tree constr = build_constraints (reqs, NULL_TREE);
17949 set_constraints (decl, constr);
17952 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
17954 if (pushed_scope)
17955 pop_scope (pushed_scope);
17957 /* If decl is a template, return its TEMPLATE_DECL so that it gets
17958 added into the symbol table; otherwise, return the TYPE_DECL. */
17959 if (DECL_LANG_SPECIFIC (decl)
17960 && DECL_TEMPLATE_INFO (decl)
17961 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
17963 decl = DECL_TI_TEMPLATE (decl);
17964 if (member_p)
17965 check_member_template (decl);
17968 return decl;
17971 /* Parse a using-directive.
17973 using-directive:
17974 using namespace :: [opt] nested-name-specifier [opt]
17975 namespace-name ; */
17977 static void
17978 cp_parser_using_directive (cp_parser* parser)
17980 tree namespace_decl;
17981 tree attribs;
17983 /* Look for the `using' keyword. */
17984 cp_parser_require_keyword (parser, RID_USING, RT_USING);
17985 /* And the `namespace' keyword. */
17986 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17987 /* Look for the optional `::' operator. */
17988 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17989 /* And the optional nested-name-specifier. */
17990 cp_parser_nested_name_specifier_opt (parser,
17991 /*typename_keyword_p=*/false,
17992 /*check_dependency_p=*/true,
17993 /*type_p=*/false,
17994 /*is_declaration=*/true);
17995 /* Get the namespace being used. */
17996 namespace_decl = cp_parser_namespace_name (parser);
17997 /* And any specified attributes. */
17998 attribs = cp_parser_attributes_opt (parser);
17999 /* Update the symbol table. */
18000 parse_using_directive (namespace_decl, attribs);
18001 /* Look for the final `;'. */
18002 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18005 /* Parse an asm-definition.
18007 asm-definition:
18008 asm ( string-literal ) ;
18010 GNU Extension:
18012 asm-definition:
18013 asm volatile [opt] ( string-literal ) ;
18014 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18015 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18016 : asm-operand-list [opt] ) ;
18017 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18018 : asm-operand-list [opt]
18019 : asm-clobber-list [opt] ) ;
18020 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18021 : asm-clobber-list [opt]
18022 : asm-goto-list ) ; */
18024 static void
18025 cp_parser_asm_definition (cp_parser* parser)
18027 tree string;
18028 tree outputs = NULL_TREE;
18029 tree inputs = NULL_TREE;
18030 tree clobbers = NULL_TREE;
18031 tree labels = NULL_TREE;
18032 tree asm_stmt;
18033 bool volatile_p = false;
18034 bool extended_p = false;
18035 bool invalid_inputs_p = false;
18036 bool invalid_outputs_p = false;
18037 bool goto_p = false;
18038 required_token missing = RT_NONE;
18040 /* Look for the `asm' keyword. */
18041 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
18043 if (parser->in_function_body
18044 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
18046 error ("%<asm%> in %<constexpr%> function");
18047 cp_function_chain->invalid_constexpr = true;
18050 /* See if the next token is `volatile'. */
18051 if (cp_parser_allow_gnu_extensions_p (parser)
18052 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
18054 /* Remember that we saw the `volatile' keyword. */
18055 volatile_p = true;
18056 /* Consume the token. */
18057 cp_lexer_consume_token (parser->lexer);
18059 if (cp_parser_allow_gnu_extensions_p (parser)
18060 && parser->in_function_body
18061 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
18063 /* Remember that we saw the `goto' keyword. */
18064 goto_p = true;
18065 /* Consume the token. */
18066 cp_lexer_consume_token (parser->lexer);
18068 /* Look for the opening `('. */
18069 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18070 return;
18071 /* Look for the string. */
18072 string = cp_parser_string_literal (parser, false, false);
18073 if (string == error_mark_node)
18075 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18076 /*consume_paren=*/true);
18077 return;
18080 /* If we're allowing GNU extensions, check for the extended assembly
18081 syntax. Unfortunately, the `:' tokens need not be separated by
18082 a space in C, and so, for compatibility, we tolerate that here
18083 too. Doing that means that we have to treat the `::' operator as
18084 two `:' tokens. */
18085 if (cp_parser_allow_gnu_extensions_p (parser)
18086 && parser->in_function_body
18087 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
18088 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
18090 bool inputs_p = false;
18091 bool clobbers_p = false;
18092 bool labels_p = false;
18094 /* The extended syntax was used. */
18095 extended_p = true;
18097 /* Look for outputs. */
18098 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18100 /* Consume the `:'. */
18101 cp_lexer_consume_token (parser->lexer);
18102 /* Parse the output-operands. */
18103 if (cp_lexer_next_token_is_not (parser->lexer,
18104 CPP_COLON)
18105 && cp_lexer_next_token_is_not (parser->lexer,
18106 CPP_SCOPE)
18107 && cp_lexer_next_token_is_not (parser->lexer,
18108 CPP_CLOSE_PAREN)
18109 && !goto_p)
18111 outputs = cp_parser_asm_operand_list (parser);
18112 if (outputs == error_mark_node)
18113 invalid_outputs_p = true;
18116 /* If the next token is `::', there are no outputs, and the
18117 next token is the beginning of the inputs. */
18118 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18119 /* The inputs are coming next. */
18120 inputs_p = true;
18122 /* Look for inputs. */
18123 if (inputs_p
18124 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18126 /* Consume the `:' or `::'. */
18127 cp_lexer_consume_token (parser->lexer);
18128 /* Parse the output-operands. */
18129 if (cp_lexer_next_token_is_not (parser->lexer,
18130 CPP_COLON)
18131 && cp_lexer_next_token_is_not (parser->lexer,
18132 CPP_SCOPE)
18133 && cp_lexer_next_token_is_not (parser->lexer,
18134 CPP_CLOSE_PAREN))
18136 inputs = cp_parser_asm_operand_list (parser);
18137 if (inputs == error_mark_node)
18138 invalid_inputs_p = true;
18141 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18142 /* The clobbers are coming next. */
18143 clobbers_p = true;
18145 /* Look for clobbers. */
18146 if (clobbers_p
18147 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18149 clobbers_p = true;
18150 /* Consume the `:' or `::'. */
18151 cp_lexer_consume_token (parser->lexer);
18152 /* Parse the clobbers. */
18153 if (cp_lexer_next_token_is_not (parser->lexer,
18154 CPP_COLON)
18155 && cp_lexer_next_token_is_not (parser->lexer,
18156 CPP_CLOSE_PAREN))
18157 clobbers = cp_parser_asm_clobber_list (parser);
18159 else if (goto_p
18160 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18161 /* The labels are coming next. */
18162 labels_p = true;
18164 /* Look for labels. */
18165 if (labels_p
18166 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
18168 labels_p = true;
18169 /* Consume the `:' or `::'. */
18170 cp_lexer_consume_token (parser->lexer);
18171 /* Parse the labels. */
18172 labels = cp_parser_asm_label_list (parser);
18175 if (goto_p && !labels_p)
18176 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
18178 else if (goto_p)
18179 missing = RT_COLON_SCOPE;
18181 /* Look for the closing `)'. */
18182 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
18183 missing ? missing : RT_CLOSE_PAREN))
18184 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18185 /*consume_paren=*/true);
18186 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18188 if (!invalid_inputs_p && !invalid_outputs_p)
18190 /* Create the ASM_EXPR. */
18191 if (parser->in_function_body)
18193 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
18194 inputs, clobbers, labels);
18195 /* If the extended syntax was not used, mark the ASM_EXPR. */
18196 if (!extended_p)
18198 tree temp = asm_stmt;
18199 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
18200 temp = TREE_OPERAND (temp, 0);
18202 ASM_INPUT_P (temp) = 1;
18205 else
18206 symtab->finalize_toplevel_asm (string);
18210 /* Declarators [gram.dcl.decl] */
18212 /* Parse an init-declarator.
18214 init-declarator:
18215 declarator initializer [opt]
18217 GNU Extension:
18219 init-declarator:
18220 declarator asm-specification [opt] attributes [opt] initializer [opt]
18222 function-definition:
18223 decl-specifier-seq [opt] declarator ctor-initializer [opt]
18224 function-body
18225 decl-specifier-seq [opt] declarator function-try-block
18227 GNU Extension:
18229 function-definition:
18230 __extension__ function-definition
18232 TM Extension:
18234 function-definition:
18235 decl-specifier-seq [opt] declarator function-transaction-block
18237 The DECL_SPECIFIERS apply to this declarator. Returns a
18238 representation of the entity declared. If MEMBER_P is TRUE, then
18239 this declarator appears in a class scope. The new DECL created by
18240 this declarator is returned.
18242 The CHECKS are access checks that should be performed once we know
18243 what entity is being declared (and, therefore, what classes have
18244 befriended it).
18246 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
18247 for a function-definition here as well. If the declarator is a
18248 declarator for a function-definition, *FUNCTION_DEFINITION_P will
18249 be TRUE upon return. By that point, the function-definition will
18250 have been completely parsed.
18252 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
18253 is FALSE.
18255 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
18256 parsed declaration if it is an uninitialized single declarator not followed
18257 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
18258 if present, will not be consumed. If returned, this declarator will be
18259 created with SD_INITIALIZED but will not call cp_finish_decl.
18261 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
18262 and there is an initializer, the pointed location_t is set to the
18263 location of the '=' or `(', or '{' in C++11 token introducing the
18264 initializer. */
18266 static tree
18267 cp_parser_init_declarator (cp_parser* parser,
18268 cp_decl_specifier_seq *decl_specifiers,
18269 vec<deferred_access_check, va_gc> *checks,
18270 bool function_definition_allowed_p,
18271 bool member_p,
18272 int declares_class_or_enum,
18273 bool* function_definition_p,
18274 tree* maybe_range_for_decl,
18275 location_t* init_loc)
18277 cp_token *token = NULL, *asm_spec_start_token = NULL,
18278 *attributes_start_token = NULL;
18279 cp_declarator *declarator;
18280 tree prefix_attributes;
18281 tree attributes = NULL;
18282 tree asm_specification;
18283 tree initializer;
18284 tree decl = NULL_TREE;
18285 tree scope;
18286 int is_initialized;
18287 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
18288 initialized with "= ..", CPP_OPEN_PAREN if initialized with
18289 "(...)". */
18290 enum cpp_ttype initialization_kind;
18291 bool is_direct_init = false;
18292 bool is_non_constant_init;
18293 int ctor_dtor_or_conv_p;
18294 bool friend_p = cp_parser_friend_p (decl_specifiers);
18295 tree pushed_scope = NULL_TREE;
18296 bool range_for_decl_p = false;
18297 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18298 location_t tmp_init_loc = UNKNOWN_LOCATION;
18300 /* Gather the attributes that were provided with the
18301 decl-specifiers. */
18302 prefix_attributes = decl_specifiers->attributes;
18304 /* Assume that this is not the declarator for a function
18305 definition. */
18306 if (function_definition_p)
18307 *function_definition_p = false;
18309 /* Default arguments are only permitted for function parameters. */
18310 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
18311 parser->default_arg_ok_p = false;
18313 /* Defer access checks while parsing the declarator; we cannot know
18314 what names are accessible until we know what is being
18315 declared. */
18316 resume_deferring_access_checks ();
18318 /* Parse the declarator. */
18319 token = cp_lexer_peek_token (parser->lexer);
18320 declarator
18321 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18322 &ctor_dtor_or_conv_p,
18323 /*parenthesized_p=*/NULL,
18324 member_p, friend_p);
18325 /* Gather up the deferred checks. */
18326 stop_deferring_access_checks ();
18328 parser->default_arg_ok_p = saved_default_arg_ok_p;
18330 /* If the DECLARATOR was erroneous, there's no need to go
18331 further. */
18332 if (declarator == cp_error_declarator)
18333 return error_mark_node;
18335 /* Check that the number of template-parameter-lists is OK. */
18336 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
18337 token->location))
18338 return error_mark_node;
18340 if (declares_class_or_enum & 2)
18341 cp_parser_check_for_definition_in_return_type (declarator,
18342 decl_specifiers->type,
18343 decl_specifiers->locations[ds_type_spec]);
18345 /* Figure out what scope the entity declared by the DECLARATOR is
18346 located in. `grokdeclarator' sometimes changes the scope, so
18347 we compute it now. */
18348 scope = get_scope_of_declarator (declarator);
18350 /* Perform any lookups in the declared type which were thought to be
18351 dependent, but are not in the scope of the declarator. */
18352 decl_specifiers->type
18353 = maybe_update_decl_type (decl_specifiers->type, scope);
18355 /* If we're allowing GNU extensions, look for an
18356 asm-specification. */
18357 if (cp_parser_allow_gnu_extensions_p (parser))
18359 /* Look for an asm-specification. */
18360 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
18361 asm_specification = cp_parser_asm_specification_opt (parser);
18363 else
18364 asm_specification = NULL_TREE;
18366 /* Look for attributes. */
18367 attributes_start_token = cp_lexer_peek_token (parser->lexer);
18368 attributes = cp_parser_attributes_opt (parser);
18370 /* Peek at the next token. */
18371 token = cp_lexer_peek_token (parser->lexer);
18373 bool bogus_implicit_tmpl = false;
18375 if (function_declarator_p (declarator))
18377 /* Check to see if the token indicates the start of a
18378 function-definition. */
18379 if (cp_parser_token_starts_function_definition_p (token))
18381 if (!function_definition_allowed_p)
18383 /* If a function-definition should not appear here, issue an
18384 error message. */
18385 cp_parser_error (parser,
18386 "a function-definition is not allowed here");
18387 return error_mark_node;
18390 location_t func_brace_location
18391 = cp_lexer_peek_token (parser->lexer)->location;
18393 /* Neither attributes nor an asm-specification are allowed
18394 on a function-definition. */
18395 if (asm_specification)
18396 error_at (asm_spec_start_token->location,
18397 "an asm-specification is not allowed "
18398 "on a function-definition");
18399 if (attributes)
18400 error_at (attributes_start_token->location,
18401 "attributes are not allowed "
18402 "on a function-definition");
18403 /* This is a function-definition. */
18404 *function_definition_p = true;
18406 /* Parse the function definition. */
18407 if (member_p)
18408 decl = cp_parser_save_member_function_body (parser,
18409 decl_specifiers,
18410 declarator,
18411 prefix_attributes);
18412 else
18413 decl =
18414 (cp_parser_function_definition_from_specifiers_and_declarator
18415 (parser, decl_specifiers, prefix_attributes, declarator));
18417 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
18419 /* This is where the prologue starts... */
18420 DECL_STRUCT_FUNCTION (decl)->function_start_locus
18421 = func_brace_location;
18424 return decl;
18427 else if (parser->fully_implicit_function_template_p)
18429 /* A non-template declaration involving a function parameter list
18430 containing an implicit template parameter will be made into a
18431 template. If the resulting declaration is not going to be an
18432 actual function then finish the template scope here to prevent it.
18433 An error message will be issued once we have a decl to talk about.
18435 FIXME probably we should do type deduction rather than create an
18436 implicit template, but the standard currently doesn't allow it. */
18437 bogus_implicit_tmpl = true;
18438 finish_fully_implicit_template (parser, NULL_TREE);
18441 /* [dcl.dcl]
18443 Only in function declarations for constructors, destructors, and
18444 type conversions can the decl-specifier-seq be omitted.
18446 We explicitly postpone this check past the point where we handle
18447 function-definitions because we tolerate function-definitions
18448 that are missing their return types in some modes. */
18449 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
18451 cp_parser_error (parser,
18452 "expected constructor, destructor, or type conversion");
18453 return error_mark_node;
18456 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
18457 if (token->type == CPP_EQ
18458 || token->type == CPP_OPEN_PAREN
18459 || token->type == CPP_OPEN_BRACE)
18461 is_initialized = SD_INITIALIZED;
18462 initialization_kind = token->type;
18463 if (maybe_range_for_decl)
18464 *maybe_range_for_decl = error_mark_node;
18465 tmp_init_loc = token->location;
18466 if (init_loc && *init_loc == UNKNOWN_LOCATION)
18467 *init_loc = tmp_init_loc;
18469 if (token->type == CPP_EQ
18470 && function_declarator_p (declarator))
18472 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
18473 if (t2->keyword == RID_DEFAULT)
18474 is_initialized = SD_DEFAULTED;
18475 else if (t2->keyword == RID_DELETE)
18476 is_initialized = SD_DELETED;
18479 else
18481 /* If the init-declarator isn't initialized and isn't followed by a
18482 `,' or `;', it's not a valid init-declarator. */
18483 if (token->type != CPP_COMMA
18484 && token->type != CPP_SEMICOLON)
18486 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
18487 range_for_decl_p = true;
18488 else
18490 if (!maybe_range_for_decl)
18491 cp_parser_error (parser, "expected initializer");
18492 return error_mark_node;
18495 is_initialized = SD_UNINITIALIZED;
18496 initialization_kind = CPP_EOF;
18499 /* Because start_decl has side-effects, we should only call it if we
18500 know we're going ahead. By this point, we know that we cannot
18501 possibly be looking at any other construct. */
18502 cp_parser_commit_to_tentative_parse (parser);
18504 /* Enter the newly declared entry in the symbol table. If we're
18505 processing a declaration in a class-specifier, we wait until
18506 after processing the initializer. */
18507 if (!member_p)
18509 if (parser->in_unbraced_linkage_specification_p)
18510 decl_specifiers->storage_class = sc_extern;
18511 decl = start_decl (declarator, decl_specifiers,
18512 range_for_decl_p? SD_INITIALIZED : is_initialized,
18513 attributes, prefix_attributes, &pushed_scope);
18514 cp_finalize_omp_declare_simd (parser, decl);
18515 cp_finalize_oacc_routine (parser, decl, false);
18516 /* Adjust location of decl if declarator->id_loc is more appropriate:
18517 set, and decl wasn't merged with another decl, in which case its
18518 location would be different from input_location, and more accurate. */
18519 if (DECL_P (decl)
18520 && declarator->id_loc != UNKNOWN_LOCATION
18521 && DECL_SOURCE_LOCATION (decl) == input_location)
18522 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
18524 else if (scope)
18525 /* Enter the SCOPE. That way unqualified names appearing in the
18526 initializer will be looked up in SCOPE. */
18527 pushed_scope = push_scope (scope);
18529 /* Perform deferred access control checks, now that we know in which
18530 SCOPE the declared entity resides. */
18531 if (!member_p && decl)
18533 tree saved_current_function_decl = NULL_TREE;
18535 /* If the entity being declared is a function, pretend that we
18536 are in its scope. If it is a `friend', it may have access to
18537 things that would not otherwise be accessible. */
18538 if (TREE_CODE (decl) == FUNCTION_DECL)
18540 saved_current_function_decl = current_function_decl;
18541 current_function_decl = decl;
18544 /* Perform access checks for template parameters. */
18545 cp_parser_perform_template_parameter_access_checks (checks);
18547 /* Perform the access control checks for the declarator and the
18548 decl-specifiers. */
18549 perform_deferred_access_checks (tf_warning_or_error);
18551 /* Restore the saved value. */
18552 if (TREE_CODE (decl) == FUNCTION_DECL)
18553 current_function_decl = saved_current_function_decl;
18556 /* Parse the initializer. */
18557 initializer = NULL_TREE;
18558 is_direct_init = false;
18559 is_non_constant_init = true;
18560 if (is_initialized)
18562 if (function_declarator_p (declarator))
18564 if (initialization_kind == CPP_EQ)
18565 initializer = cp_parser_pure_specifier (parser);
18566 else
18568 /* If the declaration was erroneous, we don't really
18569 know what the user intended, so just silently
18570 consume the initializer. */
18571 if (decl != error_mark_node)
18572 error_at (tmp_init_loc, "initializer provided for function");
18573 cp_parser_skip_to_closing_parenthesis (parser,
18574 /*recovering=*/true,
18575 /*or_comma=*/false,
18576 /*consume_paren=*/true);
18579 else
18581 /* We want to record the extra mangling scope for in-class
18582 initializers of class members and initializers of static data
18583 member templates. The former involves deferring
18584 parsing of the initializer until end of class as with default
18585 arguments. So right here we only handle the latter. */
18586 if (!member_p && processing_template_decl)
18587 start_lambda_scope (decl);
18588 initializer = cp_parser_initializer (parser,
18589 &is_direct_init,
18590 &is_non_constant_init);
18591 if (!member_p && processing_template_decl)
18592 finish_lambda_scope ();
18593 if (initializer == error_mark_node)
18594 cp_parser_skip_to_end_of_statement (parser);
18598 /* The old parser allows attributes to appear after a parenthesized
18599 initializer. Mark Mitchell proposed removing this functionality
18600 on the GCC mailing lists on 2002-08-13. This parser accepts the
18601 attributes -- but ignores them. */
18602 if (cp_parser_allow_gnu_extensions_p (parser)
18603 && initialization_kind == CPP_OPEN_PAREN)
18604 if (cp_parser_attributes_opt (parser))
18605 warning (OPT_Wattributes,
18606 "attributes after parenthesized initializer ignored");
18608 /* And now complain about a non-function implicit template. */
18609 if (bogus_implicit_tmpl && decl != error_mark_node)
18610 error_at (DECL_SOURCE_LOCATION (decl),
18611 "non-function %qD declared as implicit template", decl);
18613 /* For an in-class declaration, use `grokfield' to create the
18614 declaration. */
18615 if (member_p)
18617 if (pushed_scope)
18619 pop_scope (pushed_scope);
18620 pushed_scope = NULL_TREE;
18622 decl = grokfield (declarator, decl_specifiers,
18623 initializer, !is_non_constant_init,
18624 /*asmspec=*/NULL_TREE,
18625 chainon (attributes, prefix_attributes));
18626 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
18627 cp_parser_save_default_args (parser, decl);
18628 cp_finalize_omp_declare_simd (parser, decl);
18629 cp_finalize_oacc_routine (parser, decl, false);
18632 /* Finish processing the declaration. But, skip member
18633 declarations. */
18634 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
18636 cp_finish_decl (decl,
18637 initializer, !is_non_constant_init,
18638 asm_specification,
18639 /* If the initializer is in parentheses, then this is
18640 a direct-initialization, which means that an
18641 `explicit' constructor is OK. Otherwise, an
18642 `explicit' constructor cannot be used. */
18643 ((is_direct_init || !is_initialized)
18644 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
18646 else if ((cxx_dialect != cxx98) && friend_p
18647 && decl && TREE_CODE (decl) == FUNCTION_DECL)
18648 /* Core issue #226 (C++0x only): A default template-argument
18649 shall not be specified in a friend class template
18650 declaration. */
18651 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
18652 /*is_partial=*/false, /*is_friend_decl=*/1);
18654 if (!friend_p && pushed_scope)
18655 pop_scope (pushed_scope);
18657 if (function_declarator_p (declarator)
18658 && parser->fully_implicit_function_template_p)
18660 if (member_p)
18661 decl = finish_fully_implicit_template (parser, decl);
18662 else
18663 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
18666 return decl;
18669 /* Parse a declarator.
18671 declarator:
18672 direct-declarator
18673 ptr-operator declarator
18675 abstract-declarator:
18676 ptr-operator abstract-declarator [opt]
18677 direct-abstract-declarator
18679 GNU Extensions:
18681 declarator:
18682 attributes [opt] direct-declarator
18683 attributes [opt] ptr-operator declarator
18685 abstract-declarator:
18686 attributes [opt] ptr-operator abstract-declarator [opt]
18687 attributes [opt] direct-abstract-declarator
18689 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
18690 detect constructor, destructor or conversion operators. It is set
18691 to -1 if the declarator is a name, and +1 if it is a
18692 function. Otherwise it is set to zero. Usually you just want to
18693 test for >0, but internally the negative value is used.
18695 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
18696 a decl-specifier-seq unless it declares a constructor, destructor,
18697 or conversion. It might seem that we could check this condition in
18698 semantic analysis, rather than parsing, but that makes it difficult
18699 to handle something like `f()'. We want to notice that there are
18700 no decl-specifiers, and therefore realize that this is an
18701 expression, not a declaration.)
18703 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
18704 the declarator is a direct-declarator of the form "(...)".
18706 MEMBER_P is true iff this declarator is a member-declarator.
18708 FRIEND_P is true iff this declarator is a friend. */
18710 static cp_declarator *
18711 cp_parser_declarator (cp_parser* parser,
18712 cp_parser_declarator_kind dcl_kind,
18713 int* ctor_dtor_or_conv_p,
18714 bool* parenthesized_p,
18715 bool member_p, bool friend_p)
18717 cp_declarator *declarator;
18718 enum tree_code code;
18719 cp_cv_quals cv_quals;
18720 tree class_type;
18721 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
18723 /* Assume this is not a constructor, destructor, or type-conversion
18724 operator. */
18725 if (ctor_dtor_or_conv_p)
18726 *ctor_dtor_or_conv_p = 0;
18728 if (cp_parser_allow_gnu_extensions_p (parser))
18729 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
18731 /* Check for the ptr-operator production. */
18732 cp_parser_parse_tentatively (parser);
18733 /* Parse the ptr-operator. */
18734 code = cp_parser_ptr_operator (parser,
18735 &class_type,
18736 &cv_quals,
18737 &std_attributes);
18739 /* If that worked, then we have a ptr-operator. */
18740 if (cp_parser_parse_definitely (parser))
18742 /* If a ptr-operator was found, then this declarator was not
18743 parenthesized. */
18744 if (parenthesized_p)
18745 *parenthesized_p = true;
18746 /* The dependent declarator is optional if we are parsing an
18747 abstract-declarator. */
18748 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18749 cp_parser_parse_tentatively (parser);
18751 /* Parse the dependent declarator. */
18752 declarator = cp_parser_declarator (parser, dcl_kind,
18753 /*ctor_dtor_or_conv_p=*/NULL,
18754 /*parenthesized_p=*/NULL,
18755 /*member_p=*/false,
18756 friend_p);
18758 /* If we are parsing an abstract-declarator, we must handle the
18759 case where the dependent declarator is absent. */
18760 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
18761 && !cp_parser_parse_definitely (parser))
18762 declarator = NULL;
18764 declarator = cp_parser_make_indirect_declarator
18765 (code, class_type, cv_quals, declarator, std_attributes);
18767 /* Everything else is a direct-declarator. */
18768 else
18770 if (parenthesized_p)
18771 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
18772 CPP_OPEN_PAREN);
18773 declarator = cp_parser_direct_declarator (parser, dcl_kind,
18774 ctor_dtor_or_conv_p,
18775 member_p, friend_p);
18778 if (gnu_attributes && declarator && declarator != cp_error_declarator)
18779 declarator->attributes = gnu_attributes;
18780 return declarator;
18783 /* Parse a direct-declarator or direct-abstract-declarator.
18785 direct-declarator:
18786 declarator-id
18787 direct-declarator ( parameter-declaration-clause )
18788 cv-qualifier-seq [opt]
18789 ref-qualifier [opt]
18790 exception-specification [opt]
18791 direct-declarator [ constant-expression [opt] ]
18792 ( declarator )
18794 direct-abstract-declarator:
18795 direct-abstract-declarator [opt]
18796 ( parameter-declaration-clause )
18797 cv-qualifier-seq [opt]
18798 ref-qualifier [opt]
18799 exception-specification [opt]
18800 direct-abstract-declarator [opt] [ constant-expression [opt] ]
18801 ( abstract-declarator )
18803 Returns a representation of the declarator. DCL_KIND is
18804 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
18805 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
18806 we are parsing a direct-declarator. It is
18807 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
18808 of ambiguity we prefer an abstract declarator, as per
18809 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
18810 as for cp_parser_declarator. */
18812 static cp_declarator *
18813 cp_parser_direct_declarator (cp_parser* parser,
18814 cp_parser_declarator_kind dcl_kind,
18815 int* ctor_dtor_or_conv_p,
18816 bool member_p, bool friend_p)
18818 cp_token *token;
18819 cp_declarator *declarator = NULL;
18820 tree scope = NULL_TREE;
18821 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18822 bool saved_in_declarator_p = parser->in_declarator_p;
18823 bool first = true;
18824 tree pushed_scope = NULL_TREE;
18826 while (true)
18828 /* Peek at the next token. */
18829 token = cp_lexer_peek_token (parser->lexer);
18830 if (token->type == CPP_OPEN_PAREN)
18832 /* This is either a parameter-declaration-clause, or a
18833 parenthesized declarator. When we know we are parsing a
18834 named declarator, it must be a parenthesized declarator
18835 if FIRST is true. For instance, `(int)' is a
18836 parameter-declaration-clause, with an omitted
18837 direct-abstract-declarator. But `((*))', is a
18838 parenthesized abstract declarator. Finally, when T is a
18839 template parameter `(T)' is a
18840 parameter-declaration-clause, and not a parenthesized
18841 named declarator.
18843 We first try and parse a parameter-declaration-clause,
18844 and then try a nested declarator (if FIRST is true).
18846 It is not an error for it not to be a
18847 parameter-declaration-clause, even when FIRST is
18848 false. Consider,
18850 int i (int);
18851 int i (3);
18853 The first is the declaration of a function while the
18854 second is the definition of a variable, including its
18855 initializer.
18857 Having seen only the parenthesis, we cannot know which of
18858 these two alternatives should be selected. Even more
18859 complex are examples like:
18861 int i (int (a));
18862 int i (int (3));
18864 The former is a function-declaration; the latter is a
18865 variable initialization.
18867 Thus again, we try a parameter-declaration-clause, and if
18868 that fails, we back out and return. */
18870 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18872 tree params;
18873 bool is_declarator = false;
18875 /* In a member-declarator, the only valid interpretation
18876 of a parenthesis is the start of a
18877 parameter-declaration-clause. (It is invalid to
18878 initialize a static data member with a parenthesized
18879 initializer; only the "=" form of initialization is
18880 permitted.) */
18881 if (!member_p)
18882 cp_parser_parse_tentatively (parser);
18884 /* Consume the `('. */
18885 cp_lexer_consume_token (parser->lexer);
18886 if (first)
18888 /* If this is going to be an abstract declarator, we're
18889 in a declarator and we can't have default args. */
18890 parser->default_arg_ok_p = false;
18891 parser->in_declarator_p = true;
18894 begin_scope (sk_function_parms, NULL_TREE);
18896 /* Parse the parameter-declaration-clause. */
18897 params = cp_parser_parameter_declaration_clause (parser);
18899 /* Consume the `)'. */
18900 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18902 /* If all went well, parse the cv-qualifier-seq,
18903 ref-qualifier and the exception-specification. */
18904 if (member_p || cp_parser_parse_definitely (parser))
18906 cp_cv_quals cv_quals;
18907 cp_virt_specifiers virt_specifiers;
18908 cp_ref_qualifier ref_qual;
18909 tree exception_specification;
18910 tree late_return;
18911 tree attrs;
18912 bool memfn = (member_p || (pushed_scope
18913 && CLASS_TYPE_P (pushed_scope)));
18915 is_declarator = true;
18917 if (ctor_dtor_or_conv_p)
18918 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
18919 first = false;
18921 /* Parse the cv-qualifier-seq. */
18922 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18923 /* Parse the ref-qualifier. */
18924 ref_qual = cp_parser_ref_qualifier_opt (parser);
18925 /* Parse the tx-qualifier. */
18926 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
18927 /* And the exception-specification. */
18928 exception_specification
18929 = cp_parser_exception_specification_opt (parser);
18931 attrs = cp_parser_std_attribute_spec_seq (parser);
18933 /* In here, we handle cases where attribute is used after
18934 the function declaration. For example:
18935 void func (int x) __attribute__((vector(..))); */
18936 tree gnu_attrs = NULL_TREE;
18937 if (flag_cilkplus
18938 && cp_next_tokens_can_be_gnu_attribute_p (parser))
18940 cp_parser_parse_tentatively (parser);
18941 tree attr = cp_parser_gnu_attributes_opt (parser);
18942 if (cp_lexer_next_token_is_not (parser->lexer,
18943 CPP_SEMICOLON)
18944 && cp_lexer_next_token_is_not (parser->lexer,
18945 CPP_OPEN_BRACE))
18946 cp_parser_abort_tentative_parse (parser);
18947 else if (!cp_parser_parse_definitely (parser))
18949 else
18950 gnu_attrs = attr;
18952 tree requires_clause = NULL_TREE;
18953 late_return = (cp_parser_late_return_type_opt
18954 (parser, declarator, requires_clause,
18955 memfn ? cv_quals : -1));
18957 /* Parse the virt-specifier-seq. */
18958 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18960 /* Create the function-declarator. */
18961 declarator = make_call_declarator (declarator,
18962 params,
18963 cv_quals,
18964 virt_specifiers,
18965 ref_qual,
18966 tx_qual,
18967 exception_specification,
18968 late_return,
18969 requires_clause);
18970 declarator->std_attributes = attrs;
18971 declarator->attributes = gnu_attrs;
18972 /* Any subsequent parameter lists are to do with
18973 return type, so are not those of the declared
18974 function. */
18975 parser->default_arg_ok_p = false;
18978 /* Remove the function parms from scope. */
18979 pop_bindings_and_leave_scope ();
18981 if (is_declarator)
18982 /* Repeat the main loop. */
18983 continue;
18986 /* If this is the first, we can try a parenthesized
18987 declarator. */
18988 if (first)
18990 bool saved_in_type_id_in_expr_p;
18992 parser->default_arg_ok_p = saved_default_arg_ok_p;
18993 parser->in_declarator_p = saved_in_declarator_p;
18995 /* Consume the `('. */
18996 cp_lexer_consume_token (parser->lexer);
18997 /* Parse the nested declarator. */
18998 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18999 parser->in_type_id_in_expr_p = true;
19000 declarator
19001 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
19002 /*parenthesized_p=*/NULL,
19003 member_p, friend_p);
19004 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19005 first = false;
19006 /* Expect a `)'. */
19007 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
19008 declarator = cp_error_declarator;
19009 if (declarator == cp_error_declarator)
19010 break;
19012 goto handle_declarator;
19014 /* Otherwise, we must be done. */
19015 else
19016 break;
19018 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19019 && token->type == CPP_OPEN_SQUARE
19020 && !cp_next_tokens_can_be_attribute_p (parser))
19022 /* Parse an array-declarator. */
19023 tree bounds, attrs;
19025 if (ctor_dtor_or_conv_p)
19026 *ctor_dtor_or_conv_p = 0;
19028 first = false;
19029 parser->default_arg_ok_p = false;
19030 parser->in_declarator_p = true;
19031 /* Consume the `['. */
19032 cp_lexer_consume_token (parser->lexer);
19033 /* Peek at the next token. */
19034 token = cp_lexer_peek_token (parser->lexer);
19035 /* If the next token is `]', then there is no
19036 constant-expression. */
19037 if (token->type != CPP_CLOSE_SQUARE)
19039 bool non_constant_p;
19040 bounds
19041 = cp_parser_constant_expression (parser,
19042 /*allow_non_constant=*/true,
19043 &non_constant_p);
19044 if (!non_constant_p)
19045 /* OK */;
19046 else if (error_operand_p (bounds))
19047 /* Already gave an error. */;
19048 else if (!parser->in_function_body
19049 || current_binding_level->kind == sk_function_parms)
19051 /* Normally, the array bound must be an integral constant
19052 expression. However, as an extension, we allow VLAs
19053 in function scopes as long as they aren't part of a
19054 parameter declaration. */
19055 cp_parser_error (parser,
19056 "array bound is not an integer constant");
19057 bounds = error_mark_node;
19059 else if (processing_template_decl
19060 && !type_dependent_expression_p (bounds))
19062 /* Remember this wasn't a constant-expression. */
19063 bounds = build_nop (TREE_TYPE (bounds), bounds);
19064 TREE_SIDE_EFFECTS (bounds) = 1;
19067 else
19068 bounds = NULL_TREE;
19069 /* Look for the closing `]'. */
19070 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
19072 declarator = cp_error_declarator;
19073 break;
19076 attrs = cp_parser_std_attribute_spec_seq (parser);
19077 declarator = make_array_declarator (declarator, bounds);
19078 declarator->std_attributes = attrs;
19080 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
19083 tree qualifying_scope;
19084 tree unqualified_name;
19085 tree attrs;
19086 special_function_kind sfk;
19087 bool abstract_ok;
19088 bool pack_expansion_p = false;
19089 cp_token *declarator_id_start_token;
19091 /* Parse a declarator-id */
19092 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
19093 if (abstract_ok)
19095 cp_parser_parse_tentatively (parser);
19097 /* If we see an ellipsis, we should be looking at a
19098 parameter pack. */
19099 if (token->type == CPP_ELLIPSIS)
19101 /* Consume the `...' */
19102 cp_lexer_consume_token (parser->lexer);
19104 pack_expansion_p = true;
19108 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
19109 unqualified_name
19110 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
19111 qualifying_scope = parser->scope;
19112 if (abstract_ok)
19114 bool okay = false;
19116 if (!unqualified_name && pack_expansion_p)
19118 /* Check whether an error occurred. */
19119 okay = !cp_parser_error_occurred (parser);
19121 /* We already consumed the ellipsis to mark a
19122 parameter pack, but we have no way to report it,
19123 so abort the tentative parse. We will be exiting
19124 immediately anyway. */
19125 cp_parser_abort_tentative_parse (parser);
19127 else
19128 okay = cp_parser_parse_definitely (parser);
19130 if (!okay)
19131 unqualified_name = error_mark_node;
19132 else if (unqualified_name
19133 && (qualifying_scope
19134 || (!identifier_p (unqualified_name))))
19136 cp_parser_error (parser, "expected unqualified-id");
19137 unqualified_name = error_mark_node;
19141 if (!unqualified_name)
19142 return NULL;
19143 if (unqualified_name == error_mark_node)
19145 declarator = cp_error_declarator;
19146 pack_expansion_p = false;
19147 declarator->parameter_pack_p = false;
19148 break;
19151 attrs = cp_parser_std_attribute_spec_seq (parser);
19153 if (qualifying_scope && at_namespace_scope_p ()
19154 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
19156 /* In the declaration of a member of a template class
19157 outside of the class itself, the SCOPE will sometimes
19158 be a TYPENAME_TYPE. For example, given:
19160 template <typename T>
19161 int S<T>::R::i = 3;
19163 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
19164 this context, we must resolve S<T>::R to an ordinary
19165 type, rather than a typename type.
19167 The reason we normally avoid resolving TYPENAME_TYPEs
19168 is that a specialization of `S' might render
19169 `S<T>::R' not a type. However, if `S' is
19170 specialized, then this `i' will not be used, so there
19171 is no harm in resolving the types here. */
19172 tree type;
19174 /* Resolve the TYPENAME_TYPE. */
19175 type = resolve_typename_type (qualifying_scope,
19176 /*only_current_p=*/false);
19177 /* If that failed, the declarator is invalid. */
19178 if (TREE_CODE (type) == TYPENAME_TYPE)
19180 if (typedef_variant_p (type))
19181 error_at (declarator_id_start_token->location,
19182 "cannot define member of dependent typedef "
19183 "%qT", type);
19184 else
19185 error_at (declarator_id_start_token->location,
19186 "%<%T::%E%> is not a type",
19187 TYPE_CONTEXT (qualifying_scope),
19188 TYPE_IDENTIFIER (qualifying_scope));
19190 qualifying_scope = type;
19193 sfk = sfk_none;
19195 if (unqualified_name)
19197 tree class_type;
19199 if (qualifying_scope
19200 && CLASS_TYPE_P (qualifying_scope))
19201 class_type = qualifying_scope;
19202 else
19203 class_type = current_class_type;
19205 if (TREE_CODE (unqualified_name) == TYPE_DECL)
19207 tree name_type = TREE_TYPE (unqualified_name);
19208 if (class_type && same_type_p (name_type, class_type))
19210 if (qualifying_scope
19211 && CLASSTYPE_USE_TEMPLATE (name_type))
19213 error_at (declarator_id_start_token->location,
19214 "invalid use of constructor as a template");
19215 inform (declarator_id_start_token->location,
19216 "use %<%T::%D%> instead of %<%T::%D%> to "
19217 "name the constructor in a qualified name",
19218 class_type,
19219 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
19220 class_type, name_type);
19221 declarator = cp_error_declarator;
19222 break;
19224 else
19225 unqualified_name = constructor_name (class_type);
19227 else
19229 /* We do not attempt to print the declarator
19230 here because we do not have enough
19231 information about its original syntactic
19232 form. */
19233 cp_parser_error (parser, "invalid declarator");
19234 declarator = cp_error_declarator;
19235 break;
19239 if (class_type)
19241 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
19242 sfk = sfk_destructor;
19243 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
19244 sfk = sfk_conversion;
19245 else if (/* There's no way to declare a constructor
19246 for an anonymous type, even if the type
19247 got a name for linkage purposes. */
19248 !TYPE_WAS_ANONYMOUS (class_type)
19249 /* Handle correctly (c++/19200):
19251 struct S {
19252 struct T{};
19253 friend void S(T);
19256 and also:
19258 namespace N {
19259 void S();
19262 struct S {
19263 friend void N::S();
19264 }; */
19265 && !(friend_p
19266 && class_type != qualifying_scope)
19267 && constructor_name_p (unqualified_name,
19268 class_type))
19270 unqualified_name = constructor_name (class_type);
19271 sfk = sfk_constructor;
19273 else if (is_overloaded_fn (unqualified_name)
19274 && DECL_CONSTRUCTOR_P (get_first_fn
19275 (unqualified_name)))
19276 sfk = sfk_constructor;
19278 if (ctor_dtor_or_conv_p && sfk != sfk_none)
19279 *ctor_dtor_or_conv_p = -1;
19282 declarator = make_id_declarator (qualifying_scope,
19283 unqualified_name,
19284 sfk);
19285 declarator->std_attributes = attrs;
19286 declarator->id_loc = token->location;
19287 declarator->parameter_pack_p = pack_expansion_p;
19289 if (pack_expansion_p)
19290 maybe_warn_variadic_templates ();
19293 handle_declarator:;
19294 scope = get_scope_of_declarator (declarator);
19295 if (scope)
19297 /* Any names that appear after the declarator-id for a
19298 member are looked up in the containing scope. */
19299 if (at_function_scope_p ())
19301 /* But declarations with qualified-ids can't appear in a
19302 function. */
19303 cp_parser_error (parser, "qualified-id in declaration");
19304 declarator = cp_error_declarator;
19305 break;
19307 pushed_scope = push_scope (scope);
19309 parser->in_declarator_p = true;
19310 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
19311 || (declarator && declarator->kind == cdk_id))
19312 /* Default args are only allowed on function
19313 declarations. */
19314 parser->default_arg_ok_p = saved_default_arg_ok_p;
19315 else
19316 parser->default_arg_ok_p = false;
19318 first = false;
19320 /* We're done. */
19321 else
19322 break;
19325 /* For an abstract declarator, we might wind up with nothing at this
19326 point. That's an error; the declarator is not optional. */
19327 if (!declarator)
19328 cp_parser_error (parser, "expected declarator");
19330 /* If we entered a scope, we must exit it now. */
19331 if (pushed_scope)
19332 pop_scope (pushed_scope);
19334 parser->default_arg_ok_p = saved_default_arg_ok_p;
19335 parser->in_declarator_p = saved_in_declarator_p;
19337 return declarator;
19340 /* Parse a ptr-operator.
19342 ptr-operator:
19343 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
19344 * cv-qualifier-seq [opt]
19346 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
19347 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
19349 GNU Extension:
19351 ptr-operator:
19352 & cv-qualifier-seq [opt]
19354 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
19355 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
19356 an rvalue reference. In the case of a pointer-to-member, *TYPE is
19357 filled in with the TYPE containing the member. *CV_QUALS is
19358 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
19359 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
19360 Note that the tree codes returned by this function have nothing
19361 to do with the types of trees that will be eventually be created
19362 to represent the pointer or reference type being parsed. They are
19363 just constants with suggestive names. */
19364 static enum tree_code
19365 cp_parser_ptr_operator (cp_parser* parser,
19366 tree* type,
19367 cp_cv_quals *cv_quals,
19368 tree *attributes)
19370 enum tree_code code = ERROR_MARK;
19371 cp_token *token;
19372 tree attrs = NULL_TREE;
19374 /* Assume that it's not a pointer-to-member. */
19375 *type = NULL_TREE;
19376 /* And that there are no cv-qualifiers. */
19377 *cv_quals = TYPE_UNQUALIFIED;
19379 /* Peek at the next token. */
19380 token = cp_lexer_peek_token (parser->lexer);
19382 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
19383 if (token->type == CPP_MULT)
19384 code = INDIRECT_REF;
19385 else if (token->type == CPP_AND)
19386 code = ADDR_EXPR;
19387 else if ((cxx_dialect != cxx98) &&
19388 token->type == CPP_AND_AND) /* C++0x only */
19389 code = NON_LVALUE_EXPR;
19391 if (code != ERROR_MARK)
19393 /* Consume the `*', `&' or `&&'. */
19394 cp_lexer_consume_token (parser->lexer);
19396 /* A `*' can be followed by a cv-qualifier-seq, and so can a
19397 `&', if we are allowing GNU extensions. (The only qualifier
19398 that can legally appear after `&' is `restrict', but that is
19399 enforced during semantic analysis. */
19400 if (code == INDIRECT_REF
19401 || cp_parser_allow_gnu_extensions_p (parser))
19402 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19404 attrs = cp_parser_std_attribute_spec_seq (parser);
19405 if (attributes != NULL)
19406 *attributes = attrs;
19408 else
19410 /* Try the pointer-to-member case. */
19411 cp_parser_parse_tentatively (parser);
19412 /* Look for the optional `::' operator. */
19413 cp_parser_global_scope_opt (parser,
19414 /*current_scope_valid_p=*/false);
19415 /* Look for the nested-name specifier. */
19416 token = cp_lexer_peek_token (parser->lexer);
19417 cp_parser_nested_name_specifier (parser,
19418 /*typename_keyword_p=*/false,
19419 /*check_dependency_p=*/true,
19420 /*type_p=*/false,
19421 /*is_declaration=*/false);
19422 /* If we found it, and the next token is a `*', then we are
19423 indeed looking at a pointer-to-member operator. */
19424 if (!cp_parser_error_occurred (parser)
19425 && cp_parser_require (parser, CPP_MULT, RT_MULT))
19427 /* Indicate that the `*' operator was used. */
19428 code = INDIRECT_REF;
19430 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
19431 error_at (token->location, "%qD is a namespace", parser->scope);
19432 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
19433 error_at (token->location, "cannot form pointer to member of "
19434 "non-class %q#T", parser->scope);
19435 else
19437 /* The type of which the member is a member is given by the
19438 current SCOPE. */
19439 *type = parser->scope;
19440 /* The next name will not be qualified. */
19441 parser->scope = NULL_TREE;
19442 parser->qualifying_scope = NULL_TREE;
19443 parser->object_scope = NULL_TREE;
19444 /* Look for optional c++11 attributes. */
19445 attrs = cp_parser_std_attribute_spec_seq (parser);
19446 if (attributes != NULL)
19447 *attributes = attrs;
19448 /* Look for the optional cv-qualifier-seq. */
19449 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19452 /* If that didn't work we don't have a ptr-operator. */
19453 if (!cp_parser_parse_definitely (parser))
19454 cp_parser_error (parser, "expected ptr-operator");
19457 return code;
19460 /* Parse an (optional) cv-qualifier-seq.
19462 cv-qualifier-seq:
19463 cv-qualifier cv-qualifier-seq [opt]
19465 cv-qualifier:
19466 const
19467 volatile
19469 GNU Extension:
19471 cv-qualifier:
19472 __restrict__
19474 Returns a bitmask representing the cv-qualifiers. */
19476 static cp_cv_quals
19477 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
19479 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
19481 while (true)
19483 cp_token *token;
19484 cp_cv_quals cv_qualifier;
19486 /* Peek at the next token. */
19487 token = cp_lexer_peek_token (parser->lexer);
19488 /* See if it's a cv-qualifier. */
19489 switch (token->keyword)
19491 case RID_CONST:
19492 cv_qualifier = TYPE_QUAL_CONST;
19493 break;
19495 case RID_VOLATILE:
19496 cv_qualifier = TYPE_QUAL_VOLATILE;
19497 break;
19499 case RID_RESTRICT:
19500 cv_qualifier = TYPE_QUAL_RESTRICT;
19501 break;
19503 default:
19504 cv_qualifier = TYPE_UNQUALIFIED;
19505 break;
19508 if (!cv_qualifier)
19509 break;
19511 if (cv_quals & cv_qualifier)
19513 error_at (token->location, "duplicate cv-qualifier");
19514 cp_lexer_purge_token (parser->lexer);
19516 else
19518 cp_lexer_consume_token (parser->lexer);
19519 cv_quals |= cv_qualifier;
19523 return cv_quals;
19526 /* Parse an (optional) ref-qualifier
19528 ref-qualifier:
19532 Returns cp_ref_qualifier representing ref-qualifier. */
19534 static cp_ref_qualifier
19535 cp_parser_ref_qualifier_opt (cp_parser* parser)
19537 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
19539 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
19540 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
19541 return ref_qual;
19543 while (true)
19545 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
19546 cp_token *token = cp_lexer_peek_token (parser->lexer);
19548 switch (token->type)
19550 case CPP_AND:
19551 curr_ref_qual = REF_QUAL_LVALUE;
19552 break;
19554 case CPP_AND_AND:
19555 curr_ref_qual = REF_QUAL_RVALUE;
19556 break;
19558 default:
19559 curr_ref_qual = REF_QUAL_NONE;
19560 break;
19563 if (!curr_ref_qual)
19564 break;
19565 else if (ref_qual)
19567 error_at (token->location, "multiple ref-qualifiers");
19568 cp_lexer_purge_token (parser->lexer);
19570 else
19572 ref_qual = curr_ref_qual;
19573 cp_lexer_consume_token (parser->lexer);
19577 return ref_qual;
19580 /* Parse an optional tx-qualifier.
19582 tx-qualifier:
19583 transaction_safe
19584 transaction_safe_dynamic */
19586 static tree
19587 cp_parser_tx_qualifier_opt (cp_parser *parser)
19589 cp_token *token = cp_lexer_peek_token (parser->lexer);
19590 if (token->type == CPP_NAME)
19592 tree name = token->u.value;
19593 const char *p = IDENTIFIER_POINTER (name);
19594 const int len = strlen ("transaction_safe");
19595 if (!strncmp (p, "transaction_safe", len))
19597 p += len;
19598 if (*p == '\0'
19599 || !strcmp (p, "_dynamic"))
19601 cp_lexer_consume_token (parser->lexer);
19602 if (!flag_tm)
19604 error ("%E requires %<-fgnu-tm%>", name);
19605 return NULL_TREE;
19607 else
19608 return name;
19612 return NULL_TREE;
19615 /* Parse an (optional) virt-specifier-seq.
19617 virt-specifier-seq:
19618 virt-specifier virt-specifier-seq [opt]
19620 virt-specifier:
19621 override
19622 final
19624 Returns a bitmask representing the virt-specifiers. */
19626 static cp_virt_specifiers
19627 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
19629 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19631 while (true)
19633 cp_token *token;
19634 cp_virt_specifiers virt_specifier;
19636 /* Peek at the next token. */
19637 token = cp_lexer_peek_token (parser->lexer);
19638 /* See if it's a virt-specifier-qualifier. */
19639 if (token->type != CPP_NAME)
19640 break;
19641 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
19643 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19644 virt_specifier = VIRT_SPEC_OVERRIDE;
19646 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
19648 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19649 virt_specifier = VIRT_SPEC_FINAL;
19651 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
19653 virt_specifier = VIRT_SPEC_FINAL;
19655 else
19656 break;
19658 if (virt_specifiers & virt_specifier)
19660 error_at (token->location, "duplicate virt-specifier");
19661 cp_lexer_purge_token (parser->lexer);
19663 else
19665 cp_lexer_consume_token (parser->lexer);
19666 virt_specifiers |= virt_specifier;
19669 return virt_specifiers;
19672 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
19673 is in scope even though it isn't real. */
19675 void
19676 inject_this_parameter (tree ctype, cp_cv_quals quals)
19678 tree this_parm;
19680 if (current_class_ptr)
19682 /* We don't clear this between NSDMIs. Is it already what we want? */
19683 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
19684 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
19685 && cp_type_quals (type) == quals)
19686 return;
19689 this_parm = build_this_parm (ctype, quals);
19690 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
19691 current_class_ptr = NULL_TREE;
19692 current_class_ref
19693 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
19694 current_class_ptr = this_parm;
19697 /* Return true iff our current scope is a non-static data member
19698 initializer. */
19700 bool
19701 parsing_nsdmi (void)
19703 /* We recognize NSDMI context by the context-less 'this' pointer set up
19704 by the function above. */
19705 if (current_class_ptr
19706 && TREE_CODE (current_class_ptr) == PARM_DECL
19707 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
19708 return true;
19709 return false;
19712 /* Parse a late-specified return type, if any. This is not a separate
19713 non-terminal, but part of a function declarator, which looks like
19715 -> trailing-type-specifier-seq abstract-declarator(opt)
19717 Returns the type indicated by the type-id.
19719 In addition to this, parse any queued up omp declare simd
19720 clauses and Cilk Plus SIMD-enabled function's vector attributes.
19722 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
19723 function. */
19725 static tree
19726 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
19727 tree& requires_clause, cp_cv_quals quals)
19729 cp_token *token;
19730 tree type = NULL_TREE;
19731 bool declare_simd_p = (parser->omp_declare_simd
19732 && declarator
19733 && declarator->kind == cdk_id);
19735 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
19736 && declarator && declarator->kind == cdk_id);
19738 bool oacc_routine_p = (parser->oacc_routine
19739 && declarator
19740 && declarator->kind == cdk_id);
19742 /* Peek at the next token. */
19743 token = cp_lexer_peek_token (parser->lexer);
19744 /* A late-specified return type is indicated by an initial '->'. */
19745 if (token->type != CPP_DEREF
19746 && token->keyword != RID_REQUIRES
19747 && !(token->type == CPP_NAME
19748 && token->u.value == ridpointers[RID_REQUIRES])
19749 && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
19750 return NULL_TREE;
19752 tree save_ccp = current_class_ptr;
19753 tree save_ccr = current_class_ref;
19754 if (quals >= 0)
19756 /* DR 1207: 'this' is in scope in the trailing return type. */
19757 inject_this_parameter (current_class_type, quals);
19760 if (token->type == CPP_DEREF)
19762 /* Consume the ->. */
19763 cp_lexer_consume_token (parser->lexer);
19765 type = cp_parser_trailing_type_id (parser);
19768 /* Function declarations may be followed by a trailing
19769 requires-clause. */
19770 requires_clause = cp_parser_requires_clause_opt (parser);
19772 if (cilk_simd_fn_vector_p)
19773 declarator->attributes
19774 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
19775 declarator->attributes);
19776 if (declare_simd_p)
19777 declarator->attributes
19778 = cp_parser_late_parsing_omp_declare_simd (parser,
19779 declarator->attributes);
19780 if (oacc_routine_p)
19781 declarator->attributes
19782 = cp_parser_late_parsing_oacc_routine (parser,
19783 declarator->attributes);
19785 if (quals >= 0)
19787 current_class_ptr = save_ccp;
19788 current_class_ref = save_ccr;
19791 return type;
19794 /* Parse a declarator-id.
19796 declarator-id:
19797 id-expression
19798 :: [opt] nested-name-specifier [opt] type-name
19800 In the `id-expression' case, the value returned is as for
19801 cp_parser_id_expression if the id-expression was an unqualified-id.
19802 If the id-expression was a qualified-id, then a SCOPE_REF is
19803 returned. The first operand is the scope (either a NAMESPACE_DECL
19804 or TREE_TYPE), but the second is still just a representation of an
19805 unqualified-id. */
19807 static tree
19808 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
19810 tree id;
19811 /* The expression must be an id-expression. Assume that qualified
19812 names are the names of types so that:
19814 template <class T>
19815 int S<T>::R::i = 3;
19817 will work; we must treat `S<T>::R' as the name of a type.
19818 Similarly, assume that qualified names are templates, where
19819 required, so that:
19821 template <class T>
19822 int S<T>::R<T>::i = 3;
19824 will work, too. */
19825 id = cp_parser_id_expression (parser,
19826 /*template_keyword_p=*/false,
19827 /*check_dependency_p=*/false,
19828 /*template_p=*/NULL,
19829 /*declarator_p=*/true,
19830 optional_p);
19831 if (id && BASELINK_P (id))
19832 id = BASELINK_FUNCTIONS (id);
19833 return id;
19836 /* Parse a type-id.
19838 type-id:
19839 type-specifier-seq abstract-declarator [opt]
19841 Returns the TYPE specified. */
19843 static tree
19844 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
19845 bool is_trailing_return)
19847 cp_decl_specifier_seq type_specifier_seq;
19848 cp_declarator *abstract_declarator;
19850 /* Parse the type-specifier-seq. */
19851 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
19852 is_trailing_return,
19853 &type_specifier_seq);
19854 if (type_specifier_seq.type == error_mark_node)
19855 return error_mark_node;
19857 /* There might or might not be an abstract declarator. */
19858 cp_parser_parse_tentatively (parser);
19859 /* Look for the declarator. */
19860 abstract_declarator
19861 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
19862 /*parenthesized_p=*/NULL,
19863 /*member_p=*/false,
19864 /*friend_p=*/false);
19865 /* Check to see if there really was a declarator. */
19866 if (!cp_parser_parse_definitely (parser))
19867 abstract_declarator = NULL;
19869 if (type_specifier_seq.type
19870 /* The concepts TS allows 'auto' as a type-id. */
19871 && (!flag_concepts || parser->in_type_id_in_expr_p)
19872 /* None of the valid uses of 'auto' in C++14 involve the type-id
19873 nonterminal, but it is valid in a trailing-return-type. */
19874 && !(cxx_dialect >= cxx14 && is_trailing_return)
19875 && type_uses_auto (type_specifier_seq.type))
19877 /* A type-id with type 'auto' is only ok if the abstract declarator
19878 is a function declarator with a late-specified return type.
19880 A type-id with 'auto' is also valid in a trailing-return-type
19881 in a compound-requirement. */
19882 if (abstract_declarator
19883 && abstract_declarator->kind == cdk_function
19884 && abstract_declarator->u.function.late_return_type)
19885 /* OK */;
19886 else if (parser->in_result_type_constraint_p)
19887 /* OK */;
19888 else
19890 error ("invalid use of %<auto%>");
19891 return error_mark_node;
19895 return groktypename (&type_specifier_seq, abstract_declarator,
19896 is_template_arg);
19899 static tree
19900 cp_parser_type_id (cp_parser *parser)
19902 return cp_parser_type_id_1 (parser, false, false);
19905 static tree
19906 cp_parser_template_type_arg (cp_parser *parser)
19908 tree r;
19909 const char *saved_message = parser->type_definition_forbidden_message;
19910 parser->type_definition_forbidden_message
19911 = G_("types may not be defined in template arguments");
19912 r = cp_parser_type_id_1 (parser, true, false);
19913 parser->type_definition_forbidden_message = saved_message;
19914 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
19916 error ("invalid use of %<auto%> in template argument");
19917 r = error_mark_node;
19919 return r;
19922 static tree
19923 cp_parser_trailing_type_id (cp_parser *parser)
19925 return cp_parser_type_id_1 (parser, false, true);
19928 /* Parse a type-specifier-seq.
19930 type-specifier-seq:
19931 type-specifier type-specifier-seq [opt]
19933 GNU extension:
19935 type-specifier-seq:
19936 attributes type-specifier-seq [opt]
19938 If IS_DECLARATION is true, we are at the start of a "condition" or
19939 exception-declaration, so we might be followed by a declarator-id.
19941 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
19942 i.e. we've just seen "->".
19944 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
19946 static void
19947 cp_parser_type_specifier_seq (cp_parser* parser,
19948 bool is_declaration,
19949 bool is_trailing_return,
19950 cp_decl_specifier_seq *type_specifier_seq)
19952 bool seen_type_specifier = false;
19953 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
19954 cp_token *start_token = NULL;
19956 /* Clear the TYPE_SPECIFIER_SEQ. */
19957 clear_decl_specs (type_specifier_seq);
19959 /* In the context of a trailing return type, enum E { } is an
19960 elaborated-type-specifier followed by a function-body, not an
19961 enum-specifier. */
19962 if (is_trailing_return)
19963 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
19965 /* Parse the type-specifiers and attributes. */
19966 while (true)
19968 tree type_specifier;
19969 bool is_cv_qualifier;
19971 /* Check for attributes first. */
19972 if (cp_next_tokens_can_be_attribute_p (parser))
19974 type_specifier_seq->attributes =
19975 chainon (type_specifier_seq->attributes,
19976 cp_parser_attributes_opt (parser));
19977 continue;
19980 /* record the token of the beginning of the type specifier seq,
19981 for error reporting purposes*/
19982 if (!start_token)
19983 start_token = cp_lexer_peek_token (parser->lexer);
19985 /* Look for the type-specifier. */
19986 type_specifier = cp_parser_type_specifier (parser,
19987 flags,
19988 type_specifier_seq,
19989 /*is_declaration=*/false,
19990 NULL,
19991 &is_cv_qualifier);
19992 if (!type_specifier)
19994 /* If the first type-specifier could not be found, this is not a
19995 type-specifier-seq at all. */
19996 if (!seen_type_specifier)
19998 /* Set in_declarator_p to avoid skipping to the semicolon. */
19999 int in_decl = parser->in_declarator_p;
20000 parser->in_declarator_p = true;
20002 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
20003 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
20004 cp_parser_error (parser, "expected type-specifier");
20006 parser->in_declarator_p = in_decl;
20008 type_specifier_seq->type = error_mark_node;
20009 return;
20011 /* If subsequent type-specifiers could not be found, the
20012 type-specifier-seq is complete. */
20013 break;
20016 seen_type_specifier = true;
20017 /* The standard says that a condition can be:
20019 type-specifier-seq declarator = assignment-expression
20021 However, given:
20023 struct S {};
20024 if (int S = ...)
20026 we should treat the "S" as a declarator, not as a
20027 type-specifier. The standard doesn't say that explicitly for
20028 type-specifier-seq, but it does say that for
20029 decl-specifier-seq in an ordinary declaration. Perhaps it
20030 would be clearer just to allow a decl-specifier-seq here, and
20031 then add a semantic restriction that if any decl-specifiers
20032 that are not type-specifiers appear, the program is invalid. */
20033 if (is_declaration && !is_cv_qualifier)
20034 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
20038 /* Return whether the function currently being declared has an associated
20039 template parameter list. */
20041 static bool
20042 function_being_declared_is_template_p (cp_parser* parser)
20044 if (!current_template_parms || processing_template_parmlist)
20045 return false;
20047 if (parser->implicit_template_scope)
20048 return true;
20050 if (at_class_scope_p ()
20051 && TYPE_BEING_DEFINED (current_class_type))
20052 return parser->num_template_parameter_lists != 0;
20054 return ((int) parser->num_template_parameter_lists > template_class_depth
20055 (current_class_type));
20058 /* Parse a parameter-declaration-clause.
20060 parameter-declaration-clause:
20061 parameter-declaration-list [opt] ... [opt]
20062 parameter-declaration-list , ...
20064 Returns a representation for the parameter declarations. A return
20065 value of NULL indicates a parameter-declaration-clause consisting
20066 only of an ellipsis. */
20068 static tree
20069 cp_parser_parameter_declaration_clause (cp_parser* parser)
20071 tree parameters;
20072 cp_token *token;
20073 bool ellipsis_p;
20074 bool is_error;
20076 struct cleanup {
20077 cp_parser* parser;
20078 int auto_is_implicit_function_template_parm_p;
20079 ~cleanup() {
20080 parser->auto_is_implicit_function_template_parm_p
20081 = auto_is_implicit_function_template_parm_p;
20083 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
20085 (void) cleanup;
20087 if (!processing_specialization
20088 && !processing_template_parmlist
20089 && !processing_explicit_instantiation)
20090 if (!current_function_decl
20091 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
20092 parser->auto_is_implicit_function_template_parm_p = true;
20094 /* Peek at the next token. */
20095 token = cp_lexer_peek_token (parser->lexer);
20096 /* Check for trivial parameter-declaration-clauses. */
20097 if (token->type == CPP_ELLIPSIS)
20099 /* Consume the `...' token. */
20100 cp_lexer_consume_token (parser->lexer);
20101 return NULL_TREE;
20103 else if (token->type == CPP_CLOSE_PAREN)
20104 /* There are no parameters. */
20106 #ifndef NO_IMPLICIT_EXTERN_C
20107 if (in_system_header_at (input_location)
20108 && current_class_type == NULL
20109 && current_lang_name == lang_name_c)
20110 return NULL_TREE;
20111 else
20112 #endif
20113 return void_list_node;
20115 /* Check for `(void)', too, which is a special case. */
20116 else if (token->keyword == RID_VOID
20117 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20118 == CPP_CLOSE_PAREN))
20120 /* Consume the `void' token. */
20121 cp_lexer_consume_token (parser->lexer);
20122 /* There are no parameters. */
20123 return void_list_node;
20126 /* Parse the parameter-declaration-list. */
20127 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
20128 /* If a parse error occurred while parsing the
20129 parameter-declaration-list, then the entire
20130 parameter-declaration-clause is erroneous. */
20131 if (is_error)
20132 return NULL;
20134 /* Peek at the next token. */
20135 token = cp_lexer_peek_token (parser->lexer);
20136 /* If it's a `,', the clause should terminate with an ellipsis. */
20137 if (token->type == CPP_COMMA)
20139 /* Consume the `,'. */
20140 cp_lexer_consume_token (parser->lexer);
20141 /* Expect an ellipsis. */
20142 ellipsis_p
20143 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
20145 /* It might also be `...' if the optional trailing `,' was
20146 omitted. */
20147 else if (token->type == CPP_ELLIPSIS)
20149 /* Consume the `...' token. */
20150 cp_lexer_consume_token (parser->lexer);
20151 /* And remember that we saw it. */
20152 ellipsis_p = true;
20154 else
20155 ellipsis_p = false;
20157 /* Finish the parameter list. */
20158 if (!ellipsis_p)
20159 parameters = chainon (parameters, void_list_node);
20161 return parameters;
20164 /* Parse a parameter-declaration-list.
20166 parameter-declaration-list:
20167 parameter-declaration
20168 parameter-declaration-list , parameter-declaration
20170 Returns a representation of the parameter-declaration-list, as for
20171 cp_parser_parameter_declaration_clause. However, the
20172 `void_list_node' is never appended to the list. Upon return,
20173 *IS_ERROR will be true iff an error occurred. */
20175 static tree
20176 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
20178 tree parameters = NULL_TREE;
20179 tree *tail = &parameters;
20180 bool saved_in_unbraced_linkage_specification_p;
20181 int index = 0;
20183 /* Assume all will go well. */
20184 *is_error = false;
20185 /* The special considerations that apply to a function within an
20186 unbraced linkage specifications do not apply to the parameters
20187 to the function. */
20188 saved_in_unbraced_linkage_specification_p
20189 = parser->in_unbraced_linkage_specification_p;
20190 parser->in_unbraced_linkage_specification_p = false;
20192 /* Look for more parameters. */
20193 while (true)
20195 cp_parameter_declarator *parameter;
20196 tree decl = error_mark_node;
20197 bool parenthesized_p = false;
20198 int template_parm_idx = (function_being_declared_is_template_p (parser)?
20199 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
20200 (current_template_parms)) : 0);
20202 /* Parse the parameter. */
20203 parameter
20204 = cp_parser_parameter_declaration (parser,
20205 /*template_parm_p=*/false,
20206 &parenthesized_p);
20208 /* We don't know yet if the enclosing context is deprecated, so wait
20209 and warn in grokparms if appropriate. */
20210 deprecated_state = DEPRECATED_SUPPRESS;
20212 if (parameter)
20214 /* If a function parameter pack was specified and an implicit template
20215 parameter was introduced during cp_parser_parameter_declaration,
20216 change any implicit parameters introduced into packs. */
20217 if (parser->implicit_template_parms
20218 && parameter->declarator
20219 && parameter->declarator->parameter_pack_p)
20221 int latest_template_parm_idx = TREE_VEC_LENGTH
20222 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
20224 if (latest_template_parm_idx != template_parm_idx)
20225 parameter->decl_specifiers.type = convert_generic_types_to_packs
20226 (parameter->decl_specifiers.type,
20227 template_parm_idx, latest_template_parm_idx);
20230 decl = grokdeclarator (parameter->declarator,
20231 &parameter->decl_specifiers,
20232 PARM,
20233 parameter->default_argument != NULL_TREE,
20234 &parameter->decl_specifiers.attributes);
20237 deprecated_state = DEPRECATED_NORMAL;
20239 /* If a parse error occurred parsing the parameter declaration,
20240 then the entire parameter-declaration-list is erroneous. */
20241 if (decl == error_mark_node)
20243 *is_error = true;
20244 parameters = error_mark_node;
20245 break;
20248 if (parameter->decl_specifiers.attributes)
20249 cplus_decl_attributes (&decl,
20250 parameter->decl_specifiers.attributes,
20252 if (DECL_NAME (decl))
20253 decl = pushdecl (decl);
20255 if (decl != error_mark_node)
20257 retrofit_lang_decl (decl);
20258 DECL_PARM_INDEX (decl) = ++index;
20259 DECL_PARM_LEVEL (decl) = function_parm_depth ();
20262 /* Add the new parameter to the list. */
20263 *tail = build_tree_list (parameter->default_argument, decl);
20264 tail = &TREE_CHAIN (*tail);
20266 /* Peek at the next token. */
20267 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
20268 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
20269 /* These are for Objective-C++ */
20270 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20271 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20272 /* The parameter-declaration-list is complete. */
20273 break;
20274 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20276 cp_token *token;
20278 /* Peek at the next token. */
20279 token = cp_lexer_peek_nth_token (parser->lexer, 2);
20280 /* If it's an ellipsis, then the list is complete. */
20281 if (token->type == CPP_ELLIPSIS)
20282 break;
20283 /* Otherwise, there must be more parameters. Consume the
20284 `,'. */
20285 cp_lexer_consume_token (parser->lexer);
20286 /* When parsing something like:
20288 int i(float f, double d)
20290 we can tell after seeing the declaration for "f" that we
20291 are not looking at an initialization of a variable "i",
20292 but rather at the declaration of a function "i".
20294 Due to the fact that the parsing of template arguments
20295 (as specified to a template-id) requires backtracking we
20296 cannot use this technique when inside a template argument
20297 list. */
20298 if (!parser->in_template_argument_list_p
20299 && !parser->in_type_id_in_expr_p
20300 && cp_parser_uncommitted_to_tentative_parse_p (parser)
20301 /* However, a parameter-declaration of the form
20302 "float(f)" (which is a valid declaration of a
20303 parameter "f") can also be interpreted as an
20304 expression (the conversion of "f" to "float"). */
20305 && !parenthesized_p)
20306 cp_parser_commit_to_tentative_parse (parser);
20308 else
20310 cp_parser_error (parser, "expected %<,%> or %<...%>");
20311 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
20312 cp_parser_skip_to_closing_parenthesis (parser,
20313 /*recovering=*/true,
20314 /*or_comma=*/false,
20315 /*consume_paren=*/false);
20316 break;
20320 parser->in_unbraced_linkage_specification_p
20321 = saved_in_unbraced_linkage_specification_p;
20323 /* Reset implicit_template_scope if we are about to leave the function
20324 parameter list that introduced it. Note that for out-of-line member
20325 definitions, there will be one or more class scopes before we get to
20326 the template parameter scope. */
20328 if (cp_binding_level *its = parser->implicit_template_scope)
20329 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
20331 while (maybe_its->kind == sk_class)
20332 maybe_its = maybe_its->level_chain;
20333 if (maybe_its == its)
20335 parser->implicit_template_parms = 0;
20336 parser->implicit_template_scope = 0;
20340 return parameters;
20343 /* Parse a parameter declaration.
20345 parameter-declaration:
20346 decl-specifier-seq ... [opt] declarator
20347 decl-specifier-seq declarator = assignment-expression
20348 decl-specifier-seq ... [opt] abstract-declarator [opt]
20349 decl-specifier-seq abstract-declarator [opt] = assignment-expression
20351 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
20352 declares a template parameter. (In that case, a non-nested `>'
20353 token encountered during the parsing of the assignment-expression
20354 is not interpreted as a greater-than operator.)
20356 Returns a representation of the parameter, or NULL if an error
20357 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
20358 true iff the declarator is of the form "(p)". */
20360 static cp_parameter_declarator *
20361 cp_parser_parameter_declaration (cp_parser *parser,
20362 bool template_parm_p,
20363 bool *parenthesized_p)
20365 int declares_class_or_enum;
20366 cp_decl_specifier_seq decl_specifiers;
20367 cp_declarator *declarator;
20368 tree default_argument;
20369 cp_token *token = NULL, *declarator_token_start = NULL;
20370 const char *saved_message;
20371 bool template_parameter_pack_p = false;
20373 /* In a template parameter, `>' is not an operator.
20375 [temp.param]
20377 When parsing a default template-argument for a non-type
20378 template-parameter, the first non-nested `>' is taken as the end
20379 of the template parameter-list rather than a greater-than
20380 operator. */
20382 /* Type definitions may not appear in parameter types. */
20383 saved_message = parser->type_definition_forbidden_message;
20384 parser->type_definition_forbidden_message
20385 = G_("types may not be defined in parameter types");
20387 /* Parse the declaration-specifiers. */
20388 cp_parser_decl_specifier_seq (parser,
20389 CP_PARSER_FLAGS_NONE,
20390 &decl_specifiers,
20391 &declares_class_or_enum);
20393 /* Complain about missing 'typename' or other invalid type names. */
20394 if (!decl_specifiers.any_type_specifiers_p
20395 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20396 decl_specifiers.type = error_mark_node;
20398 /* If an error occurred, there's no reason to attempt to parse the
20399 rest of the declaration. */
20400 if (cp_parser_error_occurred (parser))
20402 parser->type_definition_forbidden_message = saved_message;
20403 return NULL;
20406 /* Peek at the next token. */
20407 token = cp_lexer_peek_token (parser->lexer);
20409 /* If the next token is a `)', `,', `=', `>', or `...', then there
20410 is no declarator. However, when variadic templates are enabled,
20411 there may be a declarator following `...'. */
20412 if (token->type == CPP_CLOSE_PAREN
20413 || token->type == CPP_COMMA
20414 || token->type == CPP_EQ
20415 || token->type == CPP_GREATER)
20417 declarator = NULL;
20418 if (parenthesized_p)
20419 *parenthesized_p = false;
20421 /* Otherwise, there should be a declarator. */
20422 else
20424 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20425 parser->default_arg_ok_p = false;
20427 /* After seeing a decl-specifier-seq, if the next token is not a
20428 "(", there is no possibility that the code is a valid
20429 expression. Therefore, if parsing tentatively, we commit at
20430 this point. */
20431 if (!parser->in_template_argument_list_p
20432 /* In an expression context, having seen:
20434 (int((char ...
20436 we cannot be sure whether we are looking at a
20437 function-type (taking a "char" as a parameter) or a cast
20438 of some object of type "char" to "int". */
20439 && !parser->in_type_id_in_expr_p
20440 && cp_parser_uncommitted_to_tentative_parse_p (parser)
20441 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20442 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
20443 cp_parser_commit_to_tentative_parse (parser);
20444 /* Parse the declarator. */
20445 declarator_token_start = token;
20446 declarator = cp_parser_declarator (parser,
20447 CP_PARSER_DECLARATOR_EITHER,
20448 /*ctor_dtor_or_conv_p=*/NULL,
20449 parenthesized_p,
20450 /*member_p=*/false,
20451 /*friend_p=*/false);
20452 parser->default_arg_ok_p = saved_default_arg_ok_p;
20453 /* After the declarator, allow more attributes. */
20454 decl_specifiers.attributes
20455 = chainon (decl_specifiers.attributes,
20456 cp_parser_attributes_opt (parser));
20458 /* If the declarator is a template parameter pack, remember that and
20459 clear the flag in the declarator itself so we don't get errors
20460 from grokdeclarator. */
20461 if (template_parm_p && declarator && declarator->parameter_pack_p)
20463 declarator->parameter_pack_p = false;
20464 template_parameter_pack_p = true;
20468 /* If the next token is an ellipsis, and we have not seen a declarator
20469 name, and if either the type of the declarator contains parameter
20470 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
20471 for, eg, abbreviated integral type names), then we actually have a
20472 parameter pack expansion expression. Otherwise, leave the ellipsis
20473 for a C-style variadic function. */
20474 token = cp_lexer_peek_token (parser->lexer);
20475 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20477 tree type = decl_specifiers.type;
20479 if (type && DECL_P (type))
20480 type = TREE_TYPE (type);
20482 if (((type
20483 && TREE_CODE (type) != TYPE_PACK_EXPANSION
20484 && (template_parm_p || uses_parameter_packs (type)))
20485 || (!type && template_parm_p))
20486 && declarator_can_be_parameter_pack (declarator))
20488 /* Consume the `...'. */
20489 cp_lexer_consume_token (parser->lexer);
20490 maybe_warn_variadic_templates ();
20492 /* Build a pack expansion type */
20493 if (template_parm_p)
20494 template_parameter_pack_p = true;
20495 else if (declarator)
20496 declarator->parameter_pack_p = true;
20497 else
20498 decl_specifiers.type = make_pack_expansion (type);
20502 /* The restriction on defining new types applies only to the type
20503 of the parameter, not to the default argument. */
20504 parser->type_definition_forbidden_message = saved_message;
20506 /* If the next token is `=', then process a default argument. */
20507 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20509 tree type = decl_specifiers.type;
20510 token = cp_lexer_peek_token (parser->lexer);
20511 /* If we are defining a class, then the tokens that make up the
20512 default argument must be saved and processed later. */
20513 if (!template_parm_p && at_class_scope_p ()
20514 && TYPE_BEING_DEFINED (current_class_type)
20515 && !LAMBDA_TYPE_P (current_class_type))
20516 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
20518 // A constrained-type-specifier may declare a type template-parameter.
20519 else if (declares_constrained_type_template_parameter (type))
20520 default_argument
20521 = cp_parser_default_type_template_argument (parser);
20523 // A constrained-type-specifier may declare a template-template-parameter.
20524 else if (declares_constrained_template_template_parameter (type))
20525 default_argument
20526 = cp_parser_default_template_template_argument (parser);
20528 /* Outside of a class definition, we can just parse the
20529 assignment-expression. */
20530 else
20531 default_argument
20532 = cp_parser_default_argument (parser, template_parm_p);
20534 if (!parser->default_arg_ok_p)
20536 permerror (token->location,
20537 "default arguments are only "
20538 "permitted for function parameters");
20540 else if ((declarator && declarator->parameter_pack_p)
20541 || template_parameter_pack_p
20542 || (decl_specifiers.type
20543 && PACK_EXPANSION_P (decl_specifiers.type)))
20545 /* Find the name of the parameter pack. */
20546 cp_declarator *id_declarator = declarator;
20547 while (id_declarator && id_declarator->kind != cdk_id)
20548 id_declarator = id_declarator->declarator;
20550 if (id_declarator && id_declarator->kind == cdk_id)
20551 error_at (declarator_token_start->location,
20552 template_parm_p
20553 ? G_("template parameter pack %qD "
20554 "cannot have a default argument")
20555 : G_("parameter pack %qD cannot have "
20556 "a default argument"),
20557 id_declarator->u.id.unqualified_name);
20558 else
20559 error_at (declarator_token_start->location,
20560 template_parm_p
20561 ? G_("template parameter pack cannot have "
20562 "a default argument")
20563 : G_("parameter pack cannot have a "
20564 "default argument"));
20566 default_argument = NULL_TREE;
20569 else
20570 default_argument = NULL_TREE;
20572 return make_parameter_declarator (&decl_specifiers,
20573 declarator,
20574 default_argument,
20575 template_parameter_pack_p);
20578 /* Parse a default argument and return it.
20580 TEMPLATE_PARM_P is true if this is a default argument for a
20581 non-type template parameter. */
20582 static tree
20583 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
20585 tree default_argument = NULL_TREE;
20586 bool saved_greater_than_is_operator_p;
20587 bool saved_local_variables_forbidden_p;
20588 bool non_constant_p, is_direct_init;
20590 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
20591 set correctly. */
20592 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
20593 parser->greater_than_is_operator_p = !template_parm_p;
20594 /* Local variable names (and the `this' keyword) may not
20595 appear in a default argument. */
20596 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20597 parser->local_variables_forbidden_p = true;
20598 /* Parse the assignment-expression. */
20599 if (template_parm_p)
20600 push_deferring_access_checks (dk_no_deferred);
20601 tree saved_class_ptr = NULL_TREE;
20602 tree saved_class_ref = NULL_TREE;
20603 /* The "this" pointer is not valid in a default argument. */
20604 if (cfun)
20606 saved_class_ptr = current_class_ptr;
20607 cp_function_chain->x_current_class_ptr = NULL_TREE;
20608 saved_class_ref = current_class_ref;
20609 cp_function_chain->x_current_class_ref = NULL_TREE;
20611 default_argument
20612 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
20613 /* Restore the "this" pointer. */
20614 if (cfun)
20616 cp_function_chain->x_current_class_ptr = saved_class_ptr;
20617 cp_function_chain->x_current_class_ref = saved_class_ref;
20619 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
20620 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20621 if (template_parm_p)
20622 pop_deferring_access_checks ();
20623 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
20624 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20626 return default_argument;
20629 /* Parse a function-body.
20631 function-body:
20632 compound_statement */
20634 static void
20635 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
20637 cp_parser_compound_statement (parser, NULL, (in_function_try_block
20638 ? BCS_TRY_BLOCK : BCS_NORMAL),
20639 true);
20642 /* Parse a ctor-initializer-opt followed by a function-body. Return
20643 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
20644 is true we are parsing a function-try-block. */
20646 static bool
20647 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
20648 bool in_function_try_block)
20650 tree body, list;
20651 bool ctor_initializer_p;
20652 const bool check_body_p =
20653 DECL_CONSTRUCTOR_P (current_function_decl)
20654 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
20655 tree last = NULL;
20657 /* Begin the function body. */
20658 body = begin_function_body ();
20659 /* Parse the optional ctor-initializer. */
20660 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
20662 /* If we're parsing a constexpr constructor definition, we need
20663 to check that the constructor body is indeed empty. However,
20664 before we get to cp_parser_function_body lot of junk has been
20665 generated, so we can't just check that we have an empty block.
20666 Rather we take a snapshot of the outermost block, and check whether
20667 cp_parser_function_body changed its state. */
20668 if (check_body_p)
20670 list = cur_stmt_list;
20671 if (STATEMENT_LIST_TAIL (list))
20672 last = STATEMENT_LIST_TAIL (list)->stmt;
20674 /* Parse the function-body. */
20675 cp_parser_function_body (parser, in_function_try_block);
20676 if (check_body_p)
20677 check_constexpr_ctor_body (last, list, /*complain=*/true);
20678 /* Finish the function body. */
20679 finish_function_body (body);
20681 return ctor_initializer_p;
20684 /* Parse an initializer.
20686 initializer:
20687 = initializer-clause
20688 ( expression-list )
20690 Returns an expression representing the initializer. If no
20691 initializer is present, NULL_TREE is returned.
20693 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
20694 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
20695 set to TRUE if there is no initializer present. If there is an
20696 initializer, and it is not a constant-expression, *NON_CONSTANT_P
20697 is set to true; otherwise it is set to false. */
20699 static tree
20700 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
20701 bool* non_constant_p)
20703 cp_token *token;
20704 tree init;
20706 /* Peek at the next token. */
20707 token = cp_lexer_peek_token (parser->lexer);
20709 /* Let our caller know whether or not this initializer was
20710 parenthesized. */
20711 *is_direct_init = (token->type != CPP_EQ);
20712 /* Assume that the initializer is constant. */
20713 *non_constant_p = false;
20715 if (token->type == CPP_EQ)
20717 /* Consume the `='. */
20718 cp_lexer_consume_token (parser->lexer);
20719 /* Parse the initializer-clause. */
20720 init = cp_parser_initializer_clause (parser, non_constant_p);
20722 else if (token->type == CPP_OPEN_PAREN)
20724 vec<tree, va_gc> *vec;
20725 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20726 /*cast_p=*/false,
20727 /*allow_expansion_p=*/true,
20728 non_constant_p);
20729 if (vec == NULL)
20730 return error_mark_node;
20731 init = build_tree_list_vec (vec);
20732 release_tree_vector (vec);
20734 else if (token->type == CPP_OPEN_BRACE)
20736 cp_lexer_set_source_position (parser->lexer);
20737 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20738 init = cp_parser_braced_list (parser, non_constant_p);
20739 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
20741 else
20743 /* Anything else is an error. */
20744 cp_parser_error (parser, "expected initializer");
20745 init = error_mark_node;
20748 return init;
20751 /* Parse an initializer-clause.
20753 initializer-clause:
20754 assignment-expression
20755 braced-init-list
20757 Returns an expression representing the initializer.
20759 If the `assignment-expression' production is used the value
20760 returned is simply a representation for the expression.
20762 Otherwise, calls cp_parser_braced_list. */
20764 static cp_expr
20765 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
20767 cp_expr initializer;
20769 /* Assume the expression is constant. */
20770 *non_constant_p = false;
20772 /* If it is not a `{', then we are looking at an
20773 assignment-expression. */
20774 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
20776 initializer
20777 = cp_parser_constant_expression (parser,
20778 /*allow_non_constant_p=*/true,
20779 non_constant_p);
20781 else
20782 initializer = cp_parser_braced_list (parser, non_constant_p);
20784 return initializer;
20787 /* Parse a brace-enclosed initializer list.
20789 braced-init-list:
20790 { initializer-list , [opt] }
20793 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
20794 the elements of the initializer-list (or NULL, if the last
20795 production is used). The TREE_TYPE for the CONSTRUCTOR will be
20796 NULL_TREE. There is no way to detect whether or not the optional
20797 trailing `,' was provided. NON_CONSTANT_P is as for
20798 cp_parser_initializer. */
20800 static cp_expr
20801 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
20803 tree initializer;
20804 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
20806 /* Consume the `{' token. */
20807 cp_lexer_consume_token (parser->lexer);
20808 /* Create a CONSTRUCTOR to represent the braced-initializer. */
20809 initializer = make_node (CONSTRUCTOR);
20810 /* If it's not a `}', then there is a non-trivial initializer. */
20811 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
20813 /* Parse the initializer list. */
20814 CONSTRUCTOR_ELTS (initializer)
20815 = cp_parser_initializer_list (parser, non_constant_p);
20816 /* A trailing `,' token is allowed. */
20817 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20818 cp_lexer_consume_token (parser->lexer);
20820 else
20821 *non_constant_p = false;
20822 /* Now, there should be a trailing `}'. */
20823 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
20824 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20825 TREE_TYPE (initializer) = init_list_type_node;
20827 cp_expr result (initializer);
20828 /* Build a location of the form:
20829 { ... }
20830 ^~~~~~~
20831 with caret==start at the open brace, finish at the close brace. */
20832 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
20833 result.set_location (combined_loc);
20834 return result;
20837 /* Consume tokens up to, and including, the next non-nested closing `]'.
20838 Returns true iff we found a closing `]'. */
20840 static bool
20841 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
20843 unsigned square_depth = 0;
20845 while (true)
20847 cp_token * token = cp_lexer_peek_token (parser->lexer);
20849 switch (token->type)
20851 case CPP_EOF:
20852 case CPP_PRAGMA_EOL:
20853 /* If we've run out of tokens, then there is no closing `]'. */
20854 return false;
20856 case CPP_OPEN_SQUARE:
20857 ++square_depth;
20858 break;
20860 case CPP_CLOSE_SQUARE:
20861 if (!square_depth--)
20863 cp_lexer_consume_token (parser->lexer);
20864 return true;
20866 break;
20868 default:
20869 break;
20872 /* Consume the token. */
20873 cp_lexer_consume_token (parser->lexer);
20877 /* Return true if we are looking at an array-designator, false otherwise. */
20879 static bool
20880 cp_parser_array_designator_p (cp_parser *parser)
20882 /* Consume the `['. */
20883 cp_lexer_consume_token (parser->lexer);
20885 cp_lexer_save_tokens (parser->lexer);
20887 /* Skip tokens until the next token is a closing square bracket.
20888 If we find the closing `]', and the next token is a `=', then
20889 we are looking at an array designator. */
20890 bool array_designator_p
20891 = (cp_parser_skip_to_closing_square_bracket (parser)
20892 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
20894 /* Roll back the tokens we skipped. */
20895 cp_lexer_rollback_tokens (parser->lexer);
20897 return array_designator_p;
20900 /* Parse an initializer-list.
20902 initializer-list:
20903 initializer-clause ... [opt]
20904 initializer-list , initializer-clause ... [opt]
20906 GNU Extension:
20908 initializer-list:
20909 designation initializer-clause ...[opt]
20910 initializer-list , designation initializer-clause ...[opt]
20912 designation:
20913 . identifier =
20914 identifier :
20915 [ constant-expression ] =
20917 Returns a vec of constructor_elt. The VALUE of each elt is an expression
20918 for the initializer. If the INDEX of the elt is non-NULL, it is the
20919 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
20920 as for cp_parser_initializer. */
20922 static vec<constructor_elt, va_gc> *
20923 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
20925 vec<constructor_elt, va_gc> *v = NULL;
20927 /* Assume all of the expressions are constant. */
20928 *non_constant_p = false;
20930 /* Parse the rest of the list. */
20931 while (true)
20933 cp_token *token;
20934 tree designator;
20935 tree initializer;
20936 bool clause_non_constant_p;
20938 /* If the next token is an identifier and the following one is a
20939 colon, we are looking at the GNU designated-initializer
20940 syntax. */
20941 if (cp_parser_allow_gnu_extensions_p (parser)
20942 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
20943 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
20945 /* Warn the user that they are using an extension. */
20946 pedwarn (input_location, OPT_Wpedantic,
20947 "ISO C++ does not allow designated initializers");
20948 /* Consume the identifier. */
20949 designator = cp_lexer_consume_token (parser->lexer)->u.value;
20950 /* Consume the `:'. */
20951 cp_lexer_consume_token (parser->lexer);
20953 /* Also handle the C99 syntax, '. id ='. */
20954 else if (cp_parser_allow_gnu_extensions_p (parser)
20955 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
20956 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
20957 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
20959 /* Warn the user that they are using an extension. */
20960 pedwarn (input_location, OPT_Wpedantic,
20961 "ISO C++ does not allow C99 designated initializers");
20962 /* Consume the `.'. */
20963 cp_lexer_consume_token (parser->lexer);
20964 /* Consume the identifier. */
20965 designator = cp_lexer_consume_token (parser->lexer)->u.value;
20966 /* Consume the `='. */
20967 cp_lexer_consume_token (parser->lexer);
20969 /* Also handle C99 array designators, '[ const ] ='. */
20970 else if (cp_parser_allow_gnu_extensions_p (parser)
20971 && !c_dialect_objc ()
20972 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
20974 /* In C++11, [ could start a lambda-introducer. */
20975 bool non_const = false;
20977 cp_parser_parse_tentatively (parser);
20979 if (!cp_parser_array_designator_p (parser))
20981 cp_parser_simulate_error (parser);
20982 designator = NULL_TREE;
20984 else
20986 designator = cp_parser_constant_expression (parser, true,
20987 &non_const);
20988 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
20989 cp_parser_require (parser, CPP_EQ, RT_EQ);
20992 if (!cp_parser_parse_definitely (parser))
20993 designator = NULL_TREE;
20994 else if (non_const)
20995 require_potential_rvalue_constant_expression (designator);
20997 else
20998 designator = NULL_TREE;
21000 /* Parse the initializer. */
21001 initializer = cp_parser_initializer_clause (parser,
21002 &clause_non_constant_p);
21003 /* If any clause is non-constant, so is the entire initializer. */
21004 if (clause_non_constant_p)
21005 *non_constant_p = true;
21007 /* If we have an ellipsis, this is an initializer pack
21008 expansion. */
21009 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21011 /* Consume the `...'. */
21012 cp_lexer_consume_token (parser->lexer);
21014 /* Turn the initializer into an initializer expansion. */
21015 initializer = make_pack_expansion (initializer);
21018 /* Add it to the vector. */
21019 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
21021 /* If the next token is not a comma, we have reached the end of
21022 the list. */
21023 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21024 break;
21026 /* Peek at the next token. */
21027 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21028 /* If the next token is a `}', then we're still done. An
21029 initializer-clause can have a trailing `,' after the
21030 initializer-list and before the closing `}'. */
21031 if (token->type == CPP_CLOSE_BRACE)
21032 break;
21034 /* Consume the `,' token. */
21035 cp_lexer_consume_token (parser->lexer);
21038 return v;
21041 /* Classes [gram.class] */
21043 /* Parse a class-name.
21045 class-name:
21046 identifier
21047 template-id
21049 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
21050 to indicate that names looked up in dependent types should be
21051 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
21052 keyword has been used to indicate that the name that appears next
21053 is a template. TAG_TYPE indicates the explicit tag given before
21054 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
21055 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
21056 is the class being defined in a class-head. If ENUM_OK is TRUE,
21057 enum-names are also accepted.
21059 Returns the TYPE_DECL representing the class. */
21061 static tree
21062 cp_parser_class_name (cp_parser *parser,
21063 bool typename_keyword_p,
21064 bool template_keyword_p,
21065 enum tag_types tag_type,
21066 bool check_dependency_p,
21067 bool class_head_p,
21068 bool is_declaration,
21069 bool enum_ok)
21071 tree decl;
21072 tree scope;
21073 bool typename_p;
21074 cp_token *token;
21075 tree identifier = NULL_TREE;
21077 /* All class-names start with an identifier. */
21078 token = cp_lexer_peek_token (parser->lexer);
21079 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
21081 cp_parser_error (parser, "expected class-name");
21082 return error_mark_node;
21085 /* PARSER->SCOPE can be cleared when parsing the template-arguments
21086 to a template-id, so we save it here. */
21087 scope = parser->scope;
21088 if (scope == error_mark_node)
21089 return error_mark_node;
21091 /* Any name names a type if we're following the `typename' keyword
21092 in a qualified name where the enclosing scope is type-dependent. */
21093 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
21094 && dependent_type_p (scope));
21095 /* Handle the common case (an identifier, but not a template-id)
21096 efficiently. */
21097 if (token->type == CPP_NAME
21098 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
21100 cp_token *identifier_token;
21101 bool ambiguous_p;
21103 /* Look for the identifier. */
21104 identifier_token = cp_lexer_peek_token (parser->lexer);
21105 ambiguous_p = identifier_token->error_reported;
21106 identifier = cp_parser_identifier (parser);
21107 /* If the next token isn't an identifier, we are certainly not
21108 looking at a class-name. */
21109 if (identifier == error_mark_node)
21110 decl = error_mark_node;
21111 /* If we know this is a type-name, there's no need to look it
21112 up. */
21113 else if (typename_p)
21114 decl = identifier;
21115 else
21117 tree ambiguous_decls;
21118 /* If we already know that this lookup is ambiguous, then
21119 we've already issued an error message; there's no reason
21120 to check again. */
21121 if (ambiguous_p)
21123 cp_parser_simulate_error (parser);
21124 return error_mark_node;
21126 /* If the next token is a `::', then the name must be a type
21127 name.
21129 [basic.lookup.qual]
21131 During the lookup for a name preceding the :: scope
21132 resolution operator, object, function, and enumerator
21133 names are ignored. */
21134 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21135 tag_type = typename_type;
21136 /* Look up the name. */
21137 decl = cp_parser_lookup_name (parser, identifier,
21138 tag_type,
21139 /*is_template=*/false,
21140 /*is_namespace=*/false,
21141 check_dependency_p,
21142 &ambiguous_decls,
21143 identifier_token->location);
21144 if (ambiguous_decls)
21146 if (cp_parser_parsing_tentatively (parser))
21147 cp_parser_simulate_error (parser);
21148 return error_mark_node;
21152 else
21154 /* Try a template-id. */
21155 decl = cp_parser_template_id (parser, template_keyword_p,
21156 check_dependency_p,
21157 tag_type,
21158 is_declaration);
21159 if (decl == error_mark_node)
21160 return error_mark_node;
21163 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
21165 /* If this is a typename, create a TYPENAME_TYPE. */
21166 if (typename_p && decl != error_mark_node)
21168 decl = make_typename_type (scope, decl, typename_type,
21169 /*complain=*/tf_error);
21170 if (decl != error_mark_node)
21171 decl = TYPE_NAME (decl);
21174 decl = strip_using_decl (decl);
21176 /* Check to see that it is really the name of a class. */
21177 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
21178 && identifier_p (TREE_OPERAND (decl, 0))
21179 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21180 /* Situations like this:
21182 template <typename T> struct A {
21183 typename T::template X<int>::I i;
21186 are problematic. Is `T::template X<int>' a class-name? The
21187 standard does not seem to be definitive, but there is no other
21188 valid interpretation of the following `::'. Therefore, those
21189 names are considered class-names. */
21191 decl = make_typename_type (scope, decl, tag_type, tf_error);
21192 if (decl != error_mark_node)
21193 decl = TYPE_NAME (decl);
21195 else if (TREE_CODE (decl) != TYPE_DECL
21196 || TREE_TYPE (decl) == error_mark_node
21197 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
21198 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
21199 /* In Objective-C 2.0, a classname followed by '.' starts a
21200 dot-syntax expression, and it's not a type-name. */
21201 || (c_dialect_objc ()
21202 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
21203 && objc_is_class_name (decl)))
21204 decl = error_mark_node;
21206 if (decl == error_mark_node)
21207 cp_parser_error (parser, "expected class-name");
21208 else if (identifier && !parser->scope)
21209 maybe_note_name_used_in_class (identifier, decl);
21211 return decl;
21214 /* Parse a class-specifier.
21216 class-specifier:
21217 class-head { member-specification [opt] }
21219 Returns the TREE_TYPE representing the class. */
21221 static tree
21222 cp_parser_class_specifier_1 (cp_parser* parser)
21224 tree type;
21225 tree attributes = NULL_TREE;
21226 bool nested_name_specifier_p;
21227 unsigned saved_num_template_parameter_lists;
21228 bool saved_in_function_body;
21229 unsigned char in_statement;
21230 bool in_switch_statement_p;
21231 bool saved_in_unbraced_linkage_specification_p;
21232 tree old_scope = NULL_TREE;
21233 tree scope = NULL_TREE;
21234 cp_token *closing_brace;
21236 push_deferring_access_checks (dk_no_deferred);
21238 /* Parse the class-head. */
21239 type = cp_parser_class_head (parser,
21240 &nested_name_specifier_p);
21241 /* If the class-head was a semantic disaster, skip the entire body
21242 of the class. */
21243 if (!type)
21245 cp_parser_skip_to_end_of_block_or_statement (parser);
21246 pop_deferring_access_checks ();
21247 return error_mark_node;
21250 /* Look for the `{'. */
21251 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
21253 pop_deferring_access_checks ();
21254 return error_mark_node;
21257 cp_ensure_no_omp_declare_simd (parser);
21258 cp_ensure_no_oacc_routine (parser);
21260 /* Issue an error message if type-definitions are forbidden here. */
21261 cp_parser_check_type_definition (parser);
21262 /* Remember that we are defining one more class. */
21263 ++parser->num_classes_being_defined;
21264 /* Inside the class, surrounding template-parameter-lists do not
21265 apply. */
21266 saved_num_template_parameter_lists
21267 = parser->num_template_parameter_lists;
21268 parser->num_template_parameter_lists = 0;
21269 /* We are not in a function body. */
21270 saved_in_function_body = parser->in_function_body;
21271 parser->in_function_body = false;
21272 /* Or in a loop. */
21273 in_statement = parser->in_statement;
21274 parser->in_statement = 0;
21275 /* Or in a switch. */
21276 in_switch_statement_p = parser->in_switch_statement_p;
21277 parser->in_switch_statement_p = false;
21278 /* We are not immediately inside an extern "lang" block. */
21279 saved_in_unbraced_linkage_specification_p
21280 = parser->in_unbraced_linkage_specification_p;
21281 parser->in_unbraced_linkage_specification_p = false;
21283 // Associate constraints with the type.
21284 if (flag_concepts)
21285 type = associate_classtype_constraints (type);
21287 /* Start the class. */
21288 if (nested_name_specifier_p)
21290 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
21291 old_scope = push_inner_scope (scope);
21293 type = begin_class_definition (type);
21295 if (type == error_mark_node)
21296 /* If the type is erroneous, skip the entire body of the class. */
21297 cp_parser_skip_to_closing_brace (parser);
21298 else
21299 /* Parse the member-specification. */
21300 cp_parser_member_specification_opt (parser);
21302 /* Look for the trailing `}'. */
21303 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21304 /* Look for trailing attributes to apply to this class. */
21305 if (cp_parser_allow_gnu_extensions_p (parser))
21306 attributes = cp_parser_gnu_attributes_opt (parser);
21307 if (type != error_mark_node)
21308 type = finish_struct (type, attributes);
21309 if (nested_name_specifier_p)
21310 pop_inner_scope (old_scope, scope);
21312 /* We've finished a type definition. Check for the common syntax
21313 error of forgetting a semicolon after the definition. We need to
21314 be careful, as we can't just check for not-a-semicolon and be done
21315 with it; the user might have typed:
21317 class X { } c = ...;
21318 class X { } *p = ...;
21320 and so forth. Instead, enumerate all the possible tokens that
21321 might follow this production; if we don't see one of them, then
21322 complain and silently insert the semicolon. */
21324 cp_token *token = cp_lexer_peek_token (parser->lexer);
21325 bool want_semicolon = true;
21327 if (cp_next_tokens_can_be_std_attribute_p (parser))
21328 /* Don't try to parse c++11 attributes here. As per the
21329 grammar, that should be a task for
21330 cp_parser_decl_specifier_seq. */
21331 want_semicolon = false;
21333 switch (token->type)
21335 case CPP_NAME:
21336 case CPP_SEMICOLON:
21337 case CPP_MULT:
21338 case CPP_AND:
21339 case CPP_OPEN_PAREN:
21340 case CPP_CLOSE_PAREN:
21341 case CPP_COMMA:
21342 want_semicolon = false;
21343 break;
21345 /* While it's legal for type qualifiers and storage class
21346 specifiers to follow type definitions in the grammar, only
21347 compiler testsuites contain code like that. Assume that if
21348 we see such code, then what we're really seeing is a case
21349 like:
21351 class X { }
21352 const <type> var = ...;
21356 class Y { }
21357 static <type> func (...) ...
21359 i.e. the qualifier or specifier applies to the next
21360 declaration. To do so, however, we need to look ahead one
21361 more token to see if *that* token is a type specifier.
21363 This code could be improved to handle:
21365 class Z { }
21366 static const <type> var = ...; */
21367 case CPP_KEYWORD:
21368 if (keyword_is_decl_specifier (token->keyword))
21370 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
21372 /* Handling user-defined types here would be nice, but very
21373 tricky. */
21374 want_semicolon
21375 = (lookahead->type == CPP_KEYWORD
21376 && keyword_begins_type_specifier (lookahead->keyword));
21378 break;
21379 default:
21380 break;
21383 /* If we don't have a type, then something is very wrong and we
21384 shouldn't try to do anything clever. Likewise for not seeing the
21385 closing brace. */
21386 if (closing_brace && TYPE_P (type) && want_semicolon)
21388 cp_token_position prev
21389 = cp_lexer_previous_token_position (parser->lexer);
21390 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
21391 location_t loc = prev_token->location;
21393 if (CLASSTYPE_DECLARED_CLASS (type))
21394 error_at (loc, "expected %<;%> after class definition");
21395 else if (TREE_CODE (type) == RECORD_TYPE)
21396 error_at (loc, "expected %<;%> after struct definition");
21397 else if (TREE_CODE (type) == UNION_TYPE)
21398 error_at (loc, "expected %<;%> after union definition");
21399 else
21400 gcc_unreachable ();
21402 /* Unget one token and smash it to look as though we encountered
21403 a semicolon in the input stream. */
21404 cp_lexer_set_token_position (parser->lexer, prev);
21405 token = cp_lexer_peek_token (parser->lexer);
21406 token->type = CPP_SEMICOLON;
21407 token->keyword = RID_MAX;
21411 /* If this class is not itself within the scope of another class,
21412 then we need to parse the bodies of all of the queued function
21413 definitions. Note that the queued functions defined in a class
21414 are not always processed immediately following the
21415 class-specifier for that class. Consider:
21417 struct A {
21418 struct B { void f() { sizeof (A); } };
21421 If `f' were processed before the processing of `A' were
21422 completed, there would be no way to compute the size of `A'.
21423 Note that the nesting we are interested in here is lexical --
21424 not the semantic nesting given by TYPE_CONTEXT. In particular,
21425 for:
21427 struct A { struct B; };
21428 struct A::B { void f() { } };
21430 there is no need to delay the parsing of `A::B::f'. */
21431 if (--parser->num_classes_being_defined == 0)
21433 tree decl;
21434 tree class_type = NULL_TREE;
21435 tree pushed_scope = NULL_TREE;
21436 unsigned ix;
21437 cp_default_arg_entry *e;
21438 tree save_ccp, save_ccr;
21440 /* In a first pass, parse default arguments to the functions.
21441 Then, in a second pass, parse the bodies of the functions.
21442 This two-phased approach handles cases like:
21444 struct S {
21445 void f() { g(); }
21446 void g(int i = 3);
21450 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
21452 decl = e->decl;
21453 /* If there are default arguments that have not yet been processed,
21454 take care of them now. */
21455 if (class_type != e->class_type)
21457 if (pushed_scope)
21458 pop_scope (pushed_scope);
21459 class_type = e->class_type;
21460 pushed_scope = push_scope (class_type);
21462 /* Make sure that any template parameters are in scope. */
21463 maybe_begin_member_template_processing (decl);
21464 /* Parse the default argument expressions. */
21465 cp_parser_late_parsing_default_args (parser, decl);
21466 /* Remove any template parameters from the symbol table. */
21467 maybe_end_member_template_processing ();
21469 vec_safe_truncate (unparsed_funs_with_default_args, 0);
21470 /* Now parse any NSDMIs. */
21471 save_ccp = current_class_ptr;
21472 save_ccr = current_class_ref;
21473 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
21475 if (class_type != DECL_CONTEXT (decl))
21477 if (pushed_scope)
21478 pop_scope (pushed_scope);
21479 class_type = DECL_CONTEXT (decl);
21480 pushed_scope = push_scope (class_type);
21482 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
21483 cp_parser_late_parsing_nsdmi (parser, decl);
21485 vec_safe_truncate (unparsed_nsdmis, 0);
21486 current_class_ptr = save_ccp;
21487 current_class_ref = save_ccr;
21488 if (pushed_scope)
21489 pop_scope (pushed_scope);
21491 /* Now do some post-NSDMI bookkeeping. */
21492 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
21493 after_nsdmi_defaulted_late_checks (class_type);
21494 vec_safe_truncate (unparsed_classes, 0);
21495 after_nsdmi_defaulted_late_checks (type);
21497 /* Now parse the body of the functions. */
21498 if (flag_openmp)
21500 /* OpenMP UDRs need to be parsed before all other functions. */
21501 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21502 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
21503 cp_parser_late_parsing_for_member (parser, decl);
21504 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21505 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
21506 cp_parser_late_parsing_for_member (parser, decl);
21508 else
21509 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21510 cp_parser_late_parsing_for_member (parser, decl);
21511 vec_safe_truncate (unparsed_funs_with_definitions, 0);
21513 else
21514 vec_safe_push (unparsed_classes, type);
21516 /* Put back any saved access checks. */
21517 pop_deferring_access_checks ();
21519 /* Restore saved state. */
21520 parser->in_switch_statement_p = in_switch_statement_p;
21521 parser->in_statement = in_statement;
21522 parser->in_function_body = saved_in_function_body;
21523 parser->num_template_parameter_lists
21524 = saved_num_template_parameter_lists;
21525 parser->in_unbraced_linkage_specification_p
21526 = saved_in_unbraced_linkage_specification_p;
21528 return type;
21531 static tree
21532 cp_parser_class_specifier (cp_parser* parser)
21534 tree ret;
21535 timevar_push (TV_PARSE_STRUCT);
21536 ret = cp_parser_class_specifier_1 (parser);
21537 timevar_pop (TV_PARSE_STRUCT);
21538 return ret;
21541 /* Parse a class-head.
21543 class-head:
21544 class-key identifier [opt] base-clause [opt]
21545 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
21546 class-key nested-name-specifier [opt] template-id
21547 base-clause [opt]
21549 class-virt-specifier:
21550 final
21552 GNU Extensions:
21553 class-key attributes identifier [opt] base-clause [opt]
21554 class-key attributes nested-name-specifier identifier base-clause [opt]
21555 class-key attributes nested-name-specifier [opt] template-id
21556 base-clause [opt]
21558 Upon return BASES is initialized to the list of base classes (or
21559 NULL, if there are none) in the same form returned by
21560 cp_parser_base_clause.
21562 Returns the TYPE of the indicated class. Sets
21563 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
21564 involving a nested-name-specifier was used, and FALSE otherwise.
21566 Returns error_mark_node if this is not a class-head.
21568 Returns NULL_TREE if the class-head is syntactically valid, but
21569 semantically invalid in a way that means we should skip the entire
21570 body of the class. */
21572 static tree
21573 cp_parser_class_head (cp_parser* parser,
21574 bool* nested_name_specifier_p)
21576 tree nested_name_specifier;
21577 enum tag_types class_key;
21578 tree id = NULL_TREE;
21579 tree type = NULL_TREE;
21580 tree attributes;
21581 tree bases;
21582 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21583 bool template_id_p = false;
21584 bool qualified_p = false;
21585 bool invalid_nested_name_p = false;
21586 bool invalid_explicit_specialization_p = false;
21587 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
21588 tree pushed_scope = NULL_TREE;
21589 unsigned num_templates;
21590 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
21591 /* Assume no nested-name-specifier will be present. */
21592 *nested_name_specifier_p = false;
21593 /* Assume no template parameter lists will be used in defining the
21594 type. */
21595 num_templates = 0;
21596 parser->colon_corrects_to_scope_p = false;
21598 /* Look for the class-key. */
21599 class_key = cp_parser_class_key (parser);
21600 if (class_key == none_type)
21601 return error_mark_node;
21603 /* Parse the attributes. */
21604 attributes = cp_parser_attributes_opt (parser);
21606 /* If the next token is `::', that is invalid -- but sometimes
21607 people do try to write:
21609 struct ::S {};
21611 Handle this gracefully by accepting the extra qualifier, and then
21612 issuing an error about it later if this really is a
21613 class-head. If it turns out just to be an elaborated type
21614 specifier, remain silent. */
21615 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
21616 qualified_p = true;
21618 push_deferring_access_checks (dk_no_check);
21620 /* Determine the name of the class. Begin by looking for an
21621 optional nested-name-specifier. */
21622 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
21623 nested_name_specifier
21624 = cp_parser_nested_name_specifier_opt (parser,
21625 /*typename_keyword_p=*/false,
21626 /*check_dependency_p=*/false,
21627 /*type_p=*/true,
21628 /*is_declaration=*/false);
21629 /* If there was a nested-name-specifier, then there *must* be an
21630 identifier. */
21631 if (nested_name_specifier)
21633 type_start_token = cp_lexer_peek_token (parser->lexer);
21634 /* Although the grammar says `identifier', it really means
21635 `class-name' or `template-name'. You are only allowed to
21636 define a class that has already been declared with this
21637 syntax.
21639 The proposed resolution for Core Issue 180 says that wherever
21640 you see `class T::X' you should treat `X' as a type-name.
21642 It is OK to define an inaccessible class; for example:
21644 class A { class B; };
21645 class A::B {};
21647 We do not know if we will see a class-name, or a
21648 template-name. We look for a class-name first, in case the
21649 class-name is a template-id; if we looked for the
21650 template-name first we would stop after the template-name. */
21651 cp_parser_parse_tentatively (parser);
21652 type = cp_parser_class_name (parser,
21653 /*typename_keyword_p=*/false,
21654 /*template_keyword_p=*/false,
21655 class_type,
21656 /*check_dependency_p=*/false,
21657 /*class_head_p=*/true,
21658 /*is_declaration=*/false);
21659 /* If that didn't work, ignore the nested-name-specifier. */
21660 if (!cp_parser_parse_definitely (parser))
21662 invalid_nested_name_p = true;
21663 type_start_token = cp_lexer_peek_token (parser->lexer);
21664 id = cp_parser_identifier (parser);
21665 if (id == error_mark_node)
21666 id = NULL_TREE;
21668 /* If we could not find a corresponding TYPE, treat this
21669 declaration like an unqualified declaration. */
21670 if (type == error_mark_node)
21671 nested_name_specifier = NULL_TREE;
21672 /* Otherwise, count the number of templates used in TYPE and its
21673 containing scopes. */
21674 else
21676 tree scope;
21678 for (scope = TREE_TYPE (type);
21679 scope && TREE_CODE (scope) != NAMESPACE_DECL;
21680 scope = get_containing_scope (scope))
21681 if (TYPE_P (scope)
21682 && CLASS_TYPE_P (scope)
21683 && CLASSTYPE_TEMPLATE_INFO (scope)
21684 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
21685 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
21686 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
21687 ++num_templates;
21690 /* Otherwise, the identifier is optional. */
21691 else
21693 /* We don't know whether what comes next is a template-id,
21694 an identifier, or nothing at all. */
21695 cp_parser_parse_tentatively (parser);
21696 /* Check for a template-id. */
21697 type_start_token = cp_lexer_peek_token (parser->lexer);
21698 id = cp_parser_template_id (parser,
21699 /*template_keyword_p=*/false,
21700 /*check_dependency_p=*/true,
21701 class_key,
21702 /*is_declaration=*/true);
21703 /* If that didn't work, it could still be an identifier. */
21704 if (!cp_parser_parse_definitely (parser))
21706 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21708 type_start_token = cp_lexer_peek_token (parser->lexer);
21709 id = cp_parser_identifier (parser);
21711 else
21712 id = NULL_TREE;
21714 else
21716 template_id_p = true;
21717 ++num_templates;
21721 pop_deferring_access_checks ();
21723 if (id)
21725 cp_parser_check_for_invalid_template_id (parser, id,
21726 class_key,
21727 type_start_token->location);
21729 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
21731 /* If it's not a `:' or a `{' then we can't really be looking at a
21732 class-head, since a class-head only appears as part of a
21733 class-specifier. We have to detect this situation before calling
21734 xref_tag, since that has irreversible side-effects. */
21735 if (!cp_parser_next_token_starts_class_definition_p (parser))
21737 cp_parser_error (parser, "expected %<{%> or %<:%>");
21738 type = error_mark_node;
21739 goto out;
21742 /* At this point, we're going ahead with the class-specifier, even
21743 if some other problem occurs. */
21744 cp_parser_commit_to_tentative_parse (parser);
21745 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
21747 cp_parser_error (parser,
21748 "cannot specify %<override%> for a class");
21749 type = error_mark_node;
21750 goto out;
21752 /* Issue the error about the overly-qualified name now. */
21753 if (qualified_p)
21755 cp_parser_error (parser,
21756 "global qualification of class name is invalid");
21757 type = error_mark_node;
21758 goto out;
21760 else if (invalid_nested_name_p)
21762 cp_parser_error (parser,
21763 "qualified name does not name a class");
21764 type = error_mark_node;
21765 goto out;
21767 else if (nested_name_specifier)
21769 tree scope;
21771 /* Reject typedef-names in class heads. */
21772 if (!DECL_IMPLICIT_TYPEDEF_P (type))
21774 error_at (type_start_token->location,
21775 "invalid class name in declaration of %qD",
21776 type);
21777 type = NULL_TREE;
21778 goto done;
21781 /* Figure out in what scope the declaration is being placed. */
21782 scope = current_scope ();
21783 /* If that scope does not contain the scope in which the
21784 class was originally declared, the program is invalid. */
21785 if (scope && !is_ancestor (scope, nested_name_specifier))
21787 if (at_namespace_scope_p ())
21788 error_at (type_start_token->location,
21789 "declaration of %qD in namespace %qD which does not "
21790 "enclose %qD",
21791 type, scope, nested_name_specifier);
21792 else
21793 error_at (type_start_token->location,
21794 "declaration of %qD in %qD which does not enclose %qD",
21795 type, scope, nested_name_specifier);
21796 type = NULL_TREE;
21797 goto done;
21799 /* [dcl.meaning]
21801 A declarator-id shall not be qualified except for the
21802 definition of a ... nested class outside of its class
21803 ... [or] the definition or explicit instantiation of a
21804 class member of a namespace outside of its namespace. */
21805 if (scope == nested_name_specifier)
21807 permerror (nested_name_specifier_token_start->location,
21808 "extra qualification not allowed");
21809 nested_name_specifier = NULL_TREE;
21810 num_templates = 0;
21813 /* An explicit-specialization must be preceded by "template <>". If
21814 it is not, try to recover gracefully. */
21815 if (at_namespace_scope_p ()
21816 && parser->num_template_parameter_lists == 0
21817 && template_id_p)
21819 error_at (type_start_token->location,
21820 "an explicit specialization must be preceded by %<template <>%>");
21821 invalid_explicit_specialization_p = true;
21822 /* Take the same action that would have been taken by
21823 cp_parser_explicit_specialization. */
21824 ++parser->num_template_parameter_lists;
21825 begin_specialization ();
21827 /* There must be no "return" statements between this point and the
21828 end of this function; set "type "to the correct return value and
21829 use "goto done;" to return. */
21830 /* Make sure that the right number of template parameters were
21831 present. */
21832 if (!cp_parser_check_template_parameters (parser, num_templates,
21833 type_start_token->location,
21834 /*declarator=*/NULL))
21836 /* If something went wrong, there is no point in even trying to
21837 process the class-definition. */
21838 type = NULL_TREE;
21839 goto done;
21842 /* Look up the type. */
21843 if (template_id_p)
21845 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
21846 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
21847 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
21849 error_at (type_start_token->location,
21850 "function template %qD redeclared as a class template", id);
21851 type = error_mark_node;
21853 else
21855 type = TREE_TYPE (id);
21856 type = maybe_process_partial_specialization (type);
21858 if (nested_name_specifier)
21859 pushed_scope = push_scope (nested_name_specifier);
21861 else if (nested_name_specifier)
21863 tree class_type;
21865 /* Given:
21867 template <typename T> struct S { struct T };
21868 template <typename T> struct S<T>::T { };
21870 we will get a TYPENAME_TYPE when processing the definition of
21871 `S::T'. We need to resolve it to the actual type before we
21872 try to define it. */
21873 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
21875 class_type = resolve_typename_type (TREE_TYPE (type),
21876 /*only_current_p=*/false);
21877 if (TREE_CODE (class_type) != TYPENAME_TYPE)
21878 type = TYPE_NAME (class_type);
21879 else
21881 cp_parser_error (parser, "could not resolve typename type");
21882 type = error_mark_node;
21886 if (maybe_process_partial_specialization (TREE_TYPE (type))
21887 == error_mark_node)
21889 type = NULL_TREE;
21890 goto done;
21893 class_type = current_class_type;
21894 /* Enter the scope indicated by the nested-name-specifier. */
21895 pushed_scope = push_scope (nested_name_specifier);
21896 /* Get the canonical version of this type. */
21897 type = TYPE_MAIN_DECL (TREE_TYPE (type));
21898 /* Call push_template_decl if it seems like we should be defining a
21899 template either from the template headers or the type we're
21900 defining, so that we diagnose both extra and missing headers. */
21901 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
21902 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
21903 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
21905 type = push_template_decl (type);
21906 if (type == error_mark_node)
21908 type = NULL_TREE;
21909 goto done;
21913 type = TREE_TYPE (type);
21914 *nested_name_specifier_p = true;
21916 else /* The name is not a nested name. */
21918 /* If the class was unnamed, create a dummy name. */
21919 if (!id)
21920 id = make_anon_name ();
21921 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
21922 parser->num_template_parameter_lists);
21925 /* Indicate whether this class was declared as a `class' or as a
21926 `struct'. */
21927 if (TREE_CODE (type) == RECORD_TYPE)
21928 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
21929 cp_parser_check_class_key (class_key, type);
21931 /* If this type was already complete, and we see another definition,
21932 that's an error. */
21933 if (type != error_mark_node && COMPLETE_TYPE_P (type))
21935 error_at (type_start_token->location, "redefinition of %q#T",
21936 type);
21937 error_at (type_start_token->location, "previous definition of %q+#T",
21938 type);
21939 type = NULL_TREE;
21940 goto done;
21942 else if (type == error_mark_node)
21943 type = NULL_TREE;
21945 if (type)
21947 /* Apply attributes now, before any use of the class as a template
21948 argument in its base list. */
21949 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
21950 fixup_attribute_variants (type);
21953 /* We will have entered the scope containing the class; the names of
21954 base classes should be looked up in that context. For example:
21956 struct A { struct B {}; struct C; };
21957 struct A::C : B {};
21959 is valid. */
21961 /* Get the list of base-classes, if there is one. */
21962 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21964 /* PR59482: enter the class scope so that base-specifiers are looked
21965 up correctly. */
21966 if (type)
21967 pushclass (type);
21968 bases = cp_parser_base_clause (parser);
21969 /* PR59482: get out of the previously pushed class scope so that the
21970 subsequent pops pop the right thing. */
21971 if (type)
21972 popclass ();
21974 else
21975 bases = NULL_TREE;
21977 /* If we're really defining a class, process the base classes.
21978 If they're invalid, fail. */
21979 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
21980 && !xref_basetypes (type, bases))
21981 type = NULL_TREE;
21983 done:
21984 /* Leave the scope given by the nested-name-specifier. We will
21985 enter the class scope itself while processing the members. */
21986 if (pushed_scope)
21987 pop_scope (pushed_scope);
21989 if (invalid_explicit_specialization_p)
21991 end_specialization ();
21992 --parser->num_template_parameter_lists;
21995 if (type)
21996 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
21997 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
21998 CLASSTYPE_FINAL (type) = 1;
21999 out:
22000 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22001 return type;
22004 /* Parse a class-key.
22006 class-key:
22007 class
22008 struct
22009 union
22011 Returns the kind of class-key specified, or none_type to indicate
22012 error. */
22014 static enum tag_types
22015 cp_parser_class_key (cp_parser* parser)
22017 cp_token *token;
22018 enum tag_types tag_type;
22020 /* Look for the class-key. */
22021 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
22022 if (!token)
22023 return none_type;
22025 /* Check to see if the TOKEN is a class-key. */
22026 tag_type = cp_parser_token_is_class_key (token);
22027 if (!tag_type)
22028 cp_parser_error (parser, "expected class-key");
22029 return tag_type;
22032 /* Parse a type-parameter-key.
22034 type-parameter-key:
22035 class
22036 typename
22039 static void
22040 cp_parser_type_parameter_key (cp_parser* parser)
22042 /* Look for the type-parameter-key. */
22043 enum tag_types tag_type = none_type;
22044 cp_token *token = cp_lexer_peek_token (parser->lexer);
22045 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
22047 cp_lexer_consume_token (parser->lexer);
22048 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
22049 /* typename is not allowed in a template template parameter
22050 by the standard until C++1Z. */
22051 pedwarn (token->location, OPT_Wpedantic,
22052 "ISO C++ forbids typename key in template template parameter;"
22053 " use -std=c++1z or -std=gnu++1z");
22055 else
22056 cp_parser_error (parser, "expected %<class%> or %<typename%>");
22058 return;
22061 /* Parse an (optional) member-specification.
22063 member-specification:
22064 member-declaration member-specification [opt]
22065 access-specifier : member-specification [opt] */
22067 static void
22068 cp_parser_member_specification_opt (cp_parser* parser)
22070 while (true)
22072 cp_token *token;
22073 enum rid keyword;
22075 /* Peek at the next token. */
22076 token = cp_lexer_peek_token (parser->lexer);
22077 /* If it's a `}', or EOF then we've seen all the members. */
22078 if (token->type == CPP_CLOSE_BRACE
22079 || token->type == CPP_EOF
22080 || token->type == CPP_PRAGMA_EOL)
22081 break;
22083 /* See if this token is a keyword. */
22084 keyword = token->keyword;
22085 switch (keyword)
22087 case RID_PUBLIC:
22088 case RID_PROTECTED:
22089 case RID_PRIVATE:
22090 /* Consume the access-specifier. */
22091 cp_lexer_consume_token (parser->lexer);
22092 /* Remember which access-specifier is active. */
22093 current_access_specifier = token->u.value;
22094 /* Look for the `:'. */
22095 cp_parser_require (parser, CPP_COLON, RT_COLON);
22096 break;
22098 default:
22099 /* Accept #pragmas at class scope. */
22100 if (token->type == CPP_PRAGMA)
22102 cp_parser_pragma (parser, pragma_member);
22103 break;
22106 /* Otherwise, the next construction must be a
22107 member-declaration. */
22108 cp_parser_member_declaration (parser);
22113 /* Parse a member-declaration.
22115 member-declaration:
22116 decl-specifier-seq [opt] member-declarator-list [opt] ;
22117 function-definition ; [opt]
22118 :: [opt] nested-name-specifier template [opt] unqualified-id ;
22119 using-declaration
22120 template-declaration
22121 alias-declaration
22123 member-declarator-list:
22124 member-declarator
22125 member-declarator-list , member-declarator
22127 member-declarator:
22128 declarator pure-specifier [opt]
22129 declarator constant-initializer [opt]
22130 identifier [opt] : constant-expression
22132 GNU Extensions:
22134 member-declaration:
22135 __extension__ member-declaration
22137 member-declarator:
22138 declarator attributes [opt] pure-specifier [opt]
22139 declarator attributes [opt] constant-initializer [opt]
22140 identifier [opt] attributes [opt] : constant-expression
22142 C++0x Extensions:
22144 member-declaration:
22145 static_assert-declaration */
22147 static void
22148 cp_parser_member_declaration (cp_parser* parser)
22150 cp_decl_specifier_seq decl_specifiers;
22151 tree prefix_attributes;
22152 tree decl;
22153 int declares_class_or_enum;
22154 bool friend_p;
22155 cp_token *token = NULL;
22156 cp_token *decl_spec_token_start = NULL;
22157 cp_token *initializer_token_start = NULL;
22158 int saved_pedantic;
22159 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22161 /* Check for the `__extension__' keyword. */
22162 if (cp_parser_extension_opt (parser, &saved_pedantic))
22164 /* Recurse. */
22165 cp_parser_member_declaration (parser);
22166 /* Restore the old value of the PEDANTIC flag. */
22167 pedantic = saved_pedantic;
22169 return;
22172 /* Check for a template-declaration. */
22173 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22175 /* An explicit specialization here is an error condition, and we
22176 expect the specialization handler to detect and report this. */
22177 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
22178 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
22179 cp_parser_explicit_specialization (parser);
22180 else
22181 cp_parser_template_declaration (parser, /*member_p=*/true);
22183 return;
22185 /* Check for a template introduction. */
22186 else if (cp_parser_template_declaration_after_export (parser, true))
22187 return;
22189 /* Check for a using-declaration. */
22190 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
22192 if (cxx_dialect < cxx11)
22194 /* Parse the using-declaration. */
22195 cp_parser_using_declaration (parser,
22196 /*access_declaration_p=*/false);
22197 return;
22199 else
22201 tree decl;
22202 bool alias_decl_expected;
22203 cp_parser_parse_tentatively (parser);
22204 decl = cp_parser_alias_declaration (parser);
22205 /* Note that if we actually see the '=' token after the
22206 identifier, cp_parser_alias_declaration commits the
22207 tentative parse. In that case, we really expect an
22208 alias-declaration. Otherwise, we expect a using
22209 declaration. */
22210 alias_decl_expected =
22211 !cp_parser_uncommitted_to_tentative_parse_p (parser);
22212 cp_parser_parse_definitely (parser);
22214 if (alias_decl_expected)
22215 finish_member_declaration (decl);
22216 else
22217 cp_parser_using_declaration (parser,
22218 /*access_declaration_p=*/false);
22219 return;
22223 /* Check for @defs. */
22224 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
22226 tree ivar, member;
22227 tree ivar_chains = cp_parser_objc_defs_expression (parser);
22228 ivar = ivar_chains;
22229 while (ivar)
22231 member = ivar;
22232 ivar = TREE_CHAIN (member);
22233 TREE_CHAIN (member) = NULL_TREE;
22234 finish_member_declaration (member);
22236 return;
22239 /* If the next token is `static_assert' we have a static assertion. */
22240 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
22242 cp_parser_static_assert (parser, /*member_p=*/true);
22243 return;
22246 parser->colon_corrects_to_scope_p = false;
22248 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
22249 goto out;
22251 /* Parse the decl-specifier-seq. */
22252 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22253 cp_parser_decl_specifier_seq (parser,
22254 CP_PARSER_FLAGS_OPTIONAL,
22255 &decl_specifiers,
22256 &declares_class_or_enum);
22257 /* Check for an invalid type-name. */
22258 if (!decl_specifiers.any_type_specifiers_p
22259 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22260 goto out;
22261 /* If there is no declarator, then the decl-specifier-seq should
22262 specify a type. */
22263 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22265 /* If there was no decl-specifier-seq, and the next token is a
22266 `;', then we have something like:
22268 struct S { ; };
22270 [class.mem]
22272 Each member-declaration shall declare at least one member
22273 name of the class. */
22274 if (!decl_specifiers.any_specifiers_p)
22276 cp_token *token = cp_lexer_peek_token (parser->lexer);
22277 if (!in_system_header_at (token->location))
22278 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
22280 else
22282 tree type;
22284 /* See if this declaration is a friend. */
22285 friend_p = cp_parser_friend_p (&decl_specifiers);
22286 /* If there were decl-specifiers, check to see if there was
22287 a class-declaration. */
22288 type = check_tag_decl (&decl_specifiers,
22289 /*explicit_type_instantiation_p=*/false);
22290 /* Nested classes have already been added to the class, but
22291 a `friend' needs to be explicitly registered. */
22292 if (friend_p)
22294 /* If the `friend' keyword was present, the friend must
22295 be introduced with a class-key. */
22296 if (!declares_class_or_enum && cxx_dialect < cxx11)
22297 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
22298 "in C++03 a class-key must be used "
22299 "when declaring a friend");
22300 /* In this case:
22302 template <typename T> struct A {
22303 friend struct A<T>::B;
22306 A<T>::B will be represented by a TYPENAME_TYPE, and
22307 therefore not recognized by check_tag_decl. */
22308 if (!type)
22310 type = decl_specifiers.type;
22311 if (type && TREE_CODE (type) == TYPE_DECL)
22312 type = TREE_TYPE (type);
22314 if (!type || !TYPE_P (type))
22315 error_at (decl_spec_token_start->location,
22316 "friend declaration does not name a class or "
22317 "function");
22318 else
22319 make_friend_class (current_class_type, type,
22320 /*complain=*/true);
22322 /* If there is no TYPE, an error message will already have
22323 been issued. */
22324 else if (!type || type == error_mark_node)
22326 /* An anonymous aggregate has to be handled specially; such
22327 a declaration really declares a data member (with a
22328 particular type), as opposed to a nested class. */
22329 else if (ANON_AGGR_TYPE_P (type))
22331 /* C++11 9.5/6. */
22332 if (decl_specifiers.storage_class != sc_none)
22333 error_at (decl_spec_token_start->location,
22334 "a storage class on an anonymous aggregate "
22335 "in class scope is not allowed");
22337 /* Remove constructors and such from TYPE, now that we
22338 know it is an anonymous aggregate. */
22339 fixup_anonymous_aggr (type);
22340 /* And make the corresponding data member. */
22341 decl = build_decl (decl_spec_token_start->location,
22342 FIELD_DECL, NULL_TREE, type);
22343 /* Add it to the class. */
22344 finish_member_declaration (decl);
22346 else
22347 cp_parser_check_access_in_redeclaration
22348 (TYPE_NAME (type),
22349 decl_spec_token_start->location);
22352 else
22354 bool assume_semicolon = false;
22356 /* Clear attributes from the decl_specifiers but keep them
22357 around as prefix attributes that apply them to the entity
22358 being declared. */
22359 prefix_attributes = decl_specifiers.attributes;
22360 decl_specifiers.attributes = NULL_TREE;
22362 /* See if these declarations will be friends. */
22363 friend_p = cp_parser_friend_p (&decl_specifiers);
22365 /* Keep going until we hit the `;' at the end of the
22366 declaration. */
22367 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22369 tree attributes = NULL_TREE;
22370 tree first_attribute;
22372 /* Peek at the next token. */
22373 token = cp_lexer_peek_token (parser->lexer);
22375 /* Check for a bitfield declaration. */
22376 if (token->type == CPP_COLON
22377 || (token->type == CPP_NAME
22378 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
22379 == CPP_COLON))
22381 tree identifier;
22382 tree width;
22384 /* Get the name of the bitfield. Note that we cannot just
22385 check TOKEN here because it may have been invalidated by
22386 the call to cp_lexer_peek_nth_token above. */
22387 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
22388 identifier = cp_parser_identifier (parser);
22389 else
22390 identifier = NULL_TREE;
22392 /* Consume the `:' token. */
22393 cp_lexer_consume_token (parser->lexer);
22394 /* Get the width of the bitfield. */
22395 width
22396 = cp_parser_constant_expression (parser);
22398 /* Look for attributes that apply to the bitfield. */
22399 attributes = cp_parser_attributes_opt (parser);
22400 /* Remember which attributes are prefix attributes and
22401 which are not. */
22402 first_attribute = attributes;
22403 /* Combine the attributes. */
22404 attributes = chainon (prefix_attributes, attributes);
22406 /* Create the bitfield declaration. */
22407 decl = grokbitfield (identifier
22408 ? make_id_declarator (NULL_TREE,
22409 identifier,
22410 sfk_none)
22411 : NULL,
22412 &decl_specifiers,
22413 width,
22414 attributes);
22416 else
22418 cp_declarator *declarator;
22419 tree initializer;
22420 tree asm_specification;
22421 int ctor_dtor_or_conv_p;
22423 /* Parse the declarator. */
22424 declarator
22425 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22426 &ctor_dtor_or_conv_p,
22427 /*parenthesized_p=*/NULL,
22428 /*member_p=*/true,
22429 friend_p);
22431 /* If something went wrong parsing the declarator, make sure
22432 that we at least consume some tokens. */
22433 if (declarator == cp_error_declarator)
22435 /* Skip to the end of the statement. */
22436 cp_parser_skip_to_end_of_statement (parser);
22437 /* If the next token is not a semicolon, that is
22438 probably because we just skipped over the body of
22439 a function. So, we consume a semicolon if
22440 present, but do not issue an error message if it
22441 is not present. */
22442 if (cp_lexer_next_token_is (parser->lexer,
22443 CPP_SEMICOLON))
22444 cp_lexer_consume_token (parser->lexer);
22445 goto out;
22448 if (declares_class_or_enum & 2)
22449 cp_parser_check_for_definition_in_return_type
22450 (declarator, decl_specifiers.type,
22451 decl_specifiers.locations[ds_type_spec]);
22453 /* Look for an asm-specification. */
22454 asm_specification = cp_parser_asm_specification_opt (parser);
22455 /* Look for attributes that apply to the declaration. */
22456 attributes = cp_parser_attributes_opt (parser);
22457 /* Remember which attributes are prefix attributes and
22458 which are not. */
22459 first_attribute = attributes;
22460 /* Combine the attributes. */
22461 attributes = chainon (prefix_attributes, attributes);
22463 /* If it's an `=', then we have a constant-initializer or a
22464 pure-specifier. It is not correct to parse the
22465 initializer before registering the member declaration
22466 since the member declaration should be in scope while
22467 its initializer is processed. However, the rest of the
22468 front end does not yet provide an interface that allows
22469 us to handle this correctly. */
22470 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22472 /* In [class.mem]:
22474 A pure-specifier shall be used only in the declaration of
22475 a virtual function.
22477 A member-declarator can contain a constant-initializer
22478 only if it declares a static member of integral or
22479 enumeration type.
22481 Therefore, if the DECLARATOR is for a function, we look
22482 for a pure-specifier; otherwise, we look for a
22483 constant-initializer. When we call `grokfield', it will
22484 perform more stringent semantics checks. */
22485 initializer_token_start = cp_lexer_peek_token (parser->lexer);
22486 if (function_declarator_p (declarator)
22487 || (decl_specifiers.type
22488 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
22489 && declarator->kind == cdk_id
22490 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
22491 == FUNCTION_TYPE)))
22492 initializer = cp_parser_pure_specifier (parser);
22493 else if (decl_specifiers.storage_class != sc_static)
22494 initializer = cp_parser_save_nsdmi (parser);
22495 else if (cxx_dialect >= cxx11)
22497 bool nonconst;
22498 /* Don't require a constant rvalue in C++11, since we
22499 might want a reference constant. We'll enforce
22500 constancy later. */
22501 cp_lexer_consume_token (parser->lexer);
22502 /* Parse the initializer. */
22503 initializer = cp_parser_initializer_clause (parser,
22504 &nonconst);
22506 else
22507 /* Parse the initializer. */
22508 initializer = cp_parser_constant_initializer (parser);
22510 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
22511 && !function_declarator_p (declarator))
22513 bool x;
22514 if (decl_specifiers.storage_class != sc_static)
22515 initializer = cp_parser_save_nsdmi (parser);
22516 else
22517 initializer = cp_parser_initializer (parser, &x, &x);
22519 /* Otherwise, there is no initializer. */
22520 else
22521 initializer = NULL_TREE;
22523 /* See if we are probably looking at a function
22524 definition. We are certainly not looking at a
22525 member-declarator. Calling `grokfield' has
22526 side-effects, so we must not do it unless we are sure
22527 that we are looking at a member-declarator. */
22528 if (cp_parser_token_starts_function_definition_p
22529 (cp_lexer_peek_token (parser->lexer)))
22531 /* The grammar does not allow a pure-specifier to be
22532 used when a member function is defined. (It is
22533 possible that this fact is an oversight in the
22534 standard, since a pure function may be defined
22535 outside of the class-specifier. */
22536 if (initializer && initializer_token_start)
22537 error_at (initializer_token_start->location,
22538 "pure-specifier on function-definition");
22539 decl = cp_parser_save_member_function_body (parser,
22540 &decl_specifiers,
22541 declarator,
22542 attributes);
22543 if (parser->fully_implicit_function_template_p)
22544 decl = finish_fully_implicit_template (parser, decl);
22545 /* If the member was not a friend, declare it here. */
22546 if (!friend_p)
22547 finish_member_declaration (decl);
22548 /* Peek at the next token. */
22549 token = cp_lexer_peek_token (parser->lexer);
22550 /* If the next token is a semicolon, consume it. */
22551 if (token->type == CPP_SEMICOLON)
22552 cp_lexer_consume_token (parser->lexer);
22553 goto out;
22555 else
22556 if (declarator->kind == cdk_function)
22557 declarator->id_loc = token->location;
22558 /* Create the declaration. */
22559 decl = grokfield (declarator, &decl_specifiers,
22560 initializer, /*init_const_expr_p=*/true,
22561 asm_specification, attributes);
22562 if (parser->fully_implicit_function_template_p)
22564 if (friend_p)
22565 finish_fully_implicit_template (parser, 0);
22566 else
22567 decl = finish_fully_implicit_template (parser, decl);
22571 cp_finalize_omp_declare_simd (parser, decl);
22572 cp_finalize_oacc_routine (parser, decl, false);
22574 /* Reset PREFIX_ATTRIBUTES. */
22575 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22576 attributes = TREE_CHAIN (attributes);
22577 if (attributes)
22578 TREE_CHAIN (attributes) = NULL_TREE;
22580 /* If there is any qualification still in effect, clear it
22581 now; we will be starting fresh with the next declarator. */
22582 parser->scope = NULL_TREE;
22583 parser->qualifying_scope = NULL_TREE;
22584 parser->object_scope = NULL_TREE;
22585 /* If it's a `,', then there are more declarators. */
22586 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22588 cp_lexer_consume_token (parser->lexer);
22589 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22591 cp_token *token = cp_lexer_previous_token (parser->lexer);
22592 error_at (token->location,
22593 "stray %<,%> at end of member declaration");
22596 /* If the next token isn't a `;', then we have a parse error. */
22597 else if (cp_lexer_next_token_is_not (parser->lexer,
22598 CPP_SEMICOLON))
22600 /* The next token might be a ways away from where the
22601 actual semicolon is missing. Find the previous token
22602 and use that for our error position. */
22603 cp_token *token = cp_lexer_previous_token (parser->lexer);
22604 error_at (token->location,
22605 "expected %<;%> at end of member declaration");
22607 /* Assume that the user meant to provide a semicolon. If
22608 we were to cp_parser_skip_to_end_of_statement, we might
22609 skip to a semicolon inside a member function definition
22610 and issue nonsensical error messages. */
22611 assume_semicolon = true;
22614 if (decl)
22616 /* Add DECL to the list of members. */
22617 if (!friend_p
22618 /* Explicitly include, eg, NSDMIs, for better error
22619 recovery (c++/58650). */
22620 || !DECL_DECLARES_FUNCTION_P (decl))
22621 finish_member_declaration (decl);
22623 if (TREE_CODE (decl) == FUNCTION_DECL)
22624 cp_parser_save_default_args (parser, decl);
22625 else if (TREE_CODE (decl) == FIELD_DECL
22626 && !DECL_C_BIT_FIELD (decl)
22627 && DECL_INITIAL (decl))
22628 /* Add DECL to the queue of NSDMI to be parsed later. */
22629 vec_safe_push (unparsed_nsdmis, decl);
22632 if (assume_semicolon)
22633 goto out;
22637 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22638 out:
22639 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22642 /* Parse a pure-specifier.
22644 pure-specifier:
22647 Returns INTEGER_ZERO_NODE if a pure specifier is found.
22648 Otherwise, ERROR_MARK_NODE is returned. */
22650 static tree
22651 cp_parser_pure_specifier (cp_parser* parser)
22653 cp_token *token;
22655 /* Look for the `=' token. */
22656 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
22657 return error_mark_node;
22658 /* Look for the `0' token. */
22659 token = cp_lexer_peek_token (parser->lexer);
22661 if (token->type == CPP_EOF
22662 || token->type == CPP_PRAGMA_EOL)
22663 return error_mark_node;
22665 cp_lexer_consume_token (parser->lexer);
22667 /* Accept = default or = delete in c++0x mode. */
22668 if (token->keyword == RID_DEFAULT
22669 || token->keyword == RID_DELETE)
22671 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
22672 return token->u.value;
22675 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
22676 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
22678 cp_parser_error (parser,
22679 "invalid pure specifier (only %<= 0%> is allowed)");
22680 cp_parser_skip_to_end_of_statement (parser);
22681 return error_mark_node;
22683 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
22685 error_at (token->location, "templates may not be %<virtual%>");
22686 return error_mark_node;
22689 return integer_zero_node;
22692 /* Parse a constant-initializer.
22694 constant-initializer:
22695 = constant-expression
22697 Returns a representation of the constant-expression. */
22699 static tree
22700 cp_parser_constant_initializer (cp_parser* parser)
22702 /* Look for the `=' token. */
22703 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
22704 return error_mark_node;
22706 /* It is invalid to write:
22708 struct S { static const int i = { 7 }; };
22711 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22713 cp_parser_error (parser,
22714 "a brace-enclosed initializer is not allowed here");
22715 /* Consume the opening brace. */
22716 cp_lexer_consume_token (parser->lexer);
22717 /* Skip the initializer. */
22718 cp_parser_skip_to_closing_brace (parser);
22719 /* Look for the trailing `}'. */
22720 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
22722 return error_mark_node;
22725 return cp_parser_constant_expression (parser);
22728 /* Derived classes [gram.class.derived] */
22730 /* Parse a base-clause.
22732 base-clause:
22733 : base-specifier-list
22735 base-specifier-list:
22736 base-specifier ... [opt]
22737 base-specifier-list , base-specifier ... [opt]
22739 Returns a TREE_LIST representing the base-classes, in the order in
22740 which they were declared. The representation of each node is as
22741 described by cp_parser_base_specifier.
22743 In the case that no bases are specified, this function will return
22744 NULL_TREE, not ERROR_MARK_NODE. */
22746 static tree
22747 cp_parser_base_clause (cp_parser* parser)
22749 tree bases = NULL_TREE;
22751 /* Look for the `:' that begins the list. */
22752 cp_parser_require (parser, CPP_COLON, RT_COLON);
22754 /* Scan the base-specifier-list. */
22755 while (true)
22757 cp_token *token;
22758 tree base;
22759 bool pack_expansion_p = false;
22761 /* Look for the base-specifier. */
22762 base = cp_parser_base_specifier (parser);
22763 /* Look for the (optional) ellipsis. */
22764 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22766 /* Consume the `...'. */
22767 cp_lexer_consume_token (parser->lexer);
22769 pack_expansion_p = true;
22772 /* Add BASE to the front of the list. */
22773 if (base && base != error_mark_node)
22775 if (pack_expansion_p)
22776 /* Make this a pack expansion type. */
22777 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
22779 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
22781 TREE_CHAIN (base) = bases;
22782 bases = base;
22785 /* Peek at the next token. */
22786 token = cp_lexer_peek_token (parser->lexer);
22787 /* If it's not a comma, then the list is complete. */
22788 if (token->type != CPP_COMMA)
22789 break;
22790 /* Consume the `,'. */
22791 cp_lexer_consume_token (parser->lexer);
22794 /* PARSER->SCOPE may still be non-NULL at this point, if the last
22795 base class had a qualified name. However, the next name that
22796 appears is certainly not qualified. */
22797 parser->scope = NULL_TREE;
22798 parser->qualifying_scope = NULL_TREE;
22799 parser->object_scope = NULL_TREE;
22801 return nreverse (bases);
22804 /* Parse a base-specifier.
22806 base-specifier:
22807 :: [opt] nested-name-specifier [opt] class-name
22808 virtual access-specifier [opt] :: [opt] nested-name-specifier
22809 [opt] class-name
22810 access-specifier virtual [opt] :: [opt] nested-name-specifier
22811 [opt] class-name
22813 Returns a TREE_LIST. The TREE_PURPOSE will be one of
22814 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
22815 indicate the specifiers provided. The TREE_VALUE will be a TYPE
22816 (or the ERROR_MARK_NODE) indicating the type that was specified. */
22818 static tree
22819 cp_parser_base_specifier (cp_parser* parser)
22821 cp_token *token;
22822 bool done = false;
22823 bool virtual_p = false;
22824 bool duplicate_virtual_error_issued_p = false;
22825 bool duplicate_access_error_issued_p = false;
22826 bool class_scope_p, template_p;
22827 tree access = access_default_node;
22828 tree type;
22830 /* Process the optional `virtual' and `access-specifier'. */
22831 while (!done)
22833 /* Peek at the next token. */
22834 token = cp_lexer_peek_token (parser->lexer);
22835 /* Process `virtual'. */
22836 switch (token->keyword)
22838 case RID_VIRTUAL:
22839 /* If `virtual' appears more than once, issue an error. */
22840 if (virtual_p && !duplicate_virtual_error_issued_p)
22842 cp_parser_error (parser,
22843 "%<virtual%> specified more than once in base-specified");
22844 duplicate_virtual_error_issued_p = true;
22847 virtual_p = true;
22849 /* Consume the `virtual' token. */
22850 cp_lexer_consume_token (parser->lexer);
22852 break;
22854 case RID_PUBLIC:
22855 case RID_PROTECTED:
22856 case RID_PRIVATE:
22857 /* If more than one access specifier appears, issue an
22858 error. */
22859 if (access != access_default_node
22860 && !duplicate_access_error_issued_p)
22862 cp_parser_error (parser,
22863 "more than one access specifier in base-specified");
22864 duplicate_access_error_issued_p = true;
22867 access = ridpointers[(int) token->keyword];
22869 /* Consume the access-specifier. */
22870 cp_lexer_consume_token (parser->lexer);
22872 break;
22874 default:
22875 done = true;
22876 break;
22879 /* It is not uncommon to see programs mechanically, erroneously, use
22880 the 'typename' keyword to denote (dependent) qualified types
22881 as base classes. */
22882 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
22884 token = cp_lexer_peek_token (parser->lexer);
22885 if (!processing_template_decl)
22886 error_at (token->location,
22887 "keyword %<typename%> not allowed outside of templates");
22888 else
22889 error_at (token->location,
22890 "keyword %<typename%> not allowed in this context "
22891 "(the base class is implicitly a type)");
22892 cp_lexer_consume_token (parser->lexer);
22895 /* Look for the optional `::' operator. */
22896 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
22897 /* Look for the nested-name-specifier. The simplest way to
22898 implement:
22900 [temp.res]
22902 The keyword `typename' is not permitted in a base-specifier or
22903 mem-initializer; in these contexts a qualified name that
22904 depends on a template-parameter is implicitly assumed to be a
22905 type name.
22907 is to pretend that we have seen the `typename' keyword at this
22908 point. */
22909 cp_parser_nested_name_specifier_opt (parser,
22910 /*typename_keyword_p=*/true,
22911 /*check_dependency_p=*/true,
22912 typename_type,
22913 /*is_declaration=*/true);
22914 /* If the base class is given by a qualified name, assume that names
22915 we see are type names or templates, as appropriate. */
22916 class_scope_p = (parser->scope && TYPE_P (parser->scope));
22917 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
22919 if (!parser->scope
22920 && cp_lexer_next_token_is_decltype (parser->lexer))
22921 /* DR 950 allows decltype as a base-specifier. */
22922 type = cp_parser_decltype (parser);
22923 else
22925 /* Otherwise, look for the class-name. */
22926 type = cp_parser_class_name (parser,
22927 class_scope_p,
22928 template_p,
22929 typename_type,
22930 /*check_dependency_p=*/true,
22931 /*class_head_p=*/false,
22932 /*is_declaration=*/true);
22933 type = TREE_TYPE (type);
22936 if (type == error_mark_node)
22937 return error_mark_node;
22939 return finish_base_specifier (type, access, virtual_p);
22942 /* Exception handling [gram.exception] */
22944 /* Parse an (optional) noexcept-specification.
22946 noexcept-specification:
22947 noexcept ( constant-expression ) [opt]
22949 If no noexcept-specification is present, returns NULL_TREE.
22950 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
22951 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
22952 there are no parentheses. CONSUMED_EXPR will be set accordingly.
22953 Otherwise, returns a noexcept specification unless RETURN_COND is true,
22954 in which case a boolean condition is returned instead. */
22956 static tree
22957 cp_parser_noexcept_specification_opt (cp_parser* parser,
22958 bool require_constexpr,
22959 bool* consumed_expr,
22960 bool return_cond)
22962 cp_token *token;
22963 const char *saved_message;
22965 /* Peek at the next token. */
22966 token = cp_lexer_peek_token (parser->lexer);
22968 /* Is it a noexcept-specification? */
22969 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
22971 tree expr;
22972 cp_lexer_consume_token (parser->lexer);
22974 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
22976 cp_lexer_consume_token (parser->lexer);
22978 if (require_constexpr)
22980 /* Types may not be defined in an exception-specification. */
22981 saved_message = parser->type_definition_forbidden_message;
22982 parser->type_definition_forbidden_message
22983 = G_("types may not be defined in an exception-specification");
22985 expr = cp_parser_constant_expression (parser);
22987 /* Restore the saved message. */
22988 parser->type_definition_forbidden_message = saved_message;
22990 else
22992 expr = cp_parser_expression (parser);
22993 *consumed_expr = true;
22996 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22998 else
23000 expr = boolean_true_node;
23001 if (!require_constexpr)
23002 *consumed_expr = false;
23005 /* We cannot build a noexcept-spec right away because this will check
23006 that expr is a constexpr. */
23007 if (!return_cond)
23008 return build_noexcept_spec (expr, tf_warning_or_error);
23009 else
23010 return expr;
23012 else
23013 return NULL_TREE;
23016 /* Parse an (optional) exception-specification.
23018 exception-specification:
23019 throw ( type-id-list [opt] )
23021 Returns a TREE_LIST representing the exception-specification. The
23022 TREE_VALUE of each node is a type. */
23024 static tree
23025 cp_parser_exception_specification_opt (cp_parser* parser)
23027 cp_token *token;
23028 tree type_id_list;
23029 const char *saved_message;
23031 /* Peek at the next token. */
23032 token = cp_lexer_peek_token (parser->lexer);
23034 /* Is it a noexcept-specification? */
23035 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
23036 false);
23037 if (type_id_list != NULL_TREE)
23038 return type_id_list;
23040 /* If it's not `throw', then there's no exception-specification. */
23041 if (!cp_parser_is_keyword (token, RID_THROW))
23042 return NULL_TREE;
23044 #if 0
23045 /* Enable this once a lot of code has transitioned to noexcept? */
23046 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
23047 warning (OPT_Wdeprecated, "dynamic exception specifications are "
23048 "deprecated in C++0x; use %<noexcept%> instead");
23049 #endif
23051 /* Consume the `throw'. */
23052 cp_lexer_consume_token (parser->lexer);
23054 /* Look for the `('. */
23055 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23057 /* Peek at the next token. */
23058 token = cp_lexer_peek_token (parser->lexer);
23059 /* If it's not a `)', then there is a type-id-list. */
23060 if (token->type != CPP_CLOSE_PAREN)
23062 /* Types may not be defined in an exception-specification. */
23063 saved_message = parser->type_definition_forbidden_message;
23064 parser->type_definition_forbidden_message
23065 = G_("types may not be defined in an exception-specification");
23066 /* Parse the type-id-list. */
23067 type_id_list = cp_parser_type_id_list (parser);
23068 /* Restore the saved message. */
23069 parser->type_definition_forbidden_message = saved_message;
23071 else
23072 type_id_list = empty_except_spec;
23074 /* Look for the `)'. */
23075 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23077 return type_id_list;
23080 /* Parse an (optional) type-id-list.
23082 type-id-list:
23083 type-id ... [opt]
23084 type-id-list , type-id ... [opt]
23086 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
23087 in the order that the types were presented. */
23089 static tree
23090 cp_parser_type_id_list (cp_parser* parser)
23092 tree types = NULL_TREE;
23094 while (true)
23096 cp_token *token;
23097 tree type;
23099 token = cp_lexer_peek_token (parser->lexer);
23101 /* Get the next type-id. */
23102 type = cp_parser_type_id (parser);
23103 /* Check for invalid 'auto'. */
23104 if (flag_concepts && type_uses_auto (type))
23106 error_at (token->location,
23107 "invalid use of %<auto%> in exception-specification");
23108 type = error_mark_node;
23110 /* Parse the optional ellipsis. */
23111 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23113 /* Consume the `...'. */
23114 cp_lexer_consume_token (parser->lexer);
23116 /* Turn the type into a pack expansion expression. */
23117 type = make_pack_expansion (type);
23119 /* Add it to the list. */
23120 types = add_exception_specifier (types, type, /*complain=*/1);
23121 /* Peek at the next token. */
23122 token = cp_lexer_peek_token (parser->lexer);
23123 /* If it is not a `,', we are done. */
23124 if (token->type != CPP_COMMA)
23125 break;
23126 /* Consume the `,'. */
23127 cp_lexer_consume_token (parser->lexer);
23130 return nreverse (types);
23133 /* Parse a try-block.
23135 try-block:
23136 try compound-statement handler-seq */
23138 static tree
23139 cp_parser_try_block (cp_parser* parser)
23141 tree try_block;
23143 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
23144 if (parser->in_function_body
23145 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
23146 error ("%<try%> in %<constexpr%> function");
23148 try_block = begin_try_block ();
23149 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
23150 finish_try_block (try_block);
23151 cp_parser_handler_seq (parser);
23152 finish_handler_sequence (try_block);
23154 return try_block;
23157 /* Parse a function-try-block.
23159 function-try-block:
23160 try ctor-initializer [opt] function-body handler-seq */
23162 static bool
23163 cp_parser_function_try_block (cp_parser* parser)
23165 tree compound_stmt;
23166 tree try_block;
23167 bool ctor_initializer_p;
23169 /* Look for the `try' keyword. */
23170 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
23171 return false;
23172 /* Let the rest of the front end know where we are. */
23173 try_block = begin_function_try_block (&compound_stmt);
23174 /* Parse the function-body. */
23175 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23176 (parser, /*in_function_try_block=*/true);
23177 /* We're done with the `try' part. */
23178 finish_function_try_block (try_block);
23179 /* Parse the handlers. */
23180 cp_parser_handler_seq (parser);
23181 /* We're done with the handlers. */
23182 finish_function_handler_sequence (try_block, compound_stmt);
23184 return ctor_initializer_p;
23187 /* Parse a handler-seq.
23189 handler-seq:
23190 handler handler-seq [opt] */
23192 static void
23193 cp_parser_handler_seq (cp_parser* parser)
23195 while (true)
23197 cp_token *token;
23199 /* Parse the handler. */
23200 cp_parser_handler (parser);
23201 /* Peek at the next token. */
23202 token = cp_lexer_peek_token (parser->lexer);
23203 /* If it's not `catch' then there are no more handlers. */
23204 if (!cp_parser_is_keyword (token, RID_CATCH))
23205 break;
23209 /* Parse a handler.
23211 handler:
23212 catch ( exception-declaration ) compound-statement */
23214 static void
23215 cp_parser_handler (cp_parser* parser)
23217 tree handler;
23218 tree declaration;
23220 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
23221 handler = begin_handler ();
23222 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23223 declaration = cp_parser_exception_declaration (parser);
23224 finish_handler_parms (declaration, handler);
23225 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23226 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
23227 finish_handler (handler);
23230 /* Parse an exception-declaration.
23232 exception-declaration:
23233 type-specifier-seq declarator
23234 type-specifier-seq abstract-declarator
23235 type-specifier-seq
23238 Returns a VAR_DECL for the declaration, or NULL_TREE if the
23239 ellipsis variant is used. */
23241 static tree
23242 cp_parser_exception_declaration (cp_parser* parser)
23244 cp_decl_specifier_seq type_specifiers;
23245 cp_declarator *declarator;
23246 const char *saved_message;
23248 /* If it's an ellipsis, it's easy to handle. */
23249 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23251 /* Consume the `...' token. */
23252 cp_lexer_consume_token (parser->lexer);
23253 return NULL_TREE;
23256 /* Types may not be defined in exception-declarations. */
23257 saved_message = parser->type_definition_forbidden_message;
23258 parser->type_definition_forbidden_message
23259 = G_("types may not be defined in exception-declarations");
23261 /* Parse the type-specifier-seq. */
23262 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
23263 /*is_trailing_return=*/false,
23264 &type_specifiers);
23265 /* If it's a `)', then there is no declarator. */
23266 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
23267 declarator = NULL;
23268 else
23269 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
23270 /*ctor_dtor_or_conv_p=*/NULL,
23271 /*parenthesized_p=*/NULL,
23272 /*member_p=*/false,
23273 /*friend_p=*/false);
23275 /* Restore the saved message. */
23276 parser->type_definition_forbidden_message = saved_message;
23278 if (!type_specifiers.any_specifiers_p)
23279 return error_mark_node;
23281 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
23284 /* Parse a throw-expression.
23286 throw-expression:
23287 throw assignment-expression [opt]
23289 Returns a THROW_EXPR representing the throw-expression. */
23291 static tree
23292 cp_parser_throw_expression (cp_parser* parser)
23294 tree expression;
23295 cp_token* token;
23297 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
23298 token = cp_lexer_peek_token (parser->lexer);
23299 /* Figure out whether or not there is an assignment-expression
23300 following the "throw" keyword. */
23301 if (token->type == CPP_COMMA
23302 || token->type == CPP_SEMICOLON
23303 || token->type == CPP_CLOSE_PAREN
23304 || token->type == CPP_CLOSE_SQUARE
23305 || token->type == CPP_CLOSE_BRACE
23306 || token->type == CPP_COLON)
23307 expression = NULL_TREE;
23308 else
23309 expression = cp_parser_assignment_expression (parser);
23311 return build_throw (expression);
23314 /* GNU Extensions */
23316 /* Parse an (optional) asm-specification.
23318 asm-specification:
23319 asm ( string-literal )
23321 If the asm-specification is present, returns a STRING_CST
23322 corresponding to the string-literal. Otherwise, returns
23323 NULL_TREE. */
23325 static tree
23326 cp_parser_asm_specification_opt (cp_parser* parser)
23328 cp_token *token;
23329 tree asm_specification;
23331 /* Peek at the next token. */
23332 token = cp_lexer_peek_token (parser->lexer);
23333 /* If the next token isn't the `asm' keyword, then there's no
23334 asm-specification. */
23335 if (!cp_parser_is_keyword (token, RID_ASM))
23336 return NULL_TREE;
23338 /* Consume the `asm' token. */
23339 cp_lexer_consume_token (parser->lexer);
23340 /* Look for the `('. */
23341 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23343 /* Look for the string-literal. */
23344 asm_specification = cp_parser_string_literal (parser, false, false);
23346 /* Look for the `)'. */
23347 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23349 return asm_specification;
23352 /* Parse an asm-operand-list.
23354 asm-operand-list:
23355 asm-operand
23356 asm-operand-list , asm-operand
23358 asm-operand:
23359 string-literal ( expression )
23360 [ string-literal ] string-literal ( expression )
23362 Returns a TREE_LIST representing the operands. The TREE_VALUE of
23363 each node is the expression. The TREE_PURPOSE is itself a
23364 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
23365 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
23366 is a STRING_CST for the string literal before the parenthesis. Returns
23367 ERROR_MARK_NODE if any of the operands are invalid. */
23369 static tree
23370 cp_parser_asm_operand_list (cp_parser* parser)
23372 tree asm_operands = NULL_TREE;
23373 bool invalid_operands = false;
23375 while (true)
23377 tree string_literal;
23378 tree expression;
23379 tree name;
23381 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
23383 /* Consume the `[' token. */
23384 cp_lexer_consume_token (parser->lexer);
23385 /* Read the operand name. */
23386 name = cp_parser_identifier (parser);
23387 if (name != error_mark_node)
23388 name = build_string (IDENTIFIER_LENGTH (name),
23389 IDENTIFIER_POINTER (name));
23390 /* Look for the closing `]'. */
23391 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23393 else
23394 name = NULL_TREE;
23395 /* Look for the string-literal. */
23396 string_literal = cp_parser_string_literal (parser, false, false);
23398 /* Look for the `('. */
23399 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23400 /* Parse the expression. */
23401 expression = cp_parser_expression (parser);
23402 /* Look for the `)'. */
23403 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23405 if (name == error_mark_node
23406 || string_literal == error_mark_node
23407 || expression == error_mark_node)
23408 invalid_operands = true;
23410 /* Add this operand to the list. */
23411 asm_operands = tree_cons (build_tree_list (name, string_literal),
23412 expression,
23413 asm_operands);
23414 /* If the next token is not a `,', there are no more
23415 operands. */
23416 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23417 break;
23418 /* Consume the `,'. */
23419 cp_lexer_consume_token (parser->lexer);
23422 return invalid_operands ? error_mark_node : nreverse (asm_operands);
23425 /* Parse an asm-clobber-list.
23427 asm-clobber-list:
23428 string-literal
23429 asm-clobber-list , string-literal
23431 Returns a TREE_LIST, indicating the clobbers in the order that they
23432 appeared. The TREE_VALUE of each node is a STRING_CST. */
23434 static tree
23435 cp_parser_asm_clobber_list (cp_parser* parser)
23437 tree clobbers = NULL_TREE;
23439 while (true)
23441 tree string_literal;
23443 /* Look for the string literal. */
23444 string_literal = cp_parser_string_literal (parser, false, false);
23445 /* Add it to the list. */
23446 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
23447 /* If the next token is not a `,', then the list is
23448 complete. */
23449 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23450 break;
23451 /* Consume the `,' token. */
23452 cp_lexer_consume_token (parser->lexer);
23455 return clobbers;
23458 /* Parse an asm-label-list.
23460 asm-label-list:
23461 identifier
23462 asm-label-list , identifier
23464 Returns a TREE_LIST, indicating the labels in the order that they
23465 appeared. The TREE_VALUE of each node is a label. */
23467 static tree
23468 cp_parser_asm_label_list (cp_parser* parser)
23470 tree labels = NULL_TREE;
23472 while (true)
23474 tree identifier, label, name;
23476 /* Look for the identifier. */
23477 identifier = cp_parser_identifier (parser);
23478 if (!error_operand_p (identifier))
23480 label = lookup_label (identifier);
23481 if (TREE_CODE (label) == LABEL_DECL)
23483 TREE_USED (label) = 1;
23484 check_goto (label);
23485 name = build_string (IDENTIFIER_LENGTH (identifier),
23486 IDENTIFIER_POINTER (identifier));
23487 labels = tree_cons (name, label, labels);
23490 /* If the next token is not a `,', then the list is
23491 complete. */
23492 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23493 break;
23494 /* Consume the `,' token. */
23495 cp_lexer_consume_token (parser->lexer);
23498 return nreverse (labels);
23501 /* Return TRUE iff the next tokens in the stream are possibly the
23502 beginning of a GNU extension attribute. */
23504 static bool
23505 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
23507 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
23510 /* Return TRUE iff the next tokens in the stream are possibly the
23511 beginning of a standard C++-11 attribute specifier. */
23513 static bool
23514 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
23516 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
23519 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23520 beginning of a standard C++-11 attribute specifier. */
23522 static bool
23523 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
23525 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23527 return (cxx_dialect >= cxx11
23528 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
23529 || (token->type == CPP_OPEN_SQUARE
23530 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
23531 && token->type == CPP_OPEN_SQUARE)));
23534 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23535 beginning of a GNU extension attribute. */
23537 static bool
23538 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
23540 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23542 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
23545 /* Return true iff the next tokens can be the beginning of either a
23546 GNU attribute list, or a standard C++11 attribute sequence. */
23548 static bool
23549 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
23551 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
23552 || cp_next_tokens_can_be_std_attribute_p (parser));
23555 /* Return true iff the next Nth tokens can be the beginning of either
23556 a GNU attribute list, or a standard C++11 attribute sequence. */
23558 static bool
23559 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
23561 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
23562 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
23565 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
23566 of GNU attributes, or return NULL. */
23568 static tree
23569 cp_parser_attributes_opt (cp_parser *parser)
23571 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
23572 return cp_parser_gnu_attributes_opt (parser);
23573 return cp_parser_std_attribute_spec_seq (parser);
23576 #define CILK_SIMD_FN_CLAUSE_MASK \
23577 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
23578 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
23579 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
23580 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
23581 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
23583 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
23584 vector [(<clauses>)] */
23586 static void
23587 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
23589 bool first_p = parser->cilk_simd_fn_info == NULL;
23590 cp_token *token = v_token;
23591 if (first_p)
23593 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
23594 parser->cilk_simd_fn_info->error_seen = false;
23595 parser->cilk_simd_fn_info->fndecl_seen = false;
23596 parser->cilk_simd_fn_info->tokens = vNULL;
23598 int paren_scope = 0;
23599 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23601 cp_lexer_consume_token (parser->lexer);
23602 v_token = cp_lexer_peek_token (parser->lexer);
23603 paren_scope++;
23605 while (paren_scope > 0)
23607 token = cp_lexer_peek_token (parser->lexer);
23608 if (token->type == CPP_OPEN_PAREN)
23609 paren_scope++;
23610 else if (token->type == CPP_CLOSE_PAREN)
23611 paren_scope--;
23612 /* Do not push the last ')' */
23613 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
23614 cp_lexer_consume_token (parser->lexer);
23617 token->type = CPP_PRAGMA_EOL;
23618 parser->lexer->next_token = token;
23619 cp_lexer_consume_token (parser->lexer);
23621 struct cp_token_cache *cp
23622 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
23623 parser->cilk_simd_fn_info->tokens.safe_push (cp);
23626 /* Parse an (optional) series of attributes.
23628 attributes:
23629 attributes attribute
23631 attribute:
23632 __attribute__ (( attribute-list [opt] ))
23634 The return value is as for cp_parser_gnu_attribute_list. */
23636 static tree
23637 cp_parser_gnu_attributes_opt (cp_parser* parser)
23639 tree attributes = NULL_TREE;
23641 while (true)
23643 cp_token *token;
23644 tree attribute_list;
23645 bool ok = true;
23647 /* Peek at the next token. */
23648 token = cp_lexer_peek_token (parser->lexer);
23649 /* If it's not `__attribute__', then we're done. */
23650 if (token->keyword != RID_ATTRIBUTE)
23651 break;
23653 /* Consume the `__attribute__' keyword. */
23654 cp_lexer_consume_token (parser->lexer);
23655 /* Look for the two `(' tokens. */
23656 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23657 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23659 /* Peek at the next token. */
23660 token = cp_lexer_peek_token (parser->lexer);
23661 if (token->type != CPP_CLOSE_PAREN)
23662 /* Parse the attribute-list. */
23663 attribute_list = cp_parser_gnu_attribute_list (parser);
23664 else
23665 /* If the next token is a `)', then there is no attribute
23666 list. */
23667 attribute_list = NULL;
23669 /* Look for the two `)' tokens. */
23670 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23671 ok = false;
23672 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23673 ok = false;
23674 if (!ok)
23675 cp_parser_skip_to_end_of_statement (parser);
23677 /* Add these new attributes to the list. */
23678 attributes = chainon (attributes, attribute_list);
23681 return attributes;
23684 /* Parse a GNU attribute-list.
23686 attribute-list:
23687 attribute
23688 attribute-list , attribute
23690 attribute:
23691 identifier
23692 identifier ( identifier )
23693 identifier ( identifier , expression-list )
23694 identifier ( expression-list )
23696 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
23697 to an attribute. The TREE_PURPOSE of each node is the identifier
23698 indicating which attribute is in use. The TREE_VALUE represents
23699 the arguments, if any. */
23701 static tree
23702 cp_parser_gnu_attribute_list (cp_parser* parser)
23704 tree attribute_list = NULL_TREE;
23705 bool save_translate_strings_p = parser->translate_strings_p;
23707 parser->translate_strings_p = false;
23708 while (true)
23710 cp_token *token;
23711 tree identifier;
23712 tree attribute;
23714 /* Look for the identifier. We also allow keywords here; for
23715 example `__attribute__ ((const))' is legal. */
23716 token = cp_lexer_peek_token (parser->lexer);
23717 if (token->type == CPP_NAME
23718 || token->type == CPP_KEYWORD)
23720 tree arguments = NULL_TREE;
23722 /* Consume the token, but save it since we need it for the
23723 SIMD enabled function parsing. */
23724 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
23726 /* Save away the identifier that indicates which attribute
23727 this is. */
23728 identifier = (token->type == CPP_KEYWORD)
23729 /* For keywords, use the canonical spelling, not the
23730 parsed identifier. */
23731 ? ridpointers[(int) token->keyword]
23732 : id_token->u.value;
23734 attribute = build_tree_list (identifier, NULL_TREE);
23736 /* Peek at the next token. */
23737 token = cp_lexer_peek_token (parser->lexer);
23738 /* If it's an `(', then parse the attribute arguments. */
23739 if (token->type == CPP_OPEN_PAREN)
23741 vec<tree, va_gc> *vec;
23742 int attr_flag = (attribute_takes_identifier_p (identifier)
23743 ? id_attr : normal_attr);
23744 if (is_cilkplus_vector_p (identifier))
23746 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
23747 continue;
23749 else
23750 vec = cp_parser_parenthesized_expression_list
23751 (parser, attr_flag, /*cast_p=*/false,
23752 /*allow_expansion_p=*/false,
23753 /*non_constant_p=*/NULL);
23754 if (vec == NULL)
23755 arguments = error_mark_node;
23756 else
23758 arguments = build_tree_list_vec (vec);
23759 release_tree_vector (vec);
23761 /* Save the arguments away. */
23762 TREE_VALUE (attribute) = arguments;
23764 else if (is_cilkplus_vector_p (identifier))
23766 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
23767 continue;
23770 if (arguments != error_mark_node)
23772 /* Add this attribute to the list. */
23773 TREE_CHAIN (attribute) = attribute_list;
23774 attribute_list = attribute;
23777 token = cp_lexer_peek_token (parser->lexer);
23779 /* Now, look for more attributes. If the next token isn't a
23780 `,', we're done. */
23781 if (token->type != CPP_COMMA)
23782 break;
23784 /* Consume the comma and keep going. */
23785 cp_lexer_consume_token (parser->lexer);
23787 parser->translate_strings_p = save_translate_strings_p;
23789 /* We built up the list in reverse order. */
23790 return nreverse (attribute_list);
23793 /* Parse a standard C++11 attribute.
23795 The returned representation is a TREE_LIST which TREE_PURPOSE is
23796 the scoped name of the attribute, and the TREE_VALUE is its
23797 arguments list.
23799 Note that the scoped name of the attribute is itself a TREE_LIST
23800 which TREE_PURPOSE is the namespace of the attribute, and
23801 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
23802 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
23803 and which TREE_PURPOSE is directly the attribute name.
23805 Clients of the attribute code should use get_attribute_namespace
23806 and get_attribute_name to get the actual namespace and name of
23807 attributes, regardless of their being GNU or C++11 attributes.
23809 attribute:
23810 attribute-token attribute-argument-clause [opt]
23812 attribute-token:
23813 identifier
23814 attribute-scoped-token
23816 attribute-scoped-token:
23817 attribute-namespace :: identifier
23819 attribute-namespace:
23820 identifier
23822 attribute-argument-clause:
23823 ( balanced-token-seq )
23825 balanced-token-seq:
23826 balanced-token [opt]
23827 balanced-token-seq balanced-token
23829 balanced-token:
23830 ( balanced-token-seq )
23831 [ balanced-token-seq ]
23832 { balanced-token-seq }. */
23834 static tree
23835 cp_parser_std_attribute (cp_parser *parser)
23837 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
23838 cp_token *token;
23840 /* First, parse name of the attribute, a.k.a attribute-token. */
23842 token = cp_lexer_peek_token (parser->lexer);
23843 if (token->type == CPP_NAME)
23844 attr_id = token->u.value;
23845 else if (token->type == CPP_KEYWORD)
23846 attr_id = ridpointers[(int) token->keyword];
23847 else if (token->flags & NAMED_OP)
23848 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
23850 if (attr_id == NULL_TREE)
23851 return NULL_TREE;
23853 cp_lexer_consume_token (parser->lexer);
23855 token = cp_lexer_peek_token (parser->lexer);
23856 if (token->type == CPP_SCOPE)
23858 /* We are seeing a scoped attribute token. */
23860 cp_lexer_consume_token (parser->lexer);
23861 attr_ns = attr_id;
23863 token = cp_lexer_consume_token (parser->lexer);
23864 if (token->type == CPP_NAME)
23865 attr_id = token->u.value;
23866 else if (token->type == CPP_KEYWORD)
23867 attr_id = ridpointers[(int) token->keyword];
23868 else
23870 error_at (token->location,
23871 "expected an identifier for the attribute name");
23872 return error_mark_node;
23874 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
23875 NULL_TREE);
23876 token = cp_lexer_peek_token (parser->lexer);
23878 else
23880 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
23881 NULL_TREE);
23882 /* C++11 noreturn attribute is equivalent to GNU's. */
23883 if (is_attribute_p ("noreturn", attr_id))
23884 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
23885 /* C++14 deprecated attribute is equivalent to GNU's. */
23886 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
23888 if (cxx_dialect == cxx11)
23889 pedwarn (token->location, OPT_Wpedantic,
23890 "%<deprecated%> is a C++14 feature;"
23891 " use %<gnu::deprecated%>");
23892 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
23894 /* Transactional Memory TS optimize_for_synchronized attribute is
23895 equivalent to GNU transaction_callable. */
23896 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
23897 TREE_PURPOSE (attribute)
23898 = get_identifier ("transaction_callable");
23899 /* Transactional Memory attributes are GNU attributes. */
23900 else if (tm_attr_to_mask (attr_id))
23901 TREE_PURPOSE (attribute) = attr_id;
23904 /* Now parse the optional argument clause of the attribute. */
23906 if (token->type != CPP_OPEN_PAREN)
23907 return attribute;
23910 vec<tree, va_gc> *vec;
23911 int attr_flag = normal_attr;
23913 if (attr_ns == get_identifier ("gnu")
23914 && attribute_takes_identifier_p (attr_id))
23915 /* A GNU attribute that takes an identifier in parameter. */
23916 attr_flag = id_attr;
23918 vec = cp_parser_parenthesized_expression_list
23919 (parser, attr_flag, /*cast_p=*/false,
23920 /*allow_expansion_p=*/true,
23921 /*non_constant_p=*/NULL);
23922 if (vec == NULL)
23923 arguments = error_mark_node;
23924 else
23926 arguments = build_tree_list_vec (vec);
23927 release_tree_vector (vec);
23930 if (arguments == error_mark_node)
23931 attribute = error_mark_node;
23932 else
23933 TREE_VALUE (attribute) = arguments;
23936 return attribute;
23939 /* Check that the attribute ATTRIBUTE appears at most once in the
23940 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
23941 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
23942 isn't implemented yet in GCC. */
23944 static void
23945 cp_parser_check_std_attribute (tree attributes, tree attribute)
23947 if (attributes)
23949 tree name = get_attribute_name (attribute);
23950 if (is_attribute_p ("noreturn", name)
23951 && lookup_attribute ("noreturn", attributes))
23952 error ("attribute noreturn can appear at most once "
23953 "in an attribute-list");
23954 else if (is_attribute_p ("deprecated", name)
23955 && lookup_attribute ("deprecated", attributes))
23956 error ("attribute deprecated can appear at most once "
23957 "in an attribute-list");
23961 /* Parse a list of standard C++-11 attributes.
23963 attribute-list:
23964 attribute [opt]
23965 attribute-list , attribute[opt]
23966 attribute ...
23967 attribute-list , attribute ...
23970 static tree
23971 cp_parser_std_attribute_list (cp_parser *parser)
23973 tree attributes = NULL_TREE, attribute = NULL_TREE;
23974 cp_token *token = NULL;
23976 while (true)
23978 attribute = cp_parser_std_attribute (parser);
23979 if (attribute == error_mark_node)
23980 break;
23981 if (attribute != NULL_TREE)
23983 cp_parser_check_std_attribute (attributes, attribute);
23984 TREE_CHAIN (attribute) = attributes;
23985 attributes = attribute;
23987 token = cp_lexer_peek_token (parser->lexer);
23988 if (token->type == CPP_ELLIPSIS)
23990 cp_lexer_consume_token (parser->lexer);
23991 TREE_VALUE (attribute)
23992 = make_pack_expansion (TREE_VALUE (attribute));
23993 token = cp_lexer_peek_token (parser->lexer);
23995 if (token->type != CPP_COMMA)
23996 break;
23997 cp_lexer_consume_token (parser->lexer);
23999 attributes = nreverse (attributes);
24000 return attributes;
24003 /* Parse a standard C++-11 attribute specifier.
24005 attribute-specifier:
24006 [ [ attribute-list ] ]
24007 alignment-specifier
24009 alignment-specifier:
24010 alignas ( type-id ... [opt] )
24011 alignas ( alignment-expression ... [opt] ). */
24013 static tree
24014 cp_parser_std_attribute_spec (cp_parser *parser)
24016 tree attributes = NULL_TREE;
24017 cp_token *token = cp_lexer_peek_token (parser->lexer);
24019 if (token->type == CPP_OPEN_SQUARE
24020 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
24022 cp_lexer_consume_token (parser->lexer);
24023 cp_lexer_consume_token (parser->lexer);
24025 attributes = cp_parser_std_attribute_list (parser);
24027 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
24028 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
24029 cp_parser_skip_to_end_of_statement (parser);
24030 else
24031 /* Warn about parsing c++11 attribute in non-c++1 mode, only
24032 when we are sure that we have actually parsed them. */
24033 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24035 else
24037 tree alignas_expr;
24039 /* Look for an alignment-specifier. */
24041 token = cp_lexer_peek_token (parser->lexer);
24043 if (token->type != CPP_KEYWORD
24044 || token->keyword != RID_ALIGNAS)
24045 return NULL_TREE;
24047 cp_lexer_consume_token (parser->lexer);
24048 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24050 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
24052 cp_parser_error (parser, "expected %<(%>");
24053 return error_mark_node;
24056 cp_parser_parse_tentatively (parser);
24057 alignas_expr = cp_parser_type_id (parser);
24059 if (!cp_parser_parse_definitely (parser))
24061 gcc_assert (alignas_expr == error_mark_node
24062 || alignas_expr == NULL_TREE);
24064 alignas_expr =
24065 cp_parser_assignment_expression (parser);
24066 if (alignas_expr == error_mark_node)
24067 cp_parser_skip_to_end_of_statement (parser);
24068 if (alignas_expr == NULL_TREE
24069 || alignas_expr == error_mark_node)
24070 return alignas_expr;
24073 alignas_expr = cxx_alignas_expr (alignas_expr);
24074 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
24076 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24078 cp_lexer_consume_token (parser->lexer);
24079 alignas_expr = make_pack_expansion (alignas_expr);
24082 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
24084 cp_parser_error (parser, "expected %<)%>");
24085 return error_mark_node;
24088 /* Build the C++-11 representation of an 'aligned'
24089 attribute. */
24090 attributes =
24091 build_tree_list (build_tree_list (get_identifier ("gnu"),
24092 get_identifier ("aligned")),
24093 alignas_expr);
24096 return attributes;
24099 /* Parse a standard C++-11 attribute-specifier-seq.
24101 attribute-specifier-seq:
24102 attribute-specifier-seq [opt] attribute-specifier
24105 static tree
24106 cp_parser_std_attribute_spec_seq (cp_parser *parser)
24108 tree attr_specs = NULL_TREE;
24109 tree attr_last = NULL_TREE;
24111 while (true)
24113 tree attr_spec = cp_parser_std_attribute_spec (parser);
24114 if (attr_spec == NULL_TREE)
24115 break;
24116 if (attr_spec == error_mark_node)
24117 return error_mark_node;
24119 if (attr_last)
24120 TREE_CHAIN (attr_last) = attr_spec;
24121 else
24122 attr_specs = attr_last = attr_spec;
24123 attr_last = tree_last (attr_last);
24126 return attr_specs;
24129 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
24130 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
24131 current value of the PEDANTIC flag, regardless of whether or not
24132 the `__extension__' keyword is present. The caller is responsible
24133 for restoring the value of the PEDANTIC flag. */
24135 static bool
24136 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
24138 /* Save the old value of the PEDANTIC flag. */
24139 *saved_pedantic = pedantic;
24141 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
24143 /* Consume the `__extension__' token. */
24144 cp_lexer_consume_token (parser->lexer);
24145 /* We're not being pedantic while the `__extension__' keyword is
24146 in effect. */
24147 pedantic = 0;
24149 return true;
24152 return false;
24155 /* Parse a label declaration.
24157 label-declaration:
24158 __label__ label-declarator-seq ;
24160 label-declarator-seq:
24161 identifier , label-declarator-seq
24162 identifier */
24164 static void
24165 cp_parser_label_declaration (cp_parser* parser)
24167 /* Look for the `__label__' keyword. */
24168 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
24170 while (true)
24172 tree identifier;
24174 /* Look for an identifier. */
24175 identifier = cp_parser_identifier (parser);
24176 /* If we failed, stop. */
24177 if (identifier == error_mark_node)
24178 break;
24179 /* Declare it as a label. */
24180 finish_label_decl (identifier);
24181 /* If the next token is a `;', stop. */
24182 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24183 break;
24184 /* Look for the `,' separating the label declarations. */
24185 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
24188 /* Look for the final `;'. */
24189 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24192 // -------------------------------------------------------------------------- //
24193 // Requires Clause
24195 // Parse a requires clause.
24197 // requires-clause:
24198 // 'requires' logical-or-expression
24200 // The required logical-or-expression must be a constant expression. Note
24201 // that we don't check that the expression is constepxr here. We defer until
24202 // we analyze constraints and then, we only check atomic constraints.
24203 static tree
24204 cp_parser_requires_clause (cp_parser *parser)
24206 // Parse the requires clause so that it is not automatically folded.
24207 ++processing_template_decl;
24208 tree expr = cp_parser_binary_expression (parser, false, false,
24209 PREC_NOT_OPERATOR, NULL);
24210 if (check_for_bare_parameter_packs (expr))
24211 expr = error_mark_node;
24212 --processing_template_decl;
24213 return expr;
24216 // Optionally parse a requires clause:
24217 static tree
24218 cp_parser_requires_clause_opt (cp_parser *parser)
24220 cp_token *tok = cp_lexer_peek_token (parser->lexer);
24221 if (tok->keyword != RID_REQUIRES)
24223 if (!flag_concepts && tok->type == CPP_NAME
24224 && tok->u.value == ridpointers[RID_REQUIRES])
24226 error_at (cp_lexer_peek_token (parser->lexer)->location,
24227 "%<requires%> only available with -fconcepts");
24228 /* Parse and discard the requires-clause. */
24229 cp_lexer_consume_token (parser->lexer);
24230 cp_parser_requires_clause (parser);
24232 return NULL_TREE;
24234 cp_lexer_consume_token (parser->lexer);
24235 return cp_parser_requires_clause (parser);
24239 /*---------------------------------------------------------------------------
24240 Requires expressions
24241 ---------------------------------------------------------------------------*/
24243 /* Parse a requires expression
24245 requirement-expression:
24246 'requires' requirement-parameter-list [opt] requirement-body */
24247 static tree
24248 cp_parser_requires_expression (cp_parser *parser)
24250 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
24251 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
24253 /* A requires-expression shall appear only within a concept
24254 definition or a requires-clause.
24256 TODO: Implement this diagnostic correctly. */
24257 if (!processing_template_decl)
24259 error_at (loc, "a requires expression cannot appear outside a template");
24260 cp_parser_skip_to_end_of_statement (parser);
24261 return error_mark_node;
24264 tree parms, reqs;
24266 /* Local parameters are delared as variables within the scope
24267 of the expression. They are not visible past the end of
24268 the expression. Expressions within the requires-expression
24269 are unevaluated. */
24270 struct scope_sentinel
24272 scope_sentinel ()
24274 ++cp_unevaluated_operand;
24275 begin_scope (sk_block, NULL_TREE);
24278 ~scope_sentinel ()
24280 pop_bindings_and_leave_scope ();
24281 --cp_unevaluated_operand;
24283 } s;
24285 /* Parse the optional parameter list. */
24286 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24288 parms = cp_parser_requirement_parameter_list (parser);
24289 if (parms == error_mark_node)
24290 return error_mark_node;
24292 else
24293 parms = NULL_TREE;
24295 /* Parse the requirement body. */
24296 reqs = cp_parser_requirement_body (parser);
24297 if (reqs == error_mark_node)
24298 return error_mark_node;
24301 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
24302 the parm chain. */
24303 grokparms (parms, &parms);
24304 return finish_requires_expr (parms, reqs);
24307 /* Parse a parameterized requirement.
24309 requirement-parameter-list:
24310 '(' parameter-declaration-clause ')' */
24311 static tree
24312 cp_parser_requirement_parameter_list (cp_parser *parser)
24314 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24315 return error_mark_node;
24317 tree parms = cp_parser_parameter_declaration_clause (parser);
24319 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24320 return error_mark_node;
24322 return parms;
24325 /* Parse the body of a requirement.
24327 requirement-body:
24328 '{' requirement-list '}' */
24329 static tree
24330 cp_parser_requirement_body (cp_parser *parser)
24332 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24333 return error_mark_node;
24335 tree reqs = cp_parser_requirement_list (parser);
24337 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24338 return error_mark_node;
24340 return reqs;
24343 /* Parse a list of requirements.
24345 requirement-list:
24346 requirement
24347 requirement-list ';' requirement[opt] */
24348 static tree
24349 cp_parser_requirement_list (cp_parser *parser)
24351 tree result = NULL_TREE;
24352 while (true)
24354 tree req = cp_parser_requirement (parser);
24355 if (req == error_mark_node)
24356 return error_mark_node;
24358 result = tree_cons (NULL_TREE, req, result);
24360 /* If we see a semi-colon, consume it. */
24361 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24362 cp_lexer_consume_token (parser->lexer);
24364 /* Stop processing at the end of the list. */
24365 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24366 break;
24369 /* Reverse the order of requirements so they are analyzed in
24370 declaration order. */
24371 return nreverse (result);
24374 /* Parse a syntactic requirement or type requirement.
24376 requirement:
24377 simple-requirement
24378 compound-requirement
24379 type-requirement
24380 nested-requirement */
24381 static tree
24382 cp_parser_requirement (cp_parser *parser)
24384 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24385 return cp_parser_compound_requirement (parser);
24386 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24387 return cp_parser_type_requirement (parser);
24388 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
24389 return cp_parser_nested_requirement (parser);
24390 else
24391 return cp_parser_simple_requirement (parser);
24394 /* Parse a simple requirement.
24396 simple-requirement:
24397 expression ';' */
24398 static tree
24399 cp_parser_simple_requirement (cp_parser *parser)
24401 tree expr = cp_parser_expression (parser, NULL, false, false);
24402 if (!expr || expr == error_mark_node)
24403 return error_mark_node;
24405 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
24406 return error_mark_node;
24408 return finish_simple_requirement (expr);
24411 /* Parse a type requirement
24413 type-requirement
24414 nested-name-specifier [opt] required-type-name ';'
24416 required-type-name:
24417 type-name
24418 'template' [opt] simple-template-id */
24419 static tree
24420 cp_parser_type_requirement (cp_parser *parser)
24422 cp_lexer_consume_token (parser->lexer);
24424 // Save the scope before parsing name specifiers.
24425 tree saved_scope = parser->scope;
24426 tree saved_object_scope = parser->object_scope;
24427 tree saved_qualifying_scope = parser->qualifying_scope;
24428 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
24429 cp_parser_nested_name_specifier_opt (parser,
24430 /*typename_keyword_p=*/true,
24431 /*check_dependency_p=*/false,
24432 /*type_p=*/true,
24433 /*is_declaration=*/false);
24435 tree type;
24436 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24438 cp_lexer_consume_token (parser->lexer);
24439 type = cp_parser_template_id (parser,
24440 /*template_keyword_p=*/true,
24441 /*check_dependency=*/false,
24442 /*tag_type=*/none_type,
24443 /*is_declaration=*/false);
24444 type = make_typename_type (parser->scope, type, typename_type,
24445 /*complain=*/tf_error);
24447 else
24448 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
24450 if (TREE_CODE (type) == TYPE_DECL)
24451 type = TREE_TYPE (type);
24453 parser->scope = saved_scope;
24454 parser->object_scope = saved_object_scope;
24455 parser->qualifying_scope = saved_qualifying_scope;
24457 if (type == error_mark_node)
24458 cp_parser_skip_to_end_of_statement (parser);
24460 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
24461 return error_mark_node;
24462 if (type == error_mark_node)
24463 return error_mark_node;
24465 return finish_type_requirement (type);
24468 /* Parse a compound requirement
24470 compound-requirement:
24471 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
24472 static tree
24473 cp_parser_compound_requirement (cp_parser *parser)
24475 /* Parse an expression enclosed in '{ }'s. */
24476 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24477 return error_mark_node;
24479 tree expr = cp_parser_expression (parser, NULL, false, false);
24480 if (!expr || expr == error_mark_node)
24481 return error_mark_node;
24483 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24484 return error_mark_node;
24486 /* Parse the optional noexcept. */
24487 bool noexcept_p = false;
24488 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
24490 cp_lexer_consume_token (parser->lexer);
24491 noexcept_p = true;
24494 /* Parse the optional trailing return type. */
24495 tree type = NULL_TREE;
24496 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
24498 cp_lexer_consume_token (parser->lexer);
24499 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
24500 parser->in_result_type_constraint_p = true;
24501 type = cp_parser_trailing_type_id (parser);
24502 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
24503 if (type == error_mark_node)
24504 return error_mark_node;
24507 return finish_compound_requirement (expr, type, noexcept_p);
24510 /* Parse a nested requirement. This is the same as a requires clause.
24512 nested-requirement:
24513 requires-clause */
24514 static tree
24515 cp_parser_nested_requirement (cp_parser *parser)
24517 cp_lexer_consume_token (parser->lexer);
24518 tree req = cp_parser_requires_clause (parser);
24519 if (req == error_mark_node)
24520 return error_mark_node;
24521 return finish_nested_requirement (req);
24524 /* Support Functions */
24526 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
24527 NAME should have one of the representations used for an
24528 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
24529 is returned. If PARSER->SCOPE is a dependent type, then a
24530 SCOPE_REF is returned.
24532 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
24533 returned; the name was already resolved when the TEMPLATE_ID_EXPR
24534 was formed. Abstractly, such entities should not be passed to this
24535 function, because they do not need to be looked up, but it is
24536 simpler to check for this special case here, rather than at the
24537 call-sites.
24539 In cases not explicitly covered above, this function returns a
24540 DECL, OVERLOAD, or baselink representing the result of the lookup.
24541 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
24542 is returned.
24544 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
24545 (e.g., "struct") that was used. In that case bindings that do not
24546 refer to types are ignored.
24548 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
24549 ignored.
24551 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
24552 are ignored.
24554 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
24555 types.
24557 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
24558 TREE_LIST of candidates if name-lookup results in an ambiguity, and
24559 NULL_TREE otherwise. */
24561 static cp_expr
24562 cp_parser_lookup_name (cp_parser *parser, tree name,
24563 enum tag_types tag_type,
24564 bool is_template,
24565 bool is_namespace,
24566 bool check_dependency,
24567 tree *ambiguous_decls,
24568 location_t name_location)
24570 tree decl;
24571 tree object_type = parser->context->object_type;
24573 /* Assume that the lookup will be unambiguous. */
24574 if (ambiguous_decls)
24575 *ambiguous_decls = NULL_TREE;
24577 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
24578 no longer valid. Note that if we are parsing tentatively, and
24579 the parse fails, OBJECT_TYPE will be automatically restored. */
24580 parser->context->object_type = NULL_TREE;
24582 if (name == error_mark_node)
24583 return error_mark_node;
24585 /* A template-id has already been resolved; there is no lookup to
24586 do. */
24587 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
24588 return name;
24589 if (BASELINK_P (name))
24591 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
24592 == TEMPLATE_ID_EXPR);
24593 return name;
24596 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
24597 it should already have been checked to make sure that the name
24598 used matches the type being destroyed. */
24599 if (TREE_CODE (name) == BIT_NOT_EXPR)
24601 tree type;
24603 /* Figure out to which type this destructor applies. */
24604 if (parser->scope)
24605 type = parser->scope;
24606 else if (object_type)
24607 type = object_type;
24608 else
24609 type = current_class_type;
24610 /* If that's not a class type, there is no destructor. */
24611 if (!type || !CLASS_TYPE_P (type))
24612 return error_mark_node;
24613 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
24614 lazily_declare_fn (sfk_destructor, type);
24615 if (!CLASSTYPE_DESTRUCTORS (type))
24616 return error_mark_node;
24617 /* If it was a class type, return the destructor. */
24618 return CLASSTYPE_DESTRUCTORS (type);
24621 /* By this point, the NAME should be an ordinary identifier. If
24622 the id-expression was a qualified name, the qualifying scope is
24623 stored in PARSER->SCOPE at this point. */
24624 gcc_assert (identifier_p (name));
24626 /* Perform the lookup. */
24627 if (parser->scope)
24629 bool dependent_p;
24631 if (parser->scope == error_mark_node)
24632 return error_mark_node;
24634 /* If the SCOPE is dependent, the lookup must be deferred until
24635 the template is instantiated -- unless we are explicitly
24636 looking up names in uninstantiated templates. Even then, we
24637 cannot look up the name if the scope is not a class type; it
24638 might, for example, be a template type parameter. */
24639 dependent_p = (TYPE_P (parser->scope)
24640 && dependent_scope_p (parser->scope));
24641 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
24642 && dependent_p)
24643 /* Defer lookup. */
24644 decl = error_mark_node;
24645 else
24647 tree pushed_scope = NULL_TREE;
24649 /* If PARSER->SCOPE is a dependent type, then it must be a
24650 class type, and we must not be checking dependencies;
24651 otherwise, we would have processed this lookup above. So
24652 that PARSER->SCOPE is not considered a dependent base by
24653 lookup_member, we must enter the scope here. */
24654 if (dependent_p)
24655 pushed_scope = push_scope (parser->scope);
24657 /* If the PARSER->SCOPE is a template specialization, it
24658 may be instantiated during name lookup. In that case,
24659 errors may be issued. Even if we rollback the current
24660 tentative parse, those errors are valid. */
24661 decl = lookup_qualified_name (parser->scope, name,
24662 tag_type != none_type,
24663 /*complain=*/true);
24665 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
24666 lookup result and the nested-name-specifier nominates a class C:
24667 * if the name specified after the nested-name-specifier, when
24668 looked up in C, is the injected-class-name of C (Clause 9), or
24669 * if the name specified after the nested-name-specifier is the
24670 same as the identifier or the simple-template-id's template-
24671 name in the last component of the nested-name-specifier,
24672 the name is instead considered to name the constructor of
24673 class C. [ Note: for example, the constructor is not an
24674 acceptable lookup result in an elaborated-type-specifier so
24675 the constructor would not be used in place of the
24676 injected-class-name. --end note ] Such a constructor name
24677 shall be used only in the declarator-id of a declaration that
24678 names a constructor or in a using-declaration. */
24679 if (tag_type == none_type
24680 && DECL_SELF_REFERENCE_P (decl)
24681 && same_type_p (DECL_CONTEXT (decl), parser->scope))
24682 decl = lookup_qualified_name (parser->scope, ctor_identifier,
24683 tag_type != none_type,
24684 /*complain=*/true);
24686 /* If we have a single function from a using decl, pull it out. */
24687 if (TREE_CODE (decl) == OVERLOAD
24688 && !really_overloaded_fn (decl))
24689 decl = OVL_FUNCTION (decl);
24691 if (pushed_scope)
24692 pop_scope (pushed_scope);
24695 /* If the scope is a dependent type and either we deferred lookup or
24696 we did lookup but didn't find the name, rememeber the name. */
24697 if (decl == error_mark_node && TYPE_P (parser->scope)
24698 && dependent_type_p (parser->scope))
24700 if (tag_type)
24702 tree type;
24704 /* The resolution to Core Issue 180 says that `struct
24705 A::B' should be considered a type-name, even if `A'
24706 is dependent. */
24707 type = make_typename_type (parser->scope, name, tag_type,
24708 /*complain=*/tf_error);
24709 if (type != error_mark_node)
24710 decl = TYPE_NAME (type);
24712 else if (is_template
24713 && (cp_parser_next_token_ends_template_argument_p (parser)
24714 || cp_lexer_next_token_is (parser->lexer,
24715 CPP_CLOSE_PAREN)))
24716 decl = make_unbound_class_template (parser->scope,
24717 name, NULL_TREE,
24718 /*complain=*/tf_error);
24719 else
24720 decl = build_qualified_name (/*type=*/NULL_TREE,
24721 parser->scope, name,
24722 is_template);
24724 parser->qualifying_scope = parser->scope;
24725 parser->object_scope = NULL_TREE;
24727 else if (object_type)
24729 /* Look up the name in the scope of the OBJECT_TYPE, unless the
24730 OBJECT_TYPE is not a class. */
24731 if (CLASS_TYPE_P (object_type))
24732 /* If the OBJECT_TYPE is a template specialization, it may
24733 be instantiated during name lookup. In that case, errors
24734 may be issued. Even if we rollback the current tentative
24735 parse, those errors are valid. */
24736 decl = lookup_member (object_type,
24737 name,
24738 /*protect=*/0,
24739 tag_type != none_type,
24740 tf_warning_or_error);
24741 else
24742 decl = NULL_TREE;
24744 if (!decl)
24746 /* Look it up in the enclosing context. */
24747 decl = lookup_name_real (name, tag_type != none_type,
24748 /*nonclass=*/0,
24749 /*block_p=*/true, is_namespace, 0);
24750 /* DR 141 says when looking for a template-name after -> or ., only
24751 consider class templates. We need to fix our handling of
24752 dependent expressions to implement that properly, but for now
24753 let's ignore namespace-scope function templates. */
24754 if (decl && is_template && !DECL_TYPE_TEMPLATE_P (decl))
24756 tree d = decl;
24757 if (is_overloaded_fn (d))
24758 d = get_first_fn (d);
24759 if (DECL_P (d) && !DECL_CLASS_SCOPE_P (d))
24760 decl = NULL_TREE;
24763 if (object_type == unknown_type_node)
24764 /* The object is type-dependent, so we can't look anything up; we used
24765 this to get the DR 141 behavior. */
24766 object_type = NULL_TREE;
24767 parser->object_scope = object_type;
24768 parser->qualifying_scope = NULL_TREE;
24770 else
24772 decl = lookup_name_real (name, tag_type != none_type,
24773 /*nonclass=*/0,
24774 /*block_p=*/true, is_namespace, 0);
24775 parser->qualifying_scope = NULL_TREE;
24776 parser->object_scope = NULL_TREE;
24779 /* If the lookup failed, let our caller know. */
24780 if (!decl || decl == error_mark_node)
24781 return error_mark_node;
24783 /* Pull out the template from an injected-class-name (or multiple). */
24784 if (is_template)
24785 decl = maybe_get_template_decl_from_type_decl (decl);
24787 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
24788 if (TREE_CODE (decl) == TREE_LIST)
24790 if (ambiguous_decls)
24791 *ambiguous_decls = decl;
24792 /* The error message we have to print is too complicated for
24793 cp_parser_error, so we incorporate its actions directly. */
24794 if (!cp_parser_simulate_error (parser))
24796 error_at (name_location, "reference to %qD is ambiguous",
24797 name);
24798 print_candidates (decl);
24800 return error_mark_node;
24803 gcc_assert (DECL_P (decl)
24804 || TREE_CODE (decl) == OVERLOAD
24805 || TREE_CODE (decl) == SCOPE_REF
24806 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
24807 || BASELINK_P (decl));
24809 /* If we have resolved the name of a member declaration, check to
24810 see if the declaration is accessible. When the name resolves to
24811 set of overloaded functions, accessibility is checked when
24812 overload resolution is done.
24814 During an explicit instantiation, access is not checked at all,
24815 as per [temp.explicit]. */
24816 if (DECL_P (decl))
24817 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
24819 maybe_record_typedef_use (decl);
24821 return cp_expr (decl, name_location);
24824 /* Like cp_parser_lookup_name, but for use in the typical case where
24825 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
24826 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
24828 static tree
24829 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
24831 return cp_parser_lookup_name (parser, name,
24832 none_type,
24833 /*is_template=*/false,
24834 /*is_namespace=*/false,
24835 /*check_dependency=*/true,
24836 /*ambiguous_decls=*/NULL,
24837 location);
24840 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
24841 the current context, return the TYPE_DECL. If TAG_NAME_P is
24842 true, the DECL indicates the class being defined in a class-head,
24843 or declared in an elaborated-type-specifier.
24845 Otherwise, return DECL. */
24847 static tree
24848 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
24850 /* If the TEMPLATE_DECL is being declared as part of a class-head,
24851 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
24853 struct A {
24854 template <typename T> struct B;
24857 template <typename T> struct A::B {};
24859 Similarly, in an elaborated-type-specifier:
24861 namespace N { struct X{}; }
24863 struct A {
24864 template <typename T> friend struct N::X;
24867 However, if the DECL refers to a class type, and we are in
24868 the scope of the class, then the name lookup automatically
24869 finds the TYPE_DECL created by build_self_reference rather
24870 than a TEMPLATE_DECL. For example, in:
24872 template <class T> struct S {
24873 S s;
24876 there is no need to handle such case. */
24878 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
24879 return DECL_TEMPLATE_RESULT (decl);
24881 return decl;
24884 /* If too many, or too few, template-parameter lists apply to the
24885 declarator, issue an error message. Returns TRUE if all went well,
24886 and FALSE otherwise. */
24888 static bool
24889 cp_parser_check_declarator_template_parameters (cp_parser* parser,
24890 cp_declarator *declarator,
24891 location_t declarator_location)
24893 switch (declarator->kind)
24895 case cdk_id:
24897 unsigned num_templates = 0;
24898 tree scope = declarator->u.id.qualifying_scope;
24900 if (scope)
24901 num_templates = num_template_headers_for_class (scope);
24902 else if (TREE_CODE (declarator->u.id.unqualified_name)
24903 == TEMPLATE_ID_EXPR)
24904 /* If the DECLARATOR has the form `X<y>' then it uses one
24905 additional level of template parameters. */
24906 ++num_templates;
24908 return cp_parser_check_template_parameters
24909 (parser, num_templates, declarator_location, declarator);
24912 case cdk_function:
24913 case cdk_array:
24914 case cdk_pointer:
24915 case cdk_reference:
24916 case cdk_ptrmem:
24917 return (cp_parser_check_declarator_template_parameters
24918 (parser, declarator->declarator, declarator_location));
24920 case cdk_error:
24921 return true;
24923 default:
24924 gcc_unreachable ();
24926 return false;
24929 /* NUM_TEMPLATES were used in the current declaration. If that is
24930 invalid, return FALSE and issue an error messages. Otherwise,
24931 return TRUE. If DECLARATOR is non-NULL, then we are checking a
24932 declarator and we can print more accurate diagnostics. */
24934 static bool
24935 cp_parser_check_template_parameters (cp_parser* parser,
24936 unsigned num_templates,
24937 location_t location,
24938 cp_declarator *declarator)
24940 /* If there are the same number of template classes and parameter
24941 lists, that's OK. */
24942 if (parser->num_template_parameter_lists == num_templates)
24943 return true;
24944 /* If there are more, but only one more, then we are referring to a
24945 member template. That's OK too. */
24946 if (parser->num_template_parameter_lists == num_templates + 1)
24947 return true;
24948 /* If there are more template classes than parameter lists, we have
24949 something like:
24951 template <class T> void S<T>::R<T>::f (); */
24952 if (parser->num_template_parameter_lists < num_templates)
24954 if (declarator && !current_function_decl)
24955 error_at (location, "specializing member %<%T::%E%> "
24956 "requires %<template<>%> syntax",
24957 declarator->u.id.qualifying_scope,
24958 declarator->u.id.unqualified_name);
24959 else if (declarator)
24960 error_at (location, "invalid declaration of %<%T::%E%>",
24961 declarator->u.id.qualifying_scope,
24962 declarator->u.id.unqualified_name);
24963 else
24964 error_at (location, "too few template-parameter-lists");
24965 return false;
24967 /* Otherwise, there are too many template parameter lists. We have
24968 something like:
24970 template <class T> template <class U> void S::f(); */
24971 error_at (location, "too many template-parameter-lists");
24972 return false;
24975 /* Parse an optional `::' token indicating that the following name is
24976 from the global namespace. If so, PARSER->SCOPE is set to the
24977 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
24978 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
24979 Returns the new value of PARSER->SCOPE, if the `::' token is
24980 present, and NULL_TREE otherwise. */
24982 static tree
24983 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
24985 cp_token *token;
24987 /* Peek at the next token. */
24988 token = cp_lexer_peek_token (parser->lexer);
24989 /* If we're looking at a `::' token then we're starting from the
24990 global namespace, not our current location. */
24991 if (token->type == CPP_SCOPE)
24993 /* Consume the `::' token. */
24994 cp_lexer_consume_token (parser->lexer);
24995 /* Set the SCOPE so that we know where to start the lookup. */
24996 parser->scope = global_namespace;
24997 parser->qualifying_scope = global_namespace;
24998 parser->object_scope = NULL_TREE;
25000 return parser->scope;
25002 else if (!current_scope_valid_p)
25004 parser->scope = NULL_TREE;
25005 parser->qualifying_scope = NULL_TREE;
25006 parser->object_scope = NULL_TREE;
25009 return NULL_TREE;
25012 /* Returns TRUE if the upcoming token sequence is the start of a
25013 constructor declarator. If FRIEND_P is true, the declarator is
25014 preceded by the `friend' specifier. */
25016 static bool
25017 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
25019 bool constructor_p;
25020 bool outside_class_specifier_p;
25021 tree nested_name_specifier;
25022 cp_token *next_token;
25024 /* The common case is that this is not a constructor declarator, so
25025 try to avoid doing lots of work if at all possible. It's not
25026 valid declare a constructor at function scope. */
25027 if (parser->in_function_body)
25028 return false;
25029 /* And only certain tokens can begin a constructor declarator. */
25030 next_token = cp_lexer_peek_token (parser->lexer);
25031 if (next_token->type != CPP_NAME
25032 && next_token->type != CPP_SCOPE
25033 && next_token->type != CPP_NESTED_NAME_SPECIFIER
25034 && next_token->type != CPP_TEMPLATE_ID)
25035 return false;
25037 /* Parse tentatively; we are going to roll back all of the tokens
25038 consumed here. */
25039 cp_parser_parse_tentatively (parser);
25040 /* Assume that we are looking at a constructor declarator. */
25041 constructor_p = true;
25043 /* Look for the optional `::' operator. */
25044 cp_parser_global_scope_opt (parser,
25045 /*current_scope_valid_p=*/false);
25046 /* Look for the nested-name-specifier. */
25047 nested_name_specifier
25048 = (cp_parser_nested_name_specifier_opt (parser,
25049 /*typename_keyword_p=*/false,
25050 /*check_dependency_p=*/false,
25051 /*type_p=*/false,
25052 /*is_declaration=*/false));
25054 outside_class_specifier_p = (!at_class_scope_p ()
25055 || !TYPE_BEING_DEFINED (current_class_type)
25056 || friend_p);
25058 /* Outside of a class-specifier, there must be a
25059 nested-name-specifier. */
25060 if (!nested_name_specifier && outside_class_specifier_p)
25061 constructor_p = false;
25062 else if (nested_name_specifier == error_mark_node)
25063 constructor_p = false;
25065 /* If we have a class scope, this is easy; DR 147 says that S::S always
25066 names the constructor, and no other qualified name could. */
25067 if (constructor_p && nested_name_specifier
25068 && CLASS_TYPE_P (nested_name_specifier))
25070 tree id = cp_parser_unqualified_id (parser,
25071 /*template_keyword_p=*/false,
25072 /*check_dependency_p=*/false,
25073 /*declarator_p=*/true,
25074 /*optional_p=*/false);
25075 if (is_overloaded_fn (id))
25076 id = DECL_NAME (get_first_fn (id));
25077 if (!constructor_name_p (id, nested_name_specifier))
25078 constructor_p = false;
25080 /* If we still think that this might be a constructor-declarator,
25081 look for a class-name. */
25082 else if (constructor_p)
25084 /* If we have:
25086 template <typename T> struct S {
25087 S();
25090 we must recognize that the nested `S' names a class. */
25091 tree type_decl;
25092 type_decl = cp_parser_class_name (parser,
25093 /*typename_keyword_p=*/false,
25094 /*template_keyword_p=*/false,
25095 none_type,
25096 /*check_dependency_p=*/false,
25097 /*class_head_p=*/false,
25098 /*is_declaration=*/false);
25099 /* If there was no class-name, then this is not a constructor.
25100 Otherwise, if we are in a class-specifier and we aren't
25101 handling a friend declaration, check that its type matches
25102 current_class_type (c++/38313). Note: error_mark_node
25103 is left alone for error recovery purposes. */
25104 constructor_p = (!cp_parser_error_occurred (parser)
25105 && (outside_class_specifier_p
25106 || type_decl == error_mark_node
25107 || same_type_p (current_class_type,
25108 TREE_TYPE (type_decl))));
25110 /* If we're still considering a constructor, we have to see a `(',
25111 to begin the parameter-declaration-clause, followed by either a
25112 `)', an `...', or a decl-specifier. We need to check for a
25113 type-specifier to avoid being fooled into thinking that:
25115 S (f) (int);
25117 is a constructor. (It is actually a function named `f' that
25118 takes one parameter (of type `int') and returns a value of type
25119 `S'. */
25120 if (constructor_p
25121 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25122 constructor_p = false;
25124 if (constructor_p
25125 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
25126 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
25127 /* A parameter declaration begins with a decl-specifier,
25128 which is either the "attribute" keyword, a storage class
25129 specifier, or (usually) a type-specifier. */
25130 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
25132 tree type;
25133 tree pushed_scope = NULL_TREE;
25134 unsigned saved_num_template_parameter_lists;
25136 /* Names appearing in the type-specifier should be looked up
25137 in the scope of the class. */
25138 if (current_class_type)
25139 type = NULL_TREE;
25140 else
25142 type = TREE_TYPE (type_decl);
25143 if (TREE_CODE (type) == TYPENAME_TYPE)
25145 type = resolve_typename_type (type,
25146 /*only_current_p=*/false);
25147 if (TREE_CODE (type) == TYPENAME_TYPE)
25149 cp_parser_abort_tentative_parse (parser);
25150 return false;
25153 pushed_scope = push_scope (type);
25156 /* Inside the constructor parameter list, surrounding
25157 template-parameter-lists do not apply. */
25158 saved_num_template_parameter_lists
25159 = parser->num_template_parameter_lists;
25160 parser->num_template_parameter_lists = 0;
25162 /* Look for the type-specifier. */
25163 cp_parser_type_specifier (parser,
25164 CP_PARSER_FLAGS_NONE,
25165 /*decl_specs=*/NULL,
25166 /*is_declarator=*/true,
25167 /*declares_class_or_enum=*/NULL,
25168 /*is_cv_qualifier=*/NULL);
25170 parser->num_template_parameter_lists
25171 = saved_num_template_parameter_lists;
25173 /* Leave the scope of the class. */
25174 if (pushed_scope)
25175 pop_scope (pushed_scope);
25177 constructor_p = !cp_parser_error_occurred (parser);
25181 /* We did not really want to consume any tokens. */
25182 cp_parser_abort_tentative_parse (parser);
25184 return constructor_p;
25187 /* Parse the definition of the function given by the DECL_SPECIFIERS,
25188 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
25189 they must be performed once we are in the scope of the function.
25191 Returns the function defined. */
25193 static tree
25194 cp_parser_function_definition_from_specifiers_and_declarator
25195 (cp_parser* parser,
25196 cp_decl_specifier_seq *decl_specifiers,
25197 tree attributes,
25198 const cp_declarator *declarator)
25200 tree fn;
25201 bool success_p;
25203 /* Begin the function-definition. */
25204 success_p = start_function (decl_specifiers, declarator, attributes);
25206 /* The things we're about to see are not directly qualified by any
25207 template headers we've seen thus far. */
25208 reset_specialization ();
25210 /* If there were names looked up in the decl-specifier-seq that we
25211 did not check, check them now. We must wait until we are in the
25212 scope of the function to perform the checks, since the function
25213 might be a friend. */
25214 perform_deferred_access_checks (tf_warning_or_error);
25216 if (success_p)
25218 cp_finalize_omp_declare_simd (parser, current_function_decl);
25219 parser->omp_declare_simd = NULL;
25220 cp_finalize_oacc_routine (parser, current_function_decl, true);
25221 parser->oacc_routine = NULL;
25224 if (!success_p)
25226 /* Skip the entire function. */
25227 cp_parser_skip_to_end_of_block_or_statement (parser);
25228 fn = error_mark_node;
25230 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
25232 /* Seen already, skip it. An error message has already been output. */
25233 cp_parser_skip_to_end_of_block_or_statement (parser);
25234 fn = current_function_decl;
25235 current_function_decl = NULL_TREE;
25236 /* If this is a function from a class, pop the nested class. */
25237 if (current_class_name)
25238 pop_nested_class ();
25240 else
25242 timevar_id_t tv;
25243 if (DECL_DECLARED_INLINE_P (current_function_decl))
25244 tv = TV_PARSE_INLINE;
25245 else
25246 tv = TV_PARSE_FUNC;
25247 timevar_push (tv);
25248 fn = cp_parser_function_definition_after_declarator (parser,
25249 /*inline_p=*/false);
25250 timevar_pop (tv);
25253 return fn;
25256 /* Parse the part of a function-definition that follows the
25257 declarator. INLINE_P is TRUE iff this function is an inline
25258 function defined within a class-specifier.
25260 Returns the function defined. */
25262 static tree
25263 cp_parser_function_definition_after_declarator (cp_parser* parser,
25264 bool inline_p)
25266 tree fn;
25267 bool ctor_initializer_p = false;
25268 bool saved_in_unbraced_linkage_specification_p;
25269 bool saved_in_function_body;
25270 unsigned saved_num_template_parameter_lists;
25271 cp_token *token;
25272 bool fully_implicit_function_template_p
25273 = parser->fully_implicit_function_template_p;
25274 parser->fully_implicit_function_template_p = false;
25275 tree implicit_template_parms
25276 = parser->implicit_template_parms;
25277 parser->implicit_template_parms = 0;
25278 cp_binding_level* implicit_template_scope
25279 = parser->implicit_template_scope;
25280 parser->implicit_template_scope = 0;
25282 saved_in_function_body = parser->in_function_body;
25283 parser->in_function_body = true;
25284 /* If the next token is `return', then the code may be trying to
25285 make use of the "named return value" extension that G++ used to
25286 support. */
25287 token = cp_lexer_peek_token (parser->lexer);
25288 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
25290 /* Consume the `return' keyword. */
25291 cp_lexer_consume_token (parser->lexer);
25292 /* Look for the identifier that indicates what value is to be
25293 returned. */
25294 cp_parser_identifier (parser);
25295 /* Issue an error message. */
25296 error_at (token->location,
25297 "named return values are no longer supported");
25298 /* Skip tokens until we reach the start of the function body. */
25299 while (true)
25301 cp_token *token = cp_lexer_peek_token (parser->lexer);
25302 if (token->type == CPP_OPEN_BRACE
25303 || token->type == CPP_EOF
25304 || token->type == CPP_PRAGMA_EOL)
25305 break;
25306 cp_lexer_consume_token (parser->lexer);
25309 /* The `extern' in `extern "C" void f () { ... }' does not apply to
25310 anything declared inside `f'. */
25311 saved_in_unbraced_linkage_specification_p
25312 = parser->in_unbraced_linkage_specification_p;
25313 parser->in_unbraced_linkage_specification_p = false;
25314 /* Inside the function, surrounding template-parameter-lists do not
25315 apply. */
25316 saved_num_template_parameter_lists
25317 = parser->num_template_parameter_lists;
25318 parser->num_template_parameter_lists = 0;
25320 start_lambda_scope (current_function_decl);
25322 /* If the next token is `try', `__transaction_atomic', or
25323 `__transaction_relaxed`, then we are looking at either function-try-block
25324 or function-transaction-block. Note that all of these include the
25325 function-body. */
25326 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
25327 ctor_initializer_p = cp_parser_function_transaction (parser,
25328 RID_TRANSACTION_ATOMIC);
25329 else if (cp_lexer_next_token_is_keyword (parser->lexer,
25330 RID_TRANSACTION_RELAXED))
25331 ctor_initializer_p = cp_parser_function_transaction (parser,
25332 RID_TRANSACTION_RELAXED);
25333 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
25334 ctor_initializer_p = cp_parser_function_try_block (parser);
25335 else
25336 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
25337 (parser, /*in_function_try_block=*/false);
25339 finish_lambda_scope ();
25341 /* Finish the function. */
25342 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
25343 (inline_p ? 2 : 0));
25344 /* Generate code for it, if necessary. */
25345 expand_or_defer_fn (fn);
25346 /* Restore the saved values. */
25347 parser->in_unbraced_linkage_specification_p
25348 = saved_in_unbraced_linkage_specification_p;
25349 parser->num_template_parameter_lists
25350 = saved_num_template_parameter_lists;
25351 parser->in_function_body = saved_in_function_body;
25353 parser->fully_implicit_function_template_p
25354 = fully_implicit_function_template_p;
25355 parser->implicit_template_parms
25356 = implicit_template_parms;
25357 parser->implicit_template_scope
25358 = implicit_template_scope;
25360 if (parser->fully_implicit_function_template_p)
25361 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
25363 return fn;
25366 /* Parse a template-declaration body (following argument list). */
25368 static void
25369 cp_parser_template_declaration_after_parameters (cp_parser* parser,
25370 tree parameter_list,
25371 bool member_p)
25373 tree decl = NULL_TREE;
25374 bool friend_p = false;
25376 /* We just processed one more parameter list. */
25377 ++parser->num_template_parameter_lists;
25379 /* Get the deferred access checks from the parameter list. These
25380 will be checked once we know what is being declared, as for a
25381 member template the checks must be performed in the scope of the
25382 class containing the member. */
25383 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
25385 /* Tentatively parse for a new template parameter list, which can either be
25386 the template keyword or a template introduction. */
25387 if (cp_parser_template_declaration_after_export (parser, member_p))
25388 /* OK */;
25389 else if (cxx_dialect >= cxx11
25390 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25391 decl = cp_parser_alias_declaration (parser);
25392 else
25394 /* There are no access checks when parsing a template, as we do not
25395 know if a specialization will be a friend. */
25396 push_deferring_access_checks (dk_no_check);
25397 cp_token *token = cp_lexer_peek_token (parser->lexer);
25398 decl = cp_parser_single_declaration (parser,
25399 checks,
25400 member_p,
25401 /*explicit_specialization_p=*/false,
25402 &friend_p);
25403 pop_deferring_access_checks ();
25405 /* If this is a member template declaration, let the front
25406 end know. */
25407 if (member_p && !friend_p && decl)
25409 if (TREE_CODE (decl) == TYPE_DECL)
25410 cp_parser_check_access_in_redeclaration (decl, token->location);
25412 decl = finish_member_template_decl (decl);
25414 else if (friend_p && decl
25415 && DECL_DECLARES_TYPE_P (decl))
25416 make_friend_class (current_class_type, TREE_TYPE (decl),
25417 /*complain=*/true);
25419 /* We are done with the current parameter list. */
25420 --parser->num_template_parameter_lists;
25422 pop_deferring_access_checks ();
25424 /* Finish up. */
25425 finish_template_decl (parameter_list);
25427 /* Check the template arguments for a literal operator template. */
25428 if (decl
25429 && DECL_DECLARES_FUNCTION_P (decl)
25430 && UDLIT_OPER_P (DECL_NAME (decl)))
25432 bool ok = true;
25433 if (parameter_list == NULL_TREE)
25434 ok = false;
25435 else
25437 int num_parms = TREE_VEC_LENGTH (parameter_list);
25438 if (num_parms == 1)
25440 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
25441 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
25442 if (TREE_TYPE (parm) != char_type_node
25443 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
25444 ok = false;
25446 else if (num_parms == 2 && cxx_dialect >= cxx14)
25448 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
25449 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
25450 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
25451 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
25452 if (TREE_TYPE (parm) != TREE_TYPE (type)
25453 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
25454 ok = false;
25456 else
25457 ok = false;
25459 if (!ok)
25461 if (cxx_dialect >= cxx14)
25462 error ("literal operator template %qD has invalid parameter list."
25463 " Expected non-type template argument pack <char...>"
25464 " or <typename CharT, CharT...>",
25465 decl);
25466 else
25467 error ("literal operator template %qD has invalid parameter list."
25468 " Expected non-type template argument pack <char...>",
25469 decl);
25473 /* Register member declarations. */
25474 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
25475 finish_member_declaration (decl);
25476 /* If DECL is a function template, we must return to parse it later.
25477 (Even though there is no definition, there might be default
25478 arguments that need handling.) */
25479 if (member_p && decl
25480 && DECL_DECLARES_FUNCTION_P (decl))
25481 vec_safe_push (unparsed_funs_with_definitions, decl);
25484 /* Parse a template introduction header for a template-declaration. Returns
25485 false if tentative parse fails. */
25487 static bool
25488 cp_parser_template_introduction (cp_parser* parser, bool member_p)
25490 cp_parser_parse_tentatively (parser);
25492 tree saved_scope = parser->scope;
25493 tree saved_object_scope = parser->object_scope;
25494 tree saved_qualifying_scope = parser->qualifying_scope;
25496 /* Look for the optional `::' operator. */
25497 cp_parser_global_scope_opt (parser,
25498 /*current_scope_valid_p=*/false);
25499 /* Look for the nested-name-specifier. */
25500 cp_parser_nested_name_specifier_opt (parser,
25501 /*typename_keyword_p=*/false,
25502 /*check_dependency_p=*/true,
25503 /*type_p=*/false,
25504 /*is_declaration=*/false);
25506 cp_token *token = cp_lexer_peek_token (parser->lexer);
25507 tree concept_name = cp_parser_identifier (parser);
25509 /* Look up the concept for which we will be matching
25510 template parameters. */
25511 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
25512 token->location);
25513 parser->scope = saved_scope;
25514 parser->object_scope = saved_object_scope;
25515 parser->qualifying_scope = saved_qualifying_scope;
25517 if (concept_name == error_mark_node)
25518 cp_parser_simulate_error (parser);
25520 /* Look for opening brace for introduction. */
25521 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
25523 if (!cp_parser_parse_definitely (parser))
25524 return false;
25526 push_deferring_access_checks (dk_deferred);
25528 /* Build vector of placeholder parameters and grab
25529 matching identifiers. */
25530 tree introduction_list = cp_parser_introduction_list (parser);
25532 /* The introduction-list shall not be empty. */
25533 int nargs = TREE_VEC_LENGTH (introduction_list);
25534 if (nargs == 0)
25536 error ("empty introduction-list");
25537 return true;
25540 /* Look for closing brace for introduction. */
25541 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25542 return true;
25544 if (tmpl_decl == error_mark_node)
25546 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
25547 token->location);
25548 return true;
25551 /* Build and associate the constraint. */
25552 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
25553 if (parms && parms != error_mark_node)
25555 cp_parser_template_declaration_after_parameters (parser, parms,
25556 member_p);
25557 return true;
25560 error_at (token->location, "no matching concept for template-introduction");
25561 return true;
25564 /* Parse a normal template-declaration following the template keyword. */
25566 static void
25567 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
25569 tree parameter_list;
25570 bool need_lang_pop;
25571 location_t location = input_location;
25573 /* Look for the `<' token. */
25574 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
25575 return;
25576 if (at_class_scope_p () && current_function_decl)
25578 /* 14.5.2.2 [temp.mem]
25580 A local class shall not have member templates. */
25581 error_at (location,
25582 "invalid declaration of member template in local class");
25583 cp_parser_skip_to_end_of_block_or_statement (parser);
25584 return;
25586 /* [temp]
25588 A template ... shall not have C linkage. */
25589 if (current_lang_name == lang_name_c)
25591 error_at (location, "template with C linkage");
25592 /* Give it C++ linkage to avoid confusing other parts of the
25593 front end. */
25594 push_lang_context (lang_name_cplusplus);
25595 need_lang_pop = true;
25597 else
25598 need_lang_pop = false;
25600 /* We cannot perform access checks on the template parameter
25601 declarations until we know what is being declared, just as we
25602 cannot check the decl-specifier list. */
25603 push_deferring_access_checks (dk_deferred);
25605 /* If the next token is `>', then we have an invalid
25606 specialization. Rather than complain about an invalid template
25607 parameter, issue an error message here. */
25608 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
25610 cp_parser_error (parser, "invalid explicit specialization");
25611 begin_specialization ();
25612 parameter_list = NULL_TREE;
25614 else
25616 /* Parse the template parameters. */
25617 parameter_list = cp_parser_template_parameter_list (parser);
25620 /* Look for the `>'. */
25621 cp_parser_skip_to_end_of_template_parameter_list (parser);
25623 /* Manage template requirements */
25624 tree reqs = get_shorthand_constraints (current_template_parms);
25625 if (tree r = cp_parser_requires_clause_opt (parser))
25626 reqs = conjoin_constraints (reqs, make_predicate_constraint (r));
25627 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
25629 cp_parser_template_declaration_after_parameters (parser, parameter_list,
25630 member_p);
25632 /* For the erroneous case of a template with C linkage, we pushed an
25633 implicit C++ linkage scope; exit that scope now. */
25634 if (need_lang_pop)
25635 pop_lang_context ();
25638 /* Parse a template-declaration, assuming that the `export' (and
25639 `extern') keywords, if present, has already been scanned. MEMBER_P
25640 is as for cp_parser_template_declaration. */
25642 static bool
25643 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
25645 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25647 cp_lexer_consume_token (parser->lexer);
25648 cp_parser_explicit_template_declaration (parser, member_p);
25649 return true;
25651 else if (flag_concepts)
25652 return cp_parser_template_introduction (parser, member_p);
25654 return false;
25657 /* Perform the deferred access checks from a template-parameter-list.
25658 CHECKS is a TREE_LIST of access checks, as returned by
25659 get_deferred_access_checks. */
25661 static void
25662 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
25664 ++processing_template_parmlist;
25665 perform_access_checks (checks, tf_warning_or_error);
25666 --processing_template_parmlist;
25669 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
25670 `function-definition' sequence that follows a template header.
25671 If MEMBER_P is true, this declaration appears in a class scope.
25673 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
25674 *FRIEND_P is set to TRUE iff the declaration is a friend. */
25676 static tree
25677 cp_parser_single_declaration (cp_parser* parser,
25678 vec<deferred_access_check, va_gc> *checks,
25679 bool member_p,
25680 bool explicit_specialization_p,
25681 bool* friend_p)
25683 int declares_class_or_enum;
25684 tree decl = NULL_TREE;
25685 cp_decl_specifier_seq decl_specifiers;
25686 bool function_definition_p = false;
25687 cp_token *decl_spec_token_start;
25689 /* This function is only used when processing a template
25690 declaration. */
25691 gcc_assert (innermost_scope_kind () == sk_template_parms
25692 || innermost_scope_kind () == sk_template_spec);
25694 /* Defer access checks until we know what is being declared. */
25695 push_deferring_access_checks (dk_deferred);
25697 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
25698 alternative. */
25699 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
25700 cp_parser_decl_specifier_seq (parser,
25701 CP_PARSER_FLAGS_OPTIONAL,
25702 &decl_specifiers,
25703 &declares_class_or_enum);
25704 if (friend_p)
25705 *friend_p = cp_parser_friend_p (&decl_specifiers);
25707 /* There are no template typedefs. */
25708 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
25710 error_at (decl_spec_token_start->location,
25711 "template declaration of %<typedef%>");
25712 decl = error_mark_node;
25715 /* Gather up the access checks that occurred the
25716 decl-specifier-seq. */
25717 stop_deferring_access_checks ();
25719 /* Check for the declaration of a template class. */
25720 if (declares_class_or_enum)
25722 if (cp_parser_declares_only_class_p (parser)
25723 || (declares_class_or_enum & 2))
25725 // If this is a declaration, but not a definition, associate
25726 // any constraints with the type declaration. Constraints
25727 // are associated with definitions in cp_parser_class_specifier.
25728 if (declares_class_or_enum == 1)
25729 associate_classtype_constraints (decl_specifiers.type);
25731 decl = shadow_tag (&decl_specifiers);
25733 /* In this case:
25735 struct C {
25736 friend template <typename T> struct A<T>::B;
25739 A<T>::B will be represented by a TYPENAME_TYPE, and
25740 therefore not recognized by shadow_tag. */
25741 if (friend_p && *friend_p
25742 && !decl
25743 && decl_specifiers.type
25744 && TYPE_P (decl_specifiers.type))
25745 decl = decl_specifiers.type;
25747 if (decl && decl != error_mark_node)
25748 decl = TYPE_NAME (decl);
25749 else
25750 decl = error_mark_node;
25752 /* Perform access checks for template parameters. */
25753 cp_parser_perform_template_parameter_access_checks (checks);
25755 /* Give a helpful diagnostic for
25756 template <class T> struct A { } a;
25757 if we aren't already recovering from an error. */
25758 if (!cp_parser_declares_only_class_p (parser)
25759 && !seen_error ())
25761 error_at (cp_lexer_peek_token (parser->lexer)->location,
25762 "a class template declaration must not declare "
25763 "anything else");
25764 cp_parser_skip_to_end_of_block_or_statement (parser);
25765 goto out;
25770 /* Complain about missing 'typename' or other invalid type names. */
25771 if (!decl_specifiers.any_type_specifiers_p
25772 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
25774 /* cp_parser_parse_and_diagnose_invalid_type_name calls
25775 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
25776 the rest of this declaration. */
25777 decl = error_mark_node;
25778 goto out;
25781 /* If it's not a template class, try for a template function. If
25782 the next token is a `;', then this declaration does not declare
25783 anything. But, if there were errors in the decl-specifiers, then
25784 the error might well have come from an attempted class-specifier.
25785 In that case, there's no need to warn about a missing declarator. */
25786 if (!decl
25787 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
25788 || decl_specifiers.type != error_mark_node))
25790 decl = cp_parser_init_declarator (parser,
25791 &decl_specifiers,
25792 checks,
25793 /*function_definition_allowed_p=*/true,
25794 member_p,
25795 declares_class_or_enum,
25796 &function_definition_p,
25797 NULL, NULL);
25799 /* 7.1.1-1 [dcl.stc]
25801 A storage-class-specifier shall not be specified in an explicit
25802 specialization... */
25803 if (decl
25804 && explicit_specialization_p
25805 && decl_specifiers.storage_class != sc_none)
25807 error_at (decl_spec_token_start->location,
25808 "explicit template specialization cannot have a storage class");
25809 decl = error_mark_node;
25812 if (decl && VAR_P (decl))
25813 check_template_variable (decl);
25816 /* Look for a trailing `;' after the declaration. */
25817 if (!function_definition_p
25818 && (decl == error_mark_node
25819 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
25820 cp_parser_skip_to_end_of_block_or_statement (parser);
25822 out:
25823 pop_deferring_access_checks ();
25825 /* Clear any current qualification; whatever comes next is the start
25826 of something new. */
25827 parser->scope = NULL_TREE;
25828 parser->qualifying_scope = NULL_TREE;
25829 parser->object_scope = NULL_TREE;
25831 return decl;
25834 /* Parse a cast-expression that is not the operand of a unary "&". */
25836 static cp_expr
25837 cp_parser_simple_cast_expression (cp_parser *parser)
25839 return cp_parser_cast_expression (parser, /*address_p=*/false,
25840 /*cast_p=*/false, /*decltype*/false, NULL);
25843 /* Parse a functional cast to TYPE. Returns an expression
25844 representing the cast. */
25846 static cp_expr
25847 cp_parser_functional_cast (cp_parser* parser, tree type)
25849 vec<tree, va_gc> *vec;
25850 tree expression_list;
25851 cp_expr cast;
25852 bool nonconst_p;
25854 location_t start_loc = input_location;
25856 if (!type)
25857 type = error_mark_node;
25859 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25861 cp_lexer_set_source_position (parser->lexer);
25862 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25863 expression_list = cp_parser_braced_list (parser, &nonconst_p);
25864 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
25865 if (TREE_CODE (type) == TYPE_DECL)
25866 type = TREE_TYPE (type);
25868 cast = finish_compound_literal (type, expression_list,
25869 tf_warning_or_error);
25870 /* Create a location of the form:
25871 type_name{i, f}
25872 ^~~~~~~~~~~~~~~
25873 with caret == start at the start of the type name,
25874 finishing at the closing brace. */
25875 location_t finish_loc
25876 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
25877 location_t combined_loc = make_location (start_loc, start_loc,
25878 finish_loc);
25879 cast.set_location (combined_loc);
25880 return cast;
25884 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
25885 /*cast_p=*/true,
25886 /*allow_expansion_p=*/true,
25887 /*non_constant_p=*/NULL);
25888 if (vec == NULL)
25889 expression_list = error_mark_node;
25890 else
25892 expression_list = build_tree_list_vec (vec);
25893 release_tree_vector (vec);
25896 cast = build_functional_cast (type, expression_list,
25897 tf_warning_or_error);
25898 /* [expr.const]/1: In an integral constant expression "only type
25899 conversions to integral or enumeration type can be used". */
25900 if (TREE_CODE (type) == TYPE_DECL)
25901 type = TREE_TYPE (type);
25902 if (cast != error_mark_node
25903 && !cast_valid_in_integral_constant_expression_p (type)
25904 && cp_parser_non_integral_constant_expression (parser,
25905 NIC_CONSTRUCTOR))
25906 return error_mark_node;
25908 /* Create a location of the form:
25909 float(i)
25910 ^~~~~~~~
25911 with caret == start at the start of the type name,
25912 finishing at the closing paren. */
25913 location_t finish_loc
25914 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
25915 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
25916 cast.set_location (combined_loc);
25917 return cast;
25920 /* Save the tokens that make up the body of a member function defined
25921 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
25922 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
25923 specifiers applied to the declaration. Returns the FUNCTION_DECL
25924 for the member function. */
25926 static tree
25927 cp_parser_save_member_function_body (cp_parser* parser,
25928 cp_decl_specifier_seq *decl_specifiers,
25929 cp_declarator *declarator,
25930 tree attributes)
25932 cp_token *first;
25933 cp_token *last;
25934 tree fn;
25936 /* Create the FUNCTION_DECL. */
25937 fn = grokmethod (decl_specifiers, declarator, attributes);
25938 cp_finalize_omp_declare_simd (parser, fn);
25939 cp_finalize_oacc_routine (parser, fn, true);
25940 /* If something went badly wrong, bail out now. */
25941 if (fn == error_mark_node)
25943 /* If there's a function-body, skip it. */
25944 if (cp_parser_token_starts_function_definition_p
25945 (cp_lexer_peek_token (parser->lexer)))
25946 cp_parser_skip_to_end_of_block_or_statement (parser);
25947 return error_mark_node;
25950 /* Remember it, if there default args to post process. */
25951 cp_parser_save_default_args (parser, fn);
25953 /* Save away the tokens that make up the body of the
25954 function. */
25955 first = parser->lexer->next_token;
25956 /* Handle function try blocks. */
25957 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
25958 cp_lexer_consume_token (parser->lexer);
25959 /* We can have braced-init-list mem-initializers before the fn body. */
25960 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
25962 cp_lexer_consume_token (parser->lexer);
25963 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
25965 /* cache_group will stop after an un-nested { } pair, too. */
25966 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
25967 break;
25969 /* variadic mem-inits have ... after the ')'. */
25970 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25971 cp_lexer_consume_token (parser->lexer);
25974 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25975 /* Handle function try blocks. */
25976 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
25977 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25978 last = parser->lexer->next_token;
25980 /* Save away the inline definition; we will process it when the
25981 class is complete. */
25982 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
25983 DECL_PENDING_INLINE_P (fn) = 1;
25985 /* We need to know that this was defined in the class, so that
25986 friend templates are handled correctly. */
25987 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
25989 /* Add FN to the queue of functions to be parsed later. */
25990 vec_safe_push (unparsed_funs_with_definitions, fn);
25992 return fn;
25995 /* Save the tokens that make up the in-class initializer for a non-static
25996 data member. Returns a DEFAULT_ARG. */
25998 static tree
25999 cp_parser_save_nsdmi (cp_parser* parser)
26001 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
26004 /* Parse a template-argument-list, as well as the trailing ">" (but
26005 not the opening "<"). See cp_parser_template_argument_list for the
26006 return value. */
26008 static tree
26009 cp_parser_enclosed_template_argument_list (cp_parser* parser)
26011 tree arguments;
26012 tree saved_scope;
26013 tree saved_qualifying_scope;
26014 tree saved_object_scope;
26015 bool saved_greater_than_is_operator_p;
26016 int saved_unevaluated_operand;
26017 int saved_inhibit_evaluation_warnings;
26019 /* [temp.names]
26021 When parsing a template-id, the first non-nested `>' is taken as
26022 the end of the template-argument-list rather than a greater-than
26023 operator. */
26024 saved_greater_than_is_operator_p
26025 = parser->greater_than_is_operator_p;
26026 parser->greater_than_is_operator_p = false;
26027 /* Parsing the argument list may modify SCOPE, so we save it
26028 here. */
26029 saved_scope = parser->scope;
26030 saved_qualifying_scope = parser->qualifying_scope;
26031 saved_object_scope = parser->object_scope;
26032 /* We need to evaluate the template arguments, even though this
26033 template-id may be nested within a "sizeof". */
26034 saved_unevaluated_operand = cp_unevaluated_operand;
26035 cp_unevaluated_operand = 0;
26036 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
26037 c_inhibit_evaluation_warnings = 0;
26038 /* Parse the template-argument-list itself. */
26039 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
26040 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26041 arguments = NULL_TREE;
26042 else
26043 arguments = cp_parser_template_argument_list (parser);
26044 /* Look for the `>' that ends the template-argument-list. If we find
26045 a '>>' instead, it's probably just a typo. */
26046 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26048 if (cxx_dialect != cxx98)
26050 /* In C++0x, a `>>' in a template argument list or cast
26051 expression is considered to be two separate `>'
26052 tokens. So, change the current token to a `>', but don't
26053 consume it: it will be consumed later when the outer
26054 template argument list (or cast expression) is parsed.
26055 Note that this replacement of `>' for `>>' is necessary
26056 even if we are parsing tentatively: in the tentative
26057 case, after calling
26058 cp_parser_enclosed_template_argument_list we will always
26059 throw away all of the template arguments and the first
26060 closing `>', either because the template argument list
26061 was erroneous or because we are replacing those tokens
26062 with a CPP_TEMPLATE_ID token. The second `>' (which will
26063 not have been thrown away) is needed either to close an
26064 outer template argument list or to complete a new-style
26065 cast. */
26066 cp_token *token = cp_lexer_peek_token (parser->lexer);
26067 token->type = CPP_GREATER;
26069 else if (!saved_greater_than_is_operator_p)
26071 /* If we're in a nested template argument list, the '>>' has
26072 to be a typo for '> >'. We emit the error message, but we
26073 continue parsing and we push a '>' as next token, so that
26074 the argument list will be parsed correctly. Note that the
26075 global source location is still on the token before the
26076 '>>', so we need to say explicitly where we want it. */
26077 cp_token *token = cp_lexer_peek_token (parser->lexer);
26078 error_at (token->location, "%<>>%> should be %<> >%> "
26079 "within a nested template argument list");
26081 token->type = CPP_GREATER;
26083 else
26085 /* If this is not a nested template argument list, the '>>'
26086 is a typo for '>'. Emit an error message and continue.
26087 Same deal about the token location, but here we can get it
26088 right by consuming the '>>' before issuing the diagnostic. */
26089 cp_token *token = cp_lexer_consume_token (parser->lexer);
26090 error_at (token->location,
26091 "spurious %<>>%>, use %<>%> to terminate "
26092 "a template argument list");
26095 else
26096 cp_parser_skip_to_end_of_template_parameter_list (parser);
26097 /* The `>' token might be a greater-than operator again now. */
26098 parser->greater_than_is_operator_p
26099 = saved_greater_than_is_operator_p;
26100 /* Restore the SAVED_SCOPE. */
26101 parser->scope = saved_scope;
26102 parser->qualifying_scope = saved_qualifying_scope;
26103 parser->object_scope = saved_object_scope;
26104 cp_unevaluated_operand = saved_unevaluated_operand;
26105 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
26107 return arguments;
26110 /* MEMBER_FUNCTION is a member function, or a friend. If default
26111 arguments, or the body of the function have not yet been parsed,
26112 parse them now. */
26114 static void
26115 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
26117 timevar_push (TV_PARSE_INMETH);
26118 /* If this member is a template, get the underlying
26119 FUNCTION_DECL. */
26120 if (DECL_FUNCTION_TEMPLATE_P (member_function))
26121 member_function = DECL_TEMPLATE_RESULT (member_function);
26123 /* There should not be any class definitions in progress at this
26124 point; the bodies of members are only parsed outside of all class
26125 definitions. */
26126 gcc_assert (parser->num_classes_being_defined == 0);
26127 /* While we're parsing the member functions we might encounter more
26128 classes. We want to handle them right away, but we don't want
26129 them getting mixed up with functions that are currently in the
26130 queue. */
26131 push_unparsed_function_queues (parser);
26133 /* Make sure that any template parameters are in scope. */
26134 maybe_begin_member_template_processing (member_function);
26136 /* If the body of the function has not yet been parsed, parse it
26137 now. */
26138 if (DECL_PENDING_INLINE_P (member_function))
26140 tree function_scope;
26141 cp_token_cache *tokens;
26143 /* The function is no longer pending; we are processing it. */
26144 tokens = DECL_PENDING_INLINE_INFO (member_function);
26145 DECL_PENDING_INLINE_INFO (member_function) = NULL;
26146 DECL_PENDING_INLINE_P (member_function) = 0;
26148 /* If this is a local class, enter the scope of the containing
26149 function. */
26150 function_scope = current_function_decl;
26151 if (function_scope)
26152 push_function_context ();
26154 /* Push the body of the function onto the lexer stack. */
26155 cp_parser_push_lexer_for_tokens (parser, tokens);
26157 /* Let the front end know that we going to be defining this
26158 function. */
26159 start_preparsed_function (member_function, NULL_TREE,
26160 SF_PRE_PARSED | SF_INCLASS_INLINE);
26162 /* Don't do access checking if it is a templated function. */
26163 if (processing_template_decl)
26164 push_deferring_access_checks (dk_no_check);
26166 /* #pragma omp declare reduction needs special parsing. */
26167 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
26169 parser->lexer->in_pragma = true;
26170 cp_parser_omp_declare_reduction_exprs (member_function, parser);
26171 finish_function (/*inline*/2);
26172 cp_check_omp_declare_reduction (member_function);
26174 else
26175 /* Now, parse the body of the function. */
26176 cp_parser_function_definition_after_declarator (parser,
26177 /*inline_p=*/true);
26179 if (processing_template_decl)
26180 pop_deferring_access_checks ();
26182 /* Leave the scope of the containing function. */
26183 if (function_scope)
26184 pop_function_context ();
26185 cp_parser_pop_lexer (parser);
26188 /* Remove any template parameters from the symbol table. */
26189 maybe_end_member_template_processing ();
26191 /* Restore the queue. */
26192 pop_unparsed_function_queues (parser);
26193 timevar_pop (TV_PARSE_INMETH);
26196 /* If DECL contains any default args, remember it on the unparsed
26197 functions queue. */
26199 static void
26200 cp_parser_save_default_args (cp_parser* parser, tree decl)
26202 tree probe;
26204 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
26205 probe;
26206 probe = TREE_CHAIN (probe))
26207 if (TREE_PURPOSE (probe))
26209 cp_default_arg_entry entry = {current_class_type, decl};
26210 vec_safe_push (unparsed_funs_with_default_args, entry);
26211 break;
26215 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
26216 which is either a FIELD_DECL or PARM_DECL. Parse it and return
26217 the result. For a PARM_DECL, PARMTYPE is the corresponding type
26218 from the parameter-type-list. */
26220 static tree
26221 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
26222 tree default_arg, tree parmtype)
26224 cp_token_cache *tokens;
26225 tree parsed_arg;
26226 bool dummy;
26228 if (default_arg == error_mark_node)
26229 return error_mark_node;
26231 /* Push the saved tokens for the default argument onto the parser's
26232 lexer stack. */
26233 tokens = DEFARG_TOKENS (default_arg);
26234 cp_parser_push_lexer_for_tokens (parser, tokens);
26236 start_lambda_scope (decl);
26238 /* Parse the default argument. */
26239 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
26240 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
26241 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26243 finish_lambda_scope ();
26245 if (parsed_arg == error_mark_node)
26246 cp_parser_skip_to_end_of_statement (parser);
26248 if (!processing_template_decl)
26250 /* In a non-template class, check conversions now. In a template,
26251 we'll wait and instantiate these as needed. */
26252 if (TREE_CODE (decl) == PARM_DECL)
26253 parsed_arg = check_default_argument (parmtype, parsed_arg,
26254 tf_warning_or_error);
26255 else
26256 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
26259 /* If the token stream has not been completely used up, then
26260 there was extra junk after the end of the default
26261 argument. */
26262 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26264 if (TREE_CODE (decl) == PARM_DECL)
26265 cp_parser_error (parser, "expected %<,%>");
26266 else
26267 cp_parser_error (parser, "expected %<;%>");
26270 /* Revert to the main lexer. */
26271 cp_parser_pop_lexer (parser);
26273 return parsed_arg;
26276 /* FIELD is a non-static data member with an initializer which we saved for
26277 later; parse it now. */
26279 static void
26280 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
26282 tree def;
26284 maybe_begin_member_template_processing (field);
26286 push_unparsed_function_queues (parser);
26287 def = cp_parser_late_parse_one_default_arg (parser, field,
26288 DECL_INITIAL (field),
26289 NULL_TREE);
26290 pop_unparsed_function_queues (parser);
26292 maybe_end_member_template_processing ();
26294 DECL_INITIAL (field) = def;
26297 /* FN is a FUNCTION_DECL which may contains a parameter with an
26298 unparsed DEFAULT_ARG. Parse the default args now. This function
26299 assumes that the current scope is the scope in which the default
26300 argument should be processed. */
26302 static void
26303 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
26305 bool saved_local_variables_forbidden_p;
26306 tree parm, parmdecl;
26308 /* While we're parsing the default args, we might (due to the
26309 statement expression extension) encounter more classes. We want
26310 to handle them right away, but we don't want them getting mixed
26311 up with default args that are currently in the queue. */
26312 push_unparsed_function_queues (parser);
26314 /* Local variable names (and the `this' keyword) may not appear
26315 in a default argument. */
26316 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
26317 parser->local_variables_forbidden_p = true;
26319 push_defarg_context (fn);
26321 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
26322 parmdecl = DECL_ARGUMENTS (fn);
26323 parm && parm != void_list_node;
26324 parm = TREE_CHAIN (parm),
26325 parmdecl = DECL_CHAIN (parmdecl))
26327 tree default_arg = TREE_PURPOSE (parm);
26328 tree parsed_arg;
26329 vec<tree, va_gc> *insts;
26330 tree copy;
26331 unsigned ix;
26333 if (!default_arg)
26334 continue;
26336 if (TREE_CODE (default_arg) != DEFAULT_ARG)
26337 /* This can happen for a friend declaration for a function
26338 already declared with default arguments. */
26339 continue;
26341 parsed_arg
26342 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
26343 default_arg,
26344 TREE_VALUE (parm));
26345 if (parsed_arg == error_mark_node)
26347 continue;
26350 TREE_PURPOSE (parm) = parsed_arg;
26352 /* Update any instantiations we've already created. */
26353 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
26354 vec_safe_iterate (insts, ix, &copy); ix++)
26355 TREE_PURPOSE (copy) = parsed_arg;
26358 pop_defarg_context ();
26360 /* Make sure no default arg is missing. */
26361 check_default_args (fn);
26363 /* Restore the state of local_variables_forbidden_p. */
26364 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
26366 /* Restore the queue. */
26367 pop_unparsed_function_queues (parser);
26370 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
26372 sizeof ... ( identifier )
26374 where the 'sizeof' token has already been consumed. */
26376 static tree
26377 cp_parser_sizeof_pack (cp_parser *parser)
26379 /* Consume the `...'. */
26380 cp_lexer_consume_token (parser->lexer);
26381 maybe_warn_variadic_templates ();
26383 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
26384 if (paren)
26385 cp_lexer_consume_token (parser->lexer);
26386 else
26387 permerror (cp_lexer_peek_token (parser->lexer)->location,
26388 "%<sizeof...%> argument must be surrounded by parentheses");
26390 cp_token *token = cp_lexer_peek_token (parser->lexer);
26391 tree name = cp_parser_identifier (parser);
26392 if (name == error_mark_node)
26393 return error_mark_node;
26394 /* The name is not qualified. */
26395 parser->scope = NULL_TREE;
26396 parser->qualifying_scope = NULL_TREE;
26397 parser->object_scope = NULL_TREE;
26398 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
26399 if (expr == error_mark_node)
26400 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
26401 token->location);
26402 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
26403 expr = TREE_TYPE (expr);
26404 else if (TREE_CODE (expr) == CONST_DECL)
26405 expr = DECL_INITIAL (expr);
26406 expr = make_pack_expansion (expr);
26407 PACK_EXPANSION_SIZEOF_P (expr) = true;
26409 if (paren)
26410 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26412 return expr;
26415 /* Parse the operand of `sizeof' (or a similar operator). Returns
26416 either a TYPE or an expression, depending on the form of the
26417 input. The KEYWORD indicates which kind of expression we have
26418 encountered. */
26420 static tree
26421 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
26423 tree expr = NULL_TREE;
26424 const char *saved_message;
26425 char *tmp;
26426 bool saved_integral_constant_expression_p;
26427 bool saved_non_integral_constant_expression_p;
26429 /* If it's a `...', then we are computing the length of a parameter
26430 pack. */
26431 if (keyword == RID_SIZEOF
26432 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26433 return cp_parser_sizeof_pack (parser);
26435 /* Types cannot be defined in a `sizeof' expression. Save away the
26436 old message. */
26437 saved_message = parser->type_definition_forbidden_message;
26438 /* And create the new one. */
26439 tmp = concat ("types may not be defined in %<",
26440 IDENTIFIER_POINTER (ridpointers[keyword]),
26441 "%> expressions", NULL);
26442 parser->type_definition_forbidden_message = tmp;
26444 /* The restrictions on constant-expressions do not apply inside
26445 sizeof expressions. */
26446 saved_integral_constant_expression_p
26447 = parser->integral_constant_expression_p;
26448 saved_non_integral_constant_expression_p
26449 = parser->non_integral_constant_expression_p;
26450 parser->integral_constant_expression_p = false;
26452 /* Do not actually evaluate the expression. */
26453 ++cp_unevaluated_operand;
26454 ++c_inhibit_evaluation_warnings;
26455 /* If it's a `(', then we might be looking at the type-id
26456 construction. */
26457 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26459 tree type = NULL_TREE;
26461 /* We can't be sure yet whether we're looking at a type-id or an
26462 expression. */
26463 cp_parser_parse_tentatively (parser);
26464 /* Note: as a GNU Extension, compound literals are considered
26465 postfix-expressions as they are in C99, so they are valid
26466 arguments to sizeof. See comment in cp_parser_cast_expression
26467 for details. */
26468 if (cp_parser_compound_literal_p (parser))
26469 cp_parser_simulate_error (parser);
26470 else
26472 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
26473 parser->in_type_id_in_expr_p = true;
26474 /* Look for the type-id. */
26475 type = cp_parser_type_id (parser);
26476 /* Look for the closing `)'. */
26477 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26478 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
26481 /* If all went well, then we're done. */
26482 if (cp_parser_parse_definitely (parser))
26484 cp_decl_specifier_seq decl_specs;
26486 /* Build a trivial decl-specifier-seq. */
26487 clear_decl_specs (&decl_specs);
26488 decl_specs.type = type;
26490 /* Call grokdeclarator to figure out what type this is. */
26491 expr = grokdeclarator (NULL,
26492 &decl_specs,
26493 TYPENAME,
26494 /*initialized=*/0,
26495 /*attrlist=*/NULL);
26499 /* If the type-id production did not work out, then we must be
26500 looking at the unary-expression production. */
26501 if (!expr)
26502 expr = cp_parser_unary_expression (parser);
26504 /* Go back to evaluating expressions. */
26505 --cp_unevaluated_operand;
26506 --c_inhibit_evaluation_warnings;
26508 /* Free the message we created. */
26509 free (tmp);
26510 /* And restore the old one. */
26511 parser->type_definition_forbidden_message = saved_message;
26512 parser->integral_constant_expression_p
26513 = saved_integral_constant_expression_p;
26514 parser->non_integral_constant_expression_p
26515 = saved_non_integral_constant_expression_p;
26517 return expr;
26520 /* If the current declaration has no declarator, return true. */
26522 static bool
26523 cp_parser_declares_only_class_p (cp_parser *parser)
26525 /* If the next token is a `;' or a `,' then there is no
26526 declarator. */
26527 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26528 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
26531 /* Update the DECL_SPECS to reflect the storage class indicated by
26532 KEYWORD. */
26534 static void
26535 cp_parser_set_storage_class (cp_parser *parser,
26536 cp_decl_specifier_seq *decl_specs,
26537 enum rid keyword,
26538 cp_token *token)
26540 cp_storage_class storage_class;
26542 if (parser->in_unbraced_linkage_specification_p)
26544 error_at (token->location, "invalid use of %qD in linkage specification",
26545 ridpointers[keyword]);
26546 return;
26548 else if (decl_specs->storage_class != sc_none)
26550 decl_specs->conflicting_specifiers_p = true;
26551 return;
26554 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
26555 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
26556 && decl_specs->gnu_thread_keyword_p)
26558 pedwarn (decl_specs->locations[ds_thread], 0,
26559 "%<__thread%> before %qD", ridpointers[keyword]);
26562 switch (keyword)
26564 case RID_AUTO:
26565 storage_class = sc_auto;
26566 break;
26567 case RID_REGISTER:
26568 storage_class = sc_register;
26569 break;
26570 case RID_STATIC:
26571 storage_class = sc_static;
26572 break;
26573 case RID_EXTERN:
26574 storage_class = sc_extern;
26575 break;
26576 case RID_MUTABLE:
26577 storage_class = sc_mutable;
26578 break;
26579 default:
26580 gcc_unreachable ();
26582 decl_specs->storage_class = storage_class;
26583 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
26585 /* A storage class specifier cannot be applied alongside a typedef
26586 specifier. If there is a typedef specifier present then set
26587 conflicting_specifiers_p which will trigger an error later
26588 on in grokdeclarator. */
26589 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
26590 decl_specs->conflicting_specifiers_p = true;
26593 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
26594 is true, the type is a class or enum definition. */
26596 static void
26597 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
26598 tree type_spec,
26599 cp_token *token,
26600 bool type_definition_p)
26602 decl_specs->any_specifiers_p = true;
26604 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
26605 (with, for example, in "typedef int wchar_t;") we remember that
26606 this is what happened. In system headers, we ignore these
26607 declarations so that G++ can work with system headers that are not
26608 C++-safe. */
26609 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
26610 && !type_definition_p
26611 && (type_spec == boolean_type_node
26612 || type_spec == char16_type_node
26613 || type_spec == char32_type_node
26614 || type_spec == wchar_type_node)
26615 && (decl_specs->type
26616 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
26617 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
26618 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
26619 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
26621 decl_specs->redefined_builtin_type = type_spec;
26622 set_and_check_decl_spec_loc (decl_specs,
26623 ds_redefined_builtin_type_spec,
26624 token);
26625 if (!decl_specs->type)
26627 decl_specs->type = type_spec;
26628 decl_specs->type_definition_p = false;
26629 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
26632 else if (decl_specs->type)
26633 decl_specs->multiple_types_p = true;
26634 else
26636 decl_specs->type = type_spec;
26637 decl_specs->type_definition_p = type_definition_p;
26638 decl_specs->redefined_builtin_type = NULL_TREE;
26639 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
26643 /* True iff TOKEN is the GNU keyword __thread. */
26645 static bool
26646 token_is__thread (cp_token *token)
26648 gcc_assert (token->keyword == RID_THREAD);
26649 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
26652 /* Set the location for a declarator specifier and check if it is
26653 duplicated.
26655 DECL_SPECS is the sequence of declarator specifiers onto which to
26656 set the location.
26658 DS is the single declarator specifier to set which location is to
26659 be set onto the existing sequence of declarators.
26661 LOCATION is the location for the declarator specifier to
26662 consider. */
26664 static void
26665 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
26666 cp_decl_spec ds, cp_token *token)
26668 gcc_assert (ds < ds_last);
26670 if (decl_specs == NULL)
26671 return;
26673 source_location location = token->location;
26675 if (decl_specs->locations[ds] == 0)
26677 decl_specs->locations[ds] = location;
26678 if (ds == ds_thread)
26679 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
26681 else
26683 if (ds == ds_long)
26685 if (decl_specs->locations[ds_long_long] != 0)
26686 error_at (location,
26687 "%<long long long%> is too long for GCC");
26688 else
26690 decl_specs->locations[ds_long_long] = location;
26691 pedwarn_cxx98 (location,
26692 OPT_Wlong_long,
26693 "ISO C++ 1998 does not support %<long long%>");
26696 else if (ds == ds_thread)
26698 bool gnu = token_is__thread (token);
26699 if (gnu != decl_specs->gnu_thread_keyword_p)
26700 error_at (location,
26701 "both %<__thread%> and %<thread_local%> specified");
26702 else
26703 error_at (location, "duplicate %qD", token->u.value);
26705 else
26707 static const char *const decl_spec_names[] = {
26708 "signed",
26709 "unsigned",
26710 "short",
26711 "long",
26712 "const",
26713 "volatile",
26714 "restrict",
26715 "inline",
26716 "virtual",
26717 "explicit",
26718 "friend",
26719 "typedef",
26720 "using",
26721 "constexpr",
26722 "__complex"
26724 error_at (location,
26725 "duplicate %qs", decl_spec_names[ds]);
26730 /* Return true iff the declarator specifier DS is present in the
26731 sequence of declarator specifiers DECL_SPECS. */
26733 bool
26734 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
26735 cp_decl_spec ds)
26737 gcc_assert (ds < ds_last);
26739 if (decl_specs == NULL)
26740 return false;
26742 return decl_specs->locations[ds] != 0;
26745 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
26746 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
26748 static bool
26749 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
26751 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
26754 /* Issue an error message indicating that TOKEN_DESC was expected.
26755 If KEYWORD is true, it indicated this function is called by
26756 cp_parser_require_keword and the required token can only be
26757 a indicated keyword. */
26759 static void
26760 cp_parser_required_error (cp_parser *parser,
26761 required_token token_desc,
26762 bool keyword)
26764 switch (token_desc)
26766 case RT_NEW:
26767 cp_parser_error (parser, "expected %<new%>");
26768 return;
26769 case RT_DELETE:
26770 cp_parser_error (parser, "expected %<delete%>");
26771 return;
26772 case RT_RETURN:
26773 cp_parser_error (parser, "expected %<return%>");
26774 return;
26775 case RT_WHILE:
26776 cp_parser_error (parser, "expected %<while%>");
26777 return;
26778 case RT_EXTERN:
26779 cp_parser_error (parser, "expected %<extern%>");
26780 return;
26781 case RT_STATIC_ASSERT:
26782 cp_parser_error (parser, "expected %<static_assert%>");
26783 return;
26784 case RT_DECLTYPE:
26785 cp_parser_error (parser, "expected %<decltype%>");
26786 return;
26787 case RT_OPERATOR:
26788 cp_parser_error (parser, "expected %<operator%>");
26789 return;
26790 case RT_CLASS:
26791 cp_parser_error (parser, "expected %<class%>");
26792 return;
26793 case RT_TEMPLATE:
26794 cp_parser_error (parser, "expected %<template%>");
26795 return;
26796 case RT_NAMESPACE:
26797 cp_parser_error (parser, "expected %<namespace%>");
26798 return;
26799 case RT_USING:
26800 cp_parser_error (parser, "expected %<using%>");
26801 return;
26802 case RT_ASM:
26803 cp_parser_error (parser, "expected %<asm%>");
26804 return;
26805 case RT_TRY:
26806 cp_parser_error (parser, "expected %<try%>");
26807 return;
26808 case RT_CATCH:
26809 cp_parser_error (parser, "expected %<catch%>");
26810 return;
26811 case RT_THROW:
26812 cp_parser_error (parser, "expected %<throw%>");
26813 return;
26814 case RT_LABEL:
26815 cp_parser_error (parser, "expected %<__label__%>");
26816 return;
26817 case RT_AT_TRY:
26818 cp_parser_error (parser, "expected %<@try%>");
26819 return;
26820 case RT_AT_SYNCHRONIZED:
26821 cp_parser_error (parser, "expected %<@synchronized%>");
26822 return;
26823 case RT_AT_THROW:
26824 cp_parser_error (parser, "expected %<@throw%>");
26825 return;
26826 case RT_TRANSACTION_ATOMIC:
26827 cp_parser_error (parser, "expected %<__transaction_atomic%>");
26828 return;
26829 case RT_TRANSACTION_RELAXED:
26830 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
26831 return;
26832 default:
26833 break;
26835 if (!keyword)
26837 switch (token_desc)
26839 case RT_SEMICOLON:
26840 cp_parser_error (parser, "expected %<;%>");
26841 return;
26842 case RT_OPEN_PAREN:
26843 cp_parser_error (parser, "expected %<(%>");
26844 return;
26845 case RT_CLOSE_BRACE:
26846 cp_parser_error (parser, "expected %<}%>");
26847 return;
26848 case RT_OPEN_BRACE:
26849 cp_parser_error (parser, "expected %<{%>");
26850 return;
26851 case RT_CLOSE_SQUARE:
26852 cp_parser_error (parser, "expected %<]%>");
26853 return;
26854 case RT_OPEN_SQUARE:
26855 cp_parser_error (parser, "expected %<[%>");
26856 return;
26857 case RT_COMMA:
26858 cp_parser_error (parser, "expected %<,%>");
26859 return;
26860 case RT_SCOPE:
26861 cp_parser_error (parser, "expected %<::%>");
26862 return;
26863 case RT_LESS:
26864 cp_parser_error (parser, "expected %<<%>");
26865 return;
26866 case RT_GREATER:
26867 cp_parser_error (parser, "expected %<>%>");
26868 return;
26869 case RT_EQ:
26870 cp_parser_error (parser, "expected %<=%>");
26871 return;
26872 case RT_ELLIPSIS:
26873 cp_parser_error (parser, "expected %<...%>");
26874 return;
26875 case RT_MULT:
26876 cp_parser_error (parser, "expected %<*%>");
26877 return;
26878 case RT_COMPL:
26879 cp_parser_error (parser, "expected %<~%>");
26880 return;
26881 case RT_COLON:
26882 cp_parser_error (parser, "expected %<:%>");
26883 return;
26884 case RT_COLON_SCOPE:
26885 cp_parser_error (parser, "expected %<:%> or %<::%>");
26886 return;
26887 case RT_CLOSE_PAREN:
26888 cp_parser_error (parser, "expected %<)%>");
26889 return;
26890 case RT_COMMA_CLOSE_PAREN:
26891 cp_parser_error (parser, "expected %<,%> or %<)%>");
26892 return;
26893 case RT_PRAGMA_EOL:
26894 cp_parser_error (parser, "expected end of line");
26895 return;
26896 case RT_NAME:
26897 cp_parser_error (parser, "expected identifier");
26898 return;
26899 case RT_SELECT:
26900 cp_parser_error (parser, "expected selection-statement");
26901 return;
26902 case RT_INTERATION:
26903 cp_parser_error (parser, "expected iteration-statement");
26904 return;
26905 case RT_JUMP:
26906 cp_parser_error (parser, "expected jump-statement");
26907 return;
26908 case RT_CLASS_KEY:
26909 cp_parser_error (parser, "expected class-key");
26910 return;
26911 case RT_CLASS_TYPENAME_TEMPLATE:
26912 cp_parser_error (parser,
26913 "expected %<class%>, %<typename%>, or %<template%>");
26914 return;
26915 default:
26916 gcc_unreachable ();
26919 else
26920 gcc_unreachable ();
26925 /* If the next token is of the indicated TYPE, consume it. Otherwise,
26926 issue an error message indicating that TOKEN_DESC was expected.
26928 Returns the token consumed, if the token had the appropriate type.
26929 Otherwise, returns NULL. */
26931 static cp_token *
26932 cp_parser_require (cp_parser* parser,
26933 enum cpp_ttype type,
26934 required_token token_desc)
26936 if (cp_lexer_next_token_is (parser->lexer, type))
26937 return cp_lexer_consume_token (parser->lexer);
26938 else
26940 /* Output the MESSAGE -- unless we're parsing tentatively. */
26941 if (!cp_parser_simulate_error (parser))
26942 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
26943 return NULL;
26947 /* An error message is produced if the next token is not '>'.
26948 All further tokens are skipped until the desired token is
26949 found or '{', '}', ';' or an unbalanced ')' or ']'. */
26951 static void
26952 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
26954 /* Current level of '< ... >'. */
26955 unsigned level = 0;
26956 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
26957 unsigned nesting_depth = 0;
26959 /* Are we ready, yet? If not, issue error message. */
26960 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
26961 return;
26963 /* Skip tokens until the desired token is found. */
26964 while (true)
26966 /* Peek at the next token. */
26967 switch (cp_lexer_peek_token (parser->lexer)->type)
26969 case CPP_LESS:
26970 if (!nesting_depth)
26971 ++level;
26972 break;
26974 case CPP_RSHIFT:
26975 if (cxx_dialect == cxx98)
26976 /* C++0x views the `>>' operator as two `>' tokens, but
26977 C++98 does not. */
26978 break;
26979 else if (!nesting_depth && level-- == 0)
26981 /* We've hit a `>>' where the first `>' closes the
26982 template argument list, and the second `>' is
26983 spurious. Just consume the `>>' and stop; we've
26984 already produced at least one error. */
26985 cp_lexer_consume_token (parser->lexer);
26986 return;
26988 /* Fall through for C++0x, so we handle the second `>' in
26989 the `>>'. */
26991 case CPP_GREATER:
26992 if (!nesting_depth && level-- == 0)
26994 /* We've reached the token we want, consume it and stop. */
26995 cp_lexer_consume_token (parser->lexer);
26996 return;
26998 break;
27000 case CPP_OPEN_PAREN:
27001 case CPP_OPEN_SQUARE:
27002 ++nesting_depth;
27003 break;
27005 case CPP_CLOSE_PAREN:
27006 case CPP_CLOSE_SQUARE:
27007 if (nesting_depth-- == 0)
27008 return;
27009 break;
27011 case CPP_EOF:
27012 case CPP_PRAGMA_EOL:
27013 case CPP_SEMICOLON:
27014 case CPP_OPEN_BRACE:
27015 case CPP_CLOSE_BRACE:
27016 /* The '>' was probably forgotten, don't look further. */
27017 return;
27019 default:
27020 break;
27023 /* Consume this token. */
27024 cp_lexer_consume_token (parser->lexer);
27028 /* If the next token is the indicated keyword, consume it. Otherwise,
27029 issue an error message indicating that TOKEN_DESC was expected.
27031 Returns the token consumed, if the token had the appropriate type.
27032 Otherwise, returns NULL. */
27034 static cp_token *
27035 cp_parser_require_keyword (cp_parser* parser,
27036 enum rid keyword,
27037 required_token token_desc)
27039 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
27041 if (token && token->keyword != keyword)
27043 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
27044 return NULL;
27047 return token;
27050 /* Returns TRUE iff TOKEN is a token that can begin the body of a
27051 function-definition. */
27053 static bool
27054 cp_parser_token_starts_function_definition_p (cp_token* token)
27056 return (/* An ordinary function-body begins with an `{'. */
27057 token->type == CPP_OPEN_BRACE
27058 /* A ctor-initializer begins with a `:'. */
27059 || token->type == CPP_COLON
27060 /* A function-try-block begins with `try'. */
27061 || token->keyword == RID_TRY
27062 /* A function-transaction-block begins with `__transaction_atomic'
27063 or `__transaction_relaxed'. */
27064 || token->keyword == RID_TRANSACTION_ATOMIC
27065 || token->keyword == RID_TRANSACTION_RELAXED
27066 /* The named return value extension begins with `return'. */
27067 || token->keyword == RID_RETURN);
27070 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
27071 definition. */
27073 static bool
27074 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
27076 cp_token *token;
27078 token = cp_lexer_peek_token (parser->lexer);
27079 return (token->type == CPP_OPEN_BRACE
27080 || (token->type == CPP_COLON
27081 && !parser->colon_doesnt_start_class_def_p));
27084 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
27085 C++0x) ending a template-argument. */
27087 static bool
27088 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
27090 cp_token *token;
27092 token = cp_lexer_peek_token (parser->lexer);
27093 return (token->type == CPP_COMMA
27094 || token->type == CPP_GREATER
27095 || token->type == CPP_ELLIPSIS
27096 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
27099 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
27100 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
27102 static bool
27103 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
27104 size_t n)
27106 cp_token *token;
27108 token = cp_lexer_peek_nth_token (parser->lexer, n);
27109 if (token->type == CPP_LESS)
27110 return true;
27111 /* Check for the sequence `<::' in the original code. It would be lexed as
27112 `[:', where `[' is a digraph, and there is no whitespace before
27113 `:'. */
27114 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
27116 cp_token *token2;
27117 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
27118 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
27119 return true;
27121 return false;
27124 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
27125 or none_type otherwise. */
27127 static enum tag_types
27128 cp_parser_token_is_class_key (cp_token* token)
27130 switch (token->keyword)
27132 case RID_CLASS:
27133 return class_type;
27134 case RID_STRUCT:
27135 return record_type;
27136 case RID_UNION:
27137 return union_type;
27139 default:
27140 return none_type;
27144 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
27145 or none_type otherwise or if the token is null. */
27147 static enum tag_types
27148 cp_parser_token_is_type_parameter_key (cp_token* token)
27150 if (!token)
27151 return none_type;
27153 switch (token->keyword)
27155 case RID_CLASS:
27156 return class_type;
27157 case RID_TYPENAME:
27158 return typename_type;
27160 default:
27161 return none_type;
27165 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
27167 static void
27168 cp_parser_check_class_key (enum tag_types class_key, tree type)
27170 if (type == error_mark_node)
27171 return;
27172 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
27174 if (permerror (input_location, "%qs tag used in naming %q#T",
27175 class_key == union_type ? "union"
27176 : class_key == record_type ? "struct" : "class",
27177 type))
27178 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
27179 "%q#T was previously declared here", type);
27183 /* Issue an error message if DECL is redeclared with different
27184 access than its original declaration [class.access.spec/3].
27185 This applies to nested classes and nested class templates.
27186 [class.mem/1]. */
27188 static void
27189 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
27191 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
27192 return;
27194 if ((TREE_PRIVATE (decl)
27195 != (current_access_specifier == access_private_node))
27196 || (TREE_PROTECTED (decl)
27197 != (current_access_specifier == access_protected_node)))
27198 error_at (location, "%qD redeclared with different access", decl);
27201 /* Look for the `template' keyword, as a syntactic disambiguator.
27202 Return TRUE iff it is present, in which case it will be
27203 consumed. */
27205 static bool
27206 cp_parser_optional_template_keyword (cp_parser *parser)
27208 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27210 /* In C++98 the `template' keyword can only be used within templates;
27211 outside templates the parser can always figure out what is a
27212 template and what is not. In C++11, per the resolution of DR 468,
27213 `template' is allowed in cases where it is not strictly necessary. */
27214 if (!processing_template_decl
27215 && pedantic && cxx_dialect == cxx98)
27217 cp_token *token = cp_lexer_peek_token (parser->lexer);
27218 pedwarn (token->location, OPT_Wpedantic,
27219 "in C++98 %<template%> (as a disambiguator) is only "
27220 "allowed within templates");
27221 /* If this part of the token stream is rescanned, the same
27222 error message would be generated. So, we purge the token
27223 from the stream. */
27224 cp_lexer_purge_token (parser->lexer);
27225 return false;
27227 else
27229 /* Consume the `template' keyword. */
27230 cp_lexer_consume_token (parser->lexer);
27231 return true;
27234 return false;
27237 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
27238 set PARSER->SCOPE, and perform other related actions. */
27240 static void
27241 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
27243 struct tree_check *check_value;
27245 /* Get the stored value. */
27246 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
27247 /* Set the scope from the stored value. */
27248 parser->scope = saved_checks_value (check_value);
27249 parser->qualifying_scope = check_value->qualifying_scope;
27250 parser->object_scope = NULL_TREE;
27253 /* Consume tokens up through a non-nested END token. Returns TRUE if we
27254 encounter the end of a block before what we were looking for. */
27256 static bool
27257 cp_parser_cache_group (cp_parser *parser,
27258 enum cpp_ttype end,
27259 unsigned depth)
27261 while (true)
27263 cp_token *token = cp_lexer_peek_token (parser->lexer);
27265 /* Abort a parenthesized expression if we encounter a semicolon. */
27266 if ((end == CPP_CLOSE_PAREN || depth == 0)
27267 && token->type == CPP_SEMICOLON)
27268 return true;
27269 /* If we've reached the end of the file, stop. */
27270 if (token->type == CPP_EOF
27271 || (end != CPP_PRAGMA_EOL
27272 && token->type == CPP_PRAGMA_EOL))
27273 return true;
27274 if (token->type == CPP_CLOSE_BRACE && depth == 0)
27275 /* We've hit the end of an enclosing block, so there's been some
27276 kind of syntax error. */
27277 return true;
27279 /* Consume the token. */
27280 cp_lexer_consume_token (parser->lexer);
27281 /* See if it starts a new group. */
27282 if (token->type == CPP_OPEN_BRACE)
27284 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
27285 /* In theory this should probably check end == '}', but
27286 cp_parser_save_member_function_body needs it to exit
27287 after either '}' or ')' when called with ')'. */
27288 if (depth == 0)
27289 return false;
27291 else if (token->type == CPP_OPEN_PAREN)
27293 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
27294 if (depth == 0 && end == CPP_CLOSE_PAREN)
27295 return false;
27297 else if (token->type == CPP_PRAGMA)
27298 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
27299 else if (token->type == end)
27300 return false;
27304 /* Like above, for caching a default argument or NSDMI. Both of these are
27305 terminated by a non-nested comma, but it can be unclear whether or not a
27306 comma is nested in a template argument list unless we do more parsing.
27307 In order to handle this ambiguity, when we encounter a ',' after a '<'
27308 we try to parse what follows as a parameter-declaration-list (in the
27309 case of a default argument) or a member-declarator (in the case of an
27310 NSDMI). If that succeeds, then we stop caching. */
27312 static tree
27313 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
27315 unsigned depth = 0;
27316 int maybe_template_id = 0;
27317 cp_token *first_token;
27318 cp_token *token;
27319 tree default_argument;
27321 /* Add tokens until we have processed the entire default
27322 argument. We add the range [first_token, token). */
27323 first_token = cp_lexer_peek_token (parser->lexer);
27324 if (first_token->type == CPP_OPEN_BRACE)
27326 /* For list-initialization, this is straightforward. */
27327 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27328 token = cp_lexer_peek_token (parser->lexer);
27330 else while (true)
27332 bool done = false;
27334 /* Peek at the next token. */
27335 token = cp_lexer_peek_token (parser->lexer);
27336 /* What we do depends on what token we have. */
27337 switch (token->type)
27339 /* In valid code, a default argument must be
27340 immediately followed by a `,' `)', or `...'. */
27341 case CPP_COMMA:
27342 if (depth == 0 && maybe_template_id)
27344 /* If we've seen a '<', we might be in a
27345 template-argument-list. Until Core issue 325 is
27346 resolved, we don't know how this situation ought
27347 to be handled, so try to DTRT. We check whether
27348 what comes after the comma is a valid parameter
27349 declaration list. If it is, then the comma ends
27350 the default argument; otherwise the default
27351 argument continues. */
27352 bool error = false;
27353 cp_token *peek;
27355 /* Set ITALP so cp_parser_parameter_declaration_list
27356 doesn't decide to commit to this parse. */
27357 bool saved_italp = parser->in_template_argument_list_p;
27358 parser->in_template_argument_list_p = true;
27360 cp_parser_parse_tentatively (parser);
27362 if (nsdmi)
27364 /* Parse declarators until we reach a non-comma or
27365 somthing that cannot be an initializer.
27366 Just checking whether we're looking at a single
27367 declarator is insufficient. Consider:
27368 int var = tuple<T,U>::x;
27369 The template parameter 'U' looks exactly like a
27370 declarator. */
27373 int ctor_dtor_or_conv_p;
27374 cp_lexer_consume_token (parser->lexer);
27375 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27376 &ctor_dtor_or_conv_p,
27377 /*parenthesized_p=*/NULL,
27378 /*member_p=*/true,
27379 /*friend_p=*/false);
27380 peek = cp_lexer_peek_token (parser->lexer);
27381 if (cp_parser_error_occurred (parser))
27382 break;
27384 while (peek->type == CPP_COMMA);
27385 /* If we met an '=' or ';' then the original comma
27386 was the end of the NSDMI. Otherwise assume
27387 we're still in the NSDMI. */
27388 error = (peek->type != CPP_EQ
27389 && peek->type != CPP_SEMICOLON);
27391 else
27393 cp_lexer_consume_token (parser->lexer);
27394 begin_scope (sk_function_parms, NULL_TREE);
27395 cp_parser_parameter_declaration_list (parser, &error);
27396 pop_bindings_and_leave_scope ();
27398 if (!cp_parser_error_occurred (parser) && !error)
27399 done = true;
27400 cp_parser_abort_tentative_parse (parser);
27402 parser->in_template_argument_list_p = saved_italp;
27403 break;
27405 case CPP_CLOSE_PAREN:
27406 case CPP_ELLIPSIS:
27407 /* If we run into a non-nested `;', `}', or `]',
27408 then the code is invalid -- but the default
27409 argument is certainly over. */
27410 case CPP_SEMICOLON:
27411 case CPP_CLOSE_BRACE:
27412 case CPP_CLOSE_SQUARE:
27413 if (depth == 0
27414 /* Handle correctly int n = sizeof ... ( p ); */
27415 && token->type != CPP_ELLIPSIS)
27416 done = true;
27417 /* Update DEPTH, if necessary. */
27418 else if (token->type == CPP_CLOSE_PAREN
27419 || token->type == CPP_CLOSE_BRACE
27420 || token->type == CPP_CLOSE_SQUARE)
27421 --depth;
27422 break;
27424 case CPP_OPEN_PAREN:
27425 case CPP_OPEN_SQUARE:
27426 case CPP_OPEN_BRACE:
27427 ++depth;
27428 break;
27430 case CPP_LESS:
27431 if (depth == 0)
27432 /* This might be the comparison operator, or it might
27433 start a template argument list. */
27434 ++maybe_template_id;
27435 break;
27437 case CPP_RSHIFT:
27438 if (cxx_dialect == cxx98)
27439 break;
27440 /* Fall through for C++0x, which treats the `>>'
27441 operator like two `>' tokens in certain
27442 cases. */
27444 case CPP_GREATER:
27445 if (depth == 0)
27447 /* This might be an operator, or it might close a
27448 template argument list. But if a previous '<'
27449 started a template argument list, this will have
27450 closed it, so we can't be in one anymore. */
27451 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
27452 if (maybe_template_id < 0)
27453 maybe_template_id = 0;
27455 break;
27457 /* If we run out of tokens, issue an error message. */
27458 case CPP_EOF:
27459 case CPP_PRAGMA_EOL:
27460 error_at (token->location, "file ends in default argument");
27461 done = true;
27462 break;
27464 case CPP_NAME:
27465 case CPP_SCOPE:
27466 /* In these cases, we should look for template-ids.
27467 For example, if the default argument is
27468 `X<int, double>()', we need to do name lookup to
27469 figure out whether or not `X' is a template; if
27470 so, the `,' does not end the default argument.
27472 That is not yet done. */
27473 break;
27475 default:
27476 break;
27479 /* If we've reached the end, stop. */
27480 if (done)
27481 break;
27483 /* Add the token to the token block. */
27484 token = cp_lexer_consume_token (parser->lexer);
27487 /* Create a DEFAULT_ARG to represent the unparsed default
27488 argument. */
27489 default_argument = make_node (DEFAULT_ARG);
27490 DEFARG_TOKENS (default_argument)
27491 = cp_token_cache_new (first_token, token);
27492 DEFARG_INSTANTIATIONS (default_argument) = NULL;
27494 return default_argument;
27497 /* Begin parsing tentatively. We always save tokens while parsing
27498 tentatively so that if the tentative parsing fails we can restore the
27499 tokens. */
27501 static void
27502 cp_parser_parse_tentatively (cp_parser* parser)
27504 /* Enter a new parsing context. */
27505 parser->context = cp_parser_context_new (parser->context);
27506 /* Begin saving tokens. */
27507 cp_lexer_save_tokens (parser->lexer);
27508 /* In order to avoid repetitive access control error messages,
27509 access checks are queued up until we are no longer parsing
27510 tentatively. */
27511 push_deferring_access_checks (dk_deferred);
27514 /* Commit to the currently active tentative parse. */
27516 static void
27517 cp_parser_commit_to_tentative_parse (cp_parser* parser)
27519 cp_parser_context *context;
27520 cp_lexer *lexer;
27522 /* Mark all of the levels as committed. */
27523 lexer = parser->lexer;
27524 for (context = parser->context; context->next; context = context->next)
27526 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
27527 break;
27528 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
27529 while (!cp_lexer_saving_tokens (lexer))
27530 lexer = lexer->next;
27531 cp_lexer_commit_tokens (lexer);
27535 /* Commit to the topmost currently active tentative parse.
27537 Note that this function shouldn't be called when there are
27538 irreversible side-effects while in a tentative state. For
27539 example, we shouldn't create a permanent entry in the symbol
27540 table, or issue an error message that might not apply if the
27541 tentative parse is aborted. */
27543 static void
27544 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
27546 cp_parser_context *context = parser->context;
27547 cp_lexer *lexer = parser->lexer;
27549 if (context)
27551 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
27552 return;
27553 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
27555 while (!cp_lexer_saving_tokens (lexer))
27556 lexer = lexer->next;
27557 cp_lexer_commit_tokens (lexer);
27561 /* Abort the currently active tentative parse. All consumed tokens
27562 will be rolled back, and no diagnostics will be issued. */
27564 static void
27565 cp_parser_abort_tentative_parse (cp_parser* parser)
27567 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
27568 || errorcount > 0);
27569 cp_parser_simulate_error (parser);
27570 /* Now, pretend that we want to see if the construct was
27571 successfully parsed. */
27572 cp_parser_parse_definitely (parser);
27575 /* Stop parsing tentatively. If a parse error has occurred, restore the
27576 token stream. Otherwise, commit to the tokens we have consumed.
27577 Returns true if no error occurred; false otherwise. */
27579 static bool
27580 cp_parser_parse_definitely (cp_parser* parser)
27582 bool error_occurred;
27583 cp_parser_context *context;
27585 /* Remember whether or not an error occurred, since we are about to
27586 destroy that information. */
27587 error_occurred = cp_parser_error_occurred (parser);
27588 /* Remove the topmost context from the stack. */
27589 context = parser->context;
27590 parser->context = context->next;
27591 /* If no parse errors occurred, commit to the tentative parse. */
27592 if (!error_occurred)
27594 /* Commit to the tokens read tentatively, unless that was
27595 already done. */
27596 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
27597 cp_lexer_commit_tokens (parser->lexer);
27599 pop_to_parent_deferring_access_checks ();
27601 /* Otherwise, if errors occurred, roll back our state so that things
27602 are just as they were before we began the tentative parse. */
27603 else
27605 cp_lexer_rollback_tokens (parser->lexer);
27606 pop_deferring_access_checks ();
27608 /* Add the context to the front of the free list. */
27609 context->next = cp_parser_context_free_list;
27610 cp_parser_context_free_list = context;
27612 return !error_occurred;
27615 /* Returns true if we are parsing tentatively and are not committed to
27616 this tentative parse. */
27618 static bool
27619 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
27621 return (cp_parser_parsing_tentatively (parser)
27622 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
27625 /* Returns nonzero iff an error has occurred during the most recent
27626 tentative parse. */
27628 static bool
27629 cp_parser_error_occurred (cp_parser* parser)
27631 return (cp_parser_parsing_tentatively (parser)
27632 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
27635 /* Returns nonzero if GNU extensions are allowed. */
27637 static bool
27638 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
27640 return parser->allow_gnu_extensions_p;
27643 /* Objective-C++ Productions */
27646 /* Parse an Objective-C expression, which feeds into a primary-expression
27647 above.
27649 objc-expression:
27650 objc-message-expression
27651 objc-string-literal
27652 objc-encode-expression
27653 objc-protocol-expression
27654 objc-selector-expression
27656 Returns a tree representation of the expression. */
27658 static cp_expr
27659 cp_parser_objc_expression (cp_parser* parser)
27661 /* Try to figure out what kind of declaration is present. */
27662 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27664 switch (kwd->type)
27666 case CPP_OPEN_SQUARE:
27667 return cp_parser_objc_message_expression (parser);
27669 case CPP_OBJC_STRING:
27670 kwd = cp_lexer_consume_token (parser->lexer);
27671 return objc_build_string_object (kwd->u.value);
27673 case CPP_KEYWORD:
27674 switch (kwd->keyword)
27676 case RID_AT_ENCODE:
27677 return cp_parser_objc_encode_expression (parser);
27679 case RID_AT_PROTOCOL:
27680 return cp_parser_objc_protocol_expression (parser);
27682 case RID_AT_SELECTOR:
27683 return cp_parser_objc_selector_expression (parser);
27685 default:
27686 break;
27688 default:
27689 error_at (kwd->location,
27690 "misplaced %<@%D%> Objective-C++ construct",
27691 kwd->u.value);
27692 cp_parser_skip_to_end_of_block_or_statement (parser);
27695 return error_mark_node;
27698 /* Parse an Objective-C message expression.
27700 objc-message-expression:
27701 [ objc-message-receiver objc-message-args ]
27703 Returns a representation of an Objective-C message. */
27705 static tree
27706 cp_parser_objc_message_expression (cp_parser* parser)
27708 tree receiver, messageargs;
27710 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
27711 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
27712 receiver = cp_parser_objc_message_receiver (parser);
27713 messageargs = cp_parser_objc_message_args (parser);
27714 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
27715 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27717 tree result = objc_build_message_expr (receiver, messageargs);
27719 /* Construct a location e.g.
27720 [self func1:5]
27721 ^~~~~~~~~~~~~~
27722 ranging from the '[' to the ']', with the caret at the start. */
27723 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
27724 protected_set_expr_location (result, combined_loc);
27726 return result;
27729 /* Parse an objc-message-receiver.
27731 objc-message-receiver:
27732 expression
27733 simple-type-specifier
27735 Returns a representation of the type or expression. */
27737 static tree
27738 cp_parser_objc_message_receiver (cp_parser* parser)
27740 tree rcv;
27742 /* An Objective-C message receiver may be either (1) a type
27743 or (2) an expression. */
27744 cp_parser_parse_tentatively (parser);
27745 rcv = cp_parser_expression (parser);
27747 /* If that worked out, fine. */
27748 if (cp_parser_parse_definitely (parser))
27749 return rcv;
27751 cp_parser_parse_tentatively (parser);
27752 rcv = cp_parser_simple_type_specifier (parser,
27753 /*decl_specs=*/NULL,
27754 CP_PARSER_FLAGS_NONE);
27756 if (cp_parser_parse_definitely (parser))
27757 return objc_get_class_reference (rcv);
27759 cp_parser_error (parser, "objective-c++ message receiver expected");
27760 return error_mark_node;
27763 /* Parse the arguments and selectors comprising an Objective-C message.
27765 objc-message-args:
27766 objc-selector
27767 objc-selector-args
27768 objc-selector-args , objc-comma-args
27770 objc-selector-args:
27771 objc-selector [opt] : assignment-expression
27772 objc-selector-args objc-selector [opt] : assignment-expression
27774 objc-comma-args:
27775 assignment-expression
27776 objc-comma-args , assignment-expression
27778 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
27779 selector arguments and TREE_VALUE containing a list of comma
27780 arguments. */
27782 static tree
27783 cp_parser_objc_message_args (cp_parser* parser)
27785 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
27786 bool maybe_unary_selector_p = true;
27787 cp_token *token = cp_lexer_peek_token (parser->lexer);
27789 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
27791 tree selector = NULL_TREE, arg;
27793 if (token->type != CPP_COLON)
27794 selector = cp_parser_objc_selector (parser);
27796 /* Detect if we have a unary selector. */
27797 if (maybe_unary_selector_p
27798 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27799 return build_tree_list (selector, NULL_TREE);
27801 maybe_unary_selector_p = false;
27802 cp_parser_require (parser, CPP_COLON, RT_COLON);
27803 arg = cp_parser_assignment_expression (parser);
27805 sel_args
27806 = chainon (sel_args,
27807 build_tree_list (selector, arg));
27809 token = cp_lexer_peek_token (parser->lexer);
27812 /* Handle non-selector arguments, if any. */
27813 while (token->type == CPP_COMMA)
27815 tree arg;
27817 cp_lexer_consume_token (parser->lexer);
27818 arg = cp_parser_assignment_expression (parser);
27820 addl_args
27821 = chainon (addl_args,
27822 build_tree_list (NULL_TREE, arg));
27824 token = cp_lexer_peek_token (parser->lexer);
27827 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
27829 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
27830 return build_tree_list (error_mark_node, error_mark_node);
27833 return build_tree_list (sel_args, addl_args);
27836 /* Parse an Objective-C encode expression.
27838 objc-encode-expression:
27839 @encode objc-typename
27841 Returns an encoded representation of the type argument. */
27843 static cp_expr
27844 cp_parser_objc_encode_expression (cp_parser* parser)
27846 tree type;
27847 cp_token *token;
27848 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
27850 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
27851 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27852 token = cp_lexer_peek_token (parser->lexer);
27853 type = complete_type (cp_parser_type_id (parser));
27854 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27856 if (!type)
27858 error_at (token->location,
27859 "%<@encode%> must specify a type as an argument");
27860 return error_mark_node;
27863 /* This happens if we find @encode(T) (where T is a template
27864 typename or something dependent on a template typename) when
27865 parsing a template. In that case, we can't compile it
27866 immediately, but we rather create an AT_ENCODE_EXPR which will
27867 need to be instantiated when the template is used.
27869 if (dependent_type_p (type))
27871 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
27872 TREE_READONLY (value) = 1;
27873 return value;
27877 /* Build a location of the form:
27878 @encode(int)
27879 ^~~~~~~~~~~~
27880 with caret==start at the @ token, finishing at the close paren. */
27881 location_t combined_loc
27882 = make_location (start_loc, start_loc,
27883 cp_lexer_previous_token (parser->lexer)->location);
27885 return cp_expr (objc_build_encode_expr (type), combined_loc);
27888 /* Parse an Objective-C @defs expression. */
27890 static tree
27891 cp_parser_objc_defs_expression (cp_parser *parser)
27893 tree name;
27895 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
27896 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27897 name = cp_parser_identifier (parser);
27898 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27900 return objc_get_class_ivars (name);
27903 /* Parse an Objective-C protocol expression.
27905 objc-protocol-expression:
27906 @protocol ( identifier )
27908 Returns a representation of the protocol expression. */
27910 static tree
27911 cp_parser_objc_protocol_expression (cp_parser* parser)
27913 tree proto;
27914 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
27916 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
27917 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27918 proto = cp_parser_identifier (parser);
27919 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27921 /* Build a location of the form:
27922 @protocol(prot)
27923 ^~~~~~~~~~~~~~~
27924 with caret==start at the @ token, finishing at the close paren. */
27925 location_t combined_loc
27926 = make_location (start_loc, start_loc,
27927 cp_lexer_previous_token (parser->lexer)->location);
27928 tree result = objc_build_protocol_expr (proto);
27929 protected_set_expr_location (result, combined_loc);
27930 return result;
27933 /* Parse an Objective-C selector expression.
27935 objc-selector-expression:
27936 @selector ( objc-method-signature )
27938 objc-method-signature:
27939 objc-selector
27940 objc-selector-seq
27942 objc-selector-seq:
27943 objc-selector :
27944 objc-selector-seq objc-selector :
27946 Returns a representation of the method selector. */
27948 static tree
27949 cp_parser_objc_selector_expression (cp_parser* parser)
27951 tree sel_seq = NULL_TREE;
27952 bool maybe_unary_selector_p = true;
27953 cp_token *token;
27954 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27956 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
27957 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27958 token = cp_lexer_peek_token (parser->lexer);
27960 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
27961 || token->type == CPP_SCOPE)
27963 tree selector = NULL_TREE;
27965 if (token->type != CPP_COLON
27966 || token->type == CPP_SCOPE)
27967 selector = cp_parser_objc_selector (parser);
27969 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
27970 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
27972 /* Detect if we have a unary selector. */
27973 if (maybe_unary_selector_p)
27975 sel_seq = selector;
27976 goto finish_selector;
27978 else
27980 cp_parser_error (parser, "expected %<:%>");
27983 maybe_unary_selector_p = false;
27984 token = cp_lexer_consume_token (parser->lexer);
27986 if (token->type == CPP_SCOPE)
27988 sel_seq
27989 = chainon (sel_seq,
27990 build_tree_list (selector, NULL_TREE));
27991 sel_seq
27992 = chainon (sel_seq,
27993 build_tree_list (NULL_TREE, NULL_TREE));
27995 else
27996 sel_seq
27997 = chainon (sel_seq,
27998 build_tree_list (selector, NULL_TREE));
28000 token = cp_lexer_peek_token (parser->lexer);
28003 finish_selector:
28004 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28007 /* Build a location of the form:
28008 @selector(func)
28009 ^~~~~~~~~~~~~~~
28010 with caret==start at the @ token, finishing at the close paren. */
28011 location_t combined_loc
28012 = make_location (loc, loc,
28013 cp_lexer_previous_token (parser->lexer)->location);
28014 tree result = objc_build_selector_expr (combined_loc, sel_seq);
28015 /* TODO: objc_build_selector_expr doesn't always honor the location. */
28016 protected_set_expr_location (result, combined_loc);
28017 return result;
28020 /* Parse a list of identifiers.
28022 objc-identifier-list:
28023 identifier
28024 objc-identifier-list , identifier
28026 Returns a TREE_LIST of identifier nodes. */
28028 static tree
28029 cp_parser_objc_identifier_list (cp_parser* parser)
28031 tree identifier;
28032 tree list;
28033 cp_token *sep;
28035 identifier = cp_parser_identifier (parser);
28036 if (identifier == error_mark_node)
28037 return error_mark_node;
28039 list = build_tree_list (NULL_TREE, identifier);
28040 sep = cp_lexer_peek_token (parser->lexer);
28042 while (sep->type == CPP_COMMA)
28044 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28045 identifier = cp_parser_identifier (parser);
28046 if (identifier == error_mark_node)
28047 return list;
28049 list = chainon (list, build_tree_list (NULL_TREE,
28050 identifier));
28051 sep = cp_lexer_peek_token (parser->lexer);
28054 return list;
28057 /* Parse an Objective-C alias declaration.
28059 objc-alias-declaration:
28060 @compatibility_alias identifier identifier ;
28062 This function registers the alias mapping with the Objective-C front end.
28063 It returns nothing. */
28065 static void
28066 cp_parser_objc_alias_declaration (cp_parser* parser)
28068 tree alias, orig;
28070 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
28071 alias = cp_parser_identifier (parser);
28072 orig = cp_parser_identifier (parser);
28073 objc_declare_alias (alias, orig);
28074 cp_parser_consume_semicolon_at_end_of_statement (parser);
28077 /* Parse an Objective-C class forward-declaration.
28079 objc-class-declaration:
28080 @class objc-identifier-list ;
28082 The function registers the forward declarations with the Objective-C
28083 front end. It returns nothing. */
28085 static void
28086 cp_parser_objc_class_declaration (cp_parser* parser)
28088 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
28089 while (true)
28091 tree id;
28093 id = cp_parser_identifier (parser);
28094 if (id == error_mark_node)
28095 break;
28097 objc_declare_class (id);
28099 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28100 cp_lexer_consume_token (parser->lexer);
28101 else
28102 break;
28104 cp_parser_consume_semicolon_at_end_of_statement (parser);
28107 /* Parse a list of Objective-C protocol references.
28109 objc-protocol-refs-opt:
28110 objc-protocol-refs [opt]
28112 objc-protocol-refs:
28113 < objc-identifier-list >
28115 Returns a TREE_LIST of identifiers, if any. */
28117 static tree
28118 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
28120 tree protorefs = NULL_TREE;
28122 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
28124 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
28125 protorefs = cp_parser_objc_identifier_list (parser);
28126 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
28129 return protorefs;
28132 /* Parse a Objective-C visibility specification. */
28134 static void
28135 cp_parser_objc_visibility_spec (cp_parser* parser)
28137 cp_token *vis = cp_lexer_peek_token (parser->lexer);
28139 switch (vis->keyword)
28141 case RID_AT_PRIVATE:
28142 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
28143 break;
28144 case RID_AT_PROTECTED:
28145 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
28146 break;
28147 case RID_AT_PUBLIC:
28148 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
28149 break;
28150 case RID_AT_PACKAGE:
28151 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
28152 break;
28153 default:
28154 return;
28157 /* Eat '@private'/'@protected'/'@public'. */
28158 cp_lexer_consume_token (parser->lexer);
28161 /* Parse an Objective-C method type. Return 'true' if it is a class
28162 (+) method, and 'false' if it is an instance (-) method. */
28164 static inline bool
28165 cp_parser_objc_method_type (cp_parser* parser)
28167 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
28168 return true;
28169 else
28170 return false;
28173 /* Parse an Objective-C protocol qualifier. */
28175 static tree
28176 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
28178 tree quals = NULL_TREE, node;
28179 cp_token *token = cp_lexer_peek_token (parser->lexer);
28181 node = token->u.value;
28183 while (node && identifier_p (node)
28184 && (node == ridpointers [(int) RID_IN]
28185 || node == ridpointers [(int) RID_OUT]
28186 || node == ridpointers [(int) RID_INOUT]
28187 || node == ridpointers [(int) RID_BYCOPY]
28188 || node == ridpointers [(int) RID_BYREF]
28189 || node == ridpointers [(int) RID_ONEWAY]))
28191 quals = tree_cons (NULL_TREE, node, quals);
28192 cp_lexer_consume_token (parser->lexer);
28193 token = cp_lexer_peek_token (parser->lexer);
28194 node = token->u.value;
28197 return quals;
28200 /* Parse an Objective-C typename. */
28202 static tree
28203 cp_parser_objc_typename (cp_parser* parser)
28205 tree type_name = NULL_TREE;
28207 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28209 tree proto_quals, cp_type = NULL_TREE;
28211 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
28212 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
28214 /* An ObjC type name may consist of just protocol qualifiers, in which
28215 case the type shall default to 'id'. */
28216 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
28218 cp_type = cp_parser_type_id (parser);
28220 /* If the type could not be parsed, an error has already
28221 been produced. For error recovery, behave as if it had
28222 not been specified, which will use the default type
28223 'id'. */
28224 if (cp_type == error_mark_node)
28226 cp_type = NULL_TREE;
28227 /* We need to skip to the closing parenthesis as
28228 cp_parser_type_id() does not seem to do it for
28229 us. */
28230 cp_parser_skip_to_closing_parenthesis (parser,
28231 /*recovering=*/true,
28232 /*or_comma=*/false,
28233 /*consume_paren=*/false);
28237 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28238 type_name = build_tree_list (proto_quals, cp_type);
28241 return type_name;
28244 /* Check to see if TYPE refers to an Objective-C selector name. */
28246 static bool
28247 cp_parser_objc_selector_p (enum cpp_ttype type)
28249 return (type == CPP_NAME || type == CPP_KEYWORD
28250 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
28251 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
28252 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
28253 || type == CPP_XOR || type == CPP_XOR_EQ);
28256 /* Parse an Objective-C selector. */
28258 static tree
28259 cp_parser_objc_selector (cp_parser* parser)
28261 cp_token *token = cp_lexer_consume_token (parser->lexer);
28263 if (!cp_parser_objc_selector_p (token->type))
28265 error_at (token->location, "invalid Objective-C++ selector name");
28266 return error_mark_node;
28269 /* C++ operator names are allowed to appear in ObjC selectors. */
28270 switch (token->type)
28272 case CPP_AND_AND: return get_identifier ("and");
28273 case CPP_AND_EQ: return get_identifier ("and_eq");
28274 case CPP_AND: return get_identifier ("bitand");
28275 case CPP_OR: return get_identifier ("bitor");
28276 case CPP_COMPL: return get_identifier ("compl");
28277 case CPP_NOT: return get_identifier ("not");
28278 case CPP_NOT_EQ: return get_identifier ("not_eq");
28279 case CPP_OR_OR: return get_identifier ("or");
28280 case CPP_OR_EQ: return get_identifier ("or_eq");
28281 case CPP_XOR: return get_identifier ("xor");
28282 case CPP_XOR_EQ: return get_identifier ("xor_eq");
28283 default: return token->u.value;
28287 /* Parse an Objective-C params list. */
28289 static tree
28290 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
28292 tree params = NULL_TREE;
28293 bool maybe_unary_selector_p = true;
28294 cp_token *token = cp_lexer_peek_token (parser->lexer);
28296 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
28298 tree selector = NULL_TREE, type_name, identifier;
28299 tree parm_attr = NULL_TREE;
28301 if (token->keyword == RID_ATTRIBUTE)
28302 break;
28304 if (token->type != CPP_COLON)
28305 selector = cp_parser_objc_selector (parser);
28307 /* Detect if we have a unary selector. */
28308 if (maybe_unary_selector_p
28309 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28311 params = selector; /* Might be followed by attributes. */
28312 break;
28315 maybe_unary_selector_p = false;
28316 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28318 /* Something went quite wrong. There should be a colon
28319 here, but there is not. Stop parsing parameters. */
28320 break;
28322 type_name = cp_parser_objc_typename (parser);
28323 /* New ObjC allows attributes on parameters too. */
28324 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
28325 parm_attr = cp_parser_attributes_opt (parser);
28326 identifier = cp_parser_identifier (parser);
28328 params
28329 = chainon (params,
28330 objc_build_keyword_decl (selector,
28331 type_name,
28332 identifier,
28333 parm_attr));
28335 token = cp_lexer_peek_token (parser->lexer);
28338 if (params == NULL_TREE)
28340 cp_parser_error (parser, "objective-c++ method declaration is expected");
28341 return error_mark_node;
28344 /* We allow tail attributes for the method. */
28345 if (token->keyword == RID_ATTRIBUTE)
28347 *attributes = cp_parser_attributes_opt (parser);
28348 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28349 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28350 return params;
28351 cp_parser_error (parser,
28352 "method attributes must be specified at the end");
28353 return error_mark_node;
28356 if (params == NULL_TREE)
28358 cp_parser_error (parser, "objective-c++ method declaration is expected");
28359 return error_mark_node;
28361 return params;
28364 /* Parse the non-keyword Objective-C params. */
28366 static tree
28367 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
28368 tree* attributes)
28370 tree params = make_node (TREE_LIST);
28371 cp_token *token = cp_lexer_peek_token (parser->lexer);
28372 *ellipsisp = false; /* Initially, assume no ellipsis. */
28374 while (token->type == CPP_COMMA)
28376 cp_parameter_declarator *parmdecl;
28377 tree parm;
28379 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28380 token = cp_lexer_peek_token (parser->lexer);
28382 if (token->type == CPP_ELLIPSIS)
28384 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
28385 *ellipsisp = true;
28386 token = cp_lexer_peek_token (parser->lexer);
28387 break;
28390 /* TODO: parse attributes for tail parameters. */
28391 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
28392 parm = grokdeclarator (parmdecl->declarator,
28393 &parmdecl->decl_specifiers,
28394 PARM, /*initialized=*/0,
28395 /*attrlist=*/NULL);
28397 chainon (params, build_tree_list (NULL_TREE, parm));
28398 token = cp_lexer_peek_token (parser->lexer);
28401 /* We allow tail attributes for the method. */
28402 if (token->keyword == RID_ATTRIBUTE)
28404 if (*attributes == NULL_TREE)
28406 *attributes = cp_parser_attributes_opt (parser);
28407 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28408 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28409 return params;
28411 else
28412 /* We have an error, but parse the attributes, so that we can
28413 carry on. */
28414 *attributes = cp_parser_attributes_opt (parser);
28416 cp_parser_error (parser,
28417 "method attributes must be specified at the end");
28418 return error_mark_node;
28421 return params;
28424 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
28426 static void
28427 cp_parser_objc_interstitial_code (cp_parser* parser)
28429 cp_token *token = cp_lexer_peek_token (parser->lexer);
28431 /* If the next token is `extern' and the following token is a string
28432 literal, then we have a linkage specification. */
28433 if (token->keyword == RID_EXTERN
28434 && cp_parser_is_pure_string_literal
28435 (cp_lexer_peek_nth_token (parser->lexer, 2)))
28436 cp_parser_linkage_specification (parser);
28437 /* Handle #pragma, if any. */
28438 else if (token->type == CPP_PRAGMA)
28439 cp_parser_pragma (parser, pragma_objc_icode);
28440 /* Allow stray semicolons. */
28441 else if (token->type == CPP_SEMICOLON)
28442 cp_lexer_consume_token (parser->lexer);
28443 /* Mark methods as optional or required, when building protocols. */
28444 else if (token->keyword == RID_AT_OPTIONAL)
28446 cp_lexer_consume_token (parser->lexer);
28447 objc_set_method_opt (true);
28449 else if (token->keyword == RID_AT_REQUIRED)
28451 cp_lexer_consume_token (parser->lexer);
28452 objc_set_method_opt (false);
28454 else if (token->keyword == RID_NAMESPACE)
28455 cp_parser_namespace_definition (parser);
28456 /* Other stray characters must generate errors. */
28457 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
28459 cp_lexer_consume_token (parser->lexer);
28460 error ("stray %qs between Objective-C++ methods",
28461 token->type == CPP_OPEN_BRACE ? "{" : "}");
28463 /* Finally, try to parse a block-declaration, or a function-definition. */
28464 else
28465 cp_parser_block_declaration (parser, /*statement_p=*/false);
28468 /* Parse a method signature. */
28470 static tree
28471 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
28473 tree rettype, kwdparms, optparms;
28474 bool ellipsis = false;
28475 bool is_class_method;
28477 is_class_method = cp_parser_objc_method_type (parser);
28478 rettype = cp_parser_objc_typename (parser);
28479 *attributes = NULL_TREE;
28480 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
28481 if (kwdparms == error_mark_node)
28482 return error_mark_node;
28483 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
28484 if (optparms == error_mark_node)
28485 return error_mark_node;
28487 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
28490 static bool
28491 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
28493 tree tattr;
28494 cp_lexer_save_tokens (parser->lexer);
28495 tattr = cp_parser_attributes_opt (parser);
28496 gcc_assert (tattr) ;
28498 /* If the attributes are followed by a method introducer, this is not allowed.
28499 Dump the attributes and flag the situation. */
28500 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
28501 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
28502 return true;
28504 /* Otherwise, the attributes introduce some interstitial code, possibly so
28505 rewind to allow that check. */
28506 cp_lexer_rollback_tokens (parser->lexer);
28507 return false;
28510 /* Parse an Objective-C method prototype list. */
28512 static void
28513 cp_parser_objc_method_prototype_list (cp_parser* parser)
28515 cp_token *token = cp_lexer_peek_token (parser->lexer);
28517 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
28519 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
28521 tree attributes, sig;
28522 bool is_class_method;
28523 if (token->type == CPP_PLUS)
28524 is_class_method = true;
28525 else
28526 is_class_method = false;
28527 sig = cp_parser_objc_method_signature (parser, &attributes);
28528 if (sig == error_mark_node)
28530 cp_parser_skip_to_end_of_block_or_statement (parser);
28531 token = cp_lexer_peek_token (parser->lexer);
28532 continue;
28534 objc_add_method_declaration (is_class_method, sig, attributes);
28535 cp_parser_consume_semicolon_at_end_of_statement (parser);
28537 else if (token->keyword == RID_AT_PROPERTY)
28538 cp_parser_objc_at_property_declaration (parser);
28539 else if (token->keyword == RID_ATTRIBUTE
28540 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
28541 warning_at (cp_lexer_peek_token (parser->lexer)->location,
28542 OPT_Wattributes,
28543 "prefix attributes are ignored for methods");
28544 else
28545 /* Allow for interspersed non-ObjC++ code. */
28546 cp_parser_objc_interstitial_code (parser);
28548 token = cp_lexer_peek_token (parser->lexer);
28551 if (token->type != CPP_EOF)
28552 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
28553 else
28554 cp_parser_error (parser, "expected %<@end%>");
28556 objc_finish_interface ();
28559 /* Parse an Objective-C method definition list. */
28561 static void
28562 cp_parser_objc_method_definition_list (cp_parser* parser)
28564 cp_token *token = cp_lexer_peek_token (parser->lexer);
28566 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
28568 tree meth;
28570 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
28572 cp_token *ptk;
28573 tree sig, attribute;
28574 bool is_class_method;
28575 if (token->type == CPP_PLUS)
28576 is_class_method = true;
28577 else
28578 is_class_method = false;
28579 push_deferring_access_checks (dk_deferred);
28580 sig = cp_parser_objc_method_signature (parser, &attribute);
28581 if (sig == error_mark_node)
28583 cp_parser_skip_to_end_of_block_or_statement (parser);
28584 token = cp_lexer_peek_token (parser->lexer);
28585 continue;
28587 objc_start_method_definition (is_class_method, sig, attribute,
28588 NULL_TREE);
28590 /* For historical reasons, we accept an optional semicolon. */
28591 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28592 cp_lexer_consume_token (parser->lexer);
28594 ptk = cp_lexer_peek_token (parser->lexer);
28595 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
28596 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
28598 perform_deferred_access_checks (tf_warning_or_error);
28599 stop_deferring_access_checks ();
28600 meth = cp_parser_function_definition_after_declarator (parser,
28601 false);
28602 pop_deferring_access_checks ();
28603 objc_finish_method_definition (meth);
28606 /* The following case will be removed once @synthesize is
28607 completely implemented. */
28608 else if (token->keyword == RID_AT_PROPERTY)
28609 cp_parser_objc_at_property_declaration (parser);
28610 else if (token->keyword == RID_AT_SYNTHESIZE)
28611 cp_parser_objc_at_synthesize_declaration (parser);
28612 else if (token->keyword == RID_AT_DYNAMIC)
28613 cp_parser_objc_at_dynamic_declaration (parser);
28614 else if (token->keyword == RID_ATTRIBUTE
28615 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
28616 warning_at (token->location, OPT_Wattributes,
28617 "prefix attributes are ignored for methods");
28618 else
28619 /* Allow for interspersed non-ObjC++ code. */
28620 cp_parser_objc_interstitial_code (parser);
28622 token = cp_lexer_peek_token (parser->lexer);
28625 if (token->type != CPP_EOF)
28626 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
28627 else
28628 cp_parser_error (parser, "expected %<@end%>");
28630 objc_finish_implementation ();
28633 /* Parse Objective-C ivars. */
28635 static void
28636 cp_parser_objc_class_ivars (cp_parser* parser)
28638 cp_token *token = cp_lexer_peek_token (parser->lexer);
28640 if (token->type != CPP_OPEN_BRACE)
28641 return; /* No ivars specified. */
28643 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
28644 token = cp_lexer_peek_token (parser->lexer);
28646 while (token->type != CPP_CLOSE_BRACE
28647 && token->keyword != RID_AT_END && token->type != CPP_EOF)
28649 cp_decl_specifier_seq declspecs;
28650 int decl_class_or_enum_p;
28651 tree prefix_attributes;
28653 cp_parser_objc_visibility_spec (parser);
28655 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28656 break;
28658 cp_parser_decl_specifier_seq (parser,
28659 CP_PARSER_FLAGS_OPTIONAL,
28660 &declspecs,
28661 &decl_class_or_enum_p);
28663 /* auto, register, static, extern, mutable. */
28664 if (declspecs.storage_class != sc_none)
28666 cp_parser_error (parser, "invalid type for instance variable");
28667 declspecs.storage_class = sc_none;
28670 /* thread_local. */
28671 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
28673 cp_parser_error (parser, "invalid type for instance variable");
28674 declspecs.locations[ds_thread] = 0;
28677 /* typedef. */
28678 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
28680 cp_parser_error (parser, "invalid type for instance variable");
28681 declspecs.locations[ds_typedef] = 0;
28684 prefix_attributes = declspecs.attributes;
28685 declspecs.attributes = NULL_TREE;
28687 /* Keep going until we hit the `;' at the end of the
28688 declaration. */
28689 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28691 tree width = NULL_TREE, attributes, first_attribute, decl;
28692 cp_declarator *declarator = NULL;
28693 int ctor_dtor_or_conv_p;
28695 /* Check for a (possibly unnamed) bitfield declaration. */
28696 token = cp_lexer_peek_token (parser->lexer);
28697 if (token->type == CPP_COLON)
28698 goto eat_colon;
28700 if (token->type == CPP_NAME
28701 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
28702 == CPP_COLON))
28704 /* Get the name of the bitfield. */
28705 declarator = make_id_declarator (NULL_TREE,
28706 cp_parser_identifier (parser),
28707 sfk_none);
28709 eat_colon:
28710 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
28711 /* Get the width of the bitfield. */
28712 width
28713 = cp_parser_constant_expression (parser);
28715 else
28717 /* Parse the declarator. */
28718 declarator
28719 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28720 &ctor_dtor_or_conv_p,
28721 /*parenthesized_p=*/NULL,
28722 /*member_p=*/false,
28723 /*friend_p=*/false);
28726 /* Look for attributes that apply to the ivar. */
28727 attributes = cp_parser_attributes_opt (parser);
28728 /* Remember which attributes are prefix attributes and
28729 which are not. */
28730 first_attribute = attributes;
28731 /* Combine the attributes. */
28732 attributes = chainon (prefix_attributes, attributes);
28734 if (width)
28735 /* Create the bitfield declaration. */
28736 decl = grokbitfield (declarator, &declspecs,
28737 width,
28738 attributes);
28739 else
28740 decl = grokfield (declarator, &declspecs,
28741 NULL_TREE, /*init_const_expr_p=*/false,
28742 NULL_TREE, attributes);
28744 /* Add the instance variable. */
28745 if (decl != error_mark_node && decl != NULL_TREE)
28746 objc_add_instance_variable (decl);
28748 /* Reset PREFIX_ATTRIBUTES. */
28749 while (attributes && TREE_CHAIN (attributes) != first_attribute)
28750 attributes = TREE_CHAIN (attributes);
28751 if (attributes)
28752 TREE_CHAIN (attributes) = NULL_TREE;
28754 token = cp_lexer_peek_token (parser->lexer);
28756 if (token->type == CPP_COMMA)
28758 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28759 continue;
28761 break;
28764 cp_parser_consume_semicolon_at_end_of_statement (parser);
28765 token = cp_lexer_peek_token (parser->lexer);
28768 if (token->keyword == RID_AT_END)
28769 cp_parser_error (parser, "expected %<}%>");
28771 /* Do not consume the RID_AT_END, so it will be read again as terminating
28772 the @interface of @implementation. */
28773 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
28774 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
28776 /* For historical reasons, we accept an optional semicolon. */
28777 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28778 cp_lexer_consume_token (parser->lexer);
28781 /* Parse an Objective-C protocol declaration. */
28783 static void
28784 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
28786 tree proto, protorefs;
28787 cp_token *tok;
28789 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
28790 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
28792 tok = cp_lexer_peek_token (parser->lexer);
28793 error_at (tok->location, "identifier expected after %<@protocol%>");
28794 cp_parser_consume_semicolon_at_end_of_statement (parser);
28795 return;
28798 /* See if we have a forward declaration or a definition. */
28799 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
28801 /* Try a forward declaration first. */
28802 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
28804 while (true)
28806 tree id;
28808 id = cp_parser_identifier (parser);
28809 if (id == error_mark_node)
28810 break;
28812 objc_declare_protocol (id, attributes);
28814 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28815 cp_lexer_consume_token (parser->lexer);
28816 else
28817 break;
28819 cp_parser_consume_semicolon_at_end_of_statement (parser);
28822 /* Ok, we got a full-fledged definition (or at least should). */
28823 else
28825 proto = cp_parser_identifier (parser);
28826 protorefs = cp_parser_objc_protocol_refs_opt (parser);
28827 objc_start_protocol (proto, protorefs, attributes);
28828 cp_parser_objc_method_prototype_list (parser);
28832 /* Parse an Objective-C superclass or category. */
28834 static void
28835 cp_parser_objc_superclass_or_category (cp_parser *parser,
28836 bool iface_p,
28837 tree *super,
28838 tree *categ, bool *is_class_extension)
28840 cp_token *next = cp_lexer_peek_token (parser->lexer);
28842 *super = *categ = NULL_TREE;
28843 *is_class_extension = false;
28844 if (next->type == CPP_COLON)
28846 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
28847 *super = cp_parser_identifier (parser);
28849 else if (next->type == CPP_OPEN_PAREN)
28851 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
28853 /* If there is no category name, and this is an @interface, we
28854 have a class extension. */
28855 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
28857 *categ = NULL_TREE;
28858 *is_class_extension = true;
28860 else
28861 *categ = cp_parser_identifier (parser);
28863 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28867 /* Parse an Objective-C class interface. */
28869 static void
28870 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
28872 tree name, super, categ, protos;
28873 bool is_class_extension;
28875 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
28876 name = cp_parser_identifier (parser);
28877 if (name == error_mark_node)
28879 /* It's hard to recover because even if valid @interface stuff
28880 is to follow, we can't compile it (or validate it) if we
28881 don't even know which class it refers to. Let's assume this
28882 was a stray '@interface' token in the stream and skip it.
28884 return;
28886 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
28887 &is_class_extension);
28888 protos = cp_parser_objc_protocol_refs_opt (parser);
28890 /* We have either a class or a category on our hands. */
28891 if (categ || is_class_extension)
28892 objc_start_category_interface (name, categ, protos, attributes);
28893 else
28895 objc_start_class_interface (name, super, protos, attributes);
28896 /* Handle instance variable declarations, if any. */
28897 cp_parser_objc_class_ivars (parser);
28898 objc_continue_interface ();
28901 cp_parser_objc_method_prototype_list (parser);
28904 /* Parse an Objective-C class implementation. */
28906 static void
28907 cp_parser_objc_class_implementation (cp_parser* parser)
28909 tree name, super, categ;
28910 bool is_class_extension;
28912 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
28913 name = cp_parser_identifier (parser);
28914 if (name == error_mark_node)
28916 /* It's hard to recover because even if valid @implementation
28917 stuff is to follow, we can't compile it (or validate it) if
28918 we don't even know which class it refers to. Let's assume
28919 this was a stray '@implementation' token in the stream and
28920 skip it.
28922 return;
28924 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
28925 &is_class_extension);
28927 /* We have either a class or a category on our hands. */
28928 if (categ)
28929 objc_start_category_implementation (name, categ);
28930 else
28932 objc_start_class_implementation (name, super);
28933 /* Handle instance variable declarations, if any. */
28934 cp_parser_objc_class_ivars (parser);
28935 objc_continue_implementation ();
28938 cp_parser_objc_method_definition_list (parser);
28941 /* Consume the @end token and finish off the implementation. */
28943 static void
28944 cp_parser_objc_end_implementation (cp_parser* parser)
28946 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
28947 objc_finish_implementation ();
28950 /* Parse an Objective-C declaration. */
28952 static void
28953 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
28955 /* Try to figure out what kind of declaration is present. */
28956 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
28958 if (attributes)
28959 switch (kwd->keyword)
28961 case RID_AT_ALIAS:
28962 case RID_AT_CLASS:
28963 case RID_AT_END:
28964 error_at (kwd->location, "attributes may not be specified before"
28965 " the %<@%D%> Objective-C++ keyword",
28966 kwd->u.value);
28967 attributes = NULL;
28968 break;
28969 case RID_AT_IMPLEMENTATION:
28970 warning_at (kwd->location, OPT_Wattributes,
28971 "prefix attributes are ignored before %<@%D%>",
28972 kwd->u.value);
28973 attributes = NULL;
28974 default:
28975 break;
28978 switch (kwd->keyword)
28980 case RID_AT_ALIAS:
28981 cp_parser_objc_alias_declaration (parser);
28982 break;
28983 case RID_AT_CLASS:
28984 cp_parser_objc_class_declaration (parser);
28985 break;
28986 case RID_AT_PROTOCOL:
28987 cp_parser_objc_protocol_declaration (parser, attributes);
28988 break;
28989 case RID_AT_INTERFACE:
28990 cp_parser_objc_class_interface (parser, attributes);
28991 break;
28992 case RID_AT_IMPLEMENTATION:
28993 cp_parser_objc_class_implementation (parser);
28994 break;
28995 case RID_AT_END:
28996 cp_parser_objc_end_implementation (parser);
28997 break;
28998 default:
28999 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
29000 kwd->u.value);
29001 cp_parser_skip_to_end_of_block_or_statement (parser);
29005 /* Parse an Objective-C try-catch-finally statement.
29007 objc-try-catch-finally-stmt:
29008 @try compound-statement objc-catch-clause-seq [opt]
29009 objc-finally-clause [opt]
29011 objc-catch-clause-seq:
29012 objc-catch-clause objc-catch-clause-seq [opt]
29014 objc-catch-clause:
29015 @catch ( objc-exception-declaration ) compound-statement
29017 objc-finally-clause:
29018 @finally compound-statement
29020 objc-exception-declaration:
29021 parameter-declaration
29022 '...'
29024 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
29026 Returns NULL_TREE.
29028 PS: This function is identical to c_parser_objc_try_catch_finally_statement
29029 for C. Keep them in sync. */
29031 static tree
29032 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
29034 location_t location;
29035 tree stmt;
29037 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
29038 location = cp_lexer_peek_token (parser->lexer)->location;
29039 objc_maybe_warn_exceptions (location);
29040 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
29041 node, lest it get absorbed into the surrounding block. */
29042 stmt = push_stmt_list ();
29043 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29044 objc_begin_try_stmt (location, pop_stmt_list (stmt));
29046 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
29048 cp_parameter_declarator *parm;
29049 tree parameter_declaration = error_mark_node;
29050 bool seen_open_paren = false;
29052 cp_lexer_consume_token (parser->lexer);
29053 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29054 seen_open_paren = true;
29055 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29057 /* We have "@catch (...)" (where the '...' are literally
29058 what is in the code). Skip the '...'.
29059 parameter_declaration is set to NULL_TREE, and
29060 objc_being_catch_clauses() knows that that means
29061 '...'. */
29062 cp_lexer_consume_token (parser->lexer);
29063 parameter_declaration = NULL_TREE;
29065 else
29067 /* We have "@catch (NSException *exception)" or something
29068 like that. Parse the parameter declaration. */
29069 parm = cp_parser_parameter_declaration (parser, false, NULL);
29070 if (parm == NULL)
29071 parameter_declaration = error_mark_node;
29072 else
29073 parameter_declaration = grokdeclarator (parm->declarator,
29074 &parm->decl_specifiers,
29075 PARM, /*initialized=*/0,
29076 /*attrlist=*/NULL);
29078 if (seen_open_paren)
29079 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29080 else
29082 /* If there was no open parenthesis, we are recovering from
29083 an error, and we are trying to figure out what mistake
29084 the user has made. */
29086 /* If there is an immediate closing parenthesis, the user
29087 probably forgot the opening one (ie, they typed "@catch
29088 NSException *e)". Parse the closing parenthesis and keep
29089 going. */
29090 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29091 cp_lexer_consume_token (parser->lexer);
29093 /* If these is no immediate closing parenthesis, the user
29094 probably doesn't know that parenthesis are required at
29095 all (ie, they typed "@catch NSException *e"). So, just
29096 forget about the closing parenthesis and keep going. */
29098 objc_begin_catch_clause (parameter_declaration);
29099 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29100 objc_finish_catch_clause ();
29102 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
29104 cp_lexer_consume_token (parser->lexer);
29105 location = cp_lexer_peek_token (parser->lexer)->location;
29106 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
29107 node, lest it get absorbed into the surrounding block. */
29108 stmt = push_stmt_list ();
29109 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29110 objc_build_finally_clause (location, pop_stmt_list (stmt));
29113 return objc_finish_try_stmt ();
29116 /* Parse an Objective-C synchronized statement.
29118 objc-synchronized-stmt:
29119 @synchronized ( expression ) compound-statement
29121 Returns NULL_TREE. */
29123 static tree
29124 cp_parser_objc_synchronized_statement (cp_parser *parser)
29126 location_t location;
29127 tree lock, stmt;
29129 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
29131 location = cp_lexer_peek_token (parser->lexer)->location;
29132 objc_maybe_warn_exceptions (location);
29133 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
29134 lock = cp_parser_expression (parser);
29135 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29137 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
29138 node, lest it get absorbed into the surrounding block. */
29139 stmt = push_stmt_list ();
29140 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29142 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
29145 /* Parse an Objective-C throw statement.
29147 objc-throw-stmt:
29148 @throw assignment-expression [opt] ;
29150 Returns a constructed '@throw' statement. */
29152 static tree
29153 cp_parser_objc_throw_statement (cp_parser *parser)
29155 tree expr = NULL_TREE;
29156 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29158 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
29160 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29161 expr = cp_parser_expression (parser);
29163 cp_parser_consume_semicolon_at_end_of_statement (parser);
29165 return objc_build_throw_stmt (loc, expr);
29168 /* Parse an Objective-C statement. */
29170 static tree
29171 cp_parser_objc_statement (cp_parser * parser)
29173 /* Try to figure out what kind of declaration is present. */
29174 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29176 switch (kwd->keyword)
29178 case RID_AT_TRY:
29179 return cp_parser_objc_try_catch_finally_statement (parser);
29180 case RID_AT_SYNCHRONIZED:
29181 return cp_parser_objc_synchronized_statement (parser);
29182 case RID_AT_THROW:
29183 return cp_parser_objc_throw_statement (parser);
29184 default:
29185 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
29186 kwd->u.value);
29187 cp_parser_skip_to_end_of_block_or_statement (parser);
29190 return error_mark_node;
29193 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
29194 look ahead to see if an objc keyword follows the attributes. This
29195 is to detect the use of prefix attributes on ObjC @interface and
29196 @protocol. */
29198 static bool
29199 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
29201 cp_lexer_save_tokens (parser->lexer);
29202 *attrib = cp_parser_attributes_opt (parser);
29203 gcc_assert (*attrib);
29204 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
29206 cp_lexer_commit_tokens (parser->lexer);
29207 return true;
29209 cp_lexer_rollback_tokens (parser->lexer);
29210 return false;
29213 /* This routine is a minimal replacement for
29214 c_parser_struct_declaration () used when parsing the list of
29215 types/names or ObjC++ properties. For example, when parsing the
29216 code
29218 @property (readonly) int a, b, c;
29220 this function is responsible for parsing "int a, int b, int c" and
29221 returning the declarations as CHAIN of DECLs.
29223 TODO: Share this code with cp_parser_objc_class_ivars. It's very
29224 similar parsing. */
29225 static tree
29226 cp_parser_objc_struct_declaration (cp_parser *parser)
29228 tree decls = NULL_TREE;
29229 cp_decl_specifier_seq declspecs;
29230 int decl_class_or_enum_p;
29231 tree prefix_attributes;
29233 cp_parser_decl_specifier_seq (parser,
29234 CP_PARSER_FLAGS_NONE,
29235 &declspecs,
29236 &decl_class_or_enum_p);
29238 if (declspecs.type == error_mark_node)
29239 return error_mark_node;
29241 /* auto, register, static, extern, mutable. */
29242 if (declspecs.storage_class != sc_none)
29244 cp_parser_error (parser, "invalid type for property");
29245 declspecs.storage_class = sc_none;
29248 /* thread_local. */
29249 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
29251 cp_parser_error (parser, "invalid type for property");
29252 declspecs.locations[ds_thread] = 0;
29255 /* typedef. */
29256 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
29258 cp_parser_error (parser, "invalid type for property");
29259 declspecs.locations[ds_typedef] = 0;
29262 prefix_attributes = declspecs.attributes;
29263 declspecs.attributes = NULL_TREE;
29265 /* Keep going until we hit the `;' at the end of the declaration. */
29266 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29268 tree attributes, first_attribute, decl;
29269 cp_declarator *declarator;
29270 cp_token *token;
29272 /* Parse the declarator. */
29273 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29274 NULL, NULL, false, false);
29276 /* Look for attributes that apply to the ivar. */
29277 attributes = cp_parser_attributes_opt (parser);
29278 /* Remember which attributes are prefix attributes and
29279 which are not. */
29280 first_attribute = attributes;
29281 /* Combine the attributes. */
29282 attributes = chainon (prefix_attributes, attributes);
29284 decl = grokfield (declarator, &declspecs,
29285 NULL_TREE, /*init_const_expr_p=*/false,
29286 NULL_TREE, attributes);
29288 if (decl == error_mark_node || decl == NULL_TREE)
29289 return error_mark_node;
29291 /* Reset PREFIX_ATTRIBUTES. */
29292 while (attributes && TREE_CHAIN (attributes) != first_attribute)
29293 attributes = TREE_CHAIN (attributes);
29294 if (attributes)
29295 TREE_CHAIN (attributes) = NULL_TREE;
29297 DECL_CHAIN (decl) = decls;
29298 decls = decl;
29300 token = cp_lexer_peek_token (parser->lexer);
29301 if (token->type == CPP_COMMA)
29303 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29304 continue;
29306 else
29307 break;
29309 return decls;
29312 /* Parse an Objective-C @property declaration. The syntax is:
29314 objc-property-declaration:
29315 '@property' objc-property-attributes[opt] struct-declaration ;
29317 objc-property-attributes:
29318 '(' objc-property-attribute-list ')'
29320 objc-property-attribute-list:
29321 objc-property-attribute
29322 objc-property-attribute-list, objc-property-attribute
29324 objc-property-attribute
29325 'getter' = identifier
29326 'setter' = identifier
29327 'readonly'
29328 'readwrite'
29329 'assign'
29330 'retain'
29331 'copy'
29332 'nonatomic'
29334 For example:
29335 @property NSString *name;
29336 @property (readonly) id object;
29337 @property (retain, nonatomic, getter=getTheName) id name;
29338 @property int a, b, c;
29340 PS: This function is identical to
29341 c_parser_objc_at_property_declaration for C. Keep them in sync. */
29342 static void
29343 cp_parser_objc_at_property_declaration (cp_parser *parser)
29345 /* The following variables hold the attributes of the properties as
29346 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
29347 seen. When we see an attribute, we set them to 'true' (if they
29348 are boolean properties) or to the identifier (if they have an
29349 argument, ie, for getter and setter). Note that here we only
29350 parse the list of attributes, check the syntax and accumulate the
29351 attributes that we find. objc_add_property_declaration() will
29352 then process the information. */
29353 bool property_assign = false;
29354 bool property_copy = false;
29355 tree property_getter_ident = NULL_TREE;
29356 bool property_nonatomic = false;
29357 bool property_readonly = false;
29358 bool property_readwrite = false;
29359 bool property_retain = false;
29360 tree property_setter_ident = NULL_TREE;
29362 /* 'properties' is the list of properties that we read. Usually a
29363 single one, but maybe more (eg, in "@property int a, b, c;" there
29364 are three). */
29365 tree properties;
29366 location_t loc;
29368 loc = cp_lexer_peek_token (parser->lexer)->location;
29370 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
29372 /* Parse the optional attribute list... */
29373 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29375 /* Eat the '('. */
29376 cp_lexer_consume_token (parser->lexer);
29378 while (true)
29380 bool syntax_error = false;
29381 cp_token *token = cp_lexer_peek_token (parser->lexer);
29382 enum rid keyword;
29384 if (token->type != CPP_NAME)
29386 cp_parser_error (parser, "expected identifier");
29387 break;
29389 keyword = C_RID_CODE (token->u.value);
29390 cp_lexer_consume_token (parser->lexer);
29391 switch (keyword)
29393 case RID_ASSIGN: property_assign = true; break;
29394 case RID_COPY: property_copy = true; break;
29395 case RID_NONATOMIC: property_nonatomic = true; break;
29396 case RID_READONLY: property_readonly = true; break;
29397 case RID_READWRITE: property_readwrite = true; break;
29398 case RID_RETAIN: property_retain = true; break;
29400 case RID_GETTER:
29401 case RID_SETTER:
29402 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29404 if (keyword == RID_GETTER)
29405 cp_parser_error (parser,
29406 "missing %<=%> (after %<getter%> attribute)");
29407 else
29408 cp_parser_error (parser,
29409 "missing %<=%> (after %<setter%> attribute)");
29410 syntax_error = true;
29411 break;
29413 cp_lexer_consume_token (parser->lexer); /* eat the = */
29414 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
29416 cp_parser_error (parser, "expected identifier");
29417 syntax_error = true;
29418 break;
29420 if (keyword == RID_SETTER)
29422 if (property_setter_ident != NULL_TREE)
29424 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
29425 cp_lexer_consume_token (parser->lexer);
29427 else
29428 property_setter_ident = cp_parser_objc_selector (parser);
29429 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29430 cp_parser_error (parser, "setter name must terminate with %<:%>");
29431 else
29432 cp_lexer_consume_token (parser->lexer);
29434 else
29436 if (property_getter_ident != NULL_TREE)
29438 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
29439 cp_lexer_consume_token (parser->lexer);
29441 else
29442 property_getter_ident = cp_parser_objc_selector (parser);
29444 break;
29445 default:
29446 cp_parser_error (parser, "unknown property attribute");
29447 syntax_error = true;
29448 break;
29451 if (syntax_error)
29452 break;
29454 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29455 cp_lexer_consume_token (parser->lexer);
29456 else
29457 break;
29460 /* FIXME: "@property (setter, assign);" will generate a spurious
29461 "error: expected ‘)’ before ‘,’ token". This is because
29462 cp_parser_require, unlike the C counterpart, will produce an
29463 error even if we are in error recovery. */
29464 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29466 cp_parser_skip_to_closing_parenthesis (parser,
29467 /*recovering=*/true,
29468 /*or_comma=*/false,
29469 /*consume_paren=*/true);
29473 /* ... and the property declaration(s). */
29474 properties = cp_parser_objc_struct_declaration (parser);
29476 if (properties == error_mark_node)
29478 cp_parser_skip_to_end_of_statement (parser);
29479 /* If the next token is now a `;', consume it. */
29480 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29481 cp_lexer_consume_token (parser->lexer);
29482 return;
29485 if (properties == NULL_TREE)
29486 cp_parser_error (parser, "expected identifier");
29487 else
29489 /* Comma-separated properties are chained together in
29490 reverse order; add them one by one. */
29491 properties = nreverse (properties);
29493 for (; properties; properties = TREE_CHAIN (properties))
29494 objc_add_property_declaration (loc, copy_node (properties),
29495 property_readonly, property_readwrite,
29496 property_assign, property_retain,
29497 property_copy, property_nonatomic,
29498 property_getter_ident, property_setter_ident);
29501 cp_parser_consume_semicolon_at_end_of_statement (parser);
29504 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
29506 objc-synthesize-declaration:
29507 @synthesize objc-synthesize-identifier-list ;
29509 objc-synthesize-identifier-list:
29510 objc-synthesize-identifier
29511 objc-synthesize-identifier-list, objc-synthesize-identifier
29513 objc-synthesize-identifier
29514 identifier
29515 identifier = identifier
29517 For example:
29518 @synthesize MyProperty;
29519 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
29521 PS: This function is identical to c_parser_objc_at_synthesize_declaration
29522 for C. Keep them in sync.
29524 static void
29525 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
29527 tree list = NULL_TREE;
29528 location_t loc;
29529 loc = cp_lexer_peek_token (parser->lexer)->location;
29531 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
29532 while (true)
29534 tree property, ivar;
29535 property = cp_parser_identifier (parser);
29536 if (property == error_mark_node)
29538 cp_parser_consume_semicolon_at_end_of_statement (parser);
29539 return;
29541 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
29543 cp_lexer_consume_token (parser->lexer);
29544 ivar = cp_parser_identifier (parser);
29545 if (ivar == error_mark_node)
29547 cp_parser_consume_semicolon_at_end_of_statement (parser);
29548 return;
29551 else
29552 ivar = NULL_TREE;
29553 list = chainon (list, build_tree_list (ivar, property));
29554 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29555 cp_lexer_consume_token (parser->lexer);
29556 else
29557 break;
29559 cp_parser_consume_semicolon_at_end_of_statement (parser);
29560 objc_add_synthesize_declaration (loc, list);
29563 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
29565 objc-dynamic-declaration:
29566 @dynamic identifier-list ;
29568 For example:
29569 @dynamic MyProperty;
29570 @dynamic MyProperty, AnotherProperty;
29572 PS: This function is identical to c_parser_objc_at_dynamic_declaration
29573 for C. Keep them in sync.
29575 static void
29576 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
29578 tree list = NULL_TREE;
29579 location_t loc;
29580 loc = cp_lexer_peek_token (parser->lexer)->location;
29582 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
29583 while (true)
29585 tree property;
29586 property = cp_parser_identifier (parser);
29587 if (property == error_mark_node)
29589 cp_parser_consume_semicolon_at_end_of_statement (parser);
29590 return;
29592 list = chainon (list, build_tree_list (NULL, property));
29593 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29594 cp_lexer_consume_token (parser->lexer);
29595 else
29596 break;
29598 cp_parser_consume_semicolon_at_end_of_statement (parser);
29599 objc_add_dynamic_declaration (loc, list);
29603 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
29605 /* Returns name of the next clause.
29606 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
29607 the token is not consumed. Otherwise appropriate pragma_omp_clause is
29608 returned and the token is consumed. */
29610 static pragma_omp_clause
29611 cp_parser_omp_clause_name (cp_parser *parser)
29613 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
29615 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
29616 result = PRAGMA_OACC_CLAUSE_AUTO;
29617 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
29618 result = PRAGMA_OMP_CLAUSE_IF;
29619 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
29620 result = PRAGMA_OMP_CLAUSE_DEFAULT;
29621 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
29622 result = PRAGMA_OACC_CLAUSE_DELETE;
29623 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
29624 result = PRAGMA_OMP_CLAUSE_PRIVATE;
29625 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29626 result = PRAGMA_OMP_CLAUSE_FOR;
29627 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29629 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29630 const char *p = IDENTIFIER_POINTER (id);
29632 switch (p[0])
29634 case 'a':
29635 if (!strcmp ("aligned", p))
29636 result = PRAGMA_OMP_CLAUSE_ALIGNED;
29637 else if (!strcmp ("async", p))
29638 result = PRAGMA_OACC_CLAUSE_ASYNC;
29639 break;
29640 case 'c':
29641 if (!strcmp ("collapse", p))
29642 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
29643 else if (!strcmp ("copy", p))
29644 result = PRAGMA_OACC_CLAUSE_COPY;
29645 else if (!strcmp ("copyin", p))
29646 result = PRAGMA_OMP_CLAUSE_COPYIN;
29647 else if (!strcmp ("copyout", p))
29648 result = PRAGMA_OACC_CLAUSE_COPYOUT;
29649 else if (!strcmp ("copyprivate", p))
29650 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
29651 else if (!strcmp ("create", p))
29652 result = PRAGMA_OACC_CLAUSE_CREATE;
29653 break;
29654 case 'd':
29655 if (!strcmp ("defaultmap", p))
29656 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
29657 else if (!strcmp ("depend", p))
29658 result = PRAGMA_OMP_CLAUSE_DEPEND;
29659 else if (!strcmp ("device", p))
29660 result = PRAGMA_OMP_CLAUSE_DEVICE;
29661 else if (!strcmp ("deviceptr", p))
29662 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
29663 else if (!strcmp ("device_resident", p))
29664 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
29665 else if (!strcmp ("dist_schedule", p))
29666 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
29667 break;
29668 case 'f':
29669 if (!strcmp ("final", p))
29670 result = PRAGMA_OMP_CLAUSE_FINAL;
29671 else if (!strcmp ("firstprivate", p))
29672 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
29673 else if (!strcmp ("from", p))
29674 result = PRAGMA_OMP_CLAUSE_FROM;
29675 break;
29676 case 'g':
29677 if (!strcmp ("gang", p))
29678 result = PRAGMA_OACC_CLAUSE_GANG;
29679 else if (!strcmp ("grainsize", p))
29680 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
29681 break;
29682 case 'h':
29683 if (!strcmp ("hint", p))
29684 result = PRAGMA_OMP_CLAUSE_HINT;
29685 else if (!strcmp ("host", p))
29686 result = PRAGMA_OACC_CLAUSE_HOST;
29687 break;
29688 case 'i':
29689 if (!strcmp ("inbranch", p))
29690 result = PRAGMA_OMP_CLAUSE_INBRANCH;
29691 else if (!strcmp ("independent", p))
29692 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
29693 else if (!strcmp ("is_device_ptr", p))
29694 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
29695 break;
29696 case 'l':
29697 if (!strcmp ("lastprivate", p))
29698 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
29699 else if (!strcmp ("linear", p))
29700 result = PRAGMA_OMP_CLAUSE_LINEAR;
29701 else if (!strcmp ("link", p))
29702 result = PRAGMA_OMP_CLAUSE_LINK;
29703 break;
29704 case 'm':
29705 if (!strcmp ("map", p))
29706 result = PRAGMA_OMP_CLAUSE_MAP;
29707 else if (!strcmp ("mergeable", p))
29708 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
29709 else if (flag_cilkplus && !strcmp ("mask", p))
29710 result = PRAGMA_CILK_CLAUSE_MASK;
29711 break;
29712 case 'n':
29713 if (!strcmp ("nogroup", p))
29714 result = PRAGMA_OMP_CLAUSE_NOGROUP;
29715 else if (!strcmp ("notinbranch", p))
29716 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
29717 else if (!strcmp ("nowait", p))
29718 result = PRAGMA_OMP_CLAUSE_NOWAIT;
29719 else if (flag_cilkplus && !strcmp ("nomask", p))
29720 result = PRAGMA_CILK_CLAUSE_NOMASK;
29721 else if (!strcmp ("num_gangs", p))
29722 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
29723 else if (!strcmp ("num_tasks", p))
29724 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
29725 else if (!strcmp ("num_teams", p))
29726 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
29727 else if (!strcmp ("num_threads", p))
29728 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
29729 else if (!strcmp ("num_workers", p))
29730 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
29731 break;
29732 case 'o':
29733 if (!strcmp ("ordered", p))
29734 result = PRAGMA_OMP_CLAUSE_ORDERED;
29735 break;
29736 case 'p':
29737 if (!strcmp ("parallel", p))
29738 result = PRAGMA_OMP_CLAUSE_PARALLEL;
29739 else if (!strcmp ("present", p))
29740 result = PRAGMA_OACC_CLAUSE_PRESENT;
29741 else if (!strcmp ("present_or_copy", p)
29742 || !strcmp ("pcopy", p))
29743 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
29744 else if (!strcmp ("present_or_copyin", p)
29745 || !strcmp ("pcopyin", p))
29746 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
29747 else if (!strcmp ("present_or_copyout", p)
29748 || !strcmp ("pcopyout", p))
29749 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
29750 else if (!strcmp ("present_or_create", p)
29751 || !strcmp ("pcreate", p))
29752 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
29753 else if (!strcmp ("priority", p))
29754 result = PRAGMA_OMP_CLAUSE_PRIORITY;
29755 else if (!strcmp ("proc_bind", p))
29756 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
29757 break;
29758 case 'r':
29759 if (!strcmp ("reduction", p))
29760 result = PRAGMA_OMP_CLAUSE_REDUCTION;
29761 break;
29762 case 's':
29763 if (!strcmp ("safelen", p))
29764 result = PRAGMA_OMP_CLAUSE_SAFELEN;
29765 else if (!strcmp ("schedule", p))
29766 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
29767 else if (!strcmp ("sections", p))
29768 result = PRAGMA_OMP_CLAUSE_SECTIONS;
29769 else if (!strcmp ("self", p))
29770 result = PRAGMA_OACC_CLAUSE_SELF;
29771 else if (!strcmp ("seq", p))
29772 result = PRAGMA_OACC_CLAUSE_SEQ;
29773 else if (!strcmp ("shared", p))
29774 result = PRAGMA_OMP_CLAUSE_SHARED;
29775 else if (!strcmp ("simd", p))
29776 result = PRAGMA_OMP_CLAUSE_SIMD;
29777 else if (!strcmp ("simdlen", p))
29778 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
29779 break;
29780 case 't':
29781 if (!strcmp ("taskgroup", p))
29782 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
29783 else if (!strcmp ("thread_limit", p))
29784 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
29785 else if (!strcmp ("threads", p))
29786 result = PRAGMA_OMP_CLAUSE_THREADS;
29787 else if (!strcmp ("tile", p))
29788 result = PRAGMA_OACC_CLAUSE_TILE;
29789 else if (!strcmp ("to", p))
29790 result = PRAGMA_OMP_CLAUSE_TO;
29791 break;
29792 case 'u':
29793 if (!strcmp ("uniform", p))
29794 result = PRAGMA_OMP_CLAUSE_UNIFORM;
29795 else if (!strcmp ("untied", p))
29796 result = PRAGMA_OMP_CLAUSE_UNTIED;
29797 else if (!strcmp ("use_device", p))
29798 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
29799 else if (!strcmp ("use_device_ptr", p))
29800 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
29801 break;
29802 case 'v':
29803 if (!strcmp ("vector", p))
29804 result = PRAGMA_OACC_CLAUSE_VECTOR;
29805 else if (!strcmp ("vector_length", p))
29806 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
29807 else if (flag_cilkplus && !strcmp ("vectorlength", p))
29808 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
29809 break;
29810 case 'w':
29811 if (!strcmp ("wait", p))
29812 result = PRAGMA_OACC_CLAUSE_WAIT;
29813 else if (!strcmp ("worker", p))
29814 result = PRAGMA_OACC_CLAUSE_WORKER;
29815 break;
29819 if (result != PRAGMA_OMP_CLAUSE_NONE)
29820 cp_lexer_consume_token (parser->lexer);
29822 return result;
29825 /* Validate that a clause of the given type does not already exist. */
29827 static void
29828 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
29829 const char *name, location_t location)
29831 tree c;
29833 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29834 if (OMP_CLAUSE_CODE (c) == code)
29836 error_at (location, "too many %qs clauses", name);
29837 break;
29841 /* OpenMP 2.5:
29842 variable-list:
29843 identifier
29844 variable-list , identifier
29846 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
29847 colon). An opening parenthesis will have been consumed by the caller.
29849 If KIND is nonzero, create the appropriate node and install the decl
29850 in OMP_CLAUSE_DECL and add the node to the head of the list.
29852 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
29853 return the list created.
29855 COLON can be NULL if only closing parenthesis should end the list,
29856 or pointer to bool which will receive false if the list is terminated
29857 by closing parenthesis or true if the list is terminated by colon. */
29859 static tree
29860 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
29861 tree list, bool *colon)
29863 cp_token *token;
29864 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
29865 if (colon)
29867 parser->colon_corrects_to_scope_p = false;
29868 *colon = false;
29870 while (1)
29872 tree name, decl;
29874 token = cp_lexer_peek_token (parser->lexer);
29875 if (kind != 0
29876 && current_class_ptr
29877 && cp_parser_is_keyword (token, RID_THIS))
29879 decl = finish_this_expr ();
29880 if (TREE_CODE (decl) == NON_LVALUE_EXPR
29881 || CONVERT_EXPR_P (decl))
29882 decl = TREE_OPERAND (decl, 0);
29883 cp_lexer_consume_token (parser->lexer);
29885 else
29887 name = cp_parser_id_expression (parser, /*template_p=*/false,
29888 /*check_dependency_p=*/true,
29889 /*template_p=*/NULL,
29890 /*declarator_p=*/false,
29891 /*optional_p=*/false);
29892 if (name == error_mark_node)
29893 goto skip_comma;
29895 decl = cp_parser_lookup_name_simple (parser, name, token->location);
29896 if (decl == error_mark_node)
29897 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
29898 token->location);
29900 if (decl == error_mark_node)
29902 else if (kind != 0)
29904 switch (kind)
29906 case OMP_CLAUSE__CACHE_:
29907 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
29909 error_at (token->location, "expected %<[%>");
29910 decl = error_mark_node;
29911 break;
29913 /* FALLTHROUGH. */
29914 case OMP_CLAUSE_MAP:
29915 case OMP_CLAUSE_FROM:
29916 case OMP_CLAUSE_TO:
29917 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
29919 location_t loc
29920 = cp_lexer_peek_token (parser->lexer)->location;
29921 cp_id_kind idk = CP_ID_KIND_NONE;
29922 cp_lexer_consume_token (parser->lexer);
29923 decl
29924 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
29925 decl, false,
29926 &idk, loc);
29928 /* FALLTHROUGH. */
29929 case OMP_CLAUSE_DEPEND:
29930 case OMP_CLAUSE_REDUCTION:
29931 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
29933 tree low_bound = NULL_TREE, length = NULL_TREE;
29935 parser->colon_corrects_to_scope_p = false;
29936 cp_lexer_consume_token (parser->lexer);
29937 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
29938 low_bound = cp_parser_expression (parser);
29939 if (!colon)
29940 parser->colon_corrects_to_scope_p
29941 = saved_colon_corrects_to_scope_p;
29942 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
29943 length = integer_one_node;
29944 else
29946 /* Look for `:'. */
29947 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29948 goto skip_comma;
29949 if (!cp_lexer_next_token_is (parser->lexer,
29950 CPP_CLOSE_SQUARE))
29951 length = cp_parser_expression (parser);
29953 /* Look for the closing `]'. */
29954 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
29955 RT_CLOSE_SQUARE))
29956 goto skip_comma;
29958 if (kind == OMP_CLAUSE__CACHE_)
29960 if (TREE_CODE (low_bound) != INTEGER_CST
29961 && !TREE_READONLY (low_bound))
29963 error_at (token->location,
29964 "%qD is not a constant", low_bound);
29965 decl = error_mark_node;
29968 if (TREE_CODE (length) != INTEGER_CST
29969 && !TREE_READONLY (length))
29971 error_at (token->location,
29972 "%qD is not a constant", length);
29973 decl = error_mark_node;
29977 decl = tree_cons (low_bound, length, decl);
29979 break;
29980 default:
29981 break;
29984 tree u = build_omp_clause (token->location, kind);
29985 OMP_CLAUSE_DECL (u) = decl;
29986 OMP_CLAUSE_CHAIN (u) = list;
29987 list = u;
29989 else
29990 list = tree_cons (decl, NULL_TREE, list);
29992 get_comma:
29993 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29994 break;
29995 cp_lexer_consume_token (parser->lexer);
29998 if (colon)
29999 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30001 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30003 *colon = true;
30004 cp_parser_require (parser, CPP_COLON, RT_COLON);
30005 return list;
30008 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30010 int ending;
30012 /* Try to resync to an unnested comma. Copied from
30013 cp_parser_parenthesized_expression_list. */
30014 skip_comma:
30015 if (colon)
30016 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30017 ending = cp_parser_skip_to_closing_parenthesis (parser,
30018 /*recovering=*/true,
30019 /*or_comma=*/true,
30020 /*consume_paren=*/true);
30021 if (ending < 0)
30022 goto get_comma;
30025 return list;
30028 /* Similarly, but expect leading and trailing parenthesis. This is a very
30029 common case for omp clauses. */
30031 static tree
30032 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
30034 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30035 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
30036 return list;
30039 /* OpenACC 2.0:
30040 copy ( variable-list )
30041 copyin ( variable-list )
30042 copyout ( variable-list )
30043 create ( variable-list )
30044 delete ( variable-list )
30045 present ( variable-list )
30046 present_or_copy ( variable-list )
30047 pcopy ( variable-list )
30048 present_or_copyin ( variable-list )
30049 pcopyin ( variable-list )
30050 present_or_copyout ( variable-list )
30051 pcopyout ( variable-list )
30052 present_or_create ( variable-list )
30053 pcreate ( variable-list ) */
30055 static tree
30056 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
30057 tree list)
30059 enum gomp_map_kind kind;
30060 switch (c_kind)
30062 case PRAGMA_OACC_CLAUSE_COPY:
30063 kind = GOMP_MAP_FORCE_TOFROM;
30064 break;
30065 case PRAGMA_OACC_CLAUSE_COPYIN:
30066 kind = GOMP_MAP_FORCE_TO;
30067 break;
30068 case PRAGMA_OACC_CLAUSE_COPYOUT:
30069 kind = GOMP_MAP_FORCE_FROM;
30070 break;
30071 case PRAGMA_OACC_CLAUSE_CREATE:
30072 kind = GOMP_MAP_FORCE_ALLOC;
30073 break;
30074 case PRAGMA_OACC_CLAUSE_DELETE:
30075 kind = GOMP_MAP_FORCE_DEALLOC;
30076 break;
30077 case PRAGMA_OACC_CLAUSE_DEVICE:
30078 kind = GOMP_MAP_FORCE_TO;
30079 break;
30080 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
30081 kind = GOMP_MAP_DEVICE_RESIDENT;
30082 break;
30083 case PRAGMA_OACC_CLAUSE_HOST:
30084 case PRAGMA_OACC_CLAUSE_SELF:
30085 kind = GOMP_MAP_FORCE_FROM;
30086 break;
30087 case PRAGMA_OACC_CLAUSE_LINK:
30088 kind = GOMP_MAP_LINK;
30089 break;
30090 case PRAGMA_OACC_CLAUSE_PRESENT:
30091 kind = GOMP_MAP_FORCE_PRESENT;
30092 break;
30093 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
30094 kind = GOMP_MAP_TOFROM;
30095 break;
30096 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
30097 kind = GOMP_MAP_TO;
30098 break;
30099 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
30100 kind = GOMP_MAP_FROM;
30101 break;
30102 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
30103 kind = GOMP_MAP_ALLOC;
30104 break;
30105 default:
30106 gcc_unreachable ();
30108 tree nl, c;
30109 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
30111 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
30112 OMP_CLAUSE_SET_MAP_KIND (c, kind);
30114 return nl;
30117 /* OpenACC 2.0:
30118 deviceptr ( variable-list ) */
30120 static tree
30121 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
30123 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30124 tree vars, t;
30126 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
30127 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
30128 variable-list must only allow for pointer variables. */
30129 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30130 for (t = vars; t; t = TREE_CHAIN (t))
30132 tree v = TREE_PURPOSE (t);
30133 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
30134 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
30135 OMP_CLAUSE_DECL (u) = v;
30136 OMP_CLAUSE_CHAIN (u) = list;
30137 list = u;
30140 return list;
30143 /* OpenACC 2.0:
30144 auto
30145 independent
30146 nohost
30147 seq */
30149 static tree
30150 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
30151 enum omp_clause_code code,
30152 tree list, location_t location)
30154 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
30155 tree c = build_omp_clause (location, code);
30156 OMP_CLAUSE_CHAIN (c) = list;
30157 return c;
30160 /* OpenACC:
30161 num_gangs ( expression )
30162 num_workers ( expression )
30163 vector_length ( expression ) */
30165 static tree
30166 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
30167 const char *str, tree list)
30169 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30171 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30172 return list;
30174 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
30176 if (t == error_mark_node
30177 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30179 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30180 /*or_comma=*/false,
30181 /*consume_paren=*/true);
30182 return list;
30185 check_no_duplicate_clause (list, code, str, loc);
30187 tree c = build_omp_clause (loc, code);
30188 OMP_CLAUSE_OPERAND (c, 0) = t;
30189 OMP_CLAUSE_CHAIN (c) = list;
30190 return c;
30193 /* OpenACC:
30195 gang [( gang-arg-list )]
30196 worker [( [num:] int-expr )]
30197 vector [( [length:] int-expr )]
30199 where gang-arg is one of:
30201 [num:] int-expr
30202 static: size-expr
30204 and size-expr may be:
30207 int-expr
30210 static tree
30211 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
30212 const char *str, tree list)
30214 const char *id = "num";
30215 cp_lexer *lexer = parser->lexer;
30216 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
30217 location_t loc = cp_lexer_peek_token (lexer)->location;
30219 if (kind == OMP_CLAUSE_VECTOR)
30220 id = "length";
30222 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
30224 cp_lexer_consume_token (lexer);
30228 cp_token *next = cp_lexer_peek_token (lexer);
30229 int idx = 0;
30231 /* Gang static argument. */
30232 if (kind == OMP_CLAUSE_GANG
30233 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
30235 cp_lexer_consume_token (lexer);
30237 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30238 goto cleanup_error;
30240 idx = 1;
30241 if (ops[idx] != NULL)
30243 cp_parser_error (parser, "too many %<static%> arguments");
30244 goto cleanup_error;
30247 /* Check for the '*' argument. */
30248 if (cp_lexer_next_token_is (lexer, CPP_MULT)
30249 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
30250 || cp_lexer_nth_token_is (parser->lexer, 2,
30251 CPP_CLOSE_PAREN)))
30253 cp_lexer_consume_token (lexer);
30254 ops[idx] = integer_minus_one_node;
30256 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
30258 cp_lexer_consume_token (lexer);
30259 continue;
30261 else break;
30264 /* Worker num: argument and vector length: arguments. */
30265 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
30266 && strcmp (id, IDENTIFIER_POINTER (next->u.value)) == 0
30267 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
30269 cp_lexer_consume_token (lexer); /* id */
30270 cp_lexer_consume_token (lexer); /* ':' */
30273 /* Now collect the actual argument. */
30274 if (ops[idx] != NULL_TREE)
30276 cp_parser_error (parser, "unexpected argument");
30277 goto cleanup_error;
30280 tree expr = cp_parser_assignment_expression (parser, NULL, false,
30281 false);
30282 if (expr == error_mark_node)
30283 goto cleanup_error;
30285 mark_exp_read (expr);
30286 ops[idx] = expr;
30288 if (kind == OMP_CLAUSE_GANG
30289 && cp_lexer_next_token_is (lexer, CPP_COMMA))
30291 cp_lexer_consume_token (lexer);
30292 continue;
30294 break;
30296 while (1);
30298 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30299 goto cleanup_error;
30302 check_no_duplicate_clause (list, kind, str, loc);
30304 c = build_omp_clause (loc, kind);
30306 if (ops[1])
30307 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
30309 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
30310 OMP_CLAUSE_CHAIN (c) = list;
30312 return c;
30314 cleanup_error:
30315 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
30316 return list;
30319 /* OpenACC 2.0:
30320 tile ( size-expr-list ) */
30322 static tree
30323 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
30325 tree c, expr = error_mark_node;
30326 tree tile = NULL_TREE;
30328 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
30330 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30331 return list;
30335 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
30336 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
30337 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
30339 cp_lexer_consume_token (parser->lexer);
30340 expr = integer_minus_one_node;
30342 else
30343 expr = cp_parser_assignment_expression (parser, NULL, false, false);
30345 if (expr == error_mark_node)
30346 return list;
30348 tile = tree_cons (NULL_TREE, expr, tile);
30350 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30351 cp_lexer_consume_token (parser->lexer);
30353 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
30355 /* Consume the trailing ')'. */
30356 cp_lexer_consume_token (parser->lexer);
30358 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
30359 tile = nreverse (tile);
30360 OMP_CLAUSE_TILE_LIST (c) = tile;
30361 OMP_CLAUSE_CHAIN (c) = list;
30362 return c;
30365 /* OpenACC 2.0
30366 Parse wait clause or directive parameters. */
30368 static tree
30369 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
30371 vec<tree, va_gc> *args;
30372 tree t, args_tree;
30374 args = cp_parser_parenthesized_expression_list (parser, non_attr,
30375 /*cast_p=*/false,
30376 /*allow_expansion_p=*/true,
30377 /*non_constant_p=*/NULL);
30379 if (args == NULL || args->length () == 0)
30381 cp_parser_error (parser, "expected integer expression before ')'");
30382 if (args != NULL)
30383 release_tree_vector (args);
30384 return list;
30387 args_tree = build_tree_list_vec (args);
30389 release_tree_vector (args);
30391 for (t = args_tree; t; t = TREE_CHAIN (t))
30393 tree targ = TREE_VALUE (t);
30395 if (targ != error_mark_node)
30397 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
30398 error ("%<wait%> expression must be integral");
30399 else
30401 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
30403 mark_rvalue_use (targ);
30404 OMP_CLAUSE_DECL (c) = targ;
30405 OMP_CLAUSE_CHAIN (c) = list;
30406 list = c;
30411 return list;
30414 /* OpenACC:
30415 wait ( int-expr-list ) */
30417 static tree
30418 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
30420 location_t location = cp_lexer_peek_token (parser->lexer)->location;
30422 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
30423 return list;
30425 list = cp_parser_oacc_wait_list (parser, location, list);
30427 return list;
30430 /* OpenMP 3.0:
30431 collapse ( constant-expression ) */
30433 static tree
30434 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
30436 tree c, num;
30437 location_t loc;
30438 HOST_WIDE_INT n;
30440 loc = cp_lexer_peek_token (parser->lexer)->location;
30441 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30442 return list;
30444 num = cp_parser_constant_expression (parser);
30446 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30447 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30448 /*or_comma=*/false,
30449 /*consume_paren=*/true);
30451 if (num == error_mark_node)
30452 return list;
30453 num = fold_non_dependent_expr (num);
30454 if (!tree_fits_shwi_p (num)
30455 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
30456 || (n = tree_to_shwi (num)) <= 0
30457 || (int) n != n)
30459 error_at (loc, "collapse argument needs positive constant integer expression");
30460 return list;
30463 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
30464 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
30465 OMP_CLAUSE_CHAIN (c) = list;
30466 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
30468 return c;
30471 /* OpenMP 2.5:
30472 default ( shared | none )
30474 OpenACC 2.0
30475 default (none) */
30477 static tree
30478 cp_parser_omp_clause_default (cp_parser *parser, tree list,
30479 location_t location, bool is_oacc)
30481 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
30482 tree c;
30484 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30485 return list;
30486 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30488 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30489 const char *p = IDENTIFIER_POINTER (id);
30491 switch (p[0])
30493 case 'n':
30494 if (strcmp ("none", p) != 0)
30495 goto invalid_kind;
30496 kind = OMP_CLAUSE_DEFAULT_NONE;
30497 break;
30499 case 's':
30500 if (strcmp ("shared", p) != 0 || is_oacc)
30501 goto invalid_kind;
30502 kind = OMP_CLAUSE_DEFAULT_SHARED;
30503 break;
30505 default:
30506 goto invalid_kind;
30509 cp_lexer_consume_token (parser->lexer);
30511 else
30513 invalid_kind:
30514 if (is_oacc)
30515 cp_parser_error (parser, "expected %<none%>");
30516 else
30517 cp_parser_error (parser, "expected %<none%> or %<shared%>");
30520 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30521 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30522 /*or_comma=*/false,
30523 /*consume_paren=*/true);
30525 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
30526 return list;
30528 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
30529 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
30530 OMP_CLAUSE_CHAIN (c) = list;
30531 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
30533 return c;
30536 /* OpenMP 3.1:
30537 final ( expression ) */
30539 static tree
30540 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
30542 tree t, c;
30544 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30545 return list;
30547 t = cp_parser_condition (parser);
30549 if (t == error_mark_node
30550 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30551 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30552 /*or_comma=*/false,
30553 /*consume_paren=*/true);
30555 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
30557 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
30558 OMP_CLAUSE_FINAL_EXPR (c) = t;
30559 OMP_CLAUSE_CHAIN (c) = list;
30561 return c;
30564 /* OpenMP 2.5:
30565 if ( expression )
30567 OpenMP 4.5:
30568 if ( directive-name-modifier : expression )
30570 directive-name-modifier:
30571 parallel | task | taskloop | target data | target | target update
30572 | target enter data | target exit data */
30574 static tree
30575 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
30576 bool is_omp)
30578 tree t, c;
30579 enum tree_code if_modifier = ERROR_MARK;
30581 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30582 return list;
30584 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30586 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30587 const char *p = IDENTIFIER_POINTER (id);
30588 int n = 2;
30590 if (strcmp ("parallel", p) == 0)
30591 if_modifier = OMP_PARALLEL;
30592 else if (strcmp ("task", p) == 0)
30593 if_modifier = OMP_TASK;
30594 else if (strcmp ("taskloop", p) == 0)
30595 if_modifier = OMP_TASKLOOP;
30596 else if (strcmp ("target", p) == 0)
30598 if_modifier = OMP_TARGET;
30599 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
30601 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
30602 p = IDENTIFIER_POINTER (id);
30603 if (strcmp ("data", p) == 0)
30604 if_modifier = OMP_TARGET_DATA;
30605 else if (strcmp ("update", p) == 0)
30606 if_modifier = OMP_TARGET_UPDATE;
30607 else if (strcmp ("enter", p) == 0)
30608 if_modifier = OMP_TARGET_ENTER_DATA;
30609 else if (strcmp ("exit", p) == 0)
30610 if_modifier = OMP_TARGET_EXIT_DATA;
30611 if (if_modifier != OMP_TARGET)
30612 n = 3;
30613 else
30615 location_t loc
30616 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
30617 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
30618 "or %<exit%>");
30619 if_modifier = ERROR_MARK;
30621 if (if_modifier == OMP_TARGET_ENTER_DATA
30622 || if_modifier == OMP_TARGET_EXIT_DATA)
30624 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
30626 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
30627 p = IDENTIFIER_POINTER (id);
30628 if (strcmp ("data", p) == 0)
30629 n = 4;
30631 if (n != 4)
30633 location_t loc
30634 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
30635 error_at (loc, "expected %<data%>");
30636 if_modifier = ERROR_MARK;
30641 if (if_modifier != ERROR_MARK)
30643 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
30645 while (n-- > 0)
30646 cp_lexer_consume_token (parser->lexer);
30648 else
30650 if (n > 2)
30652 location_t loc
30653 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
30654 error_at (loc, "expected %<:%>");
30656 if_modifier = ERROR_MARK;
30661 t = cp_parser_condition (parser);
30663 if (t == error_mark_node
30664 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30665 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30666 /*or_comma=*/false,
30667 /*consume_paren=*/true);
30669 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
30670 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
30672 if (if_modifier != ERROR_MARK
30673 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
30675 const char *p = NULL;
30676 switch (if_modifier)
30678 case OMP_PARALLEL: p = "parallel"; break;
30679 case OMP_TASK: p = "task"; break;
30680 case OMP_TASKLOOP: p = "taskloop"; break;
30681 case OMP_TARGET_DATA: p = "target data"; break;
30682 case OMP_TARGET: p = "target"; break;
30683 case OMP_TARGET_UPDATE: p = "target update"; break;
30684 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
30685 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
30686 default: gcc_unreachable ();
30688 error_at (location, "too many %<if%> clauses with %qs modifier",
30690 return list;
30692 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
30694 if (!is_omp)
30695 error_at (location, "too many %<if%> clauses");
30696 else
30697 error_at (location, "too many %<if%> clauses without modifier");
30698 return list;
30700 else if (if_modifier == ERROR_MARK
30701 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
30703 error_at (location, "if any %<if%> clause has modifier, then all "
30704 "%<if%> clauses have to use modifier");
30705 return list;
30709 c = build_omp_clause (location, OMP_CLAUSE_IF);
30710 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
30711 OMP_CLAUSE_IF_EXPR (c) = t;
30712 OMP_CLAUSE_CHAIN (c) = list;
30714 return c;
30717 /* OpenMP 3.1:
30718 mergeable */
30720 static tree
30721 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
30722 tree list, location_t location)
30724 tree c;
30726 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
30727 location);
30729 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
30730 OMP_CLAUSE_CHAIN (c) = list;
30731 return c;
30734 /* OpenMP 2.5:
30735 nowait */
30737 static tree
30738 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
30739 tree list, location_t location)
30741 tree c;
30743 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
30745 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
30746 OMP_CLAUSE_CHAIN (c) = list;
30747 return c;
30750 /* OpenMP 2.5:
30751 num_threads ( expression ) */
30753 static tree
30754 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
30755 location_t location)
30757 tree t, c;
30759 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30760 return list;
30762 t = cp_parser_expression (parser);
30764 if (t == error_mark_node
30765 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30766 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30767 /*or_comma=*/false,
30768 /*consume_paren=*/true);
30770 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
30771 "num_threads", location);
30773 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
30774 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
30775 OMP_CLAUSE_CHAIN (c) = list;
30777 return c;
30780 /* OpenMP 4.5:
30781 num_tasks ( expression ) */
30783 static tree
30784 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
30785 location_t location)
30787 tree t, c;
30789 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30790 return list;
30792 t = cp_parser_expression (parser);
30794 if (t == error_mark_node
30795 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30796 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30797 /*or_comma=*/false,
30798 /*consume_paren=*/true);
30800 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
30801 "num_tasks", location);
30803 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
30804 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
30805 OMP_CLAUSE_CHAIN (c) = list;
30807 return c;
30810 /* OpenMP 4.5:
30811 grainsize ( expression ) */
30813 static tree
30814 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
30815 location_t location)
30817 tree t, c;
30819 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30820 return list;
30822 t = cp_parser_expression (parser);
30824 if (t == error_mark_node
30825 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30826 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30827 /*or_comma=*/false,
30828 /*consume_paren=*/true);
30830 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
30831 "grainsize", location);
30833 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
30834 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
30835 OMP_CLAUSE_CHAIN (c) = list;
30837 return c;
30840 /* OpenMP 4.5:
30841 priority ( expression ) */
30843 static tree
30844 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
30845 location_t location)
30847 tree t, c;
30849 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30850 return list;
30852 t = cp_parser_expression (parser);
30854 if (t == error_mark_node
30855 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30856 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30857 /*or_comma=*/false,
30858 /*consume_paren=*/true);
30860 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
30861 "priority", location);
30863 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
30864 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
30865 OMP_CLAUSE_CHAIN (c) = list;
30867 return c;
30870 /* OpenMP 4.5:
30871 hint ( expression ) */
30873 static tree
30874 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
30875 location_t location)
30877 tree t, c;
30879 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30880 return list;
30882 t = cp_parser_expression (parser);
30884 if (t == error_mark_node
30885 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30886 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30887 /*or_comma=*/false,
30888 /*consume_paren=*/true);
30890 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
30892 c = build_omp_clause (location, OMP_CLAUSE_HINT);
30893 OMP_CLAUSE_HINT_EXPR (c) = t;
30894 OMP_CLAUSE_CHAIN (c) = list;
30896 return c;
30899 /* OpenMP 4.5:
30900 defaultmap ( tofrom : scalar ) */
30902 static tree
30903 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
30904 location_t location)
30906 tree c, id;
30907 const char *p;
30909 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30910 return list;
30912 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30914 cp_parser_error (parser, "expected %<tofrom%>");
30915 goto out_err;
30917 id = cp_lexer_peek_token (parser->lexer)->u.value;
30918 p = IDENTIFIER_POINTER (id);
30919 if (strcmp (p, "tofrom") != 0)
30921 cp_parser_error (parser, "expected %<tofrom%>");
30922 goto out_err;
30924 cp_lexer_consume_token (parser->lexer);
30925 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30926 goto out_err;
30928 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30930 cp_parser_error (parser, "expected %<scalar%>");
30931 goto out_err;
30933 id = cp_lexer_peek_token (parser->lexer)->u.value;
30934 p = IDENTIFIER_POINTER (id);
30935 if (strcmp (p, "scalar") != 0)
30937 cp_parser_error (parser, "expected %<scalar%>");
30938 goto out_err;
30940 cp_lexer_consume_token (parser->lexer);
30941 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30942 goto out_err;
30944 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
30945 location);
30947 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
30948 OMP_CLAUSE_CHAIN (c) = list;
30949 return c;
30951 out_err:
30952 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30953 /*or_comma=*/false,
30954 /*consume_paren=*/true);
30955 return list;
30958 /* OpenMP 2.5:
30959 ordered
30961 OpenMP 4.5:
30962 ordered ( constant-expression ) */
30964 static tree
30965 cp_parser_omp_clause_ordered (cp_parser *parser,
30966 tree list, location_t location)
30968 tree c, num = NULL_TREE;
30969 HOST_WIDE_INT n;
30971 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
30972 "ordered", location);
30974 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30976 cp_lexer_consume_token (parser->lexer);
30978 num = cp_parser_constant_expression (parser);
30980 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30981 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30982 /*or_comma=*/false,
30983 /*consume_paren=*/true);
30985 if (num == error_mark_node)
30986 return list;
30987 num = fold_non_dependent_expr (num);
30988 if (!tree_fits_shwi_p (num)
30989 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
30990 || (n = tree_to_shwi (num)) <= 0
30991 || (int) n != n)
30993 error_at (location,
30994 "ordered argument needs positive constant integer "
30995 "expression");
30996 return list;
31000 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
31001 OMP_CLAUSE_ORDERED_EXPR (c) = num;
31002 OMP_CLAUSE_CHAIN (c) = list;
31003 return c;
31006 /* OpenMP 2.5:
31007 reduction ( reduction-operator : variable-list )
31009 reduction-operator:
31010 One of: + * - & ^ | && ||
31012 OpenMP 3.1:
31014 reduction-operator:
31015 One of: + * - & ^ | && || min max
31017 OpenMP 4.0:
31019 reduction-operator:
31020 One of: + * - & ^ | && ||
31021 id-expression */
31023 static tree
31024 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
31026 enum tree_code code = ERROR_MARK;
31027 tree nlist, c, id = NULL_TREE;
31029 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31030 return list;
31032 switch (cp_lexer_peek_token (parser->lexer)->type)
31034 case CPP_PLUS: code = PLUS_EXPR; break;
31035 case CPP_MULT: code = MULT_EXPR; break;
31036 case CPP_MINUS: code = MINUS_EXPR; break;
31037 case CPP_AND: code = BIT_AND_EXPR; break;
31038 case CPP_XOR: code = BIT_XOR_EXPR; break;
31039 case CPP_OR: code = BIT_IOR_EXPR; break;
31040 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
31041 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
31042 default: break;
31045 if (code != ERROR_MARK)
31046 cp_lexer_consume_token (parser->lexer);
31047 else
31049 bool saved_colon_corrects_to_scope_p;
31050 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31051 parser->colon_corrects_to_scope_p = false;
31052 id = cp_parser_id_expression (parser, /*template_p=*/false,
31053 /*check_dependency_p=*/true,
31054 /*template_p=*/NULL,
31055 /*declarator_p=*/false,
31056 /*optional_p=*/false);
31057 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31058 if (identifier_p (id))
31060 const char *p = IDENTIFIER_POINTER (id);
31062 if (strcmp (p, "min") == 0)
31063 code = MIN_EXPR;
31064 else if (strcmp (p, "max") == 0)
31065 code = MAX_EXPR;
31066 else if (id == ansi_opname (PLUS_EXPR))
31067 code = PLUS_EXPR;
31068 else if (id == ansi_opname (MULT_EXPR))
31069 code = MULT_EXPR;
31070 else if (id == ansi_opname (MINUS_EXPR))
31071 code = MINUS_EXPR;
31072 else if (id == ansi_opname (BIT_AND_EXPR))
31073 code = BIT_AND_EXPR;
31074 else if (id == ansi_opname (BIT_IOR_EXPR))
31075 code = BIT_IOR_EXPR;
31076 else if (id == ansi_opname (BIT_XOR_EXPR))
31077 code = BIT_XOR_EXPR;
31078 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
31079 code = TRUTH_ANDIF_EXPR;
31080 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
31081 code = TRUTH_ORIF_EXPR;
31082 id = omp_reduction_id (code, id, NULL_TREE);
31083 tree scope = parser->scope;
31084 if (scope)
31085 id = build_qualified_name (NULL_TREE, scope, id, false);
31086 parser->scope = NULL_TREE;
31087 parser->qualifying_scope = NULL_TREE;
31088 parser->object_scope = NULL_TREE;
31090 else
31092 error ("invalid reduction-identifier");
31093 resync_fail:
31094 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31095 /*or_comma=*/false,
31096 /*consume_paren=*/true);
31097 return list;
31101 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31102 goto resync_fail;
31104 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
31105 NULL);
31106 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31108 OMP_CLAUSE_REDUCTION_CODE (c) = code;
31109 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
31112 return nlist;
31115 /* OpenMP 2.5:
31116 schedule ( schedule-kind )
31117 schedule ( schedule-kind , expression )
31119 schedule-kind:
31120 static | dynamic | guided | runtime | auto
31122 OpenMP 4.5:
31123 schedule ( schedule-modifier : schedule-kind )
31124 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
31126 schedule-modifier:
31127 simd
31128 monotonic
31129 nonmonotonic */
31131 static tree
31132 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
31134 tree c, t;
31135 int modifiers = 0, nmodifiers = 0;
31137 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31138 return list;
31140 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
31142 while (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);
31146 if (strcmp ("simd", p) == 0)
31147 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
31148 else if (strcmp ("monotonic", p) == 0)
31149 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
31150 else if (strcmp ("nonmonotonic", p) == 0)
31151 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
31152 else
31153 break;
31154 cp_lexer_consume_token (parser->lexer);
31155 if (nmodifiers++ == 0
31156 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31157 cp_lexer_consume_token (parser->lexer);
31158 else
31160 cp_parser_require (parser, CPP_COLON, RT_COLON);
31161 break;
31165 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31167 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31168 const char *p = IDENTIFIER_POINTER (id);
31170 switch (p[0])
31172 case 'd':
31173 if (strcmp ("dynamic", p) != 0)
31174 goto invalid_kind;
31175 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
31176 break;
31178 case 'g':
31179 if (strcmp ("guided", p) != 0)
31180 goto invalid_kind;
31181 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
31182 break;
31184 case 'r':
31185 if (strcmp ("runtime", p) != 0)
31186 goto invalid_kind;
31187 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
31188 break;
31190 default:
31191 goto invalid_kind;
31194 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
31195 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
31196 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31197 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
31198 else
31199 goto invalid_kind;
31200 cp_lexer_consume_token (parser->lexer);
31202 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
31203 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
31204 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
31205 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
31207 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
31208 "specified");
31209 modifiers = 0;
31212 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31214 cp_token *token;
31215 cp_lexer_consume_token (parser->lexer);
31217 token = cp_lexer_peek_token (parser->lexer);
31218 t = cp_parser_assignment_expression (parser);
31220 if (t == error_mark_node)
31221 goto resync_fail;
31222 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
31223 error_at (token->location, "schedule %<runtime%> does not take "
31224 "a %<chunk_size%> parameter");
31225 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
31226 error_at (token->location, "schedule %<auto%> does not take "
31227 "a %<chunk_size%> parameter");
31228 else
31229 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
31231 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31232 goto resync_fail;
31234 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
31235 goto resync_fail;
31237 OMP_CLAUSE_SCHEDULE_KIND (c)
31238 = (enum omp_clause_schedule_kind)
31239 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
31241 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
31242 OMP_CLAUSE_CHAIN (c) = list;
31243 return c;
31245 invalid_kind:
31246 cp_parser_error (parser, "invalid schedule kind");
31247 resync_fail:
31248 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31249 /*or_comma=*/false,
31250 /*consume_paren=*/true);
31251 return list;
31254 /* OpenMP 3.0:
31255 untied */
31257 static tree
31258 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
31259 tree list, location_t location)
31261 tree c;
31263 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
31265 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
31266 OMP_CLAUSE_CHAIN (c) = list;
31267 return c;
31270 /* OpenMP 4.0:
31271 inbranch
31272 notinbranch */
31274 static tree
31275 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
31276 tree list, location_t location)
31278 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31279 tree c = build_omp_clause (location, code);
31280 OMP_CLAUSE_CHAIN (c) = list;
31281 return c;
31284 /* OpenMP 4.0:
31285 parallel
31287 sections
31288 taskgroup */
31290 static tree
31291 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
31292 enum omp_clause_code code,
31293 tree list, location_t location)
31295 tree c = build_omp_clause (location, code);
31296 OMP_CLAUSE_CHAIN (c) = list;
31297 return c;
31300 /* OpenMP 4.5:
31301 nogroup */
31303 static tree
31304 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
31305 tree list, location_t location)
31307 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
31308 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
31309 OMP_CLAUSE_CHAIN (c) = list;
31310 return c;
31313 /* OpenMP 4.5:
31314 simd
31315 threads */
31317 static tree
31318 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
31319 enum omp_clause_code code,
31320 tree list, location_t location)
31322 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31323 tree c = build_omp_clause (location, code);
31324 OMP_CLAUSE_CHAIN (c) = list;
31325 return c;
31328 /* OpenMP 4.0:
31329 num_teams ( expression ) */
31331 static tree
31332 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
31333 location_t location)
31335 tree t, c;
31337 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31338 return list;
31340 t = cp_parser_expression (parser);
31342 if (t == error_mark_node
31343 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31344 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31345 /*or_comma=*/false,
31346 /*consume_paren=*/true);
31348 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
31349 "num_teams", location);
31351 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
31352 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
31353 OMP_CLAUSE_CHAIN (c) = list;
31355 return c;
31358 /* OpenMP 4.0:
31359 thread_limit ( expression ) */
31361 static tree
31362 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
31363 location_t location)
31365 tree t, c;
31367 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31368 return list;
31370 t = cp_parser_expression (parser);
31372 if (t == error_mark_node
31373 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31374 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31375 /*or_comma=*/false,
31376 /*consume_paren=*/true);
31378 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
31379 "thread_limit", location);
31381 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
31382 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
31383 OMP_CLAUSE_CHAIN (c) = list;
31385 return c;
31388 /* OpenMP 4.0:
31389 aligned ( variable-list )
31390 aligned ( variable-list : constant-expression ) */
31392 static tree
31393 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
31395 tree nlist, c, alignment = NULL_TREE;
31396 bool colon;
31398 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31399 return list;
31401 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
31402 &colon);
31404 if (colon)
31406 alignment = cp_parser_constant_expression (parser);
31408 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31409 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31410 /*or_comma=*/false,
31411 /*consume_paren=*/true);
31413 if (alignment == error_mark_node)
31414 alignment = NULL_TREE;
31417 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31418 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
31420 return nlist;
31423 /* OpenMP 4.0:
31424 linear ( variable-list )
31425 linear ( variable-list : expression )
31427 OpenMP 4.5:
31428 linear ( modifier ( variable-list ) )
31429 linear ( modifier ( variable-list ) : expression ) */
31431 static tree
31432 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
31433 bool is_cilk_simd_fn, bool declare_simd)
31435 tree nlist, c, step = integer_one_node;
31436 bool colon;
31437 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
31439 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31440 return list;
31442 if (!is_cilk_simd_fn
31443 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31445 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31446 const char *p = IDENTIFIER_POINTER (id);
31448 if (strcmp ("ref", p) == 0)
31449 kind = OMP_CLAUSE_LINEAR_REF;
31450 else if (strcmp ("val", p) == 0)
31451 kind = OMP_CLAUSE_LINEAR_VAL;
31452 else if (strcmp ("uval", p) == 0)
31453 kind = OMP_CLAUSE_LINEAR_UVAL;
31454 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
31455 cp_lexer_consume_token (parser->lexer);
31456 else
31457 kind = OMP_CLAUSE_LINEAR_DEFAULT;
31460 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
31461 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
31462 &colon);
31463 else
31465 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
31466 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
31467 if (colon)
31468 cp_parser_require (parser, CPP_COLON, RT_COLON);
31469 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31470 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31471 /*or_comma=*/false,
31472 /*consume_paren=*/true);
31475 if (colon)
31477 step = NULL_TREE;
31478 if (declare_simd
31479 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
31480 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
31482 cp_token *token = cp_lexer_peek_token (parser->lexer);
31483 cp_parser_parse_tentatively (parser);
31484 step = cp_parser_id_expression (parser, /*template_p=*/false,
31485 /*check_dependency_p=*/true,
31486 /*template_p=*/NULL,
31487 /*declarator_p=*/false,
31488 /*optional_p=*/false);
31489 if (step != error_mark_node)
31490 step = cp_parser_lookup_name_simple (parser, step, token->location);
31491 if (step == error_mark_node)
31493 step = NULL_TREE;
31494 cp_parser_abort_tentative_parse (parser);
31496 else if (!cp_parser_parse_definitely (parser))
31497 step = NULL_TREE;
31499 if (!step)
31500 step = cp_parser_expression (parser);
31502 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
31504 sorry ("using parameters for %<linear%> step is not supported yet");
31505 step = integer_one_node;
31507 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31508 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31509 /*or_comma=*/false,
31510 /*consume_paren=*/true);
31512 if (step == error_mark_node)
31513 return list;
31516 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31518 OMP_CLAUSE_LINEAR_STEP (c) = step;
31519 OMP_CLAUSE_LINEAR_KIND (c) = kind;
31522 return nlist;
31525 /* OpenMP 4.0:
31526 safelen ( constant-expression ) */
31528 static tree
31529 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
31530 location_t location)
31532 tree t, c;
31534 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31535 return list;
31537 t = cp_parser_constant_expression (parser);
31539 if (t == error_mark_node
31540 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31541 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31542 /*or_comma=*/false,
31543 /*consume_paren=*/true);
31545 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
31547 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
31548 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
31549 OMP_CLAUSE_CHAIN (c) = list;
31551 return c;
31554 /* OpenMP 4.0:
31555 simdlen ( constant-expression ) */
31557 static tree
31558 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
31559 location_t location)
31561 tree t, c;
31563 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31564 return list;
31566 t = cp_parser_constant_expression (parser);
31568 if (t == error_mark_node
31569 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31570 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31571 /*or_comma=*/false,
31572 /*consume_paren=*/true);
31574 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
31576 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
31577 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
31578 OMP_CLAUSE_CHAIN (c) = list;
31580 return c;
31583 /* OpenMP 4.5:
31584 vec:
31585 identifier [+/- integer]
31586 vec , identifier [+/- integer]
31589 static tree
31590 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
31591 tree list)
31593 tree vec = NULL;
31595 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31597 cp_parser_error (parser, "expected identifier");
31598 return list;
31601 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31603 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
31604 tree t, identifier = cp_parser_identifier (parser);
31605 tree addend = NULL;
31607 if (identifier == error_mark_node)
31608 t = error_mark_node;
31609 else
31611 t = cp_parser_lookup_name_simple
31612 (parser, identifier,
31613 cp_lexer_peek_token (parser->lexer)->location);
31614 if (t == error_mark_node)
31615 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
31616 id_loc);
31619 bool neg = false;
31620 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
31621 neg = true;
31622 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
31624 addend = integer_zero_node;
31625 goto add_to_vector;
31627 cp_lexer_consume_token (parser->lexer);
31629 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
31631 cp_parser_error (parser, "expected integer");
31632 return list;
31635 addend = cp_lexer_peek_token (parser->lexer)->u.value;
31636 if (TREE_CODE (addend) != INTEGER_CST)
31638 cp_parser_error (parser, "expected integer");
31639 return list;
31641 cp_lexer_consume_token (parser->lexer);
31643 add_to_vector:
31644 if (t != error_mark_node)
31646 vec = tree_cons (addend, t, vec);
31647 if (neg)
31648 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
31651 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31652 break;
31654 cp_lexer_consume_token (parser->lexer);
31657 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
31659 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
31660 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
31661 OMP_CLAUSE_DECL (u) = nreverse (vec);
31662 OMP_CLAUSE_CHAIN (u) = list;
31663 return u;
31665 return list;
31668 /* OpenMP 4.0:
31669 depend ( depend-kind : variable-list )
31671 depend-kind:
31672 in | out | inout
31674 OpenMP 4.5:
31675 depend ( source )
31677 depend ( sink : vec ) */
31679 static tree
31680 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
31682 tree nlist, c;
31683 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
31685 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31686 return list;
31688 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31690 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31691 const char *p = IDENTIFIER_POINTER (id);
31693 if (strcmp ("in", p) == 0)
31694 kind = OMP_CLAUSE_DEPEND_IN;
31695 else if (strcmp ("inout", p) == 0)
31696 kind = OMP_CLAUSE_DEPEND_INOUT;
31697 else if (strcmp ("out", p) == 0)
31698 kind = OMP_CLAUSE_DEPEND_OUT;
31699 else if (strcmp ("source", p) == 0)
31700 kind = OMP_CLAUSE_DEPEND_SOURCE;
31701 else if (strcmp ("sink", p) == 0)
31702 kind = OMP_CLAUSE_DEPEND_SINK;
31703 else
31704 goto invalid_kind;
31706 else
31707 goto invalid_kind;
31709 cp_lexer_consume_token (parser->lexer);
31711 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
31713 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
31714 OMP_CLAUSE_DEPEND_KIND (c) = kind;
31715 OMP_CLAUSE_DECL (c) = NULL_TREE;
31716 OMP_CLAUSE_CHAIN (c) = list;
31717 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31718 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31719 /*or_comma=*/false,
31720 /*consume_paren=*/true);
31721 return c;
31724 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31725 goto resync_fail;
31727 if (kind == OMP_CLAUSE_DEPEND_SINK)
31728 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
31729 else
31731 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
31732 list, NULL);
31734 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31735 OMP_CLAUSE_DEPEND_KIND (c) = kind;
31737 return nlist;
31739 invalid_kind:
31740 cp_parser_error (parser, "invalid depend kind");
31741 resync_fail:
31742 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31743 /*or_comma=*/false,
31744 /*consume_paren=*/true);
31745 return list;
31748 /* OpenMP 4.0:
31749 map ( map-kind : variable-list )
31750 map ( variable-list )
31752 map-kind:
31753 alloc | to | from | tofrom
31755 OpenMP 4.5:
31756 map-kind:
31757 alloc | to | from | tofrom | release | delete
31759 map ( always [,] map-kind: variable-list ) */
31761 static tree
31762 cp_parser_omp_clause_map (cp_parser *parser, tree list)
31764 tree nlist, c;
31765 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
31766 bool always = false;
31768 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31769 return list;
31771 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31773 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31774 const char *p = IDENTIFIER_POINTER (id);
31776 if (strcmp ("always", p) == 0)
31778 int nth = 2;
31779 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
31780 nth++;
31781 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
31782 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
31783 == RID_DELETE))
31784 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
31785 == CPP_COLON))
31787 always = true;
31788 cp_lexer_consume_token (parser->lexer);
31789 if (nth == 3)
31790 cp_lexer_consume_token (parser->lexer);
31795 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
31796 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
31798 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31799 const char *p = IDENTIFIER_POINTER (id);
31801 if (strcmp ("alloc", p) == 0)
31802 kind = GOMP_MAP_ALLOC;
31803 else if (strcmp ("to", p) == 0)
31804 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
31805 else if (strcmp ("from", p) == 0)
31806 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
31807 else if (strcmp ("tofrom", p) == 0)
31808 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
31809 else if (strcmp ("release", p) == 0)
31810 kind = GOMP_MAP_RELEASE;
31811 else
31813 cp_parser_error (parser, "invalid map kind");
31814 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31815 /*or_comma=*/false,
31816 /*consume_paren=*/true);
31817 return list;
31819 cp_lexer_consume_token (parser->lexer);
31820 cp_lexer_consume_token (parser->lexer);
31822 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
31823 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
31825 kind = GOMP_MAP_DELETE;
31826 cp_lexer_consume_token (parser->lexer);
31827 cp_lexer_consume_token (parser->lexer);
31830 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
31831 NULL);
31833 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31834 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31836 return nlist;
31839 /* OpenMP 4.0:
31840 device ( expression ) */
31842 static tree
31843 cp_parser_omp_clause_device (cp_parser *parser, tree list,
31844 location_t location)
31846 tree t, c;
31848 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31849 return list;
31851 t = cp_parser_expression (parser);
31853 if (t == error_mark_node
31854 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31855 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31856 /*or_comma=*/false,
31857 /*consume_paren=*/true);
31859 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
31860 "device", location);
31862 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
31863 OMP_CLAUSE_DEVICE_ID (c) = t;
31864 OMP_CLAUSE_CHAIN (c) = list;
31866 return c;
31869 /* OpenMP 4.0:
31870 dist_schedule ( static )
31871 dist_schedule ( static , expression ) */
31873 static tree
31874 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
31875 location_t location)
31877 tree c, t;
31879 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31880 return list;
31882 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
31884 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
31885 goto invalid_kind;
31886 cp_lexer_consume_token (parser->lexer);
31888 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31890 cp_lexer_consume_token (parser->lexer);
31892 t = cp_parser_assignment_expression (parser);
31894 if (t == error_mark_node)
31895 goto resync_fail;
31896 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
31898 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31899 goto resync_fail;
31901 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
31902 goto resync_fail;
31904 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
31905 location);
31906 OMP_CLAUSE_CHAIN (c) = list;
31907 return c;
31909 invalid_kind:
31910 cp_parser_error (parser, "invalid dist_schedule kind");
31911 resync_fail:
31912 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31913 /*or_comma=*/false,
31914 /*consume_paren=*/true);
31915 return list;
31918 /* OpenMP 4.0:
31919 proc_bind ( proc-bind-kind )
31921 proc-bind-kind:
31922 master | close | spread */
31924 static tree
31925 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
31926 location_t location)
31928 tree c;
31929 enum omp_clause_proc_bind_kind kind;
31931 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31932 return list;
31934 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31936 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31937 const char *p = IDENTIFIER_POINTER (id);
31939 if (strcmp ("master", p) == 0)
31940 kind = OMP_CLAUSE_PROC_BIND_MASTER;
31941 else if (strcmp ("close", p) == 0)
31942 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
31943 else if (strcmp ("spread", p) == 0)
31944 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
31945 else
31946 goto invalid_kind;
31948 else
31949 goto invalid_kind;
31951 cp_lexer_consume_token (parser->lexer);
31952 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
31953 goto resync_fail;
31955 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
31956 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
31957 location);
31958 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
31959 OMP_CLAUSE_CHAIN (c) = list;
31960 return c;
31962 invalid_kind:
31963 cp_parser_error (parser, "invalid depend kind");
31964 resync_fail:
31965 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31966 /*or_comma=*/false,
31967 /*consume_paren=*/true);
31968 return list;
31971 /* OpenACC:
31972 async [( int-expr )] */
31974 static tree
31975 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
31977 tree c, t;
31978 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31980 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
31982 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31984 cp_lexer_consume_token (parser->lexer);
31986 t = cp_parser_expression (parser);
31987 if (t == error_mark_node
31988 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31989 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31990 /*or_comma=*/false,
31991 /*consume_paren=*/true);
31994 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
31996 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
31997 OMP_CLAUSE_ASYNC_EXPR (c) = t;
31998 OMP_CLAUSE_CHAIN (c) = list;
31999 list = c;
32001 return list;
32004 /* Parse all OpenACC clauses. The set clauses allowed by the directive
32005 is a bitmask in MASK. Return the list of clauses found. */
32007 static tree
32008 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
32009 const char *where, cp_token *pragma_tok,
32010 bool finish_p = true)
32012 tree clauses = NULL;
32013 bool first = true;
32015 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32017 location_t here;
32018 pragma_omp_clause c_kind;
32019 omp_clause_code code;
32020 const char *c_name;
32021 tree prev = clauses;
32023 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32024 cp_lexer_consume_token (parser->lexer);
32026 here = cp_lexer_peek_token (parser->lexer)->location;
32027 c_kind = cp_parser_omp_clause_name (parser);
32029 switch (c_kind)
32031 case PRAGMA_OACC_CLAUSE_ASYNC:
32032 clauses = cp_parser_oacc_clause_async (parser, clauses);
32033 c_name = "async";
32034 break;
32035 case PRAGMA_OACC_CLAUSE_AUTO:
32036 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
32037 clauses, here);
32038 c_name = "auto";
32039 break;
32040 case PRAGMA_OACC_CLAUSE_COLLAPSE:
32041 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
32042 c_name = "collapse";
32043 break;
32044 case PRAGMA_OACC_CLAUSE_COPY:
32045 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32046 c_name = "copy";
32047 break;
32048 case PRAGMA_OACC_CLAUSE_COPYIN:
32049 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32050 c_name = "copyin";
32051 break;
32052 case PRAGMA_OACC_CLAUSE_COPYOUT:
32053 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32054 c_name = "copyout";
32055 break;
32056 case PRAGMA_OACC_CLAUSE_CREATE:
32057 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32058 c_name = "create";
32059 break;
32060 case PRAGMA_OACC_CLAUSE_DELETE:
32061 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32062 c_name = "delete";
32063 break;
32064 case PRAGMA_OMP_CLAUSE_DEFAULT:
32065 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
32066 c_name = "default";
32067 break;
32068 case PRAGMA_OACC_CLAUSE_DEVICE:
32069 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32070 c_name = "device";
32071 break;
32072 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
32073 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
32074 c_name = "deviceptr";
32075 break;
32076 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
32077 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32078 c_name = "device_resident";
32079 break;
32080 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
32081 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32082 clauses);
32083 c_name = "firstprivate";
32084 break;
32085 case PRAGMA_OACC_CLAUSE_GANG:
32086 c_name = "gang";
32087 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
32088 c_name, clauses);
32089 break;
32090 case PRAGMA_OACC_CLAUSE_HOST:
32091 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32092 c_name = "host";
32093 break;
32094 case PRAGMA_OACC_CLAUSE_IF:
32095 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
32096 c_name = "if";
32097 break;
32098 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
32099 clauses = cp_parser_oacc_simple_clause (parser,
32100 OMP_CLAUSE_INDEPENDENT,
32101 clauses, here);
32102 c_name = "independent";
32103 break;
32104 case PRAGMA_OACC_CLAUSE_LINK:
32105 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32106 c_name = "link";
32107 break;
32108 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
32109 code = OMP_CLAUSE_NUM_GANGS;
32110 c_name = "num_gangs";
32111 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32112 clauses);
32113 break;
32114 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
32115 c_name = "num_workers";
32116 code = OMP_CLAUSE_NUM_WORKERS;
32117 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32118 clauses);
32119 break;
32120 case PRAGMA_OACC_CLAUSE_PRESENT:
32121 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32122 c_name = "present";
32123 break;
32124 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
32125 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32126 c_name = "present_or_copy";
32127 break;
32128 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
32129 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32130 c_name = "present_or_copyin";
32131 break;
32132 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
32133 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32134 c_name = "present_or_copyout";
32135 break;
32136 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
32137 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32138 c_name = "present_or_create";
32139 break;
32140 case PRAGMA_OACC_CLAUSE_PRIVATE:
32141 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
32142 clauses);
32143 c_name = "private";
32144 break;
32145 case PRAGMA_OACC_CLAUSE_REDUCTION:
32146 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32147 c_name = "reduction";
32148 break;
32149 case PRAGMA_OACC_CLAUSE_SELF:
32150 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32151 c_name = "self";
32152 break;
32153 case PRAGMA_OACC_CLAUSE_SEQ:
32154 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
32155 clauses, here);
32156 c_name = "seq";
32157 break;
32158 case PRAGMA_OACC_CLAUSE_TILE:
32159 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
32160 c_name = "tile";
32161 break;
32162 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
32163 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
32164 clauses);
32165 c_name = "use_device";
32166 break;
32167 case PRAGMA_OACC_CLAUSE_VECTOR:
32168 c_name = "vector";
32169 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
32170 c_name, clauses);
32171 break;
32172 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
32173 c_name = "vector_length";
32174 code = OMP_CLAUSE_VECTOR_LENGTH;
32175 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32176 clauses);
32177 break;
32178 case PRAGMA_OACC_CLAUSE_WAIT:
32179 clauses = cp_parser_oacc_clause_wait (parser, clauses);
32180 c_name = "wait";
32181 break;
32182 case PRAGMA_OACC_CLAUSE_WORKER:
32183 c_name = "worker";
32184 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
32185 c_name, clauses);
32186 break;
32187 default:
32188 cp_parser_error (parser, "expected %<#pragma acc%> clause");
32189 goto saw_error;
32192 first = false;
32194 if (((mask >> c_kind) & 1) == 0)
32196 /* Remove the invalid clause(s) from the list to avoid
32197 confusing the rest of the compiler. */
32198 clauses = prev;
32199 error_at (here, "%qs is not valid for %qs", c_name, where);
32203 saw_error:
32204 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32206 if (finish_p)
32207 return finish_omp_clauses (clauses, false);
32209 return clauses;
32212 /* Parse all OpenMP clauses. The set clauses allowed by the directive
32213 is a bitmask in MASK. Return the list of clauses found; the result
32214 of clause default goes in *pdefault. */
32216 static tree
32217 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
32218 const char *where, cp_token *pragma_tok,
32219 bool finish_p = true)
32221 tree clauses = NULL;
32222 bool first = true;
32223 cp_token *token = NULL;
32225 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32227 pragma_omp_clause c_kind;
32228 const char *c_name;
32229 tree prev = clauses;
32231 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32232 cp_lexer_consume_token (parser->lexer);
32234 token = cp_lexer_peek_token (parser->lexer);
32235 c_kind = cp_parser_omp_clause_name (parser);
32237 switch (c_kind)
32239 case PRAGMA_OMP_CLAUSE_COLLAPSE:
32240 clauses = cp_parser_omp_clause_collapse (parser, clauses,
32241 token->location);
32242 c_name = "collapse";
32243 break;
32244 case PRAGMA_OMP_CLAUSE_COPYIN:
32245 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
32246 c_name = "copyin";
32247 break;
32248 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
32249 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
32250 clauses);
32251 c_name = "copyprivate";
32252 break;
32253 case PRAGMA_OMP_CLAUSE_DEFAULT:
32254 clauses = cp_parser_omp_clause_default (parser, clauses,
32255 token->location, false);
32256 c_name = "default";
32257 break;
32258 case PRAGMA_OMP_CLAUSE_FINAL:
32259 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
32260 c_name = "final";
32261 break;
32262 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
32263 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32264 clauses);
32265 c_name = "firstprivate";
32266 break;
32267 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
32268 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
32269 token->location);
32270 c_name = "grainsize";
32271 break;
32272 case PRAGMA_OMP_CLAUSE_HINT:
32273 clauses = cp_parser_omp_clause_hint (parser, clauses,
32274 token->location);
32275 c_name = "hint";
32276 break;
32277 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
32278 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
32279 token->location);
32280 c_name = "defaultmap";
32281 break;
32282 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
32283 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
32284 clauses);
32285 c_name = "use_device_ptr";
32286 break;
32287 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
32288 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
32289 clauses);
32290 c_name = "is_device_ptr";
32291 break;
32292 case PRAGMA_OMP_CLAUSE_IF:
32293 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
32294 true);
32295 c_name = "if";
32296 break;
32297 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
32298 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
32299 clauses);
32300 c_name = "lastprivate";
32301 break;
32302 case PRAGMA_OMP_CLAUSE_MERGEABLE:
32303 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
32304 token->location);
32305 c_name = "mergeable";
32306 break;
32307 case PRAGMA_OMP_CLAUSE_NOWAIT:
32308 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
32309 c_name = "nowait";
32310 break;
32311 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
32312 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
32313 token->location);
32314 c_name = "num_tasks";
32315 break;
32316 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
32317 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
32318 token->location);
32319 c_name = "num_threads";
32320 break;
32321 case PRAGMA_OMP_CLAUSE_ORDERED:
32322 clauses = cp_parser_omp_clause_ordered (parser, clauses,
32323 token->location);
32324 c_name = "ordered";
32325 break;
32326 case PRAGMA_OMP_CLAUSE_PRIORITY:
32327 clauses = cp_parser_omp_clause_priority (parser, clauses,
32328 token->location);
32329 c_name = "priority";
32330 break;
32331 case PRAGMA_OMP_CLAUSE_PRIVATE:
32332 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
32333 clauses);
32334 c_name = "private";
32335 break;
32336 case PRAGMA_OMP_CLAUSE_REDUCTION:
32337 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32338 c_name = "reduction";
32339 break;
32340 case PRAGMA_OMP_CLAUSE_SCHEDULE:
32341 clauses = cp_parser_omp_clause_schedule (parser, clauses,
32342 token->location);
32343 c_name = "schedule";
32344 break;
32345 case PRAGMA_OMP_CLAUSE_SHARED:
32346 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
32347 clauses);
32348 c_name = "shared";
32349 break;
32350 case PRAGMA_OMP_CLAUSE_UNTIED:
32351 clauses = cp_parser_omp_clause_untied (parser, clauses,
32352 token->location);
32353 c_name = "untied";
32354 break;
32355 case PRAGMA_OMP_CLAUSE_INBRANCH:
32356 case PRAGMA_CILK_CLAUSE_MASK:
32357 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
32358 clauses, token->location);
32359 c_name = "inbranch";
32360 break;
32361 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
32362 case PRAGMA_CILK_CLAUSE_NOMASK:
32363 clauses = cp_parser_omp_clause_branch (parser,
32364 OMP_CLAUSE_NOTINBRANCH,
32365 clauses, token->location);
32366 c_name = "notinbranch";
32367 break;
32368 case PRAGMA_OMP_CLAUSE_PARALLEL:
32369 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
32370 clauses, token->location);
32371 c_name = "parallel";
32372 if (!first)
32374 clause_not_first:
32375 error_at (token->location, "%qs must be the first clause of %qs",
32376 c_name, where);
32377 clauses = prev;
32379 break;
32380 case PRAGMA_OMP_CLAUSE_FOR:
32381 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
32382 clauses, token->location);
32383 c_name = "for";
32384 if (!first)
32385 goto clause_not_first;
32386 break;
32387 case PRAGMA_OMP_CLAUSE_SECTIONS:
32388 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
32389 clauses, token->location);
32390 c_name = "sections";
32391 if (!first)
32392 goto clause_not_first;
32393 break;
32394 case PRAGMA_OMP_CLAUSE_TASKGROUP:
32395 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
32396 clauses, token->location);
32397 c_name = "taskgroup";
32398 if (!first)
32399 goto clause_not_first;
32400 break;
32401 case PRAGMA_OMP_CLAUSE_LINK:
32402 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
32403 c_name = "to";
32404 break;
32405 case PRAGMA_OMP_CLAUSE_TO:
32406 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
32407 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
32408 clauses);
32409 else
32410 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
32411 c_name = "to";
32412 break;
32413 case PRAGMA_OMP_CLAUSE_FROM:
32414 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
32415 c_name = "from";
32416 break;
32417 case PRAGMA_OMP_CLAUSE_UNIFORM:
32418 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
32419 clauses);
32420 c_name = "uniform";
32421 break;
32422 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
32423 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
32424 token->location);
32425 c_name = "num_teams";
32426 break;
32427 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
32428 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
32429 token->location);
32430 c_name = "thread_limit";
32431 break;
32432 case PRAGMA_OMP_CLAUSE_ALIGNED:
32433 clauses = cp_parser_omp_clause_aligned (parser, clauses);
32434 c_name = "aligned";
32435 break;
32436 case PRAGMA_OMP_CLAUSE_LINEAR:
32438 bool cilk_simd_fn = false, declare_simd = false;
32439 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
32440 cilk_simd_fn = true;
32441 else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
32442 declare_simd = true;
32443 clauses = cp_parser_omp_clause_linear (parser, clauses,
32444 cilk_simd_fn, declare_simd);
32446 c_name = "linear";
32447 break;
32448 case PRAGMA_OMP_CLAUSE_DEPEND:
32449 clauses = cp_parser_omp_clause_depend (parser, clauses,
32450 token->location);
32451 c_name = "depend";
32452 break;
32453 case PRAGMA_OMP_CLAUSE_MAP:
32454 clauses = cp_parser_omp_clause_map (parser, clauses);
32455 c_name = "map";
32456 break;
32457 case PRAGMA_OMP_CLAUSE_DEVICE:
32458 clauses = cp_parser_omp_clause_device (parser, clauses,
32459 token->location);
32460 c_name = "device";
32461 break;
32462 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
32463 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
32464 token->location);
32465 c_name = "dist_schedule";
32466 break;
32467 case PRAGMA_OMP_CLAUSE_PROC_BIND:
32468 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
32469 token->location);
32470 c_name = "proc_bind";
32471 break;
32472 case PRAGMA_OMP_CLAUSE_SAFELEN:
32473 clauses = cp_parser_omp_clause_safelen (parser, clauses,
32474 token->location);
32475 c_name = "safelen";
32476 break;
32477 case PRAGMA_OMP_CLAUSE_SIMDLEN:
32478 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
32479 token->location);
32480 c_name = "simdlen";
32481 break;
32482 case PRAGMA_OMP_CLAUSE_NOGROUP:
32483 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
32484 token->location);
32485 c_name = "nogroup";
32486 break;
32487 case PRAGMA_OMP_CLAUSE_THREADS:
32488 clauses
32489 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
32490 clauses, token->location);
32491 c_name = "threads";
32492 break;
32493 case PRAGMA_OMP_CLAUSE_SIMD:
32494 clauses
32495 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
32496 clauses, token->location);
32497 c_name = "simd";
32498 break;
32499 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
32500 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
32501 c_name = "simdlen";
32502 break;
32503 default:
32504 cp_parser_error (parser, "expected %<#pragma omp%> clause");
32505 goto saw_error;
32508 first = false;
32510 if (((mask >> c_kind) & 1) == 0)
32512 /* Remove the invalid clause(s) from the list to avoid
32513 confusing the rest of the compiler. */
32514 clauses = prev;
32515 error_at (token->location, "%qs is not valid for %qs", c_name, where);
32518 saw_error:
32519 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
32520 no reason to skip to the end. */
32521 if (!(flag_cilkplus && pragma_tok == NULL))
32522 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32523 if (finish_p)
32525 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
32526 return finish_omp_clauses (clauses, false, true);
32527 else
32528 return finish_omp_clauses (clauses, true);
32530 return clauses;
32533 /* OpenMP 2.5:
32534 structured-block:
32535 statement
32537 In practice, we're also interested in adding the statement to an
32538 outer node. So it is convenient if we work around the fact that
32539 cp_parser_statement calls add_stmt. */
32541 static unsigned
32542 cp_parser_begin_omp_structured_block (cp_parser *parser)
32544 unsigned save = parser->in_statement;
32546 /* Only move the values to IN_OMP_BLOCK if they weren't false.
32547 This preserves the "not within loop or switch" style error messages
32548 for nonsense cases like
32549 void foo() {
32550 #pragma omp single
32551 break;
32554 if (parser->in_statement)
32555 parser->in_statement = IN_OMP_BLOCK;
32557 return save;
32560 static void
32561 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
32563 parser->in_statement = save;
32566 static tree
32567 cp_parser_omp_structured_block (cp_parser *parser)
32569 tree stmt = begin_omp_structured_block ();
32570 unsigned int save = cp_parser_begin_omp_structured_block (parser);
32572 cp_parser_statement (parser, NULL_TREE, false, NULL);
32574 cp_parser_end_omp_structured_block (parser, save);
32575 return finish_omp_structured_block (stmt);
32578 /* OpenMP 2.5:
32579 # pragma omp atomic new-line
32580 expression-stmt
32582 expression-stmt:
32583 x binop= expr | x++ | ++x | x-- | --x
32584 binop:
32585 +, *, -, /, &, ^, |, <<, >>
32587 where x is an lvalue expression with scalar type.
32589 OpenMP 3.1:
32590 # pragma omp atomic new-line
32591 update-stmt
32593 # pragma omp atomic read new-line
32594 read-stmt
32596 # pragma omp atomic write new-line
32597 write-stmt
32599 # pragma omp atomic update new-line
32600 update-stmt
32602 # pragma omp atomic capture new-line
32603 capture-stmt
32605 # pragma omp atomic capture new-line
32606 capture-block
32608 read-stmt:
32609 v = x
32610 write-stmt:
32611 x = expr
32612 update-stmt:
32613 expression-stmt | x = x binop expr
32614 capture-stmt:
32615 v = expression-stmt
32616 capture-block:
32617 { v = x; update-stmt; } | { update-stmt; v = x; }
32619 OpenMP 4.0:
32620 update-stmt:
32621 expression-stmt | x = x binop expr | x = expr binop x
32622 capture-stmt:
32623 v = update-stmt
32624 capture-block:
32625 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
32627 where x and v are lvalue expressions with scalar type. */
32629 static void
32630 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
32632 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
32633 tree rhs1 = NULL_TREE, orig_lhs;
32634 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
32635 bool structured_block = false;
32636 bool seq_cst = false;
32638 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32640 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32641 const char *p = IDENTIFIER_POINTER (id);
32643 if (!strcmp (p, "seq_cst"))
32645 seq_cst = true;
32646 cp_lexer_consume_token (parser->lexer);
32647 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
32648 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
32649 cp_lexer_consume_token (parser->lexer);
32652 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32654 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32655 const char *p = IDENTIFIER_POINTER (id);
32657 if (!strcmp (p, "read"))
32658 code = OMP_ATOMIC_READ;
32659 else if (!strcmp (p, "write"))
32660 code = NOP_EXPR;
32661 else if (!strcmp (p, "update"))
32662 code = OMP_ATOMIC;
32663 else if (!strcmp (p, "capture"))
32664 code = OMP_ATOMIC_CAPTURE_NEW;
32665 else
32666 p = NULL;
32667 if (p)
32668 cp_lexer_consume_token (parser->lexer);
32670 if (!seq_cst)
32672 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
32673 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
32674 cp_lexer_consume_token (parser->lexer);
32676 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32678 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32679 const char *p = IDENTIFIER_POINTER (id);
32681 if (!strcmp (p, "seq_cst"))
32683 seq_cst = true;
32684 cp_lexer_consume_token (parser->lexer);
32688 cp_parser_require_pragma_eol (parser, pragma_tok);
32690 switch (code)
32692 case OMP_ATOMIC_READ:
32693 case NOP_EXPR: /* atomic write */
32694 v = cp_parser_unary_expression (parser);
32695 if (v == error_mark_node)
32696 goto saw_error;
32697 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32698 goto saw_error;
32699 if (code == NOP_EXPR)
32700 lhs = cp_parser_expression (parser);
32701 else
32702 lhs = cp_parser_unary_expression (parser);
32703 if (lhs == error_mark_node)
32704 goto saw_error;
32705 if (code == NOP_EXPR)
32707 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
32708 opcode. */
32709 code = OMP_ATOMIC;
32710 rhs = lhs;
32711 lhs = v;
32712 v = NULL_TREE;
32714 goto done;
32715 case OMP_ATOMIC_CAPTURE_NEW:
32716 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
32718 cp_lexer_consume_token (parser->lexer);
32719 structured_block = true;
32721 else
32723 v = cp_parser_unary_expression (parser);
32724 if (v == error_mark_node)
32725 goto saw_error;
32726 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32727 goto saw_error;
32729 default:
32730 break;
32733 restart:
32734 lhs = cp_parser_unary_expression (parser);
32735 orig_lhs = lhs;
32736 switch (TREE_CODE (lhs))
32738 case ERROR_MARK:
32739 goto saw_error;
32741 case POSTINCREMENT_EXPR:
32742 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
32743 code = OMP_ATOMIC_CAPTURE_OLD;
32744 /* FALLTHROUGH */
32745 case PREINCREMENT_EXPR:
32746 lhs = TREE_OPERAND (lhs, 0);
32747 opcode = PLUS_EXPR;
32748 rhs = integer_one_node;
32749 break;
32751 case POSTDECREMENT_EXPR:
32752 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
32753 code = OMP_ATOMIC_CAPTURE_OLD;
32754 /* FALLTHROUGH */
32755 case PREDECREMENT_EXPR:
32756 lhs = TREE_OPERAND (lhs, 0);
32757 opcode = MINUS_EXPR;
32758 rhs = integer_one_node;
32759 break;
32761 case COMPOUND_EXPR:
32762 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
32763 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
32764 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
32765 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
32766 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
32767 (TREE_OPERAND (lhs, 1), 0), 0)))
32768 == BOOLEAN_TYPE)
32769 /* Undo effects of boolean_increment for post {in,de}crement. */
32770 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
32771 /* FALLTHRU */
32772 case MODIFY_EXPR:
32773 if (TREE_CODE (lhs) == MODIFY_EXPR
32774 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
32776 /* Undo effects of boolean_increment. */
32777 if (integer_onep (TREE_OPERAND (lhs, 1)))
32779 /* This is pre or post increment. */
32780 rhs = TREE_OPERAND (lhs, 1);
32781 lhs = TREE_OPERAND (lhs, 0);
32782 opcode = NOP_EXPR;
32783 if (code == OMP_ATOMIC_CAPTURE_NEW
32784 && !structured_block
32785 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
32786 code = OMP_ATOMIC_CAPTURE_OLD;
32787 break;
32790 /* FALLTHRU */
32791 default:
32792 switch (cp_lexer_peek_token (parser->lexer)->type)
32794 case CPP_MULT_EQ:
32795 opcode = MULT_EXPR;
32796 break;
32797 case CPP_DIV_EQ:
32798 opcode = TRUNC_DIV_EXPR;
32799 break;
32800 case CPP_PLUS_EQ:
32801 opcode = PLUS_EXPR;
32802 break;
32803 case CPP_MINUS_EQ:
32804 opcode = MINUS_EXPR;
32805 break;
32806 case CPP_LSHIFT_EQ:
32807 opcode = LSHIFT_EXPR;
32808 break;
32809 case CPP_RSHIFT_EQ:
32810 opcode = RSHIFT_EXPR;
32811 break;
32812 case CPP_AND_EQ:
32813 opcode = BIT_AND_EXPR;
32814 break;
32815 case CPP_OR_EQ:
32816 opcode = BIT_IOR_EXPR;
32817 break;
32818 case CPP_XOR_EQ:
32819 opcode = BIT_XOR_EXPR;
32820 break;
32821 case CPP_EQ:
32822 enum cp_parser_prec oprec;
32823 cp_token *token;
32824 cp_lexer_consume_token (parser->lexer);
32825 cp_parser_parse_tentatively (parser);
32826 rhs1 = cp_parser_simple_cast_expression (parser);
32827 if (rhs1 == error_mark_node)
32829 cp_parser_abort_tentative_parse (parser);
32830 cp_parser_simple_cast_expression (parser);
32831 goto saw_error;
32833 token = cp_lexer_peek_token (parser->lexer);
32834 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
32836 cp_parser_abort_tentative_parse (parser);
32837 cp_parser_parse_tentatively (parser);
32838 rhs = cp_parser_binary_expression (parser, false, true,
32839 PREC_NOT_OPERATOR, NULL);
32840 if (rhs == error_mark_node)
32842 cp_parser_abort_tentative_parse (parser);
32843 cp_parser_binary_expression (parser, false, true,
32844 PREC_NOT_OPERATOR, NULL);
32845 goto saw_error;
32847 switch (TREE_CODE (rhs))
32849 case MULT_EXPR:
32850 case TRUNC_DIV_EXPR:
32851 case RDIV_EXPR:
32852 case PLUS_EXPR:
32853 case MINUS_EXPR:
32854 case LSHIFT_EXPR:
32855 case RSHIFT_EXPR:
32856 case BIT_AND_EXPR:
32857 case BIT_IOR_EXPR:
32858 case BIT_XOR_EXPR:
32859 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
32861 if (cp_parser_parse_definitely (parser))
32863 opcode = TREE_CODE (rhs);
32864 rhs1 = TREE_OPERAND (rhs, 0);
32865 rhs = TREE_OPERAND (rhs, 1);
32866 goto stmt_done;
32868 else
32869 goto saw_error;
32871 break;
32872 default:
32873 break;
32875 cp_parser_abort_tentative_parse (parser);
32876 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
32878 rhs = cp_parser_expression (parser);
32879 if (rhs == error_mark_node)
32880 goto saw_error;
32881 opcode = NOP_EXPR;
32882 rhs1 = NULL_TREE;
32883 goto stmt_done;
32885 cp_parser_error (parser,
32886 "invalid form of %<#pragma omp atomic%>");
32887 goto saw_error;
32889 if (!cp_parser_parse_definitely (parser))
32890 goto saw_error;
32891 switch (token->type)
32893 case CPP_SEMICOLON:
32894 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
32896 code = OMP_ATOMIC_CAPTURE_OLD;
32897 v = lhs;
32898 lhs = NULL_TREE;
32899 lhs1 = rhs1;
32900 rhs1 = NULL_TREE;
32901 cp_lexer_consume_token (parser->lexer);
32902 goto restart;
32904 else if (structured_block)
32906 opcode = NOP_EXPR;
32907 rhs = rhs1;
32908 rhs1 = NULL_TREE;
32909 goto stmt_done;
32911 cp_parser_error (parser,
32912 "invalid form of %<#pragma omp atomic%>");
32913 goto saw_error;
32914 case CPP_MULT:
32915 opcode = MULT_EXPR;
32916 break;
32917 case CPP_DIV:
32918 opcode = TRUNC_DIV_EXPR;
32919 break;
32920 case CPP_PLUS:
32921 opcode = PLUS_EXPR;
32922 break;
32923 case CPP_MINUS:
32924 opcode = MINUS_EXPR;
32925 break;
32926 case CPP_LSHIFT:
32927 opcode = LSHIFT_EXPR;
32928 break;
32929 case CPP_RSHIFT:
32930 opcode = RSHIFT_EXPR;
32931 break;
32932 case CPP_AND:
32933 opcode = BIT_AND_EXPR;
32934 break;
32935 case CPP_OR:
32936 opcode = BIT_IOR_EXPR;
32937 break;
32938 case CPP_XOR:
32939 opcode = BIT_XOR_EXPR;
32940 break;
32941 default:
32942 cp_parser_error (parser,
32943 "invalid operator for %<#pragma omp atomic%>");
32944 goto saw_error;
32946 oprec = TOKEN_PRECEDENCE (token);
32947 gcc_assert (oprec != PREC_NOT_OPERATOR);
32948 if (commutative_tree_code (opcode))
32949 oprec = (enum cp_parser_prec) (oprec - 1);
32950 cp_lexer_consume_token (parser->lexer);
32951 rhs = cp_parser_binary_expression (parser, false, false,
32952 oprec, NULL);
32953 if (rhs == error_mark_node)
32954 goto saw_error;
32955 goto stmt_done;
32956 /* FALLTHROUGH */
32957 default:
32958 cp_parser_error (parser,
32959 "invalid operator for %<#pragma omp atomic%>");
32960 goto saw_error;
32962 cp_lexer_consume_token (parser->lexer);
32964 rhs = cp_parser_expression (parser);
32965 if (rhs == error_mark_node)
32966 goto saw_error;
32967 break;
32969 stmt_done:
32970 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
32972 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
32973 goto saw_error;
32974 v = cp_parser_unary_expression (parser);
32975 if (v == error_mark_node)
32976 goto saw_error;
32977 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32978 goto saw_error;
32979 lhs1 = cp_parser_unary_expression (parser);
32980 if (lhs1 == error_mark_node)
32981 goto saw_error;
32983 if (structured_block)
32985 cp_parser_consume_semicolon_at_end_of_statement (parser);
32986 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
32988 done:
32989 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
32990 if (!structured_block)
32991 cp_parser_consume_semicolon_at_end_of_statement (parser);
32992 return;
32994 saw_error:
32995 cp_parser_skip_to_end_of_block_or_statement (parser);
32996 if (structured_block)
32998 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
32999 cp_lexer_consume_token (parser->lexer);
33000 else if (code == OMP_ATOMIC_CAPTURE_NEW)
33002 cp_parser_skip_to_end_of_block_or_statement (parser);
33003 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33004 cp_lexer_consume_token (parser->lexer);
33010 /* OpenMP 2.5:
33011 # pragma omp barrier new-line */
33013 static void
33014 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
33016 cp_parser_require_pragma_eol (parser, pragma_tok);
33017 finish_omp_barrier ();
33020 /* OpenMP 2.5:
33021 # pragma omp critical [(name)] new-line
33022 structured-block
33024 OpenMP 4.5:
33025 # pragma omp critical [(name) [hint(expression)]] new-line
33026 structured-block */
33028 #define OMP_CRITICAL_CLAUSE_MASK \
33029 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
33031 static tree
33032 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
33034 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
33036 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33038 cp_lexer_consume_token (parser->lexer);
33040 name = cp_parser_identifier (parser);
33042 if (name == error_mark_node
33043 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33044 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33045 /*or_comma=*/false,
33046 /*consume_paren=*/true);
33047 if (name == error_mark_node)
33048 name = NULL;
33050 clauses = cp_parser_omp_all_clauses (parser,
33051 OMP_CRITICAL_CLAUSE_MASK,
33052 "#pragma omp critical", pragma_tok);
33054 else
33055 cp_parser_require_pragma_eol (parser, pragma_tok);
33057 stmt = cp_parser_omp_structured_block (parser);
33058 return c_finish_omp_critical (input_location, stmt, name, clauses);
33061 /* OpenMP 2.5:
33062 # pragma omp flush flush-vars[opt] new-line
33064 flush-vars:
33065 ( variable-list ) */
33067 static void
33068 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
33070 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33071 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
33072 cp_parser_require_pragma_eol (parser, pragma_tok);
33074 finish_omp_flush ();
33077 /* Helper function, to parse omp for increment expression. */
33079 static tree
33080 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
33082 tree cond = cp_parser_binary_expression (parser, false, true,
33083 PREC_NOT_OPERATOR, NULL);
33084 if (cond == error_mark_node
33085 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33087 cp_parser_skip_to_end_of_statement (parser);
33088 return error_mark_node;
33091 switch (TREE_CODE (cond))
33093 case GT_EXPR:
33094 case GE_EXPR:
33095 case LT_EXPR:
33096 case LE_EXPR:
33097 break;
33098 case NE_EXPR:
33099 if (code == CILK_SIMD || code == CILK_FOR)
33100 break;
33101 /* Fall through: OpenMP disallows NE_EXPR. */
33102 default:
33103 return error_mark_node;
33106 /* If decl is an iterator, preserve LHS and RHS of the relational
33107 expr until finish_omp_for. */
33108 if (decl
33109 && (type_dependent_expression_p (decl)
33110 || CLASS_TYPE_P (TREE_TYPE (decl))))
33111 return cond;
33113 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
33114 TREE_CODE (cond),
33115 TREE_OPERAND (cond, 0), ERROR_MARK,
33116 TREE_OPERAND (cond, 1), ERROR_MARK,
33117 /*overload=*/NULL, tf_warning_or_error);
33120 /* Helper function, to parse omp for increment expression. */
33122 static tree
33123 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
33125 cp_token *token = cp_lexer_peek_token (parser->lexer);
33126 enum tree_code op;
33127 tree lhs, rhs;
33128 cp_id_kind idk;
33129 bool decl_first;
33131 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
33133 op = (token->type == CPP_PLUS_PLUS
33134 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
33135 cp_lexer_consume_token (parser->lexer);
33136 lhs = cp_parser_simple_cast_expression (parser);
33137 if (lhs != decl
33138 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
33139 return error_mark_node;
33140 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
33143 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
33144 if (lhs != decl
33145 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
33146 return error_mark_node;
33148 token = cp_lexer_peek_token (parser->lexer);
33149 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
33151 op = (token->type == CPP_PLUS_PLUS
33152 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
33153 cp_lexer_consume_token (parser->lexer);
33154 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
33157 op = cp_parser_assignment_operator_opt (parser);
33158 if (op == ERROR_MARK)
33159 return error_mark_node;
33161 if (op != NOP_EXPR)
33163 rhs = cp_parser_assignment_expression (parser);
33164 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
33165 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
33168 lhs = cp_parser_binary_expression (parser, false, false,
33169 PREC_ADDITIVE_EXPRESSION, NULL);
33170 token = cp_lexer_peek_token (parser->lexer);
33171 decl_first = (lhs == decl
33172 || (processing_template_decl && cp_tree_equal (lhs, decl)));
33173 if (decl_first)
33174 lhs = NULL_TREE;
33175 if (token->type != CPP_PLUS
33176 && token->type != CPP_MINUS)
33177 return error_mark_node;
33181 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
33182 cp_lexer_consume_token (parser->lexer);
33183 rhs = cp_parser_binary_expression (parser, false, false,
33184 PREC_ADDITIVE_EXPRESSION, NULL);
33185 token = cp_lexer_peek_token (parser->lexer);
33186 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
33188 if (lhs == NULL_TREE)
33190 if (op == PLUS_EXPR)
33191 lhs = rhs;
33192 else
33193 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
33194 tf_warning_or_error);
33196 else
33197 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
33198 ERROR_MARK, NULL, tf_warning_or_error);
33201 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
33203 if (!decl_first)
33205 if ((rhs != decl
33206 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
33207 || op == MINUS_EXPR)
33208 return error_mark_node;
33209 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
33211 else
33212 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
33214 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
33217 /* Parse the initialization statement of either an OpenMP for loop or
33218 a Cilk Plus for loop.
33220 Return true if the resulting construct should have an
33221 OMP_CLAUSE_PRIVATE added to it. */
33223 static tree
33224 cp_parser_omp_for_loop_init (cp_parser *parser,
33225 enum tree_code code,
33226 tree &this_pre_body,
33227 vec<tree, va_gc> *for_block,
33228 tree &init,
33229 tree &orig_init,
33230 tree &decl,
33231 tree &real_decl)
33233 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33234 return NULL_TREE;
33236 tree add_private_clause = NULL_TREE;
33238 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
33240 init-expr:
33241 var = lb
33242 integer-type var = lb
33243 random-access-iterator-type var = lb
33244 pointer-type var = lb
33246 cp_decl_specifier_seq type_specifiers;
33248 /* First, try to parse as an initialized declaration. See
33249 cp_parser_condition, from whence the bulk of this is copied. */
33251 cp_parser_parse_tentatively (parser);
33252 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
33253 /*is_trailing_return=*/false,
33254 &type_specifiers);
33255 if (cp_parser_parse_definitely (parser))
33257 /* If parsing a type specifier seq succeeded, then this
33258 MUST be a initialized declaration. */
33259 tree asm_specification, attributes;
33260 cp_declarator *declarator;
33262 declarator = cp_parser_declarator (parser,
33263 CP_PARSER_DECLARATOR_NAMED,
33264 /*ctor_dtor_or_conv_p=*/NULL,
33265 /*parenthesized_p=*/NULL,
33266 /*member_p=*/false,
33267 /*friend_p=*/false);
33268 attributes = cp_parser_attributes_opt (parser);
33269 asm_specification = cp_parser_asm_specification_opt (parser);
33271 if (declarator == cp_error_declarator)
33272 cp_parser_skip_to_end_of_statement (parser);
33274 else
33276 tree pushed_scope, auto_node;
33278 decl = start_decl (declarator, &type_specifiers,
33279 SD_INITIALIZED, attributes,
33280 /*prefix_attributes=*/NULL_TREE,
33281 &pushed_scope);
33283 auto_node = type_uses_auto (TREE_TYPE (decl));
33284 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
33286 if (cp_lexer_next_token_is (parser->lexer,
33287 CPP_OPEN_PAREN))
33289 if (code != CILK_SIMD && code != CILK_FOR)
33290 error ("parenthesized initialization is not allowed in "
33291 "OpenMP %<for%> loop");
33292 else
33293 error ("parenthesized initialization is "
33294 "not allowed in for-loop");
33296 else
33297 /* Trigger an error. */
33298 cp_parser_require (parser, CPP_EQ, RT_EQ);
33300 init = error_mark_node;
33301 cp_parser_skip_to_end_of_statement (parser);
33303 else if (CLASS_TYPE_P (TREE_TYPE (decl))
33304 || type_dependent_expression_p (decl)
33305 || auto_node)
33307 bool is_direct_init, is_non_constant_init;
33309 init = cp_parser_initializer (parser,
33310 &is_direct_init,
33311 &is_non_constant_init);
33313 if (auto_node)
33315 TREE_TYPE (decl)
33316 = do_auto_deduction (TREE_TYPE (decl), init,
33317 auto_node);
33319 if (!CLASS_TYPE_P (TREE_TYPE (decl))
33320 && !type_dependent_expression_p (decl))
33321 goto non_class;
33324 cp_finish_decl (decl, init, !is_non_constant_init,
33325 asm_specification,
33326 LOOKUP_ONLYCONVERTING);
33327 orig_init = init;
33328 if (CLASS_TYPE_P (TREE_TYPE (decl)))
33330 vec_safe_push (for_block, this_pre_body);
33331 init = NULL_TREE;
33333 else
33334 init = pop_stmt_list (this_pre_body);
33335 this_pre_body = NULL_TREE;
33337 else
33339 /* Consume '='. */
33340 cp_lexer_consume_token (parser->lexer);
33341 init = cp_parser_assignment_expression (parser);
33343 non_class:
33344 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
33345 init = error_mark_node;
33346 else
33347 cp_finish_decl (decl, NULL_TREE,
33348 /*init_const_expr_p=*/false,
33349 asm_specification,
33350 LOOKUP_ONLYCONVERTING);
33353 if (pushed_scope)
33354 pop_scope (pushed_scope);
33357 else
33359 cp_id_kind idk;
33360 /* If parsing a type specifier sequence failed, then
33361 this MUST be a simple expression. */
33362 if (code == CILK_FOR)
33363 error ("%<_Cilk_for%> allows expression instead of declaration only "
33364 "in C, not in C++");
33365 cp_parser_parse_tentatively (parser);
33366 decl = cp_parser_primary_expression (parser, false, false,
33367 false, &idk);
33368 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
33369 if (!cp_parser_error_occurred (parser)
33370 && decl
33371 && (TREE_CODE (decl) == COMPONENT_REF
33372 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
33374 cp_parser_abort_tentative_parse (parser);
33375 cp_parser_parse_tentatively (parser);
33376 cp_token *token = cp_lexer_peek_token (parser->lexer);
33377 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
33378 /*check_dependency_p=*/true,
33379 /*template_p=*/NULL,
33380 /*declarator_p=*/false,
33381 /*optional_p=*/false);
33382 if (name != error_mark_node
33383 && last_tok == cp_lexer_peek_token (parser->lexer))
33385 decl = cp_parser_lookup_name_simple (parser, name,
33386 token->location);
33387 if (TREE_CODE (decl) == FIELD_DECL)
33388 add_private_clause = omp_privatize_field (decl, false);
33390 cp_parser_abort_tentative_parse (parser);
33391 cp_parser_parse_tentatively (parser);
33392 decl = cp_parser_primary_expression (parser, false, false,
33393 false, &idk);
33395 if (!cp_parser_error_occurred (parser)
33396 && decl
33397 && DECL_P (decl)
33398 && CLASS_TYPE_P (TREE_TYPE (decl)))
33400 tree rhs;
33402 cp_parser_parse_definitely (parser);
33403 cp_parser_require (parser, CPP_EQ, RT_EQ);
33404 rhs = cp_parser_assignment_expression (parser);
33405 orig_init = rhs;
33406 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
33407 decl, NOP_EXPR,
33408 rhs,
33409 tf_warning_or_error));
33410 if (!add_private_clause)
33411 add_private_clause = decl;
33413 else
33415 decl = NULL;
33416 cp_parser_abort_tentative_parse (parser);
33417 init = cp_parser_expression (parser);
33418 if (init)
33420 if (TREE_CODE (init) == MODIFY_EXPR
33421 || TREE_CODE (init) == MODOP_EXPR)
33422 real_decl = TREE_OPERAND (init, 0);
33426 return add_private_clause;
33429 /* Parse the restricted form of the for statement allowed by OpenMP. */
33431 static tree
33432 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
33433 tree *cclauses)
33435 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
33436 tree real_decl, initv, condv, incrv, declv;
33437 tree this_pre_body, cl, ordered_cl = NULL_TREE;
33438 location_t loc_first;
33439 bool collapse_err = false;
33440 int i, collapse = 1, ordered = 0, count, nbraces = 0;
33441 vec<tree, va_gc> *for_block = make_tree_vector ();
33442 auto_vec<tree, 4> orig_inits;
33444 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
33445 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
33446 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
33447 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
33448 && OMP_CLAUSE_ORDERED_EXPR (cl))
33450 ordered_cl = cl;
33451 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
33454 if (ordered && ordered < collapse)
33456 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
33457 "%<ordered%> clause parameter is less than %<collapse%>");
33458 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
33459 = build_int_cst (NULL_TREE, collapse);
33460 ordered = collapse;
33462 if (ordered)
33464 for (tree *pc = &clauses; *pc; )
33465 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
33467 error_at (OMP_CLAUSE_LOCATION (*pc),
33468 "%<linear%> clause may not be specified together "
33469 "with %<ordered%> clause with a parameter");
33470 *pc = OMP_CLAUSE_CHAIN (*pc);
33472 else
33473 pc = &OMP_CLAUSE_CHAIN (*pc);
33476 gcc_assert (collapse >= 1 && ordered >= 0);
33477 count = ordered ? ordered : collapse;
33479 declv = make_tree_vec (count);
33480 initv = make_tree_vec (count);
33481 condv = make_tree_vec (count);
33482 incrv = make_tree_vec (count);
33484 loc_first = cp_lexer_peek_token (parser->lexer)->location;
33486 for (i = 0; i < count; i++)
33488 int bracecount = 0;
33489 tree add_private_clause = NULL_TREE;
33490 location_t loc;
33492 if (code != CILK_FOR
33493 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
33495 cp_parser_error (parser, "for statement expected");
33496 return NULL;
33498 if (code == CILK_FOR
33499 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
33501 cp_parser_error (parser, "_Cilk_for statement expected");
33502 return NULL;
33504 loc = cp_lexer_consume_token (parser->lexer)->location;
33506 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33507 return NULL;
33509 init = orig_init = decl = real_decl = NULL;
33510 this_pre_body = push_stmt_list ();
33512 add_private_clause
33513 = cp_parser_omp_for_loop_init (parser, code,
33514 this_pre_body, for_block,
33515 init, orig_init, decl, real_decl);
33517 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
33518 if (this_pre_body)
33520 this_pre_body = pop_stmt_list (this_pre_body);
33521 if (pre_body)
33523 tree t = pre_body;
33524 pre_body = push_stmt_list ();
33525 add_stmt (t);
33526 add_stmt (this_pre_body);
33527 pre_body = pop_stmt_list (pre_body);
33529 else
33530 pre_body = this_pre_body;
33533 if (decl)
33534 real_decl = decl;
33535 if (cclauses != NULL
33536 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
33537 && real_decl != NULL_TREE)
33539 tree *c;
33540 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
33541 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
33542 && OMP_CLAUSE_DECL (*c) == real_decl)
33544 error_at (loc, "iteration variable %qD"
33545 " should not be firstprivate", real_decl);
33546 *c = OMP_CLAUSE_CHAIN (*c);
33548 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
33549 && OMP_CLAUSE_DECL (*c) == real_decl)
33551 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
33552 tree l = *c;
33553 *c = OMP_CLAUSE_CHAIN (*c);
33554 if (code == OMP_SIMD)
33556 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33557 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
33559 else
33561 OMP_CLAUSE_CHAIN (l) = clauses;
33562 clauses = l;
33564 add_private_clause = NULL_TREE;
33566 else
33568 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
33569 && OMP_CLAUSE_DECL (*c) == real_decl)
33570 add_private_clause = NULL_TREE;
33571 c = &OMP_CLAUSE_CHAIN (*c);
33575 if (add_private_clause)
33577 tree c;
33578 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
33580 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
33581 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
33582 && OMP_CLAUSE_DECL (c) == decl)
33583 break;
33584 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
33585 && OMP_CLAUSE_DECL (c) == decl)
33586 error_at (loc, "iteration variable %qD "
33587 "should not be firstprivate",
33588 decl);
33589 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
33590 && OMP_CLAUSE_DECL (c) == decl)
33591 error_at (loc, "iteration variable %qD should not be reduction",
33592 decl);
33594 if (c == NULL)
33596 if (code != OMP_SIMD)
33597 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
33598 else if (collapse == 1)
33599 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
33600 else
33601 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
33602 OMP_CLAUSE_DECL (c) = add_private_clause;
33603 c = finish_omp_clauses (c, true);
33604 if (c)
33606 OMP_CLAUSE_CHAIN (c) = clauses;
33607 clauses = c;
33608 /* For linear, signal that we need to fill up
33609 the so far unknown linear step. */
33610 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
33611 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
33616 cond = NULL;
33617 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33618 cond = cp_parser_omp_for_cond (parser, decl, code);
33619 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
33621 incr = NULL;
33622 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
33624 /* If decl is an iterator, preserve the operator on decl
33625 until finish_omp_for. */
33626 if (real_decl
33627 && ((processing_template_decl
33628 && (TREE_TYPE (real_decl) == NULL_TREE
33629 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
33630 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
33631 incr = cp_parser_omp_for_incr (parser, real_decl);
33632 else
33633 incr = cp_parser_expression (parser);
33634 if (!EXPR_HAS_LOCATION (incr))
33635 protected_set_expr_location (incr, input_location);
33638 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33639 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33640 /*or_comma=*/false,
33641 /*consume_paren=*/true);
33643 TREE_VEC_ELT (declv, i) = decl;
33644 TREE_VEC_ELT (initv, i) = init;
33645 TREE_VEC_ELT (condv, i) = cond;
33646 TREE_VEC_ELT (incrv, i) = incr;
33647 if (orig_init)
33649 orig_inits.safe_grow_cleared (i + 1);
33650 orig_inits[i] = orig_init;
33653 if (i == count - 1)
33654 break;
33656 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
33657 in between the collapsed for loops to be still considered perfectly
33658 nested. Hopefully the final version clarifies this.
33659 For now handle (multiple) {'s and empty statements. */
33660 cp_parser_parse_tentatively (parser);
33663 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
33664 break;
33665 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33667 cp_lexer_consume_token (parser->lexer);
33668 bracecount++;
33670 else if (bracecount
33671 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33672 cp_lexer_consume_token (parser->lexer);
33673 else
33675 loc = cp_lexer_peek_token (parser->lexer)->location;
33676 error_at (loc, "not enough collapsed for loops");
33677 collapse_err = true;
33678 cp_parser_abort_tentative_parse (parser);
33679 declv = NULL_TREE;
33680 break;
33683 while (1);
33685 if (declv)
33687 cp_parser_parse_definitely (parser);
33688 nbraces += bracecount;
33692 /* Note that we saved the original contents of this flag when we entered
33693 the structured block, and so we don't need to re-save it here. */
33694 if (code == CILK_SIMD || code == CILK_FOR)
33695 parser->in_statement = IN_CILK_SIMD_FOR;
33696 else
33697 parser->in_statement = IN_OMP_FOR;
33699 /* Note that the grammar doesn't call for a structured block here,
33700 though the loop as a whole is a structured block. */
33701 body = push_stmt_list ();
33702 cp_parser_statement (parser, NULL_TREE, false, NULL);
33703 body = pop_stmt_list (body);
33705 if (declv == NULL_TREE)
33706 ret = NULL_TREE;
33707 else
33708 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
33709 body, pre_body, &orig_inits, clauses);
33711 while (nbraces)
33713 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33715 cp_lexer_consume_token (parser->lexer);
33716 nbraces--;
33718 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33719 cp_lexer_consume_token (parser->lexer);
33720 else
33722 if (!collapse_err)
33724 error_at (cp_lexer_peek_token (parser->lexer)->location,
33725 "collapsed loops not perfectly nested");
33727 collapse_err = true;
33728 cp_parser_statement_seq_opt (parser, NULL);
33729 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
33730 break;
33734 while (!for_block->is_empty ())
33735 add_stmt (pop_stmt_list (for_block->pop ()));
33736 release_tree_vector (for_block);
33738 return ret;
33741 /* Helper function for OpenMP parsing, split clauses and call
33742 finish_omp_clauses on each of the set of clauses afterwards. */
33744 static void
33745 cp_omp_split_clauses (location_t loc, enum tree_code code,
33746 omp_clause_mask mask, tree clauses, tree *cclauses)
33748 int i;
33749 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
33750 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
33751 if (cclauses[i])
33752 cclauses[i] = finish_omp_clauses (cclauses[i], true);
33755 /* OpenMP 4.0:
33756 #pragma omp simd simd-clause[optseq] new-line
33757 for-loop */
33759 #define OMP_SIMD_CLAUSE_MASK \
33760 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
33761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
33762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
33763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
33764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33767 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33769 static tree
33770 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
33771 char *p_name, omp_clause_mask mask, tree *cclauses)
33773 tree clauses, sb, ret;
33774 unsigned int save;
33775 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33777 strcat (p_name, " simd");
33778 mask |= OMP_SIMD_CLAUSE_MASK;
33780 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33781 cclauses == NULL);
33782 if (cclauses)
33784 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
33785 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
33786 tree c = find_omp_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
33787 OMP_CLAUSE_ORDERED);
33788 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
33790 error_at (OMP_CLAUSE_LOCATION (c),
33791 "%<ordered%> clause with parameter may not be specified "
33792 "on %qs construct", p_name);
33793 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
33797 sb = begin_omp_structured_block ();
33798 save = cp_parser_begin_omp_structured_block (parser);
33800 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
33802 cp_parser_end_omp_structured_block (parser, save);
33803 add_stmt (finish_omp_structured_block (sb));
33805 return ret;
33808 /* OpenMP 2.5:
33809 #pragma omp for for-clause[optseq] new-line
33810 for-loop
33812 OpenMP 4.0:
33813 #pragma omp for simd for-simd-clause[optseq] new-line
33814 for-loop */
33816 #define OMP_FOR_CLAUSE_MASK \
33817 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
33821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
33823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
33824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
33825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33827 static tree
33828 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
33829 char *p_name, omp_clause_mask mask, tree *cclauses)
33831 tree clauses, sb, ret;
33832 unsigned int save;
33833 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33835 strcat (p_name, " for");
33836 mask |= OMP_FOR_CLAUSE_MASK;
33837 if (cclauses)
33838 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
33839 /* Composite distribute parallel for{, simd} disallows ordered clause. */
33840 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
33841 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
33843 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33845 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33846 const char *p = IDENTIFIER_POINTER (id);
33848 if (strcmp (p, "simd") == 0)
33850 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
33851 if (cclauses == NULL)
33852 cclauses = cclauses_buf;
33854 cp_lexer_consume_token (parser->lexer);
33855 if (!flag_openmp) /* flag_openmp_simd */
33856 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33857 cclauses);
33858 sb = begin_omp_structured_block ();
33859 save = cp_parser_begin_omp_structured_block (parser);
33860 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33861 cclauses);
33862 cp_parser_end_omp_structured_block (parser, save);
33863 tree body = finish_omp_structured_block (sb);
33864 if (ret == NULL)
33865 return ret;
33866 ret = make_node (OMP_FOR);
33867 TREE_TYPE (ret) = void_type_node;
33868 OMP_FOR_BODY (ret) = body;
33869 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33870 SET_EXPR_LOCATION (ret, loc);
33871 add_stmt (ret);
33872 return ret;
33875 if (!flag_openmp) /* flag_openmp_simd */
33877 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33878 return NULL_TREE;
33881 /* Composite distribute parallel for disallows linear clause. */
33882 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
33883 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
33885 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33886 cclauses == NULL);
33887 if (cclauses)
33889 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
33890 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33893 sb = begin_omp_structured_block ();
33894 save = cp_parser_begin_omp_structured_block (parser);
33896 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
33898 cp_parser_end_omp_structured_block (parser, save);
33899 add_stmt (finish_omp_structured_block (sb));
33901 return ret;
33904 /* OpenMP 2.5:
33905 # pragma omp master new-line
33906 structured-block */
33908 static tree
33909 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
33911 cp_parser_require_pragma_eol (parser, pragma_tok);
33912 return c_finish_omp_master (input_location,
33913 cp_parser_omp_structured_block (parser));
33916 /* OpenMP 2.5:
33917 # pragma omp ordered new-line
33918 structured-block
33920 OpenMP 4.5:
33921 # pragma omp ordered ordered-clauses new-line
33922 structured-block */
33924 #define OMP_ORDERED_CLAUSE_MASK \
33925 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
33926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
33928 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
33929 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
33931 static bool
33932 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
33933 enum pragma_context context)
33935 location_t loc = pragma_tok->location;
33937 if (context != pragma_stmt && context != pragma_compound)
33939 cp_parser_error (parser, "expected declaration specifiers");
33940 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33941 return false;
33944 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33946 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33947 const char *p = IDENTIFIER_POINTER (id);
33949 if (strcmp (p, "depend") == 0)
33951 if (context == pragma_stmt)
33953 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
33954 "%<depend%> clause may only be used in compound "
33955 "statements");
33956 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33957 return false;
33959 tree clauses
33960 = cp_parser_omp_all_clauses (parser,
33961 OMP_ORDERED_DEPEND_CLAUSE_MASK,
33962 "#pragma omp ordered", pragma_tok);
33963 c_finish_omp_ordered (loc, clauses, NULL_TREE);
33964 return false;
33968 tree clauses
33969 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
33970 "#pragma omp ordered", pragma_tok);
33971 c_finish_omp_ordered (loc, clauses,
33972 cp_parser_omp_structured_block (parser));
33973 return true;
33976 /* OpenMP 2.5:
33978 section-scope:
33979 { section-sequence }
33981 section-sequence:
33982 section-directive[opt] structured-block
33983 section-sequence section-directive structured-block */
33985 static tree
33986 cp_parser_omp_sections_scope (cp_parser *parser)
33988 tree stmt, substmt;
33989 bool error_suppress = false;
33990 cp_token *tok;
33992 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
33993 return NULL_TREE;
33995 stmt = push_stmt_list ();
33997 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
33998 != PRAGMA_OMP_SECTION)
34000 substmt = cp_parser_omp_structured_block (parser);
34001 substmt = build1 (OMP_SECTION, void_type_node, substmt);
34002 add_stmt (substmt);
34005 while (1)
34007 tok = cp_lexer_peek_token (parser->lexer);
34008 if (tok->type == CPP_CLOSE_BRACE)
34009 break;
34010 if (tok->type == CPP_EOF)
34011 break;
34013 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
34015 cp_lexer_consume_token (parser->lexer);
34016 cp_parser_require_pragma_eol (parser, tok);
34017 error_suppress = false;
34019 else if (!error_suppress)
34021 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
34022 error_suppress = true;
34025 substmt = cp_parser_omp_structured_block (parser);
34026 substmt = build1 (OMP_SECTION, void_type_node, substmt);
34027 add_stmt (substmt);
34029 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34031 substmt = pop_stmt_list (stmt);
34033 stmt = make_node (OMP_SECTIONS);
34034 TREE_TYPE (stmt) = void_type_node;
34035 OMP_SECTIONS_BODY (stmt) = substmt;
34037 add_stmt (stmt);
34038 return stmt;
34041 /* OpenMP 2.5:
34042 # pragma omp sections sections-clause[optseq] newline
34043 sections-scope */
34045 #define OMP_SECTIONS_CLAUSE_MASK \
34046 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34052 static tree
34053 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
34054 char *p_name, omp_clause_mask mask, tree *cclauses)
34056 tree clauses, ret;
34057 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34059 strcat (p_name, " sections");
34060 mask |= OMP_SECTIONS_CLAUSE_MASK;
34061 if (cclauses)
34062 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
34064 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34065 cclauses == NULL);
34066 if (cclauses)
34068 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
34069 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
34072 ret = cp_parser_omp_sections_scope (parser);
34073 if (ret)
34074 OMP_SECTIONS_CLAUSES (ret) = clauses;
34076 return ret;
34079 /* OpenMP 2.5:
34080 # pragma omp parallel parallel-clause[optseq] new-line
34081 structured-block
34082 # pragma omp parallel for parallel-for-clause[optseq] new-line
34083 structured-block
34084 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
34085 structured-block
34087 OpenMP 4.0:
34088 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
34089 structured-block */
34091 #define OMP_PARALLEL_CLAUSE_MASK \
34092 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34094 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
34096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
34098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
34100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
34102 static tree
34103 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
34104 char *p_name, omp_clause_mask mask, tree *cclauses)
34106 tree stmt, clauses, block;
34107 unsigned int save;
34108 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34110 strcat (p_name, " parallel");
34111 mask |= OMP_PARALLEL_CLAUSE_MASK;
34112 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
34113 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
34114 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
34115 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
34117 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34119 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34120 if (cclauses == NULL)
34121 cclauses = cclauses_buf;
34123 cp_lexer_consume_token (parser->lexer);
34124 if (!flag_openmp) /* flag_openmp_simd */
34125 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
34126 block = begin_omp_parallel ();
34127 save = cp_parser_begin_omp_structured_block (parser);
34128 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
34129 cp_parser_end_omp_structured_block (parser, save);
34130 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
34131 block);
34132 if (ret == NULL_TREE)
34133 return ret;
34134 OMP_PARALLEL_COMBINED (stmt) = 1;
34135 return stmt;
34137 /* When combined with distribute, parallel has to be followed by for.
34138 #pragma omp target parallel is allowed though. */
34139 else if (cclauses
34140 && (mask & (OMP_CLAUSE_MASK_1
34141 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34143 error_at (loc, "expected %<for%> after %qs", p_name);
34144 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34145 return NULL_TREE;
34147 else if (!flag_openmp) /* flag_openmp_simd */
34149 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34150 return NULL_TREE;
34152 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34154 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34155 const char *p = IDENTIFIER_POINTER (id);
34156 if (strcmp (p, "sections") == 0)
34158 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34159 cclauses = cclauses_buf;
34161 cp_lexer_consume_token (parser->lexer);
34162 block = begin_omp_parallel ();
34163 save = cp_parser_begin_omp_structured_block (parser);
34164 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
34165 cp_parser_end_omp_structured_block (parser, save);
34166 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
34167 block);
34168 OMP_PARALLEL_COMBINED (stmt) = 1;
34169 return stmt;
34173 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
34174 if (cclauses)
34176 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
34177 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
34180 block = begin_omp_parallel ();
34181 save = cp_parser_begin_omp_structured_block (parser);
34182 cp_parser_statement (parser, NULL_TREE, false, NULL);
34183 cp_parser_end_omp_structured_block (parser, save);
34184 stmt = finish_omp_parallel (clauses, block);
34185 return stmt;
34188 /* OpenMP 2.5:
34189 # pragma omp single single-clause[optseq] new-line
34190 structured-block */
34192 #define OMP_SINGLE_CLAUSE_MASK \
34193 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34194 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34195 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
34196 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34198 static tree
34199 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
34201 tree stmt = make_node (OMP_SINGLE);
34202 TREE_TYPE (stmt) = void_type_node;
34204 OMP_SINGLE_CLAUSES (stmt)
34205 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
34206 "#pragma omp single", pragma_tok);
34207 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
34209 return add_stmt (stmt);
34212 /* OpenMP 3.0:
34213 # pragma omp task task-clause[optseq] new-line
34214 structured-block */
34216 #define OMP_TASK_CLAUSE_MASK \
34217 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
34219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
34220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
34224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
34225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34226 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
34228 static tree
34229 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
34231 tree clauses, block;
34232 unsigned int save;
34234 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
34235 "#pragma omp task", pragma_tok);
34236 block = begin_omp_task ();
34237 save = cp_parser_begin_omp_structured_block (parser);
34238 cp_parser_statement (parser, NULL_TREE, false, NULL);
34239 cp_parser_end_omp_structured_block (parser, save);
34240 return finish_omp_task (clauses, block);
34243 /* OpenMP 3.0:
34244 # pragma omp taskwait new-line */
34246 static void
34247 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
34249 cp_parser_require_pragma_eol (parser, pragma_tok);
34250 finish_omp_taskwait ();
34253 /* OpenMP 3.1:
34254 # pragma omp taskyield new-line */
34256 static void
34257 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
34259 cp_parser_require_pragma_eol (parser, pragma_tok);
34260 finish_omp_taskyield ();
34263 /* OpenMP 4.0:
34264 # pragma omp taskgroup new-line
34265 structured-block */
34267 static tree
34268 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
34270 cp_parser_require_pragma_eol (parser, pragma_tok);
34271 return c_finish_omp_taskgroup (input_location,
34272 cp_parser_omp_structured_block (parser));
34276 /* OpenMP 2.5:
34277 # pragma omp threadprivate (variable-list) */
34279 static void
34280 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
34282 tree vars;
34284 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34285 cp_parser_require_pragma_eol (parser, pragma_tok);
34287 finish_omp_threadprivate (vars);
34290 /* OpenMP 4.0:
34291 # pragma omp cancel cancel-clause[optseq] new-line */
34293 #define OMP_CANCEL_CLAUSE_MASK \
34294 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
34295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
34296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
34297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
34298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
34300 static void
34301 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
34303 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
34304 "#pragma omp cancel", pragma_tok);
34305 finish_omp_cancel (clauses);
34308 /* OpenMP 4.0:
34309 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
34311 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
34312 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
34313 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
34314 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
34315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
34317 static void
34318 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
34320 tree clauses;
34321 bool point_seen = false;
34323 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34325 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34326 const char *p = IDENTIFIER_POINTER (id);
34328 if (strcmp (p, "point") == 0)
34330 cp_lexer_consume_token (parser->lexer);
34331 point_seen = true;
34334 if (!point_seen)
34336 cp_parser_error (parser, "expected %<point%>");
34337 cp_parser_require_pragma_eol (parser, pragma_tok);
34338 return;
34341 clauses = cp_parser_omp_all_clauses (parser,
34342 OMP_CANCELLATION_POINT_CLAUSE_MASK,
34343 "#pragma omp cancellation point",
34344 pragma_tok);
34345 finish_omp_cancellation_point (clauses);
34348 /* OpenMP 4.0:
34349 #pragma omp distribute distribute-clause[optseq] new-line
34350 for-loop */
34352 #define OMP_DISTRIBUTE_CLAUSE_MASK \
34353 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34354 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34355 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
34357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34359 static tree
34360 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
34361 char *p_name, omp_clause_mask mask, tree *cclauses)
34363 tree clauses, sb, ret;
34364 unsigned int save;
34365 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34367 strcat (p_name, " distribute");
34368 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
34370 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34372 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34373 const char *p = IDENTIFIER_POINTER (id);
34374 bool simd = false;
34375 bool parallel = false;
34377 if (strcmp (p, "simd") == 0)
34378 simd = true;
34379 else
34380 parallel = strcmp (p, "parallel") == 0;
34381 if (parallel || simd)
34383 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34384 if (cclauses == NULL)
34385 cclauses = cclauses_buf;
34386 cp_lexer_consume_token (parser->lexer);
34387 if (!flag_openmp) /* flag_openmp_simd */
34389 if (simd)
34390 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34391 cclauses);
34392 else
34393 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
34394 cclauses);
34396 sb = begin_omp_structured_block ();
34397 save = cp_parser_begin_omp_structured_block (parser);
34398 if (simd)
34399 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34400 cclauses);
34401 else
34402 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
34403 cclauses);
34404 cp_parser_end_omp_structured_block (parser, save);
34405 tree body = finish_omp_structured_block (sb);
34406 if (ret == NULL)
34407 return ret;
34408 ret = make_node (OMP_DISTRIBUTE);
34409 TREE_TYPE (ret) = void_type_node;
34410 OMP_FOR_BODY (ret) = body;
34411 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
34412 SET_EXPR_LOCATION (ret, loc);
34413 add_stmt (ret);
34414 return ret;
34417 if (!flag_openmp) /* flag_openmp_simd */
34419 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34420 return NULL_TREE;
34423 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34424 cclauses == NULL);
34425 if (cclauses)
34427 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
34428 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
34431 sb = begin_omp_structured_block ();
34432 save = cp_parser_begin_omp_structured_block (parser);
34434 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
34436 cp_parser_end_omp_structured_block (parser, save);
34437 add_stmt (finish_omp_structured_block (sb));
34439 return ret;
34442 /* OpenMP 4.0:
34443 # pragma omp teams teams-clause[optseq] new-line
34444 structured-block */
34446 #define OMP_TEAMS_CLAUSE_MASK \
34447 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34448 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34449 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34450 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34451 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
34452 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
34453 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
34455 static tree
34456 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
34457 char *p_name, omp_clause_mask mask, tree *cclauses)
34459 tree clauses, sb, ret;
34460 unsigned int save;
34461 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34463 strcat (p_name, " teams");
34464 mask |= OMP_TEAMS_CLAUSE_MASK;
34466 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34468 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34469 const char *p = IDENTIFIER_POINTER (id);
34470 if (strcmp (p, "distribute") == 0)
34472 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34473 if (cclauses == NULL)
34474 cclauses = cclauses_buf;
34476 cp_lexer_consume_token (parser->lexer);
34477 if (!flag_openmp) /* flag_openmp_simd */
34478 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
34479 cclauses);
34480 sb = begin_omp_structured_block ();
34481 save = cp_parser_begin_omp_structured_block (parser);
34482 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
34483 cclauses);
34484 cp_parser_end_omp_structured_block (parser, save);
34485 tree body = finish_omp_structured_block (sb);
34486 if (ret == NULL)
34487 return ret;
34488 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34489 ret = make_node (OMP_TEAMS);
34490 TREE_TYPE (ret) = void_type_node;
34491 OMP_TEAMS_CLAUSES (ret) = clauses;
34492 OMP_TEAMS_BODY (ret) = body;
34493 OMP_TEAMS_COMBINED (ret) = 1;
34494 return add_stmt (ret);
34497 if (!flag_openmp) /* flag_openmp_simd */
34499 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34500 return NULL_TREE;
34503 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34504 cclauses == NULL);
34505 if (cclauses)
34507 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
34508 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34511 tree stmt = make_node (OMP_TEAMS);
34512 TREE_TYPE (stmt) = void_type_node;
34513 OMP_TEAMS_CLAUSES (stmt) = clauses;
34514 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
34516 return add_stmt (stmt);
34519 /* OpenMP 4.0:
34520 # pragma omp target data target-data-clause[optseq] new-line
34521 structured-block */
34523 #define OMP_TARGET_DATA_CLAUSE_MASK \
34524 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34525 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34526 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34527 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
34529 static tree
34530 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
34532 tree clauses
34533 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
34534 "#pragma omp target data", pragma_tok);
34535 int map_seen = 0;
34536 for (tree *pc = &clauses; *pc;)
34538 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34539 switch (OMP_CLAUSE_MAP_KIND (*pc))
34541 case GOMP_MAP_TO:
34542 case GOMP_MAP_ALWAYS_TO:
34543 case GOMP_MAP_FROM:
34544 case GOMP_MAP_ALWAYS_FROM:
34545 case GOMP_MAP_TOFROM:
34546 case GOMP_MAP_ALWAYS_TOFROM:
34547 case GOMP_MAP_ALLOC:
34548 map_seen = 3;
34549 break;
34550 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34551 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34552 case GOMP_MAP_ALWAYS_POINTER:
34553 break;
34554 default:
34555 map_seen |= 1;
34556 error_at (OMP_CLAUSE_LOCATION (*pc),
34557 "%<#pragma omp target data%> with map-type other "
34558 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
34559 "on %<map%> clause");
34560 *pc = OMP_CLAUSE_CHAIN (*pc);
34561 continue;
34563 pc = &OMP_CLAUSE_CHAIN (*pc);
34566 if (map_seen != 3)
34568 if (map_seen == 0)
34569 error_at (pragma_tok->location,
34570 "%<#pragma omp target data%> must contain at least "
34571 "one %<map%> clause");
34572 return NULL_TREE;
34575 tree stmt = make_node (OMP_TARGET_DATA);
34576 TREE_TYPE (stmt) = void_type_node;
34577 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
34579 keep_next_level (true);
34580 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
34582 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34583 return add_stmt (stmt);
34586 /* OpenMP 4.5:
34587 # pragma omp target enter data target-enter-data-clause[optseq] new-line
34588 structured-block */
34590 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
34591 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34594 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34595 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34597 static tree
34598 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
34599 enum pragma_context context)
34601 bool data_seen = false;
34602 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34604 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34605 const char *p = IDENTIFIER_POINTER (id);
34607 if (strcmp (p, "data") == 0)
34609 cp_lexer_consume_token (parser->lexer);
34610 data_seen = true;
34613 if (!data_seen)
34615 cp_parser_error (parser, "expected %<data%>");
34616 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34617 return NULL_TREE;
34620 if (context == pragma_stmt)
34622 error_at (pragma_tok->location,
34623 "%<#pragma omp target enter data%> may only be "
34624 "used in compound statements");
34625 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34626 return NULL_TREE;
34629 tree clauses
34630 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
34631 "#pragma omp target enter data", pragma_tok);
34632 int map_seen = 0;
34633 for (tree *pc = &clauses; *pc;)
34635 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34636 switch (OMP_CLAUSE_MAP_KIND (*pc))
34638 case GOMP_MAP_TO:
34639 case GOMP_MAP_ALWAYS_TO:
34640 case GOMP_MAP_ALLOC:
34641 map_seen = 3;
34642 break;
34643 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34644 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34645 case GOMP_MAP_ALWAYS_POINTER:
34646 break;
34647 default:
34648 map_seen |= 1;
34649 error_at (OMP_CLAUSE_LOCATION (*pc),
34650 "%<#pragma omp target enter data%> with map-type other "
34651 "than %<to%> or %<alloc%> on %<map%> clause");
34652 *pc = OMP_CLAUSE_CHAIN (*pc);
34653 continue;
34655 pc = &OMP_CLAUSE_CHAIN (*pc);
34658 if (map_seen != 3)
34660 if (map_seen == 0)
34661 error_at (pragma_tok->location,
34662 "%<#pragma omp target enter data%> must contain at least "
34663 "one %<map%> clause");
34664 return NULL_TREE;
34667 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
34668 TREE_TYPE (stmt) = void_type_node;
34669 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
34670 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34671 return add_stmt (stmt);
34674 /* OpenMP 4.5:
34675 # pragma omp target exit data target-enter-data-clause[optseq] new-line
34676 structured-block */
34678 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
34679 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34680 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34681 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34682 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34685 static tree
34686 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
34687 enum pragma_context context)
34689 bool data_seen = false;
34690 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34692 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34693 const char *p = IDENTIFIER_POINTER (id);
34695 if (strcmp (p, "data") == 0)
34697 cp_lexer_consume_token (parser->lexer);
34698 data_seen = true;
34701 if (!data_seen)
34703 cp_parser_error (parser, "expected %<data%>");
34704 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34705 return NULL_TREE;
34708 if (context == pragma_stmt)
34710 error_at (pragma_tok->location,
34711 "%<#pragma omp target exit data%> may only be "
34712 "used in compound statements");
34713 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34714 return NULL_TREE;
34717 tree clauses
34718 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
34719 "#pragma omp target exit data", pragma_tok);
34720 int map_seen = 0;
34721 for (tree *pc = &clauses; *pc;)
34723 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34724 switch (OMP_CLAUSE_MAP_KIND (*pc))
34726 case GOMP_MAP_FROM:
34727 case GOMP_MAP_ALWAYS_FROM:
34728 case GOMP_MAP_RELEASE:
34729 case GOMP_MAP_DELETE:
34730 map_seen = 3;
34731 break;
34732 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34733 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34734 case GOMP_MAP_ALWAYS_POINTER:
34735 break;
34736 default:
34737 map_seen |= 1;
34738 error_at (OMP_CLAUSE_LOCATION (*pc),
34739 "%<#pragma omp target exit data%> with map-type other "
34740 "than %<from%>, %<release%> or %<delete%> on %<map%>"
34741 " clause");
34742 *pc = OMP_CLAUSE_CHAIN (*pc);
34743 continue;
34745 pc = &OMP_CLAUSE_CHAIN (*pc);
34748 if (map_seen != 3)
34750 if (map_seen == 0)
34751 error_at (pragma_tok->location,
34752 "%<#pragma omp target exit data%> must contain at least "
34753 "one %<map%> clause");
34754 return NULL_TREE;
34757 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
34758 TREE_TYPE (stmt) = void_type_node;
34759 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
34760 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34761 return add_stmt (stmt);
34764 /* OpenMP 4.0:
34765 # pragma omp target update target-update-clause[optseq] new-line */
34767 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
34768 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
34769 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
34770 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34771 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34772 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34773 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34775 static bool
34776 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
34777 enum pragma_context context)
34779 if (context == pragma_stmt)
34781 error_at (pragma_tok->location,
34782 "%<#pragma omp target update%> may only be "
34783 "used in compound statements");
34784 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34785 return false;
34788 tree clauses
34789 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
34790 "#pragma omp target update", pragma_tok);
34791 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
34792 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
34794 error_at (pragma_tok->location,
34795 "%<#pragma omp target update%> must contain at least one "
34796 "%<from%> or %<to%> clauses");
34797 return false;
34800 tree stmt = make_node (OMP_TARGET_UPDATE);
34801 TREE_TYPE (stmt) = void_type_node;
34802 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
34803 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34804 add_stmt (stmt);
34805 return false;
34808 /* OpenMP 4.0:
34809 # pragma omp target target-clause[optseq] new-line
34810 structured-block */
34812 #define OMP_TARGET_CLAUSE_MASK \
34813 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
34818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
34821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
34823 static bool
34824 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
34825 enum pragma_context context)
34827 tree *pc = NULL, stmt;
34829 if (context != pragma_stmt && context != pragma_compound)
34831 cp_parser_error (parser, "expected declaration specifiers");
34832 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34833 return false;
34836 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34838 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34839 const char *p = IDENTIFIER_POINTER (id);
34840 enum tree_code ccode = ERROR_MARK;
34842 if (strcmp (p, "teams") == 0)
34843 ccode = OMP_TEAMS;
34844 else if (strcmp (p, "parallel") == 0)
34845 ccode = OMP_PARALLEL;
34846 else if (strcmp (p, "simd") == 0)
34847 ccode = OMP_SIMD;
34848 if (ccode != ERROR_MARK)
34850 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
34851 char p_name[sizeof ("#pragma omp target teams distribute "
34852 "parallel for simd")];
34854 cp_lexer_consume_token (parser->lexer);
34855 strcpy (p_name, "#pragma omp target");
34856 if (!flag_openmp) /* flag_openmp_simd */
34858 tree stmt;
34859 switch (ccode)
34861 case OMP_TEAMS:
34862 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
34863 OMP_TARGET_CLAUSE_MASK,
34864 cclauses);
34865 break;
34866 case OMP_PARALLEL:
34867 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
34868 OMP_TARGET_CLAUSE_MASK,
34869 cclauses);
34870 break;
34871 case OMP_SIMD:
34872 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
34873 OMP_TARGET_CLAUSE_MASK,
34874 cclauses);
34875 break;
34876 default:
34877 gcc_unreachable ();
34879 return stmt != NULL_TREE;
34881 keep_next_level (true);
34882 tree sb = begin_omp_structured_block (), ret;
34883 unsigned save = cp_parser_begin_omp_structured_block (parser);
34884 switch (ccode)
34886 case OMP_TEAMS:
34887 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
34888 OMP_TARGET_CLAUSE_MASK, cclauses);
34889 break;
34890 case OMP_PARALLEL:
34891 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
34892 OMP_TARGET_CLAUSE_MASK, cclauses);
34893 break;
34894 case OMP_SIMD:
34895 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
34896 OMP_TARGET_CLAUSE_MASK, cclauses);
34897 break;
34898 default:
34899 gcc_unreachable ();
34901 cp_parser_end_omp_structured_block (parser, save);
34902 tree body = finish_omp_structured_block (sb);
34903 if (ret == NULL_TREE)
34904 return false;
34905 if (ccode == OMP_TEAMS && !processing_template_decl)
34907 /* For combined target teams, ensure the num_teams and
34908 thread_limit clause expressions are evaluated on the host,
34909 before entering the target construct. */
34910 tree c;
34911 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34912 c; c = OMP_CLAUSE_CHAIN (c))
34913 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
34914 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
34915 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
34917 tree expr = OMP_CLAUSE_OPERAND (c, 0);
34918 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
34919 if (expr == error_mark_node)
34920 continue;
34921 tree tmp = TARGET_EXPR_SLOT (expr);
34922 add_stmt (expr);
34923 OMP_CLAUSE_OPERAND (c, 0) = expr;
34924 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
34925 OMP_CLAUSE_FIRSTPRIVATE);
34926 OMP_CLAUSE_DECL (tc) = tmp;
34927 OMP_CLAUSE_CHAIN (tc)
34928 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
34929 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
34932 tree stmt = make_node (OMP_TARGET);
34933 TREE_TYPE (stmt) = void_type_node;
34934 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
34935 OMP_TARGET_BODY (stmt) = body;
34936 OMP_TARGET_COMBINED (stmt) = 1;
34937 add_stmt (stmt);
34938 pc = &OMP_TARGET_CLAUSES (stmt);
34939 goto check_clauses;
34941 else if (!flag_openmp) /* flag_openmp_simd */
34943 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34944 return false;
34946 else if (strcmp (p, "data") == 0)
34948 cp_lexer_consume_token (parser->lexer);
34949 cp_parser_omp_target_data (parser, pragma_tok);
34950 return true;
34952 else if (strcmp (p, "enter") == 0)
34954 cp_lexer_consume_token (parser->lexer);
34955 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
34956 return false;
34958 else if (strcmp (p, "exit") == 0)
34960 cp_lexer_consume_token (parser->lexer);
34961 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
34962 return false;
34964 else if (strcmp (p, "update") == 0)
34966 cp_lexer_consume_token (parser->lexer);
34967 return cp_parser_omp_target_update (parser, pragma_tok, context);
34971 stmt = make_node (OMP_TARGET);
34972 TREE_TYPE (stmt) = void_type_node;
34974 OMP_TARGET_CLAUSES (stmt)
34975 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
34976 "#pragma omp target", pragma_tok);
34977 pc = &OMP_TARGET_CLAUSES (stmt);
34978 keep_next_level (true);
34979 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
34981 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34982 add_stmt (stmt);
34984 check_clauses:
34985 while (*pc)
34987 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34988 switch (OMP_CLAUSE_MAP_KIND (*pc))
34990 case GOMP_MAP_TO:
34991 case GOMP_MAP_ALWAYS_TO:
34992 case GOMP_MAP_FROM:
34993 case GOMP_MAP_ALWAYS_FROM:
34994 case GOMP_MAP_TOFROM:
34995 case GOMP_MAP_ALWAYS_TOFROM:
34996 case GOMP_MAP_ALLOC:
34997 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34998 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34999 case GOMP_MAP_ALWAYS_POINTER:
35000 break;
35001 default:
35002 error_at (OMP_CLAUSE_LOCATION (*pc),
35003 "%<#pragma omp target%> with map-type other "
35004 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35005 "on %<map%> clause");
35006 *pc = OMP_CLAUSE_CHAIN (*pc);
35007 continue;
35009 pc = &OMP_CLAUSE_CHAIN (*pc);
35011 return true;
35014 /* OpenACC 2.0:
35015 # pragma acc cache (variable-list) new-line
35018 static tree
35019 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
35021 tree stmt, clauses;
35023 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
35024 clauses = finish_omp_clauses (clauses, false);
35026 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
35028 stmt = make_node (OACC_CACHE);
35029 TREE_TYPE (stmt) = void_type_node;
35030 OACC_CACHE_CLAUSES (stmt) = clauses;
35031 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35032 add_stmt (stmt);
35034 return stmt;
35037 /* OpenACC 2.0:
35038 # pragma acc data oacc-data-clause[optseq] new-line
35039 structured-block */
35041 #define OACC_DATA_CLAUSE_MASK \
35042 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
35054 static tree
35055 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok)
35057 tree stmt, clauses, block;
35058 unsigned int save;
35060 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
35061 "#pragma acc data", pragma_tok);
35063 block = begin_omp_parallel ();
35064 save = cp_parser_begin_omp_structured_block (parser);
35065 cp_parser_statement (parser, NULL_TREE, false, NULL);
35066 cp_parser_end_omp_structured_block (parser, save);
35067 stmt = finish_oacc_data (clauses, block);
35068 return stmt;
35071 /* OpenACC 2.0:
35072 # pragma acc host_data <clauses> new-line
35073 structured-block */
35075 #define OACC_HOST_DATA_CLAUSE_MASK \
35076 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
35078 static tree
35079 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok)
35081 tree stmt, clauses, block;
35082 unsigned int save;
35084 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
35085 "#pragma acc host_data", pragma_tok);
35087 block = begin_omp_parallel ();
35088 save = cp_parser_begin_omp_structured_block (parser);
35089 cp_parser_statement (parser, NULL_TREE, false, NULL);
35090 cp_parser_end_omp_structured_block (parser, save);
35091 stmt = finish_oacc_host_data (clauses, block);
35092 return stmt;
35095 /* OpenACC 2.0:
35096 # pragma acc declare oacc-data-clause[optseq] new-line
35099 #define OACC_DECLARE_CLAUSE_MASK \
35100 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35101 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35102 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35103 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35104 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35105 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
35106 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
35107 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35108 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35109 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35110 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35111 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
35113 static tree
35114 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
35116 tree clauses, stmt;
35117 bool error = false;
35119 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
35120 "#pragma acc declare", pragma_tok, true);
35123 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35125 error_at (pragma_tok->location,
35126 "no valid clauses specified in %<#pragma acc declare%>");
35127 return NULL_TREE;
35130 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
35132 location_t loc = OMP_CLAUSE_LOCATION (t);
35133 tree decl = OMP_CLAUSE_DECL (t);
35134 if (!DECL_P (decl))
35136 error_at (loc, "array section in %<#pragma acc declare%>");
35137 error = true;
35138 continue;
35140 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
35141 switch (OMP_CLAUSE_MAP_KIND (t))
35143 case GOMP_MAP_FORCE_ALLOC:
35144 case GOMP_MAP_FORCE_TO:
35145 case GOMP_MAP_FORCE_DEVICEPTR:
35146 case GOMP_MAP_DEVICE_RESIDENT:
35147 break;
35149 case GOMP_MAP_POINTER:
35150 /* Generated by c_finish_omp_clauses from array sections;
35151 avoid spurious diagnostics. */
35152 break;
35154 case GOMP_MAP_LINK:
35155 if (!global_bindings_p ()
35156 && (TREE_STATIC (decl)
35157 || !DECL_EXTERNAL (decl)))
35159 error_at (loc,
35160 "%qD must be a global variable in"
35161 "%<#pragma acc declare link%>",
35162 decl);
35163 error = true;
35164 continue;
35166 break;
35168 default:
35169 if (global_bindings_p ())
35171 error_at (loc, "invalid OpenACC clause at file scope");
35172 error = true;
35173 continue;
35175 if (DECL_EXTERNAL (decl))
35177 error_at (loc,
35178 "invalid use of %<extern%> variable %qD "
35179 "in %<#pragma acc declare%>", decl);
35180 error = true;
35181 continue;
35183 else if (TREE_PUBLIC (decl))
35185 error_at (loc,
35186 "invalid use of %<global%> variable %qD "
35187 "in %<#pragma acc declare%>", decl);
35188 error = true;
35189 continue;
35191 break;
35194 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
35195 || lookup_attribute ("omp declare target link",
35196 DECL_ATTRIBUTES (decl)))
35198 error_at (loc, "variable %qD used more than once with "
35199 "%<#pragma acc declare%>", decl);
35200 error = true;
35201 continue;
35204 if (!error)
35206 tree id;
35208 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
35209 id = get_identifier ("omp declare target link");
35210 else
35211 id = get_identifier ("omp declare target");
35213 DECL_ATTRIBUTES (decl)
35214 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
35215 if (global_bindings_p ())
35217 symtab_node *node = symtab_node::get (decl);
35218 if (node != NULL)
35220 node->offloadable = 1;
35221 if (ENABLE_OFFLOADING)
35223 g->have_offload = true;
35224 if (is_a <varpool_node *> (node))
35225 vec_safe_push (offload_vars, decl);
35232 if (error || global_bindings_p ())
35233 return NULL_TREE;
35235 stmt = make_node (OACC_DECLARE);
35236 TREE_TYPE (stmt) = void_type_node;
35237 OACC_DECLARE_CLAUSES (stmt) = clauses;
35238 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35240 add_stmt (stmt);
35242 return NULL_TREE;
35245 /* OpenACC 2.0:
35246 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
35250 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
35252 LOC is the location of the #pragma token.
35255 #define OACC_ENTER_DATA_CLAUSE_MASK \
35256 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35257 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35258 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35259 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35260 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35261 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35262 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35264 #define OACC_EXIT_DATA_CLAUSE_MASK \
35265 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35266 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
35269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35271 static tree
35272 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
35273 bool enter)
35275 tree stmt, clauses;
35277 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
35278 || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
35280 cp_parser_error (parser, enter
35281 ? "expected %<data%> in %<#pragma acc enter data%>"
35282 : "expected %<data%> in %<#pragma acc exit data%>");
35283 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35284 return NULL_TREE;
35287 const char *p =
35288 IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
35289 if (strcmp (p, "data") != 0)
35291 cp_parser_error (parser, "invalid pragma");
35292 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35293 return NULL_TREE;
35296 cp_lexer_consume_token (parser->lexer);
35298 if (enter)
35299 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
35300 "#pragma acc enter data", pragma_tok);
35301 else
35302 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
35303 "#pragma acc exit data", pragma_tok);
35305 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35307 error_at (pragma_tok->location,
35308 "%<#pragma acc enter data%> has no data movement clause");
35309 return NULL_TREE;
35312 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
35313 TREE_TYPE (stmt) = void_type_node;
35314 OMP_STANDALONE_CLAUSES (stmt) = clauses;
35315 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35316 add_stmt (stmt);
35317 return stmt;
35320 /* OpenACC 2.0:
35321 # pragma acc loop oacc-loop-clause[optseq] new-line
35322 structured-block */
35324 #define OACC_LOOP_CLAUSE_MASK \
35325 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
35326 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
35327 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
35328 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
35329 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
35330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
35331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
35332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
35333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
35334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
35336 static tree
35337 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
35338 omp_clause_mask mask, tree *cclauses)
35340 strcat (p_name, " loop");
35341 mask |= OACC_LOOP_CLAUSE_MASK;
35343 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
35344 cclauses == NULL);
35345 if (cclauses)
35347 clauses = c_oacc_split_loop_clauses (clauses, cclauses);
35348 if (*cclauses)
35349 *cclauses = finish_omp_clauses (*cclauses, false);
35350 if (clauses)
35351 clauses = finish_omp_clauses (clauses, false);
35354 tree block = begin_omp_structured_block ();
35355 int save = cp_parser_begin_omp_structured_block (parser);
35356 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL);
35357 cp_parser_end_omp_structured_block (parser, save);
35358 add_stmt (finish_omp_structured_block (block));
35360 return stmt;
35363 /* OpenACC 2.0:
35364 # pragma acc kernels oacc-kernels-clause[optseq] new-line
35365 structured-block
35369 # pragma acc parallel oacc-parallel-clause[optseq] new-line
35370 structured-block
35373 #define OACC_KERNELS_CLAUSE_MASK \
35374 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35375 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35376 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35377 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35378 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35379 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
35380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35382 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35383 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35385 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35386 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35387 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35389 #define OACC_PARALLEL_CLAUSE_MASK \
35390 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
35396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
35398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
35400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
35401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
35407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
35408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
35409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35411 static tree
35412 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
35413 char *p_name)
35415 omp_clause_mask mask;
35416 enum tree_code code;
35417 switch (cp_parser_pragma_kind (pragma_tok))
35419 case PRAGMA_OACC_KERNELS:
35420 strcat (p_name, " kernels");
35421 mask = OACC_KERNELS_CLAUSE_MASK;
35422 code = OACC_KERNELS;
35423 break;
35424 case PRAGMA_OACC_PARALLEL:
35425 strcat (p_name, " parallel");
35426 mask = OACC_PARALLEL_CLAUSE_MASK;
35427 code = OACC_PARALLEL;
35428 break;
35429 default:
35430 gcc_unreachable ();
35433 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35435 const char *p
35436 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
35437 if (strcmp (p, "loop") == 0)
35439 cp_lexer_consume_token (parser->lexer);
35440 mask |= OACC_LOOP_CLAUSE_MASK;
35442 tree block = begin_omp_parallel ();
35443 tree clauses;
35444 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses);
35445 return finish_omp_construct (code, block, clauses);
35449 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
35451 tree block = begin_omp_parallel ();
35452 unsigned int save = cp_parser_begin_omp_structured_block (parser);
35453 cp_parser_statement (parser, NULL_TREE, false, NULL);
35454 cp_parser_end_omp_structured_block (parser, save);
35455 return finish_omp_construct (code, block, clauses);
35458 /* OpenACC 2.0:
35459 # pragma acc update oacc-update-clause[optseq] new-line
35462 #define OACC_UPDATE_CLAUSE_MASK \
35463 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
35465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
35466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
35468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
35470 static tree
35471 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
35473 tree stmt, clauses;
35475 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
35476 "#pragma acc update", pragma_tok);
35478 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35480 error_at (pragma_tok->location,
35481 "%<#pragma acc update%> must contain at least one "
35482 "%<device%> or %<host%> or %<self%> clause");
35483 return NULL_TREE;
35486 stmt = make_node (OACC_UPDATE);
35487 TREE_TYPE (stmt) = void_type_node;
35488 OACC_UPDATE_CLAUSES (stmt) = clauses;
35489 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35490 add_stmt (stmt);
35491 return stmt;
35494 /* OpenACC 2.0:
35495 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
35497 LOC is the location of the #pragma token.
35500 #define OACC_WAIT_CLAUSE_MASK \
35501 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
35503 static tree
35504 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
35506 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
35507 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35509 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
35510 list = cp_parser_oacc_wait_list (parser, loc, list);
35512 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
35513 "#pragma acc wait", pragma_tok);
35515 stmt = c_finish_oacc_wait (loc, list, clauses);
35516 stmt = finish_expr_stmt (stmt);
35518 return stmt;
35521 /* OpenMP 4.0:
35522 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
35524 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
35525 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35526 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35527 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35528 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
35529 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
35530 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
35532 static void
35533 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
35534 enum pragma_context context)
35536 bool first_p = parser->omp_declare_simd == NULL;
35537 cp_omp_declare_simd_data data;
35538 if (first_p)
35540 data.error_seen = false;
35541 data.fndecl_seen = false;
35542 data.tokens = vNULL;
35543 parser->omp_declare_simd = &data;
35545 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
35546 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
35547 cp_lexer_consume_token (parser->lexer);
35548 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35549 parser->omp_declare_simd->error_seen = true;
35550 cp_parser_require_pragma_eol (parser, pragma_tok);
35551 struct cp_token_cache *cp
35552 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
35553 parser->omp_declare_simd->tokens.safe_push (cp);
35554 if (first_p)
35556 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
35557 cp_parser_pragma (parser, context);
35558 switch (context)
35560 case pragma_external:
35561 cp_parser_declaration (parser);
35562 break;
35563 case pragma_member:
35564 cp_parser_member_declaration (parser);
35565 break;
35566 case pragma_objc_icode:
35567 cp_parser_block_declaration (parser, /*statement_p=*/false);
35568 break;
35569 default:
35570 cp_parser_declaration_statement (parser);
35571 break;
35573 if (parser->omp_declare_simd
35574 && !parser->omp_declare_simd->error_seen
35575 && !parser->omp_declare_simd->fndecl_seen)
35576 error_at (pragma_tok->location,
35577 "%<#pragma omp declare simd%> not immediately followed by "
35578 "function declaration or definition");
35579 data.tokens.release ();
35580 parser->omp_declare_simd = NULL;
35584 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
35585 This function is modelled similar to the late parsing of omp declare
35586 simd. */
35588 static tree
35589 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
35591 struct cp_token_cache *ce;
35592 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
35593 int ii = 0;
35595 if (parser->omp_declare_simd != NULL
35596 || lookup_attribute ("simd", attrs))
35598 error ("%<#pragma omp declare simd%> of %<simd%> attribute cannot be "
35599 "used in the same function marked as a Cilk Plus SIMD-enabled "
35600 " function");
35601 parser->cilk_simd_fn_info->tokens.release ();
35602 XDELETE (parser->cilk_simd_fn_info);
35603 parser->cilk_simd_fn_info = NULL;
35604 return attrs;
35606 if (!info->error_seen && info->fndecl_seen)
35608 error ("vector attribute not immediately followed by a single function"
35609 " declaration or definition");
35610 info->error_seen = true;
35612 if (info->error_seen)
35613 return attrs;
35615 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
35617 tree c, cl;
35619 cp_parser_push_lexer_for_tokens (parser, ce);
35620 parser->lexer->in_pragma = true;
35621 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
35622 "SIMD-enabled functions attribute",
35623 NULL);
35624 cp_parser_pop_lexer (parser);
35625 if (cl)
35626 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
35628 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
35629 TREE_CHAIN (c) = attrs;
35630 attrs = c;
35632 c = build_tree_list (get_identifier ("omp declare simd"), cl);
35633 TREE_CHAIN (c) = attrs;
35634 if (processing_template_decl)
35635 ATTR_IS_DEPENDENT (c) = 1;
35636 attrs = c;
35638 info->fndecl_seen = true;
35639 parser->cilk_simd_fn_info->tokens.release ();
35640 XDELETE (parser->cilk_simd_fn_info);
35641 parser->cilk_simd_fn_info = NULL;
35642 return attrs;
35645 /* Finalize #pragma omp declare simd clauses after direct declarator has
35646 been parsed, and put that into "omp declare simd" attribute. */
35648 static tree
35649 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
35651 struct cp_token_cache *ce;
35652 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
35653 int i;
35655 if (!data->error_seen && data->fndecl_seen)
35657 error ("%<#pragma omp declare simd%> not immediately followed by "
35658 "a single function declaration or definition");
35659 data->error_seen = true;
35660 return attrs;
35662 if (data->error_seen)
35663 return attrs;
35665 FOR_EACH_VEC_ELT (data->tokens, i, ce)
35667 tree c, cl;
35669 cp_parser_push_lexer_for_tokens (parser, ce);
35670 parser->lexer->in_pragma = true;
35671 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
35672 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
35673 cp_lexer_consume_token (parser->lexer);
35674 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
35675 "#pragma omp declare simd", pragma_tok);
35676 cp_parser_pop_lexer (parser);
35677 if (cl)
35678 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
35679 c = build_tree_list (get_identifier ("omp declare simd"), cl);
35680 TREE_CHAIN (c) = attrs;
35681 if (processing_template_decl)
35682 ATTR_IS_DEPENDENT (c) = 1;
35683 attrs = c;
35686 data->fndecl_seen = true;
35687 return attrs;
35691 /* OpenMP 4.0:
35692 # pragma omp declare target new-line
35693 declarations and definitions
35694 # pragma omp end declare target new-line
35696 OpenMP 4.5:
35697 # pragma omp declare target ( extended-list ) new-line
35699 # pragma omp declare target declare-target-clauses[seq] new-line */
35701 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
35702 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
35703 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
35705 static void
35706 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
35708 tree clauses = NULL_TREE;
35709 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35710 clauses
35711 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
35712 "#pragma omp declare target", pragma_tok);
35713 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35715 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
35716 clauses);
35717 clauses = finish_omp_clauses (clauses, true);
35718 cp_parser_require_pragma_eol (parser, pragma_tok);
35720 else
35722 cp_parser_require_pragma_eol (parser, pragma_tok);
35723 scope_chain->omp_declare_target_attribute++;
35724 return;
35726 if (scope_chain->omp_declare_target_attribute)
35727 error_at (pragma_tok->location,
35728 "%<#pragma omp declare target%> with clauses in between "
35729 "%<#pragma omp declare target%> without clauses and "
35730 "%<#pragma omp end declare target%>");
35731 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
35733 tree t = OMP_CLAUSE_DECL (c), id;
35734 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
35735 tree at2 = lookup_attribute ("omp declare target link",
35736 DECL_ATTRIBUTES (t));
35737 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
35739 id = get_identifier ("omp declare target link");
35740 std::swap (at1, at2);
35742 else
35743 id = get_identifier ("omp declare target");
35744 if (at2)
35746 error_at (OMP_CLAUSE_LOCATION (c),
35747 "%qD specified both in declare target %<link%> and %<to%>"
35748 " clauses", t);
35749 continue;
35751 if (!at1)
35753 symtab_node *node = symtab_node::get (t);
35754 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
35755 if (node != NULL)
35757 node->offloadable = 1;
35758 if (ENABLE_OFFLOADING)
35760 g->have_offload = true;
35761 if (is_a <varpool_node *> (node))
35762 vec_safe_push (offload_vars, t);
35769 static void
35770 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
35772 const char *p = "";
35773 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35775 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35776 p = IDENTIFIER_POINTER (id);
35778 if (strcmp (p, "declare") == 0)
35780 cp_lexer_consume_token (parser->lexer);
35781 p = "";
35782 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35784 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35785 p = IDENTIFIER_POINTER (id);
35787 if (strcmp (p, "target") == 0)
35788 cp_lexer_consume_token (parser->lexer);
35789 else
35791 cp_parser_error (parser, "expected %<target%>");
35792 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35793 return;
35796 else
35798 cp_parser_error (parser, "expected %<declare%>");
35799 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35800 return;
35802 cp_parser_require_pragma_eol (parser, pragma_tok);
35803 if (!scope_chain->omp_declare_target_attribute)
35804 error_at (pragma_tok->location,
35805 "%<#pragma omp end declare target%> without corresponding "
35806 "%<#pragma omp declare target%>");
35807 else
35808 scope_chain->omp_declare_target_attribute--;
35811 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
35812 expression and optional initializer clause of
35813 #pragma omp declare reduction. We store the expression(s) as
35814 either 3, 6 or 7 special statements inside of the artificial function's
35815 body. The first two statements are DECL_EXPRs for the artificial
35816 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
35817 expression that uses those variables.
35818 If there was any INITIALIZER clause, this is followed by further statements,
35819 the fourth and fifth statements are DECL_EXPRs for the artificial
35820 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
35821 constructor variant (first token after open paren is not omp_priv),
35822 then the sixth statement is a statement with the function call expression
35823 that uses the OMP_PRIV and optionally OMP_ORIG variable.
35824 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
35825 to initialize the OMP_PRIV artificial variable and there is seventh
35826 statement, a DECL_EXPR of the OMP_PRIV statement again. */
35828 static bool
35829 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
35831 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
35832 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
35833 type = TREE_TYPE (type);
35834 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
35835 DECL_ARTIFICIAL (omp_out) = 1;
35836 pushdecl (omp_out);
35837 add_decl_expr (omp_out);
35838 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
35839 DECL_ARTIFICIAL (omp_in) = 1;
35840 pushdecl (omp_in);
35841 add_decl_expr (omp_in);
35842 tree combiner;
35843 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
35845 keep_next_level (true);
35846 tree block = begin_omp_structured_block ();
35847 combiner = cp_parser_expression (parser);
35848 finish_expr_stmt (combiner);
35849 block = finish_omp_structured_block (block);
35850 add_stmt (block);
35852 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
35853 return false;
35855 const char *p = "";
35856 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35858 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35859 p = IDENTIFIER_POINTER (id);
35862 if (strcmp (p, "initializer") == 0)
35864 cp_lexer_consume_token (parser->lexer);
35865 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35866 return false;
35868 p = "";
35869 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35871 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35872 p = IDENTIFIER_POINTER (id);
35875 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
35876 DECL_ARTIFICIAL (omp_priv) = 1;
35877 pushdecl (omp_priv);
35878 add_decl_expr (omp_priv);
35879 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
35880 DECL_ARTIFICIAL (omp_orig) = 1;
35881 pushdecl (omp_orig);
35882 add_decl_expr (omp_orig);
35884 keep_next_level (true);
35885 block = begin_omp_structured_block ();
35887 bool ctor = false;
35888 if (strcmp (p, "omp_priv") == 0)
35890 bool is_direct_init, is_non_constant_init;
35891 ctor = true;
35892 cp_lexer_consume_token (parser->lexer);
35893 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
35894 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
35895 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
35896 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
35897 == CPP_CLOSE_PAREN
35898 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
35899 == CPP_CLOSE_PAREN))
35901 finish_omp_structured_block (block);
35902 error ("invalid initializer clause");
35903 return false;
35905 initializer = cp_parser_initializer (parser, &is_direct_init,
35906 &is_non_constant_init);
35907 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
35908 NULL_TREE, LOOKUP_ONLYCONVERTING);
35910 else
35912 cp_parser_parse_tentatively (parser);
35913 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
35914 /*check_dependency_p=*/true,
35915 /*template_p=*/NULL,
35916 /*declarator_p=*/false,
35917 /*optional_p=*/false);
35918 vec<tree, va_gc> *args;
35919 if (fn_name == error_mark_node
35920 || cp_parser_error_occurred (parser)
35921 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
35922 || ((args = cp_parser_parenthesized_expression_list
35923 (parser, non_attr, /*cast_p=*/false,
35924 /*allow_expansion_p=*/true,
35925 /*non_constant_p=*/NULL)),
35926 cp_parser_error_occurred (parser)))
35928 finish_omp_structured_block (block);
35929 cp_parser_abort_tentative_parse (parser);
35930 cp_parser_error (parser, "expected id-expression (arguments)");
35931 return false;
35933 unsigned int i;
35934 tree arg;
35935 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
35936 if (arg == omp_priv
35937 || (TREE_CODE (arg) == ADDR_EXPR
35938 && TREE_OPERAND (arg, 0) == omp_priv))
35939 break;
35940 cp_parser_abort_tentative_parse (parser);
35941 if (arg == NULL_TREE)
35942 error ("one of the initializer call arguments should be %<omp_priv%>"
35943 " or %<&omp_priv%>");
35944 initializer = cp_parser_postfix_expression (parser, false, false, false,
35945 false, NULL);
35946 finish_expr_stmt (initializer);
35949 block = finish_omp_structured_block (block);
35950 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
35951 add_stmt (block);
35953 if (ctor)
35954 add_decl_expr (omp_orig);
35956 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
35957 return false;
35960 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
35961 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
35963 return true;
35966 /* OpenMP 4.0
35967 #pragma omp declare reduction (reduction-id : typename-list : expression) \
35968 initializer-clause[opt] new-line
35970 initializer-clause:
35971 initializer (omp_priv initializer)
35972 initializer (function-name (argument-list)) */
35974 static void
35975 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
35976 enum pragma_context)
35978 auto_vec<tree> types;
35979 enum tree_code reduc_code = ERROR_MARK;
35980 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
35981 unsigned int i;
35982 cp_token *first_token;
35983 cp_token_cache *cp;
35984 int errs;
35985 void *p;
35987 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
35988 p = obstack_alloc (&declarator_obstack, 0);
35990 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35991 goto fail;
35993 switch (cp_lexer_peek_token (parser->lexer)->type)
35995 case CPP_PLUS:
35996 reduc_code = PLUS_EXPR;
35997 break;
35998 case CPP_MULT:
35999 reduc_code = MULT_EXPR;
36000 break;
36001 case CPP_MINUS:
36002 reduc_code = MINUS_EXPR;
36003 break;
36004 case CPP_AND:
36005 reduc_code = BIT_AND_EXPR;
36006 break;
36007 case CPP_XOR:
36008 reduc_code = BIT_XOR_EXPR;
36009 break;
36010 case CPP_OR:
36011 reduc_code = BIT_IOR_EXPR;
36012 break;
36013 case CPP_AND_AND:
36014 reduc_code = TRUTH_ANDIF_EXPR;
36015 break;
36016 case CPP_OR_OR:
36017 reduc_code = TRUTH_ORIF_EXPR;
36018 break;
36019 case CPP_NAME:
36020 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
36021 break;
36022 default:
36023 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
36024 "%<|%>, %<&&%>, %<||%> or identifier");
36025 goto fail;
36028 if (reduc_code != ERROR_MARK)
36029 cp_lexer_consume_token (parser->lexer);
36031 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
36032 if (reduc_id == error_mark_node)
36033 goto fail;
36035 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36036 goto fail;
36038 /* Types may not be defined in declare reduction type list. */
36039 const char *saved_message;
36040 saved_message = parser->type_definition_forbidden_message;
36041 parser->type_definition_forbidden_message
36042 = G_("types may not be defined in declare reduction type list");
36043 bool saved_colon_corrects_to_scope_p;
36044 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
36045 parser->colon_corrects_to_scope_p = false;
36046 bool saved_colon_doesnt_start_class_def_p;
36047 saved_colon_doesnt_start_class_def_p
36048 = parser->colon_doesnt_start_class_def_p;
36049 parser->colon_doesnt_start_class_def_p = true;
36051 while (true)
36053 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36054 type = cp_parser_type_id (parser);
36055 if (type == error_mark_node)
36057 else if (ARITHMETIC_TYPE_P (type)
36058 && (orig_reduc_id == NULL_TREE
36059 || (TREE_CODE (type) != COMPLEX_TYPE
36060 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
36061 "min") == 0
36062 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
36063 "max") == 0))))
36064 error_at (loc, "predeclared arithmetic type %qT in "
36065 "%<#pragma omp declare reduction%>", type);
36066 else if (TREE_CODE (type) == FUNCTION_TYPE
36067 || TREE_CODE (type) == METHOD_TYPE
36068 || TREE_CODE (type) == ARRAY_TYPE)
36069 error_at (loc, "function or array type %qT in "
36070 "%<#pragma omp declare reduction%>", type);
36071 else if (TREE_CODE (type) == REFERENCE_TYPE)
36072 error_at (loc, "reference type %qT in "
36073 "%<#pragma omp declare reduction%>", type);
36074 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
36075 error_at (loc, "const, volatile or __restrict qualified type %qT in "
36076 "%<#pragma omp declare reduction%>", type);
36077 else
36078 types.safe_push (type);
36080 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36081 cp_lexer_consume_token (parser->lexer);
36082 else
36083 break;
36086 /* Restore the saved message. */
36087 parser->type_definition_forbidden_message = saved_message;
36088 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36089 parser->colon_doesnt_start_class_def_p
36090 = saved_colon_doesnt_start_class_def_p;
36092 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
36093 || types.is_empty ())
36095 fail:
36096 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36097 goto done;
36100 first_token = cp_lexer_peek_token (parser->lexer);
36101 cp = NULL;
36102 errs = errorcount;
36103 FOR_EACH_VEC_ELT (types, i, type)
36105 tree fntype
36106 = build_function_type_list (void_type_node,
36107 cp_build_reference_type (type, false),
36108 NULL_TREE);
36109 tree this_reduc_id = reduc_id;
36110 if (!dependent_type_p (type))
36111 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
36112 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
36113 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
36114 DECL_ARTIFICIAL (fndecl) = 1;
36115 DECL_EXTERNAL (fndecl) = 1;
36116 DECL_DECLARED_INLINE_P (fndecl) = 1;
36117 DECL_IGNORED_P (fndecl) = 1;
36118 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
36119 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
36120 DECL_ATTRIBUTES (fndecl)
36121 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
36122 DECL_ATTRIBUTES (fndecl));
36123 if (processing_template_decl)
36124 fndecl = push_template_decl (fndecl);
36125 bool block_scope = false;
36126 tree block = NULL_TREE;
36127 if (current_function_decl)
36129 block_scope = true;
36130 DECL_CONTEXT (fndecl) = global_namespace;
36131 if (!processing_template_decl)
36132 pushdecl (fndecl);
36134 else if (current_class_type)
36136 if (cp == NULL)
36138 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36139 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36140 cp_lexer_consume_token (parser->lexer);
36141 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36142 goto fail;
36143 cp = cp_token_cache_new (first_token,
36144 cp_lexer_peek_nth_token (parser->lexer,
36145 2));
36147 DECL_STATIC_FUNCTION_P (fndecl) = 1;
36148 finish_member_declaration (fndecl);
36149 DECL_PENDING_INLINE_INFO (fndecl) = cp;
36150 DECL_PENDING_INLINE_P (fndecl) = 1;
36151 vec_safe_push (unparsed_funs_with_definitions, fndecl);
36152 continue;
36154 else
36156 DECL_CONTEXT (fndecl) = current_namespace;
36157 pushdecl (fndecl);
36159 if (!block_scope)
36160 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
36161 else
36162 block = begin_omp_structured_block ();
36163 if (cp)
36165 cp_parser_push_lexer_for_tokens (parser, cp);
36166 parser->lexer->in_pragma = true;
36168 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
36170 if (!block_scope)
36171 finish_function (0);
36172 else
36173 DECL_CONTEXT (fndecl) = current_function_decl;
36174 if (cp)
36175 cp_parser_pop_lexer (parser);
36176 goto fail;
36178 if (cp)
36179 cp_parser_pop_lexer (parser);
36180 if (!block_scope)
36181 finish_function (0);
36182 else
36184 DECL_CONTEXT (fndecl) = current_function_decl;
36185 block = finish_omp_structured_block (block);
36186 if (TREE_CODE (block) == BIND_EXPR)
36187 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
36188 else if (TREE_CODE (block) == STATEMENT_LIST)
36189 DECL_SAVED_TREE (fndecl) = block;
36190 if (processing_template_decl)
36191 add_decl_expr (fndecl);
36193 cp_check_omp_declare_reduction (fndecl);
36194 if (cp == NULL && types.length () > 1)
36195 cp = cp_token_cache_new (first_token,
36196 cp_lexer_peek_nth_token (parser->lexer, 2));
36197 if (errs != errorcount)
36198 break;
36201 cp_parser_require_pragma_eol (parser, pragma_tok);
36203 done:
36204 /* Free any declarators allocated. */
36205 obstack_free (&declarator_obstack, p);
36208 /* OpenMP 4.0
36209 #pragma omp declare simd declare-simd-clauses[optseq] new-line
36210 #pragma omp declare reduction (reduction-id : typename-list : expression) \
36211 initializer-clause[opt] new-line
36212 #pragma omp declare target new-line */
36214 static void
36215 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
36216 enum pragma_context context)
36218 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36220 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36221 const char *p = IDENTIFIER_POINTER (id);
36223 if (strcmp (p, "simd") == 0)
36225 cp_lexer_consume_token (parser->lexer);
36226 cp_parser_omp_declare_simd (parser, pragma_tok,
36227 context);
36228 return;
36230 cp_ensure_no_omp_declare_simd (parser);
36231 if (strcmp (p, "reduction") == 0)
36233 cp_lexer_consume_token (parser->lexer);
36234 cp_parser_omp_declare_reduction (parser, pragma_tok,
36235 context);
36236 return;
36238 if (!flag_openmp) /* flag_openmp_simd */
36240 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36241 return;
36243 if (strcmp (p, "target") == 0)
36245 cp_lexer_consume_token (parser->lexer);
36246 cp_parser_omp_declare_target (parser, pragma_tok);
36247 return;
36250 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
36251 "or %<target%>");
36252 cp_parser_require_pragma_eol (parser, pragma_tok);
36255 /* OpenMP 4.5:
36256 #pragma omp taskloop taskloop-clause[optseq] new-line
36257 for-loop
36259 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
36260 for-loop */
36262 #define OMP_TASKLOOP_CLAUSE_MASK \
36263 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36264 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36265 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36266 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
36268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
36269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
36270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
36271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
36272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
36274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
36275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
36276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
36278 static tree
36279 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
36280 char *p_name, omp_clause_mask mask, tree *cclauses)
36282 tree clauses, sb, ret;
36283 unsigned int save;
36284 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36286 strcat (p_name, " taskloop");
36287 mask |= OMP_TASKLOOP_CLAUSE_MASK;
36289 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36291 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36292 const char *p = IDENTIFIER_POINTER (id);
36294 if (strcmp (p, "simd") == 0)
36296 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36297 if (cclauses == NULL)
36298 cclauses = cclauses_buf;
36300 cp_lexer_consume_token (parser->lexer);
36301 if (!flag_openmp) /* flag_openmp_simd */
36302 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36303 cclauses);
36304 sb = begin_omp_structured_block ();
36305 save = cp_parser_begin_omp_structured_block (parser);
36306 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36307 cclauses);
36308 cp_parser_end_omp_structured_block (parser, save);
36309 tree body = finish_omp_structured_block (sb);
36310 if (ret == NULL)
36311 return ret;
36312 ret = make_node (OMP_TASKLOOP);
36313 TREE_TYPE (ret) = void_type_node;
36314 OMP_FOR_BODY (ret) = body;
36315 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
36316 SET_EXPR_LOCATION (ret, loc);
36317 add_stmt (ret);
36318 return ret;
36321 if (!flag_openmp) /* flag_openmp_simd */
36323 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36324 return NULL_TREE;
36327 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36328 cclauses == NULL);
36329 if (cclauses)
36331 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
36332 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
36335 sb = begin_omp_structured_block ();
36336 save = cp_parser_begin_omp_structured_block (parser);
36338 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses);
36340 cp_parser_end_omp_structured_block (parser, save);
36341 add_stmt (finish_omp_structured_block (sb));
36343 return ret;
36347 /* OpenACC 2.0:
36348 # pragma acc routine oacc-routine-clause[optseq] new-line
36349 function-definition
36351 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
36354 #define OACC_ROUTINE_CLAUSE_MASK \
36355 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36358 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
36361 /* Parse the OpenACC routine pragma. This has an optional '( name )'
36362 component, which must resolve to a declared namespace-scope
36363 function. The clauses are either processed directly (for a named
36364 function), or defered until the immediatley following declaration
36365 is parsed. */
36367 static void
36368 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
36369 enum pragma_context context)
36371 bool first_p = parser->oacc_routine == NULL;
36372 location_t loc = pragma_tok->location;
36373 cp_omp_declare_simd_data data;
36374 if (first_p)
36376 data.error_seen = false;
36377 data.fndecl_seen = false;
36378 data.tokens = vNULL;
36379 data.clauses = NULL_TREE;
36380 parser->oacc_routine = &data;
36383 tree decl = NULL_TREE;
36384 /* Create a dummy claue, to record location. */
36385 tree c_head = build_omp_clause (pragma_tok->location, OMP_CLAUSE_SEQ);
36387 if (context != pragma_external)
36389 cp_parser_error (parser, "%<#pragma acc routine%> not at file scope");
36390 parser->oacc_routine->error_seen = true;
36391 parser->oacc_routine = NULL;
36392 return;
36395 /* Look for optional '( name )'. */
36396 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36398 if (!first_p)
36400 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36401 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36402 cp_lexer_consume_token (parser->lexer);
36403 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36404 parser->oacc_routine->error_seen = true;
36405 cp_parser_require_pragma_eol (parser, pragma_tok);
36407 error_at (OMP_CLAUSE_LOCATION (parser->oacc_routine->clauses),
36408 "%<#pragma acc routine%> not followed by a "
36409 "function declaration or definition");
36411 parser->oacc_routine->error_seen = true;
36412 return;
36415 cp_lexer_consume_token (parser->lexer);
36416 cp_token *token = cp_lexer_peek_token (parser->lexer);
36418 /* We parse the name as an id-expression. If it resolves to
36419 anything other than a non-overloaded function at namespace
36420 scope, it's an error. */
36421 tree id = cp_parser_id_expression (parser,
36422 /*template_keyword_p=*/false,
36423 /*check_dependency_p=*/false,
36424 /*template_p=*/NULL,
36425 /*declarator_p=*/false,
36426 /*optional_p=*/false);
36427 decl = cp_parser_lookup_name_simple (parser, id, token->location);
36428 if (id != error_mark_node && decl == error_mark_node)
36429 cp_parser_name_lookup_error (parser, id, decl, NLE_NULL,
36430 token->location);
36432 if (decl == error_mark_node
36433 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36435 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36436 parser->oacc_routine = NULL;
36437 return;
36440 /* Build a chain of clauses. */
36441 parser->lexer->in_pragma = true;
36442 tree clauses = NULL_TREE;
36443 clauses = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
36444 "#pragma acc routine",
36445 cp_lexer_peek_token
36446 (parser->lexer));
36448 /* Force clauses to be non-null, by attaching context to it. */
36449 clauses = tree_cons (c_head, clauses, NULL_TREE);
36451 if (decl && is_overloaded_fn (decl)
36452 && (TREE_CODE (decl) != FUNCTION_DECL
36453 || DECL_FUNCTION_TEMPLATE_P (decl)))
36455 error_at (loc, "%<#pragma acc routine%> names a set of overloads");
36456 parser->oacc_routine = NULL;
36457 return;
36460 /* Perhaps we should use the same rule as declarations in different
36461 namespaces? */
36462 if (!DECL_NAMESPACE_SCOPE_P (decl))
36464 error_at (loc, "%<#pragma acc routine%> does not refer to a "
36465 "namespace scope function");
36466 parser->oacc_routine = NULL;
36467 return;
36470 if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
36472 error_at (loc,
36473 "%<#pragma acc routine%> does not refer to a function");
36474 parser->oacc_routine = NULL;
36475 return;
36478 data.clauses = clauses;
36480 cp_finalize_oacc_routine (parser, decl, false);
36481 data.tokens.release ();
36482 parser->oacc_routine = NULL;
36484 else
36486 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36487 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36488 cp_lexer_consume_token (parser->lexer);
36489 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36490 parser->oacc_routine->error_seen = true;
36491 cp_parser_require_pragma_eol (parser, pragma_tok);
36493 struct cp_token_cache *cp
36494 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
36495 parser->oacc_routine->tokens.safe_push (cp);
36497 if (first_p)
36498 parser->oacc_routine->clauses = c_head;
36500 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
36501 cp_parser_pragma (parser, context);
36503 if (first_p)
36505 /* Create an empty list of clauses. */
36506 parser->oacc_routine->clauses = tree_cons (c_head, NULL_TREE,
36507 NULL_TREE);
36508 cp_parser_declaration (parser);
36510 if (parser->oacc_routine
36511 && !parser->oacc_routine->error_seen
36512 && !parser->oacc_routine->fndecl_seen)
36513 error_at (loc, "%<#pragma acc routine%> not followed by a "
36514 "function declaration or definition");
36516 data.tokens.release ();
36517 parser->oacc_routine = NULL;
36522 /* Finalize #pragma acc routine clauses after direct declarator has
36523 been parsed, and put that into "oacc function" attribute. */
36525 static tree
36526 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
36528 struct cp_token_cache *ce;
36529 cp_omp_declare_simd_data *data = parser->oacc_routine;
36530 tree cl, clauses = parser->oacc_routine->clauses;
36531 location_t loc;
36533 loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE(clauses));
36535 if ((!data->error_seen && data->fndecl_seen)
36536 || data->tokens.length () != 1)
36538 error_at (loc, "%<#pragma acc routine%> not followed by a "
36539 "function declaration or definition");
36540 data->error_seen = true;
36541 return attrs;
36543 if (data->error_seen)
36544 return attrs;
36546 ce = data->tokens[0];
36548 cp_parser_push_lexer_for_tokens (parser, ce);
36549 parser->lexer->in_pragma = true;
36550 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
36552 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
36553 cl = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
36554 "#pragma acc routine", pragma_tok);
36555 cp_parser_pop_lexer (parser);
36557 tree c_head = build_omp_clause (loc, OMP_CLAUSE_SEQ);
36559 /* Force clauses to be non-null, by attaching context to it. */
36560 parser->oacc_routine->clauses = tree_cons (c_head, cl, NULL_TREE);
36562 data->fndecl_seen = true;
36563 return attrs;
36566 /* Apply any saved OpenACC routine clauses to a just-parsed
36567 declaration. */
36569 static void
36570 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
36572 if (__builtin_expect (parser->oacc_routine != NULL, 0))
36574 tree clauses = parser->oacc_routine->clauses;
36575 location_t loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE(clauses));
36577 if (parser->oacc_routine->error_seen)
36578 return;
36580 if (fndecl == error_mark_node)
36582 parser->oacc_routine = NULL;
36583 return;
36586 if (TREE_CODE (fndecl) != FUNCTION_DECL)
36588 cp_ensure_no_oacc_routine (parser);
36589 return;
36592 if (!fndecl || TREE_CODE (fndecl) != FUNCTION_DECL)
36594 error_at (loc,
36595 "%<#pragma acc routine%> not followed by a function "
36596 "declaration or definition");
36597 parser->oacc_routine = NULL;
36600 if (get_oacc_fn_attrib (fndecl))
36602 error_at (loc, "%<#pragma acc routine%> already applied to %D",
36603 fndecl);
36604 parser->oacc_routine = NULL;
36607 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
36609 error_at (loc, "%<#pragma acc routine%> must be applied before %s",
36610 TREE_USED (fndecl) ? "use" : "definition");
36611 parser->oacc_routine = NULL;
36614 /* Process for function attrib */
36615 tree dims = build_oacc_routine_dims (TREE_VALUE (clauses));
36616 replace_oacc_fn_attrib (fndecl, dims);
36618 /* Add an "omp target" attribute. */
36619 DECL_ATTRIBUTES (fndecl)
36620 = tree_cons (get_identifier ("omp declare target"),
36621 NULL_TREE, DECL_ATTRIBUTES (fndecl));
36625 /* Main entry point to OpenMP statement pragmas. */
36627 static void
36628 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
36630 tree stmt;
36631 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
36632 omp_clause_mask mask (0);
36634 switch (cp_parser_pragma_kind (pragma_tok))
36636 case PRAGMA_OACC_ATOMIC:
36637 cp_parser_omp_atomic (parser, pragma_tok);
36638 return;
36639 case PRAGMA_OACC_CACHE:
36640 stmt = cp_parser_oacc_cache (parser, pragma_tok);
36641 break;
36642 case PRAGMA_OACC_DATA:
36643 stmt = cp_parser_oacc_data (parser, pragma_tok);
36644 break;
36645 case PRAGMA_OACC_ENTER_DATA:
36646 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
36647 break;
36648 case PRAGMA_OACC_EXIT_DATA:
36649 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
36650 break;
36651 case PRAGMA_OACC_HOST_DATA:
36652 stmt = cp_parser_oacc_host_data (parser, pragma_tok);
36653 break;
36654 case PRAGMA_OACC_KERNELS:
36655 case PRAGMA_OACC_PARALLEL:
36656 strcpy (p_name, "#pragma acc");
36657 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name);
36658 break;
36659 case PRAGMA_OACC_LOOP:
36660 strcpy (p_name, "#pragma acc");
36661 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL);
36662 break;
36663 case PRAGMA_OACC_UPDATE:
36664 stmt = cp_parser_oacc_update (parser, pragma_tok);
36665 break;
36666 case PRAGMA_OACC_WAIT:
36667 stmt = cp_parser_oacc_wait (parser, pragma_tok);
36668 break;
36669 case PRAGMA_OMP_ATOMIC:
36670 cp_parser_omp_atomic (parser, pragma_tok);
36671 return;
36672 case PRAGMA_OMP_CRITICAL:
36673 stmt = cp_parser_omp_critical (parser, pragma_tok);
36674 break;
36675 case PRAGMA_OMP_DISTRIBUTE:
36676 strcpy (p_name, "#pragma omp");
36677 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
36678 break;
36679 case PRAGMA_OMP_FOR:
36680 strcpy (p_name, "#pragma omp");
36681 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
36682 break;
36683 case PRAGMA_OMP_MASTER:
36684 stmt = cp_parser_omp_master (parser, pragma_tok);
36685 break;
36686 case PRAGMA_OMP_PARALLEL:
36687 strcpy (p_name, "#pragma omp");
36688 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
36689 break;
36690 case PRAGMA_OMP_SECTIONS:
36691 strcpy (p_name, "#pragma omp");
36692 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
36693 break;
36694 case PRAGMA_OMP_SIMD:
36695 strcpy (p_name, "#pragma omp");
36696 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
36697 break;
36698 case PRAGMA_OMP_SINGLE:
36699 stmt = cp_parser_omp_single (parser, pragma_tok);
36700 break;
36701 case PRAGMA_OMP_TASK:
36702 stmt = cp_parser_omp_task (parser, pragma_tok);
36703 break;
36704 case PRAGMA_OMP_TASKGROUP:
36705 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
36706 break;
36707 case PRAGMA_OMP_TASKLOOP:
36708 strcpy (p_name, "#pragma omp");
36709 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL);
36710 break;
36711 case PRAGMA_OMP_TEAMS:
36712 strcpy (p_name, "#pragma omp");
36713 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
36714 break;
36715 default:
36716 gcc_unreachable ();
36719 protected_set_expr_location (stmt, pragma_tok->location);
36722 /* Transactional Memory parsing routines. */
36724 /* Parse a transaction attribute.
36726 txn-attribute:
36727 attribute
36728 [ [ identifier ] ]
36730 We use this instead of cp_parser_attributes_opt for transactions to avoid
36731 the pedwarn in C++98 mode. */
36733 static tree
36734 cp_parser_txn_attribute_opt (cp_parser *parser)
36736 cp_token *token;
36737 tree attr_name, attr = NULL;
36739 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
36740 return cp_parser_attributes_opt (parser);
36742 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
36743 return NULL_TREE;
36744 cp_lexer_consume_token (parser->lexer);
36745 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
36746 goto error1;
36748 token = cp_lexer_peek_token (parser->lexer);
36749 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
36751 token = cp_lexer_consume_token (parser->lexer);
36753 attr_name = (token->type == CPP_KEYWORD
36754 /* For keywords, use the canonical spelling,
36755 not the parsed identifier. */
36756 ? ridpointers[(int) token->keyword]
36757 : token->u.value);
36758 attr = build_tree_list (attr_name, NULL_TREE);
36760 else
36761 cp_parser_error (parser, "expected identifier");
36763 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
36764 error1:
36765 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
36766 return attr;
36769 /* Parse a __transaction_atomic or __transaction_relaxed statement.
36771 transaction-statement:
36772 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
36773 compound-statement
36774 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
36777 static tree
36778 cp_parser_transaction (cp_parser *parser, cp_token *token)
36780 unsigned char old_in = parser->in_transaction;
36781 unsigned char this_in = 1, new_in;
36782 enum rid keyword = token->keyword;
36783 tree stmt, attrs, noex;
36785 cp_lexer_consume_token (parser->lexer);
36787 if (keyword == RID_TRANSACTION_RELAXED
36788 || keyword == RID_SYNCHRONIZED)
36789 this_in |= TM_STMT_ATTR_RELAXED;
36790 else
36792 attrs = cp_parser_txn_attribute_opt (parser);
36793 if (attrs)
36794 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
36797 /* Parse a noexcept specification. */
36798 if (keyword == RID_ATOMIC_NOEXCEPT)
36799 noex = boolean_true_node;
36800 else if (keyword == RID_ATOMIC_CANCEL)
36802 /* cancel-and-throw is unimplemented. */
36803 sorry ("atomic_cancel");
36804 noex = NULL_TREE;
36806 else
36807 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
36809 /* Keep track if we're in the lexical scope of an outer transaction. */
36810 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
36812 stmt = begin_transaction_stmt (token->location, NULL, this_in);
36814 parser->in_transaction = new_in;
36815 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
36816 parser->in_transaction = old_in;
36818 finish_transaction_stmt (stmt, NULL, this_in, noex);
36820 return stmt;
36823 /* Parse a __transaction_atomic or __transaction_relaxed expression.
36825 transaction-expression:
36826 __transaction_atomic txn-noexcept-spec[opt] ( expression )
36827 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
36830 static tree
36831 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
36833 unsigned char old_in = parser->in_transaction;
36834 unsigned char this_in = 1;
36835 cp_token *token;
36836 tree expr, noex;
36837 bool noex_expr;
36838 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36840 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
36841 || keyword == RID_TRANSACTION_RELAXED);
36843 if (!flag_tm)
36844 error_at (loc,
36845 keyword == RID_TRANSACTION_RELAXED
36846 ? G_("%<__transaction_relaxed%> without transactional memory "
36847 "support enabled")
36848 : G_("%<__transaction_atomic%> without transactional memory "
36849 "support enabled"));
36851 token = cp_parser_require_keyword (parser, keyword,
36852 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
36853 : RT_TRANSACTION_RELAXED));
36854 gcc_assert (token != NULL);
36856 if (keyword == RID_TRANSACTION_RELAXED)
36857 this_in |= TM_STMT_ATTR_RELAXED;
36859 /* Set this early. This might mean that we allow transaction_cancel in
36860 an expression that we find out later actually has to be a constexpr.
36861 However, we expect that cxx_constant_value will be able to deal with
36862 this; also, if the noexcept has no constexpr, then what we parse next
36863 really is a transaction's body. */
36864 parser->in_transaction = this_in;
36866 /* Parse a noexcept specification. */
36867 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
36868 true);
36870 if (!noex || !noex_expr
36871 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36873 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
36875 expr = cp_parser_expression (parser);
36876 expr = finish_parenthesized_expr (expr);
36878 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
36880 else
36882 /* The only expression that is available got parsed for the noexcept
36883 already. noexcept is true then. */
36884 expr = noex;
36885 noex = boolean_true_node;
36888 expr = build_transaction_expr (token->location, expr, this_in, noex);
36889 parser->in_transaction = old_in;
36891 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
36892 return error_mark_node;
36894 return (flag_tm ? expr : error_mark_node);
36897 /* Parse a function-transaction-block.
36899 function-transaction-block:
36900 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
36901 function-body
36902 __transaction_atomic txn-attribute[opt] function-try-block
36903 __transaction_relaxed ctor-initializer[opt] function-body
36904 __transaction_relaxed function-try-block
36907 static bool
36908 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
36910 unsigned char old_in = parser->in_transaction;
36911 unsigned char new_in = 1;
36912 tree compound_stmt, stmt, attrs;
36913 bool ctor_initializer_p;
36914 cp_token *token;
36916 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
36917 || keyword == RID_TRANSACTION_RELAXED);
36918 token = cp_parser_require_keyword (parser, keyword,
36919 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
36920 : RT_TRANSACTION_RELAXED));
36921 gcc_assert (token != NULL);
36923 if (keyword == RID_TRANSACTION_RELAXED)
36924 new_in |= TM_STMT_ATTR_RELAXED;
36925 else
36927 attrs = cp_parser_txn_attribute_opt (parser);
36928 if (attrs)
36929 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
36932 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
36934 parser->in_transaction = new_in;
36936 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
36937 ctor_initializer_p = cp_parser_function_try_block (parser);
36938 else
36939 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
36940 (parser, /*in_function_try_block=*/false);
36942 parser->in_transaction = old_in;
36944 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
36946 return ctor_initializer_p;
36949 /* Parse a __transaction_cancel statement.
36951 cancel-statement:
36952 __transaction_cancel txn-attribute[opt] ;
36953 __transaction_cancel txn-attribute[opt] throw-expression ;
36955 ??? Cancel and throw is not yet implemented. */
36957 static tree
36958 cp_parser_transaction_cancel (cp_parser *parser)
36960 cp_token *token;
36961 bool is_outer = false;
36962 tree stmt, attrs;
36964 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
36965 RT_TRANSACTION_CANCEL);
36966 gcc_assert (token != NULL);
36968 attrs = cp_parser_txn_attribute_opt (parser);
36969 if (attrs)
36970 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
36972 /* ??? Parse cancel-and-throw here. */
36974 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
36976 if (!flag_tm)
36978 error_at (token->location, "%<__transaction_cancel%> without "
36979 "transactional memory support enabled");
36980 return error_mark_node;
36982 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
36984 error_at (token->location, "%<__transaction_cancel%> within a "
36985 "%<__transaction_relaxed%>");
36986 return error_mark_node;
36988 else if (is_outer)
36990 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
36991 && !is_tm_may_cancel_outer (current_function_decl))
36993 error_at (token->location, "outer %<__transaction_cancel%> not "
36994 "within outer %<__transaction_atomic%>");
36995 error_at (token->location,
36996 " or a %<transaction_may_cancel_outer%> function");
36997 return error_mark_node;
37000 else if (parser->in_transaction == 0)
37002 error_at (token->location, "%<__transaction_cancel%> not within "
37003 "%<__transaction_atomic%>");
37004 return error_mark_node;
37007 stmt = build_tm_abort_call (token->location, is_outer);
37008 add_stmt (stmt);
37010 return stmt;
37013 /* The parser. */
37015 static GTY (()) cp_parser *the_parser;
37018 /* Special handling for the first token or line in the file. The first
37019 thing in the file might be #pragma GCC pch_preprocess, which loads a
37020 PCH file, which is a GC collection point. So we need to handle this
37021 first pragma without benefit of an existing lexer structure.
37023 Always returns one token to the caller in *FIRST_TOKEN. This is
37024 either the true first token of the file, or the first token after
37025 the initial pragma. */
37027 static void
37028 cp_parser_initial_pragma (cp_token *first_token)
37030 tree name = NULL;
37032 cp_lexer_get_preprocessor_token (NULL, first_token);
37033 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
37034 return;
37036 cp_lexer_get_preprocessor_token (NULL, first_token);
37037 if (first_token->type == CPP_STRING)
37039 name = first_token->u.value;
37041 cp_lexer_get_preprocessor_token (NULL, first_token);
37042 if (first_token->type != CPP_PRAGMA_EOL)
37043 error_at (first_token->location,
37044 "junk at end of %<#pragma GCC pch_preprocess%>");
37046 else
37047 error_at (first_token->location, "expected string literal");
37049 /* Skip to the end of the pragma. */
37050 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
37051 cp_lexer_get_preprocessor_token (NULL, first_token);
37053 /* Now actually load the PCH file. */
37054 if (name)
37055 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
37057 /* Read one more token to return to our caller. We have to do this
37058 after reading the PCH file in, since its pointers have to be
37059 live. */
37060 cp_lexer_get_preprocessor_token (NULL, first_token);
37063 /* Parses the grainsize pragma for the _Cilk_for statement.
37064 Syntax:
37065 #pragma cilk grainsize = <VALUE>. */
37067 static void
37068 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok)
37070 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
37072 tree exp = cp_parser_binary_expression (parser, false, false,
37073 PREC_NOT_OPERATOR, NULL);
37074 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37075 if (!exp || exp == error_mark_node)
37077 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
37078 return;
37081 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
37082 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
37083 cp_parser_cilk_for (parser, exp);
37084 else
37085 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
37086 "%<#pragma cilk grainsize%> is not followed by "
37087 "%<_Cilk_for%>");
37088 return;
37090 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37093 /* Normal parsing of a pragma token. Here we can (and must) use the
37094 regular lexer. */
37096 static bool
37097 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
37099 cp_token *pragma_tok;
37100 unsigned int id;
37101 tree stmt;
37102 bool ret;
37104 pragma_tok = cp_lexer_consume_token (parser->lexer);
37105 gcc_assert (pragma_tok->type == CPP_PRAGMA);
37106 parser->lexer->in_pragma = true;
37108 id = cp_parser_pragma_kind (pragma_tok);
37109 if (id != PRAGMA_OMP_DECLARE_REDUCTION && id != PRAGMA_OACC_ROUTINE)
37110 cp_ensure_no_omp_declare_simd (parser);
37111 switch (id)
37113 case PRAGMA_GCC_PCH_PREPROCESS:
37114 error_at (pragma_tok->location,
37115 "%<#pragma GCC pch_preprocess%> must be first");
37116 break;
37118 case PRAGMA_OMP_BARRIER:
37119 switch (context)
37121 case pragma_compound:
37122 cp_parser_omp_barrier (parser, pragma_tok);
37123 return false;
37124 case pragma_stmt:
37125 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
37126 "used in compound statements");
37127 break;
37128 default:
37129 goto bad_stmt;
37131 break;
37133 case PRAGMA_OMP_FLUSH:
37134 switch (context)
37136 case pragma_compound:
37137 cp_parser_omp_flush (parser, pragma_tok);
37138 return false;
37139 case pragma_stmt:
37140 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
37141 "used in compound statements");
37142 break;
37143 default:
37144 goto bad_stmt;
37146 break;
37148 case PRAGMA_OMP_TASKWAIT:
37149 switch (context)
37151 case pragma_compound:
37152 cp_parser_omp_taskwait (parser, pragma_tok);
37153 return false;
37154 case pragma_stmt:
37155 error_at (pragma_tok->location,
37156 "%<#pragma omp taskwait%> may only be "
37157 "used in compound statements");
37158 break;
37159 default:
37160 goto bad_stmt;
37162 break;
37164 case PRAGMA_OMP_TASKYIELD:
37165 switch (context)
37167 case pragma_compound:
37168 cp_parser_omp_taskyield (parser, pragma_tok);
37169 return false;
37170 case pragma_stmt:
37171 error_at (pragma_tok->location,
37172 "%<#pragma omp taskyield%> may only be "
37173 "used in compound statements");
37174 break;
37175 default:
37176 goto bad_stmt;
37178 break;
37180 case PRAGMA_OMP_CANCEL:
37181 switch (context)
37183 case pragma_compound:
37184 cp_parser_omp_cancel (parser, pragma_tok);
37185 return false;
37186 case pragma_stmt:
37187 error_at (pragma_tok->location,
37188 "%<#pragma omp cancel%> may only be "
37189 "used in compound statements");
37190 break;
37191 default:
37192 goto bad_stmt;
37194 break;
37196 case PRAGMA_OMP_CANCELLATION_POINT:
37197 switch (context)
37199 case pragma_compound:
37200 cp_parser_omp_cancellation_point (parser, pragma_tok);
37201 return false;
37202 case pragma_stmt:
37203 error_at (pragma_tok->location,
37204 "%<#pragma omp cancellation point%> may only be "
37205 "used in compound statements");
37206 break;
37207 default:
37208 goto bad_stmt;
37210 break;
37212 case PRAGMA_OMP_THREADPRIVATE:
37213 cp_parser_omp_threadprivate (parser, pragma_tok);
37214 return false;
37216 case PRAGMA_OMP_DECLARE_REDUCTION:
37217 cp_parser_omp_declare (parser, pragma_tok, context);
37218 return false;
37220 case PRAGMA_OACC_DECLARE:
37221 cp_parser_oacc_declare (parser, pragma_tok);
37222 return false;
37224 case PRAGMA_OACC_ROUTINE:
37225 cp_parser_oacc_routine (parser, pragma_tok, context);
37226 return false;
37228 case PRAGMA_OACC_ATOMIC:
37229 case PRAGMA_OACC_CACHE:
37230 case PRAGMA_OACC_DATA:
37231 case PRAGMA_OACC_ENTER_DATA:
37232 case PRAGMA_OACC_EXIT_DATA:
37233 case PRAGMA_OACC_HOST_DATA:
37234 case PRAGMA_OACC_KERNELS:
37235 case PRAGMA_OACC_PARALLEL:
37236 case PRAGMA_OACC_LOOP:
37237 case PRAGMA_OACC_UPDATE:
37238 case PRAGMA_OACC_WAIT:
37239 case PRAGMA_OMP_ATOMIC:
37240 case PRAGMA_OMP_CRITICAL:
37241 case PRAGMA_OMP_DISTRIBUTE:
37242 case PRAGMA_OMP_FOR:
37243 case PRAGMA_OMP_MASTER:
37244 case PRAGMA_OMP_PARALLEL:
37245 case PRAGMA_OMP_SECTIONS:
37246 case PRAGMA_OMP_SIMD:
37247 case PRAGMA_OMP_SINGLE:
37248 case PRAGMA_OMP_TASK:
37249 case PRAGMA_OMP_TASKGROUP:
37250 case PRAGMA_OMP_TASKLOOP:
37251 case PRAGMA_OMP_TEAMS:
37252 if (context != pragma_stmt && context != pragma_compound)
37253 goto bad_stmt;
37254 stmt = push_omp_privatization_clauses (false);
37255 cp_parser_omp_construct (parser, pragma_tok);
37256 pop_omp_privatization_clauses (stmt);
37257 return true;
37259 case PRAGMA_OMP_ORDERED:
37260 stmt = push_omp_privatization_clauses (false);
37261 ret = cp_parser_omp_ordered (parser, pragma_tok, context);
37262 pop_omp_privatization_clauses (stmt);
37263 return ret;
37265 case PRAGMA_OMP_TARGET:
37266 stmt = push_omp_privatization_clauses (false);
37267 ret = cp_parser_omp_target (parser, pragma_tok, context);
37268 pop_omp_privatization_clauses (stmt);
37269 return ret;
37271 case PRAGMA_OMP_END_DECLARE_TARGET:
37272 cp_parser_omp_end_declare_target (parser, pragma_tok);
37273 return false;
37275 case PRAGMA_OMP_SECTION:
37276 error_at (pragma_tok->location,
37277 "%<#pragma omp section%> may only be used in "
37278 "%<#pragma omp sections%> construct");
37279 break;
37281 case PRAGMA_IVDEP:
37283 if (context == pragma_external)
37285 error_at (pragma_tok->location,
37286 "%<#pragma GCC ivdep%> must be inside a function");
37287 break;
37289 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37290 cp_token *tok;
37291 tok = cp_lexer_peek_token (the_parser->lexer);
37292 if (tok->type != CPP_KEYWORD
37293 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
37294 && tok->keyword != RID_DO))
37296 cp_parser_error (parser, "for, while or do statement expected");
37297 return false;
37299 cp_parser_iteration_statement (parser, true);
37300 return true;
37303 case PRAGMA_CILK_SIMD:
37304 if (context == pragma_external)
37306 error_at (pragma_tok->location,
37307 "%<#pragma simd%> must be inside a function");
37308 break;
37310 stmt = push_omp_privatization_clauses (false);
37311 cp_parser_cilk_simd (parser, pragma_tok);
37312 pop_omp_privatization_clauses (stmt);
37313 return true;
37315 case PRAGMA_CILK_GRAINSIZE:
37316 if (context == pragma_external)
37318 error_at (pragma_tok->location,
37319 "%<#pragma cilk grainsize%> must be inside a function");
37320 break;
37323 /* Ignore the pragma if Cilk Plus is not enabled. */
37324 if (flag_cilkplus)
37326 cp_parser_cilk_grainsize (parser, pragma_tok);
37327 return true;
37329 else
37331 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
37332 "%<#pragma cilk grainsize%>");
37333 break;
37336 default:
37337 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
37338 c_invoke_pragma_handler (id);
37339 break;
37341 bad_stmt:
37342 cp_parser_error (parser, "expected declaration specifiers");
37343 break;
37346 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37347 return false;
37350 /* The interface the pragma parsers have to the lexer. */
37352 enum cpp_ttype
37353 pragma_lex (tree *value, location_t *loc)
37355 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
37356 enum cpp_ttype ret = tok->type;
37358 *value = tok->u.value;
37359 if (loc)
37360 *loc = tok->location;
37362 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
37363 ret = CPP_EOF;
37364 else if (ret == CPP_STRING)
37365 *value = cp_parser_string_literal (the_parser, false, false);
37366 else
37368 if (ret == CPP_KEYWORD)
37369 ret = CPP_NAME;
37370 cp_lexer_consume_token (the_parser->lexer);
37373 return ret;
37377 /* External interface. */
37379 /* Parse one entire translation unit. */
37381 void
37382 c_parse_file (void)
37384 static bool already_called = false;
37386 if (already_called)
37387 fatal_error (input_location,
37388 "inter-module optimizations not implemented for C++");
37389 already_called = true;
37391 the_parser = cp_parser_new ();
37392 push_deferring_access_checks (flag_access_control
37393 ? dk_no_deferred : dk_no_check);
37394 cp_parser_translation_unit (the_parser);
37395 the_parser = NULL;
37398 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
37399 vectorlength clause:
37400 Syntax:
37401 vectorlength ( constant-expression ) */
37403 static tree
37404 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
37405 bool is_simd_fn)
37407 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37408 tree expr;
37409 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
37410 safelen clause. Thus, vectorlength is represented as OMP 4.0
37411 safelen. For SIMD-enabled function it is represented by OMP 4.0
37412 simdlen. */
37413 if (!is_simd_fn)
37414 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
37415 loc);
37416 else
37417 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
37418 loc);
37420 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37421 return error_mark_node;
37423 expr = cp_parser_constant_expression (parser);
37424 expr = maybe_constant_value (expr);
37426 /* If expr == error_mark_node, then don't emit any errors nor
37427 create a clause. if any of the above functions returns
37428 error mark node then they would have emitted an error message. */
37429 if (expr == error_mark_node)
37431 else if (!TREE_TYPE (expr)
37432 || !TREE_CONSTANT (expr)
37433 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
37434 error_at (loc, "vectorlength must be an integer constant");
37435 else if (TREE_CONSTANT (expr)
37436 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
37437 error_at (loc, "vectorlength must be a power of 2");
37438 else
37440 tree c;
37441 if (!is_simd_fn)
37443 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
37444 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
37445 OMP_CLAUSE_CHAIN (c) = clauses;
37446 clauses = c;
37448 else
37450 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
37451 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
37452 OMP_CLAUSE_CHAIN (c) = clauses;
37453 clauses = c;
37457 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37458 return error_mark_node;
37459 return clauses;
37462 /* Handles the Cilk Plus #pragma simd linear clause.
37463 Syntax:
37464 linear ( simd-linear-variable-list )
37466 simd-linear-variable-list:
37467 simd-linear-variable
37468 simd-linear-variable-list , simd-linear-variable
37470 simd-linear-variable:
37471 id-expression
37472 id-expression : simd-linear-step
37474 simd-linear-step:
37475 conditional-expression */
37477 static tree
37478 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
37480 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37482 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37483 return clauses;
37484 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37486 cp_parser_error (parser, "expected identifier");
37487 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
37488 return error_mark_node;
37491 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37492 parser->colon_corrects_to_scope_p = false;
37493 while (1)
37495 cp_token *token = cp_lexer_peek_token (parser->lexer);
37496 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37498 cp_parser_error (parser, "expected variable-name");
37499 clauses = error_mark_node;
37500 break;
37503 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
37504 false, false);
37505 tree decl = cp_parser_lookup_name_simple (parser, var_name,
37506 token->location);
37507 if (decl == error_mark_node)
37509 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
37510 token->location);
37511 clauses = error_mark_node;
37513 else
37515 tree e = NULL_TREE;
37516 tree step_size = integer_one_node;
37518 /* If present, parse the linear step. Otherwise, assume the default
37519 value of 1. */
37520 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
37522 cp_lexer_consume_token (parser->lexer);
37524 e = cp_parser_assignment_expression (parser);
37525 e = maybe_constant_value (e);
37527 if (e == error_mark_node)
37529 /* If an error has occurred, then the whole pragma is
37530 considered ill-formed. Thus, no reason to keep
37531 parsing. */
37532 clauses = error_mark_node;
37533 break;
37535 else if (type_dependent_expression_p (e)
37536 || value_dependent_expression_p (e)
37537 || (TREE_TYPE (e)
37538 && INTEGRAL_TYPE_P (TREE_TYPE (e))
37539 && (TREE_CONSTANT (e)
37540 || DECL_P (e))))
37541 step_size = e;
37542 else
37543 cp_parser_error (parser,
37544 "step size must be an integer constant "
37545 "expression or an integer variable");
37548 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
37549 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
37550 OMP_CLAUSE_DECL (l) = decl;
37551 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
37552 OMP_CLAUSE_CHAIN (l) = clauses;
37553 clauses = l;
37555 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37556 cp_lexer_consume_token (parser->lexer);
37557 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37558 break;
37559 else
37561 error_at (cp_lexer_peek_token (parser->lexer)->location,
37562 "expected %<,%> or %<)%> after %qE", decl);
37563 clauses = error_mark_node;
37564 break;
37567 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37568 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
37569 return clauses;
37572 /* Returns the name of the next clause. If the clause is not
37573 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
37574 token is not consumed. Otherwise, the appropriate enum from the
37575 pragma_simd_clause is returned and the token is consumed. */
37577 static pragma_omp_clause
37578 cp_parser_cilk_simd_clause_name (cp_parser *parser)
37580 pragma_omp_clause clause_type;
37581 cp_token *token = cp_lexer_peek_token (parser->lexer);
37583 if (token->keyword == RID_PRIVATE)
37584 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
37585 else if (!token->u.value || token->type != CPP_NAME)
37586 return PRAGMA_CILK_CLAUSE_NONE;
37587 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
37588 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
37589 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
37590 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
37591 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
37592 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
37593 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
37594 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
37595 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
37596 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
37597 else
37598 return PRAGMA_CILK_CLAUSE_NONE;
37600 cp_lexer_consume_token (parser->lexer);
37601 return clause_type;
37604 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
37606 static tree
37607 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
37609 tree clauses = NULL_TREE;
37611 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37612 && clauses != error_mark_node)
37614 pragma_omp_clause c_kind;
37615 c_kind = cp_parser_cilk_simd_clause_name (parser);
37616 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
37617 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
37618 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
37619 clauses = cp_parser_cilk_simd_linear (parser, clauses);
37620 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
37621 /* Use the OpenMP 4.0 equivalent function. */
37622 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
37623 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
37624 /* Use the OpenMP 4.0 equivalent function. */
37625 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
37626 clauses);
37627 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
37628 /* Use the OMP 4.0 equivalent function. */
37629 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
37630 clauses);
37631 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
37632 /* Use the OMP 4.0 equivalent function. */
37633 clauses = cp_parser_omp_clause_reduction (parser, clauses);
37634 else
37636 clauses = error_mark_node;
37637 cp_parser_error (parser, "expected %<#pragma simd%> clause");
37638 break;
37642 cp_parser_skip_to_pragma_eol (parser, pragma_token);
37644 if (clauses == error_mark_node)
37645 return error_mark_node;
37646 else
37647 return c_finish_cilk_clauses (clauses);
37650 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
37652 static void
37653 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
37655 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
37657 if (clauses == error_mark_node)
37658 return;
37660 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
37662 error_at (cp_lexer_peek_token (parser->lexer)->location,
37663 "for statement expected");
37664 return;
37667 tree sb = begin_omp_structured_block ();
37668 int save = cp_parser_begin_omp_structured_block (parser);
37669 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
37670 if (ret)
37671 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
37672 cp_parser_end_omp_structured_block (parser, save);
37673 add_stmt (finish_omp_structured_block (sb));
37676 /* Main entry-point for parsing Cilk Plus _Cilk_for
37677 loops. The return value is error_mark_node
37678 when errors happen and CILK_FOR tree on success. */
37680 static tree
37681 cp_parser_cilk_for (cp_parser *parser, tree grain)
37683 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
37684 gcc_unreachable ();
37686 tree sb = begin_omp_structured_block ();
37687 int save = cp_parser_begin_omp_structured_block (parser);
37689 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
37690 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
37691 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
37692 clauses = finish_omp_clauses (clauses, false);
37694 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL);
37695 if (ret)
37696 cpp_validate_cilk_plus_loop (ret);
37697 else
37698 ret = error_mark_node;
37700 cp_parser_end_omp_structured_block (parser, save);
37701 add_stmt (finish_omp_structured_block (sb));
37702 return ret;
37705 /* Create an identifier for a generic parameter type (a synthesized
37706 template parameter implied by `auto' or a concept identifier). */
37708 static GTY(()) int generic_parm_count;
37709 static tree
37710 make_generic_type_name ()
37712 char buf[32];
37713 sprintf (buf, "auto:%d", ++generic_parm_count);
37714 return get_identifier (buf);
37717 /* Predicate that behaves as is_auto_or_concept but matches the parent
37718 node of the generic type rather than the generic type itself. This
37719 allows for type transformation in add_implicit_template_parms. */
37721 static inline bool
37722 tree_type_is_auto_or_concept (const_tree t)
37724 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
37727 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
37728 (creating a new template parameter list if necessary). Returns the newly
37729 created template type parm. */
37731 static tree
37732 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
37734 gcc_assert (current_binding_level->kind == sk_function_parms);
37736 /* Before committing to modifying any scope, if we're in an
37737 implicit template scope, and we're trying to synthesize a
37738 constrained parameter, try to find a previous parameter with
37739 the same name. This is the same-type rule for abbreviated
37740 function templates. */
37741 if (parser->implicit_template_scope && constr)
37743 tree t = parser->implicit_template_parms;
37744 while (t)
37746 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
37748 tree d = TREE_VALUE (t);
37749 if (TREE_CODE (d) == PARM_DECL)
37750 /* Return the TEMPLATE_PARM_INDEX. */
37751 d = DECL_INITIAL (d);
37752 return d;
37754 t = TREE_CHAIN (t);
37758 /* We are either continuing a function template that already contains implicit
37759 template parameters, creating a new fully-implicit function template, or
37760 extending an existing explicit function template with implicit template
37761 parameters. */
37763 cp_binding_level *const entry_scope = current_binding_level;
37765 bool become_template = false;
37766 cp_binding_level *parent_scope = 0;
37768 if (parser->implicit_template_scope)
37770 gcc_assert (parser->implicit_template_parms);
37772 current_binding_level = parser->implicit_template_scope;
37774 else
37776 /* Roll back to the existing template parameter scope (in the case of
37777 extending an explicit function template) or introduce a new template
37778 parameter scope ahead of the function parameter scope (or class scope
37779 in the case of out-of-line member definitions). The function scope is
37780 added back after template parameter synthesis below. */
37782 cp_binding_level *scope = entry_scope;
37784 while (scope->kind == sk_function_parms)
37786 parent_scope = scope;
37787 scope = scope->level_chain;
37789 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
37791 /* If not defining a class, then any class scope is a scope level in
37792 an out-of-line member definition. In this case simply wind back
37793 beyond the first such scope to inject the template parameter list.
37794 Otherwise wind back to the class being defined. The latter can
37795 occur in class member friend declarations such as:
37797 class A {
37798 void foo (auto);
37800 class B {
37801 friend void A::foo (auto);
37804 The template parameter list synthesized for the friend declaration
37805 must be injected in the scope of 'B'. This can also occur in
37806 erroneous cases such as:
37808 struct A {
37809 struct B {
37810 void foo (auto);
37812 void B::foo (auto) {}
37815 Here the attempted definition of 'B::foo' within 'A' is ill-formed
37816 but, nevertheless, the template parameter list synthesized for the
37817 declarator should be injected into the scope of 'A' as if the
37818 ill-formed template was specified explicitly. */
37820 while (scope->kind == sk_class && !scope->defining_class_p)
37822 parent_scope = scope;
37823 scope = scope->level_chain;
37827 current_binding_level = scope;
37829 if (scope->kind != sk_template_parms
37830 || !function_being_declared_is_template_p (parser))
37832 /* Introduce a new template parameter list for implicit template
37833 parameters. */
37835 become_template = true;
37837 parser->implicit_template_scope
37838 = begin_scope (sk_template_parms, NULL);
37840 ++processing_template_decl;
37842 parser->fully_implicit_function_template_p = true;
37843 ++parser->num_template_parameter_lists;
37845 else
37847 /* Synthesize implicit template parameters at the end of the explicit
37848 template parameter list. */
37850 gcc_assert (current_template_parms);
37852 parser->implicit_template_scope = scope;
37854 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
37855 parser->implicit_template_parms
37856 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
37860 /* Synthesize a new template parameter and track the current template
37861 parameter chain with implicit_template_parms. */
37863 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
37864 tree synth_id = make_generic_type_name ();
37865 tree synth_tmpl_parm;
37866 bool non_type = false;
37868 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
37869 synth_tmpl_parm
37870 = finish_template_type_parm (class_type_node, synth_id);
37871 else if (TREE_CODE (proto) == TEMPLATE_DECL)
37872 synth_tmpl_parm
37873 = finish_constrained_template_template_parm (proto, synth_id);
37874 else
37876 synth_tmpl_parm = copy_decl (proto);
37877 DECL_NAME (synth_tmpl_parm) = synth_id;
37878 non_type = true;
37881 // Attach the constraint to the parm before processing.
37882 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
37883 TREE_TYPE (node) = constr;
37884 tree new_parm
37885 = process_template_parm (parser->implicit_template_parms,
37886 input_location,
37887 node,
37888 /*non_type=*/non_type,
37889 /*param_pack=*/false);
37891 // Chain the new parameter to the list of implicit parameters.
37892 if (parser->implicit_template_parms)
37893 parser->implicit_template_parms
37894 = TREE_CHAIN (parser->implicit_template_parms);
37895 else
37896 parser->implicit_template_parms = new_parm;
37898 tree new_decl = getdecls ();
37899 if (non_type)
37900 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
37901 new_decl = DECL_INITIAL (new_decl);
37903 /* If creating a fully implicit function template, start the new implicit
37904 template parameter list with this synthesized type, otherwise grow the
37905 current template parameter list. */
37907 if (become_template)
37909 parent_scope->level_chain = current_binding_level;
37911 tree new_parms = make_tree_vec (1);
37912 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
37913 current_template_parms = tree_cons (size_int (processing_template_decl),
37914 new_parms, current_template_parms);
37916 else
37918 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
37919 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
37920 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
37921 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
37924 // If the new parameter was constrained, we need to add that to the
37925 // constraints in the template parameter list.
37926 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
37928 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
37929 reqs = conjoin_constraints (reqs, req);
37930 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
37933 current_binding_level = entry_scope;
37935 return new_decl;
37938 /* Finish the declaration of a fully implicit function template. Such a
37939 template has no explicit template parameter list so has not been through the
37940 normal template head and tail processing. synthesize_implicit_template_parm
37941 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
37942 provided if the declaration is a class member such that its template
37943 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
37944 form is returned. Otherwise NULL_TREE is returned. */
37946 static tree
37947 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
37949 gcc_assert (parser->fully_implicit_function_template_p);
37951 if (member_decl_opt && member_decl_opt != error_mark_node
37952 && DECL_VIRTUAL_P (member_decl_opt))
37954 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
37955 "implicit templates may not be %<virtual%>");
37956 DECL_VIRTUAL_P (member_decl_opt) = false;
37959 if (member_decl_opt)
37960 member_decl_opt = finish_member_template_decl (member_decl_opt);
37961 end_template_decl ();
37963 parser->fully_implicit_function_template_p = false;
37964 --parser->num_template_parameter_lists;
37966 return member_decl_opt;
37969 #include "gt-cp-parser.h"