* function.h (struct function): Remove cilk_frame_decl,
[official-gcc.git] / gcc / cp / parser.c
blobb469d1c176062202855d91d2225fe9540dde180c
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2017 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 #define INCLUDE_UNIQUE_PTR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "c-family/c-common.h"
27 #include "timevar.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "print-tree.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "intl.h"
34 #include "decl.h"
35 #include "c-family/c-objc.h"
36 #include "plugin.h"
37 #include "tree-pretty-print.h"
38 #include "parser.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
43 #include "context.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "c-family/name-hint.h"
49 /* The lexer. */
51 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
52 and c-lex.c) and the C++ parser. */
54 static cp_token eof_token =
56 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
59 /* The various kinds of non integral constant we encounter. */
60 enum non_integral_constant {
61 NIC_NONE,
62 /* floating-point literal */
63 NIC_FLOAT,
64 /* %<this%> */
65 NIC_THIS,
66 /* %<__FUNCTION__%> */
67 NIC_FUNC_NAME,
68 /* %<__PRETTY_FUNCTION__%> */
69 NIC_PRETTY_FUNC,
70 /* %<__func__%> */
71 NIC_C99_FUNC,
72 /* "%<va_arg%> */
73 NIC_VA_ARG,
74 /* a cast */
75 NIC_CAST,
76 /* %<typeid%> operator */
77 NIC_TYPEID,
78 /* non-constant compound literals */
79 NIC_NCC,
80 /* a function call */
81 NIC_FUNC_CALL,
82 /* an increment */
83 NIC_INC,
84 /* an decrement */
85 NIC_DEC,
86 /* an array reference */
87 NIC_ARRAY_REF,
88 /* %<->%> */
89 NIC_ARROW,
90 /* %<.%> */
91 NIC_POINT,
92 /* the address of a label */
93 NIC_ADDR_LABEL,
94 /* %<*%> */
95 NIC_STAR,
96 /* %<&%> */
97 NIC_ADDR,
98 /* %<++%> */
99 NIC_PREINCREMENT,
100 /* %<--%> */
101 NIC_PREDECREMENT,
102 /* %<new%> */
103 NIC_NEW,
104 /* %<delete%> */
105 NIC_DEL,
106 /* calls to overloaded operators */
107 NIC_OVERLOADED,
108 /* an assignment */
109 NIC_ASSIGNMENT,
110 /* a comma operator */
111 NIC_COMMA,
112 /* a call to a constructor */
113 NIC_CONSTRUCTOR,
114 /* a transaction expression */
115 NIC_TRANSACTION
118 /* The various kinds of errors about name-lookup failing. */
119 enum name_lookup_error {
120 /* NULL */
121 NLE_NULL,
122 /* is not a type */
123 NLE_TYPE,
124 /* is not a class or namespace */
125 NLE_CXX98,
126 /* is not a class, namespace, or enumeration */
127 NLE_NOT_CXX98
130 /* The various kinds of required token */
131 enum required_token {
132 RT_NONE,
133 RT_SEMICOLON, /* ';' */
134 RT_OPEN_PAREN, /* '(' */
135 RT_CLOSE_BRACE, /* '}' */
136 RT_OPEN_BRACE, /* '{' */
137 RT_CLOSE_SQUARE, /* ']' */
138 RT_OPEN_SQUARE, /* '[' */
139 RT_COMMA, /* ',' */
140 RT_SCOPE, /* '::' */
141 RT_LESS, /* '<' */
142 RT_GREATER, /* '>' */
143 RT_EQ, /* '=' */
144 RT_ELLIPSIS, /* '...' */
145 RT_MULT, /* '*' */
146 RT_COMPL, /* '~' */
147 RT_COLON, /* ':' */
148 RT_COLON_SCOPE, /* ':' or '::' */
149 RT_CLOSE_PAREN, /* ')' */
150 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
151 RT_PRAGMA_EOL, /* end of line */
152 RT_NAME, /* identifier */
154 /* The type is CPP_KEYWORD */
155 RT_NEW, /* new */
156 RT_DELETE, /* delete */
157 RT_RETURN, /* return */
158 RT_WHILE, /* while */
159 RT_EXTERN, /* extern */
160 RT_STATIC_ASSERT, /* static_assert */
161 RT_DECLTYPE, /* decltype */
162 RT_OPERATOR, /* operator */
163 RT_CLASS, /* class */
164 RT_TEMPLATE, /* template */
165 RT_NAMESPACE, /* namespace */
166 RT_USING, /* using */
167 RT_ASM, /* asm */
168 RT_TRY, /* try */
169 RT_CATCH, /* catch */
170 RT_THROW, /* throw */
171 RT_LABEL, /* __label__ */
172 RT_AT_TRY, /* @try */
173 RT_AT_SYNCHRONIZED, /* @synchronized */
174 RT_AT_THROW, /* @throw */
176 RT_SELECT, /* selection-statement */
177 RT_ITERATION, /* iteration-statement */
178 RT_JUMP, /* jump-statement */
179 RT_CLASS_KEY, /* class-key */
180 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
181 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
182 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
183 RT_TRANSACTION_CANCEL /* __transaction_cancel */
186 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
187 reverting it on destruction. */
189 class type_id_in_expr_sentinel
191 cp_parser *parser;
192 bool saved;
193 public:
194 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
195 : parser (parser),
196 saved (parser->in_type_id_in_expr_p)
197 { parser->in_type_id_in_expr_p = set; }
198 ~type_id_in_expr_sentinel ()
199 { parser->in_type_id_in_expr_p = saved; }
202 /* Prototypes. */
204 static cp_lexer *cp_lexer_new_main
205 (void);
206 static cp_lexer *cp_lexer_new_from_tokens
207 (cp_token_cache *tokens);
208 static void cp_lexer_destroy
209 (cp_lexer *);
210 static int cp_lexer_saving_tokens
211 (const cp_lexer *);
212 static cp_token *cp_lexer_token_at
213 (cp_lexer *, cp_token_position);
214 static void cp_lexer_get_preprocessor_token
215 (cp_lexer *, cp_token *);
216 static inline cp_token *cp_lexer_peek_token
217 (cp_lexer *);
218 static cp_token *cp_lexer_peek_nth_token
219 (cp_lexer *, size_t);
220 static inline bool cp_lexer_next_token_is
221 (cp_lexer *, enum cpp_ttype);
222 static bool cp_lexer_next_token_is_not
223 (cp_lexer *, enum cpp_ttype);
224 static bool cp_lexer_next_token_is_keyword
225 (cp_lexer *, enum rid);
226 static cp_token *cp_lexer_consume_token
227 (cp_lexer *);
228 static void cp_lexer_purge_token
229 (cp_lexer *);
230 static void cp_lexer_purge_tokens_after
231 (cp_lexer *, cp_token_position);
232 static void cp_lexer_save_tokens
233 (cp_lexer *);
234 static void cp_lexer_commit_tokens
235 (cp_lexer *);
236 static void cp_lexer_rollback_tokens
237 (cp_lexer *);
238 static void cp_lexer_print_token
239 (FILE *, cp_token *);
240 static inline bool cp_lexer_debugging_p
241 (cp_lexer *);
242 static void cp_lexer_start_debugging
243 (cp_lexer *) ATTRIBUTE_UNUSED;
244 static void cp_lexer_stop_debugging
245 (cp_lexer *) ATTRIBUTE_UNUSED;
247 static cp_token_cache *cp_token_cache_new
248 (cp_token *, cp_token *);
250 static void cp_parser_initial_pragma
251 (cp_token *);
253 static bool cp_parser_omp_declare_reduction_exprs
254 (tree, cp_parser *);
255 static void cp_finalize_oacc_routine
256 (cp_parser *, tree, bool);
258 /* Manifest constants. */
259 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
260 #define CP_SAVED_TOKEN_STACK 5
262 /* Variables. */
264 /* The stream to which debugging output should be written. */
265 static FILE *cp_lexer_debug_stream;
267 /* Nonzero if we are parsing an unevaluated operand: an operand to
268 sizeof, typeof, or alignof. */
269 int cp_unevaluated_operand;
271 /* Dump up to NUM tokens in BUFFER to FILE starting with token
272 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
273 first token in BUFFER. If NUM is 0, dump all the tokens. If
274 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
275 highlighted by surrounding it in [[ ]]. */
277 static void
278 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
279 cp_token *start_token, unsigned num,
280 cp_token *curr_token)
282 unsigned i, nprinted;
283 cp_token *token;
284 bool do_print;
286 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
288 if (buffer == NULL)
289 return;
291 if (num == 0)
292 num = buffer->length ();
294 if (start_token == NULL)
295 start_token = buffer->address ();
297 if (start_token > buffer->address ())
299 cp_lexer_print_token (file, &(*buffer)[0]);
300 fprintf (file, " ... ");
303 do_print = false;
304 nprinted = 0;
305 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
307 if (token == start_token)
308 do_print = true;
310 if (!do_print)
311 continue;
313 nprinted++;
314 if (token == curr_token)
315 fprintf (file, "[[");
317 cp_lexer_print_token (file, token);
319 if (token == curr_token)
320 fprintf (file, "]]");
322 switch (token->type)
324 case CPP_SEMICOLON:
325 case CPP_OPEN_BRACE:
326 case CPP_CLOSE_BRACE:
327 case CPP_EOF:
328 fputc ('\n', file);
329 break;
331 default:
332 fputc (' ', file);
336 if (i == num && i < buffer->length ())
338 fprintf (file, " ... ");
339 cp_lexer_print_token (file, &buffer->last ());
342 fprintf (file, "\n");
346 /* Dump all tokens in BUFFER to stderr. */
348 void
349 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
351 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
354 DEBUG_FUNCTION void
355 debug (vec<cp_token, va_gc> &ref)
357 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
360 DEBUG_FUNCTION void
361 debug (vec<cp_token, va_gc> *ptr)
363 if (ptr)
364 debug (*ptr);
365 else
366 fprintf (stderr, "<nil>\n");
370 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
371 description for T. */
373 static void
374 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
376 if (t)
378 fprintf (file, "%s: ", desc);
379 print_node_brief (file, "", t, 0);
384 /* Dump parser context C to FILE. */
386 static void
387 cp_debug_print_context (FILE *file, cp_parser_context *c)
389 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
390 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
391 print_node_brief (file, "", c->object_type, 0);
392 fprintf (file, "}\n");
396 /* Print the stack of parsing contexts to FILE starting with FIRST. */
398 static void
399 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
401 unsigned i;
402 cp_parser_context *c;
404 fprintf (file, "Parsing context stack:\n");
405 for (i = 0, c = first; c; c = c->next, i++)
407 fprintf (file, "\t#%u: ", i);
408 cp_debug_print_context (file, c);
413 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
415 static void
416 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
418 if (flag)
419 fprintf (file, "%s: true\n", desc);
423 /* Print an unparsed function entry UF to FILE. */
425 static void
426 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
428 unsigned i;
429 cp_default_arg_entry *default_arg_fn;
430 tree fn;
432 fprintf (file, "\tFunctions with default args:\n");
433 for (i = 0;
434 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
435 i++)
437 fprintf (file, "\t\tClass type: ");
438 print_node_brief (file, "", default_arg_fn->class_type, 0);
439 fprintf (file, "\t\tDeclaration: ");
440 print_node_brief (file, "", default_arg_fn->decl, 0);
441 fprintf (file, "\n");
444 fprintf (file, "\n\tFunctions with definitions that require "
445 "post-processing\n\t\t");
446 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
448 print_node_brief (file, "", fn, 0);
449 fprintf (file, " ");
451 fprintf (file, "\n");
453 fprintf (file, "\n\tNon-static data members with initializers that require "
454 "post-processing\n\t\t");
455 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
457 print_node_brief (file, "", fn, 0);
458 fprintf (file, " ");
460 fprintf (file, "\n");
464 /* Print the stack of unparsed member functions S to FILE. */
466 static void
467 cp_debug_print_unparsed_queues (FILE *file,
468 vec<cp_unparsed_functions_entry, va_gc> *s)
470 unsigned i;
471 cp_unparsed_functions_entry *uf;
473 fprintf (file, "Unparsed functions\n");
474 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
476 fprintf (file, "#%u:\n", i);
477 cp_debug_print_unparsed_function (file, uf);
482 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
483 the given PARSER. If FILE is NULL, the output is printed on stderr. */
485 static void
486 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
488 cp_token *next_token, *first_token, *start_token;
490 if (file == NULL)
491 file = stderr;
493 next_token = parser->lexer->next_token;
494 first_token = parser->lexer->buffer->address ();
495 start_token = (next_token > first_token + window_size / 2)
496 ? next_token - window_size / 2
497 : first_token;
498 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
499 next_token);
503 /* Dump debugging information for the given PARSER. If FILE is NULL,
504 the output is printed on stderr. */
506 void
507 cp_debug_parser (FILE *file, cp_parser *parser)
509 const size_t window_size = 20;
510 cp_token *token;
511 expanded_location eloc;
513 if (file == NULL)
514 file = stderr;
516 fprintf (file, "Parser state\n\n");
517 fprintf (file, "Number of tokens: %u\n",
518 vec_safe_length (parser->lexer->buffer));
519 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
520 cp_debug_print_tree_if_set (file, "Object scope",
521 parser->object_scope);
522 cp_debug_print_tree_if_set (file, "Qualifying scope",
523 parser->qualifying_scope);
524 cp_debug_print_context_stack (file, parser->context);
525 cp_debug_print_flag (file, "Allow GNU extensions",
526 parser->allow_gnu_extensions_p);
527 cp_debug_print_flag (file, "'>' token is greater-than",
528 parser->greater_than_is_operator_p);
529 cp_debug_print_flag (file, "Default args allowed in current "
530 "parameter list", parser->default_arg_ok_p);
531 cp_debug_print_flag (file, "Parsing integral constant-expression",
532 parser->integral_constant_expression_p);
533 cp_debug_print_flag (file, "Allow non-constant expression in current "
534 "constant-expression",
535 parser->allow_non_integral_constant_expression_p);
536 cp_debug_print_flag (file, "Seen non-constant expression",
537 parser->non_integral_constant_expression_p);
538 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
539 "current context",
540 parser->local_variables_forbidden_p);
541 cp_debug_print_flag (file, "In unbraced linkage specification",
542 parser->in_unbraced_linkage_specification_p);
543 cp_debug_print_flag (file, "Parsing a declarator",
544 parser->in_declarator_p);
545 cp_debug_print_flag (file, "In template argument list",
546 parser->in_template_argument_list_p);
547 cp_debug_print_flag (file, "Parsing an iteration statement",
548 parser->in_statement & IN_ITERATION_STMT);
549 cp_debug_print_flag (file, "Parsing a switch statement",
550 parser->in_statement & IN_SWITCH_STMT);
551 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
552 parser->in_statement & IN_OMP_BLOCK);
553 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
554 parser->in_statement & IN_OMP_FOR);
555 cp_debug_print_flag (file, "Parsing an if statement",
556 parser->in_statement & IN_IF_STMT);
557 cp_debug_print_flag (file, "Parsing a type-id in an expression "
558 "context", parser->in_type_id_in_expr_p);
559 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
560 parser->implicit_extern_c);
561 cp_debug_print_flag (file, "String expressions should be translated "
562 "to execution character set",
563 parser->translate_strings_p);
564 cp_debug_print_flag (file, "Parsing function body outside of a "
565 "local class", parser->in_function_body);
566 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
567 parser->colon_corrects_to_scope_p);
568 cp_debug_print_flag (file, "Colon doesn't start a class definition",
569 parser->colon_doesnt_start_class_def_p);
570 if (parser->type_definition_forbidden_message)
571 fprintf (file, "Error message for forbidden type definitions: %s\n",
572 parser->type_definition_forbidden_message);
573 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
574 fprintf (file, "Number of class definitions in progress: %u\n",
575 parser->num_classes_being_defined);
576 fprintf (file, "Number of template parameter lists for the current "
577 "declaration: %u\n", parser->num_template_parameter_lists);
578 cp_debug_parser_tokens (file, parser, window_size);
579 token = parser->lexer->next_token;
580 fprintf (file, "Next token to parse:\n");
581 fprintf (file, "\tToken: ");
582 cp_lexer_print_token (file, token);
583 eloc = expand_location (token->location);
584 fprintf (file, "\n\tFile: %s\n", eloc.file);
585 fprintf (file, "\tLine: %d\n", eloc.line);
586 fprintf (file, "\tColumn: %d\n", eloc.column);
589 DEBUG_FUNCTION void
590 debug (cp_parser &ref)
592 cp_debug_parser (stderr, &ref);
595 DEBUG_FUNCTION void
596 debug (cp_parser *ptr)
598 if (ptr)
599 debug (*ptr);
600 else
601 fprintf (stderr, "<nil>\n");
604 /* Allocate memory for a new lexer object and return it. */
606 static cp_lexer *
607 cp_lexer_alloc (void)
609 cp_lexer *lexer;
611 c_common_no_more_pch ();
613 /* Allocate the memory. */
614 lexer = ggc_cleared_alloc<cp_lexer> ();
616 /* Initially we are not debugging. */
617 lexer->debugging_p = false;
619 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
621 /* Create the buffer. */
622 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
624 return lexer;
628 /* Create a new main C++ lexer, the lexer that gets tokens from the
629 preprocessor. */
631 static cp_lexer *
632 cp_lexer_new_main (void)
634 cp_lexer *lexer;
635 cp_token token;
637 /* It's possible that parsing the first pragma will load a PCH file,
638 which is a GC collection point. So we have to do that before
639 allocating any memory. */
640 cp_parser_initial_pragma (&token);
642 lexer = cp_lexer_alloc ();
644 /* Put the first token in the buffer. */
645 lexer->buffer->quick_push (token);
647 /* Get the remaining tokens from the preprocessor. */
648 while (token.type != CPP_EOF)
650 cp_lexer_get_preprocessor_token (lexer, &token);
651 vec_safe_push (lexer->buffer, token);
654 lexer->last_token = lexer->buffer->address ()
655 + lexer->buffer->length ()
656 - 1;
657 lexer->next_token = lexer->buffer->length ()
658 ? lexer->buffer->address ()
659 : &eof_token;
661 /* Subsequent preprocessor diagnostics should use compiler
662 diagnostic functions to get the compiler source location. */
663 done_lexing = true;
665 gcc_assert (!lexer->next_token->purged_p);
666 return lexer;
669 /* Create a new lexer whose token stream is primed with the tokens in
670 CACHE. When these tokens are exhausted, no new tokens will be read. */
672 static cp_lexer *
673 cp_lexer_new_from_tokens (cp_token_cache *cache)
675 cp_token *first = cache->first;
676 cp_token *last = cache->last;
677 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
679 /* We do not own the buffer. */
680 lexer->buffer = NULL;
681 lexer->next_token = first == last ? &eof_token : first;
682 lexer->last_token = last;
684 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
686 /* Initially we are not debugging. */
687 lexer->debugging_p = false;
689 gcc_assert (!lexer->next_token->purged_p);
690 return lexer;
693 /* Frees all resources associated with LEXER. */
695 static void
696 cp_lexer_destroy (cp_lexer *lexer)
698 vec_free (lexer->buffer);
699 lexer->saved_tokens.release ();
700 ggc_free (lexer);
703 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
704 be used. The point of this flag is to help the compiler to fold away calls
705 to cp_lexer_debugging_p within this source file at compile time, when the
706 lexer is not being debugged. */
708 #define LEXER_DEBUGGING_ENABLED_P false
710 /* Returns nonzero if debugging information should be output. */
712 static inline bool
713 cp_lexer_debugging_p (cp_lexer *lexer)
715 if (!LEXER_DEBUGGING_ENABLED_P)
716 return false;
718 return lexer->debugging_p;
722 static inline cp_token_position
723 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
725 gcc_assert (!previous_p || lexer->next_token != &eof_token);
727 return lexer->next_token - previous_p;
730 static inline cp_token *
731 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
733 return pos;
736 static inline void
737 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
739 lexer->next_token = cp_lexer_token_at (lexer, pos);
742 static inline cp_token_position
743 cp_lexer_previous_token_position (cp_lexer *lexer)
745 if (lexer->next_token == &eof_token)
746 return lexer->last_token - 1;
747 else
748 return cp_lexer_token_position (lexer, true);
751 static inline cp_token *
752 cp_lexer_previous_token (cp_lexer *lexer)
754 cp_token_position tp = cp_lexer_previous_token_position (lexer);
756 /* Skip past purged tokens. */
757 while (tp->purged_p)
759 gcc_assert (tp != vec_safe_address (lexer->buffer));
760 tp--;
763 return cp_lexer_token_at (lexer, tp);
766 /* nonzero if we are presently saving tokens. */
768 static inline int
769 cp_lexer_saving_tokens (const cp_lexer* lexer)
771 return lexer->saved_tokens.length () != 0;
774 /* Store the next token from the preprocessor in *TOKEN. Return true
775 if we reach EOF. If LEXER is NULL, assume we are handling an
776 initial #pragma pch_preprocess, and thus want the lexer to return
777 processed strings. */
779 static void
780 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
782 static int is_extern_c = 0;
784 /* Get a new token from the preprocessor. */
785 token->type
786 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
787 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
788 token->keyword = RID_MAX;
789 token->purged_p = false;
790 token->error_reported = false;
792 /* On some systems, some header files are surrounded by an
793 implicit extern "C" block. Set a flag in the token if it
794 comes from such a header. */
795 is_extern_c += pending_lang_change;
796 pending_lang_change = 0;
797 token->implicit_extern_c = is_extern_c > 0;
799 /* Check to see if this token is a keyword. */
800 if (token->type == CPP_NAME)
802 if (IDENTIFIER_KEYWORD_P (token->u.value))
804 /* Mark this token as a keyword. */
805 token->type = CPP_KEYWORD;
806 /* Record which keyword. */
807 token->keyword = C_RID_CODE (token->u.value);
809 else
811 if (warn_cxx11_compat
812 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
813 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
815 /* Warn about the C++0x keyword (but still treat it as
816 an identifier). */
817 warning (OPT_Wc__11_compat,
818 "identifier %qE is a keyword in C++11",
819 token->u.value);
821 /* Clear out the C_RID_CODE so we don't warn about this
822 particular identifier-turned-keyword again. */
823 C_SET_RID_CODE (token->u.value, RID_MAX);
826 token->keyword = RID_MAX;
829 else if (token->type == CPP_AT_NAME)
831 /* This only happens in Objective-C++; it must be a keyword. */
832 token->type = CPP_KEYWORD;
833 switch (C_RID_CODE (token->u.value))
835 /* Replace 'class' with '@class', 'private' with '@private',
836 etc. This prevents confusion with the C++ keyword
837 'class', and makes the tokens consistent with other
838 Objective-C 'AT' keywords. For example '@class' is
839 reported as RID_AT_CLASS which is consistent with
840 '@synchronized', which is reported as
841 RID_AT_SYNCHRONIZED.
843 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
844 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
845 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
846 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
847 case RID_THROW: token->keyword = RID_AT_THROW; break;
848 case RID_TRY: token->keyword = RID_AT_TRY; break;
849 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
850 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
851 default: token->keyword = C_RID_CODE (token->u.value);
856 /* Update the globals input_location and the input file stack from TOKEN. */
857 static inline void
858 cp_lexer_set_source_position_from_token (cp_token *token)
860 if (token->type != CPP_EOF)
862 input_location = token->location;
866 /* Update the globals input_location and the input file stack from LEXER. */
867 static inline void
868 cp_lexer_set_source_position (cp_lexer *lexer)
870 cp_token *token = cp_lexer_peek_token (lexer);
871 cp_lexer_set_source_position_from_token (token);
874 /* Return a pointer to the next token in the token stream, but do not
875 consume it. */
877 static inline cp_token *
878 cp_lexer_peek_token (cp_lexer *lexer)
880 if (cp_lexer_debugging_p (lexer))
882 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
883 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
884 putc ('\n', cp_lexer_debug_stream);
886 return lexer->next_token;
889 /* Return true if the next token has the indicated TYPE. */
891 static inline bool
892 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
894 return cp_lexer_peek_token (lexer)->type == type;
897 /* Return true if the next token does not have the indicated TYPE. */
899 static inline bool
900 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
902 return !cp_lexer_next_token_is (lexer, type);
905 /* Return true if the next token is the indicated KEYWORD. */
907 static inline bool
908 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
910 return cp_lexer_peek_token (lexer)->keyword == keyword;
913 static inline bool
914 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
916 return cp_lexer_peek_nth_token (lexer, n)->type == type;
919 static inline bool
920 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
922 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
925 /* Return true if the next token is not the indicated KEYWORD. */
927 static inline bool
928 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
930 return cp_lexer_peek_token (lexer)->keyword != keyword;
933 /* Return true if KEYWORD can start a decl-specifier. */
935 bool
936 cp_keyword_starts_decl_specifier_p (enum rid keyword)
938 switch (keyword)
940 /* auto specifier: storage-class-specifier in C++,
941 simple-type-specifier in C++0x. */
942 case RID_AUTO:
943 /* Storage classes. */
944 case RID_REGISTER:
945 case RID_STATIC:
946 case RID_EXTERN:
947 case RID_MUTABLE:
948 case RID_THREAD:
949 /* Elaborated type specifiers. */
950 case RID_ENUM:
951 case RID_CLASS:
952 case RID_STRUCT:
953 case RID_UNION:
954 case RID_TYPENAME:
955 /* Simple type specifiers. */
956 case RID_CHAR:
957 case RID_CHAR16:
958 case RID_CHAR32:
959 case RID_WCHAR:
960 case RID_BOOL:
961 case RID_SHORT:
962 case RID_INT:
963 case RID_LONG:
964 case RID_SIGNED:
965 case RID_UNSIGNED:
966 case RID_FLOAT:
967 case RID_DOUBLE:
968 case RID_VOID:
969 /* GNU extensions. */
970 case RID_ATTRIBUTE:
971 case RID_TYPEOF:
972 /* C++0x extensions. */
973 case RID_DECLTYPE:
974 case RID_UNDERLYING_TYPE:
975 case RID_CONSTEXPR:
976 return true;
978 default:
979 if (keyword >= RID_FIRST_INT_N
980 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
981 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
982 return true;
983 return false;
987 /* Return true if the next token is a keyword for a decl-specifier. */
989 static bool
990 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
992 cp_token *token;
994 token = cp_lexer_peek_token (lexer);
995 return cp_keyword_starts_decl_specifier_p (token->keyword);
998 /* Returns TRUE iff the token T begins a decltype type. */
1000 static bool
1001 token_is_decltype (cp_token *t)
1003 return (t->keyword == RID_DECLTYPE
1004 || t->type == CPP_DECLTYPE);
1007 /* Returns TRUE iff the next token begins a decltype type. */
1009 static bool
1010 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1012 cp_token *t = cp_lexer_peek_token (lexer);
1013 return token_is_decltype (t);
1016 /* Called when processing a token with tree_check_value; perform or defer the
1017 associated checks and return the value. */
1019 static tree
1020 saved_checks_value (struct tree_check *check_value)
1022 /* Perform any access checks that were deferred. */
1023 vec<deferred_access_check, va_gc> *checks;
1024 deferred_access_check *chk;
1025 checks = check_value->checks;
1026 if (checks)
1028 int i;
1029 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1030 perform_or_defer_access_check (chk->binfo,
1031 chk->decl,
1032 chk->diag_decl, tf_warning_or_error);
1034 /* Return the stored value. */
1035 return check_value->value;
1038 /* Return a pointer to the Nth token in the token stream. If N is 1,
1039 then this is precisely equivalent to cp_lexer_peek_token (except
1040 that it is not inline). One would like to disallow that case, but
1041 there is one case (cp_parser_nth_token_starts_template_id) where
1042 the caller passes a variable for N and it might be 1. */
1044 static cp_token *
1045 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1047 cp_token *token;
1049 /* N is 1-based, not zero-based. */
1050 gcc_assert (n > 0);
1052 if (cp_lexer_debugging_p (lexer))
1053 fprintf (cp_lexer_debug_stream,
1054 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1056 --n;
1057 token = lexer->next_token;
1058 gcc_assert (!n || token != &eof_token);
1059 while (n != 0)
1061 ++token;
1062 if (token == lexer->last_token)
1064 token = &eof_token;
1065 break;
1068 if (!token->purged_p)
1069 --n;
1072 if (cp_lexer_debugging_p (lexer))
1074 cp_lexer_print_token (cp_lexer_debug_stream, token);
1075 putc ('\n', cp_lexer_debug_stream);
1078 return token;
1081 /* Return the next token, and advance the lexer's next_token pointer
1082 to point to the next non-purged token. */
1084 static cp_token *
1085 cp_lexer_consume_token (cp_lexer* lexer)
1087 cp_token *token = lexer->next_token;
1089 gcc_assert (token != &eof_token);
1090 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1094 lexer->next_token++;
1095 if (lexer->next_token == lexer->last_token)
1097 lexer->next_token = &eof_token;
1098 break;
1102 while (lexer->next_token->purged_p);
1104 cp_lexer_set_source_position_from_token (token);
1106 /* Provide debugging output. */
1107 if (cp_lexer_debugging_p (lexer))
1109 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1110 cp_lexer_print_token (cp_lexer_debug_stream, token);
1111 putc ('\n', cp_lexer_debug_stream);
1114 return token;
1117 /* Permanently remove the next token from the token stream, and
1118 advance the next_token pointer to refer to the next non-purged
1119 token. */
1121 static void
1122 cp_lexer_purge_token (cp_lexer *lexer)
1124 cp_token *tok = lexer->next_token;
1126 gcc_assert (tok != &eof_token);
1127 tok->purged_p = true;
1128 tok->location = UNKNOWN_LOCATION;
1129 tok->u.value = NULL_TREE;
1130 tok->keyword = RID_MAX;
1134 tok++;
1135 if (tok == lexer->last_token)
1137 tok = &eof_token;
1138 break;
1141 while (tok->purged_p);
1142 lexer->next_token = tok;
1145 /* Permanently remove all tokens after TOK, up to, but not
1146 including, the token that will be returned next by
1147 cp_lexer_peek_token. */
1149 static void
1150 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1152 cp_token *peek = lexer->next_token;
1154 if (peek == &eof_token)
1155 peek = lexer->last_token;
1157 gcc_assert (tok < peek);
1159 for ( tok += 1; tok != peek; tok += 1)
1161 tok->purged_p = true;
1162 tok->location = UNKNOWN_LOCATION;
1163 tok->u.value = NULL_TREE;
1164 tok->keyword = RID_MAX;
1168 /* Begin saving tokens. All tokens consumed after this point will be
1169 preserved. */
1171 static void
1172 cp_lexer_save_tokens (cp_lexer* lexer)
1174 /* Provide debugging output. */
1175 if (cp_lexer_debugging_p (lexer))
1176 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1178 lexer->saved_tokens.safe_push (lexer->next_token);
1181 /* Commit to the portion of the token stream most recently saved. */
1183 static void
1184 cp_lexer_commit_tokens (cp_lexer* lexer)
1186 /* Provide debugging output. */
1187 if (cp_lexer_debugging_p (lexer))
1188 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1190 lexer->saved_tokens.pop ();
1193 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1194 to the token stream. Stop saving tokens. */
1196 static void
1197 cp_lexer_rollback_tokens (cp_lexer* lexer)
1199 /* Provide debugging output. */
1200 if (cp_lexer_debugging_p (lexer))
1201 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1203 lexer->next_token = lexer->saved_tokens.pop ();
1206 /* RAII wrapper around the above functions, with sanity checking. Creating
1207 a variable saves tokens, which are committed when the variable is
1208 destroyed unless they are explicitly rolled back by calling the rollback
1209 member function. */
1211 struct saved_token_sentinel
1213 cp_lexer *lexer;
1214 unsigned len;
1215 bool commit;
1216 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1218 len = lexer->saved_tokens.length ();
1219 cp_lexer_save_tokens (lexer);
1221 void rollback ()
1223 cp_lexer_rollback_tokens (lexer);
1224 commit = false;
1226 ~saved_token_sentinel()
1228 if (commit)
1229 cp_lexer_commit_tokens (lexer);
1230 gcc_assert (lexer->saved_tokens.length () == len);
1234 /* Print a representation of the TOKEN on the STREAM. */
1236 static void
1237 cp_lexer_print_token (FILE * stream, cp_token *token)
1239 /* We don't use cpp_type2name here because the parser defines
1240 a few tokens of its own. */
1241 static const char *const token_names[] = {
1242 /* cpplib-defined token types */
1243 #define OP(e, s) #e,
1244 #define TK(e, s) #e,
1245 TTYPE_TABLE
1246 #undef OP
1247 #undef TK
1248 /* C++ parser token types - see "Manifest constants", above. */
1249 "KEYWORD",
1250 "TEMPLATE_ID",
1251 "NESTED_NAME_SPECIFIER",
1254 /* For some tokens, print the associated data. */
1255 switch (token->type)
1257 case CPP_KEYWORD:
1258 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1259 For example, `struct' is mapped to an INTEGER_CST. */
1260 if (!identifier_p (token->u.value))
1261 break;
1262 /* fall through */
1263 case CPP_NAME:
1264 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1265 break;
1267 case CPP_STRING:
1268 case CPP_STRING16:
1269 case CPP_STRING32:
1270 case CPP_WSTRING:
1271 case CPP_UTF8STRING:
1272 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1273 break;
1275 case CPP_NUMBER:
1276 print_generic_expr (stream, token->u.value);
1277 break;
1279 default:
1280 /* If we have a name for the token, print it out. Otherwise, we
1281 simply give the numeric code. */
1282 if (token->type < ARRAY_SIZE(token_names))
1283 fputs (token_names[token->type], stream);
1284 else
1285 fprintf (stream, "[%d]", token->type);
1286 break;
1290 DEBUG_FUNCTION void
1291 debug (cp_token &ref)
1293 cp_lexer_print_token (stderr, &ref);
1294 fprintf (stderr, "\n");
1297 DEBUG_FUNCTION void
1298 debug (cp_token *ptr)
1300 if (ptr)
1301 debug (*ptr);
1302 else
1303 fprintf (stderr, "<nil>\n");
1307 /* Start emitting debugging information. */
1309 static void
1310 cp_lexer_start_debugging (cp_lexer* lexer)
1312 if (!LEXER_DEBUGGING_ENABLED_P)
1313 fatal_error (input_location,
1314 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1316 lexer->debugging_p = true;
1317 cp_lexer_debug_stream = stderr;
1320 /* Stop emitting debugging information. */
1322 static void
1323 cp_lexer_stop_debugging (cp_lexer* lexer)
1325 if (!LEXER_DEBUGGING_ENABLED_P)
1326 fatal_error (input_location,
1327 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1329 lexer->debugging_p = false;
1330 cp_lexer_debug_stream = NULL;
1333 /* Create a new cp_token_cache, representing a range of tokens. */
1335 static cp_token_cache *
1336 cp_token_cache_new (cp_token *first, cp_token *last)
1338 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1339 cache->first = first;
1340 cache->last = last;
1341 return cache;
1344 /* Diagnose if #pragma omp declare simd isn't followed immediately
1345 by function declaration or definition. */
1347 static inline void
1348 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1350 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1352 error ("%<#pragma omp declare simd%> not immediately followed by "
1353 "function declaration or definition");
1354 parser->omp_declare_simd = NULL;
1358 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1359 and put that into "omp declare simd" attribute. */
1361 static inline void
1362 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1364 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1366 if (fndecl == error_mark_node)
1368 parser->omp_declare_simd = NULL;
1369 return;
1371 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1373 cp_ensure_no_omp_declare_simd (parser);
1374 return;
1379 /* Diagnose if #pragma acc routine isn't followed immediately by function
1380 declaration or definition. */
1382 static inline void
1383 cp_ensure_no_oacc_routine (cp_parser *parser)
1385 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1387 error_at (parser->oacc_routine->loc,
1388 "%<#pragma acc routine%> not immediately followed by "
1389 "function declaration or definition");
1390 parser->oacc_routine = NULL;
1394 /* Decl-specifiers. */
1396 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1398 static void
1399 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1401 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1404 /* Declarators. */
1406 /* Nothing other than the parser should be creating declarators;
1407 declarators are a semi-syntactic representation of C++ entities.
1408 Other parts of the front end that need to create entities (like
1409 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1411 static cp_declarator *make_call_declarator
1412 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1413 static cp_declarator *make_array_declarator
1414 (cp_declarator *, tree);
1415 static cp_declarator *make_pointer_declarator
1416 (cp_cv_quals, cp_declarator *, tree);
1417 static cp_declarator *make_reference_declarator
1418 (cp_cv_quals, cp_declarator *, bool, tree);
1419 static cp_declarator *make_ptrmem_declarator
1420 (cp_cv_quals, tree, cp_declarator *, tree);
1422 /* An erroneous declarator. */
1423 static cp_declarator *cp_error_declarator;
1425 /* The obstack on which declarators and related data structures are
1426 allocated. */
1427 static struct obstack declarator_obstack;
1429 /* Alloc BYTES from the declarator memory pool. */
1431 static inline void *
1432 alloc_declarator (size_t bytes)
1434 return obstack_alloc (&declarator_obstack, bytes);
1437 /* Allocate a declarator of the indicated KIND. Clear fields that are
1438 common to all declarators. */
1440 static cp_declarator *
1441 make_declarator (cp_declarator_kind kind)
1443 cp_declarator *declarator;
1445 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1446 declarator->kind = kind;
1447 declarator->parenthesized = UNKNOWN_LOCATION;
1448 declarator->attributes = NULL_TREE;
1449 declarator->std_attributes = NULL_TREE;
1450 declarator->declarator = NULL;
1451 declarator->parameter_pack_p = false;
1452 declarator->id_loc = UNKNOWN_LOCATION;
1454 return declarator;
1457 /* Make a declarator for a generalized identifier. If
1458 QUALIFYING_SCOPE is non-NULL, the identifier is
1459 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1460 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1461 is, if any. */
1463 static cp_declarator *
1464 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1465 special_function_kind sfk)
1467 cp_declarator *declarator;
1469 /* It is valid to write:
1471 class C { void f(); };
1472 typedef C D;
1473 void D::f();
1475 The standard is not clear about whether `typedef const C D' is
1476 legal; as of 2002-09-15 the committee is considering that
1477 question. EDG 3.0 allows that syntax. Therefore, we do as
1478 well. */
1479 if (qualifying_scope && TYPE_P (qualifying_scope))
1480 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1482 gcc_assert (identifier_p (unqualified_name)
1483 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1484 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1486 declarator = make_declarator (cdk_id);
1487 declarator->u.id.qualifying_scope = qualifying_scope;
1488 declarator->u.id.unqualified_name = unqualified_name;
1489 declarator->u.id.sfk = sfk;
1491 return declarator;
1494 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1495 of modifiers such as const or volatile to apply to the pointer
1496 type, represented as identifiers. ATTRIBUTES represent the attributes that
1497 appertain to the pointer or reference. */
1499 cp_declarator *
1500 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1501 tree attributes)
1503 cp_declarator *declarator;
1505 declarator = make_declarator (cdk_pointer);
1506 declarator->declarator = target;
1507 declarator->u.pointer.qualifiers = cv_qualifiers;
1508 declarator->u.pointer.class_type = NULL_TREE;
1509 if (target)
1511 declarator->id_loc = target->id_loc;
1512 declarator->parameter_pack_p = target->parameter_pack_p;
1513 target->parameter_pack_p = false;
1515 else
1516 declarator->parameter_pack_p = false;
1518 declarator->std_attributes = attributes;
1520 return declarator;
1523 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1524 represent the attributes that appertain to the pointer or
1525 reference. */
1527 cp_declarator *
1528 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1529 bool rvalue_ref, tree attributes)
1531 cp_declarator *declarator;
1533 declarator = make_declarator (cdk_reference);
1534 declarator->declarator = target;
1535 declarator->u.reference.qualifiers = cv_qualifiers;
1536 declarator->u.reference.rvalue_ref = rvalue_ref;
1537 if (target)
1539 declarator->id_loc = target->id_loc;
1540 declarator->parameter_pack_p = target->parameter_pack_p;
1541 target->parameter_pack_p = false;
1543 else
1544 declarator->parameter_pack_p = false;
1546 declarator->std_attributes = attributes;
1548 return declarator;
1551 /* Like make_pointer_declarator -- but for a pointer to a non-static
1552 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1553 appertain to the pointer or reference. */
1555 cp_declarator *
1556 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1557 cp_declarator *pointee,
1558 tree attributes)
1560 cp_declarator *declarator;
1562 declarator = make_declarator (cdk_ptrmem);
1563 declarator->declarator = pointee;
1564 declarator->u.pointer.qualifiers = cv_qualifiers;
1565 declarator->u.pointer.class_type = class_type;
1567 if (pointee)
1569 declarator->parameter_pack_p = pointee->parameter_pack_p;
1570 pointee->parameter_pack_p = false;
1572 else
1573 declarator->parameter_pack_p = false;
1575 declarator->std_attributes = attributes;
1577 return declarator;
1580 /* Make a declarator for the function given by TARGET, with the
1581 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1582 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1583 indicates what exceptions can be thrown. */
1585 cp_declarator *
1586 make_call_declarator (cp_declarator *target,
1587 tree parms,
1588 cp_cv_quals cv_qualifiers,
1589 cp_virt_specifiers virt_specifiers,
1590 cp_ref_qualifier ref_qualifier,
1591 tree tx_qualifier,
1592 tree exception_specification,
1593 tree late_return_type,
1594 tree requires_clause)
1596 cp_declarator *declarator;
1598 declarator = make_declarator (cdk_function);
1599 declarator->declarator = target;
1600 declarator->u.function.parameters = parms;
1601 declarator->u.function.qualifiers = cv_qualifiers;
1602 declarator->u.function.virt_specifiers = virt_specifiers;
1603 declarator->u.function.ref_qualifier = ref_qualifier;
1604 declarator->u.function.tx_qualifier = tx_qualifier;
1605 declarator->u.function.exception_specification = exception_specification;
1606 declarator->u.function.late_return_type = late_return_type;
1607 declarator->u.function.requires_clause = requires_clause;
1608 if (target)
1610 declarator->id_loc = target->id_loc;
1611 declarator->parameter_pack_p = target->parameter_pack_p;
1612 target->parameter_pack_p = false;
1614 else
1615 declarator->parameter_pack_p = false;
1617 return declarator;
1620 /* Make a declarator for an array of BOUNDS elements, each of which is
1621 defined by ELEMENT. */
1623 cp_declarator *
1624 make_array_declarator (cp_declarator *element, tree bounds)
1626 cp_declarator *declarator;
1628 declarator = make_declarator (cdk_array);
1629 declarator->declarator = element;
1630 declarator->u.array.bounds = bounds;
1631 if (element)
1633 declarator->id_loc = element->id_loc;
1634 declarator->parameter_pack_p = element->parameter_pack_p;
1635 element->parameter_pack_p = false;
1637 else
1638 declarator->parameter_pack_p = false;
1640 return declarator;
1643 /* Determine whether the declarator we've seen so far can be a
1644 parameter pack, when followed by an ellipsis. */
1645 static bool
1646 declarator_can_be_parameter_pack (cp_declarator *declarator)
1648 if (declarator && declarator->parameter_pack_p)
1649 /* We already saw an ellipsis. */
1650 return false;
1652 /* Search for a declarator name, or any other declarator that goes
1653 after the point where the ellipsis could appear in a parameter
1654 pack. If we find any of these, then this declarator can not be
1655 made into a parameter pack. */
1656 bool found = false;
1657 while (declarator && !found)
1659 switch ((int)declarator->kind)
1661 case cdk_id:
1662 case cdk_array:
1663 case cdk_decomp:
1664 found = true;
1665 break;
1667 case cdk_error:
1668 return true;
1670 default:
1671 declarator = declarator->declarator;
1672 break;
1676 return !found;
1679 cp_parameter_declarator *no_parameters;
1681 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1682 DECLARATOR and DEFAULT_ARGUMENT. */
1684 cp_parameter_declarator *
1685 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1686 cp_declarator *declarator,
1687 tree default_argument,
1688 location_t loc,
1689 bool template_parameter_pack_p = false)
1691 cp_parameter_declarator *parameter;
1693 parameter = ((cp_parameter_declarator *)
1694 alloc_declarator (sizeof (cp_parameter_declarator)));
1695 parameter->next = NULL;
1696 if (decl_specifiers)
1697 parameter->decl_specifiers = *decl_specifiers;
1698 else
1699 clear_decl_specs (&parameter->decl_specifiers);
1700 parameter->declarator = declarator;
1701 parameter->default_argument = default_argument;
1702 parameter->template_parameter_pack_p = template_parameter_pack_p;
1703 parameter->loc = loc;
1705 return parameter;
1708 /* Returns true iff DECLARATOR is a declaration for a function. */
1710 static bool
1711 function_declarator_p (const cp_declarator *declarator)
1713 while (declarator)
1715 if (declarator->kind == cdk_function
1716 && declarator->declarator->kind == cdk_id)
1717 return true;
1718 if (declarator->kind == cdk_id
1719 || declarator->kind == cdk_decomp
1720 || declarator->kind == cdk_error)
1721 return false;
1722 declarator = declarator->declarator;
1724 return false;
1727 /* The parser. */
1729 /* Overview
1730 --------
1732 A cp_parser parses the token stream as specified by the C++
1733 grammar. Its job is purely parsing, not semantic analysis. For
1734 example, the parser breaks the token stream into declarators,
1735 expressions, statements, and other similar syntactic constructs.
1736 It does not check that the types of the expressions on either side
1737 of an assignment-statement are compatible, or that a function is
1738 not declared with a parameter of type `void'.
1740 The parser invokes routines elsewhere in the compiler to perform
1741 semantic analysis and to build up the abstract syntax tree for the
1742 code processed.
1744 The parser (and the template instantiation code, which is, in a
1745 way, a close relative of parsing) are the only parts of the
1746 compiler that should be calling push_scope and pop_scope, or
1747 related functions. The parser (and template instantiation code)
1748 keeps track of what scope is presently active; everything else
1749 should simply honor that. (The code that generates static
1750 initializers may also need to set the scope, in order to check
1751 access control correctly when emitting the initializers.)
1753 Methodology
1754 -----------
1756 The parser is of the standard recursive-descent variety. Upcoming
1757 tokens in the token stream are examined in order to determine which
1758 production to use when parsing a non-terminal. Some C++ constructs
1759 require arbitrary look ahead to disambiguate. For example, it is
1760 impossible, in the general case, to tell whether a statement is an
1761 expression or declaration without scanning the entire statement.
1762 Therefore, the parser is capable of "parsing tentatively." When the
1763 parser is not sure what construct comes next, it enters this mode.
1764 Then, while we attempt to parse the construct, the parser queues up
1765 error messages, rather than issuing them immediately, and saves the
1766 tokens it consumes. If the construct is parsed successfully, the
1767 parser "commits", i.e., it issues any queued error messages and
1768 the tokens that were being preserved are permanently discarded.
1769 If, however, the construct is not parsed successfully, the parser
1770 rolls back its state completely so that it can resume parsing using
1771 a different alternative.
1773 Future Improvements
1774 -------------------
1776 The performance of the parser could probably be improved substantially.
1777 We could often eliminate the need to parse tentatively by looking ahead
1778 a little bit. In some places, this approach might not entirely eliminate
1779 the need to parse tentatively, but it might still speed up the average
1780 case. */
1782 /* Flags that are passed to some parsing functions. These values can
1783 be bitwise-ored together. */
1785 enum
1787 /* No flags. */
1788 CP_PARSER_FLAGS_NONE = 0x0,
1789 /* The construct is optional. If it is not present, then no error
1790 should be issued. */
1791 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1792 /* When parsing a type-specifier, treat user-defined type-names
1793 as non-type identifiers. */
1794 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1795 /* When parsing a type-specifier, do not try to parse a class-specifier
1796 or enum-specifier. */
1797 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1798 /* When parsing a decl-specifier-seq, only allow type-specifier or
1799 constexpr. */
1800 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1801 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1802 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1805 /* This type is used for parameters and variables which hold
1806 combinations of the above flags. */
1807 typedef int cp_parser_flags;
1809 /* The different kinds of declarators we want to parse. */
1811 enum cp_parser_declarator_kind
1813 /* We want an abstract declarator. */
1814 CP_PARSER_DECLARATOR_ABSTRACT,
1815 /* We want a named declarator. */
1816 CP_PARSER_DECLARATOR_NAMED,
1817 /* We don't mind, but the name must be an unqualified-id. */
1818 CP_PARSER_DECLARATOR_EITHER
1821 /* The precedence values used to parse binary expressions. The minimum value
1822 of PREC must be 1, because zero is reserved to quickly discriminate
1823 binary operators from other tokens. */
1825 enum cp_parser_prec
1827 PREC_NOT_OPERATOR,
1828 PREC_LOGICAL_OR_EXPRESSION,
1829 PREC_LOGICAL_AND_EXPRESSION,
1830 PREC_INCLUSIVE_OR_EXPRESSION,
1831 PREC_EXCLUSIVE_OR_EXPRESSION,
1832 PREC_AND_EXPRESSION,
1833 PREC_EQUALITY_EXPRESSION,
1834 PREC_RELATIONAL_EXPRESSION,
1835 PREC_SHIFT_EXPRESSION,
1836 PREC_ADDITIVE_EXPRESSION,
1837 PREC_MULTIPLICATIVE_EXPRESSION,
1838 PREC_PM_EXPRESSION,
1839 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1842 /* A mapping from a token type to a corresponding tree node type, with a
1843 precedence value. */
1845 struct cp_parser_binary_operations_map_node
1847 /* The token type. */
1848 enum cpp_ttype token_type;
1849 /* The corresponding tree code. */
1850 enum tree_code tree_type;
1851 /* The precedence of this operator. */
1852 enum cp_parser_prec prec;
1855 struct cp_parser_expression_stack_entry
1857 /* Left hand side of the binary operation we are currently
1858 parsing. */
1859 cp_expr lhs;
1860 /* Original tree code for left hand side, if it was a binary
1861 expression itself (used for -Wparentheses). */
1862 enum tree_code lhs_type;
1863 /* Tree code for the binary operation we are parsing. */
1864 enum tree_code tree_type;
1865 /* Precedence of the binary operation we are parsing. */
1866 enum cp_parser_prec prec;
1867 /* Location of the binary operation we are parsing. */
1868 location_t loc;
1871 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1872 entries because precedence levels on the stack are monotonically
1873 increasing. */
1874 typedef struct cp_parser_expression_stack_entry
1875 cp_parser_expression_stack[NUM_PREC_VALUES];
1877 /* Prototypes. */
1879 /* Constructors and destructors. */
1881 static cp_parser_context *cp_parser_context_new
1882 (cp_parser_context *);
1884 /* Class variables. */
1886 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1888 /* The operator-precedence table used by cp_parser_binary_expression.
1889 Transformed into an associative array (binops_by_token) by
1890 cp_parser_new. */
1892 static const cp_parser_binary_operations_map_node binops[] = {
1893 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1894 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1896 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1897 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1898 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1900 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1901 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1903 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1904 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1906 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1907 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1908 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1909 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1911 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1912 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1914 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1916 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1918 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1920 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1922 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1925 /* The same as binops, but initialized by cp_parser_new so that
1926 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1927 for speed. */
1928 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1930 /* Constructors and destructors. */
1932 /* Construct a new context. The context below this one on the stack
1933 is given by NEXT. */
1935 static cp_parser_context *
1936 cp_parser_context_new (cp_parser_context* next)
1938 cp_parser_context *context;
1940 /* Allocate the storage. */
1941 if (cp_parser_context_free_list != NULL)
1943 /* Pull the first entry from the free list. */
1944 context = cp_parser_context_free_list;
1945 cp_parser_context_free_list = context->next;
1946 memset (context, 0, sizeof (*context));
1948 else
1949 context = ggc_cleared_alloc<cp_parser_context> ();
1951 /* No errors have occurred yet in this context. */
1952 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1953 /* If this is not the bottommost context, copy information that we
1954 need from the previous context. */
1955 if (next)
1957 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1958 expression, then we are parsing one in this context, too. */
1959 context->object_type = next->object_type;
1960 /* Thread the stack. */
1961 context->next = next;
1964 return context;
1967 /* Managing the unparsed function queues. */
1969 #define unparsed_funs_with_default_args \
1970 parser->unparsed_queues->last ().funs_with_default_args
1971 #define unparsed_funs_with_definitions \
1972 parser->unparsed_queues->last ().funs_with_definitions
1973 #define unparsed_nsdmis \
1974 parser->unparsed_queues->last ().nsdmis
1975 #define unparsed_classes \
1976 parser->unparsed_queues->last ().classes
1978 static void
1979 push_unparsed_function_queues (cp_parser *parser)
1981 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1982 vec_safe_push (parser->unparsed_queues, e);
1985 static void
1986 pop_unparsed_function_queues (cp_parser *parser)
1988 release_tree_vector (unparsed_funs_with_definitions);
1989 parser->unparsed_queues->pop ();
1992 /* Prototypes. */
1994 /* Constructors and destructors. */
1996 static cp_parser *cp_parser_new
1997 (void);
1999 /* Routines to parse various constructs.
2001 Those that return `tree' will return the error_mark_node (rather
2002 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2003 Sometimes, they will return an ordinary node if error-recovery was
2004 attempted, even though a parse error occurred. So, to check
2005 whether or not a parse error occurred, you should always use
2006 cp_parser_error_occurred. If the construct is optional (indicated
2007 either by an `_opt' in the name of the function that does the
2008 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2009 the construct is not present. */
2011 /* Lexical conventions [gram.lex] */
2013 static cp_expr cp_parser_identifier
2014 (cp_parser *);
2015 static cp_expr cp_parser_string_literal
2016 (cp_parser *, bool, bool, bool);
2017 static cp_expr cp_parser_userdef_char_literal
2018 (cp_parser *);
2019 static tree cp_parser_userdef_string_literal
2020 (tree);
2021 static cp_expr cp_parser_userdef_numeric_literal
2022 (cp_parser *);
2024 /* Basic concepts [gram.basic] */
2026 static bool cp_parser_translation_unit
2027 (cp_parser *);
2029 /* Expressions [gram.expr] */
2031 static cp_expr cp_parser_primary_expression
2032 (cp_parser *, bool, bool, bool, cp_id_kind *);
2033 static cp_expr cp_parser_id_expression
2034 (cp_parser *, bool, bool, bool *, bool, bool);
2035 static cp_expr cp_parser_unqualified_id
2036 (cp_parser *, bool, bool, bool, bool);
2037 static tree cp_parser_nested_name_specifier_opt
2038 (cp_parser *, bool, bool, bool, bool, bool = false);
2039 static tree cp_parser_nested_name_specifier
2040 (cp_parser *, bool, bool, bool, bool);
2041 static tree cp_parser_qualifying_entity
2042 (cp_parser *, bool, bool, bool, bool, bool);
2043 static cp_expr cp_parser_postfix_expression
2044 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2045 static tree cp_parser_postfix_open_square_expression
2046 (cp_parser *, tree, bool, bool);
2047 static tree cp_parser_postfix_dot_deref_expression
2048 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2049 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2050 (cp_parser *, int, bool, bool, bool *, location_t * = NULL);
2051 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2052 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2053 static void cp_parser_pseudo_destructor_name
2054 (cp_parser *, tree, tree *, tree *);
2055 static cp_expr cp_parser_unary_expression
2056 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2057 static enum tree_code cp_parser_unary_operator
2058 (cp_token *);
2059 static tree cp_parser_new_expression
2060 (cp_parser *);
2061 static vec<tree, va_gc> *cp_parser_new_placement
2062 (cp_parser *);
2063 static tree cp_parser_new_type_id
2064 (cp_parser *, tree *);
2065 static cp_declarator *cp_parser_new_declarator_opt
2066 (cp_parser *);
2067 static cp_declarator *cp_parser_direct_new_declarator
2068 (cp_parser *);
2069 static vec<tree, va_gc> *cp_parser_new_initializer
2070 (cp_parser *);
2071 static tree cp_parser_delete_expression
2072 (cp_parser *);
2073 static cp_expr cp_parser_cast_expression
2074 (cp_parser *, bool, bool, bool, cp_id_kind *);
2075 static cp_expr cp_parser_binary_expression
2076 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2077 static tree cp_parser_question_colon_clause
2078 (cp_parser *, cp_expr);
2079 static cp_expr cp_parser_assignment_expression
2080 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2081 static enum tree_code cp_parser_assignment_operator_opt
2082 (cp_parser *);
2083 static cp_expr cp_parser_expression
2084 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2085 static cp_expr cp_parser_constant_expression
2086 (cp_parser *, bool = false, bool * = NULL, bool = false);
2087 static cp_expr cp_parser_builtin_offsetof
2088 (cp_parser *);
2089 static cp_expr cp_parser_lambda_expression
2090 (cp_parser *);
2091 static void cp_parser_lambda_introducer
2092 (cp_parser *, tree);
2093 static bool cp_parser_lambda_declarator_opt
2094 (cp_parser *, tree);
2095 static void cp_parser_lambda_body
2096 (cp_parser *, tree);
2098 /* Statements [gram.stmt.stmt] */
2100 static void cp_parser_statement
2101 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2102 static void cp_parser_label_for_labeled_statement
2103 (cp_parser *, tree);
2104 static tree cp_parser_expression_statement
2105 (cp_parser *, tree);
2106 static tree cp_parser_compound_statement
2107 (cp_parser *, tree, int, bool);
2108 static void cp_parser_statement_seq_opt
2109 (cp_parser *, tree);
2110 static tree cp_parser_selection_statement
2111 (cp_parser *, bool *, vec<tree> *);
2112 static tree cp_parser_condition
2113 (cp_parser *);
2114 static tree cp_parser_iteration_statement
2115 (cp_parser *, bool *, bool);
2116 static bool cp_parser_init_statement
2117 (cp_parser *, tree *decl);
2118 static tree cp_parser_for
2119 (cp_parser *, bool);
2120 static tree cp_parser_c_for
2121 (cp_parser *, tree, tree, bool);
2122 static tree cp_parser_range_for
2123 (cp_parser *, tree, tree, tree, bool);
2124 static void do_range_for_auto_deduction
2125 (tree, tree);
2126 static tree cp_parser_perform_range_for_lookup
2127 (tree, tree *, tree *);
2128 static tree cp_parser_range_for_member_function
2129 (tree, tree);
2130 static tree cp_parser_jump_statement
2131 (cp_parser *);
2132 static void cp_parser_declaration_statement
2133 (cp_parser *);
2135 static tree cp_parser_implicitly_scoped_statement
2136 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2137 static void cp_parser_already_scoped_statement
2138 (cp_parser *, bool *, const token_indent_info &);
2140 /* Declarations [gram.dcl.dcl] */
2142 static void cp_parser_declaration_seq_opt
2143 (cp_parser *);
2144 static void cp_parser_declaration
2145 (cp_parser *);
2146 static void cp_parser_block_declaration
2147 (cp_parser *, bool);
2148 static void cp_parser_simple_declaration
2149 (cp_parser *, bool, tree *);
2150 static void cp_parser_decl_specifier_seq
2151 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2152 static tree cp_parser_storage_class_specifier_opt
2153 (cp_parser *);
2154 static tree cp_parser_function_specifier_opt
2155 (cp_parser *, cp_decl_specifier_seq *);
2156 static tree cp_parser_type_specifier
2157 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2158 int *, bool *);
2159 static tree cp_parser_simple_type_specifier
2160 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2161 static tree cp_parser_type_name
2162 (cp_parser *, bool);
2163 static tree cp_parser_type_name
2164 (cp_parser *);
2165 static tree cp_parser_nonclass_name
2166 (cp_parser* parser);
2167 static tree cp_parser_elaborated_type_specifier
2168 (cp_parser *, bool, bool);
2169 static tree cp_parser_enum_specifier
2170 (cp_parser *);
2171 static void cp_parser_enumerator_list
2172 (cp_parser *, tree);
2173 static void cp_parser_enumerator_definition
2174 (cp_parser *, tree);
2175 static tree cp_parser_namespace_name
2176 (cp_parser *);
2177 static void cp_parser_namespace_definition
2178 (cp_parser *);
2179 static void cp_parser_namespace_body
2180 (cp_parser *);
2181 static tree cp_parser_qualified_namespace_specifier
2182 (cp_parser *);
2183 static void cp_parser_namespace_alias_definition
2184 (cp_parser *);
2185 static bool cp_parser_using_declaration
2186 (cp_parser *, bool);
2187 static void cp_parser_using_directive
2188 (cp_parser *);
2189 static tree cp_parser_alias_declaration
2190 (cp_parser *);
2191 static void cp_parser_asm_definition
2192 (cp_parser *);
2193 static void cp_parser_linkage_specification
2194 (cp_parser *);
2195 static void cp_parser_static_assert
2196 (cp_parser *, bool);
2197 static tree cp_parser_decltype
2198 (cp_parser *);
2199 static tree cp_parser_decomposition_declaration
2200 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2202 /* Declarators [gram.dcl.decl] */
2204 static tree cp_parser_init_declarator
2205 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2206 bool, bool, int, bool *, tree *, location_t *, tree *);
2207 static cp_declarator *cp_parser_declarator
2208 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2209 static cp_declarator *cp_parser_direct_declarator
2210 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2211 static enum tree_code cp_parser_ptr_operator
2212 (cp_parser *, tree *, cp_cv_quals *, tree *);
2213 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2214 (cp_parser *);
2215 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2216 (cp_parser *);
2217 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2218 (cp_parser *);
2219 static tree cp_parser_tx_qualifier_opt
2220 (cp_parser *);
2221 static tree cp_parser_late_return_type_opt
2222 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2223 static tree cp_parser_declarator_id
2224 (cp_parser *, bool);
2225 static tree cp_parser_type_id
2226 (cp_parser *);
2227 static tree cp_parser_template_type_arg
2228 (cp_parser *);
2229 static tree cp_parser_trailing_type_id (cp_parser *);
2230 static tree cp_parser_type_id_1
2231 (cp_parser *, bool, bool);
2232 static void cp_parser_type_specifier_seq
2233 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2234 static tree cp_parser_parameter_declaration_clause
2235 (cp_parser *);
2236 static tree cp_parser_parameter_declaration_list
2237 (cp_parser *, bool *);
2238 static cp_parameter_declarator *cp_parser_parameter_declaration
2239 (cp_parser *, bool, bool *);
2240 static tree cp_parser_default_argument
2241 (cp_parser *, bool);
2242 static void cp_parser_function_body
2243 (cp_parser *, bool);
2244 static tree cp_parser_initializer
2245 (cp_parser *, bool *, bool *);
2246 static cp_expr cp_parser_initializer_clause
2247 (cp_parser *, bool *);
2248 static cp_expr cp_parser_braced_list
2249 (cp_parser*, bool*);
2250 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2251 (cp_parser *, bool *);
2253 static void cp_parser_ctor_initializer_opt_and_function_body
2254 (cp_parser *, bool);
2256 static tree cp_parser_late_parsing_omp_declare_simd
2257 (cp_parser *, tree);
2259 static tree cp_parser_late_parsing_oacc_routine
2260 (cp_parser *, tree);
2262 static tree synthesize_implicit_template_parm
2263 (cp_parser *, tree);
2264 static tree finish_fully_implicit_template
2265 (cp_parser *, tree);
2267 /* Classes [gram.class] */
2269 static tree cp_parser_class_name
2270 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2271 static tree cp_parser_class_specifier
2272 (cp_parser *);
2273 static tree cp_parser_class_head
2274 (cp_parser *, bool *);
2275 static enum tag_types cp_parser_class_key
2276 (cp_parser *);
2277 static void cp_parser_type_parameter_key
2278 (cp_parser* parser);
2279 static void cp_parser_member_specification_opt
2280 (cp_parser *);
2281 static void cp_parser_member_declaration
2282 (cp_parser *);
2283 static tree cp_parser_pure_specifier
2284 (cp_parser *);
2285 static tree cp_parser_constant_initializer
2286 (cp_parser *);
2288 /* Derived classes [gram.class.derived] */
2290 static tree cp_parser_base_clause
2291 (cp_parser *);
2292 static tree cp_parser_base_specifier
2293 (cp_parser *);
2295 /* Special member functions [gram.special] */
2297 static tree cp_parser_conversion_function_id
2298 (cp_parser *);
2299 static tree cp_parser_conversion_type_id
2300 (cp_parser *);
2301 static cp_declarator *cp_parser_conversion_declarator_opt
2302 (cp_parser *);
2303 static void cp_parser_ctor_initializer_opt
2304 (cp_parser *);
2305 static void cp_parser_mem_initializer_list
2306 (cp_parser *);
2307 static tree cp_parser_mem_initializer
2308 (cp_parser *);
2309 static tree cp_parser_mem_initializer_id
2310 (cp_parser *);
2312 /* Overloading [gram.over] */
2314 static cp_expr cp_parser_operator_function_id
2315 (cp_parser *);
2316 static cp_expr cp_parser_operator
2317 (cp_parser *);
2319 /* Templates [gram.temp] */
2321 static void cp_parser_template_declaration
2322 (cp_parser *, bool);
2323 static tree cp_parser_template_parameter_list
2324 (cp_parser *);
2325 static tree cp_parser_template_parameter
2326 (cp_parser *, bool *, bool *);
2327 static tree cp_parser_type_parameter
2328 (cp_parser *, bool *);
2329 static tree cp_parser_template_id
2330 (cp_parser *, bool, bool, enum tag_types, bool);
2331 static tree cp_parser_template_name
2332 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2333 static tree cp_parser_template_argument_list
2334 (cp_parser *);
2335 static tree cp_parser_template_argument
2336 (cp_parser *);
2337 static void cp_parser_explicit_instantiation
2338 (cp_parser *);
2339 static void cp_parser_explicit_specialization
2340 (cp_parser *);
2342 /* Exception handling [gram.exception] */
2344 static tree cp_parser_try_block
2345 (cp_parser *);
2346 static void cp_parser_function_try_block
2347 (cp_parser *);
2348 static void cp_parser_handler_seq
2349 (cp_parser *);
2350 static void cp_parser_handler
2351 (cp_parser *);
2352 static tree cp_parser_exception_declaration
2353 (cp_parser *);
2354 static tree cp_parser_throw_expression
2355 (cp_parser *);
2356 static tree cp_parser_exception_specification_opt
2357 (cp_parser *);
2358 static tree cp_parser_type_id_list
2359 (cp_parser *);
2361 /* GNU Extensions */
2363 static tree cp_parser_asm_specification_opt
2364 (cp_parser *);
2365 static tree cp_parser_asm_operand_list
2366 (cp_parser *);
2367 static tree cp_parser_asm_clobber_list
2368 (cp_parser *);
2369 static tree cp_parser_asm_label_list
2370 (cp_parser *);
2371 static bool cp_next_tokens_can_be_attribute_p
2372 (cp_parser *);
2373 static bool cp_next_tokens_can_be_gnu_attribute_p
2374 (cp_parser *);
2375 static bool cp_next_tokens_can_be_std_attribute_p
2376 (cp_parser *);
2377 static bool cp_nth_tokens_can_be_std_attribute_p
2378 (cp_parser *, size_t);
2379 static bool cp_nth_tokens_can_be_gnu_attribute_p
2380 (cp_parser *, size_t);
2381 static bool cp_nth_tokens_can_be_attribute_p
2382 (cp_parser *, size_t);
2383 static tree cp_parser_attributes_opt
2384 (cp_parser *);
2385 static tree cp_parser_gnu_attributes_opt
2386 (cp_parser *);
2387 static tree cp_parser_gnu_attribute_list
2388 (cp_parser *);
2389 static tree cp_parser_std_attribute
2390 (cp_parser *, tree);
2391 static tree cp_parser_std_attribute_spec
2392 (cp_parser *);
2393 static tree cp_parser_std_attribute_spec_seq
2394 (cp_parser *);
2395 static bool cp_parser_extension_opt
2396 (cp_parser *, int *);
2397 static void cp_parser_label_declaration
2398 (cp_parser *);
2400 /* Concept Extensions */
2402 static tree cp_parser_requires_clause
2403 (cp_parser *);
2404 static tree cp_parser_requires_clause_opt
2405 (cp_parser *);
2406 static tree cp_parser_requires_expression
2407 (cp_parser *);
2408 static tree cp_parser_requirement_parameter_list
2409 (cp_parser *);
2410 static tree cp_parser_requirement_body
2411 (cp_parser *);
2412 static tree cp_parser_requirement_list
2413 (cp_parser *);
2414 static tree cp_parser_requirement
2415 (cp_parser *);
2416 static tree cp_parser_simple_requirement
2417 (cp_parser *);
2418 static tree cp_parser_compound_requirement
2419 (cp_parser *);
2420 static tree cp_parser_type_requirement
2421 (cp_parser *);
2422 static tree cp_parser_nested_requirement
2423 (cp_parser *);
2425 /* Transactional Memory Extensions */
2427 static tree cp_parser_transaction
2428 (cp_parser *, cp_token *);
2429 static tree cp_parser_transaction_expression
2430 (cp_parser *, enum rid);
2431 static void cp_parser_function_transaction
2432 (cp_parser *, enum rid);
2433 static tree cp_parser_transaction_cancel
2434 (cp_parser *);
2436 enum pragma_context {
2437 pragma_external,
2438 pragma_member,
2439 pragma_objc_icode,
2440 pragma_stmt,
2441 pragma_compound
2443 static bool cp_parser_pragma
2444 (cp_parser *, enum pragma_context, bool *);
2446 /* Objective-C++ Productions */
2448 static tree cp_parser_objc_message_receiver
2449 (cp_parser *);
2450 static tree cp_parser_objc_message_args
2451 (cp_parser *);
2452 static tree cp_parser_objc_message_expression
2453 (cp_parser *);
2454 static cp_expr cp_parser_objc_encode_expression
2455 (cp_parser *);
2456 static tree cp_parser_objc_defs_expression
2457 (cp_parser *);
2458 static tree cp_parser_objc_protocol_expression
2459 (cp_parser *);
2460 static tree cp_parser_objc_selector_expression
2461 (cp_parser *);
2462 static cp_expr cp_parser_objc_expression
2463 (cp_parser *);
2464 static bool cp_parser_objc_selector_p
2465 (enum cpp_ttype);
2466 static tree cp_parser_objc_selector
2467 (cp_parser *);
2468 static tree cp_parser_objc_protocol_refs_opt
2469 (cp_parser *);
2470 static void cp_parser_objc_declaration
2471 (cp_parser *, tree);
2472 static tree cp_parser_objc_statement
2473 (cp_parser *);
2474 static bool cp_parser_objc_valid_prefix_attributes
2475 (cp_parser *, tree *);
2476 static void cp_parser_objc_at_property_declaration
2477 (cp_parser *) ;
2478 static void cp_parser_objc_at_synthesize_declaration
2479 (cp_parser *) ;
2480 static void cp_parser_objc_at_dynamic_declaration
2481 (cp_parser *) ;
2482 static tree cp_parser_objc_struct_declaration
2483 (cp_parser *) ;
2485 /* Utility Routines */
2487 static cp_expr cp_parser_lookup_name
2488 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2489 static tree cp_parser_lookup_name_simple
2490 (cp_parser *, tree, location_t);
2491 static tree cp_parser_maybe_treat_template_as_class
2492 (tree, bool);
2493 static bool cp_parser_check_declarator_template_parameters
2494 (cp_parser *, cp_declarator *, location_t);
2495 static bool cp_parser_check_template_parameters
2496 (cp_parser *, unsigned, location_t, cp_declarator *);
2497 static cp_expr cp_parser_simple_cast_expression
2498 (cp_parser *);
2499 static tree cp_parser_global_scope_opt
2500 (cp_parser *, bool);
2501 static bool cp_parser_constructor_declarator_p
2502 (cp_parser *, bool);
2503 static tree cp_parser_function_definition_from_specifiers_and_declarator
2504 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2505 static tree cp_parser_function_definition_after_declarator
2506 (cp_parser *, bool);
2507 static bool cp_parser_template_declaration_after_export
2508 (cp_parser *, bool);
2509 static void cp_parser_perform_template_parameter_access_checks
2510 (vec<deferred_access_check, va_gc> *);
2511 static tree cp_parser_single_declaration
2512 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2513 static cp_expr cp_parser_functional_cast
2514 (cp_parser *, tree);
2515 static tree cp_parser_save_member_function_body
2516 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2517 static tree cp_parser_save_nsdmi
2518 (cp_parser *);
2519 static tree cp_parser_enclosed_template_argument_list
2520 (cp_parser *);
2521 static void cp_parser_save_default_args
2522 (cp_parser *, tree);
2523 static void cp_parser_late_parsing_for_member
2524 (cp_parser *, tree);
2525 static tree cp_parser_late_parse_one_default_arg
2526 (cp_parser *, tree, tree, tree);
2527 static void cp_parser_late_parsing_nsdmi
2528 (cp_parser *, tree);
2529 static void cp_parser_late_parsing_default_args
2530 (cp_parser *, tree);
2531 static tree cp_parser_sizeof_operand
2532 (cp_parser *, enum rid);
2533 static cp_expr cp_parser_trait_expr
2534 (cp_parser *, enum rid);
2535 static bool cp_parser_declares_only_class_p
2536 (cp_parser *);
2537 static void cp_parser_set_storage_class
2538 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2539 static void cp_parser_set_decl_spec_type
2540 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2541 static void set_and_check_decl_spec_loc
2542 (cp_decl_specifier_seq *decl_specs,
2543 cp_decl_spec ds, cp_token *);
2544 static bool cp_parser_friend_p
2545 (const cp_decl_specifier_seq *);
2546 static void cp_parser_required_error
2547 (cp_parser *, required_token, bool, location_t);
2548 static cp_token *cp_parser_require
2549 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2550 static cp_token *cp_parser_require_keyword
2551 (cp_parser *, enum rid, required_token);
2552 static bool cp_parser_token_starts_function_definition_p
2553 (cp_token *);
2554 static bool cp_parser_next_token_starts_class_definition_p
2555 (cp_parser *);
2556 static bool cp_parser_next_token_ends_template_argument_p
2557 (cp_parser *);
2558 static bool cp_parser_nth_token_starts_template_argument_list_p
2559 (cp_parser *, size_t);
2560 static enum tag_types cp_parser_token_is_class_key
2561 (cp_token *);
2562 static enum tag_types cp_parser_token_is_type_parameter_key
2563 (cp_token *);
2564 static void cp_parser_check_class_key
2565 (enum tag_types, tree type);
2566 static void cp_parser_check_access_in_redeclaration
2567 (tree type, location_t location);
2568 static bool cp_parser_optional_template_keyword
2569 (cp_parser *);
2570 static void cp_parser_pre_parsed_nested_name_specifier
2571 (cp_parser *);
2572 static bool cp_parser_cache_group
2573 (cp_parser *, enum cpp_ttype, unsigned);
2574 static tree cp_parser_cache_defarg
2575 (cp_parser *parser, bool nsdmi);
2576 static void cp_parser_parse_tentatively
2577 (cp_parser *);
2578 static void cp_parser_commit_to_tentative_parse
2579 (cp_parser *);
2580 static void cp_parser_commit_to_topmost_tentative_parse
2581 (cp_parser *);
2582 static void cp_parser_abort_tentative_parse
2583 (cp_parser *);
2584 static bool cp_parser_parse_definitely
2585 (cp_parser *);
2586 static inline bool cp_parser_parsing_tentatively
2587 (cp_parser *);
2588 static bool cp_parser_uncommitted_to_tentative_parse_p
2589 (cp_parser *);
2590 static void cp_parser_error
2591 (cp_parser *, const char *);
2592 static void cp_parser_name_lookup_error
2593 (cp_parser *, tree, tree, name_lookup_error, location_t);
2594 static bool cp_parser_simulate_error
2595 (cp_parser *);
2596 static bool cp_parser_check_type_definition
2597 (cp_parser *);
2598 static void cp_parser_check_for_definition_in_return_type
2599 (cp_declarator *, tree, location_t type_location);
2600 static void cp_parser_check_for_invalid_template_id
2601 (cp_parser *, tree, enum tag_types, location_t location);
2602 static bool cp_parser_non_integral_constant_expression
2603 (cp_parser *, non_integral_constant);
2604 static void cp_parser_diagnose_invalid_type_name
2605 (cp_parser *, tree, location_t);
2606 static bool cp_parser_parse_and_diagnose_invalid_type_name
2607 (cp_parser *);
2608 static int cp_parser_skip_to_closing_parenthesis
2609 (cp_parser *, bool, bool, bool);
2610 static void cp_parser_skip_to_end_of_statement
2611 (cp_parser *);
2612 static void cp_parser_consume_semicolon_at_end_of_statement
2613 (cp_parser *);
2614 static void cp_parser_skip_to_end_of_block_or_statement
2615 (cp_parser *);
2616 static bool cp_parser_skip_to_closing_brace
2617 (cp_parser *);
2618 static void cp_parser_skip_to_end_of_template_parameter_list
2619 (cp_parser *);
2620 static void cp_parser_skip_to_pragma_eol
2621 (cp_parser*, cp_token *);
2622 static bool cp_parser_error_occurred
2623 (cp_parser *);
2624 static bool cp_parser_allow_gnu_extensions_p
2625 (cp_parser *);
2626 static bool cp_parser_is_pure_string_literal
2627 (cp_token *);
2628 static bool cp_parser_is_string_literal
2629 (cp_token *);
2630 static bool cp_parser_is_keyword
2631 (cp_token *, enum rid);
2632 static tree cp_parser_make_typename_type
2633 (cp_parser *, tree, location_t location);
2634 static cp_declarator * cp_parser_make_indirect_declarator
2635 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2636 static bool cp_parser_compound_literal_p
2637 (cp_parser *);
2638 static bool cp_parser_array_designator_p
2639 (cp_parser *);
2640 static bool cp_parser_init_statement_p
2641 (cp_parser *);
2642 static bool cp_parser_skip_to_closing_square_bracket
2643 (cp_parser *);
2645 /* Concept-related syntactic transformations */
2647 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2648 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2650 // -------------------------------------------------------------------------- //
2651 // Unevaluated Operand Guard
2653 // Implementation of an RAII helper for unevaluated operand parsing.
2654 cp_unevaluated::cp_unevaluated ()
2656 ++cp_unevaluated_operand;
2657 ++c_inhibit_evaluation_warnings;
2660 cp_unevaluated::~cp_unevaluated ()
2662 --c_inhibit_evaluation_warnings;
2663 --cp_unevaluated_operand;
2666 // -------------------------------------------------------------------------- //
2667 // Tentative Parsing
2669 /* Returns nonzero if we are parsing tentatively. */
2671 static inline bool
2672 cp_parser_parsing_tentatively (cp_parser* parser)
2674 return parser->context->next != NULL;
2677 /* Returns nonzero if TOKEN is a string literal. */
2679 static bool
2680 cp_parser_is_pure_string_literal (cp_token* token)
2682 return (token->type == CPP_STRING ||
2683 token->type == CPP_STRING16 ||
2684 token->type == CPP_STRING32 ||
2685 token->type == CPP_WSTRING ||
2686 token->type == CPP_UTF8STRING);
2689 /* Returns nonzero if TOKEN is a string literal
2690 of a user-defined string literal. */
2692 static bool
2693 cp_parser_is_string_literal (cp_token* token)
2695 return (cp_parser_is_pure_string_literal (token) ||
2696 token->type == CPP_STRING_USERDEF ||
2697 token->type == CPP_STRING16_USERDEF ||
2698 token->type == CPP_STRING32_USERDEF ||
2699 token->type == CPP_WSTRING_USERDEF ||
2700 token->type == CPP_UTF8STRING_USERDEF);
2703 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2705 static bool
2706 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2708 return token->keyword == keyword;
2711 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2712 PRAGMA_NONE. */
2714 static enum pragma_kind
2715 cp_parser_pragma_kind (cp_token *token)
2717 if (token->type != CPP_PRAGMA)
2718 return PRAGMA_NONE;
2719 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2720 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2723 /* Helper function for cp_parser_error.
2724 Having peeked a token of kind TOK1_KIND that might signify
2725 a conflict marker, peek successor tokens to determine
2726 if we actually do have a conflict marker.
2727 Specifically, we consider a run of 7 '<', '=' or '>' characters
2728 at the start of a line as a conflict marker.
2729 These come through the lexer as three pairs and a single,
2730 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2731 If it returns true, *OUT_LOC is written to with the location/range
2732 of the marker. */
2734 static bool
2735 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2736 location_t *out_loc)
2738 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2739 if (token2->type != tok1_kind)
2740 return false;
2741 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2742 if (token3->type != tok1_kind)
2743 return false;
2744 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2745 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2746 return false;
2748 /* It must be at the start of the line. */
2749 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2750 if (LOCATION_COLUMN (start_loc) != 1)
2751 return false;
2753 /* We have a conflict marker. Construct a location of the form:
2754 <<<<<<<
2755 ^~~~~~~
2756 with start == caret, finishing at the end of the marker. */
2757 location_t finish_loc = get_finish (token4->location);
2758 *out_loc = make_location (start_loc, start_loc, finish_loc);
2760 return true;
2763 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2764 RT_CLOSE_PAREN. */
2766 static const char *
2767 get_matching_symbol (required_token token_desc)
2769 switch (token_desc)
2771 default:
2772 gcc_unreachable ();
2773 return "";
2774 case RT_CLOSE_BRACE:
2775 return "{";
2776 case RT_CLOSE_PAREN:
2777 return "(";
2781 /* Attempt to convert TOKEN_DESC from a required_token to an
2782 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2784 static enum cpp_ttype
2785 get_required_cpp_ttype (required_token token_desc)
2787 switch (token_desc)
2789 case RT_SEMICOLON:
2790 return CPP_SEMICOLON;
2791 case RT_OPEN_PAREN:
2792 return CPP_OPEN_PAREN;
2793 case RT_CLOSE_BRACE:
2794 return CPP_CLOSE_BRACE;
2795 case RT_OPEN_BRACE:
2796 return CPP_OPEN_BRACE;
2797 case RT_CLOSE_SQUARE:
2798 return CPP_CLOSE_SQUARE;
2799 case RT_OPEN_SQUARE:
2800 return CPP_OPEN_SQUARE;
2801 case RT_COMMA:
2802 return CPP_COMMA;
2803 case RT_COLON:
2804 return CPP_COLON;
2805 case RT_CLOSE_PAREN:
2806 return CPP_CLOSE_PAREN;
2808 default:
2809 /* Use CPP_EOF as a "no completions possible" code. */
2810 return CPP_EOF;
2815 /* Subroutine of cp_parser_error and cp_parser_required_error.
2817 Issue a diagnostic of the form
2818 FILE:LINE: MESSAGE before TOKEN
2819 where TOKEN is the next token in the input stream. MESSAGE
2820 (specified by the caller) is usually of the form "expected
2821 OTHER-TOKEN".
2823 This bypasses the check for tentative passing, and potentially
2824 adds material needed by cp_parser_required_error.
2826 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2827 suggesting insertion of the missing token.
2829 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2830 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2831 location. */
2833 static void
2834 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2835 required_token missing_token_desc,
2836 location_t matching_location)
2838 cp_token *token = cp_lexer_peek_token (parser->lexer);
2839 /* This diagnostic makes more sense if it is tagged to the line
2840 of the token we just peeked at. */
2841 cp_lexer_set_source_position_from_token (token);
2843 if (token->type == CPP_PRAGMA)
2845 error_at (token->location,
2846 "%<#pragma%> is not allowed here");
2847 cp_parser_skip_to_pragma_eol (parser, token);
2848 return;
2851 /* If this is actually a conflict marker, report it as such. */
2852 if (token->type == CPP_LSHIFT
2853 || token->type == CPP_RSHIFT
2854 || token->type == CPP_EQ_EQ)
2856 location_t loc;
2857 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2859 error_at (loc, "version control conflict marker in file");
2860 return;
2864 gcc_rich_location richloc (input_location);
2866 bool added_matching_location = false;
2868 if (missing_token_desc != RT_NONE)
2870 /* Potentially supply a fix-it hint, suggesting to add the
2871 missing token immediately after the *previous* token.
2872 This may move the primary location within richloc. */
2873 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2874 location_t prev_token_loc
2875 = cp_lexer_previous_token (parser->lexer)->location;
2876 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2878 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2879 Attempt to consolidate diagnostics by printing it as a
2880 secondary range within the main diagnostic. */
2881 if (matching_location != UNKNOWN_LOCATION)
2882 added_matching_location
2883 = richloc.add_location_if_nearby (matching_location);
2886 /* Actually emit the error. */
2887 c_parse_error (gmsgid,
2888 /* Because c_parser_error does not understand
2889 CPP_KEYWORD, keywords are treated like
2890 identifiers. */
2891 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2892 token->u.value, token->flags, &richloc);
2894 if (missing_token_desc != RT_NONE)
2896 /* If we weren't able to consolidate matching_location, then
2897 print it as a secondary diagnostic. */
2898 if (matching_location != UNKNOWN_LOCATION
2899 && !added_matching_location)
2900 inform (matching_location, "to match this %qs",
2901 get_matching_symbol (missing_token_desc));
2905 /* If not parsing tentatively, issue a diagnostic of the form
2906 FILE:LINE: MESSAGE before TOKEN
2907 where TOKEN is the next token in the input stream. MESSAGE
2908 (specified by the caller) is usually of the form "expected
2909 OTHER-TOKEN". */
2911 static void
2912 cp_parser_error (cp_parser* parser, const char* gmsgid)
2914 if (!cp_parser_simulate_error (parser))
2915 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2918 /* Issue an error about name-lookup failing. NAME is the
2919 IDENTIFIER_NODE DECL is the result of
2920 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2921 the thing that we hoped to find. */
2923 static void
2924 cp_parser_name_lookup_error (cp_parser* parser,
2925 tree name,
2926 tree decl,
2927 name_lookup_error desired,
2928 location_t location)
2930 /* If name lookup completely failed, tell the user that NAME was not
2931 declared. */
2932 if (decl == error_mark_node)
2934 if (parser->scope && parser->scope != global_namespace)
2935 error_at (location, "%<%E::%E%> has not been declared",
2936 parser->scope, name);
2937 else if (parser->scope == global_namespace)
2938 error_at (location, "%<::%E%> has not been declared", name);
2939 else if (parser->object_scope
2940 && !CLASS_TYPE_P (parser->object_scope))
2941 error_at (location, "request for member %qE in non-class type %qT",
2942 name, parser->object_scope);
2943 else if (parser->object_scope)
2944 error_at (location, "%<%T::%E%> has not been declared",
2945 parser->object_scope, name);
2946 else
2947 error_at (location, "%qE has not been declared", name);
2949 else if (parser->scope && parser->scope != global_namespace)
2951 switch (desired)
2953 case NLE_TYPE:
2954 error_at (location, "%<%E::%E%> is not a type",
2955 parser->scope, name);
2956 break;
2957 case NLE_CXX98:
2958 error_at (location, "%<%E::%E%> is not a class or namespace",
2959 parser->scope, name);
2960 break;
2961 case NLE_NOT_CXX98:
2962 error_at (location,
2963 "%<%E::%E%> is not a class, namespace, or enumeration",
2964 parser->scope, name);
2965 break;
2966 default:
2967 gcc_unreachable ();
2971 else if (parser->scope == global_namespace)
2973 switch (desired)
2975 case NLE_TYPE:
2976 error_at (location, "%<::%E%> is not a type", name);
2977 break;
2978 case NLE_CXX98:
2979 error_at (location, "%<::%E%> is not a class or namespace", name);
2980 break;
2981 case NLE_NOT_CXX98:
2982 error_at (location,
2983 "%<::%E%> is not a class, namespace, or enumeration",
2984 name);
2985 break;
2986 default:
2987 gcc_unreachable ();
2990 else
2992 switch (desired)
2994 case NLE_TYPE:
2995 error_at (location, "%qE is not a type", name);
2996 break;
2997 case NLE_CXX98:
2998 error_at (location, "%qE is not a class or namespace", name);
2999 break;
3000 case NLE_NOT_CXX98:
3001 error_at (location,
3002 "%qE is not a class, namespace, or enumeration", name);
3003 break;
3004 default:
3005 gcc_unreachable ();
3010 /* If we are parsing tentatively, remember that an error has occurred
3011 during this tentative parse. Returns true if the error was
3012 simulated; false if a message should be issued by the caller. */
3014 static bool
3015 cp_parser_simulate_error (cp_parser* parser)
3017 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3019 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3020 return true;
3022 return false;
3025 /* This function is called when a type is defined. If type
3026 definitions are forbidden at this point, an error message is
3027 issued. */
3029 static bool
3030 cp_parser_check_type_definition (cp_parser* parser)
3032 /* If types are forbidden here, issue a message. */
3033 if (parser->type_definition_forbidden_message)
3035 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3036 in the message need to be interpreted. */
3037 error (parser->type_definition_forbidden_message);
3038 return false;
3040 return true;
3043 /* This function is called when the DECLARATOR is processed. The TYPE
3044 was a type defined in the decl-specifiers. If it is invalid to
3045 define a type in the decl-specifiers for DECLARATOR, an error is
3046 issued. TYPE_LOCATION is the location of TYPE and is used
3047 for error reporting. */
3049 static void
3050 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3051 tree type, location_t type_location)
3053 /* [dcl.fct] forbids type definitions in return types.
3054 Unfortunately, it's not easy to know whether or not we are
3055 processing a return type until after the fact. */
3056 while (declarator
3057 && (declarator->kind == cdk_pointer
3058 || declarator->kind == cdk_reference
3059 || declarator->kind == cdk_ptrmem))
3060 declarator = declarator->declarator;
3061 if (declarator
3062 && declarator->kind == cdk_function)
3064 error_at (type_location,
3065 "new types may not be defined in a return type");
3066 inform (type_location,
3067 "(perhaps a semicolon is missing after the definition of %qT)",
3068 type);
3072 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3073 "<" in any valid C++ program. If the next token is indeed "<",
3074 issue a message warning the user about what appears to be an
3075 invalid attempt to form a template-id. LOCATION is the location
3076 of the type-specifier (TYPE) */
3078 static void
3079 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3080 tree type,
3081 enum tag_types tag_type,
3082 location_t location)
3084 cp_token_position start = 0;
3086 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3088 if (TREE_CODE (type) == TYPE_DECL)
3089 type = TREE_TYPE (type);
3090 if (TYPE_P (type) && !template_placeholder_p (type))
3091 error_at (location, "%qT is not a template", type);
3092 else if (identifier_p (type))
3094 if (tag_type != none_type)
3095 error_at (location, "%qE is not a class template", type);
3096 else
3097 error_at (location, "%qE is not a template", type);
3099 else
3100 error_at (location, "invalid template-id");
3101 /* Remember the location of the invalid "<". */
3102 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3103 start = cp_lexer_token_position (parser->lexer, true);
3104 /* Consume the "<". */
3105 cp_lexer_consume_token (parser->lexer);
3106 /* Parse the template arguments. */
3107 cp_parser_enclosed_template_argument_list (parser);
3108 /* Permanently remove the invalid template arguments so that
3109 this error message is not issued again. */
3110 if (start)
3111 cp_lexer_purge_tokens_after (parser->lexer, start);
3115 /* If parsing an integral constant-expression, issue an error message
3116 about the fact that THING appeared and return true. Otherwise,
3117 return false. In either case, set
3118 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3120 static bool
3121 cp_parser_non_integral_constant_expression (cp_parser *parser,
3122 non_integral_constant thing)
3124 parser->non_integral_constant_expression_p = true;
3125 if (parser->integral_constant_expression_p)
3127 if (!parser->allow_non_integral_constant_expression_p)
3129 const char *msg = NULL;
3130 switch (thing)
3132 case NIC_FLOAT:
3133 pedwarn (input_location, OPT_Wpedantic,
3134 "ISO C++ forbids using a floating-point literal "
3135 "in a constant-expression");
3136 return true;
3137 case NIC_CAST:
3138 error ("a cast to a type other than an integral or "
3139 "enumeration type cannot appear in a "
3140 "constant-expression");
3141 return true;
3142 case NIC_TYPEID:
3143 error ("%<typeid%> operator "
3144 "cannot appear in a constant-expression");
3145 return true;
3146 case NIC_NCC:
3147 error ("non-constant compound literals "
3148 "cannot appear in a constant-expression");
3149 return true;
3150 case NIC_FUNC_CALL:
3151 error ("a function call "
3152 "cannot appear in a constant-expression");
3153 return true;
3154 case NIC_INC:
3155 error ("an increment "
3156 "cannot appear in a constant-expression");
3157 return true;
3158 case NIC_DEC:
3159 error ("an decrement "
3160 "cannot appear in a constant-expression");
3161 return true;
3162 case NIC_ARRAY_REF:
3163 error ("an array reference "
3164 "cannot appear in a constant-expression");
3165 return true;
3166 case NIC_ADDR_LABEL:
3167 error ("the address of a label "
3168 "cannot appear in a constant-expression");
3169 return true;
3170 case NIC_OVERLOADED:
3171 error ("calls to overloaded operators "
3172 "cannot appear in a constant-expression");
3173 return true;
3174 case NIC_ASSIGNMENT:
3175 error ("an assignment cannot appear in a constant-expression");
3176 return true;
3177 case NIC_COMMA:
3178 error ("a comma operator "
3179 "cannot appear in a constant-expression");
3180 return true;
3181 case NIC_CONSTRUCTOR:
3182 error ("a call to a constructor "
3183 "cannot appear in a constant-expression");
3184 return true;
3185 case NIC_TRANSACTION:
3186 error ("a transaction expression "
3187 "cannot appear in a constant-expression");
3188 return true;
3189 case NIC_THIS:
3190 msg = "this";
3191 break;
3192 case NIC_FUNC_NAME:
3193 msg = "__FUNCTION__";
3194 break;
3195 case NIC_PRETTY_FUNC:
3196 msg = "__PRETTY_FUNCTION__";
3197 break;
3198 case NIC_C99_FUNC:
3199 msg = "__func__";
3200 break;
3201 case NIC_VA_ARG:
3202 msg = "va_arg";
3203 break;
3204 case NIC_ARROW:
3205 msg = "->";
3206 break;
3207 case NIC_POINT:
3208 msg = ".";
3209 break;
3210 case NIC_STAR:
3211 msg = "*";
3212 break;
3213 case NIC_ADDR:
3214 msg = "&";
3215 break;
3216 case NIC_PREINCREMENT:
3217 msg = "++";
3218 break;
3219 case NIC_PREDECREMENT:
3220 msg = "--";
3221 break;
3222 case NIC_NEW:
3223 msg = "new";
3224 break;
3225 case NIC_DEL:
3226 msg = "delete";
3227 break;
3228 default:
3229 gcc_unreachable ();
3231 if (msg)
3232 error ("%qs cannot appear in a constant-expression", msg);
3233 return true;
3236 return false;
3239 /* Emit a diagnostic for an invalid type name. This function commits
3240 to the current active tentative parse, if any. (Otherwise, the
3241 problematic construct might be encountered again later, resulting
3242 in duplicate error messages.) LOCATION is the location of ID. */
3244 static void
3245 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3246 location_t location)
3248 tree decl, ambiguous_decls;
3249 cp_parser_commit_to_tentative_parse (parser);
3250 /* Try to lookup the identifier. */
3251 decl = cp_parser_lookup_name (parser, id, none_type,
3252 /*is_template=*/false,
3253 /*is_namespace=*/false,
3254 /*check_dependency=*/true,
3255 &ambiguous_decls, location);
3256 if (ambiguous_decls)
3257 /* If the lookup was ambiguous, an error will already have
3258 been issued. */
3259 return;
3260 /* If the lookup found a template-name, it means that the user forgot
3261 to specify an argument list. Emit a useful error message. */
3262 if (DECL_TYPE_TEMPLATE_P (decl))
3264 error_at (location,
3265 "invalid use of template-name %qE without an argument list",
3266 decl);
3267 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3268 inform (location, "class template argument deduction is only available "
3269 "with -std=c++17 or -std=gnu++17");
3270 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3272 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3273 error_at (location, "invalid use of destructor %qD as a type", id);
3274 else if (TREE_CODE (decl) == TYPE_DECL)
3275 /* Something like 'unsigned A a;' */
3276 error_at (location, "invalid combination of multiple type-specifiers");
3277 else if (!parser->scope)
3279 /* Issue an error message. */
3280 name_hint hint;
3281 if (TREE_CODE (id) == IDENTIFIER_NODE)
3282 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3283 if (hint)
3285 gcc_rich_location richloc (location);
3286 richloc.add_fixit_replace (hint.suggestion ());
3287 error_at (&richloc,
3288 "%qE does not name a type; did you mean %qs?",
3289 id, hint.suggestion ());
3291 else
3292 error_at (location, "%qE does not name a type", id);
3293 /* If we're in a template class, it's possible that the user was
3294 referring to a type from a base class. For example:
3296 template <typename T> struct A { typedef T X; };
3297 template <typename T> struct B : public A<T> { X x; };
3299 The user should have said "typename A<T>::X". */
3300 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3301 inform (location, "C++11 %<constexpr%> only available with "
3302 "-std=c++11 or -std=gnu++11");
3303 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3304 inform (location, "C++11 %<noexcept%> only available with "
3305 "-std=c++11 or -std=gnu++11");
3306 else if (cxx_dialect < cxx11
3307 && TREE_CODE (id) == IDENTIFIER_NODE
3308 && id_equal (id, "thread_local"))
3309 inform (location, "C++11 %<thread_local%> only available with "
3310 "-std=c++11 or -std=gnu++11");
3311 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3312 inform (location, "%<concept%> only available with -fconcepts");
3313 else if (processing_template_decl && current_class_type
3314 && TYPE_BINFO (current_class_type))
3316 tree b;
3318 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3320 b = TREE_CHAIN (b))
3322 tree base_type = BINFO_TYPE (b);
3323 if (CLASS_TYPE_P (base_type)
3324 && dependent_type_p (base_type))
3326 tree field;
3327 /* Go from a particular instantiation of the
3328 template (which will have an empty TYPE_FIELDs),
3329 to the main version. */
3330 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3331 for (field = TYPE_FIELDS (base_type);
3332 field;
3333 field = DECL_CHAIN (field))
3334 if (TREE_CODE (field) == TYPE_DECL
3335 && DECL_NAME (field) == id)
3337 inform (location,
3338 "(perhaps %<typename %T::%E%> was intended)",
3339 BINFO_TYPE (b), id);
3340 break;
3342 if (field)
3343 break;
3348 /* Here we diagnose qualified-ids where the scope is actually correct,
3349 but the identifier does not resolve to a valid type name. */
3350 else if (parser->scope != error_mark_node)
3352 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3354 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3355 error_at (location_of (id),
3356 "%qE in namespace %qE does not name a template type",
3357 id, parser->scope);
3358 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3359 error_at (location_of (id),
3360 "%qE in namespace %qE does not name a template type",
3361 TREE_OPERAND (id, 0), parser->scope);
3362 else
3363 error_at (location_of (id),
3364 "%qE in namespace %qE does not name a type",
3365 id, parser->scope);
3366 if (DECL_P (decl))
3367 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3369 else if (CLASS_TYPE_P (parser->scope)
3370 && constructor_name_p (id, parser->scope))
3372 /* A<T>::A<T>() */
3373 error_at (location, "%<%T::%E%> names the constructor, not"
3374 " the type", parser->scope, id);
3375 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3376 error_at (location, "and %qT has no template constructors",
3377 parser->scope);
3379 else if (TYPE_P (parser->scope)
3380 && dependent_scope_p (parser->scope))
3382 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3383 error_at (location,
3384 "need %<typename%> before %<%T::%D::%E%> because "
3385 "%<%T::%D%> is a dependent scope",
3386 TYPE_CONTEXT (parser->scope),
3387 TYPENAME_TYPE_FULLNAME (parser->scope),
3389 TYPE_CONTEXT (parser->scope),
3390 TYPENAME_TYPE_FULLNAME (parser->scope));
3391 else
3392 error_at (location, "need %<typename%> before %<%T::%E%> because "
3393 "%qT is a dependent scope",
3394 parser->scope, id, parser->scope);
3396 else if (TYPE_P (parser->scope))
3398 if (!COMPLETE_TYPE_P (parser->scope))
3399 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3400 parser->scope);
3401 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3402 error_at (location_of (id),
3403 "%qE in %q#T does not name a template type",
3404 id, parser->scope);
3405 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3406 error_at (location_of (id),
3407 "%qE in %q#T does not name a template type",
3408 TREE_OPERAND (id, 0), parser->scope);
3409 else
3410 error_at (location_of (id),
3411 "%qE in %q#T does not name a type",
3412 id, parser->scope);
3413 if (DECL_P (decl))
3414 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3416 else
3417 gcc_unreachable ();
3421 /* Check for a common situation where a type-name should be present,
3422 but is not, and issue a sensible error message. Returns true if an
3423 invalid type-name was detected.
3425 The situation handled by this function are variable declarations of the
3426 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3427 Usually, `ID' should name a type, but if we got here it means that it
3428 does not. We try to emit the best possible error message depending on
3429 how exactly the id-expression looks like. */
3431 static bool
3432 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3434 tree id;
3435 cp_token *token = cp_lexer_peek_token (parser->lexer);
3437 /* Avoid duplicate error about ambiguous lookup. */
3438 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3440 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3441 if (next->type == CPP_NAME && next->error_reported)
3442 goto out;
3445 cp_parser_parse_tentatively (parser);
3446 id = cp_parser_id_expression (parser,
3447 /*template_keyword_p=*/false,
3448 /*check_dependency_p=*/true,
3449 /*template_p=*/NULL,
3450 /*declarator_p=*/true,
3451 /*optional_p=*/false);
3452 /* If the next token is a (, this is a function with no explicit return
3453 type, i.e. constructor, destructor or conversion op. */
3454 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3455 || TREE_CODE (id) == TYPE_DECL)
3457 cp_parser_abort_tentative_parse (parser);
3458 return false;
3460 if (!cp_parser_parse_definitely (parser))
3461 return false;
3463 /* Emit a diagnostic for the invalid type. */
3464 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3465 out:
3466 /* If we aren't in the middle of a declarator (i.e. in a
3467 parameter-declaration-clause), skip to the end of the declaration;
3468 there's no point in trying to process it. */
3469 if (!parser->in_declarator_p)
3470 cp_parser_skip_to_end_of_block_or_statement (parser);
3471 return true;
3474 /* Consume tokens up to, and including, the next non-nested closing `)'.
3475 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3476 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3477 found an unnested token of that type. */
3479 static int
3480 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3481 bool recovering,
3482 cpp_ttype or_ttype,
3483 bool consume_paren)
3485 unsigned paren_depth = 0;
3486 unsigned brace_depth = 0;
3487 unsigned square_depth = 0;
3489 if (recovering && or_ttype == CPP_EOF
3490 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3491 return 0;
3493 while (true)
3495 cp_token * token = cp_lexer_peek_token (parser->lexer);
3497 /* Have we found what we're looking for before the closing paren? */
3498 if (token->type == or_ttype && or_ttype != CPP_EOF
3499 && !brace_depth && !paren_depth && !square_depth)
3500 return -1;
3502 switch (token->type)
3504 case CPP_EOF:
3505 case CPP_PRAGMA_EOL:
3506 /* If we've run out of tokens, then there is no closing `)'. */
3507 return 0;
3509 /* This is good for lambda expression capture-lists. */
3510 case CPP_OPEN_SQUARE:
3511 ++square_depth;
3512 break;
3513 case CPP_CLOSE_SQUARE:
3514 if (!square_depth--)
3515 return 0;
3516 break;
3518 case CPP_SEMICOLON:
3519 /* This matches the processing in skip_to_end_of_statement. */
3520 if (!brace_depth)
3521 return 0;
3522 break;
3524 case CPP_OPEN_BRACE:
3525 ++brace_depth;
3526 break;
3527 case CPP_CLOSE_BRACE:
3528 if (!brace_depth--)
3529 return 0;
3530 break;
3532 case CPP_OPEN_PAREN:
3533 if (!brace_depth)
3534 ++paren_depth;
3535 break;
3537 case CPP_CLOSE_PAREN:
3538 if (!brace_depth && !paren_depth--)
3540 if (consume_paren)
3541 cp_lexer_consume_token (parser->lexer);
3542 return 1;
3544 break;
3546 default:
3547 break;
3550 /* Consume the token. */
3551 cp_lexer_consume_token (parser->lexer);
3555 /* Consume tokens up to, and including, the next non-nested closing `)'.
3556 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3557 are doing error recovery. Returns -1 if OR_COMMA is true and we
3558 found an unnested token of that type. */
3560 static int
3561 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3562 bool recovering,
3563 bool or_comma,
3564 bool consume_paren)
3566 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3567 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3568 ttype, consume_paren);
3571 /* Consume tokens until we reach the end of the current statement.
3572 Normally, that will be just before consuming a `;'. However, if a
3573 non-nested `}' comes first, then we stop before consuming that. */
3575 static void
3576 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3578 unsigned nesting_depth = 0;
3580 /* Unwind generic function template scope if necessary. */
3581 if (parser->fully_implicit_function_template_p)
3582 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3584 while (true)
3586 cp_token *token = cp_lexer_peek_token (parser->lexer);
3588 switch (token->type)
3590 case CPP_EOF:
3591 case CPP_PRAGMA_EOL:
3592 /* If we've run out of tokens, stop. */
3593 return;
3595 case CPP_SEMICOLON:
3596 /* If the next token is a `;', we have reached the end of the
3597 statement. */
3598 if (!nesting_depth)
3599 return;
3600 break;
3602 case CPP_CLOSE_BRACE:
3603 /* If this is a non-nested '}', stop before consuming it.
3604 That way, when confronted with something like:
3606 { 3 + }
3608 we stop before consuming the closing '}', even though we
3609 have not yet reached a `;'. */
3610 if (nesting_depth == 0)
3611 return;
3613 /* If it is the closing '}' for a block that we have
3614 scanned, stop -- but only after consuming the token.
3615 That way given:
3617 void f g () { ... }
3618 typedef int I;
3620 we will stop after the body of the erroneously declared
3621 function, but before consuming the following `typedef'
3622 declaration. */
3623 if (--nesting_depth == 0)
3625 cp_lexer_consume_token (parser->lexer);
3626 return;
3628 break;
3630 case CPP_OPEN_BRACE:
3631 ++nesting_depth;
3632 break;
3634 default:
3635 break;
3638 /* Consume the token. */
3639 cp_lexer_consume_token (parser->lexer);
3643 /* This function is called at the end of a statement or declaration.
3644 If the next token is a semicolon, it is consumed; otherwise, error
3645 recovery is attempted. */
3647 static void
3648 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3650 /* Look for the trailing `;'. */
3651 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3653 /* If there is additional (erroneous) input, skip to the end of
3654 the statement. */
3655 cp_parser_skip_to_end_of_statement (parser);
3656 /* If the next token is now a `;', consume it. */
3657 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3658 cp_lexer_consume_token (parser->lexer);
3662 /* Skip tokens until we have consumed an entire block, or until we
3663 have consumed a non-nested `;'. */
3665 static void
3666 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3668 int nesting_depth = 0;
3670 /* Unwind generic function template scope if necessary. */
3671 if (parser->fully_implicit_function_template_p)
3672 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3674 while (nesting_depth >= 0)
3676 cp_token *token = cp_lexer_peek_token (parser->lexer);
3678 switch (token->type)
3680 case CPP_EOF:
3681 case CPP_PRAGMA_EOL:
3682 /* If we've run out of tokens, stop. */
3683 return;
3685 case CPP_SEMICOLON:
3686 /* Stop if this is an unnested ';'. */
3687 if (!nesting_depth)
3688 nesting_depth = -1;
3689 break;
3691 case CPP_CLOSE_BRACE:
3692 /* Stop if this is an unnested '}', or closes the outermost
3693 nesting level. */
3694 nesting_depth--;
3695 if (nesting_depth < 0)
3696 return;
3697 if (!nesting_depth)
3698 nesting_depth = -1;
3699 break;
3701 case CPP_OPEN_BRACE:
3702 /* Nest. */
3703 nesting_depth++;
3704 break;
3706 default:
3707 break;
3710 /* Consume the token. */
3711 cp_lexer_consume_token (parser->lexer);
3715 /* Skip tokens until a non-nested closing curly brace is the next
3716 token, or there are no more tokens. Return true in the first case,
3717 false otherwise. */
3719 static bool
3720 cp_parser_skip_to_closing_brace (cp_parser *parser)
3722 unsigned nesting_depth = 0;
3724 while (true)
3726 cp_token *token = cp_lexer_peek_token (parser->lexer);
3728 switch (token->type)
3730 case CPP_EOF:
3731 case CPP_PRAGMA_EOL:
3732 /* If we've run out of tokens, stop. */
3733 return false;
3735 case CPP_CLOSE_BRACE:
3736 /* If the next token is a non-nested `}', then we have reached
3737 the end of the current block. */
3738 if (nesting_depth-- == 0)
3739 return true;
3740 break;
3742 case CPP_OPEN_BRACE:
3743 /* If it the next token is a `{', then we are entering a new
3744 block. Consume the entire block. */
3745 ++nesting_depth;
3746 break;
3748 default:
3749 break;
3752 /* Consume the token. */
3753 cp_lexer_consume_token (parser->lexer);
3757 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3758 parameter is the PRAGMA token, allowing us to purge the entire pragma
3759 sequence. */
3761 static void
3762 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3764 cp_token *token;
3766 parser->lexer->in_pragma = false;
3769 token = cp_lexer_consume_token (parser->lexer);
3770 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3772 /* Ensure that the pragma is not parsed again. */
3773 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3776 /* Require pragma end of line, resyncing with it as necessary. The
3777 arguments are as for cp_parser_skip_to_pragma_eol. */
3779 static void
3780 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3782 parser->lexer->in_pragma = false;
3783 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3784 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3787 /* This is a simple wrapper around make_typename_type. When the id is
3788 an unresolved identifier node, we can provide a superior diagnostic
3789 using cp_parser_diagnose_invalid_type_name. */
3791 static tree
3792 cp_parser_make_typename_type (cp_parser *parser, tree id,
3793 location_t id_location)
3795 tree result;
3796 if (identifier_p (id))
3798 result = make_typename_type (parser->scope, id, typename_type,
3799 /*complain=*/tf_none);
3800 if (result == error_mark_node)
3801 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3802 return result;
3804 return make_typename_type (parser->scope, id, typename_type, tf_error);
3807 /* This is a wrapper around the
3808 make_{pointer,ptrmem,reference}_declarator functions that decides
3809 which one to call based on the CODE and CLASS_TYPE arguments. The
3810 CODE argument should be one of the values returned by
3811 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3812 appertain to the pointer or reference. */
3814 static cp_declarator *
3815 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3816 cp_cv_quals cv_qualifiers,
3817 cp_declarator *target,
3818 tree attributes)
3820 if (code == ERROR_MARK)
3821 return cp_error_declarator;
3823 if (code == INDIRECT_REF)
3824 if (class_type == NULL_TREE)
3825 return make_pointer_declarator (cv_qualifiers, target, attributes);
3826 else
3827 return make_ptrmem_declarator (cv_qualifiers, class_type,
3828 target, attributes);
3829 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3830 return make_reference_declarator (cv_qualifiers, target,
3831 false, attributes);
3832 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3833 return make_reference_declarator (cv_qualifiers, target,
3834 true, attributes);
3835 gcc_unreachable ();
3838 /* Create a new C++ parser. */
3840 static cp_parser *
3841 cp_parser_new (void)
3843 cp_parser *parser;
3844 cp_lexer *lexer;
3845 unsigned i;
3847 /* cp_lexer_new_main is called before doing GC allocation because
3848 cp_lexer_new_main might load a PCH file. */
3849 lexer = cp_lexer_new_main ();
3851 /* Initialize the binops_by_token so that we can get the tree
3852 directly from the token. */
3853 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3854 binops_by_token[binops[i].token_type] = binops[i];
3856 parser = ggc_cleared_alloc<cp_parser> ();
3857 parser->lexer = lexer;
3858 parser->context = cp_parser_context_new (NULL);
3860 /* For now, we always accept GNU extensions. */
3861 parser->allow_gnu_extensions_p = 1;
3863 /* The `>' token is a greater-than operator, not the end of a
3864 template-id. */
3865 parser->greater_than_is_operator_p = true;
3867 parser->default_arg_ok_p = true;
3869 /* We are not parsing a constant-expression. */
3870 parser->integral_constant_expression_p = false;
3871 parser->allow_non_integral_constant_expression_p = false;
3872 parser->non_integral_constant_expression_p = false;
3874 /* Local variable names are not forbidden. */
3875 parser->local_variables_forbidden_p = false;
3877 /* We are not processing an `extern "C"' declaration. */
3878 parser->in_unbraced_linkage_specification_p = false;
3880 /* We are not processing a declarator. */
3881 parser->in_declarator_p = false;
3883 /* We are not processing a template-argument-list. */
3884 parser->in_template_argument_list_p = false;
3886 /* We are not in an iteration statement. */
3887 parser->in_statement = 0;
3889 /* We are not in a switch statement. */
3890 parser->in_switch_statement_p = false;
3892 /* We are not parsing a type-id inside an expression. */
3893 parser->in_type_id_in_expr_p = false;
3895 /* Declarations aren't implicitly extern "C". */
3896 parser->implicit_extern_c = false;
3898 /* String literals should be translated to the execution character set. */
3899 parser->translate_strings_p = true;
3901 /* We are not parsing a function body. */
3902 parser->in_function_body = false;
3904 /* We can correct until told otherwise. */
3905 parser->colon_corrects_to_scope_p = true;
3907 /* The unparsed function queue is empty. */
3908 push_unparsed_function_queues (parser);
3910 /* There are no classes being defined. */
3911 parser->num_classes_being_defined = 0;
3913 /* No template parameters apply. */
3914 parser->num_template_parameter_lists = 0;
3916 /* Special parsing data structures. */
3917 parser->omp_declare_simd = NULL;
3918 parser->oacc_routine = NULL;
3920 /* Not declaring an implicit function template. */
3921 parser->auto_is_implicit_function_template_parm_p = false;
3922 parser->fully_implicit_function_template_p = false;
3923 parser->implicit_template_parms = 0;
3924 parser->implicit_template_scope = 0;
3926 /* Allow constrained-type-specifiers. */
3927 parser->prevent_constrained_type_specifiers = 0;
3929 /* We haven't yet seen an 'extern "C"'. */
3930 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3932 return parser;
3935 /* Create a cp_lexer structure which will emit the tokens in CACHE
3936 and push it onto the parser's lexer stack. This is used for delayed
3937 parsing of in-class method bodies and default arguments, and should
3938 not be confused with tentative parsing. */
3939 static void
3940 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3942 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3943 lexer->next = parser->lexer;
3944 parser->lexer = lexer;
3946 /* Move the current source position to that of the first token in the
3947 new lexer. */
3948 cp_lexer_set_source_position_from_token (lexer->next_token);
3951 /* Pop the top lexer off the parser stack. This is never used for the
3952 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3953 static void
3954 cp_parser_pop_lexer (cp_parser *parser)
3956 cp_lexer *lexer = parser->lexer;
3957 parser->lexer = lexer->next;
3958 cp_lexer_destroy (lexer);
3960 /* Put the current source position back where it was before this
3961 lexer was pushed. */
3962 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3965 /* Lexical conventions [gram.lex] */
3967 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3968 identifier. */
3970 static cp_expr
3971 cp_parser_identifier (cp_parser* parser)
3973 cp_token *token;
3975 /* Look for the identifier. */
3976 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3977 /* Return the value. */
3978 if (token)
3979 return cp_expr (token->u.value, token->location);
3980 else
3981 return error_mark_node;
3984 /* Parse a sequence of adjacent string constants. Returns a
3985 TREE_STRING representing the combined, nul-terminated string
3986 constant. If TRANSLATE is true, translate the string to the
3987 execution character set. If WIDE_OK is true, a wide string is
3988 invalid here.
3990 C++98 [lex.string] says that if a narrow string literal token is
3991 adjacent to a wide string literal token, the behavior is undefined.
3992 However, C99 6.4.5p4 says that this results in a wide string literal.
3993 We follow C99 here, for consistency with the C front end.
3995 This code is largely lifted from lex_string() in c-lex.c.
3997 FUTURE: ObjC++ will need to handle @-strings here. */
3998 static cp_expr
3999 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4000 bool lookup_udlit = true)
4002 tree value;
4003 size_t count;
4004 struct obstack str_ob;
4005 cpp_string str, istr, *strs;
4006 cp_token *tok;
4007 enum cpp_ttype type, curr_type;
4008 int have_suffix_p = 0;
4009 tree string_tree;
4010 tree suffix_id = NULL_TREE;
4011 bool curr_tok_is_userdef_p = false;
4013 tok = cp_lexer_peek_token (parser->lexer);
4014 if (!cp_parser_is_string_literal (tok))
4016 cp_parser_error (parser, "expected string-literal");
4017 return error_mark_node;
4020 location_t loc = tok->location;
4022 if (cpp_userdef_string_p (tok->type))
4024 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4025 curr_type = cpp_userdef_string_remove_type (tok->type);
4026 curr_tok_is_userdef_p = true;
4028 else
4030 string_tree = tok->u.value;
4031 curr_type = tok->type;
4033 type = curr_type;
4035 /* Try to avoid the overhead of creating and destroying an obstack
4036 for the common case of just one string. */
4037 if (!cp_parser_is_string_literal
4038 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4040 cp_lexer_consume_token (parser->lexer);
4042 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4043 str.len = TREE_STRING_LENGTH (string_tree);
4044 count = 1;
4046 if (curr_tok_is_userdef_p)
4048 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4049 have_suffix_p = 1;
4050 curr_type = cpp_userdef_string_remove_type (tok->type);
4052 else
4053 curr_type = tok->type;
4055 strs = &str;
4057 else
4059 location_t last_tok_loc = tok->location;
4060 gcc_obstack_init (&str_ob);
4061 count = 0;
4065 cp_lexer_consume_token (parser->lexer);
4066 count++;
4067 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4068 str.len = TREE_STRING_LENGTH (string_tree);
4070 if (curr_tok_is_userdef_p)
4072 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4073 if (have_suffix_p == 0)
4075 suffix_id = curr_suffix_id;
4076 have_suffix_p = 1;
4078 else if (have_suffix_p == 1
4079 && curr_suffix_id != suffix_id)
4081 error ("inconsistent user-defined literal suffixes"
4082 " %qD and %qD in string literal",
4083 suffix_id, curr_suffix_id);
4084 have_suffix_p = -1;
4086 curr_type = cpp_userdef_string_remove_type (tok->type);
4088 else
4089 curr_type = tok->type;
4091 if (type != curr_type)
4093 if (type == CPP_STRING)
4094 type = curr_type;
4095 else if (curr_type != CPP_STRING)
4097 rich_location rich_loc (line_table, tok->location);
4098 rich_loc.add_range (last_tok_loc, false);
4099 error_at (&rich_loc,
4100 "unsupported non-standard concatenation "
4101 "of string literals");
4105 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4107 last_tok_loc = tok->location;
4109 tok = cp_lexer_peek_token (parser->lexer);
4110 if (cpp_userdef_string_p (tok->type))
4112 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4113 curr_type = cpp_userdef_string_remove_type (tok->type);
4114 curr_tok_is_userdef_p = true;
4116 else
4118 string_tree = tok->u.value;
4119 curr_type = tok->type;
4120 curr_tok_is_userdef_p = false;
4123 while (cp_parser_is_string_literal (tok));
4125 /* A string literal built by concatenation has its caret=start at
4126 the start of the initial string, and its finish at the finish of
4127 the final string literal. */
4128 loc = make_location (loc, loc, get_finish (last_tok_loc));
4130 strs = (cpp_string *) obstack_finish (&str_ob);
4133 if (type != CPP_STRING && !wide_ok)
4135 cp_parser_error (parser, "a wide string is invalid in this context");
4136 type = CPP_STRING;
4139 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4140 (parse_in, strs, count, &istr, type))
4142 value = build_string (istr.len, (const char *)istr.text);
4143 free (CONST_CAST (unsigned char *, istr.text));
4145 switch (type)
4147 default:
4148 case CPP_STRING:
4149 case CPP_UTF8STRING:
4150 TREE_TYPE (value) = char_array_type_node;
4151 break;
4152 case CPP_STRING16:
4153 TREE_TYPE (value) = char16_array_type_node;
4154 break;
4155 case CPP_STRING32:
4156 TREE_TYPE (value) = char32_array_type_node;
4157 break;
4158 case CPP_WSTRING:
4159 TREE_TYPE (value) = wchar_array_type_node;
4160 break;
4163 value = fix_string_type (value);
4165 if (have_suffix_p)
4167 tree literal = build_userdef_literal (suffix_id, value,
4168 OT_NONE, NULL_TREE);
4169 if (lookup_udlit)
4170 value = cp_parser_userdef_string_literal (literal);
4171 else
4172 value = literal;
4175 else
4176 /* cpp_interpret_string has issued an error. */
4177 value = error_mark_node;
4179 if (count > 1)
4180 obstack_free (&str_ob, 0);
4182 return cp_expr (value, loc);
4185 /* Look up a literal operator with the name and the exact arguments. */
4187 static tree
4188 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4190 tree decl;
4191 decl = lookup_name (name);
4192 if (!decl || !is_overloaded_fn (decl))
4193 return error_mark_node;
4195 for (lkp_iterator iter (decl); iter; ++iter)
4197 unsigned int ix;
4198 bool found = true;
4199 tree fn = *iter;
4200 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4201 if (parmtypes != NULL_TREE)
4203 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4204 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4206 tree tparm = TREE_VALUE (parmtypes);
4207 tree targ = TREE_TYPE ((*args)[ix]);
4208 bool ptr = TYPE_PTR_P (tparm);
4209 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4210 if ((ptr || arr || !same_type_p (tparm, targ))
4211 && (!ptr || !arr
4212 || !same_type_p (TREE_TYPE (tparm),
4213 TREE_TYPE (targ))))
4214 found = false;
4216 if (found
4217 && ix == vec_safe_length (args)
4218 /* May be this should be sufficient_parms_p instead,
4219 depending on how exactly should user-defined literals
4220 work in presence of default arguments on the literal
4221 operator parameters. */
4222 && parmtypes == void_list_node)
4223 return decl;
4227 return error_mark_node;
4230 /* Parse a user-defined char constant. Returns a call to a user-defined
4231 literal operator taking the character as an argument. */
4233 static cp_expr
4234 cp_parser_userdef_char_literal (cp_parser *parser)
4236 cp_token *token = cp_lexer_consume_token (parser->lexer);
4237 tree literal = token->u.value;
4238 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4239 tree value = USERDEF_LITERAL_VALUE (literal);
4240 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4241 tree decl, result;
4243 /* Build up a call to the user-defined operator */
4244 /* Lookup the name we got back from the id-expression. */
4245 vec<tree, va_gc> *args = make_tree_vector ();
4246 vec_safe_push (args, value);
4247 decl = lookup_literal_operator (name, args);
4248 if (!decl || decl == error_mark_node)
4250 error ("unable to find character literal operator %qD with %qT argument",
4251 name, TREE_TYPE (value));
4252 release_tree_vector (args);
4253 return error_mark_node;
4255 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4256 release_tree_vector (args);
4257 return result;
4260 /* A subroutine of cp_parser_userdef_numeric_literal to
4261 create a char... template parameter pack from a string node. */
4263 static tree
4264 make_char_string_pack (tree value)
4266 tree charvec;
4267 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4268 const char *str = TREE_STRING_POINTER (value);
4269 int i, len = TREE_STRING_LENGTH (value) - 1;
4270 tree argvec = make_tree_vec (1);
4272 /* Fill in CHARVEC with all of the parameters. */
4273 charvec = make_tree_vec (len);
4274 for (i = 0; i < len; ++i)
4275 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4277 /* Build the argument packs. */
4278 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4280 TREE_VEC_ELT (argvec, 0) = argpack;
4282 return argvec;
4285 /* A subroutine of cp_parser_userdef_numeric_literal to
4286 create a char... template parameter pack from a string node. */
4288 static tree
4289 make_string_pack (tree value)
4291 tree charvec;
4292 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4293 const unsigned char *str
4294 = (const unsigned char *) TREE_STRING_POINTER (value);
4295 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4296 int len = TREE_STRING_LENGTH (value) / sz - 1;
4297 tree argvec = make_tree_vec (2);
4299 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4300 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4302 /* First template parm is character type. */
4303 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4305 /* Fill in CHARVEC with all of the parameters. */
4306 charvec = make_tree_vec (len);
4307 for (int i = 0; i < len; ++i)
4308 TREE_VEC_ELT (charvec, i)
4309 = double_int_to_tree (str_char_type_node,
4310 double_int::from_buffer (str + i * sz, sz));
4312 /* Build the argument packs. */
4313 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4315 TREE_VEC_ELT (argvec, 1) = argpack;
4317 return argvec;
4320 /* Parse a user-defined numeric constant. returns a call to a user-defined
4321 literal operator. */
4323 static cp_expr
4324 cp_parser_userdef_numeric_literal (cp_parser *parser)
4326 cp_token *token = cp_lexer_consume_token (parser->lexer);
4327 tree literal = token->u.value;
4328 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4329 tree value = USERDEF_LITERAL_VALUE (literal);
4330 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4331 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4332 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4333 tree decl, result;
4334 vec<tree, va_gc> *args;
4336 /* Look for a literal operator taking the exact type of numeric argument
4337 as the literal value. */
4338 args = make_tree_vector ();
4339 vec_safe_push (args, value);
4340 decl = lookup_literal_operator (name, args);
4341 if (decl && decl != error_mark_node)
4343 result = finish_call_expr (decl, &args, false, true,
4344 tf_warning_or_error);
4346 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4348 warning_at (token->location, OPT_Woverflow,
4349 "integer literal exceeds range of %qT type",
4350 long_long_unsigned_type_node);
4352 else
4354 if (overflow > 0)
4355 warning_at (token->location, OPT_Woverflow,
4356 "floating literal exceeds range of %qT type",
4357 long_double_type_node);
4358 else if (overflow < 0)
4359 warning_at (token->location, OPT_Woverflow,
4360 "floating literal truncated to zero");
4363 release_tree_vector (args);
4364 return result;
4366 release_tree_vector (args);
4368 /* If the numeric argument didn't work, look for a raw literal
4369 operator taking a const char* argument consisting of the number
4370 in string format. */
4371 args = make_tree_vector ();
4372 vec_safe_push (args, num_string);
4373 decl = lookup_literal_operator (name, args);
4374 if (decl && decl != error_mark_node)
4376 result = finish_call_expr (decl, &args, false, true,
4377 tf_warning_or_error);
4378 release_tree_vector (args);
4379 return result;
4381 release_tree_vector (args);
4383 /* If the raw literal didn't work, look for a non-type template
4384 function with parameter pack char.... Call the function with
4385 template parameter characters representing the number. */
4386 args = make_tree_vector ();
4387 decl = lookup_literal_operator (name, args);
4388 if (decl && decl != error_mark_node)
4390 tree tmpl_args = make_char_string_pack (num_string);
4391 decl = lookup_template_function (decl, tmpl_args);
4392 result = finish_call_expr (decl, &args, false, true,
4393 tf_warning_or_error);
4394 release_tree_vector (args);
4395 return result;
4398 release_tree_vector (args);
4400 error ("unable to find numeric literal operator %qD", name);
4401 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4402 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4403 "to enable more built-in suffixes");
4404 return error_mark_node;
4407 /* Parse a user-defined string constant. Returns a call to a user-defined
4408 literal operator taking a character pointer and the length of the string
4409 as arguments. */
4411 static tree
4412 cp_parser_userdef_string_literal (tree literal)
4414 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4415 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4416 tree value = USERDEF_LITERAL_VALUE (literal);
4417 int len = TREE_STRING_LENGTH (value)
4418 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4419 tree decl, result;
4420 vec<tree, va_gc> *args;
4422 /* Build up a call to the user-defined operator. */
4423 /* Lookup the name we got back from the id-expression. */
4424 args = make_tree_vector ();
4425 vec_safe_push (args, value);
4426 vec_safe_push (args, build_int_cst (size_type_node, len));
4427 decl = lookup_literal_operator (name, args);
4429 if (decl && decl != error_mark_node)
4431 result = finish_call_expr (decl, &args, false, true,
4432 tf_warning_or_error);
4433 release_tree_vector (args);
4434 return result;
4436 release_tree_vector (args);
4438 /* Look for a template function with typename parameter CharT
4439 and parameter pack CharT... Call the function with
4440 template parameter characters representing the string. */
4441 args = make_tree_vector ();
4442 decl = lookup_literal_operator (name, args);
4443 if (decl && decl != error_mark_node)
4445 tree tmpl_args = make_string_pack (value);
4446 decl = lookup_template_function (decl, tmpl_args);
4447 result = finish_call_expr (decl, &args, false, true,
4448 tf_warning_or_error);
4449 release_tree_vector (args);
4450 return result;
4452 release_tree_vector (args);
4454 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4455 name, TREE_TYPE (value), size_type_node);
4456 return error_mark_node;
4460 /* Basic concepts [gram.basic] */
4462 /* Parse a translation-unit.
4464 translation-unit:
4465 declaration-seq [opt]
4467 Returns TRUE if all went well. */
4469 static bool
4470 cp_parser_translation_unit (cp_parser* parser)
4472 /* The address of the first non-permanent object on the declarator
4473 obstack. */
4474 static void *declarator_obstack_base;
4476 bool success;
4478 /* Create the declarator obstack, if necessary. */
4479 if (!cp_error_declarator)
4481 gcc_obstack_init (&declarator_obstack);
4482 /* Create the error declarator. */
4483 cp_error_declarator = make_declarator (cdk_error);
4484 /* Create the empty parameter list. */
4485 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4486 UNKNOWN_LOCATION);
4487 /* Remember where the base of the declarator obstack lies. */
4488 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4491 cp_parser_declaration_seq_opt (parser);
4493 /* If there are no tokens left then all went well. */
4494 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4496 /* Get rid of the token array; we don't need it any more. */
4497 cp_lexer_destroy (parser->lexer);
4498 parser->lexer = NULL;
4500 /* This file might have been a context that's implicitly extern
4501 "C". If so, pop the lang context. (Only relevant for PCH.) */
4502 if (parser->implicit_extern_c)
4504 pop_lang_context ();
4505 parser->implicit_extern_c = false;
4508 /* Finish up. */
4509 finish_translation_unit ();
4511 success = true;
4513 else
4515 cp_parser_error (parser, "expected declaration");
4516 success = false;
4519 /* Make sure the declarator obstack was fully cleaned up. */
4520 gcc_assert (obstack_next_free (&declarator_obstack)
4521 == declarator_obstack_base);
4523 /* All went well. */
4524 return success;
4527 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4528 decltype context. */
4530 static inline tsubst_flags_t
4531 complain_flags (bool decltype_p)
4533 tsubst_flags_t complain = tf_warning_or_error;
4534 if (decltype_p)
4535 complain |= tf_decltype;
4536 return complain;
4539 /* We're about to parse a collection of statements. If we're currently
4540 parsing tentatively, set up a firewall so that any nested
4541 cp_parser_commit_to_tentative_parse won't affect the current context. */
4543 static cp_token_position
4544 cp_parser_start_tentative_firewall (cp_parser *parser)
4546 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4547 return 0;
4549 cp_parser_parse_tentatively (parser);
4550 cp_parser_commit_to_topmost_tentative_parse (parser);
4551 return cp_lexer_token_position (parser->lexer, false);
4554 /* We've finished parsing the collection of statements. Wrap up the
4555 firewall and replace the relevant tokens with the parsed form. */
4557 static void
4558 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4559 tree expr)
4561 if (!start)
4562 return;
4564 /* Finish the firewall level. */
4565 cp_parser_parse_definitely (parser);
4566 /* And remember the result of the parse for when we try again. */
4567 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4568 token->type = CPP_PREPARSED_EXPR;
4569 token->u.value = expr;
4570 token->keyword = RID_MAX;
4571 cp_lexer_purge_tokens_after (parser->lexer, start);
4574 /* Like the above functions, but let the user modify the tokens. Used by
4575 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4576 later parses, so it makes sense to localize the effects of
4577 cp_parser_commit_to_tentative_parse. */
4579 struct tentative_firewall
4581 cp_parser *parser;
4582 bool set;
4584 tentative_firewall (cp_parser *p): parser(p)
4586 /* If we're currently parsing tentatively, start a committed level as a
4587 firewall and then an inner tentative parse. */
4588 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4590 cp_parser_parse_tentatively (parser);
4591 cp_parser_commit_to_topmost_tentative_parse (parser);
4592 cp_parser_parse_tentatively (parser);
4596 ~tentative_firewall()
4598 if (set)
4600 /* Finish the inner tentative parse and the firewall, propagating any
4601 uncommitted error state to the outer tentative parse. */
4602 bool err = cp_parser_error_occurred (parser);
4603 cp_parser_parse_definitely (parser);
4604 cp_parser_parse_definitely (parser);
4605 if (err)
4606 cp_parser_simulate_error (parser);
4611 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4612 This class is for tracking such a matching pair of symbols.
4613 In particular, it tracks the location of the first token,
4614 so that if the second token is missing, we can highlight the
4615 location of the first token when notifying the user about the
4616 problem. */
4618 template <typename traits_t>
4619 class token_pair
4621 public:
4622 /* token_pair's ctor. */
4623 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4625 /* If the next token is the opening symbol for this pair, consume it and
4626 return true.
4627 Otherwise, issue an error and return false.
4628 In either case, record the location of the opening token. */
4630 bool require_open (cp_parser *parser)
4632 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4633 return cp_parser_require (parser, traits_t::open_token_type,
4634 traits_t::required_token_open);
4637 /* Consume the next token from PARSER, recording its location as
4638 that of the opening token within the pair. */
4640 cp_token * consume_open (cp_parser *parser)
4642 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4643 gcc_assert (tok->type == traits_t::open_token_type);
4644 m_open_loc = tok->location;
4645 return tok;
4648 /* If the next token is the closing symbol for this pair, consume it
4649 and return it.
4650 Otherwise, issue an error, highlighting the location of the
4651 corresponding opening token, and return NULL. */
4653 cp_token *require_close (cp_parser *parser) const
4655 return cp_parser_require (parser, traits_t::close_token_type,
4656 traits_t::required_token_close,
4657 m_open_loc);
4660 private:
4661 location_t m_open_loc;
4664 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4666 struct matching_paren_traits
4668 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4669 static const enum required_token required_token_open = RT_OPEN_PAREN;
4670 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4671 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4674 /* "matching_parens" is a token_pair<T> class for tracking matching
4675 pairs of parentheses. */
4677 typedef token_pair<matching_paren_traits> matching_parens;
4679 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4681 struct matching_brace_traits
4683 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4684 static const enum required_token required_token_open = RT_OPEN_BRACE;
4685 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4686 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4689 /* "matching_braces" is a token_pair<T> class for tracking matching
4690 pairs of braces. */
4692 typedef token_pair<matching_brace_traits> matching_braces;
4695 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4696 enclosing parentheses. */
4698 static cp_expr
4699 cp_parser_statement_expr (cp_parser *parser)
4701 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4703 /* Consume the '('. */
4704 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4705 matching_parens parens;
4706 parens.consume_open (parser);
4707 /* Start the statement-expression. */
4708 tree expr = begin_stmt_expr ();
4709 /* Parse the compound-statement. */
4710 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4711 /* Finish up. */
4712 expr = finish_stmt_expr (expr, false);
4713 /* Consume the ')'. */
4714 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4715 if (!parens.require_close (parser))
4716 cp_parser_skip_to_end_of_statement (parser);
4718 cp_parser_end_tentative_firewall (parser, start, expr);
4719 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4720 return cp_expr (expr, combined_loc);
4723 /* Expressions [gram.expr] */
4725 /* Parse a fold-operator.
4727 fold-operator:
4728 - * / % ^ & | = < > << >>
4729 = -= *= /= %= ^= &= |= <<= >>=
4730 == != <= >= && || , .* ->*
4732 This returns the tree code corresponding to the matched operator
4733 as an int. When the current token matches a compound assignment
4734 opertor, the resulting tree code is the negative value of the
4735 non-assignment operator. */
4737 static int
4738 cp_parser_fold_operator (cp_token *token)
4740 switch (token->type)
4742 case CPP_PLUS: return PLUS_EXPR;
4743 case CPP_MINUS: return MINUS_EXPR;
4744 case CPP_MULT: return MULT_EXPR;
4745 case CPP_DIV: return TRUNC_DIV_EXPR;
4746 case CPP_MOD: return TRUNC_MOD_EXPR;
4747 case CPP_XOR: return BIT_XOR_EXPR;
4748 case CPP_AND: return BIT_AND_EXPR;
4749 case CPP_OR: return BIT_IOR_EXPR;
4750 case CPP_LSHIFT: return LSHIFT_EXPR;
4751 case CPP_RSHIFT: return RSHIFT_EXPR;
4753 case CPP_EQ: return -NOP_EXPR;
4754 case CPP_PLUS_EQ: return -PLUS_EXPR;
4755 case CPP_MINUS_EQ: return -MINUS_EXPR;
4756 case CPP_MULT_EQ: return -MULT_EXPR;
4757 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4758 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4759 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4760 case CPP_AND_EQ: return -BIT_AND_EXPR;
4761 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4762 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4763 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4765 case CPP_EQ_EQ: return EQ_EXPR;
4766 case CPP_NOT_EQ: return NE_EXPR;
4767 case CPP_LESS: return LT_EXPR;
4768 case CPP_GREATER: return GT_EXPR;
4769 case CPP_LESS_EQ: return LE_EXPR;
4770 case CPP_GREATER_EQ: return GE_EXPR;
4772 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4773 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4775 case CPP_COMMA: return COMPOUND_EXPR;
4777 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4778 case CPP_DEREF_STAR: return MEMBER_REF;
4780 default: return ERROR_MARK;
4784 /* Returns true if CODE indicates a binary expression, which is not allowed in
4785 the LHS of a fold-expression. More codes will need to be added to use this
4786 function in other contexts. */
4788 static bool
4789 is_binary_op (tree_code code)
4791 switch (code)
4793 case PLUS_EXPR:
4794 case POINTER_PLUS_EXPR:
4795 case MINUS_EXPR:
4796 case MULT_EXPR:
4797 case TRUNC_DIV_EXPR:
4798 case TRUNC_MOD_EXPR:
4799 case BIT_XOR_EXPR:
4800 case BIT_AND_EXPR:
4801 case BIT_IOR_EXPR:
4802 case LSHIFT_EXPR:
4803 case RSHIFT_EXPR:
4805 case MODOP_EXPR:
4807 case EQ_EXPR:
4808 case NE_EXPR:
4809 case LE_EXPR:
4810 case GE_EXPR:
4811 case LT_EXPR:
4812 case GT_EXPR:
4814 case TRUTH_ANDIF_EXPR:
4815 case TRUTH_ORIF_EXPR:
4817 case COMPOUND_EXPR:
4819 case DOTSTAR_EXPR:
4820 case MEMBER_REF:
4821 return true;
4823 default:
4824 return false;
4828 /* If the next token is a suitable fold operator, consume it and return as
4829 the function above. */
4831 static int
4832 cp_parser_fold_operator (cp_parser *parser)
4834 cp_token* token = cp_lexer_peek_token (parser->lexer);
4835 int code = cp_parser_fold_operator (token);
4836 if (code != ERROR_MARK)
4837 cp_lexer_consume_token (parser->lexer);
4838 return code;
4841 /* Parse a fold-expression.
4843 fold-expression:
4844 ( ... folding-operator cast-expression)
4845 ( cast-expression folding-operator ... )
4846 ( cast-expression folding operator ... folding-operator cast-expression)
4848 Note that the '(' and ')' are matched in primary expression. */
4850 static cp_expr
4851 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4853 cp_id_kind pidk;
4855 // Left fold.
4856 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4858 cp_lexer_consume_token (parser->lexer);
4859 int op = cp_parser_fold_operator (parser);
4860 if (op == ERROR_MARK)
4862 cp_parser_error (parser, "expected binary operator");
4863 return error_mark_node;
4866 tree expr = cp_parser_cast_expression (parser, false, false,
4867 false, &pidk);
4868 if (expr == error_mark_node)
4869 return error_mark_node;
4870 return finish_left_unary_fold_expr (expr, op);
4873 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4874 int op = cp_parser_fold_operator (parser);
4875 if (op == ERROR_MARK)
4877 cp_parser_error (parser, "expected binary operator");
4878 return error_mark_node;
4881 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4883 cp_parser_error (parser, "expected ...");
4884 return error_mark_node;
4886 cp_lexer_consume_token (parser->lexer);
4888 /* The operands of a fold-expression are cast-expressions, so binary or
4889 conditional expressions are not allowed. We check this here to avoid
4890 tentative parsing. */
4891 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4892 /* OK, the expression was parenthesized. */;
4893 else if (is_binary_op (TREE_CODE (expr1)))
4894 error_at (location_of (expr1),
4895 "binary expression in operand of fold-expression");
4896 else if (TREE_CODE (expr1) == COND_EXPR)
4897 error_at (location_of (expr1),
4898 "conditional expression in operand of fold-expression");
4900 // Right fold.
4901 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4902 return finish_right_unary_fold_expr (expr1, op);
4904 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4906 cp_parser_error (parser, "mismatched operator in fold-expression");
4907 return error_mark_node;
4909 cp_lexer_consume_token (parser->lexer);
4911 // Binary left or right fold.
4912 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4913 if (expr2 == error_mark_node)
4914 return error_mark_node;
4915 return finish_binary_fold_expr (expr1, expr2, op);
4918 /* Parse a primary-expression.
4920 primary-expression:
4921 literal
4922 this
4923 ( expression )
4924 id-expression
4925 lambda-expression (C++11)
4927 GNU Extensions:
4929 primary-expression:
4930 ( compound-statement )
4931 __builtin_va_arg ( assignment-expression , type-id )
4932 __builtin_offsetof ( type-id , offsetof-expression )
4934 C++ Extensions:
4935 __has_nothrow_assign ( type-id )
4936 __has_nothrow_constructor ( type-id )
4937 __has_nothrow_copy ( type-id )
4938 __has_trivial_assign ( type-id )
4939 __has_trivial_constructor ( type-id )
4940 __has_trivial_copy ( type-id )
4941 __has_trivial_destructor ( type-id )
4942 __has_virtual_destructor ( type-id )
4943 __is_abstract ( type-id )
4944 __is_base_of ( type-id , type-id )
4945 __is_class ( type-id )
4946 __is_empty ( type-id )
4947 __is_enum ( type-id )
4948 __is_final ( type-id )
4949 __is_literal_type ( type-id )
4950 __is_pod ( type-id )
4951 __is_polymorphic ( type-id )
4952 __is_std_layout ( type-id )
4953 __is_trivial ( type-id )
4954 __is_union ( type-id )
4956 Objective-C++ Extension:
4958 primary-expression:
4959 objc-expression
4961 literal:
4962 __null
4964 ADDRESS_P is true iff this expression was immediately preceded by
4965 "&" and therefore might denote a pointer-to-member. CAST_P is true
4966 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4967 true iff this expression is a template argument.
4969 Returns a representation of the expression. Upon return, *IDK
4970 indicates what kind of id-expression (if any) was present. */
4972 static cp_expr
4973 cp_parser_primary_expression (cp_parser *parser,
4974 bool address_p,
4975 bool cast_p,
4976 bool template_arg_p,
4977 bool decltype_p,
4978 cp_id_kind *idk)
4980 cp_token *token = NULL;
4982 /* Assume the primary expression is not an id-expression. */
4983 *idk = CP_ID_KIND_NONE;
4985 /* Peek at the next token. */
4986 token = cp_lexer_peek_token (parser->lexer);
4987 switch ((int) token->type)
4989 /* literal:
4990 integer-literal
4991 character-literal
4992 floating-literal
4993 string-literal
4994 boolean-literal
4995 pointer-literal
4996 user-defined-literal */
4997 case CPP_CHAR:
4998 case CPP_CHAR16:
4999 case CPP_CHAR32:
5000 case CPP_WCHAR:
5001 case CPP_UTF8CHAR:
5002 case CPP_NUMBER:
5003 case CPP_PREPARSED_EXPR:
5004 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5005 return cp_parser_userdef_numeric_literal (parser);
5006 token = cp_lexer_consume_token (parser->lexer);
5007 if (TREE_CODE (token->u.value) == FIXED_CST)
5009 error_at (token->location,
5010 "fixed-point types not supported in C++");
5011 return error_mark_node;
5013 /* Floating-point literals are only allowed in an integral
5014 constant expression if they are cast to an integral or
5015 enumeration type. */
5016 if (TREE_CODE (token->u.value) == REAL_CST
5017 && parser->integral_constant_expression_p
5018 && pedantic)
5020 /* CAST_P will be set even in invalid code like "int(2.7 +
5021 ...)". Therefore, we have to check that the next token
5022 is sure to end the cast. */
5023 if (cast_p)
5025 cp_token *next_token;
5027 next_token = cp_lexer_peek_token (parser->lexer);
5028 if (/* The comma at the end of an
5029 enumerator-definition. */
5030 next_token->type != CPP_COMMA
5031 /* The curly brace at the end of an enum-specifier. */
5032 && next_token->type != CPP_CLOSE_BRACE
5033 /* The end of a statement. */
5034 && next_token->type != CPP_SEMICOLON
5035 /* The end of the cast-expression. */
5036 && next_token->type != CPP_CLOSE_PAREN
5037 /* The end of an array bound. */
5038 && next_token->type != CPP_CLOSE_SQUARE
5039 /* The closing ">" in a template-argument-list. */
5040 && (next_token->type != CPP_GREATER
5041 || parser->greater_than_is_operator_p)
5042 /* C++0x only: A ">>" treated like two ">" tokens,
5043 in a template-argument-list. */
5044 && (next_token->type != CPP_RSHIFT
5045 || (cxx_dialect == cxx98)
5046 || parser->greater_than_is_operator_p))
5047 cast_p = false;
5050 /* If we are within a cast, then the constraint that the
5051 cast is to an integral or enumeration type will be
5052 checked at that point. If we are not within a cast, then
5053 this code is invalid. */
5054 if (!cast_p)
5055 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5057 return cp_expr (token->u.value, token->location);
5059 case CPP_CHAR_USERDEF:
5060 case CPP_CHAR16_USERDEF:
5061 case CPP_CHAR32_USERDEF:
5062 case CPP_WCHAR_USERDEF:
5063 case CPP_UTF8CHAR_USERDEF:
5064 return cp_parser_userdef_char_literal (parser);
5066 case CPP_STRING:
5067 case CPP_STRING16:
5068 case CPP_STRING32:
5069 case CPP_WSTRING:
5070 case CPP_UTF8STRING:
5071 case CPP_STRING_USERDEF:
5072 case CPP_STRING16_USERDEF:
5073 case CPP_STRING32_USERDEF:
5074 case CPP_WSTRING_USERDEF:
5075 case CPP_UTF8STRING_USERDEF:
5076 /* ??? Should wide strings be allowed when parser->translate_strings_p
5077 is false (i.e. in attributes)? If not, we can kill the third
5078 argument to cp_parser_string_literal. */
5079 return cp_parser_string_literal (parser,
5080 parser->translate_strings_p,
5081 true);
5083 case CPP_OPEN_PAREN:
5084 /* If we see `( { ' then we are looking at the beginning of
5085 a GNU statement-expression. */
5086 if (cp_parser_allow_gnu_extensions_p (parser)
5087 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5089 /* Statement-expressions are not allowed by the standard. */
5090 pedwarn (token->location, OPT_Wpedantic,
5091 "ISO C++ forbids braced-groups within expressions");
5093 /* And they're not allowed outside of a function-body; you
5094 cannot, for example, write:
5096 int i = ({ int j = 3; j + 1; });
5098 at class or namespace scope. */
5099 if (!parser->in_function_body
5100 || parser->in_template_argument_list_p)
5102 error_at (token->location,
5103 "statement-expressions are not allowed outside "
5104 "functions nor in template-argument lists");
5105 cp_parser_skip_to_end_of_block_or_statement (parser);
5106 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5107 cp_lexer_consume_token (parser->lexer);
5108 return error_mark_node;
5110 else
5111 return cp_parser_statement_expr (parser);
5113 /* Otherwise it's a normal parenthesized expression. */
5115 cp_expr expr;
5116 bool saved_greater_than_is_operator_p;
5118 location_t open_paren_loc = token->location;
5120 /* Consume the `('. */
5121 matching_parens parens;
5122 parens.consume_open (parser);
5123 /* Within a parenthesized expression, a `>' token is always
5124 the greater-than operator. */
5125 saved_greater_than_is_operator_p
5126 = parser->greater_than_is_operator_p;
5127 parser->greater_than_is_operator_p = true;
5129 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5130 /* Left fold expression. */
5131 expr = NULL_TREE;
5132 else
5133 /* Parse the parenthesized expression. */
5134 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5136 token = cp_lexer_peek_token (parser->lexer);
5137 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5139 expr = cp_parser_fold_expression (parser, expr);
5140 if (expr != error_mark_node
5141 && cxx_dialect < cxx17
5142 && !in_system_header_at (input_location))
5143 pedwarn (input_location, 0, "fold-expressions only available "
5144 "with -std=c++17 or -std=gnu++17");
5146 else
5147 /* Let the front end know that this expression was
5148 enclosed in parentheses. This matters in case, for
5149 example, the expression is of the form `A::B', since
5150 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5151 not. */
5152 expr = finish_parenthesized_expr (expr);
5154 /* DR 705: Wrapping an unqualified name in parentheses
5155 suppresses arg-dependent lookup. We want to pass back
5156 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5157 (c++/37862), but none of the others. */
5158 if (*idk != CP_ID_KIND_QUALIFIED)
5159 *idk = CP_ID_KIND_NONE;
5161 /* The `>' token might be the end of a template-id or
5162 template-parameter-list now. */
5163 parser->greater_than_is_operator_p
5164 = saved_greater_than_is_operator_p;
5166 /* Consume the `)'. */
5167 token = cp_lexer_peek_token (parser->lexer);
5168 location_t close_paren_loc = token->location;
5169 expr.set_range (open_paren_loc, close_paren_loc);
5170 if (!parens.require_close (parser)
5171 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5172 cp_parser_skip_to_end_of_statement (parser);
5174 return expr;
5177 case CPP_OPEN_SQUARE:
5179 if (c_dialect_objc ())
5181 /* We might have an Objective-C++ message. */
5182 cp_parser_parse_tentatively (parser);
5183 tree msg = cp_parser_objc_message_expression (parser);
5184 /* If that works out, we're done ... */
5185 if (cp_parser_parse_definitely (parser))
5186 return msg;
5187 /* ... else, fall though to see if it's a lambda. */
5189 cp_expr lam = cp_parser_lambda_expression (parser);
5190 /* Don't warn about a failed tentative parse. */
5191 if (cp_parser_error_occurred (parser))
5192 return error_mark_node;
5193 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5194 return lam;
5197 case CPP_OBJC_STRING:
5198 if (c_dialect_objc ())
5199 /* We have an Objective-C++ string literal. */
5200 return cp_parser_objc_expression (parser);
5201 cp_parser_error (parser, "expected primary-expression");
5202 return error_mark_node;
5204 case CPP_KEYWORD:
5205 switch (token->keyword)
5207 /* These two are the boolean literals. */
5208 case RID_TRUE:
5209 cp_lexer_consume_token (parser->lexer);
5210 return cp_expr (boolean_true_node, token->location);
5211 case RID_FALSE:
5212 cp_lexer_consume_token (parser->lexer);
5213 return cp_expr (boolean_false_node, token->location);
5215 /* The `__null' literal. */
5216 case RID_NULL:
5217 cp_lexer_consume_token (parser->lexer);
5218 return cp_expr (null_node, token->location);
5220 /* The `nullptr' literal. */
5221 case RID_NULLPTR:
5222 cp_lexer_consume_token (parser->lexer);
5223 return cp_expr (nullptr_node, token->location);
5225 /* Recognize the `this' keyword. */
5226 case RID_THIS:
5227 cp_lexer_consume_token (parser->lexer);
5228 if (parser->local_variables_forbidden_p)
5230 error_at (token->location,
5231 "%<this%> may not be used in this context");
5232 return error_mark_node;
5234 /* Pointers cannot appear in constant-expressions. */
5235 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5236 return error_mark_node;
5237 return cp_expr (finish_this_expr (), token->location);
5239 /* The `operator' keyword can be the beginning of an
5240 id-expression. */
5241 case RID_OPERATOR:
5242 goto id_expression;
5244 case RID_FUNCTION_NAME:
5245 case RID_PRETTY_FUNCTION_NAME:
5246 case RID_C99_FUNCTION_NAME:
5248 non_integral_constant name;
5250 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5251 __func__ are the names of variables -- but they are
5252 treated specially. Therefore, they are handled here,
5253 rather than relying on the generic id-expression logic
5254 below. Grammatically, these names are id-expressions.
5256 Consume the token. */
5257 token = cp_lexer_consume_token (parser->lexer);
5259 switch (token->keyword)
5261 case RID_FUNCTION_NAME:
5262 name = NIC_FUNC_NAME;
5263 break;
5264 case RID_PRETTY_FUNCTION_NAME:
5265 name = NIC_PRETTY_FUNC;
5266 break;
5267 case RID_C99_FUNCTION_NAME:
5268 name = NIC_C99_FUNC;
5269 break;
5270 default:
5271 gcc_unreachable ();
5274 if (cp_parser_non_integral_constant_expression (parser, name))
5275 return error_mark_node;
5277 /* Look up the name. */
5278 return finish_fname (token->u.value);
5281 case RID_VA_ARG:
5283 tree expression;
5284 tree type;
5285 source_location type_location;
5286 location_t start_loc
5287 = cp_lexer_peek_token (parser->lexer)->location;
5288 /* The `__builtin_va_arg' construct is used to handle
5289 `va_arg'. Consume the `__builtin_va_arg' token. */
5290 cp_lexer_consume_token (parser->lexer);
5291 /* Look for the opening `('. */
5292 matching_parens parens;
5293 parens.require_open (parser);
5294 /* Now, parse the assignment-expression. */
5295 expression = cp_parser_assignment_expression (parser);
5296 /* Look for the `,'. */
5297 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5298 type_location = cp_lexer_peek_token (parser->lexer)->location;
5299 /* Parse the type-id. */
5301 type_id_in_expr_sentinel s (parser);
5302 type = cp_parser_type_id (parser);
5304 /* Look for the closing `)'. */
5305 location_t finish_loc
5306 = cp_lexer_peek_token (parser->lexer)->location;
5307 parens.require_close (parser);
5308 /* Using `va_arg' in a constant-expression is not
5309 allowed. */
5310 if (cp_parser_non_integral_constant_expression (parser,
5311 NIC_VA_ARG))
5312 return error_mark_node;
5313 /* Construct a location of the form:
5314 __builtin_va_arg (v, int)
5315 ~~~~~~~~~~~~~~~~~~~~~^~~~
5316 with the caret at the type, ranging from the start of the
5317 "__builtin_va_arg" token to the close paren. */
5318 location_t combined_loc
5319 = make_location (type_location, start_loc, finish_loc);
5320 return build_x_va_arg (combined_loc, expression, type);
5323 case RID_OFFSETOF:
5324 return cp_parser_builtin_offsetof (parser);
5326 case RID_HAS_NOTHROW_ASSIGN:
5327 case RID_HAS_NOTHROW_CONSTRUCTOR:
5328 case RID_HAS_NOTHROW_COPY:
5329 case RID_HAS_TRIVIAL_ASSIGN:
5330 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5331 case RID_HAS_TRIVIAL_COPY:
5332 case RID_HAS_TRIVIAL_DESTRUCTOR:
5333 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5334 case RID_HAS_VIRTUAL_DESTRUCTOR:
5335 case RID_IS_ABSTRACT:
5336 case RID_IS_AGGREGATE:
5337 case RID_IS_BASE_OF:
5338 case RID_IS_CLASS:
5339 case RID_IS_EMPTY:
5340 case RID_IS_ENUM:
5341 case RID_IS_FINAL:
5342 case RID_IS_LITERAL_TYPE:
5343 case RID_IS_POD:
5344 case RID_IS_POLYMORPHIC:
5345 case RID_IS_SAME_AS:
5346 case RID_IS_STD_LAYOUT:
5347 case RID_IS_TRIVIAL:
5348 case RID_IS_TRIVIALLY_ASSIGNABLE:
5349 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5350 case RID_IS_TRIVIALLY_COPYABLE:
5351 case RID_IS_UNION:
5352 case RID_IS_ASSIGNABLE:
5353 case RID_IS_CONSTRUCTIBLE:
5354 return cp_parser_trait_expr (parser, token->keyword);
5356 // C++ concepts
5357 case RID_REQUIRES:
5358 return cp_parser_requires_expression (parser);
5360 /* Objective-C++ expressions. */
5361 case RID_AT_ENCODE:
5362 case RID_AT_PROTOCOL:
5363 case RID_AT_SELECTOR:
5364 return cp_parser_objc_expression (parser);
5366 case RID_TEMPLATE:
5367 if (parser->in_function_body
5368 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5369 == CPP_LESS))
5371 error_at (token->location,
5372 "a template declaration cannot appear at block scope");
5373 cp_parser_skip_to_end_of_block_or_statement (parser);
5374 return error_mark_node;
5376 /* FALLTHRU */
5377 default:
5378 cp_parser_error (parser, "expected primary-expression");
5379 return error_mark_node;
5382 /* An id-expression can start with either an identifier, a
5383 `::' as the beginning of a qualified-id, or the "operator"
5384 keyword. */
5385 case CPP_NAME:
5386 case CPP_SCOPE:
5387 case CPP_TEMPLATE_ID:
5388 case CPP_NESTED_NAME_SPECIFIER:
5390 id_expression:
5391 cp_expr id_expression;
5392 cp_expr decl;
5393 const char *error_msg;
5394 bool template_p;
5395 bool done;
5396 cp_token *id_expr_token;
5398 /* Parse the id-expression. */
5399 id_expression
5400 = cp_parser_id_expression (parser,
5401 /*template_keyword_p=*/false,
5402 /*check_dependency_p=*/true,
5403 &template_p,
5404 /*declarator_p=*/false,
5405 /*optional_p=*/false);
5406 if (id_expression == error_mark_node)
5407 return error_mark_node;
5408 id_expr_token = token;
5409 token = cp_lexer_peek_token (parser->lexer);
5410 done = (token->type != CPP_OPEN_SQUARE
5411 && token->type != CPP_OPEN_PAREN
5412 && token->type != CPP_DOT
5413 && token->type != CPP_DEREF
5414 && token->type != CPP_PLUS_PLUS
5415 && token->type != CPP_MINUS_MINUS);
5416 /* If we have a template-id, then no further lookup is
5417 required. If the template-id was for a template-class, we
5418 will sometimes have a TYPE_DECL at this point. */
5419 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5420 || TREE_CODE (id_expression) == TYPE_DECL)
5421 decl = id_expression;
5422 /* Look up the name. */
5423 else
5425 tree ambiguous_decls;
5427 /* If we already know that this lookup is ambiguous, then
5428 we've already issued an error message; there's no reason
5429 to check again. */
5430 if (id_expr_token->type == CPP_NAME
5431 && id_expr_token->error_reported)
5433 cp_parser_simulate_error (parser);
5434 return error_mark_node;
5437 decl = cp_parser_lookup_name (parser, id_expression,
5438 none_type,
5439 template_p,
5440 /*is_namespace=*/false,
5441 /*check_dependency=*/true,
5442 &ambiguous_decls,
5443 id_expr_token->location);
5444 /* If the lookup was ambiguous, an error will already have
5445 been issued. */
5446 if (ambiguous_decls)
5447 return error_mark_node;
5449 /* In Objective-C++, we may have an Objective-C 2.0
5450 dot-syntax for classes here. */
5451 if (c_dialect_objc ()
5452 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5453 && TREE_CODE (decl) == TYPE_DECL
5454 && objc_is_class_name (decl))
5456 tree component;
5457 cp_lexer_consume_token (parser->lexer);
5458 component = cp_parser_identifier (parser);
5459 if (component == error_mark_node)
5460 return error_mark_node;
5462 tree result = objc_build_class_component_ref (id_expression,
5463 component);
5464 /* Build a location of the form:
5465 expr.component
5466 ~~~~~^~~~~~~~~
5467 with caret at the start of the component name (at
5468 input_location), ranging from the start of the id_expression
5469 to the end of the component name. */
5470 location_t combined_loc
5471 = make_location (input_location, id_expression.get_start (),
5472 get_finish (input_location));
5473 protected_set_expr_location (result, combined_loc);
5474 return result;
5477 /* In Objective-C++, an instance variable (ivar) may be preferred
5478 to whatever cp_parser_lookup_name() found.
5479 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5480 rest of c-family, we have to do a little extra work to preserve
5481 any location information in cp_expr "decl". Given that
5482 objc_lookup_ivar is implemented in "c-family" and "objc", we
5483 have a trip through the pure "tree" type, rather than cp_expr.
5484 Naively copying it back to "decl" would implicitly give the
5485 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5486 store an EXPR_LOCATION. Hence we only update "decl" (and
5487 hence its location_t) if we get back a different tree node. */
5488 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5489 id_expression);
5490 if (decl_tree != decl.get_value ())
5491 decl = cp_expr (decl_tree);
5493 /* If name lookup gives us a SCOPE_REF, then the
5494 qualifying scope was dependent. */
5495 if (TREE_CODE (decl) == SCOPE_REF)
5497 /* At this point, we do not know if DECL is a valid
5498 integral constant expression. We assume that it is
5499 in fact such an expression, so that code like:
5501 template <int N> struct A {
5502 int a[B<N>::i];
5505 is accepted. At template-instantiation time, we
5506 will check that B<N>::i is actually a constant. */
5507 return decl;
5509 /* Check to see if DECL is a local variable in a context
5510 where that is forbidden. */
5511 if (parser->local_variables_forbidden_p
5512 && local_variable_p (decl))
5514 /* It might be that we only found DECL because we are
5515 trying to be generous with pre-ISO scoping rules.
5516 For example, consider:
5518 int i;
5519 void g() {
5520 for (int i = 0; i < 10; ++i) {}
5521 extern void f(int j = i);
5524 Here, name look up will originally find the out
5525 of scope `i'. We need to issue a warning message,
5526 but then use the global `i'. */
5527 decl = check_for_out_of_scope_variable (decl);
5528 if (local_variable_p (decl))
5530 error_at (id_expr_token->location,
5531 "local variable %qD may not appear in this context",
5532 decl.get_value ());
5533 return error_mark_node;
5538 decl = (finish_id_expression
5539 (id_expression, decl, parser->scope,
5540 idk,
5541 parser->integral_constant_expression_p,
5542 parser->allow_non_integral_constant_expression_p,
5543 &parser->non_integral_constant_expression_p,
5544 template_p, done, address_p,
5545 template_arg_p,
5546 &error_msg,
5547 id_expression.get_location ()));
5548 if (error_msg)
5549 cp_parser_error (parser, error_msg);
5550 decl.set_location (id_expr_token->location);
5551 return decl;
5554 /* Anything else is an error. */
5555 default:
5556 cp_parser_error (parser, "expected primary-expression");
5557 return error_mark_node;
5561 static inline cp_expr
5562 cp_parser_primary_expression (cp_parser *parser,
5563 bool address_p,
5564 bool cast_p,
5565 bool template_arg_p,
5566 cp_id_kind *idk)
5568 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5569 /*decltype*/false, idk);
5572 /* Parse an id-expression.
5574 id-expression:
5575 unqualified-id
5576 qualified-id
5578 qualified-id:
5579 :: [opt] nested-name-specifier template [opt] unqualified-id
5580 :: identifier
5581 :: operator-function-id
5582 :: template-id
5584 Return a representation of the unqualified portion of the
5585 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5586 a `::' or nested-name-specifier.
5588 Often, if the id-expression was a qualified-id, the caller will
5589 want to make a SCOPE_REF to represent the qualified-id. This
5590 function does not do this in order to avoid wastefully creating
5591 SCOPE_REFs when they are not required.
5593 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5594 `template' keyword.
5596 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5597 uninstantiated templates.
5599 If *TEMPLATE_P is non-NULL, it is set to true iff the
5600 `template' keyword is used to explicitly indicate that the entity
5601 named is a template.
5603 If DECLARATOR_P is true, the id-expression is appearing as part of
5604 a declarator, rather than as part of an expression. */
5606 static cp_expr
5607 cp_parser_id_expression (cp_parser *parser,
5608 bool template_keyword_p,
5609 bool check_dependency_p,
5610 bool *template_p,
5611 bool declarator_p,
5612 bool optional_p)
5614 bool global_scope_p;
5615 bool nested_name_specifier_p;
5617 /* Assume the `template' keyword was not used. */
5618 if (template_p)
5619 *template_p = template_keyword_p;
5621 /* Look for the optional `::' operator. */
5622 global_scope_p
5623 = (!template_keyword_p
5624 && (cp_parser_global_scope_opt (parser,
5625 /*current_scope_valid_p=*/false)
5626 != NULL_TREE));
5628 /* Look for the optional nested-name-specifier. */
5629 nested_name_specifier_p
5630 = (cp_parser_nested_name_specifier_opt (parser,
5631 /*typename_keyword_p=*/false,
5632 check_dependency_p,
5633 /*type_p=*/false,
5634 declarator_p,
5635 template_keyword_p)
5636 != NULL_TREE);
5638 /* If there is a nested-name-specifier, then we are looking at
5639 the first qualified-id production. */
5640 if (nested_name_specifier_p)
5642 tree saved_scope;
5643 tree saved_object_scope;
5644 tree saved_qualifying_scope;
5645 cp_expr unqualified_id;
5646 bool is_template;
5648 /* See if the next token is the `template' keyword. */
5649 if (!template_p)
5650 template_p = &is_template;
5651 *template_p = cp_parser_optional_template_keyword (parser);
5652 /* Name lookup we do during the processing of the
5653 unqualified-id might obliterate SCOPE. */
5654 saved_scope = parser->scope;
5655 saved_object_scope = parser->object_scope;
5656 saved_qualifying_scope = parser->qualifying_scope;
5657 /* Process the final unqualified-id. */
5658 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5659 check_dependency_p,
5660 declarator_p,
5661 /*optional_p=*/false);
5662 /* Restore the SAVED_SCOPE for our caller. */
5663 parser->scope = saved_scope;
5664 parser->object_scope = saved_object_scope;
5665 parser->qualifying_scope = saved_qualifying_scope;
5667 return unqualified_id;
5669 /* Otherwise, if we are in global scope, then we are looking at one
5670 of the other qualified-id productions. */
5671 else if (global_scope_p)
5673 cp_token *token;
5674 tree id;
5676 /* Peek at the next token. */
5677 token = cp_lexer_peek_token (parser->lexer);
5679 /* If it's an identifier, and the next token is not a "<", then
5680 we can avoid the template-id case. This is an optimization
5681 for this common case. */
5682 if (token->type == CPP_NAME
5683 && !cp_parser_nth_token_starts_template_argument_list_p
5684 (parser, 2))
5685 return cp_parser_identifier (parser);
5687 cp_parser_parse_tentatively (parser);
5688 /* Try a template-id. */
5689 id = cp_parser_template_id (parser,
5690 /*template_keyword_p=*/false,
5691 /*check_dependency_p=*/true,
5692 none_type,
5693 declarator_p);
5694 /* If that worked, we're done. */
5695 if (cp_parser_parse_definitely (parser))
5696 return id;
5698 /* Peek at the next token. (Changes in the token buffer may
5699 have invalidated the pointer obtained above.) */
5700 token = cp_lexer_peek_token (parser->lexer);
5702 switch (token->type)
5704 case CPP_NAME:
5705 return cp_parser_identifier (parser);
5707 case CPP_KEYWORD:
5708 if (token->keyword == RID_OPERATOR)
5709 return cp_parser_operator_function_id (parser);
5710 /* Fall through. */
5712 default:
5713 cp_parser_error (parser, "expected id-expression");
5714 return error_mark_node;
5717 else
5718 return cp_parser_unqualified_id (parser, template_keyword_p,
5719 /*check_dependency_p=*/true,
5720 declarator_p,
5721 optional_p);
5724 /* Parse an unqualified-id.
5726 unqualified-id:
5727 identifier
5728 operator-function-id
5729 conversion-function-id
5730 ~ class-name
5731 template-id
5733 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5734 keyword, in a construct like `A::template ...'.
5736 Returns a representation of unqualified-id. For the `identifier'
5737 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5738 production a BIT_NOT_EXPR is returned; the operand of the
5739 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5740 other productions, see the documentation accompanying the
5741 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5742 names are looked up in uninstantiated templates. If DECLARATOR_P
5743 is true, the unqualified-id is appearing as part of a declarator,
5744 rather than as part of an expression. */
5746 static cp_expr
5747 cp_parser_unqualified_id (cp_parser* parser,
5748 bool template_keyword_p,
5749 bool check_dependency_p,
5750 bool declarator_p,
5751 bool optional_p)
5753 cp_token *token;
5755 /* Peek at the next token. */
5756 token = cp_lexer_peek_token (parser->lexer);
5758 switch ((int) token->type)
5760 case CPP_NAME:
5762 tree id;
5764 /* We don't know yet whether or not this will be a
5765 template-id. */
5766 cp_parser_parse_tentatively (parser);
5767 /* Try a template-id. */
5768 id = cp_parser_template_id (parser, template_keyword_p,
5769 check_dependency_p,
5770 none_type,
5771 declarator_p);
5772 /* If it worked, we're done. */
5773 if (cp_parser_parse_definitely (parser))
5774 return id;
5775 /* Otherwise, it's an ordinary identifier. */
5776 return cp_parser_identifier (parser);
5779 case CPP_TEMPLATE_ID:
5780 return cp_parser_template_id (parser, template_keyword_p,
5781 check_dependency_p,
5782 none_type,
5783 declarator_p);
5785 case CPP_COMPL:
5787 tree type_decl;
5788 tree qualifying_scope;
5789 tree object_scope;
5790 tree scope;
5791 bool done;
5793 /* Consume the `~' token. */
5794 cp_lexer_consume_token (parser->lexer);
5795 /* Parse the class-name. The standard, as written, seems to
5796 say that:
5798 template <typename T> struct S { ~S (); };
5799 template <typename T> S<T>::~S() {}
5801 is invalid, since `~' must be followed by a class-name, but
5802 `S<T>' is dependent, and so not known to be a class.
5803 That's not right; we need to look in uninstantiated
5804 templates. A further complication arises from:
5806 template <typename T> void f(T t) {
5807 t.T::~T();
5810 Here, it is not possible to look up `T' in the scope of `T'
5811 itself. We must look in both the current scope, and the
5812 scope of the containing complete expression.
5814 Yet another issue is:
5816 struct S {
5817 int S;
5818 ~S();
5821 S::~S() {}
5823 The standard does not seem to say that the `S' in `~S'
5824 should refer to the type `S' and not the data member
5825 `S::S'. */
5827 /* DR 244 says that we look up the name after the "~" in the
5828 same scope as we looked up the qualifying name. That idea
5829 isn't fully worked out; it's more complicated than that. */
5830 scope = parser->scope;
5831 object_scope = parser->object_scope;
5832 qualifying_scope = parser->qualifying_scope;
5834 /* Check for invalid scopes. */
5835 if (scope == error_mark_node)
5837 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5838 cp_lexer_consume_token (parser->lexer);
5839 return error_mark_node;
5841 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5843 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5844 error_at (token->location,
5845 "scope %qT before %<~%> is not a class-name",
5846 scope);
5847 cp_parser_simulate_error (parser);
5848 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5849 cp_lexer_consume_token (parser->lexer);
5850 return error_mark_node;
5852 gcc_assert (!scope || TYPE_P (scope));
5854 /* If the name is of the form "X::~X" it's OK even if X is a
5855 typedef. */
5856 token = cp_lexer_peek_token (parser->lexer);
5857 if (scope
5858 && token->type == CPP_NAME
5859 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5860 != CPP_LESS)
5861 && (token->u.value == TYPE_IDENTIFIER (scope)
5862 || (CLASS_TYPE_P (scope)
5863 && constructor_name_p (token->u.value, scope))))
5865 cp_lexer_consume_token (parser->lexer);
5866 return build_nt (BIT_NOT_EXPR, scope);
5869 /* ~auto means the destructor of whatever the object is. */
5870 if (cp_parser_is_keyword (token, RID_AUTO))
5872 if (cxx_dialect < cxx14)
5873 pedwarn (input_location, 0,
5874 "%<~auto%> only available with "
5875 "-std=c++14 or -std=gnu++14");
5876 cp_lexer_consume_token (parser->lexer);
5877 return build_nt (BIT_NOT_EXPR, make_auto ());
5880 /* If there was an explicit qualification (S::~T), first look
5881 in the scope given by the qualification (i.e., S).
5883 Note: in the calls to cp_parser_class_name below we pass
5884 typename_type so that lookup finds the injected-class-name
5885 rather than the constructor. */
5886 done = false;
5887 type_decl = NULL_TREE;
5888 if (scope)
5890 cp_parser_parse_tentatively (parser);
5891 type_decl = cp_parser_class_name (parser,
5892 /*typename_keyword_p=*/false,
5893 /*template_keyword_p=*/false,
5894 typename_type,
5895 /*check_dependency=*/false,
5896 /*class_head_p=*/false,
5897 declarator_p);
5898 if (cp_parser_parse_definitely (parser))
5899 done = true;
5901 /* In "N::S::~S", look in "N" as well. */
5902 if (!done && scope && qualifying_scope)
5904 cp_parser_parse_tentatively (parser);
5905 parser->scope = qualifying_scope;
5906 parser->object_scope = NULL_TREE;
5907 parser->qualifying_scope = NULL_TREE;
5908 type_decl
5909 = cp_parser_class_name (parser,
5910 /*typename_keyword_p=*/false,
5911 /*template_keyword_p=*/false,
5912 typename_type,
5913 /*check_dependency=*/false,
5914 /*class_head_p=*/false,
5915 declarator_p);
5916 if (cp_parser_parse_definitely (parser))
5917 done = true;
5919 /* In "p->S::~T", look in the scope given by "*p" as well. */
5920 else if (!done && object_scope)
5922 cp_parser_parse_tentatively (parser);
5923 parser->scope = object_scope;
5924 parser->object_scope = NULL_TREE;
5925 parser->qualifying_scope = NULL_TREE;
5926 type_decl
5927 = cp_parser_class_name (parser,
5928 /*typename_keyword_p=*/false,
5929 /*template_keyword_p=*/false,
5930 typename_type,
5931 /*check_dependency=*/false,
5932 /*class_head_p=*/false,
5933 declarator_p);
5934 if (cp_parser_parse_definitely (parser))
5935 done = true;
5937 /* Look in the surrounding context. */
5938 if (!done)
5940 parser->scope = NULL_TREE;
5941 parser->object_scope = NULL_TREE;
5942 parser->qualifying_scope = NULL_TREE;
5943 if (processing_template_decl)
5944 cp_parser_parse_tentatively (parser);
5945 type_decl
5946 = cp_parser_class_name (parser,
5947 /*typename_keyword_p=*/false,
5948 /*template_keyword_p=*/false,
5949 typename_type,
5950 /*check_dependency=*/false,
5951 /*class_head_p=*/false,
5952 declarator_p);
5953 if (processing_template_decl
5954 && ! cp_parser_parse_definitely (parser))
5956 /* We couldn't find a type with this name. If we're parsing
5957 tentatively, fail and try something else. */
5958 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5960 cp_parser_simulate_error (parser);
5961 return error_mark_node;
5963 /* Otherwise, accept it and check for a match at instantiation
5964 time. */
5965 type_decl = cp_parser_identifier (parser);
5966 if (type_decl != error_mark_node)
5967 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5968 return type_decl;
5971 /* If an error occurred, assume that the name of the
5972 destructor is the same as the name of the qualifying
5973 class. That allows us to keep parsing after running
5974 into ill-formed destructor names. */
5975 if (type_decl == error_mark_node && scope)
5976 return build_nt (BIT_NOT_EXPR, scope);
5977 else if (type_decl == error_mark_node)
5978 return error_mark_node;
5980 /* Check that destructor name and scope match. */
5981 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5983 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5984 error_at (token->location,
5985 "declaration of %<~%T%> as member of %qT",
5986 type_decl, scope);
5987 cp_parser_simulate_error (parser);
5988 return error_mark_node;
5991 /* [class.dtor]
5993 A typedef-name that names a class shall not be used as the
5994 identifier in the declarator for a destructor declaration. */
5995 if (declarator_p
5996 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5997 && !DECL_SELF_REFERENCE_P (type_decl)
5998 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5999 error_at (token->location,
6000 "typedef-name %qD used as destructor declarator",
6001 type_decl);
6003 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6006 case CPP_KEYWORD:
6007 if (token->keyword == RID_OPERATOR)
6009 cp_expr id;
6011 /* This could be a template-id, so we try that first. */
6012 cp_parser_parse_tentatively (parser);
6013 /* Try a template-id. */
6014 id = cp_parser_template_id (parser, template_keyword_p,
6015 /*check_dependency_p=*/true,
6016 none_type,
6017 declarator_p);
6018 /* If that worked, we're done. */
6019 if (cp_parser_parse_definitely (parser))
6020 return id;
6021 /* We still don't know whether we're looking at an
6022 operator-function-id or a conversion-function-id. */
6023 cp_parser_parse_tentatively (parser);
6024 /* Try an operator-function-id. */
6025 id = cp_parser_operator_function_id (parser);
6026 /* If that didn't work, try a conversion-function-id. */
6027 if (!cp_parser_parse_definitely (parser))
6028 id = cp_parser_conversion_function_id (parser);
6029 else if (UDLIT_OPER_P (id))
6031 /* 17.6.3.3.5 */
6032 const char *name = UDLIT_OP_SUFFIX (id);
6033 if (name[0] != '_' && !in_system_header_at (input_location)
6034 && declarator_p)
6035 warning (OPT_Wliteral_suffix,
6036 "literal operator suffixes not preceded by %<_%>"
6037 " are reserved for future standardization");
6040 return id;
6042 /* Fall through. */
6044 default:
6045 if (optional_p)
6046 return NULL_TREE;
6047 cp_parser_error (parser, "expected unqualified-id");
6048 return error_mark_node;
6052 /* Parse an (optional) nested-name-specifier.
6054 nested-name-specifier: [C++98]
6055 class-or-namespace-name :: nested-name-specifier [opt]
6056 class-or-namespace-name :: template nested-name-specifier [opt]
6058 nested-name-specifier: [C++0x]
6059 type-name ::
6060 namespace-name ::
6061 nested-name-specifier identifier ::
6062 nested-name-specifier template [opt] simple-template-id ::
6064 PARSER->SCOPE should be set appropriately before this function is
6065 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6066 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6067 in name lookups.
6069 Sets PARSER->SCOPE to the class (TYPE) or namespace
6070 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6071 it unchanged if there is no nested-name-specifier. Returns the new
6072 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6074 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6075 part of a declaration and/or decl-specifier. */
6077 static tree
6078 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6079 bool typename_keyword_p,
6080 bool check_dependency_p,
6081 bool type_p,
6082 bool is_declaration,
6083 bool template_keyword_p /* = false */)
6085 bool success = false;
6086 cp_token_position start = 0;
6087 cp_token *token;
6089 /* Remember where the nested-name-specifier starts. */
6090 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6092 start = cp_lexer_token_position (parser->lexer, false);
6093 push_deferring_access_checks (dk_deferred);
6096 while (true)
6098 tree new_scope;
6099 tree old_scope;
6100 tree saved_qualifying_scope;
6102 /* Spot cases that cannot be the beginning of a
6103 nested-name-specifier. */
6104 token = cp_lexer_peek_token (parser->lexer);
6106 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6107 the already parsed nested-name-specifier. */
6108 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6110 /* Grab the nested-name-specifier and continue the loop. */
6111 cp_parser_pre_parsed_nested_name_specifier (parser);
6112 /* If we originally encountered this nested-name-specifier
6113 with IS_DECLARATION set to false, we will not have
6114 resolved TYPENAME_TYPEs, so we must do so here. */
6115 if (is_declaration
6116 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6118 new_scope = resolve_typename_type (parser->scope,
6119 /*only_current_p=*/false);
6120 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6121 parser->scope = new_scope;
6123 success = true;
6124 continue;
6127 /* Spot cases that cannot be the beginning of a
6128 nested-name-specifier. On the second and subsequent times
6129 through the loop, we look for the `template' keyword. */
6130 if (success && token->keyword == RID_TEMPLATE)
6132 /* A template-id can start a nested-name-specifier. */
6133 else if (token->type == CPP_TEMPLATE_ID)
6135 /* DR 743: decltype can be used in a nested-name-specifier. */
6136 else if (token_is_decltype (token))
6138 else
6140 /* If the next token is not an identifier, then it is
6141 definitely not a type-name or namespace-name. */
6142 if (token->type != CPP_NAME)
6143 break;
6144 /* If the following token is neither a `<' (to begin a
6145 template-id), nor a `::', then we are not looking at a
6146 nested-name-specifier. */
6147 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6149 if (token->type == CPP_COLON
6150 && parser->colon_corrects_to_scope_p
6151 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6153 gcc_rich_location richloc (token->location);
6154 richloc.add_fixit_replace ("::");
6155 error_at (&richloc,
6156 "found %<:%> in nested-name-specifier, "
6157 "expected %<::%>");
6158 token->type = CPP_SCOPE;
6161 if (token->type != CPP_SCOPE
6162 && !cp_parser_nth_token_starts_template_argument_list_p
6163 (parser, 2))
6164 break;
6167 /* The nested-name-specifier is optional, so we parse
6168 tentatively. */
6169 cp_parser_parse_tentatively (parser);
6171 /* Look for the optional `template' keyword, if this isn't the
6172 first time through the loop. */
6173 if (success)
6174 template_keyword_p = cp_parser_optional_template_keyword (parser);
6176 /* Save the old scope since the name lookup we are about to do
6177 might destroy it. */
6178 old_scope = parser->scope;
6179 saved_qualifying_scope = parser->qualifying_scope;
6180 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6181 look up names in "X<T>::I" in order to determine that "Y" is
6182 a template. So, if we have a typename at this point, we make
6183 an effort to look through it. */
6184 if (is_declaration
6185 && !typename_keyword_p
6186 && parser->scope
6187 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6188 parser->scope = resolve_typename_type (parser->scope,
6189 /*only_current_p=*/false);
6190 /* Parse the qualifying entity. */
6191 new_scope
6192 = cp_parser_qualifying_entity (parser,
6193 typename_keyword_p,
6194 template_keyword_p,
6195 check_dependency_p,
6196 type_p,
6197 is_declaration);
6198 /* Look for the `::' token. */
6199 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6201 /* If we found what we wanted, we keep going; otherwise, we're
6202 done. */
6203 if (!cp_parser_parse_definitely (parser))
6205 bool error_p = false;
6207 /* Restore the OLD_SCOPE since it was valid before the
6208 failed attempt at finding the last
6209 class-or-namespace-name. */
6210 parser->scope = old_scope;
6211 parser->qualifying_scope = saved_qualifying_scope;
6213 /* If the next token is a decltype, and the one after that is a
6214 `::', then the decltype has failed to resolve to a class or
6215 enumeration type. Give this error even when parsing
6216 tentatively since it can't possibly be valid--and we're going
6217 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6218 won't get another chance.*/
6219 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6220 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6221 == CPP_SCOPE))
6223 token = cp_lexer_consume_token (parser->lexer);
6224 error_at (token->location, "decltype evaluates to %qT, "
6225 "which is not a class or enumeration type",
6226 token->u.tree_check_value->value);
6227 parser->scope = error_mark_node;
6228 error_p = true;
6229 /* As below. */
6230 success = true;
6231 cp_lexer_consume_token (parser->lexer);
6234 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6235 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6237 /* If we have a non-type template-id followed by ::, it can't
6238 possibly be valid. */
6239 token = cp_lexer_peek_token (parser->lexer);
6240 tree tid = token->u.tree_check_value->value;
6241 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6242 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6244 tree tmpl = NULL_TREE;
6245 if (is_overloaded_fn (tid))
6247 tree fns = get_fns (tid);
6248 if (OVL_SINGLE_P (fns))
6249 tmpl = OVL_FIRST (fns);
6250 error_at (token->location, "function template-id %qD "
6251 "in nested-name-specifier", tid);
6253 else
6255 /* Variable template. */
6256 tmpl = TREE_OPERAND (tid, 0);
6257 gcc_assert (variable_template_p (tmpl));
6258 error_at (token->location, "variable template-id %qD "
6259 "in nested-name-specifier", tid);
6261 if (tmpl)
6262 inform (DECL_SOURCE_LOCATION (tmpl),
6263 "%qD declared here", tmpl);
6265 parser->scope = error_mark_node;
6266 error_p = true;
6267 /* As below. */
6268 success = true;
6269 cp_lexer_consume_token (parser->lexer);
6270 cp_lexer_consume_token (parser->lexer);
6274 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6275 break;
6276 /* If the next token is an identifier, and the one after
6277 that is a `::', then any valid interpretation would have
6278 found a class-or-namespace-name. */
6279 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6280 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6281 == CPP_SCOPE)
6282 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6283 != CPP_COMPL))
6285 token = cp_lexer_consume_token (parser->lexer);
6286 if (!error_p)
6288 if (!token->error_reported)
6290 tree decl;
6291 tree ambiguous_decls;
6293 decl = cp_parser_lookup_name (parser, token->u.value,
6294 none_type,
6295 /*is_template=*/false,
6296 /*is_namespace=*/false,
6297 /*check_dependency=*/true,
6298 &ambiguous_decls,
6299 token->location);
6300 if (TREE_CODE (decl) == TEMPLATE_DECL)
6301 error_at (token->location,
6302 "%qD used without template parameters",
6303 decl);
6304 else if (ambiguous_decls)
6306 // cp_parser_lookup_name has the same diagnostic,
6307 // thus make sure to emit it at most once.
6308 if (cp_parser_uncommitted_to_tentative_parse_p
6309 (parser))
6311 error_at (token->location,
6312 "reference to %qD is ambiguous",
6313 token->u.value);
6314 print_candidates (ambiguous_decls);
6316 decl = error_mark_node;
6318 else
6320 if (cxx_dialect != cxx98)
6321 cp_parser_name_lookup_error
6322 (parser, token->u.value, decl, NLE_NOT_CXX98,
6323 token->location);
6324 else
6325 cp_parser_name_lookup_error
6326 (parser, token->u.value, decl, NLE_CXX98,
6327 token->location);
6330 parser->scope = error_mark_node;
6331 error_p = true;
6332 /* Treat this as a successful nested-name-specifier
6333 due to:
6335 [basic.lookup.qual]
6337 If the name found is not a class-name (clause
6338 _class_) or namespace-name (_namespace.def_), the
6339 program is ill-formed. */
6340 success = true;
6342 cp_lexer_consume_token (parser->lexer);
6344 break;
6346 /* We've found one valid nested-name-specifier. */
6347 success = true;
6348 /* Name lookup always gives us a DECL. */
6349 if (TREE_CODE (new_scope) == TYPE_DECL)
6350 new_scope = TREE_TYPE (new_scope);
6351 /* Uses of "template" must be followed by actual templates. */
6352 if (template_keyword_p
6353 && !(CLASS_TYPE_P (new_scope)
6354 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6355 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6356 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6357 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6358 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6359 == TEMPLATE_ID_EXPR)))
6360 permerror (input_location, TYPE_P (new_scope)
6361 ? G_("%qT is not a template")
6362 : G_("%qD is not a template"),
6363 new_scope);
6364 /* If it is a class scope, try to complete it; we are about to
6365 be looking up names inside the class. */
6366 if (TYPE_P (new_scope)
6367 /* Since checking types for dependency can be expensive,
6368 avoid doing it if the type is already complete. */
6369 && !COMPLETE_TYPE_P (new_scope)
6370 /* Do not try to complete dependent types. */
6371 && !dependent_type_p (new_scope))
6373 new_scope = complete_type (new_scope);
6374 /* If it is a typedef to current class, use the current
6375 class instead, as the typedef won't have any names inside
6376 it yet. */
6377 if (!COMPLETE_TYPE_P (new_scope)
6378 && currently_open_class (new_scope))
6379 new_scope = TYPE_MAIN_VARIANT (new_scope);
6381 /* Make sure we look in the right scope the next time through
6382 the loop. */
6383 parser->scope = new_scope;
6386 /* If parsing tentatively, replace the sequence of tokens that makes
6387 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6388 token. That way, should we re-parse the token stream, we will
6389 not have to repeat the effort required to do the parse, nor will
6390 we issue duplicate error messages. */
6391 if (success && start)
6393 cp_token *token;
6395 token = cp_lexer_token_at (parser->lexer, start);
6396 /* Reset the contents of the START token. */
6397 token->type = CPP_NESTED_NAME_SPECIFIER;
6398 /* Retrieve any deferred checks. Do not pop this access checks yet
6399 so the memory will not be reclaimed during token replacing below. */
6400 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6401 token->u.tree_check_value->value = parser->scope;
6402 token->u.tree_check_value->checks = get_deferred_access_checks ();
6403 token->u.tree_check_value->qualifying_scope =
6404 parser->qualifying_scope;
6405 token->keyword = RID_MAX;
6407 /* Purge all subsequent tokens. */
6408 cp_lexer_purge_tokens_after (parser->lexer, start);
6411 if (start)
6412 pop_to_parent_deferring_access_checks ();
6414 return success ? parser->scope : NULL_TREE;
6417 /* Parse a nested-name-specifier. See
6418 cp_parser_nested_name_specifier_opt for details. This function
6419 behaves identically, except that it will an issue an error if no
6420 nested-name-specifier is present. */
6422 static tree
6423 cp_parser_nested_name_specifier (cp_parser *parser,
6424 bool typename_keyword_p,
6425 bool check_dependency_p,
6426 bool type_p,
6427 bool is_declaration)
6429 tree scope;
6431 /* Look for the nested-name-specifier. */
6432 scope = cp_parser_nested_name_specifier_opt (parser,
6433 typename_keyword_p,
6434 check_dependency_p,
6435 type_p,
6436 is_declaration);
6437 /* If it was not present, issue an error message. */
6438 if (!scope)
6440 cp_parser_error (parser, "expected nested-name-specifier");
6441 parser->scope = NULL_TREE;
6444 return scope;
6447 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6448 this is either a class-name or a namespace-name (which corresponds
6449 to the class-or-namespace-name production in the grammar). For
6450 C++0x, it can also be a type-name that refers to an enumeration
6451 type or a simple-template-id.
6453 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6454 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6455 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6456 TYPE_P is TRUE iff the next name should be taken as a class-name,
6457 even the same name is declared to be another entity in the same
6458 scope.
6460 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6461 specified by the class-or-namespace-name. If neither is found the
6462 ERROR_MARK_NODE is returned. */
6464 static tree
6465 cp_parser_qualifying_entity (cp_parser *parser,
6466 bool typename_keyword_p,
6467 bool template_keyword_p,
6468 bool check_dependency_p,
6469 bool type_p,
6470 bool is_declaration)
6472 tree saved_scope;
6473 tree saved_qualifying_scope;
6474 tree saved_object_scope;
6475 tree scope;
6476 bool only_class_p;
6477 bool successful_parse_p;
6479 /* DR 743: decltype can appear in a nested-name-specifier. */
6480 if (cp_lexer_next_token_is_decltype (parser->lexer))
6482 scope = cp_parser_decltype (parser);
6483 if (TREE_CODE (scope) != ENUMERAL_TYPE
6484 && !MAYBE_CLASS_TYPE_P (scope))
6486 cp_parser_simulate_error (parser);
6487 return error_mark_node;
6489 if (TYPE_NAME (scope))
6490 scope = TYPE_NAME (scope);
6491 return scope;
6494 /* Before we try to parse the class-name, we must save away the
6495 current PARSER->SCOPE since cp_parser_class_name will destroy
6496 it. */
6497 saved_scope = parser->scope;
6498 saved_qualifying_scope = parser->qualifying_scope;
6499 saved_object_scope = parser->object_scope;
6500 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6501 there is no need to look for a namespace-name. */
6502 only_class_p = template_keyword_p
6503 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6504 if (!only_class_p)
6505 cp_parser_parse_tentatively (parser);
6506 scope = cp_parser_class_name (parser,
6507 typename_keyword_p,
6508 template_keyword_p,
6509 type_p ? class_type : none_type,
6510 check_dependency_p,
6511 /*class_head_p=*/false,
6512 is_declaration,
6513 /*enum_ok=*/cxx_dialect > cxx98);
6514 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6515 /* If that didn't work, try for a namespace-name. */
6516 if (!only_class_p && !successful_parse_p)
6518 /* Restore the saved scope. */
6519 parser->scope = saved_scope;
6520 parser->qualifying_scope = saved_qualifying_scope;
6521 parser->object_scope = saved_object_scope;
6522 /* If we are not looking at an identifier followed by the scope
6523 resolution operator, then this is not part of a
6524 nested-name-specifier. (Note that this function is only used
6525 to parse the components of a nested-name-specifier.) */
6526 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6527 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6528 return error_mark_node;
6529 scope = cp_parser_namespace_name (parser);
6532 return scope;
6535 /* Return true if we are looking at a compound-literal, false otherwise. */
6537 static bool
6538 cp_parser_compound_literal_p (cp_parser *parser)
6540 cp_lexer_save_tokens (parser->lexer);
6542 /* Skip tokens until the next token is a closing parenthesis.
6543 If we find the closing `)', and the next token is a `{', then
6544 we are looking at a compound-literal. */
6545 bool compound_literal_p
6546 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6547 /*consume_paren=*/true)
6548 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6550 /* Roll back the tokens we skipped. */
6551 cp_lexer_rollback_tokens (parser->lexer);
6553 return compound_literal_p;
6556 /* Parse a postfix-expression.
6558 postfix-expression:
6559 primary-expression
6560 postfix-expression [ expression ]
6561 postfix-expression ( expression-list [opt] )
6562 simple-type-specifier ( expression-list [opt] )
6563 typename :: [opt] nested-name-specifier identifier
6564 ( expression-list [opt] )
6565 typename :: [opt] nested-name-specifier template [opt] template-id
6566 ( expression-list [opt] )
6567 postfix-expression . template [opt] id-expression
6568 postfix-expression -> template [opt] id-expression
6569 postfix-expression . pseudo-destructor-name
6570 postfix-expression -> pseudo-destructor-name
6571 postfix-expression ++
6572 postfix-expression --
6573 dynamic_cast < type-id > ( expression )
6574 static_cast < type-id > ( expression )
6575 reinterpret_cast < type-id > ( expression )
6576 const_cast < type-id > ( expression )
6577 typeid ( expression )
6578 typeid ( type-id )
6580 GNU Extension:
6582 postfix-expression:
6583 ( type-id ) { initializer-list , [opt] }
6585 This extension is a GNU version of the C99 compound-literal
6586 construct. (The C99 grammar uses `type-name' instead of `type-id',
6587 but they are essentially the same concept.)
6589 If ADDRESS_P is true, the postfix expression is the operand of the
6590 `&' operator. CAST_P is true if this expression is the target of a
6591 cast.
6593 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6594 class member access expressions [expr.ref].
6596 Returns a representation of the expression. */
6598 static cp_expr
6599 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6600 bool member_access_only_p, bool decltype_p,
6601 cp_id_kind * pidk_return)
6603 cp_token *token;
6604 location_t loc;
6605 enum rid keyword;
6606 cp_id_kind idk = CP_ID_KIND_NONE;
6607 cp_expr postfix_expression = NULL_TREE;
6608 bool is_member_access = false;
6610 /* Peek at the next token. */
6611 token = cp_lexer_peek_token (parser->lexer);
6612 loc = token->location;
6613 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6615 /* Some of the productions are determined by keywords. */
6616 keyword = token->keyword;
6617 switch (keyword)
6619 case RID_DYNCAST:
6620 case RID_STATCAST:
6621 case RID_REINTCAST:
6622 case RID_CONSTCAST:
6624 tree type;
6625 cp_expr expression;
6626 const char *saved_message;
6627 bool saved_in_type_id_in_expr_p;
6629 /* All of these can be handled in the same way from the point
6630 of view of parsing. Begin by consuming the token
6631 identifying the cast. */
6632 cp_lexer_consume_token (parser->lexer);
6634 /* New types cannot be defined in the cast. */
6635 saved_message = parser->type_definition_forbidden_message;
6636 parser->type_definition_forbidden_message
6637 = G_("types may not be defined in casts");
6639 /* Look for the opening `<'. */
6640 cp_parser_require (parser, CPP_LESS, RT_LESS);
6641 /* Parse the type to which we are casting. */
6642 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6643 parser->in_type_id_in_expr_p = true;
6644 type = cp_parser_type_id (parser);
6645 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6646 /* Look for the closing `>'. */
6647 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6648 /* Restore the old message. */
6649 parser->type_definition_forbidden_message = saved_message;
6651 bool saved_greater_than_is_operator_p
6652 = parser->greater_than_is_operator_p;
6653 parser->greater_than_is_operator_p = true;
6655 /* And the expression which is being cast. */
6656 matching_parens parens;
6657 parens.require_open (parser);
6658 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6659 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6660 RT_CLOSE_PAREN);
6661 location_t end_loc = close_paren ?
6662 close_paren->location : UNKNOWN_LOCATION;
6664 parser->greater_than_is_operator_p
6665 = saved_greater_than_is_operator_p;
6667 /* Only type conversions to integral or enumeration types
6668 can be used in constant-expressions. */
6669 if (!cast_valid_in_integral_constant_expression_p (type)
6670 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6672 postfix_expression = error_mark_node;
6673 break;
6676 switch (keyword)
6678 case RID_DYNCAST:
6679 postfix_expression
6680 = build_dynamic_cast (type, expression, tf_warning_or_error);
6681 break;
6682 case RID_STATCAST:
6683 postfix_expression
6684 = build_static_cast (type, expression, tf_warning_or_error);
6685 break;
6686 case RID_REINTCAST:
6687 postfix_expression
6688 = build_reinterpret_cast (type, expression,
6689 tf_warning_or_error);
6690 break;
6691 case RID_CONSTCAST:
6692 postfix_expression
6693 = build_const_cast (type, expression, tf_warning_or_error);
6694 break;
6695 default:
6696 gcc_unreachable ();
6699 /* Construct a location e.g. :
6700 reinterpret_cast <int *> (expr)
6701 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6702 ranging from the start of the "*_cast" token to the final closing
6703 paren, with the caret at the start. */
6704 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6705 postfix_expression.set_location (cp_cast_loc);
6707 break;
6709 case RID_TYPEID:
6711 tree type;
6712 const char *saved_message;
6713 bool saved_in_type_id_in_expr_p;
6715 /* Consume the `typeid' token. */
6716 cp_lexer_consume_token (parser->lexer);
6717 /* Look for the `(' token. */
6718 matching_parens parens;
6719 parens.require_open (parser);
6720 /* Types cannot be defined in a `typeid' expression. */
6721 saved_message = parser->type_definition_forbidden_message;
6722 parser->type_definition_forbidden_message
6723 = G_("types may not be defined in a %<typeid%> expression");
6724 /* We can't be sure yet whether we're looking at a type-id or an
6725 expression. */
6726 cp_parser_parse_tentatively (parser);
6727 /* Try a type-id first. */
6728 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6729 parser->in_type_id_in_expr_p = true;
6730 type = cp_parser_type_id (parser);
6731 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6732 /* Look for the `)' token. Otherwise, we can't be sure that
6733 we're not looking at an expression: consider `typeid (int
6734 (3))', for example. */
6735 cp_token *close_paren = parens.require_close (parser);
6736 /* If all went well, simply lookup the type-id. */
6737 if (cp_parser_parse_definitely (parser))
6738 postfix_expression = get_typeid (type, tf_warning_or_error);
6739 /* Otherwise, fall back to the expression variant. */
6740 else
6742 tree expression;
6744 /* Look for an expression. */
6745 expression = cp_parser_expression (parser, & idk);
6746 /* Compute its typeid. */
6747 postfix_expression = build_typeid (expression, tf_warning_or_error);
6748 /* Look for the `)' token. */
6749 close_paren = parens.require_close (parser);
6751 /* Restore the saved message. */
6752 parser->type_definition_forbidden_message = saved_message;
6753 /* `typeid' may not appear in an integral constant expression. */
6754 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6755 postfix_expression = error_mark_node;
6757 /* Construct a location e.g. :
6758 typeid (expr)
6759 ^~~~~~~~~~~~~
6760 ranging from the start of the "typeid" token to the final closing
6761 paren, with the caret at the start. */
6762 if (close_paren)
6764 location_t typeid_loc
6765 = make_location (start_loc, start_loc, close_paren->location);
6766 postfix_expression.set_location (typeid_loc);
6769 break;
6771 case RID_TYPENAME:
6773 tree type;
6774 /* The syntax permitted here is the same permitted for an
6775 elaborated-type-specifier. */
6776 ++parser->prevent_constrained_type_specifiers;
6777 type = cp_parser_elaborated_type_specifier (parser,
6778 /*is_friend=*/false,
6779 /*is_declaration=*/false);
6780 --parser->prevent_constrained_type_specifiers;
6781 postfix_expression = cp_parser_functional_cast (parser, type);
6783 break;
6785 case RID_ADDRESSOF:
6786 case RID_BUILTIN_SHUFFLE:
6787 case RID_BUILTIN_LAUNDER:
6789 vec<tree, va_gc> *vec;
6790 unsigned int i;
6791 tree p;
6793 cp_lexer_consume_token (parser->lexer);
6794 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6795 /*cast_p=*/false, /*allow_expansion_p=*/true,
6796 /*non_constant_p=*/NULL);
6797 if (vec == NULL)
6799 postfix_expression = error_mark_node;
6800 break;
6803 FOR_EACH_VEC_ELT (*vec, i, p)
6804 mark_exp_read (p);
6806 switch (keyword)
6808 case RID_ADDRESSOF:
6809 if (vec->length () == 1)
6810 postfix_expression
6811 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6812 else
6814 error_at (loc, "wrong number of arguments to "
6815 "%<__builtin_addressof%>");
6816 postfix_expression = error_mark_node;
6818 break;
6820 case RID_BUILTIN_LAUNDER:
6821 if (vec->length () == 1)
6822 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6823 tf_warning_or_error);
6824 else
6826 error_at (loc, "wrong number of arguments to "
6827 "%<__builtin_launder%>");
6828 postfix_expression = error_mark_node;
6830 break;
6832 case RID_BUILTIN_SHUFFLE:
6833 if (vec->length () == 2)
6834 postfix_expression
6835 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6836 (*vec)[1], tf_warning_or_error);
6837 else if (vec->length () == 3)
6838 postfix_expression
6839 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6840 (*vec)[2], tf_warning_or_error);
6841 else
6843 error_at (loc, "wrong number of arguments to "
6844 "%<__builtin_shuffle%>");
6845 postfix_expression = error_mark_node;
6847 break;
6849 default:
6850 gcc_unreachable ();
6852 break;
6855 default:
6857 tree type;
6859 /* If the next thing is a simple-type-specifier, we may be
6860 looking at a functional cast. We could also be looking at
6861 an id-expression. So, we try the functional cast, and if
6862 that doesn't work we fall back to the primary-expression. */
6863 cp_parser_parse_tentatively (parser);
6864 /* Look for the simple-type-specifier. */
6865 ++parser->prevent_constrained_type_specifiers;
6866 type = cp_parser_simple_type_specifier (parser,
6867 /*decl_specs=*/NULL,
6868 CP_PARSER_FLAGS_NONE);
6869 --parser->prevent_constrained_type_specifiers;
6870 /* Parse the cast itself. */
6871 if (!cp_parser_error_occurred (parser))
6872 postfix_expression
6873 = cp_parser_functional_cast (parser, type);
6874 /* If that worked, we're done. */
6875 if (cp_parser_parse_definitely (parser))
6876 break;
6878 /* If the functional-cast didn't work out, try a
6879 compound-literal. */
6880 if (cp_parser_allow_gnu_extensions_p (parser)
6881 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6883 cp_expr initializer = NULL_TREE;
6885 cp_parser_parse_tentatively (parser);
6887 matching_parens parens;
6888 parens.consume_open (parser);
6890 /* Avoid calling cp_parser_type_id pointlessly, see comment
6891 in cp_parser_cast_expression about c++/29234. */
6892 if (!cp_parser_compound_literal_p (parser))
6893 cp_parser_simulate_error (parser);
6894 else
6896 /* Parse the type. */
6897 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6898 parser->in_type_id_in_expr_p = true;
6899 type = cp_parser_type_id (parser);
6900 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6901 parens.require_close (parser);
6904 /* If things aren't going well, there's no need to
6905 keep going. */
6906 if (!cp_parser_error_occurred (parser))
6908 bool non_constant_p;
6909 /* Parse the brace-enclosed initializer list. */
6910 initializer = cp_parser_braced_list (parser,
6911 &non_constant_p);
6913 /* If that worked, we're definitely looking at a
6914 compound-literal expression. */
6915 if (cp_parser_parse_definitely (parser))
6917 /* Warn the user that a compound literal is not
6918 allowed in standard C++. */
6919 pedwarn (input_location, OPT_Wpedantic,
6920 "ISO C++ forbids compound-literals");
6921 /* For simplicity, we disallow compound literals in
6922 constant-expressions. We could
6923 allow compound literals of integer type, whose
6924 initializer was a constant, in constant
6925 expressions. Permitting that usage, as a further
6926 extension, would not change the meaning of any
6927 currently accepted programs. (Of course, as
6928 compound literals are not part of ISO C++, the
6929 standard has nothing to say.) */
6930 if (cp_parser_non_integral_constant_expression (parser,
6931 NIC_NCC))
6933 postfix_expression = error_mark_node;
6934 break;
6936 /* Form the representation of the compound-literal. */
6937 postfix_expression
6938 = finish_compound_literal (type, initializer,
6939 tf_warning_or_error, fcl_c99);
6940 postfix_expression.set_location (initializer.get_location ());
6941 break;
6945 /* It must be a primary-expression. */
6946 postfix_expression
6947 = cp_parser_primary_expression (parser, address_p, cast_p,
6948 /*template_arg_p=*/false,
6949 decltype_p,
6950 &idk);
6952 break;
6955 /* Note that we don't need to worry about calling build_cplus_new on a
6956 class-valued CALL_EXPR in decltype when it isn't the end of the
6957 postfix-expression; unary_complex_lvalue will take care of that for
6958 all these cases. */
6960 /* Keep looping until the postfix-expression is complete. */
6961 while (true)
6963 if (idk == CP_ID_KIND_UNQUALIFIED
6964 && identifier_p (postfix_expression)
6965 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6966 /* It is not a Koenig lookup function call. */
6967 postfix_expression
6968 = unqualified_name_lookup_error (postfix_expression);
6970 /* Peek at the next token. */
6971 token = cp_lexer_peek_token (parser->lexer);
6973 switch (token->type)
6975 case CPP_OPEN_SQUARE:
6976 if (cp_next_tokens_can_be_std_attribute_p (parser))
6978 cp_parser_error (parser,
6979 "two consecutive %<[%> shall "
6980 "only introduce an attribute");
6981 return error_mark_node;
6983 postfix_expression
6984 = cp_parser_postfix_open_square_expression (parser,
6985 postfix_expression,
6986 false,
6987 decltype_p);
6988 postfix_expression.set_range (start_loc,
6989 postfix_expression.get_location ());
6991 idk = CP_ID_KIND_NONE;
6992 is_member_access = false;
6993 break;
6995 case CPP_OPEN_PAREN:
6996 /* postfix-expression ( expression-list [opt] ) */
6998 bool koenig_p;
6999 bool is_builtin_constant_p;
7000 bool saved_integral_constant_expression_p = false;
7001 bool saved_non_integral_constant_expression_p = false;
7002 tsubst_flags_t complain = complain_flags (decltype_p);
7003 vec<tree, va_gc> *args;
7004 location_t close_paren_loc = UNKNOWN_LOCATION;
7006 is_member_access = false;
7008 is_builtin_constant_p
7009 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7010 if (is_builtin_constant_p)
7012 /* The whole point of __builtin_constant_p is to allow
7013 non-constant expressions to appear as arguments. */
7014 saved_integral_constant_expression_p
7015 = parser->integral_constant_expression_p;
7016 saved_non_integral_constant_expression_p
7017 = parser->non_integral_constant_expression_p;
7018 parser->integral_constant_expression_p = false;
7020 args = (cp_parser_parenthesized_expression_list
7021 (parser, non_attr,
7022 /*cast_p=*/false, /*allow_expansion_p=*/true,
7023 /*non_constant_p=*/NULL,
7024 /*close_paren_loc=*/&close_paren_loc));
7025 if (is_builtin_constant_p)
7027 parser->integral_constant_expression_p
7028 = saved_integral_constant_expression_p;
7029 parser->non_integral_constant_expression_p
7030 = saved_non_integral_constant_expression_p;
7033 if (args == NULL)
7035 postfix_expression = error_mark_node;
7036 break;
7039 /* Function calls are not permitted in
7040 constant-expressions. */
7041 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7042 && cp_parser_non_integral_constant_expression (parser,
7043 NIC_FUNC_CALL))
7045 postfix_expression = error_mark_node;
7046 release_tree_vector (args);
7047 break;
7050 koenig_p = false;
7051 if (idk == CP_ID_KIND_UNQUALIFIED
7052 || idk == CP_ID_KIND_TEMPLATE_ID)
7054 if (identifier_p (postfix_expression))
7056 if (!args->is_empty ())
7058 koenig_p = true;
7059 if (!any_type_dependent_arguments_p (args))
7060 postfix_expression
7061 = perform_koenig_lookup (postfix_expression, args,
7062 complain);
7064 else
7065 postfix_expression
7066 = unqualified_fn_lookup_error (postfix_expression);
7068 /* We do not perform argument-dependent lookup if
7069 normal lookup finds a non-function, in accordance
7070 with the expected resolution of DR 218. */
7071 else if (!args->is_empty ()
7072 && is_overloaded_fn (postfix_expression))
7074 tree fn = get_first_fn (postfix_expression);
7075 fn = STRIP_TEMPLATE (fn);
7077 /* Do not do argument dependent lookup if regular
7078 lookup finds a member function or a block-scope
7079 function declaration. [basic.lookup.argdep]/3 */
7080 if (!DECL_FUNCTION_MEMBER_P (fn)
7081 && !DECL_LOCAL_FUNCTION_P (fn))
7083 koenig_p = true;
7084 if (!any_type_dependent_arguments_p (args))
7085 postfix_expression
7086 = perform_koenig_lookup (postfix_expression, args,
7087 complain);
7092 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
7093 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
7094 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
7095 && vec_safe_length (args) == 3)
7097 tree arg0 = (*args)[0];
7098 tree arg1 = (*args)[1];
7099 tree arg2 = (*args)[2];
7100 int literal_mask = ((!!integer_zerop (arg1) << 1)
7101 | (!!integer_zerop (arg2) << 2));
7102 if (TREE_CODE (arg2) == CONST_DECL)
7103 arg2 = DECL_INITIAL (arg2);
7104 warn_for_memset (input_location, arg0, arg2, literal_mask);
7107 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7109 tree instance = TREE_OPERAND (postfix_expression, 0);
7110 tree fn = TREE_OPERAND (postfix_expression, 1);
7112 if (processing_template_decl
7113 && (type_dependent_object_expression_p (instance)
7114 || (!BASELINK_P (fn)
7115 && TREE_CODE (fn) != FIELD_DECL)
7116 || type_dependent_expression_p (fn)
7117 || any_type_dependent_arguments_p (args)))
7119 maybe_generic_this_capture (instance, fn);
7120 postfix_expression
7121 = build_min_nt_call_vec (postfix_expression, args);
7122 release_tree_vector (args);
7123 break;
7126 if (BASELINK_P (fn))
7128 postfix_expression
7129 = (build_new_method_call
7130 (instance, fn, &args, NULL_TREE,
7131 (idk == CP_ID_KIND_QUALIFIED
7132 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7133 : LOOKUP_NORMAL),
7134 /*fn_p=*/NULL,
7135 complain));
7137 else
7138 postfix_expression
7139 = finish_call_expr (postfix_expression, &args,
7140 /*disallow_virtual=*/false,
7141 /*koenig_p=*/false,
7142 complain);
7144 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7145 || TREE_CODE (postfix_expression) == MEMBER_REF
7146 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7147 postfix_expression = (build_offset_ref_call_from_tree
7148 (postfix_expression, &args,
7149 complain));
7150 else if (idk == CP_ID_KIND_QUALIFIED)
7151 /* A call to a static class member, or a namespace-scope
7152 function. */
7153 postfix_expression
7154 = finish_call_expr (postfix_expression, &args,
7155 /*disallow_virtual=*/true,
7156 koenig_p,
7157 complain);
7158 else
7159 /* All other function calls. */
7160 postfix_expression
7161 = finish_call_expr (postfix_expression, &args,
7162 /*disallow_virtual=*/false,
7163 koenig_p,
7164 complain);
7166 if (close_paren_loc != UNKNOWN_LOCATION)
7168 location_t combined_loc = make_location (token->location,
7169 start_loc,
7170 close_paren_loc);
7171 postfix_expression.set_location (combined_loc);
7174 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7175 idk = CP_ID_KIND_NONE;
7177 release_tree_vector (args);
7179 break;
7181 case CPP_DOT:
7182 case CPP_DEREF:
7183 /* postfix-expression . template [opt] id-expression
7184 postfix-expression . pseudo-destructor-name
7185 postfix-expression -> template [opt] id-expression
7186 postfix-expression -> pseudo-destructor-name */
7188 /* Consume the `.' or `->' operator. */
7189 cp_lexer_consume_token (parser->lexer);
7191 postfix_expression
7192 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7193 postfix_expression,
7194 false, &idk, loc);
7196 is_member_access = true;
7197 break;
7199 case CPP_PLUS_PLUS:
7200 /* postfix-expression ++ */
7201 /* Consume the `++' token. */
7202 cp_lexer_consume_token (parser->lexer);
7203 /* Generate a representation for the complete expression. */
7204 postfix_expression
7205 = finish_increment_expr (postfix_expression,
7206 POSTINCREMENT_EXPR);
7207 /* Increments may not appear in constant-expressions. */
7208 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7209 postfix_expression = error_mark_node;
7210 idk = CP_ID_KIND_NONE;
7211 is_member_access = false;
7212 break;
7214 case CPP_MINUS_MINUS:
7215 /* postfix-expression -- */
7216 /* Consume the `--' token. */
7217 cp_lexer_consume_token (parser->lexer);
7218 /* Generate a representation for the complete expression. */
7219 postfix_expression
7220 = finish_increment_expr (postfix_expression,
7221 POSTDECREMENT_EXPR);
7222 /* Decrements may not appear in constant-expressions. */
7223 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7224 postfix_expression = error_mark_node;
7225 idk = CP_ID_KIND_NONE;
7226 is_member_access = false;
7227 break;
7229 default:
7230 if (pidk_return != NULL)
7231 * pidk_return = idk;
7232 if (member_access_only_p)
7233 return is_member_access
7234 ? postfix_expression
7235 : cp_expr (error_mark_node);
7236 else
7237 return postfix_expression;
7241 /* We should never get here. */
7242 gcc_unreachable ();
7243 return error_mark_node;
7246 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7247 by cp_parser_builtin_offsetof. We're looking for
7249 postfix-expression [ expression ]
7250 postfix-expression [ braced-init-list ] (C++11)
7252 FOR_OFFSETOF is set if we're being called in that context, which
7253 changes how we deal with integer constant expressions. */
7255 static tree
7256 cp_parser_postfix_open_square_expression (cp_parser *parser,
7257 tree postfix_expression,
7258 bool for_offsetof,
7259 bool decltype_p)
7261 tree index = NULL_TREE;
7262 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7263 bool saved_greater_than_is_operator_p;
7265 /* Consume the `[' token. */
7266 cp_lexer_consume_token (parser->lexer);
7268 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7269 parser->greater_than_is_operator_p = true;
7271 /* Parse the index expression. */
7272 /* ??? For offsetof, there is a question of what to allow here. If
7273 offsetof is not being used in an integral constant expression context,
7274 then we *could* get the right answer by computing the value at runtime.
7275 If we are in an integral constant expression context, then we might
7276 could accept any constant expression; hard to say without analysis.
7277 Rather than open the barn door too wide right away, allow only integer
7278 constant expressions here. */
7279 if (for_offsetof)
7280 index = cp_parser_constant_expression (parser);
7281 else
7283 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7285 bool expr_nonconst_p;
7286 cp_lexer_set_source_position (parser->lexer);
7287 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7288 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7290 else
7291 index = cp_parser_expression (parser);
7294 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7296 /* Look for the closing `]'. */
7297 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7299 /* Build the ARRAY_REF. */
7300 postfix_expression = grok_array_decl (loc, postfix_expression,
7301 index, decltype_p);
7303 /* When not doing offsetof, array references are not permitted in
7304 constant-expressions. */
7305 if (!for_offsetof
7306 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7307 postfix_expression = error_mark_node;
7309 return postfix_expression;
7312 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7313 by cp_parser_builtin_offsetof. We're looking for
7315 postfix-expression . template [opt] id-expression
7316 postfix-expression . pseudo-destructor-name
7317 postfix-expression -> template [opt] id-expression
7318 postfix-expression -> pseudo-destructor-name
7320 FOR_OFFSETOF is set if we're being called in that context. That sorta
7321 limits what of the above we'll actually accept, but nevermind.
7322 TOKEN_TYPE is the "." or "->" token, which will already have been
7323 removed from the stream. */
7325 static tree
7326 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7327 enum cpp_ttype token_type,
7328 cp_expr postfix_expression,
7329 bool for_offsetof, cp_id_kind *idk,
7330 location_t location)
7332 tree name;
7333 bool dependent_p;
7334 bool pseudo_destructor_p;
7335 tree scope = NULL_TREE;
7336 location_t start_loc = postfix_expression.get_start ();
7338 /* If this is a `->' operator, dereference the pointer. */
7339 if (token_type == CPP_DEREF)
7340 postfix_expression = build_x_arrow (location, postfix_expression,
7341 tf_warning_or_error);
7342 /* Check to see whether or not the expression is type-dependent and
7343 not the current instantiation. */
7344 dependent_p = type_dependent_object_expression_p (postfix_expression);
7345 /* The identifier following the `->' or `.' is not qualified. */
7346 parser->scope = NULL_TREE;
7347 parser->qualifying_scope = NULL_TREE;
7348 parser->object_scope = NULL_TREE;
7349 *idk = CP_ID_KIND_NONE;
7351 /* Enter the scope corresponding to the type of the object
7352 given by the POSTFIX_EXPRESSION. */
7353 if (!dependent_p)
7355 scope = TREE_TYPE (postfix_expression);
7356 /* According to the standard, no expression should ever have
7357 reference type. Unfortunately, we do not currently match
7358 the standard in this respect in that our internal representation
7359 of an expression may have reference type even when the standard
7360 says it does not. Therefore, we have to manually obtain the
7361 underlying type here. */
7362 scope = non_reference (scope);
7363 /* The type of the POSTFIX_EXPRESSION must be complete. */
7364 /* Unlike the object expression in other contexts, *this is not
7365 required to be of complete type for purposes of class member
7366 access (5.2.5) outside the member function body. */
7367 if (postfix_expression != current_class_ref
7368 && scope != error_mark_node
7369 && !(processing_template_decl
7370 && current_class_type
7371 && (same_type_ignoring_top_level_qualifiers_p
7372 (scope, current_class_type))))
7374 scope = complete_type (scope);
7375 if (!COMPLETE_TYPE_P (scope)
7376 /* Avoid clobbering e.g. OVERLOADs or DECLs. */
7377 && EXPR_P (postfix_expression))
7379 /* In a template, be permissive by treating an object expression
7380 of incomplete type as dependent (after a pedwarn). */
7381 diagnostic_t kind = (processing_template_decl
7382 && MAYBE_CLASS_TYPE_P (scope)
7383 ? DK_PEDWARN
7384 : DK_ERROR);
7385 cxx_incomplete_type_diagnostic
7386 (location_of (postfix_expression),
7387 postfix_expression, scope, kind);
7388 if (!MAYBE_CLASS_TYPE_P (scope))
7389 return error_mark_node;
7390 if (processing_template_decl)
7392 dependent_p = true;
7393 scope = TREE_TYPE (postfix_expression) = NULL_TREE;
7398 if (!dependent_p)
7400 /* Let the name lookup machinery know that we are processing a
7401 class member access expression. */
7402 parser->context->object_type = scope;
7403 /* If something went wrong, we want to be able to discern that case,
7404 as opposed to the case where there was no SCOPE due to the type
7405 of expression being dependent. */
7406 if (!scope)
7407 scope = error_mark_node;
7408 /* If the SCOPE was erroneous, make the various semantic analysis
7409 functions exit quickly -- and without issuing additional error
7410 messages. */
7411 if (scope == error_mark_node)
7412 postfix_expression = error_mark_node;
7416 if (dependent_p)
7417 /* Tell cp_parser_lookup_name that there was an object, even though it's
7418 type-dependent. */
7419 parser->context->object_type = unknown_type_node;
7421 /* Assume this expression is not a pseudo-destructor access. */
7422 pseudo_destructor_p = false;
7424 /* If the SCOPE is a scalar type, then, if this is a valid program,
7425 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7426 is type dependent, it can be pseudo-destructor-name or something else.
7427 Try to parse it as pseudo-destructor-name first. */
7428 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7430 tree s;
7431 tree type;
7433 cp_parser_parse_tentatively (parser);
7434 /* Parse the pseudo-destructor-name. */
7435 s = NULL_TREE;
7436 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7437 &s, &type);
7438 if (dependent_p
7439 && (cp_parser_error_occurred (parser)
7440 || !SCALAR_TYPE_P (type)))
7441 cp_parser_abort_tentative_parse (parser);
7442 else if (cp_parser_parse_definitely (parser))
7444 pseudo_destructor_p = true;
7445 postfix_expression
7446 = finish_pseudo_destructor_expr (postfix_expression,
7447 s, type, location);
7451 if (!pseudo_destructor_p)
7453 /* If the SCOPE is not a scalar type, we are looking at an
7454 ordinary class member access expression, rather than a
7455 pseudo-destructor-name. */
7456 bool template_p;
7457 cp_token *token = cp_lexer_peek_token (parser->lexer);
7458 /* Parse the id-expression. */
7459 name = (cp_parser_id_expression
7460 (parser,
7461 cp_parser_optional_template_keyword (parser),
7462 /*check_dependency_p=*/true,
7463 &template_p,
7464 /*declarator_p=*/false,
7465 /*optional_p=*/false));
7466 /* In general, build a SCOPE_REF if the member name is qualified.
7467 However, if the name was not dependent and has already been
7468 resolved; there is no need to build the SCOPE_REF. For example;
7470 struct X { void f(); };
7471 template <typename T> void f(T* t) { t->X::f(); }
7473 Even though "t" is dependent, "X::f" is not and has been resolved
7474 to a BASELINK; there is no need to include scope information. */
7476 /* But we do need to remember that there was an explicit scope for
7477 virtual function calls. */
7478 if (parser->scope)
7479 *idk = CP_ID_KIND_QUALIFIED;
7481 /* If the name is a template-id that names a type, we will get a
7482 TYPE_DECL here. That is invalid code. */
7483 if (TREE_CODE (name) == TYPE_DECL)
7485 error_at (token->location, "invalid use of %qD", name);
7486 postfix_expression = error_mark_node;
7488 else
7490 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7492 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7494 error_at (token->location, "%<%D::%D%> is not a class member",
7495 parser->scope, name);
7496 postfix_expression = error_mark_node;
7498 else
7499 name = build_qualified_name (/*type=*/NULL_TREE,
7500 parser->scope,
7501 name,
7502 template_p);
7503 parser->scope = NULL_TREE;
7504 parser->qualifying_scope = NULL_TREE;
7505 parser->object_scope = NULL_TREE;
7507 if (parser->scope && name && BASELINK_P (name))
7508 adjust_result_of_qualified_name_lookup
7509 (name, parser->scope, scope);
7510 postfix_expression
7511 = finish_class_member_access_expr (postfix_expression, name,
7512 template_p,
7513 tf_warning_or_error);
7514 /* Build a location e.g.:
7515 ptr->access_expr
7516 ~~~^~~~~~~~~~~~~
7517 where the caret is at the deref token, ranging from
7518 the start of postfix_expression to the end of the access expr. */
7519 location_t end_loc
7520 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7521 location_t combined_loc
7522 = make_location (input_location, start_loc, end_loc);
7523 protected_set_expr_location (postfix_expression, combined_loc);
7527 /* We no longer need to look up names in the scope of the object on
7528 the left-hand side of the `.' or `->' operator. */
7529 parser->context->object_type = NULL_TREE;
7531 /* Outside of offsetof, these operators may not appear in
7532 constant-expressions. */
7533 if (!for_offsetof
7534 && (cp_parser_non_integral_constant_expression
7535 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7536 postfix_expression = error_mark_node;
7538 return postfix_expression;
7541 /* Parse a parenthesized expression-list.
7543 expression-list:
7544 assignment-expression
7545 expression-list, assignment-expression
7547 attribute-list:
7548 expression-list
7549 identifier
7550 identifier, expression-list
7552 CAST_P is true if this expression is the target of a cast.
7554 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7555 argument pack.
7557 Returns a vector of trees. Each element is a representation of an
7558 assignment-expression. NULL is returned if the ( and or ) are
7559 missing. An empty, but allocated, vector is returned on no
7560 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7561 if we are parsing an attribute list for an attribute that wants a
7562 plain identifier argument, normal_attr for an attribute that wants
7563 an expression, or non_attr if we aren't parsing an attribute list. If
7564 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7565 not all of the expressions in the list were constant.
7566 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7567 will be written to with the location of the closing parenthesis. If
7568 an error occurs, it may or may not be written to. */
7570 static vec<tree, va_gc> *
7571 cp_parser_parenthesized_expression_list (cp_parser* parser,
7572 int is_attribute_list,
7573 bool cast_p,
7574 bool allow_expansion_p,
7575 bool *non_constant_p,
7576 location_t *close_paren_loc)
7578 vec<tree, va_gc> *expression_list;
7579 bool fold_expr_p = is_attribute_list != non_attr;
7580 tree identifier = NULL_TREE;
7581 bool saved_greater_than_is_operator_p;
7583 /* Assume all the expressions will be constant. */
7584 if (non_constant_p)
7585 *non_constant_p = false;
7587 matching_parens parens;
7588 if (!parens.require_open (parser))
7589 return NULL;
7591 expression_list = make_tree_vector ();
7593 /* Within a parenthesized expression, a `>' token is always
7594 the greater-than operator. */
7595 saved_greater_than_is_operator_p
7596 = parser->greater_than_is_operator_p;
7597 parser->greater_than_is_operator_p = true;
7599 /* Consume expressions until there are no more. */
7600 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7601 while (true)
7603 tree expr;
7605 /* At the beginning of attribute lists, check to see if the
7606 next token is an identifier. */
7607 if (is_attribute_list == id_attr
7608 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7610 cp_token *token;
7612 /* Consume the identifier. */
7613 token = cp_lexer_consume_token (parser->lexer);
7614 /* Save the identifier. */
7615 identifier = token->u.value;
7617 else
7619 bool expr_non_constant_p;
7621 /* Parse the next assignment-expression. */
7622 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7624 /* A braced-init-list. */
7625 cp_lexer_set_source_position (parser->lexer);
7626 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7627 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7628 if (non_constant_p && expr_non_constant_p)
7629 *non_constant_p = true;
7631 else if (non_constant_p)
7633 expr = (cp_parser_constant_expression
7634 (parser, /*allow_non_constant_p=*/true,
7635 &expr_non_constant_p));
7636 if (expr_non_constant_p)
7637 *non_constant_p = true;
7639 else
7640 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7641 cast_p);
7643 if (fold_expr_p)
7644 expr = instantiate_non_dependent_expr (expr);
7646 /* If we have an ellipsis, then this is an expression
7647 expansion. */
7648 if (allow_expansion_p
7649 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7651 /* Consume the `...'. */
7652 cp_lexer_consume_token (parser->lexer);
7654 /* Build the argument pack. */
7655 expr = make_pack_expansion (expr);
7658 /* Add it to the list. We add error_mark_node
7659 expressions to the list, so that we can still tell if
7660 the correct form for a parenthesized expression-list
7661 is found. That gives better errors. */
7662 vec_safe_push (expression_list, expr);
7664 if (expr == error_mark_node)
7665 goto skip_comma;
7668 /* After the first item, attribute lists look the same as
7669 expression lists. */
7670 is_attribute_list = non_attr;
7672 get_comma:;
7673 /* If the next token isn't a `,', then we are done. */
7674 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7675 break;
7677 /* Otherwise, consume the `,' and keep going. */
7678 cp_lexer_consume_token (parser->lexer);
7681 if (close_paren_loc)
7682 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7684 if (!parens.require_close (parser))
7686 int ending;
7688 skip_comma:;
7689 /* We try and resync to an unnested comma, as that will give the
7690 user better diagnostics. */
7691 ending = cp_parser_skip_to_closing_parenthesis (parser,
7692 /*recovering=*/true,
7693 /*or_comma=*/true,
7694 /*consume_paren=*/true);
7695 if (ending < 0)
7696 goto get_comma;
7697 if (!ending)
7699 parser->greater_than_is_operator_p
7700 = saved_greater_than_is_operator_p;
7701 return NULL;
7705 parser->greater_than_is_operator_p
7706 = saved_greater_than_is_operator_p;
7708 if (identifier)
7709 vec_safe_insert (expression_list, 0, identifier);
7711 return expression_list;
7714 /* Parse a pseudo-destructor-name.
7716 pseudo-destructor-name:
7717 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7718 :: [opt] nested-name-specifier template template-id :: ~ type-name
7719 :: [opt] nested-name-specifier [opt] ~ type-name
7721 If either of the first two productions is used, sets *SCOPE to the
7722 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7723 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7724 or ERROR_MARK_NODE if the parse fails. */
7726 static void
7727 cp_parser_pseudo_destructor_name (cp_parser* parser,
7728 tree object,
7729 tree* scope,
7730 tree* type)
7732 bool nested_name_specifier_p;
7734 /* Handle ~auto. */
7735 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7736 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7737 && !type_dependent_expression_p (object))
7739 if (cxx_dialect < cxx14)
7740 pedwarn (input_location, 0,
7741 "%<~auto%> only available with "
7742 "-std=c++14 or -std=gnu++14");
7743 cp_lexer_consume_token (parser->lexer);
7744 cp_lexer_consume_token (parser->lexer);
7745 *scope = NULL_TREE;
7746 *type = TREE_TYPE (object);
7747 return;
7750 /* Assume that things will not work out. */
7751 *type = error_mark_node;
7753 /* Look for the optional `::' operator. */
7754 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7755 /* Look for the optional nested-name-specifier. */
7756 nested_name_specifier_p
7757 = (cp_parser_nested_name_specifier_opt (parser,
7758 /*typename_keyword_p=*/false,
7759 /*check_dependency_p=*/true,
7760 /*type_p=*/false,
7761 /*is_declaration=*/false)
7762 != NULL_TREE);
7763 /* Now, if we saw a nested-name-specifier, we might be doing the
7764 second production. */
7765 if (nested_name_specifier_p
7766 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7768 /* Consume the `template' keyword. */
7769 cp_lexer_consume_token (parser->lexer);
7770 /* Parse the template-id. */
7771 cp_parser_template_id (parser,
7772 /*template_keyword_p=*/true,
7773 /*check_dependency_p=*/false,
7774 class_type,
7775 /*is_declaration=*/true);
7776 /* Look for the `::' token. */
7777 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7779 /* If the next token is not a `~', then there might be some
7780 additional qualification. */
7781 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7783 /* At this point, we're looking for "type-name :: ~". The type-name
7784 must not be a class-name, since this is a pseudo-destructor. So,
7785 it must be either an enum-name, or a typedef-name -- both of which
7786 are just identifiers. So, we peek ahead to check that the "::"
7787 and "~" tokens are present; if they are not, then we can avoid
7788 calling type_name. */
7789 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7790 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7791 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7793 cp_parser_error (parser, "non-scalar type");
7794 return;
7797 /* Look for the type-name. */
7798 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7799 if (*scope == error_mark_node)
7800 return;
7802 /* Look for the `::' token. */
7803 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7805 else
7806 *scope = NULL_TREE;
7808 /* Look for the `~'. */
7809 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7811 /* Once we see the ~, this has to be a pseudo-destructor. */
7812 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7813 cp_parser_commit_to_topmost_tentative_parse (parser);
7815 /* Look for the type-name again. We are not responsible for
7816 checking that it matches the first type-name. */
7817 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7820 /* Parse a unary-expression.
7822 unary-expression:
7823 postfix-expression
7824 ++ cast-expression
7825 -- cast-expression
7826 unary-operator cast-expression
7827 sizeof unary-expression
7828 sizeof ( type-id )
7829 alignof ( type-id ) [C++0x]
7830 new-expression
7831 delete-expression
7833 GNU Extensions:
7835 unary-expression:
7836 __extension__ cast-expression
7837 __alignof__ unary-expression
7838 __alignof__ ( type-id )
7839 alignof unary-expression [C++0x]
7840 __real__ cast-expression
7841 __imag__ cast-expression
7842 && identifier
7843 sizeof ( type-id ) { initializer-list , [opt] }
7844 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7845 __alignof__ ( type-id ) { initializer-list , [opt] }
7847 ADDRESS_P is true iff the unary-expression is appearing as the
7848 operand of the `&' operator. CAST_P is true if this expression is
7849 the target of a cast.
7851 Returns a representation of the expression. */
7853 static cp_expr
7854 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7855 bool address_p, bool cast_p, bool decltype_p)
7857 cp_token *token;
7858 enum tree_code unary_operator;
7860 /* Peek at the next token. */
7861 token = cp_lexer_peek_token (parser->lexer);
7862 /* Some keywords give away the kind of expression. */
7863 if (token->type == CPP_KEYWORD)
7865 enum rid keyword = token->keyword;
7867 switch (keyword)
7869 case RID_ALIGNOF:
7870 case RID_SIZEOF:
7872 tree operand, ret;
7873 enum tree_code op;
7874 location_t start_loc = token->location;
7876 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7877 /* Consume the token. */
7878 cp_lexer_consume_token (parser->lexer);
7879 /* Parse the operand. */
7880 operand = cp_parser_sizeof_operand (parser, keyword);
7882 if (TYPE_P (operand))
7883 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7884 else
7886 /* ISO C++ defines alignof only with types, not with
7887 expressions. So pedwarn if alignof is used with a non-
7888 type expression. However, __alignof__ is ok. */
7889 if (id_equal (token->u.value, "alignof"))
7890 pedwarn (token->location, OPT_Wpedantic,
7891 "ISO C++ does not allow %<alignof%> "
7892 "with a non-type");
7894 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7896 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7897 SIZEOF_EXPR with the original operand. */
7898 if (op == SIZEOF_EXPR && ret != error_mark_node)
7900 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7902 if (!processing_template_decl && TYPE_P (operand))
7904 ret = build_min (SIZEOF_EXPR, size_type_node,
7905 build1 (NOP_EXPR, operand,
7906 error_mark_node));
7907 SIZEOF_EXPR_TYPE_P (ret) = 1;
7909 else
7910 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7911 TREE_SIDE_EFFECTS (ret) = 0;
7912 TREE_READONLY (ret) = 1;
7916 /* Construct a location e.g. :
7917 alignof (expr)
7918 ^~~~~~~~~~~~~~
7919 with start == caret at the start of the "alignof"/"sizeof"
7920 token, with the endpoint at the final closing paren. */
7921 location_t finish_loc
7922 = cp_lexer_previous_token (parser->lexer)->location;
7923 location_t compound_loc
7924 = make_location (start_loc, start_loc, finish_loc);
7926 cp_expr ret_expr (ret);
7927 ret_expr.set_location (compound_loc);
7928 return ret_expr;
7931 case RID_NEW:
7932 return cp_parser_new_expression (parser);
7934 case RID_DELETE:
7935 return cp_parser_delete_expression (parser);
7937 case RID_EXTENSION:
7939 /* The saved value of the PEDANTIC flag. */
7940 int saved_pedantic;
7941 tree expr;
7943 /* Save away the PEDANTIC flag. */
7944 cp_parser_extension_opt (parser, &saved_pedantic);
7945 /* Parse the cast-expression. */
7946 expr = cp_parser_simple_cast_expression (parser);
7947 /* Restore the PEDANTIC flag. */
7948 pedantic = saved_pedantic;
7950 return expr;
7953 case RID_REALPART:
7954 case RID_IMAGPART:
7956 tree expression;
7958 /* Consume the `__real__' or `__imag__' token. */
7959 cp_lexer_consume_token (parser->lexer);
7960 /* Parse the cast-expression. */
7961 expression = cp_parser_simple_cast_expression (parser);
7962 /* Create the complete representation. */
7963 return build_x_unary_op (token->location,
7964 (keyword == RID_REALPART
7965 ? REALPART_EXPR : IMAGPART_EXPR),
7966 expression,
7967 tf_warning_or_error);
7969 break;
7971 case RID_TRANSACTION_ATOMIC:
7972 case RID_TRANSACTION_RELAXED:
7973 return cp_parser_transaction_expression (parser, keyword);
7975 case RID_NOEXCEPT:
7977 tree expr;
7978 const char *saved_message;
7979 bool saved_integral_constant_expression_p;
7980 bool saved_non_integral_constant_expression_p;
7981 bool saved_greater_than_is_operator_p;
7983 location_t start_loc = token->location;
7985 cp_lexer_consume_token (parser->lexer);
7986 matching_parens parens;
7987 parens.require_open (parser);
7989 saved_message = parser->type_definition_forbidden_message;
7990 parser->type_definition_forbidden_message
7991 = G_("types may not be defined in %<noexcept%> expressions");
7993 saved_integral_constant_expression_p
7994 = parser->integral_constant_expression_p;
7995 saved_non_integral_constant_expression_p
7996 = parser->non_integral_constant_expression_p;
7997 parser->integral_constant_expression_p = false;
7999 saved_greater_than_is_operator_p
8000 = parser->greater_than_is_operator_p;
8001 parser->greater_than_is_operator_p = true;
8003 ++cp_unevaluated_operand;
8004 ++c_inhibit_evaluation_warnings;
8005 ++cp_noexcept_operand;
8006 expr = cp_parser_expression (parser);
8007 --cp_noexcept_operand;
8008 --c_inhibit_evaluation_warnings;
8009 --cp_unevaluated_operand;
8011 parser->greater_than_is_operator_p
8012 = saved_greater_than_is_operator_p;
8014 parser->integral_constant_expression_p
8015 = saved_integral_constant_expression_p;
8016 parser->non_integral_constant_expression_p
8017 = saved_non_integral_constant_expression_p;
8019 parser->type_definition_forbidden_message = saved_message;
8021 location_t finish_loc
8022 = cp_lexer_peek_token (parser->lexer)->location;
8023 parens.require_close (parser);
8025 /* Construct a location of the form:
8026 noexcept (expr)
8027 ^~~~~~~~~~~~~~~
8028 with start == caret, finishing at the close-paren. */
8029 location_t noexcept_loc
8030 = make_location (start_loc, start_loc, finish_loc);
8032 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8033 noexcept_loc);
8036 default:
8037 break;
8041 /* Look for the `:: new' and `:: delete', which also signal the
8042 beginning of a new-expression, or delete-expression,
8043 respectively. If the next token is `::', then it might be one of
8044 these. */
8045 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8047 enum rid keyword;
8049 /* See if the token after the `::' is one of the keywords in
8050 which we're interested. */
8051 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8052 /* If it's `new', we have a new-expression. */
8053 if (keyword == RID_NEW)
8054 return cp_parser_new_expression (parser);
8055 /* Similarly, for `delete'. */
8056 else if (keyword == RID_DELETE)
8057 return cp_parser_delete_expression (parser);
8060 /* Look for a unary operator. */
8061 unary_operator = cp_parser_unary_operator (token);
8062 /* The `++' and `--' operators can be handled similarly, even though
8063 they are not technically unary-operators in the grammar. */
8064 if (unary_operator == ERROR_MARK)
8066 if (token->type == CPP_PLUS_PLUS)
8067 unary_operator = PREINCREMENT_EXPR;
8068 else if (token->type == CPP_MINUS_MINUS)
8069 unary_operator = PREDECREMENT_EXPR;
8070 /* Handle the GNU address-of-label extension. */
8071 else if (cp_parser_allow_gnu_extensions_p (parser)
8072 && token->type == CPP_AND_AND)
8074 tree identifier;
8075 tree expression;
8076 location_t start_loc = token->location;
8078 /* Consume the '&&' token. */
8079 cp_lexer_consume_token (parser->lexer);
8080 /* Look for the identifier. */
8081 location_t finish_loc
8082 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8083 identifier = cp_parser_identifier (parser);
8084 /* Construct a location of the form:
8085 &&label
8086 ^~~~~~~
8087 with caret==start at the "&&", finish at the end of the label. */
8088 location_t combined_loc
8089 = make_location (start_loc, start_loc, finish_loc);
8090 /* Create an expression representing the address. */
8091 expression = finish_label_address_expr (identifier, combined_loc);
8092 if (cp_parser_non_integral_constant_expression (parser,
8093 NIC_ADDR_LABEL))
8094 expression = error_mark_node;
8095 return expression;
8098 if (unary_operator != ERROR_MARK)
8100 cp_expr cast_expression;
8101 cp_expr expression = error_mark_node;
8102 non_integral_constant non_constant_p = NIC_NONE;
8103 location_t loc = token->location;
8104 tsubst_flags_t complain = complain_flags (decltype_p);
8106 /* Consume the operator token. */
8107 token = cp_lexer_consume_token (parser->lexer);
8108 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8110 /* Parse the cast-expression. */
8111 cast_expression
8112 = cp_parser_cast_expression (parser,
8113 unary_operator == ADDR_EXPR,
8114 /*cast_p=*/false,
8115 /*decltype*/false,
8116 pidk);
8118 /* Make a location:
8119 OP_TOKEN CAST_EXPRESSION
8120 ^~~~~~~~~~~~~~~~~~~~~~~~~
8121 with start==caret at the operator token, and
8122 extending to the end of the cast_expression. */
8123 loc = make_location (loc, loc, cast_expression.get_finish ());
8125 /* Now, build an appropriate representation. */
8126 switch (unary_operator)
8128 case INDIRECT_REF:
8129 non_constant_p = NIC_STAR;
8130 expression = build_x_indirect_ref (loc, cast_expression,
8131 RO_UNARY_STAR,
8132 complain);
8133 /* TODO: build_x_indirect_ref does not always honor the
8134 location, so ensure it is set. */
8135 expression.set_location (loc);
8136 break;
8138 case ADDR_EXPR:
8139 non_constant_p = NIC_ADDR;
8140 /* Fall through. */
8141 case BIT_NOT_EXPR:
8142 expression = build_x_unary_op (loc, unary_operator,
8143 cast_expression,
8144 complain);
8145 /* TODO: build_x_unary_op does not always honor the location,
8146 so ensure it is set. */
8147 expression.set_location (loc);
8148 break;
8150 case PREINCREMENT_EXPR:
8151 case PREDECREMENT_EXPR:
8152 non_constant_p = unary_operator == PREINCREMENT_EXPR
8153 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8154 /* Fall through. */
8155 case NEGATE_EXPR:
8156 /* Immediately fold negation of a constant, unless the constant is 0
8157 (since -0 == 0) or it would overflow. */
8158 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8159 && CONSTANT_CLASS_P (cast_expression)
8160 && !integer_zerop (cast_expression)
8161 && !TREE_OVERFLOW (cast_expression))
8163 tree folded = fold_build1 (unary_operator,
8164 TREE_TYPE (cast_expression),
8165 cast_expression);
8166 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8168 expression = cp_expr (folded, loc);
8169 break;
8172 /* Fall through. */
8173 case UNARY_PLUS_EXPR:
8174 case TRUTH_NOT_EXPR:
8175 expression = finish_unary_op_expr (loc, unary_operator,
8176 cast_expression, complain);
8177 break;
8179 default:
8180 gcc_unreachable ();
8183 if (non_constant_p != NIC_NONE
8184 && cp_parser_non_integral_constant_expression (parser,
8185 non_constant_p))
8186 expression = error_mark_node;
8188 return expression;
8191 return cp_parser_postfix_expression (parser, address_p, cast_p,
8192 /*member_access_only_p=*/false,
8193 decltype_p,
8194 pidk);
8197 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8198 unary-operator, the corresponding tree code is returned. */
8200 static enum tree_code
8201 cp_parser_unary_operator (cp_token* token)
8203 switch (token->type)
8205 case CPP_MULT:
8206 return INDIRECT_REF;
8208 case CPP_AND:
8209 return ADDR_EXPR;
8211 case CPP_PLUS:
8212 return UNARY_PLUS_EXPR;
8214 case CPP_MINUS:
8215 return NEGATE_EXPR;
8217 case CPP_NOT:
8218 return TRUTH_NOT_EXPR;
8220 case CPP_COMPL:
8221 return BIT_NOT_EXPR;
8223 default:
8224 return ERROR_MARK;
8228 /* Parse a new-expression.
8230 new-expression:
8231 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8232 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8234 Returns a representation of the expression. */
8236 static tree
8237 cp_parser_new_expression (cp_parser* parser)
8239 bool global_scope_p;
8240 vec<tree, va_gc> *placement;
8241 tree type;
8242 vec<tree, va_gc> *initializer;
8243 tree nelts = NULL_TREE;
8244 tree ret;
8246 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8248 /* Look for the optional `::' operator. */
8249 global_scope_p
8250 = (cp_parser_global_scope_opt (parser,
8251 /*current_scope_valid_p=*/false)
8252 != NULL_TREE);
8253 /* Look for the `new' operator. */
8254 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8255 /* There's no easy way to tell a new-placement from the
8256 `( type-id )' construct. */
8257 cp_parser_parse_tentatively (parser);
8258 /* Look for a new-placement. */
8259 placement = cp_parser_new_placement (parser);
8260 /* If that didn't work out, there's no new-placement. */
8261 if (!cp_parser_parse_definitely (parser))
8263 if (placement != NULL)
8264 release_tree_vector (placement);
8265 placement = NULL;
8268 /* If the next token is a `(', then we have a parenthesized
8269 type-id. */
8270 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8272 cp_token *token;
8273 const char *saved_message = parser->type_definition_forbidden_message;
8275 /* Consume the `('. */
8276 matching_parens parens;
8277 parens.consume_open (parser);
8279 /* Parse the type-id. */
8280 parser->type_definition_forbidden_message
8281 = G_("types may not be defined in a new-expression");
8283 type_id_in_expr_sentinel s (parser);
8284 type = cp_parser_type_id (parser);
8286 parser->type_definition_forbidden_message = saved_message;
8288 /* Look for the closing `)'. */
8289 parens.require_close (parser);
8290 token = cp_lexer_peek_token (parser->lexer);
8291 /* There should not be a direct-new-declarator in this production,
8292 but GCC used to allowed this, so we check and emit a sensible error
8293 message for this case. */
8294 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8296 error_at (token->location,
8297 "array bound forbidden after parenthesized type-id");
8298 inform (token->location,
8299 "try removing the parentheses around the type-id");
8300 cp_parser_direct_new_declarator (parser);
8303 /* Otherwise, there must be a new-type-id. */
8304 else
8305 type = cp_parser_new_type_id (parser, &nelts);
8307 /* If the next token is a `(' or '{', then we have a new-initializer. */
8308 cp_token *token = cp_lexer_peek_token (parser->lexer);
8309 if (token->type == CPP_OPEN_PAREN
8310 || token->type == CPP_OPEN_BRACE)
8311 initializer = cp_parser_new_initializer (parser);
8312 else
8313 initializer = NULL;
8315 /* A new-expression may not appear in an integral constant
8316 expression. */
8317 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8318 ret = error_mark_node;
8319 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8320 of a new-type-id or type-id of a new-expression, the new-expression shall
8321 contain a new-initializer of the form ( assignment-expression )".
8322 Additionally, consistently with the spirit of DR 1467, we want to accept
8323 'new auto { 2 }' too. */
8324 else if ((ret = type_uses_auto (type))
8325 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8326 && (vec_safe_length (initializer) != 1
8327 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8328 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8330 error_at (token->location,
8331 "initialization of new-expression for type %<auto%> "
8332 "requires exactly one element");
8333 ret = error_mark_node;
8335 else
8337 /* Construct a location e.g.:
8338 ptr = new int[100]
8339 ^~~~~~~~~~~~
8340 with caret == start at the start of the "new" token, and the end
8341 at the end of the final token we consumed. */
8342 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8343 location_t end_loc = get_finish (end_tok->location);
8344 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8346 /* Create a representation of the new-expression. */
8347 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8348 tf_warning_or_error);
8349 protected_set_expr_location (ret, combined_loc);
8352 if (placement != NULL)
8353 release_tree_vector (placement);
8354 if (initializer != NULL)
8355 release_tree_vector (initializer);
8357 return ret;
8360 /* Parse a new-placement.
8362 new-placement:
8363 ( expression-list )
8365 Returns the same representation as for an expression-list. */
8367 static vec<tree, va_gc> *
8368 cp_parser_new_placement (cp_parser* parser)
8370 vec<tree, va_gc> *expression_list;
8372 /* Parse the expression-list. */
8373 expression_list = (cp_parser_parenthesized_expression_list
8374 (parser, non_attr, /*cast_p=*/false,
8375 /*allow_expansion_p=*/true,
8376 /*non_constant_p=*/NULL));
8378 if (expression_list && expression_list->is_empty ())
8379 error ("expected expression-list or type-id");
8381 return expression_list;
8384 /* Parse a new-type-id.
8386 new-type-id:
8387 type-specifier-seq new-declarator [opt]
8389 Returns the TYPE allocated. If the new-type-id indicates an array
8390 type, *NELTS is set to the number of elements in the last array
8391 bound; the TYPE will not include the last array bound. */
8393 static tree
8394 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8396 cp_decl_specifier_seq type_specifier_seq;
8397 cp_declarator *new_declarator;
8398 cp_declarator *declarator;
8399 cp_declarator *outer_declarator;
8400 const char *saved_message;
8402 /* The type-specifier sequence must not contain type definitions.
8403 (It cannot contain declarations of new types either, but if they
8404 are not definitions we will catch that because they are not
8405 complete.) */
8406 saved_message = parser->type_definition_forbidden_message;
8407 parser->type_definition_forbidden_message
8408 = G_("types may not be defined in a new-type-id");
8409 /* Parse the type-specifier-seq. */
8410 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8411 /*is_trailing_return=*/false,
8412 &type_specifier_seq);
8413 /* Restore the old message. */
8414 parser->type_definition_forbidden_message = saved_message;
8416 if (type_specifier_seq.type == error_mark_node)
8417 return error_mark_node;
8419 /* Parse the new-declarator. */
8420 new_declarator = cp_parser_new_declarator_opt (parser);
8422 /* Determine the number of elements in the last array dimension, if
8423 any. */
8424 *nelts = NULL_TREE;
8425 /* Skip down to the last array dimension. */
8426 declarator = new_declarator;
8427 outer_declarator = NULL;
8428 while (declarator && (declarator->kind == cdk_pointer
8429 || declarator->kind == cdk_ptrmem))
8431 outer_declarator = declarator;
8432 declarator = declarator->declarator;
8434 while (declarator
8435 && declarator->kind == cdk_array
8436 && declarator->declarator
8437 && declarator->declarator->kind == cdk_array)
8439 outer_declarator = declarator;
8440 declarator = declarator->declarator;
8443 if (declarator && declarator->kind == cdk_array)
8445 *nelts = declarator->u.array.bounds;
8446 if (*nelts == error_mark_node)
8447 *nelts = integer_one_node;
8449 if (outer_declarator)
8450 outer_declarator->declarator = declarator->declarator;
8451 else
8452 new_declarator = NULL;
8455 return groktypename (&type_specifier_seq, new_declarator, false);
8458 /* Parse an (optional) new-declarator.
8460 new-declarator:
8461 ptr-operator new-declarator [opt]
8462 direct-new-declarator
8464 Returns the declarator. */
8466 static cp_declarator *
8467 cp_parser_new_declarator_opt (cp_parser* parser)
8469 enum tree_code code;
8470 tree type, std_attributes = NULL_TREE;
8471 cp_cv_quals cv_quals;
8473 /* We don't know if there's a ptr-operator next, or not. */
8474 cp_parser_parse_tentatively (parser);
8475 /* Look for a ptr-operator. */
8476 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8477 /* If that worked, look for more new-declarators. */
8478 if (cp_parser_parse_definitely (parser))
8480 cp_declarator *declarator;
8482 /* Parse another optional declarator. */
8483 declarator = cp_parser_new_declarator_opt (parser);
8485 declarator = cp_parser_make_indirect_declarator
8486 (code, type, cv_quals, declarator, std_attributes);
8488 return declarator;
8491 /* If the next token is a `[', there is a direct-new-declarator. */
8492 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8493 return cp_parser_direct_new_declarator (parser);
8495 return NULL;
8498 /* Parse a direct-new-declarator.
8500 direct-new-declarator:
8501 [ expression ]
8502 direct-new-declarator [constant-expression]
8506 static cp_declarator *
8507 cp_parser_direct_new_declarator (cp_parser* parser)
8509 cp_declarator *declarator = NULL;
8511 while (true)
8513 tree expression;
8514 cp_token *token;
8516 /* Look for the opening `['. */
8517 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8519 token = cp_lexer_peek_token (parser->lexer);
8520 expression = cp_parser_expression (parser);
8521 /* The standard requires that the expression have integral
8522 type. DR 74 adds enumeration types. We believe that the
8523 real intent is that these expressions be handled like the
8524 expression in a `switch' condition, which also allows
8525 classes with a single conversion to integral or
8526 enumeration type. */
8527 if (!processing_template_decl)
8529 expression
8530 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8531 expression,
8532 /*complain=*/true);
8533 if (!expression)
8535 error_at (token->location,
8536 "expression in new-declarator must have integral "
8537 "or enumeration type");
8538 expression = error_mark_node;
8542 /* Look for the closing `]'. */
8543 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8545 /* Add this bound to the declarator. */
8546 declarator = make_array_declarator (declarator, expression);
8548 /* If the next token is not a `[', then there are no more
8549 bounds. */
8550 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8551 break;
8554 return declarator;
8557 /* Parse a new-initializer.
8559 new-initializer:
8560 ( expression-list [opt] )
8561 braced-init-list
8563 Returns a representation of the expression-list. */
8565 static vec<tree, va_gc> *
8566 cp_parser_new_initializer (cp_parser* parser)
8568 vec<tree, va_gc> *expression_list;
8570 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8572 tree t;
8573 bool expr_non_constant_p;
8574 cp_lexer_set_source_position (parser->lexer);
8575 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8576 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8577 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8578 expression_list = make_tree_vector_single (t);
8580 else
8581 expression_list = (cp_parser_parenthesized_expression_list
8582 (parser, non_attr, /*cast_p=*/false,
8583 /*allow_expansion_p=*/true,
8584 /*non_constant_p=*/NULL));
8586 return expression_list;
8589 /* Parse a delete-expression.
8591 delete-expression:
8592 :: [opt] delete cast-expression
8593 :: [opt] delete [ ] cast-expression
8595 Returns a representation of the expression. */
8597 static tree
8598 cp_parser_delete_expression (cp_parser* parser)
8600 bool global_scope_p;
8601 bool array_p;
8602 tree expression;
8604 /* Look for the optional `::' operator. */
8605 global_scope_p
8606 = (cp_parser_global_scope_opt (parser,
8607 /*current_scope_valid_p=*/false)
8608 != NULL_TREE);
8609 /* Look for the `delete' keyword. */
8610 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8611 /* See if the array syntax is in use. */
8612 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8614 /* Consume the `[' token. */
8615 cp_lexer_consume_token (parser->lexer);
8616 /* Look for the `]' token. */
8617 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8618 /* Remember that this is the `[]' construct. */
8619 array_p = true;
8621 else
8622 array_p = false;
8624 /* Parse the cast-expression. */
8625 expression = cp_parser_simple_cast_expression (parser);
8627 /* A delete-expression may not appear in an integral constant
8628 expression. */
8629 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8630 return error_mark_node;
8632 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8633 tf_warning_or_error);
8636 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8637 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8638 0 otherwise. */
8640 static int
8641 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8643 cp_token *token = cp_lexer_peek_token (parser->lexer);
8644 switch (token->type)
8646 case CPP_COMMA:
8647 case CPP_SEMICOLON:
8648 case CPP_QUERY:
8649 case CPP_COLON:
8650 case CPP_CLOSE_SQUARE:
8651 case CPP_CLOSE_PAREN:
8652 case CPP_CLOSE_BRACE:
8653 case CPP_OPEN_BRACE:
8654 case CPP_DOT:
8655 case CPP_DOT_STAR:
8656 case CPP_DEREF:
8657 case CPP_DEREF_STAR:
8658 case CPP_DIV:
8659 case CPP_MOD:
8660 case CPP_LSHIFT:
8661 case CPP_RSHIFT:
8662 case CPP_LESS:
8663 case CPP_GREATER:
8664 case CPP_LESS_EQ:
8665 case CPP_GREATER_EQ:
8666 case CPP_EQ_EQ:
8667 case CPP_NOT_EQ:
8668 case CPP_EQ:
8669 case CPP_MULT_EQ:
8670 case CPP_DIV_EQ:
8671 case CPP_MOD_EQ:
8672 case CPP_PLUS_EQ:
8673 case CPP_MINUS_EQ:
8674 case CPP_RSHIFT_EQ:
8675 case CPP_LSHIFT_EQ:
8676 case CPP_AND_EQ:
8677 case CPP_XOR_EQ:
8678 case CPP_OR_EQ:
8679 case CPP_XOR:
8680 case CPP_OR:
8681 case CPP_OR_OR:
8682 case CPP_EOF:
8683 case CPP_ELLIPSIS:
8684 return 0;
8686 case CPP_OPEN_PAREN:
8687 /* In ((type ()) () the last () isn't a valid cast-expression,
8688 so the whole must be parsed as postfix-expression. */
8689 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8690 != CPP_CLOSE_PAREN;
8692 case CPP_OPEN_SQUARE:
8693 /* '[' may start a primary-expression in obj-c++ and in C++11,
8694 as a lambda-expression, eg, '(void)[]{}'. */
8695 if (cxx_dialect >= cxx11)
8696 return -1;
8697 return c_dialect_objc ();
8699 case CPP_PLUS_PLUS:
8700 case CPP_MINUS_MINUS:
8701 /* '++' and '--' may or may not start a cast-expression:
8703 struct T { void operator++(int); };
8704 void f() { (T())++; }
8708 int a;
8709 (int)++a; */
8710 return -1;
8712 default:
8713 return 1;
8717 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8718 in the order: const_cast, static_cast, reinterpret_cast.
8720 Don't suggest dynamic_cast.
8722 Return the first legal cast kind found, or NULL otherwise. */
8724 static const char *
8725 get_cast_suggestion (tree dst_type, tree orig_expr)
8727 tree trial;
8729 /* Reuse the parser logic by attempting to build the various kinds of
8730 cast, with "complain" disabled.
8731 Identify the first such cast that is valid. */
8733 /* Don't attempt to run such logic within template processing. */
8734 if (processing_template_decl)
8735 return NULL;
8737 /* First try const_cast. */
8738 trial = build_const_cast (dst_type, orig_expr, tf_none);
8739 if (trial != error_mark_node)
8740 return "const_cast";
8742 /* If that fails, try static_cast. */
8743 trial = build_static_cast (dst_type, orig_expr, tf_none);
8744 if (trial != error_mark_node)
8745 return "static_cast";
8747 /* Finally, try reinterpret_cast. */
8748 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8749 if (trial != error_mark_node)
8750 return "reinterpret_cast";
8752 /* No such cast possible. */
8753 return NULL;
8756 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8757 suggesting how to convert a C-style cast of the form:
8759 (DST_TYPE)ORIG_EXPR
8761 to a C++-style cast.
8763 The primary range of RICHLOC is asssumed to be that of the original
8764 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8765 of the parens in the C-style cast. */
8767 static void
8768 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8769 location_t close_paren_loc, tree orig_expr,
8770 tree dst_type)
8772 /* This function is non-trivial, so bail out now if the warning isn't
8773 going to be emitted. */
8774 if (!warn_old_style_cast)
8775 return;
8777 /* Try to find a legal C++ cast, trying them in order:
8778 const_cast, static_cast, reinterpret_cast. */
8779 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8780 if (!cast_suggestion)
8781 return;
8783 /* Replace the open paren with "CAST_SUGGESTION<". */
8784 pretty_printer pp;
8785 pp_printf (&pp, "%s<", cast_suggestion);
8786 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8788 /* Replace the close paren with "> (". */
8789 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8791 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8792 rich_loc->add_fixit_insert_after (")");
8796 /* Parse a cast-expression.
8798 cast-expression:
8799 unary-expression
8800 ( type-id ) cast-expression
8802 ADDRESS_P is true iff the unary-expression is appearing as the
8803 operand of the `&' operator. CAST_P is true if this expression is
8804 the target of a cast.
8806 Returns a representation of the expression. */
8808 static cp_expr
8809 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8810 bool decltype_p, cp_id_kind * pidk)
8812 /* If it's a `(', then we might be looking at a cast. */
8813 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8815 tree type = NULL_TREE;
8816 cp_expr expr (NULL_TREE);
8817 int cast_expression = 0;
8818 const char *saved_message;
8820 /* There's no way to know yet whether or not this is a cast.
8821 For example, `(int (3))' is a unary-expression, while `(int)
8822 3' is a cast. So, we resort to parsing tentatively. */
8823 cp_parser_parse_tentatively (parser);
8824 /* Types may not be defined in a cast. */
8825 saved_message = parser->type_definition_forbidden_message;
8826 parser->type_definition_forbidden_message
8827 = G_("types may not be defined in casts");
8828 /* Consume the `('. */
8829 matching_parens parens;
8830 cp_token *open_paren = parens.consume_open (parser);
8831 location_t open_paren_loc = open_paren->location;
8832 location_t close_paren_loc = UNKNOWN_LOCATION;
8834 /* A very tricky bit is that `(struct S) { 3 }' is a
8835 compound-literal (which we permit in C++ as an extension).
8836 But, that construct is not a cast-expression -- it is a
8837 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8838 is legal; if the compound-literal were a cast-expression,
8839 you'd need an extra set of parentheses.) But, if we parse
8840 the type-id, and it happens to be a class-specifier, then we
8841 will commit to the parse at that point, because we cannot
8842 undo the action that is done when creating a new class. So,
8843 then we cannot back up and do a postfix-expression.
8845 Another tricky case is the following (c++/29234):
8847 struct S { void operator () (); };
8849 void foo ()
8851 ( S()() );
8854 As a type-id we parse the parenthesized S()() as a function
8855 returning a function, groktypename complains and we cannot
8856 back up in this case either.
8858 Therefore, we scan ahead to the closing `)', and check to see
8859 if the tokens after the `)' can start a cast-expression. Otherwise
8860 we are dealing with an unary-expression, a postfix-expression
8861 or something else.
8863 Yet another tricky case, in C++11, is the following (c++/54891):
8865 (void)[]{};
8867 The issue is that usually, besides the case of lambda-expressions,
8868 the parenthesized type-id cannot be followed by '[', and, eg, we
8869 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8870 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8871 we don't commit, we try a cast-expression, then an unary-expression.
8873 Save tokens so that we can put them back. */
8874 cp_lexer_save_tokens (parser->lexer);
8876 /* We may be looking at a cast-expression. */
8877 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8878 /*consume_paren=*/true))
8879 cast_expression
8880 = cp_parser_tokens_start_cast_expression (parser);
8882 /* Roll back the tokens we skipped. */
8883 cp_lexer_rollback_tokens (parser->lexer);
8884 /* If we aren't looking at a cast-expression, simulate an error so
8885 that the call to cp_parser_error_occurred below returns true. */
8886 if (!cast_expression)
8887 cp_parser_simulate_error (parser);
8888 else
8890 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8891 parser->in_type_id_in_expr_p = true;
8892 /* Look for the type-id. */
8893 type = cp_parser_type_id (parser);
8894 /* Look for the closing `)'. */
8895 cp_token *close_paren = parens.require_close (parser);
8896 if (close_paren)
8897 close_paren_loc = close_paren->location;
8898 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8901 /* Restore the saved message. */
8902 parser->type_definition_forbidden_message = saved_message;
8904 /* At this point this can only be either a cast or a
8905 parenthesized ctor such as `(T ())' that looks like a cast to
8906 function returning T. */
8907 if (!cp_parser_error_occurred (parser))
8909 /* Only commit if the cast-expression doesn't start with
8910 '++', '--', or '[' in C++11. */
8911 if (cast_expression > 0)
8912 cp_parser_commit_to_topmost_tentative_parse (parser);
8914 expr = cp_parser_cast_expression (parser,
8915 /*address_p=*/false,
8916 /*cast_p=*/true,
8917 /*decltype_p=*/false,
8918 pidk);
8920 if (cp_parser_parse_definitely (parser))
8922 /* Warn about old-style casts, if so requested. */
8923 if (warn_old_style_cast
8924 && !in_system_header_at (input_location)
8925 && !VOID_TYPE_P (type)
8926 && current_lang_name != lang_name_c)
8928 gcc_rich_location rich_loc (input_location);
8929 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
8930 expr, type);
8931 warning_at (&rich_loc, OPT_Wold_style_cast,
8932 "use of old-style cast to %q#T", type);
8935 /* Only type conversions to integral or enumeration types
8936 can be used in constant-expressions. */
8937 if (!cast_valid_in_integral_constant_expression_p (type)
8938 && cp_parser_non_integral_constant_expression (parser,
8939 NIC_CAST))
8940 return error_mark_node;
8942 /* Perform the cast. */
8943 /* Make a location:
8944 (TYPE) EXPR
8945 ^~~~~~~~~~~
8946 with start==caret at the open paren, extending to the
8947 end of "expr". */
8948 location_t cast_loc = make_location (open_paren_loc,
8949 open_paren_loc,
8950 expr.get_finish ());
8951 expr = build_c_cast (cast_loc, type, expr);
8952 return expr;
8955 else
8956 cp_parser_abort_tentative_parse (parser);
8959 /* If we get here, then it's not a cast, so it must be a
8960 unary-expression. */
8961 return cp_parser_unary_expression (parser, pidk, address_p,
8962 cast_p, decltype_p);
8965 /* Parse a binary expression of the general form:
8967 pm-expression:
8968 cast-expression
8969 pm-expression .* cast-expression
8970 pm-expression ->* cast-expression
8972 multiplicative-expression:
8973 pm-expression
8974 multiplicative-expression * pm-expression
8975 multiplicative-expression / pm-expression
8976 multiplicative-expression % pm-expression
8978 additive-expression:
8979 multiplicative-expression
8980 additive-expression + multiplicative-expression
8981 additive-expression - multiplicative-expression
8983 shift-expression:
8984 additive-expression
8985 shift-expression << additive-expression
8986 shift-expression >> additive-expression
8988 relational-expression:
8989 shift-expression
8990 relational-expression < shift-expression
8991 relational-expression > shift-expression
8992 relational-expression <= shift-expression
8993 relational-expression >= shift-expression
8995 GNU Extension:
8997 relational-expression:
8998 relational-expression <? shift-expression
8999 relational-expression >? shift-expression
9001 equality-expression:
9002 relational-expression
9003 equality-expression == relational-expression
9004 equality-expression != relational-expression
9006 and-expression:
9007 equality-expression
9008 and-expression & equality-expression
9010 exclusive-or-expression:
9011 and-expression
9012 exclusive-or-expression ^ and-expression
9014 inclusive-or-expression:
9015 exclusive-or-expression
9016 inclusive-or-expression | exclusive-or-expression
9018 logical-and-expression:
9019 inclusive-or-expression
9020 logical-and-expression && inclusive-or-expression
9022 logical-or-expression:
9023 logical-and-expression
9024 logical-or-expression || logical-and-expression
9026 All these are implemented with a single function like:
9028 binary-expression:
9029 simple-cast-expression
9030 binary-expression <token> binary-expression
9032 CAST_P is true if this expression is the target of a cast.
9034 The binops_by_token map is used to get the tree codes for each <token> type.
9035 binary-expressions are associated according to a precedence table. */
9037 #define TOKEN_PRECEDENCE(token) \
9038 (((token->type == CPP_GREATER \
9039 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9040 && !parser->greater_than_is_operator_p) \
9041 ? PREC_NOT_OPERATOR \
9042 : binops_by_token[token->type].prec)
9044 static cp_expr
9045 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9046 bool no_toplevel_fold_p,
9047 bool decltype_p,
9048 enum cp_parser_prec prec,
9049 cp_id_kind * pidk)
9051 cp_parser_expression_stack stack;
9052 cp_parser_expression_stack_entry *sp = &stack[0];
9053 cp_parser_expression_stack_entry current;
9054 cp_expr rhs;
9055 cp_token *token;
9056 enum tree_code rhs_type;
9057 enum cp_parser_prec new_prec, lookahead_prec;
9058 tree overload;
9060 /* Parse the first expression. */
9061 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9062 ? TRUTH_NOT_EXPR : ERROR_MARK);
9063 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9064 cast_p, decltype_p, pidk);
9065 current.prec = prec;
9067 if (cp_parser_error_occurred (parser))
9068 return error_mark_node;
9070 for (;;)
9072 /* Get an operator token. */
9073 token = cp_lexer_peek_token (parser->lexer);
9075 if (warn_cxx11_compat
9076 && token->type == CPP_RSHIFT
9077 && !parser->greater_than_is_operator_p)
9079 if (warning_at (token->location, OPT_Wc__11_compat,
9080 "%<>>%> operator is treated"
9081 " as two right angle brackets in C++11"))
9082 inform (token->location,
9083 "suggest parentheses around %<>>%> expression");
9086 new_prec = TOKEN_PRECEDENCE (token);
9087 if (new_prec != PREC_NOT_OPERATOR
9088 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9089 /* This is a fold-expression; handle it later. */
9090 new_prec = PREC_NOT_OPERATOR;
9092 /* Popping an entry off the stack means we completed a subexpression:
9093 - either we found a token which is not an operator (`>' where it is not
9094 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9095 will happen repeatedly;
9096 - or, we found an operator which has lower priority. This is the case
9097 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9098 parsing `3 * 4'. */
9099 if (new_prec <= current.prec)
9101 if (sp == stack)
9102 break;
9103 else
9104 goto pop;
9107 get_rhs:
9108 current.tree_type = binops_by_token[token->type].tree_type;
9109 current.loc = token->location;
9111 /* We used the operator token. */
9112 cp_lexer_consume_token (parser->lexer);
9114 /* For "false && x" or "true || x", x will never be executed;
9115 disable warnings while evaluating it. */
9116 if (current.tree_type == TRUTH_ANDIF_EXPR)
9117 c_inhibit_evaluation_warnings +=
9118 cp_fully_fold (current.lhs) == truthvalue_false_node;
9119 else if (current.tree_type == TRUTH_ORIF_EXPR)
9120 c_inhibit_evaluation_warnings +=
9121 cp_fully_fold (current.lhs) == truthvalue_true_node;
9123 /* Extract another operand. It may be the RHS of this expression
9124 or the LHS of a new, higher priority expression. */
9125 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9126 ? TRUTH_NOT_EXPR : ERROR_MARK);
9127 rhs = cp_parser_simple_cast_expression (parser);
9129 /* Get another operator token. Look up its precedence to avoid
9130 building a useless (immediately popped) stack entry for common
9131 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9132 token = cp_lexer_peek_token (parser->lexer);
9133 lookahead_prec = TOKEN_PRECEDENCE (token);
9134 if (lookahead_prec != PREC_NOT_OPERATOR
9135 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9136 lookahead_prec = PREC_NOT_OPERATOR;
9137 if (lookahead_prec > new_prec)
9139 /* ... and prepare to parse the RHS of the new, higher priority
9140 expression. Since precedence levels on the stack are
9141 monotonically increasing, we do not have to care about
9142 stack overflows. */
9143 *sp = current;
9144 ++sp;
9145 current.lhs = rhs;
9146 current.lhs_type = rhs_type;
9147 current.prec = new_prec;
9148 new_prec = lookahead_prec;
9149 goto get_rhs;
9151 pop:
9152 lookahead_prec = new_prec;
9153 /* If the stack is not empty, we have parsed into LHS the right side
9154 (`4' in the example above) of an expression we had suspended.
9155 We can use the information on the stack to recover the LHS (`3')
9156 from the stack together with the tree code (`MULT_EXPR'), and
9157 the precedence of the higher level subexpression
9158 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9159 which will be used to actually build the additive expression. */
9160 rhs = current.lhs;
9161 rhs_type = current.lhs_type;
9162 --sp;
9163 current = *sp;
9166 /* Undo the disabling of warnings done above. */
9167 if (current.tree_type == TRUTH_ANDIF_EXPR)
9168 c_inhibit_evaluation_warnings -=
9169 cp_fully_fold (current.lhs) == truthvalue_false_node;
9170 else if (current.tree_type == TRUTH_ORIF_EXPR)
9171 c_inhibit_evaluation_warnings -=
9172 cp_fully_fold (current.lhs) == truthvalue_true_node;
9174 if (warn_logical_not_paren
9175 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9176 && current.lhs_type == TRUTH_NOT_EXPR
9177 /* Avoid warning for !!x == y. */
9178 && (TREE_CODE (current.lhs) != NE_EXPR
9179 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9180 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9181 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9182 /* Avoid warning for !b == y where b is boolean. */
9183 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9184 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9185 != BOOLEAN_TYPE))))
9186 /* Avoid warning for !!b == y where b is boolean. */
9187 && (!DECL_P (current.lhs)
9188 || TREE_TYPE (current.lhs) == NULL_TREE
9189 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9190 warn_logical_not_parentheses (current.loc, current.tree_type,
9191 current.lhs, maybe_constant_value (rhs));
9193 overload = NULL;
9195 location_t combined_loc = make_location (current.loc,
9196 current.lhs.get_start (),
9197 rhs.get_finish ());
9199 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9200 ERROR_MARK for everything that is not a binary expression.
9201 This makes warn_about_parentheses miss some warnings that
9202 involve unary operators. For unary expressions we should
9203 pass the correct tree_code unless the unary expression was
9204 surrounded by parentheses.
9206 if (no_toplevel_fold_p
9207 && lookahead_prec <= current.prec
9208 && sp == stack)
9209 current.lhs = build2_loc (combined_loc,
9210 current.tree_type,
9211 TREE_CODE_CLASS (current.tree_type)
9212 == tcc_comparison
9213 ? boolean_type_node : TREE_TYPE (current.lhs),
9214 current.lhs, rhs);
9215 else
9217 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9218 current.lhs, current.lhs_type,
9219 rhs, rhs_type, &overload,
9220 complain_flags (decltype_p));
9221 /* TODO: build_x_binary_op doesn't always honor the location. */
9222 current.lhs.set_location (combined_loc);
9224 current.lhs_type = current.tree_type;
9226 /* If the binary operator required the use of an overloaded operator,
9227 then this expression cannot be an integral constant-expression.
9228 An overloaded operator can be used even if both operands are
9229 otherwise permissible in an integral constant-expression if at
9230 least one of the operands is of enumeration type. */
9232 if (overload
9233 && cp_parser_non_integral_constant_expression (parser,
9234 NIC_OVERLOADED))
9235 return error_mark_node;
9238 return current.lhs;
9241 static cp_expr
9242 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9243 bool no_toplevel_fold_p,
9244 enum cp_parser_prec prec,
9245 cp_id_kind * pidk)
9247 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9248 /*decltype*/false, prec, pidk);
9251 /* Parse the `? expression : assignment-expression' part of a
9252 conditional-expression. The LOGICAL_OR_EXPR is the
9253 logical-or-expression that started the conditional-expression.
9254 Returns a representation of the entire conditional-expression.
9256 This routine is used by cp_parser_assignment_expression.
9258 ? expression : assignment-expression
9260 GNU Extensions:
9262 ? : assignment-expression */
9264 static tree
9265 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9267 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9268 cp_expr assignment_expr;
9269 struct cp_token *token;
9270 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9272 /* Consume the `?' token. */
9273 cp_lexer_consume_token (parser->lexer);
9274 token = cp_lexer_peek_token (parser->lexer);
9275 if (cp_parser_allow_gnu_extensions_p (parser)
9276 && token->type == CPP_COLON)
9278 pedwarn (token->location, OPT_Wpedantic,
9279 "ISO C++ does not allow ?: with omitted middle operand");
9280 /* Implicit true clause. */
9281 expr = NULL_TREE;
9282 c_inhibit_evaluation_warnings +=
9283 folded_logical_or_expr == truthvalue_true_node;
9284 warn_for_omitted_condop (token->location, logical_or_expr);
9286 else
9288 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9289 parser->colon_corrects_to_scope_p = false;
9290 /* Parse the expression. */
9291 c_inhibit_evaluation_warnings +=
9292 folded_logical_or_expr == truthvalue_false_node;
9293 expr = cp_parser_expression (parser);
9294 c_inhibit_evaluation_warnings +=
9295 ((folded_logical_or_expr == truthvalue_true_node)
9296 - (folded_logical_or_expr == truthvalue_false_node));
9297 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9300 /* The next token should be a `:'. */
9301 cp_parser_require (parser, CPP_COLON, RT_COLON);
9302 /* Parse the assignment-expression. */
9303 assignment_expr = cp_parser_assignment_expression (parser);
9304 c_inhibit_evaluation_warnings -=
9305 folded_logical_or_expr == truthvalue_true_node;
9307 /* Make a location:
9308 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9309 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9310 with the caret at the "?", ranging from the start of
9311 the logical_or_expr to the end of the assignment_expr. */
9312 loc = make_location (loc,
9313 logical_or_expr.get_start (),
9314 assignment_expr.get_finish ());
9316 /* Build the conditional-expression. */
9317 return build_x_conditional_expr (loc, logical_or_expr,
9318 expr,
9319 assignment_expr,
9320 tf_warning_or_error);
9323 /* Parse an assignment-expression.
9325 assignment-expression:
9326 conditional-expression
9327 logical-or-expression assignment-operator assignment_expression
9328 throw-expression
9330 CAST_P is true if this expression is the target of a cast.
9331 DECLTYPE_P is true if this expression is the operand of decltype.
9333 Returns a representation for the expression. */
9335 static cp_expr
9336 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9337 bool cast_p, bool decltype_p)
9339 cp_expr expr;
9341 /* If the next token is the `throw' keyword, then we're looking at
9342 a throw-expression. */
9343 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9344 expr = cp_parser_throw_expression (parser);
9345 /* Otherwise, it must be that we are looking at a
9346 logical-or-expression. */
9347 else
9349 /* Parse the binary expressions (logical-or-expression). */
9350 expr = cp_parser_binary_expression (parser, cast_p, false,
9351 decltype_p,
9352 PREC_NOT_OPERATOR, pidk);
9353 /* If the next token is a `?' then we're actually looking at a
9354 conditional-expression. */
9355 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9356 return cp_parser_question_colon_clause (parser, expr);
9357 else
9359 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9361 /* If it's an assignment-operator, we're using the second
9362 production. */
9363 enum tree_code assignment_operator
9364 = cp_parser_assignment_operator_opt (parser);
9365 if (assignment_operator != ERROR_MARK)
9367 bool non_constant_p;
9369 /* Parse the right-hand side of the assignment. */
9370 cp_expr rhs = cp_parser_initializer_clause (parser,
9371 &non_constant_p);
9373 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9374 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9376 /* An assignment may not appear in a
9377 constant-expression. */
9378 if (cp_parser_non_integral_constant_expression (parser,
9379 NIC_ASSIGNMENT))
9380 return error_mark_node;
9381 /* Build the assignment expression. Its default
9382 location:
9383 LHS = RHS
9384 ~~~~^~~~~
9385 is the location of the '=' token as the
9386 caret, ranging from the start of the lhs to the
9387 end of the rhs. */
9388 loc = make_location (loc,
9389 expr.get_start (),
9390 rhs.get_finish ());
9391 expr = build_x_modify_expr (loc, expr,
9392 assignment_operator,
9393 rhs,
9394 complain_flags (decltype_p));
9395 /* TODO: build_x_modify_expr doesn't honor the location,
9396 so we must set it here. */
9397 expr.set_location (loc);
9402 return expr;
9405 /* Parse an (optional) assignment-operator.
9407 assignment-operator: one of
9408 = *= /= %= += -= >>= <<= &= ^= |=
9410 GNU Extension:
9412 assignment-operator: one of
9413 <?= >?=
9415 If the next token is an assignment operator, the corresponding tree
9416 code is returned, and the token is consumed. For example, for
9417 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9418 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9419 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9420 operator, ERROR_MARK is returned. */
9422 static enum tree_code
9423 cp_parser_assignment_operator_opt (cp_parser* parser)
9425 enum tree_code op;
9426 cp_token *token;
9428 /* Peek at the next token. */
9429 token = cp_lexer_peek_token (parser->lexer);
9431 switch (token->type)
9433 case CPP_EQ:
9434 op = NOP_EXPR;
9435 break;
9437 case CPP_MULT_EQ:
9438 op = MULT_EXPR;
9439 break;
9441 case CPP_DIV_EQ:
9442 op = TRUNC_DIV_EXPR;
9443 break;
9445 case CPP_MOD_EQ:
9446 op = TRUNC_MOD_EXPR;
9447 break;
9449 case CPP_PLUS_EQ:
9450 op = PLUS_EXPR;
9451 break;
9453 case CPP_MINUS_EQ:
9454 op = MINUS_EXPR;
9455 break;
9457 case CPP_RSHIFT_EQ:
9458 op = RSHIFT_EXPR;
9459 break;
9461 case CPP_LSHIFT_EQ:
9462 op = LSHIFT_EXPR;
9463 break;
9465 case CPP_AND_EQ:
9466 op = BIT_AND_EXPR;
9467 break;
9469 case CPP_XOR_EQ:
9470 op = BIT_XOR_EXPR;
9471 break;
9473 case CPP_OR_EQ:
9474 op = BIT_IOR_EXPR;
9475 break;
9477 default:
9478 /* Nothing else is an assignment operator. */
9479 op = ERROR_MARK;
9482 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9483 if (op != ERROR_MARK
9484 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9485 op = ERROR_MARK;
9487 /* If it was an assignment operator, consume it. */
9488 if (op != ERROR_MARK)
9489 cp_lexer_consume_token (parser->lexer);
9491 return op;
9494 /* Parse an expression.
9496 expression:
9497 assignment-expression
9498 expression , assignment-expression
9500 CAST_P is true if this expression is the target of a cast.
9501 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9502 except possibly parenthesized or on the RHS of a comma (N3276).
9504 Returns a representation of the expression. */
9506 static cp_expr
9507 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9508 bool cast_p, bool decltype_p)
9510 cp_expr expression = NULL_TREE;
9511 location_t loc = UNKNOWN_LOCATION;
9513 while (true)
9515 cp_expr assignment_expression;
9517 /* Parse the next assignment-expression. */
9518 assignment_expression
9519 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9521 /* We don't create a temporary for a call that is the immediate operand
9522 of decltype or on the RHS of a comma. But when we see a comma, we
9523 need to create a temporary for a call on the LHS. */
9524 if (decltype_p && !processing_template_decl
9525 && TREE_CODE (assignment_expression) == CALL_EXPR
9526 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9527 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9528 assignment_expression
9529 = build_cplus_new (TREE_TYPE (assignment_expression),
9530 assignment_expression, tf_warning_or_error);
9532 /* If this is the first assignment-expression, we can just
9533 save it away. */
9534 if (!expression)
9535 expression = assignment_expression;
9536 else
9538 /* Create a location with caret at the comma, ranging
9539 from the start of the LHS to the end of the RHS. */
9540 loc = make_location (loc,
9541 expression.get_start (),
9542 assignment_expression.get_finish ());
9543 expression = build_x_compound_expr (loc, expression,
9544 assignment_expression,
9545 complain_flags (decltype_p));
9546 expression.set_location (loc);
9548 /* If the next token is not a comma, or we're in a fold-expression, then
9549 we are done with the expression. */
9550 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9551 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9552 break;
9553 /* Consume the `,'. */
9554 loc = cp_lexer_peek_token (parser->lexer)->location;
9555 cp_lexer_consume_token (parser->lexer);
9556 /* A comma operator cannot appear in a constant-expression. */
9557 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9558 expression = error_mark_node;
9561 return expression;
9564 /* Parse a constant-expression.
9566 constant-expression:
9567 conditional-expression
9569 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9570 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9571 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9572 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9573 only parse a conditional-expression, otherwise parse an
9574 assignment-expression. See below for rationale. */
9576 static cp_expr
9577 cp_parser_constant_expression (cp_parser* parser,
9578 bool allow_non_constant_p,
9579 bool *non_constant_p,
9580 bool strict_p)
9582 bool saved_integral_constant_expression_p;
9583 bool saved_allow_non_integral_constant_expression_p;
9584 bool saved_non_integral_constant_expression_p;
9585 cp_expr expression;
9587 /* It might seem that we could simply parse the
9588 conditional-expression, and then check to see if it were
9589 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9590 one that the compiler can figure out is constant, possibly after
9591 doing some simplifications or optimizations. The standard has a
9592 precise definition of constant-expression, and we must honor
9593 that, even though it is somewhat more restrictive.
9595 For example:
9597 int i[(2, 3)];
9599 is not a legal declaration, because `(2, 3)' is not a
9600 constant-expression. The `,' operator is forbidden in a
9601 constant-expression. However, GCC's constant-folding machinery
9602 will fold this operation to an INTEGER_CST for `3'. */
9604 /* Save the old settings. */
9605 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9606 saved_allow_non_integral_constant_expression_p
9607 = parser->allow_non_integral_constant_expression_p;
9608 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9609 /* We are now parsing a constant-expression. */
9610 parser->integral_constant_expression_p = true;
9611 parser->allow_non_integral_constant_expression_p
9612 = (allow_non_constant_p || cxx_dialect >= cxx11);
9613 parser->non_integral_constant_expression_p = false;
9614 /* Although the grammar says "conditional-expression", when not STRICT_P,
9615 we parse an "assignment-expression", which also permits
9616 "throw-expression" and the use of assignment operators. In the case
9617 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9618 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9619 actually essential that we look for an assignment-expression.
9620 For example, cp_parser_initializer_clauses uses this function to
9621 determine whether a particular assignment-expression is in fact
9622 constant. */
9623 if (strict_p)
9625 /* Parse the binary expressions (logical-or-expression). */
9626 expression = cp_parser_binary_expression (parser, false, false, false,
9627 PREC_NOT_OPERATOR, NULL);
9628 /* If the next token is a `?' then we're actually looking at
9629 a conditional-expression; otherwise we're done. */
9630 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9631 expression = cp_parser_question_colon_clause (parser, expression);
9633 else
9634 expression = cp_parser_assignment_expression (parser);
9635 /* Restore the old settings. */
9636 parser->integral_constant_expression_p
9637 = saved_integral_constant_expression_p;
9638 parser->allow_non_integral_constant_expression_p
9639 = saved_allow_non_integral_constant_expression_p;
9640 if (cxx_dialect >= cxx11)
9642 /* Require an rvalue constant expression here; that's what our
9643 callers expect. Reference constant expressions are handled
9644 separately in e.g. cp_parser_template_argument. */
9645 tree decay = expression;
9646 if (TREE_TYPE (expression)
9647 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9648 decay = build_address (expression);
9649 bool is_const = potential_rvalue_constant_expression (decay);
9650 parser->non_integral_constant_expression_p = !is_const;
9651 if (!is_const && !allow_non_constant_p)
9652 require_potential_rvalue_constant_expression (decay);
9654 if (allow_non_constant_p)
9655 *non_constant_p = parser->non_integral_constant_expression_p;
9656 parser->non_integral_constant_expression_p
9657 = saved_non_integral_constant_expression_p;
9659 return expression;
9662 /* Parse __builtin_offsetof.
9664 offsetof-expression:
9665 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9667 offsetof-member-designator:
9668 id-expression
9669 | offsetof-member-designator "." id-expression
9670 | offsetof-member-designator "[" expression "]"
9671 | offsetof-member-designator "->" id-expression */
9673 static cp_expr
9674 cp_parser_builtin_offsetof (cp_parser *parser)
9676 int save_ice_p, save_non_ice_p;
9677 tree type;
9678 cp_expr expr;
9679 cp_id_kind dummy;
9680 cp_token *token;
9681 location_t finish_loc;
9683 /* We're about to accept non-integral-constant things, but will
9684 definitely yield an integral constant expression. Save and
9685 restore these values around our local parsing. */
9686 save_ice_p = parser->integral_constant_expression_p;
9687 save_non_ice_p = parser->non_integral_constant_expression_p;
9689 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9691 /* Consume the "__builtin_offsetof" token. */
9692 cp_lexer_consume_token (parser->lexer);
9693 /* Consume the opening `('. */
9694 matching_parens parens;
9695 parens.require_open (parser);
9696 /* Parse the type-id. */
9697 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9698 type = cp_parser_type_id (parser);
9699 /* Look for the `,'. */
9700 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9701 token = cp_lexer_peek_token (parser->lexer);
9703 /* Build the (type *)null that begins the traditional offsetof macro. */
9704 tree object_ptr
9705 = build_static_cast (build_pointer_type (type), null_pointer_node,
9706 tf_warning_or_error);
9708 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9709 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9710 true, &dummy, token->location);
9711 while (true)
9713 token = cp_lexer_peek_token (parser->lexer);
9714 switch (token->type)
9716 case CPP_OPEN_SQUARE:
9717 /* offsetof-member-designator "[" expression "]" */
9718 expr = cp_parser_postfix_open_square_expression (parser, expr,
9719 true, false);
9720 break;
9722 case CPP_DEREF:
9723 /* offsetof-member-designator "->" identifier */
9724 expr = grok_array_decl (token->location, expr,
9725 integer_zero_node, false);
9726 /* FALLTHRU */
9728 case CPP_DOT:
9729 /* offsetof-member-designator "." identifier */
9730 cp_lexer_consume_token (parser->lexer);
9731 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9732 expr, true, &dummy,
9733 token->location);
9734 break;
9736 case CPP_CLOSE_PAREN:
9737 /* Consume the ")" token. */
9738 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9739 cp_lexer_consume_token (parser->lexer);
9740 goto success;
9742 default:
9743 /* Error. We know the following require will fail, but
9744 that gives the proper error message. */
9745 parens.require_close (parser);
9746 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9747 expr = error_mark_node;
9748 goto failure;
9752 success:
9753 /* Make a location of the form:
9754 __builtin_offsetof (struct s, f)
9755 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9756 with caret at the type-id, ranging from the start of the
9757 "_builtin_offsetof" token to the close paren. */
9758 loc = make_location (loc, start_loc, finish_loc);
9759 /* The result will be an INTEGER_CST, so we need to explicitly
9760 preserve the location. */
9761 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9763 failure:
9764 parser->integral_constant_expression_p = save_ice_p;
9765 parser->non_integral_constant_expression_p = save_non_ice_p;
9767 return expr;
9770 /* Parse a trait expression.
9772 Returns a representation of the expression, the underlying type
9773 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9775 static cp_expr
9776 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9778 cp_trait_kind kind;
9779 tree type1, type2 = NULL_TREE;
9780 bool binary = false;
9781 bool variadic = false;
9783 switch (keyword)
9785 case RID_HAS_NOTHROW_ASSIGN:
9786 kind = CPTK_HAS_NOTHROW_ASSIGN;
9787 break;
9788 case RID_HAS_NOTHROW_CONSTRUCTOR:
9789 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9790 break;
9791 case RID_HAS_NOTHROW_COPY:
9792 kind = CPTK_HAS_NOTHROW_COPY;
9793 break;
9794 case RID_HAS_TRIVIAL_ASSIGN:
9795 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9796 break;
9797 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9798 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9799 break;
9800 case RID_HAS_TRIVIAL_COPY:
9801 kind = CPTK_HAS_TRIVIAL_COPY;
9802 break;
9803 case RID_HAS_TRIVIAL_DESTRUCTOR:
9804 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9805 break;
9806 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9807 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9808 break;
9809 case RID_HAS_VIRTUAL_DESTRUCTOR:
9810 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9811 break;
9812 case RID_IS_ABSTRACT:
9813 kind = CPTK_IS_ABSTRACT;
9814 break;
9815 case RID_IS_AGGREGATE:
9816 kind = CPTK_IS_AGGREGATE;
9817 break;
9818 case RID_IS_BASE_OF:
9819 kind = CPTK_IS_BASE_OF;
9820 binary = true;
9821 break;
9822 case RID_IS_CLASS:
9823 kind = CPTK_IS_CLASS;
9824 break;
9825 case RID_IS_EMPTY:
9826 kind = CPTK_IS_EMPTY;
9827 break;
9828 case RID_IS_ENUM:
9829 kind = CPTK_IS_ENUM;
9830 break;
9831 case RID_IS_FINAL:
9832 kind = CPTK_IS_FINAL;
9833 break;
9834 case RID_IS_LITERAL_TYPE:
9835 kind = CPTK_IS_LITERAL_TYPE;
9836 break;
9837 case RID_IS_POD:
9838 kind = CPTK_IS_POD;
9839 break;
9840 case RID_IS_POLYMORPHIC:
9841 kind = CPTK_IS_POLYMORPHIC;
9842 break;
9843 case RID_IS_SAME_AS:
9844 kind = CPTK_IS_SAME_AS;
9845 binary = true;
9846 break;
9847 case RID_IS_STD_LAYOUT:
9848 kind = CPTK_IS_STD_LAYOUT;
9849 break;
9850 case RID_IS_TRIVIAL:
9851 kind = CPTK_IS_TRIVIAL;
9852 break;
9853 case RID_IS_TRIVIALLY_ASSIGNABLE:
9854 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9855 binary = true;
9856 break;
9857 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9858 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9859 variadic = true;
9860 break;
9861 case RID_IS_TRIVIALLY_COPYABLE:
9862 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9863 break;
9864 case RID_IS_UNION:
9865 kind = CPTK_IS_UNION;
9866 break;
9867 case RID_UNDERLYING_TYPE:
9868 kind = CPTK_UNDERLYING_TYPE;
9869 break;
9870 case RID_BASES:
9871 kind = CPTK_BASES;
9872 break;
9873 case RID_DIRECT_BASES:
9874 kind = CPTK_DIRECT_BASES;
9875 break;
9876 case RID_IS_ASSIGNABLE:
9877 kind = CPTK_IS_ASSIGNABLE;
9878 binary = true;
9879 break;
9880 case RID_IS_CONSTRUCTIBLE:
9881 kind = CPTK_IS_CONSTRUCTIBLE;
9882 variadic = true;
9883 break;
9884 default:
9885 gcc_unreachable ();
9888 /* Get location of initial token. */
9889 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9891 /* Consume the token. */
9892 cp_lexer_consume_token (parser->lexer);
9894 matching_parens parens;
9895 parens.require_open (parser);
9898 type_id_in_expr_sentinel s (parser);
9899 type1 = cp_parser_type_id (parser);
9902 if (type1 == error_mark_node)
9903 return error_mark_node;
9905 if (binary)
9907 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9910 type_id_in_expr_sentinel s (parser);
9911 type2 = cp_parser_type_id (parser);
9914 if (type2 == error_mark_node)
9915 return error_mark_node;
9917 else if (variadic)
9919 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9921 cp_lexer_consume_token (parser->lexer);
9922 tree elt = cp_parser_type_id (parser);
9923 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9925 cp_lexer_consume_token (parser->lexer);
9926 elt = make_pack_expansion (elt);
9928 if (elt == error_mark_node)
9929 return error_mark_node;
9930 type2 = tree_cons (NULL_TREE, elt, type2);
9934 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9935 parens.require_close (parser);
9937 /* Construct a location of the form:
9938 __is_trivially_copyable(_Tp)
9939 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
9940 with start == caret, finishing at the close-paren. */
9941 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
9943 /* Complete the trait expression, which may mean either processing
9944 the trait expr now or saving it for template instantiation. */
9945 switch (kind)
9947 case CPTK_UNDERLYING_TYPE:
9948 return cp_expr (finish_underlying_type (type1), trait_loc);
9949 case CPTK_BASES:
9950 return cp_expr (finish_bases (type1, false), trait_loc);
9951 case CPTK_DIRECT_BASES:
9952 return cp_expr (finish_bases (type1, true), trait_loc);
9953 default:
9954 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
9958 /* Parse a lambda expression.
9960 lambda-expression:
9961 lambda-introducer lambda-declarator [opt] compound-statement
9963 Returns a representation of the expression. */
9965 static cp_expr
9966 cp_parser_lambda_expression (cp_parser* parser)
9968 tree lambda_expr = build_lambda_expr ();
9969 tree type;
9970 bool ok = true;
9971 cp_token *token = cp_lexer_peek_token (parser->lexer);
9972 cp_token_position start = 0;
9974 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9976 if (cp_unevaluated_operand)
9978 if (!token->error_reported)
9980 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9981 "lambda-expression in unevaluated context");
9982 token->error_reported = true;
9984 ok = false;
9986 else if (parser->in_template_argument_list_p)
9988 if (!token->error_reported)
9990 error_at (token->location, "lambda-expression in template-argument");
9991 token->error_reported = true;
9993 ok = false;
9996 /* We may be in the middle of deferred access check. Disable
9997 it now. */
9998 push_deferring_access_checks (dk_no_deferred);
10000 cp_parser_lambda_introducer (parser, lambda_expr);
10002 type = begin_lambda_type (lambda_expr);
10003 if (type == error_mark_node)
10004 return error_mark_node;
10006 record_lambda_scope (lambda_expr);
10008 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10009 determine_visibility (TYPE_NAME (type));
10011 /* Now that we've started the type, add the capture fields for any
10012 explicit captures. */
10013 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10016 /* Inside the class, surrounding template-parameter-lists do not apply. */
10017 unsigned int saved_num_template_parameter_lists
10018 = parser->num_template_parameter_lists;
10019 unsigned char in_statement = parser->in_statement;
10020 bool in_switch_statement_p = parser->in_switch_statement_p;
10021 bool fully_implicit_function_template_p
10022 = parser->fully_implicit_function_template_p;
10023 tree implicit_template_parms = parser->implicit_template_parms;
10024 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10025 bool auto_is_implicit_function_template_parm_p
10026 = parser->auto_is_implicit_function_template_parm_p;
10028 parser->num_template_parameter_lists = 0;
10029 parser->in_statement = 0;
10030 parser->in_switch_statement_p = false;
10031 parser->fully_implicit_function_template_p = false;
10032 parser->implicit_template_parms = 0;
10033 parser->implicit_template_scope = 0;
10034 parser->auto_is_implicit_function_template_parm_p = false;
10036 /* By virtue of defining a local class, a lambda expression has access to
10037 the private variables of enclosing classes. */
10039 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10041 if (ok && cp_parser_error_occurred (parser))
10042 ok = false;
10044 if (ok)
10046 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10047 && cp_parser_start_tentative_firewall (parser))
10048 start = token;
10049 cp_parser_lambda_body (parser, lambda_expr);
10051 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10053 if (cp_parser_skip_to_closing_brace (parser))
10054 cp_lexer_consume_token (parser->lexer);
10057 /* The capture list was built up in reverse order; fix that now. */
10058 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10059 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10061 if (ok)
10062 maybe_add_lambda_conv_op (type);
10064 type = finish_struct (type, /*attributes=*/NULL_TREE);
10066 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10067 parser->in_statement = in_statement;
10068 parser->in_switch_statement_p = in_switch_statement_p;
10069 parser->fully_implicit_function_template_p
10070 = fully_implicit_function_template_p;
10071 parser->implicit_template_parms = implicit_template_parms;
10072 parser->implicit_template_scope = implicit_template_scope;
10073 parser->auto_is_implicit_function_template_parm_p
10074 = auto_is_implicit_function_template_parm_p;
10077 /* This field is only used during parsing of the lambda. */
10078 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10080 /* This lambda shouldn't have any proxies left at this point. */
10081 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10082 /* And now that we're done, push proxies for an enclosing lambda. */
10083 insert_pending_capture_proxies ();
10085 if (ok)
10086 lambda_expr = build_lambda_object (lambda_expr);
10087 else
10088 lambda_expr = error_mark_node;
10090 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10092 pop_deferring_access_checks ();
10094 return lambda_expr;
10097 /* Parse the beginning of a lambda expression.
10099 lambda-introducer:
10100 [ lambda-capture [opt] ]
10102 LAMBDA_EXPR is the current representation of the lambda expression. */
10104 static void
10105 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10107 /* Need commas after the first capture. */
10108 bool first = true;
10110 /* Eat the leading `['. */
10111 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10113 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10114 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10115 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10116 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10117 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10118 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10120 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10122 cp_lexer_consume_token (parser->lexer);
10123 first = false;
10126 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10128 cp_token* capture_token;
10129 tree capture_id;
10130 tree capture_init_expr;
10131 cp_id_kind idk = CP_ID_KIND_NONE;
10132 bool explicit_init_p = false;
10134 enum capture_kind_type
10136 BY_COPY,
10137 BY_REFERENCE
10139 enum capture_kind_type capture_kind = BY_COPY;
10141 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10143 error ("expected end of capture-list");
10144 return;
10147 if (first)
10148 first = false;
10149 else
10150 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10152 /* Possibly capture `this'. */
10153 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10155 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10156 if (cxx_dialect < cxx2a
10157 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10158 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10159 "with by-copy capture default");
10160 cp_lexer_consume_token (parser->lexer);
10161 add_capture (lambda_expr,
10162 /*id=*/this_identifier,
10163 /*initializer=*/finish_this_expr (),
10164 /*by_reference_p=*/true,
10165 explicit_init_p);
10166 continue;
10169 /* Possibly capture `*this'. */
10170 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10171 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10173 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10174 if (cxx_dialect < cxx17)
10175 pedwarn (loc, 0, "%<*this%> capture only available with "
10176 "-std=c++17 or -std=gnu++17");
10177 cp_lexer_consume_token (parser->lexer);
10178 cp_lexer_consume_token (parser->lexer);
10179 add_capture (lambda_expr,
10180 /*id=*/this_identifier,
10181 /*initializer=*/finish_this_expr (),
10182 /*by_reference_p=*/false,
10183 explicit_init_p);
10184 continue;
10187 /* Remember whether we want to capture as a reference or not. */
10188 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10190 capture_kind = BY_REFERENCE;
10191 cp_lexer_consume_token (parser->lexer);
10194 /* Get the identifier. */
10195 capture_token = cp_lexer_peek_token (parser->lexer);
10196 capture_id = cp_parser_identifier (parser);
10198 if (capture_id == error_mark_node)
10199 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10200 delimiters, but I modified this to stop on unnested ']' as well. It
10201 was already changed to stop on unnested '}', so the
10202 "closing_parenthesis" name is no more misleading with my change. */
10204 cp_parser_skip_to_closing_parenthesis (parser,
10205 /*recovering=*/true,
10206 /*or_comma=*/true,
10207 /*consume_paren=*/true);
10208 break;
10211 /* Find the initializer for this capture. */
10212 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10213 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10214 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10216 bool direct, non_constant;
10217 /* An explicit initializer exists. */
10218 if (cxx_dialect < cxx14)
10219 pedwarn (input_location, 0,
10220 "lambda capture initializers "
10221 "only available with -std=c++14 or -std=gnu++14");
10222 capture_init_expr = cp_parser_initializer (parser, &direct,
10223 &non_constant);
10224 explicit_init_p = true;
10225 if (capture_init_expr == NULL_TREE)
10227 error ("empty initializer for lambda init-capture");
10228 capture_init_expr = error_mark_node;
10231 else
10233 const char* error_msg;
10235 /* Turn the identifier into an id-expression. */
10236 capture_init_expr
10237 = cp_parser_lookup_name_simple (parser, capture_id,
10238 capture_token->location);
10240 if (capture_init_expr == error_mark_node)
10242 unqualified_name_lookup_error (capture_id);
10243 continue;
10245 else if (DECL_P (capture_init_expr)
10246 && (!VAR_P (capture_init_expr)
10247 && TREE_CODE (capture_init_expr) != PARM_DECL))
10249 error_at (capture_token->location,
10250 "capture of non-variable %qD ",
10251 capture_init_expr);
10252 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10253 "%q#D declared here", capture_init_expr);
10254 continue;
10256 if (VAR_P (capture_init_expr)
10257 && decl_storage_duration (capture_init_expr) != dk_auto)
10259 if (pedwarn (capture_token->location, 0, "capture of variable "
10260 "%qD with non-automatic storage duration",
10261 capture_init_expr))
10262 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10263 "%q#D declared here", capture_init_expr);
10264 continue;
10267 capture_init_expr
10268 = finish_id_expression
10269 (capture_id,
10270 capture_init_expr,
10271 parser->scope,
10272 &idk,
10273 /*integral_constant_expression_p=*/false,
10274 /*allow_non_integral_constant_expression_p=*/false,
10275 /*non_integral_constant_expression_p=*/NULL,
10276 /*template_p=*/false,
10277 /*done=*/true,
10278 /*address_p=*/false,
10279 /*template_arg_p=*/false,
10280 &error_msg,
10281 capture_token->location);
10283 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10285 cp_lexer_consume_token (parser->lexer);
10286 capture_init_expr = make_pack_expansion (capture_init_expr);
10288 else
10289 check_for_bare_parameter_packs (capture_init_expr);
10292 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10293 && !explicit_init_p)
10295 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10296 && capture_kind == BY_COPY)
10297 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10298 "of %qD redundant with by-copy capture default",
10299 capture_id);
10300 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10301 && capture_kind == BY_REFERENCE)
10302 pedwarn (capture_token->location, 0, "explicit by-reference "
10303 "capture of %qD redundant with by-reference capture "
10304 "default", capture_id);
10307 add_capture (lambda_expr,
10308 capture_id,
10309 capture_init_expr,
10310 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10311 explicit_init_p);
10314 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10317 /* Parse the (optional) middle of a lambda expression.
10319 lambda-declarator:
10320 < template-parameter-list [opt] >
10321 ( parameter-declaration-clause [opt] )
10322 attribute-specifier [opt]
10323 decl-specifier-seq [opt]
10324 exception-specification [opt]
10325 lambda-return-type-clause [opt]
10327 LAMBDA_EXPR is the current representation of the lambda expression. */
10329 static bool
10330 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10332 /* 5.1.1.4 of the standard says:
10333 If a lambda-expression does not include a lambda-declarator, it is as if
10334 the lambda-declarator were ().
10335 This means an empty parameter list, no attributes, and no exception
10336 specification. */
10337 tree param_list = void_list_node;
10338 tree attributes = NULL_TREE;
10339 tree exception_spec = NULL_TREE;
10340 tree template_param_list = NULL_TREE;
10341 tree tx_qual = NULL_TREE;
10342 tree return_type = NULL_TREE;
10343 cp_decl_specifier_seq lambda_specs;
10344 clear_decl_specs (&lambda_specs);
10346 /* The template-parameter-list is optional, but must begin with
10347 an opening angle if present. */
10348 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10350 if (cxx_dialect < cxx14)
10351 pedwarn (parser->lexer->next_token->location, 0,
10352 "lambda templates are only available with "
10353 "-std=c++14 or -std=gnu++14");
10354 else if (cxx_dialect < cxx2a)
10355 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10356 "lambda templates are only available with "
10357 "-std=c++2a or -std=gnu++2a");
10359 cp_lexer_consume_token (parser->lexer);
10361 template_param_list = cp_parser_template_parameter_list (parser);
10363 cp_parser_skip_to_end_of_template_parameter_list (parser);
10365 /* We just processed one more parameter list. */
10366 ++parser->num_template_parameter_lists;
10369 /* The parameter-declaration-clause is optional (unless
10370 template-parameter-list was given), but must begin with an
10371 opening parenthesis if present. */
10372 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10374 matching_parens parens;
10375 parens.consume_open (parser);
10377 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10379 /* Parse parameters. */
10380 param_list = cp_parser_parameter_declaration_clause (parser);
10382 /* Default arguments shall not be specified in the
10383 parameter-declaration-clause of a lambda-declarator. */
10384 if (cxx_dialect < cxx14)
10385 for (tree t = param_list; t; t = TREE_CHAIN (t))
10386 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10387 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10388 "default argument specified for lambda parameter");
10390 parens.require_close (parser);
10392 attributes = cp_parser_attributes_opt (parser);
10394 /* In the decl-specifier-seq of the lambda-declarator, each
10395 decl-specifier shall either be mutable or constexpr. */
10396 int declares_class_or_enum;
10397 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10398 cp_parser_decl_specifier_seq (parser,
10399 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10400 &lambda_specs, &declares_class_or_enum);
10401 if (lambda_specs.storage_class == sc_mutable)
10403 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10404 if (lambda_specs.conflicting_specifiers_p)
10405 error_at (lambda_specs.locations[ds_storage_class],
10406 "duplicate %<mutable%>");
10409 tx_qual = cp_parser_tx_qualifier_opt (parser);
10411 /* Parse optional exception specification. */
10412 exception_spec = cp_parser_exception_specification_opt (parser);
10414 /* Parse optional trailing return type. */
10415 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10417 cp_lexer_consume_token (parser->lexer);
10418 return_type = cp_parser_trailing_type_id (parser);
10421 /* The function parameters must be in scope all the way until after the
10422 trailing-return-type in case of decltype. */
10423 pop_bindings_and_leave_scope ();
10425 else if (template_param_list != NULL_TREE) // generate diagnostic
10426 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10428 /* Create the function call operator.
10430 Messing with declarators like this is no uglier than building up the
10431 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10432 other code. */
10434 cp_decl_specifier_seq return_type_specs;
10435 cp_declarator* declarator;
10436 tree fco;
10437 int quals;
10438 void *p;
10440 clear_decl_specs (&return_type_specs);
10441 if (return_type)
10442 return_type_specs.type = return_type;
10443 else
10444 /* Maybe we will deduce the return type later. */
10445 return_type_specs.type = make_auto ();
10447 if (lambda_specs.locations[ds_constexpr])
10449 if (cxx_dialect >= cxx17)
10450 return_type_specs.locations[ds_constexpr]
10451 = lambda_specs.locations[ds_constexpr];
10452 else
10453 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10454 "lambda only available with -std=c++17 or -std=gnu++17");
10457 p = obstack_alloc (&declarator_obstack, 0);
10459 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none);
10461 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10462 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10463 declarator = make_call_declarator (declarator, param_list, quals,
10464 VIRT_SPEC_UNSPECIFIED,
10465 REF_QUAL_NONE,
10466 tx_qual,
10467 exception_spec,
10468 /*late_return_type=*/NULL_TREE,
10469 /*requires_clause*/NULL_TREE);
10470 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10472 fco = grokmethod (&return_type_specs,
10473 declarator,
10474 attributes);
10475 if (fco != error_mark_node)
10477 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10478 DECL_ARTIFICIAL (fco) = 1;
10479 /* Give the object parameter a different name. */
10480 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10481 if (return_type)
10482 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10484 if (template_param_list)
10486 fco = finish_member_template_decl (fco);
10487 finish_template_decl (template_param_list);
10488 --parser->num_template_parameter_lists;
10490 else if (parser->fully_implicit_function_template_p)
10491 fco = finish_fully_implicit_template (parser, fco);
10493 finish_member_declaration (fco);
10495 obstack_free (&declarator_obstack, p);
10497 return (fco != error_mark_node);
10501 /* Parse the body of a lambda expression, which is simply
10503 compound-statement
10505 but which requires special handling.
10506 LAMBDA_EXPR is the current representation of the lambda expression. */
10508 static void
10509 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10511 bool nested = (current_function_decl != NULL_TREE);
10512 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10513 bool in_function_body = parser->in_function_body;
10514 if (nested)
10515 push_function_context ();
10516 else
10517 /* Still increment function_depth so that we don't GC in the
10518 middle of an expression. */
10519 ++function_depth;
10520 vec<tree> omp_privatization_save;
10521 save_omp_privatization_clauses (omp_privatization_save);
10522 /* Clear this in case we're in the middle of a default argument. */
10523 parser->local_variables_forbidden_p = false;
10524 parser->in_function_body = true;
10526 /* Finish the function call operator
10527 - class_specifier
10528 + late_parsing_for_member
10529 + function_definition_after_declarator
10530 + ctor_initializer_opt_and_function_body */
10532 local_specialization_stack s (lss_copy);
10534 tree fco = lambda_function (lambda_expr);
10535 tree body = start_lambda_function (fco, lambda_expr);
10536 bool done = false;
10537 tree compound_stmt;
10539 matching_braces braces;
10540 if (!braces.require_open (parser))
10541 goto out;
10543 compound_stmt = begin_compound_stmt (0);
10545 /* 5.1.1.4 of the standard says:
10546 If a lambda-expression does not include a trailing-return-type, it
10547 is as if the trailing-return-type denotes the following type:
10548 * if the compound-statement is of the form
10549 { return attribute-specifier [opt] expression ; }
10550 the type of the returned expression after lvalue-to-rvalue
10551 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10552 (_conv.array_ 4.2), and function-to-pointer conversion
10553 (_conv.func_ 4.3);
10554 * otherwise, void. */
10556 /* In a lambda that has neither a lambda-return-type-clause
10557 nor a deducible form, errors should be reported for return statements
10558 in the body. Since we used void as the placeholder return type, parsing
10559 the body as usual will give such desired behavior. */
10560 if (is_auto (TREE_TYPE (TREE_TYPE (fco)))
10561 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
10562 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
10564 tree expr = NULL_TREE;
10565 cp_id_kind idk = CP_ID_KIND_NONE;
10567 /* Parse tentatively in case there's more after the initial return
10568 statement. */
10569 cp_parser_parse_tentatively (parser);
10571 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
10573 expr = cp_parser_expression (parser, &idk);
10575 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10576 braces.require_close (parser);
10578 if (cp_parser_parse_definitely (parser))
10580 if (!processing_template_decl)
10582 tree type = lambda_return_type (expr);
10583 apply_deduced_return_type (fco, type);
10584 if (type == error_mark_node)
10585 expr = error_mark_node;
10588 /* Will get error here if type not deduced yet. */
10589 finish_return_stmt (expr);
10591 done = true;
10595 if (!done)
10597 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10598 cp_parser_label_declaration (parser);
10599 cp_parser_statement_seq_opt (parser, NULL_TREE);
10600 braces.require_close (parser);
10603 finish_compound_stmt (compound_stmt);
10605 out:
10606 finish_lambda_function (body);
10609 restore_omp_privatization_clauses (omp_privatization_save);
10610 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10611 parser->in_function_body = in_function_body;
10612 if (nested)
10613 pop_function_context();
10614 else
10615 --function_depth;
10618 /* Statements [gram.stmt.stmt] */
10620 /* Parse a statement.
10622 statement:
10623 labeled-statement
10624 expression-statement
10625 compound-statement
10626 selection-statement
10627 iteration-statement
10628 jump-statement
10629 declaration-statement
10630 try-block
10632 C++11:
10634 statement:
10635 labeled-statement
10636 attribute-specifier-seq (opt) expression-statement
10637 attribute-specifier-seq (opt) compound-statement
10638 attribute-specifier-seq (opt) selection-statement
10639 attribute-specifier-seq (opt) iteration-statement
10640 attribute-specifier-seq (opt) jump-statement
10641 declaration-statement
10642 attribute-specifier-seq (opt) try-block
10644 init-statement:
10645 expression-statement
10646 simple-declaration
10648 TM Extension:
10650 statement:
10651 atomic-statement
10653 IN_COMPOUND is true when the statement is nested inside a
10654 cp_parser_compound_statement; this matters for certain pragmas.
10656 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10657 is a (possibly labeled) if statement which is not enclosed in braces
10658 and has an else clause. This is used to implement -Wparentheses.
10660 CHAIN is a vector of if-else-if conditions. */
10662 static void
10663 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10664 bool in_compound, bool *if_p, vec<tree> *chain,
10665 location_t *loc_after_labels)
10667 tree statement, std_attrs = NULL_TREE;
10668 cp_token *token;
10669 location_t statement_location, attrs_location;
10671 restart:
10672 if (if_p != NULL)
10673 *if_p = false;
10674 /* There is no statement yet. */
10675 statement = NULL_TREE;
10677 saved_token_sentinel saved_tokens (parser->lexer);
10678 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10679 if (c_dialect_objc ())
10680 /* In obj-c++, seeing '[[' might be the either the beginning of
10681 c++11 attributes, or a nested objc-message-expression. So
10682 let's parse the c++11 attributes tentatively. */
10683 cp_parser_parse_tentatively (parser);
10684 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10685 if (c_dialect_objc ())
10687 if (!cp_parser_parse_definitely (parser))
10688 std_attrs = NULL_TREE;
10691 /* Peek at the next token. */
10692 token = cp_lexer_peek_token (parser->lexer);
10693 /* Remember the location of the first token in the statement. */
10694 statement_location = token->location;
10695 /* If this is a keyword, then that will often determine what kind of
10696 statement we have. */
10697 if (token->type == CPP_KEYWORD)
10699 enum rid keyword = token->keyword;
10701 switch (keyword)
10703 case RID_CASE:
10704 case RID_DEFAULT:
10705 /* Looks like a labeled-statement with a case label.
10706 Parse the label, and then use tail recursion to parse
10707 the statement. */
10708 cp_parser_label_for_labeled_statement (parser, std_attrs);
10709 in_compound = false;
10710 goto restart;
10712 case RID_IF:
10713 case RID_SWITCH:
10714 statement = cp_parser_selection_statement (parser, if_p, chain);
10715 break;
10717 case RID_WHILE:
10718 case RID_DO:
10719 case RID_FOR:
10720 statement = cp_parser_iteration_statement (parser, if_p, false);
10721 break;
10723 case RID_BREAK:
10724 case RID_CONTINUE:
10725 case RID_RETURN:
10726 case RID_GOTO:
10727 statement = cp_parser_jump_statement (parser);
10728 break;
10730 /* Objective-C++ exception-handling constructs. */
10731 case RID_AT_TRY:
10732 case RID_AT_CATCH:
10733 case RID_AT_FINALLY:
10734 case RID_AT_SYNCHRONIZED:
10735 case RID_AT_THROW:
10736 statement = cp_parser_objc_statement (parser);
10737 break;
10739 case RID_TRY:
10740 statement = cp_parser_try_block (parser);
10741 break;
10743 case RID_NAMESPACE:
10744 /* This must be a namespace alias definition. */
10745 cp_parser_declaration_statement (parser);
10746 return;
10748 case RID_TRANSACTION_ATOMIC:
10749 case RID_TRANSACTION_RELAXED:
10750 case RID_SYNCHRONIZED:
10751 case RID_ATOMIC_NOEXCEPT:
10752 case RID_ATOMIC_CANCEL:
10753 statement = cp_parser_transaction (parser, token);
10754 break;
10755 case RID_TRANSACTION_CANCEL:
10756 statement = cp_parser_transaction_cancel (parser);
10757 break;
10759 default:
10760 /* It might be a keyword like `int' that can start a
10761 declaration-statement. */
10762 break;
10765 else if (token->type == CPP_NAME)
10767 /* If the next token is a `:', then we are looking at a
10768 labeled-statement. */
10769 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10770 if (token->type == CPP_COLON)
10772 /* Looks like a labeled-statement with an ordinary label.
10773 Parse the label, and then use tail recursion to parse
10774 the statement. */
10776 cp_parser_label_for_labeled_statement (parser, std_attrs);
10777 in_compound = false;
10778 goto restart;
10781 /* Anything that starts with a `{' must be a compound-statement. */
10782 else if (token->type == CPP_OPEN_BRACE)
10783 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10784 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10785 a statement all its own. */
10786 else if (token->type == CPP_PRAGMA)
10788 /* Only certain OpenMP pragmas are attached to statements, and thus
10789 are considered statements themselves. All others are not. In
10790 the context of a compound, accept the pragma as a "statement" and
10791 return so that we can check for a close brace. Otherwise we
10792 require a real statement and must go back and read one. */
10793 if (in_compound)
10794 cp_parser_pragma (parser, pragma_compound, if_p);
10795 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10796 goto restart;
10797 return;
10799 else if (token->type == CPP_EOF)
10801 cp_parser_error (parser, "expected statement");
10802 return;
10805 /* Everything else must be a declaration-statement or an
10806 expression-statement. Try for the declaration-statement
10807 first, unless we are looking at a `;', in which case we know that
10808 we have an expression-statement. */
10809 if (!statement)
10811 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10813 if (std_attrs != NULL_TREE)
10815 /* Attributes should be parsed as part of the the
10816 declaration, so let's un-parse them. */
10817 saved_tokens.rollback();
10818 std_attrs = NULL_TREE;
10821 cp_parser_parse_tentatively (parser);
10822 /* Try to parse the declaration-statement. */
10823 cp_parser_declaration_statement (parser);
10824 /* If that worked, we're done. */
10825 if (cp_parser_parse_definitely (parser))
10826 return;
10828 /* All preceding labels have been parsed at this point. */
10829 if (loc_after_labels != NULL)
10830 *loc_after_labels = statement_location;
10832 /* Look for an expression-statement instead. */
10833 statement = cp_parser_expression_statement (parser, in_statement_expr);
10835 /* Handle [[fallthrough]];. */
10836 if (attribute_fallthrough_p (std_attrs))
10838 /* The next token after the fallthrough attribute is ';'. */
10839 if (statement == NULL_TREE)
10841 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10842 statement = build_call_expr_internal_loc (statement_location,
10843 IFN_FALLTHROUGH,
10844 void_type_node, 0);
10845 finish_expr_stmt (statement);
10847 else
10848 warning_at (statement_location, OPT_Wattributes,
10849 "%<fallthrough%> attribute not followed by %<;%>");
10850 std_attrs = NULL_TREE;
10854 /* Set the line number for the statement. */
10855 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10856 SET_EXPR_LOCATION (statement, statement_location);
10858 /* Allow "[[fallthrough]];", but warn otherwise. */
10859 if (std_attrs != NULL_TREE)
10860 warning_at (attrs_location,
10861 OPT_Wattributes,
10862 "attributes at the beginning of statement are ignored");
10865 /* Parse the label for a labeled-statement, i.e.
10867 identifier :
10868 case constant-expression :
10869 default :
10871 GNU Extension:
10872 case constant-expression ... constant-expression : statement
10874 When a label is parsed without errors, the label is added to the
10875 parse tree by the finish_* functions, so this function doesn't
10876 have to return the label. */
10878 static void
10879 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10881 cp_token *token;
10882 tree label = NULL_TREE;
10883 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10885 /* The next token should be an identifier. */
10886 token = cp_lexer_peek_token (parser->lexer);
10887 if (token->type != CPP_NAME
10888 && token->type != CPP_KEYWORD)
10890 cp_parser_error (parser, "expected labeled-statement");
10891 return;
10894 /* Remember whether this case or a user-defined label is allowed to fall
10895 through to. */
10896 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
10898 parser->colon_corrects_to_scope_p = false;
10899 switch (token->keyword)
10901 case RID_CASE:
10903 tree expr, expr_hi;
10904 cp_token *ellipsis;
10906 /* Consume the `case' token. */
10907 cp_lexer_consume_token (parser->lexer);
10908 /* Parse the constant-expression. */
10909 expr = cp_parser_constant_expression (parser);
10910 if (check_for_bare_parameter_packs (expr))
10911 expr = error_mark_node;
10913 ellipsis = cp_lexer_peek_token (parser->lexer);
10914 if (ellipsis->type == CPP_ELLIPSIS)
10916 /* Consume the `...' token. */
10917 cp_lexer_consume_token (parser->lexer);
10918 expr_hi = cp_parser_constant_expression (parser);
10919 if (check_for_bare_parameter_packs (expr_hi))
10920 expr_hi = error_mark_node;
10922 /* We don't need to emit warnings here, as the common code
10923 will do this for us. */
10925 else
10926 expr_hi = NULL_TREE;
10928 if (parser->in_switch_statement_p)
10930 tree l = finish_case_label (token->location, expr, expr_hi);
10931 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10932 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10934 else
10935 error_at (token->location,
10936 "case label %qE not within a switch statement",
10937 expr);
10939 break;
10941 case RID_DEFAULT:
10942 /* Consume the `default' token. */
10943 cp_lexer_consume_token (parser->lexer);
10945 if (parser->in_switch_statement_p)
10947 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
10948 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10949 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10951 else
10952 error_at (token->location, "case label not within a switch statement");
10953 break;
10955 default:
10956 /* Anything else must be an ordinary label. */
10957 label = finish_label_stmt (cp_parser_identifier (parser));
10958 if (label && TREE_CODE (label) == LABEL_DECL)
10959 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
10960 break;
10963 /* Require the `:' token. */
10964 cp_parser_require (parser, CPP_COLON, RT_COLON);
10966 /* An ordinary label may optionally be followed by attributes.
10967 However, this is only permitted if the attributes are then
10968 followed by a semicolon. This is because, for backward
10969 compatibility, when parsing
10970 lab: __attribute__ ((unused)) int i;
10971 we want the attribute to attach to "i", not "lab". */
10972 if (label != NULL_TREE
10973 && cp_next_tokens_can_be_gnu_attribute_p (parser))
10975 tree attrs;
10976 cp_parser_parse_tentatively (parser);
10977 attrs = cp_parser_gnu_attributes_opt (parser);
10978 if (attrs == NULL_TREE
10979 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10980 cp_parser_abort_tentative_parse (parser);
10981 else if (!cp_parser_parse_definitely (parser))
10983 else
10984 attributes = chainon (attributes, attrs);
10987 if (attributes != NULL_TREE)
10988 cplus_decl_attributes (&label, attributes, 0);
10990 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10993 /* Parse an expression-statement.
10995 expression-statement:
10996 expression [opt] ;
10998 Returns the new EXPR_STMT -- or NULL_TREE if the expression
10999 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11000 indicates whether this expression-statement is part of an
11001 expression statement. */
11003 static tree
11004 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11006 tree statement = NULL_TREE;
11007 cp_token *token = cp_lexer_peek_token (parser->lexer);
11008 location_t loc = token->location;
11010 /* There might be attribute fallthrough. */
11011 tree attr = cp_parser_gnu_attributes_opt (parser);
11013 /* If the next token is a ';', then there is no expression
11014 statement. */
11015 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11017 statement = cp_parser_expression (parser);
11018 if (statement == error_mark_node
11019 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11021 cp_parser_skip_to_end_of_block_or_statement (parser);
11022 return error_mark_node;
11026 /* Handle [[fallthrough]];. */
11027 if (attribute_fallthrough_p (attr))
11029 /* The next token after the fallthrough attribute is ';'. */
11030 if (statement == NULL_TREE)
11031 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11032 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11033 void_type_node, 0);
11034 else
11035 warning_at (loc, OPT_Wattributes,
11036 "%<fallthrough%> attribute not followed by %<;%>");
11037 attr = NULL_TREE;
11040 /* Allow "[[fallthrough]];", but warn otherwise. */
11041 if (attr != NULL_TREE)
11042 warning_at (loc, OPT_Wattributes,
11043 "attributes at the beginning of statement are ignored");
11045 /* Give a helpful message for "A<T>::type t;" and the like. */
11046 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11047 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11049 if (TREE_CODE (statement) == SCOPE_REF)
11050 error_at (token->location, "need %<typename%> before %qE because "
11051 "%qT is a dependent scope",
11052 statement, TREE_OPERAND (statement, 0));
11053 else if (is_overloaded_fn (statement)
11054 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11056 /* A::A a; */
11057 tree fn = get_first_fn (statement);
11058 error_at (token->location,
11059 "%<%T::%D%> names the constructor, not the type",
11060 DECL_CONTEXT (fn), DECL_NAME (fn));
11064 /* Consume the final `;'. */
11065 cp_parser_consume_semicolon_at_end_of_statement (parser);
11067 if (in_statement_expr
11068 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11069 /* This is the final expression statement of a statement
11070 expression. */
11071 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11072 else if (statement)
11073 statement = finish_expr_stmt (statement);
11075 return statement;
11078 /* Parse a compound-statement.
11080 compound-statement:
11081 { statement-seq [opt] }
11083 GNU extension:
11085 compound-statement:
11086 { label-declaration-seq [opt] statement-seq [opt] }
11088 label-declaration-seq:
11089 label-declaration
11090 label-declaration-seq label-declaration
11092 Returns a tree representing the statement. */
11094 static tree
11095 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11096 int bcs_flags, bool function_body)
11098 tree compound_stmt;
11099 matching_braces braces;
11101 /* Consume the `{'. */
11102 if (!braces.require_open (parser))
11103 return error_mark_node;
11104 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11105 && !function_body && cxx_dialect < cxx14)
11106 pedwarn (input_location, OPT_Wpedantic,
11107 "compound-statement in %<constexpr%> function");
11108 /* Begin the compound-statement. */
11109 compound_stmt = begin_compound_stmt (bcs_flags);
11110 /* If the next keyword is `__label__' we have a label declaration. */
11111 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11112 cp_parser_label_declaration (parser);
11113 /* Parse an (optional) statement-seq. */
11114 cp_parser_statement_seq_opt (parser, in_statement_expr);
11115 /* Finish the compound-statement. */
11116 finish_compound_stmt (compound_stmt);
11117 /* Consume the `}'. */
11118 braces.require_close (parser);
11120 return compound_stmt;
11123 /* Parse an (optional) statement-seq.
11125 statement-seq:
11126 statement
11127 statement-seq [opt] statement */
11129 static void
11130 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11132 /* Scan statements until there aren't any more. */
11133 while (true)
11135 cp_token *token = cp_lexer_peek_token (parser->lexer);
11137 /* If we are looking at a `}', then we have run out of
11138 statements; the same is true if we have reached the end
11139 of file, or have stumbled upon a stray '@end'. */
11140 if (token->type == CPP_CLOSE_BRACE
11141 || token->type == CPP_EOF
11142 || token->type == CPP_PRAGMA_EOL
11143 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11144 break;
11146 /* If we are in a compound statement and find 'else' then
11147 something went wrong. */
11148 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11150 if (parser->in_statement & IN_IF_STMT)
11151 break;
11152 else
11154 token = cp_lexer_consume_token (parser->lexer);
11155 error_at (token->location, "%<else%> without a previous %<if%>");
11159 /* Parse the statement. */
11160 cp_parser_statement (parser, in_statement_expr, true, NULL);
11164 /* Return true if we're looking at (init; cond), false otherwise. */
11166 static bool
11167 cp_parser_init_statement_p (cp_parser *parser)
11169 /* Save tokens so that we can put them back. */
11170 cp_lexer_save_tokens (parser->lexer);
11172 /* Look for ';' that is not nested in () or {}. */
11173 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11174 /*recovering=*/false,
11175 CPP_SEMICOLON,
11176 /*consume_paren=*/false);
11178 /* Roll back the tokens we skipped. */
11179 cp_lexer_rollback_tokens (parser->lexer);
11181 return ret == -1;
11184 /* Parse a selection-statement.
11186 selection-statement:
11187 if ( init-statement [opt] condition ) statement
11188 if ( init-statement [opt] condition ) statement else statement
11189 switch ( init-statement [opt] condition ) statement
11191 Returns the new IF_STMT or SWITCH_STMT.
11193 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11194 is a (possibly labeled) if statement which is not enclosed in
11195 braces and has an else clause. This is used to implement
11196 -Wparentheses.
11198 CHAIN is a vector of if-else-if conditions. This is used to implement
11199 -Wduplicated-cond. */
11201 static tree
11202 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11203 vec<tree> *chain)
11205 cp_token *token;
11206 enum rid keyword;
11207 token_indent_info guard_tinfo;
11209 if (if_p != NULL)
11210 *if_p = false;
11212 /* Peek at the next token. */
11213 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11214 guard_tinfo = get_token_indent_info (token);
11216 /* See what kind of keyword it is. */
11217 keyword = token->keyword;
11218 switch (keyword)
11220 case RID_IF:
11221 case RID_SWITCH:
11223 tree statement;
11224 tree condition;
11226 bool cx = false;
11227 if (keyword == RID_IF
11228 && cp_lexer_next_token_is_keyword (parser->lexer,
11229 RID_CONSTEXPR))
11231 cx = true;
11232 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11233 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11234 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11235 "with -std=c++17 or -std=gnu++17");
11238 /* Look for the `('. */
11239 matching_parens parens;
11240 if (!parens.require_open (parser))
11242 cp_parser_skip_to_end_of_statement (parser);
11243 return error_mark_node;
11246 /* Begin the selection-statement. */
11247 if (keyword == RID_IF)
11249 statement = begin_if_stmt ();
11250 IF_STMT_CONSTEXPR_P (statement) = cx;
11252 else
11253 statement = begin_switch_stmt ();
11255 /* Parse the optional init-statement. */
11256 if (cp_parser_init_statement_p (parser))
11258 tree decl;
11259 if (cxx_dialect < cxx17)
11260 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11261 "init-statement in selection statements only available "
11262 "with -std=c++17 or -std=gnu++17");
11263 cp_parser_init_statement (parser, &decl);
11266 /* Parse the condition. */
11267 condition = cp_parser_condition (parser);
11268 /* Look for the `)'. */
11269 if (!parens.require_close (parser))
11270 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11271 /*consume_paren=*/true);
11273 if (keyword == RID_IF)
11275 bool nested_if;
11276 unsigned char in_statement;
11278 /* Add the condition. */
11279 condition = finish_if_stmt_cond (condition, statement);
11281 if (warn_duplicated_cond)
11282 warn_duplicated_cond_add_or_warn (token->location, condition,
11283 &chain);
11285 /* Parse the then-clause. */
11286 in_statement = parser->in_statement;
11287 parser->in_statement |= IN_IF_STMT;
11289 /* Outside a template, the non-selected branch of a constexpr
11290 if is a 'discarded statement', i.e. unevaluated. */
11291 bool was_discarded = in_discarded_stmt;
11292 bool discard_then = (cx && !processing_template_decl
11293 && integer_zerop (condition));
11294 if (discard_then)
11296 in_discarded_stmt = true;
11297 ++c_inhibit_evaluation_warnings;
11300 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11301 guard_tinfo);
11303 parser->in_statement = in_statement;
11305 finish_then_clause (statement);
11307 if (discard_then)
11309 THEN_CLAUSE (statement) = NULL_TREE;
11310 in_discarded_stmt = was_discarded;
11311 --c_inhibit_evaluation_warnings;
11314 /* If the next token is `else', parse the else-clause. */
11315 if (cp_lexer_next_token_is_keyword (parser->lexer,
11316 RID_ELSE))
11318 bool discard_else = (cx && !processing_template_decl
11319 && integer_nonzerop (condition));
11320 if (discard_else)
11322 in_discarded_stmt = true;
11323 ++c_inhibit_evaluation_warnings;
11326 guard_tinfo
11327 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11328 /* Consume the `else' keyword. */
11329 cp_lexer_consume_token (parser->lexer);
11330 if (warn_duplicated_cond)
11332 if (cp_lexer_next_token_is_keyword (parser->lexer,
11333 RID_IF)
11334 && chain == NULL)
11336 /* We've got "if (COND) else if (COND2)". Start
11337 the condition chain and add COND as the first
11338 element. */
11339 chain = new vec<tree> ();
11340 if (!CONSTANT_CLASS_P (condition)
11341 && !TREE_SIDE_EFFECTS (condition))
11343 /* Wrap it in a NOP_EXPR so that we can set the
11344 location of the condition. */
11345 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11346 condition);
11347 SET_EXPR_LOCATION (e, token->location);
11348 chain->safe_push (e);
11351 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11352 RID_IF))
11354 /* This is if-else without subsequent if. Zap the
11355 condition chain; we would have already warned at
11356 this point. */
11357 delete chain;
11358 chain = NULL;
11361 begin_else_clause (statement);
11362 /* Parse the else-clause. */
11363 cp_parser_implicitly_scoped_statement (parser, NULL,
11364 guard_tinfo, chain);
11366 finish_else_clause (statement);
11368 /* If we are currently parsing a then-clause, then
11369 IF_P will not be NULL. We set it to true to
11370 indicate that this if statement has an else clause.
11371 This may trigger the Wparentheses warning below
11372 when we get back up to the parent if statement. */
11373 if (if_p != NULL)
11374 *if_p = true;
11376 if (discard_else)
11378 ELSE_CLAUSE (statement) = NULL_TREE;
11379 in_discarded_stmt = was_discarded;
11380 --c_inhibit_evaluation_warnings;
11383 else
11385 /* This if statement does not have an else clause. If
11386 NESTED_IF is true, then the then-clause has an if
11387 statement which does have an else clause. We warn
11388 about the potential ambiguity. */
11389 if (nested_if)
11390 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11391 "suggest explicit braces to avoid ambiguous"
11392 " %<else%>");
11393 if (warn_duplicated_cond)
11395 /* We don't need the condition chain anymore. */
11396 delete chain;
11397 chain = NULL;
11401 /* Now we're all done with the if-statement. */
11402 finish_if_stmt (statement);
11404 else
11406 bool in_switch_statement_p;
11407 unsigned char in_statement;
11409 /* Add the condition. */
11410 finish_switch_cond (condition, statement);
11412 /* Parse the body of the switch-statement. */
11413 in_switch_statement_p = parser->in_switch_statement_p;
11414 in_statement = parser->in_statement;
11415 parser->in_switch_statement_p = true;
11416 parser->in_statement |= IN_SWITCH_STMT;
11417 cp_parser_implicitly_scoped_statement (parser, if_p,
11418 guard_tinfo);
11419 parser->in_switch_statement_p = in_switch_statement_p;
11420 parser->in_statement = in_statement;
11422 /* Now we're all done with the switch-statement. */
11423 finish_switch_stmt (statement);
11426 return statement;
11428 break;
11430 default:
11431 cp_parser_error (parser, "expected selection-statement");
11432 return error_mark_node;
11436 /* Parse a condition.
11438 condition:
11439 expression
11440 type-specifier-seq declarator = initializer-clause
11441 type-specifier-seq declarator braced-init-list
11443 GNU Extension:
11445 condition:
11446 type-specifier-seq declarator asm-specification [opt]
11447 attributes [opt] = assignment-expression
11449 Returns the expression that should be tested. */
11451 static tree
11452 cp_parser_condition (cp_parser* parser)
11454 cp_decl_specifier_seq type_specifiers;
11455 const char *saved_message;
11456 int declares_class_or_enum;
11458 /* Try the declaration first. */
11459 cp_parser_parse_tentatively (parser);
11460 /* New types are not allowed in the type-specifier-seq for a
11461 condition. */
11462 saved_message = parser->type_definition_forbidden_message;
11463 parser->type_definition_forbidden_message
11464 = G_("types may not be defined in conditions");
11465 /* Parse the type-specifier-seq. */
11466 cp_parser_decl_specifier_seq (parser,
11467 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11468 &type_specifiers,
11469 &declares_class_or_enum);
11470 /* Restore the saved message. */
11471 parser->type_definition_forbidden_message = saved_message;
11472 /* If all is well, we might be looking at a declaration. */
11473 if (!cp_parser_error_occurred (parser))
11475 tree decl;
11476 tree asm_specification;
11477 tree attributes;
11478 cp_declarator *declarator;
11479 tree initializer = NULL_TREE;
11481 /* Parse the declarator. */
11482 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11483 /*ctor_dtor_or_conv_p=*/NULL,
11484 /*parenthesized_p=*/NULL,
11485 /*member_p=*/false,
11486 /*friend_p=*/false);
11487 /* Parse the attributes. */
11488 attributes = cp_parser_attributes_opt (parser);
11489 /* Parse the asm-specification. */
11490 asm_specification = cp_parser_asm_specification_opt (parser);
11491 /* If the next token is not an `=' or '{', then we might still be
11492 looking at an expression. For example:
11494 if (A(a).x)
11496 looks like a decl-specifier-seq and a declarator -- but then
11497 there is no `=', so this is an expression. */
11498 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11499 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11500 cp_parser_simulate_error (parser);
11502 /* If we did see an `=' or '{', then we are looking at a declaration
11503 for sure. */
11504 if (cp_parser_parse_definitely (parser))
11506 tree pushed_scope;
11507 bool non_constant_p;
11508 int flags = LOOKUP_ONLYCONVERTING;
11510 /* Create the declaration. */
11511 decl = start_decl (declarator, &type_specifiers,
11512 /*initialized_p=*/true,
11513 attributes, /*prefix_attributes=*/NULL_TREE,
11514 &pushed_scope);
11516 /* Parse the initializer. */
11517 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11519 initializer = cp_parser_braced_list (parser, &non_constant_p);
11520 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11521 flags = 0;
11523 else
11525 /* Consume the `='. */
11526 cp_parser_require (parser, CPP_EQ, RT_EQ);
11527 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11529 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11530 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11532 /* Process the initializer. */
11533 cp_finish_decl (decl,
11534 initializer, !non_constant_p,
11535 asm_specification,
11536 flags);
11538 if (pushed_scope)
11539 pop_scope (pushed_scope);
11541 return convert_from_reference (decl);
11544 /* If we didn't even get past the declarator successfully, we are
11545 definitely not looking at a declaration. */
11546 else
11547 cp_parser_abort_tentative_parse (parser);
11549 /* Otherwise, we are looking at an expression. */
11550 return cp_parser_expression (parser);
11553 /* Parses a for-statement or range-for-statement until the closing ')',
11554 not included. */
11556 static tree
11557 cp_parser_for (cp_parser *parser, bool ivdep)
11559 tree init, scope, decl;
11560 bool is_range_for;
11562 /* Begin the for-statement. */
11563 scope = begin_for_scope (&init);
11565 /* Parse the initialization. */
11566 is_range_for = cp_parser_init_statement (parser, &decl);
11568 if (is_range_for)
11569 return cp_parser_range_for (parser, scope, init, decl, ivdep);
11570 else
11571 return cp_parser_c_for (parser, scope, init, ivdep);
11574 static tree
11575 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11577 /* Normal for loop */
11578 tree condition = NULL_TREE;
11579 tree expression = NULL_TREE;
11580 tree stmt;
11582 stmt = begin_for_stmt (scope, init);
11583 /* The init-statement has already been parsed in
11584 cp_parser_init_statement, so no work is needed here. */
11585 finish_init_stmt (stmt);
11587 /* If there's a condition, process it. */
11588 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11589 condition = cp_parser_condition (parser);
11590 else if (ivdep)
11592 cp_parser_error (parser, "missing loop condition in loop with "
11593 "%<GCC ivdep%> pragma");
11594 condition = error_mark_node;
11596 finish_for_cond (condition, stmt, ivdep);
11597 /* Look for the `;'. */
11598 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11600 /* If there's an expression, process it. */
11601 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11602 expression = cp_parser_expression (parser);
11603 finish_for_expr (expression, stmt);
11605 return stmt;
11608 /* Tries to parse a range-based for-statement:
11610 range-based-for:
11611 decl-specifier-seq declarator : expression
11613 The decl-specifier-seq declarator and the `:' are already parsed by
11614 cp_parser_init_statement. If processing_template_decl it returns a
11615 newly created RANGE_FOR_STMT; if not, it is converted to a
11616 regular FOR_STMT. */
11618 static tree
11619 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11620 bool ivdep)
11622 tree stmt, range_expr;
11623 auto_vec <cxx_binding *, 16> bindings;
11624 auto_vec <tree, 16> names;
11625 tree decomp_first_name = NULL_TREE;
11626 unsigned int decomp_cnt = 0;
11628 /* Get the range declaration momentarily out of the way so that
11629 the range expression doesn't clash with it. */
11630 if (range_decl != error_mark_node)
11632 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11634 tree v = DECL_VALUE_EXPR (range_decl);
11635 /* For decomposition declaration get all of the corresponding
11636 declarations out of the way. */
11637 if (TREE_CODE (v) == ARRAY_REF
11638 && VAR_P (TREE_OPERAND (v, 0))
11639 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11641 tree d = range_decl;
11642 range_decl = TREE_OPERAND (v, 0);
11643 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11644 decomp_first_name = d;
11645 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11647 tree name = DECL_NAME (d);
11648 names.safe_push (name);
11649 bindings.safe_push (IDENTIFIER_BINDING (name));
11650 IDENTIFIER_BINDING (name)
11651 = IDENTIFIER_BINDING (name)->previous;
11655 if (names.is_empty ())
11657 tree name = DECL_NAME (range_decl);
11658 names.safe_push (name);
11659 bindings.safe_push (IDENTIFIER_BINDING (name));
11660 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11664 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11666 bool expr_non_constant_p;
11667 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11669 else
11670 range_expr = cp_parser_expression (parser);
11672 /* Put the range declaration(s) back into scope. */
11673 for (unsigned int i = 0; i < names.length (); i++)
11675 cxx_binding *binding = bindings[i];
11676 binding->previous = IDENTIFIER_BINDING (names[i]);
11677 IDENTIFIER_BINDING (names[i]) = binding;
11680 /* If in template, STMT is converted to a normal for-statement
11681 at instantiation. If not, it is done just ahead. */
11682 if (processing_template_decl)
11684 if (check_for_bare_parameter_packs (range_expr))
11685 range_expr = error_mark_node;
11686 stmt = begin_range_for_stmt (scope, init);
11687 if (ivdep)
11688 RANGE_FOR_IVDEP (stmt) = 1;
11689 finish_range_for_decl (stmt, range_decl, range_expr);
11690 if (!type_dependent_expression_p (range_expr)
11691 /* do_auto_deduction doesn't mess with template init-lists. */
11692 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11693 do_range_for_auto_deduction (range_decl, range_expr);
11695 else
11697 stmt = begin_for_stmt (scope, init);
11698 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11699 decomp_first_name, decomp_cnt, ivdep);
11701 return stmt;
11704 /* Subroutine of cp_convert_range_for: given the initializer expression,
11705 builds up the range temporary. */
11707 static tree
11708 build_range_temp (tree range_expr)
11710 tree range_type, range_temp;
11712 /* Find out the type deduced by the declaration
11713 `auto &&__range = range_expr'. */
11714 range_type = cp_build_reference_type (make_auto (), true);
11715 range_type = do_auto_deduction (range_type, range_expr,
11716 type_uses_auto (range_type));
11718 /* Create the __range variable. */
11719 range_temp = build_decl (input_location, VAR_DECL,
11720 get_identifier ("__for_range"), range_type);
11721 TREE_USED (range_temp) = 1;
11722 DECL_ARTIFICIAL (range_temp) = 1;
11724 return range_temp;
11727 /* Used by cp_parser_range_for in template context: we aren't going to
11728 do a full conversion yet, but we still need to resolve auto in the
11729 type of the for-range-declaration if present. This is basically
11730 a shortcut version of cp_convert_range_for. */
11732 static void
11733 do_range_for_auto_deduction (tree decl, tree range_expr)
11735 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11736 if (auto_node)
11738 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11739 range_temp = convert_from_reference (build_range_temp (range_expr));
11740 iter_type = (cp_parser_perform_range_for_lookup
11741 (range_temp, &begin_dummy, &end_dummy));
11742 if (iter_type)
11744 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11745 iter_type);
11746 iter_decl = build_x_indirect_ref (input_location, iter_decl,
11747 RO_UNARY_STAR,
11748 tf_warning_or_error);
11749 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11750 iter_decl, auto_node);
11755 /* Converts a range-based for-statement into a normal
11756 for-statement, as per the definition.
11758 for (RANGE_DECL : RANGE_EXPR)
11759 BLOCK
11761 should be equivalent to:
11764 auto &&__range = RANGE_EXPR;
11765 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11766 __begin != __end;
11767 ++__begin)
11769 RANGE_DECL = *__begin;
11770 BLOCK
11774 If RANGE_EXPR is an array:
11775 BEGIN_EXPR = __range
11776 END_EXPR = __range + ARRAY_SIZE(__range)
11777 Else if RANGE_EXPR has a member 'begin' or 'end':
11778 BEGIN_EXPR = __range.begin()
11779 END_EXPR = __range.end()
11780 Else:
11781 BEGIN_EXPR = begin(__range)
11782 END_EXPR = end(__range);
11784 If __range has a member 'begin' but not 'end', or vice versa, we must
11785 still use the second alternative (it will surely fail, however).
11786 When calling begin()/end() in the third alternative we must use
11787 argument dependent lookup, but always considering 'std' as an associated
11788 namespace. */
11790 tree
11791 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11792 tree decomp_first_name, unsigned int decomp_cnt,
11793 bool ivdep)
11795 tree begin, end;
11796 tree iter_type, begin_expr, end_expr;
11797 tree condition, expression;
11799 range_expr = mark_lvalue_use (range_expr);
11801 if (range_decl == error_mark_node || range_expr == error_mark_node)
11802 /* If an error happened previously do nothing or else a lot of
11803 unhelpful errors would be issued. */
11804 begin_expr = end_expr = iter_type = error_mark_node;
11805 else
11807 tree range_temp;
11809 if (VAR_P (range_expr)
11810 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11811 /* Can't bind a reference to an array of runtime bound. */
11812 range_temp = range_expr;
11813 else
11815 range_temp = build_range_temp (range_expr);
11816 pushdecl (range_temp);
11817 cp_finish_decl (range_temp, range_expr,
11818 /*is_constant_init*/false, NULL_TREE,
11819 LOOKUP_ONLYCONVERTING);
11820 range_temp = convert_from_reference (range_temp);
11822 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11823 &begin_expr, &end_expr);
11826 /* The new for initialization statement. */
11827 begin = build_decl (input_location, VAR_DECL,
11828 get_identifier ("__for_begin"), iter_type);
11829 TREE_USED (begin) = 1;
11830 DECL_ARTIFICIAL (begin) = 1;
11831 pushdecl (begin);
11832 cp_finish_decl (begin, begin_expr,
11833 /*is_constant_init*/false, NULL_TREE,
11834 LOOKUP_ONLYCONVERTING);
11836 if (cxx_dialect >= cxx17)
11837 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11838 end = build_decl (input_location, VAR_DECL,
11839 get_identifier ("__for_end"), iter_type);
11840 TREE_USED (end) = 1;
11841 DECL_ARTIFICIAL (end) = 1;
11842 pushdecl (end);
11843 cp_finish_decl (end, end_expr,
11844 /*is_constant_init*/false, NULL_TREE,
11845 LOOKUP_ONLYCONVERTING);
11847 finish_init_stmt (statement);
11849 /* The new for condition. */
11850 condition = build_x_binary_op (input_location, NE_EXPR,
11851 begin, ERROR_MARK,
11852 end, ERROR_MARK,
11853 NULL, tf_warning_or_error);
11854 finish_for_cond (condition, statement, ivdep);
11856 /* The new increment expression. */
11857 expression = finish_unary_op_expr (input_location,
11858 PREINCREMENT_EXPR, begin,
11859 tf_warning_or_error);
11860 finish_for_expr (expression, statement);
11862 /* The declaration is initialized with *__begin inside the loop body. */
11863 cp_finish_decl (range_decl,
11864 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
11865 tf_warning_or_error),
11866 /*is_constant_init*/false, NULL_TREE,
11867 LOOKUP_ONLYCONVERTING);
11868 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11869 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
11871 return statement;
11874 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11875 We need to solve both at the same time because the method used
11876 depends on the existence of members begin or end.
11877 Returns the type deduced for the iterator expression. */
11879 static tree
11880 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11882 if (error_operand_p (range))
11884 *begin = *end = error_mark_node;
11885 return error_mark_node;
11888 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11890 error ("range-based %<for%> expression of type %qT "
11891 "has incomplete type", TREE_TYPE (range));
11892 *begin = *end = error_mark_node;
11893 return error_mark_node;
11895 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11897 /* If RANGE is an array, we will use pointer arithmetic. */
11898 *begin = decay_conversion (range, tf_warning_or_error);
11899 *end = build_binary_op (input_location, PLUS_EXPR,
11900 range,
11901 array_type_nelts_top (TREE_TYPE (range)),
11902 false);
11903 return TREE_TYPE (*begin);
11905 else
11907 /* If it is not an array, we must do a bit of magic. */
11908 tree id_begin, id_end;
11909 tree member_begin, member_end;
11911 *begin = *end = error_mark_node;
11913 id_begin = get_identifier ("begin");
11914 id_end = get_identifier ("end");
11915 member_begin = lookup_member (TREE_TYPE (range), id_begin,
11916 /*protect=*/2, /*want_type=*/false,
11917 tf_warning_or_error);
11918 member_end = lookup_member (TREE_TYPE (range), id_end,
11919 /*protect=*/2, /*want_type=*/false,
11920 tf_warning_or_error);
11922 if (member_begin != NULL_TREE || member_end != NULL_TREE)
11924 /* Use the member functions. */
11925 if (member_begin != NULL_TREE)
11926 *begin = cp_parser_range_for_member_function (range, id_begin);
11927 else
11928 error ("range-based %<for%> expression of type %qT has an "
11929 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11931 if (member_end != NULL_TREE)
11932 *end = cp_parser_range_for_member_function (range, id_end);
11933 else
11934 error ("range-based %<for%> expression of type %qT has a "
11935 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
11937 else
11939 /* Use global functions with ADL. */
11940 vec<tree, va_gc> *vec;
11941 vec = make_tree_vector ();
11943 vec_safe_push (vec, range);
11945 member_begin = perform_koenig_lookup (id_begin, vec,
11946 tf_warning_or_error);
11947 *begin = finish_call_expr (member_begin, &vec, false, true,
11948 tf_warning_or_error);
11949 member_end = perform_koenig_lookup (id_end, vec,
11950 tf_warning_or_error);
11951 *end = finish_call_expr (member_end, &vec, false, true,
11952 tf_warning_or_error);
11954 release_tree_vector (vec);
11957 /* Last common checks. */
11958 if (*begin == error_mark_node || *end == error_mark_node)
11960 /* If one of the expressions is an error do no more checks. */
11961 *begin = *end = error_mark_node;
11962 return error_mark_node;
11964 else if (type_dependent_expression_p (*begin)
11965 || type_dependent_expression_p (*end))
11966 /* Can happen, when, eg, in a template context, Koenig lookup
11967 can't resolve begin/end (c++/58503). */
11968 return NULL_TREE;
11969 else
11971 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
11972 /* The unqualified type of the __begin and __end temporaries should
11973 be the same, as required by the multiple auto declaration. */
11974 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
11976 if (cxx_dialect >= cxx17
11977 && (build_x_binary_op (input_location, NE_EXPR,
11978 *begin, ERROR_MARK,
11979 *end, ERROR_MARK,
11980 NULL, tf_none)
11981 != error_mark_node))
11982 /* P0184R0 allows __begin and __end to have different types,
11983 but make sure they are comparable so we can give a better
11984 diagnostic. */;
11985 else
11986 error ("inconsistent begin/end types in range-based %<for%> "
11987 "statement: %qT and %qT",
11988 TREE_TYPE (*begin), TREE_TYPE (*end));
11990 return iter_type;
11995 /* Helper function for cp_parser_perform_range_for_lookup.
11996 Builds a tree for RANGE.IDENTIFIER(). */
11998 static tree
11999 cp_parser_range_for_member_function (tree range, tree identifier)
12001 tree member, res;
12002 vec<tree, va_gc> *vec;
12004 member = finish_class_member_access_expr (range, identifier,
12005 false, tf_warning_or_error);
12006 if (member == error_mark_node)
12007 return error_mark_node;
12009 vec = make_tree_vector ();
12010 res = finish_call_expr (member, &vec,
12011 /*disallow_virtual=*/false,
12012 /*koenig_p=*/false,
12013 tf_warning_or_error);
12014 release_tree_vector (vec);
12015 return res;
12018 /* Parse an iteration-statement.
12020 iteration-statement:
12021 while ( condition ) statement
12022 do statement while ( expression ) ;
12023 for ( init-statement condition [opt] ; expression [opt] )
12024 statement
12026 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12028 static tree
12029 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep)
12031 cp_token *token;
12032 enum rid keyword;
12033 tree statement;
12034 unsigned char in_statement;
12035 token_indent_info guard_tinfo;
12037 /* Peek at the next token. */
12038 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12039 if (!token)
12040 return error_mark_node;
12042 guard_tinfo = get_token_indent_info (token);
12044 /* Remember whether or not we are already within an iteration
12045 statement. */
12046 in_statement = parser->in_statement;
12048 /* See what kind of keyword it is. */
12049 keyword = token->keyword;
12050 switch (keyword)
12052 case RID_WHILE:
12054 tree condition;
12056 /* Begin the while-statement. */
12057 statement = begin_while_stmt ();
12058 /* Look for the `('. */
12059 matching_parens parens;
12060 parens.require_open (parser);
12061 /* Parse the condition. */
12062 condition = cp_parser_condition (parser);
12063 finish_while_stmt_cond (condition, statement, ivdep);
12064 /* Look for the `)'. */
12065 parens.require_close (parser);
12066 /* Parse the dependent statement. */
12067 parser->in_statement = IN_ITERATION_STMT;
12068 bool prev = note_iteration_stmt_body_start ();
12069 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12070 note_iteration_stmt_body_end (prev);
12071 parser->in_statement = in_statement;
12072 /* We're done with the while-statement. */
12073 finish_while_stmt (statement);
12075 break;
12077 case RID_DO:
12079 tree expression;
12081 /* Begin the do-statement. */
12082 statement = begin_do_stmt ();
12083 /* Parse the body of the do-statement. */
12084 parser->in_statement = IN_ITERATION_STMT;
12085 bool prev = note_iteration_stmt_body_start ();
12086 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12087 note_iteration_stmt_body_end (prev);
12088 parser->in_statement = in_statement;
12089 finish_do_body (statement);
12090 /* Look for the `while' keyword. */
12091 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12092 /* Look for the `('. */
12093 matching_parens parens;
12094 parens.require_open (parser);
12095 /* Parse the expression. */
12096 expression = cp_parser_expression (parser);
12097 /* We're done with the do-statement. */
12098 finish_do_stmt (expression, statement, ivdep);
12099 /* Look for the `)'. */
12100 parens.require_close (parser);
12101 /* Look for the `;'. */
12102 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12104 break;
12106 case RID_FOR:
12108 /* Look for the `('. */
12109 matching_parens parens;
12110 parens.require_open (parser);
12112 statement = cp_parser_for (parser, ivdep);
12114 /* Look for the `)'. */
12115 parens.require_close (parser);
12117 /* Parse the body of the for-statement. */
12118 parser->in_statement = IN_ITERATION_STMT;
12119 bool prev = note_iteration_stmt_body_start ();
12120 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12121 note_iteration_stmt_body_end (prev);
12122 parser->in_statement = in_statement;
12124 /* We're done with the for-statement. */
12125 finish_for_stmt (statement);
12127 break;
12129 default:
12130 cp_parser_error (parser, "expected iteration-statement");
12131 statement = error_mark_node;
12132 break;
12135 return statement;
12138 /* Parse a init-statement or the declarator of a range-based-for.
12139 Returns true if a range-based-for declaration is seen.
12141 init-statement:
12142 expression-statement
12143 simple-declaration */
12145 static bool
12146 cp_parser_init_statement (cp_parser* parser, tree *decl)
12148 /* If the next token is a `;', then we have an empty
12149 expression-statement. Grammatically, this is also a
12150 simple-declaration, but an invalid one, because it does not
12151 declare anything. Therefore, if we did not handle this case
12152 specially, we would issue an error message about an invalid
12153 declaration. */
12154 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12156 bool is_range_for = false;
12157 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12159 /* A colon is used in range-based for. */
12160 parser->colon_corrects_to_scope_p = false;
12162 /* We're going to speculatively look for a declaration, falling back
12163 to an expression, if necessary. */
12164 cp_parser_parse_tentatively (parser);
12165 /* Parse the declaration. */
12166 cp_parser_simple_declaration (parser,
12167 /*function_definition_allowed_p=*/false,
12168 decl);
12169 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12170 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12172 /* It is a range-for, consume the ':' */
12173 cp_lexer_consume_token (parser->lexer);
12174 is_range_for = true;
12175 if (cxx_dialect < cxx11)
12177 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12178 "range-based %<for%> loops only available with "
12179 "-std=c++11 or -std=gnu++11");
12180 *decl = error_mark_node;
12183 else
12184 /* The ';' is not consumed yet because we told
12185 cp_parser_simple_declaration not to. */
12186 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12188 if (cp_parser_parse_definitely (parser))
12189 return is_range_for;
12190 /* If the tentative parse failed, then we shall need to look for an
12191 expression-statement. */
12193 /* If we are here, it is an expression-statement. */
12194 cp_parser_expression_statement (parser, NULL_TREE);
12195 return false;
12198 /* Parse a jump-statement.
12200 jump-statement:
12201 break ;
12202 continue ;
12203 return expression [opt] ;
12204 return braced-init-list ;
12205 goto identifier ;
12207 GNU extension:
12209 jump-statement:
12210 goto * expression ;
12212 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12214 static tree
12215 cp_parser_jump_statement (cp_parser* parser)
12217 tree statement = error_mark_node;
12218 cp_token *token;
12219 enum rid keyword;
12220 unsigned char in_statement;
12222 /* Peek at the next token. */
12223 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12224 if (!token)
12225 return error_mark_node;
12227 /* See what kind of keyword it is. */
12228 keyword = token->keyword;
12229 switch (keyword)
12231 case RID_BREAK:
12232 in_statement = parser->in_statement & ~IN_IF_STMT;
12233 switch (in_statement)
12235 case 0:
12236 error_at (token->location, "break statement not within loop or switch");
12237 break;
12238 default:
12239 gcc_assert ((in_statement & IN_SWITCH_STMT)
12240 || in_statement == IN_ITERATION_STMT);
12241 statement = finish_break_stmt ();
12242 if (in_statement == IN_ITERATION_STMT)
12243 break_maybe_infinite_loop ();
12244 break;
12245 case IN_OMP_BLOCK:
12246 error_at (token->location, "invalid exit from OpenMP structured block");
12247 break;
12248 case IN_OMP_FOR:
12249 error_at (token->location, "break statement used with OpenMP for loop");
12250 break;
12252 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12253 break;
12255 case RID_CONTINUE:
12256 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12258 case 0:
12259 error_at (token->location, "continue statement not within a loop");
12260 break;
12261 /* Fall through. */
12262 case IN_ITERATION_STMT:
12263 case IN_OMP_FOR:
12264 statement = finish_continue_stmt ();
12265 break;
12266 case IN_OMP_BLOCK:
12267 error_at (token->location, "invalid exit from OpenMP structured block");
12268 break;
12269 default:
12270 gcc_unreachable ();
12272 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12273 break;
12275 case RID_RETURN:
12277 tree expr;
12278 bool expr_non_constant_p;
12280 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12282 cp_lexer_set_source_position (parser->lexer);
12283 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12284 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12286 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12287 expr = cp_parser_expression (parser);
12288 else
12289 /* If the next token is a `;', then there is no
12290 expression. */
12291 expr = NULL_TREE;
12292 /* Build the return-statement. */
12293 if (current_function_auto_return_pattern && in_discarded_stmt)
12294 /* Don't deduce from a discarded return statement. */;
12295 else
12296 statement = finish_return_stmt (expr);
12297 /* Look for the final `;'. */
12298 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12300 break;
12302 case RID_GOTO:
12303 if (parser->in_function_body
12304 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12306 error ("%<goto%> in %<constexpr%> function");
12307 cp_function_chain->invalid_constexpr = true;
12310 /* Create the goto-statement. */
12311 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12313 /* Issue a warning about this use of a GNU extension. */
12314 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12315 /* Consume the '*' token. */
12316 cp_lexer_consume_token (parser->lexer);
12317 /* Parse the dependent expression. */
12318 finish_goto_stmt (cp_parser_expression (parser));
12320 else
12321 finish_goto_stmt (cp_parser_identifier (parser));
12322 /* Look for the final `;'. */
12323 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12324 break;
12326 default:
12327 cp_parser_error (parser, "expected jump-statement");
12328 break;
12331 return statement;
12334 /* Parse a declaration-statement.
12336 declaration-statement:
12337 block-declaration */
12339 static void
12340 cp_parser_declaration_statement (cp_parser* parser)
12342 void *p;
12344 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12345 p = obstack_alloc (&declarator_obstack, 0);
12347 /* Parse the block-declaration. */
12348 cp_parser_block_declaration (parser, /*statement_p=*/true);
12350 /* Free any declarators allocated. */
12351 obstack_free (&declarator_obstack, p);
12354 /* Some dependent statements (like `if (cond) statement'), are
12355 implicitly in their own scope. In other words, if the statement is
12356 a single statement (as opposed to a compound-statement), it is
12357 none-the-less treated as if it were enclosed in braces. Any
12358 declarations appearing in the dependent statement are out of scope
12359 after control passes that point. This function parses a statement,
12360 but ensures that is in its own scope, even if it is not a
12361 compound-statement.
12363 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12364 is a (possibly labeled) if statement which is not enclosed in
12365 braces and has an else clause. This is used to implement
12366 -Wparentheses.
12368 CHAIN is a vector of if-else-if conditions. This is used to implement
12369 -Wduplicated-cond.
12371 Returns the new statement. */
12373 static tree
12374 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12375 const token_indent_info &guard_tinfo,
12376 vec<tree> *chain)
12378 tree statement;
12379 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12380 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12381 token_indent_info body_tinfo
12382 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12384 if (if_p != NULL)
12385 *if_p = false;
12387 /* Mark if () ; with a special NOP_EXPR. */
12388 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12390 cp_lexer_consume_token (parser->lexer);
12391 statement = add_stmt (build_empty_stmt (body_loc));
12393 if (guard_tinfo.keyword == RID_IF
12394 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12395 warning_at (body_loc, OPT_Wempty_body,
12396 "suggest braces around empty body in an %<if%> statement");
12397 else if (guard_tinfo.keyword == RID_ELSE)
12398 warning_at (body_loc, OPT_Wempty_body,
12399 "suggest braces around empty body in an %<else%> statement");
12401 /* if a compound is opened, we simply parse the statement directly. */
12402 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12403 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12404 /* If the token is not a `{', then we must take special action. */
12405 else
12407 /* Create a compound-statement. */
12408 statement = begin_compound_stmt (0);
12409 /* Parse the dependent-statement. */
12410 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12411 &body_loc_after_labels);
12412 /* Finish the dummy compound-statement. */
12413 finish_compound_stmt (statement);
12416 token_indent_info next_tinfo
12417 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12418 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12420 if (body_loc_after_labels != UNKNOWN_LOCATION
12421 && next_tinfo.type != CPP_SEMICOLON)
12422 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12423 guard_tinfo.location, guard_tinfo.keyword);
12425 /* Return the statement. */
12426 return statement;
12429 /* For some dependent statements (like `while (cond) statement'), we
12430 have already created a scope. Therefore, even if the dependent
12431 statement is a compound-statement, we do not want to create another
12432 scope. */
12434 static void
12435 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12436 const token_indent_info &guard_tinfo)
12438 /* If the token is a `{', then we must take special action. */
12439 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12441 token_indent_info body_tinfo
12442 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12443 location_t loc_after_labels = UNKNOWN_LOCATION;
12445 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12446 &loc_after_labels);
12447 token_indent_info next_tinfo
12448 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12449 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12451 if (loc_after_labels != UNKNOWN_LOCATION
12452 && next_tinfo.type != CPP_SEMICOLON)
12453 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12454 guard_tinfo.location,
12455 guard_tinfo.keyword);
12457 else
12459 /* Avoid calling cp_parser_compound_statement, so that we
12460 don't create a new scope. Do everything else by hand. */
12461 matching_braces braces;
12462 braces.require_open (parser);
12463 /* If the next keyword is `__label__' we have a label declaration. */
12464 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12465 cp_parser_label_declaration (parser);
12466 /* Parse an (optional) statement-seq. */
12467 cp_parser_statement_seq_opt (parser, NULL_TREE);
12468 braces.require_close (parser);
12472 /* Declarations [gram.dcl.dcl] */
12474 /* Parse an optional declaration-sequence.
12476 declaration-seq:
12477 declaration
12478 declaration-seq declaration */
12480 static void
12481 cp_parser_declaration_seq_opt (cp_parser* parser)
12483 while (true)
12485 cp_token *token;
12487 token = cp_lexer_peek_token (parser->lexer);
12489 if (token->type == CPP_CLOSE_BRACE
12490 || token->type == CPP_EOF
12491 || token->type == CPP_PRAGMA_EOL)
12492 break;
12494 if (token->type == CPP_SEMICOLON)
12496 /* A declaration consisting of a single semicolon is
12497 invalid. Allow it unless we're being pedantic. */
12498 cp_lexer_consume_token (parser->lexer);
12499 if (!in_system_header_at (input_location))
12500 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12501 continue;
12504 /* If we're entering or exiting a region that's implicitly
12505 extern "C", modify the lang context appropriately. */
12506 if (!parser->implicit_extern_c && token->implicit_extern_c)
12508 push_lang_context (lang_name_c);
12509 parser->implicit_extern_c = true;
12511 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12513 pop_lang_context ();
12514 parser->implicit_extern_c = false;
12517 if (token->type == CPP_PRAGMA)
12519 /* A top-level declaration can consist solely of a #pragma.
12520 A nested declaration cannot, so this is done here and not
12521 in cp_parser_declaration. (A #pragma at block scope is
12522 handled in cp_parser_statement.) */
12523 cp_parser_pragma (parser, pragma_external, NULL);
12524 continue;
12527 /* Parse the declaration itself. */
12528 cp_parser_declaration (parser);
12532 /* Parse a declaration.
12534 declaration:
12535 block-declaration
12536 function-definition
12537 template-declaration
12538 explicit-instantiation
12539 explicit-specialization
12540 linkage-specification
12541 namespace-definition
12543 C++17:
12544 deduction-guide
12546 GNU extension:
12548 declaration:
12549 __extension__ declaration */
12551 static void
12552 cp_parser_declaration (cp_parser* parser)
12554 cp_token token1;
12555 cp_token token2;
12556 int saved_pedantic;
12557 void *p;
12558 tree attributes = NULL_TREE;
12560 /* Check for the `__extension__' keyword. */
12561 if (cp_parser_extension_opt (parser, &saved_pedantic))
12563 /* Parse the qualified declaration. */
12564 cp_parser_declaration (parser);
12565 /* Restore the PEDANTIC flag. */
12566 pedantic = saved_pedantic;
12568 return;
12571 /* Try to figure out what kind of declaration is present. */
12572 token1 = *cp_lexer_peek_token (parser->lexer);
12574 if (token1.type != CPP_EOF)
12575 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12576 else
12578 token2.type = CPP_EOF;
12579 token2.keyword = RID_MAX;
12582 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12583 p = obstack_alloc (&declarator_obstack, 0);
12585 /* If the next token is `extern' and the following token is a string
12586 literal, then we have a linkage specification. */
12587 if (token1.keyword == RID_EXTERN
12588 && cp_parser_is_pure_string_literal (&token2))
12589 cp_parser_linkage_specification (parser);
12590 /* If the next token is `template', then we have either a template
12591 declaration, an explicit instantiation, or an explicit
12592 specialization. */
12593 else if (token1.keyword == RID_TEMPLATE)
12595 /* `template <>' indicates a template specialization. */
12596 if (token2.type == CPP_LESS
12597 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12598 cp_parser_explicit_specialization (parser);
12599 /* `template <' indicates a template declaration. */
12600 else if (token2.type == CPP_LESS)
12601 cp_parser_template_declaration (parser, /*member_p=*/false);
12602 /* Anything else must be an explicit instantiation. */
12603 else
12604 cp_parser_explicit_instantiation (parser);
12606 /* If the next token is `export', then we have a template
12607 declaration. */
12608 else if (token1.keyword == RID_EXPORT)
12609 cp_parser_template_declaration (parser, /*member_p=*/false);
12610 /* If the next token is `extern', 'static' or 'inline' and the one
12611 after that is `template', we have a GNU extended explicit
12612 instantiation directive. */
12613 else if (cp_parser_allow_gnu_extensions_p (parser)
12614 && (token1.keyword == RID_EXTERN
12615 || token1.keyword == RID_STATIC
12616 || token1.keyword == RID_INLINE)
12617 && token2.keyword == RID_TEMPLATE)
12618 cp_parser_explicit_instantiation (parser);
12619 /* If the next token is `namespace', check for a named or unnamed
12620 namespace definition. */
12621 else if (token1.keyword == RID_NAMESPACE
12622 && (/* A named namespace definition. */
12623 (token2.type == CPP_NAME
12624 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12625 != CPP_EQ))
12626 || (token2.type == CPP_OPEN_SQUARE
12627 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12628 == CPP_OPEN_SQUARE)
12629 /* An unnamed namespace definition. */
12630 || token2.type == CPP_OPEN_BRACE
12631 || token2.keyword == RID_ATTRIBUTE))
12632 cp_parser_namespace_definition (parser);
12633 /* An inline (associated) namespace definition. */
12634 else if (token1.keyword == RID_INLINE
12635 && token2.keyword == RID_NAMESPACE)
12636 cp_parser_namespace_definition (parser);
12637 /* Objective-C++ declaration/definition. */
12638 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12639 cp_parser_objc_declaration (parser, NULL_TREE);
12640 else if (c_dialect_objc ()
12641 && token1.keyword == RID_ATTRIBUTE
12642 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12643 cp_parser_objc_declaration (parser, attributes);
12644 /* At this point we may have a template declared by a concept
12645 introduction. */
12646 else if (flag_concepts
12647 && cp_parser_template_declaration_after_export (parser,
12648 /*member_p=*/false))
12649 /* We did. */;
12650 else
12651 /* Try to parse a block-declaration, or a function-definition. */
12652 cp_parser_block_declaration (parser, /*statement_p=*/false);
12654 /* Free any declarators allocated. */
12655 obstack_free (&declarator_obstack, p);
12658 /* Parse a block-declaration.
12660 block-declaration:
12661 simple-declaration
12662 asm-definition
12663 namespace-alias-definition
12664 using-declaration
12665 using-directive
12667 GNU Extension:
12669 block-declaration:
12670 __extension__ block-declaration
12672 C++0x Extension:
12674 block-declaration:
12675 static_assert-declaration
12677 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12678 part of a declaration-statement. */
12680 static void
12681 cp_parser_block_declaration (cp_parser *parser,
12682 bool statement_p)
12684 cp_token *token1;
12685 int saved_pedantic;
12687 /* Check for the `__extension__' keyword. */
12688 if (cp_parser_extension_opt (parser, &saved_pedantic))
12690 /* Parse the qualified declaration. */
12691 cp_parser_block_declaration (parser, statement_p);
12692 /* Restore the PEDANTIC flag. */
12693 pedantic = saved_pedantic;
12695 return;
12698 /* Peek at the next token to figure out which kind of declaration is
12699 present. */
12700 token1 = cp_lexer_peek_token (parser->lexer);
12702 /* If the next keyword is `asm', we have an asm-definition. */
12703 if (token1->keyword == RID_ASM)
12705 if (statement_p)
12706 cp_parser_commit_to_tentative_parse (parser);
12707 cp_parser_asm_definition (parser);
12709 /* If the next keyword is `namespace', we have a
12710 namespace-alias-definition. */
12711 else if (token1->keyword == RID_NAMESPACE)
12712 cp_parser_namespace_alias_definition (parser);
12713 /* If the next keyword is `using', we have a
12714 using-declaration, a using-directive, or an alias-declaration. */
12715 else if (token1->keyword == RID_USING)
12717 cp_token *token2;
12719 if (statement_p)
12720 cp_parser_commit_to_tentative_parse (parser);
12721 /* If the token after `using' is `namespace', then we have a
12722 using-directive. */
12723 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12724 if (token2->keyword == RID_NAMESPACE)
12725 cp_parser_using_directive (parser);
12726 /* If the second token after 'using' is '=', then we have an
12727 alias-declaration. */
12728 else if (cxx_dialect >= cxx11
12729 && token2->type == CPP_NAME
12730 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12731 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12732 cp_parser_alias_declaration (parser);
12733 /* Otherwise, it's a using-declaration. */
12734 else
12735 cp_parser_using_declaration (parser,
12736 /*access_declaration_p=*/false);
12738 /* If the next keyword is `__label__' we have a misplaced label
12739 declaration. */
12740 else if (token1->keyword == RID_LABEL)
12742 cp_lexer_consume_token (parser->lexer);
12743 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12744 cp_parser_skip_to_end_of_statement (parser);
12745 /* If the next token is now a `;', consume it. */
12746 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12747 cp_lexer_consume_token (parser->lexer);
12749 /* If the next token is `static_assert' we have a static assertion. */
12750 else if (token1->keyword == RID_STATIC_ASSERT)
12751 cp_parser_static_assert (parser, /*member_p=*/false);
12752 /* Anything else must be a simple-declaration. */
12753 else
12754 cp_parser_simple_declaration (parser, !statement_p,
12755 /*maybe_range_for_decl*/NULL);
12758 /* Parse a simple-declaration.
12760 simple-declaration:
12761 decl-specifier-seq [opt] init-declarator-list [opt] ;
12762 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12763 brace-or-equal-initializer ;
12765 init-declarator-list:
12766 init-declarator
12767 init-declarator-list , init-declarator
12769 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12770 function-definition as a simple-declaration.
12772 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12773 parsed declaration if it is an uninitialized single declarator not followed
12774 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12775 if present, will not be consumed. */
12777 static void
12778 cp_parser_simple_declaration (cp_parser* parser,
12779 bool function_definition_allowed_p,
12780 tree *maybe_range_for_decl)
12782 cp_decl_specifier_seq decl_specifiers;
12783 int declares_class_or_enum;
12784 bool saw_declarator;
12785 location_t comma_loc = UNKNOWN_LOCATION;
12786 location_t init_loc = UNKNOWN_LOCATION;
12788 if (maybe_range_for_decl)
12789 *maybe_range_for_decl = NULL_TREE;
12791 /* Defer access checks until we know what is being declared; the
12792 checks for names appearing in the decl-specifier-seq should be
12793 done as if we were in the scope of the thing being declared. */
12794 push_deferring_access_checks (dk_deferred);
12796 /* Parse the decl-specifier-seq. We have to keep track of whether
12797 or not the decl-specifier-seq declares a named class or
12798 enumeration type, since that is the only case in which the
12799 init-declarator-list is allowed to be empty.
12801 [dcl.dcl]
12803 In a simple-declaration, the optional init-declarator-list can be
12804 omitted only when declaring a class or enumeration, that is when
12805 the decl-specifier-seq contains either a class-specifier, an
12806 elaborated-type-specifier, or an enum-specifier. */
12807 cp_parser_decl_specifier_seq (parser,
12808 CP_PARSER_FLAGS_OPTIONAL,
12809 &decl_specifiers,
12810 &declares_class_or_enum);
12811 /* We no longer need to defer access checks. */
12812 stop_deferring_access_checks ();
12814 /* In a block scope, a valid declaration must always have a
12815 decl-specifier-seq. By not trying to parse declarators, we can
12816 resolve the declaration/expression ambiguity more quickly. */
12817 if (!function_definition_allowed_p
12818 && !decl_specifiers.any_specifiers_p)
12820 cp_parser_error (parser, "expected declaration");
12821 goto done;
12824 /* If the next two tokens are both identifiers, the code is
12825 erroneous. The usual cause of this situation is code like:
12827 T t;
12829 where "T" should name a type -- but does not. */
12830 if (!decl_specifiers.any_type_specifiers_p
12831 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12833 /* If parsing tentatively, we should commit; we really are
12834 looking at a declaration. */
12835 cp_parser_commit_to_tentative_parse (parser);
12836 /* Give up. */
12837 goto done;
12840 /* If we have seen at least one decl-specifier, and the next token
12841 is not a parenthesis, then we must be looking at a declaration.
12842 (After "int (" we might be looking at a functional cast.) */
12843 if (decl_specifiers.any_specifiers_p
12844 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12845 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12846 && !cp_parser_error_occurred (parser))
12847 cp_parser_commit_to_tentative_parse (parser);
12849 /* Look for C++17 decomposition declaration. */
12850 for (size_t n = 1; ; n++)
12851 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
12852 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
12853 continue;
12854 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
12855 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
12856 && decl_specifiers.any_specifiers_p)
12858 tree decl
12859 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
12860 maybe_range_for_decl,
12861 &init_loc);
12863 /* The next token should be either a `,' or a `;'. */
12864 cp_token *token = cp_lexer_peek_token (parser->lexer);
12865 /* If it's a `;', we are done. */
12866 if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12867 goto finish;
12868 /* Anything else is an error. */
12869 else
12871 /* If we have already issued an error message we don't need
12872 to issue another one. */
12873 if ((decl != error_mark_node
12874 && DECL_INITIAL (decl) != error_mark_node)
12875 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12876 cp_parser_error (parser, "expected %<,%> or %<;%>");
12877 /* Skip tokens until we reach the end of the statement. */
12878 cp_parser_skip_to_end_of_statement (parser);
12879 /* If the next token is now a `;', consume it. */
12880 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12881 cp_lexer_consume_token (parser->lexer);
12882 goto done;
12885 else
12886 break;
12888 tree last_type;
12889 bool auto_specifier_p;
12890 /* NULL_TREE if both variable and function declaration are allowed,
12891 error_mark_node if function declaration are not allowed and
12892 a FUNCTION_DECL that should be diagnosed if it is followed by
12893 variable declarations. */
12894 tree auto_function_declaration;
12896 last_type = NULL_TREE;
12897 auto_specifier_p
12898 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
12899 auto_function_declaration = NULL_TREE;
12901 /* Keep going until we hit the `;' at the end of the simple
12902 declaration. */
12903 saw_declarator = false;
12904 while (cp_lexer_next_token_is_not (parser->lexer,
12905 CPP_SEMICOLON))
12907 cp_token *token;
12908 bool function_definition_p;
12909 tree decl;
12910 tree auto_result = NULL_TREE;
12912 if (saw_declarator)
12914 /* If we are processing next declarator, comma is expected */
12915 token = cp_lexer_peek_token (parser->lexer);
12916 gcc_assert (token->type == CPP_COMMA);
12917 cp_lexer_consume_token (parser->lexer);
12918 if (maybe_range_for_decl)
12920 *maybe_range_for_decl = error_mark_node;
12921 if (comma_loc == UNKNOWN_LOCATION)
12922 comma_loc = token->location;
12925 else
12926 saw_declarator = true;
12928 /* Parse the init-declarator. */
12929 decl = cp_parser_init_declarator (parser, &decl_specifiers,
12930 /*checks=*/NULL,
12931 function_definition_allowed_p,
12932 /*member_p=*/false,
12933 declares_class_or_enum,
12934 &function_definition_p,
12935 maybe_range_for_decl,
12936 &init_loc,
12937 &auto_result);
12938 /* If an error occurred while parsing tentatively, exit quickly.
12939 (That usually happens when in the body of a function; each
12940 statement is treated as a declaration-statement until proven
12941 otherwise.) */
12942 if (cp_parser_error_occurred (parser))
12943 goto done;
12945 if (auto_specifier_p && cxx_dialect >= cxx14)
12947 /* If the init-declarator-list contains more than one
12948 init-declarator, they shall all form declarations of
12949 variables. */
12950 if (auto_function_declaration == NULL_TREE)
12951 auto_function_declaration
12952 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
12953 else if (TREE_CODE (decl) == FUNCTION_DECL
12954 || auto_function_declaration != error_mark_node)
12956 error_at (decl_specifiers.locations[ds_type_spec],
12957 "non-variable %qD in declaration with more than one "
12958 "declarator with placeholder type",
12959 TREE_CODE (decl) == FUNCTION_DECL
12960 ? decl : auto_function_declaration);
12961 auto_function_declaration = error_mark_node;
12965 if (auto_result
12966 && (!processing_template_decl || !type_uses_auto (auto_result)))
12968 if (last_type
12969 && last_type != error_mark_node
12970 && !same_type_p (auto_result, last_type))
12972 /* If the list of declarators contains more than one declarator,
12973 the type of each declared variable is determined as described
12974 above. If the type deduced for the template parameter U is not
12975 the same in each deduction, the program is ill-formed. */
12976 error_at (decl_specifiers.locations[ds_type_spec],
12977 "inconsistent deduction for %qT: %qT and then %qT",
12978 decl_specifiers.type, last_type, auto_result);
12979 last_type = error_mark_node;
12981 else
12982 last_type = auto_result;
12985 /* Handle function definitions specially. */
12986 if (function_definition_p)
12988 /* If the next token is a `,', then we are probably
12989 processing something like:
12991 void f() {}, *p;
12993 which is erroneous. */
12994 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12996 cp_token *token = cp_lexer_peek_token (parser->lexer);
12997 error_at (token->location,
12998 "mixing"
12999 " declarations and function-definitions is forbidden");
13001 /* Otherwise, we're done with the list of declarators. */
13002 else
13004 pop_deferring_access_checks ();
13005 return;
13008 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13009 *maybe_range_for_decl = decl;
13010 /* The next token should be either a `,' or a `;'. */
13011 token = cp_lexer_peek_token (parser->lexer);
13012 /* If it's a `,', there are more declarators to come. */
13013 if (token->type == CPP_COMMA)
13014 /* will be consumed next time around */;
13015 /* If it's a `;', we are done. */
13016 else if (token->type == CPP_SEMICOLON)
13017 break;
13018 else if (maybe_range_for_decl)
13020 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13021 permerror (decl_specifiers.locations[ds_type_spec],
13022 "types may not be defined in a for-range-declaration");
13023 break;
13025 /* Anything else is an error. */
13026 else
13028 /* If we have already issued an error message we don't need
13029 to issue another one. */
13030 if ((decl != error_mark_node
13031 && DECL_INITIAL (decl) != error_mark_node)
13032 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13033 cp_parser_error (parser, "expected %<,%> or %<;%>");
13034 /* Skip tokens until we reach the end of the statement. */
13035 cp_parser_skip_to_end_of_statement (parser);
13036 /* If the next token is now a `;', consume it. */
13037 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13038 cp_lexer_consume_token (parser->lexer);
13039 goto done;
13041 /* After the first time around, a function-definition is not
13042 allowed -- even if it was OK at first. For example:
13044 int i, f() {}
13046 is not valid. */
13047 function_definition_allowed_p = false;
13050 /* Issue an error message if no declarators are present, and the
13051 decl-specifier-seq does not itself declare a class or
13052 enumeration: [dcl.dcl]/3. */
13053 if (!saw_declarator)
13055 if (cp_parser_declares_only_class_p (parser))
13057 if (!declares_class_or_enum
13058 && decl_specifiers.type
13059 && OVERLOAD_TYPE_P (decl_specifiers.type))
13060 /* Ensure an error is issued anyway when finish_decltype_type,
13061 called via cp_parser_decl_specifier_seq, returns a class or
13062 an enumeration (c++/51786). */
13063 decl_specifiers.type = NULL_TREE;
13064 shadow_tag (&decl_specifiers);
13066 /* Perform any deferred access checks. */
13067 perform_deferred_access_checks (tf_warning_or_error);
13070 /* Consume the `;'. */
13071 finish:
13072 if (!maybe_range_for_decl)
13073 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13074 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13076 if (init_loc != UNKNOWN_LOCATION)
13077 error_at (init_loc, "initializer in range-based %<for%> loop");
13078 if (comma_loc != UNKNOWN_LOCATION)
13079 error_at (comma_loc,
13080 "multiple declarations in range-based %<for%> loop");
13083 done:
13084 pop_deferring_access_checks ();
13087 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13088 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13089 initializer ; */
13091 static tree
13092 cp_parser_decomposition_declaration (cp_parser *parser,
13093 cp_decl_specifier_seq *decl_specifiers,
13094 tree *maybe_range_for_decl,
13095 location_t *init_loc)
13097 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13098 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13099 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13101 /* Parse the identifier-list. */
13102 auto_vec<cp_expr, 10> v;
13103 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13104 while (true)
13106 cp_expr e = cp_parser_identifier (parser);
13107 if (e.get_value () == error_mark_node)
13108 break;
13109 v.safe_push (e);
13110 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13111 break;
13112 cp_lexer_consume_token (parser->lexer);
13115 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13116 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13118 end_loc = UNKNOWN_LOCATION;
13119 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13120 false);
13121 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13122 cp_lexer_consume_token (parser->lexer);
13123 else
13125 cp_parser_skip_to_end_of_statement (parser);
13126 return error_mark_node;
13130 if (cxx_dialect < cxx17)
13131 pedwarn (loc, 0, "structured bindings only available with "
13132 "-std=c++17 or -std=gnu++17");
13134 tree pushed_scope;
13135 cp_declarator *declarator = make_declarator (cdk_decomp);
13136 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13137 declarator->id_loc = loc;
13138 if (ref_qual != REF_QUAL_NONE)
13139 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13140 ref_qual == REF_QUAL_RVALUE,
13141 NULL_TREE);
13142 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13143 NULL_TREE, decl_specifiers->attributes,
13144 &pushed_scope);
13145 tree orig_decl = decl;
13147 unsigned int i;
13148 cp_expr e;
13149 cp_decl_specifier_seq decl_specs;
13150 clear_decl_specs (&decl_specs);
13151 decl_specs.type = make_auto ();
13152 tree prev = decl;
13153 FOR_EACH_VEC_ELT (v, i, e)
13155 if (i == 0)
13156 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13157 else
13158 declarator->u.id.unqualified_name = e.get_value ();
13159 declarator->id_loc = e.get_location ();
13160 tree elt_pushed_scope;
13161 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13162 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13163 if (decl2 == error_mark_node)
13164 decl = error_mark_node;
13165 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13167 /* Ensure we've diagnosed redeclaration if we aren't creating
13168 a new VAR_DECL. */
13169 gcc_assert (errorcount);
13170 decl = error_mark_node;
13172 else
13173 prev = decl2;
13174 if (elt_pushed_scope)
13175 pop_scope (elt_pushed_scope);
13178 if (v.is_empty ())
13180 error_at (loc, "empty structured binding declaration");
13181 decl = error_mark_node;
13184 if (maybe_range_for_decl == NULL
13185 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13187 bool non_constant_p = false, is_direct_init = false;
13188 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13189 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13190 &non_constant_p);
13191 if (initializer == NULL_TREE
13192 || (TREE_CODE (initializer) == TREE_LIST
13193 && TREE_CHAIN (initializer))
13194 || (is_direct_init
13195 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13196 && CONSTRUCTOR_NELTS (initializer) != 1))
13198 error_at (loc, "invalid initializer for structured binding "
13199 "declaration");
13200 initializer = error_mark_node;
13203 if (decl != error_mark_node)
13205 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13206 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13207 cp_finish_decomp (decl, prev, v.length ());
13210 else if (decl != error_mark_node)
13212 *maybe_range_for_decl = prev;
13213 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13214 the underlying DECL. */
13215 cp_finish_decomp (decl, prev, v.length ());
13218 if (pushed_scope)
13219 pop_scope (pushed_scope);
13221 if (decl == error_mark_node && DECL_P (orig_decl))
13223 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13224 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13227 return decl;
13230 /* Parse a decl-specifier-seq.
13232 decl-specifier-seq:
13233 decl-specifier-seq [opt] decl-specifier
13234 decl-specifier attribute-specifier-seq [opt] (C++11)
13236 decl-specifier:
13237 storage-class-specifier
13238 type-specifier
13239 function-specifier
13240 friend
13241 typedef
13243 GNU Extension:
13245 decl-specifier:
13246 attributes
13248 Concepts Extension:
13250 decl-specifier:
13251 concept
13253 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13255 The parser flags FLAGS is used to control type-specifier parsing.
13257 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13258 flags:
13260 1: one of the decl-specifiers is an elaborated-type-specifier
13261 (i.e., a type declaration)
13262 2: one of the decl-specifiers is an enum-specifier or a
13263 class-specifier (i.e., a type definition)
13267 static void
13268 cp_parser_decl_specifier_seq (cp_parser* parser,
13269 cp_parser_flags flags,
13270 cp_decl_specifier_seq *decl_specs,
13271 int* declares_class_or_enum)
13273 bool constructor_possible_p = !parser->in_declarator_p;
13274 bool found_decl_spec = false;
13275 cp_token *start_token = NULL;
13276 cp_decl_spec ds;
13278 /* Clear DECL_SPECS. */
13279 clear_decl_specs (decl_specs);
13281 /* Assume no class or enumeration type is declared. */
13282 *declares_class_or_enum = 0;
13284 /* Keep reading specifiers until there are no more to read. */
13285 while (true)
13287 bool constructor_p;
13288 cp_token *token;
13289 ds = ds_last;
13291 /* Peek at the next token. */
13292 token = cp_lexer_peek_token (parser->lexer);
13294 /* Save the first token of the decl spec list for error
13295 reporting. */
13296 if (!start_token)
13297 start_token = token;
13298 /* Handle attributes. */
13299 if (cp_next_tokens_can_be_attribute_p (parser))
13301 /* Parse the attributes. */
13302 tree attrs = cp_parser_attributes_opt (parser);
13304 /* In a sequence of declaration specifiers, c++11 attributes
13305 appertain to the type that precede them. In that case
13306 [dcl.spec]/1 says:
13308 The attribute-specifier-seq affects the type only for
13309 the declaration it appears in, not other declarations
13310 involving the same type.
13312 But for now let's force the user to position the
13313 attribute either at the beginning of the declaration or
13314 after the declarator-id, which would clearly mean that it
13315 applies to the declarator. */
13316 if (cxx11_attribute_p (attrs))
13318 if (!found_decl_spec)
13319 /* The c++11 attribute is at the beginning of the
13320 declaration. It appertains to the entity being
13321 declared. */;
13322 else
13324 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13326 /* This is an attribute following a
13327 class-specifier. */
13328 if (decl_specs->type_definition_p)
13329 warn_misplaced_attr_for_class_type (token->location,
13330 decl_specs->type);
13331 attrs = NULL_TREE;
13333 else
13335 decl_specs->std_attributes
13336 = chainon (decl_specs->std_attributes,
13337 attrs);
13338 if (decl_specs->locations[ds_std_attribute] == 0)
13339 decl_specs->locations[ds_std_attribute] = token->location;
13341 continue;
13345 decl_specs->attributes
13346 = chainon (decl_specs->attributes,
13347 attrs);
13348 if (decl_specs->locations[ds_attribute] == 0)
13349 decl_specs->locations[ds_attribute] = token->location;
13350 continue;
13352 /* Assume we will find a decl-specifier keyword. */
13353 found_decl_spec = true;
13354 /* If the next token is an appropriate keyword, we can simply
13355 add it to the list. */
13356 switch (token->keyword)
13358 /* decl-specifier:
13359 friend
13360 constexpr */
13361 case RID_FRIEND:
13362 if (!at_class_scope_p ())
13364 gcc_rich_location richloc (token->location);
13365 richloc.add_fixit_remove ();
13366 error_at (&richloc, "%<friend%> used outside of class");
13367 cp_lexer_purge_token (parser->lexer);
13369 else
13371 ds = ds_friend;
13372 /* Consume the token. */
13373 cp_lexer_consume_token (parser->lexer);
13375 break;
13377 case RID_CONSTEXPR:
13378 ds = ds_constexpr;
13379 cp_lexer_consume_token (parser->lexer);
13380 break;
13382 case RID_CONCEPT:
13383 ds = ds_concept;
13384 cp_lexer_consume_token (parser->lexer);
13385 break;
13387 /* function-specifier:
13388 inline
13389 virtual
13390 explicit */
13391 case RID_INLINE:
13392 case RID_VIRTUAL:
13393 case RID_EXPLICIT:
13394 cp_parser_function_specifier_opt (parser, decl_specs);
13395 break;
13397 /* decl-specifier:
13398 typedef */
13399 case RID_TYPEDEF:
13400 ds = ds_typedef;
13401 /* Consume the token. */
13402 cp_lexer_consume_token (parser->lexer);
13403 /* A constructor declarator cannot appear in a typedef. */
13404 constructor_possible_p = false;
13405 /* The "typedef" keyword can only occur in a declaration; we
13406 may as well commit at this point. */
13407 cp_parser_commit_to_tentative_parse (parser);
13409 if (decl_specs->storage_class != sc_none)
13410 decl_specs->conflicting_specifiers_p = true;
13411 break;
13413 /* storage-class-specifier:
13414 auto
13415 register
13416 static
13417 extern
13418 mutable
13420 GNU Extension:
13421 thread */
13422 case RID_AUTO:
13423 if (cxx_dialect == cxx98)
13425 /* Consume the token. */
13426 cp_lexer_consume_token (parser->lexer);
13428 /* Complain about `auto' as a storage specifier, if
13429 we're complaining about C++0x compatibility. */
13430 gcc_rich_location richloc (token->location);
13431 richloc.add_fixit_remove ();
13432 warning_at (&richloc, OPT_Wc__11_compat,
13433 "%<auto%> changes meaning in C++11; "
13434 "please remove it");
13436 /* Set the storage class anyway. */
13437 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13438 token);
13440 else
13441 /* C++0x auto type-specifier. */
13442 found_decl_spec = false;
13443 break;
13445 case RID_REGISTER:
13446 case RID_STATIC:
13447 case RID_EXTERN:
13448 case RID_MUTABLE:
13449 /* Consume the token. */
13450 cp_lexer_consume_token (parser->lexer);
13451 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13452 token);
13453 break;
13454 case RID_THREAD:
13455 /* Consume the token. */
13456 ds = ds_thread;
13457 cp_lexer_consume_token (parser->lexer);
13458 break;
13460 default:
13461 /* We did not yet find a decl-specifier yet. */
13462 found_decl_spec = false;
13463 break;
13466 if (found_decl_spec
13467 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13468 && token->keyword != RID_CONSTEXPR)
13469 error ("decl-specifier invalid in condition");
13471 if (found_decl_spec
13472 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13473 && token->keyword != RID_MUTABLE
13474 && token->keyword != RID_CONSTEXPR)
13475 error_at (token->location, "%qD invalid in lambda",
13476 ridpointers[token->keyword]);
13478 if (ds != ds_last)
13479 set_and_check_decl_spec_loc (decl_specs, ds, token);
13481 /* Constructors are a special case. The `S' in `S()' is not a
13482 decl-specifier; it is the beginning of the declarator. */
13483 constructor_p
13484 = (!found_decl_spec
13485 && constructor_possible_p
13486 && (cp_parser_constructor_declarator_p
13487 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13489 /* If we don't have a DECL_SPEC yet, then we must be looking at
13490 a type-specifier. */
13491 if (!found_decl_spec && !constructor_p)
13493 int decl_spec_declares_class_or_enum;
13494 bool is_cv_qualifier;
13495 tree type_spec;
13497 type_spec
13498 = cp_parser_type_specifier (parser, flags,
13499 decl_specs,
13500 /*is_declaration=*/true,
13501 &decl_spec_declares_class_or_enum,
13502 &is_cv_qualifier);
13503 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13505 /* If this type-specifier referenced a user-defined type
13506 (a typedef, class-name, etc.), then we can't allow any
13507 more such type-specifiers henceforth.
13509 [dcl.spec]
13511 The longest sequence of decl-specifiers that could
13512 possibly be a type name is taken as the
13513 decl-specifier-seq of a declaration. The sequence shall
13514 be self-consistent as described below.
13516 [dcl.type]
13518 As a general rule, at most one type-specifier is allowed
13519 in the complete decl-specifier-seq of a declaration. The
13520 only exceptions are the following:
13522 -- const or volatile can be combined with any other
13523 type-specifier.
13525 -- signed or unsigned can be combined with char, long,
13526 short, or int.
13528 -- ..
13530 Example:
13532 typedef char* Pc;
13533 void g (const int Pc);
13535 Here, Pc is *not* part of the decl-specifier seq; it's
13536 the declarator. Therefore, once we see a type-specifier
13537 (other than a cv-qualifier), we forbid any additional
13538 user-defined types. We *do* still allow things like `int
13539 int' to be considered a decl-specifier-seq, and issue the
13540 error message later. */
13541 if (type_spec && !is_cv_qualifier)
13542 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13543 /* A constructor declarator cannot follow a type-specifier. */
13544 if (type_spec)
13546 constructor_possible_p = false;
13547 found_decl_spec = true;
13548 if (!is_cv_qualifier)
13549 decl_specs->any_type_specifiers_p = true;
13553 /* If we still do not have a DECL_SPEC, then there are no more
13554 decl-specifiers. */
13555 if (!found_decl_spec)
13556 break;
13558 decl_specs->any_specifiers_p = true;
13559 /* After we see one decl-specifier, further decl-specifiers are
13560 always optional. */
13561 flags |= CP_PARSER_FLAGS_OPTIONAL;
13564 /* Don't allow a friend specifier with a class definition. */
13565 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13566 && (*declares_class_or_enum & 2))
13567 error_at (decl_specs->locations[ds_friend],
13568 "class definition may not be declared a friend");
13571 /* Parse an (optional) storage-class-specifier.
13573 storage-class-specifier:
13574 auto
13575 register
13576 static
13577 extern
13578 mutable
13580 GNU Extension:
13582 storage-class-specifier:
13583 thread
13585 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13587 static tree
13588 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13590 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13592 case RID_AUTO:
13593 if (cxx_dialect != cxx98)
13594 return NULL_TREE;
13595 /* Fall through for C++98. */
13596 gcc_fallthrough ();
13598 case RID_REGISTER:
13599 case RID_STATIC:
13600 case RID_EXTERN:
13601 case RID_MUTABLE:
13602 case RID_THREAD:
13603 /* Consume the token. */
13604 return cp_lexer_consume_token (parser->lexer)->u.value;
13606 default:
13607 return NULL_TREE;
13611 /* Parse an (optional) function-specifier.
13613 function-specifier:
13614 inline
13615 virtual
13616 explicit
13618 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13619 Updates DECL_SPECS, if it is non-NULL. */
13621 static tree
13622 cp_parser_function_specifier_opt (cp_parser* parser,
13623 cp_decl_specifier_seq *decl_specs)
13625 cp_token *token = cp_lexer_peek_token (parser->lexer);
13626 switch (token->keyword)
13628 case RID_INLINE:
13629 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13630 break;
13632 case RID_VIRTUAL:
13633 /* 14.5.2.3 [temp.mem]
13635 A member function template shall not be virtual. */
13636 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13637 && current_class_type)
13638 error_at (token->location, "templates may not be %<virtual%>");
13639 else
13640 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13641 break;
13643 case RID_EXPLICIT:
13644 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13645 break;
13647 default:
13648 return NULL_TREE;
13651 /* Consume the token. */
13652 return cp_lexer_consume_token (parser->lexer)->u.value;
13655 /* Parse a linkage-specification.
13657 linkage-specification:
13658 extern string-literal { declaration-seq [opt] }
13659 extern string-literal declaration */
13661 static void
13662 cp_parser_linkage_specification (cp_parser* parser)
13664 tree linkage;
13666 /* Look for the `extern' keyword. */
13667 cp_token *extern_token
13668 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13670 /* Look for the string-literal. */
13671 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
13672 linkage = cp_parser_string_literal (parser, false, false);
13674 /* Transform the literal into an identifier. If the literal is a
13675 wide-character string, or contains embedded NULs, then we can't
13676 handle it as the user wants. */
13677 if (strlen (TREE_STRING_POINTER (linkage))
13678 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13680 cp_parser_error (parser, "invalid linkage-specification");
13681 /* Assume C++ linkage. */
13682 linkage = lang_name_cplusplus;
13684 else
13685 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13687 /* We're now using the new linkage. */
13688 push_lang_context (linkage);
13690 /* Preserve the location of the the innermost linkage specification,
13691 tracking the locations of nested specifications via a local. */
13692 location_t saved_location
13693 = parser->innermost_linkage_specification_location;
13694 /* Construct a location ranging from the start of the "extern" to
13695 the end of the string-literal, with the caret at the start, e.g.:
13696 extern "C" {
13697 ^~~~~~~~~~
13699 parser->innermost_linkage_specification_location
13700 = make_location (extern_token->location,
13701 extern_token->location,
13702 get_finish (string_token->location));
13704 /* If the next token is a `{', then we're using the first
13705 production. */
13706 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13708 cp_ensure_no_omp_declare_simd (parser);
13709 cp_ensure_no_oacc_routine (parser);
13711 /* Consume the `{' token. */
13712 matching_braces braces;
13713 braces.consume_open (parser)->location;
13714 /* Parse the declarations. */
13715 cp_parser_declaration_seq_opt (parser);
13716 /* Look for the closing `}'. */
13717 braces.require_close (parser);
13719 /* Otherwise, there's just one declaration. */
13720 else
13722 bool saved_in_unbraced_linkage_specification_p;
13724 saved_in_unbraced_linkage_specification_p
13725 = parser->in_unbraced_linkage_specification_p;
13726 parser->in_unbraced_linkage_specification_p = true;
13727 cp_parser_declaration (parser);
13728 parser->in_unbraced_linkage_specification_p
13729 = saved_in_unbraced_linkage_specification_p;
13732 /* We're done with the linkage-specification. */
13733 pop_lang_context ();
13735 /* Restore location of parent linkage specification, if any. */
13736 parser->innermost_linkage_specification_location = saved_location;
13739 /* Parse a static_assert-declaration.
13741 static_assert-declaration:
13742 static_assert ( constant-expression , string-literal ) ;
13743 static_assert ( constant-expression ) ; (C++17)
13745 If MEMBER_P, this static_assert is a class member. */
13747 static void
13748 cp_parser_static_assert(cp_parser *parser, bool member_p)
13750 cp_expr condition;
13751 location_t token_loc;
13752 tree message;
13753 bool dummy;
13755 /* Peek at the `static_assert' token so we can keep track of exactly
13756 where the static assertion started. */
13757 token_loc = cp_lexer_peek_token (parser->lexer)->location;
13759 /* Look for the `static_assert' keyword. */
13760 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13761 RT_STATIC_ASSERT))
13762 return;
13764 /* We know we are in a static assertion; commit to any tentative
13765 parse. */
13766 if (cp_parser_parsing_tentatively (parser))
13767 cp_parser_commit_to_tentative_parse (parser);
13769 /* Parse the `(' starting the static assertion condition. */
13770 matching_parens parens;
13771 parens.require_open (parser);
13773 /* Parse the constant-expression. Allow a non-constant expression
13774 here in order to give better diagnostics in finish_static_assert. */
13775 condition =
13776 cp_parser_constant_expression (parser,
13777 /*allow_non_constant_p=*/true,
13778 /*non_constant_p=*/&dummy);
13780 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13782 if (cxx_dialect < cxx17)
13783 pedwarn (input_location, OPT_Wpedantic,
13784 "static_assert without a message "
13785 "only available with -std=c++17 or -std=gnu++17");
13786 /* Eat the ')' */
13787 cp_lexer_consume_token (parser->lexer);
13788 message = build_string (1, "");
13789 TREE_TYPE (message) = char_array_type_node;
13790 fix_string_type (message);
13792 else
13794 /* Parse the separating `,'. */
13795 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13797 /* Parse the string-literal message. */
13798 message = cp_parser_string_literal (parser,
13799 /*translate=*/false,
13800 /*wide_ok=*/true);
13802 /* A `)' completes the static assertion. */
13803 if (!parens.require_close (parser))
13804 cp_parser_skip_to_closing_parenthesis (parser,
13805 /*recovering=*/true,
13806 /*or_comma=*/false,
13807 /*consume_paren=*/true);
13810 /* A semicolon terminates the declaration. */
13811 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13813 /* Get the location for the static assertion. Use that of the
13814 condition if available, otherwise, use that of the "static_assert"
13815 token. */
13816 location_t assert_loc = condition.get_location ();
13817 if (assert_loc == UNKNOWN_LOCATION)
13818 assert_loc = token_loc;
13820 /* Complete the static assertion, which may mean either processing
13821 the static assert now or saving it for template instantiation. */
13822 finish_static_assert (condition, message, assert_loc, member_p);
13825 /* Parse the expression in decltype ( expression ). */
13827 static tree
13828 cp_parser_decltype_expr (cp_parser *parser,
13829 bool &id_expression_or_member_access_p)
13831 cp_token *id_expr_start_token;
13832 tree expr;
13834 /* Since we're going to preserve any side-effects from this parse, set up a
13835 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13836 in the expression. */
13837 tentative_firewall firewall (parser);
13839 /* First, try parsing an id-expression. */
13840 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13841 cp_parser_parse_tentatively (parser);
13842 expr = cp_parser_id_expression (parser,
13843 /*template_keyword_p=*/false,
13844 /*check_dependency_p=*/true,
13845 /*template_p=*/NULL,
13846 /*declarator_p=*/false,
13847 /*optional_p=*/false);
13849 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13851 bool non_integral_constant_expression_p = false;
13852 tree id_expression = expr;
13853 cp_id_kind idk;
13854 const char *error_msg;
13856 if (identifier_p (expr))
13857 /* Lookup the name we got back from the id-expression. */
13858 expr = cp_parser_lookup_name_simple (parser, expr,
13859 id_expr_start_token->location);
13861 if (expr
13862 && expr != error_mark_node
13863 && TREE_CODE (expr) != TYPE_DECL
13864 && (TREE_CODE (expr) != BIT_NOT_EXPR
13865 || !TYPE_P (TREE_OPERAND (expr, 0)))
13866 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13868 /* Complete lookup of the id-expression. */
13869 expr = (finish_id_expression
13870 (id_expression, expr, parser->scope, &idk,
13871 /*integral_constant_expression_p=*/false,
13872 /*allow_non_integral_constant_expression_p=*/true,
13873 &non_integral_constant_expression_p,
13874 /*template_p=*/false,
13875 /*done=*/true,
13876 /*address_p=*/false,
13877 /*template_arg_p=*/false,
13878 &error_msg,
13879 id_expr_start_token->location));
13881 if (expr == error_mark_node)
13882 /* We found an id-expression, but it was something that we
13883 should not have found. This is an error, not something
13884 we can recover from, so note that we found an
13885 id-expression and we'll recover as gracefully as
13886 possible. */
13887 id_expression_or_member_access_p = true;
13890 if (expr
13891 && expr != error_mark_node
13892 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13893 /* We have an id-expression. */
13894 id_expression_or_member_access_p = true;
13897 if (!id_expression_or_member_access_p)
13899 /* Abort the id-expression parse. */
13900 cp_parser_abort_tentative_parse (parser);
13902 /* Parsing tentatively, again. */
13903 cp_parser_parse_tentatively (parser);
13905 /* Parse a class member access. */
13906 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13907 /*cast_p=*/false, /*decltype*/true,
13908 /*member_access_only_p=*/true, NULL);
13910 if (expr
13911 && expr != error_mark_node
13912 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13913 /* We have an id-expression. */
13914 id_expression_or_member_access_p = true;
13917 if (id_expression_or_member_access_p)
13918 /* We have parsed the complete id-expression or member access. */
13919 cp_parser_parse_definitely (parser);
13920 else
13922 /* Abort our attempt to parse an id-expression or member access
13923 expression. */
13924 cp_parser_abort_tentative_parse (parser);
13926 /* Parse a full expression. */
13927 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
13928 /*decltype_p=*/true);
13931 return expr;
13934 /* Parse a `decltype' type. Returns the type.
13936 simple-type-specifier:
13937 decltype ( expression )
13938 C++14 proposal:
13939 decltype ( auto ) */
13941 static tree
13942 cp_parser_decltype (cp_parser *parser)
13944 tree expr;
13945 bool id_expression_or_member_access_p = false;
13946 const char *saved_message;
13947 bool saved_integral_constant_expression_p;
13948 bool saved_non_integral_constant_expression_p;
13949 bool saved_greater_than_is_operator_p;
13950 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13952 if (start_token->type == CPP_DECLTYPE)
13954 /* Already parsed. */
13955 cp_lexer_consume_token (parser->lexer);
13956 return saved_checks_value (start_token->u.tree_check_value);
13959 /* Look for the `decltype' token. */
13960 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
13961 return error_mark_node;
13963 /* Parse the opening `('. */
13964 matching_parens parens;
13965 if (!parens.require_open (parser))
13966 return error_mark_node;
13968 /* decltype (auto) */
13969 if (cxx_dialect >= cxx14
13970 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
13972 cp_lexer_consume_token (parser->lexer);
13973 if (!parens.require_close (parser))
13974 return error_mark_node;
13975 expr = make_decltype_auto ();
13976 AUTO_IS_DECLTYPE (expr) = true;
13977 goto rewrite;
13980 /* Types cannot be defined in a `decltype' expression. Save away the
13981 old message. */
13982 saved_message = parser->type_definition_forbidden_message;
13984 /* And create the new one. */
13985 parser->type_definition_forbidden_message
13986 = G_("types may not be defined in %<decltype%> expressions");
13988 /* The restrictions on constant-expressions do not apply inside
13989 decltype expressions. */
13990 saved_integral_constant_expression_p
13991 = parser->integral_constant_expression_p;
13992 saved_non_integral_constant_expression_p
13993 = parser->non_integral_constant_expression_p;
13994 parser->integral_constant_expression_p = false;
13996 /* Within a parenthesized expression, a `>' token is always
13997 the greater-than operator. */
13998 saved_greater_than_is_operator_p
13999 = parser->greater_than_is_operator_p;
14000 parser->greater_than_is_operator_p = true;
14002 /* Do not actually evaluate the expression. */
14003 ++cp_unevaluated_operand;
14005 /* Do not warn about problems with the expression. */
14006 ++c_inhibit_evaluation_warnings;
14008 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14010 /* Go back to evaluating expressions. */
14011 --cp_unevaluated_operand;
14012 --c_inhibit_evaluation_warnings;
14014 /* The `>' token might be the end of a template-id or
14015 template-parameter-list now. */
14016 parser->greater_than_is_operator_p
14017 = saved_greater_than_is_operator_p;
14019 /* Restore the old message and the integral constant expression
14020 flags. */
14021 parser->type_definition_forbidden_message = saved_message;
14022 parser->integral_constant_expression_p
14023 = saved_integral_constant_expression_p;
14024 parser->non_integral_constant_expression_p
14025 = saved_non_integral_constant_expression_p;
14027 /* Parse to the closing `)'. */
14028 if (!parens.require_close (parser))
14030 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14031 /*consume_paren=*/true);
14032 return error_mark_node;
14035 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14036 tf_warning_or_error);
14038 rewrite:
14039 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14040 it again. */
14041 start_token->type = CPP_DECLTYPE;
14042 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14043 start_token->u.tree_check_value->value = expr;
14044 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14045 start_token->keyword = RID_MAX;
14046 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14048 return expr;
14051 /* Special member functions [gram.special] */
14053 /* Parse a conversion-function-id.
14055 conversion-function-id:
14056 operator conversion-type-id
14058 Returns an IDENTIFIER_NODE representing the operator. */
14060 static tree
14061 cp_parser_conversion_function_id (cp_parser* parser)
14063 tree type;
14064 tree saved_scope;
14065 tree saved_qualifying_scope;
14066 tree saved_object_scope;
14067 tree pushed_scope = NULL_TREE;
14069 /* Look for the `operator' token. */
14070 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14071 return error_mark_node;
14072 /* When we parse the conversion-type-id, the current scope will be
14073 reset. However, we need that information in able to look up the
14074 conversion function later, so we save it here. */
14075 saved_scope = parser->scope;
14076 saved_qualifying_scope = parser->qualifying_scope;
14077 saved_object_scope = parser->object_scope;
14078 /* We must enter the scope of the class so that the names of
14079 entities declared within the class are available in the
14080 conversion-type-id. For example, consider:
14082 struct S {
14083 typedef int I;
14084 operator I();
14087 S::operator I() { ... }
14089 In order to see that `I' is a type-name in the definition, we
14090 must be in the scope of `S'. */
14091 if (saved_scope)
14092 pushed_scope = push_scope (saved_scope);
14093 /* Parse the conversion-type-id. */
14094 type = cp_parser_conversion_type_id (parser);
14095 /* Leave the scope of the class, if any. */
14096 if (pushed_scope)
14097 pop_scope (pushed_scope);
14098 /* Restore the saved scope. */
14099 parser->scope = saved_scope;
14100 parser->qualifying_scope = saved_qualifying_scope;
14101 parser->object_scope = saved_object_scope;
14102 /* If the TYPE is invalid, indicate failure. */
14103 if (type == error_mark_node)
14104 return error_mark_node;
14105 return make_conv_op_name (type);
14108 /* Parse a conversion-type-id:
14110 conversion-type-id:
14111 type-specifier-seq conversion-declarator [opt]
14113 Returns the TYPE specified. */
14115 static tree
14116 cp_parser_conversion_type_id (cp_parser* parser)
14118 tree attributes;
14119 cp_decl_specifier_seq type_specifiers;
14120 cp_declarator *declarator;
14121 tree type_specified;
14122 const char *saved_message;
14124 /* Parse the attributes. */
14125 attributes = cp_parser_attributes_opt (parser);
14127 saved_message = parser->type_definition_forbidden_message;
14128 parser->type_definition_forbidden_message
14129 = G_("types may not be defined in a conversion-type-id");
14131 /* Parse the type-specifiers. */
14132 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14133 /*is_trailing_return=*/false,
14134 &type_specifiers);
14136 parser->type_definition_forbidden_message = saved_message;
14138 /* If that didn't work, stop. */
14139 if (type_specifiers.type == error_mark_node)
14140 return error_mark_node;
14141 /* Parse the conversion-declarator. */
14142 declarator = cp_parser_conversion_declarator_opt (parser);
14144 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14145 /*initialized=*/0, &attributes);
14146 if (attributes)
14147 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14149 /* Don't give this error when parsing tentatively. This happens to
14150 work because we always parse this definitively once. */
14151 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14152 && type_uses_auto (type_specified))
14154 if (cxx_dialect < cxx14)
14156 error ("invalid use of %<auto%> in conversion operator");
14157 return error_mark_node;
14159 else if (template_parm_scope_p ())
14160 warning (0, "use of %<auto%> in member template "
14161 "conversion operator can never be deduced");
14164 return type_specified;
14167 /* Parse an (optional) conversion-declarator.
14169 conversion-declarator:
14170 ptr-operator conversion-declarator [opt]
14174 static cp_declarator *
14175 cp_parser_conversion_declarator_opt (cp_parser* parser)
14177 enum tree_code code;
14178 tree class_type, std_attributes = NULL_TREE;
14179 cp_cv_quals cv_quals;
14181 /* We don't know if there's a ptr-operator next, or not. */
14182 cp_parser_parse_tentatively (parser);
14183 /* Try the ptr-operator. */
14184 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14185 &std_attributes);
14186 /* If it worked, look for more conversion-declarators. */
14187 if (cp_parser_parse_definitely (parser))
14189 cp_declarator *declarator;
14191 /* Parse another optional declarator. */
14192 declarator = cp_parser_conversion_declarator_opt (parser);
14194 declarator = cp_parser_make_indirect_declarator
14195 (code, class_type, cv_quals, declarator, std_attributes);
14197 return declarator;
14200 return NULL;
14203 /* Parse an (optional) ctor-initializer.
14205 ctor-initializer:
14206 : mem-initializer-list */
14208 static void
14209 cp_parser_ctor_initializer_opt (cp_parser* parser)
14211 /* If the next token is not a `:', then there is no
14212 ctor-initializer. */
14213 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14215 /* Do default initialization of any bases and members. */
14216 if (DECL_CONSTRUCTOR_P (current_function_decl))
14217 finish_mem_initializers (NULL_TREE);
14218 return;
14221 /* Consume the `:' token. */
14222 cp_lexer_consume_token (parser->lexer);
14223 /* And the mem-initializer-list. */
14224 cp_parser_mem_initializer_list (parser);
14227 /* Parse a mem-initializer-list.
14229 mem-initializer-list:
14230 mem-initializer ... [opt]
14231 mem-initializer ... [opt] , mem-initializer-list */
14233 static void
14234 cp_parser_mem_initializer_list (cp_parser* parser)
14236 tree mem_initializer_list = NULL_TREE;
14237 tree target_ctor = error_mark_node;
14238 cp_token *token = cp_lexer_peek_token (parser->lexer);
14240 /* Let the semantic analysis code know that we are starting the
14241 mem-initializer-list. */
14242 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14243 error_at (token->location,
14244 "only constructors take member initializers");
14246 /* Loop through the list. */
14247 while (true)
14249 tree mem_initializer;
14251 token = cp_lexer_peek_token (parser->lexer);
14252 /* Parse the mem-initializer. */
14253 mem_initializer = cp_parser_mem_initializer (parser);
14254 /* If the next token is a `...', we're expanding member initializers. */
14255 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14257 /* Consume the `...'. */
14258 cp_lexer_consume_token (parser->lexer);
14260 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14261 can be expanded but members cannot. */
14262 if (mem_initializer != error_mark_node
14263 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14265 error_at (token->location,
14266 "cannot expand initializer for member %qD",
14267 TREE_PURPOSE (mem_initializer));
14268 mem_initializer = error_mark_node;
14271 /* Construct the pack expansion type. */
14272 if (mem_initializer != error_mark_node)
14273 mem_initializer = make_pack_expansion (mem_initializer);
14275 if (target_ctor != error_mark_node
14276 && mem_initializer != error_mark_node)
14278 error ("mem-initializer for %qD follows constructor delegation",
14279 TREE_PURPOSE (mem_initializer));
14280 mem_initializer = error_mark_node;
14282 /* Look for a target constructor. */
14283 if (mem_initializer != error_mark_node
14284 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14285 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14287 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14288 if (mem_initializer_list)
14290 error ("constructor delegation follows mem-initializer for %qD",
14291 TREE_PURPOSE (mem_initializer_list));
14292 mem_initializer = error_mark_node;
14294 target_ctor = mem_initializer;
14296 /* Add it to the list, unless it was erroneous. */
14297 if (mem_initializer != error_mark_node)
14299 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14300 mem_initializer_list = mem_initializer;
14302 /* If the next token is not a `,', we're done. */
14303 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14304 break;
14305 /* Consume the `,' token. */
14306 cp_lexer_consume_token (parser->lexer);
14309 /* Perform semantic analysis. */
14310 if (DECL_CONSTRUCTOR_P (current_function_decl))
14311 finish_mem_initializers (mem_initializer_list);
14314 /* Parse a mem-initializer.
14316 mem-initializer:
14317 mem-initializer-id ( expression-list [opt] )
14318 mem-initializer-id braced-init-list
14320 GNU extension:
14322 mem-initializer:
14323 ( expression-list [opt] )
14325 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14326 class) or FIELD_DECL (for a non-static data member) to initialize;
14327 the TREE_VALUE is the expression-list. An empty initialization
14328 list is represented by void_list_node. */
14330 static tree
14331 cp_parser_mem_initializer (cp_parser* parser)
14333 tree mem_initializer_id;
14334 tree expression_list;
14335 tree member;
14336 cp_token *token = cp_lexer_peek_token (parser->lexer);
14338 /* Find out what is being initialized. */
14339 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14341 permerror (token->location,
14342 "anachronistic old-style base class initializer");
14343 mem_initializer_id = NULL_TREE;
14345 else
14347 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14348 if (mem_initializer_id == error_mark_node)
14349 return mem_initializer_id;
14351 member = expand_member_init (mem_initializer_id);
14352 if (member && !DECL_P (member))
14353 in_base_initializer = 1;
14355 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14357 bool expr_non_constant_p;
14358 cp_lexer_set_source_position (parser->lexer);
14359 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14360 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14361 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14362 expression_list = build_tree_list (NULL_TREE, expression_list);
14364 else
14366 vec<tree, va_gc> *vec;
14367 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14368 /*cast_p=*/false,
14369 /*allow_expansion_p=*/true,
14370 /*non_constant_p=*/NULL);
14371 if (vec == NULL)
14372 return error_mark_node;
14373 expression_list = build_tree_list_vec (vec);
14374 release_tree_vector (vec);
14377 if (expression_list == error_mark_node)
14378 return error_mark_node;
14379 if (!expression_list)
14380 expression_list = void_type_node;
14382 in_base_initializer = 0;
14384 return member ? build_tree_list (member, expression_list) : error_mark_node;
14387 /* Parse a mem-initializer-id.
14389 mem-initializer-id:
14390 :: [opt] nested-name-specifier [opt] class-name
14391 decltype-specifier (C++11)
14392 identifier
14394 Returns a TYPE indicating the class to be initialized for the first
14395 production (and the second in C++11). Returns an IDENTIFIER_NODE
14396 indicating the data member to be initialized for the last production. */
14398 static tree
14399 cp_parser_mem_initializer_id (cp_parser* parser)
14401 bool global_scope_p;
14402 bool nested_name_specifier_p;
14403 bool template_p = false;
14404 tree id;
14406 cp_token *token = cp_lexer_peek_token (parser->lexer);
14408 /* `typename' is not allowed in this context ([temp.res]). */
14409 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14411 error_at (token->location,
14412 "keyword %<typename%> not allowed in this context (a qualified "
14413 "member initializer is implicitly a type)");
14414 cp_lexer_consume_token (parser->lexer);
14416 /* Look for the optional `::' operator. */
14417 global_scope_p
14418 = (cp_parser_global_scope_opt (parser,
14419 /*current_scope_valid_p=*/false)
14420 != NULL_TREE);
14421 /* Look for the optional nested-name-specifier. The simplest way to
14422 implement:
14424 [temp.res]
14426 The keyword `typename' is not permitted in a base-specifier or
14427 mem-initializer; in these contexts a qualified name that
14428 depends on a template-parameter is implicitly assumed to be a
14429 type name.
14431 is to assume that we have seen the `typename' keyword at this
14432 point. */
14433 nested_name_specifier_p
14434 = (cp_parser_nested_name_specifier_opt (parser,
14435 /*typename_keyword_p=*/true,
14436 /*check_dependency_p=*/true,
14437 /*type_p=*/true,
14438 /*is_declaration=*/true)
14439 != NULL_TREE);
14440 if (nested_name_specifier_p)
14441 template_p = cp_parser_optional_template_keyword (parser);
14442 /* If there is a `::' operator or a nested-name-specifier, then we
14443 are definitely looking for a class-name. */
14444 if (global_scope_p || nested_name_specifier_p)
14445 return cp_parser_class_name (parser,
14446 /*typename_keyword_p=*/true,
14447 /*template_keyword_p=*/template_p,
14448 typename_type,
14449 /*check_dependency_p=*/true,
14450 /*class_head_p=*/false,
14451 /*is_declaration=*/true);
14452 /* Otherwise, we could also be looking for an ordinary identifier. */
14453 cp_parser_parse_tentatively (parser);
14454 if (cp_lexer_next_token_is_decltype (parser->lexer))
14455 /* Try a decltype-specifier. */
14456 id = cp_parser_decltype (parser);
14457 else
14458 /* Otherwise, try a class-name. */
14459 id = cp_parser_class_name (parser,
14460 /*typename_keyword_p=*/true,
14461 /*template_keyword_p=*/false,
14462 none_type,
14463 /*check_dependency_p=*/true,
14464 /*class_head_p=*/false,
14465 /*is_declaration=*/true);
14466 /* If we found one, we're done. */
14467 if (cp_parser_parse_definitely (parser))
14468 return id;
14469 /* Otherwise, look for an ordinary identifier. */
14470 return cp_parser_identifier (parser);
14473 /* Overloading [gram.over] */
14475 /* Parse an operator-function-id.
14477 operator-function-id:
14478 operator operator
14480 Returns an IDENTIFIER_NODE for the operator which is a
14481 human-readable spelling of the identifier, e.g., `operator +'. */
14483 static cp_expr
14484 cp_parser_operator_function_id (cp_parser* parser)
14486 /* Look for the `operator' keyword. */
14487 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14488 return error_mark_node;
14489 /* And then the name of the operator itself. */
14490 return cp_parser_operator (parser);
14493 /* Return an identifier node for a user-defined literal operator.
14494 The suffix identifier is chained to the operator name identifier. */
14496 tree
14497 cp_literal_operator_id (const char* name)
14499 tree identifier;
14500 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14501 + strlen (name) + 10);
14502 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14503 identifier = get_identifier (buffer);
14505 return identifier;
14508 /* Parse an operator.
14510 operator:
14511 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14512 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14513 || ++ -- , ->* -> () []
14515 GNU Extensions:
14517 operator:
14518 <? >? <?= >?=
14520 Returns an IDENTIFIER_NODE for the operator which is a
14521 human-readable spelling of the identifier, e.g., `operator +'. */
14523 static cp_expr
14524 cp_parser_operator (cp_parser* parser)
14526 tree id = NULL_TREE;
14527 cp_token *token;
14528 bool utf8 = false;
14530 /* Peek at the next token. */
14531 token = cp_lexer_peek_token (parser->lexer);
14533 location_t start_loc = token->location;
14535 /* Figure out which operator we have. */
14536 enum tree_code op = ERROR_MARK;
14537 bool assop = false;
14538 bool consumed = false;
14539 switch (token->type)
14541 case CPP_KEYWORD:
14543 /* The keyword should be either `new' or `delete'. */
14544 if (token->keyword == RID_NEW)
14545 op = NEW_EXPR;
14546 else if (token->keyword == RID_DELETE)
14547 op = DELETE_EXPR;
14548 else
14549 break;
14551 /* Consume the `new' or `delete' token. */
14552 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14554 /* Peek at the next token. */
14555 token = cp_lexer_peek_token (parser->lexer);
14556 /* If it's a `[' token then this is the array variant of the
14557 operator. */
14558 if (token->type == CPP_OPEN_SQUARE)
14560 /* Consume the `[' token. */
14561 cp_lexer_consume_token (parser->lexer);
14562 /* Look for the `]' token. */
14563 if (cp_token *close_token
14564 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14565 end_loc = close_token->location;
14566 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14568 start_loc = make_location (start_loc, start_loc, end_loc);
14569 consumed = true;
14570 break;
14573 case CPP_PLUS:
14574 op = PLUS_EXPR;
14575 break;
14577 case CPP_MINUS:
14578 op = MINUS_EXPR;
14579 break;
14581 case CPP_MULT:
14582 op = MULT_EXPR;
14583 break;
14585 case CPP_DIV:
14586 op = TRUNC_DIV_EXPR;
14587 break;
14589 case CPP_MOD:
14590 op = TRUNC_MOD_EXPR;
14591 break;
14593 case CPP_XOR:
14594 op = BIT_XOR_EXPR;
14595 break;
14597 case CPP_AND:
14598 op = BIT_AND_EXPR;
14599 break;
14601 case CPP_OR:
14602 op = BIT_IOR_EXPR;
14603 break;
14605 case CPP_COMPL:
14606 op = BIT_NOT_EXPR;
14607 break;
14609 case CPP_NOT:
14610 op = TRUTH_NOT_EXPR;
14611 break;
14613 case CPP_EQ:
14614 assop = true;
14615 op = NOP_EXPR;
14616 break;
14618 case CPP_LESS:
14619 op = LT_EXPR;
14620 break;
14622 case CPP_GREATER:
14623 op = GT_EXPR;
14624 break;
14626 case CPP_PLUS_EQ:
14627 assop = true;
14628 op = PLUS_EXPR;
14629 break;
14631 case CPP_MINUS_EQ:
14632 assop = true;
14633 op = MINUS_EXPR;
14634 break;
14636 case CPP_MULT_EQ:
14637 assop = true;
14638 op = MULT_EXPR;
14639 break;
14641 case CPP_DIV_EQ:
14642 assop = true;
14643 op = TRUNC_DIV_EXPR;
14644 break;
14646 case CPP_MOD_EQ:
14647 assop = true;
14648 op = TRUNC_MOD_EXPR;
14649 break;
14651 case CPP_XOR_EQ:
14652 assop = true;
14653 op = BIT_XOR_EXPR;
14654 break;
14656 case CPP_AND_EQ:
14657 assop = true;
14658 op = BIT_AND_EXPR;
14659 break;
14661 case CPP_OR_EQ:
14662 assop = true;
14663 op = BIT_IOR_EXPR;
14664 break;
14666 case CPP_LSHIFT:
14667 op = LSHIFT_EXPR;
14668 break;
14670 case CPP_RSHIFT:
14671 op = RSHIFT_EXPR;
14672 break;
14674 case CPP_LSHIFT_EQ:
14675 assop = true;
14676 op = LSHIFT_EXPR;
14677 break;
14679 case CPP_RSHIFT_EQ:
14680 assop = true;
14681 op = RSHIFT_EXPR;
14682 break;
14684 case CPP_EQ_EQ:
14685 op = EQ_EXPR;
14686 break;
14688 case CPP_NOT_EQ:
14689 op = NE_EXPR;
14690 break;
14692 case CPP_LESS_EQ:
14693 op = LE_EXPR;
14694 break;
14696 case CPP_GREATER_EQ:
14697 op = GE_EXPR;
14698 break;
14700 case CPP_AND_AND:
14701 op = TRUTH_ANDIF_EXPR;
14702 break;
14704 case CPP_OR_OR:
14705 op = TRUTH_ORIF_EXPR;
14706 break;
14708 case CPP_PLUS_PLUS:
14709 op = POSTINCREMENT_EXPR;
14710 break;
14712 case CPP_MINUS_MINUS:
14713 op = PREDECREMENT_EXPR;
14714 break;
14716 case CPP_COMMA:
14717 op = COMPOUND_EXPR;
14718 break;
14720 case CPP_DEREF_STAR:
14721 op = MEMBER_REF;
14722 break;
14724 case CPP_DEREF:
14725 op = COMPONENT_REF;
14726 break;
14728 case CPP_OPEN_PAREN:
14730 /* Consume the `('. */
14731 matching_parens parens;
14732 parens.consume_open (parser);
14733 /* Look for the matching `)'. */
14734 parens.require_close (parser);
14735 op = CALL_EXPR;
14736 consumed = true;
14737 break;
14740 case CPP_OPEN_SQUARE:
14741 /* Consume the `['. */
14742 cp_lexer_consume_token (parser->lexer);
14743 /* Look for the matching `]'. */
14744 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14745 op = ARRAY_REF;
14746 consumed = true;
14747 break;
14749 case CPP_UTF8STRING:
14750 case CPP_UTF8STRING_USERDEF:
14751 utf8 = true;
14752 /* FALLTHRU */
14753 case CPP_STRING:
14754 case CPP_WSTRING:
14755 case CPP_STRING16:
14756 case CPP_STRING32:
14757 case CPP_STRING_USERDEF:
14758 case CPP_WSTRING_USERDEF:
14759 case CPP_STRING16_USERDEF:
14760 case CPP_STRING32_USERDEF:
14762 tree str, string_tree;
14763 int sz, len;
14765 if (cxx_dialect == cxx98)
14766 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14768 /* Consume the string. */
14769 str = cp_parser_string_literal (parser, /*translate=*/true,
14770 /*wide_ok=*/true, /*lookup_udlit=*/false);
14771 if (str == error_mark_node)
14772 return error_mark_node;
14773 else if (TREE_CODE (str) == USERDEF_LITERAL)
14775 string_tree = USERDEF_LITERAL_VALUE (str);
14776 id = USERDEF_LITERAL_SUFFIX_ID (str);
14778 else
14780 string_tree = str;
14781 /* Look for the suffix identifier. */
14782 token = cp_lexer_peek_token (parser->lexer);
14783 if (token->type == CPP_NAME)
14784 id = cp_parser_identifier (parser);
14785 else if (token->type == CPP_KEYWORD)
14787 error ("unexpected keyword;"
14788 " remove space between quotes and suffix identifier");
14789 return error_mark_node;
14791 else
14793 error ("expected suffix identifier");
14794 return error_mark_node;
14797 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14798 (TREE_TYPE (TREE_TYPE (string_tree))));
14799 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14800 if (len != 0)
14802 error ("expected empty string after %<operator%> keyword");
14803 return error_mark_node;
14805 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14806 != char_type_node)
14808 error ("invalid encoding prefix in literal operator");
14809 return error_mark_node;
14811 if (id != error_mark_node)
14813 const char *name = IDENTIFIER_POINTER (id);
14814 id = cp_literal_operator_id (name);
14816 return id;
14819 default:
14820 /* Anything else is an error. */
14821 break;
14824 /* If we have selected an identifier, we need to consume the
14825 operator token. */
14826 if (op != ERROR_MARK)
14828 id = ovl_op_identifier (assop, op);
14829 if (!consumed)
14830 cp_lexer_consume_token (parser->lexer);
14832 /* Otherwise, no valid operator name was present. */
14833 else
14835 cp_parser_error (parser, "expected operator");
14836 id = error_mark_node;
14839 return cp_expr (id, start_loc);
14842 /* Parse a template-declaration.
14844 template-declaration:
14845 export [opt] template < template-parameter-list > declaration
14847 If MEMBER_P is TRUE, this template-declaration occurs within a
14848 class-specifier.
14850 The grammar rule given by the standard isn't correct. What
14851 is really meant is:
14853 template-declaration:
14854 export [opt] template-parameter-list-seq
14855 decl-specifier-seq [opt] init-declarator [opt] ;
14856 export [opt] template-parameter-list-seq
14857 function-definition
14859 template-parameter-list-seq:
14860 template-parameter-list-seq [opt]
14861 template < template-parameter-list >
14863 Concept Extensions:
14865 template-parameter-list-seq:
14866 template < template-parameter-list > requires-clause [opt]
14868 requires-clause:
14869 requires logical-or-expression */
14871 static void
14872 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14874 /* Check for `export'. */
14875 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14877 /* Consume the `export' token. */
14878 cp_lexer_consume_token (parser->lexer);
14879 /* Warn that we do not support `export'. */
14880 warning (0, "keyword %<export%> not implemented, and will be ignored");
14883 cp_parser_template_declaration_after_export (parser, member_p);
14886 /* Parse a template-parameter-list.
14888 template-parameter-list:
14889 template-parameter
14890 template-parameter-list , template-parameter
14892 Returns a TREE_LIST. Each node represents a template parameter.
14893 The nodes are connected via their TREE_CHAINs. */
14895 static tree
14896 cp_parser_template_parameter_list (cp_parser* parser)
14898 tree parameter_list = NULL_TREE;
14900 begin_template_parm_list ();
14902 /* The loop below parses the template parms. We first need to know
14903 the total number of template parms to be able to compute proper
14904 canonical types of each dependent type. So after the loop, when
14905 we know the total number of template parms,
14906 end_template_parm_list computes the proper canonical types and
14907 fixes up the dependent types accordingly. */
14908 while (true)
14910 tree parameter;
14911 bool is_non_type;
14912 bool is_parameter_pack;
14913 location_t parm_loc;
14915 /* Parse the template-parameter. */
14916 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
14917 parameter = cp_parser_template_parameter (parser,
14918 &is_non_type,
14919 &is_parameter_pack);
14920 /* Add it to the list. */
14921 if (parameter != error_mark_node)
14922 parameter_list = process_template_parm (parameter_list,
14923 parm_loc,
14924 parameter,
14925 is_non_type,
14926 is_parameter_pack);
14927 else
14929 tree err_parm = build_tree_list (parameter, parameter);
14930 parameter_list = chainon (parameter_list, err_parm);
14933 /* If the next token is not a `,', we're done. */
14934 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14935 break;
14936 /* Otherwise, consume the `,' token. */
14937 cp_lexer_consume_token (parser->lexer);
14940 return end_template_parm_list (parameter_list);
14943 /* Parse a introduction-list.
14945 introduction-list:
14946 introduced-parameter
14947 introduction-list , introduced-parameter
14949 introduced-parameter:
14950 ...[opt] identifier
14952 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
14953 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
14954 WILDCARD_DECL will also have DECL_NAME set and token location in
14955 DECL_SOURCE_LOCATION. */
14957 static tree
14958 cp_parser_introduction_list (cp_parser *parser)
14960 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
14962 while (true)
14964 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14965 if (is_pack)
14966 cp_lexer_consume_token (parser->lexer);
14968 /* Build placeholder. */
14969 tree parm = build_nt (WILDCARD_DECL);
14970 DECL_SOURCE_LOCATION (parm)
14971 = cp_lexer_peek_token (parser->lexer)->location;
14972 DECL_NAME (parm) = cp_parser_identifier (parser);
14973 WILDCARD_PACK_P (parm) = is_pack;
14974 vec_safe_push (introduction_vec, parm);
14976 /* If the next token is not a `,', we're done. */
14977 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14978 break;
14979 /* Otherwise, consume the `,' token. */
14980 cp_lexer_consume_token (parser->lexer);
14983 /* Convert the vec into a TREE_VEC. */
14984 tree introduction_list = make_tree_vec (introduction_vec->length ());
14985 unsigned int n;
14986 tree parm;
14987 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
14988 TREE_VEC_ELT (introduction_list, n) = parm;
14990 release_tree_vector (introduction_vec);
14991 return introduction_list;
14994 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
14995 is an abstract declarator. */
14997 static inline cp_declarator*
14998 get_id_declarator (cp_declarator *declarator)
15000 cp_declarator *d = declarator;
15001 while (d && d->kind != cdk_id)
15002 d = d->declarator;
15003 return d;
15006 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15007 is an abstract declarator. */
15009 static inline tree
15010 get_unqualified_id (cp_declarator *declarator)
15012 declarator = get_id_declarator (declarator);
15013 if (declarator)
15014 return declarator->u.id.unqualified_name;
15015 else
15016 return NULL_TREE;
15019 /* Returns true if DECL represents a constrained-parameter. */
15021 static inline bool
15022 is_constrained_parameter (tree decl)
15024 return (decl
15025 && TREE_CODE (decl) == TYPE_DECL
15026 && CONSTRAINED_PARM_CONCEPT (decl)
15027 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15030 /* Returns true if PARM declares a constrained-parameter. */
15032 static inline bool
15033 is_constrained_parameter (cp_parameter_declarator *parm)
15035 return is_constrained_parameter (parm->decl_specifiers.type);
15038 /* Check that the type parameter is only a declarator-id, and that its
15039 type is not cv-qualified. */
15041 bool
15042 cp_parser_check_constrained_type_parm (cp_parser *parser,
15043 cp_parameter_declarator *parm)
15045 if (!parm->declarator)
15046 return true;
15048 if (parm->declarator->kind != cdk_id)
15050 cp_parser_error (parser, "invalid constrained type parameter");
15051 return false;
15054 /* Don't allow cv-qualified type parameters. */
15055 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15056 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15058 cp_parser_error (parser, "cv-qualified type parameter");
15059 return false;
15062 return true;
15065 /* Finish parsing/processing a template type parameter and checking
15066 various restrictions. */
15068 static inline tree
15069 cp_parser_constrained_type_template_parm (cp_parser *parser,
15070 tree id,
15071 cp_parameter_declarator* parmdecl)
15073 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15074 return finish_template_type_parm (class_type_node, id);
15075 else
15076 return error_mark_node;
15079 static tree
15080 finish_constrained_template_template_parm (tree proto, tree id)
15082 /* FIXME: This should probably be copied, and we may need to adjust
15083 the template parameter depths. */
15084 tree saved_parms = current_template_parms;
15085 begin_template_parm_list ();
15086 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15087 end_template_parm_list ();
15089 tree parm = finish_template_template_parm (class_type_node, id);
15090 current_template_parms = saved_parms;
15092 return parm;
15095 /* Finish parsing/processing a template template parameter by borrowing
15096 the template parameter list from the prototype parameter. */
15098 static tree
15099 cp_parser_constrained_template_template_parm (cp_parser *parser,
15100 tree proto,
15101 tree id,
15102 cp_parameter_declarator *parmdecl)
15104 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15105 return error_mark_node;
15106 return finish_constrained_template_template_parm (proto, id);
15109 /* Create a new non-type template parameter from the given PARM
15110 declarator. */
15112 static tree
15113 constrained_non_type_template_parm (bool *is_non_type,
15114 cp_parameter_declarator *parm)
15116 *is_non_type = true;
15117 cp_declarator *decl = parm->declarator;
15118 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15119 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15120 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15123 /* Build a constrained template parameter based on the PARMDECL
15124 declarator. The type of PARMDECL is the constrained type, which
15125 refers to the prototype template parameter that ultimately
15126 specifies the type of the declared parameter. */
15128 static tree
15129 finish_constrained_parameter (cp_parser *parser,
15130 cp_parameter_declarator *parmdecl,
15131 bool *is_non_type,
15132 bool *is_parameter_pack)
15134 tree decl = parmdecl->decl_specifiers.type;
15135 tree id = get_unqualified_id (parmdecl->declarator);
15136 tree def = parmdecl->default_argument;
15137 tree proto = DECL_INITIAL (decl);
15139 /* A template parameter constrained by a variadic concept shall also
15140 be declared as a template parameter pack. */
15141 bool is_variadic = template_parameter_pack_p (proto);
15142 if (is_variadic && !*is_parameter_pack)
15143 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15145 /* Build the parameter. Return an error if the declarator was invalid. */
15146 tree parm;
15147 if (TREE_CODE (proto) == TYPE_DECL)
15148 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15149 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15150 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15151 parmdecl);
15152 else
15153 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15154 if (parm == error_mark_node)
15155 return error_mark_node;
15157 /* Finish the parameter decl and create a node attaching the
15158 default argument and constraint. */
15159 parm = build_tree_list (def, parm);
15160 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15162 return parm;
15165 /* Returns true if the parsed type actually represents the declaration
15166 of a type template-parameter. */
15168 static inline bool
15169 declares_constrained_type_template_parameter (tree type)
15171 return (is_constrained_parameter (type)
15172 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15176 /* Returns true if the parsed type actually represents the declaration of
15177 a template template-parameter. */
15179 static bool
15180 declares_constrained_template_template_parameter (tree type)
15182 return (is_constrained_parameter (type)
15183 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15186 /* Parse a default argument for a type template-parameter.
15187 Note that diagnostics are handled in cp_parser_template_parameter. */
15189 static tree
15190 cp_parser_default_type_template_argument (cp_parser *parser)
15192 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15194 /* Consume the `=' token. */
15195 cp_lexer_consume_token (parser->lexer);
15197 cp_token *token = cp_lexer_peek_token (parser->lexer);
15199 /* Parse the default-argument. */
15200 push_deferring_access_checks (dk_no_deferred);
15201 tree default_argument = cp_parser_type_id (parser);
15202 pop_deferring_access_checks ();
15204 if (flag_concepts && type_uses_auto (default_argument))
15206 error_at (token->location,
15207 "invalid use of %<auto%> in default template argument");
15208 return error_mark_node;
15211 return default_argument;
15214 /* Parse a default argument for a template template-parameter. */
15216 static tree
15217 cp_parser_default_template_template_argument (cp_parser *parser)
15219 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15221 bool is_template;
15223 /* Consume the `='. */
15224 cp_lexer_consume_token (parser->lexer);
15225 /* Parse the id-expression. */
15226 push_deferring_access_checks (dk_no_deferred);
15227 /* save token before parsing the id-expression, for error
15228 reporting */
15229 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15230 tree default_argument
15231 = cp_parser_id_expression (parser,
15232 /*template_keyword_p=*/false,
15233 /*check_dependency_p=*/true,
15234 /*template_p=*/&is_template,
15235 /*declarator_p=*/false,
15236 /*optional_p=*/false);
15237 if (TREE_CODE (default_argument) == TYPE_DECL)
15238 /* If the id-expression was a template-id that refers to
15239 a template-class, we already have the declaration here,
15240 so no further lookup is needed. */
15242 else
15243 /* Look up the name. */
15244 default_argument
15245 = cp_parser_lookup_name (parser, default_argument,
15246 none_type,
15247 /*is_template=*/is_template,
15248 /*is_namespace=*/false,
15249 /*check_dependency=*/true,
15250 /*ambiguous_decls=*/NULL,
15251 token->location);
15252 /* See if the default argument is valid. */
15253 default_argument = check_template_template_default_arg (default_argument);
15254 pop_deferring_access_checks ();
15255 return default_argument;
15258 /* Parse a template-parameter.
15260 template-parameter:
15261 type-parameter
15262 parameter-declaration
15264 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15265 the parameter. The TREE_PURPOSE is the default value, if any.
15266 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15267 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15268 set to true iff this parameter is a parameter pack. */
15270 static tree
15271 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15272 bool *is_parameter_pack)
15274 cp_token *token;
15275 cp_parameter_declarator *parameter_declarator;
15276 tree parm;
15278 /* Assume it is a type parameter or a template parameter. */
15279 *is_non_type = false;
15280 /* Assume it not a parameter pack. */
15281 *is_parameter_pack = false;
15282 /* Peek at the next token. */
15283 token = cp_lexer_peek_token (parser->lexer);
15284 /* If it is `template', we have a type-parameter. */
15285 if (token->keyword == RID_TEMPLATE)
15286 return cp_parser_type_parameter (parser, is_parameter_pack);
15287 /* If it is `class' or `typename' we do not know yet whether it is a
15288 type parameter or a non-type parameter. Consider:
15290 template <typename T, typename T::X X> ...
15294 template <class C, class D*> ...
15296 Here, the first parameter is a type parameter, and the second is
15297 a non-type parameter. We can tell by looking at the token after
15298 the identifier -- if it is a `,', `=', or `>' then we have a type
15299 parameter. */
15300 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15302 /* Peek at the token after `class' or `typename'. */
15303 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15304 /* If it's an ellipsis, we have a template type parameter
15305 pack. */
15306 if (token->type == CPP_ELLIPSIS)
15307 return cp_parser_type_parameter (parser, is_parameter_pack);
15308 /* If it's an identifier, skip it. */
15309 if (token->type == CPP_NAME)
15310 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15311 /* Now, see if the token looks like the end of a template
15312 parameter. */
15313 if (token->type == CPP_COMMA
15314 || token->type == CPP_EQ
15315 || token->type == CPP_GREATER)
15316 return cp_parser_type_parameter (parser, is_parameter_pack);
15319 /* Otherwise, it is a non-type parameter or a constrained parameter.
15321 [temp.param]
15323 When parsing a default template-argument for a non-type
15324 template-parameter, the first non-nested `>' is taken as the end
15325 of the template parameter-list rather than a greater-than
15326 operator. */
15327 parameter_declarator
15328 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15329 /*parenthesized_p=*/NULL);
15331 if (!parameter_declarator)
15332 return error_mark_node;
15334 /* If the parameter declaration is marked as a parameter pack, set
15335 *IS_PARAMETER_PACK to notify the caller. */
15336 if (parameter_declarator->template_parameter_pack_p)
15337 *is_parameter_pack = true;
15339 if (parameter_declarator->default_argument)
15341 /* Can happen in some cases of erroneous input (c++/34892). */
15342 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15343 /* Consume the `...' for better error recovery. */
15344 cp_lexer_consume_token (parser->lexer);
15347 // The parameter may have been constrained.
15348 if (is_constrained_parameter (parameter_declarator))
15349 return finish_constrained_parameter (parser,
15350 parameter_declarator,
15351 is_non_type,
15352 is_parameter_pack);
15354 // Now we're sure that the parameter is a non-type parameter.
15355 *is_non_type = true;
15357 parm = grokdeclarator (parameter_declarator->declarator,
15358 &parameter_declarator->decl_specifiers,
15359 TPARM, /*initialized=*/0,
15360 /*attrlist=*/NULL);
15361 if (parm == error_mark_node)
15362 return error_mark_node;
15364 return build_tree_list (parameter_declarator->default_argument, parm);
15367 /* Parse a type-parameter.
15369 type-parameter:
15370 class identifier [opt]
15371 class identifier [opt] = type-id
15372 typename identifier [opt]
15373 typename identifier [opt] = type-id
15374 template < template-parameter-list > class identifier [opt]
15375 template < template-parameter-list > class identifier [opt]
15376 = id-expression
15378 GNU Extension (variadic templates):
15380 type-parameter:
15381 class ... identifier [opt]
15382 typename ... identifier [opt]
15384 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15385 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15386 the declaration of the parameter.
15388 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15390 static tree
15391 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15393 cp_token *token;
15394 tree parameter;
15396 /* Look for a keyword to tell us what kind of parameter this is. */
15397 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15398 if (!token)
15399 return error_mark_node;
15401 switch (token->keyword)
15403 case RID_CLASS:
15404 case RID_TYPENAME:
15406 tree identifier;
15407 tree default_argument;
15409 /* If the next token is an ellipsis, we have a template
15410 argument pack. */
15411 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15413 /* Consume the `...' token. */
15414 cp_lexer_consume_token (parser->lexer);
15415 maybe_warn_variadic_templates ();
15417 *is_parameter_pack = true;
15420 /* If the next token is an identifier, then it names the
15421 parameter. */
15422 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15423 identifier = cp_parser_identifier (parser);
15424 else
15425 identifier = NULL_TREE;
15427 /* Create the parameter. */
15428 parameter = finish_template_type_parm (class_type_node, identifier);
15430 /* If the next token is an `=', we have a default argument. */
15431 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15433 default_argument
15434 = cp_parser_default_type_template_argument (parser);
15436 /* Template parameter packs cannot have default
15437 arguments. */
15438 if (*is_parameter_pack)
15440 if (identifier)
15441 error_at (token->location,
15442 "template parameter pack %qD cannot have a "
15443 "default argument", identifier);
15444 else
15445 error_at (token->location,
15446 "template parameter packs cannot have "
15447 "default arguments");
15448 default_argument = NULL_TREE;
15450 else if (check_for_bare_parameter_packs (default_argument))
15451 default_argument = error_mark_node;
15453 else
15454 default_argument = NULL_TREE;
15456 /* Create the combined representation of the parameter and the
15457 default argument. */
15458 parameter = build_tree_list (default_argument, parameter);
15460 break;
15462 case RID_TEMPLATE:
15464 tree identifier;
15465 tree default_argument;
15467 /* Look for the `<'. */
15468 cp_parser_require (parser, CPP_LESS, RT_LESS);
15469 /* Parse the template-parameter-list. */
15470 cp_parser_template_parameter_list (parser);
15471 /* Look for the `>'. */
15472 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15474 // If template requirements are present, parse them.
15475 if (flag_concepts)
15477 tree reqs = get_shorthand_constraints (current_template_parms);
15478 if (tree r = cp_parser_requires_clause_opt (parser))
15479 reqs = conjoin_constraints (reqs, normalize_expression (r));
15480 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15483 /* Look for the `class' or 'typename' keywords. */
15484 cp_parser_type_parameter_key (parser);
15485 /* If the next token is an ellipsis, we have a template
15486 argument pack. */
15487 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15489 /* Consume the `...' token. */
15490 cp_lexer_consume_token (parser->lexer);
15491 maybe_warn_variadic_templates ();
15493 *is_parameter_pack = true;
15495 /* If the next token is an `=', then there is a
15496 default-argument. If the next token is a `>', we are at
15497 the end of the parameter-list. If the next token is a `,',
15498 then we are at the end of this parameter. */
15499 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15500 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15501 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15503 identifier = cp_parser_identifier (parser);
15504 /* Treat invalid names as if the parameter were nameless. */
15505 if (identifier == error_mark_node)
15506 identifier = NULL_TREE;
15508 else
15509 identifier = NULL_TREE;
15511 /* Create the template parameter. */
15512 parameter = finish_template_template_parm (class_type_node,
15513 identifier);
15515 /* If the next token is an `=', then there is a
15516 default-argument. */
15517 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15519 default_argument
15520 = cp_parser_default_template_template_argument (parser);
15522 /* Template parameter packs cannot have default
15523 arguments. */
15524 if (*is_parameter_pack)
15526 if (identifier)
15527 error_at (token->location,
15528 "template parameter pack %qD cannot "
15529 "have a default argument",
15530 identifier);
15531 else
15532 error_at (token->location, "template parameter packs cannot "
15533 "have default arguments");
15534 default_argument = NULL_TREE;
15537 else
15538 default_argument = NULL_TREE;
15540 /* Create the combined representation of the parameter and the
15541 default argument. */
15542 parameter = build_tree_list (default_argument, parameter);
15544 break;
15546 default:
15547 gcc_unreachable ();
15548 break;
15551 return parameter;
15554 /* Parse a template-id.
15556 template-id:
15557 template-name < template-argument-list [opt] >
15559 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15560 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15561 returned. Otherwise, if the template-name names a function, or set
15562 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15563 names a class, returns a TYPE_DECL for the specialization.
15565 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15566 uninstantiated templates. */
15568 static tree
15569 cp_parser_template_id (cp_parser *parser,
15570 bool template_keyword_p,
15571 bool check_dependency_p,
15572 enum tag_types tag_type,
15573 bool is_declaration)
15575 tree templ;
15576 tree arguments;
15577 tree template_id;
15578 cp_token_position start_of_id = 0;
15579 cp_token *next_token = NULL, *next_token_2 = NULL;
15580 bool is_identifier;
15582 /* If the next token corresponds to a template-id, there is no need
15583 to reparse it. */
15584 cp_token *token = cp_lexer_peek_token (parser->lexer);
15585 if (token->type == CPP_TEMPLATE_ID)
15587 cp_lexer_consume_token (parser->lexer);
15588 return saved_checks_value (token->u.tree_check_value);
15591 /* Avoid performing name lookup if there is no possibility of
15592 finding a template-id. */
15593 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15594 || (token->type == CPP_NAME
15595 && !cp_parser_nth_token_starts_template_argument_list_p
15596 (parser, 2)))
15598 cp_parser_error (parser, "expected template-id");
15599 return error_mark_node;
15602 /* Remember where the template-id starts. */
15603 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15604 start_of_id = cp_lexer_token_position (parser->lexer, false);
15606 push_deferring_access_checks (dk_deferred);
15608 /* Parse the template-name. */
15609 is_identifier = false;
15610 templ = cp_parser_template_name (parser, template_keyword_p,
15611 check_dependency_p,
15612 is_declaration,
15613 tag_type,
15614 &is_identifier);
15615 if (templ == error_mark_node || is_identifier)
15617 pop_deferring_access_checks ();
15618 return templ;
15621 /* Since we're going to preserve any side-effects from this parse, set up a
15622 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15623 in the template arguments. */
15624 tentative_firewall firewall (parser);
15626 /* If we find the sequence `[:' after a template-name, it's probably
15627 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15628 parse correctly the argument list. */
15629 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15630 == CPP_OPEN_SQUARE)
15631 && next_token->flags & DIGRAPH
15632 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15633 == CPP_COLON)
15634 && !(next_token_2->flags & PREV_WHITE))
15636 cp_parser_parse_tentatively (parser);
15637 /* Change `:' into `::'. */
15638 next_token_2->type = CPP_SCOPE;
15639 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15640 CPP_LESS. */
15641 cp_lexer_consume_token (parser->lexer);
15643 /* Parse the arguments. */
15644 arguments = cp_parser_enclosed_template_argument_list (parser);
15645 if (!cp_parser_parse_definitely (parser))
15647 /* If we couldn't parse an argument list, then we revert our changes
15648 and return simply an error. Maybe this is not a template-id
15649 after all. */
15650 next_token_2->type = CPP_COLON;
15651 cp_parser_error (parser, "expected %<<%>");
15652 pop_deferring_access_checks ();
15653 return error_mark_node;
15655 /* Otherwise, emit an error about the invalid digraph, but continue
15656 parsing because we got our argument list. */
15657 if (permerror (next_token->location,
15658 "%<<::%> cannot begin a template-argument list"))
15660 static bool hint = false;
15661 inform (next_token->location,
15662 "%<<:%> is an alternate spelling for %<[%>."
15663 " Insert whitespace between %<<%> and %<::%>");
15664 if (!hint && !flag_permissive)
15666 inform (next_token->location, "(if you use %<-fpermissive%> "
15667 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15668 "accept your code)");
15669 hint = true;
15673 else
15675 /* Look for the `<' that starts the template-argument-list. */
15676 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15678 pop_deferring_access_checks ();
15679 return error_mark_node;
15681 /* Parse the arguments. */
15682 arguments = cp_parser_enclosed_template_argument_list (parser);
15685 /* Set the location to be of the form:
15686 template-name < template-argument-list [opt] >
15687 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15688 with caret == start at the start of the template-name,
15689 ranging until the closing '>'. */
15690 location_t finish_loc
15691 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15692 location_t combined_loc
15693 = make_location (token->location, token->location, finish_loc);
15695 /* Build a representation of the specialization. */
15696 if (identifier_p (templ))
15697 template_id = build_min_nt_loc (combined_loc,
15698 TEMPLATE_ID_EXPR,
15699 templ, arguments);
15700 else if (DECL_TYPE_TEMPLATE_P (templ)
15701 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15703 bool entering_scope;
15704 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15705 template (rather than some instantiation thereof) only if
15706 is not nested within some other construct. For example, in
15707 "template <typename T> void f(T) { A<T>::", A<T> is just an
15708 instantiation of A. */
15709 entering_scope = (template_parm_scope_p ()
15710 && cp_lexer_next_token_is (parser->lexer,
15711 CPP_SCOPE));
15712 template_id
15713 = finish_template_type (templ, arguments, entering_scope);
15715 /* A template-like identifier may be a partial concept id. */
15716 else if (flag_concepts
15717 && (template_id = (cp_parser_maybe_partial_concept_id
15718 (parser, templ, arguments))))
15719 return template_id;
15720 else if (variable_template_p (templ))
15722 template_id = lookup_template_variable (templ, arguments);
15723 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15724 SET_EXPR_LOCATION (template_id, combined_loc);
15726 else
15728 /* If it's not a class-template or a template-template, it should be
15729 a function-template. */
15730 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15731 || TREE_CODE (templ) == OVERLOAD
15732 || BASELINK_P (templ)));
15734 template_id = lookup_template_function (templ, arguments);
15735 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15736 SET_EXPR_LOCATION (template_id, combined_loc);
15739 /* If parsing tentatively, replace the sequence of tokens that makes
15740 up the template-id with a CPP_TEMPLATE_ID token. That way,
15741 should we re-parse the token stream, we will not have to repeat
15742 the effort required to do the parse, nor will we issue duplicate
15743 error messages about problems during instantiation of the
15744 template. */
15745 if (start_of_id
15746 /* Don't do this if we had a parse error in a declarator; re-parsing
15747 might succeed if a name changes meaning (60361). */
15748 && !(cp_parser_error_occurred (parser)
15749 && cp_parser_parsing_tentatively (parser)
15750 && parser->in_declarator_p))
15752 /* Reset the contents of the START_OF_ID token. */
15753 token->type = CPP_TEMPLATE_ID;
15754 token->location = combined_loc;
15756 /* We must mark the lookup as kept, so we don't throw it away on
15757 the first parse. */
15758 if (is_overloaded_fn (template_id))
15759 lookup_keep (get_fns (template_id), true);
15761 /* Retrieve any deferred checks. Do not pop this access checks yet
15762 so the memory will not be reclaimed during token replacing below. */
15763 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15764 token->u.tree_check_value->value = template_id;
15765 token->u.tree_check_value->checks = get_deferred_access_checks ();
15766 token->keyword = RID_MAX;
15768 /* Purge all subsequent tokens. */
15769 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15771 /* ??? Can we actually assume that, if template_id ==
15772 error_mark_node, we will have issued a diagnostic to the
15773 user, as opposed to simply marking the tentative parse as
15774 failed? */
15775 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15776 error_at (token->location, "parse error in template argument list");
15779 pop_to_parent_deferring_access_checks ();
15780 return template_id;
15783 /* Parse a template-name.
15785 template-name:
15786 identifier
15788 The standard should actually say:
15790 template-name:
15791 identifier
15792 operator-function-id
15794 A defect report has been filed about this issue.
15796 A conversion-function-id cannot be a template name because they cannot
15797 be part of a template-id. In fact, looking at this code:
15799 a.operator K<int>()
15801 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15802 It is impossible to call a templated conversion-function-id with an
15803 explicit argument list, since the only allowed template parameter is
15804 the type to which it is converting.
15806 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15807 `template' keyword, in a construction like:
15809 T::template f<3>()
15811 In that case `f' is taken to be a template-name, even though there
15812 is no way of knowing for sure.
15814 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15815 name refers to a set of overloaded functions, at least one of which
15816 is a template, or an IDENTIFIER_NODE with the name of the template,
15817 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15818 names are looked up inside uninstantiated templates. */
15820 static tree
15821 cp_parser_template_name (cp_parser* parser,
15822 bool template_keyword_p,
15823 bool check_dependency_p,
15824 bool is_declaration,
15825 enum tag_types tag_type,
15826 bool *is_identifier)
15828 tree identifier;
15829 tree decl;
15830 cp_token *token = cp_lexer_peek_token (parser->lexer);
15832 /* If the next token is `operator', then we have either an
15833 operator-function-id or a conversion-function-id. */
15834 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15836 /* We don't know whether we're looking at an
15837 operator-function-id or a conversion-function-id. */
15838 cp_parser_parse_tentatively (parser);
15839 /* Try an operator-function-id. */
15840 identifier = cp_parser_operator_function_id (parser);
15841 /* If that didn't work, try a conversion-function-id. */
15842 if (!cp_parser_parse_definitely (parser))
15844 cp_parser_error (parser, "expected template-name");
15845 return error_mark_node;
15848 /* Look for the identifier. */
15849 else
15850 identifier = cp_parser_identifier (parser);
15852 /* If we didn't find an identifier, we don't have a template-id. */
15853 if (identifier == error_mark_node)
15854 return error_mark_node;
15856 /* If the name immediately followed the `template' keyword, then it
15857 is a template-name. However, if the next token is not `<', then
15858 we do not treat it as a template-name, since it is not being used
15859 as part of a template-id. This enables us to handle constructs
15860 like:
15862 template <typename T> struct S { S(); };
15863 template <typename T> S<T>::S();
15865 correctly. We would treat `S' as a template -- if it were `S<T>'
15866 -- but we do not if there is no `<'. */
15868 if (processing_template_decl
15869 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15871 /* In a declaration, in a dependent context, we pretend that the
15872 "template" keyword was present in order to improve error
15873 recovery. For example, given:
15875 template <typename T> void f(T::X<int>);
15877 we want to treat "X<int>" as a template-id. */
15878 if (is_declaration
15879 && !template_keyword_p
15880 && parser->scope && TYPE_P (parser->scope)
15881 && check_dependency_p
15882 && dependent_scope_p (parser->scope)
15883 /* Do not do this for dtors (or ctors), since they never
15884 need the template keyword before their name. */
15885 && !constructor_name_p (identifier, parser->scope))
15887 cp_token_position start = 0;
15889 /* Explain what went wrong. */
15890 error_at (token->location, "non-template %qD used as template",
15891 identifier);
15892 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15893 parser->scope, identifier);
15894 /* If parsing tentatively, find the location of the "<" token. */
15895 if (cp_parser_simulate_error (parser))
15896 start = cp_lexer_token_position (parser->lexer, true);
15897 /* Parse the template arguments so that we can issue error
15898 messages about them. */
15899 cp_lexer_consume_token (parser->lexer);
15900 cp_parser_enclosed_template_argument_list (parser);
15901 /* Skip tokens until we find a good place from which to
15902 continue parsing. */
15903 cp_parser_skip_to_closing_parenthesis (parser,
15904 /*recovering=*/true,
15905 /*or_comma=*/true,
15906 /*consume_paren=*/false);
15907 /* If parsing tentatively, permanently remove the
15908 template argument list. That will prevent duplicate
15909 error messages from being issued about the missing
15910 "template" keyword. */
15911 if (start)
15912 cp_lexer_purge_tokens_after (parser->lexer, start);
15913 if (is_identifier)
15914 *is_identifier = true;
15915 parser->context->object_type = NULL_TREE;
15916 return identifier;
15919 /* If the "template" keyword is present, then there is generally
15920 no point in doing name-lookup, so we just return IDENTIFIER.
15921 But, if the qualifying scope is non-dependent then we can
15922 (and must) do name-lookup normally. */
15923 if (template_keyword_p)
15925 tree scope = (parser->scope ? parser->scope
15926 : parser->context->object_type);
15927 if (scope && TYPE_P (scope)
15928 && (!CLASS_TYPE_P (scope)
15929 || (check_dependency_p && dependent_type_p (scope))))
15931 /* We're optimizing away the call to cp_parser_lookup_name, but
15932 we still need to do this. */
15933 parser->context->object_type = NULL_TREE;
15934 return identifier;
15939 /* Look up the name. */
15940 decl = cp_parser_lookup_name (parser, identifier,
15941 tag_type,
15942 /*is_template=*/true,
15943 /*is_namespace=*/false,
15944 check_dependency_p,
15945 /*ambiguous_decls=*/NULL,
15946 token->location);
15948 decl = strip_using_decl (decl);
15950 /* If DECL is a template, then the name was a template-name. */
15951 if (TREE_CODE (decl) == TEMPLATE_DECL)
15953 if (TREE_DEPRECATED (decl)
15954 && deprecated_state != DEPRECATED_SUPPRESS)
15955 warn_deprecated_use (decl, NULL_TREE);
15957 else
15959 /* The standard does not explicitly indicate whether a name that
15960 names a set of overloaded declarations, some of which are
15961 templates, is a template-name. However, such a name should
15962 be a template-name; otherwise, there is no way to form a
15963 template-id for the overloaded templates. */
15964 bool found = false;
15966 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
15967 !found && iter; ++iter)
15968 if (TREE_CODE (*iter) == TEMPLATE_DECL)
15969 found = true;
15971 if (!found)
15973 /* The name does not name a template. */
15974 cp_parser_error (parser, "expected template-name");
15975 return error_mark_node;
15979 /* If DECL is dependent, and refers to a function, then just return
15980 its name; we will look it up again during template instantiation. */
15981 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
15983 tree scope = ovl_scope (decl);
15984 if (TYPE_P (scope) && dependent_type_p (scope))
15985 return identifier;
15988 return decl;
15991 /* Parse a template-argument-list.
15993 template-argument-list:
15994 template-argument ... [opt]
15995 template-argument-list , template-argument ... [opt]
15997 Returns a TREE_VEC containing the arguments. */
15999 static tree
16000 cp_parser_template_argument_list (cp_parser* parser)
16002 tree fixed_args[10];
16003 unsigned n_args = 0;
16004 unsigned alloced = 10;
16005 tree *arg_ary = fixed_args;
16006 tree vec;
16007 bool saved_in_template_argument_list_p;
16008 bool saved_ice_p;
16009 bool saved_non_ice_p;
16011 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16012 parser->in_template_argument_list_p = true;
16013 /* Even if the template-id appears in an integral
16014 constant-expression, the contents of the argument list do
16015 not. */
16016 saved_ice_p = parser->integral_constant_expression_p;
16017 parser->integral_constant_expression_p = false;
16018 saved_non_ice_p = parser->non_integral_constant_expression_p;
16019 parser->non_integral_constant_expression_p = false;
16021 /* Parse the arguments. */
16024 tree argument;
16026 if (n_args)
16027 /* Consume the comma. */
16028 cp_lexer_consume_token (parser->lexer);
16030 /* Parse the template-argument. */
16031 argument = cp_parser_template_argument (parser);
16033 /* If the next token is an ellipsis, we're expanding a template
16034 argument pack. */
16035 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16037 if (argument == error_mark_node)
16039 cp_token *token = cp_lexer_peek_token (parser->lexer);
16040 error_at (token->location,
16041 "expected parameter pack before %<...%>");
16043 /* Consume the `...' token. */
16044 cp_lexer_consume_token (parser->lexer);
16046 /* Make the argument into a TYPE_PACK_EXPANSION or
16047 EXPR_PACK_EXPANSION. */
16048 argument = make_pack_expansion (argument);
16051 if (n_args == alloced)
16053 alloced *= 2;
16055 if (arg_ary == fixed_args)
16057 arg_ary = XNEWVEC (tree, alloced);
16058 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16060 else
16061 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16063 arg_ary[n_args++] = argument;
16065 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16067 vec = make_tree_vec (n_args);
16069 while (n_args--)
16070 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16072 if (arg_ary != fixed_args)
16073 free (arg_ary);
16074 parser->non_integral_constant_expression_p = saved_non_ice_p;
16075 parser->integral_constant_expression_p = saved_ice_p;
16076 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16077 if (CHECKING_P)
16078 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16079 return vec;
16082 /* Parse a template-argument.
16084 template-argument:
16085 assignment-expression
16086 type-id
16087 id-expression
16089 The representation is that of an assignment-expression, type-id, or
16090 id-expression -- except that the qualified id-expression is
16091 evaluated, so that the value returned is either a DECL or an
16092 OVERLOAD.
16094 Although the standard says "assignment-expression", it forbids
16095 throw-expressions or assignments in the template argument.
16096 Therefore, we use "conditional-expression" instead. */
16098 static tree
16099 cp_parser_template_argument (cp_parser* parser)
16101 tree argument;
16102 bool template_p;
16103 bool address_p;
16104 bool maybe_type_id = false;
16105 cp_token *token = NULL, *argument_start_token = NULL;
16106 location_t loc = 0;
16107 cp_id_kind idk;
16109 /* There's really no way to know what we're looking at, so we just
16110 try each alternative in order.
16112 [temp.arg]
16114 In a template-argument, an ambiguity between a type-id and an
16115 expression is resolved to a type-id, regardless of the form of
16116 the corresponding template-parameter.
16118 Therefore, we try a type-id first. */
16119 cp_parser_parse_tentatively (parser);
16120 argument = cp_parser_template_type_arg (parser);
16121 /* If there was no error parsing the type-id but the next token is a
16122 '>>', our behavior depends on which dialect of C++ we're
16123 parsing. In C++98, we probably found a typo for '> >'. But there
16124 are type-id which are also valid expressions. For instance:
16126 struct X { int operator >> (int); };
16127 template <int V> struct Foo {};
16128 Foo<X () >> 5> r;
16130 Here 'X()' is a valid type-id of a function type, but the user just
16131 wanted to write the expression "X() >> 5". Thus, we remember that we
16132 found a valid type-id, but we still try to parse the argument as an
16133 expression to see what happens.
16135 In C++0x, the '>>' will be considered two separate '>'
16136 tokens. */
16137 if (!cp_parser_error_occurred (parser)
16138 && cxx_dialect == cxx98
16139 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16141 maybe_type_id = true;
16142 cp_parser_abort_tentative_parse (parser);
16144 else
16146 /* If the next token isn't a `,' or a `>', then this argument wasn't
16147 really finished. This means that the argument is not a valid
16148 type-id. */
16149 if (!cp_parser_next_token_ends_template_argument_p (parser))
16150 cp_parser_error (parser, "expected template-argument");
16151 /* If that worked, we're done. */
16152 if (cp_parser_parse_definitely (parser))
16153 return argument;
16155 /* We're still not sure what the argument will be. */
16156 cp_parser_parse_tentatively (parser);
16157 /* Try a template. */
16158 argument_start_token = cp_lexer_peek_token (parser->lexer);
16159 argument = cp_parser_id_expression (parser,
16160 /*template_keyword_p=*/false,
16161 /*check_dependency_p=*/true,
16162 &template_p,
16163 /*declarator_p=*/false,
16164 /*optional_p=*/false);
16165 /* If the next token isn't a `,' or a `>', then this argument wasn't
16166 really finished. */
16167 if (!cp_parser_next_token_ends_template_argument_p (parser))
16168 cp_parser_error (parser, "expected template-argument");
16169 if (!cp_parser_error_occurred (parser))
16171 /* Figure out what is being referred to. If the id-expression
16172 was for a class template specialization, then we will have a
16173 TYPE_DECL at this point. There is no need to do name lookup
16174 at this point in that case. */
16175 if (TREE_CODE (argument) != TYPE_DECL)
16176 argument = cp_parser_lookup_name (parser, argument,
16177 none_type,
16178 /*is_template=*/template_p,
16179 /*is_namespace=*/false,
16180 /*check_dependency=*/true,
16181 /*ambiguous_decls=*/NULL,
16182 argument_start_token->location);
16183 /* Handle a constrained-type-specifier for a non-type template
16184 parameter. */
16185 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16186 argument = decl;
16187 else if (TREE_CODE (argument) != TEMPLATE_DECL
16188 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16189 cp_parser_error (parser, "expected template-name");
16191 if (cp_parser_parse_definitely (parser))
16193 if (TREE_DEPRECATED (argument))
16194 warn_deprecated_use (argument, NULL_TREE);
16195 return argument;
16197 /* It must be a non-type argument. In C++17 any constant-expression is
16198 allowed. */
16199 if (cxx_dialect > cxx14)
16200 goto general_expr;
16202 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16204 -- an integral constant-expression of integral or enumeration
16205 type; or
16207 -- the name of a non-type template-parameter; or
16209 -- the name of an object or function with external linkage...
16211 -- the address of an object or function with external linkage...
16213 -- a pointer to member... */
16214 /* Look for a non-type template parameter. */
16215 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16217 cp_parser_parse_tentatively (parser);
16218 argument = cp_parser_primary_expression (parser,
16219 /*address_p=*/false,
16220 /*cast_p=*/false,
16221 /*template_arg_p=*/true,
16222 &idk);
16223 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16224 || !cp_parser_next_token_ends_template_argument_p (parser))
16225 cp_parser_simulate_error (parser);
16226 if (cp_parser_parse_definitely (parser))
16227 return argument;
16230 /* If the next token is "&", the argument must be the address of an
16231 object or function with external linkage. */
16232 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16233 if (address_p)
16235 loc = cp_lexer_peek_token (parser->lexer)->location;
16236 cp_lexer_consume_token (parser->lexer);
16238 /* See if we might have an id-expression. */
16239 token = cp_lexer_peek_token (parser->lexer);
16240 if (token->type == CPP_NAME
16241 || token->keyword == RID_OPERATOR
16242 || token->type == CPP_SCOPE
16243 || token->type == CPP_TEMPLATE_ID
16244 || token->type == CPP_NESTED_NAME_SPECIFIER)
16246 cp_parser_parse_tentatively (parser);
16247 argument = cp_parser_primary_expression (parser,
16248 address_p,
16249 /*cast_p=*/false,
16250 /*template_arg_p=*/true,
16251 &idk);
16252 if (cp_parser_error_occurred (parser)
16253 || !cp_parser_next_token_ends_template_argument_p (parser))
16254 cp_parser_abort_tentative_parse (parser);
16255 else
16257 tree probe;
16259 if (INDIRECT_REF_P (argument))
16261 /* Strip the dereference temporarily. */
16262 gcc_assert (REFERENCE_REF_P (argument));
16263 argument = TREE_OPERAND (argument, 0);
16266 /* If we're in a template, we represent a qualified-id referring
16267 to a static data member as a SCOPE_REF even if the scope isn't
16268 dependent so that we can check access control later. */
16269 probe = argument;
16270 if (TREE_CODE (probe) == SCOPE_REF)
16271 probe = TREE_OPERAND (probe, 1);
16272 if (VAR_P (probe))
16274 /* A variable without external linkage might still be a
16275 valid constant-expression, so no error is issued here
16276 if the external-linkage check fails. */
16277 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16278 cp_parser_simulate_error (parser);
16280 else if (is_overloaded_fn (argument))
16281 /* All overloaded functions are allowed; if the external
16282 linkage test does not pass, an error will be issued
16283 later. */
16285 else if (address_p
16286 && (TREE_CODE (argument) == OFFSET_REF
16287 || TREE_CODE (argument) == SCOPE_REF))
16288 /* A pointer-to-member. */
16290 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16292 else
16293 cp_parser_simulate_error (parser);
16295 if (cp_parser_parse_definitely (parser))
16297 if (address_p)
16298 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16299 tf_warning_or_error);
16300 else
16301 argument = convert_from_reference (argument);
16302 return argument;
16306 /* If the argument started with "&", there are no other valid
16307 alternatives at this point. */
16308 if (address_p)
16310 cp_parser_error (parser, "invalid non-type template argument");
16311 return error_mark_node;
16314 general_expr:
16315 /* If the argument wasn't successfully parsed as a type-id followed
16316 by '>>', the argument can only be a constant expression now.
16317 Otherwise, we try parsing the constant-expression tentatively,
16318 because the argument could really be a type-id. */
16319 if (maybe_type_id)
16320 cp_parser_parse_tentatively (parser);
16322 if (cxx_dialect <= cxx14)
16323 argument = cp_parser_constant_expression (parser);
16324 else
16326 /* With C++17 generalized non-type template arguments we need to handle
16327 lvalue constant expressions, too. */
16328 argument = cp_parser_assignment_expression (parser);
16329 require_potential_constant_expression (argument);
16332 if (!maybe_type_id)
16333 return argument;
16334 if (!cp_parser_next_token_ends_template_argument_p (parser))
16335 cp_parser_error (parser, "expected template-argument");
16336 if (cp_parser_parse_definitely (parser))
16337 return argument;
16338 /* We did our best to parse the argument as a non type-id, but that
16339 was the only alternative that matched (albeit with a '>' after
16340 it). We can assume it's just a typo from the user, and a
16341 diagnostic will then be issued. */
16342 return cp_parser_template_type_arg (parser);
16345 /* Parse an explicit-instantiation.
16347 explicit-instantiation:
16348 template declaration
16350 Although the standard says `declaration', what it really means is:
16352 explicit-instantiation:
16353 template decl-specifier-seq [opt] declarator [opt] ;
16355 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16356 supposed to be allowed. A defect report has been filed about this
16357 issue.
16359 GNU Extension:
16361 explicit-instantiation:
16362 storage-class-specifier template
16363 decl-specifier-seq [opt] declarator [opt] ;
16364 function-specifier template
16365 decl-specifier-seq [opt] declarator [opt] ; */
16367 static void
16368 cp_parser_explicit_instantiation (cp_parser* parser)
16370 int declares_class_or_enum;
16371 cp_decl_specifier_seq decl_specifiers;
16372 tree extension_specifier = NULL_TREE;
16374 timevar_push (TV_TEMPLATE_INST);
16376 /* Look for an (optional) storage-class-specifier or
16377 function-specifier. */
16378 if (cp_parser_allow_gnu_extensions_p (parser))
16380 extension_specifier
16381 = cp_parser_storage_class_specifier_opt (parser);
16382 if (!extension_specifier)
16383 extension_specifier
16384 = cp_parser_function_specifier_opt (parser,
16385 /*decl_specs=*/NULL);
16388 /* Look for the `template' keyword. */
16389 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16390 /* Let the front end know that we are processing an explicit
16391 instantiation. */
16392 begin_explicit_instantiation ();
16393 /* [temp.explicit] says that we are supposed to ignore access
16394 control while processing explicit instantiation directives. */
16395 push_deferring_access_checks (dk_no_check);
16396 /* Parse a decl-specifier-seq. */
16397 cp_parser_decl_specifier_seq (parser,
16398 CP_PARSER_FLAGS_OPTIONAL,
16399 &decl_specifiers,
16400 &declares_class_or_enum);
16401 /* If there was exactly one decl-specifier, and it declared a class,
16402 and there's no declarator, then we have an explicit type
16403 instantiation. */
16404 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16406 tree type;
16408 type = check_tag_decl (&decl_specifiers,
16409 /*explicit_type_instantiation_p=*/true);
16410 /* Turn access control back on for names used during
16411 template instantiation. */
16412 pop_deferring_access_checks ();
16413 if (type)
16414 do_type_instantiation (type, extension_specifier,
16415 /*complain=*/tf_error);
16417 else
16419 cp_declarator *declarator;
16420 tree decl;
16422 /* Parse the declarator. */
16423 declarator
16424 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16425 /*ctor_dtor_or_conv_p=*/NULL,
16426 /*parenthesized_p=*/NULL,
16427 /*member_p=*/false,
16428 /*friend_p=*/false);
16429 if (declares_class_or_enum & 2)
16430 cp_parser_check_for_definition_in_return_type (declarator,
16431 decl_specifiers.type,
16432 decl_specifiers.locations[ds_type_spec]);
16433 if (declarator != cp_error_declarator)
16435 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16436 permerror (decl_specifiers.locations[ds_inline],
16437 "explicit instantiation shall not use"
16438 " %<inline%> specifier");
16439 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16440 permerror (decl_specifiers.locations[ds_constexpr],
16441 "explicit instantiation shall not use"
16442 " %<constexpr%> specifier");
16444 decl = grokdeclarator (declarator, &decl_specifiers,
16445 NORMAL, 0, &decl_specifiers.attributes);
16446 /* Turn access control back on for names used during
16447 template instantiation. */
16448 pop_deferring_access_checks ();
16449 /* Do the explicit instantiation. */
16450 do_decl_instantiation (decl, extension_specifier);
16452 else
16454 pop_deferring_access_checks ();
16455 /* Skip the body of the explicit instantiation. */
16456 cp_parser_skip_to_end_of_statement (parser);
16459 /* We're done with the instantiation. */
16460 end_explicit_instantiation ();
16462 cp_parser_consume_semicolon_at_end_of_statement (parser);
16464 timevar_pop (TV_TEMPLATE_INST);
16467 /* Parse an explicit-specialization.
16469 explicit-specialization:
16470 template < > declaration
16472 Although the standard says `declaration', what it really means is:
16474 explicit-specialization:
16475 template <> decl-specifier [opt] init-declarator [opt] ;
16476 template <> function-definition
16477 template <> explicit-specialization
16478 template <> template-declaration */
16480 static void
16481 cp_parser_explicit_specialization (cp_parser* parser)
16483 bool need_lang_pop;
16484 cp_token *token = cp_lexer_peek_token (parser->lexer);
16486 /* Look for the `template' keyword. */
16487 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16488 /* Look for the `<'. */
16489 cp_parser_require (parser, CPP_LESS, RT_LESS);
16490 /* Look for the `>'. */
16491 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16492 /* We have processed another parameter list. */
16493 ++parser->num_template_parameter_lists;
16494 /* [temp]
16496 A template ... explicit specialization ... shall not have C
16497 linkage. */
16498 if (current_lang_name == lang_name_c)
16500 error_at (token->location, "template specialization with C linkage");
16501 maybe_show_extern_c_location ();
16502 /* Give it C++ linkage to avoid confusing other parts of the
16503 front end. */
16504 push_lang_context (lang_name_cplusplus);
16505 need_lang_pop = true;
16507 else
16508 need_lang_pop = false;
16509 /* Let the front end know that we are beginning a specialization. */
16510 if (!begin_specialization ())
16512 end_specialization ();
16513 return;
16516 /* If the next keyword is `template', we need to figure out whether
16517 or not we're looking a template-declaration. */
16518 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16520 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16521 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16522 cp_parser_template_declaration_after_export (parser,
16523 /*member_p=*/false);
16524 else
16525 cp_parser_explicit_specialization (parser);
16527 else
16528 /* Parse the dependent declaration. */
16529 cp_parser_single_declaration (parser,
16530 /*checks=*/NULL,
16531 /*member_p=*/false,
16532 /*explicit_specialization_p=*/true,
16533 /*friend_p=*/NULL);
16534 /* We're done with the specialization. */
16535 end_specialization ();
16536 /* For the erroneous case of a template with C linkage, we pushed an
16537 implicit C++ linkage scope; exit that scope now. */
16538 if (need_lang_pop)
16539 pop_lang_context ();
16540 /* We're done with this parameter list. */
16541 --parser->num_template_parameter_lists;
16544 /* Parse a type-specifier.
16546 type-specifier:
16547 simple-type-specifier
16548 class-specifier
16549 enum-specifier
16550 elaborated-type-specifier
16551 cv-qualifier
16553 GNU Extension:
16555 type-specifier:
16556 __complex__
16558 Returns a representation of the type-specifier. For a
16559 class-specifier, enum-specifier, or elaborated-type-specifier, a
16560 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16562 The parser flags FLAGS is used to control type-specifier parsing.
16564 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16565 in a decl-specifier-seq.
16567 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16568 class-specifier, enum-specifier, or elaborated-type-specifier, then
16569 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16570 if a type is declared; 2 if it is defined. Otherwise, it is set to
16571 zero.
16573 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16574 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16575 is set to FALSE. */
16577 static tree
16578 cp_parser_type_specifier (cp_parser* parser,
16579 cp_parser_flags flags,
16580 cp_decl_specifier_seq *decl_specs,
16581 bool is_declaration,
16582 int* declares_class_or_enum,
16583 bool* is_cv_qualifier)
16585 tree type_spec = NULL_TREE;
16586 cp_token *token;
16587 enum rid keyword;
16588 cp_decl_spec ds = ds_last;
16590 /* Assume this type-specifier does not declare a new type. */
16591 if (declares_class_or_enum)
16592 *declares_class_or_enum = 0;
16593 /* And that it does not specify a cv-qualifier. */
16594 if (is_cv_qualifier)
16595 *is_cv_qualifier = false;
16596 /* Peek at the next token. */
16597 token = cp_lexer_peek_token (parser->lexer);
16599 /* If we're looking at a keyword, we can use that to guide the
16600 production we choose. */
16601 keyword = token->keyword;
16602 switch (keyword)
16604 case RID_ENUM:
16605 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16606 goto elaborated_type_specifier;
16608 /* Look for the enum-specifier. */
16609 type_spec = cp_parser_enum_specifier (parser);
16610 /* If that worked, we're done. */
16611 if (type_spec)
16613 if (declares_class_or_enum)
16614 *declares_class_or_enum = 2;
16615 if (decl_specs)
16616 cp_parser_set_decl_spec_type (decl_specs,
16617 type_spec,
16618 token,
16619 /*type_definition_p=*/true);
16620 return type_spec;
16622 else
16623 goto elaborated_type_specifier;
16625 /* Any of these indicate either a class-specifier, or an
16626 elaborated-type-specifier. */
16627 case RID_CLASS:
16628 case RID_STRUCT:
16629 case RID_UNION:
16630 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16631 goto elaborated_type_specifier;
16633 /* Parse tentatively so that we can back up if we don't find a
16634 class-specifier. */
16635 cp_parser_parse_tentatively (parser);
16636 /* Look for the class-specifier. */
16637 type_spec = cp_parser_class_specifier (parser);
16638 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16639 /* If that worked, we're done. */
16640 if (cp_parser_parse_definitely (parser))
16642 if (declares_class_or_enum)
16643 *declares_class_or_enum = 2;
16644 if (decl_specs)
16645 cp_parser_set_decl_spec_type (decl_specs,
16646 type_spec,
16647 token,
16648 /*type_definition_p=*/true);
16649 return type_spec;
16652 /* Fall through. */
16653 elaborated_type_specifier:
16654 /* We're declaring (not defining) a class or enum. */
16655 if (declares_class_or_enum)
16656 *declares_class_or_enum = 1;
16658 /* Fall through. */
16659 case RID_TYPENAME:
16660 /* Look for an elaborated-type-specifier. */
16661 type_spec
16662 = (cp_parser_elaborated_type_specifier
16663 (parser,
16664 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16665 is_declaration));
16666 if (decl_specs)
16667 cp_parser_set_decl_spec_type (decl_specs,
16668 type_spec,
16669 token,
16670 /*type_definition_p=*/false);
16671 return type_spec;
16673 case RID_CONST:
16674 ds = ds_const;
16675 if (is_cv_qualifier)
16676 *is_cv_qualifier = true;
16677 break;
16679 case RID_VOLATILE:
16680 ds = ds_volatile;
16681 if (is_cv_qualifier)
16682 *is_cv_qualifier = true;
16683 break;
16685 case RID_RESTRICT:
16686 ds = ds_restrict;
16687 if (is_cv_qualifier)
16688 *is_cv_qualifier = true;
16689 break;
16691 case RID_COMPLEX:
16692 /* The `__complex__' keyword is a GNU extension. */
16693 ds = ds_complex;
16694 break;
16696 default:
16697 break;
16700 /* Handle simple keywords. */
16701 if (ds != ds_last)
16703 if (decl_specs)
16705 set_and_check_decl_spec_loc (decl_specs, ds, token);
16706 decl_specs->any_specifiers_p = true;
16708 return cp_lexer_consume_token (parser->lexer)->u.value;
16711 /* If we do not already have a type-specifier, assume we are looking
16712 at a simple-type-specifier. */
16713 type_spec = cp_parser_simple_type_specifier (parser,
16714 decl_specs,
16715 flags);
16717 /* If we didn't find a type-specifier, and a type-specifier was not
16718 optional in this context, issue an error message. */
16719 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16721 cp_parser_error (parser, "expected type specifier");
16722 return error_mark_node;
16725 return type_spec;
16728 /* Parse a simple-type-specifier.
16730 simple-type-specifier:
16731 :: [opt] nested-name-specifier [opt] type-name
16732 :: [opt] nested-name-specifier template template-id
16733 char
16734 wchar_t
16735 bool
16736 short
16738 long
16739 signed
16740 unsigned
16741 float
16742 double
16743 void
16745 C++11 Extension:
16747 simple-type-specifier:
16748 auto
16749 decltype ( expression )
16750 char16_t
16751 char32_t
16752 __underlying_type ( type-id )
16754 C++17 extension:
16756 nested-name-specifier(opt) template-name
16758 GNU Extension:
16760 simple-type-specifier:
16761 __int128
16762 __typeof__ unary-expression
16763 __typeof__ ( type-id )
16764 __typeof__ ( type-id ) { initializer-list , [opt] }
16766 Concepts Extension:
16768 simple-type-specifier:
16769 constrained-type-specifier
16771 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16772 appropriately updated. */
16774 static tree
16775 cp_parser_simple_type_specifier (cp_parser* parser,
16776 cp_decl_specifier_seq *decl_specs,
16777 cp_parser_flags flags)
16779 tree type = NULL_TREE;
16780 cp_token *token;
16781 int idx;
16783 /* Peek at the next token. */
16784 token = cp_lexer_peek_token (parser->lexer);
16786 /* If we're looking at a keyword, things are easy. */
16787 switch (token->keyword)
16789 case RID_CHAR:
16790 if (decl_specs)
16791 decl_specs->explicit_char_p = true;
16792 type = char_type_node;
16793 break;
16794 case RID_CHAR16:
16795 type = char16_type_node;
16796 break;
16797 case RID_CHAR32:
16798 type = char32_type_node;
16799 break;
16800 case RID_WCHAR:
16801 type = wchar_type_node;
16802 break;
16803 case RID_BOOL:
16804 type = boolean_type_node;
16805 break;
16806 case RID_SHORT:
16807 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16808 type = short_integer_type_node;
16809 break;
16810 case RID_INT:
16811 if (decl_specs)
16812 decl_specs->explicit_int_p = true;
16813 type = integer_type_node;
16814 break;
16815 case RID_INT_N_0:
16816 case RID_INT_N_1:
16817 case RID_INT_N_2:
16818 case RID_INT_N_3:
16819 idx = token->keyword - RID_INT_N_0;
16820 if (! int_n_enabled_p [idx])
16821 break;
16822 if (decl_specs)
16824 decl_specs->explicit_intN_p = true;
16825 decl_specs->int_n_idx = idx;
16827 type = int_n_trees [idx].signed_type;
16828 break;
16829 case RID_LONG:
16830 if (decl_specs)
16831 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16832 type = long_integer_type_node;
16833 break;
16834 case RID_SIGNED:
16835 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16836 type = integer_type_node;
16837 break;
16838 case RID_UNSIGNED:
16839 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16840 type = unsigned_type_node;
16841 break;
16842 case RID_FLOAT:
16843 type = float_type_node;
16844 break;
16845 case RID_DOUBLE:
16846 type = double_type_node;
16847 break;
16848 case RID_VOID:
16849 type = void_type_node;
16850 break;
16852 case RID_AUTO:
16853 maybe_warn_cpp0x (CPP0X_AUTO);
16854 if (parser->auto_is_implicit_function_template_parm_p)
16856 /* The 'auto' might be the placeholder return type for a function decl
16857 with trailing return type. */
16858 bool have_trailing_return_fn_decl = false;
16860 cp_parser_parse_tentatively (parser);
16861 cp_lexer_consume_token (parser->lexer);
16862 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16863 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16864 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16865 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16867 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16869 cp_lexer_consume_token (parser->lexer);
16870 cp_parser_skip_to_closing_parenthesis (parser,
16871 /*recovering*/false,
16872 /*or_comma*/false,
16873 /*consume_paren*/true);
16874 continue;
16877 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16879 have_trailing_return_fn_decl = true;
16880 break;
16883 cp_lexer_consume_token (parser->lexer);
16885 cp_parser_abort_tentative_parse (parser);
16887 if (have_trailing_return_fn_decl)
16889 type = make_auto ();
16890 break;
16893 if (cxx_dialect >= cxx14)
16895 type = synthesize_implicit_template_parm (parser, NULL_TREE);
16896 type = TREE_TYPE (type);
16898 else
16899 type = error_mark_node;
16901 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16903 if (cxx_dialect < cxx14)
16904 error_at (token->location,
16905 "use of %<auto%> in lambda parameter declaration "
16906 "only available with "
16907 "-std=c++14 or -std=gnu++14");
16909 else if (cxx_dialect < cxx14)
16910 error_at (token->location,
16911 "use of %<auto%> in parameter declaration "
16912 "only available with "
16913 "-std=c++14 or -std=gnu++14");
16914 else if (!flag_concepts)
16915 pedwarn (token->location, OPT_Wpedantic,
16916 "ISO C++ forbids use of %<auto%> in parameter "
16917 "declaration");
16919 else
16920 type = make_auto ();
16921 break;
16923 case RID_DECLTYPE:
16924 /* Since DR 743, decltype can either be a simple-type-specifier by
16925 itself or begin a nested-name-specifier. Parsing it will replace
16926 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16927 handling below decide what to do. */
16928 cp_parser_decltype (parser);
16929 cp_lexer_set_token_position (parser->lexer, token);
16930 break;
16932 case RID_TYPEOF:
16933 /* Consume the `typeof' token. */
16934 cp_lexer_consume_token (parser->lexer);
16935 /* Parse the operand to `typeof'. */
16936 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
16937 /* If it is not already a TYPE, take its type. */
16938 if (!TYPE_P (type))
16939 type = finish_typeof (type);
16941 if (decl_specs)
16942 cp_parser_set_decl_spec_type (decl_specs, type,
16943 token,
16944 /*type_definition_p=*/false);
16946 return type;
16948 case RID_UNDERLYING_TYPE:
16949 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
16950 if (decl_specs)
16951 cp_parser_set_decl_spec_type (decl_specs, type,
16952 token,
16953 /*type_definition_p=*/false);
16955 return type;
16957 case RID_BASES:
16958 case RID_DIRECT_BASES:
16959 type = cp_parser_trait_expr (parser, token->keyword);
16960 if (decl_specs)
16961 cp_parser_set_decl_spec_type (decl_specs, type,
16962 token,
16963 /*type_definition_p=*/false);
16964 return type;
16965 default:
16966 break;
16969 /* If token is an already-parsed decltype not followed by ::,
16970 it's a simple-type-specifier. */
16971 if (token->type == CPP_DECLTYPE
16972 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
16974 type = saved_checks_value (token->u.tree_check_value);
16975 if (decl_specs)
16977 cp_parser_set_decl_spec_type (decl_specs, type,
16978 token,
16979 /*type_definition_p=*/false);
16980 /* Remember that we are handling a decltype in order to
16981 implement the resolution of DR 1510 when the argument
16982 isn't instantiation dependent. */
16983 decl_specs->decltype_p = true;
16985 cp_lexer_consume_token (parser->lexer);
16986 return type;
16989 /* If the type-specifier was for a built-in type, we're done. */
16990 if (type)
16992 /* Record the type. */
16993 if (decl_specs
16994 && (token->keyword != RID_SIGNED
16995 && token->keyword != RID_UNSIGNED
16996 && token->keyword != RID_SHORT
16997 && token->keyword != RID_LONG))
16998 cp_parser_set_decl_spec_type (decl_specs,
16999 type,
17000 token,
17001 /*type_definition_p=*/false);
17002 if (decl_specs)
17003 decl_specs->any_specifiers_p = true;
17005 /* Consume the token. */
17006 cp_lexer_consume_token (parser->lexer);
17008 if (type == error_mark_node)
17009 return error_mark_node;
17011 /* There is no valid C++ program where a non-template type is
17012 followed by a "<". That usually indicates that the user thought
17013 that the type was a template. */
17014 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17015 token->location);
17017 return TYPE_NAME (type);
17020 /* The type-specifier must be a user-defined type. */
17021 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17023 bool qualified_p;
17024 bool global_p;
17026 /* Don't gobble tokens or issue error messages if this is an
17027 optional type-specifier. */
17028 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17029 cp_parser_parse_tentatively (parser);
17031 token = cp_lexer_peek_token (parser->lexer);
17033 /* Look for the optional `::' operator. */
17034 global_p
17035 = (cp_parser_global_scope_opt (parser,
17036 /*current_scope_valid_p=*/false)
17037 != NULL_TREE);
17038 /* Look for the nested-name specifier. */
17039 qualified_p
17040 = (cp_parser_nested_name_specifier_opt (parser,
17041 /*typename_keyword_p=*/false,
17042 /*check_dependency_p=*/true,
17043 /*type_p=*/false,
17044 /*is_declaration=*/false)
17045 != NULL_TREE);
17046 /* If we have seen a nested-name-specifier, and the next token
17047 is `template', then we are using the template-id production. */
17048 if (parser->scope
17049 && cp_parser_optional_template_keyword (parser))
17051 /* Look for the template-id. */
17052 type = cp_parser_template_id (parser,
17053 /*template_keyword_p=*/true,
17054 /*check_dependency_p=*/true,
17055 none_type,
17056 /*is_declaration=*/false);
17057 /* If the template-id did not name a type, we are out of
17058 luck. */
17059 if (TREE_CODE (type) != TYPE_DECL)
17061 cp_parser_error (parser, "expected template-id for type");
17062 type = NULL_TREE;
17065 /* Otherwise, look for a type-name. */
17066 else
17067 type = cp_parser_type_name (parser);
17068 /* Keep track of all name-lookups performed in class scopes. */
17069 if (type
17070 && !global_p
17071 && !qualified_p
17072 && TREE_CODE (type) == TYPE_DECL
17073 && identifier_p (DECL_NAME (type)))
17074 maybe_note_name_used_in_class (DECL_NAME (type), type);
17075 /* If it didn't work out, we don't have a TYPE. */
17076 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17077 && !cp_parser_parse_definitely (parser))
17078 type = NULL_TREE;
17079 if (!type && cxx_dialect >= cxx17)
17081 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17082 cp_parser_parse_tentatively (parser);
17084 cp_parser_global_scope_opt (parser,
17085 /*current_scope_valid_p=*/false);
17086 cp_parser_nested_name_specifier_opt (parser,
17087 /*typename_keyword_p=*/false,
17088 /*check_dependency_p=*/true,
17089 /*type_p=*/false,
17090 /*is_declaration=*/false);
17091 tree name = cp_parser_identifier (parser);
17092 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17093 && parser->scope != error_mark_node)
17095 tree tmpl = cp_parser_lookup_name (parser, name,
17096 none_type,
17097 /*is_template=*/false,
17098 /*is_namespace=*/false,
17099 /*check_dependency=*/true,
17100 /*ambiguous_decls=*/NULL,
17101 token->location);
17102 if (tmpl && tmpl != error_mark_node
17103 && (DECL_CLASS_TEMPLATE_P (tmpl)
17104 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17105 type = make_template_placeholder (tmpl);
17106 else
17108 type = error_mark_node;
17109 if (!cp_parser_simulate_error (parser))
17110 cp_parser_name_lookup_error (parser, name, tmpl,
17111 NLE_TYPE, token->location);
17114 else
17115 type = error_mark_node;
17117 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17118 && !cp_parser_parse_definitely (parser))
17119 type = NULL_TREE;
17121 if (type && decl_specs)
17122 cp_parser_set_decl_spec_type (decl_specs, type,
17123 token,
17124 /*type_definition_p=*/false);
17127 /* If we didn't get a type-name, issue an error message. */
17128 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17130 cp_parser_error (parser, "expected type-name");
17131 return error_mark_node;
17134 if (type && type != error_mark_node)
17136 /* See if TYPE is an Objective-C type, and if so, parse and
17137 accept any protocol references following it. Do this before
17138 the cp_parser_check_for_invalid_template_id() call, because
17139 Objective-C types can be followed by '<...>' which would
17140 enclose protocol names rather than template arguments, and so
17141 everything is fine. */
17142 if (c_dialect_objc () && !parser->scope
17143 && (objc_is_id (type) || objc_is_class_name (type)))
17145 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17146 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17148 /* Clobber the "unqualified" type previously entered into
17149 DECL_SPECS with the new, improved protocol-qualified version. */
17150 if (decl_specs)
17151 decl_specs->type = qual_type;
17153 return qual_type;
17156 /* There is no valid C++ program where a non-template type is
17157 followed by a "<". That usually indicates that the user
17158 thought that the type was a template. */
17159 cp_parser_check_for_invalid_template_id (parser, type,
17160 none_type,
17161 token->location);
17164 return type;
17167 /* Parse a type-name.
17169 type-name:
17170 class-name
17171 enum-name
17172 typedef-name
17173 simple-template-id [in c++0x]
17175 enum-name:
17176 identifier
17178 typedef-name:
17179 identifier
17181 Concepts:
17183 type-name:
17184 concept-name
17185 partial-concept-id
17187 concept-name:
17188 identifier
17190 Returns a TYPE_DECL for the type. */
17192 static tree
17193 cp_parser_type_name (cp_parser* parser)
17195 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17198 /* See above. */
17199 static tree
17200 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17202 tree type_decl;
17204 /* We can't know yet whether it is a class-name or not. */
17205 cp_parser_parse_tentatively (parser);
17206 /* Try a class-name. */
17207 type_decl = cp_parser_class_name (parser,
17208 typename_keyword_p,
17209 /*template_keyword_p=*/false,
17210 none_type,
17211 /*check_dependency_p=*/true,
17212 /*class_head_p=*/false,
17213 /*is_declaration=*/false);
17214 /* If it's not a class-name, keep looking. */
17215 if (!cp_parser_parse_definitely (parser))
17217 if (cxx_dialect < cxx11)
17218 /* It must be a typedef-name or an enum-name. */
17219 return cp_parser_nonclass_name (parser);
17221 cp_parser_parse_tentatively (parser);
17222 /* It is either a simple-template-id representing an
17223 instantiation of an alias template... */
17224 type_decl = cp_parser_template_id (parser,
17225 /*template_keyword_p=*/false,
17226 /*check_dependency_p=*/true,
17227 none_type,
17228 /*is_declaration=*/false);
17229 /* Note that this must be an instantiation of an alias template
17230 because [temp.names]/6 says:
17232 A template-id that names an alias template specialization
17233 is a type-name.
17235 Whereas [temp.names]/7 says:
17237 A simple-template-id that names a class template
17238 specialization is a class-name.
17240 With concepts, this could also be a partial-concept-id that
17241 declares a non-type template parameter. */
17242 if (type_decl != NULL_TREE
17243 && TREE_CODE (type_decl) == TYPE_DECL
17244 && TYPE_DECL_ALIAS_P (type_decl))
17245 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17246 else if (is_constrained_parameter (type_decl))
17247 /* Don't do anything. */ ;
17248 else
17249 cp_parser_simulate_error (parser);
17251 if (!cp_parser_parse_definitely (parser))
17252 /* ... Or a typedef-name or an enum-name. */
17253 return cp_parser_nonclass_name (parser);
17256 return type_decl;
17259 /* Check if DECL and ARGS can form a constrained-type-specifier.
17260 If ARGS is non-null, we try to form a concept check of the
17261 form DECL<?, ARGS> where ? is a wildcard that matches any
17262 kind of template argument. If ARGS is NULL, then we try to
17263 form a concept check of the form DECL<?>. */
17265 static tree
17266 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17267 tree decl, tree args)
17269 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17271 /* If we a constrained-type-specifier cannot be deduced. */
17272 if (parser->prevent_constrained_type_specifiers)
17273 return NULL_TREE;
17275 /* A constrained type specifier can only be found in an
17276 overload set or as a reference to a template declaration.
17278 FIXME: This might be masking a bug. It's possible that
17279 that the deduction below is causing template specializations
17280 to be formed with the wildcard as an argument. */
17281 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17282 return NULL_TREE;
17284 /* Try to build a call expression that evaluates the
17285 concept. This can fail if the overload set refers
17286 only to non-templates. */
17287 tree placeholder = build_nt (WILDCARD_DECL);
17288 tree check = build_concept_check (decl, placeholder, args);
17289 if (check == error_mark_node)
17290 return NULL_TREE;
17292 /* Deduce the checked constraint and the prototype parameter.
17294 FIXME: In certain cases, failure to deduce should be a
17295 diagnosable error. */
17296 tree conc;
17297 tree proto;
17298 if (!deduce_constrained_parameter (check, conc, proto))
17299 return NULL_TREE;
17301 /* In template parameter scope, this results in a constrained
17302 parameter. Return a descriptor of that parm. */
17303 if (processing_template_parmlist)
17304 return build_constrained_parameter (conc, proto, args);
17306 /* In a parameter-declaration-clause, constrained-type
17307 specifiers result in invented template parameters. */
17308 if (parser->auto_is_implicit_function_template_parm_p)
17310 tree x = build_constrained_parameter (conc, proto, args);
17311 return synthesize_implicit_template_parm (parser, x);
17313 else
17315 /* Otherwise, we're in a context where the constrained
17316 type name is deduced and the constraint applies
17317 after deduction. */
17318 return make_constrained_auto (conc, args);
17321 return NULL_TREE;
17324 /* If DECL refers to a concept, return a TYPE_DECL representing
17325 the result of using the constrained type specifier in the
17326 current context. DECL refers to a concept if
17328 - it is an overload set containing a function concept taking a single
17329 type argument, or
17331 - it is a variable concept taking a single type argument. */
17333 static tree
17334 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17336 if (flag_concepts
17337 && (TREE_CODE (decl) == OVERLOAD
17338 || BASELINK_P (decl)
17339 || variable_concept_p (decl)))
17340 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17341 else
17342 return NULL_TREE;
17345 /* Check if DECL and ARGS form a partial-concept-id. If so,
17346 assign ID to the resulting constrained placeholder.
17348 Returns true if the partial-concept-id designates a placeholder
17349 and false otherwise. Note that *id is set to NULL_TREE in
17350 this case. */
17352 static tree
17353 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17355 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17358 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17359 or a concept-name.
17361 enum-name:
17362 identifier
17364 typedef-name:
17365 identifier
17367 concept-name:
17368 identifier
17370 Returns a TYPE_DECL for the type. */
17372 static tree
17373 cp_parser_nonclass_name (cp_parser* parser)
17375 tree type_decl;
17376 tree identifier;
17378 cp_token *token = cp_lexer_peek_token (parser->lexer);
17379 identifier = cp_parser_identifier (parser);
17380 if (identifier == error_mark_node)
17381 return error_mark_node;
17383 /* Look up the type-name. */
17384 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17386 type_decl = strip_using_decl (type_decl);
17388 /* If we found an overload set, then it may refer to a concept-name. */
17389 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17390 type_decl = decl;
17392 if (TREE_CODE (type_decl) != TYPE_DECL
17393 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17395 /* See if this is an Objective-C type. */
17396 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17397 tree type = objc_get_protocol_qualified_type (identifier, protos);
17398 if (type)
17399 type_decl = TYPE_NAME (type);
17402 /* Issue an error if we did not find a type-name. */
17403 if (TREE_CODE (type_decl) != TYPE_DECL
17404 /* In Objective-C, we have the complication that class names are
17405 normally type names and start declarations (eg, the
17406 "NSObject" in "NSObject *object;"), but can be used in an
17407 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17408 is an expression. So, a classname followed by a dot is not a
17409 valid type-name. */
17410 || (objc_is_class_name (TREE_TYPE (type_decl))
17411 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17413 if (!cp_parser_simulate_error (parser))
17414 cp_parser_name_lookup_error (parser, identifier, type_decl,
17415 NLE_TYPE, token->location);
17416 return error_mark_node;
17418 /* Remember that the name was used in the definition of the
17419 current class so that we can check later to see if the
17420 meaning would have been different after the class was
17421 entirely defined. */
17422 else if (type_decl != error_mark_node
17423 && !parser->scope)
17424 maybe_note_name_used_in_class (identifier, type_decl);
17426 return type_decl;
17429 /* Parse an elaborated-type-specifier. Note that the grammar given
17430 here incorporates the resolution to DR68.
17432 elaborated-type-specifier:
17433 class-key :: [opt] nested-name-specifier [opt] identifier
17434 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17435 enum-key :: [opt] nested-name-specifier [opt] identifier
17436 typename :: [opt] nested-name-specifier identifier
17437 typename :: [opt] nested-name-specifier template [opt]
17438 template-id
17440 GNU extension:
17442 elaborated-type-specifier:
17443 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17444 class-key attributes :: [opt] nested-name-specifier [opt]
17445 template [opt] template-id
17446 enum attributes :: [opt] nested-name-specifier [opt] identifier
17448 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17449 declared `friend'. If IS_DECLARATION is TRUE, then this
17450 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17451 something is being declared.
17453 Returns the TYPE specified. */
17455 static tree
17456 cp_parser_elaborated_type_specifier (cp_parser* parser,
17457 bool is_friend,
17458 bool is_declaration)
17460 enum tag_types tag_type;
17461 tree identifier;
17462 tree type = NULL_TREE;
17463 tree attributes = NULL_TREE;
17464 tree globalscope;
17465 cp_token *token = NULL;
17467 /* See if we're looking at the `enum' keyword. */
17468 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17470 /* Consume the `enum' token. */
17471 cp_lexer_consume_token (parser->lexer);
17472 /* Remember that it's an enumeration type. */
17473 tag_type = enum_type;
17474 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17475 enums) is used here. */
17476 cp_token *token = cp_lexer_peek_token (parser->lexer);
17477 if (cp_parser_is_keyword (token, RID_CLASS)
17478 || cp_parser_is_keyword (token, RID_STRUCT))
17480 gcc_rich_location richloc (token->location);
17481 richloc.add_range (input_location, false);
17482 richloc.add_fixit_remove ();
17483 pedwarn (&richloc, 0, "elaborated-type-specifier for "
17484 "a scoped enum must not use the %qD keyword",
17485 token->u.value);
17486 /* Consume the `struct' or `class' and parse it anyway. */
17487 cp_lexer_consume_token (parser->lexer);
17489 /* Parse the attributes. */
17490 attributes = cp_parser_attributes_opt (parser);
17492 /* Or, it might be `typename'. */
17493 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17494 RID_TYPENAME))
17496 /* Consume the `typename' token. */
17497 cp_lexer_consume_token (parser->lexer);
17498 /* Remember that it's a `typename' type. */
17499 tag_type = typename_type;
17501 /* Otherwise it must be a class-key. */
17502 else
17504 tag_type = cp_parser_class_key (parser);
17505 if (tag_type == none_type)
17506 return error_mark_node;
17507 /* Parse the attributes. */
17508 attributes = cp_parser_attributes_opt (parser);
17511 /* Look for the `::' operator. */
17512 globalscope = cp_parser_global_scope_opt (parser,
17513 /*current_scope_valid_p=*/false);
17514 /* Look for the nested-name-specifier. */
17515 tree nested_name_specifier;
17516 if (tag_type == typename_type && !globalscope)
17518 nested_name_specifier
17519 = cp_parser_nested_name_specifier (parser,
17520 /*typename_keyword_p=*/true,
17521 /*check_dependency_p=*/true,
17522 /*type_p=*/true,
17523 is_declaration);
17524 if (!nested_name_specifier)
17525 return error_mark_node;
17527 else
17528 /* Even though `typename' is not present, the proposed resolution
17529 to Core Issue 180 says that in `class A<T>::B', `B' should be
17530 considered a type-name, even if `A<T>' is dependent. */
17531 nested_name_specifier
17532 = cp_parser_nested_name_specifier_opt (parser,
17533 /*typename_keyword_p=*/true,
17534 /*check_dependency_p=*/true,
17535 /*type_p=*/true,
17536 is_declaration);
17537 /* For everything but enumeration types, consider a template-id.
17538 For an enumeration type, consider only a plain identifier. */
17539 if (tag_type != enum_type)
17541 bool template_p = false;
17542 tree decl;
17544 /* Allow the `template' keyword. */
17545 template_p = cp_parser_optional_template_keyword (parser);
17546 /* If we didn't see `template', we don't know if there's a
17547 template-id or not. */
17548 if (!template_p)
17549 cp_parser_parse_tentatively (parser);
17550 /* Parse the template-id. */
17551 token = cp_lexer_peek_token (parser->lexer);
17552 decl = cp_parser_template_id (parser, template_p,
17553 /*check_dependency_p=*/true,
17554 tag_type,
17555 is_declaration);
17556 /* If we didn't find a template-id, look for an ordinary
17557 identifier. */
17558 if (!template_p && !cp_parser_parse_definitely (parser))
17560 /* We can get here when cp_parser_template_id, called by
17561 cp_parser_class_name with tag_type == none_type, succeeds
17562 and caches a BASELINK. Then, when called again here,
17563 instead of failing and returning an error_mark_node
17564 returns it (see template/typename17.C in C++11).
17565 ??? Could we diagnose this earlier? */
17566 else if (tag_type == typename_type && BASELINK_P (decl))
17568 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17569 type = error_mark_node;
17571 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17572 in effect, then we must assume that, upon instantiation, the
17573 template will correspond to a class. */
17574 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17575 && tag_type == typename_type)
17576 type = make_typename_type (parser->scope, decl,
17577 typename_type,
17578 /*complain=*/tf_error);
17579 /* If the `typename' keyword is in effect and DECL is not a type
17580 decl, then type is non existent. */
17581 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17583 else if (TREE_CODE (decl) == TYPE_DECL)
17585 type = check_elaborated_type_specifier (tag_type, decl,
17586 /*allow_template_p=*/true);
17588 /* If the next token is a semicolon, this must be a specialization,
17589 instantiation, or friend declaration. Check the scope while we
17590 still know whether or not we had a nested-name-specifier. */
17591 if (type != error_mark_node
17592 && !nested_name_specifier && !is_friend
17593 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17594 check_unqualified_spec_or_inst (type, token->location);
17596 else if (decl == error_mark_node)
17597 type = error_mark_node;
17600 if (!type)
17602 token = cp_lexer_peek_token (parser->lexer);
17603 identifier = cp_parser_identifier (parser);
17605 if (identifier == error_mark_node)
17607 parser->scope = NULL_TREE;
17608 return error_mark_node;
17611 /* For a `typename', we needn't call xref_tag. */
17612 if (tag_type == typename_type
17613 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17614 return cp_parser_make_typename_type (parser, identifier,
17615 token->location);
17617 /* Template parameter lists apply only if we are not within a
17618 function parameter list. */
17619 bool template_parm_lists_apply
17620 = parser->num_template_parameter_lists;
17621 if (template_parm_lists_apply)
17622 for (cp_binding_level *s = current_binding_level;
17623 s && s->kind != sk_template_parms;
17624 s = s->level_chain)
17625 if (s->kind == sk_function_parms)
17626 template_parm_lists_apply = false;
17628 /* Look up a qualified name in the usual way. */
17629 if (parser->scope)
17631 tree decl;
17632 tree ambiguous_decls;
17634 decl = cp_parser_lookup_name (parser, identifier,
17635 tag_type,
17636 /*is_template=*/false,
17637 /*is_namespace=*/false,
17638 /*check_dependency=*/true,
17639 &ambiguous_decls,
17640 token->location);
17642 /* If the lookup was ambiguous, an error will already have been
17643 issued. */
17644 if (ambiguous_decls)
17645 return error_mark_node;
17647 /* If we are parsing friend declaration, DECL may be a
17648 TEMPLATE_DECL tree node here. However, we need to check
17649 whether this TEMPLATE_DECL results in valid code. Consider
17650 the following example:
17652 namespace N {
17653 template <class T> class C {};
17655 class X {
17656 template <class T> friend class N::C; // #1, valid code
17658 template <class T> class Y {
17659 friend class N::C; // #2, invalid code
17662 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17663 name lookup of `N::C'. We see that friend declaration must
17664 be template for the code to be valid. Note that
17665 processing_template_decl does not work here since it is
17666 always 1 for the above two cases. */
17668 decl = (cp_parser_maybe_treat_template_as_class
17669 (decl, /*tag_name_p=*/is_friend
17670 && template_parm_lists_apply));
17672 if (TREE_CODE (decl) != TYPE_DECL)
17674 cp_parser_diagnose_invalid_type_name (parser,
17675 identifier,
17676 token->location);
17677 return error_mark_node;
17680 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17682 bool allow_template = (template_parm_lists_apply
17683 || DECL_SELF_REFERENCE_P (decl));
17684 type = check_elaborated_type_specifier (tag_type, decl,
17685 allow_template);
17687 if (type == error_mark_node)
17688 return error_mark_node;
17691 /* Forward declarations of nested types, such as
17693 class C1::C2;
17694 class C1::C2::C3;
17696 are invalid unless all components preceding the final '::'
17697 are complete. If all enclosing types are complete, these
17698 declarations become merely pointless.
17700 Invalid forward declarations of nested types are errors
17701 caught elsewhere in parsing. Those that are pointless arrive
17702 here. */
17704 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17705 && !is_friend && !processing_explicit_instantiation)
17706 warning (0, "declaration %qD does not declare anything", decl);
17708 type = TREE_TYPE (decl);
17710 else
17712 /* An elaborated-type-specifier sometimes introduces a new type and
17713 sometimes names an existing type. Normally, the rule is that it
17714 introduces a new type only if there is not an existing type of
17715 the same name already in scope. For example, given:
17717 struct S {};
17718 void f() { struct S s; }
17720 the `struct S' in the body of `f' is the same `struct S' as in
17721 the global scope; the existing definition is used. However, if
17722 there were no global declaration, this would introduce a new
17723 local class named `S'.
17725 An exception to this rule applies to the following code:
17727 namespace N { struct S; }
17729 Here, the elaborated-type-specifier names a new type
17730 unconditionally; even if there is already an `S' in the
17731 containing scope this declaration names a new type.
17732 This exception only applies if the elaborated-type-specifier
17733 forms the complete declaration:
17735 [class.name]
17737 A declaration consisting solely of `class-key identifier ;' is
17738 either a redeclaration of the name in the current scope or a
17739 forward declaration of the identifier as a class name. It
17740 introduces the name into the current scope.
17742 We are in this situation precisely when the next token is a `;'.
17744 An exception to the exception is that a `friend' declaration does
17745 *not* name a new type; i.e., given:
17747 struct S { friend struct T; };
17749 `T' is not a new type in the scope of `S'.
17751 Also, `new struct S' or `sizeof (struct S)' never results in the
17752 definition of a new type; a new type can only be declared in a
17753 declaration context. */
17755 tag_scope ts;
17756 bool template_p;
17758 if (is_friend)
17759 /* Friends have special name lookup rules. */
17760 ts = ts_within_enclosing_non_class;
17761 else if (is_declaration
17762 && cp_lexer_next_token_is (parser->lexer,
17763 CPP_SEMICOLON))
17764 /* This is a `class-key identifier ;' */
17765 ts = ts_current;
17766 else
17767 ts = ts_global;
17769 template_p =
17770 (template_parm_lists_apply
17771 && (cp_parser_next_token_starts_class_definition_p (parser)
17772 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17773 /* An unqualified name was used to reference this type, so
17774 there were no qualifying templates. */
17775 if (template_parm_lists_apply
17776 && !cp_parser_check_template_parameters (parser,
17777 /*num_templates=*/0,
17778 token->location,
17779 /*declarator=*/NULL))
17780 return error_mark_node;
17781 type = xref_tag (tag_type, identifier, ts, template_p);
17785 if (type == error_mark_node)
17786 return error_mark_node;
17788 /* Allow attributes on forward declarations of classes. */
17789 if (attributes)
17791 if (TREE_CODE (type) == TYPENAME_TYPE)
17792 warning (OPT_Wattributes,
17793 "attributes ignored on uninstantiated type");
17794 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17795 && ! processing_explicit_instantiation)
17796 warning (OPT_Wattributes,
17797 "attributes ignored on template instantiation");
17798 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17799 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17800 else
17801 warning (OPT_Wattributes,
17802 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17805 if (tag_type != enum_type)
17807 /* Indicate whether this class was declared as a `class' or as a
17808 `struct'. */
17809 if (CLASS_TYPE_P (type))
17810 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17811 cp_parser_check_class_key (tag_type, type);
17814 /* A "<" cannot follow an elaborated type specifier. If that
17815 happens, the user was probably trying to form a template-id. */
17816 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17817 token->location);
17819 return type;
17822 /* Parse an enum-specifier.
17824 enum-specifier:
17825 enum-head { enumerator-list [opt] }
17826 enum-head { enumerator-list , } [C++0x]
17828 enum-head:
17829 enum-key identifier [opt] enum-base [opt]
17830 enum-key nested-name-specifier identifier enum-base [opt]
17832 enum-key:
17833 enum
17834 enum class [C++0x]
17835 enum struct [C++0x]
17837 enum-base: [C++0x]
17838 : type-specifier-seq
17840 opaque-enum-specifier:
17841 enum-key identifier enum-base [opt] ;
17843 GNU Extensions:
17844 enum-key attributes[opt] identifier [opt] enum-base [opt]
17845 { enumerator-list [opt] }attributes[opt]
17846 enum-key attributes[opt] identifier [opt] enum-base [opt]
17847 { enumerator-list, }attributes[opt] [C++0x]
17849 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17850 if the token stream isn't an enum-specifier after all. */
17852 static tree
17853 cp_parser_enum_specifier (cp_parser* parser)
17855 tree identifier;
17856 tree type = NULL_TREE;
17857 tree prev_scope;
17858 tree nested_name_specifier = NULL_TREE;
17859 tree attributes;
17860 bool scoped_enum_p = false;
17861 bool has_underlying_type = false;
17862 bool nested_being_defined = false;
17863 bool new_value_list = false;
17864 bool is_new_type = false;
17865 bool is_unnamed = false;
17866 tree underlying_type = NULL_TREE;
17867 cp_token *type_start_token = NULL;
17868 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17870 parser->colon_corrects_to_scope_p = false;
17872 /* Parse tentatively so that we can back up if we don't find a
17873 enum-specifier. */
17874 cp_parser_parse_tentatively (parser);
17876 /* Caller guarantees that the current token is 'enum', an identifier
17877 possibly follows, and the token after that is an opening brace.
17878 If we don't have an identifier, fabricate an anonymous name for
17879 the enumeration being defined. */
17880 cp_lexer_consume_token (parser->lexer);
17882 /* Parse the "class" or "struct", which indicates a scoped
17883 enumeration type in C++0x. */
17884 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17885 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17887 if (cxx_dialect < cxx11)
17888 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17890 /* Consume the `struct' or `class' token. */
17891 cp_lexer_consume_token (parser->lexer);
17893 scoped_enum_p = true;
17896 attributes = cp_parser_attributes_opt (parser);
17898 /* Clear the qualification. */
17899 parser->scope = NULL_TREE;
17900 parser->qualifying_scope = NULL_TREE;
17901 parser->object_scope = NULL_TREE;
17903 /* Figure out in what scope the declaration is being placed. */
17904 prev_scope = current_scope ();
17906 type_start_token = cp_lexer_peek_token (parser->lexer);
17908 push_deferring_access_checks (dk_no_check);
17909 nested_name_specifier
17910 = cp_parser_nested_name_specifier_opt (parser,
17911 /*typename_keyword_p=*/true,
17912 /*check_dependency_p=*/false,
17913 /*type_p=*/false,
17914 /*is_declaration=*/false);
17916 if (nested_name_specifier)
17918 tree name;
17920 identifier = cp_parser_identifier (parser);
17921 name = cp_parser_lookup_name (parser, identifier,
17922 enum_type,
17923 /*is_template=*/false,
17924 /*is_namespace=*/false,
17925 /*check_dependency=*/true,
17926 /*ambiguous_decls=*/NULL,
17927 input_location);
17928 if (name && name != error_mark_node)
17930 type = TREE_TYPE (name);
17931 if (TREE_CODE (type) == TYPENAME_TYPE)
17933 /* Are template enums allowed in ISO? */
17934 if (template_parm_scope_p ())
17935 pedwarn (type_start_token->location, OPT_Wpedantic,
17936 "%qD is an enumeration template", name);
17937 /* ignore a typename reference, for it will be solved by name
17938 in start_enum. */
17939 type = NULL_TREE;
17942 else if (nested_name_specifier == error_mark_node)
17943 /* We already issued an error. */;
17944 else
17946 error_at (type_start_token->location,
17947 "%qD does not name an enumeration in %qT",
17948 identifier, nested_name_specifier);
17949 nested_name_specifier = error_mark_node;
17952 else
17954 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17955 identifier = cp_parser_identifier (parser);
17956 else
17958 identifier = make_anon_name ();
17959 is_unnamed = true;
17960 if (scoped_enum_p)
17961 error_at (type_start_token->location,
17962 "unnamed scoped enum is not allowed");
17965 pop_deferring_access_checks ();
17967 /* Check for the `:' that denotes a specified underlying type in C++0x.
17968 Note that a ':' could also indicate a bitfield width, however. */
17969 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17971 cp_decl_specifier_seq type_specifiers;
17973 /* Consume the `:'. */
17974 cp_lexer_consume_token (parser->lexer);
17976 /* Parse the type-specifier-seq. */
17977 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17978 /*is_trailing_return=*/false,
17979 &type_specifiers);
17981 /* At this point this is surely not elaborated type specifier. */
17982 if (!cp_parser_parse_definitely (parser))
17983 return NULL_TREE;
17985 if (cxx_dialect < cxx11)
17986 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17988 has_underlying_type = true;
17990 /* If that didn't work, stop. */
17991 if (type_specifiers.type != error_mark_node)
17993 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
17994 /*initialized=*/0, NULL);
17995 if (underlying_type == error_mark_node
17996 || check_for_bare_parameter_packs (underlying_type))
17997 underlying_type = NULL_TREE;
18001 /* Look for the `{' but don't consume it yet. */
18002 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18004 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18006 cp_parser_error (parser, "expected %<{%>");
18007 if (has_underlying_type)
18009 type = NULL_TREE;
18010 goto out;
18013 /* An opaque-enum-specifier must have a ';' here. */
18014 if ((scoped_enum_p || underlying_type)
18015 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18017 cp_parser_error (parser, "expected %<;%> or %<{%>");
18018 if (has_underlying_type)
18020 type = NULL_TREE;
18021 goto out;
18026 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18027 return NULL_TREE;
18029 if (nested_name_specifier)
18031 if (CLASS_TYPE_P (nested_name_specifier))
18033 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18034 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18035 push_scope (nested_name_specifier);
18037 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18039 push_nested_namespace (nested_name_specifier);
18043 /* Issue an error message if type-definitions are forbidden here. */
18044 if (!cp_parser_check_type_definition (parser))
18045 type = error_mark_node;
18046 else
18047 /* Create the new type. We do this before consuming the opening
18048 brace so the enum will be recorded as being on the line of its
18049 tag (or the 'enum' keyword, if there is no tag). */
18050 type = start_enum (identifier, type, underlying_type,
18051 attributes, scoped_enum_p, &is_new_type);
18053 /* If the next token is not '{' it is an opaque-enum-specifier or an
18054 elaborated-type-specifier. */
18055 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18057 timevar_push (TV_PARSE_ENUM);
18058 if (nested_name_specifier
18059 && nested_name_specifier != error_mark_node)
18061 /* The following catches invalid code such as:
18062 enum class S<int>::E { A, B, C }; */
18063 if (!processing_specialization
18064 && CLASS_TYPE_P (nested_name_specifier)
18065 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18066 error_at (type_start_token->location, "cannot add an enumerator "
18067 "list to a template instantiation");
18069 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18071 error_at (type_start_token->location,
18072 "%<%T::%E%> has not been declared",
18073 TYPE_CONTEXT (nested_name_specifier),
18074 nested_name_specifier);
18075 type = error_mark_node;
18077 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18078 && !CLASS_TYPE_P (nested_name_specifier))
18080 error_at (type_start_token->location, "nested name specifier "
18081 "%qT for enum declaration does not name a class "
18082 "or namespace", nested_name_specifier);
18083 type = error_mark_node;
18085 /* If that scope does not contain the scope in which the
18086 class was originally declared, the program is invalid. */
18087 else if (prev_scope && !is_ancestor (prev_scope,
18088 nested_name_specifier))
18090 if (at_namespace_scope_p ())
18091 error_at (type_start_token->location,
18092 "declaration of %qD in namespace %qD which does not "
18093 "enclose %qD",
18094 type, prev_scope, nested_name_specifier);
18095 else
18096 error_at (type_start_token->location,
18097 "declaration of %qD in %qD which does not "
18098 "enclose %qD",
18099 type, prev_scope, nested_name_specifier);
18100 type = error_mark_node;
18102 /* If that scope is the scope where the declaration is being placed
18103 the program is invalid. */
18104 else if (CLASS_TYPE_P (nested_name_specifier)
18105 && CLASS_TYPE_P (prev_scope)
18106 && same_type_p (nested_name_specifier, prev_scope))
18108 permerror (type_start_token->location,
18109 "extra qualification not allowed");
18110 nested_name_specifier = NULL_TREE;
18114 if (scoped_enum_p)
18115 begin_scope (sk_scoped_enum, type);
18117 /* Consume the opening brace. */
18118 matching_braces braces;
18119 braces.consume_open (parser);
18121 if (type == error_mark_node)
18122 ; /* Nothing to add */
18123 else if (OPAQUE_ENUM_P (type)
18124 || (cxx_dialect > cxx98 && processing_specialization))
18126 new_value_list = true;
18127 SET_OPAQUE_ENUM_P (type, false);
18128 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18130 else
18132 error_at (type_start_token->location,
18133 "multiple definition of %q#T", type);
18134 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18135 "previous definition here");
18136 type = error_mark_node;
18139 if (type == error_mark_node)
18140 cp_parser_skip_to_end_of_block_or_statement (parser);
18141 /* If the next token is not '}', then there are some enumerators. */
18142 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18144 if (is_unnamed && !scoped_enum_p)
18145 pedwarn (type_start_token->location, OPT_Wpedantic,
18146 "ISO C++ forbids empty unnamed enum");
18148 else
18149 cp_parser_enumerator_list (parser, type);
18151 /* Consume the final '}'. */
18152 braces.require_close (parser);
18154 if (scoped_enum_p)
18155 finish_scope ();
18156 timevar_pop (TV_PARSE_ENUM);
18158 else
18160 /* If a ';' follows, then it is an opaque-enum-specifier
18161 and additional restrictions apply. */
18162 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18164 if (is_unnamed)
18165 error_at (type_start_token->location,
18166 "opaque-enum-specifier without name");
18167 else if (nested_name_specifier)
18168 error_at (type_start_token->location,
18169 "opaque-enum-specifier must use a simple identifier");
18173 /* Look for trailing attributes to apply to this enumeration, and
18174 apply them if appropriate. */
18175 if (cp_parser_allow_gnu_extensions_p (parser))
18177 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18178 cplus_decl_attributes (&type,
18179 trailing_attr,
18180 (int) ATTR_FLAG_TYPE_IN_PLACE);
18183 /* Finish up the enumeration. */
18184 if (type != error_mark_node)
18186 if (new_value_list)
18187 finish_enum_value_list (type);
18188 if (is_new_type)
18189 finish_enum (type);
18192 if (nested_name_specifier)
18194 if (CLASS_TYPE_P (nested_name_specifier))
18196 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18197 pop_scope (nested_name_specifier);
18199 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18201 pop_nested_namespace (nested_name_specifier);
18204 out:
18205 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18206 return type;
18209 /* Parse an enumerator-list. The enumerators all have the indicated
18210 TYPE.
18212 enumerator-list:
18213 enumerator-definition
18214 enumerator-list , enumerator-definition */
18216 static void
18217 cp_parser_enumerator_list (cp_parser* parser, tree type)
18219 while (true)
18221 /* Parse an enumerator-definition. */
18222 cp_parser_enumerator_definition (parser, type);
18224 /* If the next token is not a ',', we've reached the end of
18225 the list. */
18226 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18227 break;
18228 /* Otherwise, consume the `,' and keep going. */
18229 cp_lexer_consume_token (parser->lexer);
18230 /* If the next token is a `}', there is a trailing comma. */
18231 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18233 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18234 pedwarn (input_location, OPT_Wpedantic,
18235 "comma at end of enumerator list");
18236 break;
18241 /* Parse an enumerator-definition. The enumerator has the indicated
18242 TYPE.
18244 enumerator-definition:
18245 enumerator
18246 enumerator = constant-expression
18248 enumerator:
18249 identifier
18251 GNU Extensions:
18253 enumerator-definition:
18254 enumerator attributes [opt]
18255 enumerator attributes [opt] = constant-expression */
18257 static void
18258 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18260 tree identifier;
18261 tree value;
18262 location_t loc;
18264 /* Save the input location because we are interested in the location
18265 of the identifier and not the location of the explicit value. */
18266 loc = cp_lexer_peek_token (parser->lexer)->location;
18268 /* Look for the identifier. */
18269 identifier = cp_parser_identifier (parser);
18270 if (identifier == error_mark_node)
18271 return;
18273 /* Parse any specified attributes. */
18274 tree attrs = cp_parser_attributes_opt (parser);
18276 /* If the next token is an '=', then there is an explicit value. */
18277 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18279 /* Consume the `=' token. */
18280 cp_lexer_consume_token (parser->lexer);
18281 /* Parse the value. */
18282 value = cp_parser_constant_expression (parser);
18284 else
18285 value = NULL_TREE;
18287 /* If we are processing a template, make sure the initializer of the
18288 enumerator doesn't contain any bare template parameter pack. */
18289 if (check_for_bare_parameter_packs (value))
18290 value = error_mark_node;
18292 /* Create the enumerator. */
18293 build_enumerator (identifier, value, type, attrs, loc);
18296 /* Parse a namespace-name.
18298 namespace-name:
18299 original-namespace-name
18300 namespace-alias
18302 Returns the NAMESPACE_DECL for the namespace. */
18304 static tree
18305 cp_parser_namespace_name (cp_parser* parser)
18307 tree identifier;
18308 tree namespace_decl;
18310 cp_token *token = cp_lexer_peek_token (parser->lexer);
18312 /* Get the name of the namespace. */
18313 identifier = cp_parser_identifier (parser);
18314 if (identifier == error_mark_node)
18315 return error_mark_node;
18317 /* Look up the identifier in the currently active scope. Look only
18318 for namespaces, due to:
18320 [basic.lookup.udir]
18322 When looking up a namespace-name in a using-directive or alias
18323 definition, only namespace names are considered.
18325 And:
18327 [basic.lookup.qual]
18329 During the lookup of a name preceding the :: scope resolution
18330 operator, object, function, and enumerator names are ignored.
18332 (Note that cp_parser_qualifying_entity only calls this
18333 function if the token after the name is the scope resolution
18334 operator.) */
18335 namespace_decl = cp_parser_lookup_name (parser, identifier,
18336 none_type,
18337 /*is_template=*/false,
18338 /*is_namespace=*/true,
18339 /*check_dependency=*/true,
18340 /*ambiguous_decls=*/NULL,
18341 token->location);
18342 /* If it's not a namespace, issue an error. */
18343 if (namespace_decl == error_mark_node
18344 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18346 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18347 error_at (token->location, "%qD is not a namespace-name", identifier);
18348 cp_parser_error (parser, "expected namespace-name");
18349 namespace_decl = error_mark_node;
18352 return namespace_decl;
18355 /* Parse a namespace-definition.
18357 namespace-definition:
18358 named-namespace-definition
18359 unnamed-namespace-definition
18361 named-namespace-definition:
18362 original-namespace-definition
18363 extension-namespace-definition
18365 original-namespace-definition:
18366 namespace identifier { namespace-body }
18368 extension-namespace-definition:
18369 namespace original-namespace-name { namespace-body }
18371 unnamed-namespace-definition:
18372 namespace { namespace-body } */
18374 static void
18375 cp_parser_namespace_definition (cp_parser* parser)
18377 tree identifier;
18378 int nested_definition_count = 0;
18380 cp_ensure_no_omp_declare_simd (parser);
18381 cp_ensure_no_oacc_routine (parser);
18383 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18385 if (is_inline)
18387 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18388 cp_lexer_consume_token (parser->lexer);
18391 /* Look for the `namespace' keyword. */
18392 cp_token* token
18393 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18395 /* Parse any specified attributes before the identifier. */
18396 tree attribs = cp_parser_attributes_opt (parser);
18398 for (;;)
18400 identifier = NULL_TREE;
18402 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18404 identifier = cp_parser_identifier (parser);
18406 /* Parse any attributes specified after the identifier. */
18407 attribs = chainon (attribs, cp_parser_attributes_opt (parser));
18410 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18411 break;
18413 if (!nested_definition_count && cxx_dialect < cxx17)
18414 pedwarn (input_location, OPT_Wpedantic,
18415 "nested namespace definitions only available with "
18416 "-std=c++17 or -std=gnu++17");
18418 /* Nested namespace names can create new namespaces (unlike
18419 other qualified-ids). */
18420 if (int count = identifier ? push_namespace (identifier) : 0)
18421 nested_definition_count += count;
18422 else
18423 cp_parser_error (parser, "nested namespace name required");
18424 cp_lexer_consume_token (parser->lexer);
18427 if (nested_definition_count && !identifier)
18428 cp_parser_error (parser, "namespace name required");
18430 if (nested_definition_count && attribs)
18431 error_at (token->location,
18432 "a nested namespace definition cannot have attributes");
18433 if (nested_definition_count && is_inline)
18434 error_at (token->location,
18435 "a nested namespace definition cannot be inline");
18437 /* Start the namespace. */
18438 nested_definition_count += push_namespace (identifier, is_inline);
18440 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18442 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18444 /* Look for the `{' to validate starting the namespace. */
18445 matching_braces braces;
18446 if (braces.require_open (parser))
18448 /* Parse the body of the namespace. */
18449 cp_parser_namespace_body (parser);
18451 /* Look for the final `}'. */
18452 braces.require_close (parser);
18455 if (has_visibility)
18456 pop_visibility (1);
18458 /* Pop the nested namespace definitions. */
18459 while (nested_definition_count--)
18460 pop_namespace ();
18463 /* Parse a namespace-body.
18465 namespace-body:
18466 declaration-seq [opt] */
18468 static void
18469 cp_parser_namespace_body (cp_parser* parser)
18471 cp_parser_declaration_seq_opt (parser);
18474 /* Parse a namespace-alias-definition.
18476 namespace-alias-definition:
18477 namespace identifier = qualified-namespace-specifier ; */
18479 static void
18480 cp_parser_namespace_alias_definition (cp_parser* parser)
18482 tree identifier;
18483 tree namespace_specifier;
18485 cp_token *token = cp_lexer_peek_token (parser->lexer);
18487 /* Look for the `namespace' keyword. */
18488 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18489 /* Look for the identifier. */
18490 identifier = cp_parser_identifier (parser);
18491 if (identifier == error_mark_node)
18492 return;
18493 /* Look for the `=' token. */
18494 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18495 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18497 error_at (token->location, "%<namespace%> definition is not allowed here");
18498 /* Skip the definition. */
18499 cp_lexer_consume_token (parser->lexer);
18500 if (cp_parser_skip_to_closing_brace (parser))
18501 cp_lexer_consume_token (parser->lexer);
18502 return;
18504 cp_parser_require (parser, CPP_EQ, RT_EQ);
18505 /* Look for the qualified-namespace-specifier. */
18506 namespace_specifier
18507 = cp_parser_qualified_namespace_specifier (parser);
18508 /* Look for the `;' token. */
18509 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18511 /* Register the alias in the symbol table. */
18512 do_namespace_alias (identifier, namespace_specifier);
18515 /* Parse a qualified-namespace-specifier.
18517 qualified-namespace-specifier:
18518 :: [opt] nested-name-specifier [opt] namespace-name
18520 Returns a NAMESPACE_DECL corresponding to the specified
18521 namespace. */
18523 static tree
18524 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18526 /* Look for the optional `::'. */
18527 cp_parser_global_scope_opt (parser,
18528 /*current_scope_valid_p=*/false);
18530 /* Look for the optional nested-name-specifier. */
18531 cp_parser_nested_name_specifier_opt (parser,
18532 /*typename_keyword_p=*/false,
18533 /*check_dependency_p=*/true,
18534 /*type_p=*/false,
18535 /*is_declaration=*/true);
18537 return cp_parser_namespace_name (parser);
18540 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18541 access declaration.
18543 using-declaration:
18544 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18545 using :: unqualified-id ;
18547 access-declaration:
18548 qualified-id ;
18552 static bool
18553 cp_parser_using_declaration (cp_parser* parser,
18554 bool access_declaration_p)
18556 cp_token *token;
18557 bool typename_p = false;
18558 bool global_scope_p;
18559 tree decl;
18560 tree identifier;
18561 tree qscope;
18562 int oldcount = errorcount;
18563 cp_token *diag_token = NULL;
18565 if (access_declaration_p)
18567 diag_token = cp_lexer_peek_token (parser->lexer);
18568 cp_parser_parse_tentatively (parser);
18570 else
18572 /* Look for the `using' keyword. */
18573 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18575 again:
18576 /* Peek at the next token. */
18577 token = cp_lexer_peek_token (parser->lexer);
18578 /* See if it's `typename'. */
18579 if (token->keyword == RID_TYPENAME)
18581 /* Remember that we've seen it. */
18582 typename_p = true;
18583 /* Consume the `typename' token. */
18584 cp_lexer_consume_token (parser->lexer);
18588 /* Look for the optional global scope qualification. */
18589 global_scope_p
18590 = (cp_parser_global_scope_opt (parser,
18591 /*current_scope_valid_p=*/false)
18592 != NULL_TREE);
18594 /* If we saw `typename', or didn't see `::', then there must be a
18595 nested-name-specifier present. */
18596 if (typename_p || !global_scope_p)
18598 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18599 /*check_dependency_p=*/true,
18600 /*type_p=*/false,
18601 /*is_declaration=*/true);
18602 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18604 cp_parser_skip_to_end_of_block_or_statement (parser);
18605 return false;
18608 /* Otherwise, we could be in either of the two productions. In that
18609 case, treat the nested-name-specifier as optional. */
18610 else
18611 qscope = cp_parser_nested_name_specifier_opt (parser,
18612 /*typename_keyword_p=*/false,
18613 /*check_dependency_p=*/true,
18614 /*type_p=*/false,
18615 /*is_declaration=*/true);
18616 if (!qscope)
18617 qscope = global_namespace;
18618 else if (UNSCOPED_ENUM_P (qscope))
18619 qscope = CP_TYPE_CONTEXT (qscope);
18621 if (access_declaration_p && cp_parser_error_occurred (parser))
18622 /* Something has already gone wrong; there's no need to parse
18623 further. Since an error has occurred, the return value of
18624 cp_parser_parse_definitely will be false, as required. */
18625 return cp_parser_parse_definitely (parser);
18627 token = cp_lexer_peek_token (parser->lexer);
18628 /* Parse the unqualified-id. */
18629 identifier = cp_parser_unqualified_id (parser,
18630 /*template_keyword_p=*/false,
18631 /*check_dependency_p=*/true,
18632 /*declarator_p=*/true,
18633 /*optional_p=*/false);
18635 if (access_declaration_p)
18637 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18638 cp_parser_simulate_error (parser);
18639 if (!cp_parser_parse_definitely (parser))
18640 return false;
18642 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18644 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18645 if (cxx_dialect < cxx17
18646 && !in_system_header_at (ell->location))
18647 pedwarn (ell->location, 0,
18648 "pack expansion in using-declaration only available "
18649 "with -std=c++17 or -std=gnu++17");
18650 qscope = make_pack_expansion (qscope);
18653 /* The function we call to handle a using-declaration is different
18654 depending on what scope we are in. */
18655 if (qscope == error_mark_node || identifier == error_mark_node)
18657 else if (!identifier_p (identifier)
18658 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18659 /* [namespace.udecl]
18661 A using declaration shall not name a template-id. */
18662 error_at (token->location,
18663 "a template-id may not appear in a using-declaration");
18664 else
18666 if (at_class_scope_p ())
18668 /* Create the USING_DECL. */
18669 decl = do_class_using_decl (qscope, identifier);
18671 if (decl && typename_p)
18672 USING_DECL_TYPENAME_P (decl) = 1;
18674 if (check_for_bare_parameter_packs (decl))
18676 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18677 return false;
18679 else
18680 /* Add it to the list of members in this class. */
18681 finish_member_declaration (decl);
18683 else
18685 decl = cp_parser_lookup_name_simple (parser,
18686 identifier,
18687 token->location);
18688 if (decl == error_mark_node)
18689 cp_parser_name_lookup_error (parser, identifier,
18690 decl, NLE_NULL,
18691 token->location);
18692 else if (check_for_bare_parameter_packs (decl))
18694 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18695 return false;
18697 else if (!at_namespace_scope_p ())
18698 finish_local_using_decl (decl, qscope, identifier);
18699 else
18700 finish_namespace_using_decl (decl, qscope, identifier);
18704 if (!access_declaration_p
18705 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18707 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18708 if (cxx_dialect < cxx17)
18709 pedwarn (comma->location, 0,
18710 "comma-separated list in using-declaration only available "
18711 "with -std=c++17 or -std=gnu++17");
18712 goto again;
18715 /* Look for the final `;'. */
18716 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18718 if (access_declaration_p && errorcount == oldcount)
18719 warning_at (diag_token->location, OPT_Wdeprecated,
18720 "access declarations are deprecated "
18721 "in favour of using-declarations; "
18722 "suggestion: add the %<using%> keyword");
18724 return true;
18727 /* Parse an alias-declaration.
18729 alias-declaration:
18730 using identifier attribute-specifier-seq [opt] = type-id */
18732 static tree
18733 cp_parser_alias_declaration (cp_parser* parser)
18735 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18736 location_t id_location;
18737 cp_declarator *declarator;
18738 cp_decl_specifier_seq decl_specs;
18739 bool member_p;
18740 const char *saved_message = NULL;
18742 /* Look for the `using' keyword. */
18743 cp_token *using_token
18744 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18745 if (using_token == NULL)
18746 return error_mark_node;
18748 id_location = cp_lexer_peek_token (parser->lexer)->location;
18749 id = cp_parser_identifier (parser);
18750 if (id == error_mark_node)
18751 return error_mark_node;
18753 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18754 attributes = cp_parser_attributes_opt (parser);
18755 if (attributes == error_mark_node)
18756 return error_mark_node;
18758 cp_parser_require (parser, CPP_EQ, RT_EQ);
18760 if (cp_parser_error_occurred (parser))
18761 return error_mark_node;
18763 cp_parser_commit_to_tentative_parse (parser);
18765 /* Now we are going to parse the type-id of the declaration. */
18768 [dcl.type]/3 says:
18770 "A type-specifier-seq shall not define a class or enumeration
18771 unless it appears in the type-id of an alias-declaration (7.1.3) that
18772 is not the declaration of a template-declaration."
18774 In other words, if we currently are in an alias template, the
18775 type-id should not define a type.
18777 So let's set parser->type_definition_forbidden_message in that
18778 case; cp_parser_check_type_definition (called by
18779 cp_parser_class_specifier) will then emit an error if a type is
18780 defined in the type-id. */
18781 if (parser->num_template_parameter_lists)
18783 saved_message = parser->type_definition_forbidden_message;
18784 parser->type_definition_forbidden_message =
18785 G_("types may not be defined in alias template declarations");
18788 type = cp_parser_type_id (parser);
18790 /* Restore the error message if need be. */
18791 if (parser->num_template_parameter_lists)
18792 parser->type_definition_forbidden_message = saved_message;
18794 if (type == error_mark_node
18795 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18797 cp_parser_skip_to_end_of_block_or_statement (parser);
18798 return error_mark_node;
18801 /* A typedef-name can also be introduced by an alias-declaration. The
18802 identifier following the using keyword becomes a typedef-name. It has
18803 the same semantics as if it were introduced by the typedef
18804 specifier. In particular, it does not define a new type and it shall
18805 not appear in the type-id. */
18807 clear_decl_specs (&decl_specs);
18808 decl_specs.type = type;
18809 if (attributes != NULL_TREE)
18811 decl_specs.attributes = attributes;
18812 set_and_check_decl_spec_loc (&decl_specs,
18813 ds_attribute,
18814 attrs_token);
18816 set_and_check_decl_spec_loc (&decl_specs,
18817 ds_typedef,
18818 using_token);
18819 set_and_check_decl_spec_loc (&decl_specs,
18820 ds_alias,
18821 using_token);
18823 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18824 declarator->id_loc = id_location;
18826 member_p = at_class_scope_p ();
18827 if (member_p)
18828 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18829 NULL_TREE, attributes);
18830 else
18831 decl = start_decl (declarator, &decl_specs, 0,
18832 attributes, NULL_TREE, &pushed_scope);
18833 if (decl == error_mark_node)
18834 return decl;
18836 // Attach constraints to the alias declaration.
18837 if (flag_concepts && current_template_parms)
18839 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
18840 tree constr = build_constraints (reqs, NULL_TREE);
18841 set_constraints (decl, constr);
18844 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
18846 if (pushed_scope)
18847 pop_scope (pushed_scope);
18849 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18850 added into the symbol table; otherwise, return the TYPE_DECL. */
18851 if (DECL_LANG_SPECIFIC (decl)
18852 && DECL_TEMPLATE_INFO (decl)
18853 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
18855 decl = DECL_TI_TEMPLATE (decl);
18856 if (member_p)
18857 check_member_template (decl);
18860 return decl;
18863 /* Parse a using-directive.
18865 using-directive:
18866 using namespace :: [opt] nested-name-specifier [opt]
18867 namespace-name ; */
18869 static void
18870 cp_parser_using_directive (cp_parser* parser)
18872 tree namespace_decl;
18873 tree attribs;
18875 /* Look for the `using' keyword. */
18876 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18877 /* And the `namespace' keyword. */
18878 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18879 /* Look for the optional `::' operator. */
18880 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18881 /* And the optional nested-name-specifier. */
18882 cp_parser_nested_name_specifier_opt (parser,
18883 /*typename_keyword_p=*/false,
18884 /*check_dependency_p=*/true,
18885 /*type_p=*/false,
18886 /*is_declaration=*/true);
18887 /* Get the namespace being used. */
18888 namespace_decl = cp_parser_namespace_name (parser);
18889 /* And any specified attributes. */
18890 attribs = cp_parser_attributes_opt (parser);
18892 /* Update the symbol table. */
18893 if (namespace_bindings_p ())
18894 finish_namespace_using_directive (namespace_decl, attribs);
18895 else
18896 finish_local_using_directive (namespace_decl, attribs);
18898 /* Look for the final `;'. */
18899 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18902 /* Parse an asm-definition.
18904 asm-definition:
18905 asm ( string-literal ) ;
18907 GNU Extension:
18909 asm-definition:
18910 asm volatile [opt] ( string-literal ) ;
18911 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18912 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18913 : asm-operand-list [opt] ) ;
18914 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18915 : asm-operand-list [opt]
18916 : asm-clobber-list [opt] ) ;
18917 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18918 : asm-clobber-list [opt]
18919 : asm-goto-list ) ; */
18921 static void
18922 cp_parser_asm_definition (cp_parser* parser)
18924 tree string;
18925 tree outputs = NULL_TREE;
18926 tree inputs = NULL_TREE;
18927 tree clobbers = NULL_TREE;
18928 tree labels = NULL_TREE;
18929 tree asm_stmt;
18930 bool volatile_p = false;
18931 bool extended_p = false;
18932 bool invalid_inputs_p = false;
18933 bool invalid_outputs_p = false;
18934 bool goto_p = false;
18935 required_token missing = RT_NONE;
18937 /* Look for the `asm' keyword. */
18938 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
18940 if (parser->in_function_body
18941 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
18943 error ("%<asm%> in %<constexpr%> function");
18944 cp_function_chain->invalid_constexpr = true;
18947 /* See if the next token is `volatile'. */
18948 if (cp_parser_allow_gnu_extensions_p (parser)
18949 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
18951 /* Remember that we saw the `volatile' keyword. */
18952 volatile_p = true;
18953 /* Consume the token. */
18954 cp_lexer_consume_token (parser->lexer);
18956 if (cp_parser_allow_gnu_extensions_p (parser)
18957 && parser->in_function_body
18958 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
18960 /* Remember that we saw the `goto' keyword. */
18961 goto_p = true;
18962 /* Consume the token. */
18963 cp_lexer_consume_token (parser->lexer);
18965 /* Look for the opening `('. */
18966 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18967 return;
18968 /* Look for the string. */
18969 string = cp_parser_string_literal (parser, false, false);
18970 if (string == error_mark_node)
18972 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18973 /*consume_paren=*/true);
18974 return;
18977 /* If we're allowing GNU extensions, check for the extended assembly
18978 syntax. Unfortunately, the `:' tokens need not be separated by
18979 a space in C, and so, for compatibility, we tolerate that here
18980 too. Doing that means that we have to treat the `::' operator as
18981 two `:' tokens. */
18982 if (cp_parser_allow_gnu_extensions_p (parser)
18983 && parser->in_function_body
18984 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
18985 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
18987 bool inputs_p = false;
18988 bool clobbers_p = false;
18989 bool labels_p = false;
18991 /* The extended syntax was used. */
18992 extended_p = true;
18994 /* Look for outputs. */
18995 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18997 /* Consume the `:'. */
18998 cp_lexer_consume_token (parser->lexer);
18999 /* Parse the output-operands. */
19000 if (cp_lexer_next_token_is_not (parser->lexer,
19001 CPP_COLON)
19002 && cp_lexer_next_token_is_not (parser->lexer,
19003 CPP_SCOPE)
19004 && cp_lexer_next_token_is_not (parser->lexer,
19005 CPP_CLOSE_PAREN)
19006 && !goto_p)
19008 outputs = cp_parser_asm_operand_list (parser);
19009 if (outputs == error_mark_node)
19010 invalid_outputs_p = true;
19013 /* If the next token is `::', there are no outputs, and the
19014 next token is the beginning of the inputs. */
19015 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19016 /* The inputs are coming next. */
19017 inputs_p = true;
19019 /* Look for inputs. */
19020 if (inputs_p
19021 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19023 /* Consume the `:' or `::'. */
19024 cp_lexer_consume_token (parser->lexer);
19025 /* Parse the output-operands. */
19026 if (cp_lexer_next_token_is_not (parser->lexer,
19027 CPP_COLON)
19028 && cp_lexer_next_token_is_not (parser->lexer,
19029 CPP_SCOPE)
19030 && cp_lexer_next_token_is_not (parser->lexer,
19031 CPP_CLOSE_PAREN))
19033 inputs = cp_parser_asm_operand_list (parser);
19034 if (inputs == error_mark_node)
19035 invalid_inputs_p = true;
19038 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19039 /* The clobbers are coming next. */
19040 clobbers_p = true;
19042 /* Look for clobbers. */
19043 if (clobbers_p
19044 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19046 clobbers_p = true;
19047 /* Consume the `:' or `::'. */
19048 cp_lexer_consume_token (parser->lexer);
19049 /* Parse the clobbers. */
19050 if (cp_lexer_next_token_is_not (parser->lexer,
19051 CPP_COLON)
19052 && cp_lexer_next_token_is_not (parser->lexer,
19053 CPP_CLOSE_PAREN))
19054 clobbers = cp_parser_asm_clobber_list (parser);
19056 else if (goto_p
19057 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19058 /* The labels are coming next. */
19059 labels_p = true;
19061 /* Look for labels. */
19062 if (labels_p
19063 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19065 labels_p = true;
19066 /* Consume the `:' or `::'. */
19067 cp_lexer_consume_token (parser->lexer);
19068 /* Parse the labels. */
19069 labels = cp_parser_asm_label_list (parser);
19072 if (goto_p && !labels_p)
19073 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19075 else if (goto_p)
19076 missing = RT_COLON_SCOPE;
19078 /* Look for the closing `)'. */
19079 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19080 missing ? missing : RT_CLOSE_PAREN))
19081 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19082 /*consume_paren=*/true);
19083 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19085 if (!invalid_inputs_p && !invalid_outputs_p)
19087 /* Create the ASM_EXPR. */
19088 if (parser->in_function_body)
19090 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19091 inputs, clobbers, labels);
19092 /* If the extended syntax was not used, mark the ASM_EXPR. */
19093 if (!extended_p)
19095 tree temp = asm_stmt;
19096 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19097 temp = TREE_OPERAND (temp, 0);
19099 ASM_INPUT_P (temp) = 1;
19102 else
19103 symtab->finalize_toplevel_asm (string);
19107 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19108 type that comes from the decl-specifier-seq. */
19110 static tree
19111 strip_declarator_types (tree type, cp_declarator *declarator)
19113 for (cp_declarator *d = declarator; d;)
19114 switch (d->kind)
19116 case cdk_id:
19117 case cdk_decomp:
19118 case cdk_error:
19119 d = NULL;
19120 break;
19122 default:
19123 if (TYPE_PTRMEMFUNC_P (type))
19124 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19125 type = TREE_TYPE (type);
19126 d = d->declarator;
19127 break;
19130 return type;
19133 /* Declarators [gram.dcl.decl] */
19135 /* Parse an init-declarator.
19137 init-declarator:
19138 declarator initializer [opt]
19140 GNU Extension:
19142 init-declarator:
19143 declarator asm-specification [opt] attributes [opt] initializer [opt]
19145 function-definition:
19146 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19147 function-body
19148 decl-specifier-seq [opt] declarator function-try-block
19150 GNU Extension:
19152 function-definition:
19153 __extension__ function-definition
19155 TM Extension:
19157 function-definition:
19158 decl-specifier-seq [opt] declarator function-transaction-block
19160 The DECL_SPECIFIERS apply to this declarator. Returns a
19161 representation of the entity declared. If MEMBER_P is TRUE, then
19162 this declarator appears in a class scope. The new DECL created by
19163 this declarator is returned.
19165 The CHECKS are access checks that should be performed once we know
19166 what entity is being declared (and, therefore, what classes have
19167 befriended it).
19169 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19170 for a function-definition here as well. If the declarator is a
19171 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19172 be TRUE upon return. By that point, the function-definition will
19173 have been completely parsed.
19175 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19176 is FALSE.
19178 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19179 parsed declaration if it is an uninitialized single declarator not followed
19180 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19181 if present, will not be consumed. If returned, this declarator will be
19182 created with SD_INITIALIZED but will not call cp_finish_decl.
19184 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19185 and there is an initializer, the pointed location_t is set to the
19186 location of the '=' or `(', or '{' in C++11 token introducing the
19187 initializer. */
19189 static tree
19190 cp_parser_init_declarator (cp_parser* parser,
19191 cp_decl_specifier_seq *decl_specifiers,
19192 vec<deferred_access_check, va_gc> *checks,
19193 bool function_definition_allowed_p,
19194 bool member_p,
19195 int declares_class_or_enum,
19196 bool* function_definition_p,
19197 tree* maybe_range_for_decl,
19198 location_t* init_loc,
19199 tree* auto_result)
19201 cp_token *token = NULL, *asm_spec_start_token = NULL,
19202 *attributes_start_token = NULL;
19203 cp_declarator *declarator;
19204 tree prefix_attributes;
19205 tree attributes = NULL;
19206 tree asm_specification;
19207 tree initializer;
19208 tree decl = NULL_TREE;
19209 tree scope;
19210 int is_initialized;
19211 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19212 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19213 "(...)". */
19214 enum cpp_ttype initialization_kind;
19215 bool is_direct_init = false;
19216 bool is_non_constant_init;
19217 int ctor_dtor_or_conv_p;
19218 bool friend_p = cp_parser_friend_p (decl_specifiers);
19219 tree pushed_scope = NULL_TREE;
19220 bool range_for_decl_p = false;
19221 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19222 location_t tmp_init_loc = UNKNOWN_LOCATION;
19224 /* Gather the attributes that were provided with the
19225 decl-specifiers. */
19226 prefix_attributes = decl_specifiers->attributes;
19228 /* Assume that this is not the declarator for a function
19229 definition. */
19230 if (function_definition_p)
19231 *function_definition_p = false;
19233 /* Default arguments are only permitted for function parameters. */
19234 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19235 parser->default_arg_ok_p = false;
19237 /* Defer access checks while parsing the declarator; we cannot know
19238 what names are accessible until we know what is being
19239 declared. */
19240 resume_deferring_access_checks ();
19242 token = cp_lexer_peek_token (parser->lexer);
19244 /* Parse the declarator. */
19245 declarator
19246 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19247 &ctor_dtor_or_conv_p,
19248 /*parenthesized_p=*/NULL,
19249 member_p, friend_p);
19250 /* Gather up the deferred checks. */
19251 stop_deferring_access_checks ();
19253 parser->default_arg_ok_p = saved_default_arg_ok_p;
19255 /* If the DECLARATOR was erroneous, there's no need to go
19256 further. */
19257 if (declarator == cp_error_declarator)
19258 return error_mark_node;
19260 /* Check that the number of template-parameter-lists is OK. */
19261 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19262 token->location))
19263 return error_mark_node;
19265 if (declares_class_or_enum & 2)
19266 cp_parser_check_for_definition_in_return_type (declarator,
19267 decl_specifiers->type,
19268 decl_specifiers->locations[ds_type_spec]);
19270 /* Figure out what scope the entity declared by the DECLARATOR is
19271 located in. `grokdeclarator' sometimes changes the scope, so
19272 we compute it now. */
19273 scope = get_scope_of_declarator (declarator);
19275 /* Perform any lookups in the declared type which were thought to be
19276 dependent, but are not in the scope of the declarator. */
19277 decl_specifiers->type
19278 = maybe_update_decl_type (decl_specifiers->type, scope);
19280 /* If we're allowing GNU extensions, look for an
19281 asm-specification. */
19282 if (cp_parser_allow_gnu_extensions_p (parser))
19284 /* Look for an asm-specification. */
19285 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19286 asm_specification = cp_parser_asm_specification_opt (parser);
19288 else
19289 asm_specification = NULL_TREE;
19291 /* Look for attributes. */
19292 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19293 attributes = cp_parser_attributes_opt (parser);
19295 /* Peek at the next token. */
19296 token = cp_lexer_peek_token (parser->lexer);
19298 bool bogus_implicit_tmpl = false;
19300 if (function_declarator_p (declarator))
19302 /* Handle C++17 deduction guides. */
19303 if (!decl_specifiers->type
19304 && ctor_dtor_or_conv_p <= 0
19305 && cxx_dialect >= cxx17)
19307 cp_declarator *id = get_id_declarator (declarator);
19308 tree name = id->u.id.unqualified_name;
19309 parser->scope = id->u.id.qualifying_scope;
19310 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19311 if (tmpl
19312 && (DECL_CLASS_TEMPLATE_P (tmpl)
19313 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19315 id->u.id.unqualified_name = dguide_name (tmpl);
19316 id->u.id.sfk = sfk_deduction_guide;
19317 ctor_dtor_or_conv_p = 1;
19321 /* Check to see if the token indicates the start of a
19322 function-definition. */
19323 if (cp_parser_token_starts_function_definition_p (token))
19325 if (!function_definition_allowed_p)
19327 /* If a function-definition should not appear here, issue an
19328 error message. */
19329 cp_parser_error (parser,
19330 "a function-definition is not allowed here");
19331 return error_mark_node;
19334 location_t func_brace_location
19335 = cp_lexer_peek_token (parser->lexer)->location;
19337 /* Neither attributes nor an asm-specification are allowed
19338 on a function-definition. */
19339 if (asm_specification)
19340 error_at (asm_spec_start_token->location,
19341 "an asm-specification is not allowed "
19342 "on a function-definition");
19343 if (attributes)
19344 error_at (attributes_start_token->location,
19345 "attributes are not allowed "
19346 "on a function-definition");
19347 /* This is a function-definition. */
19348 *function_definition_p = true;
19350 /* Parse the function definition. */
19351 if (member_p)
19352 decl = cp_parser_save_member_function_body (parser,
19353 decl_specifiers,
19354 declarator,
19355 prefix_attributes);
19356 else
19357 decl =
19358 (cp_parser_function_definition_from_specifiers_and_declarator
19359 (parser, decl_specifiers, prefix_attributes, declarator));
19361 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19363 /* This is where the prologue starts... */
19364 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19365 = func_brace_location;
19368 return decl;
19371 else if (parser->fully_implicit_function_template_p)
19373 /* A non-template declaration involving a function parameter list
19374 containing an implicit template parameter will be made into a
19375 template. If the resulting declaration is not going to be an
19376 actual function then finish the template scope here to prevent it.
19377 An error message will be issued once we have a decl to talk about.
19379 FIXME probably we should do type deduction rather than create an
19380 implicit template, but the standard currently doesn't allow it. */
19381 bogus_implicit_tmpl = true;
19382 finish_fully_implicit_template (parser, NULL_TREE);
19385 /* [dcl.dcl]
19387 Only in function declarations for constructors, destructors, type
19388 conversions, and deduction guides can the decl-specifier-seq be omitted.
19390 We explicitly postpone this check past the point where we handle
19391 function-definitions because we tolerate function-definitions
19392 that are missing their return types in some modes. */
19393 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19395 cp_parser_error (parser,
19396 "expected constructor, destructor, or type conversion");
19397 return error_mark_node;
19400 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19401 if (token->type == CPP_EQ
19402 || token->type == CPP_OPEN_PAREN
19403 || token->type == CPP_OPEN_BRACE)
19405 is_initialized = SD_INITIALIZED;
19406 initialization_kind = token->type;
19407 if (maybe_range_for_decl)
19408 *maybe_range_for_decl = error_mark_node;
19409 tmp_init_loc = token->location;
19410 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19411 *init_loc = tmp_init_loc;
19413 if (token->type == CPP_EQ
19414 && function_declarator_p (declarator))
19416 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19417 if (t2->keyword == RID_DEFAULT)
19418 is_initialized = SD_DEFAULTED;
19419 else if (t2->keyword == RID_DELETE)
19420 is_initialized = SD_DELETED;
19423 else
19425 /* If the init-declarator isn't initialized and isn't followed by a
19426 `,' or `;', it's not a valid init-declarator. */
19427 if (token->type != CPP_COMMA
19428 && token->type != CPP_SEMICOLON)
19430 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19431 range_for_decl_p = true;
19432 else
19434 if (!maybe_range_for_decl)
19435 cp_parser_error (parser, "expected initializer");
19436 return error_mark_node;
19439 is_initialized = SD_UNINITIALIZED;
19440 initialization_kind = CPP_EOF;
19443 /* Because start_decl has side-effects, we should only call it if we
19444 know we're going ahead. By this point, we know that we cannot
19445 possibly be looking at any other construct. */
19446 cp_parser_commit_to_tentative_parse (parser);
19448 /* Enter the newly declared entry in the symbol table. If we're
19449 processing a declaration in a class-specifier, we wait until
19450 after processing the initializer. */
19451 if (!member_p)
19453 if (parser->in_unbraced_linkage_specification_p)
19454 decl_specifiers->storage_class = sc_extern;
19455 decl = start_decl (declarator, decl_specifiers,
19456 range_for_decl_p? SD_INITIALIZED : is_initialized,
19457 attributes, prefix_attributes, &pushed_scope);
19458 cp_finalize_omp_declare_simd (parser, decl);
19459 cp_finalize_oacc_routine (parser, decl, false);
19460 /* Adjust location of decl if declarator->id_loc is more appropriate:
19461 set, and decl wasn't merged with another decl, in which case its
19462 location would be different from input_location, and more accurate. */
19463 if (DECL_P (decl)
19464 && declarator->id_loc != UNKNOWN_LOCATION
19465 && DECL_SOURCE_LOCATION (decl) == input_location)
19466 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19468 else if (scope)
19469 /* Enter the SCOPE. That way unqualified names appearing in the
19470 initializer will be looked up in SCOPE. */
19471 pushed_scope = push_scope (scope);
19473 /* Perform deferred access control checks, now that we know in which
19474 SCOPE the declared entity resides. */
19475 if (!member_p && decl)
19477 tree saved_current_function_decl = NULL_TREE;
19479 /* If the entity being declared is a function, pretend that we
19480 are in its scope. If it is a `friend', it may have access to
19481 things that would not otherwise be accessible. */
19482 if (TREE_CODE (decl) == FUNCTION_DECL)
19484 saved_current_function_decl = current_function_decl;
19485 current_function_decl = decl;
19488 /* Perform access checks for template parameters. */
19489 cp_parser_perform_template_parameter_access_checks (checks);
19491 /* Perform the access control checks for the declarator and the
19492 decl-specifiers. */
19493 perform_deferred_access_checks (tf_warning_or_error);
19495 /* Restore the saved value. */
19496 if (TREE_CODE (decl) == FUNCTION_DECL)
19497 current_function_decl = saved_current_function_decl;
19500 /* Parse the initializer. */
19501 initializer = NULL_TREE;
19502 is_direct_init = false;
19503 is_non_constant_init = true;
19504 if (is_initialized)
19506 if (function_declarator_p (declarator))
19508 if (initialization_kind == CPP_EQ)
19509 initializer = cp_parser_pure_specifier (parser);
19510 else
19512 /* If the declaration was erroneous, we don't really
19513 know what the user intended, so just silently
19514 consume the initializer. */
19515 if (decl != error_mark_node)
19516 error_at (tmp_init_loc, "initializer provided for function");
19517 cp_parser_skip_to_closing_parenthesis (parser,
19518 /*recovering=*/true,
19519 /*or_comma=*/false,
19520 /*consume_paren=*/true);
19523 else
19525 /* We want to record the extra mangling scope for in-class
19526 initializers of class members and initializers of static data
19527 member templates. The former involves deferring
19528 parsing of the initializer until end of class as with default
19529 arguments. So right here we only handle the latter. */
19530 if (!member_p && processing_template_decl)
19531 start_lambda_scope (decl);
19532 initializer = cp_parser_initializer (parser,
19533 &is_direct_init,
19534 &is_non_constant_init);
19535 if (!member_p && processing_template_decl)
19536 finish_lambda_scope ();
19537 if (initializer == error_mark_node)
19538 cp_parser_skip_to_end_of_statement (parser);
19542 /* The old parser allows attributes to appear after a parenthesized
19543 initializer. Mark Mitchell proposed removing this functionality
19544 on the GCC mailing lists on 2002-08-13. This parser accepts the
19545 attributes -- but ignores them. */
19546 if (cp_parser_allow_gnu_extensions_p (parser)
19547 && initialization_kind == CPP_OPEN_PAREN)
19548 if (cp_parser_attributes_opt (parser))
19549 warning (OPT_Wattributes,
19550 "attributes after parenthesized initializer ignored");
19552 /* And now complain about a non-function implicit template. */
19553 if (bogus_implicit_tmpl && decl != error_mark_node)
19554 error_at (DECL_SOURCE_LOCATION (decl),
19555 "non-function %qD declared as implicit template", decl);
19557 /* For an in-class declaration, use `grokfield' to create the
19558 declaration. */
19559 if (member_p)
19561 if (pushed_scope)
19563 pop_scope (pushed_scope);
19564 pushed_scope = NULL_TREE;
19566 decl = grokfield (declarator, decl_specifiers,
19567 initializer, !is_non_constant_init,
19568 /*asmspec=*/NULL_TREE,
19569 chainon (attributes, prefix_attributes));
19570 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19571 cp_parser_save_default_args (parser, decl);
19572 cp_finalize_omp_declare_simd (parser, decl);
19573 cp_finalize_oacc_routine (parser, decl, false);
19576 /* Finish processing the declaration. But, skip member
19577 declarations. */
19578 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19580 cp_finish_decl (decl,
19581 initializer, !is_non_constant_init,
19582 asm_specification,
19583 /* If the initializer is in parentheses, then this is
19584 a direct-initialization, which means that an
19585 `explicit' constructor is OK. Otherwise, an
19586 `explicit' constructor cannot be used. */
19587 ((is_direct_init || !is_initialized)
19588 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19590 else if ((cxx_dialect != cxx98) && friend_p
19591 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19592 /* Core issue #226 (C++0x only): A default template-argument
19593 shall not be specified in a friend class template
19594 declaration. */
19595 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19596 /*is_partial=*/false, /*is_friend_decl=*/1);
19598 if (!friend_p && pushed_scope)
19599 pop_scope (pushed_scope);
19601 if (function_declarator_p (declarator)
19602 && parser->fully_implicit_function_template_p)
19604 if (member_p)
19605 decl = finish_fully_implicit_template (parser, decl);
19606 else
19607 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19610 if (auto_result && is_initialized && decl_specifiers->type
19611 && type_uses_auto (decl_specifiers->type))
19612 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19614 return decl;
19617 /* Parse a declarator.
19619 declarator:
19620 direct-declarator
19621 ptr-operator declarator
19623 abstract-declarator:
19624 ptr-operator abstract-declarator [opt]
19625 direct-abstract-declarator
19627 GNU Extensions:
19629 declarator:
19630 attributes [opt] direct-declarator
19631 attributes [opt] ptr-operator declarator
19633 abstract-declarator:
19634 attributes [opt] ptr-operator abstract-declarator [opt]
19635 attributes [opt] direct-abstract-declarator
19637 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19638 detect constructors, destructors, deduction guides, or conversion operators.
19639 It is set to -1 if the declarator is a name, and +1 if it is a
19640 function. Otherwise it is set to zero. Usually you just want to
19641 test for >0, but internally the negative value is used.
19643 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19644 a decl-specifier-seq unless it declares a constructor, destructor,
19645 or conversion. It might seem that we could check this condition in
19646 semantic analysis, rather than parsing, but that makes it difficult
19647 to handle something like `f()'. We want to notice that there are
19648 no decl-specifiers, and therefore realize that this is an
19649 expression, not a declaration.)
19651 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19652 the declarator is a direct-declarator of the form "(...)".
19654 MEMBER_P is true iff this declarator is a member-declarator.
19656 FRIEND_P is true iff this declarator is a friend. */
19658 static cp_declarator *
19659 cp_parser_declarator (cp_parser* parser,
19660 cp_parser_declarator_kind dcl_kind,
19661 int* ctor_dtor_or_conv_p,
19662 bool* parenthesized_p,
19663 bool member_p, bool friend_p)
19665 cp_declarator *declarator;
19666 enum tree_code code;
19667 cp_cv_quals cv_quals;
19668 tree class_type;
19669 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19671 /* Assume this is not a constructor, destructor, or type-conversion
19672 operator. */
19673 if (ctor_dtor_or_conv_p)
19674 *ctor_dtor_or_conv_p = 0;
19676 if (cp_parser_allow_gnu_extensions_p (parser))
19677 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19679 /* Check for the ptr-operator production. */
19680 cp_parser_parse_tentatively (parser);
19681 /* Parse the ptr-operator. */
19682 code = cp_parser_ptr_operator (parser,
19683 &class_type,
19684 &cv_quals,
19685 &std_attributes);
19687 /* If that worked, then we have a ptr-operator. */
19688 if (cp_parser_parse_definitely (parser))
19690 /* If a ptr-operator was found, then this declarator was not
19691 parenthesized. */
19692 if (parenthesized_p)
19693 *parenthesized_p = true;
19694 /* The dependent declarator is optional if we are parsing an
19695 abstract-declarator. */
19696 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19697 cp_parser_parse_tentatively (parser);
19699 /* Parse the dependent declarator. */
19700 declarator = cp_parser_declarator (parser, dcl_kind,
19701 /*ctor_dtor_or_conv_p=*/NULL,
19702 /*parenthesized_p=*/NULL,
19703 /*member_p=*/false,
19704 friend_p);
19706 /* If we are parsing an abstract-declarator, we must handle the
19707 case where the dependent declarator is absent. */
19708 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19709 && !cp_parser_parse_definitely (parser))
19710 declarator = NULL;
19712 declarator = cp_parser_make_indirect_declarator
19713 (code, class_type, cv_quals, declarator, std_attributes);
19715 /* Everything else is a direct-declarator. */
19716 else
19718 if (parenthesized_p)
19719 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19720 CPP_OPEN_PAREN);
19721 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19722 ctor_dtor_or_conv_p,
19723 member_p, friend_p);
19726 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19727 declarator->attributes = gnu_attributes;
19728 return declarator;
19731 /* Parse a direct-declarator or direct-abstract-declarator.
19733 direct-declarator:
19734 declarator-id
19735 direct-declarator ( parameter-declaration-clause )
19736 cv-qualifier-seq [opt]
19737 ref-qualifier [opt]
19738 exception-specification [opt]
19739 direct-declarator [ constant-expression [opt] ]
19740 ( declarator )
19742 direct-abstract-declarator:
19743 direct-abstract-declarator [opt]
19744 ( parameter-declaration-clause )
19745 cv-qualifier-seq [opt]
19746 ref-qualifier [opt]
19747 exception-specification [opt]
19748 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19749 ( abstract-declarator )
19751 Returns a representation of the declarator. DCL_KIND is
19752 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19753 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19754 we are parsing a direct-declarator. It is
19755 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19756 of ambiguity we prefer an abstract declarator, as per
19757 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19758 as for cp_parser_declarator. */
19760 static cp_declarator *
19761 cp_parser_direct_declarator (cp_parser* parser,
19762 cp_parser_declarator_kind dcl_kind,
19763 int* ctor_dtor_or_conv_p,
19764 bool member_p, bool friend_p)
19766 cp_token *token;
19767 cp_declarator *declarator = NULL;
19768 tree scope = NULL_TREE;
19769 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19770 bool saved_in_declarator_p = parser->in_declarator_p;
19771 bool first = true;
19772 tree pushed_scope = NULL_TREE;
19773 cp_token *open_paren = NULL, *close_paren = NULL;
19775 while (true)
19777 /* Peek at the next token. */
19778 token = cp_lexer_peek_token (parser->lexer);
19779 if (token->type == CPP_OPEN_PAREN)
19781 /* This is either a parameter-declaration-clause, or a
19782 parenthesized declarator. When we know we are parsing a
19783 named declarator, it must be a parenthesized declarator
19784 if FIRST is true. For instance, `(int)' is a
19785 parameter-declaration-clause, with an omitted
19786 direct-abstract-declarator. But `((*))', is a
19787 parenthesized abstract declarator. Finally, when T is a
19788 template parameter `(T)' is a
19789 parameter-declaration-clause, and not a parenthesized
19790 named declarator.
19792 We first try and parse a parameter-declaration-clause,
19793 and then try a nested declarator (if FIRST is true).
19795 It is not an error for it not to be a
19796 parameter-declaration-clause, even when FIRST is
19797 false. Consider,
19799 int i (int);
19800 int i (3);
19802 The first is the declaration of a function while the
19803 second is the definition of a variable, including its
19804 initializer.
19806 Having seen only the parenthesis, we cannot know which of
19807 these two alternatives should be selected. Even more
19808 complex are examples like:
19810 int i (int (a));
19811 int i (int (3));
19813 The former is a function-declaration; the latter is a
19814 variable initialization.
19816 Thus again, we try a parameter-declaration-clause, and if
19817 that fails, we back out and return. */
19819 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19821 tree params;
19822 bool is_declarator = false;
19824 open_paren = NULL;
19826 /* In a member-declarator, the only valid interpretation
19827 of a parenthesis is the start of a
19828 parameter-declaration-clause. (It is invalid to
19829 initialize a static data member with a parenthesized
19830 initializer; only the "=" form of initialization is
19831 permitted.) */
19832 if (!member_p)
19833 cp_parser_parse_tentatively (parser);
19835 /* Consume the `('. */
19836 matching_parens parens;
19837 parens.consume_open (parser);
19838 if (first)
19840 /* If this is going to be an abstract declarator, we're
19841 in a declarator and we can't have default args. */
19842 parser->default_arg_ok_p = false;
19843 parser->in_declarator_p = true;
19846 begin_scope (sk_function_parms, NULL_TREE);
19848 /* Parse the parameter-declaration-clause. */
19849 params = cp_parser_parameter_declaration_clause (parser);
19851 /* Consume the `)'. */
19852 parens.require_close (parser);
19854 /* If all went well, parse the cv-qualifier-seq,
19855 ref-qualifier and the exception-specification. */
19856 if (member_p || cp_parser_parse_definitely (parser))
19858 cp_cv_quals cv_quals;
19859 cp_virt_specifiers virt_specifiers;
19860 cp_ref_qualifier ref_qual;
19861 tree exception_specification;
19862 tree late_return;
19863 tree attrs;
19864 bool memfn = (member_p || (pushed_scope
19865 && CLASS_TYPE_P (pushed_scope)));
19867 is_declarator = true;
19869 if (ctor_dtor_or_conv_p)
19870 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
19871 first = false;
19873 /* Parse the cv-qualifier-seq. */
19874 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19875 /* Parse the ref-qualifier. */
19876 ref_qual = cp_parser_ref_qualifier_opt (parser);
19877 /* Parse the tx-qualifier. */
19878 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
19879 /* And the exception-specification. */
19880 exception_specification
19881 = cp_parser_exception_specification_opt (parser);
19883 attrs = cp_parser_std_attribute_spec_seq (parser);
19885 /* In here, we handle cases where attribute is used after
19886 the function declaration. For example:
19887 void func (int x) __attribute__((vector(..))); */
19888 tree gnu_attrs = NULL_TREE;
19889 tree requires_clause = NULL_TREE;
19890 late_return = (cp_parser_late_return_type_opt
19891 (parser, declarator, requires_clause,
19892 memfn ? cv_quals : -1));
19894 /* Parse the virt-specifier-seq. */
19895 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19897 /* Create the function-declarator. */
19898 declarator = make_call_declarator (declarator,
19899 params,
19900 cv_quals,
19901 virt_specifiers,
19902 ref_qual,
19903 tx_qual,
19904 exception_specification,
19905 late_return,
19906 requires_clause);
19907 declarator->std_attributes = attrs;
19908 declarator->attributes = gnu_attrs;
19909 /* Any subsequent parameter lists are to do with
19910 return type, so are not those of the declared
19911 function. */
19912 parser->default_arg_ok_p = false;
19915 /* Remove the function parms from scope. */
19916 pop_bindings_and_leave_scope ();
19918 if (is_declarator)
19919 /* Repeat the main loop. */
19920 continue;
19923 /* If this is the first, we can try a parenthesized
19924 declarator. */
19925 if (first)
19927 bool saved_in_type_id_in_expr_p;
19929 parser->default_arg_ok_p = saved_default_arg_ok_p;
19930 parser->in_declarator_p = saved_in_declarator_p;
19932 open_paren = token;
19933 /* Consume the `('. */
19934 matching_parens parens;
19935 parens.consume_open (parser);
19936 /* Parse the nested declarator. */
19937 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19938 parser->in_type_id_in_expr_p = true;
19939 declarator
19940 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
19941 /*parenthesized_p=*/NULL,
19942 member_p, friend_p);
19943 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19944 first = false;
19945 /* Expect a `)'. */
19946 close_paren = cp_lexer_peek_token (parser->lexer);
19947 if (!parens.require_close (parser))
19948 declarator = cp_error_declarator;
19949 if (declarator == cp_error_declarator)
19950 break;
19952 goto handle_declarator;
19954 /* Otherwise, we must be done. */
19955 else
19956 break;
19958 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19959 && token->type == CPP_OPEN_SQUARE
19960 && !cp_next_tokens_can_be_attribute_p (parser))
19962 /* Parse an array-declarator. */
19963 tree bounds, attrs;
19965 if (ctor_dtor_or_conv_p)
19966 *ctor_dtor_or_conv_p = 0;
19968 open_paren = NULL;
19969 first = false;
19970 parser->default_arg_ok_p = false;
19971 parser->in_declarator_p = true;
19972 /* Consume the `['. */
19973 cp_lexer_consume_token (parser->lexer);
19974 /* Peek at the next token. */
19975 token = cp_lexer_peek_token (parser->lexer);
19976 /* If the next token is `]', then there is no
19977 constant-expression. */
19978 if (token->type != CPP_CLOSE_SQUARE)
19980 bool non_constant_p;
19981 bounds
19982 = cp_parser_constant_expression (parser,
19983 /*allow_non_constant=*/true,
19984 &non_constant_p);
19985 if (!non_constant_p)
19986 /* OK */;
19987 else if (error_operand_p (bounds))
19988 /* Already gave an error. */;
19989 else if (!parser->in_function_body
19990 || current_binding_level->kind == sk_function_parms)
19992 /* Normally, the array bound must be an integral constant
19993 expression. However, as an extension, we allow VLAs
19994 in function scopes as long as they aren't part of a
19995 parameter declaration. */
19996 cp_parser_error (parser,
19997 "array bound is not an integer constant");
19998 bounds = error_mark_node;
20000 else if (processing_template_decl
20001 && !type_dependent_expression_p (bounds))
20003 /* Remember this wasn't a constant-expression. */
20004 bounds = build_nop (TREE_TYPE (bounds), bounds);
20005 TREE_SIDE_EFFECTS (bounds) = 1;
20008 else
20009 bounds = NULL_TREE;
20010 /* Look for the closing `]'. */
20011 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20013 declarator = cp_error_declarator;
20014 break;
20017 attrs = cp_parser_std_attribute_spec_seq (parser);
20018 declarator = make_array_declarator (declarator, bounds);
20019 declarator->std_attributes = attrs;
20021 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20024 tree qualifying_scope;
20025 tree unqualified_name;
20026 tree attrs;
20027 special_function_kind sfk;
20028 bool abstract_ok;
20029 bool pack_expansion_p = false;
20030 cp_token *declarator_id_start_token;
20032 /* Parse a declarator-id */
20033 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20034 if (abstract_ok)
20036 cp_parser_parse_tentatively (parser);
20038 /* If we see an ellipsis, we should be looking at a
20039 parameter pack. */
20040 if (token->type == CPP_ELLIPSIS)
20042 /* Consume the `...' */
20043 cp_lexer_consume_token (parser->lexer);
20045 pack_expansion_p = true;
20049 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20050 unqualified_name
20051 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20052 qualifying_scope = parser->scope;
20053 if (abstract_ok)
20055 bool okay = false;
20057 if (!unqualified_name && pack_expansion_p)
20059 /* Check whether an error occurred. */
20060 okay = !cp_parser_error_occurred (parser);
20062 /* We already consumed the ellipsis to mark a
20063 parameter pack, but we have no way to report it,
20064 so abort the tentative parse. We will be exiting
20065 immediately anyway. */
20066 cp_parser_abort_tentative_parse (parser);
20068 else
20069 okay = cp_parser_parse_definitely (parser);
20071 if (!okay)
20072 unqualified_name = error_mark_node;
20073 else if (unqualified_name
20074 && (qualifying_scope
20075 || (!identifier_p (unqualified_name))))
20077 cp_parser_error (parser, "expected unqualified-id");
20078 unqualified_name = error_mark_node;
20082 if (!unqualified_name)
20083 return NULL;
20084 if (unqualified_name == error_mark_node)
20086 declarator = cp_error_declarator;
20087 pack_expansion_p = false;
20088 declarator->parameter_pack_p = false;
20089 break;
20092 attrs = cp_parser_std_attribute_spec_seq (parser);
20094 if (qualifying_scope && at_namespace_scope_p ()
20095 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20097 /* In the declaration of a member of a template class
20098 outside of the class itself, the SCOPE will sometimes
20099 be a TYPENAME_TYPE. For example, given:
20101 template <typename T>
20102 int S<T>::R::i = 3;
20104 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20105 this context, we must resolve S<T>::R to an ordinary
20106 type, rather than a typename type.
20108 The reason we normally avoid resolving TYPENAME_TYPEs
20109 is that a specialization of `S' might render
20110 `S<T>::R' not a type. However, if `S' is
20111 specialized, then this `i' will not be used, so there
20112 is no harm in resolving the types here. */
20113 tree type;
20115 /* Resolve the TYPENAME_TYPE. */
20116 type = resolve_typename_type (qualifying_scope,
20117 /*only_current_p=*/false);
20118 /* If that failed, the declarator is invalid. */
20119 if (TREE_CODE (type) == TYPENAME_TYPE)
20121 if (typedef_variant_p (type))
20122 error_at (declarator_id_start_token->location,
20123 "cannot define member of dependent typedef "
20124 "%qT", type);
20125 else
20126 error_at (declarator_id_start_token->location,
20127 "%<%T::%E%> is not a type",
20128 TYPE_CONTEXT (qualifying_scope),
20129 TYPE_IDENTIFIER (qualifying_scope));
20131 qualifying_scope = type;
20134 sfk = sfk_none;
20136 if (unqualified_name)
20138 tree class_type;
20140 if (qualifying_scope
20141 && CLASS_TYPE_P (qualifying_scope))
20142 class_type = qualifying_scope;
20143 else
20144 class_type = current_class_type;
20146 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20148 tree name_type = TREE_TYPE (unqualified_name);
20150 if (!class_type || !same_type_p (name_type, class_type))
20152 /* We do not attempt to print the declarator
20153 here because we do not have enough
20154 information about its original syntactic
20155 form. */
20156 cp_parser_error (parser, "invalid declarator");
20157 declarator = cp_error_declarator;
20158 break;
20160 else if (qualifying_scope
20161 && CLASSTYPE_USE_TEMPLATE (name_type))
20163 error_at (declarator_id_start_token->location,
20164 "invalid use of constructor as a template");
20165 inform (declarator_id_start_token->location,
20166 "use %<%T::%D%> instead of %<%T::%D%> to "
20167 "name the constructor in a qualified name",
20168 class_type,
20169 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20170 class_type, name_type);
20171 declarator = cp_error_declarator;
20172 break;
20174 unqualified_name = constructor_name (class_type);
20177 if (class_type)
20179 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20180 sfk = sfk_destructor;
20181 else if (identifier_p (unqualified_name)
20182 && IDENTIFIER_CONV_OP_P (unqualified_name))
20183 sfk = sfk_conversion;
20184 else if (/* There's no way to declare a constructor
20185 for an unnamed type, even if the type
20186 got a name for linkage purposes. */
20187 !TYPE_WAS_UNNAMED (class_type)
20188 /* Handle correctly (c++/19200):
20190 struct S {
20191 struct T{};
20192 friend void S(T);
20195 and also:
20197 namespace N {
20198 void S();
20201 struct S {
20202 friend void N::S();
20203 }; */
20204 && (!friend_p || class_type == qualifying_scope)
20205 && constructor_name_p (unqualified_name,
20206 class_type))
20207 sfk = sfk_constructor;
20208 else if (is_overloaded_fn (unqualified_name)
20209 && DECL_CONSTRUCTOR_P (get_first_fn
20210 (unqualified_name)))
20211 sfk = sfk_constructor;
20213 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20214 *ctor_dtor_or_conv_p = -1;
20217 declarator = make_id_declarator (qualifying_scope,
20218 unqualified_name,
20219 sfk);
20220 declarator->std_attributes = attrs;
20221 declarator->id_loc = token->location;
20222 declarator->parameter_pack_p = pack_expansion_p;
20224 if (pack_expansion_p)
20225 maybe_warn_variadic_templates ();
20228 handle_declarator:;
20229 scope = get_scope_of_declarator (declarator);
20230 if (scope)
20232 /* Any names that appear after the declarator-id for a
20233 member are looked up in the containing scope. */
20234 if (at_function_scope_p ())
20236 /* But declarations with qualified-ids can't appear in a
20237 function. */
20238 cp_parser_error (parser, "qualified-id in declaration");
20239 declarator = cp_error_declarator;
20240 break;
20242 pushed_scope = push_scope (scope);
20244 parser->in_declarator_p = true;
20245 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20246 || (declarator && declarator->kind == cdk_id))
20247 /* Default args are only allowed on function
20248 declarations. */
20249 parser->default_arg_ok_p = saved_default_arg_ok_p;
20250 else
20251 parser->default_arg_ok_p = false;
20253 first = false;
20255 /* We're done. */
20256 else
20257 break;
20260 /* For an abstract declarator, we might wind up with nothing at this
20261 point. That's an error; the declarator is not optional. */
20262 if (!declarator)
20263 cp_parser_error (parser, "expected declarator");
20264 else if (open_paren)
20266 /* Record overly parenthesized declarator so we can give a
20267 diagnostic about confusing decl/expr disambiguation. */
20268 if (declarator->kind == cdk_array)
20270 /* If the open and close parens are on different lines, this
20271 is probably a formatting thing, so ignore. */
20272 expanded_location open = expand_location (open_paren->location);
20273 expanded_location close = expand_location (close_paren->location);
20274 if (open.line != close.line || open.file != close.file)
20275 open_paren = NULL;
20277 if (open_paren)
20278 declarator->parenthesized = open_paren->location;
20281 /* If we entered a scope, we must exit it now. */
20282 if (pushed_scope)
20283 pop_scope (pushed_scope);
20285 parser->default_arg_ok_p = saved_default_arg_ok_p;
20286 parser->in_declarator_p = saved_in_declarator_p;
20288 return declarator;
20291 /* Parse a ptr-operator.
20293 ptr-operator:
20294 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20295 * cv-qualifier-seq [opt]
20297 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20298 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20300 GNU Extension:
20302 ptr-operator:
20303 & cv-qualifier-seq [opt]
20305 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20306 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20307 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20308 filled in with the TYPE containing the member. *CV_QUALS is
20309 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20310 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20311 Note that the tree codes returned by this function have nothing
20312 to do with the types of trees that will be eventually be created
20313 to represent the pointer or reference type being parsed. They are
20314 just constants with suggestive names. */
20315 static enum tree_code
20316 cp_parser_ptr_operator (cp_parser* parser,
20317 tree* type,
20318 cp_cv_quals *cv_quals,
20319 tree *attributes)
20321 enum tree_code code = ERROR_MARK;
20322 cp_token *token;
20323 tree attrs = NULL_TREE;
20325 /* Assume that it's not a pointer-to-member. */
20326 *type = NULL_TREE;
20327 /* And that there are no cv-qualifiers. */
20328 *cv_quals = TYPE_UNQUALIFIED;
20330 /* Peek at the next token. */
20331 token = cp_lexer_peek_token (parser->lexer);
20333 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20334 if (token->type == CPP_MULT)
20335 code = INDIRECT_REF;
20336 else if (token->type == CPP_AND)
20337 code = ADDR_EXPR;
20338 else if ((cxx_dialect != cxx98) &&
20339 token->type == CPP_AND_AND) /* C++0x only */
20340 code = NON_LVALUE_EXPR;
20342 if (code != ERROR_MARK)
20344 /* Consume the `*', `&' or `&&'. */
20345 cp_lexer_consume_token (parser->lexer);
20347 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20348 `&', if we are allowing GNU extensions. (The only qualifier
20349 that can legally appear after `&' is `restrict', but that is
20350 enforced during semantic analysis. */
20351 if (code == INDIRECT_REF
20352 || cp_parser_allow_gnu_extensions_p (parser))
20353 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20355 attrs = cp_parser_std_attribute_spec_seq (parser);
20356 if (attributes != NULL)
20357 *attributes = attrs;
20359 else
20361 /* Try the pointer-to-member case. */
20362 cp_parser_parse_tentatively (parser);
20363 /* Look for the optional `::' operator. */
20364 cp_parser_global_scope_opt (parser,
20365 /*current_scope_valid_p=*/false);
20366 /* Look for the nested-name specifier. */
20367 token = cp_lexer_peek_token (parser->lexer);
20368 cp_parser_nested_name_specifier (parser,
20369 /*typename_keyword_p=*/false,
20370 /*check_dependency_p=*/true,
20371 /*type_p=*/false,
20372 /*is_declaration=*/false);
20373 /* If we found it, and the next token is a `*', then we are
20374 indeed looking at a pointer-to-member operator. */
20375 if (!cp_parser_error_occurred (parser)
20376 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20378 /* Indicate that the `*' operator was used. */
20379 code = INDIRECT_REF;
20381 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20382 error_at (token->location, "%qD is a namespace", parser->scope);
20383 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20384 error_at (token->location, "cannot form pointer to member of "
20385 "non-class %q#T", parser->scope);
20386 else
20388 /* The type of which the member is a member is given by the
20389 current SCOPE. */
20390 *type = parser->scope;
20391 /* The next name will not be qualified. */
20392 parser->scope = NULL_TREE;
20393 parser->qualifying_scope = NULL_TREE;
20394 parser->object_scope = NULL_TREE;
20395 /* Look for optional c++11 attributes. */
20396 attrs = cp_parser_std_attribute_spec_seq (parser);
20397 if (attributes != NULL)
20398 *attributes = attrs;
20399 /* Look for the optional cv-qualifier-seq. */
20400 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20403 /* If that didn't work we don't have a ptr-operator. */
20404 if (!cp_parser_parse_definitely (parser))
20405 cp_parser_error (parser, "expected ptr-operator");
20408 return code;
20411 /* Parse an (optional) cv-qualifier-seq.
20413 cv-qualifier-seq:
20414 cv-qualifier cv-qualifier-seq [opt]
20416 cv-qualifier:
20417 const
20418 volatile
20420 GNU Extension:
20422 cv-qualifier:
20423 __restrict__
20425 Returns a bitmask representing the cv-qualifiers. */
20427 static cp_cv_quals
20428 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20430 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20432 while (true)
20434 cp_token *token;
20435 cp_cv_quals cv_qualifier;
20437 /* Peek at the next token. */
20438 token = cp_lexer_peek_token (parser->lexer);
20439 /* See if it's a cv-qualifier. */
20440 switch (token->keyword)
20442 case RID_CONST:
20443 cv_qualifier = TYPE_QUAL_CONST;
20444 break;
20446 case RID_VOLATILE:
20447 cv_qualifier = TYPE_QUAL_VOLATILE;
20448 break;
20450 case RID_RESTRICT:
20451 cv_qualifier = TYPE_QUAL_RESTRICT;
20452 break;
20454 default:
20455 cv_qualifier = TYPE_UNQUALIFIED;
20456 break;
20459 if (!cv_qualifier)
20460 break;
20462 if (cv_quals & cv_qualifier)
20464 gcc_rich_location richloc (token->location);
20465 richloc.add_fixit_remove ();
20466 error_at (&richloc, "duplicate cv-qualifier");
20467 cp_lexer_purge_token (parser->lexer);
20469 else
20471 cp_lexer_consume_token (parser->lexer);
20472 cv_quals |= cv_qualifier;
20476 return cv_quals;
20479 /* Parse an (optional) ref-qualifier
20481 ref-qualifier:
20485 Returns cp_ref_qualifier representing ref-qualifier. */
20487 static cp_ref_qualifier
20488 cp_parser_ref_qualifier_opt (cp_parser* parser)
20490 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20492 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20493 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20494 return ref_qual;
20496 while (true)
20498 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20499 cp_token *token = cp_lexer_peek_token (parser->lexer);
20501 switch (token->type)
20503 case CPP_AND:
20504 curr_ref_qual = REF_QUAL_LVALUE;
20505 break;
20507 case CPP_AND_AND:
20508 curr_ref_qual = REF_QUAL_RVALUE;
20509 break;
20511 default:
20512 curr_ref_qual = REF_QUAL_NONE;
20513 break;
20516 if (!curr_ref_qual)
20517 break;
20518 else if (ref_qual)
20520 error_at (token->location, "multiple ref-qualifiers");
20521 cp_lexer_purge_token (parser->lexer);
20523 else
20525 ref_qual = curr_ref_qual;
20526 cp_lexer_consume_token (parser->lexer);
20530 return ref_qual;
20533 /* Parse an optional tx-qualifier.
20535 tx-qualifier:
20536 transaction_safe
20537 transaction_safe_dynamic */
20539 static tree
20540 cp_parser_tx_qualifier_opt (cp_parser *parser)
20542 cp_token *token = cp_lexer_peek_token (parser->lexer);
20543 if (token->type == CPP_NAME)
20545 tree name = token->u.value;
20546 const char *p = IDENTIFIER_POINTER (name);
20547 const int len = strlen ("transaction_safe");
20548 if (!strncmp (p, "transaction_safe", len))
20550 p += len;
20551 if (*p == '\0'
20552 || !strcmp (p, "_dynamic"))
20554 cp_lexer_consume_token (parser->lexer);
20555 if (!flag_tm)
20557 error ("%qE requires %<-fgnu-tm%>", name);
20558 return NULL_TREE;
20560 else
20561 return name;
20565 return NULL_TREE;
20568 /* Parse an (optional) virt-specifier-seq.
20570 virt-specifier-seq:
20571 virt-specifier virt-specifier-seq [opt]
20573 virt-specifier:
20574 override
20575 final
20577 Returns a bitmask representing the virt-specifiers. */
20579 static cp_virt_specifiers
20580 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20582 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20584 while (true)
20586 cp_token *token;
20587 cp_virt_specifiers virt_specifier;
20589 /* Peek at the next token. */
20590 token = cp_lexer_peek_token (parser->lexer);
20591 /* See if it's a virt-specifier-qualifier. */
20592 if (token->type != CPP_NAME)
20593 break;
20594 if (id_equal (token->u.value, "override"))
20596 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20597 virt_specifier = VIRT_SPEC_OVERRIDE;
20599 else if (id_equal (token->u.value, "final"))
20601 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20602 virt_specifier = VIRT_SPEC_FINAL;
20604 else if (id_equal (token->u.value, "__final"))
20606 virt_specifier = VIRT_SPEC_FINAL;
20608 else
20609 break;
20611 if (virt_specifiers & virt_specifier)
20613 gcc_rich_location richloc (token->location);
20614 richloc.add_fixit_remove ();
20615 error_at (&richloc, "duplicate virt-specifier");
20616 cp_lexer_purge_token (parser->lexer);
20618 else
20620 cp_lexer_consume_token (parser->lexer);
20621 virt_specifiers |= virt_specifier;
20624 return virt_specifiers;
20627 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20628 is in scope even though it isn't real. */
20630 void
20631 inject_this_parameter (tree ctype, cp_cv_quals quals)
20633 tree this_parm;
20635 if (current_class_ptr)
20637 /* We don't clear this between NSDMIs. Is it already what we want? */
20638 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20639 if (DECL_P (current_class_ptr)
20640 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
20641 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
20642 && cp_type_quals (type) == quals)
20643 return;
20646 this_parm = build_this_parm (NULL_TREE, ctype, quals);
20647 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20648 current_class_ptr = NULL_TREE;
20649 current_class_ref
20650 = cp_build_fold_indirect_ref (this_parm);
20651 current_class_ptr = this_parm;
20654 /* Return true iff our current scope is a non-static data member
20655 initializer. */
20657 bool
20658 parsing_nsdmi (void)
20660 /* We recognize NSDMI context by the context-less 'this' pointer set up
20661 by the function above. */
20662 if (current_class_ptr
20663 && TREE_CODE (current_class_ptr) == PARM_DECL
20664 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20665 return true;
20666 return false;
20669 /* Parse a late-specified return type, if any. This is not a separate
20670 non-terminal, but part of a function declarator, which looks like
20672 -> trailing-type-specifier-seq abstract-declarator(opt)
20674 Returns the type indicated by the type-id.
20676 In addition to this, parse any queued up #pragma omp declare simd
20677 clauses, and #pragma acc routine clauses.
20679 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20680 function. */
20682 static tree
20683 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20684 tree& requires_clause, cp_cv_quals quals)
20686 cp_token *token;
20687 tree type = NULL_TREE;
20688 bool declare_simd_p = (parser->omp_declare_simd
20689 && declarator
20690 && declarator->kind == cdk_id);
20692 bool oacc_routine_p = (parser->oacc_routine
20693 && declarator
20694 && declarator->kind == cdk_id);
20696 /* Peek at the next token. */
20697 token = cp_lexer_peek_token (parser->lexer);
20698 /* A late-specified return type is indicated by an initial '->'. */
20699 if (token->type != CPP_DEREF
20700 && token->keyword != RID_REQUIRES
20701 && !(token->type == CPP_NAME
20702 && token->u.value == ridpointers[RID_REQUIRES])
20703 && !(declare_simd_p || oacc_routine_p))
20704 return NULL_TREE;
20706 tree save_ccp = current_class_ptr;
20707 tree save_ccr = current_class_ref;
20708 if (quals >= 0)
20710 /* DR 1207: 'this' is in scope in the trailing return type. */
20711 inject_this_parameter (current_class_type, quals);
20714 if (token->type == CPP_DEREF)
20716 /* Consume the ->. */
20717 cp_lexer_consume_token (parser->lexer);
20719 type = cp_parser_trailing_type_id (parser);
20722 /* Function declarations may be followed by a trailing
20723 requires-clause. */
20724 requires_clause = cp_parser_requires_clause_opt (parser);
20726 if (declare_simd_p)
20727 declarator->attributes
20728 = cp_parser_late_parsing_omp_declare_simd (parser,
20729 declarator->attributes);
20730 if (oacc_routine_p)
20731 declarator->attributes
20732 = cp_parser_late_parsing_oacc_routine (parser,
20733 declarator->attributes);
20735 if (quals >= 0)
20737 current_class_ptr = save_ccp;
20738 current_class_ref = save_ccr;
20741 return type;
20744 /* Parse a declarator-id.
20746 declarator-id:
20747 id-expression
20748 :: [opt] nested-name-specifier [opt] type-name
20750 In the `id-expression' case, the value returned is as for
20751 cp_parser_id_expression if the id-expression was an unqualified-id.
20752 If the id-expression was a qualified-id, then a SCOPE_REF is
20753 returned. The first operand is the scope (either a NAMESPACE_DECL
20754 or TREE_TYPE), but the second is still just a representation of an
20755 unqualified-id. */
20757 static tree
20758 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20760 tree id;
20761 /* The expression must be an id-expression. Assume that qualified
20762 names are the names of types so that:
20764 template <class T>
20765 int S<T>::R::i = 3;
20767 will work; we must treat `S<T>::R' as the name of a type.
20768 Similarly, assume that qualified names are templates, where
20769 required, so that:
20771 template <class T>
20772 int S<T>::R<T>::i = 3;
20774 will work, too. */
20775 id = cp_parser_id_expression (parser,
20776 /*template_keyword_p=*/false,
20777 /*check_dependency_p=*/false,
20778 /*template_p=*/NULL,
20779 /*declarator_p=*/true,
20780 optional_p);
20781 if (id && BASELINK_P (id))
20782 id = BASELINK_FUNCTIONS (id);
20783 return id;
20786 /* Parse a type-id.
20788 type-id:
20789 type-specifier-seq abstract-declarator [opt]
20791 Returns the TYPE specified. */
20793 static tree
20794 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
20795 bool is_trailing_return)
20797 cp_decl_specifier_seq type_specifier_seq;
20798 cp_declarator *abstract_declarator;
20800 /* Parse the type-specifier-seq. */
20801 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
20802 is_trailing_return,
20803 &type_specifier_seq);
20804 if (is_template_arg && type_specifier_seq.type
20805 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
20806 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
20807 /* A bare template name as a template argument is a template template
20808 argument, not a placeholder, so fail parsing it as a type argument. */
20810 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
20811 cp_parser_simulate_error (parser);
20812 return error_mark_node;
20814 if (type_specifier_seq.type == error_mark_node)
20815 return error_mark_node;
20817 /* There might or might not be an abstract declarator. */
20818 cp_parser_parse_tentatively (parser);
20819 /* Look for the declarator. */
20820 abstract_declarator
20821 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
20822 /*parenthesized_p=*/NULL,
20823 /*member_p=*/false,
20824 /*friend_p=*/false);
20825 /* Check to see if there really was a declarator. */
20826 if (!cp_parser_parse_definitely (parser))
20827 abstract_declarator = NULL;
20829 if (type_specifier_seq.type
20830 /* The concepts TS allows 'auto' as a type-id. */
20831 && (!flag_concepts || parser->in_type_id_in_expr_p)
20832 /* None of the valid uses of 'auto' in C++14 involve the type-id
20833 nonterminal, but it is valid in a trailing-return-type. */
20834 && !(cxx_dialect >= cxx14 && is_trailing_return))
20835 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
20837 /* A type-id with type 'auto' is only ok if the abstract declarator
20838 is a function declarator with a late-specified return type.
20840 A type-id with 'auto' is also valid in a trailing-return-type
20841 in a compound-requirement. */
20842 if (abstract_declarator
20843 && abstract_declarator->kind == cdk_function
20844 && abstract_declarator->u.function.late_return_type)
20845 /* OK */;
20846 else if (parser->in_result_type_constraint_p)
20847 /* OK */;
20848 else
20850 location_t loc = type_specifier_seq.locations[ds_type_spec];
20851 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
20853 error_at (loc, "missing template arguments after %qT",
20854 auto_node);
20855 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
20856 tmpl);
20858 else
20859 error_at (loc, "invalid use of %qT", auto_node);
20860 return error_mark_node;
20864 return groktypename (&type_specifier_seq, abstract_declarator,
20865 is_template_arg);
20868 static tree
20869 cp_parser_type_id (cp_parser *parser)
20871 return cp_parser_type_id_1 (parser, false, false);
20874 static tree
20875 cp_parser_template_type_arg (cp_parser *parser)
20877 tree r;
20878 const char *saved_message = parser->type_definition_forbidden_message;
20879 parser->type_definition_forbidden_message
20880 = G_("types may not be defined in template arguments");
20881 r = cp_parser_type_id_1 (parser, true, false);
20882 parser->type_definition_forbidden_message = saved_message;
20883 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
20885 error ("invalid use of %<auto%> in template argument");
20886 r = error_mark_node;
20888 return r;
20891 static tree
20892 cp_parser_trailing_type_id (cp_parser *parser)
20894 return cp_parser_type_id_1 (parser, false, true);
20897 /* Parse a type-specifier-seq.
20899 type-specifier-seq:
20900 type-specifier type-specifier-seq [opt]
20902 GNU extension:
20904 type-specifier-seq:
20905 attributes type-specifier-seq [opt]
20907 If IS_DECLARATION is true, we are at the start of a "condition" or
20908 exception-declaration, so we might be followed by a declarator-id.
20910 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
20911 i.e. we've just seen "->".
20913 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
20915 static void
20916 cp_parser_type_specifier_seq (cp_parser* parser,
20917 bool is_declaration,
20918 bool is_trailing_return,
20919 cp_decl_specifier_seq *type_specifier_seq)
20921 bool seen_type_specifier = false;
20922 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
20923 cp_token *start_token = NULL;
20925 /* Clear the TYPE_SPECIFIER_SEQ. */
20926 clear_decl_specs (type_specifier_seq);
20928 /* In the context of a trailing return type, enum E { } is an
20929 elaborated-type-specifier followed by a function-body, not an
20930 enum-specifier. */
20931 if (is_trailing_return)
20932 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
20934 /* Parse the type-specifiers and attributes. */
20935 while (true)
20937 tree type_specifier;
20938 bool is_cv_qualifier;
20940 /* Check for attributes first. */
20941 if (cp_next_tokens_can_be_attribute_p (parser))
20943 type_specifier_seq->attributes =
20944 chainon (type_specifier_seq->attributes,
20945 cp_parser_attributes_opt (parser));
20946 continue;
20949 /* record the token of the beginning of the type specifier seq,
20950 for error reporting purposes*/
20951 if (!start_token)
20952 start_token = cp_lexer_peek_token (parser->lexer);
20954 /* Look for the type-specifier. */
20955 type_specifier = cp_parser_type_specifier (parser,
20956 flags,
20957 type_specifier_seq,
20958 /*is_declaration=*/false,
20959 NULL,
20960 &is_cv_qualifier);
20961 if (!type_specifier)
20963 /* If the first type-specifier could not be found, this is not a
20964 type-specifier-seq at all. */
20965 if (!seen_type_specifier)
20967 /* Set in_declarator_p to avoid skipping to the semicolon. */
20968 int in_decl = parser->in_declarator_p;
20969 parser->in_declarator_p = true;
20971 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
20972 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
20973 cp_parser_error (parser, "expected type-specifier");
20975 parser->in_declarator_p = in_decl;
20977 type_specifier_seq->type = error_mark_node;
20978 return;
20980 /* If subsequent type-specifiers could not be found, the
20981 type-specifier-seq is complete. */
20982 break;
20985 seen_type_specifier = true;
20986 /* The standard says that a condition can be:
20988 type-specifier-seq declarator = assignment-expression
20990 However, given:
20992 struct S {};
20993 if (int S = ...)
20995 we should treat the "S" as a declarator, not as a
20996 type-specifier. The standard doesn't say that explicitly for
20997 type-specifier-seq, but it does say that for
20998 decl-specifier-seq in an ordinary declaration. Perhaps it
20999 would be clearer just to allow a decl-specifier-seq here, and
21000 then add a semantic restriction that if any decl-specifiers
21001 that are not type-specifiers appear, the program is invalid. */
21002 if (is_declaration && !is_cv_qualifier)
21003 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21007 /* Return whether the function currently being declared has an associated
21008 template parameter list. */
21010 static bool
21011 function_being_declared_is_template_p (cp_parser* parser)
21013 if (!current_template_parms || processing_template_parmlist)
21014 return false;
21016 if (parser->implicit_template_scope)
21017 return true;
21019 if (at_class_scope_p ()
21020 && TYPE_BEING_DEFINED (current_class_type))
21021 return parser->num_template_parameter_lists != 0;
21023 return ((int) parser->num_template_parameter_lists > template_class_depth
21024 (current_class_type));
21027 /* Parse a parameter-declaration-clause.
21029 parameter-declaration-clause:
21030 parameter-declaration-list [opt] ... [opt]
21031 parameter-declaration-list , ...
21033 Returns a representation for the parameter declarations. A return
21034 value of NULL indicates a parameter-declaration-clause consisting
21035 only of an ellipsis. */
21037 static tree
21038 cp_parser_parameter_declaration_clause (cp_parser* parser)
21040 tree parameters;
21041 cp_token *token;
21042 bool ellipsis_p;
21043 bool is_error;
21045 struct cleanup {
21046 cp_parser* parser;
21047 int auto_is_implicit_function_template_parm_p;
21048 ~cleanup() {
21049 parser->auto_is_implicit_function_template_parm_p
21050 = auto_is_implicit_function_template_parm_p;
21052 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
21054 (void) cleanup;
21056 if (!processing_specialization
21057 && !processing_template_parmlist
21058 && !processing_explicit_instantiation)
21059 if (!current_function_decl
21060 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21061 parser->auto_is_implicit_function_template_parm_p = true;
21063 /* Peek at the next token. */
21064 token = cp_lexer_peek_token (parser->lexer);
21065 /* Check for trivial parameter-declaration-clauses. */
21066 if (token->type == CPP_ELLIPSIS)
21068 /* Consume the `...' token. */
21069 cp_lexer_consume_token (parser->lexer);
21070 return NULL_TREE;
21072 else if (token->type == CPP_CLOSE_PAREN)
21073 /* There are no parameters. */
21075 #ifndef NO_IMPLICIT_EXTERN_C
21076 if (in_system_header_at (input_location)
21077 && current_class_type == NULL
21078 && current_lang_name == lang_name_c)
21079 return NULL_TREE;
21080 else
21081 #endif
21082 return void_list_node;
21084 /* Check for `(void)', too, which is a special case. */
21085 else if (token->keyword == RID_VOID
21086 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21087 == CPP_CLOSE_PAREN))
21089 /* Consume the `void' token. */
21090 cp_lexer_consume_token (parser->lexer);
21091 /* There are no parameters. */
21092 return void_list_node;
21095 /* Parse the parameter-declaration-list. */
21096 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
21097 /* If a parse error occurred while parsing the
21098 parameter-declaration-list, then the entire
21099 parameter-declaration-clause is erroneous. */
21100 if (is_error)
21101 return NULL;
21103 /* Peek at the next token. */
21104 token = cp_lexer_peek_token (parser->lexer);
21105 /* If it's a `,', the clause should terminate with an ellipsis. */
21106 if (token->type == CPP_COMMA)
21108 /* Consume the `,'. */
21109 cp_lexer_consume_token (parser->lexer);
21110 /* Expect an ellipsis. */
21111 ellipsis_p
21112 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21114 /* It might also be `...' if the optional trailing `,' was
21115 omitted. */
21116 else if (token->type == CPP_ELLIPSIS)
21118 /* Consume the `...' token. */
21119 cp_lexer_consume_token (parser->lexer);
21120 /* And remember that we saw it. */
21121 ellipsis_p = true;
21123 else
21124 ellipsis_p = false;
21126 /* Finish the parameter list. */
21127 if (!ellipsis_p)
21128 parameters = chainon (parameters, void_list_node);
21130 return parameters;
21133 /* Parse a parameter-declaration-list.
21135 parameter-declaration-list:
21136 parameter-declaration
21137 parameter-declaration-list , parameter-declaration
21139 Returns a representation of the parameter-declaration-list, as for
21140 cp_parser_parameter_declaration_clause. However, the
21141 `void_list_node' is never appended to the list. Upon return,
21142 *IS_ERROR will be true iff an error occurred. */
21144 static tree
21145 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
21147 tree parameters = NULL_TREE;
21148 tree *tail = &parameters;
21149 bool saved_in_unbraced_linkage_specification_p;
21150 int index = 0;
21152 /* Assume all will go well. */
21153 *is_error = false;
21154 /* The special considerations that apply to a function within an
21155 unbraced linkage specifications do not apply to the parameters
21156 to the function. */
21157 saved_in_unbraced_linkage_specification_p
21158 = parser->in_unbraced_linkage_specification_p;
21159 parser->in_unbraced_linkage_specification_p = false;
21161 /* Look for more parameters. */
21162 while (true)
21164 cp_parameter_declarator *parameter;
21165 tree decl = error_mark_node;
21166 bool parenthesized_p = false;
21167 int template_parm_idx = (function_being_declared_is_template_p (parser)?
21168 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21169 (current_template_parms)) : 0);
21171 /* Parse the parameter. */
21172 parameter
21173 = cp_parser_parameter_declaration (parser,
21174 /*template_parm_p=*/false,
21175 &parenthesized_p);
21177 /* We don't know yet if the enclosing context is deprecated, so wait
21178 and warn in grokparms if appropriate. */
21179 deprecated_state = DEPRECATED_SUPPRESS;
21181 if (parameter)
21183 /* If a function parameter pack was specified and an implicit template
21184 parameter was introduced during cp_parser_parameter_declaration,
21185 change any implicit parameters introduced into packs. */
21186 if (parser->implicit_template_parms
21187 && parameter->declarator
21188 && parameter->declarator->parameter_pack_p)
21190 int latest_template_parm_idx = TREE_VEC_LENGTH
21191 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21193 if (latest_template_parm_idx != template_parm_idx)
21194 parameter->decl_specifiers.type = convert_generic_types_to_packs
21195 (parameter->decl_specifiers.type,
21196 template_parm_idx, latest_template_parm_idx);
21199 decl = grokdeclarator (parameter->declarator,
21200 &parameter->decl_specifiers,
21201 PARM,
21202 parameter->default_argument != NULL_TREE,
21203 &parameter->decl_specifiers.attributes);
21204 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21205 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21208 deprecated_state = DEPRECATED_NORMAL;
21210 /* If a parse error occurred parsing the parameter declaration,
21211 then the entire parameter-declaration-list is erroneous. */
21212 if (decl == error_mark_node)
21214 *is_error = true;
21215 parameters = error_mark_node;
21216 break;
21219 if (parameter->decl_specifiers.attributes)
21220 cplus_decl_attributes (&decl,
21221 parameter->decl_specifiers.attributes,
21223 if (DECL_NAME (decl))
21224 decl = pushdecl (decl);
21226 if (decl != error_mark_node)
21228 retrofit_lang_decl (decl);
21229 DECL_PARM_INDEX (decl) = ++index;
21230 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21233 /* Add the new parameter to the list. */
21234 *tail = build_tree_list (parameter->default_argument, decl);
21235 tail = &TREE_CHAIN (*tail);
21237 /* Peek at the next token. */
21238 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21239 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21240 /* These are for Objective-C++ */
21241 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21242 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21243 /* The parameter-declaration-list is complete. */
21244 break;
21245 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21247 cp_token *token;
21249 /* Peek at the next token. */
21250 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21251 /* If it's an ellipsis, then the list is complete. */
21252 if (token->type == CPP_ELLIPSIS)
21253 break;
21254 /* Otherwise, there must be more parameters. Consume the
21255 `,'. */
21256 cp_lexer_consume_token (parser->lexer);
21257 /* When parsing something like:
21259 int i(float f, double d)
21261 we can tell after seeing the declaration for "f" that we
21262 are not looking at an initialization of a variable "i",
21263 but rather at the declaration of a function "i".
21265 Due to the fact that the parsing of template arguments
21266 (as specified to a template-id) requires backtracking we
21267 cannot use this technique when inside a template argument
21268 list. */
21269 if (!parser->in_template_argument_list_p
21270 && !parser->in_type_id_in_expr_p
21271 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21272 /* However, a parameter-declaration of the form
21273 "float(f)" (which is a valid declaration of a
21274 parameter "f") can also be interpreted as an
21275 expression (the conversion of "f" to "float"). */
21276 && !parenthesized_p)
21277 cp_parser_commit_to_tentative_parse (parser);
21279 else
21281 cp_parser_error (parser, "expected %<,%> or %<...%>");
21282 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21283 cp_parser_skip_to_closing_parenthesis (parser,
21284 /*recovering=*/true,
21285 /*or_comma=*/false,
21286 /*consume_paren=*/false);
21287 break;
21291 parser->in_unbraced_linkage_specification_p
21292 = saved_in_unbraced_linkage_specification_p;
21294 /* Reset implicit_template_scope if we are about to leave the function
21295 parameter list that introduced it. Note that for out-of-line member
21296 definitions, there will be one or more class scopes before we get to
21297 the template parameter scope. */
21299 if (cp_binding_level *its = parser->implicit_template_scope)
21300 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21302 while (maybe_its->kind == sk_class)
21303 maybe_its = maybe_its->level_chain;
21304 if (maybe_its == its)
21306 parser->implicit_template_parms = 0;
21307 parser->implicit_template_scope = 0;
21311 return parameters;
21314 /* Parse a parameter declaration.
21316 parameter-declaration:
21317 decl-specifier-seq ... [opt] declarator
21318 decl-specifier-seq declarator = assignment-expression
21319 decl-specifier-seq ... [opt] abstract-declarator [opt]
21320 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21322 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21323 declares a template parameter. (In that case, a non-nested `>'
21324 token encountered during the parsing of the assignment-expression
21325 is not interpreted as a greater-than operator.)
21327 Returns a representation of the parameter, or NULL if an error
21328 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21329 true iff the declarator is of the form "(p)". */
21331 static cp_parameter_declarator *
21332 cp_parser_parameter_declaration (cp_parser *parser,
21333 bool template_parm_p,
21334 bool *parenthesized_p)
21336 int declares_class_or_enum;
21337 cp_decl_specifier_seq decl_specifiers;
21338 cp_declarator *declarator;
21339 tree default_argument;
21340 cp_token *token = NULL, *declarator_token_start = NULL;
21341 const char *saved_message;
21342 bool template_parameter_pack_p = false;
21344 /* In a template parameter, `>' is not an operator.
21346 [temp.param]
21348 When parsing a default template-argument for a non-type
21349 template-parameter, the first non-nested `>' is taken as the end
21350 of the template parameter-list rather than a greater-than
21351 operator. */
21353 /* Type definitions may not appear in parameter types. */
21354 saved_message = parser->type_definition_forbidden_message;
21355 parser->type_definition_forbidden_message
21356 = G_("types may not be defined in parameter types");
21358 /* Parse the declaration-specifiers. */
21359 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21360 cp_parser_decl_specifier_seq (parser,
21361 CP_PARSER_FLAGS_NONE,
21362 &decl_specifiers,
21363 &declares_class_or_enum);
21365 /* Complain about missing 'typename' or other invalid type names. */
21366 if (!decl_specifiers.any_type_specifiers_p
21367 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21368 decl_specifiers.type = error_mark_node;
21370 /* If an error occurred, there's no reason to attempt to parse the
21371 rest of the declaration. */
21372 if (cp_parser_error_occurred (parser))
21374 parser->type_definition_forbidden_message = saved_message;
21375 return NULL;
21378 /* Peek at the next token. */
21379 token = cp_lexer_peek_token (parser->lexer);
21381 /* If the next token is a `)', `,', `=', `>', or `...', then there
21382 is no declarator. However, when variadic templates are enabled,
21383 there may be a declarator following `...'. */
21384 if (token->type == CPP_CLOSE_PAREN
21385 || token->type == CPP_COMMA
21386 || token->type == CPP_EQ
21387 || token->type == CPP_GREATER)
21389 declarator = NULL;
21390 if (parenthesized_p)
21391 *parenthesized_p = false;
21393 /* Otherwise, there should be a declarator. */
21394 else
21396 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21397 parser->default_arg_ok_p = false;
21399 /* After seeing a decl-specifier-seq, if the next token is not a
21400 "(", there is no possibility that the code is a valid
21401 expression. Therefore, if parsing tentatively, we commit at
21402 this point. */
21403 if (!parser->in_template_argument_list_p
21404 /* In an expression context, having seen:
21406 (int((char ...
21408 we cannot be sure whether we are looking at a
21409 function-type (taking a "char" as a parameter) or a cast
21410 of some object of type "char" to "int". */
21411 && !parser->in_type_id_in_expr_p
21412 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21413 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21414 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21415 cp_parser_commit_to_tentative_parse (parser);
21416 /* Parse the declarator. */
21417 declarator_token_start = token;
21418 declarator = cp_parser_declarator (parser,
21419 CP_PARSER_DECLARATOR_EITHER,
21420 /*ctor_dtor_or_conv_p=*/NULL,
21421 parenthesized_p,
21422 /*member_p=*/false,
21423 /*friend_p=*/false);
21424 parser->default_arg_ok_p = saved_default_arg_ok_p;
21425 /* After the declarator, allow more attributes. */
21426 decl_specifiers.attributes
21427 = chainon (decl_specifiers.attributes,
21428 cp_parser_attributes_opt (parser));
21430 /* If the declarator is a template parameter pack, remember that and
21431 clear the flag in the declarator itself so we don't get errors
21432 from grokdeclarator. */
21433 if (template_parm_p && declarator && declarator->parameter_pack_p)
21435 declarator->parameter_pack_p = false;
21436 template_parameter_pack_p = true;
21440 /* If the next token is an ellipsis, and we have not seen a declarator
21441 name, and if either the type of the declarator contains parameter
21442 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21443 for, eg, abbreviated integral type names), then we actually have a
21444 parameter pack expansion expression. Otherwise, leave the ellipsis
21445 for a C-style variadic function. */
21446 token = cp_lexer_peek_token (parser->lexer);
21447 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21449 tree type = decl_specifiers.type;
21451 if (type && DECL_P (type))
21452 type = TREE_TYPE (type);
21454 if (((type
21455 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21456 && (template_parm_p || uses_parameter_packs (type)))
21457 || (!type && template_parm_p))
21458 && declarator_can_be_parameter_pack (declarator))
21460 /* Consume the `...'. */
21461 cp_lexer_consume_token (parser->lexer);
21462 maybe_warn_variadic_templates ();
21464 /* Build a pack expansion type */
21465 if (template_parm_p)
21466 template_parameter_pack_p = true;
21467 else if (declarator)
21468 declarator->parameter_pack_p = true;
21469 else
21470 decl_specifiers.type = make_pack_expansion (type);
21474 /* The restriction on defining new types applies only to the type
21475 of the parameter, not to the default argument. */
21476 parser->type_definition_forbidden_message = saved_message;
21478 /* If the next token is `=', then process a default argument. */
21479 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21481 tree type = decl_specifiers.type;
21482 token = cp_lexer_peek_token (parser->lexer);
21483 /* If we are defining a class, then the tokens that make up the
21484 default argument must be saved and processed later. */
21485 if (!template_parm_p && at_class_scope_p ()
21486 && TYPE_BEING_DEFINED (current_class_type)
21487 && !LAMBDA_TYPE_P (current_class_type))
21488 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21490 // A constrained-type-specifier may declare a type template-parameter.
21491 else if (declares_constrained_type_template_parameter (type))
21492 default_argument
21493 = cp_parser_default_type_template_argument (parser);
21495 // A constrained-type-specifier may declare a template-template-parameter.
21496 else if (declares_constrained_template_template_parameter (type))
21497 default_argument
21498 = cp_parser_default_template_template_argument (parser);
21500 /* Outside of a class definition, we can just parse the
21501 assignment-expression. */
21502 else
21503 default_argument
21504 = cp_parser_default_argument (parser, template_parm_p);
21506 if (!parser->default_arg_ok_p)
21508 permerror (token->location,
21509 "default arguments are only "
21510 "permitted for function parameters");
21512 else if ((declarator && declarator->parameter_pack_p)
21513 || template_parameter_pack_p
21514 || (decl_specifiers.type
21515 && PACK_EXPANSION_P (decl_specifiers.type)))
21517 /* Find the name of the parameter pack. */
21518 cp_declarator *id_declarator = declarator;
21519 while (id_declarator && id_declarator->kind != cdk_id)
21520 id_declarator = id_declarator->declarator;
21522 if (id_declarator && id_declarator->kind == cdk_id)
21523 error_at (declarator_token_start->location,
21524 template_parm_p
21525 ? G_("template parameter pack %qD "
21526 "cannot have a default argument")
21527 : G_("parameter pack %qD cannot have "
21528 "a default argument"),
21529 id_declarator->u.id.unqualified_name);
21530 else
21531 error_at (declarator_token_start->location,
21532 template_parm_p
21533 ? G_("template parameter pack cannot have "
21534 "a default argument")
21535 : G_("parameter pack cannot have a "
21536 "default argument"));
21538 default_argument = NULL_TREE;
21541 else
21542 default_argument = NULL_TREE;
21544 /* Generate a location for the parameter, ranging from the start of the
21545 initial token to the end of the final token (using input_location for
21546 the latter, set up by cp_lexer_set_source_position_from_token when
21547 consuming tokens).
21549 If we have a identifier, then use it for the caret location, e.g.
21551 extern int callee (int one, int (*two)(int, int), float three);
21552 ~~~~~~^~~~~~~~~~~~~~
21554 otherwise, reuse the start location for the caret location e.g.:
21556 extern int callee (int one, int (*)(int, int), float three);
21557 ^~~~~~~~~~~~~~~~~
21560 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
21561 ? declarator->id_loc
21562 : decl_spec_token_start->location);
21563 location_t param_loc = make_location (caret_loc,
21564 decl_spec_token_start->location,
21565 input_location);
21567 return make_parameter_declarator (&decl_specifiers,
21568 declarator,
21569 default_argument,
21570 param_loc,
21571 template_parameter_pack_p);
21574 /* Parse a default argument and return it.
21576 TEMPLATE_PARM_P is true if this is a default argument for a
21577 non-type template parameter. */
21578 static tree
21579 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21581 tree default_argument = NULL_TREE;
21582 bool saved_greater_than_is_operator_p;
21583 bool saved_local_variables_forbidden_p;
21584 bool non_constant_p, is_direct_init;
21586 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21587 set correctly. */
21588 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21589 parser->greater_than_is_operator_p = !template_parm_p;
21590 /* Local variable names (and the `this' keyword) may not
21591 appear in a default argument. */
21592 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21593 parser->local_variables_forbidden_p = true;
21594 /* Parse the assignment-expression. */
21595 if (template_parm_p)
21596 push_deferring_access_checks (dk_no_deferred);
21597 tree saved_class_ptr = NULL_TREE;
21598 tree saved_class_ref = NULL_TREE;
21599 /* The "this" pointer is not valid in a default argument. */
21600 if (cfun)
21602 saved_class_ptr = current_class_ptr;
21603 cp_function_chain->x_current_class_ptr = NULL_TREE;
21604 saved_class_ref = current_class_ref;
21605 cp_function_chain->x_current_class_ref = NULL_TREE;
21607 default_argument
21608 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21609 /* Restore the "this" pointer. */
21610 if (cfun)
21612 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21613 cp_function_chain->x_current_class_ref = saved_class_ref;
21615 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21616 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21617 if (template_parm_p)
21618 pop_deferring_access_checks ();
21619 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21620 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21622 return default_argument;
21625 /* Parse a function-body.
21627 function-body:
21628 compound_statement */
21630 static void
21631 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21633 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21634 ? BCS_TRY_BLOCK : BCS_NORMAL),
21635 true);
21638 /* Parse a ctor-initializer-opt followed by a function-body. Return
21639 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21640 is true we are parsing a function-try-block. */
21642 static void
21643 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21644 bool in_function_try_block)
21646 tree body, list;
21647 const bool check_body_p =
21648 DECL_CONSTRUCTOR_P (current_function_decl)
21649 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21650 tree last = NULL;
21652 /* Begin the function body. */
21653 body = begin_function_body ();
21654 /* Parse the optional ctor-initializer. */
21655 cp_parser_ctor_initializer_opt (parser);
21657 /* If we're parsing a constexpr constructor definition, we need
21658 to check that the constructor body is indeed empty. However,
21659 before we get to cp_parser_function_body lot of junk has been
21660 generated, so we can't just check that we have an empty block.
21661 Rather we take a snapshot of the outermost block, and check whether
21662 cp_parser_function_body changed its state. */
21663 if (check_body_p)
21665 list = cur_stmt_list;
21666 if (STATEMENT_LIST_TAIL (list))
21667 last = STATEMENT_LIST_TAIL (list)->stmt;
21669 /* Parse the function-body. */
21670 cp_parser_function_body (parser, in_function_try_block);
21671 if (check_body_p)
21672 check_constexpr_ctor_body (last, list, /*complain=*/true);
21673 /* Finish the function body. */
21674 finish_function_body (body);
21677 /* Parse an initializer.
21679 initializer:
21680 = initializer-clause
21681 ( expression-list )
21683 Returns an expression representing the initializer. If no
21684 initializer is present, NULL_TREE is returned.
21686 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21687 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21688 set to TRUE if there is no initializer present. If there is an
21689 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21690 is set to true; otherwise it is set to false. */
21692 static tree
21693 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21694 bool* non_constant_p)
21696 cp_token *token;
21697 tree init;
21699 /* Peek at the next token. */
21700 token = cp_lexer_peek_token (parser->lexer);
21702 /* Let our caller know whether or not this initializer was
21703 parenthesized. */
21704 *is_direct_init = (token->type != CPP_EQ);
21705 /* Assume that the initializer is constant. */
21706 *non_constant_p = false;
21708 if (token->type == CPP_EQ)
21710 /* Consume the `='. */
21711 cp_lexer_consume_token (parser->lexer);
21712 /* Parse the initializer-clause. */
21713 init = cp_parser_initializer_clause (parser, non_constant_p);
21715 else if (token->type == CPP_OPEN_PAREN)
21717 vec<tree, va_gc> *vec;
21718 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21719 /*cast_p=*/false,
21720 /*allow_expansion_p=*/true,
21721 non_constant_p);
21722 if (vec == NULL)
21723 return error_mark_node;
21724 init = build_tree_list_vec (vec);
21725 release_tree_vector (vec);
21727 else if (token->type == CPP_OPEN_BRACE)
21729 cp_lexer_set_source_position (parser->lexer);
21730 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21731 init = cp_parser_braced_list (parser, non_constant_p);
21732 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21734 else
21736 /* Anything else is an error. */
21737 cp_parser_error (parser, "expected initializer");
21738 init = error_mark_node;
21741 if (check_for_bare_parameter_packs (init))
21742 init = error_mark_node;
21744 return init;
21747 /* Parse an initializer-clause.
21749 initializer-clause:
21750 assignment-expression
21751 braced-init-list
21753 Returns an expression representing the initializer.
21755 If the `assignment-expression' production is used the value
21756 returned is simply a representation for the expression.
21758 Otherwise, calls cp_parser_braced_list. */
21760 static cp_expr
21761 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21763 cp_expr initializer;
21765 /* Assume the expression is constant. */
21766 *non_constant_p = false;
21768 /* If it is not a `{', then we are looking at an
21769 assignment-expression. */
21770 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21772 initializer
21773 = cp_parser_constant_expression (parser,
21774 /*allow_non_constant_p=*/true,
21775 non_constant_p);
21777 else
21778 initializer = cp_parser_braced_list (parser, non_constant_p);
21780 return initializer;
21783 /* Parse a brace-enclosed initializer list.
21785 braced-init-list:
21786 { initializer-list , [opt] }
21787 { designated-initializer-list , [opt] }
21790 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21791 the elements of the initializer-list (or NULL, if the last
21792 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21793 NULL_TREE. There is no way to detect whether or not the optional
21794 trailing `,' was provided. NON_CONSTANT_P is as for
21795 cp_parser_initializer. */
21797 static cp_expr
21798 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
21800 tree initializer;
21801 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
21803 /* Consume the `{' token. */
21804 matching_braces braces;
21805 braces.consume_open (parser);
21806 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21807 initializer = make_node (CONSTRUCTOR);
21808 /* If it's not a `}', then there is a non-trivial initializer. */
21809 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
21811 /* Parse the initializer list. */
21812 CONSTRUCTOR_ELTS (initializer)
21813 = cp_parser_initializer_list (parser, non_constant_p);
21814 /* A trailing `,' token is allowed. */
21815 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21816 cp_lexer_consume_token (parser->lexer);
21818 else
21819 *non_constant_p = false;
21820 /* Now, there should be a trailing `}'. */
21821 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
21822 braces.require_close (parser);
21823 TREE_TYPE (initializer) = init_list_type_node;
21825 cp_expr result (initializer);
21826 /* Build a location of the form:
21827 { ... }
21828 ^~~~~~~
21829 with caret==start at the open brace, finish at the close brace. */
21830 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
21831 result.set_location (combined_loc);
21832 return result;
21835 /* Consume tokens up to, and including, the next non-nested closing `]'.
21836 Returns true iff we found a closing `]'. */
21838 static bool
21839 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
21841 unsigned square_depth = 0;
21843 while (true)
21845 cp_token * token = cp_lexer_peek_token (parser->lexer);
21847 switch (token->type)
21849 case CPP_EOF:
21850 case CPP_PRAGMA_EOL:
21851 /* If we've run out of tokens, then there is no closing `]'. */
21852 return false;
21854 case CPP_OPEN_SQUARE:
21855 ++square_depth;
21856 break;
21858 case CPP_CLOSE_SQUARE:
21859 if (!square_depth--)
21861 cp_lexer_consume_token (parser->lexer);
21862 return true;
21864 break;
21866 default:
21867 break;
21870 /* Consume the token. */
21871 cp_lexer_consume_token (parser->lexer);
21875 /* Return true if we are looking at an array-designator, false otherwise. */
21877 static bool
21878 cp_parser_array_designator_p (cp_parser *parser)
21880 /* Consume the `['. */
21881 cp_lexer_consume_token (parser->lexer);
21883 cp_lexer_save_tokens (parser->lexer);
21885 /* Skip tokens until the next token is a closing square bracket.
21886 If we find the closing `]', and the next token is a `=', then
21887 we are looking at an array designator. */
21888 bool array_designator_p
21889 = (cp_parser_skip_to_closing_square_bracket (parser)
21890 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
21892 /* Roll back the tokens we skipped. */
21893 cp_lexer_rollback_tokens (parser->lexer);
21895 return array_designator_p;
21898 /* Parse an initializer-list.
21900 initializer-list:
21901 initializer-clause ... [opt]
21902 initializer-list , initializer-clause ... [opt]
21904 C++2A Extension:
21906 designated-initializer-list:
21907 designated-initializer-clause
21908 designated-initializer-list , designated-initializer-clause
21910 designated-initializer-clause:
21911 designator brace-or-equal-initializer
21913 designator:
21914 . identifier
21916 GNU Extension:
21918 initializer-list:
21919 designation initializer-clause ...[opt]
21920 initializer-list , designation initializer-clause ...[opt]
21922 designation:
21923 . identifier =
21924 identifier :
21925 [ constant-expression ] =
21927 Returns a vec of constructor_elt. The VALUE of each elt is an expression
21928 for the initializer. If the INDEX of the elt is non-NULL, it is the
21929 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
21930 as for cp_parser_initializer. */
21932 static vec<constructor_elt, va_gc> *
21933 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
21935 vec<constructor_elt, va_gc> *v = NULL;
21936 bool first_p = true;
21937 tree first_designator = NULL_TREE;
21939 /* Assume all of the expressions are constant. */
21940 *non_constant_p = false;
21942 /* Parse the rest of the list. */
21943 while (true)
21945 cp_token *token;
21946 tree designator;
21947 tree initializer;
21948 bool clause_non_constant_p;
21949 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21951 /* Handle the C++2A syntax, '. id ='. */
21952 if ((cxx_dialect >= cxx2a
21953 || cp_parser_allow_gnu_extensions_p (parser))
21954 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
21955 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
21956 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
21957 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
21958 == CPP_OPEN_BRACE)))
21960 if (cxx_dialect < cxx2a)
21961 pedwarn (loc, OPT_Wpedantic,
21962 "C++ designated initializers only available with "
21963 "-std=c++2a or -std=gnu++2a");
21964 /* Consume the `.'. */
21965 cp_lexer_consume_token (parser->lexer);
21966 /* Consume the identifier. */
21967 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21968 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21969 /* Consume the `='. */
21970 cp_lexer_consume_token (parser->lexer);
21972 /* Also, if the next token is an identifier and the following one is a
21973 colon, we are looking at the GNU designated-initializer
21974 syntax. */
21975 else if (cp_parser_allow_gnu_extensions_p (parser)
21976 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21977 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21978 == CPP_COLON))
21980 /* Warn the user that they are using an extension. */
21981 pedwarn (loc, OPT_Wpedantic,
21982 "ISO C++ does not allow GNU designated initializers");
21983 /* Consume the identifier. */
21984 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21985 /* Consume the `:'. */
21986 cp_lexer_consume_token (parser->lexer);
21988 /* Also handle C99 array designators, '[ const ] ='. */
21989 else if (cp_parser_allow_gnu_extensions_p (parser)
21990 && !c_dialect_objc ()
21991 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21993 /* In C++11, [ could start a lambda-introducer. */
21994 bool non_const = false;
21996 cp_parser_parse_tentatively (parser);
21998 if (!cp_parser_array_designator_p (parser))
22000 cp_parser_simulate_error (parser);
22001 designator = NULL_TREE;
22003 else
22005 designator = cp_parser_constant_expression (parser, true,
22006 &non_const);
22007 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22008 cp_parser_require (parser, CPP_EQ, RT_EQ);
22011 if (!cp_parser_parse_definitely (parser))
22012 designator = NULL_TREE;
22013 else if (non_const)
22014 require_potential_rvalue_constant_expression (designator);
22015 if (designator)
22016 /* Warn the user that they are using an extension. */
22017 pedwarn (loc, OPT_Wpedantic,
22018 "ISO C++ does not allow C99 designated initializers");
22020 else
22021 designator = NULL_TREE;
22023 if (first_p)
22025 first_designator = designator;
22026 first_p = false;
22028 else if (cxx_dialect >= cxx2a
22029 && first_designator != error_mark_node
22030 && (!first_designator != !designator))
22032 error_at (loc, "either all initializer clauses should be designated "
22033 "or none of them should be");
22034 first_designator = error_mark_node;
22036 else if (cxx_dialect < cxx2a && !first_designator)
22037 first_designator = designator;
22039 /* Parse the initializer. */
22040 initializer = cp_parser_initializer_clause (parser,
22041 &clause_non_constant_p);
22042 /* If any clause is non-constant, so is the entire initializer. */
22043 if (clause_non_constant_p)
22044 *non_constant_p = true;
22046 /* If we have an ellipsis, this is an initializer pack
22047 expansion. */
22048 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22050 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22052 /* Consume the `...'. */
22053 cp_lexer_consume_token (parser->lexer);
22055 if (designator && cxx_dialect >= cxx2a)
22056 error_at (loc,
22057 "%<...%> not allowed in designated initializer list");
22059 /* Turn the initializer into an initializer expansion. */
22060 initializer = make_pack_expansion (initializer);
22063 /* Add it to the vector. */
22064 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22066 /* If the next token is not a comma, we have reached the end of
22067 the list. */
22068 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22069 break;
22071 /* Peek at the next token. */
22072 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22073 /* If the next token is a `}', then we're still done. An
22074 initializer-clause can have a trailing `,' after the
22075 initializer-list and before the closing `}'. */
22076 if (token->type == CPP_CLOSE_BRACE)
22077 break;
22079 /* Consume the `,' token. */
22080 cp_lexer_consume_token (parser->lexer);
22083 /* The same identifier shall not appear in multiple designators
22084 of a designated-initializer-list. */
22085 if (first_designator)
22087 unsigned int i;
22088 tree designator, val;
22089 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22090 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22092 if (IDENTIFIER_MARKED (designator))
22094 error_at (EXPR_LOC_OR_LOC (val, input_location),
22095 "%<.%s%> designator used multiple times in "
22096 "the same initializer list",
22097 IDENTIFIER_POINTER (designator));
22098 (*v)[i].index = NULL_TREE;
22100 else
22101 IDENTIFIER_MARKED (designator) = 1;
22103 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22104 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22105 IDENTIFIER_MARKED (designator) = 0;
22108 return v;
22111 /* Classes [gram.class] */
22113 /* Parse a class-name.
22115 class-name:
22116 identifier
22117 template-id
22119 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22120 to indicate that names looked up in dependent types should be
22121 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22122 keyword has been used to indicate that the name that appears next
22123 is a template. TAG_TYPE indicates the explicit tag given before
22124 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22125 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22126 is the class being defined in a class-head. If ENUM_OK is TRUE,
22127 enum-names are also accepted.
22129 Returns the TYPE_DECL representing the class. */
22131 static tree
22132 cp_parser_class_name (cp_parser *parser,
22133 bool typename_keyword_p,
22134 bool template_keyword_p,
22135 enum tag_types tag_type,
22136 bool check_dependency_p,
22137 bool class_head_p,
22138 bool is_declaration,
22139 bool enum_ok)
22141 tree decl;
22142 tree scope;
22143 bool typename_p;
22144 cp_token *token;
22145 tree identifier = NULL_TREE;
22147 /* All class-names start with an identifier. */
22148 token = cp_lexer_peek_token (parser->lexer);
22149 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22151 cp_parser_error (parser, "expected class-name");
22152 return error_mark_node;
22155 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22156 to a template-id, so we save it here. */
22157 scope = parser->scope;
22158 if (scope == error_mark_node)
22159 return error_mark_node;
22161 /* Any name names a type if we're following the `typename' keyword
22162 in a qualified name where the enclosing scope is type-dependent. */
22163 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22164 && dependent_type_p (scope));
22165 /* Handle the common case (an identifier, but not a template-id)
22166 efficiently. */
22167 if (token->type == CPP_NAME
22168 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22170 cp_token *identifier_token;
22171 bool ambiguous_p;
22173 /* Look for the identifier. */
22174 identifier_token = cp_lexer_peek_token (parser->lexer);
22175 ambiguous_p = identifier_token->error_reported;
22176 identifier = cp_parser_identifier (parser);
22177 /* If the next token isn't an identifier, we are certainly not
22178 looking at a class-name. */
22179 if (identifier == error_mark_node)
22180 decl = error_mark_node;
22181 /* If we know this is a type-name, there's no need to look it
22182 up. */
22183 else if (typename_p)
22184 decl = identifier;
22185 else
22187 tree ambiguous_decls;
22188 /* If we already know that this lookup is ambiguous, then
22189 we've already issued an error message; there's no reason
22190 to check again. */
22191 if (ambiguous_p)
22193 cp_parser_simulate_error (parser);
22194 return error_mark_node;
22196 /* If the next token is a `::', then the name must be a type
22197 name.
22199 [basic.lookup.qual]
22201 During the lookup for a name preceding the :: scope
22202 resolution operator, object, function, and enumerator
22203 names are ignored. */
22204 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22205 tag_type = scope_type;
22206 /* Look up the name. */
22207 decl = cp_parser_lookup_name (parser, identifier,
22208 tag_type,
22209 /*is_template=*/false,
22210 /*is_namespace=*/false,
22211 check_dependency_p,
22212 &ambiguous_decls,
22213 identifier_token->location);
22214 if (ambiguous_decls)
22216 if (cp_parser_parsing_tentatively (parser))
22217 cp_parser_simulate_error (parser);
22218 return error_mark_node;
22222 else
22224 /* Try a template-id. */
22225 decl = cp_parser_template_id (parser, template_keyword_p,
22226 check_dependency_p,
22227 tag_type,
22228 is_declaration);
22229 if (decl == error_mark_node)
22230 return error_mark_node;
22233 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22235 /* If this is a typename, create a TYPENAME_TYPE. */
22236 if (typename_p && decl != error_mark_node)
22238 decl = make_typename_type (scope, decl, typename_type,
22239 /*complain=*/tf_error);
22240 if (decl != error_mark_node)
22241 decl = TYPE_NAME (decl);
22244 decl = strip_using_decl (decl);
22246 /* Check to see that it is really the name of a class. */
22247 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22248 && identifier_p (TREE_OPERAND (decl, 0))
22249 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22250 /* Situations like this:
22252 template <typename T> struct A {
22253 typename T::template X<int>::I i;
22256 are problematic. Is `T::template X<int>' a class-name? The
22257 standard does not seem to be definitive, but there is no other
22258 valid interpretation of the following `::'. Therefore, those
22259 names are considered class-names. */
22261 decl = make_typename_type (scope, decl, tag_type, tf_error);
22262 if (decl != error_mark_node)
22263 decl = TYPE_NAME (decl);
22265 else if (TREE_CODE (decl) != TYPE_DECL
22266 || TREE_TYPE (decl) == error_mark_node
22267 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22268 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22269 /* In Objective-C 2.0, a classname followed by '.' starts a
22270 dot-syntax expression, and it's not a type-name. */
22271 || (c_dialect_objc ()
22272 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22273 && objc_is_class_name (decl)))
22274 decl = error_mark_node;
22276 if (decl == error_mark_node)
22277 cp_parser_error (parser, "expected class-name");
22278 else if (identifier && !parser->scope)
22279 maybe_note_name_used_in_class (identifier, decl);
22281 return decl;
22284 /* Parse a class-specifier.
22286 class-specifier:
22287 class-head { member-specification [opt] }
22289 Returns the TREE_TYPE representing the class. */
22291 static tree
22292 cp_parser_class_specifier_1 (cp_parser* parser)
22294 tree type;
22295 tree attributes = NULL_TREE;
22296 bool nested_name_specifier_p;
22297 unsigned saved_num_template_parameter_lists;
22298 bool saved_in_function_body;
22299 unsigned char in_statement;
22300 bool in_switch_statement_p;
22301 bool saved_in_unbraced_linkage_specification_p;
22302 tree old_scope = NULL_TREE;
22303 tree scope = NULL_TREE;
22304 cp_token *closing_brace;
22306 push_deferring_access_checks (dk_no_deferred);
22308 /* Parse the class-head. */
22309 type = cp_parser_class_head (parser,
22310 &nested_name_specifier_p);
22311 /* If the class-head was a semantic disaster, skip the entire body
22312 of the class. */
22313 if (!type)
22315 cp_parser_skip_to_end_of_block_or_statement (parser);
22316 pop_deferring_access_checks ();
22317 return error_mark_node;
22320 /* Look for the `{'. */
22321 matching_braces braces;
22322 if (!braces.require_open (parser))
22324 pop_deferring_access_checks ();
22325 return error_mark_node;
22328 cp_ensure_no_omp_declare_simd (parser);
22329 cp_ensure_no_oacc_routine (parser);
22331 /* Issue an error message if type-definitions are forbidden here. */
22332 cp_parser_check_type_definition (parser);
22333 /* Remember that we are defining one more class. */
22334 ++parser->num_classes_being_defined;
22335 /* Inside the class, surrounding template-parameter-lists do not
22336 apply. */
22337 saved_num_template_parameter_lists
22338 = parser->num_template_parameter_lists;
22339 parser->num_template_parameter_lists = 0;
22340 /* We are not in a function body. */
22341 saved_in_function_body = parser->in_function_body;
22342 parser->in_function_body = false;
22343 /* Or in a loop. */
22344 in_statement = parser->in_statement;
22345 parser->in_statement = 0;
22346 /* Or in a switch. */
22347 in_switch_statement_p = parser->in_switch_statement_p;
22348 parser->in_switch_statement_p = false;
22349 /* We are not immediately inside an extern "lang" block. */
22350 saved_in_unbraced_linkage_specification_p
22351 = parser->in_unbraced_linkage_specification_p;
22352 parser->in_unbraced_linkage_specification_p = false;
22354 // Associate constraints with the type.
22355 if (flag_concepts)
22356 type = associate_classtype_constraints (type);
22358 /* Start the class. */
22359 if (nested_name_specifier_p)
22361 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22362 old_scope = push_inner_scope (scope);
22364 type = begin_class_definition (type);
22366 if (type == error_mark_node)
22367 /* If the type is erroneous, skip the entire body of the class. */
22368 cp_parser_skip_to_closing_brace (parser);
22369 else
22370 /* Parse the member-specification. */
22371 cp_parser_member_specification_opt (parser);
22373 /* Look for the trailing `}'. */
22374 closing_brace = braces.require_close (parser);
22375 /* Look for trailing attributes to apply to this class. */
22376 if (cp_parser_allow_gnu_extensions_p (parser))
22377 attributes = cp_parser_gnu_attributes_opt (parser);
22378 if (type != error_mark_node)
22379 type = finish_struct (type, attributes);
22380 if (nested_name_specifier_p)
22381 pop_inner_scope (old_scope, scope);
22383 /* We've finished a type definition. Check for the common syntax
22384 error of forgetting a semicolon after the definition. We need to
22385 be careful, as we can't just check for not-a-semicolon and be done
22386 with it; the user might have typed:
22388 class X { } c = ...;
22389 class X { } *p = ...;
22391 and so forth. Instead, enumerate all the possible tokens that
22392 might follow this production; if we don't see one of them, then
22393 complain and silently insert the semicolon. */
22395 cp_token *token = cp_lexer_peek_token (parser->lexer);
22396 bool want_semicolon = true;
22398 if (cp_next_tokens_can_be_std_attribute_p (parser))
22399 /* Don't try to parse c++11 attributes here. As per the
22400 grammar, that should be a task for
22401 cp_parser_decl_specifier_seq. */
22402 want_semicolon = false;
22404 switch (token->type)
22406 case CPP_NAME:
22407 case CPP_SEMICOLON:
22408 case CPP_MULT:
22409 case CPP_AND:
22410 case CPP_OPEN_PAREN:
22411 case CPP_CLOSE_PAREN:
22412 case CPP_COMMA:
22413 want_semicolon = false;
22414 break;
22416 /* While it's legal for type qualifiers and storage class
22417 specifiers to follow type definitions in the grammar, only
22418 compiler testsuites contain code like that. Assume that if
22419 we see such code, then what we're really seeing is a case
22420 like:
22422 class X { }
22423 const <type> var = ...;
22427 class Y { }
22428 static <type> func (...) ...
22430 i.e. the qualifier or specifier applies to the next
22431 declaration. To do so, however, we need to look ahead one
22432 more token to see if *that* token is a type specifier.
22434 This code could be improved to handle:
22436 class Z { }
22437 static const <type> var = ...; */
22438 case CPP_KEYWORD:
22439 if (keyword_is_decl_specifier (token->keyword))
22441 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22443 /* Handling user-defined types here would be nice, but very
22444 tricky. */
22445 want_semicolon
22446 = (lookahead->type == CPP_KEYWORD
22447 && keyword_begins_type_specifier (lookahead->keyword));
22449 break;
22450 default:
22451 break;
22454 /* If we don't have a type, then something is very wrong and we
22455 shouldn't try to do anything clever. Likewise for not seeing the
22456 closing brace. */
22457 if (closing_brace && TYPE_P (type) && want_semicolon)
22459 /* Locate the closing brace. */
22460 cp_token_position prev
22461 = cp_lexer_previous_token_position (parser->lexer);
22462 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22463 location_t loc = prev_token->location;
22465 /* We want to suggest insertion of a ';' immediately *after* the
22466 closing brace, so, if we can, offset the location by 1 column. */
22467 location_t next_loc = loc;
22468 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22469 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22471 rich_location richloc (line_table, next_loc);
22473 /* If we successfully offset the location, suggest the fix-it. */
22474 if (next_loc != loc)
22475 richloc.add_fixit_insert_before (next_loc, ";");
22477 if (CLASSTYPE_DECLARED_CLASS (type))
22478 error_at (&richloc,
22479 "expected %<;%> after class definition");
22480 else if (TREE_CODE (type) == RECORD_TYPE)
22481 error_at (&richloc,
22482 "expected %<;%> after struct definition");
22483 else if (TREE_CODE (type) == UNION_TYPE)
22484 error_at (&richloc,
22485 "expected %<;%> after union definition");
22486 else
22487 gcc_unreachable ();
22489 /* Unget one token and smash it to look as though we encountered
22490 a semicolon in the input stream. */
22491 cp_lexer_set_token_position (parser->lexer, prev);
22492 token = cp_lexer_peek_token (parser->lexer);
22493 token->type = CPP_SEMICOLON;
22494 token->keyword = RID_MAX;
22498 /* If this class is not itself within the scope of another class,
22499 then we need to parse the bodies of all of the queued function
22500 definitions. Note that the queued functions defined in a class
22501 are not always processed immediately following the
22502 class-specifier for that class. Consider:
22504 struct A {
22505 struct B { void f() { sizeof (A); } };
22508 If `f' were processed before the processing of `A' were
22509 completed, there would be no way to compute the size of `A'.
22510 Note that the nesting we are interested in here is lexical --
22511 not the semantic nesting given by TYPE_CONTEXT. In particular,
22512 for:
22514 struct A { struct B; };
22515 struct A::B { void f() { } };
22517 there is no need to delay the parsing of `A::B::f'. */
22518 if (--parser->num_classes_being_defined == 0)
22520 tree decl;
22521 tree class_type = NULL_TREE;
22522 tree pushed_scope = NULL_TREE;
22523 unsigned ix;
22524 cp_default_arg_entry *e;
22525 tree save_ccp, save_ccr;
22527 /* In a first pass, parse default arguments to the functions.
22528 Then, in a second pass, parse the bodies of the functions.
22529 This two-phased approach handles cases like:
22531 struct S {
22532 void f() { g(); }
22533 void g(int i = 3);
22537 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22539 decl = e->decl;
22540 /* If there are default arguments that have not yet been processed,
22541 take care of them now. */
22542 if (class_type != e->class_type)
22544 if (pushed_scope)
22545 pop_scope (pushed_scope);
22546 class_type = e->class_type;
22547 pushed_scope = push_scope (class_type);
22549 /* Make sure that any template parameters are in scope. */
22550 maybe_begin_member_template_processing (decl);
22551 /* Parse the default argument expressions. */
22552 cp_parser_late_parsing_default_args (parser, decl);
22553 /* Remove any template parameters from the symbol table. */
22554 maybe_end_member_template_processing ();
22556 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22557 /* Now parse any NSDMIs. */
22558 save_ccp = current_class_ptr;
22559 save_ccr = current_class_ref;
22560 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22562 if (class_type != DECL_CONTEXT (decl))
22564 if (pushed_scope)
22565 pop_scope (pushed_scope);
22566 class_type = DECL_CONTEXT (decl);
22567 pushed_scope = push_scope (class_type);
22569 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22570 cp_parser_late_parsing_nsdmi (parser, decl);
22572 vec_safe_truncate (unparsed_nsdmis, 0);
22573 current_class_ptr = save_ccp;
22574 current_class_ref = save_ccr;
22575 if (pushed_scope)
22576 pop_scope (pushed_scope);
22578 /* Now do some post-NSDMI bookkeeping. */
22579 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22580 after_nsdmi_defaulted_late_checks (class_type);
22581 vec_safe_truncate (unparsed_classes, 0);
22582 after_nsdmi_defaulted_late_checks (type);
22584 /* Now parse the body of the functions. */
22585 if (flag_openmp)
22587 /* OpenMP UDRs need to be parsed before all other functions. */
22588 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22589 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22590 cp_parser_late_parsing_for_member (parser, decl);
22591 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22592 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22593 cp_parser_late_parsing_for_member (parser, decl);
22595 else
22596 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22597 cp_parser_late_parsing_for_member (parser, decl);
22598 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22600 else
22601 vec_safe_push (unparsed_classes, type);
22603 /* Put back any saved access checks. */
22604 pop_deferring_access_checks ();
22606 /* Restore saved state. */
22607 parser->in_switch_statement_p = in_switch_statement_p;
22608 parser->in_statement = in_statement;
22609 parser->in_function_body = saved_in_function_body;
22610 parser->num_template_parameter_lists
22611 = saved_num_template_parameter_lists;
22612 parser->in_unbraced_linkage_specification_p
22613 = saved_in_unbraced_linkage_specification_p;
22615 return type;
22618 static tree
22619 cp_parser_class_specifier (cp_parser* parser)
22621 tree ret;
22622 timevar_push (TV_PARSE_STRUCT);
22623 ret = cp_parser_class_specifier_1 (parser);
22624 timevar_pop (TV_PARSE_STRUCT);
22625 return ret;
22628 /* Parse a class-head.
22630 class-head:
22631 class-key identifier [opt] base-clause [opt]
22632 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22633 class-key nested-name-specifier [opt] template-id
22634 base-clause [opt]
22636 class-virt-specifier:
22637 final
22639 GNU Extensions:
22640 class-key attributes identifier [opt] base-clause [opt]
22641 class-key attributes nested-name-specifier identifier base-clause [opt]
22642 class-key attributes nested-name-specifier [opt] template-id
22643 base-clause [opt]
22645 Upon return BASES is initialized to the list of base classes (or
22646 NULL, if there are none) in the same form returned by
22647 cp_parser_base_clause.
22649 Returns the TYPE of the indicated class. Sets
22650 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22651 involving a nested-name-specifier was used, and FALSE otherwise.
22653 Returns error_mark_node if this is not a class-head.
22655 Returns NULL_TREE if the class-head is syntactically valid, but
22656 semantically invalid in a way that means we should skip the entire
22657 body of the class. */
22659 static tree
22660 cp_parser_class_head (cp_parser* parser,
22661 bool* nested_name_specifier_p)
22663 tree nested_name_specifier;
22664 enum tag_types class_key;
22665 tree id = NULL_TREE;
22666 tree type = NULL_TREE;
22667 tree attributes;
22668 tree bases;
22669 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22670 bool template_id_p = false;
22671 bool qualified_p = false;
22672 bool invalid_nested_name_p = false;
22673 bool invalid_explicit_specialization_p = false;
22674 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22675 tree pushed_scope = NULL_TREE;
22676 unsigned num_templates;
22677 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22678 /* Assume no nested-name-specifier will be present. */
22679 *nested_name_specifier_p = false;
22680 /* Assume no template parameter lists will be used in defining the
22681 type. */
22682 num_templates = 0;
22683 parser->colon_corrects_to_scope_p = false;
22685 /* Look for the class-key. */
22686 class_key = cp_parser_class_key (parser);
22687 if (class_key == none_type)
22688 return error_mark_node;
22690 location_t class_head_start_location = input_location;
22692 /* Parse the attributes. */
22693 attributes = cp_parser_attributes_opt (parser);
22695 /* If the next token is `::', that is invalid -- but sometimes
22696 people do try to write:
22698 struct ::S {};
22700 Handle this gracefully by accepting the extra qualifier, and then
22701 issuing an error about it later if this really is a
22702 class-head. If it turns out just to be an elaborated type
22703 specifier, remain silent. */
22704 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22705 qualified_p = true;
22707 push_deferring_access_checks (dk_no_check);
22709 /* Determine the name of the class. Begin by looking for an
22710 optional nested-name-specifier. */
22711 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22712 nested_name_specifier
22713 = cp_parser_nested_name_specifier_opt (parser,
22714 /*typename_keyword_p=*/false,
22715 /*check_dependency_p=*/false,
22716 /*type_p=*/true,
22717 /*is_declaration=*/false);
22718 /* If there was a nested-name-specifier, then there *must* be an
22719 identifier. */
22721 cp_token *bad_template_keyword = NULL;
22723 if (nested_name_specifier)
22725 type_start_token = cp_lexer_peek_token (parser->lexer);
22726 /* Although the grammar says `identifier', it really means
22727 `class-name' or `template-name'. You are only allowed to
22728 define a class that has already been declared with this
22729 syntax.
22731 The proposed resolution for Core Issue 180 says that wherever
22732 you see `class T::X' you should treat `X' as a type-name.
22734 It is OK to define an inaccessible class; for example:
22736 class A { class B; };
22737 class A::B {};
22739 We do not know if we will see a class-name, or a
22740 template-name. We look for a class-name first, in case the
22741 class-name is a template-id; if we looked for the
22742 template-name first we would stop after the template-name. */
22743 cp_parser_parse_tentatively (parser);
22744 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22745 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
22746 type = cp_parser_class_name (parser,
22747 /*typename_keyword_p=*/false,
22748 /*template_keyword_p=*/false,
22749 class_type,
22750 /*check_dependency_p=*/false,
22751 /*class_head_p=*/true,
22752 /*is_declaration=*/false);
22753 /* If that didn't work, ignore the nested-name-specifier. */
22754 if (!cp_parser_parse_definitely (parser))
22756 invalid_nested_name_p = true;
22757 type_start_token = cp_lexer_peek_token (parser->lexer);
22758 id = cp_parser_identifier (parser);
22759 if (id == error_mark_node)
22760 id = NULL_TREE;
22762 /* If we could not find a corresponding TYPE, treat this
22763 declaration like an unqualified declaration. */
22764 if (type == error_mark_node)
22765 nested_name_specifier = NULL_TREE;
22766 /* Otherwise, count the number of templates used in TYPE and its
22767 containing scopes. */
22768 else
22770 tree scope;
22772 for (scope = TREE_TYPE (type);
22773 scope && TREE_CODE (scope) != NAMESPACE_DECL;
22774 scope = get_containing_scope (scope))
22775 if (TYPE_P (scope)
22776 && CLASS_TYPE_P (scope)
22777 && CLASSTYPE_TEMPLATE_INFO (scope)
22778 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
22779 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
22780 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
22781 ++num_templates;
22784 /* Otherwise, the identifier is optional. */
22785 else
22787 /* We don't know whether what comes next is a template-id,
22788 an identifier, or nothing at all. */
22789 cp_parser_parse_tentatively (parser);
22790 /* Check for a template-id. */
22791 type_start_token = cp_lexer_peek_token (parser->lexer);
22792 id = cp_parser_template_id (parser,
22793 /*template_keyword_p=*/false,
22794 /*check_dependency_p=*/true,
22795 class_key,
22796 /*is_declaration=*/true);
22797 /* If that didn't work, it could still be an identifier. */
22798 if (!cp_parser_parse_definitely (parser))
22800 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22802 type_start_token = cp_lexer_peek_token (parser->lexer);
22803 id = cp_parser_identifier (parser);
22805 else
22806 id = NULL_TREE;
22808 else
22810 template_id_p = true;
22811 ++num_templates;
22815 pop_deferring_access_checks ();
22817 if (id)
22819 cp_parser_check_for_invalid_template_id (parser, id,
22820 class_key,
22821 type_start_token->location);
22823 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
22825 /* If it's not a `:' or a `{' then we can't really be looking at a
22826 class-head, since a class-head only appears as part of a
22827 class-specifier. We have to detect this situation before calling
22828 xref_tag, since that has irreversible side-effects. */
22829 if (!cp_parser_next_token_starts_class_definition_p (parser))
22831 cp_parser_error (parser, "expected %<{%> or %<:%>");
22832 type = error_mark_node;
22833 goto out;
22836 /* At this point, we're going ahead with the class-specifier, even
22837 if some other problem occurs. */
22838 cp_parser_commit_to_tentative_parse (parser);
22839 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
22841 cp_parser_error (parser,
22842 "cannot specify %<override%> for a class");
22843 type = error_mark_node;
22844 goto out;
22846 /* Issue the error about the overly-qualified name now. */
22847 if (qualified_p)
22849 cp_parser_error (parser,
22850 "global qualification of class name is invalid");
22851 type = error_mark_node;
22852 goto out;
22854 else if (invalid_nested_name_p)
22856 cp_parser_error (parser,
22857 "qualified name does not name a class");
22858 type = error_mark_node;
22859 goto out;
22861 else if (nested_name_specifier)
22863 tree scope;
22865 if (bad_template_keyword)
22866 /* [temp.names]: in a qualified-id formed by a class-head-name, the
22867 keyword template shall not appear at the top level. */
22868 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
22869 "keyword %<template%> not allowed in class-head-name");
22871 /* Reject typedef-names in class heads. */
22872 if (!DECL_IMPLICIT_TYPEDEF_P (type))
22874 error_at (type_start_token->location,
22875 "invalid class name in declaration of %qD",
22876 type);
22877 type = NULL_TREE;
22878 goto done;
22881 /* Figure out in what scope the declaration is being placed. */
22882 scope = current_scope ();
22883 /* If that scope does not contain the scope in which the
22884 class was originally declared, the program is invalid. */
22885 if (scope && !is_ancestor (scope, nested_name_specifier))
22887 if (at_namespace_scope_p ())
22888 error_at (type_start_token->location,
22889 "declaration of %qD in namespace %qD which does not "
22890 "enclose %qD",
22891 type, scope, nested_name_specifier);
22892 else
22893 error_at (type_start_token->location,
22894 "declaration of %qD in %qD which does not enclose %qD",
22895 type, scope, nested_name_specifier);
22896 type = NULL_TREE;
22897 goto done;
22899 /* [dcl.meaning]
22901 A declarator-id shall not be qualified except for the
22902 definition of a ... nested class outside of its class
22903 ... [or] the definition or explicit instantiation of a
22904 class member of a namespace outside of its namespace. */
22905 if (scope == nested_name_specifier)
22907 permerror (nested_name_specifier_token_start->location,
22908 "extra qualification not allowed");
22909 nested_name_specifier = NULL_TREE;
22910 num_templates = 0;
22913 /* An explicit-specialization must be preceded by "template <>". If
22914 it is not, try to recover gracefully. */
22915 if (at_namespace_scope_p ()
22916 && parser->num_template_parameter_lists == 0
22917 && !processing_template_parmlist
22918 && template_id_p)
22920 /* Build a location of this form:
22921 struct typename <ARGS>
22922 ^~~~~~~~~~~~~~~~~~~~~~
22923 with caret==start at the start token, and
22924 finishing at the end of the type. */
22925 location_t reported_loc
22926 = make_location (class_head_start_location,
22927 class_head_start_location,
22928 get_finish (type_start_token->location));
22929 rich_location richloc (line_table, reported_loc);
22930 richloc.add_fixit_insert_before (class_head_start_location,
22931 "template <> ");
22932 error_at (&richloc,
22933 "an explicit specialization must be preceded by"
22934 " %<template <>%>");
22935 invalid_explicit_specialization_p = true;
22936 /* Take the same action that would have been taken by
22937 cp_parser_explicit_specialization. */
22938 ++parser->num_template_parameter_lists;
22939 begin_specialization ();
22941 /* There must be no "return" statements between this point and the
22942 end of this function; set "type "to the correct return value and
22943 use "goto done;" to return. */
22944 /* Make sure that the right number of template parameters were
22945 present. */
22946 if (!cp_parser_check_template_parameters (parser, num_templates,
22947 type_start_token->location,
22948 /*declarator=*/NULL))
22950 /* If something went wrong, there is no point in even trying to
22951 process the class-definition. */
22952 type = NULL_TREE;
22953 goto done;
22956 /* Look up the type. */
22957 if (template_id_p)
22959 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
22960 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
22961 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
22963 error_at (type_start_token->location,
22964 "function template %qD redeclared as a class template", id);
22965 type = error_mark_node;
22967 else
22969 type = TREE_TYPE (id);
22970 type = maybe_process_partial_specialization (type);
22972 /* Check the scope while we still know whether or not we had a
22973 nested-name-specifier. */
22974 if (type != error_mark_node)
22975 check_unqualified_spec_or_inst (type, type_start_token->location);
22977 if (nested_name_specifier)
22978 pushed_scope = push_scope (nested_name_specifier);
22980 else if (nested_name_specifier)
22982 tree class_type;
22984 /* Given:
22986 template <typename T> struct S { struct T };
22987 template <typename T> struct S<T>::T { };
22989 we will get a TYPENAME_TYPE when processing the definition of
22990 `S::T'. We need to resolve it to the actual type before we
22991 try to define it. */
22992 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
22994 class_type = resolve_typename_type (TREE_TYPE (type),
22995 /*only_current_p=*/false);
22996 if (TREE_CODE (class_type) != TYPENAME_TYPE)
22997 type = TYPE_NAME (class_type);
22998 else
23000 cp_parser_error (parser, "could not resolve typename type");
23001 type = error_mark_node;
23005 if (maybe_process_partial_specialization (TREE_TYPE (type))
23006 == error_mark_node)
23008 type = NULL_TREE;
23009 goto done;
23012 class_type = current_class_type;
23013 /* Enter the scope indicated by the nested-name-specifier. */
23014 pushed_scope = push_scope (nested_name_specifier);
23015 /* Get the canonical version of this type. */
23016 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23017 /* Call push_template_decl if it seems like we should be defining a
23018 template either from the template headers or the type we're
23019 defining, so that we diagnose both extra and missing headers. */
23020 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23021 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23022 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23024 type = push_template_decl (type);
23025 if (type == error_mark_node)
23027 type = NULL_TREE;
23028 goto done;
23032 type = TREE_TYPE (type);
23033 *nested_name_specifier_p = true;
23035 else /* The name is not a nested name. */
23037 /* If the class was unnamed, create a dummy name. */
23038 if (!id)
23039 id = make_anon_name ();
23040 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23041 ? ts_within_enclosing_non_class
23042 : ts_current);
23043 type = xref_tag (class_key, id, tag_scope,
23044 parser->num_template_parameter_lists);
23047 /* Indicate whether this class was declared as a `class' or as a
23048 `struct'. */
23049 if (TREE_CODE (type) == RECORD_TYPE)
23050 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23051 cp_parser_check_class_key (class_key, type);
23053 /* If this type was already complete, and we see another definition,
23054 that's an error. */
23055 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23057 error_at (type_start_token->location, "redefinition of %q#T",
23058 type);
23059 inform (location_of (type), "previous definition of %q#T",
23060 type);
23061 type = NULL_TREE;
23062 goto done;
23064 else if (type == error_mark_node)
23065 type = NULL_TREE;
23067 if (type)
23069 /* Apply attributes now, before any use of the class as a template
23070 argument in its base list. */
23071 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23072 fixup_attribute_variants (type);
23075 /* We will have entered the scope containing the class; the names of
23076 base classes should be looked up in that context. For example:
23078 struct A { struct B {}; struct C; };
23079 struct A::C : B {};
23081 is valid. */
23083 /* Get the list of base-classes, if there is one. */
23084 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23086 /* PR59482: enter the class scope so that base-specifiers are looked
23087 up correctly. */
23088 if (type)
23089 pushclass (type);
23090 bases = cp_parser_base_clause (parser);
23091 /* PR59482: get out of the previously pushed class scope so that the
23092 subsequent pops pop the right thing. */
23093 if (type)
23094 popclass ();
23096 else
23097 bases = NULL_TREE;
23099 /* If we're really defining a class, process the base classes.
23100 If they're invalid, fail. */
23101 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23102 xref_basetypes (type, bases);
23104 done:
23105 /* Leave the scope given by the nested-name-specifier. We will
23106 enter the class scope itself while processing the members. */
23107 if (pushed_scope)
23108 pop_scope (pushed_scope);
23110 if (invalid_explicit_specialization_p)
23112 end_specialization ();
23113 --parser->num_template_parameter_lists;
23116 if (type)
23117 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23118 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23119 CLASSTYPE_FINAL (type) = 1;
23120 out:
23121 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23122 return type;
23125 /* Parse a class-key.
23127 class-key:
23128 class
23129 struct
23130 union
23132 Returns the kind of class-key specified, or none_type to indicate
23133 error. */
23135 static enum tag_types
23136 cp_parser_class_key (cp_parser* parser)
23138 cp_token *token;
23139 enum tag_types tag_type;
23141 /* Look for the class-key. */
23142 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23143 if (!token)
23144 return none_type;
23146 /* Check to see if the TOKEN is a class-key. */
23147 tag_type = cp_parser_token_is_class_key (token);
23148 if (!tag_type)
23149 cp_parser_error (parser, "expected class-key");
23150 return tag_type;
23153 /* Parse a type-parameter-key.
23155 type-parameter-key:
23156 class
23157 typename
23160 static void
23161 cp_parser_type_parameter_key (cp_parser* parser)
23163 /* Look for the type-parameter-key. */
23164 enum tag_types tag_type = none_type;
23165 cp_token *token = cp_lexer_peek_token (parser->lexer);
23166 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23168 cp_lexer_consume_token (parser->lexer);
23169 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23170 /* typename is not allowed in a template template parameter
23171 by the standard until C++17. */
23172 pedwarn (token->location, OPT_Wpedantic,
23173 "ISO C++ forbids typename key in template template parameter;"
23174 " use -std=c++17 or -std=gnu++17");
23176 else
23177 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23179 return;
23182 /* Parse an (optional) member-specification.
23184 member-specification:
23185 member-declaration member-specification [opt]
23186 access-specifier : member-specification [opt] */
23188 static void
23189 cp_parser_member_specification_opt (cp_parser* parser)
23191 while (true)
23193 cp_token *token;
23194 enum rid keyword;
23196 /* Peek at the next token. */
23197 token = cp_lexer_peek_token (parser->lexer);
23198 /* If it's a `}', or EOF then we've seen all the members. */
23199 if (token->type == CPP_CLOSE_BRACE
23200 || token->type == CPP_EOF
23201 || token->type == CPP_PRAGMA_EOL)
23202 break;
23204 /* See if this token is a keyword. */
23205 keyword = token->keyword;
23206 switch (keyword)
23208 case RID_PUBLIC:
23209 case RID_PROTECTED:
23210 case RID_PRIVATE:
23211 /* Consume the access-specifier. */
23212 cp_lexer_consume_token (parser->lexer);
23213 /* Remember which access-specifier is active. */
23214 current_access_specifier = token->u.value;
23215 /* Look for the `:'. */
23216 cp_parser_require (parser, CPP_COLON, RT_COLON);
23217 break;
23219 default:
23220 /* Accept #pragmas at class scope. */
23221 if (token->type == CPP_PRAGMA)
23223 cp_parser_pragma (parser, pragma_member, NULL);
23224 break;
23227 /* Otherwise, the next construction must be a
23228 member-declaration. */
23229 cp_parser_member_declaration (parser);
23234 /* Parse a member-declaration.
23236 member-declaration:
23237 decl-specifier-seq [opt] member-declarator-list [opt] ;
23238 function-definition ; [opt]
23239 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23240 using-declaration
23241 template-declaration
23242 alias-declaration
23244 member-declarator-list:
23245 member-declarator
23246 member-declarator-list , member-declarator
23248 member-declarator:
23249 declarator pure-specifier [opt]
23250 declarator constant-initializer [opt]
23251 identifier [opt] : constant-expression
23253 GNU Extensions:
23255 member-declaration:
23256 __extension__ member-declaration
23258 member-declarator:
23259 declarator attributes [opt] pure-specifier [opt]
23260 declarator attributes [opt] constant-initializer [opt]
23261 identifier [opt] attributes [opt] : constant-expression
23263 C++0x Extensions:
23265 member-declaration:
23266 static_assert-declaration */
23268 static void
23269 cp_parser_member_declaration (cp_parser* parser)
23271 cp_decl_specifier_seq decl_specifiers;
23272 tree prefix_attributes;
23273 tree decl;
23274 int declares_class_or_enum;
23275 bool friend_p;
23276 cp_token *token = NULL;
23277 cp_token *decl_spec_token_start = NULL;
23278 cp_token *initializer_token_start = NULL;
23279 int saved_pedantic;
23280 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23282 /* Check for the `__extension__' keyword. */
23283 if (cp_parser_extension_opt (parser, &saved_pedantic))
23285 /* Recurse. */
23286 cp_parser_member_declaration (parser);
23287 /* Restore the old value of the PEDANTIC flag. */
23288 pedantic = saved_pedantic;
23290 return;
23293 /* Check for a template-declaration. */
23294 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23296 /* An explicit specialization here is an error condition, and we
23297 expect the specialization handler to detect and report this. */
23298 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23299 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23300 cp_parser_explicit_specialization (parser);
23301 else
23302 cp_parser_template_declaration (parser, /*member_p=*/true);
23304 return;
23306 /* Check for a template introduction. */
23307 else if (cp_parser_template_declaration_after_export (parser, true))
23308 return;
23310 /* Check for a using-declaration. */
23311 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23313 if (cxx_dialect < cxx11)
23315 /* Parse the using-declaration. */
23316 cp_parser_using_declaration (parser,
23317 /*access_declaration_p=*/false);
23318 return;
23320 else
23322 tree decl;
23323 bool alias_decl_expected;
23324 cp_parser_parse_tentatively (parser);
23325 decl = cp_parser_alias_declaration (parser);
23326 /* Note that if we actually see the '=' token after the
23327 identifier, cp_parser_alias_declaration commits the
23328 tentative parse. In that case, we really expect an
23329 alias-declaration. Otherwise, we expect a using
23330 declaration. */
23331 alias_decl_expected =
23332 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23333 cp_parser_parse_definitely (parser);
23335 if (alias_decl_expected)
23336 finish_member_declaration (decl);
23337 else
23338 cp_parser_using_declaration (parser,
23339 /*access_declaration_p=*/false);
23340 return;
23344 /* Check for @defs. */
23345 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23347 tree ivar, member;
23348 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23349 ivar = ivar_chains;
23350 while (ivar)
23352 member = ivar;
23353 ivar = TREE_CHAIN (member);
23354 TREE_CHAIN (member) = NULL_TREE;
23355 finish_member_declaration (member);
23357 return;
23360 /* If the next token is `static_assert' we have a static assertion. */
23361 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23363 cp_parser_static_assert (parser, /*member_p=*/true);
23364 return;
23367 parser->colon_corrects_to_scope_p = false;
23369 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23370 goto out;
23372 /* Parse the decl-specifier-seq. */
23373 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23374 cp_parser_decl_specifier_seq (parser,
23375 CP_PARSER_FLAGS_OPTIONAL,
23376 &decl_specifiers,
23377 &declares_class_or_enum);
23378 /* Check for an invalid type-name. */
23379 if (!decl_specifiers.any_type_specifiers_p
23380 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23381 goto out;
23382 /* If there is no declarator, then the decl-specifier-seq should
23383 specify a type. */
23384 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23386 /* If there was no decl-specifier-seq, and the next token is a
23387 `;', then we have something like:
23389 struct S { ; };
23391 [class.mem]
23393 Each member-declaration shall declare at least one member
23394 name of the class. */
23395 if (!decl_specifiers.any_specifiers_p)
23397 cp_token *token = cp_lexer_peek_token (parser->lexer);
23398 if (!in_system_header_at (token->location))
23400 gcc_rich_location richloc (token->location);
23401 richloc.add_fixit_remove ();
23402 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23405 else
23407 tree type;
23409 /* See if this declaration is a friend. */
23410 friend_p = cp_parser_friend_p (&decl_specifiers);
23411 /* If there were decl-specifiers, check to see if there was
23412 a class-declaration. */
23413 type = check_tag_decl (&decl_specifiers,
23414 /*explicit_type_instantiation_p=*/false);
23415 /* Nested classes have already been added to the class, but
23416 a `friend' needs to be explicitly registered. */
23417 if (friend_p)
23419 /* If the `friend' keyword was present, the friend must
23420 be introduced with a class-key. */
23421 if (!declares_class_or_enum && cxx_dialect < cxx11)
23422 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23423 "in C++03 a class-key must be used "
23424 "when declaring a friend");
23425 /* In this case:
23427 template <typename T> struct A {
23428 friend struct A<T>::B;
23431 A<T>::B will be represented by a TYPENAME_TYPE, and
23432 therefore not recognized by check_tag_decl. */
23433 if (!type)
23435 type = decl_specifiers.type;
23436 if (type && TREE_CODE (type) == TYPE_DECL)
23437 type = TREE_TYPE (type);
23439 if (!type || !TYPE_P (type))
23440 error_at (decl_spec_token_start->location,
23441 "friend declaration does not name a class or "
23442 "function");
23443 else
23444 make_friend_class (current_class_type, type,
23445 /*complain=*/true);
23447 /* If there is no TYPE, an error message will already have
23448 been issued. */
23449 else if (!type || type == error_mark_node)
23451 /* An anonymous aggregate has to be handled specially; such
23452 a declaration really declares a data member (with a
23453 particular type), as opposed to a nested class. */
23454 else if (ANON_AGGR_TYPE_P (type))
23456 /* C++11 9.5/6. */
23457 if (decl_specifiers.storage_class != sc_none)
23458 error_at (decl_spec_token_start->location,
23459 "a storage class on an anonymous aggregate "
23460 "in class scope is not allowed");
23462 /* Remove constructors and such from TYPE, now that we
23463 know it is an anonymous aggregate. */
23464 fixup_anonymous_aggr (type);
23465 /* And make the corresponding data member. */
23466 decl = build_decl (decl_spec_token_start->location,
23467 FIELD_DECL, NULL_TREE, type);
23468 /* Add it to the class. */
23469 finish_member_declaration (decl);
23471 else
23472 cp_parser_check_access_in_redeclaration
23473 (TYPE_NAME (type),
23474 decl_spec_token_start->location);
23477 else
23479 bool assume_semicolon = false;
23481 /* Clear attributes from the decl_specifiers but keep them
23482 around as prefix attributes that apply them to the entity
23483 being declared. */
23484 prefix_attributes = decl_specifiers.attributes;
23485 decl_specifiers.attributes = NULL_TREE;
23487 /* See if these declarations will be friends. */
23488 friend_p = cp_parser_friend_p (&decl_specifiers);
23490 /* Keep going until we hit the `;' at the end of the
23491 declaration. */
23492 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23494 tree attributes = NULL_TREE;
23495 tree first_attribute;
23496 tree initializer;
23497 bool is_bitfld = false;
23498 bool named_bitfld = false;
23500 /* Peek at the next token. */
23501 token = cp_lexer_peek_token (parser->lexer);
23503 /* The following code wants to know early if it is a bit-field
23504 or some other declaration. Attributes can appear before
23505 the `:' token, but are hopefully rare enough that the
23506 simplicity of the tentative lookup pays off. */
23507 if (cp_next_tokens_can_be_attribute_p (parser)
23508 || (token->type == CPP_NAME
23509 && cp_nth_tokens_can_be_attribute_p (parser, 2)
23510 && (named_bitfld = true)))
23512 cp_parser_parse_tentatively (parser);
23513 if (named_bitfld)
23514 cp_lexer_consume_token (parser->lexer);
23515 cp_parser_attributes_opt (parser);
23516 token = cp_lexer_peek_token (parser->lexer);
23517 is_bitfld = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
23518 cp_parser_abort_tentative_parse (parser);
23521 /* Check for a bitfield declaration. */
23522 if (is_bitfld
23523 || token->type == CPP_COLON
23524 || (token->type == CPP_NAME
23525 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23526 && (named_bitfld = true)))
23528 tree identifier;
23529 tree width;
23530 tree late_attributes = NULL_TREE;
23532 if (named_bitfld)
23533 identifier = cp_parser_identifier (parser);
23534 else
23535 identifier = NULL_TREE;
23537 /* Look for attributes that apply to the bitfield. */
23538 attributes = cp_parser_attributes_opt (parser);
23540 /* Consume the `:' token. */
23541 cp_lexer_consume_token (parser->lexer);
23543 /* Get the width of the bitfield. */
23544 width = cp_parser_constant_expression (parser, false, NULL,
23545 cxx_dialect >= cxx11);
23547 /* In C++2A and as extension for C++11 and above we allow
23548 default member initializers for bit-fields. */
23549 initializer = NULL_TREE;
23550 if (cxx_dialect >= cxx11
23551 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
23552 || cp_lexer_next_token_is (parser->lexer,
23553 CPP_OPEN_BRACE)))
23555 location_t loc
23556 = cp_lexer_peek_token (parser->lexer)->location;
23557 if (cxx_dialect < cxx2a
23558 && !in_system_header_at (loc)
23559 && identifier != NULL_TREE)
23560 pedwarn (loc, 0,
23561 "default member initializers for bit-fields "
23562 "only available with -std=c++2a or "
23563 "-std=gnu++2a");
23565 initializer = cp_parser_save_nsdmi (parser);
23566 if (identifier == NULL_TREE)
23568 error_at (loc, "default member initializer for "
23569 "unnamed bit-field");
23570 initializer = NULL_TREE;
23573 else
23575 /* Look for attributes that apply to the bitfield after
23576 the `:' token and width. This is where GCC used to
23577 parse attributes in the past, pedwarn if there is
23578 a std attribute. */
23579 if (cp_next_tokens_can_be_std_attribute_p (parser))
23580 pedwarn (input_location, OPT_Wpedantic,
23581 "ISO C++ allows bit-field attributes only "
23582 "before the %<:%> token");
23584 late_attributes = cp_parser_attributes_opt (parser);
23587 attributes = chainon (attributes, late_attributes);
23589 /* Remember which attributes are prefix attributes and
23590 which are not. */
23591 first_attribute = attributes;
23592 /* Combine the attributes. */
23593 attributes = chainon (prefix_attributes, attributes);
23595 /* Create the bitfield declaration. */
23596 decl = grokbitfield (identifier
23597 ? make_id_declarator (NULL_TREE,
23598 identifier,
23599 sfk_none)
23600 : NULL,
23601 &decl_specifiers,
23602 width, initializer,
23603 attributes);
23605 else
23607 cp_declarator *declarator;
23608 tree asm_specification;
23609 int ctor_dtor_or_conv_p;
23611 /* Parse the declarator. */
23612 declarator
23613 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23614 &ctor_dtor_or_conv_p,
23615 /*parenthesized_p=*/NULL,
23616 /*member_p=*/true,
23617 friend_p);
23619 /* If something went wrong parsing the declarator, make sure
23620 that we at least consume some tokens. */
23621 if (declarator == cp_error_declarator)
23623 /* Skip to the end of the statement. */
23624 cp_parser_skip_to_end_of_statement (parser);
23625 /* If the next token is not a semicolon, that is
23626 probably because we just skipped over the body of
23627 a function. So, we consume a semicolon if
23628 present, but do not issue an error message if it
23629 is not present. */
23630 if (cp_lexer_next_token_is (parser->lexer,
23631 CPP_SEMICOLON))
23632 cp_lexer_consume_token (parser->lexer);
23633 goto out;
23636 if (declares_class_or_enum & 2)
23637 cp_parser_check_for_definition_in_return_type
23638 (declarator, decl_specifiers.type,
23639 decl_specifiers.locations[ds_type_spec]);
23641 /* Look for an asm-specification. */
23642 asm_specification = cp_parser_asm_specification_opt (parser);
23643 /* Look for attributes that apply to the declaration. */
23644 attributes = cp_parser_attributes_opt (parser);
23645 /* Remember which attributes are prefix attributes and
23646 which are not. */
23647 first_attribute = attributes;
23648 /* Combine the attributes. */
23649 attributes = chainon (prefix_attributes, attributes);
23651 /* If it's an `=', then we have a constant-initializer or a
23652 pure-specifier. It is not correct to parse the
23653 initializer before registering the member declaration
23654 since the member declaration should be in scope while
23655 its initializer is processed. However, the rest of the
23656 front end does not yet provide an interface that allows
23657 us to handle this correctly. */
23658 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23660 /* In [class.mem]:
23662 A pure-specifier shall be used only in the declaration of
23663 a virtual function.
23665 A member-declarator can contain a constant-initializer
23666 only if it declares a static member of integral or
23667 enumeration type.
23669 Therefore, if the DECLARATOR is for a function, we look
23670 for a pure-specifier; otherwise, we look for a
23671 constant-initializer. When we call `grokfield', it will
23672 perform more stringent semantics checks. */
23673 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23674 if (function_declarator_p (declarator)
23675 || (decl_specifiers.type
23676 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23677 && declarator->kind == cdk_id
23678 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23679 == FUNCTION_TYPE)))
23680 initializer = cp_parser_pure_specifier (parser);
23681 else if (decl_specifiers.storage_class != sc_static)
23682 initializer = cp_parser_save_nsdmi (parser);
23683 else if (cxx_dialect >= cxx11)
23685 bool nonconst;
23686 /* Don't require a constant rvalue in C++11, since we
23687 might want a reference constant. We'll enforce
23688 constancy later. */
23689 cp_lexer_consume_token (parser->lexer);
23690 /* Parse the initializer. */
23691 initializer = cp_parser_initializer_clause (parser,
23692 &nonconst);
23694 else
23695 /* Parse the initializer. */
23696 initializer = cp_parser_constant_initializer (parser);
23698 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23699 && !function_declarator_p (declarator))
23701 bool x;
23702 if (decl_specifiers.storage_class != sc_static)
23703 initializer = cp_parser_save_nsdmi (parser);
23704 else
23705 initializer = cp_parser_initializer (parser, &x, &x);
23707 /* Otherwise, there is no initializer. */
23708 else
23709 initializer = NULL_TREE;
23711 /* See if we are probably looking at a function
23712 definition. We are certainly not looking at a
23713 member-declarator. Calling `grokfield' has
23714 side-effects, so we must not do it unless we are sure
23715 that we are looking at a member-declarator. */
23716 if (cp_parser_token_starts_function_definition_p
23717 (cp_lexer_peek_token (parser->lexer)))
23719 /* The grammar does not allow a pure-specifier to be
23720 used when a member function is defined. (It is
23721 possible that this fact is an oversight in the
23722 standard, since a pure function may be defined
23723 outside of the class-specifier. */
23724 if (initializer && initializer_token_start)
23725 error_at (initializer_token_start->location,
23726 "pure-specifier on function-definition");
23727 decl = cp_parser_save_member_function_body (parser,
23728 &decl_specifiers,
23729 declarator,
23730 attributes);
23731 if (parser->fully_implicit_function_template_p)
23732 decl = finish_fully_implicit_template (parser, decl);
23733 /* If the member was not a friend, declare it here. */
23734 if (!friend_p)
23735 finish_member_declaration (decl);
23736 /* Peek at the next token. */
23737 token = cp_lexer_peek_token (parser->lexer);
23738 /* If the next token is a semicolon, consume it. */
23739 if (token->type == CPP_SEMICOLON)
23741 location_t semicolon_loc
23742 = cp_lexer_consume_token (parser->lexer)->location;
23743 gcc_rich_location richloc (semicolon_loc);
23744 richloc.add_fixit_remove ();
23745 warning_at (&richloc, OPT_Wextra_semi,
23746 "extra %<;%> after in-class "
23747 "function definition");
23749 goto out;
23751 else
23752 if (declarator->kind == cdk_function)
23753 declarator->id_loc = token->location;
23754 /* Create the declaration. */
23755 decl = grokfield (declarator, &decl_specifiers,
23756 initializer, /*init_const_expr_p=*/true,
23757 asm_specification, attributes);
23758 if (parser->fully_implicit_function_template_p)
23760 if (friend_p)
23761 finish_fully_implicit_template (parser, 0);
23762 else
23763 decl = finish_fully_implicit_template (parser, decl);
23767 cp_finalize_omp_declare_simd (parser, decl);
23768 cp_finalize_oacc_routine (parser, decl, false);
23770 /* Reset PREFIX_ATTRIBUTES. */
23771 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23772 attributes = TREE_CHAIN (attributes);
23773 if (attributes)
23774 TREE_CHAIN (attributes) = NULL_TREE;
23776 /* If there is any qualification still in effect, clear it
23777 now; we will be starting fresh with the next declarator. */
23778 parser->scope = NULL_TREE;
23779 parser->qualifying_scope = NULL_TREE;
23780 parser->object_scope = NULL_TREE;
23781 /* If it's a `,', then there are more declarators. */
23782 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23784 cp_lexer_consume_token (parser->lexer);
23785 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23787 cp_token *token = cp_lexer_previous_token (parser->lexer);
23788 gcc_rich_location richloc (token->location);
23789 richloc.add_fixit_remove ();
23790 error_at (&richloc, "stray %<,%> at end of "
23791 "member declaration");
23794 /* If the next token isn't a `;', then we have a parse error. */
23795 else if (cp_lexer_next_token_is_not (parser->lexer,
23796 CPP_SEMICOLON))
23798 /* The next token might be a ways away from where the
23799 actual semicolon is missing. Find the previous token
23800 and use that for our error position. */
23801 cp_token *token = cp_lexer_previous_token (parser->lexer);
23802 gcc_rich_location richloc (token->location);
23803 richloc.add_fixit_insert_after (";");
23804 error_at (&richloc, "expected %<;%> at end of "
23805 "member declaration");
23807 /* Assume that the user meant to provide a semicolon. If
23808 we were to cp_parser_skip_to_end_of_statement, we might
23809 skip to a semicolon inside a member function definition
23810 and issue nonsensical error messages. */
23811 assume_semicolon = true;
23814 if (decl)
23816 /* Add DECL to the list of members. */
23817 if (!friend_p
23818 /* Explicitly include, eg, NSDMIs, for better error
23819 recovery (c++/58650). */
23820 || !DECL_DECLARES_FUNCTION_P (decl))
23821 finish_member_declaration (decl);
23823 if (TREE_CODE (decl) == FUNCTION_DECL)
23824 cp_parser_save_default_args (parser, decl);
23825 else if (TREE_CODE (decl) == FIELD_DECL
23826 && DECL_INITIAL (decl))
23827 /* Add DECL to the queue of NSDMI to be parsed later. */
23828 vec_safe_push (unparsed_nsdmis, decl);
23831 if (assume_semicolon)
23832 goto out;
23836 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23837 out:
23838 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23841 /* Parse a pure-specifier.
23843 pure-specifier:
23846 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23847 Otherwise, ERROR_MARK_NODE is returned. */
23849 static tree
23850 cp_parser_pure_specifier (cp_parser* parser)
23852 cp_token *token;
23854 /* Look for the `=' token. */
23855 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23856 return error_mark_node;
23857 /* Look for the `0' token. */
23858 token = cp_lexer_peek_token (parser->lexer);
23860 if (token->type == CPP_EOF
23861 || token->type == CPP_PRAGMA_EOL)
23862 return error_mark_node;
23864 cp_lexer_consume_token (parser->lexer);
23866 /* Accept = default or = delete in c++0x mode. */
23867 if (token->keyword == RID_DEFAULT
23868 || token->keyword == RID_DELETE)
23870 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
23871 return token->u.value;
23874 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
23875 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
23877 cp_parser_error (parser,
23878 "invalid pure specifier (only %<= 0%> is allowed)");
23879 cp_parser_skip_to_end_of_statement (parser);
23880 return error_mark_node;
23882 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
23884 error_at (token->location, "templates may not be %<virtual%>");
23885 return error_mark_node;
23888 return integer_zero_node;
23891 /* Parse a constant-initializer.
23893 constant-initializer:
23894 = constant-expression
23896 Returns a representation of the constant-expression. */
23898 static tree
23899 cp_parser_constant_initializer (cp_parser* parser)
23901 /* Look for the `=' token. */
23902 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23903 return error_mark_node;
23905 /* It is invalid to write:
23907 struct S { static const int i = { 7 }; };
23910 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23912 cp_parser_error (parser,
23913 "a brace-enclosed initializer is not allowed here");
23914 /* Consume the opening brace. */
23915 matching_braces braces;
23916 braces.consume_open (parser);
23917 /* Skip the initializer. */
23918 cp_parser_skip_to_closing_brace (parser);
23919 /* Look for the trailing `}'. */
23920 braces.require_close (parser);
23922 return error_mark_node;
23925 return cp_parser_constant_expression (parser);
23928 /* Derived classes [gram.class.derived] */
23930 /* Parse a base-clause.
23932 base-clause:
23933 : base-specifier-list
23935 base-specifier-list:
23936 base-specifier ... [opt]
23937 base-specifier-list , base-specifier ... [opt]
23939 Returns a TREE_LIST representing the base-classes, in the order in
23940 which they were declared. The representation of each node is as
23941 described by cp_parser_base_specifier.
23943 In the case that no bases are specified, this function will return
23944 NULL_TREE, not ERROR_MARK_NODE. */
23946 static tree
23947 cp_parser_base_clause (cp_parser* parser)
23949 tree bases = NULL_TREE;
23951 /* Look for the `:' that begins the list. */
23952 cp_parser_require (parser, CPP_COLON, RT_COLON);
23954 /* Scan the base-specifier-list. */
23955 while (true)
23957 cp_token *token;
23958 tree base;
23959 bool pack_expansion_p = false;
23961 /* Look for the base-specifier. */
23962 base = cp_parser_base_specifier (parser);
23963 /* Look for the (optional) ellipsis. */
23964 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23966 /* Consume the `...'. */
23967 cp_lexer_consume_token (parser->lexer);
23969 pack_expansion_p = true;
23972 /* Add BASE to the front of the list. */
23973 if (base && base != error_mark_node)
23975 if (pack_expansion_p)
23976 /* Make this a pack expansion type. */
23977 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
23979 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
23981 TREE_CHAIN (base) = bases;
23982 bases = base;
23985 /* Peek at the next token. */
23986 token = cp_lexer_peek_token (parser->lexer);
23987 /* If it's not a comma, then the list is complete. */
23988 if (token->type != CPP_COMMA)
23989 break;
23990 /* Consume the `,'. */
23991 cp_lexer_consume_token (parser->lexer);
23994 /* PARSER->SCOPE may still be non-NULL at this point, if the last
23995 base class had a qualified name. However, the next name that
23996 appears is certainly not qualified. */
23997 parser->scope = NULL_TREE;
23998 parser->qualifying_scope = NULL_TREE;
23999 parser->object_scope = NULL_TREE;
24001 return nreverse (bases);
24004 /* Parse a base-specifier.
24006 base-specifier:
24007 :: [opt] nested-name-specifier [opt] class-name
24008 virtual access-specifier [opt] :: [opt] nested-name-specifier
24009 [opt] class-name
24010 access-specifier virtual [opt] :: [opt] nested-name-specifier
24011 [opt] class-name
24013 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24014 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24015 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24016 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24018 static tree
24019 cp_parser_base_specifier (cp_parser* parser)
24021 cp_token *token;
24022 bool done = false;
24023 bool virtual_p = false;
24024 bool duplicate_virtual_error_issued_p = false;
24025 bool duplicate_access_error_issued_p = false;
24026 bool class_scope_p, template_p;
24027 tree access = access_default_node;
24028 tree type;
24030 /* Process the optional `virtual' and `access-specifier'. */
24031 while (!done)
24033 /* Peek at the next token. */
24034 token = cp_lexer_peek_token (parser->lexer);
24035 /* Process `virtual'. */
24036 switch (token->keyword)
24038 case RID_VIRTUAL:
24039 /* If `virtual' appears more than once, issue an error. */
24040 if (virtual_p && !duplicate_virtual_error_issued_p)
24042 cp_parser_error (parser,
24043 "%<virtual%> specified more than once in base-specifier");
24044 duplicate_virtual_error_issued_p = true;
24047 virtual_p = true;
24049 /* Consume the `virtual' token. */
24050 cp_lexer_consume_token (parser->lexer);
24052 break;
24054 case RID_PUBLIC:
24055 case RID_PROTECTED:
24056 case RID_PRIVATE:
24057 /* If more than one access specifier appears, issue an
24058 error. */
24059 if (access != access_default_node
24060 && !duplicate_access_error_issued_p)
24062 cp_parser_error (parser,
24063 "more than one access specifier in base-specifier");
24064 duplicate_access_error_issued_p = true;
24067 access = ridpointers[(int) token->keyword];
24069 /* Consume the access-specifier. */
24070 cp_lexer_consume_token (parser->lexer);
24072 break;
24074 default:
24075 done = true;
24076 break;
24079 /* It is not uncommon to see programs mechanically, erroneously, use
24080 the 'typename' keyword to denote (dependent) qualified types
24081 as base classes. */
24082 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24084 token = cp_lexer_peek_token (parser->lexer);
24085 if (!processing_template_decl)
24086 error_at (token->location,
24087 "keyword %<typename%> not allowed outside of templates");
24088 else
24089 error_at (token->location,
24090 "keyword %<typename%> not allowed in this context "
24091 "(the base class is implicitly a type)");
24092 cp_lexer_consume_token (parser->lexer);
24095 /* Look for the optional `::' operator. */
24096 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24097 /* Look for the nested-name-specifier. The simplest way to
24098 implement:
24100 [temp.res]
24102 The keyword `typename' is not permitted in a base-specifier or
24103 mem-initializer; in these contexts a qualified name that
24104 depends on a template-parameter is implicitly assumed to be a
24105 type name.
24107 is to pretend that we have seen the `typename' keyword at this
24108 point. */
24109 cp_parser_nested_name_specifier_opt (parser,
24110 /*typename_keyword_p=*/true,
24111 /*check_dependency_p=*/true,
24112 /*type_p=*/true,
24113 /*is_declaration=*/true);
24114 /* If the base class is given by a qualified name, assume that names
24115 we see are type names or templates, as appropriate. */
24116 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24117 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24119 if (!parser->scope
24120 && cp_lexer_next_token_is_decltype (parser->lexer))
24121 /* DR 950 allows decltype as a base-specifier. */
24122 type = cp_parser_decltype (parser);
24123 else
24125 /* Otherwise, look for the class-name. */
24126 type = cp_parser_class_name (parser,
24127 class_scope_p,
24128 template_p,
24129 typename_type,
24130 /*check_dependency_p=*/true,
24131 /*class_head_p=*/false,
24132 /*is_declaration=*/true);
24133 type = TREE_TYPE (type);
24136 if (type == error_mark_node)
24137 return error_mark_node;
24139 return finish_base_specifier (type, access, virtual_p);
24142 /* Exception handling [gram.exception] */
24144 /* Parse an (optional) noexcept-specification.
24146 noexcept-specification:
24147 noexcept ( constant-expression ) [opt]
24149 If no noexcept-specification is present, returns NULL_TREE.
24150 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24151 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24152 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24153 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24154 in which case a boolean condition is returned instead. */
24156 static tree
24157 cp_parser_noexcept_specification_opt (cp_parser* parser,
24158 bool require_constexpr,
24159 bool* consumed_expr,
24160 bool return_cond)
24162 cp_token *token;
24163 const char *saved_message;
24165 /* Peek at the next token. */
24166 token = cp_lexer_peek_token (parser->lexer);
24168 /* Is it a noexcept-specification? */
24169 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24171 tree expr;
24172 cp_lexer_consume_token (parser->lexer);
24174 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24176 matching_parens parens;
24177 parens.consume_open (parser);
24179 if (require_constexpr)
24181 /* Types may not be defined in an exception-specification. */
24182 saved_message = parser->type_definition_forbidden_message;
24183 parser->type_definition_forbidden_message
24184 = G_("types may not be defined in an exception-specification");
24186 expr = cp_parser_constant_expression (parser);
24188 /* Restore the saved message. */
24189 parser->type_definition_forbidden_message = saved_message;
24191 else
24193 expr = cp_parser_expression (parser);
24194 *consumed_expr = true;
24197 parens.require_close (parser);
24199 else
24201 expr = boolean_true_node;
24202 if (!require_constexpr)
24203 *consumed_expr = false;
24206 /* We cannot build a noexcept-spec right away because this will check
24207 that expr is a constexpr. */
24208 if (!return_cond)
24209 return build_noexcept_spec (expr, tf_warning_or_error);
24210 else
24211 return expr;
24213 else
24214 return NULL_TREE;
24217 /* Parse an (optional) exception-specification.
24219 exception-specification:
24220 throw ( type-id-list [opt] )
24222 Returns a TREE_LIST representing the exception-specification. The
24223 TREE_VALUE of each node is a type. */
24225 static tree
24226 cp_parser_exception_specification_opt (cp_parser* parser)
24228 cp_token *token;
24229 tree type_id_list;
24230 const char *saved_message;
24232 /* Peek at the next token. */
24233 token = cp_lexer_peek_token (parser->lexer);
24235 /* Is it a noexcept-specification? */
24236 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24237 false);
24238 if (type_id_list != NULL_TREE)
24239 return type_id_list;
24241 /* If it's not `throw', then there's no exception-specification. */
24242 if (!cp_parser_is_keyword (token, RID_THROW))
24243 return NULL_TREE;
24245 location_t loc = token->location;
24247 /* Consume the `throw'. */
24248 cp_lexer_consume_token (parser->lexer);
24250 /* Look for the `('. */
24251 matching_parens parens;
24252 parens.require_open (parser);
24254 /* Peek at the next token. */
24255 token = cp_lexer_peek_token (parser->lexer);
24256 /* If it's not a `)', then there is a type-id-list. */
24257 if (token->type != CPP_CLOSE_PAREN)
24259 /* Types may not be defined in an exception-specification. */
24260 saved_message = parser->type_definition_forbidden_message;
24261 parser->type_definition_forbidden_message
24262 = G_("types may not be defined in an exception-specification");
24263 /* Parse the type-id-list. */
24264 type_id_list = cp_parser_type_id_list (parser);
24265 /* Restore the saved message. */
24266 parser->type_definition_forbidden_message = saved_message;
24268 if (cxx_dialect >= cxx17)
24270 error_at (loc, "ISO C++17 does not allow dynamic exception "
24271 "specifications");
24272 type_id_list = NULL_TREE;
24274 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24275 warning_at (loc, OPT_Wdeprecated,
24276 "dynamic exception specifications are deprecated in "
24277 "C++11");
24279 /* In C++17, throw() is equivalent to noexcept (true). throw()
24280 is deprecated in C++11 and above as well, but is still widely used,
24281 so don't warn about it yet. */
24282 else if (cxx_dialect >= cxx17)
24283 type_id_list = noexcept_true_spec;
24284 else
24285 type_id_list = empty_except_spec;
24287 /* Look for the `)'. */
24288 parens.require_close (parser);
24290 return type_id_list;
24293 /* Parse an (optional) type-id-list.
24295 type-id-list:
24296 type-id ... [opt]
24297 type-id-list , type-id ... [opt]
24299 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24300 in the order that the types were presented. */
24302 static tree
24303 cp_parser_type_id_list (cp_parser* parser)
24305 tree types = NULL_TREE;
24307 while (true)
24309 cp_token *token;
24310 tree type;
24312 token = cp_lexer_peek_token (parser->lexer);
24314 /* Get the next type-id. */
24315 type = cp_parser_type_id (parser);
24316 /* Check for invalid 'auto'. */
24317 if (flag_concepts && type_uses_auto (type))
24319 error_at (token->location,
24320 "invalid use of %<auto%> in exception-specification");
24321 type = error_mark_node;
24323 /* Parse the optional ellipsis. */
24324 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24326 /* Consume the `...'. */
24327 cp_lexer_consume_token (parser->lexer);
24329 /* Turn the type into a pack expansion expression. */
24330 type = make_pack_expansion (type);
24332 /* Add it to the list. */
24333 types = add_exception_specifier (types, type, /*complain=*/1);
24334 /* Peek at the next token. */
24335 token = cp_lexer_peek_token (parser->lexer);
24336 /* If it is not a `,', we are done. */
24337 if (token->type != CPP_COMMA)
24338 break;
24339 /* Consume the `,'. */
24340 cp_lexer_consume_token (parser->lexer);
24343 return nreverse (types);
24346 /* Parse a try-block.
24348 try-block:
24349 try compound-statement handler-seq */
24351 static tree
24352 cp_parser_try_block (cp_parser* parser)
24354 tree try_block;
24356 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24357 if (parser->in_function_body
24358 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24359 error ("%<try%> in %<constexpr%> function");
24361 try_block = begin_try_block ();
24362 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24363 finish_try_block (try_block);
24364 cp_parser_handler_seq (parser);
24365 finish_handler_sequence (try_block);
24367 return try_block;
24370 /* Parse a function-try-block.
24372 function-try-block:
24373 try ctor-initializer [opt] function-body handler-seq */
24375 static void
24376 cp_parser_function_try_block (cp_parser* parser)
24378 tree compound_stmt;
24379 tree try_block;
24381 /* Look for the `try' keyword. */
24382 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24383 return;
24384 /* Let the rest of the front end know where we are. */
24385 try_block = begin_function_try_block (&compound_stmt);
24386 /* Parse the function-body. */
24387 cp_parser_ctor_initializer_opt_and_function_body
24388 (parser, /*in_function_try_block=*/true);
24389 /* We're done with the `try' part. */
24390 finish_function_try_block (try_block);
24391 /* Parse the handlers. */
24392 cp_parser_handler_seq (parser);
24393 /* We're done with the handlers. */
24394 finish_function_handler_sequence (try_block, compound_stmt);
24397 /* Parse a handler-seq.
24399 handler-seq:
24400 handler handler-seq [opt] */
24402 static void
24403 cp_parser_handler_seq (cp_parser* parser)
24405 while (true)
24407 cp_token *token;
24409 /* Parse the handler. */
24410 cp_parser_handler (parser);
24411 /* Peek at the next token. */
24412 token = cp_lexer_peek_token (parser->lexer);
24413 /* If it's not `catch' then there are no more handlers. */
24414 if (!cp_parser_is_keyword (token, RID_CATCH))
24415 break;
24419 /* Parse a handler.
24421 handler:
24422 catch ( exception-declaration ) compound-statement */
24424 static void
24425 cp_parser_handler (cp_parser* parser)
24427 tree handler;
24428 tree declaration;
24430 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24431 handler = begin_handler ();
24432 matching_parens parens;
24433 parens.require_open (parser);
24434 declaration = cp_parser_exception_declaration (parser);
24435 finish_handler_parms (declaration, handler);
24436 parens.require_close (parser);
24437 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24438 finish_handler (handler);
24441 /* Parse an exception-declaration.
24443 exception-declaration:
24444 type-specifier-seq declarator
24445 type-specifier-seq abstract-declarator
24446 type-specifier-seq
24449 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24450 ellipsis variant is used. */
24452 static tree
24453 cp_parser_exception_declaration (cp_parser* parser)
24455 cp_decl_specifier_seq type_specifiers;
24456 cp_declarator *declarator;
24457 const char *saved_message;
24459 /* If it's an ellipsis, it's easy to handle. */
24460 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24462 /* Consume the `...' token. */
24463 cp_lexer_consume_token (parser->lexer);
24464 return NULL_TREE;
24467 /* Types may not be defined in exception-declarations. */
24468 saved_message = parser->type_definition_forbidden_message;
24469 parser->type_definition_forbidden_message
24470 = G_("types may not be defined in exception-declarations");
24472 /* Parse the type-specifier-seq. */
24473 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24474 /*is_trailing_return=*/false,
24475 &type_specifiers);
24476 /* If it's a `)', then there is no declarator. */
24477 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24478 declarator = NULL;
24479 else
24480 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24481 /*ctor_dtor_or_conv_p=*/NULL,
24482 /*parenthesized_p=*/NULL,
24483 /*member_p=*/false,
24484 /*friend_p=*/false);
24486 /* Restore the saved message. */
24487 parser->type_definition_forbidden_message = saved_message;
24489 if (!type_specifiers.any_specifiers_p)
24490 return error_mark_node;
24492 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24495 /* Parse a throw-expression.
24497 throw-expression:
24498 throw assignment-expression [opt]
24500 Returns a THROW_EXPR representing the throw-expression. */
24502 static tree
24503 cp_parser_throw_expression (cp_parser* parser)
24505 tree expression;
24506 cp_token* token;
24508 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24509 token = cp_lexer_peek_token (parser->lexer);
24510 /* Figure out whether or not there is an assignment-expression
24511 following the "throw" keyword. */
24512 if (token->type == CPP_COMMA
24513 || token->type == CPP_SEMICOLON
24514 || token->type == CPP_CLOSE_PAREN
24515 || token->type == CPP_CLOSE_SQUARE
24516 || token->type == CPP_CLOSE_BRACE
24517 || token->type == CPP_COLON)
24518 expression = NULL_TREE;
24519 else
24520 expression = cp_parser_assignment_expression (parser);
24522 return build_throw (expression);
24525 /* GNU Extensions */
24527 /* Parse an (optional) asm-specification.
24529 asm-specification:
24530 asm ( string-literal )
24532 If the asm-specification is present, returns a STRING_CST
24533 corresponding to the string-literal. Otherwise, returns
24534 NULL_TREE. */
24536 static tree
24537 cp_parser_asm_specification_opt (cp_parser* parser)
24539 cp_token *token;
24540 tree asm_specification;
24542 /* Peek at the next token. */
24543 token = cp_lexer_peek_token (parser->lexer);
24544 /* If the next token isn't the `asm' keyword, then there's no
24545 asm-specification. */
24546 if (!cp_parser_is_keyword (token, RID_ASM))
24547 return NULL_TREE;
24549 /* Consume the `asm' token. */
24550 cp_lexer_consume_token (parser->lexer);
24551 /* Look for the `('. */
24552 matching_parens parens;
24553 parens.require_open (parser);
24555 /* Look for the string-literal. */
24556 asm_specification = cp_parser_string_literal (parser, false, false);
24558 /* Look for the `)'. */
24559 parens.require_close (parser);
24561 return asm_specification;
24564 /* Parse an asm-operand-list.
24566 asm-operand-list:
24567 asm-operand
24568 asm-operand-list , asm-operand
24570 asm-operand:
24571 string-literal ( expression )
24572 [ string-literal ] string-literal ( expression )
24574 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24575 each node is the expression. The TREE_PURPOSE is itself a
24576 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24577 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24578 is a STRING_CST for the string literal before the parenthesis. Returns
24579 ERROR_MARK_NODE if any of the operands are invalid. */
24581 static tree
24582 cp_parser_asm_operand_list (cp_parser* parser)
24584 tree asm_operands = NULL_TREE;
24585 bool invalid_operands = false;
24587 while (true)
24589 tree string_literal;
24590 tree expression;
24591 tree name;
24593 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24595 /* Consume the `[' token. */
24596 cp_lexer_consume_token (parser->lexer);
24597 /* Read the operand name. */
24598 name = cp_parser_identifier (parser);
24599 if (name != error_mark_node)
24600 name = build_string (IDENTIFIER_LENGTH (name),
24601 IDENTIFIER_POINTER (name));
24602 /* Look for the closing `]'. */
24603 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24605 else
24606 name = NULL_TREE;
24607 /* Look for the string-literal. */
24608 string_literal = cp_parser_string_literal (parser, false, false);
24610 /* Look for the `('. */
24611 matching_parens parens;
24612 parens.require_open (parser);
24613 /* Parse the expression. */
24614 expression = cp_parser_expression (parser);
24615 /* Look for the `)'. */
24616 parens.require_close (parser);
24618 if (name == error_mark_node
24619 || string_literal == error_mark_node
24620 || expression == error_mark_node)
24621 invalid_operands = true;
24623 /* Add this operand to the list. */
24624 asm_operands = tree_cons (build_tree_list (name, string_literal),
24625 expression,
24626 asm_operands);
24627 /* If the next token is not a `,', there are no more
24628 operands. */
24629 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24630 break;
24631 /* Consume the `,'. */
24632 cp_lexer_consume_token (parser->lexer);
24635 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24638 /* Parse an asm-clobber-list.
24640 asm-clobber-list:
24641 string-literal
24642 asm-clobber-list , string-literal
24644 Returns a TREE_LIST, indicating the clobbers in the order that they
24645 appeared. The TREE_VALUE of each node is a STRING_CST. */
24647 static tree
24648 cp_parser_asm_clobber_list (cp_parser* parser)
24650 tree clobbers = NULL_TREE;
24652 while (true)
24654 tree string_literal;
24656 /* Look for the string literal. */
24657 string_literal = cp_parser_string_literal (parser, false, false);
24658 /* Add it to the list. */
24659 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24660 /* If the next token is not a `,', then the list is
24661 complete. */
24662 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24663 break;
24664 /* Consume the `,' token. */
24665 cp_lexer_consume_token (parser->lexer);
24668 return clobbers;
24671 /* Parse an asm-label-list.
24673 asm-label-list:
24674 identifier
24675 asm-label-list , identifier
24677 Returns a TREE_LIST, indicating the labels in the order that they
24678 appeared. The TREE_VALUE of each node is a label. */
24680 static tree
24681 cp_parser_asm_label_list (cp_parser* parser)
24683 tree labels = NULL_TREE;
24685 while (true)
24687 tree identifier, label, name;
24689 /* Look for the identifier. */
24690 identifier = cp_parser_identifier (parser);
24691 if (!error_operand_p (identifier))
24693 label = lookup_label (identifier);
24694 if (TREE_CODE (label) == LABEL_DECL)
24696 TREE_USED (label) = 1;
24697 check_goto (label);
24698 name = build_string (IDENTIFIER_LENGTH (identifier),
24699 IDENTIFIER_POINTER (identifier));
24700 labels = tree_cons (name, label, labels);
24703 /* If the next token is not a `,', then the list is
24704 complete. */
24705 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24706 break;
24707 /* Consume the `,' token. */
24708 cp_lexer_consume_token (parser->lexer);
24711 return nreverse (labels);
24714 /* Return TRUE iff the next tokens in the stream are possibly the
24715 beginning of a GNU extension attribute. */
24717 static bool
24718 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24720 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24723 /* Return TRUE iff the next tokens in the stream are possibly the
24724 beginning of a standard C++-11 attribute specifier. */
24726 static bool
24727 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24729 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24732 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24733 beginning of a standard C++-11 attribute specifier. */
24735 static bool
24736 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24738 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24740 return (cxx_dialect >= cxx11
24741 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24742 || (token->type == CPP_OPEN_SQUARE
24743 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24744 && token->type == CPP_OPEN_SQUARE)));
24747 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24748 beginning of a GNU extension attribute. */
24750 static bool
24751 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24753 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24755 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24758 /* Return true iff the next tokens can be the beginning of either a
24759 GNU attribute list, or a standard C++11 attribute sequence. */
24761 static bool
24762 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
24764 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
24765 || cp_next_tokens_can_be_std_attribute_p (parser));
24768 /* Return true iff the next Nth tokens can be the beginning of either
24769 a GNU attribute list, or a standard C++11 attribute sequence. */
24771 static bool
24772 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
24774 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
24775 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
24778 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24779 of GNU attributes, or return NULL. */
24781 static tree
24782 cp_parser_attributes_opt (cp_parser *parser)
24784 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
24785 return cp_parser_gnu_attributes_opt (parser);
24786 return cp_parser_std_attribute_spec_seq (parser);
24789 /* Parse an (optional) series of attributes.
24791 attributes:
24792 attributes attribute
24794 attribute:
24795 __attribute__ (( attribute-list [opt] ))
24797 The return value is as for cp_parser_gnu_attribute_list. */
24799 static tree
24800 cp_parser_gnu_attributes_opt (cp_parser* parser)
24802 tree attributes = NULL_TREE;
24804 while (true)
24806 cp_token *token;
24807 tree attribute_list;
24808 bool ok = true;
24810 /* Peek at the next token. */
24811 token = cp_lexer_peek_token (parser->lexer);
24812 /* If it's not `__attribute__', then we're done. */
24813 if (token->keyword != RID_ATTRIBUTE)
24814 break;
24816 /* Consume the `__attribute__' keyword. */
24817 cp_lexer_consume_token (parser->lexer);
24818 /* Look for the two `(' tokens. */
24819 matching_parens outer_parens;
24820 outer_parens.require_open (parser);
24821 matching_parens inner_parens;
24822 inner_parens.require_open (parser);
24824 /* Peek at the next token. */
24825 token = cp_lexer_peek_token (parser->lexer);
24826 if (token->type != CPP_CLOSE_PAREN)
24827 /* Parse the attribute-list. */
24828 attribute_list = cp_parser_gnu_attribute_list (parser);
24829 else
24830 /* If the next token is a `)', then there is no attribute
24831 list. */
24832 attribute_list = NULL;
24834 /* Look for the two `)' tokens. */
24835 if (!inner_parens.require_close (parser))
24836 ok = false;
24837 if (!outer_parens.require_close (parser))
24838 ok = false;
24839 if (!ok)
24840 cp_parser_skip_to_end_of_statement (parser);
24842 /* Add these new attributes to the list. */
24843 attributes = chainon (attributes, attribute_list);
24846 return attributes;
24849 /* Parse a GNU attribute-list.
24851 attribute-list:
24852 attribute
24853 attribute-list , attribute
24855 attribute:
24856 identifier
24857 identifier ( identifier )
24858 identifier ( identifier , expression-list )
24859 identifier ( expression-list )
24861 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
24862 to an attribute. The TREE_PURPOSE of each node is the identifier
24863 indicating which attribute is in use. The TREE_VALUE represents
24864 the arguments, if any. */
24866 static tree
24867 cp_parser_gnu_attribute_list (cp_parser* parser)
24869 tree attribute_list = NULL_TREE;
24870 bool save_translate_strings_p = parser->translate_strings_p;
24872 parser->translate_strings_p = false;
24873 while (true)
24875 cp_token *token;
24876 tree identifier;
24877 tree attribute;
24879 /* Look for the identifier. We also allow keywords here; for
24880 example `__attribute__ ((const))' is legal. */
24881 token = cp_lexer_peek_token (parser->lexer);
24882 if (token->type == CPP_NAME
24883 || token->type == CPP_KEYWORD)
24885 tree arguments = NULL_TREE;
24887 /* Consume the token, but save it since we need it for the
24888 SIMD enabled function parsing. */
24889 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
24891 /* Save away the identifier that indicates which attribute
24892 this is. */
24893 identifier = (token->type == CPP_KEYWORD)
24894 /* For keywords, use the canonical spelling, not the
24895 parsed identifier. */
24896 ? ridpointers[(int) token->keyword]
24897 : id_token->u.value;
24899 identifier = canonicalize_attr_name (identifier);
24900 attribute = build_tree_list (identifier, NULL_TREE);
24902 /* Peek at the next token. */
24903 token = cp_lexer_peek_token (parser->lexer);
24904 /* If it's an `(', then parse the attribute arguments. */
24905 if (token->type == CPP_OPEN_PAREN)
24907 vec<tree, va_gc> *vec;
24908 int attr_flag = (attribute_takes_identifier_p (identifier)
24909 ? id_attr : normal_attr);
24910 vec = cp_parser_parenthesized_expression_list
24911 (parser, attr_flag, /*cast_p=*/false,
24912 /*allow_expansion_p=*/false,
24913 /*non_constant_p=*/NULL);
24914 if (vec == NULL)
24915 arguments = error_mark_node;
24916 else
24918 arguments = build_tree_list_vec (vec);
24919 release_tree_vector (vec);
24921 /* Save the arguments away. */
24922 TREE_VALUE (attribute) = arguments;
24925 if (arguments != error_mark_node)
24927 /* Add this attribute to the list. */
24928 TREE_CHAIN (attribute) = attribute_list;
24929 attribute_list = attribute;
24932 token = cp_lexer_peek_token (parser->lexer);
24934 /* Now, look for more attributes. If the next token isn't a
24935 `,', we're done. */
24936 if (token->type != CPP_COMMA)
24937 break;
24939 /* Consume the comma and keep going. */
24940 cp_lexer_consume_token (parser->lexer);
24942 parser->translate_strings_p = save_translate_strings_p;
24944 /* We built up the list in reverse order. */
24945 return nreverse (attribute_list);
24948 /* Parse a standard C++11 attribute.
24950 The returned representation is a TREE_LIST which TREE_PURPOSE is
24951 the scoped name of the attribute, and the TREE_VALUE is its
24952 arguments list.
24954 Note that the scoped name of the attribute is itself a TREE_LIST
24955 which TREE_PURPOSE is the namespace of the attribute, and
24956 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
24957 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
24958 and which TREE_PURPOSE is directly the attribute name.
24960 Clients of the attribute code should use get_attribute_namespace
24961 and get_attribute_name to get the actual namespace and name of
24962 attributes, regardless of their being GNU or C++11 attributes.
24964 attribute:
24965 attribute-token attribute-argument-clause [opt]
24967 attribute-token:
24968 identifier
24969 attribute-scoped-token
24971 attribute-scoped-token:
24972 attribute-namespace :: identifier
24974 attribute-namespace:
24975 identifier
24977 attribute-argument-clause:
24978 ( balanced-token-seq )
24980 balanced-token-seq:
24981 balanced-token [opt]
24982 balanced-token-seq balanced-token
24984 balanced-token:
24985 ( balanced-token-seq )
24986 [ balanced-token-seq ]
24987 { balanced-token-seq }. */
24989 static tree
24990 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
24992 tree attribute, attr_id = NULL_TREE, arguments;
24993 cp_token *token;
24995 /* First, parse name of the attribute, a.k.a attribute-token. */
24997 token = cp_lexer_peek_token (parser->lexer);
24998 if (token->type == CPP_NAME)
24999 attr_id = token->u.value;
25000 else if (token->type == CPP_KEYWORD)
25001 attr_id = ridpointers[(int) token->keyword];
25002 else if (token->flags & NAMED_OP)
25003 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25005 if (attr_id == NULL_TREE)
25006 return NULL_TREE;
25008 cp_lexer_consume_token (parser->lexer);
25010 token = cp_lexer_peek_token (parser->lexer);
25011 if (token->type == CPP_SCOPE)
25013 /* We are seeing a scoped attribute token. */
25015 cp_lexer_consume_token (parser->lexer);
25016 if (attr_ns)
25017 error_at (token->location, "attribute using prefix used together "
25018 "with scoped attribute token");
25019 attr_ns = attr_id;
25021 token = cp_lexer_consume_token (parser->lexer);
25022 if (token->type == CPP_NAME)
25023 attr_id = token->u.value;
25024 else if (token->type == CPP_KEYWORD)
25025 attr_id = ridpointers[(int) token->keyword];
25026 else if (token->flags & NAMED_OP)
25027 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25028 else
25030 error_at (token->location,
25031 "expected an identifier for the attribute name");
25032 return error_mark_node;
25035 attr_id = canonicalize_attr_name (attr_id);
25036 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25037 NULL_TREE);
25038 token = cp_lexer_peek_token (parser->lexer);
25040 else if (attr_ns)
25041 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25042 NULL_TREE);
25043 else
25045 attr_id = canonicalize_attr_name (attr_id);
25046 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25047 NULL_TREE);
25048 /* C++11 noreturn attribute is equivalent to GNU's. */
25049 if (is_attribute_p ("noreturn", attr_id))
25050 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25051 /* C++14 deprecated attribute is equivalent to GNU's. */
25052 else if (is_attribute_p ("deprecated", attr_id))
25053 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25054 /* C++17 fallthrough attribute is equivalent to GNU's. */
25055 else if (is_attribute_p ("fallthrough", attr_id))
25056 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25057 /* Transactional Memory TS optimize_for_synchronized attribute is
25058 equivalent to GNU transaction_callable. */
25059 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25060 TREE_PURPOSE (attribute)
25061 = get_identifier ("transaction_callable");
25062 /* Transactional Memory attributes are GNU attributes. */
25063 else if (tm_attr_to_mask (attr_id))
25064 TREE_PURPOSE (attribute) = attr_id;
25067 /* Now parse the optional argument clause of the attribute. */
25069 if (token->type != CPP_OPEN_PAREN)
25070 return attribute;
25073 vec<tree, va_gc> *vec;
25074 int attr_flag = normal_attr;
25076 if (attr_ns == get_identifier ("gnu")
25077 && attribute_takes_identifier_p (attr_id))
25078 /* A GNU attribute that takes an identifier in parameter. */
25079 attr_flag = id_attr;
25081 vec = cp_parser_parenthesized_expression_list
25082 (parser, attr_flag, /*cast_p=*/false,
25083 /*allow_expansion_p=*/true,
25084 /*non_constant_p=*/NULL);
25085 if (vec == NULL)
25086 arguments = error_mark_node;
25087 else
25089 arguments = build_tree_list_vec (vec);
25090 release_tree_vector (vec);
25093 if (arguments == error_mark_node)
25094 attribute = error_mark_node;
25095 else
25096 TREE_VALUE (attribute) = arguments;
25099 return attribute;
25102 /* Check that the attribute ATTRIBUTE appears at most once in the
25103 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25104 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25105 isn't implemented yet in GCC. */
25107 static void
25108 cp_parser_check_std_attribute (tree attributes, tree attribute)
25110 if (attributes)
25112 tree name = get_attribute_name (attribute);
25113 if (is_attribute_p ("noreturn", name)
25114 && lookup_attribute ("noreturn", attributes))
25115 error ("attribute %<noreturn%> can appear at most once "
25116 "in an attribute-list");
25117 else if (is_attribute_p ("deprecated", name)
25118 && lookup_attribute ("deprecated", attributes))
25119 error ("attribute %<deprecated%> can appear at most once "
25120 "in an attribute-list");
25124 /* Parse a list of standard C++-11 attributes.
25126 attribute-list:
25127 attribute [opt]
25128 attribute-list , attribute[opt]
25129 attribute ...
25130 attribute-list , attribute ...
25133 static tree
25134 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25136 tree attributes = NULL_TREE, attribute = NULL_TREE;
25137 cp_token *token = NULL;
25139 while (true)
25141 attribute = cp_parser_std_attribute (parser, attr_ns);
25142 if (attribute == error_mark_node)
25143 break;
25144 if (attribute != NULL_TREE)
25146 cp_parser_check_std_attribute (attributes, attribute);
25147 TREE_CHAIN (attribute) = attributes;
25148 attributes = attribute;
25150 token = cp_lexer_peek_token (parser->lexer);
25151 if (token->type == CPP_ELLIPSIS)
25153 cp_lexer_consume_token (parser->lexer);
25154 if (attribute == NULL_TREE)
25155 error_at (token->location,
25156 "expected attribute before %<...%>");
25157 else
25159 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25160 if (pack == error_mark_node)
25161 return error_mark_node;
25162 TREE_VALUE (attribute) = pack;
25164 token = cp_lexer_peek_token (parser->lexer);
25166 if (token->type != CPP_COMMA)
25167 break;
25168 cp_lexer_consume_token (parser->lexer);
25170 attributes = nreverse (attributes);
25171 return attributes;
25174 /* Parse a standard C++-11 attribute specifier.
25176 attribute-specifier:
25177 [ [ attribute-using-prefix [opt] attribute-list ] ]
25178 alignment-specifier
25180 attribute-using-prefix:
25181 using attribute-namespace :
25183 alignment-specifier:
25184 alignas ( type-id ... [opt] )
25185 alignas ( alignment-expression ... [opt] ). */
25187 static tree
25188 cp_parser_std_attribute_spec (cp_parser *parser)
25190 tree attributes = NULL_TREE;
25191 cp_token *token = cp_lexer_peek_token (parser->lexer);
25193 if (token->type == CPP_OPEN_SQUARE
25194 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25196 tree attr_ns = NULL_TREE;
25198 cp_lexer_consume_token (parser->lexer);
25199 cp_lexer_consume_token (parser->lexer);
25201 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25203 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25204 if (token->type == CPP_NAME)
25205 attr_ns = token->u.value;
25206 else if (token->type == CPP_KEYWORD)
25207 attr_ns = ridpointers[(int) token->keyword];
25208 else if (token->flags & NAMED_OP)
25209 attr_ns = get_identifier (cpp_type2name (token->type,
25210 token->flags));
25211 if (attr_ns
25212 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25214 if (cxx_dialect < cxx17
25215 && !in_system_header_at (input_location))
25216 pedwarn (input_location, 0,
25217 "attribute using prefix only available "
25218 "with -std=c++17 or -std=gnu++17");
25220 cp_lexer_consume_token (parser->lexer);
25221 cp_lexer_consume_token (parser->lexer);
25222 cp_lexer_consume_token (parser->lexer);
25224 else
25225 attr_ns = NULL_TREE;
25228 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25230 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25231 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25232 cp_parser_skip_to_end_of_statement (parser);
25233 else
25234 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25235 when we are sure that we have actually parsed them. */
25236 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25238 else
25240 tree alignas_expr;
25242 /* Look for an alignment-specifier. */
25244 token = cp_lexer_peek_token (parser->lexer);
25246 if (token->type != CPP_KEYWORD
25247 || token->keyword != RID_ALIGNAS)
25248 return NULL_TREE;
25250 cp_lexer_consume_token (parser->lexer);
25251 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25253 matching_parens parens;
25254 if (!parens.require_open (parser))
25256 cp_parser_error (parser, "expected %<(%>");
25257 return error_mark_node;
25260 cp_parser_parse_tentatively (parser);
25261 alignas_expr = cp_parser_type_id (parser);
25263 if (!cp_parser_parse_definitely (parser))
25265 alignas_expr = cp_parser_assignment_expression (parser);
25266 if (alignas_expr == error_mark_node)
25267 cp_parser_skip_to_end_of_statement (parser);
25268 if (alignas_expr == NULL_TREE
25269 || alignas_expr == error_mark_node)
25270 return alignas_expr;
25273 alignas_expr = cxx_alignas_expr (alignas_expr);
25274 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25276 /* Handle alignas (pack...). */
25277 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25279 cp_lexer_consume_token (parser->lexer);
25280 alignas_expr = make_pack_expansion (alignas_expr);
25283 /* Something went wrong, so don't build the attribute. */
25284 if (alignas_expr == error_mark_node)
25285 return error_mark_node;
25287 if (!parens.require_close (parser))
25289 cp_parser_error (parser, "expected %<)%>");
25290 return error_mark_node;
25293 /* Build the C++-11 representation of an 'aligned'
25294 attribute. */
25295 attributes =
25296 build_tree_list (build_tree_list (get_identifier ("gnu"),
25297 get_identifier ("aligned")),
25298 alignas_expr);
25301 return attributes;
25304 /* Parse a standard C++-11 attribute-specifier-seq.
25306 attribute-specifier-seq:
25307 attribute-specifier-seq [opt] attribute-specifier
25310 static tree
25311 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25313 tree attr_specs = NULL_TREE;
25314 tree attr_last = NULL_TREE;
25316 while (true)
25318 tree attr_spec = cp_parser_std_attribute_spec (parser);
25319 if (attr_spec == NULL_TREE)
25320 break;
25321 if (attr_spec == error_mark_node)
25322 return error_mark_node;
25324 if (attr_last)
25325 TREE_CHAIN (attr_last) = attr_spec;
25326 else
25327 attr_specs = attr_last = attr_spec;
25328 attr_last = tree_last (attr_last);
25331 return attr_specs;
25334 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25335 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25336 current value of the PEDANTIC flag, regardless of whether or not
25337 the `__extension__' keyword is present. The caller is responsible
25338 for restoring the value of the PEDANTIC flag. */
25340 static bool
25341 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25343 /* Save the old value of the PEDANTIC flag. */
25344 *saved_pedantic = pedantic;
25346 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25348 /* Consume the `__extension__' token. */
25349 cp_lexer_consume_token (parser->lexer);
25350 /* We're not being pedantic while the `__extension__' keyword is
25351 in effect. */
25352 pedantic = 0;
25354 return true;
25357 return false;
25360 /* Parse a label declaration.
25362 label-declaration:
25363 __label__ label-declarator-seq ;
25365 label-declarator-seq:
25366 identifier , label-declarator-seq
25367 identifier */
25369 static void
25370 cp_parser_label_declaration (cp_parser* parser)
25372 /* Look for the `__label__' keyword. */
25373 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25375 while (true)
25377 tree identifier;
25379 /* Look for an identifier. */
25380 identifier = cp_parser_identifier (parser);
25381 /* If we failed, stop. */
25382 if (identifier == error_mark_node)
25383 break;
25384 /* Declare it as a label. */
25385 finish_label_decl (identifier);
25386 /* If the next token is a `;', stop. */
25387 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25388 break;
25389 /* Look for the `,' separating the label declarations. */
25390 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25393 /* Look for the final `;'. */
25394 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25397 // -------------------------------------------------------------------------- //
25398 // Requires Clause
25400 // Parse a requires clause.
25402 // requires-clause:
25403 // 'requires' logical-or-expression
25405 // The required logical-or-expression must be a constant expression. Note
25406 // that we don't check that the expression is constepxr here. We defer until
25407 // we analyze constraints and then, we only check atomic constraints.
25408 static tree
25409 cp_parser_requires_clause (cp_parser *parser)
25411 // Parse the requires clause so that it is not automatically folded.
25412 ++processing_template_decl;
25413 tree expr = cp_parser_binary_expression (parser, false, false,
25414 PREC_NOT_OPERATOR, NULL);
25415 if (check_for_bare_parameter_packs (expr))
25416 expr = error_mark_node;
25417 --processing_template_decl;
25418 return expr;
25421 // Optionally parse a requires clause:
25422 static tree
25423 cp_parser_requires_clause_opt (cp_parser *parser)
25425 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25426 if (tok->keyword != RID_REQUIRES)
25428 if (!flag_concepts && tok->type == CPP_NAME
25429 && tok->u.value == ridpointers[RID_REQUIRES])
25431 error_at (cp_lexer_peek_token (parser->lexer)->location,
25432 "%<requires%> only available with -fconcepts");
25433 /* Parse and discard the requires-clause. */
25434 cp_lexer_consume_token (parser->lexer);
25435 cp_parser_requires_clause (parser);
25437 return NULL_TREE;
25439 cp_lexer_consume_token (parser->lexer);
25440 return cp_parser_requires_clause (parser);
25444 /*---------------------------------------------------------------------------
25445 Requires expressions
25446 ---------------------------------------------------------------------------*/
25448 /* Parse a requires expression
25450 requirement-expression:
25451 'requires' requirement-parameter-list [opt] requirement-body */
25452 static tree
25453 cp_parser_requires_expression (cp_parser *parser)
25455 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25456 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25458 /* A requires-expression shall appear only within a concept
25459 definition or a requires-clause.
25461 TODO: Implement this diagnostic correctly. */
25462 if (!processing_template_decl)
25464 error_at (loc, "a requires expression cannot appear outside a template");
25465 cp_parser_skip_to_end_of_statement (parser);
25466 return error_mark_node;
25469 tree parms, reqs;
25471 /* Local parameters are delared as variables within the scope
25472 of the expression. They are not visible past the end of
25473 the expression. Expressions within the requires-expression
25474 are unevaluated. */
25475 struct scope_sentinel
25477 scope_sentinel ()
25479 ++cp_unevaluated_operand;
25480 begin_scope (sk_block, NULL_TREE);
25483 ~scope_sentinel ()
25485 pop_bindings_and_leave_scope ();
25486 --cp_unevaluated_operand;
25488 } s;
25490 /* Parse the optional parameter list. */
25491 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25493 parms = cp_parser_requirement_parameter_list (parser);
25494 if (parms == error_mark_node)
25495 return error_mark_node;
25497 else
25498 parms = NULL_TREE;
25500 /* Parse the requirement body. */
25501 reqs = cp_parser_requirement_body (parser);
25502 if (reqs == error_mark_node)
25503 return error_mark_node;
25506 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25507 the parm chain. */
25508 grokparms (parms, &parms);
25509 return finish_requires_expr (parms, reqs);
25512 /* Parse a parameterized requirement.
25514 requirement-parameter-list:
25515 '(' parameter-declaration-clause ')' */
25516 static tree
25517 cp_parser_requirement_parameter_list (cp_parser *parser)
25519 matching_parens parens;
25520 if (!parens.require_open (parser))
25521 return error_mark_node;
25523 tree parms = cp_parser_parameter_declaration_clause (parser);
25525 if (!parens.require_close (parser))
25526 return error_mark_node;
25528 return parms;
25531 /* Parse the body of a requirement.
25533 requirement-body:
25534 '{' requirement-list '}' */
25535 static tree
25536 cp_parser_requirement_body (cp_parser *parser)
25538 matching_braces braces;
25539 if (!braces.require_open (parser))
25540 return error_mark_node;
25542 tree reqs = cp_parser_requirement_list (parser);
25544 if (!braces.require_close (parser))
25545 return error_mark_node;
25547 return reqs;
25550 /* Parse a list of requirements.
25552 requirement-list:
25553 requirement
25554 requirement-list ';' requirement[opt] */
25555 static tree
25556 cp_parser_requirement_list (cp_parser *parser)
25558 tree result = NULL_TREE;
25559 while (true)
25561 tree req = cp_parser_requirement (parser);
25562 if (req == error_mark_node)
25563 return error_mark_node;
25565 result = tree_cons (NULL_TREE, req, result);
25567 /* If we see a semi-colon, consume it. */
25568 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25569 cp_lexer_consume_token (parser->lexer);
25571 /* Stop processing at the end of the list. */
25572 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25573 break;
25576 /* Reverse the order of requirements so they are analyzed in
25577 declaration order. */
25578 return nreverse (result);
25581 /* Parse a syntactic requirement or type requirement.
25583 requirement:
25584 simple-requirement
25585 compound-requirement
25586 type-requirement
25587 nested-requirement */
25588 static tree
25589 cp_parser_requirement (cp_parser *parser)
25591 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25592 return cp_parser_compound_requirement (parser);
25593 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25594 return cp_parser_type_requirement (parser);
25595 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25596 return cp_parser_nested_requirement (parser);
25597 else
25598 return cp_parser_simple_requirement (parser);
25601 /* Parse a simple requirement.
25603 simple-requirement:
25604 expression ';' */
25605 static tree
25606 cp_parser_simple_requirement (cp_parser *parser)
25608 tree expr = cp_parser_expression (parser, NULL, false, false);
25609 if (!expr || expr == error_mark_node)
25610 return error_mark_node;
25612 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25613 return error_mark_node;
25615 return finish_simple_requirement (expr);
25618 /* Parse a type requirement
25620 type-requirement
25621 nested-name-specifier [opt] required-type-name ';'
25623 required-type-name:
25624 type-name
25625 'template' [opt] simple-template-id */
25626 static tree
25627 cp_parser_type_requirement (cp_parser *parser)
25629 cp_lexer_consume_token (parser->lexer);
25631 // Save the scope before parsing name specifiers.
25632 tree saved_scope = parser->scope;
25633 tree saved_object_scope = parser->object_scope;
25634 tree saved_qualifying_scope = parser->qualifying_scope;
25635 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25636 cp_parser_nested_name_specifier_opt (parser,
25637 /*typename_keyword_p=*/true,
25638 /*check_dependency_p=*/false,
25639 /*type_p=*/true,
25640 /*is_declaration=*/false);
25642 tree type;
25643 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25645 cp_lexer_consume_token (parser->lexer);
25646 type = cp_parser_template_id (parser,
25647 /*template_keyword_p=*/true,
25648 /*check_dependency=*/false,
25649 /*tag_type=*/none_type,
25650 /*is_declaration=*/false);
25651 type = make_typename_type (parser->scope, type, typename_type,
25652 /*complain=*/tf_error);
25654 else
25655 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
25657 if (TREE_CODE (type) == TYPE_DECL)
25658 type = TREE_TYPE (type);
25660 parser->scope = saved_scope;
25661 parser->object_scope = saved_object_scope;
25662 parser->qualifying_scope = saved_qualifying_scope;
25664 if (type == error_mark_node)
25665 cp_parser_skip_to_end_of_statement (parser);
25667 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25668 return error_mark_node;
25669 if (type == error_mark_node)
25670 return error_mark_node;
25672 return finish_type_requirement (type);
25675 /* Parse a compound requirement
25677 compound-requirement:
25678 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25679 static tree
25680 cp_parser_compound_requirement (cp_parser *parser)
25682 /* Parse an expression enclosed in '{ }'s. */
25683 matching_braces braces;
25684 if (!braces.require_open (parser))
25685 return error_mark_node;
25687 tree expr = cp_parser_expression (parser, NULL, false, false);
25688 if (!expr || expr == error_mark_node)
25689 return error_mark_node;
25691 if (!braces.require_close (parser))
25692 return error_mark_node;
25694 /* Parse the optional noexcept. */
25695 bool noexcept_p = false;
25696 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
25698 cp_lexer_consume_token (parser->lexer);
25699 noexcept_p = true;
25702 /* Parse the optional trailing return type. */
25703 tree type = NULL_TREE;
25704 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
25706 cp_lexer_consume_token (parser->lexer);
25707 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
25708 parser->in_result_type_constraint_p = true;
25709 type = cp_parser_trailing_type_id (parser);
25710 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
25711 if (type == error_mark_node)
25712 return error_mark_node;
25715 return finish_compound_requirement (expr, type, noexcept_p);
25718 /* Parse a nested requirement. This is the same as a requires clause.
25720 nested-requirement:
25721 requires-clause */
25722 static tree
25723 cp_parser_nested_requirement (cp_parser *parser)
25725 cp_lexer_consume_token (parser->lexer);
25726 tree req = cp_parser_requires_clause (parser);
25727 if (req == error_mark_node)
25728 return error_mark_node;
25729 return finish_nested_requirement (req);
25732 /* Support Functions */
25734 /* Return the appropriate prefer_type argument for lookup_name_real based on
25735 tag_type and template_mem_access. */
25737 static inline int
25738 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
25740 /* DR 141: When looking in the current enclosing context for a template-name
25741 after -> or ., only consider class templates. */
25742 if (template_mem_access)
25743 return 2;
25744 switch (tag_type)
25746 case none_type: return 0; // No preference.
25747 case scope_type: return 1; // Type or namespace.
25748 default: return 2; // Type only.
25752 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25753 NAME should have one of the representations used for an
25754 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25755 is returned. If PARSER->SCOPE is a dependent type, then a
25756 SCOPE_REF is returned.
25758 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
25759 returned; the name was already resolved when the TEMPLATE_ID_EXPR
25760 was formed. Abstractly, such entities should not be passed to this
25761 function, because they do not need to be looked up, but it is
25762 simpler to check for this special case here, rather than at the
25763 call-sites.
25765 In cases not explicitly covered above, this function returns a
25766 DECL, OVERLOAD, or baselink representing the result of the lookup.
25767 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
25768 is returned.
25770 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
25771 (e.g., "struct") that was used. In that case bindings that do not
25772 refer to types are ignored.
25774 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
25775 ignored.
25777 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
25778 are ignored.
25780 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
25781 types.
25783 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
25784 TREE_LIST of candidates if name-lookup results in an ambiguity, and
25785 NULL_TREE otherwise. */
25787 static cp_expr
25788 cp_parser_lookup_name (cp_parser *parser, tree name,
25789 enum tag_types tag_type,
25790 bool is_template,
25791 bool is_namespace,
25792 bool check_dependency,
25793 tree *ambiguous_decls,
25794 location_t name_location)
25796 tree decl;
25797 tree object_type = parser->context->object_type;
25799 /* Assume that the lookup will be unambiguous. */
25800 if (ambiguous_decls)
25801 *ambiguous_decls = NULL_TREE;
25803 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
25804 no longer valid. Note that if we are parsing tentatively, and
25805 the parse fails, OBJECT_TYPE will be automatically restored. */
25806 parser->context->object_type = NULL_TREE;
25808 if (name == error_mark_node)
25809 return error_mark_node;
25811 /* A template-id has already been resolved; there is no lookup to
25812 do. */
25813 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
25814 return name;
25815 if (BASELINK_P (name))
25817 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
25818 == TEMPLATE_ID_EXPR);
25819 return name;
25822 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
25823 it should already have been checked to make sure that the name
25824 used matches the type being destroyed. */
25825 if (TREE_CODE (name) == BIT_NOT_EXPR)
25827 tree type;
25829 /* Figure out to which type this destructor applies. */
25830 if (parser->scope)
25831 type = parser->scope;
25832 else if (object_type)
25833 type = object_type;
25834 else
25835 type = current_class_type;
25836 /* If that's not a class type, there is no destructor. */
25837 if (!type || !CLASS_TYPE_P (type))
25838 return error_mark_node;
25840 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
25841 lazily_declare_fn (sfk_destructor, type);
25843 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
25844 return dtor;
25846 return error_mark_node;
25849 /* By this point, the NAME should be an ordinary identifier. If
25850 the id-expression was a qualified name, the qualifying scope is
25851 stored in PARSER->SCOPE at this point. */
25852 gcc_assert (identifier_p (name));
25854 /* Perform the lookup. */
25855 if (parser->scope)
25857 bool dependent_p;
25859 if (parser->scope == error_mark_node)
25860 return error_mark_node;
25862 /* If the SCOPE is dependent, the lookup must be deferred until
25863 the template is instantiated -- unless we are explicitly
25864 looking up names in uninstantiated templates. Even then, we
25865 cannot look up the name if the scope is not a class type; it
25866 might, for example, be a template type parameter. */
25867 dependent_p = (TYPE_P (parser->scope)
25868 && dependent_scope_p (parser->scope));
25869 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
25870 && dependent_p)
25871 /* Defer lookup. */
25872 decl = error_mark_node;
25873 else
25875 tree pushed_scope = NULL_TREE;
25877 /* If PARSER->SCOPE is a dependent type, then it must be a
25878 class type, and we must not be checking dependencies;
25879 otherwise, we would have processed this lookup above. So
25880 that PARSER->SCOPE is not considered a dependent base by
25881 lookup_member, we must enter the scope here. */
25882 if (dependent_p)
25883 pushed_scope = push_scope (parser->scope);
25885 /* If the PARSER->SCOPE is a template specialization, it
25886 may be instantiated during name lookup. In that case,
25887 errors may be issued. Even if we rollback the current
25888 tentative parse, those errors are valid. */
25889 decl = lookup_qualified_name (parser->scope, name,
25890 prefer_type_arg (tag_type),
25891 /*complain=*/true);
25893 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
25894 lookup result and the nested-name-specifier nominates a class C:
25895 * if the name specified after the nested-name-specifier, when
25896 looked up in C, is the injected-class-name of C (Clause 9), or
25897 * if the name specified after the nested-name-specifier is the
25898 same as the identifier or the simple-template-id's template-
25899 name in the last component of the nested-name-specifier,
25900 the name is instead considered to name the constructor of
25901 class C. [ Note: for example, the constructor is not an
25902 acceptable lookup result in an elaborated-type-specifier so
25903 the constructor would not be used in place of the
25904 injected-class-name. --end note ] Such a constructor name
25905 shall be used only in the declarator-id of a declaration that
25906 names a constructor or in a using-declaration. */
25907 if (tag_type == none_type
25908 && DECL_SELF_REFERENCE_P (decl)
25909 && same_type_p (DECL_CONTEXT (decl), parser->scope))
25910 decl = lookup_qualified_name (parser->scope, ctor_identifier,
25911 prefer_type_arg (tag_type),
25912 /*complain=*/true);
25914 /* If we have a single function from a using decl, pull it out. */
25915 if (TREE_CODE (decl) == OVERLOAD
25916 && !really_overloaded_fn (decl))
25917 decl = OVL_FUNCTION (decl);
25919 if (pushed_scope)
25920 pop_scope (pushed_scope);
25923 /* If the scope is a dependent type and either we deferred lookup or
25924 we did lookup but didn't find the name, rememeber the name. */
25925 if (decl == error_mark_node && TYPE_P (parser->scope)
25926 && dependent_type_p (parser->scope))
25928 if (tag_type)
25930 tree type;
25932 /* The resolution to Core Issue 180 says that `struct
25933 A::B' should be considered a type-name, even if `A'
25934 is dependent. */
25935 type = make_typename_type (parser->scope, name, tag_type,
25936 /*complain=*/tf_error);
25937 if (type != error_mark_node)
25938 decl = TYPE_NAME (type);
25940 else if (is_template
25941 && (cp_parser_next_token_ends_template_argument_p (parser)
25942 || cp_lexer_next_token_is (parser->lexer,
25943 CPP_CLOSE_PAREN)))
25944 decl = make_unbound_class_template (parser->scope,
25945 name, NULL_TREE,
25946 /*complain=*/tf_error);
25947 else
25948 decl = build_qualified_name (/*type=*/NULL_TREE,
25949 parser->scope, name,
25950 is_template);
25952 parser->qualifying_scope = parser->scope;
25953 parser->object_scope = NULL_TREE;
25955 else if (object_type)
25957 /* Look up the name in the scope of the OBJECT_TYPE, unless the
25958 OBJECT_TYPE is not a class. */
25959 if (CLASS_TYPE_P (object_type))
25960 /* If the OBJECT_TYPE is a template specialization, it may
25961 be instantiated during name lookup. In that case, errors
25962 may be issued. Even if we rollback the current tentative
25963 parse, those errors are valid. */
25964 decl = lookup_member (object_type,
25965 name,
25966 /*protect=*/0,
25967 prefer_type_arg (tag_type),
25968 tf_warning_or_error);
25969 else
25970 decl = NULL_TREE;
25972 if (!decl)
25973 /* Look it up in the enclosing context. DR 141: When looking for a
25974 template-name after -> or ., only consider class templates. */
25975 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
25976 /*nonclass=*/0,
25977 /*block_p=*/true, is_namespace, 0);
25978 if (object_type == unknown_type_node)
25979 /* The object is type-dependent, so we can't look anything up; we used
25980 this to get the DR 141 behavior. */
25981 object_type = NULL_TREE;
25982 parser->object_scope = object_type;
25983 parser->qualifying_scope = NULL_TREE;
25985 else
25987 decl = lookup_name_real (name, prefer_type_arg (tag_type),
25988 /*nonclass=*/0,
25989 /*block_p=*/true, is_namespace, 0);
25990 parser->qualifying_scope = NULL_TREE;
25991 parser->object_scope = NULL_TREE;
25994 /* If the lookup failed, let our caller know. */
25995 if (!decl || decl == error_mark_node)
25996 return error_mark_node;
25998 /* Pull out the template from an injected-class-name (or multiple). */
25999 if (is_template)
26000 decl = maybe_get_template_decl_from_type_decl (decl);
26002 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26003 if (TREE_CODE (decl) == TREE_LIST)
26005 if (ambiguous_decls)
26006 *ambiguous_decls = decl;
26007 /* The error message we have to print is too complicated for
26008 cp_parser_error, so we incorporate its actions directly. */
26009 if (!cp_parser_simulate_error (parser))
26011 error_at (name_location, "reference to %qD is ambiguous",
26012 name);
26013 print_candidates (decl);
26015 return error_mark_node;
26018 gcc_assert (DECL_P (decl)
26019 || TREE_CODE (decl) == OVERLOAD
26020 || TREE_CODE (decl) == SCOPE_REF
26021 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26022 || BASELINK_P (decl));
26024 /* If we have resolved the name of a member declaration, check to
26025 see if the declaration is accessible. When the name resolves to
26026 set of overloaded functions, accessibility is checked when
26027 overload resolution is done.
26029 During an explicit instantiation, access is not checked at all,
26030 as per [temp.explicit]. */
26031 if (DECL_P (decl))
26032 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26034 maybe_record_typedef_use (decl);
26036 return cp_expr (decl, name_location);
26039 /* Like cp_parser_lookup_name, but for use in the typical case where
26040 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26041 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26043 static tree
26044 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26046 return cp_parser_lookup_name (parser, name,
26047 none_type,
26048 /*is_template=*/false,
26049 /*is_namespace=*/false,
26050 /*check_dependency=*/true,
26051 /*ambiguous_decls=*/NULL,
26052 location);
26055 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26056 the current context, return the TYPE_DECL. If TAG_NAME_P is
26057 true, the DECL indicates the class being defined in a class-head,
26058 or declared in an elaborated-type-specifier.
26060 Otherwise, return DECL. */
26062 static tree
26063 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26065 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26066 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26068 struct A {
26069 template <typename T> struct B;
26072 template <typename T> struct A::B {};
26074 Similarly, in an elaborated-type-specifier:
26076 namespace N { struct X{}; }
26078 struct A {
26079 template <typename T> friend struct N::X;
26082 However, if the DECL refers to a class type, and we are in
26083 the scope of the class, then the name lookup automatically
26084 finds the TYPE_DECL created by build_self_reference rather
26085 than a TEMPLATE_DECL. For example, in:
26087 template <class T> struct S {
26088 S s;
26091 there is no need to handle such case. */
26093 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26094 return DECL_TEMPLATE_RESULT (decl);
26096 return decl;
26099 /* If too many, or too few, template-parameter lists apply to the
26100 declarator, issue an error message. Returns TRUE if all went well,
26101 and FALSE otherwise. */
26103 static bool
26104 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26105 cp_declarator *declarator,
26106 location_t declarator_location)
26108 switch (declarator->kind)
26110 case cdk_id:
26112 unsigned num_templates = 0;
26113 tree scope = declarator->u.id.qualifying_scope;
26115 if (scope)
26116 num_templates = num_template_headers_for_class (scope);
26117 else if (TREE_CODE (declarator->u.id.unqualified_name)
26118 == TEMPLATE_ID_EXPR)
26119 /* If the DECLARATOR has the form `X<y>' then it uses one
26120 additional level of template parameters. */
26121 ++num_templates;
26123 return cp_parser_check_template_parameters
26124 (parser, num_templates, declarator_location, declarator);
26127 case cdk_function:
26128 case cdk_array:
26129 case cdk_pointer:
26130 case cdk_reference:
26131 case cdk_ptrmem:
26132 return (cp_parser_check_declarator_template_parameters
26133 (parser, declarator->declarator, declarator_location));
26135 case cdk_decomp:
26136 case cdk_error:
26137 return true;
26139 default:
26140 gcc_unreachable ();
26142 return false;
26145 /* NUM_TEMPLATES were used in the current declaration. If that is
26146 invalid, return FALSE and issue an error messages. Otherwise,
26147 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26148 declarator and we can print more accurate diagnostics. */
26150 static bool
26151 cp_parser_check_template_parameters (cp_parser* parser,
26152 unsigned num_templates,
26153 location_t location,
26154 cp_declarator *declarator)
26156 /* If there are the same number of template classes and parameter
26157 lists, that's OK. */
26158 if (parser->num_template_parameter_lists == num_templates)
26159 return true;
26160 /* If there are more, but only one more, then we are referring to a
26161 member template. That's OK too. */
26162 if (parser->num_template_parameter_lists == num_templates + 1)
26163 return true;
26164 /* If there are more template classes than parameter lists, we have
26165 something like:
26167 template <class T> void S<T>::R<T>::f (); */
26168 if (parser->num_template_parameter_lists < num_templates)
26170 if (declarator && !current_function_decl)
26171 error_at (location, "specializing member %<%T::%E%> "
26172 "requires %<template<>%> syntax",
26173 declarator->u.id.qualifying_scope,
26174 declarator->u.id.unqualified_name);
26175 else if (declarator)
26176 error_at (location, "invalid declaration of %<%T::%E%>",
26177 declarator->u.id.qualifying_scope,
26178 declarator->u.id.unqualified_name);
26179 else
26180 error_at (location, "too few template-parameter-lists");
26181 return false;
26183 /* Otherwise, there are too many template parameter lists. We have
26184 something like:
26186 template <class T> template <class U> void S::f(); */
26187 error_at (location, "too many template-parameter-lists");
26188 return false;
26191 /* Parse an optional `::' token indicating that the following name is
26192 from the global namespace. If so, PARSER->SCOPE is set to the
26193 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26194 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26195 Returns the new value of PARSER->SCOPE, if the `::' token is
26196 present, and NULL_TREE otherwise. */
26198 static tree
26199 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26201 cp_token *token;
26203 /* Peek at the next token. */
26204 token = cp_lexer_peek_token (parser->lexer);
26205 /* If we're looking at a `::' token then we're starting from the
26206 global namespace, not our current location. */
26207 if (token->type == CPP_SCOPE)
26209 /* Consume the `::' token. */
26210 cp_lexer_consume_token (parser->lexer);
26211 /* Set the SCOPE so that we know where to start the lookup. */
26212 parser->scope = global_namespace;
26213 parser->qualifying_scope = global_namespace;
26214 parser->object_scope = NULL_TREE;
26216 return parser->scope;
26218 else if (!current_scope_valid_p)
26220 parser->scope = NULL_TREE;
26221 parser->qualifying_scope = NULL_TREE;
26222 parser->object_scope = NULL_TREE;
26225 return NULL_TREE;
26228 /* Returns TRUE if the upcoming token sequence is the start of a
26229 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26230 declarator is preceded by the `friend' specifier. */
26232 static bool
26233 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26235 bool constructor_p;
26236 bool outside_class_specifier_p;
26237 tree nested_name_specifier;
26238 cp_token *next_token;
26240 /* The common case is that this is not a constructor declarator, so
26241 try to avoid doing lots of work if at all possible. It's not
26242 valid declare a constructor at function scope. */
26243 if (parser->in_function_body)
26244 return false;
26245 /* And only certain tokens can begin a constructor declarator. */
26246 next_token = cp_lexer_peek_token (parser->lexer);
26247 if (next_token->type != CPP_NAME
26248 && next_token->type != CPP_SCOPE
26249 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26250 && next_token->type != CPP_TEMPLATE_ID)
26251 return false;
26253 /* Parse tentatively; we are going to roll back all of the tokens
26254 consumed here. */
26255 cp_parser_parse_tentatively (parser);
26256 /* Assume that we are looking at a constructor declarator. */
26257 constructor_p = true;
26259 /* Look for the optional `::' operator. */
26260 cp_parser_global_scope_opt (parser,
26261 /*current_scope_valid_p=*/false);
26262 /* Look for the nested-name-specifier. */
26263 nested_name_specifier
26264 = (cp_parser_nested_name_specifier_opt (parser,
26265 /*typename_keyword_p=*/false,
26266 /*check_dependency_p=*/false,
26267 /*type_p=*/false,
26268 /*is_declaration=*/false));
26270 outside_class_specifier_p = (!at_class_scope_p ()
26271 || !TYPE_BEING_DEFINED (current_class_type)
26272 || friend_p);
26274 /* Outside of a class-specifier, there must be a
26275 nested-name-specifier. Except in C++17 mode, where we
26276 might be declaring a guiding declaration. */
26277 if (!nested_name_specifier && outside_class_specifier_p
26278 && cxx_dialect < cxx17)
26279 constructor_p = false;
26280 else if (nested_name_specifier == error_mark_node)
26281 constructor_p = false;
26283 /* If we have a class scope, this is easy; DR 147 says that S::S always
26284 names the constructor, and no other qualified name could. */
26285 if (constructor_p && nested_name_specifier
26286 && CLASS_TYPE_P (nested_name_specifier))
26288 tree id = cp_parser_unqualified_id (parser,
26289 /*template_keyword_p=*/false,
26290 /*check_dependency_p=*/false,
26291 /*declarator_p=*/true,
26292 /*optional_p=*/false);
26293 if (is_overloaded_fn (id))
26294 id = DECL_NAME (get_first_fn (id));
26295 if (!constructor_name_p (id, nested_name_specifier))
26296 constructor_p = false;
26298 /* If we still think that this might be a constructor-declarator,
26299 look for a class-name. */
26300 else if (constructor_p)
26302 /* If we have:
26304 template <typename T> struct S {
26305 S();
26308 we must recognize that the nested `S' names a class. */
26309 if (cxx_dialect >= cxx17)
26310 cp_parser_parse_tentatively (parser);
26312 tree type_decl;
26313 type_decl = cp_parser_class_name (parser,
26314 /*typename_keyword_p=*/false,
26315 /*template_keyword_p=*/false,
26316 none_type,
26317 /*check_dependency_p=*/false,
26318 /*class_head_p=*/false,
26319 /*is_declaration=*/false);
26321 if (cxx_dialect >= cxx17
26322 && !cp_parser_parse_definitely (parser))
26324 type_decl = NULL_TREE;
26325 tree tmpl = cp_parser_template_name (parser,
26326 /*template_keyword*/false,
26327 /*check_dependency_p*/false,
26328 /*is_declaration*/false,
26329 none_type,
26330 /*is_identifier*/NULL);
26331 if (DECL_CLASS_TEMPLATE_P (tmpl)
26332 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26333 /* It's a deduction guide, return true. */;
26334 else
26335 cp_parser_simulate_error (parser);
26338 /* If there was no class-name, then this is not a constructor.
26339 Otherwise, if we are in a class-specifier and we aren't
26340 handling a friend declaration, check that its type matches
26341 current_class_type (c++/38313). Note: error_mark_node
26342 is left alone for error recovery purposes. */
26343 constructor_p = (!cp_parser_error_occurred (parser)
26344 && (outside_class_specifier_p
26345 || type_decl == NULL_TREE
26346 || type_decl == error_mark_node
26347 || same_type_p (current_class_type,
26348 TREE_TYPE (type_decl))));
26350 /* If we're still considering a constructor, we have to see a `(',
26351 to begin the parameter-declaration-clause, followed by either a
26352 `)', an `...', or a decl-specifier. We need to check for a
26353 type-specifier to avoid being fooled into thinking that:
26355 S (f) (int);
26357 is a constructor. (It is actually a function named `f' that
26358 takes one parameter (of type `int') and returns a value of type
26359 `S'. */
26360 if (constructor_p
26361 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26362 constructor_p = false;
26364 if (constructor_p
26365 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26366 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26367 /* A parameter declaration begins with a decl-specifier,
26368 which is either the "attribute" keyword, a storage class
26369 specifier, or (usually) a type-specifier. */
26370 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26372 tree type;
26373 tree pushed_scope = NULL_TREE;
26374 unsigned saved_num_template_parameter_lists;
26376 /* Names appearing in the type-specifier should be looked up
26377 in the scope of the class. */
26378 if (current_class_type)
26379 type = NULL_TREE;
26380 else if (type_decl)
26382 type = TREE_TYPE (type_decl);
26383 if (TREE_CODE (type) == TYPENAME_TYPE)
26385 type = resolve_typename_type (type,
26386 /*only_current_p=*/false);
26387 if (TREE_CODE (type) == TYPENAME_TYPE)
26389 cp_parser_abort_tentative_parse (parser);
26390 return false;
26393 pushed_scope = push_scope (type);
26396 /* Inside the constructor parameter list, surrounding
26397 template-parameter-lists do not apply. */
26398 saved_num_template_parameter_lists
26399 = parser->num_template_parameter_lists;
26400 parser->num_template_parameter_lists = 0;
26402 /* Look for the type-specifier. */
26403 cp_parser_type_specifier (parser,
26404 CP_PARSER_FLAGS_NONE,
26405 /*decl_specs=*/NULL,
26406 /*is_declarator=*/true,
26407 /*declares_class_or_enum=*/NULL,
26408 /*is_cv_qualifier=*/NULL);
26410 parser->num_template_parameter_lists
26411 = saved_num_template_parameter_lists;
26413 /* Leave the scope of the class. */
26414 if (pushed_scope)
26415 pop_scope (pushed_scope);
26417 constructor_p = !cp_parser_error_occurred (parser);
26421 /* We did not really want to consume any tokens. */
26422 cp_parser_abort_tentative_parse (parser);
26424 return constructor_p;
26427 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26428 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26429 they must be performed once we are in the scope of the function.
26431 Returns the function defined. */
26433 static tree
26434 cp_parser_function_definition_from_specifiers_and_declarator
26435 (cp_parser* parser,
26436 cp_decl_specifier_seq *decl_specifiers,
26437 tree attributes,
26438 const cp_declarator *declarator)
26440 tree fn;
26441 bool success_p;
26443 /* Begin the function-definition. */
26444 success_p = start_function (decl_specifiers, declarator, attributes);
26446 /* The things we're about to see are not directly qualified by any
26447 template headers we've seen thus far. */
26448 reset_specialization ();
26450 /* If there were names looked up in the decl-specifier-seq that we
26451 did not check, check them now. We must wait until we are in the
26452 scope of the function to perform the checks, since the function
26453 might be a friend. */
26454 perform_deferred_access_checks (tf_warning_or_error);
26456 if (success_p)
26458 cp_finalize_omp_declare_simd (parser, current_function_decl);
26459 parser->omp_declare_simd = NULL;
26460 cp_finalize_oacc_routine (parser, current_function_decl, true);
26461 parser->oacc_routine = NULL;
26464 if (!success_p)
26466 /* Skip the entire function. */
26467 cp_parser_skip_to_end_of_block_or_statement (parser);
26468 fn = error_mark_node;
26470 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26472 /* Seen already, skip it. An error message has already been output. */
26473 cp_parser_skip_to_end_of_block_or_statement (parser);
26474 fn = current_function_decl;
26475 current_function_decl = NULL_TREE;
26476 /* If this is a function from a class, pop the nested class. */
26477 if (current_class_name)
26478 pop_nested_class ();
26480 else
26482 timevar_id_t tv;
26483 if (DECL_DECLARED_INLINE_P (current_function_decl))
26484 tv = TV_PARSE_INLINE;
26485 else
26486 tv = TV_PARSE_FUNC;
26487 timevar_push (tv);
26488 fn = cp_parser_function_definition_after_declarator (parser,
26489 /*inline_p=*/false);
26490 timevar_pop (tv);
26493 return fn;
26496 /* Parse the part of a function-definition that follows the
26497 declarator. INLINE_P is TRUE iff this function is an inline
26498 function defined within a class-specifier.
26500 Returns the function defined. */
26502 static tree
26503 cp_parser_function_definition_after_declarator (cp_parser* parser,
26504 bool inline_p)
26506 tree fn;
26507 bool saved_in_unbraced_linkage_specification_p;
26508 bool saved_in_function_body;
26509 unsigned saved_num_template_parameter_lists;
26510 cp_token *token;
26511 bool fully_implicit_function_template_p
26512 = parser->fully_implicit_function_template_p;
26513 parser->fully_implicit_function_template_p = false;
26514 tree implicit_template_parms
26515 = parser->implicit_template_parms;
26516 parser->implicit_template_parms = 0;
26517 cp_binding_level* implicit_template_scope
26518 = parser->implicit_template_scope;
26519 parser->implicit_template_scope = 0;
26521 saved_in_function_body = parser->in_function_body;
26522 parser->in_function_body = true;
26523 /* If the next token is `return', then the code may be trying to
26524 make use of the "named return value" extension that G++ used to
26525 support. */
26526 token = cp_lexer_peek_token (parser->lexer);
26527 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26529 /* Consume the `return' keyword. */
26530 cp_lexer_consume_token (parser->lexer);
26531 /* Look for the identifier that indicates what value is to be
26532 returned. */
26533 cp_parser_identifier (parser);
26534 /* Issue an error message. */
26535 error_at (token->location,
26536 "named return values are no longer supported");
26537 /* Skip tokens until we reach the start of the function body. */
26538 while (true)
26540 cp_token *token = cp_lexer_peek_token (parser->lexer);
26541 if (token->type == CPP_OPEN_BRACE
26542 || token->type == CPP_EOF
26543 || token->type == CPP_PRAGMA_EOL)
26544 break;
26545 cp_lexer_consume_token (parser->lexer);
26548 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26549 anything declared inside `f'. */
26550 saved_in_unbraced_linkage_specification_p
26551 = parser->in_unbraced_linkage_specification_p;
26552 parser->in_unbraced_linkage_specification_p = false;
26553 /* Inside the function, surrounding template-parameter-lists do not
26554 apply. */
26555 saved_num_template_parameter_lists
26556 = parser->num_template_parameter_lists;
26557 parser->num_template_parameter_lists = 0;
26559 /* If the next token is `try', `__transaction_atomic', or
26560 `__transaction_relaxed`, then we are looking at either function-try-block
26561 or function-transaction-block. Note that all of these include the
26562 function-body. */
26563 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26564 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
26565 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26566 RID_TRANSACTION_RELAXED))
26567 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
26568 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26569 cp_parser_function_try_block (parser);
26570 else
26571 cp_parser_ctor_initializer_opt_and_function_body
26572 (parser, /*in_function_try_block=*/false);
26574 /* Finish the function. */
26575 fn = finish_function (inline_p);
26576 /* Generate code for it, if necessary. */
26577 expand_or_defer_fn (fn);
26578 /* Restore the saved values. */
26579 parser->in_unbraced_linkage_specification_p
26580 = saved_in_unbraced_linkage_specification_p;
26581 parser->num_template_parameter_lists
26582 = saved_num_template_parameter_lists;
26583 parser->in_function_body = saved_in_function_body;
26585 parser->fully_implicit_function_template_p
26586 = fully_implicit_function_template_p;
26587 parser->implicit_template_parms
26588 = implicit_template_parms;
26589 parser->implicit_template_scope
26590 = implicit_template_scope;
26592 if (parser->fully_implicit_function_template_p)
26593 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26595 return fn;
26598 /* Parse a template-declaration body (following argument list). */
26600 static void
26601 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26602 tree parameter_list,
26603 bool member_p)
26605 tree decl = NULL_TREE;
26606 bool friend_p = false;
26608 /* We just processed one more parameter list. */
26609 ++parser->num_template_parameter_lists;
26611 /* Get the deferred access checks from the parameter list. These
26612 will be checked once we know what is being declared, as for a
26613 member template the checks must be performed in the scope of the
26614 class containing the member. */
26615 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26617 /* Tentatively parse for a new template parameter list, which can either be
26618 the template keyword or a template introduction. */
26619 if (cp_parser_template_declaration_after_export (parser, member_p))
26620 /* OK */;
26621 else if (cxx_dialect >= cxx11
26622 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26623 decl = cp_parser_alias_declaration (parser);
26624 else
26626 /* There are no access checks when parsing a template, as we do not
26627 know if a specialization will be a friend. */
26628 push_deferring_access_checks (dk_no_check);
26629 cp_token *token = cp_lexer_peek_token (parser->lexer);
26630 decl = cp_parser_single_declaration (parser,
26631 checks,
26632 member_p,
26633 /*explicit_specialization_p=*/false,
26634 &friend_p);
26635 pop_deferring_access_checks ();
26637 /* If this is a member template declaration, let the front
26638 end know. */
26639 if (member_p && !friend_p && decl)
26641 if (TREE_CODE (decl) == TYPE_DECL)
26642 cp_parser_check_access_in_redeclaration (decl, token->location);
26644 decl = finish_member_template_decl (decl);
26646 else if (friend_p && decl
26647 && DECL_DECLARES_TYPE_P (decl))
26648 make_friend_class (current_class_type, TREE_TYPE (decl),
26649 /*complain=*/true);
26651 /* We are done with the current parameter list. */
26652 --parser->num_template_parameter_lists;
26654 pop_deferring_access_checks ();
26656 /* Finish up. */
26657 finish_template_decl (parameter_list);
26659 /* Check the template arguments for a literal operator template. */
26660 if (decl
26661 && DECL_DECLARES_FUNCTION_P (decl)
26662 && UDLIT_OPER_P (DECL_NAME (decl)))
26664 bool ok = true;
26665 if (parameter_list == NULL_TREE)
26666 ok = false;
26667 else
26669 int num_parms = TREE_VEC_LENGTH (parameter_list);
26670 if (num_parms == 1)
26672 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
26673 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26674 if (TREE_TYPE (parm) != char_type_node
26675 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26676 ok = false;
26678 else if (num_parms == 2 && cxx_dialect >= cxx14)
26680 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
26681 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
26682 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
26683 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26684 if (parm == error_mark_node
26685 || TREE_TYPE (parm) != TREE_TYPE (type)
26686 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26687 ok = false;
26689 else
26690 ok = false;
26692 if (!ok)
26694 if (cxx_dialect >= cxx14)
26695 error ("literal operator template %qD has invalid parameter list."
26696 " Expected non-type template argument pack <char...>"
26697 " or <typename CharT, CharT...>",
26698 decl);
26699 else
26700 error ("literal operator template %qD has invalid parameter list."
26701 " Expected non-type template argument pack <char...>",
26702 decl);
26706 /* Register member declarations. */
26707 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
26708 finish_member_declaration (decl);
26709 /* If DECL is a function template, we must return to parse it later.
26710 (Even though there is no definition, there might be default
26711 arguments that need handling.) */
26712 if (member_p && decl
26713 && DECL_DECLARES_FUNCTION_P (decl))
26714 vec_safe_push (unparsed_funs_with_definitions, decl);
26717 /* Parse a template introduction header for a template-declaration. Returns
26718 false if tentative parse fails. */
26720 static bool
26721 cp_parser_template_introduction (cp_parser* parser, bool member_p)
26723 cp_parser_parse_tentatively (parser);
26725 tree saved_scope = parser->scope;
26726 tree saved_object_scope = parser->object_scope;
26727 tree saved_qualifying_scope = parser->qualifying_scope;
26729 /* Look for the optional `::' operator. */
26730 cp_parser_global_scope_opt (parser,
26731 /*current_scope_valid_p=*/false);
26732 /* Look for the nested-name-specifier. */
26733 cp_parser_nested_name_specifier_opt (parser,
26734 /*typename_keyword_p=*/false,
26735 /*check_dependency_p=*/true,
26736 /*type_p=*/false,
26737 /*is_declaration=*/false);
26739 cp_token *token = cp_lexer_peek_token (parser->lexer);
26740 tree concept_name = cp_parser_identifier (parser);
26742 /* Look up the concept for which we will be matching
26743 template parameters. */
26744 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
26745 token->location);
26746 parser->scope = saved_scope;
26747 parser->object_scope = saved_object_scope;
26748 parser->qualifying_scope = saved_qualifying_scope;
26750 if (concept_name == error_mark_node)
26751 cp_parser_simulate_error (parser);
26753 /* Look for opening brace for introduction. */
26754 matching_braces braces;
26755 braces.require_open (parser);
26757 if (!cp_parser_parse_definitely (parser))
26758 return false;
26760 push_deferring_access_checks (dk_deferred);
26762 /* Build vector of placeholder parameters and grab
26763 matching identifiers. */
26764 tree introduction_list = cp_parser_introduction_list (parser);
26766 /* The introduction-list shall not be empty. */
26767 int nargs = TREE_VEC_LENGTH (introduction_list);
26768 if (nargs == 0)
26770 error ("empty introduction-list");
26771 return true;
26774 /* Look for closing brace for introduction. */
26775 if (!braces.require_close (parser))
26776 return true;
26778 if (tmpl_decl == error_mark_node)
26780 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
26781 token->location);
26782 return true;
26785 /* Build and associate the constraint. */
26786 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
26787 if (parms && parms != error_mark_node)
26789 cp_parser_template_declaration_after_parameters (parser, parms,
26790 member_p);
26791 return true;
26794 error_at (token->location, "no matching concept for template-introduction");
26795 return true;
26798 /* Parse a normal template-declaration following the template keyword. */
26800 static void
26801 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
26803 tree parameter_list;
26804 bool need_lang_pop;
26805 location_t location = input_location;
26807 /* Look for the `<' token. */
26808 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
26809 return;
26810 if (at_class_scope_p () && current_function_decl)
26812 /* 14.5.2.2 [temp.mem]
26814 A local class shall not have member templates. */
26815 error_at (location,
26816 "invalid declaration of member template in local class");
26817 cp_parser_skip_to_end_of_block_or_statement (parser);
26818 return;
26820 /* [temp]
26822 A template ... shall not have C linkage. */
26823 if (current_lang_name == lang_name_c)
26825 error_at (location, "template with C linkage");
26826 maybe_show_extern_c_location ();
26827 /* Give it C++ linkage to avoid confusing other parts of the
26828 front end. */
26829 push_lang_context (lang_name_cplusplus);
26830 need_lang_pop = true;
26832 else
26833 need_lang_pop = false;
26835 /* We cannot perform access checks on the template parameter
26836 declarations until we know what is being declared, just as we
26837 cannot check the decl-specifier list. */
26838 push_deferring_access_checks (dk_deferred);
26840 /* If the next token is `>', then we have an invalid
26841 specialization. Rather than complain about an invalid template
26842 parameter, issue an error message here. */
26843 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
26845 cp_parser_error (parser, "invalid explicit specialization");
26846 begin_specialization ();
26847 parameter_list = NULL_TREE;
26849 else
26851 /* Parse the template parameters. */
26852 parameter_list = cp_parser_template_parameter_list (parser);
26855 /* Look for the `>'. */
26856 cp_parser_skip_to_end_of_template_parameter_list (parser);
26858 /* Manage template requirements */
26859 if (flag_concepts)
26861 tree reqs = get_shorthand_constraints (current_template_parms);
26862 if (tree r = cp_parser_requires_clause_opt (parser))
26863 reqs = conjoin_constraints (reqs, normalize_expression (r));
26864 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
26867 cp_parser_template_declaration_after_parameters (parser, parameter_list,
26868 member_p);
26870 /* For the erroneous case of a template with C linkage, we pushed an
26871 implicit C++ linkage scope; exit that scope now. */
26872 if (need_lang_pop)
26873 pop_lang_context ();
26876 /* Parse a template-declaration, assuming that the `export' (and
26877 `extern') keywords, if present, has already been scanned. MEMBER_P
26878 is as for cp_parser_template_declaration. */
26880 static bool
26881 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
26883 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26885 cp_lexer_consume_token (parser->lexer);
26886 cp_parser_explicit_template_declaration (parser, member_p);
26887 return true;
26889 else if (flag_concepts)
26890 return cp_parser_template_introduction (parser, member_p);
26892 return false;
26895 /* Perform the deferred access checks from a template-parameter-list.
26896 CHECKS is a TREE_LIST of access checks, as returned by
26897 get_deferred_access_checks. */
26899 static void
26900 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
26902 ++processing_template_parmlist;
26903 perform_access_checks (checks, tf_warning_or_error);
26904 --processing_template_parmlist;
26907 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
26908 `function-definition' sequence that follows a template header.
26909 If MEMBER_P is true, this declaration appears in a class scope.
26911 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
26912 *FRIEND_P is set to TRUE iff the declaration is a friend. */
26914 static tree
26915 cp_parser_single_declaration (cp_parser* parser,
26916 vec<deferred_access_check, va_gc> *checks,
26917 bool member_p,
26918 bool explicit_specialization_p,
26919 bool* friend_p)
26921 int declares_class_or_enum;
26922 tree decl = NULL_TREE;
26923 cp_decl_specifier_seq decl_specifiers;
26924 bool function_definition_p = false;
26925 cp_token *decl_spec_token_start;
26927 /* This function is only used when processing a template
26928 declaration. */
26929 gcc_assert (innermost_scope_kind () == sk_template_parms
26930 || innermost_scope_kind () == sk_template_spec);
26932 /* Defer access checks until we know what is being declared. */
26933 push_deferring_access_checks (dk_deferred);
26935 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
26936 alternative. */
26937 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
26938 cp_parser_decl_specifier_seq (parser,
26939 CP_PARSER_FLAGS_OPTIONAL,
26940 &decl_specifiers,
26941 &declares_class_or_enum);
26942 if (friend_p)
26943 *friend_p = cp_parser_friend_p (&decl_specifiers);
26945 /* There are no template typedefs. */
26946 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
26948 error_at (decl_spec_token_start->location,
26949 "template declaration of %<typedef%>");
26950 decl = error_mark_node;
26953 /* Gather up the access checks that occurred the
26954 decl-specifier-seq. */
26955 stop_deferring_access_checks ();
26957 /* Check for the declaration of a template class. */
26958 if (declares_class_or_enum)
26960 if (cp_parser_declares_only_class_p (parser)
26961 || (declares_class_or_enum & 2))
26963 // If this is a declaration, but not a definition, associate
26964 // any constraints with the type declaration. Constraints
26965 // are associated with definitions in cp_parser_class_specifier.
26966 if (declares_class_or_enum == 1)
26967 associate_classtype_constraints (decl_specifiers.type);
26969 decl = shadow_tag (&decl_specifiers);
26971 /* In this case:
26973 struct C {
26974 friend template <typename T> struct A<T>::B;
26977 A<T>::B will be represented by a TYPENAME_TYPE, and
26978 therefore not recognized by shadow_tag. */
26979 if (friend_p && *friend_p
26980 && !decl
26981 && decl_specifiers.type
26982 && TYPE_P (decl_specifiers.type))
26983 decl = decl_specifiers.type;
26985 if (decl && decl != error_mark_node)
26986 decl = TYPE_NAME (decl);
26987 else
26988 decl = error_mark_node;
26990 /* Perform access checks for template parameters. */
26991 cp_parser_perform_template_parameter_access_checks (checks);
26993 /* Give a helpful diagnostic for
26994 template <class T> struct A { } a;
26995 if we aren't already recovering from an error. */
26996 if (!cp_parser_declares_only_class_p (parser)
26997 && !seen_error ())
26999 error_at (cp_lexer_peek_token (parser->lexer)->location,
27000 "a class template declaration must not declare "
27001 "anything else");
27002 cp_parser_skip_to_end_of_block_or_statement (parser);
27003 goto out;
27008 /* Complain about missing 'typename' or other invalid type names. */
27009 if (!decl_specifiers.any_type_specifiers_p
27010 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27012 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27013 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27014 the rest of this declaration. */
27015 decl = error_mark_node;
27016 goto out;
27019 /* If it's not a template class, try for a template function. If
27020 the next token is a `;', then this declaration does not declare
27021 anything. But, if there were errors in the decl-specifiers, then
27022 the error might well have come from an attempted class-specifier.
27023 In that case, there's no need to warn about a missing declarator. */
27024 if (!decl
27025 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27026 || decl_specifiers.type != error_mark_node))
27028 decl = cp_parser_init_declarator (parser,
27029 &decl_specifiers,
27030 checks,
27031 /*function_definition_allowed_p=*/true,
27032 member_p,
27033 declares_class_or_enum,
27034 &function_definition_p,
27035 NULL, NULL, NULL);
27037 /* 7.1.1-1 [dcl.stc]
27039 A storage-class-specifier shall not be specified in an explicit
27040 specialization... */
27041 if (decl
27042 && explicit_specialization_p
27043 && decl_specifiers.storage_class != sc_none)
27045 error_at (decl_spec_token_start->location,
27046 "explicit template specialization cannot have a storage class");
27047 decl = error_mark_node;
27050 if (decl && VAR_P (decl))
27051 check_template_variable (decl);
27054 /* Look for a trailing `;' after the declaration. */
27055 if (!function_definition_p
27056 && (decl == error_mark_node
27057 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27058 cp_parser_skip_to_end_of_block_or_statement (parser);
27060 out:
27061 pop_deferring_access_checks ();
27063 /* Clear any current qualification; whatever comes next is the start
27064 of something new. */
27065 parser->scope = NULL_TREE;
27066 parser->qualifying_scope = NULL_TREE;
27067 parser->object_scope = NULL_TREE;
27069 return decl;
27072 /* Parse a cast-expression that is not the operand of a unary "&". */
27074 static cp_expr
27075 cp_parser_simple_cast_expression (cp_parser *parser)
27077 return cp_parser_cast_expression (parser, /*address_p=*/false,
27078 /*cast_p=*/false, /*decltype*/false, NULL);
27081 /* Parse a functional cast to TYPE. Returns an expression
27082 representing the cast. */
27084 static cp_expr
27085 cp_parser_functional_cast (cp_parser* parser, tree type)
27087 vec<tree, va_gc> *vec;
27088 tree expression_list;
27089 cp_expr cast;
27090 bool nonconst_p;
27092 location_t start_loc = input_location;
27094 if (!type)
27095 type = error_mark_node;
27097 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27099 cp_lexer_set_source_position (parser->lexer);
27100 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27101 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27102 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27103 if (TREE_CODE (type) == TYPE_DECL)
27104 type = TREE_TYPE (type);
27106 cast = finish_compound_literal (type, expression_list,
27107 tf_warning_or_error, fcl_functional);
27108 /* Create a location of the form:
27109 type_name{i, f}
27110 ^~~~~~~~~~~~~~~
27111 with caret == start at the start of the type name,
27112 finishing at the closing brace. */
27113 location_t finish_loc
27114 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27115 location_t combined_loc = make_location (start_loc, start_loc,
27116 finish_loc);
27117 cast.set_location (combined_loc);
27118 return cast;
27122 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27123 /*cast_p=*/true,
27124 /*allow_expansion_p=*/true,
27125 /*non_constant_p=*/NULL);
27126 if (vec == NULL)
27127 expression_list = error_mark_node;
27128 else
27130 expression_list = build_tree_list_vec (vec);
27131 release_tree_vector (vec);
27134 cast = build_functional_cast (type, expression_list,
27135 tf_warning_or_error);
27136 /* [expr.const]/1: In an integral constant expression "only type
27137 conversions to integral or enumeration type can be used". */
27138 if (TREE_CODE (type) == TYPE_DECL)
27139 type = TREE_TYPE (type);
27140 if (cast != error_mark_node
27141 && !cast_valid_in_integral_constant_expression_p (type)
27142 && cp_parser_non_integral_constant_expression (parser,
27143 NIC_CONSTRUCTOR))
27144 return error_mark_node;
27146 /* Create a location of the form:
27147 float(i)
27148 ^~~~~~~~
27149 with caret == start at the start of the type name,
27150 finishing at the closing paren. */
27151 location_t finish_loc
27152 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27153 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27154 cast.set_location (combined_loc);
27155 return cast;
27158 /* Save the tokens that make up the body of a member function defined
27159 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27160 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27161 specifiers applied to the declaration. Returns the FUNCTION_DECL
27162 for the member function. */
27164 static tree
27165 cp_parser_save_member_function_body (cp_parser* parser,
27166 cp_decl_specifier_seq *decl_specifiers,
27167 cp_declarator *declarator,
27168 tree attributes)
27170 cp_token *first;
27171 cp_token *last;
27172 tree fn;
27173 bool function_try_block = false;
27175 /* Create the FUNCTION_DECL. */
27176 fn = grokmethod (decl_specifiers, declarator, attributes);
27177 cp_finalize_omp_declare_simd (parser, fn);
27178 cp_finalize_oacc_routine (parser, fn, true);
27179 /* If something went badly wrong, bail out now. */
27180 if (fn == error_mark_node)
27182 /* If there's a function-body, skip it. */
27183 if (cp_parser_token_starts_function_definition_p
27184 (cp_lexer_peek_token (parser->lexer)))
27185 cp_parser_skip_to_end_of_block_or_statement (parser);
27186 return error_mark_node;
27189 /* Remember it, if there default args to post process. */
27190 cp_parser_save_default_args (parser, fn);
27192 /* Save away the tokens that make up the body of the
27193 function. */
27194 first = parser->lexer->next_token;
27196 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27197 cp_lexer_consume_token (parser->lexer);
27198 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27199 RID_TRANSACTION_ATOMIC))
27201 cp_lexer_consume_token (parser->lexer);
27202 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27203 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27204 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27205 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27206 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27207 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27208 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27210 cp_lexer_consume_token (parser->lexer);
27211 cp_lexer_consume_token (parser->lexer);
27212 cp_lexer_consume_token (parser->lexer);
27213 cp_lexer_consume_token (parser->lexer);
27214 cp_lexer_consume_token (parser->lexer);
27216 else
27217 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27218 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27220 cp_lexer_consume_token (parser->lexer);
27221 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27222 break;
27226 /* Handle function try blocks. */
27227 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27229 cp_lexer_consume_token (parser->lexer);
27230 function_try_block = true;
27232 /* We can have braced-init-list mem-initializers before the fn body. */
27233 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27235 cp_lexer_consume_token (parser->lexer);
27236 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27238 /* cache_group will stop after an un-nested { } pair, too. */
27239 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27240 break;
27242 /* variadic mem-inits have ... after the ')'. */
27243 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27244 cp_lexer_consume_token (parser->lexer);
27247 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27248 /* Handle function try blocks. */
27249 if (function_try_block)
27250 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27251 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27252 last = parser->lexer->next_token;
27254 /* Save away the inline definition; we will process it when the
27255 class is complete. */
27256 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27257 DECL_PENDING_INLINE_P (fn) = 1;
27259 /* We need to know that this was defined in the class, so that
27260 friend templates are handled correctly. */
27261 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27263 /* Add FN to the queue of functions to be parsed later. */
27264 vec_safe_push (unparsed_funs_with_definitions, fn);
27266 return fn;
27269 /* Save the tokens that make up the in-class initializer for a non-static
27270 data member. Returns a DEFAULT_ARG. */
27272 static tree
27273 cp_parser_save_nsdmi (cp_parser* parser)
27275 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27278 /* Parse a template-argument-list, as well as the trailing ">" (but
27279 not the opening "<"). See cp_parser_template_argument_list for the
27280 return value. */
27282 static tree
27283 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27285 tree arguments;
27286 tree saved_scope;
27287 tree saved_qualifying_scope;
27288 tree saved_object_scope;
27289 bool saved_greater_than_is_operator_p;
27290 int saved_unevaluated_operand;
27291 int saved_inhibit_evaluation_warnings;
27293 /* [temp.names]
27295 When parsing a template-id, the first non-nested `>' is taken as
27296 the end of the template-argument-list rather than a greater-than
27297 operator. */
27298 saved_greater_than_is_operator_p
27299 = parser->greater_than_is_operator_p;
27300 parser->greater_than_is_operator_p = false;
27301 /* Parsing the argument list may modify SCOPE, so we save it
27302 here. */
27303 saved_scope = parser->scope;
27304 saved_qualifying_scope = parser->qualifying_scope;
27305 saved_object_scope = parser->object_scope;
27306 /* We need to evaluate the template arguments, even though this
27307 template-id may be nested within a "sizeof". */
27308 saved_unevaluated_operand = cp_unevaluated_operand;
27309 cp_unevaluated_operand = 0;
27310 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27311 c_inhibit_evaluation_warnings = 0;
27312 /* Parse the template-argument-list itself. */
27313 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27314 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27315 arguments = NULL_TREE;
27316 else
27317 arguments = cp_parser_template_argument_list (parser);
27318 /* Look for the `>' that ends the template-argument-list. If we find
27319 a '>>' instead, it's probably just a typo. */
27320 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27322 if (cxx_dialect != cxx98)
27324 /* In C++0x, a `>>' in a template argument list or cast
27325 expression is considered to be two separate `>'
27326 tokens. So, change the current token to a `>', but don't
27327 consume it: it will be consumed later when the outer
27328 template argument list (or cast expression) is parsed.
27329 Note that this replacement of `>' for `>>' is necessary
27330 even if we are parsing tentatively: in the tentative
27331 case, after calling
27332 cp_parser_enclosed_template_argument_list we will always
27333 throw away all of the template arguments and the first
27334 closing `>', either because the template argument list
27335 was erroneous or because we are replacing those tokens
27336 with a CPP_TEMPLATE_ID token. The second `>' (which will
27337 not have been thrown away) is needed either to close an
27338 outer template argument list or to complete a new-style
27339 cast. */
27340 cp_token *token = cp_lexer_peek_token (parser->lexer);
27341 token->type = CPP_GREATER;
27343 else if (!saved_greater_than_is_operator_p)
27345 /* If we're in a nested template argument list, the '>>' has
27346 to be a typo for '> >'. We emit the error message, but we
27347 continue parsing and we push a '>' as next token, so that
27348 the argument list will be parsed correctly. Note that the
27349 global source location is still on the token before the
27350 '>>', so we need to say explicitly where we want it. */
27351 cp_token *token = cp_lexer_peek_token (parser->lexer);
27352 gcc_rich_location richloc (token->location);
27353 richloc.add_fixit_replace ("> >");
27354 error_at (&richloc, "%<>>%> should be %<> >%> "
27355 "within a nested template argument list");
27357 token->type = CPP_GREATER;
27359 else
27361 /* If this is not a nested template argument list, the '>>'
27362 is a typo for '>'. Emit an error message and continue.
27363 Same deal about the token location, but here we can get it
27364 right by consuming the '>>' before issuing the diagnostic. */
27365 cp_token *token = cp_lexer_consume_token (parser->lexer);
27366 error_at (token->location,
27367 "spurious %<>>%>, use %<>%> to terminate "
27368 "a template argument list");
27371 else
27372 cp_parser_skip_to_end_of_template_parameter_list (parser);
27373 /* The `>' token might be a greater-than operator again now. */
27374 parser->greater_than_is_operator_p
27375 = saved_greater_than_is_operator_p;
27376 /* Restore the SAVED_SCOPE. */
27377 parser->scope = saved_scope;
27378 parser->qualifying_scope = saved_qualifying_scope;
27379 parser->object_scope = saved_object_scope;
27380 cp_unevaluated_operand = saved_unevaluated_operand;
27381 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27383 return arguments;
27386 /* MEMBER_FUNCTION is a member function, or a friend. If default
27387 arguments, or the body of the function have not yet been parsed,
27388 parse them now. */
27390 static void
27391 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27393 timevar_push (TV_PARSE_INMETH);
27394 /* If this member is a template, get the underlying
27395 FUNCTION_DECL. */
27396 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27397 member_function = DECL_TEMPLATE_RESULT (member_function);
27399 /* There should not be any class definitions in progress at this
27400 point; the bodies of members are only parsed outside of all class
27401 definitions. */
27402 gcc_assert (parser->num_classes_being_defined == 0);
27403 /* While we're parsing the member functions we might encounter more
27404 classes. We want to handle them right away, but we don't want
27405 them getting mixed up with functions that are currently in the
27406 queue. */
27407 push_unparsed_function_queues (parser);
27409 /* Make sure that any template parameters are in scope. */
27410 maybe_begin_member_template_processing (member_function);
27412 /* If the body of the function has not yet been parsed, parse it
27413 now. */
27414 if (DECL_PENDING_INLINE_P (member_function))
27416 tree function_scope;
27417 cp_token_cache *tokens;
27419 /* The function is no longer pending; we are processing it. */
27420 tokens = DECL_PENDING_INLINE_INFO (member_function);
27421 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27422 DECL_PENDING_INLINE_P (member_function) = 0;
27424 /* If this is a local class, enter the scope of the containing
27425 function. */
27426 function_scope = current_function_decl;
27427 if (function_scope)
27428 push_function_context ();
27430 /* Push the body of the function onto the lexer stack. */
27431 cp_parser_push_lexer_for_tokens (parser, tokens);
27433 /* Let the front end know that we going to be defining this
27434 function. */
27435 start_preparsed_function (member_function, NULL_TREE,
27436 SF_PRE_PARSED | SF_INCLASS_INLINE);
27438 /* Don't do access checking if it is a templated function. */
27439 if (processing_template_decl)
27440 push_deferring_access_checks (dk_no_check);
27442 /* #pragma omp declare reduction needs special parsing. */
27443 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27445 parser->lexer->in_pragma = true;
27446 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27447 finish_function (/*inline_p=*/true);
27448 cp_check_omp_declare_reduction (member_function);
27450 else
27451 /* Now, parse the body of the function. */
27452 cp_parser_function_definition_after_declarator (parser,
27453 /*inline_p=*/true);
27455 if (processing_template_decl)
27456 pop_deferring_access_checks ();
27458 /* Leave the scope of the containing function. */
27459 if (function_scope)
27460 pop_function_context ();
27461 cp_parser_pop_lexer (parser);
27464 /* Remove any template parameters from the symbol table. */
27465 maybe_end_member_template_processing ();
27467 /* Restore the queue. */
27468 pop_unparsed_function_queues (parser);
27469 timevar_pop (TV_PARSE_INMETH);
27472 /* If DECL contains any default args, remember it on the unparsed
27473 functions queue. */
27475 static void
27476 cp_parser_save_default_args (cp_parser* parser, tree decl)
27478 tree probe;
27480 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27481 probe;
27482 probe = TREE_CHAIN (probe))
27483 if (TREE_PURPOSE (probe))
27485 cp_default_arg_entry entry = {current_class_type, decl};
27486 vec_safe_push (unparsed_funs_with_default_args, entry);
27487 break;
27491 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27492 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27493 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27494 from the parameter-type-list. */
27496 static tree
27497 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27498 tree default_arg, tree parmtype)
27500 cp_token_cache *tokens;
27501 tree parsed_arg;
27502 bool dummy;
27504 if (default_arg == error_mark_node)
27505 return error_mark_node;
27507 /* Push the saved tokens for the default argument onto the parser's
27508 lexer stack. */
27509 tokens = DEFARG_TOKENS (default_arg);
27510 cp_parser_push_lexer_for_tokens (parser, tokens);
27512 start_lambda_scope (decl);
27514 /* Parse the default argument. */
27515 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27516 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27517 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27519 finish_lambda_scope ();
27521 if (parsed_arg == error_mark_node)
27522 cp_parser_skip_to_end_of_statement (parser);
27524 if (!processing_template_decl)
27526 /* In a non-template class, check conversions now. In a template,
27527 we'll wait and instantiate these as needed. */
27528 if (TREE_CODE (decl) == PARM_DECL)
27529 parsed_arg = check_default_argument (parmtype, parsed_arg,
27530 tf_warning_or_error);
27531 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27532 parsed_arg = error_mark_node;
27533 else
27534 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
27537 /* If the token stream has not been completely used up, then
27538 there was extra junk after the end of the default
27539 argument. */
27540 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27542 if (TREE_CODE (decl) == PARM_DECL)
27543 cp_parser_error (parser, "expected %<,%>");
27544 else
27545 cp_parser_error (parser, "expected %<;%>");
27548 /* Revert to the main lexer. */
27549 cp_parser_pop_lexer (parser);
27551 return parsed_arg;
27554 /* FIELD is a non-static data member with an initializer which we saved for
27555 later; parse it now. */
27557 static void
27558 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27560 tree def;
27562 maybe_begin_member_template_processing (field);
27564 push_unparsed_function_queues (parser);
27565 def = cp_parser_late_parse_one_default_arg (parser, field,
27566 DECL_INITIAL (field),
27567 NULL_TREE);
27568 pop_unparsed_function_queues (parser);
27570 maybe_end_member_template_processing ();
27572 DECL_INITIAL (field) = def;
27575 /* FN is a FUNCTION_DECL which may contains a parameter with an
27576 unparsed DEFAULT_ARG. Parse the default args now. This function
27577 assumes that the current scope is the scope in which the default
27578 argument should be processed. */
27580 static void
27581 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27583 bool saved_local_variables_forbidden_p;
27584 tree parm, parmdecl;
27586 /* While we're parsing the default args, we might (due to the
27587 statement expression extension) encounter more classes. We want
27588 to handle them right away, but we don't want them getting mixed
27589 up with default args that are currently in the queue. */
27590 push_unparsed_function_queues (parser);
27592 /* Local variable names (and the `this' keyword) may not appear
27593 in a default argument. */
27594 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27595 parser->local_variables_forbidden_p = true;
27597 push_defarg_context (fn);
27599 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27600 parmdecl = DECL_ARGUMENTS (fn);
27601 parm && parm != void_list_node;
27602 parm = TREE_CHAIN (parm),
27603 parmdecl = DECL_CHAIN (parmdecl))
27605 tree default_arg = TREE_PURPOSE (parm);
27606 tree parsed_arg;
27607 vec<tree, va_gc> *insts;
27608 tree copy;
27609 unsigned ix;
27611 if (!default_arg)
27612 continue;
27614 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27615 /* This can happen for a friend declaration for a function
27616 already declared with default arguments. */
27617 continue;
27619 parsed_arg
27620 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27621 default_arg,
27622 TREE_VALUE (parm));
27623 TREE_PURPOSE (parm) = parsed_arg;
27625 /* Update any instantiations we've already created. */
27626 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27627 vec_safe_iterate (insts, ix, &copy); ix++)
27628 TREE_PURPOSE (copy) = parsed_arg;
27631 pop_defarg_context ();
27633 /* Make sure no default arg is missing. */
27634 check_default_args (fn);
27636 /* Restore the state of local_variables_forbidden_p. */
27637 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27639 /* Restore the queue. */
27640 pop_unparsed_function_queues (parser);
27643 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27645 sizeof ... ( identifier )
27647 where the 'sizeof' token has already been consumed. */
27649 static tree
27650 cp_parser_sizeof_pack (cp_parser *parser)
27652 /* Consume the `...'. */
27653 cp_lexer_consume_token (parser->lexer);
27654 maybe_warn_variadic_templates ();
27656 matching_parens parens;
27657 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
27658 if (paren)
27659 parens.consume_open (parser);
27660 else
27661 permerror (cp_lexer_peek_token (parser->lexer)->location,
27662 "%<sizeof...%> argument must be surrounded by parentheses");
27664 cp_token *token = cp_lexer_peek_token (parser->lexer);
27665 tree name = cp_parser_identifier (parser);
27666 if (name == error_mark_node)
27667 return error_mark_node;
27668 /* The name is not qualified. */
27669 parser->scope = NULL_TREE;
27670 parser->qualifying_scope = NULL_TREE;
27671 parser->object_scope = NULL_TREE;
27672 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
27673 if (expr == error_mark_node)
27674 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
27675 token->location);
27676 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
27677 expr = TREE_TYPE (expr);
27678 else if (TREE_CODE (expr) == CONST_DECL)
27679 expr = DECL_INITIAL (expr);
27680 expr = make_pack_expansion (expr);
27681 PACK_EXPANSION_SIZEOF_P (expr) = true;
27683 if (paren)
27684 parens.require_close (parser);
27686 return expr;
27689 /* Parse the operand of `sizeof' (or a similar operator). Returns
27690 either a TYPE or an expression, depending on the form of the
27691 input. The KEYWORD indicates which kind of expression we have
27692 encountered. */
27694 static tree
27695 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
27697 tree expr = NULL_TREE;
27698 const char *saved_message;
27699 char *tmp;
27700 bool saved_integral_constant_expression_p;
27701 bool saved_non_integral_constant_expression_p;
27703 /* If it's a `...', then we are computing the length of a parameter
27704 pack. */
27705 if (keyword == RID_SIZEOF
27706 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27707 return cp_parser_sizeof_pack (parser);
27709 /* Types cannot be defined in a `sizeof' expression. Save away the
27710 old message. */
27711 saved_message = parser->type_definition_forbidden_message;
27712 /* And create the new one. */
27713 tmp = concat ("types may not be defined in %<",
27714 IDENTIFIER_POINTER (ridpointers[keyword]),
27715 "%> expressions", NULL);
27716 parser->type_definition_forbidden_message = tmp;
27718 /* The restrictions on constant-expressions do not apply inside
27719 sizeof expressions. */
27720 saved_integral_constant_expression_p
27721 = parser->integral_constant_expression_p;
27722 saved_non_integral_constant_expression_p
27723 = parser->non_integral_constant_expression_p;
27724 parser->integral_constant_expression_p = false;
27726 /* Do not actually evaluate the expression. */
27727 ++cp_unevaluated_operand;
27728 ++c_inhibit_evaluation_warnings;
27729 /* If it's a `(', then we might be looking at the type-id
27730 construction. */
27731 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27733 tree type = NULL_TREE;
27735 /* We can't be sure yet whether we're looking at a type-id or an
27736 expression. */
27737 cp_parser_parse_tentatively (parser);
27739 matching_parens parens;
27740 parens.consume_open (parser);
27742 /* Note: as a GNU Extension, compound literals are considered
27743 postfix-expressions as they are in C99, so they are valid
27744 arguments to sizeof. See comment in cp_parser_cast_expression
27745 for details. */
27746 if (cp_parser_compound_literal_p (parser))
27747 cp_parser_simulate_error (parser);
27748 else
27750 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
27751 parser->in_type_id_in_expr_p = true;
27752 /* Look for the type-id. */
27753 type = cp_parser_type_id (parser);
27754 /* Look for the closing `)'. */
27755 parens.require_close (parser);
27756 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
27759 /* If all went well, then we're done. */
27760 if (cp_parser_parse_definitely (parser))
27762 cp_decl_specifier_seq decl_specs;
27764 /* Build a trivial decl-specifier-seq. */
27765 clear_decl_specs (&decl_specs);
27766 decl_specs.type = type;
27768 /* Call grokdeclarator to figure out what type this is. */
27769 expr = grokdeclarator (NULL,
27770 &decl_specs,
27771 TYPENAME,
27772 /*initialized=*/0,
27773 /*attrlist=*/NULL);
27777 /* If the type-id production did not work out, then we must be
27778 looking at the unary-expression production. */
27779 if (!expr)
27780 expr = cp_parser_unary_expression (parser);
27782 /* Go back to evaluating expressions. */
27783 --cp_unevaluated_operand;
27784 --c_inhibit_evaluation_warnings;
27786 /* Free the message we created. */
27787 free (tmp);
27788 /* And restore the old one. */
27789 parser->type_definition_forbidden_message = saved_message;
27790 parser->integral_constant_expression_p
27791 = saved_integral_constant_expression_p;
27792 parser->non_integral_constant_expression_p
27793 = saved_non_integral_constant_expression_p;
27795 return expr;
27798 /* If the current declaration has no declarator, return true. */
27800 static bool
27801 cp_parser_declares_only_class_p (cp_parser *parser)
27803 /* If the next token is a `;' or a `,' then there is no
27804 declarator. */
27805 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27806 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
27809 /* Update the DECL_SPECS to reflect the storage class indicated by
27810 KEYWORD. */
27812 static void
27813 cp_parser_set_storage_class (cp_parser *parser,
27814 cp_decl_specifier_seq *decl_specs,
27815 enum rid keyword,
27816 cp_token *token)
27818 cp_storage_class storage_class;
27820 if (parser->in_unbraced_linkage_specification_p)
27822 error_at (token->location, "invalid use of %qD in linkage specification",
27823 ridpointers[keyword]);
27824 return;
27826 else if (decl_specs->storage_class != sc_none)
27828 decl_specs->conflicting_specifiers_p = true;
27829 return;
27832 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
27833 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
27834 && decl_specs->gnu_thread_keyword_p)
27836 pedwarn (decl_specs->locations[ds_thread], 0,
27837 "%<__thread%> before %qD", ridpointers[keyword]);
27840 switch (keyword)
27842 case RID_AUTO:
27843 storage_class = sc_auto;
27844 break;
27845 case RID_REGISTER:
27846 storage_class = sc_register;
27847 break;
27848 case RID_STATIC:
27849 storage_class = sc_static;
27850 break;
27851 case RID_EXTERN:
27852 storage_class = sc_extern;
27853 break;
27854 case RID_MUTABLE:
27855 storage_class = sc_mutable;
27856 break;
27857 default:
27858 gcc_unreachable ();
27860 decl_specs->storage_class = storage_class;
27861 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
27863 /* A storage class specifier cannot be applied alongside a typedef
27864 specifier. If there is a typedef specifier present then set
27865 conflicting_specifiers_p which will trigger an error later
27866 on in grokdeclarator. */
27867 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
27868 decl_specs->conflicting_specifiers_p = true;
27871 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
27872 is true, the type is a class or enum definition. */
27874 static void
27875 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
27876 tree type_spec,
27877 cp_token *token,
27878 bool type_definition_p)
27880 decl_specs->any_specifiers_p = true;
27882 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
27883 (with, for example, in "typedef int wchar_t;") we remember that
27884 this is what happened. In system headers, we ignore these
27885 declarations so that G++ can work with system headers that are not
27886 C++-safe. */
27887 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
27888 && !type_definition_p
27889 && (type_spec == boolean_type_node
27890 || type_spec == char16_type_node
27891 || type_spec == char32_type_node
27892 || type_spec == wchar_type_node)
27893 && (decl_specs->type
27894 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
27895 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
27896 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
27897 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
27899 decl_specs->redefined_builtin_type = type_spec;
27900 set_and_check_decl_spec_loc (decl_specs,
27901 ds_redefined_builtin_type_spec,
27902 token);
27903 if (!decl_specs->type)
27905 decl_specs->type = type_spec;
27906 decl_specs->type_definition_p = false;
27907 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
27910 else if (decl_specs->type)
27911 decl_specs->multiple_types_p = true;
27912 else
27914 decl_specs->type = type_spec;
27915 decl_specs->type_definition_p = type_definition_p;
27916 decl_specs->redefined_builtin_type = NULL_TREE;
27917 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
27921 /* True iff TOKEN is the GNU keyword __thread. */
27923 static bool
27924 token_is__thread (cp_token *token)
27926 gcc_assert (token->keyword == RID_THREAD);
27927 return id_equal (token->u.value, "__thread");
27930 /* Set the location for a declarator specifier and check if it is
27931 duplicated.
27933 DECL_SPECS is the sequence of declarator specifiers onto which to
27934 set the location.
27936 DS is the single declarator specifier to set which location is to
27937 be set onto the existing sequence of declarators.
27939 LOCATION is the location for the declarator specifier to
27940 consider. */
27942 static void
27943 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
27944 cp_decl_spec ds, cp_token *token)
27946 gcc_assert (ds < ds_last);
27948 if (decl_specs == NULL)
27949 return;
27951 source_location location = token->location;
27953 if (decl_specs->locations[ds] == 0)
27955 decl_specs->locations[ds] = location;
27956 if (ds == ds_thread)
27957 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
27959 else
27961 if (ds == ds_long)
27963 if (decl_specs->locations[ds_long_long] != 0)
27964 error_at (location,
27965 "%<long long long%> is too long for GCC");
27966 else
27968 decl_specs->locations[ds_long_long] = location;
27969 pedwarn_cxx98 (location,
27970 OPT_Wlong_long,
27971 "ISO C++ 1998 does not support %<long long%>");
27974 else if (ds == ds_thread)
27976 bool gnu = token_is__thread (token);
27977 if (gnu != decl_specs->gnu_thread_keyword_p)
27978 error_at (location,
27979 "both %<__thread%> and %<thread_local%> specified");
27980 else
27982 gcc_rich_location richloc (location);
27983 richloc.add_fixit_remove ();
27984 error_at (&richloc, "duplicate %qD", token->u.value);
27987 else
27989 static const char *const decl_spec_names[] = {
27990 "signed",
27991 "unsigned",
27992 "short",
27993 "long",
27994 "const",
27995 "volatile",
27996 "restrict",
27997 "inline",
27998 "virtual",
27999 "explicit",
28000 "friend",
28001 "typedef",
28002 "using",
28003 "constexpr",
28004 "__complex"
28006 gcc_rich_location richloc (location);
28007 richloc.add_fixit_remove ();
28008 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28013 /* Return true iff the declarator specifier DS is present in the
28014 sequence of declarator specifiers DECL_SPECS. */
28016 bool
28017 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28018 cp_decl_spec ds)
28020 gcc_assert (ds < ds_last);
28022 if (decl_specs == NULL)
28023 return false;
28025 return decl_specs->locations[ds] != 0;
28028 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28029 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28031 static bool
28032 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28034 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28037 /* Issue an error message indicating that TOKEN_DESC was expected.
28038 If KEYWORD is true, it indicated this function is called by
28039 cp_parser_require_keword and the required token can only be
28040 a indicated keyword.
28042 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28043 within any error as the location of an "opening" token matching
28044 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28045 RT_CLOSE_PAREN). */
28047 static void
28048 cp_parser_required_error (cp_parser *parser,
28049 required_token token_desc,
28050 bool keyword,
28051 location_t matching_location)
28053 if (cp_parser_simulate_error (parser))
28054 return;
28056 const char *gmsgid = NULL;
28057 switch (token_desc)
28059 case RT_NEW:
28060 gmsgid = G_("expected %<new%>");
28061 break;
28062 case RT_DELETE:
28063 gmsgid = G_("expected %<delete%>");
28064 break;
28065 case RT_RETURN:
28066 gmsgid = G_("expected %<return%>");
28067 break;
28068 case RT_WHILE:
28069 gmsgid = G_("expected %<while%>");
28070 break;
28071 case RT_EXTERN:
28072 gmsgid = G_("expected %<extern%>");
28073 break;
28074 case RT_STATIC_ASSERT:
28075 gmsgid = G_("expected %<static_assert%>");
28076 break;
28077 case RT_DECLTYPE:
28078 gmsgid = G_("expected %<decltype%>");
28079 break;
28080 case RT_OPERATOR:
28081 gmsgid = G_("expected %<operator%>");
28082 break;
28083 case RT_CLASS:
28084 gmsgid = G_("expected %<class%>");
28085 break;
28086 case RT_TEMPLATE:
28087 gmsgid = G_("expected %<template%>");
28088 break;
28089 case RT_NAMESPACE:
28090 gmsgid = G_("expected %<namespace%>");
28091 break;
28092 case RT_USING:
28093 gmsgid = G_("expected %<using%>");
28094 break;
28095 case RT_ASM:
28096 gmsgid = G_("expected %<asm%>");
28097 break;
28098 case RT_TRY:
28099 gmsgid = G_("expected %<try%>");
28100 break;
28101 case RT_CATCH:
28102 gmsgid = G_("expected %<catch%>");
28103 break;
28104 case RT_THROW:
28105 gmsgid = G_("expected %<throw%>");
28106 break;
28107 case RT_LABEL:
28108 gmsgid = G_("expected %<__label__%>");
28109 break;
28110 case RT_AT_TRY:
28111 gmsgid = G_("expected %<@try%>");
28112 break;
28113 case RT_AT_SYNCHRONIZED:
28114 gmsgid = G_("expected %<@synchronized%>");
28115 break;
28116 case RT_AT_THROW:
28117 gmsgid = G_("expected %<@throw%>");
28118 break;
28119 case RT_TRANSACTION_ATOMIC:
28120 gmsgid = G_("expected %<__transaction_atomic%>");
28121 break;
28122 case RT_TRANSACTION_RELAXED:
28123 gmsgid = G_("expected %<__transaction_relaxed%>");
28124 break;
28125 default:
28126 break;
28129 if (!gmsgid && !keyword)
28131 switch (token_desc)
28133 case RT_SEMICOLON:
28134 gmsgid = G_("expected %<;%>");
28135 break;
28136 case RT_OPEN_PAREN:
28137 gmsgid = G_("expected %<(%>");
28138 break;
28139 case RT_CLOSE_BRACE:
28140 gmsgid = G_("expected %<}%>");
28141 break;
28142 case RT_OPEN_BRACE:
28143 gmsgid = G_("expected %<{%>");
28144 break;
28145 case RT_CLOSE_SQUARE:
28146 gmsgid = G_("expected %<]%>");
28147 break;
28148 case RT_OPEN_SQUARE:
28149 gmsgid = G_("expected %<[%>");
28150 break;
28151 case RT_COMMA:
28152 gmsgid = G_("expected %<,%>");
28153 break;
28154 case RT_SCOPE:
28155 gmsgid = G_("expected %<::%>");
28156 break;
28157 case RT_LESS:
28158 gmsgid = G_("expected %<<%>");
28159 break;
28160 case RT_GREATER:
28161 gmsgid = G_("expected %<>%>");
28162 break;
28163 case RT_EQ:
28164 gmsgid = G_("expected %<=%>");
28165 break;
28166 case RT_ELLIPSIS:
28167 gmsgid = G_("expected %<...%>");
28168 break;
28169 case RT_MULT:
28170 gmsgid = G_("expected %<*%>");
28171 break;
28172 case RT_COMPL:
28173 gmsgid = G_("expected %<~%>");
28174 break;
28175 case RT_COLON:
28176 gmsgid = G_("expected %<:%>");
28177 break;
28178 case RT_COLON_SCOPE:
28179 gmsgid = G_("expected %<:%> or %<::%>");
28180 break;
28181 case RT_CLOSE_PAREN:
28182 gmsgid = G_("expected %<)%>");
28183 break;
28184 case RT_COMMA_CLOSE_PAREN:
28185 gmsgid = G_("expected %<,%> or %<)%>");
28186 break;
28187 case RT_PRAGMA_EOL:
28188 gmsgid = G_("expected end of line");
28189 break;
28190 case RT_NAME:
28191 gmsgid = G_("expected identifier");
28192 break;
28193 case RT_SELECT:
28194 gmsgid = G_("expected selection-statement");
28195 break;
28196 case RT_ITERATION:
28197 gmsgid = G_("expected iteration-statement");
28198 break;
28199 case RT_JUMP:
28200 gmsgid = G_("expected jump-statement");
28201 break;
28202 case RT_CLASS_KEY:
28203 gmsgid = G_("expected class-key");
28204 break;
28205 case RT_CLASS_TYPENAME_TEMPLATE:
28206 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28207 break;
28208 default:
28209 gcc_unreachable ();
28213 if (gmsgid)
28214 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28218 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28219 issue an error message indicating that TOKEN_DESC was expected.
28221 Returns the token consumed, if the token had the appropriate type.
28222 Otherwise, returns NULL.
28224 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28225 within any error as the location of an "opening" token matching
28226 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28227 RT_CLOSE_PAREN). */
28229 static cp_token *
28230 cp_parser_require (cp_parser* parser,
28231 enum cpp_ttype type,
28232 required_token token_desc,
28233 location_t matching_location)
28235 if (cp_lexer_next_token_is (parser->lexer, type))
28236 return cp_lexer_consume_token (parser->lexer);
28237 else
28239 /* Output the MESSAGE -- unless we're parsing tentatively. */
28240 if (!cp_parser_simulate_error (parser))
28241 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28242 matching_location);
28243 return NULL;
28247 /* An error message is produced if the next token is not '>'.
28248 All further tokens are skipped until the desired token is
28249 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28251 static void
28252 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28254 /* Current level of '< ... >'. */
28255 unsigned level = 0;
28256 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28257 unsigned nesting_depth = 0;
28259 /* Are we ready, yet? If not, issue error message. */
28260 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28261 return;
28263 /* Skip tokens until the desired token is found. */
28264 while (true)
28266 /* Peek at the next token. */
28267 switch (cp_lexer_peek_token (parser->lexer)->type)
28269 case CPP_LESS:
28270 if (!nesting_depth)
28271 ++level;
28272 break;
28274 case CPP_RSHIFT:
28275 if (cxx_dialect == cxx98)
28276 /* C++0x views the `>>' operator as two `>' tokens, but
28277 C++98 does not. */
28278 break;
28279 else if (!nesting_depth && level-- == 0)
28281 /* We've hit a `>>' where the first `>' closes the
28282 template argument list, and the second `>' is
28283 spurious. Just consume the `>>' and stop; we've
28284 already produced at least one error. */
28285 cp_lexer_consume_token (parser->lexer);
28286 return;
28288 /* Fall through for C++0x, so we handle the second `>' in
28289 the `>>'. */
28290 gcc_fallthrough ();
28292 case CPP_GREATER:
28293 if (!nesting_depth && level-- == 0)
28295 /* We've reached the token we want, consume it and stop. */
28296 cp_lexer_consume_token (parser->lexer);
28297 return;
28299 break;
28301 case CPP_OPEN_PAREN:
28302 case CPP_OPEN_SQUARE:
28303 ++nesting_depth;
28304 break;
28306 case CPP_CLOSE_PAREN:
28307 case CPP_CLOSE_SQUARE:
28308 if (nesting_depth-- == 0)
28309 return;
28310 break;
28312 case CPP_EOF:
28313 case CPP_PRAGMA_EOL:
28314 case CPP_SEMICOLON:
28315 case CPP_OPEN_BRACE:
28316 case CPP_CLOSE_BRACE:
28317 /* The '>' was probably forgotten, don't look further. */
28318 return;
28320 default:
28321 break;
28324 /* Consume this token. */
28325 cp_lexer_consume_token (parser->lexer);
28329 /* If the next token is the indicated keyword, consume it. Otherwise,
28330 issue an error message indicating that TOKEN_DESC was expected.
28332 Returns the token consumed, if the token had the appropriate type.
28333 Otherwise, returns NULL. */
28335 static cp_token *
28336 cp_parser_require_keyword (cp_parser* parser,
28337 enum rid keyword,
28338 required_token token_desc)
28340 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28342 if (token && token->keyword != keyword)
28344 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28345 UNKNOWN_LOCATION);
28346 return NULL;
28349 return token;
28352 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28353 function-definition. */
28355 static bool
28356 cp_parser_token_starts_function_definition_p (cp_token* token)
28358 return (/* An ordinary function-body begins with an `{'. */
28359 token->type == CPP_OPEN_BRACE
28360 /* A ctor-initializer begins with a `:'. */
28361 || token->type == CPP_COLON
28362 /* A function-try-block begins with `try'. */
28363 || token->keyword == RID_TRY
28364 /* A function-transaction-block begins with `__transaction_atomic'
28365 or `__transaction_relaxed'. */
28366 || token->keyword == RID_TRANSACTION_ATOMIC
28367 || token->keyword == RID_TRANSACTION_RELAXED
28368 /* The named return value extension begins with `return'. */
28369 || token->keyword == RID_RETURN);
28372 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28373 definition. */
28375 static bool
28376 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28378 cp_token *token;
28380 token = cp_lexer_peek_token (parser->lexer);
28381 return (token->type == CPP_OPEN_BRACE
28382 || (token->type == CPP_COLON
28383 && !parser->colon_doesnt_start_class_def_p));
28386 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28387 C++0x) ending a template-argument. */
28389 static bool
28390 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28392 cp_token *token;
28394 token = cp_lexer_peek_token (parser->lexer);
28395 return (token->type == CPP_COMMA
28396 || token->type == CPP_GREATER
28397 || token->type == CPP_ELLIPSIS
28398 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28401 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28402 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28404 static bool
28405 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28406 size_t n)
28408 cp_token *token;
28410 token = cp_lexer_peek_nth_token (parser->lexer, n);
28411 if (token->type == CPP_LESS)
28412 return true;
28413 /* Check for the sequence `<::' in the original code. It would be lexed as
28414 `[:', where `[' is a digraph, and there is no whitespace before
28415 `:'. */
28416 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28418 cp_token *token2;
28419 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28420 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28421 return true;
28423 return false;
28426 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28427 or none_type otherwise. */
28429 static enum tag_types
28430 cp_parser_token_is_class_key (cp_token* token)
28432 switch (token->keyword)
28434 case RID_CLASS:
28435 return class_type;
28436 case RID_STRUCT:
28437 return record_type;
28438 case RID_UNION:
28439 return union_type;
28441 default:
28442 return none_type;
28446 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28447 or none_type otherwise or if the token is null. */
28449 static enum tag_types
28450 cp_parser_token_is_type_parameter_key (cp_token* token)
28452 if (!token)
28453 return none_type;
28455 switch (token->keyword)
28457 case RID_CLASS:
28458 return class_type;
28459 case RID_TYPENAME:
28460 return typename_type;
28462 default:
28463 return none_type;
28467 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28469 static void
28470 cp_parser_check_class_key (enum tag_types class_key, tree type)
28472 if (type == error_mark_node)
28473 return;
28474 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28476 if (permerror (input_location, "%qs tag used in naming %q#T",
28477 class_key == union_type ? "union"
28478 : class_key == record_type ? "struct" : "class",
28479 type))
28480 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28481 "%q#T was previously declared here", type);
28485 /* Issue an error message if DECL is redeclared with different
28486 access than its original declaration [class.access.spec/3].
28487 This applies to nested classes, nested class templates and
28488 enumerations [class.mem/1]. */
28490 static void
28491 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28493 if (!decl
28494 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28495 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28496 return;
28498 if ((TREE_PRIVATE (decl)
28499 != (current_access_specifier == access_private_node))
28500 || (TREE_PROTECTED (decl)
28501 != (current_access_specifier == access_protected_node)))
28502 error_at (location, "%qD redeclared with different access", decl);
28505 /* Look for the `template' keyword, as a syntactic disambiguator.
28506 Return TRUE iff it is present, in which case it will be
28507 consumed. */
28509 static bool
28510 cp_parser_optional_template_keyword (cp_parser *parser)
28512 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28514 /* In C++98 the `template' keyword can only be used within templates;
28515 outside templates the parser can always figure out what is a
28516 template and what is not. In C++11, per the resolution of DR 468,
28517 `template' is allowed in cases where it is not strictly necessary. */
28518 if (!processing_template_decl
28519 && pedantic && cxx_dialect == cxx98)
28521 cp_token *token = cp_lexer_peek_token (parser->lexer);
28522 pedwarn (token->location, OPT_Wpedantic,
28523 "in C++98 %<template%> (as a disambiguator) is only "
28524 "allowed within templates");
28525 /* If this part of the token stream is rescanned, the same
28526 error message would be generated. So, we purge the token
28527 from the stream. */
28528 cp_lexer_purge_token (parser->lexer);
28529 return false;
28531 else
28533 /* Consume the `template' keyword. */
28534 cp_lexer_consume_token (parser->lexer);
28535 return true;
28538 return false;
28541 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28542 set PARSER->SCOPE, and perform other related actions. */
28544 static void
28545 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28547 struct tree_check *check_value;
28549 /* Get the stored value. */
28550 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28551 /* Set the scope from the stored value. */
28552 parser->scope = saved_checks_value (check_value);
28553 parser->qualifying_scope = check_value->qualifying_scope;
28554 parser->object_scope = NULL_TREE;
28557 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28558 encounter the end of a block before what we were looking for. */
28560 static bool
28561 cp_parser_cache_group (cp_parser *parser,
28562 enum cpp_ttype end,
28563 unsigned depth)
28565 while (true)
28567 cp_token *token = cp_lexer_peek_token (parser->lexer);
28569 /* Abort a parenthesized expression if we encounter a semicolon. */
28570 if ((end == CPP_CLOSE_PAREN || depth == 0)
28571 && token->type == CPP_SEMICOLON)
28572 return true;
28573 /* If we've reached the end of the file, stop. */
28574 if (token->type == CPP_EOF
28575 || (end != CPP_PRAGMA_EOL
28576 && token->type == CPP_PRAGMA_EOL))
28577 return true;
28578 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28579 /* We've hit the end of an enclosing block, so there's been some
28580 kind of syntax error. */
28581 return true;
28583 /* Consume the token. */
28584 cp_lexer_consume_token (parser->lexer);
28585 /* See if it starts a new group. */
28586 if (token->type == CPP_OPEN_BRACE)
28588 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28589 /* In theory this should probably check end == '}', but
28590 cp_parser_save_member_function_body needs it to exit
28591 after either '}' or ')' when called with ')'. */
28592 if (depth == 0)
28593 return false;
28595 else if (token->type == CPP_OPEN_PAREN)
28597 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28598 if (depth == 0 && end == CPP_CLOSE_PAREN)
28599 return false;
28601 else if (token->type == CPP_PRAGMA)
28602 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28603 else if (token->type == end)
28604 return false;
28608 /* Like above, for caching a default argument or NSDMI. Both of these are
28609 terminated by a non-nested comma, but it can be unclear whether or not a
28610 comma is nested in a template argument list unless we do more parsing.
28611 In order to handle this ambiguity, when we encounter a ',' after a '<'
28612 we try to parse what follows as a parameter-declaration-list (in the
28613 case of a default argument) or a member-declarator (in the case of an
28614 NSDMI). If that succeeds, then we stop caching. */
28616 static tree
28617 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28619 unsigned depth = 0;
28620 int maybe_template_id = 0;
28621 cp_token *first_token;
28622 cp_token *token;
28623 tree default_argument;
28625 /* Add tokens until we have processed the entire default
28626 argument. We add the range [first_token, token). */
28627 first_token = cp_lexer_peek_token (parser->lexer);
28628 if (first_token->type == CPP_OPEN_BRACE)
28630 /* For list-initialization, this is straightforward. */
28631 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28632 token = cp_lexer_peek_token (parser->lexer);
28634 else while (true)
28636 bool done = false;
28638 /* Peek at the next token. */
28639 token = cp_lexer_peek_token (parser->lexer);
28640 /* What we do depends on what token we have. */
28641 switch (token->type)
28643 /* In valid code, a default argument must be
28644 immediately followed by a `,' `)', or `...'. */
28645 case CPP_COMMA:
28646 if (depth == 0 && maybe_template_id)
28648 /* If we've seen a '<', we might be in a
28649 template-argument-list. Until Core issue 325 is
28650 resolved, we don't know how this situation ought
28651 to be handled, so try to DTRT. We check whether
28652 what comes after the comma is a valid parameter
28653 declaration list. If it is, then the comma ends
28654 the default argument; otherwise the default
28655 argument continues. */
28656 bool error = false;
28657 cp_token *peek;
28659 /* Set ITALP so cp_parser_parameter_declaration_list
28660 doesn't decide to commit to this parse. */
28661 bool saved_italp = parser->in_template_argument_list_p;
28662 parser->in_template_argument_list_p = true;
28664 cp_parser_parse_tentatively (parser);
28666 if (nsdmi)
28668 /* Parse declarators until we reach a non-comma or
28669 somthing that cannot be an initializer.
28670 Just checking whether we're looking at a single
28671 declarator is insufficient. Consider:
28672 int var = tuple<T,U>::x;
28673 The template parameter 'U' looks exactly like a
28674 declarator. */
28677 int ctor_dtor_or_conv_p;
28678 cp_lexer_consume_token (parser->lexer);
28679 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28680 &ctor_dtor_or_conv_p,
28681 /*parenthesized_p=*/NULL,
28682 /*member_p=*/true,
28683 /*friend_p=*/false);
28684 peek = cp_lexer_peek_token (parser->lexer);
28685 if (cp_parser_error_occurred (parser))
28686 break;
28688 while (peek->type == CPP_COMMA);
28689 /* If we met an '=' or ';' then the original comma
28690 was the end of the NSDMI. Otherwise assume
28691 we're still in the NSDMI. */
28692 error = (peek->type != CPP_EQ
28693 && peek->type != CPP_SEMICOLON);
28695 else
28697 cp_lexer_consume_token (parser->lexer);
28698 begin_scope (sk_function_parms, NULL_TREE);
28699 cp_parser_parameter_declaration_list (parser, &error);
28700 pop_bindings_and_leave_scope ();
28702 if (!cp_parser_error_occurred (parser) && !error)
28703 done = true;
28704 cp_parser_abort_tentative_parse (parser);
28706 parser->in_template_argument_list_p = saved_italp;
28707 break;
28709 /* FALLTHRU */
28710 case CPP_CLOSE_PAREN:
28711 case CPP_ELLIPSIS:
28712 /* If we run into a non-nested `;', `}', or `]',
28713 then the code is invalid -- but the default
28714 argument is certainly over. */
28715 case CPP_SEMICOLON:
28716 case CPP_CLOSE_BRACE:
28717 case CPP_CLOSE_SQUARE:
28718 if (depth == 0
28719 /* Handle correctly int n = sizeof ... ( p ); */
28720 && token->type != CPP_ELLIPSIS)
28721 done = true;
28722 /* Update DEPTH, if necessary. */
28723 else if (token->type == CPP_CLOSE_PAREN
28724 || token->type == CPP_CLOSE_BRACE
28725 || token->type == CPP_CLOSE_SQUARE)
28726 --depth;
28727 break;
28729 case CPP_OPEN_PAREN:
28730 case CPP_OPEN_SQUARE:
28731 case CPP_OPEN_BRACE:
28732 ++depth;
28733 break;
28735 case CPP_LESS:
28736 if (depth == 0)
28737 /* This might be the comparison operator, or it might
28738 start a template argument list. */
28739 ++maybe_template_id;
28740 break;
28742 case CPP_RSHIFT:
28743 if (cxx_dialect == cxx98)
28744 break;
28745 /* Fall through for C++0x, which treats the `>>'
28746 operator like two `>' tokens in certain
28747 cases. */
28748 gcc_fallthrough ();
28750 case CPP_GREATER:
28751 if (depth == 0)
28753 /* This might be an operator, or it might close a
28754 template argument list. But if a previous '<'
28755 started a template argument list, this will have
28756 closed it, so we can't be in one anymore. */
28757 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
28758 if (maybe_template_id < 0)
28759 maybe_template_id = 0;
28761 break;
28763 /* If we run out of tokens, issue an error message. */
28764 case CPP_EOF:
28765 case CPP_PRAGMA_EOL:
28766 error_at (token->location, "file ends in default argument");
28767 return error_mark_node;
28769 case CPP_NAME:
28770 case CPP_SCOPE:
28771 /* In these cases, we should look for template-ids.
28772 For example, if the default argument is
28773 `X<int, double>()', we need to do name lookup to
28774 figure out whether or not `X' is a template; if
28775 so, the `,' does not end the default argument.
28777 That is not yet done. */
28778 break;
28780 default:
28781 break;
28784 /* If we've reached the end, stop. */
28785 if (done)
28786 break;
28788 /* Add the token to the token block. */
28789 token = cp_lexer_consume_token (parser->lexer);
28792 /* Create a DEFAULT_ARG to represent the unparsed default
28793 argument. */
28794 default_argument = make_node (DEFAULT_ARG);
28795 DEFARG_TOKENS (default_argument)
28796 = cp_token_cache_new (first_token, token);
28797 DEFARG_INSTANTIATIONS (default_argument) = NULL;
28799 return default_argument;
28802 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
28804 location_t
28805 defarg_location (tree default_argument)
28807 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
28808 location_t start = tokens->first->location;
28809 location_t end = tokens->last->location;
28810 return make_location (start, start, end);
28813 /* Begin parsing tentatively. We always save tokens while parsing
28814 tentatively so that if the tentative parsing fails we can restore the
28815 tokens. */
28817 static void
28818 cp_parser_parse_tentatively (cp_parser* parser)
28820 /* Enter a new parsing context. */
28821 parser->context = cp_parser_context_new (parser->context);
28822 /* Begin saving tokens. */
28823 cp_lexer_save_tokens (parser->lexer);
28824 /* In order to avoid repetitive access control error messages,
28825 access checks are queued up until we are no longer parsing
28826 tentatively. */
28827 push_deferring_access_checks (dk_deferred);
28830 /* Commit to the currently active tentative parse. */
28832 static void
28833 cp_parser_commit_to_tentative_parse (cp_parser* parser)
28835 cp_parser_context *context;
28836 cp_lexer *lexer;
28838 /* Mark all of the levels as committed. */
28839 lexer = parser->lexer;
28840 for (context = parser->context; context->next; context = context->next)
28842 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28843 break;
28844 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28845 while (!cp_lexer_saving_tokens (lexer))
28846 lexer = lexer->next;
28847 cp_lexer_commit_tokens (lexer);
28851 /* Commit to the topmost currently active tentative parse.
28853 Note that this function shouldn't be called when there are
28854 irreversible side-effects while in a tentative state. For
28855 example, we shouldn't create a permanent entry in the symbol
28856 table, or issue an error message that might not apply if the
28857 tentative parse is aborted. */
28859 static void
28860 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
28862 cp_parser_context *context = parser->context;
28863 cp_lexer *lexer = parser->lexer;
28865 if (context)
28867 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28868 return;
28869 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28871 while (!cp_lexer_saving_tokens (lexer))
28872 lexer = lexer->next;
28873 cp_lexer_commit_tokens (lexer);
28877 /* Abort the currently active tentative parse. All consumed tokens
28878 will be rolled back, and no diagnostics will be issued. */
28880 static void
28881 cp_parser_abort_tentative_parse (cp_parser* parser)
28883 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
28884 || errorcount > 0);
28885 cp_parser_simulate_error (parser);
28886 /* Now, pretend that we want to see if the construct was
28887 successfully parsed. */
28888 cp_parser_parse_definitely (parser);
28891 /* Stop parsing tentatively. If a parse error has occurred, restore the
28892 token stream. Otherwise, commit to the tokens we have consumed.
28893 Returns true if no error occurred; false otherwise. */
28895 static bool
28896 cp_parser_parse_definitely (cp_parser* parser)
28898 bool error_occurred;
28899 cp_parser_context *context;
28901 /* Remember whether or not an error occurred, since we are about to
28902 destroy that information. */
28903 error_occurred = cp_parser_error_occurred (parser);
28904 /* Remove the topmost context from the stack. */
28905 context = parser->context;
28906 parser->context = context->next;
28907 /* If no parse errors occurred, commit to the tentative parse. */
28908 if (!error_occurred)
28910 /* Commit to the tokens read tentatively, unless that was
28911 already done. */
28912 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
28913 cp_lexer_commit_tokens (parser->lexer);
28915 pop_to_parent_deferring_access_checks ();
28917 /* Otherwise, if errors occurred, roll back our state so that things
28918 are just as they were before we began the tentative parse. */
28919 else
28921 cp_lexer_rollback_tokens (parser->lexer);
28922 pop_deferring_access_checks ();
28924 /* Add the context to the front of the free list. */
28925 context->next = cp_parser_context_free_list;
28926 cp_parser_context_free_list = context;
28928 return !error_occurred;
28931 /* Returns true if we are parsing tentatively and are not committed to
28932 this tentative parse. */
28934 static bool
28935 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
28937 return (cp_parser_parsing_tentatively (parser)
28938 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
28941 /* Returns nonzero iff an error has occurred during the most recent
28942 tentative parse. */
28944 static bool
28945 cp_parser_error_occurred (cp_parser* parser)
28947 return (cp_parser_parsing_tentatively (parser)
28948 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
28951 /* Returns nonzero if GNU extensions are allowed. */
28953 static bool
28954 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
28956 return parser->allow_gnu_extensions_p;
28959 /* Objective-C++ Productions */
28962 /* Parse an Objective-C expression, which feeds into a primary-expression
28963 above.
28965 objc-expression:
28966 objc-message-expression
28967 objc-string-literal
28968 objc-encode-expression
28969 objc-protocol-expression
28970 objc-selector-expression
28972 Returns a tree representation of the expression. */
28974 static cp_expr
28975 cp_parser_objc_expression (cp_parser* parser)
28977 /* Try to figure out what kind of declaration is present. */
28978 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
28980 switch (kwd->type)
28982 case CPP_OPEN_SQUARE:
28983 return cp_parser_objc_message_expression (parser);
28985 case CPP_OBJC_STRING:
28986 kwd = cp_lexer_consume_token (parser->lexer);
28987 return objc_build_string_object (kwd->u.value);
28989 case CPP_KEYWORD:
28990 switch (kwd->keyword)
28992 case RID_AT_ENCODE:
28993 return cp_parser_objc_encode_expression (parser);
28995 case RID_AT_PROTOCOL:
28996 return cp_parser_objc_protocol_expression (parser);
28998 case RID_AT_SELECTOR:
28999 return cp_parser_objc_selector_expression (parser);
29001 default:
29002 break;
29004 /* FALLTHRU */
29005 default:
29006 error_at (kwd->location,
29007 "misplaced %<@%D%> Objective-C++ construct",
29008 kwd->u.value);
29009 cp_parser_skip_to_end_of_block_or_statement (parser);
29012 return error_mark_node;
29015 /* Parse an Objective-C message expression.
29017 objc-message-expression:
29018 [ objc-message-receiver objc-message-args ]
29020 Returns a representation of an Objective-C message. */
29022 static tree
29023 cp_parser_objc_message_expression (cp_parser* parser)
29025 tree receiver, messageargs;
29027 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29028 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29029 receiver = cp_parser_objc_message_receiver (parser);
29030 messageargs = cp_parser_objc_message_args (parser);
29031 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29032 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29034 tree result = objc_build_message_expr (receiver, messageargs);
29036 /* Construct a location e.g.
29037 [self func1:5]
29038 ^~~~~~~~~~~~~~
29039 ranging from the '[' to the ']', with the caret at the start. */
29040 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29041 protected_set_expr_location (result, combined_loc);
29043 return result;
29046 /* Parse an objc-message-receiver.
29048 objc-message-receiver:
29049 expression
29050 simple-type-specifier
29052 Returns a representation of the type or expression. */
29054 static tree
29055 cp_parser_objc_message_receiver (cp_parser* parser)
29057 tree rcv;
29059 /* An Objective-C message receiver may be either (1) a type
29060 or (2) an expression. */
29061 cp_parser_parse_tentatively (parser);
29062 rcv = cp_parser_expression (parser);
29064 /* If that worked out, fine. */
29065 if (cp_parser_parse_definitely (parser))
29066 return rcv;
29068 cp_parser_parse_tentatively (parser);
29069 rcv = cp_parser_simple_type_specifier (parser,
29070 /*decl_specs=*/NULL,
29071 CP_PARSER_FLAGS_NONE);
29073 if (cp_parser_parse_definitely (parser))
29074 return objc_get_class_reference (rcv);
29076 cp_parser_error (parser, "objective-c++ message receiver expected");
29077 return error_mark_node;
29080 /* Parse the arguments and selectors comprising an Objective-C message.
29082 objc-message-args:
29083 objc-selector
29084 objc-selector-args
29085 objc-selector-args , objc-comma-args
29087 objc-selector-args:
29088 objc-selector [opt] : assignment-expression
29089 objc-selector-args objc-selector [opt] : assignment-expression
29091 objc-comma-args:
29092 assignment-expression
29093 objc-comma-args , assignment-expression
29095 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29096 selector arguments and TREE_VALUE containing a list of comma
29097 arguments. */
29099 static tree
29100 cp_parser_objc_message_args (cp_parser* parser)
29102 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29103 bool maybe_unary_selector_p = true;
29104 cp_token *token = cp_lexer_peek_token (parser->lexer);
29106 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29108 tree selector = NULL_TREE, arg;
29110 if (token->type != CPP_COLON)
29111 selector = cp_parser_objc_selector (parser);
29113 /* Detect if we have a unary selector. */
29114 if (maybe_unary_selector_p
29115 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29116 return build_tree_list (selector, NULL_TREE);
29118 maybe_unary_selector_p = false;
29119 cp_parser_require (parser, CPP_COLON, RT_COLON);
29120 arg = cp_parser_assignment_expression (parser);
29122 sel_args
29123 = chainon (sel_args,
29124 build_tree_list (selector, arg));
29126 token = cp_lexer_peek_token (parser->lexer);
29129 /* Handle non-selector arguments, if any. */
29130 while (token->type == CPP_COMMA)
29132 tree arg;
29134 cp_lexer_consume_token (parser->lexer);
29135 arg = cp_parser_assignment_expression (parser);
29137 addl_args
29138 = chainon (addl_args,
29139 build_tree_list (NULL_TREE, arg));
29141 token = cp_lexer_peek_token (parser->lexer);
29144 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29146 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29147 return build_tree_list (error_mark_node, error_mark_node);
29150 return build_tree_list (sel_args, addl_args);
29153 /* Parse an Objective-C encode expression.
29155 objc-encode-expression:
29156 @encode objc-typename
29158 Returns an encoded representation of the type argument. */
29160 static cp_expr
29161 cp_parser_objc_encode_expression (cp_parser* parser)
29163 tree type;
29164 cp_token *token;
29165 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29167 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29168 matching_parens parens;
29169 parens.require_open (parser);
29170 token = cp_lexer_peek_token (parser->lexer);
29171 type = complete_type (cp_parser_type_id (parser));
29172 parens.require_close (parser);
29174 if (!type)
29176 error_at (token->location,
29177 "%<@encode%> must specify a type as an argument");
29178 return error_mark_node;
29181 /* This happens if we find @encode(T) (where T is a template
29182 typename or something dependent on a template typename) when
29183 parsing a template. In that case, we can't compile it
29184 immediately, but we rather create an AT_ENCODE_EXPR which will
29185 need to be instantiated when the template is used.
29187 if (dependent_type_p (type))
29189 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29190 TREE_READONLY (value) = 1;
29191 return value;
29195 /* Build a location of the form:
29196 @encode(int)
29197 ^~~~~~~~~~~~
29198 with caret==start at the @ token, finishing at the close paren. */
29199 location_t combined_loc
29200 = make_location (start_loc, start_loc,
29201 cp_lexer_previous_token (parser->lexer)->location);
29203 return cp_expr (objc_build_encode_expr (type), combined_loc);
29206 /* Parse an Objective-C @defs expression. */
29208 static tree
29209 cp_parser_objc_defs_expression (cp_parser *parser)
29211 tree name;
29213 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29214 matching_parens parens;
29215 parens.require_open (parser);
29216 name = cp_parser_identifier (parser);
29217 parens.require_close (parser);
29219 return objc_get_class_ivars (name);
29222 /* Parse an Objective-C protocol expression.
29224 objc-protocol-expression:
29225 @protocol ( identifier )
29227 Returns a representation of the protocol expression. */
29229 static tree
29230 cp_parser_objc_protocol_expression (cp_parser* parser)
29232 tree proto;
29233 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29235 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29236 matching_parens parens;
29237 parens.require_open (parser);
29238 proto = cp_parser_identifier (parser);
29239 parens.require_close (parser);
29241 /* Build a location of the form:
29242 @protocol(prot)
29243 ^~~~~~~~~~~~~~~
29244 with caret==start at the @ token, finishing at the close paren. */
29245 location_t combined_loc
29246 = make_location (start_loc, start_loc,
29247 cp_lexer_previous_token (parser->lexer)->location);
29248 tree result = objc_build_protocol_expr (proto);
29249 protected_set_expr_location (result, combined_loc);
29250 return result;
29253 /* Parse an Objective-C selector expression.
29255 objc-selector-expression:
29256 @selector ( objc-method-signature )
29258 objc-method-signature:
29259 objc-selector
29260 objc-selector-seq
29262 objc-selector-seq:
29263 objc-selector :
29264 objc-selector-seq objc-selector :
29266 Returns a representation of the method selector. */
29268 static tree
29269 cp_parser_objc_selector_expression (cp_parser* parser)
29271 tree sel_seq = NULL_TREE;
29272 bool maybe_unary_selector_p = true;
29273 cp_token *token;
29274 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29276 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29277 matching_parens parens;
29278 parens.require_open (parser);
29279 token = cp_lexer_peek_token (parser->lexer);
29281 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29282 || token->type == CPP_SCOPE)
29284 tree selector = NULL_TREE;
29286 if (token->type != CPP_COLON
29287 || token->type == CPP_SCOPE)
29288 selector = cp_parser_objc_selector (parser);
29290 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29291 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29293 /* Detect if we have a unary selector. */
29294 if (maybe_unary_selector_p)
29296 sel_seq = selector;
29297 goto finish_selector;
29299 else
29301 cp_parser_error (parser, "expected %<:%>");
29304 maybe_unary_selector_p = false;
29305 token = cp_lexer_consume_token (parser->lexer);
29307 if (token->type == CPP_SCOPE)
29309 sel_seq
29310 = chainon (sel_seq,
29311 build_tree_list (selector, NULL_TREE));
29312 sel_seq
29313 = chainon (sel_seq,
29314 build_tree_list (NULL_TREE, NULL_TREE));
29316 else
29317 sel_seq
29318 = chainon (sel_seq,
29319 build_tree_list (selector, NULL_TREE));
29321 token = cp_lexer_peek_token (parser->lexer);
29324 finish_selector:
29325 parens.require_close (parser);
29328 /* Build a location of the form:
29329 @selector(func)
29330 ^~~~~~~~~~~~~~~
29331 with caret==start at the @ token, finishing at the close paren. */
29332 location_t combined_loc
29333 = make_location (loc, loc,
29334 cp_lexer_previous_token (parser->lexer)->location);
29335 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29336 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29337 protected_set_expr_location (result, combined_loc);
29338 return result;
29341 /* Parse a list of identifiers.
29343 objc-identifier-list:
29344 identifier
29345 objc-identifier-list , identifier
29347 Returns a TREE_LIST of identifier nodes. */
29349 static tree
29350 cp_parser_objc_identifier_list (cp_parser* parser)
29352 tree identifier;
29353 tree list;
29354 cp_token *sep;
29356 identifier = cp_parser_identifier (parser);
29357 if (identifier == error_mark_node)
29358 return error_mark_node;
29360 list = build_tree_list (NULL_TREE, identifier);
29361 sep = cp_lexer_peek_token (parser->lexer);
29363 while (sep->type == CPP_COMMA)
29365 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29366 identifier = cp_parser_identifier (parser);
29367 if (identifier == error_mark_node)
29368 return list;
29370 list = chainon (list, build_tree_list (NULL_TREE,
29371 identifier));
29372 sep = cp_lexer_peek_token (parser->lexer);
29375 return list;
29378 /* Parse an Objective-C alias declaration.
29380 objc-alias-declaration:
29381 @compatibility_alias identifier identifier ;
29383 This function registers the alias mapping with the Objective-C front end.
29384 It returns nothing. */
29386 static void
29387 cp_parser_objc_alias_declaration (cp_parser* parser)
29389 tree alias, orig;
29391 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29392 alias = cp_parser_identifier (parser);
29393 orig = cp_parser_identifier (parser);
29394 objc_declare_alias (alias, orig);
29395 cp_parser_consume_semicolon_at_end_of_statement (parser);
29398 /* Parse an Objective-C class forward-declaration.
29400 objc-class-declaration:
29401 @class objc-identifier-list ;
29403 The function registers the forward declarations with the Objective-C
29404 front end. It returns nothing. */
29406 static void
29407 cp_parser_objc_class_declaration (cp_parser* parser)
29409 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29410 while (true)
29412 tree id;
29414 id = cp_parser_identifier (parser);
29415 if (id == error_mark_node)
29416 break;
29418 objc_declare_class (id);
29420 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29421 cp_lexer_consume_token (parser->lexer);
29422 else
29423 break;
29425 cp_parser_consume_semicolon_at_end_of_statement (parser);
29428 /* Parse a list of Objective-C protocol references.
29430 objc-protocol-refs-opt:
29431 objc-protocol-refs [opt]
29433 objc-protocol-refs:
29434 < objc-identifier-list >
29436 Returns a TREE_LIST of identifiers, if any. */
29438 static tree
29439 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29441 tree protorefs = NULL_TREE;
29443 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29445 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29446 protorefs = cp_parser_objc_identifier_list (parser);
29447 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29450 return protorefs;
29453 /* Parse a Objective-C visibility specification. */
29455 static void
29456 cp_parser_objc_visibility_spec (cp_parser* parser)
29458 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29460 switch (vis->keyword)
29462 case RID_AT_PRIVATE:
29463 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29464 break;
29465 case RID_AT_PROTECTED:
29466 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29467 break;
29468 case RID_AT_PUBLIC:
29469 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29470 break;
29471 case RID_AT_PACKAGE:
29472 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29473 break;
29474 default:
29475 return;
29478 /* Eat '@private'/'@protected'/'@public'. */
29479 cp_lexer_consume_token (parser->lexer);
29482 /* Parse an Objective-C method type. Return 'true' if it is a class
29483 (+) method, and 'false' if it is an instance (-) method. */
29485 static inline bool
29486 cp_parser_objc_method_type (cp_parser* parser)
29488 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29489 return true;
29490 else
29491 return false;
29494 /* Parse an Objective-C protocol qualifier. */
29496 static tree
29497 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29499 tree quals = NULL_TREE, node;
29500 cp_token *token = cp_lexer_peek_token (parser->lexer);
29502 node = token->u.value;
29504 while (node && identifier_p (node)
29505 && (node == ridpointers [(int) RID_IN]
29506 || node == ridpointers [(int) RID_OUT]
29507 || node == ridpointers [(int) RID_INOUT]
29508 || node == ridpointers [(int) RID_BYCOPY]
29509 || node == ridpointers [(int) RID_BYREF]
29510 || node == ridpointers [(int) RID_ONEWAY]))
29512 quals = tree_cons (NULL_TREE, node, quals);
29513 cp_lexer_consume_token (parser->lexer);
29514 token = cp_lexer_peek_token (parser->lexer);
29515 node = token->u.value;
29518 return quals;
29521 /* Parse an Objective-C typename. */
29523 static tree
29524 cp_parser_objc_typename (cp_parser* parser)
29526 tree type_name = NULL_TREE;
29528 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29530 tree proto_quals, cp_type = NULL_TREE;
29532 matching_parens parens;
29533 parens.consume_open (parser); /* Eat '('. */
29534 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29536 /* An ObjC type name may consist of just protocol qualifiers, in which
29537 case the type shall default to 'id'. */
29538 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29540 cp_type = cp_parser_type_id (parser);
29542 /* If the type could not be parsed, an error has already
29543 been produced. For error recovery, behave as if it had
29544 not been specified, which will use the default type
29545 'id'. */
29546 if (cp_type == error_mark_node)
29548 cp_type = NULL_TREE;
29549 /* We need to skip to the closing parenthesis as
29550 cp_parser_type_id() does not seem to do it for
29551 us. */
29552 cp_parser_skip_to_closing_parenthesis (parser,
29553 /*recovering=*/true,
29554 /*or_comma=*/false,
29555 /*consume_paren=*/false);
29559 parens.require_close (parser);
29560 type_name = build_tree_list (proto_quals, cp_type);
29563 return type_name;
29566 /* Check to see if TYPE refers to an Objective-C selector name. */
29568 static bool
29569 cp_parser_objc_selector_p (enum cpp_ttype type)
29571 return (type == CPP_NAME || type == CPP_KEYWORD
29572 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29573 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29574 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29575 || type == CPP_XOR || type == CPP_XOR_EQ);
29578 /* Parse an Objective-C selector. */
29580 static tree
29581 cp_parser_objc_selector (cp_parser* parser)
29583 cp_token *token = cp_lexer_consume_token (parser->lexer);
29585 if (!cp_parser_objc_selector_p (token->type))
29587 error_at (token->location, "invalid Objective-C++ selector name");
29588 return error_mark_node;
29591 /* C++ operator names are allowed to appear in ObjC selectors. */
29592 switch (token->type)
29594 case CPP_AND_AND: return get_identifier ("and");
29595 case CPP_AND_EQ: return get_identifier ("and_eq");
29596 case CPP_AND: return get_identifier ("bitand");
29597 case CPP_OR: return get_identifier ("bitor");
29598 case CPP_COMPL: return get_identifier ("compl");
29599 case CPP_NOT: return get_identifier ("not");
29600 case CPP_NOT_EQ: return get_identifier ("not_eq");
29601 case CPP_OR_OR: return get_identifier ("or");
29602 case CPP_OR_EQ: return get_identifier ("or_eq");
29603 case CPP_XOR: return get_identifier ("xor");
29604 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29605 default: return token->u.value;
29609 /* Parse an Objective-C params list. */
29611 static tree
29612 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29614 tree params = NULL_TREE;
29615 bool maybe_unary_selector_p = true;
29616 cp_token *token = cp_lexer_peek_token (parser->lexer);
29618 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29620 tree selector = NULL_TREE, type_name, identifier;
29621 tree parm_attr = NULL_TREE;
29623 if (token->keyword == RID_ATTRIBUTE)
29624 break;
29626 if (token->type != CPP_COLON)
29627 selector = cp_parser_objc_selector (parser);
29629 /* Detect if we have a unary selector. */
29630 if (maybe_unary_selector_p
29631 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29633 params = selector; /* Might be followed by attributes. */
29634 break;
29637 maybe_unary_selector_p = false;
29638 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29640 /* Something went quite wrong. There should be a colon
29641 here, but there is not. Stop parsing parameters. */
29642 break;
29644 type_name = cp_parser_objc_typename (parser);
29645 /* New ObjC allows attributes on parameters too. */
29646 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
29647 parm_attr = cp_parser_attributes_opt (parser);
29648 identifier = cp_parser_identifier (parser);
29650 params
29651 = chainon (params,
29652 objc_build_keyword_decl (selector,
29653 type_name,
29654 identifier,
29655 parm_attr));
29657 token = cp_lexer_peek_token (parser->lexer);
29660 if (params == NULL_TREE)
29662 cp_parser_error (parser, "objective-c++ method declaration is expected");
29663 return error_mark_node;
29666 /* We allow tail attributes for the method. */
29667 if (token->keyword == RID_ATTRIBUTE)
29669 *attributes = cp_parser_attributes_opt (parser);
29670 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29671 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29672 return params;
29673 cp_parser_error (parser,
29674 "method attributes must be specified at the end");
29675 return error_mark_node;
29678 if (params == NULL_TREE)
29680 cp_parser_error (parser, "objective-c++ method declaration is expected");
29681 return error_mark_node;
29683 return params;
29686 /* Parse the non-keyword Objective-C params. */
29688 static tree
29689 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
29690 tree* attributes)
29692 tree params = make_node (TREE_LIST);
29693 cp_token *token = cp_lexer_peek_token (parser->lexer);
29694 *ellipsisp = false; /* Initially, assume no ellipsis. */
29696 while (token->type == CPP_COMMA)
29698 cp_parameter_declarator *parmdecl;
29699 tree parm;
29701 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29702 token = cp_lexer_peek_token (parser->lexer);
29704 if (token->type == CPP_ELLIPSIS)
29706 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
29707 *ellipsisp = true;
29708 token = cp_lexer_peek_token (parser->lexer);
29709 break;
29712 /* TODO: parse attributes for tail parameters. */
29713 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
29714 parm = grokdeclarator (parmdecl->declarator,
29715 &parmdecl->decl_specifiers,
29716 PARM, /*initialized=*/0,
29717 /*attrlist=*/NULL);
29719 chainon (params, build_tree_list (NULL_TREE, parm));
29720 token = cp_lexer_peek_token (parser->lexer);
29723 /* We allow tail attributes for the method. */
29724 if (token->keyword == RID_ATTRIBUTE)
29726 if (*attributes == NULL_TREE)
29728 *attributes = cp_parser_attributes_opt (parser);
29729 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29730 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29731 return params;
29733 else
29734 /* We have an error, but parse the attributes, so that we can
29735 carry on. */
29736 *attributes = cp_parser_attributes_opt (parser);
29738 cp_parser_error (parser,
29739 "method attributes must be specified at the end");
29740 return error_mark_node;
29743 return params;
29746 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29748 static void
29749 cp_parser_objc_interstitial_code (cp_parser* parser)
29751 cp_token *token = cp_lexer_peek_token (parser->lexer);
29753 /* If the next token is `extern' and the following token is a string
29754 literal, then we have a linkage specification. */
29755 if (token->keyword == RID_EXTERN
29756 && cp_parser_is_pure_string_literal
29757 (cp_lexer_peek_nth_token (parser->lexer, 2)))
29758 cp_parser_linkage_specification (parser);
29759 /* Handle #pragma, if any. */
29760 else if (token->type == CPP_PRAGMA)
29761 cp_parser_pragma (parser, pragma_objc_icode, NULL);
29762 /* Allow stray semicolons. */
29763 else if (token->type == CPP_SEMICOLON)
29764 cp_lexer_consume_token (parser->lexer);
29765 /* Mark methods as optional or required, when building protocols. */
29766 else if (token->keyword == RID_AT_OPTIONAL)
29768 cp_lexer_consume_token (parser->lexer);
29769 objc_set_method_opt (true);
29771 else if (token->keyword == RID_AT_REQUIRED)
29773 cp_lexer_consume_token (parser->lexer);
29774 objc_set_method_opt (false);
29776 else if (token->keyword == RID_NAMESPACE)
29777 cp_parser_namespace_definition (parser);
29778 /* Other stray characters must generate errors. */
29779 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
29781 cp_lexer_consume_token (parser->lexer);
29782 error ("stray %qs between Objective-C++ methods",
29783 token->type == CPP_OPEN_BRACE ? "{" : "}");
29785 /* Finally, try to parse a block-declaration, or a function-definition. */
29786 else
29787 cp_parser_block_declaration (parser, /*statement_p=*/false);
29790 /* Parse a method signature. */
29792 static tree
29793 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
29795 tree rettype, kwdparms, optparms;
29796 bool ellipsis = false;
29797 bool is_class_method;
29799 is_class_method = cp_parser_objc_method_type (parser);
29800 rettype = cp_parser_objc_typename (parser);
29801 *attributes = NULL_TREE;
29802 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
29803 if (kwdparms == error_mark_node)
29804 return error_mark_node;
29805 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
29806 if (optparms == error_mark_node)
29807 return error_mark_node;
29809 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
29812 static bool
29813 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
29815 tree tattr;
29816 cp_lexer_save_tokens (parser->lexer);
29817 tattr = cp_parser_attributes_opt (parser);
29818 gcc_assert (tattr) ;
29820 /* If the attributes are followed by a method introducer, this is not allowed.
29821 Dump the attributes and flag the situation. */
29822 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
29823 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
29824 return true;
29826 /* Otherwise, the attributes introduce some interstitial code, possibly so
29827 rewind to allow that check. */
29828 cp_lexer_rollback_tokens (parser->lexer);
29829 return false;
29832 /* Parse an Objective-C method prototype list. */
29834 static void
29835 cp_parser_objc_method_prototype_list (cp_parser* parser)
29837 cp_token *token = cp_lexer_peek_token (parser->lexer);
29839 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29841 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29843 tree attributes, sig;
29844 bool is_class_method;
29845 if (token->type == CPP_PLUS)
29846 is_class_method = true;
29847 else
29848 is_class_method = false;
29849 sig = cp_parser_objc_method_signature (parser, &attributes);
29850 if (sig == error_mark_node)
29852 cp_parser_skip_to_end_of_block_or_statement (parser);
29853 token = cp_lexer_peek_token (parser->lexer);
29854 continue;
29856 objc_add_method_declaration (is_class_method, sig, attributes);
29857 cp_parser_consume_semicolon_at_end_of_statement (parser);
29859 else if (token->keyword == RID_AT_PROPERTY)
29860 cp_parser_objc_at_property_declaration (parser);
29861 else if (token->keyword == RID_ATTRIBUTE
29862 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29863 warning_at (cp_lexer_peek_token (parser->lexer)->location,
29864 OPT_Wattributes,
29865 "prefix attributes are ignored for methods");
29866 else
29867 /* Allow for interspersed non-ObjC++ code. */
29868 cp_parser_objc_interstitial_code (parser);
29870 token = cp_lexer_peek_token (parser->lexer);
29873 if (token->type != CPP_EOF)
29874 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29875 else
29876 cp_parser_error (parser, "expected %<@end%>");
29878 objc_finish_interface ();
29881 /* Parse an Objective-C method definition list. */
29883 static void
29884 cp_parser_objc_method_definition_list (cp_parser* parser)
29886 cp_token *token = cp_lexer_peek_token (parser->lexer);
29888 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29890 tree meth;
29892 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29894 cp_token *ptk;
29895 tree sig, attribute;
29896 bool is_class_method;
29897 if (token->type == CPP_PLUS)
29898 is_class_method = true;
29899 else
29900 is_class_method = false;
29901 push_deferring_access_checks (dk_deferred);
29902 sig = cp_parser_objc_method_signature (parser, &attribute);
29903 if (sig == error_mark_node)
29905 cp_parser_skip_to_end_of_block_or_statement (parser);
29906 token = cp_lexer_peek_token (parser->lexer);
29907 continue;
29909 objc_start_method_definition (is_class_method, sig, attribute,
29910 NULL_TREE);
29912 /* For historical reasons, we accept an optional semicolon. */
29913 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29914 cp_lexer_consume_token (parser->lexer);
29916 ptk = cp_lexer_peek_token (parser->lexer);
29917 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
29918 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
29920 perform_deferred_access_checks (tf_warning_or_error);
29921 stop_deferring_access_checks ();
29922 meth = cp_parser_function_definition_after_declarator (parser,
29923 false);
29924 pop_deferring_access_checks ();
29925 objc_finish_method_definition (meth);
29928 /* The following case will be removed once @synthesize is
29929 completely implemented. */
29930 else if (token->keyword == RID_AT_PROPERTY)
29931 cp_parser_objc_at_property_declaration (parser);
29932 else if (token->keyword == RID_AT_SYNTHESIZE)
29933 cp_parser_objc_at_synthesize_declaration (parser);
29934 else if (token->keyword == RID_AT_DYNAMIC)
29935 cp_parser_objc_at_dynamic_declaration (parser);
29936 else if (token->keyword == RID_ATTRIBUTE
29937 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29938 warning_at (token->location, OPT_Wattributes,
29939 "prefix attributes are ignored for methods");
29940 else
29941 /* Allow for interspersed non-ObjC++ code. */
29942 cp_parser_objc_interstitial_code (parser);
29944 token = cp_lexer_peek_token (parser->lexer);
29947 if (token->type != CPP_EOF)
29948 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29949 else
29950 cp_parser_error (parser, "expected %<@end%>");
29952 objc_finish_implementation ();
29955 /* Parse Objective-C ivars. */
29957 static void
29958 cp_parser_objc_class_ivars (cp_parser* parser)
29960 cp_token *token = cp_lexer_peek_token (parser->lexer);
29962 if (token->type != CPP_OPEN_BRACE)
29963 return; /* No ivars specified. */
29965 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
29966 token = cp_lexer_peek_token (parser->lexer);
29968 while (token->type != CPP_CLOSE_BRACE
29969 && token->keyword != RID_AT_END && token->type != CPP_EOF)
29971 cp_decl_specifier_seq declspecs;
29972 int decl_class_or_enum_p;
29973 tree prefix_attributes;
29975 cp_parser_objc_visibility_spec (parser);
29977 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29978 break;
29980 cp_parser_decl_specifier_seq (parser,
29981 CP_PARSER_FLAGS_OPTIONAL,
29982 &declspecs,
29983 &decl_class_or_enum_p);
29985 /* auto, register, static, extern, mutable. */
29986 if (declspecs.storage_class != sc_none)
29988 cp_parser_error (parser, "invalid type for instance variable");
29989 declspecs.storage_class = sc_none;
29992 /* thread_local. */
29993 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
29995 cp_parser_error (parser, "invalid type for instance variable");
29996 declspecs.locations[ds_thread] = 0;
29999 /* typedef. */
30000 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30002 cp_parser_error (parser, "invalid type for instance variable");
30003 declspecs.locations[ds_typedef] = 0;
30006 prefix_attributes = declspecs.attributes;
30007 declspecs.attributes = NULL_TREE;
30009 /* Keep going until we hit the `;' at the end of the
30010 declaration. */
30011 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30013 tree width = NULL_TREE, attributes, first_attribute, decl;
30014 cp_declarator *declarator = NULL;
30015 int ctor_dtor_or_conv_p;
30017 /* Check for a (possibly unnamed) bitfield declaration. */
30018 token = cp_lexer_peek_token (parser->lexer);
30019 if (token->type == CPP_COLON)
30020 goto eat_colon;
30022 if (token->type == CPP_NAME
30023 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30024 == CPP_COLON))
30026 /* Get the name of the bitfield. */
30027 declarator = make_id_declarator (NULL_TREE,
30028 cp_parser_identifier (parser),
30029 sfk_none);
30031 eat_colon:
30032 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30033 /* Get the width of the bitfield. */
30034 width
30035 = cp_parser_constant_expression (parser);
30037 else
30039 /* Parse the declarator. */
30040 declarator
30041 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30042 &ctor_dtor_or_conv_p,
30043 /*parenthesized_p=*/NULL,
30044 /*member_p=*/false,
30045 /*friend_p=*/false);
30048 /* Look for attributes that apply to the ivar. */
30049 attributes = cp_parser_attributes_opt (parser);
30050 /* Remember which attributes are prefix attributes and
30051 which are not. */
30052 first_attribute = attributes;
30053 /* Combine the attributes. */
30054 attributes = chainon (prefix_attributes, attributes);
30056 if (width)
30057 /* Create the bitfield declaration. */
30058 decl = grokbitfield (declarator, &declspecs,
30059 width, NULL_TREE, attributes);
30060 else
30061 decl = grokfield (declarator, &declspecs,
30062 NULL_TREE, /*init_const_expr_p=*/false,
30063 NULL_TREE, attributes);
30065 /* Add the instance variable. */
30066 if (decl != error_mark_node && decl != NULL_TREE)
30067 objc_add_instance_variable (decl);
30069 /* Reset PREFIX_ATTRIBUTES. */
30070 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30071 attributes = TREE_CHAIN (attributes);
30072 if (attributes)
30073 TREE_CHAIN (attributes) = NULL_TREE;
30075 token = cp_lexer_peek_token (parser->lexer);
30077 if (token->type == CPP_COMMA)
30079 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30080 continue;
30082 break;
30085 cp_parser_consume_semicolon_at_end_of_statement (parser);
30086 token = cp_lexer_peek_token (parser->lexer);
30089 if (token->keyword == RID_AT_END)
30090 cp_parser_error (parser, "expected %<}%>");
30092 /* Do not consume the RID_AT_END, so it will be read again as terminating
30093 the @interface of @implementation. */
30094 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30095 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30097 /* For historical reasons, we accept an optional semicolon. */
30098 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30099 cp_lexer_consume_token (parser->lexer);
30102 /* Parse an Objective-C protocol declaration. */
30104 static void
30105 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30107 tree proto, protorefs;
30108 cp_token *tok;
30110 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30111 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30113 tok = cp_lexer_peek_token (parser->lexer);
30114 error_at (tok->location, "identifier expected after %<@protocol%>");
30115 cp_parser_consume_semicolon_at_end_of_statement (parser);
30116 return;
30119 /* See if we have a forward declaration or a definition. */
30120 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30122 /* Try a forward declaration first. */
30123 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30125 while (true)
30127 tree id;
30129 id = cp_parser_identifier (parser);
30130 if (id == error_mark_node)
30131 break;
30133 objc_declare_protocol (id, attributes);
30135 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30136 cp_lexer_consume_token (parser->lexer);
30137 else
30138 break;
30140 cp_parser_consume_semicolon_at_end_of_statement (parser);
30143 /* Ok, we got a full-fledged definition (or at least should). */
30144 else
30146 proto = cp_parser_identifier (parser);
30147 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30148 objc_start_protocol (proto, protorefs, attributes);
30149 cp_parser_objc_method_prototype_list (parser);
30153 /* Parse an Objective-C superclass or category. */
30155 static void
30156 cp_parser_objc_superclass_or_category (cp_parser *parser,
30157 bool iface_p,
30158 tree *super,
30159 tree *categ, bool *is_class_extension)
30161 cp_token *next = cp_lexer_peek_token (parser->lexer);
30163 *super = *categ = NULL_TREE;
30164 *is_class_extension = false;
30165 if (next->type == CPP_COLON)
30167 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30168 *super = cp_parser_identifier (parser);
30170 else if (next->type == CPP_OPEN_PAREN)
30172 matching_parens parens;
30173 parens.consume_open (parser); /* Eat '('. */
30175 /* If there is no category name, and this is an @interface, we
30176 have a class extension. */
30177 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30179 *categ = NULL_TREE;
30180 *is_class_extension = true;
30182 else
30183 *categ = cp_parser_identifier (parser);
30185 parens.require_close (parser);
30189 /* Parse an Objective-C class interface. */
30191 static void
30192 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30194 tree name, super, categ, protos;
30195 bool is_class_extension;
30197 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30198 name = cp_parser_identifier (parser);
30199 if (name == error_mark_node)
30201 /* It's hard to recover because even if valid @interface stuff
30202 is to follow, we can't compile it (or validate it) if we
30203 don't even know which class it refers to. Let's assume this
30204 was a stray '@interface' token in the stream and skip it.
30206 return;
30208 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30209 &is_class_extension);
30210 protos = cp_parser_objc_protocol_refs_opt (parser);
30212 /* We have either a class or a category on our hands. */
30213 if (categ || is_class_extension)
30214 objc_start_category_interface (name, categ, protos, attributes);
30215 else
30217 objc_start_class_interface (name, super, protos, attributes);
30218 /* Handle instance variable declarations, if any. */
30219 cp_parser_objc_class_ivars (parser);
30220 objc_continue_interface ();
30223 cp_parser_objc_method_prototype_list (parser);
30226 /* Parse an Objective-C class implementation. */
30228 static void
30229 cp_parser_objc_class_implementation (cp_parser* parser)
30231 tree name, super, categ;
30232 bool is_class_extension;
30234 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30235 name = cp_parser_identifier (parser);
30236 if (name == error_mark_node)
30238 /* It's hard to recover because even if valid @implementation
30239 stuff is to follow, we can't compile it (or validate it) if
30240 we don't even know which class it refers to. Let's assume
30241 this was a stray '@implementation' token in the stream and
30242 skip it.
30244 return;
30246 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30247 &is_class_extension);
30249 /* We have either a class or a category on our hands. */
30250 if (categ)
30251 objc_start_category_implementation (name, categ);
30252 else
30254 objc_start_class_implementation (name, super);
30255 /* Handle instance variable declarations, if any. */
30256 cp_parser_objc_class_ivars (parser);
30257 objc_continue_implementation ();
30260 cp_parser_objc_method_definition_list (parser);
30263 /* Consume the @end token and finish off the implementation. */
30265 static void
30266 cp_parser_objc_end_implementation (cp_parser* parser)
30268 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30269 objc_finish_implementation ();
30272 /* Parse an Objective-C declaration. */
30274 static void
30275 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30277 /* Try to figure out what kind of declaration is present. */
30278 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30280 if (attributes)
30281 switch (kwd->keyword)
30283 case RID_AT_ALIAS:
30284 case RID_AT_CLASS:
30285 case RID_AT_END:
30286 error_at (kwd->location, "attributes may not be specified before"
30287 " the %<@%D%> Objective-C++ keyword",
30288 kwd->u.value);
30289 attributes = NULL;
30290 break;
30291 case RID_AT_IMPLEMENTATION:
30292 warning_at (kwd->location, OPT_Wattributes,
30293 "prefix attributes are ignored before %<@%D%>",
30294 kwd->u.value);
30295 attributes = NULL;
30296 default:
30297 break;
30300 switch (kwd->keyword)
30302 case RID_AT_ALIAS:
30303 cp_parser_objc_alias_declaration (parser);
30304 break;
30305 case RID_AT_CLASS:
30306 cp_parser_objc_class_declaration (parser);
30307 break;
30308 case RID_AT_PROTOCOL:
30309 cp_parser_objc_protocol_declaration (parser, attributes);
30310 break;
30311 case RID_AT_INTERFACE:
30312 cp_parser_objc_class_interface (parser, attributes);
30313 break;
30314 case RID_AT_IMPLEMENTATION:
30315 cp_parser_objc_class_implementation (parser);
30316 break;
30317 case RID_AT_END:
30318 cp_parser_objc_end_implementation (parser);
30319 break;
30320 default:
30321 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30322 kwd->u.value);
30323 cp_parser_skip_to_end_of_block_or_statement (parser);
30327 /* Parse an Objective-C try-catch-finally statement.
30329 objc-try-catch-finally-stmt:
30330 @try compound-statement objc-catch-clause-seq [opt]
30331 objc-finally-clause [opt]
30333 objc-catch-clause-seq:
30334 objc-catch-clause objc-catch-clause-seq [opt]
30336 objc-catch-clause:
30337 @catch ( objc-exception-declaration ) compound-statement
30339 objc-finally-clause:
30340 @finally compound-statement
30342 objc-exception-declaration:
30343 parameter-declaration
30344 '...'
30346 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30348 Returns NULL_TREE.
30350 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30351 for C. Keep them in sync. */
30353 static tree
30354 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30356 location_t location;
30357 tree stmt;
30359 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30360 location = cp_lexer_peek_token (parser->lexer)->location;
30361 objc_maybe_warn_exceptions (location);
30362 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30363 node, lest it get absorbed into the surrounding block. */
30364 stmt = push_stmt_list ();
30365 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30366 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30368 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30370 cp_parameter_declarator *parm;
30371 tree parameter_declaration = error_mark_node;
30372 bool seen_open_paren = false;
30373 matching_parens parens;
30375 cp_lexer_consume_token (parser->lexer);
30376 if (parens.require_open (parser))
30377 seen_open_paren = true;
30378 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30380 /* We have "@catch (...)" (where the '...' are literally
30381 what is in the code). Skip the '...'.
30382 parameter_declaration is set to NULL_TREE, and
30383 objc_being_catch_clauses() knows that that means
30384 '...'. */
30385 cp_lexer_consume_token (parser->lexer);
30386 parameter_declaration = NULL_TREE;
30388 else
30390 /* We have "@catch (NSException *exception)" or something
30391 like that. Parse the parameter declaration. */
30392 parm = cp_parser_parameter_declaration (parser, false, NULL);
30393 if (parm == NULL)
30394 parameter_declaration = error_mark_node;
30395 else
30396 parameter_declaration = grokdeclarator (parm->declarator,
30397 &parm->decl_specifiers,
30398 PARM, /*initialized=*/0,
30399 /*attrlist=*/NULL);
30401 if (seen_open_paren)
30402 parens.require_close (parser);
30403 else
30405 /* If there was no open parenthesis, we are recovering from
30406 an error, and we are trying to figure out what mistake
30407 the user has made. */
30409 /* If there is an immediate closing parenthesis, the user
30410 probably forgot the opening one (ie, they typed "@catch
30411 NSException *e)". Parse the closing parenthesis and keep
30412 going. */
30413 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30414 cp_lexer_consume_token (parser->lexer);
30416 /* If these is no immediate closing parenthesis, the user
30417 probably doesn't know that parenthesis are required at
30418 all (ie, they typed "@catch NSException *e"). So, just
30419 forget about the closing parenthesis and keep going. */
30421 objc_begin_catch_clause (parameter_declaration);
30422 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30423 objc_finish_catch_clause ();
30425 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30427 cp_lexer_consume_token (parser->lexer);
30428 location = cp_lexer_peek_token (parser->lexer)->location;
30429 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30430 node, lest it get absorbed into the surrounding block. */
30431 stmt = push_stmt_list ();
30432 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30433 objc_build_finally_clause (location, pop_stmt_list (stmt));
30436 return objc_finish_try_stmt ();
30439 /* Parse an Objective-C synchronized statement.
30441 objc-synchronized-stmt:
30442 @synchronized ( expression ) compound-statement
30444 Returns NULL_TREE. */
30446 static tree
30447 cp_parser_objc_synchronized_statement (cp_parser *parser)
30449 location_t location;
30450 tree lock, stmt;
30452 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30454 location = cp_lexer_peek_token (parser->lexer)->location;
30455 objc_maybe_warn_exceptions (location);
30456 matching_parens parens;
30457 parens.require_open (parser);
30458 lock = cp_parser_expression (parser);
30459 parens.require_close (parser);
30461 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30462 node, lest it get absorbed into the surrounding block. */
30463 stmt = push_stmt_list ();
30464 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30466 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30469 /* Parse an Objective-C throw statement.
30471 objc-throw-stmt:
30472 @throw assignment-expression [opt] ;
30474 Returns a constructed '@throw' statement. */
30476 static tree
30477 cp_parser_objc_throw_statement (cp_parser *parser)
30479 tree expr = NULL_TREE;
30480 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30482 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30484 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30485 expr = cp_parser_expression (parser);
30487 cp_parser_consume_semicolon_at_end_of_statement (parser);
30489 return objc_build_throw_stmt (loc, expr);
30492 /* Parse an Objective-C statement. */
30494 static tree
30495 cp_parser_objc_statement (cp_parser * parser)
30497 /* Try to figure out what kind of declaration is present. */
30498 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30500 switch (kwd->keyword)
30502 case RID_AT_TRY:
30503 return cp_parser_objc_try_catch_finally_statement (parser);
30504 case RID_AT_SYNCHRONIZED:
30505 return cp_parser_objc_synchronized_statement (parser);
30506 case RID_AT_THROW:
30507 return cp_parser_objc_throw_statement (parser);
30508 default:
30509 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30510 kwd->u.value);
30511 cp_parser_skip_to_end_of_block_or_statement (parser);
30514 return error_mark_node;
30517 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30518 look ahead to see if an objc keyword follows the attributes. This
30519 is to detect the use of prefix attributes on ObjC @interface and
30520 @protocol. */
30522 static bool
30523 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30525 cp_lexer_save_tokens (parser->lexer);
30526 *attrib = cp_parser_attributes_opt (parser);
30527 gcc_assert (*attrib);
30528 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30530 cp_lexer_commit_tokens (parser->lexer);
30531 return true;
30533 cp_lexer_rollback_tokens (parser->lexer);
30534 return false;
30537 /* This routine is a minimal replacement for
30538 c_parser_struct_declaration () used when parsing the list of
30539 types/names or ObjC++ properties. For example, when parsing the
30540 code
30542 @property (readonly) int a, b, c;
30544 this function is responsible for parsing "int a, int b, int c" and
30545 returning the declarations as CHAIN of DECLs.
30547 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30548 similar parsing. */
30549 static tree
30550 cp_parser_objc_struct_declaration (cp_parser *parser)
30552 tree decls = NULL_TREE;
30553 cp_decl_specifier_seq declspecs;
30554 int decl_class_or_enum_p;
30555 tree prefix_attributes;
30557 cp_parser_decl_specifier_seq (parser,
30558 CP_PARSER_FLAGS_NONE,
30559 &declspecs,
30560 &decl_class_or_enum_p);
30562 if (declspecs.type == error_mark_node)
30563 return error_mark_node;
30565 /* auto, register, static, extern, mutable. */
30566 if (declspecs.storage_class != sc_none)
30568 cp_parser_error (parser, "invalid type for property");
30569 declspecs.storage_class = sc_none;
30572 /* thread_local. */
30573 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30575 cp_parser_error (parser, "invalid type for property");
30576 declspecs.locations[ds_thread] = 0;
30579 /* typedef. */
30580 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30582 cp_parser_error (parser, "invalid type for property");
30583 declspecs.locations[ds_typedef] = 0;
30586 prefix_attributes = declspecs.attributes;
30587 declspecs.attributes = NULL_TREE;
30589 /* Keep going until we hit the `;' at the end of the declaration. */
30590 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30592 tree attributes, first_attribute, decl;
30593 cp_declarator *declarator;
30594 cp_token *token;
30596 /* Parse the declarator. */
30597 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30598 NULL, NULL, false, false);
30600 /* Look for attributes that apply to the ivar. */
30601 attributes = cp_parser_attributes_opt (parser);
30602 /* Remember which attributes are prefix attributes and
30603 which are not. */
30604 first_attribute = attributes;
30605 /* Combine the attributes. */
30606 attributes = chainon (prefix_attributes, attributes);
30608 decl = grokfield (declarator, &declspecs,
30609 NULL_TREE, /*init_const_expr_p=*/false,
30610 NULL_TREE, attributes);
30612 if (decl == error_mark_node || decl == NULL_TREE)
30613 return error_mark_node;
30615 /* Reset PREFIX_ATTRIBUTES. */
30616 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30617 attributes = TREE_CHAIN (attributes);
30618 if (attributes)
30619 TREE_CHAIN (attributes) = NULL_TREE;
30621 DECL_CHAIN (decl) = decls;
30622 decls = decl;
30624 token = cp_lexer_peek_token (parser->lexer);
30625 if (token->type == CPP_COMMA)
30627 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30628 continue;
30630 else
30631 break;
30633 return decls;
30636 /* Parse an Objective-C @property declaration. The syntax is:
30638 objc-property-declaration:
30639 '@property' objc-property-attributes[opt] struct-declaration ;
30641 objc-property-attributes:
30642 '(' objc-property-attribute-list ')'
30644 objc-property-attribute-list:
30645 objc-property-attribute
30646 objc-property-attribute-list, objc-property-attribute
30648 objc-property-attribute
30649 'getter' = identifier
30650 'setter' = identifier
30651 'readonly'
30652 'readwrite'
30653 'assign'
30654 'retain'
30655 'copy'
30656 'nonatomic'
30658 For example:
30659 @property NSString *name;
30660 @property (readonly) id object;
30661 @property (retain, nonatomic, getter=getTheName) id name;
30662 @property int a, b, c;
30664 PS: This function is identical to
30665 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30666 static void
30667 cp_parser_objc_at_property_declaration (cp_parser *parser)
30669 /* The following variables hold the attributes of the properties as
30670 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30671 seen. When we see an attribute, we set them to 'true' (if they
30672 are boolean properties) or to the identifier (if they have an
30673 argument, ie, for getter and setter). Note that here we only
30674 parse the list of attributes, check the syntax and accumulate the
30675 attributes that we find. objc_add_property_declaration() will
30676 then process the information. */
30677 bool property_assign = false;
30678 bool property_copy = false;
30679 tree property_getter_ident = NULL_TREE;
30680 bool property_nonatomic = false;
30681 bool property_readonly = false;
30682 bool property_readwrite = false;
30683 bool property_retain = false;
30684 tree property_setter_ident = NULL_TREE;
30686 /* 'properties' is the list of properties that we read. Usually a
30687 single one, but maybe more (eg, in "@property int a, b, c;" there
30688 are three). */
30689 tree properties;
30690 location_t loc;
30692 loc = cp_lexer_peek_token (parser->lexer)->location;
30694 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
30696 /* Parse the optional attribute list... */
30697 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30699 /* Eat the '('. */
30700 matching_parens parens;
30701 parens.consume_open (parser);
30703 while (true)
30705 bool syntax_error = false;
30706 cp_token *token = cp_lexer_peek_token (parser->lexer);
30707 enum rid keyword;
30709 if (token->type != CPP_NAME)
30711 cp_parser_error (parser, "expected identifier");
30712 break;
30714 keyword = C_RID_CODE (token->u.value);
30715 cp_lexer_consume_token (parser->lexer);
30716 switch (keyword)
30718 case RID_ASSIGN: property_assign = true; break;
30719 case RID_COPY: property_copy = true; break;
30720 case RID_NONATOMIC: property_nonatomic = true; break;
30721 case RID_READONLY: property_readonly = true; break;
30722 case RID_READWRITE: property_readwrite = true; break;
30723 case RID_RETAIN: property_retain = true; break;
30725 case RID_GETTER:
30726 case RID_SETTER:
30727 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30729 if (keyword == RID_GETTER)
30730 cp_parser_error (parser,
30731 "missing %<=%> (after %<getter%> attribute)");
30732 else
30733 cp_parser_error (parser,
30734 "missing %<=%> (after %<setter%> attribute)");
30735 syntax_error = true;
30736 break;
30738 cp_lexer_consume_token (parser->lexer); /* eat the = */
30739 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
30741 cp_parser_error (parser, "expected identifier");
30742 syntax_error = true;
30743 break;
30745 if (keyword == RID_SETTER)
30747 if (property_setter_ident != NULL_TREE)
30749 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
30750 cp_lexer_consume_token (parser->lexer);
30752 else
30753 property_setter_ident = cp_parser_objc_selector (parser);
30754 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30755 cp_parser_error (parser, "setter name must terminate with %<:%>");
30756 else
30757 cp_lexer_consume_token (parser->lexer);
30759 else
30761 if (property_getter_ident != NULL_TREE)
30763 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
30764 cp_lexer_consume_token (parser->lexer);
30766 else
30767 property_getter_ident = cp_parser_objc_selector (parser);
30769 break;
30770 default:
30771 cp_parser_error (parser, "unknown property attribute");
30772 syntax_error = true;
30773 break;
30776 if (syntax_error)
30777 break;
30779 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30780 cp_lexer_consume_token (parser->lexer);
30781 else
30782 break;
30785 /* FIXME: "@property (setter, assign);" will generate a spurious
30786 "error: expected ‘)’ before ‘,’ token". This is because
30787 cp_parser_require, unlike the C counterpart, will produce an
30788 error even if we are in error recovery. */
30789 if (!parens.require_close (parser))
30791 cp_parser_skip_to_closing_parenthesis (parser,
30792 /*recovering=*/true,
30793 /*or_comma=*/false,
30794 /*consume_paren=*/true);
30798 /* ... and the property declaration(s). */
30799 properties = cp_parser_objc_struct_declaration (parser);
30801 if (properties == error_mark_node)
30803 cp_parser_skip_to_end_of_statement (parser);
30804 /* If the next token is now a `;', consume it. */
30805 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30806 cp_lexer_consume_token (parser->lexer);
30807 return;
30810 if (properties == NULL_TREE)
30811 cp_parser_error (parser, "expected identifier");
30812 else
30814 /* Comma-separated properties are chained together in
30815 reverse order; add them one by one. */
30816 properties = nreverse (properties);
30818 for (; properties; properties = TREE_CHAIN (properties))
30819 objc_add_property_declaration (loc, copy_node (properties),
30820 property_readonly, property_readwrite,
30821 property_assign, property_retain,
30822 property_copy, property_nonatomic,
30823 property_getter_ident, property_setter_ident);
30826 cp_parser_consume_semicolon_at_end_of_statement (parser);
30829 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
30831 objc-synthesize-declaration:
30832 @synthesize objc-synthesize-identifier-list ;
30834 objc-synthesize-identifier-list:
30835 objc-synthesize-identifier
30836 objc-synthesize-identifier-list, objc-synthesize-identifier
30838 objc-synthesize-identifier
30839 identifier
30840 identifier = identifier
30842 For example:
30843 @synthesize MyProperty;
30844 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
30846 PS: This function is identical to c_parser_objc_at_synthesize_declaration
30847 for C. Keep them in sync.
30849 static void
30850 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
30852 tree list = NULL_TREE;
30853 location_t loc;
30854 loc = cp_lexer_peek_token (parser->lexer)->location;
30856 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
30857 while (true)
30859 tree property, ivar;
30860 property = cp_parser_identifier (parser);
30861 if (property == error_mark_node)
30863 cp_parser_consume_semicolon_at_end_of_statement (parser);
30864 return;
30866 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
30868 cp_lexer_consume_token (parser->lexer);
30869 ivar = cp_parser_identifier (parser);
30870 if (ivar == error_mark_node)
30872 cp_parser_consume_semicolon_at_end_of_statement (parser);
30873 return;
30876 else
30877 ivar = NULL_TREE;
30878 list = chainon (list, build_tree_list (ivar, property));
30879 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30880 cp_lexer_consume_token (parser->lexer);
30881 else
30882 break;
30884 cp_parser_consume_semicolon_at_end_of_statement (parser);
30885 objc_add_synthesize_declaration (loc, list);
30888 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
30890 objc-dynamic-declaration:
30891 @dynamic identifier-list ;
30893 For example:
30894 @dynamic MyProperty;
30895 @dynamic MyProperty, AnotherProperty;
30897 PS: This function is identical to c_parser_objc_at_dynamic_declaration
30898 for C. Keep them in sync.
30900 static void
30901 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
30903 tree list = NULL_TREE;
30904 location_t loc;
30905 loc = cp_lexer_peek_token (parser->lexer)->location;
30907 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
30908 while (true)
30910 tree property;
30911 property = cp_parser_identifier (parser);
30912 if (property == error_mark_node)
30914 cp_parser_consume_semicolon_at_end_of_statement (parser);
30915 return;
30917 list = chainon (list, build_tree_list (NULL, property));
30918 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30919 cp_lexer_consume_token (parser->lexer);
30920 else
30921 break;
30923 cp_parser_consume_semicolon_at_end_of_statement (parser);
30924 objc_add_dynamic_declaration (loc, list);
30928 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
30930 /* Returns name of the next clause.
30931 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
30932 the token is not consumed. Otherwise appropriate pragma_omp_clause is
30933 returned and the token is consumed. */
30935 static pragma_omp_clause
30936 cp_parser_omp_clause_name (cp_parser *parser)
30938 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
30940 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
30941 result = PRAGMA_OACC_CLAUSE_AUTO;
30942 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
30943 result = PRAGMA_OMP_CLAUSE_IF;
30944 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
30945 result = PRAGMA_OMP_CLAUSE_DEFAULT;
30946 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
30947 result = PRAGMA_OACC_CLAUSE_DELETE;
30948 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
30949 result = PRAGMA_OMP_CLAUSE_PRIVATE;
30950 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30951 result = PRAGMA_OMP_CLAUSE_FOR;
30952 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30954 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30955 const char *p = IDENTIFIER_POINTER (id);
30957 switch (p[0])
30959 case 'a':
30960 if (!strcmp ("aligned", p))
30961 result = PRAGMA_OMP_CLAUSE_ALIGNED;
30962 else if (!strcmp ("async", p))
30963 result = PRAGMA_OACC_CLAUSE_ASYNC;
30964 break;
30965 case 'c':
30966 if (!strcmp ("collapse", p))
30967 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
30968 else if (!strcmp ("copy", p))
30969 result = PRAGMA_OACC_CLAUSE_COPY;
30970 else if (!strcmp ("copyin", p))
30971 result = PRAGMA_OMP_CLAUSE_COPYIN;
30972 else if (!strcmp ("copyout", p))
30973 result = PRAGMA_OACC_CLAUSE_COPYOUT;
30974 else if (!strcmp ("copyprivate", p))
30975 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
30976 else if (!strcmp ("create", p))
30977 result = PRAGMA_OACC_CLAUSE_CREATE;
30978 break;
30979 case 'd':
30980 if (!strcmp ("defaultmap", p))
30981 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
30982 else if (!strcmp ("depend", p))
30983 result = PRAGMA_OMP_CLAUSE_DEPEND;
30984 else if (!strcmp ("device", p))
30985 result = PRAGMA_OMP_CLAUSE_DEVICE;
30986 else if (!strcmp ("deviceptr", p))
30987 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
30988 else if (!strcmp ("device_resident", p))
30989 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
30990 else if (!strcmp ("dist_schedule", p))
30991 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
30992 break;
30993 case 'f':
30994 if (!strcmp ("final", p))
30995 result = PRAGMA_OMP_CLAUSE_FINAL;
30996 else if (!strcmp ("firstprivate", p))
30997 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
30998 else if (!strcmp ("from", p))
30999 result = PRAGMA_OMP_CLAUSE_FROM;
31000 break;
31001 case 'g':
31002 if (!strcmp ("gang", p))
31003 result = PRAGMA_OACC_CLAUSE_GANG;
31004 else if (!strcmp ("grainsize", p))
31005 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31006 break;
31007 case 'h':
31008 if (!strcmp ("hint", p))
31009 result = PRAGMA_OMP_CLAUSE_HINT;
31010 else if (!strcmp ("host", p))
31011 result = PRAGMA_OACC_CLAUSE_HOST;
31012 break;
31013 case 'i':
31014 if (!strcmp ("inbranch", p))
31015 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31016 else if (!strcmp ("independent", p))
31017 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31018 else if (!strcmp ("is_device_ptr", p))
31019 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31020 break;
31021 case 'l':
31022 if (!strcmp ("lastprivate", p))
31023 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31024 else if (!strcmp ("linear", p))
31025 result = PRAGMA_OMP_CLAUSE_LINEAR;
31026 else if (!strcmp ("link", p))
31027 result = PRAGMA_OMP_CLAUSE_LINK;
31028 break;
31029 case 'm':
31030 if (!strcmp ("map", p))
31031 result = PRAGMA_OMP_CLAUSE_MAP;
31032 else if (!strcmp ("mergeable", p))
31033 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31034 break;
31035 case 'n':
31036 if (!strcmp ("nogroup", p))
31037 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31038 else if (!strcmp ("notinbranch", p))
31039 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31040 else if (!strcmp ("nowait", p))
31041 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31042 else if (!strcmp ("num_gangs", p))
31043 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31044 else if (!strcmp ("num_tasks", p))
31045 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31046 else if (!strcmp ("num_teams", p))
31047 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31048 else if (!strcmp ("num_threads", p))
31049 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31050 else if (!strcmp ("num_workers", p))
31051 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31052 break;
31053 case 'o':
31054 if (!strcmp ("ordered", p))
31055 result = PRAGMA_OMP_CLAUSE_ORDERED;
31056 break;
31057 case 'p':
31058 if (!strcmp ("parallel", p))
31059 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31060 else if (!strcmp ("present", p))
31061 result = PRAGMA_OACC_CLAUSE_PRESENT;
31062 else if (!strcmp ("present_or_copy", p)
31063 || !strcmp ("pcopy", p))
31064 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
31065 else if (!strcmp ("present_or_copyin", p)
31066 || !strcmp ("pcopyin", p))
31067 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
31068 else if (!strcmp ("present_or_copyout", p)
31069 || !strcmp ("pcopyout", p))
31070 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
31071 else if (!strcmp ("present_or_create", p)
31072 || !strcmp ("pcreate", p))
31073 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
31074 else if (!strcmp ("priority", p))
31075 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31076 else if (!strcmp ("proc_bind", p))
31077 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31078 break;
31079 case 'r':
31080 if (!strcmp ("reduction", p))
31081 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31082 break;
31083 case 's':
31084 if (!strcmp ("safelen", p))
31085 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31086 else if (!strcmp ("schedule", p))
31087 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31088 else if (!strcmp ("sections", p))
31089 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31090 else if (!strcmp ("self", p))
31091 result = PRAGMA_OACC_CLAUSE_SELF;
31092 else if (!strcmp ("seq", p))
31093 result = PRAGMA_OACC_CLAUSE_SEQ;
31094 else if (!strcmp ("shared", p))
31095 result = PRAGMA_OMP_CLAUSE_SHARED;
31096 else if (!strcmp ("simd", p))
31097 result = PRAGMA_OMP_CLAUSE_SIMD;
31098 else if (!strcmp ("simdlen", p))
31099 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31100 break;
31101 case 't':
31102 if (!strcmp ("taskgroup", p))
31103 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31104 else if (!strcmp ("thread_limit", p))
31105 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31106 else if (!strcmp ("threads", p))
31107 result = PRAGMA_OMP_CLAUSE_THREADS;
31108 else if (!strcmp ("tile", p))
31109 result = PRAGMA_OACC_CLAUSE_TILE;
31110 else if (!strcmp ("to", p))
31111 result = PRAGMA_OMP_CLAUSE_TO;
31112 break;
31113 case 'u':
31114 if (!strcmp ("uniform", p))
31115 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31116 else if (!strcmp ("untied", p))
31117 result = PRAGMA_OMP_CLAUSE_UNTIED;
31118 else if (!strcmp ("use_device", p))
31119 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31120 else if (!strcmp ("use_device_ptr", p))
31121 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31122 break;
31123 case 'v':
31124 if (!strcmp ("vector", p))
31125 result = PRAGMA_OACC_CLAUSE_VECTOR;
31126 else if (!strcmp ("vector_length", p))
31127 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31128 break;
31129 case 'w':
31130 if (!strcmp ("wait", p))
31131 result = PRAGMA_OACC_CLAUSE_WAIT;
31132 else if (!strcmp ("worker", p))
31133 result = PRAGMA_OACC_CLAUSE_WORKER;
31134 break;
31138 if (result != PRAGMA_OMP_CLAUSE_NONE)
31139 cp_lexer_consume_token (parser->lexer);
31141 return result;
31144 /* Validate that a clause of the given type does not already exist. */
31146 static void
31147 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31148 const char *name, location_t location)
31150 tree c;
31152 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31153 if (OMP_CLAUSE_CODE (c) == code)
31155 error_at (location, "too many %qs clauses", name);
31156 break;
31160 /* OpenMP 2.5:
31161 variable-list:
31162 identifier
31163 variable-list , identifier
31165 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31166 colon). An opening parenthesis will have been consumed by the caller.
31168 If KIND is nonzero, create the appropriate node and install the decl
31169 in OMP_CLAUSE_DECL and add the node to the head of the list.
31171 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31172 return the list created.
31174 COLON can be NULL if only closing parenthesis should end the list,
31175 or pointer to bool which will receive false if the list is terminated
31176 by closing parenthesis or true if the list is terminated by colon. */
31178 static tree
31179 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31180 tree list, bool *colon)
31182 cp_token *token;
31183 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31184 if (colon)
31186 parser->colon_corrects_to_scope_p = false;
31187 *colon = false;
31189 while (1)
31191 tree name, decl;
31193 token = cp_lexer_peek_token (parser->lexer);
31194 if (kind != 0
31195 && current_class_ptr
31196 && cp_parser_is_keyword (token, RID_THIS))
31198 decl = finish_this_expr ();
31199 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31200 || CONVERT_EXPR_P (decl))
31201 decl = TREE_OPERAND (decl, 0);
31202 cp_lexer_consume_token (parser->lexer);
31204 else
31206 name = cp_parser_id_expression (parser, /*template_p=*/false,
31207 /*check_dependency_p=*/true,
31208 /*template_p=*/NULL,
31209 /*declarator_p=*/false,
31210 /*optional_p=*/false);
31211 if (name == error_mark_node)
31212 goto skip_comma;
31214 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31215 if (decl == error_mark_node)
31216 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31217 token->location);
31219 if (decl == error_mark_node)
31221 else if (kind != 0)
31223 switch (kind)
31225 case OMP_CLAUSE__CACHE_:
31226 /* The OpenACC cache directive explicitly only allows "array
31227 elements or subarrays". */
31228 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31230 error_at (token->location, "expected %<[%>");
31231 decl = error_mark_node;
31232 break;
31234 /* FALLTHROUGH. */
31235 case OMP_CLAUSE_MAP:
31236 case OMP_CLAUSE_FROM:
31237 case OMP_CLAUSE_TO:
31238 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31240 location_t loc
31241 = cp_lexer_peek_token (parser->lexer)->location;
31242 cp_id_kind idk = CP_ID_KIND_NONE;
31243 cp_lexer_consume_token (parser->lexer);
31244 decl = convert_from_reference (decl);
31245 decl
31246 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31247 decl, false,
31248 &idk, loc);
31250 /* FALLTHROUGH. */
31251 case OMP_CLAUSE_DEPEND:
31252 case OMP_CLAUSE_REDUCTION:
31253 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31255 tree low_bound = NULL_TREE, length = NULL_TREE;
31257 parser->colon_corrects_to_scope_p = false;
31258 cp_lexer_consume_token (parser->lexer);
31259 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31260 low_bound = cp_parser_expression (parser);
31261 if (!colon)
31262 parser->colon_corrects_to_scope_p
31263 = saved_colon_corrects_to_scope_p;
31264 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31265 length = integer_one_node;
31266 else
31268 /* Look for `:'. */
31269 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31270 goto skip_comma;
31271 if (!cp_lexer_next_token_is (parser->lexer,
31272 CPP_CLOSE_SQUARE))
31273 length = cp_parser_expression (parser);
31275 /* Look for the closing `]'. */
31276 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31277 RT_CLOSE_SQUARE))
31278 goto skip_comma;
31280 decl = tree_cons (low_bound, length, decl);
31282 break;
31283 default:
31284 break;
31287 tree u = build_omp_clause (token->location, kind);
31288 OMP_CLAUSE_DECL (u) = decl;
31289 OMP_CLAUSE_CHAIN (u) = list;
31290 list = u;
31292 else
31293 list = tree_cons (decl, NULL_TREE, list);
31295 get_comma:
31296 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31297 break;
31298 cp_lexer_consume_token (parser->lexer);
31301 if (colon)
31302 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31304 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31306 *colon = true;
31307 cp_parser_require (parser, CPP_COLON, RT_COLON);
31308 return list;
31311 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31313 int ending;
31315 /* Try to resync to an unnested comma. Copied from
31316 cp_parser_parenthesized_expression_list. */
31317 skip_comma:
31318 if (colon)
31319 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31320 ending = cp_parser_skip_to_closing_parenthesis (parser,
31321 /*recovering=*/true,
31322 /*or_comma=*/true,
31323 /*consume_paren=*/true);
31324 if (ending < 0)
31325 goto get_comma;
31328 return list;
31331 /* Similarly, but expect leading and trailing parenthesis. This is a very
31332 common case for omp clauses. */
31334 static tree
31335 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31337 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31338 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31339 return list;
31342 /* OpenACC 2.0:
31343 copy ( variable-list )
31344 copyin ( variable-list )
31345 copyout ( variable-list )
31346 create ( variable-list )
31347 delete ( variable-list )
31348 present ( variable-list )
31349 present_or_copy ( variable-list )
31350 pcopy ( variable-list )
31351 present_or_copyin ( variable-list )
31352 pcopyin ( variable-list )
31353 present_or_copyout ( variable-list )
31354 pcopyout ( variable-list )
31355 present_or_create ( variable-list )
31356 pcreate ( variable-list ) */
31358 static tree
31359 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31360 tree list)
31362 enum gomp_map_kind kind;
31363 switch (c_kind)
31365 case PRAGMA_OACC_CLAUSE_COPY:
31366 kind = GOMP_MAP_FORCE_TOFROM;
31367 break;
31368 case PRAGMA_OACC_CLAUSE_COPYIN:
31369 kind = GOMP_MAP_FORCE_TO;
31370 break;
31371 case PRAGMA_OACC_CLAUSE_COPYOUT:
31372 kind = GOMP_MAP_FORCE_FROM;
31373 break;
31374 case PRAGMA_OACC_CLAUSE_CREATE:
31375 kind = GOMP_MAP_FORCE_ALLOC;
31376 break;
31377 case PRAGMA_OACC_CLAUSE_DELETE:
31378 kind = GOMP_MAP_DELETE;
31379 break;
31380 case PRAGMA_OACC_CLAUSE_DEVICE:
31381 kind = GOMP_MAP_FORCE_TO;
31382 break;
31383 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31384 kind = GOMP_MAP_DEVICE_RESIDENT;
31385 break;
31386 case PRAGMA_OACC_CLAUSE_HOST:
31387 case PRAGMA_OACC_CLAUSE_SELF:
31388 kind = GOMP_MAP_FORCE_FROM;
31389 break;
31390 case PRAGMA_OACC_CLAUSE_LINK:
31391 kind = GOMP_MAP_LINK;
31392 break;
31393 case PRAGMA_OACC_CLAUSE_PRESENT:
31394 kind = GOMP_MAP_FORCE_PRESENT;
31395 break;
31396 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31397 kind = GOMP_MAP_TOFROM;
31398 break;
31399 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31400 kind = GOMP_MAP_TO;
31401 break;
31402 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31403 kind = GOMP_MAP_FROM;
31404 break;
31405 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31406 kind = GOMP_MAP_ALLOC;
31407 break;
31408 default:
31409 gcc_unreachable ();
31411 tree nl, c;
31412 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31414 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31415 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31417 return nl;
31420 /* OpenACC 2.0:
31421 deviceptr ( variable-list ) */
31423 static tree
31424 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31426 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31427 tree vars, t;
31429 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31430 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31431 variable-list must only allow for pointer variables. */
31432 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31433 for (t = vars; t; t = TREE_CHAIN (t))
31435 tree v = TREE_PURPOSE (t);
31436 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31437 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31438 OMP_CLAUSE_DECL (u) = v;
31439 OMP_CLAUSE_CHAIN (u) = list;
31440 list = u;
31443 return list;
31446 /* OpenACC 2.0:
31447 auto
31448 independent
31449 nohost
31450 seq */
31452 static tree
31453 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31454 enum omp_clause_code code,
31455 tree list, location_t location)
31457 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31458 tree c = build_omp_clause (location, code);
31459 OMP_CLAUSE_CHAIN (c) = list;
31460 return c;
31463 /* OpenACC:
31464 num_gangs ( expression )
31465 num_workers ( expression )
31466 vector_length ( expression ) */
31468 static tree
31469 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31470 const char *str, tree list)
31472 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31474 matching_parens parens;
31475 if (!parens.require_open (parser))
31476 return list;
31478 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31480 if (t == error_mark_node
31481 || !parens.require_close (parser))
31483 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31484 /*or_comma=*/false,
31485 /*consume_paren=*/true);
31486 return list;
31489 check_no_duplicate_clause (list, code, str, loc);
31491 tree c = build_omp_clause (loc, code);
31492 OMP_CLAUSE_OPERAND (c, 0) = t;
31493 OMP_CLAUSE_CHAIN (c) = list;
31494 return c;
31497 /* OpenACC:
31499 gang [( gang-arg-list )]
31500 worker [( [num:] int-expr )]
31501 vector [( [length:] int-expr )]
31503 where gang-arg is one of:
31505 [num:] int-expr
31506 static: size-expr
31508 and size-expr may be:
31511 int-expr
31514 static tree
31515 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31516 const char *str, tree list)
31518 const char *id = "num";
31519 cp_lexer *lexer = parser->lexer;
31520 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31521 location_t loc = cp_lexer_peek_token (lexer)->location;
31523 if (kind == OMP_CLAUSE_VECTOR)
31524 id = "length";
31526 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31528 matching_parens parens;
31529 parens.consume_open (parser);
31533 cp_token *next = cp_lexer_peek_token (lexer);
31534 int idx = 0;
31536 /* Gang static argument. */
31537 if (kind == OMP_CLAUSE_GANG
31538 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31540 cp_lexer_consume_token (lexer);
31542 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31543 goto cleanup_error;
31545 idx = 1;
31546 if (ops[idx] != NULL)
31548 cp_parser_error (parser, "too many %<static%> arguments");
31549 goto cleanup_error;
31552 /* Check for the '*' argument. */
31553 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31554 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31555 || cp_lexer_nth_token_is (parser->lexer, 2,
31556 CPP_CLOSE_PAREN)))
31558 cp_lexer_consume_token (lexer);
31559 ops[idx] = integer_minus_one_node;
31561 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31563 cp_lexer_consume_token (lexer);
31564 continue;
31566 else break;
31569 /* Worker num: argument and vector length: arguments. */
31570 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31571 && id_equal (next->u.value, id)
31572 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31574 cp_lexer_consume_token (lexer); /* id */
31575 cp_lexer_consume_token (lexer); /* ':' */
31578 /* Now collect the actual argument. */
31579 if (ops[idx] != NULL_TREE)
31581 cp_parser_error (parser, "unexpected argument");
31582 goto cleanup_error;
31585 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31586 false);
31587 if (expr == error_mark_node)
31588 goto cleanup_error;
31590 mark_exp_read (expr);
31591 ops[idx] = expr;
31593 if (kind == OMP_CLAUSE_GANG
31594 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31596 cp_lexer_consume_token (lexer);
31597 continue;
31599 break;
31601 while (1);
31603 if (!parens.require_close (parser))
31604 goto cleanup_error;
31607 check_no_duplicate_clause (list, kind, str, loc);
31609 c = build_omp_clause (loc, kind);
31611 if (ops[1])
31612 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31614 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31615 OMP_CLAUSE_CHAIN (c) = list;
31617 return c;
31619 cleanup_error:
31620 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31621 return list;
31624 /* OpenACC 2.0:
31625 tile ( size-expr-list ) */
31627 static tree
31628 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31630 tree c, expr = error_mark_node;
31631 tree tile = NULL_TREE;
31633 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31634 so, but the spec authors never considered such a case and have
31635 differing opinions on what it might mean, including 'not
31636 allowed'.) */
31637 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
31638 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
31639 clause_loc);
31641 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31642 return list;
31646 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
31647 return list;
31649 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
31650 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31651 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
31653 cp_lexer_consume_token (parser->lexer);
31654 expr = integer_zero_node;
31656 else
31657 expr = cp_parser_constant_expression (parser);
31659 tile = tree_cons (NULL_TREE, expr, tile);
31661 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
31663 /* Consume the trailing ')'. */
31664 cp_lexer_consume_token (parser->lexer);
31666 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
31667 tile = nreverse (tile);
31668 OMP_CLAUSE_TILE_LIST (c) = tile;
31669 OMP_CLAUSE_CHAIN (c) = list;
31670 return c;
31673 /* OpenACC 2.0
31674 Parse wait clause or directive parameters. */
31676 static tree
31677 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
31679 vec<tree, va_gc> *args;
31680 tree t, args_tree;
31682 args = cp_parser_parenthesized_expression_list (parser, non_attr,
31683 /*cast_p=*/false,
31684 /*allow_expansion_p=*/true,
31685 /*non_constant_p=*/NULL);
31687 if (args == NULL || args->length () == 0)
31689 cp_parser_error (parser, "expected integer expression before ')'");
31690 if (args != NULL)
31691 release_tree_vector (args);
31692 return list;
31695 args_tree = build_tree_list_vec (args);
31697 release_tree_vector (args);
31699 for (t = args_tree; t; t = TREE_CHAIN (t))
31701 tree targ = TREE_VALUE (t);
31703 if (targ != error_mark_node)
31705 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
31706 error ("%<wait%> expression must be integral");
31707 else
31709 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
31711 targ = mark_rvalue_use (targ);
31712 OMP_CLAUSE_DECL (c) = targ;
31713 OMP_CLAUSE_CHAIN (c) = list;
31714 list = c;
31719 return list;
31722 /* OpenACC:
31723 wait ( int-expr-list ) */
31725 static tree
31726 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
31728 location_t location = cp_lexer_peek_token (parser->lexer)->location;
31730 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
31731 return list;
31733 list = cp_parser_oacc_wait_list (parser, location, list);
31735 return list;
31738 /* OpenMP 3.0:
31739 collapse ( constant-expression ) */
31741 static tree
31742 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
31744 tree c, num;
31745 location_t loc;
31746 HOST_WIDE_INT n;
31748 loc = cp_lexer_peek_token (parser->lexer)->location;
31749 matching_parens parens;
31750 if (!parens.require_open (parser))
31751 return list;
31753 num = cp_parser_constant_expression (parser);
31755 if (!parens.require_close (parser))
31756 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31757 /*or_comma=*/false,
31758 /*consume_paren=*/true);
31760 if (num == error_mark_node)
31761 return list;
31762 num = fold_non_dependent_expr (num);
31763 if (!tree_fits_shwi_p (num)
31764 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31765 || (n = tree_to_shwi (num)) <= 0
31766 || (int) n != n)
31768 error_at (loc, "collapse argument needs positive constant integer expression");
31769 return list;
31772 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
31773 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
31774 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
31775 OMP_CLAUSE_CHAIN (c) = list;
31776 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
31778 return c;
31781 /* OpenMP 2.5:
31782 default ( none | shared )
31784 OpenACC:
31785 default ( none | present ) */
31787 static tree
31788 cp_parser_omp_clause_default (cp_parser *parser, tree list,
31789 location_t location, bool is_oacc)
31791 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
31792 tree c;
31794 matching_parens parens;
31795 if (!parens.require_open (parser))
31796 return list;
31797 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31799 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31800 const char *p = IDENTIFIER_POINTER (id);
31802 switch (p[0])
31804 case 'n':
31805 if (strcmp ("none", p) != 0)
31806 goto invalid_kind;
31807 kind = OMP_CLAUSE_DEFAULT_NONE;
31808 break;
31810 case 'p':
31811 if (strcmp ("present", p) != 0 || !is_oacc)
31812 goto invalid_kind;
31813 kind = OMP_CLAUSE_DEFAULT_PRESENT;
31814 break;
31816 case 's':
31817 if (strcmp ("shared", p) != 0 || is_oacc)
31818 goto invalid_kind;
31819 kind = OMP_CLAUSE_DEFAULT_SHARED;
31820 break;
31822 default:
31823 goto invalid_kind;
31826 cp_lexer_consume_token (parser->lexer);
31828 else
31830 invalid_kind:
31831 if (is_oacc)
31832 cp_parser_error (parser, "expected %<none%> or %<present%>");
31833 else
31834 cp_parser_error (parser, "expected %<none%> or %<shared%>");
31837 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
31838 || !parens.require_close (parser))
31839 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31840 /*or_comma=*/false,
31841 /*consume_paren=*/true);
31843 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
31844 return list;
31846 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
31847 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
31848 OMP_CLAUSE_CHAIN (c) = list;
31849 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
31851 return c;
31854 /* OpenMP 3.1:
31855 final ( expression ) */
31857 static tree
31858 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
31860 tree t, c;
31862 matching_parens parens;
31863 if (!parens.require_open (parser))
31864 return list;
31866 t = cp_parser_condition (parser);
31868 if (t == error_mark_node
31869 || !parens.require_close (parser))
31870 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31871 /*or_comma=*/false,
31872 /*consume_paren=*/true);
31874 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
31876 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
31877 OMP_CLAUSE_FINAL_EXPR (c) = t;
31878 OMP_CLAUSE_CHAIN (c) = list;
31880 return c;
31883 /* OpenMP 2.5:
31884 if ( expression )
31886 OpenMP 4.5:
31887 if ( directive-name-modifier : expression )
31889 directive-name-modifier:
31890 parallel | task | taskloop | target data | target | target update
31891 | target enter data | target exit data */
31893 static tree
31894 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
31895 bool is_omp)
31897 tree t, c;
31898 enum tree_code if_modifier = ERROR_MARK;
31900 matching_parens parens;
31901 if (!parens.require_open (parser))
31902 return list;
31904 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31906 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31907 const char *p = IDENTIFIER_POINTER (id);
31908 int n = 2;
31910 if (strcmp ("parallel", p) == 0)
31911 if_modifier = OMP_PARALLEL;
31912 else if (strcmp ("task", p) == 0)
31913 if_modifier = OMP_TASK;
31914 else if (strcmp ("taskloop", p) == 0)
31915 if_modifier = OMP_TASKLOOP;
31916 else if (strcmp ("target", p) == 0)
31918 if_modifier = OMP_TARGET;
31919 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
31921 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
31922 p = IDENTIFIER_POINTER (id);
31923 if (strcmp ("data", p) == 0)
31924 if_modifier = OMP_TARGET_DATA;
31925 else if (strcmp ("update", p) == 0)
31926 if_modifier = OMP_TARGET_UPDATE;
31927 else if (strcmp ("enter", p) == 0)
31928 if_modifier = OMP_TARGET_ENTER_DATA;
31929 else if (strcmp ("exit", p) == 0)
31930 if_modifier = OMP_TARGET_EXIT_DATA;
31931 if (if_modifier != OMP_TARGET)
31932 n = 3;
31933 else
31935 location_t loc
31936 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
31937 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
31938 "or %<exit%>");
31939 if_modifier = ERROR_MARK;
31941 if (if_modifier == OMP_TARGET_ENTER_DATA
31942 || if_modifier == OMP_TARGET_EXIT_DATA)
31944 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
31946 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
31947 p = IDENTIFIER_POINTER (id);
31948 if (strcmp ("data", p) == 0)
31949 n = 4;
31951 if (n != 4)
31953 location_t loc
31954 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
31955 error_at (loc, "expected %<data%>");
31956 if_modifier = ERROR_MARK;
31961 if (if_modifier != ERROR_MARK)
31963 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
31965 while (n-- > 0)
31966 cp_lexer_consume_token (parser->lexer);
31968 else
31970 if (n > 2)
31972 location_t loc
31973 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
31974 error_at (loc, "expected %<:%>");
31976 if_modifier = ERROR_MARK;
31981 t = cp_parser_condition (parser);
31983 if (t == error_mark_node
31984 || !parens.require_close (parser))
31985 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31986 /*or_comma=*/false,
31987 /*consume_paren=*/true);
31989 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
31990 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
31992 if (if_modifier != ERROR_MARK
31993 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
31995 const char *p = NULL;
31996 switch (if_modifier)
31998 case OMP_PARALLEL: p = "parallel"; break;
31999 case OMP_TASK: p = "task"; break;
32000 case OMP_TASKLOOP: p = "taskloop"; break;
32001 case OMP_TARGET_DATA: p = "target data"; break;
32002 case OMP_TARGET: p = "target"; break;
32003 case OMP_TARGET_UPDATE: p = "target update"; break;
32004 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32005 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32006 default: gcc_unreachable ();
32008 error_at (location, "too many %<if%> clauses with %qs modifier",
32010 return list;
32012 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32014 if (!is_omp)
32015 error_at (location, "too many %<if%> clauses");
32016 else
32017 error_at (location, "too many %<if%> clauses without modifier");
32018 return list;
32020 else if (if_modifier == ERROR_MARK
32021 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32023 error_at (location, "if any %<if%> clause has modifier, then all "
32024 "%<if%> clauses have to use modifier");
32025 return list;
32029 c = build_omp_clause (location, OMP_CLAUSE_IF);
32030 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32031 OMP_CLAUSE_IF_EXPR (c) = t;
32032 OMP_CLAUSE_CHAIN (c) = list;
32034 return c;
32037 /* OpenMP 3.1:
32038 mergeable */
32040 static tree
32041 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32042 tree list, location_t location)
32044 tree c;
32046 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32047 location);
32049 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32050 OMP_CLAUSE_CHAIN (c) = list;
32051 return c;
32054 /* OpenMP 2.5:
32055 nowait */
32057 static tree
32058 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32059 tree list, location_t location)
32061 tree c;
32063 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32065 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32066 OMP_CLAUSE_CHAIN (c) = list;
32067 return c;
32070 /* OpenMP 2.5:
32071 num_threads ( expression ) */
32073 static tree
32074 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32075 location_t location)
32077 tree t, c;
32079 matching_parens parens;
32080 if (!parens.require_open (parser))
32081 return list;
32083 t = cp_parser_expression (parser);
32085 if (t == error_mark_node
32086 || !parens.require_close (parser))
32087 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32088 /*or_comma=*/false,
32089 /*consume_paren=*/true);
32091 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32092 "num_threads", location);
32094 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32095 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32096 OMP_CLAUSE_CHAIN (c) = list;
32098 return c;
32101 /* OpenMP 4.5:
32102 num_tasks ( expression ) */
32104 static tree
32105 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32106 location_t location)
32108 tree t, c;
32110 matching_parens parens;
32111 if (!parens.require_open (parser))
32112 return list;
32114 t = cp_parser_expression (parser);
32116 if (t == error_mark_node
32117 || !parens.require_close (parser))
32118 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32119 /*or_comma=*/false,
32120 /*consume_paren=*/true);
32122 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32123 "num_tasks", location);
32125 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32126 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32127 OMP_CLAUSE_CHAIN (c) = list;
32129 return c;
32132 /* OpenMP 4.5:
32133 grainsize ( expression ) */
32135 static tree
32136 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32137 location_t location)
32139 tree t, c;
32141 matching_parens parens;
32142 if (!parens.require_open (parser))
32143 return list;
32145 t = cp_parser_expression (parser);
32147 if (t == error_mark_node
32148 || !parens.require_close (parser))
32149 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32150 /*or_comma=*/false,
32151 /*consume_paren=*/true);
32153 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32154 "grainsize", location);
32156 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32157 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32158 OMP_CLAUSE_CHAIN (c) = list;
32160 return c;
32163 /* OpenMP 4.5:
32164 priority ( expression ) */
32166 static tree
32167 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32168 location_t location)
32170 tree t, c;
32172 matching_parens parens;
32173 if (!parens.require_open (parser))
32174 return list;
32176 t = cp_parser_expression (parser);
32178 if (t == error_mark_node
32179 || !parens.require_close (parser))
32180 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32181 /*or_comma=*/false,
32182 /*consume_paren=*/true);
32184 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32185 "priority", location);
32187 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32188 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32189 OMP_CLAUSE_CHAIN (c) = list;
32191 return c;
32194 /* OpenMP 4.5:
32195 hint ( expression ) */
32197 static tree
32198 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32199 location_t location)
32201 tree t, c;
32203 matching_parens parens;
32204 if (!parens.require_open (parser))
32205 return list;
32207 t = cp_parser_expression (parser);
32209 if (t == error_mark_node
32210 || !parens.require_close (parser))
32211 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32212 /*or_comma=*/false,
32213 /*consume_paren=*/true);
32215 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32217 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32218 OMP_CLAUSE_HINT_EXPR (c) = t;
32219 OMP_CLAUSE_CHAIN (c) = list;
32221 return c;
32224 /* OpenMP 4.5:
32225 defaultmap ( tofrom : scalar ) */
32227 static tree
32228 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32229 location_t location)
32231 tree c, id;
32232 const char *p;
32234 matching_parens parens;
32235 if (!parens.require_open (parser))
32236 return list;
32238 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32240 cp_parser_error (parser, "expected %<tofrom%>");
32241 goto out_err;
32243 id = cp_lexer_peek_token (parser->lexer)->u.value;
32244 p = IDENTIFIER_POINTER (id);
32245 if (strcmp (p, "tofrom") != 0)
32247 cp_parser_error (parser, "expected %<tofrom%>");
32248 goto out_err;
32250 cp_lexer_consume_token (parser->lexer);
32251 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32252 goto out_err;
32254 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32256 cp_parser_error (parser, "expected %<scalar%>");
32257 goto out_err;
32259 id = cp_lexer_peek_token (parser->lexer)->u.value;
32260 p = IDENTIFIER_POINTER (id);
32261 if (strcmp (p, "scalar") != 0)
32263 cp_parser_error (parser, "expected %<scalar%>");
32264 goto out_err;
32266 cp_lexer_consume_token (parser->lexer);
32267 if (!parens.require_close (parser))
32268 goto out_err;
32270 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32271 location);
32273 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32274 OMP_CLAUSE_CHAIN (c) = list;
32275 return c;
32277 out_err:
32278 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32279 /*or_comma=*/false,
32280 /*consume_paren=*/true);
32281 return list;
32284 /* OpenMP 2.5:
32285 ordered
32287 OpenMP 4.5:
32288 ordered ( constant-expression ) */
32290 static tree
32291 cp_parser_omp_clause_ordered (cp_parser *parser,
32292 tree list, location_t location)
32294 tree c, num = NULL_TREE;
32295 HOST_WIDE_INT n;
32297 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32298 "ordered", location);
32300 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32302 matching_parens parens;
32303 parens.consume_open (parser);
32305 num = cp_parser_constant_expression (parser);
32307 if (!parens.require_close (parser))
32308 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32309 /*or_comma=*/false,
32310 /*consume_paren=*/true);
32312 if (num == error_mark_node)
32313 return list;
32314 num = fold_non_dependent_expr (num);
32315 if (!tree_fits_shwi_p (num)
32316 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32317 || (n = tree_to_shwi (num)) <= 0
32318 || (int) n != n)
32320 error_at (location,
32321 "ordered argument needs positive constant integer "
32322 "expression");
32323 return list;
32327 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32328 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32329 OMP_CLAUSE_CHAIN (c) = list;
32330 return c;
32333 /* OpenMP 2.5:
32334 reduction ( reduction-operator : variable-list )
32336 reduction-operator:
32337 One of: + * - & ^ | && ||
32339 OpenMP 3.1:
32341 reduction-operator:
32342 One of: + * - & ^ | && || min max
32344 OpenMP 4.0:
32346 reduction-operator:
32347 One of: + * - & ^ | && ||
32348 id-expression */
32350 static tree
32351 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32353 enum tree_code code = ERROR_MARK;
32354 tree nlist, c, id = NULL_TREE;
32356 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32357 return list;
32359 switch (cp_lexer_peek_token (parser->lexer)->type)
32361 case CPP_PLUS: code = PLUS_EXPR; break;
32362 case CPP_MULT: code = MULT_EXPR; break;
32363 case CPP_MINUS: code = MINUS_EXPR; break;
32364 case CPP_AND: code = BIT_AND_EXPR; break;
32365 case CPP_XOR: code = BIT_XOR_EXPR; break;
32366 case CPP_OR: code = BIT_IOR_EXPR; break;
32367 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32368 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32369 default: break;
32372 if (code != ERROR_MARK)
32373 cp_lexer_consume_token (parser->lexer);
32374 else
32376 bool saved_colon_corrects_to_scope_p;
32377 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32378 parser->colon_corrects_to_scope_p = false;
32379 id = cp_parser_id_expression (parser, /*template_p=*/false,
32380 /*check_dependency_p=*/true,
32381 /*template_p=*/NULL,
32382 /*declarator_p=*/false,
32383 /*optional_p=*/false);
32384 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32385 if (identifier_p (id))
32387 const char *p = IDENTIFIER_POINTER (id);
32389 if (strcmp (p, "min") == 0)
32390 code = MIN_EXPR;
32391 else if (strcmp (p, "max") == 0)
32392 code = MAX_EXPR;
32393 else if (id == ovl_op_identifier (false, PLUS_EXPR))
32394 code = PLUS_EXPR;
32395 else if (id == ovl_op_identifier (false, MULT_EXPR))
32396 code = MULT_EXPR;
32397 else if (id == ovl_op_identifier (false, MINUS_EXPR))
32398 code = MINUS_EXPR;
32399 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
32400 code = BIT_AND_EXPR;
32401 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
32402 code = BIT_IOR_EXPR;
32403 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
32404 code = BIT_XOR_EXPR;
32405 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
32406 code = TRUTH_ANDIF_EXPR;
32407 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
32408 code = TRUTH_ORIF_EXPR;
32409 id = omp_reduction_id (code, id, NULL_TREE);
32410 tree scope = parser->scope;
32411 if (scope)
32412 id = build_qualified_name (NULL_TREE, scope, id, false);
32413 parser->scope = NULL_TREE;
32414 parser->qualifying_scope = NULL_TREE;
32415 parser->object_scope = NULL_TREE;
32417 else
32419 error ("invalid reduction-identifier");
32420 resync_fail:
32421 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32422 /*or_comma=*/false,
32423 /*consume_paren=*/true);
32424 return list;
32428 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32429 goto resync_fail;
32431 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32432 NULL);
32433 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32435 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32436 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32439 return nlist;
32442 /* OpenMP 2.5:
32443 schedule ( schedule-kind )
32444 schedule ( schedule-kind , expression )
32446 schedule-kind:
32447 static | dynamic | guided | runtime | auto
32449 OpenMP 4.5:
32450 schedule ( schedule-modifier : schedule-kind )
32451 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32453 schedule-modifier:
32454 simd
32455 monotonic
32456 nonmonotonic */
32458 static tree
32459 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32461 tree c, t;
32462 int modifiers = 0, nmodifiers = 0;
32464 matching_parens parens;
32465 if (!parens.require_open (parser))
32466 return list;
32468 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32470 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32472 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32473 const char *p = IDENTIFIER_POINTER (id);
32474 if (strcmp ("simd", p) == 0)
32475 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32476 else if (strcmp ("monotonic", p) == 0)
32477 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32478 else if (strcmp ("nonmonotonic", p) == 0)
32479 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32480 else
32481 break;
32482 cp_lexer_consume_token (parser->lexer);
32483 if (nmodifiers++ == 0
32484 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32485 cp_lexer_consume_token (parser->lexer);
32486 else
32488 cp_parser_require (parser, CPP_COLON, RT_COLON);
32489 break;
32493 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32495 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32496 const char *p = IDENTIFIER_POINTER (id);
32498 switch (p[0])
32500 case 'd':
32501 if (strcmp ("dynamic", p) != 0)
32502 goto invalid_kind;
32503 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32504 break;
32506 case 'g':
32507 if (strcmp ("guided", p) != 0)
32508 goto invalid_kind;
32509 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32510 break;
32512 case 'r':
32513 if (strcmp ("runtime", p) != 0)
32514 goto invalid_kind;
32515 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32516 break;
32518 default:
32519 goto invalid_kind;
32522 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32523 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32524 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32525 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32526 else
32527 goto invalid_kind;
32528 cp_lexer_consume_token (parser->lexer);
32530 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32531 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32532 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32533 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32535 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32536 "specified");
32537 modifiers = 0;
32540 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32542 cp_token *token;
32543 cp_lexer_consume_token (parser->lexer);
32545 token = cp_lexer_peek_token (parser->lexer);
32546 t = cp_parser_assignment_expression (parser);
32548 if (t == error_mark_node)
32549 goto resync_fail;
32550 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32551 error_at (token->location, "schedule %<runtime%> does not take "
32552 "a %<chunk_size%> parameter");
32553 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32554 error_at (token->location, "schedule %<auto%> does not take "
32555 "a %<chunk_size%> parameter");
32556 else
32557 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32559 if (!parens.require_close (parser))
32560 goto resync_fail;
32562 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32563 goto resync_fail;
32565 OMP_CLAUSE_SCHEDULE_KIND (c)
32566 = (enum omp_clause_schedule_kind)
32567 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32569 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32570 OMP_CLAUSE_CHAIN (c) = list;
32571 return c;
32573 invalid_kind:
32574 cp_parser_error (parser, "invalid schedule kind");
32575 resync_fail:
32576 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32577 /*or_comma=*/false,
32578 /*consume_paren=*/true);
32579 return list;
32582 /* OpenMP 3.0:
32583 untied */
32585 static tree
32586 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32587 tree list, location_t location)
32589 tree c;
32591 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32593 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32594 OMP_CLAUSE_CHAIN (c) = list;
32595 return c;
32598 /* OpenMP 4.0:
32599 inbranch
32600 notinbranch */
32602 static tree
32603 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32604 tree list, location_t location)
32606 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32607 tree c = build_omp_clause (location, code);
32608 OMP_CLAUSE_CHAIN (c) = list;
32609 return c;
32612 /* OpenMP 4.0:
32613 parallel
32615 sections
32616 taskgroup */
32618 static tree
32619 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32620 enum omp_clause_code code,
32621 tree list, location_t location)
32623 tree c = build_omp_clause (location, code);
32624 OMP_CLAUSE_CHAIN (c) = list;
32625 return c;
32628 /* OpenMP 4.5:
32629 nogroup */
32631 static tree
32632 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
32633 tree list, location_t location)
32635 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
32636 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
32637 OMP_CLAUSE_CHAIN (c) = list;
32638 return c;
32641 /* OpenMP 4.5:
32642 simd
32643 threads */
32645 static tree
32646 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
32647 enum omp_clause_code code,
32648 tree list, location_t location)
32650 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32651 tree c = build_omp_clause (location, code);
32652 OMP_CLAUSE_CHAIN (c) = list;
32653 return c;
32656 /* OpenMP 4.0:
32657 num_teams ( expression ) */
32659 static tree
32660 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
32661 location_t location)
32663 tree t, c;
32665 matching_parens parens;
32666 if (!parens.require_open (parser))
32667 return list;
32669 t = cp_parser_expression (parser);
32671 if (t == error_mark_node
32672 || !parens.require_close (parser))
32673 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32674 /*or_comma=*/false,
32675 /*consume_paren=*/true);
32677 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
32678 "num_teams", location);
32680 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
32681 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
32682 OMP_CLAUSE_CHAIN (c) = list;
32684 return c;
32687 /* OpenMP 4.0:
32688 thread_limit ( expression ) */
32690 static tree
32691 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
32692 location_t location)
32694 tree t, c;
32696 matching_parens parens;
32697 if (!parens.require_open (parser))
32698 return list;
32700 t = cp_parser_expression (parser);
32702 if (t == error_mark_node
32703 || !parens.require_close (parser))
32704 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32705 /*or_comma=*/false,
32706 /*consume_paren=*/true);
32708 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
32709 "thread_limit", location);
32711 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
32712 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
32713 OMP_CLAUSE_CHAIN (c) = list;
32715 return c;
32718 /* OpenMP 4.0:
32719 aligned ( variable-list )
32720 aligned ( variable-list : constant-expression ) */
32722 static tree
32723 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
32725 tree nlist, c, alignment = NULL_TREE;
32726 bool colon;
32728 matching_parens parens;
32729 if (!parens.require_open (parser))
32730 return list;
32732 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
32733 &colon);
32735 if (colon)
32737 alignment = cp_parser_constant_expression (parser);
32739 if (!parens.require_close (parser))
32740 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32741 /*or_comma=*/false,
32742 /*consume_paren=*/true);
32744 if (alignment == error_mark_node)
32745 alignment = NULL_TREE;
32748 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32749 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
32751 return nlist;
32754 /* OpenMP 4.0:
32755 linear ( variable-list )
32756 linear ( variable-list : expression )
32758 OpenMP 4.5:
32759 linear ( modifier ( variable-list ) )
32760 linear ( modifier ( variable-list ) : expression ) */
32762 static tree
32763 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
32764 bool declare_simd)
32766 tree nlist, c, step = integer_one_node;
32767 bool colon;
32768 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
32770 matching_parens parens;
32771 if (!parens.require_open (parser))
32772 return list;
32774 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32776 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32777 const char *p = IDENTIFIER_POINTER (id);
32779 if (strcmp ("ref", p) == 0)
32780 kind = OMP_CLAUSE_LINEAR_REF;
32781 else if (strcmp ("val", p) == 0)
32782 kind = OMP_CLAUSE_LINEAR_VAL;
32783 else if (strcmp ("uval", p) == 0)
32784 kind = OMP_CLAUSE_LINEAR_UVAL;
32785 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
32786 cp_lexer_consume_token (parser->lexer);
32787 else
32788 kind = OMP_CLAUSE_LINEAR_DEFAULT;
32791 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
32792 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
32793 &colon);
32794 else
32796 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
32797 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
32798 if (colon)
32799 cp_parser_require (parser, CPP_COLON, RT_COLON);
32800 else if (!parens.require_close (parser))
32801 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32802 /*or_comma=*/false,
32803 /*consume_paren=*/true);
32806 if (colon)
32808 step = NULL_TREE;
32809 if (declare_simd
32810 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32811 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
32813 cp_token *token = cp_lexer_peek_token (parser->lexer);
32814 cp_parser_parse_tentatively (parser);
32815 step = cp_parser_id_expression (parser, /*template_p=*/false,
32816 /*check_dependency_p=*/true,
32817 /*template_p=*/NULL,
32818 /*declarator_p=*/false,
32819 /*optional_p=*/false);
32820 if (step != error_mark_node)
32821 step = cp_parser_lookup_name_simple (parser, step, token->location);
32822 if (step == error_mark_node)
32824 step = NULL_TREE;
32825 cp_parser_abort_tentative_parse (parser);
32827 else if (!cp_parser_parse_definitely (parser))
32828 step = NULL_TREE;
32830 if (!step)
32831 step = cp_parser_expression (parser);
32833 if (!parens.require_close (parser))
32834 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32835 /*or_comma=*/false,
32836 /*consume_paren=*/true);
32838 if (step == error_mark_node)
32839 return list;
32842 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32844 OMP_CLAUSE_LINEAR_STEP (c) = step;
32845 OMP_CLAUSE_LINEAR_KIND (c) = kind;
32848 return nlist;
32851 /* OpenMP 4.0:
32852 safelen ( constant-expression ) */
32854 static tree
32855 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
32856 location_t location)
32858 tree t, c;
32860 matching_parens parens;
32861 if (!parens.require_open (parser))
32862 return list;
32864 t = cp_parser_constant_expression (parser);
32866 if (t == error_mark_node
32867 || !parens.require_close (parser))
32868 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32869 /*or_comma=*/false,
32870 /*consume_paren=*/true);
32872 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
32874 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
32875 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
32876 OMP_CLAUSE_CHAIN (c) = list;
32878 return c;
32881 /* OpenMP 4.0:
32882 simdlen ( constant-expression ) */
32884 static tree
32885 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
32886 location_t location)
32888 tree t, c;
32890 matching_parens parens;
32891 if (!parens.require_open (parser))
32892 return list;
32894 t = cp_parser_constant_expression (parser);
32896 if (t == error_mark_node
32897 || !parens.require_close (parser))
32898 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32899 /*or_comma=*/false,
32900 /*consume_paren=*/true);
32902 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
32904 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
32905 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
32906 OMP_CLAUSE_CHAIN (c) = list;
32908 return c;
32911 /* OpenMP 4.5:
32912 vec:
32913 identifier [+/- integer]
32914 vec , identifier [+/- integer]
32917 static tree
32918 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
32919 tree list)
32921 tree vec = NULL;
32923 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32925 cp_parser_error (parser, "expected identifier");
32926 return list;
32929 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32931 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
32932 tree t, identifier = cp_parser_identifier (parser);
32933 tree addend = NULL;
32935 if (identifier == error_mark_node)
32936 t = error_mark_node;
32937 else
32939 t = cp_parser_lookup_name_simple
32940 (parser, identifier,
32941 cp_lexer_peek_token (parser->lexer)->location);
32942 if (t == error_mark_node)
32943 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
32944 id_loc);
32947 bool neg = false;
32948 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
32949 neg = true;
32950 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
32952 addend = integer_zero_node;
32953 goto add_to_vector;
32955 cp_lexer_consume_token (parser->lexer);
32957 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
32959 cp_parser_error (parser, "expected integer");
32960 return list;
32963 addend = cp_lexer_peek_token (parser->lexer)->u.value;
32964 if (TREE_CODE (addend) != INTEGER_CST)
32966 cp_parser_error (parser, "expected integer");
32967 return list;
32969 cp_lexer_consume_token (parser->lexer);
32971 add_to_vector:
32972 if (t != error_mark_node)
32974 vec = tree_cons (addend, t, vec);
32975 if (neg)
32976 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
32979 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
32980 break;
32982 cp_lexer_consume_token (parser->lexer);
32985 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
32987 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
32988 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
32989 OMP_CLAUSE_DECL (u) = nreverse (vec);
32990 OMP_CLAUSE_CHAIN (u) = list;
32991 return u;
32993 return list;
32996 /* OpenMP 4.0:
32997 depend ( depend-kind : variable-list )
32999 depend-kind:
33000 in | out | inout
33002 OpenMP 4.5:
33003 depend ( source )
33005 depend ( sink : vec ) */
33007 static tree
33008 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33010 tree nlist, c;
33011 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
33013 matching_parens parens;
33014 if (!parens.require_open (parser))
33015 return list;
33017 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33019 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33020 const char *p = IDENTIFIER_POINTER (id);
33022 if (strcmp ("in", p) == 0)
33023 kind = OMP_CLAUSE_DEPEND_IN;
33024 else if (strcmp ("inout", p) == 0)
33025 kind = OMP_CLAUSE_DEPEND_INOUT;
33026 else if (strcmp ("out", p) == 0)
33027 kind = OMP_CLAUSE_DEPEND_OUT;
33028 else if (strcmp ("source", p) == 0)
33029 kind = OMP_CLAUSE_DEPEND_SOURCE;
33030 else if (strcmp ("sink", p) == 0)
33031 kind = OMP_CLAUSE_DEPEND_SINK;
33032 else
33033 goto invalid_kind;
33035 else
33036 goto invalid_kind;
33038 cp_lexer_consume_token (parser->lexer);
33040 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33042 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33043 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33044 OMP_CLAUSE_DECL (c) = NULL_TREE;
33045 OMP_CLAUSE_CHAIN (c) = list;
33046 if (!parens.require_close (parser))
33047 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33048 /*or_comma=*/false,
33049 /*consume_paren=*/true);
33050 return c;
33053 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33054 goto resync_fail;
33056 if (kind == OMP_CLAUSE_DEPEND_SINK)
33057 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
33058 else
33060 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
33061 list, NULL);
33063 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33064 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33066 return nlist;
33068 invalid_kind:
33069 cp_parser_error (parser, "invalid depend kind");
33070 resync_fail:
33071 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33072 /*or_comma=*/false,
33073 /*consume_paren=*/true);
33074 return list;
33077 /* OpenMP 4.0:
33078 map ( map-kind : variable-list )
33079 map ( variable-list )
33081 map-kind:
33082 alloc | to | from | tofrom
33084 OpenMP 4.5:
33085 map-kind:
33086 alloc | to | from | tofrom | release | delete
33088 map ( always [,] map-kind: variable-list ) */
33090 static tree
33091 cp_parser_omp_clause_map (cp_parser *parser, tree list)
33093 tree nlist, c;
33094 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
33095 bool always = false;
33097 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33098 return list;
33100 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33102 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33103 const char *p = IDENTIFIER_POINTER (id);
33105 if (strcmp ("always", p) == 0)
33107 int nth = 2;
33108 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
33109 nth++;
33110 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
33111 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
33112 == RID_DELETE))
33113 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
33114 == CPP_COLON))
33116 always = true;
33117 cp_lexer_consume_token (parser->lexer);
33118 if (nth == 3)
33119 cp_lexer_consume_token (parser->lexer);
33124 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33125 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33127 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33128 const char *p = IDENTIFIER_POINTER (id);
33130 if (strcmp ("alloc", p) == 0)
33131 kind = GOMP_MAP_ALLOC;
33132 else if (strcmp ("to", p) == 0)
33133 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
33134 else if (strcmp ("from", p) == 0)
33135 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
33136 else if (strcmp ("tofrom", p) == 0)
33137 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
33138 else if (strcmp ("release", p) == 0)
33139 kind = GOMP_MAP_RELEASE;
33140 else
33142 cp_parser_error (parser, "invalid map kind");
33143 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33144 /*or_comma=*/false,
33145 /*consume_paren=*/true);
33146 return list;
33148 cp_lexer_consume_token (parser->lexer);
33149 cp_lexer_consume_token (parser->lexer);
33151 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33152 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33154 kind = GOMP_MAP_DELETE;
33155 cp_lexer_consume_token (parser->lexer);
33156 cp_lexer_consume_token (parser->lexer);
33159 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33160 NULL);
33162 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33163 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33165 return nlist;
33168 /* OpenMP 4.0:
33169 device ( expression ) */
33171 static tree
33172 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33173 location_t location)
33175 tree t, c;
33177 matching_parens parens;
33178 if (!parens.require_open (parser))
33179 return list;
33181 t = cp_parser_expression (parser);
33183 if (t == error_mark_node
33184 || !parens.require_close (parser))
33185 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33186 /*or_comma=*/false,
33187 /*consume_paren=*/true);
33189 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33190 "device", location);
33192 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33193 OMP_CLAUSE_DEVICE_ID (c) = t;
33194 OMP_CLAUSE_CHAIN (c) = list;
33196 return c;
33199 /* OpenMP 4.0:
33200 dist_schedule ( static )
33201 dist_schedule ( static , expression ) */
33203 static tree
33204 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33205 location_t location)
33207 tree c, t;
33209 matching_parens parens;
33210 if (!parens.require_open (parser))
33211 return list;
33213 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33215 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33216 goto invalid_kind;
33217 cp_lexer_consume_token (parser->lexer);
33219 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33221 cp_lexer_consume_token (parser->lexer);
33223 t = cp_parser_assignment_expression (parser);
33225 if (t == error_mark_node)
33226 goto resync_fail;
33227 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33229 if (!parens.require_close (parser))
33230 goto resync_fail;
33232 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33233 goto resync_fail;
33235 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33236 location);
33237 OMP_CLAUSE_CHAIN (c) = list;
33238 return c;
33240 invalid_kind:
33241 cp_parser_error (parser, "invalid dist_schedule kind");
33242 resync_fail:
33243 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33244 /*or_comma=*/false,
33245 /*consume_paren=*/true);
33246 return list;
33249 /* OpenMP 4.0:
33250 proc_bind ( proc-bind-kind )
33252 proc-bind-kind:
33253 master | close | spread */
33255 static tree
33256 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33257 location_t location)
33259 tree c;
33260 enum omp_clause_proc_bind_kind kind;
33262 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33263 return list;
33265 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33267 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33268 const char *p = IDENTIFIER_POINTER (id);
33270 if (strcmp ("master", p) == 0)
33271 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33272 else if (strcmp ("close", p) == 0)
33273 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33274 else if (strcmp ("spread", p) == 0)
33275 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33276 else
33277 goto invalid_kind;
33279 else
33280 goto invalid_kind;
33282 cp_lexer_consume_token (parser->lexer);
33283 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33284 goto resync_fail;
33286 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33287 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33288 location);
33289 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33290 OMP_CLAUSE_CHAIN (c) = list;
33291 return c;
33293 invalid_kind:
33294 cp_parser_error (parser, "invalid depend kind");
33295 resync_fail:
33296 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33297 /*or_comma=*/false,
33298 /*consume_paren=*/true);
33299 return list;
33302 /* OpenACC:
33303 async [( int-expr )] */
33305 static tree
33306 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33308 tree c, t;
33309 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33311 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33313 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33315 matching_parens parens;
33316 parens.consume_open (parser);
33318 t = cp_parser_expression (parser);
33319 if (t == error_mark_node
33320 || !parens.require_close (parser))
33321 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33322 /*or_comma=*/false,
33323 /*consume_paren=*/true);
33326 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33328 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33329 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33330 OMP_CLAUSE_CHAIN (c) = list;
33331 list = c;
33333 return list;
33336 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33337 is a bitmask in MASK. Return the list of clauses found. */
33339 static tree
33340 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33341 const char *where, cp_token *pragma_tok,
33342 bool finish_p = true)
33344 tree clauses = NULL;
33345 bool first = true;
33347 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33349 location_t here;
33350 pragma_omp_clause c_kind;
33351 omp_clause_code code;
33352 const char *c_name;
33353 tree prev = clauses;
33355 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33356 cp_lexer_consume_token (parser->lexer);
33358 here = cp_lexer_peek_token (parser->lexer)->location;
33359 c_kind = cp_parser_omp_clause_name (parser);
33361 switch (c_kind)
33363 case PRAGMA_OACC_CLAUSE_ASYNC:
33364 clauses = cp_parser_oacc_clause_async (parser, clauses);
33365 c_name = "async";
33366 break;
33367 case PRAGMA_OACC_CLAUSE_AUTO:
33368 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33369 clauses, here);
33370 c_name = "auto";
33371 break;
33372 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33373 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33374 c_name = "collapse";
33375 break;
33376 case PRAGMA_OACC_CLAUSE_COPY:
33377 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33378 c_name = "copy";
33379 break;
33380 case PRAGMA_OACC_CLAUSE_COPYIN:
33381 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33382 c_name = "copyin";
33383 break;
33384 case PRAGMA_OACC_CLAUSE_COPYOUT:
33385 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33386 c_name = "copyout";
33387 break;
33388 case PRAGMA_OACC_CLAUSE_CREATE:
33389 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33390 c_name = "create";
33391 break;
33392 case PRAGMA_OACC_CLAUSE_DELETE:
33393 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33394 c_name = "delete";
33395 break;
33396 case PRAGMA_OMP_CLAUSE_DEFAULT:
33397 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33398 c_name = "default";
33399 break;
33400 case PRAGMA_OACC_CLAUSE_DEVICE:
33401 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33402 c_name = "device";
33403 break;
33404 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33405 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33406 c_name = "deviceptr";
33407 break;
33408 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33409 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33410 c_name = "device_resident";
33411 break;
33412 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33413 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33414 clauses);
33415 c_name = "firstprivate";
33416 break;
33417 case PRAGMA_OACC_CLAUSE_GANG:
33418 c_name = "gang";
33419 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33420 c_name, clauses);
33421 break;
33422 case PRAGMA_OACC_CLAUSE_HOST:
33423 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33424 c_name = "host";
33425 break;
33426 case PRAGMA_OACC_CLAUSE_IF:
33427 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33428 c_name = "if";
33429 break;
33430 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33431 clauses = cp_parser_oacc_simple_clause (parser,
33432 OMP_CLAUSE_INDEPENDENT,
33433 clauses, here);
33434 c_name = "independent";
33435 break;
33436 case PRAGMA_OACC_CLAUSE_LINK:
33437 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33438 c_name = "link";
33439 break;
33440 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33441 code = OMP_CLAUSE_NUM_GANGS;
33442 c_name = "num_gangs";
33443 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33444 clauses);
33445 break;
33446 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33447 c_name = "num_workers";
33448 code = OMP_CLAUSE_NUM_WORKERS;
33449 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33450 clauses);
33451 break;
33452 case PRAGMA_OACC_CLAUSE_PRESENT:
33453 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33454 c_name = "present";
33455 break;
33456 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33457 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33458 c_name = "present_or_copy";
33459 break;
33460 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33461 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33462 c_name = "present_or_copyin";
33463 break;
33464 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33465 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33466 c_name = "present_or_copyout";
33467 break;
33468 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33469 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33470 c_name = "present_or_create";
33471 break;
33472 case PRAGMA_OACC_CLAUSE_PRIVATE:
33473 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33474 clauses);
33475 c_name = "private";
33476 break;
33477 case PRAGMA_OACC_CLAUSE_REDUCTION:
33478 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33479 c_name = "reduction";
33480 break;
33481 case PRAGMA_OACC_CLAUSE_SELF:
33482 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33483 c_name = "self";
33484 break;
33485 case PRAGMA_OACC_CLAUSE_SEQ:
33486 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33487 clauses, here);
33488 c_name = "seq";
33489 break;
33490 case PRAGMA_OACC_CLAUSE_TILE:
33491 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33492 c_name = "tile";
33493 break;
33494 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33495 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33496 clauses);
33497 c_name = "use_device";
33498 break;
33499 case PRAGMA_OACC_CLAUSE_VECTOR:
33500 c_name = "vector";
33501 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33502 c_name, clauses);
33503 break;
33504 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33505 c_name = "vector_length";
33506 code = OMP_CLAUSE_VECTOR_LENGTH;
33507 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33508 clauses);
33509 break;
33510 case PRAGMA_OACC_CLAUSE_WAIT:
33511 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33512 c_name = "wait";
33513 break;
33514 case PRAGMA_OACC_CLAUSE_WORKER:
33515 c_name = "worker";
33516 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33517 c_name, clauses);
33518 break;
33519 default:
33520 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33521 goto saw_error;
33524 first = false;
33526 if (((mask >> c_kind) & 1) == 0)
33528 /* Remove the invalid clause(s) from the list to avoid
33529 confusing the rest of the compiler. */
33530 clauses = prev;
33531 error_at (here, "%qs is not valid for %qs", c_name, where);
33535 saw_error:
33536 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33538 if (finish_p)
33539 return finish_omp_clauses (clauses, C_ORT_ACC);
33541 return clauses;
33544 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33545 is a bitmask in MASK. Return the list of clauses found; the result
33546 of clause default goes in *pdefault. */
33548 static tree
33549 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33550 const char *where, cp_token *pragma_tok,
33551 bool finish_p = true)
33553 tree clauses = NULL;
33554 bool first = true;
33555 cp_token *token = NULL;
33557 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33559 pragma_omp_clause c_kind;
33560 const char *c_name;
33561 tree prev = clauses;
33563 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33564 cp_lexer_consume_token (parser->lexer);
33566 token = cp_lexer_peek_token (parser->lexer);
33567 c_kind = cp_parser_omp_clause_name (parser);
33569 switch (c_kind)
33571 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33572 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33573 token->location);
33574 c_name = "collapse";
33575 break;
33576 case PRAGMA_OMP_CLAUSE_COPYIN:
33577 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33578 c_name = "copyin";
33579 break;
33580 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33581 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33582 clauses);
33583 c_name = "copyprivate";
33584 break;
33585 case PRAGMA_OMP_CLAUSE_DEFAULT:
33586 clauses = cp_parser_omp_clause_default (parser, clauses,
33587 token->location, false);
33588 c_name = "default";
33589 break;
33590 case PRAGMA_OMP_CLAUSE_FINAL:
33591 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33592 c_name = "final";
33593 break;
33594 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33595 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33596 clauses);
33597 c_name = "firstprivate";
33598 break;
33599 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33600 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33601 token->location);
33602 c_name = "grainsize";
33603 break;
33604 case PRAGMA_OMP_CLAUSE_HINT:
33605 clauses = cp_parser_omp_clause_hint (parser, clauses,
33606 token->location);
33607 c_name = "hint";
33608 break;
33609 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33610 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33611 token->location);
33612 c_name = "defaultmap";
33613 break;
33614 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33615 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33616 clauses);
33617 c_name = "use_device_ptr";
33618 break;
33619 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33620 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33621 clauses);
33622 c_name = "is_device_ptr";
33623 break;
33624 case PRAGMA_OMP_CLAUSE_IF:
33625 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33626 true);
33627 c_name = "if";
33628 break;
33629 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33630 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33631 clauses);
33632 c_name = "lastprivate";
33633 break;
33634 case PRAGMA_OMP_CLAUSE_MERGEABLE:
33635 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
33636 token->location);
33637 c_name = "mergeable";
33638 break;
33639 case PRAGMA_OMP_CLAUSE_NOWAIT:
33640 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
33641 c_name = "nowait";
33642 break;
33643 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
33644 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
33645 token->location);
33646 c_name = "num_tasks";
33647 break;
33648 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
33649 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
33650 token->location);
33651 c_name = "num_threads";
33652 break;
33653 case PRAGMA_OMP_CLAUSE_ORDERED:
33654 clauses = cp_parser_omp_clause_ordered (parser, clauses,
33655 token->location);
33656 c_name = "ordered";
33657 break;
33658 case PRAGMA_OMP_CLAUSE_PRIORITY:
33659 clauses = cp_parser_omp_clause_priority (parser, clauses,
33660 token->location);
33661 c_name = "priority";
33662 break;
33663 case PRAGMA_OMP_CLAUSE_PRIVATE:
33664 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33665 clauses);
33666 c_name = "private";
33667 break;
33668 case PRAGMA_OMP_CLAUSE_REDUCTION:
33669 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33670 c_name = "reduction";
33671 break;
33672 case PRAGMA_OMP_CLAUSE_SCHEDULE:
33673 clauses = cp_parser_omp_clause_schedule (parser, clauses,
33674 token->location);
33675 c_name = "schedule";
33676 break;
33677 case PRAGMA_OMP_CLAUSE_SHARED:
33678 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
33679 clauses);
33680 c_name = "shared";
33681 break;
33682 case PRAGMA_OMP_CLAUSE_UNTIED:
33683 clauses = cp_parser_omp_clause_untied (parser, clauses,
33684 token->location);
33685 c_name = "untied";
33686 break;
33687 case PRAGMA_OMP_CLAUSE_INBRANCH:
33688 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
33689 clauses, token->location);
33690 c_name = "inbranch";
33691 break;
33692 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
33693 clauses = cp_parser_omp_clause_branch (parser,
33694 OMP_CLAUSE_NOTINBRANCH,
33695 clauses, token->location);
33696 c_name = "notinbranch";
33697 break;
33698 case PRAGMA_OMP_CLAUSE_PARALLEL:
33699 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
33700 clauses, token->location);
33701 c_name = "parallel";
33702 if (!first)
33704 clause_not_first:
33705 error_at (token->location, "%qs must be the first clause of %qs",
33706 c_name, where);
33707 clauses = prev;
33709 break;
33710 case PRAGMA_OMP_CLAUSE_FOR:
33711 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
33712 clauses, token->location);
33713 c_name = "for";
33714 if (!first)
33715 goto clause_not_first;
33716 break;
33717 case PRAGMA_OMP_CLAUSE_SECTIONS:
33718 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
33719 clauses, token->location);
33720 c_name = "sections";
33721 if (!first)
33722 goto clause_not_first;
33723 break;
33724 case PRAGMA_OMP_CLAUSE_TASKGROUP:
33725 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
33726 clauses, token->location);
33727 c_name = "taskgroup";
33728 if (!first)
33729 goto clause_not_first;
33730 break;
33731 case PRAGMA_OMP_CLAUSE_LINK:
33732 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
33733 c_name = "to";
33734 break;
33735 case PRAGMA_OMP_CLAUSE_TO:
33736 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
33737 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
33738 clauses);
33739 else
33740 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
33741 c_name = "to";
33742 break;
33743 case PRAGMA_OMP_CLAUSE_FROM:
33744 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
33745 c_name = "from";
33746 break;
33747 case PRAGMA_OMP_CLAUSE_UNIFORM:
33748 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
33749 clauses);
33750 c_name = "uniform";
33751 break;
33752 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
33753 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
33754 token->location);
33755 c_name = "num_teams";
33756 break;
33757 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
33758 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
33759 token->location);
33760 c_name = "thread_limit";
33761 break;
33762 case PRAGMA_OMP_CLAUSE_ALIGNED:
33763 clauses = cp_parser_omp_clause_aligned (parser, clauses);
33764 c_name = "aligned";
33765 break;
33766 case PRAGMA_OMP_CLAUSE_LINEAR:
33768 bool declare_simd = false;
33769 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
33770 declare_simd = true;
33771 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
33773 c_name = "linear";
33774 break;
33775 case PRAGMA_OMP_CLAUSE_DEPEND:
33776 clauses = cp_parser_omp_clause_depend (parser, clauses,
33777 token->location);
33778 c_name = "depend";
33779 break;
33780 case PRAGMA_OMP_CLAUSE_MAP:
33781 clauses = cp_parser_omp_clause_map (parser, clauses);
33782 c_name = "map";
33783 break;
33784 case PRAGMA_OMP_CLAUSE_DEVICE:
33785 clauses = cp_parser_omp_clause_device (parser, clauses,
33786 token->location);
33787 c_name = "device";
33788 break;
33789 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
33790 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
33791 token->location);
33792 c_name = "dist_schedule";
33793 break;
33794 case PRAGMA_OMP_CLAUSE_PROC_BIND:
33795 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
33796 token->location);
33797 c_name = "proc_bind";
33798 break;
33799 case PRAGMA_OMP_CLAUSE_SAFELEN:
33800 clauses = cp_parser_omp_clause_safelen (parser, clauses,
33801 token->location);
33802 c_name = "safelen";
33803 break;
33804 case PRAGMA_OMP_CLAUSE_SIMDLEN:
33805 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
33806 token->location);
33807 c_name = "simdlen";
33808 break;
33809 case PRAGMA_OMP_CLAUSE_NOGROUP:
33810 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
33811 token->location);
33812 c_name = "nogroup";
33813 break;
33814 case PRAGMA_OMP_CLAUSE_THREADS:
33815 clauses
33816 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
33817 clauses, token->location);
33818 c_name = "threads";
33819 break;
33820 case PRAGMA_OMP_CLAUSE_SIMD:
33821 clauses
33822 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
33823 clauses, token->location);
33824 c_name = "simd";
33825 break;
33826 default:
33827 cp_parser_error (parser, "expected %<#pragma omp%> clause");
33828 goto saw_error;
33831 first = false;
33833 if (((mask >> c_kind) & 1) == 0)
33835 /* Remove the invalid clause(s) from the list to avoid
33836 confusing the rest of the compiler. */
33837 clauses = prev;
33838 error_at (token->location, "%qs is not valid for %qs", c_name, where);
33841 saw_error:
33842 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33843 if (finish_p)
33845 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
33846 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
33847 else
33848 return finish_omp_clauses (clauses, C_ORT_OMP);
33850 return clauses;
33853 /* OpenMP 2.5:
33854 structured-block:
33855 statement
33857 In practice, we're also interested in adding the statement to an
33858 outer node. So it is convenient if we work around the fact that
33859 cp_parser_statement calls add_stmt. */
33861 static unsigned
33862 cp_parser_begin_omp_structured_block (cp_parser *parser)
33864 unsigned save = parser->in_statement;
33866 /* Only move the values to IN_OMP_BLOCK if they weren't false.
33867 This preserves the "not within loop or switch" style error messages
33868 for nonsense cases like
33869 void foo() {
33870 #pragma omp single
33871 break;
33874 if (parser->in_statement)
33875 parser->in_statement = IN_OMP_BLOCK;
33877 return save;
33880 static void
33881 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
33883 parser->in_statement = save;
33886 static tree
33887 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
33889 tree stmt = begin_omp_structured_block ();
33890 unsigned int save = cp_parser_begin_omp_structured_block (parser);
33892 cp_parser_statement (parser, NULL_TREE, false, if_p);
33894 cp_parser_end_omp_structured_block (parser, save);
33895 return finish_omp_structured_block (stmt);
33898 /* OpenMP 2.5:
33899 # pragma omp atomic new-line
33900 expression-stmt
33902 expression-stmt:
33903 x binop= expr | x++ | ++x | x-- | --x
33904 binop:
33905 +, *, -, /, &, ^, |, <<, >>
33907 where x is an lvalue expression with scalar type.
33909 OpenMP 3.1:
33910 # pragma omp atomic new-line
33911 update-stmt
33913 # pragma omp atomic read new-line
33914 read-stmt
33916 # pragma omp atomic write new-line
33917 write-stmt
33919 # pragma omp atomic update new-line
33920 update-stmt
33922 # pragma omp atomic capture new-line
33923 capture-stmt
33925 # pragma omp atomic capture new-line
33926 capture-block
33928 read-stmt:
33929 v = x
33930 write-stmt:
33931 x = expr
33932 update-stmt:
33933 expression-stmt | x = x binop expr
33934 capture-stmt:
33935 v = expression-stmt
33936 capture-block:
33937 { v = x; update-stmt; } | { update-stmt; v = x; }
33939 OpenMP 4.0:
33940 update-stmt:
33941 expression-stmt | x = x binop expr | x = expr binop x
33942 capture-stmt:
33943 v = update-stmt
33944 capture-block:
33945 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
33947 where x and v are lvalue expressions with scalar type. */
33949 static void
33950 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
33952 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
33953 tree rhs1 = NULL_TREE, orig_lhs;
33954 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
33955 bool structured_block = false;
33956 bool seq_cst = false;
33958 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33960 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33961 const char *p = IDENTIFIER_POINTER (id);
33963 if (!strcmp (p, "seq_cst"))
33965 seq_cst = true;
33966 cp_lexer_consume_token (parser->lexer);
33967 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33968 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33969 cp_lexer_consume_token (parser->lexer);
33972 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33974 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33975 const char *p = IDENTIFIER_POINTER (id);
33977 if (!strcmp (p, "read"))
33978 code = OMP_ATOMIC_READ;
33979 else if (!strcmp (p, "write"))
33980 code = NOP_EXPR;
33981 else if (!strcmp (p, "update"))
33982 code = OMP_ATOMIC;
33983 else if (!strcmp (p, "capture"))
33984 code = OMP_ATOMIC_CAPTURE_NEW;
33985 else
33986 p = NULL;
33987 if (p)
33988 cp_lexer_consume_token (parser->lexer);
33990 if (!seq_cst)
33992 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33993 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33994 cp_lexer_consume_token (parser->lexer);
33996 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33998 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33999 const char *p = IDENTIFIER_POINTER (id);
34001 if (!strcmp (p, "seq_cst"))
34003 seq_cst = true;
34004 cp_lexer_consume_token (parser->lexer);
34008 cp_parser_require_pragma_eol (parser, pragma_tok);
34010 switch (code)
34012 case OMP_ATOMIC_READ:
34013 case NOP_EXPR: /* atomic write */
34014 v = cp_parser_unary_expression (parser);
34015 if (v == error_mark_node)
34016 goto saw_error;
34017 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34018 goto saw_error;
34019 if (code == NOP_EXPR)
34020 lhs = cp_parser_expression (parser);
34021 else
34022 lhs = cp_parser_unary_expression (parser);
34023 if (lhs == error_mark_node)
34024 goto saw_error;
34025 if (code == NOP_EXPR)
34027 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34028 opcode. */
34029 code = OMP_ATOMIC;
34030 rhs = lhs;
34031 lhs = v;
34032 v = NULL_TREE;
34034 goto done;
34035 case OMP_ATOMIC_CAPTURE_NEW:
34036 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34038 cp_lexer_consume_token (parser->lexer);
34039 structured_block = true;
34041 else
34043 v = cp_parser_unary_expression (parser);
34044 if (v == error_mark_node)
34045 goto saw_error;
34046 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34047 goto saw_error;
34049 default:
34050 break;
34053 restart:
34054 lhs = cp_parser_unary_expression (parser);
34055 orig_lhs = lhs;
34056 switch (TREE_CODE (lhs))
34058 case ERROR_MARK:
34059 goto saw_error;
34061 case POSTINCREMENT_EXPR:
34062 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34063 code = OMP_ATOMIC_CAPTURE_OLD;
34064 /* FALLTHROUGH */
34065 case PREINCREMENT_EXPR:
34066 lhs = TREE_OPERAND (lhs, 0);
34067 opcode = PLUS_EXPR;
34068 rhs = integer_one_node;
34069 break;
34071 case POSTDECREMENT_EXPR:
34072 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34073 code = OMP_ATOMIC_CAPTURE_OLD;
34074 /* FALLTHROUGH */
34075 case PREDECREMENT_EXPR:
34076 lhs = TREE_OPERAND (lhs, 0);
34077 opcode = MINUS_EXPR;
34078 rhs = integer_one_node;
34079 break;
34081 case COMPOUND_EXPR:
34082 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
34083 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
34084 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
34085 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
34086 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34087 (TREE_OPERAND (lhs, 1), 0), 0)))
34088 == BOOLEAN_TYPE)
34089 /* Undo effects of boolean_increment for post {in,de}crement. */
34090 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
34091 /* FALLTHRU */
34092 case MODIFY_EXPR:
34093 if (TREE_CODE (lhs) == MODIFY_EXPR
34094 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
34096 /* Undo effects of boolean_increment. */
34097 if (integer_onep (TREE_OPERAND (lhs, 1)))
34099 /* This is pre or post increment. */
34100 rhs = TREE_OPERAND (lhs, 1);
34101 lhs = TREE_OPERAND (lhs, 0);
34102 opcode = NOP_EXPR;
34103 if (code == OMP_ATOMIC_CAPTURE_NEW
34104 && !structured_block
34105 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
34106 code = OMP_ATOMIC_CAPTURE_OLD;
34107 break;
34110 /* FALLTHRU */
34111 default:
34112 switch (cp_lexer_peek_token (parser->lexer)->type)
34114 case CPP_MULT_EQ:
34115 opcode = MULT_EXPR;
34116 break;
34117 case CPP_DIV_EQ:
34118 opcode = TRUNC_DIV_EXPR;
34119 break;
34120 case CPP_PLUS_EQ:
34121 opcode = PLUS_EXPR;
34122 break;
34123 case CPP_MINUS_EQ:
34124 opcode = MINUS_EXPR;
34125 break;
34126 case CPP_LSHIFT_EQ:
34127 opcode = LSHIFT_EXPR;
34128 break;
34129 case CPP_RSHIFT_EQ:
34130 opcode = RSHIFT_EXPR;
34131 break;
34132 case CPP_AND_EQ:
34133 opcode = BIT_AND_EXPR;
34134 break;
34135 case CPP_OR_EQ:
34136 opcode = BIT_IOR_EXPR;
34137 break;
34138 case CPP_XOR_EQ:
34139 opcode = BIT_XOR_EXPR;
34140 break;
34141 case CPP_EQ:
34142 enum cp_parser_prec oprec;
34143 cp_token *token;
34144 cp_lexer_consume_token (parser->lexer);
34145 cp_parser_parse_tentatively (parser);
34146 rhs1 = cp_parser_simple_cast_expression (parser);
34147 if (rhs1 == error_mark_node)
34149 cp_parser_abort_tentative_parse (parser);
34150 cp_parser_simple_cast_expression (parser);
34151 goto saw_error;
34153 token = cp_lexer_peek_token (parser->lexer);
34154 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34156 cp_parser_abort_tentative_parse (parser);
34157 cp_parser_parse_tentatively (parser);
34158 rhs = cp_parser_binary_expression (parser, false, true,
34159 PREC_NOT_OPERATOR, NULL);
34160 if (rhs == error_mark_node)
34162 cp_parser_abort_tentative_parse (parser);
34163 cp_parser_binary_expression (parser, false, true,
34164 PREC_NOT_OPERATOR, NULL);
34165 goto saw_error;
34167 switch (TREE_CODE (rhs))
34169 case MULT_EXPR:
34170 case TRUNC_DIV_EXPR:
34171 case RDIV_EXPR:
34172 case PLUS_EXPR:
34173 case MINUS_EXPR:
34174 case LSHIFT_EXPR:
34175 case RSHIFT_EXPR:
34176 case BIT_AND_EXPR:
34177 case BIT_IOR_EXPR:
34178 case BIT_XOR_EXPR:
34179 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34181 if (cp_parser_parse_definitely (parser))
34183 opcode = TREE_CODE (rhs);
34184 rhs1 = TREE_OPERAND (rhs, 0);
34185 rhs = TREE_OPERAND (rhs, 1);
34186 goto stmt_done;
34188 else
34189 goto saw_error;
34191 break;
34192 default:
34193 break;
34195 cp_parser_abort_tentative_parse (parser);
34196 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34198 rhs = cp_parser_expression (parser);
34199 if (rhs == error_mark_node)
34200 goto saw_error;
34201 opcode = NOP_EXPR;
34202 rhs1 = NULL_TREE;
34203 goto stmt_done;
34205 cp_parser_error (parser,
34206 "invalid form of %<#pragma omp atomic%>");
34207 goto saw_error;
34209 if (!cp_parser_parse_definitely (parser))
34210 goto saw_error;
34211 switch (token->type)
34213 case CPP_SEMICOLON:
34214 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34216 code = OMP_ATOMIC_CAPTURE_OLD;
34217 v = lhs;
34218 lhs = NULL_TREE;
34219 lhs1 = rhs1;
34220 rhs1 = NULL_TREE;
34221 cp_lexer_consume_token (parser->lexer);
34222 goto restart;
34224 else if (structured_block)
34226 opcode = NOP_EXPR;
34227 rhs = rhs1;
34228 rhs1 = NULL_TREE;
34229 goto stmt_done;
34231 cp_parser_error (parser,
34232 "invalid form of %<#pragma omp atomic%>");
34233 goto saw_error;
34234 case CPP_MULT:
34235 opcode = MULT_EXPR;
34236 break;
34237 case CPP_DIV:
34238 opcode = TRUNC_DIV_EXPR;
34239 break;
34240 case CPP_PLUS:
34241 opcode = PLUS_EXPR;
34242 break;
34243 case CPP_MINUS:
34244 opcode = MINUS_EXPR;
34245 break;
34246 case CPP_LSHIFT:
34247 opcode = LSHIFT_EXPR;
34248 break;
34249 case CPP_RSHIFT:
34250 opcode = RSHIFT_EXPR;
34251 break;
34252 case CPP_AND:
34253 opcode = BIT_AND_EXPR;
34254 break;
34255 case CPP_OR:
34256 opcode = BIT_IOR_EXPR;
34257 break;
34258 case CPP_XOR:
34259 opcode = BIT_XOR_EXPR;
34260 break;
34261 default:
34262 cp_parser_error (parser,
34263 "invalid operator for %<#pragma omp atomic%>");
34264 goto saw_error;
34266 oprec = TOKEN_PRECEDENCE (token);
34267 gcc_assert (oprec != PREC_NOT_OPERATOR);
34268 if (commutative_tree_code (opcode))
34269 oprec = (enum cp_parser_prec) (oprec - 1);
34270 cp_lexer_consume_token (parser->lexer);
34271 rhs = cp_parser_binary_expression (parser, false, false,
34272 oprec, NULL);
34273 if (rhs == error_mark_node)
34274 goto saw_error;
34275 goto stmt_done;
34276 /* FALLTHROUGH */
34277 default:
34278 cp_parser_error (parser,
34279 "invalid operator for %<#pragma omp atomic%>");
34280 goto saw_error;
34282 cp_lexer_consume_token (parser->lexer);
34284 rhs = cp_parser_expression (parser);
34285 if (rhs == error_mark_node)
34286 goto saw_error;
34287 break;
34289 stmt_done:
34290 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34292 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34293 goto saw_error;
34294 v = cp_parser_unary_expression (parser);
34295 if (v == error_mark_node)
34296 goto saw_error;
34297 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34298 goto saw_error;
34299 lhs1 = cp_parser_unary_expression (parser);
34300 if (lhs1 == error_mark_node)
34301 goto saw_error;
34303 if (structured_block)
34305 cp_parser_consume_semicolon_at_end_of_statement (parser);
34306 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34308 done:
34309 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34310 if (!structured_block)
34311 cp_parser_consume_semicolon_at_end_of_statement (parser);
34312 return;
34314 saw_error:
34315 cp_parser_skip_to_end_of_block_or_statement (parser);
34316 if (structured_block)
34318 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34319 cp_lexer_consume_token (parser->lexer);
34320 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34322 cp_parser_skip_to_end_of_block_or_statement (parser);
34323 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34324 cp_lexer_consume_token (parser->lexer);
34330 /* OpenMP 2.5:
34331 # pragma omp barrier new-line */
34333 static void
34334 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34336 cp_parser_require_pragma_eol (parser, pragma_tok);
34337 finish_omp_barrier ();
34340 /* OpenMP 2.5:
34341 # pragma omp critical [(name)] new-line
34342 structured-block
34344 OpenMP 4.5:
34345 # pragma omp critical [(name) [hint(expression)]] new-line
34346 structured-block */
34348 #define OMP_CRITICAL_CLAUSE_MASK \
34349 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34351 static tree
34352 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34354 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34356 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34358 matching_parens parens;
34359 parens.consume_open (parser);
34361 name = cp_parser_identifier (parser);
34363 if (name == error_mark_node
34364 || !parens.require_close (parser))
34365 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34366 /*or_comma=*/false,
34367 /*consume_paren=*/true);
34368 if (name == error_mark_node)
34369 name = NULL;
34371 clauses = cp_parser_omp_all_clauses (parser,
34372 OMP_CRITICAL_CLAUSE_MASK,
34373 "#pragma omp critical", pragma_tok);
34375 else
34376 cp_parser_require_pragma_eol (parser, pragma_tok);
34378 stmt = cp_parser_omp_structured_block (parser, if_p);
34379 return c_finish_omp_critical (input_location, stmt, name, clauses);
34382 /* OpenMP 2.5:
34383 # pragma omp flush flush-vars[opt] new-line
34385 flush-vars:
34386 ( variable-list ) */
34388 static void
34389 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34391 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34392 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34393 cp_parser_require_pragma_eol (parser, pragma_tok);
34395 finish_omp_flush ();
34398 /* Helper function, to parse omp for increment expression. */
34400 static tree
34401 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
34403 tree cond = cp_parser_binary_expression (parser, false, true,
34404 PREC_NOT_OPERATOR, NULL);
34405 if (cond == error_mark_node
34406 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34408 cp_parser_skip_to_end_of_statement (parser);
34409 return error_mark_node;
34412 switch (TREE_CODE (cond))
34414 case GT_EXPR:
34415 case GE_EXPR:
34416 case LT_EXPR:
34417 case LE_EXPR:
34418 break;
34419 case NE_EXPR:
34420 /* Fall through: OpenMP disallows NE_EXPR. */
34421 gcc_fallthrough ();
34422 default:
34423 return error_mark_node;
34426 /* If decl is an iterator, preserve LHS and RHS of the relational
34427 expr until finish_omp_for. */
34428 if (decl
34429 && (type_dependent_expression_p (decl)
34430 || CLASS_TYPE_P (TREE_TYPE (decl))))
34431 return cond;
34433 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34434 TREE_CODE (cond),
34435 TREE_OPERAND (cond, 0), ERROR_MARK,
34436 TREE_OPERAND (cond, 1), ERROR_MARK,
34437 /*overload=*/NULL, tf_warning_or_error);
34440 /* Helper function, to parse omp for increment expression. */
34442 static tree
34443 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34445 cp_token *token = cp_lexer_peek_token (parser->lexer);
34446 enum tree_code op;
34447 tree lhs, rhs;
34448 cp_id_kind idk;
34449 bool decl_first;
34451 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34453 op = (token->type == CPP_PLUS_PLUS
34454 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34455 cp_lexer_consume_token (parser->lexer);
34456 lhs = cp_parser_simple_cast_expression (parser);
34457 if (lhs != decl
34458 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34459 return error_mark_node;
34460 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34463 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34464 if (lhs != decl
34465 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34466 return error_mark_node;
34468 token = cp_lexer_peek_token (parser->lexer);
34469 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34471 op = (token->type == CPP_PLUS_PLUS
34472 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34473 cp_lexer_consume_token (parser->lexer);
34474 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34477 op = cp_parser_assignment_operator_opt (parser);
34478 if (op == ERROR_MARK)
34479 return error_mark_node;
34481 if (op != NOP_EXPR)
34483 rhs = cp_parser_assignment_expression (parser);
34484 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34485 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34488 lhs = cp_parser_binary_expression (parser, false, false,
34489 PREC_ADDITIVE_EXPRESSION, NULL);
34490 token = cp_lexer_peek_token (parser->lexer);
34491 decl_first = (lhs == decl
34492 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34493 if (decl_first)
34494 lhs = NULL_TREE;
34495 if (token->type != CPP_PLUS
34496 && token->type != CPP_MINUS)
34497 return error_mark_node;
34501 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34502 cp_lexer_consume_token (parser->lexer);
34503 rhs = cp_parser_binary_expression (parser, false, false,
34504 PREC_ADDITIVE_EXPRESSION, NULL);
34505 token = cp_lexer_peek_token (parser->lexer);
34506 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34508 if (lhs == NULL_TREE)
34510 if (op == PLUS_EXPR)
34511 lhs = rhs;
34512 else
34513 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34514 tf_warning_or_error);
34516 else
34517 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34518 ERROR_MARK, NULL, tf_warning_or_error);
34521 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34523 if (!decl_first)
34525 if ((rhs != decl
34526 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34527 || op == MINUS_EXPR)
34528 return error_mark_node;
34529 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34531 else
34532 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34534 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34537 /* Parse the initialization statement of an OpenMP for loop.
34539 Return true if the resulting construct should have an
34540 OMP_CLAUSE_PRIVATE added to it. */
34542 static tree
34543 cp_parser_omp_for_loop_init (cp_parser *parser,
34544 tree &this_pre_body,
34545 vec<tree, va_gc> *for_block,
34546 tree &init,
34547 tree &orig_init,
34548 tree &decl,
34549 tree &real_decl)
34551 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34552 return NULL_TREE;
34554 tree add_private_clause = NULL_TREE;
34556 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34558 init-expr:
34559 var = lb
34560 integer-type var = lb
34561 random-access-iterator-type var = lb
34562 pointer-type var = lb
34564 cp_decl_specifier_seq type_specifiers;
34566 /* First, try to parse as an initialized declaration. See
34567 cp_parser_condition, from whence the bulk of this is copied. */
34569 cp_parser_parse_tentatively (parser);
34570 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34571 /*is_trailing_return=*/false,
34572 &type_specifiers);
34573 if (cp_parser_parse_definitely (parser))
34575 /* If parsing a type specifier seq succeeded, then this
34576 MUST be a initialized declaration. */
34577 tree asm_specification, attributes;
34578 cp_declarator *declarator;
34580 declarator = cp_parser_declarator (parser,
34581 CP_PARSER_DECLARATOR_NAMED,
34582 /*ctor_dtor_or_conv_p=*/NULL,
34583 /*parenthesized_p=*/NULL,
34584 /*member_p=*/false,
34585 /*friend_p=*/false);
34586 attributes = cp_parser_attributes_opt (parser);
34587 asm_specification = cp_parser_asm_specification_opt (parser);
34589 if (declarator == cp_error_declarator)
34590 cp_parser_skip_to_end_of_statement (parser);
34592 else
34594 tree pushed_scope, auto_node;
34596 decl = start_decl (declarator, &type_specifiers,
34597 SD_INITIALIZED, attributes,
34598 /*prefix_attributes=*/NULL_TREE,
34599 &pushed_scope);
34601 auto_node = type_uses_auto (TREE_TYPE (decl));
34602 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34604 if (cp_lexer_next_token_is (parser->lexer,
34605 CPP_OPEN_PAREN))
34606 error ("parenthesized initialization is not allowed in "
34607 "OpenMP %<for%> loop");
34608 else
34609 /* Trigger an error. */
34610 cp_parser_require (parser, CPP_EQ, RT_EQ);
34612 init = error_mark_node;
34613 cp_parser_skip_to_end_of_statement (parser);
34615 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34616 || type_dependent_expression_p (decl)
34617 || auto_node)
34619 bool is_direct_init, is_non_constant_init;
34621 init = cp_parser_initializer (parser,
34622 &is_direct_init,
34623 &is_non_constant_init);
34625 if (auto_node)
34627 TREE_TYPE (decl)
34628 = do_auto_deduction (TREE_TYPE (decl), init,
34629 auto_node);
34631 if (!CLASS_TYPE_P (TREE_TYPE (decl))
34632 && !type_dependent_expression_p (decl))
34633 goto non_class;
34636 cp_finish_decl (decl, init, !is_non_constant_init,
34637 asm_specification,
34638 LOOKUP_ONLYCONVERTING);
34639 orig_init = init;
34640 if (CLASS_TYPE_P (TREE_TYPE (decl)))
34642 vec_safe_push (for_block, this_pre_body);
34643 init = NULL_TREE;
34645 else
34647 init = pop_stmt_list (this_pre_body);
34648 if (init && TREE_CODE (init) == STATEMENT_LIST)
34650 tree_stmt_iterator i = tsi_start (init);
34651 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34652 while (!tsi_end_p (i))
34654 tree t = tsi_stmt (i);
34655 if (TREE_CODE (t) == DECL_EXPR
34656 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
34658 tsi_delink (&i);
34659 vec_safe_push (for_block, t);
34660 continue;
34662 break;
34664 if (tsi_one_before_end_p (i))
34666 tree t = tsi_stmt (i);
34667 tsi_delink (&i);
34668 free_stmt_list (init);
34669 init = t;
34673 this_pre_body = NULL_TREE;
34675 else
34677 /* Consume '='. */
34678 cp_lexer_consume_token (parser->lexer);
34679 init = cp_parser_assignment_expression (parser);
34681 non_class:
34682 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
34683 init = error_mark_node;
34684 else
34685 cp_finish_decl (decl, NULL_TREE,
34686 /*init_const_expr_p=*/false,
34687 asm_specification,
34688 LOOKUP_ONLYCONVERTING);
34691 if (pushed_scope)
34692 pop_scope (pushed_scope);
34695 else
34697 cp_id_kind idk;
34698 /* If parsing a type specifier sequence failed, then
34699 this MUST be a simple expression. */
34700 cp_parser_parse_tentatively (parser);
34701 decl = cp_parser_primary_expression (parser, false, false,
34702 false, &idk);
34703 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
34704 if (!cp_parser_error_occurred (parser)
34705 && decl
34706 && (TREE_CODE (decl) == COMPONENT_REF
34707 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
34709 cp_parser_abort_tentative_parse (parser);
34710 cp_parser_parse_tentatively (parser);
34711 cp_token *token = cp_lexer_peek_token (parser->lexer);
34712 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
34713 /*check_dependency_p=*/true,
34714 /*template_p=*/NULL,
34715 /*declarator_p=*/false,
34716 /*optional_p=*/false);
34717 if (name != error_mark_node
34718 && last_tok == cp_lexer_peek_token (parser->lexer))
34720 decl = cp_parser_lookup_name_simple (parser, name,
34721 token->location);
34722 if (TREE_CODE (decl) == FIELD_DECL)
34723 add_private_clause = omp_privatize_field (decl, false);
34725 cp_parser_abort_tentative_parse (parser);
34726 cp_parser_parse_tentatively (parser);
34727 decl = cp_parser_primary_expression (parser, false, false,
34728 false, &idk);
34730 if (!cp_parser_error_occurred (parser)
34731 && decl
34732 && DECL_P (decl)
34733 && CLASS_TYPE_P (TREE_TYPE (decl)))
34735 tree rhs;
34737 cp_parser_parse_definitely (parser);
34738 cp_parser_require (parser, CPP_EQ, RT_EQ);
34739 rhs = cp_parser_assignment_expression (parser);
34740 orig_init = rhs;
34741 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
34742 decl, NOP_EXPR,
34743 rhs,
34744 tf_warning_or_error));
34745 if (!add_private_clause)
34746 add_private_clause = decl;
34748 else
34750 decl = NULL;
34751 cp_parser_abort_tentative_parse (parser);
34752 init = cp_parser_expression (parser);
34753 if (init)
34755 if (TREE_CODE (init) == MODIFY_EXPR
34756 || TREE_CODE (init) == MODOP_EXPR)
34757 real_decl = TREE_OPERAND (init, 0);
34761 return add_private_clause;
34764 /* Parse the restricted form of the for statement allowed by OpenMP. */
34766 static tree
34767 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
34768 tree *cclauses, bool *if_p)
34770 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
34771 tree real_decl, initv, condv, incrv, declv;
34772 tree this_pre_body, cl, ordered_cl = NULL_TREE;
34773 location_t loc_first;
34774 bool collapse_err = false;
34775 int i, collapse = 1, ordered = 0, count, nbraces = 0;
34776 vec<tree, va_gc> *for_block = make_tree_vector ();
34777 auto_vec<tree, 4> orig_inits;
34778 bool tiling = false;
34780 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
34781 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
34782 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
34783 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
34785 tiling = true;
34786 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
34788 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
34789 && OMP_CLAUSE_ORDERED_EXPR (cl))
34791 ordered_cl = cl;
34792 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
34795 if (ordered && ordered < collapse)
34797 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
34798 "%<ordered%> clause parameter is less than %<collapse%>");
34799 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
34800 = build_int_cst (NULL_TREE, collapse);
34801 ordered = collapse;
34803 if (ordered)
34805 for (tree *pc = &clauses; *pc; )
34806 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
34808 error_at (OMP_CLAUSE_LOCATION (*pc),
34809 "%<linear%> clause may not be specified together "
34810 "with %<ordered%> clause with a parameter");
34811 *pc = OMP_CLAUSE_CHAIN (*pc);
34813 else
34814 pc = &OMP_CLAUSE_CHAIN (*pc);
34817 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
34818 count = ordered ? ordered : collapse;
34820 declv = make_tree_vec (count);
34821 initv = make_tree_vec (count);
34822 condv = make_tree_vec (count);
34823 incrv = make_tree_vec (count);
34825 loc_first = cp_lexer_peek_token (parser->lexer)->location;
34827 for (i = 0; i < count; i++)
34829 int bracecount = 0;
34830 tree add_private_clause = NULL_TREE;
34831 location_t loc;
34833 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34835 if (!collapse_err)
34836 cp_parser_error (parser, "for statement expected");
34837 return NULL;
34839 loc = cp_lexer_consume_token (parser->lexer)->location;
34841 matching_parens parens;
34842 if (!parens.require_open (parser))
34843 return NULL;
34845 init = orig_init = decl = real_decl = NULL;
34846 this_pre_body = push_stmt_list ();
34848 add_private_clause
34849 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
34850 init, orig_init, decl, real_decl);
34852 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34853 if (this_pre_body)
34855 this_pre_body = pop_stmt_list (this_pre_body);
34856 if (pre_body)
34858 tree t = pre_body;
34859 pre_body = push_stmt_list ();
34860 add_stmt (t);
34861 add_stmt (this_pre_body);
34862 pre_body = pop_stmt_list (pre_body);
34864 else
34865 pre_body = this_pre_body;
34868 if (decl)
34869 real_decl = decl;
34870 if (cclauses != NULL
34871 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
34872 && real_decl != NULL_TREE)
34874 tree *c;
34875 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
34876 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
34877 && OMP_CLAUSE_DECL (*c) == real_decl)
34879 error_at (loc, "iteration variable %qD"
34880 " should not be firstprivate", real_decl);
34881 *c = OMP_CLAUSE_CHAIN (*c);
34883 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
34884 && OMP_CLAUSE_DECL (*c) == real_decl)
34886 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
34887 tree l = *c;
34888 *c = OMP_CLAUSE_CHAIN (*c);
34889 if (code == OMP_SIMD)
34891 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34892 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
34894 else
34896 OMP_CLAUSE_CHAIN (l) = clauses;
34897 clauses = l;
34899 add_private_clause = NULL_TREE;
34901 else
34903 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
34904 && OMP_CLAUSE_DECL (*c) == real_decl)
34905 add_private_clause = NULL_TREE;
34906 c = &OMP_CLAUSE_CHAIN (*c);
34910 if (add_private_clause)
34912 tree c;
34913 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
34915 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
34916 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
34917 && OMP_CLAUSE_DECL (c) == decl)
34918 break;
34919 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
34920 && OMP_CLAUSE_DECL (c) == decl)
34921 error_at (loc, "iteration variable %qD "
34922 "should not be firstprivate",
34923 decl);
34924 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
34925 && OMP_CLAUSE_DECL (c) == decl)
34926 error_at (loc, "iteration variable %qD should not be reduction",
34927 decl);
34929 if (c == NULL)
34931 if (code != OMP_SIMD)
34932 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
34933 else if (collapse == 1)
34934 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
34935 else
34936 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
34937 OMP_CLAUSE_DECL (c) = add_private_clause;
34938 c = finish_omp_clauses (c, C_ORT_OMP);
34939 if (c)
34941 OMP_CLAUSE_CHAIN (c) = clauses;
34942 clauses = c;
34943 /* For linear, signal that we need to fill up
34944 the so far unknown linear step. */
34945 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
34946 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
34951 cond = NULL;
34952 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34953 cond = cp_parser_omp_for_cond (parser, decl);
34954 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34956 incr = NULL;
34957 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
34959 /* If decl is an iterator, preserve the operator on decl
34960 until finish_omp_for. */
34961 if (real_decl
34962 && ((processing_template_decl
34963 && (TREE_TYPE (real_decl) == NULL_TREE
34964 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
34965 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
34966 incr = cp_parser_omp_for_incr (parser, real_decl);
34967 else
34968 incr = cp_parser_expression (parser);
34969 if (!EXPR_HAS_LOCATION (incr))
34970 protected_set_expr_location (incr, input_location);
34973 if (!parens.require_close (parser))
34974 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34975 /*or_comma=*/false,
34976 /*consume_paren=*/true);
34978 TREE_VEC_ELT (declv, i) = decl;
34979 TREE_VEC_ELT (initv, i) = init;
34980 TREE_VEC_ELT (condv, i) = cond;
34981 TREE_VEC_ELT (incrv, i) = incr;
34982 if (orig_init)
34984 orig_inits.safe_grow_cleared (i + 1);
34985 orig_inits[i] = orig_init;
34988 if (i == count - 1)
34989 break;
34991 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
34992 in between the collapsed for loops to be still considered perfectly
34993 nested. Hopefully the final version clarifies this.
34994 For now handle (multiple) {'s and empty statements. */
34995 cp_parser_parse_tentatively (parser);
34996 for (;;)
34998 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34999 break;
35000 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35002 cp_lexer_consume_token (parser->lexer);
35003 bracecount++;
35005 else if (bracecount
35006 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35007 cp_lexer_consume_token (parser->lexer);
35008 else
35010 loc = cp_lexer_peek_token (parser->lexer)->location;
35011 error_at (loc, "not enough for loops to collapse");
35012 collapse_err = true;
35013 cp_parser_abort_tentative_parse (parser);
35014 declv = NULL_TREE;
35015 break;
35019 if (declv)
35021 cp_parser_parse_definitely (parser);
35022 nbraces += bracecount;
35026 if (nbraces)
35027 if_p = NULL;
35029 /* Note that we saved the original contents of this flag when we entered
35030 the structured block, and so we don't need to re-save it here. */
35031 parser->in_statement = IN_OMP_FOR;
35033 /* Note that the grammar doesn't call for a structured block here,
35034 though the loop as a whole is a structured block. */
35035 body = push_stmt_list ();
35036 cp_parser_statement (parser, NULL_TREE, false, if_p);
35037 body = pop_stmt_list (body);
35039 if (declv == NULL_TREE)
35040 ret = NULL_TREE;
35041 else
35042 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
35043 body, pre_body, &orig_inits, clauses);
35045 while (nbraces)
35047 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35049 cp_lexer_consume_token (parser->lexer);
35050 nbraces--;
35052 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35053 cp_lexer_consume_token (parser->lexer);
35054 else
35056 if (!collapse_err)
35058 error_at (cp_lexer_peek_token (parser->lexer)->location,
35059 "collapsed loops not perfectly nested");
35061 collapse_err = true;
35062 cp_parser_statement_seq_opt (parser, NULL);
35063 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
35064 break;
35068 while (!for_block->is_empty ())
35070 tree t = for_block->pop ();
35071 if (TREE_CODE (t) == STATEMENT_LIST)
35072 add_stmt (pop_stmt_list (t));
35073 else
35074 add_stmt (t);
35076 release_tree_vector (for_block);
35078 return ret;
35081 /* Helper function for OpenMP parsing, split clauses and call
35082 finish_omp_clauses on each of the set of clauses afterwards. */
35084 static void
35085 cp_omp_split_clauses (location_t loc, enum tree_code code,
35086 omp_clause_mask mask, tree clauses, tree *cclauses)
35088 int i;
35089 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
35090 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
35091 if (cclauses[i])
35092 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
35095 /* OpenMP 4.0:
35096 #pragma omp simd simd-clause[optseq] new-line
35097 for-loop */
35099 #define OMP_SIMD_CLAUSE_MASK \
35100 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35101 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35102 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35103 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35104 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35105 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35106 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35107 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35109 static tree
35110 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
35111 char *p_name, omp_clause_mask mask, tree *cclauses,
35112 bool *if_p)
35114 tree clauses, sb, ret;
35115 unsigned int save;
35116 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35118 strcat (p_name, " simd");
35119 mask |= OMP_SIMD_CLAUSE_MASK;
35121 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35122 cclauses == NULL);
35123 if (cclauses)
35125 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35126 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35127 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35128 OMP_CLAUSE_ORDERED);
35129 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35131 error_at (OMP_CLAUSE_LOCATION (c),
35132 "%<ordered%> clause with parameter may not be specified "
35133 "on %qs construct", p_name);
35134 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35138 sb = begin_omp_structured_block ();
35139 save = cp_parser_begin_omp_structured_block (parser);
35141 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35143 cp_parser_end_omp_structured_block (parser, save);
35144 add_stmt (finish_omp_structured_block (sb));
35146 return ret;
35149 /* OpenMP 2.5:
35150 #pragma omp for for-clause[optseq] new-line
35151 for-loop
35153 OpenMP 4.0:
35154 #pragma omp for simd for-simd-clause[optseq] new-line
35155 for-loop */
35157 #define OMP_FOR_CLAUSE_MASK \
35158 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35164 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35165 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35166 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35168 static tree
35169 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35170 char *p_name, omp_clause_mask mask, tree *cclauses,
35171 bool *if_p)
35173 tree clauses, sb, ret;
35174 unsigned int save;
35175 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35177 strcat (p_name, " for");
35178 mask |= OMP_FOR_CLAUSE_MASK;
35179 /* parallel for{, simd} disallows nowait clause, but for
35180 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35181 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35182 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35183 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35184 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35185 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35187 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35189 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35190 const char *p = IDENTIFIER_POINTER (id);
35192 if (strcmp (p, "simd") == 0)
35194 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35195 if (cclauses == NULL)
35196 cclauses = cclauses_buf;
35198 cp_lexer_consume_token (parser->lexer);
35199 if (!flag_openmp) /* flag_openmp_simd */
35200 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35201 cclauses, if_p);
35202 sb = begin_omp_structured_block ();
35203 save = cp_parser_begin_omp_structured_block (parser);
35204 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35205 cclauses, if_p);
35206 cp_parser_end_omp_structured_block (parser, save);
35207 tree body = finish_omp_structured_block (sb);
35208 if (ret == NULL)
35209 return ret;
35210 ret = make_node (OMP_FOR);
35211 TREE_TYPE (ret) = void_type_node;
35212 OMP_FOR_BODY (ret) = body;
35213 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35214 SET_EXPR_LOCATION (ret, loc);
35215 add_stmt (ret);
35216 return ret;
35219 if (!flag_openmp) /* flag_openmp_simd */
35221 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35222 return NULL_TREE;
35225 /* Composite distribute parallel for disallows linear clause. */
35226 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35227 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35229 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35230 cclauses == NULL);
35231 if (cclauses)
35233 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35234 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35237 sb = begin_omp_structured_block ();
35238 save = cp_parser_begin_omp_structured_block (parser);
35240 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35242 cp_parser_end_omp_structured_block (parser, save);
35243 add_stmt (finish_omp_structured_block (sb));
35245 return ret;
35248 /* OpenMP 2.5:
35249 # pragma omp master new-line
35250 structured-block */
35252 static tree
35253 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35255 cp_parser_require_pragma_eol (parser, pragma_tok);
35256 return c_finish_omp_master (input_location,
35257 cp_parser_omp_structured_block (parser, if_p));
35260 /* OpenMP 2.5:
35261 # pragma omp ordered new-line
35262 structured-block
35264 OpenMP 4.5:
35265 # pragma omp ordered ordered-clauses new-line
35266 structured-block */
35268 #define OMP_ORDERED_CLAUSE_MASK \
35269 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35272 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35273 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35275 static bool
35276 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35277 enum pragma_context context, bool *if_p)
35279 location_t loc = pragma_tok->location;
35281 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35283 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35284 const char *p = IDENTIFIER_POINTER (id);
35286 if (strcmp (p, "depend") == 0)
35288 if (!flag_openmp) /* flag_openmp_simd */
35290 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35291 return false;
35293 if (context == pragma_stmt)
35295 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35296 "%<depend%> clause may only be used in compound "
35297 "statements");
35298 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35299 return false;
35301 tree clauses
35302 = cp_parser_omp_all_clauses (parser,
35303 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35304 "#pragma omp ordered", pragma_tok);
35305 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35306 return false;
35310 tree clauses
35311 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35312 "#pragma omp ordered", pragma_tok);
35314 if (!flag_openmp /* flag_openmp_simd */
35315 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
35316 return false;
35318 c_finish_omp_ordered (loc, clauses,
35319 cp_parser_omp_structured_block (parser, if_p));
35320 return true;
35323 /* OpenMP 2.5:
35325 section-scope:
35326 { section-sequence }
35328 section-sequence:
35329 section-directive[opt] structured-block
35330 section-sequence section-directive structured-block */
35332 static tree
35333 cp_parser_omp_sections_scope (cp_parser *parser)
35335 tree stmt, substmt;
35336 bool error_suppress = false;
35337 cp_token *tok;
35339 matching_braces braces;
35340 if (!braces.require_open (parser))
35341 return NULL_TREE;
35343 stmt = push_stmt_list ();
35345 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35346 != PRAGMA_OMP_SECTION)
35348 substmt = cp_parser_omp_structured_block (parser, NULL);
35349 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35350 add_stmt (substmt);
35353 while (1)
35355 tok = cp_lexer_peek_token (parser->lexer);
35356 if (tok->type == CPP_CLOSE_BRACE)
35357 break;
35358 if (tok->type == CPP_EOF)
35359 break;
35361 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35363 cp_lexer_consume_token (parser->lexer);
35364 cp_parser_require_pragma_eol (parser, tok);
35365 error_suppress = false;
35367 else if (!error_suppress)
35369 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35370 error_suppress = true;
35373 substmt = cp_parser_omp_structured_block (parser, NULL);
35374 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35375 add_stmt (substmt);
35377 braces.require_close (parser);
35379 substmt = pop_stmt_list (stmt);
35381 stmt = make_node (OMP_SECTIONS);
35382 TREE_TYPE (stmt) = void_type_node;
35383 OMP_SECTIONS_BODY (stmt) = substmt;
35385 add_stmt (stmt);
35386 return stmt;
35389 /* OpenMP 2.5:
35390 # pragma omp sections sections-clause[optseq] newline
35391 sections-scope */
35393 #define OMP_SECTIONS_CLAUSE_MASK \
35394 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35400 static tree
35401 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35402 char *p_name, omp_clause_mask mask, tree *cclauses)
35404 tree clauses, ret;
35405 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35407 strcat (p_name, " sections");
35408 mask |= OMP_SECTIONS_CLAUSE_MASK;
35409 if (cclauses)
35410 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35412 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35413 cclauses == NULL);
35414 if (cclauses)
35416 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35417 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35420 ret = cp_parser_omp_sections_scope (parser);
35421 if (ret)
35422 OMP_SECTIONS_CLAUSES (ret) = clauses;
35424 return ret;
35427 /* OpenMP 2.5:
35428 # pragma omp parallel parallel-clause[optseq] new-line
35429 structured-block
35430 # pragma omp parallel for parallel-for-clause[optseq] new-line
35431 structured-block
35432 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35433 structured-block
35435 OpenMP 4.0:
35436 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35437 structured-block */
35439 #define OMP_PARALLEL_CLAUSE_MASK \
35440 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35445 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35446 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35447 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35448 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35450 static tree
35451 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35452 char *p_name, omp_clause_mask mask, tree *cclauses,
35453 bool *if_p)
35455 tree stmt, clauses, block;
35456 unsigned int save;
35457 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35459 strcat (p_name, " parallel");
35460 mask |= OMP_PARALLEL_CLAUSE_MASK;
35461 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35462 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35463 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35464 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35466 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35468 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35469 if (cclauses == NULL)
35470 cclauses = cclauses_buf;
35472 cp_lexer_consume_token (parser->lexer);
35473 if (!flag_openmp) /* flag_openmp_simd */
35474 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35475 if_p);
35476 block = begin_omp_parallel ();
35477 save = cp_parser_begin_omp_structured_block (parser);
35478 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35479 if_p);
35480 cp_parser_end_omp_structured_block (parser, save);
35481 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35482 block);
35483 if (ret == NULL_TREE)
35484 return ret;
35485 OMP_PARALLEL_COMBINED (stmt) = 1;
35486 return stmt;
35488 /* When combined with distribute, parallel has to be followed by for.
35489 #pragma omp target parallel is allowed though. */
35490 else if (cclauses
35491 && (mask & (OMP_CLAUSE_MASK_1
35492 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35494 error_at (loc, "expected %<for%> after %qs", p_name);
35495 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35496 return NULL_TREE;
35498 else if (!flag_openmp) /* flag_openmp_simd */
35500 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35501 return NULL_TREE;
35503 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35505 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35506 const char *p = IDENTIFIER_POINTER (id);
35507 if (strcmp (p, "sections") == 0)
35509 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35510 cclauses = cclauses_buf;
35512 cp_lexer_consume_token (parser->lexer);
35513 block = begin_omp_parallel ();
35514 save = cp_parser_begin_omp_structured_block (parser);
35515 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35516 cp_parser_end_omp_structured_block (parser, save);
35517 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35518 block);
35519 OMP_PARALLEL_COMBINED (stmt) = 1;
35520 return stmt;
35524 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35525 cclauses == NULL);
35526 if (cclauses)
35528 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35529 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35532 block = begin_omp_parallel ();
35533 save = cp_parser_begin_omp_structured_block (parser);
35534 cp_parser_statement (parser, NULL_TREE, false, if_p);
35535 cp_parser_end_omp_structured_block (parser, save);
35536 stmt = finish_omp_parallel (clauses, block);
35537 return stmt;
35540 /* OpenMP 2.5:
35541 # pragma omp single single-clause[optseq] new-line
35542 structured-block */
35544 #define OMP_SINGLE_CLAUSE_MASK \
35545 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35546 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35547 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35548 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35550 static tree
35551 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35553 tree stmt = make_node (OMP_SINGLE);
35554 TREE_TYPE (stmt) = void_type_node;
35556 OMP_SINGLE_CLAUSES (stmt)
35557 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35558 "#pragma omp single", pragma_tok);
35559 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35561 return add_stmt (stmt);
35564 /* OpenMP 3.0:
35565 # pragma omp task task-clause[optseq] new-line
35566 structured-block */
35568 #define OMP_TASK_CLAUSE_MASK \
35569 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35570 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35573 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35574 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35575 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35576 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35577 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35578 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35580 static tree
35581 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35583 tree clauses, block;
35584 unsigned int save;
35586 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35587 "#pragma omp task", pragma_tok);
35588 block = begin_omp_task ();
35589 save = cp_parser_begin_omp_structured_block (parser);
35590 cp_parser_statement (parser, NULL_TREE, false, if_p);
35591 cp_parser_end_omp_structured_block (parser, save);
35592 return finish_omp_task (clauses, block);
35595 /* OpenMP 3.0:
35596 # pragma omp taskwait new-line */
35598 static void
35599 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35601 cp_parser_require_pragma_eol (parser, pragma_tok);
35602 finish_omp_taskwait ();
35605 /* OpenMP 3.1:
35606 # pragma omp taskyield new-line */
35608 static void
35609 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35611 cp_parser_require_pragma_eol (parser, pragma_tok);
35612 finish_omp_taskyield ();
35615 /* OpenMP 4.0:
35616 # pragma omp taskgroup new-line
35617 structured-block */
35619 static tree
35620 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35622 cp_parser_require_pragma_eol (parser, pragma_tok);
35623 return c_finish_omp_taskgroup (input_location,
35624 cp_parser_omp_structured_block (parser,
35625 if_p));
35629 /* OpenMP 2.5:
35630 # pragma omp threadprivate (variable-list) */
35632 static void
35633 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
35635 tree vars;
35637 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35638 cp_parser_require_pragma_eol (parser, pragma_tok);
35640 finish_omp_threadprivate (vars);
35643 /* OpenMP 4.0:
35644 # pragma omp cancel cancel-clause[optseq] new-line */
35646 #define OMP_CANCEL_CLAUSE_MASK \
35647 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35648 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35649 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35650 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35651 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35653 static void
35654 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
35656 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
35657 "#pragma omp cancel", pragma_tok);
35658 finish_omp_cancel (clauses);
35661 /* OpenMP 4.0:
35662 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35664 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35665 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35667 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35670 static void
35671 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
35672 enum pragma_context context)
35674 tree clauses;
35675 bool point_seen = false;
35677 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35679 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35680 const char *p = IDENTIFIER_POINTER (id);
35682 if (strcmp (p, "point") == 0)
35684 cp_lexer_consume_token (parser->lexer);
35685 point_seen = true;
35688 if (!point_seen)
35690 cp_parser_error (parser, "expected %<point%>");
35691 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35692 return;
35695 if (context != pragma_compound)
35697 if (context == pragma_stmt)
35698 error_at (pragma_tok->location,
35699 "%<#pragma %s%> may only be used in compound statements",
35700 "omp cancellation point");
35701 else
35702 cp_parser_error (parser, "expected declaration specifiers");
35703 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35704 return;
35707 clauses = cp_parser_omp_all_clauses (parser,
35708 OMP_CANCELLATION_POINT_CLAUSE_MASK,
35709 "#pragma omp cancellation point",
35710 pragma_tok);
35711 finish_omp_cancellation_point (clauses);
35714 /* OpenMP 4.0:
35715 #pragma omp distribute distribute-clause[optseq] new-line
35716 for-loop */
35718 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35719 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35722 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35723 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35725 static tree
35726 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
35727 char *p_name, omp_clause_mask mask, tree *cclauses,
35728 bool *if_p)
35730 tree clauses, sb, ret;
35731 unsigned int save;
35732 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35734 strcat (p_name, " distribute");
35735 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
35737 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35739 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35740 const char *p = IDENTIFIER_POINTER (id);
35741 bool simd = false;
35742 bool parallel = false;
35744 if (strcmp (p, "simd") == 0)
35745 simd = true;
35746 else
35747 parallel = strcmp (p, "parallel") == 0;
35748 if (parallel || simd)
35750 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35751 if (cclauses == NULL)
35752 cclauses = cclauses_buf;
35753 cp_lexer_consume_token (parser->lexer);
35754 if (!flag_openmp) /* flag_openmp_simd */
35756 if (simd)
35757 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35758 cclauses, if_p);
35759 else
35760 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35761 cclauses, if_p);
35763 sb = begin_omp_structured_block ();
35764 save = cp_parser_begin_omp_structured_block (parser);
35765 if (simd)
35766 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35767 cclauses, if_p);
35768 else
35769 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35770 cclauses, if_p);
35771 cp_parser_end_omp_structured_block (parser, save);
35772 tree body = finish_omp_structured_block (sb);
35773 if (ret == NULL)
35774 return ret;
35775 ret = make_node (OMP_DISTRIBUTE);
35776 TREE_TYPE (ret) = void_type_node;
35777 OMP_FOR_BODY (ret) = body;
35778 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35779 SET_EXPR_LOCATION (ret, loc);
35780 add_stmt (ret);
35781 return ret;
35784 if (!flag_openmp) /* flag_openmp_simd */
35786 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35787 return NULL_TREE;
35790 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35791 cclauses == NULL);
35792 if (cclauses)
35794 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
35795 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35798 sb = begin_omp_structured_block ();
35799 save = cp_parser_begin_omp_structured_block (parser);
35801 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
35803 cp_parser_end_omp_structured_block (parser, save);
35804 add_stmt (finish_omp_structured_block (sb));
35806 return ret;
35809 /* OpenMP 4.0:
35810 # pragma omp teams teams-clause[optseq] new-line
35811 structured-block */
35813 #define OMP_TEAMS_CLAUSE_MASK \
35814 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
35819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
35820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
35822 static tree
35823 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
35824 char *p_name, omp_clause_mask mask, tree *cclauses,
35825 bool *if_p)
35827 tree clauses, sb, ret;
35828 unsigned int save;
35829 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35831 strcat (p_name, " teams");
35832 mask |= OMP_TEAMS_CLAUSE_MASK;
35834 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35836 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35837 const char *p = IDENTIFIER_POINTER (id);
35838 if (strcmp (p, "distribute") == 0)
35840 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35841 if (cclauses == NULL)
35842 cclauses = cclauses_buf;
35844 cp_lexer_consume_token (parser->lexer);
35845 if (!flag_openmp) /* flag_openmp_simd */
35846 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35847 cclauses, if_p);
35848 sb = begin_omp_structured_block ();
35849 save = cp_parser_begin_omp_structured_block (parser);
35850 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35851 cclauses, if_p);
35852 cp_parser_end_omp_structured_block (parser, save);
35853 tree body = finish_omp_structured_block (sb);
35854 if (ret == NULL)
35855 return ret;
35856 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35857 ret = make_node (OMP_TEAMS);
35858 TREE_TYPE (ret) = void_type_node;
35859 OMP_TEAMS_CLAUSES (ret) = clauses;
35860 OMP_TEAMS_BODY (ret) = body;
35861 OMP_TEAMS_COMBINED (ret) = 1;
35862 SET_EXPR_LOCATION (ret, loc);
35863 return add_stmt (ret);
35866 if (!flag_openmp) /* flag_openmp_simd */
35868 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35869 return NULL_TREE;
35872 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35873 cclauses == NULL);
35874 if (cclauses)
35876 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
35877 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35880 tree stmt = make_node (OMP_TEAMS);
35881 TREE_TYPE (stmt) = void_type_node;
35882 OMP_TEAMS_CLAUSES (stmt) = clauses;
35883 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35884 SET_EXPR_LOCATION (stmt, loc);
35886 return add_stmt (stmt);
35889 /* OpenMP 4.0:
35890 # pragma omp target data target-data-clause[optseq] new-line
35891 structured-block */
35893 #define OMP_TARGET_DATA_CLAUSE_MASK \
35894 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35897 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
35899 static tree
35900 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35902 tree clauses
35903 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
35904 "#pragma omp target data", pragma_tok);
35905 int map_seen = 0;
35906 for (tree *pc = &clauses; *pc;)
35908 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35909 switch (OMP_CLAUSE_MAP_KIND (*pc))
35911 case GOMP_MAP_TO:
35912 case GOMP_MAP_ALWAYS_TO:
35913 case GOMP_MAP_FROM:
35914 case GOMP_MAP_ALWAYS_FROM:
35915 case GOMP_MAP_TOFROM:
35916 case GOMP_MAP_ALWAYS_TOFROM:
35917 case GOMP_MAP_ALLOC:
35918 map_seen = 3;
35919 break;
35920 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35921 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35922 case GOMP_MAP_ALWAYS_POINTER:
35923 break;
35924 default:
35925 map_seen |= 1;
35926 error_at (OMP_CLAUSE_LOCATION (*pc),
35927 "%<#pragma omp target data%> with map-type other "
35928 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35929 "on %<map%> clause");
35930 *pc = OMP_CLAUSE_CHAIN (*pc);
35931 continue;
35933 pc = &OMP_CLAUSE_CHAIN (*pc);
35936 if (map_seen != 3)
35938 if (map_seen == 0)
35939 error_at (pragma_tok->location,
35940 "%<#pragma omp target data%> must contain at least "
35941 "one %<map%> clause");
35942 return NULL_TREE;
35945 tree stmt = make_node (OMP_TARGET_DATA);
35946 TREE_TYPE (stmt) = void_type_node;
35947 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
35949 keep_next_level (true);
35950 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35952 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35953 return add_stmt (stmt);
35956 /* OpenMP 4.5:
35957 # pragma omp target enter data target-enter-data-clause[optseq] new-line
35958 structured-block */
35960 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
35961 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35967 static tree
35968 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
35969 enum pragma_context context)
35971 bool data_seen = false;
35972 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35974 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35975 const char *p = IDENTIFIER_POINTER (id);
35977 if (strcmp (p, "data") == 0)
35979 cp_lexer_consume_token (parser->lexer);
35980 data_seen = true;
35983 if (!data_seen)
35985 cp_parser_error (parser, "expected %<data%>");
35986 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35987 return NULL_TREE;
35990 if (context == pragma_stmt)
35992 error_at (pragma_tok->location,
35993 "%<#pragma %s%> may only be used in compound statements",
35994 "omp target enter data");
35995 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35996 return NULL_TREE;
35999 tree clauses
36000 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
36001 "#pragma omp target enter data", pragma_tok);
36002 int map_seen = 0;
36003 for (tree *pc = &clauses; *pc;)
36005 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36006 switch (OMP_CLAUSE_MAP_KIND (*pc))
36008 case GOMP_MAP_TO:
36009 case GOMP_MAP_ALWAYS_TO:
36010 case GOMP_MAP_ALLOC:
36011 map_seen = 3;
36012 break;
36013 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36014 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36015 case GOMP_MAP_ALWAYS_POINTER:
36016 break;
36017 default:
36018 map_seen |= 1;
36019 error_at (OMP_CLAUSE_LOCATION (*pc),
36020 "%<#pragma omp target enter data%> with map-type other "
36021 "than %<to%> or %<alloc%> on %<map%> clause");
36022 *pc = OMP_CLAUSE_CHAIN (*pc);
36023 continue;
36025 pc = &OMP_CLAUSE_CHAIN (*pc);
36028 if (map_seen != 3)
36030 if (map_seen == 0)
36031 error_at (pragma_tok->location,
36032 "%<#pragma omp target enter data%> must contain at least "
36033 "one %<map%> clause");
36034 return NULL_TREE;
36037 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
36038 TREE_TYPE (stmt) = void_type_node;
36039 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
36040 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36041 return add_stmt (stmt);
36044 /* OpenMP 4.5:
36045 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36046 structured-block */
36048 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36049 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36055 static tree
36056 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
36057 enum pragma_context context)
36059 bool data_seen = false;
36060 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36062 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36063 const char *p = IDENTIFIER_POINTER (id);
36065 if (strcmp (p, "data") == 0)
36067 cp_lexer_consume_token (parser->lexer);
36068 data_seen = true;
36071 if (!data_seen)
36073 cp_parser_error (parser, "expected %<data%>");
36074 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36075 return NULL_TREE;
36078 if (context == pragma_stmt)
36080 error_at (pragma_tok->location,
36081 "%<#pragma %s%> may only be used in compound statements",
36082 "omp target exit data");
36083 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36084 return NULL_TREE;
36087 tree clauses
36088 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
36089 "#pragma omp target exit data", pragma_tok);
36090 int map_seen = 0;
36091 for (tree *pc = &clauses; *pc;)
36093 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36094 switch (OMP_CLAUSE_MAP_KIND (*pc))
36096 case GOMP_MAP_FROM:
36097 case GOMP_MAP_ALWAYS_FROM:
36098 case GOMP_MAP_RELEASE:
36099 case GOMP_MAP_DELETE:
36100 map_seen = 3;
36101 break;
36102 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36103 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36104 case GOMP_MAP_ALWAYS_POINTER:
36105 break;
36106 default:
36107 map_seen |= 1;
36108 error_at (OMP_CLAUSE_LOCATION (*pc),
36109 "%<#pragma omp target exit data%> with map-type other "
36110 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36111 " clause");
36112 *pc = OMP_CLAUSE_CHAIN (*pc);
36113 continue;
36115 pc = &OMP_CLAUSE_CHAIN (*pc);
36118 if (map_seen != 3)
36120 if (map_seen == 0)
36121 error_at (pragma_tok->location,
36122 "%<#pragma omp target exit data%> must contain at least "
36123 "one %<map%> clause");
36124 return NULL_TREE;
36127 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
36128 TREE_TYPE (stmt) = void_type_node;
36129 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36130 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36131 return add_stmt (stmt);
36134 /* OpenMP 4.0:
36135 # pragma omp target update target-update-clause[optseq] new-line */
36137 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36138 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36145 static bool
36146 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36147 enum pragma_context context)
36149 if (context == pragma_stmt)
36151 error_at (pragma_tok->location,
36152 "%<#pragma %s%> may only be used in compound statements",
36153 "omp target update");
36154 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36155 return false;
36158 tree clauses
36159 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36160 "#pragma omp target update", pragma_tok);
36161 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36162 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36164 error_at (pragma_tok->location,
36165 "%<#pragma omp target update%> must contain at least one "
36166 "%<from%> or %<to%> clauses");
36167 return false;
36170 tree stmt = make_node (OMP_TARGET_UPDATE);
36171 TREE_TYPE (stmt) = void_type_node;
36172 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36173 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36174 add_stmt (stmt);
36175 return false;
36178 /* OpenMP 4.0:
36179 # pragma omp target target-clause[optseq] new-line
36180 structured-block */
36182 #define OMP_TARGET_CLAUSE_MASK \
36183 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36184 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36185 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36186 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36187 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36188 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36189 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36190 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36191 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36193 static bool
36194 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36195 enum pragma_context context, bool *if_p)
36197 tree *pc = NULL, stmt;
36199 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36201 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36202 const char *p = IDENTIFIER_POINTER (id);
36203 enum tree_code ccode = ERROR_MARK;
36205 if (strcmp (p, "teams") == 0)
36206 ccode = OMP_TEAMS;
36207 else if (strcmp (p, "parallel") == 0)
36208 ccode = OMP_PARALLEL;
36209 else if (strcmp (p, "simd") == 0)
36210 ccode = OMP_SIMD;
36211 if (ccode != ERROR_MARK)
36213 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36214 char p_name[sizeof ("#pragma omp target teams distribute "
36215 "parallel for simd")];
36217 cp_lexer_consume_token (parser->lexer);
36218 strcpy (p_name, "#pragma omp target");
36219 if (!flag_openmp) /* flag_openmp_simd */
36221 tree stmt;
36222 switch (ccode)
36224 case OMP_TEAMS:
36225 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36226 OMP_TARGET_CLAUSE_MASK,
36227 cclauses, if_p);
36228 break;
36229 case OMP_PARALLEL:
36230 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36231 OMP_TARGET_CLAUSE_MASK,
36232 cclauses, if_p);
36233 break;
36234 case OMP_SIMD:
36235 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36236 OMP_TARGET_CLAUSE_MASK,
36237 cclauses, if_p);
36238 break;
36239 default:
36240 gcc_unreachable ();
36242 return stmt != NULL_TREE;
36244 keep_next_level (true);
36245 tree sb = begin_omp_structured_block (), ret;
36246 unsigned save = cp_parser_begin_omp_structured_block (parser);
36247 switch (ccode)
36249 case OMP_TEAMS:
36250 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36251 OMP_TARGET_CLAUSE_MASK, cclauses,
36252 if_p);
36253 break;
36254 case OMP_PARALLEL:
36255 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36256 OMP_TARGET_CLAUSE_MASK, cclauses,
36257 if_p);
36258 break;
36259 case OMP_SIMD:
36260 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36261 OMP_TARGET_CLAUSE_MASK, cclauses,
36262 if_p);
36263 break;
36264 default:
36265 gcc_unreachable ();
36267 cp_parser_end_omp_structured_block (parser, save);
36268 tree body = finish_omp_structured_block (sb);
36269 if (ret == NULL_TREE)
36270 return false;
36271 if (ccode == OMP_TEAMS && !processing_template_decl)
36273 /* For combined target teams, ensure the num_teams and
36274 thread_limit clause expressions are evaluated on the host,
36275 before entering the target construct. */
36276 tree c;
36277 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36278 c; c = OMP_CLAUSE_CHAIN (c))
36279 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36280 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36281 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36283 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36284 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36285 if (expr == error_mark_node)
36286 continue;
36287 tree tmp = TARGET_EXPR_SLOT (expr);
36288 add_stmt (expr);
36289 OMP_CLAUSE_OPERAND (c, 0) = expr;
36290 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36291 OMP_CLAUSE_FIRSTPRIVATE);
36292 OMP_CLAUSE_DECL (tc) = tmp;
36293 OMP_CLAUSE_CHAIN (tc)
36294 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36295 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36298 tree stmt = make_node (OMP_TARGET);
36299 TREE_TYPE (stmt) = void_type_node;
36300 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36301 OMP_TARGET_BODY (stmt) = body;
36302 OMP_TARGET_COMBINED (stmt) = 1;
36303 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36304 add_stmt (stmt);
36305 pc = &OMP_TARGET_CLAUSES (stmt);
36306 goto check_clauses;
36308 else if (!flag_openmp) /* flag_openmp_simd */
36310 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36311 return false;
36313 else if (strcmp (p, "data") == 0)
36315 cp_lexer_consume_token (parser->lexer);
36316 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36317 return true;
36319 else if (strcmp (p, "enter") == 0)
36321 cp_lexer_consume_token (parser->lexer);
36322 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36323 return false;
36325 else if (strcmp (p, "exit") == 0)
36327 cp_lexer_consume_token (parser->lexer);
36328 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36329 return false;
36331 else if (strcmp (p, "update") == 0)
36333 cp_lexer_consume_token (parser->lexer);
36334 return cp_parser_omp_target_update (parser, pragma_tok, context);
36337 if (!flag_openmp) /* flag_openmp_simd */
36339 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36340 return false;
36343 stmt = make_node (OMP_TARGET);
36344 TREE_TYPE (stmt) = void_type_node;
36346 OMP_TARGET_CLAUSES (stmt)
36347 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36348 "#pragma omp target", pragma_tok);
36349 pc = &OMP_TARGET_CLAUSES (stmt);
36350 keep_next_level (true);
36351 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36353 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36354 add_stmt (stmt);
36356 check_clauses:
36357 while (*pc)
36359 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36360 switch (OMP_CLAUSE_MAP_KIND (*pc))
36362 case GOMP_MAP_TO:
36363 case GOMP_MAP_ALWAYS_TO:
36364 case GOMP_MAP_FROM:
36365 case GOMP_MAP_ALWAYS_FROM:
36366 case GOMP_MAP_TOFROM:
36367 case GOMP_MAP_ALWAYS_TOFROM:
36368 case GOMP_MAP_ALLOC:
36369 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36370 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36371 case GOMP_MAP_ALWAYS_POINTER:
36372 break;
36373 default:
36374 error_at (OMP_CLAUSE_LOCATION (*pc),
36375 "%<#pragma omp target%> with map-type other "
36376 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36377 "on %<map%> clause");
36378 *pc = OMP_CLAUSE_CHAIN (*pc);
36379 continue;
36381 pc = &OMP_CLAUSE_CHAIN (*pc);
36383 return true;
36386 /* OpenACC 2.0:
36387 # pragma acc cache (variable-list) new-line
36390 static tree
36391 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36393 tree stmt, clauses;
36395 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36396 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36398 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36400 stmt = make_node (OACC_CACHE);
36401 TREE_TYPE (stmt) = void_type_node;
36402 OACC_CACHE_CLAUSES (stmt) = clauses;
36403 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36404 add_stmt (stmt);
36406 return stmt;
36409 /* OpenACC 2.0:
36410 # pragma acc data oacc-data-clause[optseq] new-line
36411 structured-block */
36413 #define OACC_DATA_CLAUSE_MASK \
36414 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36421 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36422 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36423 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36424 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36426 static tree
36427 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36429 tree stmt, clauses, block;
36430 unsigned int save;
36432 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36433 "#pragma acc data", pragma_tok);
36435 block = begin_omp_parallel ();
36436 save = cp_parser_begin_omp_structured_block (parser);
36437 cp_parser_statement (parser, NULL_TREE, false, if_p);
36438 cp_parser_end_omp_structured_block (parser, save);
36439 stmt = finish_oacc_data (clauses, block);
36440 return stmt;
36443 /* OpenACC 2.0:
36444 # pragma acc host_data <clauses> new-line
36445 structured-block */
36447 #define OACC_HOST_DATA_CLAUSE_MASK \
36448 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36450 static tree
36451 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36453 tree stmt, clauses, block;
36454 unsigned int save;
36456 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36457 "#pragma acc host_data", pragma_tok);
36459 block = begin_omp_parallel ();
36460 save = cp_parser_begin_omp_structured_block (parser);
36461 cp_parser_statement (parser, NULL_TREE, false, if_p);
36462 cp_parser_end_omp_structured_block (parser, save);
36463 stmt = finish_oacc_host_data (clauses, block);
36464 return stmt;
36467 /* OpenACC 2.0:
36468 # pragma acc declare oacc-data-clause[optseq] new-line
36471 #define OACC_DECLARE_CLAUSE_MASK \
36472 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36474 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36475 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36477 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36478 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36479 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36480 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36482 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36483 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36485 static tree
36486 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36488 tree clauses, stmt;
36489 bool error = false;
36491 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36492 "#pragma acc declare", pragma_tok, true);
36495 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36497 error_at (pragma_tok->location,
36498 "no valid clauses specified in %<#pragma acc declare%>");
36499 return NULL_TREE;
36502 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36504 location_t loc = OMP_CLAUSE_LOCATION (t);
36505 tree decl = OMP_CLAUSE_DECL (t);
36506 if (!DECL_P (decl))
36508 error_at (loc, "array section in %<#pragma acc declare%>");
36509 error = true;
36510 continue;
36512 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36513 switch (OMP_CLAUSE_MAP_KIND (t))
36515 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36516 case GOMP_MAP_FORCE_ALLOC:
36517 case GOMP_MAP_FORCE_TO:
36518 case GOMP_MAP_FORCE_DEVICEPTR:
36519 case GOMP_MAP_DEVICE_RESIDENT:
36520 break;
36522 case GOMP_MAP_LINK:
36523 if (!global_bindings_p ()
36524 && (TREE_STATIC (decl)
36525 || !DECL_EXTERNAL (decl)))
36527 error_at (loc,
36528 "%qD must be a global variable in "
36529 "%<#pragma acc declare link%>",
36530 decl);
36531 error = true;
36532 continue;
36534 break;
36536 default:
36537 if (global_bindings_p ())
36539 error_at (loc, "invalid OpenACC clause at file scope");
36540 error = true;
36541 continue;
36543 if (DECL_EXTERNAL (decl))
36545 error_at (loc,
36546 "invalid use of %<extern%> variable %qD "
36547 "in %<#pragma acc declare%>", decl);
36548 error = true;
36549 continue;
36551 else if (TREE_PUBLIC (decl))
36553 error_at (loc,
36554 "invalid use of %<global%> variable %qD "
36555 "in %<#pragma acc declare%>", decl);
36556 error = true;
36557 continue;
36559 break;
36562 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36563 || lookup_attribute ("omp declare target link",
36564 DECL_ATTRIBUTES (decl)))
36566 error_at (loc, "variable %qD used more than once with "
36567 "%<#pragma acc declare%>", decl);
36568 error = true;
36569 continue;
36572 if (!error)
36574 tree id;
36576 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36577 id = get_identifier ("omp declare target link");
36578 else
36579 id = get_identifier ("omp declare target");
36581 DECL_ATTRIBUTES (decl)
36582 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36583 if (global_bindings_p ())
36585 symtab_node *node = symtab_node::get (decl);
36586 if (node != NULL)
36588 node->offloadable = 1;
36589 if (ENABLE_OFFLOADING)
36591 g->have_offload = true;
36592 if (is_a <varpool_node *> (node))
36593 vec_safe_push (offload_vars, decl);
36600 if (error || global_bindings_p ())
36601 return NULL_TREE;
36603 stmt = make_node (OACC_DECLARE);
36604 TREE_TYPE (stmt) = void_type_node;
36605 OACC_DECLARE_CLAUSES (stmt) = clauses;
36606 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36608 add_stmt (stmt);
36610 return NULL_TREE;
36613 /* OpenACC 2.0:
36614 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36618 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36620 LOC is the location of the #pragma token.
36623 #define OACC_ENTER_DATA_CLAUSE_MASK \
36624 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36625 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36626 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36627 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36628 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36629 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36630 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36632 #define OACC_EXIT_DATA_CLAUSE_MASK \
36633 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36634 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36635 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36636 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36639 static tree
36640 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
36641 bool enter)
36643 location_t loc = pragma_tok->location;
36644 tree stmt, clauses;
36645 const char *p = "";
36647 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36648 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36650 if (strcmp (p, "data") != 0)
36652 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
36653 enter ? "enter" : "exit");
36654 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36655 return NULL_TREE;
36658 cp_lexer_consume_token (parser->lexer);
36660 if (enter)
36661 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
36662 "#pragma acc enter data", pragma_tok);
36663 else
36664 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
36665 "#pragma acc exit data", pragma_tok);
36667 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36669 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
36670 enter ? "enter" : "exit");
36671 return NULL_TREE;
36674 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
36675 TREE_TYPE (stmt) = void_type_node;
36676 OMP_STANDALONE_CLAUSES (stmt) = clauses;
36677 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36678 add_stmt (stmt);
36679 return stmt;
36682 /* OpenACC 2.0:
36683 # pragma acc loop oacc-loop-clause[optseq] new-line
36684 structured-block */
36686 #define OACC_LOOP_CLAUSE_MASK \
36687 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36688 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36689 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36690 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36691 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36698 static tree
36699 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
36700 omp_clause_mask mask, tree *cclauses, bool *if_p)
36702 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
36704 strcat (p_name, " loop");
36705 mask |= OACC_LOOP_CLAUSE_MASK;
36707 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
36708 cclauses == NULL);
36709 if (cclauses)
36711 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
36712 if (*cclauses)
36713 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
36714 if (clauses)
36715 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36718 tree block = begin_omp_structured_block ();
36719 int save = cp_parser_begin_omp_structured_block (parser);
36720 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
36721 cp_parser_end_omp_structured_block (parser, save);
36722 add_stmt (finish_omp_structured_block (block));
36724 return stmt;
36727 /* OpenACC 2.0:
36728 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36729 structured-block
36733 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36734 structured-block
36737 #define OACC_KERNELS_CLAUSE_MASK \
36738 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36742 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36756 #define OACC_PARALLEL_CLAUSE_MASK \
36757 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36760 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
36765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36767 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36768 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36769 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36770 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36771 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36772 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36773 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36774 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36778 static tree
36779 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
36780 char *p_name, bool *if_p)
36782 omp_clause_mask mask;
36783 enum tree_code code;
36784 switch (cp_parser_pragma_kind (pragma_tok))
36786 case PRAGMA_OACC_KERNELS:
36787 strcat (p_name, " kernels");
36788 mask = OACC_KERNELS_CLAUSE_MASK;
36789 code = OACC_KERNELS;
36790 break;
36791 case PRAGMA_OACC_PARALLEL:
36792 strcat (p_name, " parallel");
36793 mask = OACC_PARALLEL_CLAUSE_MASK;
36794 code = OACC_PARALLEL;
36795 break;
36796 default:
36797 gcc_unreachable ();
36800 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36802 const char *p
36803 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36804 if (strcmp (p, "loop") == 0)
36806 cp_lexer_consume_token (parser->lexer);
36807 tree block = begin_omp_parallel ();
36808 tree clauses;
36809 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
36810 if_p);
36811 return finish_omp_construct (code, block, clauses);
36815 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
36817 tree block = begin_omp_parallel ();
36818 unsigned int save = cp_parser_begin_omp_structured_block (parser);
36819 cp_parser_statement (parser, NULL_TREE, false, if_p);
36820 cp_parser_end_omp_structured_block (parser, save);
36821 return finish_omp_construct (code, block, clauses);
36824 /* OpenACC 2.0:
36825 # pragma acc update oacc-update-clause[optseq] new-line
36828 #define OACC_UPDATE_CLAUSE_MASK \
36829 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
36831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
36832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
36834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
36836 static tree
36837 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
36839 tree stmt, clauses;
36841 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
36842 "#pragma acc update", pragma_tok);
36844 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36846 error_at (pragma_tok->location,
36847 "%<#pragma acc update%> must contain at least one "
36848 "%<device%> or %<host%> or %<self%> clause");
36849 return NULL_TREE;
36852 stmt = make_node (OACC_UPDATE);
36853 TREE_TYPE (stmt) = void_type_node;
36854 OACC_UPDATE_CLAUSES (stmt) = clauses;
36855 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36856 add_stmt (stmt);
36857 return stmt;
36860 /* OpenACC 2.0:
36861 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
36863 LOC is the location of the #pragma token.
36866 #define OACC_WAIT_CLAUSE_MASK \
36867 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
36869 static tree
36870 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
36872 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
36873 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36875 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36876 list = cp_parser_oacc_wait_list (parser, loc, list);
36878 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
36879 "#pragma acc wait", pragma_tok);
36881 stmt = c_finish_oacc_wait (loc, list, clauses);
36882 stmt = finish_expr_stmt (stmt);
36884 return stmt;
36887 /* OpenMP 4.0:
36888 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
36890 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
36891 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36892 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
36895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
36896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
36898 static void
36899 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
36900 enum pragma_context context)
36902 bool first_p = parser->omp_declare_simd == NULL;
36903 cp_omp_declare_simd_data data;
36904 if (first_p)
36906 data.error_seen = false;
36907 data.fndecl_seen = false;
36908 data.tokens = vNULL;
36909 data.clauses = NULL_TREE;
36910 /* It is safe to take the address of a local variable; it will only be
36911 used while this scope is live. */
36912 parser->omp_declare_simd = &data;
36915 /* Store away all pragma tokens. */
36916 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36917 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36918 cp_lexer_consume_token (parser->lexer);
36919 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36920 parser->omp_declare_simd->error_seen = true;
36921 cp_parser_require_pragma_eol (parser, pragma_tok);
36922 struct cp_token_cache *cp
36923 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
36924 parser->omp_declare_simd->tokens.safe_push (cp);
36926 if (first_p)
36928 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
36929 cp_parser_pragma (parser, context, NULL);
36930 switch (context)
36932 case pragma_external:
36933 cp_parser_declaration (parser);
36934 break;
36935 case pragma_member:
36936 cp_parser_member_declaration (parser);
36937 break;
36938 case pragma_objc_icode:
36939 cp_parser_block_declaration (parser, /*statement_p=*/false);
36940 break;
36941 default:
36942 cp_parser_declaration_statement (parser);
36943 break;
36945 if (parser->omp_declare_simd
36946 && !parser->omp_declare_simd->error_seen
36947 && !parser->omp_declare_simd->fndecl_seen)
36948 error_at (pragma_tok->location,
36949 "%<#pragma omp declare simd%> not immediately followed by "
36950 "function declaration or definition");
36951 data.tokens.release ();
36952 parser->omp_declare_simd = NULL;
36956 /* Finalize #pragma omp declare simd clauses after direct declarator has
36957 been parsed, and put that into "omp declare simd" attribute. */
36959 static tree
36960 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
36962 struct cp_token_cache *ce;
36963 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
36964 int i;
36966 if (!data->error_seen && data->fndecl_seen)
36968 error ("%<#pragma omp declare simd%> not immediately followed by "
36969 "a single function declaration or definition");
36970 data->error_seen = true;
36972 if (data->error_seen)
36973 return attrs;
36975 FOR_EACH_VEC_ELT (data->tokens, i, ce)
36977 tree c, cl;
36979 cp_parser_push_lexer_for_tokens (parser, ce);
36980 parser->lexer->in_pragma = true;
36981 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
36982 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
36983 cp_lexer_consume_token (parser->lexer);
36984 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
36985 "#pragma omp declare simd", pragma_tok);
36986 cp_parser_pop_lexer (parser);
36987 if (cl)
36988 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
36989 c = build_tree_list (get_identifier ("omp declare simd"), cl);
36990 TREE_CHAIN (c) = attrs;
36991 if (processing_template_decl)
36992 ATTR_IS_DEPENDENT (c) = 1;
36993 attrs = c;
36996 data->fndecl_seen = true;
36997 return attrs;
37001 /* OpenMP 4.0:
37002 # pragma omp declare target new-line
37003 declarations and definitions
37004 # pragma omp end declare target new-line
37006 OpenMP 4.5:
37007 # pragma omp declare target ( extended-list ) new-line
37009 # pragma omp declare target declare-target-clauses[seq] new-line */
37011 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37012 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37015 static void
37016 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
37018 tree clauses = NULL_TREE;
37019 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37020 clauses
37021 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
37022 "#pragma omp declare target", pragma_tok);
37023 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37025 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37026 clauses);
37027 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37028 cp_parser_require_pragma_eol (parser, pragma_tok);
37030 else
37032 cp_parser_require_pragma_eol (parser, pragma_tok);
37033 scope_chain->omp_declare_target_attribute++;
37034 return;
37036 if (scope_chain->omp_declare_target_attribute)
37037 error_at (pragma_tok->location,
37038 "%<#pragma omp declare target%> with clauses in between "
37039 "%<#pragma omp declare target%> without clauses and "
37040 "%<#pragma omp end declare target%>");
37041 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
37043 tree t = OMP_CLAUSE_DECL (c), id;
37044 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
37045 tree at2 = lookup_attribute ("omp declare target link",
37046 DECL_ATTRIBUTES (t));
37047 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
37049 id = get_identifier ("omp declare target link");
37050 std::swap (at1, at2);
37052 else
37053 id = get_identifier ("omp declare target");
37054 if (at2)
37056 error_at (OMP_CLAUSE_LOCATION (c),
37057 "%qD specified both in declare target %<link%> and %<to%>"
37058 " clauses", t);
37059 continue;
37061 if (!at1)
37063 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
37064 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
37065 continue;
37067 symtab_node *node = symtab_node::get (t);
37068 if (node != NULL)
37070 node->offloadable = 1;
37071 if (ENABLE_OFFLOADING)
37073 g->have_offload = true;
37074 if (is_a <varpool_node *> (node))
37075 vec_safe_push (offload_vars, t);
37082 static void
37083 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37085 const char *p = "";
37086 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37088 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37089 p = IDENTIFIER_POINTER (id);
37091 if (strcmp (p, "declare") == 0)
37093 cp_lexer_consume_token (parser->lexer);
37094 p = "";
37095 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37097 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37098 p = IDENTIFIER_POINTER (id);
37100 if (strcmp (p, "target") == 0)
37101 cp_lexer_consume_token (parser->lexer);
37102 else
37104 cp_parser_error (parser, "expected %<target%>");
37105 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37106 return;
37109 else
37111 cp_parser_error (parser, "expected %<declare%>");
37112 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37113 return;
37115 cp_parser_require_pragma_eol (parser, pragma_tok);
37116 if (!scope_chain->omp_declare_target_attribute)
37117 error_at (pragma_tok->location,
37118 "%<#pragma omp end declare target%> without corresponding "
37119 "%<#pragma omp declare target%>");
37120 else
37121 scope_chain->omp_declare_target_attribute--;
37124 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37125 expression and optional initializer clause of
37126 #pragma omp declare reduction. We store the expression(s) as
37127 either 3, 6 or 7 special statements inside of the artificial function's
37128 body. The first two statements are DECL_EXPRs for the artificial
37129 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37130 expression that uses those variables.
37131 If there was any INITIALIZER clause, this is followed by further statements,
37132 the fourth and fifth statements are DECL_EXPRs for the artificial
37133 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37134 constructor variant (first token after open paren is not omp_priv),
37135 then the sixth statement is a statement with the function call expression
37136 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37137 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37138 to initialize the OMP_PRIV artificial variable and there is seventh
37139 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37141 static bool
37142 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37144 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37145 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
37146 type = TREE_TYPE (type);
37147 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37148 DECL_ARTIFICIAL (omp_out) = 1;
37149 pushdecl (omp_out);
37150 add_decl_expr (omp_out);
37151 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37152 DECL_ARTIFICIAL (omp_in) = 1;
37153 pushdecl (omp_in);
37154 add_decl_expr (omp_in);
37155 tree combiner;
37156 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37158 keep_next_level (true);
37159 tree block = begin_omp_structured_block ();
37160 combiner = cp_parser_expression (parser);
37161 finish_expr_stmt (combiner);
37162 block = finish_omp_structured_block (block);
37163 add_stmt (block);
37165 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37166 return false;
37168 const char *p = "";
37169 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37171 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37172 p = IDENTIFIER_POINTER (id);
37175 if (strcmp (p, "initializer") == 0)
37177 cp_lexer_consume_token (parser->lexer);
37178 matching_parens parens;
37179 if (!parens.require_open (parser))
37180 return false;
37182 p = "";
37183 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37185 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37186 p = IDENTIFIER_POINTER (id);
37189 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37190 DECL_ARTIFICIAL (omp_priv) = 1;
37191 pushdecl (omp_priv);
37192 add_decl_expr (omp_priv);
37193 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37194 DECL_ARTIFICIAL (omp_orig) = 1;
37195 pushdecl (omp_orig);
37196 add_decl_expr (omp_orig);
37198 keep_next_level (true);
37199 block = begin_omp_structured_block ();
37201 bool ctor = false;
37202 if (strcmp (p, "omp_priv") == 0)
37204 bool is_direct_init, is_non_constant_init;
37205 ctor = true;
37206 cp_lexer_consume_token (parser->lexer);
37207 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37208 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37209 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37210 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37211 == CPP_CLOSE_PAREN
37212 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37213 == CPP_CLOSE_PAREN))
37215 finish_omp_structured_block (block);
37216 error ("invalid initializer clause");
37217 return false;
37219 initializer = cp_parser_initializer (parser, &is_direct_init,
37220 &is_non_constant_init);
37221 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37222 NULL_TREE, LOOKUP_ONLYCONVERTING);
37224 else
37226 cp_parser_parse_tentatively (parser);
37227 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37228 /*check_dependency_p=*/true,
37229 /*template_p=*/NULL,
37230 /*declarator_p=*/false,
37231 /*optional_p=*/false);
37232 vec<tree, va_gc> *args;
37233 if (fn_name == error_mark_node
37234 || cp_parser_error_occurred (parser)
37235 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37236 || ((args = cp_parser_parenthesized_expression_list
37237 (parser, non_attr, /*cast_p=*/false,
37238 /*allow_expansion_p=*/true,
37239 /*non_constant_p=*/NULL)),
37240 cp_parser_error_occurred (parser)))
37242 finish_omp_structured_block (block);
37243 cp_parser_abort_tentative_parse (parser);
37244 cp_parser_error (parser, "expected id-expression (arguments)");
37245 return false;
37247 unsigned int i;
37248 tree arg;
37249 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37250 if (arg == omp_priv
37251 || (TREE_CODE (arg) == ADDR_EXPR
37252 && TREE_OPERAND (arg, 0) == omp_priv))
37253 break;
37254 cp_parser_abort_tentative_parse (parser);
37255 if (arg == NULL_TREE)
37256 error ("one of the initializer call arguments should be %<omp_priv%>"
37257 " or %<&omp_priv%>");
37258 initializer = cp_parser_postfix_expression (parser, false, false, false,
37259 false, NULL);
37260 finish_expr_stmt (initializer);
37263 block = finish_omp_structured_block (block);
37264 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37265 add_stmt (block);
37267 if (ctor)
37268 add_decl_expr (omp_orig);
37270 if (!parens.require_close (parser))
37271 return false;
37274 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37275 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
37276 UNKNOWN_LOCATION);
37278 return true;
37281 /* OpenMP 4.0
37282 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37283 initializer-clause[opt] new-line
37285 initializer-clause:
37286 initializer (omp_priv initializer)
37287 initializer (function-name (argument-list)) */
37289 static void
37290 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37291 enum pragma_context)
37293 auto_vec<tree> types;
37294 enum tree_code reduc_code = ERROR_MARK;
37295 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37296 unsigned int i;
37297 cp_token *first_token;
37298 cp_token_cache *cp;
37299 int errs;
37300 void *p;
37302 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37303 p = obstack_alloc (&declarator_obstack, 0);
37305 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37306 goto fail;
37308 switch (cp_lexer_peek_token (parser->lexer)->type)
37310 case CPP_PLUS:
37311 reduc_code = PLUS_EXPR;
37312 break;
37313 case CPP_MULT:
37314 reduc_code = MULT_EXPR;
37315 break;
37316 case CPP_MINUS:
37317 reduc_code = MINUS_EXPR;
37318 break;
37319 case CPP_AND:
37320 reduc_code = BIT_AND_EXPR;
37321 break;
37322 case CPP_XOR:
37323 reduc_code = BIT_XOR_EXPR;
37324 break;
37325 case CPP_OR:
37326 reduc_code = BIT_IOR_EXPR;
37327 break;
37328 case CPP_AND_AND:
37329 reduc_code = TRUTH_ANDIF_EXPR;
37330 break;
37331 case CPP_OR_OR:
37332 reduc_code = TRUTH_ORIF_EXPR;
37333 break;
37334 case CPP_NAME:
37335 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37336 break;
37337 default:
37338 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37339 "%<|%>, %<&&%>, %<||%> or identifier");
37340 goto fail;
37343 if (reduc_code != ERROR_MARK)
37344 cp_lexer_consume_token (parser->lexer);
37346 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37347 if (reduc_id == error_mark_node)
37348 goto fail;
37350 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37351 goto fail;
37353 /* Types may not be defined in declare reduction type list. */
37354 const char *saved_message;
37355 saved_message = parser->type_definition_forbidden_message;
37356 parser->type_definition_forbidden_message
37357 = G_("types may not be defined in declare reduction type list");
37358 bool saved_colon_corrects_to_scope_p;
37359 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37360 parser->colon_corrects_to_scope_p = false;
37361 bool saved_colon_doesnt_start_class_def_p;
37362 saved_colon_doesnt_start_class_def_p
37363 = parser->colon_doesnt_start_class_def_p;
37364 parser->colon_doesnt_start_class_def_p = true;
37366 while (true)
37368 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37369 type = cp_parser_type_id (parser);
37370 if (type == error_mark_node)
37372 else if (ARITHMETIC_TYPE_P (type)
37373 && (orig_reduc_id == NULL_TREE
37374 || (TREE_CODE (type) != COMPLEX_TYPE
37375 && (id_equal (orig_reduc_id, "min")
37376 || id_equal (orig_reduc_id, "max")))))
37377 error_at (loc, "predeclared arithmetic type %qT in "
37378 "%<#pragma omp declare reduction%>", type);
37379 else if (TREE_CODE (type) == FUNCTION_TYPE
37380 || TREE_CODE (type) == METHOD_TYPE
37381 || TREE_CODE (type) == ARRAY_TYPE)
37382 error_at (loc, "function or array type %qT in "
37383 "%<#pragma omp declare reduction%>", type);
37384 else if (TREE_CODE (type) == REFERENCE_TYPE)
37385 error_at (loc, "reference type %qT in "
37386 "%<#pragma omp declare reduction%>", type);
37387 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37388 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37389 "%<#pragma omp declare reduction%>", type);
37390 else
37391 types.safe_push (type);
37393 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37394 cp_lexer_consume_token (parser->lexer);
37395 else
37396 break;
37399 /* Restore the saved message. */
37400 parser->type_definition_forbidden_message = saved_message;
37401 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37402 parser->colon_doesnt_start_class_def_p
37403 = saved_colon_doesnt_start_class_def_p;
37405 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37406 || types.is_empty ())
37408 fail:
37409 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37410 goto done;
37413 first_token = cp_lexer_peek_token (parser->lexer);
37414 cp = NULL;
37415 errs = errorcount;
37416 FOR_EACH_VEC_ELT (types, i, type)
37418 tree fntype
37419 = build_function_type_list (void_type_node,
37420 cp_build_reference_type (type, false),
37421 NULL_TREE);
37422 tree this_reduc_id = reduc_id;
37423 if (!dependent_type_p (type))
37424 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37425 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37426 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37427 DECL_ARTIFICIAL (fndecl) = 1;
37428 DECL_EXTERNAL (fndecl) = 1;
37429 DECL_DECLARED_INLINE_P (fndecl) = 1;
37430 DECL_IGNORED_P (fndecl) = 1;
37431 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37432 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37433 DECL_ATTRIBUTES (fndecl)
37434 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37435 DECL_ATTRIBUTES (fndecl));
37436 if (processing_template_decl)
37437 fndecl = push_template_decl (fndecl);
37438 bool block_scope = false;
37439 tree block = NULL_TREE;
37440 if (current_function_decl)
37442 block_scope = true;
37443 DECL_CONTEXT (fndecl) = global_namespace;
37444 if (!processing_template_decl)
37445 pushdecl (fndecl);
37447 else if (current_class_type)
37449 if (cp == NULL)
37451 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37452 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37453 cp_lexer_consume_token (parser->lexer);
37454 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37455 goto fail;
37456 cp = cp_token_cache_new (first_token,
37457 cp_lexer_peek_nth_token (parser->lexer,
37458 2));
37460 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37461 finish_member_declaration (fndecl);
37462 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37463 DECL_PENDING_INLINE_P (fndecl) = 1;
37464 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37465 continue;
37467 else
37469 DECL_CONTEXT (fndecl) = current_namespace;
37470 pushdecl (fndecl);
37472 if (!block_scope)
37473 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37474 else
37475 block = begin_omp_structured_block ();
37476 if (cp)
37478 cp_parser_push_lexer_for_tokens (parser, cp);
37479 parser->lexer->in_pragma = true;
37481 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37483 if (!block_scope)
37484 finish_function (/*inline_p=*/false);
37485 else
37486 DECL_CONTEXT (fndecl) = current_function_decl;
37487 if (cp)
37488 cp_parser_pop_lexer (parser);
37489 goto fail;
37491 if (cp)
37492 cp_parser_pop_lexer (parser);
37493 if (!block_scope)
37494 finish_function (/*inline_p=*/false);
37495 else
37497 DECL_CONTEXT (fndecl) = current_function_decl;
37498 block = finish_omp_structured_block (block);
37499 if (TREE_CODE (block) == BIND_EXPR)
37500 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37501 else if (TREE_CODE (block) == STATEMENT_LIST)
37502 DECL_SAVED_TREE (fndecl) = block;
37503 if (processing_template_decl)
37504 add_decl_expr (fndecl);
37506 cp_check_omp_declare_reduction (fndecl);
37507 if (cp == NULL && types.length () > 1)
37508 cp = cp_token_cache_new (first_token,
37509 cp_lexer_peek_nth_token (parser->lexer, 2));
37510 if (errs != errorcount)
37511 break;
37514 cp_parser_require_pragma_eol (parser, pragma_tok);
37516 done:
37517 /* Free any declarators allocated. */
37518 obstack_free (&declarator_obstack, p);
37521 /* OpenMP 4.0
37522 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37523 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37524 initializer-clause[opt] new-line
37525 #pragma omp declare target new-line */
37527 static bool
37528 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37529 enum pragma_context context)
37531 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37533 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37534 const char *p = IDENTIFIER_POINTER (id);
37536 if (strcmp (p, "simd") == 0)
37538 cp_lexer_consume_token (parser->lexer);
37539 cp_parser_omp_declare_simd (parser, pragma_tok,
37540 context);
37541 return true;
37543 cp_ensure_no_omp_declare_simd (parser);
37544 if (strcmp (p, "reduction") == 0)
37546 cp_lexer_consume_token (parser->lexer);
37547 cp_parser_omp_declare_reduction (parser, pragma_tok,
37548 context);
37549 return false;
37551 if (!flag_openmp) /* flag_openmp_simd */
37553 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37554 return false;
37556 if (strcmp (p, "target") == 0)
37558 cp_lexer_consume_token (parser->lexer);
37559 cp_parser_omp_declare_target (parser, pragma_tok);
37560 return false;
37563 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37564 "or %<target%>");
37565 cp_parser_require_pragma_eol (parser, pragma_tok);
37566 return false;
37569 /* OpenMP 4.5:
37570 #pragma omp taskloop taskloop-clause[optseq] new-line
37571 for-loop
37573 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37574 for-loop */
37576 #define OMP_TASKLOOP_CLAUSE_MASK \
37577 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37578 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37579 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37580 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37581 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37582 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37585 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37590 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37592 static tree
37593 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37594 char *p_name, omp_clause_mask mask, tree *cclauses,
37595 bool *if_p)
37597 tree clauses, sb, ret;
37598 unsigned int save;
37599 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37601 strcat (p_name, " taskloop");
37602 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37604 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37606 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37607 const char *p = IDENTIFIER_POINTER (id);
37609 if (strcmp (p, "simd") == 0)
37611 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37612 if (cclauses == NULL)
37613 cclauses = cclauses_buf;
37615 cp_lexer_consume_token (parser->lexer);
37616 if (!flag_openmp) /* flag_openmp_simd */
37617 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37618 cclauses, if_p);
37619 sb = begin_omp_structured_block ();
37620 save = cp_parser_begin_omp_structured_block (parser);
37621 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37622 cclauses, if_p);
37623 cp_parser_end_omp_structured_block (parser, save);
37624 tree body = finish_omp_structured_block (sb);
37625 if (ret == NULL)
37626 return ret;
37627 ret = make_node (OMP_TASKLOOP);
37628 TREE_TYPE (ret) = void_type_node;
37629 OMP_FOR_BODY (ret) = body;
37630 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37631 SET_EXPR_LOCATION (ret, loc);
37632 add_stmt (ret);
37633 return ret;
37636 if (!flag_openmp) /* flag_openmp_simd */
37638 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37639 return NULL_TREE;
37642 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37643 cclauses == NULL);
37644 if (cclauses)
37646 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
37647 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37650 sb = begin_omp_structured_block ();
37651 save = cp_parser_begin_omp_structured_block (parser);
37653 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
37654 if_p);
37656 cp_parser_end_omp_structured_block (parser, save);
37657 add_stmt (finish_omp_structured_block (sb));
37659 return ret;
37663 /* OpenACC 2.0:
37664 # pragma acc routine oacc-routine-clause[optseq] new-line
37665 function-definition
37667 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37670 #define OACC_ROUTINE_CLAUSE_MASK \
37671 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37672 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37673 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37677 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37678 component, which must resolve to a declared namespace-scope
37679 function. The clauses are either processed directly (for a named
37680 function), or defered until the immediatley following declaration
37681 is parsed. */
37683 static void
37684 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
37685 enum pragma_context context)
37687 gcc_checking_assert (context == pragma_external);
37688 /* The checking for "another pragma following this one" in the "no optional
37689 '( name )'" case makes sure that we dont re-enter. */
37690 gcc_checking_assert (parser->oacc_routine == NULL);
37692 cp_oacc_routine_data data;
37693 data.error_seen = false;
37694 data.fndecl_seen = false;
37695 data.tokens = vNULL;
37696 data.clauses = NULL_TREE;
37697 data.loc = pragma_tok->location;
37698 /* It is safe to take the address of a local variable; it will only be
37699 used while this scope is live. */
37700 parser->oacc_routine = &data;
37702 /* Look for optional '( name )'. */
37703 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37705 matching_parens parens;
37706 parens.consume_open (parser); /* '(' */
37708 /* We parse the name as an id-expression. If it resolves to
37709 anything other than a non-overloaded function at namespace
37710 scope, it's an error. */
37711 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
37712 tree name = cp_parser_id_expression (parser,
37713 /*template_keyword_p=*/false,
37714 /*check_dependency_p=*/false,
37715 /*template_p=*/NULL,
37716 /*declarator_p=*/false,
37717 /*optional_p=*/false);
37718 tree decl = cp_parser_lookup_name_simple (parser, name, name_loc);
37719 if (name != error_mark_node && decl == error_mark_node)
37720 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
37722 if (decl == error_mark_node
37723 || !parens.require_close (parser))
37725 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37726 parser->oacc_routine = NULL;
37727 return;
37730 data.clauses
37731 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37732 "#pragma acc routine",
37733 cp_lexer_peek_token (parser->lexer));
37735 if (decl && is_overloaded_fn (decl)
37736 && (TREE_CODE (decl) != FUNCTION_DECL
37737 || DECL_FUNCTION_TEMPLATE_P (decl)))
37739 error_at (name_loc,
37740 "%<#pragma acc routine%> names a set of overloads");
37741 parser->oacc_routine = NULL;
37742 return;
37745 /* Perhaps we should use the same rule as declarations in different
37746 namespaces? */
37747 if (!DECL_NAMESPACE_SCOPE_P (decl))
37749 error_at (name_loc,
37750 "%qD does not refer to a namespace scope function", decl);
37751 parser->oacc_routine = NULL;
37752 return;
37755 if (TREE_CODE (decl) != FUNCTION_DECL)
37757 error_at (name_loc, "%qD does not refer to a function", decl);
37758 parser->oacc_routine = NULL;
37759 return;
37762 cp_finalize_oacc_routine (parser, decl, false);
37763 parser->oacc_routine = NULL;
37765 else /* No optional '( name )'. */
37767 /* Store away all pragma tokens. */
37768 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37769 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37770 cp_lexer_consume_token (parser->lexer);
37771 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37772 parser->oacc_routine->error_seen = true;
37773 cp_parser_require_pragma_eol (parser, pragma_tok);
37774 struct cp_token_cache *cp
37775 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37776 parser->oacc_routine->tokens.safe_push (cp);
37778 /* Emit a helpful diagnostic if there's another pragma following this
37779 one. */
37780 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37782 cp_ensure_no_oacc_routine (parser);
37783 data.tokens.release ();
37784 /* ..., and then just keep going. */
37785 return;
37788 /* We only have to consider the pragma_external case here. */
37789 cp_parser_declaration (parser);
37790 if (parser->oacc_routine
37791 && !parser->oacc_routine->fndecl_seen)
37792 cp_ensure_no_oacc_routine (parser);
37793 else
37794 parser->oacc_routine = NULL;
37795 data.tokens.release ();
37799 /* Finalize #pragma acc routine clauses after direct declarator has
37800 been parsed. */
37802 static tree
37803 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
37805 struct cp_token_cache *ce;
37806 cp_oacc_routine_data *data = parser->oacc_routine;
37808 if (!data->error_seen && data->fndecl_seen)
37810 error_at (data->loc,
37811 "%<#pragma acc routine%> not immediately followed by "
37812 "a single function declaration or definition");
37813 data->error_seen = true;
37815 if (data->error_seen)
37816 return attrs;
37818 gcc_checking_assert (data->tokens.length () == 1);
37819 ce = data->tokens[0];
37821 cp_parser_push_lexer_for_tokens (parser, ce);
37822 parser->lexer->in_pragma = true;
37823 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37825 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37826 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
37827 parser->oacc_routine->clauses
37828 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37829 "#pragma acc routine", pragma_tok);
37830 cp_parser_pop_lexer (parser);
37831 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
37832 fndecl_seen. */
37834 return attrs;
37837 /* Apply any saved OpenACC routine clauses to a just-parsed
37838 declaration. */
37840 static void
37841 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
37843 if (__builtin_expect (parser->oacc_routine != NULL, 0))
37845 /* Keep going if we're in error reporting mode. */
37846 if (parser->oacc_routine->error_seen
37847 || fndecl == error_mark_node)
37848 return;
37850 if (parser->oacc_routine->fndecl_seen)
37852 error_at (parser->oacc_routine->loc,
37853 "%<#pragma acc routine%> not immediately followed by"
37854 " a single function declaration or definition");
37855 parser->oacc_routine = NULL;
37856 return;
37858 if (TREE_CODE (fndecl) != FUNCTION_DECL)
37860 cp_ensure_no_oacc_routine (parser);
37861 return;
37864 if (oacc_get_fn_attrib (fndecl))
37866 error_at (parser->oacc_routine->loc,
37867 "%<#pragma acc routine%> already applied to %qD", fndecl);
37868 parser->oacc_routine = NULL;
37869 return;
37872 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
37874 error_at (parser->oacc_routine->loc,
37875 TREE_USED (fndecl)
37876 ? G_("%<#pragma acc routine%> must be applied before use")
37877 : G_("%<#pragma acc routine%> must be applied before "
37878 "definition"));
37879 parser->oacc_routine = NULL;
37880 return;
37883 /* Process the routine's dimension clauses. */
37884 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
37885 oacc_replace_fn_attrib (fndecl, dims);
37887 /* Add an "omp declare target" attribute. */
37888 DECL_ATTRIBUTES (fndecl)
37889 = tree_cons (get_identifier ("omp declare target"),
37890 NULL_TREE, DECL_ATTRIBUTES (fndecl));
37892 /* Don't unset parser->oacc_routine here: we may still need it to
37893 diagnose wrong usage. But, remember that we've used this "#pragma acc
37894 routine". */
37895 parser->oacc_routine->fndecl_seen = true;
37899 /* Main entry point to OpenMP statement pragmas. */
37901 static void
37902 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37904 tree stmt;
37905 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
37906 omp_clause_mask mask (0);
37908 switch (cp_parser_pragma_kind (pragma_tok))
37910 case PRAGMA_OACC_ATOMIC:
37911 cp_parser_omp_atomic (parser, pragma_tok);
37912 return;
37913 case PRAGMA_OACC_CACHE:
37914 stmt = cp_parser_oacc_cache (parser, pragma_tok);
37915 break;
37916 case PRAGMA_OACC_DATA:
37917 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
37918 break;
37919 case PRAGMA_OACC_ENTER_DATA:
37920 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
37921 break;
37922 case PRAGMA_OACC_EXIT_DATA:
37923 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
37924 break;
37925 case PRAGMA_OACC_HOST_DATA:
37926 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
37927 break;
37928 case PRAGMA_OACC_KERNELS:
37929 case PRAGMA_OACC_PARALLEL:
37930 strcpy (p_name, "#pragma acc");
37931 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
37932 if_p);
37933 break;
37934 case PRAGMA_OACC_LOOP:
37935 strcpy (p_name, "#pragma acc");
37936 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
37937 if_p);
37938 break;
37939 case PRAGMA_OACC_UPDATE:
37940 stmt = cp_parser_oacc_update (parser, pragma_tok);
37941 break;
37942 case PRAGMA_OACC_WAIT:
37943 stmt = cp_parser_oacc_wait (parser, pragma_tok);
37944 break;
37945 case PRAGMA_OMP_ATOMIC:
37946 cp_parser_omp_atomic (parser, pragma_tok);
37947 return;
37948 case PRAGMA_OMP_CRITICAL:
37949 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
37950 break;
37951 case PRAGMA_OMP_DISTRIBUTE:
37952 strcpy (p_name, "#pragma omp");
37953 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
37954 if_p);
37955 break;
37956 case PRAGMA_OMP_FOR:
37957 strcpy (p_name, "#pragma omp");
37958 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
37959 if_p);
37960 break;
37961 case PRAGMA_OMP_MASTER:
37962 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
37963 break;
37964 case PRAGMA_OMP_PARALLEL:
37965 strcpy (p_name, "#pragma omp");
37966 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
37967 if_p);
37968 break;
37969 case PRAGMA_OMP_SECTIONS:
37970 strcpy (p_name, "#pragma omp");
37971 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
37972 break;
37973 case PRAGMA_OMP_SIMD:
37974 strcpy (p_name, "#pragma omp");
37975 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
37976 if_p);
37977 break;
37978 case PRAGMA_OMP_SINGLE:
37979 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
37980 break;
37981 case PRAGMA_OMP_TASK:
37982 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
37983 break;
37984 case PRAGMA_OMP_TASKGROUP:
37985 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
37986 break;
37987 case PRAGMA_OMP_TASKLOOP:
37988 strcpy (p_name, "#pragma omp");
37989 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
37990 if_p);
37991 break;
37992 case PRAGMA_OMP_TEAMS:
37993 strcpy (p_name, "#pragma omp");
37994 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
37995 if_p);
37996 break;
37997 default:
37998 gcc_unreachable ();
38001 protected_set_expr_location (stmt, pragma_tok->location);
38004 /* Transactional Memory parsing routines. */
38006 /* Parse a transaction attribute.
38008 txn-attribute:
38009 attribute
38010 [ [ identifier ] ]
38012 We use this instead of cp_parser_attributes_opt for transactions to avoid
38013 the pedwarn in C++98 mode. */
38015 static tree
38016 cp_parser_txn_attribute_opt (cp_parser *parser)
38018 cp_token *token;
38019 tree attr_name, attr = NULL;
38021 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
38022 return cp_parser_attributes_opt (parser);
38024 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
38025 return NULL_TREE;
38026 cp_lexer_consume_token (parser->lexer);
38027 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
38028 goto error1;
38030 token = cp_lexer_peek_token (parser->lexer);
38031 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
38033 token = cp_lexer_consume_token (parser->lexer);
38035 attr_name = (token->type == CPP_KEYWORD
38036 /* For keywords, use the canonical spelling,
38037 not the parsed identifier. */
38038 ? ridpointers[(int) token->keyword]
38039 : token->u.value);
38040 attr = build_tree_list (attr_name, NULL_TREE);
38042 else
38043 cp_parser_error (parser, "expected identifier");
38045 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38046 error1:
38047 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38048 return attr;
38051 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38053 transaction-statement:
38054 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38055 compound-statement
38056 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38059 static tree
38060 cp_parser_transaction (cp_parser *parser, cp_token *token)
38062 unsigned char old_in = parser->in_transaction;
38063 unsigned char this_in = 1, new_in;
38064 enum rid keyword = token->keyword;
38065 tree stmt, attrs, noex;
38067 cp_lexer_consume_token (parser->lexer);
38069 if (keyword == RID_TRANSACTION_RELAXED
38070 || keyword == RID_SYNCHRONIZED)
38071 this_in |= TM_STMT_ATTR_RELAXED;
38072 else
38074 attrs = cp_parser_txn_attribute_opt (parser);
38075 if (attrs)
38076 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38079 /* Parse a noexcept specification. */
38080 if (keyword == RID_ATOMIC_NOEXCEPT)
38081 noex = boolean_true_node;
38082 else if (keyword == RID_ATOMIC_CANCEL)
38084 /* cancel-and-throw is unimplemented. */
38085 sorry ("atomic_cancel");
38086 noex = NULL_TREE;
38088 else
38089 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38091 /* Keep track if we're in the lexical scope of an outer transaction. */
38092 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38094 stmt = begin_transaction_stmt (token->location, NULL, this_in);
38096 parser->in_transaction = new_in;
38097 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38098 parser->in_transaction = old_in;
38100 finish_transaction_stmt (stmt, NULL, this_in, noex);
38102 return stmt;
38105 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38107 transaction-expression:
38108 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38109 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38112 static tree
38113 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38115 unsigned char old_in = parser->in_transaction;
38116 unsigned char this_in = 1;
38117 cp_token *token;
38118 tree expr, noex;
38119 bool noex_expr;
38120 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38122 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38123 || keyword == RID_TRANSACTION_RELAXED);
38125 if (!flag_tm)
38126 error_at (loc,
38127 keyword == RID_TRANSACTION_RELAXED
38128 ? G_("%<__transaction_relaxed%> without transactional memory "
38129 "support enabled")
38130 : G_("%<__transaction_atomic%> without transactional memory "
38131 "support enabled"));
38133 token = cp_parser_require_keyword (parser, keyword,
38134 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38135 : RT_TRANSACTION_RELAXED));
38136 gcc_assert (token != NULL);
38138 if (keyword == RID_TRANSACTION_RELAXED)
38139 this_in |= TM_STMT_ATTR_RELAXED;
38141 /* Set this early. This might mean that we allow transaction_cancel in
38142 an expression that we find out later actually has to be a constexpr.
38143 However, we expect that cxx_constant_value will be able to deal with
38144 this; also, if the noexcept has no constexpr, then what we parse next
38145 really is a transaction's body. */
38146 parser->in_transaction = this_in;
38148 /* Parse a noexcept specification. */
38149 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38150 true);
38152 if (!noex || !noex_expr
38153 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38155 matching_parens parens;
38156 parens.require_open (parser);
38158 expr = cp_parser_expression (parser);
38159 expr = finish_parenthesized_expr (expr);
38161 parens.require_close (parser);
38163 else
38165 /* The only expression that is available got parsed for the noexcept
38166 already. noexcept is true then. */
38167 expr = noex;
38168 noex = boolean_true_node;
38171 expr = build_transaction_expr (token->location, expr, this_in, noex);
38172 parser->in_transaction = old_in;
38174 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38175 return error_mark_node;
38177 return (flag_tm ? expr : error_mark_node);
38180 /* Parse a function-transaction-block.
38182 function-transaction-block:
38183 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38184 function-body
38185 __transaction_atomic txn-attribute[opt] function-try-block
38186 __transaction_relaxed ctor-initializer[opt] function-body
38187 __transaction_relaxed function-try-block
38190 static void
38191 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38193 unsigned char old_in = parser->in_transaction;
38194 unsigned char new_in = 1;
38195 tree compound_stmt, stmt, attrs;
38196 cp_token *token;
38198 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38199 || keyword == RID_TRANSACTION_RELAXED);
38200 token = cp_parser_require_keyword (parser, keyword,
38201 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38202 : RT_TRANSACTION_RELAXED));
38203 gcc_assert (token != NULL);
38205 if (keyword == RID_TRANSACTION_RELAXED)
38206 new_in |= TM_STMT_ATTR_RELAXED;
38207 else
38209 attrs = cp_parser_txn_attribute_opt (parser);
38210 if (attrs)
38211 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38214 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38216 parser->in_transaction = new_in;
38218 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38219 cp_parser_function_try_block (parser);
38220 else
38221 cp_parser_ctor_initializer_opt_and_function_body
38222 (parser, /*in_function_try_block=*/false);
38224 parser->in_transaction = old_in;
38226 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38229 /* Parse a __transaction_cancel statement.
38231 cancel-statement:
38232 __transaction_cancel txn-attribute[opt] ;
38233 __transaction_cancel txn-attribute[opt] throw-expression ;
38235 ??? Cancel and throw is not yet implemented. */
38237 static tree
38238 cp_parser_transaction_cancel (cp_parser *parser)
38240 cp_token *token;
38241 bool is_outer = false;
38242 tree stmt, attrs;
38244 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38245 RT_TRANSACTION_CANCEL);
38246 gcc_assert (token != NULL);
38248 attrs = cp_parser_txn_attribute_opt (parser);
38249 if (attrs)
38250 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38252 /* ??? Parse cancel-and-throw here. */
38254 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38256 if (!flag_tm)
38258 error_at (token->location, "%<__transaction_cancel%> without "
38259 "transactional memory support enabled");
38260 return error_mark_node;
38262 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38264 error_at (token->location, "%<__transaction_cancel%> within a "
38265 "%<__transaction_relaxed%>");
38266 return error_mark_node;
38268 else if (is_outer)
38270 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38271 && !is_tm_may_cancel_outer (current_function_decl))
38273 error_at (token->location, "outer %<__transaction_cancel%> not "
38274 "within outer %<__transaction_atomic%>");
38275 error_at (token->location,
38276 " or a %<transaction_may_cancel_outer%> function");
38277 return error_mark_node;
38280 else if (parser->in_transaction == 0)
38282 error_at (token->location, "%<__transaction_cancel%> not within "
38283 "%<__transaction_atomic%>");
38284 return error_mark_node;
38287 stmt = build_tm_abort_call (token->location, is_outer);
38288 add_stmt (stmt);
38290 return stmt;
38293 /* The parser. */
38295 static GTY (()) cp_parser *the_parser;
38298 /* Special handling for the first token or line in the file. The first
38299 thing in the file might be #pragma GCC pch_preprocess, which loads a
38300 PCH file, which is a GC collection point. So we need to handle this
38301 first pragma without benefit of an existing lexer structure.
38303 Always returns one token to the caller in *FIRST_TOKEN. This is
38304 either the true first token of the file, or the first token after
38305 the initial pragma. */
38307 static void
38308 cp_parser_initial_pragma (cp_token *first_token)
38310 tree name = NULL;
38312 cp_lexer_get_preprocessor_token (NULL, first_token);
38313 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38314 return;
38316 cp_lexer_get_preprocessor_token (NULL, first_token);
38317 if (first_token->type == CPP_STRING)
38319 name = first_token->u.value;
38321 cp_lexer_get_preprocessor_token (NULL, first_token);
38322 if (first_token->type != CPP_PRAGMA_EOL)
38323 error_at (first_token->location,
38324 "junk at end of %<#pragma GCC pch_preprocess%>");
38326 else
38327 error_at (first_token->location, "expected string literal");
38329 /* Skip to the end of the pragma. */
38330 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38331 cp_lexer_get_preprocessor_token (NULL, first_token);
38333 /* Now actually load the PCH file. */
38334 if (name)
38335 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38337 /* Read one more token to return to our caller. We have to do this
38338 after reading the PCH file in, since its pointers have to be
38339 live. */
38340 cp_lexer_get_preprocessor_token (NULL, first_token);
38343 /* Normal parsing of a pragma token. Here we can (and must) use the
38344 regular lexer. */
38346 static bool
38347 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38349 cp_token *pragma_tok;
38350 unsigned int id;
38351 tree stmt;
38352 bool ret;
38354 pragma_tok = cp_lexer_consume_token (parser->lexer);
38355 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38356 parser->lexer->in_pragma = true;
38358 id = cp_parser_pragma_kind (pragma_tok);
38359 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38360 cp_ensure_no_omp_declare_simd (parser);
38361 switch (id)
38363 case PRAGMA_GCC_PCH_PREPROCESS:
38364 error_at (pragma_tok->location,
38365 "%<#pragma GCC pch_preprocess%> must be first");
38366 break;
38368 case PRAGMA_OMP_BARRIER:
38369 switch (context)
38371 case pragma_compound:
38372 cp_parser_omp_barrier (parser, pragma_tok);
38373 return false;
38374 case pragma_stmt:
38375 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38376 "used in compound statements", "omp barrier");
38377 break;
38378 default:
38379 goto bad_stmt;
38381 break;
38383 case PRAGMA_OMP_FLUSH:
38384 switch (context)
38386 case pragma_compound:
38387 cp_parser_omp_flush (parser, pragma_tok);
38388 return false;
38389 case pragma_stmt:
38390 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38391 "used in compound statements", "omp flush");
38392 break;
38393 default:
38394 goto bad_stmt;
38396 break;
38398 case PRAGMA_OMP_TASKWAIT:
38399 switch (context)
38401 case pragma_compound:
38402 cp_parser_omp_taskwait (parser, pragma_tok);
38403 return false;
38404 case pragma_stmt:
38405 error_at (pragma_tok->location,
38406 "%<#pragma %s%> may only be used in compound statements",
38407 "omp taskwait");
38408 break;
38409 default:
38410 goto bad_stmt;
38412 break;
38414 case PRAGMA_OMP_TASKYIELD:
38415 switch (context)
38417 case pragma_compound:
38418 cp_parser_omp_taskyield (parser, pragma_tok);
38419 return false;
38420 case pragma_stmt:
38421 error_at (pragma_tok->location,
38422 "%<#pragma %s%> may only be used in compound statements",
38423 "omp taskyield");
38424 break;
38425 default:
38426 goto bad_stmt;
38428 break;
38430 case PRAGMA_OMP_CANCEL:
38431 switch (context)
38433 case pragma_compound:
38434 cp_parser_omp_cancel (parser, pragma_tok);
38435 return false;
38436 case pragma_stmt:
38437 error_at (pragma_tok->location,
38438 "%<#pragma %s%> may only be used in compound statements",
38439 "omp cancel");
38440 break;
38441 default:
38442 goto bad_stmt;
38444 break;
38446 case PRAGMA_OMP_CANCELLATION_POINT:
38447 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38448 return false;
38450 case PRAGMA_OMP_THREADPRIVATE:
38451 cp_parser_omp_threadprivate (parser, pragma_tok);
38452 return false;
38454 case PRAGMA_OMP_DECLARE:
38455 return cp_parser_omp_declare (parser, pragma_tok, context);
38457 case PRAGMA_OACC_DECLARE:
38458 cp_parser_oacc_declare (parser, pragma_tok);
38459 return false;
38461 case PRAGMA_OACC_ENTER_DATA:
38462 if (context == pragma_stmt)
38464 error_at (pragma_tok->location,
38465 "%<#pragma %s%> may only be used in compound statements",
38466 "acc enter data");
38467 break;
38469 else if (context != pragma_compound)
38470 goto bad_stmt;
38471 cp_parser_omp_construct (parser, pragma_tok, if_p);
38472 return true;
38474 case PRAGMA_OACC_EXIT_DATA:
38475 if (context == pragma_stmt)
38477 error_at (pragma_tok->location,
38478 "%<#pragma %s%> may only be used in compound statements",
38479 "acc exit data");
38480 break;
38482 else if (context != pragma_compound)
38483 goto bad_stmt;
38484 cp_parser_omp_construct (parser, pragma_tok, if_p);
38485 return true;
38487 case PRAGMA_OACC_ROUTINE:
38488 if (context != pragma_external)
38490 error_at (pragma_tok->location,
38491 "%<#pragma acc routine%> must be at file scope");
38492 break;
38494 cp_parser_oacc_routine (parser, pragma_tok, context);
38495 return false;
38497 case PRAGMA_OACC_UPDATE:
38498 if (context == pragma_stmt)
38500 error_at (pragma_tok->location,
38501 "%<#pragma %s%> may only be used in compound statements",
38502 "acc update");
38503 break;
38505 else if (context != pragma_compound)
38506 goto bad_stmt;
38507 cp_parser_omp_construct (parser, pragma_tok, if_p);
38508 return true;
38510 case PRAGMA_OACC_WAIT:
38511 if (context == pragma_stmt)
38513 error_at (pragma_tok->location,
38514 "%<#pragma %s%> may only be used in compound statements",
38515 "acc wait");
38516 break;
38518 else if (context != pragma_compound)
38519 goto bad_stmt;
38520 cp_parser_omp_construct (parser, pragma_tok, if_p);
38521 return true;
38523 case PRAGMA_OACC_ATOMIC:
38524 case PRAGMA_OACC_CACHE:
38525 case PRAGMA_OACC_DATA:
38526 case PRAGMA_OACC_HOST_DATA:
38527 case PRAGMA_OACC_KERNELS:
38528 case PRAGMA_OACC_PARALLEL:
38529 case PRAGMA_OACC_LOOP:
38530 case PRAGMA_OMP_ATOMIC:
38531 case PRAGMA_OMP_CRITICAL:
38532 case PRAGMA_OMP_DISTRIBUTE:
38533 case PRAGMA_OMP_FOR:
38534 case PRAGMA_OMP_MASTER:
38535 case PRAGMA_OMP_PARALLEL:
38536 case PRAGMA_OMP_SECTIONS:
38537 case PRAGMA_OMP_SIMD:
38538 case PRAGMA_OMP_SINGLE:
38539 case PRAGMA_OMP_TASK:
38540 case PRAGMA_OMP_TASKGROUP:
38541 case PRAGMA_OMP_TASKLOOP:
38542 case PRAGMA_OMP_TEAMS:
38543 if (context != pragma_stmt && context != pragma_compound)
38544 goto bad_stmt;
38545 stmt = push_omp_privatization_clauses (false);
38546 cp_parser_omp_construct (parser, pragma_tok, if_p);
38547 pop_omp_privatization_clauses (stmt);
38548 return true;
38550 case PRAGMA_OMP_ORDERED:
38551 if (context != pragma_stmt && context != pragma_compound)
38552 goto bad_stmt;
38553 stmt = push_omp_privatization_clauses (false);
38554 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38555 pop_omp_privatization_clauses (stmt);
38556 return ret;
38558 case PRAGMA_OMP_TARGET:
38559 if (context != pragma_stmt && context != pragma_compound)
38560 goto bad_stmt;
38561 stmt = push_omp_privatization_clauses (false);
38562 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38563 pop_omp_privatization_clauses (stmt);
38564 return ret;
38566 case PRAGMA_OMP_END_DECLARE_TARGET:
38567 cp_parser_omp_end_declare_target (parser, pragma_tok);
38568 return false;
38570 case PRAGMA_OMP_SECTION:
38571 error_at (pragma_tok->location,
38572 "%<#pragma omp section%> may only be used in "
38573 "%<#pragma omp sections%> construct");
38574 break;
38576 case PRAGMA_IVDEP:
38578 if (context == pragma_external)
38580 error_at (pragma_tok->location,
38581 "%<#pragma GCC ivdep%> must be inside a function");
38582 break;
38584 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38585 cp_token *tok;
38586 tok = cp_lexer_peek_token (the_parser->lexer);
38587 if (tok->type != CPP_KEYWORD
38588 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
38589 && tok->keyword != RID_DO))
38591 cp_parser_error (parser, "for, while or do statement expected");
38592 return false;
38594 cp_parser_iteration_statement (parser, if_p, true);
38595 return true;
38598 default:
38599 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
38600 c_invoke_pragma_handler (id);
38601 break;
38603 bad_stmt:
38604 cp_parser_error (parser, "expected declaration specifiers");
38605 break;
38608 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38609 return false;
38612 /* The interface the pragma parsers have to the lexer. */
38614 enum cpp_ttype
38615 pragma_lex (tree *value, location_t *loc)
38617 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38618 enum cpp_ttype ret = tok->type;
38620 *value = tok->u.value;
38621 if (loc)
38622 *loc = tok->location;
38624 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
38625 ret = CPP_EOF;
38626 else if (ret == CPP_STRING)
38627 *value = cp_parser_string_literal (the_parser, false, false);
38628 else
38630 if (ret == CPP_KEYWORD)
38631 ret = CPP_NAME;
38632 cp_lexer_consume_token (the_parser->lexer);
38635 return ret;
38639 /* External interface. */
38641 /* Parse one entire translation unit. */
38643 void
38644 c_parse_file (void)
38646 static bool already_called = false;
38648 if (already_called)
38649 fatal_error (input_location,
38650 "inter-module optimizations not implemented for C++");
38651 already_called = true;
38653 the_parser = cp_parser_new ();
38654 push_deferring_access_checks (flag_access_control
38655 ? dk_no_deferred : dk_no_check);
38656 cp_parser_translation_unit (the_parser);
38657 the_parser = NULL;
38660 /* Create an identifier for a generic parameter type (a synthesized
38661 template parameter implied by `auto' or a concept identifier). */
38663 static GTY(()) int generic_parm_count;
38664 static tree
38665 make_generic_type_name ()
38667 char buf[32];
38668 sprintf (buf, "auto:%d", ++generic_parm_count);
38669 return get_identifier (buf);
38672 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
38673 (creating a new template parameter list if necessary). Returns the newly
38674 created template type parm. */
38676 static tree
38677 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
38679 gcc_assert (current_binding_level->kind == sk_function_parms);
38681 /* Before committing to modifying any scope, if we're in an
38682 implicit template scope, and we're trying to synthesize a
38683 constrained parameter, try to find a previous parameter with
38684 the same name. This is the same-type rule for abbreviated
38685 function templates.
38687 NOTE: We can generate implicit parameters when tentatively
38688 parsing a nested name specifier, only to reject that parse
38689 later. However, matching the same template-id as part of a
38690 direct-declarator should generate an identical template
38691 parameter, so this rule will merge them. */
38692 if (parser->implicit_template_scope && constr)
38694 tree t = parser->implicit_template_parms;
38695 while (t)
38697 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
38699 tree d = TREE_VALUE (t);
38700 if (TREE_CODE (d) == PARM_DECL)
38701 /* Return the TEMPLATE_PARM_INDEX. */
38702 d = DECL_INITIAL (d);
38703 return d;
38705 t = TREE_CHAIN (t);
38709 /* We are either continuing a function template that already contains implicit
38710 template parameters, creating a new fully-implicit function template, or
38711 extending an existing explicit function template with implicit template
38712 parameters. */
38714 cp_binding_level *const entry_scope = current_binding_level;
38716 bool become_template = false;
38717 cp_binding_level *parent_scope = 0;
38719 if (parser->implicit_template_scope)
38721 gcc_assert (parser->implicit_template_parms);
38723 current_binding_level = parser->implicit_template_scope;
38725 else
38727 /* Roll back to the existing template parameter scope (in the case of
38728 extending an explicit function template) or introduce a new template
38729 parameter scope ahead of the function parameter scope (or class scope
38730 in the case of out-of-line member definitions). The function scope is
38731 added back after template parameter synthesis below. */
38733 cp_binding_level *scope = entry_scope;
38735 while (scope->kind == sk_function_parms)
38737 parent_scope = scope;
38738 scope = scope->level_chain;
38740 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
38742 /* If not defining a class, then any class scope is a scope level in
38743 an out-of-line member definition. In this case simply wind back
38744 beyond the first such scope to inject the template parameter list.
38745 Otherwise wind back to the class being defined. The latter can
38746 occur in class member friend declarations such as:
38748 class A {
38749 void foo (auto);
38751 class B {
38752 friend void A::foo (auto);
38755 The template parameter list synthesized for the friend declaration
38756 must be injected in the scope of 'B'. This can also occur in
38757 erroneous cases such as:
38759 struct A {
38760 struct B {
38761 void foo (auto);
38763 void B::foo (auto) {}
38766 Here the attempted definition of 'B::foo' within 'A' is ill-formed
38767 but, nevertheless, the template parameter list synthesized for the
38768 declarator should be injected into the scope of 'A' as if the
38769 ill-formed template was specified explicitly. */
38771 while (scope->kind == sk_class && !scope->defining_class_p)
38773 parent_scope = scope;
38774 scope = scope->level_chain;
38778 current_binding_level = scope;
38780 if (scope->kind != sk_template_parms
38781 || !function_being_declared_is_template_p (parser))
38783 /* Introduce a new template parameter list for implicit template
38784 parameters. */
38786 become_template = true;
38788 parser->implicit_template_scope
38789 = begin_scope (sk_template_parms, NULL);
38791 ++processing_template_decl;
38793 parser->fully_implicit_function_template_p = true;
38794 ++parser->num_template_parameter_lists;
38796 else
38798 /* Synthesize implicit template parameters at the end of the explicit
38799 template parameter list. */
38801 gcc_assert (current_template_parms);
38803 parser->implicit_template_scope = scope;
38805 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38806 parser->implicit_template_parms
38807 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
38811 /* Synthesize a new template parameter and track the current template
38812 parameter chain with implicit_template_parms. */
38814 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
38815 tree synth_id = make_generic_type_name ();
38816 tree synth_tmpl_parm;
38817 bool non_type = false;
38819 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
38820 synth_tmpl_parm
38821 = finish_template_type_parm (class_type_node, synth_id);
38822 else if (TREE_CODE (proto) == TEMPLATE_DECL)
38823 synth_tmpl_parm
38824 = finish_constrained_template_template_parm (proto, synth_id);
38825 else
38827 synth_tmpl_parm = copy_decl (proto);
38828 DECL_NAME (synth_tmpl_parm) = synth_id;
38829 non_type = true;
38832 // Attach the constraint to the parm before processing.
38833 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
38834 TREE_TYPE (node) = constr;
38835 tree new_parm
38836 = process_template_parm (parser->implicit_template_parms,
38837 input_location,
38838 node,
38839 /*non_type=*/non_type,
38840 /*param_pack=*/false);
38842 // Chain the new parameter to the list of implicit parameters.
38843 if (parser->implicit_template_parms)
38844 parser->implicit_template_parms
38845 = TREE_CHAIN (parser->implicit_template_parms);
38846 else
38847 parser->implicit_template_parms = new_parm;
38849 tree new_decl = get_local_decls ();
38850 if (non_type)
38851 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
38852 new_decl = DECL_INITIAL (new_decl);
38854 /* If creating a fully implicit function template, start the new implicit
38855 template parameter list with this synthesized type, otherwise grow the
38856 current template parameter list. */
38858 if (become_template)
38860 parent_scope->level_chain = current_binding_level;
38862 tree new_parms = make_tree_vec (1);
38863 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
38864 current_template_parms = tree_cons (size_int (processing_template_decl),
38865 new_parms, current_template_parms);
38867 else
38869 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38870 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
38871 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
38872 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
38875 // If the new parameter was constrained, we need to add that to the
38876 // constraints in the template parameter list.
38877 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
38879 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
38880 reqs = conjoin_constraints (reqs, req);
38881 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
38884 current_binding_level = entry_scope;
38886 return new_decl;
38889 /* Finish the declaration of a fully implicit function template. Such a
38890 template has no explicit template parameter list so has not been through the
38891 normal template head and tail processing. synthesize_implicit_template_parm
38892 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
38893 provided if the declaration is a class member such that its template
38894 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
38895 form is returned. Otherwise NULL_TREE is returned. */
38897 static tree
38898 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
38900 gcc_assert (parser->fully_implicit_function_template_p);
38902 if (member_decl_opt && member_decl_opt != error_mark_node
38903 && DECL_VIRTUAL_P (member_decl_opt))
38905 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
38906 "implicit templates may not be %<virtual%>");
38907 DECL_VIRTUAL_P (member_decl_opt) = false;
38910 if (member_decl_opt)
38911 member_decl_opt = finish_member_template_decl (member_decl_opt);
38912 end_template_decl ();
38914 parser->fully_implicit_function_template_p = false;
38915 --parser->num_template_parameter_lists;
38917 return member_decl_opt;
38920 /* Helper function for diagnostics that have complained about things
38921 being used with 'extern "C"' linkage.
38923 Attempt to issue a note showing where the 'extern "C"' linkage began. */
38925 void
38926 maybe_show_extern_c_location (void)
38928 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
38929 inform (the_parser->innermost_linkage_specification_location,
38930 "%<extern \"C\"%> linkage started here");
38933 #include "gt-cp-parser.h"