Implement P0018R3, C++17 lambda capture of *this by value as [=,*this]
[official-gcc.git] / gcc / cp / parser.c
blobf672b8de18859f65ff60e4f8bc3e6ee68ce9cb4e
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"
42 #include "cp-cilkplus.h"
43 #include "gcc-rich-location.h"
44 #include "tree-iterator.h"
47 /* The lexer. */
49 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
50 and c-lex.c) and the C++ parser. */
52 static cp_token eof_token =
54 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
57 /* The various kinds of non integral constant we encounter. */
58 enum non_integral_constant {
59 NIC_NONE,
60 /* floating-point literal */
61 NIC_FLOAT,
62 /* %<this%> */
63 NIC_THIS,
64 /* %<__FUNCTION__%> */
65 NIC_FUNC_NAME,
66 /* %<__PRETTY_FUNCTION__%> */
67 NIC_PRETTY_FUNC,
68 /* %<__func__%> */
69 NIC_C99_FUNC,
70 /* "%<va_arg%> */
71 NIC_VA_ARG,
72 /* a cast */
73 NIC_CAST,
74 /* %<typeid%> operator */
75 NIC_TYPEID,
76 /* non-constant compound literals */
77 NIC_NCC,
78 /* a function call */
79 NIC_FUNC_CALL,
80 /* an increment */
81 NIC_INC,
82 /* an decrement */
83 NIC_DEC,
84 /* an array reference */
85 NIC_ARRAY_REF,
86 /* %<->%> */
87 NIC_ARROW,
88 /* %<.%> */
89 NIC_POINT,
90 /* the address of a label */
91 NIC_ADDR_LABEL,
92 /* %<*%> */
93 NIC_STAR,
94 /* %<&%> */
95 NIC_ADDR,
96 /* %<++%> */
97 NIC_PREINCREMENT,
98 /* %<--%> */
99 NIC_PREDECREMENT,
100 /* %<new%> */
101 NIC_NEW,
102 /* %<delete%> */
103 NIC_DEL,
104 /* calls to overloaded operators */
105 NIC_OVERLOADED,
106 /* an assignment */
107 NIC_ASSIGNMENT,
108 /* a comma operator */
109 NIC_COMMA,
110 /* a call to a constructor */
111 NIC_CONSTRUCTOR,
112 /* a transaction expression */
113 NIC_TRANSACTION
116 /* The various kinds of errors about name-lookup failing. */
117 enum name_lookup_error {
118 /* NULL */
119 NLE_NULL,
120 /* is not a type */
121 NLE_TYPE,
122 /* is not a class or namespace */
123 NLE_CXX98,
124 /* is not a class, namespace, or enumeration */
125 NLE_NOT_CXX98
128 /* The various kinds of required token */
129 enum required_token {
130 RT_NONE,
131 RT_SEMICOLON, /* ';' */
132 RT_OPEN_PAREN, /* '(' */
133 RT_CLOSE_BRACE, /* '}' */
134 RT_OPEN_BRACE, /* '{' */
135 RT_CLOSE_SQUARE, /* ']' */
136 RT_OPEN_SQUARE, /* '[' */
137 RT_COMMA, /* ',' */
138 RT_SCOPE, /* '::' */
139 RT_LESS, /* '<' */
140 RT_GREATER, /* '>' */
141 RT_EQ, /* '=' */
142 RT_ELLIPSIS, /* '...' */
143 RT_MULT, /* '*' */
144 RT_COMPL, /* '~' */
145 RT_COLON, /* ':' */
146 RT_COLON_SCOPE, /* ':' or '::' */
147 RT_CLOSE_PAREN, /* ')' */
148 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
149 RT_PRAGMA_EOL, /* end of line */
150 RT_NAME, /* identifier */
152 /* The type is CPP_KEYWORD */
153 RT_NEW, /* new */
154 RT_DELETE, /* delete */
155 RT_RETURN, /* return */
156 RT_WHILE, /* while */
157 RT_EXTERN, /* extern */
158 RT_STATIC_ASSERT, /* static_assert */
159 RT_DECLTYPE, /* decltype */
160 RT_OPERATOR, /* operator */
161 RT_CLASS, /* class */
162 RT_TEMPLATE, /* template */
163 RT_NAMESPACE, /* namespace */
164 RT_USING, /* using */
165 RT_ASM, /* asm */
166 RT_TRY, /* try */
167 RT_CATCH, /* catch */
168 RT_THROW, /* throw */
169 RT_LABEL, /* __label__ */
170 RT_AT_TRY, /* @try */
171 RT_AT_SYNCHRONIZED, /* @synchronized */
172 RT_AT_THROW, /* @throw */
174 RT_SELECT, /* selection-statement */
175 RT_INTERATION, /* iteration-statement */
176 RT_JUMP, /* jump-statement */
177 RT_CLASS_KEY, /* class-key */
178 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
179 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
180 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
181 RT_TRANSACTION_CANCEL /* __transaction_cancel */
184 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
185 reverting it on destruction. */
187 class type_id_in_expr_sentinel
189 cp_parser *parser;
190 bool saved;
191 public:
192 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
193 : parser (parser),
194 saved (parser->in_type_id_in_expr_p)
195 { parser->in_type_id_in_expr_p = set; }
196 ~type_id_in_expr_sentinel ()
197 { parser->in_type_id_in_expr_p = saved; }
200 /* Prototypes. */
202 static cp_lexer *cp_lexer_new_main
203 (void);
204 static cp_lexer *cp_lexer_new_from_tokens
205 (cp_token_cache *tokens);
206 static void cp_lexer_destroy
207 (cp_lexer *);
208 static int cp_lexer_saving_tokens
209 (const cp_lexer *);
210 static cp_token *cp_lexer_token_at
211 (cp_lexer *, cp_token_position);
212 static void cp_lexer_get_preprocessor_token
213 (cp_lexer *, cp_token *);
214 static inline cp_token *cp_lexer_peek_token
215 (cp_lexer *);
216 static cp_token *cp_lexer_peek_nth_token
217 (cp_lexer *, size_t);
218 static inline bool cp_lexer_next_token_is
219 (cp_lexer *, enum cpp_ttype);
220 static bool cp_lexer_next_token_is_not
221 (cp_lexer *, enum cpp_ttype);
222 static bool cp_lexer_next_token_is_keyword
223 (cp_lexer *, enum rid);
224 static cp_token *cp_lexer_consume_token
225 (cp_lexer *);
226 static void cp_lexer_purge_token
227 (cp_lexer *);
228 static void cp_lexer_purge_tokens_after
229 (cp_lexer *, cp_token_position);
230 static void cp_lexer_save_tokens
231 (cp_lexer *);
232 static void cp_lexer_commit_tokens
233 (cp_lexer *);
234 static void cp_lexer_rollback_tokens
235 (cp_lexer *);
236 static void cp_lexer_print_token
237 (FILE *, cp_token *);
238 static inline bool cp_lexer_debugging_p
239 (cp_lexer *);
240 static void cp_lexer_start_debugging
241 (cp_lexer *) ATTRIBUTE_UNUSED;
242 static void cp_lexer_stop_debugging
243 (cp_lexer *) ATTRIBUTE_UNUSED;
245 static cp_token_cache *cp_token_cache_new
246 (cp_token *, cp_token *);
248 static void cp_parser_initial_pragma
249 (cp_token *);
251 static tree cp_literal_operator_id
252 (const char *);
254 static void cp_parser_cilk_simd
255 (cp_parser *, cp_token *, bool *);
256 static tree cp_parser_cilk_for
257 (cp_parser *, tree, bool *);
258 static bool cp_parser_omp_declare_reduction_exprs
259 (tree, cp_parser *);
260 static tree cp_parser_cilk_simd_vectorlength
261 (cp_parser *, tree, bool);
262 static void cp_finalize_oacc_routine
263 (cp_parser *, tree, bool);
265 /* Manifest constants. */
266 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
267 #define CP_SAVED_TOKEN_STACK 5
269 /* Variables. */
271 /* The stream to which debugging output should be written. */
272 static FILE *cp_lexer_debug_stream;
274 /* Nonzero if we are parsing an unevaluated operand: an operand to
275 sizeof, typeof, or alignof. */
276 int cp_unevaluated_operand;
278 /* Dump up to NUM tokens in BUFFER to FILE starting with token
279 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
280 first token in BUFFER. If NUM is 0, dump all the tokens. If
281 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
282 highlighted by surrounding it in [[ ]]. */
284 static void
285 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
286 cp_token *start_token, unsigned num,
287 cp_token *curr_token)
289 unsigned i, nprinted;
290 cp_token *token;
291 bool do_print;
293 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
295 if (buffer == NULL)
296 return;
298 if (num == 0)
299 num = buffer->length ();
301 if (start_token == NULL)
302 start_token = buffer->address ();
304 if (start_token > buffer->address ())
306 cp_lexer_print_token (file, &(*buffer)[0]);
307 fprintf (file, " ... ");
310 do_print = false;
311 nprinted = 0;
312 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
314 if (token == start_token)
315 do_print = true;
317 if (!do_print)
318 continue;
320 nprinted++;
321 if (token == curr_token)
322 fprintf (file, "[[");
324 cp_lexer_print_token (file, token);
326 if (token == curr_token)
327 fprintf (file, "]]");
329 switch (token->type)
331 case CPP_SEMICOLON:
332 case CPP_OPEN_BRACE:
333 case CPP_CLOSE_BRACE:
334 case CPP_EOF:
335 fputc ('\n', file);
336 break;
338 default:
339 fputc (' ', file);
343 if (i == num && i < buffer->length ())
345 fprintf (file, " ... ");
346 cp_lexer_print_token (file, &buffer->last ());
349 fprintf (file, "\n");
353 /* Dump all tokens in BUFFER to stderr. */
355 void
356 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
358 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
361 DEBUG_FUNCTION void
362 debug (vec<cp_token, va_gc> &ref)
364 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
367 DEBUG_FUNCTION void
368 debug (vec<cp_token, va_gc> *ptr)
370 if (ptr)
371 debug (*ptr);
372 else
373 fprintf (stderr, "<nil>\n");
377 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
378 description for T. */
380 static void
381 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
383 if (t)
385 fprintf (file, "%s: ", desc);
386 print_node_brief (file, "", t, 0);
391 /* Dump parser context C to FILE. */
393 static void
394 cp_debug_print_context (FILE *file, cp_parser_context *c)
396 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
397 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
398 print_node_brief (file, "", c->object_type, 0);
399 fprintf (file, "}\n");
403 /* Print the stack of parsing contexts to FILE starting with FIRST. */
405 static void
406 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
408 unsigned i;
409 cp_parser_context *c;
411 fprintf (file, "Parsing context stack:\n");
412 for (i = 0, c = first; c; c = c->next, i++)
414 fprintf (file, "\t#%u: ", i);
415 cp_debug_print_context (file, c);
420 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
422 static void
423 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
425 if (flag)
426 fprintf (file, "%s: true\n", desc);
430 /* Print an unparsed function entry UF to FILE. */
432 static void
433 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
435 unsigned i;
436 cp_default_arg_entry *default_arg_fn;
437 tree fn;
439 fprintf (file, "\tFunctions with default args:\n");
440 for (i = 0;
441 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
442 i++)
444 fprintf (file, "\t\tClass type: ");
445 print_node_brief (file, "", default_arg_fn->class_type, 0);
446 fprintf (file, "\t\tDeclaration: ");
447 print_node_brief (file, "", default_arg_fn->decl, 0);
448 fprintf (file, "\n");
451 fprintf (file, "\n\tFunctions with definitions that require "
452 "post-processing\n\t\t");
453 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
455 print_node_brief (file, "", fn, 0);
456 fprintf (file, " ");
458 fprintf (file, "\n");
460 fprintf (file, "\n\tNon-static data members with initializers that require "
461 "post-processing\n\t\t");
462 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
464 print_node_brief (file, "", fn, 0);
465 fprintf (file, " ");
467 fprintf (file, "\n");
471 /* Print the stack of unparsed member functions S to FILE. */
473 static void
474 cp_debug_print_unparsed_queues (FILE *file,
475 vec<cp_unparsed_functions_entry, va_gc> *s)
477 unsigned i;
478 cp_unparsed_functions_entry *uf;
480 fprintf (file, "Unparsed functions\n");
481 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
483 fprintf (file, "#%u:\n", i);
484 cp_debug_print_unparsed_function (file, uf);
489 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
490 the given PARSER. If FILE is NULL, the output is printed on stderr. */
492 static void
493 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
495 cp_token *next_token, *first_token, *start_token;
497 if (file == NULL)
498 file = stderr;
500 next_token = parser->lexer->next_token;
501 first_token = parser->lexer->buffer->address ();
502 start_token = (next_token > first_token + window_size / 2)
503 ? next_token - window_size / 2
504 : first_token;
505 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
506 next_token);
510 /* Dump debugging information for the given PARSER. If FILE is NULL,
511 the output is printed on stderr. */
513 void
514 cp_debug_parser (FILE *file, cp_parser *parser)
516 const size_t window_size = 20;
517 cp_token *token;
518 expanded_location eloc;
520 if (file == NULL)
521 file = stderr;
523 fprintf (file, "Parser state\n\n");
524 fprintf (file, "Number of tokens: %u\n",
525 vec_safe_length (parser->lexer->buffer));
526 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
527 cp_debug_print_tree_if_set (file, "Object scope",
528 parser->object_scope);
529 cp_debug_print_tree_if_set (file, "Qualifying scope",
530 parser->qualifying_scope);
531 cp_debug_print_context_stack (file, parser->context);
532 cp_debug_print_flag (file, "Allow GNU extensions",
533 parser->allow_gnu_extensions_p);
534 cp_debug_print_flag (file, "'>' token is greater-than",
535 parser->greater_than_is_operator_p);
536 cp_debug_print_flag (file, "Default args allowed in current "
537 "parameter list", parser->default_arg_ok_p);
538 cp_debug_print_flag (file, "Parsing integral constant-expression",
539 parser->integral_constant_expression_p);
540 cp_debug_print_flag (file, "Allow non-constant expression in current "
541 "constant-expression",
542 parser->allow_non_integral_constant_expression_p);
543 cp_debug_print_flag (file, "Seen non-constant expression",
544 parser->non_integral_constant_expression_p);
545 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
546 "current context",
547 parser->local_variables_forbidden_p);
548 cp_debug_print_flag (file, "In unbraced linkage specification",
549 parser->in_unbraced_linkage_specification_p);
550 cp_debug_print_flag (file, "Parsing a declarator",
551 parser->in_declarator_p);
552 cp_debug_print_flag (file, "In template argument list",
553 parser->in_template_argument_list_p);
554 cp_debug_print_flag (file, "Parsing an iteration statement",
555 parser->in_statement & IN_ITERATION_STMT);
556 cp_debug_print_flag (file, "Parsing a switch statement",
557 parser->in_statement & IN_SWITCH_STMT);
558 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
559 parser->in_statement & IN_OMP_BLOCK);
560 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
561 parser->in_statement & IN_CILK_SIMD_FOR);
562 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
563 parser->in_statement & IN_OMP_FOR);
564 cp_debug_print_flag (file, "Parsing an if statement",
565 parser->in_statement & IN_IF_STMT);
566 cp_debug_print_flag (file, "Parsing a type-id in an expression "
567 "context", parser->in_type_id_in_expr_p);
568 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
569 parser->implicit_extern_c);
570 cp_debug_print_flag (file, "String expressions should be translated "
571 "to execution character set",
572 parser->translate_strings_p);
573 cp_debug_print_flag (file, "Parsing function body outside of a "
574 "local class", parser->in_function_body);
575 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
576 parser->colon_corrects_to_scope_p);
577 cp_debug_print_flag (file, "Colon doesn't start a class definition",
578 parser->colon_doesnt_start_class_def_p);
579 if (parser->type_definition_forbidden_message)
580 fprintf (file, "Error message for forbidden type definitions: %s\n",
581 parser->type_definition_forbidden_message);
582 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
583 fprintf (file, "Number of class definitions in progress: %u\n",
584 parser->num_classes_being_defined);
585 fprintf (file, "Number of template parameter lists for the current "
586 "declaration: %u\n", parser->num_template_parameter_lists);
587 cp_debug_parser_tokens (file, parser, window_size);
588 token = parser->lexer->next_token;
589 fprintf (file, "Next token to parse:\n");
590 fprintf (file, "\tToken: ");
591 cp_lexer_print_token (file, token);
592 eloc = expand_location (token->location);
593 fprintf (file, "\n\tFile: %s\n", eloc.file);
594 fprintf (file, "\tLine: %d\n", eloc.line);
595 fprintf (file, "\tColumn: %d\n", eloc.column);
598 DEBUG_FUNCTION void
599 debug (cp_parser &ref)
601 cp_debug_parser (stderr, &ref);
604 DEBUG_FUNCTION void
605 debug (cp_parser *ptr)
607 if (ptr)
608 debug (*ptr);
609 else
610 fprintf (stderr, "<nil>\n");
613 /* Allocate memory for a new lexer object and return it. */
615 static cp_lexer *
616 cp_lexer_alloc (void)
618 cp_lexer *lexer;
620 c_common_no_more_pch ();
622 /* Allocate the memory. */
623 lexer = ggc_cleared_alloc<cp_lexer> ();
625 /* Initially we are not debugging. */
626 lexer->debugging_p = false;
628 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
630 /* Create the buffer. */
631 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
633 return lexer;
637 /* Create a new main C++ lexer, the lexer that gets tokens from the
638 preprocessor. */
640 static cp_lexer *
641 cp_lexer_new_main (void)
643 cp_lexer *lexer;
644 cp_token token;
646 /* It's possible that parsing the first pragma will load a PCH file,
647 which is a GC collection point. So we have to do that before
648 allocating any memory. */
649 cp_parser_initial_pragma (&token);
651 lexer = cp_lexer_alloc ();
653 /* Put the first token in the buffer. */
654 lexer->buffer->quick_push (token);
656 /* Get the remaining tokens from the preprocessor. */
657 while (token.type != CPP_EOF)
659 cp_lexer_get_preprocessor_token (lexer, &token);
660 vec_safe_push (lexer->buffer, token);
663 lexer->last_token = lexer->buffer->address ()
664 + lexer->buffer->length ()
665 - 1;
666 lexer->next_token = lexer->buffer->length ()
667 ? lexer->buffer->address ()
668 : &eof_token;
670 /* Subsequent preprocessor diagnostics should use compiler
671 diagnostic functions to get the compiler source location. */
672 done_lexing = true;
674 gcc_assert (!lexer->next_token->purged_p);
675 return lexer;
678 /* Create a new lexer whose token stream is primed with the tokens in
679 CACHE. When these tokens are exhausted, no new tokens will be read. */
681 static cp_lexer *
682 cp_lexer_new_from_tokens (cp_token_cache *cache)
684 cp_token *first = cache->first;
685 cp_token *last = cache->last;
686 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
688 /* We do not own the buffer. */
689 lexer->buffer = NULL;
690 lexer->next_token = first == last ? &eof_token : first;
691 lexer->last_token = last;
693 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
695 /* Initially we are not debugging. */
696 lexer->debugging_p = false;
698 gcc_assert (!lexer->next_token->purged_p);
699 return lexer;
702 /* Frees all resources associated with LEXER. */
704 static void
705 cp_lexer_destroy (cp_lexer *lexer)
707 vec_free (lexer->buffer);
708 lexer->saved_tokens.release ();
709 ggc_free (lexer);
712 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
713 be used. The point of this flag is to help the compiler to fold away calls
714 to cp_lexer_debugging_p within this source file at compile time, when the
715 lexer is not being debugged. */
717 #define LEXER_DEBUGGING_ENABLED_P false
719 /* Returns nonzero if debugging information should be output. */
721 static inline bool
722 cp_lexer_debugging_p (cp_lexer *lexer)
724 if (!LEXER_DEBUGGING_ENABLED_P)
725 return false;
727 return lexer->debugging_p;
731 static inline cp_token_position
732 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
734 gcc_assert (!previous_p || lexer->next_token != &eof_token);
736 return lexer->next_token - previous_p;
739 static inline cp_token *
740 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
742 return pos;
745 static inline void
746 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
748 lexer->next_token = cp_lexer_token_at (lexer, pos);
751 static inline cp_token_position
752 cp_lexer_previous_token_position (cp_lexer *lexer)
754 if (lexer->next_token == &eof_token)
755 return lexer->last_token - 1;
756 else
757 return cp_lexer_token_position (lexer, true);
760 static inline cp_token *
761 cp_lexer_previous_token (cp_lexer *lexer)
763 cp_token_position tp = cp_lexer_previous_token_position (lexer);
765 /* Skip past purged tokens. */
766 while (tp->purged_p)
768 gcc_assert (tp != lexer->buffer->address ());
769 tp--;
772 return cp_lexer_token_at (lexer, tp);
775 /* nonzero if we are presently saving tokens. */
777 static inline int
778 cp_lexer_saving_tokens (const cp_lexer* lexer)
780 return lexer->saved_tokens.length () != 0;
783 /* Store the next token from the preprocessor in *TOKEN. Return true
784 if we reach EOF. If LEXER is NULL, assume we are handling an
785 initial #pragma pch_preprocess, and thus want the lexer to return
786 processed strings. */
788 static void
789 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
791 static int is_extern_c = 0;
793 /* Get a new token from the preprocessor. */
794 token->type
795 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
796 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
797 token->keyword = RID_MAX;
798 token->purged_p = false;
799 token->error_reported = false;
801 /* On some systems, some header files are surrounded by an
802 implicit extern "C" block. Set a flag in the token if it
803 comes from such a header. */
804 is_extern_c += pending_lang_change;
805 pending_lang_change = 0;
806 token->implicit_extern_c = is_extern_c > 0;
808 /* Check to see if this token is a keyword. */
809 if (token->type == CPP_NAME)
811 if (C_IS_RESERVED_WORD (token->u.value))
813 /* Mark this token as a keyword. */
814 token->type = CPP_KEYWORD;
815 /* Record which keyword. */
816 token->keyword = C_RID_CODE (token->u.value);
818 else
820 if (warn_cxx11_compat
821 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
822 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
824 /* Warn about the C++0x keyword (but still treat it as
825 an identifier). */
826 warning (OPT_Wc__11_compat,
827 "identifier %qE is a keyword in C++11",
828 token->u.value);
830 /* Clear out the C_RID_CODE so we don't warn about this
831 particular identifier-turned-keyword again. */
832 C_SET_RID_CODE (token->u.value, RID_MAX);
835 token->keyword = RID_MAX;
838 else if (token->type == CPP_AT_NAME)
840 /* This only happens in Objective-C++; it must be a keyword. */
841 token->type = CPP_KEYWORD;
842 switch (C_RID_CODE (token->u.value))
844 /* Replace 'class' with '@class', 'private' with '@private',
845 etc. This prevents confusion with the C++ keyword
846 'class', and makes the tokens consistent with other
847 Objective-C 'AT' keywords. For example '@class' is
848 reported as RID_AT_CLASS which is consistent with
849 '@synchronized', which is reported as
850 RID_AT_SYNCHRONIZED.
852 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
853 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
854 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
855 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
856 case RID_THROW: token->keyword = RID_AT_THROW; break;
857 case RID_TRY: token->keyword = RID_AT_TRY; break;
858 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
859 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
860 default: token->keyword = C_RID_CODE (token->u.value);
865 /* Update the globals input_location and the input file stack from TOKEN. */
866 static inline void
867 cp_lexer_set_source_position_from_token (cp_token *token)
869 if (token->type != CPP_EOF)
871 input_location = token->location;
875 /* Update the globals input_location and the input file stack from LEXER. */
876 static inline void
877 cp_lexer_set_source_position (cp_lexer *lexer)
879 cp_token *token = cp_lexer_peek_token (lexer);
880 cp_lexer_set_source_position_from_token (token);
883 /* Return a pointer to the next token in the token stream, but do not
884 consume it. */
886 static inline cp_token *
887 cp_lexer_peek_token (cp_lexer *lexer)
889 if (cp_lexer_debugging_p (lexer))
891 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
892 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
893 putc ('\n', cp_lexer_debug_stream);
895 return lexer->next_token;
898 /* Return true if the next token has the indicated TYPE. */
900 static inline bool
901 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
903 return cp_lexer_peek_token (lexer)->type == type;
906 /* Return true if the next token does not have the indicated TYPE. */
908 static inline bool
909 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
911 return !cp_lexer_next_token_is (lexer, type);
914 /* Return true if the next token is the indicated KEYWORD. */
916 static inline bool
917 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
919 return cp_lexer_peek_token (lexer)->keyword == keyword;
922 static inline bool
923 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
925 return cp_lexer_peek_nth_token (lexer, n)->type == type;
928 static inline bool
929 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
931 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
934 /* Return true if the next token is not the indicated KEYWORD. */
936 static inline bool
937 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
939 return cp_lexer_peek_token (lexer)->keyword != keyword;
942 /* Return true if KEYWORD can start a decl-specifier. */
944 bool
945 cp_keyword_starts_decl_specifier_p (enum rid keyword)
947 switch (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 case RID_CONSTEXPR:
985 return true;
987 default:
988 if (keyword >= RID_FIRST_INT_N
989 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
990 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
991 return true;
992 return false;
996 /* Return true if the next token is a keyword for a decl-specifier. */
998 static bool
999 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
1001 cp_token *token;
1003 token = cp_lexer_peek_token (lexer);
1004 return cp_keyword_starts_decl_specifier_p (token->keyword);
1007 /* Returns TRUE iff the token T begins a decltype type. */
1009 static bool
1010 token_is_decltype (cp_token *t)
1012 return (t->keyword == RID_DECLTYPE
1013 || t->type == CPP_DECLTYPE);
1016 /* Returns TRUE iff the next token begins a decltype type. */
1018 static bool
1019 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1021 cp_token *t = cp_lexer_peek_token (lexer);
1022 return token_is_decltype (t);
1025 /* Called when processing a token with tree_check_value; perform or defer the
1026 associated checks and return the value. */
1028 static tree
1029 saved_checks_value (struct tree_check *check_value)
1031 /* Perform any access checks that were deferred. */
1032 vec<deferred_access_check, va_gc> *checks;
1033 deferred_access_check *chk;
1034 checks = check_value->checks;
1035 if (checks)
1037 int i;
1038 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1039 perform_or_defer_access_check (chk->binfo,
1040 chk->decl,
1041 chk->diag_decl, tf_warning_or_error);
1043 /* Return the stored value. */
1044 return check_value->value;
1047 /* Return a pointer to the Nth token in the token stream. If N is 1,
1048 then this is precisely equivalent to cp_lexer_peek_token (except
1049 that it is not inline). One would like to disallow that case, but
1050 there is one case (cp_parser_nth_token_starts_template_id) where
1051 the caller passes a variable for N and it might be 1. */
1053 static cp_token *
1054 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1056 cp_token *token;
1058 /* N is 1-based, not zero-based. */
1059 gcc_assert (n > 0);
1061 if (cp_lexer_debugging_p (lexer))
1062 fprintf (cp_lexer_debug_stream,
1063 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1065 --n;
1066 token = lexer->next_token;
1067 gcc_assert (!n || token != &eof_token);
1068 while (n != 0)
1070 ++token;
1071 if (token == lexer->last_token)
1073 token = &eof_token;
1074 break;
1077 if (!token->purged_p)
1078 --n;
1081 if (cp_lexer_debugging_p (lexer))
1083 cp_lexer_print_token (cp_lexer_debug_stream, token);
1084 putc ('\n', cp_lexer_debug_stream);
1087 return token;
1090 /* Return the next token, and advance the lexer's next_token pointer
1091 to point to the next non-purged token. */
1093 static cp_token *
1094 cp_lexer_consume_token (cp_lexer* lexer)
1096 cp_token *token = lexer->next_token;
1098 gcc_assert (token != &eof_token);
1099 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1103 lexer->next_token++;
1104 if (lexer->next_token == lexer->last_token)
1106 lexer->next_token = &eof_token;
1107 break;
1111 while (lexer->next_token->purged_p);
1113 cp_lexer_set_source_position_from_token (token);
1115 /* Provide debugging output. */
1116 if (cp_lexer_debugging_p (lexer))
1118 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1119 cp_lexer_print_token (cp_lexer_debug_stream, token);
1120 putc ('\n', cp_lexer_debug_stream);
1123 return token;
1126 /* Permanently remove the next token from the token stream, and
1127 advance the next_token pointer to refer to the next non-purged
1128 token. */
1130 static void
1131 cp_lexer_purge_token (cp_lexer *lexer)
1133 cp_token *tok = lexer->next_token;
1135 gcc_assert (tok != &eof_token);
1136 tok->purged_p = true;
1137 tok->location = UNKNOWN_LOCATION;
1138 tok->u.value = NULL_TREE;
1139 tok->keyword = RID_MAX;
1143 tok++;
1144 if (tok == lexer->last_token)
1146 tok = &eof_token;
1147 break;
1150 while (tok->purged_p);
1151 lexer->next_token = tok;
1154 /* Permanently remove all tokens after TOK, up to, but not
1155 including, the token that will be returned next by
1156 cp_lexer_peek_token. */
1158 static void
1159 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1161 cp_token *peek = lexer->next_token;
1163 if (peek == &eof_token)
1164 peek = lexer->last_token;
1166 gcc_assert (tok < peek);
1168 for ( tok += 1; tok != peek; tok += 1)
1170 tok->purged_p = true;
1171 tok->location = UNKNOWN_LOCATION;
1172 tok->u.value = NULL_TREE;
1173 tok->keyword = RID_MAX;
1177 /* Begin saving tokens. All tokens consumed after this point will be
1178 preserved. */
1180 static void
1181 cp_lexer_save_tokens (cp_lexer* lexer)
1183 /* Provide debugging output. */
1184 if (cp_lexer_debugging_p (lexer))
1185 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1187 lexer->saved_tokens.safe_push (lexer->next_token);
1190 /* Commit to the portion of the token stream most recently saved. */
1192 static void
1193 cp_lexer_commit_tokens (cp_lexer* lexer)
1195 /* Provide debugging output. */
1196 if (cp_lexer_debugging_p (lexer))
1197 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1199 lexer->saved_tokens.pop ();
1202 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1203 to the token stream. Stop saving tokens. */
1205 static void
1206 cp_lexer_rollback_tokens (cp_lexer* lexer)
1208 /* Provide debugging output. */
1209 if (cp_lexer_debugging_p (lexer))
1210 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1212 lexer->next_token = lexer->saved_tokens.pop ();
1215 /* RAII wrapper around the above functions, with sanity checking. Creating
1216 a variable saves tokens, which are committed when the variable is
1217 destroyed unless they are explicitly rolled back by calling the rollback
1218 member function. */
1220 struct saved_token_sentinel
1222 cp_lexer *lexer;
1223 unsigned len;
1224 bool commit;
1225 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1227 len = lexer->saved_tokens.length ();
1228 cp_lexer_save_tokens (lexer);
1230 void rollback ()
1232 cp_lexer_rollback_tokens (lexer);
1233 commit = false;
1235 ~saved_token_sentinel()
1237 if (commit)
1238 cp_lexer_commit_tokens (lexer);
1239 gcc_assert (lexer->saved_tokens.length () == len);
1243 /* Print a representation of the TOKEN on the STREAM. */
1245 static void
1246 cp_lexer_print_token (FILE * stream, cp_token *token)
1248 /* We don't use cpp_type2name here because the parser defines
1249 a few tokens of its own. */
1250 static const char *const token_names[] = {
1251 /* cpplib-defined token types */
1252 #define OP(e, s) #e,
1253 #define TK(e, s) #e,
1254 TTYPE_TABLE
1255 #undef OP
1256 #undef TK
1257 /* C++ parser token types - see "Manifest constants", above. */
1258 "KEYWORD",
1259 "TEMPLATE_ID",
1260 "NESTED_NAME_SPECIFIER",
1263 /* For some tokens, print the associated data. */
1264 switch (token->type)
1266 case CPP_KEYWORD:
1267 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1268 For example, `struct' is mapped to an INTEGER_CST. */
1269 if (!identifier_p (token->u.value))
1270 break;
1271 /* fall through */
1272 case CPP_NAME:
1273 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1274 break;
1276 case CPP_STRING:
1277 case CPP_STRING16:
1278 case CPP_STRING32:
1279 case CPP_WSTRING:
1280 case CPP_UTF8STRING:
1281 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1282 break;
1284 case CPP_NUMBER:
1285 print_generic_expr (stream, token->u.value, 0);
1286 break;
1288 default:
1289 /* If we have a name for the token, print it out. Otherwise, we
1290 simply give the numeric code. */
1291 if (token->type < ARRAY_SIZE(token_names))
1292 fputs (token_names[token->type], stream);
1293 else
1294 fprintf (stream, "[%d]", token->type);
1295 break;
1299 DEBUG_FUNCTION void
1300 debug (cp_token &ref)
1302 cp_lexer_print_token (stderr, &ref);
1303 fprintf (stderr, "\n");
1306 DEBUG_FUNCTION void
1307 debug (cp_token *ptr)
1309 if (ptr)
1310 debug (*ptr);
1311 else
1312 fprintf (stderr, "<nil>\n");
1316 /* Start emitting debugging information. */
1318 static void
1319 cp_lexer_start_debugging (cp_lexer* lexer)
1321 if (!LEXER_DEBUGGING_ENABLED_P)
1322 fatal_error (input_location,
1323 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1325 lexer->debugging_p = true;
1326 cp_lexer_debug_stream = stderr;
1329 /* Stop emitting debugging information. */
1331 static void
1332 cp_lexer_stop_debugging (cp_lexer* lexer)
1334 if (!LEXER_DEBUGGING_ENABLED_P)
1335 fatal_error (input_location,
1336 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1338 lexer->debugging_p = false;
1339 cp_lexer_debug_stream = NULL;
1342 /* Create a new cp_token_cache, representing a range of tokens. */
1344 static cp_token_cache *
1345 cp_token_cache_new (cp_token *first, cp_token *last)
1347 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1348 cache->first = first;
1349 cache->last = last;
1350 return cache;
1353 /* Diagnose if #pragma omp declare simd isn't followed immediately
1354 by function declaration or definition. */
1356 static inline void
1357 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1359 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1361 error ("%<#pragma omp declare simd%> not immediately followed by "
1362 "function declaration or definition");
1363 parser->omp_declare_simd = NULL;
1367 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1368 and put that into "omp declare simd" attribute. */
1370 static inline void
1371 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1373 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1375 if (fndecl == error_mark_node)
1377 parser->omp_declare_simd = NULL;
1378 return;
1380 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1382 cp_ensure_no_omp_declare_simd (parser);
1383 return;
1388 /* Diagnose if #pragma acc routine isn't followed immediately by function
1389 declaration or definition. */
1391 static inline void
1392 cp_ensure_no_oacc_routine (cp_parser *parser)
1394 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1396 error_at (parser->oacc_routine->loc,
1397 "%<#pragma acc routine%> not immediately followed by "
1398 "function declaration or definition");
1399 parser->oacc_routine = NULL;
1403 /* Decl-specifiers. */
1405 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1407 static void
1408 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1410 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1413 /* Declarators. */
1415 /* Nothing other than the parser should be creating declarators;
1416 declarators are a semi-syntactic representation of C++ entities.
1417 Other parts of the front end that need to create entities (like
1418 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1420 static cp_declarator *make_call_declarator
1421 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1422 static cp_declarator *make_array_declarator
1423 (cp_declarator *, tree);
1424 static cp_declarator *make_pointer_declarator
1425 (cp_cv_quals, cp_declarator *, tree);
1426 static cp_declarator *make_reference_declarator
1427 (cp_cv_quals, cp_declarator *, bool, tree);
1428 static cp_declarator *make_ptrmem_declarator
1429 (cp_cv_quals, tree, cp_declarator *, tree);
1431 /* An erroneous declarator. */
1432 static cp_declarator *cp_error_declarator;
1434 /* The obstack on which declarators and related data structures are
1435 allocated. */
1436 static struct obstack declarator_obstack;
1438 /* Alloc BYTES from the declarator memory pool. */
1440 static inline void *
1441 alloc_declarator (size_t bytes)
1443 return obstack_alloc (&declarator_obstack, bytes);
1446 /* Allocate a declarator of the indicated KIND. Clear fields that are
1447 common to all declarators. */
1449 static cp_declarator *
1450 make_declarator (cp_declarator_kind kind)
1452 cp_declarator *declarator;
1454 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1455 declarator->kind = kind;
1456 declarator->attributes = NULL_TREE;
1457 declarator->std_attributes = NULL_TREE;
1458 declarator->declarator = NULL;
1459 declarator->parameter_pack_p = false;
1460 declarator->id_loc = UNKNOWN_LOCATION;
1462 return declarator;
1465 /* Make a declarator for a generalized identifier. If
1466 QUALIFYING_SCOPE is non-NULL, the identifier is
1467 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1468 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1469 is, if any. */
1471 static cp_declarator *
1472 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1473 special_function_kind sfk)
1475 cp_declarator *declarator;
1477 /* It is valid to write:
1479 class C { void f(); };
1480 typedef C D;
1481 void D::f();
1483 The standard is not clear about whether `typedef const C D' is
1484 legal; as of 2002-09-15 the committee is considering that
1485 question. EDG 3.0 allows that syntax. Therefore, we do as
1486 well. */
1487 if (qualifying_scope && TYPE_P (qualifying_scope))
1488 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1490 gcc_assert (identifier_p (unqualified_name)
1491 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1492 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1494 declarator = make_declarator (cdk_id);
1495 declarator->u.id.qualifying_scope = qualifying_scope;
1496 declarator->u.id.unqualified_name = unqualified_name;
1497 declarator->u.id.sfk = sfk;
1499 return declarator;
1502 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1503 of modifiers such as const or volatile to apply to the pointer
1504 type, represented as identifiers. ATTRIBUTES represent the attributes that
1505 appertain to the pointer or reference. */
1507 cp_declarator *
1508 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1509 tree attributes)
1511 cp_declarator *declarator;
1513 declarator = make_declarator (cdk_pointer);
1514 declarator->declarator = target;
1515 declarator->u.pointer.qualifiers = cv_qualifiers;
1516 declarator->u.pointer.class_type = NULL_TREE;
1517 if (target)
1519 declarator->id_loc = target->id_loc;
1520 declarator->parameter_pack_p = target->parameter_pack_p;
1521 target->parameter_pack_p = false;
1523 else
1524 declarator->parameter_pack_p = false;
1526 declarator->std_attributes = attributes;
1528 return declarator;
1531 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1532 represent the attributes that appertain to the pointer or
1533 reference. */
1535 cp_declarator *
1536 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1537 bool rvalue_ref, tree attributes)
1539 cp_declarator *declarator;
1541 declarator = make_declarator (cdk_reference);
1542 declarator->declarator = target;
1543 declarator->u.reference.qualifiers = cv_qualifiers;
1544 declarator->u.reference.rvalue_ref = rvalue_ref;
1545 if (target)
1547 declarator->id_loc = target->id_loc;
1548 declarator->parameter_pack_p = target->parameter_pack_p;
1549 target->parameter_pack_p = false;
1551 else
1552 declarator->parameter_pack_p = false;
1554 declarator->std_attributes = attributes;
1556 return declarator;
1559 /* Like make_pointer_declarator -- but for a pointer to a non-static
1560 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1561 appertain to the pointer or reference. */
1563 cp_declarator *
1564 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1565 cp_declarator *pointee,
1566 tree attributes)
1568 cp_declarator *declarator;
1570 declarator = make_declarator (cdk_ptrmem);
1571 declarator->declarator = pointee;
1572 declarator->u.pointer.qualifiers = cv_qualifiers;
1573 declarator->u.pointer.class_type = class_type;
1575 if (pointee)
1577 declarator->parameter_pack_p = pointee->parameter_pack_p;
1578 pointee->parameter_pack_p = false;
1580 else
1581 declarator->parameter_pack_p = false;
1583 declarator->std_attributes = attributes;
1585 return declarator;
1588 /* Make a declarator for the function given by TARGET, with the
1589 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1590 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1591 indicates what exceptions can be thrown. */
1593 cp_declarator *
1594 make_call_declarator (cp_declarator *target,
1595 tree parms,
1596 cp_cv_quals cv_qualifiers,
1597 cp_virt_specifiers virt_specifiers,
1598 cp_ref_qualifier ref_qualifier,
1599 tree tx_qualifier,
1600 tree exception_specification,
1601 tree late_return_type,
1602 tree requires_clause)
1604 cp_declarator *declarator;
1606 declarator = make_declarator (cdk_function);
1607 declarator->declarator = target;
1608 declarator->u.function.parameters = parms;
1609 declarator->u.function.qualifiers = cv_qualifiers;
1610 declarator->u.function.virt_specifiers = virt_specifiers;
1611 declarator->u.function.ref_qualifier = ref_qualifier;
1612 declarator->u.function.tx_qualifier = tx_qualifier;
1613 declarator->u.function.exception_specification = exception_specification;
1614 declarator->u.function.late_return_type = late_return_type;
1615 declarator->u.function.requires_clause = requires_clause;
1616 if (target)
1618 declarator->id_loc = target->id_loc;
1619 declarator->parameter_pack_p = target->parameter_pack_p;
1620 target->parameter_pack_p = false;
1622 else
1623 declarator->parameter_pack_p = false;
1625 return declarator;
1628 /* Make a declarator for an array of BOUNDS elements, each of which is
1629 defined by ELEMENT. */
1631 cp_declarator *
1632 make_array_declarator (cp_declarator *element, tree bounds)
1634 cp_declarator *declarator;
1636 declarator = make_declarator (cdk_array);
1637 declarator->declarator = element;
1638 declarator->u.array.bounds = bounds;
1639 if (element)
1641 declarator->id_loc = element->id_loc;
1642 declarator->parameter_pack_p = element->parameter_pack_p;
1643 element->parameter_pack_p = false;
1645 else
1646 declarator->parameter_pack_p = false;
1648 return declarator;
1651 /* Determine whether the declarator we've seen so far can be a
1652 parameter pack, when followed by an ellipsis. */
1653 static bool
1654 declarator_can_be_parameter_pack (cp_declarator *declarator)
1656 if (declarator && declarator->parameter_pack_p)
1657 /* We already saw an ellipsis. */
1658 return false;
1660 /* Search for a declarator name, or any other declarator that goes
1661 after the point where the ellipsis could appear in a parameter
1662 pack. If we find any of these, then this declarator can not be
1663 made into a parameter pack. */
1664 bool found = false;
1665 while (declarator && !found)
1667 switch ((int)declarator->kind)
1669 case cdk_id:
1670 case cdk_array:
1671 found = true;
1672 break;
1674 case cdk_error:
1675 return true;
1677 default:
1678 declarator = declarator->declarator;
1679 break;
1683 return !found;
1686 cp_parameter_declarator *no_parameters;
1688 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1689 DECLARATOR and DEFAULT_ARGUMENT. */
1691 cp_parameter_declarator *
1692 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1693 cp_declarator *declarator,
1694 tree default_argument,
1695 bool template_parameter_pack_p = false)
1697 cp_parameter_declarator *parameter;
1699 parameter = ((cp_parameter_declarator *)
1700 alloc_declarator (sizeof (cp_parameter_declarator)));
1701 parameter->next = NULL;
1702 if (decl_specifiers)
1703 parameter->decl_specifiers = *decl_specifiers;
1704 else
1705 clear_decl_specs (&parameter->decl_specifiers);
1706 parameter->declarator = declarator;
1707 parameter->default_argument = default_argument;
1708 parameter->template_parameter_pack_p = template_parameter_pack_p;
1710 return parameter;
1713 /* Returns true iff DECLARATOR is a declaration for a function. */
1715 static bool
1716 function_declarator_p (const cp_declarator *declarator)
1718 while (declarator)
1720 if (declarator->kind == cdk_function
1721 && declarator->declarator->kind == cdk_id)
1722 return true;
1723 if (declarator->kind == cdk_id
1724 || declarator->kind == cdk_error)
1725 return false;
1726 declarator = declarator->declarator;
1728 return false;
1731 /* The parser. */
1733 /* Overview
1734 --------
1736 A cp_parser parses the token stream as specified by the C++
1737 grammar. Its job is purely parsing, not semantic analysis. For
1738 example, the parser breaks the token stream into declarators,
1739 expressions, statements, and other similar syntactic constructs.
1740 It does not check that the types of the expressions on either side
1741 of an assignment-statement are compatible, or that a function is
1742 not declared with a parameter of type `void'.
1744 The parser invokes routines elsewhere in the compiler to perform
1745 semantic analysis and to build up the abstract syntax tree for the
1746 code processed.
1748 The parser (and the template instantiation code, which is, in a
1749 way, a close relative of parsing) are the only parts of the
1750 compiler that should be calling push_scope and pop_scope, or
1751 related functions. The parser (and template instantiation code)
1752 keeps track of what scope is presently active; everything else
1753 should simply honor that. (The code that generates static
1754 initializers may also need to set the scope, in order to check
1755 access control correctly when emitting the initializers.)
1757 Methodology
1758 -----------
1760 The parser is of the standard recursive-descent variety. Upcoming
1761 tokens in the token stream are examined in order to determine which
1762 production to use when parsing a non-terminal. Some C++ constructs
1763 require arbitrary look ahead to disambiguate. For example, it is
1764 impossible, in the general case, to tell whether a statement is an
1765 expression or declaration without scanning the entire statement.
1766 Therefore, the parser is capable of "parsing tentatively." When the
1767 parser is not sure what construct comes next, it enters this mode.
1768 Then, while we attempt to parse the construct, the parser queues up
1769 error messages, rather than issuing them immediately, and saves the
1770 tokens it consumes. If the construct is parsed successfully, the
1771 parser "commits", i.e., it issues any queued error messages and
1772 the tokens that were being preserved are permanently discarded.
1773 If, however, the construct is not parsed successfully, the parser
1774 rolls back its state completely so that it can resume parsing using
1775 a different alternative.
1777 Future Improvements
1778 -------------------
1780 The performance of the parser could probably be improved substantially.
1781 We could often eliminate the need to parse tentatively by looking ahead
1782 a little bit. In some places, this approach might not entirely eliminate
1783 the need to parse tentatively, but it might still speed up the average
1784 case. */
1786 /* Flags that are passed to some parsing functions. These values can
1787 be bitwise-ored together. */
1789 enum
1791 /* No flags. */
1792 CP_PARSER_FLAGS_NONE = 0x0,
1793 /* The construct is optional. If it is not present, then no error
1794 should be issued. */
1795 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1796 /* When parsing a type-specifier, treat user-defined type-names
1797 as non-type identifiers. */
1798 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1799 /* When parsing a type-specifier, do not try to parse a class-specifier
1800 or enum-specifier. */
1801 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1802 /* When parsing a decl-specifier-seq, only allow type-specifier or
1803 constexpr. */
1804 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1805 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1806 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1809 /* This type is used for parameters and variables which hold
1810 combinations of the above flags. */
1811 typedef int cp_parser_flags;
1813 /* The different kinds of declarators we want to parse. */
1815 enum cp_parser_declarator_kind
1817 /* We want an abstract declarator. */
1818 CP_PARSER_DECLARATOR_ABSTRACT,
1819 /* We want a named declarator. */
1820 CP_PARSER_DECLARATOR_NAMED,
1821 /* We don't mind, but the name must be an unqualified-id. */
1822 CP_PARSER_DECLARATOR_EITHER
1825 /* The precedence values used to parse binary expressions. The minimum value
1826 of PREC must be 1, because zero is reserved to quickly discriminate
1827 binary operators from other tokens. */
1829 enum cp_parser_prec
1831 PREC_NOT_OPERATOR,
1832 PREC_LOGICAL_OR_EXPRESSION,
1833 PREC_LOGICAL_AND_EXPRESSION,
1834 PREC_INCLUSIVE_OR_EXPRESSION,
1835 PREC_EXCLUSIVE_OR_EXPRESSION,
1836 PREC_AND_EXPRESSION,
1837 PREC_EQUALITY_EXPRESSION,
1838 PREC_RELATIONAL_EXPRESSION,
1839 PREC_SHIFT_EXPRESSION,
1840 PREC_ADDITIVE_EXPRESSION,
1841 PREC_MULTIPLICATIVE_EXPRESSION,
1842 PREC_PM_EXPRESSION,
1843 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1846 /* A mapping from a token type to a corresponding tree node type, with a
1847 precedence value. */
1849 struct cp_parser_binary_operations_map_node
1851 /* The token type. */
1852 enum cpp_ttype token_type;
1853 /* The corresponding tree code. */
1854 enum tree_code tree_type;
1855 /* The precedence of this operator. */
1856 enum cp_parser_prec prec;
1859 struct cp_parser_expression_stack_entry
1861 /* Left hand side of the binary operation we are currently
1862 parsing. */
1863 cp_expr lhs;
1864 /* Original tree code for left hand side, if it was a binary
1865 expression itself (used for -Wparentheses). */
1866 enum tree_code lhs_type;
1867 /* Tree code for the binary operation we are parsing. */
1868 enum tree_code tree_type;
1869 /* Precedence of the binary operation we are parsing. */
1870 enum cp_parser_prec prec;
1871 /* Location of the binary operation we are parsing. */
1872 location_t loc;
1875 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1876 entries because precedence levels on the stack are monotonically
1877 increasing. */
1878 typedef struct cp_parser_expression_stack_entry
1879 cp_parser_expression_stack[NUM_PREC_VALUES];
1881 /* Prototypes. */
1883 /* Constructors and destructors. */
1885 static cp_parser_context *cp_parser_context_new
1886 (cp_parser_context *);
1888 /* Class variables. */
1890 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1892 /* The operator-precedence table used by cp_parser_binary_expression.
1893 Transformed into an associative array (binops_by_token) by
1894 cp_parser_new. */
1896 static const cp_parser_binary_operations_map_node binops[] = {
1897 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1898 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1900 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1901 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1902 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1904 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1905 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1907 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1908 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1910 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1911 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1912 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1913 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1915 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1916 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1918 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1920 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1922 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1924 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1926 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1929 /* The same as binops, but initialized by cp_parser_new so that
1930 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1931 for speed. */
1932 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1934 /* Constructors and destructors. */
1936 /* Construct a new context. The context below this one on the stack
1937 is given by NEXT. */
1939 static cp_parser_context *
1940 cp_parser_context_new (cp_parser_context* next)
1942 cp_parser_context *context;
1944 /* Allocate the storage. */
1945 if (cp_parser_context_free_list != NULL)
1947 /* Pull the first entry from the free list. */
1948 context = cp_parser_context_free_list;
1949 cp_parser_context_free_list = context->next;
1950 memset (context, 0, sizeof (*context));
1952 else
1953 context = ggc_cleared_alloc<cp_parser_context> ();
1955 /* No errors have occurred yet in this context. */
1956 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1957 /* If this is not the bottommost context, copy information that we
1958 need from the previous context. */
1959 if (next)
1961 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1962 expression, then we are parsing one in this context, too. */
1963 context->object_type = next->object_type;
1964 /* Thread the stack. */
1965 context->next = next;
1968 return context;
1971 /* Managing the unparsed function queues. */
1973 #define unparsed_funs_with_default_args \
1974 parser->unparsed_queues->last ().funs_with_default_args
1975 #define unparsed_funs_with_definitions \
1976 parser->unparsed_queues->last ().funs_with_definitions
1977 #define unparsed_nsdmis \
1978 parser->unparsed_queues->last ().nsdmis
1979 #define unparsed_classes \
1980 parser->unparsed_queues->last ().classes
1982 static void
1983 push_unparsed_function_queues (cp_parser *parser)
1985 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1986 vec_safe_push (parser->unparsed_queues, e);
1989 static void
1990 pop_unparsed_function_queues (cp_parser *parser)
1992 release_tree_vector (unparsed_funs_with_definitions);
1993 parser->unparsed_queues->pop ();
1996 /* Prototypes. */
1998 /* Constructors and destructors. */
2000 static cp_parser *cp_parser_new
2001 (void);
2003 /* Routines to parse various constructs.
2005 Those that return `tree' will return the error_mark_node (rather
2006 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2007 Sometimes, they will return an ordinary node if error-recovery was
2008 attempted, even though a parse error occurred. So, to check
2009 whether or not a parse error occurred, you should always use
2010 cp_parser_error_occurred. If the construct is optional (indicated
2011 either by an `_opt' in the name of the function that does the
2012 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2013 the construct is not present. */
2015 /* Lexical conventions [gram.lex] */
2017 static cp_expr cp_parser_identifier
2018 (cp_parser *);
2019 static cp_expr cp_parser_string_literal
2020 (cp_parser *, bool, bool, bool);
2021 static cp_expr cp_parser_userdef_char_literal
2022 (cp_parser *);
2023 static tree cp_parser_userdef_string_literal
2024 (tree);
2025 static cp_expr cp_parser_userdef_numeric_literal
2026 (cp_parser *);
2028 /* Basic concepts [gram.basic] */
2030 static bool cp_parser_translation_unit
2031 (cp_parser *);
2033 /* Expressions [gram.expr] */
2035 static cp_expr cp_parser_primary_expression
2036 (cp_parser *, bool, bool, bool, cp_id_kind *);
2037 static cp_expr cp_parser_id_expression
2038 (cp_parser *, bool, bool, bool *, bool, bool);
2039 static cp_expr cp_parser_unqualified_id
2040 (cp_parser *, bool, bool, bool, bool);
2041 static tree cp_parser_nested_name_specifier_opt
2042 (cp_parser *, bool, bool, bool, bool);
2043 static tree cp_parser_nested_name_specifier
2044 (cp_parser *, bool, bool, bool, bool);
2045 static tree cp_parser_qualifying_entity
2046 (cp_parser *, bool, bool, bool, bool, bool);
2047 static cp_expr cp_parser_postfix_expression
2048 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2049 static tree cp_parser_postfix_open_square_expression
2050 (cp_parser *, tree, bool, bool);
2051 static tree cp_parser_postfix_dot_deref_expression
2052 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2053 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2054 (cp_parser *, int, bool, bool, bool *, location_t * = NULL);
2055 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2056 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2057 static void cp_parser_pseudo_destructor_name
2058 (cp_parser *, tree, tree *, tree *);
2059 static cp_expr cp_parser_unary_expression
2060 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2061 static enum tree_code cp_parser_unary_operator
2062 (cp_token *);
2063 static tree cp_parser_new_expression
2064 (cp_parser *);
2065 static vec<tree, va_gc> *cp_parser_new_placement
2066 (cp_parser *);
2067 static tree cp_parser_new_type_id
2068 (cp_parser *, tree *);
2069 static cp_declarator *cp_parser_new_declarator_opt
2070 (cp_parser *);
2071 static cp_declarator *cp_parser_direct_new_declarator
2072 (cp_parser *);
2073 static vec<tree, va_gc> *cp_parser_new_initializer
2074 (cp_parser *);
2075 static tree cp_parser_delete_expression
2076 (cp_parser *);
2077 static cp_expr cp_parser_cast_expression
2078 (cp_parser *, bool, bool, bool, cp_id_kind *);
2079 static cp_expr cp_parser_binary_expression
2080 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2081 static tree cp_parser_question_colon_clause
2082 (cp_parser *, cp_expr);
2083 static cp_expr cp_parser_assignment_expression
2084 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2085 static enum tree_code cp_parser_assignment_operator_opt
2086 (cp_parser *);
2087 static cp_expr cp_parser_expression
2088 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2089 static cp_expr cp_parser_constant_expression
2090 (cp_parser *, bool = false, bool * = NULL);
2091 static cp_expr cp_parser_builtin_offsetof
2092 (cp_parser *);
2093 static cp_expr cp_parser_lambda_expression
2094 (cp_parser *);
2095 static void cp_parser_lambda_introducer
2096 (cp_parser *, tree);
2097 static bool cp_parser_lambda_declarator_opt
2098 (cp_parser *, tree);
2099 static void cp_parser_lambda_body
2100 (cp_parser *, tree);
2102 /* Statements [gram.stmt.stmt] */
2104 static void cp_parser_statement
2105 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL);
2106 static void cp_parser_label_for_labeled_statement
2107 (cp_parser *, tree);
2108 static tree cp_parser_expression_statement
2109 (cp_parser *, tree);
2110 static tree cp_parser_compound_statement
2111 (cp_parser *, tree, int, bool);
2112 static void cp_parser_statement_seq_opt
2113 (cp_parser *, tree);
2114 static tree cp_parser_selection_statement
2115 (cp_parser *, bool *, vec<tree> *);
2116 static tree cp_parser_condition
2117 (cp_parser *);
2118 static tree cp_parser_iteration_statement
2119 (cp_parser *, bool *, bool);
2120 static bool cp_parser_for_init_statement
2121 (cp_parser *, tree *decl);
2122 static tree cp_parser_for
2123 (cp_parser *, bool);
2124 static tree cp_parser_c_for
2125 (cp_parser *, tree, tree, bool);
2126 static tree cp_parser_range_for
2127 (cp_parser *, tree, tree, tree, bool);
2128 static void do_range_for_auto_deduction
2129 (tree, tree);
2130 static tree cp_parser_perform_range_for_lookup
2131 (tree, tree *, tree *);
2132 static tree cp_parser_range_for_member_function
2133 (tree, tree);
2134 static tree cp_parser_jump_statement
2135 (cp_parser *);
2136 static void cp_parser_declaration_statement
2137 (cp_parser *);
2139 static tree cp_parser_implicitly_scoped_statement
2140 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2141 static void cp_parser_already_scoped_statement
2142 (cp_parser *, bool *, const token_indent_info &);
2144 /* Declarations [gram.dcl.dcl] */
2146 static void cp_parser_declaration_seq_opt
2147 (cp_parser *);
2148 static void cp_parser_declaration
2149 (cp_parser *);
2150 static void cp_parser_block_declaration
2151 (cp_parser *, bool);
2152 static void cp_parser_simple_declaration
2153 (cp_parser *, bool, tree *);
2154 static void cp_parser_decl_specifier_seq
2155 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2156 static tree cp_parser_storage_class_specifier_opt
2157 (cp_parser *);
2158 static tree cp_parser_function_specifier_opt
2159 (cp_parser *, cp_decl_specifier_seq *);
2160 static tree cp_parser_type_specifier
2161 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2162 int *, bool *);
2163 static tree cp_parser_simple_type_specifier
2164 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2165 static tree cp_parser_type_name
2166 (cp_parser *, bool);
2167 static tree cp_parser_type_name
2168 (cp_parser *);
2169 static tree cp_parser_nonclass_name
2170 (cp_parser* parser);
2171 static tree cp_parser_elaborated_type_specifier
2172 (cp_parser *, bool, bool);
2173 static tree cp_parser_enum_specifier
2174 (cp_parser *);
2175 static void cp_parser_enumerator_list
2176 (cp_parser *, tree);
2177 static void cp_parser_enumerator_definition
2178 (cp_parser *, tree);
2179 static tree cp_parser_namespace_name
2180 (cp_parser *);
2181 static void cp_parser_namespace_definition
2182 (cp_parser *);
2183 static void cp_parser_namespace_body
2184 (cp_parser *);
2185 static tree cp_parser_qualified_namespace_specifier
2186 (cp_parser *);
2187 static void cp_parser_namespace_alias_definition
2188 (cp_parser *);
2189 static bool cp_parser_using_declaration
2190 (cp_parser *, bool);
2191 static void cp_parser_using_directive
2192 (cp_parser *);
2193 static tree cp_parser_alias_declaration
2194 (cp_parser *);
2195 static void cp_parser_asm_definition
2196 (cp_parser *);
2197 static void cp_parser_linkage_specification
2198 (cp_parser *);
2199 static void cp_parser_static_assert
2200 (cp_parser *, bool);
2201 static tree cp_parser_decltype
2202 (cp_parser *);
2204 /* Declarators [gram.dcl.decl] */
2206 static tree cp_parser_init_declarator
2207 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2208 bool, bool, int, bool *, tree *, location_t *, tree *);
2209 static cp_declarator *cp_parser_declarator
2210 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2211 static cp_declarator *cp_parser_direct_declarator
2212 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2213 static enum tree_code cp_parser_ptr_operator
2214 (cp_parser *, tree *, cp_cv_quals *, tree *);
2215 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2216 (cp_parser *);
2217 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2218 (cp_parser *);
2219 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2220 (cp_parser *);
2221 static tree cp_parser_tx_qualifier_opt
2222 (cp_parser *);
2223 static tree cp_parser_late_return_type_opt
2224 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2225 static tree cp_parser_declarator_id
2226 (cp_parser *, bool);
2227 static tree cp_parser_type_id
2228 (cp_parser *);
2229 static tree cp_parser_template_type_arg
2230 (cp_parser *);
2231 static tree cp_parser_trailing_type_id (cp_parser *);
2232 static tree cp_parser_type_id_1
2233 (cp_parser *, bool, bool);
2234 static void cp_parser_type_specifier_seq
2235 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2236 static tree cp_parser_parameter_declaration_clause
2237 (cp_parser *);
2238 static tree cp_parser_parameter_declaration_list
2239 (cp_parser *, bool *);
2240 static cp_parameter_declarator *cp_parser_parameter_declaration
2241 (cp_parser *, bool, bool *);
2242 static tree cp_parser_default_argument
2243 (cp_parser *, bool);
2244 static void cp_parser_function_body
2245 (cp_parser *, bool);
2246 static tree cp_parser_initializer
2247 (cp_parser *, bool *, bool *);
2248 static cp_expr cp_parser_initializer_clause
2249 (cp_parser *, bool *);
2250 static cp_expr cp_parser_braced_list
2251 (cp_parser*, bool*);
2252 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2253 (cp_parser *, bool *);
2255 static bool cp_parser_ctor_initializer_opt_and_function_body
2256 (cp_parser *, bool);
2258 static tree cp_parser_late_parsing_omp_declare_simd
2259 (cp_parser *, tree);
2261 static tree cp_parser_late_parsing_cilk_simd_fn_info
2262 (cp_parser *, tree);
2264 static tree cp_parser_late_parsing_oacc_routine
2265 (cp_parser *, tree);
2267 static tree synthesize_implicit_template_parm
2268 (cp_parser *, tree);
2269 static tree finish_fully_implicit_template
2270 (cp_parser *, tree);
2272 /* Classes [gram.class] */
2274 static tree cp_parser_class_name
2275 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2276 static tree cp_parser_class_specifier
2277 (cp_parser *);
2278 static tree cp_parser_class_head
2279 (cp_parser *, bool *);
2280 static enum tag_types cp_parser_class_key
2281 (cp_parser *);
2282 static void cp_parser_type_parameter_key
2283 (cp_parser* parser);
2284 static void cp_parser_member_specification_opt
2285 (cp_parser *);
2286 static void cp_parser_member_declaration
2287 (cp_parser *);
2288 static tree cp_parser_pure_specifier
2289 (cp_parser *);
2290 static tree cp_parser_constant_initializer
2291 (cp_parser *);
2293 /* Derived classes [gram.class.derived] */
2295 static tree cp_parser_base_clause
2296 (cp_parser *);
2297 static tree cp_parser_base_specifier
2298 (cp_parser *);
2300 /* Special member functions [gram.special] */
2302 static tree cp_parser_conversion_function_id
2303 (cp_parser *);
2304 static tree cp_parser_conversion_type_id
2305 (cp_parser *);
2306 static cp_declarator *cp_parser_conversion_declarator_opt
2307 (cp_parser *);
2308 static bool cp_parser_ctor_initializer_opt
2309 (cp_parser *);
2310 static void cp_parser_mem_initializer_list
2311 (cp_parser *);
2312 static tree cp_parser_mem_initializer
2313 (cp_parser *);
2314 static tree cp_parser_mem_initializer_id
2315 (cp_parser *);
2317 /* Overloading [gram.over] */
2319 static cp_expr cp_parser_operator_function_id
2320 (cp_parser *);
2321 static cp_expr cp_parser_operator
2322 (cp_parser *);
2324 /* Templates [gram.temp] */
2326 static void cp_parser_template_declaration
2327 (cp_parser *, bool);
2328 static tree cp_parser_template_parameter_list
2329 (cp_parser *);
2330 static tree cp_parser_template_parameter
2331 (cp_parser *, bool *, bool *);
2332 static tree cp_parser_type_parameter
2333 (cp_parser *, bool *);
2334 static tree cp_parser_template_id
2335 (cp_parser *, bool, bool, enum tag_types, bool);
2336 static tree cp_parser_template_name
2337 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2338 static tree cp_parser_template_argument_list
2339 (cp_parser *);
2340 static tree cp_parser_template_argument
2341 (cp_parser *);
2342 static void cp_parser_explicit_instantiation
2343 (cp_parser *);
2344 static void cp_parser_explicit_specialization
2345 (cp_parser *);
2347 /* Exception handling [gram.exception] */
2349 static tree cp_parser_try_block
2350 (cp_parser *);
2351 static bool cp_parser_function_try_block
2352 (cp_parser *);
2353 static void cp_parser_handler_seq
2354 (cp_parser *);
2355 static void cp_parser_handler
2356 (cp_parser *);
2357 static tree cp_parser_exception_declaration
2358 (cp_parser *);
2359 static tree cp_parser_throw_expression
2360 (cp_parser *);
2361 static tree cp_parser_exception_specification_opt
2362 (cp_parser *);
2363 static tree cp_parser_type_id_list
2364 (cp_parser *);
2366 /* GNU Extensions */
2368 static tree cp_parser_asm_specification_opt
2369 (cp_parser *);
2370 static tree cp_parser_asm_operand_list
2371 (cp_parser *);
2372 static tree cp_parser_asm_clobber_list
2373 (cp_parser *);
2374 static tree cp_parser_asm_label_list
2375 (cp_parser *);
2376 static bool cp_next_tokens_can_be_attribute_p
2377 (cp_parser *);
2378 static bool cp_next_tokens_can_be_gnu_attribute_p
2379 (cp_parser *);
2380 static bool cp_next_tokens_can_be_std_attribute_p
2381 (cp_parser *);
2382 static bool cp_nth_tokens_can_be_std_attribute_p
2383 (cp_parser *, size_t);
2384 static bool cp_nth_tokens_can_be_gnu_attribute_p
2385 (cp_parser *, size_t);
2386 static bool cp_nth_tokens_can_be_attribute_p
2387 (cp_parser *, size_t);
2388 static tree cp_parser_attributes_opt
2389 (cp_parser *);
2390 static tree cp_parser_gnu_attributes_opt
2391 (cp_parser *);
2392 static tree cp_parser_gnu_attribute_list
2393 (cp_parser *);
2394 static tree cp_parser_std_attribute
2395 (cp_parser *, tree);
2396 static tree cp_parser_std_attribute_spec
2397 (cp_parser *);
2398 static tree cp_parser_std_attribute_spec_seq
2399 (cp_parser *);
2400 static bool cp_parser_extension_opt
2401 (cp_parser *, int *);
2402 static void cp_parser_label_declaration
2403 (cp_parser *);
2405 /* Concept Extensions */
2407 static tree cp_parser_requires_clause
2408 (cp_parser *);
2409 static tree cp_parser_requires_clause_opt
2410 (cp_parser *);
2411 static tree cp_parser_requires_expression
2412 (cp_parser *);
2413 static tree cp_parser_requirement_parameter_list
2414 (cp_parser *);
2415 static tree cp_parser_requirement_body
2416 (cp_parser *);
2417 static tree cp_parser_requirement_list
2418 (cp_parser *);
2419 static tree cp_parser_requirement
2420 (cp_parser *);
2421 static tree cp_parser_simple_requirement
2422 (cp_parser *);
2423 static tree cp_parser_compound_requirement
2424 (cp_parser *);
2425 static tree cp_parser_type_requirement
2426 (cp_parser *);
2427 static tree cp_parser_nested_requirement
2428 (cp_parser *);
2430 /* Transactional Memory Extensions */
2432 static tree cp_parser_transaction
2433 (cp_parser *, cp_token *);
2434 static tree cp_parser_transaction_expression
2435 (cp_parser *, enum rid);
2436 static bool cp_parser_function_transaction
2437 (cp_parser *, enum rid);
2438 static tree cp_parser_transaction_cancel
2439 (cp_parser *);
2441 enum pragma_context {
2442 pragma_external,
2443 pragma_member,
2444 pragma_objc_icode,
2445 pragma_stmt,
2446 pragma_compound
2448 static bool cp_parser_pragma
2449 (cp_parser *, enum pragma_context, bool *);
2451 /* Objective-C++ Productions */
2453 static tree cp_parser_objc_message_receiver
2454 (cp_parser *);
2455 static tree cp_parser_objc_message_args
2456 (cp_parser *);
2457 static tree cp_parser_objc_message_expression
2458 (cp_parser *);
2459 static cp_expr cp_parser_objc_encode_expression
2460 (cp_parser *);
2461 static tree cp_parser_objc_defs_expression
2462 (cp_parser *);
2463 static tree cp_parser_objc_protocol_expression
2464 (cp_parser *);
2465 static tree cp_parser_objc_selector_expression
2466 (cp_parser *);
2467 static cp_expr cp_parser_objc_expression
2468 (cp_parser *);
2469 static bool cp_parser_objc_selector_p
2470 (enum cpp_ttype);
2471 static tree cp_parser_objc_selector
2472 (cp_parser *);
2473 static tree cp_parser_objc_protocol_refs_opt
2474 (cp_parser *);
2475 static void cp_parser_objc_declaration
2476 (cp_parser *, tree);
2477 static tree cp_parser_objc_statement
2478 (cp_parser *);
2479 static bool cp_parser_objc_valid_prefix_attributes
2480 (cp_parser *, tree *);
2481 static void cp_parser_objc_at_property_declaration
2482 (cp_parser *) ;
2483 static void cp_parser_objc_at_synthesize_declaration
2484 (cp_parser *) ;
2485 static void cp_parser_objc_at_dynamic_declaration
2486 (cp_parser *) ;
2487 static tree cp_parser_objc_struct_declaration
2488 (cp_parser *) ;
2490 /* Utility Routines */
2492 static cp_expr cp_parser_lookup_name
2493 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2494 static tree cp_parser_lookup_name_simple
2495 (cp_parser *, tree, location_t);
2496 static tree cp_parser_maybe_treat_template_as_class
2497 (tree, bool);
2498 static bool cp_parser_check_declarator_template_parameters
2499 (cp_parser *, cp_declarator *, location_t);
2500 static bool cp_parser_check_template_parameters
2501 (cp_parser *, unsigned, location_t, cp_declarator *);
2502 static cp_expr cp_parser_simple_cast_expression
2503 (cp_parser *);
2504 static tree cp_parser_global_scope_opt
2505 (cp_parser *, bool);
2506 static bool cp_parser_constructor_declarator_p
2507 (cp_parser *, bool);
2508 static tree cp_parser_function_definition_from_specifiers_and_declarator
2509 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2510 static tree cp_parser_function_definition_after_declarator
2511 (cp_parser *, bool);
2512 static bool cp_parser_template_declaration_after_export
2513 (cp_parser *, bool);
2514 static void cp_parser_perform_template_parameter_access_checks
2515 (vec<deferred_access_check, va_gc> *);
2516 static tree cp_parser_single_declaration
2517 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2518 static cp_expr cp_parser_functional_cast
2519 (cp_parser *, tree);
2520 static tree cp_parser_save_member_function_body
2521 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2522 static tree cp_parser_save_nsdmi
2523 (cp_parser *);
2524 static tree cp_parser_enclosed_template_argument_list
2525 (cp_parser *);
2526 static void cp_parser_save_default_args
2527 (cp_parser *, tree);
2528 static void cp_parser_late_parsing_for_member
2529 (cp_parser *, tree);
2530 static tree cp_parser_late_parse_one_default_arg
2531 (cp_parser *, tree, tree, tree);
2532 static void cp_parser_late_parsing_nsdmi
2533 (cp_parser *, tree);
2534 static void cp_parser_late_parsing_default_args
2535 (cp_parser *, tree);
2536 static tree cp_parser_sizeof_operand
2537 (cp_parser *, enum rid);
2538 static tree cp_parser_trait_expr
2539 (cp_parser *, enum rid);
2540 static bool cp_parser_declares_only_class_p
2541 (cp_parser *);
2542 static void cp_parser_set_storage_class
2543 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2544 static void cp_parser_set_decl_spec_type
2545 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2546 static void set_and_check_decl_spec_loc
2547 (cp_decl_specifier_seq *decl_specs,
2548 cp_decl_spec ds, cp_token *);
2549 static bool cp_parser_friend_p
2550 (const cp_decl_specifier_seq *);
2551 static void cp_parser_required_error
2552 (cp_parser *, required_token, bool);
2553 static cp_token *cp_parser_require
2554 (cp_parser *, enum cpp_ttype, required_token);
2555 static cp_token *cp_parser_require_keyword
2556 (cp_parser *, enum rid, required_token);
2557 static bool cp_parser_token_starts_function_definition_p
2558 (cp_token *);
2559 static bool cp_parser_next_token_starts_class_definition_p
2560 (cp_parser *);
2561 static bool cp_parser_next_token_ends_template_argument_p
2562 (cp_parser *);
2563 static bool cp_parser_nth_token_starts_template_argument_list_p
2564 (cp_parser *, size_t);
2565 static enum tag_types cp_parser_token_is_class_key
2566 (cp_token *);
2567 static enum tag_types cp_parser_token_is_type_parameter_key
2568 (cp_token *);
2569 static void cp_parser_check_class_key
2570 (enum tag_types, tree type);
2571 static void cp_parser_check_access_in_redeclaration
2572 (tree type, location_t location);
2573 static bool cp_parser_optional_template_keyword
2574 (cp_parser *);
2575 static void cp_parser_pre_parsed_nested_name_specifier
2576 (cp_parser *);
2577 static bool cp_parser_cache_group
2578 (cp_parser *, enum cpp_ttype, unsigned);
2579 static tree cp_parser_cache_defarg
2580 (cp_parser *parser, bool nsdmi);
2581 static void cp_parser_parse_tentatively
2582 (cp_parser *);
2583 static void cp_parser_commit_to_tentative_parse
2584 (cp_parser *);
2585 static void cp_parser_commit_to_topmost_tentative_parse
2586 (cp_parser *);
2587 static void cp_parser_abort_tentative_parse
2588 (cp_parser *);
2589 static bool cp_parser_parse_definitely
2590 (cp_parser *);
2591 static inline bool cp_parser_parsing_tentatively
2592 (cp_parser *);
2593 static bool cp_parser_uncommitted_to_tentative_parse_p
2594 (cp_parser *);
2595 static void cp_parser_error
2596 (cp_parser *, const char *);
2597 static void cp_parser_name_lookup_error
2598 (cp_parser *, tree, tree, name_lookup_error, location_t);
2599 static bool cp_parser_simulate_error
2600 (cp_parser *);
2601 static bool cp_parser_check_type_definition
2602 (cp_parser *);
2603 static void cp_parser_check_for_definition_in_return_type
2604 (cp_declarator *, tree, location_t type_location);
2605 static void cp_parser_check_for_invalid_template_id
2606 (cp_parser *, tree, enum tag_types, location_t location);
2607 static bool cp_parser_non_integral_constant_expression
2608 (cp_parser *, non_integral_constant);
2609 static void cp_parser_diagnose_invalid_type_name
2610 (cp_parser *, tree, location_t);
2611 static bool cp_parser_parse_and_diagnose_invalid_type_name
2612 (cp_parser *);
2613 static int cp_parser_skip_to_closing_parenthesis
2614 (cp_parser *, bool, bool, bool);
2615 static void cp_parser_skip_to_end_of_statement
2616 (cp_parser *);
2617 static void cp_parser_consume_semicolon_at_end_of_statement
2618 (cp_parser *);
2619 static void cp_parser_skip_to_end_of_block_or_statement
2620 (cp_parser *);
2621 static bool cp_parser_skip_to_closing_brace
2622 (cp_parser *);
2623 static void cp_parser_skip_to_end_of_template_parameter_list
2624 (cp_parser *);
2625 static void cp_parser_skip_to_pragma_eol
2626 (cp_parser*, cp_token *);
2627 static bool cp_parser_error_occurred
2628 (cp_parser *);
2629 static bool cp_parser_allow_gnu_extensions_p
2630 (cp_parser *);
2631 static bool cp_parser_is_pure_string_literal
2632 (cp_token *);
2633 static bool cp_parser_is_string_literal
2634 (cp_token *);
2635 static bool cp_parser_is_keyword
2636 (cp_token *, enum rid);
2637 static tree cp_parser_make_typename_type
2638 (cp_parser *, tree, location_t location);
2639 static cp_declarator * cp_parser_make_indirect_declarator
2640 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2641 static bool cp_parser_compound_literal_p
2642 (cp_parser *);
2643 static bool cp_parser_array_designator_p
2644 (cp_parser *);
2645 static bool cp_parser_skip_to_closing_square_bracket
2646 (cp_parser *);
2648 /* Concept-related syntactic transformations */
2650 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2651 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2653 // -------------------------------------------------------------------------- //
2654 // Unevaluated Operand Guard
2656 // Implementation of an RAII helper for unevaluated operand parsing.
2657 cp_unevaluated::cp_unevaluated ()
2659 ++cp_unevaluated_operand;
2660 ++c_inhibit_evaluation_warnings;
2663 cp_unevaluated::~cp_unevaluated ()
2665 --c_inhibit_evaluation_warnings;
2666 --cp_unevaluated_operand;
2669 // -------------------------------------------------------------------------- //
2670 // Tentative Parsing
2672 /* Returns nonzero if we are parsing tentatively. */
2674 static inline bool
2675 cp_parser_parsing_tentatively (cp_parser* parser)
2677 return parser->context->next != NULL;
2680 /* Returns nonzero if TOKEN is a string literal. */
2682 static bool
2683 cp_parser_is_pure_string_literal (cp_token* token)
2685 return (token->type == CPP_STRING ||
2686 token->type == CPP_STRING16 ||
2687 token->type == CPP_STRING32 ||
2688 token->type == CPP_WSTRING ||
2689 token->type == CPP_UTF8STRING);
2692 /* Returns nonzero if TOKEN is a string literal
2693 of a user-defined string literal. */
2695 static bool
2696 cp_parser_is_string_literal (cp_token* token)
2698 return (cp_parser_is_pure_string_literal (token) ||
2699 token->type == CPP_STRING_USERDEF ||
2700 token->type == CPP_STRING16_USERDEF ||
2701 token->type == CPP_STRING32_USERDEF ||
2702 token->type == CPP_WSTRING_USERDEF ||
2703 token->type == CPP_UTF8STRING_USERDEF);
2706 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2708 static bool
2709 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2711 return token->keyword == keyword;
2714 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2715 PRAGMA_NONE. */
2717 static enum pragma_kind
2718 cp_parser_pragma_kind (cp_token *token)
2720 if (token->type != CPP_PRAGMA)
2721 return PRAGMA_NONE;
2722 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2723 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2726 /* Helper function for cp_parser_error.
2727 Having peeked a token of kind TOK1_KIND that might signify
2728 a conflict marker, peek successor tokens to determine
2729 if we actually do have a conflict marker.
2730 Specifically, we consider a run of 7 '<', '=' or '>' characters
2731 at the start of a line as a conflict marker.
2732 These come through the lexer as three pairs and a single,
2733 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2734 If it returns true, *OUT_LOC is written to with the location/range
2735 of the marker. */
2737 static bool
2738 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2739 location_t *out_loc)
2741 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2742 if (token2->type != tok1_kind)
2743 return false;
2744 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2745 if (token3->type != tok1_kind)
2746 return false;
2747 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2748 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2749 return false;
2751 /* It must be at the start of the line. */
2752 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2753 if (LOCATION_COLUMN (start_loc) != 1)
2754 return false;
2756 /* We have a conflict marker. Construct a location of the form:
2757 <<<<<<<
2758 ^~~~~~~
2759 with start == caret, finishing at the end of the marker. */
2760 location_t finish_loc = get_finish (token4->location);
2761 *out_loc = make_location (start_loc, start_loc, finish_loc);
2763 return true;
2766 /* If not parsing tentatively, issue a diagnostic of the form
2767 FILE:LINE: MESSAGE before TOKEN
2768 where TOKEN is the next token in the input stream. MESSAGE
2769 (specified by the caller) is usually of the form "expected
2770 OTHER-TOKEN". */
2772 static void
2773 cp_parser_error (cp_parser* parser, const char* gmsgid)
2775 if (!cp_parser_simulate_error (parser))
2777 cp_token *token = cp_lexer_peek_token (parser->lexer);
2778 /* This diagnostic makes more sense if it is tagged to the line
2779 of the token we just peeked at. */
2780 cp_lexer_set_source_position_from_token (token);
2782 if (token->type == CPP_PRAGMA)
2784 error_at (token->location,
2785 "%<#pragma%> is not allowed here");
2786 cp_parser_skip_to_pragma_eol (parser, token);
2787 return;
2790 /* If this is actually a conflict marker, report it as such. */
2791 if (token->type == CPP_LSHIFT
2792 || token->type == CPP_RSHIFT
2793 || token->type == CPP_EQ_EQ)
2795 location_t loc;
2796 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2798 error_at (loc, "version control conflict marker in file");
2799 return;
2803 c_parse_error (gmsgid,
2804 /* Because c_parser_error does not understand
2805 CPP_KEYWORD, keywords are treated like
2806 identifiers. */
2807 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2808 token->u.value, token->flags);
2812 /* Issue an error about name-lookup failing. NAME is the
2813 IDENTIFIER_NODE DECL is the result of
2814 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2815 the thing that we hoped to find. */
2817 static void
2818 cp_parser_name_lookup_error (cp_parser* parser,
2819 tree name,
2820 tree decl,
2821 name_lookup_error desired,
2822 location_t location)
2824 /* If name lookup completely failed, tell the user that NAME was not
2825 declared. */
2826 if (decl == error_mark_node)
2828 if (parser->scope && parser->scope != global_namespace)
2829 error_at (location, "%<%E::%E%> has not been declared",
2830 parser->scope, name);
2831 else if (parser->scope == global_namespace)
2832 error_at (location, "%<::%E%> has not been declared", name);
2833 else if (parser->object_scope
2834 && !CLASS_TYPE_P (parser->object_scope))
2835 error_at (location, "request for member %qE in non-class type %qT",
2836 name, parser->object_scope);
2837 else if (parser->object_scope)
2838 error_at (location, "%<%T::%E%> has not been declared",
2839 parser->object_scope, name);
2840 else
2841 error_at (location, "%qE has not been declared", name);
2843 else if (parser->scope && parser->scope != global_namespace)
2845 switch (desired)
2847 case NLE_TYPE:
2848 error_at (location, "%<%E::%E%> is not a type",
2849 parser->scope, name);
2850 break;
2851 case NLE_CXX98:
2852 error_at (location, "%<%E::%E%> is not a class or namespace",
2853 parser->scope, name);
2854 break;
2855 case NLE_NOT_CXX98:
2856 error_at (location,
2857 "%<%E::%E%> is not a class, namespace, or enumeration",
2858 parser->scope, name);
2859 break;
2860 default:
2861 gcc_unreachable ();
2865 else if (parser->scope == global_namespace)
2867 switch (desired)
2869 case NLE_TYPE:
2870 error_at (location, "%<::%E%> is not a type", name);
2871 break;
2872 case NLE_CXX98:
2873 error_at (location, "%<::%E%> is not a class or namespace", name);
2874 break;
2875 case NLE_NOT_CXX98:
2876 error_at (location,
2877 "%<::%E%> is not a class, namespace, or enumeration",
2878 name);
2879 break;
2880 default:
2881 gcc_unreachable ();
2884 else
2886 switch (desired)
2888 case NLE_TYPE:
2889 error_at (location, "%qE is not a type", name);
2890 break;
2891 case NLE_CXX98:
2892 error_at (location, "%qE is not a class or namespace", name);
2893 break;
2894 case NLE_NOT_CXX98:
2895 error_at (location,
2896 "%qE is not a class, namespace, or enumeration", name);
2897 break;
2898 default:
2899 gcc_unreachable ();
2904 /* If we are parsing tentatively, remember that an error has occurred
2905 during this tentative parse. Returns true if the error was
2906 simulated; false if a message should be issued by the caller. */
2908 static bool
2909 cp_parser_simulate_error (cp_parser* parser)
2911 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2913 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2914 return true;
2916 return false;
2919 /* This function is called when a type is defined. If type
2920 definitions are forbidden at this point, an error message is
2921 issued. */
2923 static bool
2924 cp_parser_check_type_definition (cp_parser* parser)
2926 /* If types are forbidden here, issue a message. */
2927 if (parser->type_definition_forbidden_message)
2929 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2930 in the message need to be interpreted. */
2931 error (parser->type_definition_forbidden_message);
2932 return false;
2934 return true;
2937 /* This function is called when the DECLARATOR is processed. The TYPE
2938 was a type defined in the decl-specifiers. If it is invalid to
2939 define a type in the decl-specifiers for DECLARATOR, an error is
2940 issued. TYPE_LOCATION is the location of TYPE and is used
2941 for error reporting. */
2943 static void
2944 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2945 tree type, location_t type_location)
2947 /* [dcl.fct] forbids type definitions in return types.
2948 Unfortunately, it's not easy to know whether or not we are
2949 processing a return type until after the fact. */
2950 while (declarator
2951 && (declarator->kind == cdk_pointer
2952 || declarator->kind == cdk_reference
2953 || declarator->kind == cdk_ptrmem))
2954 declarator = declarator->declarator;
2955 if (declarator
2956 && declarator->kind == cdk_function)
2958 error_at (type_location,
2959 "new types may not be defined in a return type");
2960 inform (type_location,
2961 "(perhaps a semicolon is missing after the definition of %qT)",
2962 type);
2966 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2967 "<" in any valid C++ program. If the next token is indeed "<",
2968 issue a message warning the user about what appears to be an
2969 invalid attempt to form a template-id. LOCATION is the location
2970 of the type-specifier (TYPE) */
2972 static void
2973 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2974 tree type,
2975 enum tag_types tag_type,
2976 location_t location)
2978 cp_token_position start = 0;
2980 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2982 if (TYPE_P (type))
2983 error_at (location, "%qT is not a template", type);
2984 else if (identifier_p (type))
2986 if (tag_type != none_type)
2987 error_at (location, "%qE is not a class template", type);
2988 else
2989 error_at (location, "%qE is not a template", type);
2991 else
2992 error_at (location, "invalid template-id");
2993 /* Remember the location of the invalid "<". */
2994 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2995 start = cp_lexer_token_position (parser->lexer, true);
2996 /* Consume the "<". */
2997 cp_lexer_consume_token (parser->lexer);
2998 /* Parse the template arguments. */
2999 cp_parser_enclosed_template_argument_list (parser);
3000 /* Permanently remove the invalid template arguments so that
3001 this error message is not issued again. */
3002 if (start)
3003 cp_lexer_purge_tokens_after (parser->lexer, start);
3007 /* If parsing an integral constant-expression, issue an error message
3008 about the fact that THING appeared and return true. Otherwise,
3009 return false. In either case, set
3010 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3012 static bool
3013 cp_parser_non_integral_constant_expression (cp_parser *parser,
3014 non_integral_constant thing)
3016 parser->non_integral_constant_expression_p = true;
3017 if (parser->integral_constant_expression_p)
3019 if (!parser->allow_non_integral_constant_expression_p)
3021 const char *msg = NULL;
3022 switch (thing)
3024 case NIC_FLOAT:
3025 error ("floating-point literal "
3026 "cannot appear in a constant-expression");
3027 return true;
3028 case NIC_CAST:
3029 error ("a cast to a type other than an integral or "
3030 "enumeration type cannot appear in a "
3031 "constant-expression");
3032 return true;
3033 case NIC_TYPEID:
3034 error ("%<typeid%> operator "
3035 "cannot appear in a constant-expression");
3036 return true;
3037 case NIC_NCC:
3038 error ("non-constant compound literals "
3039 "cannot appear in a constant-expression");
3040 return true;
3041 case NIC_FUNC_CALL:
3042 error ("a function call "
3043 "cannot appear in a constant-expression");
3044 return true;
3045 case NIC_INC:
3046 error ("an increment "
3047 "cannot appear in a constant-expression");
3048 return true;
3049 case NIC_DEC:
3050 error ("an decrement "
3051 "cannot appear in a constant-expression");
3052 return true;
3053 case NIC_ARRAY_REF:
3054 error ("an array reference "
3055 "cannot appear in a constant-expression");
3056 return true;
3057 case NIC_ADDR_LABEL:
3058 error ("the address of a label "
3059 "cannot appear in a constant-expression");
3060 return true;
3061 case NIC_OVERLOADED:
3062 error ("calls to overloaded operators "
3063 "cannot appear in a constant-expression");
3064 return true;
3065 case NIC_ASSIGNMENT:
3066 error ("an assignment cannot appear in a constant-expression");
3067 return true;
3068 case NIC_COMMA:
3069 error ("a comma operator "
3070 "cannot appear in a constant-expression");
3071 return true;
3072 case NIC_CONSTRUCTOR:
3073 error ("a call to a constructor "
3074 "cannot appear in a constant-expression");
3075 return true;
3076 case NIC_TRANSACTION:
3077 error ("a transaction expression "
3078 "cannot appear in a constant-expression");
3079 return true;
3080 case NIC_THIS:
3081 msg = "this";
3082 break;
3083 case NIC_FUNC_NAME:
3084 msg = "__FUNCTION__";
3085 break;
3086 case NIC_PRETTY_FUNC:
3087 msg = "__PRETTY_FUNCTION__";
3088 break;
3089 case NIC_C99_FUNC:
3090 msg = "__func__";
3091 break;
3092 case NIC_VA_ARG:
3093 msg = "va_arg";
3094 break;
3095 case NIC_ARROW:
3096 msg = "->";
3097 break;
3098 case NIC_POINT:
3099 msg = ".";
3100 break;
3101 case NIC_STAR:
3102 msg = "*";
3103 break;
3104 case NIC_ADDR:
3105 msg = "&";
3106 break;
3107 case NIC_PREINCREMENT:
3108 msg = "++";
3109 break;
3110 case NIC_PREDECREMENT:
3111 msg = "--";
3112 break;
3113 case NIC_NEW:
3114 msg = "new";
3115 break;
3116 case NIC_DEL:
3117 msg = "delete";
3118 break;
3119 default:
3120 gcc_unreachable ();
3122 if (msg)
3123 error ("%qs cannot appear in a constant-expression", msg);
3124 return true;
3127 return false;
3130 /* Emit a diagnostic for an invalid type name. This function commits
3131 to the current active tentative parse, if any. (Otherwise, the
3132 problematic construct might be encountered again later, resulting
3133 in duplicate error messages.) LOCATION is the location of ID. */
3135 static void
3136 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3137 location_t location)
3139 tree decl, ambiguous_decls;
3140 cp_parser_commit_to_tentative_parse (parser);
3141 /* Try to lookup the identifier. */
3142 decl = cp_parser_lookup_name (parser, id, none_type,
3143 /*is_template=*/false,
3144 /*is_namespace=*/false,
3145 /*check_dependency=*/true,
3146 &ambiguous_decls, location);
3147 if (ambiguous_decls)
3148 /* If the lookup was ambiguous, an error will already have
3149 been issued. */
3150 return;
3151 /* If the lookup found a template-name, it means that the user forgot
3152 to specify an argument list. Emit a useful error message. */
3153 if (DECL_TYPE_TEMPLATE_P (decl))
3155 error_at (location,
3156 "invalid use of template-name %qE without an argument list",
3157 decl);
3158 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3160 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3161 error_at (location, "invalid use of destructor %qD as a type", id);
3162 else if (TREE_CODE (decl) == TYPE_DECL)
3163 /* Something like 'unsigned A a;' */
3164 error_at (location, "invalid combination of multiple type-specifiers");
3165 else if (!parser->scope)
3167 /* Issue an error message. */
3168 const char *suggestion = NULL;
3169 if (TREE_CODE (id) == IDENTIFIER_NODE)
3170 suggestion = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME);
3171 if (suggestion)
3173 gcc_rich_location richloc (location);
3174 richloc.add_fixit_replace (suggestion);
3175 error_at_rich_loc (&richloc,
3176 "%qE does not name a type; did you mean %qs?",
3177 id, suggestion);
3179 else
3180 error_at (location, "%qE does not name a type", id);
3181 /* If we're in a template class, it's possible that the user was
3182 referring to a type from a base class. For example:
3184 template <typename T> struct A { typedef T X; };
3185 template <typename T> struct B : public A<T> { X x; };
3187 The user should have said "typename A<T>::X". */
3188 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3189 inform (location, "C++11 %<constexpr%> only available with "
3190 "-std=c++11 or -std=gnu++11");
3191 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3192 inform (location, "C++11 %<noexcept%> only available with "
3193 "-std=c++11 or -std=gnu++11");
3194 else if (cxx_dialect < cxx11
3195 && TREE_CODE (id) == IDENTIFIER_NODE
3196 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
3197 inform (location, "C++11 %<thread_local%> only available with "
3198 "-std=c++11 or -std=gnu++11");
3199 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3200 inform (location, "%<concept%> only available with -fconcepts");
3201 else if (processing_template_decl && current_class_type
3202 && TYPE_BINFO (current_class_type))
3204 tree b;
3206 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3208 b = TREE_CHAIN (b))
3210 tree base_type = BINFO_TYPE (b);
3211 if (CLASS_TYPE_P (base_type)
3212 && dependent_type_p (base_type))
3214 tree field;
3215 /* Go from a particular instantiation of the
3216 template (which will have an empty TYPE_FIELDs),
3217 to the main version. */
3218 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3219 for (field = TYPE_FIELDS (base_type);
3220 field;
3221 field = DECL_CHAIN (field))
3222 if (TREE_CODE (field) == TYPE_DECL
3223 && DECL_NAME (field) == id)
3225 inform (location,
3226 "(perhaps %<typename %T::%E%> was intended)",
3227 BINFO_TYPE (b), id);
3228 break;
3230 if (field)
3231 break;
3236 /* Here we diagnose qualified-ids where the scope is actually correct,
3237 but the identifier does not resolve to a valid type name. */
3238 else if (parser->scope != error_mark_node)
3240 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3242 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3243 error_at (location_of (id),
3244 "%qE in namespace %qE does not name a template type",
3245 id, parser->scope);
3246 else
3247 error_at (location_of (id),
3248 "%qE in namespace %qE does not name a type",
3249 id, parser->scope);
3250 if (DECL_P (decl))
3251 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3253 else if (CLASS_TYPE_P (parser->scope)
3254 && constructor_name_p (id, parser->scope))
3256 /* A<T>::A<T>() */
3257 error_at (location, "%<%T::%E%> names the constructor, not"
3258 " the type", parser->scope, id);
3259 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3260 error_at (location, "and %qT has no template constructors",
3261 parser->scope);
3263 else if (TYPE_P (parser->scope)
3264 && dependent_scope_p (parser->scope))
3265 error_at (location, "need %<typename%> before %<%T::%E%> because "
3266 "%qT is a dependent scope",
3267 parser->scope, id, parser->scope);
3268 else if (TYPE_P (parser->scope))
3270 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3271 error_at (location_of (id),
3272 "%qE in %q#T does not name a template type",
3273 id, parser->scope);
3274 else
3275 error_at (location_of (id),
3276 "%qE in %q#T does not name a type",
3277 id, parser->scope);
3278 if (DECL_P (decl))
3279 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3281 else
3282 gcc_unreachable ();
3286 /* Check for a common situation where a type-name should be present,
3287 but is not, and issue a sensible error message. Returns true if an
3288 invalid type-name was detected.
3290 The situation handled by this function are variable declarations of the
3291 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3292 Usually, `ID' should name a type, but if we got here it means that it
3293 does not. We try to emit the best possible error message depending on
3294 how exactly the id-expression looks like. */
3296 static bool
3297 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3299 tree id;
3300 cp_token *token = cp_lexer_peek_token (parser->lexer);
3302 /* Avoid duplicate error about ambiguous lookup. */
3303 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3305 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3306 if (next->type == CPP_NAME && next->error_reported)
3307 goto out;
3310 cp_parser_parse_tentatively (parser);
3311 id = cp_parser_id_expression (parser,
3312 /*template_keyword_p=*/false,
3313 /*check_dependency_p=*/true,
3314 /*template_p=*/NULL,
3315 /*declarator_p=*/true,
3316 /*optional_p=*/false);
3317 /* If the next token is a (, this is a function with no explicit return
3318 type, i.e. constructor, destructor or conversion op. */
3319 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3320 || TREE_CODE (id) == TYPE_DECL)
3322 cp_parser_abort_tentative_parse (parser);
3323 return false;
3325 if (!cp_parser_parse_definitely (parser))
3326 return false;
3328 /* Emit a diagnostic for the invalid type. */
3329 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3330 out:
3331 /* If we aren't in the middle of a declarator (i.e. in a
3332 parameter-declaration-clause), skip to the end of the declaration;
3333 there's no point in trying to process it. */
3334 if (!parser->in_declarator_p)
3335 cp_parser_skip_to_end_of_block_or_statement (parser);
3336 return true;
3339 /* Consume tokens up to, and including, the next non-nested closing `)'.
3340 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3341 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3342 found an unnested token of that type. */
3344 static int
3345 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3346 bool recovering,
3347 cpp_ttype or_ttype,
3348 bool consume_paren)
3350 unsigned paren_depth = 0;
3351 unsigned brace_depth = 0;
3352 unsigned square_depth = 0;
3354 if (recovering && or_ttype == CPP_EOF
3355 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3356 return 0;
3358 while (true)
3360 cp_token * token = cp_lexer_peek_token (parser->lexer);
3362 /* Have we found what we're looking for before the closing paren? */
3363 if (token->type == or_ttype && or_ttype != CPP_EOF
3364 && !brace_depth && !paren_depth && !square_depth)
3365 return -1;
3367 switch (token->type)
3369 case CPP_EOF:
3370 case CPP_PRAGMA_EOL:
3371 /* If we've run out of tokens, then there is no closing `)'. */
3372 return 0;
3374 /* This is good for lambda expression capture-lists. */
3375 case CPP_OPEN_SQUARE:
3376 ++square_depth;
3377 break;
3378 case CPP_CLOSE_SQUARE:
3379 if (!square_depth--)
3380 return 0;
3381 break;
3383 case CPP_SEMICOLON:
3384 /* This matches the processing in skip_to_end_of_statement. */
3385 if (!brace_depth)
3386 return 0;
3387 break;
3389 case CPP_OPEN_BRACE:
3390 ++brace_depth;
3391 break;
3392 case CPP_CLOSE_BRACE:
3393 if (!brace_depth--)
3394 return 0;
3395 break;
3397 case CPP_OPEN_PAREN:
3398 if (!brace_depth)
3399 ++paren_depth;
3400 break;
3402 case CPP_CLOSE_PAREN:
3403 if (!brace_depth && !paren_depth--)
3405 if (consume_paren)
3406 cp_lexer_consume_token (parser->lexer);
3407 return 1;
3409 break;
3411 default:
3412 break;
3415 /* Consume the token. */
3416 cp_lexer_consume_token (parser->lexer);
3420 /* Consume tokens up to, and including, the next non-nested closing `)'.
3421 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3422 are doing error recovery. Returns -1 if OR_COMMA is true and we
3423 found an unnested token of that type. */
3425 static int
3426 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3427 bool recovering,
3428 bool or_comma,
3429 bool consume_paren)
3431 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3432 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3433 ttype, consume_paren);
3436 /* Consume tokens until we reach the end of the current statement.
3437 Normally, that will be just before consuming a `;'. However, if a
3438 non-nested `}' comes first, then we stop before consuming that. */
3440 static void
3441 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3443 unsigned nesting_depth = 0;
3445 /* Unwind generic function template scope if necessary. */
3446 if (parser->fully_implicit_function_template_p)
3447 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3449 while (true)
3451 cp_token *token = cp_lexer_peek_token (parser->lexer);
3453 switch (token->type)
3455 case CPP_EOF:
3456 case CPP_PRAGMA_EOL:
3457 /* If we've run out of tokens, stop. */
3458 return;
3460 case CPP_SEMICOLON:
3461 /* If the next token is a `;', we have reached the end of the
3462 statement. */
3463 if (!nesting_depth)
3464 return;
3465 break;
3467 case CPP_CLOSE_BRACE:
3468 /* If this is a non-nested '}', stop before consuming it.
3469 That way, when confronted with something like:
3471 { 3 + }
3473 we stop before consuming the closing '}', even though we
3474 have not yet reached a `;'. */
3475 if (nesting_depth == 0)
3476 return;
3478 /* If it is the closing '}' for a block that we have
3479 scanned, stop -- but only after consuming the token.
3480 That way given:
3482 void f g () { ... }
3483 typedef int I;
3485 we will stop after the body of the erroneously declared
3486 function, but before consuming the following `typedef'
3487 declaration. */
3488 if (--nesting_depth == 0)
3490 cp_lexer_consume_token (parser->lexer);
3491 return;
3493 /* XXX Really fallthru? */
3494 /* FALLTHRU */
3496 case CPP_OPEN_BRACE:
3497 ++nesting_depth;
3498 break;
3500 default:
3501 break;
3504 /* Consume the token. */
3505 cp_lexer_consume_token (parser->lexer);
3509 /* This function is called at the end of a statement or declaration.
3510 If the next token is a semicolon, it is consumed; otherwise, error
3511 recovery is attempted. */
3513 static void
3514 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3516 /* Look for the trailing `;'. */
3517 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3519 /* If there is additional (erroneous) input, skip to the end of
3520 the statement. */
3521 cp_parser_skip_to_end_of_statement (parser);
3522 /* If the next token is now a `;', consume it. */
3523 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3524 cp_lexer_consume_token (parser->lexer);
3528 /* Skip tokens until we have consumed an entire block, or until we
3529 have consumed a non-nested `;'. */
3531 static void
3532 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3534 int nesting_depth = 0;
3536 /* Unwind generic function template scope if necessary. */
3537 if (parser->fully_implicit_function_template_p)
3538 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3540 while (nesting_depth >= 0)
3542 cp_token *token = cp_lexer_peek_token (parser->lexer);
3544 switch (token->type)
3546 case CPP_EOF:
3547 case CPP_PRAGMA_EOL:
3548 /* If we've run out of tokens, stop. */
3549 return;
3551 case CPP_SEMICOLON:
3552 /* Stop if this is an unnested ';'. */
3553 if (!nesting_depth)
3554 nesting_depth = -1;
3555 break;
3557 case CPP_CLOSE_BRACE:
3558 /* Stop if this is an unnested '}', or closes the outermost
3559 nesting level. */
3560 nesting_depth--;
3561 if (nesting_depth < 0)
3562 return;
3563 if (!nesting_depth)
3564 nesting_depth = -1;
3565 break;
3567 case CPP_OPEN_BRACE:
3568 /* Nest. */
3569 nesting_depth++;
3570 break;
3572 default:
3573 break;
3576 /* Consume the token. */
3577 cp_lexer_consume_token (parser->lexer);
3581 /* Skip tokens until a non-nested closing curly brace is the next
3582 token, or there are no more tokens. Return true in the first case,
3583 false otherwise. */
3585 static bool
3586 cp_parser_skip_to_closing_brace (cp_parser *parser)
3588 unsigned nesting_depth = 0;
3590 while (true)
3592 cp_token *token = cp_lexer_peek_token (parser->lexer);
3594 switch (token->type)
3596 case CPP_EOF:
3597 case CPP_PRAGMA_EOL:
3598 /* If we've run out of tokens, stop. */
3599 return false;
3601 case CPP_CLOSE_BRACE:
3602 /* If the next token is a non-nested `}', then we have reached
3603 the end of the current block. */
3604 if (nesting_depth-- == 0)
3605 return true;
3606 break;
3608 case CPP_OPEN_BRACE:
3609 /* If it the next token is a `{', then we are entering a new
3610 block. Consume the entire block. */
3611 ++nesting_depth;
3612 break;
3614 default:
3615 break;
3618 /* Consume the token. */
3619 cp_lexer_consume_token (parser->lexer);
3623 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3624 parameter is the PRAGMA token, allowing us to purge the entire pragma
3625 sequence. */
3627 static void
3628 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3630 cp_token *token;
3632 parser->lexer->in_pragma = false;
3635 token = cp_lexer_consume_token (parser->lexer);
3636 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3638 /* Ensure that the pragma is not parsed again. */
3639 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3642 /* Require pragma end of line, resyncing with it as necessary. The
3643 arguments are as for cp_parser_skip_to_pragma_eol. */
3645 static void
3646 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3648 parser->lexer->in_pragma = false;
3649 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3650 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3653 /* This is a simple wrapper around make_typename_type. When the id is
3654 an unresolved identifier node, we can provide a superior diagnostic
3655 using cp_parser_diagnose_invalid_type_name. */
3657 static tree
3658 cp_parser_make_typename_type (cp_parser *parser, tree id,
3659 location_t id_location)
3661 tree result;
3662 if (identifier_p (id))
3664 result = make_typename_type (parser->scope, id, typename_type,
3665 /*complain=*/tf_none);
3666 if (result == error_mark_node)
3667 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3668 return result;
3670 return make_typename_type (parser->scope, id, typename_type, tf_error);
3673 /* This is a wrapper around the
3674 make_{pointer,ptrmem,reference}_declarator functions that decides
3675 which one to call based on the CODE and CLASS_TYPE arguments. The
3676 CODE argument should be one of the values returned by
3677 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3678 appertain to the pointer or reference. */
3680 static cp_declarator *
3681 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3682 cp_cv_quals cv_qualifiers,
3683 cp_declarator *target,
3684 tree attributes)
3686 if (code == ERROR_MARK)
3687 return cp_error_declarator;
3689 if (code == INDIRECT_REF)
3690 if (class_type == NULL_TREE)
3691 return make_pointer_declarator (cv_qualifiers, target, attributes);
3692 else
3693 return make_ptrmem_declarator (cv_qualifiers, class_type,
3694 target, attributes);
3695 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3696 return make_reference_declarator (cv_qualifiers, target,
3697 false, attributes);
3698 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3699 return make_reference_declarator (cv_qualifiers, target,
3700 true, attributes);
3701 gcc_unreachable ();
3704 /* Create a new C++ parser. */
3706 static cp_parser *
3707 cp_parser_new (void)
3709 cp_parser *parser;
3710 cp_lexer *lexer;
3711 unsigned i;
3713 /* cp_lexer_new_main is called before doing GC allocation because
3714 cp_lexer_new_main might load a PCH file. */
3715 lexer = cp_lexer_new_main ();
3717 /* Initialize the binops_by_token so that we can get the tree
3718 directly from the token. */
3719 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3720 binops_by_token[binops[i].token_type] = binops[i];
3722 parser = ggc_cleared_alloc<cp_parser> ();
3723 parser->lexer = lexer;
3724 parser->context = cp_parser_context_new (NULL);
3726 /* For now, we always accept GNU extensions. */
3727 parser->allow_gnu_extensions_p = 1;
3729 /* The `>' token is a greater-than operator, not the end of a
3730 template-id. */
3731 parser->greater_than_is_operator_p = true;
3733 parser->default_arg_ok_p = true;
3735 /* We are not parsing a constant-expression. */
3736 parser->integral_constant_expression_p = false;
3737 parser->allow_non_integral_constant_expression_p = false;
3738 parser->non_integral_constant_expression_p = false;
3740 /* Local variable names are not forbidden. */
3741 parser->local_variables_forbidden_p = false;
3743 /* We are not processing an `extern "C"' declaration. */
3744 parser->in_unbraced_linkage_specification_p = false;
3746 /* We are not processing a declarator. */
3747 parser->in_declarator_p = false;
3749 /* We are not processing a template-argument-list. */
3750 parser->in_template_argument_list_p = false;
3752 /* We are not in an iteration statement. */
3753 parser->in_statement = 0;
3755 /* We are not in a switch statement. */
3756 parser->in_switch_statement_p = false;
3758 /* We are not parsing a type-id inside an expression. */
3759 parser->in_type_id_in_expr_p = false;
3761 /* Declarations aren't implicitly extern "C". */
3762 parser->implicit_extern_c = false;
3764 /* String literals should be translated to the execution character set. */
3765 parser->translate_strings_p = true;
3767 /* We are not parsing a function body. */
3768 parser->in_function_body = false;
3770 /* We can correct until told otherwise. */
3771 parser->colon_corrects_to_scope_p = true;
3773 /* The unparsed function queue is empty. */
3774 push_unparsed_function_queues (parser);
3776 /* There are no classes being defined. */
3777 parser->num_classes_being_defined = 0;
3779 /* No template parameters apply. */
3780 parser->num_template_parameter_lists = 0;
3782 /* Special parsing data structures. */
3783 parser->omp_declare_simd = NULL;
3784 parser->cilk_simd_fn_info = NULL;
3785 parser->oacc_routine = NULL;
3787 /* Not declaring an implicit function template. */
3788 parser->auto_is_implicit_function_template_parm_p = false;
3789 parser->fully_implicit_function_template_p = false;
3790 parser->implicit_template_parms = 0;
3791 parser->implicit_template_scope = 0;
3793 /* Allow constrained-type-specifiers. */
3794 parser->prevent_constrained_type_specifiers = 0;
3796 return parser;
3799 /* Create a cp_lexer structure which will emit the tokens in CACHE
3800 and push it onto the parser's lexer stack. This is used for delayed
3801 parsing of in-class method bodies and default arguments, and should
3802 not be confused with tentative parsing. */
3803 static void
3804 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3806 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3807 lexer->next = parser->lexer;
3808 parser->lexer = lexer;
3810 /* Move the current source position to that of the first token in the
3811 new lexer. */
3812 cp_lexer_set_source_position_from_token (lexer->next_token);
3815 /* Pop the top lexer off the parser stack. This is never used for the
3816 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3817 static void
3818 cp_parser_pop_lexer (cp_parser *parser)
3820 cp_lexer *lexer = parser->lexer;
3821 parser->lexer = lexer->next;
3822 cp_lexer_destroy (lexer);
3824 /* Put the current source position back where it was before this
3825 lexer was pushed. */
3826 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3829 /* Lexical conventions [gram.lex] */
3831 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3832 identifier. */
3834 static cp_expr
3835 cp_parser_identifier (cp_parser* parser)
3837 cp_token *token;
3839 /* Look for the identifier. */
3840 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3841 /* Return the value. */
3842 if (token)
3843 return cp_expr (token->u.value, token->location);
3844 else
3845 return error_mark_node;
3848 /* Parse a sequence of adjacent string constants. Returns a
3849 TREE_STRING representing the combined, nul-terminated string
3850 constant. If TRANSLATE is true, translate the string to the
3851 execution character set. If WIDE_OK is true, a wide string is
3852 invalid here.
3854 C++98 [lex.string] says that if a narrow string literal token is
3855 adjacent to a wide string literal token, the behavior is undefined.
3856 However, C99 6.4.5p4 says that this results in a wide string literal.
3857 We follow C99 here, for consistency with the C front end.
3859 This code is largely lifted from lex_string() in c-lex.c.
3861 FUTURE: ObjC++ will need to handle @-strings here. */
3862 static cp_expr
3863 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3864 bool lookup_udlit = true)
3866 tree value;
3867 size_t count;
3868 struct obstack str_ob;
3869 cpp_string str, istr, *strs;
3870 cp_token *tok;
3871 enum cpp_ttype type, curr_type;
3872 int have_suffix_p = 0;
3873 tree string_tree;
3874 tree suffix_id = NULL_TREE;
3875 bool curr_tok_is_userdef_p = false;
3877 tok = cp_lexer_peek_token (parser->lexer);
3878 if (!cp_parser_is_string_literal (tok))
3880 cp_parser_error (parser, "expected string-literal");
3881 return error_mark_node;
3884 location_t loc = tok->location;
3886 if (cpp_userdef_string_p (tok->type))
3888 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3889 curr_type = cpp_userdef_string_remove_type (tok->type);
3890 curr_tok_is_userdef_p = true;
3892 else
3894 string_tree = tok->u.value;
3895 curr_type = tok->type;
3897 type = curr_type;
3899 /* Try to avoid the overhead of creating and destroying an obstack
3900 for the common case of just one string. */
3901 if (!cp_parser_is_string_literal
3902 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3904 cp_lexer_consume_token (parser->lexer);
3906 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3907 str.len = TREE_STRING_LENGTH (string_tree);
3908 count = 1;
3910 if (curr_tok_is_userdef_p)
3912 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3913 have_suffix_p = 1;
3914 curr_type = cpp_userdef_string_remove_type (tok->type);
3916 else
3917 curr_type = tok->type;
3919 strs = &str;
3921 else
3923 location_t last_tok_loc = tok->location;
3924 gcc_obstack_init (&str_ob);
3925 count = 0;
3929 cp_lexer_consume_token (parser->lexer);
3930 count++;
3931 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3932 str.len = TREE_STRING_LENGTH (string_tree);
3934 if (curr_tok_is_userdef_p)
3936 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3937 if (have_suffix_p == 0)
3939 suffix_id = curr_suffix_id;
3940 have_suffix_p = 1;
3942 else if (have_suffix_p == 1
3943 && curr_suffix_id != suffix_id)
3945 error ("inconsistent user-defined literal suffixes"
3946 " %qD and %qD in string literal",
3947 suffix_id, curr_suffix_id);
3948 have_suffix_p = -1;
3950 curr_type = cpp_userdef_string_remove_type (tok->type);
3952 else
3953 curr_type = tok->type;
3955 if (type != curr_type)
3957 if (type == CPP_STRING)
3958 type = curr_type;
3959 else if (curr_type != CPP_STRING)
3961 rich_location rich_loc (line_table, tok->location);
3962 rich_loc.add_range (last_tok_loc, false);
3963 error_at_rich_loc (&rich_loc,
3964 "unsupported non-standard concatenation "
3965 "of string literals");
3969 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3971 last_tok_loc = tok->location;
3973 tok = cp_lexer_peek_token (parser->lexer);
3974 if (cpp_userdef_string_p (tok->type))
3976 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3977 curr_type = cpp_userdef_string_remove_type (tok->type);
3978 curr_tok_is_userdef_p = true;
3980 else
3982 string_tree = tok->u.value;
3983 curr_type = tok->type;
3984 curr_tok_is_userdef_p = false;
3987 while (cp_parser_is_string_literal (tok));
3989 /* A string literal built by concatenation has its caret=start at
3990 the start of the initial string, and its finish at the finish of
3991 the final string literal. */
3992 loc = make_location (loc, loc, get_finish (last_tok_loc));
3994 strs = (cpp_string *) obstack_finish (&str_ob);
3997 if (type != CPP_STRING && !wide_ok)
3999 cp_parser_error (parser, "a wide string is invalid in this context");
4000 type = CPP_STRING;
4003 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4004 (parse_in, strs, count, &istr, type))
4006 value = build_string (istr.len, (const char *)istr.text);
4007 free (CONST_CAST (unsigned char *, istr.text));
4009 switch (type)
4011 default:
4012 case CPP_STRING:
4013 case CPP_UTF8STRING:
4014 TREE_TYPE (value) = char_array_type_node;
4015 break;
4016 case CPP_STRING16:
4017 TREE_TYPE (value) = char16_array_type_node;
4018 break;
4019 case CPP_STRING32:
4020 TREE_TYPE (value) = char32_array_type_node;
4021 break;
4022 case CPP_WSTRING:
4023 TREE_TYPE (value) = wchar_array_type_node;
4024 break;
4027 value = fix_string_type (value);
4029 if (have_suffix_p)
4031 tree literal = build_userdef_literal (suffix_id, value,
4032 OT_NONE, NULL_TREE);
4033 if (lookup_udlit)
4034 value = cp_parser_userdef_string_literal (literal);
4035 else
4036 value = literal;
4039 else
4040 /* cpp_interpret_string has issued an error. */
4041 value = error_mark_node;
4043 if (count > 1)
4044 obstack_free (&str_ob, 0);
4046 return cp_expr (value, loc);
4049 /* Look up a literal operator with the name and the exact arguments. */
4051 static tree
4052 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4054 tree decl, fns;
4055 decl = lookup_name (name);
4056 if (!decl || !is_overloaded_fn (decl))
4057 return error_mark_node;
4059 for (fns = decl; fns; fns = OVL_NEXT (fns))
4061 unsigned int ix;
4062 bool found = true;
4063 tree fn = OVL_CURRENT (fns);
4064 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4065 if (parmtypes != NULL_TREE)
4067 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4068 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4070 tree tparm = TREE_VALUE (parmtypes);
4071 tree targ = TREE_TYPE ((*args)[ix]);
4072 bool ptr = TYPE_PTR_P (tparm);
4073 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4074 if ((ptr || arr || !same_type_p (tparm, targ))
4075 && (!ptr || !arr
4076 || !same_type_p (TREE_TYPE (tparm),
4077 TREE_TYPE (targ))))
4078 found = false;
4080 if (found
4081 && ix == vec_safe_length (args)
4082 /* May be this should be sufficient_parms_p instead,
4083 depending on how exactly should user-defined literals
4084 work in presence of default arguments on the literal
4085 operator parameters. */
4086 && parmtypes == void_list_node)
4087 return decl;
4091 return error_mark_node;
4094 /* Parse a user-defined char constant. Returns a call to a user-defined
4095 literal operator taking the character as an argument. */
4097 static cp_expr
4098 cp_parser_userdef_char_literal (cp_parser *parser)
4100 cp_token *token = cp_lexer_consume_token (parser->lexer);
4101 tree literal = token->u.value;
4102 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4103 tree value = USERDEF_LITERAL_VALUE (literal);
4104 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4105 tree decl, result;
4107 /* Build up a call to the user-defined operator */
4108 /* Lookup the name we got back from the id-expression. */
4109 vec<tree, va_gc> *args = make_tree_vector ();
4110 vec_safe_push (args, value);
4111 decl = lookup_literal_operator (name, args);
4112 if (!decl || decl == error_mark_node)
4114 error ("unable to find character literal operator %qD with %qT argument",
4115 name, TREE_TYPE (value));
4116 release_tree_vector (args);
4117 return error_mark_node;
4119 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4120 release_tree_vector (args);
4121 return result;
4124 /* A subroutine of cp_parser_userdef_numeric_literal to
4125 create a char... template parameter pack from a string node. */
4127 static tree
4128 make_char_string_pack (tree value)
4130 tree charvec;
4131 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4132 const char *str = TREE_STRING_POINTER (value);
4133 int i, len = TREE_STRING_LENGTH (value) - 1;
4134 tree argvec = make_tree_vec (1);
4136 /* Fill in CHARVEC with all of the parameters. */
4137 charvec = make_tree_vec (len);
4138 for (i = 0; i < len; ++i)
4139 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4141 /* Build the argument packs. */
4142 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4143 TREE_TYPE (argpack) = char_type_node;
4145 TREE_VEC_ELT (argvec, 0) = argpack;
4147 return argvec;
4150 /* A subroutine of cp_parser_userdef_numeric_literal to
4151 create a char... template parameter pack from a string node. */
4153 static tree
4154 make_string_pack (tree value)
4156 tree charvec;
4157 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4158 const unsigned char *str
4159 = (const unsigned char *) TREE_STRING_POINTER (value);
4160 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4161 int len = TREE_STRING_LENGTH (value) / sz - 1;
4162 tree argvec = make_tree_vec (2);
4164 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4165 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4167 /* First template parm is character type. */
4168 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4170 /* Fill in CHARVEC with all of the parameters. */
4171 charvec = make_tree_vec (len);
4172 for (int i = 0; i < len; ++i)
4173 TREE_VEC_ELT (charvec, i)
4174 = double_int_to_tree (str_char_type_node,
4175 double_int::from_buffer (str + i * sz, sz));
4177 /* Build the argument packs. */
4178 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4179 TREE_TYPE (argpack) = str_char_type_node;
4181 TREE_VEC_ELT (argvec, 1) = argpack;
4183 return argvec;
4186 /* Parse a user-defined numeric constant. returns a call to a user-defined
4187 literal operator. */
4189 static cp_expr
4190 cp_parser_userdef_numeric_literal (cp_parser *parser)
4192 cp_token *token = cp_lexer_consume_token (parser->lexer);
4193 tree literal = token->u.value;
4194 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4195 tree value = USERDEF_LITERAL_VALUE (literal);
4196 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4197 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4198 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4199 tree decl, result;
4200 vec<tree, va_gc> *args;
4202 /* Look for a literal operator taking the exact type of numeric argument
4203 as the literal value. */
4204 args = make_tree_vector ();
4205 vec_safe_push (args, value);
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);
4212 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4214 warning_at (token->location, OPT_Woverflow,
4215 "integer literal exceeds range of %qT type",
4216 long_long_unsigned_type_node);
4218 else
4220 if (overflow > 0)
4221 warning_at (token->location, OPT_Woverflow,
4222 "floating literal exceeds range of %qT type",
4223 long_double_type_node);
4224 else if (overflow < 0)
4225 warning_at (token->location, OPT_Woverflow,
4226 "floating literal truncated to zero");
4229 release_tree_vector (args);
4230 return result;
4232 release_tree_vector (args);
4234 /* If the numeric argument didn't work, look for a raw literal
4235 operator taking a const char* argument consisting of the number
4236 in string format. */
4237 args = make_tree_vector ();
4238 vec_safe_push (args, num_string);
4239 decl = lookup_literal_operator (name, args);
4240 if (decl && decl != error_mark_node)
4242 result = finish_call_expr (decl, &args, false, true,
4243 tf_warning_or_error);
4244 release_tree_vector (args);
4245 return result;
4247 release_tree_vector (args);
4249 /* If the raw literal didn't work, look for a non-type template
4250 function with parameter pack char.... Call the function with
4251 template parameter characters representing the number. */
4252 args = make_tree_vector ();
4253 decl = lookup_literal_operator (name, args);
4254 if (decl && decl != error_mark_node)
4256 tree tmpl_args = make_char_string_pack (num_string);
4257 decl = lookup_template_function (decl, tmpl_args);
4258 result = finish_call_expr (decl, &args, false, true,
4259 tf_warning_or_error);
4260 release_tree_vector (args);
4261 return result;
4264 release_tree_vector (args);
4266 error ("unable to find numeric literal operator %qD", name);
4267 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4268 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4269 "to enable more built-in suffixes");
4270 return error_mark_node;
4273 /* Parse a user-defined string constant. Returns a call to a user-defined
4274 literal operator taking a character pointer and the length of the string
4275 as arguments. */
4277 static tree
4278 cp_parser_userdef_string_literal (tree literal)
4280 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4281 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4282 tree value = USERDEF_LITERAL_VALUE (literal);
4283 int len = TREE_STRING_LENGTH (value)
4284 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4285 tree decl, result;
4286 vec<tree, va_gc> *args;
4288 /* Build up a call to the user-defined operator. */
4289 /* Lookup the name we got back from the id-expression. */
4290 args = make_tree_vector ();
4291 vec_safe_push (args, value);
4292 vec_safe_push (args, build_int_cst (size_type_node, len));
4293 decl = lookup_literal_operator (name, args);
4295 if (decl && decl != error_mark_node)
4297 result = finish_call_expr (decl, &args, false, true,
4298 tf_warning_or_error);
4299 release_tree_vector (args);
4300 return result;
4302 release_tree_vector (args);
4304 /* Look for a template function with typename parameter CharT
4305 and parameter pack CharT... Call the function with
4306 template parameter characters representing the string. */
4307 args = make_tree_vector ();
4308 decl = lookup_literal_operator (name, args);
4309 if (decl && decl != error_mark_node)
4311 tree tmpl_args = make_string_pack (value);
4312 decl = lookup_template_function (decl, tmpl_args);
4313 result = finish_call_expr (decl, &args, false, true,
4314 tf_warning_or_error);
4315 release_tree_vector (args);
4316 return result;
4318 release_tree_vector (args);
4320 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4321 name, TREE_TYPE (value), size_type_node);
4322 return error_mark_node;
4326 /* Basic concepts [gram.basic] */
4328 /* Parse a translation-unit.
4330 translation-unit:
4331 declaration-seq [opt]
4333 Returns TRUE if all went well. */
4335 static bool
4336 cp_parser_translation_unit (cp_parser* parser)
4338 /* The address of the first non-permanent object on the declarator
4339 obstack. */
4340 static void *declarator_obstack_base;
4342 bool success;
4344 /* Create the declarator obstack, if necessary. */
4345 if (!cp_error_declarator)
4347 gcc_obstack_init (&declarator_obstack);
4348 /* Create the error declarator. */
4349 cp_error_declarator = make_declarator (cdk_error);
4350 /* Create the empty parameter list. */
4351 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4352 /* Remember where the base of the declarator obstack lies. */
4353 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4356 cp_parser_declaration_seq_opt (parser);
4358 /* If there are no tokens left then all went well. */
4359 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4361 /* Get rid of the token array; we don't need it any more. */
4362 cp_lexer_destroy (parser->lexer);
4363 parser->lexer = NULL;
4365 /* This file might have been a context that's implicitly extern
4366 "C". If so, pop the lang context. (Only relevant for PCH.) */
4367 if (parser->implicit_extern_c)
4369 pop_lang_context ();
4370 parser->implicit_extern_c = false;
4373 /* Finish up. */
4374 finish_translation_unit ();
4376 success = true;
4378 else
4380 cp_parser_error (parser, "expected declaration");
4381 success = false;
4384 /* Make sure the declarator obstack was fully cleaned up. */
4385 gcc_assert (obstack_next_free (&declarator_obstack)
4386 == declarator_obstack_base);
4388 /* All went well. */
4389 return success;
4392 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4393 decltype context. */
4395 static inline tsubst_flags_t
4396 complain_flags (bool decltype_p)
4398 tsubst_flags_t complain = tf_warning_or_error;
4399 if (decltype_p)
4400 complain |= tf_decltype;
4401 return complain;
4404 /* We're about to parse a collection of statements. If we're currently
4405 parsing tentatively, set up a firewall so that any nested
4406 cp_parser_commit_to_tentative_parse won't affect the current context. */
4408 static cp_token_position
4409 cp_parser_start_tentative_firewall (cp_parser *parser)
4411 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4412 return 0;
4414 cp_parser_parse_tentatively (parser);
4415 cp_parser_commit_to_topmost_tentative_parse (parser);
4416 return cp_lexer_token_position (parser->lexer, false);
4419 /* We've finished parsing the collection of statements. Wrap up the
4420 firewall and replace the relevant tokens with the parsed form. */
4422 static void
4423 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4424 tree expr)
4426 if (!start)
4427 return;
4429 /* Finish the firewall level. */
4430 cp_parser_parse_definitely (parser);
4431 /* And remember the result of the parse for when we try again. */
4432 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4433 token->type = CPP_PREPARSED_EXPR;
4434 token->u.value = expr;
4435 token->keyword = RID_MAX;
4436 cp_lexer_purge_tokens_after (parser->lexer, start);
4439 /* Like the above functions, but let the user modify the tokens. Used by
4440 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4441 later parses, so it makes sense to localize the effects of
4442 cp_parser_commit_to_tentative_parse. */
4444 struct tentative_firewall
4446 cp_parser *parser;
4447 bool set;
4449 tentative_firewall (cp_parser *p): parser(p)
4451 /* If we're currently parsing tentatively, start a committed level as a
4452 firewall and then an inner tentative parse. */
4453 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4455 cp_parser_parse_tentatively (parser);
4456 cp_parser_commit_to_topmost_tentative_parse (parser);
4457 cp_parser_parse_tentatively (parser);
4461 ~tentative_firewall()
4463 if (set)
4465 /* Finish the inner tentative parse and the firewall, propagating any
4466 uncommitted error state to the outer tentative parse. */
4467 bool err = cp_parser_error_occurred (parser);
4468 cp_parser_parse_definitely (parser);
4469 cp_parser_parse_definitely (parser);
4470 if (err)
4471 cp_parser_simulate_error (parser);
4476 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4477 enclosing parentheses. */
4479 static cp_expr
4480 cp_parser_statement_expr (cp_parser *parser)
4482 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4484 /* Consume the '('. */
4485 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4486 cp_lexer_consume_token (parser->lexer);
4487 /* Start the statement-expression. */
4488 tree expr = begin_stmt_expr ();
4489 /* Parse the compound-statement. */
4490 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4491 /* Finish up. */
4492 expr = finish_stmt_expr (expr, false);
4493 /* Consume the ')'. */
4494 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4495 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4496 cp_parser_skip_to_end_of_statement (parser);
4498 cp_parser_end_tentative_firewall (parser, start, expr);
4499 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4500 return cp_expr (expr, combined_loc);
4503 /* Expressions [gram.expr] */
4505 /* Parse a fold-operator.
4507 fold-operator:
4508 - * / % ^ & | = < > << >>
4509 = -= *= /= %= ^= &= |= <<= >>=
4510 == != <= >= && || , .* ->*
4512 This returns the tree code corresponding to the matched operator
4513 as an int. When the current token matches a compound assignment
4514 opertor, the resulting tree code is the negative value of the
4515 non-assignment operator. */
4517 static int
4518 cp_parser_fold_operator (cp_token *token)
4520 switch (token->type)
4522 case CPP_PLUS: return PLUS_EXPR;
4523 case CPP_MINUS: return MINUS_EXPR;
4524 case CPP_MULT: return MULT_EXPR;
4525 case CPP_DIV: return TRUNC_DIV_EXPR;
4526 case CPP_MOD: return TRUNC_MOD_EXPR;
4527 case CPP_XOR: return BIT_XOR_EXPR;
4528 case CPP_AND: return BIT_AND_EXPR;
4529 case CPP_OR: return BIT_IOR_EXPR;
4530 case CPP_LSHIFT: return LSHIFT_EXPR;
4531 case CPP_RSHIFT: return RSHIFT_EXPR;
4533 case CPP_EQ: return -NOP_EXPR;
4534 case CPP_PLUS_EQ: return -PLUS_EXPR;
4535 case CPP_MINUS_EQ: return -MINUS_EXPR;
4536 case CPP_MULT_EQ: return -MULT_EXPR;
4537 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4538 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4539 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4540 case CPP_AND_EQ: return -BIT_AND_EXPR;
4541 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4542 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4543 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4545 case CPP_EQ_EQ: return EQ_EXPR;
4546 case CPP_NOT_EQ: return NE_EXPR;
4547 case CPP_LESS: return LT_EXPR;
4548 case CPP_GREATER: return GT_EXPR;
4549 case CPP_LESS_EQ: return LE_EXPR;
4550 case CPP_GREATER_EQ: return GE_EXPR;
4552 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4553 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4555 case CPP_COMMA: return COMPOUND_EXPR;
4557 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4558 case CPP_DEREF_STAR: return MEMBER_REF;
4560 default: return ERROR_MARK;
4564 /* Returns true if CODE indicates a binary expression, which is not allowed in
4565 the LHS of a fold-expression. More codes will need to be added to use this
4566 function in other contexts. */
4568 static bool
4569 is_binary_op (tree_code code)
4571 switch (code)
4573 case PLUS_EXPR:
4574 case POINTER_PLUS_EXPR:
4575 case MINUS_EXPR:
4576 case MULT_EXPR:
4577 case TRUNC_DIV_EXPR:
4578 case TRUNC_MOD_EXPR:
4579 case BIT_XOR_EXPR:
4580 case BIT_AND_EXPR:
4581 case BIT_IOR_EXPR:
4582 case LSHIFT_EXPR:
4583 case RSHIFT_EXPR:
4585 case MODOP_EXPR:
4587 case EQ_EXPR:
4588 case NE_EXPR:
4589 case LE_EXPR:
4590 case GE_EXPR:
4591 case LT_EXPR:
4592 case GT_EXPR:
4594 case TRUTH_ANDIF_EXPR:
4595 case TRUTH_ORIF_EXPR:
4597 case COMPOUND_EXPR:
4599 case DOTSTAR_EXPR:
4600 case MEMBER_REF:
4601 return true;
4603 default:
4604 return false;
4608 /* If the next token is a suitable fold operator, consume it and return as
4609 the function above. */
4611 static int
4612 cp_parser_fold_operator (cp_parser *parser)
4614 cp_token* token = cp_lexer_peek_token (parser->lexer);
4615 int code = cp_parser_fold_operator (token);
4616 if (code != ERROR_MARK)
4617 cp_lexer_consume_token (parser->lexer);
4618 return code;
4621 /* Parse a fold-expression.
4623 fold-expression:
4624 ( ... folding-operator cast-expression)
4625 ( cast-expression folding-operator ... )
4626 ( cast-expression folding operator ... folding-operator cast-expression)
4628 Note that the '(' and ')' are matched in primary expression. */
4630 static cp_expr
4631 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4633 cp_id_kind pidk;
4635 // Left fold.
4636 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4638 cp_lexer_consume_token (parser->lexer);
4639 int op = cp_parser_fold_operator (parser);
4640 if (op == ERROR_MARK)
4642 cp_parser_error (parser, "expected binary operator");
4643 return error_mark_node;
4646 tree expr = cp_parser_cast_expression (parser, false, false,
4647 false, &pidk);
4648 if (expr == error_mark_node)
4649 return error_mark_node;
4650 return finish_left_unary_fold_expr (expr, op);
4653 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4654 int op = cp_parser_fold_operator (parser);
4655 if (op == ERROR_MARK)
4657 cp_parser_error (parser, "expected binary operator");
4658 return error_mark_node;
4661 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4663 cp_parser_error (parser, "expected ...");
4664 return error_mark_node;
4666 cp_lexer_consume_token (parser->lexer);
4668 /* The operands of a fold-expression are cast-expressions, so binary or
4669 conditional expressions are not allowed. We check this here to avoid
4670 tentative parsing. */
4671 if (is_binary_op (TREE_CODE (expr1)))
4672 error_at (location_of (expr1),
4673 "binary expression in operand of fold-expression");
4674 else if (TREE_CODE (expr1) == COND_EXPR)
4675 error_at (location_of (expr1),
4676 "conditional expression in operand of fold-expression");
4678 // Right fold.
4679 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4680 return finish_right_unary_fold_expr (expr1, op);
4682 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4684 cp_parser_error (parser, "mismatched operator in fold-expression");
4685 return error_mark_node;
4687 cp_lexer_consume_token (parser->lexer);
4689 // Binary left or right fold.
4690 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4691 if (expr2 == error_mark_node)
4692 return error_mark_node;
4693 return finish_binary_fold_expr (expr1, expr2, op);
4696 /* Parse a primary-expression.
4698 primary-expression:
4699 literal
4700 this
4701 ( expression )
4702 id-expression
4703 lambda-expression (C++11)
4705 GNU Extensions:
4707 primary-expression:
4708 ( compound-statement )
4709 __builtin_va_arg ( assignment-expression , type-id )
4710 __builtin_offsetof ( type-id , offsetof-expression )
4712 C++ Extensions:
4713 __has_nothrow_assign ( type-id )
4714 __has_nothrow_constructor ( type-id )
4715 __has_nothrow_copy ( type-id )
4716 __has_trivial_assign ( type-id )
4717 __has_trivial_constructor ( type-id )
4718 __has_trivial_copy ( type-id )
4719 __has_trivial_destructor ( type-id )
4720 __has_virtual_destructor ( type-id )
4721 __is_abstract ( type-id )
4722 __is_base_of ( type-id , type-id )
4723 __is_class ( type-id )
4724 __is_empty ( type-id )
4725 __is_enum ( type-id )
4726 __is_final ( type-id )
4727 __is_literal_type ( type-id )
4728 __is_pod ( type-id )
4729 __is_polymorphic ( type-id )
4730 __is_std_layout ( type-id )
4731 __is_trivial ( type-id )
4732 __is_union ( type-id )
4734 Objective-C++ Extension:
4736 primary-expression:
4737 objc-expression
4739 literal:
4740 __null
4742 ADDRESS_P is true iff this expression was immediately preceded by
4743 "&" and therefore might denote a pointer-to-member. CAST_P is true
4744 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4745 true iff this expression is a template argument.
4747 Returns a representation of the expression. Upon return, *IDK
4748 indicates what kind of id-expression (if any) was present. */
4750 static cp_expr
4751 cp_parser_primary_expression (cp_parser *parser,
4752 bool address_p,
4753 bool cast_p,
4754 bool template_arg_p,
4755 bool decltype_p,
4756 cp_id_kind *idk)
4758 cp_token *token = NULL;
4760 /* Assume the primary expression is not an id-expression. */
4761 *idk = CP_ID_KIND_NONE;
4763 /* Peek at the next token. */
4764 token = cp_lexer_peek_token (parser->lexer);
4765 switch ((int) token->type)
4767 /* literal:
4768 integer-literal
4769 character-literal
4770 floating-literal
4771 string-literal
4772 boolean-literal
4773 pointer-literal
4774 user-defined-literal */
4775 case CPP_CHAR:
4776 case CPP_CHAR16:
4777 case CPP_CHAR32:
4778 case CPP_WCHAR:
4779 case CPP_UTF8CHAR:
4780 case CPP_NUMBER:
4781 case CPP_PREPARSED_EXPR:
4782 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4783 return cp_parser_userdef_numeric_literal (parser);
4784 token = cp_lexer_consume_token (parser->lexer);
4785 if (TREE_CODE (token->u.value) == FIXED_CST)
4787 error_at (token->location,
4788 "fixed-point types not supported in C++");
4789 return error_mark_node;
4791 /* Floating-point literals are only allowed in an integral
4792 constant expression if they are cast to an integral or
4793 enumeration type. */
4794 if (TREE_CODE (token->u.value) == REAL_CST
4795 && parser->integral_constant_expression_p
4796 && pedantic)
4798 /* CAST_P will be set even in invalid code like "int(2.7 +
4799 ...)". Therefore, we have to check that the next token
4800 is sure to end the cast. */
4801 if (cast_p)
4803 cp_token *next_token;
4805 next_token = cp_lexer_peek_token (parser->lexer);
4806 if (/* The comma at the end of an
4807 enumerator-definition. */
4808 next_token->type != CPP_COMMA
4809 /* The curly brace at the end of an enum-specifier. */
4810 && next_token->type != CPP_CLOSE_BRACE
4811 /* The end of a statement. */
4812 && next_token->type != CPP_SEMICOLON
4813 /* The end of the cast-expression. */
4814 && next_token->type != CPP_CLOSE_PAREN
4815 /* The end of an array bound. */
4816 && next_token->type != CPP_CLOSE_SQUARE
4817 /* The closing ">" in a template-argument-list. */
4818 && (next_token->type != CPP_GREATER
4819 || parser->greater_than_is_operator_p)
4820 /* C++0x only: A ">>" treated like two ">" tokens,
4821 in a template-argument-list. */
4822 && (next_token->type != CPP_RSHIFT
4823 || (cxx_dialect == cxx98)
4824 || parser->greater_than_is_operator_p))
4825 cast_p = false;
4828 /* If we are within a cast, then the constraint that the
4829 cast is to an integral or enumeration type will be
4830 checked at that point. If we are not within a cast, then
4831 this code is invalid. */
4832 if (!cast_p)
4833 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4835 return cp_expr (token->u.value, token->location);
4837 case CPP_CHAR_USERDEF:
4838 case CPP_CHAR16_USERDEF:
4839 case CPP_CHAR32_USERDEF:
4840 case CPP_WCHAR_USERDEF:
4841 case CPP_UTF8CHAR_USERDEF:
4842 return cp_parser_userdef_char_literal (parser);
4844 case CPP_STRING:
4845 case CPP_STRING16:
4846 case CPP_STRING32:
4847 case CPP_WSTRING:
4848 case CPP_UTF8STRING:
4849 case CPP_STRING_USERDEF:
4850 case CPP_STRING16_USERDEF:
4851 case CPP_STRING32_USERDEF:
4852 case CPP_WSTRING_USERDEF:
4853 case CPP_UTF8STRING_USERDEF:
4854 /* ??? Should wide strings be allowed when parser->translate_strings_p
4855 is false (i.e. in attributes)? If not, we can kill the third
4856 argument to cp_parser_string_literal. */
4857 return cp_parser_string_literal (parser,
4858 parser->translate_strings_p,
4859 true);
4861 case CPP_OPEN_PAREN:
4862 /* If we see `( { ' then we are looking at the beginning of
4863 a GNU statement-expression. */
4864 if (cp_parser_allow_gnu_extensions_p (parser)
4865 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4867 /* Statement-expressions are not allowed by the standard. */
4868 pedwarn (token->location, OPT_Wpedantic,
4869 "ISO C++ forbids braced-groups within expressions");
4871 /* And they're not allowed outside of a function-body; you
4872 cannot, for example, write:
4874 int i = ({ int j = 3; j + 1; });
4876 at class or namespace scope. */
4877 if (!parser->in_function_body
4878 || parser->in_template_argument_list_p)
4880 error_at (token->location,
4881 "statement-expressions are not allowed outside "
4882 "functions nor in template-argument lists");
4883 cp_parser_skip_to_end_of_block_or_statement (parser);
4884 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4885 cp_lexer_consume_token (parser->lexer);
4886 return error_mark_node;
4888 else
4889 return cp_parser_statement_expr (parser);
4891 /* Otherwise it's a normal parenthesized expression. */
4893 cp_expr expr;
4894 bool saved_greater_than_is_operator_p;
4896 location_t open_paren_loc = token->location;
4898 /* Consume the `('. */
4899 cp_lexer_consume_token (parser->lexer);
4900 /* Within a parenthesized expression, a `>' token is always
4901 the greater-than operator. */
4902 saved_greater_than_is_operator_p
4903 = parser->greater_than_is_operator_p;
4904 parser->greater_than_is_operator_p = true;
4906 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4907 /* Left fold expression. */
4908 expr = NULL_TREE;
4909 else
4910 /* Parse the parenthesized expression. */
4911 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4913 token = cp_lexer_peek_token (parser->lexer);
4914 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
4916 expr = cp_parser_fold_expression (parser, expr);
4917 if (expr != error_mark_node
4918 && cxx_dialect < cxx1z
4919 && !in_system_header_at (input_location))
4920 pedwarn (input_location, 0, "fold-expressions only available "
4921 "with -std=c++1z or -std=gnu++1z");
4923 else
4924 /* Let the front end know that this expression was
4925 enclosed in parentheses. This matters in case, for
4926 example, the expression is of the form `A::B', since
4927 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4928 not. */
4929 expr = finish_parenthesized_expr (expr);
4931 /* DR 705: Wrapping an unqualified name in parentheses
4932 suppresses arg-dependent lookup. We want to pass back
4933 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4934 (c++/37862), but none of the others. */
4935 if (*idk != CP_ID_KIND_QUALIFIED)
4936 *idk = CP_ID_KIND_NONE;
4938 /* The `>' token might be the end of a template-id or
4939 template-parameter-list now. */
4940 parser->greater_than_is_operator_p
4941 = saved_greater_than_is_operator_p;
4943 /* Consume the `)'. */
4944 token = cp_lexer_peek_token (parser->lexer);
4945 location_t close_paren_loc = token->location;
4946 expr.set_range (open_paren_loc, close_paren_loc);
4947 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)
4948 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4949 cp_parser_skip_to_end_of_statement (parser);
4951 return expr;
4954 case CPP_OPEN_SQUARE:
4956 if (c_dialect_objc ())
4958 /* We might have an Objective-C++ message. */
4959 cp_parser_parse_tentatively (parser);
4960 tree msg = cp_parser_objc_message_expression (parser);
4961 /* If that works out, we're done ... */
4962 if (cp_parser_parse_definitely (parser))
4963 return msg;
4964 /* ... else, fall though to see if it's a lambda. */
4966 cp_expr lam = cp_parser_lambda_expression (parser);
4967 /* Don't warn about a failed tentative parse. */
4968 if (cp_parser_error_occurred (parser))
4969 return error_mark_node;
4970 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4971 return lam;
4974 case CPP_OBJC_STRING:
4975 if (c_dialect_objc ())
4976 /* We have an Objective-C++ string literal. */
4977 return cp_parser_objc_expression (parser);
4978 cp_parser_error (parser, "expected primary-expression");
4979 return error_mark_node;
4981 case CPP_KEYWORD:
4982 switch (token->keyword)
4984 /* These two are the boolean literals. */
4985 case RID_TRUE:
4986 cp_lexer_consume_token (parser->lexer);
4987 return cp_expr (boolean_true_node, token->location);
4988 case RID_FALSE:
4989 cp_lexer_consume_token (parser->lexer);
4990 return cp_expr (boolean_false_node, token->location);
4992 /* The `__null' literal. */
4993 case RID_NULL:
4994 cp_lexer_consume_token (parser->lexer);
4995 return cp_expr (null_node, token->location);
4997 /* The `nullptr' literal. */
4998 case RID_NULLPTR:
4999 cp_lexer_consume_token (parser->lexer);
5000 return cp_expr (nullptr_node, token->location);
5002 /* Recognize the `this' keyword. */
5003 case RID_THIS:
5004 cp_lexer_consume_token (parser->lexer);
5005 if (parser->local_variables_forbidden_p)
5007 error_at (token->location,
5008 "%<this%> may not be used in this context");
5009 return error_mark_node;
5011 /* Pointers cannot appear in constant-expressions. */
5012 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5013 return error_mark_node;
5014 return cp_expr (finish_this_expr (), token->location);
5016 /* The `operator' keyword can be the beginning of an
5017 id-expression. */
5018 case RID_OPERATOR:
5019 goto id_expression;
5021 case RID_FUNCTION_NAME:
5022 case RID_PRETTY_FUNCTION_NAME:
5023 case RID_C99_FUNCTION_NAME:
5025 non_integral_constant name;
5027 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5028 __func__ are the names of variables -- but they are
5029 treated specially. Therefore, they are handled here,
5030 rather than relying on the generic id-expression logic
5031 below. Grammatically, these names are id-expressions.
5033 Consume the token. */
5034 token = cp_lexer_consume_token (parser->lexer);
5036 switch (token->keyword)
5038 case RID_FUNCTION_NAME:
5039 name = NIC_FUNC_NAME;
5040 break;
5041 case RID_PRETTY_FUNCTION_NAME:
5042 name = NIC_PRETTY_FUNC;
5043 break;
5044 case RID_C99_FUNCTION_NAME:
5045 name = NIC_C99_FUNC;
5046 break;
5047 default:
5048 gcc_unreachable ();
5051 if (cp_parser_non_integral_constant_expression (parser, name))
5052 return error_mark_node;
5054 /* Look up the name. */
5055 return finish_fname (token->u.value);
5058 case RID_VA_ARG:
5060 tree expression;
5061 tree type;
5062 source_location type_location;
5063 location_t start_loc
5064 = cp_lexer_peek_token (parser->lexer)->location;
5065 /* The `__builtin_va_arg' construct is used to handle
5066 `va_arg'. Consume the `__builtin_va_arg' token. */
5067 cp_lexer_consume_token (parser->lexer);
5068 /* Look for the opening `('. */
5069 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5070 /* Now, parse the assignment-expression. */
5071 expression = cp_parser_assignment_expression (parser);
5072 /* Look for the `,'. */
5073 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5074 type_location = cp_lexer_peek_token (parser->lexer)->location;
5075 /* Parse the type-id. */
5077 type_id_in_expr_sentinel s (parser);
5078 type = cp_parser_type_id (parser);
5080 /* Look for the closing `)'. */
5081 location_t finish_loc
5082 = cp_lexer_peek_token (parser->lexer)->location;
5083 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5084 /* Using `va_arg' in a constant-expression is not
5085 allowed. */
5086 if (cp_parser_non_integral_constant_expression (parser,
5087 NIC_VA_ARG))
5088 return error_mark_node;
5089 /* Construct a location of the form:
5090 __builtin_va_arg (v, int)
5091 ~~~~~~~~~~~~~~~~~~~~~^~~~
5092 with the caret at the type, ranging from the start of the
5093 "__builtin_va_arg" token to the close paren. */
5094 location_t combined_loc
5095 = make_location (type_location, start_loc, finish_loc);
5096 return build_x_va_arg (combined_loc, expression, type);
5099 case RID_OFFSETOF:
5100 return cp_parser_builtin_offsetof (parser);
5102 case RID_HAS_NOTHROW_ASSIGN:
5103 case RID_HAS_NOTHROW_CONSTRUCTOR:
5104 case RID_HAS_NOTHROW_COPY:
5105 case RID_HAS_TRIVIAL_ASSIGN:
5106 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5107 case RID_HAS_TRIVIAL_COPY:
5108 case RID_HAS_TRIVIAL_DESTRUCTOR:
5109 case RID_HAS_VIRTUAL_DESTRUCTOR:
5110 case RID_IS_ABSTRACT:
5111 case RID_IS_BASE_OF:
5112 case RID_IS_CLASS:
5113 case RID_IS_EMPTY:
5114 case RID_IS_ENUM:
5115 case RID_IS_FINAL:
5116 case RID_IS_LITERAL_TYPE:
5117 case RID_IS_POD:
5118 case RID_IS_POLYMORPHIC:
5119 case RID_IS_SAME_AS:
5120 case RID_IS_STD_LAYOUT:
5121 case RID_IS_TRIVIAL:
5122 case RID_IS_TRIVIALLY_ASSIGNABLE:
5123 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5124 case RID_IS_TRIVIALLY_COPYABLE:
5125 case RID_IS_UNION:
5126 return cp_parser_trait_expr (parser, token->keyword);
5128 // C++ concepts
5129 case RID_REQUIRES:
5130 return cp_parser_requires_expression (parser);
5132 /* Objective-C++ expressions. */
5133 case RID_AT_ENCODE:
5134 case RID_AT_PROTOCOL:
5135 case RID_AT_SELECTOR:
5136 return cp_parser_objc_expression (parser);
5138 case RID_TEMPLATE:
5139 if (parser->in_function_body
5140 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5141 == CPP_LESS))
5143 error_at (token->location,
5144 "a template declaration cannot appear at block scope");
5145 cp_parser_skip_to_end_of_block_or_statement (parser);
5146 return error_mark_node;
5148 /* FALLTHRU */
5149 default:
5150 cp_parser_error (parser, "expected primary-expression");
5151 return error_mark_node;
5154 /* An id-expression can start with either an identifier, a
5155 `::' as the beginning of a qualified-id, or the "operator"
5156 keyword. */
5157 case CPP_NAME:
5158 case CPP_SCOPE:
5159 case CPP_TEMPLATE_ID:
5160 case CPP_NESTED_NAME_SPECIFIER:
5162 id_expression:
5163 cp_expr id_expression;
5164 cp_expr decl;
5165 const char *error_msg;
5166 bool template_p;
5167 bool done;
5168 cp_token *id_expr_token;
5170 /* Parse the id-expression. */
5171 id_expression
5172 = cp_parser_id_expression (parser,
5173 /*template_keyword_p=*/false,
5174 /*check_dependency_p=*/true,
5175 &template_p,
5176 /*declarator_p=*/false,
5177 /*optional_p=*/false);
5178 if (id_expression == error_mark_node)
5179 return error_mark_node;
5180 id_expr_token = token;
5181 token = cp_lexer_peek_token (parser->lexer);
5182 done = (token->type != CPP_OPEN_SQUARE
5183 && token->type != CPP_OPEN_PAREN
5184 && token->type != CPP_DOT
5185 && token->type != CPP_DEREF
5186 && token->type != CPP_PLUS_PLUS
5187 && token->type != CPP_MINUS_MINUS);
5188 /* If we have a template-id, then no further lookup is
5189 required. If the template-id was for a template-class, we
5190 will sometimes have a TYPE_DECL at this point. */
5191 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5192 || TREE_CODE (id_expression) == TYPE_DECL)
5193 decl = id_expression;
5194 /* Look up the name. */
5195 else
5197 tree ambiguous_decls;
5199 /* If we already know that this lookup is ambiguous, then
5200 we've already issued an error message; there's no reason
5201 to check again. */
5202 if (id_expr_token->type == CPP_NAME
5203 && id_expr_token->error_reported)
5205 cp_parser_simulate_error (parser);
5206 return error_mark_node;
5209 decl = cp_parser_lookup_name (parser, id_expression,
5210 none_type,
5211 template_p,
5212 /*is_namespace=*/false,
5213 /*check_dependency=*/true,
5214 &ambiguous_decls,
5215 id_expr_token->location);
5216 /* If the lookup was ambiguous, an error will already have
5217 been issued. */
5218 if (ambiguous_decls)
5219 return error_mark_node;
5221 /* In Objective-C++, we may have an Objective-C 2.0
5222 dot-syntax for classes here. */
5223 if (c_dialect_objc ()
5224 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5225 && TREE_CODE (decl) == TYPE_DECL
5226 && objc_is_class_name (decl))
5228 tree component;
5229 cp_lexer_consume_token (parser->lexer);
5230 component = cp_parser_identifier (parser);
5231 if (component == error_mark_node)
5232 return error_mark_node;
5234 tree result = objc_build_class_component_ref (id_expression,
5235 component);
5236 /* Build a location of the form:
5237 expr.component
5238 ~~~~~^~~~~~~~~
5239 with caret at the start of the component name (at
5240 input_location), ranging from the start of the id_expression
5241 to the end of the component name. */
5242 location_t combined_loc
5243 = make_location (input_location, id_expression.get_start (),
5244 get_finish (input_location));
5245 protected_set_expr_location (result, combined_loc);
5246 return result;
5249 /* In Objective-C++, an instance variable (ivar) may be preferred
5250 to whatever cp_parser_lookup_name() found.
5251 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5252 rest of c-family, we have to do a little extra work to preserve
5253 any location information in cp_expr "decl". Given that
5254 objc_lookup_ivar is implemented in "c-family" and "objc", we
5255 have a trip through the pure "tree" type, rather than cp_expr.
5256 Naively copying it back to "decl" would implicitly give the
5257 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5258 store an EXPR_LOCATION. Hence we only update "decl" (and
5259 hence its location_t) if we get back a different tree node. */
5260 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5261 id_expression);
5262 if (decl_tree != decl.get_value ())
5263 decl = cp_expr (decl_tree);
5265 /* If name lookup gives us a SCOPE_REF, then the
5266 qualifying scope was dependent. */
5267 if (TREE_CODE (decl) == SCOPE_REF)
5269 /* At this point, we do not know if DECL is a valid
5270 integral constant expression. We assume that it is
5271 in fact such an expression, so that code like:
5273 template <int N> struct A {
5274 int a[B<N>::i];
5277 is accepted. At template-instantiation time, we
5278 will check that B<N>::i is actually a constant. */
5279 return decl;
5281 /* Check to see if DECL is a local variable in a context
5282 where that is forbidden. */
5283 if (parser->local_variables_forbidden_p
5284 && local_variable_p (decl))
5286 /* It might be that we only found DECL because we are
5287 trying to be generous with pre-ISO scoping rules.
5288 For example, consider:
5290 int i;
5291 void g() {
5292 for (int i = 0; i < 10; ++i) {}
5293 extern void f(int j = i);
5296 Here, name look up will originally find the out
5297 of scope `i'. We need to issue a warning message,
5298 but then use the global `i'. */
5299 decl = check_for_out_of_scope_variable (decl);
5300 if (local_variable_p (decl))
5302 error_at (id_expr_token->location,
5303 "local variable %qD may not appear in this context",
5304 decl.get_value ());
5305 return error_mark_node;
5310 decl = (finish_id_expression
5311 (id_expression, decl, parser->scope,
5312 idk,
5313 parser->integral_constant_expression_p,
5314 parser->allow_non_integral_constant_expression_p,
5315 &parser->non_integral_constant_expression_p,
5316 template_p, done, address_p,
5317 template_arg_p,
5318 &error_msg,
5319 id_expr_token->location));
5320 if (error_msg)
5321 cp_parser_error (parser, error_msg);
5322 decl.set_location (id_expr_token->location);
5323 return decl;
5326 /* Anything else is an error. */
5327 default:
5328 cp_parser_error (parser, "expected primary-expression");
5329 return error_mark_node;
5333 static inline cp_expr
5334 cp_parser_primary_expression (cp_parser *parser,
5335 bool address_p,
5336 bool cast_p,
5337 bool template_arg_p,
5338 cp_id_kind *idk)
5340 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5341 /*decltype*/false, idk);
5344 /* Parse an id-expression.
5346 id-expression:
5347 unqualified-id
5348 qualified-id
5350 qualified-id:
5351 :: [opt] nested-name-specifier template [opt] unqualified-id
5352 :: identifier
5353 :: operator-function-id
5354 :: template-id
5356 Return a representation of the unqualified portion of the
5357 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5358 a `::' or nested-name-specifier.
5360 Often, if the id-expression was a qualified-id, the caller will
5361 want to make a SCOPE_REF to represent the qualified-id. This
5362 function does not do this in order to avoid wastefully creating
5363 SCOPE_REFs when they are not required.
5365 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5366 `template' keyword.
5368 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5369 uninstantiated templates.
5371 If *TEMPLATE_P is non-NULL, it is set to true iff the
5372 `template' keyword is used to explicitly indicate that the entity
5373 named is a template.
5375 If DECLARATOR_P is true, the id-expression is appearing as part of
5376 a declarator, rather than as part of an expression. */
5378 static cp_expr
5379 cp_parser_id_expression (cp_parser *parser,
5380 bool template_keyword_p,
5381 bool check_dependency_p,
5382 bool *template_p,
5383 bool declarator_p,
5384 bool optional_p)
5386 bool global_scope_p;
5387 bool nested_name_specifier_p;
5389 /* Assume the `template' keyword was not used. */
5390 if (template_p)
5391 *template_p = template_keyword_p;
5393 /* Look for the optional `::' operator. */
5394 global_scope_p
5395 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
5396 != NULL_TREE);
5397 /* Look for the optional nested-name-specifier. */
5398 nested_name_specifier_p
5399 = (cp_parser_nested_name_specifier_opt (parser,
5400 /*typename_keyword_p=*/false,
5401 check_dependency_p,
5402 /*type_p=*/false,
5403 declarator_p)
5404 != NULL_TREE);
5405 /* If there is a nested-name-specifier, then we are looking at
5406 the first qualified-id production. */
5407 if (nested_name_specifier_p)
5409 tree saved_scope;
5410 tree saved_object_scope;
5411 tree saved_qualifying_scope;
5412 tree unqualified_id;
5413 bool is_template;
5415 /* See if the next token is the `template' keyword. */
5416 if (!template_p)
5417 template_p = &is_template;
5418 *template_p = cp_parser_optional_template_keyword (parser);
5419 /* Name lookup we do during the processing of the
5420 unqualified-id might obliterate SCOPE. */
5421 saved_scope = parser->scope;
5422 saved_object_scope = parser->object_scope;
5423 saved_qualifying_scope = parser->qualifying_scope;
5424 /* Process the final unqualified-id. */
5425 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5426 check_dependency_p,
5427 declarator_p,
5428 /*optional_p=*/false);
5429 /* Restore the SAVED_SCOPE for our caller. */
5430 parser->scope = saved_scope;
5431 parser->object_scope = saved_object_scope;
5432 parser->qualifying_scope = saved_qualifying_scope;
5434 return unqualified_id;
5436 /* Otherwise, if we are in global scope, then we are looking at one
5437 of the other qualified-id productions. */
5438 else if (global_scope_p)
5440 cp_token *token;
5441 tree id;
5443 /* Peek at the next token. */
5444 token = cp_lexer_peek_token (parser->lexer);
5446 /* If it's an identifier, and the next token is not a "<", then
5447 we can avoid the template-id case. This is an optimization
5448 for this common case. */
5449 if (token->type == CPP_NAME
5450 && !cp_parser_nth_token_starts_template_argument_list_p
5451 (parser, 2))
5452 return cp_parser_identifier (parser);
5454 cp_parser_parse_tentatively (parser);
5455 /* Try a template-id. */
5456 id = cp_parser_template_id (parser,
5457 /*template_keyword_p=*/false,
5458 /*check_dependency_p=*/true,
5459 none_type,
5460 declarator_p);
5461 /* If that worked, we're done. */
5462 if (cp_parser_parse_definitely (parser))
5463 return id;
5465 /* Peek at the next token. (Changes in the token buffer may
5466 have invalidated the pointer obtained above.) */
5467 token = cp_lexer_peek_token (parser->lexer);
5469 switch (token->type)
5471 case CPP_NAME:
5472 return cp_parser_identifier (parser);
5474 case CPP_KEYWORD:
5475 if (token->keyword == RID_OPERATOR)
5476 return cp_parser_operator_function_id (parser);
5477 /* Fall through. */
5479 default:
5480 cp_parser_error (parser, "expected id-expression");
5481 return error_mark_node;
5484 else
5485 return cp_parser_unqualified_id (parser, template_keyword_p,
5486 /*check_dependency_p=*/true,
5487 declarator_p,
5488 optional_p);
5491 /* Parse an unqualified-id.
5493 unqualified-id:
5494 identifier
5495 operator-function-id
5496 conversion-function-id
5497 ~ class-name
5498 template-id
5500 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5501 keyword, in a construct like `A::template ...'.
5503 Returns a representation of unqualified-id. For the `identifier'
5504 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5505 production a BIT_NOT_EXPR is returned; the operand of the
5506 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5507 other productions, see the documentation accompanying the
5508 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5509 names are looked up in uninstantiated templates. If DECLARATOR_P
5510 is true, the unqualified-id is appearing as part of a declarator,
5511 rather than as part of an expression. */
5513 static cp_expr
5514 cp_parser_unqualified_id (cp_parser* parser,
5515 bool template_keyword_p,
5516 bool check_dependency_p,
5517 bool declarator_p,
5518 bool optional_p)
5520 cp_token *token;
5522 /* Peek at the next token. */
5523 token = cp_lexer_peek_token (parser->lexer);
5525 switch ((int) token->type)
5527 case CPP_NAME:
5529 tree id;
5531 /* We don't know yet whether or not this will be a
5532 template-id. */
5533 cp_parser_parse_tentatively (parser);
5534 /* Try a template-id. */
5535 id = cp_parser_template_id (parser, template_keyword_p,
5536 check_dependency_p,
5537 none_type,
5538 declarator_p);
5539 /* If it worked, we're done. */
5540 if (cp_parser_parse_definitely (parser))
5541 return id;
5542 /* Otherwise, it's an ordinary identifier. */
5543 return cp_parser_identifier (parser);
5546 case CPP_TEMPLATE_ID:
5547 return cp_parser_template_id (parser, template_keyword_p,
5548 check_dependency_p,
5549 none_type,
5550 declarator_p);
5552 case CPP_COMPL:
5554 tree type_decl;
5555 tree qualifying_scope;
5556 tree object_scope;
5557 tree scope;
5558 bool done;
5560 /* Consume the `~' token. */
5561 cp_lexer_consume_token (parser->lexer);
5562 /* Parse the class-name. The standard, as written, seems to
5563 say that:
5565 template <typename T> struct S { ~S (); };
5566 template <typename T> S<T>::~S() {}
5568 is invalid, since `~' must be followed by a class-name, but
5569 `S<T>' is dependent, and so not known to be a class.
5570 That's not right; we need to look in uninstantiated
5571 templates. A further complication arises from:
5573 template <typename T> void f(T t) {
5574 t.T::~T();
5577 Here, it is not possible to look up `T' in the scope of `T'
5578 itself. We must look in both the current scope, and the
5579 scope of the containing complete expression.
5581 Yet another issue is:
5583 struct S {
5584 int S;
5585 ~S();
5588 S::~S() {}
5590 The standard does not seem to say that the `S' in `~S'
5591 should refer to the type `S' and not the data member
5592 `S::S'. */
5594 /* DR 244 says that we look up the name after the "~" in the
5595 same scope as we looked up the qualifying name. That idea
5596 isn't fully worked out; it's more complicated than that. */
5597 scope = parser->scope;
5598 object_scope = parser->object_scope;
5599 qualifying_scope = parser->qualifying_scope;
5601 /* Check for invalid scopes. */
5602 if (scope == error_mark_node)
5604 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5605 cp_lexer_consume_token (parser->lexer);
5606 return error_mark_node;
5608 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5610 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5611 error_at (token->location,
5612 "scope %qT before %<~%> is not a class-name",
5613 scope);
5614 cp_parser_simulate_error (parser);
5615 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5616 cp_lexer_consume_token (parser->lexer);
5617 return error_mark_node;
5619 gcc_assert (!scope || TYPE_P (scope));
5621 /* If the name is of the form "X::~X" it's OK even if X is a
5622 typedef. */
5623 token = cp_lexer_peek_token (parser->lexer);
5624 if (scope
5625 && token->type == CPP_NAME
5626 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5627 != CPP_LESS)
5628 && (token->u.value == TYPE_IDENTIFIER (scope)
5629 || (CLASS_TYPE_P (scope)
5630 && constructor_name_p (token->u.value, scope))))
5632 cp_lexer_consume_token (parser->lexer);
5633 return build_nt (BIT_NOT_EXPR, scope);
5636 /* ~auto means the destructor of whatever the object is. */
5637 if (cp_parser_is_keyword (token, RID_AUTO))
5639 if (cxx_dialect < cxx14)
5640 pedwarn (input_location, 0,
5641 "%<~auto%> only available with "
5642 "-std=c++14 or -std=gnu++14");
5643 cp_lexer_consume_token (parser->lexer);
5644 return build_nt (BIT_NOT_EXPR, make_auto ());
5647 /* If there was an explicit qualification (S::~T), first look
5648 in the scope given by the qualification (i.e., S).
5650 Note: in the calls to cp_parser_class_name below we pass
5651 typename_type so that lookup finds the injected-class-name
5652 rather than the constructor. */
5653 done = false;
5654 type_decl = NULL_TREE;
5655 if (scope)
5657 cp_parser_parse_tentatively (parser);
5658 type_decl = cp_parser_class_name (parser,
5659 /*typename_keyword_p=*/false,
5660 /*template_keyword_p=*/false,
5661 typename_type,
5662 /*check_dependency=*/false,
5663 /*class_head_p=*/false,
5664 declarator_p);
5665 if (cp_parser_parse_definitely (parser))
5666 done = true;
5668 /* In "N::S::~S", look in "N" as well. */
5669 if (!done && scope && qualifying_scope)
5671 cp_parser_parse_tentatively (parser);
5672 parser->scope = qualifying_scope;
5673 parser->object_scope = NULL_TREE;
5674 parser->qualifying_scope = NULL_TREE;
5675 type_decl
5676 = cp_parser_class_name (parser,
5677 /*typename_keyword_p=*/false,
5678 /*template_keyword_p=*/false,
5679 typename_type,
5680 /*check_dependency=*/false,
5681 /*class_head_p=*/false,
5682 declarator_p);
5683 if (cp_parser_parse_definitely (parser))
5684 done = true;
5686 /* In "p->S::~T", look in the scope given by "*p" as well. */
5687 else if (!done && object_scope)
5689 cp_parser_parse_tentatively (parser);
5690 parser->scope = object_scope;
5691 parser->object_scope = NULL_TREE;
5692 parser->qualifying_scope = NULL_TREE;
5693 type_decl
5694 = cp_parser_class_name (parser,
5695 /*typename_keyword_p=*/false,
5696 /*template_keyword_p=*/false,
5697 typename_type,
5698 /*check_dependency=*/false,
5699 /*class_head_p=*/false,
5700 declarator_p);
5701 if (cp_parser_parse_definitely (parser))
5702 done = true;
5704 /* Look in the surrounding context. */
5705 if (!done)
5707 parser->scope = NULL_TREE;
5708 parser->object_scope = NULL_TREE;
5709 parser->qualifying_scope = NULL_TREE;
5710 if (processing_template_decl)
5711 cp_parser_parse_tentatively (parser);
5712 type_decl
5713 = cp_parser_class_name (parser,
5714 /*typename_keyword_p=*/false,
5715 /*template_keyword_p=*/false,
5716 typename_type,
5717 /*check_dependency=*/false,
5718 /*class_head_p=*/false,
5719 declarator_p);
5720 if (processing_template_decl
5721 && ! cp_parser_parse_definitely (parser))
5723 /* We couldn't find a type with this name. If we're parsing
5724 tentatively, fail and try something else. */
5725 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5727 cp_parser_simulate_error (parser);
5728 return error_mark_node;
5730 /* Otherwise, accept it and check for a match at instantiation
5731 time. */
5732 type_decl = cp_parser_identifier (parser);
5733 if (type_decl != error_mark_node)
5734 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5735 return type_decl;
5738 /* If an error occurred, assume that the name of the
5739 destructor is the same as the name of the qualifying
5740 class. That allows us to keep parsing after running
5741 into ill-formed destructor names. */
5742 if (type_decl == error_mark_node && scope)
5743 return build_nt (BIT_NOT_EXPR, scope);
5744 else if (type_decl == error_mark_node)
5745 return error_mark_node;
5747 /* Check that destructor name and scope match. */
5748 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5750 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5751 error_at (token->location,
5752 "declaration of %<~%T%> as member of %qT",
5753 type_decl, scope);
5754 cp_parser_simulate_error (parser);
5755 return error_mark_node;
5758 /* [class.dtor]
5760 A typedef-name that names a class shall not be used as the
5761 identifier in the declarator for a destructor declaration. */
5762 if (declarator_p
5763 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5764 && !DECL_SELF_REFERENCE_P (type_decl)
5765 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5766 error_at (token->location,
5767 "typedef-name %qD used as destructor declarator",
5768 type_decl);
5770 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5773 case CPP_KEYWORD:
5774 if (token->keyword == RID_OPERATOR)
5776 cp_expr id;
5778 /* This could be a template-id, so we try that first. */
5779 cp_parser_parse_tentatively (parser);
5780 /* Try a template-id. */
5781 id = cp_parser_template_id (parser, template_keyword_p,
5782 /*check_dependency_p=*/true,
5783 none_type,
5784 declarator_p);
5785 /* If that worked, we're done. */
5786 if (cp_parser_parse_definitely (parser))
5787 return id;
5788 /* We still don't know whether we're looking at an
5789 operator-function-id or a conversion-function-id. */
5790 cp_parser_parse_tentatively (parser);
5791 /* Try an operator-function-id. */
5792 id = cp_parser_operator_function_id (parser);
5793 /* If that didn't work, try a conversion-function-id. */
5794 if (!cp_parser_parse_definitely (parser))
5795 id = cp_parser_conversion_function_id (parser);
5796 else if (UDLIT_OPER_P (id))
5798 /* 17.6.3.3.5 */
5799 const char *name = UDLIT_OP_SUFFIX (id);
5800 if (name[0] != '_' && !in_system_header_at (input_location)
5801 && declarator_p)
5802 warning (0, "literal operator suffixes not preceded by %<_%>"
5803 " are reserved for future standardization");
5806 return id;
5808 /* Fall through. */
5810 default:
5811 if (optional_p)
5812 return NULL_TREE;
5813 cp_parser_error (parser, "expected unqualified-id");
5814 return error_mark_node;
5818 /* Parse an (optional) nested-name-specifier.
5820 nested-name-specifier: [C++98]
5821 class-or-namespace-name :: nested-name-specifier [opt]
5822 class-or-namespace-name :: template nested-name-specifier [opt]
5824 nested-name-specifier: [C++0x]
5825 type-name ::
5826 namespace-name ::
5827 nested-name-specifier identifier ::
5828 nested-name-specifier template [opt] simple-template-id ::
5830 PARSER->SCOPE should be set appropriately before this function is
5831 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5832 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5833 in name lookups.
5835 Sets PARSER->SCOPE to the class (TYPE) or namespace
5836 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5837 it unchanged if there is no nested-name-specifier. Returns the new
5838 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5840 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5841 part of a declaration and/or decl-specifier. */
5843 static tree
5844 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5845 bool typename_keyword_p,
5846 bool check_dependency_p,
5847 bool type_p,
5848 bool is_declaration)
5850 bool success = false;
5851 cp_token_position start = 0;
5852 cp_token *token;
5854 /* Remember where the nested-name-specifier starts. */
5855 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5857 start = cp_lexer_token_position (parser->lexer, false);
5858 push_deferring_access_checks (dk_deferred);
5861 while (true)
5863 tree new_scope;
5864 tree old_scope;
5865 tree saved_qualifying_scope;
5866 bool template_keyword_p;
5868 /* Spot cases that cannot be the beginning of a
5869 nested-name-specifier. */
5870 token = cp_lexer_peek_token (parser->lexer);
5872 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5873 the already parsed nested-name-specifier. */
5874 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5876 /* Grab the nested-name-specifier and continue the loop. */
5877 cp_parser_pre_parsed_nested_name_specifier (parser);
5878 /* If we originally encountered this nested-name-specifier
5879 with IS_DECLARATION set to false, we will not have
5880 resolved TYPENAME_TYPEs, so we must do so here. */
5881 if (is_declaration
5882 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5884 new_scope = resolve_typename_type (parser->scope,
5885 /*only_current_p=*/false);
5886 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5887 parser->scope = new_scope;
5889 success = true;
5890 continue;
5893 /* Spot cases that cannot be the beginning of a
5894 nested-name-specifier. On the second and subsequent times
5895 through the loop, we look for the `template' keyword. */
5896 if (success && token->keyword == RID_TEMPLATE)
5898 /* A template-id can start a nested-name-specifier. */
5899 else if (token->type == CPP_TEMPLATE_ID)
5901 /* DR 743: decltype can be used in a nested-name-specifier. */
5902 else if (token_is_decltype (token))
5904 else
5906 /* If the next token is not an identifier, then it is
5907 definitely not a type-name or namespace-name. */
5908 if (token->type != CPP_NAME)
5909 break;
5910 /* If the following token is neither a `<' (to begin a
5911 template-id), nor a `::', then we are not looking at a
5912 nested-name-specifier. */
5913 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5915 if (token->type == CPP_COLON
5916 && parser->colon_corrects_to_scope_p
5917 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5919 error_at (token->location,
5920 "found %<:%> in nested-name-specifier, expected %<::%>");
5921 token->type = CPP_SCOPE;
5924 if (token->type != CPP_SCOPE
5925 && !cp_parser_nth_token_starts_template_argument_list_p
5926 (parser, 2))
5927 break;
5930 /* The nested-name-specifier is optional, so we parse
5931 tentatively. */
5932 cp_parser_parse_tentatively (parser);
5934 /* Look for the optional `template' keyword, if this isn't the
5935 first time through the loop. */
5936 if (success)
5937 template_keyword_p = cp_parser_optional_template_keyword (parser);
5938 else
5939 template_keyword_p = false;
5941 /* Save the old scope since the name lookup we are about to do
5942 might destroy it. */
5943 old_scope = parser->scope;
5944 saved_qualifying_scope = parser->qualifying_scope;
5945 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5946 look up names in "X<T>::I" in order to determine that "Y" is
5947 a template. So, if we have a typename at this point, we make
5948 an effort to look through it. */
5949 if (is_declaration
5950 && !typename_keyword_p
5951 && parser->scope
5952 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5953 parser->scope = resolve_typename_type (parser->scope,
5954 /*only_current_p=*/false);
5955 /* Parse the qualifying entity. */
5956 new_scope
5957 = cp_parser_qualifying_entity (parser,
5958 typename_keyword_p,
5959 template_keyword_p,
5960 check_dependency_p,
5961 type_p,
5962 is_declaration);
5963 /* Look for the `::' token. */
5964 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5966 /* If we found what we wanted, we keep going; otherwise, we're
5967 done. */
5968 if (!cp_parser_parse_definitely (parser))
5970 bool error_p = false;
5972 /* Restore the OLD_SCOPE since it was valid before the
5973 failed attempt at finding the last
5974 class-or-namespace-name. */
5975 parser->scope = old_scope;
5976 parser->qualifying_scope = saved_qualifying_scope;
5978 /* If the next token is a decltype, and the one after that is a
5979 `::', then the decltype has failed to resolve to a class or
5980 enumeration type. Give this error even when parsing
5981 tentatively since it can't possibly be valid--and we're going
5982 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5983 won't get another chance.*/
5984 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5985 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5986 == CPP_SCOPE))
5988 token = cp_lexer_consume_token (parser->lexer);
5989 error_at (token->location, "decltype evaluates to %qT, "
5990 "which is not a class or enumeration type",
5991 token->u.tree_check_value->value);
5992 parser->scope = error_mark_node;
5993 error_p = true;
5994 /* As below. */
5995 success = true;
5996 cp_lexer_consume_token (parser->lexer);
5999 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6000 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6002 /* If we have a non-type template-id followed by ::, it can't
6003 possibly be valid. */
6004 token = cp_lexer_peek_token (parser->lexer);
6005 tree tid = token->u.tree_check_value->value;
6006 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6007 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6009 tree tmpl = NULL_TREE;
6010 if (is_overloaded_fn (tid))
6012 tree fns = get_fns (tid);
6013 if (!OVL_CHAIN (fns))
6014 tmpl = OVL_CURRENT (fns);
6015 error_at (token->location, "function template-id %qD "
6016 "in nested-name-specifier", tid);
6018 else
6020 /* Variable template. */
6021 tmpl = TREE_OPERAND (tid, 0);
6022 gcc_assert (variable_template_p (tmpl));
6023 error_at (token->location, "variable template-id %qD "
6024 "in nested-name-specifier", tid);
6026 if (tmpl)
6027 inform (DECL_SOURCE_LOCATION (tmpl),
6028 "%qD declared here", tmpl);
6030 parser->scope = error_mark_node;
6031 error_p = true;
6032 /* As below. */
6033 success = true;
6034 cp_lexer_consume_token (parser->lexer);
6035 cp_lexer_consume_token (parser->lexer);
6039 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6040 break;
6041 /* If the next token is an identifier, and the one after
6042 that is a `::', then any valid interpretation would have
6043 found a class-or-namespace-name. */
6044 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6045 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6046 == CPP_SCOPE)
6047 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6048 != CPP_COMPL))
6050 token = cp_lexer_consume_token (parser->lexer);
6051 if (!error_p)
6053 if (!token->error_reported)
6055 tree decl;
6056 tree ambiguous_decls;
6058 decl = cp_parser_lookup_name (parser, token->u.value,
6059 none_type,
6060 /*is_template=*/false,
6061 /*is_namespace=*/false,
6062 /*check_dependency=*/true,
6063 &ambiguous_decls,
6064 token->location);
6065 if (TREE_CODE (decl) == TEMPLATE_DECL)
6066 error_at (token->location,
6067 "%qD used without template parameters",
6068 decl);
6069 else if (ambiguous_decls)
6071 // cp_parser_lookup_name has the same diagnostic,
6072 // thus make sure to emit it at most once.
6073 if (cp_parser_uncommitted_to_tentative_parse_p
6074 (parser))
6076 error_at (token->location,
6077 "reference to %qD is ambiguous",
6078 token->u.value);
6079 print_candidates (ambiguous_decls);
6081 decl = error_mark_node;
6083 else
6085 if (cxx_dialect != cxx98)
6086 cp_parser_name_lookup_error
6087 (parser, token->u.value, decl, NLE_NOT_CXX98,
6088 token->location);
6089 else
6090 cp_parser_name_lookup_error
6091 (parser, token->u.value, decl, NLE_CXX98,
6092 token->location);
6095 parser->scope = error_mark_node;
6096 error_p = true;
6097 /* Treat this as a successful nested-name-specifier
6098 due to:
6100 [basic.lookup.qual]
6102 If the name found is not a class-name (clause
6103 _class_) or namespace-name (_namespace.def_), the
6104 program is ill-formed. */
6105 success = true;
6107 cp_lexer_consume_token (parser->lexer);
6109 break;
6111 /* We've found one valid nested-name-specifier. */
6112 success = true;
6113 /* Name lookup always gives us a DECL. */
6114 if (TREE_CODE (new_scope) == TYPE_DECL)
6115 new_scope = TREE_TYPE (new_scope);
6116 /* Uses of "template" must be followed by actual templates. */
6117 if (template_keyword_p
6118 && !(CLASS_TYPE_P (new_scope)
6119 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6120 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6121 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6122 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6123 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6124 == TEMPLATE_ID_EXPR)))
6125 permerror (input_location, TYPE_P (new_scope)
6126 ? G_("%qT is not a template")
6127 : G_("%qD is not a template"),
6128 new_scope);
6129 /* If it is a class scope, try to complete it; we are about to
6130 be looking up names inside the class. */
6131 if (TYPE_P (new_scope)
6132 /* Since checking types for dependency can be expensive,
6133 avoid doing it if the type is already complete. */
6134 && !COMPLETE_TYPE_P (new_scope)
6135 /* Do not try to complete dependent types. */
6136 && !dependent_type_p (new_scope))
6138 new_scope = complete_type (new_scope);
6139 /* If it is a typedef to current class, use the current
6140 class instead, as the typedef won't have any names inside
6141 it yet. */
6142 if (!COMPLETE_TYPE_P (new_scope)
6143 && currently_open_class (new_scope))
6144 new_scope = TYPE_MAIN_VARIANT (new_scope);
6146 /* Make sure we look in the right scope the next time through
6147 the loop. */
6148 parser->scope = new_scope;
6151 /* If parsing tentatively, replace the sequence of tokens that makes
6152 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6153 token. That way, should we re-parse the token stream, we will
6154 not have to repeat the effort required to do the parse, nor will
6155 we issue duplicate error messages. */
6156 if (success && start)
6158 cp_token *token;
6160 token = cp_lexer_token_at (parser->lexer, start);
6161 /* Reset the contents of the START token. */
6162 token->type = CPP_NESTED_NAME_SPECIFIER;
6163 /* Retrieve any deferred checks. Do not pop this access checks yet
6164 so the memory will not be reclaimed during token replacing below. */
6165 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6166 token->u.tree_check_value->value = parser->scope;
6167 token->u.tree_check_value->checks = get_deferred_access_checks ();
6168 token->u.tree_check_value->qualifying_scope =
6169 parser->qualifying_scope;
6170 token->keyword = RID_MAX;
6172 /* Purge all subsequent tokens. */
6173 cp_lexer_purge_tokens_after (parser->lexer, start);
6176 if (start)
6177 pop_to_parent_deferring_access_checks ();
6179 return success ? parser->scope : NULL_TREE;
6182 /* Parse a nested-name-specifier. See
6183 cp_parser_nested_name_specifier_opt for details. This function
6184 behaves identically, except that it will an issue an error if no
6185 nested-name-specifier is present. */
6187 static tree
6188 cp_parser_nested_name_specifier (cp_parser *parser,
6189 bool typename_keyword_p,
6190 bool check_dependency_p,
6191 bool type_p,
6192 bool is_declaration)
6194 tree scope;
6196 /* Look for the nested-name-specifier. */
6197 scope = cp_parser_nested_name_specifier_opt (parser,
6198 typename_keyword_p,
6199 check_dependency_p,
6200 type_p,
6201 is_declaration);
6202 /* If it was not present, issue an error message. */
6203 if (!scope)
6205 cp_parser_error (parser, "expected nested-name-specifier");
6206 parser->scope = NULL_TREE;
6209 return scope;
6212 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6213 this is either a class-name or a namespace-name (which corresponds
6214 to the class-or-namespace-name production in the grammar). For
6215 C++0x, it can also be a type-name that refers to an enumeration
6216 type or a simple-template-id.
6218 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6219 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6220 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6221 TYPE_P is TRUE iff the next name should be taken as a class-name,
6222 even the same name is declared to be another entity in the same
6223 scope.
6225 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6226 specified by the class-or-namespace-name. If neither is found the
6227 ERROR_MARK_NODE is returned. */
6229 static tree
6230 cp_parser_qualifying_entity (cp_parser *parser,
6231 bool typename_keyword_p,
6232 bool template_keyword_p,
6233 bool check_dependency_p,
6234 bool type_p,
6235 bool is_declaration)
6237 tree saved_scope;
6238 tree saved_qualifying_scope;
6239 tree saved_object_scope;
6240 tree scope;
6241 bool only_class_p;
6242 bool successful_parse_p;
6244 /* DR 743: decltype can appear in a nested-name-specifier. */
6245 if (cp_lexer_next_token_is_decltype (parser->lexer))
6247 scope = cp_parser_decltype (parser);
6248 if (TREE_CODE (scope) != ENUMERAL_TYPE
6249 && !MAYBE_CLASS_TYPE_P (scope))
6251 cp_parser_simulate_error (parser);
6252 return error_mark_node;
6254 if (TYPE_NAME (scope))
6255 scope = TYPE_NAME (scope);
6256 return scope;
6259 /* Before we try to parse the class-name, we must save away the
6260 current PARSER->SCOPE since cp_parser_class_name will destroy
6261 it. */
6262 saved_scope = parser->scope;
6263 saved_qualifying_scope = parser->qualifying_scope;
6264 saved_object_scope = parser->object_scope;
6265 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6266 there is no need to look for a namespace-name. */
6267 only_class_p = template_keyword_p
6268 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6269 if (!only_class_p)
6270 cp_parser_parse_tentatively (parser);
6271 scope = cp_parser_class_name (parser,
6272 typename_keyword_p,
6273 template_keyword_p,
6274 type_p ? class_type : none_type,
6275 check_dependency_p,
6276 /*class_head_p=*/false,
6277 is_declaration,
6278 /*enum_ok=*/cxx_dialect > cxx98);
6279 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6280 /* If that didn't work, try for a namespace-name. */
6281 if (!only_class_p && !successful_parse_p)
6283 /* Restore the saved scope. */
6284 parser->scope = saved_scope;
6285 parser->qualifying_scope = saved_qualifying_scope;
6286 parser->object_scope = saved_object_scope;
6287 /* If we are not looking at an identifier followed by the scope
6288 resolution operator, then this is not part of a
6289 nested-name-specifier. (Note that this function is only used
6290 to parse the components of a nested-name-specifier.) */
6291 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6292 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6293 return error_mark_node;
6294 scope = cp_parser_namespace_name (parser);
6297 return scope;
6300 /* Return true if we are looking at a compound-literal, false otherwise. */
6302 static bool
6303 cp_parser_compound_literal_p (cp_parser *parser)
6305 /* Consume the `('. */
6306 cp_lexer_consume_token (parser->lexer);
6308 cp_lexer_save_tokens (parser->lexer);
6310 /* Skip tokens until the next token is a closing parenthesis.
6311 If we find the closing `)', and the next token is a `{', then
6312 we are looking at a compound-literal. */
6313 bool compound_literal_p
6314 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6315 /*consume_paren=*/true)
6316 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6318 /* Roll back the tokens we skipped. */
6319 cp_lexer_rollback_tokens (parser->lexer);
6321 return compound_literal_p;
6324 /* Parse a postfix-expression.
6326 postfix-expression:
6327 primary-expression
6328 postfix-expression [ expression ]
6329 postfix-expression ( expression-list [opt] )
6330 simple-type-specifier ( expression-list [opt] )
6331 typename :: [opt] nested-name-specifier identifier
6332 ( expression-list [opt] )
6333 typename :: [opt] nested-name-specifier template [opt] template-id
6334 ( expression-list [opt] )
6335 postfix-expression . template [opt] id-expression
6336 postfix-expression -> template [opt] id-expression
6337 postfix-expression . pseudo-destructor-name
6338 postfix-expression -> pseudo-destructor-name
6339 postfix-expression ++
6340 postfix-expression --
6341 dynamic_cast < type-id > ( expression )
6342 static_cast < type-id > ( expression )
6343 reinterpret_cast < type-id > ( expression )
6344 const_cast < type-id > ( expression )
6345 typeid ( expression )
6346 typeid ( type-id )
6348 GNU Extension:
6350 postfix-expression:
6351 ( type-id ) { initializer-list , [opt] }
6353 This extension is a GNU version of the C99 compound-literal
6354 construct. (The C99 grammar uses `type-name' instead of `type-id',
6355 but they are essentially the same concept.)
6357 If ADDRESS_P is true, the postfix expression is the operand of the
6358 `&' operator. CAST_P is true if this expression is the target of a
6359 cast.
6361 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6362 class member access expressions [expr.ref].
6364 Returns a representation of the expression. */
6366 static cp_expr
6367 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6368 bool member_access_only_p, bool decltype_p,
6369 cp_id_kind * pidk_return)
6371 cp_token *token;
6372 location_t loc;
6373 enum rid keyword;
6374 cp_id_kind idk = CP_ID_KIND_NONE;
6375 cp_expr postfix_expression = NULL_TREE;
6376 bool is_member_access = false;
6377 int saved_in_statement = -1;
6379 /* Peek at the next token. */
6380 token = cp_lexer_peek_token (parser->lexer);
6381 loc = token->location;
6382 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6384 /* Some of the productions are determined by keywords. */
6385 keyword = token->keyword;
6386 switch (keyword)
6388 case RID_DYNCAST:
6389 case RID_STATCAST:
6390 case RID_REINTCAST:
6391 case RID_CONSTCAST:
6393 tree type;
6394 cp_expr expression;
6395 const char *saved_message;
6396 bool saved_in_type_id_in_expr_p;
6398 /* All of these can be handled in the same way from the point
6399 of view of parsing. Begin by consuming the token
6400 identifying the cast. */
6401 cp_lexer_consume_token (parser->lexer);
6403 /* New types cannot be defined in the cast. */
6404 saved_message = parser->type_definition_forbidden_message;
6405 parser->type_definition_forbidden_message
6406 = G_("types may not be defined in casts");
6408 /* Look for the opening `<'. */
6409 cp_parser_require (parser, CPP_LESS, RT_LESS);
6410 /* Parse the type to which we are casting. */
6411 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6412 parser->in_type_id_in_expr_p = true;
6413 type = cp_parser_type_id (parser);
6414 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6415 /* Look for the closing `>'. */
6416 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6417 /* Restore the old message. */
6418 parser->type_definition_forbidden_message = saved_message;
6420 bool saved_greater_than_is_operator_p
6421 = parser->greater_than_is_operator_p;
6422 parser->greater_than_is_operator_p = true;
6424 /* And the expression which is being cast. */
6425 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6426 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6427 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6428 RT_CLOSE_PAREN);
6429 location_t end_loc = close_paren ?
6430 close_paren->location : UNKNOWN_LOCATION;
6432 parser->greater_than_is_operator_p
6433 = saved_greater_than_is_operator_p;
6435 /* Only type conversions to integral or enumeration types
6436 can be used in constant-expressions. */
6437 if (!cast_valid_in_integral_constant_expression_p (type)
6438 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6439 return error_mark_node;
6441 switch (keyword)
6443 case RID_DYNCAST:
6444 postfix_expression
6445 = build_dynamic_cast (type, expression, tf_warning_or_error);
6446 break;
6447 case RID_STATCAST:
6448 postfix_expression
6449 = build_static_cast (type, expression, tf_warning_or_error);
6450 break;
6451 case RID_REINTCAST:
6452 postfix_expression
6453 = build_reinterpret_cast (type, expression,
6454 tf_warning_or_error);
6455 break;
6456 case RID_CONSTCAST:
6457 postfix_expression
6458 = build_const_cast (type, expression, tf_warning_or_error);
6459 break;
6460 default:
6461 gcc_unreachable ();
6464 /* Construct a location e.g. :
6465 reinterpret_cast <int *> (expr)
6466 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6467 ranging from the start of the "*_cast" token to the final closing
6468 paren, with the caret at the start. */
6469 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6470 postfix_expression.set_location (cp_cast_loc);
6472 break;
6474 case RID_TYPEID:
6476 tree type;
6477 const char *saved_message;
6478 bool saved_in_type_id_in_expr_p;
6480 /* Consume the `typeid' token. */
6481 cp_lexer_consume_token (parser->lexer);
6482 /* Look for the `(' token. */
6483 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6484 /* Types cannot be defined in a `typeid' expression. */
6485 saved_message = parser->type_definition_forbidden_message;
6486 parser->type_definition_forbidden_message
6487 = G_("types may not be defined in a %<typeid%> expression");
6488 /* We can't be sure yet whether we're looking at a type-id or an
6489 expression. */
6490 cp_parser_parse_tentatively (parser);
6491 /* Try a type-id first. */
6492 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6493 parser->in_type_id_in_expr_p = true;
6494 type = cp_parser_type_id (parser);
6495 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6496 /* Look for the `)' token. Otherwise, we can't be sure that
6497 we're not looking at an expression: consider `typeid (int
6498 (3))', for example. */
6499 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6500 /* If all went well, simply lookup the type-id. */
6501 if (cp_parser_parse_definitely (parser))
6502 postfix_expression = get_typeid (type, tf_warning_or_error);
6503 /* Otherwise, fall back to the expression variant. */
6504 else
6506 tree expression;
6508 /* Look for an expression. */
6509 expression = cp_parser_expression (parser, & idk);
6510 /* Compute its typeid. */
6511 postfix_expression = build_typeid (expression, tf_warning_or_error);
6512 /* Look for the `)' token. */
6513 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6515 /* Restore the saved message. */
6516 parser->type_definition_forbidden_message = saved_message;
6517 /* `typeid' may not appear in an integral constant expression. */
6518 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6519 return error_mark_node;
6521 break;
6523 case RID_TYPENAME:
6525 tree type;
6526 /* The syntax permitted here is the same permitted for an
6527 elaborated-type-specifier. */
6528 ++parser->prevent_constrained_type_specifiers;
6529 type = cp_parser_elaborated_type_specifier (parser,
6530 /*is_friend=*/false,
6531 /*is_declaration=*/false);
6532 --parser->prevent_constrained_type_specifiers;
6533 postfix_expression = cp_parser_functional_cast (parser, type);
6535 break;
6537 case RID_CILK_SPAWN:
6539 location_t cilk_spawn_loc
6540 = cp_lexer_peek_token (parser->lexer)->location;
6541 cp_lexer_consume_token (parser->lexer);
6542 token = cp_lexer_peek_token (parser->lexer);
6543 if (token->type == CPP_SEMICOLON)
6545 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6546 "an expression");
6547 postfix_expression = error_mark_node;
6548 break;
6550 else if (!current_function_decl)
6552 error_at (token->location, "%<_Cilk_spawn%> may only be used "
6553 "inside a function");
6554 postfix_expression = error_mark_node;
6555 break;
6557 else
6559 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6560 saved_in_statement = parser->in_statement;
6561 parser->in_statement |= IN_CILK_SPAWN;
6563 cfun->calls_cilk_spawn = 1;
6564 postfix_expression =
6565 cp_parser_postfix_expression (parser, false, false,
6566 false, false, &idk);
6567 if (!flag_cilkplus)
6569 error_at (token->location, "-fcilkplus must be enabled to use"
6570 " %<_Cilk_spawn%>");
6571 cfun->calls_cilk_spawn = 0;
6573 else if (saved_in_statement & IN_CILK_SPAWN)
6575 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6576 "are not permitted");
6577 postfix_expression = error_mark_node;
6578 cfun->calls_cilk_spawn = 0;
6580 else
6582 location_t loc = postfix_expression.get_location ();
6583 postfix_expression = build_cilk_spawn (token->location,
6584 postfix_expression);
6585 /* Build a location of the form:
6586 _Cilk_spawn expr
6587 ~~~~~~~~~~~~^~~~
6588 with caret at the expr, ranging from the start of the
6589 _Cilk_spawn token to the end of the expression. */
6590 location_t combined_loc =
6591 make_location (loc, cilk_spawn_loc, get_finish (loc));
6592 postfix_expression.set_location (combined_loc);
6593 if (postfix_expression != error_mark_node)
6594 SET_EXPR_LOCATION (postfix_expression, input_location);
6595 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6597 break;
6600 case RID_BUILTIN_SHUFFLE:
6602 vec<tree, va_gc> *vec;
6603 unsigned int i;
6604 tree p;
6606 cp_lexer_consume_token (parser->lexer);
6607 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6608 /*cast_p=*/false, /*allow_expansion_p=*/true,
6609 /*non_constant_p=*/NULL);
6610 if (vec == NULL)
6611 return error_mark_node;
6613 FOR_EACH_VEC_ELT (*vec, i, p)
6614 mark_exp_read (p);
6616 if (vec->length () == 2)
6617 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
6618 tf_warning_or_error);
6619 else if (vec->length () == 3)
6620 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6621 tf_warning_or_error);
6622 else
6624 error_at (loc, "wrong number of arguments to "
6625 "%<__builtin_shuffle%>");
6626 return error_mark_node;
6628 break;
6631 default:
6633 tree type;
6635 /* If the next thing is a simple-type-specifier, we may be
6636 looking at a functional cast. We could also be looking at
6637 an id-expression. So, we try the functional cast, and if
6638 that doesn't work we fall back to the primary-expression. */
6639 cp_parser_parse_tentatively (parser);
6640 /* Look for the simple-type-specifier. */
6641 ++parser->prevent_constrained_type_specifiers;
6642 type = cp_parser_simple_type_specifier (parser,
6643 /*decl_specs=*/NULL,
6644 CP_PARSER_FLAGS_NONE);
6645 --parser->prevent_constrained_type_specifiers;
6646 /* Parse the cast itself. */
6647 if (!cp_parser_error_occurred (parser))
6648 postfix_expression
6649 = cp_parser_functional_cast (parser, type);
6650 /* If that worked, we're done. */
6651 if (cp_parser_parse_definitely (parser))
6652 break;
6654 /* If the functional-cast didn't work out, try a
6655 compound-literal. */
6656 if (cp_parser_allow_gnu_extensions_p (parser)
6657 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6659 cp_expr initializer = NULL_TREE;
6661 cp_parser_parse_tentatively (parser);
6663 /* Avoid calling cp_parser_type_id pointlessly, see comment
6664 in cp_parser_cast_expression about c++/29234. */
6665 if (!cp_parser_compound_literal_p (parser))
6666 cp_parser_simulate_error (parser);
6667 else
6669 /* Parse the type. */
6670 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6671 parser->in_type_id_in_expr_p = true;
6672 type = cp_parser_type_id (parser);
6673 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6674 /* Look for the `)'. */
6675 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6678 /* If things aren't going well, there's no need to
6679 keep going. */
6680 if (!cp_parser_error_occurred (parser))
6682 bool non_constant_p;
6683 /* Parse the brace-enclosed initializer list. */
6684 initializer = cp_parser_braced_list (parser,
6685 &non_constant_p);
6687 /* If that worked, we're definitely looking at a
6688 compound-literal expression. */
6689 if (cp_parser_parse_definitely (parser))
6691 /* Warn the user that a compound literal is not
6692 allowed in standard C++. */
6693 pedwarn (input_location, OPT_Wpedantic,
6694 "ISO C++ forbids compound-literals");
6695 /* For simplicity, we disallow compound literals in
6696 constant-expressions. We could
6697 allow compound literals of integer type, whose
6698 initializer was a constant, in constant
6699 expressions. Permitting that usage, as a further
6700 extension, would not change the meaning of any
6701 currently accepted programs. (Of course, as
6702 compound literals are not part of ISO C++, the
6703 standard has nothing to say.) */
6704 if (cp_parser_non_integral_constant_expression (parser,
6705 NIC_NCC))
6707 postfix_expression = error_mark_node;
6708 break;
6710 /* Form the representation of the compound-literal. */
6711 postfix_expression
6712 = finish_compound_literal (type, initializer,
6713 tf_warning_or_error);
6714 postfix_expression.set_location (initializer.get_location ());
6715 break;
6719 /* It must be a primary-expression. */
6720 postfix_expression
6721 = cp_parser_primary_expression (parser, address_p, cast_p,
6722 /*template_arg_p=*/false,
6723 decltype_p,
6724 &idk);
6726 break;
6729 /* Note that we don't need to worry about calling build_cplus_new on a
6730 class-valued CALL_EXPR in decltype when it isn't the end of the
6731 postfix-expression; unary_complex_lvalue will take care of that for
6732 all these cases. */
6734 /* Keep looping until the postfix-expression is complete. */
6735 while (true)
6737 if (idk == CP_ID_KIND_UNQUALIFIED
6738 && identifier_p (postfix_expression)
6739 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6740 /* It is not a Koenig lookup function call. */
6741 postfix_expression
6742 = unqualified_name_lookup_error (postfix_expression);
6744 /* Peek at the next token. */
6745 token = cp_lexer_peek_token (parser->lexer);
6747 switch (token->type)
6749 case CPP_OPEN_SQUARE:
6750 if (cp_next_tokens_can_be_std_attribute_p (parser))
6752 cp_parser_error (parser,
6753 "two consecutive %<[%> shall "
6754 "only introduce an attribute");
6755 return error_mark_node;
6757 postfix_expression
6758 = cp_parser_postfix_open_square_expression (parser,
6759 postfix_expression,
6760 false,
6761 decltype_p);
6762 postfix_expression.set_range (start_loc,
6763 postfix_expression.get_location ());
6765 idk = CP_ID_KIND_NONE;
6766 is_member_access = false;
6767 break;
6769 case CPP_OPEN_PAREN:
6770 /* postfix-expression ( expression-list [opt] ) */
6772 bool koenig_p;
6773 bool is_builtin_constant_p;
6774 bool saved_integral_constant_expression_p = false;
6775 bool saved_non_integral_constant_expression_p = false;
6776 tsubst_flags_t complain = complain_flags (decltype_p);
6777 vec<tree, va_gc> *args;
6778 location_t close_paren_loc = UNKNOWN_LOCATION;
6780 is_member_access = false;
6782 is_builtin_constant_p
6783 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6784 if (is_builtin_constant_p)
6786 /* The whole point of __builtin_constant_p is to allow
6787 non-constant expressions to appear as arguments. */
6788 saved_integral_constant_expression_p
6789 = parser->integral_constant_expression_p;
6790 saved_non_integral_constant_expression_p
6791 = parser->non_integral_constant_expression_p;
6792 parser->integral_constant_expression_p = false;
6794 args = (cp_parser_parenthesized_expression_list
6795 (parser, non_attr,
6796 /*cast_p=*/false, /*allow_expansion_p=*/true,
6797 /*non_constant_p=*/NULL,
6798 /*close_paren_loc=*/&close_paren_loc));
6799 if (is_builtin_constant_p)
6801 parser->integral_constant_expression_p
6802 = saved_integral_constant_expression_p;
6803 parser->non_integral_constant_expression_p
6804 = saved_non_integral_constant_expression_p;
6807 if (args == NULL)
6809 postfix_expression = error_mark_node;
6810 break;
6813 /* Function calls are not permitted in
6814 constant-expressions. */
6815 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6816 && cp_parser_non_integral_constant_expression (parser,
6817 NIC_FUNC_CALL))
6819 postfix_expression = error_mark_node;
6820 release_tree_vector (args);
6821 break;
6824 koenig_p = false;
6825 if (idk == CP_ID_KIND_UNQUALIFIED
6826 || idk == CP_ID_KIND_TEMPLATE_ID)
6828 if (identifier_p (postfix_expression))
6830 if (!args->is_empty ())
6832 koenig_p = true;
6833 if (!any_type_dependent_arguments_p (args))
6834 postfix_expression
6835 = perform_koenig_lookup (postfix_expression, args,
6836 complain);
6838 else
6839 postfix_expression
6840 = unqualified_fn_lookup_error (postfix_expression);
6842 /* We do not perform argument-dependent lookup if
6843 normal lookup finds a non-function, in accordance
6844 with the expected resolution of DR 218. */
6845 else if (!args->is_empty ()
6846 && is_overloaded_fn (postfix_expression))
6848 tree fn = get_first_fn (postfix_expression);
6849 fn = STRIP_TEMPLATE (fn);
6851 /* Do not do argument dependent lookup if regular
6852 lookup finds a member function or a block-scope
6853 function declaration. [basic.lookup.argdep]/3 */
6854 if (!DECL_FUNCTION_MEMBER_P (fn)
6855 && !DECL_LOCAL_FUNCTION_P (fn))
6857 koenig_p = true;
6858 if (!any_type_dependent_arguments_p (args))
6859 postfix_expression
6860 = perform_koenig_lookup (postfix_expression, args,
6861 complain);
6866 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6867 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6868 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6869 && vec_safe_length (args) == 3)
6871 tree arg0 = (*args)[0];
6872 tree arg1 = (*args)[1];
6873 tree arg2 = (*args)[2];
6874 int literal_mask = ((!!integer_zerop (arg1) << 1)
6875 | (!!integer_zerop (arg2) << 2));
6876 if (TREE_CODE (arg2) == CONST_DECL)
6877 arg2 = DECL_INITIAL (arg2);
6878 warn_for_memset (input_location, arg0, arg2, literal_mask);
6881 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6883 tree instance = TREE_OPERAND (postfix_expression, 0);
6884 tree fn = TREE_OPERAND (postfix_expression, 1);
6886 if (processing_template_decl
6887 && (type_dependent_object_expression_p (instance)
6888 || (!BASELINK_P (fn)
6889 && TREE_CODE (fn) != FIELD_DECL)
6890 || type_dependent_expression_p (fn)
6891 || any_type_dependent_arguments_p (args)))
6893 postfix_expression
6894 = build_nt_call_vec (postfix_expression, args);
6895 release_tree_vector (args);
6896 break;
6899 if (BASELINK_P (fn))
6901 postfix_expression
6902 = (build_new_method_call
6903 (instance, fn, &args, NULL_TREE,
6904 (idk == CP_ID_KIND_QUALIFIED
6905 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6906 : LOOKUP_NORMAL),
6907 /*fn_p=*/NULL,
6908 complain));
6910 else
6911 postfix_expression
6912 = finish_call_expr (postfix_expression, &args,
6913 /*disallow_virtual=*/false,
6914 /*koenig_p=*/false,
6915 complain);
6917 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6918 || TREE_CODE (postfix_expression) == MEMBER_REF
6919 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6920 postfix_expression = (build_offset_ref_call_from_tree
6921 (postfix_expression, &args,
6922 complain));
6923 else if (idk == CP_ID_KIND_QUALIFIED)
6924 /* A call to a static class member, or a namespace-scope
6925 function. */
6926 postfix_expression
6927 = finish_call_expr (postfix_expression, &args,
6928 /*disallow_virtual=*/true,
6929 koenig_p,
6930 complain);
6931 else
6932 /* All other function calls. */
6933 postfix_expression
6934 = finish_call_expr (postfix_expression, &args,
6935 /*disallow_virtual=*/false,
6936 koenig_p,
6937 complain);
6939 if (close_paren_loc != UNKNOWN_LOCATION)
6941 location_t combined_loc = make_location (token->location,
6942 start_loc,
6943 close_paren_loc);
6944 postfix_expression.set_location (combined_loc);
6947 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6948 idk = CP_ID_KIND_NONE;
6950 release_tree_vector (args);
6952 break;
6954 case CPP_DOT:
6955 case CPP_DEREF:
6956 /* postfix-expression . template [opt] id-expression
6957 postfix-expression . pseudo-destructor-name
6958 postfix-expression -> template [opt] id-expression
6959 postfix-expression -> pseudo-destructor-name */
6961 /* Consume the `.' or `->' operator. */
6962 cp_lexer_consume_token (parser->lexer);
6964 postfix_expression
6965 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6966 postfix_expression,
6967 false, &idk, loc);
6969 is_member_access = true;
6970 break;
6972 case CPP_PLUS_PLUS:
6973 /* postfix-expression ++ */
6974 /* Consume the `++' token. */
6975 cp_lexer_consume_token (parser->lexer);
6976 /* Generate a representation for the complete expression. */
6977 postfix_expression
6978 = finish_increment_expr (postfix_expression,
6979 POSTINCREMENT_EXPR);
6980 /* Increments may not appear in constant-expressions. */
6981 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6982 postfix_expression = error_mark_node;
6983 idk = CP_ID_KIND_NONE;
6984 is_member_access = false;
6985 break;
6987 case CPP_MINUS_MINUS:
6988 /* postfix-expression -- */
6989 /* Consume the `--' token. */
6990 cp_lexer_consume_token (parser->lexer);
6991 /* Generate a representation for the complete expression. */
6992 postfix_expression
6993 = finish_increment_expr (postfix_expression,
6994 POSTDECREMENT_EXPR);
6995 /* Decrements may not appear in constant-expressions. */
6996 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6997 postfix_expression = error_mark_node;
6998 idk = CP_ID_KIND_NONE;
6999 is_member_access = false;
7000 break;
7002 default:
7003 if (pidk_return != NULL)
7004 * pidk_return = idk;
7005 if (member_access_only_p)
7006 return is_member_access
7007 ? postfix_expression
7008 : cp_expr (error_mark_node);
7009 else
7010 return postfix_expression;
7014 /* We should never get here. */
7015 gcc_unreachable ();
7016 return error_mark_node;
7019 /* This function parses Cilk Plus array notations. If a normal array expr. is
7020 parsed then the array index is passed back to the caller through *INIT_INDEX
7021 and the function returns a NULL_TREE. If array notation expr. is parsed,
7022 then *INIT_INDEX is ignored by the caller and the function returns
7023 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
7024 error_mark_node. */
7026 static tree
7027 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
7028 tree array_value)
7030 cp_token *token = NULL;
7031 tree length_index, stride = NULL_TREE, value_tree, array_type;
7032 if (!array_value || array_value == error_mark_node)
7034 cp_parser_skip_to_end_of_statement (parser);
7035 return error_mark_node;
7038 array_type = TREE_TYPE (array_value);
7040 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
7041 parser->colon_corrects_to_scope_p = false;
7042 token = cp_lexer_peek_token (parser->lexer);
7044 if (!token)
7046 cp_parser_error (parser, "expected %<:%> or numeral");
7047 return error_mark_node;
7049 else if (token->type == CPP_COLON)
7051 /* Consume the ':'. */
7052 cp_lexer_consume_token (parser->lexer);
7054 /* If we are here, then we have a case like this A[:]. */
7055 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
7057 cp_parser_error (parser, "expected %<]%>");
7058 cp_parser_skip_to_end_of_statement (parser);
7059 return error_mark_node;
7061 *init_index = NULL_TREE;
7062 stride = NULL_TREE;
7063 length_index = NULL_TREE;
7065 else
7067 /* If we are here, then there are three valid possibilities:
7068 1. ARRAY [ EXP ]
7069 2. ARRAY [ EXP : EXP ]
7070 3. ARRAY [ EXP : EXP : EXP ] */
7072 *init_index = cp_parser_expression (parser);
7073 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
7075 /* This indicates that we have a normal array expression. */
7076 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7077 return NULL_TREE;
7080 /* Consume the ':'. */
7081 cp_lexer_consume_token (parser->lexer);
7082 length_index = cp_parser_expression (parser);
7083 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7085 cp_lexer_consume_token (parser->lexer);
7086 stride = cp_parser_expression (parser);
7089 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7091 if (*init_index == error_mark_node || length_index == error_mark_node
7092 || stride == error_mark_node || array_type == error_mark_node)
7094 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
7095 cp_lexer_consume_token (parser->lexer);
7096 return error_mark_node;
7098 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7100 value_tree = build_array_notation_ref (loc, array_value, *init_index,
7101 length_index, stride, array_type);
7102 return value_tree;
7105 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7106 by cp_parser_builtin_offsetof. We're looking for
7108 postfix-expression [ expression ]
7109 postfix-expression [ braced-init-list ] (C++11)
7111 FOR_OFFSETOF is set if we're being called in that context, which
7112 changes how we deal with integer constant expressions. */
7114 static tree
7115 cp_parser_postfix_open_square_expression (cp_parser *parser,
7116 tree postfix_expression,
7117 bool for_offsetof,
7118 bool decltype_p)
7120 tree index = NULL_TREE;
7121 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7122 bool saved_greater_than_is_operator_p;
7124 /* Consume the `[' token. */
7125 cp_lexer_consume_token (parser->lexer);
7127 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7128 parser->greater_than_is_operator_p = true;
7130 /* Parse the index expression. */
7131 /* ??? For offsetof, there is a question of what to allow here. If
7132 offsetof is not being used in an integral constant expression context,
7133 then we *could* get the right answer by computing the value at runtime.
7134 If we are in an integral constant expression context, then we might
7135 could accept any constant expression; hard to say without analysis.
7136 Rather than open the barn door too wide right away, allow only integer
7137 constant expressions here. */
7138 if (for_offsetof)
7139 index = cp_parser_constant_expression (parser);
7140 else
7142 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7144 bool expr_nonconst_p;
7145 cp_lexer_set_source_position (parser->lexer);
7146 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7147 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7148 if (flag_cilkplus
7149 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7151 error_at (cp_lexer_peek_token (parser->lexer)->location,
7152 "braced list index is not allowed with array "
7153 "notation");
7154 cp_parser_skip_to_end_of_statement (parser);
7155 return error_mark_node;
7158 else if (flag_cilkplus)
7160 /* Here are have these two options:
7161 ARRAY[EXP : EXP] - Array notation expr with default
7162 stride of 1.
7163 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7164 stride. */
7165 tree an_exp = cp_parser_array_notation (loc, parser, &index,
7166 postfix_expression);
7167 if (an_exp)
7168 return an_exp;
7170 else
7171 index = cp_parser_expression (parser);
7174 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7176 /* Look for the closing `]'. */
7177 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7179 /* Build the ARRAY_REF. */
7180 postfix_expression = grok_array_decl (loc, postfix_expression,
7181 index, decltype_p);
7183 /* When not doing offsetof, array references are not permitted in
7184 constant-expressions. */
7185 if (!for_offsetof
7186 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7187 postfix_expression = error_mark_node;
7189 return postfix_expression;
7192 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7193 by cp_parser_builtin_offsetof. We're looking for
7195 postfix-expression . template [opt] id-expression
7196 postfix-expression . pseudo-destructor-name
7197 postfix-expression -> template [opt] id-expression
7198 postfix-expression -> pseudo-destructor-name
7200 FOR_OFFSETOF is set if we're being called in that context. That sorta
7201 limits what of the above we'll actually accept, but nevermind.
7202 TOKEN_TYPE is the "." or "->" token, which will already have been
7203 removed from the stream. */
7205 static tree
7206 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7207 enum cpp_ttype token_type,
7208 cp_expr postfix_expression,
7209 bool for_offsetof, cp_id_kind *idk,
7210 location_t location)
7212 tree name;
7213 bool dependent_p;
7214 bool pseudo_destructor_p;
7215 tree scope = NULL_TREE;
7216 location_t start_loc = postfix_expression.get_start ();
7218 /* If this is a `->' operator, dereference the pointer. */
7219 if (token_type == CPP_DEREF)
7220 postfix_expression = build_x_arrow (location, postfix_expression,
7221 tf_warning_or_error);
7222 /* Check to see whether or not the expression is type-dependent and
7223 not the current instantiation. */
7224 dependent_p = type_dependent_object_expression_p (postfix_expression);
7225 /* The identifier following the `->' or `.' is not qualified. */
7226 parser->scope = NULL_TREE;
7227 parser->qualifying_scope = NULL_TREE;
7228 parser->object_scope = NULL_TREE;
7229 *idk = CP_ID_KIND_NONE;
7231 /* Enter the scope corresponding to the type of the object
7232 given by the POSTFIX_EXPRESSION. */
7233 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
7235 scope = TREE_TYPE (postfix_expression);
7236 /* According to the standard, no expression should ever have
7237 reference type. Unfortunately, we do not currently match
7238 the standard in this respect in that our internal representation
7239 of an expression may have reference type even when the standard
7240 says it does not. Therefore, we have to manually obtain the
7241 underlying type here. */
7242 scope = non_reference (scope);
7243 /* The type of the POSTFIX_EXPRESSION must be complete. */
7244 /* Unlike the object expression in other contexts, *this is not
7245 required to be of complete type for purposes of class member
7246 access (5.2.5) outside the member function body. */
7247 if (postfix_expression != current_class_ref
7248 && !(processing_template_decl
7249 && current_class_type
7250 && (same_type_ignoring_top_level_qualifiers_p
7251 (scope, current_class_type))))
7252 scope = complete_type_or_else (scope, postfix_expression);
7253 /* Let the name lookup machinery know that we are processing a
7254 class member access expression. */
7255 parser->context->object_type = scope;
7256 /* If something went wrong, we want to be able to discern that case,
7257 as opposed to the case where there was no SCOPE due to the type
7258 of expression being dependent. */
7259 if (!scope)
7260 scope = error_mark_node;
7261 /* If the SCOPE was erroneous, make the various semantic analysis
7262 functions exit quickly -- and without issuing additional error
7263 messages. */
7264 if (scope == error_mark_node)
7265 postfix_expression = error_mark_node;
7267 else
7268 /* Tell cp_parser_lookup_name that there was an object, even though it's
7269 type-dependent. */
7270 parser->context->object_type = unknown_type_node;
7272 /* Assume this expression is not a pseudo-destructor access. */
7273 pseudo_destructor_p = false;
7275 /* If the SCOPE is a scalar type, then, if this is a valid program,
7276 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7277 is type dependent, it can be pseudo-destructor-name or something else.
7278 Try to parse it as pseudo-destructor-name first. */
7279 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7281 tree s;
7282 tree type;
7284 cp_parser_parse_tentatively (parser);
7285 /* Parse the pseudo-destructor-name. */
7286 s = NULL_TREE;
7287 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7288 &s, &type);
7289 if (dependent_p
7290 && (cp_parser_error_occurred (parser)
7291 || !SCALAR_TYPE_P (type)))
7292 cp_parser_abort_tentative_parse (parser);
7293 else if (cp_parser_parse_definitely (parser))
7295 pseudo_destructor_p = true;
7296 postfix_expression
7297 = finish_pseudo_destructor_expr (postfix_expression,
7298 s, type, location);
7302 if (!pseudo_destructor_p)
7304 /* If the SCOPE is not a scalar type, we are looking at an
7305 ordinary class member access expression, rather than a
7306 pseudo-destructor-name. */
7307 bool template_p;
7308 cp_token *token = cp_lexer_peek_token (parser->lexer);
7309 /* Parse the id-expression. */
7310 name = (cp_parser_id_expression
7311 (parser,
7312 cp_parser_optional_template_keyword (parser),
7313 /*check_dependency_p=*/true,
7314 &template_p,
7315 /*declarator_p=*/false,
7316 /*optional_p=*/false));
7317 /* In general, build a SCOPE_REF if the member name is qualified.
7318 However, if the name was not dependent and has already been
7319 resolved; there is no need to build the SCOPE_REF. For example;
7321 struct X { void f(); };
7322 template <typename T> void f(T* t) { t->X::f(); }
7324 Even though "t" is dependent, "X::f" is not and has been resolved
7325 to a BASELINK; there is no need to include scope information. */
7327 /* But we do need to remember that there was an explicit scope for
7328 virtual function calls. */
7329 if (parser->scope)
7330 *idk = CP_ID_KIND_QUALIFIED;
7332 /* If the name is a template-id that names a type, we will get a
7333 TYPE_DECL here. That is invalid code. */
7334 if (TREE_CODE (name) == TYPE_DECL)
7336 error_at (token->location, "invalid use of %qD", name);
7337 postfix_expression = error_mark_node;
7339 else
7341 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7343 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7345 error_at (token->location, "%<%D::%D%> is not a class member",
7346 parser->scope, name);
7347 postfix_expression = error_mark_node;
7349 else
7350 name = build_qualified_name (/*type=*/NULL_TREE,
7351 parser->scope,
7352 name,
7353 template_p);
7354 parser->scope = NULL_TREE;
7355 parser->qualifying_scope = NULL_TREE;
7356 parser->object_scope = NULL_TREE;
7358 if (parser->scope && name && BASELINK_P (name))
7359 adjust_result_of_qualified_name_lookup
7360 (name, parser->scope, scope);
7361 postfix_expression
7362 = finish_class_member_access_expr (postfix_expression, name,
7363 template_p,
7364 tf_warning_or_error);
7365 /* Build a location e.g.:
7366 ptr->access_expr
7367 ~~~^~~~~~~~~~~~~
7368 where the caret is at the deref token, ranging from
7369 the start of postfix_expression to the end of the access expr. */
7370 location_t end_loc
7371 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7372 location_t combined_loc
7373 = make_location (input_location, start_loc, end_loc);
7374 protected_set_expr_location (postfix_expression, combined_loc);
7378 /* We no longer need to look up names in the scope of the object on
7379 the left-hand side of the `.' or `->' operator. */
7380 parser->context->object_type = NULL_TREE;
7382 /* Outside of offsetof, these operators may not appear in
7383 constant-expressions. */
7384 if (!for_offsetof
7385 && (cp_parser_non_integral_constant_expression
7386 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7387 postfix_expression = error_mark_node;
7389 return postfix_expression;
7392 /* Parse a parenthesized expression-list.
7394 expression-list:
7395 assignment-expression
7396 expression-list, assignment-expression
7398 attribute-list:
7399 expression-list
7400 identifier
7401 identifier, expression-list
7403 CAST_P is true if this expression is the target of a cast.
7405 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7406 argument pack.
7408 Returns a vector of trees. Each element is a representation of an
7409 assignment-expression. NULL is returned if the ( and or ) are
7410 missing. An empty, but allocated, vector is returned on no
7411 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7412 if we are parsing an attribute list for an attribute that wants a
7413 plain identifier argument, normal_attr for an attribute that wants
7414 an expression, or non_attr if we aren't parsing an attribute list. If
7415 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7416 not all of the expressions in the list were constant.
7417 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7418 will be written to with the location of the closing parenthesis. If
7419 an error occurs, it may or may not be written to. */
7421 static vec<tree, va_gc> *
7422 cp_parser_parenthesized_expression_list (cp_parser* parser,
7423 int is_attribute_list,
7424 bool cast_p,
7425 bool allow_expansion_p,
7426 bool *non_constant_p,
7427 location_t *close_paren_loc)
7429 vec<tree, va_gc> *expression_list;
7430 bool fold_expr_p = is_attribute_list != non_attr;
7431 tree identifier = NULL_TREE;
7432 bool saved_greater_than_is_operator_p;
7434 /* Assume all the expressions will be constant. */
7435 if (non_constant_p)
7436 *non_constant_p = false;
7438 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
7439 return NULL;
7441 expression_list = make_tree_vector ();
7443 /* Within a parenthesized expression, a `>' token is always
7444 the greater-than operator. */
7445 saved_greater_than_is_operator_p
7446 = parser->greater_than_is_operator_p;
7447 parser->greater_than_is_operator_p = true;
7449 /* Consume expressions until there are no more. */
7450 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7451 while (true)
7453 tree expr;
7455 /* At the beginning of attribute lists, check to see if the
7456 next token is an identifier. */
7457 if (is_attribute_list == id_attr
7458 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7460 cp_token *token;
7462 /* Consume the identifier. */
7463 token = cp_lexer_consume_token (parser->lexer);
7464 /* Save the identifier. */
7465 identifier = token->u.value;
7467 else
7469 bool expr_non_constant_p;
7471 /* Parse the next assignment-expression. */
7472 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7474 /* A braced-init-list. */
7475 cp_lexer_set_source_position (parser->lexer);
7476 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7477 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7478 if (non_constant_p && expr_non_constant_p)
7479 *non_constant_p = true;
7481 else if (non_constant_p)
7483 expr = (cp_parser_constant_expression
7484 (parser, /*allow_non_constant_p=*/true,
7485 &expr_non_constant_p));
7486 if (expr_non_constant_p)
7487 *non_constant_p = true;
7489 else
7490 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7491 cast_p);
7493 if (fold_expr_p)
7494 expr = instantiate_non_dependent_expr (expr);
7496 /* If we have an ellipsis, then this is an expression
7497 expansion. */
7498 if (allow_expansion_p
7499 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7501 /* Consume the `...'. */
7502 cp_lexer_consume_token (parser->lexer);
7504 /* Build the argument pack. */
7505 expr = make_pack_expansion (expr);
7508 /* Add it to the list. We add error_mark_node
7509 expressions to the list, so that we can still tell if
7510 the correct form for a parenthesized expression-list
7511 is found. That gives better errors. */
7512 vec_safe_push (expression_list, expr);
7514 if (expr == error_mark_node)
7515 goto skip_comma;
7518 /* After the first item, attribute lists look the same as
7519 expression lists. */
7520 is_attribute_list = non_attr;
7522 get_comma:;
7523 /* If the next token isn't a `,', then we are done. */
7524 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7525 break;
7527 /* Otherwise, consume the `,' and keep going. */
7528 cp_lexer_consume_token (parser->lexer);
7531 if (close_paren_loc)
7532 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7534 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7536 int ending;
7538 skip_comma:;
7539 /* We try and resync to an unnested comma, as that will give the
7540 user better diagnostics. */
7541 ending = cp_parser_skip_to_closing_parenthesis (parser,
7542 /*recovering=*/true,
7543 /*or_comma=*/true,
7544 /*consume_paren=*/true);
7545 if (ending < 0)
7546 goto get_comma;
7547 if (!ending)
7549 parser->greater_than_is_operator_p
7550 = saved_greater_than_is_operator_p;
7551 return NULL;
7555 parser->greater_than_is_operator_p
7556 = saved_greater_than_is_operator_p;
7558 if (identifier)
7559 vec_safe_insert (expression_list, 0, identifier);
7561 return expression_list;
7564 /* Parse a pseudo-destructor-name.
7566 pseudo-destructor-name:
7567 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7568 :: [opt] nested-name-specifier template template-id :: ~ type-name
7569 :: [opt] nested-name-specifier [opt] ~ type-name
7571 If either of the first two productions is used, sets *SCOPE to the
7572 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7573 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7574 or ERROR_MARK_NODE if the parse fails. */
7576 static void
7577 cp_parser_pseudo_destructor_name (cp_parser* parser,
7578 tree object,
7579 tree* scope,
7580 tree* type)
7582 bool nested_name_specifier_p;
7584 /* Handle ~auto. */
7585 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7586 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7587 && !type_dependent_expression_p (object))
7589 if (cxx_dialect < cxx14)
7590 pedwarn (input_location, 0,
7591 "%<~auto%> only available with "
7592 "-std=c++14 or -std=gnu++14");
7593 cp_lexer_consume_token (parser->lexer);
7594 cp_lexer_consume_token (parser->lexer);
7595 *scope = NULL_TREE;
7596 *type = TREE_TYPE (object);
7597 return;
7600 /* Assume that things will not work out. */
7601 *type = error_mark_node;
7603 /* Look for the optional `::' operator. */
7604 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7605 /* Look for the optional nested-name-specifier. */
7606 nested_name_specifier_p
7607 = (cp_parser_nested_name_specifier_opt (parser,
7608 /*typename_keyword_p=*/false,
7609 /*check_dependency_p=*/true,
7610 /*type_p=*/false,
7611 /*is_declaration=*/false)
7612 != NULL_TREE);
7613 /* Now, if we saw a nested-name-specifier, we might be doing the
7614 second production. */
7615 if (nested_name_specifier_p
7616 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7618 /* Consume the `template' keyword. */
7619 cp_lexer_consume_token (parser->lexer);
7620 /* Parse the template-id. */
7621 cp_parser_template_id (parser,
7622 /*template_keyword_p=*/true,
7623 /*check_dependency_p=*/false,
7624 class_type,
7625 /*is_declaration=*/true);
7626 /* Look for the `::' token. */
7627 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7629 /* If the next token is not a `~', then there might be some
7630 additional qualification. */
7631 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7633 /* At this point, we're looking for "type-name :: ~". The type-name
7634 must not be a class-name, since this is a pseudo-destructor. So,
7635 it must be either an enum-name, or a typedef-name -- both of which
7636 are just identifiers. So, we peek ahead to check that the "::"
7637 and "~" tokens are present; if they are not, then we can avoid
7638 calling type_name. */
7639 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7640 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7641 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7643 cp_parser_error (parser, "non-scalar type");
7644 return;
7647 /* Look for the type-name. */
7648 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7649 if (*scope == error_mark_node)
7650 return;
7652 /* Look for the `::' token. */
7653 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7655 else
7656 *scope = NULL_TREE;
7658 /* Look for the `~'. */
7659 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7661 /* Once we see the ~, this has to be a pseudo-destructor. */
7662 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7663 cp_parser_commit_to_topmost_tentative_parse (parser);
7665 /* Look for the type-name again. We are not responsible for
7666 checking that it matches the first type-name. */
7667 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7670 /* Parse a unary-expression.
7672 unary-expression:
7673 postfix-expression
7674 ++ cast-expression
7675 -- cast-expression
7676 unary-operator cast-expression
7677 sizeof unary-expression
7678 sizeof ( type-id )
7679 alignof ( type-id ) [C++0x]
7680 new-expression
7681 delete-expression
7683 GNU Extensions:
7685 unary-expression:
7686 __extension__ cast-expression
7687 __alignof__ unary-expression
7688 __alignof__ ( type-id )
7689 alignof unary-expression [C++0x]
7690 __real__ cast-expression
7691 __imag__ cast-expression
7692 && identifier
7693 sizeof ( type-id ) { initializer-list , [opt] }
7694 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7695 __alignof__ ( type-id ) { initializer-list , [opt] }
7697 ADDRESS_P is true iff the unary-expression is appearing as the
7698 operand of the `&' operator. CAST_P is true if this expression is
7699 the target of a cast.
7701 Returns a representation of the expression. */
7703 static cp_expr
7704 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7705 bool address_p, bool cast_p, bool decltype_p)
7707 cp_token *token;
7708 enum tree_code unary_operator;
7710 /* Peek at the next token. */
7711 token = cp_lexer_peek_token (parser->lexer);
7712 /* Some keywords give away the kind of expression. */
7713 if (token->type == CPP_KEYWORD)
7715 enum rid keyword = token->keyword;
7717 switch (keyword)
7719 case RID_ALIGNOF:
7720 case RID_SIZEOF:
7722 tree operand, ret;
7723 enum tree_code op;
7724 location_t first_loc;
7726 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7727 /* Consume the token. */
7728 cp_lexer_consume_token (parser->lexer);
7729 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7730 /* Parse the operand. */
7731 operand = cp_parser_sizeof_operand (parser, keyword);
7733 if (TYPE_P (operand))
7734 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7735 else
7737 /* ISO C++ defines alignof only with types, not with
7738 expressions. So pedwarn if alignof is used with a non-
7739 type expression. However, __alignof__ is ok. */
7740 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7741 pedwarn (token->location, OPT_Wpedantic,
7742 "ISO C++ does not allow %<alignof%> "
7743 "with a non-type");
7745 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7747 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7748 SIZEOF_EXPR with the original operand. */
7749 if (op == SIZEOF_EXPR && ret != error_mark_node)
7751 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7753 if (!processing_template_decl && TYPE_P (operand))
7755 ret = build_min (SIZEOF_EXPR, size_type_node,
7756 build1 (NOP_EXPR, operand,
7757 error_mark_node));
7758 SIZEOF_EXPR_TYPE_P (ret) = 1;
7760 else
7761 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7762 TREE_SIDE_EFFECTS (ret) = 0;
7763 TREE_READONLY (ret) = 1;
7765 SET_EXPR_LOCATION (ret, first_loc);
7767 return ret;
7770 case RID_NEW:
7771 return cp_parser_new_expression (parser);
7773 case RID_DELETE:
7774 return cp_parser_delete_expression (parser);
7776 case RID_EXTENSION:
7778 /* The saved value of the PEDANTIC flag. */
7779 int saved_pedantic;
7780 tree expr;
7782 /* Save away the PEDANTIC flag. */
7783 cp_parser_extension_opt (parser, &saved_pedantic);
7784 /* Parse the cast-expression. */
7785 expr = cp_parser_simple_cast_expression (parser);
7786 /* Restore the PEDANTIC flag. */
7787 pedantic = saved_pedantic;
7789 return expr;
7792 case RID_REALPART:
7793 case RID_IMAGPART:
7795 tree expression;
7797 /* Consume the `__real__' or `__imag__' token. */
7798 cp_lexer_consume_token (parser->lexer);
7799 /* Parse the cast-expression. */
7800 expression = cp_parser_simple_cast_expression (parser);
7801 /* Create the complete representation. */
7802 return build_x_unary_op (token->location,
7803 (keyword == RID_REALPART
7804 ? REALPART_EXPR : IMAGPART_EXPR),
7805 expression,
7806 tf_warning_or_error);
7808 break;
7810 case RID_TRANSACTION_ATOMIC:
7811 case RID_TRANSACTION_RELAXED:
7812 return cp_parser_transaction_expression (parser, keyword);
7814 case RID_NOEXCEPT:
7816 tree expr;
7817 const char *saved_message;
7818 bool saved_integral_constant_expression_p;
7819 bool saved_non_integral_constant_expression_p;
7820 bool saved_greater_than_is_operator_p;
7822 cp_lexer_consume_token (parser->lexer);
7823 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7825 saved_message = parser->type_definition_forbidden_message;
7826 parser->type_definition_forbidden_message
7827 = G_("types may not be defined in %<noexcept%> expressions");
7829 saved_integral_constant_expression_p
7830 = parser->integral_constant_expression_p;
7831 saved_non_integral_constant_expression_p
7832 = parser->non_integral_constant_expression_p;
7833 parser->integral_constant_expression_p = false;
7835 saved_greater_than_is_operator_p
7836 = parser->greater_than_is_operator_p;
7837 parser->greater_than_is_operator_p = true;
7839 ++cp_unevaluated_operand;
7840 ++c_inhibit_evaluation_warnings;
7841 ++cp_noexcept_operand;
7842 expr = cp_parser_expression (parser);
7843 --cp_noexcept_operand;
7844 --c_inhibit_evaluation_warnings;
7845 --cp_unevaluated_operand;
7847 parser->greater_than_is_operator_p
7848 = saved_greater_than_is_operator_p;
7850 parser->integral_constant_expression_p
7851 = saved_integral_constant_expression_p;
7852 parser->non_integral_constant_expression_p
7853 = saved_non_integral_constant_expression_p;
7855 parser->type_definition_forbidden_message = saved_message;
7857 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7858 return finish_noexcept_expr (expr, tf_warning_or_error);
7861 default:
7862 break;
7866 /* Look for the `:: new' and `:: delete', which also signal the
7867 beginning of a new-expression, or delete-expression,
7868 respectively. If the next token is `::', then it might be one of
7869 these. */
7870 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7872 enum rid keyword;
7874 /* See if the token after the `::' is one of the keywords in
7875 which we're interested. */
7876 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7877 /* If it's `new', we have a new-expression. */
7878 if (keyword == RID_NEW)
7879 return cp_parser_new_expression (parser);
7880 /* Similarly, for `delete'. */
7881 else if (keyword == RID_DELETE)
7882 return cp_parser_delete_expression (parser);
7885 /* Look for a unary operator. */
7886 unary_operator = cp_parser_unary_operator (token);
7887 /* The `++' and `--' operators can be handled similarly, even though
7888 they are not technically unary-operators in the grammar. */
7889 if (unary_operator == ERROR_MARK)
7891 if (token->type == CPP_PLUS_PLUS)
7892 unary_operator = PREINCREMENT_EXPR;
7893 else if (token->type == CPP_MINUS_MINUS)
7894 unary_operator = PREDECREMENT_EXPR;
7895 /* Handle the GNU address-of-label extension. */
7896 else if (cp_parser_allow_gnu_extensions_p (parser)
7897 && token->type == CPP_AND_AND)
7899 tree identifier;
7900 tree expression;
7901 location_t start_loc = token->location;
7903 /* Consume the '&&' token. */
7904 cp_lexer_consume_token (parser->lexer);
7905 /* Look for the identifier. */
7906 location_t finish_loc
7907 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
7908 identifier = cp_parser_identifier (parser);
7909 /* Construct a location of the form:
7910 &&label
7911 ^~~~~~~
7912 with caret==start at the "&&", finish at the end of the label. */
7913 location_t combined_loc
7914 = make_location (start_loc, start_loc, finish_loc);
7915 /* Create an expression representing the address. */
7916 expression = finish_label_address_expr (identifier, combined_loc);
7917 if (cp_parser_non_integral_constant_expression (parser,
7918 NIC_ADDR_LABEL))
7919 expression = error_mark_node;
7920 return expression;
7923 if (unary_operator != ERROR_MARK)
7925 cp_expr cast_expression;
7926 cp_expr expression = error_mark_node;
7927 non_integral_constant non_constant_p = NIC_NONE;
7928 location_t loc = token->location;
7929 tsubst_flags_t complain = complain_flags (decltype_p);
7931 /* Consume the operator token. */
7932 token = cp_lexer_consume_token (parser->lexer);
7933 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
7935 /* Parse the cast-expression. */
7936 cast_expression
7937 = cp_parser_cast_expression (parser,
7938 unary_operator == ADDR_EXPR,
7939 /*cast_p=*/false,
7940 /*decltype*/false,
7941 pidk);
7943 /* Make a location:
7944 OP_TOKEN CAST_EXPRESSION
7945 ^~~~~~~~~~~~~~~~~~~~~~~~~
7946 with start==caret at the operator token, and
7947 extending to the end of the cast_expression. */
7948 loc = make_location (loc, loc, cast_expression.get_finish ());
7950 /* Now, build an appropriate representation. */
7951 switch (unary_operator)
7953 case INDIRECT_REF:
7954 non_constant_p = NIC_STAR;
7955 expression = build_x_indirect_ref (loc, cast_expression,
7956 RO_UNARY_STAR,
7957 complain);
7958 /* TODO: build_x_indirect_ref does not always honor the
7959 location, so ensure it is set. */
7960 expression.set_location (loc);
7961 break;
7963 case ADDR_EXPR:
7964 non_constant_p = NIC_ADDR;
7965 /* Fall through. */
7966 case BIT_NOT_EXPR:
7967 expression = build_x_unary_op (loc, unary_operator,
7968 cast_expression,
7969 complain);
7970 /* TODO: build_x_unary_op does not always honor the location,
7971 so ensure it is set. */
7972 expression.set_location (loc);
7973 break;
7975 case PREINCREMENT_EXPR:
7976 case PREDECREMENT_EXPR:
7977 non_constant_p = unary_operator == PREINCREMENT_EXPR
7978 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7979 /* Fall through. */
7980 case NEGATE_EXPR:
7981 /* Immediately fold negation of a constant, unless the constant is 0
7982 (since -0 == 0) or it would overflow. */
7983 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
7984 && CONSTANT_CLASS_P (cast_expression)
7985 && !integer_zerop (cast_expression)
7986 && !TREE_OVERFLOW (cast_expression))
7988 tree folded = fold_build1 (unary_operator,
7989 TREE_TYPE (cast_expression),
7990 cast_expression);
7991 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
7993 expression = cp_expr (folded, loc);
7994 break;
7997 /* Fall through. */
7998 case UNARY_PLUS_EXPR:
7999 case TRUTH_NOT_EXPR:
8000 expression = finish_unary_op_expr (loc, unary_operator,
8001 cast_expression, complain);
8002 break;
8004 default:
8005 gcc_unreachable ();
8008 if (non_constant_p != NIC_NONE
8009 && cp_parser_non_integral_constant_expression (parser,
8010 non_constant_p))
8011 expression = error_mark_node;
8013 return expression;
8016 return cp_parser_postfix_expression (parser, address_p, cast_p,
8017 /*member_access_only_p=*/false,
8018 decltype_p,
8019 pidk);
8022 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8023 unary-operator, the corresponding tree code is returned. */
8025 static enum tree_code
8026 cp_parser_unary_operator (cp_token* token)
8028 switch (token->type)
8030 case CPP_MULT:
8031 return INDIRECT_REF;
8033 case CPP_AND:
8034 return ADDR_EXPR;
8036 case CPP_PLUS:
8037 return UNARY_PLUS_EXPR;
8039 case CPP_MINUS:
8040 return NEGATE_EXPR;
8042 case CPP_NOT:
8043 return TRUTH_NOT_EXPR;
8045 case CPP_COMPL:
8046 return BIT_NOT_EXPR;
8048 default:
8049 return ERROR_MARK;
8053 /* Parse a new-expression.
8055 new-expression:
8056 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8057 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8059 Returns a representation of the expression. */
8061 static tree
8062 cp_parser_new_expression (cp_parser* parser)
8064 bool global_scope_p;
8065 vec<tree, va_gc> *placement;
8066 tree type;
8067 vec<tree, va_gc> *initializer;
8068 tree nelts = NULL_TREE;
8069 tree ret;
8071 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8073 /* Look for the optional `::' operator. */
8074 global_scope_p
8075 = (cp_parser_global_scope_opt (parser,
8076 /*current_scope_valid_p=*/false)
8077 != NULL_TREE);
8078 /* Look for the `new' operator. */
8079 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8080 /* There's no easy way to tell a new-placement from the
8081 `( type-id )' construct. */
8082 cp_parser_parse_tentatively (parser);
8083 /* Look for a new-placement. */
8084 placement = cp_parser_new_placement (parser);
8085 /* If that didn't work out, there's no new-placement. */
8086 if (!cp_parser_parse_definitely (parser))
8088 if (placement != NULL)
8089 release_tree_vector (placement);
8090 placement = NULL;
8093 /* If the next token is a `(', then we have a parenthesized
8094 type-id. */
8095 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8097 cp_token *token;
8098 const char *saved_message = parser->type_definition_forbidden_message;
8100 /* Consume the `('. */
8101 cp_lexer_consume_token (parser->lexer);
8103 /* Parse the type-id. */
8104 parser->type_definition_forbidden_message
8105 = G_("types may not be defined in a new-expression");
8107 type_id_in_expr_sentinel s (parser);
8108 type = cp_parser_type_id (parser);
8110 parser->type_definition_forbidden_message = saved_message;
8112 /* Look for the closing `)'. */
8113 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8114 token = cp_lexer_peek_token (parser->lexer);
8115 /* There should not be a direct-new-declarator in this production,
8116 but GCC used to allowed this, so we check and emit a sensible error
8117 message for this case. */
8118 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8120 error_at (token->location,
8121 "array bound forbidden after parenthesized type-id");
8122 inform (token->location,
8123 "try removing the parentheses around the type-id");
8124 cp_parser_direct_new_declarator (parser);
8127 /* Otherwise, there must be a new-type-id. */
8128 else
8129 type = cp_parser_new_type_id (parser, &nelts);
8131 /* If the next token is a `(' or '{', then we have a new-initializer. */
8132 cp_token *token = cp_lexer_peek_token (parser->lexer);
8133 if (token->type == CPP_OPEN_PAREN
8134 || token->type == CPP_OPEN_BRACE)
8135 initializer = cp_parser_new_initializer (parser);
8136 else
8137 initializer = NULL;
8139 /* A new-expression may not appear in an integral constant
8140 expression. */
8141 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8142 ret = error_mark_node;
8143 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8144 of a new-type-id or type-id of a new-expression, the new-expression shall
8145 contain a new-initializer of the form ( assignment-expression )".
8146 Additionally, consistently with the spirit of DR 1467, we want to accept
8147 'new auto { 2 }' too. */
8148 else if (type_uses_auto (type)
8149 && (vec_safe_length (initializer) != 1
8150 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8151 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8153 error_at (token->location,
8154 "initialization of new-expression for type %<auto%> "
8155 "requires exactly one element");
8156 ret = error_mark_node;
8158 else
8160 /* Construct a location e.g.:
8161 ptr = new int[100]
8162 ^~~~~~~~~~~~
8163 with caret == start at the start of the "new" token, and the end
8164 at the end of the final token we consumed. */
8165 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8166 location_t end_loc = get_finish (end_tok->location);
8167 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8169 /* Create a representation of the new-expression. */
8170 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8171 tf_warning_or_error);
8172 protected_set_expr_location (ret, combined_loc);
8175 if (placement != NULL)
8176 release_tree_vector (placement);
8177 if (initializer != NULL)
8178 release_tree_vector (initializer);
8180 return ret;
8183 /* Parse a new-placement.
8185 new-placement:
8186 ( expression-list )
8188 Returns the same representation as for an expression-list. */
8190 static vec<tree, va_gc> *
8191 cp_parser_new_placement (cp_parser* parser)
8193 vec<tree, va_gc> *expression_list;
8195 /* Parse the expression-list. */
8196 expression_list = (cp_parser_parenthesized_expression_list
8197 (parser, non_attr, /*cast_p=*/false,
8198 /*allow_expansion_p=*/true,
8199 /*non_constant_p=*/NULL));
8201 if (expression_list && expression_list->is_empty ())
8202 error ("expected expression-list or type-id");
8204 return expression_list;
8207 /* Parse a new-type-id.
8209 new-type-id:
8210 type-specifier-seq new-declarator [opt]
8212 Returns the TYPE allocated. If the new-type-id indicates an array
8213 type, *NELTS is set to the number of elements in the last array
8214 bound; the TYPE will not include the last array bound. */
8216 static tree
8217 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8219 cp_decl_specifier_seq type_specifier_seq;
8220 cp_declarator *new_declarator;
8221 cp_declarator *declarator;
8222 cp_declarator *outer_declarator;
8223 const char *saved_message;
8225 /* The type-specifier sequence must not contain type definitions.
8226 (It cannot contain declarations of new types either, but if they
8227 are not definitions we will catch that because they are not
8228 complete.) */
8229 saved_message = parser->type_definition_forbidden_message;
8230 parser->type_definition_forbidden_message
8231 = G_("types may not be defined in a new-type-id");
8232 /* Parse the type-specifier-seq. */
8233 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8234 /*is_trailing_return=*/false,
8235 &type_specifier_seq);
8236 /* Restore the old message. */
8237 parser->type_definition_forbidden_message = saved_message;
8239 if (type_specifier_seq.type == error_mark_node)
8240 return error_mark_node;
8242 /* Parse the new-declarator. */
8243 new_declarator = cp_parser_new_declarator_opt (parser);
8245 /* Determine the number of elements in the last array dimension, if
8246 any. */
8247 *nelts = NULL_TREE;
8248 /* Skip down to the last array dimension. */
8249 declarator = new_declarator;
8250 outer_declarator = NULL;
8251 while (declarator && (declarator->kind == cdk_pointer
8252 || declarator->kind == cdk_ptrmem))
8254 outer_declarator = declarator;
8255 declarator = declarator->declarator;
8257 while (declarator
8258 && declarator->kind == cdk_array
8259 && declarator->declarator
8260 && declarator->declarator->kind == cdk_array)
8262 outer_declarator = declarator;
8263 declarator = declarator->declarator;
8266 if (declarator && declarator->kind == cdk_array)
8268 *nelts = declarator->u.array.bounds;
8269 if (*nelts == error_mark_node)
8270 *nelts = integer_one_node;
8272 if (outer_declarator)
8273 outer_declarator->declarator = declarator->declarator;
8274 else
8275 new_declarator = NULL;
8278 return groktypename (&type_specifier_seq, new_declarator, false);
8281 /* Parse an (optional) new-declarator.
8283 new-declarator:
8284 ptr-operator new-declarator [opt]
8285 direct-new-declarator
8287 Returns the declarator. */
8289 static cp_declarator *
8290 cp_parser_new_declarator_opt (cp_parser* parser)
8292 enum tree_code code;
8293 tree type, std_attributes = NULL_TREE;
8294 cp_cv_quals cv_quals;
8296 /* We don't know if there's a ptr-operator next, or not. */
8297 cp_parser_parse_tentatively (parser);
8298 /* Look for a ptr-operator. */
8299 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8300 /* If that worked, look for more new-declarators. */
8301 if (cp_parser_parse_definitely (parser))
8303 cp_declarator *declarator;
8305 /* Parse another optional declarator. */
8306 declarator = cp_parser_new_declarator_opt (parser);
8308 declarator = cp_parser_make_indirect_declarator
8309 (code, type, cv_quals, declarator, std_attributes);
8311 return declarator;
8314 /* If the next token is a `[', there is a direct-new-declarator. */
8315 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8316 return cp_parser_direct_new_declarator (parser);
8318 return NULL;
8321 /* Parse a direct-new-declarator.
8323 direct-new-declarator:
8324 [ expression ]
8325 direct-new-declarator [constant-expression]
8329 static cp_declarator *
8330 cp_parser_direct_new_declarator (cp_parser* parser)
8332 cp_declarator *declarator = NULL;
8334 while (true)
8336 tree expression;
8337 cp_token *token;
8339 /* Look for the opening `['. */
8340 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8342 token = cp_lexer_peek_token (parser->lexer);
8343 expression = cp_parser_expression (parser);
8344 /* The standard requires that the expression have integral
8345 type. DR 74 adds enumeration types. We believe that the
8346 real intent is that these expressions be handled like the
8347 expression in a `switch' condition, which also allows
8348 classes with a single conversion to integral or
8349 enumeration type. */
8350 if (!processing_template_decl)
8352 expression
8353 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8354 expression,
8355 /*complain=*/true);
8356 if (!expression)
8358 error_at (token->location,
8359 "expression in new-declarator must have integral "
8360 "or enumeration type");
8361 expression = error_mark_node;
8365 /* Look for the closing `]'. */
8366 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8368 /* Add this bound to the declarator. */
8369 declarator = make_array_declarator (declarator, expression);
8371 /* If the next token is not a `[', then there are no more
8372 bounds. */
8373 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8374 break;
8377 return declarator;
8380 /* Parse a new-initializer.
8382 new-initializer:
8383 ( expression-list [opt] )
8384 braced-init-list
8386 Returns a representation of the expression-list. */
8388 static vec<tree, va_gc> *
8389 cp_parser_new_initializer (cp_parser* parser)
8391 vec<tree, va_gc> *expression_list;
8393 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8395 tree t;
8396 bool expr_non_constant_p;
8397 cp_lexer_set_source_position (parser->lexer);
8398 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8399 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8400 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8401 expression_list = make_tree_vector_single (t);
8403 else
8404 expression_list = (cp_parser_parenthesized_expression_list
8405 (parser, non_attr, /*cast_p=*/false,
8406 /*allow_expansion_p=*/true,
8407 /*non_constant_p=*/NULL));
8409 return expression_list;
8412 /* Parse a delete-expression.
8414 delete-expression:
8415 :: [opt] delete cast-expression
8416 :: [opt] delete [ ] cast-expression
8418 Returns a representation of the expression. */
8420 static tree
8421 cp_parser_delete_expression (cp_parser* parser)
8423 bool global_scope_p;
8424 bool array_p;
8425 tree expression;
8427 /* Look for the optional `::' operator. */
8428 global_scope_p
8429 = (cp_parser_global_scope_opt (parser,
8430 /*current_scope_valid_p=*/false)
8431 != NULL_TREE);
8432 /* Look for the `delete' keyword. */
8433 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8434 /* See if the array syntax is in use. */
8435 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8437 /* Consume the `[' token. */
8438 cp_lexer_consume_token (parser->lexer);
8439 /* Look for the `]' token. */
8440 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8441 /* Remember that this is the `[]' construct. */
8442 array_p = true;
8444 else
8445 array_p = false;
8447 /* Parse the cast-expression. */
8448 expression = cp_parser_simple_cast_expression (parser);
8450 /* A delete-expression may not appear in an integral constant
8451 expression. */
8452 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8453 return error_mark_node;
8455 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8456 tf_warning_or_error);
8459 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8460 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8461 0 otherwise. */
8463 static int
8464 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8466 cp_token *token = cp_lexer_peek_token (parser->lexer);
8467 switch (token->type)
8469 case CPP_COMMA:
8470 case CPP_SEMICOLON:
8471 case CPP_QUERY:
8472 case CPP_COLON:
8473 case CPP_CLOSE_SQUARE:
8474 case CPP_CLOSE_PAREN:
8475 case CPP_CLOSE_BRACE:
8476 case CPP_OPEN_BRACE:
8477 case CPP_DOT:
8478 case CPP_DOT_STAR:
8479 case CPP_DEREF:
8480 case CPP_DEREF_STAR:
8481 case CPP_DIV:
8482 case CPP_MOD:
8483 case CPP_LSHIFT:
8484 case CPP_RSHIFT:
8485 case CPP_LESS:
8486 case CPP_GREATER:
8487 case CPP_LESS_EQ:
8488 case CPP_GREATER_EQ:
8489 case CPP_EQ_EQ:
8490 case CPP_NOT_EQ:
8491 case CPP_EQ:
8492 case CPP_MULT_EQ:
8493 case CPP_DIV_EQ:
8494 case CPP_MOD_EQ:
8495 case CPP_PLUS_EQ:
8496 case CPP_MINUS_EQ:
8497 case CPP_RSHIFT_EQ:
8498 case CPP_LSHIFT_EQ:
8499 case CPP_AND_EQ:
8500 case CPP_XOR_EQ:
8501 case CPP_OR_EQ:
8502 case CPP_XOR:
8503 case CPP_OR:
8504 case CPP_OR_OR:
8505 case CPP_EOF:
8506 case CPP_ELLIPSIS:
8507 return 0;
8509 case CPP_OPEN_PAREN:
8510 /* In ((type ()) () the last () isn't a valid cast-expression,
8511 so the whole must be parsed as postfix-expression. */
8512 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8513 != CPP_CLOSE_PAREN;
8515 case CPP_OPEN_SQUARE:
8516 /* '[' may start a primary-expression in obj-c++ and in C++11,
8517 as a lambda-expression, eg, '(void)[]{}'. */
8518 if (cxx_dialect >= cxx11)
8519 return -1;
8520 return c_dialect_objc ();
8522 case CPP_PLUS_PLUS:
8523 case CPP_MINUS_MINUS:
8524 /* '++' and '--' may or may not start a cast-expression:
8526 struct T { void operator++(int); };
8527 void f() { (T())++; }
8531 int a;
8532 (int)++a; */
8533 return -1;
8535 default:
8536 return 1;
8540 /* Parse a cast-expression.
8542 cast-expression:
8543 unary-expression
8544 ( type-id ) cast-expression
8546 ADDRESS_P is true iff the unary-expression is appearing as the
8547 operand of the `&' operator. CAST_P is true if this expression is
8548 the target of a cast.
8550 Returns a representation of the expression. */
8552 static cp_expr
8553 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8554 bool decltype_p, cp_id_kind * pidk)
8556 /* If it's a `(', then we might be looking at a cast. */
8557 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8559 tree type = NULL_TREE;
8560 cp_expr expr (NULL_TREE);
8561 int cast_expression = 0;
8562 const char *saved_message;
8564 /* There's no way to know yet whether or not this is a cast.
8565 For example, `(int (3))' is a unary-expression, while `(int)
8566 3' is a cast. So, we resort to parsing tentatively. */
8567 cp_parser_parse_tentatively (parser);
8568 /* Types may not be defined in a cast. */
8569 saved_message = parser->type_definition_forbidden_message;
8570 parser->type_definition_forbidden_message
8571 = G_("types may not be defined in casts");
8572 /* Consume the `('. */
8573 cp_token *open_paren = cp_lexer_consume_token (parser->lexer);
8574 location_t open_paren_loc = open_paren->location;
8576 /* A very tricky bit is that `(struct S) { 3 }' is a
8577 compound-literal (which we permit in C++ as an extension).
8578 But, that construct is not a cast-expression -- it is a
8579 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8580 is legal; if the compound-literal were a cast-expression,
8581 you'd need an extra set of parentheses.) But, if we parse
8582 the type-id, and it happens to be a class-specifier, then we
8583 will commit to the parse at that point, because we cannot
8584 undo the action that is done when creating a new class. So,
8585 then we cannot back up and do a postfix-expression.
8587 Another tricky case is the following (c++/29234):
8589 struct S { void operator () (); };
8591 void foo ()
8593 ( S()() );
8596 As a type-id we parse the parenthesized S()() as a function
8597 returning a function, groktypename complains and we cannot
8598 back up in this case either.
8600 Therefore, we scan ahead to the closing `)', and check to see
8601 if the tokens after the `)' can start a cast-expression. Otherwise
8602 we are dealing with an unary-expression, a postfix-expression
8603 or something else.
8605 Yet another tricky case, in C++11, is the following (c++/54891):
8607 (void)[]{};
8609 The issue is that usually, besides the case of lambda-expressions,
8610 the parenthesized type-id cannot be followed by '[', and, eg, we
8611 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8612 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8613 we don't commit, we try a cast-expression, then an unary-expression.
8615 Save tokens so that we can put them back. */
8616 cp_lexer_save_tokens (parser->lexer);
8618 /* We may be looking at a cast-expression. */
8619 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8620 /*consume_paren=*/true))
8621 cast_expression
8622 = cp_parser_tokens_start_cast_expression (parser);
8624 /* Roll back the tokens we skipped. */
8625 cp_lexer_rollback_tokens (parser->lexer);
8626 /* If we aren't looking at a cast-expression, simulate an error so
8627 that the call to cp_parser_error_occurred below returns true. */
8628 if (!cast_expression)
8629 cp_parser_simulate_error (parser);
8630 else
8632 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8633 parser->in_type_id_in_expr_p = true;
8634 /* Look for the type-id. */
8635 type = cp_parser_type_id (parser);
8636 /* Look for the closing `)'. */
8637 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8638 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8641 /* Restore the saved message. */
8642 parser->type_definition_forbidden_message = saved_message;
8644 /* At this point this can only be either a cast or a
8645 parenthesized ctor such as `(T ())' that looks like a cast to
8646 function returning T. */
8647 if (!cp_parser_error_occurred (parser))
8649 /* Only commit if the cast-expression doesn't start with
8650 '++', '--', or '[' in C++11. */
8651 if (cast_expression > 0)
8652 cp_parser_commit_to_topmost_tentative_parse (parser);
8654 expr = cp_parser_cast_expression (parser,
8655 /*address_p=*/false,
8656 /*cast_p=*/true,
8657 /*decltype_p=*/false,
8658 pidk);
8660 if (cp_parser_parse_definitely (parser))
8662 /* Warn about old-style casts, if so requested. */
8663 if (warn_old_style_cast
8664 && !in_system_header_at (input_location)
8665 && !VOID_TYPE_P (type)
8666 && current_lang_name != lang_name_c)
8667 warning (OPT_Wold_style_cast, "use of old-style cast");
8669 /* Only type conversions to integral or enumeration types
8670 can be used in constant-expressions. */
8671 if (!cast_valid_in_integral_constant_expression_p (type)
8672 && cp_parser_non_integral_constant_expression (parser,
8673 NIC_CAST))
8674 return error_mark_node;
8676 /* Perform the cast. */
8677 /* Make a location:
8678 (TYPE) EXPR
8679 ^~~~~~~~~~~
8680 with start==caret at the open paren, extending to the
8681 end of "expr". */
8682 location_t cast_loc = make_location (open_paren_loc,
8683 open_paren_loc,
8684 expr.get_finish ());
8685 expr = build_c_cast (cast_loc, type, expr);
8686 return expr;
8689 else
8690 cp_parser_abort_tentative_parse (parser);
8693 /* If we get here, then it's not a cast, so it must be a
8694 unary-expression. */
8695 return cp_parser_unary_expression (parser, pidk, address_p,
8696 cast_p, decltype_p);
8699 /* Parse a binary expression of the general form:
8701 pm-expression:
8702 cast-expression
8703 pm-expression .* cast-expression
8704 pm-expression ->* cast-expression
8706 multiplicative-expression:
8707 pm-expression
8708 multiplicative-expression * pm-expression
8709 multiplicative-expression / pm-expression
8710 multiplicative-expression % pm-expression
8712 additive-expression:
8713 multiplicative-expression
8714 additive-expression + multiplicative-expression
8715 additive-expression - multiplicative-expression
8717 shift-expression:
8718 additive-expression
8719 shift-expression << additive-expression
8720 shift-expression >> additive-expression
8722 relational-expression:
8723 shift-expression
8724 relational-expression < shift-expression
8725 relational-expression > shift-expression
8726 relational-expression <= shift-expression
8727 relational-expression >= shift-expression
8729 GNU Extension:
8731 relational-expression:
8732 relational-expression <? shift-expression
8733 relational-expression >? shift-expression
8735 equality-expression:
8736 relational-expression
8737 equality-expression == relational-expression
8738 equality-expression != relational-expression
8740 and-expression:
8741 equality-expression
8742 and-expression & equality-expression
8744 exclusive-or-expression:
8745 and-expression
8746 exclusive-or-expression ^ and-expression
8748 inclusive-or-expression:
8749 exclusive-or-expression
8750 inclusive-or-expression | exclusive-or-expression
8752 logical-and-expression:
8753 inclusive-or-expression
8754 logical-and-expression && inclusive-or-expression
8756 logical-or-expression:
8757 logical-and-expression
8758 logical-or-expression || logical-and-expression
8760 All these are implemented with a single function like:
8762 binary-expression:
8763 simple-cast-expression
8764 binary-expression <token> binary-expression
8766 CAST_P is true if this expression is the target of a cast.
8768 The binops_by_token map is used to get the tree codes for each <token> type.
8769 binary-expressions are associated according to a precedence table. */
8771 #define TOKEN_PRECEDENCE(token) \
8772 (((token->type == CPP_GREATER \
8773 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8774 && !parser->greater_than_is_operator_p) \
8775 ? PREC_NOT_OPERATOR \
8776 : binops_by_token[token->type].prec)
8778 static cp_expr
8779 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8780 bool no_toplevel_fold_p,
8781 bool decltype_p,
8782 enum cp_parser_prec prec,
8783 cp_id_kind * pidk)
8785 cp_parser_expression_stack stack;
8786 cp_parser_expression_stack_entry *sp = &stack[0];
8787 cp_parser_expression_stack_entry current;
8788 cp_expr rhs;
8789 cp_token *token;
8790 enum tree_code rhs_type;
8791 enum cp_parser_prec new_prec, lookahead_prec;
8792 tree overload;
8794 /* Parse the first expression. */
8795 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8796 ? TRUTH_NOT_EXPR : ERROR_MARK);
8797 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8798 cast_p, decltype_p, pidk);
8799 current.prec = prec;
8801 if (cp_parser_error_occurred (parser))
8802 return error_mark_node;
8804 for (;;)
8806 /* Get an operator token. */
8807 token = cp_lexer_peek_token (parser->lexer);
8809 if (warn_cxx11_compat
8810 && token->type == CPP_RSHIFT
8811 && !parser->greater_than_is_operator_p)
8813 if (warning_at (token->location, OPT_Wc__11_compat,
8814 "%<>>%> operator is treated"
8815 " as two right angle brackets in C++11"))
8816 inform (token->location,
8817 "suggest parentheses around %<>>%> expression");
8820 new_prec = TOKEN_PRECEDENCE (token);
8821 if (new_prec != PREC_NOT_OPERATOR
8822 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8823 /* This is a fold-expression; handle it later. */
8824 new_prec = PREC_NOT_OPERATOR;
8826 /* Popping an entry off the stack means we completed a subexpression:
8827 - either we found a token which is not an operator (`>' where it is not
8828 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8829 will happen repeatedly;
8830 - or, we found an operator which has lower priority. This is the case
8831 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8832 parsing `3 * 4'. */
8833 if (new_prec <= current.prec)
8835 if (sp == stack)
8836 break;
8837 else
8838 goto pop;
8841 get_rhs:
8842 current.tree_type = binops_by_token[token->type].tree_type;
8843 current.loc = token->location;
8845 /* We used the operator token. */
8846 cp_lexer_consume_token (parser->lexer);
8848 /* For "false && x" or "true || x", x will never be executed;
8849 disable warnings while evaluating it. */
8850 if (current.tree_type == TRUTH_ANDIF_EXPR)
8851 c_inhibit_evaluation_warnings +=
8852 cp_fully_fold (current.lhs) == truthvalue_false_node;
8853 else if (current.tree_type == TRUTH_ORIF_EXPR)
8854 c_inhibit_evaluation_warnings +=
8855 cp_fully_fold (current.lhs) == truthvalue_true_node;
8857 /* Extract another operand. It may be the RHS of this expression
8858 or the LHS of a new, higher priority expression. */
8859 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8860 ? TRUTH_NOT_EXPR : ERROR_MARK);
8861 rhs = cp_parser_simple_cast_expression (parser);
8863 /* Get another operator token. Look up its precedence to avoid
8864 building a useless (immediately popped) stack entry for common
8865 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8866 token = cp_lexer_peek_token (parser->lexer);
8867 lookahead_prec = TOKEN_PRECEDENCE (token);
8868 if (lookahead_prec != PREC_NOT_OPERATOR
8869 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8870 lookahead_prec = PREC_NOT_OPERATOR;
8871 if (lookahead_prec > new_prec)
8873 /* ... and prepare to parse the RHS of the new, higher priority
8874 expression. Since precedence levels on the stack are
8875 monotonically increasing, we do not have to care about
8876 stack overflows. */
8877 *sp = current;
8878 ++sp;
8879 current.lhs = rhs;
8880 current.lhs_type = rhs_type;
8881 current.prec = new_prec;
8882 new_prec = lookahead_prec;
8883 goto get_rhs;
8885 pop:
8886 lookahead_prec = new_prec;
8887 /* If the stack is not empty, we have parsed into LHS the right side
8888 (`4' in the example above) of an expression we had suspended.
8889 We can use the information on the stack to recover the LHS (`3')
8890 from the stack together with the tree code (`MULT_EXPR'), and
8891 the precedence of the higher level subexpression
8892 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8893 which will be used to actually build the additive expression. */
8894 rhs = current.lhs;
8895 rhs_type = current.lhs_type;
8896 --sp;
8897 current = *sp;
8900 /* Undo the disabling of warnings done above. */
8901 if (current.tree_type == TRUTH_ANDIF_EXPR)
8902 c_inhibit_evaluation_warnings -=
8903 cp_fully_fold (current.lhs) == truthvalue_false_node;
8904 else if (current.tree_type == TRUTH_ORIF_EXPR)
8905 c_inhibit_evaluation_warnings -=
8906 cp_fully_fold (current.lhs) == truthvalue_true_node;
8908 if (warn_logical_not_paren
8909 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
8910 && current.lhs_type == TRUTH_NOT_EXPR
8911 /* Avoid warning for !!x == y. */
8912 && (TREE_CODE (current.lhs) != NE_EXPR
8913 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
8914 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
8915 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
8916 /* Avoid warning for !b == y where b is boolean. */
8917 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
8918 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
8919 != BOOLEAN_TYPE))))
8920 /* Avoid warning for !!b == y where b is boolean. */
8921 && (!DECL_P (current.lhs)
8922 || TREE_TYPE (current.lhs) == NULL_TREE
8923 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
8924 warn_logical_not_parentheses (current.loc, current.tree_type,
8925 current.lhs, maybe_constant_value (rhs));
8927 overload = NULL;
8929 location_t combined_loc = make_location (current.loc,
8930 current.lhs.get_start (),
8931 rhs.get_finish ());
8933 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8934 ERROR_MARK for everything that is not a binary expression.
8935 This makes warn_about_parentheses miss some warnings that
8936 involve unary operators. For unary expressions we should
8937 pass the correct tree_code unless the unary expression was
8938 surrounded by parentheses.
8940 if (no_toplevel_fold_p
8941 && lookahead_prec <= current.prec
8942 && sp == stack)
8943 current.lhs = build2_loc (combined_loc,
8944 current.tree_type,
8945 TREE_CODE_CLASS (current.tree_type)
8946 == tcc_comparison
8947 ? boolean_type_node : TREE_TYPE (current.lhs),
8948 current.lhs, rhs);
8949 else
8951 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
8952 current.lhs, current.lhs_type,
8953 rhs, rhs_type, &overload,
8954 complain_flags (decltype_p));
8955 /* TODO: build_x_binary_op doesn't always honor the location. */
8956 current.lhs.set_location (combined_loc);
8958 current.lhs_type = current.tree_type;
8960 /* If the binary operator required the use of an overloaded operator,
8961 then this expression cannot be an integral constant-expression.
8962 An overloaded operator can be used even if both operands are
8963 otherwise permissible in an integral constant-expression if at
8964 least one of the operands is of enumeration type. */
8966 if (overload
8967 && cp_parser_non_integral_constant_expression (parser,
8968 NIC_OVERLOADED))
8969 return error_mark_node;
8972 return current.lhs;
8975 static cp_expr
8976 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8977 bool no_toplevel_fold_p,
8978 enum cp_parser_prec prec,
8979 cp_id_kind * pidk)
8981 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8982 /*decltype*/false, prec, pidk);
8985 /* Parse the `? expression : assignment-expression' part of a
8986 conditional-expression. The LOGICAL_OR_EXPR is the
8987 logical-or-expression that started the conditional-expression.
8988 Returns a representation of the entire conditional-expression.
8990 This routine is used by cp_parser_assignment_expression.
8992 ? expression : assignment-expression
8994 GNU Extensions:
8996 ? : assignment-expression */
8998 static tree
8999 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9001 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9002 cp_expr assignment_expr;
9003 struct cp_token *token;
9004 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9006 /* Consume the `?' token. */
9007 cp_lexer_consume_token (parser->lexer);
9008 token = cp_lexer_peek_token (parser->lexer);
9009 if (cp_parser_allow_gnu_extensions_p (parser)
9010 && token->type == CPP_COLON)
9012 pedwarn (token->location, OPT_Wpedantic,
9013 "ISO C++ does not allow ?: with omitted middle operand");
9014 /* Implicit true clause. */
9015 expr = NULL_TREE;
9016 c_inhibit_evaluation_warnings +=
9017 folded_logical_or_expr == truthvalue_true_node;
9018 warn_for_omitted_condop (token->location, logical_or_expr);
9020 else
9022 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9023 parser->colon_corrects_to_scope_p = false;
9024 /* Parse the expression. */
9025 c_inhibit_evaluation_warnings +=
9026 folded_logical_or_expr == truthvalue_false_node;
9027 expr = cp_parser_expression (parser);
9028 c_inhibit_evaluation_warnings +=
9029 ((folded_logical_or_expr == truthvalue_true_node)
9030 - (folded_logical_or_expr == truthvalue_false_node));
9031 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9034 /* The next token should be a `:'. */
9035 cp_parser_require (parser, CPP_COLON, RT_COLON);
9036 /* Parse the assignment-expression. */
9037 assignment_expr = cp_parser_assignment_expression (parser);
9038 c_inhibit_evaluation_warnings -=
9039 folded_logical_or_expr == truthvalue_true_node;
9041 /* Make a location:
9042 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9043 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9044 with the caret at the "?", ranging from the start of
9045 the logical_or_expr to the end of the assignment_expr. */
9046 loc = make_location (loc,
9047 logical_or_expr.get_start (),
9048 assignment_expr.get_finish ());
9050 /* Build the conditional-expression. */
9051 return build_x_conditional_expr (loc, logical_or_expr,
9052 expr,
9053 assignment_expr,
9054 tf_warning_or_error);
9057 /* Parse an assignment-expression.
9059 assignment-expression:
9060 conditional-expression
9061 logical-or-expression assignment-operator assignment_expression
9062 throw-expression
9064 CAST_P is true if this expression is the target of a cast.
9065 DECLTYPE_P is true if this expression is the operand of decltype.
9067 Returns a representation for the expression. */
9069 static cp_expr
9070 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9071 bool cast_p, bool decltype_p)
9073 cp_expr expr;
9075 /* If the next token is the `throw' keyword, then we're looking at
9076 a throw-expression. */
9077 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9078 expr = cp_parser_throw_expression (parser);
9079 /* Otherwise, it must be that we are looking at a
9080 logical-or-expression. */
9081 else
9083 /* Parse the binary expressions (logical-or-expression). */
9084 expr = cp_parser_binary_expression (parser, cast_p, false,
9085 decltype_p,
9086 PREC_NOT_OPERATOR, pidk);
9087 /* If the next token is a `?' then we're actually looking at a
9088 conditional-expression. */
9089 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9090 return cp_parser_question_colon_clause (parser, expr);
9091 else
9093 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9095 /* If it's an assignment-operator, we're using the second
9096 production. */
9097 enum tree_code assignment_operator
9098 = cp_parser_assignment_operator_opt (parser);
9099 if (assignment_operator != ERROR_MARK)
9101 bool non_constant_p;
9103 /* Parse the right-hand side of the assignment. */
9104 cp_expr rhs = cp_parser_initializer_clause (parser,
9105 &non_constant_p);
9107 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9108 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9110 /* An assignment may not appear in a
9111 constant-expression. */
9112 if (cp_parser_non_integral_constant_expression (parser,
9113 NIC_ASSIGNMENT))
9114 return error_mark_node;
9115 /* Build the assignment expression. Its default
9116 location:
9117 LHS = RHS
9118 ~~~~^~~~~
9119 is the location of the '=' token as the
9120 caret, ranging from the start of the lhs to the
9121 end of the rhs. */
9122 loc = make_location (loc,
9123 expr.get_start (),
9124 rhs.get_finish ());
9125 expr = build_x_modify_expr (loc, expr,
9126 assignment_operator,
9127 rhs,
9128 complain_flags (decltype_p));
9129 /* TODO: build_x_modify_expr doesn't honor the location,
9130 so we must set it here. */
9131 expr.set_location (loc);
9136 return expr;
9139 /* Parse an (optional) assignment-operator.
9141 assignment-operator: one of
9142 = *= /= %= += -= >>= <<= &= ^= |=
9144 GNU Extension:
9146 assignment-operator: one of
9147 <?= >?=
9149 If the next token is an assignment operator, the corresponding tree
9150 code is returned, and the token is consumed. For example, for
9151 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9152 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9153 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9154 operator, ERROR_MARK is returned. */
9156 static enum tree_code
9157 cp_parser_assignment_operator_opt (cp_parser* parser)
9159 enum tree_code op;
9160 cp_token *token;
9162 /* Peek at the next token. */
9163 token = cp_lexer_peek_token (parser->lexer);
9165 switch (token->type)
9167 case CPP_EQ:
9168 op = NOP_EXPR;
9169 break;
9171 case CPP_MULT_EQ:
9172 op = MULT_EXPR;
9173 break;
9175 case CPP_DIV_EQ:
9176 op = TRUNC_DIV_EXPR;
9177 break;
9179 case CPP_MOD_EQ:
9180 op = TRUNC_MOD_EXPR;
9181 break;
9183 case CPP_PLUS_EQ:
9184 op = PLUS_EXPR;
9185 break;
9187 case CPP_MINUS_EQ:
9188 op = MINUS_EXPR;
9189 break;
9191 case CPP_RSHIFT_EQ:
9192 op = RSHIFT_EXPR;
9193 break;
9195 case CPP_LSHIFT_EQ:
9196 op = LSHIFT_EXPR;
9197 break;
9199 case CPP_AND_EQ:
9200 op = BIT_AND_EXPR;
9201 break;
9203 case CPP_XOR_EQ:
9204 op = BIT_XOR_EXPR;
9205 break;
9207 case CPP_OR_EQ:
9208 op = BIT_IOR_EXPR;
9209 break;
9211 default:
9212 /* Nothing else is an assignment operator. */
9213 op = ERROR_MARK;
9216 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9217 if (op != ERROR_MARK
9218 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9219 op = ERROR_MARK;
9221 /* If it was an assignment operator, consume it. */
9222 if (op != ERROR_MARK)
9223 cp_lexer_consume_token (parser->lexer);
9225 return op;
9228 /* Parse an expression.
9230 expression:
9231 assignment-expression
9232 expression , assignment-expression
9234 CAST_P is true if this expression is the target of a cast.
9235 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9236 except possibly parenthesized or on the RHS of a comma (N3276).
9238 Returns a representation of the expression. */
9240 static cp_expr
9241 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9242 bool cast_p, bool decltype_p)
9244 cp_expr expression = NULL_TREE;
9245 location_t loc = UNKNOWN_LOCATION;
9247 while (true)
9249 cp_expr assignment_expression;
9251 /* Parse the next assignment-expression. */
9252 assignment_expression
9253 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9255 /* We don't create a temporary for a call that is the immediate operand
9256 of decltype or on the RHS of a comma. But when we see a comma, we
9257 need to create a temporary for a call on the LHS. */
9258 if (decltype_p && !processing_template_decl
9259 && TREE_CODE (assignment_expression) == CALL_EXPR
9260 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9261 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9262 assignment_expression
9263 = build_cplus_new (TREE_TYPE (assignment_expression),
9264 assignment_expression, tf_warning_or_error);
9266 /* If this is the first assignment-expression, we can just
9267 save it away. */
9268 if (!expression)
9269 expression = assignment_expression;
9270 else
9272 /* Create a location with caret at the comma, ranging
9273 from the start of the LHS to the end of the RHS. */
9274 loc = make_location (loc,
9275 expression.get_start (),
9276 assignment_expression.get_finish ());
9277 expression = build_x_compound_expr (loc, expression,
9278 assignment_expression,
9279 complain_flags (decltype_p));
9280 expression.set_location (loc);
9282 /* If the next token is not a comma, or we're in a fold-expression, then
9283 we are done with the expression. */
9284 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9285 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9286 break;
9287 /* Consume the `,'. */
9288 loc = cp_lexer_peek_token (parser->lexer)->location;
9289 cp_lexer_consume_token (parser->lexer);
9290 /* A comma operator cannot appear in a constant-expression. */
9291 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9292 expression = error_mark_node;
9295 return expression;
9298 /* Parse a constant-expression.
9300 constant-expression:
9301 conditional-expression
9303 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9304 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9305 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9306 is false, NON_CONSTANT_P should be NULL. */
9308 static cp_expr
9309 cp_parser_constant_expression (cp_parser* parser,
9310 bool allow_non_constant_p,
9311 bool *non_constant_p)
9313 bool saved_integral_constant_expression_p;
9314 bool saved_allow_non_integral_constant_expression_p;
9315 bool saved_non_integral_constant_expression_p;
9316 cp_expr expression;
9318 /* It might seem that we could simply parse the
9319 conditional-expression, and then check to see if it were
9320 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9321 one that the compiler can figure out is constant, possibly after
9322 doing some simplifications or optimizations. The standard has a
9323 precise definition of constant-expression, and we must honor
9324 that, even though it is somewhat more restrictive.
9326 For example:
9328 int i[(2, 3)];
9330 is not a legal declaration, because `(2, 3)' is not a
9331 constant-expression. The `,' operator is forbidden in a
9332 constant-expression. However, GCC's constant-folding machinery
9333 will fold this operation to an INTEGER_CST for `3'. */
9335 /* Save the old settings. */
9336 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9337 saved_allow_non_integral_constant_expression_p
9338 = parser->allow_non_integral_constant_expression_p;
9339 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9340 /* We are now parsing a constant-expression. */
9341 parser->integral_constant_expression_p = true;
9342 parser->allow_non_integral_constant_expression_p
9343 = (allow_non_constant_p || cxx_dialect >= cxx11);
9344 parser->non_integral_constant_expression_p = false;
9345 /* Although the grammar says "conditional-expression", we parse an
9346 "assignment-expression", which also permits "throw-expression"
9347 and the use of assignment operators. In the case that
9348 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9349 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9350 actually essential that we look for an assignment-expression.
9351 For example, cp_parser_initializer_clauses uses this function to
9352 determine whether a particular assignment-expression is in fact
9353 constant. */
9354 expression = cp_parser_assignment_expression (parser);
9355 /* Restore the old settings. */
9356 parser->integral_constant_expression_p
9357 = saved_integral_constant_expression_p;
9358 parser->allow_non_integral_constant_expression_p
9359 = saved_allow_non_integral_constant_expression_p;
9360 if (cxx_dialect >= cxx11)
9362 /* Require an rvalue constant expression here; that's what our
9363 callers expect. Reference constant expressions are handled
9364 separately in e.g. cp_parser_template_argument. */
9365 bool is_const = potential_rvalue_constant_expression (expression);
9366 parser->non_integral_constant_expression_p = !is_const;
9367 if (!is_const && !allow_non_constant_p)
9368 require_potential_rvalue_constant_expression (expression);
9370 if (allow_non_constant_p)
9371 *non_constant_p = parser->non_integral_constant_expression_p;
9372 parser->non_integral_constant_expression_p
9373 = saved_non_integral_constant_expression_p;
9375 return expression;
9378 /* Parse __builtin_offsetof.
9380 offsetof-expression:
9381 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9383 offsetof-member-designator:
9384 id-expression
9385 | offsetof-member-designator "." id-expression
9386 | offsetof-member-designator "[" expression "]"
9387 | offsetof-member-designator "->" id-expression */
9389 static cp_expr
9390 cp_parser_builtin_offsetof (cp_parser *parser)
9392 int save_ice_p, save_non_ice_p;
9393 tree type;
9394 cp_expr expr;
9395 cp_id_kind dummy;
9396 cp_token *token;
9397 location_t finish_loc;
9399 /* We're about to accept non-integral-constant things, but will
9400 definitely yield an integral constant expression. Save and
9401 restore these values around our local parsing. */
9402 save_ice_p = parser->integral_constant_expression_p;
9403 save_non_ice_p = parser->non_integral_constant_expression_p;
9405 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9407 /* Consume the "__builtin_offsetof" token. */
9408 cp_lexer_consume_token (parser->lexer);
9409 /* Consume the opening `('. */
9410 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9411 /* Parse the type-id. */
9412 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9413 type = cp_parser_type_id (parser);
9414 /* Look for the `,'. */
9415 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9416 token = cp_lexer_peek_token (parser->lexer);
9418 /* Build the (type *)null that begins the traditional offsetof macro. */
9419 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
9420 tf_warning_or_error);
9422 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9423 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
9424 true, &dummy, token->location);
9425 while (true)
9427 token = cp_lexer_peek_token (parser->lexer);
9428 switch (token->type)
9430 case CPP_OPEN_SQUARE:
9431 /* offsetof-member-designator "[" expression "]" */
9432 expr = cp_parser_postfix_open_square_expression (parser, expr,
9433 true, false);
9434 break;
9436 case CPP_DEREF:
9437 /* offsetof-member-designator "->" identifier */
9438 expr = grok_array_decl (token->location, expr,
9439 integer_zero_node, false);
9440 /* FALLTHRU */
9442 case CPP_DOT:
9443 /* offsetof-member-designator "." identifier */
9444 cp_lexer_consume_token (parser->lexer);
9445 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9446 expr, true, &dummy,
9447 token->location);
9448 break;
9450 case CPP_CLOSE_PAREN:
9451 /* Consume the ")" token. */
9452 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9453 cp_lexer_consume_token (parser->lexer);
9454 goto success;
9456 default:
9457 /* Error. We know the following require will fail, but
9458 that gives the proper error message. */
9459 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9460 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9461 expr = error_mark_node;
9462 goto failure;
9466 success:
9467 /* Make a location of the form:
9468 __builtin_offsetof (struct s, f)
9469 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9470 with caret at the type-id, ranging from the start of the
9471 "_builtin_offsetof" token to the close paren. */
9472 loc = make_location (loc, start_loc, finish_loc);
9473 /* The result will be an INTEGER_CST, so we need to explicitly
9474 preserve the location. */
9475 expr = cp_expr (finish_offsetof (expr, loc), loc);
9477 failure:
9478 parser->integral_constant_expression_p = save_ice_p;
9479 parser->non_integral_constant_expression_p = save_non_ice_p;
9481 return expr;
9484 /* Parse a trait expression.
9486 Returns a representation of the expression, the underlying type
9487 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9489 static tree
9490 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9492 cp_trait_kind kind;
9493 tree type1, type2 = NULL_TREE;
9494 bool binary = false;
9495 bool variadic = false;
9497 switch (keyword)
9499 case RID_HAS_NOTHROW_ASSIGN:
9500 kind = CPTK_HAS_NOTHROW_ASSIGN;
9501 break;
9502 case RID_HAS_NOTHROW_CONSTRUCTOR:
9503 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9504 break;
9505 case RID_HAS_NOTHROW_COPY:
9506 kind = CPTK_HAS_NOTHROW_COPY;
9507 break;
9508 case RID_HAS_TRIVIAL_ASSIGN:
9509 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9510 break;
9511 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9512 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9513 break;
9514 case RID_HAS_TRIVIAL_COPY:
9515 kind = CPTK_HAS_TRIVIAL_COPY;
9516 break;
9517 case RID_HAS_TRIVIAL_DESTRUCTOR:
9518 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9519 break;
9520 case RID_HAS_VIRTUAL_DESTRUCTOR:
9521 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9522 break;
9523 case RID_IS_ABSTRACT:
9524 kind = CPTK_IS_ABSTRACT;
9525 break;
9526 case RID_IS_BASE_OF:
9527 kind = CPTK_IS_BASE_OF;
9528 binary = true;
9529 break;
9530 case RID_IS_CLASS:
9531 kind = CPTK_IS_CLASS;
9532 break;
9533 case RID_IS_EMPTY:
9534 kind = CPTK_IS_EMPTY;
9535 break;
9536 case RID_IS_ENUM:
9537 kind = CPTK_IS_ENUM;
9538 break;
9539 case RID_IS_FINAL:
9540 kind = CPTK_IS_FINAL;
9541 break;
9542 case RID_IS_LITERAL_TYPE:
9543 kind = CPTK_IS_LITERAL_TYPE;
9544 break;
9545 case RID_IS_POD:
9546 kind = CPTK_IS_POD;
9547 break;
9548 case RID_IS_POLYMORPHIC:
9549 kind = CPTK_IS_POLYMORPHIC;
9550 break;
9551 case RID_IS_SAME_AS:
9552 kind = CPTK_IS_SAME_AS;
9553 binary = true;
9554 break;
9555 case RID_IS_STD_LAYOUT:
9556 kind = CPTK_IS_STD_LAYOUT;
9557 break;
9558 case RID_IS_TRIVIAL:
9559 kind = CPTK_IS_TRIVIAL;
9560 break;
9561 case RID_IS_TRIVIALLY_ASSIGNABLE:
9562 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9563 binary = true;
9564 break;
9565 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9566 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9567 variadic = true;
9568 break;
9569 case RID_IS_TRIVIALLY_COPYABLE:
9570 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9571 break;
9572 case RID_IS_UNION:
9573 kind = CPTK_IS_UNION;
9574 break;
9575 case RID_UNDERLYING_TYPE:
9576 kind = CPTK_UNDERLYING_TYPE;
9577 break;
9578 case RID_BASES:
9579 kind = CPTK_BASES;
9580 break;
9581 case RID_DIRECT_BASES:
9582 kind = CPTK_DIRECT_BASES;
9583 break;
9584 default:
9585 gcc_unreachable ();
9588 /* Consume the token. */
9589 cp_lexer_consume_token (parser->lexer);
9591 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9594 type_id_in_expr_sentinel s (parser);
9595 type1 = cp_parser_type_id (parser);
9598 if (type1 == error_mark_node)
9599 return error_mark_node;
9601 if (binary)
9603 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9606 type_id_in_expr_sentinel s (parser);
9607 type2 = cp_parser_type_id (parser);
9610 if (type2 == error_mark_node)
9611 return error_mark_node;
9613 else if (variadic)
9615 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9617 cp_lexer_consume_token (parser->lexer);
9618 tree elt = cp_parser_type_id (parser);
9619 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9621 cp_lexer_consume_token (parser->lexer);
9622 elt = make_pack_expansion (elt);
9624 if (elt == error_mark_node)
9625 return error_mark_node;
9626 type2 = tree_cons (NULL_TREE, elt, type2);
9630 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9632 /* Complete the trait expression, which may mean either processing
9633 the trait expr now or saving it for template instantiation. */
9634 switch(kind)
9636 case CPTK_UNDERLYING_TYPE:
9637 return finish_underlying_type (type1);
9638 case CPTK_BASES:
9639 return finish_bases (type1, false);
9640 case CPTK_DIRECT_BASES:
9641 return finish_bases (type1, true);
9642 default:
9643 return finish_trait_expr (kind, type1, type2);
9647 /* Lambdas that appear in variable initializer or default argument scope
9648 get that in their mangling, so we need to record it. We might as well
9649 use the count for function and namespace scopes as well. */
9650 static GTY(()) tree lambda_scope;
9651 static GTY(()) int lambda_count;
9652 struct GTY(()) tree_int
9654 tree t;
9655 int i;
9657 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
9659 static void
9660 start_lambda_scope (tree decl)
9662 tree_int ti;
9663 gcc_assert (decl);
9664 /* Once we're inside a function, we ignore other scopes and just push
9665 the function again so that popping works properly. */
9666 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
9667 decl = current_function_decl;
9668 ti.t = lambda_scope;
9669 ti.i = lambda_count;
9670 vec_safe_push (lambda_scope_stack, ti);
9671 if (lambda_scope != decl)
9673 /* Don't reset the count if we're still in the same function. */
9674 lambda_scope = decl;
9675 lambda_count = 0;
9679 static void
9680 record_lambda_scope (tree lambda)
9682 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
9683 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
9686 static void
9687 finish_lambda_scope (void)
9689 tree_int *p = &lambda_scope_stack->last ();
9690 if (lambda_scope != p->t)
9692 lambda_scope = p->t;
9693 lambda_count = p->i;
9695 lambda_scope_stack->pop ();
9698 /* Parse a lambda expression.
9700 lambda-expression:
9701 lambda-introducer lambda-declarator [opt] compound-statement
9703 Returns a representation of the expression. */
9705 static cp_expr
9706 cp_parser_lambda_expression (cp_parser* parser)
9708 tree lambda_expr = build_lambda_expr ();
9709 tree type;
9710 bool ok = true;
9711 cp_token *token = cp_lexer_peek_token (parser->lexer);
9712 cp_token_position start = 0;
9714 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9716 if (cp_unevaluated_operand)
9718 if (!token->error_reported)
9720 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9721 "lambda-expression in unevaluated context");
9722 token->error_reported = true;
9724 ok = false;
9726 else if (parser->in_template_argument_list_p)
9728 if (!token->error_reported)
9730 error_at (token->location, "lambda-expression in template-argument");
9731 token->error_reported = true;
9733 ok = false;
9736 /* We may be in the middle of deferred access check. Disable
9737 it now. */
9738 push_deferring_access_checks (dk_no_deferred);
9740 cp_parser_lambda_introducer (parser, lambda_expr);
9742 type = begin_lambda_type (lambda_expr);
9743 if (type == error_mark_node)
9744 return error_mark_node;
9746 record_lambda_scope (lambda_expr);
9748 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9749 determine_visibility (TYPE_NAME (type));
9751 /* Now that we've started the type, add the capture fields for any
9752 explicit captures. */
9753 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9756 /* Inside the class, surrounding template-parameter-lists do not apply. */
9757 unsigned int saved_num_template_parameter_lists
9758 = parser->num_template_parameter_lists;
9759 unsigned char in_statement = parser->in_statement;
9760 bool in_switch_statement_p = parser->in_switch_statement_p;
9761 bool fully_implicit_function_template_p
9762 = parser->fully_implicit_function_template_p;
9763 tree implicit_template_parms = parser->implicit_template_parms;
9764 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9765 bool auto_is_implicit_function_template_parm_p
9766 = parser->auto_is_implicit_function_template_parm_p;
9768 parser->num_template_parameter_lists = 0;
9769 parser->in_statement = 0;
9770 parser->in_switch_statement_p = false;
9771 parser->fully_implicit_function_template_p = false;
9772 parser->implicit_template_parms = 0;
9773 parser->implicit_template_scope = 0;
9774 parser->auto_is_implicit_function_template_parm_p = false;
9776 /* By virtue of defining a local class, a lambda expression has access to
9777 the private variables of enclosing classes. */
9779 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9781 if (ok && cp_parser_error_occurred (parser))
9782 ok = false;
9784 if (ok)
9786 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9787 && cp_parser_start_tentative_firewall (parser))
9788 start = token;
9789 cp_parser_lambda_body (parser, lambda_expr);
9791 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9793 if (cp_parser_skip_to_closing_brace (parser))
9794 cp_lexer_consume_token (parser->lexer);
9797 /* The capture list was built up in reverse order; fix that now. */
9798 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9799 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9801 if (ok)
9802 maybe_add_lambda_conv_op (type);
9804 type = finish_struct (type, /*attributes=*/NULL_TREE);
9806 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9807 parser->in_statement = in_statement;
9808 parser->in_switch_statement_p = in_switch_statement_p;
9809 parser->fully_implicit_function_template_p
9810 = fully_implicit_function_template_p;
9811 parser->implicit_template_parms = implicit_template_parms;
9812 parser->implicit_template_scope = implicit_template_scope;
9813 parser->auto_is_implicit_function_template_parm_p
9814 = auto_is_implicit_function_template_parm_p;
9817 /* This field is only used during parsing of the lambda. */
9818 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9820 /* This lambda shouldn't have any proxies left at this point. */
9821 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9822 /* And now that we're done, push proxies for an enclosing lambda. */
9823 insert_pending_capture_proxies ();
9825 if (ok)
9826 lambda_expr = build_lambda_object (lambda_expr);
9827 else
9828 lambda_expr = error_mark_node;
9830 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9832 pop_deferring_access_checks ();
9834 return lambda_expr;
9837 /* Parse the beginning of a lambda expression.
9839 lambda-introducer:
9840 [ lambda-capture [opt] ]
9842 LAMBDA_EXPR is the current representation of the lambda expression. */
9844 static void
9845 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9847 /* Need commas after the first capture. */
9848 bool first = true;
9850 /* Eat the leading `['. */
9851 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9853 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9854 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9855 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9856 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9857 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9858 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9860 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9862 cp_lexer_consume_token (parser->lexer);
9863 first = false;
9866 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9868 cp_token* capture_token;
9869 tree capture_id;
9870 tree capture_init_expr;
9871 cp_id_kind idk = CP_ID_KIND_NONE;
9872 bool explicit_init_p = false;
9874 enum capture_kind_type
9876 BY_COPY,
9877 BY_REFERENCE
9879 enum capture_kind_type capture_kind = BY_COPY;
9881 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9883 error ("expected end of capture-list");
9884 return;
9887 if (first)
9888 first = false;
9889 else
9890 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9892 /* Possibly capture `this'. */
9893 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9895 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9896 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9897 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9898 "with by-copy capture default");
9899 cp_lexer_consume_token (parser->lexer);
9900 add_capture (lambda_expr,
9901 /*id=*/this_identifier,
9902 /*initializer=*/finish_this_expr (),
9903 /*by_reference_p=*/true,
9904 explicit_init_p);
9905 continue;
9908 /* Possibly capture `*this'. */
9909 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
9910 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
9912 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9913 if (cxx_dialect < cxx1z)
9914 pedwarn (loc, 0, "%<*this%> capture only available with "
9915 "-std=c++1z or -std=gnu++1z");
9916 cp_lexer_consume_token (parser->lexer);
9917 cp_lexer_consume_token (parser->lexer);
9918 add_capture (lambda_expr,
9919 /*id=*/this_identifier,
9920 /*initializer=*/finish_this_expr (),
9921 /*by_reference_p=*/false,
9922 explicit_init_p);
9923 continue;
9926 /* Remember whether we want to capture as a reference or not. */
9927 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9929 capture_kind = BY_REFERENCE;
9930 cp_lexer_consume_token (parser->lexer);
9933 /* Get the identifier. */
9934 capture_token = cp_lexer_peek_token (parser->lexer);
9935 capture_id = cp_parser_identifier (parser);
9937 if (capture_id == error_mark_node)
9938 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9939 delimiters, but I modified this to stop on unnested ']' as well. It
9940 was already changed to stop on unnested '}', so the
9941 "closing_parenthesis" name is no more misleading with my change. */
9943 cp_parser_skip_to_closing_parenthesis (parser,
9944 /*recovering=*/true,
9945 /*or_comma=*/true,
9946 /*consume_paren=*/true);
9947 break;
9950 /* Find the initializer for this capture. */
9951 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9952 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9953 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9955 bool direct, non_constant;
9956 /* An explicit initializer exists. */
9957 if (cxx_dialect < cxx14)
9958 pedwarn (input_location, 0,
9959 "lambda capture initializers "
9960 "only available with -std=c++14 or -std=gnu++14");
9961 capture_init_expr = cp_parser_initializer (parser, &direct,
9962 &non_constant);
9963 explicit_init_p = true;
9964 if (capture_init_expr == NULL_TREE)
9966 error ("empty initializer for lambda init-capture");
9967 capture_init_expr = error_mark_node;
9970 else
9972 const char* error_msg;
9974 /* Turn the identifier into an id-expression. */
9975 capture_init_expr
9976 = cp_parser_lookup_name_simple (parser, capture_id,
9977 capture_token->location);
9979 if (capture_init_expr == error_mark_node)
9981 unqualified_name_lookup_error (capture_id);
9982 continue;
9984 else if (DECL_P (capture_init_expr)
9985 && (!VAR_P (capture_init_expr)
9986 && TREE_CODE (capture_init_expr) != PARM_DECL))
9988 error_at (capture_token->location,
9989 "capture of non-variable %qD ",
9990 capture_init_expr);
9991 inform (DECL_SOURCE_LOCATION (capture_init_expr),
9992 "%q#D declared here", capture_init_expr);
9993 continue;
9995 if (VAR_P (capture_init_expr)
9996 && decl_storage_duration (capture_init_expr) != dk_auto)
9998 if (pedwarn (capture_token->location, 0, "capture of variable "
9999 "%qD with non-automatic storage duration",
10000 capture_init_expr))
10001 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10002 "%q#D declared here", capture_init_expr);
10003 continue;
10006 capture_init_expr
10007 = finish_id_expression
10008 (capture_id,
10009 capture_init_expr,
10010 parser->scope,
10011 &idk,
10012 /*integral_constant_expression_p=*/false,
10013 /*allow_non_integral_constant_expression_p=*/false,
10014 /*non_integral_constant_expression_p=*/NULL,
10015 /*template_p=*/false,
10016 /*done=*/true,
10017 /*address_p=*/false,
10018 /*template_arg_p=*/false,
10019 &error_msg,
10020 capture_token->location);
10022 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10024 cp_lexer_consume_token (parser->lexer);
10025 capture_init_expr = make_pack_expansion (capture_init_expr);
10027 else
10028 check_for_bare_parameter_packs (capture_init_expr);
10031 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10032 && !explicit_init_p)
10034 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10035 && capture_kind == BY_COPY)
10036 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10037 "of %qD redundant with by-copy capture default",
10038 capture_id);
10039 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10040 && capture_kind == BY_REFERENCE)
10041 pedwarn (capture_token->location, 0, "explicit by-reference "
10042 "capture of %qD redundant with by-reference capture "
10043 "default", capture_id);
10046 add_capture (lambda_expr,
10047 capture_id,
10048 capture_init_expr,
10049 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10050 explicit_init_p);
10053 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10056 /* Parse the (optional) middle of a lambda expression.
10058 lambda-declarator:
10059 < template-parameter-list [opt] >
10060 ( parameter-declaration-clause [opt] )
10061 attribute-specifier [opt]
10062 decl-specifier-seq [opt]
10063 exception-specification [opt]
10064 lambda-return-type-clause [opt]
10066 LAMBDA_EXPR is the current representation of the lambda expression. */
10068 static bool
10069 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10071 /* 5.1.1.4 of the standard says:
10072 If a lambda-expression does not include a lambda-declarator, it is as if
10073 the lambda-declarator were ().
10074 This means an empty parameter list, no attributes, and no exception
10075 specification. */
10076 tree param_list = void_list_node;
10077 tree attributes = NULL_TREE;
10078 tree exception_spec = NULL_TREE;
10079 tree template_param_list = NULL_TREE;
10080 tree tx_qual = NULL_TREE;
10081 cp_decl_specifier_seq lambda_specs;
10082 clear_decl_specs (&lambda_specs);
10084 /* The template-parameter-list is optional, but must begin with
10085 an opening angle if present. */
10086 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10088 if (cxx_dialect < cxx14)
10089 pedwarn (parser->lexer->next_token->location, 0,
10090 "lambda templates are only available with "
10091 "-std=c++14 or -std=gnu++14");
10093 cp_lexer_consume_token (parser->lexer);
10095 template_param_list = cp_parser_template_parameter_list (parser);
10097 cp_parser_skip_to_end_of_template_parameter_list (parser);
10099 /* We just processed one more parameter list. */
10100 ++parser->num_template_parameter_lists;
10103 /* The parameter-declaration-clause is optional (unless
10104 template-parameter-list was given), but must begin with an
10105 opening parenthesis if present. */
10106 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10108 cp_lexer_consume_token (parser->lexer);
10110 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10112 /* Parse parameters. */
10113 param_list = cp_parser_parameter_declaration_clause (parser);
10115 /* Default arguments shall not be specified in the
10116 parameter-declaration-clause of a lambda-declarator. */
10117 for (tree t = param_list; t; t = TREE_CHAIN (t))
10118 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
10119 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10120 "default argument specified for lambda parameter");
10122 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10124 attributes = cp_parser_attributes_opt (parser);
10126 /* In the decl-specifier-seq of the lambda-declarator, each
10127 decl-specifier shall either be mutable or constexpr. */
10128 int declares_class_or_enum;
10129 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10130 cp_parser_decl_specifier_seq (parser,
10131 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10132 &lambda_specs, &declares_class_or_enum);
10133 if (lambda_specs.storage_class == sc_mutable)
10135 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10136 if (lambda_specs.conflicting_specifiers_p)
10137 error_at (lambda_specs.locations[ds_storage_class],
10138 "duplicate %<mutable%>");
10141 tx_qual = cp_parser_tx_qualifier_opt (parser);
10143 /* Parse optional exception specification. */
10144 exception_spec = cp_parser_exception_specification_opt (parser);
10146 /* Parse optional trailing return type. */
10147 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10149 cp_lexer_consume_token (parser->lexer);
10150 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10151 = cp_parser_trailing_type_id (parser);
10154 /* The function parameters must be in scope all the way until after the
10155 trailing-return-type in case of decltype. */
10156 pop_bindings_and_leave_scope ();
10158 else if (template_param_list != NULL_TREE) // generate diagnostic
10159 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10161 /* Create the function call operator.
10163 Messing with declarators like this is no uglier than building up the
10164 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10165 other code. */
10167 cp_decl_specifier_seq return_type_specs;
10168 cp_declarator* declarator;
10169 tree fco;
10170 int quals;
10171 void *p;
10173 clear_decl_specs (&return_type_specs);
10174 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10175 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
10176 else
10177 /* Maybe we will deduce the return type later. */
10178 return_type_specs.type = make_auto ();
10180 if (lambda_specs.locations[ds_constexpr])
10182 if (cxx_dialect >= cxx1z)
10183 return_type_specs.locations[ds_constexpr]
10184 = lambda_specs.locations[ds_constexpr];
10185 else
10186 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10187 "lambda only available with -std=c++1z or -std=gnu++1z");
10190 p = obstack_alloc (&declarator_obstack, 0);
10192 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
10193 sfk_none);
10195 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10196 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10197 declarator = make_call_declarator (declarator, param_list, quals,
10198 VIRT_SPEC_UNSPECIFIED,
10199 REF_QUAL_NONE,
10200 tx_qual,
10201 exception_spec,
10202 /*late_return_type=*/NULL_TREE,
10203 /*requires_clause*/NULL_TREE);
10204 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10206 fco = grokmethod (&return_type_specs,
10207 declarator,
10208 attributes);
10209 if (fco != error_mark_node)
10211 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10212 DECL_ARTIFICIAL (fco) = 1;
10213 /* Give the object parameter a different name. */
10214 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10215 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10216 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10218 if (template_param_list)
10220 fco = finish_member_template_decl (fco);
10221 finish_template_decl (template_param_list);
10222 --parser->num_template_parameter_lists;
10224 else if (parser->fully_implicit_function_template_p)
10225 fco = finish_fully_implicit_template (parser, fco);
10227 finish_member_declaration (fco);
10229 obstack_free (&declarator_obstack, p);
10231 return (fco != error_mark_node);
10235 /* Parse the body of a lambda expression, which is simply
10237 compound-statement
10239 but which requires special handling.
10240 LAMBDA_EXPR is the current representation of the lambda expression. */
10242 static void
10243 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10245 bool nested = (current_function_decl != NULL_TREE);
10246 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10247 if (nested)
10248 push_function_context ();
10249 else
10250 /* Still increment function_depth so that we don't GC in the
10251 middle of an expression. */
10252 ++function_depth;
10253 vec<tree> omp_privatization_save;
10254 save_omp_privatization_clauses (omp_privatization_save);
10255 /* Clear this in case we're in the middle of a default argument. */
10256 parser->local_variables_forbidden_p = false;
10258 /* Finish the function call operator
10259 - class_specifier
10260 + late_parsing_for_member
10261 + function_definition_after_declarator
10262 + ctor_initializer_opt_and_function_body */
10264 tree fco = lambda_function (lambda_expr);
10265 tree body;
10266 bool done = false;
10267 tree compound_stmt;
10268 tree cap;
10270 /* Let the front end know that we are going to be defining this
10271 function. */
10272 start_preparsed_function (fco,
10273 NULL_TREE,
10274 SF_PRE_PARSED | SF_INCLASS_INLINE);
10276 start_lambda_scope (fco);
10277 body = begin_function_body ();
10279 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10280 goto out;
10282 /* Push the proxies for any explicit captures. */
10283 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
10284 cap = TREE_CHAIN (cap))
10285 build_capture_proxy (TREE_PURPOSE (cap));
10287 compound_stmt = begin_compound_stmt (0);
10289 /* 5.1.1.4 of the standard says:
10290 If a lambda-expression does not include a trailing-return-type, it
10291 is as if the trailing-return-type denotes the following type:
10292 * if the compound-statement is of the form
10293 { return attribute-specifier [opt] expression ; }
10294 the type of the returned expression after lvalue-to-rvalue
10295 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10296 (_conv.array_ 4.2), and function-to-pointer conversion
10297 (_conv.func_ 4.3);
10298 * otherwise, void. */
10300 /* In a lambda that has neither a lambda-return-type-clause
10301 nor a deducible form, errors should be reported for return statements
10302 in the body. Since we used void as the placeholder return type, parsing
10303 the body as usual will give such desired behavior. */
10304 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10305 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
10306 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
10308 tree expr = NULL_TREE;
10309 cp_id_kind idk = CP_ID_KIND_NONE;
10311 /* Parse tentatively in case there's more after the initial return
10312 statement. */
10313 cp_parser_parse_tentatively (parser);
10315 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
10317 expr = cp_parser_expression (parser, &idk);
10319 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10320 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10322 if (cp_parser_parse_definitely (parser))
10324 if (!processing_template_decl)
10326 tree type = lambda_return_type (expr);
10327 apply_deduced_return_type (fco, type);
10328 if (type == error_mark_node)
10329 expr = error_mark_node;
10332 /* Will get error here if type not deduced yet. */
10333 finish_return_stmt (expr);
10335 done = true;
10339 if (!done)
10341 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10342 cp_parser_label_declaration (parser);
10343 cp_parser_statement_seq_opt (parser, NULL_TREE);
10344 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10347 finish_compound_stmt (compound_stmt);
10349 out:
10350 finish_function_body (body);
10351 finish_lambda_scope ();
10353 /* Finish the function and generate code for it if necessary. */
10354 tree fn = finish_function (/*inline*/2);
10356 /* Only expand if the call op is not a template. */
10357 if (!DECL_TEMPLATE_INFO (fco))
10358 expand_or_defer_fn (fn);
10361 restore_omp_privatization_clauses (omp_privatization_save);
10362 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10363 if (nested)
10364 pop_function_context();
10365 else
10366 --function_depth;
10369 /* Statements [gram.stmt.stmt] */
10371 /* Parse a statement.
10373 statement:
10374 labeled-statement
10375 expression-statement
10376 compound-statement
10377 selection-statement
10378 iteration-statement
10379 jump-statement
10380 declaration-statement
10381 try-block
10383 C++11:
10385 statement:
10386 labeled-statement
10387 attribute-specifier-seq (opt) expression-statement
10388 attribute-specifier-seq (opt) compound-statement
10389 attribute-specifier-seq (opt) selection-statement
10390 attribute-specifier-seq (opt) iteration-statement
10391 attribute-specifier-seq (opt) jump-statement
10392 declaration-statement
10393 attribute-specifier-seq (opt) try-block
10395 TM Extension:
10397 statement:
10398 atomic-statement
10400 IN_COMPOUND is true when the statement is nested inside a
10401 cp_parser_compound_statement; this matters for certain pragmas.
10403 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10404 is a (possibly labeled) if statement which is not enclosed in braces
10405 and has an else clause. This is used to implement -Wparentheses.
10407 CHAIN is a vector of if-else-if conditions. */
10409 static void
10410 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10411 bool in_compound, bool *if_p, vec<tree> *chain)
10413 tree statement, std_attrs = NULL_TREE;
10414 cp_token *token;
10415 location_t statement_location, attrs_location;
10417 restart:
10418 if (if_p != NULL)
10419 *if_p = false;
10420 /* There is no statement yet. */
10421 statement = NULL_TREE;
10423 saved_token_sentinel saved_tokens (parser->lexer);
10424 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10425 if (c_dialect_objc ())
10426 /* In obj-c++, seeing '[[' might be the either the beginning of
10427 c++11 attributes, or a nested objc-message-expression. So
10428 let's parse the c++11 attributes tentatively. */
10429 cp_parser_parse_tentatively (parser);
10430 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10431 if (c_dialect_objc ())
10433 if (!cp_parser_parse_definitely (parser))
10434 std_attrs = NULL_TREE;
10437 /* Peek at the next token. */
10438 token = cp_lexer_peek_token (parser->lexer);
10439 /* Remember the location of the first token in the statement. */
10440 statement_location = token->location;
10441 /* If this is a keyword, then that will often determine what kind of
10442 statement we have. */
10443 if (token->type == CPP_KEYWORD)
10445 enum rid keyword = token->keyword;
10447 switch (keyword)
10449 case RID_CASE:
10450 case RID_DEFAULT:
10451 /* Looks like a labeled-statement with a case label.
10452 Parse the label, and then use tail recursion to parse
10453 the statement. */
10454 cp_parser_label_for_labeled_statement (parser, std_attrs);
10455 in_compound = false;
10456 goto restart;
10458 case RID_IF:
10459 case RID_SWITCH:
10460 statement = cp_parser_selection_statement (parser, if_p, chain);
10461 break;
10463 case RID_WHILE:
10464 case RID_DO:
10465 case RID_FOR:
10466 statement = cp_parser_iteration_statement (parser, if_p, false);
10467 break;
10469 case RID_CILK_FOR:
10470 if (!flag_cilkplus)
10472 error_at (cp_lexer_peek_token (parser->lexer)->location,
10473 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10474 cp_lexer_consume_token (parser->lexer);
10475 statement = error_mark_node;
10477 else
10478 statement = cp_parser_cilk_for (parser, integer_zero_node, if_p);
10479 break;
10481 case RID_BREAK:
10482 case RID_CONTINUE:
10483 case RID_RETURN:
10484 case RID_GOTO:
10485 statement = cp_parser_jump_statement (parser);
10486 break;
10488 case RID_CILK_SYNC:
10489 cp_lexer_consume_token (parser->lexer);
10490 if (flag_cilkplus)
10492 tree sync_expr = build_cilk_sync ();
10493 SET_EXPR_LOCATION (sync_expr,
10494 token->location);
10495 statement = finish_expr_stmt (sync_expr);
10497 else
10499 error_at (token->location, "-fcilkplus must be enabled to use"
10500 " %<_Cilk_sync%>");
10501 statement = error_mark_node;
10503 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10504 break;
10506 /* Objective-C++ exception-handling constructs. */
10507 case RID_AT_TRY:
10508 case RID_AT_CATCH:
10509 case RID_AT_FINALLY:
10510 case RID_AT_SYNCHRONIZED:
10511 case RID_AT_THROW:
10512 statement = cp_parser_objc_statement (parser);
10513 break;
10515 case RID_TRY:
10516 statement = cp_parser_try_block (parser);
10517 break;
10519 case RID_NAMESPACE:
10520 /* This must be a namespace alias definition. */
10521 cp_parser_declaration_statement (parser);
10522 return;
10524 case RID_TRANSACTION_ATOMIC:
10525 case RID_TRANSACTION_RELAXED:
10526 case RID_SYNCHRONIZED:
10527 case RID_ATOMIC_NOEXCEPT:
10528 case RID_ATOMIC_CANCEL:
10529 statement = cp_parser_transaction (parser, token);
10530 break;
10531 case RID_TRANSACTION_CANCEL:
10532 statement = cp_parser_transaction_cancel (parser);
10533 break;
10535 default:
10536 /* It might be a keyword like `int' that can start a
10537 declaration-statement. */
10538 break;
10541 else if (token->type == CPP_NAME)
10543 /* If the next token is a `:', then we are looking at a
10544 labeled-statement. */
10545 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10546 if (token->type == CPP_COLON)
10548 /* Looks like a labeled-statement with an ordinary label.
10549 Parse the label, and then use tail recursion to parse
10550 the statement. */
10552 cp_parser_label_for_labeled_statement (parser, std_attrs);
10553 in_compound = false;
10554 goto restart;
10557 /* Anything that starts with a `{' must be a compound-statement. */
10558 else if (token->type == CPP_OPEN_BRACE)
10559 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10560 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10561 a statement all its own. */
10562 else if (token->type == CPP_PRAGMA)
10564 /* Only certain OpenMP pragmas are attached to statements, and thus
10565 are considered statements themselves. All others are not. In
10566 the context of a compound, accept the pragma as a "statement" and
10567 return so that we can check for a close brace. Otherwise we
10568 require a real statement and must go back and read one. */
10569 if (in_compound)
10570 cp_parser_pragma (parser, pragma_compound, if_p);
10571 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10572 goto restart;
10573 return;
10575 else if (token->type == CPP_EOF)
10577 cp_parser_error (parser, "expected statement");
10578 return;
10581 /* Everything else must be a declaration-statement or an
10582 expression-statement. Try for the declaration-statement
10583 first, unless we are looking at a `;', in which case we know that
10584 we have an expression-statement. */
10585 if (!statement)
10587 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10589 if (std_attrs != NULL_TREE)
10591 /* Attributes should be parsed as part of the the
10592 declaration, so let's un-parse them. */
10593 saved_tokens.rollback();
10594 std_attrs = NULL_TREE;
10597 cp_parser_parse_tentatively (parser);
10598 /* Try to parse the declaration-statement. */
10599 cp_parser_declaration_statement (parser);
10600 /* If that worked, we're done. */
10601 if (cp_parser_parse_definitely (parser))
10602 return;
10604 /* Look for an expression-statement instead. */
10605 statement = cp_parser_expression_statement (parser, in_statement_expr);
10607 /* Handle [[fallthrough]];. */
10608 if (attribute_fallthrough_p (std_attrs))
10610 /* The next token after the fallthrough attribute is ';'. */
10611 if (statement == NULL_TREE)
10613 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10614 statement = build_call_expr_internal_loc (statement_location,
10615 IFN_FALLTHROUGH,
10616 void_type_node, 0);
10617 finish_expr_stmt (statement);
10619 else
10620 warning_at (statement_location, OPT_Wattributes,
10621 "%<fallthrough%> attribute not followed by %<;%>");
10622 std_attrs = NULL_TREE;
10626 /* Set the line number for the statement. */
10627 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10628 SET_EXPR_LOCATION (statement, statement_location);
10630 /* Allow "[[fallthrough]];", but warn otherwise. */
10631 if (std_attrs != NULL_TREE)
10632 warning_at (attrs_location,
10633 OPT_Wattributes,
10634 "attributes at the beginning of statement are ignored");
10637 /* Parse the label for a labeled-statement, i.e.
10639 identifier :
10640 case constant-expression :
10641 default :
10643 GNU Extension:
10644 case constant-expression ... constant-expression : statement
10646 When a label is parsed without errors, the label is added to the
10647 parse tree by the finish_* functions, so this function doesn't
10648 have to return the label. */
10650 static void
10651 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10653 cp_token *token;
10654 tree label = NULL_TREE;
10655 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10657 /* The next token should be an identifier. */
10658 token = cp_lexer_peek_token (parser->lexer);
10659 if (token->type != CPP_NAME
10660 && token->type != CPP_KEYWORD)
10662 cp_parser_error (parser, "expected labeled-statement");
10663 return;
10666 /* Remember whether this case or a user-defined label is allowed to fall
10667 through to. */
10668 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
10670 parser->colon_corrects_to_scope_p = false;
10671 switch (token->keyword)
10673 case RID_CASE:
10675 tree expr, expr_hi;
10676 cp_token *ellipsis;
10678 /* Consume the `case' token. */
10679 cp_lexer_consume_token (parser->lexer);
10680 /* Parse the constant-expression. */
10681 expr = cp_parser_constant_expression (parser);
10682 if (check_for_bare_parameter_packs (expr))
10683 expr = error_mark_node;
10685 ellipsis = cp_lexer_peek_token (parser->lexer);
10686 if (ellipsis->type == CPP_ELLIPSIS)
10688 /* Consume the `...' token. */
10689 cp_lexer_consume_token (parser->lexer);
10690 expr_hi = cp_parser_constant_expression (parser);
10691 if (check_for_bare_parameter_packs (expr_hi))
10692 expr_hi = error_mark_node;
10694 /* We don't need to emit warnings here, as the common code
10695 will do this for us. */
10697 else
10698 expr_hi = NULL_TREE;
10700 if (parser->in_switch_statement_p)
10702 tree l = finish_case_label (token->location, expr, expr_hi);
10703 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10704 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10706 else
10707 error_at (token->location,
10708 "case label %qE not within a switch statement",
10709 expr);
10711 break;
10713 case RID_DEFAULT:
10714 /* Consume the `default' token. */
10715 cp_lexer_consume_token (parser->lexer);
10717 if (parser->in_switch_statement_p)
10719 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
10720 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10721 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10723 else
10724 error_at (token->location, "case label not within a switch statement");
10725 break;
10727 default:
10728 /* Anything else must be an ordinary label. */
10729 label = finish_label_stmt (cp_parser_identifier (parser));
10730 if (label && TREE_CODE (label) == LABEL_DECL)
10731 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
10732 break;
10735 /* Require the `:' token. */
10736 cp_parser_require (parser, CPP_COLON, RT_COLON);
10738 /* An ordinary label may optionally be followed by attributes.
10739 However, this is only permitted if the attributes are then
10740 followed by a semicolon. This is because, for backward
10741 compatibility, when parsing
10742 lab: __attribute__ ((unused)) int i;
10743 we want the attribute to attach to "i", not "lab". */
10744 if (label != NULL_TREE
10745 && cp_next_tokens_can_be_gnu_attribute_p (parser))
10747 tree attrs;
10748 cp_parser_parse_tentatively (parser);
10749 attrs = cp_parser_gnu_attributes_opt (parser);
10750 if (attrs == NULL_TREE
10751 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10752 cp_parser_abort_tentative_parse (parser);
10753 else if (!cp_parser_parse_definitely (parser))
10755 else
10756 attributes = chainon (attributes, attrs);
10759 if (attributes != NULL_TREE)
10760 cplus_decl_attributes (&label, attributes, 0);
10762 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10765 /* Parse an expression-statement.
10767 expression-statement:
10768 expression [opt] ;
10770 Returns the new EXPR_STMT -- or NULL_TREE if the expression
10771 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
10772 indicates whether this expression-statement is part of an
10773 expression statement. */
10775 static tree
10776 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
10778 tree statement = NULL_TREE;
10779 cp_token *token = cp_lexer_peek_token (parser->lexer);
10780 location_t loc = token->location;
10782 /* There might be attribute fallthrough. */
10783 tree attr = cp_parser_gnu_attributes_opt (parser);
10785 /* If the next token is a ';', then there is no expression
10786 statement. */
10787 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10789 statement = cp_parser_expression (parser);
10790 if (statement == error_mark_node
10791 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10793 cp_parser_skip_to_end_of_block_or_statement (parser);
10794 return error_mark_node;
10798 /* Handle [[fallthrough]];. */
10799 if (attribute_fallthrough_p (attr))
10801 /* The next token after the fallthrough attribute is ';'. */
10802 if (statement == NULL_TREE)
10803 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10804 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
10805 void_type_node, 0);
10806 else
10807 warning_at (loc, OPT_Wattributes,
10808 "%<fallthrough%> attribute not followed by %<;%>");
10809 attr = NULL_TREE;
10812 /* Allow "[[fallthrough]];", but warn otherwise. */
10813 if (attr != NULL_TREE)
10814 warning_at (loc, OPT_Wattributes,
10815 "attributes at the beginning of statement are ignored");
10817 /* Give a helpful message for "A<T>::type t;" and the like. */
10818 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
10819 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10821 if (TREE_CODE (statement) == SCOPE_REF)
10822 error_at (token->location, "need %<typename%> before %qE because "
10823 "%qT is a dependent scope",
10824 statement, TREE_OPERAND (statement, 0));
10825 else if (is_overloaded_fn (statement)
10826 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
10828 /* A::A a; */
10829 tree fn = get_first_fn (statement);
10830 error_at (token->location,
10831 "%<%T::%D%> names the constructor, not the type",
10832 DECL_CONTEXT (fn), DECL_NAME (fn));
10836 /* Consume the final `;'. */
10837 cp_parser_consume_semicolon_at_end_of_statement (parser);
10839 if (in_statement_expr
10840 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10841 /* This is the final expression statement of a statement
10842 expression. */
10843 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10844 else if (statement)
10845 statement = finish_expr_stmt (statement);
10847 return statement;
10850 /* Parse a compound-statement.
10852 compound-statement:
10853 { statement-seq [opt] }
10855 GNU extension:
10857 compound-statement:
10858 { label-declaration-seq [opt] statement-seq [opt] }
10860 label-declaration-seq:
10861 label-declaration
10862 label-declaration-seq label-declaration
10864 Returns a tree representing the statement. */
10866 static tree
10867 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10868 int bcs_flags, bool function_body)
10870 tree compound_stmt;
10872 /* Consume the `{'. */
10873 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10874 return error_mark_node;
10875 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10876 && !function_body && cxx_dialect < cxx14)
10877 pedwarn (input_location, OPT_Wpedantic,
10878 "compound-statement in constexpr function");
10879 /* Begin the compound-statement. */
10880 compound_stmt = begin_compound_stmt (bcs_flags);
10881 /* If the next keyword is `__label__' we have a label declaration. */
10882 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10883 cp_parser_label_declaration (parser);
10884 /* Parse an (optional) statement-seq. */
10885 cp_parser_statement_seq_opt (parser, in_statement_expr);
10886 /* Finish the compound-statement. */
10887 finish_compound_stmt (compound_stmt);
10888 /* Consume the `}'. */
10889 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10891 return compound_stmt;
10894 /* Parse an (optional) statement-seq.
10896 statement-seq:
10897 statement
10898 statement-seq [opt] statement */
10900 static void
10901 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10903 /* Scan statements until there aren't any more. */
10904 while (true)
10906 cp_token *token = cp_lexer_peek_token (parser->lexer);
10908 /* If we are looking at a `}', then we have run out of
10909 statements; the same is true if we have reached the end
10910 of file, or have stumbled upon a stray '@end'. */
10911 if (token->type == CPP_CLOSE_BRACE
10912 || token->type == CPP_EOF
10913 || token->type == CPP_PRAGMA_EOL
10914 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10915 break;
10917 /* If we are in a compound statement and find 'else' then
10918 something went wrong. */
10919 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10921 if (parser->in_statement & IN_IF_STMT)
10922 break;
10923 else
10925 token = cp_lexer_consume_token (parser->lexer);
10926 error_at (token->location, "%<else%> without a previous %<if%>");
10930 /* Parse the statement. */
10931 cp_parser_statement (parser, in_statement_expr, true, NULL);
10935 /* Parse a selection-statement.
10937 selection-statement:
10938 if ( condition ) statement
10939 if ( condition ) statement else statement
10940 switch ( condition ) statement
10942 Returns the new IF_STMT or SWITCH_STMT.
10944 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10945 is a (possibly labeled) if statement which is not enclosed in
10946 braces and has an else clause. This is used to implement
10947 -Wparentheses.
10949 CHAIN is a vector of if-else-if conditions. This is used to implement
10950 -Wduplicated-cond. */
10952 static tree
10953 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
10954 vec<tree> *chain)
10956 cp_token *token;
10957 enum rid keyword;
10958 token_indent_info guard_tinfo;
10960 if (if_p != NULL)
10961 *if_p = false;
10963 /* Peek at the next token. */
10964 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10965 guard_tinfo = get_token_indent_info (token);
10967 /* See what kind of keyword it is. */
10968 keyword = token->keyword;
10969 switch (keyword)
10971 case RID_IF:
10972 case RID_SWITCH:
10974 tree statement;
10975 tree condition;
10977 bool cx = false;
10978 if (keyword == RID_IF
10979 && cp_lexer_next_token_is_keyword (parser->lexer,
10980 RID_CONSTEXPR))
10982 cx = true;
10983 cp_token *tok = cp_lexer_consume_token (parser->lexer);
10984 if (cxx_dialect < cxx1z && !in_system_header_at (tok->location))
10985 pedwarn (tok->location, 0, "%<if constexpr%> only available "
10986 "with -std=c++1z or -std=gnu++1z");
10989 /* Look for the `('. */
10990 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10992 cp_parser_skip_to_end_of_statement (parser);
10993 return error_mark_node;
10996 /* Begin the selection-statement. */
10997 if (keyword == RID_IF)
10999 statement = begin_if_stmt ();
11000 IF_STMT_CONSTEXPR_P (statement) = cx;
11002 else
11003 statement = begin_switch_stmt ();
11005 /* Parse the condition. */
11006 condition = cp_parser_condition (parser);
11007 /* Look for the `)'. */
11008 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11009 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11010 /*consume_paren=*/true);
11012 if (keyword == RID_IF)
11014 bool nested_if;
11015 unsigned char in_statement;
11017 /* Add the condition. */
11018 condition = finish_if_stmt_cond (condition, statement);
11020 if (warn_duplicated_cond)
11021 warn_duplicated_cond_add_or_warn (token->location, condition,
11022 &chain);
11024 /* Parse the then-clause. */
11025 in_statement = parser->in_statement;
11026 parser->in_statement |= IN_IF_STMT;
11028 /* Outside a template, the non-selected branch of a constexpr
11029 if is a 'discarded statement', i.e. unevaluated. */
11030 bool was_discarded = parser->in_discarded_stmt;
11031 bool discard_then = (cx && !processing_template_decl
11032 && integer_zerop (condition));
11033 if (discard_then)
11035 parser->in_discarded_stmt = true;
11036 ++c_inhibit_evaluation_warnings;
11039 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11040 guard_tinfo);
11042 parser->in_statement = in_statement;
11044 finish_then_clause (statement);
11046 if (discard_then)
11048 THEN_CLAUSE (statement) = NULL_TREE;
11049 parser->in_discarded_stmt = was_discarded;
11050 --c_inhibit_evaluation_warnings;
11053 /* If the next token is `else', parse the else-clause. */
11054 if (cp_lexer_next_token_is_keyword (parser->lexer,
11055 RID_ELSE))
11057 bool discard_else = (cx && !processing_template_decl
11058 && integer_nonzerop (condition));
11059 if (discard_else)
11061 parser->in_discarded_stmt = true;
11062 ++c_inhibit_evaluation_warnings;
11065 guard_tinfo
11066 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11067 /* Consume the `else' keyword. */
11068 cp_lexer_consume_token (parser->lexer);
11069 if (warn_duplicated_cond)
11071 if (cp_lexer_next_token_is_keyword (parser->lexer,
11072 RID_IF)
11073 && chain == NULL)
11075 /* We've got "if (COND) else if (COND2)". Start
11076 the condition chain and add COND as the first
11077 element. */
11078 chain = new vec<tree> ();
11079 if (!CONSTANT_CLASS_P (condition)
11080 && !TREE_SIDE_EFFECTS (condition))
11082 /* Wrap it in a NOP_EXPR so that we can set the
11083 location of the condition. */
11084 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11085 condition);
11086 SET_EXPR_LOCATION (e, token->location);
11087 chain->safe_push (e);
11090 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11091 RID_IF))
11093 /* This is if-else without subsequent if. Zap the
11094 condition chain; we would have already warned at
11095 this point. */
11096 delete chain;
11097 chain = NULL;
11100 begin_else_clause (statement);
11101 /* Parse the else-clause. */
11102 cp_parser_implicitly_scoped_statement (parser, NULL,
11103 guard_tinfo, chain);
11105 finish_else_clause (statement);
11107 /* If we are currently parsing a then-clause, then
11108 IF_P will not be NULL. We set it to true to
11109 indicate that this if statement has an else clause.
11110 This may trigger the Wparentheses warning below
11111 when we get back up to the parent if statement. */
11112 if (if_p != NULL)
11113 *if_p = true;
11115 if (discard_else)
11117 ELSE_CLAUSE (statement) = NULL_TREE;
11118 parser->in_discarded_stmt = was_discarded;
11119 --c_inhibit_evaluation_warnings;
11122 else
11124 /* This if statement does not have an else clause. If
11125 NESTED_IF is true, then the then-clause has an if
11126 statement which does have an else clause. We warn
11127 about the potential ambiguity. */
11128 if (nested_if)
11129 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11130 "suggest explicit braces to avoid ambiguous"
11131 " %<else%>");
11132 if (warn_duplicated_cond)
11134 /* We don't need the condition chain anymore. */
11135 delete chain;
11136 chain = NULL;
11140 /* Now we're all done with the if-statement. */
11141 finish_if_stmt (statement);
11143 else
11145 bool in_switch_statement_p;
11146 unsigned char in_statement;
11148 /* Add the condition. */
11149 finish_switch_cond (condition, statement);
11151 /* Parse the body of the switch-statement. */
11152 in_switch_statement_p = parser->in_switch_statement_p;
11153 in_statement = parser->in_statement;
11154 parser->in_switch_statement_p = true;
11155 parser->in_statement |= IN_SWITCH_STMT;
11156 cp_parser_implicitly_scoped_statement (parser, if_p,
11157 guard_tinfo);
11158 parser->in_switch_statement_p = in_switch_statement_p;
11159 parser->in_statement = in_statement;
11161 /* Now we're all done with the switch-statement. */
11162 finish_switch_stmt (statement);
11165 return statement;
11167 break;
11169 default:
11170 cp_parser_error (parser, "expected selection-statement");
11171 return error_mark_node;
11175 /* Parse a condition.
11177 condition:
11178 expression
11179 type-specifier-seq declarator = initializer-clause
11180 type-specifier-seq declarator braced-init-list
11182 GNU Extension:
11184 condition:
11185 type-specifier-seq declarator asm-specification [opt]
11186 attributes [opt] = assignment-expression
11188 Returns the expression that should be tested. */
11190 static tree
11191 cp_parser_condition (cp_parser* parser)
11193 cp_decl_specifier_seq type_specifiers;
11194 const char *saved_message;
11195 int declares_class_or_enum;
11197 /* Try the declaration first. */
11198 cp_parser_parse_tentatively (parser);
11199 /* New types are not allowed in the type-specifier-seq for a
11200 condition. */
11201 saved_message = parser->type_definition_forbidden_message;
11202 parser->type_definition_forbidden_message
11203 = G_("types may not be defined in conditions");
11204 /* Parse the type-specifier-seq. */
11205 cp_parser_decl_specifier_seq (parser,
11206 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11207 &type_specifiers,
11208 &declares_class_or_enum);
11209 /* Restore the saved message. */
11210 parser->type_definition_forbidden_message = saved_message;
11211 /* If all is well, we might be looking at a declaration. */
11212 if (!cp_parser_error_occurred (parser))
11214 tree decl;
11215 tree asm_specification;
11216 tree attributes;
11217 cp_declarator *declarator;
11218 tree initializer = NULL_TREE;
11220 /* Parse the declarator. */
11221 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11222 /*ctor_dtor_or_conv_p=*/NULL,
11223 /*parenthesized_p=*/NULL,
11224 /*member_p=*/false,
11225 /*friend_p=*/false);
11226 /* Parse the attributes. */
11227 attributes = cp_parser_attributes_opt (parser);
11228 /* Parse the asm-specification. */
11229 asm_specification = cp_parser_asm_specification_opt (parser);
11230 /* If the next token is not an `=' or '{', then we might still be
11231 looking at an expression. For example:
11233 if (A(a).x)
11235 looks like a decl-specifier-seq and a declarator -- but then
11236 there is no `=', so this is an expression. */
11237 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11238 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11239 cp_parser_simulate_error (parser);
11241 /* If we did see an `=' or '{', then we are looking at a declaration
11242 for sure. */
11243 if (cp_parser_parse_definitely (parser))
11245 tree pushed_scope;
11246 bool non_constant_p;
11247 bool flags = LOOKUP_ONLYCONVERTING;
11249 /* Create the declaration. */
11250 decl = start_decl (declarator, &type_specifiers,
11251 /*initialized_p=*/true,
11252 attributes, /*prefix_attributes=*/NULL_TREE,
11253 &pushed_scope);
11255 /* Parse the initializer. */
11256 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11258 initializer = cp_parser_braced_list (parser, &non_constant_p);
11259 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11260 flags = 0;
11262 else
11264 /* Consume the `='. */
11265 cp_parser_require (parser, CPP_EQ, RT_EQ);
11266 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11268 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11269 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11271 /* Process the initializer. */
11272 cp_finish_decl (decl,
11273 initializer, !non_constant_p,
11274 asm_specification,
11275 flags);
11277 if (pushed_scope)
11278 pop_scope (pushed_scope);
11280 return convert_from_reference (decl);
11283 /* If we didn't even get past the declarator successfully, we are
11284 definitely not looking at a declaration. */
11285 else
11286 cp_parser_abort_tentative_parse (parser);
11288 /* Otherwise, we are looking at an expression. */
11289 return cp_parser_expression (parser);
11292 /* Parses a for-statement or range-for-statement until the closing ')',
11293 not included. */
11295 static tree
11296 cp_parser_for (cp_parser *parser, bool ivdep)
11298 tree init, scope, decl;
11299 bool is_range_for;
11301 /* Begin the for-statement. */
11302 scope = begin_for_scope (&init);
11304 /* Parse the initialization. */
11305 is_range_for = cp_parser_for_init_statement (parser, &decl);
11307 if (is_range_for)
11308 return cp_parser_range_for (parser, scope, init, decl, ivdep);
11309 else
11310 return cp_parser_c_for (parser, scope, init, ivdep);
11313 static tree
11314 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11316 /* Normal for loop */
11317 tree condition = NULL_TREE;
11318 tree expression = NULL_TREE;
11319 tree stmt;
11321 stmt = begin_for_stmt (scope, init);
11322 /* The for-init-statement has already been parsed in
11323 cp_parser_for_init_statement, so no work is needed here. */
11324 finish_for_init_stmt (stmt);
11326 /* If there's a condition, process it. */
11327 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11328 condition = cp_parser_condition (parser);
11329 else if (ivdep)
11331 cp_parser_error (parser, "missing loop condition in loop with "
11332 "%<GCC ivdep%> pragma");
11333 condition = error_mark_node;
11335 finish_for_cond (condition, stmt, ivdep);
11336 /* Look for the `;'. */
11337 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11339 /* If there's an expression, process it. */
11340 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11341 expression = cp_parser_expression (parser);
11342 finish_for_expr (expression, stmt);
11344 return stmt;
11347 /* Tries to parse a range-based for-statement:
11349 range-based-for:
11350 decl-specifier-seq declarator : expression
11352 The decl-specifier-seq declarator and the `:' are already parsed by
11353 cp_parser_for_init_statement. If processing_template_decl it returns a
11354 newly created RANGE_FOR_STMT; if not, it is converted to a
11355 regular FOR_STMT. */
11357 static tree
11358 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11359 bool ivdep)
11361 tree stmt, range_expr;
11362 cxx_binding *binding = NULL;
11363 tree name = NULL_TREE;
11365 /* Get the range declaration momentarily out of the way so that
11366 the range expression doesn't clash with it. */
11367 if (range_decl != error_mark_node)
11369 name = DECL_NAME (range_decl);
11370 binding = IDENTIFIER_BINDING (name);
11371 IDENTIFIER_BINDING (name) = binding->previous;
11374 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11376 bool expr_non_constant_p;
11377 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11379 else
11380 range_expr = cp_parser_expression (parser);
11382 /* Put the range declaration back into scope. */
11383 if (range_decl != error_mark_node)
11385 binding->previous = IDENTIFIER_BINDING (name);
11386 IDENTIFIER_BINDING (name) = binding;
11389 /* If in template, STMT is converted to a normal for-statement
11390 at instantiation. If not, it is done just ahead. */
11391 if (processing_template_decl)
11393 if (check_for_bare_parameter_packs (range_expr))
11394 range_expr = error_mark_node;
11395 stmt = begin_range_for_stmt (scope, init);
11396 if (ivdep)
11397 RANGE_FOR_IVDEP (stmt) = 1;
11398 finish_range_for_decl (stmt, range_decl, range_expr);
11399 if (!type_dependent_expression_p (range_expr)
11400 /* do_auto_deduction doesn't mess with template init-lists. */
11401 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11402 do_range_for_auto_deduction (range_decl, range_expr);
11404 else
11406 stmt = begin_for_stmt (scope, init);
11407 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
11409 return stmt;
11412 /* Subroutine of cp_convert_range_for: given the initializer expression,
11413 builds up the range temporary. */
11415 static tree
11416 build_range_temp (tree range_expr)
11418 tree range_type, range_temp;
11420 /* Find out the type deduced by the declaration
11421 `auto &&__range = range_expr'. */
11422 range_type = cp_build_reference_type (make_auto (), true);
11423 range_type = do_auto_deduction (range_type, range_expr,
11424 type_uses_auto (range_type));
11426 /* Create the __range variable. */
11427 range_temp = build_decl (input_location, VAR_DECL,
11428 get_identifier ("__for_range"), range_type);
11429 TREE_USED (range_temp) = 1;
11430 DECL_ARTIFICIAL (range_temp) = 1;
11432 return range_temp;
11435 /* Used by cp_parser_range_for in template context: we aren't going to
11436 do a full conversion yet, but we still need to resolve auto in the
11437 type of the for-range-declaration if present. This is basically
11438 a shortcut version of cp_convert_range_for. */
11440 static void
11441 do_range_for_auto_deduction (tree decl, tree range_expr)
11443 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11444 if (auto_node)
11446 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11447 range_temp = convert_from_reference (build_range_temp (range_expr));
11448 iter_type = (cp_parser_perform_range_for_lookup
11449 (range_temp, &begin_dummy, &end_dummy));
11450 if (iter_type)
11452 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11453 iter_type);
11454 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
11455 tf_warning_or_error);
11456 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11457 iter_decl, auto_node);
11462 /* Converts a range-based for-statement into a normal
11463 for-statement, as per the definition.
11465 for (RANGE_DECL : RANGE_EXPR)
11466 BLOCK
11468 should be equivalent to:
11471 auto &&__range = RANGE_EXPR;
11472 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11473 __begin != __end;
11474 ++__begin)
11476 RANGE_DECL = *__begin;
11477 BLOCK
11481 If RANGE_EXPR is an array:
11482 BEGIN_EXPR = __range
11483 END_EXPR = __range + ARRAY_SIZE(__range)
11484 Else if RANGE_EXPR has a member 'begin' or 'end':
11485 BEGIN_EXPR = __range.begin()
11486 END_EXPR = __range.end()
11487 Else:
11488 BEGIN_EXPR = begin(__range)
11489 END_EXPR = end(__range);
11491 If __range has a member 'begin' but not 'end', or vice versa, we must
11492 still use the second alternative (it will surely fail, however).
11493 When calling begin()/end() in the third alternative we must use
11494 argument dependent lookup, but always considering 'std' as an associated
11495 namespace. */
11497 tree
11498 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11499 bool ivdep)
11501 tree begin, end;
11502 tree iter_type, begin_expr, end_expr;
11503 tree condition, expression;
11505 if (range_decl == error_mark_node || range_expr == error_mark_node)
11506 /* If an error happened previously do nothing or else a lot of
11507 unhelpful errors would be issued. */
11508 begin_expr = end_expr = iter_type = error_mark_node;
11509 else
11511 tree range_temp;
11513 if (VAR_P (range_expr)
11514 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11515 /* Can't bind a reference to an array of runtime bound. */
11516 range_temp = range_expr;
11517 else
11519 range_temp = build_range_temp (range_expr);
11520 pushdecl (range_temp);
11521 cp_finish_decl (range_temp, range_expr,
11522 /*is_constant_init*/false, NULL_TREE,
11523 LOOKUP_ONLYCONVERTING);
11524 range_temp = convert_from_reference (range_temp);
11526 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11527 &begin_expr, &end_expr);
11530 /* The new for initialization statement. */
11531 begin = build_decl (input_location, VAR_DECL,
11532 get_identifier ("__for_begin"), iter_type);
11533 TREE_USED (begin) = 1;
11534 DECL_ARTIFICIAL (begin) = 1;
11535 pushdecl (begin);
11536 cp_finish_decl (begin, begin_expr,
11537 /*is_constant_init*/false, NULL_TREE,
11538 LOOKUP_ONLYCONVERTING);
11540 if (cxx_dialect >= cxx1z)
11541 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11542 end = build_decl (input_location, VAR_DECL,
11543 get_identifier ("__for_end"), iter_type);
11544 TREE_USED (end) = 1;
11545 DECL_ARTIFICIAL (end) = 1;
11546 pushdecl (end);
11547 cp_finish_decl (end, end_expr,
11548 /*is_constant_init*/false, NULL_TREE,
11549 LOOKUP_ONLYCONVERTING);
11551 finish_for_init_stmt (statement);
11553 /* The new for condition. */
11554 condition = build_x_binary_op (input_location, NE_EXPR,
11555 begin, ERROR_MARK,
11556 end, ERROR_MARK,
11557 NULL, tf_warning_or_error);
11558 finish_for_cond (condition, statement, ivdep);
11560 /* The new increment expression. */
11561 expression = finish_unary_op_expr (input_location,
11562 PREINCREMENT_EXPR, begin,
11563 tf_warning_or_error);
11564 finish_for_expr (expression, statement);
11566 /* The declaration is initialized with *__begin inside the loop body. */
11567 cp_finish_decl (range_decl,
11568 build_x_indirect_ref (input_location, begin, RO_NULL,
11569 tf_warning_or_error),
11570 /*is_constant_init*/false, NULL_TREE,
11571 LOOKUP_ONLYCONVERTING);
11573 return statement;
11576 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11577 We need to solve both at the same time because the method used
11578 depends on the existence of members begin or end.
11579 Returns the type deduced for the iterator expression. */
11581 static tree
11582 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11584 if (error_operand_p (range))
11586 *begin = *end = error_mark_node;
11587 return error_mark_node;
11590 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11592 error ("range-based %<for%> expression of type %qT "
11593 "has incomplete type", TREE_TYPE (range));
11594 *begin = *end = error_mark_node;
11595 return error_mark_node;
11597 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11599 /* If RANGE is an array, we will use pointer arithmetic. */
11600 *begin = decay_conversion (range, tf_warning_or_error);
11601 *end = build_binary_op (input_location, PLUS_EXPR,
11602 range,
11603 array_type_nelts_top (TREE_TYPE (range)),
11605 return TREE_TYPE (*begin);
11607 else
11609 /* If it is not an array, we must do a bit of magic. */
11610 tree id_begin, id_end;
11611 tree member_begin, member_end;
11613 *begin = *end = error_mark_node;
11615 id_begin = get_identifier ("begin");
11616 id_end = get_identifier ("end");
11617 member_begin = lookup_member (TREE_TYPE (range), id_begin,
11618 /*protect=*/2, /*want_type=*/false,
11619 tf_warning_or_error);
11620 member_end = lookup_member (TREE_TYPE (range), id_end,
11621 /*protect=*/2, /*want_type=*/false,
11622 tf_warning_or_error);
11624 if (member_begin != NULL_TREE || member_end != NULL_TREE)
11626 /* Use the member functions. */
11627 if (member_begin != NULL_TREE)
11628 *begin = cp_parser_range_for_member_function (range, id_begin);
11629 else
11630 error ("range-based %<for%> expression of type %qT has an "
11631 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11633 if (member_end != NULL_TREE)
11634 *end = cp_parser_range_for_member_function (range, id_end);
11635 else
11636 error ("range-based %<for%> expression of type %qT has a "
11637 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
11639 else
11641 /* Use global functions with ADL. */
11642 vec<tree, va_gc> *vec;
11643 vec = make_tree_vector ();
11645 vec_safe_push (vec, range);
11647 member_begin = perform_koenig_lookup (id_begin, vec,
11648 tf_warning_or_error);
11649 *begin = finish_call_expr (member_begin, &vec, false, true,
11650 tf_warning_or_error);
11651 member_end = perform_koenig_lookup (id_end, vec,
11652 tf_warning_or_error);
11653 *end = finish_call_expr (member_end, &vec, false, true,
11654 tf_warning_or_error);
11656 release_tree_vector (vec);
11659 /* Last common checks. */
11660 if (*begin == error_mark_node || *end == error_mark_node)
11662 /* If one of the expressions is an error do no more checks. */
11663 *begin = *end = error_mark_node;
11664 return error_mark_node;
11666 else if (type_dependent_expression_p (*begin)
11667 || type_dependent_expression_p (*end))
11668 /* Can happen, when, eg, in a template context, Koenig lookup
11669 can't resolve begin/end (c++/58503). */
11670 return NULL_TREE;
11671 else
11673 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
11674 /* The unqualified type of the __begin and __end temporaries should
11675 be the same, as required by the multiple auto declaration. */
11676 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
11678 if (cxx_dialect >= cxx1z
11679 && (build_x_binary_op (input_location, NE_EXPR,
11680 *begin, ERROR_MARK,
11681 *end, ERROR_MARK,
11682 NULL, tf_none)
11683 != error_mark_node))
11684 /* P0184R0 allows __begin and __end to have different types,
11685 but make sure they are comparable so we can give a better
11686 diagnostic. */;
11687 else
11688 error ("inconsistent begin/end types in range-based %<for%> "
11689 "statement: %qT and %qT",
11690 TREE_TYPE (*begin), TREE_TYPE (*end));
11692 return iter_type;
11697 /* Helper function for cp_parser_perform_range_for_lookup.
11698 Builds a tree for RANGE.IDENTIFIER(). */
11700 static tree
11701 cp_parser_range_for_member_function (tree range, tree identifier)
11703 tree member, res;
11704 vec<tree, va_gc> *vec;
11706 member = finish_class_member_access_expr (range, identifier,
11707 false, tf_warning_or_error);
11708 if (member == error_mark_node)
11709 return error_mark_node;
11711 vec = make_tree_vector ();
11712 res = finish_call_expr (member, &vec,
11713 /*disallow_virtual=*/false,
11714 /*koenig_p=*/false,
11715 tf_warning_or_error);
11716 release_tree_vector (vec);
11717 return res;
11720 /* Parse an iteration-statement.
11722 iteration-statement:
11723 while ( condition ) statement
11724 do statement while ( expression ) ;
11725 for ( for-init-statement condition [opt] ; expression [opt] )
11726 statement
11728 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
11730 static tree
11731 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep)
11733 cp_token *token;
11734 enum rid keyword;
11735 tree statement;
11736 unsigned char in_statement;
11737 token_indent_info guard_tinfo;
11739 /* Peek at the next token. */
11740 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
11741 if (!token)
11742 return error_mark_node;
11744 guard_tinfo = get_token_indent_info (token);
11746 /* Remember whether or not we are already within an iteration
11747 statement. */
11748 in_statement = parser->in_statement;
11750 /* See what kind of keyword it is. */
11751 keyword = token->keyword;
11752 switch (keyword)
11754 case RID_WHILE:
11756 tree condition;
11758 /* Begin the while-statement. */
11759 statement = begin_while_stmt ();
11760 /* Look for the `('. */
11761 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11762 /* Parse the condition. */
11763 condition = cp_parser_condition (parser);
11764 finish_while_stmt_cond (condition, statement, ivdep);
11765 /* Look for the `)'. */
11766 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11767 /* Parse the dependent statement. */
11768 parser->in_statement = IN_ITERATION_STMT;
11769 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11770 parser->in_statement = in_statement;
11771 /* We're done with the while-statement. */
11772 finish_while_stmt (statement);
11774 break;
11776 case RID_DO:
11778 tree expression;
11780 /* Begin the do-statement. */
11781 statement = begin_do_stmt ();
11782 /* Parse the body of the do-statement. */
11783 parser->in_statement = IN_ITERATION_STMT;
11784 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
11785 parser->in_statement = in_statement;
11786 finish_do_body (statement);
11787 /* Look for the `while' keyword. */
11788 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
11789 /* Look for the `('. */
11790 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11791 /* Parse the expression. */
11792 expression = cp_parser_expression (parser);
11793 /* We're done with the do-statement. */
11794 finish_do_stmt (expression, statement, ivdep);
11795 /* Look for the `)'. */
11796 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11797 /* Look for the `;'. */
11798 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11800 break;
11802 case RID_FOR:
11804 /* Look for the `('. */
11805 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11807 statement = cp_parser_for (parser, ivdep);
11809 /* Look for the `)'. */
11810 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11812 /* Parse the body of the for-statement. */
11813 parser->in_statement = IN_ITERATION_STMT;
11814 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11815 parser->in_statement = in_statement;
11817 /* We're done with the for-statement. */
11818 finish_for_stmt (statement);
11820 break;
11822 default:
11823 cp_parser_error (parser, "expected iteration-statement");
11824 statement = error_mark_node;
11825 break;
11828 return statement;
11831 /* Parse a for-init-statement or the declarator of a range-based-for.
11832 Returns true if a range-based-for declaration is seen.
11834 for-init-statement:
11835 expression-statement
11836 simple-declaration */
11838 static bool
11839 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
11841 /* If the next token is a `;', then we have an empty
11842 expression-statement. Grammatically, this is also a
11843 simple-declaration, but an invalid one, because it does not
11844 declare anything. Therefore, if we did not handle this case
11845 specially, we would issue an error message about an invalid
11846 declaration. */
11847 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11849 bool is_range_for = false;
11850 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11852 /* A colon is used in range-based for. */
11853 parser->colon_corrects_to_scope_p = false;
11855 /* We're going to speculatively look for a declaration, falling back
11856 to an expression, if necessary. */
11857 cp_parser_parse_tentatively (parser);
11858 /* Parse the declaration. */
11859 cp_parser_simple_declaration (parser,
11860 /*function_definition_allowed_p=*/false,
11861 decl);
11862 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11863 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11865 /* It is a range-for, consume the ':' */
11866 cp_lexer_consume_token (parser->lexer);
11867 is_range_for = true;
11868 if (cxx_dialect < cxx11)
11870 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11871 "range-based %<for%> loops only available with "
11872 "-std=c++11 or -std=gnu++11");
11873 *decl = error_mark_node;
11876 else
11877 /* The ';' is not consumed yet because we told
11878 cp_parser_simple_declaration not to. */
11879 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11881 if (cp_parser_parse_definitely (parser))
11882 return is_range_for;
11883 /* If the tentative parse failed, then we shall need to look for an
11884 expression-statement. */
11886 /* If we are here, it is an expression-statement. */
11887 cp_parser_expression_statement (parser, NULL_TREE);
11888 return false;
11891 /* Parse a jump-statement.
11893 jump-statement:
11894 break ;
11895 continue ;
11896 return expression [opt] ;
11897 return braced-init-list ;
11898 goto identifier ;
11900 GNU extension:
11902 jump-statement:
11903 goto * expression ;
11905 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
11907 static tree
11908 cp_parser_jump_statement (cp_parser* parser)
11910 tree statement = error_mark_node;
11911 cp_token *token;
11912 enum rid keyword;
11913 unsigned char in_statement;
11915 /* Peek at the next token. */
11916 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
11917 if (!token)
11918 return error_mark_node;
11920 /* See what kind of keyword it is. */
11921 keyword = token->keyword;
11922 switch (keyword)
11924 case RID_BREAK:
11925 in_statement = parser->in_statement & ~IN_IF_STMT;
11926 switch (in_statement)
11928 case 0:
11929 error_at (token->location, "break statement not within loop or switch");
11930 break;
11931 default:
11932 gcc_assert ((in_statement & IN_SWITCH_STMT)
11933 || in_statement == IN_ITERATION_STMT);
11934 statement = finish_break_stmt ();
11935 if (in_statement == IN_ITERATION_STMT)
11936 break_maybe_infinite_loop ();
11937 break;
11938 case IN_OMP_BLOCK:
11939 error_at (token->location, "invalid exit from OpenMP structured block");
11940 break;
11941 case IN_OMP_FOR:
11942 error_at (token->location, "break statement used with OpenMP for loop");
11943 break;
11944 case IN_CILK_SIMD_FOR:
11945 error_at (token->location, "break statement used with Cilk Plus for loop");
11946 break;
11948 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11949 break;
11951 case RID_CONTINUE:
11952 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
11954 case 0:
11955 error_at (token->location, "continue statement not within a loop");
11956 break;
11957 case IN_CILK_SIMD_FOR:
11958 error_at (token->location,
11959 "continue statement within %<#pragma simd%> loop body");
11960 /* Fall through. */
11961 case IN_ITERATION_STMT:
11962 case IN_OMP_FOR:
11963 statement = finish_continue_stmt ();
11964 break;
11965 case IN_OMP_BLOCK:
11966 error_at (token->location, "invalid exit from OpenMP structured block");
11967 break;
11968 default:
11969 gcc_unreachable ();
11971 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11972 break;
11974 case RID_RETURN:
11976 tree expr;
11977 bool expr_non_constant_p;
11979 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11981 cp_lexer_set_source_position (parser->lexer);
11982 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11983 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11985 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11986 expr = cp_parser_expression (parser);
11987 else
11988 /* If the next token is a `;', then there is no
11989 expression. */
11990 expr = NULL_TREE;
11991 /* Build the return-statement. */
11992 if (current_function_auto_return_pattern && parser->in_discarded_stmt)
11993 /* Don't deduce from a discarded return statement. */;
11994 else
11995 statement = finish_return_stmt (expr);
11996 /* Look for the final `;'. */
11997 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11999 break;
12001 case RID_GOTO:
12002 if (parser->in_function_body
12003 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12005 error ("%<goto%> in %<constexpr%> function");
12006 cp_function_chain->invalid_constexpr = true;
12009 /* Create the goto-statement. */
12010 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12012 /* Issue a warning about this use of a GNU extension. */
12013 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12014 /* Consume the '*' token. */
12015 cp_lexer_consume_token (parser->lexer);
12016 /* Parse the dependent expression. */
12017 finish_goto_stmt (cp_parser_expression (parser));
12019 else
12020 finish_goto_stmt (cp_parser_identifier (parser));
12021 /* Look for the final `;'. */
12022 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12023 break;
12025 default:
12026 cp_parser_error (parser, "expected jump-statement");
12027 break;
12030 return statement;
12033 /* Parse a declaration-statement.
12035 declaration-statement:
12036 block-declaration */
12038 static void
12039 cp_parser_declaration_statement (cp_parser* parser)
12041 void *p;
12043 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12044 p = obstack_alloc (&declarator_obstack, 0);
12046 /* Parse the block-declaration. */
12047 cp_parser_block_declaration (parser, /*statement_p=*/true);
12049 /* Free any declarators allocated. */
12050 obstack_free (&declarator_obstack, p);
12053 /* Some dependent statements (like `if (cond) statement'), are
12054 implicitly in their own scope. In other words, if the statement is
12055 a single statement (as opposed to a compound-statement), it is
12056 none-the-less treated as if it were enclosed in braces. Any
12057 declarations appearing in the dependent statement are out of scope
12058 after control passes that point. This function parses a statement,
12059 but ensures that is in its own scope, even if it is not a
12060 compound-statement.
12062 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12063 is a (possibly labeled) if statement which is not enclosed in
12064 braces and has an else clause. This is used to implement
12065 -Wparentheses.
12067 CHAIN is a vector of if-else-if conditions. This is used to implement
12068 -Wduplicated-cond.
12070 Returns the new statement. */
12072 static tree
12073 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12074 const token_indent_info &guard_tinfo,
12075 vec<tree> *chain)
12077 tree statement;
12078 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12079 token_indent_info body_tinfo
12080 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12082 if (if_p != NULL)
12083 *if_p = false;
12085 /* Mark if () ; with a special NOP_EXPR. */
12086 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12088 cp_lexer_consume_token (parser->lexer);
12089 statement = add_stmt (build_empty_stmt (body_loc));
12091 if (guard_tinfo.keyword == RID_IF
12092 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12093 warning_at (body_loc, OPT_Wempty_body,
12094 "suggest braces around empty body in an %<if%> statement");
12095 else if (guard_tinfo.keyword == RID_ELSE)
12096 warning_at (body_loc, OPT_Wempty_body,
12097 "suggest braces around empty body in an %<else%> statement");
12099 /* if a compound is opened, we simply parse the statement directly. */
12100 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12101 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12102 /* If the token is not a `{', then we must take special action. */
12103 else
12105 /* Create a compound-statement. */
12106 statement = begin_compound_stmt (0);
12107 /* Parse the dependent-statement. */
12108 cp_parser_statement (parser, NULL_TREE, false, if_p, chain);
12109 /* Finish the dummy compound-statement. */
12110 finish_compound_stmt (statement);
12113 token_indent_info next_tinfo
12114 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12115 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12117 /* Return the statement. */
12118 return statement;
12121 /* For some dependent statements (like `while (cond) statement'), we
12122 have already created a scope. Therefore, even if the dependent
12123 statement is a compound-statement, we do not want to create another
12124 scope. */
12126 static void
12127 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12128 const token_indent_info &guard_tinfo)
12130 /* If the token is a `{', then we must take special action. */
12131 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12133 token_indent_info body_tinfo
12134 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12136 cp_parser_statement (parser, NULL_TREE, false, if_p);
12137 token_indent_info next_tinfo
12138 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12139 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12141 else
12143 /* Avoid calling cp_parser_compound_statement, so that we
12144 don't create a new scope. Do everything else by hand. */
12145 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
12146 /* If the next keyword is `__label__' we have a label declaration. */
12147 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12148 cp_parser_label_declaration (parser);
12149 /* Parse an (optional) statement-seq. */
12150 cp_parser_statement_seq_opt (parser, NULL_TREE);
12151 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12155 /* Declarations [gram.dcl.dcl] */
12157 /* Parse an optional declaration-sequence.
12159 declaration-seq:
12160 declaration
12161 declaration-seq declaration */
12163 static void
12164 cp_parser_declaration_seq_opt (cp_parser* parser)
12166 while (true)
12168 cp_token *token;
12170 token = cp_lexer_peek_token (parser->lexer);
12172 if (token->type == CPP_CLOSE_BRACE
12173 || token->type == CPP_EOF
12174 || token->type == CPP_PRAGMA_EOL)
12175 break;
12177 if (token->type == CPP_SEMICOLON)
12179 /* A declaration consisting of a single semicolon is
12180 invalid. Allow it unless we're being pedantic. */
12181 cp_lexer_consume_token (parser->lexer);
12182 if (!in_system_header_at (input_location))
12183 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12184 continue;
12187 /* If we're entering or exiting a region that's implicitly
12188 extern "C", modify the lang context appropriately. */
12189 if (!parser->implicit_extern_c && token->implicit_extern_c)
12191 push_lang_context (lang_name_c);
12192 parser->implicit_extern_c = true;
12194 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12196 pop_lang_context ();
12197 parser->implicit_extern_c = false;
12200 if (token->type == CPP_PRAGMA)
12202 /* A top-level declaration can consist solely of a #pragma.
12203 A nested declaration cannot, so this is done here and not
12204 in cp_parser_declaration. (A #pragma at block scope is
12205 handled in cp_parser_statement.) */
12206 cp_parser_pragma (parser, pragma_external, NULL);
12207 continue;
12210 /* Parse the declaration itself. */
12211 cp_parser_declaration (parser);
12215 /* Parse a declaration.
12217 declaration:
12218 block-declaration
12219 function-definition
12220 template-declaration
12221 explicit-instantiation
12222 explicit-specialization
12223 linkage-specification
12224 namespace-definition
12226 GNU extension:
12228 declaration:
12229 __extension__ declaration */
12231 static void
12232 cp_parser_declaration (cp_parser* parser)
12234 cp_token token1;
12235 cp_token token2;
12236 int saved_pedantic;
12237 void *p;
12238 tree attributes = NULL_TREE;
12240 /* Check for the `__extension__' keyword. */
12241 if (cp_parser_extension_opt (parser, &saved_pedantic))
12243 /* Parse the qualified declaration. */
12244 cp_parser_declaration (parser);
12245 /* Restore the PEDANTIC flag. */
12246 pedantic = saved_pedantic;
12248 return;
12251 /* Try to figure out what kind of declaration is present. */
12252 token1 = *cp_lexer_peek_token (parser->lexer);
12254 if (token1.type != CPP_EOF)
12255 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12256 else
12258 token2.type = CPP_EOF;
12259 token2.keyword = RID_MAX;
12262 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12263 p = obstack_alloc (&declarator_obstack, 0);
12265 /* If the next token is `extern' and the following token is a string
12266 literal, then we have a linkage specification. */
12267 if (token1.keyword == RID_EXTERN
12268 && cp_parser_is_pure_string_literal (&token2))
12269 cp_parser_linkage_specification (parser);
12270 /* If the next token is `template', then we have either a template
12271 declaration, an explicit instantiation, or an explicit
12272 specialization. */
12273 else if (token1.keyword == RID_TEMPLATE)
12275 /* `template <>' indicates a template specialization. */
12276 if (token2.type == CPP_LESS
12277 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12278 cp_parser_explicit_specialization (parser);
12279 /* `template <' indicates a template declaration. */
12280 else if (token2.type == CPP_LESS)
12281 cp_parser_template_declaration (parser, /*member_p=*/false);
12282 /* Anything else must be an explicit instantiation. */
12283 else
12284 cp_parser_explicit_instantiation (parser);
12286 /* If the next token is `export', then we have a template
12287 declaration. */
12288 else if (token1.keyword == RID_EXPORT)
12289 cp_parser_template_declaration (parser, /*member_p=*/false);
12290 /* If the next token is `extern', 'static' or 'inline' and the one
12291 after that is `template', we have a GNU extended explicit
12292 instantiation directive. */
12293 else if (cp_parser_allow_gnu_extensions_p (parser)
12294 && (token1.keyword == RID_EXTERN
12295 || token1.keyword == RID_STATIC
12296 || token1.keyword == RID_INLINE)
12297 && token2.keyword == RID_TEMPLATE)
12298 cp_parser_explicit_instantiation (parser);
12299 /* If the next token is `namespace', check for a named or unnamed
12300 namespace definition. */
12301 else if (token1.keyword == RID_NAMESPACE
12302 && (/* A named namespace definition. */
12303 (token2.type == CPP_NAME
12304 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12305 != CPP_EQ))
12306 || (token2.type == CPP_OPEN_SQUARE
12307 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12308 == CPP_OPEN_SQUARE)
12309 /* An unnamed namespace definition. */
12310 || token2.type == CPP_OPEN_BRACE
12311 || token2.keyword == RID_ATTRIBUTE))
12312 cp_parser_namespace_definition (parser);
12313 /* An inline (associated) namespace definition. */
12314 else if (token1.keyword == RID_INLINE
12315 && token2.keyword == RID_NAMESPACE)
12316 cp_parser_namespace_definition (parser);
12317 /* Objective-C++ declaration/definition. */
12318 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12319 cp_parser_objc_declaration (parser, NULL_TREE);
12320 else if (c_dialect_objc ()
12321 && token1.keyword == RID_ATTRIBUTE
12322 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12323 cp_parser_objc_declaration (parser, attributes);
12324 /* At this point we may have a template declared by a concept
12325 introduction. */
12326 else if (flag_concepts
12327 && cp_parser_template_declaration_after_export (parser,
12328 /*member_p=*/false))
12329 /* We did. */;
12330 else
12331 /* Try to parse a block-declaration, or a function-definition. */
12332 cp_parser_block_declaration (parser, /*statement_p=*/false);
12334 /* Free any declarators allocated. */
12335 obstack_free (&declarator_obstack, p);
12338 /* Parse a block-declaration.
12340 block-declaration:
12341 simple-declaration
12342 asm-definition
12343 namespace-alias-definition
12344 using-declaration
12345 using-directive
12347 GNU Extension:
12349 block-declaration:
12350 __extension__ block-declaration
12352 C++0x Extension:
12354 block-declaration:
12355 static_assert-declaration
12357 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12358 part of a declaration-statement. */
12360 static void
12361 cp_parser_block_declaration (cp_parser *parser,
12362 bool statement_p)
12364 cp_token *token1;
12365 int saved_pedantic;
12367 /* Check for the `__extension__' keyword. */
12368 if (cp_parser_extension_opt (parser, &saved_pedantic))
12370 /* Parse the qualified declaration. */
12371 cp_parser_block_declaration (parser, statement_p);
12372 /* Restore the PEDANTIC flag. */
12373 pedantic = saved_pedantic;
12375 return;
12378 /* Peek at the next token to figure out which kind of declaration is
12379 present. */
12380 token1 = cp_lexer_peek_token (parser->lexer);
12382 /* If the next keyword is `asm', we have an asm-definition. */
12383 if (token1->keyword == RID_ASM)
12385 if (statement_p)
12386 cp_parser_commit_to_tentative_parse (parser);
12387 cp_parser_asm_definition (parser);
12389 /* If the next keyword is `namespace', we have a
12390 namespace-alias-definition. */
12391 else if (token1->keyword == RID_NAMESPACE)
12392 cp_parser_namespace_alias_definition (parser);
12393 /* If the next keyword is `using', we have a
12394 using-declaration, a using-directive, or an alias-declaration. */
12395 else if (token1->keyword == RID_USING)
12397 cp_token *token2;
12399 if (statement_p)
12400 cp_parser_commit_to_tentative_parse (parser);
12401 /* If the token after `using' is `namespace', then we have a
12402 using-directive. */
12403 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12404 if (token2->keyword == RID_NAMESPACE)
12405 cp_parser_using_directive (parser);
12406 /* If the second token after 'using' is '=', then we have an
12407 alias-declaration. */
12408 else if (cxx_dialect >= cxx11
12409 && token2->type == CPP_NAME
12410 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12411 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12412 cp_parser_alias_declaration (parser);
12413 /* Otherwise, it's a using-declaration. */
12414 else
12415 cp_parser_using_declaration (parser,
12416 /*access_declaration_p=*/false);
12418 /* If the next keyword is `__label__' we have a misplaced label
12419 declaration. */
12420 else if (token1->keyword == RID_LABEL)
12422 cp_lexer_consume_token (parser->lexer);
12423 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12424 cp_parser_skip_to_end_of_statement (parser);
12425 /* If the next token is now a `;', consume it. */
12426 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12427 cp_lexer_consume_token (parser->lexer);
12429 /* If the next token is `static_assert' we have a static assertion. */
12430 else if (token1->keyword == RID_STATIC_ASSERT)
12431 cp_parser_static_assert (parser, /*member_p=*/false);
12432 /* Anything else must be a simple-declaration. */
12433 else
12434 cp_parser_simple_declaration (parser, !statement_p,
12435 /*maybe_range_for_decl*/NULL);
12438 /* Parse a simple-declaration.
12440 simple-declaration:
12441 decl-specifier-seq [opt] init-declarator-list [opt] ;
12443 init-declarator-list:
12444 init-declarator
12445 init-declarator-list , init-declarator
12447 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12448 function-definition as a simple-declaration.
12450 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12451 parsed declaration if it is an uninitialized single declarator not followed
12452 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12453 if present, will not be consumed. */
12455 static void
12456 cp_parser_simple_declaration (cp_parser* parser,
12457 bool function_definition_allowed_p,
12458 tree *maybe_range_for_decl)
12460 cp_decl_specifier_seq decl_specifiers;
12461 int declares_class_or_enum;
12462 bool saw_declarator;
12463 location_t comma_loc = UNKNOWN_LOCATION;
12464 location_t init_loc = UNKNOWN_LOCATION;
12466 if (maybe_range_for_decl)
12467 *maybe_range_for_decl = NULL_TREE;
12469 /* Defer access checks until we know what is being declared; the
12470 checks for names appearing in the decl-specifier-seq should be
12471 done as if we were in the scope of the thing being declared. */
12472 push_deferring_access_checks (dk_deferred);
12474 /* Parse the decl-specifier-seq. We have to keep track of whether
12475 or not the decl-specifier-seq declares a named class or
12476 enumeration type, since that is the only case in which the
12477 init-declarator-list is allowed to be empty.
12479 [dcl.dcl]
12481 In a simple-declaration, the optional init-declarator-list can be
12482 omitted only when declaring a class or enumeration, that is when
12483 the decl-specifier-seq contains either a class-specifier, an
12484 elaborated-type-specifier, or an enum-specifier. */
12485 cp_parser_decl_specifier_seq (parser,
12486 CP_PARSER_FLAGS_OPTIONAL,
12487 &decl_specifiers,
12488 &declares_class_or_enum);
12489 /* We no longer need to defer access checks. */
12490 stop_deferring_access_checks ();
12492 /* In a block scope, a valid declaration must always have a
12493 decl-specifier-seq. By not trying to parse declarators, we can
12494 resolve the declaration/expression ambiguity more quickly. */
12495 if (!function_definition_allowed_p
12496 && !decl_specifiers.any_specifiers_p)
12498 cp_parser_error (parser, "expected declaration");
12499 goto done;
12502 /* If the next two tokens are both identifiers, the code is
12503 erroneous. The usual cause of this situation is code like:
12505 T t;
12507 where "T" should name a type -- but does not. */
12508 if (!decl_specifiers.any_type_specifiers_p
12509 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12511 /* If parsing tentatively, we should commit; we really are
12512 looking at a declaration. */
12513 cp_parser_commit_to_tentative_parse (parser);
12514 /* Give up. */
12515 goto done;
12518 /* If we have seen at least one decl-specifier, and the next token
12519 is not a parenthesis, then we must be looking at a declaration.
12520 (After "int (" we might be looking at a functional cast.) */
12521 if (decl_specifiers.any_specifiers_p
12522 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12523 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12524 && !cp_parser_error_occurred (parser))
12525 cp_parser_commit_to_tentative_parse (parser);
12527 tree last_type;
12529 last_type = NULL_TREE;
12531 /* Keep going until we hit the `;' at the end of the simple
12532 declaration. */
12533 saw_declarator = false;
12534 while (cp_lexer_next_token_is_not (parser->lexer,
12535 CPP_SEMICOLON))
12537 cp_token *token;
12538 bool function_definition_p;
12539 tree decl;
12540 tree auto_result = NULL_TREE;
12542 if (saw_declarator)
12544 /* If we are processing next declarator, comma is expected */
12545 token = cp_lexer_peek_token (parser->lexer);
12546 gcc_assert (token->type == CPP_COMMA);
12547 cp_lexer_consume_token (parser->lexer);
12548 if (maybe_range_for_decl)
12550 *maybe_range_for_decl = error_mark_node;
12551 if (comma_loc == UNKNOWN_LOCATION)
12552 comma_loc = token->location;
12555 else
12556 saw_declarator = true;
12558 /* Parse the init-declarator. */
12559 decl = cp_parser_init_declarator (parser, &decl_specifiers,
12560 /*checks=*/NULL,
12561 function_definition_allowed_p,
12562 /*member_p=*/false,
12563 declares_class_or_enum,
12564 &function_definition_p,
12565 maybe_range_for_decl,
12566 &init_loc,
12567 &auto_result);
12568 /* If an error occurred while parsing tentatively, exit quickly.
12569 (That usually happens when in the body of a function; each
12570 statement is treated as a declaration-statement until proven
12571 otherwise.) */
12572 if (cp_parser_error_occurred (parser))
12573 goto done;
12575 if (auto_result)
12577 if (last_type && last_type != error_mark_node
12578 && !same_type_p (auto_result, last_type))
12580 /* If the list of declarators contains more than one declarator,
12581 the type of each declared variable is determined as described
12582 above. If the type deduced for the template parameter U is not
12583 the same in each deduction, the program is ill-formed. */
12584 error_at (decl_specifiers.locations[ds_type_spec],
12585 "inconsistent deduction for %qT: %qT and then %qT",
12586 decl_specifiers.type, last_type, auto_result);
12587 last_type = error_mark_node;
12589 else
12590 last_type = auto_result;
12593 /* Handle function definitions specially. */
12594 if (function_definition_p)
12596 /* If the next token is a `,', then we are probably
12597 processing something like:
12599 void f() {}, *p;
12601 which is erroneous. */
12602 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12604 cp_token *token = cp_lexer_peek_token (parser->lexer);
12605 error_at (token->location,
12606 "mixing"
12607 " declarations and function-definitions is forbidden");
12609 /* Otherwise, we're done with the list of declarators. */
12610 else
12612 pop_deferring_access_checks ();
12613 return;
12616 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
12617 *maybe_range_for_decl = decl;
12618 /* The next token should be either a `,' or a `;'. */
12619 token = cp_lexer_peek_token (parser->lexer);
12620 /* If it's a `,', there are more declarators to come. */
12621 if (token->type == CPP_COMMA)
12622 /* will be consumed next time around */;
12623 /* If it's a `;', we are done. */
12624 else if (token->type == CPP_SEMICOLON)
12625 break;
12626 else if (maybe_range_for_decl)
12628 if (declares_class_or_enum && token->type == CPP_COLON)
12629 permerror (decl_specifiers.locations[ds_type_spec],
12630 "types may not be defined in a for-range-declaration");
12631 break;
12633 /* Anything else is an error. */
12634 else
12636 /* If we have already issued an error message we don't need
12637 to issue another one. */
12638 if ((decl != error_mark_node
12639 && DECL_INITIAL (decl) != error_mark_node)
12640 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12641 cp_parser_error (parser, "expected %<,%> or %<;%>");
12642 /* Skip tokens until we reach the end of the statement. */
12643 cp_parser_skip_to_end_of_statement (parser);
12644 /* If the next token is now a `;', consume it. */
12645 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12646 cp_lexer_consume_token (parser->lexer);
12647 goto done;
12649 /* After the first time around, a function-definition is not
12650 allowed -- even if it was OK at first. For example:
12652 int i, f() {}
12654 is not valid. */
12655 function_definition_allowed_p = false;
12658 /* Issue an error message if no declarators are present, and the
12659 decl-specifier-seq does not itself declare a class or
12660 enumeration: [dcl.dcl]/3. */
12661 if (!saw_declarator)
12663 if (cp_parser_declares_only_class_p (parser))
12665 if (!declares_class_or_enum
12666 && decl_specifiers.type
12667 && OVERLOAD_TYPE_P (decl_specifiers.type))
12668 /* Ensure an error is issued anyway when finish_decltype_type,
12669 called via cp_parser_decl_specifier_seq, returns a class or
12670 an enumeration (c++/51786). */
12671 decl_specifiers.type = NULL_TREE;
12672 shadow_tag (&decl_specifiers);
12674 /* Perform any deferred access checks. */
12675 perform_deferred_access_checks (tf_warning_or_error);
12678 /* Consume the `;'. */
12679 if (!maybe_range_for_decl)
12680 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12681 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12683 if (init_loc != UNKNOWN_LOCATION)
12684 error_at (init_loc, "initializer in range-based %<for%> loop");
12685 if (comma_loc != UNKNOWN_LOCATION)
12686 error_at (comma_loc,
12687 "multiple declarations in range-based %<for%> loop");
12690 done:
12691 pop_deferring_access_checks ();
12694 /* Parse a decl-specifier-seq.
12696 decl-specifier-seq:
12697 decl-specifier-seq [opt] decl-specifier
12698 decl-specifier attribute-specifier-seq [opt] (C++11)
12700 decl-specifier:
12701 storage-class-specifier
12702 type-specifier
12703 function-specifier
12704 friend
12705 typedef
12707 GNU Extension:
12709 decl-specifier:
12710 attributes
12712 Concepts Extension:
12714 decl-specifier:
12715 concept
12717 Set *DECL_SPECS to a representation of the decl-specifier-seq.
12719 The parser flags FLAGS is used to control type-specifier parsing.
12721 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
12722 flags:
12724 1: one of the decl-specifiers is an elaborated-type-specifier
12725 (i.e., a type declaration)
12726 2: one of the decl-specifiers is an enum-specifier or a
12727 class-specifier (i.e., a type definition)
12731 static void
12732 cp_parser_decl_specifier_seq (cp_parser* parser,
12733 cp_parser_flags flags,
12734 cp_decl_specifier_seq *decl_specs,
12735 int* declares_class_or_enum)
12737 bool constructor_possible_p = !parser->in_declarator_p;
12738 bool found_decl_spec = false;
12739 cp_token *start_token = NULL;
12740 cp_decl_spec ds;
12742 /* Clear DECL_SPECS. */
12743 clear_decl_specs (decl_specs);
12745 /* Assume no class or enumeration type is declared. */
12746 *declares_class_or_enum = 0;
12748 /* Keep reading specifiers until there are no more to read. */
12749 while (true)
12751 bool constructor_p;
12752 cp_token *token;
12753 ds = ds_last;
12755 /* Peek at the next token. */
12756 token = cp_lexer_peek_token (parser->lexer);
12758 /* Save the first token of the decl spec list for error
12759 reporting. */
12760 if (!start_token)
12761 start_token = token;
12762 /* Handle attributes. */
12763 if (cp_next_tokens_can_be_attribute_p (parser))
12765 /* Parse the attributes. */
12766 tree attrs = cp_parser_attributes_opt (parser);
12768 /* In a sequence of declaration specifiers, c++11 attributes
12769 appertain to the type that precede them. In that case
12770 [dcl.spec]/1 says:
12772 The attribute-specifier-seq affects the type only for
12773 the declaration it appears in, not other declarations
12774 involving the same type.
12776 But for now let's force the user to position the
12777 attribute either at the beginning of the declaration or
12778 after the declarator-id, which would clearly mean that it
12779 applies to the declarator. */
12780 if (cxx11_attribute_p (attrs))
12782 if (!found_decl_spec)
12783 /* The c++11 attribute is at the beginning of the
12784 declaration. It appertains to the entity being
12785 declared. */;
12786 else
12788 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
12790 /* This is an attribute following a
12791 class-specifier. */
12792 if (decl_specs->type_definition_p)
12793 warn_misplaced_attr_for_class_type (token->location,
12794 decl_specs->type);
12795 attrs = NULL_TREE;
12797 else
12799 decl_specs->std_attributes
12800 = chainon (decl_specs->std_attributes,
12801 attrs);
12802 if (decl_specs->locations[ds_std_attribute] == 0)
12803 decl_specs->locations[ds_std_attribute] = token->location;
12805 continue;
12809 decl_specs->attributes
12810 = chainon (decl_specs->attributes,
12811 attrs);
12812 if (decl_specs->locations[ds_attribute] == 0)
12813 decl_specs->locations[ds_attribute] = token->location;
12814 continue;
12816 /* Assume we will find a decl-specifier keyword. */
12817 found_decl_spec = true;
12818 /* If the next token is an appropriate keyword, we can simply
12819 add it to the list. */
12820 switch (token->keyword)
12822 /* decl-specifier:
12823 friend
12824 constexpr */
12825 case RID_FRIEND:
12826 if (!at_class_scope_p ())
12828 error_at (token->location, "%<friend%> used outside of class");
12829 cp_lexer_purge_token (parser->lexer);
12831 else
12833 ds = ds_friend;
12834 /* Consume the token. */
12835 cp_lexer_consume_token (parser->lexer);
12837 break;
12839 case RID_CONSTEXPR:
12840 ds = ds_constexpr;
12841 cp_lexer_consume_token (parser->lexer);
12842 break;
12844 case RID_CONCEPT:
12845 ds = ds_concept;
12846 cp_lexer_consume_token (parser->lexer);
12847 break;
12849 /* function-specifier:
12850 inline
12851 virtual
12852 explicit */
12853 case RID_INLINE:
12854 case RID_VIRTUAL:
12855 case RID_EXPLICIT:
12856 cp_parser_function_specifier_opt (parser, decl_specs);
12857 break;
12859 /* decl-specifier:
12860 typedef */
12861 case RID_TYPEDEF:
12862 ds = ds_typedef;
12863 /* Consume the token. */
12864 cp_lexer_consume_token (parser->lexer);
12865 /* A constructor declarator cannot appear in a typedef. */
12866 constructor_possible_p = false;
12867 /* The "typedef" keyword can only occur in a declaration; we
12868 may as well commit at this point. */
12869 cp_parser_commit_to_tentative_parse (parser);
12871 if (decl_specs->storage_class != sc_none)
12872 decl_specs->conflicting_specifiers_p = true;
12873 break;
12875 /* storage-class-specifier:
12876 auto
12877 register
12878 static
12879 extern
12880 mutable
12882 GNU Extension:
12883 thread */
12884 case RID_AUTO:
12885 if (cxx_dialect == cxx98)
12887 /* Consume the token. */
12888 cp_lexer_consume_token (parser->lexer);
12890 /* Complain about `auto' as a storage specifier, if
12891 we're complaining about C++0x compatibility. */
12892 warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
12893 " changes meaning in C++11; please remove it");
12895 /* Set the storage class anyway. */
12896 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
12897 token);
12899 else
12900 /* C++0x auto type-specifier. */
12901 found_decl_spec = false;
12902 break;
12904 case RID_REGISTER:
12905 case RID_STATIC:
12906 case RID_EXTERN:
12907 case RID_MUTABLE:
12908 /* Consume the token. */
12909 cp_lexer_consume_token (parser->lexer);
12910 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
12911 token);
12912 break;
12913 case RID_THREAD:
12914 /* Consume the token. */
12915 ds = ds_thread;
12916 cp_lexer_consume_token (parser->lexer);
12917 break;
12919 default:
12920 /* We did not yet find a decl-specifier yet. */
12921 found_decl_spec = false;
12922 break;
12925 if (found_decl_spec
12926 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
12927 && token->keyword != RID_CONSTEXPR)
12928 error ("decl-specifier invalid in condition");
12930 if (found_decl_spec
12931 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
12932 && token->keyword != RID_MUTABLE
12933 && token->keyword != RID_CONSTEXPR)
12934 error_at (token->location, "%qD invalid in lambda",
12935 ridpointers[token->keyword]);
12937 if (ds != ds_last)
12938 set_and_check_decl_spec_loc (decl_specs, ds, token);
12940 /* Constructors are a special case. The `S' in `S()' is not a
12941 decl-specifier; it is the beginning of the declarator. */
12942 constructor_p
12943 = (!found_decl_spec
12944 && constructor_possible_p
12945 && (cp_parser_constructor_declarator_p
12946 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
12948 /* If we don't have a DECL_SPEC yet, then we must be looking at
12949 a type-specifier. */
12950 if (!found_decl_spec && !constructor_p)
12952 int decl_spec_declares_class_or_enum;
12953 bool is_cv_qualifier;
12954 tree type_spec;
12956 type_spec
12957 = cp_parser_type_specifier (parser, flags,
12958 decl_specs,
12959 /*is_declaration=*/true,
12960 &decl_spec_declares_class_or_enum,
12961 &is_cv_qualifier);
12962 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
12964 /* If this type-specifier referenced a user-defined type
12965 (a typedef, class-name, etc.), then we can't allow any
12966 more such type-specifiers henceforth.
12968 [dcl.spec]
12970 The longest sequence of decl-specifiers that could
12971 possibly be a type name is taken as the
12972 decl-specifier-seq of a declaration. The sequence shall
12973 be self-consistent as described below.
12975 [dcl.type]
12977 As a general rule, at most one type-specifier is allowed
12978 in the complete decl-specifier-seq of a declaration. The
12979 only exceptions are the following:
12981 -- const or volatile can be combined with any other
12982 type-specifier.
12984 -- signed or unsigned can be combined with char, long,
12985 short, or int.
12987 -- ..
12989 Example:
12991 typedef char* Pc;
12992 void g (const int Pc);
12994 Here, Pc is *not* part of the decl-specifier seq; it's
12995 the declarator. Therefore, once we see a type-specifier
12996 (other than a cv-qualifier), we forbid any additional
12997 user-defined types. We *do* still allow things like `int
12998 int' to be considered a decl-specifier-seq, and issue the
12999 error message later. */
13000 if (type_spec && !is_cv_qualifier)
13001 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13002 /* A constructor declarator cannot follow a type-specifier. */
13003 if (type_spec)
13005 constructor_possible_p = false;
13006 found_decl_spec = true;
13007 if (!is_cv_qualifier)
13008 decl_specs->any_type_specifiers_p = true;
13012 /* If we still do not have a DECL_SPEC, then there are no more
13013 decl-specifiers. */
13014 if (!found_decl_spec)
13015 break;
13017 decl_specs->any_specifiers_p = true;
13018 /* After we see one decl-specifier, further decl-specifiers are
13019 always optional. */
13020 flags |= CP_PARSER_FLAGS_OPTIONAL;
13023 /* Don't allow a friend specifier with a class definition. */
13024 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13025 && (*declares_class_or_enum & 2))
13026 error_at (decl_specs->locations[ds_friend],
13027 "class definition may not be declared a friend");
13030 /* Parse an (optional) storage-class-specifier.
13032 storage-class-specifier:
13033 auto
13034 register
13035 static
13036 extern
13037 mutable
13039 GNU Extension:
13041 storage-class-specifier:
13042 thread
13044 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13046 static tree
13047 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13049 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13051 case RID_AUTO:
13052 if (cxx_dialect != cxx98)
13053 return NULL_TREE;
13054 /* Fall through for C++98. */
13055 gcc_fallthrough ();
13057 case RID_REGISTER:
13058 case RID_STATIC:
13059 case RID_EXTERN:
13060 case RID_MUTABLE:
13061 case RID_THREAD:
13062 /* Consume the token. */
13063 return cp_lexer_consume_token (parser->lexer)->u.value;
13065 default:
13066 return NULL_TREE;
13070 /* Parse an (optional) function-specifier.
13072 function-specifier:
13073 inline
13074 virtual
13075 explicit
13077 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13078 Updates DECL_SPECS, if it is non-NULL. */
13080 static tree
13081 cp_parser_function_specifier_opt (cp_parser* parser,
13082 cp_decl_specifier_seq *decl_specs)
13084 cp_token *token = cp_lexer_peek_token (parser->lexer);
13085 switch (token->keyword)
13087 case RID_INLINE:
13088 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13089 break;
13091 case RID_VIRTUAL:
13092 /* 14.5.2.3 [temp.mem]
13094 A member function template shall not be virtual. */
13095 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13096 && current_class_type)
13097 error_at (token->location, "templates may not be %<virtual%>");
13098 else
13099 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13100 break;
13102 case RID_EXPLICIT:
13103 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13104 break;
13106 default:
13107 return NULL_TREE;
13110 /* Consume the token. */
13111 return cp_lexer_consume_token (parser->lexer)->u.value;
13114 /* Parse a linkage-specification.
13116 linkage-specification:
13117 extern string-literal { declaration-seq [opt] }
13118 extern string-literal declaration */
13120 static void
13121 cp_parser_linkage_specification (cp_parser* parser)
13123 tree linkage;
13125 /* Look for the `extern' keyword. */
13126 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13128 /* Look for the string-literal. */
13129 linkage = cp_parser_string_literal (parser, false, false);
13131 /* Transform the literal into an identifier. If the literal is a
13132 wide-character string, or contains embedded NULs, then we can't
13133 handle it as the user wants. */
13134 if (strlen (TREE_STRING_POINTER (linkage))
13135 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13137 cp_parser_error (parser, "invalid linkage-specification");
13138 /* Assume C++ linkage. */
13139 linkage = lang_name_cplusplus;
13141 else
13142 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13144 /* We're now using the new linkage. */
13145 push_lang_context (linkage);
13147 /* If the next token is a `{', then we're using the first
13148 production. */
13149 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13151 cp_ensure_no_omp_declare_simd (parser);
13152 cp_ensure_no_oacc_routine (parser);
13154 /* Consume the `{' token. */
13155 cp_lexer_consume_token (parser->lexer);
13156 /* Parse the declarations. */
13157 cp_parser_declaration_seq_opt (parser);
13158 /* Look for the closing `}'. */
13159 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13161 /* Otherwise, there's just one declaration. */
13162 else
13164 bool saved_in_unbraced_linkage_specification_p;
13166 saved_in_unbraced_linkage_specification_p
13167 = parser->in_unbraced_linkage_specification_p;
13168 parser->in_unbraced_linkage_specification_p = true;
13169 cp_parser_declaration (parser);
13170 parser->in_unbraced_linkage_specification_p
13171 = saved_in_unbraced_linkage_specification_p;
13174 /* We're done with the linkage-specification. */
13175 pop_lang_context ();
13178 /* Parse a static_assert-declaration.
13180 static_assert-declaration:
13181 static_assert ( constant-expression , string-literal ) ;
13182 static_assert ( constant-expression ) ; (C++1Z)
13184 If MEMBER_P, this static_assert is a class member. */
13186 static void
13187 cp_parser_static_assert(cp_parser *parser, bool member_p)
13189 tree condition;
13190 tree message;
13191 cp_token *token;
13192 location_t saved_loc;
13193 bool dummy;
13195 /* Peek at the `static_assert' token so we can keep track of exactly
13196 where the static assertion started. */
13197 token = cp_lexer_peek_token (parser->lexer);
13198 saved_loc = token->location;
13200 /* Look for the `static_assert' keyword. */
13201 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13202 RT_STATIC_ASSERT))
13203 return;
13205 /* We know we are in a static assertion; commit to any tentative
13206 parse. */
13207 if (cp_parser_parsing_tentatively (parser))
13208 cp_parser_commit_to_tentative_parse (parser);
13210 /* Parse the `(' starting the static assertion condition. */
13211 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
13213 /* Parse the constant-expression. Allow a non-constant expression
13214 here in order to give better diagnostics in finish_static_assert. */
13215 condition =
13216 cp_parser_constant_expression (parser,
13217 /*allow_non_constant_p=*/true,
13218 /*non_constant_p=*/&dummy);
13220 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13222 if (cxx_dialect < cxx1z)
13223 pedwarn (input_location, OPT_Wpedantic,
13224 "static_assert without a message "
13225 "only available with -std=c++1z or -std=gnu++1z");
13226 /* Eat the ')' */
13227 cp_lexer_consume_token (parser->lexer);
13228 message = build_string (1, "");
13229 TREE_TYPE (message) = char_array_type_node;
13230 fix_string_type (message);
13232 else
13234 /* Parse the separating `,'. */
13235 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13237 /* Parse the string-literal message. */
13238 message = cp_parser_string_literal (parser,
13239 /*translate=*/false,
13240 /*wide_ok=*/true);
13242 /* A `)' completes the static assertion. */
13243 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13244 cp_parser_skip_to_closing_parenthesis (parser,
13245 /*recovering=*/true,
13246 /*or_comma=*/false,
13247 /*consume_paren=*/true);
13250 /* A semicolon terminates the declaration. */
13251 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13253 /* Complete the static assertion, which may mean either processing
13254 the static assert now or saving it for template instantiation. */
13255 finish_static_assert (condition, message, saved_loc, member_p);
13258 /* Parse the expression in decltype ( expression ). */
13260 static tree
13261 cp_parser_decltype_expr (cp_parser *parser,
13262 bool &id_expression_or_member_access_p)
13264 cp_token *id_expr_start_token;
13265 tree expr;
13267 /* Since we're going to preserve any side-effects from this parse, set up a
13268 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13269 in the expression. */
13270 tentative_firewall firewall (parser);
13272 /* First, try parsing an id-expression. */
13273 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13274 cp_parser_parse_tentatively (parser);
13275 expr = cp_parser_id_expression (parser,
13276 /*template_keyword_p=*/false,
13277 /*check_dependency_p=*/true,
13278 /*template_p=*/NULL,
13279 /*declarator_p=*/false,
13280 /*optional_p=*/false);
13282 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13284 bool non_integral_constant_expression_p = false;
13285 tree id_expression = expr;
13286 cp_id_kind idk;
13287 const char *error_msg;
13289 if (identifier_p (expr))
13290 /* Lookup the name we got back from the id-expression. */
13291 expr = cp_parser_lookup_name_simple (parser, expr,
13292 id_expr_start_token->location);
13294 if (expr
13295 && expr != error_mark_node
13296 && TREE_CODE (expr) != TYPE_DECL
13297 && (TREE_CODE (expr) != BIT_NOT_EXPR
13298 || !TYPE_P (TREE_OPERAND (expr, 0)))
13299 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13301 /* Complete lookup of the id-expression. */
13302 expr = (finish_id_expression
13303 (id_expression, expr, parser->scope, &idk,
13304 /*integral_constant_expression_p=*/false,
13305 /*allow_non_integral_constant_expression_p=*/true,
13306 &non_integral_constant_expression_p,
13307 /*template_p=*/false,
13308 /*done=*/true,
13309 /*address_p=*/false,
13310 /*template_arg_p=*/false,
13311 &error_msg,
13312 id_expr_start_token->location));
13314 if (expr == error_mark_node)
13315 /* We found an id-expression, but it was something that we
13316 should not have found. This is an error, not something
13317 we can recover from, so note that we found an
13318 id-expression and we'll recover as gracefully as
13319 possible. */
13320 id_expression_or_member_access_p = true;
13323 if (expr
13324 && expr != error_mark_node
13325 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13326 /* We have an id-expression. */
13327 id_expression_or_member_access_p = true;
13330 if (!id_expression_or_member_access_p)
13332 /* Abort the id-expression parse. */
13333 cp_parser_abort_tentative_parse (parser);
13335 /* Parsing tentatively, again. */
13336 cp_parser_parse_tentatively (parser);
13338 /* Parse a class member access. */
13339 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13340 /*cast_p=*/false, /*decltype*/true,
13341 /*member_access_only_p=*/true, NULL);
13343 if (expr
13344 && expr != error_mark_node
13345 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13346 /* We have an id-expression. */
13347 id_expression_or_member_access_p = true;
13350 if (id_expression_or_member_access_p)
13351 /* We have parsed the complete id-expression or member access. */
13352 cp_parser_parse_definitely (parser);
13353 else
13355 /* Abort our attempt to parse an id-expression or member access
13356 expression. */
13357 cp_parser_abort_tentative_parse (parser);
13359 /* Parse a full expression. */
13360 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
13361 /*decltype_p=*/true);
13364 return expr;
13367 /* Parse a `decltype' type. Returns the type.
13369 simple-type-specifier:
13370 decltype ( expression )
13371 C++14 proposal:
13372 decltype ( auto ) */
13374 static tree
13375 cp_parser_decltype (cp_parser *parser)
13377 tree expr;
13378 bool id_expression_or_member_access_p = false;
13379 const char *saved_message;
13380 bool saved_integral_constant_expression_p;
13381 bool saved_non_integral_constant_expression_p;
13382 bool saved_greater_than_is_operator_p;
13383 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13385 if (start_token->type == CPP_DECLTYPE)
13387 /* Already parsed. */
13388 cp_lexer_consume_token (parser->lexer);
13389 return saved_checks_value (start_token->u.tree_check_value);
13392 /* Look for the `decltype' token. */
13393 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
13394 return error_mark_node;
13396 /* Parse the opening `('. */
13397 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13398 return error_mark_node;
13400 /* decltype (auto) */
13401 if (cxx_dialect >= cxx14
13402 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
13404 cp_lexer_consume_token (parser->lexer);
13405 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13406 return error_mark_node;
13407 expr = make_decltype_auto ();
13408 AUTO_IS_DECLTYPE (expr) = true;
13409 goto rewrite;
13412 /* Types cannot be defined in a `decltype' expression. Save away the
13413 old message. */
13414 saved_message = parser->type_definition_forbidden_message;
13416 /* And create the new one. */
13417 parser->type_definition_forbidden_message
13418 = G_("types may not be defined in %<decltype%> expressions");
13420 /* The restrictions on constant-expressions do not apply inside
13421 decltype expressions. */
13422 saved_integral_constant_expression_p
13423 = parser->integral_constant_expression_p;
13424 saved_non_integral_constant_expression_p
13425 = parser->non_integral_constant_expression_p;
13426 parser->integral_constant_expression_p = false;
13428 /* Within a parenthesized expression, a `>' token is always
13429 the greater-than operator. */
13430 saved_greater_than_is_operator_p
13431 = parser->greater_than_is_operator_p;
13432 parser->greater_than_is_operator_p = true;
13434 /* Do not actually evaluate the expression. */
13435 ++cp_unevaluated_operand;
13437 /* Do not warn about problems with the expression. */
13438 ++c_inhibit_evaluation_warnings;
13440 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
13442 /* Go back to evaluating expressions. */
13443 --cp_unevaluated_operand;
13444 --c_inhibit_evaluation_warnings;
13446 /* The `>' token might be the end of a template-id or
13447 template-parameter-list now. */
13448 parser->greater_than_is_operator_p
13449 = saved_greater_than_is_operator_p;
13451 /* Restore the old message and the integral constant expression
13452 flags. */
13453 parser->type_definition_forbidden_message = saved_message;
13454 parser->integral_constant_expression_p
13455 = saved_integral_constant_expression_p;
13456 parser->non_integral_constant_expression_p
13457 = saved_non_integral_constant_expression_p;
13459 /* Parse to the closing `)'. */
13460 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13462 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13463 /*consume_paren=*/true);
13464 return error_mark_node;
13467 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
13468 tf_warning_or_error);
13470 rewrite:
13471 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
13472 it again. */
13473 start_token->type = CPP_DECLTYPE;
13474 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13475 start_token->u.tree_check_value->value = expr;
13476 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
13477 start_token->keyword = RID_MAX;
13478 cp_lexer_purge_tokens_after (parser->lexer, start_token);
13480 return expr;
13483 /* Special member functions [gram.special] */
13485 /* Parse a conversion-function-id.
13487 conversion-function-id:
13488 operator conversion-type-id
13490 Returns an IDENTIFIER_NODE representing the operator. */
13492 static tree
13493 cp_parser_conversion_function_id (cp_parser* parser)
13495 tree type;
13496 tree saved_scope;
13497 tree saved_qualifying_scope;
13498 tree saved_object_scope;
13499 tree pushed_scope = NULL_TREE;
13501 /* Look for the `operator' token. */
13502 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13503 return error_mark_node;
13504 /* When we parse the conversion-type-id, the current scope will be
13505 reset. However, we need that information in able to look up the
13506 conversion function later, so we save it here. */
13507 saved_scope = parser->scope;
13508 saved_qualifying_scope = parser->qualifying_scope;
13509 saved_object_scope = parser->object_scope;
13510 /* We must enter the scope of the class so that the names of
13511 entities declared within the class are available in the
13512 conversion-type-id. For example, consider:
13514 struct S {
13515 typedef int I;
13516 operator I();
13519 S::operator I() { ... }
13521 In order to see that `I' is a type-name in the definition, we
13522 must be in the scope of `S'. */
13523 if (saved_scope)
13524 pushed_scope = push_scope (saved_scope);
13525 /* Parse the conversion-type-id. */
13526 type = cp_parser_conversion_type_id (parser);
13527 /* Leave the scope of the class, if any. */
13528 if (pushed_scope)
13529 pop_scope (pushed_scope);
13530 /* Restore the saved scope. */
13531 parser->scope = saved_scope;
13532 parser->qualifying_scope = saved_qualifying_scope;
13533 parser->object_scope = saved_object_scope;
13534 /* If the TYPE is invalid, indicate failure. */
13535 if (type == error_mark_node)
13536 return error_mark_node;
13537 return mangle_conv_op_name_for_type (type);
13540 /* Parse a conversion-type-id:
13542 conversion-type-id:
13543 type-specifier-seq conversion-declarator [opt]
13545 Returns the TYPE specified. */
13547 static tree
13548 cp_parser_conversion_type_id (cp_parser* parser)
13550 tree attributes;
13551 cp_decl_specifier_seq type_specifiers;
13552 cp_declarator *declarator;
13553 tree type_specified;
13554 const char *saved_message;
13556 /* Parse the attributes. */
13557 attributes = cp_parser_attributes_opt (parser);
13559 saved_message = parser->type_definition_forbidden_message;
13560 parser->type_definition_forbidden_message
13561 = G_("types may not be defined in a conversion-type-id");
13563 /* Parse the type-specifiers. */
13564 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13565 /*is_trailing_return=*/false,
13566 &type_specifiers);
13568 parser->type_definition_forbidden_message = saved_message;
13570 /* If that didn't work, stop. */
13571 if (type_specifiers.type == error_mark_node)
13572 return error_mark_node;
13573 /* Parse the conversion-declarator. */
13574 declarator = cp_parser_conversion_declarator_opt (parser);
13576 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
13577 /*initialized=*/0, &attributes);
13578 if (attributes)
13579 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
13581 /* Don't give this error when parsing tentatively. This happens to
13582 work because we always parse this definitively once. */
13583 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
13584 && type_uses_auto (type_specified))
13586 if (cxx_dialect < cxx14)
13588 error ("invalid use of %<auto%> in conversion operator");
13589 return error_mark_node;
13591 else if (template_parm_scope_p ())
13592 warning (0, "use of %<auto%> in member template "
13593 "conversion operator can never be deduced");
13596 return type_specified;
13599 /* Parse an (optional) conversion-declarator.
13601 conversion-declarator:
13602 ptr-operator conversion-declarator [opt]
13606 static cp_declarator *
13607 cp_parser_conversion_declarator_opt (cp_parser* parser)
13609 enum tree_code code;
13610 tree class_type, std_attributes = NULL_TREE;
13611 cp_cv_quals cv_quals;
13613 /* We don't know if there's a ptr-operator next, or not. */
13614 cp_parser_parse_tentatively (parser);
13615 /* Try the ptr-operator. */
13616 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
13617 &std_attributes);
13618 /* If it worked, look for more conversion-declarators. */
13619 if (cp_parser_parse_definitely (parser))
13621 cp_declarator *declarator;
13623 /* Parse another optional declarator. */
13624 declarator = cp_parser_conversion_declarator_opt (parser);
13626 declarator = cp_parser_make_indirect_declarator
13627 (code, class_type, cv_quals, declarator, std_attributes);
13629 return declarator;
13632 return NULL;
13635 /* Parse an (optional) ctor-initializer.
13637 ctor-initializer:
13638 : mem-initializer-list
13640 Returns TRUE iff the ctor-initializer was actually present. */
13642 static bool
13643 cp_parser_ctor_initializer_opt (cp_parser* parser)
13645 /* If the next token is not a `:', then there is no
13646 ctor-initializer. */
13647 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13649 /* Do default initialization of any bases and members. */
13650 if (DECL_CONSTRUCTOR_P (current_function_decl))
13651 finish_mem_initializers (NULL_TREE);
13653 return false;
13656 /* Consume the `:' token. */
13657 cp_lexer_consume_token (parser->lexer);
13658 /* And the mem-initializer-list. */
13659 cp_parser_mem_initializer_list (parser);
13661 return true;
13664 /* Parse a mem-initializer-list.
13666 mem-initializer-list:
13667 mem-initializer ... [opt]
13668 mem-initializer ... [opt] , mem-initializer-list */
13670 static void
13671 cp_parser_mem_initializer_list (cp_parser* parser)
13673 tree mem_initializer_list = NULL_TREE;
13674 tree target_ctor = error_mark_node;
13675 cp_token *token = cp_lexer_peek_token (parser->lexer);
13677 /* Let the semantic analysis code know that we are starting the
13678 mem-initializer-list. */
13679 if (!DECL_CONSTRUCTOR_P (current_function_decl))
13680 error_at (token->location,
13681 "only constructors take member initializers");
13683 /* Loop through the list. */
13684 while (true)
13686 tree mem_initializer;
13688 token = cp_lexer_peek_token (parser->lexer);
13689 /* Parse the mem-initializer. */
13690 mem_initializer = cp_parser_mem_initializer (parser);
13691 /* If the next token is a `...', we're expanding member initializers. */
13692 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13694 /* Consume the `...'. */
13695 cp_lexer_consume_token (parser->lexer);
13697 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
13698 can be expanded but members cannot. */
13699 if (mem_initializer != error_mark_node
13700 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
13702 error_at (token->location,
13703 "cannot expand initializer for member %<%D%>",
13704 TREE_PURPOSE (mem_initializer));
13705 mem_initializer = error_mark_node;
13708 /* Construct the pack expansion type. */
13709 if (mem_initializer != error_mark_node)
13710 mem_initializer = make_pack_expansion (mem_initializer);
13712 if (target_ctor != error_mark_node
13713 && mem_initializer != error_mark_node)
13715 error ("mem-initializer for %qD follows constructor delegation",
13716 TREE_PURPOSE (mem_initializer));
13717 mem_initializer = error_mark_node;
13719 /* Look for a target constructor. */
13720 if (mem_initializer != error_mark_node
13721 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
13722 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
13724 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
13725 if (mem_initializer_list)
13727 error ("constructor delegation follows mem-initializer for %qD",
13728 TREE_PURPOSE (mem_initializer_list));
13729 mem_initializer = error_mark_node;
13731 target_ctor = mem_initializer;
13733 /* Add it to the list, unless it was erroneous. */
13734 if (mem_initializer != error_mark_node)
13736 TREE_CHAIN (mem_initializer) = mem_initializer_list;
13737 mem_initializer_list = mem_initializer;
13739 /* If the next token is not a `,', we're done. */
13740 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13741 break;
13742 /* Consume the `,' token. */
13743 cp_lexer_consume_token (parser->lexer);
13746 /* Perform semantic analysis. */
13747 if (DECL_CONSTRUCTOR_P (current_function_decl))
13748 finish_mem_initializers (mem_initializer_list);
13751 /* Parse a mem-initializer.
13753 mem-initializer:
13754 mem-initializer-id ( expression-list [opt] )
13755 mem-initializer-id braced-init-list
13757 GNU extension:
13759 mem-initializer:
13760 ( expression-list [opt] )
13762 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
13763 class) or FIELD_DECL (for a non-static data member) to initialize;
13764 the TREE_VALUE is the expression-list. An empty initialization
13765 list is represented by void_list_node. */
13767 static tree
13768 cp_parser_mem_initializer (cp_parser* parser)
13770 tree mem_initializer_id;
13771 tree expression_list;
13772 tree member;
13773 cp_token *token = cp_lexer_peek_token (parser->lexer);
13775 /* Find out what is being initialized. */
13776 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
13778 permerror (token->location,
13779 "anachronistic old-style base class initializer");
13780 mem_initializer_id = NULL_TREE;
13782 else
13784 mem_initializer_id = cp_parser_mem_initializer_id (parser);
13785 if (mem_initializer_id == error_mark_node)
13786 return mem_initializer_id;
13788 member = expand_member_init (mem_initializer_id);
13789 if (member && !DECL_P (member))
13790 in_base_initializer = 1;
13792 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13794 bool expr_non_constant_p;
13795 cp_lexer_set_source_position (parser->lexer);
13796 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13797 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
13798 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
13799 expression_list = build_tree_list (NULL_TREE, expression_list);
13801 else
13803 vec<tree, va_gc> *vec;
13804 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
13805 /*cast_p=*/false,
13806 /*allow_expansion_p=*/true,
13807 /*non_constant_p=*/NULL);
13808 if (vec == NULL)
13809 return error_mark_node;
13810 expression_list = build_tree_list_vec (vec);
13811 release_tree_vector (vec);
13814 if (expression_list == error_mark_node)
13815 return error_mark_node;
13816 if (!expression_list)
13817 expression_list = void_type_node;
13819 in_base_initializer = 0;
13821 return member ? build_tree_list (member, expression_list) : error_mark_node;
13824 /* Parse a mem-initializer-id.
13826 mem-initializer-id:
13827 :: [opt] nested-name-specifier [opt] class-name
13828 decltype-specifier (C++11)
13829 identifier
13831 Returns a TYPE indicating the class to be initialized for the first
13832 production (and the second in C++11). Returns an IDENTIFIER_NODE
13833 indicating the data member to be initialized for the last production. */
13835 static tree
13836 cp_parser_mem_initializer_id (cp_parser* parser)
13838 bool global_scope_p;
13839 bool nested_name_specifier_p;
13840 bool template_p = false;
13841 tree id;
13843 cp_token *token = cp_lexer_peek_token (parser->lexer);
13845 /* `typename' is not allowed in this context ([temp.res]). */
13846 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13848 error_at (token->location,
13849 "keyword %<typename%> not allowed in this context (a qualified "
13850 "member initializer is implicitly a type)");
13851 cp_lexer_consume_token (parser->lexer);
13853 /* Look for the optional `::' operator. */
13854 global_scope_p
13855 = (cp_parser_global_scope_opt (parser,
13856 /*current_scope_valid_p=*/false)
13857 != NULL_TREE);
13858 /* Look for the optional nested-name-specifier. The simplest way to
13859 implement:
13861 [temp.res]
13863 The keyword `typename' is not permitted in a base-specifier or
13864 mem-initializer; in these contexts a qualified name that
13865 depends on a template-parameter is implicitly assumed to be a
13866 type name.
13868 is to assume that we have seen the `typename' keyword at this
13869 point. */
13870 nested_name_specifier_p
13871 = (cp_parser_nested_name_specifier_opt (parser,
13872 /*typename_keyword_p=*/true,
13873 /*check_dependency_p=*/true,
13874 /*type_p=*/true,
13875 /*is_declaration=*/true)
13876 != NULL_TREE);
13877 if (nested_name_specifier_p)
13878 template_p = cp_parser_optional_template_keyword (parser);
13879 /* If there is a `::' operator or a nested-name-specifier, then we
13880 are definitely looking for a class-name. */
13881 if (global_scope_p || nested_name_specifier_p)
13882 return cp_parser_class_name (parser,
13883 /*typename_keyword_p=*/true,
13884 /*template_keyword_p=*/template_p,
13885 typename_type,
13886 /*check_dependency_p=*/true,
13887 /*class_head_p=*/false,
13888 /*is_declaration=*/true);
13889 /* Otherwise, we could also be looking for an ordinary identifier. */
13890 cp_parser_parse_tentatively (parser);
13891 if (cp_lexer_next_token_is_decltype (parser->lexer))
13892 /* Try a decltype-specifier. */
13893 id = cp_parser_decltype (parser);
13894 else
13895 /* Otherwise, try a class-name. */
13896 id = cp_parser_class_name (parser,
13897 /*typename_keyword_p=*/true,
13898 /*template_keyword_p=*/false,
13899 none_type,
13900 /*check_dependency_p=*/true,
13901 /*class_head_p=*/false,
13902 /*is_declaration=*/true);
13903 /* If we found one, we're done. */
13904 if (cp_parser_parse_definitely (parser))
13905 return id;
13906 /* Otherwise, look for an ordinary identifier. */
13907 return cp_parser_identifier (parser);
13910 /* Overloading [gram.over] */
13912 /* Parse an operator-function-id.
13914 operator-function-id:
13915 operator operator
13917 Returns an IDENTIFIER_NODE for the operator which is a
13918 human-readable spelling of the identifier, e.g., `operator +'. */
13920 static cp_expr
13921 cp_parser_operator_function_id (cp_parser* parser)
13923 /* Look for the `operator' keyword. */
13924 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13925 return error_mark_node;
13926 /* And then the name of the operator itself. */
13927 return cp_parser_operator (parser);
13930 /* Return an identifier node for a user-defined literal operator.
13931 The suffix identifier is chained to the operator name identifier. */
13933 static tree
13934 cp_literal_operator_id (const char* name)
13936 tree identifier;
13937 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
13938 + strlen (name) + 10);
13939 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
13940 identifier = get_identifier (buffer);
13942 return identifier;
13945 /* Parse an operator.
13947 operator:
13948 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
13949 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
13950 || ++ -- , ->* -> () []
13952 GNU Extensions:
13954 operator:
13955 <? >? <?= >?=
13957 Returns an IDENTIFIER_NODE for the operator which is a
13958 human-readable spelling of the identifier, e.g., `operator +'. */
13960 static cp_expr
13961 cp_parser_operator (cp_parser* parser)
13963 tree id = NULL_TREE;
13964 cp_token *token;
13965 bool utf8 = false;
13967 /* Peek at the next token. */
13968 token = cp_lexer_peek_token (parser->lexer);
13970 location_t start_loc = token->location;
13972 /* Figure out which operator we have. */
13973 switch (token->type)
13975 case CPP_KEYWORD:
13977 enum tree_code op;
13979 /* The keyword should be either `new' or `delete'. */
13980 if (token->keyword == RID_NEW)
13981 op = NEW_EXPR;
13982 else if (token->keyword == RID_DELETE)
13983 op = DELETE_EXPR;
13984 else
13985 break;
13987 /* Consume the `new' or `delete' token. */
13988 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
13990 /* Peek at the next token. */
13991 token = cp_lexer_peek_token (parser->lexer);
13992 /* If it's a `[' token then this is the array variant of the
13993 operator. */
13994 if (token->type == CPP_OPEN_SQUARE)
13996 /* Consume the `[' token. */
13997 cp_lexer_consume_token (parser->lexer);
13998 /* Look for the `]' token. */
13999 if (cp_token *close_token
14000 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14001 end_loc = close_token->location;
14002 id = ansi_opname (op == NEW_EXPR
14003 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
14005 /* Otherwise, we have the non-array variant. */
14006 else
14007 id = ansi_opname (op);
14009 location_t loc = make_location (start_loc, start_loc, end_loc);
14011 return cp_expr (id, loc);
14014 case CPP_PLUS:
14015 id = ansi_opname (PLUS_EXPR);
14016 break;
14018 case CPP_MINUS:
14019 id = ansi_opname (MINUS_EXPR);
14020 break;
14022 case CPP_MULT:
14023 id = ansi_opname (MULT_EXPR);
14024 break;
14026 case CPP_DIV:
14027 id = ansi_opname (TRUNC_DIV_EXPR);
14028 break;
14030 case CPP_MOD:
14031 id = ansi_opname (TRUNC_MOD_EXPR);
14032 break;
14034 case CPP_XOR:
14035 id = ansi_opname (BIT_XOR_EXPR);
14036 break;
14038 case CPP_AND:
14039 id = ansi_opname (BIT_AND_EXPR);
14040 break;
14042 case CPP_OR:
14043 id = ansi_opname (BIT_IOR_EXPR);
14044 break;
14046 case CPP_COMPL:
14047 id = ansi_opname (BIT_NOT_EXPR);
14048 break;
14050 case CPP_NOT:
14051 id = ansi_opname (TRUTH_NOT_EXPR);
14052 break;
14054 case CPP_EQ:
14055 id = ansi_assopname (NOP_EXPR);
14056 break;
14058 case CPP_LESS:
14059 id = ansi_opname (LT_EXPR);
14060 break;
14062 case CPP_GREATER:
14063 id = ansi_opname (GT_EXPR);
14064 break;
14066 case CPP_PLUS_EQ:
14067 id = ansi_assopname (PLUS_EXPR);
14068 break;
14070 case CPP_MINUS_EQ:
14071 id = ansi_assopname (MINUS_EXPR);
14072 break;
14074 case CPP_MULT_EQ:
14075 id = ansi_assopname (MULT_EXPR);
14076 break;
14078 case CPP_DIV_EQ:
14079 id = ansi_assopname (TRUNC_DIV_EXPR);
14080 break;
14082 case CPP_MOD_EQ:
14083 id = ansi_assopname (TRUNC_MOD_EXPR);
14084 break;
14086 case CPP_XOR_EQ:
14087 id = ansi_assopname (BIT_XOR_EXPR);
14088 break;
14090 case CPP_AND_EQ:
14091 id = ansi_assopname (BIT_AND_EXPR);
14092 break;
14094 case CPP_OR_EQ:
14095 id = ansi_assopname (BIT_IOR_EXPR);
14096 break;
14098 case CPP_LSHIFT:
14099 id = ansi_opname (LSHIFT_EXPR);
14100 break;
14102 case CPP_RSHIFT:
14103 id = ansi_opname (RSHIFT_EXPR);
14104 break;
14106 case CPP_LSHIFT_EQ:
14107 id = ansi_assopname (LSHIFT_EXPR);
14108 break;
14110 case CPP_RSHIFT_EQ:
14111 id = ansi_assopname (RSHIFT_EXPR);
14112 break;
14114 case CPP_EQ_EQ:
14115 id = ansi_opname (EQ_EXPR);
14116 break;
14118 case CPP_NOT_EQ:
14119 id = ansi_opname (NE_EXPR);
14120 break;
14122 case CPP_LESS_EQ:
14123 id = ansi_opname (LE_EXPR);
14124 break;
14126 case CPP_GREATER_EQ:
14127 id = ansi_opname (GE_EXPR);
14128 break;
14130 case CPP_AND_AND:
14131 id = ansi_opname (TRUTH_ANDIF_EXPR);
14132 break;
14134 case CPP_OR_OR:
14135 id = ansi_opname (TRUTH_ORIF_EXPR);
14136 break;
14138 case CPP_PLUS_PLUS:
14139 id = ansi_opname (POSTINCREMENT_EXPR);
14140 break;
14142 case CPP_MINUS_MINUS:
14143 id = ansi_opname (PREDECREMENT_EXPR);
14144 break;
14146 case CPP_COMMA:
14147 id = ansi_opname (COMPOUND_EXPR);
14148 break;
14150 case CPP_DEREF_STAR:
14151 id = ansi_opname (MEMBER_REF);
14152 break;
14154 case CPP_DEREF:
14155 id = ansi_opname (COMPONENT_REF);
14156 break;
14158 case CPP_OPEN_PAREN:
14159 /* Consume the `('. */
14160 cp_lexer_consume_token (parser->lexer);
14161 /* Look for the matching `)'. */
14162 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14163 return ansi_opname (CALL_EXPR);
14165 case CPP_OPEN_SQUARE:
14166 /* Consume the `['. */
14167 cp_lexer_consume_token (parser->lexer);
14168 /* Look for the matching `]'. */
14169 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14170 return ansi_opname (ARRAY_REF);
14172 case CPP_UTF8STRING:
14173 case CPP_UTF8STRING_USERDEF:
14174 utf8 = true;
14175 /* FALLTHRU */
14176 case CPP_STRING:
14177 case CPP_WSTRING:
14178 case CPP_STRING16:
14179 case CPP_STRING32:
14180 case CPP_STRING_USERDEF:
14181 case CPP_WSTRING_USERDEF:
14182 case CPP_STRING16_USERDEF:
14183 case CPP_STRING32_USERDEF:
14185 tree str, string_tree;
14186 int sz, len;
14188 if (cxx_dialect == cxx98)
14189 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14191 /* Consume the string. */
14192 str = cp_parser_string_literal (parser, /*translate=*/true,
14193 /*wide_ok=*/true, /*lookup_udlit=*/false);
14194 if (str == error_mark_node)
14195 return error_mark_node;
14196 else if (TREE_CODE (str) == USERDEF_LITERAL)
14198 string_tree = USERDEF_LITERAL_VALUE (str);
14199 id = USERDEF_LITERAL_SUFFIX_ID (str);
14201 else
14203 string_tree = str;
14204 /* Look for the suffix identifier. */
14205 token = cp_lexer_peek_token (parser->lexer);
14206 if (token->type == CPP_NAME)
14207 id = cp_parser_identifier (parser);
14208 else if (token->type == CPP_KEYWORD)
14210 error ("unexpected keyword;"
14211 " remove space between quotes and suffix identifier");
14212 return error_mark_node;
14214 else
14216 error ("expected suffix identifier");
14217 return error_mark_node;
14220 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14221 (TREE_TYPE (TREE_TYPE (string_tree))));
14222 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14223 if (len != 0)
14225 error ("expected empty string after %<operator%> keyword");
14226 return error_mark_node;
14228 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14229 != char_type_node)
14231 error ("invalid encoding prefix in literal operator");
14232 return error_mark_node;
14234 if (id != error_mark_node)
14236 const char *name = IDENTIFIER_POINTER (id);
14237 id = cp_literal_operator_id (name);
14239 return id;
14242 default:
14243 /* Anything else is an error. */
14244 break;
14247 /* If we have selected an identifier, we need to consume the
14248 operator token. */
14249 if (id)
14250 cp_lexer_consume_token (parser->lexer);
14251 /* Otherwise, no valid operator name was present. */
14252 else
14254 cp_parser_error (parser, "expected operator");
14255 id = error_mark_node;
14258 return cp_expr (id, start_loc);
14261 /* Parse a template-declaration.
14263 template-declaration:
14264 export [opt] template < template-parameter-list > declaration
14266 If MEMBER_P is TRUE, this template-declaration occurs within a
14267 class-specifier.
14269 The grammar rule given by the standard isn't correct. What
14270 is really meant is:
14272 template-declaration:
14273 export [opt] template-parameter-list-seq
14274 decl-specifier-seq [opt] init-declarator [opt] ;
14275 export [opt] template-parameter-list-seq
14276 function-definition
14278 template-parameter-list-seq:
14279 template-parameter-list-seq [opt]
14280 template < template-parameter-list >
14282 Concept Extensions:
14284 template-parameter-list-seq:
14285 template < template-parameter-list > requires-clause [opt]
14287 requires-clause:
14288 requires logical-or-expression */
14290 static void
14291 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14293 /* Check for `export'. */
14294 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14296 /* Consume the `export' token. */
14297 cp_lexer_consume_token (parser->lexer);
14298 /* Warn that we do not support `export'. */
14299 warning (0, "keyword %<export%> not implemented, and will be ignored");
14302 cp_parser_template_declaration_after_export (parser, member_p);
14305 /* Parse a template-parameter-list.
14307 template-parameter-list:
14308 template-parameter
14309 template-parameter-list , template-parameter
14311 Returns a TREE_LIST. Each node represents a template parameter.
14312 The nodes are connected via their TREE_CHAINs. */
14314 static tree
14315 cp_parser_template_parameter_list (cp_parser* parser)
14317 tree parameter_list = NULL_TREE;
14319 begin_template_parm_list ();
14321 /* The loop below parses the template parms. We first need to know
14322 the total number of template parms to be able to compute proper
14323 canonical types of each dependent type. So after the loop, when
14324 we know the total number of template parms,
14325 end_template_parm_list computes the proper canonical types and
14326 fixes up the dependent types accordingly. */
14327 while (true)
14329 tree parameter;
14330 bool is_non_type;
14331 bool is_parameter_pack;
14332 location_t parm_loc;
14334 /* Parse the template-parameter. */
14335 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
14336 parameter = cp_parser_template_parameter (parser,
14337 &is_non_type,
14338 &is_parameter_pack);
14339 /* Add it to the list. */
14340 if (parameter != error_mark_node)
14341 parameter_list = process_template_parm (parameter_list,
14342 parm_loc,
14343 parameter,
14344 is_non_type,
14345 is_parameter_pack);
14346 else
14348 tree err_parm = build_tree_list (parameter, parameter);
14349 parameter_list = chainon (parameter_list, err_parm);
14352 /* If the next token is not a `,', we're done. */
14353 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14354 break;
14355 /* Otherwise, consume the `,' token. */
14356 cp_lexer_consume_token (parser->lexer);
14359 return end_template_parm_list (parameter_list);
14362 /* Parse a introduction-list.
14364 introduction-list:
14365 introduced-parameter
14366 introduction-list , introduced-parameter
14368 introduced-parameter:
14369 ...[opt] identifier
14371 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
14372 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
14373 WILDCARD_DECL will also have DECL_NAME set and token location in
14374 DECL_SOURCE_LOCATION. */
14376 static tree
14377 cp_parser_introduction_list (cp_parser *parser)
14379 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
14381 while (true)
14383 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14384 if (is_pack)
14385 cp_lexer_consume_token (parser->lexer);
14387 /* Build placeholder. */
14388 tree parm = build_nt (WILDCARD_DECL);
14389 DECL_SOURCE_LOCATION (parm)
14390 = cp_lexer_peek_token (parser->lexer)->location;
14391 DECL_NAME (parm) = cp_parser_identifier (parser);
14392 WILDCARD_PACK_P (parm) = is_pack;
14393 vec_safe_push (introduction_vec, parm);
14395 /* If the next token is not a `,', we're done. */
14396 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14397 break;
14398 /* Otherwise, consume the `,' token. */
14399 cp_lexer_consume_token (parser->lexer);
14402 /* Convert the vec into a TREE_VEC. */
14403 tree introduction_list = make_tree_vec (introduction_vec->length ());
14404 unsigned int n;
14405 tree parm;
14406 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
14407 TREE_VEC_ELT (introduction_list, n) = parm;
14409 release_tree_vector (introduction_vec);
14410 return introduction_list;
14413 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
14414 is an abstract declarator. */
14416 static inline cp_declarator*
14417 get_id_declarator (cp_declarator *declarator)
14419 cp_declarator *d = declarator;
14420 while (d && d->kind != cdk_id)
14421 d = d->declarator;
14422 return d;
14425 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
14426 is an abstract declarator. */
14428 static inline tree
14429 get_unqualified_id (cp_declarator *declarator)
14431 declarator = get_id_declarator (declarator);
14432 if (declarator)
14433 return declarator->u.id.unqualified_name;
14434 else
14435 return NULL_TREE;
14438 /* Returns true if DECL represents a constrained-parameter. */
14440 static inline bool
14441 is_constrained_parameter (tree decl)
14443 return (decl
14444 && TREE_CODE (decl) == TYPE_DECL
14445 && CONSTRAINED_PARM_CONCEPT (decl)
14446 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
14449 /* Returns true if PARM declares a constrained-parameter. */
14451 static inline bool
14452 is_constrained_parameter (cp_parameter_declarator *parm)
14454 return is_constrained_parameter (parm->decl_specifiers.type);
14457 /* Check that the type parameter is only a declarator-id, and that its
14458 type is not cv-qualified. */
14460 bool
14461 cp_parser_check_constrained_type_parm (cp_parser *parser,
14462 cp_parameter_declarator *parm)
14464 if (!parm->declarator)
14465 return true;
14467 if (parm->declarator->kind != cdk_id)
14469 cp_parser_error (parser, "invalid constrained type parameter");
14470 return false;
14473 /* Don't allow cv-qualified type parameters. */
14474 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
14475 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
14477 cp_parser_error (parser, "cv-qualified type parameter");
14478 return false;
14481 return true;
14484 /* Finish parsing/processing a template type parameter and checking
14485 various restrictions. */
14487 static inline tree
14488 cp_parser_constrained_type_template_parm (cp_parser *parser,
14489 tree id,
14490 cp_parameter_declarator* parmdecl)
14492 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
14493 return finish_template_type_parm (class_type_node, id);
14494 else
14495 return error_mark_node;
14498 static tree
14499 finish_constrained_template_template_parm (tree proto, tree id)
14501 /* FIXME: This should probably be copied, and we may need to adjust
14502 the template parameter depths. */
14503 tree saved_parms = current_template_parms;
14504 begin_template_parm_list ();
14505 current_template_parms = DECL_TEMPLATE_PARMS (proto);
14506 end_template_parm_list ();
14508 tree parm = finish_template_template_parm (class_type_node, id);
14509 current_template_parms = saved_parms;
14511 return parm;
14514 /* Finish parsing/processing a template template parameter by borrowing
14515 the template parameter list from the prototype parameter. */
14517 static tree
14518 cp_parser_constrained_template_template_parm (cp_parser *parser,
14519 tree proto,
14520 tree id,
14521 cp_parameter_declarator *parmdecl)
14523 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
14524 return error_mark_node;
14525 return finish_constrained_template_template_parm (proto, id);
14528 /* Create a new non-type template parameter from the given PARM
14529 declarator. */
14531 static tree
14532 constrained_non_type_template_parm (bool *is_non_type,
14533 cp_parameter_declarator *parm)
14535 *is_non_type = true;
14536 cp_declarator *decl = parm->declarator;
14537 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
14538 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
14539 return grokdeclarator (decl, specs, TPARM, 0, NULL);
14542 /* Build a constrained template parameter based on the PARMDECL
14543 declarator. The type of PARMDECL is the constrained type, which
14544 refers to the prototype template parameter that ultimately
14545 specifies the type of the declared parameter. */
14547 static tree
14548 finish_constrained_parameter (cp_parser *parser,
14549 cp_parameter_declarator *parmdecl,
14550 bool *is_non_type,
14551 bool *is_parameter_pack)
14553 tree decl = parmdecl->decl_specifiers.type;
14554 tree id = get_unqualified_id (parmdecl->declarator);
14555 tree def = parmdecl->default_argument;
14556 tree proto = DECL_INITIAL (decl);
14558 /* A template parameter constrained by a variadic concept shall also
14559 be declared as a template parameter pack. */
14560 bool is_variadic = template_parameter_pack_p (proto);
14561 if (is_variadic && !*is_parameter_pack)
14562 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
14564 /* Build the parameter. Return an error if the declarator was invalid. */
14565 tree parm;
14566 if (TREE_CODE (proto) == TYPE_DECL)
14567 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
14568 else if (TREE_CODE (proto) == TEMPLATE_DECL)
14569 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
14570 parmdecl);
14571 else
14572 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
14573 if (parm == error_mark_node)
14574 return error_mark_node;
14576 /* Finish the parameter decl and create a node attaching the
14577 default argument and constraint. */
14578 parm = build_tree_list (def, parm);
14579 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
14581 return parm;
14584 /* Returns true if the parsed type actually represents the declaration
14585 of a type template-parameter. */
14587 static inline bool
14588 declares_constrained_type_template_parameter (tree type)
14590 return (is_constrained_parameter (type)
14591 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
14595 /* Returns true if the parsed type actually represents the declaration of
14596 a template template-parameter. */
14598 static bool
14599 declares_constrained_template_template_parameter (tree type)
14601 return (is_constrained_parameter (type)
14602 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
14605 /* Parse a default argument for a type template-parameter.
14606 Note that diagnostics are handled in cp_parser_template_parameter. */
14608 static tree
14609 cp_parser_default_type_template_argument (cp_parser *parser)
14611 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14613 /* Consume the `=' token. */
14614 cp_lexer_consume_token (parser->lexer);
14616 cp_token *token = cp_lexer_peek_token (parser->lexer);
14618 /* Parse the default-argument. */
14619 push_deferring_access_checks (dk_no_deferred);
14620 tree default_argument = cp_parser_type_id (parser);
14621 pop_deferring_access_checks ();
14623 if (flag_concepts && type_uses_auto (default_argument))
14625 error_at (token->location,
14626 "invalid use of %<auto%> in default template argument");
14627 return error_mark_node;
14630 return default_argument;
14633 /* Parse a default argument for a template template-parameter. */
14635 static tree
14636 cp_parser_default_template_template_argument (cp_parser *parser)
14638 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14640 bool is_template;
14642 /* Consume the `='. */
14643 cp_lexer_consume_token (parser->lexer);
14644 /* Parse the id-expression. */
14645 push_deferring_access_checks (dk_no_deferred);
14646 /* save token before parsing the id-expression, for error
14647 reporting */
14648 const cp_token* token = cp_lexer_peek_token (parser->lexer);
14649 tree default_argument
14650 = cp_parser_id_expression (parser,
14651 /*template_keyword_p=*/false,
14652 /*check_dependency_p=*/true,
14653 /*template_p=*/&is_template,
14654 /*declarator_p=*/false,
14655 /*optional_p=*/false);
14656 if (TREE_CODE (default_argument) == TYPE_DECL)
14657 /* If the id-expression was a template-id that refers to
14658 a template-class, we already have the declaration here,
14659 so no further lookup is needed. */
14661 else
14662 /* Look up the name. */
14663 default_argument
14664 = cp_parser_lookup_name (parser, default_argument,
14665 none_type,
14666 /*is_template=*/is_template,
14667 /*is_namespace=*/false,
14668 /*check_dependency=*/true,
14669 /*ambiguous_decls=*/NULL,
14670 token->location);
14671 /* See if the default argument is valid. */
14672 default_argument = check_template_template_default_arg (default_argument);
14673 pop_deferring_access_checks ();
14674 return default_argument;
14677 /* Parse a template-parameter.
14679 template-parameter:
14680 type-parameter
14681 parameter-declaration
14683 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
14684 the parameter. The TREE_PURPOSE is the default value, if any.
14685 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
14686 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
14687 set to true iff this parameter is a parameter pack. */
14689 static tree
14690 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
14691 bool *is_parameter_pack)
14693 cp_token *token;
14694 cp_parameter_declarator *parameter_declarator;
14695 tree parm;
14697 /* Assume it is a type parameter or a template parameter. */
14698 *is_non_type = false;
14699 /* Assume it not a parameter pack. */
14700 *is_parameter_pack = false;
14701 /* Peek at the next token. */
14702 token = cp_lexer_peek_token (parser->lexer);
14703 /* If it is `class' or `template', we have a type-parameter. */
14704 if (token->keyword == RID_TEMPLATE)
14705 return cp_parser_type_parameter (parser, is_parameter_pack);
14706 /* If it is `class' or `typename' we do not know yet whether it is a
14707 type parameter or a non-type parameter. Consider:
14709 template <typename T, typename T::X X> ...
14713 template <class C, class D*> ...
14715 Here, the first parameter is a type parameter, and the second is
14716 a non-type parameter. We can tell by looking at the token after
14717 the identifier -- if it is a `,', `=', or `>' then we have a type
14718 parameter. */
14719 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
14721 /* Peek at the token after `class' or `typename'. */
14722 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14723 /* If it's an ellipsis, we have a template type parameter
14724 pack. */
14725 if (token->type == CPP_ELLIPSIS)
14726 return cp_parser_type_parameter (parser, is_parameter_pack);
14727 /* If it's an identifier, skip it. */
14728 if (token->type == CPP_NAME)
14729 token = cp_lexer_peek_nth_token (parser->lexer, 3);
14730 /* Now, see if the token looks like the end of a template
14731 parameter. */
14732 if (token->type == CPP_COMMA
14733 || token->type == CPP_EQ
14734 || token->type == CPP_GREATER)
14735 return cp_parser_type_parameter (parser, is_parameter_pack);
14738 /* Otherwise, it is a non-type parameter or a constrained parameter.
14740 [temp.param]
14742 When parsing a default template-argument for a non-type
14743 template-parameter, the first non-nested `>' is taken as the end
14744 of the template parameter-list rather than a greater-than
14745 operator. */
14746 parameter_declarator
14747 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
14748 /*parenthesized_p=*/NULL);
14750 if (!parameter_declarator)
14751 return error_mark_node;
14753 /* If the parameter declaration is marked as a parameter pack, set
14754 *IS_PARAMETER_PACK to notify the caller. */
14755 if (parameter_declarator->template_parameter_pack_p)
14756 *is_parameter_pack = true;
14758 if (parameter_declarator->default_argument)
14760 /* Can happen in some cases of erroneous input (c++/34892). */
14761 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14762 /* Consume the `...' for better error recovery. */
14763 cp_lexer_consume_token (parser->lexer);
14766 // The parameter may have been constrained.
14767 if (is_constrained_parameter (parameter_declarator))
14768 return finish_constrained_parameter (parser,
14769 parameter_declarator,
14770 is_non_type,
14771 is_parameter_pack);
14773 // Now we're sure that the parameter is a non-type parameter.
14774 *is_non_type = true;
14776 parm = grokdeclarator (parameter_declarator->declarator,
14777 &parameter_declarator->decl_specifiers,
14778 TPARM, /*initialized=*/0,
14779 /*attrlist=*/NULL);
14780 if (parm == error_mark_node)
14781 return error_mark_node;
14783 return build_tree_list (parameter_declarator->default_argument, parm);
14786 /* Parse a type-parameter.
14788 type-parameter:
14789 class identifier [opt]
14790 class identifier [opt] = type-id
14791 typename identifier [opt]
14792 typename identifier [opt] = type-id
14793 template < template-parameter-list > class identifier [opt]
14794 template < template-parameter-list > class identifier [opt]
14795 = id-expression
14797 GNU Extension (variadic templates):
14799 type-parameter:
14800 class ... identifier [opt]
14801 typename ... identifier [opt]
14803 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
14804 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
14805 the declaration of the parameter.
14807 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
14809 static tree
14810 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
14812 cp_token *token;
14813 tree parameter;
14815 /* Look for a keyword to tell us what kind of parameter this is. */
14816 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
14817 if (!token)
14818 return error_mark_node;
14820 switch (token->keyword)
14822 case RID_CLASS:
14823 case RID_TYPENAME:
14825 tree identifier;
14826 tree default_argument;
14828 /* If the next token is an ellipsis, we have a template
14829 argument pack. */
14830 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14832 /* Consume the `...' token. */
14833 cp_lexer_consume_token (parser->lexer);
14834 maybe_warn_variadic_templates ();
14836 *is_parameter_pack = true;
14839 /* If the next token is an identifier, then it names the
14840 parameter. */
14841 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14842 identifier = cp_parser_identifier (parser);
14843 else
14844 identifier = NULL_TREE;
14846 /* Create the parameter. */
14847 parameter = finish_template_type_parm (class_type_node, identifier);
14849 /* If the next token is an `=', we have a default argument. */
14850 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14852 default_argument
14853 = cp_parser_default_type_template_argument (parser);
14855 /* Template parameter packs cannot have default
14856 arguments. */
14857 if (*is_parameter_pack)
14859 if (identifier)
14860 error_at (token->location,
14861 "template parameter pack %qD cannot have a "
14862 "default argument", identifier);
14863 else
14864 error_at (token->location,
14865 "template parameter packs cannot have "
14866 "default arguments");
14867 default_argument = NULL_TREE;
14869 else if (check_for_bare_parameter_packs (default_argument))
14870 default_argument = error_mark_node;
14872 else
14873 default_argument = NULL_TREE;
14875 /* Create the combined representation of the parameter and the
14876 default argument. */
14877 parameter = build_tree_list (default_argument, parameter);
14879 break;
14881 case RID_TEMPLATE:
14883 tree identifier;
14884 tree default_argument;
14886 /* Look for the `<'. */
14887 cp_parser_require (parser, CPP_LESS, RT_LESS);
14888 /* Parse the template-parameter-list. */
14889 cp_parser_template_parameter_list (parser);
14890 /* Look for the `>'. */
14891 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14893 // If template requirements are present, parse them.
14894 if (flag_concepts)
14896 tree reqs = get_shorthand_constraints (current_template_parms);
14897 if (tree r = cp_parser_requires_clause_opt (parser))
14898 reqs = conjoin_constraints (reqs, normalize_expression (r));
14899 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
14902 /* Look for the `class' or 'typename' keywords. */
14903 cp_parser_type_parameter_key (parser);
14904 /* If the next token is an ellipsis, we have a template
14905 argument pack. */
14906 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14908 /* Consume the `...' token. */
14909 cp_lexer_consume_token (parser->lexer);
14910 maybe_warn_variadic_templates ();
14912 *is_parameter_pack = true;
14914 /* If the next token is an `=', then there is a
14915 default-argument. If the next token is a `>', we are at
14916 the end of the parameter-list. If the next token is a `,',
14917 then we are at the end of this parameter. */
14918 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
14919 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
14920 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14922 identifier = cp_parser_identifier (parser);
14923 /* Treat invalid names as if the parameter were nameless. */
14924 if (identifier == error_mark_node)
14925 identifier = NULL_TREE;
14927 else
14928 identifier = NULL_TREE;
14930 /* Create the template parameter. */
14931 parameter = finish_template_template_parm (class_type_node,
14932 identifier);
14934 /* If the next token is an `=', then there is a
14935 default-argument. */
14936 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14938 default_argument
14939 = cp_parser_default_template_template_argument (parser);
14941 /* Template parameter packs cannot have default
14942 arguments. */
14943 if (*is_parameter_pack)
14945 if (identifier)
14946 error_at (token->location,
14947 "template parameter pack %qD cannot "
14948 "have a default argument",
14949 identifier);
14950 else
14951 error_at (token->location, "template parameter packs cannot "
14952 "have default arguments");
14953 default_argument = NULL_TREE;
14956 else
14957 default_argument = NULL_TREE;
14959 /* Create the combined representation of the parameter and the
14960 default argument. */
14961 parameter = build_tree_list (default_argument, parameter);
14963 break;
14965 default:
14966 gcc_unreachable ();
14967 break;
14970 return parameter;
14973 /* Parse a template-id.
14975 template-id:
14976 template-name < template-argument-list [opt] >
14978 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
14979 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
14980 returned. Otherwise, if the template-name names a function, or set
14981 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
14982 names a class, returns a TYPE_DECL for the specialization.
14984 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
14985 uninstantiated templates. */
14987 static tree
14988 cp_parser_template_id (cp_parser *parser,
14989 bool template_keyword_p,
14990 bool check_dependency_p,
14991 enum tag_types tag_type,
14992 bool is_declaration)
14994 tree templ;
14995 tree arguments;
14996 tree template_id;
14997 cp_token_position start_of_id = 0;
14998 cp_token *next_token = NULL, *next_token_2 = NULL;
14999 bool is_identifier;
15001 /* If the next token corresponds to a template-id, there is no need
15002 to reparse it. */
15003 next_token = cp_lexer_peek_token (parser->lexer);
15004 if (next_token->type == CPP_TEMPLATE_ID)
15006 cp_lexer_consume_token (parser->lexer);
15007 return saved_checks_value (next_token->u.tree_check_value);
15010 /* Avoid performing name lookup if there is no possibility of
15011 finding a template-id. */
15012 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
15013 || (next_token->type == CPP_NAME
15014 && !cp_parser_nth_token_starts_template_argument_list_p
15015 (parser, 2)))
15017 cp_parser_error (parser, "expected template-id");
15018 return error_mark_node;
15021 /* Remember where the template-id starts. */
15022 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15023 start_of_id = cp_lexer_token_position (parser->lexer, false);
15025 push_deferring_access_checks (dk_deferred);
15027 /* Parse the template-name. */
15028 is_identifier = false;
15029 templ = cp_parser_template_name (parser, template_keyword_p,
15030 check_dependency_p,
15031 is_declaration,
15032 tag_type,
15033 &is_identifier);
15034 if (templ == error_mark_node || is_identifier)
15036 pop_deferring_access_checks ();
15037 return templ;
15040 /* Since we're going to preserve any side-effects from this parse, set up a
15041 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15042 in the template arguments. */
15043 tentative_firewall firewall (parser);
15045 /* If we find the sequence `[:' after a template-name, it's probably
15046 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15047 parse correctly the argument list. */
15048 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15049 == CPP_OPEN_SQUARE)
15050 && next_token->flags & DIGRAPH
15051 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15052 == CPP_COLON)
15053 && !(next_token_2->flags & PREV_WHITE))
15055 cp_parser_parse_tentatively (parser);
15056 /* Change `:' into `::'. */
15057 next_token_2->type = CPP_SCOPE;
15058 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15059 CPP_LESS. */
15060 cp_lexer_consume_token (parser->lexer);
15062 /* Parse the arguments. */
15063 arguments = cp_parser_enclosed_template_argument_list (parser);
15064 if (!cp_parser_parse_definitely (parser))
15066 /* If we couldn't parse an argument list, then we revert our changes
15067 and return simply an error. Maybe this is not a template-id
15068 after all. */
15069 next_token_2->type = CPP_COLON;
15070 cp_parser_error (parser, "expected %<<%>");
15071 pop_deferring_access_checks ();
15072 return error_mark_node;
15074 /* Otherwise, emit an error about the invalid digraph, but continue
15075 parsing because we got our argument list. */
15076 if (permerror (next_token->location,
15077 "%<<::%> cannot begin a template-argument list"))
15079 static bool hint = false;
15080 inform (next_token->location,
15081 "%<<:%> is an alternate spelling for %<[%>."
15082 " Insert whitespace between %<<%> and %<::%>");
15083 if (!hint && !flag_permissive)
15085 inform (next_token->location, "(if you use %<-fpermissive%> "
15086 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15087 "accept your code)");
15088 hint = true;
15092 else
15094 /* Look for the `<' that starts the template-argument-list. */
15095 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15097 pop_deferring_access_checks ();
15098 return error_mark_node;
15100 /* Parse the arguments. */
15101 arguments = cp_parser_enclosed_template_argument_list (parser);
15104 /* Build a representation of the specialization. */
15105 if (identifier_p (templ))
15106 template_id = build_min_nt_loc (next_token->location,
15107 TEMPLATE_ID_EXPR,
15108 templ, arguments);
15109 else if (DECL_TYPE_TEMPLATE_P (templ)
15110 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15112 bool entering_scope;
15113 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15114 template (rather than some instantiation thereof) only if
15115 is not nested within some other construct. For example, in
15116 "template <typename T> void f(T) { A<T>::", A<T> is just an
15117 instantiation of A. */
15118 entering_scope = (template_parm_scope_p ()
15119 && cp_lexer_next_token_is (parser->lexer,
15120 CPP_SCOPE));
15121 template_id
15122 = finish_template_type (templ, arguments, entering_scope);
15124 /* A template-like identifier may be a partial concept id. */
15125 else if (flag_concepts
15126 && (template_id = (cp_parser_maybe_partial_concept_id
15127 (parser, templ, arguments))))
15128 return template_id;
15129 else if (variable_template_p (templ))
15131 template_id = lookup_template_variable (templ, arguments);
15132 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15133 SET_EXPR_LOCATION (template_id, next_token->location);
15135 else
15137 /* If it's not a class-template or a template-template, it should be
15138 a function-template. */
15139 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15140 || TREE_CODE (templ) == OVERLOAD
15141 || BASELINK_P (templ)));
15143 template_id = lookup_template_function (templ, arguments);
15144 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15145 SET_EXPR_LOCATION (template_id, next_token->location);
15148 /* If parsing tentatively, replace the sequence of tokens that makes
15149 up the template-id with a CPP_TEMPLATE_ID token. That way,
15150 should we re-parse the token stream, we will not have to repeat
15151 the effort required to do the parse, nor will we issue duplicate
15152 error messages about problems during instantiation of the
15153 template. */
15154 if (start_of_id
15155 /* Don't do this if we had a parse error in a declarator; re-parsing
15156 might succeed if a name changes meaning (60361). */
15157 && !(cp_parser_error_occurred (parser)
15158 && cp_parser_parsing_tentatively (parser)
15159 && parser->in_declarator_p))
15161 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
15163 /* Reset the contents of the START_OF_ID token. */
15164 token->type = CPP_TEMPLATE_ID;
15166 /* Update the location to be of the form:
15167 template-name < template-argument-list [opt] >
15168 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15169 with caret == start at the start of the template-name,
15170 ranging until the closing '>'. */
15171 location_t finish_loc
15172 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15173 location_t combined_loc
15174 = make_location (token->location, token->location, finish_loc);
15175 token->location = combined_loc;
15177 /* Retrieve any deferred checks. Do not pop this access checks yet
15178 so the memory will not be reclaimed during token replacing below. */
15179 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15180 token->u.tree_check_value->value = template_id;
15181 token->u.tree_check_value->checks = get_deferred_access_checks ();
15182 token->keyword = RID_MAX;
15184 /* Purge all subsequent tokens. */
15185 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15187 /* ??? Can we actually assume that, if template_id ==
15188 error_mark_node, we will have issued a diagnostic to the
15189 user, as opposed to simply marking the tentative parse as
15190 failed? */
15191 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15192 error_at (token->location, "parse error in template argument list");
15195 pop_to_parent_deferring_access_checks ();
15196 return template_id;
15199 /* Parse a template-name.
15201 template-name:
15202 identifier
15204 The standard should actually say:
15206 template-name:
15207 identifier
15208 operator-function-id
15210 A defect report has been filed about this issue.
15212 A conversion-function-id cannot be a template name because they cannot
15213 be part of a template-id. In fact, looking at this code:
15215 a.operator K<int>()
15217 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15218 It is impossible to call a templated conversion-function-id with an
15219 explicit argument list, since the only allowed template parameter is
15220 the type to which it is converting.
15222 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15223 `template' keyword, in a construction like:
15225 T::template f<3>()
15227 In that case `f' is taken to be a template-name, even though there
15228 is no way of knowing for sure.
15230 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15231 name refers to a set of overloaded functions, at least one of which
15232 is a template, or an IDENTIFIER_NODE with the name of the template,
15233 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15234 names are looked up inside uninstantiated templates. */
15236 static tree
15237 cp_parser_template_name (cp_parser* parser,
15238 bool template_keyword_p,
15239 bool check_dependency_p,
15240 bool is_declaration,
15241 enum tag_types tag_type,
15242 bool *is_identifier)
15244 tree identifier;
15245 tree decl;
15246 tree fns;
15247 cp_token *token = cp_lexer_peek_token (parser->lexer);
15249 /* If the next token is `operator', then we have either an
15250 operator-function-id or a conversion-function-id. */
15251 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15253 /* We don't know whether we're looking at an
15254 operator-function-id or a conversion-function-id. */
15255 cp_parser_parse_tentatively (parser);
15256 /* Try an operator-function-id. */
15257 identifier = cp_parser_operator_function_id (parser);
15258 /* If that didn't work, try a conversion-function-id. */
15259 if (!cp_parser_parse_definitely (parser))
15261 cp_parser_error (parser, "expected template-name");
15262 return error_mark_node;
15265 /* Look for the identifier. */
15266 else
15267 identifier = cp_parser_identifier (parser);
15269 /* If we didn't find an identifier, we don't have a template-id. */
15270 if (identifier == error_mark_node)
15271 return error_mark_node;
15273 /* If the name immediately followed the `template' keyword, then it
15274 is a template-name. However, if the next token is not `<', then
15275 we do not treat it as a template-name, since it is not being used
15276 as part of a template-id. This enables us to handle constructs
15277 like:
15279 template <typename T> struct S { S(); };
15280 template <typename T> S<T>::S();
15282 correctly. We would treat `S' as a template -- if it were `S<T>'
15283 -- but we do not if there is no `<'. */
15285 if (processing_template_decl
15286 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15288 /* In a declaration, in a dependent context, we pretend that the
15289 "template" keyword was present in order to improve error
15290 recovery. For example, given:
15292 template <typename T> void f(T::X<int>);
15294 we want to treat "X<int>" as a template-id. */
15295 if (is_declaration
15296 && !template_keyword_p
15297 && parser->scope && TYPE_P (parser->scope)
15298 && check_dependency_p
15299 && dependent_scope_p (parser->scope)
15300 /* Do not do this for dtors (or ctors), since they never
15301 need the template keyword before their name. */
15302 && !constructor_name_p (identifier, parser->scope))
15304 cp_token_position start = 0;
15306 /* Explain what went wrong. */
15307 error_at (token->location, "non-template %qD used as template",
15308 identifier);
15309 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15310 parser->scope, identifier);
15311 /* If parsing tentatively, find the location of the "<" token. */
15312 if (cp_parser_simulate_error (parser))
15313 start = cp_lexer_token_position (parser->lexer, true);
15314 /* Parse the template arguments so that we can issue error
15315 messages about them. */
15316 cp_lexer_consume_token (parser->lexer);
15317 cp_parser_enclosed_template_argument_list (parser);
15318 /* Skip tokens until we find a good place from which to
15319 continue parsing. */
15320 cp_parser_skip_to_closing_parenthesis (parser,
15321 /*recovering=*/true,
15322 /*or_comma=*/true,
15323 /*consume_paren=*/false);
15324 /* If parsing tentatively, permanently remove the
15325 template argument list. That will prevent duplicate
15326 error messages from being issued about the missing
15327 "template" keyword. */
15328 if (start)
15329 cp_lexer_purge_tokens_after (parser->lexer, start);
15330 if (is_identifier)
15331 *is_identifier = true;
15332 return identifier;
15335 /* If the "template" keyword is present, then there is generally
15336 no point in doing name-lookup, so we just return IDENTIFIER.
15337 But, if the qualifying scope is non-dependent then we can
15338 (and must) do name-lookup normally. */
15339 if (template_keyword_p
15340 && (!parser->scope
15341 || (TYPE_P (parser->scope)
15342 && dependent_type_p (parser->scope))))
15343 return identifier;
15346 /* Look up the name. */
15347 decl = cp_parser_lookup_name (parser, identifier,
15348 tag_type,
15349 /*is_template=*/true,
15350 /*is_namespace=*/false,
15351 check_dependency_p,
15352 /*ambiguous_decls=*/NULL,
15353 token->location);
15355 decl = strip_using_decl (decl);
15357 /* If DECL is a template, then the name was a template-name. */
15358 if (TREE_CODE (decl) == TEMPLATE_DECL)
15360 if (TREE_DEPRECATED (decl)
15361 && deprecated_state != DEPRECATED_SUPPRESS)
15362 warn_deprecated_use (decl, NULL_TREE);
15364 else
15366 tree fn = NULL_TREE;
15368 /* The standard does not explicitly indicate whether a name that
15369 names a set of overloaded declarations, some of which are
15370 templates, is a template-name. However, such a name should
15371 be a template-name; otherwise, there is no way to form a
15372 template-id for the overloaded templates. */
15373 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
15374 if (TREE_CODE (fns) == OVERLOAD)
15375 for (fn = fns; fn; fn = OVL_NEXT (fn))
15376 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
15377 break;
15379 if (!fn)
15381 /* The name does not name a template. */
15382 cp_parser_error (parser, "expected template-name");
15383 return error_mark_node;
15387 /* If DECL is dependent, and refers to a function, then just return
15388 its name; we will look it up again during template instantiation. */
15389 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
15391 tree scope = ovl_scope (decl);
15392 if (TYPE_P (scope) && dependent_type_p (scope))
15393 return identifier;
15396 return decl;
15399 /* Parse a template-argument-list.
15401 template-argument-list:
15402 template-argument ... [opt]
15403 template-argument-list , template-argument ... [opt]
15405 Returns a TREE_VEC containing the arguments. */
15407 static tree
15408 cp_parser_template_argument_list (cp_parser* parser)
15410 tree fixed_args[10];
15411 unsigned n_args = 0;
15412 unsigned alloced = 10;
15413 tree *arg_ary = fixed_args;
15414 tree vec;
15415 bool saved_in_template_argument_list_p;
15416 bool saved_ice_p;
15417 bool saved_non_ice_p;
15419 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
15420 parser->in_template_argument_list_p = true;
15421 /* Even if the template-id appears in an integral
15422 constant-expression, the contents of the argument list do
15423 not. */
15424 saved_ice_p = parser->integral_constant_expression_p;
15425 parser->integral_constant_expression_p = false;
15426 saved_non_ice_p = parser->non_integral_constant_expression_p;
15427 parser->non_integral_constant_expression_p = false;
15429 /* Parse the arguments. */
15432 tree argument;
15434 if (n_args)
15435 /* Consume the comma. */
15436 cp_lexer_consume_token (parser->lexer);
15438 /* Parse the template-argument. */
15439 argument = cp_parser_template_argument (parser);
15441 /* If the next token is an ellipsis, we're expanding a template
15442 argument pack. */
15443 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15445 if (argument == error_mark_node)
15447 cp_token *token = cp_lexer_peek_token (parser->lexer);
15448 error_at (token->location,
15449 "expected parameter pack before %<...%>");
15451 /* Consume the `...' token. */
15452 cp_lexer_consume_token (parser->lexer);
15454 /* Make the argument into a TYPE_PACK_EXPANSION or
15455 EXPR_PACK_EXPANSION. */
15456 argument = make_pack_expansion (argument);
15459 if (n_args == alloced)
15461 alloced *= 2;
15463 if (arg_ary == fixed_args)
15465 arg_ary = XNEWVEC (tree, alloced);
15466 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
15468 else
15469 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
15471 arg_ary[n_args++] = argument;
15473 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15475 vec = make_tree_vec (n_args);
15477 while (n_args--)
15478 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
15480 if (arg_ary != fixed_args)
15481 free (arg_ary);
15482 parser->non_integral_constant_expression_p = saved_non_ice_p;
15483 parser->integral_constant_expression_p = saved_ice_p;
15484 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
15485 if (CHECKING_P)
15486 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
15487 return vec;
15490 /* Parse a template-argument.
15492 template-argument:
15493 assignment-expression
15494 type-id
15495 id-expression
15497 The representation is that of an assignment-expression, type-id, or
15498 id-expression -- except that the qualified id-expression is
15499 evaluated, so that the value returned is either a DECL or an
15500 OVERLOAD.
15502 Although the standard says "assignment-expression", it forbids
15503 throw-expressions or assignments in the template argument.
15504 Therefore, we use "conditional-expression" instead. */
15506 static tree
15507 cp_parser_template_argument (cp_parser* parser)
15509 tree argument;
15510 bool template_p;
15511 bool address_p;
15512 bool maybe_type_id = false;
15513 cp_token *token = NULL, *argument_start_token = NULL;
15514 location_t loc = 0;
15515 cp_id_kind idk;
15517 /* There's really no way to know what we're looking at, so we just
15518 try each alternative in order.
15520 [temp.arg]
15522 In a template-argument, an ambiguity between a type-id and an
15523 expression is resolved to a type-id, regardless of the form of
15524 the corresponding template-parameter.
15526 Therefore, we try a type-id first. */
15527 cp_parser_parse_tentatively (parser);
15528 argument = cp_parser_template_type_arg (parser);
15529 /* If there was no error parsing the type-id but the next token is a
15530 '>>', our behavior depends on which dialect of C++ we're
15531 parsing. In C++98, we probably found a typo for '> >'. But there
15532 are type-id which are also valid expressions. For instance:
15534 struct X { int operator >> (int); };
15535 template <int V> struct Foo {};
15536 Foo<X () >> 5> r;
15538 Here 'X()' is a valid type-id of a function type, but the user just
15539 wanted to write the expression "X() >> 5". Thus, we remember that we
15540 found a valid type-id, but we still try to parse the argument as an
15541 expression to see what happens.
15543 In C++0x, the '>>' will be considered two separate '>'
15544 tokens. */
15545 if (!cp_parser_error_occurred (parser)
15546 && cxx_dialect == cxx98
15547 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15549 maybe_type_id = true;
15550 cp_parser_abort_tentative_parse (parser);
15552 else
15554 /* If the next token isn't a `,' or a `>', then this argument wasn't
15555 really finished. This means that the argument is not a valid
15556 type-id. */
15557 if (!cp_parser_next_token_ends_template_argument_p (parser))
15558 cp_parser_error (parser, "expected template-argument");
15559 /* If that worked, we're done. */
15560 if (cp_parser_parse_definitely (parser))
15561 return argument;
15563 /* We're still not sure what the argument will be. */
15564 cp_parser_parse_tentatively (parser);
15565 /* Try a template. */
15566 argument_start_token = cp_lexer_peek_token (parser->lexer);
15567 argument = cp_parser_id_expression (parser,
15568 /*template_keyword_p=*/false,
15569 /*check_dependency_p=*/true,
15570 &template_p,
15571 /*declarator_p=*/false,
15572 /*optional_p=*/false);
15573 /* If the next token isn't a `,' or a `>', then this argument wasn't
15574 really finished. */
15575 if (!cp_parser_next_token_ends_template_argument_p (parser))
15576 cp_parser_error (parser, "expected template-argument");
15577 if (!cp_parser_error_occurred (parser))
15579 /* Figure out what is being referred to. If the id-expression
15580 was for a class template specialization, then we will have a
15581 TYPE_DECL at this point. There is no need to do name lookup
15582 at this point in that case. */
15583 if (TREE_CODE (argument) != TYPE_DECL)
15584 argument = cp_parser_lookup_name (parser, argument,
15585 none_type,
15586 /*is_template=*/template_p,
15587 /*is_namespace=*/false,
15588 /*check_dependency=*/true,
15589 /*ambiguous_decls=*/NULL,
15590 argument_start_token->location);
15591 /* Handle a constrained-type-specifier for a non-type template
15592 parameter. */
15593 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
15594 argument = decl;
15595 else if (TREE_CODE (argument) != TEMPLATE_DECL
15596 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
15597 cp_parser_error (parser, "expected template-name");
15599 if (cp_parser_parse_definitely (parser))
15601 if (TREE_DEPRECATED (argument))
15602 warn_deprecated_use (argument, NULL_TREE);
15603 return argument;
15605 /* It must be a non-type argument. In C++17 any constant-expression is
15606 allowed. */
15607 if (cxx_dialect > cxx14)
15608 goto general_expr;
15610 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
15612 -- an integral constant-expression of integral or enumeration
15613 type; or
15615 -- the name of a non-type template-parameter; or
15617 -- the name of an object or function with external linkage...
15619 -- the address of an object or function with external linkage...
15621 -- a pointer to member... */
15622 /* Look for a non-type template parameter. */
15623 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15625 cp_parser_parse_tentatively (parser);
15626 argument = cp_parser_primary_expression (parser,
15627 /*address_p=*/false,
15628 /*cast_p=*/false,
15629 /*template_arg_p=*/true,
15630 &idk);
15631 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
15632 || !cp_parser_next_token_ends_template_argument_p (parser))
15633 cp_parser_simulate_error (parser);
15634 if (cp_parser_parse_definitely (parser))
15635 return argument;
15638 /* If the next token is "&", the argument must be the address of an
15639 object or function with external linkage. */
15640 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
15641 if (address_p)
15643 loc = cp_lexer_peek_token (parser->lexer)->location;
15644 cp_lexer_consume_token (parser->lexer);
15646 /* See if we might have an id-expression. */
15647 token = cp_lexer_peek_token (parser->lexer);
15648 if (token->type == CPP_NAME
15649 || token->keyword == RID_OPERATOR
15650 || token->type == CPP_SCOPE
15651 || token->type == CPP_TEMPLATE_ID
15652 || token->type == CPP_NESTED_NAME_SPECIFIER)
15654 cp_parser_parse_tentatively (parser);
15655 argument = cp_parser_primary_expression (parser,
15656 address_p,
15657 /*cast_p=*/false,
15658 /*template_arg_p=*/true,
15659 &idk);
15660 if (cp_parser_error_occurred (parser)
15661 || !cp_parser_next_token_ends_template_argument_p (parser))
15662 cp_parser_abort_tentative_parse (parser);
15663 else
15665 tree probe;
15667 if (INDIRECT_REF_P (argument))
15669 /* Strip the dereference temporarily. */
15670 gcc_assert (REFERENCE_REF_P (argument));
15671 argument = TREE_OPERAND (argument, 0);
15674 /* If we're in a template, we represent a qualified-id referring
15675 to a static data member as a SCOPE_REF even if the scope isn't
15676 dependent so that we can check access control later. */
15677 probe = argument;
15678 if (TREE_CODE (probe) == SCOPE_REF)
15679 probe = TREE_OPERAND (probe, 1);
15680 if (VAR_P (probe))
15682 /* A variable without external linkage might still be a
15683 valid constant-expression, so no error is issued here
15684 if the external-linkage check fails. */
15685 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
15686 cp_parser_simulate_error (parser);
15688 else if (is_overloaded_fn (argument))
15689 /* All overloaded functions are allowed; if the external
15690 linkage test does not pass, an error will be issued
15691 later. */
15693 else if (address_p
15694 && (TREE_CODE (argument) == OFFSET_REF
15695 || TREE_CODE (argument) == SCOPE_REF))
15696 /* A pointer-to-member. */
15698 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
15700 else
15701 cp_parser_simulate_error (parser);
15703 if (cp_parser_parse_definitely (parser))
15705 if (address_p)
15706 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
15707 tf_warning_or_error);
15708 else
15709 argument = convert_from_reference (argument);
15710 return argument;
15714 /* If the argument started with "&", there are no other valid
15715 alternatives at this point. */
15716 if (address_p)
15718 cp_parser_error (parser, "invalid non-type template argument");
15719 return error_mark_node;
15722 general_expr:
15723 /* If the argument wasn't successfully parsed as a type-id followed
15724 by '>>', the argument can only be a constant expression now.
15725 Otherwise, we try parsing the constant-expression tentatively,
15726 because the argument could really be a type-id. */
15727 if (maybe_type_id)
15728 cp_parser_parse_tentatively (parser);
15730 if (cxx_dialect <= cxx14)
15731 argument = cp_parser_constant_expression (parser);
15732 else
15734 /* With C++17 generalized non-type template arguments we need to handle
15735 lvalue constant expressions, too. */
15736 argument = cp_parser_assignment_expression (parser);
15737 require_potential_constant_expression (argument);
15740 if (!maybe_type_id)
15741 return argument;
15742 if (!cp_parser_next_token_ends_template_argument_p (parser))
15743 cp_parser_error (parser, "expected template-argument");
15744 if (cp_parser_parse_definitely (parser))
15745 return argument;
15746 /* We did our best to parse the argument as a non type-id, but that
15747 was the only alternative that matched (albeit with a '>' after
15748 it). We can assume it's just a typo from the user, and a
15749 diagnostic will then be issued. */
15750 return cp_parser_template_type_arg (parser);
15753 /* Parse an explicit-instantiation.
15755 explicit-instantiation:
15756 template declaration
15758 Although the standard says `declaration', what it really means is:
15760 explicit-instantiation:
15761 template decl-specifier-seq [opt] declarator [opt] ;
15763 Things like `template int S<int>::i = 5, int S<double>::j;' are not
15764 supposed to be allowed. A defect report has been filed about this
15765 issue.
15767 GNU Extension:
15769 explicit-instantiation:
15770 storage-class-specifier template
15771 decl-specifier-seq [opt] declarator [opt] ;
15772 function-specifier template
15773 decl-specifier-seq [opt] declarator [opt] ; */
15775 static void
15776 cp_parser_explicit_instantiation (cp_parser* parser)
15778 int declares_class_or_enum;
15779 cp_decl_specifier_seq decl_specifiers;
15780 tree extension_specifier = NULL_TREE;
15782 timevar_push (TV_TEMPLATE_INST);
15784 /* Look for an (optional) storage-class-specifier or
15785 function-specifier. */
15786 if (cp_parser_allow_gnu_extensions_p (parser))
15788 extension_specifier
15789 = cp_parser_storage_class_specifier_opt (parser);
15790 if (!extension_specifier)
15791 extension_specifier
15792 = cp_parser_function_specifier_opt (parser,
15793 /*decl_specs=*/NULL);
15796 /* Look for the `template' keyword. */
15797 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15798 /* Let the front end know that we are processing an explicit
15799 instantiation. */
15800 begin_explicit_instantiation ();
15801 /* [temp.explicit] says that we are supposed to ignore access
15802 control while processing explicit instantiation directives. */
15803 push_deferring_access_checks (dk_no_check);
15804 /* Parse a decl-specifier-seq. */
15805 cp_parser_decl_specifier_seq (parser,
15806 CP_PARSER_FLAGS_OPTIONAL,
15807 &decl_specifiers,
15808 &declares_class_or_enum);
15809 /* If there was exactly one decl-specifier, and it declared a class,
15810 and there's no declarator, then we have an explicit type
15811 instantiation. */
15812 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
15814 tree type;
15816 type = check_tag_decl (&decl_specifiers,
15817 /*explicit_type_instantiation_p=*/true);
15818 /* Turn access control back on for names used during
15819 template instantiation. */
15820 pop_deferring_access_checks ();
15821 if (type)
15822 do_type_instantiation (type, extension_specifier,
15823 /*complain=*/tf_error);
15825 else
15827 cp_declarator *declarator;
15828 tree decl;
15830 /* Parse the declarator. */
15831 declarator
15832 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15833 /*ctor_dtor_or_conv_p=*/NULL,
15834 /*parenthesized_p=*/NULL,
15835 /*member_p=*/false,
15836 /*friend_p=*/false);
15837 if (declares_class_or_enum & 2)
15838 cp_parser_check_for_definition_in_return_type (declarator,
15839 decl_specifiers.type,
15840 decl_specifiers.locations[ds_type_spec]);
15841 if (declarator != cp_error_declarator)
15843 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
15844 permerror (decl_specifiers.locations[ds_inline],
15845 "explicit instantiation shall not use"
15846 " %<inline%> specifier");
15847 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
15848 permerror (decl_specifiers.locations[ds_constexpr],
15849 "explicit instantiation shall not use"
15850 " %<constexpr%> specifier");
15852 decl = grokdeclarator (declarator, &decl_specifiers,
15853 NORMAL, 0, &decl_specifiers.attributes);
15854 /* Turn access control back on for names used during
15855 template instantiation. */
15856 pop_deferring_access_checks ();
15857 /* Do the explicit instantiation. */
15858 do_decl_instantiation (decl, extension_specifier);
15860 else
15862 pop_deferring_access_checks ();
15863 /* Skip the body of the explicit instantiation. */
15864 cp_parser_skip_to_end_of_statement (parser);
15867 /* We're done with the instantiation. */
15868 end_explicit_instantiation ();
15870 cp_parser_consume_semicolon_at_end_of_statement (parser);
15872 timevar_pop (TV_TEMPLATE_INST);
15875 /* Parse an explicit-specialization.
15877 explicit-specialization:
15878 template < > declaration
15880 Although the standard says `declaration', what it really means is:
15882 explicit-specialization:
15883 template <> decl-specifier [opt] init-declarator [opt] ;
15884 template <> function-definition
15885 template <> explicit-specialization
15886 template <> template-declaration */
15888 static void
15889 cp_parser_explicit_specialization (cp_parser* parser)
15891 bool need_lang_pop;
15892 cp_token *token = cp_lexer_peek_token (parser->lexer);
15894 /* Look for the `template' keyword. */
15895 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15896 /* Look for the `<'. */
15897 cp_parser_require (parser, CPP_LESS, RT_LESS);
15898 /* Look for the `>'. */
15899 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15900 /* We have processed another parameter list. */
15901 ++parser->num_template_parameter_lists;
15902 /* [temp]
15904 A template ... explicit specialization ... shall not have C
15905 linkage. */
15906 if (current_lang_name == lang_name_c)
15908 error_at (token->location, "template specialization with C linkage");
15909 /* Give it C++ linkage to avoid confusing other parts of the
15910 front end. */
15911 push_lang_context (lang_name_cplusplus);
15912 need_lang_pop = true;
15914 else
15915 need_lang_pop = false;
15916 /* Let the front end know that we are beginning a specialization. */
15917 if (!begin_specialization ())
15919 end_specialization ();
15920 return;
15923 /* If the next keyword is `template', we need to figure out whether
15924 or not we're looking a template-declaration. */
15925 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15927 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15928 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
15929 cp_parser_template_declaration_after_export (parser,
15930 /*member_p=*/false);
15931 else
15932 cp_parser_explicit_specialization (parser);
15934 else
15935 /* Parse the dependent declaration. */
15936 cp_parser_single_declaration (parser,
15937 /*checks=*/NULL,
15938 /*member_p=*/false,
15939 /*explicit_specialization_p=*/true,
15940 /*friend_p=*/NULL);
15941 /* We're done with the specialization. */
15942 end_specialization ();
15943 /* For the erroneous case of a template with C linkage, we pushed an
15944 implicit C++ linkage scope; exit that scope now. */
15945 if (need_lang_pop)
15946 pop_lang_context ();
15947 /* We're done with this parameter list. */
15948 --parser->num_template_parameter_lists;
15951 /* Parse a type-specifier.
15953 type-specifier:
15954 simple-type-specifier
15955 class-specifier
15956 enum-specifier
15957 elaborated-type-specifier
15958 cv-qualifier
15960 GNU Extension:
15962 type-specifier:
15963 __complex__
15965 Returns a representation of the type-specifier. For a
15966 class-specifier, enum-specifier, or elaborated-type-specifier, a
15967 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
15969 The parser flags FLAGS is used to control type-specifier parsing.
15971 If IS_DECLARATION is TRUE, then this type-specifier is appearing
15972 in a decl-specifier-seq.
15974 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
15975 class-specifier, enum-specifier, or elaborated-type-specifier, then
15976 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
15977 if a type is declared; 2 if it is defined. Otherwise, it is set to
15978 zero.
15980 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
15981 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
15982 is set to FALSE. */
15984 static tree
15985 cp_parser_type_specifier (cp_parser* parser,
15986 cp_parser_flags flags,
15987 cp_decl_specifier_seq *decl_specs,
15988 bool is_declaration,
15989 int* declares_class_or_enum,
15990 bool* is_cv_qualifier)
15992 tree type_spec = NULL_TREE;
15993 cp_token *token;
15994 enum rid keyword;
15995 cp_decl_spec ds = ds_last;
15997 /* Assume this type-specifier does not declare a new type. */
15998 if (declares_class_or_enum)
15999 *declares_class_or_enum = 0;
16000 /* And that it does not specify a cv-qualifier. */
16001 if (is_cv_qualifier)
16002 *is_cv_qualifier = false;
16003 /* Peek at the next token. */
16004 token = cp_lexer_peek_token (parser->lexer);
16006 /* If we're looking at a keyword, we can use that to guide the
16007 production we choose. */
16008 keyword = token->keyword;
16009 switch (keyword)
16011 case RID_ENUM:
16012 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16013 goto elaborated_type_specifier;
16015 /* Look for the enum-specifier. */
16016 type_spec = cp_parser_enum_specifier (parser);
16017 /* If that worked, we're done. */
16018 if (type_spec)
16020 if (declares_class_or_enum)
16021 *declares_class_or_enum = 2;
16022 if (decl_specs)
16023 cp_parser_set_decl_spec_type (decl_specs,
16024 type_spec,
16025 token,
16026 /*type_definition_p=*/true);
16027 return type_spec;
16029 else
16030 goto elaborated_type_specifier;
16032 /* Any of these indicate either a class-specifier, or an
16033 elaborated-type-specifier. */
16034 case RID_CLASS:
16035 case RID_STRUCT:
16036 case RID_UNION:
16037 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16038 goto elaborated_type_specifier;
16040 /* Parse tentatively so that we can back up if we don't find a
16041 class-specifier. */
16042 cp_parser_parse_tentatively (parser);
16043 /* Look for the class-specifier. */
16044 type_spec = cp_parser_class_specifier (parser);
16045 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16046 /* If that worked, we're done. */
16047 if (cp_parser_parse_definitely (parser))
16049 if (declares_class_or_enum)
16050 *declares_class_or_enum = 2;
16051 if (decl_specs)
16052 cp_parser_set_decl_spec_type (decl_specs,
16053 type_spec,
16054 token,
16055 /*type_definition_p=*/true);
16056 return type_spec;
16059 /* Fall through. */
16060 elaborated_type_specifier:
16061 /* We're declaring (not defining) a class or enum. */
16062 if (declares_class_or_enum)
16063 *declares_class_or_enum = 1;
16065 /* Fall through. */
16066 case RID_TYPENAME:
16067 /* Look for an elaborated-type-specifier. */
16068 type_spec
16069 = (cp_parser_elaborated_type_specifier
16070 (parser,
16071 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16072 is_declaration));
16073 if (decl_specs)
16074 cp_parser_set_decl_spec_type (decl_specs,
16075 type_spec,
16076 token,
16077 /*type_definition_p=*/false);
16078 return type_spec;
16080 case RID_CONST:
16081 ds = ds_const;
16082 if (is_cv_qualifier)
16083 *is_cv_qualifier = true;
16084 break;
16086 case RID_VOLATILE:
16087 ds = ds_volatile;
16088 if (is_cv_qualifier)
16089 *is_cv_qualifier = true;
16090 break;
16092 case RID_RESTRICT:
16093 ds = ds_restrict;
16094 if (is_cv_qualifier)
16095 *is_cv_qualifier = true;
16096 break;
16098 case RID_COMPLEX:
16099 /* The `__complex__' keyword is a GNU extension. */
16100 ds = ds_complex;
16101 break;
16103 default:
16104 break;
16107 /* Handle simple keywords. */
16108 if (ds != ds_last)
16110 if (decl_specs)
16112 set_and_check_decl_spec_loc (decl_specs, ds, token);
16113 decl_specs->any_specifiers_p = true;
16115 return cp_lexer_consume_token (parser->lexer)->u.value;
16118 /* If we do not already have a type-specifier, assume we are looking
16119 at a simple-type-specifier. */
16120 type_spec = cp_parser_simple_type_specifier (parser,
16121 decl_specs,
16122 flags);
16124 /* If we didn't find a type-specifier, and a type-specifier was not
16125 optional in this context, issue an error message. */
16126 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16128 cp_parser_error (parser, "expected type specifier");
16129 return error_mark_node;
16132 return type_spec;
16135 /* Parse a simple-type-specifier.
16137 simple-type-specifier:
16138 :: [opt] nested-name-specifier [opt] type-name
16139 :: [opt] nested-name-specifier template template-id
16140 char
16141 wchar_t
16142 bool
16143 short
16145 long
16146 signed
16147 unsigned
16148 float
16149 double
16150 void
16152 C++0x Extension:
16154 simple-type-specifier:
16155 auto
16156 decltype ( expression )
16157 char16_t
16158 char32_t
16159 __underlying_type ( type-id )
16161 GNU Extension:
16163 simple-type-specifier:
16164 __int128
16165 __typeof__ unary-expression
16166 __typeof__ ( type-id )
16167 __typeof__ ( type-id ) { initializer-list , [opt] }
16169 Concepts Extension:
16171 simple-type-specifier:
16172 constrained-type-specifier
16174 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16175 appropriately updated. */
16177 static tree
16178 cp_parser_simple_type_specifier (cp_parser* parser,
16179 cp_decl_specifier_seq *decl_specs,
16180 cp_parser_flags flags)
16182 tree type = NULL_TREE;
16183 cp_token *token;
16184 int idx;
16186 /* Peek at the next token. */
16187 token = cp_lexer_peek_token (parser->lexer);
16189 /* If we're looking at a keyword, things are easy. */
16190 switch (token->keyword)
16192 case RID_CHAR:
16193 if (decl_specs)
16194 decl_specs->explicit_char_p = true;
16195 type = char_type_node;
16196 break;
16197 case RID_CHAR16:
16198 type = char16_type_node;
16199 break;
16200 case RID_CHAR32:
16201 type = char32_type_node;
16202 break;
16203 case RID_WCHAR:
16204 type = wchar_type_node;
16205 break;
16206 case RID_BOOL:
16207 type = boolean_type_node;
16208 break;
16209 case RID_SHORT:
16210 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16211 type = short_integer_type_node;
16212 break;
16213 case RID_INT:
16214 if (decl_specs)
16215 decl_specs->explicit_int_p = true;
16216 type = integer_type_node;
16217 break;
16218 case RID_INT_N_0:
16219 case RID_INT_N_1:
16220 case RID_INT_N_2:
16221 case RID_INT_N_3:
16222 idx = token->keyword - RID_INT_N_0;
16223 if (! int_n_enabled_p [idx])
16224 break;
16225 if (decl_specs)
16227 decl_specs->explicit_intN_p = true;
16228 decl_specs->int_n_idx = idx;
16230 type = int_n_trees [idx].signed_type;
16231 break;
16232 case RID_LONG:
16233 if (decl_specs)
16234 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16235 type = long_integer_type_node;
16236 break;
16237 case RID_SIGNED:
16238 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16239 type = integer_type_node;
16240 break;
16241 case RID_UNSIGNED:
16242 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16243 type = unsigned_type_node;
16244 break;
16245 case RID_FLOAT:
16246 type = float_type_node;
16247 break;
16248 case RID_DOUBLE:
16249 type = double_type_node;
16250 break;
16251 case RID_VOID:
16252 type = void_type_node;
16253 break;
16255 case RID_AUTO:
16256 maybe_warn_cpp0x (CPP0X_AUTO);
16257 if (parser->auto_is_implicit_function_template_parm_p)
16259 /* The 'auto' might be the placeholder return type for a function decl
16260 with trailing return type. */
16261 bool have_trailing_return_fn_decl = false;
16263 cp_parser_parse_tentatively (parser);
16264 cp_lexer_consume_token (parser->lexer);
16265 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16266 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16267 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16268 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16270 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16272 cp_lexer_consume_token (parser->lexer);
16273 cp_parser_skip_to_closing_parenthesis (parser,
16274 /*recovering*/false,
16275 /*or_comma*/false,
16276 /*consume_paren*/true);
16277 continue;
16280 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16282 have_trailing_return_fn_decl = true;
16283 break;
16286 cp_lexer_consume_token (parser->lexer);
16288 cp_parser_abort_tentative_parse (parser);
16290 if (have_trailing_return_fn_decl)
16292 type = make_auto ();
16293 break;
16296 if (cxx_dialect >= cxx14)
16298 type = synthesize_implicit_template_parm (parser, NULL_TREE);
16299 type = TREE_TYPE (type);
16301 else
16302 type = error_mark_node;
16304 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16306 if (cxx_dialect < cxx14)
16307 error_at (token->location,
16308 "use of %<auto%> in lambda parameter declaration "
16309 "only available with "
16310 "-std=c++14 or -std=gnu++14");
16312 else if (cxx_dialect < cxx14)
16313 error_at (token->location,
16314 "use of %<auto%> in parameter declaration "
16315 "only available with "
16316 "-std=c++14 or -std=gnu++14");
16317 else if (!flag_concepts)
16318 pedwarn (token->location, OPT_Wpedantic,
16319 "ISO C++ forbids use of %<auto%> in parameter "
16320 "declaration");
16322 else
16323 type = make_auto ();
16324 break;
16326 case RID_DECLTYPE:
16327 /* Since DR 743, decltype can either be a simple-type-specifier by
16328 itself or begin a nested-name-specifier. Parsing it will replace
16329 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16330 handling below decide what to do. */
16331 cp_parser_decltype (parser);
16332 cp_lexer_set_token_position (parser->lexer, token);
16333 break;
16335 case RID_TYPEOF:
16336 /* Consume the `typeof' token. */
16337 cp_lexer_consume_token (parser->lexer);
16338 /* Parse the operand to `typeof'. */
16339 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
16340 /* If it is not already a TYPE, take its type. */
16341 if (!TYPE_P (type))
16342 type = finish_typeof (type);
16344 if (decl_specs)
16345 cp_parser_set_decl_spec_type (decl_specs, type,
16346 token,
16347 /*type_definition_p=*/false);
16349 return type;
16351 case RID_UNDERLYING_TYPE:
16352 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
16353 if (decl_specs)
16354 cp_parser_set_decl_spec_type (decl_specs, type,
16355 token,
16356 /*type_definition_p=*/false);
16358 return type;
16360 case RID_BASES:
16361 case RID_DIRECT_BASES:
16362 type = cp_parser_trait_expr (parser, token->keyword);
16363 if (decl_specs)
16364 cp_parser_set_decl_spec_type (decl_specs, type,
16365 token,
16366 /*type_definition_p=*/false);
16367 return type;
16368 default:
16369 break;
16372 /* If token is an already-parsed decltype not followed by ::,
16373 it's a simple-type-specifier. */
16374 if (token->type == CPP_DECLTYPE
16375 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
16377 type = saved_checks_value (token->u.tree_check_value);
16378 if (decl_specs)
16380 cp_parser_set_decl_spec_type (decl_specs, type,
16381 token,
16382 /*type_definition_p=*/false);
16383 /* Remember that we are handling a decltype in order to
16384 implement the resolution of DR 1510 when the argument
16385 isn't instantiation dependent. */
16386 decl_specs->decltype_p = true;
16388 cp_lexer_consume_token (parser->lexer);
16389 return type;
16392 /* If the type-specifier was for a built-in type, we're done. */
16393 if (type)
16395 /* Record the type. */
16396 if (decl_specs
16397 && (token->keyword != RID_SIGNED
16398 && token->keyword != RID_UNSIGNED
16399 && token->keyword != RID_SHORT
16400 && token->keyword != RID_LONG))
16401 cp_parser_set_decl_spec_type (decl_specs,
16402 type,
16403 token,
16404 /*type_definition_p=*/false);
16405 if (decl_specs)
16406 decl_specs->any_specifiers_p = true;
16408 /* Consume the token. */
16409 cp_lexer_consume_token (parser->lexer);
16411 if (type == error_mark_node)
16412 return error_mark_node;
16414 /* There is no valid C++ program where a non-template type is
16415 followed by a "<". That usually indicates that the user thought
16416 that the type was a template. */
16417 cp_parser_check_for_invalid_template_id (parser, type, none_type,
16418 token->location);
16420 return TYPE_NAME (type);
16423 /* The type-specifier must be a user-defined type. */
16424 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
16426 bool qualified_p;
16427 bool global_p;
16429 /* Don't gobble tokens or issue error messages if this is an
16430 optional type-specifier. */
16431 if (flags & CP_PARSER_FLAGS_OPTIONAL)
16432 cp_parser_parse_tentatively (parser);
16434 /* Look for the optional `::' operator. */
16435 global_p
16436 = (cp_parser_global_scope_opt (parser,
16437 /*current_scope_valid_p=*/false)
16438 != NULL_TREE);
16439 /* Look for the nested-name specifier. */
16440 qualified_p
16441 = (cp_parser_nested_name_specifier_opt (parser,
16442 /*typename_keyword_p=*/false,
16443 /*check_dependency_p=*/true,
16444 /*type_p=*/false,
16445 /*is_declaration=*/false)
16446 != NULL_TREE);
16447 token = cp_lexer_peek_token (parser->lexer);
16448 /* If we have seen a nested-name-specifier, and the next token
16449 is `template', then we are using the template-id production. */
16450 if (parser->scope
16451 && cp_parser_optional_template_keyword (parser))
16453 /* Look for the template-id. */
16454 type = cp_parser_template_id (parser,
16455 /*template_keyword_p=*/true,
16456 /*check_dependency_p=*/true,
16457 none_type,
16458 /*is_declaration=*/false);
16459 /* If the template-id did not name a type, we are out of
16460 luck. */
16461 if (TREE_CODE (type) != TYPE_DECL)
16463 cp_parser_error (parser, "expected template-id for type");
16464 type = NULL_TREE;
16467 /* Otherwise, look for a type-name. */
16468 else
16469 type = cp_parser_type_name (parser);
16470 /* Keep track of all name-lookups performed in class scopes. */
16471 if (type
16472 && !global_p
16473 && !qualified_p
16474 && TREE_CODE (type) == TYPE_DECL
16475 && identifier_p (DECL_NAME (type)))
16476 maybe_note_name_used_in_class (DECL_NAME (type), type);
16477 /* If it didn't work out, we don't have a TYPE. */
16478 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
16479 && !cp_parser_parse_definitely (parser))
16480 type = NULL_TREE;
16481 if (type && decl_specs)
16482 cp_parser_set_decl_spec_type (decl_specs, type,
16483 token,
16484 /*type_definition_p=*/false);
16487 /* If we didn't get a type-name, issue an error message. */
16488 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16490 cp_parser_error (parser, "expected type-name");
16491 return error_mark_node;
16494 if (type && type != error_mark_node)
16496 /* See if TYPE is an Objective-C type, and if so, parse and
16497 accept any protocol references following it. Do this before
16498 the cp_parser_check_for_invalid_template_id() call, because
16499 Objective-C types can be followed by '<...>' which would
16500 enclose protocol names rather than template arguments, and so
16501 everything is fine. */
16502 if (c_dialect_objc () && !parser->scope
16503 && (objc_is_id (type) || objc_is_class_name (type)))
16505 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16506 tree qual_type = objc_get_protocol_qualified_type (type, protos);
16508 /* Clobber the "unqualified" type previously entered into
16509 DECL_SPECS with the new, improved protocol-qualified version. */
16510 if (decl_specs)
16511 decl_specs->type = qual_type;
16513 return qual_type;
16516 /* There is no valid C++ program where a non-template type is
16517 followed by a "<". That usually indicates that the user
16518 thought that the type was a template. */
16519 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
16520 none_type,
16521 token->location);
16524 return type;
16527 /* Parse a type-name.
16529 type-name:
16530 class-name
16531 enum-name
16532 typedef-name
16533 simple-template-id [in c++0x]
16535 enum-name:
16536 identifier
16538 typedef-name:
16539 identifier
16541 Concepts:
16543 type-name:
16544 concept-name
16545 partial-concept-id
16547 concept-name:
16548 identifier
16550 Returns a TYPE_DECL for the type. */
16552 static tree
16553 cp_parser_type_name (cp_parser* parser)
16555 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
16558 /* See above. */
16559 static tree
16560 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
16562 tree type_decl;
16564 /* We can't know yet whether it is a class-name or not. */
16565 cp_parser_parse_tentatively (parser);
16566 /* Try a class-name. */
16567 type_decl = cp_parser_class_name (parser,
16568 typename_keyword_p,
16569 /*template_keyword_p=*/false,
16570 none_type,
16571 /*check_dependency_p=*/true,
16572 /*class_head_p=*/false,
16573 /*is_declaration=*/false);
16574 /* If it's not a class-name, keep looking. */
16575 if (!cp_parser_parse_definitely (parser))
16577 if (cxx_dialect < cxx11)
16578 /* It must be a typedef-name or an enum-name. */
16579 return cp_parser_nonclass_name (parser);
16581 cp_parser_parse_tentatively (parser);
16582 /* It is either a simple-template-id representing an
16583 instantiation of an alias template... */
16584 type_decl = cp_parser_template_id (parser,
16585 /*template_keyword_p=*/false,
16586 /*check_dependency_p=*/true,
16587 none_type,
16588 /*is_declaration=*/false);
16589 /* Note that this must be an instantiation of an alias template
16590 because [temp.names]/6 says:
16592 A template-id that names an alias template specialization
16593 is a type-name.
16595 Whereas [temp.names]/7 says:
16597 A simple-template-id that names a class template
16598 specialization is a class-name.
16600 With concepts, this could also be a partial-concept-id that
16601 declares a non-type template parameter. */
16602 if (type_decl != NULL_TREE
16603 && TREE_CODE (type_decl) == TYPE_DECL
16604 && TYPE_DECL_ALIAS_P (type_decl))
16605 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
16606 else if (is_constrained_parameter (type_decl))
16607 /* Don't do anything. */ ;
16608 else
16609 cp_parser_simulate_error (parser);
16611 if (!cp_parser_parse_definitely (parser))
16612 /* ... Or a typedef-name or an enum-name. */
16613 return cp_parser_nonclass_name (parser);
16616 return type_decl;
16619 /* Check if DECL and ARGS can form a constrained-type-specifier.
16620 If ARGS is non-null, we try to form a concept check of the
16621 form DECL<?, ARGS> where ? is a wildcard that matches any
16622 kind of template argument. If ARGS is NULL, then we try to
16623 form a concept check of the form DECL<?>. */
16625 static tree
16626 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
16627 tree decl, tree args)
16629 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
16631 /* If we a constrained-type-specifier cannot be deduced. */
16632 if (parser->prevent_constrained_type_specifiers)
16633 return NULL_TREE;
16635 /* A constrained type specifier can only be found in an
16636 overload set or as a reference to a template declaration.
16638 FIXME: This might be masking a bug. It's possible that
16639 that the deduction below is causing template specializations
16640 to be formed with the wildcard as an argument. */
16641 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
16642 return NULL_TREE;
16644 /* Try to build a call expression that evaluates the
16645 concept. This can fail if the overload set refers
16646 only to non-templates. */
16647 tree placeholder = build_nt (WILDCARD_DECL);
16648 tree check = build_concept_check (decl, placeholder, args);
16649 if (check == error_mark_node)
16650 return NULL_TREE;
16652 /* Deduce the checked constraint and the prototype parameter.
16654 FIXME: In certain cases, failure to deduce should be a
16655 diagnosable error. */
16656 tree conc;
16657 tree proto;
16658 if (!deduce_constrained_parameter (check, conc, proto))
16659 return NULL_TREE;
16661 /* In template parameter scope, this results in a constrained
16662 parameter. Return a descriptor of that parm. */
16663 if (processing_template_parmlist)
16664 return build_constrained_parameter (conc, proto, args);
16666 /* In a parameter-declaration-clause, constrained-type
16667 specifiers result in invented template parameters. */
16668 if (parser->auto_is_implicit_function_template_parm_p)
16670 tree x = build_constrained_parameter (conc, proto, args);
16671 return synthesize_implicit_template_parm (parser, x);
16673 else
16675 /* Otherwise, we're in a context where the constrained
16676 type name is deduced and the constraint applies
16677 after deduction. */
16678 return make_constrained_auto (conc, args);
16681 return NULL_TREE;
16684 /* If DECL refers to a concept, return a TYPE_DECL representing
16685 the result of using the constrained type specifier in the
16686 current context. DECL refers to a concept if
16688 - it is an overload set containing a function concept taking a single
16689 type argument, or
16691 - it is a variable concept taking a single type argument. */
16693 static tree
16694 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
16696 if (flag_concepts
16697 && (TREE_CODE (decl) == OVERLOAD
16698 || BASELINK_P (decl)
16699 || variable_concept_p (decl)))
16700 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
16701 else
16702 return NULL_TREE;
16705 /* Check if DECL and ARGS form a partial-concept-id. If so,
16706 assign ID to the resulting constrained placeholder.
16708 Returns true if the partial-concept-id designates a placeholder
16709 and false otherwise. Note that *id is set to NULL_TREE in
16710 this case. */
16712 static tree
16713 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
16715 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
16718 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
16719 or a concept-name.
16721 enum-name:
16722 identifier
16724 typedef-name:
16725 identifier
16727 concept-name:
16728 identifier
16730 Returns a TYPE_DECL for the type. */
16732 static tree
16733 cp_parser_nonclass_name (cp_parser* parser)
16735 tree type_decl;
16736 tree identifier;
16738 cp_token *token = cp_lexer_peek_token (parser->lexer);
16739 identifier = cp_parser_identifier (parser);
16740 if (identifier == error_mark_node)
16741 return error_mark_node;
16743 /* Look up the type-name. */
16744 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
16746 type_decl = strip_using_decl (type_decl);
16748 /* If we found an overload set, then it may refer to a concept-name. */
16749 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
16750 type_decl = decl;
16752 if (TREE_CODE (type_decl) != TYPE_DECL
16753 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
16755 /* See if this is an Objective-C type. */
16756 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16757 tree type = objc_get_protocol_qualified_type (identifier, protos);
16758 if (type)
16759 type_decl = TYPE_NAME (type);
16762 /* Issue an error if we did not find a type-name. */
16763 if (TREE_CODE (type_decl) != TYPE_DECL
16764 /* In Objective-C, we have the complication that class names are
16765 normally type names and start declarations (eg, the
16766 "NSObject" in "NSObject *object;"), but can be used in an
16767 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
16768 is an expression. So, a classname followed by a dot is not a
16769 valid type-name. */
16770 || (objc_is_class_name (TREE_TYPE (type_decl))
16771 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
16773 if (!cp_parser_simulate_error (parser))
16774 cp_parser_name_lookup_error (parser, identifier, type_decl,
16775 NLE_TYPE, token->location);
16776 return error_mark_node;
16778 /* Remember that the name was used in the definition of the
16779 current class so that we can check later to see if the
16780 meaning would have been different after the class was
16781 entirely defined. */
16782 else if (type_decl != error_mark_node
16783 && !parser->scope)
16784 maybe_note_name_used_in_class (identifier, type_decl);
16786 return type_decl;
16789 /* Parse an elaborated-type-specifier. Note that the grammar given
16790 here incorporates the resolution to DR68.
16792 elaborated-type-specifier:
16793 class-key :: [opt] nested-name-specifier [opt] identifier
16794 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
16795 enum-key :: [opt] nested-name-specifier [opt] identifier
16796 typename :: [opt] nested-name-specifier identifier
16797 typename :: [opt] nested-name-specifier template [opt]
16798 template-id
16800 GNU extension:
16802 elaborated-type-specifier:
16803 class-key attributes :: [opt] nested-name-specifier [opt] identifier
16804 class-key attributes :: [opt] nested-name-specifier [opt]
16805 template [opt] template-id
16806 enum attributes :: [opt] nested-name-specifier [opt] identifier
16808 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
16809 declared `friend'. If IS_DECLARATION is TRUE, then this
16810 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
16811 something is being declared.
16813 Returns the TYPE specified. */
16815 static tree
16816 cp_parser_elaborated_type_specifier (cp_parser* parser,
16817 bool is_friend,
16818 bool is_declaration)
16820 enum tag_types tag_type;
16821 tree identifier;
16822 tree type = NULL_TREE;
16823 tree attributes = NULL_TREE;
16824 tree globalscope;
16825 cp_token *token = NULL;
16827 /* See if we're looking at the `enum' keyword. */
16828 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
16830 /* Consume the `enum' token. */
16831 cp_lexer_consume_token (parser->lexer);
16832 /* Remember that it's an enumeration type. */
16833 tag_type = enum_type;
16834 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
16835 enums) is used here. */
16836 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
16837 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
16839 pedwarn (input_location, 0, "elaborated-type-specifier "
16840 "for a scoped enum must not use the %<%D%> keyword",
16841 cp_lexer_peek_token (parser->lexer)->u.value);
16842 /* Consume the `struct' or `class' and parse it anyway. */
16843 cp_lexer_consume_token (parser->lexer);
16845 /* Parse the attributes. */
16846 attributes = cp_parser_attributes_opt (parser);
16848 /* Or, it might be `typename'. */
16849 else if (cp_lexer_next_token_is_keyword (parser->lexer,
16850 RID_TYPENAME))
16852 /* Consume the `typename' token. */
16853 cp_lexer_consume_token (parser->lexer);
16854 /* Remember that it's a `typename' type. */
16855 tag_type = typename_type;
16857 /* Otherwise it must be a class-key. */
16858 else
16860 tag_type = cp_parser_class_key (parser);
16861 if (tag_type == none_type)
16862 return error_mark_node;
16863 /* Parse the attributes. */
16864 attributes = cp_parser_attributes_opt (parser);
16867 /* Look for the `::' operator. */
16868 globalscope = cp_parser_global_scope_opt (parser,
16869 /*current_scope_valid_p=*/false);
16870 /* Look for the nested-name-specifier. */
16871 if (tag_type == typename_type && !globalscope)
16873 if (!cp_parser_nested_name_specifier (parser,
16874 /*typename_keyword_p=*/true,
16875 /*check_dependency_p=*/true,
16876 /*type_p=*/true,
16877 is_declaration))
16878 return error_mark_node;
16880 else
16881 /* Even though `typename' is not present, the proposed resolution
16882 to Core Issue 180 says that in `class A<T>::B', `B' should be
16883 considered a type-name, even if `A<T>' is dependent. */
16884 cp_parser_nested_name_specifier_opt (parser,
16885 /*typename_keyword_p=*/true,
16886 /*check_dependency_p=*/true,
16887 /*type_p=*/true,
16888 is_declaration);
16889 /* For everything but enumeration types, consider a template-id.
16890 For an enumeration type, consider only a plain identifier. */
16891 if (tag_type != enum_type)
16893 bool template_p = false;
16894 tree decl;
16896 /* Allow the `template' keyword. */
16897 template_p = cp_parser_optional_template_keyword (parser);
16898 /* If we didn't see `template', we don't know if there's a
16899 template-id or not. */
16900 if (!template_p)
16901 cp_parser_parse_tentatively (parser);
16902 /* Parse the template-id. */
16903 token = cp_lexer_peek_token (parser->lexer);
16904 decl = cp_parser_template_id (parser, template_p,
16905 /*check_dependency_p=*/true,
16906 tag_type,
16907 is_declaration);
16908 /* If we didn't find a template-id, look for an ordinary
16909 identifier. */
16910 if (!template_p && !cp_parser_parse_definitely (parser))
16912 /* We can get here when cp_parser_template_id, called by
16913 cp_parser_class_name with tag_type == none_type, succeeds
16914 and caches a BASELINK. Then, when called again here,
16915 instead of failing and returning an error_mark_node
16916 returns it (see template/typename17.C in C++11).
16917 ??? Could we diagnose this earlier? */
16918 else if (tag_type == typename_type && BASELINK_P (decl))
16920 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
16921 type = error_mark_node;
16923 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
16924 in effect, then we must assume that, upon instantiation, the
16925 template will correspond to a class. */
16926 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16927 && tag_type == typename_type)
16928 type = make_typename_type (parser->scope, decl,
16929 typename_type,
16930 /*complain=*/tf_error);
16931 /* If the `typename' keyword is in effect and DECL is not a type
16932 decl, then type is non existent. */
16933 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
16935 else if (TREE_CODE (decl) == TYPE_DECL)
16936 type = check_elaborated_type_specifier (tag_type, decl,
16937 /*allow_template_p=*/true);
16938 else if (decl == error_mark_node)
16939 type = error_mark_node;
16942 if (!type)
16944 token = cp_lexer_peek_token (parser->lexer);
16945 identifier = cp_parser_identifier (parser);
16947 if (identifier == error_mark_node)
16949 parser->scope = NULL_TREE;
16950 return error_mark_node;
16953 /* For a `typename', we needn't call xref_tag. */
16954 if (tag_type == typename_type
16955 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
16956 return cp_parser_make_typename_type (parser, identifier,
16957 token->location);
16959 /* Template parameter lists apply only if we are not within a
16960 function parameter list. */
16961 bool template_parm_lists_apply
16962 = parser->num_template_parameter_lists;
16963 if (template_parm_lists_apply)
16964 for (cp_binding_level *s = current_binding_level;
16965 s && s->kind != sk_template_parms;
16966 s = s->level_chain)
16967 if (s->kind == sk_function_parms)
16968 template_parm_lists_apply = false;
16970 /* Look up a qualified name in the usual way. */
16971 if (parser->scope)
16973 tree decl;
16974 tree ambiguous_decls;
16976 decl = cp_parser_lookup_name (parser, identifier,
16977 tag_type,
16978 /*is_template=*/false,
16979 /*is_namespace=*/false,
16980 /*check_dependency=*/true,
16981 &ambiguous_decls,
16982 token->location);
16984 /* If the lookup was ambiguous, an error will already have been
16985 issued. */
16986 if (ambiguous_decls)
16987 return error_mark_node;
16989 /* If we are parsing friend declaration, DECL may be a
16990 TEMPLATE_DECL tree node here. However, we need to check
16991 whether this TEMPLATE_DECL results in valid code. Consider
16992 the following example:
16994 namespace N {
16995 template <class T> class C {};
16997 class X {
16998 template <class T> friend class N::C; // #1, valid code
17000 template <class T> class Y {
17001 friend class N::C; // #2, invalid code
17004 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17005 name lookup of `N::C'. We see that friend declaration must
17006 be template for the code to be valid. Note that
17007 processing_template_decl does not work here since it is
17008 always 1 for the above two cases. */
17010 decl = (cp_parser_maybe_treat_template_as_class
17011 (decl, /*tag_name_p=*/is_friend
17012 && template_parm_lists_apply));
17014 if (TREE_CODE (decl) != TYPE_DECL)
17016 cp_parser_diagnose_invalid_type_name (parser,
17017 identifier,
17018 token->location);
17019 return error_mark_node;
17022 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17024 bool allow_template = (template_parm_lists_apply
17025 || DECL_SELF_REFERENCE_P (decl));
17026 type = check_elaborated_type_specifier (tag_type, decl,
17027 allow_template);
17029 if (type == error_mark_node)
17030 return error_mark_node;
17033 /* Forward declarations of nested types, such as
17035 class C1::C2;
17036 class C1::C2::C3;
17038 are invalid unless all components preceding the final '::'
17039 are complete. If all enclosing types are complete, these
17040 declarations become merely pointless.
17042 Invalid forward declarations of nested types are errors
17043 caught elsewhere in parsing. Those that are pointless arrive
17044 here. */
17046 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17047 && !is_friend && !processing_explicit_instantiation)
17048 warning (0, "declaration %qD does not declare anything", decl);
17050 type = TREE_TYPE (decl);
17052 else
17054 /* An elaborated-type-specifier sometimes introduces a new type and
17055 sometimes names an existing type. Normally, the rule is that it
17056 introduces a new type only if there is not an existing type of
17057 the same name already in scope. For example, given:
17059 struct S {};
17060 void f() { struct S s; }
17062 the `struct S' in the body of `f' is the same `struct S' as in
17063 the global scope; the existing definition is used. However, if
17064 there were no global declaration, this would introduce a new
17065 local class named `S'.
17067 An exception to this rule applies to the following code:
17069 namespace N { struct S; }
17071 Here, the elaborated-type-specifier names a new type
17072 unconditionally; even if there is already an `S' in the
17073 containing scope this declaration names a new type.
17074 This exception only applies if the elaborated-type-specifier
17075 forms the complete declaration:
17077 [class.name]
17079 A declaration consisting solely of `class-key identifier ;' is
17080 either a redeclaration of the name in the current scope or a
17081 forward declaration of the identifier as a class name. It
17082 introduces the name into the current scope.
17084 We are in this situation precisely when the next token is a `;'.
17086 An exception to the exception is that a `friend' declaration does
17087 *not* name a new type; i.e., given:
17089 struct S { friend struct T; };
17091 `T' is not a new type in the scope of `S'.
17093 Also, `new struct S' or `sizeof (struct S)' never results in the
17094 definition of a new type; a new type can only be declared in a
17095 declaration context. */
17097 tag_scope ts;
17098 bool template_p;
17100 if (is_friend)
17101 /* Friends have special name lookup rules. */
17102 ts = ts_within_enclosing_non_class;
17103 else if (is_declaration
17104 && cp_lexer_next_token_is (parser->lexer,
17105 CPP_SEMICOLON))
17106 /* This is a `class-key identifier ;' */
17107 ts = ts_current;
17108 else
17109 ts = ts_global;
17111 template_p =
17112 (template_parm_lists_apply
17113 && (cp_parser_next_token_starts_class_definition_p (parser)
17114 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17115 /* An unqualified name was used to reference this type, so
17116 there were no qualifying templates. */
17117 if (template_parm_lists_apply
17118 && !cp_parser_check_template_parameters (parser,
17119 /*num_templates=*/0,
17120 token->location,
17121 /*declarator=*/NULL))
17122 return error_mark_node;
17123 type = xref_tag (tag_type, identifier, ts, template_p);
17127 if (type == error_mark_node)
17128 return error_mark_node;
17130 /* Allow attributes on forward declarations of classes. */
17131 if (attributes)
17133 if (TREE_CODE (type) == TYPENAME_TYPE)
17134 warning (OPT_Wattributes,
17135 "attributes ignored on uninstantiated type");
17136 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17137 && ! processing_explicit_instantiation)
17138 warning (OPT_Wattributes,
17139 "attributes ignored on template instantiation");
17140 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17141 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17142 else
17143 warning (OPT_Wattributes,
17144 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17147 if (tag_type != enum_type)
17149 /* Indicate whether this class was declared as a `class' or as a
17150 `struct'. */
17151 if (CLASS_TYPE_P (type))
17152 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17153 cp_parser_check_class_key (tag_type, type);
17156 /* A "<" cannot follow an elaborated type specifier. If that
17157 happens, the user was probably trying to form a template-id. */
17158 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17159 token->location);
17161 return type;
17164 /* Parse an enum-specifier.
17166 enum-specifier:
17167 enum-head { enumerator-list [opt] }
17168 enum-head { enumerator-list , } [C++0x]
17170 enum-head:
17171 enum-key identifier [opt] enum-base [opt]
17172 enum-key nested-name-specifier identifier enum-base [opt]
17174 enum-key:
17175 enum
17176 enum class [C++0x]
17177 enum struct [C++0x]
17179 enum-base: [C++0x]
17180 : type-specifier-seq
17182 opaque-enum-specifier:
17183 enum-key identifier enum-base [opt] ;
17185 GNU Extensions:
17186 enum-key attributes[opt] identifier [opt] enum-base [opt]
17187 { enumerator-list [opt] }attributes[opt]
17188 enum-key attributes[opt] identifier [opt] enum-base [opt]
17189 { enumerator-list, }attributes[opt] [C++0x]
17191 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17192 if the token stream isn't an enum-specifier after all. */
17194 static tree
17195 cp_parser_enum_specifier (cp_parser* parser)
17197 tree identifier;
17198 tree type = NULL_TREE;
17199 tree prev_scope;
17200 tree nested_name_specifier = NULL_TREE;
17201 tree attributes;
17202 bool scoped_enum_p = false;
17203 bool has_underlying_type = false;
17204 bool nested_being_defined = false;
17205 bool new_value_list = false;
17206 bool is_new_type = false;
17207 bool is_unnamed = false;
17208 tree underlying_type = NULL_TREE;
17209 cp_token *type_start_token = NULL;
17210 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17212 parser->colon_corrects_to_scope_p = false;
17214 /* Parse tentatively so that we can back up if we don't find a
17215 enum-specifier. */
17216 cp_parser_parse_tentatively (parser);
17218 /* Caller guarantees that the current token is 'enum', an identifier
17219 possibly follows, and the token after that is an opening brace.
17220 If we don't have an identifier, fabricate an anonymous name for
17221 the enumeration being defined. */
17222 cp_lexer_consume_token (parser->lexer);
17224 /* Parse the "class" or "struct", which indicates a scoped
17225 enumeration type in C++0x. */
17226 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17227 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17229 if (cxx_dialect < cxx11)
17230 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17232 /* Consume the `struct' or `class' token. */
17233 cp_lexer_consume_token (parser->lexer);
17235 scoped_enum_p = true;
17238 attributes = cp_parser_attributes_opt (parser);
17240 /* Clear the qualification. */
17241 parser->scope = NULL_TREE;
17242 parser->qualifying_scope = NULL_TREE;
17243 parser->object_scope = NULL_TREE;
17245 /* Figure out in what scope the declaration is being placed. */
17246 prev_scope = current_scope ();
17248 type_start_token = cp_lexer_peek_token (parser->lexer);
17250 push_deferring_access_checks (dk_no_check);
17251 nested_name_specifier
17252 = cp_parser_nested_name_specifier_opt (parser,
17253 /*typename_keyword_p=*/true,
17254 /*check_dependency_p=*/false,
17255 /*type_p=*/false,
17256 /*is_declaration=*/false);
17258 if (nested_name_specifier)
17260 tree name;
17262 identifier = cp_parser_identifier (parser);
17263 name = cp_parser_lookup_name (parser, identifier,
17264 enum_type,
17265 /*is_template=*/false,
17266 /*is_namespace=*/false,
17267 /*check_dependency=*/true,
17268 /*ambiguous_decls=*/NULL,
17269 input_location);
17270 if (name && name != error_mark_node)
17272 type = TREE_TYPE (name);
17273 if (TREE_CODE (type) == TYPENAME_TYPE)
17275 /* Are template enums allowed in ISO? */
17276 if (template_parm_scope_p ())
17277 pedwarn (type_start_token->location, OPT_Wpedantic,
17278 "%qD is an enumeration template", name);
17279 /* ignore a typename reference, for it will be solved by name
17280 in start_enum. */
17281 type = NULL_TREE;
17284 else if (nested_name_specifier == error_mark_node)
17285 /* We already issued an error. */;
17286 else
17288 error_at (type_start_token->location,
17289 "%qD does not name an enumeration in %qT",
17290 identifier, nested_name_specifier);
17291 nested_name_specifier = error_mark_node;
17294 else
17296 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17297 identifier = cp_parser_identifier (parser);
17298 else
17300 identifier = make_anon_name ();
17301 is_unnamed = true;
17302 if (scoped_enum_p)
17303 error_at (type_start_token->location,
17304 "unnamed scoped enum is not allowed");
17307 pop_deferring_access_checks ();
17309 /* Check for the `:' that denotes a specified underlying type in C++0x.
17310 Note that a ':' could also indicate a bitfield width, however. */
17311 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17313 cp_decl_specifier_seq type_specifiers;
17315 /* Consume the `:'. */
17316 cp_lexer_consume_token (parser->lexer);
17318 /* Parse the type-specifier-seq. */
17319 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17320 /*is_trailing_return=*/false,
17321 &type_specifiers);
17323 /* At this point this is surely not elaborated type specifier. */
17324 if (!cp_parser_parse_definitely (parser))
17325 return NULL_TREE;
17327 if (cxx_dialect < cxx11)
17328 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17330 has_underlying_type = true;
17332 /* If that didn't work, stop. */
17333 if (type_specifiers.type != error_mark_node)
17335 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
17336 /*initialized=*/0, NULL);
17337 if (underlying_type == error_mark_node
17338 || check_for_bare_parameter_packs (underlying_type))
17339 underlying_type = NULL_TREE;
17343 /* Look for the `{' but don't consume it yet. */
17344 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17346 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
17348 cp_parser_error (parser, "expected %<{%>");
17349 if (has_underlying_type)
17351 type = NULL_TREE;
17352 goto out;
17355 /* An opaque-enum-specifier must have a ';' here. */
17356 if ((scoped_enum_p || underlying_type)
17357 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17359 cp_parser_error (parser, "expected %<;%> or %<{%>");
17360 if (has_underlying_type)
17362 type = NULL_TREE;
17363 goto out;
17368 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
17369 return NULL_TREE;
17371 if (nested_name_specifier)
17373 if (CLASS_TYPE_P (nested_name_specifier))
17375 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
17376 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
17377 push_scope (nested_name_specifier);
17379 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17381 push_nested_namespace (nested_name_specifier);
17385 /* Issue an error message if type-definitions are forbidden here. */
17386 if (!cp_parser_check_type_definition (parser))
17387 type = error_mark_node;
17388 else
17389 /* Create the new type. We do this before consuming the opening
17390 brace so the enum will be recorded as being on the line of its
17391 tag (or the 'enum' keyword, if there is no tag). */
17392 type = start_enum (identifier, type, underlying_type,
17393 attributes, scoped_enum_p, &is_new_type);
17395 /* If the next token is not '{' it is an opaque-enum-specifier or an
17396 elaborated-type-specifier. */
17397 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17399 timevar_push (TV_PARSE_ENUM);
17400 if (nested_name_specifier
17401 && nested_name_specifier != error_mark_node)
17403 /* The following catches invalid code such as:
17404 enum class S<int>::E { A, B, C }; */
17405 if (!processing_specialization
17406 && CLASS_TYPE_P (nested_name_specifier)
17407 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
17408 error_at (type_start_token->location, "cannot add an enumerator "
17409 "list to a template instantiation");
17411 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
17413 error_at (type_start_token->location,
17414 "%<%T::%E%> has not been declared",
17415 TYPE_CONTEXT (nested_name_specifier),
17416 nested_name_specifier);
17417 type = error_mark_node;
17419 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
17420 && !CLASS_TYPE_P (nested_name_specifier))
17422 error_at (type_start_token->location, "nested name specifier "
17423 "%qT for enum declaration does not name a class "
17424 "or namespace", nested_name_specifier);
17425 type = error_mark_node;
17427 /* If that scope does not contain the scope in which the
17428 class was originally declared, the program is invalid. */
17429 else if (prev_scope && !is_ancestor (prev_scope,
17430 nested_name_specifier))
17432 if (at_namespace_scope_p ())
17433 error_at (type_start_token->location,
17434 "declaration of %qD in namespace %qD which does not "
17435 "enclose %qD",
17436 type, prev_scope, nested_name_specifier);
17437 else
17438 error_at (type_start_token->location,
17439 "declaration of %qD in %qD which does not "
17440 "enclose %qD",
17441 type, prev_scope, nested_name_specifier);
17442 type = error_mark_node;
17444 /* If that scope is the scope where the declaration is being placed
17445 the program is invalid. */
17446 else if (CLASS_TYPE_P (nested_name_specifier)
17447 && CLASS_TYPE_P (prev_scope)
17448 && same_type_p (nested_name_specifier, prev_scope))
17450 permerror (type_start_token->location,
17451 "extra qualification not allowed");
17452 nested_name_specifier = NULL_TREE;
17456 if (scoped_enum_p)
17457 begin_scope (sk_scoped_enum, type);
17459 /* Consume the opening brace. */
17460 cp_lexer_consume_token (parser->lexer);
17462 if (type == error_mark_node)
17463 ; /* Nothing to add */
17464 else if (OPAQUE_ENUM_P (type)
17465 || (cxx_dialect > cxx98 && processing_specialization))
17467 new_value_list = true;
17468 SET_OPAQUE_ENUM_P (type, false);
17469 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17471 else
17473 error_at (type_start_token->location,
17474 "multiple definition of %q#T", type);
17475 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
17476 "previous definition here");
17477 type = error_mark_node;
17480 if (type == error_mark_node)
17481 cp_parser_skip_to_end_of_block_or_statement (parser);
17482 /* If the next token is not '}', then there are some enumerators. */
17483 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17485 if (is_unnamed && !scoped_enum_p)
17486 pedwarn (type_start_token->location, OPT_Wpedantic,
17487 "ISO C++ forbids empty unnamed enum");
17489 else
17490 cp_parser_enumerator_list (parser, type);
17492 /* Consume the final '}'. */
17493 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17495 if (scoped_enum_p)
17496 finish_scope ();
17497 timevar_pop (TV_PARSE_ENUM);
17499 else
17501 /* If a ';' follows, then it is an opaque-enum-specifier
17502 and additional restrictions apply. */
17503 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17505 if (is_unnamed)
17506 error_at (type_start_token->location,
17507 "opaque-enum-specifier without name");
17508 else if (nested_name_specifier)
17509 error_at (type_start_token->location,
17510 "opaque-enum-specifier must use a simple identifier");
17514 /* Look for trailing attributes to apply to this enumeration, and
17515 apply them if appropriate. */
17516 if (cp_parser_allow_gnu_extensions_p (parser))
17518 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
17519 cplus_decl_attributes (&type,
17520 trailing_attr,
17521 (int) ATTR_FLAG_TYPE_IN_PLACE);
17524 /* Finish up the enumeration. */
17525 if (type != error_mark_node)
17527 if (new_value_list)
17528 finish_enum_value_list (type);
17529 if (is_new_type)
17530 finish_enum (type);
17533 if (nested_name_specifier)
17535 if (CLASS_TYPE_P (nested_name_specifier))
17537 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
17538 pop_scope (nested_name_specifier);
17540 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17542 pop_nested_namespace (nested_name_specifier);
17545 out:
17546 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17547 return type;
17550 /* Parse an enumerator-list. The enumerators all have the indicated
17551 TYPE.
17553 enumerator-list:
17554 enumerator-definition
17555 enumerator-list , enumerator-definition */
17557 static void
17558 cp_parser_enumerator_list (cp_parser* parser, tree type)
17560 while (true)
17562 /* Parse an enumerator-definition. */
17563 cp_parser_enumerator_definition (parser, type);
17565 /* If the next token is not a ',', we've reached the end of
17566 the list. */
17567 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17568 break;
17569 /* Otherwise, consume the `,' and keep going. */
17570 cp_lexer_consume_token (parser->lexer);
17571 /* If the next token is a `}', there is a trailing comma. */
17572 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17574 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
17575 pedwarn (input_location, OPT_Wpedantic,
17576 "comma at end of enumerator list");
17577 break;
17582 /* Parse an enumerator-definition. The enumerator has the indicated
17583 TYPE.
17585 enumerator-definition:
17586 enumerator
17587 enumerator = constant-expression
17589 enumerator:
17590 identifier
17592 GNU Extensions:
17594 enumerator-definition:
17595 enumerator attributes [opt]
17596 enumerator attributes [opt] = constant-expression */
17598 static void
17599 cp_parser_enumerator_definition (cp_parser* parser, tree type)
17601 tree identifier;
17602 tree value;
17603 location_t loc;
17605 /* Save the input location because we are interested in the location
17606 of the identifier and not the location of the explicit value. */
17607 loc = cp_lexer_peek_token (parser->lexer)->location;
17609 /* Look for the identifier. */
17610 identifier = cp_parser_identifier (parser);
17611 if (identifier == error_mark_node)
17612 return;
17614 /* Parse any specified attributes. */
17615 tree attrs = cp_parser_attributes_opt (parser);
17617 /* If the next token is an '=', then there is an explicit value. */
17618 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17620 /* Consume the `=' token. */
17621 cp_lexer_consume_token (parser->lexer);
17622 /* Parse the value. */
17623 value = cp_parser_constant_expression (parser);
17625 else
17626 value = NULL_TREE;
17628 /* If we are processing a template, make sure the initializer of the
17629 enumerator doesn't contain any bare template parameter pack. */
17630 if (check_for_bare_parameter_packs (value))
17631 value = error_mark_node;
17633 /* Create the enumerator. */
17634 build_enumerator (identifier, value, type, attrs, loc);
17637 /* Parse a namespace-name.
17639 namespace-name:
17640 original-namespace-name
17641 namespace-alias
17643 Returns the NAMESPACE_DECL for the namespace. */
17645 static tree
17646 cp_parser_namespace_name (cp_parser* parser)
17648 tree identifier;
17649 tree namespace_decl;
17651 cp_token *token = cp_lexer_peek_token (parser->lexer);
17653 /* Get the name of the namespace. */
17654 identifier = cp_parser_identifier (parser);
17655 if (identifier == error_mark_node)
17656 return error_mark_node;
17658 /* Look up the identifier in the currently active scope. Look only
17659 for namespaces, due to:
17661 [basic.lookup.udir]
17663 When looking up a namespace-name in a using-directive or alias
17664 definition, only namespace names are considered.
17666 And:
17668 [basic.lookup.qual]
17670 During the lookup of a name preceding the :: scope resolution
17671 operator, object, function, and enumerator names are ignored.
17673 (Note that cp_parser_qualifying_entity only calls this
17674 function if the token after the name is the scope resolution
17675 operator.) */
17676 namespace_decl = cp_parser_lookup_name (parser, identifier,
17677 none_type,
17678 /*is_template=*/false,
17679 /*is_namespace=*/true,
17680 /*check_dependency=*/true,
17681 /*ambiguous_decls=*/NULL,
17682 token->location);
17683 /* If it's not a namespace, issue an error. */
17684 if (namespace_decl == error_mark_node
17685 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
17687 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17688 error_at (token->location, "%qD is not a namespace-name", identifier);
17689 cp_parser_error (parser, "expected namespace-name");
17690 namespace_decl = error_mark_node;
17693 return namespace_decl;
17696 /* Parse a namespace-definition.
17698 namespace-definition:
17699 named-namespace-definition
17700 unnamed-namespace-definition
17702 named-namespace-definition:
17703 original-namespace-definition
17704 extension-namespace-definition
17706 original-namespace-definition:
17707 namespace identifier { namespace-body }
17709 extension-namespace-definition:
17710 namespace original-namespace-name { namespace-body }
17712 unnamed-namespace-definition:
17713 namespace { namespace-body } */
17715 static void
17716 cp_parser_namespace_definition (cp_parser* parser)
17718 tree identifier, attribs;
17719 bool has_visibility;
17720 bool is_inline;
17721 cp_token* token;
17722 int nested_definition_count = 0;
17724 cp_ensure_no_omp_declare_simd (parser);
17725 cp_ensure_no_oacc_routine (parser);
17726 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
17728 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
17729 is_inline = true;
17730 cp_lexer_consume_token (parser->lexer);
17732 else
17733 is_inline = false;
17735 /* Look for the `namespace' keyword. */
17736 token = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17738 /* Parse any specified attributes before the identifier. */
17739 attribs = cp_parser_attributes_opt (parser);
17741 /* Get the name of the namespace. We do not attempt to distinguish
17742 between an original-namespace-definition and an
17743 extension-namespace-definition at this point. The semantic
17744 analysis routines are responsible for that. */
17745 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17746 identifier = cp_parser_identifier (parser);
17747 else
17748 identifier = NULL_TREE;
17750 /* Parse any specified attributes after the identifier. */
17751 tree post_ident_attribs = cp_parser_attributes_opt (parser);
17752 if (post_ident_attribs)
17754 if (attribs)
17755 attribs = chainon (attribs, post_ident_attribs);
17756 else
17757 attribs = post_ident_attribs;
17760 /* Start the namespace. */
17761 bool ok = push_namespace (identifier);
17763 /* Parse any nested namespace definition. */
17764 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17766 if (attribs)
17767 error_at (token->location, "a nested namespace definition cannot have attributes");
17768 if (cxx_dialect < cxx1z)
17769 pedwarn (input_location, OPT_Wpedantic,
17770 "nested namespace definitions only available with "
17771 "-std=c++1z or -std=gnu++1z");
17772 if (is_inline)
17773 error_at (token->location, "a nested namespace definition cannot be inline");
17774 while (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17776 cp_lexer_consume_token (parser->lexer);
17777 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17778 identifier = cp_parser_identifier (parser);
17779 else
17781 cp_parser_error (parser, "nested identifier required");
17782 break;
17784 ++nested_definition_count;
17785 push_namespace (identifier);
17789 /* Look for the `{' to validate starting the namespace. */
17790 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
17792 /* "inline namespace" is equivalent to a stub namespace definition
17793 followed by a strong using directive. */
17794 if (is_inline && ok)
17796 tree name_space = current_namespace;
17797 /* Set up namespace association. */
17798 DECL_NAMESPACE_ASSOCIATIONS (name_space)
17799 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
17800 DECL_NAMESPACE_ASSOCIATIONS (name_space));
17801 /* Import the contents of the inline namespace. */
17802 pop_namespace ();
17803 do_using_directive (name_space);
17804 push_namespace (identifier);
17807 has_visibility = handle_namespace_attrs (current_namespace, attribs);
17809 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
17811 /* Parse the body of the namespace. */
17812 cp_parser_namespace_body (parser);
17814 if (has_visibility)
17815 pop_visibility (1);
17817 /* Finish the nested namespace definitions. */
17818 while (nested_definition_count--)
17819 pop_namespace ();
17821 /* Finish the namespace. */
17822 if (ok)
17823 pop_namespace ();
17824 /* Look for the final `}'. */
17825 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17828 /* Parse a namespace-body.
17830 namespace-body:
17831 declaration-seq [opt] */
17833 static void
17834 cp_parser_namespace_body (cp_parser* parser)
17836 cp_parser_declaration_seq_opt (parser);
17839 /* Parse a namespace-alias-definition.
17841 namespace-alias-definition:
17842 namespace identifier = qualified-namespace-specifier ; */
17844 static void
17845 cp_parser_namespace_alias_definition (cp_parser* parser)
17847 tree identifier;
17848 tree namespace_specifier;
17850 cp_token *token = cp_lexer_peek_token (parser->lexer);
17852 /* Look for the `namespace' keyword. */
17853 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17854 /* Look for the identifier. */
17855 identifier = cp_parser_identifier (parser);
17856 if (identifier == error_mark_node)
17857 return;
17858 /* Look for the `=' token. */
17859 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
17860 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17862 error_at (token->location, "%<namespace%> definition is not allowed here");
17863 /* Skip the definition. */
17864 cp_lexer_consume_token (parser->lexer);
17865 if (cp_parser_skip_to_closing_brace (parser))
17866 cp_lexer_consume_token (parser->lexer);
17867 return;
17869 cp_parser_require (parser, CPP_EQ, RT_EQ);
17870 /* Look for the qualified-namespace-specifier. */
17871 namespace_specifier
17872 = cp_parser_qualified_namespace_specifier (parser);
17873 /* Look for the `;' token. */
17874 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17876 /* Register the alias in the symbol table. */
17877 do_namespace_alias (identifier, namespace_specifier);
17880 /* Parse a qualified-namespace-specifier.
17882 qualified-namespace-specifier:
17883 :: [opt] nested-name-specifier [opt] namespace-name
17885 Returns a NAMESPACE_DECL corresponding to the specified
17886 namespace. */
17888 static tree
17889 cp_parser_qualified_namespace_specifier (cp_parser* parser)
17891 /* Look for the optional `::'. */
17892 cp_parser_global_scope_opt (parser,
17893 /*current_scope_valid_p=*/false);
17895 /* Look for the optional nested-name-specifier. */
17896 cp_parser_nested_name_specifier_opt (parser,
17897 /*typename_keyword_p=*/false,
17898 /*check_dependency_p=*/true,
17899 /*type_p=*/false,
17900 /*is_declaration=*/true);
17902 return cp_parser_namespace_name (parser);
17905 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
17906 access declaration.
17908 using-declaration:
17909 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
17910 using :: unqualified-id ;
17912 access-declaration:
17913 qualified-id ;
17917 static bool
17918 cp_parser_using_declaration (cp_parser* parser,
17919 bool access_declaration_p)
17921 cp_token *token;
17922 bool typename_p = false;
17923 bool global_scope_p;
17924 tree decl;
17925 tree identifier;
17926 tree qscope;
17927 int oldcount = errorcount;
17928 cp_token *diag_token = NULL;
17930 if (access_declaration_p)
17932 diag_token = cp_lexer_peek_token (parser->lexer);
17933 cp_parser_parse_tentatively (parser);
17935 else
17937 /* Look for the `using' keyword. */
17938 cp_parser_require_keyword (parser, RID_USING, RT_USING);
17940 /* Peek at the next token. */
17941 token = cp_lexer_peek_token (parser->lexer);
17942 /* See if it's `typename'. */
17943 if (token->keyword == RID_TYPENAME)
17945 /* Remember that we've seen it. */
17946 typename_p = true;
17947 /* Consume the `typename' token. */
17948 cp_lexer_consume_token (parser->lexer);
17952 /* Look for the optional global scope qualification. */
17953 global_scope_p
17954 = (cp_parser_global_scope_opt (parser,
17955 /*current_scope_valid_p=*/false)
17956 != NULL_TREE);
17958 /* If we saw `typename', or didn't see `::', then there must be a
17959 nested-name-specifier present. */
17960 if (typename_p || !global_scope_p)
17962 qscope = cp_parser_nested_name_specifier (parser, typename_p,
17963 /*check_dependency_p=*/true,
17964 /*type_p=*/false,
17965 /*is_declaration=*/true);
17966 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
17968 cp_parser_skip_to_end_of_block_or_statement (parser);
17969 return false;
17972 /* Otherwise, we could be in either of the two productions. In that
17973 case, treat the nested-name-specifier as optional. */
17974 else
17975 qscope = cp_parser_nested_name_specifier_opt (parser,
17976 /*typename_keyword_p=*/false,
17977 /*check_dependency_p=*/true,
17978 /*type_p=*/false,
17979 /*is_declaration=*/true);
17980 if (!qscope)
17981 qscope = global_namespace;
17982 else if (UNSCOPED_ENUM_P (qscope))
17983 qscope = CP_TYPE_CONTEXT (qscope);
17985 if (access_declaration_p && cp_parser_error_occurred (parser))
17986 /* Something has already gone wrong; there's no need to parse
17987 further. Since an error has occurred, the return value of
17988 cp_parser_parse_definitely will be false, as required. */
17989 return cp_parser_parse_definitely (parser);
17991 token = cp_lexer_peek_token (parser->lexer);
17992 /* Parse the unqualified-id. */
17993 identifier = cp_parser_unqualified_id (parser,
17994 /*template_keyword_p=*/false,
17995 /*check_dependency_p=*/true,
17996 /*declarator_p=*/true,
17997 /*optional_p=*/false);
17999 if (access_declaration_p)
18001 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18002 cp_parser_simulate_error (parser);
18003 if (!cp_parser_parse_definitely (parser))
18004 return false;
18007 /* The function we call to handle a using-declaration is different
18008 depending on what scope we are in. */
18009 if (qscope == error_mark_node || identifier == error_mark_node)
18011 else if (!identifier_p (identifier)
18012 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18013 /* [namespace.udecl]
18015 A using declaration shall not name a template-id. */
18016 error_at (token->location,
18017 "a template-id may not appear in a using-declaration");
18018 else
18020 if (at_class_scope_p ())
18022 /* Create the USING_DECL. */
18023 decl = do_class_using_decl (parser->scope, identifier);
18025 if (decl && typename_p)
18026 USING_DECL_TYPENAME_P (decl) = 1;
18028 if (check_for_bare_parameter_packs (decl))
18030 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18031 return false;
18033 else
18034 /* Add it to the list of members in this class. */
18035 finish_member_declaration (decl);
18037 else
18039 decl = cp_parser_lookup_name_simple (parser,
18040 identifier,
18041 token->location);
18042 if (decl == error_mark_node)
18043 cp_parser_name_lookup_error (parser, identifier,
18044 decl, NLE_NULL,
18045 token->location);
18046 else if (check_for_bare_parameter_packs (decl))
18048 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18049 return false;
18051 else if (!at_namespace_scope_p ())
18052 do_local_using_decl (decl, qscope, identifier);
18053 else
18054 do_toplevel_using_decl (decl, qscope, identifier);
18058 /* Look for the final `;'. */
18059 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18061 if (access_declaration_p && errorcount == oldcount)
18062 warning_at (diag_token->location, OPT_Wdeprecated,
18063 "access declarations are deprecated "
18064 "in favour of using-declarations; "
18065 "suggestion: add the %<using%> keyword");
18067 return true;
18070 /* Parse an alias-declaration.
18072 alias-declaration:
18073 using identifier attribute-specifier-seq [opt] = type-id */
18075 static tree
18076 cp_parser_alias_declaration (cp_parser* parser)
18078 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18079 location_t id_location;
18080 cp_declarator *declarator;
18081 cp_decl_specifier_seq decl_specs;
18082 bool member_p;
18083 const char *saved_message = NULL;
18085 /* Look for the `using' keyword. */
18086 cp_token *using_token
18087 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18088 if (using_token == NULL)
18089 return error_mark_node;
18091 id_location = cp_lexer_peek_token (parser->lexer)->location;
18092 id = cp_parser_identifier (parser);
18093 if (id == error_mark_node)
18094 return error_mark_node;
18096 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18097 attributes = cp_parser_attributes_opt (parser);
18098 if (attributes == error_mark_node)
18099 return error_mark_node;
18101 cp_parser_require (parser, CPP_EQ, RT_EQ);
18103 if (cp_parser_error_occurred (parser))
18104 return error_mark_node;
18106 cp_parser_commit_to_tentative_parse (parser);
18108 /* Now we are going to parse the type-id of the declaration. */
18111 [dcl.type]/3 says:
18113 "A type-specifier-seq shall not define a class or enumeration
18114 unless it appears in the type-id of an alias-declaration (7.1.3) that
18115 is not the declaration of a template-declaration."
18117 In other words, if we currently are in an alias template, the
18118 type-id should not define a type.
18120 So let's set parser->type_definition_forbidden_message in that
18121 case; cp_parser_check_type_definition (called by
18122 cp_parser_class_specifier) will then emit an error if a type is
18123 defined in the type-id. */
18124 if (parser->num_template_parameter_lists)
18126 saved_message = parser->type_definition_forbidden_message;
18127 parser->type_definition_forbidden_message =
18128 G_("types may not be defined in alias template declarations");
18131 type = cp_parser_type_id (parser);
18133 /* Restore the error message if need be. */
18134 if (parser->num_template_parameter_lists)
18135 parser->type_definition_forbidden_message = saved_message;
18137 if (type == error_mark_node
18138 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18140 cp_parser_skip_to_end_of_block_or_statement (parser);
18141 return error_mark_node;
18144 /* A typedef-name can also be introduced by an alias-declaration. The
18145 identifier following the using keyword becomes a typedef-name. It has
18146 the same semantics as if it were introduced by the typedef
18147 specifier. In particular, it does not define a new type and it shall
18148 not appear in the type-id. */
18150 clear_decl_specs (&decl_specs);
18151 decl_specs.type = type;
18152 if (attributes != NULL_TREE)
18154 decl_specs.attributes = attributes;
18155 set_and_check_decl_spec_loc (&decl_specs,
18156 ds_attribute,
18157 attrs_token);
18159 set_and_check_decl_spec_loc (&decl_specs,
18160 ds_typedef,
18161 using_token);
18162 set_and_check_decl_spec_loc (&decl_specs,
18163 ds_alias,
18164 using_token);
18166 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18167 declarator->id_loc = id_location;
18169 member_p = at_class_scope_p ();
18170 if (member_p)
18171 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18172 NULL_TREE, attributes);
18173 else
18174 decl = start_decl (declarator, &decl_specs, 0,
18175 attributes, NULL_TREE, &pushed_scope);
18176 if (decl == error_mark_node)
18177 return decl;
18179 // Attach constraints to the alias declaration.
18180 if (flag_concepts && current_template_parms)
18182 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
18183 tree constr = build_constraints (reqs, NULL_TREE);
18184 set_constraints (decl, constr);
18187 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
18189 if (pushed_scope)
18190 pop_scope (pushed_scope);
18192 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18193 added into the symbol table; otherwise, return the TYPE_DECL. */
18194 if (DECL_LANG_SPECIFIC (decl)
18195 && DECL_TEMPLATE_INFO (decl)
18196 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
18198 decl = DECL_TI_TEMPLATE (decl);
18199 if (member_p)
18200 check_member_template (decl);
18203 return decl;
18206 /* Parse a using-directive.
18208 using-directive:
18209 using namespace :: [opt] nested-name-specifier [opt]
18210 namespace-name ; */
18212 static void
18213 cp_parser_using_directive (cp_parser* parser)
18215 tree namespace_decl;
18216 tree attribs;
18218 /* Look for the `using' keyword. */
18219 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18220 /* And the `namespace' keyword. */
18221 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18222 /* Look for the optional `::' operator. */
18223 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18224 /* And the optional nested-name-specifier. */
18225 cp_parser_nested_name_specifier_opt (parser,
18226 /*typename_keyword_p=*/false,
18227 /*check_dependency_p=*/true,
18228 /*type_p=*/false,
18229 /*is_declaration=*/true);
18230 /* Get the namespace being used. */
18231 namespace_decl = cp_parser_namespace_name (parser);
18232 /* And any specified attributes. */
18233 attribs = cp_parser_attributes_opt (parser);
18234 /* Update the symbol table. */
18235 parse_using_directive (namespace_decl, attribs);
18236 /* Look for the final `;'. */
18237 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18240 /* Parse an asm-definition.
18242 asm-definition:
18243 asm ( string-literal ) ;
18245 GNU Extension:
18247 asm-definition:
18248 asm volatile [opt] ( string-literal ) ;
18249 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18250 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18251 : asm-operand-list [opt] ) ;
18252 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18253 : asm-operand-list [opt]
18254 : asm-clobber-list [opt] ) ;
18255 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18256 : asm-clobber-list [opt]
18257 : asm-goto-list ) ; */
18259 static void
18260 cp_parser_asm_definition (cp_parser* parser)
18262 tree string;
18263 tree outputs = NULL_TREE;
18264 tree inputs = NULL_TREE;
18265 tree clobbers = NULL_TREE;
18266 tree labels = NULL_TREE;
18267 tree asm_stmt;
18268 bool volatile_p = false;
18269 bool extended_p = false;
18270 bool invalid_inputs_p = false;
18271 bool invalid_outputs_p = false;
18272 bool goto_p = false;
18273 required_token missing = RT_NONE;
18275 /* Look for the `asm' keyword. */
18276 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
18278 if (parser->in_function_body
18279 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
18281 error ("%<asm%> in %<constexpr%> function");
18282 cp_function_chain->invalid_constexpr = true;
18285 /* See if the next token is `volatile'. */
18286 if (cp_parser_allow_gnu_extensions_p (parser)
18287 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
18289 /* Remember that we saw the `volatile' keyword. */
18290 volatile_p = true;
18291 /* Consume the token. */
18292 cp_lexer_consume_token (parser->lexer);
18294 if (cp_parser_allow_gnu_extensions_p (parser)
18295 && parser->in_function_body
18296 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
18298 /* Remember that we saw the `goto' keyword. */
18299 goto_p = true;
18300 /* Consume the token. */
18301 cp_lexer_consume_token (parser->lexer);
18303 /* Look for the opening `('. */
18304 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18305 return;
18306 /* Look for the string. */
18307 string = cp_parser_string_literal (parser, false, false);
18308 if (string == error_mark_node)
18310 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18311 /*consume_paren=*/true);
18312 return;
18315 /* If we're allowing GNU extensions, check for the extended assembly
18316 syntax. Unfortunately, the `:' tokens need not be separated by
18317 a space in C, and so, for compatibility, we tolerate that here
18318 too. Doing that means that we have to treat the `::' operator as
18319 two `:' tokens. */
18320 if (cp_parser_allow_gnu_extensions_p (parser)
18321 && parser->in_function_body
18322 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
18323 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
18325 bool inputs_p = false;
18326 bool clobbers_p = false;
18327 bool labels_p = false;
18329 /* The extended syntax was used. */
18330 extended_p = true;
18332 /* Look for outputs. */
18333 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18335 /* Consume the `:'. */
18336 cp_lexer_consume_token (parser->lexer);
18337 /* Parse the output-operands. */
18338 if (cp_lexer_next_token_is_not (parser->lexer,
18339 CPP_COLON)
18340 && cp_lexer_next_token_is_not (parser->lexer,
18341 CPP_SCOPE)
18342 && cp_lexer_next_token_is_not (parser->lexer,
18343 CPP_CLOSE_PAREN)
18344 && !goto_p)
18346 outputs = cp_parser_asm_operand_list (parser);
18347 if (outputs == error_mark_node)
18348 invalid_outputs_p = true;
18351 /* If the next token is `::', there are no outputs, and the
18352 next token is the beginning of the inputs. */
18353 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18354 /* The inputs are coming next. */
18355 inputs_p = true;
18357 /* Look for inputs. */
18358 if (inputs_p
18359 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18361 /* Consume the `:' or `::'. */
18362 cp_lexer_consume_token (parser->lexer);
18363 /* Parse the output-operands. */
18364 if (cp_lexer_next_token_is_not (parser->lexer,
18365 CPP_COLON)
18366 && cp_lexer_next_token_is_not (parser->lexer,
18367 CPP_SCOPE)
18368 && cp_lexer_next_token_is_not (parser->lexer,
18369 CPP_CLOSE_PAREN))
18371 inputs = cp_parser_asm_operand_list (parser);
18372 if (inputs == error_mark_node)
18373 invalid_inputs_p = true;
18376 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18377 /* The clobbers are coming next. */
18378 clobbers_p = true;
18380 /* Look for clobbers. */
18381 if (clobbers_p
18382 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18384 clobbers_p = true;
18385 /* Consume the `:' or `::'. */
18386 cp_lexer_consume_token (parser->lexer);
18387 /* Parse the clobbers. */
18388 if (cp_lexer_next_token_is_not (parser->lexer,
18389 CPP_COLON)
18390 && cp_lexer_next_token_is_not (parser->lexer,
18391 CPP_CLOSE_PAREN))
18392 clobbers = cp_parser_asm_clobber_list (parser);
18394 else if (goto_p
18395 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18396 /* The labels are coming next. */
18397 labels_p = true;
18399 /* Look for labels. */
18400 if (labels_p
18401 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
18403 labels_p = true;
18404 /* Consume the `:' or `::'. */
18405 cp_lexer_consume_token (parser->lexer);
18406 /* Parse the labels. */
18407 labels = cp_parser_asm_label_list (parser);
18410 if (goto_p && !labels_p)
18411 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
18413 else if (goto_p)
18414 missing = RT_COLON_SCOPE;
18416 /* Look for the closing `)'. */
18417 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
18418 missing ? missing : RT_CLOSE_PAREN))
18419 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18420 /*consume_paren=*/true);
18421 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18423 if (!invalid_inputs_p && !invalid_outputs_p)
18425 /* Create the ASM_EXPR. */
18426 if (parser->in_function_body)
18428 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
18429 inputs, clobbers, labels);
18430 /* If the extended syntax was not used, mark the ASM_EXPR. */
18431 if (!extended_p)
18433 tree temp = asm_stmt;
18434 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
18435 temp = TREE_OPERAND (temp, 0);
18437 ASM_INPUT_P (temp) = 1;
18440 else
18441 symtab->finalize_toplevel_asm (string);
18445 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
18446 type that comes from the decl-specifier-seq. */
18448 static tree
18449 strip_declarator_types (tree type, cp_declarator *declarator)
18451 for (cp_declarator *d = declarator; d;)
18452 switch (d->kind)
18454 case cdk_id:
18455 case cdk_error:
18456 d = NULL;
18457 break;
18459 default:
18460 if (TYPE_PTRMEMFUNC_P (type))
18461 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
18462 type = TREE_TYPE (type);
18463 d = d->declarator;
18464 break;
18467 return type;
18470 /* Declarators [gram.dcl.decl] */
18472 /* Parse an init-declarator.
18474 init-declarator:
18475 declarator initializer [opt]
18477 GNU Extension:
18479 init-declarator:
18480 declarator asm-specification [opt] attributes [opt] initializer [opt]
18482 function-definition:
18483 decl-specifier-seq [opt] declarator ctor-initializer [opt]
18484 function-body
18485 decl-specifier-seq [opt] declarator function-try-block
18487 GNU Extension:
18489 function-definition:
18490 __extension__ function-definition
18492 TM Extension:
18494 function-definition:
18495 decl-specifier-seq [opt] declarator function-transaction-block
18497 The DECL_SPECIFIERS apply to this declarator. Returns a
18498 representation of the entity declared. If MEMBER_P is TRUE, then
18499 this declarator appears in a class scope. The new DECL created by
18500 this declarator is returned.
18502 The CHECKS are access checks that should be performed once we know
18503 what entity is being declared (and, therefore, what classes have
18504 befriended it).
18506 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
18507 for a function-definition here as well. If the declarator is a
18508 declarator for a function-definition, *FUNCTION_DEFINITION_P will
18509 be TRUE upon return. By that point, the function-definition will
18510 have been completely parsed.
18512 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
18513 is FALSE.
18515 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
18516 parsed declaration if it is an uninitialized single declarator not followed
18517 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
18518 if present, will not be consumed. If returned, this declarator will be
18519 created with SD_INITIALIZED but will not call cp_finish_decl.
18521 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
18522 and there is an initializer, the pointed location_t is set to the
18523 location of the '=' or `(', or '{' in C++11 token introducing the
18524 initializer. */
18526 static tree
18527 cp_parser_init_declarator (cp_parser* parser,
18528 cp_decl_specifier_seq *decl_specifiers,
18529 vec<deferred_access_check, va_gc> *checks,
18530 bool function_definition_allowed_p,
18531 bool member_p,
18532 int declares_class_or_enum,
18533 bool* function_definition_p,
18534 tree* maybe_range_for_decl,
18535 location_t* init_loc,
18536 tree* auto_result)
18538 cp_token *token = NULL, *asm_spec_start_token = NULL,
18539 *attributes_start_token = NULL;
18540 cp_declarator *declarator;
18541 tree prefix_attributes;
18542 tree attributes = NULL;
18543 tree asm_specification;
18544 tree initializer;
18545 tree decl = NULL_TREE;
18546 tree scope;
18547 int is_initialized;
18548 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
18549 initialized with "= ..", CPP_OPEN_PAREN if initialized with
18550 "(...)". */
18551 enum cpp_ttype initialization_kind;
18552 bool is_direct_init = false;
18553 bool is_non_constant_init;
18554 int ctor_dtor_or_conv_p;
18555 bool friend_p = cp_parser_friend_p (decl_specifiers);
18556 tree pushed_scope = NULL_TREE;
18557 bool range_for_decl_p = false;
18558 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18559 location_t tmp_init_loc = UNKNOWN_LOCATION;
18561 /* Gather the attributes that were provided with the
18562 decl-specifiers. */
18563 prefix_attributes = decl_specifiers->attributes;
18565 /* Assume that this is not the declarator for a function
18566 definition. */
18567 if (function_definition_p)
18568 *function_definition_p = false;
18570 /* Default arguments are only permitted for function parameters. */
18571 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
18572 parser->default_arg_ok_p = false;
18574 /* Defer access checks while parsing the declarator; we cannot know
18575 what names are accessible until we know what is being
18576 declared. */
18577 resume_deferring_access_checks ();
18579 /* Parse the declarator. */
18580 token = cp_lexer_peek_token (parser->lexer);
18581 declarator
18582 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18583 &ctor_dtor_or_conv_p,
18584 /*parenthesized_p=*/NULL,
18585 member_p, friend_p);
18586 /* Gather up the deferred checks. */
18587 stop_deferring_access_checks ();
18589 parser->default_arg_ok_p = saved_default_arg_ok_p;
18591 /* If the DECLARATOR was erroneous, there's no need to go
18592 further. */
18593 if (declarator == cp_error_declarator)
18594 return error_mark_node;
18596 /* Check that the number of template-parameter-lists is OK. */
18597 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
18598 token->location))
18599 return error_mark_node;
18601 if (declares_class_or_enum & 2)
18602 cp_parser_check_for_definition_in_return_type (declarator,
18603 decl_specifiers->type,
18604 decl_specifiers->locations[ds_type_spec]);
18606 /* Figure out what scope the entity declared by the DECLARATOR is
18607 located in. `grokdeclarator' sometimes changes the scope, so
18608 we compute it now. */
18609 scope = get_scope_of_declarator (declarator);
18611 /* Perform any lookups in the declared type which were thought to be
18612 dependent, but are not in the scope of the declarator. */
18613 decl_specifiers->type
18614 = maybe_update_decl_type (decl_specifiers->type, scope);
18616 /* If we're allowing GNU extensions, look for an
18617 asm-specification. */
18618 if (cp_parser_allow_gnu_extensions_p (parser))
18620 /* Look for an asm-specification. */
18621 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
18622 asm_specification = cp_parser_asm_specification_opt (parser);
18624 else
18625 asm_specification = NULL_TREE;
18627 /* Look for attributes. */
18628 attributes_start_token = cp_lexer_peek_token (parser->lexer);
18629 attributes = cp_parser_attributes_opt (parser);
18631 /* Peek at the next token. */
18632 token = cp_lexer_peek_token (parser->lexer);
18634 bool bogus_implicit_tmpl = false;
18636 if (function_declarator_p (declarator))
18638 /* Check to see if the token indicates the start of a
18639 function-definition. */
18640 if (cp_parser_token_starts_function_definition_p (token))
18642 if (!function_definition_allowed_p)
18644 /* If a function-definition should not appear here, issue an
18645 error message. */
18646 cp_parser_error (parser,
18647 "a function-definition is not allowed here");
18648 return error_mark_node;
18651 location_t func_brace_location
18652 = cp_lexer_peek_token (parser->lexer)->location;
18654 /* Neither attributes nor an asm-specification are allowed
18655 on a function-definition. */
18656 if (asm_specification)
18657 error_at (asm_spec_start_token->location,
18658 "an asm-specification is not allowed "
18659 "on a function-definition");
18660 if (attributes)
18661 error_at (attributes_start_token->location,
18662 "attributes are not allowed "
18663 "on a function-definition");
18664 /* This is a function-definition. */
18665 *function_definition_p = true;
18667 /* Parse the function definition. */
18668 if (member_p)
18669 decl = cp_parser_save_member_function_body (parser,
18670 decl_specifiers,
18671 declarator,
18672 prefix_attributes);
18673 else
18674 decl =
18675 (cp_parser_function_definition_from_specifiers_and_declarator
18676 (parser, decl_specifiers, prefix_attributes, declarator));
18678 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
18680 /* This is where the prologue starts... */
18681 DECL_STRUCT_FUNCTION (decl)->function_start_locus
18682 = func_brace_location;
18685 return decl;
18688 else if (parser->fully_implicit_function_template_p)
18690 /* A non-template declaration involving a function parameter list
18691 containing an implicit template parameter will be made into a
18692 template. If the resulting declaration is not going to be an
18693 actual function then finish the template scope here to prevent it.
18694 An error message will be issued once we have a decl to talk about.
18696 FIXME probably we should do type deduction rather than create an
18697 implicit template, but the standard currently doesn't allow it. */
18698 bogus_implicit_tmpl = true;
18699 finish_fully_implicit_template (parser, NULL_TREE);
18702 /* [dcl.dcl]
18704 Only in function declarations for constructors, destructors, and
18705 type conversions can the decl-specifier-seq be omitted.
18707 We explicitly postpone this check past the point where we handle
18708 function-definitions because we tolerate function-definitions
18709 that are missing their return types in some modes. */
18710 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
18712 cp_parser_error (parser,
18713 "expected constructor, destructor, or type conversion");
18714 return error_mark_node;
18717 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
18718 if (token->type == CPP_EQ
18719 || token->type == CPP_OPEN_PAREN
18720 || token->type == CPP_OPEN_BRACE)
18722 is_initialized = SD_INITIALIZED;
18723 initialization_kind = token->type;
18724 if (maybe_range_for_decl)
18725 *maybe_range_for_decl = error_mark_node;
18726 tmp_init_loc = token->location;
18727 if (init_loc && *init_loc == UNKNOWN_LOCATION)
18728 *init_loc = tmp_init_loc;
18730 if (token->type == CPP_EQ
18731 && function_declarator_p (declarator))
18733 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
18734 if (t2->keyword == RID_DEFAULT)
18735 is_initialized = SD_DEFAULTED;
18736 else if (t2->keyword == RID_DELETE)
18737 is_initialized = SD_DELETED;
18740 else
18742 /* If the init-declarator isn't initialized and isn't followed by a
18743 `,' or `;', it's not a valid init-declarator. */
18744 if (token->type != CPP_COMMA
18745 && token->type != CPP_SEMICOLON)
18747 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
18748 range_for_decl_p = true;
18749 else
18751 if (!maybe_range_for_decl)
18752 cp_parser_error (parser, "expected initializer");
18753 return error_mark_node;
18756 is_initialized = SD_UNINITIALIZED;
18757 initialization_kind = CPP_EOF;
18760 /* Because start_decl has side-effects, we should only call it if we
18761 know we're going ahead. By this point, we know that we cannot
18762 possibly be looking at any other construct. */
18763 cp_parser_commit_to_tentative_parse (parser);
18765 /* Enter the newly declared entry in the symbol table. If we're
18766 processing a declaration in a class-specifier, we wait until
18767 after processing the initializer. */
18768 if (!member_p)
18770 if (parser->in_unbraced_linkage_specification_p)
18771 decl_specifiers->storage_class = sc_extern;
18772 decl = start_decl (declarator, decl_specifiers,
18773 range_for_decl_p? SD_INITIALIZED : is_initialized,
18774 attributes, prefix_attributes, &pushed_scope);
18775 cp_finalize_omp_declare_simd (parser, decl);
18776 cp_finalize_oacc_routine (parser, decl, false);
18777 /* Adjust location of decl if declarator->id_loc is more appropriate:
18778 set, and decl wasn't merged with another decl, in which case its
18779 location would be different from input_location, and more accurate. */
18780 if (DECL_P (decl)
18781 && declarator->id_loc != UNKNOWN_LOCATION
18782 && DECL_SOURCE_LOCATION (decl) == input_location)
18783 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
18785 else if (scope)
18786 /* Enter the SCOPE. That way unqualified names appearing in the
18787 initializer will be looked up in SCOPE. */
18788 pushed_scope = push_scope (scope);
18790 /* Perform deferred access control checks, now that we know in which
18791 SCOPE the declared entity resides. */
18792 if (!member_p && decl)
18794 tree saved_current_function_decl = NULL_TREE;
18796 /* If the entity being declared is a function, pretend that we
18797 are in its scope. If it is a `friend', it may have access to
18798 things that would not otherwise be accessible. */
18799 if (TREE_CODE (decl) == FUNCTION_DECL)
18801 saved_current_function_decl = current_function_decl;
18802 current_function_decl = decl;
18805 /* Perform access checks for template parameters. */
18806 cp_parser_perform_template_parameter_access_checks (checks);
18808 /* Perform the access control checks for the declarator and the
18809 decl-specifiers. */
18810 perform_deferred_access_checks (tf_warning_or_error);
18812 /* Restore the saved value. */
18813 if (TREE_CODE (decl) == FUNCTION_DECL)
18814 current_function_decl = saved_current_function_decl;
18817 /* Parse the initializer. */
18818 initializer = NULL_TREE;
18819 is_direct_init = false;
18820 is_non_constant_init = true;
18821 if (is_initialized)
18823 if (function_declarator_p (declarator))
18825 if (initialization_kind == CPP_EQ)
18826 initializer = cp_parser_pure_specifier (parser);
18827 else
18829 /* If the declaration was erroneous, we don't really
18830 know what the user intended, so just silently
18831 consume the initializer. */
18832 if (decl != error_mark_node)
18833 error_at (tmp_init_loc, "initializer provided for function");
18834 cp_parser_skip_to_closing_parenthesis (parser,
18835 /*recovering=*/true,
18836 /*or_comma=*/false,
18837 /*consume_paren=*/true);
18840 else
18842 /* We want to record the extra mangling scope for in-class
18843 initializers of class members and initializers of static data
18844 member templates. The former involves deferring
18845 parsing of the initializer until end of class as with default
18846 arguments. So right here we only handle the latter. */
18847 if (!member_p && processing_template_decl)
18848 start_lambda_scope (decl);
18849 initializer = cp_parser_initializer (parser,
18850 &is_direct_init,
18851 &is_non_constant_init);
18852 if (!member_p && processing_template_decl)
18853 finish_lambda_scope ();
18854 if (initializer == error_mark_node)
18855 cp_parser_skip_to_end_of_statement (parser);
18859 /* The old parser allows attributes to appear after a parenthesized
18860 initializer. Mark Mitchell proposed removing this functionality
18861 on the GCC mailing lists on 2002-08-13. This parser accepts the
18862 attributes -- but ignores them. */
18863 if (cp_parser_allow_gnu_extensions_p (parser)
18864 && initialization_kind == CPP_OPEN_PAREN)
18865 if (cp_parser_attributes_opt (parser))
18866 warning (OPT_Wattributes,
18867 "attributes after parenthesized initializer ignored");
18869 /* And now complain about a non-function implicit template. */
18870 if (bogus_implicit_tmpl && decl != error_mark_node)
18871 error_at (DECL_SOURCE_LOCATION (decl),
18872 "non-function %qD declared as implicit template", decl);
18874 /* For an in-class declaration, use `grokfield' to create the
18875 declaration. */
18876 if (member_p)
18878 if (pushed_scope)
18880 pop_scope (pushed_scope);
18881 pushed_scope = NULL_TREE;
18883 decl = grokfield (declarator, decl_specifiers,
18884 initializer, !is_non_constant_init,
18885 /*asmspec=*/NULL_TREE,
18886 chainon (attributes, prefix_attributes));
18887 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
18888 cp_parser_save_default_args (parser, decl);
18889 cp_finalize_omp_declare_simd (parser, decl);
18890 cp_finalize_oacc_routine (parser, decl, false);
18893 /* Finish processing the declaration. But, skip member
18894 declarations. */
18895 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
18897 cp_finish_decl (decl,
18898 initializer, !is_non_constant_init,
18899 asm_specification,
18900 /* If the initializer is in parentheses, then this is
18901 a direct-initialization, which means that an
18902 `explicit' constructor is OK. Otherwise, an
18903 `explicit' constructor cannot be used. */
18904 ((is_direct_init || !is_initialized)
18905 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
18907 else if ((cxx_dialect != cxx98) && friend_p
18908 && decl && TREE_CODE (decl) == FUNCTION_DECL)
18909 /* Core issue #226 (C++0x only): A default template-argument
18910 shall not be specified in a friend class template
18911 declaration. */
18912 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
18913 /*is_partial=*/false, /*is_friend_decl=*/1);
18915 if (!friend_p && pushed_scope)
18916 pop_scope (pushed_scope);
18918 if (function_declarator_p (declarator)
18919 && parser->fully_implicit_function_template_p)
18921 if (member_p)
18922 decl = finish_fully_implicit_template (parser, decl);
18923 else
18924 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
18927 if (auto_result && is_initialized && decl_specifiers->type
18928 && type_uses_auto (decl_specifiers->type))
18929 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
18931 return decl;
18934 /* Parse a declarator.
18936 declarator:
18937 direct-declarator
18938 ptr-operator declarator
18940 abstract-declarator:
18941 ptr-operator abstract-declarator [opt]
18942 direct-abstract-declarator
18944 GNU Extensions:
18946 declarator:
18947 attributes [opt] direct-declarator
18948 attributes [opt] ptr-operator declarator
18950 abstract-declarator:
18951 attributes [opt] ptr-operator abstract-declarator [opt]
18952 attributes [opt] direct-abstract-declarator
18954 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
18955 detect constructor, destructor or conversion operators. It is set
18956 to -1 if the declarator is a name, and +1 if it is a
18957 function. Otherwise it is set to zero. Usually you just want to
18958 test for >0, but internally the negative value is used.
18960 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
18961 a decl-specifier-seq unless it declares a constructor, destructor,
18962 or conversion. It might seem that we could check this condition in
18963 semantic analysis, rather than parsing, but that makes it difficult
18964 to handle something like `f()'. We want to notice that there are
18965 no decl-specifiers, and therefore realize that this is an
18966 expression, not a declaration.)
18968 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
18969 the declarator is a direct-declarator of the form "(...)".
18971 MEMBER_P is true iff this declarator is a member-declarator.
18973 FRIEND_P is true iff this declarator is a friend. */
18975 static cp_declarator *
18976 cp_parser_declarator (cp_parser* parser,
18977 cp_parser_declarator_kind dcl_kind,
18978 int* ctor_dtor_or_conv_p,
18979 bool* parenthesized_p,
18980 bool member_p, bool friend_p)
18982 cp_declarator *declarator;
18983 enum tree_code code;
18984 cp_cv_quals cv_quals;
18985 tree class_type;
18986 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
18988 /* Assume this is not a constructor, destructor, or type-conversion
18989 operator. */
18990 if (ctor_dtor_or_conv_p)
18991 *ctor_dtor_or_conv_p = 0;
18993 if (cp_parser_allow_gnu_extensions_p (parser))
18994 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
18996 /* Check for the ptr-operator production. */
18997 cp_parser_parse_tentatively (parser);
18998 /* Parse the ptr-operator. */
18999 code = cp_parser_ptr_operator (parser,
19000 &class_type,
19001 &cv_quals,
19002 &std_attributes);
19004 /* If that worked, then we have a ptr-operator. */
19005 if (cp_parser_parse_definitely (parser))
19007 /* If a ptr-operator was found, then this declarator was not
19008 parenthesized. */
19009 if (parenthesized_p)
19010 *parenthesized_p = true;
19011 /* The dependent declarator is optional if we are parsing an
19012 abstract-declarator. */
19013 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19014 cp_parser_parse_tentatively (parser);
19016 /* Parse the dependent declarator. */
19017 declarator = cp_parser_declarator (parser, dcl_kind,
19018 /*ctor_dtor_or_conv_p=*/NULL,
19019 /*parenthesized_p=*/NULL,
19020 /*member_p=*/false,
19021 friend_p);
19023 /* If we are parsing an abstract-declarator, we must handle the
19024 case where the dependent declarator is absent. */
19025 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19026 && !cp_parser_parse_definitely (parser))
19027 declarator = NULL;
19029 declarator = cp_parser_make_indirect_declarator
19030 (code, class_type, cv_quals, declarator, std_attributes);
19032 /* Everything else is a direct-declarator. */
19033 else
19035 if (parenthesized_p)
19036 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19037 CPP_OPEN_PAREN);
19038 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19039 ctor_dtor_or_conv_p,
19040 member_p, friend_p);
19043 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19044 declarator->attributes = gnu_attributes;
19045 return declarator;
19048 /* Parse a direct-declarator or direct-abstract-declarator.
19050 direct-declarator:
19051 declarator-id
19052 direct-declarator ( parameter-declaration-clause )
19053 cv-qualifier-seq [opt]
19054 ref-qualifier [opt]
19055 exception-specification [opt]
19056 direct-declarator [ constant-expression [opt] ]
19057 ( declarator )
19059 direct-abstract-declarator:
19060 direct-abstract-declarator [opt]
19061 ( parameter-declaration-clause )
19062 cv-qualifier-seq [opt]
19063 ref-qualifier [opt]
19064 exception-specification [opt]
19065 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19066 ( abstract-declarator )
19068 Returns a representation of the declarator. DCL_KIND is
19069 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19070 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19071 we are parsing a direct-declarator. It is
19072 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19073 of ambiguity we prefer an abstract declarator, as per
19074 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19075 as for cp_parser_declarator. */
19077 static cp_declarator *
19078 cp_parser_direct_declarator (cp_parser* parser,
19079 cp_parser_declarator_kind dcl_kind,
19080 int* ctor_dtor_or_conv_p,
19081 bool member_p, bool friend_p)
19083 cp_token *token;
19084 cp_declarator *declarator = NULL;
19085 tree scope = NULL_TREE;
19086 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19087 bool saved_in_declarator_p = parser->in_declarator_p;
19088 bool first = true;
19089 tree pushed_scope = NULL_TREE;
19091 while (true)
19093 /* Peek at the next token. */
19094 token = cp_lexer_peek_token (parser->lexer);
19095 if (token->type == CPP_OPEN_PAREN)
19097 /* This is either a parameter-declaration-clause, or a
19098 parenthesized declarator. When we know we are parsing a
19099 named declarator, it must be a parenthesized declarator
19100 if FIRST is true. For instance, `(int)' is a
19101 parameter-declaration-clause, with an omitted
19102 direct-abstract-declarator. But `((*))', is a
19103 parenthesized abstract declarator. Finally, when T is a
19104 template parameter `(T)' is a
19105 parameter-declaration-clause, and not a parenthesized
19106 named declarator.
19108 We first try and parse a parameter-declaration-clause,
19109 and then try a nested declarator (if FIRST is true).
19111 It is not an error for it not to be a
19112 parameter-declaration-clause, even when FIRST is
19113 false. Consider,
19115 int i (int);
19116 int i (3);
19118 The first is the declaration of a function while the
19119 second is the definition of a variable, including its
19120 initializer.
19122 Having seen only the parenthesis, we cannot know which of
19123 these two alternatives should be selected. Even more
19124 complex are examples like:
19126 int i (int (a));
19127 int i (int (3));
19129 The former is a function-declaration; the latter is a
19130 variable initialization.
19132 Thus again, we try a parameter-declaration-clause, and if
19133 that fails, we back out and return. */
19135 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19137 tree params;
19138 bool is_declarator = false;
19140 /* In a member-declarator, the only valid interpretation
19141 of a parenthesis is the start of a
19142 parameter-declaration-clause. (It is invalid to
19143 initialize a static data member with a parenthesized
19144 initializer; only the "=" form of initialization is
19145 permitted.) */
19146 if (!member_p)
19147 cp_parser_parse_tentatively (parser);
19149 /* Consume the `('. */
19150 cp_lexer_consume_token (parser->lexer);
19151 if (first)
19153 /* If this is going to be an abstract declarator, we're
19154 in a declarator and we can't have default args. */
19155 parser->default_arg_ok_p = false;
19156 parser->in_declarator_p = true;
19159 begin_scope (sk_function_parms, NULL_TREE);
19161 /* Parse the parameter-declaration-clause. */
19162 params = cp_parser_parameter_declaration_clause (parser);
19164 /* Consume the `)'. */
19165 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19167 /* If all went well, parse the cv-qualifier-seq,
19168 ref-qualifier and the exception-specification. */
19169 if (member_p || cp_parser_parse_definitely (parser))
19171 cp_cv_quals cv_quals;
19172 cp_virt_specifiers virt_specifiers;
19173 cp_ref_qualifier ref_qual;
19174 tree exception_specification;
19175 tree late_return;
19176 tree attrs;
19177 bool memfn = (member_p || (pushed_scope
19178 && CLASS_TYPE_P (pushed_scope)));
19180 is_declarator = true;
19182 if (ctor_dtor_or_conv_p)
19183 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
19184 first = false;
19186 /* Parse the cv-qualifier-seq. */
19187 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19188 /* Parse the ref-qualifier. */
19189 ref_qual = cp_parser_ref_qualifier_opt (parser);
19190 /* Parse the tx-qualifier. */
19191 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
19192 /* And the exception-specification. */
19193 exception_specification
19194 = cp_parser_exception_specification_opt (parser);
19196 attrs = cp_parser_std_attribute_spec_seq (parser);
19198 /* In here, we handle cases where attribute is used after
19199 the function declaration. For example:
19200 void func (int x) __attribute__((vector(..))); */
19201 tree gnu_attrs = NULL_TREE;
19202 if (flag_cilkplus
19203 && cp_next_tokens_can_be_gnu_attribute_p (parser))
19205 cp_parser_parse_tentatively (parser);
19206 tree attr = cp_parser_gnu_attributes_opt (parser);
19207 if (cp_lexer_next_token_is_not (parser->lexer,
19208 CPP_SEMICOLON)
19209 && cp_lexer_next_token_is_not (parser->lexer,
19210 CPP_OPEN_BRACE))
19211 cp_parser_abort_tentative_parse (parser);
19212 else if (!cp_parser_parse_definitely (parser))
19214 else
19215 gnu_attrs = attr;
19217 tree requires_clause = NULL_TREE;
19218 late_return = (cp_parser_late_return_type_opt
19219 (parser, declarator, requires_clause,
19220 memfn ? cv_quals : -1));
19222 /* Parse the virt-specifier-seq. */
19223 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19225 /* Create the function-declarator. */
19226 declarator = make_call_declarator (declarator,
19227 params,
19228 cv_quals,
19229 virt_specifiers,
19230 ref_qual,
19231 tx_qual,
19232 exception_specification,
19233 late_return,
19234 requires_clause);
19235 declarator->std_attributes = attrs;
19236 declarator->attributes = gnu_attrs;
19237 /* Any subsequent parameter lists are to do with
19238 return type, so are not those of the declared
19239 function. */
19240 parser->default_arg_ok_p = false;
19243 /* Remove the function parms from scope. */
19244 pop_bindings_and_leave_scope ();
19246 if (is_declarator)
19247 /* Repeat the main loop. */
19248 continue;
19251 /* If this is the first, we can try a parenthesized
19252 declarator. */
19253 if (first)
19255 bool saved_in_type_id_in_expr_p;
19257 parser->default_arg_ok_p = saved_default_arg_ok_p;
19258 parser->in_declarator_p = saved_in_declarator_p;
19260 /* Consume the `('. */
19261 cp_lexer_consume_token (parser->lexer);
19262 /* Parse the nested declarator. */
19263 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19264 parser->in_type_id_in_expr_p = true;
19265 declarator
19266 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
19267 /*parenthesized_p=*/NULL,
19268 member_p, friend_p);
19269 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19270 first = false;
19271 /* Expect a `)'. */
19272 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
19273 declarator = cp_error_declarator;
19274 if (declarator == cp_error_declarator)
19275 break;
19277 goto handle_declarator;
19279 /* Otherwise, we must be done. */
19280 else
19281 break;
19283 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19284 && token->type == CPP_OPEN_SQUARE
19285 && !cp_next_tokens_can_be_attribute_p (parser))
19287 /* Parse an array-declarator. */
19288 tree bounds, attrs;
19290 if (ctor_dtor_or_conv_p)
19291 *ctor_dtor_or_conv_p = 0;
19293 first = false;
19294 parser->default_arg_ok_p = false;
19295 parser->in_declarator_p = true;
19296 /* Consume the `['. */
19297 cp_lexer_consume_token (parser->lexer);
19298 /* Peek at the next token. */
19299 token = cp_lexer_peek_token (parser->lexer);
19300 /* If the next token is `]', then there is no
19301 constant-expression. */
19302 if (token->type != CPP_CLOSE_SQUARE)
19304 bool non_constant_p;
19305 bounds
19306 = cp_parser_constant_expression (parser,
19307 /*allow_non_constant=*/true,
19308 &non_constant_p);
19309 if (!non_constant_p)
19310 /* OK */;
19311 else if (error_operand_p (bounds))
19312 /* Already gave an error. */;
19313 else if (!parser->in_function_body
19314 || current_binding_level->kind == sk_function_parms)
19316 /* Normally, the array bound must be an integral constant
19317 expression. However, as an extension, we allow VLAs
19318 in function scopes as long as they aren't part of a
19319 parameter declaration. */
19320 cp_parser_error (parser,
19321 "array bound is not an integer constant");
19322 bounds = error_mark_node;
19324 else if (processing_template_decl
19325 && !type_dependent_expression_p (bounds))
19327 /* Remember this wasn't a constant-expression. */
19328 bounds = build_nop (TREE_TYPE (bounds), bounds);
19329 TREE_SIDE_EFFECTS (bounds) = 1;
19332 else
19333 bounds = NULL_TREE;
19334 /* Look for the closing `]'. */
19335 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
19337 declarator = cp_error_declarator;
19338 break;
19341 attrs = cp_parser_std_attribute_spec_seq (parser);
19342 declarator = make_array_declarator (declarator, bounds);
19343 declarator->std_attributes = attrs;
19345 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
19348 tree qualifying_scope;
19349 tree unqualified_name;
19350 tree attrs;
19351 special_function_kind sfk;
19352 bool abstract_ok;
19353 bool pack_expansion_p = false;
19354 cp_token *declarator_id_start_token;
19356 /* Parse a declarator-id */
19357 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
19358 if (abstract_ok)
19360 cp_parser_parse_tentatively (parser);
19362 /* If we see an ellipsis, we should be looking at a
19363 parameter pack. */
19364 if (token->type == CPP_ELLIPSIS)
19366 /* Consume the `...' */
19367 cp_lexer_consume_token (parser->lexer);
19369 pack_expansion_p = true;
19373 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
19374 unqualified_name
19375 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
19376 qualifying_scope = parser->scope;
19377 if (abstract_ok)
19379 bool okay = false;
19381 if (!unqualified_name && pack_expansion_p)
19383 /* Check whether an error occurred. */
19384 okay = !cp_parser_error_occurred (parser);
19386 /* We already consumed the ellipsis to mark a
19387 parameter pack, but we have no way to report it,
19388 so abort the tentative parse. We will be exiting
19389 immediately anyway. */
19390 cp_parser_abort_tentative_parse (parser);
19392 else
19393 okay = cp_parser_parse_definitely (parser);
19395 if (!okay)
19396 unqualified_name = error_mark_node;
19397 else if (unqualified_name
19398 && (qualifying_scope
19399 || (!identifier_p (unqualified_name))))
19401 cp_parser_error (parser, "expected unqualified-id");
19402 unqualified_name = error_mark_node;
19406 if (!unqualified_name)
19407 return NULL;
19408 if (unqualified_name == error_mark_node)
19410 declarator = cp_error_declarator;
19411 pack_expansion_p = false;
19412 declarator->parameter_pack_p = false;
19413 break;
19416 attrs = cp_parser_std_attribute_spec_seq (parser);
19418 if (qualifying_scope && at_namespace_scope_p ()
19419 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
19421 /* In the declaration of a member of a template class
19422 outside of the class itself, the SCOPE will sometimes
19423 be a TYPENAME_TYPE. For example, given:
19425 template <typename T>
19426 int S<T>::R::i = 3;
19428 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
19429 this context, we must resolve S<T>::R to an ordinary
19430 type, rather than a typename type.
19432 The reason we normally avoid resolving TYPENAME_TYPEs
19433 is that a specialization of `S' might render
19434 `S<T>::R' not a type. However, if `S' is
19435 specialized, then this `i' will not be used, so there
19436 is no harm in resolving the types here. */
19437 tree type;
19439 /* Resolve the TYPENAME_TYPE. */
19440 type = resolve_typename_type (qualifying_scope,
19441 /*only_current_p=*/false);
19442 /* If that failed, the declarator is invalid. */
19443 if (TREE_CODE (type) == TYPENAME_TYPE)
19445 if (typedef_variant_p (type))
19446 error_at (declarator_id_start_token->location,
19447 "cannot define member of dependent typedef "
19448 "%qT", type);
19449 else
19450 error_at (declarator_id_start_token->location,
19451 "%<%T::%E%> is not a type",
19452 TYPE_CONTEXT (qualifying_scope),
19453 TYPE_IDENTIFIER (qualifying_scope));
19455 qualifying_scope = type;
19458 sfk = sfk_none;
19460 if (unqualified_name)
19462 tree class_type;
19464 if (qualifying_scope
19465 && CLASS_TYPE_P (qualifying_scope))
19466 class_type = qualifying_scope;
19467 else
19468 class_type = current_class_type;
19470 if (TREE_CODE (unqualified_name) == TYPE_DECL)
19472 tree name_type = TREE_TYPE (unqualified_name);
19473 if (class_type && same_type_p (name_type, class_type))
19475 if (qualifying_scope
19476 && CLASSTYPE_USE_TEMPLATE (name_type))
19478 error_at (declarator_id_start_token->location,
19479 "invalid use of constructor as a template");
19480 inform (declarator_id_start_token->location,
19481 "use %<%T::%D%> instead of %<%T::%D%> to "
19482 "name the constructor in a qualified name",
19483 class_type,
19484 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
19485 class_type, name_type);
19486 declarator = cp_error_declarator;
19487 break;
19489 else
19490 unqualified_name = constructor_name (class_type);
19492 else
19494 /* We do not attempt to print the declarator
19495 here because we do not have enough
19496 information about its original syntactic
19497 form. */
19498 cp_parser_error (parser, "invalid declarator");
19499 declarator = cp_error_declarator;
19500 break;
19504 if (class_type)
19506 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
19507 sfk = sfk_destructor;
19508 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
19509 sfk = sfk_conversion;
19510 else if (/* There's no way to declare a constructor
19511 for an unnamed type, even if the type
19512 got a name for linkage purposes. */
19513 !TYPE_WAS_UNNAMED (class_type)
19514 /* Handle correctly (c++/19200):
19516 struct S {
19517 struct T{};
19518 friend void S(T);
19521 and also:
19523 namespace N {
19524 void S();
19527 struct S {
19528 friend void N::S();
19529 }; */
19530 && !(friend_p
19531 && class_type != qualifying_scope)
19532 && constructor_name_p (unqualified_name,
19533 class_type))
19535 unqualified_name = constructor_name (class_type);
19536 sfk = sfk_constructor;
19538 else if (is_overloaded_fn (unqualified_name)
19539 && DECL_CONSTRUCTOR_P (get_first_fn
19540 (unqualified_name)))
19541 sfk = sfk_constructor;
19543 if (ctor_dtor_or_conv_p && sfk != sfk_none)
19544 *ctor_dtor_or_conv_p = -1;
19547 declarator = make_id_declarator (qualifying_scope,
19548 unqualified_name,
19549 sfk);
19550 declarator->std_attributes = attrs;
19551 declarator->id_loc = token->location;
19552 declarator->parameter_pack_p = pack_expansion_p;
19554 if (pack_expansion_p)
19555 maybe_warn_variadic_templates ();
19558 handle_declarator:;
19559 scope = get_scope_of_declarator (declarator);
19560 if (scope)
19562 /* Any names that appear after the declarator-id for a
19563 member are looked up in the containing scope. */
19564 if (at_function_scope_p ())
19566 /* But declarations with qualified-ids can't appear in a
19567 function. */
19568 cp_parser_error (parser, "qualified-id in declaration");
19569 declarator = cp_error_declarator;
19570 break;
19572 pushed_scope = push_scope (scope);
19574 parser->in_declarator_p = true;
19575 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
19576 || (declarator && declarator->kind == cdk_id))
19577 /* Default args are only allowed on function
19578 declarations. */
19579 parser->default_arg_ok_p = saved_default_arg_ok_p;
19580 else
19581 parser->default_arg_ok_p = false;
19583 first = false;
19585 /* We're done. */
19586 else
19587 break;
19590 /* For an abstract declarator, we might wind up with nothing at this
19591 point. That's an error; the declarator is not optional. */
19592 if (!declarator)
19593 cp_parser_error (parser, "expected declarator");
19595 /* If we entered a scope, we must exit it now. */
19596 if (pushed_scope)
19597 pop_scope (pushed_scope);
19599 parser->default_arg_ok_p = saved_default_arg_ok_p;
19600 parser->in_declarator_p = saved_in_declarator_p;
19602 return declarator;
19605 /* Parse a ptr-operator.
19607 ptr-operator:
19608 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
19609 * cv-qualifier-seq [opt]
19611 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
19612 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
19614 GNU Extension:
19616 ptr-operator:
19617 & cv-qualifier-seq [opt]
19619 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
19620 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
19621 an rvalue reference. In the case of a pointer-to-member, *TYPE is
19622 filled in with the TYPE containing the member. *CV_QUALS is
19623 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
19624 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
19625 Note that the tree codes returned by this function have nothing
19626 to do with the types of trees that will be eventually be created
19627 to represent the pointer or reference type being parsed. They are
19628 just constants with suggestive names. */
19629 static enum tree_code
19630 cp_parser_ptr_operator (cp_parser* parser,
19631 tree* type,
19632 cp_cv_quals *cv_quals,
19633 tree *attributes)
19635 enum tree_code code = ERROR_MARK;
19636 cp_token *token;
19637 tree attrs = NULL_TREE;
19639 /* Assume that it's not a pointer-to-member. */
19640 *type = NULL_TREE;
19641 /* And that there are no cv-qualifiers. */
19642 *cv_quals = TYPE_UNQUALIFIED;
19644 /* Peek at the next token. */
19645 token = cp_lexer_peek_token (parser->lexer);
19647 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
19648 if (token->type == CPP_MULT)
19649 code = INDIRECT_REF;
19650 else if (token->type == CPP_AND)
19651 code = ADDR_EXPR;
19652 else if ((cxx_dialect != cxx98) &&
19653 token->type == CPP_AND_AND) /* C++0x only */
19654 code = NON_LVALUE_EXPR;
19656 if (code != ERROR_MARK)
19658 /* Consume the `*', `&' or `&&'. */
19659 cp_lexer_consume_token (parser->lexer);
19661 /* A `*' can be followed by a cv-qualifier-seq, and so can a
19662 `&', if we are allowing GNU extensions. (The only qualifier
19663 that can legally appear after `&' is `restrict', but that is
19664 enforced during semantic analysis. */
19665 if (code == INDIRECT_REF
19666 || cp_parser_allow_gnu_extensions_p (parser))
19667 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19669 attrs = cp_parser_std_attribute_spec_seq (parser);
19670 if (attributes != NULL)
19671 *attributes = attrs;
19673 else
19675 /* Try the pointer-to-member case. */
19676 cp_parser_parse_tentatively (parser);
19677 /* Look for the optional `::' operator. */
19678 cp_parser_global_scope_opt (parser,
19679 /*current_scope_valid_p=*/false);
19680 /* Look for the nested-name specifier. */
19681 token = cp_lexer_peek_token (parser->lexer);
19682 cp_parser_nested_name_specifier (parser,
19683 /*typename_keyword_p=*/false,
19684 /*check_dependency_p=*/true,
19685 /*type_p=*/false,
19686 /*is_declaration=*/false);
19687 /* If we found it, and the next token is a `*', then we are
19688 indeed looking at a pointer-to-member operator. */
19689 if (!cp_parser_error_occurred (parser)
19690 && cp_parser_require (parser, CPP_MULT, RT_MULT))
19692 /* Indicate that the `*' operator was used. */
19693 code = INDIRECT_REF;
19695 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
19696 error_at (token->location, "%qD is a namespace", parser->scope);
19697 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
19698 error_at (token->location, "cannot form pointer to member of "
19699 "non-class %q#T", parser->scope);
19700 else
19702 /* The type of which the member is a member is given by the
19703 current SCOPE. */
19704 *type = parser->scope;
19705 /* The next name will not be qualified. */
19706 parser->scope = NULL_TREE;
19707 parser->qualifying_scope = NULL_TREE;
19708 parser->object_scope = NULL_TREE;
19709 /* Look for optional c++11 attributes. */
19710 attrs = cp_parser_std_attribute_spec_seq (parser);
19711 if (attributes != NULL)
19712 *attributes = attrs;
19713 /* Look for the optional cv-qualifier-seq. */
19714 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19717 /* If that didn't work we don't have a ptr-operator. */
19718 if (!cp_parser_parse_definitely (parser))
19719 cp_parser_error (parser, "expected ptr-operator");
19722 return code;
19725 /* Parse an (optional) cv-qualifier-seq.
19727 cv-qualifier-seq:
19728 cv-qualifier cv-qualifier-seq [opt]
19730 cv-qualifier:
19731 const
19732 volatile
19734 GNU Extension:
19736 cv-qualifier:
19737 __restrict__
19739 Returns a bitmask representing the cv-qualifiers. */
19741 static cp_cv_quals
19742 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
19744 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
19746 while (true)
19748 cp_token *token;
19749 cp_cv_quals cv_qualifier;
19751 /* Peek at the next token. */
19752 token = cp_lexer_peek_token (parser->lexer);
19753 /* See if it's a cv-qualifier. */
19754 switch (token->keyword)
19756 case RID_CONST:
19757 cv_qualifier = TYPE_QUAL_CONST;
19758 break;
19760 case RID_VOLATILE:
19761 cv_qualifier = TYPE_QUAL_VOLATILE;
19762 break;
19764 case RID_RESTRICT:
19765 cv_qualifier = TYPE_QUAL_RESTRICT;
19766 break;
19768 default:
19769 cv_qualifier = TYPE_UNQUALIFIED;
19770 break;
19773 if (!cv_qualifier)
19774 break;
19776 if (cv_quals & cv_qualifier)
19778 error_at (token->location, "duplicate cv-qualifier");
19779 cp_lexer_purge_token (parser->lexer);
19781 else
19783 cp_lexer_consume_token (parser->lexer);
19784 cv_quals |= cv_qualifier;
19788 return cv_quals;
19791 /* Parse an (optional) ref-qualifier
19793 ref-qualifier:
19797 Returns cp_ref_qualifier representing ref-qualifier. */
19799 static cp_ref_qualifier
19800 cp_parser_ref_qualifier_opt (cp_parser* parser)
19802 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
19804 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
19805 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
19806 return ref_qual;
19808 while (true)
19810 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
19811 cp_token *token = cp_lexer_peek_token (parser->lexer);
19813 switch (token->type)
19815 case CPP_AND:
19816 curr_ref_qual = REF_QUAL_LVALUE;
19817 break;
19819 case CPP_AND_AND:
19820 curr_ref_qual = REF_QUAL_RVALUE;
19821 break;
19823 default:
19824 curr_ref_qual = REF_QUAL_NONE;
19825 break;
19828 if (!curr_ref_qual)
19829 break;
19830 else if (ref_qual)
19832 error_at (token->location, "multiple ref-qualifiers");
19833 cp_lexer_purge_token (parser->lexer);
19835 else
19837 ref_qual = curr_ref_qual;
19838 cp_lexer_consume_token (parser->lexer);
19842 return ref_qual;
19845 /* Parse an optional tx-qualifier.
19847 tx-qualifier:
19848 transaction_safe
19849 transaction_safe_dynamic */
19851 static tree
19852 cp_parser_tx_qualifier_opt (cp_parser *parser)
19854 cp_token *token = cp_lexer_peek_token (parser->lexer);
19855 if (token->type == CPP_NAME)
19857 tree name = token->u.value;
19858 const char *p = IDENTIFIER_POINTER (name);
19859 const int len = strlen ("transaction_safe");
19860 if (!strncmp (p, "transaction_safe", len))
19862 p += len;
19863 if (*p == '\0'
19864 || !strcmp (p, "_dynamic"))
19866 cp_lexer_consume_token (parser->lexer);
19867 if (!flag_tm)
19869 error ("%E requires %<-fgnu-tm%>", name);
19870 return NULL_TREE;
19872 else
19873 return name;
19877 return NULL_TREE;
19880 /* Parse an (optional) virt-specifier-seq.
19882 virt-specifier-seq:
19883 virt-specifier virt-specifier-seq [opt]
19885 virt-specifier:
19886 override
19887 final
19889 Returns a bitmask representing the virt-specifiers. */
19891 static cp_virt_specifiers
19892 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
19894 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19896 while (true)
19898 cp_token *token;
19899 cp_virt_specifiers virt_specifier;
19901 /* Peek at the next token. */
19902 token = cp_lexer_peek_token (parser->lexer);
19903 /* See if it's a virt-specifier-qualifier. */
19904 if (token->type != CPP_NAME)
19905 break;
19906 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
19908 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19909 virt_specifier = VIRT_SPEC_OVERRIDE;
19911 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
19913 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19914 virt_specifier = VIRT_SPEC_FINAL;
19916 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
19918 virt_specifier = VIRT_SPEC_FINAL;
19920 else
19921 break;
19923 if (virt_specifiers & virt_specifier)
19925 error_at (token->location, "duplicate virt-specifier");
19926 cp_lexer_purge_token (parser->lexer);
19928 else
19930 cp_lexer_consume_token (parser->lexer);
19931 virt_specifiers |= virt_specifier;
19934 return virt_specifiers;
19937 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
19938 is in scope even though it isn't real. */
19940 void
19941 inject_this_parameter (tree ctype, cp_cv_quals quals)
19943 tree this_parm;
19945 if (current_class_ptr)
19947 /* We don't clear this between NSDMIs. Is it already what we want? */
19948 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
19949 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
19950 && cp_type_quals (type) == quals)
19951 return;
19954 this_parm = build_this_parm (ctype, quals);
19955 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
19956 current_class_ptr = NULL_TREE;
19957 current_class_ref
19958 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
19959 current_class_ptr = this_parm;
19962 /* Return true iff our current scope is a non-static data member
19963 initializer. */
19965 bool
19966 parsing_nsdmi (void)
19968 /* We recognize NSDMI context by the context-less 'this' pointer set up
19969 by the function above. */
19970 if (current_class_ptr
19971 && TREE_CODE (current_class_ptr) == PARM_DECL
19972 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
19973 return true;
19974 return false;
19977 /* Parse a late-specified return type, if any. This is not a separate
19978 non-terminal, but part of a function declarator, which looks like
19980 -> trailing-type-specifier-seq abstract-declarator(opt)
19982 Returns the type indicated by the type-id.
19984 In addition to this, parse any queued up #pragma omp declare simd
19985 clauses, Cilk Plus SIMD-enabled functions' vector attributes, and
19986 #pragma acc routine clauses.
19988 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
19989 function. */
19991 static tree
19992 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
19993 tree& requires_clause, cp_cv_quals quals)
19995 cp_token *token;
19996 tree type = NULL_TREE;
19997 bool declare_simd_p = (parser->omp_declare_simd
19998 && declarator
19999 && declarator->kind == cdk_id);
20001 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
20002 && declarator && declarator->kind == cdk_id);
20004 bool oacc_routine_p = (parser->oacc_routine
20005 && declarator
20006 && declarator->kind == cdk_id);
20008 /* Peek at the next token. */
20009 token = cp_lexer_peek_token (parser->lexer);
20010 /* A late-specified return type is indicated by an initial '->'. */
20011 if (token->type != CPP_DEREF
20012 && token->keyword != RID_REQUIRES
20013 && !(token->type == CPP_NAME
20014 && token->u.value == ridpointers[RID_REQUIRES])
20015 && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
20016 return NULL_TREE;
20018 tree save_ccp = current_class_ptr;
20019 tree save_ccr = current_class_ref;
20020 if (quals >= 0)
20022 /* DR 1207: 'this' is in scope in the trailing return type. */
20023 inject_this_parameter (current_class_type, quals);
20026 if (token->type == CPP_DEREF)
20028 /* Consume the ->. */
20029 cp_lexer_consume_token (parser->lexer);
20031 type = cp_parser_trailing_type_id (parser);
20034 /* Function declarations may be followed by a trailing
20035 requires-clause. */
20036 requires_clause = cp_parser_requires_clause_opt (parser);
20038 if (cilk_simd_fn_vector_p)
20039 declarator->attributes
20040 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
20041 declarator->attributes);
20042 if (declare_simd_p)
20043 declarator->attributes
20044 = cp_parser_late_parsing_omp_declare_simd (parser,
20045 declarator->attributes);
20046 if (oacc_routine_p)
20047 declarator->attributes
20048 = cp_parser_late_parsing_oacc_routine (parser,
20049 declarator->attributes);
20051 if (quals >= 0)
20053 current_class_ptr = save_ccp;
20054 current_class_ref = save_ccr;
20057 return type;
20060 /* Parse a declarator-id.
20062 declarator-id:
20063 id-expression
20064 :: [opt] nested-name-specifier [opt] type-name
20066 In the `id-expression' case, the value returned is as for
20067 cp_parser_id_expression if the id-expression was an unqualified-id.
20068 If the id-expression was a qualified-id, then a SCOPE_REF is
20069 returned. The first operand is the scope (either a NAMESPACE_DECL
20070 or TREE_TYPE), but the second is still just a representation of an
20071 unqualified-id. */
20073 static tree
20074 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20076 tree id;
20077 /* The expression must be an id-expression. Assume that qualified
20078 names are the names of types so that:
20080 template <class T>
20081 int S<T>::R::i = 3;
20083 will work; we must treat `S<T>::R' as the name of a type.
20084 Similarly, assume that qualified names are templates, where
20085 required, so that:
20087 template <class T>
20088 int S<T>::R<T>::i = 3;
20090 will work, too. */
20091 id = cp_parser_id_expression (parser,
20092 /*template_keyword_p=*/false,
20093 /*check_dependency_p=*/false,
20094 /*template_p=*/NULL,
20095 /*declarator_p=*/true,
20096 optional_p);
20097 if (id && BASELINK_P (id))
20098 id = BASELINK_FUNCTIONS (id);
20099 return id;
20102 /* Parse a type-id.
20104 type-id:
20105 type-specifier-seq abstract-declarator [opt]
20107 Returns the TYPE specified. */
20109 static tree
20110 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
20111 bool is_trailing_return)
20113 cp_decl_specifier_seq type_specifier_seq;
20114 cp_declarator *abstract_declarator;
20116 /* Parse the type-specifier-seq. */
20117 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
20118 is_trailing_return,
20119 &type_specifier_seq);
20120 if (type_specifier_seq.type == error_mark_node)
20121 return error_mark_node;
20123 /* There might or might not be an abstract declarator. */
20124 cp_parser_parse_tentatively (parser);
20125 /* Look for the declarator. */
20126 abstract_declarator
20127 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
20128 /*parenthesized_p=*/NULL,
20129 /*member_p=*/false,
20130 /*friend_p=*/false);
20131 /* Check to see if there really was a declarator. */
20132 if (!cp_parser_parse_definitely (parser))
20133 abstract_declarator = NULL;
20135 if (type_specifier_seq.type
20136 /* The concepts TS allows 'auto' as a type-id. */
20137 && (!flag_concepts || parser->in_type_id_in_expr_p)
20138 /* None of the valid uses of 'auto' in C++14 involve the type-id
20139 nonterminal, but it is valid in a trailing-return-type. */
20140 && !(cxx_dialect >= cxx14 && is_trailing_return)
20141 && type_uses_auto (type_specifier_seq.type))
20143 /* A type-id with type 'auto' is only ok if the abstract declarator
20144 is a function declarator with a late-specified return type.
20146 A type-id with 'auto' is also valid in a trailing-return-type
20147 in a compound-requirement. */
20148 if (abstract_declarator
20149 && abstract_declarator->kind == cdk_function
20150 && abstract_declarator->u.function.late_return_type)
20151 /* OK */;
20152 else if (parser->in_result_type_constraint_p)
20153 /* OK */;
20154 else
20156 error ("invalid use of %<auto%>");
20157 return error_mark_node;
20161 return groktypename (&type_specifier_seq, abstract_declarator,
20162 is_template_arg);
20165 static tree
20166 cp_parser_type_id (cp_parser *parser)
20168 return cp_parser_type_id_1 (parser, false, false);
20171 static tree
20172 cp_parser_template_type_arg (cp_parser *parser)
20174 tree r;
20175 const char *saved_message = parser->type_definition_forbidden_message;
20176 parser->type_definition_forbidden_message
20177 = G_("types may not be defined in template arguments");
20178 r = cp_parser_type_id_1 (parser, true, false);
20179 parser->type_definition_forbidden_message = saved_message;
20180 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
20182 error ("invalid use of %<auto%> in template argument");
20183 r = error_mark_node;
20185 return r;
20188 static tree
20189 cp_parser_trailing_type_id (cp_parser *parser)
20191 return cp_parser_type_id_1 (parser, false, true);
20194 /* Parse a type-specifier-seq.
20196 type-specifier-seq:
20197 type-specifier type-specifier-seq [opt]
20199 GNU extension:
20201 type-specifier-seq:
20202 attributes type-specifier-seq [opt]
20204 If IS_DECLARATION is true, we are at the start of a "condition" or
20205 exception-declaration, so we might be followed by a declarator-id.
20207 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
20208 i.e. we've just seen "->".
20210 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
20212 static void
20213 cp_parser_type_specifier_seq (cp_parser* parser,
20214 bool is_declaration,
20215 bool is_trailing_return,
20216 cp_decl_specifier_seq *type_specifier_seq)
20218 bool seen_type_specifier = false;
20219 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
20220 cp_token *start_token = NULL;
20222 /* Clear the TYPE_SPECIFIER_SEQ. */
20223 clear_decl_specs (type_specifier_seq);
20225 /* In the context of a trailing return type, enum E { } is an
20226 elaborated-type-specifier followed by a function-body, not an
20227 enum-specifier. */
20228 if (is_trailing_return)
20229 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
20231 /* Parse the type-specifiers and attributes. */
20232 while (true)
20234 tree type_specifier;
20235 bool is_cv_qualifier;
20237 /* Check for attributes first. */
20238 if (cp_next_tokens_can_be_attribute_p (parser))
20240 type_specifier_seq->attributes =
20241 chainon (type_specifier_seq->attributes,
20242 cp_parser_attributes_opt (parser));
20243 continue;
20246 /* record the token of the beginning of the type specifier seq,
20247 for error reporting purposes*/
20248 if (!start_token)
20249 start_token = cp_lexer_peek_token (parser->lexer);
20251 /* Look for the type-specifier. */
20252 type_specifier = cp_parser_type_specifier (parser,
20253 flags,
20254 type_specifier_seq,
20255 /*is_declaration=*/false,
20256 NULL,
20257 &is_cv_qualifier);
20258 if (!type_specifier)
20260 /* If the first type-specifier could not be found, this is not a
20261 type-specifier-seq at all. */
20262 if (!seen_type_specifier)
20264 /* Set in_declarator_p to avoid skipping to the semicolon. */
20265 int in_decl = parser->in_declarator_p;
20266 parser->in_declarator_p = true;
20268 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
20269 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
20270 cp_parser_error (parser, "expected type-specifier");
20272 parser->in_declarator_p = in_decl;
20274 type_specifier_seq->type = error_mark_node;
20275 return;
20277 /* If subsequent type-specifiers could not be found, the
20278 type-specifier-seq is complete. */
20279 break;
20282 seen_type_specifier = true;
20283 /* The standard says that a condition can be:
20285 type-specifier-seq declarator = assignment-expression
20287 However, given:
20289 struct S {};
20290 if (int S = ...)
20292 we should treat the "S" as a declarator, not as a
20293 type-specifier. The standard doesn't say that explicitly for
20294 type-specifier-seq, but it does say that for
20295 decl-specifier-seq in an ordinary declaration. Perhaps it
20296 would be clearer just to allow a decl-specifier-seq here, and
20297 then add a semantic restriction that if any decl-specifiers
20298 that are not type-specifiers appear, the program is invalid. */
20299 if (is_declaration && !is_cv_qualifier)
20300 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
20304 /* Return whether the function currently being declared has an associated
20305 template parameter list. */
20307 static bool
20308 function_being_declared_is_template_p (cp_parser* parser)
20310 if (!current_template_parms || processing_template_parmlist)
20311 return false;
20313 if (parser->implicit_template_scope)
20314 return true;
20316 if (at_class_scope_p ()
20317 && TYPE_BEING_DEFINED (current_class_type))
20318 return parser->num_template_parameter_lists != 0;
20320 return ((int) parser->num_template_parameter_lists > template_class_depth
20321 (current_class_type));
20324 /* Parse a parameter-declaration-clause.
20326 parameter-declaration-clause:
20327 parameter-declaration-list [opt] ... [opt]
20328 parameter-declaration-list , ...
20330 Returns a representation for the parameter declarations. A return
20331 value of NULL indicates a parameter-declaration-clause consisting
20332 only of an ellipsis. */
20334 static tree
20335 cp_parser_parameter_declaration_clause (cp_parser* parser)
20337 tree parameters;
20338 cp_token *token;
20339 bool ellipsis_p;
20340 bool is_error;
20342 struct cleanup {
20343 cp_parser* parser;
20344 int auto_is_implicit_function_template_parm_p;
20345 ~cleanup() {
20346 parser->auto_is_implicit_function_template_parm_p
20347 = auto_is_implicit_function_template_parm_p;
20349 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
20351 (void) cleanup;
20353 if (!processing_specialization
20354 && !processing_template_parmlist
20355 && !processing_explicit_instantiation)
20356 if (!current_function_decl
20357 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
20358 parser->auto_is_implicit_function_template_parm_p = true;
20360 /* Peek at the next token. */
20361 token = cp_lexer_peek_token (parser->lexer);
20362 /* Check for trivial parameter-declaration-clauses. */
20363 if (token->type == CPP_ELLIPSIS)
20365 /* Consume the `...' token. */
20366 cp_lexer_consume_token (parser->lexer);
20367 return NULL_TREE;
20369 else if (token->type == CPP_CLOSE_PAREN)
20370 /* There are no parameters. */
20372 #ifndef NO_IMPLICIT_EXTERN_C
20373 if (in_system_header_at (input_location)
20374 && current_class_type == NULL
20375 && current_lang_name == lang_name_c)
20376 return NULL_TREE;
20377 else
20378 #endif
20379 return void_list_node;
20381 /* Check for `(void)', too, which is a special case. */
20382 else if (token->keyword == RID_VOID
20383 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20384 == CPP_CLOSE_PAREN))
20386 /* Consume the `void' token. */
20387 cp_lexer_consume_token (parser->lexer);
20388 /* There are no parameters. */
20389 return void_list_node;
20392 /* Parse the parameter-declaration-list. */
20393 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
20394 /* If a parse error occurred while parsing the
20395 parameter-declaration-list, then the entire
20396 parameter-declaration-clause is erroneous. */
20397 if (is_error)
20398 return NULL;
20400 /* Peek at the next token. */
20401 token = cp_lexer_peek_token (parser->lexer);
20402 /* If it's a `,', the clause should terminate with an ellipsis. */
20403 if (token->type == CPP_COMMA)
20405 /* Consume the `,'. */
20406 cp_lexer_consume_token (parser->lexer);
20407 /* Expect an ellipsis. */
20408 ellipsis_p
20409 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
20411 /* It might also be `...' if the optional trailing `,' was
20412 omitted. */
20413 else if (token->type == CPP_ELLIPSIS)
20415 /* Consume the `...' token. */
20416 cp_lexer_consume_token (parser->lexer);
20417 /* And remember that we saw it. */
20418 ellipsis_p = true;
20420 else
20421 ellipsis_p = false;
20423 /* Finish the parameter list. */
20424 if (!ellipsis_p)
20425 parameters = chainon (parameters, void_list_node);
20427 return parameters;
20430 /* Parse a parameter-declaration-list.
20432 parameter-declaration-list:
20433 parameter-declaration
20434 parameter-declaration-list , parameter-declaration
20436 Returns a representation of the parameter-declaration-list, as for
20437 cp_parser_parameter_declaration_clause. However, the
20438 `void_list_node' is never appended to the list. Upon return,
20439 *IS_ERROR will be true iff an error occurred. */
20441 static tree
20442 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
20444 tree parameters = NULL_TREE;
20445 tree *tail = &parameters;
20446 bool saved_in_unbraced_linkage_specification_p;
20447 int index = 0;
20449 /* Assume all will go well. */
20450 *is_error = false;
20451 /* The special considerations that apply to a function within an
20452 unbraced linkage specifications do not apply to the parameters
20453 to the function. */
20454 saved_in_unbraced_linkage_specification_p
20455 = parser->in_unbraced_linkage_specification_p;
20456 parser->in_unbraced_linkage_specification_p = false;
20458 /* Look for more parameters. */
20459 while (true)
20461 cp_parameter_declarator *parameter;
20462 tree decl = error_mark_node;
20463 bool parenthesized_p = false;
20464 int template_parm_idx = (function_being_declared_is_template_p (parser)?
20465 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
20466 (current_template_parms)) : 0);
20468 /* Parse the parameter. */
20469 parameter
20470 = cp_parser_parameter_declaration (parser,
20471 /*template_parm_p=*/false,
20472 &parenthesized_p);
20474 /* We don't know yet if the enclosing context is deprecated, so wait
20475 and warn in grokparms if appropriate. */
20476 deprecated_state = DEPRECATED_SUPPRESS;
20478 if (parameter)
20480 /* If a function parameter pack was specified and an implicit template
20481 parameter was introduced during cp_parser_parameter_declaration,
20482 change any implicit parameters introduced into packs. */
20483 if (parser->implicit_template_parms
20484 && parameter->declarator
20485 && parameter->declarator->parameter_pack_p)
20487 int latest_template_parm_idx = TREE_VEC_LENGTH
20488 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
20490 if (latest_template_parm_idx != template_parm_idx)
20491 parameter->decl_specifiers.type = convert_generic_types_to_packs
20492 (parameter->decl_specifiers.type,
20493 template_parm_idx, latest_template_parm_idx);
20496 decl = grokdeclarator (parameter->declarator,
20497 &parameter->decl_specifiers,
20498 PARM,
20499 parameter->default_argument != NULL_TREE,
20500 &parameter->decl_specifiers.attributes);
20503 deprecated_state = DEPRECATED_NORMAL;
20505 /* If a parse error occurred parsing the parameter declaration,
20506 then the entire parameter-declaration-list is erroneous. */
20507 if (decl == error_mark_node)
20509 *is_error = true;
20510 parameters = error_mark_node;
20511 break;
20514 if (parameter->decl_specifiers.attributes)
20515 cplus_decl_attributes (&decl,
20516 parameter->decl_specifiers.attributes,
20518 if (DECL_NAME (decl))
20519 decl = pushdecl (decl);
20521 if (decl != error_mark_node)
20523 retrofit_lang_decl (decl);
20524 DECL_PARM_INDEX (decl) = ++index;
20525 DECL_PARM_LEVEL (decl) = function_parm_depth ();
20528 /* Add the new parameter to the list. */
20529 *tail = build_tree_list (parameter->default_argument, decl);
20530 tail = &TREE_CHAIN (*tail);
20532 /* Peek at the next token. */
20533 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
20534 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
20535 /* These are for Objective-C++ */
20536 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20537 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20538 /* The parameter-declaration-list is complete. */
20539 break;
20540 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20542 cp_token *token;
20544 /* Peek at the next token. */
20545 token = cp_lexer_peek_nth_token (parser->lexer, 2);
20546 /* If it's an ellipsis, then the list is complete. */
20547 if (token->type == CPP_ELLIPSIS)
20548 break;
20549 /* Otherwise, there must be more parameters. Consume the
20550 `,'. */
20551 cp_lexer_consume_token (parser->lexer);
20552 /* When parsing something like:
20554 int i(float f, double d)
20556 we can tell after seeing the declaration for "f" that we
20557 are not looking at an initialization of a variable "i",
20558 but rather at the declaration of a function "i".
20560 Due to the fact that the parsing of template arguments
20561 (as specified to a template-id) requires backtracking we
20562 cannot use this technique when inside a template argument
20563 list. */
20564 if (!parser->in_template_argument_list_p
20565 && !parser->in_type_id_in_expr_p
20566 && cp_parser_uncommitted_to_tentative_parse_p (parser)
20567 /* However, a parameter-declaration of the form
20568 "float(f)" (which is a valid declaration of a
20569 parameter "f") can also be interpreted as an
20570 expression (the conversion of "f" to "float"). */
20571 && !parenthesized_p)
20572 cp_parser_commit_to_tentative_parse (parser);
20574 else
20576 cp_parser_error (parser, "expected %<,%> or %<...%>");
20577 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
20578 cp_parser_skip_to_closing_parenthesis (parser,
20579 /*recovering=*/true,
20580 /*or_comma=*/false,
20581 /*consume_paren=*/false);
20582 break;
20586 parser->in_unbraced_linkage_specification_p
20587 = saved_in_unbraced_linkage_specification_p;
20589 /* Reset implicit_template_scope if we are about to leave the function
20590 parameter list that introduced it. Note that for out-of-line member
20591 definitions, there will be one or more class scopes before we get to
20592 the template parameter scope. */
20594 if (cp_binding_level *its = parser->implicit_template_scope)
20595 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
20597 while (maybe_its->kind == sk_class)
20598 maybe_its = maybe_its->level_chain;
20599 if (maybe_its == its)
20601 parser->implicit_template_parms = 0;
20602 parser->implicit_template_scope = 0;
20606 return parameters;
20609 /* Parse a parameter declaration.
20611 parameter-declaration:
20612 decl-specifier-seq ... [opt] declarator
20613 decl-specifier-seq declarator = assignment-expression
20614 decl-specifier-seq ... [opt] abstract-declarator [opt]
20615 decl-specifier-seq abstract-declarator [opt] = assignment-expression
20617 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
20618 declares a template parameter. (In that case, a non-nested `>'
20619 token encountered during the parsing of the assignment-expression
20620 is not interpreted as a greater-than operator.)
20622 Returns a representation of the parameter, or NULL if an error
20623 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
20624 true iff the declarator is of the form "(p)". */
20626 static cp_parameter_declarator *
20627 cp_parser_parameter_declaration (cp_parser *parser,
20628 bool template_parm_p,
20629 bool *parenthesized_p)
20631 int declares_class_or_enum;
20632 cp_decl_specifier_seq decl_specifiers;
20633 cp_declarator *declarator;
20634 tree default_argument;
20635 cp_token *token = NULL, *declarator_token_start = NULL;
20636 const char *saved_message;
20637 bool template_parameter_pack_p = false;
20639 /* In a template parameter, `>' is not an operator.
20641 [temp.param]
20643 When parsing a default template-argument for a non-type
20644 template-parameter, the first non-nested `>' is taken as the end
20645 of the template parameter-list rather than a greater-than
20646 operator. */
20648 /* Type definitions may not appear in parameter types. */
20649 saved_message = parser->type_definition_forbidden_message;
20650 parser->type_definition_forbidden_message
20651 = G_("types may not be defined in parameter types");
20653 /* Parse the declaration-specifiers. */
20654 cp_parser_decl_specifier_seq (parser,
20655 CP_PARSER_FLAGS_NONE,
20656 &decl_specifiers,
20657 &declares_class_or_enum);
20659 /* Complain about missing 'typename' or other invalid type names. */
20660 if (!decl_specifiers.any_type_specifiers_p
20661 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20662 decl_specifiers.type = error_mark_node;
20664 /* If an error occurred, there's no reason to attempt to parse the
20665 rest of the declaration. */
20666 if (cp_parser_error_occurred (parser))
20668 parser->type_definition_forbidden_message = saved_message;
20669 return NULL;
20672 /* Peek at the next token. */
20673 token = cp_lexer_peek_token (parser->lexer);
20675 /* If the next token is a `)', `,', `=', `>', or `...', then there
20676 is no declarator. However, when variadic templates are enabled,
20677 there may be a declarator following `...'. */
20678 if (token->type == CPP_CLOSE_PAREN
20679 || token->type == CPP_COMMA
20680 || token->type == CPP_EQ
20681 || token->type == CPP_GREATER)
20683 declarator = NULL;
20684 if (parenthesized_p)
20685 *parenthesized_p = false;
20687 /* Otherwise, there should be a declarator. */
20688 else
20690 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20691 parser->default_arg_ok_p = false;
20693 /* After seeing a decl-specifier-seq, if the next token is not a
20694 "(", there is no possibility that the code is a valid
20695 expression. Therefore, if parsing tentatively, we commit at
20696 this point. */
20697 if (!parser->in_template_argument_list_p
20698 /* In an expression context, having seen:
20700 (int((char ...
20702 we cannot be sure whether we are looking at a
20703 function-type (taking a "char" as a parameter) or a cast
20704 of some object of type "char" to "int". */
20705 && !parser->in_type_id_in_expr_p
20706 && cp_parser_uncommitted_to_tentative_parse_p (parser)
20707 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20708 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
20709 cp_parser_commit_to_tentative_parse (parser);
20710 /* Parse the declarator. */
20711 declarator_token_start = token;
20712 declarator = cp_parser_declarator (parser,
20713 CP_PARSER_DECLARATOR_EITHER,
20714 /*ctor_dtor_or_conv_p=*/NULL,
20715 parenthesized_p,
20716 /*member_p=*/false,
20717 /*friend_p=*/false);
20718 parser->default_arg_ok_p = saved_default_arg_ok_p;
20719 /* After the declarator, allow more attributes. */
20720 decl_specifiers.attributes
20721 = chainon (decl_specifiers.attributes,
20722 cp_parser_attributes_opt (parser));
20724 /* If the declarator is a template parameter pack, remember that and
20725 clear the flag in the declarator itself so we don't get errors
20726 from grokdeclarator. */
20727 if (template_parm_p && declarator && declarator->parameter_pack_p)
20729 declarator->parameter_pack_p = false;
20730 template_parameter_pack_p = true;
20734 /* If the next token is an ellipsis, and we have not seen a declarator
20735 name, and if either the type of the declarator contains parameter
20736 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
20737 for, eg, abbreviated integral type names), then we actually have a
20738 parameter pack expansion expression. Otherwise, leave the ellipsis
20739 for a C-style variadic function. */
20740 token = cp_lexer_peek_token (parser->lexer);
20741 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20743 tree type = decl_specifiers.type;
20745 if (type && DECL_P (type))
20746 type = TREE_TYPE (type);
20748 if (((type
20749 && TREE_CODE (type) != TYPE_PACK_EXPANSION
20750 && (template_parm_p || uses_parameter_packs (type)))
20751 || (!type && template_parm_p))
20752 && declarator_can_be_parameter_pack (declarator))
20754 /* Consume the `...'. */
20755 cp_lexer_consume_token (parser->lexer);
20756 maybe_warn_variadic_templates ();
20758 /* Build a pack expansion type */
20759 if (template_parm_p)
20760 template_parameter_pack_p = true;
20761 else if (declarator)
20762 declarator->parameter_pack_p = true;
20763 else
20764 decl_specifiers.type = make_pack_expansion (type);
20768 /* The restriction on defining new types applies only to the type
20769 of the parameter, not to the default argument. */
20770 parser->type_definition_forbidden_message = saved_message;
20772 /* If the next token is `=', then process a default argument. */
20773 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20775 tree type = decl_specifiers.type;
20776 token = cp_lexer_peek_token (parser->lexer);
20777 /* If we are defining a class, then the tokens that make up the
20778 default argument must be saved and processed later. */
20779 if (!template_parm_p && at_class_scope_p ()
20780 && TYPE_BEING_DEFINED (current_class_type)
20781 && !LAMBDA_TYPE_P (current_class_type))
20782 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
20784 // A constrained-type-specifier may declare a type template-parameter.
20785 else if (declares_constrained_type_template_parameter (type))
20786 default_argument
20787 = cp_parser_default_type_template_argument (parser);
20789 // A constrained-type-specifier may declare a template-template-parameter.
20790 else if (declares_constrained_template_template_parameter (type))
20791 default_argument
20792 = cp_parser_default_template_template_argument (parser);
20794 /* Outside of a class definition, we can just parse the
20795 assignment-expression. */
20796 else
20797 default_argument
20798 = cp_parser_default_argument (parser, template_parm_p);
20800 if (!parser->default_arg_ok_p)
20802 permerror (token->location,
20803 "default arguments are only "
20804 "permitted for function parameters");
20806 else if ((declarator && declarator->parameter_pack_p)
20807 || template_parameter_pack_p
20808 || (decl_specifiers.type
20809 && PACK_EXPANSION_P (decl_specifiers.type)))
20811 /* Find the name of the parameter pack. */
20812 cp_declarator *id_declarator = declarator;
20813 while (id_declarator && id_declarator->kind != cdk_id)
20814 id_declarator = id_declarator->declarator;
20816 if (id_declarator && id_declarator->kind == cdk_id)
20817 error_at (declarator_token_start->location,
20818 template_parm_p
20819 ? G_("template parameter pack %qD "
20820 "cannot have a default argument")
20821 : G_("parameter pack %qD cannot have "
20822 "a default argument"),
20823 id_declarator->u.id.unqualified_name);
20824 else
20825 error_at (declarator_token_start->location,
20826 template_parm_p
20827 ? G_("template parameter pack cannot have "
20828 "a default argument")
20829 : G_("parameter pack cannot have a "
20830 "default argument"));
20832 default_argument = NULL_TREE;
20835 else
20836 default_argument = NULL_TREE;
20838 return make_parameter_declarator (&decl_specifiers,
20839 declarator,
20840 default_argument,
20841 template_parameter_pack_p);
20844 /* Parse a default argument and return it.
20846 TEMPLATE_PARM_P is true if this is a default argument for a
20847 non-type template parameter. */
20848 static tree
20849 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
20851 tree default_argument = NULL_TREE;
20852 bool saved_greater_than_is_operator_p;
20853 bool saved_local_variables_forbidden_p;
20854 bool non_constant_p, is_direct_init;
20856 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
20857 set correctly. */
20858 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
20859 parser->greater_than_is_operator_p = !template_parm_p;
20860 /* Local variable names (and the `this' keyword) may not
20861 appear in a default argument. */
20862 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20863 parser->local_variables_forbidden_p = true;
20864 /* Parse the assignment-expression. */
20865 if (template_parm_p)
20866 push_deferring_access_checks (dk_no_deferred);
20867 tree saved_class_ptr = NULL_TREE;
20868 tree saved_class_ref = NULL_TREE;
20869 /* The "this" pointer is not valid in a default argument. */
20870 if (cfun)
20872 saved_class_ptr = current_class_ptr;
20873 cp_function_chain->x_current_class_ptr = NULL_TREE;
20874 saved_class_ref = current_class_ref;
20875 cp_function_chain->x_current_class_ref = NULL_TREE;
20877 default_argument
20878 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
20879 /* Restore the "this" pointer. */
20880 if (cfun)
20882 cp_function_chain->x_current_class_ptr = saved_class_ptr;
20883 cp_function_chain->x_current_class_ref = saved_class_ref;
20885 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
20886 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20887 if (template_parm_p)
20888 pop_deferring_access_checks ();
20889 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
20890 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20892 return default_argument;
20895 /* Parse a function-body.
20897 function-body:
20898 compound_statement */
20900 static void
20901 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
20903 cp_parser_compound_statement (parser, NULL, (in_function_try_block
20904 ? BCS_TRY_BLOCK : BCS_NORMAL),
20905 true);
20908 /* Parse a ctor-initializer-opt followed by a function-body. Return
20909 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
20910 is true we are parsing a function-try-block. */
20912 static bool
20913 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
20914 bool in_function_try_block)
20916 tree body, list;
20917 bool ctor_initializer_p;
20918 const bool check_body_p =
20919 DECL_CONSTRUCTOR_P (current_function_decl)
20920 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
20921 tree last = NULL;
20923 /* Begin the function body. */
20924 body = begin_function_body ();
20925 /* Parse the optional ctor-initializer. */
20926 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
20928 /* If we're parsing a constexpr constructor definition, we need
20929 to check that the constructor body is indeed empty. However,
20930 before we get to cp_parser_function_body lot of junk has been
20931 generated, so we can't just check that we have an empty block.
20932 Rather we take a snapshot of the outermost block, and check whether
20933 cp_parser_function_body changed its state. */
20934 if (check_body_p)
20936 list = cur_stmt_list;
20937 if (STATEMENT_LIST_TAIL (list))
20938 last = STATEMENT_LIST_TAIL (list)->stmt;
20940 /* Parse the function-body. */
20941 cp_parser_function_body (parser, in_function_try_block);
20942 if (check_body_p)
20943 check_constexpr_ctor_body (last, list, /*complain=*/true);
20944 /* Finish the function body. */
20945 finish_function_body (body);
20947 return ctor_initializer_p;
20950 /* Parse an initializer.
20952 initializer:
20953 = initializer-clause
20954 ( expression-list )
20956 Returns an expression representing the initializer. If no
20957 initializer is present, NULL_TREE is returned.
20959 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
20960 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
20961 set to TRUE if there is no initializer present. If there is an
20962 initializer, and it is not a constant-expression, *NON_CONSTANT_P
20963 is set to true; otherwise it is set to false. */
20965 static tree
20966 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
20967 bool* non_constant_p)
20969 cp_token *token;
20970 tree init;
20972 /* Peek at the next token. */
20973 token = cp_lexer_peek_token (parser->lexer);
20975 /* Let our caller know whether or not this initializer was
20976 parenthesized. */
20977 *is_direct_init = (token->type != CPP_EQ);
20978 /* Assume that the initializer is constant. */
20979 *non_constant_p = false;
20981 if (token->type == CPP_EQ)
20983 /* Consume the `='. */
20984 cp_lexer_consume_token (parser->lexer);
20985 /* Parse the initializer-clause. */
20986 init = cp_parser_initializer_clause (parser, non_constant_p);
20988 else if (token->type == CPP_OPEN_PAREN)
20990 vec<tree, va_gc> *vec;
20991 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20992 /*cast_p=*/false,
20993 /*allow_expansion_p=*/true,
20994 non_constant_p);
20995 if (vec == NULL)
20996 return error_mark_node;
20997 init = build_tree_list_vec (vec);
20998 release_tree_vector (vec);
21000 else if (token->type == CPP_OPEN_BRACE)
21002 cp_lexer_set_source_position (parser->lexer);
21003 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21004 init = cp_parser_braced_list (parser, non_constant_p);
21005 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21007 else
21009 /* Anything else is an error. */
21010 cp_parser_error (parser, "expected initializer");
21011 init = error_mark_node;
21014 if (check_for_bare_parameter_packs (init))
21015 init = error_mark_node;
21017 return init;
21020 /* Parse an initializer-clause.
21022 initializer-clause:
21023 assignment-expression
21024 braced-init-list
21026 Returns an expression representing the initializer.
21028 If the `assignment-expression' production is used the value
21029 returned is simply a representation for the expression.
21031 Otherwise, calls cp_parser_braced_list. */
21033 static cp_expr
21034 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21036 cp_expr initializer;
21038 /* Assume the expression is constant. */
21039 *non_constant_p = false;
21041 /* If it is not a `{', then we are looking at an
21042 assignment-expression. */
21043 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21045 initializer
21046 = cp_parser_constant_expression (parser,
21047 /*allow_non_constant_p=*/true,
21048 non_constant_p);
21050 else
21051 initializer = cp_parser_braced_list (parser, non_constant_p);
21053 return initializer;
21056 /* Parse a brace-enclosed initializer list.
21058 braced-init-list:
21059 { initializer-list , [opt] }
21062 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21063 the elements of the initializer-list (or NULL, if the last
21064 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21065 NULL_TREE. There is no way to detect whether or not the optional
21066 trailing `,' was provided. NON_CONSTANT_P is as for
21067 cp_parser_initializer. */
21069 static cp_expr
21070 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
21072 tree initializer;
21073 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
21075 /* Consume the `{' token. */
21076 cp_lexer_consume_token (parser->lexer);
21077 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21078 initializer = make_node (CONSTRUCTOR);
21079 /* If it's not a `}', then there is a non-trivial initializer. */
21080 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
21082 /* Parse the initializer list. */
21083 CONSTRUCTOR_ELTS (initializer)
21084 = cp_parser_initializer_list (parser, non_constant_p);
21085 /* A trailing `,' token is allowed. */
21086 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21087 cp_lexer_consume_token (parser->lexer);
21089 else
21090 *non_constant_p = false;
21091 /* Now, there should be a trailing `}'. */
21092 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
21093 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21094 TREE_TYPE (initializer) = init_list_type_node;
21096 cp_expr result (initializer);
21097 /* Build a location of the form:
21098 { ... }
21099 ^~~~~~~
21100 with caret==start at the open brace, finish at the close brace. */
21101 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
21102 result.set_location (combined_loc);
21103 return result;
21106 /* Consume tokens up to, and including, the next non-nested closing `]'.
21107 Returns true iff we found a closing `]'. */
21109 static bool
21110 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
21112 unsigned square_depth = 0;
21114 while (true)
21116 cp_token * token = cp_lexer_peek_token (parser->lexer);
21118 switch (token->type)
21120 case CPP_EOF:
21121 case CPP_PRAGMA_EOL:
21122 /* If we've run out of tokens, then there is no closing `]'. */
21123 return false;
21125 case CPP_OPEN_SQUARE:
21126 ++square_depth;
21127 break;
21129 case CPP_CLOSE_SQUARE:
21130 if (!square_depth--)
21132 cp_lexer_consume_token (parser->lexer);
21133 return true;
21135 break;
21137 default:
21138 break;
21141 /* Consume the token. */
21142 cp_lexer_consume_token (parser->lexer);
21146 /* Return true if we are looking at an array-designator, false otherwise. */
21148 static bool
21149 cp_parser_array_designator_p (cp_parser *parser)
21151 /* Consume the `['. */
21152 cp_lexer_consume_token (parser->lexer);
21154 cp_lexer_save_tokens (parser->lexer);
21156 /* Skip tokens until the next token is a closing square bracket.
21157 If we find the closing `]', and the next token is a `=', then
21158 we are looking at an array designator. */
21159 bool array_designator_p
21160 = (cp_parser_skip_to_closing_square_bracket (parser)
21161 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
21163 /* Roll back the tokens we skipped. */
21164 cp_lexer_rollback_tokens (parser->lexer);
21166 return array_designator_p;
21169 /* Parse an initializer-list.
21171 initializer-list:
21172 initializer-clause ... [opt]
21173 initializer-list , initializer-clause ... [opt]
21175 GNU Extension:
21177 initializer-list:
21178 designation initializer-clause ...[opt]
21179 initializer-list , designation initializer-clause ...[opt]
21181 designation:
21182 . identifier =
21183 identifier :
21184 [ constant-expression ] =
21186 Returns a vec of constructor_elt. The VALUE of each elt is an expression
21187 for the initializer. If the INDEX of the elt is non-NULL, it is the
21188 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
21189 as for cp_parser_initializer. */
21191 static vec<constructor_elt, va_gc> *
21192 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
21194 vec<constructor_elt, va_gc> *v = NULL;
21196 /* Assume all of the expressions are constant. */
21197 *non_constant_p = false;
21199 /* Parse the rest of the list. */
21200 while (true)
21202 cp_token *token;
21203 tree designator;
21204 tree initializer;
21205 bool clause_non_constant_p;
21207 /* If the next token is an identifier and the following one is a
21208 colon, we are looking at the GNU designated-initializer
21209 syntax. */
21210 if (cp_parser_allow_gnu_extensions_p (parser)
21211 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21212 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
21214 /* Warn the user that they are using an extension. */
21215 pedwarn (input_location, OPT_Wpedantic,
21216 "ISO C++ does not allow designated initializers");
21217 /* Consume the identifier. */
21218 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21219 /* Consume the `:'. */
21220 cp_lexer_consume_token (parser->lexer);
21222 /* Also handle the C99 syntax, '. id ='. */
21223 else if (cp_parser_allow_gnu_extensions_p (parser)
21224 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
21225 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
21226 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
21228 /* Warn the user that they are using an extension. */
21229 pedwarn (input_location, OPT_Wpedantic,
21230 "ISO C++ does not allow C99 designated initializers");
21231 /* Consume the `.'. */
21232 cp_lexer_consume_token (parser->lexer);
21233 /* Consume the identifier. */
21234 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21235 /* Consume the `='. */
21236 cp_lexer_consume_token (parser->lexer);
21238 /* Also handle C99 array designators, '[ const ] ='. */
21239 else if (cp_parser_allow_gnu_extensions_p (parser)
21240 && !c_dialect_objc ()
21241 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21243 /* In C++11, [ could start a lambda-introducer. */
21244 bool non_const = false;
21246 cp_parser_parse_tentatively (parser);
21248 if (!cp_parser_array_designator_p (parser))
21250 cp_parser_simulate_error (parser);
21251 designator = NULL_TREE;
21253 else
21255 designator = cp_parser_constant_expression (parser, true,
21256 &non_const);
21257 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21258 cp_parser_require (parser, CPP_EQ, RT_EQ);
21261 if (!cp_parser_parse_definitely (parser))
21262 designator = NULL_TREE;
21263 else if (non_const)
21264 require_potential_rvalue_constant_expression (designator);
21266 else
21267 designator = NULL_TREE;
21269 /* Parse the initializer. */
21270 initializer = cp_parser_initializer_clause (parser,
21271 &clause_non_constant_p);
21272 /* If any clause is non-constant, so is the entire initializer. */
21273 if (clause_non_constant_p)
21274 *non_constant_p = true;
21276 /* If we have an ellipsis, this is an initializer pack
21277 expansion. */
21278 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21280 /* Consume the `...'. */
21281 cp_lexer_consume_token (parser->lexer);
21283 /* Turn the initializer into an initializer expansion. */
21284 initializer = make_pack_expansion (initializer);
21287 /* Add it to the vector. */
21288 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
21290 /* If the next token is not a comma, we have reached the end of
21291 the list. */
21292 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21293 break;
21295 /* Peek at the next token. */
21296 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21297 /* If the next token is a `}', then we're still done. An
21298 initializer-clause can have a trailing `,' after the
21299 initializer-list and before the closing `}'. */
21300 if (token->type == CPP_CLOSE_BRACE)
21301 break;
21303 /* Consume the `,' token. */
21304 cp_lexer_consume_token (parser->lexer);
21307 return v;
21310 /* Classes [gram.class] */
21312 /* Parse a class-name.
21314 class-name:
21315 identifier
21316 template-id
21318 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
21319 to indicate that names looked up in dependent types should be
21320 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
21321 keyword has been used to indicate that the name that appears next
21322 is a template. TAG_TYPE indicates the explicit tag given before
21323 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
21324 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
21325 is the class being defined in a class-head. If ENUM_OK is TRUE,
21326 enum-names are also accepted.
21328 Returns the TYPE_DECL representing the class. */
21330 static tree
21331 cp_parser_class_name (cp_parser *parser,
21332 bool typename_keyword_p,
21333 bool template_keyword_p,
21334 enum tag_types tag_type,
21335 bool check_dependency_p,
21336 bool class_head_p,
21337 bool is_declaration,
21338 bool enum_ok)
21340 tree decl;
21341 tree scope;
21342 bool typename_p;
21343 cp_token *token;
21344 tree identifier = NULL_TREE;
21346 /* All class-names start with an identifier. */
21347 token = cp_lexer_peek_token (parser->lexer);
21348 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
21350 cp_parser_error (parser, "expected class-name");
21351 return error_mark_node;
21354 /* PARSER->SCOPE can be cleared when parsing the template-arguments
21355 to a template-id, so we save it here. */
21356 scope = parser->scope;
21357 if (scope == error_mark_node)
21358 return error_mark_node;
21360 /* Any name names a type if we're following the `typename' keyword
21361 in a qualified name where the enclosing scope is type-dependent. */
21362 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
21363 && dependent_type_p (scope));
21364 /* Handle the common case (an identifier, but not a template-id)
21365 efficiently. */
21366 if (token->type == CPP_NAME
21367 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
21369 cp_token *identifier_token;
21370 bool ambiguous_p;
21372 /* Look for the identifier. */
21373 identifier_token = cp_lexer_peek_token (parser->lexer);
21374 ambiguous_p = identifier_token->error_reported;
21375 identifier = cp_parser_identifier (parser);
21376 /* If the next token isn't an identifier, we are certainly not
21377 looking at a class-name. */
21378 if (identifier == error_mark_node)
21379 decl = error_mark_node;
21380 /* If we know this is a type-name, there's no need to look it
21381 up. */
21382 else if (typename_p)
21383 decl = identifier;
21384 else
21386 tree ambiguous_decls;
21387 /* If we already know that this lookup is ambiguous, then
21388 we've already issued an error message; there's no reason
21389 to check again. */
21390 if (ambiguous_p)
21392 cp_parser_simulate_error (parser);
21393 return error_mark_node;
21395 /* If the next token is a `::', then the name must be a type
21396 name.
21398 [basic.lookup.qual]
21400 During the lookup for a name preceding the :: scope
21401 resolution operator, object, function, and enumerator
21402 names are ignored. */
21403 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21404 tag_type = scope_type;
21405 /* Look up the name. */
21406 decl = cp_parser_lookup_name (parser, identifier,
21407 tag_type,
21408 /*is_template=*/false,
21409 /*is_namespace=*/false,
21410 check_dependency_p,
21411 &ambiguous_decls,
21412 identifier_token->location);
21413 if (ambiguous_decls)
21415 if (cp_parser_parsing_tentatively (parser))
21416 cp_parser_simulate_error (parser);
21417 return error_mark_node;
21421 else
21423 /* Try a template-id. */
21424 decl = cp_parser_template_id (parser, template_keyword_p,
21425 check_dependency_p,
21426 tag_type,
21427 is_declaration);
21428 if (decl == error_mark_node)
21429 return error_mark_node;
21432 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
21434 /* If this is a typename, create a TYPENAME_TYPE. */
21435 if (typename_p && decl != error_mark_node)
21437 decl = make_typename_type (scope, decl, typename_type,
21438 /*complain=*/tf_error);
21439 if (decl != error_mark_node)
21440 decl = TYPE_NAME (decl);
21443 decl = strip_using_decl (decl);
21445 /* Check to see that it is really the name of a class. */
21446 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
21447 && identifier_p (TREE_OPERAND (decl, 0))
21448 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21449 /* Situations like this:
21451 template <typename T> struct A {
21452 typename T::template X<int>::I i;
21455 are problematic. Is `T::template X<int>' a class-name? The
21456 standard does not seem to be definitive, but there is no other
21457 valid interpretation of the following `::'. Therefore, those
21458 names are considered class-names. */
21460 decl = make_typename_type (scope, decl, tag_type, tf_error);
21461 if (decl != error_mark_node)
21462 decl = TYPE_NAME (decl);
21464 else if (TREE_CODE (decl) != TYPE_DECL
21465 || TREE_TYPE (decl) == error_mark_node
21466 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
21467 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
21468 /* In Objective-C 2.0, a classname followed by '.' starts a
21469 dot-syntax expression, and it's not a type-name. */
21470 || (c_dialect_objc ()
21471 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
21472 && objc_is_class_name (decl)))
21473 decl = error_mark_node;
21475 if (decl == error_mark_node)
21476 cp_parser_error (parser, "expected class-name");
21477 else if (identifier && !parser->scope)
21478 maybe_note_name_used_in_class (identifier, decl);
21480 return decl;
21483 /* Parse a class-specifier.
21485 class-specifier:
21486 class-head { member-specification [opt] }
21488 Returns the TREE_TYPE representing the class. */
21490 static tree
21491 cp_parser_class_specifier_1 (cp_parser* parser)
21493 tree type;
21494 tree attributes = NULL_TREE;
21495 bool nested_name_specifier_p;
21496 unsigned saved_num_template_parameter_lists;
21497 bool saved_in_function_body;
21498 unsigned char in_statement;
21499 bool in_switch_statement_p;
21500 bool saved_in_unbraced_linkage_specification_p;
21501 tree old_scope = NULL_TREE;
21502 tree scope = NULL_TREE;
21503 cp_token *closing_brace;
21505 push_deferring_access_checks (dk_no_deferred);
21507 /* Parse the class-head. */
21508 type = cp_parser_class_head (parser,
21509 &nested_name_specifier_p);
21510 /* If the class-head was a semantic disaster, skip the entire body
21511 of the class. */
21512 if (!type)
21514 cp_parser_skip_to_end_of_block_or_statement (parser);
21515 pop_deferring_access_checks ();
21516 return error_mark_node;
21519 /* Look for the `{'. */
21520 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
21522 pop_deferring_access_checks ();
21523 return error_mark_node;
21526 cp_ensure_no_omp_declare_simd (parser);
21527 cp_ensure_no_oacc_routine (parser);
21529 /* Issue an error message if type-definitions are forbidden here. */
21530 cp_parser_check_type_definition (parser);
21531 /* Remember that we are defining one more class. */
21532 ++parser->num_classes_being_defined;
21533 /* Inside the class, surrounding template-parameter-lists do not
21534 apply. */
21535 saved_num_template_parameter_lists
21536 = parser->num_template_parameter_lists;
21537 parser->num_template_parameter_lists = 0;
21538 /* We are not in a function body. */
21539 saved_in_function_body = parser->in_function_body;
21540 parser->in_function_body = false;
21541 /* Or in a loop. */
21542 in_statement = parser->in_statement;
21543 parser->in_statement = 0;
21544 /* Or in a switch. */
21545 in_switch_statement_p = parser->in_switch_statement_p;
21546 parser->in_switch_statement_p = false;
21547 /* We are not immediately inside an extern "lang" block. */
21548 saved_in_unbraced_linkage_specification_p
21549 = parser->in_unbraced_linkage_specification_p;
21550 parser->in_unbraced_linkage_specification_p = false;
21552 // Associate constraints with the type.
21553 if (flag_concepts)
21554 type = associate_classtype_constraints (type);
21556 /* Start the class. */
21557 if (nested_name_specifier_p)
21559 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
21560 old_scope = push_inner_scope (scope);
21562 type = begin_class_definition (type);
21564 if (type == error_mark_node)
21565 /* If the type is erroneous, skip the entire body of the class. */
21566 cp_parser_skip_to_closing_brace (parser);
21567 else
21568 /* Parse the member-specification. */
21569 cp_parser_member_specification_opt (parser);
21571 /* Look for the trailing `}'. */
21572 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21573 /* Look for trailing attributes to apply to this class. */
21574 if (cp_parser_allow_gnu_extensions_p (parser))
21575 attributes = cp_parser_gnu_attributes_opt (parser);
21576 if (type != error_mark_node)
21577 type = finish_struct (type, attributes);
21578 if (nested_name_specifier_p)
21579 pop_inner_scope (old_scope, scope);
21581 /* We've finished a type definition. Check for the common syntax
21582 error of forgetting a semicolon after the definition. We need to
21583 be careful, as we can't just check for not-a-semicolon and be done
21584 with it; the user might have typed:
21586 class X { } c = ...;
21587 class X { } *p = ...;
21589 and so forth. Instead, enumerate all the possible tokens that
21590 might follow this production; if we don't see one of them, then
21591 complain and silently insert the semicolon. */
21593 cp_token *token = cp_lexer_peek_token (parser->lexer);
21594 bool want_semicolon = true;
21596 if (cp_next_tokens_can_be_std_attribute_p (parser))
21597 /* Don't try to parse c++11 attributes here. As per the
21598 grammar, that should be a task for
21599 cp_parser_decl_specifier_seq. */
21600 want_semicolon = false;
21602 switch (token->type)
21604 case CPP_NAME:
21605 case CPP_SEMICOLON:
21606 case CPP_MULT:
21607 case CPP_AND:
21608 case CPP_OPEN_PAREN:
21609 case CPP_CLOSE_PAREN:
21610 case CPP_COMMA:
21611 want_semicolon = false;
21612 break;
21614 /* While it's legal for type qualifiers and storage class
21615 specifiers to follow type definitions in the grammar, only
21616 compiler testsuites contain code like that. Assume that if
21617 we see such code, then what we're really seeing is a case
21618 like:
21620 class X { }
21621 const <type> var = ...;
21625 class Y { }
21626 static <type> func (...) ...
21628 i.e. the qualifier or specifier applies to the next
21629 declaration. To do so, however, we need to look ahead one
21630 more token to see if *that* token is a type specifier.
21632 This code could be improved to handle:
21634 class Z { }
21635 static const <type> var = ...; */
21636 case CPP_KEYWORD:
21637 if (keyword_is_decl_specifier (token->keyword))
21639 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
21641 /* Handling user-defined types here would be nice, but very
21642 tricky. */
21643 want_semicolon
21644 = (lookahead->type == CPP_KEYWORD
21645 && keyword_begins_type_specifier (lookahead->keyword));
21647 break;
21648 default:
21649 break;
21652 /* If we don't have a type, then something is very wrong and we
21653 shouldn't try to do anything clever. Likewise for not seeing the
21654 closing brace. */
21655 if (closing_brace && TYPE_P (type) && want_semicolon)
21657 /* Locate the closing brace. */
21658 cp_token_position prev
21659 = cp_lexer_previous_token_position (parser->lexer);
21660 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
21661 location_t loc = prev_token->location;
21663 /* We want to suggest insertion of a ';' immediately *after* the
21664 closing brace, so, if we can, offset the location by 1 column. */
21665 location_t next_loc = loc;
21666 if (!linemap_location_from_macro_expansion_p (line_table, loc))
21667 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
21669 rich_location richloc (line_table, next_loc);
21670 richloc.add_fixit_insert_before (next_loc, ";");
21672 if (CLASSTYPE_DECLARED_CLASS (type))
21673 error_at_rich_loc (&richloc,
21674 "expected %<;%> after class definition");
21675 else if (TREE_CODE (type) == RECORD_TYPE)
21676 error_at_rich_loc (&richloc,
21677 "expected %<;%> after struct definition");
21678 else if (TREE_CODE (type) == UNION_TYPE)
21679 error_at_rich_loc (&richloc,
21680 "expected %<;%> after union definition");
21681 else
21682 gcc_unreachable ();
21684 /* Unget one token and smash it to look as though we encountered
21685 a semicolon in the input stream. */
21686 cp_lexer_set_token_position (parser->lexer, prev);
21687 token = cp_lexer_peek_token (parser->lexer);
21688 token->type = CPP_SEMICOLON;
21689 token->keyword = RID_MAX;
21693 /* If this class is not itself within the scope of another class,
21694 then we need to parse the bodies of all of the queued function
21695 definitions. Note that the queued functions defined in a class
21696 are not always processed immediately following the
21697 class-specifier for that class. Consider:
21699 struct A {
21700 struct B { void f() { sizeof (A); } };
21703 If `f' were processed before the processing of `A' were
21704 completed, there would be no way to compute the size of `A'.
21705 Note that the nesting we are interested in here is lexical --
21706 not the semantic nesting given by TYPE_CONTEXT. In particular,
21707 for:
21709 struct A { struct B; };
21710 struct A::B { void f() { } };
21712 there is no need to delay the parsing of `A::B::f'. */
21713 if (--parser->num_classes_being_defined == 0)
21715 tree decl;
21716 tree class_type = NULL_TREE;
21717 tree pushed_scope = NULL_TREE;
21718 unsigned ix;
21719 cp_default_arg_entry *e;
21720 tree save_ccp, save_ccr;
21722 /* In a first pass, parse default arguments to the functions.
21723 Then, in a second pass, parse the bodies of the functions.
21724 This two-phased approach handles cases like:
21726 struct S {
21727 void f() { g(); }
21728 void g(int i = 3);
21732 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
21734 decl = e->decl;
21735 /* If there are default arguments that have not yet been processed,
21736 take care of them now. */
21737 if (class_type != e->class_type)
21739 if (pushed_scope)
21740 pop_scope (pushed_scope);
21741 class_type = e->class_type;
21742 pushed_scope = push_scope (class_type);
21744 /* Make sure that any template parameters are in scope. */
21745 maybe_begin_member_template_processing (decl);
21746 /* Parse the default argument expressions. */
21747 cp_parser_late_parsing_default_args (parser, decl);
21748 /* Remove any template parameters from the symbol table. */
21749 maybe_end_member_template_processing ();
21751 vec_safe_truncate (unparsed_funs_with_default_args, 0);
21752 /* Now parse any NSDMIs. */
21753 save_ccp = current_class_ptr;
21754 save_ccr = current_class_ref;
21755 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
21757 if (class_type != DECL_CONTEXT (decl))
21759 if (pushed_scope)
21760 pop_scope (pushed_scope);
21761 class_type = DECL_CONTEXT (decl);
21762 pushed_scope = push_scope (class_type);
21764 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
21765 cp_parser_late_parsing_nsdmi (parser, decl);
21767 vec_safe_truncate (unparsed_nsdmis, 0);
21768 current_class_ptr = save_ccp;
21769 current_class_ref = save_ccr;
21770 if (pushed_scope)
21771 pop_scope (pushed_scope);
21773 /* Now do some post-NSDMI bookkeeping. */
21774 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
21775 after_nsdmi_defaulted_late_checks (class_type);
21776 vec_safe_truncate (unparsed_classes, 0);
21777 after_nsdmi_defaulted_late_checks (type);
21779 /* Now parse the body of the functions. */
21780 if (flag_openmp)
21782 /* OpenMP UDRs need to be parsed before all other functions. */
21783 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21784 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
21785 cp_parser_late_parsing_for_member (parser, decl);
21786 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21787 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
21788 cp_parser_late_parsing_for_member (parser, decl);
21790 else
21791 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21792 cp_parser_late_parsing_for_member (parser, decl);
21793 vec_safe_truncate (unparsed_funs_with_definitions, 0);
21795 else
21796 vec_safe_push (unparsed_classes, type);
21798 /* Put back any saved access checks. */
21799 pop_deferring_access_checks ();
21801 /* Restore saved state. */
21802 parser->in_switch_statement_p = in_switch_statement_p;
21803 parser->in_statement = in_statement;
21804 parser->in_function_body = saved_in_function_body;
21805 parser->num_template_parameter_lists
21806 = saved_num_template_parameter_lists;
21807 parser->in_unbraced_linkage_specification_p
21808 = saved_in_unbraced_linkage_specification_p;
21810 return type;
21813 static tree
21814 cp_parser_class_specifier (cp_parser* parser)
21816 tree ret;
21817 timevar_push (TV_PARSE_STRUCT);
21818 ret = cp_parser_class_specifier_1 (parser);
21819 timevar_pop (TV_PARSE_STRUCT);
21820 return ret;
21823 /* Parse a class-head.
21825 class-head:
21826 class-key identifier [opt] base-clause [opt]
21827 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
21828 class-key nested-name-specifier [opt] template-id
21829 base-clause [opt]
21831 class-virt-specifier:
21832 final
21834 GNU Extensions:
21835 class-key attributes identifier [opt] base-clause [opt]
21836 class-key attributes nested-name-specifier identifier base-clause [opt]
21837 class-key attributes nested-name-specifier [opt] template-id
21838 base-clause [opt]
21840 Upon return BASES is initialized to the list of base classes (or
21841 NULL, if there are none) in the same form returned by
21842 cp_parser_base_clause.
21844 Returns the TYPE of the indicated class. Sets
21845 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
21846 involving a nested-name-specifier was used, and FALSE otherwise.
21848 Returns error_mark_node if this is not a class-head.
21850 Returns NULL_TREE if the class-head is syntactically valid, but
21851 semantically invalid in a way that means we should skip the entire
21852 body of the class. */
21854 static tree
21855 cp_parser_class_head (cp_parser* parser,
21856 bool* nested_name_specifier_p)
21858 tree nested_name_specifier;
21859 enum tag_types class_key;
21860 tree id = NULL_TREE;
21861 tree type = NULL_TREE;
21862 tree attributes;
21863 tree bases;
21864 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21865 bool template_id_p = false;
21866 bool qualified_p = false;
21867 bool invalid_nested_name_p = false;
21868 bool invalid_explicit_specialization_p = false;
21869 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
21870 tree pushed_scope = NULL_TREE;
21871 unsigned num_templates;
21872 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
21873 /* Assume no nested-name-specifier will be present. */
21874 *nested_name_specifier_p = false;
21875 /* Assume no template parameter lists will be used in defining the
21876 type. */
21877 num_templates = 0;
21878 parser->colon_corrects_to_scope_p = false;
21880 /* Look for the class-key. */
21881 class_key = cp_parser_class_key (parser);
21882 if (class_key == none_type)
21883 return error_mark_node;
21885 location_t class_head_start_location = input_location;
21887 /* Parse the attributes. */
21888 attributes = cp_parser_attributes_opt (parser);
21890 /* If the next token is `::', that is invalid -- but sometimes
21891 people do try to write:
21893 struct ::S {};
21895 Handle this gracefully by accepting the extra qualifier, and then
21896 issuing an error about it later if this really is a
21897 class-head. If it turns out just to be an elaborated type
21898 specifier, remain silent. */
21899 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
21900 qualified_p = true;
21902 push_deferring_access_checks (dk_no_check);
21904 /* Determine the name of the class. Begin by looking for an
21905 optional nested-name-specifier. */
21906 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
21907 nested_name_specifier
21908 = cp_parser_nested_name_specifier_opt (parser,
21909 /*typename_keyword_p=*/false,
21910 /*check_dependency_p=*/false,
21911 /*type_p=*/true,
21912 /*is_declaration=*/false);
21913 /* If there was a nested-name-specifier, then there *must* be an
21914 identifier. */
21915 if (nested_name_specifier)
21917 type_start_token = cp_lexer_peek_token (parser->lexer);
21918 /* Although the grammar says `identifier', it really means
21919 `class-name' or `template-name'. You are only allowed to
21920 define a class that has already been declared with this
21921 syntax.
21923 The proposed resolution for Core Issue 180 says that wherever
21924 you see `class T::X' you should treat `X' as a type-name.
21926 It is OK to define an inaccessible class; for example:
21928 class A { class B; };
21929 class A::B {};
21931 We do not know if we will see a class-name, or a
21932 template-name. We look for a class-name first, in case the
21933 class-name is a template-id; if we looked for the
21934 template-name first we would stop after the template-name. */
21935 cp_parser_parse_tentatively (parser);
21936 type = cp_parser_class_name (parser,
21937 /*typename_keyword_p=*/false,
21938 /*template_keyword_p=*/false,
21939 class_type,
21940 /*check_dependency_p=*/false,
21941 /*class_head_p=*/true,
21942 /*is_declaration=*/false);
21943 /* If that didn't work, ignore the nested-name-specifier. */
21944 if (!cp_parser_parse_definitely (parser))
21946 invalid_nested_name_p = true;
21947 type_start_token = cp_lexer_peek_token (parser->lexer);
21948 id = cp_parser_identifier (parser);
21949 if (id == error_mark_node)
21950 id = NULL_TREE;
21952 /* If we could not find a corresponding TYPE, treat this
21953 declaration like an unqualified declaration. */
21954 if (type == error_mark_node)
21955 nested_name_specifier = NULL_TREE;
21956 /* Otherwise, count the number of templates used in TYPE and its
21957 containing scopes. */
21958 else
21960 tree scope;
21962 for (scope = TREE_TYPE (type);
21963 scope && TREE_CODE (scope) != NAMESPACE_DECL;
21964 scope = get_containing_scope (scope))
21965 if (TYPE_P (scope)
21966 && CLASS_TYPE_P (scope)
21967 && CLASSTYPE_TEMPLATE_INFO (scope)
21968 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
21969 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
21970 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
21971 ++num_templates;
21974 /* Otherwise, the identifier is optional. */
21975 else
21977 /* We don't know whether what comes next is a template-id,
21978 an identifier, or nothing at all. */
21979 cp_parser_parse_tentatively (parser);
21980 /* Check for a template-id. */
21981 type_start_token = cp_lexer_peek_token (parser->lexer);
21982 id = cp_parser_template_id (parser,
21983 /*template_keyword_p=*/false,
21984 /*check_dependency_p=*/true,
21985 class_key,
21986 /*is_declaration=*/true);
21987 /* If that didn't work, it could still be an identifier. */
21988 if (!cp_parser_parse_definitely (parser))
21990 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21992 type_start_token = cp_lexer_peek_token (parser->lexer);
21993 id = cp_parser_identifier (parser);
21995 else
21996 id = NULL_TREE;
21998 else
22000 template_id_p = true;
22001 ++num_templates;
22005 pop_deferring_access_checks ();
22007 if (id)
22009 cp_parser_check_for_invalid_template_id (parser, id,
22010 class_key,
22011 type_start_token->location);
22013 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
22015 /* If it's not a `:' or a `{' then we can't really be looking at a
22016 class-head, since a class-head only appears as part of a
22017 class-specifier. We have to detect this situation before calling
22018 xref_tag, since that has irreversible side-effects. */
22019 if (!cp_parser_next_token_starts_class_definition_p (parser))
22021 cp_parser_error (parser, "expected %<{%> or %<:%>");
22022 type = error_mark_node;
22023 goto out;
22026 /* At this point, we're going ahead with the class-specifier, even
22027 if some other problem occurs. */
22028 cp_parser_commit_to_tentative_parse (parser);
22029 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
22031 cp_parser_error (parser,
22032 "cannot specify %<override%> for a class");
22033 type = error_mark_node;
22034 goto out;
22036 /* Issue the error about the overly-qualified name now. */
22037 if (qualified_p)
22039 cp_parser_error (parser,
22040 "global qualification of class name is invalid");
22041 type = error_mark_node;
22042 goto out;
22044 else if (invalid_nested_name_p)
22046 cp_parser_error (parser,
22047 "qualified name does not name a class");
22048 type = error_mark_node;
22049 goto out;
22051 else if (nested_name_specifier)
22053 tree scope;
22055 /* Reject typedef-names in class heads. */
22056 if (!DECL_IMPLICIT_TYPEDEF_P (type))
22058 error_at (type_start_token->location,
22059 "invalid class name in declaration of %qD",
22060 type);
22061 type = NULL_TREE;
22062 goto done;
22065 /* Figure out in what scope the declaration is being placed. */
22066 scope = current_scope ();
22067 /* If that scope does not contain the scope in which the
22068 class was originally declared, the program is invalid. */
22069 if (scope && !is_ancestor (scope, nested_name_specifier))
22071 if (at_namespace_scope_p ())
22072 error_at (type_start_token->location,
22073 "declaration of %qD in namespace %qD which does not "
22074 "enclose %qD",
22075 type, scope, nested_name_specifier);
22076 else
22077 error_at (type_start_token->location,
22078 "declaration of %qD in %qD which does not enclose %qD",
22079 type, scope, nested_name_specifier);
22080 type = NULL_TREE;
22081 goto done;
22083 /* [dcl.meaning]
22085 A declarator-id shall not be qualified except for the
22086 definition of a ... nested class outside of its class
22087 ... [or] the definition or explicit instantiation of a
22088 class member of a namespace outside of its namespace. */
22089 if (scope == nested_name_specifier)
22091 permerror (nested_name_specifier_token_start->location,
22092 "extra qualification not allowed");
22093 nested_name_specifier = NULL_TREE;
22094 num_templates = 0;
22097 /* An explicit-specialization must be preceded by "template <>". If
22098 it is not, try to recover gracefully. */
22099 if (at_namespace_scope_p ()
22100 && parser->num_template_parameter_lists == 0
22101 && !processing_template_parmlist
22102 && template_id_p)
22104 /* Build a location of this form:
22105 struct typename <ARGS>
22106 ^~~~~~~~~~~~~~~~~~~~~~
22107 with caret==start at the start token, and
22108 finishing at the end of the type. */
22109 location_t reported_loc
22110 = make_location (class_head_start_location,
22111 class_head_start_location,
22112 get_finish (type_start_token->location));
22113 rich_location richloc (line_table, reported_loc);
22114 richloc.add_fixit_insert_before (class_head_start_location,
22115 "template <> ");
22116 error_at_rich_loc
22117 (&richloc,
22118 "an explicit specialization must be preceded by %<template <>%>");
22119 invalid_explicit_specialization_p = true;
22120 /* Take the same action that would have been taken by
22121 cp_parser_explicit_specialization. */
22122 ++parser->num_template_parameter_lists;
22123 begin_specialization ();
22125 /* There must be no "return" statements between this point and the
22126 end of this function; set "type "to the correct return value and
22127 use "goto done;" to return. */
22128 /* Make sure that the right number of template parameters were
22129 present. */
22130 if (!cp_parser_check_template_parameters (parser, num_templates,
22131 type_start_token->location,
22132 /*declarator=*/NULL))
22134 /* If something went wrong, there is no point in even trying to
22135 process the class-definition. */
22136 type = NULL_TREE;
22137 goto done;
22140 /* Look up the type. */
22141 if (template_id_p)
22143 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
22144 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
22145 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
22147 error_at (type_start_token->location,
22148 "function template %qD redeclared as a class template", id);
22149 type = error_mark_node;
22151 else
22153 type = TREE_TYPE (id);
22154 type = maybe_process_partial_specialization (type);
22156 if (nested_name_specifier)
22157 pushed_scope = push_scope (nested_name_specifier);
22159 else if (nested_name_specifier)
22161 tree class_type;
22163 /* Given:
22165 template <typename T> struct S { struct T };
22166 template <typename T> struct S<T>::T { };
22168 we will get a TYPENAME_TYPE when processing the definition of
22169 `S::T'. We need to resolve it to the actual type before we
22170 try to define it. */
22171 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
22173 class_type = resolve_typename_type (TREE_TYPE (type),
22174 /*only_current_p=*/false);
22175 if (TREE_CODE (class_type) != TYPENAME_TYPE)
22176 type = TYPE_NAME (class_type);
22177 else
22179 cp_parser_error (parser, "could not resolve typename type");
22180 type = error_mark_node;
22184 if (maybe_process_partial_specialization (TREE_TYPE (type))
22185 == error_mark_node)
22187 type = NULL_TREE;
22188 goto done;
22191 class_type = current_class_type;
22192 /* Enter the scope indicated by the nested-name-specifier. */
22193 pushed_scope = push_scope (nested_name_specifier);
22194 /* Get the canonical version of this type. */
22195 type = TYPE_MAIN_DECL (TREE_TYPE (type));
22196 /* Call push_template_decl if it seems like we should be defining a
22197 template either from the template headers or the type we're
22198 defining, so that we diagnose both extra and missing headers. */
22199 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
22200 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
22201 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
22203 type = push_template_decl (type);
22204 if (type == error_mark_node)
22206 type = NULL_TREE;
22207 goto done;
22211 type = TREE_TYPE (type);
22212 *nested_name_specifier_p = true;
22214 else /* The name is not a nested name. */
22216 /* If the class was unnamed, create a dummy name. */
22217 if (!id)
22218 id = make_anon_name ();
22219 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
22220 parser->num_template_parameter_lists);
22223 /* Indicate whether this class was declared as a `class' or as a
22224 `struct'. */
22225 if (TREE_CODE (type) == RECORD_TYPE)
22226 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
22227 cp_parser_check_class_key (class_key, type);
22229 /* If this type was already complete, and we see another definition,
22230 that's an error. */
22231 if (type != error_mark_node && COMPLETE_TYPE_P (type))
22233 error_at (type_start_token->location, "redefinition of %q#T",
22234 type);
22235 inform (location_of (type), "previous definition of %q#T",
22236 type);
22237 type = NULL_TREE;
22238 goto done;
22240 else if (type == error_mark_node)
22241 type = NULL_TREE;
22243 if (type)
22245 /* Apply attributes now, before any use of the class as a template
22246 argument in its base list. */
22247 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
22248 fixup_attribute_variants (type);
22251 /* We will have entered the scope containing the class; the names of
22252 base classes should be looked up in that context. For example:
22254 struct A { struct B {}; struct C; };
22255 struct A::C : B {};
22257 is valid. */
22259 /* Get the list of base-classes, if there is one. */
22260 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22262 /* PR59482: enter the class scope so that base-specifiers are looked
22263 up correctly. */
22264 if (type)
22265 pushclass (type);
22266 bases = cp_parser_base_clause (parser);
22267 /* PR59482: get out of the previously pushed class scope so that the
22268 subsequent pops pop the right thing. */
22269 if (type)
22270 popclass ();
22272 else
22273 bases = NULL_TREE;
22275 /* If we're really defining a class, process the base classes.
22276 If they're invalid, fail. */
22277 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22278 xref_basetypes (type, bases);
22280 done:
22281 /* Leave the scope given by the nested-name-specifier. We will
22282 enter the class scope itself while processing the members. */
22283 if (pushed_scope)
22284 pop_scope (pushed_scope);
22286 if (invalid_explicit_specialization_p)
22288 end_specialization ();
22289 --parser->num_template_parameter_lists;
22292 if (type)
22293 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
22294 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
22295 CLASSTYPE_FINAL (type) = 1;
22296 out:
22297 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22298 return type;
22301 /* Parse a class-key.
22303 class-key:
22304 class
22305 struct
22306 union
22308 Returns the kind of class-key specified, or none_type to indicate
22309 error. */
22311 static enum tag_types
22312 cp_parser_class_key (cp_parser* parser)
22314 cp_token *token;
22315 enum tag_types tag_type;
22317 /* Look for the class-key. */
22318 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
22319 if (!token)
22320 return none_type;
22322 /* Check to see if the TOKEN is a class-key. */
22323 tag_type = cp_parser_token_is_class_key (token);
22324 if (!tag_type)
22325 cp_parser_error (parser, "expected class-key");
22326 return tag_type;
22329 /* Parse a type-parameter-key.
22331 type-parameter-key:
22332 class
22333 typename
22336 static void
22337 cp_parser_type_parameter_key (cp_parser* parser)
22339 /* Look for the type-parameter-key. */
22340 enum tag_types tag_type = none_type;
22341 cp_token *token = cp_lexer_peek_token (parser->lexer);
22342 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
22344 cp_lexer_consume_token (parser->lexer);
22345 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
22346 /* typename is not allowed in a template template parameter
22347 by the standard until C++1Z. */
22348 pedwarn (token->location, OPT_Wpedantic,
22349 "ISO C++ forbids typename key in template template parameter;"
22350 " use -std=c++1z or -std=gnu++1z");
22352 else
22353 cp_parser_error (parser, "expected %<class%> or %<typename%>");
22355 return;
22358 /* Parse an (optional) member-specification.
22360 member-specification:
22361 member-declaration member-specification [opt]
22362 access-specifier : member-specification [opt] */
22364 static void
22365 cp_parser_member_specification_opt (cp_parser* parser)
22367 while (true)
22369 cp_token *token;
22370 enum rid keyword;
22372 /* Peek at the next token. */
22373 token = cp_lexer_peek_token (parser->lexer);
22374 /* If it's a `}', or EOF then we've seen all the members. */
22375 if (token->type == CPP_CLOSE_BRACE
22376 || token->type == CPP_EOF
22377 || token->type == CPP_PRAGMA_EOL)
22378 break;
22380 /* See if this token is a keyword. */
22381 keyword = token->keyword;
22382 switch (keyword)
22384 case RID_PUBLIC:
22385 case RID_PROTECTED:
22386 case RID_PRIVATE:
22387 /* Consume the access-specifier. */
22388 cp_lexer_consume_token (parser->lexer);
22389 /* Remember which access-specifier is active. */
22390 current_access_specifier = token->u.value;
22391 /* Look for the `:'. */
22392 cp_parser_require (parser, CPP_COLON, RT_COLON);
22393 break;
22395 default:
22396 /* Accept #pragmas at class scope. */
22397 if (token->type == CPP_PRAGMA)
22399 cp_parser_pragma (parser, pragma_member, NULL);
22400 break;
22403 /* Otherwise, the next construction must be a
22404 member-declaration. */
22405 cp_parser_member_declaration (parser);
22410 /* Parse a member-declaration.
22412 member-declaration:
22413 decl-specifier-seq [opt] member-declarator-list [opt] ;
22414 function-definition ; [opt]
22415 :: [opt] nested-name-specifier template [opt] unqualified-id ;
22416 using-declaration
22417 template-declaration
22418 alias-declaration
22420 member-declarator-list:
22421 member-declarator
22422 member-declarator-list , member-declarator
22424 member-declarator:
22425 declarator pure-specifier [opt]
22426 declarator constant-initializer [opt]
22427 identifier [opt] : constant-expression
22429 GNU Extensions:
22431 member-declaration:
22432 __extension__ member-declaration
22434 member-declarator:
22435 declarator attributes [opt] pure-specifier [opt]
22436 declarator attributes [opt] constant-initializer [opt]
22437 identifier [opt] attributes [opt] : constant-expression
22439 C++0x Extensions:
22441 member-declaration:
22442 static_assert-declaration */
22444 static void
22445 cp_parser_member_declaration (cp_parser* parser)
22447 cp_decl_specifier_seq decl_specifiers;
22448 tree prefix_attributes;
22449 tree decl;
22450 int declares_class_or_enum;
22451 bool friend_p;
22452 cp_token *token = NULL;
22453 cp_token *decl_spec_token_start = NULL;
22454 cp_token *initializer_token_start = NULL;
22455 int saved_pedantic;
22456 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22458 /* Check for the `__extension__' keyword. */
22459 if (cp_parser_extension_opt (parser, &saved_pedantic))
22461 /* Recurse. */
22462 cp_parser_member_declaration (parser);
22463 /* Restore the old value of the PEDANTIC flag. */
22464 pedantic = saved_pedantic;
22466 return;
22469 /* Check for a template-declaration. */
22470 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22472 /* An explicit specialization here is an error condition, and we
22473 expect the specialization handler to detect and report this. */
22474 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
22475 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
22476 cp_parser_explicit_specialization (parser);
22477 else
22478 cp_parser_template_declaration (parser, /*member_p=*/true);
22480 return;
22482 /* Check for a template introduction. */
22483 else if (cp_parser_template_declaration_after_export (parser, true))
22484 return;
22486 /* Check for a using-declaration. */
22487 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
22489 if (cxx_dialect < cxx11)
22491 /* Parse the using-declaration. */
22492 cp_parser_using_declaration (parser,
22493 /*access_declaration_p=*/false);
22494 return;
22496 else
22498 tree decl;
22499 bool alias_decl_expected;
22500 cp_parser_parse_tentatively (parser);
22501 decl = cp_parser_alias_declaration (parser);
22502 /* Note that if we actually see the '=' token after the
22503 identifier, cp_parser_alias_declaration commits the
22504 tentative parse. In that case, we really expect an
22505 alias-declaration. Otherwise, we expect a using
22506 declaration. */
22507 alias_decl_expected =
22508 !cp_parser_uncommitted_to_tentative_parse_p (parser);
22509 cp_parser_parse_definitely (parser);
22511 if (alias_decl_expected)
22512 finish_member_declaration (decl);
22513 else
22514 cp_parser_using_declaration (parser,
22515 /*access_declaration_p=*/false);
22516 return;
22520 /* Check for @defs. */
22521 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
22523 tree ivar, member;
22524 tree ivar_chains = cp_parser_objc_defs_expression (parser);
22525 ivar = ivar_chains;
22526 while (ivar)
22528 member = ivar;
22529 ivar = TREE_CHAIN (member);
22530 TREE_CHAIN (member) = NULL_TREE;
22531 finish_member_declaration (member);
22533 return;
22536 /* If the next token is `static_assert' we have a static assertion. */
22537 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
22539 cp_parser_static_assert (parser, /*member_p=*/true);
22540 return;
22543 parser->colon_corrects_to_scope_p = false;
22545 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
22546 goto out;
22548 /* Parse the decl-specifier-seq. */
22549 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22550 cp_parser_decl_specifier_seq (parser,
22551 CP_PARSER_FLAGS_OPTIONAL,
22552 &decl_specifiers,
22553 &declares_class_or_enum);
22554 /* Check for an invalid type-name. */
22555 if (!decl_specifiers.any_type_specifiers_p
22556 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22557 goto out;
22558 /* If there is no declarator, then the decl-specifier-seq should
22559 specify a type. */
22560 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22562 /* If there was no decl-specifier-seq, and the next token is a
22563 `;', then we have something like:
22565 struct S { ; };
22567 [class.mem]
22569 Each member-declaration shall declare at least one member
22570 name of the class. */
22571 if (!decl_specifiers.any_specifiers_p)
22573 cp_token *token = cp_lexer_peek_token (parser->lexer);
22574 if (!in_system_header_at (token->location))
22575 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
22577 else
22579 tree type;
22581 /* See if this declaration is a friend. */
22582 friend_p = cp_parser_friend_p (&decl_specifiers);
22583 /* If there were decl-specifiers, check to see if there was
22584 a class-declaration. */
22585 type = check_tag_decl (&decl_specifiers,
22586 /*explicit_type_instantiation_p=*/false);
22587 /* Nested classes have already been added to the class, but
22588 a `friend' needs to be explicitly registered. */
22589 if (friend_p)
22591 /* If the `friend' keyword was present, the friend must
22592 be introduced with a class-key. */
22593 if (!declares_class_or_enum && cxx_dialect < cxx11)
22594 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
22595 "in C++03 a class-key must be used "
22596 "when declaring a friend");
22597 /* In this case:
22599 template <typename T> struct A {
22600 friend struct A<T>::B;
22603 A<T>::B will be represented by a TYPENAME_TYPE, and
22604 therefore not recognized by check_tag_decl. */
22605 if (!type)
22607 type = decl_specifiers.type;
22608 if (type && TREE_CODE (type) == TYPE_DECL)
22609 type = TREE_TYPE (type);
22611 if (!type || !TYPE_P (type))
22612 error_at (decl_spec_token_start->location,
22613 "friend declaration does not name a class or "
22614 "function");
22615 else
22616 make_friend_class (current_class_type, type,
22617 /*complain=*/true);
22619 /* If there is no TYPE, an error message will already have
22620 been issued. */
22621 else if (!type || type == error_mark_node)
22623 /* An anonymous aggregate has to be handled specially; such
22624 a declaration really declares a data member (with a
22625 particular type), as opposed to a nested class. */
22626 else if (ANON_AGGR_TYPE_P (type))
22628 /* C++11 9.5/6. */
22629 if (decl_specifiers.storage_class != sc_none)
22630 error_at (decl_spec_token_start->location,
22631 "a storage class on an anonymous aggregate "
22632 "in class scope is not allowed");
22634 /* Remove constructors and such from TYPE, now that we
22635 know it is an anonymous aggregate. */
22636 fixup_anonymous_aggr (type);
22637 /* And make the corresponding data member. */
22638 decl = build_decl (decl_spec_token_start->location,
22639 FIELD_DECL, NULL_TREE, type);
22640 /* Add it to the class. */
22641 finish_member_declaration (decl);
22643 else
22644 cp_parser_check_access_in_redeclaration
22645 (TYPE_NAME (type),
22646 decl_spec_token_start->location);
22649 else
22651 bool assume_semicolon = false;
22653 /* Clear attributes from the decl_specifiers but keep them
22654 around as prefix attributes that apply them to the entity
22655 being declared. */
22656 prefix_attributes = decl_specifiers.attributes;
22657 decl_specifiers.attributes = NULL_TREE;
22659 /* See if these declarations will be friends. */
22660 friend_p = cp_parser_friend_p (&decl_specifiers);
22662 /* Keep going until we hit the `;' at the end of the
22663 declaration. */
22664 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22666 tree attributes = NULL_TREE;
22667 tree first_attribute;
22669 /* Peek at the next token. */
22670 token = cp_lexer_peek_token (parser->lexer);
22672 /* Check for a bitfield declaration. */
22673 if (token->type == CPP_COLON
22674 || (token->type == CPP_NAME
22675 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
22676 == CPP_COLON))
22678 tree identifier;
22679 tree width;
22681 /* Get the name of the bitfield. Note that we cannot just
22682 check TOKEN here because it may have been invalidated by
22683 the call to cp_lexer_peek_nth_token above. */
22684 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
22685 identifier = cp_parser_identifier (parser);
22686 else
22687 identifier = NULL_TREE;
22689 /* Consume the `:' token. */
22690 cp_lexer_consume_token (parser->lexer);
22691 /* Get the width of the bitfield. */
22692 width
22693 = cp_parser_constant_expression (parser);
22695 /* Look for attributes that apply to the bitfield. */
22696 attributes = cp_parser_attributes_opt (parser);
22697 /* Remember which attributes are prefix attributes and
22698 which are not. */
22699 first_attribute = attributes;
22700 /* Combine the attributes. */
22701 attributes = chainon (prefix_attributes, attributes);
22703 /* Create the bitfield declaration. */
22704 decl = grokbitfield (identifier
22705 ? make_id_declarator (NULL_TREE,
22706 identifier,
22707 sfk_none)
22708 : NULL,
22709 &decl_specifiers,
22710 width,
22711 attributes);
22713 else
22715 cp_declarator *declarator;
22716 tree initializer;
22717 tree asm_specification;
22718 int ctor_dtor_or_conv_p;
22720 /* Parse the declarator. */
22721 declarator
22722 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22723 &ctor_dtor_or_conv_p,
22724 /*parenthesized_p=*/NULL,
22725 /*member_p=*/true,
22726 friend_p);
22728 /* If something went wrong parsing the declarator, make sure
22729 that we at least consume some tokens. */
22730 if (declarator == cp_error_declarator)
22732 /* Skip to the end of the statement. */
22733 cp_parser_skip_to_end_of_statement (parser);
22734 /* If the next token is not a semicolon, that is
22735 probably because we just skipped over the body of
22736 a function. So, we consume a semicolon if
22737 present, but do not issue an error message if it
22738 is not present. */
22739 if (cp_lexer_next_token_is (parser->lexer,
22740 CPP_SEMICOLON))
22741 cp_lexer_consume_token (parser->lexer);
22742 goto out;
22745 if (declares_class_or_enum & 2)
22746 cp_parser_check_for_definition_in_return_type
22747 (declarator, decl_specifiers.type,
22748 decl_specifiers.locations[ds_type_spec]);
22750 /* Look for an asm-specification. */
22751 asm_specification = cp_parser_asm_specification_opt (parser);
22752 /* Look for attributes that apply to the declaration. */
22753 attributes = cp_parser_attributes_opt (parser);
22754 /* Remember which attributes are prefix attributes and
22755 which are not. */
22756 first_attribute = attributes;
22757 /* Combine the attributes. */
22758 attributes = chainon (prefix_attributes, attributes);
22760 /* If it's an `=', then we have a constant-initializer or a
22761 pure-specifier. It is not correct to parse the
22762 initializer before registering the member declaration
22763 since the member declaration should be in scope while
22764 its initializer is processed. However, the rest of the
22765 front end does not yet provide an interface that allows
22766 us to handle this correctly. */
22767 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22769 /* In [class.mem]:
22771 A pure-specifier shall be used only in the declaration of
22772 a virtual function.
22774 A member-declarator can contain a constant-initializer
22775 only if it declares a static member of integral or
22776 enumeration type.
22778 Therefore, if the DECLARATOR is for a function, we look
22779 for a pure-specifier; otherwise, we look for a
22780 constant-initializer. When we call `grokfield', it will
22781 perform more stringent semantics checks. */
22782 initializer_token_start = cp_lexer_peek_token (parser->lexer);
22783 if (function_declarator_p (declarator)
22784 || (decl_specifiers.type
22785 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
22786 && declarator->kind == cdk_id
22787 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
22788 == FUNCTION_TYPE)))
22789 initializer = cp_parser_pure_specifier (parser);
22790 else if (decl_specifiers.storage_class != sc_static)
22791 initializer = cp_parser_save_nsdmi (parser);
22792 else if (cxx_dialect >= cxx11)
22794 bool nonconst;
22795 /* Don't require a constant rvalue in C++11, since we
22796 might want a reference constant. We'll enforce
22797 constancy later. */
22798 cp_lexer_consume_token (parser->lexer);
22799 /* Parse the initializer. */
22800 initializer = cp_parser_initializer_clause (parser,
22801 &nonconst);
22803 else
22804 /* Parse the initializer. */
22805 initializer = cp_parser_constant_initializer (parser);
22807 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
22808 && !function_declarator_p (declarator))
22810 bool x;
22811 if (decl_specifiers.storage_class != sc_static)
22812 initializer = cp_parser_save_nsdmi (parser);
22813 else
22814 initializer = cp_parser_initializer (parser, &x, &x);
22816 /* Otherwise, there is no initializer. */
22817 else
22818 initializer = NULL_TREE;
22820 /* See if we are probably looking at a function
22821 definition. We are certainly not looking at a
22822 member-declarator. Calling `grokfield' has
22823 side-effects, so we must not do it unless we are sure
22824 that we are looking at a member-declarator. */
22825 if (cp_parser_token_starts_function_definition_p
22826 (cp_lexer_peek_token (parser->lexer)))
22828 /* The grammar does not allow a pure-specifier to be
22829 used when a member function is defined. (It is
22830 possible that this fact is an oversight in the
22831 standard, since a pure function may be defined
22832 outside of the class-specifier. */
22833 if (initializer && initializer_token_start)
22834 error_at (initializer_token_start->location,
22835 "pure-specifier on function-definition");
22836 decl = cp_parser_save_member_function_body (parser,
22837 &decl_specifiers,
22838 declarator,
22839 attributes);
22840 if (parser->fully_implicit_function_template_p)
22841 decl = finish_fully_implicit_template (parser, decl);
22842 /* If the member was not a friend, declare it here. */
22843 if (!friend_p)
22844 finish_member_declaration (decl);
22845 /* Peek at the next token. */
22846 token = cp_lexer_peek_token (parser->lexer);
22847 /* If the next token is a semicolon, consume it. */
22848 if (token->type == CPP_SEMICOLON)
22849 cp_lexer_consume_token (parser->lexer);
22850 goto out;
22852 else
22853 if (declarator->kind == cdk_function)
22854 declarator->id_loc = token->location;
22855 /* Create the declaration. */
22856 decl = grokfield (declarator, &decl_specifiers,
22857 initializer, /*init_const_expr_p=*/true,
22858 asm_specification, attributes);
22859 if (parser->fully_implicit_function_template_p)
22861 if (friend_p)
22862 finish_fully_implicit_template (parser, 0);
22863 else
22864 decl = finish_fully_implicit_template (parser, decl);
22868 cp_finalize_omp_declare_simd (parser, decl);
22869 cp_finalize_oacc_routine (parser, decl, false);
22871 /* Reset PREFIX_ATTRIBUTES. */
22872 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22873 attributes = TREE_CHAIN (attributes);
22874 if (attributes)
22875 TREE_CHAIN (attributes) = NULL_TREE;
22877 /* If there is any qualification still in effect, clear it
22878 now; we will be starting fresh with the next declarator. */
22879 parser->scope = NULL_TREE;
22880 parser->qualifying_scope = NULL_TREE;
22881 parser->object_scope = NULL_TREE;
22882 /* If it's a `,', then there are more declarators. */
22883 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22885 cp_lexer_consume_token (parser->lexer);
22886 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22888 cp_token *token = cp_lexer_previous_token (parser->lexer);
22889 error_at (token->location,
22890 "stray %<,%> at end of member declaration");
22893 /* If the next token isn't a `;', then we have a parse error. */
22894 else if (cp_lexer_next_token_is_not (parser->lexer,
22895 CPP_SEMICOLON))
22897 /* The next token might be a ways away from where the
22898 actual semicolon is missing. Find the previous token
22899 and use that for our error position. */
22900 cp_token *token = cp_lexer_previous_token (parser->lexer);
22901 error_at (token->location,
22902 "expected %<;%> at end of member declaration");
22904 /* Assume that the user meant to provide a semicolon. If
22905 we were to cp_parser_skip_to_end_of_statement, we might
22906 skip to a semicolon inside a member function definition
22907 and issue nonsensical error messages. */
22908 assume_semicolon = true;
22911 if (decl)
22913 /* Add DECL to the list of members. */
22914 if (!friend_p
22915 /* Explicitly include, eg, NSDMIs, for better error
22916 recovery (c++/58650). */
22917 || !DECL_DECLARES_FUNCTION_P (decl))
22918 finish_member_declaration (decl);
22920 if (TREE_CODE (decl) == FUNCTION_DECL)
22921 cp_parser_save_default_args (parser, decl);
22922 else if (TREE_CODE (decl) == FIELD_DECL
22923 && !DECL_C_BIT_FIELD (decl)
22924 && DECL_INITIAL (decl))
22925 /* Add DECL to the queue of NSDMI to be parsed later. */
22926 vec_safe_push (unparsed_nsdmis, decl);
22929 if (assume_semicolon)
22930 goto out;
22934 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22935 out:
22936 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22939 /* Parse a pure-specifier.
22941 pure-specifier:
22944 Returns INTEGER_ZERO_NODE if a pure specifier is found.
22945 Otherwise, ERROR_MARK_NODE is returned. */
22947 static tree
22948 cp_parser_pure_specifier (cp_parser* parser)
22950 cp_token *token;
22952 /* Look for the `=' token. */
22953 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
22954 return error_mark_node;
22955 /* Look for the `0' token. */
22956 token = cp_lexer_peek_token (parser->lexer);
22958 if (token->type == CPP_EOF
22959 || token->type == CPP_PRAGMA_EOL)
22960 return error_mark_node;
22962 cp_lexer_consume_token (parser->lexer);
22964 /* Accept = default or = delete in c++0x mode. */
22965 if (token->keyword == RID_DEFAULT
22966 || token->keyword == RID_DELETE)
22968 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
22969 return token->u.value;
22972 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
22973 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
22975 cp_parser_error (parser,
22976 "invalid pure specifier (only %<= 0%> is allowed)");
22977 cp_parser_skip_to_end_of_statement (parser);
22978 return error_mark_node;
22980 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
22982 error_at (token->location, "templates may not be %<virtual%>");
22983 return error_mark_node;
22986 return integer_zero_node;
22989 /* Parse a constant-initializer.
22991 constant-initializer:
22992 = constant-expression
22994 Returns a representation of the constant-expression. */
22996 static tree
22997 cp_parser_constant_initializer (cp_parser* parser)
22999 /* Look for the `=' token. */
23000 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23001 return error_mark_node;
23003 /* It is invalid to write:
23005 struct S { static const int i = { 7 }; };
23008 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23010 cp_parser_error (parser,
23011 "a brace-enclosed initializer is not allowed here");
23012 /* Consume the opening brace. */
23013 cp_lexer_consume_token (parser->lexer);
23014 /* Skip the initializer. */
23015 cp_parser_skip_to_closing_brace (parser);
23016 /* Look for the trailing `}'. */
23017 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
23019 return error_mark_node;
23022 return cp_parser_constant_expression (parser);
23025 /* Derived classes [gram.class.derived] */
23027 /* Parse a base-clause.
23029 base-clause:
23030 : base-specifier-list
23032 base-specifier-list:
23033 base-specifier ... [opt]
23034 base-specifier-list , base-specifier ... [opt]
23036 Returns a TREE_LIST representing the base-classes, in the order in
23037 which they were declared. The representation of each node is as
23038 described by cp_parser_base_specifier.
23040 In the case that no bases are specified, this function will return
23041 NULL_TREE, not ERROR_MARK_NODE. */
23043 static tree
23044 cp_parser_base_clause (cp_parser* parser)
23046 tree bases = NULL_TREE;
23048 /* Look for the `:' that begins the list. */
23049 cp_parser_require (parser, CPP_COLON, RT_COLON);
23051 /* Scan the base-specifier-list. */
23052 while (true)
23054 cp_token *token;
23055 tree base;
23056 bool pack_expansion_p = false;
23058 /* Look for the base-specifier. */
23059 base = cp_parser_base_specifier (parser);
23060 /* Look for the (optional) ellipsis. */
23061 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23063 /* Consume the `...'. */
23064 cp_lexer_consume_token (parser->lexer);
23066 pack_expansion_p = true;
23069 /* Add BASE to the front of the list. */
23070 if (base && base != error_mark_node)
23072 if (pack_expansion_p)
23073 /* Make this a pack expansion type. */
23074 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
23076 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
23078 TREE_CHAIN (base) = bases;
23079 bases = base;
23082 /* Peek at the next token. */
23083 token = cp_lexer_peek_token (parser->lexer);
23084 /* If it's not a comma, then the list is complete. */
23085 if (token->type != CPP_COMMA)
23086 break;
23087 /* Consume the `,'. */
23088 cp_lexer_consume_token (parser->lexer);
23091 /* PARSER->SCOPE may still be non-NULL at this point, if the last
23092 base class had a qualified name. However, the next name that
23093 appears is certainly not qualified. */
23094 parser->scope = NULL_TREE;
23095 parser->qualifying_scope = NULL_TREE;
23096 parser->object_scope = NULL_TREE;
23098 return nreverse (bases);
23101 /* Parse a base-specifier.
23103 base-specifier:
23104 :: [opt] nested-name-specifier [opt] class-name
23105 virtual access-specifier [opt] :: [opt] nested-name-specifier
23106 [opt] class-name
23107 access-specifier virtual [opt] :: [opt] nested-name-specifier
23108 [opt] class-name
23110 Returns a TREE_LIST. The TREE_PURPOSE will be one of
23111 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
23112 indicate the specifiers provided. The TREE_VALUE will be a TYPE
23113 (or the ERROR_MARK_NODE) indicating the type that was specified. */
23115 static tree
23116 cp_parser_base_specifier (cp_parser* parser)
23118 cp_token *token;
23119 bool done = false;
23120 bool virtual_p = false;
23121 bool duplicate_virtual_error_issued_p = false;
23122 bool duplicate_access_error_issued_p = false;
23123 bool class_scope_p, template_p;
23124 tree access = access_default_node;
23125 tree type;
23127 /* Process the optional `virtual' and `access-specifier'. */
23128 while (!done)
23130 /* Peek at the next token. */
23131 token = cp_lexer_peek_token (parser->lexer);
23132 /* Process `virtual'. */
23133 switch (token->keyword)
23135 case RID_VIRTUAL:
23136 /* If `virtual' appears more than once, issue an error. */
23137 if (virtual_p && !duplicate_virtual_error_issued_p)
23139 cp_parser_error (parser,
23140 "%<virtual%> specified more than once in base-specified");
23141 duplicate_virtual_error_issued_p = true;
23144 virtual_p = true;
23146 /* Consume the `virtual' token. */
23147 cp_lexer_consume_token (parser->lexer);
23149 break;
23151 case RID_PUBLIC:
23152 case RID_PROTECTED:
23153 case RID_PRIVATE:
23154 /* If more than one access specifier appears, issue an
23155 error. */
23156 if (access != access_default_node
23157 && !duplicate_access_error_issued_p)
23159 cp_parser_error (parser,
23160 "more than one access specifier in base-specified");
23161 duplicate_access_error_issued_p = true;
23164 access = ridpointers[(int) token->keyword];
23166 /* Consume the access-specifier. */
23167 cp_lexer_consume_token (parser->lexer);
23169 break;
23171 default:
23172 done = true;
23173 break;
23176 /* It is not uncommon to see programs mechanically, erroneously, use
23177 the 'typename' keyword to denote (dependent) qualified types
23178 as base classes. */
23179 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
23181 token = cp_lexer_peek_token (parser->lexer);
23182 if (!processing_template_decl)
23183 error_at (token->location,
23184 "keyword %<typename%> not allowed outside of templates");
23185 else
23186 error_at (token->location,
23187 "keyword %<typename%> not allowed in this context "
23188 "(the base class is implicitly a type)");
23189 cp_lexer_consume_token (parser->lexer);
23192 /* Look for the optional `::' operator. */
23193 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
23194 /* Look for the nested-name-specifier. The simplest way to
23195 implement:
23197 [temp.res]
23199 The keyword `typename' is not permitted in a base-specifier or
23200 mem-initializer; in these contexts a qualified name that
23201 depends on a template-parameter is implicitly assumed to be a
23202 type name.
23204 is to pretend that we have seen the `typename' keyword at this
23205 point. */
23206 cp_parser_nested_name_specifier_opt (parser,
23207 /*typename_keyword_p=*/true,
23208 /*check_dependency_p=*/true,
23209 typename_type,
23210 /*is_declaration=*/true);
23211 /* If the base class is given by a qualified name, assume that names
23212 we see are type names or templates, as appropriate. */
23213 class_scope_p = (parser->scope && TYPE_P (parser->scope));
23214 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
23216 if (!parser->scope
23217 && cp_lexer_next_token_is_decltype (parser->lexer))
23218 /* DR 950 allows decltype as a base-specifier. */
23219 type = cp_parser_decltype (parser);
23220 else
23222 /* Otherwise, look for the class-name. */
23223 type = cp_parser_class_name (parser,
23224 class_scope_p,
23225 template_p,
23226 typename_type,
23227 /*check_dependency_p=*/true,
23228 /*class_head_p=*/false,
23229 /*is_declaration=*/true);
23230 type = TREE_TYPE (type);
23233 if (type == error_mark_node)
23234 return error_mark_node;
23236 return finish_base_specifier (type, access, virtual_p);
23239 /* Exception handling [gram.exception] */
23241 /* Parse an (optional) noexcept-specification.
23243 noexcept-specification:
23244 noexcept ( constant-expression ) [opt]
23246 If no noexcept-specification is present, returns NULL_TREE.
23247 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
23248 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
23249 there are no parentheses. CONSUMED_EXPR will be set accordingly.
23250 Otherwise, returns a noexcept specification unless RETURN_COND is true,
23251 in which case a boolean condition is returned instead. */
23253 static tree
23254 cp_parser_noexcept_specification_opt (cp_parser* parser,
23255 bool require_constexpr,
23256 bool* consumed_expr,
23257 bool return_cond)
23259 cp_token *token;
23260 const char *saved_message;
23262 /* Peek at the next token. */
23263 token = cp_lexer_peek_token (parser->lexer);
23265 /* Is it a noexcept-specification? */
23266 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
23268 tree expr;
23269 cp_lexer_consume_token (parser->lexer);
23271 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
23273 cp_lexer_consume_token (parser->lexer);
23275 if (require_constexpr)
23277 /* Types may not be defined in an exception-specification. */
23278 saved_message = parser->type_definition_forbidden_message;
23279 parser->type_definition_forbidden_message
23280 = G_("types may not be defined in an exception-specification");
23282 expr = cp_parser_constant_expression (parser);
23284 /* Restore the saved message. */
23285 parser->type_definition_forbidden_message = saved_message;
23287 else
23289 expr = cp_parser_expression (parser);
23290 *consumed_expr = true;
23293 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23295 else
23297 expr = boolean_true_node;
23298 if (!require_constexpr)
23299 *consumed_expr = false;
23302 /* We cannot build a noexcept-spec right away because this will check
23303 that expr is a constexpr. */
23304 if (!return_cond)
23305 return build_noexcept_spec (expr, tf_warning_or_error);
23306 else
23307 return expr;
23309 else
23310 return NULL_TREE;
23313 /* Parse an (optional) exception-specification.
23315 exception-specification:
23316 throw ( type-id-list [opt] )
23318 Returns a TREE_LIST representing the exception-specification. The
23319 TREE_VALUE of each node is a type. */
23321 static tree
23322 cp_parser_exception_specification_opt (cp_parser* parser)
23324 cp_token *token;
23325 tree type_id_list;
23326 const char *saved_message;
23328 /* Peek at the next token. */
23329 token = cp_lexer_peek_token (parser->lexer);
23331 /* Is it a noexcept-specification? */
23332 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
23333 false);
23334 if (type_id_list != NULL_TREE)
23335 return type_id_list;
23337 /* If it's not `throw', then there's no exception-specification. */
23338 if (!cp_parser_is_keyword (token, RID_THROW))
23339 return NULL_TREE;
23341 #if 0
23342 /* Enable this once a lot of code has transitioned to noexcept? */
23343 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
23344 warning (OPT_Wdeprecated, "dynamic exception specifications are "
23345 "deprecated in C++0x; use %<noexcept%> instead");
23346 #endif
23348 /* Consume the `throw'. */
23349 cp_lexer_consume_token (parser->lexer);
23351 /* Look for the `('. */
23352 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23354 /* Peek at the next token. */
23355 token = cp_lexer_peek_token (parser->lexer);
23356 /* If it's not a `)', then there is a type-id-list. */
23357 if (token->type != CPP_CLOSE_PAREN)
23359 /* Types may not be defined in an exception-specification. */
23360 saved_message = parser->type_definition_forbidden_message;
23361 parser->type_definition_forbidden_message
23362 = G_("types may not be defined in an exception-specification");
23363 /* Parse the type-id-list. */
23364 type_id_list = cp_parser_type_id_list (parser);
23365 /* Restore the saved message. */
23366 parser->type_definition_forbidden_message = saved_message;
23368 else
23369 type_id_list = empty_except_spec;
23371 /* Look for the `)'. */
23372 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23374 return type_id_list;
23377 /* Parse an (optional) type-id-list.
23379 type-id-list:
23380 type-id ... [opt]
23381 type-id-list , type-id ... [opt]
23383 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
23384 in the order that the types were presented. */
23386 static tree
23387 cp_parser_type_id_list (cp_parser* parser)
23389 tree types = NULL_TREE;
23391 while (true)
23393 cp_token *token;
23394 tree type;
23396 token = cp_lexer_peek_token (parser->lexer);
23398 /* Get the next type-id. */
23399 type = cp_parser_type_id (parser);
23400 /* Check for invalid 'auto'. */
23401 if (flag_concepts && type_uses_auto (type))
23403 error_at (token->location,
23404 "invalid use of %<auto%> in exception-specification");
23405 type = error_mark_node;
23407 /* Parse the optional ellipsis. */
23408 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23410 /* Consume the `...'. */
23411 cp_lexer_consume_token (parser->lexer);
23413 /* Turn the type into a pack expansion expression. */
23414 type = make_pack_expansion (type);
23416 /* Add it to the list. */
23417 types = add_exception_specifier (types, type, /*complain=*/1);
23418 /* Peek at the next token. */
23419 token = cp_lexer_peek_token (parser->lexer);
23420 /* If it is not a `,', we are done. */
23421 if (token->type != CPP_COMMA)
23422 break;
23423 /* Consume the `,'. */
23424 cp_lexer_consume_token (parser->lexer);
23427 return nreverse (types);
23430 /* Parse a try-block.
23432 try-block:
23433 try compound-statement handler-seq */
23435 static tree
23436 cp_parser_try_block (cp_parser* parser)
23438 tree try_block;
23440 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
23441 if (parser->in_function_body
23442 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
23443 error ("%<try%> in %<constexpr%> function");
23445 try_block = begin_try_block ();
23446 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
23447 finish_try_block (try_block);
23448 cp_parser_handler_seq (parser);
23449 finish_handler_sequence (try_block);
23451 return try_block;
23454 /* Parse a function-try-block.
23456 function-try-block:
23457 try ctor-initializer [opt] function-body handler-seq */
23459 static bool
23460 cp_parser_function_try_block (cp_parser* parser)
23462 tree compound_stmt;
23463 tree try_block;
23464 bool ctor_initializer_p;
23466 /* Look for the `try' keyword. */
23467 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
23468 return false;
23469 /* Let the rest of the front end know where we are. */
23470 try_block = begin_function_try_block (&compound_stmt);
23471 /* Parse the function-body. */
23472 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23473 (parser, /*in_function_try_block=*/true);
23474 /* We're done with the `try' part. */
23475 finish_function_try_block (try_block);
23476 /* Parse the handlers. */
23477 cp_parser_handler_seq (parser);
23478 /* We're done with the handlers. */
23479 finish_function_handler_sequence (try_block, compound_stmt);
23481 return ctor_initializer_p;
23484 /* Parse a handler-seq.
23486 handler-seq:
23487 handler handler-seq [opt] */
23489 static void
23490 cp_parser_handler_seq (cp_parser* parser)
23492 while (true)
23494 cp_token *token;
23496 /* Parse the handler. */
23497 cp_parser_handler (parser);
23498 /* Peek at the next token. */
23499 token = cp_lexer_peek_token (parser->lexer);
23500 /* If it's not `catch' then there are no more handlers. */
23501 if (!cp_parser_is_keyword (token, RID_CATCH))
23502 break;
23506 /* Parse a handler.
23508 handler:
23509 catch ( exception-declaration ) compound-statement */
23511 static void
23512 cp_parser_handler (cp_parser* parser)
23514 tree handler;
23515 tree declaration;
23517 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
23518 handler = begin_handler ();
23519 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23520 declaration = cp_parser_exception_declaration (parser);
23521 finish_handler_parms (declaration, handler);
23522 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23523 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
23524 finish_handler (handler);
23527 /* Parse an exception-declaration.
23529 exception-declaration:
23530 type-specifier-seq declarator
23531 type-specifier-seq abstract-declarator
23532 type-specifier-seq
23535 Returns a VAR_DECL for the declaration, or NULL_TREE if the
23536 ellipsis variant is used. */
23538 static tree
23539 cp_parser_exception_declaration (cp_parser* parser)
23541 cp_decl_specifier_seq type_specifiers;
23542 cp_declarator *declarator;
23543 const char *saved_message;
23545 /* If it's an ellipsis, it's easy to handle. */
23546 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23548 /* Consume the `...' token. */
23549 cp_lexer_consume_token (parser->lexer);
23550 return NULL_TREE;
23553 /* Types may not be defined in exception-declarations. */
23554 saved_message = parser->type_definition_forbidden_message;
23555 parser->type_definition_forbidden_message
23556 = G_("types may not be defined in exception-declarations");
23558 /* Parse the type-specifier-seq. */
23559 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
23560 /*is_trailing_return=*/false,
23561 &type_specifiers);
23562 /* If it's a `)', then there is no declarator. */
23563 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
23564 declarator = NULL;
23565 else
23566 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
23567 /*ctor_dtor_or_conv_p=*/NULL,
23568 /*parenthesized_p=*/NULL,
23569 /*member_p=*/false,
23570 /*friend_p=*/false);
23572 /* Restore the saved message. */
23573 parser->type_definition_forbidden_message = saved_message;
23575 if (!type_specifiers.any_specifiers_p)
23576 return error_mark_node;
23578 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
23581 /* Parse a throw-expression.
23583 throw-expression:
23584 throw assignment-expression [opt]
23586 Returns a THROW_EXPR representing the throw-expression. */
23588 static tree
23589 cp_parser_throw_expression (cp_parser* parser)
23591 tree expression;
23592 cp_token* token;
23594 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
23595 token = cp_lexer_peek_token (parser->lexer);
23596 /* Figure out whether or not there is an assignment-expression
23597 following the "throw" keyword. */
23598 if (token->type == CPP_COMMA
23599 || token->type == CPP_SEMICOLON
23600 || token->type == CPP_CLOSE_PAREN
23601 || token->type == CPP_CLOSE_SQUARE
23602 || token->type == CPP_CLOSE_BRACE
23603 || token->type == CPP_COLON)
23604 expression = NULL_TREE;
23605 else
23606 expression = cp_parser_assignment_expression (parser);
23608 return build_throw (expression);
23611 /* GNU Extensions */
23613 /* Parse an (optional) asm-specification.
23615 asm-specification:
23616 asm ( string-literal )
23618 If the asm-specification is present, returns a STRING_CST
23619 corresponding to the string-literal. Otherwise, returns
23620 NULL_TREE. */
23622 static tree
23623 cp_parser_asm_specification_opt (cp_parser* parser)
23625 cp_token *token;
23626 tree asm_specification;
23628 /* Peek at the next token. */
23629 token = cp_lexer_peek_token (parser->lexer);
23630 /* If the next token isn't the `asm' keyword, then there's no
23631 asm-specification. */
23632 if (!cp_parser_is_keyword (token, RID_ASM))
23633 return NULL_TREE;
23635 /* Consume the `asm' token. */
23636 cp_lexer_consume_token (parser->lexer);
23637 /* Look for the `('. */
23638 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23640 /* Look for the string-literal. */
23641 asm_specification = cp_parser_string_literal (parser, false, false);
23643 /* Look for the `)'. */
23644 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23646 return asm_specification;
23649 /* Parse an asm-operand-list.
23651 asm-operand-list:
23652 asm-operand
23653 asm-operand-list , asm-operand
23655 asm-operand:
23656 string-literal ( expression )
23657 [ string-literal ] string-literal ( expression )
23659 Returns a TREE_LIST representing the operands. The TREE_VALUE of
23660 each node is the expression. The TREE_PURPOSE is itself a
23661 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
23662 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
23663 is a STRING_CST for the string literal before the parenthesis. Returns
23664 ERROR_MARK_NODE if any of the operands are invalid. */
23666 static tree
23667 cp_parser_asm_operand_list (cp_parser* parser)
23669 tree asm_operands = NULL_TREE;
23670 bool invalid_operands = false;
23672 while (true)
23674 tree string_literal;
23675 tree expression;
23676 tree name;
23678 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
23680 /* Consume the `[' token. */
23681 cp_lexer_consume_token (parser->lexer);
23682 /* Read the operand name. */
23683 name = cp_parser_identifier (parser);
23684 if (name != error_mark_node)
23685 name = build_string (IDENTIFIER_LENGTH (name),
23686 IDENTIFIER_POINTER (name));
23687 /* Look for the closing `]'. */
23688 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23690 else
23691 name = NULL_TREE;
23692 /* Look for the string-literal. */
23693 string_literal = cp_parser_string_literal (parser, false, false);
23695 /* Look for the `('. */
23696 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23697 /* Parse the expression. */
23698 expression = cp_parser_expression (parser);
23699 /* Look for the `)'. */
23700 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23702 if (name == error_mark_node
23703 || string_literal == error_mark_node
23704 || expression == error_mark_node)
23705 invalid_operands = true;
23707 /* Add this operand to the list. */
23708 asm_operands = tree_cons (build_tree_list (name, string_literal),
23709 expression,
23710 asm_operands);
23711 /* If the next token is not a `,', there are no more
23712 operands. */
23713 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23714 break;
23715 /* Consume the `,'. */
23716 cp_lexer_consume_token (parser->lexer);
23719 return invalid_operands ? error_mark_node : nreverse (asm_operands);
23722 /* Parse an asm-clobber-list.
23724 asm-clobber-list:
23725 string-literal
23726 asm-clobber-list , string-literal
23728 Returns a TREE_LIST, indicating the clobbers in the order that they
23729 appeared. The TREE_VALUE of each node is a STRING_CST. */
23731 static tree
23732 cp_parser_asm_clobber_list (cp_parser* parser)
23734 tree clobbers = NULL_TREE;
23736 while (true)
23738 tree string_literal;
23740 /* Look for the string literal. */
23741 string_literal = cp_parser_string_literal (parser, false, false);
23742 /* Add it to the list. */
23743 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
23744 /* If the next token is not a `,', then the list is
23745 complete. */
23746 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23747 break;
23748 /* Consume the `,' token. */
23749 cp_lexer_consume_token (parser->lexer);
23752 return clobbers;
23755 /* Parse an asm-label-list.
23757 asm-label-list:
23758 identifier
23759 asm-label-list , identifier
23761 Returns a TREE_LIST, indicating the labels in the order that they
23762 appeared. The TREE_VALUE of each node is a label. */
23764 static tree
23765 cp_parser_asm_label_list (cp_parser* parser)
23767 tree labels = NULL_TREE;
23769 while (true)
23771 tree identifier, label, name;
23773 /* Look for the identifier. */
23774 identifier = cp_parser_identifier (parser);
23775 if (!error_operand_p (identifier))
23777 label = lookup_label (identifier);
23778 if (TREE_CODE (label) == LABEL_DECL)
23780 TREE_USED (label) = 1;
23781 check_goto (label);
23782 name = build_string (IDENTIFIER_LENGTH (identifier),
23783 IDENTIFIER_POINTER (identifier));
23784 labels = tree_cons (name, label, labels);
23787 /* If the next token is not a `,', then the list is
23788 complete. */
23789 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23790 break;
23791 /* Consume the `,' token. */
23792 cp_lexer_consume_token (parser->lexer);
23795 return nreverse (labels);
23798 /* Return TRUE iff the next tokens in the stream are possibly the
23799 beginning of a GNU extension attribute. */
23801 static bool
23802 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
23804 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
23807 /* Return TRUE iff the next tokens in the stream are possibly the
23808 beginning of a standard C++-11 attribute specifier. */
23810 static bool
23811 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
23813 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
23816 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23817 beginning of a standard C++-11 attribute specifier. */
23819 static bool
23820 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
23822 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23824 return (cxx_dialect >= cxx11
23825 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
23826 || (token->type == CPP_OPEN_SQUARE
23827 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
23828 && token->type == CPP_OPEN_SQUARE)));
23831 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23832 beginning of a GNU extension attribute. */
23834 static bool
23835 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
23837 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23839 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
23842 /* Return true iff the next tokens can be the beginning of either a
23843 GNU attribute list, or a standard C++11 attribute sequence. */
23845 static bool
23846 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
23848 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
23849 || cp_next_tokens_can_be_std_attribute_p (parser));
23852 /* Return true iff the next Nth tokens can be the beginning of either
23853 a GNU attribute list, or a standard C++11 attribute sequence. */
23855 static bool
23856 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
23858 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
23859 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
23862 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
23863 of GNU attributes, or return NULL. */
23865 static tree
23866 cp_parser_attributes_opt (cp_parser *parser)
23868 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
23869 return cp_parser_gnu_attributes_opt (parser);
23870 return cp_parser_std_attribute_spec_seq (parser);
23873 #define CILK_SIMD_FN_CLAUSE_MASK \
23874 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
23875 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
23876 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
23877 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
23878 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
23880 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
23881 vector [(<clauses>)] */
23883 static void
23884 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
23886 bool first_p = parser->cilk_simd_fn_info == NULL;
23887 cp_token *token = v_token;
23888 if (first_p)
23890 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
23891 parser->cilk_simd_fn_info->error_seen = false;
23892 parser->cilk_simd_fn_info->fndecl_seen = false;
23893 parser->cilk_simd_fn_info->tokens = vNULL;
23894 parser->cilk_simd_fn_info->clauses = NULL_TREE;
23896 int paren_scope = 0;
23897 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23899 cp_lexer_consume_token (parser->lexer);
23900 v_token = cp_lexer_peek_token (parser->lexer);
23901 paren_scope++;
23903 while (paren_scope > 0)
23905 token = cp_lexer_peek_token (parser->lexer);
23906 if (token->type == CPP_OPEN_PAREN)
23907 paren_scope++;
23908 else if (token->type == CPP_CLOSE_PAREN)
23909 paren_scope--;
23910 /* Do not push the last ')' */
23911 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
23912 cp_lexer_consume_token (parser->lexer);
23915 token->type = CPP_PRAGMA_EOL;
23916 parser->lexer->next_token = token;
23917 cp_lexer_consume_token (parser->lexer);
23919 struct cp_token_cache *cp
23920 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
23921 parser->cilk_simd_fn_info->tokens.safe_push (cp);
23924 /* Parse an (optional) series of attributes.
23926 attributes:
23927 attributes attribute
23929 attribute:
23930 __attribute__ (( attribute-list [opt] ))
23932 The return value is as for cp_parser_gnu_attribute_list. */
23934 static tree
23935 cp_parser_gnu_attributes_opt (cp_parser* parser)
23937 tree attributes = NULL_TREE;
23939 while (true)
23941 cp_token *token;
23942 tree attribute_list;
23943 bool ok = true;
23945 /* Peek at the next token. */
23946 token = cp_lexer_peek_token (parser->lexer);
23947 /* If it's not `__attribute__', then we're done. */
23948 if (token->keyword != RID_ATTRIBUTE)
23949 break;
23951 /* Consume the `__attribute__' keyword. */
23952 cp_lexer_consume_token (parser->lexer);
23953 /* Look for the two `(' tokens. */
23954 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23955 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23957 /* Peek at the next token. */
23958 token = cp_lexer_peek_token (parser->lexer);
23959 if (token->type != CPP_CLOSE_PAREN)
23960 /* Parse the attribute-list. */
23961 attribute_list = cp_parser_gnu_attribute_list (parser);
23962 else
23963 /* If the next token is a `)', then there is no attribute
23964 list. */
23965 attribute_list = NULL;
23967 /* Look for the two `)' tokens. */
23968 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23969 ok = false;
23970 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23971 ok = false;
23972 if (!ok)
23973 cp_parser_skip_to_end_of_statement (parser);
23975 /* Add these new attributes to the list. */
23976 attributes = chainon (attributes, attribute_list);
23979 return attributes;
23982 /* Parse a GNU attribute-list.
23984 attribute-list:
23985 attribute
23986 attribute-list , attribute
23988 attribute:
23989 identifier
23990 identifier ( identifier )
23991 identifier ( identifier , expression-list )
23992 identifier ( expression-list )
23994 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
23995 to an attribute. The TREE_PURPOSE of each node is the identifier
23996 indicating which attribute is in use. The TREE_VALUE represents
23997 the arguments, if any. */
23999 static tree
24000 cp_parser_gnu_attribute_list (cp_parser* parser)
24002 tree attribute_list = NULL_TREE;
24003 bool save_translate_strings_p = parser->translate_strings_p;
24005 parser->translate_strings_p = false;
24006 while (true)
24008 cp_token *token;
24009 tree identifier;
24010 tree attribute;
24012 /* Look for the identifier. We also allow keywords here; for
24013 example `__attribute__ ((const))' is legal. */
24014 token = cp_lexer_peek_token (parser->lexer);
24015 if (token->type == CPP_NAME
24016 || token->type == CPP_KEYWORD)
24018 tree arguments = NULL_TREE;
24020 /* Consume the token, but save it since we need it for the
24021 SIMD enabled function parsing. */
24022 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
24024 /* Save away the identifier that indicates which attribute
24025 this is. */
24026 identifier = (token->type == CPP_KEYWORD)
24027 /* For keywords, use the canonical spelling, not the
24028 parsed identifier. */
24029 ? ridpointers[(int) token->keyword]
24030 : id_token->u.value;
24032 attribute = build_tree_list (identifier, NULL_TREE);
24034 /* Peek at the next token. */
24035 token = cp_lexer_peek_token (parser->lexer);
24036 /* If it's an `(', then parse the attribute arguments. */
24037 if (token->type == CPP_OPEN_PAREN)
24039 vec<tree, va_gc> *vec;
24040 int attr_flag = (attribute_takes_identifier_p (identifier)
24041 ? id_attr : normal_attr);
24042 if (is_cilkplus_vector_p (identifier))
24044 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24045 continue;
24047 else
24048 vec = cp_parser_parenthesized_expression_list
24049 (parser, attr_flag, /*cast_p=*/false,
24050 /*allow_expansion_p=*/false,
24051 /*non_constant_p=*/NULL);
24052 if (vec == NULL)
24053 arguments = error_mark_node;
24054 else
24056 arguments = build_tree_list_vec (vec);
24057 release_tree_vector (vec);
24059 /* Save the arguments away. */
24060 TREE_VALUE (attribute) = arguments;
24062 else if (is_cilkplus_vector_p (identifier))
24064 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24065 continue;
24068 if (arguments != error_mark_node)
24070 /* Add this attribute to the list. */
24071 TREE_CHAIN (attribute) = attribute_list;
24072 attribute_list = attribute;
24075 token = cp_lexer_peek_token (parser->lexer);
24077 /* Now, look for more attributes. If the next token isn't a
24078 `,', we're done. */
24079 if (token->type != CPP_COMMA)
24080 break;
24082 /* Consume the comma and keep going. */
24083 cp_lexer_consume_token (parser->lexer);
24085 parser->translate_strings_p = save_translate_strings_p;
24087 /* We built up the list in reverse order. */
24088 return nreverse (attribute_list);
24091 /* Parse a standard C++11 attribute.
24093 The returned representation is a TREE_LIST which TREE_PURPOSE is
24094 the scoped name of the attribute, and the TREE_VALUE is its
24095 arguments list.
24097 Note that the scoped name of the attribute is itself a TREE_LIST
24098 which TREE_PURPOSE is the namespace of the attribute, and
24099 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
24100 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
24101 and which TREE_PURPOSE is directly the attribute name.
24103 Clients of the attribute code should use get_attribute_namespace
24104 and get_attribute_name to get the actual namespace and name of
24105 attributes, regardless of their being GNU or C++11 attributes.
24107 attribute:
24108 attribute-token attribute-argument-clause [opt]
24110 attribute-token:
24111 identifier
24112 attribute-scoped-token
24114 attribute-scoped-token:
24115 attribute-namespace :: identifier
24117 attribute-namespace:
24118 identifier
24120 attribute-argument-clause:
24121 ( balanced-token-seq )
24123 balanced-token-seq:
24124 balanced-token [opt]
24125 balanced-token-seq balanced-token
24127 balanced-token:
24128 ( balanced-token-seq )
24129 [ balanced-token-seq ]
24130 { balanced-token-seq }. */
24132 static tree
24133 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
24135 tree attribute, attr_id = NULL_TREE, arguments;
24136 cp_token *token;
24138 /* First, parse name of the attribute, a.k.a attribute-token. */
24140 token = cp_lexer_peek_token (parser->lexer);
24141 if (token->type == CPP_NAME)
24142 attr_id = token->u.value;
24143 else if (token->type == CPP_KEYWORD)
24144 attr_id = ridpointers[(int) token->keyword];
24145 else if (token->flags & NAMED_OP)
24146 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
24148 if (attr_id == NULL_TREE)
24149 return NULL_TREE;
24151 cp_lexer_consume_token (parser->lexer);
24153 token = cp_lexer_peek_token (parser->lexer);
24154 if (token->type == CPP_SCOPE)
24156 /* We are seeing a scoped attribute token. */
24158 cp_lexer_consume_token (parser->lexer);
24159 if (attr_ns)
24160 error_at (token->location, "attribute using prefix used together "
24161 "with scoped attribute token");
24162 attr_ns = attr_id;
24164 token = cp_lexer_consume_token (parser->lexer);
24165 if (token->type == CPP_NAME)
24166 attr_id = token->u.value;
24167 else if (token->type == CPP_KEYWORD)
24168 attr_id = ridpointers[(int) token->keyword];
24169 else if (token->flags & NAMED_OP)
24170 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
24171 else
24173 error_at (token->location,
24174 "expected an identifier for the attribute name");
24175 return error_mark_node;
24177 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
24178 NULL_TREE);
24179 token = cp_lexer_peek_token (parser->lexer);
24181 else if (attr_ns)
24182 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
24183 NULL_TREE);
24184 else
24186 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
24187 NULL_TREE);
24188 /* C++11 noreturn attribute is equivalent to GNU's. */
24189 if (is_attribute_p ("noreturn", attr_id))
24190 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24191 /* C++14 deprecated attribute is equivalent to GNU's. */
24192 else if (is_attribute_p ("deprecated", attr_id))
24194 if (cxx_dialect == cxx11)
24195 pedwarn (token->location, OPT_Wpedantic,
24196 "%<deprecated%> is a C++14 feature;"
24197 " use %<gnu::deprecated%>");
24198 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24200 /* C++17 fallthrough attribute is equivalent to GNU's. */
24201 else if (is_attribute_p ("fallthrough", attr_id))
24203 if (cxx_dialect < cxx1z)
24204 pedwarn (token->location, OPT_Wpedantic,
24205 "%<fallthrough%> is a C++17 feature;"
24206 " use %<gnu::fallthrough%>");
24207 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24209 /* Transactional Memory TS optimize_for_synchronized attribute is
24210 equivalent to GNU transaction_callable. */
24211 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
24212 TREE_PURPOSE (attribute)
24213 = get_identifier ("transaction_callable");
24214 /* Transactional Memory attributes are GNU attributes. */
24215 else if (tm_attr_to_mask (attr_id))
24216 TREE_PURPOSE (attribute) = attr_id;
24219 /* Now parse the optional argument clause of the attribute. */
24221 if (token->type != CPP_OPEN_PAREN)
24222 return attribute;
24225 vec<tree, va_gc> *vec;
24226 int attr_flag = normal_attr;
24228 if (attr_ns == get_identifier ("gnu")
24229 && attribute_takes_identifier_p (attr_id))
24230 /* A GNU attribute that takes an identifier in parameter. */
24231 attr_flag = id_attr;
24233 vec = cp_parser_parenthesized_expression_list
24234 (parser, attr_flag, /*cast_p=*/false,
24235 /*allow_expansion_p=*/true,
24236 /*non_constant_p=*/NULL);
24237 if (vec == NULL)
24238 arguments = error_mark_node;
24239 else
24241 arguments = build_tree_list_vec (vec);
24242 release_tree_vector (vec);
24245 if (arguments == error_mark_node)
24246 attribute = error_mark_node;
24247 else
24248 TREE_VALUE (attribute) = arguments;
24251 return attribute;
24254 /* Check that the attribute ATTRIBUTE appears at most once in the
24255 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
24256 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
24257 isn't implemented yet in GCC. */
24259 static void
24260 cp_parser_check_std_attribute (tree attributes, tree attribute)
24262 if (attributes)
24264 tree name = get_attribute_name (attribute);
24265 if (is_attribute_p ("noreturn", name)
24266 && lookup_attribute ("noreturn", attributes))
24267 error ("attribute %<noreturn%> can appear at most once "
24268 "in an attribute-list");
24269 else if (is_attribute_p ("deprecated", name)
24270 && lookup_attribute ("deprecated", attributes))
24271 error ("attribute %<deprecated%> can appear at most once "
24272 "in an attribute-list");
24276 /* Parse a list of standard C++-11 attributes.
24278 attribute-list:
24279 attribute [opt]
24280 attribute-list , attribute[opt]
24281 attribute ...
24282 attribute-list , attribute ...
24285 static tree
24286 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
24288 tree attributes = NULL_TREE, attribute = NULL_TREE;
24289 cp_token *token = NULL;
24291 while (true)
24293 attribute = cp_parser_std_attribute (parser, attr_ns);
24294 if (attribute == error_mark_node)
24295 break;
24296 if (attribute != NULL_TREE)
24298 cp_parser_check_std_attribute (attributes, attribute);
24299 TREE_CHAIN (attribute) = attributes;
24300 attributes = attribute;
24302 token = cp_lexer_peek_token (parser->lexer);
24303 if (token->type == CPP_ELLIPSIS)
24305 cp_lexer_consume_token (parser->lexer);
24306 if (attribute == NULL_TREE)
24307 error_at (token->location,
24308 "expected attribute before %<...%>");
24309 else
24310 TREE_VALUE (attribute)
24311 = make_pack_expansion (TREE_VALUE (attribute));
24312 token = cp_lexer_peek_token (parser->lexer);
24314 if (token->type != CPP_COMMA)
24315 break;
24316 cp_lexer_consume_token (parser->lexer);
24318 attributes = nreverse (attributes);
24319 return attributes;
24322 /* Parse a standard C++-11 attribute specifier.
24324 attribute-specifier:
24325 [ [ attribute-using-prefix [opt] attribute-list ] ]
24326 alignment-specifier
24328 attribute-using-prefix:
24329 using attribute-namespace :
24331 alignment-specifier:
24332 alignas ( type-id ... [opt] )
24333 alignas ( alignment-expression ... [opt] ). */
24335 static tree
24336 cp_parser_std_attribute_spec (cp_parser *parser)
24338 tree attributes = NULL_TREE;
24339 cp_token *token = cp_lexer_peek_token (parser->lexer);
24341 if (token->type == CPP_OPEN_SQUARE
24342 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
24344 tree attr_ns = NULL_TREE;
24346 cp_lexer_consume_token (parser->lexer);
24347 cp_lexer_consume_token (parser->lexer);
24349 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
24351 token = cp_lexer_peek_nth_token (parser->lexer, 2);
24352 if (token->type == CPP_NAME)
24353 attr_ns = token->u.value;
24354 else if (token->type == CPP_KEYWORD)
24355 attr_ns = ridpointers[(int) token->keyword];
24356 else if (token->flags & NAMED_OP)
24357 attr_ns = get_identifier (cpp_type2name (token->type,
24358 token->flags));
24359 if (attr_ns
24360 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
24362 if (cxx_dialect < cxx1z
24363 && !in_system_header_at (input_location))
24364 pedwarn (input_location, 0,
24365 "attribute using prefix only available "
24366 "with -std=c++1z or -std=gnu++1z");
24368 cp_lexer_consume_token (parser->lexer);
24369 cp_lexer_consume_token (parser->lexer);
24370 cp_lexer_consume_token (parser->lexer);
24372 else
24373 attr_ns = NULL_TREE;
24376 attributes = cp_parser_std_attribute_list (parser, attr_ns);
24378 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
24379 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
24380 cp_parser_skip_to_end_of_statement (parser);
24381 else
24382 /* Warn about parsing c++11 attribute in non-c++1 mode, only
24383 when we are sure that we have actually parsed them. */
24384 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24386 else
24388 tree alignas_expr;
24390 /* Look for an alignment-specifier. */
24392 token = cp_lexer_peek_token (parser->lexer);
24394 if (token->type != CPP_KEYWORD
24395 || token->keyword != RID_ALIGNAS)
24396 return NULL_TREE;
24398 cp_lexer_consume_token (parser->lexer);
24399 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24401 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
24403 cp_parser_error (parser, "expected %<(%>");
24404 return error_mark_node;
24407 cp_parser_parse_tentatively (parser);
24408 alignas_expr = cp_parser_type_id (parser);
24410 if (!cp_parser_parse_definitely (parser))
24412 gcc_assert (alignas_expr == error_mark_node
24413 || alignas_expr == NULL_TREE);
24415 alignas_expr =
24416 cp_parser_assignment_expression (parser);
24417 if (alignas_expr == error_mark_node)
24418 cp_parser_skip_to_end_of_statement (parser);
24419 if (alignas_expr == NULL_TREE
24420 || alignas_expr == error_mark_node)
24421 return alignas_expr;
24424 alignas_expr = cxx_alignas_expr (alignas_expr);
24425 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
24427 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24429 cp_lexer_consume_token (parser->lexer);
24430 alignas_expr = make_pack_expansion (alignas_expr);
24433 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
24435 cp_parser_error (parser, "expected %<)%>");
24436 return error_mark_node;
24439 /* Build the C++-11 representation of an 'aligned'
24440 attribute. */
24441 attributes =
24442 build_tree_list (build_tree_list (get_identifier ("gnu"),
24443 get_identifier ("aligned")),
24444 alignas_expr);
24447 return attributes;
24450 /* Parse a standard C++-11 attribute-specifier-seq.
24452 attribute-specifier-seq:
24453 attribute-specifier-seq [opt] attribute-specifier
24456 static tree
24457 cp_parser_std_attribute_spec_seq (cp_parser *parser)
24459 tree attr_specs = NULL_TREE;
24460 tree attr_last = NULL_TREE;
24462 while (true)
24464 tree attr_spec = cp_parser_std_attribute_spec (parser);
24465 if (attr_spec == NULL_TREE)
24466 break;
24467 if (attr_spec == error_mark_node)
24468 return error_mark_node;
24470 if (attr_last)
24471 TREE_CHAIN (attr_last) = attr_spec;
24472 else
24473 attr_specs = attr_last = attr_spec;
24474 attr_last = tree_last (attr_last);
24477 return attr_specs;
24480 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
24481 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
24482 current value of the PEDANTIC flag, regardless of whether or not
24483 the `__extension__' keyword is present. The caller is responsible
24484 for restoring the value of the PEDANTIC flag. */
24486 static bool
24487 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
24489 /* Save the old value of the PEDANTIC flag. */
24490 *saved_pedantic = pedantic;
24492 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
24494 /* Consume the `__extension__' token. */
24495 cp_lexer_consume_token (parser->lexer);
24496 /* We're not being pedantic while the `__extension__' keyword is
24497 in effect. */
24498 pedantic = 0;
24500 return true;
24503 return false;
24506 /* Parse a label declaration.
24508 label-declaration:
24509 __label__ label-declarator-seq ;
24511 label-declarator-seq:
24512 identifier , label-declarator-seq
24513 identifier */
24515 static void
24516 cp_parser_label_declaration (cp_parser* parser)
24518 /* Look for the `__label__' keyword. */
24519 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
24521 while (true)
24523 tree identifier;
24525 /* Look for an identifier. */
24526 identifier = cp_parser_identifier (parser);
24527 /* If we failed, stop. */
24528 if (identifier == error_mark_node)
24529 break;
24530 /* Declare it as a label. */
24531 finish_label_decl (identifier);
24532 /* If the next token is a `;', stop. */
24533 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24534 break;
24535 /* Look for the `,' separating the label declarations. */
24536 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
24539 /* Look for the final `;'. */
24540 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24543 // -------------------------------------------------------------------------- //
24544 // Requires Clause
24546 // Parse a requires clause.
24548 // requires-clause:
24549 // 'requires' logical-or-expression
24551 // The required logical-or-expression must be a constant expression. Note
24552 // that we don't check that the expression is constepxr here. We defer until
24553 // we analyze constraints and then, we only check atomic constraints.
24554 static tree
24555 cp_parser_requires_clause (cp_parser *parser)
24557 // Parse the requires clause so that it is not automatically folded.
24558 ++processing_template_decl;
24559 tree expr = cp_parser_binary_expression (parser, false, false,
24560 PREC_NOT_OPERATOR, NULL);
24561 if (check_for_bare_parameter_packs (expr))
24562 expr = error_mark_node;
24563 --processing_template_decl;
24564 return expr;
24567 // Optionally parse a requires clause:
24568 static tree
24569 cp_parser_requires_clause_opt (cp_parser *parser)
24571 cp_token *tok = cp_lexer_peek_token (parser->lexer);
24572 if (tok->keyword != RID_REQUIRES)
24574 if (!flag_concepts && tok->type == CPP_NAME
24575 && tok->u.value == ridpointers[RID_REQUIRES])
24577 error_at (cp_lexer_peek_token (parser->lexer)->location,
24578 "%<requires%> only available with -fconcepts");
24579 /* Parse and discard the requires-clause. */
24580 cp_lexer_consume_token (parser->lexer);
24581 cp_parser_requires_clause (parser);
24583 return NULL_TREE;
24585 cp_lexer_consume_token (parser->lexer);
24586 return cp_parser_requires_clause (parser);
24590 /*---------------------------------------------------------------------------
24591 Requires expressions
24592 ---------------------------------------------------------------------------*/
24594 /* Parse a requires expression
24596 requirement-expression:
24597 'requires' requirement-parameter-list [opt] requirement-body */
24598 static tree
24599 cp_parser_requires_expression (cp_parser *parser)
24601 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
24602 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
24604 /* A requires-expression shall appear only within a concept
24605 definition or a requires-clause.
24607 TODO: Implement this diagnostic correctly. */
24608 if (!processing_template_decl)
24610 error_at (loc, "a requires expression cannot appear outside a template");
24611 cp_parser_skip_to_end_of_statement (parser);
24612 return error_mark_node;
24615 tree parms, reqs;
24617 /* Local parameters are delared as variables within the scope
24618 of the expression. They are not visible past the end of
24619 the expression. Expressions within the requires-expression
24620 are unevaluated. */
24621 struct scope_sentinel
24623 scope_sentinel ()
24625 ++cp_unevaluated_operand;
24626 begin_scope (sk_block, NULL_TREE);
24629 ~scope_sentinel ()
24631 pop_bindings_and_leave_scope ();
24632 --cp_unevaluated_operand;
24634 } s;
24636 /* Parse the optional parameter list. */
24637 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24639 parms = cp_parser_requirement_parameter_list (parser);
24640 if (parms == error_mark_node)
24641 return error_mark_node;
24643 else
24644 parms = NULL_TREE;
24646 /* Parse the requirement body. */
24647 reqs = cp_parser_requirement_body (parser);
24648 if (reqs == error_mark_node)
24649 return error_mark_node;
24652 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
24653 the parm chain. */
24654 grokparms (parms, &parms);
24655 return finish_requires_expr (parms, reqs);
24658 /* Parse a parameterized requirement.
24660 requirement-parameter-list:
24661 '(' parameter-declaration-clause ')' */
24662 static tree
24663 cp_parser_requirement_parameter_list (cp_parser *parser)
24665 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24666 return error_mark_node;
24668 tree parms = cp_parser_parameter_declaration_clause (parser);
24670 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24671 return error_mark_node;
24673 return parms;
24676 /* Parse the body of a requirement.
24678 requirement-body:
24679 '{' requirement-list '}' */
24680 static tree
24681 cp_parser_requirement_body (cp_parser *parser)
24683 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24684 return error_mark_node;
24686 tree reqs = cp_parser_requirement_list (parser);
24688 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24689 return error_mark_node;
24691 return reqs;
24694 /* Parse a list of requirements.
24696 requirement-list:
24697 requirement
24698 requirement-list ';' requirement[opt] */
24699 static tree
24700 cp_parser_requirement_list (cp_parser *parser)
24702 tree result = NULL_TREE;
24703 while (true)
24705 tree req = cp_parser_requirement (parser);
24706 if (req == error_mark_node)
24707 return error_mark_node;
24709 result = tree_cons (NULL_TREE, req, result);
24711 /* If we see a semi-colon, consume it. */
24712 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24713 cp_lexer_consume_token (parser->lexer);
24715 /* Stop processing at the end of the list. */
24716 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24717 break;
24720 /* Reverse the order of requirements so they are analyzed in
24721 declaration order. */
24722 return nreverse (result);
24725 /* Parse a syntactic requirement or type requirement.
24727 requirement:
24728 simple-requirement
24729 compound-requirement
24730 type-requirement
24731 nested-requirement */
24732 static tree
24733 cp_parser_requirement (cp_parser *parser)
24735 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24736 return cp_parser_compound_requirement (parser);
24737 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24738 return cp_parser_type_requirement (parser);
24739 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
24740 return cp_parser_nested_requirement (parser);
24741 else
24742 return cp_parser_simple_requirement (parser);
24745 /* Parse a simple requirement.
24747 simple-requirement:
24748 expression ';' */
24749 static tree
24750 cp_parser_simple_requirement (cp_parser *parser)
24752 tree expr = cp_parser_expression (parser, NULL, false, false);
24753 if (!expr || expr == error_mark_node)
24754 return error_mark_node;
24756 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
24757 return error_mark_node;
24759 return finish_simple_requirement (expr);
24762 /* Parse a type requirement
24764 type-requirement
24765 nested-name-specifier [opt] required-type-name ';'
24767 required-type-name:
24768 type-name
24769 'template' [opt] simple-template-id */
24770 static tree
24771 cp_parser_type_requirement (cp_parser *parser)
24773 cp_lexer_consume_token (parser->lexer);
24775 // Save the scope before parsing name specifiers.
24776 tree saved_scope = parser->scope;
24777 tree saved_object_scope = parser->object_scope;
24778 tree saved_qualifying_scope = parser->qualifying_scope;
24779 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
24780 cp_parser_nested_name_specifier_opt (parser,
24781 /*typename_keyword_p=*/true,
24782 /*check_dependency_p=*/false,
24783 /*type_p=*/true,
24784 /*is_declaration=*/false);
24786 tree type;
24787 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24789 cp_lexer_consume_token (parser->lexer);
24790 type = cp_parser_template_id (parser,
24791 /*template_keyword_p=*/true,
24792 /*check_dependency=*/false,
24793 /*tag_type=*/none_type,
24794 /*is_declaration=*/false);
24795 type = make_typename_type (parser->scope, type, typename_type,
24796 /*complain=*/tf_error);
24798 else
24799 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
24801 if (TREE_CODE (type) == TYPE_DECL)
24802 type = TREE_TYPE (type);
24804 parser->scope = saved_scope;
24805 parser->object_scope = saved_object_scope;
24806 parser->qualifying_scope = saved_qualifying_scope;
24808 if (type == error_mark_node)
24809 cp_parser_skip_to_end_of_statement (parser);
24811 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
24812 return error_mark_node;
24813 if (type == error_mark_node)
24814 return error_mark_node;
24816 return finish_type_requirement (type);
24819 /* Parse a compound requirement
24821 compound-requirement:
24822 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
24823 static tree
24824 cp_parser_compound_requirement (cp_parser *parser)
24826 /* Parse an expression enclosed in '{ }'s. */
24827 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24828 return error_mark_node;
24830 tree expr = cp_parser_expression (parser, NULL, false, false);
24831 if (!expr || expr == error_mark_node)
24832 return error_mark_node;
24834 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24835 return error_mark_node;
24837 /* Parse the optional noexcept. */
24838 bool noexcept_p = false;
24839 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
24841 cp_lexer_consume_token (parser->lexer);
24842 noexcept_p = true;
24845 /* Parse the optional trailing return type. */
24846 tree type = NULL_TREE;
24847 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
24849 cp_lexer_consume_token (parser->lexer);
24850 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
24851 parser->in_result_type_constraint_p = true;
24852 type = cp_parser_trailing_type_id (parser);
24853 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
24854 if (type == error_mark_node)
24855 return error_mark_node;
24858 return finish_compound_requirement (expr, type, noexcept_p);
24861 /* Parse a nested requirement. This is the same as a requires clause.
24863 nested-requirement:
24864 requires-clause */
24865 static tree
24866 cp_parser_nested_requirement (cp_parser *parser)
24868 cp_lexer_consume_token (parser->lexer);
24869 tree req = cp_parser_requires_clause (parser);
24870 if (req == error_mark_node)
24871 return error_mark_node;
24872 return finish_nested_requirement (req);
24875 /* Support Functions */
24877 /* Return the appropriate prefer_type argument for lookup_name_real based on
24878 tag_type and template_mem_access. */
24880 static inline int
24881 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
24883 /* DR 141: When looking in the current enclosing context for a template-name
24884 after -> or ., only consider class templates. */
24885 if (template_mem_access)
24886 return 2;
24887 switch (tag_type)
24889 case none_type: return 0; // No preference.
24890 case scope_type: return 1; // Type or namespace.
24891 default: return 2; // Type only.
24895 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
24896 NAME should have one of the representations used for an
24897 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
24898 is returned. If PARSER->SCOPE is a dependent type, then a
24899 SCOPE_REF is returned.
24901 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
24902 returned; the name was already resolved when the TEMPLATE_ID_EXPR
24903 was formed. Abstractly, such entities should not be passed to this
24904 function, because they do not need to be looked up, but it is
24905 simpler to check for this special case here, rather than at the
24906 call-sites.
24908 In cases not explicitly covered above, this function returns a
24909 DECL, OVERLOAD, or baselink representing the result of the lookup.
24910 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
24911 is returned.
24913 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
24914 (e.g., "struct") that was used. In that case bindings that do not
24915 refer to types are ignored.
24917 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
24918 ignored.
24920 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
24921 are ignored.
24923 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
24924 types.
24926 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
24927 TREE_LIST of candidates if name-lookup results in an ambiguity, and
24928 NULL_TREE otherwise. */
24930 static cp_expr
24931 cp_parser_lookup_name (cp_parser *parser, tree name,
24932 enum tag_types tag_type,
24933 bool is_template,
24934 bool is_namespace,
24935 bool check_dependency,
24936 tree *ambiguous_decls,
24937 location_t name_location)
24939 tree decl;
24940 tree object_type = parser->context->object_type;
24942 /* Assume that the lookup will be unambiguous. */
24943 if (ambiguous_decls)
24944 *ambiguous_decls = NULL_TREE;
24946 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
24947 no longer valid. Note that if we are parsing tentatively, and
24948 the parse fails, OBJECT_TYPE will be automatically restored. */
24949 parser->context->object_type = NULL_TREE;
24951 if (name == error_mark_node)
24952 return error_mark_node;
24954 /* A template-id has already been resolved; there is no lookup to
24955 do. */
24956 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
24957 return name;
24958 if (BASELINK_P (name))
24960 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
24961 == TEMPLATE_ID_EXPR);
24962 return name;
24965 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
24966 it should already have been checked to make sure that the name
24967 used matches the type being destroyed. */
24968 if (TREE_CODE (name) == BIT_NOT_EXPR)
24970 tree type;
24972 /* Figure out to which type this destructor applies. */
24973 if (parser->scope)
24974 type = parser->scope;
24975 else if (object_type)
24976 type = object_type;
24977 else
24978 type = current_class_type;
24979 /* If that's not a class type, there is no destructor. */
24980 if (!type || !CLASS_TYPE_P (type))
24981 return error_mark_node;
24982 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
24983 lazily_declare_fn (sfk_destructor, type);
24984 if (!CLASSTYPE_DESTRUCTORS (type))
24985 return error_mark_node;
24986 /* If it was a class type, return the destructor. */
24987 return CLASSTYPE_DESTRUCTORS (type);
24990 /* By this point, the NAME should be an ordinary identifier. If
24991 the id-expression was a qualified name, the qualifying scope is
24992 stored in PARSER->SCOPE at this point. */
24993 gcc_assert (identifier_p (name));
24995 /* Perform the lookup. */
24996 if (parser->scope)
24998 bool dependent_p;
25000 if (parser->scope == error_mark_node)
25001 return error_mark_node;
25003 /* If the SCOPE is dependent, the lookup must be deferred until
25004 the template is instantiated -- unless we are explicitly
25005 looking up names in uninstantiated templates. Even then, we
25006 cannot look up the name if the scope is not a class type; it
25007 might, for example, be a template type parameter. */
25008 dependent_p = (TYPE_P (parser->scope)
25009 && dependent_scope_p (parser->scope));
25010 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
25011 && dependent_p)
25012 /* Defer lookup. */
25013 decl = error_mark_node;
25014 else
25016 tree pushed_scope = NULL_TREE;
25018 /* If PARSER->SCOPE is a dependent type, then it must be a
25019 class type, and we must not be checking dependencies;
25020 otherwise, we would have processed this lookup above. So
25021 that PARSER->SCOPE is not considered a dependent base by
25022 lookup_member, we must enter the scope here. */
25023 if (dependent_p)
25024 pushed_scope = push_scope (parser->scope);
25026 /* If the PARSER->SCOPE is a template specialization, it
25027 may be instantiated during name lookup. In that case,
25028 errors may be issued. Even if we rollback the current
25029 tentative parse, those errors are valid. */
25030 decl = lookup_qualified_name (parser->scope, name,
25031 prefer_type_arg (tag_type),
25032 /*complain=*/true);
25034 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
25035 lookup result and the nested-name-specifier nominates a class C:
25036 * if the name specified after the nested-name-specifier, when
25037 looked up in C, is the injected-class-name of C (Clause 9), or
25038 * if the name specified after the nested-name-specifier is the
25039 same as the identifier or the simple-template-id's template-
25040 name in the last component of the nested-name-specifier,
25041 the name is instead considered to name the constructor of
25042 class C. [ Note: for example, the constructor is not an
25043 acceptable lookup result in an elaborated-type-specifier so
25044 the constructor would not be used in place of the
25045 injected-class-name. --end note ] Such a constructor name
25046 shall be used only in the declarator-id of a declaration that
25047 names a constructor or in a using-declaration. */
25048 if (tag_type == none_type
25049 && DECL_SELF_REFERENCE_P (decl)
25050 && same_type_p (DECL_CONTEXT (decl), parser->scope))
25051 decl = lookup_qualified_name (parser->scope, ctor_identifier,
25052 prefer_type_arg (tag_type),
25053 /*complain=*/true);
25055 /* If we have a single function from a using decl, pull it out. */
25056 if (TREE_CODE (decl) == OVERLOAD
25057 && !really_overloaded_fn (decl))
25058 decl = OVL_FUNCTION (decl);
25060 if (pushed_scope)
25061 pop_scope (pushed_scope);
25064 /* If the scope is a dependent type and either we deferred lookup or
25065 we did lookup but didn't find the name, rememeber the name. */
25066 if (decl == error_mark_node && TYPE_P (parser->scope)
25067 && dependent_type_p (parser->scope))
25069 if (tag_type)
25071 tree type;
25073 /* The resolution to Core Issue 180 says that `struct
25074 A::B' should be considered a type-name, even if `A'
25075 is dependent. */
25076 type = make_typename_type (parser->scope, name, tag_type,
25077 /*complain=*/tf_error);
25078 if (type != error_mark_node)
25079 decl = TYPE_NAME (type);
25081 else if (is_template
25082 && (cp_parser_next_token_ends_template_argument_p (parser)
25083 || cp_lexer_next_token_is (parser->lexer,
25084 CPP_CLOSE_PAREN)))
25085 decl = make_unbound_class_template (parser->scope,
25086 name, NULL_TREE,
25087 /*complain=*/tf_error);
25088 else
25089 decl = build_qualified_name (/*type=*/NULL_TREE,
25090 parser->scope, name,
25091 is_template);
25093 parser->qualifying_scope = parser->scope;
25094 parser->object_scope = NULL_TREE;
25096 else if (object_type)
25098 /* Look up the name in the scope of the OBJECT_TYPE, unless the
25099 OBJECT_TYPE is not a class. */
25100 if (CLASS_TYPE_P (object_type))
25101 /* If the OBJECT_TYPE is a template specialization, it may
25102 be instantiated during name lookup. In that case, errors
25103 may be issued. Even if we rollback the current tentative
25104 parse, those errors are valid. */
25105 decl = lookup_member (object_type,
25106 name,
25107 /*protect=*/0,
25108 prefer_type_arg (tag_type),
25109 tf_warning_or_error);
25110 else
25111 decl = NULL_TREE;
25113 if (!decl)
25114 /* Look it up in the enclosing context. DR 141: When looking for a
25115 template-name after -> or ., only consider class templates. */
25116 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
25117 /*nonclass=*/0,
25118 /*block_p=*/true, is_namespace, 0);
25119 if (object_type == unknown_type_node)
25120 /* The object is type-dependent, so we can't look anything up; we used
25121 this to get the DR 141 behavior. */
25122 object_type = NULL_TREE;
25123 parser->object_scope = object_type;
25124 parser->qualifying_scope = NULL_TREE;
25126 else
25128 decl = lookup_name_real (name, prefer_type_arg (tag_type),
25129 /*nonclass=*/0,
25130 /*block_p=*/true, is_namespace, 0);
25131 parser->qualifying_scope = NULL_TREE;
25132 parser->object_scope = NULL_TREE;
25135 /* If the lookup failed, let our caller know. */
25136 if (!decl || decl == error_mark_node)
25137 return error_mark_node;
25139 /* Pull out the template from an injected-class-name (or multiple). */
25140 if (is_template)
25141 decl = maybe_get_template_decl_from_type_decl (decl);
25143 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
25144 if (TREE_CODE (decl) == TREE_LIST)
25146 if (ambiguous_decls)
25147 *ambiguous_decls = decl;
25148 /* The error message we have to print is too complicated for
25149 cp_parser_error, so we incorporate its actions directly. */
25150 if (!cp_parser_simulate_error (parser))
25152 error_at (name_location, "reference to %qD is ambiguous",
25153 name);
25154 print_candidates (decl);
25156 return error_mark_node;
25159 gcc_assert (DECL_P (decl)
25160 || TREE_CODE (decl) == OVERLOAD
25161 || TREE_CODE (decl) == SCOPE_REF
25162 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
25163 || BASELINK_P (decl));
25165 /* If we have resolved the name of a member declaration, check to
25166 see if the declaration is accessible. When the name resolves to
25167 set of overloaded functions, accessibility is checked when
25168 overload resolution is done.
25170 During an explicit instantiation, access is not checked at all,
25171 as per [temp.explicit]. */
25172 if (DECL_P (decl))
25173 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
25175 maybe_record_typedef_use (decl);
25177 return cp_expr (decl, name_location);
25180 /* Like cp_parser_lookup_name, but for use in the typical case where
25181 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
25182 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
25184 static tree
25185 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
25187 return cp_parser_lookup_name (parser, name,
25188 none_type,
25189 /*is_template=*/false,
25190 /*is_namespace=*/false,
25191 /*check_dependency=*/true,
25192 /*ambiguous_decls=*/NULL,
25193 location);
25196 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
25197 the current context, return the TYPE_DECL. If TAG_NAME_P is
25198 true, the DECL indicates the class being defined in a class-head,
25199 or declared in an elaborated-type-specifier.
25201 Otherwise, return DECL. */
25203 static tree
25204 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
25206 /* If the TEMPLATE_DECL is being declared as part of a class-head,
25207 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
25209 struct A {
25210 template <typename T> struct B;
25213 template <typename T> struct A::B {};
25215 Similarly, in an elaborated-type-specifier:
25217 namespace N { struct X{}; }
25219 struct A {
25220 template <typename T> friend struct N::X;
25223 However, if the DECL refers to a class type, and we are in
25224 the scope of the class, then the name lookup automatically
25225 finds the TYPE_DECL created by build_self_reference rather
25226 than a TEMPLATE_DECL. For example, in:
25228 template <class T> struct S {
25229 S s;
25232 there is no need to handle such case. */
25234 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
25235 return DECL_TEMPLATE_RESULT (decl);
25237 return decl;
25240 /* If too many, or too few, template-parameter lists apply to the
25241 declarator, issue an error message. Returns TRUE if all went well,
25242 and FALSE otherwise. */
25244 static bool
25245 cp_parser_check_declarator_template_parameters (cp_parser* parser,
25246 cp_declarator *declarator,
25247 location_t declarator_location)
25249 switch (declarator->kind)
25251 case cdk_id:
25253 unsigned num_templates = 0;
25254 tree scope = declarator->u.id.qualifying_scope;
25256 if (scope)
25257 num_templates = num_template_headers_for_class (scope);
25258 else if (TREE_CODE (declarator->u.id.unqualified_name)
25259 == TEMPLATE_ID_EXPR)
25260 /* If the DECLARATOR has the form `X<y>' then it uses one
25261 additional level of template parameters. */
25262 ++num_templates;
25264 return cp_parser_check_template_parameters
25265 (parser, num_templates, declarator_location, declarator);
25268 case cdk_function:
25269 case cdk_array:
25270 case cdk_pointer:
25271 case cdk_reference:
25272 case cdk_ptrmem:
25273 return (cp_parser_check_declarator_template_parameters
25274 (parser, declarator->declarator, declarator_location));
25276 case cdk_error:
25277 return true;
25279 default:
25280 gcc_unreachable ();
25282 return false;
25285 /* NUM_TEMPLATES were used in the current declaration. If that is
25286 invalid, return FALSE and issue an error messages. Otherwise,
25287 return TRUE. If DECLARATOR is non-NULL, then we are checking a
25288 declarator and we can print more accurate diagnostics. */
25290 static bool
25291 cp_parser_check_template_parameters (cp_parser* parser,
25292 unsigned num_templates,
25293 location_t location,
25294 cp_declarator *declarator)
25296 /* If there are the same number of template classes and parameter
25297 lists, that's OK. */
25298 if (parser->num_template_parameter_lists == num_templates)
25299 return true;
25300 /* If there are more, but only one more, then we are referring to a
25301 member template. That's OK too. */
25302 if (parser->num_template_parameter_lists == num_templates + 1)
25303 return true;
25304 /* If there are more template classes than parameter lists, we have
25305 something like:
25307 template <class T> void S<T>::R<T>::f (); */
25308 if (parser->num_template_parameter_lists < num_templates)
25310 if (declarator && !current_function_decl)
25311 error_at (location, "specializing member %<%T::%E%> "
25312 "requires %<template<>%> syntax",
25313 declarator->u.id.qualifying_scope,
25314 declarator->u.id.unqualified_name);
25315 else if (declarator)
25316 error_at (location, "invalid declaration of %<%T::%E%>",
25317 declarator->u.id.qualifying_scope,
25318 declarator->u.id.unqualified_name);
25319 else
25320 error_at (location, "too few template-parameter-lists");
25321 return false;
25323 /* Otherwise, there are too many template parameter lists. We have
25324 something like:
25326 template <class T> template <class U> void S::f(); */
25327 error_at (location, "too many template-parameter-lists");
25328 return false;
25331 /* Parse an optional `::' token indicating that the following name is
25332 from the global namespace. If so, PARSER->SCOPE is set to the
25333 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
25334 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
25335 Returns the new value of PARSER->SCOPE, if the `::' token is
25336 present, and NULL_TREE otherwise. */
25338 static tree
25339 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
25341 cp_token *token;
25343 /* Peek at the next token. */
25344 token = cp_lexer_peek_token (parser->lexer);
25345 /* If we're looking at a `::' token then we're starting from the
25346 global namespace, not our current location. */
25347 if (token->type == CPP_SCOPE)
25349 /* Consume the `::' token. */
25350 cp_lexer_consume_token (parser->lexer);
25351 /* Set the SCOPE so that we know where to start the lookup. */
25352 parser->scope = global_namespace;
25353 parser->qualifying_scope = global_namespace;
25354 parser->object_scope = NULL_TREE;
25356 return parser->scope;
25358 else if (!current_scope_valid_p)
25360 parser->scope = NULL_TREE;
25361 parser->qualifying_scope = NULL_TREE;
25362 parser->object_scope = NULL_TREE;
25365 return NULL_TREE;
25368 /* Returns TRUE if the upcoming token sequence is the start of a
25369 constructor declarator. If FRIEND_P is true, the declarator is
25370 preceded by the `friend' specifier. */
25372 static bool
25373 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
25375 bool constructor_p;
25376 bool outside_class_specifier_p;
25377 tree nested_name_specifier;
25378 cp_token *next_token;
25380 /* The common case is that this is not a constructor declarator, so
25381 try to avoid doing lots of work if at all possible. It's not
25382 valid declare a constructor at function scope. */
25383 if (parser->in_function_body)
25384 return false;
25385 /* And only certain tokens can begin a constructor declarator. */
25386 next_token = cp_lexer_peek_token (parser->lexer);
25387 if (next_token->type != CPP_NAME
25388 && next_token->type != CPP_SCOPE
25389 && next_token->type != CPP_NESTED_NAME_SPECIFIER
25390 && next_token->type != CPP_TEMPLATE_ID)
25391 return false;
25393 /* Parse tentatively; we are going to roll back all of the tokens
25394 consumed here. */
25395 cp_parser_parse_tentatively (parser);
25396 /* Assume that we are looking at a constructor declarator. */
25397 constructor_p = true;
25399 /* Look for the optional `::' operator. */
25400 cp_parser_global_scope_opt (parser,
25401 /*current_scope_valid_p=*/false);
25402 /* Look for the nested-name-specifier. */
25403 nested_name_specifier
25404 = (cp_parser_nested_name_specifier_opt (parser,
25405 /*typename_keyword_p=*/false,
25406 /*check_dependency_p=*/false,
25407 /*type_p=*/false,
25408 /*is_declaration=*/false));
25410 outside_class_specifier_p = (!at_class_scope_p ()
25411 || !TYPE_BEING_DEFINED (current_class_type)
25412 || friend_p);
25414 /* Outside of a class-specifier, there must be a
25415 nested-name-specifier. */
25416 if (!nested_name_specifier && outside_class_specifier_p)
25417 constructor_p = false;
25418 else if (nested_name_specifier == error_mark_node)
25419 constructor_p = false;
25421 /* If we have a class scope, this is easy; DR 147 says that S::S always
25422 names the constructor, and no other qualified name could. */
25423 if (constructor_p && nested_name_specifier
25424 && CLASS_TYPE_P (nested_name_specifier))
25426 tree id = cp_parser_unqualified_id (parser,
25427 /*template_keyword_p=*/false,
25428 /*check_dependency_p=*/false,
25429 /*declarator_p=*/true,
25430 /*optional_p=*/false);
25431 if (is_overloaded_fn (id))
25432 id = DECL_NAME (get_first_fn (id));
25433 if (!constructor_name_p (id, nested_name_specifier))
25434 constructor_p = false;
25436 /* If we still think that this might be a constructor-declarator,
25437 look for a class-name. */
25438 else if (constructor_p)
25440 /* If we have:
25442 template <typename T> struct S {
25443 S();
25446 we must recognize that the nested `S' names a class. */
25447 tree type_decl;
25448 type_decl = cp_parser_class_name (parser,
25449 /*typename_keyword_p=*/false,
25450 /*template_keyword_p=*/false,
25451 none_type,
25452 /*check_dependency_p=*/false,
25453 /*class_head_p=*/false,
25454 /*is_declaration=*/false);
25455 /* If there was no class-name, then this is not a constructor.
25456 Otherwise, if we are in a class-specifier and we aren't
25457 handling a friend declaration, check that its type matches
25458 current_class_type (c++/38313). Note: error_mark_node
25459 is left alone for error recovery purposes. */
25460 constructor_p = (!cp_parser_error_occurred (parser)
25461 && (outside_class_specifier_p
25462 || type_decl == error_mark_node
25463 || same_type_p (current_class_type,
25464 TREE_TYPE (type_decl))));
25466 /* If we're still considering a constructor, we have to see a `(',
25467 to begin the parameter-declaration-clause, followed by either a
25468 `)', an `...', or a decl-specifier. We need to check for a
25469 type-specifier to avoid being fooled into thinking that:
25471 S (f) (int);
25473 is a constructor. (It is actually a function named `f' that
25474 takes one parameter (of type `int') and returns a value of type
25475 `S'. */
25476 if (constructor_p
25477 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25478 constructor_p = false;
25480 if (constructor_p
25481 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
25482 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
25483 /* A parameter declaration begins with a decl-specifier,
25484 which is either the "attribute" keyword, a storage class
25485 specifier, or (usually) a type-specifier. */
25486 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
25488 tree type;
25489 tree pushed_scope = NULL_TREE;
25490 unsigned saved_num_template_parameter_lists;
25492 /* Names appearing in the type-specifier should be looked up
25493 in the scope of the class. */
25494 if (current_class_type)
25495 type = NULL_TREE;
25496 else
25498 type = TREE_TYPE (type_decl);
25499 if (TREE_CODE (type) == TYPENAME_TYPE)
25501 type = resolve_typename_type (type,
25502 /*only_current_p=*/false);
25503 if (TREE_CODE (type) == TYPENAME_TYPE)
25505 cp_parser_abort_tentative_parse (parser);
25506 return false;
25509 pushed_scope = push_scope (type);
25512 /* Inside the constructor parameter list, surrounding
25513 template-parameter-lists do not apply. */
25514 saved_num_template_parameter_lists
25515 = parser->num_template_parameter_lists;
25516 parser->num_template_parameter_lists = 0;
25518 /* Look for the type-specifier. */
25519 cp_parser_type_specifier (parser,
25520 CP_PARSER_FLAGS_NONE,
25521 /*decl_specs=*/NULL,
25522 /*is_declarator=*/true,
25523 /*declares_class_or_enum=*/NULL,
25524 /*is_cv_qualifier=*/NULL);
25526 parser->num_template_parameter_lists
25527 = saved_num_template_parameter_lists;
25529 /* Leave the scope of the class. */
25530 if (pushed_scope)
25531 pop_scope (pushed_scope);
25533 constructor_p = !cp_parser_error_occurred (parser);
25537 /* We did not really want to consume any tokens. */
25538 cp_parser_abort_tentative_parse (parser);
25540 return constructor_p;
25543 /* Parse the definition of the function given by the DECL_SPECIFIERS,
25544 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
25545 they must be performed once we are in the scope of the function.
25547 Returns the function defined. */
25549 static tree
25550 cp_parser_function_definition_from_specifiers_and_declarator
25551 (cp_parser* parser,
25552 cp_decl_specifier_seq *decl_specifiers,
25553 tree attributes,
25554 const cp_declarator *declarator)
25556 tree fn;
25557 bool success_p;
25559 /* Begin the function-definition. */
25560 success_p = start_function (decl_specifiers, declarator, attributes);
25562 /* The things we're about to see are not directly qualified by any
25563 template headers we've seen thus far. */
25564 reset_specialization ();
25566 /* If there were names looked up in the decl-specifier-seq that we
25567 did not check, check them now. We must wait until we are in the
25568 scope of the function to perform the checks, since the function
25569 might be a friend. */
25570 perform_deferred_access_checks (tf_warning_or_error);
25572 if (success_p)
25574 cp_finalize_omp_declare_simd (parser, current_function_decl);
25575 parser->omp_declare_simd = NULL;
25576 cp_finalize_oacc_routine (parser, current_function_decl, true);
25577 parser->oacc_routine = NULL;
25580 if (!success_p)
25582 /* Skip the entire function. */
25583 cp_parser_skip_to_end_of_block_or_statement (parser);
25584 fn = error_mark_node;
25586 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
25588 /* Seen already, skip it. An error message has already been output. */
25589 cp_parser_skip_to_end_of_block_or_statement (parser);
25590 fn = current_function_decl;
25591 current_function_decl = NULL_TREE;
25592 /* If this is a function from a class, pop the nested class. */
25593 if (current_class_name)
25594 pop_nested_class ();
25596 else
25598 timevar_id_t tv;
25599 if (DECL_DECLARED_INLINE_P (current_function_decl))
25600 tv = TV_PARSE_INLINE;
25601 else
25602 tv = TV_PARSE_FUNC;
25603 timevar_push (tv);
25604 fn = cp_parser_function_definition_after_declarator (parser,
25605 /*inline_p=*/false);
25606 timevar_pop (tv);
25609 return fn;
25612 /* Parse the part of a function-definition that follows the
25613 declarator. INLINE_P is TRUE iff this function is an inline
25614 function defined within a class-specifier.
25616 Returns the function defined. */
25618 static tree
25619 cp_parser_function_definition_after_declarator (cp_parser* parser,
25620 bool inline_p)
25622 tree fn;
25623 bool ctor_initializer_p = false;
25624 bool saved_in_unbraced_linkage_specification_p;
25625 bool saved_in_function_body;
25626 unsigned saved_num_template_parameter_lists;
25627 cp_token *token;
25628 bool fully_implicit_function_template_p
25629 = parser->fully_implicit_function_template_p;
25630 parser->fully_implicit_function_template_p = false;
25631 tree implicit_template_parms
25632 = parser->implicit_template_parms;
25633 parser->implicit_template_parms = 0;
25634 cp_binding_level* implicit_template_scope
25635 = parser->implicit_template_scope;
25636 parser->implicit_template_scope = 0;
25638 saved_in_function_body = parser->in_function_body;
25639 parser->in_function_body = true;
25640 /* If the next token is `return', then the code may be trying to
25641 make use of the "named return value" extension that G++ used to
25642 support. */
25643 token = cp_lexer_peek_token (parser->lexer);
25644 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
25646 /* Consume the `return' keyword. */
25647 cp_lexer_consume_token (parser->lexer);
25648 /* Look for the identifier that indicates what value is to be
25649 returned. */
25650 cp_parser_identifier (parser);
25651 /* Issue an error message. */
25652 error_at (token->location,
25653 "named return values are no longer supported");
25654 /* Skip tokens until we reach the start of the function body. */
25655 while (true)
25657 cp_token *token = cp_lexer_peek_token (parser->lexer);
25658 if (token->type == CPP_OPEN_BRACE
25659 || token->type == CPP_EOF
25660 || token->type == CPP_PRAGMA_EOL)
25661 break;
25662 cp_lexer_consume_token (parser->lexer);
25665 /* The `extern' in `extern "C" void f () { ... }' does not apply to
25666 anything declared inside `f'. */
25667 saved_in_unbraced_linkage_specification_p
25668 = parser->in_unbraced_linkage_specification_p;
25669 parser->in_unbraced_linkage_specification_p = false;
25670 /* Inside the function, surrounding template-parameter-lists do not
25671 apply. */
25672 saved_num_template_parameter_lists
25673 = parser->num_template_parameter_lists;
25674 parser->num_template_parameter_lists = 0;
25676 start_lambda_scope (current_function_decl);
25678 /* If the next token is `try', `__transaction_atomic', or
25679 `__transaction_relaxed`, then we are looking at either function-try-block
25680 or function-transaction-block. Note that all of these include the
25681 function-body. */
25682 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
25683 ctor_initializer_p = cp_parser_function_transaction (parser,
25684 RID_TRANSACTION_ATOMIC);
25685 else if (cp_lexer_next_token_is_keyword (parser->lexer,
25686 RID_TRANSACTION_RELAXED))
25687 ctor_initializer_p = cp_parser_function_transaction (parser,
25688 RID_TRANSACTION_RELAXED);
25689 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
25690 ctor_initializer_p = cp_parser_function_try_block (parser);
25691 else
25692 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
25693 (parser, /*in_function_try_block=*/false);
25695 finish_lambda_scope ();
25697 /* Finish the function. */
25698 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
25699 (inline_p ? 2 : 0));
25700 /* Generate code for it, if necessary. */
25701 expand_or_defer_fn (fn);
25702 /* Restore the saved values. */
25703 parser->in_unbraced_linkage_specification_p
25704 = saved_in_unbraced_linkage_specification_p;
25705 parser->num_template_parameter_lists
25706 = saved_num_template_parameter_lists;
25707 parser->in_function_body = saved_in_function_body;
25709 parser->fully_implicit_function_template_p
25710 = fully_implicit_function_template_p;
25711 parser->implicit_template_parms
25712 = implicit_template_parms;
25713 parser->implicit_template_scope
25714 = implicit_template_scope;
25716 if (parser->fully_implicit_function_template_p)
25717 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
25719 return fn;
25722 /* Parse a template-declaration body (following argument list). */
25724 static void
25725 cp_parser_template_declaration_after_parameters (cp_parser* parser,
25726 tree parameter_list,
25727 bool member_p)
25729 tree decl = NULL_TREE;
25730 bool friend_p = false;
25732 /* We just processed one more parameter list. */
25733 ++parser->num_template_parameter_lists;
25735 /* Get the deferred access checks from the parameter list. These
25736 will be checked once we know what is being declared, as for a
25737 member template the checks must be performed in the scope of the
25738 class containing the member. */
25739 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
25741 /* Tentatively parse for a new template parameter list, which can either be
25742 the template keyword or a template introduction. */
25743 if (cp_parser_template_declaration_after_export (parser, member_p))
25744 /* OK */;
25745 else if (cxx_dialect >= cxx11
25746 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25747 decl = cp_parser_alias_declaration (parser);
25748 else
25750 /* There are no access checks when parsing a template, as we do not
25751 know if a specialization will be a friend. */
25752 push_deferring_access_checks (dk_no_check);
25753 cp_token *token = cp_lexer_peek_token (parser->lexer);
25754 decl = cp_parser_single_declaration (parser,
25755 checks,
25756 member_p,
25757 /*explicit_specialization_p=*/false,
25758 &friend_p);
25759 pop_deferring_access_checks ();
25761 /* If this is a member template declaration, let the front
25762 end know. */
25763 if (member_p && !friend_p && decl)
25765 if (TREE_CODE (decl) == TYPE_DECL)
25766 cp_parser_check_access_in_redeclaration (decl, token->location);
25768 decl = finish_member_template_decl (decl);
25770 else if (friend_p && decl
25771 && DECL_DECLARES_TYPE_P (decl))
25772 make_friend_class (current_class_type, TREE_TYPE (decl),
25773 /*complain=*/true);
25775 /* We are done with the current parameter list. */
25776 --parser->num_template_parameter_lists;
25778 pop_deferring_access_checks ();
25780 /* Finish up. */
25781 finish_template_decl (parameter_list);
25783 /* Check the template arguments for a literal operator template. */
25784 if (decl
25785 && DECL_DECLARES_FUNCTION_P (decl)
25786 && UDLIT_OPER_P (DECL_NAME (decl)))
25788 bool ok = true;
25789 if (parameter_list == NULL_TREE)
25790 ok = false;
25791 else
25793 int num_parms = TREE_VEC_LENGTH (parameter_list);
25794 if (num_parms == 1)
25796 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
25797 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
25798 if (TREE_TYPE (parm) != char_type_node
25799 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
25800 ok = false;
25802 else if (num_parms == 2 && cxx_dialect >= cxx14)
25804 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
25805 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
25806 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
25807 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
25808 if (parm == error_mark_node
25809 || TREE_TYPE (parm) != TREE_TYPE (type)
25810 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
25811 ok = false;
25813 else
25814 ok = false;
25816 if (!ok)
25818 if (cxx_dialect >= cxx14)
25819 error ("literal operator template %qD has invalid parameter list."
25820 " Expected non-type template argument pack <char...>"
25821 " or <typename CharT, CharT...>",
25822 decl);
25823 else
25824 error ("literal operator template %qD has invalid parameter list."
25825 " Expected non-type template argument pack <char...>",
25826 decl);
25830 /* Register member declarations. */
25831 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
25832 finish_member_declaration (decl);
25833 /* If DECL is a function template, we must return to parse it later.
25834 (Even though there is no definition, there might be default
25835 arguments that need handling.) */
25836 if (member_p && decl
25837 && DECL_DECLARES_FUNCTION_P (decl))
25838 vec_safe_push (unparsed_funs_with_definitions, decl);
25841 /* Parse a template introduction header for a template-declaration. Returns
25842 false if tentative parse fails. */
25844 static bool
25845 cp_parser_template_introduction (cp_parser* parser, bool member_p)
25847 cp_parser_parse_tentatively (parser);
25849 tree saved_scope = parser->scope;
25850 tree saved_object_scope = parser->object_scope;
25851 tree saved_qualifying_scope = parser->qualifying_scope;
25853 /* Look for the optional `::' operator. */
25854 cp_parser_global_scope_opt (parser,
25855 /*current_scope_valid_p=*/false);
25856 /* Look for the nested-name-specifier. */
25857 cp_parser_nested_name_specifier_opt (parser,
25858 /*typename_keyword_p=*/false,
25859 /*check_dependency_p=*/true,
25860 /*type_p=*/false,
25861 /*is_declaration=*/false);
25863 cp_token *token = cp_lexer_peek_token (parser->lexer);
25864 tree concept_name = cp_parser_identifier (parser);
25866 /* Look up the concept for which we will be matching
25867 template parameters. */
25868 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
25869 token->location);
25870 parser->scope = saved_scope;
25871 parser->object_scope = saved_object_scope;
25872 parser->qualifying_scope = saved_qualifying_scope;
25874 if (concept_name == error_mark_node)
25875 cp_parser_simulate_error (parser);
25877 /* Look for opening brace for introduction. */
25878 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
25880 if (!cp_parser_parse_definitely (parser))
25881 return false;
25883 push_deferring_access_checks (dk_deferred);
25885 /* Build vector of placeholder parameters and grab
25886 matching identifiers. */
25887 tree introduction_list = cp_parser_introduction_list (parser);
25889 /* The introduction-list shall not be empty. */
25890 int nargs = TREE_VEC_LENGTH (introduction_list);
25891 if (nargs == 0)
25893 error ("empty introduction-list");
25894 return true;
25897 /* Look for closing brace for introduction. */
25898 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25899 return true;
25901 if (tmpl_decl == error_mark_node)
25903 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
25904 token->location);
25905 return true;
25908 /* Build and associate the constraint. */
25909 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
25910 if (parms && parms != error_mark_node)
25912 cp_parser_template_declaration_after_parameters (parser, parms,
25913 member_p);
25914 return true;
25917 error_at (token->location, "no matching concept for template-introduction");
25918 return true;
25921 /* Parse a normal template-declaration following the template keyword. */
25923 static void
25924 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
25926 tree parameter_list;
25927 bool need_lang_pop;
25928 location_t location = input_location;
25930 /* Look for the `<' token. */
25931 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
25932 return;
25933 if (at_class_scope_p () && current_function_decl)
25935 /* 14.5.2.2 [temp.mem]
25937 A local class shall not have member templates. */
25938 error_at (location,
25939 "invalid declaration of member template in local class");
25940 cp_parser_skip_to_end_of_block_or_statement (parser);
25941 return;
25943 /* [temp]
25945 A template ... shall not have C linkage. */
25946 if (current_lang_name == lang_name_c)
25948 error_at (location, "template with C linkage");
25949 /* Give it C++ linkage to avoid confusing other parts of the
25950 front end. */
25951 push_lang_context (lang_name_cplusplus);
25952 need_lang_pop = true;
25954 else
25955 need_lang_pop = false;
25957 /* We cannot perform access checks on the template parameter
25958 declarations until we know what is being declared, just as we
25959 cannot check the decl-specifier list. */
25960 push_deferring_access_checks (dk_deferred);
25962 /* If the next token is `>', then we have an invalid
25963 specialization. Rather than complain about an invalid template
25964 parameter, issue an error message here. */
25965 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
25967 cp_parser_error (parser, "invalid explicit specialization");
25968 begin_specialization ();
25969 parameter_list = NULL_TREE;
25971 else
25973 /* Parse the template parameters. */
25974 parameter_list = cp_parser_template_parameter_list (parser);
25977 /* Look for the `>'. */
25978 cp_parser_skip_to_end_of_template_parameter_list (parser);
25980 /* Manage template requirements */
25981 if (flag_concepts)
25983 tree reqs = get_shorthand_constraints (current_template_parms);
25984 if (tree r = cp_parser_requires_clause_opt (parser))
25985 reqs = conjoin_constraints (reqs, normalize_expression (r));
25986 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
25989 cp_parser_template_declaration_after_parameters (parser, parameter_list,
25990 member_p);
25992 /* For the erroneous case of a template with C linkage, we pushed an
25993 implicit C++ linkage scope; exit that scope now. */
25994 if (need_lang_pop)
25995 pop_lang_context ();
25998 /* Parse a template-declaration, assuming that the `export' (and
25999 `extern') keywords, if present, has already been scanned. MEMBER_P
26000 is as for cp_parser_template_declaration. */
26002 static bool
26003 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
26005 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26007 cp_lexer_consume_token (parser->lexer);
26008 cp_parser_explicit_template_declaration (parser, member_p);
26009 return true;
26011 else if (flag_concepts)
26012 return cp_parser_template_introduction (parser, member_p);
26014 return false;
26017 /* Perform the deferred access checks from a template-parameter-list.
26018 CHECKS is a TREE_LIST of access checks, as returned by
26019 get_deferred_access_checks. */
26021 static void
26022 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
26024 ++processing_template_parmlist;
26025 perform_access_checks (checks, tf_warning_or_error);
26026 --processing_template_parmlist;
26029 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
26030 `function-definition' sequence that follows a template header.
26031 If MEMBER_P is true, this declaration appears in a class scope.
26033 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
26034 *FRIEND_P is set to TRUE iff the declaration is a friend. */
26036 static tree
26037 cp_parser_single_declaration (cp_parser* parser,
26038 vec<deferred_access_check, va_gc> *checks,
26039 bool member_p,
26040 bool explicit_specialization_p,
26041 bool* friend_p)
26043 int declares_class_or_enum;
26044 tree decl = NULL_TREE;
26045 cp_decl_specifier_seq decl_specifiers;
26046 bool function_definition_p = false;
26047 cp_token *decl_spec_token_start;
26049 /* This function is only used when processing a template
26050 declaration. */
26051 gcc_assert (innermost_scope_kind () == sk_template_parms
26052 || innermost_scope_kind () == sk_template_spec);
26054 /* Defer access checks until we know what is being declared. */
26055 push_deferring_access_checks (dk_deferred);
26057 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
26058 alternative. */
26059 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
26060 cp_parser_decl_specifier_seq (parser,
26061 CP_PARSER_FLAGS_OPTIONAL,
26062 &decl_specifiers,
26063 &declares_class_or_enum);
26064 if (friend_p)
26065 *friend_p = cp_parser_friend_p (&decl_specifiers);
26067 /* There are no template typedefs. */
26068 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
26070 error_at (decl_spec_token_start->location,
26071 "template declaration of %<typedef%>");
26072 decl = error_mark_node;
26075 /* Gather up the access checks that occurred the
26076 decl-specifier-seq. */
26077 stop_deferring_access_checks ();
26079 /* Check for the declaration of a template class. */
26080 if (declares_class_or_enum)
26082 if (cp_parser_declares_only_class_p (parser)
26083 || (declares_class_or_enum & 2))
26085 // If this is a declaration, but not a definition, associate
26086 // any constraints with the type declaration. Constraints
26087 // are associated with definitions in cp_parser_class_specifier.
26088 if (declares_class_or_enum == 1)
26089 associate_classtype_constraints (decl_specifiers.type);
26091 decl = shadow_tag (&decl_specifiers);
26093 /* In this case:
26095 struct C {
26096 friend template <typename T> struct A<T>::B;
26099 A<T>::B will be represented by a TYPENAME_TYPE, and
26100 therefore not recognized by shadow_tag. */
26101 if (friend_p && *friend_p
26102 && !decl
26103 && decl_specifiers.type
26104 && TYPE_P (decl_specifiers.type))
26105 decl = decl_specifiers.type;
26107 if (decl && decl != error_mark_node)
26108 decl = TYPE_NAME (decl);
26109 else
26110 decl = error_mark_node;
26112 /* Perform access checks for template parameters. */
26113 cp_parser_perform_template_parameter_access_checks (checks);
26115 /* Give a helpful diagnostic for
26116 template <class T> struct A { } a;
26117 if we aren't already recovering from an error. */
26118 if (!cp_parser_declares_only_class_p (parser)
26119 && !seen_error ())
26121 error_at (cp_lexer_peek_token (parser->lexer)->location,
26122 "a class template declaration must not declare "
26123 "anything else");
26124 cp_parser_skip_to_end_of_block_or_statement (parser);
26125 goto out;
26130 /* Complain about missing 'typename' or other invalid type names. */
26131 if (!decl_specifiers.any_type_specifiers_p
26132 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
26134 /* cp_parser_parse_and_diagnose_invalid_type_name calls
26135 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
26136 the rest of this declaration. */
26137 decl = error_mark_node;
26138 goto out;
26141 /* If it's not a template class, try for a template function. If
26142 the next token is a `;', then this declaration does not declare
26143 anything. But, if there were errors in the decl-specifiers, then
26144 the error might well have come from an attempted class-specifier.
26145 In that case, there's no need to warn about a missing declarator. */
26146 if (!decl
26147 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
26148 || decl_specifiers.type != error_mark_node))
26150 decl = cp_parser_init_declarator (parser,
26151 &decl_specifiers,
26152 checks,
26153 /*function_definition_allowed_p=*/true,
26154 member_p,
26155 declares_class_or_enum,
26156 &function_definition_p,
26157 NULL, NULL, NULL);
26159 /* 7.1.1-1 [dcl.stc]
26161 A storage-class-specifier shall not be specified in an explicit
26162 specialization... */
26163 if (decl
26164 && explicit_specialization_p
26165 && decl_specifiers.storage_class != sc_none)
26167 error_at (decl_spec_token_start->location,
26168 "explicit template specialization cannot have a storage class");
26169 decl = error_mark_node;
26172 if (decl && VAR_P (decl))
26173 check_template_variable (decl);
26176 /* Look for a trailing `;' after the declaration. */
26177 if (!function_definition_p
26178 && (decl == error_mark_node
26179 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
26180 cp_parser_skip_to_end_of_block_or_statement (parser);
26182 out:
26183 pop_deferring_access_checks ();
26185 /* Clear any current qualification; whatever comes next is the start
26186 of something new. */
26187 parser->scope = NULL_TREE;
26188 parser->qualifying_scope = NULL_TREE;
26189 parser->object_scope = NULL_TREE;
26191 return decl;
26194 /* Parse a cast-expression that is not the operand of a unary "&". */
26196 static cp_expr
26197 cp_parser_simple_cast_expression (cp_parser *parser)
26199 return cp_parser_cast_expression (parser, /*address_p=*/false,
26200 /*cast_p=*/false, /*decltype*/false, NULL);
26203 /* Parse a functional cast to TYPE. Returns an expression
26204 representing the cast. */
26206 static cp_expr
26207 cp_parser_functional_cast (cp_parser* parser, tree type)
26209 vec<tree, va_gc> *vec;
26210 tree expression_list;
26211 cp_expr cast;
26212 bool nonconst_p;
26214 location_t start_loc = input_location;
26216 if (!type)
26217 type = error_mark_node;
26219 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26221 cp_lexer_set_source_position (parser->lexer);
26222 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26223 expression_list = cp_parser_braced_list (parser, &nonconst_p);
26224 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
26225 if (TREE_CODE (type) == TYPE_DECL)
26226 type = TREE_TYPE (type);
26228 cast = finish_compound_literal (type, expression_list,
26229 tf_warning_or_error);
26230 /* Create a location of the form:
26231 type_name{i, f}
26232 ^~~~~~~~~~~~~~~
26233 with caret == start at the start of the type name,
26234 finishing at the closing brace. */
26235 location_t finish_loc
26236 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26237 location_t combined_loc = make_location (start_loc, start_loc,
26238 finish_loc);
26239 cast.set_location (combined_loc);
26240 return cast;
26244 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
26245 /*cast_p=*/true,
26246 /*allow_expansion_p=*/true,
26247 /*non_constant_p=*/NULL);
26248 if (vec == NULL)
26249 expression_list = error_mark_node;
26250 else
26252 expression_list = build_tree_list_vec (vec);
26253 release_tree_vector (vec);
26256 cast = build_functional_cast (type, expression_list,
26257 tf_warning_or_error);
26258 /* [expr.const]/1: In an integral constant expression "only type
26259 conversions to integral or enumeration type can be used". */
26260 if (TREE_CODE (type) == TYPE_DECL)
26261 type = TREE_TYPE (type);
26262 if (cast != error_mark_node
26263 && !cast_valid_in_integral_constant_expression_p (type)
26264 && cp_parser_non_integral_constant_expression (parser,
26265 NIC_CONSTRUCTOR))
26266 return error_mark_node;
26268 /* Create a location of the form:
26269 float(i)
26270 ^~~~~~~~
26271 with caret == start at the start of the type name,
26272 finishing at the closing paren. */
26273 location_t finish_loc
26274 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26275 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
26276 cast.set_location (combined_loc);
26277 return cast;
26280 /* Save the tokens that make up the body of a member function defined
26281 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
26282 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
26283 specifiers applied to the declaration. Returns the FUNCTION_DECL
26284 for the member function. */
26286 static tree
26287 cp_parser_save_member_function_body (cp_parser* parser,
26288 cp_decl_specifier_seq *decl_specifiers,
26289 cp_declarator *declarator,
26290 tree attributes)
26292 cp_token *first;
26293 cp_token *last;
26294 tree fn;
26295 bool function_try_block = false;
26297 /* Create the FUNCTION_DECL. */
26298 fn = grokmethod (decl_specifiers, declarator, attributes);
26299 cp_finalize_omp_declare_simd (parser, fn);
26300 cp_finalize_oacc_routine (parser, fn, true);
26301 /* If something went badly wrong, bail out now. */
26302 if (fn == error_mark_node)
26304 /* If there's a function-body, skip it. */
26305 if (cp_parser_token_starts_function_definition_p
26306 (cp_lexer_peek_token (parser->lexer)))
26307 cp_parser_skip_to_end_of_block_or_statement (parser);
26308 return error_mark_node;
26311 /* Remember it, if there default args to post process. */
26312 cp_parser_save_default_args (parser, fn);
26314 /* Save away the tokens that make up the body of the
26315 function. */
26316 first = parser->lexer->next_token;
26318 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
26319 cp_lexer_consume_token (parser->lexer);
26320 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26321 RID_TRANSACTION_ATOMIC))
26323 cp_lexer_consume_token (parser->lexer);
26324 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
26325 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
26326 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
26327 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
26328 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
26329 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
26330 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
26332 cp_lexer_consume_token (parser->lexer);
26333 cp_lexer_consume_token (parser->lexer);
26334 cp_lexer_consume_token (parser->lexer);
26335 cp_lexer_consume_token (parser->lexer);
26336 cp_lexer_consume_token (parser->lexer);
26338 else
26339 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
26340 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
26342 cp_lexer_consume_token (parser->lexer);
26343 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26344 break;
26348 /* Handle function try blocks. */
26349 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26351 cp_lexer_consume_token (parser->lexer);
26352 function_try_block = true;
26354 /* We can have braced-init-list mem-initializers before the fn body. */
26355 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26357 cp_lexer_consume_token (parser->lexer);
26358 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
26360 /* cache_group will stop after an un-nested { } pair, too. */
26361 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26362 break;
26364 /* variadic mem-inits have ... after the ')'. */
26365 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26366 cp_lexer_consume_token (parser->lexer);
26369 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26370 /* Handle function try blocks. */
26371 if (function_try_block)
26372 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
26373 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26374 last = parser->lexer->next_token;
26376 /* Save away the inline definition; we will process it when the
26377 class is complete. */
26378 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
26379 DECL_PENDING_INLINE_P (fn) = 1;
26381 /* We need to know that this was defined in the class, so that
26382 friend templates are handled correctly. */
26383 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
26385 /* Add FN to the queue of functions to be parsed later. */
26386 vec_safe_push (unparsed_funs_with_definitions, fn);
26388 return fn;
26391 /* Save the tokens that make up the in-class initializer for a non-static
26392 data member. Returns a DEFAULT_ARG. */
26394 static tree
26395 cp_parser_save_nsdmi (cp_parser* parser)
26397 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
26400 /* Parse a template-argument-list, as well as the trailing ">" (but
26401 not the opening "<"). See cp_parser_template_argument_list for the
26402 return value. */
26404 static tree
26405 cp_parser_enclosed_template_argument_list (cp_parser* parser)
26407 tree arguments;
26408 tree saved_scope;
26409 tree saved_qualifying_scope;
26410 tree saved_object_scope;
26411 bool saved_greater_than_is_operator_p;
26412 int saved_unevaluated_operand;
26413 int saved_inhibit_evaluation_warnings;
26415 /* [temp.names]
26417 When parsing a template-id, the first non-nested `>' is taken as
26418 the end of the template-argument-list rather than a greater-than
26419 operator. */
26420 saved_greater_than_is_operator_p
26421 = parser->greater_than_is_operator_p;
26422 parser->greater_than_is_operator_p = false;
26423 /* Parsing the argument list may modify SCOPE, so we save it
26424 here. */
26425 saved_scope = parser->scope;
26426 saved_qualifying_scope = parser->qualifying_scope;
26427 saved_object_scope = parser->object_scope;
26428 /* We need to evaluate the template arguments, even though this
26429 template-id may be nested within a "sizeof". */
26430 saved_unevaluated_operand = cp_unevaluated_operand;
26431 cp_unevaluated_operand = 0;
26432 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
26433 c_inhibit_evaluation_warnings = 0;
26434 /* Parse the template-argument-list itself. */
26435 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
26436 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26437 arguments = NULL_TREE;
26438 else
26439 arguments = cp_parser_template_argument_list (parser);
26440 /* Look for the `>' that ends the template-argument-list. If we find
26441 a '>>' instead, it's probably just a typo. */
26442 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26444 if (cxx_dialect != cxx98)
26446 /* In C++0x, a `>>' in a template argument list or cast
26447 expression is considered to be two separate `>'
26448 tokens. So, change the current token to a `>', but don't
26449 consume it: it will be consumed later when the outer
26450 template argument list (or cast expression) is parsed.
26451 Note that this replacement of `>' for `>>' is necessary
26452 even if we are parsing tentatively: in the tentative
26453 case, after calling
26454 cp_parser_enclosed_template_argument_list we will always
26455 throw away all of the template arguments and the first
26456 closing `>', either because the template argument list
26457 was erroneous or because we are replacing those tokens
26458 with a CPP_TEMPLATE_ID token. The second `>' (which will
26459 not have been thrown away) is needed either to close an
26460 outer template argument list or to complete a new-style
26461 cast. */
26462 cp_token *token = cp_lexer_peek_token (parser->lexer);
26463 token->type = CPP_GREATER;
26465 else if (!saved_greater_than_is_operator_p)
26467 /* If we're in a nested template argument list, the '>>' has
26468 to be a typo for '> >'. We emit the error message, but we
26469 continue parsing and we push a '>' as next token, so that
26470 the argument list will be parsed correctly. Note that the
26471 global source location is still on the token before the
26472 '>>', so we need to say explicitly where we want it. */
26473 cp_token *token = cp_lexer_peek_token (parser->lexer);
26474 gcc_rich_location richloc (token->location);
26475 richloc.add_fixit_replace ("> >");
26476 error_at_rich_loc (&richloc, "%<>>%> should be %<> >%> "
26477 "within a nested template argument list");
26479 token->type = CPP_GREATER;
26481 else
26483 /* If this is not a nested template argument list, the '>>'
26484 is a typo for '>'. Emit an error message and continue.
26485 Same deal about the token location, but here we can get it
26486 right by consuming the '>>' before issuing the diagnostic. */
26487 cp_token *token = cp_lexer_consume_token (parser->lexer);
26488 error_at (token->location,
26489 "spurious %<>>%>, use %<>%> to terminate "
26490 "a template argument list");
26493 else
26494 cp_parser_skip_to_end_of_template_parameter_list (parser);
26495 /* The `>' token might be a greater-than operator again now. */
26496 parser->greater_than_is_operator_p
26497 = saved_greater_than_is_operator_p;
26498 /* Restore the SAVED_SCOPE. */
26499 parser->scope = saved_scope;
26500 parser->qualifying_scope = saved_qualifying_scope;
26501 parser->object_scope = saved_object_scope;
26502 cp_unevaluated_operand = saved_unevaluated_operand;
26503 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
26505 return arguments;
26508 /* MEMBER_FUNCTION is a member function, or a friend. If default
26509 arguments, or the body of the function have not yet been parsed,
26510 parse them now. */
26512 static void
26513 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
26515 timevar_push (TV_PARSE_INMETH);
26516 /* If this member is a template, get the underlying
26517 FUNCTION_DECL. */
26518 if (DECL_FUNCTION_TEMPLATE_P (member_function))
26519 member_function = DECL_TEMPLATE_RESULT (member_function);
26521 /* There should not be any class definitions in progress at this
26522 point; the bodies of members are only parsed outside of all class
26523 definitions. */
26524 gcc_assert (parser->num_classes_being_defined == 0);
26525 /* While we're parsing the member functions we might encounter more
26526 classes. We want to handle them right away, but we don't want
26527 them getting mixed up with functions that are currently in the
26528 queue. */
26529 push_unparsed_function_queues (parser);
26531 /* Make sure that any template parameters are in scope. */
26532 maybe_begin_member_template_processing (member_function);
26534 /* If the body of the function has not yet been parsed, parse it
26535 now. */
26536 if (DECL_PENDING_INLINE_P (member_function))
26538 tree function_scope;
26539 cp_token_cache *tokens;
26541 /* The function is no longer pending; we are processing it. */
26542 tokens = DECL_PENDING_INLINE_INFO (member_function);
26543 DECL_PENDING_INLINE_INFO (member_function) = NULL;
26544 DECL_PENDING_INLINE_P (member_function) = 0;
26546 /* If this is a local class, enter the scope of the containing
26547 function. */
26548 function_scope = current_function_decl;
26549 if (function_scope)
26550 push_function_context ();
26552 /* Push the body of the function onto the lexer stack. */
26553 cp_parser_push_lexer_for_tokens (parser, tokens);
26555 /* Let the front end know that we going to be defining this
26556 function. */
26557 start_preparsed_function (member_function, NULL_TREE,
26558 SF_PRE_PARSED | SF_INCLASS_INLINE);
26560 /* Don't do access checking if it is a templated function. */
26561 if (processing_template_decl)
26562 push_deferring_access_checks (dk_no_check);
26564 /* #pragma omp declare reduction needs special parsing. */
26565 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
26567 parser->lexer->in_pragma = true;
26568 cp_parser_omp_declare_reduction_exprs (member_function, parser);
26569 finish_function (/*inline*/2);
26570 cp_check_omp_declare_reduction (member_function);
26572 else
26573 /* Now, parse the body of the function. */
26574 cp_parser_function_definition_after_declarator (parser,
26575 /*inline_p=*/true);
26577 if (processing_template_decl)
26578 pop_deferring_access_checks ();
26580 /* Leave the scope of the containing function. */
26581 if (function_scope)
26582 pop_function_context ();
26583 cp_parser_pop_lexer (parser);
26586 /* Remove any template parameters from the symbol table. */
26587 maybe_end_member_template_processing ();
26589 /* Restore the queue. */
26590 pop_unparsed_function_queues (parser);
26591 timevar_pop (TV_PARSE_INMETH);
26594 /* If DECL contains any default args, remember it on the unparsed
26595 functions queue. */
26597 static void
26598 cp_parser_save_default_args (cp_parser* parser, tree decl)
26600 tree probe;
26602 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
26603 probe;
26604 probe = TREE_CHAIN (probe))
26605 if (TREE_PURPOSE (probe))
26607 cp_default_arg_entry entry = {current_class_type, decl};
26608 vec_safe_push (unparsed_funs_with_default_args, entry);
26609 break;
26613 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
26614 which is either a FIELD_DECL or PARM_DECL. Parse it and return
26615 the result. For a PARM_DECL, PARMTYPE is the corresponding type
26616 from the parameter-type-list. */
26618 static tree
26619 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
26620 tree default_arg, tree parmtype)
26622 cp_token_cache *tokens;
26623 tree parsed_arg;
26624 bool dummy;
26626 if (default_arg == error_mark_node)
26627 return error_mark_node;
26629 /* Push the saved tokens for the default argument onto the parser's
26630 lexer stack. */
26631 tokens = DEFARG_TOKENS (default_arg);
26632 cp_parser_push_lexer_for_tokens (parser, tokens);
26634 start_lambda_scope (decl);
26636 /* Parse the default argument. */
26637 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
26638 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
26639 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26641 finish_lambda_scope ();
26643 if (parsed_arg == error_mark_node)
26644 cp_parser_skip_to_end_of_statement (parser);
26646 if (!processing_template_decl)
26648 /* In a non-template class, check conversions now. In a template,
26649 we'll wait and instantiate these as needed. */
26650 if (TREE_CODE (decl) == PARM_DECL)
26651 parsed_arg = check_default_argument (parmtype, parsed_arg,
26652 tf_warning_or_error);
26653 else
26654 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
26657 /* If the token stream has not been completely used up, then
26658 there was extra junk after the end of the default
26659 argument. */
26660 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26662 if (TREE_CODE (decl) == PARM_DECL)
26663 cp_parser_error (parser, "expected %<,%>");
26664 else
26665 cp_parser_error (parser, "expected %<;%>");
26668 /* Revert to the main lexer. */
26669 cp_parser_pop_lexer (parser);
26671 return parsed_arg;
26674 /* FIELD is a non-static data member with an initializer which we saved for
26675 later; parse it now. */
26677 static void
26678 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
26680 tree def;
26682 maybe_begin_member_template_processing (field);
26684 push_unparsed_function_queues (parser);
26685 def = cp_parser_late_parse_one_default_arg (parser, field,
26686 DECL_INITIAL (field),
26687 NULL_TREE);
26688 pop_unparsed_function_queues (parser);
26690 maybe_end_member_template_processing ();
26692 DECL_INITIAL (field) = def;
26695 /* FN is a FUNCTION_DECL which may contains a parameter with an
26696 unparsed DEFAULT_ARG. Parse the default args now. This function
26697 assumes that the current scope is the scope in which the default
26698 argument should be processed. */
26700 static void
26701 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
26703 bool saved_local_variables_forbidden_p;
26704 tree parm, parmdecl;
26706 /* While we're parsing the default args, we might (due to the
26707 statement expression extension) encounter more classes. We want
26708 to handle them right away, but we don't want them getting mixed
26709 up with default args that are currently in the queue. */
26710 push_unparsed_function_queues (parser);
26712 /* Local variable names (and the `this' keyword) may not appear
26713 in a default argument. */
26714 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
26715 parser->local_variables_forbidden_p = true;
26717 push_defarg_context (fn);
26719 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
26720 parmdecl = DECL_ARGUMENTS (fn);
26721 parm && parm != void_list_node;
26722 parm = TREE_CHAIN (parm),
26723 parmdecl = DECL_CHAIN (parmdecl))
26725 tree default_arg = TREE_PURPOSE (parm);
26726 tree parsed_arg;
26727 vec<tree, va_gc> *insts;
26728 tree copy;
26729 unsigned ix;
26731 if (!default_arg)
26732 continue;
26734 if (TREE_CODE (default_arg) != DEFAULT_ARG)
26735 /* This can happen for a friend declaration for a function
26736 already declared with default arguments. */
26737 continue;
26739 parsed_arg
26740 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
26741 default_arg,
26742 TREE_VALUE (parm));
26743 if (parsed_arg == error_mark_node)
26745 continue;
26748 TREE_PURPOSE (parm) = parsed_arg;
26750 /* Update any instantiations we've already created. */
26751 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
26752 vec_safe_iterate (insts, ix, &copy); ix++)
26753 TREE_PURPOSE (copy) = parsed_arg;
26756 pop_defarg_context ();
26758 /* Make sure no default arg is missing. */
26759 check_default_args (fn);
26761 /* Restore the state of local_variables_forbidden_p. */
26762 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
26764 /* Restore the queue. */
26765 pop_unparsed_function_queues (parser);
26768 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
26770 sizeof ... ( identifier )
26772 where the 'sizeof' token has already been consumed. */
26774 static tree
26775 cp_parser_sizeof_pack (cp_parser *parser)
26777 /* Consume the `...'. */
26778 cp_lexer_consume_token (parser->lexer);
26779 maybe_warn_variadic_templates ();
26781 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
26782 if (paren)
26783 cp_lexer_consume_token (parser->lexer);
26784 else
26785 permerror (cp_lexer_peek_token (parser->lexer)->location,
26786 "%<sizeof...%> argument must be surrounded by parentheses");
26788 cp_token *token = cp_lexer_peek_token (parser->lexer);
26789 tree name = cp_parser_identifier (parser);
26790 if (name == error_mark_node)
26791 return error_mark_node;
26792 /* The name is not qualified. */
26793 parser->scope = NULL_TREE;
26794 parser->qualifying_scope = NULL_TREE;
26795 parser->object_scope = NULL_TREE;
26796 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
26797 if (expr == error_mark_node)
26798 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
26799 token->location);
26800 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
26801 expr = TREE_TYPE (expr);
26802 else if (TREE_CODE (expr) == CONST_DECL)
26803 expr = DECL_INITIAL (expr);
26804 expr = make_pack_expansion (expr);
26805 PACK_EXPANSION_SIZEOF_P (expr) = true;
26807 if (paren)
26808 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26810 return expr;
26813 /* Parse the operand of `sizeof' (or a similar operator). Returns
26814 either a TYPE or an expression, depending on the form of the
26815 input. The KEYWORD indicates which kind of expression we have
26816 encountered. */
26818 static tree
26819 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
26821 tree expr = NULL_TREE;
26822 const char *saved_message;
26823 char *tmp;
26824 bool saved_integral_constant_expression_p;
26825 bool saved_non_integral_constant_expression_p;
26827 /* If it's a `...', then we are computing the length of a parameter
26828 pack. */
26829 if (keyword == RID_SIZEOF
26830 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26831 return cp_parser_sizeof_pack (parser);
26833 /* Types cannot be defined in a `sizeof' expression. Save away the
26834 old message. */
26835 saved_message = parser->type_definition_forbidden_message;
26836 /* And create the new one. */
26837 tmp = concat ("types may not be defined in %<",
26838 IDENTIFIER_POINTER (ridpointers[keyword]),
26839 "%> expressions", NULL);
26840 parser->type_definition_forbidden_message = tmp;
26842 /* The restrictions on constant-expressions do not apply inside
26843 sizeof expressions. */
26844 saved_integral_constant_expression_p
26845 = parser->integral_constant_expression_p;
26846 saved_non_integral_constant_expression_p
26847 = parser->non_integral_constant_expression_p;
26848 parser->integral_constant_expression_p = false;
26850 /* Do not actually evaluate the expression. */
26851 ++cp_unevaluated_operand;
26852 ++c_inhibit_evaluation_warnings;
26853 /* If it's a `(', then we might be looking at the type-id
26854 construction. */
26855 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26857 tree type = NULL_TREE;
26859 /* We can't be sure yet whether we're looking at a type-id or an
26860 expression. */
26861 cp_parser_parse_tentatively (parser);
26862 /* Note: as a GNU Extension, compound literals are considered
26863 postfix-expressions as they are in C99, so they are valid
26864 arguments to sizeof. See comment in cp_parser_cast_expression
26865 for details. */
26866 if (cp_parser_compound_literal_p (parser))
26867 cp_parser_simulate_error (parser);
26868 else
26870 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
26871 parser->in_type_id_in_expr_p = true;
26872 /* Look for the type-id. */
26873 type = cp_parser_type_id (parser);
26874 /* Look for the closing `)'. */
26875 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26876 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
26879 /* If all went well, then we're done. */
26880 if (cp_parser_parse_definitely (parser))
26882 cp_decl_specifier_seq decl_specs;
26884 /* Build a trivial decl-specifier-seq. */
26885 clear_decl_specs (&decl_specs);
26886 decl_specs.type = type;
26888 /* Call grokdeclarator to figure out what type this is. */
26889 expr = grokdeclarator (NULL,
26890 &decl_specs,
26891 TYPENAME,
26892 /*initialized=*/0,
26893 /*attrlist=*/NULL);
26897 /* If the type-id production did not work out, then we must be
26898 looking at the unary-expression production. */
26899 if (!expr)
26900 expr = cp_parser_unary_expression (parser);
26902 /* Go back to evaluating expressions. */
26903 --cp_unevaluated_operand;
26904 --c_inhibit_evaluation_warnings;
26906 /* Free the message we created. */
26907 free (tmp);
26908 /* And restore the old one. */
26909 parser->type_definition_forbidden_message = saved_message;
26910 parser->integral_constant_expression_p
26911 = saved_integral_constant_expression_p;
26912 parser->non_integral_constant_expression_p
26913 = saved_non_integral_constant_expression_p;
26915 return expr;
26918 /* If the current declaration has no declarator, return true. */
26920 static bool
26921 cp_parser_declares_only_class_p (cp_parser *parser)
26923 /* If the next token is a `;' or a `,' then there is no
26924 declarator. */
26925 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26926 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
26929 /* Update the DECL_SPECS to reflect the storage class indicated by
26930 KEYWORD. */
26932 static void
26933 cp_parser_set_storage_class (cp_parser *parser,
26934 cp_decl_specifier_seq *decl_specs,
26935 enum rid keyword,
26936 cp_token *token)
26938 cp_storage_class storage_class;
26940 if (parser->in_unbraced_linkage_specification_p)
26942 error_at (token->location, "invalid use of %qD in linkage specification",
26943 ridpointers[keyword]);
26944 return;
26946 else if (decl_specs->storage_class != sc_none)
26948 decl_specs->conflicting_specifiers_p = true;
26949 return;
26952 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
26953 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
26954 && decl_specs->gnu_thread_keyword_p)
26956 pedwarn (decl_specs->locations[ds_thread], 0,
26957 "%<__thread%> before %qD", ridpointers[keyword]);
26960 switch (keyword)
26962 case RID_AUTO:
26963 storage_class = sc_auto;
26964 break;
26965 case RID_REGISTER:
26966 storage_class = sc_register;
26967 break;
26968 case RID_STATIC:
26969 storage_class = sc_static;
26970 break;
26971 case RID_EXTERN:
26972 storage_class = sc_extern;
26973 break;
26974 case RID_MUTABLE:
26975 storage_class = sc_mutable;
26976 break;
26977 default:
26978 gcc_unreachable ();
26980 decl_specs->storage_class = storage_class;
26981 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
26983 /* A storage class specifier cannot be applied alongside a typedef
26984 specifier. If there is a typedef specifier present then set
26985 conflicting_specifiers_p which will trigger an error later
26986 on in grokdeclarator. */
26987 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
26988 decl_specs->conflicting_specifiers_p = true;
26991 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
26992 is true, the type is a class or enum definition. */
26994 static void
26995 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
26996 tree type_spec,
26997 cp_token *token,
26998 bool type_definition_p)
27000 decl_specs->any_specifiers_p = true;
27002 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
27003 (with, for example, in "typedef int wchar_t;") we remember that
27004 this is what happened. In system headers, we ignore these
27005 declarations so that G++ can work with system headers that are not
27006 C++-safe. */
27007 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
27008 && !type_definition_p
27009 && (type_spec == boolean_type_node
27010 || type_spec == char16_type_node
27011 || type_spec == char32_type_node
27012 || type_spec == wchar_type_node)
27013 && (decl_specs->type
27014 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
27015 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
27016 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
27017 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
27019 decl_specs->redefined_builtin_type = type_spec;
27020 set_and_check_decl_spec_loc (decl_specs,
27021 ds_redefined_builtin_type_spec,
27022 token);
27023 if (!decl_specs->type)
27025 decl_specs->type = type_spec;
27026 decl_specs->type_definition_p = false;
27027 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
27030 else if (decl_specs->type)
27031 decl_specs->multiple_types_p = true;
27032 else
27034 decl_specs->type = type_spec;
27035 decl_specs->type_definition_p = type_definition_p;
27036 decl_specs->redefined_builtin_type = NULL_TREE;
27037 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
27041 /* True iff TOKEN is the GNU keyword __thread. */
27043 static bool
27044 token_is__thread (cp_token *token)
27046 gcc_assert (token->keyword == RID_THREAD);
27047 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
27050 /* Set the location for a declarator specifier and check if it is
27051 duplicated.
27053 DECL_SPECS is the sequence of declarator specifiers onto which to
27054 set the location.
27056 DS is the single declarator specifier to set which location is to
27057 be set onto the existing sequence of declarators.
27059 LOCATION is the location for the declarator specifier to
27060 consider. */
27062 static void
27063 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
27064 cp_decl_spec ds, cp_token *token)
27066 gcc_assert (ds < ds_last);
27068 if (decl_specs == NULL)
27069 return;
27071 source_location location = token->location;
27073 if (decl_specs->locations[ds] == 0)
27075 decl_specs->locations[ds] = location;
27076 if (ds == ds_thread)
27077 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
27079 else
27081 if (ds == ds_long)
27083 if (decl_specs->locations[ds_long_long] != 0)
27084 error_at (location,
27085 "%<long long long%> is too long for GCC");
27086 else
27088 decl_specs->locations[ds_long_long] = location;
27089 pedwarn_cxx98 (location,
27090 OPT_Wlong_long,
27091 "ISO C++ 1998 does not support %<long long%>");
27094 else if (ds == ds_thread)
27096 bool gnu = token_is__thread (token);
27097 if (gnu != decl_specs->gnu_thread_keyword_p)
27098 error_at (location,
27099 "both %<__thread%> and %<thread_local%> specified");
27100 else
27101 error_at (location, "duplicate %qD", token->u.value);
27103 else
27105 static const char *const decl_spec_names[] = {
27106 "signed",
27107 "unsigned",
27108 "short",
27109 "long",
27110 "const",
27111 "volatile",
27112 "restrict",
27113 "inline",
27114 "virtual",
27115 "explicit",
27116 "friend",
27117 "typedef",
27118 "using",
27119 "constexpr",
27120 "__complex"
27122 error_at (location,
27123 "duplicate %qs", decl_spec_names[ds]);
27128 /* Return true iff the declarator specifier DS is present in the
27129 sequence of declarator specifiers DECL_SPECS. */
27131 bool
27132 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
27133 cp_decl_spec ds)
27135 gcc_assert (ds < ds_last);
27137 if (decl_specs == NULL)
27138 return false;
27140 return decl_specs->locations[ds] != 0;
27143 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
27144 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
27146 static bool
27147 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
27149 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
27152 /* Issue an error message indicating that TOKEN_DESC was expected.
27153 If KEYWORD is true, it indicated this function is called by
27154 cp_parser_require_keword and the required token can only be
27155 a indicated keyword. */
27157 static void
27158 cp_parser_required_error (cp_parser *parser,
27159 required_token token_desc,
27160 bool keyword)
27162 switch (token_desc)
27164 case RT_NEW:
27165 cp_parser_error (parser, "expected %<new%>");
27166 return;
27167 case RT_DELETE:
27168 cp_parser_error (parser, "expected %<delete%>");
27169 return;
27170 case RT_RETURN:
27171 cp_parser_error (parser, "expected %<return%>");
27172 return;
27173 case RT_WHILE:
27174 cp_parser_error (parser, "expected %<while%>");
27175 return;
27176 case RT_EXTERN:
27177 cp_parser_error (parser, "expected %<extern%>");
27178 return;
27179 case RT_STATIC_ASSERT:
27180 cp_parser_error (parser, "expected %<static_assert%>");
27181 return;
27182 case RT_DECLTYPE:
27183 cp_parser_error (parser, "expected %<decltype%>");
27184 return;
27185 case RT_OPERATOR:
27186 cp_parser_error (parser, "expected %<operator%>");
27187 return;
27188 case RT_CLASS:
27189 cp_parser_error (parser, "expected %<class%>");
27190 return;
27191 case RT_TEMPLATE:
27192 cp_parser_error (parser, "expected %<template%>");
27193 return;
27194 case RT_NAMESPACE:
27195 cp_parser_error (parser, "expected %<namespace%>");
27196 return;
27197 case RT_USING:
27198 cp_parser_error (parser, "expected %<using%>");
27199 return;
27200 case RT_ASM:
27201 cp_parser_error (parser, "expected %<asm%>");
27202 return;
27203 case RT_TRY:
27204 cp_parser_error (parser, "expected %<try%>");
27205 return;
27206 case RT_CATCH:
27207 cp_parser_error (parser, "expected %<catch%>");
27208 return;
27209 case RT_THROW:
27210 cp_parser_error (parser, "expected %<throw%>");
27211 return;
27212 case RT_LABEL:
27213 cp_parser_error (parser, "expected %<__label__%>");
27214 return;
27215 case RT_AT_TRY:
27216 cp_parser_error (parser, "expected %<@try%>");
27217 return;
27218 case RT_AT_SYNCHRONIZED:
27219 cp_parser_error (parser, "expected %<@synchronized%>");
27220 return;
27221 case RT_AT_THROW:
27222 cp_parser_error (parser, "expected %<@throw%>");
27223 return;
27224 case RT_TRANSACTION_ATOMIC:
27225 cp_parser_error (parser, "expected %<__transaction_atomic%>");
27226 return;
27227 case RT_TRANSACTION_RELAXED:
27228 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
27229 return;
27230 default:
27231 break;
27233 if (!keyword)
27235 switch (token_desc)
27237 case RT_SEMICOLON:
27238 cp_parser_error (parser, "expected %<;%>");
27239 return;
27240 case RT_OPEN_PAREN:
27241 cp_parser_error (parser, "expected %<(%>");
27242 return;
27243 case RT_CLOSE_BRACE:
27244 cp_parser_error (parser, "expected %<}%>");
27245 return;
27246 case RT_OPEN_BRACE:
27247 cp_parser_error (parser, "expected %<{%>");
27248 return;
27249 case RT_CLOSE_SQUARE:
27250 cp_parser_error (parser, "expected %<]%>");
27251 return;
27252 case RT_OPEN_SQUARE:
27253 cp_parser_error (parser, "expected %<[%>");
27254 return;
27255 case RT_COMMA:
27256 cp_parser_error (parser, "expected %<,%>");
27257 return;
27258 case RT_SCOPE:
27259 cp_parser_error (parser, "expected %<::%>");
27260 return;
27261 case RT_LESS:
27262 cp_parser_error (parser, "expected %<<%>");
27263 return;
27264 case RT_GREATER:
27265 cp_parser_error (parser, "expected %<>%>");
27266 return;
27267 case RT_EQ:
27268 cp_parser_error (parser, "expected %<=%>");
27269 return;
27270 case RT_ELLIPSIS:
27271 cp_parser_error (parser, "expected %<...%>");
27272 return;
27273 case RT_MULT:
27274 cp_parser_error (parser, "expected %<*%>");
27275 return;
27276 case RT_COMPL:
27277 cp_parser_error (parser, "expected %<~%>");
27278 return;
27279 case RT_COLON:
27280 cp_parser_error (parser, "expected %<:%>");
27281 return;
27282 case RT_COLON_SCOPE:
27283 cp_parser_error (parser, "expected %<:%> or %<::%>");
27284 return;
27285 case RT_CLOSE_PAREN:
27286 cp_parser_error (parser, "expected %<)%>");
27287 return;
27288 case RT_COMMA_CLOSE_PAREN:
27289 cp_parser_error (parser, "expected %<,%> or %<)%>");
27290 return;
27291 case RT_PRAGMA_EOL:
27292 cp_parser_error (parser, "expected end of line");
27293 return;
27294 case RT_NAME:
27295 cp_parser_error (parser, "expected identifier");
27296 return;
27297 case RT_SELECT:
27298 cp_parser_error (parser, "expected selection-statement");
27299 return;
27300 case RT_INTERATION:
27301 cp_parser_error (parser, "expected iteration-statement");
27302 return;
27303 case RT_JUMP:
27304 cp_parser_error (parser, "expected jump-statement");
27305 return;
27306 case RT_CLASS_KEY:
27307 cp_parser_error (parser, "expected class-key");
27308 return;
27309 case RT_CLASS_TYPENAME_TEMPLATE:
27310 cp_parser_error (parser,
27311 "expected %<class%>, %<typename%>, or %<template%>");
27312 return;
27313 default:
27314 gcc_unreachable ();
27317 else
27318 gcc_unreachable ();
27323 /* If the next token is of the indicated TYPE, consume it. Otherwise,
27324 issue an error message indicating that TOKEN_DESC was expected.
27326 Returns the token consumed, if the token had the appropriate type.
27327 Otherwise, returns NULL. */
27329 static cp_token *
27330 cp_parser_require (cp_parser* parser,
27331 enum cpp_ttype type,
27332 required_token token_desc)
27334 if (cp_lexer_next_token_is (parser->lexer, type))
27335 return cp_lexer_consume_token (parser->lexer);
27336 else
27338 /* Output the MESSAGE -- unless we're parsing tentatively. */
27339 if (!cp_parser_simulate_error (parser))
27340 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
27341 return NULL;
27345 /* An error message is produced if the next token is not '>'.
27346 All further tokens are skipped until the desired token is
27347 found or '{', '}', ';' or an unbalanced ')' or ']'. */
27349 static void
27350 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
27352 /* Current level of '< ... >'. */
27353 unsigned level = 0;
27354 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
27355 unsigned nesting_depth = 0;
27357 /* Are we ready, yet? If not, issue error message. */
27358 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
27359 return;
27361 /* Skip tokens until the desired token is found. */
27362 while (true)
27364 /* Peek at the next token. */
27365 switch (cp_lexer_peek_token (parser->lexer)->type)
27367 case CPP_LESS:
27368 if (!nesting_depth)
27369 ++level;
27370 break;
27372 case CPP_RSHIFT:
27373 if (cxx_dialect == cxx98)
27374 /* C++0x views the `>>' operator as two `>' tokens, but
27375 C++98 does not. */
27376 break;
27377 else if (!nesting_depth && level-- == 0)
27379 /* We've hit a `>>' where the first `>' closes the
27380 template argument list, and the second `>' is
27381 spurious. Just consume the `>>' and stop; we've
27382 already produced at least one error. */
27383 cp_lexer_consume_token (parser->lexer);
27384 return;
27386 /* Fall through for C++0x, so we handle the second `>' in
27387 the `>>'. */
27388 gcc_fallthrough ();
27390 case CPP_GREATER:
27391 if (!nesting_depth && level-- == 0)
27393 /* We've reached the token we want, consume it and stop. */
27394 cp_lexer_consume_token (parser->lexer);
27395 return;
27397 break;
27399 case CPP_OPEN_PAREN:
27400 case CPP_OPEN_SQUARE:
27401 ++nesting_depth;
27402 break;
27404 case CPP_CLOSE_PAREN:
27405 case CPP_CLOSE_SQUARE:
27406 if (nesting_depth-- == 0)
27407 return;
27408 break;
27410 case CPP_EOF:
27411 case CPP_PRAGMA_EOL:
27412 case CPP_SEMICOLON:
27413 case CPP_OPEN_BRACE:
27414 case CPP_CLOSE_BRACE:
27415 /* The '>' was probably forgotten, don't look further. */
27416 return;
27418 default:
27419 break;
27422 /* Consume this token. */
27423 cp_lexer_consume_token (parser->lexer);
27427 /* If the next token is the indicated keyword, consume it. Otherwise,
27428 issue an error message indicating that TOKEN_DESC was expected.
27430 Returns the token consumed, if the token had the appropriate type.
27431 Otherwise, returns NULL. */
27433 static cp_token *
27434 cp_parser_require_keyword (cp_parser* parser,
27435 enum rid keyword,
27436 required_token token_desc)
27438 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
27440 if (token && token->keyword != keyword)
27442 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
27443 return NULL;
27446 return token;
27449 /* Returns TRUE iff TOKEN is a token that can begin the body of a
27450 function-definition. */
27452 static bool
27453 cp_parser_token_starts_function_definition_p (cp_token* token)
27455 return (/* An ordinary function-body begins with an `{'. */
27456 token->type == CPP_OPEN_BRACE
27457 /* A ctor-initializer begins with a `:'. */
27458 || token->type == CPP_COLON
27459 /* A function-try-block begins with `try'. */
27460 || token->keyword == RID_TRY
27461 /* A function-transaction-block begins with `__transaction_atomic'
27462 or `__transaction_relaxed'. */
27463 || token->keyword == RID_TRANSACTION_ATOMIC
27464 || token->keyword == RID_TRANSACTION_RELAXED
27465 /* The named return value extension begins with `return'. */
27466 || token->keyword == RID_RETURN);
27469 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
27470 definition. */
27472 static bool
27473 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
27475 cp_token *token;
27477 token = cp_lexer_peek_token (parser->lexer);
27478 return (token->type == CPP_OPEN_BRACE
27479 || (token->type == CPP_COLON
27480 && !parser->colon_doesnt_start_class_def_p));
27483 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
27484 C++0x) ending a template-argument. */
27486 static bool
27487 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
27489 cp_token *token;
27491 token = cp_lexer_peek_token (parser->lexer);
27492 return (token->type == CPP_COMMA
27493 || token->type == CPP_GREATER
27494 || token->type == CPP_ELLIPSIS
27495 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
27498 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
27499 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
27501 static bool
27502 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
27503 size_t n)
27505 cp_token *token;
27507 token = cp_lexer_peek_nth_token (parser->lexer, n);
27508 if (token->type == CPP_LESS)
27509 return true;
27510 /* Check for the sequence `<::' in the original code. It would be lexed as
27511 `[:', where `[' is a digraph, and there is no whitespace before
27512 `:'. */
27513 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
27515 cp_token *token2;
27516 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
27517 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
27518 return true;
27520 return false;
27523 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
27524 or none_type otherwise. */
27526 static enum tag_types
27527 cp_parser_token_is_class_key (cp_token* token)
27529 switch (token->keyword)
27531 case RID_CLASS:
27532 return class_type;
27533 case RID_STRUCT:
27534 return record_type;
27535 case RID_UNION:
27536 return union_type;
27538 default:
27539 return none_type;
27543 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
27544 or none_type otherwise or if the token is null. */
27546 static enum tag_types
27547 cp_parser_token_is_type_parameter_key (cp_token* token)
27549 if (!token)
27550 return none_type;
27552 switch (token->keyword)
27554 case RID_CLASS:
27555 return class_type;
27556 case RID_TYPENAME:
27557 return typename_type;
27559 default:
27560 return none_type;
27564 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
27566 static void
27567 cp_parser_check_class_key (enum tag_types class_key, tree type)
27569 if (type == error_mark_node)
27570 return;
27571 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
27573 if (permerror (input_location, "%qs tag used in naming %q#T",
27574 class_key == union_type ? "union"
27575 : class_key == record_type ? "struct" : "class",
27576 type))
27577 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
27578 "%q#T was previously declared here", type);
27582 /* Issue an error message if DECL is redeclared with different
27583 access than its original declaration [class.access.spec/3].
27584 This applies to nested classes, nested class templates and
27585 enumerations [class.mem/1]. */
27587 static void
27588 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
27590 if (!decl
27591 || (!CLASS_TYPE_P (TREE_TYPE (decl))
27592 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
27593 return;
27595 if ((TREE_PRIVATE (decl)
27596 != (current_access_specifier == access_private_node))
27597 || (TREE_PROTECTED (decl)
27598 != (current_access_specifier == access_protected_node)))
27599 error_at (location, "%qD redeclared with different access", decl);
27602 /* Look for the `template' keyword, as a syntactic disambiguator.
27603 Return TRUE iff it is present, in which case it will be
27604 consumed. */
27606 static bool
27607 cp_parser_optional_template_keyword (cp_parser *parser)
27609 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27611 /* In C++98 the `template' keyword can only be used within templates;
27612 outside templates the parser can always figure out what is a
27613 template and what is not. In C++11, per the resolution of DR 468,
27614 `template' is allowed in cases where it is not strictly necessary. */
27615 if (!processing_template_decl
27616 && pedantic && cxx_dialect == cxx98)
27618 cp_token *token = cp_lexer_peek_token (parser->lexer);
27619 pedwarn (token->location, OPT_Wpedantic,
27620 "in C++98 %<template%> (as a disambiguator) is only "
27621 "allowed within templates");
27622 /* If this part of the token stream is rescanned, the same
27623 error message would be generated. So, we purge the token
27624 from the stream. */
27625 cp_lexer_purge_token (parser->lexer);
27626 return false;
27628 else
27630 /* Consume the `template' keyword. */
27631 cp_lexer_consume_token (parser->lexer);
27632 return true;
27635 return false;
27638 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
27639 set PARSER->SCOPE, and perform other related actions. */
27641 static void
27642 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
27644 struct tree_check *check_value;
27646 /* Get the stored value. */
27647 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
27648 /* Set the scope from the stored value. */
27649 parser->scope = saved_checks_value (check_value);
27650 parser->qualifying_scope = check_value->qualifying_scope;
27651 parser->object_scope = NULL_TREE;
27654 /* Consume tokens up through a non-nested END token. Returns TRUE if we
27655 encounter the end of a block before what we were looking for. */
27657 static bool
27658 cp_parser_cache_group (cp_parser *parser,
27659 enum cpp_ttype end,
27660 unsigned depth)
27662 while (true)
27664 cp_token *token = cp_lexer_peek_token (parser->lexer);
27666 /* Abort a parenthesized expression if we encounter a semicolon. */
27667 if ((end == CPP_CLOSE_PAREN || depth == 0)
27668 && token->type == CPP_SEMICOLON)
27669 return true;
27670 /* If we've reached the end of the file, stop. */
27671 if (token->type == CPP_EOF
27672 || (end != CPP_PRAGMA_EOL
27673 && token->type == CPP_PRAGMA_EOL))
27674 return true;
27675 if (token->type == CPP_CLOSE_BRACE && depth == 0)
27676 /* We've hit the end of an enclosing block, so there's been some
27677 kind of syntax error. */
27678 return true;
27680 /* Consume the token. */
27681 cp_lexer_consume_token (parser->lexer);
27682 /* See if it starts a new group. */
27683 if (token->type == CPP_OPEN_BRACE)
27685 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
27686 /* In theory this should probably check end == '}', but
27687 cp_parser_save_member_function_body needs it to exit
27688 after either '}' or ')' when called with ')'. */
27689 if (depth == 0)
27690 return false;
27692 else if (token->type == CPP_OPEN_PAREN)
27694 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
27695 if (depth == 0 && end == CPP_CLOSE_PAREN)
27696 return false;
27698 else if (token->type == CPP_PRAGMA)
27699 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
27700 else if (token->type == end)
27701 return false;
27705 /* Like above, for caching a default argument or NSDMI. Both of these are
27706 terminated by a non-nested comma, but it can be unclear whether or not a
27707 comma is nested in a template argument list unless we do more parsing.
27708 In order to handle this ambiguity, when we encounter a ',' after a '<'
27709 we try to parse what follows as a parameter-declaration-list (in the
27710 case of a default argument) or a member-declarator (in the case of an
27711 NSDMI). If that succeeds, then we stop caching. */
27713 static tree
27714 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
27716 unsigned depth = 0;
27717 int maybe_template_id = 0;
27718 cp_token *first_token;
27719 cp_token *token;
27720 tree default_argument;
27722 /* Add tokens until we have processed the entire default
27723 argument. We add the range [first_token, token). */
27724 first_token = cp_lexer_peek_token (parser->lexer);
27725 if (first_token->type == CPP_OPEN_BRACE)
27727 /* For list-initialization, this is straightforward. */
27728 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27729 token = cp_lexer_peek_token (parser->lexer);
27731 else while (true)
27733 bool done = false;
27735 /* Peek at the next token. */
27736 token = cp_lexer_peek_token (parser->lexer);
27737 /* What we do depends on what token we have. */
27738 switch (token->type)
27740 /* In valid code, a default argument must be
27741 immediately followed by a `,' `)', or `...'. */
27742 case CPP_COMMA:
27743 if (depth == 0 && maybe_template_id)
27745 /* If we've seen a '<', we might be in a
27746 template-argument-list. Until Core issue 325 is
27747 resolved, we don't know how this situation ought
27748 to be handled, so try to DTRT. We check whether
27749 what comes after the comma is a valid parameter
27750 declaration list. If it is, then the comma ends
27751 the default argument; otherwise the default
27752 argument continues. */
27753 bool error = false;
27754 cp_token *peek;
27756 /* Set ITALP so cp_parser_parameter_declaration_list
27757 doesn't decide to commit to this parse. */
27758 bool saved_italp = parser->in_template_argument_list_p;
27759 parser->in_template_argument_list_p = true;
27761 cp_parser_parse_tentatively (parser);
27763 if (nsdmi)
27765 /* Parse declarators until we reach a non-comma or
27766 somthing that cannot be an initializer.
27767 Just checking whether we're looking at a single
27768 declarator is insufficient. Consider:
27769 int var = tuple<T,U>::x;
27770 The template parameter 'U' looks exactly like a
27771 declarator. */
27774 int ctor_dtor_or_conv_p;
27775 cp_lexer_consume_token (parser->lexer);
27776 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27777 &ctor_dtor_or_conv_p,
27778 /*parenthesized_p=*/NULL,
27779 /*member_p=*/true,
27780 /*friend_p=*/false);
27781 peek = cp_lexer_peek_token (parser->lexer);
27782 if (cp_parser_error_occurred (parser))
27783 break;
27785 while (peek->type == CPP_COMMA);
27786 /* If we met an '=' or ';' then the original comma
27787 was the end of the NSDMI. Otherwise assume
27788 we're still in the NSDMI. */
27789 error = (peek->type != CPP_EQ
27790 && peek->type != CPP_SEMICOLON);
27792 else
27794 cp_lexer_consume_token (parser->lexer);
27795 begin_scope (sk_function_parms, NULL_TREE);
27796 cp_parser_parameter_declaration_list (parser, &error);
27797 pop_bindings_and_leave_scope ();
27799 if (!cp_parser_error_occurred (parser) && !error)
27800 done = true;
27801 cp_parser_abort_tentative_parse (parser);
27803 parser->in_template_argument_list_p = saved_italp;
27804 break;
27806 /* XXX Really fallthru? */
27807 /* FALLTHRU */
27808 case CPP_CLOSE_PAREN:
27809 case CPP_ELLIPSIS:
27810 /* If we run into a non-nested `;', `}', or `]',
27811 then the code is invalid -- but the default
27812 argument is certainly over. */
27813 case CPP_SEMICOLON:
27814 case CPP_CLOSE_BRACE:
27815 case CPP_CLOSE_SQUARE:
27816 if (depth == 0
27817 /* Handle correctly int n = sizeof ... ( p ); */
27818 && token->type != CPP_ELLIPSIS)
27819 done = true;
27820 /* Update DEPTH, if necessary. */
27821 else if (token->type == CPP_CLOSE_PAREN
27822 || token->type == CPP_CLOSE_BRACE
27823 || token->type == CPP_CLOSE_SQUARE)
27824 --depth;
27825 break;
27827 case CPP_OPEN_PAREN:
27828 case CPP_OPEN_SQUARE:
27829 case CPP_OPEN_BRACE:
27830 ++depth;
27831 break;
27833 case CPP_LESS:
27834 if (depth == 0)
27835 /* This might be the comparison operator, or it might
27836 start a template argument list. */
27837 ++maybe_template_id;
27838 break;
27840 case CPP_RSHIFT:
27841 if (cxx_dialect == cxx98)
27842 break;
27843 /* Fall through for C++0x, which treats the `>>'
27844 operator like two `>' tokens in certain
27845 cases. */
27846 gcc_fallthrough ();
27848 case CPP_GREATER:
27849 if (depth == 0)
27851 /* This might be an operator, or it might close a
27852 template argument list. But if a previous '<'
27853 started a template argument list, this will have
27854 closed it, so we can't be in one anymore. */
27855 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
27856 if (maybe_template_id < 0)
27857 maybe_template_id = 0;
27859 break;
27861 /* If we run out of tokens, issue an error message. */
27862 case CPP_EOF:
27863 case CPP_PRAGMA_EOL:
27864 error_at (token->location, "file ends in default argument");
27865 return error_mark_node;
27867 case CPP_NAME:
27868 case CPP_SCOPE:
27869 /* In these cases, we should look for template-ids.
27870 For example, if the default argument is
27871 `X<int, double>()', we need to do name lookup to
27872 figure out whether or not `X' is a template; if
27873 so, the `,' does not end the default argument.
27875 That is not yet done. */
27876 break;
27878 default:
27879 break;
27882 /* If we've reached the end, stop. */
27883 if (done)
27884 break;
27886 /* Add the token to the token block. */
27887 token = cp_lexer_consume_token (parser->lexer);
27890 /* Create a DEFAULT_ARG to represent the unparsed default
27891 argument. */
27892 default_argument = make_node (DEFAULT_ARG);
27893 DEFARG_TOKENS (default_argument)
27894 = cp_token_cache_new (first_token, token);
27895 DEFARG_INSTANTIATIONS (default_argument) = NULL;
27897 return default_argument;
27900 /* Begin parsing tentatively. We always save tokens while parsing
27901 tentatively so that if the tentative parsing fails we can restore the
27902 tokens. */
27904 static void
27905 cp_parser_parse_tentatively (cp_parser* parser)
27907 /* Enter a new parsing context. */
27908 parser->context = cp_parser_context_new (parser->context);
27909 /* Begin saving tokens. */
27910 cp_lexer_save_tokens (parser->lexer);
27911 /* In order to avoid repetitive access control error messages,
27912 access checks are queued up until we are no longer parsing
27913 tentatively. */
27914 push_deferring_access_checks (dk_deferred);
27917 /* Commit to the currently active tentative parse. */
27919 static void
27920 cp_parser_commit_to_tentative_parse (cp_parser* parser)
27922 cp_parser_context *context;
27923 cp_lexer *lexer;
27925 /* Mark all of the levels as committed. */
27926 lexer = parser->lexer;
27927 for (context = parser->context; context->next; context = context->next)
27929 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
27930 break;
27931 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
27932 while (!cp_lexer_saving_tokens (lexer))
27933 lexer = lexer->next;
27934 cp_lexer_commit_tokens (lexer);
27938 /* Commit to the topmost currently active tentative parse.
27940 Note that this function shouldn't be called when there are
27941 irreversible side-effects while in a tentative state. For
27942 example, we shouldn't create a permanent entry in the symbol
27943 table, or issue an error message that might not apply if the
27944 tentative parse is aborted. */
27946 static void
27947 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
27949 cp_parser_context *context = parser->context;
27950 cp_lexer *lexer = parser->lexer;
27952 if (context)
27954 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
27955 return;
27956 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
27958 while (!cp_lexer_saving_tokens (lexer))
27959 lexer = lexer->next;
27960 cp_lexer_commit_tokens (lexer);
27964 /* Abort the currently active tentative parse. All consumed tokens
27965 will be rolled back, and no diagnostics will be issued. */
27967 static void
27968 cp_parser_abort_tentative_parse (cp_parser* parser)
27970 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
27971 || errorcount > 0);
27972 cp_parser_simulate_error (parser);
27973 /* Now, pretend that we want to see if the construct was
27974 successfully parsed. */
27975 cp_parser_parse_definitely (parser);
27978 /* Stop parsing tentatively. If a parse error has occurred, restore the
27979 token stream. Otherwise, commit to the tokens we have consumed.
27980 Returns true if no error occurred; false otherwise. */
27982 static bool
27983 cp_parser_parse_definitely (cp_parser* parser)
27985 bool error_occurred;
27986 cp_parser_context *context;
27988 /* Remember whether or not an error occurred, since we are about to
27989 destroy that information. */
27990 error_occurred = cp_parser_error_occurred (parser);
27991 /* Remove the topmost context from the stack. */
27992 context = parser->context;
27993 parser->context = context->next;
27994 /* If no parse errors occurred, commit to the tentative parse. */
27995 if (!error_occurred)
27997 /* Commit to the tokens read tentatively, unless that was
27998 already done. */
27999 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
28000 cp_lexer_commit_tokens (parser->lexer);
28002 pop_to_parent_deferring_access_checks ();
28004 /* Otherwise, if errors occurred, roll back our state so that things
28005 are just as they were before we began the tentative parse. */
28006 else
28008 cp_lexer_rollback_tokens (parser->lexer);
28009 pop_deferring_access_checks ();
28011 /* Add the context to the front of the free list. */
28012 context->next = cp_parser_context_free_list;
28013 cp_parser_context_free_list = context;
28015 return !error_occurred;
28018 /* Returns true if we are parsing tentatively and are not committed to
28019 this tentative parse. */
28021 static bool
28022 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
28024 return (cp_parser_parsing_tentatively (parser)
28025 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
28028 /* Returns nonzero iff an error has occurred during the most recent
28029 tentative parse. */
28031 static bool
28032 cp_parser_error_occurred (cp_parser* parser)
28034 return (cp_parser_parsing_tentatively (parser)
28035 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
28038 /* Returns nonzero if GNU extensions are allowed. */
28040 static bool
28041 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
28043 return parser->allow_gnu_extensions_p;
28046 /* Objective-C++ Productions */
28049 /* Parse an Objective-C expression, which feeds into a primary-expression
28050 above.
28052 objc-expression:
28053 objc-message-expression
28054 objc-string-literal
28055 objc-encode-expression
28056 objc-protocol-expression
28057 objc-selector-expression
28059 Returns a tree representation of the expression. */
28061 static cp_expr
28062 cp_parser_objc_expression (cp_parser* parser)
28064 /* Try to figure out what kind of declaration is present. */
28065 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
28067 switch (kwd->type)
28069 case CPP_OPEN_SQUARE:
28070 return cp_parser_objc_message_expression (parser);
28072 case CPP_OBJC_STRING:
28073 kwd = cp_lexer_consume_token (parser->lexer);
28074 return objc_build_string_object (kwd->u.value);
28076 case CPP_KEYWORD:
28077 switch (kwd->keyword)
28079 case RID_AT_ENCODE:
28080 return cp_parser_objc_encode_expression (parser);
28082 case RID_AT_PROTOCOL:
28083 return cp_parser_objc_protocol_expression (parser);
28085 case RID_AT_SELECTOR:
28086 return cp_parser_objc_selector_expression (parser);
28088 default:
28089 break;
28091 default:
28092 error_at (kwd->location,
28093 "misplaced %<@%D%> Objective-C++ construct",
28094 kwd->u.value);
28095 cp_parser_skip_to_end_of_block_or_statement (parser);
28098 return error_mark_node;
28101 /* Parse an Objective-C message expression.
28103 objc-message-expression:
28104 [ objc-message-receiver objc-message-args ]
28106 Returns a representation of an Objective-C message. */
28108 static tree
28109 cp_parser_objc_message_expression (cp_parser* parser)
28111 tree receiver, messageargs;
28113 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28114 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
28115 receiver = cp_parser_objc_message_receiver (parser);
28116 messageargs = cp_parser_objc_message_args (parser);
28117 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
28118 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
28120 tree result = objc_build_message_expr (receiver, messageargs);
28122 /* Construct a location e.g.
28123 [self func1:5]
28124 ^~~~~~~~~~~~~~
28125 ranging from the '[' to the ']', with the caret at the start. */
28126 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
28127 protected_set_expr_location (result, combined_loc);
28129 return result;
28132 /* Parse an objc-message-receiver.
28134 objc-message-receiver:
28135 expression
28136 simple-type-specifier
28138 Returns a representation of the type or expression. */
28140 static tree
28141 cp_parser_objc_message_receiver (cp_parser* parser)
28143 tree rcv;
28145 /* An Objective-C message receiver may be either (1) a type
28146 or (2) an expression. */
28147 cp_parser_parse_tentatively (parser);
28148 rcv = cp_parser_expression (parser);
28150 /* If that worked out, fine. */
28151 if (cp_parser_parse_definitely (parser))
28152 return rcv;
28154 cp_parser_parse_tentatively (parser);
28155 rcv = cp_parser_simple_type_specifier (parser,
28156 /*decl_specs=*/NULL,
28157 CP_PARSER_FLAGS_NONE);
28159 if (cp_parser_parse_definitely (parser))
28160 return objc_get_class_reference (rcv);
28162 cp_parser_error (parser, "objective-c++ message receiver expected");
28163 return error_mark_node;
28166 /* Parse the arguments and selectors comprising an Objective-C message.
28168 objc-message-args:
28169 objc-selector
28170 objc-selector-args
28171 objc-selector-args , objc-comma-args
28173 objc-selector-args:
28174 objc-selector [opt] : assignment-expression
28175 objc-selector-args objc-selector [opt] : assignment-expression
28177 objc-comma-args:
28178 assignment-expression
28179 objc-comma-args , assignment-expression
28181 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
28182 selector arguments and TREE_VALUE containing a list of comma
28183 arguments. */
28185 static tree
28186 cp_parser_objc_message_args (cp_parser* parser)
28188 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
28189 bool maybe_unary_selector_p = true;
28190 cp_token *token = cp_lexer_peek_token (parser->lexer);
28192 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
28194 tree selector = NULL_TREE, arg;
28196 if (token->type != CPP_COLON)
28197 selector = cp_parser_objc_selector (parser);
28199 /* Detect if we have a unary selector. */
28200 if (maybe_unary_selector_p
28201 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28202 return build_tree_list (selector, NULL_TREE);
28204 maybe_unary_selector_p = false;
28205 cp_parser_require (parser, CPP_COLON, RT_COLON);
28206 arg = cp_parser_assignment_expression (parser);
28208 sel_args
28209 = chainon (sel_args,
28210 build_tree_list (selector, arg));
28212 token = cp_lexer_peek_token (parser->lexer);
28215 /* Handle non-selector arguments, if any. */
28216 while (token->type == CPP_COMMA)
28218 tree arg;
28220 cp_lexer_consume_token (parser->lexer);
28221 arg = cp_parser_assignment_expression (parser);
28223 addl_args
28224 = chainon (addl_args,
28225 build_tree_list (NULL_TREE, arg));
28227 token = cp_lexer_peek_token (parser->lexer);
28230 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
28232 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
28233 return build_tree_list (error_mark_node, error_mark_node);
28236 return build_tree_list (sel_args, addl_args);
28239 /* Parse an Objective-C encode expression.
28241 objc-encode-expression:
28242 @encode objc-typename
28244 Returns an encoded representation of the type argument. */
28246 static cp_expr
28247 cp_parser_objc_encode_expression (cp_parser* parser)
28249 tree type;
28250 cp_token *token;
28251 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28253 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
28254 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28255 token = cp_lexer_peek_token (parser->lexer);
28256 type = complete_type (cp_parser_type_id (parser));
28257 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28259 if (!type)
28261 error_at (token->location,
28262 "%<@encode%> must specify a type as an argument");
28263 return error_mark_node;
28266 /* This happens if we find @encode(T) (where T is a template
28267 typename or something dependent on a template typename) when
28268 parsing a template. In that case, we can't compile it
28269 immediately, but we rather create an AT_ENCODE_EXPR which will
28270 need to be instantiated when the template is used.
28272 if (dependent_type_p (type))
28274 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
28275 TREE_READONLY (value) = 1;
28276 return value;
28280 /* Build a location of the form:
28281 @encode(int)
28282 ^~~~~~~~~~~~
28283 with caret==start at the @ token, finishing at the close paren. */
28284 location_t combined_loc
28285 = make_location (start_loc, start_loc,
28286 cp_lexer_previous_token (parser->lexer)->location);
28288 return cp_expr (objc_build_encode_expr (type), combined_loc);
28291 /* Parse an Objective-C @defs expression. */
28293 static tree
28294 cp_parser_objc_defs_expression (cp_parser *parser)
28296 tree name;
28298 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
28299 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28300 name = cp_parser_identifier (parser);
28301 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28303 return objc_get_class_ivars (name);
28306 /* Parse an Objective-C protocol expression.
28308 objc-protocol-expression:
28309 @protocol ( identifier )
28311 Returns a representation of the protocol expression. */
28313 static tree
28314 cp_parser_objc_protocol_expression (cp_parser* parser)
28316 tree proto;
28317 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28319 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
28320 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28321 proto = cp_parser_identifier (parser);
28322 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28324 /* Build a location of the form:
28325 @protocol(prot)
28326 ^~~~~~~~~~~~~~~
28327 with caret==start at the @ token, finishing at the close paren. */
28328 location_t combined_loc
28329 = make_location (start_loc, start_loc,
28330 cp_lexer_previous_token (parser->lexer)->location);
28331 tree result = objc_build_protocol_expr (proto);
28332 protected_set_expr_location (result, combined_loc);
28333 return result;
28336 /* Parse an Objective-C selector expression.
28338 objc-selector-expression:
28339 @selector ( objc-method-signature )
28341 objc-method-signature:
28342 objc-selector
28343 objc-selector-seq
28345 objc-selector-seq:
28346 objc-selector :
28347 objc-selector-seq objc-selector :
28349 Returns a representation of the method selector. */
28351 static tree
28352 cp_parser_objc_selector_expression (cp_parser* parser)
28354 tree sel_seq = NULL_TREE;
28355 bool maybe_unary_selector_p = true;
28356 cp_token *token;
28357 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28359 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
28360 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28361 token = cp_lexer_peek_token (parser->lexer);
28363 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
28364 || token->type == CPP_SCOPE)
28366 tree selector = NULL_TREE;
28368 if (token->type != CPP_COLON
28369 || token->type == CPP_SCOPE)
28370 selector = cp_parser_objc_selector (parser);
28372 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
28373 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
28375 /* Detect if we have a unary selector. */
28376 if (maybe_unary_selector_p)
28378 sel_seq = selector;
28379 goto finish_selector;
28381 else
28383 cp_parser_error (parser, "expected %<:%>");
28386 maybe_unary_selector_p = false;
28387 token = cp_lexer_consume_token (parser->lexer);
28389 if (token->type == CPP_SCOPE)
28391 sel_seq
28392 = chainon (sel_seq,
28393 build_tree_list (selector, NULL_TREE));
28394 sel_seq
28395 = chainon (sel_seq,
28396 build_tree_list (NULL_TREE, NULL_TREE));
28398 else
28399 sel_seq
28400 = chainon (sel_seq,
28401 build_tree_list (selector, NULL_TREE));
28403 token = cp_lexer_peek_token (parser->lexer);
28406 finish_selector:
28407 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28410 /* Build a location of the form:
28411 @selector(func)
28412 ^~~~~~~~~~~~~~~
28413 with caret==start at the @ token, finishing at the close paren. */
28414 location_t combined_loc
28415 = make_location (loc, loc,
28416 cp_lexer_previous_token (parser->lexer)->location);
28417 tree result = objc_build_selector_expr (combined_loc, sel_seq);
28418 /* TODO: objc_build_selector_expr doesn't always honor the location. */
28419 protected_set_expr_location (result, combined_loc);
28420 return result;
28423 /* Parse a list of identifiers.
28425 objc-identifier-list:
28426 identifier
28427 objc-identifier-list , identifier
28429 Returns a TREE_LIST of identifier nodes. */
28431 static tree
28432 cp_parser_objc_identifier_list (cp_parser* parser)
28434 tree identifier;
28435 tree list;
28436 cp_token *sep;
28438 identifier = cp_parser_identifier (parser);
28439 if (identifier == error_mark_node)
28440 return error_mark_node;
28442 list = build_tree_list (NULL_TREE, identifier);
28443 sep = cp_lexer_peek_token (parser->lexer);
28445 while (sep->type == CPP_COMMA)
28447 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28448 identifier = cp_parser_identifier (parser);
28449 if (identifier == error_mark_node)
28450 return list;
28452 list = chainon (list, build_tree_list (NULL_TREE,
28453 identifier));
28454 sep = cp_lexer_peek_token (parser->lexer);
28457 return list;
28460 /* Parse an Objective-C alias declaration.
28462 objc-alias-declaration:
28463 @compatibility_alias identifier identifier ;
28465 This function registers the alias mapping with the Objective-C front end.
28466 It returns nothing. */
28468 static void
28469 cp_parser_objc_alias_declaration (cp_parser* parser)
28471 tree alias, orig;
28473 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
28474 alias = cp_parser_identifier (parser);
28475 orig = cp_parser_identifier (parser);
28476 objc_declare_alias (alias, orig);
28477 cp_parser_consume_semicolon_at_end_of_statement (parser);
28480 /* Parse an Objective-C class forward-declaration.
28482 objc-class-declaration:
28483 @class objc-identifier-list ;
28485 The function registers the forward declarations with the Objective-C
28486 front end. It returns nothing. */
28488 static void
28489 cp_parser_objc_class_declaration (cp_parser* parser)
28491 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
28492 while (true)
28494 tree id;
28496 id = cp_parser_identifier (parser);
28497 if (id == error_mark_node)
28498 break;
28500 objc_declare_class (id);
28502 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28503 cp_lexer_consume_token (parser->lexer);
28504 else
28505 break;
28507 cp_parser_consume_semicolon_at_end_of_statement (parser);
28510 /* Parse a list of Objective-C protocol references.
28512 objc-protocol-refs-opt:
28513 objc-protocol-refs [opt]
28515 objc-protocol-refs:
28516 < objc-identifier-list >
28518 Returns a TREE_LIST of identifiers, if any. */
28520 static tree
28521 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
28523 tree protorefs = NULL_TREE;
28525 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
28527 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
28528 protorefs = cp_parser_objc_identifier_list (parser);
28529 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
28532 return protorefs;
28535 /* Parse a Objective-C visibility specification. */
28537 static void
28538 cp_parser_objc_visibility_spec (cp_parser* parser)
28540 cp_token *vis = cp_lexer_peek_token (parser->lexer);
28542 switch (vis->keyword)
28544 case RID_AT_PRIVATE:
28545 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
28546 break;
28547 case RID_AT_PROTECTED:
28548 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
28549 break;
28550 case RID_AT_PUBLIC:
28551 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
28552 break;
28553 case RID_AT_PACKAGE:
28554 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
28555 break;
28556 default:
28557 return;
28560 /* Eat '@private'/'@protected'/'@public'. */
28561 cp_lexer_consume_token (parser->lexer);
28564 /* Parse an Objective-C method type. Return 'true' if it is a class
28565 (+) method, and 'false' if it is an instance (-) method. */
28567 static inline bool
28568 cp_parser_objc_method_type (cp_parser* parser)
28570 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
28571 return true;
28572 else
28573 return false;
28576 /* Parse an Objective-C protocol qualifier. */
28578 static tree
28579 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
28581 tree quals = NULL_TREE, node;
28582 cp_token *token = cp_lexer_peek_token (parser->lexer);
28584 node = token->u.value;
28586 while (node && identifier_p (node)
28587 && (node == ridpointers [(int) RID_IN]
28588 || node == ridpointers [(int) RID_OUT]
28589 || node == ridpointers [(int) RID_INOUT]
28590 || node == ridpointers [(int) RID_BYCOPY]
28591 || node == ridpointers [(int) RID_BYREF]
28592 || node == ridpointers [(int) RID_ONEWAY]))
28594 quals = tree_cons (NULL_TREE, node, quals);
28595 cp_lexer_consume_token (parser->lexer);
28596 token = cp_lexer_peek_token (parser->lexer);
28597 node = token->u.value;
28600 return quals;
28603 /* Parse an Objective-C typename. */
28605 static tree
28606 cp_parser_objc_typename (cp_parser* parser)
28608 tree type_name = NULL_TREE;
28610 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28612 tree proto_quals, cp_type = NULL_TREE;
28614 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
28615 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
28617 /* An ObjC type name may consist of just protocol qualifiers, in which
28618 case the type shall default to 'id'. */
28619 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
28621 cp_type = cp_parser_type_id (parser);
28623 /* If the type could not be parsed, an error has already
28624 been produced. For error recovery, behave as if it had
28625 not been specified, which will use the default type
28626 'id'. */
28627 if (cp_type == error_mark_node)
28629 cp_type = NULL_TREE;
28630 /* We need to skip to the closing parenthesis as
28631 cp_parser_type_id() does not seem to do it for
28632 us. */
28633 cp_parser_skip_to_closing_parenthesis (parser,
28634 /*recovering=*/true,
28635 /*or_comma=*/false,
28636 /*consume_paren=*/false);
28640 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28641 type_name = build_tree_list (proto_quals, cp_type);
28644 return type_name;
28647 /* Check to see if TYPE refers to an Objective-C selector name. */
28649 static bool
28650 cp_parser_objc_selector_p (enum cpp_ttype type)
28652 return (type == CPP_NAME || type == CPP_KEYWORD
28653 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
28654 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
28655 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
28656 || type == CPP_XOR || type == CPP_XOR_EQ);
28659 /* Parse an Objective-C selector. */
28661 static tree
28662 cp_parser_objc_selector (cp_parser* parser)
28664 cp_token *token = cp_lexer_consume_token (parser->lexer);
28666 if (!cp_parser_objc_selector_p (token->type))
28668 error_at (token->location, "invalid Objective-C++ selector name");
28669 return error_mark_node;
28672 /* C++ operator names are allowed to appear in ObjC selectors. */
28673 switch (token->type)
28675 case CPP_AND_AND: return get_identifier ("and");
28676 case CPP_AND_EQ: return get_identifier ("and_eq");
28677 case CPP_AND: return get_identifier ("bitand");
28678 case CPP_OR: return get_identifier ("bitor");
28679 case CPP_COMPL: return get_identifier ("compl");
28680 case CPP_NOT: return get_identifier ("not");
28681 case CPP_NOT_EQ: return get_identifier ("not_eq");
28682 case CPP_OR_OR: return get_identifier ("or");
28683 case CPP_OR_EQ: return get_identifier ("or_eq");
28684 case CPP_XOR: return get_identifier ("xor");
28685 case CPP_XOR_EQ: return get_identifier ("xor_eq");
28686 default: return token->u.value;
28690 /* Parse an Objective-C params list. */
28692 static tree
28693 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
28695 tree params = NULL_TREE;
28696 bool maybe_unary_selector_p = true;
28697 cp_token *token = cp_lexer_peek_token (parser->lexer);
28699 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
28701 tree selector = NULL_TREE, type_name, identifier;
28702 tree parm_attr = NULL_TREE;
28704 if (token->keyword == RID_ATTRIBUTE)
28705 break;
28707 if (token->type != CPP_COLON)
28708 selector = cp_parser_objc_selector (parser);
28710 /* Detect if we have a unary selector. */
28711 if (maybe_unary_selector_p
28712 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28714 params = selector; /* Might be followed by attributes. */
28715 break;
28718 maybe_unary_selector_p = false;
28719 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28721 /* Something went quite wrong. There should be a colon
28722 here, but there is not. Stop parsing parameters. */
28723 break;
28725 type_name = cp_parser_objc_typename (parser);
28726 /* New ObjC allows attributes on parameters too. */
28727 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
28728 parm_attr = cp_parser_attributes_opt (parser);
28729 identifier = cp_parser_identifier (parser);
28731 params
28732 = chainon (params,
28733 objc_build_keyword_decl (selector,
28734 type_name,
28735 identifier,
28736 parm_attr));
28738 token = cp_lexer_peek_token (parser->lexer);
28741 if (params == NULL_TREE)
28743 cp_parser_error (parser, "objective-c++ method declaration is expected");
28744 return error_mark_node;
28747 /* We allow tail attributes for the method. */
28748 if (token->keyword == RID_ATTRIBUTE)
28750 *attributes = cp_parser_attributes_opt (parser);
28751 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28752 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28753 return params;
28754 cp_parser_error (parser,
28755 "method attributes must be specified at the end");
28756 return error_mark_node;
28759 if (params == NULL_TREE)
28761 cp_parser_error (parser, "objective-c++ method declaration is expected");
28762 return error_mark_node;
28764 return params;
28767 /* Parse the non-keyword Objective-C params. */
28769 static tree
28770 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
28771 tree* attributes)
28773 tree params = make_node (TREE_LIST);
28774 cp_token *token = cp_lexer_peek_token (parser->lexer);
28775 *ellipsisp = false; /* Initially, assume no ellipsis. */
28777 while (token->type == CPP_COMMA)
28779 cp_parameter_declarator *parmdecl;
28780 tree parm;
28782 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28783 token = cp_lexer_peek_token (parser->lexer);
28785 if (token->type == CPP_ELLIPSIS)
28787 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
28788 *ellipsisp = true;
28789 token = cp_lexer_peek_token (parser->lexer);
28790 break;
28793 /* TODO: parse attributes for tail parameters. */
28794 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
28795 parm = grokdeclarator (parmdecl->declarator,
28796 &parmdecl->decl_specifiers,
28797 PARM, /*initialized=*/0,
28798 /*attrlist=*/NULL);
28800 chainon (params, build_tree_list (NULL_TREE, parm));
28801 token = cp_lexer_peek_token (parser->lexer);
28804 /* We allow tail attributes for the method. */
28805 if (token->keyword == RID_ATTRIBUTE)
28807 if (*attributes == NULL_TREE)
28809 *attributes = cp_parser_attributes_opt (parser);
28810 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28811 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28812 return params;
28814 else
28815 /* We have an error, but parse the attributes, so that we can
28816 carry on. */
28817 *attributes = cp_parser_attributes_opt (parser);
28819 cp_parser_error (parser,
28820 "method attributes must be specified at the end");
28821 return error_mark_node;
28824 return params;
28827 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
28829 static void
28830 cp_parser_objc_interstitial_code (cp_parser* parser)
28832 cp_token *token = cp_lexer_peek_token (parser->lexer);
28834 /* If the next token is `extern' and the following token is a string
28835 literal, then we have a linkage specification. */
28836 if (token->keyword == RID_EXTERN
28837 && cp_parser_is_pure_string_literal
28838 (cp_lexer_peek_nth_token (parser->lexer, 2)))
28839 cp_parser_linkage_specification (parser);
28840 /* Handle #pragma, if any. */
28841 else if (token->type == CPP_PRAGMA)
28842 cp_parser_pragma (parser, pragma_objc_icode, NULL);
28843 /* Allow stray semicolons. */
28844 else if (token->type == CPP_SEMICOLON)
28845 cp_lexer_consume_token (parser->lexer);
28846 /* Mark methods as optional or required, when building protocols. */
28847 else if (token->keyword == RID_AT_OPTIONAL)
28849 cp_lexer_consume_token (parser->lexer);
28850 objc_set_method_opt (true);
28852 else if (token->keyword == RID_AT_REQUIRED)
28854 cp_lexer_consume_token (parser->lexer);
28855 objc_set_method_opt (false);
28857 else if (token->keyword == RID_NAMESPACE)
28858 cp_parser_namespace_definition (parser);
28859 /* Other stray characters must generate errors. */
28860 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
28862 cp_lexer_consume_token (parser->lexer);
28863 error ("stray %qs between Objective-C++ methods",
28864 token->type == CPP_OPEN_BRACE ? "{" : "}");
28866 /* Finally, try to parse a block-declaration, or a function-definition. */
28867 else
28868 cp_parser_block_declaration (parser, /*statement_p=*/false);
28871 /* Parse a method signature. */
28873 static tree
28874 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
28876 tree rettype, kwdparms, optparms;
28877 bool ellipsis = false;
28878 bool is_class_method;
28880 is_class_method = cp_parser_objc_method_type (parser);
28881 rettype = cp_parser_objc_typename (parser);
28882 *attributes = NULL_TREE;
28883 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
28884 if (kwdparms == error_mark_node)
28885 return error_mark_node;
28886 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
28887 if (optparms == error_mark_node)
28888 return error_mark_node;
28890 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
28893 static bool
28894 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
28896 tree tattr;
28897 cp_lexer_save_tokens (parser->lexer);
28898 tattr = cp_parser_attributes_opt (parser);
28899 gcc_assert (tattr) ;
28901 /* If the attributes are followed by a method introducer, this is not allowed.
28902 Dump the attributes and flag the situation. */
28903 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
28904 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
28905 return true;
28907 /* Otherwise, the attributes introduce some interstitial code, possibly so
28908 rewind to allow that check. */
28909 cp_lexer_rollback_tokens (parser->lexer);
28910 return false;
28913 /* Parse an Objective-C method prototype list. */
28915 static void
28916 cp_parser_objc_method_prototype_list (cp_parser* parser)
28918 cp_token *token = cp_lexer_peek_token (parser->lexer);
28920 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
28922 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
28924 tree attributes, sig;
28925 bool is_class_method;
28926 if (token->type == CPP_PLUS)
28927 is_class_method = true;
28928 else
28929 is_class_method = false;
28930 sig = cp_parser_objc_method_signature (parser, &attributes);
28931 if (sig == error_mark_node)
28933 cp_parser_skip_to_end_of_block_or_statement (parser);
28934 token = cp_lexer_peek_token (parser->lexer);
28935 continue;
28937 objc_add_method_declaration (is_class_method, sig, attributes);
28938 cp_parser_consume_semicolon_at_end_of_statement (parser);
28940 else if (token->keyword == RID_AT_PROPERTY)
28941 cp_parser_objc_at_property_declaration (parser);
28942 else if (token->keyword == RID_ATTRIBUTE
28943 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
28944 warning_at (cp_lexer_peek_token (parser->lexer)->location,
28945 OPT_Wattributes,
28946 "prefix attributes are ignored for methods");
28947 else
28948 /* Allow for interspersed non-ObjC++ code. */
28949 cp_parser_objc_interstitial_code (parser);
28951 token = cp_lexer_peek_token (parser->lexer);
28954 if (token->type != CPP_EOF)
28955 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
28956 else
28957 cp_parser_error (parser, "expected %<@end%>");
28959 objc_finish_interface ();
28962 /* Parse an Objective-C method definition list. */
28964 static void
28965 cp_parser_objc_method_definition_list (cp_parser* parser)
28967 cp_token *token = cp_lexer_peek_token (parser->lexer);
28969 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
28971 tree meth;
28973 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
28975 cp_token *ptk;
28976 tree sig, attribute;
28977 bool is_class_method;
28978 if (token->type == CPP_PLUS)
28979 is_class_method = true;
28980 else
28981 is_class_method = false;
28982 push_deferring_access_checks (dk_deferred);
28983 sig = cp_parser_objc_method_signature (parser, &attribute);
28984 if (sig == error_mark_node)
28986 cp_parser_skip_to_end_of_block_or_statement (parser);
28987 token = cp_lexer_peek_token (parser->lexer);
28988 continue;
28990 objc_start_method_definition (is_class_method, sig, attribute,
28991 NULL_TREE);
28993 /* For historical reasons, we accept an optional semicolon. */
28994 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28995 cp_lexer_consume_token (parser->lexer);
28997 ptk = cp_lexer_peek_token (parser->lexer);
28998 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
28999 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
29001 perform_deferred_access_checks (tf_warning_or_error);
29002 stop_deferring_access_checks ();
29003 meth = cp_parser_function_definition_after_declarator (parser,
29004 false);
29005 pop_deferring_access_checks ();
29006 objc_finish_method_definition (meth);
29009 /* The following case will be removed once @synthesize is
29010 completely implemented. */
29011 else if (token->keyword == RID_AT_PROPERTY)
29012 cp_parser_objc_at_property_declaration (parser);
29013 else if (token->keyword == RID_AT_SYNTHESIZE)
29014 cp_parser_objc_at_synthesize_declaration (parser);
29015 else if (token->keyword == RID_AT_DYNAMIC)
29016 cp_parser_objc_at_dynamic_declaration (parser);
29017 else if (token->keyword == RID_ATTRIBUTE
29018 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29019 warning_at (token->location, OPT_Wattributes,
29020 "prefix attributes are ignored for methods");
29021 else
29022 /* Allow for interspersed non-ObjC++ code. */
29023 cp_parser_objc_interstitial_code (parser);
29025 token = cp_lexer_peek_token (parser->lexer);
29028 if (token->type != CPP_EOF)
29029 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29030 else
29031 cp_parser_error (parser, "expected %<@end%>");
29033 objc_finish_implementation ();
29036 /* Parse Objective-C ivars. */
29038 static void
29039 cp_parser_objc_class_ivars (cp_parser* parser)
29041 cp_token *token = cp_lexer_peek_token (parser->lexer);
29043 if (token->type != CPP_OPEN_BRACE)
29044 return; /* No ivars specified. */
29046 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
29047 token = cp_lexer_peek_token (parser->lexer);
29049 while (token->type != CPP_CLOSE_BRACE
29050 && token->keyword != RID_AT_END && token->type != CPP_EOF)
29052 cp_decl_specifier_seq declspecs;
29053 int decl_class_or_enum_p;
29054 tree prefix_attributes;
29056 cp_parser_objc_visibility_spec (parser);
29058 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29059 break;
29061 cp_parser_decl_specifier_seq (parser,
29062 CP_PARSER_FLAGS_OPTIONAL,
29063 &declspecs,
29064 &decl_class_or_enum_p);
29066 /* auto, register, static, extern, mutable. */
29067 if (declspecs.storage_class != sc_none)
29069 cp_parser_error (parser, "invalid type for instance variable");
29070 declspecs.storage_class = sc_none;
29073 /* thread_local. */
29074 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
29076 cp_parser_error (parser, "invalid type for instance variable");
29077 declspecs.locations[ds_thread] = 0;
29080 /* typedef. */
29081 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
29083 cp_parser_error (parser, "invalid type for instance variable");
29084 declspecs.locations[ds_typedef] = 0;
29087 prefix_attributes = declspecs.attributes;
29088 declspecs.attributes = NULL_TREE;
29090 /* Keep going until we hit the `;' at the end of the
29091 declaration. */
29092 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29094 tree width = NULL_TREE, attributes, first_attribute, decl;
29095 cp_declarator *declarator = NULL;
29096 int ctor_dtor_or_conv_p;
29098 /* Check for a (possibly unnamed) bitfield declaration. */
29099 token = cp_lexer_peek_token (parser->lexer);
29100 if (token->type == CPP_COLON)
29101 goto eat_colon;
29103 if (token->type == CPP_NAME
29104 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
29105 == CPP_COLON))
29107 /* Get the name of the bitfield. */
29108 declarator = make_id_declarator (NULL_TREE,
29109 cp_parser_identifier (parser),
29110 sfk_none);
29112 eat_colon:
29113 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
29114 /* Get the width of the bitfield. */
29115 width
29116 = cp_parser_constant_expression (parser);
29118 else
29120 /* Parse the declarator. */
29121 declarator
29122 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29123 &ctor_dtor_or_conv_p,
29124 /*parenthesized_p=*/NULL,
29125 /*member_p=*/false,
29126 /*friend_p=*/false);
29129 /* Look for attributes that apply to the ivar. */
29130 attributes = cp_parser_attributes_opt (parser);
29131 /* Remember which attributes are prefix attributes and
29132 which are not. */
29133 first_attribute = attributes;
29134 /* Combine the attributes. */
29135 attributes = chainon (prefix_attributes, attributes);
29137 if (width)
29138 /* Create the bitfield declaration. */
29139 decl = grokbitfield (declarator, &declspecs,
29140 width,
29141 attributes);
29142 else
29143 decl = grokfield (declarator, &declspecs,
29144 NULL_TREE, /*init_const_expr_p=*/false,
29145 NULL_TREE, attributes);
29147 /* Add the instance variable. */
29148 if (decl != error_mark_node && decl != NULL_TREE)
29149 objc_add_instance_variable (decl);
29151 /* Reset PREFIX_ATTRIBUTES. */
29152 while (attributes && TREE_CHAIN (attributes) != first_attribute)
29153 attributes = TREE_CHAIN (attributes);
29154 if (attributes)
29155 TREE_CHAIN (attributes) = NULL_TREE;
29157 token = cp_lexer_peek_token (parser->lexer);
29159 if (token->type == CPP_COMMA)
29161 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29162 continue;
29164 break;
29167 cp_parser_consume_semicolon_at_end_of_statement (parser);
29168 token = cp_lexer_peek_token (parser->lexer);
29171 if (token->keyword == RID_AT_END)
29172 cp_parser_error (parser, "expected %<}%>");
29174 /* Do not consume the RID_AT_END, so it will be read again as terminating
29175 the @interface of @implementation. */
29176 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
29177 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
29179 /* For historical reasons, we accept an optional semicolon. */
29180 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29181 cp_lexer_consume_token (parser->lexer);
29184 /* Parse an Objective-C protocol declaration. */
29186 static void
29187 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
29189 tree proto, protorefs;
29190 cp_token *tok;
29192 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29193 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
29195 tok = cp_lexer_peek_token (parser->lexer);
29196 error_at (tok->location, "identifier expected after %<@protocol%>");
29197 cp_parser_consume_semicolon_at_end_of_statement (parser);
29198 return;
29201 /* See if we have a forward declaration or a definition. */
29202 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
29204 /* Try a forward declaration first. */
29205 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
29207 while (true)
29209 tree id;
29211 id = cp_parser_identifier (parser);
29212 if (id == error_mark_node)
29213 break;
29215 objc_declare_protocol (id, attributes);
29217 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29218 cp_lexer_consume_token (parser->lexer);
29219 else
29220 break;
29222 cp_parser_consume_semicolon_at_end_of_statement (parser);
29225 /* Ok, we got a full-fledged definition (or at least should). */
29226 else
29228 proto = cp_parser_identifier (parser);
29229 protorefs = cp_parser_objc_protocol_refs_opt (parser);
29230 objc_start_protocol (proto, protorefs, attributes);
29231 cp_parser_objc_method_prototype_list (parser);
29235 /* Parse an Objective-C superclass or category. */
29237 static void
29238 cp_parser_objc_superclass_or_category (cp_parser *parser,
29239 bool iface_p,
29240 tree *super,
29241 tree *categ, bool *is_class_extension)
29243 cp_token *next = cp_lexer_peek_token (parser->lexer);
29245 *super = *categ = NULL_TREE;
29246 *is_class_extension = false;
29247 if (next->type == CPP_COLON)
29249 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
29250 *super = cp_parser_identifier (parser);
29252 else if (next->type == CPP_OPEN_PAREN)
29254 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
29256 /* If there is no category name, and this is an @interface, we
29257 have a class extension. */
29258 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29260 *categ = NULL_TREE;
29261 *is_class_extension = true;
29263 else
29264 *categ = cp_parser_identifier (parser);
29266 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29270 /* Parse an Objective-C class interface. */
29272 static void
29273 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
29275 tree name, super, categ, protos;
29276 bool is_class_extension;
29278 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
29279 name = cp_parser_identifier (parser);
29280 if (name == error_mark_node)
29282 /* It's hard to recover because even if valid @interface stuff
29283 is to follow, we can't compile it (or validate it) if we
29284 don't even know which class it refers to. Let's assume this
29285 was a stray '@interface' token in the stream and skip it.
29287 return;
29289 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
29290 &is_class_extension);
29291 protos = cp_parser_objc_protocol_refs_opt (parser);
29293 /* We have either a class or a category on our hands. */
29294 if (categ || is_class_extension)
29295 objc_start_category_interface (name, categ, protos, attributes);
29296 else
29298 objc_start_class_interface (name, super, protos, attributes);
29299 /* Handle instance variable declarations, if any. */
29300 cp_parser_objc_class_ivars (parser);
29301 objc_continue_interface ();
29304 cp_parser_objc_method_prototype_list (parser);
29307 /* Parse an Objective-C class implementation. */
29309 static void
29310 cp_parser_objc_class_implementation (cp_parser* parser)
29312 tree name, super, categ;
29313 bool is_class_extension;
29315 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
29316 name = cp_parser_identifier (parser);
29317 if (name == error_mark_node)
29319 /* It's hard to recover because even if valid @implementation
29320 stuff is to follow, we can't compile it (or validate it) if
29321 we don't even know which class it refers to. Let's assume
29322 this was a stray '@implementation' token in the stream and
29323 skip it.
29325 return;
29327 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
29328 &is_class_extension);
29330 /* We have either a class or a category on our hands. */
29331 if (categ)
29332 objc_start_category_implementation (name, categ);
29333 else
29335 objc_start_class_implementation (name, super);
29336 /* Handle instance variable declarations, if any. */
29337 cp_parser_objc_class_ivars (parser);
29338 objc_continue_implementation ();
29341 cp_parser_objc_method_definition_list (parser);
29344 /* Consume the @end token and finish off the implementation. */
29346 static void
29347 cp_parser_objc_end_implementation (cp_parser* parser)
29349 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29350 objc_finish_implementation ();
29353 /* Parse an Objective-C declaration. */
29355 static void
29356 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
29358 /* Try to figure out what kind of declaration is present. */
29359 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29361 if (attributes)
29362 switch (kwd->keyword)
29364 case RID_AT_ALIAS:
29365 case RID_AT_CLASS:
29366 case RID_AT_END:
29367 error_at (kwd->location, "attributes may not be specified before"
29368 " the %<@%D%> Objective-C++ keyword",
29369 kwd->u.value);
29370 attributes = NULL;
29371 break;
29372 case RID_AT_IMPLEMENTATION:
29373 warning_at (kwd->location, OPT_Wattributes,
29374 "prefix attributes are ignored before %<@%D%>",
29375 kwd->u.value);
29376 attributes = NULL;
29377 default:
29378 break;
29381 switch (kwd->keyword)
29383 case RID_AT_ALIAS:
29384 cp_parser_objc_alias_declaration (parser);
29385 break;
29386 case RID_AT_CLASS:
29387 cp_parser_objc_class_declaration (parser);
29388 break;
29389 case RID_AT_PROTOCOL:
29390 cp_parser_objc_protocol_declaration (parser, attributes);
29391 break;
29392 case RID_AT_INTERFACE:
29393 cp_parser_objc_class_interface (parser, attributes);
29394 break;
29395 case RID_AT_IMPLEMENTATION:
29396 cp_parser_objc_class_implementation (parser);
29397 break;
29398 case RID_AT_END:
29399 cp_parser_objc_end_implementation (parser);
29400 break;
29401 default:
29402 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
29403 kwd->u.value);
29404 cp_parser_skip_to_end_of_block_or_statement (parser);
29408 /* Parse an Objective-C try-catch-finally statement.
29410 objc-try-catch-finally-stmt:
29411 @try compound-statement objc-catch-clause-seq [opt]
29412 objc-finally-clause [opt]
29414 objc-catch-clause-seq:
29415 objc-catch-clause objc-catch-clause-seq [opt]
29417 objc-catch-clause:
29418 @catch ( objc-exception-declaration ) compound-statement
29420 objc-finally-clause:
29421 @finally compound-statement
29423 objc-exception-declaration:
29424 parameter-declaration
29425 '...'
29427 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
29429 Returns NULL_TREE.
29431 PS: This function is identical to c_parser_objc_try_catch_finally_statement
29432 for C. Keep them in sync. */
29434 static tree
29435 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
29437 location_t location;
29438 tree stmt;
29440 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
29441 location = cp_lexer_peek_token (parser->lexer)->location;
29442 objc_maybe_warn_exceptions (location);
29443 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
29444 node, lest it get absorbed into the surrounding block. */
29445 stmt = push_stmt_list ();
29446 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29447 objc_begin_try_stmt (location, pop_stmt_list (stmt));
29449 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
29451 cp_parameter_declarator *parm;
29452 tree parameter_declaration = error_mark_node;
29453 bool seen_open_paren = false;
29455 cp_lexer_consume_token (parser->lexer);
29456 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29457 seen_open_paren = true;
29458 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29460 /* We have "@catch (...)" (where the '...' are literally
29461 what is in the code). Skip the '...'.
29462 parameter_declaration is set to NULL_TREE, and
29463 objc_being_catch_clauses() knows that that means
29464 '...'. */
29465 cp_lexer_consume_token (parser->lexer);
29466 parameter_declaration = NULL_TREE;
29468 else
29470 /* We have "@catch (NSException *exception)" or something
29471 like that. Parse the parameter declaration. */
29472 parm = cp_parser_parameter_declaration (parser, false, NULL);
29473 if (parm == NULL)
29474 parameter_declaration = error_mark_node;
29475 else
29476 parameter_declaration = grokdeclarator (parm->declarator,
29477 &parm->decl_specifiers,
29478 PARM, /*initialized=*/0,
29479 /*attrlist=*/NULL);
29481 if (seen_open_paren)
29482 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29483 else
29485 /* If there was no open parenthesis, we are recovering from
29486 an error, and we are trying to figure out what mistake
29487 the user has made. */
29489 /* If there is an immediate closing parenthesis, the user
29490 probably forgot the opening one (ie, they typed "@catch
29491 NSException *e)". Parse the closing parenthesis and keep
29492 going. */
29493 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29494 cp_lexer_consume_token (parser->lexer);
29496 /* If these is no immediate closing parenthesis, the user
29497 probably doesn't know that parenthesis are required at
29498 all (ie, they typed "@catch NSException *e"). So, just
29499 forget about the closing parenthesis and keep going. */
29501 objc_begin_catch_clause (parameter_declaration);
29502 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29503 objc_finish_catch_clause ();
29505 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
29507 cp_lexer_consume_token (parser->lexer);
29508 location = cp_lexer_peek_token (parser->lexer)->location;
29509 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
29510 node, lest it get absorbed into the surrounding block. */
29511 stmt = push_stmt_list ();
29512 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29513 objc_build_finally_clause (location, pop_stmt_list (stmt));
29516 return objc_finish_try_stmt ();
29519 /* Parse an Objective-C synchronized statement.
29521 objc-synchronized-stmt:
29522 @synchronized ( expression ) compound-statement
29524 Returns NULL_TREE. */
29526 static tree
29527 cp_parser_objc_synchronized_statement (cp_parser *parser)
29529 location_t location;
29530 tree lock, stmt;
29532 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
29534 location = cp_lexer_peek_token (parser->lexer)->location;
29535 objc_maybe_warn_exceptions (location);
29536 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
29537 lock = cp_parser_expression (parser);
29538 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29540 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
29541 node, lest it get absorbed into the surrounding block. */
29542 stmt = push_stmt_list ();
29543 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29545 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
29548 /* Parse an Objective-C throw statement.
29550 objc-throw-stmt:
29551 @throw assignment-expression [opt] ;
29553 Returns a constructed '@throw' statement. */
29555 static tree
29556 cp_parser_objc_throw_statement (cp_parser *parser)
29558 tree expr = NULL_TREE;
29559 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29561 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
29563 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29564 expr = cp_parser_expression (parser);
29566 cp_parser_consume_semicolon_at_end_of_statement (parser);
29568 return objc_build_throw_stmt (loc, expr);
29571 /* Parse an Objective-C statement. */
29573 static tree
29574 cp_parser_objc_statement (cp_parser * parser)
29576 /* Try to figure out what kind of declaration is present. */
29577 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29579 switch (kwd->keyword)
29581 case RID_AT_TRY:
29582 return cp_parser_objc_try_catch_finally_statement (parser);
29583 case RID_AT_SYNCHRONIZED:
29584 return cp_parser_objc_synchronized_statement (parser);
29585 case RID_AT_THROW:
29586 return cp_parser_objc_throw_statement (parser);
29587 default:
29588 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
29589 kwd->u.value);
29590 cp_parser_skip_to_end_of_block_or_statement (parser);
29593 return error_mark_node;
29596 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
29597 look ahead to see if an objc keyword follows the attributes. This
29598 is to detect the use of prefix attributes on ObjC @interface and
29599 @protocol. */
29601 static bool
29602 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
29604 cp_lexer_save_tokens (parser->lexer);
29605 *attrib = cp_parser_attributes_opt (parser);
29606 gcc_assert (*attrib);
29607 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
29609 cp_lexer_commit_tokens (parser->lexer);
29610 return true;
29612 cp_lexer_rollback_tokens (parser->lexer);
29613 return false;
29616 /* This routine is a minimal replacement for
29617 c_parser_struct_declaration () used when parsing the list of
29618 types/names or ObjC++ properties. For example, when parsing the
29619 code
29621 @property (readonly) int a, b, c;
29623 this function is responsible for parsing "int a, int b, int c" and
29624 returning the declarations as CHAIN of DECLs.
29626 TODO: Share this code with cp_parser_objc_class_ivars. It's very
29627 similar parsing. */
29628 static tree
29629 cp_parser_objc_struct_declaration (cp_parser *parser)
29631 tree decls = NULL_TREE;
29632 cp_decl_specifier_seq declspecs;
29633 int decl_class_or_enum_p;
29634 tree prefix_attributes;
29636 cp_parser_decl_specifier_seq (parser,
29637 CP_PARSER_FLAGS_NONE,
29638 &declspecs,
29639 &decl_class_or_enum_p);
29641 if (declspecs.type == error_mark_node)
29642 return error_mark_node;
29644 /* auto, register, static, extern, mutable. */
29645 if (declspecs.storage_class != sc_none)
29647 cp_parser_error (parser, "invalid type for property");
29648 declspecs.storage_class = sc_none;
29651 /* thread_local. */
29652 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
29654 cp_parser_error (parser, "invalid type for property");
29655 declspecs.locations[ds_thread] = 0;
29658 /* typedef. */
29659 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
29661 cp_parser_error (parser, "invalid type for property");
29662 declspecs.locations[ds_typedef] = 0;
29665 prefix_attributes = declspecs.attributes;
29666 declspecs.attributes = NULL_TREE;
29668 /* Keep going until we hit the `;' at the end of the declaration. */
29669 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29671 tree attributes, first_attribute, decl;
29672 cp_declarator *declarator;
29673 cp_token *token;
29675 /* Parse the declarator. */
29676 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29677 NULL, NULL, false, false);
29679 /* Look for attributes that apply to the ivar. */
29680 attributes = cp_parser_attributes_opt (parser);
29681 /* Remember which attributes are prefix attributes and
29682 which are not. */
29683 first_attribute = attributes;
29684 /* Combine the attributes. */
29685 attributes = chainon (prefix_attributes, attributes);
29687 decl = grokfield (declarator, &declspecs,
29688 NULL_TREE, /*init_const_expr_p=*/false,
29689 NULL_TREE, attributes);
29691 if (decl == error_mark_node || decl == NULL_TREE)
29692 return error_mark_node;
29694 /* Reset PREFIX_ATTRIBUTES. */
29695 while (attributes && TREE_CHAIN (attributes) != first_attribute)
29696 attributes = TREE_CHAIN (attributes);
29697 if (attributes)
29698 TREE_CHAIN (attributes) = NULL_TREE;
29700 DECL_CHAIN (decl) = decls;
29701 decls = decl;
29703 token = cp_lexer_peek_token (parser->lexer);
29704 if (token->type == CPP_COMMA)
29706 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29707 continue;
29709 else
29710 break;
29712 return decls;
29715 /* Parse an Objective-C @property declaration. The syntax is:
29717 objc-property-declaration:
29718 '@property' objc-property-attributes[opt] struct-declaration ;
29720 objc-property-attributes:
29721 '(' objc-property-attribute-list ')'
29723 objc-property-attribute-list:
29724 objc-property-attribute
29725 objc-property-attribute-list, objc-property-attribute
29727 objc-property-attribute
29728 'getter' = identifier
29729 'setter' = identifier
29730 'readonly'
29731 'readwrite'
29732 'assign'
29733 'retain'
29734 'copy'
29735 'nonatomic'
29737 For example:
29738 @property NSString *name;
29739 @property (readonly) id object;
29740 @property (retain, nonatomic, getter=getTheName) id name;
29741 @property int a, b, c;
29743 PS: This function is identical to
29744 c_parser_objc_at_property_declaration for C. Keep them in sync. */
29745 static void
29746 cp_parser_objc_at_property_declaration (cp_parser *parser)
29748 /* The following variables hold the attributes of the properties as
29749 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
29750 seen. When we see an attribute, we set them to 'true' (if they
29751 are boolean properties) or to the identifier (if they have an
29752 argument, ie, for getter and setter). Note that here we only
29753 parse the list of attributes, check the syntax and accumulate the
29754 attributes that we find. objc_add_property_declaration() will
29755 then process the information. */
29756 bool property_assign = false;
29757 bool property_copy = false;
29758 tree property_getter_ident = NULL_TREE;
29759 bool property_nonatomic = false;
29760 bool property_readonly = false;
29761 bool property_readwrite = false;
29762 bool property_retain = false;
29763 tree property_setter_ident = NULL_TREE;
29765 /* 'properties' is the list of properties that we read. Usually a
29766 single one, but maybe more (eg, in "@property int a, b, c;" there
29767 are three). */
29768 tree properties;
29769 location_t loc;
29771 loc = cp_lexer_peek_token (parser->lexer)->location;
29773 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
29775 /* Parse the optional attribute list... */
29776 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29778 /* Eat the '('. */
29779 cp_lexer_consume_token (parser->lexer);
29781 while (true)
29783 bool syntax_error = false;
29784 cp_token *token = cp_lexer_peek_token (parser->lexer);
29785 enum rid keyword;
29787 if (token->type != CPP_NAME)
29789 cp_parser_error (parser, "expected identifier");
29790 break;
29792 keyword = C_RID_CODE (token->u.value);
29793 cp_lexer_consume_token (parser->lexer);
29794 switch (keyword)
29796 case RID_ASSIGN: property_assign = true; break;
29797 case RID_COPY: property_copy = true; break;
29798 case RID_NONATOMIC: property_nonatomic = true; break;
29799 case RID_READONLY: property_readonly = true; break;
29800 case RID_READWRITE: property_readwrite = true; break;
29801 case RID_RETAIN: property_retain = true; break;
29803 case RID_GETTER:
29804 case RID_SETTER:
29805 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29807 if (keyword == RID_GETTER)
29808 cp_parser_error (parser,
29809 "missing %<=%> (after %<getter%> attribute)");
29810 else
29811 cp_parser_error (parser,
29812 "missing %<=%> (after %<setter%> attribute)");
29813 syntax_error = true;
29814 break;
29816 cp_lexer_consume_token (parser->lexer); /* eat the = */
29817 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
29819 cp_parser_error (parser, "expected identifier");
29820 syntax_error = true;
29821 break;
29823 if (keyword == RID_SETTER)
29825 if (property_setter_ident != NULL_TREE)
29827 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
29828 cp_lexer_consume_token (parser->lexer);
29830 else
29831 property_setter_ident = cp_parser_objc_selector (parser);
29832 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29833 cp_parser_error (parser, "setter name must terminate with %<:%>");
29834 else
29835 cp_lexer_consume_token (parser->lexer);
29837 else
29839 if (property_getter_ident != NULL_TREE)
29841 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
29842 cp_lexer_consume_token (parser->lexer);
29844 else
29845 property_getter_ident = cp_parser_objc_selector (parser);
29847 break;
29848 default:
29849 cp_parser_error (parser, "unknown property attribute");
29850 syntax_error = true;
29851 break;
29854 if (syntax_error)
29855 break;
29857 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29858 cp_lexer_consume_token (parser->lexer);
29859 else
29860 break;
29863 /* FIXME: "@property (setter, assign);" will generate a spurious
29864 "error: expected ‘)’ before ‘,’ token". This is because
29865 cp_parser_require, unlike the C counterpart, will produce an
29866 error even if we are in error recovery. */
29867 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29869 cp_parser_skip_to_closing_parenthesis (parser,
29870 /*recovering=*/true,
29871 /*or_comma=*/false,
29872 /*consume_paren=*/true);
29876 /* ... and the property declaration(s). */
29877 properties = cp_parser_objc_struct_declaration (parser);
29879 if (properties == error_mark_node)
29881 cp_parser_skip_to_end_of_statement (parser);
29882 /* If the next token is now a `;', consume it. */
29883 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29884 cp_lexer_consume_token (parser->lexer);
29885 return;
29888 if (properties == NULL_TREE)
29889 cp_parser_error (parser, "expected identifier");
29890 else
29892 /* Comma-separated properties are chained together in
29893 reverse order; add them one by one. */
29894 properties = nreverse (properties);
29896 for (; properties; properties = TREE_CHAIN (properties))
29897 objc_add_property_declaration (loc, copy_node (properties),
29898 property_readonly, property_readwrite,
29899 property_assign, property_retain,
29900 property_copy, property_nonatomic,
29901 property_getter_ident, property_setter_ident);
29904 cp_parser_consume_semicolon_at_end_of_statement (parser);
29907 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
29909 objc-synthesize-declaration:
29910 @synthesize objc-synthesize-identifier-list ;
29912 objc-synthesize-identifier-list:
29913 objc-synthesize-identifier
29914 objc-synthesize-identifier-list, objc-synthesize-identifier
29916 objc-synthesize-identifier
29917 identifier
29918 identifier = identifier
29920 For example:
29921 @synthesize MyProperty;
29922 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
29924 PS: This function is identical to c_parser_objc_at_synthesize_declaration
29925 for C. Keep them in sync.
29927 static void
29928 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
29930 tree list = NULL_TREE;
29931 location_t loc;
29932 loc = cp_lexer_peek_token (parser->lexer)->location;
29934 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
29935 while (true)
29937 tree property, ivar;
29938 property = cp_parser_identifier (parser);
29939 if (property == error_mark_node)
29941 cp_parser_consume_semicolon_at_end_of_statement (parser);
29942 return;
29944 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
29946 cp_lexer_consume_token (parser->lexer);
29947 ivar = cp_parser_identifier (parser);
29948 if (ivar == error_mark_node)
29950 cp_parser_consume_semicolon_at_end_of_statement (parser);
29951 return;
29954 else
29955 ivar = NULL_TREE;
29956 list = chainon (list, build_tree_list (ivar, property));
29957 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29958 cp_lexer_consume_token (parser->lexer);
29959 else
29960 break;
29962 cp_parser_consume_semicolon_at_end_of_statement (parser);
29963 objc_add_synthesize_declaration (loc, list);
29966 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
29968 objc-dynamic-declaration:
29969 @dynamic identifier-list ;
29971 For example:
29972 @dynamic MyProperty;
29973 @dynamic MyProperty, AnotherProperty;
29975 PS: This function is identical to c_parser_objc_at_dynamic_declaration
29976 for C. Keep them in sync.
29978 static void
29979 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
29981 tree list = NULL_TREE;
29982 location_t loc;
29983 loc = cp_lexer_peek_token (parser->lexer)->location;
29985 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
29986 while (true)
29988 tree property;
29989 property = cp_parser_identifier (parser);
29990 if (property == error_mark_node)
29992 cp_parser_consume_semicolon_at_end_of_statement (parser);
29993 return;
29995 list = chainon (list, build_tree_list (NULL, property));
29996 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29997 cp_lexer_consume_token (parser->lexer);
29998 else
29999 break;
30001 cp_parser_consume_semicolon_at_end_of_statement (parser);
30002 objc_add_dynamic_declaration (loc, list);
30006 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
30008 /* Returns name of the next clause.
30009 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
30010 the token is not consumed. Otherwise appropriate pragma_omp_clause is
30011 returned and the token is consumed. */
30013 static pragma_omp_clause
30014 cp_parser_omp_clause_name (cp_parser *parser)
30016 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
30018 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
30019 result = PRAGMA_OACC_CLAUSE_AUTO;
30020 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
30021 result = PRAGMA_OMP_CLAUSE_IF;
30022 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
30023 result = PRAGMA_OMP_CLAUSE_DEFAULT;
30024 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
30025 result = PRAGMA_OACC_CLAUSE_DELETE;
30026 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
30027 result = PRAGMA_OMP_CLAUSE_PRIVATE;
30028 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30029 result = PRAGMA_OMP_CLAUSE_FOR;
30030 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30032 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30033 const char *p = IDENTIFIER_POINTER (id);
30035 switch (p[0])
30037 case 'a':
30038 if (!strcmp ("aligned", p))
30039 result = PRAGMA_OMP_CLAUSE_ALIGNED;
30040 else if (!strcmp ("async", p))
30041 result = PRAGMA_OACC_CLAUSE_ASYNC;
30042 break;
30043 case 'c':
30044 if (!strcmp ("collapse", p))
30045 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
30046 else if (!strcmp ("copy", p))
30047 result = PRAGMA_OACC_CLAUSE_COPY;
30048 else if (!strcmp ("copyin", p))
30049 result = PRAGMA_OMP_CLAUSE_COPYIN;
30050 else if (!strcmp ("copyout", p))
30051 result = PRAGMA_OACC_CLAUSE_COPYOUT;
30052 else if (!strcmp ("copyprivate", p))
30053 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
30054 else if (!strcmp ("create", p))
30055 result = PRAGMA_OACC_CLAUSE_CREATE;
30056 break;
30057 case 'd':
30058 if (!strcmp ("defaultmap", p))
30059 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
30060 else if (!strcmp ("depend", p))
30061 result = PRAGMA_OMP_CLAUSE_DEPEND;
30062 else if (!strcmp ("device", p))
30063 result = PRAGMA_OMP_CLAUSE_DEVICE;
30064 else if (!strcmp ("deviceptr", p))
30065 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
30066 else if (!strcmp ("device_resident", p))
30067 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
30068 else if (!strcmp ("dist_schedule", p))
30069 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
30070 break;
30071 case 'f':
30072 if (!strcmp ("final", p))
30073 result = PRAGMA_OMP_CLAUSE_FINAL;
30074 else if (!strcmp ("firstprivate", p))
30075 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
30076 else if (!strcmp ("from", p))
30077 result = PRAGMA_OMP_CLAUSE_FROM;
30078 break;
30079 case 'g':
30080 if (!strcmp ("gang", p))
30081 result = PRAGMA_OACC_CLAUSE_GANG;
30082 else if (!strcmp ("grainsize", p))
30083 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
30084 break;
30085 case 'h':
30086 if (!strcmp ("hint", p))
30087 result = PRAGMA_OMP_CLAUSE_HINT;
30088 else if (!strcmp ("host", p))
30089 result = PRAGMA_OACC_CLAUSE_HOST;
30090 break;
30091 case 'i':
30092 if (!strcmp ("inbranch", p))
30093 result = PRAGMA_OMP_CLAUSE_INBRANCH;
30094 else if (!strcmp ("independent", p))
30095 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
30096 else if (!strcmp ("is_device_ptr", p))
30097 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
30098 break;
30099 case 'l':
30100 if (!strcmp ("lastprivate", p))
30101 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
30102 else if (!strcmp ("linear", p))
30103 result = PRAGMA_OMP_CLAUSE_LINEAR;
30104 else if (!strcmp ("link", p))
30105 result = PRAGMA_OMP_CLAUSE_LINK;
30106 break;
30107 case 'm':
30108 if (!strcmp ("map", p))
30109 result = PRAGMA_OMP_CLAUSE_MAP;
30110 else if (!strcmp ("mergeable", p))
30111 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
30112 else if (flag_cilkplus && !strcmp ("mask", p))
30113 result = PRAGMA_CILK_CLAUSE_MASK;
30114 break;
30115 case 'n':
30116 if (!strcmp ("nogroup", p))
30117 result = PRAGMA_OMP_CLAUSE_NOGROUP;
30118 else if (!strcmp ("notinbranch", p))
30119 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
30120 else if (!strcmp ("nowait", p))
30121 result = PRAGMA_OMP_CLAUSE_NOWAIT;
30122 else if (flag_cilkplus && !strcmp ("nomask", p))
30123 result = PRAGMA_CILK_CLAUSE_NOMASK;
30124 else if (!strcmp ("num_gangs", p))
30125 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
30126 else if (!strcmp ("num_tasks", p))
30127 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
30128 else if (!strcmp ("num_teams", p))
30129 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
30130 else if (!strcmp ("num_threads", p))
30131 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
30132 else if (!strcmp ("num_workers", p))
30133 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
30134 break;
30135 case 'o':
30136 if (!strcmp ("ordered", p))
30137 result = PRAGMA_OMP_CLAUSE_ORDERED;
30138 break;
30139 case 'p':
30140 if (!strcmp ("parallel", p))
30141 result = PRAGMA_OMP_CLAUSE_PARALLEL;
30142 else if (!strcmp ("present", p))
30143 result = PRAGMA_OACC_CLAUSE_PRESENT;
30144 else if (!strcmp ("present_or_copy", p)
30145 || !strcmp ("pcopy", p))
30146 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
30147 else if (!strcmp ("present_or_copyin", p)
30148 || !strcmp ("pcopyin", p))
30149 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
30150 else if (!strcmp ("present_or_copyout", p)
30151 || !strcmp ("pcopyout", p))
30152 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
30153 else if (!strcmp ("present_or_create", p)
30154 || !strcmp ("pcreate", p))
30155 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
30156 else if (!strcmp ("priority", p))
30157 result = PRAGMA_OMP_CLAUSE_PRIORITY;
30158 else if (!strcmp ("proc_bind", p))
30159 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
30160 break;
30161 case 'r':
30162 if (!strcmp ("reduction", p))
30163 result = PRAGMA_OMP_CLAUSE_REDUCTION;
30164 break;
30165 case 's':
30166 if (!strcmp ("safelen", p))
30167 result = PRAGMA_OMP_CLAUSE_SAFELEN;
30168 else if (!strcmp ("schedule", p))
30169 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
30170 else if (!strcmp ("sections", p))
30171 result = PRAGMA_OMP_CLAUSE_SECTIONS;
30172 else if (!strcmp ("self", p))
30173 result = PRAGMA_OACC_CLAUSE_SELF;
30174 else if (!strcmp ("seq", p))
30175 result = PRAGMA_OACC_CLAUSE_SEQ;
30176 else if (!strcmp ("shared", p))
30177 result = PRAGMA_OMP_CLAUSE_SHARED;
30178 else if (!strcmp ("simd", p))
30179 result = PRAGMA_OMP_CLAUSE_SIMD;
30180 else if (!strcmp ("simdlen", p))
30181 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
30182 break;
30183 case 't':
30184 if (!strcmp ("taskgroup", p))
30185 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
30186 else if (!strcmp ("thread_limit", p))
30187 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
30188 else if (!strcmp ("threads", p))
30189 result = PRAGMA_OMP_CLAUSE_THREADS;
30190 else if (!strcmp ("tile", p))
30191 result = PRAGMA_OACC_CLAUSE_TILE;
30192 else if (!strcmp ("to", p))
30193 result = PRAGMA_OMP_CLAUSE_TO;
30194 break;
30195 case 'u':
30196 if (!strcmp ("uniform", p))
30197 result = PRAGMA_OMP_CLAUSE_UNIFORM;
30198 else if (!strcmp ("untied", p))
30199 result = PRAGMA_OMP_CLAUSE_UNTIED;
30200 else if (!strcmp ("use_device", p))
30201 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
30202 else if (!strcmp ("use_device_ptr", p))
30203 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
30204 break;
30205 case 'v':
30206 if (!strcmp ("vector", p))
30207 result = PRAGMA_OACC_CLAUSE_VECTOR;
30208 else if (!strcmp ("vector_length", p))
30209 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
30210 else if (flag_cilkplus && !strcmp ("vectorlength", p))
30211 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
30212 break;
30213 case 'w':
30214 if (!strcmp ("wait", p))
30215 result = PRAGMA_OACC_CLAUSE_WAIT;
30216 else if (!strcmp ("worker", p))
30217 result = PRAGMA_OACC_CLAUSE_WORKER;
30218 break;
30222 if (result != PRAGMA_OMP_CLAUSE_NONE)
30223 cp_lexer_consume_token (parser->lexer);
30225 return result;
30228 /* Validate that a clause of the given type does not already exist. */
30230 static void
30231 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
30232 const char *name, location_t location)
30234 tree c;
30236 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
30237 if (OMP_CLAUSE_CODE (c) == code)
30239 error_at (location, "too many %qs clauses", name);
30240 break;
30244 /* OpenMP 2.5:
30245 variable-list:
30246 identifier
30247 variable-list , identifier
30249 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
30250 colon). An opening parenthesis will have been consumed by the caller.
30252 If KIND is nonzero, create the appropriate node and install the decl
30253 in OMP_CLAUSE_DECL and add the node to the head of the list.
30255 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
30256 return the list created.
30258 COLON can be NULL if only closing parenthesis should end the list,
30259 or pointer to bool which will receive false if the list is terminated
30260 by closing parenthesis or true if the list is terminated by colon. */
30262 static tree
30263 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
30264 tree list, bool *colon)
30266 cp_token *token;
30267 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
30268 if (colon)
30270 parser->colon_corrects_to_scope_p = false;
30271 *colon = false;
30273 while (1)
30275 tree name, decl;
30277 token = cp_lexer_peek_token (parser->lexer);
30278 if (kind != 0
30279 && current_class_ptr
30280 && cp_parser_is_keyword (token, RID_THIS))
30282 decl = finish_this_expr ();
30283 if (TREE_CODE (decl) == NON_LVALUE_EXPR
30284 || CONVERT_EXPR_P (decl))
30285 decl = TREE_OPERAND (decl, 0);
30286 cp_lexer_consume_token (parser->lexer);
30288 else
30290 name = cp_parser_id_expression (parser, /*template_p=*/false,
30291 /*check_dependency_p=*/true,
30292 /*template_p=*/NULL,
30293 /*declarator_p=*/false,
30294 /*optional_p=*/false);
30295 if (name == error_mark_node)
30296 goto skip_comma;
30298 decl = cp_parser_lookup_name_simple (parser, name, token->location);
30299 if (decl == error_mark_node)
30300 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
30301 token->location);
30303 if (decl == error_mark_node)
30305 else if (kind != 0)
30307 switch (kind)
30309 case OMP_CLAUSE__CACHE_:
30310 /* The OpenACC cache directive explicitly only allows "array
30311 elements or subarrays". */
30312 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
30314 error_at (token->location, "expected %<[%>");
30315 decl = error_mark_node;
30316 break;
30318 /* FALLTHROUGH. */
30319 case OMP_CLAUSE_MAP:
30320 case OMP_CLAUSE_FROM:
30321 case OMP_CLAUSE_TO:
30322 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
30324 location_t loc
30325 = cp_lexer_peek_token (parser->lexer)->location;
30326 cp_id_kind idk = CP_ID_KIND_NONE;
30327 cp_lexer_consume_token (parser->lexer);
30328 decl = convert_from_reference (decl);
30329 decl
30330 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
30331 decl, false,
30332 &idk, loc);
30334 /* FALLTHROUGH. */
30335 case OMP_CLAUSE_DEPEND:
30336 case OMP_CLAUSE_REDUCTION:
30337 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
30339 tree low_bound = NULL_TREE, length = NULL_TREE;
30341 parser->colon_corrects_to_scope_p = false;
30342 cp_lexer_consume_token (parser->lexer);
30343 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30344 low_bound = cp_parser_expression (parser);
30345 if (!colon)
30346 parser->colon_corrects_to_scope_p
30347 = saved_colon_corrects_to_scope_p;
30348 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
30349 length = integer_one_node;
30350 else
30352 /* Look for `:'. */
30353 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30354 goto skip_comma;
30355 if (!cp_lexer_next_token_is (parser->lexer,
30356 CPP_CLOSE_SQUARE))
30357 length = cp_parser_expression (parser);
30359 /* Look for the closing `]'. */
30360 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
30361 RT_CLOSE_SQUARE))
30362 goto skip_comma;
30364 decl = tree_cons (low_bound, length, decl);
30366 break;
30367 default:
30368 break;
30371 tree u = build_omp_clause (token->location, kind);
30372 OMP_CLAUSE_DECL (u) = decl;
30373 OMP_CLAUSE_CHAIN (u) = list;
30374 list = u;
30376 else
30377 list = tree_cons (decl, NULL_TREE, list);
30379 get_comma:
30380 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
30381 break;
30382 cp_lexer_consume_token (parser->lexer);
30385 if (colon)
30386 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30388 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30390 *colon = true;
30391 cp_parser_require (parser, CPP_COLON, RT_COLON);
30392 return list;
30395 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30397 int ending;
30399 /* Try to resync to an unnested comma. Copied from
30400 cp_parser_parenthesized_expression_list. */
30401 skip_comma:
30402 if (colon)
30403 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30404 ending = cp_parser_skip_to_closing_parenthesis (parser,
30405 /*recovering=*/true,
30406 /*or_comma=*/true,
30407 /*consume_paren=*/true);
30408 if (ending < 0)
30409 goto get_comma;
30412 return list;
30415 /* Similarly, but expect leading and trailing parenthesis. This is a very
30416 common case for omp clauses. */
30418 static tree
30419 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
30421 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30422 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
30423 return list;
30426 /* OpenACC 2.0:
30427 copy ( variable-list )
30428 copyin ( variable-list )
30429 copyout ( variable-list )
30430 create ( variable-list )
30431 delete ( variable-list )
30432 present ( variable-list )
30433 present_or_copy ( variable-list )
30434 pcopy ( variable-list )
30435 present_or_copyin ( variable-list )
30436 pcopyin ( variable-list )
30437 present_or_copyout ( variable-list )
30438 pcopyout ( variable-list )
30439 present_or_create ( variable-list )
30440 pcreate ( variable-list ) */
30442 static tree
30443 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
30444 tree list)
30446 enum gomp_map_kind kind;
30447 switch (c_kind)
30449 case PRAGMA_OACC_CLAUSE_COPY:
30450 kind = GOMP_MAP_FORCE_TOFROM;
30451 break;
30452 case PRAGMA_OACC_CLAUSE_COPYIN:
30453 kind = GOMP_MAP_FORCE_TO;
30454 break;
30455 case PRAGMA_OACC_CLAUSE_COPYOUT:
30456 kind = GOMP_MAP_FORCE_FROM;
30457 break;
30458 case PRAGMA_OACC_CLAUSE_CREATE:
30459 kind = GOMP_MAP_FORCE_ALLOC;
30460 break;
30461 case PRAGMA_OACC_CLAUSE_DELETE:
30462 kind = GOMP_MAP_DELETE;
30463 break;
30464 case PRAGMA_OACC_CLAUSE_DEVICE:
30465 kind = GOMP_MAP_FORCE_TO;
30466 break;
30467 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
30468 kind = GOMP_MAP_DEVICE_RESIDENT;
30469 break;
30470 case PRAGMA_OACC_CLAUSE_HOST:
30471 case PRAGMA_OACC_CLAUSE_SELF:
30472 kind = GOMP_MAP_FORCE_FROM;
30473 break;
30474 case PRAGMA_OACC_CLAUSE_LINK:
30475 kind = GOMP_MAP_LINK;
30476 break;
30477 case PRAGMA_OACC_CLAUSE_PRESENT:
30478 kind = GOMP_MAP_FORCE_PRESENT;
30479 break;
30480 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
30481 kind = GOMP_MAP_TOFROM;
30482 break;
30483 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
30484 kind = GOMP_MAP_TO;
30485 break;
30486 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
30487 kind = GOMP_MAP_FROM;
30488 break;
30489 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
30490 kind = GOMP_MAP_ALLOC;
30491 break;
30492 default:
30493 gcc_unreachable ();
30495 tree nl, c;
30496 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
30498 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
30499 OMP_CLAUSE_SET_MAP_KIND (c, kind);
30501 return nl;
30504 /* OpenACC 2.0:
30505 deviceptr ( variable-list ) */
30507 static tree
30508 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
30510 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30511 tree vars, t;
30513 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
30514 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
30515 variable-list must only allow for pointer variables. */
30516 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30517 for (t = vars; t; t = TREE_CHAIN (t))
30519 tree v = TREE_PURPOSE (t);
30520 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
30521 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
30522 OMP_CLAUSE_DECL (u) = v;
30523 OMP_CLAUSE_CHAIN (u) = list;
30524 list = u;
30527 return list;
30530 /* OpenACC 2.0:
30531 auto
30532 independent
30533 nohost
30534 seq */
30536 static tree
30537 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
30538 enum omp_clause_code code,
30539 tree list, location_t location)
30541 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
30542 tree c = build_omp_clause (location, code);
30543 OMP_CLAUSE_CHAIN (c) = list;
30544 return c;
30547 /* OpenACC:
30548 num_gangs ( expression )
30549 num_workers ( expression )
30550 vector_length ( expression ) */
30552 static tree
30553 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
30554 const char *str, tree list)
30556 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30558 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30559 return list;
30561 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
30563 if (t == error_mark_node
30564 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30566 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30567 /*or_comma=*/false,
30568 /*consume_paren=*/true);
30569 return list;
30572 check_no_duplicate_clause (list, code, str, loc);
30574 tree c = build_omp_clause (loc, code);
30575 OMP_CLAUSE_OPERAND (c, 0) = t;
30576 OMP_CLAUSE_CHAIN (c) = list;
30577 return c;
30580 /* OpenACC:
30582 gang [( gang-arg-list )]
30583 worker [( [num:] int-expr )]
30584 vector [( [length:] int-expr )]
30586 where gang-arg is one of:
30588 [num:] int-expr
30589 static: size-expr
30591 and size-expr may be:
30594 int-expr
30597 static tree
30598 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
30599 const char *str, tree list)
30601 const char *id = "num";
30602 cp_lexer *lexer = parser->lexer;
30603 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
30604 location_t loc = cp_lexer_peek_token (lexer)->location;
30606 if (kind == OMP_CLAUSE_VECTOR)
30607 id = "length";
30609 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
30611 cp_lexer_consume_token (lexer);
30615 cp_token *next = cp_lexer_peek_token (lexer);
30616 int idx = 0;
30618 /* Gang static argument. */
30619 if (kind == OMP_CLAUSE_GANG
30620 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
30622 cp_lexer_consume_token (lexer);
30624 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30625 goto cleanup_error;
30627 idx = 1;
30628 if (ops[idx] != NULL)
30630 cp_parser_error (parser, "too many %<static%> arguments");
30631 goto cleanup_error;
30634 /* Check for the '*' argument. */
30635 if (cp_lexer_next_token_is (lexer, CPP_MULT)
30636 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
30637 || cp_lexer_nth_token_is (parser->lexer, 2,
30638 CPP_CLOSE_PAREN)))
30640 cp_lexer_consume_token (lexer);
30641 ops[idx] = integer_minus_one_node;
30643 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
30645 cp_lexer_consume_token (lexer);
30646 continue;
30648 else break;
30651 /* Worker num: argument and vector length: arguments. */
30652 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
30653 && strcmp (id, IDENTIFIER_POINTER (next->u.value)) == 0
30654 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
30656 cp_lexer_consume_token (lexer); /* id */
30657 cp_lexer_consume_token (lexer); /* ':' */
30660 /* Now collect the actual argument. */
30661 if (ops[idx] != NULL_TREE)
30663 cp_parser_error (parser, "unexpected argument");
30664 goto cleanup_error;
30667 tree expr = cp_parser_assignment_expression (parser, NULL, false,
30668 false);
30669 if (expr == error_mark_node)
30670 goto cleanup_error;
30672 mark_exp_read (expr);
30673 ops[idx] = expr;
30675 if (kind == OMP_CLAUSE_GANG
30676 && cp_lexer_next_token_is (lexer, CPP_COMMA))
30678 cp_lexer_consume_token (lexer);
30679 continue;
30681 break;
30683 while (1);
30685 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30686 goto cleanup_error;
30689 check_no_duplicate_clause (list, kind, str, loc);
30691 c = build_omp_clause (loc, kind);
30693 if (ops[1])
30694 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
30696 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
30697 OMP_CLAUSE_CHAIN (c) = list;
30699 return c;
30701 cleanup_error:
30702 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
30703 return list;
30706 /* OpenACC 2.0:
30707 tile ( size-expr-list ) */
30709 static tree
30710 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
30712 tree c, expr = error_mark_node;
30713 tree tile = NULL_TREE;
30715 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
30717 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30718 return list;
30722 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
30723 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
30724 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
30726 cp_lexer_consume_token (parser->lexer);
30727 expr = integer_minus_one_node;
30729 else
30730 expr = cp_parser_assignment_expression (parser, NULL, false, false);
30732 if (expr == error_mark_node)
30733 return list;
30735 tile = tree_cons (NULL_TREE, expr, tile);
30737 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30738 cp_lexer_consume_token (parser->lexer);
30740 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
30742 /* Consume the trailing ')'. */
30743 cp_lexer_consume_token (parser->lexer);
30745 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
30746 tile = nreverse (tile);
30747 OMP_CLAUSE_TILE_LIST (c) = tile;
30748 OMP_CLAUSE_CHAIN (c) = list;
30749 return c;
30752 /* OpenACC 2.0
30753 Parse wait clause or directive parameters. */
30755 static tree
30756 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
30758 vec<tree, va_gc> *args;
30759 tree t, args_tree;
30761 args = cp_parser_parenthesized_expression_list (parser, non_attr,
30762 /*cast_p=*/false,
30763 /*allow_expansion_p=*/true,
30764 /*non_constant_p=*/NULL);
30766 if (args == NULL || args->length () == 0)
30768 cp_parser_error (parser, "expected integer expression before ')'");
30769 if (args != NULL)
30770 release_tree_vector (args);
30771 return list;
30774 args_tree = build_tree_list_vec (args);
30776 release_tree_vector (args);
30778 for (t = args_tree; t; t = TREE_CHAIN (t))
30780 tree targ = TREE_VALUE (t);
30782 if (targ != error_mark_node)
30784 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
30785 error ("%<wait%> expression must be integral");
30786 else
30788 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
30790 mark_rvalue_use (targ);
30791 OMP_CLAUSE_DECL (c) = targ;
30792 OMP_CLAUSE_CHAIN (c) = list;
30793 list = c;
30798 return list;
30801 /* OpenACC:
30802 wait ( int-expr-list ) */
30804 static tree
30805 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
30807 location_t location = cp_lexer_peek_token (parser->lexer)->location;
30809 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
30810 return list;
30812 list = cp_parser_oacc_wait_list (parser, location, list);
30814 return list;
30817 /* OpenMP 3.0:
30818 collapse ( constant-expression ) */
30820 static tree
30821 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
30823 tree c, num;
30824 location_t loc;
30825 HOST_WIDE_INT n;
30827 loc = cp_lexer_peek_token (parser->lexer)->location;
30828 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30829 return list;
30831 num = cp_parser_constant_expression (parser);
30833 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30834 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30835 /*or_comma=*/false,
30836 /*consume_paren=*/true);
30838 if (num == error_mark_node)
30839 return list;
30840 num = fold_non_dependent_expr (num);
30841 if (!tree_fits_shwi_p (num)
30842 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
30843 || (n = tree_to_shwi (num)) <= 0
30844 || (int) n != n)
30846 error_at (loc, "collapse argument needs positive constant integer expression");
30847 return list;
30850 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
30851 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
30852 OMP_CLAUSE_CHAIN (c) = list;
30853 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
30855 return c;
30858 /* OpenMP 2.5:
30859 default ( shared | none )
30861 OpenACC 2.0
30862 default (none) */
30864 static tree
30865 cp_parser_omp_clause_default (cp_parser *parser, tree list,
30866 location_t location, bool is_oacc)
30868 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
30869 tree c;
30871 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30872 return list;
30873 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30875 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30876 const char *p = IDENTIFIER_POINTER (id);
30878 switch (p[0])
30880 case 'n':
30881 if (strcmp ("none", p) != 0)
30882 goto invalid_kind;
30883 kind = OMP_CLAUSE_DEFAULT_NONE;
30884 break;
30886 case 's':
30887 if (strcmp ("shared", p) != 0 || is_oacc)
30888 goto invalid_kind;
30889 kind = OMP_CLAUSE_DEFAULT_SHARED;
30890 break;
30892 default:
30893 goto invalid_kind;
30896 cp_lexer_consume_token (parser->lexer);
30898 else
30900 invalid_kind:
30901 if (is_oacc)
30902 cp_parser_error (parser, "expected %<none%>");
30903 else
30904 cp_parser_error (parser, "expected %<none%> or %<shared%>");
30907 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30908 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30909 /*or_comma=*/false,
30910 /*consume_paren=*/true);
30912 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
30913 return list;
30915 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
30916 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
30917 OMP_CLAUSE_CHAIN (c) = list;
30918 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
30920 return c;
30923 /* OpenMP 3.1:
30924 final ( expression ) */
30926 static tree
30927 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
30929 tree t, c;
30931 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30932 return list;
30934 t = cp_parser_condition (parser);
30936 if (t == error_mark_node
30937 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30938 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30939 /*or_comma=*/false,
30940 /*consume_paren=*/true);
30942 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
30944 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
30945 OMP_CLAUSE_FINAL_EXPR (c) = t;
30946 OMP_CLAUSE_CHAIN (c) = list;
30948 return c;
30951 /* OpenMP 2.5:
30952 if ( expression )
30954 OpenMP 4.5:
30955 if ( directive-name-modifier : expression )
30957 directive-name-modifier:
30958 parallel | task | taskloop | target data | target | target update
30959 | target enter data | target exit data */
30961 static tree
30962 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
30963 bool is_omp)
30965 tree t, c;
30966 enum tree_code if_modifier = ERROR_MARK;
30968 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30969 return list;
30971 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30973 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30974 const char *p = IDENTIFIER_POINTER (id);
30975 int n = 2;
30977 if (strcmp ("parallel", p) == 0)
30978 if_modifier = OMP_PARALLEL;
30979 else if (strcmp ("task", p) == 0)
30980 if_modifier = OMP_TASK;
30981 else if (strcmp ("taskloop", p) == 0)
30982 if_modifier = OMP_TASKLOOP;
30983 else if (strcmp ("target", p) == 0)
30985 if_modifier = OMP_TARGET;
30986 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
30988 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
30989 p = IDENTIFIER_POINTER (id);
30990 if (strcmp ("data", p) == 0)
30991 if_modifier = OMP_TARGET_DATA;
30992 else if (strcmp ("update", p) == 0)
30993 if_modifier = OMP_TARGET_UPDATE;
30994 else if (strcmp ("enter", p) == 0)
30995 if_modifier = OMP_TARGET_ENTER_DATA;
30996 else if (strcmp ("exit", p) == 0)
30997 if_modifier = OMP_TARGET_EXIT_DATA;
30998 if (if_modifier != OMP_TARGET)
30999 n = 3;
31000 else
31002 location_t loc
31003 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
31004 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
31005 "or %<exit%>");
31006 if_modifier = ERROR_MARK;
31008 if (if_modifier == OMP_TARGET_ENTER_DATA
31009 || if_modifier == OMP_TARGET_EXIT_DATA)
31011 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
31013 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
31014 p = IDENTIFIER_POINTER (id);
31015 if (strcmp ("data", p) == 0)
31016 n = 4;
31018 if (n != 4)
31020 location_t loc
31021 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
31022 error_at (loc, "expected %<data%>");
31023 if_modifier = ERROR_MARK;
31028 if (if_modifier != ERROR_MARK)
31030 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
31032 while (n-- > 0)
31033 cp_lexer_consume_token (parser->lexer);
31035 else
31037 if (n > 2)
31039 location_t loc
31040 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
31041 error_at (loc, "expected %<:%>");
31043 if_modifier = ERROR_MARK;
31048 t = cp_parser_condition (parser);
31050 if (t == error_mark_node
31051 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31052 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31053 /*or_comma=*/false,
31054 /*consume_paren=*/true);
31056 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
31057 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
31059 if (if_modifier != ERROR_MARK
31060 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
31062 const char *p = NULL;
31063 switch (if_modifier)
31065 case OMP_PARALLEL: p = "parallel"; break;
31066 case OMP_TASK: p = "task"; break;
31067 case OMP_TASKLOOP: p = "taskloop"; break;
31068 case OMP_TARGET_DATA: p = "target data"; break;
31069 case OMP_TARGET: p = "target"; break;
31070 case OMP_TARGET_UPDATE: p = "target update"; break;
31071 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
31072 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
31073 default: gcc_unreachable ();
31075 error_at (location, "too many %<if%> clauses with %qs modifier",
31077 return list;
31079 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
31081 if (!is_omp)
31082 error_at (location, "too many %<if%> clauses");
31083 else
31084 error_at (location, "too many %<if%> clauses without modifier");
31085 return list;
31087 else if (if_modifier == ERROR_MARK
31088 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
31090 error_at (location, "if any %<if%> clause has modifier, then all "
31091 "%<if%> clauses have to use modifier");
31092 return list;
31096 c = build_omp_clause (location, OMP_CLAUSE_IF);
31097 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
31098 OMP_CLAUSE_IF_EXPR (c) = t;
31099 OMP_CLAUSE_CHAIN (c) = list;
31101 return c;
31104 /* OpenMP 3.1:
31105 mergeable */
31107 static tree
31108 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
31109 tree list, location_t location)
31111 tree c;
31113 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
31114 location);
31116 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
31117 OMP_CLAUSE_CHAIN (c) = list;
31118 return c;
31121 /* OpenMP 2.5:
31122 nowait */
31124 static tree
31125 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
31126 tree list, location_t location)
31128 tree c;
31130 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
31132 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
31133 OMP_CLAUSE_CHAIN (c) = list;
31134 return c;
31137 /* OpenMP 2.5:
31138 num_threads ( expression ) */
31140 static tree
31141 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
31142 location_t location)
31144 tree t, c;
31146 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31147 return list;
31149 t = cp_parser_expression (parser);
31151 if (t == error_mark_node
31152 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31153 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31154 /*or_comma=*/false,
31155 /*consume_paren=*/true);
31157 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
31158 "num_threads", location);
31160 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
31161 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
31162 OMP_CLAUSE_CHAIN (c) = list;
31164 return c;
31167 /* OpenMP 4.5:
31168 num_tasks ( expression ) */
31170 static tree
31171 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
31172 location_t location)
31174 tree t, c;
31176 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31177 return list;
31179 t = cp_parser_expression (parser);
31181 if (t == error_mark_node
31182 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31183 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31184 /*or_comma=*/false,
31185 /*consume_paren=*/true);
31187 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
31188 "num_tasks", location);
31190 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
31191 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
31192 OMP_CLAUSE_CHAIN (c) = list;
31194 return c;
31197 /* OpenMP 4.5:
31198 grainsize ( expression ) */
31200 static tree
31201 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
31202 location_t location)
31204 tree t, c;
31206 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31207 return list;
31209 t = cp_parser_expression (parser);
31211 if (t == error_mark_node
31212 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31213 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31214 /*or_comma=*/false,
31215 /*consume_paren=*/true);
31217 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
31218 "grainsize", location);
31220 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
31221 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
31222 OMP_CLAUSE_CHAIN (c) = list;
31224 return c;
31227 /* OpenMP 4.5:
31228 priority ( expression ) */
31230 static tree
31231 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
31232 location_t location)
31234 tree t, c;
31236 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31237 return list;
31239 t = cp_parser_expression (parser);
31241 if (t == error_mark_node
31242 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31243 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31244 /*or_comma=*/false,
31245 /*consume_paren=*/true);
31247 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
31248 "priority", location);
31250 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
31251 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
31252 OMP_CLAUSE_CHAIN (c) = list;
31254 return c;
31257 /* OpenMP 4.5:
31258 hint ( expression ) */
31260 static tree
31261 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
31262 location_t location)
31264 tree t, c;
31266 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31267 return list;
31269 t = cp_parser_expression (parser);
31271 if (t == error_mark_node
31272 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31273 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31274 /*or_comma=*/false,
31275 /*consume_paren=*/true);
31277 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
31279 c = build_omp_clause (location, OMP_CLAUSE_HINT);
31280 OMP_CLAUSE_HINT_EXPR (c) = t;
31281 OMP_CLAUSE_CHAIN (c) = list;
31283 return c;
31286 /* OpenMP 4.5:
31287 defaultmap ( tofrom : scalar ) */
31289 static tree
31290 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
31291 location_t location)
31293 tree c, id;
31294 const char *p;
31296 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31297 return list;
31299 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31301 cp_parser_error (parser, "expected %<tofrom%>");
31302 goto out_err;
31304 id = cp_lexer_peek_token (parser->lexer)->u.value;
31305 p = IDENTIFIER_POINTER (id);
31306 if (strcmp (p, "tofrom") != 0)
31308 cp_parser_error (parser, "expected %<tofrom%>");
31309 goto out_err;
31311 cp_lexer_consume_token (parser->lexer);
31312 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31313 goto out_err;
31315 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31317 cp_parser_error (parser, "expected %<scalar%>");
31318 goto out_err;
31320 id = cp_lexer_peek_token (parser->lexer)->u.value;
31321 p = IDENTIFIER_POINTER (id);
31322 if (strcmp (p, "scalar") != 0)
31324 cp_parser_error (parser, "expected %<scalar%>");
31325 goto out_err;
31327 cp_lexer_consume_token (parser->lexer);
31328 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31329 goto out_err;
31331 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
31332 location);
31334 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
31335 OMP_CLAUSE_CHAIN (c) = list;
31336 return c;
31338 out_err:
31339 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31340 /*or_comma=*/false,
31341 /*consume_paren=*/true);
31342 return list;
31345 /* OpenMP 2.5:
31346 ordered
31348 OpenMP 4.5:
31349 ordered ( constant-expression ) */
31351 static tree
31352 cp_parser_omp_clause_ordered (cp_parser *parser,
31353 tree list, location_t location)
31355 tree c, num = NULL_TREE;
31356 HOST_WIDE_INT n;
31358 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
31359 "ordered", location);
31361 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31363 cp_lexer_consume_token (parser->lexer);
31365 num = cp_parser_constant_expression (parser);
31367 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31368 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31369 /*or_comma=*/false,
31370 /*consume_paren=*/true);
31372 if (num == error_mark_node)
31373 return list;
31374 num = fold_non_dependent_expr (num);
31375 if (!tree_fits_shwi_p (num)
31376 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31377 || (n = tree_to_shwi (num)) <= 0
31378 || (int) n != n)
31380 error_at (location,
31381 "ordered argument needs positive constant integer "
31382 "expression");
31383 return list;
31387 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
31388 OMP_CLAUSE_ORDERED_EXPR (c) = num;
31389 OMP_CLAUSE_CHAIN (c) = list;
31390 return c;
31393 /* OpenMP 2.5:
31394 reduction ( reduction-operator : variable-list )
31396 reduction-operator:
31397 One of: + * - & ^ | && ||
31399 OpenMP 3.1:
31401 reduction-operator:
31402 One of: + * - & ^ | && || min max
31404 OpenMP 4.0:
31406 reduction-operator:
31407 One of: + * - & ^ | && ||
31408 id-expression */
31410 static tree
31411 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
31413 enum tree_code code = ERROR_MARK;
31414 tree nlist, c, id = NULL_TREE;
31416 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31417 return list;
31419 switch (cp_lexer_peek_token (parser->lexer)->type)
31421 case CPP_PLUS: code = PLUS_EXPR; break;
31422 case CPP_MULT: code = MULT_EXPR; break;
31423 case CPP_MINUS: code = MINUS_EXPR; break;
31424 case CPP_AND: code = BIT_AND_EXPR; break;
31425 case CPP_XOR: code = BIT_XOR_EXPR; break;
31426 case CPP_OR: code = BIT_IOR_EXPR; break;
31427 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
31428 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
31429 default: break;
31432 if (code != ERROR_MARK)
31433 cp_lexer_consume_token (parser->lexer);
31434 else
31436 bool saved_colon_corrects_to_scope_p;
31437 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31438 parser->colon_corrects_to_scope_p = false;
31439 id = cp_parser_id_expression (parser, /*template_p=*/false,
31440 /*check_dependency_p=*/true,
31441 /*template_p=*/NULL,
31442 /*declarator_p=*/false,
31443 /*optional_p=*/false);
31444 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31445 if (identifier_p (id))
31447 const char *p = IDENTIFIER_POINTER (id);
31449 if (strcmp (p, "min") == 0)
31450 code = MIN_EXPR;
31451 else if (strcmp (p, "max") == 0)
31452 code = MAX_EXPR;
31453 else if (id == ansi_opname (PLUS_EXPR))
31454 code = PLUS_EXPR;
31455 else if (id == ansi_opname (MULT_EXPR))
31456 code = MULT_EXPR;
31457 else if (id == ansi_opname (MINUS_EXPR))
31458 code = MINUS_EXPR;
31459 else if (id == ansi_opname (BIT_AND_EXPR))
31460 code = BIT_AND_EXPR;
31461 else if (id == ansi_opname (BIT_IOR_EXPR))
31462 code = BIT_IOR_EXPR;
31463 else if (id == ansi_opname (BIT_XOR_EXPR))
31464 code = BIT_XOR_EXPR;
31465 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
31466 code = TRUTH_ANDIF_EXPR;
31467 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
31468 code = TRUTH_ORIF_EXPR;
31469 id = omp_reduction_id (code, id, NULL_TREE);
31470 tree scope = parser->scope;
31471 if (scope)
31472 id = build_qualified_name (NULL_TREE, scope, id, false);
31473 parser->scope = NULL_TREE;
31474 parser->qualifying_scope = NULL_TREE;
31475 parser->object_scope = NULL_TREE;
31477 else
31479 error ("invalid reduction-identifier");
31480 resync_fail:
31481 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31482 /*or_comma=*/false,
31483 /*consume_paren=*/true);
31484 return list;
31488 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31489 goto resync_fail;
31491 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
31492 NULL);
31493 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31495 OMP_CLAUSE_REDUCTION_CODE (c) = code;
31496 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
31499 return nlist;
31502 /* OpenMP 2.5:
31503 schedule ( schedule-kind )
31504 schedule ( schedule-kind , expression )
31506 schedule-kind:
31507 static | dynamic | guided | runtime | auto
31509 OpenMP 4.5:
31510 schedule ( schedule-modifier : schedule-kind )
31511 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
31513 schedule-modifier:
31514 simd
31515 monotonic
31516 nonmonotonic */
31518 static tree
31519 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
31521 tree c, t;
31522 int modifiers = 0, nmodifiers = 0;
31524 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31525 return list;
31527 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
31529 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31531 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31532 const char *p = IDENTIFIER_POINTER (id);
31533 if (strcmp ("simd", p) == 0)
31534 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
31535 else if (strcmp ("monotonic", p) == 0)
31536 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
31537 else if (strcmp ("nonmonotonic", p) == 0)
31538 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
31539 else
31540 break;
31541 cp_lexer_consume_token (parser->lexer);
31542 if (nmodifiers++ == 0
31543 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31544 cp_lexer_consume_token (parser->lexer);
31545 else
31547 cp_parser_require (parser, CPP_COLON, RT_COLON);
31548 break;
31552 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31554 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31555 const char *p = IDENTIFIER_POINTER (id);
31557 switch (p[0])
31559 case 'd':
31560 if (strcmp ("dynamic", p) != 0)
31561 goto invalid_kind;
31562 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
31563 break;
31565 case 'g':
31566 if (strcmp ("guided", p) != 0)
31567 goto invalid_kind;
31568 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
31569 break;
31571 case 'r':
31572 if (strcmp ("runtime", p) != 0)
31573 goto invalid_kind;
31574 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
31575 break;
31577 default:
31578 goto invalid_kind;
31581 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
31582 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
31583 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31584 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
31585 else
31586 goto invalid_kind;
31587 cp_lexer_consume_token (parser->lexer);
31589 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
31590 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
31591 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
31592 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
31594 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
31595 "specified");
31596 modifiers = 0;
31599 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31601 cp_token *token;
31602 cp_lexer_consume_token (parser->lexer);
31604 token = cp_lexer_peek_token (parser->lexer);
31605 t = cp_parser_assignment_expression (parser);
31607 if (t == error_mark_node)
31608 goto resync_fail;
31609 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
31610 error_at (token->location, "schedule %<runtime%> does not take "
31611 "a %<chunk_size%> parameter");
31612 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
31613 error_at (token->location, "schedule %<auto%> does not take "
31614 "a %<chunk_size%> parameter");
31615 else
31616 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
31618 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31619 goto resync_fail;
31621 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
31622 goto resync_fail;
31624 OMP_CLAUSE_SCHEDULE_KIND (c)
31625 = (enum omp_clause_schedule_kind)
31626 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
31628 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
31629 OMP_CLAUSE_CHAIN (c) = list;
31630 return c;
31632 invalid_kind:
31633 cp_parser_error (parser, "invalid schedule kind");
31634 resync_fail:
31635 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31636 /*or_comma=*/false,
31637 /*consume_paren=*/true);
31638 return list;
31641 /* OpenMP 3.0:
31642 untied */
31644 static tree
31645 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
31646 tree list, location_t location)
31648 tree c;
31650 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
31652 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
31653 OMP_CLAUSE_CHAIN (c) = list;
31654 return c;
31657 /* OpenMP 4.0:
31658 inbranch
31659 notinbranch */
31661 static tree
31662 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
31663 tree list, location_t location)
31665 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31666 tree c = build_omp_clause (location, code);
31667 OMP_CLAUSE_CHAIN (c) = list;
31668 return c;
31671 /* OpenMP 4.0:
31672 parallel
31674 sections
31675 taskgroup */
31677 static tree
31678 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
31679 enum omp_clause_code code,
31680 tree list, location_t location)
31682 tree c = build_omp_clause (location, code);
31683 OMP_CLAUSE_CHAIN (c) = list;
31684 return c;
31687 /* OpenMP 4.5:
31688 nogroup */
31690 static tree
31691 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
31692 tree list, location_t location)
31694 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
31695 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
31696 OMP_CLAUSE_CHAIN (c) = list;
31697 return c;
31700 /* OpenMP 4.5:
31701 simd
31702 threads */
31704 static tree
31705 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
31706 enum omp_clause_code code,
31707 tree list, location_t location)
31709 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31710 tree c = build_omp_clause (location, code);
31711 OMP_CLAUSE_CHAIN (c) = list;
31712 return c;
31715 /* OpenMP 4.0:
31716 num_teams ( expression ) */
31718 static tree
31719 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
31720 location_t location)
31722 tree t, c;
31724 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31725 return list;
31727 t = cp_parser_expression (parser);
31729 if (t == error_mark_node
31730 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31731 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31732 /*or_comma=*/false,
31733 /*consume_paren=*/true);
31735 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
31736 "num_teams", location);
31738 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
31739 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
31740 OMP_CLAUSE_CHAIN (c) = list;
31742 return c;
31745 /* OpenMP 4.0:
31746 thread_limit ( expression ) */
31748 static tree
31749 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
31750 location_t location)
31752 tree t, c;
31754 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31755 return list;
31757 t = cp_parser_expression (parser);
31759 if (t == error_mark_node
31760 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31761 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31762 /*or_comma=*/false,
31763 /*consume_paren=*/true);
31765 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
31766 "thread_limit", location);
31768 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
31769 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
31770 OMP_CLAUSE_CHAIN (c) = list;
31772 return c;
31775 /* OpenMP 4.0:
31776 aligned ( variable-list )
31777 aligned ( variable-list : constant-expression ) */
31779 static tree
31780 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
31782 tree nlist, c, alignment = NULL_TREE;
31783 bool colon;
31785 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31786 return list;
31788 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
31789 &colon);
31791 if (colon)
31793 alignment = cp_parser_constant_expression (parser);
31795 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31796 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31797 /*or_comma=*/false,
31798 /*consume_paren=*/true);
31800 if (alignment == error_mark_node)
31801 alignment = NULL_TREE;
31804 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31805 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
31807 return nlist;
31810 /* OpenMP 4.0:
31811 linear ( variable-list )
31812 linear ( variable-list : expression )
31814 OpenMP 4.5:
31815 linear ( modifier ( variable-list ) )
31816 linear ( modifier ( variable-list ) : expression ) */
31818 static tree
31819 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
31820 bool is_cilk_simd_fn, bool declare_simd)
31822 tree nlist, c, step = integer_one_node;
31823 bool colon;
31824 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
31826 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31827 return list;
31829 if (!is_cilk_simd_fn
31830 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31832 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31833 const char *p = IDENTIFIER_POINTER (id);
31835 if (strcmp ("ref", p) == 0)
31836 kind = OMP_CLAUSE_LINEAR_REF;
31837 else if (strcmp ("val", p) == 0)
31838 kind = OMP_CLAUSE_LINEAR_VAL;
31839 else if (strcmp ("uval", p) == 0)
31840 kind = OMP_CLAUSE_LINEAR_UVAL;
31841 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
31842 cp_lexer_consume_token (parser->lexer);
31843 else
31844 kind = OMP_CLAUSE_LINEAR_DEFAULT;
31847 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
31848 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
31849 &colon);
31850 else
31852 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
31853 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
31854 if (colon)
31855 cp_parser_require (parser, CPP_COLON, RT_COLON);
31856 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31857 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31858 /*or_comma=*/false,
31859 /*consume_paren=*/true);
31862 if (colon)
31864 step = NULL_TREE;
31865 if (declare_simd
31866 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
31867 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
31869 cp_token *token = cp_lexer_peek_token (parser->lexer);
31870 cp_parser_parse_tentatively (parser);
31871 step = cp_parser_id_expression (parser, /*template_p=*/false,
31872 /*check_dependency_p=*/true,
31873 /*template_p=*/NULL,
31874 /*declarator_p=*/false,
31875 /*optional_p=*/false);
31876 if (step != error_mark_node)
31877 step = cp_parser_lookup_name_simple (parser, step, token->location);
31878 if (step == error_mark_node)
31880 step = NULL_TREE;
31881 cp_parser_abort_tentative_parse (parser);
31883 else if (!cp_parser_parse_definitely (parser))
31884 step = NULL_TREE;
31886 if (!step)
31887 step = cp_parser_expression (parser);
31889 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
31891 sorry ("using parameters for %<linear%> step is not supported yet");
31892 step = integer_one_node;
31894 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31895 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31896 /*or_comma=*/false,
31897 /*consume_paren=*/true);
31899 if (step == error_mark_node)
31900 return list;
31903 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31905 OMP_CLAUSE_LINEAR_STEP (c) = step;
31906 OMP_CLAUSE_LINEAR_KIND (c) = kind;
31909 return nlist;
31912 /* OpenMP 4.0:
31913 safelen ( constant-expression ) */
31915 static tree
31916 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
31917 location_t location)
31919 tree t, c;
31921 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31922 return list;
31924 t = cp_parser_constant_expression (parser);
31926 if (t == error_mark_node
31927 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31928 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31929 /*or_comma=*/false,
31930 /*consume_paren=*/true);
31932 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
31934 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
31935 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
31936 OMP_CLAUSE_CHAIN (c) = list;
31938 return c;
31941 /* OpenMP 4.0:
31942 simdlen ( constant-expression ) */
31944 static tree
31945 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
31946 location_t location)
31948 tree t, c;
31950 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31951 return list;
31953 t = cp_parser_constant_expression (parser);
31955 if (t == error_mark_node
31956 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31957 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31958 /*or_comma=*/false,
31959 /*consume_paren=*/true);
31961 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
31963 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
31964 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
31965 OMP_CLAUSE_CHAIN (c) = list;
31967 return c;
31970 /* OpenMP 4.5:
31971 vec:
31972 identifier [+/- integer]
31973 vec , identifier [+/- integer]
31976 static tree
31977 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
31978 tree list)
31980 tree vec = NULL;
31982 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31984 cp_parser_error (parser, "expected identifier");
31985 return list;
31988 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31990 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
31991 tree t, identifier = cp_parser_identifier (parser);
31992 tree addend = NULL;
31994 if (identifier == error_mark_node)
31995 t = error_mark_node;
31996 else
31998 t = cp_parser_lookup_name_simple
31999 (parser, identifier,
32000 cp_lexer_peek_token (parser->lexer)->location);
32001 if (t == error_mark_node)
32002 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
32003 id_loc);
32006 bool neg = false;
32007 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
32008 neg = true;
32009 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
32011 addend = integer_zero_node;
32012 goto add_to_vector;
32014 cp_lexer_consume_token (parser->lexer);
32016 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
32018 cp_parser_error (parser, "expected integer");
32019 return list;
32022 addend = cp_lexer_peek_token (parser->lexer)->u.value;
32023 if (TREE_CODE (addend) != INTEGER_CST)
32025 cp_parser_error (parser, "expected integer");
32026 return list;
32028 cp_lexer_consume_token (parser->lexer);
32030 add_to_vector:
32031 if (t != error_mark_node)
32033 vec = tree_cons (addend, t, vec);
32034 if (neg)
32035 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
32038 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
32039 break;
32041 cp_lexer_consume_token (parser->lexer);
32044 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
32046 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
32047 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
32048 OMP_CLAUSE_DECL (u) = nreverse (vec);
32049 OMP_CLAUSE_CHAIN (u) = list;
32050 return u;
32052 return list;
32055 /* OpenMP 4.0:
32056 depend ( depend-kind : variable-list )
32058 depend-kind:
32059 in | out | inout
32061 OpenMP 4.5:
32062 depend ( source )
32064 depend ( sink : vec ) */
32066 static tree
32067 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
32069 tree nlist, c;
32070 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
32072 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32073 return list;
32075 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32077 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32078 const char *p = IDENTIFIER_POINTER (id);
32080 if (strcmp ("in", p) == 0)
32081 kind = OMP_CLAUSE_DEPEND_IN;
32082 else if (strcmp ("inout", p) == 0)
32083 kind = OMP_CLAUSE_DEPEND_INOUT;
32084 else if (strcmp ("out", p) == 0)
32085 kind = OMP_CLAUSE_DEPEND_OUT;
32086 else if (strcmp ("source", p) == 0)
32087 kind = OMP_CLAUSE_DEPEND_SOURCE;
32088 else if (strcmp ("sink", p) == 0)
32089 kind = OMP_CLAUSE_DEPEND_SINK;
32090 else
32091 goto invalid_kind;
32093 else
32094 goto invalid_kind;
32096 cp_lexer_consume_token (parser->lexer);
32098 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
32100 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
32101 OMP_CLAUSE_DEPEND_KIND (c) = kind;
32102 OMP_CLAUSE_DECL (c) = NULL_TREE;
32103 OMP_CLAUSE_CHAIN (c) = list;
32104 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32105 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32106 /*or_comma=*/false,
32107 /*consume_paren=*/true);
32108 return c;
32111 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32112 goto resync_fail;
32114 if (kind == OMP_CLAUSE_DEPEND_SINK)
32115 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
32116 else
32118 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
32119 list, NULL);
32121 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32122 OMP_CLAUSE_DEPEND_KIND (c) = kind;
32124 return nlist;
32126 invalid_kind:
32127 cp_parser_error (parser, "invalid depend kind");
32128 resync_fail:
32129 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32130 /*or_comma=*/false,
32131 /*consume_paren=*/true);
32132 return list;
32135 /* OpenMP 4.0:
32136 map ( map-kind : variable-list )
32137 map ( variable-list )
32139 map-kind:
32140 alloc | to | from | tofrom
32142 OpenMP 4.5:
32143 map-kind:
32144 alloc | to | from | tofrom | release | delete
32146 map ( always [,] map-kind: variable-list ) */
32148 static tree
32149 cp_parser_omp_clause_map (cp_parser *parser, tree list)
32151 tree nlist, c;
32152 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
32153 bool always = false;
32155 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32156 return list;
32158 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32160 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32161 const char *p = IDENTIFIER_POINTER (id);
32163 if (strcmp ("always", p) == 0)
32165 int nth = 2;
32166 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
32167 nth++;
32168 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
32169 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
32170 == RID_DELETE))
32171 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
32172 == CPP_COLON))
32174 always = true;
32175 cp_lexer_consume_token (parser->lexer);
32176 if (nth == 3)
32177 cp_lexer_consume_token (parser->lexer);
32182 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32183 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
32185 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32186 const char *p = IDENTIFIER_POINTER (id);
32188 if (strcmp ("alloc", p) == 0)
32189 kind = GOMP_MAP_ALLOC;
32190 else if (strcmp ("to", p) == 0)
32191 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
32192 else if (strcmp ("from", p) == 0)
32193 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
32194 else if (strcmp ("tofrom", p) == 0)
32195 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
32196 else if (strcmp ("release", p) == 0)
32197 kind = GOMP_MAP_RELEASE;
32198 else
32200 cp_parser_error (parser, "invalid map kind");
32201 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32202 /*or_comma=*/false,
32203 /*consume_paren=*/true);
32204 return list;
32206 cp_lexer_consume_token (parser->lexer);
32207 cp_lexer_consume_token (parser->lexer);
32209 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
32210 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
32212 kind = GOMP_MAP_DELETE;
32213 cp_lexer_consume_token (parser->lexer);
32214 cp_lexer_consume_token (parser->lexer);
32217 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
32218 NULL);
32220 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32221 OMP_CLAUSE_SET_MAP_KIND (c, kind);
32223 return nlist;
32226 /* OpenMP 4.0:
32227 device ( expression ) */
32229 static tree
32230 cp_parser_omp_clause_device (cp_parser *parser, tree list,
32231 location_t location)
32233 tree t, c;
32235 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32236 return list;
32238 t = cp_parser_expression (parser);
32240 if (t == error_mark_node
32241 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32242 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32243 /*or_comma=*/false,
32244 /*consume_paren=*/true);
32246 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
32247 "device", location);
32249 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
32250 OMP_CLAUSE_DEVICE_ID (c) = t;
32251 OMP_CLAUSE_CHAIN (c) = list;
32253 return c;
32256 /* OpenMP 4.0:
32257 dist_schedule ( static )
32258 dist_schedule ( static , expression ) */
32260 static tree
32261 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
32262 location_t location)
32264 tree c, t;
32266 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32267 return list;
32269 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
32271 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32272 goto invalid_kind;
32273 cp_lexer_consume_token (parser->lexer);
32275 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32277 cp_lexer_consume_token (parser->lexer);
32279 t = cp_parser_assignment_expression (parser);
32281 if (t == error_mark_node)
32282 goto resync_fail;
32283 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
32285 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32286 goto resync_fail;
32288 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32289 goto resync_fail;
32291 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
32292 location);
32293 OMP_CLAUSE_CHAIN (c) = list;
32294 return c;
32296 invalid_kind:
32297 cp_parser_error (parser, "invalid dist_schedule kind");
32298 resync_fail:
32299 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32300 /*or_comma=*/false,
32301 /*consume_paren=*/true);
32302 return list;
32305 /* OpenMP 4.0:
32306 proc_bind ( proc-bind-kind )
32308 proc-bind-kind:
32309 master | close | spread */
32311 static tree
32312 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
32313 location_t location)
32315 tree c;
32316 enum omp_clause_proc_bind_kind kind;
32318 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32319 return list;
32321 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32323 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32324 const char *p = IDENTIFIER_POINTER (id);
32326 if (strcmp ("master", p) == 0)
32327 kind = OMP_CLAUSE_PROC_BIND_MASTER;
32328 else if (strcmp ("close", p) == 0)
32329 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
32330 else if (strcmp ("spread", p) == 0)
32331 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
32332 else
32333 goto invalid_kind;
32335 else
32336 goto invalid_kind;
32338 cp_lexer_consume_token (parser->lexer);
32339 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32340 goto resync_fail;
32342 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
32343 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
32344 location);
32345 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
32346 OMP_CLAUSE_CHAIN (c) = list;
32347 return c;
32349 invalid_kind:
32350 cp_parser_error (parser, "invalid depend kind");
32351 resync_fail:
32352 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32353 /*or_comma=*/false,
32354 /*consume_paren=*/true);
32355 return list;
32358 /* OpenACC:
32359 async [( int-expr )] */
32361 static tree
32362 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
32364 tree c, t;
32365 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32367 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
32369 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32371 cp_lexer_consume_token (parser->lexer);
32373 t = cp_parser_expression (parser);
32374 if (t == error_mark_node
32375 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32376 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32377 /*or_comma=*/false,
32378 /*consume_paren=*/true);
32381 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
32383 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
32384 OMP_CLAUSE_ASYNC_EXPR (c) = t;
32385 OMP_CLAUSE_CHAIN (c) = list;
32386 list = c;
32388 return list;
32391 /* Parse all OpenACC clauses. The set clauses allowed by the directive
32392 is a bitmask in MASK. Return the list of clauses found. */
32394 static tree
32395 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
32396 const char *where, cp_token *pragma_tok,
32397 bool finish_p = true)
32399 tree clauses = NULL;
32400 bool first = true;
32402 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32404 location_t here;
32405 pragma_omp_clause c_kind;
32406 omp_clause_code code;
32407 const char *c_name;
32408 tree prev = clauses;
32410 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32411 cp_lexer_consume_token (parser->lexer);
32413 here = cp_lexer_peek_token (parser->lexer)->location;
32414 c_kind = cp_parser_omp_clause_name (parser);
32416 switch (c_kind)
32418 case PRAGMA_OACC_CLAUSE_ASYNC:
32419 clauses = cp_parser_oacc_clause_async (parser, clauses);
32420 c_name = "async";
32421 break;
32422 case PRAGMA_OACC_CLAUSE_AUTO:
32423 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
32424 clauses, here);
32425 c_name = "auto";
32426 break;
32427 case PRAGMA_OACC_CLAUSE_COLLAPSE:
32428 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
32429 c_name = "collapse";
32430 break;
32431 case PRAGMA_OACC_CLAUSE_COPY:
32432 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32433 c_name = "copy";
32434 break;
32435 case PRAGMA_OACC_CLAUSE_COPYIN:
32436 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32437 c_name = "copyin";
32438 break;
32439 case PRAGMA_OACC_CLAUSE_COPYOUT:
32440 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32441 c_name = "copyout";
32442 break;
32443 case PRAGMA_OACC_CLAUSE_CREATE:
32444 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32445 c_name = "create";
32446 break;
32447 case PRAGMA_OACC_CLAUSE_DELETE:
32448 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32449 c_name = "delete";
32450 break;
32451 case PRAGMA_OMP_CLAUSE_DEFAULT:
32452 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
32453 c_name = "default";
32454 break;
32455 case PRAGMA_OACC_CLAUSE_DEVICE:
32456 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32457 c_name = "device";
32458 break;
32459 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
32460 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
32461 c_name = "deviceptr";
32462 break;
32463 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
32464 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32465 c_name = "device_resident";
32466 break;
32467 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
32468 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32469 clauses);
32470 c_name = "firstprivate";
32471 break;
32472 case PRAGMA_OACC_CLAUSE_GANG:
32473 c_name = "gang";
32474 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
32475 c_name, clauses);
32476 break;
32477 case PRAGMA_OACC_CLAUSE_HOST:
32478 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32479 c_name = "host";
32480 break;
32481 case PRAGMA_OACC_CLAUSE_IF:
32482 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
32483 c_name = "if";
32484 break;
32485 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
32486 clauses = cp_parser_oacc_simple_clause (parser,
32487 OMP_CLAUSE_INDEPENDENT,
32488 clauses, here);
32489 c_name = "independent";
32490 break;
32491 case PRAGMA_OACC_CLAUSE_LINK:
32492 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32493 c_name = "link";
32494 break;
32495 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
32496 code = OMP_CLAUSE_NUM_GANGS;
32497 c_name = "num_gangs";
32498 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32499 clauses);
32500 break;
32501 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
32502 c_name = "num_workers";
32503 code = OMP_CLAUSE_NUM_WORKERS;
32504 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32505 clauses);
32506 break;
32507 case PRAGMA_OACC_CLAUSE_PRESENT:
32508 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32509 c_name = "present";
32510 break;
32511 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
32512 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32513 c_name = "present_or_copy";
32514 break;
32515 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
32516 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32517 c_name = "present_or_copyin";
32518 break;
32519 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
32520 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32521 c_name = "present_or_copyout";
32522 break;
32523 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
32524 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32525 c_name = "present_or_create";
32526 break;
32527 case PRAGMA_OACC_CLAUSE_PRIVATE:
32528 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
32529 clauses);
32530 c_name = "private";
32531 break;
32532 case PRAGMA_OACC_CLAUSE_REDUCTION:
32533 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32534 c_name = "reduction";
32535 break;
32536 case PRAGMA_OACC_CLAUSE_SELF:
32537 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32538 c_name = "self";
32539 break;
32540 case PRAGMA_OACC_CLAUSE_SEQ:
32541 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
32542 clauses, here);
32543 c_name = "seq";
32544 break;
32545 case PRAGMA_OACC_CLAUSE_TILE:
32546 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
32547 c_name = "tile";
32548 break;
32549 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
32550 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
32551 clauses);
32552 c_name = "use_device";
32553 break;
32554 case PRAGMA_OACC_CLAUSE_VECTOR:
32555 c_name = "vector";
32556 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
32557 c_name, clauses);
32558 break;
32559 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
32560 c_name = "vector_length";
32561 code = OMP_CLAUSE_VECTOR_LENGTH;
32562 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32563 clauses);
32564 break;
32565 case PRAGMA_OACC_CLAUSE_WAIT:
32566 clauses = cp_parser_oacc_clause_wait (parser, clauses);
32567 c_name = "wait";
32568 break;
32569 case PRAGMA_OACC_CLAUSE_WORKER:
32570 c_name = "worker";
32571 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
32572 c_name, clauses);
32573 break;
32574 default:
32575 cp_parser_error (parser, "expected %<#pragma acc%> clause");
32576 goto saw_error;
32579 first = false;
32581 if (((mask >> c_kind) & 1) == 0)
32583 /* Remove the invalid clause(s) from the list to avoid
32584 confusing the rest of the compiler. */
32585 clauses = prev;
32586 error_at (here, "%qs is not valid for %qs", c_name, where);
32590 saw_error:
32591 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32593 if (finish_p)
32594 return finish_omp_clauses (clauses, C_ORT_ACC);
32596 return clauses;
32599 /* Parse all OpenMP clauses. The set clauses allowed by the directive
32600 is a bitmask in MASK. Return the list of clauses found; the result
32601 of clause default goes in *pdefault. */
32603 static tree
32604 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
32605 const char *where, cp_token *pragma_tok,
32606 bool finish_p = true)
32608 tree clauses = NULL;
32609 bool first = true;
32610 cp_token *token = NULL;
32612 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32614 pragma_omp_clause c_kind;
32615 const char *c_name;
32616 tree prev = clauses;
32618 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32619 cp_lexer_consume_token (parser->lexer);
32621 token = cp_lexer_peek_token (parser->lexer);
32622 c_kind = cp_parser_omp_clause_name (parser);
32624 switch (c_kind)
32626 case PRAGMA_OMP_CLAUSE_COLLAPSE:
32627 clauses = cp_parser_omp_clause_collapse (parser, clauses,
32628 token->location);
32629 c_name = "collapse";
32630 break;
32631 case PRAGMA_OMP_CLAUSE_COPYIN:
32632 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
32633 c_name = "copyin";
32634 break;
32635 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
32636 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
32637 clauses);
32638 c_name = "copyprivate";
32639 break;
32640 case PRAGMA_OMP_CLAUSE_DEFAULT:
32641 clauses = cp_parser_omp_clause_default (parser, clauses,
32642 token->location, false);
32643 c_name = "default";
32644 break;
32645 case PRAGMA_OMP_CLAUSE_FINAL:
32646 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
32647 c_name = "final";
32648 break;
32649 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
32650 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32651 clauses);
32652 c_name = "firstprivate";
32653 break;
32654 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
32655 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
32656 token->location);
32657 c_name = "grainsize";
32658 break;
32659 case PRAGMA_OMP_CLAUSE_HINT:
32660 clauses = cp_parser_omp_clause_hint (parser, clauses,
32661 token->location);
32662 c_name = "hint";
32663 break;
32664 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
32665 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
32666 token->location);
32667 c_name = "defaultmap";
32668 break;
32669 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
32670 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
32671 clauses);
32672 c_name = "use_device_ptr";
32673 break;
32674 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
32675 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
32676 clauses);
32677 c_name = "is_device_ptr";
32678 break;
32679 case PRAGMA_OMP_CLAUSE_IF:
32680 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
32681 true);
32682 c_name = "if";
32683 break;
32684 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
32685 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
32686 clauses);
32687 c_name = "lastprivate";
32688 break;
32689 case PRAGMA_OMP_CLAUSE_MERGEABLE:
32690 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
32691 token->location);
32692 c_name = "mergeable";
32693 break;
32694 case PRAGMA_OMP_CLAUSE_NOWAIT:
32695 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
32696 c_name = "nowait";
32697 break;
32698 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
32699 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
32700 token->location);
32701 c_name = "num_tasks";
32702 break;
32703 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
32704 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
32705 token->location);
32706 c_name = "num_threads";
32707 break;
32708 case PRAGMA_OMP_CLAUSE_ORDERED:
32709 clauses = cp_parser_omp_clause_ordered (parser, clauses,
32710 token->location);
32711 c_name = "ordered";
32712 break;
32713 case PRAGMA_OMP_CLAUSE_PRIORITY:
32714 clauses = cp_parser_omp_clause_priority (parser, clauses,
32715 token->location);
32716 c_name = "priority";
32717 break;
32718 case PRAGMA_OMP_CLAUSE_PRIVATE:
32719 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
32720 clauses);
32721 c_name = "private";
32722 break;
32723 case PRAGMA_OMP_CLAUSE_REDUCTION:
32724 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32725 c_name = "reduction";
32726 break;
32727 case PRAGMA_OMP_CLAUSE_SCHEDULE:
32728 clauses = cp_parser_omp_clause_schedule (parser, clauses,
32729 token->location);
32730 c_name = "schedule";
32731 break;
32732 case PRAGMA_OMP_CLAUSE_SHARED:
32733 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
32734 clauses);
32735 c_name = "shared";
32736 break;
32737 case PRAGMA_OMP_CLAUSE_UNTIED:
32738 clauses = cp_parser_omp_clause_untied (parser, clauses,
32739 token->location);
32740 c_name = "untied";
32741 break;
32742 case PRAGMA_OMP_CLAUSE_INBRANCH:
32743 case PRAGMA_CILK_CLAUSE_MASK:
32744 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
32745 clauses, token->location);
32746 c_name = "inbranch";
32747 break;
32748 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
32749 case PRAGMA_CILK_CLAUSE_NOMASK:
32750 clauses = cp_parser_omp_clause_branch (parser,
32751 OMP_CLAUSE_NOTINBRANCH,
32752 clauses, token->location);
32753 c_name = "notinbranch";
32754 break;
32755 case PRAGMA_OMP_CLAUSE_PARALLEL:
32756 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
32757 clauses, token->location);
32758 c_name = "parallel";
32759 if (!first)
32761 clause_not_first:
32762 error_at (token->location, "%qs must be the first clause of %qs",
32763 c_name, where);
32764 clauses = prev;
32766 break;
32767 case PRAGMA_OMP_CLAUSE_FOR:
32768 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
32769 clauses, token->location);
32770 c_name = "for";
32771 if (!first)
32772 goto clause_not_first;
32773 break;
32774 case PRAGMA_OMP_CLAUSE_SECTIONS:
32775 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
32776 clauses, token->location);
32777 c_name = "sections";
32778 if (!first)
32779 goto clause_not_first;
32780 break;
32781 case PRAGMA_OMP_CLAUSE_TASKGROUP:
32782 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
32783 clauses, token->location);
32784 c_name = "taskgroup";
32785 if (!first)
32786 goto clause_not_first;
32787 break;
32788 case PRAGMA_OMP_CLAUSE_LINK:
32789 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
32790 c_name = "to";
32791 break;
32792 case PRAGMA_OMP_CLAUSE_TO:
32793 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
32794 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
32795 clauses);
32796 else
32797 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
32798 c_name = "to";
32799 break;
32800 case PRAGMA_OMP_CLAUSE_FROM:
32801 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
32802 c_name = "from";
32803 break;
32804 case PRAGMA_OMP_CLAUSE_UNIFORM:
32805 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
32806 clauses);
32807 c_name = "uniform";
32808 break;
32809 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
32810 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
32811 token->location);
32812 c_name = "num_teams";
32813 break;
32814 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
32815 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
32816 token->location);
32817 c_name = "thread_limit";
32818 break;
32819 case PRAGMA_OMP_CLAUSE_ALIGNED:
32820 clauses = cp_parser_omp_clause_aligned (parser, clauses);
32821 c_name = "aligned";
32822 break;
32823 case PRAGMA_OMP_CLAUSE_LINEAR:
32825 bool cilk_simd_fn = false, declare_simd = false;
32826 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
32827 cilk_simd_fn = true;
32828 else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
32829 declare_simd = true;
32830 clauses = cp_parser_omp_clause_linear (parser, clauses,
32831 cilk_simd_fn, declare_simd);
32833 c_name = "linear";
32834 break;
32835 case PRAGMA_OMP_CLAUSE_DEPEND:
32836 clauses = cp_parser_omp_clause_depend (parser, clauses,
32837 token->location);
32838 c_name = "depend";
32839 break;
32840 case PRAGMA_OMP_CLAUSE_MAP:
32841 clauses = cp_parser_omp_clause_map (parser, clauses);
32842 c_name = "map";
32843 break;
32844 case PRAGMA_OMP_CLAUSE_DEVICE:
32845 clauses = cp_parser_omp_clause_device (parser, clauses,
32846 token->location);
32847 c_name = "device";
32848 break;
32849 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
32850 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
32851 token->location);
32852 c_name = "dist_schedule";
32853 break;
32854 case PRAGMA_OMP_CLAUSE_PROC_BIND:
32855 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
32856 token->location);
32857 c_name = "proc_bind";
32858 break;
32859 case PRAGMA_OMP_CLAUSE_SAFELEN:
32860 clauses = cp_parser_omp_clause_safelen (parser, clauses,
32861 token->location);
32862 c_name = "safelen";
32863 break;
32864 case PRAGMA_OMP_CLAUSE_SIMDLEN:
32865 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
32866 token->location);
32867 c_name = "simdlen";
32868 break;
32869 case PRAGMA_OMP_CLAUSE_NOGROUP:
32870 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
32871 token->location);
32872 c_name = "nogroup";
32873 break;
32874 case PRAGMA_OMP_CLAUSE_THREADS:
32875 clauses
32876 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
32877 clauses, token->location);
32878 c_name = "threads";
32879 break;
32880 case PRAGMA_OMP_CLAUSE_SIMD:
32881 clauses
32882 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
32883 clauses, token->location);
32884 c_name = "simd";
32885 break;
32886 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
32887 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
32888 c_name = "simdlen";
32889 break;
32890 default:
32891 cp_parser_error (parser, "expected %<#pragma omp%> clause");
32892 goto saw_error;
32895 first = false;
32897 if (((mask >> c_kind) & 1) == 0)
32899 /* Remove the invalid clause(s) from the list to avoid
32900 confusing the rest of the compiler. */
32901 clauses = prev;
32902 error_at (token->location, "%qs is not valid for %qs", c_name, where);
32905 saw_error:
32906 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
32907 no reason to skip to the end. */
32908 if (!(flag_cilkplus && pragma_tok == NULL))
32909 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32910 if (finish_p)
32912 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
32913 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
32914 else
32915 return finish_omp_clauses (clauses, C_ORT_OMP);
32917 return clauses;
32920 /* OpenMP 2.5:
32921 structured-block:
32922 statement
32924 In practice, we're also interested in adding the statement to an
32925 outer node. So it is convenient if we work around the fact that
32926 cp_parser_statement calls add_stmt. */
32928 static unsigned
32929 cp_parser_begin_omp_structured_block (cp_parser *parser)
32931 unsigned save = parser->in_statement;
32933 /* Only move the values to IN_OMP_BLOCK if they weren't false.
32934 This preserves the "not within loop or switch" style error messages
32935 for nonsense cases like
32936 void foo() {
32937 #pragma omp single
32938 break;
32941 if (parser->in_statement)
32942 parser->in_statement = IN_OMP_BLOCK;
32944 return save;
32947 static void
32948 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
32950 parser->in_statement = save;
32953 static tree
32954 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
32956 tree stmt = begin_omp_structured_block ();
32957 unsigned int save = cp_parser_begin_omp_structured_block (parser);
32959 cp_parser_statement (parser, NULL_TREE, false, if_p);
32961 cp_parser_end_omp_structured_block (parser, save);
32962 return finish_omp_structured_block (stmt);
32965 /* OpenMP 2.5:
32966 # pragma omp atomic new-line
32967 expression-stmt
32969 expression-stmt:
32970 x binop= expr | x++ | ++x | x-- | --x
32971 binop:
32972 +, *, -, /, &, ^, |, <<, >>
32974 where x is an lvalue expression with scalar type.
32976 OpenMP 3.1:
32977 # pragma omp atomic new-line
32978 update-stmt
32980 # pragma omp atomic read new-line
32981 read-stmt
32983 # pragma omp atomic write new-line
32984 write-stmt
32986 # pragma omp atomic update new-line
32987 update-stmt
32989 # pragma omp atomic capture new-line
32990 capture-stmt
32992 # pragma omp atomic capture new-line
32993 capture-block
32995 read-stmt:
32996 v = x
32997 write-stmt:
32998 x = expr
32999 update-stmt:
33000 expression-stmt | x = x binop expr
33001 capture-stmt:
33002 v = expression-stmt
33003 capture-block:
33004 { v = x; update-stmt; } | { update-stmt; v = x; }
33006 OpenMP 4.0:
33007 update-stmt:
33008 expression-stmt | x = x binop expr | x = expr binop x
33009 capture-stmt:
33010 v = update-stmt
33011 capture-block:
33012 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
33014 where x and v are lvalue expressions with scalar type. */
33016 static void
33017 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
33019 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
33020 tree rhs1 = NULL_TREE, orig_lhs;
33021 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
33022 bool structured_block = false;
33023 bool seq_cst = false;
33025 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33027 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33028 const char *p = IDENTIFIER_POINTER (id);
33030 if (!strcmp (p, "seq_cst"))
33032 seq_cst = true;
33033 cp_lexer_consume_token (parser->lexer);
33034 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33035 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33036 cp_lexer_consume_token (parser->lexer);
33039 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33041 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33042 const char *p = IDENTIFIER_POINTER (id);
33044 if (!strcmp (p, "read"))
33045 code = OMP_ATOMIC_READ;
33046 else if (!strcmp (p, "write"))
33047 code = NOP_EXPR;
33048 else if (!strcmp (p, "update"))
33049 code = OMP_ATOMIC;
33050 else if (!strcmp (p, "capture"))
33051 code = OMP_ATOMIC_CAPTURE_NEW;
33052 else
33053 p = NULL;
33054 if (p)
33055 cp_lexer_consume_token (parser->lexer);
33057 if (!seq_cst)
33059 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33060 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33061 cp_lexer_consume_token (parser->lexer);
33063 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33065 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33066 const char *p = IDENTIFIER_POINTER (id);
33068 if (!strcmp (p, "seq_cst"))
33070 seq_cst = true;
33071 cp_lexer_consume_token (parser->lexer);
33075 cp_parser_require_pragma_eol (parser, pragma_tok);
33077 switch (code)
33079 case OMP_ATOMIC_READ:
33080 case NOP_EXPR: /* atomic write */
33081 v = cp_parser_unary_expression (parser);
33082 if (v == error_mark_node)
33083 goto saw_error;
33084 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33085 goto saw_error;
33086 if (code == NOP_EXPR)
33087 lhs = cp_parser_expression (parser);
33088 else
33089 lhs = cp_parser_unary_expression (parser);
33090 if (lhs == error_mark_node)
33091 goto saw_error;
33092 if (code == NOP_EXPR)
33094 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
33095 opcode. */
33096 code = OMP_ATOMIC;
33097 rhs = lhs;
33098 lhs = v;
33099 v = NULL_TREE;
33101 goto done;
33102 case OMP_ATOMIC_CAPTURE_NEW:
33103 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33105 cp_lexer_consume_token (parser->lexer);
33106 structured_block = true;
33108 else
33110 v = cp_parser_unary_expression (parser);
33111 if (v == error_mark_node)
33112 goto saw_error;
33113 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33114 goto saw_error;
33116 default:
33117 break;
33120 restart:
33121 lhs = cp_parser_unary_expression (parser);
33122 orig_lhs = lhs;
33123 switch (TREE_CODE (lhs))
33125 case ERROR_MARK:
33126 goto saw_error;
33128 case POSTINCREMENT_EXPR:
33129 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
33130 code = OMP_ATOMIC_CAPTURE_OLD;
33131 /* FALLTHROUGH */
33132 case PREINCREMENT_EXPR:
33133 lhs = TREE_OPERAND (lhs, 0);
33134 opcode = PLUS_EXPR;
33135 rhs = integer_one_node;
33136 break;
33138 case POSTDECREMENT_EXPR:
33139 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
33140 code = OMP_ATOMIC_CAPTURE_OLD;
33141 /* FALLTHROUGH */
33142 case PREDECREMENT_EXPR:
33143 lhs = TREE_OPERAND (lhs, 0);
33144 opcode = MINUS_EXPR;
33145 rhs = integer_one_node;
33146 break;
33148 case COMPOUND_EXPR:
33149 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
33150 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
33151 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
33152 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
33153 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
33154 (TREE_OPERAND (lhs, 1), 0), 0)))
33155 == BOOLEAN_TYPE)
33156 /* Undo effects of boolean_increment for post {in,de}crement. */
33157 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
33158 /* FALLTHRU */
33159 case MODIFY_EXPR:
33160 if (TREE_CODE (lhs) == MODIFY_EXPR
33161 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
33163 /* Undo effects of boolean_increment. */
33164 if (integer_onep (TREE_OPERAND (lhs, 1)))
33166 /* This is pre or post increment. */
33167 rhs = TREE_OPERAND (lhs, 1);
33168 lhs = TREE_OPERAND (lhs, 0);
33169 opcode = NOP_EXPR;
33170 if (code == OMP_ATOMIC_CAPTURE_NEW
33171 && !structured_block
33172 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
33173 code = OMP_ATOMIC_CAPTURE_OLD;
33174 break;
33177 /* FALLTHRU */
33178 default:
33179 switch (cp_lexer_peek_token (parser->lexer)->type)
33181 case CPP_MULT_EQ:
33182 opcode = MULT_EXPR;
33183 break;
33184 case CPP_DIV_EQ:
33185 opcode = TRUNC_DIV_EXPR;
33186 break;
33187 case CPP_PLUS_EQ:
33188 opcode = PLUS_EXPR;
33189 break;
33190 case CPP_MINUS_EQ:
33191 opcode = MINUS_EXPR;
33192 break;
33193 case CPP_LSHIFT_EQ:
33194 opcode = LSHIFT_EXPR;
33195 break;
33196 case CPP_RSHIFT_EQ:
33197 opcode = RSHIFT_EXPR;
33198 break;
33199 case CPP_AND_EQ:
33200 opcode = BIT_AND_EXPR;
33201 break;
33202 case CPP_OR_EQ:
33203 opcode = BIT_IOR_EXPR;
33204 break;
33205 case CPP_XOR_EQ:
33206 opcode = BIT_XOR_EXPR;
33207 break;
33208 case CPP_EQ:
33209 enum cp_parser_prec oprec;
33210 cp_token *token;
33211 cp_lexer_consume_token (parser->lexer);
33212 cp_parser_parse_tentatively (parser);
33213 rhs1 = cp_parser_simple_cast_expression (parser);
33214 if (rhs1 == error_mark_node)
33216 cp_parser_abort_tentative_parse (parser);
33217 cp_parser_simple_cast_expression (parser);
33218 goto saw_error;
33220 token = cp_lexer_peek_token (parser->lexer);
33221 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
33223 cp_parser_abort_tentative_parse (parser);
33224 cp_parser_parse_tentatively (parser);
33225 rhs = cp_parser_binary_expression (parser, false, true,
33226 PREC_NOT_OPERATOR, NULL);
33227 if (rhs == error_mark_node)
33229 cp_parser_abort_tentative_parse (parser);
33230 cp_parser_binary_expression (parser, false, true,
33231 PREC_NOT_OPERATOR, NULL);
33232 goto saw_error;
33234 switch (TREE_CODE (rhs))
33236 case MULT_EXPR:
33237 case TRUNC_DIV_EXPR:
33238 case RDIV_EXPR:
33239 case PLUS_EXPR:
33240 case MINUS_EXPR:
33241 case LSHIFT_EXPR:
33242 case RSHIFT_EXPR:
33243 case BIT_AND_EXPR:
33244 case BIT_IOR_EXPR:
33245 case BIT_XOR_EXPR:
33246 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
33248 if (cp_parser_parse_definitely (parser))
33250 opcode = TREE_CODE (rhs);
33251 rhs1 = TREE_OPERAND (rhs, 0);
33252 rhs = TREE_OPERAND (rhs, 1);
33253 goto stmt_done;
33255 else
33256 goto saw_error;
33258 break;
33259 default:
33260 break;
33262 cp_parser_abort_tentative_parse (parser);
33263 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
33265 rhs = cp_parser_expression (parser);
33266 if (rhs == error_mark_node)
33267 goto saw_error;
33268 opcode = NOP_EXPR;
33269 rhs1 = NULL_TREE;
33270 goto stmt_done;
33272 cp_parser_error (parser,
33273 "invalid form of %<#pragma omp atomic%>");
33274 goto saw_error;
33276 if (!cp_parser_parse_definitely (parser))
33277 goto saw_error;
33278 switch (token->type)
33280 case CPP_SEMICOLON:
33281 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33283 code = OMP_ATOMIC_CAPTURE_OLD;
33284 v = lhs;
33285 lhs = NULL_TREE;
33286 lhs1 = rhs1;
33287 rhs1 = NULL_TREE;
33288 cp_lexer_consume_token (parser->lexer);
33289 goto restart;
33291 else if (structured_block)
33293 opcode = NOP_EXPR;
33294 rhs = rhs1;
33295 rhs1 = NULL_TREE;
33296 goto stmt_done;
33298 cp_parser_error (parser,
33299 "invalid form of %<#pragma omp atomic%>");
33300 goto saw_error;
33301 case CPP_MULT:
33302 opcode = MULT_EXPR;
33303 break;
33304 case CPP_DIV:
33305 opcode = TRUNC_DIV_EXPR;
33306 break;
33307 case CPP_PLUS:
33308 opcode = PLUS_EXPR;
33309 break;
33310 case CPP_MINUS:
33311 opcode = MINUS_EXPR;
33312 break;
33313 case CPP_LSHIFT:
33314 opcode = LSHIFT_EXPR;
33315 break;
33316 case CPP_RSHIFT:
33317 opcode = RSHIFT_EXPR;
33318 break;
33319 case CPP_AND:
33320 opcode = BIT_AND_EXPR;
33321 break;
33322 case CPP_OR:
33323 opcode = BIT_IOR_EXPR;
33324 break;
33325 case CPP_XOR:
33326 opcode = BIT_XOR_EXPR;
33327 break;
33328 default:
33329 cp_parser_error (parser,
33330 "invalid operator for %<#pragma omp atomic%>");
33331 goto saw_error;
33333 oprec = TOKEN_PRECEDENCE (token);
33334 gcc_assert (oprec != PREC_NOT_OPERATOR);
33335 if (commutative_tree_code (opcode))
33336 oprec = (enum cp_parser_prec) (oprec - 1);
33337 cp_lexer_consume_token (parser->lexer);
33338 rhs = cp_parser_binary_expression (parser, false, false,
33339 oprec, NULL);
33340 if (rhs == error_mark_node)
33341 goto saw_error;
33342 goto stmt_done;
33343 /* FALLTHROUGH */
33344 default:
33345 cp_parser_error (parser,
33346 "invalid operator for %<#pragma omp atomic%>");
33347 goto saw_error;
33349 cp_lexer_consume_token (parser->lexer);
33351 rhs = cp_parser_expression (parser);
33352 if (rhs == error_mark_node)
33353 goto saw_error;
33354 break;
33356 stmt_done:
33357 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33359 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
33360 goto saw_error;
33361 v = cp_parser_unary_expression (parser);
33362 if (v == error_mark_node)
33363 goto saw_error;
33364 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33365 goto saw_error;
33366 lhs1 = cp_parser_unary_expression (parser);
33367 if (lhs1 == error_mark_node)
33368 goto saw_error;
33370 if (structured_block)
33372 cp_parser_consume_semicolon_at_end_of_statement (parser);
33373 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
33375 done:
33376 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
33377 if (!structured_block)
33378 cp_parser_consume_semicolon_at_end_of_statement (parser);
33379 return;
33381 saw_error:
33382 cp_parser_skip_to_end_of_block_or_statement (parser);
33383 if (structured_block)
33385 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33386 cp_lexer_consume_token (parser->lexer);
33387 else if (code == OMP_ATOMIC_CAPTURE_NEW)
33389 cp_parser_skip_to_end_of_block_or_statement (parser);
33390 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33391 cp_lexer_consume_token (parser->lexer);
33397 /* OpenMP 2.5:
33398 # pragma omp barrier new-line */
33400 static void
33401 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
33403 cp_parser_require_pragma_eol (parser, pragma_tok);
33404 finish_omp_barrier ();
33407 /* OpenMP 2.5:
33408 # pragma omp critical [(name)] new-line
33409 structured-block
33411 OpenMP 4.5:
33412 # pragma omp critical [(name) [hint(expression)]] new-line
33413 structured-block */
33415 #define OMP_CRITICAL_CLAUSE_MASK \
33416 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
33418 static tree
33419 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
33421 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
33423 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33425 cp_lexer_consume_token (parser->lexer);
33427 name = cp_parser_identifier (parser);
33429 if (name == error_mark_node
33430 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33431 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33432 /*or_comma=*/false,
33433 /*consume_paren=*/true);
33434 if (name == error_mark_node)
33435 name = NULL;
33437 clauses = cp_parser_omp_all_clauses (parser,
33438 OMP_CRITICAL_CLAUSE_MASK,
33439 "#pragma omp critical", pragma_tok);
33441 else
33442 cp_parser_require_pragma_eol (parser, pragma_tok);
33444 stmt = cp_parser_omp_structured_block (parser, if_p);
33445 return c_finish_omp_critical (input_location, stmt, name, clauses);
33448 /* OpenMP 2.5:
33449 # pragma omp flush flush-vars[opt] new-line
33451 flush-vars:
33452 ( variable-list ) */
33454 static void
33455 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
33457 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33458 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
33459 cp_parser_require_pragma_eol (parser, pragma_tok);
33461 finish_omp_flush ();
33464 /* Helper function, to parse omp for increment expression. */
33466 static tree
33467 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
33469 tree cond = cp_parser_binary_expression (parser, false, true,
33470 PREC_NOT_OPERATOR, NULL);
33471 if (cond == error_mark_node
33472 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33474 cp_parser_skip_to_end_of_statement (parser);
33475 return error_mark_node;
33478 switch (TREE_CODE (cond))
33480 case GT_EXPR:
33481 case GE_EXPR:
33482 case LT_EXPR:
33483 case LE_EXPR:
33484 break;
33485 case NE_EXPR:
33486 if (code == CILK_SIMD || code == CILK_FOR)
33487 break;
33488 /* Fall through: OpenMP disallows NE_EXPR. */
33489 gcc_fallthrough ();
33490 default:
33491 return error_mark_node;
33494 /* If decl is an iterator, preserve LHS and RHS of the relational
33495 expr until finish_omp_for. */
33496 if (decl
33497 && (type_dependent_expression_p (decl)
33498 || CLASS_TYPE_P (TREE_TYPE (decl))))
33499 return cond;
33501 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
33502 TREE_CODE (cond),
33503 TREE_OPERAND (cond, 0), ERROR_MARK,
33504 TREE_OPERAND (cond, 1), ERROR_MARK,
33505 /*overload=*/NULL, tf_warning_or_error);
33508 /* Helper function, to parse omp for increment expression. */
33510 static tree
33511 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
33513 cp_token *token = cp_lexer_peek_token (parser->lexer);
33514 enum tree_code op;
33515 tree lhs, rhs;
33516 cp_id_kind idk;
33517 bool decl_first;
33519 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
33521 op = (token->type == CPP_PLUS_PLUS
33522 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
33523 cp_lexer_consume_token (parser->lexer);
33524 lhs = cp_parser_simple_cast_expression (parser);
33525 if (lhs != decl
33526 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
33527 return error_mark_node;
33528 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
33531 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
33532 if (lhs != decl
33533 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
33534 return error_mark_node;
33536 token = cp_lexer_peek_token (parser->lexer);
33537 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
33539 op = (token->type == CPP_PLUS_PLUS
33540 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
33541 cp_lexer_consume_token (parser->lexer);
33542 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
33545 op = cp_parser_assignment_operator_opt (parser);
33546 if (op == ERROR_MARK)
33547 return error_mark_node;
33549 if (op != NOP_EXPR)
33551 rhs = cp_parser_assignment_expression (parser);
33552 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
33553 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
33556 lhs = cp_parser_binary_expression (parser, false, false,
33557 PREC_ADDITIVE_EXPRESSION, NULL);
33558 token = cp_lexer_peek_token (parser->lexer);
33559 decl_first = (lhs == decl
33560 || (processing_template_decl && cp_tree_equal (lhs, decl)));
33561 if (decl_first)
33562 lhs = NULL_TREE;
33563 if (token->type != CPP_PLUS
33564 && token->type != CPP_MINUS)
33565 return error_mark_node;
33569 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
33570 cp_lexer_consume_token (parser->lexer);
33571 rhs = cp_parser_binary_expression (parser, false, false,
33572 PREC_ADDITIVE_EXPRESSION, NULL);
33573 token = cp_lexer_peek_token (parser->lexer);
33574 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
33576 if (lhs == NULL_TREE)
33578 if (op == PLUS_EXPR)
33579 lhs = rhs;
33580 else
33581 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
33582 tf_warning_or_error);
33584 else
33585 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
33586 ERROR_MARK, NULL, tf_warning_or_error);
33589 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
33591 if (!decl_first)
33593 if ((rhs != decl
33594 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
33595 || op == MINUS_EXPR)
33596 return error_mark_node;
33597 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
33599 else
33600 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
33602 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
33605 /* Parse the initialization statement of either an OpenMP for loop or
33606 a Cilk Plus for loop.
33608 Return true if the resulting construct should have an
33609 OMP_CLAUSE_PRIVATE added to it. */
33611 static tree
33612 cp_parser_omp_for_loop_init (cp_parser *parser,
33613 enum tree_code code,
33614 tree &this_pre_body,
33615 vec<tree, va_gc> *for_block,
33616 tree &init,
33617 tree &orig_init,
33618 tree &decl,
33619 tree &real_decl)
33621 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33622 return NULL_TREE;
33624 tree add_private_clause = NULL_TREE;
33626 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
33628 init-expr:
33629 var = lb
33630 integer-type var = lb
33631 random-access-iterator-type var = lb
33632 pointer-type var = lb
33634 cp_decl_specifier_seq type_specifiers;
33636 /* First, try to parse as an initialized declaration. See
33637 cp_parser_condition, from whence the bulk of this is copied. */
33639 cp_parser_parse_tentatively (parser);
33640 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
33641 /*is_trailing_return=*/false,
33642 &type_specifiers);
33643 if (cp_parser_parse_definitely (parser))
33645 /* If parsing a type specifier seq succeeded, then this
33646 MUST be a initialized declaration. */
33647 tree asm_specification, attributes;
33648 cp_declarator *declarator;
33650 declarator = cp_parser_declarator (parser,
33651 CP_PARSER_DECLARATOR_NAMED,
33652 /*ctor_dtor_or_conv_p=*/NULL,
33653 /*parenthesized_p=*/NULL,
33654 /*member_p=*/false,
33655 /*friend_p=*/false);
33656 attributes = cp_parser_attributes_opt (parser);
33657 asm_specification = cp_parser_asm_specification_opt (parser);
33659 if (declarator == cp_error_declarator)
33660 cp_parser_skip_to_end_of_statement (parser);
33662 else
33664 tree pushed_scope, auto_node;
33666 decl = start_decl (declarator, &type_specifiers,
33667 SD_INITIALIZED, attributes,
33668 /*prefix_attributes=*/NULL_TREE,
33669 &pushed_scope);
33671 auto_node = type_uses_auto (TREE_TYPE (decl));
33672 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
33674 if (cp_lexer_next_token_is (parser->lexer,
33675 CPP_OPEN_PAREN))
33677 if (code != CILK_SIMD && code != CILK_FOR)
33678 error ("parenthesized initialization is not allowed in "
33679 "OpenMP %<for%> loop");
33680 else
33681 error ("parenthesized initialization is "
33682 "not allowed in for-loop");
33684 else
33685 /* Trigger an error. */
33686 cp_parser_require (parser, CPP_EQ, RT_EQ);
33688 init = error_mark_node;
33689 cp_parser_skip_to_end_of_statement (parser);
33691 else if (CLASS_TYPE_P (TREE_TYPE (decl))
33692 || type_dependent_expression_p (decl)
33693 || auto_node)
33695 bool is_direct_init, is_non_constant_init;
33697 init = cp_parser_initializer (parser,
33698 &is_direct_init,
33699 &is_non_constant_init);
33701 if (auto_node)
33703 TREE_TYPE (decl)
33704 = do_auto_deduction (TREE_TYPE (decl), init,
33705 auto_node);
33707 if (!CLASS_TYPE_P (TREE_TYPE (decl))
33708 && !type_dependent_expression_p (decl))
33709 goto non_class;
33712 cp_finish_decl (decl, init, !is_non_constant_init,
33713 asm_specification,
33714 LOOKUP_ONLYCONVERTING);
33715 orig_init = init;
33716 if (CLASS_TYPE_P (TREE_TYPE (decl)))
33718 vec_safe_push (for_block, this_pre_body);
33719 init = NULL_TREE;
33721 else
33723 init = pop_stmt_list (this_pre_body);
33724 if (init && TREE_CODE (init) == STATEMENT_LIST)
33726 tree_stmt_iterator i = tsi_start (init);
33727 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
33728 while (!tsi_end_p (i))
33730 tree t = tsi_stmt (i);
33731 if (TREE_CODE (t) == DECL_EXPR
33732 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
33734 tsi_delink (&i);
33735 vec_safe_push (for_block, t);
33736 continue;
33738 break;
33740 if (tsi_one_before_end_p (i))
33742 tree t = tsi_stmt (i);
33743 tsi_delink (&i);
33744 free_stmt_list (init);
33745 init = t;
33749 this_pre_body = NULL_TREE;
33751 else
33753 /* Consume '='. */
33754 cp_lexer_consume_token (parser->lexer);
33755 init = cp_parser_assignment_expression (parser);
33757 non_class:
33758 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
33759 init = error_mark_node;
33760 else
33761 cp_finish_decl (decl, NULL_TREE,
33762 /*init_const_expr_p=*/false,
33763 asm_specification,
33764 LOOKUP_ONLYCONVERTING);
33767 if (pushed_scope)
33768 pop_scope (pushed_scope);
33771 else
33773 cp_id_kind idk;
33774 /* If parsing a type specifier sequence failed, then
33775 this MUST be a simple expression. */
33776 if (code == CILK_FOR)
33777 error ("%<_Cilk_for%> allows expression instead of declaration only "
33778 "in C, not in C++");
33779 cp_parser_parse_tentatively (parser);
33780 decl = cp_parser_primary_expression (parser, false, false,
33781 false, &idk);
33782 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
33783 if (!cp_parser_error_occurred (parser)
33784 && decl
33785 && (TREE_CODE (decl) == COMPONENT_REF
33786 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
33788 cp_parser_abort_tentative_parse (parser);
33789 cp_parser_parse_tentatively (parser);
33790 cp_token *token = cp_lexer_peek_token (parser->lexer);
33791 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
33792 /*check_dependency_p=*/true,
33793 /*template_p=*/NULL,
33794 /*declarator_p=*/false,
33795 /*optional_p=*/false);
33796 if (name != error_mark_node
33797 && last_tok == cp_lexer_peek_token (parser->lexer))
33799 decl = cp_parser_lookup_name_simple (parser, name,
33800 token->location);
33801 if (TREE_CODE (decl) == FIELD_DECL)
33802 add_private_clause = omp_privatize_field (decl, false);
33804 cp_parser_abort_tentative_parse (parser);
33805 cp_parser_parse_tentatively (parser);
33806 decl = cp_parser_primary_expression (parser, false, false,
33807 false, &idk);
33809 if (!cp_parser_error_occurred (parser)
33810 && decl
33811 && DECL_P (decl)
33812 && CLASS_TYPE_P (TREE_TYPE (decl)))
33814 tree rhs;
33816 cp_parser_parse_definitely (parser);
33817 cp_parser_require (parser, CPP_EQ, RT_EQ);
33818 rhs = cp_parser_assignment_expression (parser);
33819 orig_init = rhs;
33820 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
33821 decl, NOP_EXPR,
33822 rhs,
33823 tf_warning_or_error));
33824 if (!add_private_clause)
33825 add_private_clause = decl;
33827 else
33829 decl = NULL;
33830 cp_parser_abort_tentative_parse (parser);
33831 init = cp_parser_expression (parser);
33832 if (init)
33834 if (TREE_CODE (init) == MODIFY_EXPR
33835 || TREE_CODE (init) == MODOP_EXPR)
33836 real_decl = TREE_OPERAND (init, 0);
33840 return add_private_clause;
33843 /* Parse the restricted form of the for statement allowed by OpenMP. */
33845 static tree
33846 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
33847 tree *cclauses, bool *if_p)
33849 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
33850 tree real_decl, initv, condv, incrv, declv;
33851 tree this_pre_body, cl, ordered_cl = NULL_TREE;
33852 location_t loc_first;
33853 bool collapse_err = false;
33854 int i, collapse = 1, ordered = 0, count, nbraces = 0;
33855 vec<tree, va_gc> *for_block = make_tree_vector ();
33856 auto_vec<tree, 4> orig_inits;
33858 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
33859 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
33860 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
33861 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
33862 && OMP_CLAUSE_ORDERED_EXPR (cl))
33864 ordered_cl = cl;
33865 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
33868 if (ordered && ordered < collapse)
33870 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
33871 "%<ordered%> clause parameter is less than %<collapse%>");
33872 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
33873 = build_int_cst (NULL_TREE, collapse);
33874 ordered = collapse;
33876 if (ordered)
33878 for (tree *pc = &clauses; *pc; )
33879 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
33881 error_at (OMP_CLAUSE_LOCATION (*pc),
33882 "%<linear%> clause may not be specified together "
33883 "with %<ordered%> clause with a parameter");
33884 *pc = OMP_CLAUSE_CHAIN (*pc);
33886 else
33887 pc = &OMP_CLAUSE_CHAIN (*pc);
33890 gcc_assert (collapse >= 1 && ordered >= 0);
33891 count = ordered ? ordered : collapse;
33893 declv = make_tree_vec (count);
33894 initv = make_tree_vec (count);
33895 condv = make_tree_vec (count);
33896 incrv = make_tree_vec (count);
33898 loc_first = cp_lexer_peek_token (parser->lexer)->location;
33900 for (i = 0; i < count; i++)
33902 int bracecount = 0;
33903 tree add_private_clause = NULL_TREE;
33904 location_t loc;
33906 if (code != CILK_FOR
33907 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
33909 cp_parser_error (parser, "for statement expected");
33910 return NULL;
33912 if (code == CILK_FOR
33913 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
33915 cp_parser_error (parser, "_Cilk_for statement expected");
33916 return NULL;
33918 loc = cp_lexer_consume_token (parser->lexer)->location;
33920 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33921 return NULL;
33923 init = orig_init = decl = real_decl = NULL;
33924 this_pre_body = push_stmt_list ();
33926 add_private_clause
33927 = cp_parser_omp_for_loop_init (parser, code,
33928 this_pre_body, for_block,
33929 init, orig_init, decl, real_decl);
33931 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
33932 if (this_pre_body)
33934 this_pre_body = pop_stmt_list (this_pre_body);
33935 if (pre_body)
33937 tree t = pre_body;
33938 pre_body = push_stmt_list ();
33939 add_stmt (t);
33940 add_stmt (this_pre_body);
33941 pre_body = pop_stmt_list (pre_body);
33943 else
33944 pre_body = this_pre_body;
33947 if (decl)
33948 real_decl = decl;
33949 if (cclauses != NULL
33950 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
33951 && real_decl != NULL_TREE)
33953 tree *c;
33954 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
33955 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
33956 && OMP_CLAUSE_DECL (*c) == real_decl)
33958 error_at (loc, "iteration variable %qD"
33959 " should not be firstprivate", real_decl);
33960 *c = OMP_CLAUSE_CHAIN (*c);
33962 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
33963 && OMP_CLAUSE_DECL (*c) == real_decl)
33965 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
33966 tree l = *c;
33967 *c = OMP_CLAUSE_CHAIN (*c);
33968 if (code == OMP_SIMD)
33970 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33971 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
33973 else
33975 OMP_CLAUSE_CHAIN (l) = clauses;
33976 clauses = l;
33978 add_private_clause = NULL_TREE;
33980 else
33982 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
33983 && OMP_CLAUSE_DECL (*c) == real_decl)
33984 add_private_clause = NULL_TREE;
33985 c = &OMP_CLAUSE_CHAIN (*c);
33989 if (add_private_clause)
33991 tree c;
33992 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
33994 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
33995 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
33996 && OMP_CLAUSE_DECL (c) == decl)
33997 break;
33998 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
33999 && OMP_CLAUSE_DECL (c) == decl)
34000 error_at (loc, "iteration variable %qD "
34001 "should not be firstprivate",
34002 decl);
34003 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
34004 && OMP_CLAUSE_DECL (c) == decl)
34005 error_at (loc, "iteration variable %qD should not be reduction",
34006 decl);
34008 if (c == NULL)
34010 if (code != OMP_SIMD)
34011 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
34012 else if (collapse == 1)
34013 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
34014 else
34015 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
34016 OMP_CLAUSE_DECL (c) = add_private_clause;
34017 c = finish_omp_clauses (c, C_ORT_OMP);
34018 if (c)
34020 OMP_CLAUSE_CHAIN (c) = clauses;
34021 clauses = c;
34022 /* For linear, signal that we need to fill up
34023 the so far unknown linear step. */
34024 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
34025 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
34030 cond = NULL;
34031 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34032 cond = cp_parser_omp_for_cond (parser, decl, code);
34033 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34035 incr = NULL;
34036 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
34038 /* If decl is an iterator, preserve the operator on decl
34039 until finish_omp_for. */
34040 if (real_decl
34041 && ((processing_template_decl
34042 && (TREE_TYPE (real_decl) == NULL_TREE
34043 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
34044 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
34045 incr = cp_parser_omp_for_incr (parser, real_decl);
34046 else
34047 incr = cp_parser_expression (parser);
34048 if (!EXPR_HAS_LOCATION (incr))
34049 protected_set_expr_location (incr, input_location);
34052 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
34053 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34054 /*or_comma=*/false,
34055 /*consume_paren=*/true);
34057 TREE_VEC_ELT (declv, i) = decl;
34058 TREE_VEC_ELT (initv, i) = init;
34059 TREE_VEC_ELT (condv, i) = cond;
34060 TREE_VEC_ELT (incrv, i) = incr;
34061 if (orig_init)
34063 orig_inits.safe_grow_cleared (i + 1);
34064 orig_inits[i] = orig_init;
34067 if (i == count - 1)
34068 break;
34070 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
34071 in between the collapsed for loops to be still considered perfectly
34072 nested. Hopefully the final version clarifies this.
34073 For now handle (multiple) {'s and empty statements. */
34074 cp_parser_parse_tentatively (parser);
34077 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34078 break;
34079 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34081 cp_lexer_consume_token (parser->lexer);
34082 bracecount++;
34084 else if (bracecount
34085 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34086 cp_lexer_consume_token (parser->lexer);
34087 else
34089 loc = cp_lexer_peek_token (parser->lexer)->location;
34090 error_at (loc, "not enough collapsed for loops");
34091 collapse_err = true;
34092 cp_parser_abort_tentative_parse (parser);
34093 declv = NULL_TREE;
34094 break;
34097 while (1);
34099 if (declv)
34101 cp_parser_parse_definitely (parser);
34102 nbraces += bracecount;
34106 if (nbraces)
34107 if_p = NULL;
34109 /* Note that we saved the original contents of this flag when we entered
34110 the structured block, and so we don't need to re-save it here. */
34111 if (code == CILK_SIMD || code == CILK_FOR)
34112 parser->in_statement = IN_CILK_SIMD_FOR;
34113 else
34114 parser->in_statement = IN_OMP_FOR;
34116 /* Note that the grammar doesn't call for a structured block here,
34117 though the loop as a whole is a structured block. */
34118 body = push_stmt_list ();
34119 cp_parser_statement (parser, NULL_TREE, false, if_p);
34120 body = pop_stmt_list (body);
34122 if (declv == NULL_TREE)
34123 ret = NULL_TREE;
34124 else
34125 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
34126 body, pre_body, &orig_inits, clauses);
34128 while (nbraces)
34130 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34132 cp_lexer_consume_token (parser->lexer);
34133 nbraces--;
34135 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34136 cp_lexer_consume_token (parser->lexer);
34137 else
34139 if (!collapse_err)
34141 error_at (cp_lexer_peek_token (parser->lexer)->location,
34142 "collapsed loops not perfectly nested");
34144 collapse_err = true;
34145 cp_parser_statement_seq_opt (parser, NULL);
34146 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
34147 break;
34151 while (!for_block->is_empty ())
34153 tree t = for_block->pop ();
34154 if (TREE_CODE (t) == STATEMENT_LIST)
34155 add_stmt (pop_stmt_list (t));
34156 else
34157 add_stmt (t);
34159 release_tree_vector (for_block);
34161 return ret;
34164 /* Helper function for OpenMP parsing, split clauses and call
34165 finish_omp_clauses on each of the set of clauses afterwards. */
34167 static void
34168 cp_omp_split_clauses (location_t loc, enum tree_code code,
34169 omp_clause_mask mask, tree clauses, tree *cclauses)
34171 int i;
34172 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
34173 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
34174 if (cclauses[i])
34175 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
34178 /* OpenMP 4.0:
34179 #pragma omp simd simd-clause[optseq] new-line
34180 for-loop */
34182 #define OMP_SIMD_CLAUSE_MASK \
34183 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
34184 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
34185 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34186 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
34187 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34188 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34189 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34190 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34192 static tree
34193 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
34194 char *p_name, omp_clause_mask mask, tree *cclauses,
34195 bool *if_p)
34197 tree clauses, sb, ret;
34198 unsigned int save;
34199 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34201 strcat (p_name, " simd");
34202 mask |= OMP_SIMD_CLAUSE_MASK;
34204 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34205 cclauses == NULL);
34206 if (cclauses)
34208 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
34209 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
34210 tree c = find_omp_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
34211 OMP_CLAUSE_ORDERED);
34212 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
34214 error_at (OMP_CLAUSE_LOCATION (c),
34215 "%<ordered%> clause with parameter may not be specified "
34216 "on %qs construct", p_name);
34217 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
34221 sb = begin_omp_structured_block ();
34222 save = cp_parser_begin_omp_structured_block (parser);
34224 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
34226 cp_parser_end_omp_structured_block (parser, save);
34227 add_stmt (finish_omp_structured_block (sb));
34229 return ret;
34232 /* OpenMP 2.5:
34233 #pragma omp for for-clause[optseq] new-line
34234 for-loop
34236 OpenMP 4.0:
34237 #pragma omp for simd for-simd-clause[optseq] new-line
34238 for-loop */
34240 #define OMP_FOR_CLAUSE_MASK \
34241 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34242 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34243 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34244 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34245 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34246 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
34247 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
34248 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
34249 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34251 static tree
34252 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
34253 char *p_name, omp_clause_mask mask, tree *cclauses,
34254 bool *if_p)
34256 tree clauses, sb, ret;
34257 unsigned int save;
34258 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34260 strcat (p_name, " for");
34261 mask |= OMP_FOR_CLAUSE_MASK;
34262 /* parallel for{, simd} disallows nowait clause, but for
34263 target {teams distribute ,}parallel for{, simd} it should be accepted. */
34264 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
34265 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
34266 /* Composite distribute parallel for{, simd} disallows ordered clause. */
34267 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34268 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
34270 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34272 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34273 const char *p = IDENTIFIER_POINTER (id);
34275 if (strcmp (p, "simd") == 0)
34277 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34278 if (cclauses == NULL)
34279 cclauses = cclauses_buf;
34281 cp_lexer_consume_token (parser->lexer);
34282 if (!flag_openmp) /* flag_openmp_simd */
34283 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34284 cclauses, if_p);
34285 sb = begin_omp_structured_block ();
34286 save = cp_parser_begin_omp_structured_block (parser);
34287 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34288 cclauses, if_p);
34289 cp_parser_end_omp_structured_block (parser, save);
34290 tree body = finish_omp_structured_block (sb);
34291 if (ret == NULL)
34292 return ret;
34293 ret = make_node (OMP_FOR);
34294 TREE_TYPE (ret) = void_type_node;
34295 OMP_FOR_BODY (ret) = body;
34296 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34297 SET_EXPR_LOCATION (ret, loc);
34298 add_stmt (ret);
34299 return ret;
34302 if (!flag_openmp) /* flag_openmp_simd */
34304 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34305 return NULL_TREE;
34308 /* Composite distribute parallel for disallows linear clause. */
34309 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34310 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
34312 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34313 cclauses == NULL);
34314 if (cclauses)
34316 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
34317 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34320 sb = begin_omp_structured_block ();
34321 save = cp_parser_begin_omp_structured_block (parser);
34323 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
34325 cp_parser_end_omp_structured_block (parser, save);
34326 add_stmt (finish_omp_structured_block (sb));
34328 return ret;
34331 /* OpenMP 2.5:
34332 # pragma omp master new-line
34333 structured-block */
34335 static tree
34336 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34338 cp_parser_require_pragma_eol (parser, pragma_tok);
34339 return c_finish_omp_master (input_location,
34340 cp_parser_omp_structured_block (parser, if_p));
34343 /* OpenMP 2.5:
34344 # pragma omp ordered new-line
34345 structured-block
34347 OpenMP 4.5:
34348 # pragma omp ordered ordered-clauses new-line
34349 structured-block */
34351 #define OMP_ORDERED_CLAUSE_MASK \
34352 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
34353 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
34355 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
34356 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
34358 static bool
34359 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
34360 enum pragma_context context, bool *if_p)
34362 location_t loc = pragma_tok->location;
34364 if (context != pragma_stmt && context != pragma_compound)
34366 cp_parser_error (parser, "expected declaration specifiers");
34367 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34368 return false;
34371 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34373 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34374 const char *p = IDENTIFIER_POINTER (id);
34376 if (strcmp (p, "depend") == 0)
34378 if (context == pragma_stmt)
34380 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
34381 "%<depend%> clause may only be used in compound "
34382 "statements");
34383 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34384 return false;
34386 tree clauses
34387 = cp_parser_omp_all_clauses (parser,
34388 OMP_ORDERED_DEPEND_CLAUSE_MASK,
34389 "#pragma omp ordered", pragma_tok);
34390 c_finish_omp_ordered (loc, clauses, NULL_TREE);
34391 return false;
34395 tree clauses
34396 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
34397 "#pragma omp ordered", pragma_tok);
34398 c_finish_omp_ordered (loc, clauses,
34399 cp_parser_omp_structured_block (parser, if_p));
34400 return true;
34403 /* OpenMP 2.5:
34405 section-scope:
34406 { section-sequence }
34408 section-sequence:
34409 section-directive[opt] structured-block
34410 section-sequence section-directive structured-block */
34412 static tree
34413 cp_parser_omp_sections_scope (cp_parser *parser)
34415 tree stmt, substmt;
34416 bool error_suppress = false;
34417 cp_token *tok;
34419 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
34420 return NULL_TREE;
34422 stmt = push_stmt_list ();
34424 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
34425 != PRAGMA_OMP_SECTION)
34427 substmt = cp_parser_omp_structured_block (parser, NULL);
34428 substmt = build1 (OMP_SECTION, void_type_node, substmt);
34429 add_stmt (substmt);
34432 while (1)
34434 tok = cp_lexer_peek_token (parser->lexer);
34435 if (tok->type == CPP_CLOSE_BRACE)
34436 break;
34437 if (tok->type == CPP_EOF)
34438 break;
34440 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
34442 cp_lexer_consume_token (parser->lexer);
34443 cp_parser_require_pragma_eol (parser, tok);
34444 error_suppress = false;
34446 else if (!error_suppress)
34448 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
34449 error_suppress = true;
34452 substmt = cp_parser_omp_structured_block (parser, NULL);
34453 substmt = build1 (OMP_SECTION, void_type_node, substmt);
34454 add_stmt (substmt);
34456 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34458 substmt = pop_stmt_list (stmt);
34460 stmt = make_node (OMP_SECTIONS);
34461 TREE_TYPE (stmt) = void_type_node;
34462 OMP_SECTIONS_BODY (stmt) = substmt;
34464 add_stmt (stmt);
34465 return stmt;
34468 /* OpenMP 2.5:
34469 # pragma omp sections sections-clause[optseq] newline
34470 sections-scope */
34472 #define OMP_SECTIONS_CLAUSE_MASK \
34473 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34474 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34475 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34477 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34479 static tree
34480 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
34481 char *p_name, omp_clause_mask mask, tree *cclauses)
34483 tree clauses, ret;
34484 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34486 strcat (p_name, " sections");
34487 mask |= OMP_SECTIONS_CLAUSE_MASK;
34488 if (cclauses)
34489 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
34491 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34492 cclauses == NULL);
34493 if (cclauses)
34495 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
34496 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
34499 ret = cp_parser_omp_sections_scope (parser);
34500 if (ret)
34501 OMP_SECTIONS_CLAUSES (ret) = clauses;
34503 return ret;
34506 /* OpenMP 2.5:
34507 # pragma omp parallel parallel-clause[optseq] new-line
34508 structured-block
34509 # pragma omp parallel for parallel-for-clause[optseq] new-line
34510 structured-block
34511 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
34512 structured-block
34514 OpenMP 4.0:
34515 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
34516 structured-block */
34518 #define OMP_PARALLEL_CLAUSE_MASK \
34519 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
34523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
34525 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34526 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
34527 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
34529 static tree
34530 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
34531 char *p_name, omp_clause_mask mask, tree *cclauses,
34532 bool *if_p)
34534 tree stmt, clauses, block;
34535 unsigned int save;
34536 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34538 strcat (p_name, " parallel");
34539 mask |= OMP_PARALLEL_CLAUSE_MASK;
34540 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
34541 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
34542 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
34543 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
34545 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34547 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34548 if (cclauses == NULL)
34549 cclauses = cclauses_buf;
34551 cp_lexer_consume_token (parser->lexer);
34552 if (!flag_openmp) /* flag_openmp_simd */
34553 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
34554 if_p);
34555 block = begin_omp_parallel ();
34556 save = cp_parser_begin_omp_structured_block (parser);
34557 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
34558 if_p);
34559 cp_parser_end_omp_structured_block (parser, save);
34560 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
34561 block);
34562 if (ret == NULL_TREE)
34563 return ret;
34564 OMP_PARALLEL_COMBINED (stmt) = 1;
34565 return stmt;
34567 /* When combined with distribute, parallel has to be followed by for.
34568 #pragma omp target parallel is allowed though. */
34569 else if (cclauses
34570 && (mask & (OMP_CLAUSE_MASK_1
34571 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34573 error_at (loc, "expected %<for%> after %qs", p_name);
34574 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34575 return NULL_TREE;
34577 else if (!flag_openmp) /* flag_openmp_simd */
34579 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34580 return NULL_TREE;
34582 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34584 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34585 const char *p = IDENTIFIER_POINTER (id);
34586 if (strcmp (p, "sections") == 0)
34588 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34589 cclauses = cclauses_buf;
34591 cp_lexer_consume_token (parser->lexer);
34592 block = begin_omp_parallel ();
34593 save = cp_parser_begin_omp_structured_block (parser);
34594 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
34595 cp_parser_end_omp_structured_block (parser, save);
34596 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
34597 block);
34598 OMP_PARALLEL_COMBINED (stmt) = 1;
34599 return stmt;
34603 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34604 cclauses == NULL);
34605 if (cclauses)
34607 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
34608 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
34611 block = begin_omp_parallel ();
34612 save = cp_parser_begin_omp_structured_block (parser);
34613 cp_parser_statement (parser, NULL_TREE, false, if_p);
34614 cp_parser_end_omp_structured_block (parser, save);
34615 stmt = finish_omp_parallel (clauses, block);
34616 return stmt;
34619 /* OpenMP 2.5:
34620 # pragma omp single single-clause[optseq] new-line
34621 structured-block */
34623 #define OMP_SINGLE_CLAUSE_MASK \
34624 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34625 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34626 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
34627 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34629 static tree
34630 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34632 tree stmt = make_node (OMP_SINGLE);
34633 TREE_TYPE (stmt) = void_type_node;
34635 OMP_SINGLE_CLAUSES (stmt)
34636 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
34637 "#pragma omp single", pragma_tok);
34638 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
34640 return add_stmt (stmt);
34643 /* OpenMP 3.0:
34644 # pragma omp task task-clause[optseq] new-line
34645 structured-block */
34647 #define OMP_TASK_CLAUSE_MASK \
34648 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34649 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
34650 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
34651 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34652 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34653 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34654 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
34655 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
34656 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34657 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
34659 static tree
34660 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34662 tree clauses, block;
34663 unsigned int save;
34665 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
34666 "#pragma omp task", pragma_tok);
34667 block = begin_omp_task ();
34668 save = cp_parser_begin_omp_structured_block (parser);
34669 cp_parser_statement (parser, NULL_TREE, false, if_p);
34670 cp_parser_end_omp_structured_block (parser, save);
34671 return finish_omp_task (clauses, block);
34674 /* OpenMP 3.0:
34675 # pragma omp taskwait new-line */
34677 static void
34678 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
34680 cp_parser_require_pragma_eol (parser, pragma_tok);
34681 finish_omp_taskwait ();
34684 /* OpenMP 3.1:
34685 # pragma omp taskyield new-line */
34687 static void
34688 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
34690 cp_parser_require_pragma_eol (parser, pragma_tok);
34691 finish_omp_taskyield ();
34694 /* OpenMP 4.0:
34695 # pragma omp taskgroup new-line
34696 structured-block */
34698 static tree
34699 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34701 cp_parser_require_pragma_eol (parser, pragma_tok);
34702 return c_finish_omp_taskgroup (input_location,
34703 cp_parser_omp_structured_block (parser,
34704 if_p));
34708 /* OpenMP 2.5:
34709 # pragma omp threadprivate (variable-list) */
34711 static void
34712 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
34714 tree vars;
34716 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34717 cp_parser_require_pragma_eol (parser, pragma_tok);
34719 finish_omp_threadprivate (vars);
34722 /* OpenMP 4.0:
34723 # pragma omp cancel cancel-clause[optseq] new-line */
34725 #define OMP_CANCEL_CLAUSE_MASK \
34726 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
34727 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
34728 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
34729 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
34730 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
34732 static void
34733 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
34735 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
34736 "#pragma omp cancel", pragma_tok);
34737 finish_omp_cancel (clauses);
34740 /* OpenMP 4.0:
34741 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
34743 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
34744 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
34745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
34746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
34747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
34749 static void
34750 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
34751 enum pragma_context context)
34753 tree clauses;
34754 bool point_seen = false;
34756 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34758 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34759 const char *p = IDENTIFIER_POINTER (id);
34761 if (strcmp (p, "point") == 0)
34763 cp_lexer_consume_token (parser->lexer);
34764 point_seen = true;
34767 if (!point_seen)
34769 cp_parser_error (parser, "expected %<point%>");
34770 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34771 return;
34774 if (context != pragma_compound)
34776 if (context == pragma_stmt)
34777 error_at (pragma_tok->location,
34778 "%<#pragma omp cancellation point%> may only be used in"
34779 " compound statements");
34780 else
34781 cp_parser_error (parser, "expected declaration specifiers");
34782 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34783 return;
34786 clauses = cp_parser_omp_all_clauses (parser,
34787 OMP_CANCELLATION_POINT_CLAUSE_MASK,
34788 "#pragma omp cancellation point",
34789 pragma_tok);
34790 finish_omp_cancellation_point (clauses);
34793 /* OpenMP 4.0:
34794 #pragma omp distribute distribute-clause[optseq] new-line
34795 for-loop */
34797 #define OMP_DISTRIBUTE_CLAUSE_MASK \
34798 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34799 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34800 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
34802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34804 static tree
34805 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
34806 char *p_name, omp_clause_mask mask, tree *cclauses,
34807 bool *if_p)
34809 tree clauses, sb, ret;
34810 unsigned int save;
34811 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34813 strcat (p_name, " distribute");
34814 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
34816 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34818 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34819 const char *p = IDENTIFIER_POINTER (id);
34820 bool simd = false;
34821 bool parallel = false;
34823 if (strcmp (p, "simd") == 0)
34824 simd = true;
34825 else
34826 parallel = strcmp (p, "parallel") == 0;
34827 if (parallel || simd)
34829 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34830 if (cclauses == NULL)
34831 cclauses = cclauses_buf;
34832 cp_lexer_consume_token (parser->lexer);
34833 if (!flag_openmp) /* flag_openmp_simd */
34835 if (simd)
34836 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34837 cclauses, if_p);
34838 else
34839 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
34840 cclauses, if_p);
34842 sb = begin_omp_structured_block ();
34843 save = cp_parser_begin_omp_structured_block (parser);
34844 if (simd)
34845 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34846 cclauses, if_p);
34847 else
34848 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
34849 cclauses, if_p);
34850 cp_parser_end_omp_structured_block (parser, save);
34851 tree body = finish_omp_structured_block (sb);
34852 if (ret == NULL)
34853 return ret;
34854 ret = make_node (OMP_DISTRIBUTE);
34855 TREE_TYPE (ret) = void_type_node;
34856 OMP_FOR_BODY (ret) = body;
34857 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
34858 SET_EXPR_LOCATION (ret, loc);
34859 add_stmt (ret);
34860 return ret;
34863 if (!flag_openmp) /* flag_openmp_simd */
34865 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34866 return NULL_TREE;
34869 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34870 cclauses == NULL);
34871 if (cclauses)
34873 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
34874 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
34877 sb = begin_omp_structured_block ();
34878 save = cp_parser_begin_omp_structured_block (parser);
34880 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
34882 cp_parser_end_omp_structured_block (parser, save);
34883 add_stmt (finish_omp_structured_block (sb));
34885 return ret;
34888 /* OpenMP 4.0:
34889 # pragma omp teams teams-clause[optseq] new-line
34890 structured-block */
34892 #define OMP_TEAMS_CLAUSE_MASK \
34893 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34897 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
34898 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
34899 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
34901 static tree
34902 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
34903 char *p_name, omp_clause_mask mask, tree *cclauses,
34904 bool *if_p)
34906 tree clauses, sb, ret;
34907 unsigned int save;
34908 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34910 strcat (p_name, " teams");
34911 mask |= OMP_TEAMS_CLAUSE_MASK;
34913 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34915 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34916 const char *p = IDENTIFIER_POINTER (id);
34917 if (strcmp (p, "distribute") == 0)
34919 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34920 if (cclauses == NULL)
34921 cclauses = cclauses_buf;
34923 cp_lexer_consume_token (parser->lexer);
34924 if (!flag_openmp) /* flag_openmp_simd */
34925 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
34926 cclauses, if_p);
34927 sb = begin_omp_structured_block ();
34928 save = cp_parser_begin_omp_structured_block (parser);
34929 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
34930 cclauses, if_p);
34931 cp_parser_end_omp_structured_block (parser, save);
34932 tree body = finish_omp_structured_block (sb);
34933 if (ret == NULL)
34934 return ret;
34935 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34936 ret = make_node (OMP_TEAMS);
34937 TREE_TYPE (ret) = void_type_node;
34938 OMP_TEAMS_CLAUSES (ret) = clauses;
34939 OMP_TEAMS_BODY (ret) = body;
34940 OMP_TEAMS_COMBINED (ret) = 1;
34941 return add_stmt (ret);
34944 if (!flag_openmp) /* flag_openmp_simd */
34946 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34947 return NULL_TREE;
34950 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34951 cclauses == NULL);
34952 if (cclauses)
34954 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
34955 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34958 tree stmt = make_node (OMP_TEAMS);
34959 TREE_TYPE (stmt) = void_type_node;
34960 OMP_TEAMS_CLAUSES (stmt) = clauses;
34961 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
34963 return add_stmt (stmt);
34966 /* OpenMP 4.0:
34967 # pragma omp target data target-data-clause[optseq] new-line
34968 structured-block */
34970 #define OMP_TARGET_DATA_CLAUSE_MASK \
34971 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34972 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
34976 static tree
34977 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34979 tree clauses
34980 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
34981 "#pragma omp target data", pragma_tok);
34982 int map_seen = 0;
34983 for (tree *pc = &clauses; *pc;)
34985 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34986 switch (OMP_CLAUSE_MAP_KIND (*pc))
34988 case GOMP_MAP_TO:
34989 case GOMP_MAP_ALWAYS_TO:
34990 case GOMP_MAP_FROM:
34991 case GOMP_MAP_ALWAYS_FROM:
34992 case GOMP_MAP_TOFROM:
34993 case GOMP_MAP_ALWAYS_TOFROM:
34994 case GOMP_MAP_ALLOC:
34995 map_seen = 3;
34996 break;
34997 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34998 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34999 case GOMP_MAP_ALWAYS_POINTER:
35000 break;
35001 default:
35002 map_seen |= 1;
35003 error_at (OMP_CLAUSE_LOCATION (*pc),
35004 "%<#pragma omp target data%> with map-type other "
35005 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35006 "on %<map%> clause");
35007 *pc = OMP_CLAUSE_CHAIN (*pc);
35008 continue;
35010 pc = &OMP_CLAUSE_CHAIN (*pc);
35013 if (map_seen != 3)
35015 if (map_seen == 0)
35016 error_at (pragma_tok->location,
35017 "%<#pragma omp target data%> must contain at least "
35018 "one %<map%> clause");
35019 return NULL_TREE;
35022 tree stmt = make_node (OMP_TARGET_DATA);
35023 TREE_TYPE (stmt) = void_type_node;
35024 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
35026 keep_next_level (true);
35027 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35029 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35030 return add_stmt (stmt);
35033 /* OpenMP 4.5:
35034 # pragma omp target enter data target-enter-data-clause[optseq] new-line
35035 structured-block */
35037 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
35038 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35044 static tree
35045 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
35046 enum pragma_context context)
35048 bool data_seen = false;
35049 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35051 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35052 const char *p = IDENTIFIER_POINTER (id);
35054 if (strcmp (p, "data") == 0)
35056 cp_lexer_consume_token (parser->lexer);
35057 data_seen = true;
35060 if (!data_seen)
35062 cp_parser_error (parser, "expected %<data%>");
35063 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35064 return NULL_TREE;
35067 if (context == pragma_stmt)
35069 error_at (pragma_tok->location,
35070 "%<#pragma omp target enter data%> may only be "
35071 "used in compound statements");
35072 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35073 return NULL_TREE;
35076 tree clauses
35077 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
35078 "#pragma omp target enter data", pragma_tok);
35079 int map_seen = 0;
35080 for (tree *pc = &clauses; *pc;)
35082 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35083 switch (OMP_CLAUSE_MAP_KIND (*pc))
35085 case GOMP_MAP_TO:
35086 case GOMP_MAP_ALWAYS_TO:
35087 case GOMP_MAP_ALLOC:
35088 map_seen = 3;
35089 break;
35090 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35091 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35092 case GOMP_MAP_ALWAYS_POINTER:
35093 break;
35094 default:
35095 map_seen |= 1;
35096 error_at (OMP_CLAUSE_LOCATION (*pc),
35097 "%<#pragma omp target enter data%> with map-type other "
35098 "than %<to%> or %<alloc%> on %<map%> clause");
35099 *pc = OMP_CLAUSE_CHAIN (*pc);
35100 continue;
35102 pc = &OMP_CLAUSE_CHAIN (*pc);
35105 if (map_seen != 3)
35107 if (map_seen == 0)
35108 error_at (pragma_tok->location,
35109 "%<#pragma omp target enter data%> must contain at least "
35110 "one %<map%> clause");
35111 return NULL_TREE;
35114 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
35115 TREE_TYPE (stmt) = void_type_node;
35116 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
35117 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35118 return add_stmt (stmt);
35121 /* OpenMP 4.5:
35122 # pragma omp target exit data target-enter-data-clause[optseq] new-line
35123 structured-block */
35125 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
35126 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35132 static tree
35133 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
35134 enum pragma_context context)
35136 bool data_seen = false;
35137 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35139 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35140 const char *p = IDENTIFIER_POINTER (id);
35142 if (strcmp (p, "data") == 0)
35144 cp_lexer_consume_token (parser->lexer);
35145 data_seen = true;
35148 if (!data_seen)
35150 cp_parser_error (parser, "expected %<data%>");
35151 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35152 return NULL_TREE;
35155 if (context == pragma_stmt)
35157 error_at (pragma_tok->location,
35158 "%<#pragma omp target exit data%> may only be "
35159 "used in compound statements");
35160 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35161 return NULL_TREE;
35164 tree clauses
35165 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
35166 "#pragma omp target exit data", pragma_tok);
35167 int map_seen = 0;
35168 for (tree *pc = &clauses; *pc;)
35170 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35171 switch (OMP_CLAUSE_MAP_KIND (*pc))
35173 case GOMP_MAP_FROM:
35174 case GOMP_MAP_ALWAYS_FROM:
35175 case GOMP_MAP_RELEASE:
35176 case GOMP_MAP_DELETE:
35177 map_seen = 3;
35178 break;
35179 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35180 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35181 case GOMP_MAP_ALWAYS_POINTER:
35182 break;
35183 default:
35184 map_seen |= 1;
35185 error_at (OMP_CLAUSE_LOCATION (*pc),
35186 "%<#pragma omp target exit data%> with map-type other "
35187 "than %<from%>, %<release%> or %<delete%> on %<map%>"
35188 " clause");
35189 *pc = OMP_CLAUSE_CHAIN (*pc);
35190 continue;
35192 pc = &OMP_CLAUSE_CHAIN (*pc);
35195 if (map_seen != 3)
35197 if (map_seen == 0)
35198 error_at (pragma_tok->location,
35199 "%<#pragma omp target exit data%> must contain at least "
35200 "one %<map%> clause");
35201 return NULL_TREE;
35204 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
35205 TREE_TYPE (stmt) = void_type_node;
35206 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
35207 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35208 return add_stmt (stmt);
35211 /* OpenMP 4.0:
35212 # pragma omp target update target-update-clause[optseq] new-line */
35214 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
35215 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
35216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
35217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35222 static bool
35223 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
35224 enum pragma_context context)
35226 if (context == pragma_stmt)
35228 error_at (pragma_tok->location,
35229 "%<#pragma omp target update%> may only be "
35230 "used in compound statements");
35231 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35232 return false;
35235 tree clauses
35236 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
35237 "#pragma omp target update", pragma_tok);
35238 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
35239 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
35241 error_at (pragma_tok->location,
35242 "%<#pragma omp target update%> must contain at least one "
35243 "%<from%> or %<to%> clauses");
35244 return false;
35247 tree stmt = make_node (OMP_TARGET_UPDATE);
35248 TREE_TYPE (stmt) = void_type_node;
35249 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
35250 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35251 add_stmt (stmt);
35252 return false;
35255 /* OpenMP 4.0:
35256 # pragma omp target target-clause[optseq] new-line
35257 structured-block */
35259 #define OMP_TARGET_CLAUSE_MASK \
35260 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35261 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35262 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35263 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35264 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35265 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35266 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
35268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
35270 static bool
35271 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
35272 enum pragma_context context, bool *if_p)
35274 tree *pc = NULL, stmt;
35276 if (context != pragma_stmt && context != pragma_compound)
35278 cp_parser_error (parser, "expected declaration specifiers");
35279 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35280 return false;
35283 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35285 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35286 const char *p = IDENTIFIER_POINTER (id);
35287 enum tree_code ccode = ERROR_MARK;
35289 if (strcmp (p, "teams") == 0)
35290 ccode = OMP_TEAMS;
35291 else if (strcmp (p, "parallel") == 0)
35292 ccode = OMP_PARALLEL;
35293 else if (strcmp (p, "simd") == 0)
35294 ccode = OMP_SIMD;
35295 if (ccode != ERROR_MARK)
35297 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
35298 char p_name[sizeof ("#pragma omp target teams distribute "
35299 "parallel for simd")];
35301 cp_lexer_consume_token (parser->lexer);
35302 strcpy (p_name, "#pragma omp target");
35303 if (!flag_openmp) /* flag_openmp_simd */
35305 tree stmt;
35306 switch (ccode)
35308 case OMP_TEAMS:
35309 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
35310 OMP_TARGET_CLAUSE_MASK,
35311 cclauses, if_p);
35312 break;
35313 case OMP_PARALLEL:
35314 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
35315 OMP_TARGET_CLAUSE_MASK,
35316 cclauses, if_p);
35317 break;
35318 case OMP_SIMD:
35319 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
35320 OMP_TARGET_CLAUSE_MASK,
35321 cclauses, if_p);
35322 break;
35323 default:
35324 gcc_unreachable ();
35326 return stmt != NULL_TREE;
35328 keep_next_level (true);
35329 tree sb = begin_omp_structured_block (), ret;
35330 unsigned save = cp_parser_begin_omp_structured_block (parser);
35331 switch (ccode)
35333 case OMP_TEAMS:
35334 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
35335 OMP_TARGET_CLAUSE_MASK, cclauses,
35336 if_p);
35337 break;
35338 case OMP_PARALLEL:
35339 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
35340 OMP_TARGET_CLAUSE_MASK, cclauses,
35341 if_p);
35342 break;
35343 case OMP_SIMD:
35344 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
35345 OMP_TARGET_CLAUSE_MASK, cclauses,
35346 if_p);
35347 break;
35348 default:
35349 gcc_unreachable ();
35351 cp_parser_end_omp_structured_block (parser, save);
35352 tree body = finish_omp_structured_block (sb);
35353 if (ret == NULL_TREE)
35354 return false;
35355 if (ccode == OMP_TEAMS && !processing_template_decl)
35357 /* For combined target teams, ensure the num_teams and
35358 thread_limit clause expressions are evaluated on the host,
35359 before entering the target construct. */
35360 tree c;
35361 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35362 c; c = OMP_CLAUSE_CHAIN (c))
35363 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
35364 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
35365 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
35367 tree expr = OMP_CLAUSE_OPERAND (c, 0);
35368 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
35369 if (expr == error_mark_node)
35370 continue;
35371 tree tmp = TARGET_EXPR_SLOT (expr);
35372 add_stmt (expr);
35373 OMP_CLAUSE_OPERAND (c, 0) = expr;
35374 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
35375 OMP_CLAUSE_FIRSTPRIVATE);
35376 OMP_CLAUSE_DECL (tc) = tmp;
35377 OMP_CLAUSE_CHAIN (tc)
35378 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35379 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
35382 tree stmt = make_node (OMP_TARGET);
35383 TREE_TYPE (stmt) = void_type_node;
35384 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35385 OMP_TARGET_BODY (stmt) = body;
35386 OMP_TARGET_COMBINED (stmt) = 1;
35387 add_stmt (stmt);
35388 pc = &OMP_TARGET_CLAUSES (stmt);
35389 goto check_clauses;
35391 else if (!flag_openmp) /* flag_openmp_simd */
35393 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35394 return false;
35396 else if (strcmp (p, "data") == 0)
35398 cp_lexer_consume_token (parser->lexer);
35399 cp_parser_omp_target_data (parser, pragma_tok, if_p);
35400 return true;
35402 else if (strcmp (p, "enter") == 0)
35404 cp_lexer_consume_token (parser->lexer);
35405 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
35406 return false;
35408 else if (strcmp (p, "exit") == 0)
35410 cp_lexer_consume_token (parser->lexer);
35411 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
35412 return false;
35414 else if (strcmp (p, "update") == 0)
35416 cp_lexer_consume_token (parser->lexer);
35417 return cp_parser_omp_target_update (parser, pragma_tok, context);
35421 stmt = make_node (OMP_TARGET);
35422 TREE_TYPE (stmt) = void_type_node;
35424 OMP_TARGET_CLAUSES (stmt)
35425 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
35426 "#pragma omp target", pragma_tok);
35427 pc = &OMP_TARGET_CLAUSES (stmt);
35428 keep_next_level (true);
35429 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35431 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35432 add_stmt (stmt);
35434 check_clauses:
35435 while (*pc)
35437 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35438 switch (OMP_CLAUSE_MAP_KIND (*pc))
35440 case GOMP_MAP_TO:
35441 case GOMP_MAP_ALWAYS_TO:
35442 case GOMP_MAP_FROM:
35443 case GOMP_MAP_ALWAYS_FROM:
35444 case GOMP_MAP_TOFROM:
35445 case GOMP_MAP_ALWAYS_TOFROM:
35446 case GOMP_MAP_ALLOC:
35447 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35448 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35449 case GOMP_MAP_ALWAYS_POINTER:
35450 break;
35451 default:
35452 error_at (OMP_CLAUSE_LOCATION (*pc),
35453 "%<#pragma omp target%> with map-type other "
35454 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35455 "on %<map%> clause");
35456 *pc = OMP_CLAUSE_CHAIN (*pc);
35457 continue;
35459 pc = &OMP_CLAUSE_CHAIN (*pc);
35461 return true;
35464 /* OpenACC 2.0:
35465 # pragma acc cache (variable-list) new-line
35468 static tree
35469 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
35471 tree stmt, clauses;
35473 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
35474 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
35476 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
35478 stmt = make_node (OACC_CACHE);
35479 TREE_TYPE (stmt) = void_type_node;
35480 OACC_CACHE_CLAUSES (stmt) = clauses;
35481 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35482 add_stmt (stmt);
35484 return stmt;
35487 /* OpenACC 2.0:
35488 # pragma acc data oacc-data-clause[optseq] new-line
35489 structured-block */
35491 #define OACC_DATA_CLAUSE_MASK \
35492 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35493 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35494 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35495 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35496 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35497 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35498 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35499 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35500 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35501 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35502 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
35504 static tree
35505 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35507 tree stmt, clauses, block;
35508 unsigned int save;
35510 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
35511 "#pragma acc data", pragma_tok);
35513 block = begin_omp_parallel ();
35514 save = cp_parser_begin_omp_structured_block (parser);
35515 cp_parser_statement (parser, NULL_TREE, false, if_p);
35516 cp_parser_end_omp_structured_block (parser, save);
35517 stmt = finish_oacc_data (clauses, block);
35518 return stmt;
35521 /* OpenACC 2.0:
35522 # pragma acc host_data <clauses> new-line
35523 structured-block */
35525 #define OACC_HOST_DATA_CLAUSE_MASK \
35526 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
35528 static tree
35529 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35531 tree stmt, clauses, block;
35532 unsigned int save;
35534 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
35535 "#pragma acc host_data", pragma_tok);
35537 block = begin_omp_parallel ();
35538 save = cp_parser_begin_omp_structured_block (parser);
35539 cp_parser_statement (parser, NULL_TREE, false, if_p);
35540 cp_parser_end_omp_structured_block (parser, save);
35541 stmt = finish_oacc_host_data (clauses, block);
35542 return stmt;
35545 /* OpenACC 2.0:
35546 # pragma acc declare oacc-data-clause[optseq] new-line
35549 #define OACC_DECLARE_CLAUSE_MASK \
35550 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35554 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
35556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
35557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35561 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
35563 static tree
35564 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
35566 tree clauses, stmt;
35567 bool error = false;
35569 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
35570 "#pragma acc declare", pragma_tok, true);
35573 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35575 error_at (pragma_tok->location,
35576 "no valid clauses specified in %<#pragma acc declare%>");
35577 return NULL_TREE;
35580 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
35582 location_t loc = OMP_CLAUSE_LOCATION (t);
35583 tree decl = OMP_CLAUSE_DECL (t);
35584 if (!DECL_P (decl))
35586 error_at (loc, "array section in %<#pragma acc declare%>");
35587 error = true;
35588 continue;
35590 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
35591 switch (OMP_CLAUSE_MAP_KIND (t))
35593 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35594 case GOMP_MAP_FORCE_ALLOC:
35595 case GOMP_MAP_FORCE_TO:
35596 case GOMP_MAP_FORCE_DEVICEPTR:
35597 case GOMP_MAP_DEVICE_RESIDENT:
35598 break;
35600 case GOMP_MAP_LINK:
35601 if (!global_bindings_p ()
35602 && (TREE_STATIC (decl)
35603 || !DECL_EXTERNAL (decl)))
35605 error_at (loc,
35606 "%qD must be a global variable in"
35607 "%<#pragma acc declare link%>",
35608 decl);
35609 error = true;
35610 continue;
35612 break;
35614 default:
35615 if (global_bindings_p ())
35617 error_at (loc, "invalid OpenACC clause at file scope");
35618 error = true;
35619 continue;
35621 if (DECL_EXTERNAL (decl))
35623 error_at (loc,
35624 "invalid use of %<extern%> variable %qD "
35625 "in %<#pragma acc declare%>", decl);
35626 error = true;
35627 continue;
35629 else if (TREE_PUBLIC (decl))
35631 error_at (loc,
35632 "invalid use of %<global%> variable %qD "
35633 "in %<#pragma acc declare%>", decl);
35634 error = true;
35635 continue;
35637 break;
35640 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
35641 || lookup_attribute ("omp declare target link",
35642 DECL_ATTRIBUTES (decl)))
35644 error_at (loc, "variable %qD used more than once with "
35645 "%<#pragma acc declare%>", decl);
35646 error = true;
35647 continue;
35650 if (!error)
35652 tree id;
35654 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
35655 id = get_identifier ("omp declare target link");
35656 else
35657 id = get_identifier ("omp declare target");
35659 DECL_ATTRIBUTES (decl)
35660 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
35661 if (global_bindings_p ())
35663 symtab_node *node = symtab_node::get (decl);
35664 if (node != NULL)
35666 node->offloadable = 1;
35667 if (ENABLE_OFFLOADING)
35669 g->have_offload = true;
35670 if (is_a <varpool_node *> (node))
35671 vec_safe_push (offload_vars, decl);
35678 if (error || global_bindings_p ())
35679 return NULL_TREE;
35681 stmt = make_node (OACC_DECLARE);
35682 TREE_TYPE (stmt) = void_type_node;
35683 OACC_DECLARE_CLAUSES (stmt) = clauses;
35684 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35686 add_stmt (stmt);
35688 return NULL_TREE;
35691 /* OpenACC 2.0:
35692 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
35696 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
35698 LOC is the location of the #pragma token.
35701 #define OACC_ENTER_DATA_CLAUSE_MASK \
35702 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35703 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35704 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35705 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35706 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35707 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35708 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35710 #define OACC_EXIT_DATA_CLAUSE_MASK \
35711 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35712 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35714 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
35715 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35717 static tree
35718 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
35719 bool enter)
35721 tree stmt, clauses;
35723 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
35724 || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
35726 cp_parser_error (parser, enter
35727 ? "expected %<data%> in %<#pragma acc enter data%>"
35728 : "expected %<data%> in %<#pragma acc exit data%>");
35729 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35730 return NULL_TREE;
35733 const char *p =
35734 IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
35735 if (strcmp (p, "data") != 0)
35737 cp_parser_error (parser, "invalid pragma");
35738 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35739 return NULL_TREE;
35742 cp_lexer_consume_token (parser->lexer);
35744 if (enter)
35745 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
35746 "#pragma acc enter data", pragma_tok);
35747 else
35748 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
35749 "#pragma acc exit data", pragma_tok);
35751 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35753 error_at (pragma_tok->location,
35754 "%<#pragma acc enter data%> has no data movement clause");
35755 return NULL_TREE;
35758 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
35759 TREE_TYPE (stmt) = void_type_node;
35760 OMP_STANDALONE_CLAUSES (stmt) = clauses;
35761 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35762 add_stmt (stmt);
35763 return stmt;
35766 /* OpenACC 2.0:
35767 # pragma acc loop oacc-loop-clause[optseq] new-line
35768 structured-block */
35770 #define OACC_LOOP_CLAUSE_MASK \
35771 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
35772 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
35773 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
35774 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
35775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
35776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
35777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
35778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
35779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
35780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
35782 static tree
35783 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
35784 omp_clause_mask mask, tree *cclauses, bool *if_p)
35786 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
35788 strcat (p_name, " loop");
35789 mask |= OACC_LOOP_CLAUSE_MASK;
35791 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
35792 cclauses == NULL);
35793 if (cclauses)
35795 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
35796 if (*cclauses)
35797 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
35798 if (clauses)
35799 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
35802 tree block = begin_omp_structured_block ();
35803 int save = cp_parser_begin_omp_structured_block (parser);
35804 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
35805 cp_parser_end_omp_structured_block (parser, save);
35806 add_stmt (finish_omp_structured_block (block));
35808 return stmt;
35811 /* OpenACC 2.0:
35812 # pragma acc kernels oacc-kernels-clause[optseq] new-line
35813 structured-block
35817 # pragma acc parallel oacc-parallel-clause[optseq] new-line
35818 structured-block
35821 #define OACC_KERNELS_CLAUSE_MASK \
35822 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
35828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35837 #define OACC_PARALLEL_CLAUSE_MASK \
35838 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
35844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
35846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
35848 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
35849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35851 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
35855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
35856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
35857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35859 static tree
35860 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
35861 char *p_name, bool *if_p)
35863 omp_clause_mask mask;
35864 enum tree_code code;
35865 switch (cp_parser_pragma_kind (pragma_tok))
35867 case PRAGMA_OACC_KERNELS:
35868 strcat (p_name, " kernels");
35869 mask = OACC_KERNELS_CLAUSE_MASK;
35870 code = OACC_KERNELS;
35871 break;
35872 case PRAGMA_OACC_PARALLEL:
35873 strcat (p_name, " parallel");
35874 mask = OACC_PARALLEL_CLAUSE_MASK;
35875 code = OACC_PARALLEL;
35876 break;
35877 default:
35878 gcc_unreachable ();
35881 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35883 const char *p
35884 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
35885 if (strcmp (p, "loop") == 0)
35887 cp_lexer_consume_token (parser->lexer);
35888 tree block = begin_omp_parallel ();
35889 tree clauses;
35890 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
35891 if_p);
35892 return finish_omp_construct (code, block, clauses);
35896 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
35898 tree block = begin_omp_parallel ();
35899 unsigned int save = cp_parser_begin_omp_structured_block (parser);
35900 cp_parser_statement (parser, NULL_TREE, false, if_p);
35901 cp_parser_end_omp_structured_block (parser, save);
35902 return finish_omp_construct (code, block, clauses);
35905 /* OpenACC 2.0:
35906 # pragma acc update oacc-update-clause[optseq] new-line
35909 #define OACC_UPDATE_CLAUSE_MASK \
35910 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
35912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
35913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35914 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
35915 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
35917 static tree
35918 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
35920 tree stmt, clauses;
35922 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
35923 "#pragma acc update", pragma_tok);
35925 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35927 error_at (pragma_tok->location,
35928 "%<#pragma acc update%> must contain at least one "
35929 "%<device%> or %<host%> or %<self%> clause");
35930 return NULL_TREE;
35933 stmt = make_node (OACC_UPDATE);
35934 TREE_TYPE (stmt) = void_type_node;
35935 OACC_UPDATE_CLAUSES (stmt) = clauses;
35936 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35937 add_stmt (stmt);
35938 return stmt;
35941 /* OpenACC 2.0:
35942 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
35944 LOC is the location of the #pragma token.
35947 #define OACC_WAIT_CLAUSE_MASK \
35948 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
35950 static tree
35951 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
35953 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
35954 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35956 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
35957 list = cp_parser_oacc_wait_list (parser, loc, list);
35959 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
35960 "#pragma acc wait", pragma_tok);
35962 stmt = c_finish_oacc_wait (loc, list, clauses);
35963 stmt = finish_expr_stmt (stmt);
35965 return stmt;
35968 /* OpenMP 4.0:
35969 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
35971 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
35972 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
35976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
35977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
35979 static void
35980 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
35981 enum pragma_context context)
35983 bool first_p = parser->omp_declare_simd == NULL;
35984 cp_omp_declare_simd_data data;
35985 if (first_p)
35987 data.error_seen = false;
35988 data.fndecl_seen = false;
35989 data.tokens = vNULL;
35990 data.clauses = NULL_TREE;
35991 /* It is safe to take the address of a local variable; it will only be
35992 used while this scope is live. */
35993 parser->omp_declare_simd = &data;
35996 /* Store away all pragma tokens. */
35997 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
35998 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
35999 cp_lexer_consume_token (parser->lexer);
36000 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36001 parser->omp_declare_simd->error_seen = true;
36002 cp_parser_require_pragma_eol (parser, pragma_tok);
36003 struct cp_token_cache *cp
36004 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
36005 parser->omp_declare_simd->tokens.safe_push (cp);
36007 if (first_p)
36009 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
36010 cp_parser_pragma (parser, context, NULL);
36011 switch (context)
36013 case pragma_external:
36014 cp_parser_declaration (parser);
36015 break;
36016 case pragma_member:
36017 cp_parser_member_declaration (parser);
36018 break;
36019 case pragma_objc_icode:
36020 cp_parser_block_declaration (parser, /*statement_p=*/false);
36021 break;
36022 default:
36023 cp_parser_declaration_statement (parser);
36024 break;
36026 if (parser->omp_declare_simd
36027 && !parser->omp_declare_simd->error_seen
36028 && !parser->omp_declare_simd->fndecl_seen)
36029 error_at (pragma_tok->location,
36030 "%<#pragma omp declare simd%> not immediately followed by "
36031 "function declaration or definition");
36032 data.tokens.release ();
36033 parser->omp_declare_simd = NULL;
36037 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
36038 This function is modelled similar to the late parsing of omp declare
36039 simd. */
36041 static tree
36042 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
36044 struct cp_token_cache *ce;
36045 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
36046 int ii = 0;
36048 if (parser->omp_declare_simd != NULL
36049 || lookup_attribute ("simd", attrs))
36051 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
36052 "used in the same function marked as a Cilk Plus SIMD-enabled "
36053 "function");
36054 parser->cilk_simd_fn_info->tokens.release ();
36055 XDELETE (parser->cilk_simd_fn_info);
36056 parser->cilk_simd_fn_info = NULL;
36057 return attrs;
36059 if (!info->error_seen && info->fndecl_seen)
36061 error ("vector attribute not immediately followed by a single function"
36062 " declaration or definition");
36063 info->error_seen = true;
36065 if (info->error_seen)
36066 return attrs;
36068 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
36070 tree c, cl;
36072 cp_parser_push_lexer_for_tokens (parser, ce);
36073 parser->lexer->in_pragma = true;
36074 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
36075 "SIMD-enabled functions attribute",
36076 NULL);
36077 cp_parser_pop_lexer (parser);
36078 if (cl)
36079 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
36081 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
36082 TREE_CHAIN (c) = attrs;
36083 attrs = c;
36085 c = build_tree_list (get_identifier ("omp declare simd"), cl);
36086 TREE_CHAIN (c) = attrs;
36087 if (processing_template_decl)
36088 ATTR_IS_DEPENDENT (c) = 1;
36089 attrs = c;
36091 info->fndecl_seen = true;
36092 parser->cilk_simd_fn_info->tokens.release ();
36093 XDELETE (parser->cilk_simd_fn_info);
36094 parser->cilk_simd_fn_info = NULL;
36095 return attrs;
36098 /* Finalize #pragma omp declare simd clauses after direct declarator has
36099 been parsed, and put that into "omp declare simd" attribute. */
36101 static tree
36102 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
36104 struct cp_token_cache *ce;
36105 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
36106 int i;
36108 if (!data->error_seen && data->fndecl_seen)
36110 error ("%<#pragma omp declare simd%> not immediately followed by "
36111 "a single function declaration or definition");
36112 data->error_seen = true;
36114 if (data->error_seen)
36115 return attrs;
36117 FOR_EACH_VEC_ELT (data->tokens, i, ce)
36119 tree c, cl;
36121 cp_parser_push_lexer_for_tokens (parser, ce);
36122 parser->lexer->in_pragma = true;
36123 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
36124 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
36125 cp_lexer_consume_token (parser->lexer);
36126 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
36127 "#pragma omp declare simd", pragma_tok);
36128 cp_parser_pop_lexer (parser);
36129 if (cl)
36130 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
36131 c = build_tree_list (get_identifier ("omp declare simd"), cl);
36132 TREE_CHAIN (c) = attrs;
36133 if (processing_template_decl)
36134 ATTR_IS_DEPENDENT (c) = 1;
36135 attrs = c;
36138 data->fndecl_seen = true;
36139 return attrs;
36143 /* OpenMP 4.0:
36144 # pragma omp declare target new-line
36145 declarations and definitions
36146 # pragma omp end declare target new-line
36148 OpenMP 4.5:
36149 # pragma omp declare target ( extended-list ) new-line
36151 # pragma omp declare target declare-target-clauses[seq] new-line */
36153 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
36154 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
36157 static void
36158 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
36160 tree clauses = NULL_TREE;
36161 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36162 clauses
36163 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
36164 "#pragma omp declare target", pragma_tok);
36165 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36167 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
36168 clauses);
36169 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
36170 cp_parser_require_pragma_eol (parser, pragma_tok);
36172 else
36174 cp_parser_require_pragma_eol (parser, pragma_tok);
36175 scope_chain->omp_declare_target_attribute++;
36176 return;
36178 if (scope_chain->omp_declare_target_attribute)
36179 error_at (pragma_tok->location,
36180 "%<#pragma omp declare target%> with clauses in between "
36181 "%<#pragma omp declare target%> without clauses and "
36182 "%<#pragma omp end declare target%>");
36183 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
36185 tree t = OMP_CLAUSE_DECL (c), id;
36186 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
36187 tree at2 = lookup_attribute ("omp declare target link",
36188 DECL_ATTRIBUTES (t));
36189 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
36191 id = get_identifier ("omp declare target link");
36192 std::swap (at1, at2);
36194 else
36195 id = get_identifier ("omp declare target");
36196 if (at2)
36198 error_at (OMP_CLAUSE_LOCATION (c),
36199 "%qD specified both in declare target %<link%> and %<to%>"
36200 " clauses", t);
36201 continue;
36203 if (!at1)
36205 symtab_node *node = symtab_node::get (t);
36206 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
36207 if (node != NULL)
36209 node->offloadable = 1;
36210 if (ENABLE_OFFLOADING)
36212 g->have_offload = true;
36213 if (is_a <varpool_node *> (node))
36214 vec_safe_push (offload_vars, t);
36221 static void
36222 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
36224 const char *p = "";
36225 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36227 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36228 p = IDENTIFIER_POINTER (id);
36230 if (strcmp (p, "declare") == 0)
36232 cp_lexer_consume_token (parser->lexer);
36233 p = "";
36234 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36236 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36237 p = IDENTIFIER_POINTER (id);
36239 if (strcmp (p, "target") == 0)
36240 cp_lexer_consume_token (parser->lexer);
36241 else
36243 cp_parser_error (parser, "expected %<target%>");
36244 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36245 return;
36248 else
36250 cp_parser_error (parser, "expected %<declare%>");
36251 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36252 return;
36254 cp_parser_require_pragma_eol (parser, pragma_tok);
36255 if (!scope_chain->omp_declare_target_attribute)
36256 error_at (pragma_tok->location,
36257 "%<#pragma omp end declare target%> without corresponding "
36258 "%<#pragma omp declare target%>");
36259 else
36260 scope_chain->omp_declare_target_attribute--;
36263 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
36264 expression and optional initializer clause of
36265 #pragma omp declare reduction. We store the expression(s) as
36266 either 3, 6 or 7 special statements inside of the artificial function's
36267 body. The first two statements are DECL_EXPRs for the artificial
36268 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
36269 expression that uses those variables.
36270 If there was any INITIALIZER clause, this is followed by further statements,
36271 the fourth and fifth statements are DECL_EXPRs for the artificial
36272 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
36273 constructor variant (first token after open paren is not omp_priv),
36274 then the sixth statement is a statement with the function call expression
36275 that uses the OMP_PRIV and optionally OMP_ORIG variable.
36276 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
36277 to initialize the OMP_PRIV artificial variable and there is seventh
36278 statement, a DECL_EXPR of the OMP_PRIV statement again. */
36280 static bool
36281 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
36283 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
36284 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
36285 type = TREE_TYPE (type);
36286 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
36287 DECL_ARTIFICIAL (omp_out) = 1;
36288 pushdecl (omp_out);
36289 add_decl_expr (omp_out);
36290 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
36291 DECL_ARTIFICIAL (omp_in) = 1;
36292 pushdecl (omp_in);
36293 add_decl_expr (omp_in);
36294 tree combiner;
36295 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
36297 keep_next_level (true);
36298 tree block = begin_omp_structured_block ();
36299 combiner = cp_parser_expression (parser);
36300 finish_expr_stmt (combiner);
36301 block = finish_omp_structured_block (block);
36302 add_stmt (block);
36304 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36305 return false;
36307 const char *p = "";
36308 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36310 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36311 p = IDENTIFIER_POINTER (id);
36314 if (strcmp (p, "initializer") == 0)
36316 cp_lexer_consume_token (parser->lexer);
36317 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36318 return false;
36320 p = "";
36321 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36323 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36324 p = IDENTIFIER_POINTER (id);
36327 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
36328 DECL_ARTIFICIAL (omp_priv) = 1;
36329 pushdecl (omp_priv);
36330 add_decl_expr (omp_priv);
36331 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
36332 DECL_ARTIFICIAL (omp_orig) = 1;
36333 pushdecl (omp_orig);
36334 add_decl_expr (omp_orig);
36336 keep_next_level (true);
36337 block = begin_omp_structured_block ();
36339 bool ctor = false;
36340 if (strcmp (p, "omp_priv") == 0)
36342 bool is_direct_init, is_non_constant_init;
36343 ctor = true;
36344 cp_lexer_consume_token (parser->lexer);
36345 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
36346 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
36347 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36348 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
36349 == CPP_CLOSE_PAREN
36350 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
36351 == CPP_CLOSE_PAREN))
36353 finish_omp_structured_block (block);
36354 error ("invalid initializer clause");
36355 return false;
36357 initializer = cp_parser_initializer (parser, &is_direct_init,
36358 &is_non_constant_init);
36359 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
36360 NULL_TREE, LOOKUP_ONLYCONVERTING);
36362 else
36364 cp_parser_parse_tentatively (parser);
36365 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
36366 /*check_dependency_p=*/true,
36367 /*template_p=*/NULL,
36368 /*declarator_p=*/false,
36369 /*optional_p=*/false);
36370 vec<tree, va_gc> *args;
36371 if (fn_name == error_mark_node
36372 || cp_parser_error_occurred (parser)
36373 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36374 || ((args = cp_parser_parenthesized_expression_list
36375 (parser, non_attr, /*cast_p=*/false,
36376 /*allow_expansion_p=*/true,
36377 /*non_constant_p=*/NULL)),
36378 cp_parser_error_occurred (parser)))
36380 finish_omp_structured_block (block);
36381 cp_parser_abort_tentative_parse (parser);
36382 cp_parser_error (parser, "expected id-expression (arguments)");
36383 return false;
36385 unsigned int i;
36386 tree arg;
36387 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
36388 if (arg == omp_priv
36389 || (TREE_CODE (arg) == ADDR_EXPR
36390 && TREE_OPERAND (arg, 0) == omp_priv))
36391 break;
36392 cp_parser_abort_tentative_parse (parser);
36393 if (arg == NULL_TREE)
36394 error ("one of the initializer call arguments should be %<omp_priv%>"
36395 " or %<&omp_priv%>");
36396 initializer = cp_parser_postfix_expression (parser, false, false, false,
36397 false, NULL);
36398 finish_expr_stmt (initializer);
36401 block = finish_omp_structured_block (block);
36402 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
36403 add_stmt (block);
36405 if (ctor)
36406 add_decl_expr (omp_orig);
36408 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36409 return false;
36412 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
36413 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
36415 return true;
36418 /* OpenMP 4.0
36419 #pragma omp declare reduction (reduction-id : typename-list : expression) \
36420 initializer-clause[opt] new-line
36422 initializer-clause:
36423 initializer (omp_priv initializer)
36424 initializer (function-name (argument-list)) */
36426 static void
36427 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
36428 enum pragma_context)
36430 auto_vec<tree> types;
36431 enum tree_code reduc_code = ERROR_MARK;
36432 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
36433 unsigned int i;
36434 cp_token *first_token;
36435 cp_token_cache *cp;
36436 int errs;
36437 void *p;
36439 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
36440 p = obstack_alloc (&declarator_obstack, 0);
36442 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36443 goto fail;
36445 switch (cp_lexer_peek_token (parser->lexer)->type)
36447 case CPP_PLUS:
36448 reduc_code = PLUS_EXPR;
36449 break;
36450 case CPP_MULT:
36451 reduc_code = MULT_EXPR;
36452 break;
36453 case CPP_MINUS:
36454 reduc_code = MINUS_EXPR;
36455 break;
36456 case CPP_AND:
36457 reduc_code = BIT_AND_EXPR;
36458 break;
36459 case CPP_XOR:
36460 reduc_code = BIT_XOR_EXPR;
36461 break;
36462 case CPP_OR:
36463 reduc_code = BIT_IOR_EXPR;
36464 break;
36465 case CPP_AND_AND:
36466 reduc_code = TRUTH_ANDIF_EXPR;
36467 break;
36468 case CPP_OR_OR:
36469 reduc_code = TRUTH_ORIF_EXPR;
36470 break;
36471 case CPP_NAME:
36472 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
36473 break;
36474 default:
36475 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
36476 "%<|%>, %<&&%>, %<||%> or identifier");
36477 goto fail;
36480 if (reduc_code != ERROR_MARK)
36481 cp_lexer_consume_token (parser->lexer);
36483 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
36484 if (reduc_id == error_mark_node)
36485 goto fail;
36487 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36488 goto fail;
36490 /* Types may not be defined in declare reduction type list. */
36491 const char *saved_message;
36492 saved_message = parser->type_definition_forbidden_message;
36493 parser->type_definition_forbidden_message
36494 = G_("types may not be defined in declare reduction type list");
36495 bool saved_colon_corrects_to_scope_p;
36496 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
36497 parser->colon_corrects_to_scope_p = false;
36498 bool saved_colon_doesnt_start_class_def_p;
36499 saved_colon_doesnt_start_class_def_p
36500 = parser->colon_doesnt_start_class_def_p;
36501 parser->colon_doesnt_start_class_def_p = true;
36503 while (true)
36505 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36506 type = cp_parser_type_id (parser);
36507 if (type == error_mark_node)
36509 else if (ARITHMETIC_TYPE_P (type)
36510 && (orig_reduc_id == NULL_TREE
36511 || (TREE_CODE (type) != COMPLEX_TYPE
36512 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
36513 "min") == 0
36514 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
36515 "max") == 0))))
36516 error_at (loc, "predeclared arithmetic type %qT in "
36517 "%<#pragma omp declare reduction%>", type);
36518 else if (TREE_CODE (type) == FUNCTION_TYPE
36519 || TREE_CODE (type) == METHOD_TYPE
36520 || TREE_CODE (type) == ARRAY_TYPE)
36521 error_at (loc, "function or array type %qT in "
36522 "%<#pragma omp declare reduction%>", type);
36523 else if (TREE_CODE (type) == REFERENCE_TYPE)
36524 error_at (loc, "reference type %qT in "
36525 "%<#pragma omp declare reduction%>", type);
36526 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
36527 error_at (loc, "const, volatile or __restrict qualified type %qT in "
36528 "%<#pragma omp declare reduction%>", type);
36529 else
36530 types.safe_push (type);
36532 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36533 cp_lexer_consume_token (parser->lexer);
36534 else
36535 break;
36538 /* Restore the saved message. */
36539 parser->type_definition_forbidden_message = saved_message;
36540 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36541 parser->colon_doesnt_start_class_def_p
36542 = saved_colon_doesnt_start_class_def_p;
36544 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
36545 || types.is_empty ())
36547 fail:
36548 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36549 goto done;
36552 first_token = cp_lexer_peek_token (parser->lexer);
36553 cp = NULL;
36554 errs = errorcount;
36555 FOR_EACH_VEC_ELT (types, i, type)
36557 tree fntype
36558 = build_function_type_list (void_type_node,
36559 cp_build_reference_type (type, false),
36560 NULL_TREE);
36561 tree this_reduc_id = reduc_id;
36562 if (!dependent_type_p (type))
36563 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
36564 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
36565 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
36566 DECL_ARTIFICIAL (fndecl) = 1;
36567 DECL_EXTERNAL (fndecl) = 1;
36568 DECL_DECLARED_INLINE_P (fndecl) = 1;
36569 DECL_IGNORED_P (fndecl) = 1;
36570 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
36571 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
36572 DECL_ATTRIBUTES (fndecl)
36573 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
36574 DECL_ATTRIBUTES (fndecl));
36575 if (processing_template_decl)
36576 fndecl = push_template_decl (fndecl);
36577 bool block_scope = false;
36578 tree block = NULL_TREE;
36579 if (current_function_decl)
36581 block_scope = true;
36582 DECL_CONTEXT (fndecl) = global_namespace;
36583 if (!processing_template_decl)
36584 pushdecl (fndecl);
36586 else if (current_class_type)
36588 if (cp == NULL)
36590 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36591 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36592 cp_lexer_consume_token (parser->lexer);
36593 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36594 goto fail;
36595 cp = cp_token_cache_new (first_token,
36596 cp_lexer_peek_nth_token (parser->lexer,
36597 2));
36599 DECL_STATIC_FUNCTION_P (fndecl) = 1;
36600 finish_member_declaration (fndecl);
36601 DECL_PENDING_INLINE_INFO (fndecl) = cp;
36602 DECL_PENDING_INLINE_P (fndecl) = 1;
36603 vec_safe_push (unparsed_funs_with_definitions, fndecl);
36604 continue;
36606 else
36608 DECL_CONTEXT (fndecl) = current_namespace;
36609 pushdecl (fndecl);
36611 if (!block_scope)
36612 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
36613 else
36614 block = begin_omp_structured_block ();
36615 if (cp)
36617 cp_parser_push_lexer_for_tokens (parser, cp);
36618 parser->lexer->in_pragma = true;
36620 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
36622 if (!block_scope)
36623 finish_function (0);
36624 else
36625 DECL_CONTEXT (fndecl) = current_function_decl;
36626 if (cp)
36627 cp_parser_pop_lexer (parser);
36628 goto fail;
36630 if (cp)
36631 cp_parser_pop_lexer (parser);
36632 if (!block_scope)
36633 finish_function (0);
36634 else
36636 DECL_CONTEXT (fndecl) = current_function_decl;
36637 block = finish_omp_structured_block (block);
36638 if (TREE_CODE (block) == BIND_EXPR)
36639 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
36640 else if (TREE_CODE (block) == STATEMENT_LIST)
36641 DECL_SAVED_TREE (fndecl) = block;
36642 if (processing_template_decl)
36643 add_decl_expr (fndecl);
36645 cp_check_omp_declare_reduction (fndecl);
36646 if (cp == NULL && types.length () > 1)
36647 cp = cp_token_cache_new (first_token,
36648 cp_lexer_peek_nth_token (parser->lexer, 2));
36649 if (errs != errorcount)
36650 break;
36653 cp_parser_require_pragma_eol (parser, pragma_tok);
36655 done:
36656 /* Free any declarators allocated. */
36657 obstack_free (&declarator_obstack, p);
36660 /* OpenMP 4.0
36661 #pragma omp declare simd declare-simd-clauses[optseq] new-line
36662 #pragma omp declare reduction (reduction-id : typename-list : expression) \
36663 initializer-clause[opt] new-line
36664 #pragma omp declare target new-line */
36666 static void
36667 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
36668 enum pragma_context context)
36670 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36672 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36673 const char *p = IDENTIFIER_POINTER (id);
36675 if (strcmp (p, "simd") == 0)
36677 cp_lexer_consume_token (parser->lexer);
36678 cp_parser_omp_declare_simd (parser, pragma_tok,
36679 context);
36680 return;
36682 cp_ensure_no_omp_declare_simd (parser);
36683 if (strcmp (p, "reduction") == 0)
36685 cp_lexer_consume_token (parser->lexer);
36686 cp_parser_omp_declare_reduction (parser, pragma_tok,
36687 context);
36688 return;
36690 if (!flag_openmp) /* flag_openmp_simd */
36692 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36693 return;
36695 if (strcmp (p, "target") == 0)
36697 cp_lexer_consume_token (parser->lexer);
36698 cp_parser_omp_declare_target (parser, pragma_tok);
36699 return;
36702 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
36703 "or %<target%>");
36704 cp_parser_require_pragma_eol (parser, pragma_tok);
36707 /* OpenMP 4.5:
36708 #pragma omp taskloop taskloop-clause[optseq] new-line
36709 for-loop
36711 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
36712 for-loop */
36714 #define OMP_TASKLOOP_CLAUSE_MASK \
36715 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
36720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
36721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
36722 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
36723 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
36724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36725 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
36726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
36727 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
36728 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
36730 static tree
36731 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
36732 char *p_name, omp_clause_mask mask, tree *cclauses,
36733 bool *if_p)
36735 tree clauses, sb, ret;
36736 unsigned int save;
36737 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36739 strcat (p_name, " taskloop");
36740 mask |= OMP_TASKLOOP_CLAUSE_MASK;
36742 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36744 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36745 const char *p = IDENTIFIER_POINTER (id);
36747 if (strcmp (p, "simd") == 0)
36749 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36750 if (cclauses == NULL)
36751 cclauses = cclauses_buf;
36753 cp_lexer_consume_token (parser->lexer);
36754 if (!flag_openmp) /* flag_openmp_simd */
36755 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36756 cclauses, if_p);
36757 sb = begin_omp_structured_block ();
36758 save = cp_parser_begin_omp_structured_block (parser);
36759 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36760 cclauses, if_p);
36761 cp_parser_end_omp_structured_block (parser, save);
36762 tree body = finish_omp_structured_block (sb);
36763 if (ret == NULL)
36764 return ret;
36765 ret = make_node (OMP_TASKLOOP);
36766 TREE_TYPE (ret) = void_type_node;
36767 OMP_FOR_BODY (ret) = body;
36768 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
36769 SET_EXPR_LOCATION (ret, loc);
36770 add_stmt (ret);
36771 return ret;
36774 if (!flag_openmp) /* flag_openmp_simd */
36776 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36777 return NULL_TREE;
36780 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36781 cclauses == NULL);
36782 if (cclauses)
36784 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
36785 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
36788 sb = begin_omp_structured_block ();
36789 save = cp_parser_begin_omp_structured_block (parser);
36791 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
36792 if_p);
36794 cp_parser_end_omp_structured_block (parser, save);
36795 add_stmt (finish_omp_structured_block (sb));
36797 return ret;
36801 /* OpenACC 2.0:
36802 # pragma acc routine oacc-routine-clause[optseq] new-line
36803 function-definition
36805 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
36808 #define OACC_ROUTINE_CLAUSE_MASK \
36809 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
36815 /* Parse the OpenACC routine pragma. This has an optional '( name )'
36816 component, which must resolve to a declared namespace-scope
36817 function. The clauses are either processed directly (for a named
36818 function), or defered until the immediatley following declaration
36819 is parsed. */
36821 static void
36822 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
36823 enum pragma_context context)
36825 gcc_checking_assert (context == pragma_external);
36826 /* The checking for "another pragma following this one" in the "no optional
36827 '( name )'" case makes sure that we dont re-enter. */
36828 gcc_checking_assert (parser->oacc_routine == NULL);
36830 cp_oacc_routine_data data;
36831 data.error_seen = false;
36832 data.fndecl_seen = false;
36833 data.tokens = vNULL;
36834 data.clauses = NULL_TREE;
36835 data.loc = pragma_tok->location;
36836 /* It is safe to take the address of a local variable; it will only be
36837 used while this scope is live. */
36838 parser->oacc_routine = &data;
36840 /* Look for optional '( name )'. */
36841 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36843 cp_lexer_consume_token (parser->lexer); /* '(' */
36845 /* We parse the name as an id-expression. If it resolves to
36846 anything other than a non-overloaded function at namespace
36847 scope, it's an error. */
36848 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
36849 tree name = cp_parser_id_expression (parser,
36850 /*template_keyword_p=*/false,
36851 /*check_dependency_p=*/false,
36852 /*template_p=*/NULL,
36853 /*declarator_p=*/false,
36854 /*optional_p=*/false);
36855 tree decl = cp_parser_lookup_name_simple (parser, name, name_loc);
36856 if (name != error_mark_node && decl == error_mark_node)
36857 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
36859 if (decl == error_mark_node
36860 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36862 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36863 parser->oacc_routine = NULL;
36864 return;
36867 data.clauses
36868 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
36869 "#pragma acc routine",
36870 cp_lexer_peek_token (parser->lexer));
36872 if (decl && is_overloaded_fn (decl)
36873 && (TREE_CODE (decl) != FUNCTION_DECL
36874 || DECL_FUNCTION_TEMPLATE_P (decl)))
36876 error_at (name_loc,
36877 "%<#pragma acc routine%> names a set of overloads");
36878 parser->oacc_routine = NULL;
36879 return;
36882 /* Perhaps we should use the same rule as declarations in different
36883 namespaces? */
36884 if (!DECL_NAMESPACE_SCOPE_P (decl))
36886 error_at (name_loc,
36887 "%qD does not refer to a namespace scope function", decl);
36888 parser->oacc_routine = NULL;
36889 return;
36892 if (TREE_CODE (decl) != FUNCTION_DECL)
36894 error_at (name_loc, "%qD does not refer to a function", decl);
36895 parser->oacc_routine = NULL;
36896 return;
36899 cp_finalize_oacc_routine (parser, decl, false);
36900 parser->oacc_routine = NULL;
36902 else /* No optional '( name )'. */
36904 /* Store away all pragma tokens. */
36905 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36906 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36907 cp_lexer_consume_token (parser->lexer);
36908 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36909 parser->oacc_routine->error_seen = true;
36910 cp_parser_require_pragma_eol (parser, pragma_tok);
36911 struct cp_token_cache *cp
36912 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
36913 parser->oacc_routine->tokens.safe_push (cp);
36915 /* Emit a helpful diagnostic if there's another pragma following this
36916 one. */
36917 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
36919 cp_ensure_no_oacc_routine (parser);
36920 data.tokens.release ();
36921 /* ..., and then just keep going. */
36922 return;
36925 /* We only have to consider the pragma_external case here. */
36926 cp_parser_declaration (parser);
36927 if (parser->oacc_routine
36928 && !parser->oacc_routine->fndecl_seen)
36929 cp_ensure_no_oacc_routine (parser);
36930 else
36931 parser->oacc_routine = NULL;
36932 data.tokens.release ();
36936 /* Finalize #pragma acc routine clauses after direct declarator has
36937 been parsed. */
36939 static tree
36940 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
36942 struct cp_token_cache *ce;
36943 cp_oacc_routine_data *data = parser->oacc_routine;
36945 if (!data->error_seen && data->fndecl_seen)
36947 error_at (data->loc,
36948 "%<#pragma acc routine%> not immediately followed by "
36949 "a single function declaration or definition");
36950 data->error_seen = true;
36952 if (data->error_seen)
36953 return attrs;
36955 gcc_checking_assert (data->tokens.length () == 1);
36956 ce = data->tokens[0];
36958 cp_parser_push_lexer_for_tokens (parser, ce);
36959 parser->lexer->in_pragma = true;
36960 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
36962 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
36963 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
36964 parser->oacc_routine->clauses
36965 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
36966 "#pragma acc routine", pragma_tok);
36967 cp_parser_pop_lexer (parser);
36968 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
36969 fndecl_seen. */
36971 return attrs;
36974 /* Apply any saved OpenACC routine clauses to a just-parsed
36975 declaration. */
36977 static void
36978 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
36980 if (__builtin_expect (parser->oacc_routine != NULL, 0))
36982 /* Keep going if we're in error reporting mode. */
36983 if (parser->oacc_routine->error_seen
36984 || fndecl == error_mark_node)
36985 return;
36987 if (parser->oacc_routine->fndecl_seen)
36989 error_at (parser->oacc_routine->loc,
36990 "%<#pragma acc routine%> not immediately followed by"
36991 " a single function declaration or definition");
36992 parser->oacc_routine = NULL;
36993 return;
36995 if (TREE_CODE (fndecl) != FUNCTION_DECL)
36997 cp_ensure_no_oacc_routine (parser);
36998 return;
37001 if (get_oacc_fn_attrib (fndecl))
37003 error_at (parser->oacc_routine->loc,
37004 "%<#pragma acc routine%> already applied to %qD", fndecl);
37005 parser->oacc_routine = NULL;
37006 return;
37009 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
37011 error_at (parser->oacc_routine->loc,
37012 "%<#pragma acc routine%> must be applied before %s",
37013 TREE_USED (fndecl) ? "use" : "definition");
37014 parser->oacc_routine = NULL;
37015 return;
37018 /* Process the routine's dimension clauses. */
37019 tree dims = build_oacc_routine_dims (parser->oacc_routine->clauses);
37020 replace_oacc_fn_attrib (fndecl, dims);
37022 /* Add an "omp declare target" attribute. */
37023 DECL_ATTRIBUTES (fndecl)
37024 = tree_cons (get_identifier ("omp declare target"),
37025 NULL_TREE, DECL_ATTRIBUTES (fndecl));
37027 /* Don't unset parser->oacc_routine here: we may still need it to
37028 diagnose wrong usage. But, remember that we've used this "#pragma acc
37029 routine". */
37030 parser->oacc_routine->fndecl_seen = true;
37034 /* Main entry point to OpenMP statement pragmas. */
37036 static void
37037 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37039 tree stmt;
37040 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
37041 omp_clause_mask mask (0);
37043 switch (cp_parser_pragma_kind (pragma_tok))
37045 case PRAGMA_OACC_ATOMIC:
37046 cp_parser_omp_atomic (parser, pragma_tok);
37047 return;
37048 case PRAGMA_OACC_CACHE:
37049 stmt = cp_parser_oacc_cache (parser, pragma_tok);
37050 break;
37051 case PRAGMA_OACC_DATA:
37052 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
37053 break;
37054 case PRAGMA_OACC_ENTER_DATA:
37055 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
37056 break;
37057 case PRAGMA_OACC_EXIT_DATA:
37058 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
37059 break;
37060 case PRAGMA_OACC_HOST_DATA:
37061 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
37062 break;
37063 case PRAGMA_OACC_KERNELS:
37064 case PRAGMA_OACC_PARALLEL:
37065 strcpy (p_name, "#pragma acc");
37066 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
37067 if_p);
37068 break;
37069 case PRAGMA_OACC_LOOP:
37070 strcpy (p_name, "#pragma acc");
37071 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
37072 if_p);
37073 break;
37074 case PRAGMA_OACC_UPDATE:
37075 stmt = cp_parser_oacc_update (parser, pragma_tok);
37076 break;
37077 case PRAGMA_OACC_WAIT:
37078 stmt = cp_parser_oacc_wait (parser, pragma_tok);
37079 break;
37080 case PRAGMA_OMP_ATOMIC:
37081 cp_parser_omp_atomic (parser, pragma_tok);
37082 return;
37083 case PRAGMA_OMP_CRITICAL:
37084 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
37085 break;
37086 case PRAGMA_OMP_DISTRIBUTE:
37087 strcpy (p_name, "#pragma omp");
37088 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
37089 if_p);
37090 break;
37091 case PRAGMA_OMP_FOR:
37092 strcpy (p_name, "#pragma omp");
37093 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
37094 if_p);
37095 break;
37096 case PRAGMA_OMP_MASTER:
37097 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
37098 break;
37099 case PRAGMA_OMP_PARALLEL:
37100 strcpy (p_name, "#pragma omp");
37101 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
37102 if_p);
37103 break;
37104 case PRAGMA_OMP_SECTIONS:
37105 strcpy (p_name, "#pragma omp");
37106 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
37107 break;
37108 case PRAGMA_OMP_SIMD:
37109 strcpy (p_name, "#pragma omp");
37110 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
37111 if_p);
37112 break;
37113 case PRAGMA_OMP_SINGLE:
37114 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
37115 break;
37116 case PRAGMA_OMP_TASK:
37117 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
37118 break;
37119 case PRAGMA_OMP_TASKGROUP:
37120 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
37121 break;
37122 case PRAGMA_OMP_TASKLOOP:
37123 strcpy (p_name, "#pragma omp");
37124 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
37125 if_p);
37126 break;
37127 case PRAGMA_OMP_TEAMS:
37128 strcpy (p_name, "#pragma omp");
37129 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
37130 if_p);
37131 break;
37132 default:
37133 gcc_unreachable ();
37136 protected_set_expr_location (stmt, pragma_tok->location);
37139 /* Transactional Memory parsing routines. */
37141 /* Parse a transaction attribute.
37143 txn-attribute:
37144 attribute
37145 [ [ identifier ] ]
37147 We use this instead of cp_parser_attributes_opt for transactions to avoid
37148 the pedwarn in C++98 mode. */
37150 static tree
37151 cp_parser_txn_attribute_opt (cp_parser *parser)
37153 cp_token *token;
37154 tree attr_name, attr = NULL;
37156 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
37157 return cp_parser_attributes_opt (parser);
37159 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
37160 return NULL_TREE;
37161 cp_lexer_consume_token (parser->lexer);
37162 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
37163 goto error1;
37165 token = cp_lexer_peek_token (parser->lexer);
37166 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
37168 token = cp_lexer_consume_token (parser->lexer);
37170 attr_name = (token->type == CPP_KEYWORD
37171 /* For keywords, use the canonical spelling,
37172 not the parsed identifier. */
37173 ? ridpointers[(int) token->keyword]
37174 : token->u.value);
37175 attr = build_tree_list (attr_name, NULL_TREE);
37177 else
37178 cp_parser_error (parser, "expected identifier");
37180 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
37181 error1:
37182 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
37183 return attr;
37186 /* Parse a __transaction_atomic or __transaction_relaxed statement.
37188 transaction-statement:
37189 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
37190 compound-statement
37191 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
37194 static tree
37195 cp_parser_transaction (cp_parser *parser, cp_token *token)
37197 unsigned char old_in = parser->in_transaction;
37198 unsigned char this_in = 1, new_in;
37199 enum rid keyword = token->keyword;
37200 tree stmt, attrs, noex;
37202 cp_lexer_consume_token (parser->lexer);
37204 if (keyword == RID_TRANSACTION_RELAXED
37205 || keyword == RID_SYNCHRONIZED)
37206 this_in |= TM_STMT_ATTR_RELAXED;
37207 else
37209 attrs = cp_parser_txn_attribute_opt (parser);
37210 if (attrs)
37211 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37214 /* Parse a noexcept specification. */
37215 if (keyword == RID_ATOMIC_NOEXCEPT)
37216 noex = boolean_true_node;
37217 else if (keyword == RID_ATOMIC_CANCEL)
37219 /* cancel-and-throw is unimplemented. */
37220 sorry ("atomic_cancel");
37221 noex = NULL_TREE;
37223 else
37224 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
37226 /* Keep track if we're in the lexical scope of an outer transaction. */
37227 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
37229 stmt = begin_transaction_stmt (token->location, NULL, this_in);
37231 parser->in_transaction = new_in;
37232 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
37233 parser->in_transaction = old_in;
37235 finish_transaction_stmt (stmt, NULL, this_in, noex);
37237 return stmt;
37240 /* Parse a __transaction_atomic or __transaction_relaxed expression.
37242 transaction-expression:
37243 __transaction_atomic txn-noexcept-spec[opt] ( expression )
37244 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
37247 static tree
37248 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
37250 unsigned char old_in = parser->in_transaction;
37251 unsigned char this_in = 1;
37252 cp_token *token;
37253 tree expr, noex;
37254 bool noex_expr;
37255 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37257 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
37258 || keyword == RID_TRANSACTION_RELAXED);
37260 if (!flag_tm)
37261 error_at (loc,
37262 keyword == RID_TRANSACTION_RELAXED
37263 ? G_("%<__transaction_relaxed%> without transactional memory "
37264 "support enabled")
37265 : G_("%<__transaction_atomic%> without transactional memory "
37266 "support enabled"));
37268 token = cp_parser_require_keyword (parser, keyword,
37269 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37270 : RT_TRANSACTION_RELAXED));
37271 gcc_assert (token != NULL);
37273 if (keyword == RID_TRANSACTION_RELAXED)
37274 this_in |= TM_STMT_ATTR_RELAXED;
37276 /* Set this early. This might mean that we allow transaction_cancel in
37277 an expression that we find out later actually has to be a constexpr.
37278 However, we expect that cxx_constant_value will be able to deal with
37279 this; also, if the noexcept has no constexpr, then what we parse next
37280 really is a transaction's body. */
37281 parser->in_transaction = this_in;
37283 /* Parse a noexcept specification. */
37284 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
37285 true);
37287 if (!noex || !noex_expr
37288 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37290 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
37292 expr = cp_parser_expression (parser);
37293 expr = finish_parenthesized_expr (expr);
37295 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
37297 else
37299 /* The only expression that is available got parsed for the noexcept
37300 already. noexcept is true then. */
37301 expr = noex;
37302 noex = boolean_true_node;
37305 expr = build_transaction_expr (token->location, expr, this_in, noex);
37306 parser->in_transaction = old_in;
37308 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
37309 return error_mark_node;
37311 return (flag_tm ? expr : error_mark_node);
37314 /* Parse a function-transaction-block.
37316 function-transaction-block:
37317 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
37318 function-body
37319 __transaction_atomic txn-attribute[opt] function-try-block
37320 __transaction_relaxed ctor-initializer[opt] function-body
37321 __transaction_relaxed function-try-block
37324 static bool
37325 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
37327 unsigned char old_in = parser->in_transaction;
37328 unsigned char new_in = 1;
37329 tree compound_stmt, stmt, attrs;
37330 bool ctor_initializer_p;
37331 cp_token *token;
37333 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
37334 || keyword == RID_TRANSACTION_RELAXED);
37335 token = cp_parser_require_keyword (parser, keyword,
37336 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37337 : RT_TRANSACTION_RELAXED));
37338 gcc_assert (token != NULL);
37340 if (keyword == RID_TRANSACTION_RELAXED)
37341 new_in |= TM_STMT_ATTR_RELAXED;
37342 else
37344 attrs = cp_parser_txn_attribute_opt (parser);
37345 if (attrs)
37346 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37349 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
37351 parser->in_transaction = new_in;
37353 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
37354 ctor_initializer_p = cp_parser_function_try_block (parser);
37355 else
37356 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
37357 (parser, /*in_function_try_block=*/false);
37359 parser->in_transaction = old_in;
37361 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
37363 return ctor_initializer_p;
37366 /* Parse a __transaction_cancel statement.
37368 cancel-statement:
37369 __transaction_cancel txn-attribute[opt] ;
37370 __transaction_cancel txn-attribute[opt] throw-expression ;
37372 ??? Cancel and throw is not yet implemented. */
37374 static tree
37375 cp_parser_transaction_cancel (cp_parser *parser)
37377 cp_token *token;
37378 bool is_outer = false;
37379 tree stmt, attrs;
37381 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
37382 RT_TRANSACTION_CANCEL);
37383 gcc_assert (token != NULL);
37385 attrs = cp_parser_txn_attribute_opt (parser);
37386 if (attrs)
37387 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
37389 /* ??? Parse cancel-and-throw here. */
37391 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
37393 if (!flag_tm)
37395 error_at (token->location, "%<__transaction_cancel%> without "
37396 "transactional memory support enabled");
37397 return error_mark_node;
37399 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
37401 error_at (token->location, "%<__transaction_cancel%> within a "
37402 "%<__transaction_relaxed%>");
37403 return error_mark_node;
37405 else if (is_outer)
37407 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
37408 && !is_tm_may_cancel_outer (current_function_decl))
37410 error_at (token->location, "outer %<__transaction_cancel%> not "
37411 "within outer %<__transaction_atomic%>");
37412 error_at (token->location,
37413 " or a %<transaction_may_cancel_outer%> function");
37414 return error_mark_node;
37417 else if (parser->in_transaction == 0)
37419 error_at (token->location, "%<__transaction_cancel%> not within "
37420 "%<__transaction_atomic%>");
37421 return error_mark_node;
37424 stmt = build_tm_abort_call (token->location, is_outer);
37425 add_stmt (stmt);
37427 return stmt;
37430 /* The parser. */
37432 static GTY (()) cp_parser *the_parser;
37435 /* Special handling for the first token or line in the file. The first
37436 thing in the file might be #pragma GCC pch_preprocess, which loads a
37437 PCH file, which is a GC collection point. So we need to handle this
37438 first pragma without benefit of an existing lexer structure.
37440 Always returns one token to the caller in *FIRST_TOKEN. This is
37441 either the true first token of the file, or the first token after
37442 the initial pragma. */
37444 static void
37445 cp_parser_initial_pragma (cp_token *first_token)
37447 tree name = NULL;
37449 cp_lexer_get_preprocessor_token (NULL, first_token);
37450 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
37451 return;
37453 cp_lexer_get_preprocessor_token (NULL, first_token);
37454 if (first_token->type == CPP_STRING)
37456 name = first_token->u.value;
37458 cp_lexer_get_preprocessor_token (NULL, first_token);
37459 if (first_token->type != CPP_PRAGMA_EOL)
37460 error_at (first_token->location,
37461 "junk at end of %<#pragma GCC pch_preprocess%>");
37463 else
37464 error_at (first_token->location, "expected string literal");
37466 /* Skip to the end of the pragma. */
37467 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
37468 cp_lexer_get_preprocessor_token (NULL, first_token);
37470 /* Now actually load the PCH file. */
37471 if (name)
37472 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
37474 /* Read one more token to return to our caller. We have to do this
37475 after reading the PCH file in, since its pointers have to be
37476 live. */
37477 cp_lexer_get_preprocessor_token (NULL, first_token);
37480 /* Parses the grainsize pragma for the _Cilk_for statement.
37481 Syntax:
37482 #pragma cilk grainsize = <VALUE>. */
37484 static void
37485 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37487 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
37489 tree exp = cp_parser_binary_expression (parser, false, false,
37490 PREC_NOT_OPERATOR, NULL);
37491 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37492 if (!exp || exp == error_mark_node)
37494 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
37495 return;
37498 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
37499 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
37500 cp_parser_cilk_for (parser, exp, if_p);
37501 else
37502 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
37503 "%<#pragma cilk grainsize%> is not followed by "
37504 "%<_Cilk_for%>");
37505 return;
37507 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37510 /* Normal parsing of a pragma token. Here we can (and must) use the
37511 regular lexer. */
37513 static bool
37514 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
37516 cp_token *pragma_tok;
37517 unsigned int id;
37518 tree stmt;
37519 bool ret;
37521 pragma_tok = cp_lexer_consume_token (parser->lexer);
37522 gcc_assert (pragma_tok->type == CPP_PRAGMA);
37523 parser->lexer->in_pragma = true;
37525 id = cp_parser_pragma_kind (pragma_tok);
37526 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
37527 cp_ensure_no_omp_declare_simd (parser);
37528 switch (id)
37530 case PRAGMA_GCC_PCH_PREPROCESS:
37531 error_at (pragma_tok->location,
37532 "%<#pragma GCC pch_preprocess%> must be first");
37533 break;
37535 case PRAGMA_OMP_BARRIER:
37536 switch (context)
37538 case pragma_compound:
37539 cp_parser_omp_barrier (parser, pragma_tok);
37540 return false;
37541 case pragma_stmt:
37542 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
37543 "used in compound statements");
37544 break;
37545 default:
37546 goto bad_stmt;
37548 break;
37550 case PRAGMA_OMP_FLUSH:
37551 switch (context)
37553 case pragma_compound:
37554 cp_parser_omp_flush (parser, pragma_tok);
37555 return false;
37556 case pragma_stmt:
37557 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
37558 "used in compound statements");
37559 break;
37560 default:
37561 goto bad_stmt;
37563 break;
37565 case PRAGMA_OMP_TASKWAIT:
37566 switch (context)
37568 case pragma_compound:
37569 cp_parser_omp_taskwait (parser, pragma_tok);
37570 return false;
37571 case pragma_stmt:
37572 error_at (pragma_tok->location,
37573 "%<#pragma omp taskwait%> may only be "
37574 "used in compound statements");
37575 break;
37576 default:
37577 goto bad_stmt;
37579 break;
37581 case PRAGMA_OMP_TASKYIELD:
37582 switch (context)
37584 case pragma_compound:
37585 cp_parser_omp_taskyield (parser, pragma_tok);
37586 return false;
37587 case pragma_stmt:
37588 error_at (pragma_tok->location,
37589 "%<#pragma omp taskyield%> may only be "
37590 "used in compound statements");
37591 break;
37592 default:
37593 goto bad_stmt;
37595 break;
37597 case PRAGMA_OMP_CANCEL:
37598 switch (context)
37600 case pragma_compound:
37601 cp_parser_omp_cancel (parser, pragma_tok);
37602 return false;
37603 case pragma_stmt:
37604 error_at (pragma_tok->location,
37605 "%<#pragma omp cancel%> may only be "
37606 "used in compound statements");
37607 break;
37608 default:
37609 goto bad_stmt;
37611 break;
37613 case PRAGMA_OMP_CANCELLATION_POINT:
37614 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
37615 return false;
37617 case PRAGMA_OMP_THREADPRIVATE:
37618 cp_parser_omp_threadprivate (parser, pragma_tok);
37619 return false;
37621 case PRAGMA_OMP_DECLARE:
37622 cp_parser_omp_declare (parser, pragma_tok, context);
37623 return false;
37625 case PRAGMA_OACC_DECLARE:
37626 cp_parser_oacc_declare (parser, pragma_tok);
37627 return false;
37629 case PRAGMA_OACC_ROUTINE:
37630 if (context != pragma_external)
37632 error_at (pragma_tok->location,
37633 "%<#pragma acc routine%> must be at file scope");
37634 break;
37636 cp_parser_oacc_routine (parser, pragma_tok, context);
37637 return false;
37639 case PRAGMA_OACC_ATOMIC:
37640 case PRAGMA_OACC_CACHE:
37641 case PRAGMA_OACC_DATA:
37642 case PRAGMA_OACC_ENTER_DATA:
37643 case PRAGMA_OACC_EXIT_DATA:
37644 case PRAGMA_OACC_HOST_DATA:
37645 case PRAGMA_OACC_KERNELS:
37646 case PRAGMA_OACC_PARALLEL:
37647 case PRAGMA_OACC_LOOP:
37648 case PRAGMA_OACC_UPDATE:
37649 case PRAGMA_OACC_WAIT:
37650 case PRAGMA_OMP_ATOMIC:
37651 case PRAGMA_OMP_CRITICAL:
37652 case PRAGMA_OMP_DISTRIBUTE:
37653 case PRAGMA_OMP_FOR:
37654 case PRAGMA_OMP_MASTER:
37655 case PRAGMA_OMP_PARALLEL:
37656 case PRAGMA_OMP_SECTIONS:
37657 case PRAGMA_OMP_SIMD:
37658 case PRAGMA_OMP_SINGLE:
37659 case PRAGMA_OMP_TASK:
37660 case PRAGMA_OMP_TASKGROUP:
37661 case PRAGMA_OMP_TASKLOOP:
37662 case PRAGMA_OMP_TEAMS:
37663 if (context != pragma_stmt && context != pragma_compound)
37664 goto bad_stmt;
37665 stmt = push_omp_privatization_clauses (false);
37666 cp_parser_omp_construct (parser, pragma_tok, if_p);
37667 pop_omp_privatization_clauses (stmt);
37668 return true;
37670 case PRAGMA_OMP_ORDERED:
37671 stmt = push_omp_privatization_clauses (false);
37672 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
37673 pop_omp_privatization_clauses (stmt);
37674 return ret;
37676 case PRAGMA_OMP_TARGET:
37677 stmt = push_omp_privatization_clauses (false);
37678 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
37679 pop_omp_privatization_clauses (stmt);
37680 return ret;
37682 case PRAGMA_OMP_END_DECLARE_TARGET:
37683 cp_parser_omp_end_declare_target (parser, pragma_tok);
37684 return false;
37686 case PRAGMA_OMP_SECTION:
37687 error_at (pragma_tok->location,
37688 "%<#pragma omp section%> may only be used in "
37689 "%<#pragma omp sections%> construct");
37690 break;
37692 case PRAGMA_IVDEP:
37694 if (context == pragma_external)
37696 error_at (pragma_tok->location,
37697 "%<#pragma GCC ivdep%> must be inside a function");
37698 break;
37700 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37701 cp_token *tok;
37702 tok = cp_lexer_peek_token (the_parser->lexer);
37703 if (tok->type != CPP_KEYWORD
37704 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
37705 && tok->keyword != RID_DO))
37707 cp_parser_error (parser, "for, while or do statement expected");
37708 return false;
37710 cp_parser_iteration_statement (parser, if_p, true);
37711 return true;
37714 case PRAGMA_CILK_SIMD:
37715 if (context == pragma_external)
37717 error_at (pragma_tok->location,
37718 "%<#pragma simd%> must be inside a function");
37719 break;
37721 stmt = push_omp_privatization_clauses (false);
37722 cp_parser_cilk_simd (parser, pragma_tok, if_p);
37723 pop_omp_privatization_clauses (stmt);
37724 return true;
37726 case PRAGMA_CILK_GRAINSIZE:
37727 if (context == pragma_external)
37729 error_at (pragma_tok->location,
37730 "%<#pragma cilk grainsize%> must be inside a function");
37731 break;
37734 /* Ignore the pragma if Cilk Plus is not enabled. */
37735 if (flag_cilkplus)
37737 cp_parser_cilk_grainsize (parser, pragma_tok, if_p);
37738 return true;
37740 else
37742 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
37743 "%<#pragma cilk grainsize%>");
37744 break;
37747 default:
37748 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
37749 c_invoke_pragma_handler (id);
37750 break;
37752 bad_stmt:
37753 cp_parser_error (parser, "expected declaration specifiers");
37754 break;
37757 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37758 return false;
37761 /* The interface the pragma parsers have to the lexer. */
37763 enum cpp_ttype
37764 pragma_lex (tree *value, location_t *loc)
37766 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
37767 enum cpp_ttype ret = tok->type;
37769 *value = tok->u.value;
37770 if (loc)
37771 *loc = tok->location;
37773 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
37774 ret = CPP_EOF;
37775 else if (ret == CPP_STRING)
37776 *value = cp_parser_string_literal (the_parser, false, false);
37777 else
37779 if (ret == CPP_KEYWORD)
37780 ret = CPP_NAME;
37781 cp_lexer_consume_token (the_parser->lexer);
37784 return ret;
37788 /* External interface. */
37790 /* Parse one entire translation unit. */
37792 void
37793 c_parse_file (void)
37795 static bool already_called = false;
37797 if (already_called)
37798 fatal_error (input_location,
37799 "inter-module optimizations not implemented for C++");
37800 already_called = true;
37802 the_parser = cp_parser_new ();
37803 push_deferring_access_checks (flag_access_control
37804 ? dk_no_deferred : dk_no_check);
37805 cp_parser_translation_unit (the_parser);
37806 the_parser = NULL;
37809 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
37810 vectorlength clause:
37811 Syntax:
37812 vectorlength ( constant-expression ) */
37814 static tree
37815 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
37816 bool is_simd_fn)
37818 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37819 tree expr;
37820 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
37821 safelen clause. Thus, vectorlength is represented as OMP 4.0
37822 safelen. For SIMD-enabled function it is represented by OMP 4.0
37823 simdlen. */
37824 if (!is_simd_fn)
37825 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
37826 loc);
37827 else
37828 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
37829 loc);
37831 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37832 return error_mark_node;
37834 expr = cp_parser_constant_expression (parser);
37835 expr = maybe_constant_value (expr);
37837 /* If expr == error_mark_node, then don't emit any errors nor
37838 create a clause. if any of the above functions returns
37839 error mark node then they would have emitted an error message. */
37840 if (expr == error_mark_node)
37842 else if (!TREE_TYPE (expr)
37843 || !TREE_CONSTANT (expr)
37844 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
37845 error_at (loc, "vectorlength must be an integer constant");
37846 else if (TREE_CONSTANT (expr)
37847 && !pow2p_hwi (TREE_INT_CST_LOW (expr)))
37848 error_at (loc, "vectorlength must be a power of 2");
37849 else
37851 tree c;
37852 if (!is_simd_fn)
37854 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
37855 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
37856 OMP_CLAUSE_CHAIN (c) = clauses;
37857 clauses = c;
37859 else
37861 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
37862 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
37863 OMP_CLAUSE_CHAIN (c) = clauses;
37864 clauses = c;
37868 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37869 return error_mark_node;
37870 return clauses;
37873 /* Handles the Cilk Plus #pragma simd linear clause.
37874 Syntax:
37875 linear ( simd-linear-variable-list )
37877 simd-linear-variable-list:
37878 simd-linear-variable
37879 simd-linear-variable-list , simd-linear-variable
37881 simd-linear-variable:
37882 id-expression
37883 id-expression : simd-linear-step
37885 simd-linear-step:
37886 conditional-expression */
37888 static tree
37889 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
37891 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37893 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37894 return clauses;
37895 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37897 cp_parser_error (parser, "expected identifier");
37898 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
37899 return error_mark_node;
37902 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37903 parser->colon_corrects_to_scope_p = false;
37904 while (1)
37906 cp_token *token = cp_lexer_peek_token (parser->lexer);
37907 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37909 cp_parser_error (parser, "expected variable-name");
37910 clauses = error_mark_node;
37911 break;
37914 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
37915 false, false);
37916 tree decl = cp_parser_lookup_name_simple (parser, var_name,
37917 token->location);
37918 if (decl == error_mark_node)
37920 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
37921 token->location);
37922 clauses = error_mark_node;
37924 else
37926 tree e = NULL_TREE;
37927 tree step_size = integer_one_node;
37929 /* If present, parse the linear step. Otherwise, assume the default
37930 value of 1. */
37931 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
37933 cp_lexer_consume_token (parser->lexer);
37935 e = cp_parser_assignment_expression (parser);
37936 e = maybe_constant_value (e);
37938 if (e == error_mark_node)
37940 /* If an error has occurred, then the whole pragma is
37941 considered ill-formed. Thus, no reason to keep
37942 parsing. */
37943 clauses = error_mark_node;
37944 break;
37946 else if (type_dependent_expression_p (e)
37947 || value_dependent_expression_p (e)
37948 || (TREE_TYPE (e)
37949 && INTEGRAL_TYPE_P (TREE_TYPE (e))
37950 && (TREE_CONSTANT (e)
37951 || DECL_P (e))))
37952 step_size = e;
37953 else
37954 cp_parser_error (parser,
37955 "step size must be an integer constant "
37956 "expression or an integer variable");
37959 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
37960 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
37961 OMP_CLAUSE_DECL (l) = decl;
37962 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
37963 OMP_CLAUSE_CHAIN (l) = clauses;
37964 clauses = l;
37966 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37967 cp_lexer_consume_token (parser->lexer);
37968 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37969 break;
37970 else
37972 error_at (cp_lexer_peek_token (parser->lexer)->location,
37973 "expected %<,%> or %<)%> after %qE", decl);
37974 clauses = error_mark_node;
37975 break;
37978 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37979 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
37980 return clauses;
37983 /* Returns the name of the next clause. If the clause is not
37984 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
37985 token is not consumed. Otherwise, the appropriate enum from the
37986 pragma_simd_clause is returned and the token is consumed. */
37988 static pragma_omp_clause
37989 cp_parser_cilk_simd_clause_name (cp_parser *parser)
37991 pragma_omp_clause clause_type;
37992 cp_token *token = cp_lexer_peek_token (parser->lexer);
37994 if (token->keyword == RID_PRIVATE)
37995 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
37996 else if (!token->u.value || token->type != CPP_NAME)
37997 return PRAGMA_CILK_CLAUSE_NONE;
37998 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
37999 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
38000 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
38001 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
38002 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
38003 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
38004 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
38005 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
38006 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
38007 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
38008 else
38009 return PRAGMA_CILK_CLAUSE_NONE;
38011 cp_lexer_consume_token (parser->lexer);
38012 return clause_type;
38015 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
38017 static tree
38018 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
38020 tree clauses = NULL_TREE;
38022 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38023 && clauses != error_mark_node)
38025 pragma_omp_clause c_kind;
38026 c_kind = cp_parser_cilk_simd_clause_name (parser);
38027 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
38028 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
38029 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
38030 clauses = cp_parser_cilk_simd_linear (parser, clauses);
38031 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
38032 /* Use the OpenMP 4.0 equivalent function. */
38033 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
38034 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
38035 /* Use the OpenMP 4.0 equivalent function. */
38036 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
38037 clauses);
38038 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
38039 /* Use the OMP 4.0 equivalent function. */
38040 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
38041 clauses);
38042 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
38043 /* Use the OMP 4.0 equivalent function. */
38044 clauses = cp_parser_omp_clause_reduction (parser, clauses);
38045 else
38047 clauses = error_mark_node;
38048 cp_parser_error (parser, "expected %<#pragma simd%> clause");
38049 break;
38053 cp_parser_skip_to_pragma_eol (parser, pragma_token);
38055 if (clauses == error_mark_node)
38056 return error_mark_node;
38057 else
38058 return finish_omp_clauses (clauses, C_ORT_CILK);
38061 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
38063 static void
38064 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token, bool *if_p)
38066 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
38068 if (clauses == error_mark_node)
38069 return;
38071 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
38073 error_at (cp_lexer_peek_token (parser->lexer)->location,
38074 "for statement expected");
38075 return;
38078 tree sb = begin_omp_structured_block ();
38079 int save = cp_parser_begin_omp_structured_block (parser);
38080 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL, if_p);
38081 if (ret)
38082 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
38083 cp_parser_end_omp_structured_block (parser, save);
38084 add_stmt (finish_omp_structured_block (sb));
38087 /* Main entry-point for parsing Cilk Plus _Cilk_for
38088 loops. The return value is error_mark_node
38089 when errors happen and CILK_FOR tree on success. */
38091 static tree
38092 cp_parser_cilk_for (cp_parser *parser, tree grain, bool *if_p)
38094 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
38095 gcc_unreachable ();
38097 tree sb = begin_omp_structured_block ();
38098 int save = cp_parser_begin_omp_structured_block (parser);
38100 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
38101 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
38102 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
38103 clauses = finish_omp_clauses (clauses, C_ORT_CILK);
38105 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL, if_p);
38106 if (ret)
38107 cpp_validate_cilk_plus_loop (ret);
38108 else
38109 ret = error_mark_node;
38111 cp_parser_end_omp_structured_block (parser, save);
38112 add_stmt (finish_omp_structured_block (sb));
38113 return ret;
38116 /* Create an identifier for a generic parameter type (a synthesized
38117 template parameter implied by `auto' or a concept identifier). */
38119 static GTY(()) int generic_parm_count;
38120 static tree
38121 make_generic_type_name ()
38123 char buf[32];
38124 sprintf (buf, "auto:%d", ++generic_parm_count);
38125 return get_identifier (buf);
38128 /* Predicate that behaves as is_auto_or_concept but matches the parent
38129 node of the generic type rather than the generic type itself. This
38130 allows for type transformation in add_implicit_template_parms. */
38132 static inline bool
38133 tree_type_is_auto_or_concept (const_tree t)
38135 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
38138 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
38139 (creating a new template parameter list if necessary). Returns the newly
38140 created template type parm. */
38142 static tree
38143 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
38145 gcc_assert (current_binding_level->kind == sk_function_parms);
38147 /* Before committing to modifying any scope, if we're in an
38148 implicit template scope, and we're trying to synthesize a
38149 constrained parameter, try to find a previous parameter with
38150 the same name. This is the same-type rule for abbreviated
38151 function templates.
38153 NOTE: We can generate implicit parameters when tentatively
38154 parsing a nested name specifier, only to reject that parse
38155 later. However, matching the same template-id as part of a
38156 direct-declarator should generate an identical template
38157 parameter, so this rule will merge them. */
38158 if (parser->implicit_template_scope && constr)
38160 tree t = parser->implicit_template_parms;
38161 while (t)
38163 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
38165 tree d = TREE_VALUE (t);
38166 if (TREE_CODE (d) == PARM_DECL)
38167 /* Return the TEMPLATE_PARM_INDEX. */
38168 d = DECL_INITIAL (d);
38169 return d;
38171 t = TREE_CHAIN (t);
38175 /* We are either continuing a function template that already contains implicit
38176 template parameters, creating a new fully-implicit function template, or
38177 extending an existing explicit function template with implicit template
38178 parameters. */
38180 cp_binding_level *const entry_scope = current_binding_level;
38182 bool become_template = false;
38183 cp_binding_level *parent_scope = 0;
38185 if (parser->implicit_template_scope)
38187 gcc_assert (parser->implicit_template_parms);
38189 current_binding_level = parser->implicit_template_scope;
38191 else
38193 /* Roll back to the existing template parameter scope (in the case of
38194 extending an explicit function template) or introduce a new template
38195 parameter scope ahead of the function parameter scope (or class scope
38196 in the case of out-of-line member definitions). The function scope is
38197 added back after template parameter synthesis below. */
38199 cp_binding_level *scope = entry_scope;
38201 while (scope->kind == sk_function_parms)
38203 parent_scope = scope;
38204 scope = scope->level_chain;
38206 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
38208 /* If not defining a class, then any class scope is a scope level in
38209 an out-of-line member definition. In this case simply wind back
38210 beyond the first such scope to inject the template parameter list.
38211 Otherwise wind back to the class being defined. The latter can
38212 occur in class member friend declarations such as:
38214 class A {
38215 void foo (auto);
38217 class B {
38218 friend void A::foo (auto);
38221 The template parameter list synthesized for the friend declaration
38222 must be injected in the scope of 'B'. This can also occur in
38223 erroneous cases such as:
38225 struct A {
38226 struct B {
38227 void foo (auto);
38229 void B::foo (auto) {}
38232 Here the attempted definition of 'B::foo' within 'A' is ill-formed
38233 but, nevertheless, the template parameter list synthesized for the
38234 declarator should be injected into the scope of 'A' as if the
38235 ill-formed template was specified explicitly. */
38237 while (scope->kind == sk_class && !scope->defining_class_p)
38239 parent_scope = scope;
38240 scope = scope->level_chain;
38244 current_binding_level = scope;
38246 if (scope->kind != sk_template_parms
38247 || !function_being_declared_is_template_p (parser))
38249 /* Introduce a new template parameter list for implicit template
38250 parameters. */
38252 become_template = true;
38254 parser->implicit_template_scope
38255 = begin_scope (sk_template_parms, NULL);
38257 ++processing_template_decl;
38259 parser->fully_implicit_function_template_p = true;
38260 ++parser->num_template_parameter_lists;
38262 else
38264 /* Synthesize implicit template parameters at the end of the explicit
38265 template parameter list. */
38267 gcc_assert (current_template_parms);
38269 parser->implicit_template_scope = scope;
38271 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38272 parser->implicit_template_parms
38273 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
38277 /* Synthesize a new template parameter and track the current template
38278 parameter chain with implicit_template_parms. */
38280 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
38281 tree synth_id = make_generic_type_name ();
38282 tree synth_tmpl_parm;
38283 bool non_type = false;
38285 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
38286 synth_tmpl_parm
38287 = finish_template_type_parm (class_type_node, synth_id);
38288 else if (TREE_CODE (proto) == TEMPLATE_DECL)
38289 synth_tmpl_parm
38290 = finish_constrained_template_template_parm (proto, synth_id);
38291 else
38293 synth_tmpl_parm = copy_decl (proto);
38294 DECL_NAME (synth_tmpl_parm) = synth_id;
38295 non_type = true;
38298 // Attach the constraint to the parm before processing.
38299 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
38300 TREE_TYPE (node) = constr;
38301 tree new_parm
38302 = process_template_parm (parser->implicit_template_parms,
38303 input_location,
38304 node,
38305 /*non_type=*/non_type,
38306 /*param_pack=*/false);
38308 // Chain the new parameter to the list of implicit parameters.
38309 if (parser->implicit_template_parms)
38310 parser->implicit_template_parms
38311 = TREE_CHAIN (parser->implicit_template_parms);
38312 else
38313 parser->implicit_template_parms = new_parm;
38315 tree new_decl = getdecls ();
38316 if (non_type)
38317 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
38318 new_decl = DECL_INITIAL (new_decl);
38320 /* If creating a fully implicit function template, start the new implicit
38321 template parameter list with this synthesized type, otherwise grow the
38322 current template parameter list. */
38324 if (become_template)
38326 parent_scope->level_chain = current_binding_level;
38328 tree new_parms = make_tree_vec (1);
38329 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
38330 current_template_parms = tree_cons (size_int (processing_template_decl),
38331 new_parms, current_template_parms);
38333 else
38335 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38336 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
38337 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
38338 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
38341 // If the new parameter was constrained, we need to add that to the
38342 // constraints in the template parameter list.
38343 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
38345 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
38346 reqs = conjoin_constraints (reqs, req);
38347 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
38350 current_binding_level = entry_scope;
38352 return new_decl;
38355 /* Finish the declaration of a fully implicit function template. Such a
38356 template has no explicit template parameter list so has not been through the
38357 normal template head and tail processing. synthesize_implicit_template_parm
38358 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
38359 provided if the declaration is a class member such that its template
38360 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
38361 form is returned. Otherwise NULL_TREE is returned. */
38363 static tree
38364 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
38366 gcc_assert (parser->fully_implicit_function_template_p);
38368 if (member_decl_opt && member_decl_opt != error_mark_node
38369 && DECL_VIRTUAL_P (member_decl_opt))
38371 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
38372 "implicit templates may not be %<virtual%>");
38373 DECL_VIRTUAL_P (member_decl_opt) = false;
38376 if (member_decl_opt)
38377 member_decl_opt = finish_member_template_decl (member_decl_opt);
38378 end_template_decl ();
38380 parser->fully_implicit_function_template_p = false;
38381 --parser->num_template_parameter_lists;
38383 return member_decl_opt;
38386 #include "gt-cp-parser.h"