C++: fix fix-it hints for misspellings within explicit namespaces
[official-gcc.git] / gcc / cp / parser.c
blob4ab0b6974d624a791c69ad33671ee5f5a0791612
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 #include "system.h"
23 #include "coretypes.h"
24 #include "cp-tree.h"
25 #include "c-family/c-common.h"
26 #include "timevar.h"
27 #include "stringpool.h"
28 #include "cgraph.h"
29 #include "print-tree.h"
30 #include "attribs.h"
31 #include "trans-mem.h"
32 #include "intl.h"
33 #include "decl.h"
34 #include "c-family/c-objc.h"
35 #include "plugin.h"
36 #include "tree-pretty-print.h"
37 #include "parser.h"
38 #include "gomp-constants.h"
39 #include "omp-general.h"
40 #include "omp-offload.h"
41 #include "c-family/c-indentation.h"
42 #include "context.h"
43 #include "cp-cilkplus.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
48 /* The lexer. */
50 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
51 and c-lex.c) and the C++ parser. */
53 static cp_token eof_token =
55 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
58 /* The various kinds of non integral constant we encounter. */
59 enum non_integral_constant {
60 NIC_NONE,
61 /* floating-point literal */
62 NIC_FLOAT,
63 /* %<this%> */
64 NIC_THIS,
65 /* %<__FUNCTION__%> */
66 NIC_FUNC_NAME,
67 /* %<__PRETTY_FUNCTION__%> */
68 NIC_PRETTY_FUNC,
69 /* %<__func__%> */
70 NIC_C99_FUNC,
71 /* "%<va_arg%> */
72 NIC_VA_ARG,
73 /* a cast */
74 NIC_CAST,
75 /* %<typeid%> operator */
76 NIC_TYPEID,
77 /* non-constant compound literals */
78 NIC_NCC,
79 /* a function call */
80 NIC_FUNC_CALL,
81 /* an increment */
82 NIC_INC,
83 /* an decrement */
84 NIC_DEC,
85 /* an array reference */
86 NIC_ARRAY_REF,
87 /* %<->%> */
88 NIC_ARROW,
89 /* %<.%> */
90 NIC_POINT,
91 /* the address of a label */
92 NIC_ADDR_LABEL,
93 /* %<*%> */
94 NIC_STAR,
95 /* %<&%> */
96 NIC_ADDR,
97 /* %<++%> */
98 NIC_PREINCREMENT,
99 /* %<--%> */
100 NIC_PREDECREMENT,
101 /* %<new%> */
102 NIC_NEW,
103 /* %<delete%> */
104 NIC_DEL,
105 /* calls to overloaded operators */
106 NIC_OVERLOADED,
107 /* an assignment */
108 NIC_ASSIGNMENT,
109 /* a comma operator */
110 NIC_COMMA,
111 /* a call to a constructor */
112 NIC_CONSTRUCTOR,
113 /* a transaction expression */
114 NIC_TRANSACTION
117 /* The various kinds of errors about name-lookup failing. */
118 enum name_lookup_error {
119 /* NULL */
120 NLE_NULL,
121 /* is not a type */
122 NLE_TYPE,
123 /* is not a class or namespace */
124 NLE_CXX98,
125 /* is not a class, namespace, or enumeration */
126 NLE_NOT_CXX98
129 /* The various kinds of required token */
130 enum required_token {
131 RT_NONE,
132 RT_SEMICOLON, /* ';' */
133 RT_OPEN_PAREN, /* '(' */
134 RT_CLOSE_BRACE, /* '}' */
135 RT_OPEN_BRACE, /* '{' */
136 RT_CLOSE_SQUARE, /* ']' */
137 RT_OPEN_SQUARE, /* '[' */
138 RT_COMMA, /* ',' */
139 RT_SCOPE, /* '::' */
140 RT_LESS, /* '<' */
141 RT_GREATER, /* '>' */
142 RT_EQ, /* '=' */
143 RT_ELLIPSIS, /* '...' */
144 RT_MULT, /* '*' */
145 RT_COMPL, /* '~' */
146 RT_COLON, /* ':' */
147 RT_COLON_SCOPE, /* ':' or '::' */
148 RT_CLOSE_PAREN, /* ')' */
149 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
150 RT_PRAGMA_EOL, /* end of line */
151 RT_NAME, /* identifier */
153 /* The type is CPP_KEYWORD */
154 RT_NEW, /* new */
155 RT_DELETE, /* delete */
156 RT_RETURN, /* return */
157 RT_WHILE, /* while */
158 RT_EXTERN, /* extern */
159 RT_STATIC_ASSERT, /* static_assert */
160 RT_DECLTYPE, /* decltype */
161 RT_OPERATOR, /* operator */
162 RT_CLASS, /* class */
163 RT_TEMPLATE, /* template */
164 RT_NAMESPACE, /* namespace */
165 RT_USING, /* using */
166 RT_ASM, /* asm */
167 RT_TRY, /* try */
168 RT_CATCH, /* catch */
169 RT_THROW, /* throw */
170 RT_LABEL, /* __label__ */
171 RT_AT_TRY, /* @try */
172 RT_AT_SYNCHRONIZED, /* @synchronized */
173 RT_AT_THROW, /* @throw */
175 RT_SELECT, /* selection-statement */
176 RT_INTERATION, /* iteration-statement */
177 RT_JUMP, /* jump-statement */
178 RT_CLASS_KEY, /* class-key */
179 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
180 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
181 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
182 RT_TRANSACTION_CANCEL /* __transaction_cancel */
185 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
186 reverting it on destruction. */
188 class type_id_in_expr_sentinel
190 cp_parser *parser;
191 bool saved;
192 public:
193 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
194 : parser (parser),
195 saved (parser->in_type_id_in_expr_p)
196 { parser->in_type_id_in_expr_p = set; }
197 ~type_id_in_expr_sentinel ()
198 { parser->in_type_id_in_expr_p = saved; }
201 /* Prototypes. */
203 static cp_lexer *cp_lexer_new_main
204 (void);
205 static cp_lexer *cp_lexer_new_from_tokens
206 (cp_token_cache *tokens);
207 static void cp_lexer_destroy
208 (cp_lexer *);
209 static int cp_lexer_saving_tokens
210 (const cp_lexer *);
211 static cp_token *cp_lexer_token_at
212 (cp_lexer *, cp_token_position);
213 static void cp_lexer_get_preprocessor_token
214 (cp_lexer *, cp_token *);
215 static inline cp_token *cp_lexer_peek_token
216 (cp_lexer *);
217 static cp_token *cp_lexer_peek_nth_token
218 (cp_lexer *, size_t);
219 static inline bool cp_lexer_next_token_is
220 (cp_lexer *, enum cpp_ttype);
221 static bool cp_lexer_next_token_is_not
222 (cp_lexer *, enum cpp_ttype);
223 static bool cp_lexer_next_token_is_keyword
224 (cp_lexer *, enum rid);
225 static cp_token *cp_lexer_consume_token
226 (cp_lexer *);
227 static void cp_lexer_purge_token
228 (cp_lexer *);
229 static void cp_lexer_purge_tokens_after
230 (cp_lexer *, cp_token_position);
231 static void cp_lexer_save_tokens
232 (cp_lexer *);
233 static void cp_lexer_commit_tokens
234 (cp_lexer *);
235 static void cp_lexer_rollback_tokens
236 (cp_lexer *);
237 static void cp_lexer_print_token
238 (FILE *, cp_token *);
239 static inline bool cp_lexer_debugging_p
240 (cp_lexer *);
241 static void cp_lexer_start_debugging
242 (cp_lexer *) ATTRIBUTE_UNUSED;
243 static void cp_lexer_stop_debugging
244 (cp_lexer *) ATTRIBUTE_UNUSED;
246 static cp_token_cache *cp_token_cache_new
247 (cp_token *, cp_token *);
249 static void cp_parser_initial_pragma
250 (cp_token *);
252 static tree cp_literal_operator_id
253 (const char *);
255 static void cp_parser_cilk_simd
256 (cp_parser *, cp_token *, bool *);
257 static tree cp_parser_cilk_for
258 (cp_parser *, tree, bool *);
259 static bool cp_parser_omp_declare_reduction_exprs
260 (tree, cp_parser *);
261 static tree cp_parser_cilk_simd_vectorlength
262 (cp_parser *, tree, bool);
263 static void cp_finalize_oacc_routine
264 (cp_parser *, tree, bool);
266 /* Manifest constants. */
267 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
268 #define CP_SAVED_TOKEN_STACK 5
270 /* Variables. */
272 /* The stream to which debugging output should be written. */
273 static FILE *cp_lexer_debug_stream;
275 /* Nonzero if we are parsing an unevaluated operand: an operand to
276 sizeof, typeof, or alignof. */
277 int cp_unevaluated_operand;
279 /* Dump up to NUM tokens in BUFFER to FILE starting with token
280 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
281 first token in BUFFER. If NUM is 0, dump all the tokens. If
282 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
283 highlighted by surrounding it in [[ ]]. */
285 static void
286 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
287 cp_token *start_token, unsigned num,
288 cp_token *curr_token)
290 unsigned i, nprinted;
291 cp_token *token;
292 bool do_print;
294 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
296 if (buffer == NULL)
297 return;
299 if (num == 0)
300 num = buffer->length ();
302 if (start_token == NULL)
303 start_token = buffer->address ();
305 if (start_token > buffer->address ())
307 cp_lexer_print_token (file, &(*buffer)[0]);
308 fprintf (file, " ... ");
311 do_print = false;
312 nprinted = 0;
313 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
315 if (token == start_token)
316 do_print = true;
318 if (!do_print)
319 continue;
321 nprinted++;
322 if (token == curr_token)
323 fprintf (file, "[[");
325 cp_lexer_print_token (file, token);
327 if (token == curr_token)
328 fprintf (file, "]]");
330 switch (token->type)
332 case CPP_SEMICOLON:
333 case CPP_OPEN_BRACE:
334 case CPP_CLOSE_BRACE:
335 case CPP_EOF:
336 fputc ('\n', file);
337 break;
339 default:
340 fputc (' ', file);
344 if (i == num && i < buffer->length ())
346 fprintf (file, " ... ");
347 cp_lexer_print_token (file, &buffer->last ());
350 fprintf (file, "\n");
354 /* Dump all tokens in BUFFER to stderr. */
356 void
357 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
359 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
362 DEBUG_FUNCTION void
363 debug (vec<cp_token, va_gc> &ref)
365 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
368 DEBUG_FUNCTION void
369 debug (vec<cp_token, va_gc> *ptr)
371 if (ptr)
372 debug (*ptr);
373 else
374 fprintf (stderr, "<nil>\n");
378 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
379 description for T. */
381 static void
382 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
384 if (t)
386 fprintf (file, "%s: ", desc);
387 print_node_brief (file, "", t, 0);
392 /* Dump parser context C to FILE. */
394 static void
395 cp_debug_print_context (FILE *file, cp_parser_context *c)
397 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
398 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
399 print_node_brief (file, "", c->object_type, 0);
400 fprintf (file, "}\n");
404 /* Print the stack of parsing contexts to FILE starting with FIRST. */
406 static void
407 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
409 unsigned i;
410 cp_parser_context *c;
412 fprintf (file, "Parsing context stack:\n");
413 for (i = 0, c = first; c; c = c->next, i++)
415 fprintf (file, "\t#%u: ", i);
416 cp_debug_print_context (file, c);
421 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
423 static void
424 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
426 if (flag)
427 fprintf (file, "%s: true\n", desc);
431 /* Print an unparsed function entry UF to FILE. */
433 static void
434 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
436 unsigned i;
437 cp_default_arg_entry *default_arg_fn;
438 tree fn;
440 fprintf (file, "\tFunctions with default args:\n");
441 for (i = 0;
442 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
443 i++)
445 fprintf (file, "\t\tClass type: ");
446 print_node_brief (file, "", default_arg_fn->class_type, 0);
447 fprintf (file, "\t\tDeclaration: ");
448 print_node_brief (file, "", default_arg_fn->decl, 0);
449 fprintf (file, "\n");
452 fprintf (file, "\n\tFunctions with definitions that require "
453 "post-processing\n\t\t");
454 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
456 print_node_brief (file, "", fn, 0);
457 fprintf (file, " ");
459 fprintf (file, "\n");
461 fprintf (file, "\n\tNon-static data members with initializers that require "
462 "post-processing\n\t\t");
463 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
465 print_node_brief (file, "", fn, 0);
466 fprintf (file, " ");
468 fprintf (file, "\n");
472 /* Print the stack of unparsed member functions S to FILE. */
474 static void
475 cp_debug_print_unparsed_queues (FILE *file,
476 vec<cp_unparsed_functions_entry, va_gc> *s)
478 unsigned i;
479 cp_unparsed_functions_entry *uf;
481 fprintf (file, "Unparsed functions\n");
482 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
484 fprintf (file, "#%u:\n", i);
485 cp_debug_print_unparsed_function (file, uf);
490 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
491 the given PARSER. If FILE is NULL, the output is printed on stderr. */
493 static void
494 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
496 cp_token *next_token, *first_token, *start_token;
498 if (file == NULL)
499 file = stderr;
501 next_token = parser->lexer->next_token;
502 first_token = parser->lexer->buffer->address ();
503 start_token = (next_token > first_token + window_size / 2)
504 ? next_token - window_size / 2
505 : first_token;
506 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
507 next_token);
511 /* Dump debugging information for the given PARSER. If FILE is NULL,
512 the output is printed on stderr. */
514 void
515 cp_debug_parser (FILE *file, cp_parser *parser)
517 const size_t window_size = 20;
518 cp_token *token;
519 expanded_location eloc;
521 if (file == NULL)
522 file = stderr;
524 fprintf (file, "Parser state\n\n");
525 fprintf (file, "Number of tokens: %u\n",
526 vec_safe_length (parser->lexer->buffer));
527 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
528 cp_debug_print_tree_if_set (file, "Object scope",
529 parser->object_scope);
530 cp_debug_print_tree_if_set (file, "Qualifying scope",
531 parser->qualifying_scope);
532 cp_debug_print_context_stack (file, parser->context);
533 cp_debug_print_flag (file, "Allow GNU extensions",
534 parser->allow_gnu_extensions_p);
535 cp_debug_print_flag (file, "'>' token is greater-than",
536 parser->greater_than_is_operator_p);
537 cp_debug_print_flag (file, "Default args allowed in current "
538 "parameter list", parser->default_arg_ok_p);
539 cp_debug_print_flag (file, "Parsing integral constant-expression",
540 parser->integral_constant_expression_p);
541 cp_debug_print_flag (file, "Allow non-constant expression in current "
542 "constant-expression",
543 parser->allow_non_integral_constant_expression_p);
544 cp_debug_print_flag (file, "Seen non-constant expression",
545 parser->non_integral_constant_expression_p);
546 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
547 "current context",
548 parser->local_variables_forbidden_p);
549 cp_debug_print_flag (file, "In unbraced linkage specification",
550 parser->in_unbraced_linkage_specification_p);
551 cp_debug_print_flag (file, "Parsing a declarator",
552 parser->in_declarator_p);
553 cp_debug_print_flag (file, "In template argument list",
554 parser->in_template_argument_list_p);
555 cp_debug_print_flag (file, "Parsing an iteration statement",
556 parser->in_statement & IN_ITERATION_STMT);
557 cp_debug_print_flag (file, "Parsing a switch statement",
558 parser->in_statement & IN_SWITCH_STMT);
559 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
560 parser->in_statement & IN_OMP_BLOCK);
561 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
562 parser->in_statement & IN_CILK_SIMD_FOR);
563 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
564 parser->in_statement & IN_OMP_FOR);
565 cp_debug_print_flag (file, "Parsing an if statement",
566 parser->in_statement & IN_IF_STMT);
567 cp_debug_print_flag (file, "Parsing a type-id in an expression "
568 "context", parser->in_type_id_in_expr_p);
569 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
570 parser->implicit_extern_c);
571 cp_debug_print_flag (file, "String expressions should be translated "
572 "to execution character set",
573 parser->translate_strings_p);
574 cp_debug_print_flag (file, "Parsing function body outside of a "
575 "local class", parser->in_function_body);
576 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
577 parser->colon_corrects_to_scope_p);
578 cp_debug_print_flag (file, "Colon doesn't start a class definition",
579 parser->colon_doesnt_start_class_def_p);
580 if (parser->type_definition_forbidden_message)
581 fprintf (file, "Error message for forbidden type definitions: %s\n",
582 parser->type_definition_forbidden_message);
583 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
584 fprintf (file, "Number of class definitions in progress: %u\n",
585 parser->num_classes_being_defined);
586 fprintf (file, "Number of template parameter lists for the current "
587 "declaration: %u\n", parser->num_template_parameter_lists);
588 cp_debug_parser_tokens (file, parser, window_size);
589 token = parser->lexer->next_token;
590 fprintf (file, "Next token to parse:\n");
591 fprintf (file, "\tToken: ");
592 cp_lexer_print_token (file, token);
593 eloc = expand_location (token->location);
594 fprintf (file, "\n\tFile: %s\n", eloc.file);
595 fprintf (file, "\tLine: %d\n", eloc.line);
596 fprintf (file, "\tColumn: %d\n", eloc.column);
599 DEBUG_FUNCTION void
600 debug (cp_parser &ref)
602 cp_debug_parser (stderr, &ref);
605 DEBUG_FUNCTION void
606 debug (cp_parser *ptr)
608 if (ptr)
609 debug (*ptr);
610 else
611 fprintf (stderr, "<nil>\n");
614 /* Allocate memory for a new lexer object and return it. */
616 static cp_lexer *
617 cp_lexer_alloc (void)
619 cp_lexer *lexer;
621 c_common_no_more_pch ();
623 /* Allocate the memory. */
624 lexer = ggc_cleared_alloc<cp_lexer> ();
626 /* Initially we are not debugging. */
627 lexer->debugging_p = false;
629 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
631 /* Create the buffer. */
632 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
634 return lexer;
638 /* Create a new main C++ lexer, the lexer that gets tokens from the
639 preprocessor. */
641 static cp_lexer *
642 cp_lexer_new_main (void)
644 cp_lexer *lexer;
645 cp_token token;
647 /* It's possible that parsing the first pragma will load a PCH file,
648 which is a GC collection point. So we have to do that before
649 allocating any memory. */
650 cp_parser_initial_pragma (&token);
652 lexer = cp_lexer_alloc ();
654 /* Put the first token in the buffer. */
655 lexer->buffer->quick_push (token);
657 /* Get the remaining tokens from the preprocessor. */
658 while (token.type != CPP_EOF)
660 cp_lexer_get_preprocessor_token (lexer, &token);
661 vec_safe_push (lexer->buffer, token);
664 lexer->last_token = lexer->buffer->address ()
665 + lexer->buffer->length ()
666 - 1;
667 lexer->next_token = lexer->buffer->length ()
668 ? lexer->buffer->address ()
669 : &eof_token;
671 /* Subsequent preprocessor diagnostics should use compiler
672 diagnostic functions to get the compiler source location. */
673 done_lexing = true;
675 gcc_assert (!lexer->next_token->purged_p);
676 return lexer;
679 /* Create a new lexer whose token stream is primed with the tokens in
680 CACHE. When these tokens are exhausted, no new tokens will be read. */
682 static cp_lexer *
683 cp_lexer_new_from_tokens (cp_token_cache *cache)
685 cp_token *first = cache->first;
686 cp_token *last = cache->last;
687 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
689 /* We do not own the buffer. */
690 lexer->buffer = NULL;
691 lexer->next_token = first == last ? &eof_token : first;
692 lexer->last_token = last;
694 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
696 /* Initially we are not debugging. */
697 lexer->debugging_p = false;
699 gcc_assert (!lexer->next_token->purged_p);
700 return lexer;
703 /* Frees all resources associated with LEXER. */
705 static void
706 cp_lexer_destroy (cp_lexer *lexer)
708 vec_free (lexer->buffer);
709 lexer->saved_tokens.release ();
710 ggc_free (lexer);
713 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
714 be used. The point of this flag is to help the compiler to fold away calls
715 to cp_lexer_debugging_p within this source file at compile time, when the
716 lexer is not being debugged. */
718 #define LEXER_DEBUGGING_ENABLED_P false
720 /* Returns nonzero if debugging information should be output. */
722 static inline bool
723 cp_lexer_debugging_p (cp_lexer *lexer)
725 if (!LEXER_DEBUGGING_ENABLED_P)
726 return false;
728 return lexer->debugging_p;
732 static inline cp_token_position
733 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
735 gcc_assert (!previous_p || lexer->next_token != &eof_token);
737 return lexer->next_token - previous_p;
740 static inline cp_token *
741 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
743 return pos;
746 static inline void
747 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
749 lexer->next_token = cp_lexer_token_at (lexer, pos);
752 static inline cp_token_position
753 cp_lexer_previous_token_position (cp_lexer *lexer)
755 if (lexer->next_token == &eof_token)
756 return lexer->last_token - 1;
757 else
758 return cp_lexer_token_position (lexer, true);
761 static inline cp_token *
762 cp_lexer_previous_token (cp_lexer *lexer)
764 cp_token_position tp = cp_lexer_previous_token_position (lexer);
766 /* Skip past purged tokens. */
767 while (tp->purged_p)
769 gcc_assert (tp != vec_safe_address (lexer->buffer));
770 tp--;
773 return cp_lexer_token_at (lexer, tp);
776 /* nonzero if we are presently saving tokens. */
778 static inline int
779 cp_lexer_saving_tokens (const cp_lexer* lexer)
781 return lexer->saved_tokens.length () != 0;
784 /* Store the next token from the preprocessor in *TOKEN. Return true
785 if we reach EOF. If LEXER is NULL, assume we are handling an
786 initial #pragma pch_preprocess, and thus want the lexer to return
787 processed strings. */
789 static void
790 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
792 static int is_extern_c = 0;
794 /* Get a new token from the preprocessor. */
795 token->type
796 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
797 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
798 token->keyword = RID_MAX;
799 token->purged_p = false;
800 token->error_reported = false;
802 /* On some systems, some header files are surrounded by an
803 implicit extern "C" block. Set a flag in the token if it
804 comes from such a header. */
805 is_extern_c += pending_lang_change;
806 pending_lang_change = 0;
807 token->implicit_extern_c = is_extern_c > 0;
809 /* Check to see if this token is a keyword. */
810 if (token->type == CPP_NAME)
812 if (C_IS_RESERVED_WORD (token->u.value))
814 /* Mark this token as a keyword. */
815 token->type = CPP_KEYWORD;
816 /* Record which keyword. */
817 token->keyword = C_RID_CODE (token->u.value);
819 else
821 if (warn_cxx11_compat
822 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
823 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
825 /* Warn about the C++0x keyword (but still treat it as
826 an identifier). */
827 warning (OPT_Wc__11_compat,
828 "identifier %qE is a keyword in C++11",
829 token->u.value);
831 /* Clear out the C_RID_CODE so we don't warn about this
832 particular identifier-turned-keyword again. */
833 C_SET_RID_CODE (token->u.value, RID_MAX);
836 token->keyword = RID_MAX;
839 else if (token->type == CPP_AT_NAME)
841 /* This only happens in Objective-C++; it must be a keyword. */
842 token->type = CPP_KEYWORD;
843 switch (C_RID_CODE (token->u.value))
845 /* Replace 'class' with '@class', 'private' with '@private',
846 etc. This prevents confusion with the C++ keyword
847 'class', and makes the tokens consistent with other
848 Objective-C 'AT' keywords. For example '@class' is
849 reported as RID_AT_CLASS which is consistent with
850 '@synchronized', which is reported as
851 RID_AT_SYNCHRONIZED.
853 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
854 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
855 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
856 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
857 case RID_THROW: token->keyword = RID_AT_THROW; break;
858 case RID_TRY: token->keyword = RID_AT_TRY; break;
859 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
860 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
861 default: token->keyword = C_RID_CODE (token->u.value);
866 /* Update the globals input_location and the input file stack from TOKEN. */
867 static inline void
868 cp_lexer_set_source_position_from_token (cp_token *token)
870 if (token->type != CPP_EOF)
872 input_location = token->location;
876 /* Update the globals input_location and the input file stack from LEXER. */
877 static inline void
878 cp_lexer_set_source_position (cp_lexer *lexer)
880 cp_token *token = cp_lexer_peek_token (lexer);
881 cp_lexer_set_source_position_from_token (token);
884 /* Return a pointer to the next token in the token stream, but do not
885 consume it. */
887 static inline cp_token *
888 cp_lexer_peek_token (cp_lexer *lexer)
890 if (cp_lexer_debugging_p (lexer))
892 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
893 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
894 putc ('\n', cp_lexer_debug_stream);
896 return lexer->next_token;
899 /* Return true if the next token has the indicated TYPE. */
901 static inline bool
902 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
904 return cp_lexer_peek_token (lexer)->type == type;
907 /* Return true if the next token does not have the indicated TYPE. */
909 static inline bool
910 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
912 return !cp_lexer_next_token_is (lexer, type);
915 /* Return true if the next token is the indicated KEYWORD. */
917 static inline bool
918 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
920 return cp_lexer_peek_token (lexer)->keyword == keyword;
923 static inline bool
924 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
926 return cp_lexer_peek_nth_token (lexer, n)->type == type;
929 static inline bool
930 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
932 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
935 /* Return true if the next token is not the indicated KEYWORD. */
937 static inline bool
938 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
940 return cp_lexer_peek_token (lexer)->keyword != keyword;
943 /* Return true if KEYWORD can start a decl-specifier. */
945 bool
946 cp_keyword_starts_decl_specifier_p (enum rid keyword)
948 switch (keyword)
950 /* auto specifier: storage-class-specifier in C++,
951 simple-type-specifier in C++0x. */
952 case RID_AUTO:
953 /* Storage classes. */
954 case RID_REGISTER:
955 case RID_STATIC:
956 case RID_EXTERN:
957 case RID_MUTABLE:
958 case RID_THREAD:
959 /* Elaborated type specifiers. */
960 case RID_ENUM:
961 case RID_CLASS:
962 case RID_STRUCT:
963 case RID_UNION:
964 case RID_TYPENAME:
965 /* Simple type specifiers. */
966 case RID_CHAR:
967 case RID_CHAR16:
968 case RID_CHAR32:
969 case RID_WCHAR:
970 case RID_BOOL:
971 case RID_SHORT:
972 case RID_INT:
973 case RID_LONG:
974 case RID_SIGNED:
975 case RID_UNSIGNED:
976 case RID_FLOAT:
977 case RID_DOUBLE:
978 case RID_VOID:
979 /* GNU extensions. */
980 case RID_ATTRIBUTE:
981 case RID_TYPEOF:
982 /* C++0x extensions. */
983 case RID_DECLTYPE:
984 case RID_UNDERLYING_TYPE:
985 case RID_CONSTEXPR:
986 return true;
988 default:
989 if (keyword >= RID_FIRST_INT_N
990 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
991 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
992 return true;
993 return false;
997 /* Return true if the next token is a keyword for a decl-specifier. */
999 static bool
1000 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
1002 cp_token *token;
1004 token = cp_lexer_peek_token (lexer);
1005 return cp_keyword_starts_decl_specifier_p (token->keyword);
1008 /* Returns TRUE iff the token T begins a decltype type. */
1010 static bool
1011 token_is_decltype (cp_token *t)
1013 return (t->keyword == RID_DECLTYPE
1014 || t->type == CPP_DECLTYPE);
1017 /* Returns TRUE iff the next token begins a decltype type. */
1019 static bool
1020 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1022 cp_token *t = cp_lexer_peek_token (lexer);
1023 return token_is_decltype (t);
1026 /* Called when processing a token with tree_check_value; perform or defer the
1027 associated checks and return the value. */
1029 static tree
1030 saved_checks_value (struct tree_check *check_value)
1032 /* Perform any access checks that were deferred. */
1033 vec<deferred_access_check, va_gc> *checks;
1034 deferred_access_check *chk;
1035 checks = check_value->checks;
1036 if (checks)
1038 int i;
1039 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1040 perform_or_defer_access_check (chk->binfo,
1041 chk->decl,
1042 chk->diag_decl, tf_warning_or_error);
1044 /* Return the stored value. */
1045 return check_value->value;
1048 /* Return a pointer to the Nth token in the token stream. If N is 1,
1049 then this is precisely equivalent to cp_lexer_peek_token (except
1050 that it is not inline). One would like to disallow that case, but
1051 there is one case (cp_parser_nth_token_starts_template_id) where
1052 the caller passes a variable for N and it might be 1. */
1054 static cp_token *
1055 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1057 cp_token *token;
1059 /* N is 1-based, not zero-based. */
1060 gcc_assert (n > 0);
1062 if (cp_lexer_debugging_p (lexer))
1063 fprintf (cp_lexer_debug_stream,
1064 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1066 --n;
1067 token = lexer->next_token;
1068 gcc_assert (!n || token != &eof_token);
1069 while (n != 0)
1071 ++token;
1072 if (token == lexer->last_token)
1074 token = &eof_token;
1075 break;
1078 if (!token->purged_p)
1079 --n;
1082 if (cp_lexer_debugging_p (lexer))
1084 cp_lexer_print_token (cp_lexer_debug_stream, token);
1085 putc ('\n', cp_lexer_debug_stream);
1088 return token;
1091 /* Return the next token, and advance the lexer's next_token pointer
1092 to point to the next non-purged token. */
1094 static cp_token *
1095 cp_lexer_consume_token (cp_lexer* lexer)
1097 cp_token *token = lexer->next_token;
1099 gcc_assert (token != &eof_token);
1100 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1104 lexer->next_token++;
1105 if (lexer->next_token == lexer->last_token)
1107 lexer->next_token = &eof_token;
1108 break;
1112 while (lexer->next_token->purged_p);
1114 cp_lexer_set_source_position_from_token (token);
1116 /* Provide debugging output. */
1117 if (cp_lexer_debugging_p (lexer))
1119 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1120 cp_lexer_print_token (cp_lexer_debug_stream, token);
1121 putc ('\n', cp_lexer_debug_stream);
1124 return token;
1127 /* Permanently remove the next token from the token stream, and
1128 advance the next_token pointer to refer to the next non-purged
1129 token. */
1131 static void
1132 cp_lexer_purge_token (cp_lexer *lexer)
1134 cp_token *tok = lexer->next_token;
1136 gcc_assert (tok != &eof_token);
1137 tok->purged_p = true;
1138 tok->location = UNKNOWN_LOCATION;
1139 tok->u.value = NULL_TREE;
1140 tok->keyword = RID_MAX;
1144 tok++;
1145 if (tok == lexer->last_token)
1147 tok = &eof_token;
1148 break;
1151 while (tok->purged_p);
1152 lexer->next_token = tok;
1155 /* Permanently remove all tokens after TOK, up to, but not
1156 including, the token that will be returned next by
1157 cp_lexer_peek_token. */
1159 static void
1160 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1162 cp_token *peek = lexer->next_token;
1164 if (peek == &eof_token)
1165 peek = lexer->last_token;
1167 gcc_assert (tok < peek);
1169 for ( tok += 1; tok != peek; tok += 1)
1171 tok->purged_p = true;
1172 tok->location = UNKNOWN_LOCATION;
1173 tok->u.value = NULL_TREE;
1174 tok->keyword = RID_MAX;
1178 /* Begin saving tokens. All tokens consumed after this point will be
1179 preserved. */
1181 static void
1182 cp_lexer_save_tokens (cp_lexer* lexer)
1184 /* Provide debugging output. */
1185 if (cp_lexer_debugging_p (lexer))
1186 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1188 lexer->saved_tokens.safe_push (lexer->next_token);
1191 /* Commit to the portion of the token stream most recently saved. */
1193 static void
1194 cp_lexer_commit_tokens (cp_lexer* lexer)
1196 /* Provide debugging output. */
1197 if (cp_lexer_debugging_p (lexer))
1198 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1200 lexer->saved_tokens.pop ();
1203 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1204 to the token stream. Stop saving tokens. */
1206 static void
1207 cp_lexer_rollback_tokens (cp_lexer* lexer)
1209 /* Provide debugging output. */
1210 if (cp_lexer_debugging_p (lexer))
1211 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1213 lexer->next_token = lexer->saved_tokens.pop ();
1216 /* RAII wrapper around the above functions, with sanity checking. Creating
1217 a variable saves tokens, which are committed when the variable is
1218 destroyed unless they are explicitly rolled back by calling the rollback
1219 member function. */
1221 struct saved_token_sentinel
1223 cp_lexer *lexer;
1224 unsigned len;
1225 bool commit;
1226 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1228 len = lexer->saved_tokens.length ();
1229 cp_lexer_save_tokens (lexer);
1231 void rollback ()
1233 cp_lexer_rollback_tokens (lexer);
1234 commit = false;
1236 ~saved_token_sentinel()
1238 if (commit)
1239 cp_lexer_commit_tokens (lexer);
1240 gcc_assert (lexer->saved_tokens.length () == len);
1244 /* Print a representation of the TOKEN on the STREAM. */
1246 static void
1247 cp_lexer_print_token (FILE * stream, cp_token *token)
1249 /* We don't use cpp_type2name here because the parser defines
1250 a few tokens of its own. */
1251 static const char *const token_names[] = {
1252 /* cpplib-defined token types */
1253 #define OP(e, s) #e,
1254 #define TK(e, s) #e,
1255 TTYPE_TABLE
1256 #undef OP
1257 #undef TK
1258 /* C++ parser token types - see "Manifest constants", above. */
1259 "KEYWORD",
1260 "TEMPLATE_ID",
1261 "NESTED_NAME_SPECIFIER",
1264 /* For some tokens, print the associated data. */
1265 switch (token->type)
1267 case CPP_KEYWORD:
1268 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1269 For example, `struct' is mapped to an INTEGER_CST. */
1270 if (!identifier_p (token->u.value))
1271 break;
1272 /* fall through */
1273 case CPP_NAME:
1274 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1275 break;
1277 case CPP_STRING:
1278 case CPP_STRING16:
1279 case CPP_STRING32:
1280 case CPP_WSTRING:
1281 case CPP_UTF8STRING:
1282 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1283 break;
1285 case CPP_NUMBER:
1286 print_generic_expr (stream, token->u.value, 0);
1287 break;
1289 default:
1290 /* If we have a name for the token, print it out. Otherwise, we
1291 simply give the numeric code. */
1292 if (token->type < ARRAY_SIZE(token_names))
1293 fputs (token_names[token->type], stream);
1294 else
1295 fprintf (stream, "[%d]", token->type);
1296 break;
1300 DEBUG_FUNCTION void
1301 debug (cp_token &ref)
1303 cp_lexer_print_token (stderr, &ref);
1304 fprintf (stderr, "\n");
1307 DEBUG_FUNCTION void
1308 debug (cp_token *ptr)
1310 if (ptr)
1311 debug (*ptr);
1312 else
1313 fprintf (stderr, "<nil>\n");
1317 /* Start emitting debugging information. */
1319 static void
1320 cp_lexer_start_debugging (cp_lexer* lexer)
1322 if (!LEXER_DEBUGGING_ENABLED_P)
1323 fatal_error (input_location,
1324 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1326 lexer->debugging_p = true;
1327 cp_lexer_debug_stream = stderr;
1330 /* Stop emitting debugging information. */
1332 static void
1333 cp_lexer_stop_debugging (cp_lexer* lexer)
1335 if (!LEXER_DEBUGGING_ENABLED_P)
1336 fatal_error (input_location,
1337 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1339 lexer->debugging_p = false;
1340 cp_lexer_debug_stream = NULL;
1343 /* Create a new cp_token_cache, representing a range of tokens. */
1345 static cp_token_cache *
1346 cp_token_cache_new (cp_token *first, cp_token *last)
1348 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1349 cache->first = first;
1350 cache->last = last;
1351 return cache;
1354 /* Diagnose if #pragma omp declare simd isn't followed immediately
1355 by function declaration or definition. */
1357 static inline void
1358 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1360 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1362 error ("%<#pragma omp declare simd%> not immediately followed by "
1363 "function declaration or definition");
1364 parser->omp_declare_simd = NULL;
1368 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1369 and put that into "omp declare simd" attribute. */
1371 static inline void
1372 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1374 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1376 if (fndecl == error_mark_node)
1378 parser->omp_declare_simd = NULL;
1379 return;
1381 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1383 cp_ensure_no_omp_declare_simd (parser);
1384 return;
1389 /* Diagnose if #pragma acc routine isn't followed immediately by function
1390 declaration or definition. */
1392 static inline void
1393 cp_ensure_no_oacc_routine (cp_parser *parser)
1395 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1397 error_at (parser->oacc_routine->loc,
1398 "%<#pragma acc routine%> not immediately followed by "
1399 "function declaration or definition");
1400 parser->oacc_routine = NULL;
1404 /* Decl-specifiers. */
1406 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1408 static void
1409 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1411 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1414 /* Declarators. */
1416 /* Nothing other than the parser should be creating declarators;
1417 declarators are a semi-syntactic representation of C++ entities.
1418 Other parts of the front end that need to create entities (like
1419 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1421 static cp_declarator *make_call_declarator
1422 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1423 static cp_declarator *make_array_declarator
1424 (cp_declarator *, tree);
1425 static cp_declarator *make_pointer_declarator
1426 (cp_cv_quals, cp_declarator *, tree);
1427 static cp_declarator *make_reference_declarator
1428 (cp_cv_quals, cp_declarator *, bool, tree);
1429 static cp_declarator *make_ptrmem_declarator
1430 (cp_cv_quals, tree, cp_declarator *, tree);
1432 /* An erroneous declarator. */
1433 static cp_declarator *cp_error_declarator;
1435 /* The obstack on which declarators and related data structures are
1436 allocated. */
1437 static struct obstack declarator_obstack;
1439 /* Alloc BYTES from the declarator memory pool. */
1441 static inline void *
1442 alloc_declarator (size_t bytes)
1444 return obstack_alloc (&declarator_obstack, bytes);
1447 /* Allocate a declarator of the indicated KIND. Clear fields that are
1448 common to all declarators. */
1450 static cp_declarator *
1451 make_declarator (cp_declarator_kind kind)
1453 cp_declarator *declarator;
1455 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1456 declarator->kind = kind;
1457 declarator->attributes = NULL_TREE;
1458 declarator->std_attributes = NULL_TREE;
1459 declarator->declarator = NULL;
1460 declarator->parameter_pack_p = false;
1461 declarator->id_loc = UNKNOWN_LOCATION;
1463 return declarator;
1466 /* Make a declarator for a generalized identifier. If
1467 QUALIFYING_SCOPE is non-NULL, the identifier is
1468 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1469 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1470 is, if any. */
1472 static cp_declarator *
1473 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1474 special_function_kind sfk)
1476 cp_declarator *declarator;
1478 /* It is valid to write:
1480 class C { void f(); };
1481 typedef C D;
1482 void D::f();
1484 The standard is not clear about whether `typedef const C D' is
1485 legal; as of 2002-09-15 the committee is considering that
1486 question. EDG 3.0 allows that syntax. Therefore, we do as
1487 well. */
1488 if (qualifying_scope && TYPE_P (qualifying_scope))
1489 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1491 gcc_assert (identifier_p (unqualified_name)
1492 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1493 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1495 declarator = make_declarator (cdk_id);
1496 declarator->u.id.qualifying_scope = qualifying_scope;
1497 declarator->u.id.unqualified_name = unqualified_name;
1498 declarator->u.id.sfk = sfk;
1500 return declarator;
1503 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1504 of modifiers such as const or volatile to apply to the pointer
1505 type, represented as identifiers. ATTRIBUTES represent the attributes that
1506 appertain to the pointer or reference. */
1508 cp_declarator *
1509 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1510 tree attributes)
1512 cp_declarator *declarator;
1514 declarator = make_declarator (cdk_pointer);
1515 declarator->declarator = target;
1516 declarator->u.pointer.qualifiers = cv_qualifiers;
1517 declarator->u.pointer.class_type = NULL_TREE;
1518 if (target)
1520 declarator->id_loc = target->id_loc;
1521 declarator->parameter_pack_p = target->parameter_pack_p;
1522 target->parameter_pack_p = false;
1524 else
1525 declarator->parameter_pack_p = false;
1527 declarator->std_attributes = attributes;
1529 return declarator;
1532 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1533 represent the attributes that appertain to the pointer or
1534 reference. */
1536 cp_declarator *
1537 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1538 bool rvalue_ref, tree attributes)
1540 cp_declarator *declarator;
1542 declarator = make_declarator (cdk_reference);
1543 declarator->declarator = target;
1544 declarator->u.reference.qualifiers = cv_qualifiers;
1545 declarator->u.reference.rvalue_ref = rvalue_ref;
1546 if (target)
1548 declarator->id_loc = target->id_loc;
1549 declarator->parameter_pack_p = target->parameter_pack_p;
1550 target->parameter_pack_p = false;
1552 else
1553 declarator->parameter_pack_p = false;
1555 declarator->std_attributes = attributes;
1557 return declarator;
1560 /* Like make_pointer_declarator -- but for a pointer to a non-static
1561 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1562 appertain to the pointer or reference. */
1564 cp_declarator *
1565 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1566 cp_declarator *pointee,
1567 tree attributes)
1569 cp_declarator *declarator;
1571 declarator = make_declarator (cdk_ptrmem);
1572 declarator->declarator = pointee;
1573 declarator->u.pointer.qualifiers = cv_qualifiers;
1574 declarator->u.pointer.class_type = class_type;
1576 if (pointee)
1578 declarator->parameter_pack_p = pointee->parameter_pack_p;
1579 pointee->parameter_pack_p = false;
1581 else
1582 declarator->parameter_pack_p = false;
1584 declarator->std_attributes = attributes;
1586 return declarator;
1589 /* Make a declarator for the function given by TARGET, with the
1590 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1591 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1592 indicates what exceptions can be thrown. */
1594 cp_declarator *
1595 make_call_declarator (cp_declarator *target,
1596 tree parms,
1597 cp_cv_quals cv_qualifiers,
1598 cp_virt_specifiers virt_specifiers,
1599 cp_ref_qualifier ref_qualifier,
1600 tree tx_qualifier,
1601 tree exception_specification,
1602 tree late_return_type,
1603 tree requires_clause)
1605 cp_declarator *declarator;
1607 declarator = make_declarator (cdk_function);
1608 declarator->declarator = target;
1609 declarator->u.function.parameters = parms;
1610 declarator->u.function.qualifiers = cv_qualifiers;
1611 declarator->u.function.virt_specifiers = virt_specifiers;
1612 declarator->u.function.ref_qualifier = ref_qualifier;
1613 declarator->u.function.tx_qualifier = tx_qualifier;
1614 declarator->u.function.exception_specification = exception_specification;
1615 declarator->u.function.late_return_type = late_return_type;
1616 declarator->u.function.requires_clause = requires_clause;
1617 if (target)
1619 declarator->id_loc = target->id_loc;
1620 declarator->parameter_pack_p = target->parameter_pack_p;
1621 target->parameter_pack_p = false;
1623 else
1624 declarator->parameter_pack_p = false;
1626 return declarator;
1629 /* Make a declarator for an array of BOUNDS elements, each of which is
1630 defined by ELEMENT. */
1632 cp_declarator *
1633 make_array_declarator (cp_declarator *element, tree bounds)
1635 cp_declarator *declarator;
1637 declarator = make_declarator (cdk_array);
1638 declarator->declarator = element;
1639 declarator->u.array.bounds = bounds;
1640 if (element)
1642 declarator->id_loc = element->id_loc;
1643 declarator->parameter_pack_p = element->parameter_pack_p;
1644 element->parameter_pack_p = false;
1646 else
1647 declarator->parameter_pack_p = false;
1649 return declarator;
1652 /* Determine whether the declarator we've seen so far can be a
1653 parameter pack, when followed by an ellipsis. */
1654 static bool
1655 declarator_can_be_parameter_pack (cp_declarator *declarator)
1657 if (declarator && declarator->parameter_pack_p)
1658 /* We already saw an ellipsis. */
1659 return false;
1661 /* Search for a declarator name, or any other declarator that goes
1662 after the point where the ellipsis could appear in a parameter
1663 pack. If we find any of these, then this declarator can not be
1664 made into a parameter pack. */
1665 bool found = false;
1666 while (declarator && !found)
1668 switch ((int)declarator->kind)
1670 case cdk_id:
1671 case cdk_array:
1672 case cdk_decomp:
1673 found = true;
1674 break;
1676 case cdk_error:
1677 return true;
1679 default:
1680 declarator = declarator->declarator;
1681 break;
1685 return !found;
1688 cp_parameter_declarator *no_parameters;
1690 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1691 DECLARATOR and DEFAULT_ARGUMENT. */
1693 cp_parameter_declarator *
1694 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1695 cp_declarator *declarator,
1696 tree default_argument,
1697 bool template_parameter_pack_p = false)
1699 cp_parameter_declarator *parameter;
1701 parameter = ((cp_parameter_declarator *)
1702 alloc_declarator (sizeof (cp_parameter_declarator)));
1703 parameter->next = NULL;
1704 if (decl_specifiers)
1705 parameter->decl_specifiers = *decl_specifiers;
1706 else
1707 clear_decl_specs (&parameter->decl_specifiers);
1708 parameter->declarator = declarator;
1709 parameter->default_argument = default_argument;
1710 parameter->template_parameter_pack_p = template_parameter_pack_p;
1712 return parameter;
1715 /* Returns true iff DECLARATOR is a declaration for a function. */
1717 static bool
1718 function_declarator_p (const cp_declarator *declarator)
1720 while (declarator)
1722 if (declarator->kind == cdk_function
1723 && declarator->declarator->kind == cdk_id)
1724 return true;
1725 if (declarator->kind == cdk_id
1726 || declarator->kind == cdk_decomp
1727 || declarator->kind == cdk_error)
1728 return false;
1729 declarator = declarator->declarator;
1731 return false;
1734 /* The parser. */
1736 /* Overview
1737 --------
1739 A cp_parser parses the token stream as specified by the C++
1740 grammar. Its job is purely parsing, not semantic analysis. For
1741 example, the parser breaks the token stream into declarators,
1742 expressions, statements, and other similar syntactic constructs.
1743 It does not check that the types of the expressions on either side
1744 of an assignment-statement are compatible, or that a function is
1745 not declared with a parameter of type `void'.
1747 The parser invokes routines elsewhere in the compiler to perform
1748 semantic analysis and to build up the abstract syntax tree for the
1749 code processed.
1751 The parser (and the template instantiation code, which is, in a
1752 way, a close relative of parsing) are the only parts of the
1753 compiler that should be calling push_scope and pop_scope, or
1754 related functions. The parser (and template instantiation code)
1755 keeps track of what scope is presently active; everything else
1756 should simply honor that. (The code that generates static
1757 initializers may also need to set the scope, in order to check
1758 access control correctly when emitting the initializers.)
1760 Methodology
1761 -----------
1763 The parser is of the standard recursive-descent variety. Upcoming
1764 tokens in the token stream are examined in order to determine which
1765 production to use when parsing a non-terminal. Some C++ constructs
1766 require arbitrary look ahead to disambiguate. For example, it is
1767 impossible, in the general case, to tell whether a statement is an
1768 expression or declaration without scanning the entire statement.
1769 Therefore, the parser is capable of "parsing tentatively." When the
1770 parser is not sure what construct comes next, it enters this mode.
1771 Then, while we attempt to parse the construct, the parser queues up
1772 error messages, rather than issuing them immediately, and saves the
1773 tokens it consumes. If the construct is parsed successfully, the
1774 parser "commits", i.e., it issues any queued error messages and
1775 the tokens that were being preserved are permanently discarded.
1776 If, however, the construct is not parsed successfully, the parser
1777 rolls back its state completely so that it can resume parsing using
1778 a different alternative.
1780 Future Improvements
1781 -------------------
1783 The performance of the parser could probably be improved substantially.
1784 We could often eliminate the need to parse tentatively by looking ahead
1785 a little bit. In some places, this approach might not entirely eliminate
1786 the need to parse tentatively, but it might still speed up the average
1787 case. */
1789 /* Flags that are passed to some parsing functions. These values can
1790 be bitwise-ored together. */
1792 enum
1794 /* No flags. */
1795 CP_PARSER_FLAGS_NONE = 0x0,
1796 /* The construct is optional. If it is not present, then no error
1797 should be issued. */
1798 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1799 /* When parsing a type-specifier, treat user-defined type-names
1800 as non-type identifiers. */
1801 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1802 /* When parsing a type-specifier, do not try to parse a class-specifier
1803 or enum-specifier. */
1804 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1805 /* When parsing a decl-specifier-seq, only allow type-specifier or
1806 constexpr. */
1807 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1808 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1809 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1812 /* This type is used for parameters and variables which hold
1813 combinations of the above flags. */
1814 typedef int cp_parser_flags;
1816 /* The different kinds of declarators we want to parse. */
1818 enum cp_parser_declarator_kind
1820 /* We want an abstract declarator. */
1821 CP_PARSER_DECLARATOR_ABSTRACT,
1822 /* We want a named declarator. */
1823 CP_PARSER_DECLARATOR_NAMED,
1824 /* We don't mind, but the name must be an unqualified-id. */
1825 CP_PARSER_DECLARATOR_EITHER
1828 /* The precedence values used to parse binary expressions. The minimum value
1829 of PREC must be 1, because zero is reserved to quickly discriminate
1830 binary operators from other tokens. */
1832 enum cp_parser_prec
1834 PREC_NOT_OPERATOR,
1835 PREC_LOGICAL_OR_EXPRESSION,
1836 PREC_LOGICAL_AND_EXPRESSION,
1837 PREC_INCLUSIVE_OR_EXPRESSION,
1838 PREC_EXCLUSIVE_OR_EXPRESSION,
1839 PREC_AND_EXPRESSION,
1840 PREC_EQUALITY_EXPRESSION,
1841 PREC_RELATIONAL_EXPRESSION,
1842 PREC_SHIFT_EXPRESSION,
1843 PREC_ADDITIVE_EXPRESSION,
1844 PREC_MULTIPLICATIVE_EXPRESSION,
1845 PREC_PM_EXPRESSION,
1846 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1849 /* A mapping from a token type to a corresponding tree node type, with a
1850 precedence value. */
1852 struct cp_parser_binary_operations_map_node
1854 /* The token type. */
1855 enum cpp_ttype token_type;
1856 /* The corresponding tree code. */
1857 enum tree_code tree_type;
1858 /* The precedence of this operator. */
1859 enum cp_parser_prec prec;
1862 struct cp_parser_expression_stack_entry
1864 /* Left hand side of the binary operation we are currently
1865 parsing. */
1866 cp_expr lhs;
1867 /* Original tree code for left hand side, if it was a binary
1868 expression itself (used for -Wparentheses). */
1869 enum tree_code lhs_type;
1870 /* Tree code for the binary operation we are parsing. */
1871 enum tree_code tree_type;
1872 /* Precedence of the binary operation we are parsing. */
1873 enum cp_parser_prec prec;
1874 /* Location of the binary operation we are parsing. */
1875 location_t loc;
1878 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1879 entries because precedence levels on the stack are monotonically
1880 increasing. */
1881 typedef struct cp_parser_expression_stack_entry
1882 cp_parser_expression_stack[NUM_PREC_VALUES];
1884 /* Prototypes. */
1886 /* Constructors and destructors. */
1888 static cp_parser_context *cp_parser_context_new
1889 (cp_parser_context *);
1891 /* Class variables. */
1893 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1895 /* The operator-precedence table used by cp_parser_binary_expression.
1896 Transformed into an associative array (binops_by_token) by
1897 cp_parser_new. */
1899 static const cp_parser_binary_operations_map_node binops[] = {
1900 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1901 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1903 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1904 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1905 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1907 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1908 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1910 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1911 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1913 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1914 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1915 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1916 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1918 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1919 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1921 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1923 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1925 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1927 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1929 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1932 /* The same as binops, but initialized by cp_parser_new so that
1933 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1934 for speed. */
1935 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1937 /* Constructors and destructors. */
1939 /* Construct a new context. The context below this one on the stack
1940 is given by NEXT. */
1942 static cp_parser_context *
1943 cp_parser_context_new (cp_parser_context* next)
1945 cp_parser_context *context;
1947 /* Allocate the storage. */
1948 if (cp_parser_context_free_list != NULL)
1950 /* Pull the first entry from the free list. */
1951 context = cp_parser_context_free_list;
1952 cp_parser_context_free_list = context->next;
1953 memset (context, 0, sizeof (*context));
1955 else
1956 context = ggc_cleared_alloc<cp_parser_context> ();
1958 /* No errors have occurred yet in this context. */
1959 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1960 /* If this is not the bottommost context, copy information that we
1961 need from the previous context. */
1962 if (next)
1964 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1965 expression, then we are parsing one in this context, too. */
1966 context->object_type = next->object_type;
1967 /* Thread the stack. */
1968 context->next = next;
1971 return context;
1974 /* Managing the unparsed function queues. */
1976 #define unparsed_funs_with_default_args \
1977 parser->unparsed_queues->last ().funs_with_default_args
1978 #define unparsed_funs_with_definitions \
1979 parser->unparsed_queues->last ().funs_with_definitions
1980 #define unparsed_nsdmis \
1981 parser->unparsed_queues->last ().nsdmis
1982 #define unparsed_classes \
1983 parser->unparsed_queues->last ().classes
1985 static void
1986 push_unparsed_function_queues (cp_parser *parser)
1988 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1989 vec_safe_push (parser->unparsed_queues, e);
1992 static void
1993 pop_unparsed_function_queues (cp_parser *parser)
1995 release_tree_vector (unparsed_funs_with_definitions);
1996 parser->unparsed_queues->pop ();
1999 /* Prototypes. */
2001 /* Constructors and destructors. */
2003 static cp_parser *cp_parser_new
2004 (void);
2006 /* Routines to parse various constructs.
2008 Those that return `tree' will return the error_mark_node (rather
2009 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2010 Sometimes, they will return an ordinary node if error-recovery was
2011 attempted, even though a parse error occurred. So, to check
2012 whether or not a parse error occurred, you should always use
2013 cp_parser_error_occurred. If the construct is optional (indicated
2014 either by an `_opt' in the name of the function that does the
2015 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2016 the construct is not present. */
2018 /* Lexical conventions [gram.lex] */
2020 static cp_expr cp_parser_identifier
2021 (cp_parser *);
2022 static cp_expr cp_parser_string_literal
2023 (cp_parser *, bool, bool, bool);
2024 static cp_expr cp_parser_userdef_char_literal
2025 (cp_parser *);
2026 static tree cp_parser_userdef_string_literal
2027 (tree);
2028 static cp_expr cp_parser_userdef_numeric_literal
2029 (cp_parser *);
2031 /* Basic concepts [gram.basic] */
2033 static bool cp_parser_translation_unit
2034 (cp_parser *);
2036 /* Expressions [gram.expr] */
2038 static cp_expr cp_parser_primary_expression
2039 (cp_parser *, bool, bool, bool, cp_id_kind *);
2040 static cp_expr cp_parser_id_expression
2041 (cp_parser *, bool, bool, bool *, bool, bool);
2042 static cp_expr cp_parser_unqualified_id
2043 (cp_parser *, bool, bool, bool, bool);
2044 static tree cp_parser_nested_name_specifier_opt
2045 (cp_parser *, bool, bool, bool, bool);
2046 static tree cp_parser_nested_name_specifier
2047 (cp_parser *, bool, bool, bool, bool);
2048 static tree cp_parser_qualifying_entity
2049 (cp_parser *, bool, bool, bool, bool, bool);
2050 static cp_expr cp_parser_postfix_expression
2051 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2052 static tree cp_parser_postfix_open_square_expression
2053 (cp_parser *, tree, bool, bool);
2054 static tree cp_parser_postfix_dot_deref_expression
2055 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2056 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2057 (cp_parser *, int, bool, bool, bool *, location_t * = NULL);
2058 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2059 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2060 static void cp_parser_pseudo_destructor_name
2061 (cp_parser *, tree, tree *, tree *);
2062 static cp_expr cp_parser_unary_expression
2063 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2064 static enum tree_code cp_parser_unary_operator
2065 (cp_token *);
2066 static tree cp_parser_new_expression
2067 (cp_parser *);
2068 static vec<tree, va_gc> *cp_parser_new_placement
2069 (cp_parser *);
2070 static tree cp_parser_new_type_id
2071 (cp_parser *, tree *);
2072 static cp_declarator *cp_parser_new_declarator_opt
2073 (cp_parser *);
2074 static cp_declarator *cp_parser_direct_new_declarator
2075 (cp_parser *);
2076 static vec<tree, va_gc> *cp_parser_new_initializer
2077 (cp_parser *);
2078 static tree cp_parser_delete_expression
2079 (cp_parser *);
2080 static cp_expr cp_parser_cast_expression
2081 (cp_parser *, bool, bool, bool, cp_id_kind *);
2082 static cp_expr cp_parser_binary_expression
2083 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2084 static tree cp_parser_question_colon_clause
2085 (cp_parser *, cp_expr);
2086 static cp_expr cp_parser_assignment_expression
2087 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2088 static enum tree_code cp_parser_assignment_operator_opt
2089 (cp_parser *);
2090 static cp_expr cp_parser_expression
2091 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2092 static cp_expr cp_parser_constant_expression
2093 (cp_parser *, bool = false, bool * = NULL);
2094 static cp_expr cp_parser_builtin_offsetof
2095 (cp_parser *);
2096 static cp_expr cp_parser_lambda_expression
2097 (cp_parser *);
2098 static void cp_parser_lambda_introducer
2099 (cp_parser *, tree);
2100 static bool cp_parser_lambda_declarator_opt
2101 (cp_parser *, tree);
2102 static void cp_parser_lambda_body
2103 (cp_parser *, tree);
2105 /* Statements [gram.stmt.stmt] */
2107 static void cp_parser_statement
2108 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL);
2109 static void cp_parser_label_for_labeled_statement
2110 (cp_parser *, tree);
2111 static tree cp_parser_expression_statement
2112 (cp_parser *, tree);
2113 static tree cp_parser_compound_statement
2114 (cp_parser *, tree, int, bool);
2115 static void cp_parser_statement_seq_opt
2116 (cp_parser *, tree);
2117 static tree cp_parser_selection_statement
2118 (cp_parser *, bool *, vec<tree> *);
2119 static tree cp_parser_condition
2120 (cp_parser *);
2121 static tree cp_parser_iteration_statement
2122 (cp_parser *, bool *, bool);
2123 static bool cp_parser_init_statement
2124 (cp_parser *, tree *decl);
2125 static tree cp_parser_for
2126 (cp_parser *, bool);
2127 static tree cp_parser_c_for
2128 (cp_parser *, tree, tree, bool);
2129 static tree cp_parser_range_for
2130 (cp_parser *, tree, tree, tree, bool);
2131 static void do_range_for_auto_deduction
2132 (tree, tree);
2133 static tree cp_parser_perform_range_for_lookup
2134 (tree, tree *, tree *);
2135 static tree cp_parser_range_for_member_function
2136 (tree, tree);
2137 static tree cp_parser_jump_statement
2138 (cp_parser *);
2139 static void cp_parser_declaration_statement
2140 (cp_parser *);
2142 static tree cp_parser_implicitly_scoped_statement
2143 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2144 static void cp_parser_already_scoped_statement
2145 (cp_parser *, bool *, const token_indent_info &);
2147 /* Declarations [gram.dcl.dcl] */
2149 static void cp_parser_declaration_seq_opt
2150 (cp_parser *);
2151 static void cp_parser_declaration
2152 (cp_parser *);
2153 static void cp_parser_block_declaration
2154 (cp_parser *, bool);
2155 static void cp_parser_simple_declaration
2156 (cp_parser *, bool, tree *);
2157 static void cp_parser_decl_specifier_seq
2158 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2159 static tree cp_parser_storage_class_specifier_opt
2160 (cp_parser *);
2161 static tree cp_parser_function_specifier_opt
2162 (cp_parser *, cp_decl_specifier_seq *);
2163 static tree cp_parser_type_specifier
2164 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2165 int *, bool *);
2166 static tree cp_parser_simple_type_specifier
2167 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2168 static tree cp_parser_type_name
2169 (cp_parser *, bool);
2170 static tree cp_parser_type_name
2171 (cp_parser *);
2172 static tree cp_parser_nonclass_name
2173 (cp_parser* parser);
2174 static tree cp_parser_elaborated_type_specifier
2175 (cp_parser *, bool, bool);
2176 static tree cp_parser_enum_specifier
2177 (cp_parser *);
2178 static void cp_parser_enumerator_list
2179 (cp_parser *, tree);
2180 static void cp_parser_enumerator_definition
2181 (cp_parser *, tree);
2182 static tree cp_parser_namespace_name
2183 (cp_parser *);
2184 static void cp_parser_namespace_definition
2185 (cp_parser *);
2186 static void cp_parser_namespace_body
2187 (cp_parser *);
2188 static tree cp_parser_qualified_namespace_specifier
2189 (cp_parser *);
2190 static void cp_parser_namespace_alias_definition
2191 (cp_parser *);
2192 static bool cp_parser_using_declaration
2193 (cp_parser *, bool);
2194 static void cp_parser_using_directive
2195 (cp_parser *);
2196 static tree cp_parser_alias_declaration
2197 (cp_parser *);
2198 static void cp_parser_asm_definition
2199 (cp_parser *);
2200 static void cp_parser_linkage_specification
2201 (cp_parser *);
2202 static void cp_parser_static_assert
2203 (cp_parser *, bool);
2204 static tree cp_parser_decltype
2205 (cp_parser *);
2206 static tree cp_parser_decomposition_declaration
2207 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2209 /* Declarators [gram.dcl.decl] */
2211 static tree cp_parser_init_declarator
2212 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2213 bool, bool, int, bool *, tree *, location_t *, tree *);
2214 static cp_declarator *cp_parser_declarator
2215 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2216 static cp_declarator *cp_parser_direct_declarator
2217 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2218 static enum tree_code cp_parser_ptr_operator
2219 (cp_parser *, tree *, cp_cv_quals *, tree *);
2220 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2221 (cp_parser *);
2222 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2223 (cp_parser *);
2224 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2225 (cp_parser *);
2226 static tree cp_parser_tx_qualifier_opt
2227 (cp_parser *);
2228 static tree cp_parser_late_return_type_opt
2229 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2230 static tree cp_parser_declarator_id
2231 (cp_parser *, bool);
2232 static tree cp_parser_type_id
2233 (cp_parser *);
2234 static tree cp_parser_template_type_arg
2235 (cp_parser *);
2236 static tree cp_parser_trailing_type_id (cp_parser *);
2237 static tree cp_parser_type_id_1
2238 (cp_parser *, bool, bool);
2239 static void cp_parser_type_specifier_seq
2240 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2241 static tree cp_parser_parameter_declaration_clause
2242 (cp_parser *);
2243 static tree cp_parser_parameter_declaration_list
2244 (cp_parser *, bool *);
2245 static cp_parameter_declarator *cp_parser_parameter_declaration
2246 (cp_parser *, bool, bool *);
2247 static tree cp_parser_default_argument
2248 (cp_parser *, bool);
2249 static void cp_parser_function_body
2250 (cp_parser *, bool);
2251 static tree cp_parser_initializer
2252 (cp_parser *, bool *, bool *);
2253 static cp_expr cp_parser_initializer_clause
2254 (cp_parser *, bool *);
2255 static cp_expr cp_parser_braced_list
2256 (cp_parser*, bool*);
2257 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2258 (cp_parser *, bool *);
2260 static bool cp_parser_ctor_initializer_opt_and_function_body
2261 (cp_parser *, bool);
2263 static tree cp_parser_late_parsing_omp_declare_simd
2264 (cp_parser *, tree);
2266 static tree cp_parser_late_parsing_cilk_simd_fn_info
2267 (cp_parser *, tree);
2269 static tree cp_parser_late_parsing_oacc_routine
2270 (cp_parser *, tree);
2272 static tree synthesize_implicit_template_parm
2273 (cp_parser *, tree);
2274 static tree finish_fully_implicit_template
2275 (cp_parser *, tree);
2277 /* Classes [gram.class] */
2279 static tree cp_parser_class_name
2280 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2281 static tree cp_parser_class_specifier
2282 (cp_parser *);
2283 static tree cp_parser_class_head
2284 (cp_parser *, bool *);
2285 static enum tag_types cp_parser_class_key
2286 (cp_parser *);
2287 static void cp_parser_type_parameter_key
2288 (cp_parser* parser);
2289 static void cp_parser_member_specification_opt
2290 (cp_parser *);
2291 static void cp_parser_member_declaration
2292 (cp_parser *);
2293 static tree cp_parser_pure_specifier
2294 (cp_parser *);
2295 static tree cp_parser_constant_initializer
2296 (cp_parser *);
2298 /* Derived classes [gram.class.derived] */
2300 static tree cp_parser_base_clause
2301 (cp_parser *);
2302 static tree cp_parser_base_specifier
2303 (cp_parser *);
2305 /* Special member functions [gram.special] */
2307 static tree cp_parser_conversion_function_id
2308 (cp_parser *);
2309 static tree cp_parser_conversion_type_id
2310 (cp_parser *);
2311 static cp_declarator *cp_parser_conversion_declarator_opt
2312 (cp_parser *);
2313 static bool cp_parser_ctor_initializer_opt
2314 (cp_parser *);
2315 static void cp_parser_mem_initializer_list
2316 (cp_parser *);
2317 static tree cp_parser_mem_initializer
2318 (cp_parser *);
2319 static tree cp_parser_mem_initializer_id
2320 (cp_parser *);
2322 /* Overloading [gram.over] */
2324 static cp_expr cp_parser_operator_function_id
2325 (cp_parser *);
2326 static cp_expr cp_parser_operator
2327 (cp_parser *);
2329 /* Templates [gram.temp] */
2331 static void cp_parser_template_declaration
2332 (cp_parser *, bool);
2333 static tree cp_parser_template_parameter_list
2334 (cp_parser *);
2335 static tree cp_parser_template_parameter
2336 (cp_parser *, bool *, bool *);
2337 static tree cp_parser_type_parameter
2338 (cp_parser *, bool *);
2339 static tree cp_parser_template_id
2340 (cp_parser *, bool, bool, enum tag_types, bool);
2341 static tree cp_parser_template_name
2342 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2343 static tree cp_parser_template_argument_list
2344 (cp_parser *);
2345 static tree cp_parser_template_argument
2346 (cp_parser *);
2347 static void cp_parser_explicit_instantiation
2348 (cp_parser *);
2349 static void cp_parser_explicit_specialization
2350 (cp_parser *);
2352 /* Exception handling [gram.exception] */
2354 static tree cp_parser_try_block
2355 (cp_parser *);
2356 static bool cp_parser_function_try_block
2357 (cp_parser *);
2358 static void cp_parser_handler_seq
2359 (cp_parser *);
2360 static void cp_parser_handler
2361 (cp_parser *);
2362 static tree cp_parser_exception_declaration
2363 (cp_parser *);
2364 static tree cp_parser_throw_expression
2365 (cp_parser *);
2366 static tree cp_parser_exception_specification_opt
2367 (cp_parser *);
2368 static tree cp_parser_type_id_list
2369 (cp_parser *);
2371 /* GNU Extensions */
2373 static tree cp_parser_asm_specification_opt
2374 (cp_parser *);
2375 static tree cp_parser_asm_operand_list
2376 (cp_parser *);
2377 static tree cp_parser_asm_clobber_list
2378 (cp_parser *);
2379 static tree cp_parser_asm_label_list
2380 (cp_parser *);
2381 static bool cp_next_tokens_can_be_attribute_p
2382 (cp_parser *);
2383 static bool cp_next_tokens_can_be_gnu_attribute_p
2384 (cp_parser *);
2385 static bool cp_next_tokens_can_be_std_attribute_p
2386 (cp_parser *);
2387 static bool cp_nth_tokens_can_be_std_attribute_p
2388 (cp_parser *, size_t);
2389 static bool cp_nth_tokens_can_be_gnu_attribute_p
2390 (cp_parser *, size_t);
2391 static bool cp_nth_tokens_can_be_attribute_p
2392 (cp_parser *, size_t);
2393 static tree cp_parser_attributes_opt
2394 (cp_parser *);
2395 static tree cp_parser_gnu_attributes_opt
2396 (cp_parser *);
2397 static tree cp_parser_gnu_attribute_list
2398 (cp_parser *);
2399 static tree cp_parser_std_attribute
2400 (cp_parser *, tree);
2401 static tree cp_parser_std_attribute_spec
2402 (cp_parser *);
2403 static tree cp_parser_std_attribute_spec_seq
2404 (cp_parser *);
2405 static bool cp_parser_extension_opt
2406 (cp_parser *, int *);
2407 static void cp_parser_label_declaration
2408 (cp_parser *);
2410 /* Concept Extensions */
2412 static tree cp_parser_requires_clause
2413 (cp_parser *);
2414 static tree cp_parser_requires_clause_opt
2415 (cp_parser *);
2416 static tree cp_parser_requires_expression
2417 (cp_parser *);
2418 static tree cp_parser_requirement_parameter_list
2419 (cp_parser *);
2420 static tree cp_parser_requirement_body
2421 (cp_parser *);
2422 static tree cp_parser_requirement_list
2423 (cp_parser *);
2424 static tree cp_parser_requirement
2425 (cp_parser *);
2426 static tree cp_parser_simple_requirement
2427 (cp_parser *);
2428 static tree cp_parser_compound_requirement
2429 (cp_parser *);
2430 static tree cp_parser_type_requirement
2431 (cp_parser *);
2432 static tree cp_parser_nested_requirement
2433 (cp_parser *);
2435 /* Transactional Memory Extensions */
2437 static tree cp_parser_transaction
2438 (cp_parser *, cp_token *);
2439 static tree cp_parser_transaction_expression
2440 (cp_parser *, enum rid);
2441 static bool cp_parser_function_transaction
2442 (cp_parser *, enum rid);
2443 static tree cp_parser_transaction_cancel
2444 (cp_parser *);
2446 enum pragma_context {
2447 pragma_external,
2448 pragma_member,
2449 pragma_objc_icode,
2450 pragma_stmt,
2451 pragma_compound
2453 static bool cp_parser_pragma
2454 (cp_parser *, enum pragma_context, bool *);
2456 /* Objective-C++ Productions */
2458 static tree cp_parser_objc_message_receiver
2459 (cp_parser *);
2460 static tree cp_parser_objc_message_args
2461 (cp_parser *);
2462 static tree cp_parser_objc_message_expression
2463 (cp_parser *);
2464 static cp_expr cp_parser_objc_encode_expression
2465 (cp_parser *);
2466 static tree cp_parser_objc_defs_expression
2467 (cp_parser *);
2468 static tree cp_parser_objc_protocol_expression
2469 (cp_parser *);
2470 static tree cp_parser_objc_selector_expression
2471 (cp_parser *);
2472 static cp_expr cp_parser_objc_expression
2473 (cp_parser *);
2474 static bool cp_parser_objc_selector_p
2475 (enum cpp_ttype);
2476 static tree cp_parser_objc_selector
2477 (cp_parser *);
2478 static tree cp_parser_objc_protocol_refs_opt
2479 (cp_parser *);
2480 static void cp_parser_objc_declaration
2481 (cp_parser *, tree);
2482 static tree cp_parser_objc_statement
2483 (cp_parser *);
2484 static bool cp_parser_objc_valid_prefix_attributes
2485 (cp_parser *, tree *);
2486 static void cp_parser_objc_at_property_declaration
2487 (cp_parser *) ;
2488 static void cp_parser_objc_at_synthesize_declaration
2489 (cp_parser *) ;
2490 static void cp_parser_objc_at_dynamic_declaration
2491 (cp_parser *) ;
2492 static tree cp_parser_objc_struct_declaration
2493 (cp_parser *) ;
2495 /* Utility Routines */
2497 static cp_expr cp_parser_lookup_name
2498 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2499 static tree cp_parser_lookup_name_simple
2500 (cp_parser *, tree, location_t);
2501 static tree cp_parser_maybe_treat_template_as_class
2502 (tree, bool);
2503 static bool cp_parser_check_declarator_template_parameters
2504 (cp_parser *, cp_declarator *, location_t);
2505 static bool cp_parser_check_template_parameters
2506 (cp_parser *, unsigned, location_t, cp_declarator *);
2507 static cp_expr cp_parser_simple_cast_expression
2508 (cp_parser *);
2509 static tree cp_parser_global_scope_opt
2510 (cp_parser *, bool);
2511 static bool cp_parser_constructor_declarator_p
2512 (cp_parser *, bool);
2513 static tree cp_parser_function_definition_from_specifiers_and_declarator
2514 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2515 static tree cp_parser_function_definition_after_declarator
2516 (cp_parser *, bool);
2517 static bool cp_parser_template_declaration_after_export
2518 (cp_parser *, bool);
2519 static void cp_parser_perform_template_parameter_access_checks
2520 (vec<deferred_access_check, va_gc> *);
2521 static tree cp_parser_single_declaration
2522 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2523 static cp_expr cp_parser_functional_cast
2524 (cp_parser *, tree);
2525 static tree cp_parser_save_member_function_body
2526 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2527 static tree cp_parser_save_nsdmi
2528 (cp_parser *);
2529 static tree cp_parser_enclosed_template_argument_list
2530 (cp_parser *);
2531 static void cp_parser_save_default_args
2532 (cp_parser *, tree);
2533 static void cp_parser_late_parsing_for_member
2534 (cp_parser *, tree);
2535 static tree cp_parser_late_parse_one_default_arg
2536 (cp_parser *, tree, tree, tree);
2537 static void cp_parser_late_parsing_nsdmi
2538 (cp_parser *, tree);
2539 static void cp_parser_late_parsing_default_args
2540 (cp_parser *, tree);
2541 static tree cp_parser_sizeof_operand
2542 (cp_parser *, enum rid);
2543 static tree cp_parser_trait_expr
2544 (cp_parser *, enum rid);
2545 static bool cp_parser_declares_only_class_p
2546 (cp_parser *);
2547 static void cp_parser_set_storage_class
2548 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2549 static void cp_parser_set_decl_spec_type
2550 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2551 static void set_and_check_decl_spec_loc
2552 (cp_decl_specifier_seq *decl_specs,
2553 cp_decl_spec ds, cp_token *);
2554 static bool cp_parser_friend_p
2555 (const cp_decl_specifier_seq *);
2556 static void cp_parser_required_error
2557 (cp_parser *, required_token, bool);
2558 static cp_token *cp_parser_require
2559 (cp_parser *, enum cpp_ttype, required_token);
2560 static cp_token *cp_parser_require_keyword
2561 (cp_parser *, enum rid, required_token);
2562 static bool cp_parser_token_starts_function_definition_p
2563 (cp_token *);
2564 static bool cp_parser_next_token_starts_class_definition_p
2565 (cp_parser *);
2566 static bool cp_parser_next_token_ends_template_argument_p
2567 (cp_parser *);
2568 static bool cp_parser_nth_token_starts_template_argument_list_p
2569 (cp_parser *, size_t);
2570 static enum tag_types cp_parser_token_is_class_key
2571 (cp_token *);
2572 static enum tag_types cp_parser_token_is_type_parameter_key
2573 (cp_token *);
2574 static void cp_parser_check_class_key
2575 (enum tag_types, tree type);
2576 static void cp_parser_check_access_in_redeclaration
2577 (tree type, location_t location);
2578 static bool cp_parser_optional_template_keyword
2579 (cp_parser *);
2580 static void cp_parser_pre_parsed_nested_name_specifier
2581 (cp_parser *);
2582 static bool cp_parser_cache_group
2583 (cp_parser *, enum cpp_ttype, unsigned);
2584 static tree cp_parser_cache_defarg
2585 (cp_parser *parser, bool nsdmi);
2586 static void cp_parser_parse_tentatively
2587 (cp_parser *);
2588 static void cp_parser_commit_to_tentative_parse
2589 (cp_parser *);
2590 static void cp_parser_commit_to_topmost_tentative_parse
2591 (cp_parser *);
2592 static void cp_parser_abort_tentative_parse
2593 (cp_parser *);
2594 static bool cp_parser_parse_definitely
2595 (cp_parser *);
2596 static inline bool cp_parser_parsing_tentatively
2597 (cp_parser *);
2598 static bool cp_parser_uncommitted_to_tentative_parse_p
2599 (cp_parser *);
2600 static void cp_parser_error
2601 (cp_parser *, const char *);
2602 static void cp_parser_name_lookup_error
2603 (cp_parser *, tree, tree, name_lookup_error, location_t);
2604 static bool cp_parser_simulate_error
2605 (cp_parser *);
2606 static bool cp_parser_check_type_definition
2607 (cp_parser *);
2608 static void cp_parser_check_for_definition_in_return_type
2609 (cp_declarator *, tree, location_t type_location);
2610 static void cp_parser_check_for_invalid_template_id
2611 (cp_parser *, tree, enum tag_types, location_t location);
2612 static bool cp_parser_non_integral_constant_expression
2613 (cp_parser *, non_integral_constant);
2614 static void cp_parser_diagnose_invalid_type_name
2615 (cp_parser *, tree, location_t);
2616 static bool cp_parser_parse_and_diagnose_invalid_type_name
2617 (cp_parser *);
2618 static int cp_parser_skip_to_closing_parenthesis
2619 (cp_parser *, bool, bool, bool);
2620 static void cp_parser_skip_to_end_of_statement
2621 (cp_parser *);
2622 static void cp_parser_consume_semicolon_at_end_of_statement
2623 (cp_parser *);
2624 static void cp_parser_skip_to_end_of_block_or_statement
2625 (cp_parser *);
2626 static bool cp_parser_skip_to_closing_brace
2627 (cp_parser *);
2628 static void cp_parser_skip_to_end_of_template_parameter_list
2629 (cp_parser *);
2630 static void cp_parser_skip_to_pragma_eol
2631 (cp_parser*, cp_token *);
2632 static bool cp_parser_error_occurred
2633 (cp_parser *);
2634 static bool cp_parser_allow_gnu_extensions_p
2635 (cp_parser *);
2636 static bool cp_parser_is_pure_string_literal
2637 (cp_token *);
2638 static bool cp_parser_is_string_literal
2639 (cp_token *);
2640 static bool cp_parser_is_keyword
2641 (cp_token *, enum rid);
2642 static tree cp_parser_make_typename_type
2643 (cp_parser *, tree, location_t location);
2644 static cp_declarator * cp_parser_make_indirect_declarator
2645 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2646 static bool cp_parser_compound_literal_p
2647 (cp_parser *);
2648 static bool cp_parser_array_designator_p
2649 (cp_parser *);
2650 static bool cp_parser_init_statement_p
2651 (cp_parser *);
2652 static bool cp_parser_skip_to_closing_square_bracket
2653 (cp_parser *);
2655 /* Concept-related syntactic transformations */
2657 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2658 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2660 // -------------------------------------------------------------------------- //
2661 // Unevaluated Operand Guard
2663 // Implementation of an RAII helper for unevaluated operand parsing.
2664 cp_unevaluated::cp_unevaluated ()
2666 ++cp_unevaluated_operand;
2667 ++c_inhibit_evaluation_warnings;
2670 cp_unevaluated::~cp_unevaluated ()
2672 --c_inhibit_evaluation_warnings;
2673 --cp_unevaluated_operand;
2676 // -------------------------------------------------------------------------- //
2677 // Tentative Parsing
2679 /* Returns nonzero if we are parsing tentatively. */
2681 static inline bool
2682 cp_parser_parsing_tentatively (cp_parser* parser)
2684 return parser->context->next != NULL;
2687 /* Returns nonzero if TOKEN is a string literal. */
2689 static bool
2690 cp_parser_is_pure_string_literal (cp_token* token)
2692 return (token->type == CPP_STRING ||
2693 token->type == CPP_STRING16 ||
2694 token->type == CPP_STRING32 ||
2695 token->type == CPP_WSTRING ||
2696 token->type == CPP_UTF8STRING);
2699 /* Returns nonzero if TOKEN is a string literal
2700 of a user-defined string literal. */
2702 static bool
2703 cp_parser_is_string_literal (cp_token* token)
2705 return (cp_parser_is_pure_string_literal (token) ||
2706 token->type == CPP_STRING_USERDEF ||
2707 token->type == CPP_STRING16_USERDEF ||
2708 token->type == CPP_STRING32_USERDEF ||
2709 token->type == CPP_WSTRING_USERDEF ||
2710 token->type == CPP_UTF8STRING_USERDEF);
2713 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2715 static bool
2716 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2718 return token->keyword == keyword;
2721 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2722 PRAGMA_NONE. */
2724 static enum pragma_kind
2725 cp_parser_pragma_kind (cp_token *token)
2727 if (token->type != CPP_PRAGMA)
2728 return PRAGMA_NONE;
2729 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2730 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2733 /* Helper function for cp_parser_error.
2734 Having peeked a token of kind TOK1_KIND that might signify
2735 a conflict marker, peek successor tokens to determine
2736 if we actually do have a conflict marker.
2737 Specifically, we consider a run of 7 '<', '=' or '>' characters
2738 at the start of a line as a conflict marker.
2739 These come through the lexer as three pairs and a single,
2740 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2741 If it returns true, *OUT_LOC is written to with the location/range
2742 of the marker. */
2744 static bool
2745 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2746 location_t *out_loc)
2748 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2749 if (token2->type != tok1_kind)
2750 return false;
2751 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2752 if (token3->type != tok1_kind)
2753 return false;
2754 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2755 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2756 return false;
2758 /* It must be at the start of the line. */
2759 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2760 if (LOCATION_COLUMN (start_loc) != 1)
2761 return false;
2763 /* We have a conflict marker. Construct a location of the form:
2764 <<<<<<<
2765 ^~~~~~~
2766 with start == caret, finishing at the end of the marker. */
2767 location_t finish_loc = get_finish (token4->location);
2768 *out_loc = make_location (start_loc, start_loc, finish_loc);
2770 return true;
2773 /* If not parsing tentatively, issue a diagnostic of the form
2774 FILE:LINE: MESSAGE before TOKEN
2775 where TOKEN is the next token in the input stream. MESSAGE
2776 (specified by the caller) is usually of the form "expected
2777 OTHER-TOKEN". */
2779 static void
2780 cp_parser_error (cp_parser* parser, const char* gmsgid)
2782 if (!cp_parser_simulate_error (parser))
2784 cp_token *token = cp_lexer_peek_token (parser->lexer);
2785 /* This diagnostic makes more sense if it is tagged to the line
2786 of the token we just peeked at. */
2787 cp_lexer_set_source_position_from_token (token);
2789 if (token->type == CPP_PRAGMA)
2791 error_at (token->location,
2792 "%<#pragma%> is not allowed here");
2793 cp_parser_skip_to_pragma_eol (parser, token);
2794 return;
2797 /* If this is actually a conflict marker, report it as such. */
2798 if (token->type == CPP_LSHIFT
2799 || token->type == CPP_RSHIFT
2800 || token->type == CPP_EQ_EQ)
2802 location_t loc;
2803 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2805 error_at (loc, "version control conflict marker in file");
2806 return;
2810 c_parse_error (gmsgid,
2811 /* Because c_parser_error does not understand
2812 CPP_KEYWORD, keywords are treated like
2813 identifiers. */
2814 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2815 token->u.value, token->flags);
2819 /* Issue an error about name-lookup failing. NAME is the
2820 IDENTIFIER_NODE DECL is the result of
2821 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2822 the thing that we hoped to find. */
2824 static void
2825 cp_parser_name_lookup_error (cp_parser* parser,
2826 tree name,
2827 tree decl,
2828 name_lookup_error desired,
2829 location_t location)
2831 /* If name lookup completely failed, tell the user that NAME was not
2832 declared. */
2833 if (decl == error_mark_node)
2835 if (parser->scope && parser->scope != global_namespace)
2836 error_at (location, "%<%E::%E%> has not been declared",
2837 parser->scope, name);
2838 else if (parser->scope == global_namespace)
2839 error_at (location, "%<::%E%> has not been declared", name);
2840 else if (parser->object_scope
2841 && !CLASS_TYPE_P (parser->object_scope))
2842 error_at (location, "request for member %qE in non-class type %qT",
2843 name, parser->object_scope);
2844 else if (parser->object_scope)
2845 error_at (location, "%<%T::%E%> has not been declared",
2846 parser->object_scope, name);
2847 else
2848 error_at (location, "%qE has not been declared", name);
2850 else if (parser->scope && parser->scope != global_namespace)
2852 switch (desired)
2854 case NLE_TYPE:
2855 error_at (location, "%<%E::%E%> is not a type",
2856 parser->scope, name);
2857 break;
2858 case NLE_CXX98:
2859 error_at (location, "%<%E::%E%> is not a class or namespace",
2860 parser->scope, name);
2861 break;
2862 case NLE_NOT_CXX98:
2863 error_at (location,
2864 "%<%E::%E%> is not a class, namespace, or enumeration",
2865 parser->scope, name);
2866 break;
2867 default:
2868 gcc_unreachable ();
2872 else if (parser->scope == global_namespace)
2874 switch (desired)
2876 case NLE_TYPE:
2877 error_at (location, "%<::%E%> is not a type", name);
2878 break;
2879 case NLE_CXX98:
2880 error_at (location, "%<::%E%> is not a class or namespace", name);
2881 break;
2882 case NLE_NOT_CXX98:
2883 error_at (location,
2884 "%<::%E%> is not a class, namespace, or enumeration",
2885 name);
2886 break;
2887 default:
2888 gcc_unreachable ();
2891 else
2893 switch (desired)
2895 case NLE_TYPE:
2896 error_at (location, "%qE is not a type", name);
2897 break;
2898 case NLE_CXX98:
2899 error_at (location, "%qE is not a class or namespace", name);
2900 break;
2901 case NLE_NOT_CXX98:
2902 error_at (location,
2903 "%qE is not a class, namespace, or enumeration", name);
2904 break;
2905 default:
2906 gcc_unreachable ();
2911 /* If we are parsing tentatively, remember that an error has occurred
2912 during this tentative parse. Returns true if the error was
2913 simulated; false if a message should be issued by the caller. */
2915 static bool
2916 cp_parser_simulate_error (cp_parser* parser)
2918 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2920 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2921 return true;
2923 return false;
2926 /* This function is called when a type is defined. If type
2927 definitions are forbidden at this point, an error message is
2928 issued. */
2930 static bool
2931 cp_parser_check_type_definition (cp_parser* parser)
2933 /* If types are forbidden here, issue a message. */
2934 if (parser->type_definition_forbidden_message)
2936 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2937 in the message need to be interpreted. */
2938 error (parser->type_definition_forbidden_message);
2939 return false;
2941 return true;
2944 /* This function is called when the DECLARATOR is processed. The TYPE
2945 was a type defined in the decl-specifiers. If it is invalid to
2946 define a type in the decl-specifiers for DECLARATOR, an error is
2947 issued. TYPE_LOCATION is the location of TYPE and is used
2948 for error reporting. */
2950 static void
2951 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2952 tree type, location_t type_location)
2954 /* [dcl.fct] forbids type definitions in return types.
2955 Unfortunately, it's not easy to know whether or not we are
2956 processing a return type until after the fact. */
2957 while (declarator
2958 && (declarator->kind == cdk_pointer
2959 || declarator->kind == cdk_reference
2960 || declarator->kind == cdk_ptrmem))
2961 declarator = declarator->declarator;
2962 if (declarator
2963 && declarator->kind == cdk_function)
2965 error_at (type_location,
2966 "new types may not be defined in a return type");
2967 inform (type_location,
2968 "(perhaps a semicolon is missing after the definition of %qT)",
2969 type);
2973 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2974 "<" in any valid C++ program. If the next token is indeed "<",
2975 issue a message warning the user about what appears to be an
2976 invalid attempt to form a template-id. LOCATION is the location
2977 of the type-specifier (TYPE) */
2979 static void
2980 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2981 tree type,
2982 enum tag_types tag_type,
2983 location_t location)
2985 cp_token_position start = 0;
2987 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2989 if (TYPE_P (type))
2990 error_at (location, "%qT is not a template", type);
2991 else if (identifier_p (type))
2993 if (tag_type != none_type)
2994 error_at (location, "%qE is not a class template", type);
2995 else
2996 error_at (location, "%qE is not a template", type);
2998 else
2999 error_at (location, "invalid template-id");
3000 /* Remember the location of the invalid "<". */
3001 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3002 start = cp_lexer_token_position (parser->lexer, true);
3003 /* Consume the "<". */
3004 cp_lexer_consume_token (parser->lexer);
3005 /* Parse the template arguments. */
3006 cp_parser_enclosed_template_argument_list (parser);
3007 /* Permanently remove the invalid template arguments so that
3008 this error message is not issued again. */
3009 if (start)
3010 cp_lexer_purge_tokens_after (parser->lexer, start);
3014 /* If parsing an integral constant-expression, issue an error message
3015 about the fact that THING appeared and return true. Otherwise,
3016 return false. In either case, set
3017 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3019 static bool
3020 cp_parser_non_integral_constant_expression (cp_parser *parser,
3021 non_integral_constant thing)
3023 parser->non_integral_constant_expression_p = true;
3024 if (parser->integral_constant_expression_p)
3026 if (!parser->allow_non_integral_constant_expression_p)
3028 const char *msg = NULL;
3029 switch (thing)
3031 case NIC_FLOAT:
3032 pedwarn (input_location, OPT_Wpedantic,
3033 "ISO C++ forbids using a floating-point literal "
3034 "in a constant-expression");
3035 return true;
3036 case NIC_CAST:
3037 error ("a cast to a type other than an integral or "
3038 "enumeration type cannot appear in a "
3039 "constant-expression");
3040 return true;
3041 case NIC_TYPEID:
3042 error ("%<typeid%> operator "
3043 "cannot appear in a constant-expression");
3044 return true;
3045 case NIC_NCC:
3046 error ("non-constant compound literals "
3047 "cannot appear in a constant-expression");
3048 return true;
3049 case NIC_FUNC_CALL:
3050 error ("a function call "
3051 "cannot appear in a constant-expression");
3052 return true;
3053 case NIC_INC:
3054 error ("an increment "
3055 "cannot appear in a constant-expression");
3056 return true;
3057 case NIC_DEC:
3058 error ("an decrement "
3059 "cannot appear in a constant-expression");
3060 return true;
3061 case NIC_ARRAY_REF:
3062 error ("an array reference "
3063 "cannot appear in a constant-expression");
3064 return true;
3065 case NIC_ADDR_LABEL:
3066 error ("the address of a label "
3067 "cannot appear in a constant-expression");
3068 return true;
3069 case NIC_OVERLOADED:
3070 error ("calls to overloaded operators "
3071 "cannot appear in a constant-expression");
3072 return true;
3073 case NIC_ASSIGNMENT:
3074 error ("an assignment cannot appear in a constant-expression");
3075 return true;
3076 case NIC_COMMA:
3077 error ("a comma operator "
3078 "cannot appear in a constant-expression");
3079 return true;
3080 case NIC_CONSTRUCTOR:
3081 error ("a call to a constructor "
3082 "cannot appear in a constant-expression");
3083 return true;
3084 case NIC_TRANSACTION:
3085 error ("a transaction expression "
3086 "cannot appear in a constant-expression");
3087 return true;
3088 case NIC_THIS:
3089 msg = "this";
3090 break;
3091 case NIC_FUNC_NAME:
3092 msg = "__FUNCTION__";
3093 break;
3094 case NIC_PRETTY_FUNC:
3095 msg = "__PRETTY_FUNCTION__";
3096 break;
3097 case NIC_C99_FUNC:
3098 msg = "__func__";
3099 break;
3100 case NIC_VA_ARG:
3101 msg = "va_arg";
3102 break;
3103 case NIC_ARROW:
3104 msg = "->";
3105 break;
3106 case NIC_POINT:
3107 msg = ".";
3108 break;
3109 case NIC_STAR:
3110 msg = "*";
3111 break;
3112 case NIC_ADDR:
3113 msg = "&";
3114 break;
3115 case NIC_PREINCREMENT:
3116 msg = "++";
3117 break;
3118 case NIC_PREDECREMENT:
3119 msg = "--";
3120 break;
3121 case NIC_NEW:
3122 msg = "new";
3123 break;
3124 case NIC_DEL:
3125 msg = "delete";
3126 break;
3127 default:
3128 gcc_unreachable ();
3130 if (msg)
3131 error ("%qs cannot appear in a constant-expression", msg);
3132 return true;
3135 return false;
3138 /* Emit a diagnostic for an invalid type name. This function commits
3139 to the current active tentative parse, if any. (Otherwise, the
3140 problematic construct might be encountered again later, resulting
3141 in duplicate error messages.) LOCATION is the location of ID. */
3143 static void
3144 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3145 location_t location)
3147 tree decl, ambiguous_decls;
3148 cp_parser_commit_to_tentative_parse (parser);
3149 /* Try to lookup the identifier. */
3150 decl = cp_parser_lookup_name (parser, id, none_type,
3151 /*is_template=*/false,
3152 /*is_namespace=*/false,
3153 /*check_dependency=*/true,
3154 &ambiguous_decls, location);
3155 if (ambiguous_decls)
3156 /* If the lookup was ambiguous, an error will already have
3157 been issued. */
3158 return;
3159 /* If the lookup found a template-name, it means that the user forgot
3160 to specify an argument list. Emit a useful error message. */
3161 if (DECL_TYPE_TEMPLATE_P (decl))
3163 error_at (location,
3164 "invalid use of template-name %qE without an argument list",
3165 decl);
3166 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx1z)
3167 inform (location, "class template argument deduction is only available "
3168 "with -std=c++1z or -std=gnu++1z");
3169 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3171 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3172 error_at (location, "invalid use of destructor %qD as a type", id);
3173 else if (TREE_CODE (decl) == TYPE_DECL)
3174 /* Something like 'unsigned A a;' */
3175 error_at (location, "invalid combination of multiple type-specifiers");
3176 else if (!parser->scope)
3178 /* Issue an error message. */
3179 const char *suggestion = NULL;
3180 if (TREE_CODE (id) == IDENTIFIER_NODE)
3181 suggestion = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME);
3182 if (suggestion)
3184 gcc_rich_location richloc (location);
3185 richloc.add_fixit_replace (suggestion);
3186 error_at_rich_loc (&richloc,
3187 "%qE does not name a type; did you mean %qs?",
3188 id, suggestion);
3190 else
3191 error_at (location, "%qE does not name a type", id);
3192 /* If we're in a template class, it's possible that the user was
3193 referring to a type from a base class. For example:
3195 template <typename T> struct A { typedef T X; };
3196 template <typename T> struct B : public A<T> { X x; };
3198 The user should have said "typename A<T>::X". */
3199 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3200 inform (location, "C++11 %<constexpr%> only available with "
3201 "-std=c++11 or -std=gnu++11");
3202 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3203 inform (location, "C++11 %<noexcept%> only available with "
3204 "-std=c++11 or -std=gnu++11");
3205 else if (cxx_dialect < cxx11
3206 && TREE_CODE (id) == IDENTIFIER_NODE
3207 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
3208 inform (location, "C++11 %<thread_local%> only available with "
3209 "-std=c++11 or -std=gnu++11");
3210 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3211 inform (location, "%<concept%> only available with -fconcepts");
3212 else if (processing_template_decl && current_class_type
3213 && TYPE_BINFO (current_class_type))
3215 tree b;
3217 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3219 b = TREE_CHAIN (b))
3221 tree base_type = BINFO_TYPE (b);
3222 if (CLASS_TYPE_P (base_type)
3223 && dependent_type_p (base_type))
3225 tree field;
3226 /* Go from a particular instantiation of the
3227 template (which will have an empty TYPE_FIELDs),
3228 to the main version. */
3229 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3230 for (field = TYPE_FIELDS (base_type);
3231 field;
3232 field = DECL_CHAIN (field))
3233 if (TREE_CODE (field) == TYPE_DECL
3234 && DECL_NAME (field) == id)
3236 inform (location,
3237 "(perhaps %<typename %T::%E%> was intended)",
3238 BINFO_TYPE (b), id);
3239 break;
3241 if (field)
3242 break;
3247 /* Here we diagnose qualified-ids where the scope is actually correct,
3248 but the identifier does not resolve to a valid type name. */
3249 else if (parser->scope != error_mark_node)
3251 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3253 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3254 error_at (location_of (id),
3255 "%qE in namespace %qE does not name a template type",
3256 id, parser->scope);
3257 else
3258 error_at (location_of (id),
3259 "%qE in namespace %qE does not name a type",
3260 id, parser->scope);
3261 if (DECL_P (decl))
3262 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3264 else if (CLASS_TYPE_P (parser->scope)
3265 && constructor_name_p (id, parser->scope))
3267 /* A<T>::A<T>() */
3268 error_at (location, "%<%T::%E%> names the constructor, not"
3269 " the type", parser->scope, id);
3270 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3271 error_at (location, "and %qT has no template constructors",
3272 parser->scope);
3274 else if (TYPE_P (parser->scope)
3275 && dependent_scope_p (parser->scope))
3276 error_at (location, "need %<typename%> before %<%T::%E%> because "
3277 "%qT is a dependent scope",
3278 parser->scope, id, parser->scope);
3279 else if (TYPE_P (parser->scope))
3281 if (!COMPLETE_TYPE_P (parser->scope))
3282 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3283 parser->scope);
3284 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3285 error_at (location_of (id),
3286 "%qE in %q#T does not name a template type",
3287 id, parser->scope);
3288 else
3289 error_at (location_of (id),
3290 "%qE in %q#T does not name a type",
3291 id, parser->scope);
3292 if (DECL_P (decl))
3293 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3295 else
3296 gcc_unreachable ();
3300 /* Check for a common situation where a type-name should be present,
3301 but is not, and issue a sensible error message. Returns true if an
3302 invalid type-name was detected.
3304 The situation handled by this function are variable declarations of the
3305 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3306 Usually, `ID' should name a type, but if we got here it means that it
3307 does not. We try to emit the best possible error message depending on
3308 how exactly the id-expression looks like. */
3310 static bool
3311 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3313 tree id;
3314 cp_token *token = cp_lexer_peek_token (parser->lexer);
3316 /* Avoid duplicate error about ambiguous lookup. */
3317 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3319 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3320 if (next->type == CPP_NAME && next->error_reported)
3321 goto out;
3324 cp_parser_parse_tentatively (parser);
3325 id = cp_parser_id_expression (parser,
3326 /*template_keyword_p=*/false,
3327 /*check_dependency_p=*/true,
3328 /*template_p=*/NULL,
3329 /*declarator_p=*/true,
3330 /*optional_p=*/false);
3331 /* If the next token is a (, this is a function with no explicit return
3332 type, i.e. constructor, destructor or conversion op. */
3333 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3334 || TREE_CODE (id) == TYPE_DECL)
3336 cp_parser_abort_tentative_parse (parser);
3337 return false;
3339 if (!cp_parser_parse_definitely (parser))
3340 return false;
3342 /* Emit a diagnostic for the invalid type. */
3343 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3344 out:
3345 /* If we aren't in the middle of a declarator (i.e. in a
3346 parameter-declaration-clause), skip to the end of the declaration;
3347 there's no point in trying to process it. */
3348 if (!parser->in_declarator_p)
3349 cp_parser_skip_to_end_of_block_or_statement (parser);
3350 return true;
3353 /* Consume tokens up to, and including, the next non-nested closing `)'.
3354 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3355 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3356 found an unnested token of that type. */
3358 static int
3359 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3360 bool recovering,
3361 cpp_ttype or_ttype,
3362 bool consume_paren)
3364 unsigned paren_depth = 0;
3365 unsigned brace_depth = 0;
3366 unsigned square_depth = 0;
3368 if (recovering && or_ttype == CPP_EOF
3369 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3370 return 0;
3372 while (true)
3374 cp_token * token = cp_lexer_peek_token (parser->lexer);
3376 /* Have we found what we're looking for before the closing paren? */
3377 if (token->type == or_ttype && or_ttype != CPP_EOF
3378 && !brace_depth && !paren_depth && !square_depth)
3379 return -1;
3381 switch (token->type)
3383 case CPP_EOF:
3384 case CPP_PRAGMA_EOL:
3385 /* If we've run out of tokens, then there is no closing `)'. */
3386 return 0;
3388 /* This is good for lambda expression capture-lists. */
3389 case CPP_OPEN_SQUARE:
3390 ++square_depth;
3391 break;
3392 case CPP_CLOSE_SQUARE:
3393 if (!square_depth--)
3394 return 0;
3395 break;
3397 case CPP_SEMICOLON:
3398 /* This matches the processing in skip_to_end_of_statement. */
3399 if (!brace_depth)
3400 return 0;
3401 break;
3403 case CPP_OPEN_BRACE:
3404 ++brace_depth;
3405 break;
3406 case CPP_CLOSE_BRACE:
3407 if (!brace_depth--)
3408 return 0;
3409 break;
3411 case CPP_OPEN_PAREN:
3412 if (!brace_depth)
3413 ++paren_depth;
3414 break;
3416 case CPP_CLOSE_PAREN:
3417 if (!brace_depth && !paren_depth--)
3419 if (consume_paren)
3420 cp_lexer_consume_token (parser->lexer);
3421 return 1;
3423 break;
3425 default:
3426 break;
3429 /* Consume the token. */
3430 cp_lexer_consume_token (parser->lexer);
3434 /* Consume tokens up to, and including, the next non-nested closing `)'.
3435 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3436 are doing error recovery. Returns -1 if OR_COMMA is true and we
3437 found an unnested token of that type. */
3439 static int
3440 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3441 bool recovering,
3442 bool or_comma,
3443 bool consume_paren)
3445 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3446 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3447 ttype, consume_paren);
3450 /* Consume tokens until we reach the end of the current statement.
3451 Normally, that will be just before consuming a `;'. However, if a
3452 non-nested `}' comes first, then we stop before consuming that. */
3454 static void
3455 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3457 unsigned nesting_depth = 0;
3459 /* Unwind generic function template scope if necessary. */
3460 if (parser->fully_implicit_function_template_p)
3461 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3463 while (true)
3465 cp_token *token = cp_lexer_peek_token (parser->lexer);
3467 switch (token->type)
3469 case CPP_EOF:
3470 case CPP_PRAGMA_EOL:
3471 /* If we've run out of tokens, stop. */
3472 return;
3474 case CPP_SEMICOLON:
3475 /* If the next token is a `;', we have reached the end of the
3476 statement. */
3477 if (!nesting_depth)
3478 return;
3479 break;
3481 case CPP_CLOSE_BRACE:
3482 /* If this is a non-nested '}', stop before consuming it.
3483 That way, when confronted with something like:
3485 { 3 + }
3487 we stop before consuming the closing '}', even though we
3488 have not yet reached a `;'. */
3489 if (nesting_depth == 0)
3490 return;
3492 /* If it is the closing '}' for a block that we have
3493 scanned, stop -- but only after consuming the token.
3494 That way given:
3496 void f g () { ... }
3497 typedef int I;
3499 we will stop after the body of the erroneously declared
3500 function, but before consuming the following `typedef'
3501 declaration. */
3502 if (--nesting_depth == 0)
3504 cp_lexer_consume_token (parser->lexer);
3505 return;
3507 break;
3509 case CPP_OPEN_BRACE:
3510 ++nesting_depth;
3511 break;
3513 default:
3514 break;
3517 /* Consume the token. */
3518 cp_lexer_consume_token (parser->lexer);
3522 /* This function is called at the end of a statement or declaration.
3523 If the next token is a semicolon, it is consumed; otherwise, error
3524 recovery is attempted. */
3526 static void
3527 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3529 /* Look for the trailing `;'. */
3530 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3532 /* If there is additional (erroneous) input, skip to the end of
3533 the statement. */
3534 cp_parser_skip_to_end_of_statement (parser);
3535 /* If the next token is now a `;', consume it. */
3536 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3537 cp_lexer_consume_token (parser->lexer);
3541 /* Skip tokens until we have consumed an entire block, or until we
3542 have consumed a non-nested `;'. */
3544 static void
3545 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3547 int nesting_depth = 0;
3549 /* Unwind generic function template scope if necessary. */
3550 if (parser->fully_implicit_function_template_p)
3551 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3553 while (nesting_depth >= 0)
3555 cp_token *token = cp_lexer_peek_token (parser->lexer);
3557 switch (token->type)
3559 case CPP_EOF:
3560 case CPP_PRAGMA_EOL:
3561 /* If we've run out of tokens, stop. */
3562 return;
3564 case CPP_SEMICOLON:
3565 /* Stop if this is an unnested ';'. */
3566 if (!nesting_depth)
3567 nesting_depth = -1;
3568 break;
3570 case CPP_CLOSE_BRACE:
3571 /* Stop if this is an unnested '}', or closes the outermost
3572 nesting level. */
3573 nesting_depth--;
3574 if (nesting_depth < 0)
3575 return;
3576 if (!nesting_depth)
3577 nesting_depth = -1;
3578 break;
3580 case CPP_OPEN_BRACE:
3581 /* Nest. */
3582 nesting_depth++;
3583 break;
3585 default:
3586 break;
3589 /* Consume the token. */
3590 cp_lexer_consume_token (parser->lexer);
3594 /* Skip tokens until a non-nested closing curly brace is the next
3595 token, or there are no more tokens. Return true in the first case,
3596 false otherwise. */
3598 static bool
3599 cp_parser_skip_to_closing_brace (cp_parser *parser)
3601 unsigned nesting_depth = 0;
3603 while (true)
3605 cp_token *token = cp_lexer_peek_token (parser->lexer);
3607 switch (token->type)
3609 case CPP_EOF:
3610 case CPP_PRAGMA_EOL:
3611 /* If we've run out of tokens, stop. */
3612 return false;
3614 case CPP_CLOSE_BRACE:
3615 /* If the next token is a non-nested `}', then we have reached
3616 the end of the current block. */
3617 if (nesting_depth-- == 0)
3618 return true;
3619 break;
3621 case CPP_OPEN_BRACE:
3622 /* If it the next token is a `{', then we are entering a new
3623 block. Consume the entire block. */
3624 ++nesting_depth;
3625 break;
3627 default:
3628 break;
3631 /* Consume the token. */
3632 cp_lexer_consume_token (parser->lexer);
3636 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3637 parameter is the PRAGMA token, allowing us to purge the entire pragma
3638 sequence. */
3640 static void
3641 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3643 cp_token *token;
3645 parser->lexer->in_pragma = false;
3648 token = cp_lexer_consume_token (parser->lexer);
3649 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3651 /* Ensure that the pragma is not parsed again. */
3652 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3655 /* Require pragma end of line, resyncing with it as necessary. The
3656 arguments are as for cp_parser_skip_to_pragma_eol. */
3658 static void
3659 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3661 parser->lexer->in_pragma = false;
3662 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3663 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3666 /* This is a simple wrapper around make_typename_type. When the id is
3667 an unresolved identifier node, we can provide a superior diagnostic
3668 using cp_parser_diagnose_invalid_type_name. */
3670 static tree
3671 cp_parser_make_typename_type (cp_parser *parser, tree id,
3672 location_t id_location)
3674 tree result;
3675 if (identifier_p (id))
3677 result = make_typename_type (parser->scope, id, typename_type,
3678 /*complain=*/tf_none);
3679 if (result == error_mark_node)
3680 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3681 return result;
3683 return make_typename_type (parser->scope, id, typename_type, tf_error);
3686 /* This is a wrapper around the
3687 make_{pointer,ptrmem,reference}_declarator functions that decides
3688 which one to call based on the CODE and CLASS_TYPE arguments. The
3689 CODE argument should be one of the values returned by
3690 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3691 appertain to the pointer or reference. */
3693 static cp_declarator *
3694 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3695 cp_cv_quals cv_qualifiers,
3696 cp_declarator *target,
3697 tree attributes)
3699 if (code == ERROR_MARK)
3700 return cp_error_declarator;
3702 if (code == INDIRECT_REF)
3703 if (class_type == NULL_TREE)
3704 return make_pointer_declarator (cv_qualifiers, target, attributes);
3705 else
3706 return make_ptrmem_declarator (cv_qualifiers, class_type,
3707 target, attributes);
3708 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3709 return make_reference_declarator (cv_qualifiers, target,
3710 false, attributes);
3711 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3712 return make_reference_declarator (cv_qualifiers, target,
3713 true, attributes);
3714 gcc_unreachable ();
3717 /* Create a new C++ parser. */
3719 static cp_parser *
3720 cp_parser_new (void)
3722 cp_parser *parser;
3723 cp_lexer *lexer;
3724 unsigned i;
3726 /* cp_lexer_new_main is called before doing GC allocation because
3727 cp_lexer_new_main might load a PCH file. */
3728 lexer = cp_lexer_new_main ();
3730 /* Initialize the binops_by_token so that we can get the tree
3731 directly from the token. */
3732 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3733 binops_by_token[binops[i].token_type] = binops[i];
3735 parser = ggc_cleared_alloc<cp_parser> ();
3736 parser->lexer = lexer;
3737 parser->context = cp_parser_context_new (NULL);
3739 /* For now, we always accept GNU extensions. */
3740 parser->allow_gnu_extensions_p = 1;
3742 /* The `>' token is a greater-than operator, not the end of a
3743 template-id. */
3744 parser->greater_than_is_operator_p = true;
3746 parser->default_arg_ok_p = true;
3748 /* We are not parsing a constant-expression. */
3749 parser->integral_constant_expression_p = false;
3750 parser->allow_non_integral_constant_expression_p = false;
3751 parser->non_integral_constant_expression_p = false;
3753 /* Local variable names are not forbidden. */
3754 parser->local_variables_forbidden_p = false;
3756 /* We are not processing an `extern "C"' declaration. */
3757 parser->in_unbraced_linkage_specification_p = false;
3759 /* We are not processing a declarator. */
3760 parser->in_declarator_p = false;
3762 /* We are not processing a template-argument-list. */
3763 parser->in_template_argument_list_p = false;
3765 /* We are not in an iteration statement. */
3766 parser->in_statement = 0;
3768 /* We are not in a switch statement. */
3769 parser->in_switch_statement_p = false;
3771 /* We are not parsing a type-id inside an expression. */
3772 parser->in_type_id_in_expr_p = false;
3774 /* Declarations aren't implicitly extern "C". */
3775 parser->implicit_extern_c = false;
3777 /* String literals should be translated to the execution character set. */
3778 parser->translate_strings_p = true;
3780 /* We are not parsing a function body. */
3781 parser->in_function_body = false;
3783 /* We can correct until told otherwise. */
3784 parser->colon_corrects_to_scope_p = true;
3786 /* The unparsed function queue is empty. */
3787 push_unparsed_function_queues (parser);
3789 /* There are no classes being defined. */
3790 parser->num_classes_being_defined = 0;
3792 /* No template parameters apply. */
3793 parser->num_template_parameter_lists = 0;
3795 /* Special parsing data structures. */
3796 parser->omp_declare_simd = NULL;
3797 parser->cilk_simd_fn_info = NULL;
3798 parser->oacc_routine = NULL;
3800 /* Not declaring an implicit function template. */
3801 parser->auto_is_implicit_function_template_parm_p = false;
3802 parser->fully_implicit_function_template_p = false;
3803 parser->implicit_template_parms = 0;
3804 parser->implicit_template_scope = 0;
3806 /* Allow constrained-type-specifiers. */
3807 parser->prevent_constrained_type_specifiers = 0;
3809 return parser;
3812 /* Create a cp_lexer structure which will emit the tokens in CACHE
3813 and push it onto the parser's lexer stack. This is used for delayed
3814 parsing of in-class method bodies and default arguments, and should
3815 not be confused with tentative parsing. */
3816 static void
3817 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3819 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3820 lexer->next = parser->lexer;
3821 parser->lexer = lexer;
3823 /* Move the current source position to that of the first token in the
3824 new lexer. */
3825 cp_lexer_set_source_position_from_token (lexer->next_token);
3828 /* Pop the top lexer off the parser stack. This is never used for the
3829 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3830 static void
3831 cp_parser_pop_lexer (cp_parser *parser)
3833 cp_lexer *lexer = parser->lexer;
3834 parser->lexer = lexer->next;
3835 cp_lexer_destroy (lexer);
3837 /* Put the current source position back where it was before this
3838 lexer was pushed. */
3839 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3842 /* Lexical conventions [gram.lex] */
3844 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3845 identifier. */
3847 static cp_expr
3848 cp_parser_identifier (cp_parser* parser)
3850 cp_token *token;
3852 /* Look for the identifier. */
3853 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3854 /* Return the value. */
3855 if (token)
3856 return cp_expr (token->u.value, token->location);
3857 else
3858 return error_mark_node;
3861 /* Parse a sequence of adjacent string constants. Returns a
3862 TREE_STRING representing the combined, nul-terminated string
3863 constant. If TRANSLATE is true, translate the string to the
3864 execution character set. If WIDE_OK is true, a wide string is
3865 invalid here.
3867 C++98 [lex.string] says that if a narrow string literal token is
3868 adjacent to a wide string literal token, the behavior is undefined.
3869 However, C99 6.4.5p4 says that this results in a wide string literal.
3870 We follow C99 here, for consistency with the C front end.
3872 This code is largely lifted from lex_string() in c-lex.c.
3874 FUTURE: ObjC++ will need to handle @-strings here. */
3875 static cp_expr
3876 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3877 bool lookup_udlit = true)
3879 tree value;
3880 size_t count;
3881 struct obstack str_ob;
3882 cpp_string str, istr, *strs;
3883 cp_token *tok;
3884 enum cpp_ttype type, curr_type;
3885 int have_suffix_p = 0;
3886 tree string_tree;
3887 tree suffix_id = NULL_TREE;
3888 bool curr_tok_is_userdef_p = false;
3890 tok = cp_lexer_peek_token (parser->lexer);
3891 if (!cp_parser_is_string_literal (tok))
3893 cp_parser_error (parser, "expected string-literal");
3894 return error_mark_node;
3897 location_t loc = tok->location;
3899 if (cpp_userdef_string_p (tok->type))
3901 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3902 curr_type = cpp_userdef_string_remove_type (tok->type);
3903 curr_tok_is_userdef_p = true;
3905 else
3907 string_tree = tok->u.value;
3908 curr_type = tok->type;
3910 type = curr_type;
3912 /* Try to avoid the overhead of creating and destroying an obstack
3913 for the common case of just one string. */
3914 if (!cp_parser_is_string_literal
3915 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3917 cp_lexer_consume_token (parser->lexer);
3919 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3920 str.len = TREE_STRING_LENGTH (string_tree);
3921 count = 1;
3923 if (curr_tok_is_userdef_p)
3925 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3926 have_suffix_p = 1;
3927 curr_type = cpp_userdef_string_remove_type (tok->type);
3929 else
3930 curr_type = tok->type;
3932 strs = &str;
3934 else
3936 location_t last_tok_loc = tok->location;
3937 gcc_obstack_init (&str_ob);
3938 count = 0;
3942 cp_lexer_consume_token (parser->lexer);
3943 count++;
3944 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3945 str.len = TREE_STRING_LENGTH (string_tree);
3947 if (curr_tok_is_userdef_p)
3949 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3950 if (have_suffix_p == 0)
3952 suffix_id = curr_suffix_id;
3953 have_suffix_p = 1;
3955 else if (have_suffix_p == 1
3956 && curr_suffix_id != suffix_id)
3958 error ("inconsistent user-defined literal suffixes"
3959 " %qD and %qD in string literal",
3960 suffix_id, curr_suffix_id);
3961 have_suffix_p = -1;
3963 curr_type = cpp_userdef_string_remove_type (tok->type);
3965 else
3966 curr_type = tok->type;
3968 if (type != curr_type)
3970 if (type == CPP_STRING)
3971 type = curr_type;
3972 else if (curr_type != CPP_STRING)
3974 rich_location rich_loc (line_table, tok->location);
3975 rich_loc.add_range (last_tok_loc, false);
3976 error_at_rich_loc (&rich_loc,
3977 "unsupported non-standard concatenation "
3978 "of string literals");
3982 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3984 last_tok_loc = tok->location;
3986 tok = cp_lexer_peek_token (parser->lexer);
3987 if (cpp_userdef_string_p (tok->type))
3989 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3990 curr_type = cpp_userdef_string_remove_type (tok->type);
3991 curr_tok_is_userdef_p = true;
3993 else
3995 string_tree = tok->u.value;
3996 curr_type = tok->type;
3997 curr_tok_is_userdef_p = false;
4000 while (cp_parser_is_string_literal (tok));
4002 /* A string literal built by concatenation has its caret=start at
4003 the start of the initial string, and its finish at the finish of
4004 the final string literal. */
4005 loc = make_location (loc, loc, get_finish (last_tok_loc));
4007 strs = (cpp_string *) obstack_finish (&str_ob);
4010 if (type != CPP_STRING && !wide_ok)
4012 cp_parser_error (parser, "a wide string is invalid in this context");
4013 type = CPP_STRING;
4016 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4017 (parse_in, strs, count, &istr, type))
4019 value = build_string (istr.len, (const char *)istr.text);
4020 free (CONST_CAST (unsigned char *, istr.text));
4022 switch (type)
4024 default:
4025 case CPP_STRING:
4026 case CPP_UTF8STRING:
4027 TREE_TYPE (value) = char_array_type_node;
4028 break;
4029 case CPP_STRING16:
4030 TREE_TYPE (value) = char16_array_type_node;
4031 break;
4032 case CPP_STRING32:
4033 TREE_TYPE (value) = char32_array_type_node;
4034 break;
4035 case CPP_WSTRING:
4036 TREE_TYPE (value) = wchar_array_type_node;
4037 break;
4040 value = fix_string_type (value);
4042 if (have_suffix_p)
4044 tree literal = build_userdef_literal (suffix_id, value,
4045 OT_NONE, NULL_TREE);
4046 if (lookup_udlit)
4047 value = cp_parser_userdef_string_literal (literal);
4048 else
4049 value = literal;
4052 else
4053 /* cpp_interpret_string has issued an error. */
4054 value = error_mark_node;
4056 if (count > 1)
4057 obstack_free (&str_ob, 0);
4059 return cp_expr (value, loc);
4062 /* Look up a literal operator with the name and the exact arguments. */
4064 static tree
4065 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4067 tree decl, fns;
4068 decl = lookup_name (name);
4069 if (!decl || !is_overloaded_fn (decl))
4070 return error_mark_node;
4072 for (fns = decl; fns; fns = OVL_NEXT (fns))
4074 unsigned int ix;
4075 bool found = true;
4076 tree fn = OVL_CURRENT (fns);
4077 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4078 if (parmtypes != NULL_TREE)
4080 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4081 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4083 tree tparm = TREE_VALUE (parmtypes);
4084 tree targ = TREE_TYPE ((*args)[ix]);
4085 bool ptr = TYPE_PTR_P (tparm);
4086 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4087 if ((ptr || arr || !same_type_p (tparm, targ))
4088 && (!ptr || !arr
4089 || !same_type_p (TREE_TYPE (tparm),
4090 TREE_TYPE (targ))))
4091 found = false;
4093 if (found
4094 && ix == vec_safe_length (args)
4095 /* May be this should be sufficient_parms_p instead,
4096 depending on how exactly should user-defined literals
4097 work in presence of default arguments on the literal
4098 operator parameters. */
4099 && parmtypes == void_list_node)
4100 return decl;
4104 return error_mark_node;
4107 /* Parse a user-defined char constant. Returns a call to a user-defined
4108 literal operator taking the character as an argument. */
4110 static cp_expr
4111 cp_parser_userdef_char_literal (cp_parser *parser)
4113 cp_token *token = cp_lexer_consume_token (parser->lexer);
4114 tree literal = token->u.value;
4115 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4116 tree value = USERDEF_LITERAL_VALUE (literal);
4117 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4118 tree decl, result;
4120 /* Build up a call to the user-defined operator */
4121 /* Lookup the name we got back from the id-expression. */
4122 vec<tree, va_gc> *args = make_tree_vector ();
4123 vec_safe_push (args, value);
4124 decl = lookup_literal_operator (name, args);
4125 if (!decl || decl == error_mark_node)
4127 error ("unable to find character literal operator %qD with %qT argument",
4128 name, TREE_TYPE (value));
4129 release_tree_vector (args);
4130 return error_mark_node;
4132 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4133 release_tree_vector (args);
4134 return result;
4137 /* A subroutine of cp_parser_userdef_numeric_literal to
4138 create a char... template parameter pack from a string node. */
4140 static tree
4141 make_char_string_pack (tree value)
4143 tree charvec;
4144 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4145 const char *str = TREE_STRING_POINTER (value);
4146 int i, len = TREE_STRING_LENGTH (value) - 1;
4147 tree argvec = make_tree_vec (1);
4149 /* Fill in CHARVEC with all of the parameters. */
4150 charvec = make_tree_vec (len);
4151 for (i = 0; i < len; ++i)
4152 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4154 /* Build the argument packs. */
4155 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4156 TREE_TYPE (argpack) = char_type_node;
4158 TREE_VEC_ELT (argvec, 0) = argpack;
4160 return argvec;
4163 /* A subroutine of cp_parser_userdef_numeric_literal to
4164 create a char... template parameter pack from a string node. */
4166 static tree
4167 make_string_pack (tree value)
4169 tree charvec;
4170 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4171 const unsigned char *str
4172 = (const unsigned char *) TREE_STRING_POINTER (value);
4173 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4174 int len = TREE_STRING_LENGTH (value) / sz - 1;
4175 tree argvec = make_tree_vec (2);
4177 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4178 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4180 /* First template parm is character type. */
4181 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4183 /* Fill in CHARVEC with all of the parameters. */
4184 charvec = make_tree_vec (len);
4185 for (int i = 0; i < len; ++i)
4186 TREE_VEC_ELT (charvec, i)
4187 = double_int_to_tree (str_char_type_node,
4188 double_int::from_buffer (str + i * sz, sz));
4190 /* Build the argument packs. */
4191 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4192 TREE_TYPE (argpack) = str_char_type_node;
4194 TREE_VEC_ELT (argvec, 1) = argpack;
4196 return argvec;
4199 /* Parse a user-defined numeric constant. returns a call to a user-defined
4200 literal operator. */
4202 static cp_expr
4203 cp_parser_userdef_numeric_literal (cp_parser *parser)
4205 cp_token *token = cp_lexer_consume_token (parser->lexer);
4206 tree literal = token->u.value;
4207 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4208 tree value = USERDEF_LITERAL_VALUE (literal);
4209 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4210 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4211 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4212 tree decl, result;
4213 vec<tree, va_gc> *args;
4215 /* Look for a literal operator taking the exact type of numeric argument
4216 as the literal value. */
4217 args = make_tree_vector ();
4218 vec_safe_push (args, value);
4219 decl = lookup_literal_operator (name, args);
4220 if (decl && decl != error_mark_node)
4222 result = finish_call_expr (decl, &args, false, true,
4223 tf_warning_or_error);
4225 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4227 warning_at (token->location, OPT_Woverflow,
4228 "integer literal exceeds range of %qT type",
4229 long_long_unsigned_type_node);
4231 else
4233 if (overflow > 0)
4234 warning_at (token->location, OPT_Woverflow,
4235 "floating literal exceeds range of %qT type",
4236 long_double_type_node);
4237 else if (overflow < 0)
4238 warning_at (token->location, OPT_Woverflow,
4239 "floating literal truncated to zero");
4242 release_tree_vector (args);
4243 return result;
4245 release_tree_vector (args);
4247 /* If the numeric argument didn't work, look for a raw literal
4248 operator taking a const char* argument consisting of the number
4249 in string format. */
4250 args = make_tree_vector ();
4251 vec_safe_push (args, num_string);
4252 decl = lookup_literal_operator (name, args);
4253 if (decl && decl != error_mark_node)
4255 result = finish_call_expr (decl, &args, false, true,
4256 tf_warning_or_error);
4257 release_tree_vector (args);
4258 return result;
4260 release_tree_vector (args);
4262 /* If the raw literal didn't work, look for a non-type template
4263 function with parameter pack char.... Call the function with
4264 template parameter characters representing the number. */
4265 args = make_tree_vector ();
4266 decl = lookup_literal_operator (name, args);
4267 if (decl && decl != error_mark_node)
4269 tree tmpl_args = make_char_string_pack (num_string);
4270 decl = lookup_template_function (decl, tmpl_args);
4271 result = finish_call_expr (decl, &args, false, true,
4272 tf_warning_or_error);
4273 release_tree_vector (args);
4274 return result;
4277 release_tree_vector (args);
4279 error ("unable to find numeric literal operator %qD", name);
4280 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4281 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4282 "to enable more built-in suffixes");
4283 return error_mark_node;
4286 /* Parse a user-defined string constant. Returns a call to a user-defined
4287 literal operator taking a character pointer and the length of the string
4288 as arguments. */
4290 static tree
4291 cp_parser_userdef_string_literal (tree literal)
4293 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4294 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4295 tree value = USERDEF_LITERAL_VALUE (literal);
4296 int len = TREE_STRING_LENGTH (value)
4297 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4298 tree decl, result;
4299 vec<tree, va_gc> *args;
4301 /* Build up a call to the user-defined operator. */
4302 /* Lookup the name we got back from the id-expression. */
4303 args = make_tree_vector ();
4304 vec_safe_push (args, value);
4305 vec_safe_push (args, build_int_cst (size_type_node, len));
4306 decl = lookup_literal_operator (name, args);
4308 if (decl && decl != error_mark_node)
4310 result = finish_call_expr (decl, &args, false, true,
4311 tf_warning_or_error);
4312 release_tree_vector (args);
4313 return result;
4315 release_tree_vector (args);
4317 /* Look for a template function with typename parameter CharT
4318 and parameter pack CharT... Call the function with
4319 template parameter characters representing the string. */
4320 args = make_tree_vector ();
4321 decl = lookup_literal_operator (name, args);
4322 if (decl && decl != error_mark_node)
4324 tree tmpl_args = make_string_pack (value);
4325 decl = lookup_template_function (decl, tmpl_args);
4326 result = finish_call_expr (decl, &args, false, true,
4327 tf_warning_or_error);
4328 release_tree_vector (args);
4329 return result;
4331 release_tree_vector (args);
4333 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4334 name, TREE_TYPE (value), size_type_node);
4335 return error_mark_node;
4339 /* Basic concepts [gram.basic] */
4341 /* Parse a translation-unit.
4343 translation-unit:
4344 declaration-seq [opt]
4346 Returns TRUE if all went well. */
4348 static bool
4349 cp_parser_translation_unit (cp_parser* parser)
4351 /* The address of the first non-permanent object on the declarator
4352 obstack. */
4353 static void *declarator_obstack_base;
4355 bool success;
4357 /* Create the declarator obstack, if necessary. */
4358 if (!cp_error_declarator)
4360 gcc_obstack_init (&declarator_obstack);
4361 /* Create the error declarator. */
4362 cp_error_declarator = make_declarator (cdk_error);
4363 /* Create the empty parameter list. */
4364 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4365 /* Remember where the base of the declarator obstack lies. */
4366 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4369 cp_parser_declaration_seq_opt (parser);
4371 /* If there are no tokens left then all went well. */
4372 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4374 /* Get rid of the token array; we don't need it any more. */
4375 cp_lexer_destroy (parser->lexer);
4376 parser->lexer = NULL;
4378 /* This file might have been a context that's implicitly extern
4379 "C". If so, pop the lang context. (Only relevant for PCH.) */
4380 if (parser->implicit_extern_c)
4382 pop_lang_context ();
4383 parser->implicit_extern_c = false;
4386 /* Finish up. */
4387 finish_translation_unit ();
4389 success = true;
4391 else
4393 cp_parser_error (parser, "expected declaration");
4394 success = false;
4397 /* Make sure the declarator obstack was fully cleaned up. */
4398 gcc_assert (obstack_next_free (&declarator_obstack)
4399 == declarator_obstack_base);
4401 /* All went well. */
4402 return success;
4405 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4406 decltype context. */
4408 static inline tsubst_flags_t
4409 complain_flags (bool decltype_p)
4411 tsubst_flags_t complain = tf_warning_or_error;
4412 if (decltype_p)
4413 complain |= tf_decltype;
4414 return complain;
4417 /* We're about to parse a collection of statements. If we're currently
4418 parsing tentatively, set up a firewall so that any nested
4419 cp_parser_commit_to_tentative_parse won't affect the current context. */
4421 static cp_token_position
4422 cp_parser_start_tentative_firewall (cp_parser *parser)
4424 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4425 return 0;
4427 cp_parser_parse_tentatively (parser);
4428 cp_parser_commit_to_topmost_tentative_parse (parser);
4429 return cp_lexer_token_position (parser->lexer, false);
4432 /* We've finished parsing the collection of statements. Wrap up the
4433 firewall and replace the relevant tokens with the parsed form. */
4435 static void
4436 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4437 tree expr)
4439 if (!start)
4440 return;
4442 /* Finish the firewall level. */
4443 cp_parser_parse_definitely (parser);
4444 /* And remember the result of the parse for when we try again. */
4445 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4446 token->type = CPP_PREPARSED_EXPR;
4447 token->u.value = expr;
4448 token->keyword = RID_MAX;
4449 cp_lexer_purge_tokens_after (parser->lexer, start);
4452 /* Like the above functions, but let the user modify the tokens. Used by
4453 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4454 later parses, so it makes sense to localize the effects of
4455 cp_parser_commit_to_tentative_parse. */
4457 struct tentative_firewall
4459 cp_parser *parser;
4460 bool set;
4462 tentative_firewall (cp_parser *p): parser(p)
4464 /* If we're currently parsing tentatively, start a committed level as a
4465 firewall and then an inner tentative parse. */
4466 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4468 cp_parser_parse_tentatively (parser);
4469 cp_parser_commit_to_topmost_tentative_parse (parser);
4470 cp_parser_parse_tentatively (parser);
4474 ~tentative_firewall()
4476 if (set)
4478 /* Finish the inner tentative parse and the firewall, propagating any
4479 uncommitted error state to the outer tentative parse. */
4480 bool err = cp_parser_error_occurred (parser);
4481 cp_parser_parse_definitely (parser);
4482 cp_parser_parse_definitely (parser);
4483 if (err)
4484 cp_parser_simulate_error (parser);
4489 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4490 enclosing parentheses. */
4492 static cp_expr
4493 cp_parser_statement_expr (cp_parser *parser)
4495 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4497 /* Consume the '('. */
4498 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4499 cp_lexer_consume_token (parser->lexer);
4500 /* Start the statement-expression. */
4501 tree expr = begin_stmt_expr ();
4502 /* Parse the compound-statement. */
4503 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4504 /* Finish up. */
4505 expr = finish_stmt_expr (expr, false);
4506 /* Consume the ')'. */
4507 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4508 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4509 cp_parser_skip_to_end_of_statement (parser);
4511 cp_parser_end_tentative_firewall (parser, start, expr);
4512 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4513 return cp_expr (expr, combined_loc);
4516 /* Expressions [gram.expr] */
4518 /* Parse a fold-operator.
4520 fold-operator:
4521 - * / % ^ & | = < > << >>
4522 = -= *= /= %= ^= &= |= <<= >>=
4523 == != <= >= && || , .* ->*
4525 This returns the tree code corresponding to the matched operator
4526 as an int. When the current token matches a compound assignment
4527 opertor, the resulting tree code is the negative value of the
4528 non-assignment operator. */
4530 static int
4531 cp_parser_fold_operator (cp_token *token)
4533 switch (token->type)
4535 case CPP_PLUS: return PLUS_EXPR;
4536 case CPP_MINUS: return MINUS_EXPR;
4537 case CPP_MULT: return MULT_EXPR;
4538 case CPP_DIV: return TRUNC_DIV_EXPR;
4539 case CPP_MOD: return TRUNC_MOD_EXPR;
4540 case CPP_XOR: return BIT_XOR_EXPR;
4541 case CPP_AND: return BIT_AND_EXPR;
4542 case CPP_OR: return BIT_IOR_EXPR;
4543 case CPP_LSHIFT: return LSHIFT_EXPR;
4544 case CPP_RSHIFT: return RSHIFT_EXPR;
4546 case CPP_EQ: return -NOP_EXPR;
4547 case CPP_PLUS_EQ: return -PLUS_EXPR;
4548 case CPP_MINUS_EQ: return -MINUS_EXPR;
4549 case CPP_MULT_EQ: return -MULT_EXPR;
4550 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4551 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4552 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4553 case CPP_AND_EQ: return -BIT_AND_EXPR;
4554 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4555 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4556 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4558 case CPP_EQ_EQ: return EQ_EXPR;
4559 case CPP_NOT_EQ: return NE_EXPR;
4560 case CPP_LESS: return LT_EXPR;
4561 case CPP_GREATER: return GT_EXPR;
4562 case CPP_LESS_EQ: return LE_EXPR;
4563 case CPP_GREATER_EQ: return GE_EXPR;
4565 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4566 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4568 case CPP_COMMA: return COMPOUND_EXPR;
4570 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4571 case CPP_DEREF_STAR: return MEMBER_REF;
4573 default: return ERROR_MARK;
4577 /* Returns true if CODE indicates a binary expression, which is not allowed in
4578 the LHS of a fold-expression. More codes will need to be added to use this
4579 function in other contexts. */
4581 static bool
4582 is_binary_op (tree_code code)
4584 switch (code)
4586 case PLUS_EXPR:
4587 case POINTER_PLUS_EXPR:
4588 case MINUS_EXPR:
4589 case MULT_EXPR:
4590 case TRUNC_DIV_EXPR:
4591 case TRUNC_MOD_EXPR:
4592 case BIT_XOR_EXPR:
4593 case BIT_AND_EXPR:
4594 case BIT_IOR_EXPR:
4595 case LSHIFT_EXPR:
4596 case RSHIFT_EXPR:
4598 case MODOP_EXPR:
4600 case EQ_EXPR:
4601 case NE_EXPR:
4602 case LE_EXPR:
4603 case GE_EXPR:
4604 case LT_EXPR:
4605 case GT_EXPR:
4607 case TRUTH_ANDIF_EXPR:
4608 case TRUTH_ORIF_EXPR:
4610 case COMPOUND_EXPR:
4612 case DOTSTAR_EXPR:
4613 case MEMBER_REF:
4614 return true;
4616 default:
4617 return false;
4621 /* If the next token is a suitable fold operator, consume it and return as
4622 the function above. */
4624 static int
4625 cp_parser_fold_operator (cp_parser *parser)
4627 cp_token* token = cp_lexer_peek_token (parser->lexer);
4628 int code = cp_parser_fold_operator (token);
4629 if (code != ERROR_MARK)
4630 cp_lexer_consume_token (parser->lexer);
4631 return code;
4634 /* Parse a fold-expression.
4636 fold-expression:
4637 ( ... folding-operator cast-expression)
4638 ( cast-expression folding-operator ... )
4639 ( cast-expression folding operator ... folding-operator cast-expression)
4641 Note that the '(' and ')' are matched in primary expression. */
4643 static cp_expr
4644 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4646 cp_id_kind pidk;
4648 // Left fold.
4649 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4651 cp_lexer_consume_token (parser->lexer);
4652 int op = cp_parser_fold_operator (parser);
4653 if (op == ERROR_MARK)
4655 cp_parser_error (parser, "expected binary operator");
4656 return error_mark_node;
4659 tree expr = cp_parser_cast_expression (parser, false, false,
4660 false, &pidk);
4661 if (expr == error_mark_node)
4662 return error_mark_node;
4663 return finish_left_unary_fold_expr (expr, op);
4666 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4667 int op = cp_parser_fold_operator (parser);
4668 if (op == ERROR_MARK)
4670 cp_parser_error (parser, "expected binary operator");
4671 return error_mark_node;
4674 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4676 cp_parser_error (parser, "expected ...");
4677 return error_mark_node;
4679 cp_lexer_consume_token (parser->lexer);
4681 /* The operands of a fold-expression are cast-expressions, so binary or
4682 conditional expressions are not allowed. We check this here to avoid
4683 tentative parsing. */
4684 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4685 /* OK, the expression was parenthesized. */;
4686 else if (is_binary_op (TREE_CODE (expr1)))
4687 error_at (location_of (expr1),
4688 "binary expression in operand of fold-expression");
4689 else if (TREE_CODE (expr1) == COND_EXPR)
4690 error_at (location_of (expr1),
4691 "conditional expression in operand of fold-expression");
4693 // Right fold.
4694 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4695 return finish_right_unary_fold_expr (expr1, op);
4697 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4699 cp_parser_error (parser, "mismatched operator in fold-expression");
4700 return error_mark_node;
4702 cp_lexer_consume_token (parser->lexer);
4704 // Binary left or right fold.
4705 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4706 if (expr2 == error_mark_node)
4707 return error_mark_node;
4708 return finish_binary_fold_expr (expr1, expr2, op);
4711 /* Parse a primary-expression.
4713 primary-expression:
4714 literal
4715 this
4716 ( expression )
4717 id-expression
4718 lambda-expression (C++11)
4720 GNU Extensions:
4722 primary-expression:
4723 ( compound-statement )
4724 __builtin_va_arg ( assignment-expression , type-id )
4725 __builtin_offsetof ( type-id , offsetof-expression )
4727 C++ Extensions:
4728 __has_nothrow_assign ( type-id )
4729 __has_nothrow_constructor ( type-id )
4730 __has_nothrow_copy ( type-id )
4731 __has_trivial_assign ( type-id )
4732 __has_trivial_constructor ( type-id )
4733 __has_trivial_copy ( type-id )
4734 __has_trivial_destructor ( type-id )
4735 __has_virtual_destructor ( type-id )
4736 __is_abstract ( type-id )
4737 __is_base_of ( type-id , type-id )
4738 __is_class ( type-id )
4739 __is_empty ( type-id )
4740 __is_enum ( type-id )
4741 __is_final ( type-id )
4742 __is_literal_type ( type-id )
4743 __is_pod ( type-id )
4744 __is_polymorphic ( type-id )
4745 __is_std_layout ( type-id )
4746 __is_trivial ( type-id )
4747 __is_union ( type-id )
4749 Objective-C++ Extension:
4751 primary-expression:
4752 objc-expression
4754 literal:
4755 __null
4757 ADDRESS_P is true iff this expression was immediately preceded by
4758 "&" and therefore might denote a pointer-to-member. CAST_P is true
4759 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4760 true iff this expression is a template argument.
4762 Returns a representation of the expression. Upon return, *IDK
4763 indicates what kind of id-expression (if any) was present. */
4765 static cp_expr
4766 cp_parser_primary_expression (cp_parser *parser,
4767 bool address_p,
4768 bool cast_p,
4769 bool template_arg_p,
4770 bool decltype_p,
4771 cp_id_kind *idk)
4773 cp_token *token = NULL;
4775 /* Assume the primary expression is not an id-expression. */
4776 *idk = CP_ID_KIND_NONE;
4778 /* Peek at the next token. */
4779 token = cp_lexer_peek_token (parser->lexer);
4780 switch ((int) token->type)
4782 /* literal:
4783 integer-literal
4784 character-literal
4785 floating-literal
4786 string-literal
4787 boolean-literal
4788 pointer-literal
4789 user-defined-literal */
4790 case CPP_CHAR:
4791 case CPP_CHAR16:
4792 case CPP_CHAR32:
4793 case CPP_WCHAR:
4794 case CPP_UTF8CHAR:
4795 case CPP_NUMBER:
4796 case CPP_PREPARSED_EXPR:
4797 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4798 return cp_parser_userdef_numeric_literal (parser);
4799 token = cp_lexer_consume_token (parser->lexer);
4800 if (TREE_CODE (token->u.value) == FIXED_CST)
4802 error_at (token->location,
4803 "fixed-point types not supported in C++");
4804 return error_mark_node;
4806 /* Floating-point literals are only allowed in an integral
4807 constant expression if they are cast to an integral or
4808 enumeration type. */
4809 if (TREE_CODE (token->u.value) == REAL_CST
4810 && parser->integral_constant_expression_p
4811 && pedantic)
4813 /* CAST_P will be set even in invalid code like "int(2.7 +
4814 ...)". Therefore, we have to check that the next token
4815 is sure to end the cast. */
4816 if (cast_p)
4818 cp_token *next_token;
4820 next_token = cp_lexer_peek_token (parser->lexer);
4821 if (/* The comma at the end of an
4822 enumerator-definition. */
4823 next_token->type != CPP_COMMA
4824 /* The curly brace at the end of an enum-specifier. */
4825 && next_token->type != CPP_CLOSE_BRACE
4826 /* The end of a statement. */
4827 && next_token->type != CPP_SEMICOLON
4828 /* The end of the cast-expression. */
4829 && next_token->type != CPP_CLOSE_PAREN
4830 /* The end of an array bound. */
4831 && next_token->type != CPP_CLOSE_SQUARE
4832 /* The closing ">" in a template-argument-list. */
4833 && (next_token->type != CPP_GREATER
4834 || parser->greater_than_is_operator_p)
4835 /* C++0x only: A ">>" treated like two ">" tokens,
4836 in a template-argument-list. */
4837 && (next_token->type != CPP_RSHIFT
4838 || (cxx_dialect == cxx98)
4839 || parser->greater_than_is_operator_p))
4840 cast_p = false;
4843 /* If we are within a cast, then the constraint that the
4844 cast is to an integral or enumeration type will be
4845 checked at that point. If we are not within a cast, then
4846 this code is invalid. */
4847 if (!cast_p)
4848 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4850 return cp_expr (token->u.value, token->location);
4852 case CPP_CHAR_USERDEF:
4853 case CPP_CHAR16_USERDEF:
4854 case CPP_CHAR32_USERDEF:
4855 case CPP_WCHAR_USERDEF:
4856 case CPP_UTF8CHAR_USERDEF:
4857 return cp_parser_userdef_char_literal (parser);
4859 case CPP_STRING:
4860 case CPP_STRING16:
4861 case CPP_STRING32:
4862 case CPP_WSTRING:
4863 case CPP_UTF8STRING:
4864 case CPP_STRING_USERDEF:
4865 case CPP_STRING16_USERDEF:
4866 case CPP_STRING32_USERDEF:
4867 case CPP_WSTRING_USERDEF:
4868 case CPP_UTF8STRING_USERDEF:
4869 /* ??? Should wide strings be allowed when parser->translate_strings_p
4870 is false (i.e. in attributes)? If not, we can kill the third
4871 argument to cp_parser_string_literal. */
4872 return cp_parser_string_literal (parser,
4873 parser->translate_strings_p,
4874 true);
4876 case CPP_OPEN_PAREN:
4877 /* If we see `( { ' then we are looking at the beginning of
4878 a GNU statement-expression. */
4879 if (cp_parser_allow_gnu_extensions_p (parser)
4880 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4882 /* Statement-expressions are not allowed by the standard. */
4883 pedwarn (token->location, OPT_Wpedantic,
4884 "ISO C++ forbids braced-groups within expressions");
4886 /* And they're not allowed outside of a function-body; you
4887 cannot, for example, write:
4889 int i = ({ int j = 3; j + 1; });
4891 at class or namespace scope. */
4892 if (!parser->in_function_body
4893 || parser->in_template_argument_list_p)
4895 error_at (token->location,
4896 "statement-expressions are not allowed outside "
4897 "functions nor in template-argument lists");
4898 cp_parser_skip_to_end_of_block_or_statement (parser);
4899 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4900 cp_lexer_consume_token (parser->lexer);
4901 return error_mark_node;
4903 else
4904 return cp_parser_statement_expr (parser);
4906 /* Otherwise it's a normal parenthesized expression. */
4908 cp_expr expr;
4909 bool saved_greater_than_is_operator_p;
4911 location_t open_paren_loc = token->location;
4913 /* Consume the `('. */
4914 cp_lexer_consume_token (parser->lexer);
4915 /* Within a parenthesized expression, a `>' token is always
4916 the greater-than operator. */
4917 saved_greater_than_is_operator_p
4918 = parser->greater_than_is_operator_p;
4919 parser->greater_than_is_operator_p = true;
4921 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4922 /* Left fold expression. */
4923 expr = NULL_TREE;
4924 else
4925 /* Parse the parenthesized expression. */
4926 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4928 token = cp_lexer_peek_token (parser->lexer);
4929 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
4931 expr = cp_parser_fold_expression (parser, expr);
4932 if (expr != error_mark_node
4933 && cxx_dialect < cxx1z
4934 && !in_system_header_at (input_location))
4935 pedwarn (input_location, 0, "fold-expressions only available "
4936 "with -std=c++1z or -std=gnu++1z");
4938 else
4939 /* Let the front end know that this expression was
4940 enclosed in parentheses. This matters in case, for
4941 example, the expression is of the form `A::B', since
4942 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4943 not. */
4944 expr = finish_parenthesized_expr (expr);
4946 /* DR 705: Wrapping an unqualified name in parentheses
4947 suppresses arg-dependent lookup. We want to pass back
4948 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4949 (c++/37862), but none of the others. */
4950 if (*idk != CP_ID_KIND_QUALIFIED)
4951 *idk = CP_ID_KIND_NONE;
4953 /* The `>' token might be the end of a template-id or
4954 template-parameter-list now. */
4955 parser->greater_than_is_operator_p
4956 = saved_greater_than_is_operator_p;
4958 /* Consume the `)'. */
4959 token = cp_lexer_peek_token (parser->lexer);
4960 location_t close_paren_loc = token->location;
4961 expr.set_range (open_paren_loc, close_paren_loc);
4962 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)
4963 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4964 cp_parser_skip_to_end_of_statement (parser);
4966 return expr;
4969 case CPP_OPEN_SQUARE:
4971 if (c_dialect_objc ())
4973 /* We might have an Objective-C++ message. */
4974 cp_parser_parse_tentatively (parser);
4975 tree msg = cp_parser_objc_message_expression (parser);
4976 /* If that works out, we're done ... */
4977 if (cp_parser_parse_definitely (parser))
4978 return msg;
4979 /* ... else, fall though to see if it's a lambda. */
4981 cp_expr lam = cp_parser_lambda_expression (parser);
4982 /* Don't warn about a failed tentative parse. */
4983 if (cp_parser_error_occurred (parser))
4984 return error_mark_node;
4985 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4986 return lam;
4989 case CPP_OBJC_STRING:
4990 if (c_dialect_objc ())
4991 /* We have an Objective-C++ string literal. */
4992 return cp_parser_objc_expression (parser);
4993 cp_parser_error (parser, "expected primary-expression");
4994 return error_mark_node;
4996 case CPP_KEYWORD:
4997 switch (token->keyword)
4999 /* These two are the boolean literals. */
5000 case RID_TRUE:
5001 cp_lexer_consume_token (parser->lexer);
5002 return cp_expr (boolean_true_node, token->location);
5003 case RID_FALSE:
5004 cp_lexer_consume_token (parser->lexer);
5005 return cp_expr (boolean_false_node, token->location);
5007 /* The `__null' literal. */
5008 case RID_NULL:
5009 cp_lexer_consume_token (parser->lexer);
5010 return cp_expr (null_node, token->location);
5012 /* The `nullptr' literal. */
5013 case RID_NULLPTR:
5014 cp_lexer_consume_token (parser->lexer);
5015 return cp_expr (nullptr_node, token->location);
5017 /* Recognize the `this' keyword. */
5018 case RID_THIS:
5019 cp_lexer_consume_token (parser->lexer);
5020 if (parser->local_variables_forbidden_p)
5022 error_at (token->location,
5023 "%<this%> may not be used in this context");
5024 return error_mark_node;
5026 /* Pointers cannot appear in constant-expressions. */
5027 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5028 return error_mark_node;
5029 return cp_expr (finish_this_expr (), token->location);
5031 /* The `operator' keyword can be the beginning of an
5032 id-expression. */
5033 case RID_OPERATOR:
5034 goto id_expression;
5036 case RID_FUNCTION_NAME:
5037 case RID_PRETTY_FUNCTION_NAME:
5038 case RID_C99_FUNCTION_NAME:
5040 non_integral_constant name;
5042 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5043 __func__ are the names of variables -- but they are
5044 treated specially. Therefore, they are handled here,
5045 rather than relying on the generic id-expression logic
5046 below. Grammatically, these names are id-expressions.
5048 Consume the token. */
5049 token = cp_lexer_consume_token (parser->lexer);
5051 switch (token->keyword)
5053 case RID_FUNCTION_NAME:
5054 name = NIC_FUNC_NAME;
5055 break;
5056 case RID_PRETTY_FUNCTION_NAME:
5057 name = NIC_PRETTY_FUNC;
5058 break;
5059 case RID_C99_FUNCTION_NAME:
5060 name = NIC_C99_FUNC;
5061 break;
5062 default:
5063 gcc_unreachable ();
5066 if (cp_parser_non_integral_constant_expression (parser, name))
5067 return error_mark_node;
5069 /* Look up the name. */
5070 return finish_fname (token->u.value);
5073 case RID_VA_ARG:
5075 tree expression;
5076 tree type;
5077 source_location type_location;
5078 location_t start_loc
5079 = cp_lexer_peek_token (parser->lexer)->location;
5080 /* The `__builtin_va_arg' construct is used to handle
5081 `va_arg'. Consume the `__builtin_va_arg' token. */
5082 cp_lexer_consume_token (parser->lexer);
5083 /* Look for the opening `('. */
5084 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5085 /* Now, parse the assignment-expression. */
5086 expression = cp_parser_assignment_expression (parser);
5087 /* Look for the `,'. */
5088 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5089 type_location = cp_lexer_peek_token (parser->lexer)->location;
5090 /* Parse the type-id. */
5092 type_id_in_expr_sentinel s (parser);
5093 type = cp_parser_type_id (parser);
5095 /* Look for the closing `)'. */
5096 location_t finish_loc
5097 = cp_lexer_peek_token (parser->lexer)->location;
5098 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5099 /* Using `va_arg' in a constant-expression is not
5100 allowed. */
5101 if (cp_parser_non_integral_constant_expression (parser,
5102 NIC_VA_ARG))
5103 return error_mark_node;
5104 /* Construct a location of the form:
5105 __builtin_va_arg (v, int)
5106 ~~~~~~~~~~~~~~~~~~~~~^~~~
5107 with the caret at the type, ranging from the start of the
5108 "__builtin_va_arg" token to the close paren. */
5109 location_t combined_loc
5110 = make_location (type_location, start_loc, finish_loc);
5111 return build_x_va_arg (combined_loc, expression, type);
5114 case RID_OFFSETOF:
5115 return cp_parser_builtin_offsetof (parser);
5117 case RID_HAS_NOTHROW_ASSIGN:
5118 case RID_HAS_NOTHROW_CONSTRUCTOR:
5119 case RID_HAS_NOTHROW_COPY:
5120 case RID_HAS_TRIVIAL_ASSIGN:
5121 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5122 case RID_HAS_TRIVIAL_COPY:
5123 case RID_HAS_TRIVIAL_DESTRUCTOR:
5124 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5125 case RID_HAS_VIRTUAL_DESTRUCTOR:
5126 case RID_IS_ABSTRACT:
5127 case RID_IS_BASE_OF:
5128 case RID_IS_CLASS:
5129 case RID_IS_EMPTY:
5130 case RID_IS_ENUM:
5131 case RID_IS_FINAL:
5132 case RID_IS_LITERAL_TYPE:
5133 case RID_IS_POD:
5134 case RID_IS_POLYMORPHIC:
5135 case RID_IS_SAME_AS:
5136 case RID_IS_STD_LAYOUT:
5137 case RID_IS_TRIVIAL:
5138 case RID_IS_TRIVIALLY_ASSIGNABLE:
5139 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5140 case RID_IS_TRIVIALLY_COPYABLE:
5141 case RID_IS_UNION:
5142 return cp_parser_trait_expr (parser, token->keyword);
5144 // C++ concepts
5145 case RID_REQUIRES:
5146 return cp_parser_requires_expression (parser);
5148 /* Objective-C++ expressions. */
5149 case RID_AT_ENCODE:
5150 case RID_AT_PROTOCOL:
5151 case RID_AT_SELECTOR:
5152 return cp_parser_objc_expression (parser);
5154 case RID_TEMPLATE:
5155 if (parser->in_function_body
5156 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5157 == CPP_LESS))
5159 error_at (token->location,
5160 "a template declaration cannot appear at block scope");
5161 cp_parser_skip_to_end_of_block_or_statement (parser);
5162 return error_mark_node;
5164 /* FALLTHRU */
5165 default:
5166 cp_parser_error (parser, "expected primary-expression");
5167 return error_mark_node;
5170 /* An id-expression can start with either an identifier, a
5171 `::' as the beginning of a qualified-id, or the "operator"
5172 keyword. */
5173 case CPP_NAME:
5174 case CPP_SCOPE:
5175 case CPP_TEMPLATE_ID:
5176 case CPP_NESTED_NAME_SPECIFIER:
5178 id_expression:
5179 cp_expr id_expression;
5180 cp_expr decl;
5181 const char *error_msg;
5182 bool template_p;
5183 bool done;
5184 cp_token *id_expr_token;
5186 /* Parse the id-expression. */
5187 id_expression
5188 = cp_parser_id_expression (parser,
5189 /*template_keyword_p=*/false,
5190 /*check_dependency_p=*/true,
5191 &template_p,
5192 /*declarator_p=*/false,
5193 /*optional_p=*/false);
5194 if (id_expression == error_mark_node)
5195 return error_mark_node;
5196 id_expr_token = token;
5197 token = cp_lexer_peek_token (parser->lexer);
5198 done = (token->type != CPP_OPEN_SQUARE
5199 && token->type != CPP_OPEN_PAREN
5200 && token->type != CPP_DOT
5201 && token->type != CPP_DEREF
5202 && token->type != CPP_PLUS_PLUS
5203 && token->type != CPP_MINUS_MINUS);
5204 /* If we have a template-id, then no further lookup is
5205 required. If the template-id was for a template-class, we
5206 will sometimes have a TYPE_DECL at this point. */
5207 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5208 || TREE_CODE (id_expression) == TYPE_DECL)
5209 decl = id_expression;
5210 /* Look up the name. */
5211 else
5213 tree ambiguous_decls;
5215 /* If we already know that this lookup is ambiguous, then
5216 we've already issued an error message; there's no reason
5217 to check again. */
5218 if (id_expr_token->type == CPP_NAME
5219 && id_expr_token->error_reported)
5221 cp_parser_simulate_error (parser);
5222 return error_mark_node;
5225 decl = cp_parser_lookup_name (parser, id_expression,
5226 none_type,
5227 template_p,
5228 /*is_namespace=*/false,
5229 /*check_dependency=*/true,
5230 &ambiguous_decls,
5231 id_expr_token->location);
5232 /* If the lookup was ambiguous, an error will already have
5233 been issued. */
5234 if (ambiguous_decls)
5235 return error_mark_node;
5237 /* In Objective-C++, we may have an Objective-C 2.0
5238 dot-syntax for classes here. */
5239 if (c_dialect_objc ()
5240 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5241 && TREE_CODE (decl) == TYPE_DECL
5242 && objc_is_class_name (decl))
5244 tree component;
5245 cp_lexer_consume_token (parser->lexer);
5246 component = cp_parser_identifier (parser);
5247 if (component == error_mark_node)
5248 return error_mark_node;
5250 tree result = objc_build_class_component_ref (id_expression,
5251 component);
5252 /* Build a location of the form:
5253 expr.component
5254 ~~~~~^~~~~~~~~
5255 with caret at the start of the component name (at
5256 input_location), ranging from the start of the id_expression
5257 to the end of the component name. */
5258 location_t combined_loc
5259 = make_location (input_location, id_expression.get_start (),
5260 get_finish (input_location));
5261 protected_set_expr_location (result, combined_loc);
5262 return result;
5265 /* In Objective-C++, an instance variable (ivar) may be preferred
5266 to whatever cp_parser_lookup_name() found.
5267 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5268 rest of c-family, we have to do a little extra work to preserve
5269 any location information in cp_expr "decl". Given that
5270 objc_lookup_ivar is implemented in "c-family" and "objc", we
5271 have a trip through the pure "tree" type, rather than cp_expr.
5272 Naively copying it back to "decl" would implicitly give the
5273 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5274 store an EXPR_LOCATION. Hence we only update "decl" (and
5275 hence its location_t) if we get back a different tree node. */
5276 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5277 id_expression);
5278 if (decl_tree != decl.get_value ())
5279 decl = cp_expr (decl_tree);
5281 /* If name lookup gives us a SCOPE_REF, then the
5282 qualifying scope was dependent. */
5283 if (TREE_CODE (decl) == SCOPE_REF)
5285 /* At this point, we do not know if DECL is a valid
5286 integral constant expression. We assume that it is
5287 in fact such an expression, so that code like:
5289 template <int N> struct A {
5290 int a[B<N>::i];
5293 is accepted. At template-instantiation time, we
5294 will check that B<N>::i is actually a constant. */
5295 return decl;
5297 /* Check to see if DECL is a local variable in a context
5298 where that is forbidden. */
5299 if (parser->local_variables_forbidden_p
5300 && local_variable_p (decl))
5302 /* It might be that we only found DECL because we are
5303 trying to be generous with pre-ISO scoping rules.
5304 For example, consider:
5306 int i;
5307 void g() {
5308 for (int i = 0; i < 10; ++i) {}
5309 extern void f(int j = i);
5312 Here, name look up will originally find the out
5313 of scope `i'. We need to issue a warning message,
5314 but then use the global `i'. */
5315 decl = check_for_out_of_scope_variable (decl);
5316 if (local_variable_p (decl))
5318 error_at (id_expr_token->location,
5319 "local variable %qD may not appear in this context",
5320 decl.get_value ());
5321 return error_mark_node;
5326 decl = (finish_id_expression
5327 (id_expression, decl, parser->scope,
5328 idk,
5329 parser->integral_constant_expression_p,
5330 parser->allow_non_integral_constant_expression_p,
5331 &parser->non_integral_constant_expression_p,
5332 template_p, done, address_p,
5333 template_arg_p,
5334 &error_msg,
5335 id_expression.get_location ()));
5336 if (error_msg)
5337 cp_parser_error (parser, error_msg);
5338 decl.set_location (id_expr_token->location);
5339 return decl;
5342 /* Anything else is an error. */
5343 default:
5344 cp_parser_error (parser, "expected primary-expression");
5345 return error_mark_node;
5349 static inline cp_expr
5350 cp_parser_primary_expression (cp_parser *parser,
5351 bool address_p,
5352 bool cast_p,
5353 bool template_arg_p,
5354 cp_id_kind *idk)
5356 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5357 /*decltype*/false, idk);
5360 /* Parse an id-expression.
5362 id-expression:
5363 unqualified-id
5364 qualified-id
5366 qualified-id:
5367 :: [opt] nested-name-specifier template [opt] unqualified-id
5368 :: identifier
5369 :: operator-function-id
5370 :: template-id
5372 Return a representation of the unqualified portion of the
5373 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5374 a `::' or nested-name-specifier.
5376 Often, if the id-expression was a qualified-id, the caller will
5377 want to make a SCOPE_REF to represent the qualified-id. This
5378 function does not do this in order to avoid wastefully creating
5379 SCOPE_REFs when they are not required.
5381 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5382 `template' keyword.
5384 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5385 uninstantiated templates.
5387 If *TEMPLATE_P is non-NULL, it is set to true iff the
5388 `template' keyword is used to explicitly indicate that the entity
5389 named is a template.
5391 If DECLARATOR_P is true, the id-expression is appearing as part of
5392 a declarator, rather than as part of an expression. */
5394 static cp_expr
5395 cp_parser_id_expression (cp_parser *parser,
5396 bool template_keyword_p,
5397 bool check_dependency_p,
5398 bool *template_p,
5399 bool declarator_p,
5400 bool optional_p)
5402 bool global_scope_p;
5403 bool nested_name_specifier_p;
5405 /* Assume the `template' keyword was not used. */
5406 if (template_p)
5407 *template_p = template_keyword_p;
5409 /* Look for the optional `::' operator. */
5410 global_scope_p
5411 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
5412 != NULL_TREE);
5413 /* Look for the optional nested-name-specifier. */
5414 nested_name_specifier_p
5415 = (cp_parser_nested_name_specifier_opt (parser,
5416 /*typename_keyword_p=*/false,
5417 check_dependency_p,
5418 /*type_p=*/false,
5419 declarator_p)
5420 != NULL_TREE);
5421 /* If there is a nested-name-specifier, then we are looking at
5422 the first qualified-id production. */
5423 if (nested_name_specifier_p)
5425 tree saved_scope;
5426 tree saved_object_scope;
5427 tree saved_qualifying_scope;
5428 cp_expr unqualified_id;
5429 bool is_template;
5431 /* See if the next token is the `template' keyword. */
5432 if (!template_p)
5433 template_p = &is_template;
5434 *template_p = cp_parser_optional_template_keyword (parser);
5435 /* Name lookup we do during the processing of the
5436 unqualified-id might obliterate SCOPE. */
5437 saved_scope = parser->scope;
5438 saved_object_scope = parser->object_scope;
5439 saved_qualifying_scope = parser->qualifying_scope;
5440 /* Process the final unqualified-id. */
5441 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5442 check_dependency_p,
5443 declarator_p,
5444 /*optional_p=*/false);
5445 /* Restore the SAVED_SCOPE for our caller. */
5446 parser->scope = saved_scope;
5447 parser->object_scope = saved_object_scope;
5448 parser->qualifying_scope = saved_qualifying_scope;
5450 return unqualified_id;
5452 /* Otherwise, if we are in global scope, then we are looking at one
5453 of the other qualified-id productions. */
5454 else if (global_scope_p)
5456 cp_token *token;
5457 tree id;
5459 /* Peek at the next token. */
5460 token = cp_lexer_peek_token (parser->lexer);
5462 /* If it's an identifier, and the next token is not a "<", then
5463 we can avoid the template-id case. This is an optimization
5464 for this common case. */
5465 if (token->type == CPP_NAME
5466 && !cp_parser_nth_token_starts_template_argument_list_p
5467 (parser, 2))
5468 return cp_parser_identifier (parser);
5470 cp_parser_parse_tentatively (parser);
5471 /* Try a template-id. */
5472 id = cp_parser_template_id (parser,
5473 /*template_keyword_p=*/false,
5474 /*check_dependency_p=*/true,
5475 none_type,
5476 declarator_p);
5477 /* If that worked, we're done. */
5478 if (cp_parser_parse_definitely (parser))
5479 return id;
5481 /* Peek at the next token. (Changes in the token buffer may
5482 have invalidated the pointer obtained above.) */
5483 token = cp_lexer_peek_token (parser->lexer);
5485 switch (token->type)
5487 case CPP_NAME:
5488 return cp_parser_identifier (parser);
5490 case CPP_KEYWORD:
5491 if (token->keyword == RID_OPERATOR)
5492 return cp_parser_operator_function_id (parser);
5493 /* Fall through. */
5495 default:
5496 cp_parser_error (parser, "expected id-expression");
5497 return error_mark_node;
5500 else
5501 return cp_parser_unqualified_id (parser, template_keyword_p,
5502 /*check_dependency_p=*/true,
5503 declarator_p,
5504 optional_p);
5507 /* Parse an unqualified-id.
5509 unqualified-id:
5510 identifier
5511 operator-function-id
5512 conversion-function-id
5513 ~ class-name
5514 template-id
5516 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5517 keyword, in a construct like `A::template ...'.
5519 Returns a representation of unqualified-id. For the `identifier'
5520 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5521 production a BIT_NOT_EXPR is returned; the operand of the
5522 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5523 other productions, see the documentation accompanying the
5524 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5525 names are looked up in uninstantiated templates. If DECLARATOR_P
5526 is true, the unqualified-id is appearing as part of a declarator,
5527 rather than as part of an expression. */
5529 static cp_expr
5530 cp_parser_unqualified_id (cp_parser* parser,
5531 bool template_keyword_p,
5532 bool check_dependency_p,
5533 bool declarator_p,
5534 bool optional_p)
5536 cp_token *token;
5538 /* Peek at the next token. */
5539 token = cp_lexer_peek_token (parser->lexer);
5541 switch ((int) token->type)
5543 case CPP_NAME:
5545 tree id;
5547 /* We don't know yet whether or not this will be a
5548 template-id. */
5549 cp_parser_parse_tentatively (parser);
5550 /* Try a template-id. */
5551 id = cp_parser_template_id (parser, template_keyword_p,
5552 check_dependency_p,
5553 none_type,
5554 declarator_p);
5555 /* If it worked, we're done. */
5556 if (cp_parser_parse_definitely (parser))
5557 return id;
5558 /* Otherwise, it's an ordinary identifier. */
5559 return cp_parser_identifier (parser);
5562 case CPP_TEMPLATE_ID:
5563 return cp_parser_template_id (parser, template_keyword_p,
5564 check_dependency_p,
5565 none_type,
5566 declarator_p);
5568 case CPP_COMPL:
5570 tree type_decl;
5571 tree qualifying_scope;
5572 tree object_scope;
5573 tree scope;
5574 bool done;
5576 /* Consume the `~' token. */
5577 cp_lexer_consume_token (parser->lexer);
5578 /* Parse the class-name. The standard, as written, seems to
5579 say that:
5581 template <typename T> struct S { ~S (); };
5582 template <typename T> S<T>::~S() {}
5584 is invalid, since `~' must be followed by a class-name, but
5585 `S<T>' is dependent, and so not known to be a class.
5586 That's not right; we need to look in uninstantiated
5587 templates. A further complication arises from:
5589 template <typename T> void f(T t) {
5590 t.T::~T();
5593 Here, it is not possible to look up `T' in the scope of `T'
5594 itself. We must look in both the current scope, and the
5595 scope of the containing complete expression.
5597 Yet another issue is:
5599 struct S {
5600 int S;
5601 ~S();
5604 S::~S() {}
5606 The standard does not seem to say that the `S' in `~S'
5607 should refer to the type `S' and not the data member
5608 `S::S'. */
5610 /* DR 244 says that we look up the name after the "~" in the
5611 same scope as we looked up the qualifying name. That idea
5612 isn't fully worked out; it's more complicated than that. */
5613 scope = parser->scope;
5614 object_scope = parser->object_scope;
5615 qualifying_scope = parser->qualifying_scope;
5617 /* Check for invalid scopes. */
5618 if (scope == error_mark_node)
5620 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5621 cp_lexer_consume_token (parser->lexer);
5622 return error_mark_node;
5624 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5626 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5627 error_at (token->location,
5628 "scope %qT before %<~%> is not a class-name",
5629 scope);
5630 cp_parser_simulate_error (parser);
5631 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5632 cp_lexer_consume_token (parser->lexer);
5633 return error_mark_node;
5635 gcc_assert (!scope || TYPE_P (scope));
5637 /* If the name is of the form "X::~X" it's OK even if X is a
5638 typedef. */
5639 token = cp_lexer_peek_token (parser->lexer);
5640 if (scope
5641 && token->type == CPP_NAME
5642 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5643 != CPP_LESS)
5644 && (token->u.value == TYPE_IDENTIFIER (scope)
5645 || (CLASS_TYPE_P (scope)
5646 && constructor_name_p (token->u.value, scope))))
5648 cp_lexer_consume_token (parser->lexer);
5649 return build_nt (BIT_NOT_EXPR, scope);
5652 /* ~auto means the destructor of whatever the object is. */
5653 if (cp_parser_is_keyword (token, RID_AUTO))
5655 if (cxx_dialect < cxx14)
5656 pedwarn (input_location, 0,
5657 "%<~auto%> only available with "
5658 "-std=c++14 or -std=gnu++14");
5659 cp_lexer_consume_token (parser->lexer);
5660 return build_nt (BIT_NOT_EXPR, make_auto ());
5663 /* If there was an explicit qualification (S::~T), first look
5664 in the scope given by the qualification (i.e., S).
5666 Note: in the calls to cp_parser_class_name below we pass
5667 typename_type so that lookup finds the injected-class-name
5668 rather than the constructor. */
5669 done = false;
5670 type_decl = NULL_TREE;
5671 if (scope)
5673 cp_parser_parse_tentatively (parser);
5674 type_decl = cp_parser_class_name (parser,
5675 /*typename_keyword_p=*/false,
5676 /*template_keyword_p=*/false,
5677 typename_type,
5678 /*check_dependency=*/false,
5679 /*class_head_p=*/false,
5680 declarator_p);
5681 if (cp_parser_parse_definitely (parser))
5682 done = true;
5684 /* In "N::S::~S", look in "N" as well. */
5685 if (!done && scope && qualifying_scope)
5687 cp_parser_parse_tentatively (parser);
5688 parser->scope = qualifying_scope;
5689 parser->object_scope = NULL_TREE;
5690 parser->qualifying_scope = NULL_TREE;
5691 type_decl
5692 = cp_parser_class_name (parser,
5693 /*typename_keyword_p=*/false,
5694 /*template_keyword_p=*/false,
5695 typename_type,
5696 /*check_dependency=*/false,
5697 /*class_head_p=*/false,
5698 declarator_p);
5699 if (cp_parser_parse_definitely (parser))
5700 done = true;
5702 /* In "p->S::~T", look in the scope given by "*p" as well. */
5703 else if (!done && object_scope)
5705 cp_parser_parse_tentatively (parser);
5706 parser->scope = object_scope;
5707 parser->object_scope = NULL_TREE;
5708 parser->qualifying_scope = NULL_TREE;
5709 type_decl
5710 = cp_parser_class_name (parser,
5711 /*typename_keyword_p=*/false,
5712 /*template_keyword_p=*/false,
5713 typename_type,
5714 /*check_dependency=*/false,
5715 /*class_head_p=*/false,
5716 declarator_p);
5717 if (cp_parser_parse_definitely (parser))
5718 done = true;
5720 /* Look in the surrounding context. */
5721 if (!done)
5723 parser->scope = NULL_TREE;
5724 parser->object_scope = NULL_TREE;
5725 parser->qualifying_scope = NULL_TREE;
5726 if (processing_template_decl)
5727 cp_parser_parse_tentatively (parser);
5728 type_decl
5729 = cp_parser_class_name (parser,
5730 /*typename_keyword_p=*/false,
5731 /*template_keyword_p=*/false,
5732 typename_type,
5733 /*check_dependency=*/false,
5734 /*class_head_p=*/false,
5735 declarator_p);
5736 if (processing_template_decl
5737 && ! cp_parser_parse_definitely (parser))
5739 /* We couldn't find a type with this name. If we're parsing
5740 tentatively, fail and try something else. */
5741 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5743 cp_parser_simulate_error (parser);
5744 return error_mark_node;
5746 /* Otherwise, accept it and check for a match at instantiation
5747 time. */
5748 type_decl = cp_parser_identifier (parser);
5749 if (type_decl != error_mark_node)
5750 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5751 return type_decl;
5754 /* If an error occurred, assume that the name of the
5755 destructor is the same as the name of the qualifying
5756 class. That allows us to keep parsing after running
5757 into ill-formed destructor names. */
5758 if (type_decl == error_mark_node && scope)
5759 return build_nt (BIT_NOT_EXPR, scope);
5760 else if (type_decl == error_mark_node)
5761 return error_mark_node;
5763 /* Check that destructor name and scope match. */
5764 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5766 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5767 error_at (token->location,
5768 "declaration of %<~%T%> as member of %qT",
5769 type_decl, scope);
5770 cp_parser_simulate_error (parser);
5771 return error_mark_node;
5774 /* [class.dtor]
5776 A typedef-name that names a class shall not be used as the
5777 identifier in the declarator for a destructor declaration. */
5778 if (declarator_p
5779 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5780 && !DECL_SELF_REFERENCE_P (type_decl)
5781 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5782 error_at (token->location,
5783 "typedef-name %qD used as destructor declarator",
5784 type_decl);
5786 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5789 case CPP_KEYWORD:
5790 if (token->keyword == RID_OPERATOR)
5792 cp_expr id;
5794 /* This could be a template-id, so we try that first. */
5795 cp_parser_parse_tentatively (parser);
5796 /* Try a template-id. */
5797 id = cp_parser_template_id (parser, template_keyword_p,
5798 /*check_dependency_p=*/true,
5799 none_type,
5800 declarator_p);
5801 /* If that worked, we're done. */
5802 if (cp_parser_parse_definitely (parser))
5803 return id;
5804 /* We still don't know whether we're looking at an
5805 operator-function-id or a conversion-function-id. */
5806 cp_parser_parse_tentatively (parser);
5807 /* Try an operator-function-id. */
5808 id = cp_parser_operator_function_id (parser);
5809 /* If that didn't work, try a conversion-function-id. */
5810 if (!cp_parser_parse_definitely (parser))
5811 id = cp_parser_conversion_function_id (parser);
5812 else if (UDLIT_OPER_P (id))
5814 /* 17.6.3.3.5 */
5815 const char *name = UDLIT_OP_SUFFIX (id);
5816 if (name[0] != '_' && !in_system_header_at (input_location)
5817 && declarator_p)
5818 warning (0, "literal operator suffixes not preceded by %<_%>"
5819 " are reserved for future standardization");
5822 return id;
5824 /* Fall through. */
5826 default:
5827 if (optional_p)
5828 return NULL_TREE;
5829 cp_parser_error (parser, "expected unqualified-id");
5830 return error_mark_node;
5834 /* Parse an (optional) nested-name-specifier.
5836 nested-name-specifier: [C++98]
5837 class-or-namespace-name :: nested-name-specifier [opt]
5838 class-or-namespace-name :: template nested-name-specifier [opt]
5840 nested-name-specifier: [C++0x]
5841 type-name ::
5842 namespace-name ::
5843 nested-name-specifier identifier ::
5844 nested-name-specifier template [opt] simple-template-id ::
5846 PARSER->SCOPE should be set appropriately before this function is
5847 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5848 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5849 in name lookups.
5851 Sets PARSER->SCOPE to the class (TYPE) or namespace
5852 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5853 it unchanged if there is no nested-name-specifier. Returns the new
5854 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5856 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5857 part of a declaration and/or decl-specifier. */
5859 static tree
5860 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5861 bool typename_keyword_p,
5862 bool check_dependency_p,
5863 bool type_p,
5864 bool is_declaration)
5866 bool success = false;
5867 cp_token_position start = 0;
5868 cp_token *token;
5870 /* Remember where the nested-name-specifier starts. */
5871 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5873 start = cp_lexer_token_position (parser->lexer, false);
5874 push_deferring_access_checks (dk_deferred);
5877 while (true)
5879 tree new_scope;
5880 tree old_scope;
5881 tree saved_qualifying_scope;
5882 bool template_keyword_p;
5884 /* Spot cases that cannot be the beginning of a
5885 nested-name-specifier. */
5886 token = cp_lexer_peek_token (parser->lexer);
5888 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5889 the already parsed nested-name-specifier. */
5890 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5892 /* Grab the nested-name-specifier and continue the loop. */
5893 cp_parser_pre_parsed_nested_name_specifier (parser);
5894 /* If we originally encountered this nested-name-specifier
5895 with IS_DECLARATION set to false, we will not have
5896 resolved TYPENAME_TYPEs, so we must do so here. */
5897 if (is_declaration
5898 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5900 new_scope = resolve_typename_type (parser->scope,
5901 /*only_current_p=*/false);
5902 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5903 parser->scope = new_scope;
5905 success = true;
5906 continue;
5909 /* Spot cases that cannot be the beginning of a
5910 nested-name-specifier. On the second and subsequent times
5911 through the loop, we look for the `template' keyword. */
5912 if (success && token->keyword == RID_TEMPLATE)
5914 /* A template-id can start a nested-name-specifier. */
5915 else if (token->type == CPP_TEMPLATE_ID)
5917 /* DR 743: decltype can be used in a nested-name-specifier. */
5918 else if (token_is_decltype (token))
5920 else
5922 /* If the next token is not an identifier, then it is
5923 definitely not a type-name or namespace-name. */
5924 if (token->type != CPP_NAME)
5925 break;
5926 /* If the following token is neither a `<' (to begin a
5927 template-id), nor a `::', then we are not looking at a
5928 nested-name-specifier. */
5929 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5931 if (token->type == CPP_COLON
5932 && parser->colon_corrects_to_scope_p
5933 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5935 error_at (token->location,
5936 "found %<:%> in nested-name-specifier, expected %<::%>");
5937 token->type = CPP_SCOPE;
5940 if (token->type != CPP_SCOPE
5941 && !cp_parser_nth_token_starts_template_argument_list_p
5942 (parser, 2))
5943 break;
5946 /* The nested-name-specifier is optional, so we parse
5947 tentatively. */
5948 cp_parser_parse_tentatively (parser);
5950 /* Look for the optional `template' keyword, if this isn't the
5951 first time through the loop. */
5952 if (success)
5953 template_keyword_p = cp_parser_optional_template_keyword (parser);
5954 else
5955 template_keyword_p = false;
5957 /* Save the old scope since the name lookup we are about to do
5958 might destroy it. */
5959 old_scope = parser->scope;
5960 saved_qualifying_scope = parser->qualifying_scope;
5961 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5962 look up names in "X<T>::I" in order to determine that "Y" is
5963 a template. So, if we have a typename at this point, we make
5964 an effort to look through it. */
5965 if (is_declaration
5966 && !typename_keyword_p
5967 && parser->scope
5968 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5969 parser->scope = resolve_typename_type (parser->scope,
5970 /*only_current_p=*/false);
5971 /* Parse the qualifying entity. */
5972 new_scope
5973 = cp_parser_qualifying_entity (parser,
5974 typename_keyword_p,
5975 template_keyword_p,
5976 check_dependency_p,
5977 type_p,
5978 is_declaration);
5979 /* Look for the `::' token. */
5980 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5982 /* If we found what we wanted, we keep going; otherwise, we're
5983 done. */
5984 if (!cp_parser_parse_definitely (parser))
5986 bool error_p = false;
5988 /* Restore the OLD_SCOPE since it was valid before the
5989 failed attempt at finding the last
5990 class-or-namespace-name. */
5991 parser->scope = old_scope;
5992 parser->qualifying_scope = saved_qualifying_scope;
5994 /* If the next token is a decltype, and the one after that is a
5995 `::', then the decltype has failed to resolve to a class or
5996 enumeration type. Give this error even when parsing
5997 tentatively since it can't possibly be valid--and we're going
5998 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5999 won't get another chance.*/
6000 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6001 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6002 == CPP_SCOPE))
6004 token = cp_lexer_consume_token (parser->lexer);
6005 error_at (token->location, "decltype evaluates to %qT, "
6006 "which is not a class or enumeration type",
6007 token->u.tree_check_value->value);
6008 parser->scope = error_mark_node;
6009 error_p = true;
6010 /* As below. */
6011 success = true;
6012 cp_lexer_consume_token (parser->lexer);
6015 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6016 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6018 /* If we have a non-type template-id followed by ::, it can't
6019 possibly be valid. */
6020 token = cp_lexer_peek_token (parser->lexer);
6021 tree tid = token->u.tree_check_value->value;
6022 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6023 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6025 tree tmpl = NULL_TREE;
6026 if (is_overloaded_fn (tid))
6028 tree fns = get_fns (tid);
6029 if (!OVL_CHAIN (fns))
6030 tmpl = OVL_CURRENT (fns);
6031 error_at (token->location, "function template-id %qD "
6032 "in nested-name-specifier", tid);
6034 else
6036 /* Variable template. */
6037 tmpl = TREE_OPERAND (tid, 0);
6038 gcc_assert (variable_template_p (tmpl));
6039 error_at (token->location, "variable template-id %qD "
6040 "in nested-name-specifier", tid);
6042 if (tmpl)
6043 inform (DECL_SOURCE_LOCATION (tmpl),
6044 "%qD declared here", tmpl);
6046 parser->scope = error_mark_node;
6047 error_p = true;
6048 /* As below. */
6049 success = true;
6050 cp_lexer_consume_token (parser->lexer);
6051 cp_lexer_consume_token (parser->lexer);
6055 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6056 break;
6057 /* If the next token is an identifier, and the one after
6058 that is a `::', then any valid interpretation would have
6059 found a class-or-namespace-name. */
6060 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6061 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6062 == CPP_SCOPE)
6063 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6064 != CPP_COMPL))
6066 token = cp_lexer_consume_token (parser->lexer);
6067 if (!error_p)
6069 if (!token->error_reported)
6071 tree decl;
6072 tree ambiguous_decls;
6074 decl = cp_parser_lookup_name (parser, token->u.value,
6075 none_type,
6076 /*is_template=*/false,
6077 /*is_namespace=*/false,
6078 /*check_dependency=*/true,
6079 &ambiguous_decls,
6080 token->location);
6081 if (TREE_CODE (decl) == TEMPLATE_DECL)
6082 error_at (token->location,
6083 "%qD used without template parameters",
6084 decl);
6085 else if (ambiguous_decls)
6087 // cp_parser_lookup_name has the same diagnostic,
6088 // thus make sure to emit it at most once.
6089 if (cp_parser_uncommitted_to_tentative_parse_p
6090 (parser))
6092 error_at (token->location,
6093 "reference to %qD is ambiguous",
6094 token->u.value);
6095 print_candidates (ambiguous_decls);
6097 decl = error_mark_node;
6099 else
6101 if (cxx_dialect != cxx98)
6102 cp_parser_name_lookup_error
6103 (parser, token->u.value, decl, NLE_NOT_CXX98,
6104 token->location);
6105 else
6106 cp_parser_name_lookup_error
6107 (parser, token->u.value, decl, NLE_CXX98,
6108 token->location);
6111 parser->scope = error_mark_node;
6112 error_p = true;
6113 /* Treat this as a successful nested-name-specifier
6114 due to:
6116 [basic.lookup.qual]
6118 If the name found is not a class-name (clause
6119 _class_) or namespace-name (_namespace.def_), the
6120 program is ill-formed. */
6121 success = true;
6123 cp_lexer_consume_token (parser->lexer);
6125 break;
6127 /* We've found one valid nested-name-specifier. */
6128 success = true;
6129 /* Name lookup always gives us a DECL. */
6130 if (TREE_CODE (new_scope) == TYPE_DECL)
6131 new_scope = TREE_TYPE (new_scope);
6132 /* Uses of "template" must be followed by actual templates. */
6133 if (template_keyword_p
6134 && !(CLASS_TYPE_P (new_scope)
6135 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6136 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6137 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6138 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6139 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6140 == TEMPLATE_ID_EXPR)))
6141 permerror (input_location, TYPE_P (new_scope)
6142 ? G_("%qT is not a template")
6143 : G_("%qD is not a template"),
6144 new_scope);
6145 /* If it is a class scope, try to complete it; we are about to
6146 be looking up names inside the class. */
6147 if (TYPE_P (new_scope)
6148 /* Since checking types for dependency can be expensive,
6149 avoid doing it if the type is already complete. */
6150 && !COMPLETE_TYPE_P (new_scope)
6151 /* Do not try to complete dependent types. */
6152 && !dependent_type_p (new_scope))
6154 new_scope = complete_type (new_scope);
6155 /* If it is a typedef to current class, use the current
6156 class instead, as the typedef won't have any names inside
6157 it yet. */
6158 if (!COMPLETE_TYPE_P (new_scope)
6159 && currently_open_class (new_scope))
6160 new_scope = TYPE_MAIN_VARIANT (new_scope);
6162 /* Make sure we look in the right scope the next time through
6163 the loop. */
6164 parser->scope = new_scope;
6167 /* If parsing tentatively, replace the sequence of tokens that makes
6168 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6169 token. That way, should we re-parse the token stream, we will
6170 not have to repeat the effort required to do the parse, nor will
6171 we issue duplicate error messages. */
6172 if (success && start)
6174 cp_token *token;
6176 token = cp_lexer_token_at (parser->lexer, start);
6177 /* Reset the contents of the START token. */
6178 token->type = CPP_NESTED_NAME_SPECIFIER;
6179 /* Retrieve any deferred checks. Do not pop this access checks yet
6180 so the memory will not be reclaimed during token replacing below. */
6181 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6182 token->u.tree_check_value->value = parser->scope;
6183 token->u.tree_check_value->checks = get_deferred_access_checks ();
6184 token->u.tree_check_value->qualifying_scope =
6185 parser->qualifying_scope;
6186 token->keyword = RID_MAX;
6188 /* Purge all subsequent tokens. */
6189 cp_lexer_purge_tokens_after (parser->lexer, start);
6192 if (start)
6193 pop_to_parent_deferring_access_checks ();
6195 return success ? parser->scope : NULL_TREE;
6198 /* Parse a nested-name-specifier. See
6199 cp_parser_nested_name_specifier_opt for details. This function
6200 behaves identically, except that it will an issue an error if no
6201 nested-name-specifier is present. */
6203 static tree
6204 cp_parser_nested_name_specifier (cp_parser *parser,
6205 bool typename_keyword_p,
6206 bool check_dependency_p,
6207 bool type_p,
6208 bool is_declaration)
6210 tree scope;
6212 /* Look for the nested-name-specifier. */
6213 scope = cp_parser_nested_name_specifier_opt (parser,
6214 typename_keyword_p,
6215 check_dependency_p,
6216 type_p,
6217 is_declaration);
6218 /* If it was not present, issue an error message. */
6219 if (!scope)
6221 cp_parser_error (parser, "expected nested-name-specifier");
6222 parser->scope = NULL_TREE;
6225 return scope;
6228 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6229 this is either a class-name or a namespace-name (which corresponds
6230 to the class-or-namespace-name production in the grammar). For
6231 C++0x, it can also be a type-name that refers to an enumeration
6232 type or a simple-template-id.
6234 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6235 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6236 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6237 TYPE_P is TRUE iff the next name should be taken as a class-name,
6238 even the same name is declared to be another entity in the same
6239 scope.
6241 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6242 specified by the class-or-namespace-name. If neither is found the
6243 ERROR_MARK_NODE is returned. */
6245 static tree
6246 cp_parser_qualifying_entity (cp_parser *parser,
6247 bool typename_keyword_p,
6248 bool template_keyword_p,
6249 bool check_dependency_p,
6250 bool type_p,
6251 bool is_declaration)
6253 tree saved_scope;
6254 tree saved_qualifying_scope;
6255 tree saved_object_scope;
6256 tree scope;
6257 bool only_class_p;
6258 bool successful_parse_p;
6260 /* DR 743: decltype can appear in a nested-name-specifier. */
6261 if (cp_lexer_next_token_is_decltype (parser->lexer))
6263 scope = cp_parser_decltype (parser);
6264 if (TREE_CODE (scope) != ENUMERAL_TYPE
6265 && !MAYBE_CLASS_TYPE_P (scope))
6267 cp_parser_simulate_error (parser);
6268 return error_mark_node;
6270 if (TYPE_NAME (scope))
6271 scope = TYPE_NAME (scope);
6272 return scope;
6275 /* Before we try to parse the class-name, we must save away the
6276 current PARSER->SCOPE since cp_parser_class_name will destroy
6277 it. */
6278 saved_scope = parser->scope;
6279 saved_qualifying_scope = parser->qualifying_scope;
6280 saved_object_scope = parser->object_scope;
6281 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6282 there is no need to look for a namespace-name. */
6283 only_class_p = template_keyword_p
6284 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6285 if (!only_class_p)
6286 cp_parser_parse_tentatively (parser);
6287 scope = cp_parser_class_name (parser,
6288 typename_keyword_p,
6289 template_keyword_p,
6290 type_p ? class_type : none_type,
6291 check_dependency_p,
6292 /*class_head_p=*/false,
6293 is_declaration,
6294 /*enum_ok=*/cxx_dialect > cxx98);
6295 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6296 /* If that didn't work, try for a namespace-name. */
6297 if (!only_class_p && !successful_parse_p)
6299 /* Restore the saved scope. */
6300 parser->scope = saved_scope;
6301 parser->qualifying_scope = saved_qualifying_scope;
6302 parser->object_scope = saved_object_scope;
6303 /* If we are not looking at an identifier followed by the scope
6304 resolution operator, then this is not part of a
6305 nested-name-specifier. (Note that this function is only used
6306 to parse the components of a nested-name-specifier.) */
6307 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6308 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6309 return error_mark_node;
6310 scope = cp_parser_namespace_name (parser);
6313 return scope;
6316 /* Return true if we are looking at a compound-literal, false otherwise. */
6318 static bool
6319 cp_parser_compound_literal_p (cp_parser *parser)
6321 /* Consume the `('. */
6322 cp_lexer_consume_token (parser->lexer);
6324 cp_lexer_save_tokens (parser->lexer);
6326 /* Skip tokens until the next token is a closing parenthesis.
6327 If we find the closing `)', and the next token is a `{', then
6328 we are looking at a compound-literal. */
6329 bool compound_literal_p
6330 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6331 /*consume_paren=*/true)
6332 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6334 /* Roll back the tokens we skipped. */
6335 cp_lexer_rollback_tokens (parser->lexer);
6337 return compound_literal_p;
6340 /* Parse a postfix-expression.
6342 postfix-expression:
6343 primary-expression
6344 postfix-expression [ expression ]
6345 postfix-expression ( expression-list [opt] )
6346 simple-type-specifier ( expression-list [opt] )
6347 typename :: [opt] nested-name-specifier identifier
6348 ( expression-list [opt] )
6349 typename :: [opt] nested-name-specifier template [opt] template-id
6350 ( expression-list [opt] )
6351 postfix-expression . template [opt] id-expression
6352 postfix-expression -> template [opt] id-expression
6353 postfix-expression . pseudo-destructor-name
6354 postfix-expression -> pseudo-destructor-name
6355 postfix-expression ++
6356 postfix-expression --
6357 dynamic_cast < type-id > ( expression )
6358 static_cast < type-id > ( expression )
6359 reinterpret_cast < type-id > ( expression )
6360 const_cast < type-id > ( expression )
6361 typeid ( expression )
6362 typeid ( type-id )
6364 GNU Extension:
6366 postfix-expression:
6367 ( type-id ) { initializer-list , [opt] }
6369 This extension is a GNU version of the C99 compound-literal
6370 construct. (The C99 grammar uses `type-name' instead of `type-id',
6371 but they are essentially the same concept.)
6373 If ADDRESS_P is true, the postfix expression is the operand of the
6374 `&' operator. CAST_P is true if this expression is the target of a
6375 cast.
6377 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6378 class member access expressions [expr.ref].
6380 Returns a representation of the expression. */
6382 static cp_expr
6383 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6384 bool member_access_only_p, bool decltype_p,
6385 cp_id_kind * pidk_return)
6387 cp_token *token;
6388 location_t loc;
6389 enum rid keyword;
6390 cp_id_kind idk = CP_ID_KIND_NONE;
6391 cp_expr postfix_expression = NULL_TREE;
6392 bool is_member_access = false;
6393 int saved_in_statement = -1;
6395 /* Peek at the next token. */
6396 token = cp_lexer_peek_token (parser->lexer);
6397 loc = token->location;
6398 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6400 /* Some of the productions are determined by keywords. */
6401 keyword = token->keyword;
6402 switch (keyword)
6404 case RID_DYNCAST:
6405 case RID_STATCAST:
6406 case RID_REINTCAST:
6407 case RID_CONSTCAST:
6409 tree type;
6410 cp_expr expression;
6411 const char *saved_message;
6412 bool saved_in_type_id_in_expr_p;
6414 /* All of these can be handled in the same way from the point
6415 of view of parsing. Begin by consuming the token
6416 identifying the cast. */
6417 cp_lexer_consume_token (parser->lexer);
6419 /* New types cannot be defined in the cast. */
6420 saved_message = parser->type_definition_forbidden_message;
6421 parser->type_definition_forbidden_message
6422 = G_("types may not be defined in casts");
6424 /* Look for the opening `<'. */
6425 cp_parser_require (parser, CPP_LESS, RT_LESS);
6426 /* Parse the type to which we are casting. */
6427 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6428 parser->in_type_id_in_expr_p = true;
6429 type = cp_parser_type_id (parser);
6430 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6431 /* Look for the closing `>'. */
6432 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6433 /* Restore the old message. */
6434 parser->type_definition_forbidden_message = saved_message;
6436 bool saved_greater_than_is_operator_p
6437 = parser->greater_than_is_operator_p;
6438 parser->greater_than_is_operator_p = true;
6440 /* And the expression which is being cast. */
6441 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6442 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6443 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6444 RT_CLOSE_PAREN);
6445 location_t end_loc = close_paren ?
6446 close_paren->location : UNKNOWN_LOCATION;
6448 parser->greater_than_is_operator_p
6449 = saved_greater_than_is_operator_p;
6451 /* Only type conversions to integral or enumeration types
6452 can be used in constant-expressions. */
6453 if (!cast_valid_in_integral_constant_expression_p (type)
6454 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6456 postfix_expression = error_mark_node;
6457 break;
6460 switch (keyword)
6462 case RID_DYNCAST:
6463 postfix_expression
6464 = build_dynamic_cast (type, expression, tf_warning_or_error);
6465 break;
6466 case RID_STATCAST:
6467 postfix_expression
6468 = build_static_cast (type, expression, tf_warning_or_error);
6469 break;
6470 case RID_REINTCAST:
6471 postfix_expression
6472 = build_reinterpret_cast (type, expression,
6473 tf_warning_or_error);
6474 break;
6475 case RID_CONSTCAST:
6476 postfix_expression
6477 = build_const_cast (type, expression, tf_warning_or_error);
6478 break;
6479 default:
6480 gcc_unreachable ();
6483 /* Construct a location e.g. :
6484 reinterpret_cast <int *> (expr)
6485 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6486 ranging from the start of the "*_cast" token to the final closing
6487 paren, with the caret at the start. */
6488 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6489 postfix_expression.set_location (cp_cast_loc);
6491 break;
6493 case RID_TYPEID:
6495 tree type;
6496 const char *saved_message;
6497 bool saved_in_type_id_in_expr_p;
6499 /* Consume the `typeid' token. */
6500 cp_lexer_consume_token (parser->lexer);
6501 /* Look for the `(' token. */
6502 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6503 /* Types cannot be defined in a `typeid' expression. */
6504 saved_message = parser->type_definition_forbidden_message;
6505 parser->type_definition_forbidden_message
6506 = G_("types may not be defined in a %<typeid%> expression");
6507 /* We can't be sure yet whether we're looking at a type-id or an
6508 expression. */
6509 cp_parser_parse_tentatively (parser);
6510 /* Try a type-id first. */
6511 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6512 parser->in_type_id_in_expr_p = true;
6513 type = cp_parser_type_id (parser);
6514 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6515 /* Look for the `)' token. Otherwise, we can't be sure that
6516 we're not looking at an expression: consider `typeid (int
6517 (3))', for example. */
6518 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6519 /* If all went well, simply lookup the type-id. */
6520 if (cp_parser_parse_definitely (parser))
6521 postfix_expression = get_typeid (type, tf_warning_or_error);
6522 /* Otherwise, fall back to the expression variant. */
6523 else
6525 tree expression;
6527 /* Look for an expression. */
6528 expression = cp_parser_expression (parser, & idk);
6529 /* Compute its typeid. */
6530 postfix_expression = build_typeid (expression, tf_warning_or_error);
6531 /* Look for the `)' token. */
6532 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6534 /* Restore the saved message. */
6535 parser->type_definition_forbidden_message = saved_message;
6536 /* `typeid' may not appear in an integral constant expression. */
6537 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6538 postfix_expression = error_mark_node;
6540 break;
6542 case RID_TYPENAME:
6544 tree type;
6545 /* The syntax permitted here is the same permitted for an
6546 elaborated-type-specifier. */
6547 ++parser->prevent_constrained_type_specifiers;
6548 type = cp_parser_elaborated_type_specifier (parser,
6549 /*is_friend=*/false,
6550 /*is_declaration=*/false);
6551 --parser->prevent_constrained_type_specifiers;
6552 postfix_expression = cp_parser_functional_cast (parser, type);
6554 break;
6556 case RID_CILK_SPAWN:
6558 location_t cilk_spawn_loc
6559 = cp_lexer_peek_token (parser->lexer)->location;
6560 cp_lexer_consume_token (parser->lexer);
6561 token = cp_lexer_peek_token (parser->lexer);
6562 if (token->type == CPP_SEMICOLON)
6564 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6565 "an expression");
6566 postfix_expression = error_mark_node;
6567 break;
6569 else if (!current_function_decl)
6571 error_at (token->location, "%<_Cilk_spawn%> may only be used "
6572 "inside a function");
6573 postfix_expression = error_mark_node;
6574 break;
6576 else
6578 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6579 saved_in_statement = parser->in_statement;
6580 parser->in_statement |= IN_CILK_SPAWN;
6582 cfun->calls_cilk_spawn = 1;
6583 postfix_expression =
6584 cp_parser_postfix_expression (parser, false, false,
6585 false, false, &idk);
6586 if (!flag_cilkplus)
6588 error_at (token->location, "-fcilkplus must be enabled to use"
6589 " %<_Cilk_spawn%>");
6590 cfun->calls_cilk_spawn = 0;
6592 else if (saved_in_statement & IN_CILK_SPAWN)
6594 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6595 "are not permitted");
6596 postfix_expression = error_mark_node;
6597 cfun->calls_cilk_spawn = 0;
6599 else
6601 location_t loc = postfix_expression.get_location ();
6602 postfix_expression = build_cilk_spawn (token->location,
6603 postfix_expression);
6604 /* Build a location of the form:
6605 _Cilk_spawn expr
6606 ~~~~~~~~~~~~^~~~
6607 with caret at the expr, ranging from the start of the
6608 _Cilk_spawn token to the end of the expression. */
6609 location_t combined_loc =
6610 make_location (loc, cilk_spawn_loc, get_finish (loc));
6611 postfix_expression.set_location (combined_loc);
6612 if (postfix_expression != error_mark_node)
6613 SET_EXPR_LOCATION (postfix_expression, input_location);
6614 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6616 break;
6619 case RID_ADDRESSOF:
6620 case RID_BUILTIN_SHUFFLE:
6621 case RID_BUILTIN_LAUNDER:
6623 vec<tree, va_gc> *vec;
6624 unsigned int i;
6625 tree p;
6627 cp_lexer_consume_token (parser->lexer);
6628 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6629 /*cast_p=*/false, /*allow_expansion_p=*/true,
6630 /*non_constant_p=*/NULL);
6631 if (vec == NULL)
6633 postfix_expression = error_mark_node;
6634 break;
6637 FOR_EACH_VEC_ELT (*vec, i, p)
6638 mark_exp_read (p);
6640 switch (keyword)
6642 case RID_ADDRESSOF:
6643 if (vec->length () == 1)
6644 postfix_expression
6645 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6646 else
6648 error_at (loc, "wrong number of arguments to "
6649 "%<__builtin_addressof%>");
6650 postfix_expression = error_mark_node;
6652 break;
6654 case RID_BUILTIN_LAUNDER:
6655 if (vec->length () == 1)
6656 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6657 tf_warning_or_error);
6658 else
6660 error_at (loc, "wrong number of arguments to "
6661 "%<__builtin_launder%>");
6662 postfix_expression = error_mark_node;
6664 break;
6666 case RID_BUILTIN_SHUFFLE:
6667 if (vec->length () == 2)
6668 postfix_expression
6669 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6670 (*vec)[1], tf_warning_or_error);
6671 else if (vec->length () == 3)
6672 postfix_expression
6673 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6674 (*vec)[2], tf_warning_or_error);
6675 else
6677 error_at (loc, "wrong number of arguments to "
6678 "%<__builtin_shuffle%>");
6679 postfix_expression = error_mark_node;
6681 break;
6683 default:
6684 gcc_unreachable ();
6686 break;
6689 default:
6691 tree type;
6693 /* If the next thing is a simple-type-specifier, we may be
6694 looking at a functional cast. We could also be looking at
6695 an id-expression. So, we try the functional cast, and if
6696 that doesn't work we fall back to the primary-expression. */
6697 cp_parser_parse_tentatively (parser);
6698 /* Look for the simple-type-specifier. */
6699 ++parser->prevent_constrained_type_specifiers;
6700 type = cp_parser_simple_type_specifier (parser,
6701 /*decl_specs=*/NULL,
6702 CP_PARSER_FLAGS_NONE);
6703 --parser->prevent_constrained_type_specifiers;
6704 /* Parse the cast itself. */
6705 if (!cp_parser_error_occurred (parser))
6706 postfix_expression
6707 = cp_parser_functional_cast (parser, type);
6708 /* If that worked, we're done. */
6709 if (cp_parser_parse_definitely (parser))
6710 break;
6712 /* If the functional-cast didn't work out, try a
6713 compound-literal. */
6714 if (cp_parser_allow_gnu_extensions_p (parser)
6715 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6717 cp_expr initializer = NULL_TREE;
6719 cp_parser_parse_tentatively (parser);
6721 /* Avoid calling cp_parser_type_id pointlessly, see comment
6722 in cp_parser_cast_expression about c++/29234. */
6723 if (!cp_parser_compound_literal_p (parser))
6724 cp_parser_simulate_error (parser);
6725 else
6727 /* Parse the type. */
6728 bool 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 `)'. */
6733 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6736 /* If things aren't going well, there's no need to
6737 keep going. */
6738 if (!cp_parser_error_occurred (parser))
6740 bool non_constant_p;
6741 /* Parse the brace-enclosed initializer list. */
6742 initializer = cp_parser_braced_list (parser,
6743 &non_constant_p);
6745 /* If that worked, we're definitely looking at a
6746 compound-literal expression. */
6747 if (cp_parser_parse_definitely (parser))
6749 /* Warn the user that a compound literal is not
6750 allowed in standard C++. */
6751 pedwarn (input_location, OPT_Wpedantic,
6752 "ISO C++ forbids compound-literals");
6753 /* For simplicity, we disallow compound literals in
6754 constant-expressions. We could
6755 allow compound literals of integer type, whose
6756 initializer was a constant, in constant
6757 expressions. Permitting that usage, as a further
6758 extension, would not change the meaning of any
6759 currently accepted programs. (Of course, as
6760 compound literals are not part of ISO C++, the
6761 standard has nothing to say.) */
6762 if (cp_parser_non_integral_constant_expression (parser,
6763 NIC_NCC))
6765 postfix_expression = error_mark_node;
6766 break;
6768 /* Form the representation of the compound-literal. */
6769 postfix_expression
6770 = finish_compound_literal (type, initializer,
6771 tf_warning_or_error);
6772 postfix_expression.set_location (initializer.get_location ());
6773 break;
6777 /* It must be a primary-expression. */
6778 postfix_expression
6779 = cp_parser_primary_expression (parser, address_p, cast_p,
6780 /*template_arg_p=*/false,
6781 decltype_p,
6782 &idk);
6784 break;
6787 /* Note that we don't need to worry about calling build_cplus_new on a
6788 class-valued CALL_EXPR in decltype when it isn't the end of the
6789 postfix-expression; unary_complex_lvalue will take care of that for
6790 all these cases. */
6792 /* Keep looping until the postfix-expression is complete. */
6793 while (true)
6795 if (idk == CP_ID_KIND_UNQUALIFIED
6796 && identifier_p (postfix_expression)
6797 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6798 /* It is not a Koenig lookup function call. */
6799 postfix_expression
6800 = unqualified_name_lookup_error (postfix_expression);
6802 /* Peek at the next token. */
6803 token = cp_lexer_peek_token (parser->lexer);
6805 switch (token->type)
6807 case CPP_OPEN_SQUARE:
6808 if (cp_next_tokens_can_be_std_attribute_p (parser))
6810 cp_parser_error (parser,
6811 "two consecutive %<[%> shall "
6812 "only introduce an attribute");
6813 return error_mark_node;
6815 postfix_expression
6816 = cp_parser_postfix_open_square_expression (parser,
6817 postfix_expression,
6818 false,
6819 decltype_p);
6820 postfix_expression.set_range (start_loc,
6821 postfix_expression.get_location ());
6823 idk = CP_ID_KIND_NONE;
6824 is_member_access = false;
6825 break;
6827 case CPP_OPEN_PAREN:
6828 /* postfix-expression ( expression-list [opt] ) */
6830 bool koenig_p;
6831 bool is_builtin_constant_p;
6832 bool saved_integral_constant_expression_p = false;
6833 bool saved_non_integral_constant_expression_p = false;
6834 tsubst_flags_t complain = complain_flags (decltype_p);
6835 vec<tree, va_gc> *args;
6836 location_t close_paren_loc = UNKNOWN_LOCATION;
6838 is_member_access = false;
6840 is_builtin_constant_p
6841 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6842 if (is_builtin_constant_p)
6844 /* The whole point of __builtin_constant_p is to allow
6845 non-constant expressions to appear as arguments. */
6846 saved_integral_constant_expression_p
6847 = parser->integral_constant_expression_p;
6848 saved_non_integral_constant_expression_p
6849 = parser->non_integral_constant_expression_p;
6850 parser->integral_constant_expression_p = false;
6852 args = (cp_parser_parenthesized_expression_list
6853 (parser, non_attr,
6854 /*cast_p=*/false, /*allow_expansion_p=*/true,
6855 /*non_constant_p=*/NULL,
6856 /*close_paren_loc=*/&close_paren_loc));
6857 if (is_builtin_constant_p)
6859 parser->integral_constant_expression_p
6860 = saved_integral_constant_expression_p;
6861 parser->non_integral_constant_expression_p
6862 = saved_non_integral_constant_expression_p;
6865 if (args == NULL)
6867 postfix_expression = error_mark_node;
6868 break;
6871 /* Function calls are not permitted in
6872 constant-expressions. */
6873 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6874 && cp_parser_non_integral_constant_expression (parser,
6875 NIC_FUNC_CALL))
6877 postfix_expression = error_mark_node;
6878 release_tree_vector (args);
6879 break;
6882 koenig_p = false;
6883 if (idk == CP_ID_KIND_UNQUALIFIED
6884 || idk == CP_ID_KIND_TEMPLATE_ID)
6886 if (identifier_p (postfix_expression))
6888 if (!args->is_empty ())
6890 koenig_p = true;
6891 if (!any_type_dependent_arguments_p (args))
6892 postfix_expression
6893 = perform_koenig_lookup (postfix_expression, args,
6894 complain);
6896 else
6897 postfix_expression
6898 = unqualified_fn_lookup_error (postfix_expression);
6900 /* We do not perform argument-dependent lookup if
6901 normal lookup finds a non-function, in accordance
6902 with the expected resolution of DR 218. */
6903 else if (!args->is_empty ()
6904 && is_overloaded_fn (postfix_expression))
6906 tree fn = get_first_fn (postfix_expression);
6907 fn = STRIP_TEMPLATE (fn);
6909 /* Do not do argument dependent lookup if regular
6910 lookup finds a member function or a block-scope
6911 function declaration. [basic.lookup.argdep]/3 */
6912 if (!DECL_FUNCTION_MEMBER_P (fn)
6913 && !DECL_LOCAL_FUNCTION_P (fn))
6915 koenig_p = true;
6916 if (!any_type_dependent_arguments_p (args))
6917 postfix_expression
6918 = perform_koenig_lookup (postfix_expression, args,
6919 complain);
6924 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6925 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6926 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6927 && vec_safe_length (args) == 3)
6929 tree arg0 = (*args)[0];
6930 tree arg1 = (*args)[1];
6931 tree arg2 = (*args)[2];
6932 int literal_mask = ((!!integer_zerop (arg1) << 1)
6933 | (!!integer_zerop (arg2) << 2));
6934 if (TREE_CODE (arg2) == CONST_DECL)
6935 arg2 = DECL_INITIAL (arg2);
6936 warn_for_memset (input_location, arg0, arg2, literal_mask);
6939 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6940 && warn_restrict)
6942 unsigned i;
6943 tree arg;
6944 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6945 TREE_VISITED (arg) = 0;
6947 unsigned param_pos = 0;
6948 for (tree decl = DECL_ARGUMENTS (postfix_expression);
6949 decl != NULL_TREE;
6950 decl = DECL_CHAIN (decl), param_pos++)
6952 tree type = TREE_TYPE (decl);
6953 if (POINTER_TYPE_P (type) && TYPE_RESTRICT (type)
6954 && !TYPE_READONLY (TREE_TYPE (type)))
6955 warn_for_restrict (param_pos, args);
6958 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6959 TREE_VISITED (arg) = 0;
6962 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6964 tree instance = TREE_OPERAND (postfix_expression, 0);
6965 tree fn = TREE_OPERAND (postfix_expression, 1);
6967 if (processing_template_decl
6968 && (type_dependent_object_expression_p (instance)
6969 || (!BASELINK_P (fn)
6970 && TREE_CODE (fn) != FIELD_DECL)
6971 || type_dependent_expression_p (fn)
6972 || any_type_dependent_arguments_p (args)))
6974 maybe_generic_this_capture (instance, fn);
6975 postfix_expression
6976 = build_nt_call_vec (postfix_expression, args);
6977 release_tree_vector (args);
6978 break;
6981 if (BASELINK_P (fn))
6983 postfix_expression
6984 = (build_new_method_call
6985 (instance, fn, &args, NULL_TREE,
6986 (idk == CP_ID_KIND_QUALIFIED
6987 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6988 : LOOKUP_NORMAL),
6989 /*fn_p=*/NULL,
6990 complain));
6992 else
6993 postfix_expression
6994 = finish_call_expr (postfix_expression, &args,
6995 /*disallow_virtual=*/false,
6996 /*koenig_p=*/false,
6997 complain);
6999 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7000 || TREE_CODE (postfix_expression) == MEMBER_REF
7001 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7002 postfix_expression = (build_offset_ref_call_from_tree
7003 (postfix_expression, &args,
7004 complain));
7005 else if (idk == CP_ID_KIND_QUALIFIED)
7006 /* A call to a static class member, or a namespace-scope
7007 function. */
7008 postfix_expression
7009 = finish_call_expr (postfix_expression, &args,
7010 /*disallow_virtual=*/true,
7011 koenig_p,
7012 complain);
7013 else
7014 /* All other function calls. */
7015 postfix_expression
7016 = finish_call_expr (postfix_expression, &args,
7017 /*disallow_virtual=*/false,
7018 koenig_p,
7019 complain);
7021 if (close_paren_loc != UNKNOWN_LOCATION)
7023 location_t combined_loc = make_location (token->location,
7024 start_loc,
7025 close_paren_loc);
7026 postfix_expression.set_location (combined_loc);
7029 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7030 idk = CP_ID_KIND_NONE;
7032 release_tree_vector (args);
7034 break;
7036 case CPP_DOT:
7037 case CPP_DEREF:
7038 /* postfix-expression . template [opt] id-expression
7039 postfix-expression . pseudo-destructor-name
7040 postfix-expression -> template [opt] id-expression
7041 postfix-expression -> pseudo-destructor-name */
7043 /* Consume the `.' or `->' operator. */
7044 cp_lexer_consume_token (parser->lexer);
7046 postfix_expression
7047 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7048 postfix_expression,
7049 false, &idk, loc);
7051 is_member_access = true;
7052 break;
7054 case CPP_PLUS_PLUS:
7055 /* postfix-expression ++ */
7056 /* Consume the `++' token. */
7057 cp_lexer_consume_token (parser->lexer);
7058 /* Generate a representation for the complete expression. */
7059 postfix_expression
7060 = finish_increment_expr (postfix_expression,
7061 POSTINCREMENT_EXPR);
7062 /* Increments may not appear in constant-expressions. */
7063 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7064 postfix_expression = error_mark_node;
7065 idk = CP_ID_KIND_NONE;
7066 is_member_access = false;
7067 break;
7069 case CPP_MINUS_MINUS:
7070 /* postfix-expression -- */
7071 /* Consume the `--' token. */
7072 cp_lexer_consume_token (parser->lexer);
7073 /* Generate a representation for the complete expression. */
7074 postfix_expression
7075 = finish_increment_expr (postfix_expression,
7076 POSTDECREMENT_EXPR);
7077 /* Decrements may not appear in constant-expressions. */
7078 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7079 postfix_expression = error_mark_node;
7080 idk = CP_ID_KIND_NONE;
7081 is_member_access = false;
7082 break;
7084 default:
7085 if (pidk_return != NULL)
7086 * pidk_return = idk;
7087 if (member_access_only_p)
7088 return is_member_access
7089 ? postfix_expression
7090 : cp_expr (error_mark_node);
7091 else
7092 return postfix_expression;
7096 /* We should never get here. */
7097 gcc_unreachable ();
7098 return error_mark_node;
7101 /* This function parses Cilk Plus array notations. If a normal array expr. is
7102 parsed then the array index is passed back to the caller through *INIT_INDEX
7103 and the function returns a NULL_TREE. If array notation expr. is parsed,
7104 then *INIT_INDEX is ignored by the caller and the function returns
7105 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
7106 error_mark_node. */
7108 static tree
7109 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
7110 tree array_value)
7112 cp_token *token = NULL;
7113 tree length_index, stride = NULL_TREE, value_tree, array_type;
7114 if (!array_value || array_value == error_mark_node)
7116 cp_parser_skip_to_end_of_statement (parser);
7117 return error_mark_node;
7120 array_type = TREE_TYPE (array_value);
7122 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
7123 parser->colon_corrects_to_scope_p = false;
7124 token = cp_lexer_peek_token (parser->lexer);
7126 if (!token)
7128 cp_parser_error (parser, "expected %<:%> or numeral");
7129 return error_mark_node;
7131 else if (token->type == CPP_COLON)
7133 /* Consume the ':'. */
7134 cp_lexer_consume_token (parser->lexer);
7136 /* If we are here, then we have a case like this A[:]. */
7137 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
7139 cp_parser_error (parser, "expected %<]%>");
7140 cp_parser_skip_to_end_of_statement (parser);
7141 return error_mark_node;
7143 *init_index = NULL_TREE;
7144 stride = NULL_TREE;
7145 length_index = NULL_TREE;
7147 else
7149 /* If we are here, then there are three valid possibilities:
7150 1. ARRAY [ EXP ]
7151 2. ARRAY [ EXP : EXP ]
7152 3. ARRAY [ EXP : EXP : EXP ] */
7154 *init_index = cp_parser_expression (parser);
7155 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
7157 /* This indicates that we have a normal array expression. */
7158 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7159 return NULL_TREE;
7162 /* Consume the ':'. */
7163 cp_lexer_consume_token (parser->lexer);
7164 length_index = cp_parser_expression (parser);
7165 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7167 cp_lexer_consume_token (parser->lexer);
7168 stride = cp_parser_expression (parser);
7171 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7173 if (*init_index == error_mark_node || length_index == error_mark_node
7174 || stride == error_mark_node || array_type == error_mark_node)
7176 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
7177 cp_lexer_consume_token (parser->lexer);
7178 return error_mark_node;
7180 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7182 value_tree = build_array_notation_ref (loc, array_value, *init_index,
7183 length_index, stride, array_type);
7184 return value_tree;
7187 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7188 by cp_parser_builtin_offsetof. We're looking for
7190 postfix-expression [ expression ]
7191 postfix-expression [ braced-init-list ] (C++11)
7193 FOR_OFFSETOF is set if we're being called in that context, which
7194 changes how we deal with integer constant expressions. */
7196 static tree
7197 cp_parser_postfix_open_square_expression (cp_parser *parser,
7198 tree postfix_expression,
7199 bool for_offsetof,
7200 bool decltype_p)
7202 tree index = NULL_TREE;
7203 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7204 bool saved_greater_than_is_operator_p;
7206 /* Consume the `[' token. */
7207 cp_lexer_consume_token (parser->lexer);
7209 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7210 parser->greater_than_is_operator_p = true;
7212 /* Parse the index expression. */
7213 /* ??? For offsetof, there is a question of what to allow here. If
7214 offsetof is not being used in an integral constant expression context,
7215 then we *could* get the right answer by computing the value at runtime.
7216 If we are in an integral constant expression context, then we might
7217 could accept any constant expression; hard to say without analysis.
7218 Rather than open the barn door too wide right away, allow only integer
7219 constant expressions here. */
7220 if (for_offsetof)
7221 index = cp_parser_constant_expression (parser);
7222 else
7224 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7226 bool expr_nonconst_p;
7227 cp_lexer_set_source_position (parser->lexer);
7228 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7229 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7230 if (flag_cilkplus
7231 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7233 error_at (cp_lexer_peek_token (parser->lexer)->location,
7234 "braced list index is not allowed with array "
7235 "notation");
7236 cp_parser_skip_to_end_of_statement (parser);
7237 return error_mark_node;
7240 else if (flag_cilkplus)
7242 /* Here are have these two options:
7243 ARRAY[EXP : EXP] - Array notation expr with default
7244 stride of 1.
7245 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7246 stride. */
7247 tree an_exp = cp_parser_array_notation (loc, parser, &index,
7248 postfix_expression);
7249 if (an_exp)
7250 return an_exp;
7252 else
7253 index = cp_parser_expression (parser);
7256 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7258 /* Look for the closing `]'. */
7259 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7261 /* Build the ARRAY_REF. */
7262 postfix_expression = grok_array_decl (loc, postfix_expression,
7263 index, decltype_p);
7265 /* When not doing offsetof, array references are not permitted in
7266 constant-expressions. */
7267 if (!for_offsetof
7268 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7269 postfix_expression = error_mark_node;
7271 return postfix_expression;
7274 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7275 by cp_parser_builtin_offsetof. We're looking for
7277 postfix-expression . template [opt] id-expression
7278 postfix-expression . pseudo-destructor-name
7279 postfix-expression -> template [opt] id-expression
7280 postfix-expression -> pseudo-destructor-name
7282 FOR_OFFSETOF is set if we're being called in that context. That sorta
7283 limits what of the above we'll actually accept, but nevermind.
7284 TOKEN_TYPE is the "." or "->" token, which will already have been
7285 removed from the stream. */
7287 static tree
7288 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7289 enum cpp_ttype token_type,
7290 cp_expr postfix_expression,
7291 bool for_offsetof, cp_id_kind *idk,
7292 location_t location)
7294 tree name;
7295 bool dependent_p;
7296 bool pseudo_destructor_p;
7297 tree scope = NULL_TREE;
7298 location_t start_loc = postfix_expression.get_start ();
7300 /* If this is a `->' operator, dereference the pointer. */
7301 if (token_type == CPP_DEREF)
7302 postfix_expression = build_x_arrow (location, postfix_expression,
7303 tf_warning_or_error);
7304 /* Check to see whether or not the expression is type-dependent and
7305 not the current instantiation. */
7306 dependent_p = type_dependent_object_expression_p (postfix_expression);
7307 /* The identifier following the `->' or `.' is not qualified. */
7308 parser->scope = NULL_TREE;
7309 parser->qualifying_scope = NULL_TREE;
7310 parser->object_scope = NULL_TREE;
7311 *idk = CP_ID_KIND_NONE;
7313 /* Enter the scope corresponding to the type of the object
7314 given by the POSTFIX_EXPRESSION. */
7315 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
7317 scope = TREE_TYPE (postfix_expression);
7318 /* According to the standard, no expression should ever have
7319 reference type. Unfortunately, we do not currently match
7320 the standard in this respect in that our internal representation
7321 of an expression may have reference type even when the standard
7322 says it does not. Therefore, we have to manually obtain the
7323 underlying type here. */
7324 scope = non_reference (scope);
7325 /* The type of the POSTFIX_EXPRESSION must be complete. */
7326 /* Unlike the object expression in other contexts, *this is not
7327 required to be of complete type for purposes of class member
7328 access (5.2.5) outside the member function body. */
7329 if (postfix_expression != current_class_ref
7330 && !(processing_template_decl
7331 && current_class_type
7332 && (same_type_ignoring_top_level_qualifiers_p
7333 (scope, current_class_type))))
7334 scope = complete_type_or_else (scope, postfix_expression);
7335 /* Let the name lookup machinery know that we are processing a
7336 class member access expression. */
7337 parser->context->object_type = scope;
7338 /* If something went wrong, we want to be able to discern that case,
7339 as opposed to the case where there was no SCOPE due to the type
7340 of expression being dependent. */
7341 if (!scope)
7342 scope = error_mark_node;
7343 /* If the SCOPE was erroneous, make the various semantic analysis
7344 functions exit quickly -- and without issuing additional error
7345 messages. */
7346 if (scope == error_mark_node)
7347 postfix_expression = error_mark_node;
7349 else
7350 /* Tell cp_parser_lookup_name that there was an object, even though it's
7351 type-dependent. */
7352 parser->context->object_type = unknown_type_node;
7354 /* Assume this expression is not a pseudo-destructor access. */
7355 pseudo_destructor_p = false;
7357 /* If the SCOPE is a scalar type, then, if this is a valid program,
7358 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7359 is type dependent, it can be pseudo-destructor-name or something else.
7360 Try to parse it as pseudo-destructor-name first. */
7361 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7363 tree s;
7364 tree type;
7366 cp_parser_parse_tentatively (parser);
7367 /* Parse the pseudo-destructor-name. */
7368 s = NULL_TREE;
7369 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7370 &s, &type);
7371 if (dependent_p
7372 && (cp_parser_error_occurred (parser)
7373 || !SCALAR_TYPE_P (type)))
7374 cp_parser_abort_tentative_parse (parser);
7375 else if (cp_parser_parse_definitely (parser))
7377 pseudo_destructor_p = true;
7378 postfix_expression
7379 = finish_pseudo_destructor_expr (postfix_expression,
7380 s, type, location);
7384 if (!pseudo_destructor_p)
7386 /* If the SCOPE is not a scalar type, we are looking at an
7387 ordinary class member access expression, rather than a
7388 pseudo-destructor-name. */
7389 bool template_p;
7390 cp_token *token = cp_lexer_peek_token (parser->lexer);
7391 /* Parse the id-expression. */
7392 name = (cp_parser_id_expression
7393 (parser,
7394 cp_parser_optional_template_keyword (parser),
7395 /*check_dependency_p=*/true,
7396 &template_p,
7397 /*declarator_p=*/false,
7398 /*optional_p=*/false));
7399 /* In general, build a SCOPE_REF if the member name is qualified.
7400 However, if the name was not dependent and has already been
7401 resolved; there is no need to build the SCOPE_REF. For example;
7403 struct X { void f(); };
7404 template <typename T> void f(T* t) { t->X::f(); }
7406 Even though "t" is dependent, "X::f" is not and has been resolved
7407 to a BASELINK; there is no need to include scope information. */
7409 /* But we do need to remember that there was an explicit scope for
7410 virtual function calls. */
7411 if (parser->scope)
7412 *idk = CP_ID_KIND_QUALIFIED;
7414 /* If the name is a template-id that names a type, we will get a
7415 TYPE_DECL here. That is invalid code. */
7416 if (TREE_CODE (name) == TYPE_DECL)
7418 error_at (token->location, "invalid use of %qD", name);
7419 postfix_expression = error_mark_node;
7421 else
7423 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7425 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7427 error_at (token->location, "%<%D::%D%> is not a class member",
7428 parser->scope, name);
7429 postfix_expression = error_mark_node;
7431 else
7432 name = build_qualified_name (/*type=*/NULL_TREE,
7433 parser->scope,
7434 name,
7435 template_p);
7436 parser->scope = NULL_TREE;
7437 parser->qualifying_scope = NULL_TREE;
7438 parser->object_scope = NULL_TREE;
7440 if (parser->scope && name && BASELINK_P (name))
7441 adjust_result_of_qualified_name_lookup
7442 (name, parser->scope, scope);
7443 postfix_expression
7444 = finish_class_member_access_expr (postfix_expression, name,
7445 template_p,
7446 tf_warning_or_error);
7447 /* Build a location e.g.:
7448 ptr->access_expr
7449 ~~~^~~~~~~~~~~~~
7450 where the caret is at the deref token, ranging from
7451 the start of postfix_expression to the end of the access expr. */
7452 location_t end_loc
7453 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7454 location_t combined_loc
7455 = make_location (input_location, start_loc, end_loc);
7456 protected_set_expr_location (postfix_expression, combined_loc);
7460 /* We no longer need to look up names in the scope of the object on
7461 the left-hand side of the `.' or `->' operator. */
7462 parser->context->object_type = NULL_TREE;
7464 /* Outside of offsetof, these operators may not appear in
7465 constant-expressions. */
7466 if (!for_offsetof
7467 && (cp_parser_non_integral_constant_expression
7468 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7469 postfix_expression = error_mark_node;
7471 return postfix_expression;
7474 /* Parse a parenthesized expression-list.
7476 expression-list:
7477 assignment-expression
7478 expression-list, assignment-expression
7480 attribute-list:
7481 expression-list
7482 identifier
7483 identifier, expression-list
7485 CAST_P is true if this expression is the target of a cast.
7487 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7488 argument pack.
7490 Returns a vector of trees. Each element is a representation of an
7491 assignment-expression. NULL is returned if the ( and or ) are
7492 missing. An empty, but allocated, vector is returned on no
7493 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7494 if we are parsing an attribute list for an attribute that wants a
7495 plain identifier argument, normal_attr for an attribute that wants
7496 an expression, or non_attr if we aren't parsing an attribute list. If
7497 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7498 not all of the expressions in the list were constant.
7499 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7500 will be written to with the location of the closing parenthesis. If
7501 an error occurs, it may or may not be written to. */
7503 static vec<tree, va_gc> *
7504 cp_parser_parenthesized_expression_list (cp_parser* parser,
7505 int is_attribute_list,
7506 bool cast_p,
7507 bool allow_expansion_p,
7508 bool *non_constant_p,
7509 location_t *close_paren_loc)
7511 vec<tree, va_gc> *expression_list;
7512 bool fold_expr_p = is_attribute_list != non_attr;
7513 tree identifier = NULL_TREE;
7514 bool saved_greater_than_is_operator_p;
7516 /* Assume all the expressions will be constant. */
7517 if (non_constant_p)
7518 *non_constant_p = false;
7520 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
7521 return NULL;
7523 expression_list = make_tree_vector ();
7525 /* Within a parenthesized expression, a `>' token is always
7526 the greater-than operator. */
7527 saved_greater_than_is_operator_p
7528 = parser->greater_than_is_operator_p;
7529 parser->greater_than_is_operator_p = true;
7531 /* Consume expressions until there are no more. */
7532 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7533 while (true)
7535 tree expr;
7537 /* At the beginning of attribute lists, check to see if the
7538 next token is an identifier. */
7539 if (is_attribute_list == id_attr
7540 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7542 cp_token *token;
7544 /* Consume the identifier. */
7545 token = cp_lexer_consume_token (parser->lexer);
7546 /* Save the identifier. */
7547 identifier = token->u.value;
7549 else
7551 bool expr_non_constant_p;
7553 /* Parse the next assignment-expression. */
7554 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7556 /* A braced-init-list. */
7557 cp_lexer_set_source_position (parser->lexer);
7558 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7559 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7560 if (non_constant_p && expr_non_constant_p)
7561 *non_constant_p = true;
7563 else if (non_constant_p)
7565 expr = (cp_parser_constant_expression
7566 (parser, /*allow_non_constant_p=*/true,
7567 &expr_non_constant_p));
7568 if (expr_non_constant_p)
7569 *non_constant_p = true;
7571 else
7572 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7573 cast_p);
7575 if (fold_expr_p)
7576 expr = instantiate_non_dependent_expr (expr);
7578 /* If we have an ellipsis, then this is an expression
7579 expansion. */
7580 if (allow_expansion_p
7581 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7583 /* Consume the `...'. */
7584 cp_lexer_consume_token (parser->lexer);
7586 /* Build the argument pack. */
7587 expr = make_pack_expansion (expr);
7590 /* Add it to the list. We add error_mark_node
7591 expressions to the list, so that we can still tell if
7592 the correct form for a parenthesized expression-list
7593 is found. That gives better errors. */
7594 vec_safe_push (expression_list, expr);
7596 if (expr == error_mark_node)
7597 goto skip_comma;
7600 /* After the first item, attribute lists look the same as
7601 expression lists. */
7602 is_attribute_list = non_attr;
7604 get_comma:;
7605 /* If the next token isn't a `,', then we are done. */
7606 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7607 break;
7609 /* Otherwise, consume the `,' and keep going. */
7610 cp_lexer_consume_token (parser->lexer);
7613 if (close_paren_loc)
7614 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7616 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7618 int ending;
7620 skip_comma:;
7621 /* We try and resync to an unnested comma, as that will give the
7622 user better diagnostics. */
7623 ending = cp_parser_skip_to_closing_parenthesis (parser,
7624 /*recovering=*/true,
7625 /*or_comma=*/true,
7626 /*consume_paren=*/true);
7627 if (ending < 0)
7628 goto get_comma;
7629 if (!ending)
7631 parser->greater_than_is_operator_p
7632 = saved_greater_than_is_operator_p;
7633 return NULL;
7637 parser->greater_than_is_operator_p
7638 = saved_greater_than_is_operator_p;
7640 if (identifier)
7641 vec_safe_insert (expression_list, 0, identifier);
7643 return expression_list;
7646 /* Parse a pseudo-destructor-name.
7648 pseudo-destructor-name:
7649 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7650 :: [opt] nested-name-specifier template template-id :: ~ type-name
7651 :: [opt] nested-name-specifier [opt] ~ type-name
7653 If either of the first two productions is used, sets *SCOPE to the
7654 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7655 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7656 or ERROR_MARK_NODE if the parse fails. */
7658 static void
7659 cp_parser_pseudo_destructor_name (cp_parser* parser,
7660 tree object,
7661 tree* scope,
7662 tree* type)
7664 bool nested_name_specifier_p;
7666 /* Handle ~auto. */
7667 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7668 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7669 && !type_dependent_expression_p (object))
7671 if (cxx_dialect < cxx14)
7672 pedwarn (input_location, 0,
7673 "%<~auto%> only available with "
7674 "-std=c++14 or -std=gnu++14");
7675 cp_lexer_consume_token (parser->lexer);
7676 cp_lexer_consume_token (parser->lexer);
7677 *scope = NULL_TREE;
7678 *type = TREE_TYPE (object);
7679 return;
7682 /* Assume that things will not work out. */
7683 *type = error_mark_node;
7685 /* Look for the optional `::' operator. */
7686 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7687 /* Look for the optional nested-name-specifier. */
7688 nested_name_specifier_p
7689 = (cp_parser_nested_name_specifier_opt (parser,
7690 /*typename_keyword_p=*/false,
7691 /*check_dependency_p=*/true,
7692 /*type_p=*/false,
7693 /*is_declaration=*/false)
7694 != NULL_TREE);
7695 /* Now, if we saw a nested-name-specifier, we might be doing the
7696 second production. */
7697 if (nested_name_specifier_p
7698 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7700 /* Consume the `template' keyword. */
7701 cp_lexer_consume_token (parser->lexer);
7702 /* Parse the template-id. */
7703 cp_parser_template_id (parser,
7704 /*template_keyword_p=*/true,
7705 /*check_dependency_p=*/false,
7706 class_type,
7707 /*is_declaration=*/true);
7708 /* Look for the `::' token. */
7709 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7711 /* If the next token is not a `~', then there might be some
7712 additional qualification. */
7713 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7715 /* At this point, we're looking for "type-name :: ~". The type-name
7716 must not be a class-name, since this is a pseudo-destructor. So,
7717 it must be either an enum-name, or a typedef-name -- both of which
7718 are just identifiers. So, we peek ahead to check that the "::"
7719 and "~" tokens are present; if they are not, then we can avoid
7720 calling type_name. */
7721 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7722 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7723 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7725 cp_parser_error (parser, "non-scalar type");
7726 return;
7729 /* Look for the type-name. */
7730 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7731 if (*scope == error_mark_node)
7732 return;
7734 /* Look for the `::' token. */
7735 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7737 else
7738 *scope = NULL_TREE;
7740 /* Look for the `~'. */
7741 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7743 /* Once we see the ~, this has to be a pseudo-destructor. */
7744 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7745 cp_parser_commit_to_topmost_tentative_parse (parser);
7747 /* Look for the type-name again. We are not responsible for
7748 checking that it matches the first type-name. */
7749 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7752 /* Parse a unary-expression.
7754 unary-expression:
7755 postfix-expression
7756 ++ cast-expression
7757 -- cast-expression
7758 unary-operator cast-expression
7759 sizeof unary-expression
7760 sizeof ( type-id )
7761 alignof ( type-id ) [C++0x]
7762 new-expression
7763 delete-expression
7765 GNU Extensions:
7767 unary-expression:
7768 __extension__ cast-expression
7769 __alignof__ unary-expression
7770 __alignof__ ( type-id )
7771 alignof unary-expression [C++0x]
7772 __real__ cast-expression
7773 __imag__ cast-expression
7774 && identifier
7775 sizeof ( type-id ) { initializer-list , [opt] }
7776 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7777 __alignof__ ( type-id ) { initializer-list , [opt] }
7779 ADDRESS_P is true iff the unary-expression is appearing as the
7780 operand of the `&' operator. CAST_P is true if this expression is
7781 the target of a cast.
7783 Returns a representation of the expression. */
7785 static cp_expr
7786 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7787 bool address_p, bool cast_p, bool decltype_p)
7789 cp_token *token;
7790 enum tree_code unary_operator;
7792 /* Peek at the next token. */
7793 token = cp_lexer_peek_token (parser->lexer);
7794 /* Some keywords give away the kind of expression. */
7795 if (token->type == CPP_KEYWORD)
7797 enum rid keyword = token->keyword;
7799 switch (keyword)
7801 case RID_ALIGNOF:
7802 case RID_SIZEOF:
7804 tree operand, ret;
7805 enum tree_code op;
7806 location_t first_loc;
7808 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7809 /* Consume the token. */
7810 cp_lexer_consume_token (parser->lexer);
7811 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7812 /* Parse the operand. */
7813 operand = cp_parser_sizeof_operand (parser, keyword);
7815 if (TYPE_P (operand))
7816 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7817 else
7819 /* ISO C++ defines alignof only with types, not with
7820 expressions. So pedwarn if alignof is used with a non-
7821 type expression. However, __alignof__ is ok. */
7822 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7823 pedwarn (token->location, OPT_Wpedantic,
7824 "ISO C++ does not allow %<alignof%> "
7825 "with a non-type");
7827 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7829 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7830 SIZEOF_EXPR with the original operand. */
7831 if (op == SIZEOF_EXPR && ret != error_mark_node)
7833 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7835 if (!processing_template_decl && TYPE_P (operand))
7837 ret = build_min (SIZEOF_EXPR, size_type_node,
7838 build1 (NOP_EXPR, operand,
7839 error_mark_node));
7840 SIZEOF_EXPR_TYPE_P (ret) = 1;
7842 else
7843 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7844 TREE_SIDE_EFFECTS (ret) = 0;
7845 TREE_READONLY (ret) = 1;
7847 SET_EXPR_LOCATION (ret, first_loc);
7849 return ret;
7852 case RID_NEW:
7853 return cp_parser_new_expression (parser);
7855 case RID_DELETE:
7856 return cp_parser_delete_expression (parser);
7858 case RID_EXTENSION:
7860 /* The saved value of the PEDANTIC flag. */
7861 int saved_pedantic;
7862 tree expr;
7864 /* Save away the PEDANTIC flag. */
7865 cp_parser_extension_opt (parser, &saved_pedantic);
7866 /* Parse the cast-expression. */
7867 expr = cp_parser_simple_cast_expression (parser);
7868 /* Restore the PEDANTIC flag. */
7869 pedantic = saved_pedantic;
7871 return expr;
7874 case RID_REALPART:
7875 case RID_IMAGPART:
7877 tree expression;
7879 /* Consume the `__real__' or `__imag__' token. */
7880 cp_lexer_consume_token (parser->lexer);
7881 /* Parse the cast-expression. */
7882 expression = cp_parser_simple_cast_expression (parser);
7883 /* Create the complete representation. */
7884 return build_x_unary_op (token->location,
7885 (keyword == RID_REALPART
7886 ? REALPART_EXPR : IMAGPART_EXPR),
7887 expression,
7888 tf_warning_or_error);
7890 break;
7892 case RID_TRANSACTION_ATOMIC:
7893 case RID_TRANSACTION_RELAXED:
7894 return cp_parser_transaction_expression (parser, keyword);
7896 case RID_NOEXCEPT:
7898 tree expr;
7899 const char *saved_message;
7900 bool saved_integral_constant_expression_p;
7901 bool saved_non_integral_constant_expression_p;
7902 bool saved_greater_than_is_operator_p;
7904 cp_lexer_consume_token (parser->lexer);
7905 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7907 saved_message = parser->type_definition_forbidden_message;
7908 parser->type_definition_forbidden_message
7909 = G_("types may not be defined in %<noexcept%> expressions");
7911 saved_integral_constant_expression_p
7912 = parser->integral_constant_expression_p;
7913 saved_non_integral_constant_expression_p
7914 = parser->non_integral_constant_expression_p;
7915 parser->integral_constant_expression_p = false;
7917 saved_greater_than_is_operator_p
7918 = parser->greater_than_is_operator_p;
7919 parser->greater_than_is_operator_p = true;
7921 ++cp_unevaluated_operand;
7922 ++c_inhibit_evaluation_warnings;
7923 ++cp_noexcept_operand;
7924 expr = cp_parser_expression (parser);
7925 --cp_noexcept_operand;
7926 --c_inhibit_evaluation_warnings;
7927 --cp_unevaluated_operand;
7929 parser->greater_than_is_operator_p
7930 = saved_greater_than_is_operator_p;
7932 parser->integral_constant_expression_p
7933 = saved_integral_constant_expression_p;
7934 parser->non_integral_constant_expression_p
7935 = saved_non_integral_constant_expression_p;
7937 parser->type_definition_forbidden_message = saved_message;
7939 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7940 return finish_noexcept_expr (expr, tf_warning_or_error);
7943 default:
7944 break;
7948 /* Look for the `:: new' and `:: delete', which also signal the
7949 beginning of a new-expression, or delete-expression,
7950 respectively. If the next token is `::', then it might be one of
7951 these. */
7952 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7954 enum rid keyword;
7956 /* See if the token after the `::' is one of the keywords in
7957 which we're interested. */
7958 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7959 /* If it's `new', we have a new-expression. */
7960 if (keyword == RID_NEW)
7961 return cp_parser_new_expression (parser);
7962 /* Similarly, for `delete'. */
7963 else if (keyword == RID_DELETE)
7964 return cp_parser_delete_expression (parser);
7967 /* Look for a unary operator. */
7968 unary_operator = cp_parser_unary_operator (token);
7969 /* The `++' and `--' operators can be handled similarly, even though
7970 they are not technically unary-operators in the grammar. */
7971 if (unary_operator == ERROR_MARK)
7973 if (token->type == CPP_PLUS_PLUS)
7974 unary_operator = PREINCREMENT_EXPR;
7975 else if (token->type == CPP_MINUS_MINUS)
7976 unary_operator = PREDECREMENT_EXPR;
7977 /* Handle the GNU address-of-label extension. */
7978 else if (cp_parser_allow_gnu_extensions_p (parser)
7979 && token->type == CPP_AND_AND)
7981 tree identifier;
7982 tree expression;
7983 location_t start_loc = token->location;
7985 /* Consume the '&&' token. */
7986 cp_lexer_consume_token (parser->lexer);
7987 /* Look for the identifier. */
7988 location_t finish_loc
7989 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
7990 identifier = cp_parser_identifier (parser);
7991 /* Construct a location of the form:
7992 &&label
7993 ^~~~~~~
7994 with caret==start at the "&&", finish at the end of the label. */
7995 location_t combined_loc
7996 = make_location (start_loc, start_loc, finish_loc);
7997 /* Create an expression representing the address. */
7998 expression = finish_label_address_expr (identifier, combined_loc);
7999 if (cp_parser_non_integral_constant_expression (parser,
8000 NIC_ADDR_LABEL))
8001 expression = error_mark_node;
8002 return expression;
8005 if (unary_operator != ERROR_MARK)
8007 cp_expr cast_expression;
8008 cp_expr expression = error_mark_node;
8009 non_integral_constant non_constant_p = NIC_NONE;
8010 location_t loc = token->location;
8011 tsubst_flags_t complain = complain_flags (decltype_p);
8013 /* Consume the operator token. */
8014 token = cp_lexer_consume_token (parser->lexer);
8015 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8017 /* Parse the cast-expression. */
8018 cast_expression
8019 = cp_parser_cast_expression (parser,
8020 unary_operator == ADDR_EXPR,
8021 /*cast_p=*/false,
8022 /*decltype*/false,
8023 pidk);
8025 /* Make a location:
8026 OP_TOKEN CAST_EXPRESSION
8027 ^~~~~~~~~~~~~~~~~~~~~~~~~
8028 with start==caret at the operator token, and
8029 extending to the end of the cast_expression. */
8030 loc = make_location (loc, loc, cast_expression.get_finish ());
8032 /* Now, build an appropriate representation. */
8033 switch (unary_operator)
8035 case INDIRECT_REF:
8036 non_constant_p = NIC_STAR;
8037 expression = build_x_indirect_ref (loc, cast_expression,
8038 RO_UNARY_STAR,
8039 complain);
8040 /* TODO: build_x_indirect_ref does not always honor the
8041 location, so ensure it is set. */
8042 expression.set_location (loc);
8043 break;
8045 case ADDR_EXPR:
8046 non_constant_p = NIC_ADDR;
8047 /* Fall through. */
8048 case BIT_NOT_EXPR:
8049 expression = build_x_unary_op (loc, unary_operator,
8050 cast_expression,
8051 complain);
8052 /* TODO: build_x_unary_op does not always honor the location,
8053 so ensure it is set. */
8054 expression.set_location (loc);
8055 break;
8057 case PREINCREMENT_EXPR:
8058 case PREDECREMENT_EXPR:
8059 non_constant_p = unary_operator == PREINCREMENT_EXPR
8060 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8061 /* Fall through. */
8062 case NEGATE_EXPR:
8063 /* Immediately fold negation of a constant, unless the constant is 0
8064 (since -0 == 0) or it would overflow. */
8065 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8066 && CONSTANT_CLASS_P (cast_expression)
8067 && !integer_zerop (cast_expression)
8068 && !TREE_OVERFLOW (cast_expression))
8070 tree folded = fold_build1 (unary_operator,
8071 TREE_TYPE (cast_expression),
8072 cast_expression);
8073 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8075 expression = cp_expr (folded, loc);
8076 break;
8079 /* Fall through. */
8080 case UNARY_PLUS_EXPR:
8081 case TRUTH_NOT_EXPR:
8082 expression = finish_unary_op_expr (loc, unary_operator,
8083 cast_expression, complain);
8084 break;
8086 default:
8087 gcc_unreachable ();
8090 if (non_constant_p != NIC_NONE
8091 && cp_parser_non_integral_constant_expression (parser,
8092 non_constant_p))
8093 expression = error_mark_node;
8095 return expression;
8098 return cp_parser_postfix_expression (parser, address_p, cast_p,
8099 /*member_access_only_p=*/false,
8100 decltype_p,
8101 pidk);
8104 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8105 unary-operator, the corresponding tree code is returned. */
8107 static enum tree_code
8108 cp_parser_unary_operator (cp_token* token)
8110 switch (token->type)
8112 case CPP_MULT:
8113 return INDIRECT_REF;
8115 case CPP_AND:
8116 return ADDR_EXPR;
8118 case CPP_PLUS:
8119 return UNARY_PLUS_EXPR;
8121 case CPP_MINUS:
8122 return NEGATE_EXPR;
8124 case CPP_NOT:
8125 return TRUTH_NOT_EXPR;
8127 case CPP_COMPL:
8128 return BIT_NOT_EXPR;
8130 default:
8131 return ERROR_MARK;
8135 /* Parse a new-expression.
8137 new-expression:
8138 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8139 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8141 Returns a representation of the expression. */
8143 static tree
8144 cp_parser_new_expression (cp_parser* parser)
8146 bool global_scope_p;
8147 vec<tree, va_gc> *placement;
8148 tree type;
8149 vec<tree, va_gc> *initializer;
8150 tree nelts = NULL_TREE;
8151 tree ret;
8153 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8155 /* Look for the optional `::' operator. */
8156 global_scope_p
8157 = (cp_parser_global_scope_opt (parser,
8158 /*current_scope_valid_p=*/false)
8159 != NULL_TREE);
8160 /* Look for the `new' operator. */
8161 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8162 /* There's no easy way to tell a new-placement from the
8163 `( type-id )' construct. */
8164 cp_parser_parse_tentatively (parser);
8165 /* Look for a new-placement. */
8166 placement = cp_parser_new_placement (parser);
8167 /* If that didn't work out, there's no new-placement. */
8168 if (!cp_parser_parse_definitely (parser))
8170 if (placement != NULL)
8171 release_tree_vector (placement);
8172 placement = NULL;
8175 /* If the next token is a `(', then we have a parenthesized
8176 type-id. */
8177 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8179 cp_token *token;
8180 const char *saved_message = parser->type_definition_forbidden_message;
8182 /* Consume the `('. */
8183 cp_lexer_consume_token (parser->lexer);
8185 /* Parse the type-id. */
8186 parser->type_definition_forbidden_message
8187 = G_("types may not be defined in a new-expression");
8189 type_id_in_expr_sentinel s (parser);
8190 type = cp_parser_type_id (parser);
8192 parser->type_definition_forbidden_message = saved_message;
8194 /* Look for the closing `)'. */
8195 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8196 token = cp_lexer_peek_token (parser->lexer);
8197 /* There should not be a direct-new-declarator in this production,
8198 but GCC used to allowed this, so we check and emit a sensible error
8199 message for this case. */
8200 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8202 error_at (token->location,
8203 "array bound forbidden after parenthesized type-id");
8204 inform (token->location,
8205 "try removing the parentheses around the type-id");
8206 cp_parser_direct_new_declarator (parser);
8209 /* Otherwise, there must be a new-type-id. */
8210 else
8211 type = cp_parser_new_type_id (parser, &nelts);
8213 /* If the next token is a `(' or '{', then we have a new-initializer. */
8214 cp_token *token = cp_lexer_peek_token (parser->lexer);
8215 if (token->type == CPP_OPEN_PAREN
8216 || token->type == CPP_OPEN_BRACE)
8217 initializer = cp_parser_new_initializer (parser);
8218 else
8219 initializer = NULL;
8221 /* A new-expression may not appear in an integral constant
8222 expression. */
8223 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8224 ret = error_mark_node;
8225 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8226 of a new-type-id or type-id of a new-expression, the new-expression shall
8227 contain a new-initializer of the form ( assignment-expression )".
8228 Additionally, consistently with the spirit of DR 1467, we want to accept
8229 'new auto { 2 }' too. */
8230 else if (type_uses_auto (type)
8231 && (vec_safe_length (initializer) != 1
8232 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8233 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8235 error_at (token->location,
8236 "initialization of new-expression for type %<auto%> "
8237 "requires exactly one element");
8238 ret = error_mark_node;
8240 else
8242 /* Construct a location e.g.:
8243 ptr = new int[100]
8244 ^~~~~~~~~~~~
8245 with caret == start at the start of the "new" token, and the end
8246 at the end of the final token we consumed. */
8247 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8248 location_t end_loc = get_finish (end_tok->location);
8249 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8251 /* Create a representation of the new-expression. */
8252 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8253 tf_warning_or_error);
8254 protected_set_expr_location (ret, combined_loc);
8257 if (placement != NULL)
8258 release_tree_vector (placement);
8259 if (initializer != NULL)
8260 release_tree_vector (initializer);
8262 return ret;
8265 /* Parse a new-placement.
8267 new-placement:
8268 ( expression-list )
8270 Returns the same representation as for an expression-list. */
8272 static vec<tree, va_gc> *
8273 cp_parser_new_placement (cp_parser* parser)
8275 vec<tree, va_gc> *expression_list;
8277 /* Parse the expression-list. */
8278 expression_list = (cp_parser_parenthesized_expression_list
8279 (parser, non_attr, /*cast_p=*/false,
8280 /*allow_expansion_p=*/true,
8281 /*non_constant_p=*/NULL));
8283 if (expression_list && expression_list->is_empty ())
8284 error ("expected expression-list or type-id");
8286 return expression_list;
8289 /* Parse a new-type-id.
8291 new-type-id:
8292 type-specifier-seq new-declarator [opt]
8294 Returns the TYPE allocated. If the new-type-id indicates an array
8295 type, *NELTS is set to the number of elements in the last array
8296 bound; the TYPE will not include the last array bound. */
8298 static tree
8299 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8301 cp_decl_specifier_seq type_specifier_seq;
8302 cp_declarator *new_declarator;
8303 cp_declarator *declarator;
8304 cp_declarator *outer_declarator;
8305 const char *saved_message;
8307 /* The type-specifier sequence must not contain type definitions.
8308 (It cannot contain declarations of new types either, but if they
8309 are not definitions we will catch that because they are not
8310 complete.) */
8311 saved_message = parser->type_definition_forbidden_message;
8312 parser->type_definition_forbidden_message
8313 = G_("types may not be defined in a new-type-id");
8314 /* Parse the type-specifier-seq. */
8315 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8316 /*is_trailing_return=*/false,
8317 &type_specifier_seq);
8318 /* Restore the old message. */
8319 parser->type_definition_forbidden_message = saved_message;
8321 if (type_specifier_seq.type == error_mark_node)
8322 return error_mark_node;
8324 /* Parse the new-declarator. */
8325 new_declarator = cp_parser_new_declarator_opt (parser);
8327 /* Determine the number of elements in the last array dimension, if
8328 any. */
8329 *nelts = NULL_TREE;
8330 /* Skip down to the last array dimension. */
8331 declarator = new_declarator;
8332 outer_declarator = NULL;
8333 while (declarator && (declarator->kind == cdk_pointer
8334 || declarator->kind == cdk_ptrmem))
8336 outer_declarator = declarator;
8337 declarator = declarator->declarator;
8339 while (declarator
8340 && declarator->kind == cdk_array
8341 && declarator->declarator
8342 && declarator->declarator->kind == cdk_array)
8344 outer_declarator = declarator;
8345 declarator = declarator->declarator;
8348 if (declarator && declarator->kind == cdk_array)
8350 *nelts = declarator->u.array.bounds;
8351 if (*nelts == error_mark_node)
8352 *nelts = integer_one_node;
8354 if (outer_declarator)
8355 outer_declarator->declarator = declarator->declarator;
8356 else
8357 new_declarator = NULL;
8360 return groktypename (&type_specifier_seq, new_declarator, false);
8363 /* Parse an (optional) new-declarator.
8365 new-declarator:
8366 ptr-operator new-declarator [opt]
8367 direct-new-declarator
8369 Returns the declarator. */
8371 static cp_declarator *
8372 cp_parser_new_declarator_opt (cp_parser* parser)
8374 enum tree_code code;
8375 tree type, std_attributes = NULL_TREE;
8376 cp_cv_quals cv_quals;
8378 /* We don't know if there's a ptr-operator next, or not. */
8379 cp_parser_parse_tentatively (parser);
8380 /* Look for a ptr-operator. */
8381 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8382 /* If that worked, look for more new-declarators. */
8383 if (cp_parser_parse_definitely (parser))
8385 cp_declarator *declarator;
8387 /* Parse another optional declarator. */
8388 declarator = cp_parser_new_declarator_opt (parser);
8390 declarator = cp_parser_make_indirect_declarator
8391 (code, type, cv_quals, declarator, std_attributes);
8393 return declarator;
8396 /* If the next token is a `[', there is a direct-new-declarator. */
8397 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8398 return cp_parser_direct_new_declarator (parser);
8400 return NULL;
8403 /* Parse a direct-new-declarator.
8405 direct-new-declarator:
8406 [ expression ]
8407 direct-new-declarator [constant-expression]
8411 static cp_declarator *
8412 cp_parser_direct_new_declarator (cp_parser* parser)
8414 cp_declarator *declarator = NULL;
8416 while (true)
8418 tree expression;
8419 cp_token *token;
8421 /* Look for the opening `['. */
8422 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8424 token = cp_lexer_peek_token (parser->lexer);
8425 expression = cp_parser_expression (parser);
8426 /* The standard requires that the expression have integral
8427 type. DR 74 adds enumeration types. We believe that the
8428 real intent is that these expressions be handled like the
8429 expression in a `switch' condition, which also allows
8430 classes with a single conversion to integral or
8431 enumeration type. */
8432 if (!processing_template_decl)
8434 expression
8435 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8436 expression,
8437 /*complain=*/true);
8438 if (!expression)
8440 error_at (token->location,
8441 "expression in new-declarator must have integral "
8442 "or enumeration type");
8443 expression = error_mark_node;
8447 /* Look for the closing `]'. */
8448 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8450 /* Add this bound to the declarator. */
8451 declarator = make_array_declarator (declarator, expression);
8453 /* If the next token is not a `[', then there are no more
8454 bounds. */
8455 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8456 break;
8459 return declarator;
8462 /* Parse a new-initializer.
8464 new-initializer:
8465 ( expression-list [opt] )
8466 braced-init-list
8468 Returns a representation of the expression-list. */
8470 static vec<tree, va_gc> *
8471 cp_parser_new_initializer (cp_parser* parser)
8473 vec<tree, va_gc> *expression_list;
8475 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8477 tree t;
8478 bool expr_non_constant_p;
8479 cp_lexer_set_source_position (parser->lexer);
8480 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8481 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8482 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8483 expression_list = make_tree_vector_single (t);
8485 else
8486 expression_list = (cp_parser_parenthesized_expression_list
8487 (parser, non_attr, /*cast_p=*/false,
8488 /*allow_expansion_p=*/true,
8489 /*non_constant_p=*/NULL));
8491 return expression_list;
8494 /* Parse a delete-expression.
8496 delete-expression:
8497 :: [opt] delete cast-expression
8498 :: [opt] delete [ ] cast-expression
8500 Returns a representation of the expression. */
8502 static tree
8503 cp_parser_delete_expression (cp_parser* parser)
8505 bool global_scope_p;
8506 bool array_p;
8507 tree expression;
8509 /* Look for the optional `::' operator. */
8510 global_scope_p
8511 = (cp_parser_global_scope_opt (parser,
8512 /*current_scope_valid_p=*/false)
8513 != NULL_TREE);
8514 /* Look for the `delete' keyword. */
8515 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8516 /* See if the array syntax is in use. */
8517 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8519 /* Consume the `[' token. */
8520 cp_lexer_consume_token (parser->lexer);
8521 /* Look for the `]' token. */
8522 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8523 /* Remember that this is the `[]' construct. */
8524 array_p = true;
8526 else
8527 array_p = false;
8529 /* Parse the cast-expression. */
8530 expression = cp_parser_simple_cast_expression (parser);
8532 /* A delete-expression may not appear in an integral constant
8533 expression. */
8534 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8535 return error_mark_node;
8537 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8538 tf_warning_or_error);
8541 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8542 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8543 0 otherwise. */
8545 static int
8546 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8548 cp_token *token = cp_lexer_peek_token (parser->lexer);
8549 switch (token->type)
8551 case CPP_COMMA:
8552 case CPP_SEMICOLON:
8553 case CPP_QUERY:
8554 case CPP_COLON:
8555 case CPP_CLOSE_SQUARE:
8556 case CPP_CLOSE_PAREN:
8557 case CPP_CLOSE_BRACE:
8558 case CPP_OPEN_BRACE:
8559 case CPP_DOT:
8560 case CPP_DOT_STAR:
8561 case CPP_DEREF:
8562 case CPP_DEREF_STAR:
8563 case CPP_DIV:
8564 case CPP_MOD:
8565 case CPP_LSHIFT:
8566 case CPP_RSHIFT:
8567 case CPP_LESS:
8568 case CPP_GREATER:
8569 case CPP_LESS_EQ:
8570 case CPP_GREATER_EQ:
8571 case CPP_EQ_EQ:
8572 case CPP_NOT_EQ:
8573 case CPP_EQ:
8574 case CPP_MULT_EQ:
8575 case CPP_DIV_EQ:
8576 case CPP_MOD_EQ:
8577 case CPP_PLUS_EQ:
8578 case CPP_MINUS_EQ:
8579 case CPP_RSHIFT_EQ:
8580 case CPP_LSHIFT_EQ:
8581 case CPP_AND_EQ:
8582 case CPP_XOR_EQ:
8583 case CPP_OR_EQ:
8584 case CPP_XOR:
8585 case CPP_OR:
8586 case CPP_OR_OR:
8587 case CPP_EOF:
8588 case CPP_ELLIPSIS:
8589 return 0;
8591 case CPP_OPEN_PAREN:
8592 /* In ((type ()) () the last () isn't a valid cast-expression,
8593 so the whole must be parsed as postfix-expression. */
8594 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8595 != CPP_CLOSE_PAREN;
8597 case CPP_OPEN_SQUARE:
8598 /* '[' may start a primary-expression in obj-c++ and in C++11,
8599 as a lambda-expression, eg, '(void)[]{}'. */
8600 if (cxx_dialect >= cxx11)
8601 return -1;
8602 return c_dialect_objc ();
8604 case CPP_PLUS_PLUS:
8605 case CPP_MINUS_MINUS:
8606 /* '++' and '--' may or may not start a cast-expression:
8608 struct T { void operator++(int); };
8609 void f() { (T())++; }
8613 int a;
8614 (int)++a; */
8615 return -1;
8617 default:
8618 return 1;
8622 /* Parse a cast-expression.
8624 cast-expression:
8625 unary-expression
8626 ( type-id ) cast-expression
8628 ADDRESS_P is true iff the unary-expression is appearing as the
8629 operand of the `&' operator. CAST_P is true if this expression is
8630 the target of a cast.
8632 Returns a representation of the expression. */
8634 static cp_expr
8635 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8636 bool decltype_p, cp_id_kind * pidk)
8638 /* If it's a `(', then we might be looking at a cast. */
8639 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8641 tree type = NULL_TREE;
8642 cp_expr expr (NULL_TREE);
8643 int cast_expression = 0;
8644 const char *saved_message;
8646 /* There's no way to know yet whether or not this is a cast.
8647 For example, `(int (3))' is a unary-expression, while `(int)
8648 3' is a cast. So, we resort to parsing tentatively. */
8649 cp_parser_parse_tentatively (parser);
8650 /* Types may not be defined in a cast. */
8651 saved_message = parser->type_definition_forbidden_message;
8652 parser->type_definition_forbidden_message
8653 = G_("types may not be defined in casts");
8654 /* Consume the `('. */
8655 cp_token *open_paren = cp_lexer_consume_token (parser->lexer);
8656 location_t open_paren_loc = open_paren->location;
8658 /* A very tricky bit is that `(struct S) { 3 }' is a
8659 compound-literal (which we permit in C++ as an extension).
8660 But, that construct is not a cast-expression -- it is a
8661 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8662 is legal; if the compound-literal were a cast-expression,
8663 you'd need an extra set of parentheses.) But, if we parse
8664 the type-id, and it happens to be a class-specifier, then we
8665 will commit to the parse at that point, because we cannot
8666 undo the action that is done when creating a new class. So,
8667 then we cannot back up and do a postfix-expression.
8669 Another tricky case is the following (c++/29234):
8671 struct S { void operator () (); };
8673 void foo ()
8675 ( S()() );
8678 As a type-id we parse the parenthesized S()() as a function
8679 returning a function, groktypename complains and we cannot
8680 back up in this case either.
8682 Therefore, we scan ahead to the closing `)', and check to see
8683 if the tokens after the `)' can start a cast-expression. Otherwise
8684 we are dealing with an unary-expression, a postfix-expression
8685 or something else.
8687 Yet another tricky case, in C++11, is the following (c++/54891):
8689 (void)[]{};
8691 The issue is that usually, besides the case of lambda-expressions,
8692 the parenthesized type-id cannot be followed by '[', and, eg, we
8693 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8694 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8695 we don't commit, we try a cast-expression, then an unary-expression.
8697 Save tokens so that we can put them back. */
8698 cp_lexer_save_tokens (parser->lexer);
8700 /* We may be looking at a cast-expression. */
8701 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8702 /*consume_paren=*/true))
8703 cast_expression
8704 = cp_parser_tokens_start_cast_expression (parser);
8706 /* Roll back the tokens we skipped. */
8707 cp_lexer_rollback_tokens (parser->lexer);
8708 /* If we aren't looking at a cast-expression, simulate an error so
8709 that the call to cp_parser_error_occurred below returns true. */
8710 if (!cast_expression)
8711 cp_parser_simulate_error (parser);
8712 else
8714 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8715 parser->in_type_id_in_expr_p = true;
8716 /* Look for the type-id. */
8717 type = cp_parser_type_id (parser);
8718 /* Look for the closing `)'. */
8719 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8720 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8723 /* Restore the saved message. */
8724 parser->type_definition_forbidden_message = saved_message;
8726 /* At this point this can only be either a cast or a
8727 parenthesized ctor such as `(T ())' that looks like a cast to
8728 function returning T. */
8729 if (!cp_parser_error_occurred (parser))
8731 /* Only commit if the cast-expression doesn't start with
8732 '++', '--', or '[' in C++11. */
8733 if (cast_expression > 0)
8734 cp_parser_commit_to_topmost_tentative_parse (parser);
8736 expr = cp_parser_cast_expression (parser,
8737 /*address_p=*/false,
8738 /*cast_p=*/true,
8739 /*decltype_p=*/false,
8740 pidk);
8742 if (cp_parser_parse_definitely (parser))
8744 /* Warn about old-style casts, if so requested. */
8745 if (warn_old_style_cast
8746 && !in_system_header_at (input_location)
8747 && !VOID_TYPE_P (type)
8748 && current_lang_name != lang_name_c)
8749 warning (OPT_Wold_style_cast, "use of old-style cast");
8751 /* Only type conversions to integral or enumeration types
8752 can be used in constant-expressions. */
8753 if (!cast_valid_in_integral_constant_expression_p (type)
8754 && cp_parser_non_integral_constant_expression (parser,
8755 NIC_CAST))
8756 return error_mark_node;
8758 /* Perform the cast. */
8759 /* Make a location:
8760 (TYPE) EXPR
8761 ^~~~~~~~~~~
8762 with start==caret at the open paren, extending to the
8763 end of "expr". */
8764 location_t cast_loc = make_location (open_paren_loc,
8765 open_paren_loc,
8766 expr.get_finish ());
8767 expr = build_c_cast (cast_loc, type, expr);
8768 return expr;
8771 else
8772 cp_parser_abort_tentative_parse (parser);
8775 /* If we get here, then it's not a cast, so it must be a
8776 unary-expression. */
8777 return cp_parser_unary_expression (parser, pidk, address_p,
8778 cast_p, decltype_p);
8781 /* Parse a binary expression of the general form:
8783 pm-expression:
8784 cast-expression
8785 pm-expression .* cast-expression
8786 pm-expression ->* cast-expression
8788 multiplicative-expression:
8789 pm-expression
8790 multiplicative-expression * pm-expression
8791 multiplicative-expression / pm-expression
8792 multiplicative-expression % pm-expression
8794 additive-expression:
8795 multiplicative-expression
8796 additive-expression + multiplicative-expression
8797 additive-expression - multiplicative-expression
8799 shift-expression:
8800 additive-expression
8801 shift-expression << additive-expression
8802 shift-expression >> additive-expression
8804 relational-expression:
8805 shift-expression
8806 relational-expression < shift-expression
8807 relational-expression > shift-expression
8808 relational-expression <= shift-expression
8809 relational-expression >= shift-expression
8811 GNU Extension:
8813 relational-expression:
8814 relational-expression <? shift-expression
8815 relational-expression >? shift-expression
8817 equality-expression:
8818 relational-expression
8819 equality-expression == relational-expression
8820 equality-expression != relational-expression
8822 and-expression:
8823 equality-expression
8824 and-expression & equality-expression
8826 exclusive-or-expression:
8827 and-expression
8828 exclusive-or-expression ^ and-expression
8830 inclusive-or-expression:
8831 exclusive-or-expression
8832 inclusive-or-expression | exclusive-or-expression
8834 logical-and-expression:
8835 inclusive-or-expression
8836 logical-and-expression && inclusive-or-expression
8838 logical-or-expression:
8839 logical-and-expression
8840 logical-or-expression || logical-and-expression
8842 All these are implemented with a single function like:
8844 binary-expression:
8845 simple-cast-expression
8846 binary-expression <token> binary-expression
8848 CAST_P is true if this expression is the target of a cast.
8850 The binops_by_token map is used to get the tree codes for each <token> type.
8851 binary-expressions are associated according to a precedence table. */
8853 #define TOKEN_PRECEDENCE(token) \
8854 (((token->type == CPP_GREATER \
8855 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8856 && !parser->greater_than_is_operator_p) \
8857 ? PREC_NOT_OPERATOR \
8858 : binops_by_token[token->type].prec)
8860 static cp_expr
8861 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8862 bool no_toplevel_fold_p,
8863 bool decltype_p,
8864 enum cp_parser_prec prec,
8865 cp_id_kind * pidk)
8867 cp_parser_expression_stack stack;
8868 cp_parser_expression_stack_entry *sp = &stack[0];
8869 cp_parser_expression_stack_entry current;
8870 cp_expr rhs;
8871 cp_token *token;
8872 enum tree_code rhs_type;
8873 enum cp_parser_prec new_prec, lookahead_prec;
8874 tree overload;
8876 /* Parse the first expression. */
8877 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8878 ? TRUTH_NOT_EXPR : ERROR_MARK);
8879 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8880 cast_p, decltype_p, pidk);
8881 current.prec = prec;
8883 if (cp_parser_error_occurred (parser))
8884 return error_mark_node;
8886 for (;;)
8888 /* Get an operator token. */
8889 token = cp_lexer_peek_token (parser->lexer);
8891 if (warn_cxx11_compat
8892 && token->type == CPP_RSHIFT
8893 && !parser->greater_than_is_operator_p)
8895 if (warning_at (token->location, OPT_Wc__11_compat,
8896 "%<>>%> operator is treated"
8897 " as two right angle brackets in C++11"))
8898 inform (token->location,
8899 "suggest parentheses around %<>>%> expression");
8902 new_prec = TOKEN_PRECEDENCE (token);
8903 if (new_prec != PREC_NOT_OPERATOR
8904 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8905 /* This is a fold-expression; handle it later. */
8906 new_prec = PREC_NOT_OPERATOR;
8908 /* Popping an entry off the stack means we completed a subexpression:
8909 - either we found a token which is not an operator (`>' where it is not
8910 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8911 will happen repeatedly;
8912 - or, we found an operator which has lower priority. This is the case
8913 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8914 parsing `3 * 4'. */
8915 if (new_prec <= current.prec)
8917 if (sp == stack)
8918 break;
8919 else
8920 goto pop;
8923 get_rhs:
8924 current.tree_type = binops_by_token[token->type].tree_type;
8925 current.loc = token->location;
8927 /* We used the operator token. */
8928 cp_lexer_consume_token (parser->lexer);
8930 /* For "false && x" or "true || x", x will never be executed;
8931 disable warnings while evaluating it. */
8932 if (current.tree_type == TRUTH_ANDIF_EXPR)
8933 c_inhibit_evaluation_warnings +=
8934 cp_fully_fold (current.lhs) == truthvalue_false_node;
8935 else if (current.tree_type == TRUTH_ORIF_EXPR)
8936 c_inhibit_evaluation_warnings +=
8937 cp_fully_fold (current.lhs) == truthvalue_true_node;
8939 /* Extract another operand. It may be the RHS of this expression
8940 or the LHS of a new, higher priority expression. */
8941 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8942 ? TRUTH_NOT_EXPR : ERROR_MARK);
8943 rhs = cp_parser_simple_cast_expression (parser);
8945 /* Get another operator token. Look up its precedence to avoid
8946 building a useless (immediately popped) stack entry for common
8947 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8948 token = cp_lexer_peek_token (parser->lexer);
8949 lookahead_prec = TOKEN_PRECEDENCE (token);
8950 if (lookahead_prec != PREC_NOT_OPERATOR
8951 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8952 lookahead_prec = PREC_NOT_OPERATOR;
8953 if (lookahead_prec > new_prec)
8955 /* ... and prepare to parse the RHS of the new, higher priority
8956 expression. Since precedence levels on the stack are
8957 monotonically increasing, we do not have to care about
8958 stack overflows. */
8959 *sp = current;
8960 ++sp;
8961 current.lhs = rhs;
8962 current.lhs_type = rhs_type;
8963 current.prec = new_prec;
8964 new_prec = lookahead_prec;
8965 goto get_rhs;
8967 pop:
8968 lookahead_prec = new_prec;
8969 /* If the stack is not empty, we have parsed into LHS the right side
8970 (`4' in the example above) of an expression we had suspended.
8971 We can use the information on the stack to recover the LHS (`3')
8972 from the stack together with the tree code (`MULT_EXPR'), and
8973 the precedence of the higher level subexpression
8974 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8975 which will be used to actually build the additive expression. */
8976 rhs = current.lhs;
8977 rhs_type = current.lhs_type;
8978 --sp;
8979 current = *sp;
8982 /* Undo the disabling of warnings done above. */
8983 if (current.tree_type == TRUTH_ANDIF_EXPR)
8984 c_inhibit_evaluation_warnings -=
8985 cp_fully_fold (current.lhs) == truthvalue_false_node;
8986 else if (current.tree_type == TRUTH_ORIF_EXPR)
8987 c_inhibit_evaluation_warnings -=
8988 cp_fully_fold (current.lhs) == truthvalue_true_node;
8990 if (warn_logical_not_paren
8991 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
8992 && current.lhs_type == TRUTH_NOT_EXPR
8993 /* Avoid warning for !!x == y. */
8994 && (TREE_CODE (current.lhs) != NE_EXPR
8995 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
8996 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
8997 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
8998 /* Avoid warning for !b == y where b is boolean. */
8999 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9000 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9001 != BOOLEAN_TYPE))))
9002 /* Avoid warning for !!b == y where b is boolean. */
9003 && (!DECL_P (current.lhs)
9004 || TREE_TYPE (current.lhs) == NULL_TREE
9005 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9006 warn_logical_not_parentheses (current.loc, current.tree_type,
9007 current.lhs, maybe_constant_value (rhs));
9009 overload = NULL;
9011 location_t combined_loc = make_location (current.loc,
9012 current.lhs.get_start (),
9013 rhs.get_finish ());
9015 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9016 ERROR_MARK for everything that is not a binary expression.
9017 This makes warn_about_parentheses miss some warnings that
9018 involve unary operators. For unary expressions we should
9019 pass the correct tree_code unless the unary expression was
9020 surrounded by parentheses.
9022 if (no_toplevel_fold_p
9023 && lookahead_prec <= current.prec
9024 && sp == stack)
9025 current.lhs = build2_loc (combined_loc,
9026 current.tree_type,
9027 TREE_CODE_CLASS (current.tree_type)
9028 == tcc_comparison
9029 ? boolean_type_node : TREE_TYPE (current.lhs),
9030 current.lhs, rhs);
9031 else
9033 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9034 current.lhs, current.lhs_type,
9035 rhs, rhs_type, &overload,
9036 complain_flags (decltype_p));
9037 /* TODO: build_x_binary_op doesn't always honor the location. */
9038 current.lhs.set_location (combined_loc);
9040 current.lhs_type = current.tree_type;
9042 /* If the binary operator required the use of an overloaded operator,
9043 then this expression cannot be an integral constant-expression.
9044 An overloaded operator can be used even if both operands are
9045 otherwise permissible in an integral constant-expression if at
9046 least one of the operands is of enumeration type. */
9048 if (overload
9049 && cp_parser_non_integral_constant_expression (parser,
9050 NIC_OVERLOADED))
9051 return error_mark_node;
9054 return current.lhs;
9057 static cp_expr
9058 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9059 bool no_toplevel_fold_p,
9060 enum cp_parser_prec prec,
9061 cp_id_kind * pidk)
9063 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9064 /*decltype*/false, prec, pidk);
9067 /* Parse the `? expression : assignment-expression' part of a
9068 conditional-expression. The LOGICAL_OR_EXPR is the
9069 logical-or-expression that started the conditional-expression.
9070 Returns a representation of the entire conditional-expression.
9072 This routine is used by cp_parser_assignment_expression.
9074 ? expression : assignment-expression
9076 GNU Extensions:
9078 ? : assignment-expression */
9080 static tree
9081 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9083 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9084 cp_expr assignment_expr;
9085 struct cp_token *token;
9086 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9088 /* Consume the `?' token. */
9089 cp_lexer_consume_token (parser->lexer);
9090 token = cp_lexer_peek_token (parser->lexer);
9091 if (cp_parser_allow_gnu_extensions_p (parser)
9092 && token->type == CPP_COLON)
9094 pedwarn (token->location, OPT_Wpedantic,
9095 "ISO C++ does not allow ?: with omitted middle operand");
9096 /* Implicit true clause. */
9097 expr = NULL_TREE;
9098 c_inhibit_evaluation_warnings +=
9099 folded_logical_or_expr == truthvalue_true_node;
9100 warn_for_omitted_condop (token->location, logical_or_expr);
9102 else
9104 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9105 parser->colon_corrects_to_scope_p = false;
9106 /* Parse the expression. */
9107 c_inhibit_evaluation_warnings +=
9108 folded_logical_or_expr == truthvalue_false_node;
9109 expr = cp_parser_expression (parser);
9110 c_inhibit_evaluation_warnings +=
9111 ((folded_logical_or_expr == truthvalue_true_node)
9112 - (folded_logical_or_expr == truthvalue_false_node));
9113 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9116 /* The next token should be a `:'. */
9117 cp_parser_require (parser, CPP_COLON, RT_COLON);
9118 /* Parse the assignment-expression. */
9119 assignment_expr = cp_parser_assignment_expression (parser);
9120 c_inhibit_evaluation_warnings -=
9121 folded_logical_or_expr == truthvalue_true_node;
9123 /* Make a location:
9124 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9125 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9126 with the caret at the "?", ranging from the start of
9127 the logical_or_expr to the end of the assignment_expr. */
9128 loc = make_location (loc,
9129 logical_or_expr.get_start (),
9130 assignment_expr.get_finish ());
9132 /* Build the conditional-expression. */
9133 return build_x_conditional_expr (loc, logical_or_expr,
9134 expr,
9135 assignment_expr,
9136 tf_warning_or_error);
9139 /* Parse an assignment-expression.
9141 assignment-expression:
9142 conditional-expression
9143 logical-or-expression assignment-operator assignment_expression
9144 throw-expression
9146 CAST_P is true if this expression is the target of a cast.
9147 DECLTYPE_P is true if this expression is the operand of decltype.
9149 Returns a representation for the expression. */
9151 static cp_expr
9152 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9153 bool cast_p, bool decltype_p)
9155 cp_expr expr;
9157 /* If the next token is the `throw' keyword, then we're looking at
9158 a throw-expression. */
9159 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9160 expr = cp_parser_throw_expression (parser);
9161 /* Otherwise, it must be that we are looking at a
9162 logical-or-expression. */
9163 else
9165 /* Parse the binary expressions (logical-or-expression). */
9166 expr = cp_parser_binary_expression (parser, cast_p, false,
9167 decltype_p,
9168 PREC_NOT_OPERATOR, pidk);
9169 /* If the next token is a `?' then we're actually looking at a
9170 conditional-expression. */
9171 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9172 return cp_parser_question_colon_clause (parser, expr);
9173 else
9175 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9177 /* If it's an assignment-operator, we're using the second
9178 production. */
9179 enum tree_code assignment_operator
9180 = cp_parser_assignment_operator_opt (parser);
9181 if (assignment_operator != ERROR_MARK)
9183 bool non_constant_p;
9185 /* Parse the right-hand side of the assignment. */
9186 cp_expr rhs = cp_parser_initializer_clause (parser,
9187 &non_constant_p);
9189 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9190 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9192 /* An assignment may not appear in a
9193 constant-expression. */
9194 if (cp_parser_non_integral_constant_expression (parser,
9195 NIC_ASSIGNMENT))
9196 return error_mark_node;
9197 /* Build the assignment expression. Its default
9198 location:
9199 LHS = RHS
9200 ~~~~^~~~~
9201 is the location of the '=' token as the
9202 caret, ranging from the start of the lhs to the
9203 end of the rhs. */
9204 loc = make_location (loc,
9205 expr.get_start (),
9206 rhs.get_finish ());
9207 expr = build_x_modify_expr (loc, expr,
9208 assignment_operator,
9209 rhs,
9210 complain_flags (decltype_p));
9211 /* TODO: build_x_modify_expr doesn't honor the location,
9212 so we must set it here. */
9213 expr.set_location (loc);
9218 return expr;
9221 /* Parse an (optional) assignment-operator.
9223 assignment-operator: one of
9224 = *= /= %= += -= >>= <<= &= ^= |=
9226 GNU Extension:
9228 assignment-operator: one of
9229 <?= >?=
9231 If the next token is an assignment operator, the corresponding tree
9232 code is returned, and the token is consumed. For example, for
9233 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9234 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9235 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9236 operator, ERROR_MARK is returned. */
9238 static enum tree_code
9239 cp_parser_assignment_operator_opt (cp_parser* parser)
9241 enum tree_code op;
9242 cp_token *token;
9244 /* Peek at the next token. */
9245 token = cp_lexer_peek_token (parser->lexer);
9247 switch (token->type)
9249 case CPP_EQ:
9250 op = NOP_EXPR;
9251 break;
9253 case CPP_MULT_EQ:
9254 op = MULT_EXPR;
9255 break;
9257 case CPP_DIV_EQ:
9258 op = TRUNC_DIV_EXPR;
9259 break;
9261 case CPP_MOD_EQ:
9262 op = TRUNC_MOD_EXPR;
9263 break;
9265 case CPP_PLUS_EQ:
9266 op = PLUS_EXPR;
9267 break;
9269 case CPP_MINUS_EQ:
9270 op = MINUS_EXPR;
9271 break;
9273 case CPP_RSHIFT_EQ:
9274 op = RSHIFT_EXPR;
9275 break;
9277 case CPP_LSHIFT_EQ:
9278 op = LSHIFT_EXPR;
9279 break;
9281 case CPP_AND_EQ:
9282 op = BIT_AND_EXPR;
9283 break;
9285 case CPP_XOR_EQ:
9286 op = BIT_XOR_EXPR;
9287 break;
9289 case CPP_OR_EQ:
9290 op = BIT_IOR_EXPR;
9291 break;
9293 default:
9294 /* Nothing else is an assignment operator. */
9295 op = ERROR_MARK;
9298 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9299 if (op != ERROR_MARK
9300 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9301 op = ERROR_MARK;
9303 /* If it was an assignment operator, consume it. */
9304 if (op != ERROR_MARK)
9305 cp_lexer_consume_token (parser->lexer);
9307 return op;
9310 /* Parse an expression.
9312 expression:
9313 assignment-expression
9314 expression , assignment-expression
9316 CAST_P is true if this expression is the target of a cast.
9317 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9318 except possibly parenthesized or on the RHS of a comma (N3276).
9320 Returns a representation of the expression. */
9322 static cp_expr
9323 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9324 bool cast_p, bool decltype_p)
9326 cp_expr expression = NULL_TREE;
9327 location_t loc = UNKNOWN_LOCATION;
9329 while (true)
9331 cp_expr assignment_expression;
9333 /* Parse the next assignment-expression. */
9334 assignment_expression
9335 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9337 /* We don't create a temporary for a call that is the immediate operand
9338 of decltype or on the RHS of a comma. But when we see a comma, we
9339 need to create a temporary for a call on the LHS. */
9340 if (decltype_p && !processing_template_decl
9341 && TREE_CODE (assignment_expression) == CALL_EXPR
9342 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9343 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9344 assignment_expression
9345 = build_cplus_new (TREE_TYPE (assignment_expression),
9346 assignment_expression, tf_warning_or_error);
9348 /* If this is the first assignment-expression, we can just
9349 save it away. */
9350 if (!expression)
9351 expression = assignment_expression;
9352 else
9354 /* Create a location with caret at the comma, ranging
9355 from the start of the LHS to the end of the RHS. */
9356 loc = make_location (loc,
9357 expression.get_start (),
9358 assignment_expression.get_finish ());
9359 expression = build_x_compound_expr (loc, expression,
9360 assignment_expression,
9361 complain_flags (decltype_p));
9362 expression.set_location (loc);
9364 /* If the next token is not a comma, or we're in a fold-expression, then
9365 we are done with the expression. */
9366 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9367 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9368 break;
9369 /* Consume the `,'. */
9370 loc = cp_lexer_peek_token (parser->lexer)->location;
9371 cp_lexer_consume_token (parser->lexer);
9372 /* A comma operator cannot appear in a constant-expression. */
9373 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9374 expression = error_mark_node;
9377 return expression;
9380 /* Parse a constant-expression.
9382 constant-expression:
9383 conditional-expression
9385 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9386 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9387 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9388 is false, NON_CONSTANT_P should be NULL. */
9390 static cp_expr
9391 cp_parser_constant_expression (cp_parser* parser,
9392 bool allow_non_constant_p,
9393 bool *non_constant_p)
9395 bool saved_integral_constant_expression_p;
9396 bool saved_allow_non_integral_constant_expression_p;
9397 bool saved_non_integral_constant_expression_p;
9398 cp_expr expression;
9400 /* It might seem that we could simply parse the
9401 conditional-expression, and then check to see if it were
9402 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9403 one that the compiler can figure out is constant, possibly after
9404 doing some simplifications or optimizations. The standard has a
9405 precise definition of constant-expression, and we must honor
9406 that, even though it is somewhat more restrictive.
9408 For example:
9410 int i[(2, 3)];
9412 is not a legal declaration, because `(2, 3)' is not a
9413 constant-expression. The `,' operator is forbidden in a
9414 constant-expression. However, GCC's constant-folding machinery
9415 will fold this operation to an INTEGER_CST for `3'. */
9417 /* Save the old settings. */
9418 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9419 saved_allow_non_integral_constant_expression_p
9420 = parser->allow_non_integral_constant_expression_p;
9421 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9422 /* We are now parsing a constant-expression. */
9423 parser->integral_constant_expression_p = true;
9424 parser->allow_non_integral_constant_expression_p
9425 = (allow_non_constant_p || cxx_dialect >= cxx11);
9426 parser->non_integral_constant_expression_p = false;
9427 /* Although the grammar says "conditional-expression", we parse an
9428 "assignment-expression", which also permits "throw-expression"
9429 and the use of assignment operators. In the case that
9430 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9431 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9432 actually essential that we look for an assignment-expression.
9433 For example, cp_parser_initializer_clauses uses this function to
9434 determine whether a particular assignment-expression is in fact
9435 constant. */
9436 expression = cp_parser_assignment_expression (parser);
9437 /* Restore the old settings. */
9438 parser->integral_constant_expression_p
9439 = saved_integral_constant_expression_p;
9440 parser->allow_non_integral_constant_expression_p
9441 = saved_allow_non_integral_constant_expression_p;
9442 if (cxx_dialect >= cxx11)
9444 /* Require an rvalue constant expression here; that's what our
9445 callers expect. Reference constant expressions are handled
9446 separately in e.g. cp_parser_template_argument. */
9447 bool is_const = potential_rvalue_constant_expression (expression);
9448 parser->non_integral_constant_expression_p = !is_const;
9449 if (!is_const && !allow_non_constant_p)
9450 require_potential_rvalue_constant_expression (expression);
9452 if (allow_non_constant_p)
9453 *non_constant_p = parser->non_integral_constant_expression_p;
9454 parser->non_integral_constant_expression_p
9455 = saved_non_integral_constant_expression_p;
9457 return expression;
9460 /* Parse __builtin_offsetof.
9462 offsetof-expression:
9463 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9465 offsetof-member-designator:
9466 id-expression
9467 | offsetof-member-designator "." id-expression
9468 | offsetof-member-designator "[" expression "]"
9469 | offsetof-member-designator "->" id-expression */
9471 static cp_expr
9472 cp_parser_builtin_offsetof (cp_parser *parser)
9474 int save_ice_p, save_non_ice_p;
9475 tree type;
9476 cp_expr expr;
9477 cp_id_kind dummy;
9478 cp_token *token;
9479 location_t finish_loc;
9481 /* We're about to accept non-integral-constant things, but will
9482 definitely yield an integral constant expression. Save and
9483 restore these values around our local parsing. */
9484 save_ice_p = parser->integral_constant_expression_p;
9485 save_non_ice_p = parser->non_integral_constant_expression_p;
9487 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9489 /* Consume the "__builtin_offsetof" token. */
9490 cp_lexer_consume_token (parser->lexer);
9491 /* Consume the opening `('. */
9492 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9493 /* Parse the type-id. */
9494 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9495 type = cp_parser_type_id (parser);
9496 /* Look for the `,'. */
9497 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9498 token = cp_lexer_peek_token (parser->lexer);
9500 /* Build the (type *)null that begins the traditional offsetof macro. */
9501 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
9502 tf_warning_or_error);
9504 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9505 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
9506 true, &dummy, token->location);
9507 while (true)
9509 token = cp_lexer_peek_token (parser->lexer);
9510 switch (token->type)
9512 case CPP_OPEN_SQUARE:
9513 /* offsetof-member-designator "[" expression "]" */
9514 expr = cp_parser_postfix_open_square_expression (parser, expr,
9515 true, false);
9516 break;
9518 case CPP_DEREF:
9519 /* offsetof-member-designator "->" identifier */
9520 expr = grok_array_decl (token->location, expr,
9521 integer_zero_node, false);
9522 /* FALLTHRU */
9524 case CPP_DOT:
9525 /* offsetof-member-designator "." identifier */
9526 cp_lexer_consume_token (parser->lexer);
9527 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9528 expr, true, &dummy,
9529 token->location);
9530 break;
9532 case CPP_CLOSE_PAREN:
9533 /* Consume the ")" token. */
9534 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9535 cp_lexer_consume_token (parser->lexer);
9536 goto success;
9538 default:
9539 /* Error. We know the following require will fail, but
9540 that gives the proper error message. */
9541 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9542 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9543 expr = error_mark_node;
9544 goto failure;
9548 success:
9549 /* Make a location of the form:
9550 __builtin_offsetof (struct s, f)
9551 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9552 with caret at the type-id, ranging from the start of the
9553 "_builtin_offsetof" token to the close paren. */
9554 loc = make_location (loc, start_loc, finish_loc);
9555 /* The result will be an INTEGER_CST, so we need to explicitly
9556 preserve the location. */
9557 expr = cp_expr (finish_offsetof (expr, loc), loc);
9559 failure:
9560 parser->integral_constant_expression_p = save_ice_p;
9561 parser->non_integral_constant_expression_p = save_non_ice_p;
9563 return expr;
9566 /* Parse a trait expression.
9568 Returns a representation of the expression, the underlying type
9569 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9571 static tree
9572 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9574 cp_trait_kind kind;
9575 tree type1, type2 = NULL_TREE;
9576 bool binary = false;
9577 bool variadic = false;
9579 switch (keyword)
9581 case RID_HAS_NOTHROW_ASSIGN:
9582 kind = CPTK_HAS_NOTHROW_ASSIGN;
9583 break;
9584 case RID_HAS_NOTHROW_CONSTRUCTOR:
9585 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9586 break;
9587 case RID_HAS_NOTHROW_COPY:
9588 kind = CPTK_HAS_NOTHROW_COPY;
9589 break;
9590 case RID_HAS_TRIVIAL_ASSIGN:
9591 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9592 break;
9593 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9594 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9595 break;
9596 case RID_HAS_TRIVIAL_COPY:
9597 kind = CPTK_HAS_TRIVIAL_COPY;
9598 break;
9599 case RID_HAS_TRIVIAL_DESTRUCTOR:
9600 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9601 break;
9602 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9603 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9604 break;
9605 case RID_HAS_VIRTUAL_DESTRUCTOR:
9606 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9607 break;
9608 case RID_IS_ABSTRACT:
9609 kind = CPTK_IS_ABSTRACT;
9610 break;
9611 case RID_IS_BASE_OF:
9612 kind = CPTK_IS_BASE_OF;
9613 binary = true;
9614 break;
9615 case RID_IS_CLASS:
9616 kind = CPTK_IS_CLASS;
9617 break;
9618 case RID_IS_EMPTY:
9619 kind = CPTK_IS_EMPTY;
9620 break;
9621 case RID_IS_ENUM:
9622 kind = CPTK_IS_ENUM;
9623 break;
9624 case RID_IS_FINAL:
9625 kind = CPTK_IS_FINAL;
9626 break;
9627 case RID_IS_LITERAL_TYPE:
9628 kind = CPTK_IS_LITERAL_TYPE;
9629 break;
9630 case RID_IS_POD:
9631 kind = CPTK_IS_POD;
9632 break;
9633 case RID_IS_POLYMORPHIC:
9634 kind = CPTK_IS_POLYMORPHIC;
9635 break;
9636 case RID_IS_SAME_AS:
9637 kind = CPTK_IS_SAME_AS;
9638 binary = true;
9639 break;
9640 case RID_IS_STD_LAYOUT:
9641 kind = CPTK_IS_STD_LAYOUT;
9642 break;
9643 case RID_IS_TRIVIAL:
9644 kind = CPTK_IS_TRIVIAL;
9645 break;
9646 case RID_IS_TRIVIALLY_ASSIGNABLE:
9647 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9648 binary = true;
9649 break;
9650 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9651 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9652 variadic = true;
9653 break;
9654 case RID_IS_TRIVIALLY_COPYABLE:
9655 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9656 break;
9657 case RID_IS_UNION:
9658 kind = CPTK_IS_UNION;
9659 break;
9660 case RID_UNDERLYING_TYPE:
9661 kind = CPTK_UNDERLYING_TYPE;
9662 break;
9663 case RID_BASES:
9664 kind = CPTK_BASES;
9665 break;
9666 case RID_DIRECT_BASES:
9667 kind = CPTK_DIRECT_BASES;
9668 break;
9669 default:
9670 gcc_unreachable ();
9673 /* Consume the token. */
9674 cp_lexer_consume_token (parser->lexer);
9676 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9679 type_id_in_expr_sentinel s (parser);
9680 type1 = cp_parser_type_id (parser);
9683 if (type1 == error_mark_node)
9684 return error_mark_node;
9686 if (binary)
9688 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9691 type_id_in_expr_sentinel s (parser);
9692 type2 = cp_parser_type_id (parser);
9695 if (type2 == error_mark_node)
9696 return error_mark_node;
9698 else if (variadic)
9700 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9702 cp_lexer_consume_token (parser->lexer);
9703 tree elt = cp_parser_type_id (parser);
9704 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9706 cp_lexer_consume_token (parser->lexer);
9707 elt = make_pack_expansion (elt);
9709 if (elt == error_mark_node)
9710 return error_mark_node;
9711 type2 = tree_cons (NULL_TREE, elt, type2);
9715 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9717 /* Complete the trait expression, which may mean either processing
9718 the trait expr now or saving it for template instantiation. */
9719 switch (kind)
9721 case CPTK_UNDERLYING_TYPE:
9722 return finish_underlying_type (type1);
9723 case CPTK_BASES:
9724 return finish_bases (type1, false);
9725 case CPTK_DIRECT_BASES:
9726 return finish_bases (type1, true);
9727 default:
9728 return finish_trait_expr (kind, type1, type2);
9732 /* Lambdas that appear in variable initializer or default argument scope
9733 get that in their mangling, so we need to record it. We might as well
9734 use the count for function and namespace scopes as well. */
9735 static GTY(()) tree lambda_scope;
9736 static GTY(()) int lambda_count;
9737 struct GTY(()) tree_int
9739 tree t;
9740 int i;
9742 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
9744 static void
9745 start_lambda_scope (tree decl)
9747 tree_int ti;
9748 gcc_assert (decl);
9749 /* Once we're inside a function, we ignore other scopes and just push
9750 the function again so that popping works properly. */
9751 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
9752 decl = current_function_decl;
9753 ti.t = lambda_scope;
9754 ti.i = lambda_count;
9755 vec_safe_push (lambda_scope_stack, ti);
9756 if (lambda_scope != decl)
9758 /* Don't reset the count if we're still in the same function. */
9759 lambda_scope = decl;
9760 lambda_count = 0;
9764 static void
9765 record_lambda_scope (tree lambda)
9767 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
9768 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
9771 static void
9772 finish_lambda_scope (void)
9774 tree_int *p = &lambda_scope_stack->last ();
9775 if (lambda_scope != p->t)
9777 lambda_scope = p->t;
9778 lambda_count = p->i;
9780 lambda_scope_stack->pop ();
9783 /* Parse a lambda expression.
9785 lambda-expression:
9786 lambda-introducer lambda-declarator [opt] compound-statement
9788 Returns a representation of the expression. */
9790 static cp_expr
9791 cp_parser_lambda_expression (cp_parser* parser)
9793 tree lambda_expr = build_lambda_expr ();
9794 tree type;
9795 bool ok = true;
9796 cp_token *token = cp_lexer_peek_token (parser->lexer);
9797 cp_token_position start = 0;
9799 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9801 if (cp_unevaluated_operand)
9803 if (!token->error_reported)
9805 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9806 "lambda-expression in unevaluated context");
9807 token->error_reported = true;
9809 ok = false;
9811 else if (parser->in_template_argument_list_p)
9813 if (!token->error_reported)
9815 error_at (token->location, "lambda-expression in template-argument");
9816 token->error_reported = true;
9818 ok = false;
9821 /* We may be in the middle of deferred access check. Disable
9822 it now. */
9823 push_deferring_access_checks (dk_no_deferred);
9825 cp_parser_lambda_introducer (parser, lambda_expr);
9827 type = begin_lambda_type (lambda_expr);
9828 if (type == error_mark_node)
9829 return error_mark_node;
9831 record_lambda_scope (lambda_expr);
9833 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9834 determine_visibility (TYPE_NAME (type));
9836 /* Now that we've started the type, add the capture fields for any
9837 explicit captures. */
9838 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9841 /* Inside the class, surrounding template-parameter-lists do not apply. */
9842 unsigned int saved_num_template_parameter_lists
9843 = parser->num_template_parameter_lists;
9844 unsigned char in_statement = parser->in_statement;
9845 bool in_switch_statement_p = parser->in_switch_statement_p;
9846 bool fully_implicit_function_template_p
9847 = parser->fully_implicit_function_template_p;
9848 tree implicit_template_parms = parser->implicit_template_parms;
9849 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9850 bool auto_is_implicit_function_template_parm_p
9851 = parser->auto_is_implicit_function_template_parm_p;
9853 parser->num_template_parameter_lists = 0;
9854 parser->in_statement = 0;
9855 parser->in_switch_statement_p = false;
9856 parser->fully_implicit_function_template_p = false;
9857 parser->implicit_template_parms = 0;
9858 parser->implicit_template_scope = 0;
9859 parser->auto_is_implicit_function_template_parm_p = false;
9861 /* By virtue of defining a local class, a lambda expression has access to
9862 the private variables of enclosing classes. */
9864 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9866 if (ok && cp_parser_error_occurred (parser))
9867 ok = false;
9869 if (ok)
9871 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9872 && cp_parser_start_tentative_firewall (parser))
9873 start = token;
9874 cp_parser_lambda_body (parser, lambda_expr);
9876 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9878 if (cp_parser_skip_to_closing_brace (parser))
9879 cp_lexer_consume_token (parser->lexer);
9882 /* The capture list was built up in reverse order; fix that now. */
9883 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9884 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9886 if (ok)
9887 maybe_add_lambda_conv_op (type);
9889 type = finish_struct (type, /*attributes=*/NULL_TREE);
9891 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9892 parser->in_statement = in_statement;
9893 parser->in_switch_statement_p = in_switch_statement_p;
9894 parser->fully_implicit_function_template_p
9895 = fully_implicit_function_template_p;
9896 parser->implicit_template_parms = implicit_template_parms;
9897 parser->implicit_template_scope = implicit_template_scope;
9898 parser->auto_is_implicit_function_template_parm_p
9899 = auto_is_implicit_function_template_parm_p;
9902 /* This field is only used during parsing of the lambda. */
9903 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9905 /* This lambda shouldn't have any proxies left at this point. */
9906 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9907 /* And now that we're done, push proxies for an enclosing lambda. */
9908 insert_pending_capture_proxies ();
9910 if (ok)
9911 lambda_expr = build_lambda_object (lambda_expr);
9912 else
9913 lambda_expr = error_mark_node;
9915 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9917 pop_deferring_access_checks ();
9919 return lambda_expr;
9922 /* Parse the beginning of a lambda expression.
9924 lambda-introducer:
9925 [ lambda-capture [opt] ]
9927 LAMBDA_EXPR is the current representation of the lambda expression. */
9929 static void
9930 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9932 /* Need commas after the first capture. */
9933 bool first = true;
9935 /* Eat the leading `['. */
9936 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9938 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9939 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9940 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9941 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9942 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9943 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9945 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9947 cp_lexer_consume_token (parser->lexer);
9948 first = false;
9951 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9953 cp_token* capture_token;
9954 tree capture_id;
9955 tree capture_init_expr;
9956 cp_id_kind idk = CP_ID_KIND_NONE;
9957 bool explicit_init_p = false;
9959 enum capture_kind_type
9961 BY_COPY,
9962 BY_REFERENCE
9964 enum capture_kind_type capture_kind = BY_COPY;
9966 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9968 error ("expected end of capture-list");
9969 return;
9972 if (first)
9973 first = false;
9974 else
9975 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9977 /* Possibly capture `this'. */
9978 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9980 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9981 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9982 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9983 "with by-copy capture default");
9984 cp_lexer_consume_token (parser->lexer);
9985 add_capture (lambda_expr,
9986 /*id=*/this_identifier,
9987 /*initializer=*/finish_this_expr (),
9988 /*by_reference_p=*/true,
9989 explicit_init_p);
9990 continue;
9993 /* Possibly capture `*this'. */
9994 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
9995 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
9997 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9998 if (cxx_dialect < cxx1z)
9999 pedwarn (loc, 0, "%<*this%> capture only available with "
10000 "-std=c++1z or -std=gnu++1z");
10001 cp_lexer_consume_token (parser->lexer);
10002 cp_lexer_consume_token (parser->lexer);
10003 add_capture (lambda_expr,
10004 /*id=*/this_identifier,
10005 /*initializer=*/finish_this_expr (),
10006 /*by_reference_p=*/false,
10007 explicit_init_p);
10008 continue;
10011 /* Remember whether we want to capture as a reference or not. */
10012 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10014 capture_kind = BY_REFERENCE;
10015 cp_lexer_consume_token (parser->lexer);
10018 /* Get the identifier. */
10019 capture_token = cp_lexer_peek_token (parser->lexer);
10020 capture_id = cp_parser_identifier (parser);
10022 if (capture_id == error_mark_node)
10023 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10024 delimiters, but I modified this to stop on unnested ']' as well. It
10025 was already changed to stop on unnested '}', so the
10026 "closing_parenthesis" name is no more misleading with my change. */
10028 cp_parser_skip_to_closing_parenthesis (parser,
10029 /*recovering=*/true,
10030 /*or_comma=*/true,
10031 /*consume_paren=*/true);
10032 break;
10035 /* Find the initializer for this capture. */
10036 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10037 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10038 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10040 bool direct, non_constant;
10041 /* An explicit initializer exists. */
10042 if (cxx_dialect < cxx14)
10043 pedwarn (input_location, 0,
10044 "lambda capture initializers "
10045 "only available with -std=c++14 or -std=gnu++14");
10046 capture_init_expr = cp_parser_initializer (parser, &direct,
10047 &non_constant);
10048 explicit_init_p = true;
10049 if (capture_init_expr == NULL_TREE)
10051 error ("empty initializer for lambda init-capture");
10052 capture_init_expr = error_mark_node;
10055 else
10057 const char* error_msg;
10059 /* Turn the identifier into an id-expression. */
10060 capture_init_expr
10061 = cp_parser_lookup_name_simple (parser, capture_id,
10062 capture_token->location);
10064 if (capture_init_expr == error_mark_node)
10066 unqualified_name_lookup_error (capture_id);
10067 continue;
10069 else if (DECL_P (capture_init_expr)
10070 && (!VAR_P (capture_init_expr)
10071 && TREE_CODE (capture_init_expr) != PARM_DECL))
10073 error_at (capture_token->location,
10074 "capture of non-variable %qD ",
10075 capture_init_expr);
10076 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10077 "%q#D declared here", capture_init_expr);
10078 continue;
10080 if (VAR_P (capture_init_expr)
10081 && decl_storage_duration (capture_init_expr) != dk_auto)
10083 if (pedwarn (capture_token->location, 0, "capture of variable "
10084 "%qD with non-automatic storage duration",
10085 capture_init_expr))
10086 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10087 "%q#D declared here", capture_init_expr);
10088 continue;
10091 capture_init_expr
10092 = finish_id_expression
10093 (capture_id,
10094 capture_init_expr,
10095 parser->scope,
10096 &idk,
10097 /*integral_constant_expression_p=*/false,
10098 /*allow_non_integral_constant_expression_p=*/false,
10099 /*non_integral_constant_expression_p=*/NULL,
10100 /*template_p=*/false,
10101 /*done=*/true,
10102 /*address_p=*/false,
10103 /*template_arg_p=*/false,
10104 &error_msg,
10105 capture_token->location);
10107 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10109 cp_lexer_consume_token (parser->lexer);
10110 capture_init_expr = make_pack_expansion (capture_init_expr);
10112 else
10113 check_for_bare_parameter_packs (capture_init_expr);
10116 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10117 && !explicit_init_p)
10119 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10120 && capture_kind == BY_COPY)
10121 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10122 "of %qD redundant with by-copy capture default",
10123 capture_id);
10124 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10125 && capture_kind == BY_REFERENCE)
10126 pedwarn (capture_token->location, 0, "explicit by-reference "
10127 "capture of %qD redundant with by-reference capture "
10128 "default", capture_id);
10131 add_capture (lambda_expr,
10132 capture_id,
10133 capture_init_expr,
10134 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10135 explicit_init_p);
10138 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10141 /* Parse the (optional) middle of a lambda expression.
10143 lambda-declarator:
10144 < template-parameter-list [opt] >
10145 ( parameter-declaration-clause [opt] )
10146 attribute-specifier [opt]
10147 decl-specifier-seq [opt]
10148 exception-specification [opt]
10149 lambda-return-type-clause [opt]
10151 LAMBDA_EXPR is the current representation of the lambda expression. */
10153 static bool
10154 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10156 /* 5.1.1.4 of the standard says:
10157 If a lambda-expression does not include a lambda-declarator, it is as if
10158 the lambda-declarator were ().
10159 This means an empty parameter list, no attributes, and no exception
10160 specification. */
10161 tree param_list = void_list_node;
10162 tree attributes = NULL_TREE;
10163 tree exception_spec = NULL_TREE;
10164 tree template_param_list = NULL_TREE;
10165 tree tx_qual = NULL_TREE;
10166 cp_decl_specifier_seq lambda_specs;
10167 clear_decl_specs (&lambda_specs);
10169 /* The template-parameter-list is optional, but must begin with
10170 an opening angle if present. */
10171 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10173 if (cxx_dialect < cxx14)
10174 pedwarn (parser->lexer->next_token->location, 0,
10175 "lambda templates are only available with "
10176 "-std=c++14 or -std=gnu++14");
10178 cp_lexer_consume_token (parser->lexer);
10180 template_param_list = cp_parser_template_parameter_list (parser);
10182 cp_parser_skip_to_end_of_template_parameter_list (parser);
10184 /* We just processed one more parameter list. */
10185 ++parser->num_template_parameter_lists;
10188 /* The parameter-declaration-clause is optional (unless
10189 template-parameter-list was given), but must begin with an
10190 opening parenthesis if present. */
10191 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10193 cp_lexer_consume_token (parser->lexer);
10195 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10197 /* Parse parameters. */
10198 param_list = cp_parser_parameter_declaration_clause (parser);
10200 /* Default arguments shall not be specified in the
10201 parameter-declaration-clause of a lambda-declarator. */
10202 if (cxx_dialect < cxx14)
10203 for (tree t = param_list; t; t = TREE_CHAIN (t))
10204 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10205 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10206 "default argument specified for lambda parameter");
10208 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10210 attributes = cp_parser_attributes_opt (parser);
10212 /* In the decl-specifier-seq of the lambda-declarator, each
10213 decl-specifier shall either be mutable or constexpr. */
10214 int declares_class_or_enum;
10215 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10216 cp_parser_decl_specifier_seq (parser,
10217 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10218 &lambda_specs, &declares_class_or_enum);
10219 if (lambda_specs.storage_class == sc_mutable)
10221 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10222 if (lambda_specs.conflicting_specifiers_p)
10223 error_at (lambda_specs.locations[ds_storage_class],
10224 "duplicate %<mutable%>");
10227 tx_qual = cp_parser_tx_qualifier_opt (parser);
10229 /* Parse optional exception specification. */
10230 exception_spec = cp_parser_exception_specification_opt (parser);
10232 /* Parse optional trailing return type. */
10233 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10235 cp_lexer_consume_token (parser->lexer);
10236 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10237 = cp_parser_trailing_type_id (parser);
10240 /* The function parameters must be in scope all the way until after the
10241 trailing-return-type in case of decltype. */
10242 pop_bindings_and_leave_scope ();
10244 else if (template_param_list != NULL_TREE) // generate diagnostic
10245 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10247 /* Create the function call operator.
10249 Messing with declarators like this is no uglier than building up the
10250 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10251 other code. */
10253 cp_decl_specifier_seq return_type_specs;
10254 cp_declarator* declarator;
10255 tree fco;
10256 int quals;
10257 void *p;
10259 clear_decl_specs (&return_type_specs);
10260 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10261 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
10262 else
10263 /* Maybe we will deduce the return type later. */
10264 return_type_specs.type = make_auto ();
10266 if (lambda_specs.locations[ds_constexpr])
10268 if (cxx_dialect >= cxx1z)
10269 return_type_specs.locations[ds_constexpr]
10270 = lambda_specs.locations[ds_constexpr];
10271 else
10272 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10273 "lambda only available with -std=c++1z or -std=gnu++1z");
10276 p = obstack_alloc (&declarator_obstack, 0);
10278 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
10279 sfk_none);
10281 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10282 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10283 declarator = make_call_declarator (declarator, param_list, quals,
10284 VIRT_SPEC_UNSPECIFIED,
10285 REF_QUAL_NONE,
10286 tx_qual,
10287 exception_spec,
10288 /*late_return_type=*/NULL_TREE,
10289 /*requires_clause*/NULL_TREE);
10290 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10292 fco = grokmethod (&return_type_specs,
10293 declarator,
10294 attributes);
10295 if (fco != error_mark_node)
10297 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10298 DECL_ARTIFICIAL (fco) = 1;
10299 /* Give the object parameter a different name. */
10300 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10301 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10302 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10304 if (template_param_list)
10306 fco = finish_member_template_decl (fco);
10307 finish_template_decl (template_param_list);
10308 --parser->num_template_parameter_lists;
10310 else if (parser->fully_implicit_function_template_p)
10311 fco = finish_fully_implicit_template (parser, fco);
10313 finish_member_declaration (fco);
10315 obstack_free (&declarator_obstack, p);
10317 return (fco != error_mark_node);
10321 /* Parse the body of a lambda expression, which is simply
10323 compound-statement
10325 but which requires special handling.
10326 LAMBDA_EXPR is the current representation of the lambda expression. */
10328 static void
10329 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10331 bool nested = (current_function_decl != NULL_TREE);
10332 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10333 if (nested)
10334 push_function_context ();
10335 else
10336 /* Still increment function_depth so that we don't GC in the
10337 middle of an expression. */
10338 ++function_depth;
10339 vec<tree> omp_privatization_save;
10340 save_omp_privatization_clauses (omp_privatization_save);
10341 /* Clear this in case we're in the middle of a default argument. */
10342 parser->local_variables_forbidden_p = false;
10344 /* Finish the function call operator
10345 - class_specifier
10346 + late_parsing_for_member
10347 + function_definition_after_declarator
10348 + ctor_initializer_opt_and_function_body */
10350 tree fco = lambda_function (lambda_expr);
10351 tree body;
10352 bool done = false;
10353 tree compound_stmt;
10354 tree cap;
10356 /* Let the front end know that we are going to be defining this
10357 function. */
10358 start_preparsed_function (fco,
10359 NULL_TREE,
10360 SF_PRE_PARSED | SF_INCLASS_INLINE);
10362 start_lambda_scope (fco);
10363 body = begin_function_body ();
10365 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10366 goto out;
10368 /* Push the proxies for any explicit captures. */
10369 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
10370 cap = TREE_CHAIN (cap))
10371 build_capture_proxy (TREE_PURPOSE (cap));
10373 compound_stmt = begin_compound_stmt (0);
10375 /* 5.1.1.4 of the standard says:
10376 If a lambda-expression does not include a trailing-return-type, it
10377 is as if the trailing-return-type denotes the following type:
10378 * if the compound-statement is of the form
10379 { return attribute-specifier [opt] expression ; }
10380 the type of the returned expression after lvalue-to-rvalue
10381 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10382 (_conv.array_ 4.2), and function-to-pointer conversion
10383 (_conv.func_ 4.3);
10384 * otherwise, void. */
10386 /* In a lambda that has neither a lambda-return-type-clause
10387 nor a deducible form, errors should be reported for return statements
10388 in the body. Since we used void as the placeholder return type, parsing
10389 the body as usual will give such desired behavior. */
10390 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10391 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
10392 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
10394 tree expr = NULL_TREE;
10395 cp_id_kind idk = CP_ID_KIND_NONE;
10397 /* Parse tentatively in case there's more after the initial return
10398 statement. */
10399 cp_parser_parse_tentatively (parser);
10401 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
10403 expr = cp_parser_expression (parser, &idk);
10405 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10406 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10408 if (cp_parser_parse_definitely (parser))
10410 if (!processing_template_decl)
10412 tree type = lambda_return_type (expr);
10413 apply_deduced_return_type (fco, type);
10414 if (type == error_mark_node)
10415 expr = error_mark_node;
10418 /* Will get error here if type not deduced yet. */
10419 finish_return_stmt (expr);
10421 done = true;
10425 if (!done)
10427 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10428 cp_parser_label_declaration (parser);
10429 cp_parser_statement_seq_opt (parser, NULL_TREE);
10430 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10433 finish_compound_stmt (compound_stmt);
10435 out:
10436 finish_function_body (body);
10437 finish_lambda_scope ();
10439 /* Finish the function and generate code for it if necessary. */
10440 tree fn = finish_function (/*inline*/2);
10442 /* Only expand if the call op is not a template. */
10443 if (!DECL_TEMPLATE_INFO (fco))
10444 expand_or_defer_fn (fn);
10447 restore_omp_privatization_clauses (omp_privatization_save);
10448 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10449 if (nested)
10450 pop_function_context();
10451 else
10452 --function_depth;
10455 /* Statements [gram.stmt.stmt] */
10457 /* Parse a statement.
10459 statement:
10460 labeled-statement
10461 expression-statement
10462 compound-statement
10463 selection-statement
10464 iteration-statement
10465 jump-statement
10466 declaration-statement
10467 try-block
10469 C++11:
10471 statement:
10472 labeled-statement
10473 attribute-specifier-seq (opt) expression-statement
10474 attribute-specifier-seq (opt) compound-statement
10475 attribute-specifier-seq (opt) selection-statement
10476 attribute-specifier-seq (opt) iteration-statement
10477 attribute-specifier-seq (opt) jump-statement
10478 declaration-statement
10479 attribute-specifier-seq (opt) try-block
10481 init-statement:
10482 expression-statement
10483 simple-declaration
10485 TM Extension:
10487 statement:
10488 atomic-statement
10490 IN_COMPOUND is true when the statement is nested inside a
10491 cp_parser_compound_statement; this matters for certain pragmas.
10493 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10494 is a (possibly labeled) if statement which is not enclosed in braces
10495 and has an else clause. This is used to implement -Wparentheses.
10497 CHAIN is a vector of if-else-if conditions. */
10499 static void
10500 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10501 bool in_compound, bool *if_p, vec<tree> *chain)
10503 tree statement, std_attrs = NULL_TREE;
10504 cp_token *token;
10505 location_t statement_location, attrs_location;
10507 restart:
10508 if (if_p != NULL)
10509 *if_p = false;
10510 /* There is no statement yet. */
10511 statement = NULL_TREE;
10513 saved_token_sentinel saved_tokens (parser->lexer);
10514 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10515 if (c_dialect_objc ())
10516 /* In obj-c++, seeing '[[' might be the either the beginning of
10517 c++11 attributes, or a nested objc-message-expression. So
10518 let's parse the c++11 attributes tentatively. */
10519 cp_parser_parse_tentatively (parser);
10520 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10521 if (c_dialect_objc ())
10523 if (!cp_parser_parse_definitely (parser))
10524 std_attrs = NULL_TREE;
10527 /* Peek at the next token. */
10528 token = cp_lexer_peek_token (parser->lexer);
10529 /* Remember the location of the first token in the statement. */
10530 statement_location = token->location;
10531 /* If this is a keyword, then that will often determine what kind of
10532 statement we have. */
10533 if (token->type == CPP_KEYWORD)
10535 enum rid keyword = token->keyword;
10537 switch (keyword)
10539 case RID_CASE:
10540 case RID_DEFAULT:
10541 /* Looks like a labeled-statement with a case label.
10542 Parse the label, and then use tail recursion to parse
10543 the statement. */
10544 cp_parser_label_for_labeled_statement (parser, std_attrs);
10545 in_compound = false;
10546 goto restart;
10548 case RID_IF:
10549 case RID_SWITCH:
10550 statement = cp_parser_selection_statement (parser, if_p, chain);
10551 break;
10553 case RID_WHILE:
10554 case RID_DO:
10555 case RID_FOR:
10556 statement = cp_parser_iteration_statement (parser, if_p, false);
10557 break;
10559 case RID_CILK_FOR:
10560 if (!flag_cilkplus)
10562 error_at (cp_lexer_peek_token (parser->lexer)->location,
10563 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10564 cp_lexer_consume_token (parser->lexer);
10565 statement = error_mark_node;
10567 else
10568 statement = cp_parser_cilk_for (parser, integer_zero_node, if_p);
10569 break;
10571 case RID_BREAK:
10572 case RID_CONTINUE:
10573 case RID_RETURN:
10574 case RID_GOTO:
10575 statement = cp_parser_jump_statement (parser);
10576 break;
10578 case RID_CILK_SYNC:
10579 cp_lexer_consume_token (parser->lexer);
10580 if (flag_cilkplus)
10582 tree sync_expr = build_cilk_sync ();
10583 SET_EXPR_LOCATION (sync_expr,
10584 token->location);
10585 statement = finish_expr_stmt (sync_expr);
10587 else
10589 error_at (token->location, "-fcilkplus must be enabled to use"
10590 " %<_Cilk_sync%>");
10591 statement = error_mark_node;
10593 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10594 break;
10596 /* Objective-C++ exception-handling constructs. */
10597 case RID_AT_TRY:
10598 case RID_AT_CATCH:
10599 case RID_AT_FINALLY:
10600 case RID_AT_SYNCHRONIZED:
10601 case RID_AT_THROW:
10602 statement = cp_parser_objc_statement (parser);
10603 break;
10605 case RID_TRY:
10606 statement = cp_parser_try_block (parser);
10607 break;
10609 case RID_NAMESPACE:
10610 /* This must be a namespace alias definition. */
10611 cp_parser_declaration_statement (parser);
10612 return;
10614 case RID_TRANSACTION_ATOMIC:
10615 case RID_TRANSACTION_RELAXED:
10616 case RID_SYNCHRONIZED:
10617 case RID_ATOMIC_NOEXCEPT:
10618 case RID_ATOMIC_CANCEL:
10619 statement = cp_parser_transaction (parser, token);
10620 break;
10621 case RID_TRANSACTION_CANCEL:
10622 statement = cp_parser_transaction_cancel (parser);
10623 break;
10625 default:
10626 /* It might be a keyword like `int' that can start a
10627 declaration-statement. */
10628 break;
10631 else if (token->type == CPP_NAME)
10633 /* If the next token is a `:', then we are looking at a
10634 labeled-statement. */
10635 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10636 if (token->type == CPP_COLON)
10638 /* Looks like a labeled-statement with an ordinary label.
10639 Parse the label, and then use tail recursion to parse
10640 the statement. */
10642 cp_parser_label_for_labeled_statement (parser, std_attrs);
10643 in_compound = false;
10644 goto restart;
10647 /* Anything that starts with a `{' must be a compound-statement. */
10648 else if (token->type == CPP_OPEN_BRACE)
10649 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10650 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10651 a statement all its own. */
10652 else if (token->type == CPP_PRAGMA)
10654 /* Only certain OpenMP pragmas are attached to statements, and thus
10655 are considered statements themselves. All others are not. In
10656 the context of a compound, accept the pragma as a "statement" and
10657 return so that we can check for a close brace. Otherwise we
10658 require a real statement and must go back and read one. */
10659 if (in_compound)
10660 cp_parser_pragma (parser, pragma_compound, if_p);
10661 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10662 goto restart;
10663 return;
10665 else if (token->type == CPP_EOF)
10667 cp_parser_error (parser, "expected statement");
10668 return;
10671 /* Everything else must be a declaration-statement or an
10672 expression-statement. Try for the declaration-statement
10673 first, unless we are looking at a `;', in which case we know that
10674 we have an expression-statement. */
10675 if (!statement)
10677 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10679 if (std_attrs != NULL_TREE)
10681 /* Attributes should be parsed as part of the the
10682 declaration, so let's un-parse them. */
10683 saved_tokens.rollback();
10684 std_attrs = NULL_TREE;
10687 cp_parser_parse_tentatively (parser);
10688 /* Try to parse the declaration-statement. */
10689 cp_parser_declaration_statement (parser);
10690 /* If that worked, we're done. */
10691 if (cp_parser_parse_definitely (parser))
10692 return;
10694 /* Look for an expression-statement instead. */
10695 statement = cp_parser_expression_statement (parser, in_statement_expr);
10697 /* Handle [[fallthrough]];. */
10698 if (attribute_fallthrough_p (std_attrs))
10700 /* The next token after the fallthrough attribute is ';'. */
10701 if (statement == NULL_TREE)
10703 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10704 statement = build_call_expr_internal_loc (statement_location,
10705 IFN_FALLTHROUGH,
10706 void_type_node, 0);
10707 finish_expr_stmt (statement);
10709 else
10710 warning_at (statement_location, OPT_Wattributes,
10711 "%<fallthrough%> attribute not followed by %<;%>");
10712 std_attrs = NULL_TREE;
10716 /* Set the line number for the statement. */
10717 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10718 SET_EXPR_LOCATION (statement, statement_location);
10720 /* Allow "[[fallthrough]];", but warn otherwise. */
10721 if (std_attrs != NULL_TREE)
10722 warning_at (attrs_location,
10723 OPT_Wattributes,
10724 "attributes at the beginning of statement are ignored");
10727 /* Parse the label for a labeled-statement, i.e.
10729 identifier :
10730 case constant-expression :
10731 default :
10733 GNU Extension:
10734 case constant-expression ... constant-expression : statement
10736 When a label is parsed without errors, the label is added to the
10737 parse tree by the finish_* functions, so this function doesn't
10738 have to return the label. */
10740 static void
10741 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10743 cp_token *token;
10744 tree label = NULL_TREE;
10745 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10747 /* The next token should be an identifier. */
10748 token = cp_lexer_peek_token (parser->lexer);
10749 if (token->type != CPP_NAME
10750 && token->type != CPP_KEYWORD)
10752 cp_parser_error (parser, "expected labeled-statement");
10753 return;
10756 /* Remember whether this case or a user-defined label is allowed to fall
10757 through to. */
10758 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
10760 parser->colon_corrects_to_scope_p = false;
10761 switch (token->keyword)
10763 case RID_CASE:
10765 tree expr, expr_hi;
10766 cp_token *ellipsis;
10768 /* Consume the `case' token. */
10769 cp_lexer_consume_token (parser->lexer);
10770 /* Parse the constant-expression. */
10771 expr = cp_parser_constant_expression (parser);
10772 if (check_for_bare_parameter_packs (expr))
10773 expr = error_mark_node;
10775 ellipsis = cp_lexer_peek_token (parser->lexer);
10776 if (ellipsis->type == CPP_ELLIPSIS)
10778 /* Consume the `...' token. */
10779 cp_lexer_consume_token (parser->lexer);
10780 expr_hi = cp_parser_constant_expression (parser);
10781 if (check_for_bare_parameter_packs (expr_hi))
10782 expr_hi = error_mark_node;
10784 /* We don't need to emit warnings here, as the common code
10785 will do this for us. */
10787 else
10788 expr_hi = NULL_TREE;
10790 if (parser->in_switch_statement_p)
10792 tree l = finish_case_label (token->location, expr, expr_hi);
10793 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10794 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10796 else
10797 error_at (token->location,
10798 "case label %qE not within a switch statement",
10799 expr);
10801 break;
10803 case RID_DEFAULT:
10804 /* Consume the `default' token. */
10805 cp_lexer_consume_token (parser->lexer);
10807 if (parser->in_switch_statement_p)
10809 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
10810 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10811 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10813 else
10814 error_at (token->location, "case label not within a switch statement");
10815 break;
10817 default:
10818 /* Anything else must be an ordinary label. */
10819 label = finish_label_stmt (cp_parser_identifier (parser));
10820 if (label && TREE_CODE (label) == LABEL_DECL)
10821 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
10822 break;
10825 /* Require the `:' token. */
10826 cp_parser_require (parser, CPP_COLON, RT_COLON);
10828 /* An ordinary label may optionally be followed by attributes.
10829 However, this is only permitted if the attributes are then
10830 followed by a semicolon. This is because, for backward
10831 compatibility, when parsing
10832 lab: __attribute__ ((unused)) int i;
10833 we want the attribute to attach to "i", not "lab". */
10834 if (label != NULL_TREE
10835 && cp_next_tokens_can_be_gnu_attribute_p (parser))
10837 tree attrs;
10838 cp_parser_parse_tentatively (parser);
10839 attrs = cp_parser_gnu_attributes_opt (parser);
10840 if (attrs == NULL_TREE
10841 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10842 cp_parser_abort_tentative_parse (parser);
10843 else if (!cp_parser_parse_definitely (parser))
10845 else
10846 attributes = chainon (attributes, attrs);
10849 if (attributes != NULL_TREE)
10850 cplus_decl_attributes (&label, attributes, 0);
10852 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10855 /* Parse an expression-statement.
10857 expression-statement:
10858 expression [opt] ;
10860 Returns the new EXPR_STMT -- or NULL_TREE if the expression
10861 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
10862 indicates whether this expression-statement is part of an
10863 expression statement. */
10865 static tree
10866 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
10868 tree statement = NULL_TREE;
10869 cp_token *token = cp_lexer_peek_token (parser->lexer);
10870 location_t loc = token->location;
10872 /* There might be attribute fallthrough. */
10873 tree attr = cp_parser_gnu_attributes_opt (parser);
10875 /* If the next token is a ';', then there is no expression
10876 statement. */
10877 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10879 statement = cp_parser_expression (parser);
10880 if (statement == error_mark_node
10881 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10883 cp_parser_skip_to_end_of_block_or_statement (parser);
10884 return error_mark_node;
10888 /* Handle [[fallthrough]];. */
10889 if (attribute_fallthrough_p (attr))
10891 /* The next token after the fallthrough attribute is ';'. */
10892 if (statement == NULL_TREE)
10893 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10894 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
10895 void_type_node, 0);
10896 else
10897 warning_at (loc, OPT_Wattributes,
10898 "%<fallthrough%> attribute not followed by %<;%>");
10899 attr = NULL_TREE;
10902 /* Allow "[[fallthrough]];", but warn otherwise. */
10903 if (attr != NULL_TREE)
10904 warning_at (loc, OPT_Wattributes,
10905 "attributes at the beginning of statement are ignored");
10907 /* Give a helpful message for "A<T>::type t;" and the like. */
10908 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
10909 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10911 if (TREE_CODE (statement) == SCOPE_REF)
10912 error_at (token->location, "need %<typename%> before %qE because "
10913 "%qT is a dependent scope",
10914 statement, TREE_OPERAND (statement, 0));
10915 else if (is_overloaded_fn (statement)
10916 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
10918 /* A::A a; */
10919 tree fn = get_first_fn (statement);
10920 error_at (token->location,
10921 "%<%T::%D%> names the constructor, not the type",
10922 DECL_CONTEXT (fn), DECL_NAME (fn));
10926 /* Consume the final `;'. */
10927 cp_parser_consume_semicolon_at_end_of_statement (parser);
10929 if (in_statement_expr
10930 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10931 /* This is the final expression statement of a statement
10932 expression. */
10933 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10934 else if (statement)
10935 statement = finish_expr_stmt (statement);
10937 return statement;
10940 /* Parse a compound-statement.
10942 compound-statement:
10943 { statement-seq [opt] }
10945 GNU extension:
10947 compound-statement:
10948 { label-declaration-seq [opt] statement-seq [opt] }
10950 label-declaration-seq:
10951 label-declaration
10952 label-declaration-seq label-declaration
10954 Returns a tree representing the statement. */
10956 static tree
10957 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10958 int bcs_flags, bool function_body)
10960 tree compound_stmt;
10962 /* Consume the `{'. */
10963 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10964 return error_mark_node;
10965 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10966 && !function_body && cxx_dialect < cxx14)
10967 pedwarn (input_location, OPT_Wpedantic,
10968 "compound-statement in constexpr function");
10969 /* Begin the compound-statement. */
10970 compound_stmt = begin_compound_stmt (bcs_flags);
10971 /* If the next keyword is `__label__' we have a label declaration. */
10972 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10973 cp_parser_label_declaration (parser);
10974 /* Parse an (optional) statement-seq. */
10975 cp_parser_statement_seq_opt (parser, in_statement_expr);
10976 /* Finish the compound-statement. */
10977 finish_compound_stmt (compound_stmt);
10978 /* Consume the `}'. */
10979 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10981 return compound_stmt;
10984 /* Parse an (optional) statement-seq.
10986 statement-seq:
10987 statement
10988 statement-seq [opt] statement */
10990 static void
10991 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10993 /* Scan statements until there aren't any more. */
10994 while (true)
10996 cp_token *token = cp_lexer_peek_token (parser->lexer);
10998 /* If we are looking at a `}', then we have run out of
10999 statements; the same is true if we have reached the end
11000 of file, or have stumbled upon a stray '@end'. */
11001 if (token->type == CPP_CLOSE_BRACE
11002 || token->type == CPP_EOF
11003 || token->type == CPP_PRAGMA_EOL
11004 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11005 break;
11007 /* If we are in a compound statement and find 'else' then
11008 something went wrong. */
11009 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11011 if (parser->in_statement & IN_IF_STMT)
11012 break;
11013 else
11015 token = cp_lexer_consume_token (parser->lexer);
11016 error_at (token->location, "%<else%> without a previous %<if%>");
11020 /* Parse the statement. */
11021 cp_parser_statement (parser, in_statement_expr, true, NULL);
11025 /* Return true if we're looking at (init; cond), false otherwise. */
11027 static bool
11028 cp_parser_init_statement_p (cp_parser *parser)
11030 /* Save tokens so that we can put them back. */
11031 cp_lexer_save_tokens (parser->lexer);
11033 /* Look for ';' that is not nested in () or {}. */
11034 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11035 /*recovering=*/false,
11036 CPP_SEMICOLON,
11037 /*consume_paren=*/false);
11039 /* Roll back the tokens we skipped. */
11040 cp_lexer_rollback_tokens (parser->lexer);
11042 return ret == -1;
11045 /* Parse a selection-statement.
11047 selection-statement:
11048 if ( init-statement [opt] condition ) statement
11049 if ( init-statement [opt] condition ) statement else statement
11050 switch ( init-statement [opt] condition ) statement
11052 Returns the new IF_STMT or SWITCH_STMT.
11054 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11055 is a (possibly labeled) if statement which is not enclosed in
11056 braces and has an else clause. This is used to implement
11057 -Wparentheses.
11059 CHAIN is a vector of if-else-if conditions. This is used to implement
11060 -Wduplicated-cond. */
11062 static tree
11063 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11064 vec<tree> *chain)
11066 cp_token *token;
11067 enum rid keyword;
11068 token_indent_info guard_tinfo;
11070 if (if_p != NULL)
11071 *if_p = false;
11073 /* Peek at the next token. */
11074 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11075 guard_tinfo = get_token_indent_info (token);
11077 /* See what kind of keyword it is. */
11078 keyword = token->keyword;
11079 switch (keyword)
11081 case RID_IF:
11082 case RID_SWITCH:
11084 tree statement;
11085 tree condition;
11087 bool cx = false;
11088 if (keyword == RID_IF
11089 && cp_lexer_next_token_is_keyword (parser->lexer,
11090 RID_CONSTEXPR))
11092 cx = true;
11093 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11094 if (cxx_dialect < cxx1z && !in_system_header_at (tok->location))
11095 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11096 "with -std=c++1z or -std=gnu++1z");
11099 /* Look for the `('. */
11100 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11102 cp_parser_skip_to_end_of_statement (parser);
11103 return error_mark_node;
11106 /* Begin the selection-statement. */
11107 if (keyword == RID_IF)
11109 statement = begin_if_stmt ();
11110 IF_STMT_CONSTEXPR_P (statement) = cx;
11112 else
11113 statement = begin_switch_stmt ();
11115 /* Parse the optional init-statement. */
11116 if (cp_parser_init_statement_p (parser))
11118 tree decl;
11119 if (cxx_dialect < cxx1z)
11120 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11121 "init-statement in selection statements only available "
11122 "with -std=c++1z or -std=gnu++1z");
11123 cp_parser_init_statement (parser, &decl);
11126 /* Parse the condition. */
11127 condition = cp_parser_condition (parser);
11128 /* Look for the `)'. */
11129 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11130 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11131 /*consume_paren=*/true);
11133 if (keyword == RID_IF)
11135 bool nested_if;
11136 unsigned char in_statement;
11138 /* Add the condition. */
11139 condition = finish_if_stmt_cond (condition, statement);
11141 if (warn_duplicated_cond)
11142 warn_duplicated_cond_add_or_warn (token->location, condition,
11143 &chain);
11145 /* Parse the then-clause. */
11146 in_statement = parser->in_statement;
11147 parser->in_statement |= IN_IF_STMT;
11149 /* Outside a template, the non-selected branch of a constexpr
11150 if is a 'discarded statement', i.e. unevaluated. */
11151 bool was_discarded = in_discarded_stmt;
11152 bool discard_then = (cx && !processing_template_decl
11153 && integer_zerop (condition));
11154 if (discard_then)
11156 in_discarded_stmt = true;
11157 ++c_inhibit_evaluation_warnings;
11160 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11161 guard_tinfo);
11163 parser->in_statement = in_statement;
11165 finish_then_clause (statement);
11167 if (discard_then)
11169 THEN_CLAUSE (statement) = NULL_TREE;
11170 in_discarded_stmt = was_discarded;
11171 --c_inhibit_evaluation_warnings;
11174 /* If the next token is `else', parse the else-clause. */
11175 if (cp_lexer_next_token_is_keyword (parser->lexer,
11176 RID_ELSE))
11178 bool discard_else = (cx && !processing_template_decl
11179 && integer_nonzerop (condition));
11180 if (discard_else)
11182 in_discarded_stmt = true;
11183 ++c_inhibit_evaluation_warnings;
11186 guard_tinfo
11187 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11188 /* Consume the `else' keyword. */
11189 cp_lexer_consume_token (parser->lexer);
11190 if (warn_duplicated_cond)
11192 if (cp_lexer_next_token_is_keyword (parser->lexer,
11193 RID_IF)
11194 && chain == NULL)
11196 /* We've got "if (COND) else if (COND2)". Start
11197 the condition chain and add COND as the first
11198 element. */
11199 chain = new vec<tree> ();
11200 if (!CONSTANT_CLASS_P (condition)
11201 && !TREE_SIDE_EFFECTS (condition))
11203 /* Wrap it in a NOP_EXPR so that we can set the
11204 location of the condition. */
11205 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11206 condition);
11207 SET_EXPR_LOCATION (e, token->location);
11208 chain->safe_push (e);
11211 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11212 RID_IF))
11214 /* This is if-else without subsequent if. Zap the
11215 condition chain; we would have already warned at
11216 this point. */
11217 delete chain;
11218 chain = NULL;
11221 begin_else_clause (statement);
11222 /* Parse the else-clause. */
11223 cp_parser_implicitly_scoped_statement (parser, NULL,
11224 guard_tinfo, chain);
11226 finish_else_clause (statement);
11228 /* If we are currently parsing a then-clause, then
11229 IF_P will not be NULL. We set it to true to
11230 indicate that this if statement has an else clause.
11231 This may trigger the Wparentheses warning below
11232 when we get back up to the parent if statement. */
11233 if (if_p != NULL)
11234 *if_p = true;
11236 if (discard_else)
11238 ELSE_CLAUSE (statement) = NULL_TREE;
11239 in_discarded_stmt = was_discarded;
11240 --c_inhibit_evaluation_warnings;
11243 else
11245 /* This if statement does not have an else clause. If
11246 NESTED_IF is true, then the then-clause has an if
11247 statement which does have an else clause. We warn
11248 about the potential ambiguity. */
11249 if (nested_if)
11250 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11251 "suggest explicit braces to avoid ambiguous"
11252 " %<else%>");
11253 if (warn_duplicated_cond)
11255 /* We don't need the condition chain anymore. */
11256 delete chain;
11257 chain = NULL;
11261 /* Now we're all done with the if-statement. */
11262 finish_if_stmt (statement);
11264 else
11266 bool in_switch_statement_p;
11267 unsigned char in_statement;
11269 /* Add the condition. */
11270 finish_switch_cond (condition, statement);
11272 /* Parse the body of the switch-statement. */
11273 in_switch_statement_p = parser->in_switch_statement_p;
11274 in_statement = parser->in_statement;
11275 parser->in_switch_statement_p = true;
11276 parser->in_statement |= IN_SWITCH_STMT;
11277 cp_parser_implicitly_scoped_statement (parser, if_p,
11278 guard_tinfo);
11279 parser->in_switch_statement_p = in_switch_statement_p;
11280 parser->in_statement = in_statement;
11282 /* Now we're all done with the switch-statement. */
11283 finish_switch_stmt (statement);
11286 return statement;
11288 break;
11290 default:
11291 cp_parser_error (parser, "expected selection-statement");
11292 return error_mark_node;
11296 /* Parse a condition.
11298 condition:
11299 expression
11300 type-specifier-seq declarator = initializer-clause
11301 type-specifier-seq declarator braced-init-list
11303 GNU Extension:
11305 condition:
11306 type-specifier-seq declarator asm-specification [opt]
11307 attributes [opt] = assignment-expression
11309 Returns the expression that should be tested. */
11311 static tree
11312 cp_parser_condition (cp_parser* parser)
11314 cp_decl_specifier_seq type_specifiers;
11315 const char *saved_message;
11316 int declares_class_or_enum;
11318 /* Try the declaration first. */
11319 cp_parser_parse_tentatively (parser);
11320 /* New types are not allowed in the type-specifier-seq for a
11321 condition. */
11322 saved_message = parser->type_definition_forbidden_message;
11323 parser->type_definition_forbidden_message
11324 = G_("types may not be defined in conditions");
11325 /* Parse the type-specifier-seq. */
11326 cp_parser_decl_specifier_seq (parser,
11327 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11328 &type_specifiers,
11329 &declares_class_or_enum);
11330 /* Restore the saved message. */
11331 parser->type_definition_forbidden_message = saved_message;
11332 /* If all is well, we might be looking at a declaration. */
11333 if (!cp_parser_error_occurred (parser))
11335 tree decl;
11336 tree asm_specification;
11337 tree attributes;
11338 cp_declarator *declarator;
11339 tree initializer = NULL_TREE;
11341 /* Parse the declarator. */
11342 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11343 /*ctor_dtor_or_conv_p=*/NULL,
11344 /*parenthesized_p=*/NULL,
11345 /*member_p=*/false,
11346 /*friend_p=*/false);
11347 /* Parse the attributes. */
11348 attributes = cp_parser_attributes_opt (parser);
11349 /* Parse the asm-specification. */
11350 asm_specification = cp_parser_asm_specification_opt (parser);
11351 /* If the next token is not an `=' or '{', then we might still be
11352 looking at an expression. For example:
11354 if (A(a).x)
11356 looks like a decl-specifier-seq and a declarator -- but then
11357 there is no `=', so this is an expression. */
11358 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11359 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11360 cp_parser_simulate_error (parser);
11362 /* If we did see an `=' or '{', then we are looking at a declaration
11363 for sure. */
11364 if (cp_parser_parse_definitely (parser))
11366 tree pushed_scope;
11367 bool non_constant_p;
11368 int flags = LOOKUP_ONLYCONVERTING;
11370 /* Create the declaration. */
11371 decl = start_decl (declarator, &type_specifiers,
11372 /*initialized_p=*/true,
11373 attributes, /*prefix_attributes=*/NULL_TREE,
11374 &pushed_scope);
11376 /* Parse the initializer. */
11377 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11379 initializer = cp_parser_braced_list (parser, &non_constant_p);
11380 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11381 flags = 0;
11383 else
11385 /* Consume the `='. */
11386 cp_parser_require (parser, CPP_EQ, RT_EQ);
11387 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11389 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11390 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11392 /* Process the initializer. */
11393 cp_finish_decl (decl,
11394 initializer, !non_constant_p,
11395 asm_specification,
11396 flags);
11398 if (pushed_scope)
11399 pop_scope (pushed_scope);
11401 return convert_from_reference (decl);
11404 /* If we didn't even get past the declarator successfully, we are
11405 definitely not looking at a declaration. */
11406 else
11407 cp_parser_abort_tentative_parse (parser);
11409 /* Otherwise, we are looking at an expression. */
11410 return cp_parser_expression (parser);
11413 /* Parses a for-statement or range-for-statement until the closing ')',
11414 not included. */
11416 static tree
11417 cp_parser_for (cp_parser *parser, bool ivdep)
11419 tree init, scope, decl;
11420 bool is_range_for;
11422 /* Begin the for-statement. */
11423 scope = begin_for_scope (&init);
11425 /* Parse the initialization. */
11426 is_range_for = cp_parser_init_statement (parser, &decl);
11428 if (is_range_for)
11429 return cp_parser_range_for (parser, scope, init, decl, ivdep);
11430 else
11431 return cp_parser_c_for (parser, scope, init, ivdep);
11434 static tree
11435 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11437 /* Normal for loop */
11438 tree condition = NULL_TREE;
11439 tree expression = NULL_TREE;
11440 tree stmt;
11442 stmt = begin_for_stmt (scope, init);
11443 /* The init-statement has already been parsed in
11444 cp_parser_init_statement, so no work is needed here. */
11445 finish_init_stmt (stmt);
11447 /* If there's a condition, process it. */
11448 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11449 condition = cp_parser_condition (parser);
11450 else if (ivdep)
11452 cp_parser_error (parser, "missing loop condition in loop with "
11453 "%<GCC ivdep%> pragma");
11454 condition = error_mark_node;
11456 finish_for_cond (condition, stmt, ivdep);
11457 /* Look for the `;'. */
11458 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11460 /* If there's an expression, process it. */
11461 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11462 expression = cp_parser_expression (parser);
11463 finish_for_expr (expression, stmt);
11465 return stmt;
11468 /* Tries to parse a range-based for-statement:
11470 range-based-for:
11471 decl-specifier-seq declarator : expression
11473 The decl-specifier-seq declarator and the `:' are already parsed by
11474 cp_parser_init_statement. If processing_template_decl it returns a
11475 newly created RANGE_FOR_STMT; if not, it is converted to a
11476 regular FOR_STMT. */
11478 static tree
11479 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11480 bool ivdep)
11482 tree stmt, range_expr;
11483 auto_vec <cxx_binding *, 16> bindings;
11484 auto_vec <tree, 16> names;
11485 tree decomp_first_name = NULL_TREE;
11486 unsigned int decomp_cnt = 0;
11488 /* Get the range declaration momentarily out of the way so that
11489 the range expression doesn't clash with it. */
11490 if (range_decl != error_mark_node)
11492 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11494 tree v = DECL_VALUE_EXPR (range_decl);
11495 /* For decomposition declaration get all of the corresponding
11496 declarations out of the way. */
11497 if (TREE_CODE (v) == ARRAY_REF
11498 && VAR_P (TREE_OPERAND (v, 0))
11499 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11501 tree d = range_decl;
11502 range_decl = TREE_OPERAND (v, 0);
11503 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11504 decomp_first_name = d;
11505 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11507 tree name = DECL_NAME (d);
11508 names.safe_push (name);
11509 bindings.safe_push (IDENTIFIER_BINDING (name));
11510 IDENTIFIER_BINDING (name)
11511 = IDENTIFIER_BINDING (name)->previous;
11515 if (names.is_empty ())
11517 tree name = DECL_NAME (range_decl);
11518 names.safe_push (name);
11519 bindings.safe_push (IDENTIFIER_BINDING (name));
11520 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11524 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11526 bool expr_non_constant_p;
11527 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11529 else
11530 range_expr = cp_parser_expression (parser);
11532 /* Put the range declaration(s) back into scope. */
11533 for (unsigned int i = 0; i < names.length (); i++)
11535 cxx_binding *binding = bindings[i];
11536 binding->previous = IDENTIFIER_BINDING (names[i]);
11537 IDENTIFIER_BINDING (names[i]) = binding;
11540 /* If in template, STMT is converted to a normal for-statement
11541 at instantiation. If not, it is done just ahead. */
11542 if (processing_template_decl)
11544 if (check_for_bare_parameter_packs (range_expr))
11545 range_expr = error_mark_node;
11546 stmt = begin_range_for_stmt (scope, init);
11547 if (ivdep)
11548 RANGE_FOR_IVDEP (stmt) = 1;
11549 finish_range_for_decl (stmt, range_decl, range_expr);
11550 if (!type_dependent_expression_p (range_expr)
11551 /* do_auto_deduction doesn't mess with template init-lists. */
11552 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11553 do_range_for_auto_deduction (range_decl, range_expr);
11555 else
11557 stmt = begin_for_stmt (scope, init);
11558 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11559 decomp_first_name, decomp_cnt, ivdep);
11561 return stmt;
11564 /* Subroutine of cp_convert_range_for: given the initializer expression,
11565 builds up the range temporary. */
11567 static tree
11568 build_range_temp (tree range_expr)
11570 tree range_type, range_temp;
11572 /* Find out the type deduced by the declaration
11573 `auto &&__range = range_expr'. */
11574 range_type = cp_build_reference_type (make_auto (), true);
11575 range_type = do_auto_deduction (range_type, range_expr,
11576 type_uses_auto (range_type));
11578 /* Create the __range variable. */
11579 range_temp = build_decl (input_location, VAR_DECL,
11580 get_identifier ("__for_range"), range_type);
11581 TREE_USED (range_temp) = 1;
11582 DECL_ARTIFICIAL (range_temp) = 1;
11584 return range_temp;
11587 /* Used by cp_parser_range_for in template context: we aren't going to
11588 do a full conversion yet, but we still need to resolve auto in the
11589 type of the for-range-declaration if present. This is basically
11590 a shortcut version of cp_convert_range_for. */
11592 static void
11593 do_range_for_auto_deduction (tree decl, tree range_expr)
11595 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11596 if (auto_node)
11598 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11599 range_temp = convert_from_reference (build_range_temp (range_expr));
11600 iter_type = (cp_parser_perform_range_for_lookup
11601 (range_temp, &begin_dummy, &end_dummy));
11602 if (iter_type)
11604 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11605 iter_type);
11606 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
11607 tf_warning_or_error);
11608 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11609 iter_decl, auto_node);
11614 /* Converts a range-based for-statement into a normal
11615 for-statement, as per the definition.
11617 for (RANGE_DECL : RANGE_EXPR)
11618 BLOCK
11620 should be equivalent to:
11623 auto &&__range = RANGE_EXPR;
11624 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11625 __begin != __end;
11626 ++__begin)
11628 RANGE_DECL = *__begin;
11629 BLOCK
11633 If RANGE_EXPR is an array:
11634 BEGIN_EXPR = __range
11635 END_EXPR = __range + ARRAY_SIZE(__range)
11636 Else if RANGE_EXPR has a member 'begin' or 'end':
11637 BEGIN_EXPR = __range.begin()
11638 END_EXPR = __range.end()
11639 Else:
11640 BEGIN_EXPR = begin(__range)
11641 END_EXPR = end(__range);
11643 If __range has a member 'begin' but not 'end', or vice versa, we must
11644 still use the second alternative (it will surely fail, however).
11645 When calling begin()/end() in the third alternative we must use
11646 argument dependent lookup, but always considering 'std' as an associated
11647 namespace. */
11649 tree
11650 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11651 tree decomp_first_name, unsigned int decomp_cnt,
11652 bool ivdep)
11654 tree begin, end;
11655 tree iter_type, begin_expr, end_expr;
11656 tree condition, expression;
11658 if (range_decl == error_mark_node || range_expr == error_mark_node)
11659 /* If an error happened previously do nothing or else a lot of
11660 unhelpful errors would be issued. */
11661 begin_expr = end_expr = iter_type = error_mark_node;
11662 else
11664 tree range_temp;
11666 if (VAR_P (range_expr)
11667 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11668 /* Can't bind a reference to an array of runtime bound. */
11669 range_temp = range_expr;
11670 else
11672 range_temp = build_range_temp (range_expr);
11673 pushdecl (range_temp);
11674 cp_finish_decl (range_temp, range_expr,
11675 /*is_constant_init*/false, NULL_TREE,
11676 LOOKUP_ONLYCONVERTING);
11677 range_temp = convert_from_reference (range_temp);
11679 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11680 &begin_expr, &end_expr);
11683 /* The new for initialization statement. */
11684 begin = build_decl (input_location, VAR_DECL,
11685 get_identifier ("__for_begin"), iter_type);
11686 TREE_USED (begin) = 1;
11687 DECL_ARTIFICIAL (begin) = 1;
11688 pushdecl (begin);
11689 cp_finish_decl (begin, begin_expr,
11690 /*is_constant_init*/false, NULL_TREE,
11691 LOOKUP_ONLYCONVERTING);
11693 if (cxx_dialect >= cxx1z)
11694 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11695 end = build_decl (input_location, VAR_DECL,
11696 get_identifier ("__for_end"), iter_type);
11697 TREE_USED (end) = 1;
11698 DECL_ARTIFICIAL (end) = 1;
11699 pushdecl (end);
11700 cp_finish_decl (end, end_expr,
11701 /*is_constant_init*/false, NULL_TREE,
11702 LOOKUP_ONLYCONVERTING);
11704 finish_init_stmt (statement);
11706 /* The new for condition. */
11707 condition = build_x_binary_op (input_location, NE_EXPR,
11708 begin, ERROR_MARK,
11709 end, ERROR_MARK,
11710 NULL, tf_warning_or_error);
11711 finish_for_cond (condition, statement, ivdep);
11713 /* The new increment expression. */
11714 expression = finish_unary_op_expr (input_location,
11715 PREINCREMENT_EXPR, begin,
11716 tf_warning_or_error);
11717 finish_for_expr (expression, statement);
11719 /* The declaration is initialized with *__begin inside the loop body. */
11720 cp_finish_decl (range_decl,
11721 build_x_indirect_ref (input_location, begin, RO_NULL,
11722 tf_warning_or_error),
11723 /*is_constant_init*/false, NULL_TREE,
11724 LOOKUP_ONLYCONVERTING);
11725 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11726 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
11728 return statement;
11731 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11732 We need to solve both at the same time because the method used
11733 depends on the existence of members begin or end.
11734 Returns the type deduced for the iterator expression. */
11736 static tree
11737 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11739 if (error_operand_p (range))
11741 *begin = *end = error_mark_node;
11742 return error_mark_node;
11745 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11747 error ("range-based %<for%> expression of type %qT "
11748 "has incomplete type", TREE_TYPE (range));
11749 *begin = *end = error_mark_node;
11750 return error_mark_node;
11752 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11754 /* If RANGE is an array, we will use pointer arithmetic. */
11755 *begin = decay_conversion (range, tf_warning_or_error);
11756 *end = build_binary_op (input_location, PLUS_EXPR,
11757 range,
11758 array_type_nelts_top (TREE_TYPE (range)),
11760 return TREE_TYPE (*begin);
11762 else
11764 /* If it is not an array, we must do a bit of magic. */
11765 tree id_begin, id_end;
11766 tree member_begin, member_end;
11768 *begin = *end = error_mark_node;
11770 id_begin = get_identifier ("begin");
11771 id_end = get_identifier ("end");
11772 member_begin = lookup_member (TREE_TYPE (range), id_begin,
11773 /*protect=*/2, /*want_type=*/false,
11774 tf_warning_or_error);
11775 member_end = lookup_member (TREE_TYPE (range), id_end,
11776 /*protect=*/2, /*want_type=*/false,
11777 tf_warning_or_error);
11779 if (member_begin != NULL_TREE || member_end != NULL_TREE)
11781 /* Use the member functions. */
11782 if (member_begin != NULL_TREE)
11783 *begin = cp_parser_range_for_member_function (range, id_begin);
11784 else
11785 error ("range-based %<for%> expression of type %qT has an "
11786 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11788 if (member_end != NULL_TREE)
11789 *end = cp_parser_range_for_member_function (range, id_end);
11790 else
11791 error ("range-based %<for%> expression of type %qT has a "
11792 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
11794 else
11796 /* Use global functions with ADL. */
11797 vec<tree, va_gc> *vec;
11798 vec = make_tree_vector ();
11800 vec_safe_push (vec, range);
11802 member_begin = perform_koenig_lookup (id_begin, vec,
11803 tf_warning_or_error);
11804 *begin = finish_call_expr (member_begin, &vec, false, true,
11805 tf_warning_or_error);
11806 member_end = perform_koenig_lookup (id_end, vec,
11807 tf_warning_or_error);
11808 *end = finish_call_expr (member_end, &vec, false, true,
11809 tf_warning_or_error);
11811 release_tree_vector (vec);
11814 /* Last common checks. */
11815 if (*begin == error_mark_node || *end == error_mark_node)
11817 /* If one of the expressions is an error do no more checks. */
11818 *begin = *end = error_mark_node;
11819 return error_mark_node;
11821 else if (type_dependent_expression_p (*begin)
11822 || type_dependent_expression_p (*end))
11823 /* Can happen, when, eg, in a template context, Koenig lookup
11824 can't resolve begin/end (c++/58503). */
11825 return NULL_TREE;
11826 else
11828 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
11829 /* The unqualified type of the __begin and __end temporaries should
11830 be the same, as required by the multiple auto declaration. */
11831 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
11833 if (cxx_dialect >= cxx1z
11834 && (build_x_binary_op (input_location, NE_EXPR,
11835 *begin, ERROR_MARK,
11836 *end, ERROR_MARK,
11837 NULL, tf_none)
11838 != error_mark_node))
11839 /* P0184R0 allows __begin and __end to have different types,
11840 but make sure they are comparable so we can give a better
11841 diagnostic. */;
11842 else
11843 error ("inconsistent begin/end types in range-based %<for%> "
11844 "statement: %qT and %qT",
11845 TREE_TYPE (*begin), TREE_TYPE (*end));
11847 return iter_type;
11852 /* Helper function for cp_parser_perform_range_for_lookup.
11853 Builds a tree for RANGE.IDENTIFIER(). */
11855 static tree
11856 cp_parser_range_for_member_function (tree range, tree identifier)
11858 tree member, res;
11859 vec<tree, va_gc> *vec;
11861 member = finish_class_member_access_expr (range, identifier,
11862 false, tf_warning_or_error);
11863 if (member == error_mark_node)
11864 return error_mark_node;
11866 vec = make_tree_vector ();
11867 res = finish_call_expr (member, &vec,
11868 /*disallow_virtual=*/false,
11869 /*koenig_p=*/false,
11870 tf_warning_or_error);
11871 release_tree_vector (vec);
11872 return res;
11875 /* Parse an iteration-statement.
11877 iteration-statement:
11878 while ( condition ) statement
11879 do statement while ( expression ) ;
11880 for ( init-statement condition [opt] ; expression [opt] )
11881 statement
11883 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
11885 static tree
11886 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep)
11888 cp_token *token;
11889 enum rid keyword;
11890 tree statement;
11891 unsigned char in_statement;
11892 token_indent_info guard_tinfo;
11894 /* Peek at the next token. */
11895 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
11896 if (!token)
11897 return error_mark_node;
11899 guard_tinfo = get_token_indent_info (token);
11901 /* Remember whether or not we are already within an iteration
11902 statement. */
11903 in_statement = parser->in_statement;
11905 /* See what kind of keyword it is. */
11906 keyword = token->keyword;
11907 switch (keyword)
11909 case RID_WHILE:
11911 tree condition;
11913 /* Begin the while-statement. */
11914 statement = begin_while_stmt ();
11915 /* Look for the `('. */
11916 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11917 /* Parse the condition. */
11918 condition = cp_parser_condition (parser);
11919 finish_while_stmt_cond (condition, statement, ivdep);
11920 /* Look for the `)'. */
11921 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11922 /* Parse the dependent statement. */
11923 parser->in_statement = IN_ITERATION_STMT;
11924 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11925 parser->in_statement = in_statement;
11926 /* We're done with the while-statement. */
11927 finish_while_stmt (statement);
11929 break;
11931 case RID_DO:
11933 tree expression;
11935 /* Begin the do-statement. */
11936 statement = begin_do_stmt ();
11937 /* Parse the body of the do-statement. */
11938 parser->in_statement = IN_ITERATION_STMT;
11939 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
11940 parser->in_statement = in_statement;
11941 finish_do_body (statement);
11942 /* Look for the `while' keyword. */
11943 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
11944 /* Look for the `('. */
11945 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11946 /* Parse the expression. */
11947 expression = cp_parser_expression (parser);
11948 /* We're done with the do-statement. */
11949 finish_do_stmt (expression, statement, ivdep);
11950 /* Look for the `)'. */
11951 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11952 /* Look for the `;'. */
11953 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11955 break;
11957 case RID_FOR:
11959 /* Look for the `('. */
11960 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11962 statement = cp_parser_for (parser, ivdep);
11964 /* Look for the `)'. */
11965 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11967 /* Parse the body of the for-statement. */
11968 parser->in_statement = IN_ITERATION_STMT;
11969 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11970 parser->in_statement = in_statement;
11972 /* We're done with the for-statement. */
11973 finish_for_stmt (statement);
11975 break;
11977 default:
11978 cp_parser_error (parser, "expected iteration-statement");
11979 statement = error_mark_node;
11980 break;
11983 return statement;
11986 /* Parse a init-statement or the declarator of a range-based-for.
11987 Returns true if a range-based-for declaration is seen.
11989 init-statement:
11990 expression-statement
11991 simple-declaration */
11993 static bool
11994 cp_parser_init_statement (cp_parser* parser, tree *decl)
11996 /* If the next token is a `;', then we have an empty
11997 expression-statement. Grammatically, this is also a
11998 simple-declaration, but an invalid one, because it does not
11999 declare anything. Therefore, if we did not handle this case
12000 specially, we would issue an error message about an invalid
12001 declaration. */
12002 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12004 bool is_range_for = false;
12005 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12007 /* A colon is used in range-based for. */
12008 parser->colon_corrects_to_scope_p = false;
12010 /* We're going to speculatively look for a declaration, falling back
12011 to an expression, if necessary. */
12012 cp_parser_parse_tentatively (parser);
12013 /* Parse the declaration. */
12014 cp_parser_simple_declaration (parser,
12015 /*function_definition_allowed_p=*/false,
12016 decl);
12017 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12018 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12020 /* It is a range-for, consume the ':' */
12021 cp_lexer_consume_token (parser->lexer);
12022 is_range_for = true;
12023 if (cxx_dialect < cxx11)
12025 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12026 "range-based %<for%> loops only available with "
12027 "-std=c++11 or -std=gnu++11");
12028 *decl = error_mark_node;
12031 else
12032 /* The ';' is not consumed yet because we told
12033 cp_parser_simple_declaration not to. */
12034 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12036 if (cp_parser_parse_definitely (parser))
12037 return is_range_for;
12038 /* If the tentative parse failed, then we shall need to look for an
12039 expression-statement. */
12041 /* If we are here, it is an expression-statement. */
12042 cp_parser_expression_statement (parser, NULL_TREE);
12043 return false;
12046 /* Parse a jump-statement.
12048 jump-statement:
12049 break ;
12050 continue ;
12051 return expression [opt] ;
12052 return braced-init-list ;
12053 goto identifier ;
12055 GNU extension:
12057 jump-statement:
12058 goto * expression ;
12060 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12062 static tree
12063 cp_parser_jump_statement (cp_parser* parser)
12065 tree statement = error_mark_node;
12066 cp_token *token;
12067 enum rid keyword;
12068 unsigned char in_statement;
12070 /* Peek at the next token. */
12071 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12072 if (!token)
12073 return error_mark_node;
12075 /* See what kind of keyword it is. */
12076 keyword = token->keyword;
12077 switch (keyword)
12079 case RID_BREAK:
12080 in_statement = parser->in_statement & ~IN_IF_STMT;
12081 switch (in_statement)
12083 case 0:
12084 error_at (token->location, "break statement not within loop or switch");
12085 break;
12086 default:
12087 gcc_assert ((in_statement & IN_SWITCH_STMT)
12088 || in_statement == IN_ITERATION_STMT);
12089 statement = finish_break_stmt ();
12090 if (in_statement == IN_ITERATION_STMT)
12091 break_maybe_infinite_loop ();
12092 break;
12093 case IN_OMP_BLOCK:
12094 error_at (token->location, "invalid exit from OpenMP structured block");
12095 break;
12096 case IN_OMP_FOR:
12097 error_at (token->location, "break statement used with OpenMP for loop");
12098 break;
12099 case IN_CILK_SIMD_FOR:
12100 error_at (token->location, "break statement used with Cilk Plus for loop");
12101 break;
12103 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12104 break;
12106 case RID_CONTINUE:
12107 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12109 case 0:
12110 error_at (token->location, "continue statement not within a loop");
12111 break;
12112 case IN_CILK_SIMD_FOR:
12113 error_at (token->location,
12114 "continue statement within %<#pragma simd%> loop body");
12115 /* Fall through. */
12116 case IN_ITERATION_STMT:
12117 case IN_OMP_FOR:
12118 statement = finish_continue_stmt ();
12119 break;
12120 case IN_OMP_BLOCK:
12121 error_at (token->location, "invalid exit from OpenMP structured block");
12122 break;
12123 default:
12124 gcc_unreachable ();
12126 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12127 break;
12129 case RID_RETURN:
12131 tree expr;
12132 bool expr_non_constant_p;
12134 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12136 cp_lexer_set_source_position (parser->lexer);
12137 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12138 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12140 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12141 expr = cp_parser_expression (parser);
12142 else
12143 /* If the next token is a `;', then there is no
12144 expression. */
12145 expr = NULL_TREE;
12146 /* Build the return-statement. */
12147 if (current_function_auto_return_pattern && in_discarded_stmt)
12148 /* Don't deduce from a discarded return statement. */;
12149 else
12150 statement = finish_return_stmt (expr);
12151 /* Look for the final `;'. */
12152 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12154 break;
12156 case RID_GOTO:
12157 if (parser->in_function_body
12158 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12160 error ("%<goto%> in %<constexpr%> function");
12161 cp_function_chain->invalid_constexpr = true;
12164 /* Create the goto-statement. */
12165 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12167 /* Issue a warning about this use of a GNU extension. */
12168 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12169 /* Consume the '*' token. */
12170 cp_lexer_consume_token (parser->lexer);
12171 /* Parse the dependent expression. */
12172 finish_goto_stmt (cp_parser_expression (parser));
12174 else
12175 finish_goto_stmt (cp_parser_identifier (parser));
12176 /* Look for the final `;'. */
12177 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12178 break;
12180 default:
12181 cp_parser_error (parser, "expected jump-statement");
12182 break;
12185 return statement;
12188 /* Parse a declaration-statement.
12190 declaration-statement:
12191 block-declaration */
12193 static void
12194 cp_parser_declaration_statement (cp_parser* parser)
12196 void *p;
12198 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12199 p = obstack_alloc (&declarator_obstack, 0);
12201 /* Parse the block-declaration. */
12202 cp_parser_block_declaration (parser, /*statement_p=*/true);
12204 /* Free any declarators allocated. */
12205 obstack_free (&declarator_obstack, p);
12208 /* Some dependent statements (like `if (cond) statement'), are
12209 implicitly in their own scope. In other words, if the statement is
12210 a single statement (as opposed to a compound-statement), it is
12211 none-the-less treated as if it were enclosed in braces. Any
12212 declarations appearing in the dependent statement are out of scope
12213 after control passes that point. This function parses a statement,
12214 but ensures that is in its own scope, even if it is not a
12215 compound-statement.
12217 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12218 is a (possibly labeled) if statement which is not enclosed in
12219 braces and has an else clause. This is used to implement
12220 -Wparentheses.
12222 CHAIN is a vector of if-else-if conditions. This is used to implement
12223 -Wduplicated-cond.
12225 Returns the new statement. */
12227 static tree
12228 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12229 const token_indent_info &guard_tinfo,
12230 vec<tree> *chain)
12232 tree statement;
12233 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12234 token_indent_info body_tinfo
12235 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12237 if (if_p != NULL)
12238 *if_p = false;
12240 /* Mark if () ; with a special NOP_EXPR. */
12241 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12243 cp_lexer_consume_token (parser->lexer);
12244 statement = add_stmt (build_empty_stmt (body_loc));
12246 if (guard_tinfo.keyword == RID_IF
12247 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12248 warning_at (body_loc, OPT_Wempty_body,
12249 "suggest braces around empty body in an %<if%> statement");
12250 else if (guard_tinfo.keyword == RID_ELSE)
12251 warning_at (body_loc, OPT_Wempty_body,
12252 "suggest braces around empty body in an %<else%> statement");
12254 /* if a compound is opened, we simply parse the statement directly. */
12255 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12256 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12257 /* If the token is not a `{', then we must take special action. */
12258 else
12260 /* Create a compound-statement. */
12261 statement = begin_compound_stmt (0);
12262 /* Parse the dependent-statement. */
12263 cp_parser_statement (parser, NULL_TREE, false, if_p, chain);
12264 /* Finish the dummy compound-statement. */
12265 finish_compound_stmt (statement);
12268 token_indent_info next_tinfo
12269 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12270 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12272 /* Return the statement. */
12273 return statement;
12276 /* For some dependent statements (like `while (cond) statement'), we
12277 have already created a scope. Therefore, even if the dependent
12278 statement is a compound-statement, we do not want to create another
12279 scope. */
12281 static void
12282 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12283 const token_indent_info &guard_tinfo)
12285 /* If the token is a `{', then we must take special action. */
12286 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12288 token_indent_info body_tinfo
12289 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12291 cp_parser_statement (parser, NULL_TREE, false, if_p);
12292 token_indent_info next_tinfo
12293 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12294 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12296 else
12298 /* Avoid calling cp_parser_compound_statement, so that we
12299 don't create a new scope. Do everything else by hand. */
12300 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
12301 /* If the next keyword is `__label__' we have a label declaration. */
12302 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12303 cp_parser_label_declaration (parser);
12304 /* Parse an (optional) statement-seq. */
12305 cp_parser_statement_seq_opt (parser, NULL_TREE);
12306 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12310 /* Declarations [gram.dcl.dcl] */
12312 /* Parse an optional declaration-sequence.
12314 declaration-seq:
12315 declaration
12316 declaration-seq declaration */
12318 static void
12319 cp_parser_declaration_seq_opt (cp_parser* parser)
12321 while (true)
12323 cp_token *token;
12325 token = cp_lexer_peek_token (parser->lexer);
12327 if (token->type == CPP_CLOSE_BRACE
12328 || token->type == CPP_EOF
12329 || token->type == CPP_PRAGMA_EOL)
12330 break;
12332 if (token->type == CPP_SEMICOLON)
12334 /* A declaration consisting of a single semicolon is
12335 invalid. Allow it unless we're being pedantic. */
12336 cp_lexer_consume_token (parser->lexer);
12337 if (!in_system_header_at (input_location))
12338 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12339 continue;
12342 /* If we're entering or exiting a region that's implicitly
12343 extern "C", modify the lang context appropriately. */
12344 if (!parser->implicit_extern_c && token->implicit_extern_c)
12346 push_lang_context (lang_name_c);
12347 parser->implicit_extern_c = true;
12349 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12351 pop_lang_context ();
12352 parser->implicit_extern_c = false;
12355 if (token->type == CPP_PRAGMA)
12357 /* A top-level declaration can consist solely of a #pragma.
12358 A nested declaration cannot, so this is done here and not
12359 in cp_parser_declaration. (A #pragma at block scope is
12360 handled in cp_parser_statement.) */
12361 cp_parser_pragma (parser, pragma_external, NULL);
12362 continue;
12365 /* Parse the declaration itself. */
12366 cp_parser_declaration (parser);
12370 /* Parse a declaration.
12372 declaration:
12373 block-declaration
12374 function-definition
12375 template-declaration
12376 explicit-instantiation
12377 explicit-specialization
12378 linkage-specification
12379 namespace-definition
12381 C++17:
12382 deduction-guide
12384 GNU extension:
12386 declaration:
12387 __extension__ declaration */
12389 static void
12390 cp_parser_declaration (cp_parser* parser)
12392 cp_token token1;
12393 cp_token token2;
12394 int saved_pedantic;
12395 void *p;
12396 tree attributes = NULL_TREE;
12398 /* Check for the `__extension__' keyword. */
12399 if (cp_parser_extension_opt (parser, &saved_pedantic))
12401 /* Parse the qualified declaration. */
12402 cp_parser_declaration (parser);
12403 /* Restore the PEDANTIC flag. */
12404 pedantic = saved_pedantic;
12406 return;
12409 /* Try to figure out what kind of declaration is present. */
12410 token1 = *cp_lexer_peek_token (parser->lexer);
12412 if (token1.type != CPP_EOF)
12413 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12414 else
12416 token2.type = CPP_EOF;
12417 token2.keyword = RID_MAX;
12420 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12421 p = obstack_alloc (&declarator_obstack, 0);
12423 /* If the next token is `extern' and the following token is a string
12424 literal, then we have a linkage specification. */
12425 if (token1.keyword == RID_EXTERN
12426 && cp_parser_is_pure_string_literal (&token2))
12427 cp_parser_linkage_specification (parser);
12428 /* If the next token is `template', then we have either a template
12429 declaration, an explicit instantiation, or an explicit
12430 specialization. */
12431 else if (token1.keyword == RID_TEMPLATE)
12433 /* `template <>' indicates a template specialization. */
12434 if (token2.type == CPP_LESS
12435 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12436 cp_parser_explicit_specialization (parser);
12437 /* `template <' indicates a template declaration. */
12438 else if (token2.type == CPP_LESS)
12439 cp_parser_template_declaration (parser, /*member_p=*/false);
12440 /* Anything else must be an explicit instantiation. */
12441 else
12442 cp_parser_explicit_instantiation (parser);
12444 /* If the next token is `export', then we have a template
12445 declaration. */
12446 else if (token1.keyword == RID_EXPORT)
12447 cp_parser_template_declaration (parser, /*member_p=*/false);
12448 /* If the next token is `extern', 'static' or 'inline' and the one
12449 after that is `template', we have a GNU extended explicit
12450 instantiation directive. */
12451 else if (cp_parser_allow_gnu_extensions_p (parser)
12452 && (token1.keyword == RID_EXTERN
12453 || token1.keyword == RID_STATIC
12454 || token1.keyword == RID_INLINE)
12455 && token2.keyword == RID_TEMPLATE)
12456 cp_parser_explicit_instantiation (parser);
12457 /* If the next token is `namespace', check for a named or unnamed
12458 namespace definition. */
12459 else if (token1.keyword == RID_NAMESPACE
12460 && (/* A named namespace definition. */
12461 (token2.type == CPP_NAME
12462 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12463 != CPP_EQ))
12464 || (token2.type == CPP_OPEN_SQUARE
12465 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12466 == CPP_OPEN_SQUARE)
12467 /* An unnamed namespace definition. */
12468 || token2.type == CPP_OPEN_BRACE
12469 || token2.keyword == RID_ATTRIBUTE))
12470 cp_parser_namespace_definition (parser);
12471 /* An inline (associated) namespace definition. */
12472 else if (token1.keyword == RID_INLINE
12473 && token2.keyword == RID_NAMESPACE)
12474 cp_parser_namespace_definition (parser);
12475 /* Objective-C++ declaration/definition. */
12476 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12477 cp_parser_objc_declaration (parser, NULL_TREE);
12478 else if (c_dialect_objc ()
12479 && token1.keyword == RID_ATTRIBUTE
12480 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12481 cp_parser_objc_declaration (parser, attributes);
12482 /* At this point we may have a template declared by a concept
12483 introduction. */
12484 else if (flag_concepts
12485 && cp_parser_template_declaration_after_export (parser,
12486 /*member_p=*/false))
12487 /* We did. */;
12488 else
12489 /* Try to parse a block-declaration, or a function-definition. */
12490 cp_parser_block_declaration (parser, /*statement_p=*/false);
12492 /* Free any declarators allocated. */
12493 obstack_free (&declarator_obstack, p);
12496 /* Parse a block-declaration.
12498 block-declaration:
12499 simple-declaration
12500 asm-definition
12501 namespace-alias-definition
12502 using-declaration
12503 using-directive
12505 GNU Extension:
12507 block-declaration:
12508 __extension__ block-declaration
12510 C++0x Extension:
12512 block-declaration:
12513 static_assert-declaration
12515 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12516 part of a declaration-statement. */
12518 static void
12519 cp_parser_block_declaration (cp_parser *parser,
12520 bool statement_p)
12522 cp_token *token1;
12523 int saved_pedantic;
12525 /* Check for the `__extension__' keyword. */
12526 if (cp_parser_extension_opt (parser, &saved_pedantic))
12528 /* Parse the qualified declaration. */
12529 cp_parser_block_declaration (parser, statement_p);
12530 /* Restore the PEDANTIC flag. */
12531 pedantic = saved_pedantic;
12533 return;
12536 /* Peek at the next token to figure out which kind of declaration is
12537 present. */
12538 token1 = cp_lexer_peek_token (parser->lexer);
12540 /* If the next keyword is `asm', we have an asm-definition. */
12541 if (token1->keyword == RID_ASM)
12543 if (statement_p)
12544 cp_parser_commit_to_tentative_parse (parser);
12545 cp_parser_asm_definition (parser);
12547 /* If the next keyword is `namespace', we have a
12548 namespace-alias-definition. */
12549 else if (token1->keyword == RID_NAMESPACE)
12550 cp_parser_namespace_alias_definition (parser);
12551 /* If the next keyword is `using', we have a
12552 using-declaration, a using-directive, or an alias-declaration. */
12553 else if (token1->keyword == RID_USING)
12555 cp_token *token2;
12557 if (statement_p)
12558 cp_parser_commit_to_tentative_parse (parser);
12559 /* If the token after `using' is `namespace', then we have a
12560 using-directive. */
12561 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12562 if (token2->keyword == RID_NAMESPACE)
12563 cp_parser_using_directive (parser);
12564 /* If the second token after 'using' is '=', then we have an
12565 alias-declaration. */
12566 else if (cxx_dialect >= cxx11
12567 && token2->type == CPP_NAME
12568 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12569 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12570 cp_parser_alias_declaration (parser);
12571 /* Otherwise, it's a using-declaration. */
12572 else
12573 cp_parser_using_declaration (parser,
12574 /*access_declaration_p=*/false);
12576 /* If the next keyword is `__label__' we have a misplaced label
12577 declaration. */
12578 else if (token1->keyword == RID_LABEL)
12580 cp_lexer_consume_token (parser->lexer);
12581 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12582 cp_parser_skip_to_end_of_statement (parser);
12583 /* If the next token is now a `;', consume it. */
12584 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12585 cp_lexer_consume_token (parser->lexer);
12587 /* If the next token is `static_assert' we have a static assertion. */
12588 else if (token1->keyword == RID_STATIC_ASSERT)
12589 cp_parser_static_assert (parser, /*member_p=*/false);
12590 /* Anything else must be a simple-declaration. */
12591 else
12592 cp_parser_simple_declaration (parser, !statement_p,
12593 /*maybe_range_for_decl*/NULL);
12596 /* Parse a simple-declaration.
12598 simple-declaration:
12599 decl-specifier-seq [opt] init-declarator-list [opt] ;
12600 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12601 brace-or-equal-initializer ;
12603 init-declarator-list:
12604 init-declarator
12605 init-declarator-list , init-declarator
12607 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12608 function-definition as a simple-declaration.
12610 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12611 parsed declaration if it is an uninitialized single declarator not followed
12612 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12613 if present, will not be consumed. */
12615 static void
12616 cp_parser_simple_declaration (cp_parser* parser,
12617 bool function_definition_allowed_p,
12618 tree *maybe_range_for_decl)
12620 cp_decl_specifier_seq decl_specifiers;
12621 int declares_class_or_enum;
12622 bool saw_declarator;
12623 location_t comma_loc = UNKNOWN_LOCATION;
12624 location_t init_loc = UNKNOWN_LOCATION;
12626 if (maybe_range_for_decl)
12627 *maybe_range_for_decl = NULL_TREE;
12629 /* Defer access checks until we know what is being declared; the
12630 checks for names appearing in the decl-specifier-seq should be
12631 done as if we were in the scope of the thing being declared. */
12632 push_deferring_access_checks (dk_deferred);
12634 /* Parse the decl-specifier-seq. We have to keep track of whether
12635 or not the decl-specifier-seq declares a named class or
12636 enumeration type, since that is the only case in which the
12637 init-declarator-list is allowed to be empty.
12639 [dcl.dcl]
12641 In a simple-declaration, the optional init-declarator-list can be
12642 omitted only when declaring a class or enumeration, that is when
12643 the decl-specifier-seq contains either a class-specifier, an
12644 elaborated-type-specifier, or an enum-specifier. */
12645 cp_parser_decl_specifier_seq (parser,
12646 CP_PARSER_FLAGS_OPTIONAL,
12647 &decl_specifiers,
12648 &declares_class_or_enum);
12649 /* We no longer need to defer access checks. */
12650 stop_deferring_access_checks ();
12652 /* In a block scope, a valid declaration must always have a
12653 decl-specifier-seq. By not trying to parse declarators, we can
12654 resolve the declaration/expression ambiguity more quickly. */
12655 if (!function_definition_allowed_p
12656 && !decl_specifiers.any_specifiers_p)
12658 cp_parser_error (parser, "expected declaration");
12659 goto done;
12662 /* If the next two tokens are both identifiers, the code is
12663 erroneous. The usual cause of this situation is code like:
12665 T t;
12667 where "T" should name a type -- but does not. */
12668 if (!decl_specifiers.any_type_specifiers_p
12669 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12671 /* If parsing tentatively, we should commit; we really are
12672 looking at a declaration. */
12673 cp_parser_commit_to_tentative_parse (parser);
12674 /* Give up. */
12675 goto done;
12678 /* If we have seen at least one decl-specifier, and the next token
12679 is not a parenthesis, then we must be looking at a declaration.
12680 (After "int (" we might be looking at a functional cast.) */
12681 if (decl_specifiers.any_specifiers_p
12682 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12683 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12684 && !cp_parser_error_occurred (parser))
12685 cp_parser_commit_to_tentative_parse (parser);
12687 /* Look for C++17 decomposition declaration. */
12688 for (size_t n = 1; ; n++)
12689 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
12690 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
12691 continue;
12692 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
12693 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
12694 && decl_specifiers.any_specifiers_p)
12696 tree decl
12697 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
12698 maybe_range_for_decl,
12699 &init_loc);
12701 /* The next token should be either a `,' or a `;'. */
12702 cp_token *token = cp_lexer_peek_token (parser->lexer);
12703 /* If it's a `;', we are done. */
12704 if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12705 goto finish;
12706 /* Anything else is an error. */
12707 else
12709 /* If we have already issued an error message we don't need
12710 to issue another one. */
12711 if ((decl != error_mark_node
12712 && DECL_INITIAL (decl) != error_mark_node)
12713 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12714 cp_parser_error (parser, "expected %<,%> or %<;%>");
12715 /* Skip tokens until we reach the end of the statement. */
12716 cp_parser_skip_to_end_of_statement (parser);
12717 /* If the next token is now a `;', consume it. */
12718 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12719 cp_lexer_consume_token (parser->lexer);
12720 goto done;
12723 else
12724 break;
12726 tree last_type;
12727 bool auto_specifier_p;
12728 /* NULL_TREE if both variable and function declaration are allowed,
12729 error_mark_node if function declaration are not allowed and
12730 a FUNCTION_DECL that should be diagnosed if it is followed by
12731 variable declarations. */
12732 tree auto_function_declaration;
12734 last_type = NULL_TREE;
12735 auto_specifier_p
12736 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
12737 auto_function_declaration = NULL_TREE;
12739 /* Keep going until we hit the `;' at the end of the simple
12740 declaration. */
12741 saw_declarator = false;
12742 while (cp_lexer_next_token_is_not (parser->lexer,
12743 CPP_SEMICOLON))
12745 cp_token *token;
12746 bool function_definition_p;
12747 tree decl;
12748 tree auto_result = NULL_TREE;
12750 if (saw_declarator)
12752 /* If we are processing next declarator, comma is expected */
12753 token = cp_lexer_peek_token (parser->lexer);
12754 gcc_assert (token->type == CPP_COMMA);
12755 cp_lexer_consume_token (parser->lexer);
12756 if (maybe_range_for_decl)
12758 *maybe_range_for_decl = error_mark_node;
12759 if (comma_loc == UNKNOWN_LOCATION)
12760 comma_loc = token->location;
12763 else
12764 saw_declarator = true;
12766 /* Parse the init-declarator. */
12767 decl = cp_parser_init_declarator (parser, &decl_specifiers,
12768 /*checks=*/NULL,
12769 function_definition_allowed_p,
12770 /*member_p=*/false,
12771 declares_class_or_enum,
12772 &function_definition_p,
12773 maybe_range_for_decl,
12774 &init_loc,
12775 &auto_result);
12776 /* If an error occurred while parsing tentatively, exit quickly.
12777 (That usually happens when in the body of a function; each
12778 statement is treated as a declaration-statement until proven
12779 otherwise.) */
12780 if (cp_parser_error_occurred (parser))
12781 goto done;
12783 if (auto_specifier_p && cxx_dialect >= cxx14)
12785 /* If the init-declarator-list contains more than one
12786 init-declarator, they shall all form declarations of
12787 variables. */
12788 if (auto_function_declaration == NULL_TREE)
12789 auto_function_declaration
12790 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
12791 else if (TREE_CODE (decl) == FUNCTION_DECL
12792 || auto_function_declaration != error_mark_node)
12794 error_at (decl_specifiers.locations[ds_type_spec],
12795 "non-variable %qD in declaration with more than one "
12796 "declarator with placeholder type",
12797 TREE_CODE (decl) == FUNCTION_DECL
12798 ? decl : auto_function_declaration);
12799 auto_function_declaration = error_mark_node;
12803 if (auto_result
12804 && (!processing_template_decl || !type_uses_auto (auto_result)))
12806 if (last_type
12807 && last_type != error_mark_node
12808 && !same_type_p (auto_result, last_type))
12810 /* If the list of declarators contains more than one declarator,
12811 the type of each declared variable is determined as described
12812 above. If the type deduced for the template parameter U is not
12813 the same in each deduction, the program is ill-formed. */
12814 error_at (decl_specifiers.locations[ds_type_spec],
12815 "inconsistent deduction for %qT: %qT and then %qT",
12816 decl_specifiers.type, last_type, auto_result);
12817 last_type = error_mark_node;
12819 else
12820 last_type = auto_result;
12823 /* Handle function definitions specially. */
12824 if (function_definition_p)
12826 /* If the next token is a `,', then we are probably
12827 processing something like:
12829 void f() {}, *p;
12831 which is erroneous. */
12832 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12834 cp_token *token = cp_lexer_peek_token (parser->lexer);
12835 error_at (token->location,
12836 "mixing"
12837 " declarations and function-definitions is forbidden");
12839 /* Otherwise, we're done with the list of declarators. */
12840 else
12842 pop_deferring_access_checks ();
12843 return;
12846 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
12847 *maybe_range_for_decl = decl;
12848 /* The next token should be either a `,' or a `;'. */
12849 token = cp_lexer_peek_token (parser->lexer);
12850 /* If it's a `,', there are more declarators to come. */
12851 if (token->type == CPP_COMMA)
12852 /* will be consumed next time around */;
12853 /* If it's a `;', we are done. */
12854 else if (token->type == CPP_SEMICOLON)
12855 break;
12856 else if (maybe_range_for_decl)
12858 if (declares_class_or_enum && token->type == CPP_COLON)
12859 permerror (decl_specifiers.locations[ds_type_spec],
12860 "types may not be defined in a for-range-declaration");
12861 break;
12863 /* Anything else is an error. */
12864 else
12866 /* If we have already issued an error message we don't need
12867 to issue another one. */
12868 if ((decl != error_mark_node
12869 && DECL_INITIAL (decl) != error_mark_node)
12870 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12871 cp_parser_error (parser, "expected %<,%> or %<;%>");
12872 /* Skip tokens until we reach the end of the statement. */
12873 cp_parser_skip_to_end_of_statement (parser);
12874 /* If the next token is now a `;', consume it. */
12875 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12876 cp_lexer_consume_token (parser->lexer);
12877 goto done;
12879 /* After the first time around, a function-definition is not
12880 allowed -- even if it was OK at first. For example:
12882 int i, f() {}
12884 is not valid. */
12885 function_definition_allowed_p = false;
12888 /* Issue an error message if no declarators are present, and the
12889 decl-specifier-seq does not itself declare a class or
12890 enumeration: [dcl.dcl]/3. */
12891 if (!saw_declarator)
12893 if (cp_parser_declares_only_class_p (parser))
12895 if (!declares_class_or_enum
12896 && decl_specifiers.type
12897 && OVERLOAD_TYPE_P (decl_specifiers.type))
12898 /* Ensure an error is issued anyway when finish_decltype_type,
12899 called via cp_parser_decl_specifier_seq, returns a class or
12900 an enumeration (c++/51786). */
12901 decl_specifiers.type = NULL_TREE;
12902 shadow_tag (&decl_specifiers);
12904 /* Perform any deferred access checks. */
12905 perform_deferred_access_checks (tf_warning_or_error);
12908 /* Consume the `;'. */
12909 finish:
12910 if (!maybe_range_for_decl)
12911 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12912 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12914 if (init_loc != UNKNOWN_LOCATION)
12915 error_at (init_loc, "initializer in range-based %<for%> loop");
12916 if (comma_loc != UNKNOWN_LOCATION)
12917 error_at (comma_loc,
12918 "multiple declarations in range-based %<for%> loop");
12921 done:
12922 pop_deferring_access_checks ();
12925 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
12926 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12927 initializer ; */
12929 static tree
12930 cp_parser_decomposition_declaration (cp_parser *parser,
12931 cp_decl_specifier_seq *decl_specifiers,
12932 tree *maybe_range_for_decl,
12933 location_t *init_loc)
12935 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
12936 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
12937 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
12939 /* Parse the identifier-list. */
12940 auto_vec<cp_expr, 10> v;
12941 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
12942 while (true)
12944 cp_expr e = cp_parser_identifier (parser);
12945 if (e.get_value () == error_mark_node)
12946 break;
12947 v.safe_push (e);
12948 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12949 break;
12950 cp_lexer_consume_token (parser->lexer);
12953 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
12954 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
12956 end_loc = UNKNOWN_LOCATION;
12957 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
12958 false);
12959 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
12960 cp_lexer_consume_token (parser->lexer);
12961 else
12963 cp_parser_skip_to_end_of_statement (parser);
12964 return error_mark_node;
12968 if (cxx_dialect < cxx1z)
12969 pedwarn (loc, 0, "decomposition declaration only available with "
12970 "-std=c++1z or -std=gnu++1z");
12972 tree pushed_scope;
12973 cp_declarator *declarator = make_declarator (cdk_decomp);
12974 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
12975 declarator->id_loc = loc;
12976 if (ref_qual != REF_QUAL_NONE)
12977 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
12978 ref_qual == REF_QUAL_RVALUE,
12979 NULL_TREE);
12980 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
12981 NULL_TREE, decl_specifiers->attributes,
12982 &pushed_scope);
12983 tree orig_decl = decl;
12985 unsigned int i;
12986 cp_expr e;
12987 cp_decl_specifier_seq decl_specs;
12988 clear_decl_specs (&decl_specs);
12989 decl_specs.type = make_auto ();
12990 tree prev = decl;
12991 FOR_EACH_VEC_ELT (v, i, e)
12993 if (i == 0)
12994 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
12995 else
12996 declarator->u.id.unqualified_name = e.get_value ();
12997 declarator->id_loc = e.get_location ();
12998 tree elt_pushed_scope;
12999 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13000 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13001 if (decl2 == error_mark_node)
13002 decl = error_mark_node;
13003 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13005 /* Ensure we've diagnosed redeclaration if we aren't creating
13006 a new VAR_DECL. */
13007 gcc_assert (errorcount);
13008 decl = error_mark_node;
13010 else
13011 prev = decl2;
13012 if (elt_pushed_scope)
13013 pop_scope (elt_pushed_scope);
13016 if (v.is_empty ())
13018 error_at (loc, "empty decomposition declaration");
13019 decl = error_mark_node;
13022 if (maybe_range_for_decl == NULL
13023 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13025 bool non_constant_p = false, is_direct_init = false;
13026 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13027 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13028 &non_constant_p);
13030 if (decl != error_mark_node)
13032 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13033 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13034 cp_finish_decomp (decl, prev, v.length ());
13037 else if (decl != error_mark_node)
13039 *maybe_range_for_decl = prev;
13040 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13041 the underlying DECL. */
13042 cp_finish_decomp (decl, prev, v.length ());
13045 if (pushed_scope)
13046 pop_scope (pushed_scope);
13048 if (decl == error_mark_node && DECL_P (orig_decl))
13050 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13051 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13054 return decl;
13057 /* Parse a decl-specifier-seq.
13059 decl-specifier-seq:
13060 decl-specifier-seq [opt] decl-specifier
13061 decl-specifier attribute-specifier-seq [opt] (C++11)
13063 decl-specifier:
13064 storage-class-specifier
13065 type-specifier
13066 function-specifier
13067 friend
13068 typedef
13070 GNU Extension:
13072 decl-specifier:
13073 attributes
13075 Concepts Extension:
13077 decl-specifier:
13078 concept
13080 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13082 The parser flags FLAGS is used to control type-specifier parsing.
13084 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13085 flags:
13087 1: one of the decl-specifiers is an elaborated-type-specifier
13088 (i.e., a type declaration)
13089 2: one of the decl-specifiers is an enum-specifier or a
13090 class-specifier (i.e., a type definition)
13094 static void
13095 cp_parser_decl_specifier_seq (cp_parser* parser,
13096 cp_parser_flags flags,
13097 cp_decl_specifier_seq *decl_specs,
13098 int* declares_class_or_enum)
13100 bool constructor_possible_p = !parser->in_declarator_p;
13101 bool found_decl_spec = false;
13102 cp_token *start_token = NULL;
13103 cp_decl_spec ds;
13105 /* Clear DECL_SPECS. */
13106 clear_decl_specs (decl_specs);
13108 /* Assume no class or enumeration type is declared. */
13109 *declares_class_or_enum = 0;
13111 /* Keep reading specifiers until there are no more to read. */
13112 while (true)
13114 bool constructor_p;
13115 cp_token *token;
13116 ds = ds_last;
13118 /* Peek at the next token. */
13119 token = cp_lexer_peek_token (parser->lexer);
13121 /* Save the first token of the decl spec list for error
13122 reporting. */
13123 if (!start_token)
13124 start_token = token;
13125 /* Handle attributes. */
13126 if (cp_next_tokens_can_be_attribute_p (parser))
13128 /* Parse the attributes. */
13129 tree attrs = cp_parser_attributes_opt (parser);
13131 /* In a sequence of declaration specifiers, c++11 attributes
13132 appertain to the type that precede them. In that case
13133 [dcl.spec]/1 says:
13135 The attribute-specifier-seq affects the type only for
13136 the declaration it appears in, not other declarations
13137 involving the same type.
13139 But for now let's force the user to position the
13140 attribute either at the beginning of the declaration or
13141 after the declarator-id, which would clearly mean that it
13142 applies to the declarator. */
13143 if (cxx11_attribute_p (attrs))
13145 if (!found_decl_spec)
13146 /* The c++11 attribute is at the beginning of the
13147 declaration. It appertains to the entity being
13148 declared. */;
13149 else
13151 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13153 /* This is an attribute following a
13154 class-specifier. */
13155 if (decl_specs->type_definition_p)
13156 warn_misplaced_attr_for_class_type (token->location,
13157 decl_specs->type);
13158 attrs = NULL_TREE;
13160 else
13162 decl_specs->std_attributes
13163 = chainon (decl_specs->std_attributes,
13164 attrs);
13165 if (decl_specs->locations[ds_std_attribute] == 0)
13166 decl_specs->locations[ds_std_attribute] = token->location;
13168 continue;
13172 decl_specs->attributes
13173 = chainon (decl_specs->attributes,
13174 attrs);
13175 if (decl_specs->locations[ds_attribute] == 0)
13176 decl_specs->locations[ds_attribute] = token->location;
13177 continue;
13179 /* Assume we will find a decl-specifier keyword. */
13180 found_decl_spec = true;
13181 /* If the next token is an appropriate keyword, we can simply
13182 add it to the list. */
13183 switch (token->keyword)
13185 /* decl-specifier:
13186 friend
13187 constexpr */
13188 case RID_FRIEND:
13189 if (!at_class_scope_p ())
13191 error_at (token->location, "%<friend%> used outside of class");
13192 cp_lexer_purge_token (parser->lexer);
13194 else
13196 ds = ds_friend;
13197 /* Consume the token. */
13198 cp_lexer_consume_token (parser->lexer);
13200 break;
13202 case RID_CONSTEXPR:
13203 ds = ds_constexpr;
13204 cp_lexer_consume_token (parser->lexer);
13205 break;
13207 case RID_CONCEPT:
13208 ds = ds_concept;
13209 cp_lexer_consume_token (parser->lexer);
13210 break;
13212 /* function-specifier:
13213 inline
13214 virtual
13215 explicit */
13216 case RID_INLINE:
13217 case RID_VIRTUAL:
13218 case RID_EXPLICIT:
13219 cp_parser_function_specifier_opt (parser, decl_specs);
13220 break;
13222 /* decl-specifier:
13223 typedef */
13224 case RID_TYPEDEF:
13225 ds = ds_typedef;
13226 /* Consume the token. */
13227 cp_lexer_consume_token (parser->lexer);
13228 /* A constructor declarator cannot appear in a typedef. */
13229 constructor_possible_p = false;
13230 /* The "typedef" keyword can only occur in a declaration; we
13231 may as well commit at this point. */
13232 cp_parser_commit_to_tentative_parse (parser);
13234 if (decl_specs->storage_class != sc_none)
13235 decl_specs->conflicting_specifiers_p = true;
13236 break;
13238 /* storage-class-specifier:
13239 auto
13240 register
13241 static
13242 extern
13243 mutable
13245 GNU Extension:
13246 thread */
13247 case RID_AUTO:
13248 if (cxx_dialect == cxx98)
13250 /* Consume the token. */
13251 cp_lexer_consume_token (parser->lexer);
13253 /* Complain about `auto' as a storage specifier, if
13254 we're complaining about C++0x compatibility. */
13255 warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
13256 " changes meaning in C++11; please remove it");
13258 /* Set the storage class anyway. */
13259 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13260 token);
13262 else
13263 /* C++0x auto type-specifier. */
13264 found_decl_spec = false;
13265 break;
13267 case RID_REGISTER:
13268 case RID_STATIC:
13269 case RID_EXTERN:
13270 case RID_MUTABLE:
13271 /* Consume the token. */
13272 cp_lexer_consume_token (parser->lexer);
13273 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13274 token);
13275 break;
13276 case RID_THREAD:
13277 /* Consume the token. */
13278 ds = ds_thread;
13279 cp_lexer_consume_token (parser->lexer);
13280 break;
13282 default:
13283 /* We did not yet find a decl-specifier yet. */
13284 found_decl_spec = false;
13285 break;
13288 if (found_decl_spec
13289 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13290 && token->keyword != RID_CONSTEXPR)
13291 error ("decl-specifier invalid in condition");
13293 if (found_decl_spec
13294 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13295 && token->keyword != RID_MUTABLE
13296 && token->keyword != RID_CONSTEXPR)
13297 error_at (token->location, "%qD invalid in lambda",
13298 ridpointers[token->keyword]);
13300 if (ds != ds_last)
13301 set_and_check_decl_spec_loc (decl_specs, ds, token);
13303 /* Constructors are a special case. The `S' in `S()' is not a
13304 decl-specifier; it is the beginning of the declarator. */
13305 constructor_p
13306 = (!found_decl_spec
13307 && constructor_possible_p
13308 && (cp_parser_constructor_declarator_p
13309 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13311 /* If we don't have a DECL_SPEC yet, then we must be looking at
13312 a type-specifier. */
13313 if (!found_decl_spec && !constructor_p)
13315 int decl_spec_declares_class_or_enum;
13316 bool is_cv_qualifier;
13317 tree type_spec;
13319 type_spec
13320 = cp_parser_type_specifier (parser, flags,
13321 decl_specs,
13322 /*is_declaration=*/true,
13323 &decl_spec_declares_class_or_enum,
13324 &is_cv_qualifier);
13325 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13327 /* If this type-specifier referenced a user-defined type
13328 (a typedef, class-name, etc.), then we can't allow any
13329 more such type-specifiers henceforth.
13331 [dcl.spec]
13333 The longest sequence of decl-specifiers that could
13334 possibly be a type name is taken as the
13335 decl-specifier-seq of a declaration. The sequence shall
13336 be self-consistent as described below.
13338 [dcl.type]
13340 As a general rule, at most one type-specifier is allowed
13341 in the complete decl-specifier-seq of a declaration. The
13342 only exceptions are the following:
13344 -- const or volatile can be combined with any other
13345 type-specifier.
13347 -- signed or unsigned can be combined with char, long,
13348 short, or int.
13350 -- ..
13352 Example:
13354 typedef char* Pc;
13355 void g (const int Pc);
13357 Here, Pc is *not* part of the decl-specifier seq; it's
13358 the declarator. Therefore, once we see a type-specifier
13359 (other than a cv-qualifier), we forbid any additional
13360 user-defined types. We *do* still allow things like `int
13361 int' to be considered a decl-specifier-seq, and issue the
13362 error message later. */
13363 if (type_spec && !is_cv_qualifier)
13364 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13365 /* A constructor declarator cannot follow a type-specifier. */
13366 if (type_spec)
13368 constructor_possible_p = false;
13369 found_decl_spec = true;
13370 if (!is_cv_qualifier)
13371 decl_specs->any_type_specifiers_p = true;
13375 /* If we still do not have a DECL_SPEC, then there are no more
13376 decl-specifiers. */
13377 if (!found_decl_spec)
13378 break;
13380 decl_specs->any_specifiers_p = true;
13381 /* After we see one decl-specifier, further decl-specifiers are
13382 always optional. */
13383 flags |= CP_PARSER_FLAGS_OPTIONAL;
13386 /* Don't allow a friend specifier with a class definition. */
13387 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13388 && (*declares_class_or_enum & 2))
13389 error_at (decl_specs->locations[ds_friend],
13390 "class definition may not be declared a friend");
13393 /* Parse an (optional) storage-class-specifier.
13395 storage-class-specifier:
13396 auto
13397 register
13398 static
13399 extern
13400 mutable
13402 GNU Extension:
13404 storage-class-specifier:
13405 thread
13407 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13409 static tree
13410 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13412 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13414 case RID_AUTO:
13415 if (cxx_dialect != cxx98)
13416 return NULL_TREE;
13417 /* Fall through for C++98. */
13418 gcc_fallthrough ();
13420 case RID_REGISTER:
13421 case RID_STATIC:
13422 case RID_EXTERN:
13423 case RID_MUTABLE:
13424 case RID_THREAD:
13425 /* Consume the token. */
13426 return cp_lexer_consume_token (parser->lexer)->u.value;
13428 default:
13429 return NULL_TREE;
13433 /* Parse an (optional) function-specifier.
13435 function-specifier:
13436 inline
13437 virtual
13438 explicit
13440 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13441 Updates DECL_SPECS, if it is non-NULL. */
13443 static tree
13444 cp_parser_function_specifier_opt (cp_parser* parser,
13445 cp_decl_specifier_seq *decl_specs)
13447 cp_token *token = cp_lexer_peek_token (parser->lexer);
13448 switch (token->keyword)
13450 case RID_INLINE:
13451 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13452 break;
13454 case RID_VIRTUAL:
13455 /* 14.5.2.3 [temp.mem]
13457 A member function template shall not be virtual. */
13458 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13459 && current_class_type)
13460 error_at (token->location, "templates may not be %<virtual%>");
13461 else
13462 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13463 break;
13465 case RID_EXPLICIT:
13466 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13467 break;
13469 default:
13470 return NULL_TREE;
13473 /* Consume the token. */
13474 return cp_lexer_consume_token (parser->lexer)->u.value;
13477 /* Parse a linkage-specification.
13479 linkage-specification:
13480 extern string-literal { declaration-seq [opt] }
13481 extern string-literal declaration */
13483 static void
13484 cp_parser_linkage_specification (cp_parser* parser)
13486 tree linkage;
13488 /* Look for the `extern' keyword. */
13489 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13491 /* Look for the string-literal. */
13492 linkage = cp_parser_string_literal (parser, false, false);
13494 /* Transform the literal into an identifier. If the literal is a
13495 wide-character string, or contains embedded NULs, then we can't
13496 handle it as the user wants. */
13497 if (strlen (TREE_STRING_POINTER (linkage))
13498 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13500 cp_parser_error (parser, "invalid linkage-specification");
13501 /* Assume C++ linkage. */
13502 linkage = lang_name_cplusplus;
13504 else
13505 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13507 /* We're now using the new linkage. */
13508 push_lang_context (linkage);
13510 /* If the next token is a `{', then we're using the first
13511 production. */
13512 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13514 cp_ensure_no_omp_declare_simd (parser);
13515 cp_ensure_no_oacc_routine (parser);
13517 /* Consume the `{' token. */
13518 cp_lexer_consume_token (parser->lexer);
13519 /* Parse the declarations. */
13520 cp_parser_declaration_seq_opt (parser);
13521 /* Look for the closing `}'. */
13522 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13524 /* Otherwise, there's just one declaration. */
13525 else
13527 bool saved_in_unbraced_linkage_specification_p;
13529 saved_in_unbraced_linkage_specification_p
13530 = parser->in_unbraced_linkage_specification_p;
13531 parser->in_unbraced_linkage_specification_p = true;
13532 cp_parser_declaration (parser);
13533 parser->in_unbraced_linkage_specification_p
13534 = saved_in_unbraced_linkage_specification_p;
13537 /* We're done with the linkage-specification. */
13538 pop_lang_context ();
13541 /* Parse a static_assert-declaration.
13543 static_assert-declaration:
13544 static_assert ( constant-expression , string-literal ) ;
13545 static_assert ( constant-expression ) ; (C++1Z)
13547 If MEMBER_P, this static_assert is a class member. */
13549 static void
13550 cp_parser_static_assert(cp_parser *parser, bool member_p)
13552 tree condition;
13553 tree message;
13554 cp_token *token;
13555 location_t saved_loc;
13556 bool dummy;
13558 /* Peek at the `static_assert' token so we can keep track of exactly
13559 where the static assertion started. */
13560 token = cp_lexer_peek_token (parser->lexer);
13561 saved_loc = token->location;
13563 /* Look for the `static_assert' keyword. */
13564 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13565 RT_STATIC_ASSERT))
13566 return;
13568 /* We know we are in a static assertion; commit to any tentative
13569 parse. */
13570 if (cp_parser_parsing_tentatively (parser))
13571 cp_parser_commit_to_tentative_parse (parser);
13573 /* Parse the `(' starting the static assertion condition. */
13574 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
13576 /* Parse the constant-expression. Allow a non-constant expression
13577 here in order to give better diagnostics in finish_static_assert. */
13578 condition =
13579 cp_parser_constant_expression (parser,
13580 /*allow_non_constant_p=*/true,
13581 /*non_constant_p=*/&dummy);
13583 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13585 if (cxx_dialect < cxx1z)
13586 pedwarn (input_location, OPT_Wpedantic,
13587 "static_assert without a message "
13588 "only available with -std=c++1z or -std=gnu++1z");
13589 /* Eat the ')' */
13590 cp_lexer_consume_token (parser->lexer);
13591 message = build_string (1, "");
13592 TREE_TYPE (message) = char_array_type_node;
13593 fix_string_type (message);
13595 else
13597 /* Parse the separating `,'. */
13598 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13600 /* Parse the string-literal message. */
13601 message = cp_parser_string_literal (parser,
13602 /*translate=*/false,
13603 /*wide_ok=*/true);
13605 /* A `)' completes the static assertion. */
13606 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13607 cp_parser_skip_to_closing_parenthesis (parser,
13608 /*recovering=*/true,
13609 /*or_comma=*/false,
13610 /*consume_paren=*/true);
13613 /* A semicolon terminates the declaration. */
13614 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13616 /* Complete the static assertion, which may mean either processing
13617 the static assert now or saving it for template instantiation. */
13618 finish_static_assert (condition, message, saved_loc, member_p);
13621 /* Parse the expression in decltype ( expression ). */
13623 static tree
13624 cp_parser_decltype_expr (cp_parser *parser,
13625 bool &id_expression_or_member_access_p)
13627 cp_token *id_expr_start_token;
13628 tree expr;
13630 /* Since we're going to preserve any side-effects from this parse, set up a
13631 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13632 in the expression. */
13633 tentative_firewall firewall (parser);
13635 /* First, try parsing an id-expression. */
13636 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13637 cp_parser_parse_tentatively (parser);
13638 expr = cp_parser_id_expression (parser,
13639 /*template_keyword_p=*/false,
13640 /*check_dependency_p=*/true,
13641 /*template_p=*/NULL,
13642 /*declarator_p=*/false,
13643 /*optional_p=*/false);
13645 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13647 bool non_integral_constant_expression_p = false;
13648 tree id_expression = expr;
13649 cp_id_kind idk;
13650 const char *error_msg;
13652 if (identifier_p (expr))
13653 /* Lookup the name we got back from the id-expression. */
13654 expr = cp_parser_lookup_name_simple (parser, expr,
13655 id_expr_start_token->location);
13657 if (expr
13658 && expr != error_mark_node
13659 && TREE_CODE (expr) != TYPE_DECL
13660 && (TREE_CODE (expr) != BIT_NOT_EXPR
13661 || !TYPE_P (TREE_OPERAND (expr, 0)))
13662 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13664 /* Complete lookup of the id-expression. */
13665 expr = (finish_id_expression
13666 (id_expression, expr, parser->scope, &idk,
13667 /*integral_constant_expression_p=*/false,
13668 /*allow_non_integral_constant_expression_p=*/true,
13669 &non_integral_constant_expression_p,
13670 /*template_p=*/false,
13671 /*done=*/true,
13672 /*address_p=*/false,
13673 /*template_arg_p=*/false,
13674 &error_msg,
13675 id_expr_start_token->location));
13677 if (expr == error_mark_node)
13678 /* We found an id-expression, but it was something that we
13679 should not have found. This is an error, not something
13680 we can recover from, so note that we found an
13681 id-expression and we'll recover as gracefully as
13682 possible. */
13683 id_expression_or_member_access_p = true;
13686 if (expr
13687 && expr != error_mark_node
13688 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13689 /* We have an id-expression. */
13690 id_expression_or_member_access_p = true;
13693 if (!id_expression_or_member_access_p)
13695 /* Abort the id-expression parse. */
13696 cp_parser_abort_tentative_parse (parser);
13698 /* Parsing tentatively, again. */
13699 cp_parser_parse_tentatively (parser);
13701 /* Parse a class member access. */
13702 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13703 /*cast_p=*/false, /*decltype*/true,
13704 /*member_access_only_p=*/true, NULL);
13706 if (expr
13707 && expr != error_mark_node
13708 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13709 /* We have an id-expression. */
13710 id_expression_or_member_access_p = true;
13713 if (id_expression_or_member_access_p)
13714 /* We have parsed the complete id-expression or member access. */
13715 cp_parser_parse_definitely (parser);
13716 else
13718 /* Abort our attempt to parse an id-expression or member access
13719 expression. */
13720 cp_parser_abort_tentative_parse (parser);
13722 /* Parse a full expression. */
13723 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
13724 /*decltype_p=*/true);
13727 return expr;
13730 /* Parse a `decltype' type. Returns the type.
13732 simple-type-specifier:
13733 decltype ( expression )
13734 C++14 proposal:
13735 decltype ( auto ) */
13737 static tree
13738 cp_parser_decltype (cp_parser *parser)
13740 tree expr;
13741 bool id_expression_or_member_access_p = false;
13742 const char *saved_message;
13743 bool saved_integral_constant_expression_p;
13744 bool saved_non_integral_constant_expression_p;
13745 bool saved_greater_than_is_operator_p;
13746 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13748 if (start_token->type == CPP_DECLTYPE)
13750 /* Already parsed. */
13751 cp_lexer_consume_token (parser->lexer);
13752 return saved_checks_value (start_token->u.tree_check_value);
13755 /* Look for the `decltype' token. */
13756 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
13757 return error_mark_node;
13759 /* Parse the opening `('. */
13760 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13761 return error_mark_node;
13763 /* decltype (auto) */
13764 if (cxx_dialect >= cxx14
13765 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
13767 cp_lexer_consume_token (parser->lexer);
13768 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13769 return error_mark_node;
13770 expr = make_decltype_auto ();
13771 AUTO_IS_DECLTYPE (expr) = true;
13772 goto rewrite;
13775 /* Types cannot be defined in a `decltype' expression. Save away the
13776 old message. */
13777 saved_message = parser->type_definition_forbidden_message;
13779 /* And create the new one. */
13780 parser->type_definition_forbidden_message
13781 = G_("types may not be defined in %<decltype%> expressions");
13783 /* The restrictions on constant-expressions do not apply inside
13784 decltype expressions. */
13785 saved_integral_constant_expression_p
13786 = parser->integral_constant_expression_p;
13787 saved_non_integral_constant_expression_p
13788 = parser->non_integral_constant_expression_p;
13789 parser->integral_constant_expression_p = false;
13791 /* Within a parenthesized expression, a `>' token is always
13792 the greater-than operator. */
13793 saved_greater_than_is_operator_p
13794 = parser->greater_than_is_operator_p;
13795 parser->greater_than_is_operator_p = true;
13797 /* Do not actually evaluate the expression. */
13798 ++cp_unevaluated_operand;
13800 /* Do not warn about problems with the expression. */
13801 ++c_inhibit_evaluation_warnings;
13803 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
13805 /* Go back to evaluating expressions. */
13806 --cp_unevaluated_operand;
13807 --c_inhibit_evaluation_warnings;
13809 /* The `>' token might be the end of a template-id or
13810 template-parameter-list now. */
13811 parser->greater_than_is_operator_p
13812 = saved_greater_than_is_operator_p;
13814 /* Restore the old message and the integral constant expression
13815 flags. */
13816 parser->type_definition_forbidden_message = saved_message;
13817 parser->integral_constant_expression_p
13818 = saved_integral_constant_expression_p;
13819 parser->non_integral_constant_expression_p
13820 = saved_non_integral_constant_expression_p;
13822 /* Parse to the closing `)'. */
13823 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13825 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13826 /*consume_paren=*/true);
13827 return error_mark_node;
13830 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
13831 tf_warning_or_error);
13833 rewrite:
13834 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
13835 it again. */
13836 start_token->type = CPP_DECLTYPE;
13837 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13838 start_token->u.tree_check_value->value = expr;
13839 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
13840 start_token->keyword = RID_MAX;
13841 cp_lexer_purge_tokens_after (parser->lexer, start_token);
13843 return expr;
13846 /* Special member functions [gram.special] */
13848 /* Parse a conversion-function-id.
13850 conversion-function-id:
13851 operator conversion-type-id
13853 Returns an IDENTIFIER_NODE representing the operator. */
13855 static tree
13856 cp_parser_conversion_function_id (cp_parser* parser)
13858 tree type;
13859 tree saved_scope;
13860 tree saved_qualifying_scope;
13861 tree saved_object_scope;
13862 tree pushed_scope = NULL_TREE;
13864 /* Look for the `operator' token. */
13865 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13866 return error_mark_node;
13867 /* When we parse the conversion-type-id, the current scope will be
13868 reset. However, we need that information in able to look up the
13869 conversion function later, so we save it here. */
13870 saved_scope = parser->scope;
13871 saved_qualifying_scope = parser->qualifying_scope;
13872 saved_object_scope = parser->object_scope;
13873 /* We must enter the scope of the class so that the names of
13874 entities declared within the class are available in the
13875 conversion-type-id. For example, consider:
13877 struct S {
13878 typedef int I;
13879 operator I();
13882 S::operator I() { ... }
13884 In order to see that `I' is a type-name in the definition, we
13885 must be in the scope of `S'. */
13886 if (saved_scope)
13887 pushed_scope = push_scope (saved_scope);
13888 /* Parse the conversion-type-id. */
13889 type = cp_parser_conversion_type_id (parser);
13890 /* Leave the scope of the class, if any. */
13891 if (pushed_scope)
13892 pop_scope (pushed_scope);
13893 /* Restore the saved scope. */
13894 parser->scope = saved_scope;
13895 parser->qualifying_scope = saved_qualifying_scope;
13896 parser->object_scope = saved_object_scope;
13897 /* If the TYPE is invalid, indicate failure. */
13898 if (type == error_mark_node)
13899 return error_mark_node;
13900 return mangle_conv_op_name_for_type (type);
13903 /* Parse a conversion-type-id:
13905 conversion-type-id:
13906 type-specifier-seq conversion-declarator [opt]
13908 Returns the TYPE specified. */
13910 static tree
13911 cp_parser_conversion_type_id (cp_parser* parser)
13913 tree attributes;
13914 cp_decl_specifier_seq type_specifiers;
13915 cp_declarator *declarator;
13916 tree type_specified;
13917 const char *saved_message;
13919 /* Parse the attributes. */
13920 attributes = cp_parser_attributes_opt (parser);
13922 saved_message = parser->type_definition_forbidden_message;
13923 parser->type_definition_forbidden_message
13924 = G_("types may not be defined in a conversion-type-id");
13926 /* Parse the type-specifiers. */
13927 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13928 /*is_trailing_return=*/false,
13929 &type_specifiers);
13931 parser->type_definition_forbidden_message = saved_message;
13933 /* If that didn't work, stop. */
13934 if (type_specifiers.type == error_mark_node)
13935 return error_mark_node;
13936 /* Parse the conversion-declarator. */
13937 declarator = cp_parser_conversion_declarator_opt (parser);
13939 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
13940 /*initialized=*/0, &attributes);
13941 if (attributes)
13942 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
13944 /* Don't give this error when parsing tentatively. This happens to
13945 work because we always parse this definitively once. */
13946 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
13947 && type_uses_auto (type_specified))
13949 if (cxx_dialect < cxx14)
13951 error ("invalid use of %<auto%> in conversion operator");
13952 return error_mark_node;
13954 else if (template_parm_scope_p ())
13955 warning (0, "use of %<auto%> in member template "
13956 "conversion operator can never be deduced");
13959 return type_specified;
13962 /* Parse an (optional) conversion-declarator.
13964 conversion-declarator:
13965 ptr-operator conversion-declarator [opt]
13969 static cp_declarator *
13970 cp_parser_conversion_declarator_opt (cp_parser* parser)
13972 enum tree_code code;
13973 tree class_type, std_attributes = NULL_TREE;
13974 cp_cv_quals cv_quals;
13976 /* We don't know if there's a ptr-operator next, or not. */
13977 cp_parser_parse_tentatively (parser);
13978 /* Try the ptr-operator. */
13979 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
13980 &std_attributes);
13981 /* If it worked, look for more conversion-declarators. */
13982 if (cp_parser_parse_definitely (parser))
13984 cp_declarator *declarator;
13986 /* Parse another optional declarator. */
13987 declarator = cp_parser_conversion_declarator_opt (parser);
13989 declarator = cp_parser_make_indirect_declarator
13990 (code, class_type, cv_quals, declarator, std_attributes);
13992 return declarator;
13995 return NULL;
13998 /* Parse an (optional) ctor-initializer.
14000 ctor-initializer:
14001 : mem-initializer-list
14003 Returns TRUE iff the ctor-initializer was actually present. */
14005 static bool
14006 cp_parser_ctor_initializer_opt (cp_parser* parser)
14008 /* If the next token is not a `:', then there is no
14009 ctor-initializer. */
14010 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14012 /* Do default initialization of any bases and members. */
14013 if (DECL_CONSTRUCTOR_P (current_function_decl))
14014 finish_mem_initializers (NULL_TREE);
14016 return false;
14019 /* Consume the `:' token. */
14020 cp_lexer_consume_token (parser->lexer);
14021 /* And the mem-initializer-list. */
14022 cp_parser_mem_initializer_list (parser);
14024 return true;
14027 /* Parse a mem-initializer-list.
14029 mem-initializer-list:
14030 mem-initializer ... [opt]
14031 mem-initializer ... [opt] , mem-initializer-list */
14033 static void
14034 cp_parser_mem_initializer_list (cp_parser* parser)
14036 tree mem_initializer_list = NULL_TREE;
14037 tree target_ctor = error_mark_node;
14038 cp_token *token = cp_lexer_peek_token (parser->lexer);
14040 /* Let the semantic analysis code know that we are starting the
14041 mem-initializer-list. */
14042 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14043 error_at (token->location,
14044 "only constructors take member initializers");
14046 /* Loop through the list. */
14047 while (true)
14049 tree mem_initializer;
14051 token = cp_lexer_peek_token (parser->lexer);
14052 /* Parse the mem-initializer. */
14053 mem_initializer = cp_parser_mem_initializer (parser);
14054 /* If the next token is a `...', we're expanding member initializers. */
14055 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14057 /* Consume the `...'. */
14058 cp_lexer_consume_token (parser->lexer);
14060 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14061 can be expanded but members cannot. */
14062 if (mem_initializer != error_mark_node
14063 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14065 error_at (token->location,
14066 "cannot expand initializer for member %<%D%>",
14067 TREE_PURPOSE (mem_initializer));
14068 mem_initializer = error_mark_node;
14071 /* Construct the pack expansion type. */
14072 if (mem_initializer != error_mark_node)
14073 mem_initializer = make_pack_expansion (mem_initializer);
14075 if (target_ctor != error_mark_node
14076 && mem_initializer != error_mark_node)
14078 error ("mem-initializer for %qD follows constructor delegation",
14079 TREE_PURPOSE (mem_initializer));
14080 mem_initializer = error_mark_node;
14082 /* Look for a target constructor. */
14083 if (mem_initializer != error_mark_node
14084 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14085 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14087 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14088 if (mem_initializer_list)
14090 error ("constructor delegation follows mem-initializer for %qD",
14091 TREE_PURPOSE (mem_initializer_list));
14092 mem_initializer = error_mark_node;
14094 target_ctor = mem_initializer;
14096 /* Add it to the list, unless it was erroneous. */
14097 if (mem_initializer != error_mark_node)
14099 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14100 mem_initializer_list = mem_initializer;
14102 /* If the next token is not a `,', we're done. */
14103 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14104 break;
14105 /* Consume the `,' token. */
14106 cp_lexer_consume_token (parser->lexer);
14109 /* Perform semantic analysis. */
14110 if (DECL_CONSTRUCTOR_P (current_function_decl))
14111 finish_mem_initializers (mem_initializer_list);
14114 /* Parse a mem-initializer.
14116 mem-initializer:
14117 mem-initializer-id ( expression-list [opt] )
14118 mem-initializer-id braced-init-list
14120 GNU extension:
14122 mem-initializer:
14123 ( expression-list [opt] )
14125 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14126 class) or FIELD_DECL (for a non-static data member) to initialize;
14127 the TREE_VALUE is the expression-list. An empty initialization
14128 list is represented by void_list_node. */
14130 static tree
14131 cp_parser_mem_initializer (cp_parser* parser)
14133 tree mem_initializer_id;
14134 tree expression_list;
14135 tree member;
14136 cp_token *token = cp_lexer_peek_token (parser->lexer);
14138 /* Find out what is being initialized. */
14139 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14141 permerror (token->location,
14142 "anachronistic old-style base class initializer");
14143 mem_initializer_id = NULL_TREE;
14145 else
14147 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14148 if (mem_initializer_id == error_mark_node)
14149 return mem_initializer_id;
14151 member = expand_member_init (mem_initializer_id);
14152 if (member && !DECL_P (member))
14153 in_base_initializer = 1;
14155 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14157 bool expr_non_constant_p;
14158 cp_lexer_set_source_position (parser->lexer);
14159 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14160 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14161 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14162 expression_list = build_tree_list (NULL_TREE, expression_list);
14164 else
14166 vec<tree, va_gc> *vec;
14167 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14168 /*cast_p=*/false,
14169 /*allow_expansion_p=*/true,
14170 /*non_constant_p=*/NULL);
14171 if (vec == NULL)
14172 return error_mark_node;
14173 expression_list = build_tree_list_vec (vec);
14174 release_tree_vector (vec);
14177 if (expression_list == error_mark_node)
14178 return error_mark_node;
14179 if (!expression_list)
14180 expression_list = void_type_node;
14182 in_base_initializer = 0;
14184 return member ? build_tree_list (member, expression_list) : error_mark_node;
14187 /* Parse a mem-initializer-id.
14189 mem-initializer-id:
14190 :: [opt] nested-name-specifier [opt] class-name
14191 decltype-specifier (C++11)
14192 identifier
14194 Returns a TYPE indicating the class to be initialized for the first
14195 production (and the second in C++11). Returns an IDENTIFIER_NODE
14196 indicating the data member to be initialized for the last production. */
14198 static tree
14199 cp_parser_mem_initializer_id (cp_parser* parser)
14201 bool global_scope_p;
14202 bool nested_name_specifier_p;
14203 bool template_p = false;
14204 tree id;
14206 cp_token *token = cp_lexer_peek_token (parser->lexer);
14208 /* `typename' is not allowed in this context ([temp.res]). */
14209 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14211 error_at (token->location,
14212 "keyword %<typename%> not allowed in this context (a qualified "
14213 "member initializer is implicitly a type)");
14214 cp_lexer_consume_token (parser->lexer);
14216 /* Look for the optional `::' operator. */
14217 global_scope_p
14218 = (cp_parser_global_scope_opt (parser,
14219 /*current_scope_valid_p=*/false)
14220 != NULL_TREE);
14221 /* Look for the optional nested-name-specifier. The simplest way to
14222 implement:
14224 [temp.res]
14226 The keyword `typename' is not permitted in a base-specifier or
14227 mem-initializer; in these contexts a qualified name that
14228 depends on a template-parameter is implicitly assumed to be a
14229 type name.
14231 is to assume that we have seen the `typename' keyword at this
14232 point. */
14233 nested_name_specifier_p
14234 = (cp_parser_nested_name_specifier_opt (parser,
14235 /*typename_keyword_p=*/true,
14236 /*check_dependency_p=*/true,
14237 /*type_p=*/true,
14238 /*is_declaration=*/true)
14239 != NULL_TREE);
14240 if (nested_name_specifier_p)
14241 template_p = cp_parser_optional_template_keyword (parser);
14242 /* If there is a `::' operator or a nested-name-specifier, then we
14243 are definitely looking for a class-name. */
14244 if (global_scope_p || nested_name_specifier_p)
14245 return cp_parser_class_name (parser,
14246 /*typename_keyword_p=*/true,
14247 /*template_keyword_p=*/template_p,
14248 typename_type,
14249 /*check_dependency_p=*/true,
14250 /*class_head_p=*/false,
14251 /*is_declaration=*/true);
14252 /* Otherwise, we could also be looking for an ordinary identifier. */
14253 cp_parser_parse_tentatively (parser);
14254 if (cp_lexer_next_token_is_decltype (parser->lexer))
14255 /* Try a decltype-specifier. */
14256 id = cp_parser_decltype (parser);
14257 else
14258 /* Otherwise, try a class-name. */
14259 id = cp_parser_class_name (parser,
14260 /*typename_keyword_p=*/true,
14261 /*template_keyword_p=*/false,
14262 none_type,
14263 /*check_dependency_p=*/true,
14264 /*class_head_p=*/false,
14265 /*is_declaration=*/true);
14266 /* If we found one, we're done. */
14267 if (cp_parser_parse_definitely (parser))
14268 return id;
14269 /* Otherwise, look for an ordinary identifier. */
14270 return cp_parser_identifier (parser);
14273 /* Overloading [gram.over] */
14275 /* Parse an operator-function-id.
14277 operator-function-id:
14278 operator operator
14280 Returns an IDENTIFIER_NODE for the operator which is a
14281 human-readable spelling of the identifier, e.g., `operator +'. */
14283 static cp_expr
14284 cp_parser_operator_function_id (cp_parser* parser)
14286 /* Look for the `operator' keyword. */
14287 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14288 return error_mark_node;
14289 /* And then the name of the operator itself. */
14290 return cp_parser_operator (parser);
14293 /* Return an identifier node for a user-defined literal operator.
14294 The suffix identifier is chained to the operator name identifier. */
14296 static tree
14297 cp_literal_operator_id (const char* name)
14299 tree identifier;
14300 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14301 + strlen (name) + 10);
14302 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14303 identifier = get_identifier (buffer);
14305 return identifier;
14308 /* Parse an operator.
14310 operator:
14311 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14312 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14313 || ++ -- , ->* -> () []
14315 GNU Extensions:
14317 operator:
14318 <? >? <?= >?=
14320 Returns an IDENTIFIER_NODE for the operator which is a
14321 human-readable spelling of the identifier, e.g., `operator +'. */
14323 static cp_expr
14324 cp_parser_operator (cp_parser* parser)
14326 tree id = NULL_TREE;
14327 cp_token *token;
14328 bool utf8 = false;
14330 /* Peek at the next token. */
14331 token = cp_lexer_peek_token (parser->lexer);
14333 location_t start_loc = token->location;
14335 /* Figure out which operator we have. */
14336 switch (token->type)
14338 case CPP_KEYWORD:
14340 enum tree_code op;
14342 /* The keyword should be either `new' or `delete'. */
14343 if (token->keyword == RID_NEW)
14344 op = NEW_EXPR;
14345 else if (token->keyword == RID_DELETE)
14346 op = DELETE_EXPR;
14347 else
14348 break;
14350 /* Consume the `new' or `delete' token. */
14351 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14353 /* Peek at the next token. */
14354 token = cp_lexer_peek_token (parser->lexer);
14355 /* If it's a `[' token then this is the array variant of the
14356 operator. */
14357 if (token->type == CPP_OPEN_SQUARE)
14359 /* Consume the `[' token. */
14360 cp_lexer_consume_token (parser->lexer);
14361 /* Look for the `]' token. */
14362 if (cp_token *close_token
14363 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14364 end_loc = close_token->location;
14365 id = ansi_opname (op == NEW_EXPR
14366 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
14368 /* Otherwise, we have the non-array variant. */
14369 else
14370 id = ansi_opname (op);
14372 location_t loc = make_location (start_loc, start_loc, end_loc);
14374 return cp_expr (id, loc);
14377 case CPP_PLUS:
14378 id = ansi_opname (PLUS_EXPR);
14379 break;
14381 case CPP_MINUS:
14382 id = ansi_opname (MINUS_EXPR);
14383 break;
14385 case CPP_MULT:
14386 id = ansi_opname (MULT_EXPR);
14387 break;
14389 case CPP_DIV:
14390 id = ansi_opname (TRUNC_DIV_EXPR);
14391 break;
14393 case CPP_MOD:
14394 id = ansi_opname (TRUNC_MOD_EXPR);
14395 break;
14397 case CPP_XOR:
14398 id = ansi_opname (BIT_XOR_EXPR);
14399 break;
14401 case CPP_AND:
14402 id = ansi_opname (BIT_AND_EXPR);
14403 break;
14405 case CPP_OR:
14406 id = ansi_opname (BIT_IOR_EXPR);
14407 break;
14409 case CPP_COMPL:
14410 id = ansi_opname (BIT_NOT_EXPR);
14411 break;
14413 case CPP_NOT:
14414 id = ansi_opname (TRUTH_NOT_EXPR);
14415 break;
14417 case CPP_EQ:
14418 id = ansi_assopname (NOP_EXPR);
14419 break;
14421 case CPP_LESS:
14422 id = ansi_opname (LT_EXPR);
14423 break;
14425 case CPP_GREATER:
14426 id = ansi_opname (GT_EXPR);
14427 break;
14429 case CPP_PLUS_EQ:
14430 id = ansi_assopname (PLUS_EXPR);
14431 break;
14433 case CPP_MINUS_EQ:
14434 id = ansi_assopname (MINUS_EXPR);
14435 break;
14437 case CPP_MULT_EQ:
14438 id = ansi_assopname (MULT_EXPR);
14439 break;
14441 case CPP_DIV_EQ:
14442 id = ansi_assopname (TRUNC_DIV_EXPR);
14443 break;
14445 case CPP_MOD_EQ:
14446 id = ansi_assopname (TRUNC_MOD_EXPR);
14447 break;
14449 case CPP_XOR_EQ:
14450 id = ansi_assopname (BIT_XOR_EXPR);
14451 break;
14453 case CPP_AND_EQ:
14454 id = ansi_assopname (BIT_AND_EXPR);
14455 break;
14457 case CPP_OR_EQ:
14458 id = ansi_assopname (BIT_IOR_EXPR);
14459 break;
14461 case CPP_LSHIFT:
14462 id = ansi_opname (LSHIFT_EXPR);
14463 break;
14465 case CPP_RSHIFT:
14466 id = ansi_opname (RSHIFT_EXPR);
14467 break;
14469 case CPP_LSHIFT_EQ:
14470 id = ansi_assopname (LSHIFT_EXPR);
14471 break;
14473 case CPP_RSHIFT_EQ:
14474 id = ansi_assopname (RSHIFT_EXPR);
14475 break;
14477 case CPP_EQ_EQ:
14478 id = ansi_opname (EQ_EXPR);
14479 break;
14481 case CPP_NOT_EQ:
14482 id = ansi_opname (NE_EXPR);
14483 break;
14485 case CPP_LESS_EQ:
14486 id = ansi_opname (LE_EXPR);
14487 break;
14489 case CPP_GREATER_EQ:
14490 id = ansi_opname (GE_EXPR);
14491 break;
14493 case CPP_AND_AND:
14494 id = ansi_opname (TRUTH_ANDIF_EXPR);
14495 break;
14497 case CPP_OR_OR:
14498 id = ansi_opname (TRUTH_ORIF_EXPR);
14499 break;
14501 case CPP_PLUS_PLUS:
14502 id = ansi_opname (POSTINCREMENT_EXPR);
14503 break;
14505 case CPP_MINUS_MINUS:
14506 id = ansi_opname (PREDECREMENT_EXPR);
14507 break;
14509 case CPP_COMMA:
14510 id = ansi_opname (COMPOUND_EXPR);
14511 break;
14513 case CPP_DEREF_STAR:
14514 id = ansi_opname (MEMBER_REF);
14515 break;
14517 case CPP_DEREF:
14518 id = ansi_opname (COMPONENT_REF);
14519 break;
14521 case CPP_OPEN_PAREN:
14522 /* Consume the `('. */
14523 cp_lexer_consume_token (parser->lexer);
14524 /* Look for the matching `)'. */
14525 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14526 return ansi_opname (CALL_EXPR);
14528 case CPP_OPEN_SQUARE:
14529 /* Consume the `['. */
14530 cp_lexer_consume_token (parser->lexer);
14531 /* Look for the matching `]'. */
14532 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14533 return ansi_opname (ARRAY_REF);
14535 case CPP_UTF8STRING:
14536 case CPP_UTF8STRING_USERDEF:
14537 utf8 = true;
14538 /* FALLTHRU */
14539 case CPP_STRING:
14540 case CPP_WSTRING:
14541 case CPP_STRING16:
14542 case CPP_STRING32:
14543 case CPP_STRING_USERDEF:
14544 case CPP_WSTRING_USERDEF:
14545 case CPP_STRING16_USERDEF:
14546 case CPP_STRING32_USERDEF:
14548 tree str, string_tree;
14549 int sz, len;
14551 if (cxx_dialect == cxx98)
14552 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14554 /* Consume the string. */
14555 str = cp_parser_string_literal (parser, /*translate=*/true,
14556 /*wide_ok=*/true, /*lookup_udlit=*/false);
14557 if (str == error_mark_node)
14558 return error_mark_node;
14559 else if (TREE_CODE (str) == USERDEF_LITERAL)
14561 string_tree = USERDEF_LITERAL_VALUE (str);
14562 id = USERDEF_LITERAL_SUFFIX_ID (str);
14564 else
14566 string_tree = str;
14567 /* Look for the suffix identifier. */
14568 token = cp_lexer_peek_token (parser->lexer);
14569 if (token->type == CPP_NAME)
14570 id = cp_parser_identifier (parser);
14571 else if (token->type == CPP_KEYWORD)
14573 error ("unexpected keyword;"
14574 " remove space between quotes and suffix identifier");
14575 return error_mark_node;
14577 else
14579 error ("expected suffix identifier");
14580 return error_mark_node;
14583 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14584 (TREE_TYPE (TREE_TYPE (string_tree))));
14585 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14586 if (len != 0)
14588 error ("expected empty string after %<operator%> keyword");
14589 return error_mark_node;
14591 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14592 != char_type_node)
14594 error ("invalid encoding prefix in literal operator");
14595 return error_mark_node;
14597 if (id != error_mark_node)
14599 const char *name = IDENTIFIER_POINTER (id);
14600 id = cp_literal_operator_id (name);
14602 return id;
14605 default:
14606 /* Anything else is an error. */
14607 break;
14610 /* If we have selected an identifier, we need to consume the
14611 operator token. */
14612 if (id)
14613 cp_lexer_consume_token (parser->lexer);
14614 /* Otherwise, no valid operator name was present. */
14615 else
14617 cp_parser_error (parser, "expected operator");
14618 id = error_mark_node;
14621 return cp_expr (id, start_loc);
14624 /* Parse a template-declaration.
14626 template-declaration:
14627 export [opt] template < template-parameter-list > declaration
14629 If MEMBER_P is TRUE, this template-declaration occurs within a
14630 class-specifier.
14632 The grammar rule given by the standard isn't correct. What
14633 is really meant is:
14635 template-declaration:
14636 export [opt] template-parameter-list-seq
14637 decl-specifier-seq [opt] init-declarator [opt] ;
14638 export [opt] template-parameter-list-seq
14639 function-definition
14641 template-parameter-list-seq:
14642 template-parameter-list-seq [opt]
14643 template < template-parameter-list >
14645 Concept Extensions:
14647 template-parameter-list-seq:
14648 template < template-parameter-list > requires-clause [opt]
14650 requires-clause:
14651 requires logical-or-expression */
14653 static void
14654 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14656 /* Check for `export'. */
14657 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14659 /* Consume the `export' token. */
14660 cp_lexer_consume_token (parser->lexer);
14661 /* Warn that we do not support `export'. */
14662 warning (0, "keyword %<export%> not implemented, and will be ignored");
14665 cp_parser_template_declaration_after_export (parser, member_p);
14668 /* Parse a template-parameter-list.
14670 template-parameter-list:
14671 template-parameter
14672 template-parameter-list , template-parameter
14674 Returns a TREE_LIST. Each node represents a template parameter.
14675 The nodes are connected via their TREE_CHAINs. */
14677 static tree
14678 cp_parser_template_parameter_list (cp_parser* parser)
14680 tree parameter_list = NULL_TREE;
14682 begin_template_parm_list ();
14684 /* The loop below parses the template parms. We first need to know
14685 the total number of template parms to be able to compute proper
14686 canonical types of each dependent type. So after the loop, when
14687 we know the total number of template parms,
14688 end_template_parm_list computes the proper canonical types and
14689 fixes up the dependent types accordingly. */
14690 while (true)
14692 tree parameter;
14693 bool is_non_type;
14694 bool is_parameter_pack;
14695 location_t parm_loc;
14697 /* Parse the template-parameter. */
14698 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
14699 parameter = cp_parser_template_parameter (parser,
14700 &is_non_type,
14701 &is_parameter_pack);
14702 /* Add it to the list. */
14703 if (parameter != error_mark_node)
14704 parameter_list = process_template_parm (parameter_list,
14705 parm_loc,
14706 parameter,
14707 is_non_type,
14708 is_parameter_pack);
14709 else
14711 tree err_parm = build_tree_list (parameter, parameter);
14712 parameter_list = chainon (parameter_list, err_parm);
14715 /* If the next token is not a `,', we're done. */
14716 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14717 break;
14718 /* Otherwise, consume the `,' token. */
14719 cp_lexer_consume_token (parser->lexer);
14722 return end_template_parm_list (parameter_list);
14725 /* Parse a introduction-list.
14727 introduction-list:
14728 introduced-parameter
14729 introduction-list , introduced-parameter
14731 introduced-parameter:
14732 ...[opt] identifier
14734 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
14735 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
14736 WILDCARD_DECL will also have DECL_NAME set and token location in
14737 DECL_SOURCE_LOCATION. */
14739 static tree
14740 cp_parser_introduction_list (cp_parser *parser)
14742 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
14744 while (true)
14746 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14747 if (is_pack)
14748 cp_lexer_consume_token (parser->lexer);
14750 /* Build placeholder. */
14751 tree parm = build_nt (WILDCARD_DECL);
14752 DECL_SOURCE_LOCATION (parm)
14753 = cp_lexer_peek_token (parser->lexer)->location;
14754 DECL_NAME (parm) = cp_parser_identifier (parser);
14755 WILDCARD_PACK_P (parm) = is_pack;
14756 vec_safe_push (introduction_vec, parm);
14758 /* If the next token is not a `,', we're done. */
14759 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14760 break;
14761 /* Otherwise, consume the `,' token. */
14762 cp_lexer_consume_token (parser->lexer);
14765 /* Convert the vec into a TREE_VEC. */
14766 tree introduction_list = make_tree_vec (introduction_vec->length ());
14767 unsigned int n;
14768 tree parm;
14769 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
14770 TREE_VEC_ELT (introduction_list, n) = parm;
14772 release_tree_vector (introduction_vec);
14773 return introduction_list;
14776 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
14777 is an abstract declarator. */
14779 static inline cp_declarator*
14780 get_id_declarator (cp_declarator *declarator)
14782 cp_declarator *d = declarator;
14783 while (d && d->kind != cdk_id)
14784 d = d->declarator;
14785 return d;
14788 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
14789 is an abstract declarator. */
14791 static inline tree
14792 get_unqualified_id (cp_declarator *declarator)
14794 declarator = get_id_declarator (declarator);
14795 if (declarator)
14796 return declarator->u.id.unqualified_name;
14797 else
14798 return NULL_TREE;
14801 /* Returns true if DECL represents a constrained-parameter. */
14803 static inline bool
14804 is_constrained_parameter (tree decl)
14806 return (decl
14807 && TREE_CODE (decl) == TYPE_DECL
14808 && CONSTRAINED_PARM_CONCEPT (decl)
14809 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
14812 /* Returns true if PARM declares a constrained-parameter. */
14814 static inline bool
14815 is_constrained_parameter (cp_parameter_declarator *parm)
14817 return is_constrained_parameter (parm->decl_specifiers.type);
14820 /* Check that the type parameter is only a declarator-id, and that its
14821 type is not cv-qualified. */
14823 bool
14824 cp_parser_check_constrained_type_parm (cp_parser *parser,
14825 cp_parameter_declarator *parm)
14827 if (!parm->declarator)
14828 return true;
14830 if (parm->declarator->kind != cdk_id)
14832 cp_parser_error (parser, "invalid constrained type parameter");
14833 return false;
14836 /* Don't allow cv-qualified type parameters. */
14837 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
14838 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
14840 cp_parser_error (parser, "cv-qualified type parameter");
14841 return false;
14844 return true;
14847 /* Finish parsing/processing a template type parameter and checking
14848 various restrictions. */
14850 static inline tree
14851 cp_parser_constrained_type_template_parm (cp_parser *parser,
14852 tree id,
14853 cp_parameter_declarator* parmdecl)
14855 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
14856 return finish_template_type_parm (class_type_node, id);
14857 else
14858 return error_mark_node;
14861 static tree
14862 finish_constrained_template_template_parm (tree proto, tree id)
14864 /* FIXME: This should probably be copied, and we may need to adjust
14865 the template parameter depths. */
14866 tree saved_parms = current_template_parms;
14867 begin_template_parm_list ();
14868 current_template_parms = DECL_TEMPLATE_PARMS (proto);
14869 end_template_parm_list ();
14871 tree parm = finish_template_template_parm (class_type_node, id);
14872 current_template_parms = saved_parms;
14874 return parm;
14877 /* Finish parsing/processing a template template parameter by borrowing
14878 the template parameter list from the prototype parameter. */
14880 static tree
14881 cp_parser_constrained_template_template_parm (cp_parser *parser,
14882 tree proto,
14883 tree id,
14884 cp_parameter_declarator *parmdecl)
14886 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
14887 return error_mark_node;
14888 return finish_constrained_template_template_parm (proto, id);
14891 /* Create a new non-type template parameter from the given PARM
14892 declarator. */
14894 static tree
14895 constrained_non_type_template_parm (bool *is_non_type,
14896 cp_parameter_declarator *parm)
14898 *is_non_type = true;
14899 cp_declarator *decl = parm->declarator;
14900 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
14901 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
14902 return grokdeclarator (decl, specs, TPARM, 0, NULL);
14905 /* Build a constrained template parameter based on the PARMDECL
14906 declarator. The type of PARMDECL is the constrained type, which
14907 refers to the prototype template parameter that ultimately
14908 specifies the type of the declared parameter. */
14910 static tree
14911 finish_constrained_parameter (cp_parser *parser,
14912 cp_parameter_declarator *parmdecl,
14913 bool *is_non_type,
14914 bool *is_parameter_pack)
14916 tree decl = parmdecl->decl_specifiers.type;
14917 tree id = get_unqualified_id (parmdecl->declarator);
14918 tree def = parmdecl->default_argument;
14919 tree proto = DECL_INITIAL (decl);
14921 /* A template parameter constrained by a variadic concept shall also
14922 be declared as a template parameter pack. */
14923 bool is_variadic = template_parameter_pack_p (proto);
14924 if (is_variadic && !*is_parameter_pack)
14925 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
14927 /* Build the parameter. Return an error if the declarator was invalid. */
14928 tree parm;
14929 if (TREE_CODE (proto) == TYPE_DECL)
14930 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
14931 else if (TREE_CODE (proto) == TEMPLATE_DECL)
14932 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
14933 parmdecl);
14934 else
14935 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
14936 if (parm == error_mark_node)
14937 return error_mark_node;
14939 /* Finish the parameter decl and create a node attaching the
14940 default argument and constraint. */
14941 parm = build_tree_list (def, parm);
14942 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
14944 return parm;
14947 /* Returns true if the parsed type actually represents the declaration
14948 of a type template-parameter. */
14950 static inline bool
14951 declares_constrained_type_template_parameter (tree type)
14953 return (is_constrained_parameter (type)
14954 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
14958 /* Returns true if the parsed type actually represents the declaration of
14959 a template template-parameter. */
14961 static bool
14962 declares_constrained_template_template_parameter (tree type)
14964 return (is_constrained_parameter (type)
14965 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
14968 /* Parse a default argument for a type template-parameter.
14969 Note that diagnostics are handled in cp_parser_template_parameter. */
14971 static tree
14972 cp_parser_default_type_template_argument (cp_parser *parser)
14974 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14976 /* Consume the `=' token. */
14977 cp_lexer_consume_token (parser->lexer);
14979 cp_token *token = cp_lexer_peek_token (parser->lexer);
14981 /* Parse the default-argument. */
14982 push_deferring_access_checks (dk_no_deferred);
14983 tree default_argument = cp_parser_type_id (parser);
14984 pop_deferring_access_checks ();
14986 if (flag_concepts && type_uses_auto (default_argument))
14988 error_at (token->location,
14989 "invalid use of %<auto%> in default template argument");
14990 return error_mark_node;
14993 return default_argument;
14996 /* Parse a default argument for a template template-parameter. */
14998 static tree
14999 cp_parser_default_template_template_argument (cp_parser *parser)
15001 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15003 bool is_template;
15005 /* Consume the `='. */
15006 cp_lexer_consume_token (parser->lexer);
15007 /* Parse the id-expression. */
15008 push_deferring_access_checks (dk_no_deferred);
15009 /* save token before parsing the id-expression, for error
15010 reporting */
15011 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15012 tree default_argument
15013 = cp_parser_id_expression (parser,
15014 /*template_keyword_p=*/false,
15015 /*check_dependency_p=*/true,
15016 /*template_p=*/&is_template,
15017 /*declarator_p=*/false,
15018 /*optional_p=*/false);
15019 if (TREE_CODE (default_argument) == TYPE_DECL)
15020 /* If the id-expression was a template-id that refers to
15021 a template-class, we already have the declaration here,
15022 so no further lookup is needed. */
15024 else
15025 /* Look up the name. */
15026 default_argument
15027 = cp_parser_lookup_name (parser, default_argument,
15028 none_type,
15029 /*is_template=*/is_template,
15030 /*is_namespace=*/false,
15031 /*check_dependency=*/true,
15032 /*ambiguous_decls=*/NULL,
15033 token->location);
15034 /* See if the default argument is valid. */
15035 default_argument = check_template_template_default_arg (default_argument);
15036 pop_deferring_access_checks ();
15037 return default_argument;
15040 /* Parse a template-parameter.
15042 template-parameter:
15043 type-parameter
15044 parameter-declaration
15046 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15047 the parameter. The TREE_PURPOSE is the default value, if any.
15048 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15049 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15050 set to true iff this parameter is a parameter pack. */
15052 static tree
15053 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15054 bool *is_parameter_pack)
15056 cp_token *token;
15057 cp_parameter_declarator *parameter_declarator;
15058 tree parm;
15060 /* Assume it is a type parameter or a template parameter. */
15061 *is_non_type = false;
15062 /* Assume it not a parameter pack. */
15063 *is_parameter_pack = false;
15064 /* Peek at the next token. */
15065 token = cp_lexer_peek_token (parser->lexer);
15066 /* If it is `class' or `template', we have a type-parameter. */
15067 if (token->keyword == RID_TEMPLATE)
15068 return cp_parser_type_parameter (parser, is_parameter_pack);
15069 /* If it is `class' or `typename' we do not know yet whether it is a
15070 type parameter or a non-type parameter. Consider:
15072 template <typename T, typename T::X X> ...
15076 template <class C, class D*> ...
15078 Here, the first parameter is a type parameter, and the second is
15079 a non-type parameter. We can tell by looking at the token after
15080 the identifier -- if it is a `,', `=', or `>' then we have a type
15081 parameter. */
15082 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15084 /* Peek at the token after `class' or `typename'. */
15085 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15086 /* If it's an ellipsis, we have a template type parameter
15087 pack. */
15088 if (token->type == CPP_ELLIPSIS)
15089 return cp_parser_type_parameter (parser, is_parameter_pack);
15090 /* If it's an identifier, skip it. */
15091 if (token->type == CPP_NAME)
15092 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15093 /* Now, see if the token looks like the end of a template
15094 parameter. */
15095 if (token->type == CPP_COMMA
15096 || token->type == CPP_EQ
15097 || token->type == CPP_GREATER)
15098 return cp_parser_type_parameter (parser, is_parameter_pack);
15101 /* Otherwise, it is a non-type parameter or a constrained parameter.
15103 [temp.param]
15105 When parsing a default template-argument for a non-type
15106 template-parameter, the first non-nested `>' is taken as the end
15107 of the template parameter-list rather than a greater-than
15108 operator. */
15109 parameter_declarator
15110 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15111 /*parenthesized_p=*/NULL);
15113 if (!parameter_declarator)
15114 return error_mark_node;
15116 /* If the parameter declaration is marked as a parameter pack, set
15117 *IS_PARAMETER_PACK to notify the caller. */
15118 if (parameter_declarator->template_parameter_pack_p)
15119 *is_parameter_pack = true;
15121 if (parameter_declarator->default_argument)
15123 /* Can happen in some cases of erroneous input (c++/34892). */
15124 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15125 /* Consume the `...' for better error recovery. */
15126 cp_lexer_consume_token (parser->lexer);
15129 // The parameter may have been constrained.
15130 if (is_constrained_parameter (parameter_declarator))
15131 return finish_constrained_parameter (parser,
15132 parameter_declarator,
15133 is_non_type,
15134 is_parameter_pack);
15136 // Now we're sure that the parameter is a non-type parameter.
15137 *is_non_type = true;
15139 parm = grokdeclarator (parameter_declarator->declarator,
15140 &parameter_declarator->decl_specifiers,
15141 TPARM, /*initialized=*/0,
15142 /*attrlist=*/NULL);
15143 if (parm == error_mark_node)
15144 return error_mark_node;
15146 return build_tree_list (parameter_declarator->default_argument, parm);
15149 /* Parse a type-parameter.
15151 type-parameter:
15152 class identifier [opt]
15153 class identifier [opt] = type-id
15154 typename identifier [opt]
15155 typename identifier [opt] = type-id
15156 template < template-parameter-list > class identifier [opt]
15157 template < template-parameter-list > class identifier [opt]
15158 = id-expression
15160 GNU Extension (variadic templates):
15162 type-parameter:
15163 class ... identifier [opt]
15164 typename ... identifier [opt]
15166 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15167 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15168 the declaration of the parameter.
15170 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15172 static tree
15173 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15175 cp_token *token;
15176 tree parameter;
15178 /* Look for a keyword to tell us what kind of parameter this is. */
15179 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15180 if (!token)
15181 return error_mark_node;
15183 switch (token->keyword)
15185 case RID_CLASS:
15186 case RID_TYPENAME:
15188 tree identifier;
15189 tree default_argument;
15191 /* If the next token is an ellipsis, we have a template
15192 argument pack. */
15193 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15195 /* Consume the `...' token. */
15196 cp_lexer_consume_token (parser->lexer);
15197 maybe_warn_variadic_templates ();
15199 *is_parameter_pack = true;
15202 /* If the next token is an identifier, then it names the
15203 parameter. */
15204 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15205 identifier = cp_parser_identifier (parser);
15206 else
15207 identifier = NULL_TREE;
15209 /* Create the parameter. */
15210 parameter = finish_template_type_parm (class_type_node, identifier);
15212 /* If the next token is an `=', we have a default argument. */
15213 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15215 default_argument
15216 = cp_parser_default_type_template_argument (parser);
15218 /* Template parameter packs cannot have default
15219 arguments. */
15220 if (*is_parameter_pack)
15222 if (identifier)
15223 error_at (token->location,
15224 "template parameter pack %qD cannot have a "
15225 "default argument", identifier);
15226 else
15227 error_at (token->location,
15228 "template parameter packs cannot have "
15229 "default arguments");
15230 default_argument = NULL_TREE;
15232 else if (check_for_bare_parameter_packs (default_argument))
15233 default_argument = error_mark_node;
15235 else
15236 default_argument = NULL_TREE;
15238 /* Create the combined representation of the parameter and the
15239 default argument. */
15240 parameter = build_tree_list (default_argument, parameter);
15242 break;
15244 case RID_TEMPLATE:
15246 tree identifier;
15247 tree default_argument;
15249 /* Look for the `<'. */
15250 cp_parser_require (parser, CPP_LESS, RT_LESS);
15251 /* Parse the template-parameter-list. */
15252 cp_parser_template_parameter_list (parser);
15253 /* Look for the `>'. */
15254 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15256 // If template requirements are present, parse them.
15257 if (flag_concepts)
15259 tree reqs = get_shorthand_constraints (current_template_parms);
15260 if (tree r = cp_parser_requires_clause_opt (parser))
15261 reqs = conjoin_constraints (reqs, normalize_expression (r));
15262 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15265 /* Look for the `class' or 'typename' keywords. */
15266 cp_parser_type_parameter_key (parser);
15267 /* If the next token is an ellipsis, we have a template
15268 argument pack. */
15269 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15271 /* Consume the `...' token. */
15272 cp_lexer_consume_token (parser->lexer);
15273 maybe_warn_variadic_templates ();
15275 *is_parameter_pack = true;
15277 /* If the next token is an `=', then there is a
15278 default-argument. If the next token is a `>', we are at
15279 the end of the parameter-list. If the next token is a `,',
15280 then we are at the end of this parameter. */
15281 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15282 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15283 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15285 identifier = cp_parser_identifier (parser);
15286 /* Treat invalid names as if the parameter were nameless. */
15287 if (identifier == error_mark_node)
15288 identifier = NULL_TREE;
15290 else
15291 identifier = NULL_TREE;
15293 /* Create the template parameter. */
15294 parameter = finish_template_template_parm (class_type_node,
15295 identifier);
15297 /* If the next token is an `=', then there is a
15298 default-argument. */
15299 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15301 default_argument
15302 = cp_parser_default_template_template_argument (parser);
15304 /* Template parameter packs cannot have default
15305 arguments. */
15306 if (*is_parameter_pack)
15308 if (identifier)
15309 error_at (token->location,
15310 "template parameter pack %qD cannot "
15311 "have a default argument",
15312 identifier);
15313 else
15314 error_at (token->location, "template parameter packs cannot "
15315 "have default arguments");
15316 default_argument = NULL_TREE;
15319 else
15320 default_argument = NULL_TREE;
15322 /* Create the combined representation of the parameter and the
15323 default argument. */
15324 parameter = build_tree_list (default_argument, parameter);
15326 break;
15328 default:
15329 gcc_unreachable ();
15330 break;
15333 return parameter;
15336 /* Parse a template-id.
15338 template-id:
15339 template-name < template-argument-list [opt] >
15341 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15342 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15343 returned. Otherwise, if the template-name names a function, or set
15344 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15345 names a class, returns a TYPE_DECL for the specialization.
15347 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15348 uninstantiated templates. */
15350 static tree
15351 cp_parser_template_id (cp_parser *parser,
15352 bool template_keyword_p,
15353 bool check_dependency_p,
15354 enum tag_types tag_type,
15355 bool is_declaration)
15357 tree templ;
15358 tree arguments;
15359 tree template_id;
15360 cp_token_position start_of_id = 0;
15361 cp_token *next_token = NULL, *next_token_2 = NULL;
15362 bool is_identifier;
15364 /* If the next token corresponds to a template-id, there is no need
15365 to reparse it. */
15366 next_token = cp_lexer_peek_token (parser->lexer);
15367 if (next_token->type == CPP_TEMPLATE_ID)
15369 cp_lexer_consume_token (parser->lexer);
15370 return saved_checks_value (next_token->u.tree_check_value);
15373 /* Avoid performing name lookup if there is no possibility of
15374 finding a template-id. */
15375 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
15376 || (next_token->type == CPP_NAME
15377 && !cp_parser_nth_token_starts_template_argument_list_p
15378 (parser, 2)))
15380 cp_parser_error (parser, "expected template-id");
15381 return error_mark_node;
15384 /* Remember where the template-id starts. */
15385 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15386 start_of_id = cp_lexer_token_position (parser->lexer, false);
15388 push_deferring_access_checks (dk_deferred);
15390 /* Parse the template-name. */
15391 is_identifier = false;
15392 templ = cp_parser_template_name (parser, template_keyword_p,
15393 check_dependency_p,
15394 is_declaration,
15395 tag_type,
15396 &is_identifier);
15397 if (templ == error_mark_node || is_identifier)
15399 pop_deferring_access_checks ();
15400 return templ;
15403 /* Since we're going to preserve any side-effects from this parse, set up a
15404 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15405 in the template arguments. */
15406 tentative_firewall firewall (parser);
15408 /* If we find the sequence `[:' after a template-name, it's probably
15409 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15410 parse correctly the argument list. */
15411 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15412 == CPP_OPEN_SQUARE)
15413 && next_token->flags & DIGRAPH
15414 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15415 == CPP_COLON)
15416 && !(next_token_2->flags & PREV_WHITE))
15418 cp_parser_parse_tentatively (parser);
15419 /* Change `:' into `::'. */
15420 next_token_2->type = CPP_SCOPE;
15421 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15422 CPP_LESS. */
15423 cp_lexer_consume_token (parser->lexer);
15425 /* Parse the arguments. */
15426 arguments = cp_parser_enclosed_template_argument_list (parser);
15427 if (!cp_parser_parse_definitely (parser))
15429 /* If we couldn't parse an argument list, then we revert our changes
15430 and return simply an error. Maybe this is not a template-id
15431 after all. */
15432 next_token_2->type = CPP_COLON;
15433 cp_parser_error (parser, "expected %<<%>");
15434 pop_deferring_access_checks ();
15435 return error_mark_node;
15437 /* Otherwise, emit an error about the invalid digraph, but continue
15438 parsing because we got our argument list. */
15439 if (permerror (next_token->location,
15440 "%<<::%> cannot begin a template-argument list"))
15442 static bool hint = false;
15443 inform (next_token->location,
15444 "%<<:%> is an alternate spelling for %<[%>."
15445 " Insert whitespace between %<<%> and %<::%>");
15446 if (!hint && !flag_permissive)
15448 inform (next_token->location, "(if you use %<-fpermissive%> "
15449 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15450 "accept your code)");
15451 hint = true;
15455 else
15457 /* Look for the `<' that starts the template-argument-list. */
15458 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15460 pop_deferring_access_checks ();
15461 return error_mark_node;
15463 /* Parse the arguments. */
15464 arguments = cp_parser_enclosed_template_argument_list (parser);
15467 /* Build a representation of the specialization. */
15468 if (identifier_p (templ))
15469 template_id = build_min_nt_loc (next_token->location,
15470 TEMPLATE_ID_EXPR,
15471 templ, arguments);
15472 else if (DECL_TYPE_TEMPLATE_P (templ)
15473 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15475 bool entering_scope;
15476 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15477 template (rather than some instantiation thereof) only if
15478 is not nested within some other construct. For example, in
15479 "template <typename T> void f(T) { A<T>::", A<T> is just an
15480 instantiation of A. */
15481 entering_scope = (template_parm_scope_p ()
15482 && cp_lexer_next_token_is (parser->lexer,
15483 CPP_SCOPE));
15484 template_id
15485 = finish_template_type (templ, arguments, entering_scope);
15487 /* A template-like identifier may be a partial concept id. */
15488 else if (flag_concepts
15489 && (template_id = (cp_parser_maybe_partial_concept_id
15490 (parser, templ, arguments))))
15491 return template_id;
15492 else if (variable_template_p (templ))
15494 template_id = lookup_template_variable (templ, arguments);
15495 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15496 SET_EXPR_LOCATION (template_id, next_token->location);
15498 else
15500 /* If it's not a class-template or a template-template, it should be
15501 a function-template. */
15502 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15503 || TREE_CODE (templ) == OVERLOAD
15504 || BASELINK_P (templ)));
15506 template_id = lookup_template_function (templ, arguments);
15507 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15508 SET_EXPR_LOCATION (template_id, next_token->location);
15511 /* If parsing tentatively, replace the sequence of tokens that makes
15512 up the template-id with a CPP_TEMPLATE_ID token. That way,
15513 should we re-parse the token stream, we will not have to repeat
15514 the effort required to do the parse, nor will we issue duplicate
15515 error messages about problems during instantiation of the
15516 template. */
15517 if (start_of_id
15518 /* Don't do this if we had a parse error in a declarator; re-parsing
15519 might succeed if a name changes meaning (60361). */
15520 && !(cp_parser_error_occurred (parser)
15521 && cp_parser_parsing_tentatively (parser)
15522 && parser->in_declarator_p))
15524 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
15526 /* Reset the contents of the START_OF_ID token. */
15527 token->type = CPP_TEMPLATE_ID;
15529 /* Update the location to be of the form:
15530 template-name < template-argument-list [opt] >
15531 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15532 with caret == start at the start of the template-name,
15533 ranging until the closing '>'. */
15534 location_t finish_loc
15535 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15536 location_t combined_loc
15537 = make_location (token->location, token->location, finish_loc);
15538 token->location = combined_loc;
15540 /* Retrieve any deferred checks. Do not pop this access checks yet
15541 so the memory will not be reclaimed during token replacing below. */
15542 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15543 token->u.tree_check_value->value = template_id;
15544 token->u.tree_check_value->checks = get_deferred_access_checks ();
15545 token->keyword = RID_MAX;
15547 /* Purge all subsequent tokens. */
15548 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15550 /* ??? Can we actually assume that, if template_id ==
15551 error_mark_node, we will have issued a diagnostic to the
15552 user, as opposed to simply marking the tentative parse as
15553 failed? */
15554 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15555 error_at (token->location, "parse error in template argument list");
15558 pop_to_parent_deferring_access_checks ();
15559 return template_id;
15562 /* Parse a template-name.
15564 template-name:
15565 identifier
15567 The standard should actually say:
15569 template-name:
15570 identifier
15571 operator-function-id
15573 A defect report has been filed about this issue.
15575 A conversion-function-id cannot be a template name because they cannot
15576 be part of a template-id. In fact, looking at this code:
15578 a.operator K<int>()
15580 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15581 It is impossible to call a templated conversion-function-id with an
15582 explicit argument list, since the only allowed template parameter is
15583 the type to which it is converting.
15585 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15586 `template' keyword, in a construction like:
15588 T::template f<3>()
15590 In that case `f' is taken to be a template-name, even though there
15591 is no way of knowing for sure.
15593 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15594 name refers to a set of overloaded functions, at least one of which
15595 is a template, or an IDENTIFIER_NODE with the name of the template,
15596 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15597 names are looked up inside uninstantiated templates. */
15599 static tree
15600 cp_parser_template_name (cp_parser* parser,
15601 bool template_keyword_p,
15602 bool check_dependency_p,
15603 bool is_declaration,
15604 enum tag_types tag_type,
15605 bool *is_identifier)
15607 tree identifier;
15608 tree decl;
15609 tree fns;
15610 cp_token *token = cp_lexer_peek_token (parser->lexer);
15612 /* If the next token is `operator', then we have either an
15613 operator-function-id or a conversion-function-id. */
15614 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15616 /* We don't know whether we're looking at an
15617 operator-function-id or a conversion-function-id. */
15618 cp_parser_parse_tentatively (parser);
15619 /* Try an operator-function-id. */
15620 identifier = cp_parser_operator_function_id (parser);
15621 /* If that didn't work, try a conversion-function-id. */
15622 if (!cp_parser_parse_definitely (parser))
15624 cp_parser_error (parser, "expected template-name");
15625 return error_mark_node;
15628 /* Look for the identifier. */
15629 else
15630 identifier = cp_parser_identifier (parser);
15632 /* If we didn't find an identifier, we don't have a template-id. */
15633 if (identifier == error_mark_node)
15634 return error_mark_node;
15636 /* If the name immediately followed the `template' keyword, then it
15637 is a template-name. However, if the next token is not `<', then
15638 we do not treat it as a template-name, since it is not being used
15639 as part of a template-id. This enables us to handle constructs
15640 like:
15642 template <typename T> struct S { S(); };
15643 template <typename T> S<T>::S();
15645 correctly. We would treat `S' as a template -- if it were `S<T>'
15646 -- but we do not if there is no `<'. */
15648 if (processing_template_decl
15649 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15651 /* In a declaration, in a dependent context, we pretend that the
15652 "template" keyword was present in order to improve error
15653 recovery. For example, given:
15655 template <typename T> void f(T::X<int>);
15657 we want to treat "X<int>" as a template-id. */
15658 if (is_declaration
15659 && !template_keyword_p
15660 && parser->scope && TYPE_P (parser->scope)
15661 && check_dependency_p
15662 && dependent_scope_p (parser->scope)
15663 /* Do not do this for dtors (or ctors), since they never
15664 need the template keyword before their name. */
15665 && !constructor_name_p (identifier, parser->scope))
15667 cp_token_position start = 0;
15669 /* Explain what went wrong. */
15670 error_at (token->location, "non-template %qD used as template",
15671 identifier);
15672 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15673 parser->scope, identifier);
15674 /* If parsing tentatively, find the location of the "<" token. */
15675 if (cp_parser_simulate_error (parser))
15676 start = cp_lexer_token_position (parser->lexer, true);
15677 /* Parse the template arguments so that we can issue error
15678 messages about them. */
15679 cp_lexer_consume_token (parser->lexer);
15680 cp_parser_enclosed_template_argument_list (parser);
15681 /* Skip tokens until we find a good place from which to
15682 continue parsing. */
15683 cp_parser_skip_to_closing_parenthesis (parser,
15684 /*recovering=*/true,
15685 /*or_comma=*/true,
15686 /*consume_paren=*/false);
15687 /* If parsing tentatively, permanently remove the
15688 template argument list. That will prevent duplicate
15689 error messages from being issued about the missing
15690 "template" keyword. */
15691 if (start)
15692 cp_lexer_purge_tokens_after (parser->lexer, start);
15693 if (is_identifier)
15694 *is_identifier = true;
15695 return identifier;
15698 /* If the "template" keyword is present, then there is generally
15699 no point in doing name-lookup, so we just return IDENTIFIER.
15700 But, if the qualifying scope is non-dependent then we can
15701 (and must) do name-lookup normally. */
15702 if (template_keyword_p
15703 && (!parser->scope
15704 || (TYPE_P (parser->scope)
15705 && dependent_type_p (parser->scope))))
15706 return identifier;
15709 /* Look up the name. */
15710 decl = cp_parser_lookup_name (parser, identifier,
15711 tag_type,
15712 /*is_template=*/true,
15713 /*is_namespace=*/false,
15714 check_dependency_p,
15715 /*ambiguous_decls=*/NULL,
15716 token->location);
15718 decl = strip_using_decl (decl);
15720 /* If DECL is a template, then the name was a template-name. */
15721 if (TREE_CODE (decl) == TEMPLATE_DECL)
15723 if (TREE_DEPRECATED (decl)
15724 && deprecated_state != DEPRECATED_SUPPRESS)
15725 warn_deprecated_use (decl, NULL_TREE);
15727 else
15729 tree fn = NULL_TREE;
15731 /* The standard does not explicitly indicate whether a name that
15732 names a set of overloaded declarations, some of which are
15733 templates, is a template-name. However, such a name should
15734 be a template-name; otherwise, there is no way to form a
15735 template-id for the overloaded templates. */
15736 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
15737 if (TREE_CODE (fns) == OVERLOAD)
15738 for (fn = fns; fn; fn = OVL_NEXT (fn))
15739 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
15740 break;
15742 if (!fn)
15744 /* The name does not name a template. */
15745 cp_parser_error (parser, "expected template-name");
15746 return error_mark_node;
15750 /* If DECL is dependent, and refers to a function, then just return
15751 its name; we will look it up again during template instantiation. */
15752 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
15754 tree scope = ovl_scope (decl);
15755 if (TYPE_P (scope) && dependent_type_p (scope))
15756 return identifier;
15759 return decl;
15762 /* Parse a template-argument-list.
15764 template-argument-list:
15765 template-argument ... [opt]
15766 template-argument-list , template-argument ... [opt]
15768 Returns a TREE_VEC containing the arguments. */
15770 static tree
15771 cp_parser_template_argument_list (cp_parser* parser)
15773 tree fixed_args[10];
15774 unsigned n_args = 0;
15775 unsigned alloced = 10;
15776 tree *arg_ary = fixed_args;
15777 tree vec;
15778 bool saved_in_template_argument_list_p;
15779 bool saved_ice_p;
15780 bool saved_non_ice_p;
15782 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
15783 parser->in_template_argument_list_p = true;
15784 /* Even if the template-id appears in an integral
15785 constant-expression, the contents of the argument list do
15786 not. */
15787 saved_ice_p = parser->integral_constant_expression_p;
15788 parser->integral_constant_expression_p = false;
15789 saved_non_ice_p = parser->non_integral_constant_expression_p;
15790 parser->non_integral_constant_expression_p = false;
15792 /* Parse the arguments. */
15795 tree argument;
15797 if (n_args)
15798 /* Consume the comma. */
15799 cp_lexer_consume_token (parser->lexer);
15801 /* Parse the template-argument. */
15802 argument = cp_parser_template_argument (parser);
15804 /* If the next token is an ellipsis, we're expanding a template
15805 argument pack. */
15806 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15808 if (argument == error_mark_node)
15810 cp_token *token = cp_lexer_peek_token (parser->lexer);
15811 error_at (token->location,
15812 "expected parameter pack before %<...%>");
15814 /* Consume the `...' token. */
15815 cp_lexer_consume_token (parser->lexer);
15817 /* Make the argument into a TYPE_PACK_EXPANSION or
15818 EXPR_PACK_EXPANSION. */
15819 argument = make_pack_expansion (argument);
15822 if (n_args == alloced)
15824 alloced *= 2;
15826 if (arg_ary == fixed_args)
15828 arg_ary = XNEWVEC (tree, alloced);
15829 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
15831 else
15832 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
15834 arg_ary[n_args++] = argument;
15836 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15838 vec = make_tree_vec (n_args);
15840 while (n_args--)
15841 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
15843 if (arg_ary != fixed_args)
15844 free (arg_ary);
15845 parser->non_integral_constant_expression_p = saved_non_ice_p;
15846 parser->integral_constant_expression_p = saved_ice_p;
15847 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
15848 if (CHECKING_P)
15849 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
15850 return vec;
15853 /* Parse a template-argument.
15855 template-argument:
15856 assignment-expression
15857 type-id
15858 id-expression
15860 The representation is that of an assignment-expression, type-id, or
15861 id-expression -- except that the qualified id-expression is
15862 evaluated, so that the value returned is either a DECL or an
15863 OVERLOAD.
15865 Although the standard says "assignment-expression", it forbids
15866 throw-expressions or assignments in the template argument.
15867 Therefore, we use "conditional-expression" instead. */
15869 static tree
15870 cp_parser_template_argument (cp_parser* parser)
15872 tree argument;
15873 bool template_p;
15874 bool address_p;
15875 bool maybe_type_id = false;
15876 cp_token *token = NULL, *argument_start_token = NULL;
15877 location_t loc = 0;
15878 cp_id_kind idk;
15880 /* There's really no way to know what we're looking at, so we just
15881 try each alternative in order.
15883 [temp.arg]
15885 In a template-argument, an ambiguity between a type-id and an
15886 expression is resolved to a type-id, regardless of the form of
15887 the corresponding template-parameter.
15889 Therefore, we try a type-id first. */
15890 cp_parser_parse_tentatively (parser);
15891 argument = cp_parser_template_type_arg (parser);
15892 /* If there was no error parsing the type-id but the next token is a
15893 '>>', our behavior depends on which dialect of C++ we're
15894 parsing. In C++98, we probably found a typo for '> >'. But there
15895 are type-id which are also valid expressions. For instance:
15897 struct X { int operator >> (int); };
15898 template <int V> struct Foo {};
15899 Foo<X () >> 5> r;
15901 Here 'X()' is a valid type-id of a function type, but the user just
15902 wanted to write the expression "X() >> 5". Thus, we remember that we
15903 found a valid type-id, but we still try to parse the argument as an
15904 expression to see what happens.
15906 In C++0x, the '>>' will be considered two separate '>'
15907 tokens. */
15908 if (!cp_parser_error_occurred (parser)
15909 && cxx_dialect == cxx98
15910 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15912 maybe_type_id = true;
15913 cp_parser_abort_tentative_parse (parser);
15915 else
15917 /* If the next token isn't a `,' or a `>', then this argument wasn't
15918 really finished. This means that the argument is not a valid
15919 type-id. */
15920 if (!cp_parser_next_token_ends_template_argument_p (parser))
15921 cp_parser_error (parser, "expected template-argument");
15922 /* If that worked, we're done. */
15923 if (cp_parser_parse_definitely (parser))
15924 return argument;
15926 /* We're still not sure what the argument will be. */
15927 cp_parser_parse_tentatively (parser);
15928 /* Try a template. */
15929 argument_start_token = cp_lexer_peek_token (parser->lexer);
15930 argument = cp_parser_id_expression (parser,
15931 /*template_keyword_p=*/false,
15932 /*check_dependency_p=*/true,
15933 &template_p,
15934 /*declarator_p=*/false,
15935 /*optional_p=*/false);
15936 /* If the next token isn't a `,' or a `>', then this argument wasn't
15937 really finished. */
15938 if (!cp_parser_next_token_ends_template_argument_p (parser))
15939 cp_parser_error (parser, "expected template-argument");
15940 if (!cp_parser_error_occurred (parser))
15942 /* Figure out what is being referred to. If the id-expression
15943 was for a class template specialization, then we will have a
15944 TYPE_DECL at this point. There is no need to do name lookup
15945 at this point in that case. */
15946 if (TREE_CODE (argument) != TYPE_DECL)
15947 argument = cp_parser_lookup_name (parser, argument,
15948 none_type,
15949 /*is_template=*/template_p,
15950 /*is_namespace=*/false,
15951 /*check_dependency=*/true,
15952 /*ambiguous_decls=*/NULL,
15953 argument_start_token->location);
15954 /* Handle a constrained-type-specifier for a non-type template
15955 parameter. */
15956 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
15957 argument = decl;
15958 else if (TREE_CODE (argument) != TEMPLATE_DECL
15959 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
15960 cp_parser_error (parser, "expected template-name");
15962 if (cp_parser_parse_definitely (parser))
15964 if (TREE_DEPRECATED (argument))
15965 warn_deprecated_use (argument, NULL_TREE);
15966 return argument;
15968 /* It must be a non-type argument. In C++17 any constant-expression is
15969 allowed. */
15970 if (cxx_dialect > cxx14)
15971 goto general_expr;
15973 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
15975 -- an integral constant-expression of integral or enumeration
15976 type; or
15978 -- the name of a non-type template-parameter; or
15980 -- the name of an object or function with external linkage...
15982 -- the address of an object or function with external linkage...
15984 -- a pointer to member... */
15985 /* Look for a non-type template parameter. */
15986 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15988 cp_parser_parse_tentatively (parser);
15989 argument = cp_parser_primary_expression (parser,
15990 /*address_p=*/false,
15991 /*cast_p=*/false,
15992 /*template_arg_p=*/true,
15993 &idk);
15994 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
15995 || !cp_parser_next_token_ends_template_argument_p (parser))
15996 cp_parser_simulate_error (parser);
15997 if (cp_parser_parse_definitely (parser))
15998 return argument;
16001 /* If the next token is "&", the argument must be the address of an
16002 object or function with external linkage. */
16003 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16004 if (address_p)
16006 loc = cp_lexer_peek_token (parser->lexer)->location;
16007 cp_lexer_consume_token (parser->lexer);
16009 /* See if we might have an id-expression. */
16010 token = cp_lexer_peek_token (parser->lexer);
16011 if (token->type == CPP_NAME
16012 || token->keyword == RID_OPERATOR
16013 || token->type == CPP_SCOPE
16014 || token->type == CPP_TEMPLATE_ID
16015 || token->type == CPP_NESTED_NAME_SPECIFIER)
16017 cp_parser_parse_tentatively (parser);
16018 argument = cp_parser_primary_expression (parser,
16019 address_p,
16020 /*cast_p=*/false,
16021 /*template_arg_p=*/true,
16022 &idk);
16023 if (cp_parser_error_occurred (parser)
16024 || !cp_parser_next_token_ends_template_argument_p (parser))
16025 cp_parser_abort_tentative_parse (parser);
16026 else
16028 tree probe;
16030 if (INDIRECT_REF_P (argument))
16032 /* Strip the dereference temporarily. */
16033 gcc_assert (REFERENCE_REF_P (argument));
16034 argument = TREE_OPERAND (argument, 0);
16037 /* If we're in a template, we represent a qualified-id referring
16038 to a static data member as a SCOPE_REF even if the scope isn't
16039 dependent so that we can check access control later. */
16040 probe = argument;
16041 if (TREE_CODE (probe) == SCOPE_REF)
16042 probe = TREE_OPERAND (probe, 1);
16043 if (VAR_P (probe))
16045 /* A variable without external linkage might still be a
16046 valid constant-expression, so no error is issued here
16047 if the external-linkage check fails. */
16048 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16049 cp_parser_simulate_error (parser);
16051 else if (is_overloaded_fn (argument))
16052 /* All overloaded functions are allowed; if the external
16053 linkage test does not pass, an error will be issued
16054 later. */
16056 else if (address_p
16057 && (TREE_CODE (argument) == OFFSET_REF
16058 || TREE_CODE (argument) == SCOPE_REF))
16059 /* A pointer-to-member. */
16061 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16063 else
16064 cp_parser_simulate_error (parser);
16066 if (cp_parser_parse_definitely (parser))
16068 if (address_p)
16069 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16070 tf_warning_or_error);
16071 else
16072 argument = convert_from_reference (argument);
16073 return argument;
16077 /* If the argument started with "&", there are no other valid
16078 alternatives at this point. */
16079 if (address_p)
16081 cp_parser_error (parser, "invalid non-type template argument");
16082 return error_mark_node;
16085 general_expr:
16086 /* If the argument wasn't successfully parsed as a type-id followed
16087 by '>>', the argument can only be a constant expression now.
16088 Otherwise, we try parsing the constant-expression tentatively,
16089 because the argument could really be a type-id. */
16090 if (maybe_type_id)
16091 cp_parser_parse_tentatively (parser);
16093 if (cxx_dialect <= cxx14)
16094 argument = cp_parser_constant_expression (parser);
16095 else
16097 /* With C++17 generalized non-type template arguments we need to handle
16098 lvalue constant expressions, too. */
16099 argument = cp_parser_assignment_expression (parser);
16100 require_potential_constant_expression (argument);
16103 if (!maybe_type_id)
16104 return argument;
16105 if (!cp_parser_next_token_ends_template_argument_p (parser))
16106 cp_parser_error (parser, "expected template-argument");
16107 if (cp_parser_parse_definitely (parser))
16108 return argument;
16109 /* We did our best to parse the argument as a non type-id, but that
16110 was the only alternative that matched (albeit with a '>' after
16111 it). We can assume it's just a typo from the user, and a
16112 diagnostic will then be issued. */
16113 return cp_parser_template_type_arg (parser);
16116 /* Parse an explicit-instantiation.
16118 explicit-instantiation:
16119 template declaration
16121 Although the standard says `declaration', what it really means is:
16123 explicit-instantiation:
16124 template decl-specifier-seq [opt] declarator [opt] ;
16126 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16127 supposed to be allowed. A defect report has been filed about this
16128 issue.
16130 GNU Extension:
16132 explicit-instantiation:
16133 storage-class-specifier template
16134 decl-specifier-seq [opt] declarator [opt] ;
16135 function-specifier template
16136 decl-specifier-seq [opt] declarator [opt] ; */
16138 static void
16139 cp_parser_explicit_instantiation (cp_parser* parser)
16141 int declares_class_or_enum;
16142 cp_decl_specifier_seq decl_specifiers;
16143 tree extension_specifier = NULL_TREE;
16145 timevar_push (TV_TEMPLATE_INST);
16147 /* Look for an (optional) storage-class-specifier or
16148 function-specifier. */
16149 if (cp_parser_allow_gnu_extensions_p (parser))
16151 extension_specifier
16152 = cp_parser_storage_class_specifier_opt (parser);
16153 if (!extension_specifier)
16154 extension_specifier
16155 = cp_parser_function_specifier_opt (parser,
16156 /*decl_specs=*/NULL);
16159 /* Look for the `template' keyword. */
16160 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16161 /* Let the front end know that we are processing an explicit
16162 instantiation. */
16163 begin_explicit_instantiation ();
16164 /* [temp.explicit] says that we are supposed to ignore access
16165 control while processing explicit instantiation directives. */
16166 push_deferring_access_checks (dk_no_check);
16167 /* Parse a decl-specifier-seq. */
16168 cp_parser_decl_specifier_seq (parser,
16169 CP_PARSER_FLAGS_OPTIONAL,
16170 &decl_specifiers,
16171 &declares_class_or_enum);
16172 /* If there was exactly one decl-specifier, and it declared a class,
16173 and there's no declarator, then we have an explicit type
16174 instantiation. */
16175 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16177 tree type;
16179 type = check_tag_decl (&decl_specifiers,
16180 /*explicit_type_instantiation_p=*/true);
16181 /* Turn access control back on for names used during
16182 template instantiation. */
16183 pop_deferring_access_checks ();
16184 if (type)
16185 do_type_instantiation (type, extension_specifier,
16186 /*complain=*/tf_error);
16188 else
16190 cp_declarator *declarator;
16191 tree decl;
16193 /* Parse the declarator. */
16194 declarator
16195 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16196 /*ctor_dtor_or_conv_p=*/NULL,
16197 /*parenthesized_p=*/NULL,
16198 /*member_p=*/false,
16199 /*friend_p=*/false);
16200 if (declares_class_or_enum & 2)
16201 cp_parser_check_for_definition_in_return_type (declarator,
16202 decl_specifiers.type,
16203 decl_specifiers.locations[ds_type_spec]);
16204 if (declarator != cp_error_declarator)
16206 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16207 permerror (decl_specifiers.locations[ds_inline],
16208 "explicit instantiation shall not use"
16209 " %<inline%> specifier");
16210 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16211 permerror (decl_specifiers.locations[ds_constexpr],
16212 "explicit instantiation shall not use"
16213 " %<constexpr%> specifier");
16215 decl = grokdeclarator (declarator, &decl_specifiers,
16216 NORMAL, 0, &decl_specifiers.attributes);
16217 /* Turn access control back on for names used during
16218 template instantiation. */
16219 pop_deferring_access_checks ();
16220 /* Do the explicit instantiation. */
16221 do_decl_instantiation (decl, extension_specifier);
16223 else
16225 pop_deferring_access_checks ();
16226 /* Skip the body of the explicit instantiation. */
16227 cp_parser_skip_to_end_of_statement (parser);
16230 /* We're done with the instantiation. */
16231 end_explicit_instantiation ();
16233 cp_parser_consume_semicolon_at_end_of_statement (parser);
16235 timevar_pop (TV_TEMPLATE_INST);
16238 /* Parse an explicit-specialization.
16240 explicit-specialization:
16241 template < > declaration
16243 Although the standard says `declaration', what it really means is:
16245 explicit-specialization:
16246 template <> decl-specifier [opt] init-declarator [opt] ;
16247 template <> function-definition
16248 template <> explicit-specialization
16249 template <> template-declaration */
16251 static void
16252 cp_parser_explicit_specialization (cp_parser* parser)
16254 bool need_lang_pop;
16255 cp_token *token = cp_lexer_peek_token (parser->lexer);
16257 /* Look for the `template' keyword. */
16258 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16259 /* Look for the `<'. */
16260 cp_parser_require (parser, CPP_LESS, RT_LESS);
16261 /* Look for the `>'. */
16262 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16263 /* We have processed another parameter list. */
16264 ++parser->num_template_parameter_lists;
16265 /* [temp]
16267 A template ... explicit specialization ... shall not have C
16268 linkage. */
16269 if (current_lang_name == lang_name_c)
16271 error_at (token->location, "template specialization with C linkage");
16272 /* Give it C++ linkage to avoid confusing other parts of the
16273 front end. */
16274 push_lang_context (lang_name_cplusplus);
16275 need_lang_pop = true;
16277 else
16278 need_lang_pop = false;
16279 /* Let the front end know that we are beginning a specialization. */
16280 if (!begin_specialization ())
16282 end_specialization ();
16283 return;
16286 /* If the next keyword is `template', we need to figure out whether
16287 or not we're looking a template-declaration. */
16288 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16290 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16291 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16292 cp_parser_template_declaration_after_export (parser,
16293 /*member_p=*/false);
16294 else
16295 cp_parser_explicit_specialization (parser);
16297 else
16298 /* Parse the dependent declaration. */
16299 cp_parser_single_declaration (parser,
16300 /*checks=*/NULL,
16301 /*member_p=*/false,
16302 /*explicit_specialization_p=*/true,
16303 /*friend_p=*/NULL);
16304 /* We're done with the specialization. */
16305 end_specialization ();
16306 /* For the erroneous case of a template with C linkage, we pushed an
16307 implicit C++ linkage scope; exit that scope now. */
16308 if (need_lang_pop)
16309 pop_lang_context ();
16310 /* We're done with this parameter list. */
16311 --parser->num_template_parameter_lists;
16314 /* Parse a type-specifier.
16316 type-specifier:
16317 simple-type-specifier
16318 class-specifier
16319 enum-specifier
16320 elaborated-type-specifier
16321 cv-qualifier
16323 GNU Extension:
16325 type-specifier:
16326 __complex__
16328 Returns a representation of the type-specifier. For a
16329 class-specifier, enum-specifier, or elaborated-type-specifier, a
16330 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16332 The parser flags FLAGS is used to control type-specifier parsing.
16334 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16335 in a decl-specifier-seq.
16337 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16338 class-specifier, enum-specifier, or elaborated-type-specifier, then
16339 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16340 if a type is declared; 2 if it is defined. Otherwise, it is set to
16341 zero.
16343 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16344 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16345 is set to FALSE. */
16347 static tree
16348 cp_parser_type_specifier (cp_parser* parser,
16349 cp_parser_flags flags,
16350 cp_decl_specifier_seq *decl_specs,
16351 bool is_declaration,
16352 int* declares_class_or_enum,
16353 bool* is_cv_qualifier)
16355 tree type_spec = NULL_TREE;
16356 cp_token *token;
16357 enum rid keyword;
16358 cp_decl_spec ds = ds_last;
16360 /* Assume this type-specifier does not declare a new type. */
16361 if (declares_class_or_enum)
16362 *declares_class_or_enum = 0;
16363 /* And that it does not specify a cv-qualifier. */
16364 if (is_cv_qualifier)
16365 *is_cv_qualifier = false;
16366 /* Peek at the next token. */
16367 token = cp_lexer_peek_token (parser->lexer);
16369 /* If we're looking at a keyword, we can use that to guide the
16370 production we choose. */
16371 keyword = token->keyword;
16372 switch (keyword)
16374 case RID_ENUM:
16375 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16376 goto elaborated_type_specifier;
16378 /* Look for the enum-specifier. */
16379 type_spec = cp_parser_enum_specifier (parser);
16380 /* If that worked, we're done. */
16381 if (type_spec)
16383 if (declares_class_or_enum)
16384 *declares_class_or_enum = 2;
16385 if (decl_specs)
16386 cp_parser_set_decl_spec_type (decl_specs,
16387 type_spec,
16388 token,
16389 /*type_definition_p=*/true);
16390 return type_spec;
16392 else
16393 goto elaborated_type_specifier;
16395 /* Any of these indicate either a class-specifier, or an
16396 elaborated-type-specifier. */
16397 case RID_CLASS:
16398 case RID_STRUCT:
16399 case RID_UNION:
16400 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16401 goto elaborated_type_specifier;
16403 /* Parse tentatively so that we can back up if we don't find a
16404 class-specifier. */
16405 cp_parser_parse_tentatively (parser);
16406 /* Look for the class-specifier. */
16407 type_spec = cp_parser_class_specifier (parser);
16408 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16409 /* If that worked, we're done. */
16410 if (cp_parser_parse_definitely (parser))
16412 if (declares_class_or_enum)
16413 *declares_class_or_enum = 2;
16414 if (decl_specs)
16415 cp_parser_set_decl_spec_type (decl_specs,
16416 type_spec,
16417 token,
16418 /*type_definition_p=*/true);
16419 return type_spec;
16422 /* Fall through. */
16423 elaborated_type_specifier:
16424 /* We're declaring (not defining) a class or enum. */
16425 if (declares_class_or_enum)
16426 *declares_class_or_enum = 1;
16428 /* Fall through. */
16429 case RID_TYPENAME:
16430 /* Look for an elaborated-type-specifier. */
16431 type_spec
16432 = (cp_parser_elaborated_type_specifier
16433 (parser,
16434 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16435 is_declaration));
16436 if (decl_specs)
16437 cp_parser_set_decl_spec_type (decl_specs,
16438 type_spec,
16439 token,
16440 /*type_definition_p=*/false);
16441 return type_spec;
16443 case RID_CONST:
16444 ds = ds_const;
16445 if (is_cv_qualifier)
16446 *is_cv_qualifier = true;
16447 break;
16449 case RID_VOLATILE:
16450 ds = ds_volatile;
16451 if (is_cv_qualifier)
16452 *is_cv_qualifier = true;
16453 break;
16455 case RID_RESTRICT:
16456 ds = ds_restrict;
16457 if (is_cv_qualifier)
16458 *is_cv_qualifier = true;
16459 break;
16461 case RID_COMPLEX:
16462 /* The `__complex__' keyword is a GNU extension. */
16463 ds = ds_complex;
16464 break;
16466 default:
16467 break;
16470 /* Handle simple keywords. */
16471 if (ds != ds_last)
16473 if (decl_specs)
16475 set_and_check_decl_spec_loc (decl_specs, ds, token);
16476 decl_specs->any_specifiers_p = true;
16478 return cp_lexer_consume_token (parser->lexer)->u.value;
16481 /* If we do not already have a type-specifier, assume we are looking
16482 at a simple-type-specifier. */
16483 type_spec = cp_parser_simple_type_specifier (parser,
16484 decl_specs,
16485 flags);
16487 /* If we didn't find a type-specifier, and a type-specifier was not
16488 optional in this context, issue an error message. */
16489 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16491 cp_parser_error (parser, "expected type specifier");
16492 return error_mark_node;
16495 return type_spec;
16498 /* Parse a simple-type-specifier.
16500 simple-type-specifier:
16501 :: [opt] nested-name-specifier [opt] type-name
16502 :: [opt] nested-name-specifier template template-id
16503 char
16504 wchar_t
16505 bool
16506 short
16508 long
16509 signed
16510 unsigned
16511 float
16512 double
16513 void
16515 C++11 Extension:
16517 simple-type-specifier:
16518 auto
16519 decltype ( expression )
16520 char16_t
16521 char32_t
16522 __underlying_type ( type-id )
16524 C++17 extension:
16526 nested-name-specifier(opt) template-name
16528 GNU Extension:
16530 simple-type-specifier:
16531 __int128
16532 __typeof__ unary-expression
16533 __typeof__ ( type-id )
16534 __typeof__ ( type-id ) { initializer-list , [opt] }
16536 Concepts Extension:
16538 simple-type-specifier:
16539 constrained-type-specifier
16541 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16542 appropriately updated. */
16544 static tree
16545 cp_parser_simple_type_specifier (cp_parser* parser,
16546 cp_decl_specifier_seq *decl_specs,
16547 cp_parser_flags flags)
16549 tree type = NULL_TREE;
16550 cp_token *token;
16551 int idx;
16553 /* Peek at the next token. */
16554 token = cp_lexer_peek_token (parser->lexer);
16556 /* If we're looking at a keyword, things are easy. */
16557 switch (token->keyword)
16559 case RID_CHAR:
16560 if (decl_specs)
16561 decl_specs->explicit_char_p = true;
16562 type = char_type_node;
16563 break;
16564 case RID_CHAR16:
16565 type = char16_type_node;
16566 break;
16567 case RID_CHAR32:
16568 type = char32_type_node;
16569 break;
16570 case RID_WCHAR:
16571 type = wchar_type_node;
16572 break;
16573 case RID_BOOL:
16574 type = boolean_type_node;
16575 break;
16576 case RID_SHORT:
16577 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16578 type = short_integer_type_node;
16579 break;
16580 case RID_INT:
16581 if (decl_specs)
16582 decl_specs->explicit_int_p = true;
16583 type = integer_type_node;
16584 break;
16585 case RID_INT_N_0:
16586 case RID_INT_N_1:
16587 case RID_INT_N_2:
16588 case RID_INT_N_3:
16589 idx = token->keyword - RID_INT_N_0;
16590 if (! int_n_enabled_p [idx])
16591 break;
16592 if (decl_specs)
16594 decl_specs->explicit_intN_p = true;
16595 decl_specs->int_n_idx = idx;
16597 type = int_n_trees [idx].signed_type;
16598 break;
16599 case RID_LONG:
16600 if (decl_specs)
16601 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16602 type = long_integer_type_node;
16603 break;
16604 case RID_SIGNED:
16605 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16606 type = integer_type_node;
16607 break;
16608 case RID_UNSIGNED:
16609 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16610 type = unsigned_type_node;
16611 break;
16612 case RID_FLOAT:
16613 type = float_type_node;
16614 break;
16615 case RID_DOUBLE:
16616 type = double_type_node;
16617 break;
16618 case RID_VOID:
16619 type = void_type_node;
16620 break;
16622 case RID_AUTO:
16623 maybe_warn_cpp0x (CPP0X_AUTO);
16624 if (parser->auto_is_implicit_function_template_parm_p)
16626 /* The 'auto' might be the placeholder return type for a function decl
16627 with trailing return type. */
16628 bool have_trailing_return_fn_decl = false;
16630 cp_parser_parse_tentatively (parser);
16631 cp_lexer_consume_token (parser->lexer);
16632 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16633 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16634 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16635 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16637 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16639 cp_lexer_consume_token (parser->lexer);
16640 cp_parser_skip_to_closing_parenthesis (parser,
16641 /*recovering*/false,
16642 /*or_comma*/false,
16643 /*consume_paren*/true);
16644 continue;
16647 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16649 have_trailing_return_fn_decl = true;
16650 break;
16653 cp_lexer_consume_token (parser->lexer);
16655 cp_parser_abort_tentative_parse (parser);
16657 if (have_trailing_return_fn_decl)
16659 type = make_auto ();
16660 break;
16663 if (cxx_dialect >= cxx14)
16665 type = synthesize_implicit_template_parm (parser, NULL_TREE);
16666 type = TREE_TYPE (type);
16668 else
16669 type = error_mark_node;
16671 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16673 if (cxx_dialect < cxx14)
16674 error_at (token->location,
16675 "use of %<auto%> in lambda parameter declaration "
16676 "only available with "
16677 "-std=c++14 or -std=gnu++14");
16679 else if (cxx_dialect < cxx14)
16680 error_at (token->location,
16681 "use of %<auto%> in parameter declaration "
16682 "only available with "
16683 "-std=c++14 or -std=gnu++14");
16684 else if (!flag_concepts)
16685 pedwarn (token->location, OPT_Wpedantic,
16686 "ISO C++ forbids use of %<auto%> in parameter "
16687 "declaration");
16689 else
16690 type = make_auto ();
16691 break;
16693 case RID_DECLTYPE:
16694 /* Since DR 743, decltype can either be a simple-type-specifier by
16695 itself or begin a nested-name-specifier. Parsing it will replace
16696 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16697 handling below decide what to do. */
16698 cp_parser_decltype (parser);
16699 cp_lexer_set_token_position (parser->lexer, token);
16700 break;
16702 case RID_TYPEOF:
16703 /* Consume the `typeof' token. */
16704 cp_lexer_consume_token (parser->lexer);
16705 /* Parse the operand to `typeof'. */
16706 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
16707 /* If it is not already a TYPE, take its type. */
16708 if (!TYPE_P (type))
16709 type = finish_typeof (type);
16711 if (decl_specs)
16712 cp_parser_set_decl_spec_type (decl_specs, type,
16713 token,
16714 /*type_definition_p=*/false);
16716 return type;
16718 case RID_UNDERLYING_TYPE:
16719 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
16720 if (decl_specs)
16721 cp_parser_set_decl_spec_type (decl_specs, type,
16722 token,
16723 /*type_definition_p=*/false);
16725 return type;
16727 case RID_BASES:
16728 case RID_DIRECT_BASES:
16729 type = cp_parser_trait_expr (parser, token->keyword);
16730 if (decl_specs)
16731 cp_parser_set_decl_spec_type (decl_specs, type,
16732 token,
16733 /*type_definition_p=*/false);
16734 return type;
16735 default:
16736 break;
16739 /* If token is an already-parsed decltype not followed by ::,
16740 it's a simple-type-specifier. */
16741 if (token->type == CPP_DECLTYPE
16742 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
16744 type = saved_checks_value (token->u.tree_check_value);
16745 if (decl_specs)
16747 cp_parser_set_decl_spec_type (decl_specs, type,
16748 token,
16749 /*type_definition_p=*/false);
16750 /* Remember that we are handling a decltype in order to
16751 implement the resolution of DR 1510 when the argument
16752 isn't instantiation dependent. */
16753 decl_specs->decltype_p = true;
16755 cp_lexer_consume_token (parser->lexer);
16756 return type;
16759 /* If the type-specifier was for a built-in type, we're done. */
16760 if (type)
16762 /* Record the type. */
16763 if (decl_specs
16764 && (token->keyword != RID_SIGNED
16765 && token->keyword != RID_UNSIGNED
16766 && token->keyword != RID_SHORT
16767 && token->keyword != RID_LONG))
16768 cp_parser_set_decl_spec_type (decl_specs,
16769 type,
16770 token,
16771 /*type_definition_p=*/false);
16772 if (decl_specs)
16773 decl_specs->any_specifiers_p = true;
16775 /* Consume the token. */
16776 cp_lexer_consume_token (parser->lexer);
16778 if (type == error_mark_node)
16779 return error_mark_node;
16781 /* There is no valid C++ program where a non-template type is
16782 followed by a "<". That usually indicates that the user thought
16783 that the type was a template. */
16784 cp_parser_check_for_invalid_template_id (parser, type, none_type,
16785 token->location);
16787 return TYPE_NAME (type);
16790 /* The type-specifier must be a user-defined type. */
16791 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
16793 bool qualified_p;
16794 bool global_p;
16796 /* Don't gobble tokens or issue error messages if this is an
16797 optional type-specifier. */
16798 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx1z)
16799 cp_parser_parse_tentatively (parser);
16801 token = cp_lexer_peek_token (parser->lexer);
16803 /* Look for the optional `::' operator. */
16804 global_p
16805 = (cp_parser_global_scope_opt (parser,
16806 /*current_scope_valid_p=*/false)
16807 != NULL_TREE);
16808 /* Look for the nested-name specifier. */
16809 qualified_p
16810 = (cp_parser_nested_name_specifier_opt (parser,
16811 /*typename_keyword_p=*/false,
16812 /*check_dependency_p=*/true,
16813 /*type_p=*/false,
16814 /*is_declaration=*/false)
16815 != NULL_TREE);
16816 /* If we have seen a nested-name-specifier, and the next token
16817 is `template', then we are using the template-id production. */
16818 if (parser->scope
16819 && cp_parser_optional_template_keyword (parser))
16821 /* Look for the template-id. */
16822 type = cp_parser_template_id (parser,
16823 /*template_keyword_p=*/true,
16824 /*check_dependency_p=*/true,
16825 none_type,
16826 /*is_declaration=*/false);
16827 /* If the template-id did not name a type, we are out of
16828 luck. */
16829 if (TREE_CODE (type) != TYPE_DECL)
16831 cp_parser_error (parser, "expected template-id for type");
16832 type = NULL_TREE;
16835 /* Otherwise, look for a type-name. */
16836 else
16837 type = cp_parser_type_name (parser);
16838 /* Keep track of all name-lookups performed in class scopes. */
16839 if (type
16840 && !global_p
16841 && !qualified_p
16842 && TREE_CODE (type) == TYPE_DECL
16843 && identifier_p (DECL_NAME (type)))
16844 maybe_note_name_used_in_class (DECL_NAME (type), type);
16845 /* If it didn't work out, we don't have a TYPE. */
16846 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx1z)
16847 && !cp_parser_parse_definitely (parser))
16848 type = NULL_TREE;
16849 if (!type && cxx_dialect >= cxx1z)
16851 if (flags & CP_PARSER_FLAGS_OPTIONAL)
16852 cp_parser_parse_tentatively (parser);
16854 cp_parser_global_scope_opt (parser,
16855 /*current_scope_valid_p=*/false);
16856 cp_parser_nested_name_specifier_opt (parser,
16857 /*typename_keyword_p=*/false,
16858 /*check_dependency_p=*/true,
16859 /*type_p=*/false,
16860 /*is_declaration=*/false);
16861 tree name = cp_parser_identifier (parser);
16862 if (name && TREE_CODE (name) == IDENTIFIER_NODE
16863 && parser->scope != error_mark_node)
16865 tree tmpl = cp_parser_lookup_name (parser, name,
16866 none_type,
16867 /*is_template=*/false,
16868 /*is_namespace=*/false,
16869 /*check_dependency=*/true,
16870 /*ambiguous_decls=*/NULL,
16871 token->location);
16872 if (tmpl && tmpl != error_mark_node
16873 && (DECL_CLASS_TEMPLATE_P (tmpl)
16874 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
16875 type = make_template_placeholder (tmpl);
16876 else
16878 type = error_mark_node;
16879 if (!cp_parser_simulate_error (parser))
16880 cp_parser_name_lookup_error (parser, name, tmpl,
16881 NLE_TYPE, token->location);
16884 else
16885 type = error_mark_node;
16887 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
16888 && !cp_parser_parse_definitely (parser))
16889 type = NULL_TREE;
16891 if (type && decl_specs)
16892 cp_parser_set_decl_spec_type (decl_specs, type,
16893 token,
16894 /*type_definition_p=*/false);
16897 /* If we didn't get a type-name, issue an error message. */
16898 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16900 cp_parser_error (parser, "expected type-name");
16901 return error_mark_node;
16904 if (type && type != error_mark_node)
16906 /* See if TYPE is an Objective-C type, and if so, parse and
16907 accept any protocol references following it. Do this before
16908 the cp_parser_check_for_invalid_template_id() call, because
16909 Objective-C types can be followed by '<...>' which would
16910 enclose protocol names rather than template arguments, and so
16911 everything is fine. */
16912 if (c_dialect_objc () && !parser->scope
16913 && (objc_is_id (type) || objc_is_class_name (type)))
16915 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16916 tree qual_type = objc_get_protocol_qualified_type (type, protos);
16918 /* Clobber the "unqualified" type previously entered into
16919 DECL_SPECS with the new, improved protocol-qualified version. */
16920 if (decl_specs)
16921 decl_specs->type = qual_type;
16923 return qual_type;
16926 /* There is no valid C++ program where a non-template type is
16927 followed by a "<". That usually indicates that the user
16928 thought that the type was a template. */
16929 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
16930 none_type,
16931 token->location);
16934 return type;
16937 /* Parse a type-name.
16939 type-name:
16940 class-name
16941 enum-name
16942 typedef-name
16943 simple-template-id [in c++0x]
16945 enum-name:
16946 identifier
16948 typedef-name:
16949 identifier
16951 Concepts:
16953 type-name:
16954 concept-name
16955 partial-concept-id
16957 concept-name:
16958 identifier
16960 Returns a TYPE_DECL for the type. */
16962 static tree
16963 cp_parser_type_name (cp_parser* parser)
16965 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
16968 /* See above. */
16969 static tree
16970 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
16972 tree type_decl;
16974 /* We can't know yet whether it is a class-name or not. */
16975 cp_parser_parse_tentatively (parser);
16976 /* Try a class-name. */
16977 type_decl = cp_parser_class_name (parser,
16978 typename_keyword_p,
16979 /*template_keyword_p=*/false,
16980 none_type,
16981 /*check_dependency_p=*/true,
16982 /*class_head_p=*/false,
16983 /*is_declaration=*/false);
16984 /* If it's not a class-name, keep looking. */
16985 if (!cp_parser_parse_definitely (parser))
16987 if (cxx_dialect < cxx11)
16988 /* It must be a typedef-name or an enum-name. */
16989 return cp_parser_nonclass_name (parser);
16991 cp_parser_parse_tentatively (parser);
16992 /* It is either a simple-template-id representing an
16993 instantiation of an alias template... */
16994 type_decl = cp_parser_template_id (parser,
16995 /*template_keyword_p=*/false,
16996 /*check_dependency_p=*/true,
16997 none_type,
16998 /*is_declaration=*/false);
16999 /* Note that this must be an instantiation of an alias template
17000 because [temp.names]/6 says:
17002 A template-id that names an alias template specialization
17003 is a type-name.
17005 Whereas [temp.names]/7 says:
17007 A simple-template-id that names a class template
17008 specialization is a class-name.
17010 With concepts, this could also be a partial-concept-id that
17011 declares a non-type template parameter. */
17012 if (type_decl != NULL_TREE
17013 && TREE_CODE (type_decl) == TYPE_DECL
17014 && TYPE_DECL_ALIAS_P (type_decl))
17015 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17016 else if (is_constrained_parameter (type_decl))
17017 /* Don't do anything. */ ;
17018 else
17019 cp_parser_simulate_error (parser);
17021 if (!cp_parser_parse_definitely (parser))
17022 /* ... Or a typedef-name or an enum-name. */
17023 return cp_parser_nonclass_name (parser);
17026 return type_decl;
17029 /* Check if DECL and ARGS can form a constrained-type-specifier.
17030 If ARGS is non-null, we try to form a concept check of the
17031 form DECL<?, ARGS> where ? is a wildcard that matches any
17032 kind of template argument. If ARGS is NULL, then we try to
17033 form a concept check of the form DECL<?>. */
17035 static tree
17036 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17037 tree decl, tree args)
17039 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17041 /* If we a constrained-type-specifier cannot be deduced. */
17042 if (parser->prevent_constrained_type_specifiers)
17043 return NULL_TREE;
17045 /* A constrained type specifier can only be found in an
17046 overload set or as a reference to a template declaration.
17048 FIXME: This might be masking a bug. It's possible that
17049 that the deduction below is causing template specializations
17050 to be formed with the wildcard as an argument. */
17051 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17052 return NULL_TREE;
17054 /* Try to build a call expression that evaluates the
17055 concept. This can fail if the overload set refers
17056 only to non-templates. */
17057 tree placeholder = build_nt (WILDCARD_DECL);
17058 tree check = build_concept_check (decl, placeholder, args);
17059 if (check == error_mark_node)
17060 return NULL_TREE;
17062 /* Deduce the checked constraint and the prototype parameter.
17064 FIXME: In certain cases, failure to deduce should be a
17065 diagnosable error. */
17066 tree conc;
17067 tree proto;
17068 if (!deduce_constrained_parameter (check, conc, proto))
17069 return NULL_TREE;
17071 /* In template parameter scope, this results in a constrained
17072 parameter. Return a descriptor of that parm. */
17073 if (processing_template_parmlist)
17074 return build_constrained_parameter (conc, proto, args);
17076 /* In a parameter-declaration-clause, constrained-type
17077 specifiers result in invented template parameters. */
17078 if (parser->auto_is_implicit_function_template_parm_p)
17080 tree x = build_constrained_parameter (conc, proto, args);
17081 return synthesize_implicit_template_parm (parser, x);
17083 else
17085 /* Otherwise, we're in a context where the constrained
17086 type name is deduced and the constraint applies
17087 after deduction. */
17088 return make_constrained_auto (conc, args);
17091 return NULL_TREE;
17094 /* If DECL refers to a concept, return a TYPE_DECL representing
17095 the result of using the constrained type specifier in the
17096 current context. DECL refers to a concept if
17098 - it is an overload set containing a function concept taking a single
17099 type argument, or
17101 - it is a variable concept taking a single type argument. */
17103 static tree
17104 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17106 if (flag_concepts
17107 && (TREE_CODE (decl) == OVERLOAD
17108 || BASELINK_P (decl)
17109 || variable_concept_p (decl)))
17110 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17111 else
17112 return NULL_TREE;
17115 /* Check if DECL and ARGS form a partial-concept-id. If so,
17116 assign ID to the resulting constrained placeholder.
17118 Returns true if the partial-concept-id designates a placeholder
17119 and false otherwise. Note that *id is set to NULL_TREE in
17120 this case. */
17122 static tree
17123 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17125 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17128 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17129 or a concept-name.
17131 enum-name:
17132 identifier
17134 typedef-name:
17135 identifier
17137 concept-name:
17138 identifier
17140 Returns a TYPE_DECL for the type. */
17142 static tree
17143 cp_parser_nonclass_name (cp_parser* parser)
17145 tree type_decl;
17146 tree identifier;
17148 cp_token *token = cp_lexer_peek_token (parser->lexer);
17149 identifier = cp_parser_identifier (parser);
17150 if (identifier == error_mark_node)
17151 return error_mark_node;
17153 /* Look up the type-name. */
17154 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17156 type_decl = strip_using_decl (type_decl);
17158 /* If we found an overload set, then it may refer to a concept-name. */
17159 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17160 type_decl = decl;
17162 if (TREE_CODE (type_decl) != TYPE_DECL
17163 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17165 /* See if this is an Objective-C type. */
17166 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17167 tree type = objc_get_protocol_qualified_type (identifier, protos);
17168 if (type)
17169 type_decl = TYPE_NAME (type);
17172 /* Issue an error if we did not find a type-name. */
17173 if (TREE_CODE (type_decl) != TYPE_DECL
17174 /* In Objective-C, we have the complication that class names are
17175 normally type names and start declarations (eg, the
17176 "NSObject" in "NSObject *object;"), but can be used in an
17177 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17178 is an expression. So, a classname followed by a dot is not a
17179 valid type-name. */
17180 || (objc_is_class_name (TREE_TYPE (type_decl))
17181 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17183 if (!cp_parser_simulate_error (parser))
17184 cp_parser_name_lookup_error (parser, identifier, type_decl,
17185 NLE_TYPE, token->location);
17186 return error_mark_node;
17188 /* Remember that the name was used in the definition of the
17189 current class so that we can check later to see if the
17190 meaning would have been different after the class was
17191 entirely defined. */
17192 else if (type_decl != error_mark_node
17193 && !parser->scope)
17194 maybe_note_name_used_in_class (identifier, type_decl);
17196 return type_decl;
17199 /* Parse an elaborated-type-specifier. Note that the grammar given
17200 here incorporates the resolution to DR68.
17202 elaborated-type-specifier:
17203 class-key :: [opt] nested-name-specifier [opt] identifier
17204 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17205 enum-key :: [opt] nested-name-specifier [opt] identifier
17206 typename :: [opt] nested-name-specifier identifier
17207 typename :: [opt] nested-name-specifier template [opt]
17208 template-id
17210 GNU extension:
17212 elaborated-type-specifier:
17213 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17214 class-key attributes :: [opt] nested-name-specifier [opt]
17215 template [opt] template-id
17216 enum attributes :: [opt] nested-name-specifier [opt] identifier
17218 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17219 declared `friend'. If IS_DECLARATION is TRUE, then this
17220 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17221 something is being declared.
17223 Returns the TYPE specified. */
17225 static tree
17226 cp_parser_elaborated_type_specifier (cp_parser* parser,
17227 bool is_friend,
17228 bool is_declaration)
17230 enum tag_types tag_type;
17231 tree identifier;
17232 tree type = NULL_TREE;
17233 tree attributes = NULL_TREE;
17234 tree globalscope;
17235 cp_token *token = NULL;
17237 /* See if we're looking at the `enum' keyword. */
17238 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17240 /* Consume the `enum' token. */
17241 cp_lexer_consume_token (parser->lexer);
17242 /* Remember that it's an enumeration type. */
17243 tag_type = enum_type;
17244 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17245 enums) is used here. */
17246 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17247 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17249 pedwarn (input_location, 0, "elaborated-type-specifier "
17250 "for a scoped enum must not use the %<%D%> keyword",
17251 cp_lexer_peek_token (parser->lexer)->u.value);
17252 /* Consume the `struct' or `class' and parse it anyway. */
17253 cp_lexer_consume_token (parser->lexer);
17255 /* Parse the attributes. */
17256 attributes = cp_parser_attributes_opt (parser);
17258 /* Or, it might be `typename'. */
17259 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17260 RID_TYPENAME))
17262 /* Consume the `typename' token. */
17263 cp_lexer_consume_token (parser->lexer);
17264 /* Remember that it's a `typename' type. */
17265 tag_type = typename_type;
17267 /* Otherwise it must be a class-key. */
17268 else
17270 tag_type = cp_parser_class_key (parser);
17271 if (tag_type == none_type)
17272 return error_mark_node;
17273 /* Parse the attributes. */
17274 attributes = cp_parser_attributes_opt (parser);
17277 /* Look for the `::' operator. */
17278 globalscope = cp_parser_global_scope_opt (parser,
17279 /*current_scope_valid_p=*/false);
17280 /* Look for the nested-name-specifier. */
17281 tree nested_name_specifier;
17282 if (tag_type == typename_type && !globalscope)
17284 nested_name_specifier
17285 = cp_parser_nested_name_specifier (parser,
17286 /*typename_keyword_p=*/true,
17287 /*check_dependency_p=*/true,
17288 /*type_p=*/true,
17289 is_declaration);
17290 if (!nested_name_specifier)
17291 return error_mark_node;
17293 else
17294 /* Even though `typename' is not present, the proposed resolution
17295 to Core Issue 180 says that in `class A<T>::B', `B' should be
17296 considered a type-name, even if `A<T>' is dependent. */
17297 nested_name_specifier
17298 = cp_parser_nested_name_specifier_opt (parser,
17299 /*typename_keyword_p=*/true,
17300 /*check_dependency_p=*/true,
17301 /*type_p=*/true,
17302 is_declaration);
17303 /* For everything but enumeration types, consider a template-id.
17304 For an enumeration type, consider only a plain identifier. */
17305 if (tag_type != enum_type)
17307 bool template_p = false;
17308 tree decl;
17310 /* Allow the `template' keyword. */
17311 template_p = cp_parser_optional_template_keyword (parser);
17312 /* If we didn't see `template', we don't know if there's a
17313 template-id or not. */
17314 if (!template_p)
17315 cp_parser_parse_tentatively (parser);
17316 /* Parse the template-id. */
17317 token = cp_lexer_peek_token (parser->lexer);
17318 decl = cp_parser_template_id (parser, template_p,
17319 /*check_dependency_p=*/true,
17320 tag_type,
17321 is_declaration);
17322 /* If we didn't find a template-id, look for an ordinary
17323 identifier. */
17324 if (!template_p && !cp_parser_parse_definitely (parser))
17326 /* We can get here when cp_parser_template_id, called by
17327 cp_parser_class_name with tag_type == none_type, succeeds
17328 and caches a BASELINK. Then, when called again here,
17329 instead of failing and returning an error_mark_node
17330 returns it (see template/typename17.C in C++11).
17331 ??? Could we diagnose this earlier? */
17332 else if (tag_type == typename_type && BASELINK_P (decl))
17334 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17335 type = error_mark_node;
17337 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17338 in effect, then we must assume that, upon instantiation, the
17339 template will correspond to a class. */
17340 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17341 && tag_type == typename_type)
17342 type = make_typename_type (parser->scope, decl,
17343 typename_type,
17344 /*complain=*/tf_error);
17345 /* If the `typename' keyword is in effect and DECL is not a type
17346 decl, then type is non existent. */
17347 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17349 else if (TREE_CODE (decl) == TYPE_DECL)
17351 type = check_elaborated_type_specifier (tag_type, decl,
17352 /*allow_template_p=*/true);
17354 /* If the next token is a semicolon, this must be a specialization,
17355 instantiation, or friend declaration. Check the scope while we
17356 still know whether or not we had a nested-name-specifier. */
17357 if (type != error_mark_node
17358 && !nested_name_specifier && !is_friend
17359 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17360 check_unqualified_spec_or_inst (type, token->location);
17362 else if (decl == error_mark_node)
17363 type = error_mark_node;
17366 if (!type)
17368 token = cp_lexer_peek_token (parser->lexer);
17369 identifier = cp_parser_identifier (parser);
17371 if (identifier == error_mark_node)
17373 parser->scope = NULL_TREE;
17374 return error_mark_node;
17377 /* For a `typename', we needn't call xref_tag. */
17378 if (tag_type == typename_type
17379 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17380 return cp_parser_make_typename_type (parser, identifier,
17381 token->location);
17383 /* Template parameter lists apply only if we are not within a
17384 function parameter list. */
17385 bool template_parm_lists_apply
17386 = parser->num_template_parameter_lists;
17387 if (template_parm_lists_apply)
17388 for (cp_binding_level *s = current_binding_level;
17389 s && s->kind != sk_template_parms;
17390 s = s->level_chain)
17391 if (s->kind == sk_function_parms)
17392 template_parm_lists_apply = false;
17394 /* Look up a qualified name in the usual way. */
17395 if (parser->scope)
17397 tree decl;
17398 tree ambiguous_decls;
17400 decl = cp_parser_lookup_name (parser, identifier,
17401 tag_type,
17402 /*is_template=*/false,
17403 /*is_namespace=*/false,
17404 /*check_dependency=*/true,
17405 &ambiguous_decls,
17406 token->location);
17408 /* If the lookup was ambiguous, an error will already have been
17409 issued. */
17410 if (ambiguous_decls)
17411 return error_mark_node;
17413 /* If we are parsing friend declaration, DECL may be a
17414 TEMPLATE_DECL tree node here. However, we need to check
17415 whether this TEMPLATE_DECL results in valid code. Consider
17416 the following example:
17418 namespace N {
17419 template <class T> class C {};
17421 class X {
17422 template <class T> friend class N::C; // #1, valid code
17424 template <class T> class Y {
17425 friend class N::C; // #2, invalid code
17428 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17429 name lookup of `N::C'. We see that friend declaration must
17430 be template for the code to be valid. Note that
17431 processing_template_decl does not work here since it is
17432 always 1 for the above two cases. */
17434 decl = (cp_parser_maybe_treat_template_as_class
17435 (decl, /*tag_name_p=*/is_friend
17436 && template_parm_lists_apply));
17438 if (TREE_CODE (decl) != TYPE_DECL)
17440 cp_parser_diagnose_invalid_type_name (parser,
17441 identifier,
17442 token->location);
17443 return error_mark_node;
17446 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17448 bool allow_template = (template_parm_lists_apply
17449 || DECL_SELF_REFERENCE_P (decl));
17450 type = check_elaborated_type_specifier (tag_type, decl,
17451 allow_template);
17453 if (type == error_mark_node)
17454 return error_mark_node;
17457 /* Forward declarations of nested types, such as
17459 class C1::C2;
17460 class C1::C2::C3;
17462 are invalid unless all components preceding the final '::'
17463 are complete. If all enclosing types are complete, these
17464 declarations become merely pointless.
17466 Invalid forward declarations of nested types are errors
17467 caught elsewhere in parsing. Those that are pointless arrive
17468 here. */
17470 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17471 && !is_friend && !processing_explicit_instantiation)
17472 warning (0, "declaration %qD does not declare anything", decl);
17474 type = TREE_TYPE (decl);
17476 else
17478 /* An elaborated-type-specifier sometimes introduces a new type and
17479 sometimes names an existing type. Normally, the rule is that it
17480 introduces a new type only if there is not an existing type of
17481 the same name already in scope. For example, given:
17483 struct S {};
17484 void f() { struct S s; }
17486 the `struct S' in the body of `f' is the same `struct S' as in
17487 the global scope; the existing definition is used. However, if
17488 there were no global declaration, this would introduce a new
17489 local class named `S'.
17491 An exception to this rule applies to the following code:
17493 namespace N { struct S; }
17495 Here, the elaborated-type-specifier names a new type
17496 unconditionally; even if there is already an `S' in the
17497 containing scope this declaration names a new type.
17498 This exception only applies if the elaborated-type-specifier
17499 forms the complete declaration:
17501 [class.name]
17503 A declaration consisting solely of `class-key identifier ;' is
17504 either a redeclaration of the name in the current scope or a
17505 forward declaration of the identifier as a class name. It
17506 introduces the name into the current scope.
17508 We are in this situation precisely when the next token is a `;'.
17510 An exception to the exception is that a `friend' declaration does
17511 *not* name a new type; i.e., given:
17513 struct S { friend struct T; };
17515 `T' is not a new type in the scope of `S'.
17517 Also, `new struct S' or `sizeof (struct S)' never results in the
17518 definition of a new type; a new type can only be declared in a
17519 declaration context. */
17521 tag_scope ts;
17522 bool template_p;
17524 if (is_friend)
17525 /* Friends have special name lookup rules. */
17526 ts = ts_within_enclosing_non_class;
17527 else if (is_declaration
17528 && cp_lexer_next_token_is (parser->lexer,
17529 CPP_SEMICOLON))
17530 /* This is a `class-key identifier ;' */
17531 ts = ts_current;
17532 else
17533 ts = ts_global;
17535 template_p =
17536 (template_parm_lists_apply
17537 && (cp_parser_next_token_starts_class_definition_p (parser)
17538 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17539 /* An unqualified name was used to reference this type, so
17540 there were no qualifying templates. */
17541 if (template_parm_lists_apply
17542 && !cp_parser_check_template_parameters (parser,
17543 /*num_templates=*/0,
17544 token->location,
17545 /*declarator=*/NULL))
17546 return error_mark_node;
17547 type = xref_tag (tag_type, identifier, ts, template_p);
17551 if (type == error_mark_node)
17552 return error_mark_node;
17554 /* Allow attributes on forward declarations of classes. */
17555 if (attributes)
17557 if (TREE_CODE (type) == TYPENAME_TYPE)
17558 warning (OPT_Wattributes,
17559 "attributes ignored on uninstantiated type");
17560 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17561 && ! processing_explicit_instantiation)
17562 warning (OPT_Wattributes,
17563 "attributes ignored on template instantiation");
17564 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17565 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17566 else
17567 warning (OPT_Wattributes,
17568 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17571 if (tag_type != enum_type)
17573 /* Indicate whether this class was declared as a `class' or as a
17574 `struct'. */
17575 if (CLASS_TYPE_P (type))
17576 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17577 cp_parser_check_class_key (tag_type, type);
17580 /* A "<" cannot follow an elaborated type specifier. If that
17581 happens, the user was probably trying to form a template-id. */
17582 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17583 token->location);
17585 return type;
17588 /* Parse an enum-specifier.
17590 enum-specifier:
17591 enum-head { enumerator-list [opt] }
17592 enum-head { enumerator-list , } [C++0x]
17594 enum-head:
17595 enum-key identifier [opt] enum-base [opt]
17596 enum-key nested-name-specifier identifier enum-base [opt]
17598 enum-key:
17599 enum
17600 enum class [C++0x]
17601 enum struct [C++0x]
17603 enum-base: [C++0x]
17604 : type-specifier-seq
17606 opaque-enum-specifier:
17607 enum-key identifier enum-base [opt] ;
17609 GNU Extensions:
17610 enum-key attributes[opt] identifier [opt] enum-base [opt]
17611 { enumerator-list [opt] }attributes[opt]
17612 enum-key attributes[opt] identifier [opt] enum-base [opt]
17613 { enumerator-list, }attributes[opt] [C++0x]
17615 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17616 if the token stream isn't an enum-specifier after all. */
17618 static tree
17619 cp_parser_enum_specifier (cp_parser* parser)
17621 tree identifier;
17622 tree type = NULL_TREE;
17623 tree prev_scope;
17624 tree nested_name_specifier = NULL_TREE;
17625 tree attributes;
17626 bool scoped_enum_p = false;
17627 bool has_underlying_type = false;
17628 bool nested_being_defined = false;
17629 bool new_value_list = false;
17630 bool is_new_type = false;
17631 bool is_unnamed = false;
17632 tree underlying_type = NULL_TREE;
17633 cp_token *type_start_token = NULL;
17634 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17636 parser->colon_corrects_to_scope_p = false;
17638 /* Parse tentatively so that we can back up if we don't find a
17639 enum-specifier. */
17640 cp_parser_parse_tentatively (parser);
17642 /* Caller guarantees that the current token is 'enum', an identifier
17643 possibly follows, and the token after that is an opening brace.
17644 If we don't have an identifier, fabricate an anonymous name for
17645 the enumeration being defined. */
17646 cp_lexer_consume_token (parser->lexer);
17648 /* Parse the "class" or "struct", which indicates a scoped
17649 enumeration type in C++0x. */
17650 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17651 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17653 if (cxx_dialect < cxx11)
17654 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17656 /* Consume the `struct' or `class' token. */
17657 cp_lexer_consume_token (parser->lexer);
17659 scoped_enum_p = true;
17662 attributes = cp_parser_attributes_opt (parser);
17664 /* Clear the qualification. */
17665 parser->scope = NULL_TREE;
17666 parser->qualifying_scope = NULL_TREE;
17667 parser->object_scope = NULL_TREE;
17669 /* Figure out in what scope the declaration is being placed. */
17670 prev_scope = current_scope ();
17672 type_start_token = cp_lexer_peek_token (parser->lexer);
17674 push_deferring_access_checks (dk_no_check);
17675 nested_name_specifier
17676 = cp_parser_nested_name_specifier_opt (parser,
17677 /*typename_keyword_p=*/true,
17678 /*check_dependency_p=*/false,
17679 /*type_p=*/false,
17680 /*is_declaration=*/false);
17682 if (nested_name_specifier)
17684 tree name;
17686 identifier = cp_parser_identifier (parser);
17687 name = cp_parser_lookup_name (parser, identifier,
17688 enum_type,
17689 /*is_template=*/false,
17690 /*is_namespace=*/false,
17691 /*check_dependency=*/true,
17692 /*ambiguous_decls=*/NULL,
17693 input_location);
17694 if (name && name != error_mark_node)
17696 type = TREE_TYPE (name);
17697 if (TREE_CODE (type) == TYPENAME_TYPE)
17699 /* Are template enums allowed in ISO? */
17700 if (template_parm_scope_p ())
17701 pedwarn (type_start_token->location, OPT_Wpedantic,
17702 "%qD is an enumeration template", name);
17703 /* ignore a typename reference, for it will be solved by name
17704 in start_enum. */
17705 type = NULL_TREE;
17708 else if (nested_name_specifier == error_mark_node)
17709 /* We already issued an error. */;
17710 else
17712 error_at (type_start_token->location,
17713 "%qD does not name an enumeration in %qT",
17714 identifier, nested_name_specifier);
17715 nested_name_specifier = error_mark_node;
17718 else
17720 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17721 identifier = cp_parser_identifier (parser);
17722 else
17724 identifier = make_anon_name ();
17725 is_unnamed = true;
17726 if (scoped_enum_p)
17727 error_at (type_start_token->location,
17728 "unnamed scoped enum is not allowed");
17731 pop_deferring_access_checks ();
17733 /* Check for the `:' that denotes a specified underlying type in C++0x.
17734 Note that a ':' could also indicate a bitfield width, however. */
17735 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17737 cp_decl_specifier_seq type_specifiers;
17739 /* Consume the `:'. */
17740 cp_lexer_consume_token (parser->lexer);
17742 /* Parse the type-specifier-seq. */
17743 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17744 /*is_trailing_return=*/false,
17745 &type_specifiers);
17747 /* At this point this is surely not elaborated type specifier. */
17748 if (!cp_parser_parse_definitely (parser))
17749 return NULL_TREE;
17751 if (cxx_dialect < cxx11)
17752 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17754 has_underlying_type = true;
17756 /* If that didn't work, stop. */
17757 if (type_specifiers.type != error_mark_node)
17759 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
17760 /*initialized=*/0, NULL);
17761 if (underlying_type == error_mark_node
17762 || check_for_bare_parameter_packs (underlying_type))
17763 underlying_type = NULL_TREE;
17767 /* Look for the `{' but don't consume it yet. */
17768 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17770 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
17772 cp_parser_error (parser, "expected %<{%>");
17773 if (has_underlying_type)
17775 type = NULL_TREE;
17776 goto out;
17779 /* An opaque-enum-specifier must have a ';' here. */
17780 if ((scoped_enum_p || underlying_type)
17781 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17783 cp_parser_error (parser, "expected %<;%> or %<{%>");
17784 if (has_underlying_type)
17786 type = NULL_TREE;
17787 goto out;
17792 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
17793 return NULL_TREE;
17795 if (nested_name_specifier)
17797 if (CLASS_TYPE_P (nested_name_specifier))
17799 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
17800 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
17801 push_scope (nested_name_specifier);
17803 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17805 push_nested_namespace (nested_name_specifier);
17809 /* Issue an error message if type-definitions are forbidden here. */
17810 if (!cp_parser_check_type_definition (parser))
17811 type = error_mark_node;
17812 else
17813 /* Create the new type. We do this before consuming the opening
17814 brace so the enum will be recorded as being on the line of its
17815 tag (or the 'enum' keyword, if there is no tag). */
17816 type = start_enum (identifier, type, underlying_type,
17817 attributes, scoped_enum_p, &is_new_type);
17819 /* If the next token is not '{' it is an opaque-enum-specifier or an
17820 elaborated-type-specifier. */
17821 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17823 timevar_push (TV_PARSE_ENUM);
17824 if (nested_name_specifier
17825 && nested_name_specifier != error_mark_node)
17827 /* The following catches invalid code such as:
17828 enum class S<int>::E { A, B, C }; */
17829 if (!processing_specialization
17830 && CLASS_TYPE_P (nested_name_specifier)
17831 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
17832 error_at (type_start_token->location, "cannot add an enumerator "
17833 "list to a template instantiation");
17835 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
17837 error_at (type_start_token->location,
17838 "%<%T::%E%> has not been declared",
17839 TYPE_CONTEXT (nested_name_specifier),
17840 nested_name_specifier);
17841 type = error_mark_node;
17843 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
17844 && !CLASS_TYPE_P (nested_name_specifier))
17846 error_at (type_start_token->location, "nested name specifier "
17847 "%qT for enum declaration does not name a class "
17848 "or namespace", nested_name_specifier);
17849 type = error_mark_node;
17851 /* If that scope does not contain the scope in which the
17852 class was originally declared, the program is invalid. */
17853 else if (prev_scope && !is_ancestor (prev_scope,
17854 nested_name_specifier))
17856 if (at_namespace_scope_p ())
17857 error_at (type_start_token->location,
17858 "declaration of %qD in namespace %qD which does not "
17859 "enclose %qD",
17860 type, prev_scope, nested_name_specifier);
17861 else
17862 error_at (type_start_token->location,
17863 "declaration of %qD in %qD which does not "
17864 "enclose %qD",
17865 type, prev_scope, nested_name_specifier);
17866 type = error_mark_node;
17868 /* If that scope is the scope where the declaration is being placed
17869 the program is invalid. */
17870 else if (CLASS_TYPE_P (nested_name_specifier)
17871 && CLASS_TYPE_P (prev_scope)
17872 && same_type_p (nested_name_specifier, prev_scope))
17874 permerror (type_start_token->location,
17875 "extra qualification not allowed");
17876 nested_name_specifier = NULL_TREE;
17880 if (scoped_enum_p)
17881 begin_scope (sk_scoped_enum, type);
17883 /* Consume the opening brace. */
17884 cp_lexer_consume_token (parser->lexer);
17886 if (type == error_mark_node)
17887 ; /* Nothing to add */
17888 else if (OPAQUE_ENUM_P (type)
17889 || (cxx_dialect > cxx98 && processing_specialization))
17891 new_value_list = true;
17892 SET_OPAQUE_ENUM_P (type, false);
17893 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17895 else
17897 error_at (type_start_token->location,
17898 "multiple definition of %q#T", type);
17899 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
17900 "previous definition here");
17901 type = error_mark_node;
17904 if (type == error_mark_node)
17905 cp_parser_skip_to_end_of_block_or_statement (parser);
17906 /* If the next token is not '}', then there are some enumerators. */
17907 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17909 if (is_unnamed && !scoped_enum_p)
17910 pedwarn (type_start_token->location, OPT_Wpedantic,
17911 "ISO C++ forbids empty unnamed enum");
17913 else
17914 cp_parser_enumerator_list (parser, type);
17916 /* Consume the final '}'. */
17917 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17919 if (scoped_enum_p)
17920 finish_scope ();
17921 timevar_pop (TV_PARSE_ENUM);
17923 else
17925 /* If a ';' follows, then it is an opaque-enum-specifier
17926 and additional restrictions apply. */
17927 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17929 if (is_unnamed)
17930 error_at (type_start_token->location,
17931 "opaque-enum-specifier without name");
17932 else if (nested_name_specifier)
17933 error_at (type_start_token->location,
17934 "opaque-enum-specifier must use a simple identifier");
17938 /* Look for trailing attributes to apply to this enumeration, and
17939 apply them if appropriate. */
17940 if (cp_parser_allow_gnu_extensions_p (parser))
17942 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
17943 cplus_decl_attributes (&type,
17944 trailing_attr,
17945 (int) ATTR_FLAG_TYPE_IN_PLACE);
17948 /* Finish up the enumeration. */
17949 if (type != error_mark_node)
17951 if (new_value_list)
17952 finish_enum_value_list (type);
17953 if (is_new_type)
17954 finish_enum (type);
17957 if (nested_name_specifier)
17959 if (CLASS_TYPE_P (nested_name_specifier))
17961 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
17962 pop_scope (nested_name_specifier);
17964 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17966 pop_nested_namespace (nested_name_specifier);
17969 out:
17970 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17971 return type;
17974 /* Parse an enumerator-list. The enumerators all have the indicated
17975 TYPE.
17977 enumerator-list:
17978 enumerator-definition
17979 enumerator-list , enumerator-definition */
17981 static void
17982 cp_parser_enumerator_list (cp_parser* parser, tree type)
17984 while (true)
17986 /* Parse an enumerator-definition. */
17987 cp_parser_enumerator_definition (parser, type);
17989 /* If the next token is not a ',', we've reached the end of
17990 the list. */
17991 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17992 break;
17993 /* Otherwise, consume the `,' and keep going. */
17994 cp_lexer_consume_token (parser->lexer);
17995 /* If the next token is a `}', there is a trailing comma. */
17996 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17998 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
17999 pedwarn (input_location, OPT_Wpedantic,
18000 "comma at end of enumerator list");
18001 break;
18006 /* Parse an enumerator-definition. The enumerator has the indicated
18007 TYPE.
18009 enumerator-definition:
18010 enumerator
18011 enumerator = constant-expression
18013 enumerator:
18014 identifier
18016 GNU Extensions:
18018 enumerator-definition:
18019 enumerator attributes [opt]
18020 enumerator attributes [opt] = constant-expression */
18022 static void
18023 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18025 tree identifier;
18026 tree value;
18027 location_t loc;
18029 /* Save the input location because we are interested in the location
18030 of the identifier and not the location of the explicit value. */
18031 loc = cp_lexer_peek_token (parser->lexer)->location;
18033 /* Look for the identifier. */
18034 identifier = cp_parser_identifier (parser);
18035 if (identifier == error_mark_node)
18036 return;
18038 /* Parse any specified attributes. */
18039 tree attrs = cp_parser_attributes_opt (parser);
18041 /* If the next token is an '=', then there is an explicit value. */
18042 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18044 /* Consume the `=' token. */
18045 cp_lexer_consume_token (parser->lexer);
18046 /* Parse the value. */
18047 value = cp_parser_constant_expression (parser);
18049 else
18050 value = NULL_TREE;
18052 /* If we are processing a template, make sure the initializer of the
18053 enumerator doesn't contain any bare template parameter pack. */
18054 if (check_for_bare_parameter_packs (value))
18055 value = error_mark_node;
18057 /* Create the enumerator. */
18058 build_enumerator (identifier, value, type, attrs, loc);
18061 /* Parse a namespace-name.
18063 namespace-name:
18064 original-namespace-name
18065 namespace-alias
18067 Returns the NAMESPACE_DECL for the namespace. */
18069 static tree
18070 cp_parser_namespace_name (cp_parser* parser)
18072 tree identifier;
18073 tree namespace_decl;
18075 cp_token *token = cp_lexer_peek_token (parser->lexer);
18077 /* Get the name of the namespace. */
18078 identifier = cp_parser_identifier (parser);
18079 if (identifier == error_mark_node)
18080 return error_mark_node;
18082 /* Look up the identifier in the currently active scope. Look only
18083 for namespaces, due to:
18085 [basic.lookup.udir]
18087 When looking up a namespace-name in a using-directive or alias
18088 definition, only namespace names are considered.
18090 And:
18092 [basic.lookup.qual]
18094 During the lookup of a name preceding the :: scope resolution
18095 operator, object, function, and enumerator names are ignored.
18097 (Note that cp_parser_qualifying_entity only calls this
18098 function if the token after the name is the scope resolution
18099 operator.) */
18100 namespace_decl = cp_parser_lookup_name (parser, identifier,
18101 none_type,
18102 /*is_template=*/false,
18103 /*is_namespace=*/true,
18104 /*check_dependency=*/true,
18105 /*ambiguous_decls=*/NULL,
18106 token->location);
18107 /* If it's not a namespace, issue an error. */
18108 if (namespace_decl == error_mark_node
18109 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18111 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18112 error_at (token->location, "%qD is not a namespace-name", identifier);
18113 cp_parser_error (parser, "expected namespace-name");
18114 namespace_decl = error_mark_node;
18117 return namespace_decl;
18120 /* Parse a namespace-definition.
18122 namespace-definition:
18123 named-namespace-definition
18124 unnamed-namespace-definition
18126 named-namespace-definition:
18127 original-namespace-definition
18128 extension-namespace-definition
18130 original-namespace-definition:
18131 namespace identifier { namespace-body }
18133 extension-namespace-definition:
18134 namespace original-namespace-name { namespace-body }
18136 unnamed-namespace-definition:
18137 namespace { namespace-body } */
18139 static void
18140 cp_parser_namespace_definition (cp_parser* parser)
18142 tree identifier, attribs;
18143 bool has_visibility;
18144 bool is_inline;
18145 cp_token* token;
18146 int nested_definition_count = 0;
18148 cp_ensure_no_omp_declare_simd (parser);
18149 cp_ensure_no_oacc_routine (parser);
18150 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
18152 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18153 is_inline = true;
18154 cp_lexer_consume_token (parser->lexer);
18156 else
18157 is_inline = false;
18159 /* Look for the `namespace' keyword. */
18160 token = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18162 /* Parse any specified attributes before the identifier. */
18163 attribs = cp_parser_attributes_opt (parser);
18165 /* Get the name of the namespace. We do not attempt to distinguish
18166 between an original-namespace-definition and an
18167 extension-namespace-definition at this point. The semantic
18168 analysis routines are responsible for that. */
18169 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18170 identifier = cp_parser_identifier (parser);
18171 else
18172 identifier = NULL_TREE;
18174 /* Parse any specified attributes after the identifier. */
18175 tree post_ident_attribs = cp_parser_attributes_opt (parser);
18176 if (post_ident_attribs)
18178 if (attribs)
18179 attribs = chainon (attribs, post_ident_attribs);
18180 else
18181 attribs = post_ident_attribs;
18184 /* Start the namespace. */
18185 bool ok = push_namespace (identifier);
18187 /* Parse any nested namespace definition. */
18188 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18190 if (attribs)
18191 error_at (token->location, "a nested namespace definition cannot have attributes");
18192 if (cxx_dialect < cxx1z)
18193 pedwarn (input_location, OPT_Wpedantic,
18194 "nested namespace definitions only available with "
18195 "-std=c++1z or -std=gnu++1z");
18196 if (is_inline)
18197 error_at (token->location, "a nested namespace definition cannot be inline");
18198 while (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18200 cp_lexer_consume_token (parser->lexer);
18201 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18202 identifier = cp_parser_identifier (parser);
18203 else
18205 cp_parser_error (parser, "nested identifier required");
18206 break;
18208 if (push_namespace (identifier))
18209 ++nested_definition_count;
18213 /* Look for the `{' to validate starting the namespace. */
18214 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
18216 /* "inline namespace" is equivalent to a stub namespace definition
18217 followed by a strong using directive. */
18218 if (is_inline && ok)
18220 tree name_space = current_namespace;
18221 /* Set up namespace association. */
18222 DECL_NAMESPACE_ASSOCIATIONS (name_space)
18223 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
18224 DECL_NAMESPACE_ASSOCIATIONS (name_space));
18225 /* Import the contents of the inline namespace. */
18226 pop_namespace ();
18227 do_using_directive (name_space);
18228 push_namespace (identifier);
18231 has_visibility = handle_namespace_attrs (current_namespace, attribs);
18233 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18235 /* Parse the body of the namespace. */
18236 cp_parser_namespace_body (parser);
18238 if (has_visibility)
18239 pop_visibility (1);
18241 /* Finish the nested namespace definitions. */
18242 while (nested_definition_count--)
18243 pop_namespace ();
18245 /* Finish the namespace. */
18246 if (ok)
18247 pop_namespace ();
18248 /* Look for the final `}'. */
18249 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18252 /* Parse a namespace-body.
18254 namespace-body:
18255 declaration-seq [opt] */
18257 static void
18258 cp_parser_namespace_body (cp_parser* parser)
18260 cp_parser_declaration_seq_opt (parser);
18263 /* Parse a namespace-alias-definition.
18265 namespace-alias-definition:
18266 namespace identifier = qualified-namespace-specifier ; */
18268 static void
18269 cp_parser_namespace_alias_definition (cp_parser* parser)
18271 tree identifier;
18272 tree namespace_specifier;
18274 cp_token *token = cp_lexer_peek_token (parser->lexer);
18276 /* Look for the `namespace' keyword. */
18277 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18278 /* Look for the identifier. */
18279 identifier = cp_parser_identifier (parser);
18280 if (identifier == error_mark_node)
18281 return;
18282 /* Look for the `=' token. */
18283 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18284 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18286 error_at (token->location, "%<namespace%> definition is not allowed here");
18287 /* Skip the definition. */
18288 cp_lexer_consume_token (parser->lexer);
18289 if (cp_parser_skip_to_closing_brace (parser))
18290 cp_lexer_consume_token (parser->lexer);
18291 return;
18293 cp_parser_require (parser, CPP_EQ, RT_EQ);
18294 /* Look for the qualified-namespace-specifier. */
18295 namespace_specifier
18296 = cp_parser_qualified_namespace_specifier (parser);
18297 /* Look for the `;' token. */
18298 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18300 /* Register the alias in the symbol table. */
18301 do_namespace_alias (identifier, namespace_specifier);
18304 /* Parse a qualified-namespace-specifier.
18306 qualified-namespace-specifier:
18307 :: [opt] nested-name-specifier [opt] namespace-name
18309 Returns a NAMESPACE_DECL corresponding to the specified
18310 namespace. */
18312 static tree
18313 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18315 /* Look for the optional `::'. */
18316 cp_parser_global_scope_opt (parser,
18317 /*current_scope_valid_p=*/false);
18319 /* Look for the optional nested-name-specifier. */
18320 cp_parser_nested_name_specifier_opt (parser,
18321 /*typename_keyword_p=*/false,
18322 /*check_dependency_p=*/true,
18323 /*type_p=*/false,
18324 /*is_declaration=*/true);
18326 return cp_parser_namespace_name (parser);
18329 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18330 access declaration.
18332 using-declaration:
18333 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18334 using :: unqualified-id ;
18336 access-declaration:
18337 qualified-id ;
18341 static bool
18342 cp_parser_using_declaration (cp_parser* parser,
18343 bool access_declaration_p)
18345 cp_token *token;
18346 bool typename_p = false;
18347 bool global_scope_p;
18348 tree decl;
18349 tree identifier;
18350 tree qscope;
18351 int oldcount = errorcount;
18352 cp_token *diag_token = NULL;
18354 if (access_declaration_p)
18356 diag_token = cp_lexer_peek_token (parser->lexer);
18357 cp_parser_parse_tentatively (parser);
18359 else
18361 /* Look for the `using' keyword. */
18362 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18364 again:
18365 /* Peek at the next token. */
18366 token = cp_lexer_peek_token (parser->lexer);
18367 /* See if it's `typename'. */
18368 if (token->keyword == RID_TYPENAME)
18370 /* Remember that we've seen it. */
18371 typename_p = true;
18372 /* Consume the `typename' token. */
18373 cp_lexer_consume_token (parser->lexer);
18377 /* Look for the optional global scope qualification. */
18378 global_scope_p
18379 = (cp_parser_global_scope_opt (parser,
18380 /*current_scope_valid_p=*/false)
18381 != NULL_TREE);
18383 /* If we saw `typename', or didn't see `::', then there must be a
18384 nested-name-specifier present. */
18385 if (typename_p || !global_scope_p)
18387 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18388 /*check_dependency_p=*/true,
18389 /*type_p=*/false,
18390 /*is_declaration=*/true);
18391 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18393 cp_parser_skip_to_end_of_block_or_statement (parser);
18394 return false;
18397 /* Otherwise, we could be in either of the two productions. In that
18398 case, treat the nested-name-specifier as optional. */
18399 else
18400 qscope = cp_parser_nested_name_specifier_opt (parser,
18401 /*typename_keyword_p=*/false,
18402 /*check_dependency_p=*/true,
18403 /*type_p=*/false,
18404 /*is_declaration=*/true);
18405 if (!qscope)
18406 qscope = global_namespace;
18407 else if (UNSCOPED_ENUM_P (qscope))
18408 qscope = CP_TYPE_CONTEXT (qscope);
18410 if (access_declaration_p && cp_parser_error_occurred (parser))
18411 /* Something has already gone wrong; there's no need to parse
18412 further. Since an error has occurred, the return value of
18413 cp_parser_parse_definitely will be false, as required. */
18414 return cp_parser_parse_definitely (parser);
18416 token = cp_lexer_peek_token (parser->lexer);
18417 /* Parse the unqualified-id. */
18418 identifier = cp_parser_unqualified_id (parser,
18419 /*template_keyword_p=*/false,
18420 /*check_dependency_p=*/true,
18421 /*declarator_p=*/true,
18422 /*optional_p=*/false);
18424 if (access_declaration_p)
18426 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18427 cp_parser_simulate_error (parser);
18428 if (!cp_parser_parse_definitely (parser))
18429 return false;
18431 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18433 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18434 if (cxx_dialect < cxx1z
18435 && !in_system_header_at (ell->location))
18436 pedwarn (ell->location, 0,
18437 "pack expansion in using-declaration only available "
18438 "with -std=c++1z or -std=gnu++1z");
18439 qscope = make_pack_expansion (qscope);
18442 /* The function we call to handle a using-declaration is different
18443 depending on what scope we are in. */
18444 if (qscope == error_mark_node || identifier == error_mark_node)
18446 else if (!identifier_p (identifier)
18447 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18448 /* [namespace.udecl]
18450 A using declaration shall not name a template-id. */
18451 error_at (token->location,
18452 "a template-id may not appear in a using-declaration");
18453 else
18455 if (at_class_scope_p ())
18457 /* Create the USING_DECL. */
18458 decl = do_class_using_decl (qscope, identifier);
18460 if (decl && typename_p)
18461 USING_DECL_TYPENAME_P (decl) = 1;
18463 if (check_for_bare_parameter_packs (decl))
18465 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18466 return false;
18468 else
18469 /* Add it to the list of members in this class. */
18470 finish_member_declaration (decl);
18472 else
18474 decl = cp_parser_lookup_name_simple (parser,
18475 identifier,
18476 token->location);
18477 if (decl == error_mark_node)
18478 cp_parser_name_lookup_error (parser, identifier,
18479 decl, NLE_NULL,
18480 token->location);
18481 else if (check_for_bare_parameter_packs (decl))
18483 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18484 return false;
18486 else if (!at_namespace_scope_p ())
18487 do_local_using_decl (decl, qscope, identifier);
18488 else
18489 do_toplevel_using_decl (decl, qscope, identifier);
18493 if (!access_declaration_p
18494 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18496 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18497 if (cxx_dialect < cxx1z)
18498 pedwarn (comma->location, 0,
18499 "comma-separated list in using-declaration only available "
18500 "with -std=c++1z or -std=gnu++1z");
18501 goto again;
18504 /* Look for the final `;'. */
18505 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18507 if (access_declaration_p && errorcount == oldcount)
18508 warning_at (diag_token->location, OPT_Wdeprecated,
18509 "access declarations are deprecated "
18510 "in favour of using-declarations; "
18511 "suggestion: add the %<using%> keyword");
18513 return true;
18516 /* Parse an alias-declaration.
18518 alias-declaration:
18519 using identifier attribute-specifier-seq [opt] = type-id */
18521 static tree
18522 cp_parser_alias_declaration (cp_parser* parser)
18524 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18525 location_t id_location;
18526 cp_declarator *declarator;
18527 cp_decl_specifier_seq decl_specs;
18528 bool member_p;
18529 const char *saved_message = NULL;
18531 /* Look for the `using' keyword. */
18532 cp_token *using_token
18533 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18534 if (using_token == NULL)
18535 return error_mark_node;
18537 id_location = cp_lexer_peek_token (parser->lexer)->location;
18538 id = cp_parser_identifier (parser);
18539 if (id == error_mark_node)
18540 return error_mark_node;
18542 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18543 attributes = cp_parser_attributes_opt (parser);
18544 if (attributes == error_mark_node)
18545 return error_mark_node;
18547 cp_parser_require (parser, CPP_EQ, RT_EQ);
18549 if (cp_parser_error_occurred (parser))
18550 return error_mark_node;
18552 cp_parser_commit_to_tentative_parse (parser);
18554 /* Now we are going to parse the type-id of the declaration. */
18557 [dcl.type]/3 says:
18559 "A type-specifier-seq shall not define a class or enumeration
18560 unless it appears in the type-id of an alias-declaration (7.1.3) that
18561 is not the declaration of a template-declaration."
18563 In other words, if we currently are in an alias template, the
18564 type-id should not define a type.
18566 So let's set parser->type_definition_forbidden_message in that
18567 case; cp_parser_check_type_definition (called by
18568 cp_parser_class_specifier) will then emit an error if a type is
18569 defined in the type-id. */
18570 if (parser->num_template_parameter_lists)
18572 saved_message = parser->type_definition_forbidden_message;
18573 parser->type_definition_forbidden_message =
18574 G_("types may not be defined in alias template declarations");
18577 type = cp_parser_type_id (parser);
18579 /* Restore the error message if need be. */
18580 if (parser->num_template_parameter_lists)
18581 parser->type_definition_forbidden_message = saved_message;
18583 if (type == error_mark_node
18584 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18586 cp_parser_skip_to_end_of_block_or_statement (parser);
18587 return error_mark_node;
18590 /* A typedef-name can also be introduced by an alias-declaration. The
18591 identifier following the using keyword becomes a typedef-name. It has
18592 the same semantics as if it were introduced by the typedef
18593 specifier. In particular, it does not define a new type and it shall
18594 not appear in the type-id. */
18596 clear_decl_specs (&decl_specs);
18597 decl_specs.type = type;
18598 if (attributes != NULL_TREE)
18600 decl_specs.attributes = attributes;
18601 set_and_check_decl_spec_loc (&decl_specs,
18602 ds_attribute,
18603 attrs_token);
18605 set_and_check_decl_spec_loc (&decl_specs,
18606 ds_typedef,
18607 using_token);
18608 set_and_check_decl_spec_loc (&decl_specs,
18609 ds_alias,
18610 using_token);
18612 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18613 declarator->id_loc = id_location;
18615 member_p = at_class_scope_p ();
18616 if (member_p)
18617 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18618 NULL_TREE, attributes);
18619 else
18620 decl = start_decl (declarator, &decl_specs, 0,
18621 attributes, NULL_TREE, &pushed_scope);
18622 if (decl == error_mark_node)
18623 return decl;
18625 // Attach constraints to the alias declaration.
18626 if (flag_concepts && current_template_parms)
18628 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
18629 tree constr = build_constraints (reqs, NULL_TREE);
18630 set_constraints (decl, constr);
18633 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
18635 if (pushed_scope)
18636 pop_scope (pushed_scope);
18638 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18639 added into the symbol table; otherwise, return the TYPE_DECL. */
18640 if (DECL_LANG_SPECIFIC (decl)
18641 && DECL_TEMPLATE_INFO (decl)
18642 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
18644 decl = DECL_TI_TEMPLATE (decl);
18645 if (member_p)
18646 check_member_template (decl);
18649 return decl;
18652 /* Parse a using-directive.
18654 using-directive:
18655 using namespace :: [opt] nested-name-specifier [opt]
18656 namespace-name ; */
18658 static void
18659 cp_parser_using_directive (cp_parser* parser)
18661 tree namespace_decl;
18662 tree attribs;
18664 /* Look for the `using' keyword. */
18665 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18666 /* And the `namespace' keyword. */
18667 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18668 /* Look for the optional `::' operator. */
18669 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18670 /* And the optional nested-name-specifier. */
18671 cp_parser_nested_name_specifier_opt (parser,
18672 /*typename_keyword_p=*/false,
18673 /*check_dependency_p=*/true,
18674 /*type_p=*/false,
18675 /*is_declaration=*/true);
18676 /* Get the namespace being used. */
18677 namespace_decl = cp_parser_namespace_name (parser);
18678 /* And any specified attributes. */
18679 attribs = cp_parser_attributes_opt (parser);
18680 /* Update the symbol table. */
18681 parse_using_directive (namespace_decl, attribs);
18682 /* Look for the final `;'. */
18683 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18686 /* Parse an asm-definition.
18688 asm-definition:
18689 asm ( string-literal ) ;
18691 GNU Extension:
18693 asm-definition:
18694 asm volatile [opt] ( string-literal ) ;
18695 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18696 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18697 : asm-operand-list [opt] ) ;
18698 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18699 : asm-operand-list [opt]
18700 : asm-clobber-list [opt] ) ;
18701 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18702 : asm-clobber-list [opt]
18703 : asm-goto-list ) ; */
18705 static void
18706 cp_parser_asm_definition (cp_parser* parser)
18708 tree string;
18709 tree outputs = NULL_TREE;
18710 tree inputs = NULL_TREE;
18711 tree clobbers = NULL_TREE;
18712 tree labels = NULL_TREE;
18713 tree asm_stmt;
18714 bool volatile_p = false;
18715 bool extended_p = false;
18716 bool invalid_inputs_p = false;
18717 bool invalid_outputs_p = false;
18718 bool goto_p = false;
18719 required_token missing = RT_NONE;
18721 /* Look for the `asm' keyword. */
18722 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
18724 if (parser->in_function_body
18725 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
18727 error ("%<asm%> in %<constexpr%> function");
18728 cp_function_chain->invalid_constexpr = true;
18731 /* See if the next token is `volatile'. */
18732 if (cp_parser_allow_gnu_extensions_p (parser)
18733 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
18735 /* Remember that we saw the `volatile' keyword. */
18736 volatile_p = true;
18737 /* Consume the token. */
18738 cp_lexer_consume_token (parser->lexer);
18740 if (cp_parser_allow_gnu_extensions_p (parser)
18741 && parser->in_function_body
18742 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
18744 /* Remember that we saw the `goto' keyword. */
18745 goto_p = true;
18746 /* Consume the token. */
18747 cp_lexer_consume_token (parser->lexer);
18749 /* Look for the opening `('. */
18750 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18751 return;
18752 /* Look for the string. */
18753 string = cp_parser_string_literal (parser, false, false);
18754 if (string == error_mark_node)
18756 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18757 /*consume_paren=*/true);
18758 return;
18761 /* If we're allowing GNU extensions, check for the extended assembly
18762 syntax. Unfortunately, the `:' tokens need not be separated by
18763 a space in C, and so, for compatibility, we tolerate that here
18764 too. Doing that means that we have to treat the `::' operator as
18765 two `:' tokens. */
18766 if (cp_parser_allow_gnu_extensions_p (parser)
18767 && parser->in_function_body
18768 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
18769 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
18771 bool inputs_p = false;
18772 bool clobbers_p = false;
18773 bool labels_p = false;
18775 /* The extended syntax was used. */
18776 extended_p = true;
18778 /* Look for outputs. */
18779 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18781 /* Consume the `:'. */
18782 cp_lexer_consume_token (parser->lexer);
18783 /* Parse the output-operands. */
18784 if (cp_lexer_next_token_is_not (parser->lexer,
18785 CPP_COLON)
18786 && cp_lexer_next_token_is_not (parser->lexer,
18787 CPP_SCOPE)
18788 && cp_lexer_next_token_is_not (parser->lexer,
18789 CPP_CLOSE_PAREN)
18790 && !goto_p)
18792 outputs = cp_parser_asm_operand_list (parser);
18793 if (outputs == error_mark_node)
18794 invalid_outputs_p = true;
18797 /* If the next token is `::', there are no outputs, and the
18798 next token is the beginning of the inputs. */
18799 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18800 /* The inputs are coming next. */
18801 inputs_p = true;
18803 /* Look for inputs. */
18804 if (inputs_p
18805 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18807 /* Consume the `:' or `::'. */
18808 cp_lexer_consume_token (parser->lexer);
18809 /* Parse the output-operands. */
18810 if (cp_lexer_next_token_is_not (parser->lexer,
18811 CPP_COLON)
18812 && cp_lexer_next_token_is_not (parser->lexer,
18813 CPP_SCOPE)
18814 && cp_lexer_next_token_is_not (parser->lexer,
18815 CPP_CLOSE_PAREN))
18817 inputs = cp_parser_asm_operand_list (parser);
18818 if (inputs == error_mark_node)
18819 invalid_inputs_p = true;
18822 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18823 /* The clobbers are coming next. */
18824 clobbers_p = true;
18826 /* Look for clobbers. */
18827 if (clobbers_p
18828 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18830 clobbers_p = true;
18831 /* Consume the `:' or `::'. */
18832 cp_lexer_consume_token (parser->lexer);
18833 /* Parse the clobbers. */
18834 if (cp_lexer_next_token_is_not (parser->lexer,
18835 CPP_COLON)
18836 && cp_lexer_next_token_is_not (parser->lexer,
18837 CPP_CLOSE_PAREN))
18838 clobbers = cp_parser_asm_clobber_list (parser);
18840 else if (goto_p
18841 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18842 /* The labels are coming next. */
18843 labels_p = true;
18845 /* Look for labels. */
18846 if (labels_p
18847 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
18849 labels_p = true;
18850 /* Consume the `:' or `::'. */
18851 cp_lexer_consume_token (parser->lexer);
18852 /* Parse the labels. */
18853 labels = cp_parser_asm_label_list (parser);
18856 if (goto_p && !labels_p)
18857 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
18859 else if (goto_p)
18860 missing = RT_COLON_SCOPE;
18862 /* Look for the closing `)'. */
18863 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
18864 missing ? missing : RT_CLOSE_PAREN))
18865 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18866 /*consume_paren=*/true);
18867 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18869 if (!invalid_inputs_p && !invalid_outputs_p)
18871 /* Create the ASM_EXPR. */
18872 if (parser->in_function_body)
18874 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
18875 inputs, clobbers, labels);
18876 /* If the extended syntax was not used, mark the ASM_EXPR. */
18877 if (!extended_p)
18879 tree temp = asm_stmt;
18880 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
18881 temp = TREE_OPERAND (temp, 0);
18883 ASM_INPUT_P (temp) = 1;
18886 else
18887 symtab->finalize_toplevel_asm (string);
18891 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
18892 type that comes from the decl-specifier-seq. */
18894 static tree
18895 strip_declarator_types (tree type, cp_declarator *declarator)
18897 for (cp_declarator *d = declarator; d;)
18898 switch (d->kind)
18900 case cdk_id:
18901 case cdk_decomp:
18902 case cdk_error:
18903 d = NULL;
18904 break;
18906 default:
18907 if (TYPE_PTRMEMFUNC_P (type))
18908 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
18909 type = TREE_TYPE (type);
18910 d = d->declarator;
18911 break;
18914 return type;
18917 /* Declarators [gram.dcl.decl] */
18919 /* Parse an init-declarator.
18921 init-declarator:
18922 declarator initializer [opt]
18924 GNU Extension:
18926 init-declarator:
18927 declarator asm-specification [opt] attributes [opt] initializer [opt]
18929 function-definition:
18930 decl-specifier-seq [opt] declarator ctor-initializer [opt]
18931 function-body
18932 decl-specifier-seq [opt] declarator function-try-block
18934 GNU Extension:
18936 function-definition:
18937 __extension__ function-definition
18939 TM Extension:
18941 function-definition:
18942 decl-specifier-seq [opt] declarator function-transaction-block
18944 The DECL_SPECIFIERS apply to this declarator. Returns a
18945 representation of the entity declared. If MEMBER_P is TRUE, then
18946 this declarator appears in a class scope. The new DECL created by
18947 this declarator is returned.
18949 The CHECKS are access checks that should be performed once we know
18950 what entity is being declared (and, therefore, what classes have
18951 befriended it).
18953 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
18954 for a function-definition here as well. If the declarator is a
18955 declarator for a function-definition, *FUNCTION_DEFINITION_P will
18956 be TRUE upon return. By that point, the function-definition will
18957 have been completely parsed.
18959 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
18960 is FALSE.
18962 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
18963 parsed declaration if it is an uninitialized single declarator not followed
18964 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
18965 if present, will not be consumed. If returned, this declarator will be
18966 created with SD_INITIALIZED but will not call cp_finish_decl.
18968 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
18969 and there is an initializer, the pointed location_t is set to the
18970 location of the '=' or `(', or '{' in C++11 token introducing the
18971 initializer. */
18973 static tree
18974 cp_parser_init_declarator (cp_parser* parser,
18975 cp_decl_specifier_seq *decl_specifiers,
18976 vec<deferred_access_check, va_gc> *checks,
18977 bool function_definition_allowed_p,
18978 bool member_p,
18979 int declares_class_or_enum,
18980 bool* function_definition_p,
18981 tree* maybe_range_for_decl,
18982 location_t* init_loc,
18983 tree* auto_result)
18985 cp_token *token = NULL, *asm_spec_start_token = NULL,
18986 *attributes_start_token = NULL;
18987 cp_declarator *declarator;
18988 tree prefix_attributes;
18989 tree attributes = NULL;
18990 tree asm_specification;
18991 tree initializer;
18992 tree decl = NULL_TREE;
18993 tree scope;
18994 int is_initialized;
18995 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
18996 initialized with "= ..", CPP_OPEN_PAREN if initialized with
18997 "(...)". */
18998 enum cpp_ttype initialization_kind;
18999 bool is_direct_init = false;
19000 bool is_non_constant_init;
19001 int ctor_dtor_or_conv_p;
19002 bool friend_p = cp_parser_friend_p (decl_specifiers);
19003 tree pushed_scope = NULL_TREE;
19004 bool range_for_decl_p = false;
19005 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19006 location_t tmp_init_loc = UNKNOWN_LOCATION;
19008 /* Gather the attributes that were provided with the
19009 decl-specifiers. */
19010 prefix_attributes = decl_specifiers->attributes;
19012 /* Assume that this is not the declarator for a function
19013 definition. */
19014 if (function_definition_p)
19015 *function_definition_p = false;
19017 /* Default arguments are only permitted for function parameters. */
19018 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19019 parser->default_arg_ok_p = false;
19021 /* Defer access checks while parsing the declarator; we cannot know
19022 what names are accessible until we know what is being
19023 declared. */
19024 resume_deferring_access_checks ();
19026 token = cp_lexer_peek_token (parser->lexer);
19028 cp_parser_declarator_kind cdk = CP_PARSER_DECLARATOR_NAMED;
19029 if (token->type == CPP_OPEN_PAREN
19030 && decl_specifiers->type
19031 && is_auto (decl_specifiers->type)
19032 && CLASS_PLACEHOLDER_TEMPLATE (decl_specifiers->type))
19034 // C++17 deduction guide.
19035 cdk = CP_PARSER_DECLARATOR_ABSTRACT;
19037 for (int i = 0; i < ds_last; ++i)
19038 if (i != ds_type_spec
19039 && decl_specifiers->locations[i]
19040 && !cp_parser_simulate_error (parser))
19041 error_at (decl_specifiers->locations[i],
19042 "decl-specifier in declaration of deduction guide");
19045 /* Parse the declarator. */
19046 declarator
19047 = cp_parser_declarator (parser, cdk,
19048 &ctor_dtor_or_conv_p,
19049 /*parenthesized_p=*/NULL,
19050 member_p, friend_p);
19051 /* Gather up the deferred checks. */
19052 stop_deferring_access_checks ();
19054 parser->default_arg_ok_p = saved_default_arg_ok_p;
19056 /* If the DECLARATOR was erroneous, there's no need to go
19057 further. */
19058 if (declarator == cp_error_declarator)
19059 return error_mark_node;
19061 if (cdk == CP_PARSER_DECLARATOR_ABSTRACT)
19063 gcc_assert (declarator->kind == cdk_function
19064 && !declarator->declarator);
19065 tree t = CLASS_PLACEHOLDER_TEMPLATE (decl_specifiers->type);
19066 declarator->declarator = make_id_declarator (NULL_TREE, dguide_name (t),
19067 sfk_none);
19068 declarator->declarator->id_loc
19069 = decl_specifiers->locations[ds_type_spec];
19072 /* Check that the number of template-parameter-lists is OK. */
19073 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19074 token->location))
19075 return error_mark_node;
19077 if (declares_class_or_enum & 2)
19078 cp_parser_check_for_definition_in_return_type (declarator,
19079 decl_specifiers->type,
19080 decl_specifiers->locations[ds_type_spec]);
19082 /* Figure out what scope the entity declared by the DECLARATOR is
19083 located in. `grokdeclarator' sometimes changes the scope, so
19084 we compute it now. */
19085 scope = get_scope_of_declarator (declarator);
19087 /* Perform any lookups in the declared type which were thought to be
19088 dependent, but are not in the scope of the declarator. */
19089 decl_specifiers->type
19090 = maybe_update_decl_type (decl_specifiers->type, scope);
19092 /* If we're allowing GNU extensions, look for an
19093 asm-specification. */
19094 if (cp_parser_allow_gnu_extensions_p (parser))
19096 /* Look for an asm-specification. */
19097 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19098 asm_specification = cp_parser_asm_specification_opt (parser);
19100 else
19101 asm_specification = NULL_TREE;
19103 /* Look for attributes. */
19104 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19105 attributes = cp_parser_attributes_opt (parser);
19107 /* Peek at the next token. */
19108 token = cp_lexer_peek_token (parser->lexer);
19110 bool bogus_implicit_tmpl = false;
19112 if (function_declarator_p (declarator))
19114 /* Check to see if the token indicates the start of a
19115 function-definition. */
19116 if (cp_parser_token_starts_function_definition_p (token))
19118 if (!function_definition_allowed_p)
19120 /* If a function-definition should not appear here, issue an
19121 error message. */
19122 cp_parser_error (parser,
19123 "a function-definition is not allowed here");
19124 return error_mark_node;
19127 location_t func_brace_location
19128 = cp_lexer_peek_token (parser->lexer)->location;
19130 /* Neither attributes nor an asm-specification are allowed
19131 on a function-definition. */
19132 if (asm_specification)
19133 error_at (asm_spec_start_token->location,
19134 "an asm-specification is not allowed "
19135 "on a function-definition");
19136 if (attributes)
19137 error_at (attributes_start_token->location,
19138 "attributes are not allowed "
19139 "on a function-definition");
19140 /* This is a function-definition. */
19141 *function_definition_p = true;
19143 /* Parse the function definition. */
19144 if (member_p)
19145 decl = cp_parser_save_member_function_body (parser,
19146 decl_specifiers,
19147 declarator,
19148 prefix_attributes);
19149 else
19150 decl =
19151 (cp_parser_function_definition_from_specifiers_and_declarator
19152 (parser, decl_specifiers, prefix_attributes, declarator));
19154 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19156 /* This is where the prologue starts... */
19157 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19158 = func_brace_location;
19161 return decl;
19164 else if (parser->fully_implicit_function_template_p)
19166 /* A non-template declaration involving a function parameter list
19167 containing an implicit template parameter will be made into a
19168 template. If the resulting declaration is not going to be an
19169 actual function then finish the template scope here to prevent it.
19170 An error message will be issued once we have a decl to talk about.
19172 FIXME probably we should do type deduction rather than create an
19173 implicit template, but the standard currently doesn't allow it. */
19174 bogus_implicit_tmpl = true;
19175 finish_fully_implicit_template (parser, NULL_TREE);
19178 /* [dcl.dcl]
19180 Only in function declarations for constructors, destructors, and
19181 type conversions can the decl-specifier-seq be omitted.
19183 We explicitly postpone this check past the point where we handle
19184 function-definitions because we tolerate function-definitions
19185 that are missing their return types in some modes. */
19186 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19188 cp_parser_error (parser,
19189 "expected constructor, destructor, or type conversion");
19190 return error_mark_node;
19193 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19194 if (token->type == CPP_EQ
19195 || token->type == CPP_OPEN_PAREN
19196 || token->type == CPP_OPEN_BRACE)
19198 is_initialized = SD_INITIALIZED;
19199 initialization_kind = token->type;
19200 if (maybe_range_for_decl)
19201 *maybe_range_for_decl = error_mark_node;
19202 tmp_init_loc = token->location;
19203 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19204 *init_loc = tmp_init_loc;
19206 if (token->type == CPP_EQ
19207 && function_declarator_p (declarator))
19209 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19210 if (t2->keyword == RID_DEFAULT)
19211 is_initialized = SD_DEFAULTED;
19212 else if (t2->keyword == RID_DELETE)
19213 is_initialized = SD_DELETED;
19216 else
19218 /* If the init-declarator isn't initialized and isn't followed by a
19219 `,' or `;', it's not a valid init-declarator. */
19220 if (token->type != CPP_COMMA
19221 && token->type != CPP_SEMICOLON)
19223 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19224 range_for_decl_p = true;
19225 else
19227 if (!maybe_range_for_decl)
19228 cp_parser_error (parser, "expected initializer");
19229 return error_mark_node;
19232 is_initialized = SD_UNINITIALIZED;
19233 initialization_kind = CPP_EOF;
19236 /* Because start_decl has side-effects, we should only call it if we
19237 know we're going ahead. By this point, we know that we cannot
19238 possibly be looking at any other construct. */
19239 cp_parser_commit_to_tentative_parse (parser);
19241 /* Enter the newly declared entry in the symbol table. If we're
19242 processing a declaration in a class-specifier, we wait until
19243 after processing the initializer. */
19244 if (!member_p)
19246 if (parser->in_unbraced_linkage_specification_p)
19247 decl_specifiers->storage_class = sc_extern;
19248 decl = start_decl (declarator, decl_specifiers,
19249 range_for_decl_p? SD_INITIALIZED : is_initialized,
19250 attributes, prefix_attributes, &pushed_scope);
19251 cp_finalize_omp_declare_simd (parser, decl);
19252 cp_finalize_oacc_routine (parser, decl, false);
19253 /* Adjust location of decl if declarator->id_loc is more appropriate:
19254 set, and decl wasn't merged with another decl, in which case its
19255 location would be different from input_location, and more accurate. */
19256 if (DECL_P (decl)
19257 && declarator->id_loc != UNKNOWN_LOCATION
19258 && DECL_SOURCE_LOCATION (decl) == input_location)
19259 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19261 else if (scope)
19262 /* Enter the SCOPE. That way unqualified names appearing in the
19263 initializer will be looked up in SCOPE. */
19264 pushed_scope = push_scope (scope);
19266 /* Perform deferred access control checks, now that we know in which
19267 SCOPE the declared entity resides. */
19268 if (!member_p && decl)
19270 tree saved_current_function_decl = NULL_TREE;
19272 /* If the entity being declared is a function, pretend that we
19273 are in its scope. If it is a `friend', it may have access to
19274 things that would not otherwise be accessible. */
19275 if (TREE_CODE (decl) == FUNCTION_DECL)
19277 saved_current_function_decl = current_function_decl;
19278 current_function_decl = decl;
19281 /* Perform access checks for template parameters. */
19282 cp_parser_perform_template_parameter_access_checks (checks);
19284 /* Perform the access control checks for the declarator and the
19285 decl-specifiers. */
19286 perform_deferred_access_checks (tf_warning_or_error);
19288 /* Restore the saved value. */
19289 if (TREE_CODE (decl) == FUNCTION_DECL)
19290 current_function_decl = saved_current_function_decl;
19293 /* Parse the initializer. */
19294 initializer = NULL_TREE;
19295 is_direct_init = false;
19296 is_non_constant_init = true;
19297 if (is_initialized)
19299 if (function_declarator_p (declarator))
19301 if (initialization_kind == CPP_EQ)
19302 initializer = cp_parser_pure_specifier (parser);
19303 else
19305 /* If the declaration was erroneous, we don't really
19306 know what the user intended, so just silently
19307 consume the initializer. */
19308 if (decl != error_mark_node)
19309 error_at (tmp_init_loc, "initializer provided for function");
19310 cp_parser_skip_to_closing_parenthesis (parser,
19311 /*recovering=*/true,
19312 /*or_comma=*/false,
19313 /*consume_paren=*/true);
19316 else
19318 /* We want to record the extra mangling scope for in-class
19319 initializers of class members and initializers of static data
19320 member templates. The former involves deferring
19321 parsing of the initializer until end of class as with default
19322 arguments. So right here we only handle the latter. */
19323 if (!member_p && processing_template_decl)
19324 start_lambda_scope (decl);
19325 initializer = cp_parser_initializer (parser,
19326 &is_direct_init,
19327 &is_non_constant_init);
19328 if (!member_p && processing_template_decl)
19329 finish_lambda_scope ();
19330 if (initializer == error_mark_node)
19331 cp_parser_skip_to_end_of_statement (parser);
19335 /* The old parser allows attributes to appear after a parenthesized
19336 initializer. Mark Mitchell proposed removing this functionality
19337 on the GCC mailing lists on 2002-08-13. This parser accepts the
19338 attributes -- but ignores them. */
19339 if (cp_parser_allow_gnu_extensions_p (parser)
19340 && initialization_kind == CPP_OPEN_PAREN)
19341 if (cp_parser_attributes_opt (parser))
19342 warning (OPT_Wattributes,
19343 "attributes after parenthesized initializer ignored");
19345 /* And now complain about a non-function implicit template. */
19346 if (bogus_implicit_tmpl && decl != error_mark_node)
19347 error_at (DECL_SOURCE_LOCATION (decl),
19348 "non-function %qD declared as implicit template", decl);
19350 /* For an in-class declaration, use `grokfield' to create the
19351 declaration. */
19352 if (member_p)
19354 if (pushed_scope)
19356 pop_scope (pushed_scope);
19357 pushed_scope = NULL_TREE;
19359 decl = grokfield (declarator, decl_specifiers,
19360 initializer, !is_non_constant_init,
19361 /*asmspec=*/NULL_TREE,
19362 chainon (attributes, prefix_attributes));
19363 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19364 cp_parser_save_default_args (parser, decl);
19365 cp_finalize_omp_declare_simd (parser, decl);
19366 cp_finalize_oacc_routine (parser, decl, false);
19369 /* Finish processing the declaration. But, skip member
19370 declarations. */
19371 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19373 cp_finish_decl (decl,
19374 initializer, !is_non_constant_init,
19375 asm_specification,
19376 /* If the initializer is in parentheses, then this is
19377 a direct-initialization, which means that an
19378 `explicit' constructor is OK. Otherwise, an
19379 `explicit' constructor cannot be used. */
19380 ((is_direct_init || !is_initialized)
19381 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19383 else if ((cxx_dialect != cxx98) && friend_p
19384 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19385 /* Core issue #226 (C++0x only): A default template-argument
19386 shall not be specified in a friend class template
19387 declaration. */
19388 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19389 /*is_partial=*/false, /*is_friend_decl=*/1);
19391 if (!friend_p && pushed_scope)
19392 pop_scope (pushed_scope);
19394 if (function_declarator_p (declarator)
19395 && parser->fully_implicit_function_template_p)
19397 if (member_p)
19398 decl = finish_fully_implicit_template (parser, decl);
19399 else
19400 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19403 if (auto_result && is_initialized && decl_specifiers->type
19404 && type_uses_auto (decl_specifiers->type))
19405 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19407 return decl;
19410 /* Parse a declarator.
19412 declarator:
19413 direct-declarator
19414 ptr-operator declarator
19416 abstract-declarator:
19417 ptr-operator abstract-declarator [opt]
19418 direct-abstract-declarator
19420 GNU Extensions:
19422 declarator:
19423 attributes [opt] direct-declarator
19424 attributes [opt] ptr-operator declarator
19426 abstract-declarator:
19427 attributes [opt] ptr-operator abstract-declarator [opt]
19428 attributes [opt] direct-abstract-declarator
19430 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19431 detect constructor, destructor or conversion operators. It is set
19432 to -1 if the declarator is a name, and +1 if it is a
19433 function. Otherwise it is set to zero. Usually you just want to
19434 test for >0, but internally the negative value is used.
19436 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19437 a decl-specifier-seq unless it declares a constructor, destructor,
19438 or conversion. It might seem that we could check this condition in
19439 semantic analysis, rather than parsing, but that makes it difficult
19440 to handle something like `f()'. We want to notice that there are
19441 no decl-specifiers, and therefore realize that this is an
19442 expression, not a declaration.)
19444 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19445 the declarator is a direct-declarator of the form "(...)".
19447 MEMBER_P is true iff this declarator is a member-declarator.
19449 FRIEND_P is true iff this declarator is a friend. */
19451 static cp_declarator *
19452 cp_parser_declarator (cp_parser* parser,
19453 cp_parser_declarator_kind dcl_kind,
19454 int* ctor_dtor_or_conv_p,
19455 bool* parenthesized_p,
19456 bool member_p, bool friend_p)
19458 cp_declarator *declarator;
19459 enum tree_code code;
19460 cp_cv_quals cv_quals;
19461 tree class_type;
19462 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19464 /* Assume this is not a constructor, destructor, or type-conversion
19465 operator. */
19466 if (ctor_dtor_or_conv_p)
19467 *ctor_dtor_or_conv_p = 0;
19469 if (cp_parser_allow_gnu_extensions_p (parser))
19470 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19472 /* Check for the ptr-operator production. */
19473 cp_parser_parse_tentatively (parser);
19474 /* Parse the ptr-operator. */
19475 code = cp_parser_ptr_operator (parser,
19476 &class_type,
19477 &cv_quals,
19478 &std_attributes);
19480 /* If that worked, then we have a ptr-operator. */
19481 if (cp_parser_parse_definitely (parser))
19483 /* If a ptr-operator was found, then this declarator was not
19484 parenthesized. */
19485 if (parenthesized_p)
19486 *parenthesized_p = true;
19487 /* The dependent declarator is optional if we are parsing an
19488 abstract-declarator. */
19489 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19490 cp_parser_parse_tentatively (parser);
19492 /* Parse the dependent declarator. */
19493 declarator = cp_parser_declarator (parser, dcl_kind,
19494 /*ctor_dtor_or_conv_p=*/NULL,
19495 /*parenthesized_p=*/NULL,
19496 /*member_p=*/false,
19497 friend_p);
19499 /* If we are parsing an abstract-declarator, we must handle the
19500 case where the dependent declarator is absent. */
19501 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19502 && !cp_parser_parse_definitely (parser))
19503 declarator = NULL;
19505 declarator = cp_parser_make_indirect_declarator
19506 (code, class_type, cv_quals, declarator, std_attributes);
19508 /* Everything else is a direct-declarator. */
19509 else
19511 if (parenthesized_p)
19512 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19513 CPP_OPEN_PAREN);
19514 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19515 ctor_dtor_or_conv_p,
19516 member_p, friend_p);
19519 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19520 declarator->attributes = gnu_attributes;
19521 return declarator;
19524 /* Parse a direct-declarator or direct-abstract-declarator.
19526 direct-declarator:
19527 declarator-id
19528 direct-declarator ( parameter-declaration-clause )
19529 cv-qualifier-seq [opt]
19530 ref-qualifier [opt]
19531 exception-specification [opt]
19532 direct-declarator [ constant-expression [opt] ]
19533 ( declarator )
19535 direct-abstract-declarator:
19536 direct-abstract-declarator [opt]
19537 ( parameter-declaration-clause )
19538 cv-qualifier-seq [opt]
19539 ref-qualifier [opt]
19540 exception-specification [opt]
19541 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19542 ( abstract-declarator )
19544 Returns a representation of the declarator. DCL_KIND is
19545 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19546 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19547 we are parsing a direct-declarator. It is
19548 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19549 of ambiguity we prefer an abstract declarator, as per
19550 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19551 as for cp_parser_declarator. */
19553 static cp_declarator *
19554 cp_parser_direct_declarator (cp_parser* parser,
19555 cp_parser_declarator_kind dcl_kind,
19556 int* ctor_dtor_or_conv_p,
19557 bool member_p, bool friend_p)
19559 cp_token *token;
19560 cp_declarator *declarator = NULL;
19561 tree scope = NULL_TREE;
19562 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19563 bool saved_in_declarator_p = parser->in_declarator_p;
19564 bool first = true;
19565 tree pushed_scope = NULL_TREE;
19567 while (true)
19569 /* Peek at the next token. */
19570 token = cp_lexer_peek_token (parser->lexer);
19571 if (token->type == CPP_OPEN_PAREN)
19573 /* This is either a parameter-declaration-clause, or a
19574 parenthesized declarator. When we know we are parsing a
19575 named declarator, it must be a parenthesized declarator
19576 if FIRST is true. For instance, `(int)' is a
19577 parameter-declaration-clause, with an omitted
19578 direct-abstract-declarator. But `((*))', is a
19579 parenthesized abstract declarator. Finally, when T is a
19580 template parameter `(T)' is a
19581 parameter-declaration-clause, and not a parenthesized
19582 named declarator.
19584 We first try and parse a parameter-declaration-clause,
19585 and then try a nested declarator (if FIRST is true).
19587 It is not an error for it not to be a
19588 parameter-declaration-clause, even when FIRST is
19589 false. Consider,
19591 int i (int);
19592 int i (3);
19594 The first is the declaration of a function while the
19595 second is the definition of a variable, including its
19596 initializer.
19598 Having seen only the parenthesis, we cannot know which of
19599 these two alternatives should be selected. Even more
19600 complex are examples like:
19602 int i (int (a));
19603 int i (int (3));
19605 The former is a function-declaration; the latter is a
19606 variable initialization.
19608 Thus again, we try a parameter-declaration-clause, and if
19609 that fails, we back out and return. */
19611 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19613 tree params;
19614 bool is_declarator = false;
19616 /* In a member-declarator, the only valid interpretation
19617 of a parenthesis is the start of a
19618 parameter-declaration-clause. (It is invalid to
19619 initialize a static data member with a parenthesized
19620 initializer; only the "=" form of initialization is
19621 permitted.) */
19622 if (!member_p)
19623 cp_parser_parse_tentatively (parser);
19625 /* Consume the `('. */
19626 cp_lexer_consume_token (parser->lexer);
19627 if (first)
19629 /* If this is going to be an abstract declarator, we're
19630 in a declarator and we can't have default args. */
19631 parser->default_arg_ok_p = false;
19632 parser->in_declarator_p = true;
19635 begin_scope (sk_function_parms, NULL_TREE);
19637 /* Parse the parameter-declaration-clause. */
19638 params = cp_parser_parameter_declaration_clause (parser);
19640 /* Consume the `)'. */
19641 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19643 /* If all went well, parse the cv-qualifier-seq,
19644 ref-qualifier and the exception-specification. */
19645 if (member_p || cp_parser_parse_definitely (parser))
19647 cp_cv_quals cv_quals;
19648 cp_virt_specifiers virt_specifiers;
19649 cp_ref_qualifier ref_qual;
19650 tree exception_specification;
19651 tree late_return;
19652 tree attrs;
19653 bool memfn = (member_p || (pushed_scope
19654 && CLASS_TYPE_P (pushed_scope)));
19656 is_declarator = true;
19658 if (ctor_dtor_or_conv_p)
19659 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
19660 first = false;
19662 /* Parse the cv-qualifier-seq. */
19663 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19664 /* Parse the ref-qualifier. */
19665 ref_qual = cp_parser_ref_qualifier_opt (parser);
19666 /* Parse the tx-qualifier. */
19667 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
19668 /* And the exception-specification. */
19669 exception_specification
19670 = cp_parser_exception_specification_opt (parser);
19672 attrs = cp_parser_std_attribute_spec_seq (parser);
19674 /* In here, we handle cases where attribute is used after
19675 the function declaration. For example:
19676 void func (int x) __attribute__((vector(..))); */
19677 tree gnu_attrs = NULL_TREE;
19678 if (flag_cilkplus
19679 && cp_next_tokens_can_be_gnu_attribute_p (parser))
19681 cp_parser_parse_tentatively (parser);
19682 tree attr = cp_parser_gnu_attributes_opt (parser);
19683 if (cp_lexer_next_token_is_not (parser->lexer,
19684 CPP_SEMICOLON)
19685 && cp_lexer_next_token_is_not (parser->lexer,
19686 CPP_OPEN_BRACE))
19687 cp_parser_abort_tentative_parse (parser);
19688 else if (!cp_parser_parse_definitely (parser))
19690 else
19691 gnu_attrs = attr;
19693 tree requires_clause = NULL_TREE;
19694 late_return = (cp_parser_late_return_type_opt
19695 (parser, declarator, requires_clause,
19696 memfn ? cv_quals : -1));
19698 /* Parse the virt-specifier-seq. */
19699 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19701 /* Create the function-declarator. */
19702 declarator = make_call_declarator (declarator,
19703 params,
19704 cv_quals,
19705 virt_specifiers,
19706 ref_qual,
19707 tx_qual,
19708 exception_specification,
19709 late_return,
19710 requires_clause);
19711 declarator->std_attributes = attrs;
19712 declarator->attributes = gnu_attrs;
19713 /* Any subsequent parameter lists are to do with
19714 return type, so are not those of the declared
19715 function. */
19716 parser->default_arg_ok_p = false;
19719 /* Remove the function parms from scope. */
19720 pop_bindings_and_leave_scope ();
19722 if (is_declarator)
19723 /* Repeat the main loop. */
19724 continue;
19727 /* If this is the first, we can try a parenthesized
19728 declarator. */
19729 if (first)
19731 bool saved_in_type_id_in_expr_p;
19733 parser->default_arg_ok_p = saved_default_arg_ok_p;
19734 parser->in_declarator_p = saved_in_declarator_p;
19736 /* Consume the `('. */
19737 cp_lexer_consume_token (parser->lexer);
19738 /* Parse the nested declarator. */
19739 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19740 parser->in_type_id_in_expr_p = true;
19741 declarator
19742 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
19743 /*parenthesized_p=*/NULL,
19744 member_p, friend_p);
19745 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19746 first = false;
19747 /* Expect a `)'. */
19748 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
19749 declarator = cp_error_declarator;
19750 if (declarator == cp_error_declarator)
19751 break;
19753 goto handle_declarator;
19755 /* Otherwise, we must be done. */
19756 else
19757 break;
19759 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19760 && token->type == CPP_OPEN_SQUARE
19761 && !cp_next_tokens_can_be_attribute_p (parser))
19763 /* Parse an array-declarator. */
19764 tree bounds, attrs;
19766 if (ctor_dtor_or_conv_p)
19767 *ctor_dtor_or_conv_p = 0;
19769 first = false;
19770 parser->default_arg_ok_p = false;
19771 parser->in_declarator_p = true;
19772 /* Consume the `['. */
19773 cp_lexer_consume_token (parser->lexer);
19774 /* Peek at the next token. */
19775 token = cp_lexer_peek_token (parser->lexer);
19776 /* If the next token is `]', then there is no
19777 constant-expression. */
19778 if (token->type != CPP_CLOSE_SQUARE)
19780 bool non_constant_p;
19781 bounds
19782 = cp_parser_constant_expression (parser,
19783 /*allow_non_constant=*/true,
19784 &non_constant_p);
19785 if (!non_constant_p)
19786 /* OK */;
19787 else if (error_operand_p (bounds))
19788 /* Already gave an error. */;
19789 else if (!parser->in_function_body
19790 || current_binding_level->kind == sk_function_parms)
19792 /* Normally, the array bound must be an integral constant
19793 expression. However, as an extension, we allow VLAs
19794 in function scopes as long as they aren't part of a
19795 parameter declaration. */
19796 cp_parser_error (parser,
19797 "array bound is not an integer constant");
19798 bounds = error_mark_node;
19800 else if (processing_template_decl
19801 && !type_dependent_expression_p (bounds))
19803 /* Remember this wasn't a constant-expression. */
19804 bounds = build_nop (TREE_TYPE (bounds), bounds);
19805 TREE_SIDE_EFFECTS (bounds) = 1;
19808 else
19809 bounds = NULL_TREE;
19810 /* Look for the closing `]'. */
19811 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
19813 declarator = cp_error_declarator;
19814 break;
19817 attrs = cp_parser_std_attribute_spec_seq (parser);
19818 declarator = make_array_declarator (declarator, bounds);
19819 declarator->std_attributes = attrs;
19821 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
19824 tree qualifying_scope;
19825 tree unqualified_name;
19826 tree attrs;
19827 special_function_kind sfk;
19828 bool abstract_ok;
19829 bool pack_expansion_p = false;
19830 cp_token *declarator_id_start_token;
19832 /* Parse a declarator-id */
19833 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
19834 if (abstract_ok)
19836 cp_parser_parse_tentatively (parser);
19838 /* If we see an ellipsis, we should be looking at a
19839 parameter pack. */
19840 if (token->type == CPP_ELLIPSIS)
19842 /* Consume the `...' */
19843 cp_lexer_consume_token (parser->lexer);
19845 pack_expansion_p = true;
19849 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
19850 unqualified_name
19851 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
19852 qualifying_scope = parser->scope;
19853 if (abstract_ok)
19855 bool okay = false;
19857 if (!unqualified_name && pack_expansion_p)
19859 /* Check whether an error occurred. */
19860 okay = !cp_parser_error_occurred (parser);
19862 /* We already consumed the ellipsis to mark a
19863 parameter pack, but we have no way to report it,
19864 so abort the tentative parse. We will be exiting
19865 immediately anyway. */
19866 cp_parser_abort_tentative_parse (parser);
19868 else
19869 okay = cp_parser_parse_definitely (parser);
19871 if (!okay)
19872 unqualified_name = error_mark_node;
19873 else if (unqualified_name
19874 && (qualifying_scope
19875 || (!identifier_p (unqualified_name))))
19877 cp_parser_error (parser, "expected unqualified-id");
19878 unqualified_name = error_mark_node;
19882 if (!unqualified_name)
19883 return NULL;
19884 if (unqualified_name == error_mark_node)
19886 declarator = cp_error_declarator;
19887 pack_expansion_p = false;
19888 declarator->parameter_pack_p = false;
19889 break;
19892 attrs = cp_parser_std_attribute_spec_seq (parser);
19894 if (qualifying_scope && at_namespace_scope_p ()
19895 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
19897 /* In the declaration of a member of a template class
19898 outside of the class itself, the SCOPE will sometimes
19899 be a TYPENAME_TYPE. For example, given:
19901 template <typename T>
19902 int S<T>::R::i = 3;
19904 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
19905 this context, we must resolve S<T>::R to an ordinary
19906 type, rather than a typename type.
19908 The reason we normally avoid resolving TYPENAME_TYPEs
19909 is that a specialization of `S' might render
19910 `S<T>::R' not a type. However, if `S' is
19911 specialized, then this `i' will not be used, so there
19912 is no harm in resolving the types here. */
19913 tree type;
19915 /* Resolve the TYPENAME_TYPE. */
19916 type = resolve_typename_type (qualifying_scope,
19917 /*only_current_p=*/false);
19918 /* If that failed, the declarator is invalid. */
19919 if (TREE_CODE (type) == TYPENAME_TYPE)
19921 if (typedef_variant_p (type))
19922 error_at (declarator_id_start_token->location,
19923 "cannot define member of dependent typedef "
19924 "%qT", type);
19925 else
19926 error_at (declarator_id_start_token->location,
19927 "%<%T::%E%> is not a type",
19928 TYPE_CONTEXT (qualifying_scope),
19929 TYPE_IDENTIFIER (qualifying_scope));
19931 qualifying_scope = type;
19934 sfk = sfk_none;
19936 if (unqualified_name)
19938 tree class_type;
19940 if (qualifying_scope
19941 && CLASS_TYPE_P (qualifying_scope))
19942 class_type = qualifying_scope;
19943 else
19944 class_type = current_class_type;
19946 if (TREE_CODE (unqualified_name) == TYPE_DECL)
19948 tree name_type = TREE_TYPE (unqualified_name);
19949 if (class_type && same_type_p (name_type, class_type))
19951 if (qualifying_scope
19952 && CLASSTYPE_USE_TEMPLATE (name_type))
19954 error_at (declarator_id_start_token->location,
19955 "invalid use of constructor as a template");
19956 inform (declarator_id_start_token->location,
19957 "use %<%T::%D%> instead of %<%T::%D%> to "
19958 "name the constructor in a qualified name",
19959 class_type,
19960 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
19961 class_type, name_type);
19962 declarator = cp_error_declarator;
19963 break;
19965 else
19966 unqualified_name = constructor_name (class_type);
19968 else
19970 /* We do not attempt to print the declarator
19971 here because we do not have enough
19972 information about its original syntactic
19973 form. */
19974 cp_parser_error (parser, "invalid declarator");
19975 declarator = cp_error_declarator;
19976 break;
19980 if (class_type)
19982 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
19983 sfk = sfk_destructor;
19984 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
19985 sfk = sfk_conversion;
19986 else if (/* There's no way to declare a constructor
19987 for an unnamed type, even if the type
19988 got a name for linkage purposes. */
19989 !TYPE_WAS_UNNAMED (class_type)
19990 /* Handle correctly (c++/19200):
19992 struct S {
19993 struct T{};
19994 friend void S(T);
19997 and also:
19999 namespace N {
20000 void S();
20003 struct S {
20004 friend void N::S();
20005 }; */
20006 && !(friend_p
20007 && class_type != qualifying_scope)
20008 && constructor_name_p (unqualified_name,
20009 class_type))
20011 unqualified_name = constructor_name (class_type);
20012 sfk = sfk_constructor;
20014 else if (is_overloaded_fn (unqualified_name)
20015 && DECL_CONSTRUCTOR_P (get_first_fn
20016 (unqualified_name)))
20017 sfk = sfk_constructor;
20019 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20020 *ctor_dtor_or_conv_p = -1;
20023 declarator = make_id_declarator (qualifying_scope,
20024 unqualified_name,
20025 sfk);
20026 declarator->std_attributes = attrs;
20027 declarator->id_loc = token->location;
20028 declarator->parameter_pack_p = pack_expansion_p;
20030 if (pack_expansion_p)
20031 maybe_warn_variadic_templates ();
20034 handle_declarator:;
20035 scope = get_scope_of_declarator (declarator);
20036 if (scope)
20038 /* Any names that appear after the declarator-id for a
20039 member are looked up in the containing scope. */
20040 if (at_function_scope_p ())
20042 /* But declarations with qualified-ids can't appear in a
20043 function. */
20044 cp_parser_error (parser, "qualified-id in declaration");
20045 declarator = cp_error_declarator;
20046 break;
20048 pushed_scope = push_scope (scope);
20050 parser->in_declarator_p = true;
20051 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20052 || (declarator && declarator->kind == cdk_id))
20053 /* Default args are only allowed on function
20054 declarations. */
20055 parser->default_arg_ok_p = saved_default_arg_ok_p;
20056 else
20057 parser->default_arg_ok_p = false;
20059 first = false;
20061 /* We're done. */
20062 else
20063 break;
20066 /* For an abstract declarator, we might wind up with nothing at this
20067 point. That's an error; the declarator is not optional. */
20068 if (!declarator)
20069 cp_parser_error (parser, "expected declarator");
20071 /* If we entered a scope, we must exit it now. */
20072 if (pushed_scope)
20073 pop_scope (pushed_scope);
20075 parser->default_arg_ok_p = saved_default_arg_ok_p;
20076 parser->in_declarator_p = saved_in_declarator_p;
20078 return declarator;
20081 /* Parse a ptr-operator.
20083 ptr-operator:
20084 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20085 * cv-qualifier-seq [opt]
20087 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20088 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20090 GNU Extension:
20092 ptr-operator:
20093 & cv-qualifier-seq [opt]
20095 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20096 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20097 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20098 filled in with the TYPE containing the member. *CV_QUALS is
20099 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20100 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20101 Note that the tree codes returned by this function have nothing
20102 to do with the types of trees that will be eventually be created
20103 to represent the pointer or reference type being parsed. They are
20104 just constants with suggestive names. */
20105 static enum tree_code
20106 cp_parser_ptr_operator (cp_parser* parser,
20107 tree* type,
20108 cp_cv_quals *cv_quals,
20109 tree *attributes)
20111 enum tree_code code = ERROR_MARK;
20112 cp_token *token;
20113 tree attrs = NULL_TREE;
20115 /* Assume that it's not a pointer-to-member. */
20116 *type = NULL_TREE;
20117 /* And that there are no cv-qualifiers. */
20118 *cv_quals = TYPE_UNQUALIFIED;
20120 /* Peek at the next token. */
20121 token = cp_lexer_peek_token (parser->lexer);
20123 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20124 if (token->type == CPP_MULT)
20125 code = INDIRECT_REF;
20126 else if (token->type == CPP_AND)
20127 code = ADDR_EXPR;
20128 else if ((cxx_dialect != cxx98) &&
20129 token->type == CPP_AND_AND) /* C++0x only */
20130 code = NON_LVALUE_EXPR;
20132 if (code != ERROR_MARK)
20134 /* Consume the `*', `&' or `&&'. */
20135 cp_lexer_consume_token (parser->lexer);
20137 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20138 `&', if we are allowing GNU extensions. (The only qualifier
20139 that can legally appear after `&' is `restrict', but that is
20140 enforced during semantic analysis. */
20141 if (code == INDIRECT_REF
20142 || cp_parser_allow_gnu_extensions_p (parser))
20143 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20145 attrs = cp_parser_std_attribute_spec_seq (parser);
20146 if (attributes != NULL)
20147 *attributes = attrs;
20149 else
20151 /* Try the pointer-to-member case. */
20152 cp_parser_parse_tentatively (parser);
20153 /* Look for the optional `::' operator. */
20154 cp_parser_global_scope_opt (parser,
20155 /*current_scope_valid_p=*/false);
20156 /* Look for the nested-name specifier. */
20157 token = cp_lexer_peek_token (parser->lexer);
20158 cp_parser_nested_name_specifier (parser,
20159 /*typename_keyword_p=*/false,
20160 /*check_dependency_p=*/true,
20161 /*type_p=*/false,
20162 /*is_declaration=*/false);
20163 /* If we found it, and the next token is a `*', then we are
20164 indeed looking at a pointer-to-member operator. */
20165 if (!cp_parser_error_occurred (parser)
20166 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20168 /* Indicate that the `*' operator was used. */
20169 code = INDIRECT_REF;
20171 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20172 error_at (token->location, "%qD is a namespace", parser->scope);
20173 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20174 error_at (token->location, "cannot form pointer to member of "
20175 "non-class %q#T", parser->scope);
20176 else
20178 /* The type of which the member is a member is given by the
20179 current SCOPE. */
20180 *type = parser->scope;
20181 /* The next name will not be qualified. */
20182 parser->scope = NULL_TREE;
20183 parser->qualifying_scope = NULL_TREE;
20184 parser->object_scope = NULL_TREE;
20185 /* Look for optional c++11 attributes. */
20186 attrs = cp_parser_std_attribute_spec_seq (parser);
20187 if (attributes != NULL)
20188 *attributes = attrs;
20189 /* Look for the optional cv-qualifier-seq. */
20190 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20193 /* If that didn't work we don't have a ptr-operator. */
20194 if (!cp_parser_parse_definitely (parser))
20195 cp_parser_error (parser, "expected ptr-operator");
20198 return code;
20201 /* Parse an (optional) cv-qualifier-seq.
20203 cv-qualifier-seq:
20204 cv-qualifier cv-qualifier-seq [opt]
20206 cv-qualifier:
20207 const
20208 volatile
20210 GNU Extension:
20212 cv-qualifier:
20213 __restrict__
20215 Returns a bitmask representing the cv-qualifiers. */
20217 static cp_cv_quals
20218 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20220 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20222 while (true)
20224 cp_token *token;
20225 cp_cv_quals cv_qualifier;
20227 /* Peek at the next token. */
20228 token = cp_lexer_peek_token (parser->lexer);
20229 /* See if it's a cv-qualifier. */
20230 switch (token->keyword)
20232 case RID_CONST:
20233 cv_qualifier = TYPE_QUAL_CONST;
20234 break;
20236 case RID_VOLATILE:
20237 cv_qualifier = TYPE_QUAL_VOLATILE;
20238 break;
20240 case RID_RESTRICT:
20241 cv_qualifier = TYPE_QUAL_RESTRICT;
20242 break;
20244 default:
20245 cv_qualifier = TYPE_UNQUALIFIED;
20246 break;
20249 if (!cv_qualifier)
20250 break;
20252 if (cv_quals & cv_qualifier)
20254 error_at (token->location, "duplicate cv-qualifier");
20255 cp_lexer_purge_token (parser->lexer);
20257 else
20259 cp_lexer_consume_token (parser->lexer);
20260 cv_quals |= cv_qualifier;
20264 return cv_quals;
20267 /* Parse an (optional) ref-qualifier
20269 ref-qualifier:
20273 Returns cp_ref_qualifier representing ref-qualifier. */
20275 static cp_ref_qualifier
20276 cp_parser_ref_qualifier_opt (cp_parser* parser)
20278 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20280 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20281 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20282 return ref_qual;
20284 while (true)
20286 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20287 cp_token *token = cp_lexer_peek_token (parser->lexer);
20289 switch (token->type)
20291 case CPP_AND:
20292 curr_ref_qual = REF_QUAL_LVALUE;
20293 break;
20295 case CPP_AND_AND:
20296 curr_ref_qual = REF_QUAL_RVALUE;
20297 break;
20299 default:
20300 curr_ref_qual = REF_QUAL_NONE;
20301 break;
20304 if (!curr_ref_qual)
20305 break;
20306 else if (ref_qual)
20308 error_at (token->location, "multiple ref-qualifiers");
20309 cp_lexer_purge_token (parser->lexer);
20311 else
20313 ref_qual = curr_ref_qual;
20314 cp_lexer_consume_token (parser->lexer);
20318 return ref_qual;
20321 /* Parse an optional tx-qualifier.
20323 tx-qualifier:
20324 transaction_safe
20325 transaction_safe_dynamic */
20327 static tree
20328 cp_parser_tx_qualifier_opt (cp_parser *parser)
20330 cp_token *token = cp_lexer_peek_token (parser->lexer);
20331 if (token->type == CPP_NAME)
20333 tree name = token->u.value;
20334 const char *p = IDENTIFIER_POINTER (name);
20335 const int len = strlen ("transaction_safe");
20336 if (!strncmp (p, "transaction_safe", len))
20338 p += len;
20339 if (*p == '\0'
20340 || !strcmp (p, "_dynamic"))
20342 cp_lexer_consume_token (parser->lexer);
20343 if (!flag_tm)
20345 error ("%E requires %<-fgnu-tm%>", name);
20346 return NULL_TREE;
20348 else
20349 return name;
20353 return NULL_TREE;
20356 /* Parse an (optional) virt-specifier-seq.
20358 virt-specifier-seq:
20359 virt-specifier virt-specifier-seq [opt]
20361 virt-specifier:
20362 override
20363 final
20365 Returns a bitmask representing the virt-specifiers. */
20367 static cp_virt_specifiers
20368 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20370 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20372 while (true)
20374 cp_token *token;
20375 cp_virt_specifiers virt_specifier;
20377 /* Peek at the next token. */
20378 token = cp_lexer_peek_token (parser->lexer);
20379 /* See if it's a virt-specifier-qualifier. */
20380 if (token->type != CPP_NAME)
20381 break;
20382 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
20384 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20385 virt_specifier = VIRT_SPEC_OVERRIDE;
20387 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
20389 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20390 virt_specifier = VIRT_SPEC_FINAL;
20392 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
20394 virt_specifier = VIRT_SPEC_FINAL;
20396 else
20397 break;
20399 if (virt_specifiers & virt_specifier)
20401 error_at (token->location, "duplicate virt-specifier");
20402 cp_lexer_purge_token (parser->lexer);
20404 else
20406 cp_lexer_consume_token (parser->lexer);
20407 virt_specifiers |= virt_specifier;
20410 return virt_specifiers;
20413 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20414 is in scope even though it isn't real. */
20416 void
20417 inject_this_parameter (tree ctype, cp_cv_quals quals)
20419 tree this_parm;
20421 if (current_class_ptr)
20423 /* We don't clear this between NSDMIs. Is it already what we want? */
20424 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20425 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
20426 && cp_type_quals (type) == quals)
20427 return;
20430 this_parm = build_this_parm (ctype, quals);
20431 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20432 current_class_ptr = NULL_TREE;
20433 current_class_ref
20434 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
20435 current_class_ptr = this_parm;
20438 /* Return true iff our current scope is a non-static data member
20439 initializer. */
20441 bool
20442 parsing_nsdmi (void)
20444 /* We recognize NSDMI context by the context-less 'this' pointer set up
20445 by the function above. */
20446 if (current_class_ptr
20447 && TREE_CODE (current_class_ptr) == PARM_DECL
20448 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20449 return true;
20450 return false;
20453 /* Parse a late-specified return type, if any. This is not a separate
20454 non-terminal, but part of a function declarator, which looks like
20456 -> trailing-type-specifier-seq abstract-declarator(opt)
20458 Returns the type indicated by the type-id.
20460 In addition to this, parse any queued up #pragma omp declare simd
20461 clauses, Cilk Plus SIMD-enabled functions' vector attributes, and
20462 #pragma acc routine clauses.
20464 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20465 function. */
20467 static tree
20468 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20469 tree& requires_clause, cp_cv_quals quals)
20471 cp_token *token;
20472 tree type = NULL_TREE;
20473 bool declare_simd_p = (parser->omp_declare_simd
20474 && declarator
20475 && declarator->kind == cdk_id);
20477 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
20478 && declarator && declarator->kind == cdk_id);
20480 bool oacc_routine_p = (parser->oacc_routine
20481 && declarator
20482 && declarator->kind == cdk_id);
20484 /* Peek at the next token. */
20485 token = cp_lexer_peek_token (parser->lexer);
20486 /* A late-specified return type is indicated by an initial '->'. */
20487 if (token->type != CPP_DEREF
20488 && token->keyword != RID_REQUIRES
20489 && !(token->type == CPP_NAME
20490 && token->u.value == ridpointers[RID_REQUIRES])
20491 && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
20492 return NULL_TREE;
20494 tree save_ccp = current_class_ptr;
20495 tree save_ccr = current_class_ref;
20496 if (quals >= 0)
20498 /* DR 1207: 'this' is in scope in the trailing return type. */
20499 inject_this_parameter (current_class_type, quals);
20502 if (token->type == CPP_DEREF)
20504 /* Consume the ->. */
20505 cp_lexer_consume_token (parser->lexer);
20507 type = cp_parser_trailing_type_id (parser);
20510 /* Function declarations may be followed by a trailing
20511 requires-clause. */
20512 requires_clause = cp_parser_requires_clause_opt (parser);
20514 if (cilk_simd_fn_vector_p)
20515 declarator->attributes
20516 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
20517 declarator->attributes);
20518 if (declare_simd_p)
20519 declarator->attributes
20520 = cp_parser_late_parsing_omp_declare_simd (parser,
20521 declarator->attributes);
20522 if (oacc_routine_p)
20523 declarator->attributes
20524 = cp_parser_late_parsing_oacc_routine (parser,
20525 declarator->attributes);
20527 if (quals >= 0)
20529 current_class_ptr = save_ccp;
20530 current_class_ref = save_ccr;
20533 return type;
20536 /* Parse a declarator-id.
20538 declarator-id:
20539 id-expression
20540 :: [opt] nested-name-specifier [opt] type-name
20542 In the `id-expression' case, the value returned is as for
20543 cp_parser_id_expression if the id-expression was an unqualified-id.
20544 If the id-expression was a qualified-id, then a SCOPE_REF is
20545 returned. The first operand is the scope (either a NAMESPACE_DECL
20546 or TREE_TYPE), but the second is still just a representation of an
20547 unqualified-id. */
20549 static tree
20550 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20552 tree id;
20553 /* The expression must be an id-expression. Assume that qualified
20554 names are the names of types so that:
20556 template <class T>
20557 int S<T>::R::i = 3;
20559 will work; we must treat `S<T>::R' as the name of a type.
20560 Similarly, assume that qualified names are templates, where
20561 required, so that:
20563 template <class T>
20564 int S<T>::R<T>::i = 3;
20566 will work, too. */
20567 id = cp_parser_id_expression (parser,
20568 /*template_keyword_p=*/false,
20569 /*check_dependency_p=*/false,
20570 /*template_p=*/NULL,
20571 /*declarator_p=*/true,
20572 optional_p);
20573 if (id && BASELINK_P (id))
20574 id = BASELINK_FUNCTIONS (id);
20575 return id;
20578 /* Parse a type-id.
20580 type-id:
20581 type-specifier-seq abstract-declarator [opt]
20583 Returns the TYPE specified. */
20585 static tree
20586 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
20587 bool is_trailing_return)
20589 cp_decl_specifier_seq type_specifier_seq;
20590 cp_declarator *abstract_declarator;
20592 /* Parse the type-specifier-seq. */
20593 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
20594 is_trailing_return,
20595 &type_specifier_seq);
20596 if (is_template_arg && type_specifier_seq.type
20597 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
20598 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
20599 /* A bare template name as a template argument is a template template
20600 argument, not a placeholder, so fail parsing it as a type argument. */
20602 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
20603 cp_parser_simulate_error (parser);
20604 return error_mark_node;
20606 if (type_specifier_seq.type == error_mark_node)
20607 return error_mark_node;
20609 /* There might or might not be an abstract declarator. */
20610 cp_parser_parse_tentatively (parser);
20611 /* Look for the declarator. */
20612 abstract_declarator
20613 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
20614 /*parenthesized_p=*/NULL,
20615 /*member_p=*/false,
20616 /*friend_p=*/false);
20617 /* Check to see if there really was a declarator. */
20618 if (!cp_parser_parse_definitely (parser))
20619 abstract_declarator = NULL;
20621 if (type_specifier_seq.type
20622 /* The concepts TS allows 'auto' as a type-id. */
20623 && (!flag_concepts || parser->in_type_id_in_expr_p)
20624 /* None of the valid uses of 'auto' in C++14 involve the type-id
20625 nonterminal, but it is valid in a trailing-return-type. */
20626 && !(cxx_dialect >= cxx14 && is_trailing_return))
20627 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
20629 /* A type-id with type 'auto' is only ok if the abstract declarator
20630 is a function declarator with a late-specified return type.
20632 A type-id with 'auto' is also valid in a trailing-return-type
20633 in a compound-requirement. */
20634 if (abstract_declarator
20635 && abstract_declarator->kind == cdk_function
20636 && abstract_declarator->u.function.late_return_type)
20637 /* OK */;
20638 else if (parser->in_result_type_constraint_p)
20639 /* OK */;
20640 else
20642 location_t loc = type_specifier_seq.locations[ds_type_spec];
20643 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
20645 error_at (loc, "missing template arguments after %qT",
20646 auto_node);
20647 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
20648 tmpl);
20650 else
20651 error_at (loc, "invalid use of %qT", auto_node);
20652 return error_mark_node;
20656 return groktypename (&type_specifier_seq, abstract_declarator,
20657 is_template_arg);
20660 static tree
20661 cp_parser_type_id (cp_parser *parser)
20663 return cp_parser_type_id_1 (parser, false, false);
20666 static tree
20667 cp_parser_template_type_arg (cp_parser *parser)
20669 tree r;
20670 const char *saved_message = parser->type_definition_forbidden_message;
20671 parser->type_definition_forbidden_message
20672 = G_("types may not be defined in template arguments");
20673 r = cp_parser_type_id_1 (parser, true, false);
20674 parser->type_definition_forbidden_message = saved_message;
20675 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
20677 error ("invalid use of %<auto%> in template argument");
20678 r = error_mark_node;
20680 return r;
20683 static tree
20684 cp_parser_trailing_type_id (cp_parser *parser)
20686 return cp_parser_type_id_1 (parser, false, true);
20689 /* Parse a type-specifier-seq.
20691 type-specifier-seq:
20692 type-specifier type-specifier-seq [opt]
20694 GNU extension:
20696 type-specifier-seq:
20697 attributes type-specifier-seq [opt]
20699 If IS_DECLARATION is true, we are at the start of a "condition" or
20700 exception-declaration, so we might be followed by a declarator-id.
20702 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
20703 i.e. we've just seen "->".
20705 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
20707 static void
20708 cp_parser_type_specifier_seq (cp_parser* parser,
20709 bool is_declaration,
20710 bool is_trailing_return,
20711 cp_decl_specifier_seq *type_specifier_seq)
20713 bool seen_type_specifier = false;
20714 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
20715 cp_token *start_token = NULL;
20717 /* Clear the TYPE_SPECIFIER_SEQ. */
20718 clear_decl_specs (type_specifier_seq);
20720 /* In the context of a trailing return type, enum E { } is an
20721 elaborated-type-specifier followed by a function-body, not an
20722 enum-specifier. */
20723 if (is_trailing_return)
20724 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
20726 /* Parse the type-specifiers and attributes. */
20727 while (true)
20729 tree type_specifier;
20730 bool is_cv_qualifier;
20732 /* Check for attributes first. */
20733 if (cp_next_tokens_can_be_attribute_p (parser))
20735 type_specifier_seq->attributes =
20736 chainon (type_specifier_seq->attributes,
20737 cp_parser_attributes_opt (parser));
20738 continue;
20741 /* record the token of the beginning of the type specifier seq,
20742 for error reporting purposes*/
20743 if (!start_token)
20744 start_token = cp_lexer_peek_token (parser->lexer);
20746 /* Look for the type-specifier. */
20747 type_specifier = cp_parser_type_specifier (parser,
20748 flags,
20749 type_specifier_seq,
20750 /*is_declaration=*/false,
20751 NULL,
20752 &is_cv_qualifier);
20753 if (!type_specifier)
20755 /* If the first type-specifier could not be found, this is not a
20756 type-specifier-seq at all. */
20757 if (!seen_type_specifier)
20759 /* Set in_declarator_p to avoid skipping to the semicolon. */
20760 int in_decl = parser->in_declarator_p;
20761 parser->in_declarator_p = true;
20763 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
20764 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
20765 cp_parser_error (parser, "expected type-specifier");
20767 parser->in_declarator_p = in_decl;
20769 type_specifier_seq->type = error_mark_node;
20770 return;
20772 /* If subsequent type-specifiers could not be found, the
20773 type-specifier-seq is complete. */
20774 break;
20777 seen_type_specifier = true;
20778 /* The standard says that a condition can be:
20780 type-specifier-seq declarator = assignment-expression
20782 However, given:
20784 struct S {};
20785 if (int S = ...)
20787 we should treat the "S" as a declarator, not as a
20788 type-specifier. The standard doesn't say that explicitly for
20789 type-specifier-seq, but it does say that for
20790 decl-specifier-seq in an ordinary declaration. Perhaps it
20791 would be clearer just to allow a decl-specifier-seq here, and
20792 then add a semantic restriction that if any decl-specifiers
20793 that are not type-specifiers appear, the program is invalid. */
20794 if (is_declaration && !is_cv_qualifier)
20795 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
20799 /* Return whether the function currently being declared has an associated
20800 template parameter list. */
20802 static bool
20803 function_being_declared_is_template_p (cp_parser* parser)
20805 if (!current_template_parms || processing_template_parmlist)
20806 return false;
20808 if (parser->implicit_template_scope)
20809 return true;
20811 if (at_class_scope_p ()
20812 && TYPE_BEING_DEFINED (current_class_type))
20813 return parser->num_template_parameter_lists != 0;
20815 return ((int) parser->num_template_parameter_lists > template_class_depth
20816 (current_class_type));
20819 /* Parse a parameter-declaration-clause.
20821 parameter-declaration-clause:
20822 parameter-declaration-list [opt] ... [opt]
20823 parameter-declaration-list , ...
20825 Returns a representation for the parameter declarations. A return
20826 value of NULL indicates a parameter-declaration-clause consisting
20827 only of an ellipsis. */
20829 static tree
20830 cp_parser_parameter_declaration_clause (cp_parser* parser)
20832 tree parameters;
20833 cp_token *token;
20834 bool ellipsis_p;
20835 bool is_error;
20837 struct cleanup {
20838 cp_parser* parser;
20839 int auto_is_implicit_function_template_parm_p;
20840 ~cleanup() {
20841 parser->auto_is_implicit_function_template_parm_p
20842 = auto_is_implicit_function_template_parm_p;
20844 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
20846 (void) cleanup;
20848 if (!processing_specialization
20849 && !processing_template_parmlist
20850 && !processing_explicit_instantiation)
20851 if (!current_function_decl
20852 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
20853 parser->auto_is_implicit_function_template_parm_p = true;
20855 /* Peek at the next token. */
20856 token = cp_lexer_peek_token (parser->lexer);
20857 /* Check for trivial parameter-declaration-clauses. */
20858 if (token->type == CPP_ELLIPSIS)
20860 /* Consume the `...' token. */
20861 cp_lexer_consume_token (parser->lexer);
20862 return NULL_TREE;
20864 else if (token->type == CPP_CLOSE_PAREN)
20865 /* There are no parameters. */
20867 #ifndef NO_IMPLICIT_EXTERN_C
20868 if (in_system_header_at (input_location)
20869 && current_class_type == NULL
20870 && current_lang_name == lang_name_c)
20871 return NULL_TREE;
20872 else
20873 #endif
20874 return void_list_node;
20876 /* Check for `(void)', too, which is a special case. */
20877 else if (token->keyword == RID_VOID
20878 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20879 == CPP_CLOSE_PAREN))
20881 /* Consume the `void' token. */
20882 cp_lexer_consume_token (parser->lexer);
20883 /* There are no parameters. */
20884 return void_list_node;
20887 /* Parse the parameter-declaration-list. */
20888 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
20889 /* If a parse error occurred while parsing the
20890 parameter-declaration-list, then the entire
20891 parameter-declaration-clause is erroneous. */
20892 if (is_error)
20893 return NULL;
20895 /* Peek at the next token. */
20896 token = cp_lexer_peek_token (parser->lexer);
20897 /* If it's a `,', the clause should terminate with an ellipsis. */
20898 if (token->type == CPP_COMMA)
20900 /* Consume the `,'. */
20901 cp_lexer_consume_token (parser->lexer);
20902 /* Expect an ellipsis. */
20903 ellipsis_p
20904 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
20906 /* It might also be `...' if the optional trailing `,' was
20907 omitted. */
20908 else if (token->type == CPP_ELLIPSIS)
20910 /* Consume the `...' token. */
20911 cp_lexer_consume_token (parser->lexer);
20912 /* And remember that we saw it. */
20913 ellipsis_p = true;
20915 else
20916 ellipsis_p = false;
20918 /* Finish the parameter list. */
20919 if (!ellipsis_p)
20920 parameters = chainon (parameters, void_list_node);
20922 return parameters;
20925 /* Parse a parameter-declaration-list.
20927 parameter-declaration-list:
20928 parameter-declaration
20929 parameter-declaration-list , parameter-declaration
20931 Returns a representation of the parameter-declaration-list, as for
20932 cp_parser_parameter_declaration_clause. However, the
20933 `void_list_node' is never appended to the list. Upon return,
20934 *IS_ERROR will be true iff an error occurred. */
20936 static tree
20937 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
20939 tree parameters = NULL_TREE;
20940 tree *tail = &parameters;
20941 bool saved_in_unbraced_linkage_specification_p;
20942 int index = 0;
20944 /* Assume all will go well. */
20945 *is_error = false;
20946 /* The special considerations that apply to a function within an
20947 unbraced linkage specifications do not apply to the parameters
20948 to the function. */
20949 saved_in_unbraced_linkage_specification_p
20950 = parser->in_unbraced_linkage_specification_p;
20951 parser->in_unbraced_linkage_specification_p = false;
20953 /* Look for more parameters. */
20954 while (true)
20956 cp_parameter_declarator *parameter;
20957 tree decl = error_mark_node;
20958 bool parenthesized_p = false;
20959 int template_parm_idx = (function_being_declared_is_template_p (parser)?
20960 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
20961 (current_template_parms)) : 0);
20963 /* Parse the parameter. */
20964 parameter
20965 = cp_parser_parameter_declaration (parser,
20966 /*template_parm_p=*/false,
20967 &parenthesized_p);
20969 /* We don't know yet if the enclosing context is deprecated, so wait
20970 and warn in grokparms if appropriate. */
20971 deprecated_state = DEPRECATED_SUPPRESS;
20973 if (parameter)
20975 /* If a function parameter pack was specified and an implicit template
20976 parameter was introduced during cp_parser_parameter_declaration,
20977 change any implicit parameters introduced into packs. */
20978 if (parser->implicit_template_parms
20979 && parameter->declarator
20980 && parameter->declarator->parameter_pack_p)
20982 int latest_template_parm_idx = TREE_VEC_LENGTH
20983 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
20985 if (latest_template_parm_idx != template_parm_idx)
20986 parameter->decl_specifiers.type = convert_generic_types_to_packs
20987 (parameter->decl_specifiers.type,
20988 template_parm_idx, latest_template_parm_idx);
20991 decl = grokdeclarator (parameter->declarator,
20992 &parameter->decl_specifiers,
20993 PARM,
20994 parameter->default_argument != NULL_TREE,
20995 &parameter->decl_specifiers.attributes);
20998 deprecated_state = DEPRECATED_NORMAL;
21000 /* If a parse error occurred parsing the parameter declaration,
21001 then the entire parameter-declaration-list is erroneous. */
21002 if (decl == error_mark_node)
21004 *is_error = true;
21005 parameters = error_mark_node;
21006 break;
21009 if (parameter->decl_specifiers.attributes)
21010 cplus_decl_attributes (&decl,
21011 parameter->decl_specifiers.attributes,
21013 if (DECL_NAME (decl))
21014 decl = pushdecl (decl);
21016 if (decl != error_mark_node)
21018 retrofit_lang_decl (decl);
21019 DECL_PARM_INDEX (decl) = ++index;
21020 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21023 /* Add the new parameter to the list. */
21024 *tail = build_tree_list (parameter->default_argument, decl);
21025 tail = &TREE_CHAIN (*tail);
21027 /* Peek at the next token. */
21028 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21029 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21030 /* These are for Objective-C++ */
21031 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21032 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21033 /* The parameter-declaration-list is complete. */
21034 break;
21035 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21037 cp_token *token;
21039 /* Peek at the next token. */
21040 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21041 /* If it's an ellipsis, then the list is complete. */
21042 if (token->type == CPP_ELLIPSIS)
21043 break;
21044 /* Otherwise, there must be more parameters. Consume the
21045 `,'. */
21046 cp_lexer_consume_token (parser->lexer);
21047 /* When parsing something like:
21049 int i(float f, double d)
21051 we can tell after seeing the declaration for "f" that we
21052 are not looking at an initialization of a variable "i",
21053 but rather at the declaration of a function "i".
21055 Due to the fact that the parsing of template arguments
21056 (as specified to a template-id) requires backtracking we
21057 cannot use this technique when inside a template argument
21058 list. */
21059 if (!parser->in_template_argument_list_p
21060 && !parser->in_type_id_in_expr_p
21061 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21062 /* However, a parameter-declaration of the form
21063 "float(f)" (which is a valid declaration of a
21064 parameter "f") can also be interpreted as an
21065 expression (the conversion of "f" to "float"). */
21066 && !parenthesized_p)
21067 cp_parser_commit_to_tentative_parse (parser);
21069 else
21071 cp_parser_error (parser, "expected %<,%> or %<...%>");
21072 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21073 cp_parser_skip_to_closing_parenthesis (parser,
21074 /*recovering=*/true,
21075 /*or_comma=*/false,
21076 /*consume_paren=*/false);
21077 break;
21081 parser->in_unbraced_linkage_specification_p
21082 = saved_in_unbraced_linkage_specification_p;
21084 /* Reset implicit_template_scope if we are about to leave the function
21085 parameter list that introduced it. Note that for out-of-line member
21086 definitions, there will be one or more class scopes before we get to
21087 the template parameter scope. */
21089 if (cp_binding_level *its = parser->implicit_template_scope)
21090 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21092 while (maybe_its->kind == sk_class)
21093 maybe_its = maybe_its->level_chain;
21094 if (maybe_its == its)
21096 parser->implicit_template_parms = 0;
21097 parser->implicit_template_scope = 0;
21101 return parameters;
21104 /* Parse a parameter declaration.
21106 parameter-declaration:
21107 decl-specifier-seq ... [opt] declarator
21108 decl-specifier-seq declarator = assignment-expression
21109 decl-specifier-seq ... [opt] abstract-declarator [opt]
21110 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21112 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21113 declares a template parameter. (In that case, a non-nested `>'
21114 token encountered during the parsing of the assignment-expression
21115 is not interpreted as a greater-than operator.)
21117 Returns a representation of the parameter, or NULL if an error
21118 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21119 true iff the declarator is of the form "(p)". */
21121 static cp_parameter_declarator *
21122 cp_parser_parameter_declaration (cp_parser *parser,
21123 bool template_parm_p,
21124 bool *parenthesized_p)
21126 int declares_class_or_enum;
21127 cp_decl_specifier_seq decl_specifiers;
21128 cp_declarator *declarator;
21129 tree default_argument;
21130 cp_token *token = NULL, *declarator_token_start = NULL;
21131 const char *saved_message;
21132 bool template_parameter_pack_p = false;
21134 /* In a template parameter, `>' is not an operator.
21136 [temp.param]
21138 When parsing a default template-argument for a non-type
21139 template-parameter, the first non-nested `>' is taken as the end
21140 of the template parameter-list rather than a greater-than
21141 operator. */
21143 /* Type definitions may not appear in parameter types. */
21144 saved_message = parser->type_definition_forbidden_message;
21145 parser->type_definition_forbidden_message
21146 = G_("types may not be defined in parameter types");
21148 /* Parse the declaration-specifiers. */
21149 cp_parser_decl_specifier_seq (parser,
21150 CP_PARSER_FLAGS_NONE,
21151 &decl_specifiers,
21152 &declares_class_or_enum);
21154 /* Complain about missing 'typename' or other invalid type names. */
21155 if (!decl_specifiers.any_type_specifiers_p
21156 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21157 decl_specifiers.type = error_mark_node;
21159 /* If an error occurred, there's no reason to attempt to parse the
21160 rest of the declaration. */
21161 if (cp_parser_error_occurred (parser))
21163 parser->type_definition_forbidden_message = saved_message;
21164 return NULL;
21167 /* Peek at the next token. */
21168 token = cp_lexer_peek_token (parser->lexer);
21170 /* If the next token is a `)', `,', `=', `>', or `...', then there
21171 is no declarator. However, when variadic templates are enabled,
21172 there may be a declarator following `...'. */
21173 if (token->type == CPP_CLOSE_PAREN
21174 || token->type == CPP_COMMA
21175 || token->type == CPP_EQ
21176 || token->type == CPP_GREATER)
21178 declarator = NULL;
21179 if (parenthesized_p)
21180 *parenthesized_p = false;
21182 /* Otherwise, there should be a declarator. */
21183 else
21185 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21186 parser->default_arg_ok_p = false;
21188 /* After seeing a decl-specifier-seq, if the next token is not a
21189 "(", there is no possibility that the code is a valid
21190 expression. Therefore, if parsing tentatively, we commit at
21191 this point. */
21192 if (!parser->in_template_argument_list_p
21193 /* In an expression context, having seen:
21195 (int((char ...
21197 we cannot be sure whether we are looking at a
21198 function-type (taking a "char" as a parameter) or a cast
21199 of some object of type "char" to "int". */
21200 && !parser->in_type_id_in_expr_p
21201 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21202 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21203 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21204 cp_parser_commit_to_tentative_parse (parser);
21205 /* Parse the declarator. */
21206 declarator_token_start = token;
21207 declarator = cp_parser_declarator (parser,
21208 CP_PARSER_DECLARATOR_EITHER,
21209 /*ctor_dtor_or_conv_p=*/NULL,
21210 parenthesized_p,
21211 /*member_p=*/false,
21212 /*friend_p=*/false);
21213 parser->default_arg_ok_p = saved_default_arg_ok_p;
21214 /* After the declarator, allow more attributes. */
21215 decl_specifiers.attributes
21216 = chainon (decl_specifiers.attributes,
21217 cp_parser_attributes_opt (parser));
21219 /* If the declarator is a template parameter pack, remember that and
21220 clear the flag in the declarator itself so we don't get errors
21221 from grokdeclarator. */
21222 if (template_parm_p && declarator && declarator->parameter_pack_p)
21224 declarator->parameter_pack_p = false;
21225 template_parameter_pack_p = true;
21229 /* If the next token is an ellipsis, and we have not seen a declarator
21230 name, and if either the type of the declarator contains parameter
21231 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21232 for, eg, abbreviated integral type names), then we actually have a
21233 parameter pack expansion expression. Otherwise, leave the ellipsis
21234 for a C-style variadic function. */
21235 token = cp_lexer_peek_token (parser->lexer);
21236 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21238 tree type = decl_specifiers.type;
21240 if (type && DECL_P (type))
21241 type = TREE_TYPE (type);
21243 if (((type
21244 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21245 && (template_parm_p || uses_parameter_packs (type)))
21246 || (!type && template_parm_p))
21247 && declarator_can_be_parameter_pack (declarator))
21249 /* Consume the `...'. */
21250 cp_lexer_consume_token (parser->lexer);
21251 maybe_warn_variadic_templates ();
21253 /* Build a pack expansion type */
21254 if (template_parm_p)
21255 template_parameter_pack_p = true;
21256 else if (declarator)
21257 declarator->parameter_pack_p = true;
21258 else
21259 decl_specifiers.type = make_pack_expansion (type);
21263 /* The restriction on defining new types applies only to the type
21264 of the parameter, not to the default argument. */
21265 parser->type_definition_forbidden_message = saved_message;
21267 /* If the next token is `=', then process a default argument. */
21268 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21270 tree type = decl_specifiers.type;
21271 token = cp_lexer_peek_token (parser->lexer);
21272 /* If we are defining a class, then the tokens that make up the
21273 default argument must be saved and processed later. */
21274 if (!template_parm_p && at_class_scope_p ()
21275 && TYPE_BEING_DEFINED (current_class_type)
21276 && !LAMBDA_TYPE_P (current_class_type))
21277 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21279 // A constrained-type-specifier may declare a type template-parameter.
21280 else if (declares_constrained_type_template_parameter (type))
21281 default_argument
21282 = cp_parser_default_type_template_argument (parser);
21284 // A constrained-type-specifier may declare a template-template-parameter.
21285 else if (declares_constrained_template_template_parameter (type))
21286 default_argument
21287 = cp_parser_default_template_template_argument (parser);
21289 /* Outside of a class definition, we can just parse the
21290 assignment-expression. */
21291 else
21292 default_argument
21293 = cp_parser_default_argument (parser, template_parm_p);
21295 if (!parser->default_arg_ok_p)
21297 permerror (token->location,
21298 "default arguments are only "
21299 "permitted for function parameters");
21301 else if ((declarator && declarator->parameter_pack_p)
21302 || template_parameter_pack_p
21303 || (decl_specifiers.type
21304 && PACK_EXPANSION_P (decl_specifiers.type)))
21306 /* Find the name of the parameter pack. */
21307 cp_declarator *id_declarator = declarator;
21308 while (id_declarator && id_declarator->kind != cdk_id)
21309 id_declarator = id_declarator->declarator;
21311 if (id_declarator && id_declarator->kind == cdk_id)
21312 error_at (declarator_token_start->location,
21313 template_parm_p
21314 ? G_("template parameter pack %qD "
21315 "cannot have a default argument")
21316 : G_("parameter pack %qD cannot have "
21317 "a default argument"),
21318 id_declarator->u.id.unqualified_name);
21319 else
21320 error_at (declarator_token_start->location,
21321 template_parm_p
21322 ? G_("template parameter pack cannot have "
21323 "a default argument")
21324 : G_("parameter pack cannot have a "
21325 "default argument"));
21327 default_argument = NULL_TREE;
21330 else
21331 default_argument = NULL_TREE;
21333 return make_parameter_declarator (&decl_specifiers,
21334 declarator,
21335 default_argument,
21336 template_parameter_pack_p);
21339 /* Parse a default argument and return it.
21341 TEMPLATE_PARM_P is true if this is a default argument for a
21342 non-type template parameter. */
21343 static tree
21344 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21346 tree default_argument = NULL_TREE;
21347 bool saved_greater_than_is_operator_p;
21348 bool saved_local_variables_forbidden_p;
21349 bool non_constant_p, is_direct_init;
21351 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21352 set correctly. */
21353 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21354 parser->greater_than_is_operator_p = !template_parm_p;
21355 /* Local variable names (and the `this' keyword) may not
21356 appear in a default argument. */
21357 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21358 parser->local_variables_forbidden_p = true;
21359 /* Parse the assignment-expression. */
21360 if (template_parm_p)
21361 push_deferring_access_checks (dk_no_deferred);
21362 tree saved_class_ptr = NULL_TREE;
21363 tree saved_class_ref = NULL_TREE;
21364 /* The "this" pointer is not valid in a default argument. */
21365 if (cfun)
21367 saved_class_ptr = current_class_ptr;
21368 cp_function_chain->x_current_class_ptr = NULL_TREE;
21369 saved_class_ref = current_class_ref;
21370 cp_function_chain->x_current_class_ref = NULL_TREE;
21372 default_argument
21373 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21374 /* Restore the "this" pointer. */
21375 if (cfun)
21377 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21378 cp_function_chain->x_current_class_ref = saved_class_ref;
21380 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21381 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21382 if (template_parm_p)
21383 pop_deferring_access_checks ();
21384 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21385 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21387 return default_argument;
21390 /* Parse a function-body.
21392 function-body:
21393 compound_statement */
21395 static void
21396 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21398 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21399 ? BCS_TRY_BLOCK : BCS_NORMAL),
21400 true);
21403 /* Parse a ctor-initializer-opt followed by a function-body. Return
21404 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21405 is true we are parsing a function-try-block. */
21407 static bool
21408 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21409 bool in_function_try_block)
21411 tree body, list;
21412 bool ctor_initializer_p;
21413 const bool check_body_p =
21414 DECL_CONSTRUCTOR_P (current_function_decl)
21415 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21416 tree last = NULL;
21418 /* Begin the function body. */
21419 body = begin_function_body ();
21420 /* Parse the optional ctor-initializer. */
21421 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
21423 /* If we're parsing a constexpr constructor definition, we need
21424 to check that the constructor body is indeed empty. However,
21425 before we get to cp_parser_function_body lot of junk has been
21426 generated, so we can't just check that we have an empty block.
21427 Rather we take a snapshot of the outermost block, and check whether
21428 cp_parser_function_body changed its state. */
21429 if (check_body_p)
21431 list = cur_stmt_list;
21432 if (STATEMENT_LIST_TAIL (list))
21433 last = STATEMENT_LIST_TAIL (list)->stmt;
21435 /* Parse the function-body. */
21436 cp_parser_function_body (parser, in_function_try_block);
21437 if (check_body_p)
21438 check_constexpr_ctor_body (last, list, /*complain=*/true);
21439 /* Finish the function body. */
21440 finish_function_body (body);
21442 return ctor_initializer_p;
21445 /* Parse an initializer.
21447 initializer:
21448 = initializer-clause
21449 ( expression-list )
21451 Returns an expression representing the initializer. If no
21452 initializer is present, NULL_TREE is returned.
21454 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21455 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21456 set to TRUE if there is no initializer present. If there is an
21457 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21458 is set to true; otherwise it is set to false. */
21460 static tree
21461 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21462 bool* non_constant_p)
21464 cp_token *token;
21465 tree init;
21467 /* Peek at the next token. */
21468 token = cp_lexer_peek_token (parser->lexer);
21470 /* Let our caller know whether or not this initializer was
21471 parenthesized. */
21472 *is_direct_init = (token->type != CPP_EQ);
21473 /* Assume that the initializer is constant. */
21474 *non_constant_p = false;
21476 if (token->type == CPP_EQ)
21478 /* Consume the `='. */
21479 cp_lexer_consume_token (parser->lexer);
21480 /* Parse the initializer-clause. */
21481 init = cp_parser_initializer_clause (parser, non_constant_p);
21483 else if (token->type == CPP_OPEN_PAREN)
21485 vec<tree, va_gc> *vec;
21486 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21487 /*cast_p=*/false,
21488 /*allow_expansion_p=*/true,
21489 non_constant_p);
21490 if (vec == NULL)
21491 return error_mark_node;
21492 init = build_tree_list_vec (vec);
21493 release_tree_vector (vec);
21495 else if (token->type == CPP_OPEN_BRACE)
21497 cp_lexer_set_source_position (parser->lexer);
21498 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21499 init = cp_parser_braced_list (parser, non_constant_p);
21500 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21502 else
21504 /* Anything else is an error. */
21505 cp_parser_error (parser, "expected initializer");
21506 init = error_mark_node;
21509 if (check_for_bare_parameter_packs (init))
21510 init = error_mark_node;
21512 return init;
21515 /* Parse an initializer-clause.
21517 initializer-clause:
21518 assignment-expression
21519 braced-init-list
21521 Returns an expression representing the initializer.
21523 If the `assignment-expression' production is used the value
21524 returned is simply a representation for the expression.
21526 Otherwise, calls cp_parser_braced_list. */
21528 static cp_expr
21529 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21531 cp_expr initializer;
21533 /* Assume the expression is constant. */
21534 *non_constant_p = false;
21536 /* If it is not a `{', then we are looking at an
21537 assignment-expression. */
21538 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21540 initializer
21541 = cp_parser_constant_expression (parser,
21542 /*allow_non_constant_p=*/true,
21543 non_constant_p);
21545 else
21546 initializer = cp_parser_braced_list (parser, non_constant_p);
21548 return initializer;
21551 /* Parse a brace-enclosed initializer list.
21553 braced-init-list:
21554 { initializer-list , [opt] }
21557 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21558 the elements of the initializer-list (or NULL, if the last
21559 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21560 NULL_TREE. There is no way to detect whether or not the optional
21561 trailing `,' was provided. NON_CONSTANT_P is as for
21562 cp_parser_initializer. */
21564 static cp_expr
21565 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
21567 tree initializer;
21568 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
21570 /* Consume the `{' token. */
21571 cp_lexer_consume_token (parser->lexer);
21572 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21573 initializer = make_node (CONSTRUCTOR);
21574 /* If it's not a `}', then there is a non-trivial initializer. */
21575 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
21577 /* Parse the initializer list. */
21578 CONSTRUCTOR_ELTS (initializer)
21579 = cp_parser_initializer_list (parser, non_constant_p);
21580 /* A trailing `,' token is allowed. */
21581 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21582 cp_lexer_consume_token (parser->lexer);
21584 else
21585 *non_constant_p = false;
21586 /* Now, there should be a trailing `}'. */
21587 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
21588 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21589 TREE_TYPE (initializer) = init_list_type_node;
21591 cp_expr result (initializer);
21592 /* Build a location of the form:
21593 { ... }
21594 ^~~~~~~
21595 with caret==start at the open brace, finish at the close brace. */
21596 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
21597 result.set_location (combined_loc);
21598 return result;
21601 /* Consume tokens up to, and including, the next non-nested closing `]'.
21602 Returns true iff we found a closing `]'. */
21604 static bool
21605 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
21607 unsigned square_depth = 0;
21609 while (true)
21611 cp_token * token = cp_lexer_peek_token (parser->lexer);
21613 switch (token->type)
21615 case CPP_EOF:
21616 case CPP_PRAGMA_EOL:
21617 /* If we've run out of tokens, then there is no closing `]'. */
21618 return false;
21620 case CPP_OPEN_SQUARE:
21621 ++square_depth;
21622 break;
21624 case CPP_CLOSE_SQUARE:
21625 if (!square_depth--)
21627 cp_lexer_consume_token (parser->lexer);
21628 return true;
21630 break;
21632 default:
21633 break;
21636 /* Consume the token. */
21637 cp_lexer_consume_token (parser->lexer);
21641 /* Return true if we are looking at an array-designator, false otherwise. */
21643 static bool
21644 cp_parser_array_designator_p (cp_parser *parser)
21646 /* Consume the `['. */
21647 cp_lexer_consume_token (parser->lexer);
21649 cp_lexer_save_tokens (parser->lexer);
21651 /* Skip tokens until the next token is a closing square bracket.
21652 If we find the closing `]', and the next token is a `=', then
21653 we are looking at an array designator. */
21654 bool array_designator_p
21655 = (cp_parser_skip_to_closing_square_bracket (parser)
21656 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
21658 /* Roll back the tokens we skipped. */
21659 cp_lexer_rollback_tokens (parser->lexer);
21661 return array_designator_p;
21664 /* Parse an initializer-list.
21666 initializer-list:
21667 initializer-clause ... [opt]
21668 initializer-list , initializer-clause ... [opt]
21670 GNU Extension:
21672 initializer-list:
21673 designation initializer-clause ...[opt]
21674 initializer-list , designation initializer-clause ...[opt]
21676 designation:
21677 . identifier =
21678 identifier :
21679 [ constant-expression ] =
21681 Returns a vec of constructor_elt. The VALUE of each elt is an expression
21682 for the initializer. If the INDEX of the elt is non-NULL, it is the
21683 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
21684 as for cp_parser_initializer. */
21686 static vec<constructor_elt, va_gc> *
21687 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
21689 vec<constructor_elt, va_gc> *v = NULL;
21691 /* Assume all of the expressions are constant. */
21692 *non_constant_p = false;
21694 /* Parse the rest of the list. */
21695 while (true)
21697 cp_token *token;
21698 tree designator;
21699 tree initializer;
21700 bool clause_non_constant_p;
21702 /* If the next token is an identifier and the following one is a
21703 colon, we are looking at the GNU designated-initializer
21704 syntax. */
21705 if (cp_parser_allow_gnu_extensions_p (parser)
21706 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21707 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
21709 /* Warn the user that they are using an extension. */
21710 pedwarn (input_location, OPT_Wpedantic,
21711 "ISO C++ does not allow designated initializers");
21712 /* Consume the identifier. */
21713 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21714 /* Consume the `:'. */
21715 cp_lexer_consume_token (parser->lexer);
21717 /* Also handle the C99 syntax, '. id ='. */
21718 else if (cp_parser_allow_gnu_extensions_p (parser)
21719 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
21720 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
21721 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
21723 /* Warn the user that they are using an extension. */
21724 pedwarn (input_location, OPT_Wpedantic,
21725 "ISO C++ does not allow C99 designated initializers");
21726 /* Consume the `.'. */
21727 cp_lexer_consume_token (parser->lexer);
21728 /* Consume the identifier. */
21729 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21730 /* Consume the `='. */
21731 cp_lexer_consume_token (parser->lexer);
21733 /* Also handle C99 array designators, '[ const ] ='. */
21734 else if (cp_parser_allow_gnu_extensions_p (parser)
21735 && !c_dialect_objc ()
21736 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21738 /* In C++11, [ could start a lambda-introducer. */
21739 bool non_const = false;
21741 cp_parser_parse_tentatively (parser);
21743 if (!cp_parser_array_designator_p (parser))
21745 cp_parser_simulate_error (parser);
21746 designator = NULL_TREE;
21748 else
21750 designator = cp_parser_constant_expression (parser, true,
21751 &non_const);
21752 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21753 cp_parser_require (parser, CPP_EQ, RT_EQ);
21756 if (!cp_parser_parse_definitely (parser))
21757 designator = NULL_TREE;
21758 else if (non_const)
21759 require_potential_rvalue_constant_expression (designator);
21761 else
21762 designator = NULL_TREE;
21764 /* Parse the initializer. */
21765 initializer = cp_parser_initializer_clause (parser,
21766 &clause_non_constant_p);
21767 /* If any clause is non-constant, so is the entire initializer. */
21768 if (clause_non_constant_p)
21769 *non_constant_p = true;
21771 /* If we have an ellipsis, this is an initializer pack
21772 expansion. */
21773 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21775 /* Consume the `...'. */
21776 cp_lexer_consume_token (parser->lexer);
21778 /* Turn the initializer into an initializer expansion. */
21779 initializer = make_pack_expansion (initializer);
21782 /* Add it to the vector. */
21783 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
21785 /* If the next token is not a comma, we have reached the end of
21786 the list. */
21787 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21788 break;
21790 /* Peek at the next token. */
21791 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21792 /* If the next token is a `}', then we're still done. An
21793 initializer-clause can have a trailing `,' after the
21794 initializer-list and before the closing `}'. */
21795 if (token->type == CPP_CLOSE_BRACE)
21796 break;
21798 /* Consume the `,' token. */
21799 cp_lexer_consume_token (parser->lexer);
21802 return v;
21805 /* Classes [gram.class] */
21807 /* Parse a class-name.
21809 class-name:
21810 identifier
21811 template-id
21813 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
21814 to indicate that names looked up in dependent types should be
21815 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
21816 keyword has been used to indicate that the name that appears next
21817 is a template. TAG_TYPE indicates the explicit tag given before
21818 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
21819 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
21820 is the class being defined in a class-head. If ENUM_OK is TRUE,
21821 enum-names are also accepted.
21823 Returns the TYPE_DECL representing the class. */
21825 static tree
21826 cp_parser_class_name (cp_parser *parser,
21827 bool typename_keyword_p,
21828 bool template_keyword_p,
21829 enum tag_types tag_type,
21830 bool check_dependency_p,
21831 bool class_head_p,
21832 bool is_declaration,
21833 bool enum_ok)
21835 tree decl;
21836 tree scope;
21837 bool typename_p;
21838 cp_token *token;
21839 tree identifier = NULL_TREE;
21841 /* All class-names start with an identifier. */
21842 token = cp_lexer_peek_token (parser->lexer);
21843 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
21845 cp_parser_error (parser, "expected class-name");
21846 return error_mark_node;
21849 /* PARSER->SCOPE can be cleared when parsing the template-arguments
21850 to a template-id, so we save it here. */
21851 scope = parser->scope;
21852 if (scope == error_mark_node)
21853 return error_mark_node;
21855 /* Any name names a type if we're following the `typename' keyword
21856 in a qualified name where the enclosing scope is type-dependent. */
21857 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
21858 && dependent_type_p (scope));
21859 /* Handle the common case (an identifier, but not a template-id)
21860 efficiently. */
21861 if (token->type == CPP_NAME
21862 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
21864 cp_token *identifier_token;
21865 bool ambiguous_p;
21867 /* Look for the identifier. */
21868 identifier_token = cp_lexer_peek_token (parser->lexer);
21869 ambiguous_p = identifier_token->error_reported;
21870 identifier = cp_parser_identifier (parser);
21871 /* If the next token isn't an identifier, we are certainly not
21872 looking at a class-name. */
21873 if (identifier == error_mark_node)
21874 decl = error_mark_node;
21875 /* If we know this is a type-name, there's no need to look it
21876 up. */
21877 else if (typename_p)
21878 decl = identifier;
21879 else
21881 tree ambiguous_decls;
21882 /* If we already know that this lookup is ambiguous, then
21883 we've already issued an error message; there's no reason
21884 to check again. */
21885 if (ambiguous_p)
21887 cp_parser_simulate_error (parser);
21888 return error_mark_node;
21890 /* If the next token is a `::', then the name must be a type
21891 name.
21893 [basic.lookup.qual]
21895 During the lookup for a name preceding the :: scope
21896 resolution operator, object, function, and enumerator
21897 names are ignored. */
21898 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21899 tag_type = scope_type;
21900 /* Look up the name. */
21901 decl = cp_parser_lookup_name (parser, identifier,
21902 tag_type,
21903 /*is_template=*/false,
21904 /*is_namespace=*/false,
21905 check_dependency_p,
21906 &ambiguous_decls,
21907 identifier_token->location);
21908 if (ambiguous_decls)
21910 if (cp_parser_parsing_tentatively (parser))
21911 cp_parser_simulate_error (parser);
21912 return error_mark_node;
21916 else
21918 /* Try a template-id. */
21919 decl = cp_parser_template_id (parser, template_keyword_p,
21920 check_dependency_p,
21921 tag_type,
21922 is_declaration);
21923 if (decl == error_mark_node)
21924 return error_mark_node;
21927 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
21929 /* If this is a typename, create a TYPENAME_TYPE. */
21930 if (typename_p && decl != error_mark_node)
21932 decl = make_typename_type (scope, decl, typename_type,
21933 /*complain=*/tf_error);
21934 if (decl != error_mark_node)
21935 decl = TYPE_NAME (decl);
21938 decl = strip_using_decl (decl);
21940 /* Check to see that it is really the name of a class. */
21941 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
21942 && identifier_p (TREE_OPERAND (decl, 0))
21943 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21944 /* Situations like this:
21946 template <typename T> struct A {
21947 typename T::template X<int>::I i;
21950 are problematic. Is `T::template X<int>' a class-name? The
21951 standard does not seem to be definitive, but there is no other
21952 valid interpretation of the following `::'. Therefore, those
21953 names are considered class-names. */
21955 decl = make_typename_type (scope, decl, tag_type, tf_error);
21956 if (decl != error_mark_node)
21957 decl = TYPE_NAME (decl);
21959 else if (TREE_CODE (decl) != TYPE_DECL
21960 || TREE_TYPE (decl) == error_mark_node
21961 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
21962 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
21963 /* In Objective-C 2.0, a classname followed by '.' starts a
21964 dot-syntax expression, and it's not a type-name. */
21965 || (c_dialect_objc ()
21966 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
21967 && objc_is_class_name (decl)))
21968 decl = error_mark_node;
21970 if (decl == error_mark_node)
21971 cp_parser_error (parser, "expected class-name");
21972 else if (identifier && !parser->scope)
21973 maybe_note_name_used_in_class (identifier, decl);
21975 return decl;
21978 /* Parse a class-specifier.
21980 class-specifier:
21981 class-head { member-specification [opt] }
21983 Returns the TREE_TYPE representing the class. */
21985 static tree
21986 cp_parser_class_specifier_1 (cp_parser* parser)
21988 tree type;
21989 tree attributes = NULL_TREE;
21990 bool nested_name_specifier_p;
21991 unsigned saved_num_template_parameter_lists;
21992 bool saved_in_function_body;
21993 unsigned char in_statement;
21994 bool in_switch_statement_p;
21995 bool saved_in_unbraced_linkage_specification_p;
21996 tree old_scope = NULL_TREE;
21997 tree scope = NULL_TREE;
21998 cp_token *closing_brace;
22000 push_deferring_access_checks (dk_no_deferred);
22002 /* Parse the class-head. */
22003 type = cp_parser_class_head (parser,
22004 &nested_name_specifier_p);
22005 /* If the class-head was a semantic disaster, skip the entire body
22006 of the class. */
22007 if (!type)
22009 cp_parser_skip_to_end_of_block_or_statement (parser);
22010 pop_deferring_access_checks ();
22011 return error_mark_node;
22014 /* Look for the `{'. */
22015 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
22017 pop_deferring_access_checks ();
22018 return error_mark_node;
22021 cp_ensure_no_omp_declare_simd (parser);
22022 cp_ensure_no_oacc_routine (parser);
22024 /* Issue an error message if type-definitions are forbidden here. */
22025 cp_parser_check_type_definition (parser);
22026 /* Remember that we are defining one more class. */
22027 ++parser->num_classes_being_defined;
22028 /* Inside the class, surrounding template-parameter-lists do not
22029 apply. */
22030 saved_num_template_parameter_lists
22031 = parser->num_template_parameter_lists;
22032 parser->num_template_parameter_lists = 0;
22033 /* We are not in a function body. */
22034 saved_in_function_body = parser->in_function_body;
22035 parser->in_function_body = false;
22036 /* Or in a loop. */
22037 in_statement = parser->in_statement;
22038 parser->in_statement = 0;
22039 /* Or in a switch. */
22040 in_switch_statement_p = parser->in_switch_statement_p;
22041 parser->in_switch_statement_p = false;
22042 /* We are not immediately inside an extern "lang" block. */
22043 saved_in_unbraced_linkage_specification_p
22044 = parser->in_unbraced_linkage_specification_p;
22045 parser->in_unbraced_linkage_specification_p = false;
22047 // Associate constraints with the type.
22048 if (flag_concepts)
22049 type = associate_classtype_constraints (type);
22051 /* Start the class. */
22052 if (nested_name_specifier_p)
22054 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22055 old_scope = push_inner_scope (scope);
22057 type = begin_class_definition (type);
22059 if (type == error_mark_node)
22060 /* If the type is erroneous, skip the entire body of the class. */
22061 cp_parser_skip_to_closing_brace (parser);
22062 else
22063 /* Parse the member-specification. */
22064 cp_parser_member_specification_opt (parser);
22066 /* Look for the trailing `}'. */
22067 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
22068 /* Look for trailing attributes to apply to this class. */
22069 if (cp_parser_allow_gnu_extensions_p (parser))
22070 attributes = cp_parser_gnu_attributes_opt (parser);
22071 if (type != error_mark_node)
22072 type = finish_struct (type, attributes);
22073 if (nested_name_specifier_p)
22074 pop_inner_scope (old_scope, scope);
22076 /* We've finished a type definition. Check for the common syntax
22077 error of forgetting a semicolon after the definition. We need to
22078 be careful, as we can't just check for not-a-semicolon and be done
22079 with it; the user might have typed:
22081 class X { } c = ...;
22082 class X { } *p = ...;
22084 and so forth. Instead, enumerate all the possible tokens that
22085 might follow this production; if we don't see one of them, then
22086 complain and silently insert the semicolon. */
22088 cp_token *token = cp_lexer_peek_token (parser->lexer);
22089 bool want_semicolon = true;
22091 if (cp_next_tokens_can_be_std_attribute_p (parser))
22092 /* Don't try to parse c++11 attributes here. As per the
22093 grammar, that should be a task for
22094 cp_parser_decl_specifier_seq. */
22095 want_semicolon = false;
22097 switch (token->type)
22099 case CPP_NAME:
22100 case CPP_SEMICOLON:
22101 case CPP_MULT:
22102 case CPP_AND:
22103 case CPP_OPEN_PAREN:
22104 case CPP_CLOSE_PAREN:
22105 case CPP_COMMA:
22106 want_semicolon = false;
22107 break;
22109 /* While it's legal for type qualifiers and storage class
22110 specifiers to follow type definitions in the grammar, only
22111 compiler testsuites contain code like that. Assume that if
22112 we see such code, then what we're really seeing is a case
22113 like:
22115 class X { }
22116 const <type> var = ...;
22120 class Y { }
22121 static <type> func (...) ...
22123 i.e. the qualifier or specifier applies to the next
22124 declaration. To do so, however, we need to look ahead one
22125 more token to see if *that* token is a type specifier.
22127 This code could be improved to handle:
22129 class Z { }
22130 static const <type> var = ...; */
22131 case CPP_KEYWORD:
22132 if (keyword_is_decl_specifier (token->keyword))
22134 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22136 /* Handling user-defined types here would be nice, but very
22137 tricky. */
22138 want_semicolon
22139 = (lookahead->type == CPP_KEYWORD
22140 && keyword_begins_type_specifier (lookahead->keyword));
22142 break;
22143 default:
22144 break;
22147 /* If we don't have a type, then something is very wrong and we
22148 shouldn't try to do anything clever. Likewise for not seeing the
22149 closing brace. */
22150 if (closing_brace && TYPE_P (type) && want_semicolon)
22152 /* Locate the closing brace. */
22153 cp_token_position prev
22154 = cp_lexer_previous_token_position (parser->lexer);
22155 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22156 location_t loc = prev_token->location;
22158 /* We want to suggest insertion of a ';' immediately *after* the
22159 closing brace, so, if we can, offset the location by 1 column. */
22160 location_t next_loc = loc;
22161 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22162 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22164 rich_location richloc (line_table, next_loc);
22166 /* If we successfully offset the location, suggest the fix-it. */
22167 if (next_loc != loc)
22168 richloc.add_fixit_insert_before (next_loc, ";");
22170 if (CLASSTYPE_DECLARED_CLASS (type))
22171 error_at_rich_loc (&richloc,
22172 "expected %<;%> after class definition");
22173 else if (TREE_CODE (type) == RECORD_TYPE)
22174 error_at_rich_loc (&richloc,
22175 "expected %<;%> after struct definition");
22176 else if (TREE_CODE (type) == UNION_TYPE)
22177 error_at_rich_loc (&richloc,
22178 "expected %<;%> after union definition");
22179 else
22180 gcc_unreachable ();
22182 /* Unget one token and smash it to look as though we encountered
22183 a semicolon in the input stream. */
22184 cp_lexer_set_token_position (parser->lexer, prev);
22185 token = cp_lexer_peek_token (parser->lexer);
22186 token->type = CPP_SEMICOLON;
22187 token->keyword = RID_MAX;
22191 /* If this class is not itself within the scope of another class,
22192 then we need to parse the bodies of all of the queued function
22193 definitions. Note that the queued functions defined in a class
22194 are not always processed immediately following the
22195 class-specifier for that class. Consider:
22197 struct A {
22198 struct B { void f() { sizeof (A); } };
22201 If `f' were processed before the processing of `A' were
22202 completed, there would be no way to compute the size of `A'.
22203 Note that the nesting we are interested in here is lexical --
22204 not the semantic nesting given by TYPE_CONTEXT. In particular,
22205 for:
22207 struct A { struct B; };
22208 struct A::B { void f() { } };
22210 there is no need to delay the parsing of `A::B::f'. */
22211 if (--parser->num_classes_being_defined == 0)
22213 tree decl;
22214 tree class_type = NULL_TREE;
22215 tree pushed_scope = NULL_TREE;
22216 unsigned ix;
22217 cp_default_arg_entry *e;
22218 tree save_ccp, save_ccr;
22220 /* In a first pass, parse default arguments to the functions.
22221 Then, in a second pass, parse the bodies of the functions.
22222 This two-phased approach handles cases like:
22224 struct S {
22225 void f() { g(); }
22226 void g(int i = 3);
22230 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22232 decl = e->decl;
22233 /* If there are default arguments that have not yet been processed,
22234 take care of them now. */
22235 if (class_type != e->class_type)
22237 if (pushed_scope)
22238 pop_scope (pushed_scope);
22239 class_type = e->class_type;
22240 pushed_scope = push_scope (class_type);
22242 /* Make sure that any template parameters are in scope. */
22243 maybe_begin_member_template_processing (decl);
22244 /* Parse the default argument expressions. */
22245 cp_parser_late_parsing_default_args (parser, decl);
22246 /* Remove any template parameters from the symbol table. */
22247 maybe_end_member_template_processing ();
22249 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22250 /* Now parse any NSDMIs. */
22251 save_ccp = current_class_ptr;
22252 save_ccr = current_class_ref;
22253 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22255 if (class_type != DECL_CONTEXT (decl))
22257 if (pushed_scope)
22258 pop_scope (pushed_scope);
22259 class_type = DECL_CONTEXT (decl);
22260 pushed_scope = push_scope (class_type);
22262 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22263 cp_parser_late_parsing_nsdmi (parser, decl);
22265 vec_safe_truncate (unparsed_nsdmis, 0);
22266 current_class_ptr = save_ccp;
22267 current_class_ref = save_ccr;
22268 if (pushed_scope)
22269 pop_scope (pushed_scope);
22271 /* Now do some post-NSDMI bookkeeping. */
22272 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22273 after_nsdmi_defaulted_late_checks (class_type);
22274 vec_safe_truncate (unparsed_classes, 0);
22275 after_nsdmi_defaulted_late_checks (type);
22277 /* Now parse the body of the functions. */
22278 if (flag_openmp)
22280 /* OpenMP UDRs need to be parsed before all other functions. */
22281 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22282 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22283 cp_parser_late_parsing_for_member (parser, decl);
22284 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22285 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22286 cp_parser_late_parsing_for_member (parser, decl);
22288 else
22289 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22290 cp_parser_late_parsing_for_member (parser, decl);
22291 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22293 else
22294 vec_safe_push (unparsed_classes, type);
22296 /* Put back any saved access checks. */
22297 pop_deferring_access_checks ();
22299 /* Restore saved state. */
22300 parser->in_switch_statement_p = in_switch_statement_p;
22301 parser->in_statement = in_statement;
22302 parser->in_function_body = saved_in_function_body;
22303 parser->num_template_parameter_lists
22304 = saved_num_template_parameter_lists;
22305 parser->in_unbraced_linkage_specification_p
22306 = saved_in_unbraced_linkage_specification_p;
22308 return type;
22311 static tree
22312 cp_parser_class_specifier (cp_parser* parser)
22314 tree ret;
22315 timevar_push (TV_PARSE_STRUCT);
22316 ret = cp_parser_class_specifier_1 (parser);
22317 timevar_pop (TV_PARSE_STRUCT);
22318 return ret;
22321 /* Parse a class-head.
22323 class-head:
22324 class-key identifier [opt] base-clause [opt]
22325 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22326 class-key nested-name-specifier [opt] template-id
22327 base-clause [opt]
22329 class-virt-specifier:
22330 final
22332 GNU Extensions:
22333 class-key attributes identifier [opt] base-clause [opt]
22334 class-key attributes nested-name-specifier identifier base-clause [opt]
22335 class-key attributes nested-name-specifier [opt] template-id
22336 base-clause [opt]
22338 Upon return BASES is initialized to the list of base classes (or
22339 NULL, if there are none) in the same form returned by
22340 cp_parser_base_clause.
22342 Returns the TYPE of the indicated class. Sets
22343 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22344 involving a nested-name-specifier was used, and FALSE otherwise.
22346 Returns error_mark_node if this is not a class-head.
22348 Returns NULL_TREE if the class-head is syntactically valid, but
22349 semantically invalid in a way that means we should skip the entire
22350 body of the class. */
22352 static tree
22353 cp_parser_class_head (cp_parser* parser,
22354 bool* nested_name_specifier_p)
22356 tree nested_name_specifier;
22357 enum tag_types class_key;
22358 tree id = NULL_TREE;
22359 tree type = NULL_TREE;
22360 tree attributes;
22361 tree bases;
22362 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22363 bool template_id_p = false;
22364 bool qualified_p = false;
22365 bool invalid_nested_name_p = false;
22366 bool invalid_explicit_specialization_p = false;
22367 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22368 tree pushed_scope = NULL_TREE;
22369 unsigned num_templates;
22370 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22371 /* Assume no nested-name-specifier will be present. */
22372 *nested_name_specifier_p = false;
22373 /* Assume no template parameter lists will be used in defining the
22374 type. */
22375 num_templates = 0;
22376 parser->colon_corrects_to_scope_p = false;
22378 /* Look for the class-key. */
22379 class_key = cp_parser_class_key (parser);
22380 if (class_key == none_type)
22381 return error_mark_node;
22383 location_t class_head_start_location = input_location;
22385 /* Parse the attributes. */
22386 attributes = cp_parser_attributes_opt (parser);
22388 /* If the next token is `::', that is invalid -- but sometimes
22389 people do try to write:
22391 struct ::S {};
22393 Handle this gracefully by accepting the extra qualifier, and then
22394 issuing an error about it later if this really is a
22395 class-head. If it turns out just to be an elaborated type
22396 specifier, remain silent. */
22397 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22398 qualified_p = true;
22400 push_deferring_access_checks (dk_no_check);
22402 /* Determine the name of the class. Begin by looking for an
22403 optional nested-name-specifier. */
22404 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22405 nested_name_specifier
22406 = cp_parser_nested_name_specifier_opt (parser,
22407 /*typename_keyword_p=*/false,
22408 /*check_dependency_p=*/false,
22409 /*type_p=*/true,
22410 /*is_declaration=*/false);
22411 /* If there was a nested-name-specifier, then there *must* be an
22412 identifier. */
22413 if (nested_name_specifier)
22415 type_start_token = cp_lexer_peek_token (parser->lexer);
22416 /* Although the grammar says `identifier', it really means
22417 `class-name' or `template-name'. You are only allowed to
22418 define a class that has already been declared with this
22419 syntax.
22421 The proposed resolution for Core Issue 180 says that wherever
22422 you see `class T::X' you should treat `X' as a type-name.
22424 It is OK to define an inaccessible class; for example:
22426 class A { class B; };
22427 class A::B {};
22429 We do not know if we will see a class-name, or a
22430 template-name. We look for a class-name first, in case the
22431 class-name is a template-id; if we looked for the
22432 template-name first we would stop after the template-name. */
22433 cp_parser_parse_tentatively (parser);
22434 type = cp_parser_class_name (parser,
22435 /*typename_keyword_p=*/false,
22436 /*template_keyword_p=*/false,
22437 class_type,
22438 /*check_dependency_p=*/false,
22439 /*class_head_p=*/true,
22440 /*is_declaration=*/false);
22441 /* If that didn't work, ignore the nested-name-specifier. */
22442 if (!cp_parser_parse_definitely (parser))
22444 invalid_nested_name_p = true;
22445 type_start_token = cp_lexer_peek_token (parser->lexer);
22446 id = cp_parser_identifier (parser);
22447 if (id == error_mark_node)
22448 id = NULL_TREE;
22450 /* If we could not find a corresponding TYPE, treat this
22451 declaration like an unqualified declaration. */
22452 if (type == error_mark_node)
22453 nested_name_specifier = NULL_TREE;
22454 /* Otherwise, count the number of templates used in TYPE and its
22455 containing scopes. */
22456 else
22458 tree scope;
22460 for (scope = TREE_TYPE (type);
22461 scope && TREE_CODE (scope) != NAMESPACE_DECL;
22462 scope = get_containing_scope (scope))
22463 if (TYPE_P (scope)
22464 && CLASS_TYPE_P (scope)
22465 && CLASSTYPE_TEMPLATE_INFO (scope)
22466 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
22467 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
22468 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
22469 ++num_templates;
22472 /* Otherwise, the identifier is optional. */
22473 else
22475 /* We don't know whether what comes next is a template-id,
22476 an identifier, or nothing at all. */
22477 cp_parser_parse_tentatively (parser);
22478 /* Check for a template-id. */
22479 type_start_token = cp_lexer_peek_token (parser->lexer);
22480 id = cp_parser_template_id (parser,
22481 /*template_keyword_p=*/false,
22482 /*check_dependency_p=*/true,
22483 class_key,
22484 /*is_declaration=*/true);
22485 /* If that didn't work, it could still be an identifier. */
22486 if (!cp_parser_parse_definitely (parser))
22488 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22490 type_start_token = cp_lexer_peek_token (parser->lexer);
22491 id = cp_parser_identifier (parser);
22493 else
22494 id = NULL_TREE;
22496 else
22498 template_id_p = true;
22499 ++num_templates;
22503 pop_deferring_access_checks ();
22505 if (id)
22507 cp_parser_check_for_invalid_template_id (parser, id,
22508 class_key,
22509 type_start_token->location);
22511 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
22513 /* If it's not a `:' or a `{' then we can't really be looking at a
22514 class-head, since a class-head only appears as part of a
22515 class-specifier. We have to detect this situation before calling
22516 xref_tag, since that has irreversible side-effects. */
22517 if (!cp_parser_next_token_starts_class_definition_p (parser))
22519 cp_parser_error (parser, "expected %<{%> or %<:%>");
22520 type = error_mark_node;
22521 goto out;
22524 /* At this point, we're going ahead with the class-specifier, even
22525 if some other problem occurs. */
22526 cp_parser_commit_to_tentative_parse (parser);
22527 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
22529 cp_parser_error (parser,
22530 "cannot specify %<override%> for a class");
22531 type = error_mark_node;
22532 goto out;
22534 /* Issue the error about the overly-qualified name now. */
22535 if (qualified_p)
22537 cp_parser_error (parser,
22538 "global qualification of class name is invalid");
22539 type = error_mark_node;
22540 goto out;
22542 else if (invalid_nested_name_p)
22544 cp_parser_error (parser,
22545 "qualified name does not name a class");
22546 type = error_mark_node;
22547 goto out;
22549 else if (nested_name_specifier)
22551 tree scope;
22553 /* Reject typedef-names in class heads. */
22554 if (!DECL_IMPLICIT_TYPEDEF_P (type))
22556 error_at (type_start_token->location,
22557 "invalid class name in declaration of %qD",
22558 type);
22559 type = NULL_TREE;
22560 goto done;
22563 /* Figure out in what scope the declaration is being placed. */
22564 scope = current_scope ();
22565 /* If that scope does not contain the scope in which the
22566 class was originally declared, the program is invalid. */
22567 if (scope && !is_ancestor (scope, nested_name_specifier))
22569 if (at_namespace_scope_p ())
22570 error_at (type_start_token->location,
22571 "declaration of %qD in namespace %qD which does not "
22572 "enclose %qD",
22573 type, scope, nested_name_specifier);
22574 else
22575 error_at (type_start_token->location,
22576 "declaration of %qD in %qD which does not enclose %qD",
22577 type, scope, nested_name_specifier);
22578 type = NULL_TREE;
22579 goto done;
22581 /* [dcl.meaning]
22583 A declarator-id shall not be qualified except for the
22584 definition of a ... nested class outside of its class
22585 ... [or] the definition or explicit instantiation of a
22586 class member of a namespace outside of its namespace. */
22587 if (scope == nested_name_specifier)
22589 permerror (nested_name_specifier_token_start->location,
22590 "extra qualification not allowed");
22591 nested_name_specifier = NULL_TREE;
22592 num_templates = 0;
22595 /* An explicit-specialization must be preceded by "template <>". If
22596 it is not, try to recover gracefully. */
22597 if (at_namespace_scope_p ()
22598 && parser->num_template_parameter_lists == 0
22599 && !processing_template_parmlist
22600 && template_id_p)
22602 /* Build a location of this form:
22603 struct typename <ARGS>
22604 ^~~~~~~~~~~~~~~~~~~~~~
22605 with caret==start at the start token, and
22606 finishing at the end of the type. */
22607 location_t reported_loc
22608 = make_location (class_head_start_location,
22609 class_head_start_location,
22610 get_finish (type_start_token->location));
22611 rich_location richloc (line_table, reported_loc);
22612 richloc.add_fixit_insert_before (class_head_start_location,
22613 "template <> ");
22614 error_at_rich_loc
22615 (&richloc,
22616 "an explicit specialization must be preceded by %<template <>%>");
22617 invalid_explicit_specialization_p = true;
22618 /* Take the same action that would have been taken by
22619 cp_parser_explicit_specialization. */
22620 ++parser->num_template_parameter_lists;
22621 begin_specialization ();
22623 /* There must be no "return" statements between this point and the
22624 end of this function; set "type "to the correct return value and
22625 use "goto done;" to return. */
22626 /* Make sure that the right number of template parameters were
22627 present. */
22628 if (!cp_parser_check_template_parameters (parser, num_templates,
22629 type_start_token->location,
22630 /*declarator=*/NULL))
22632 /* If something went wrong, there is no point in even trying to
22633 process the class-definition. */
22634 type = NULL_TREE;
22635 goto done;
22638 /* Look up the type. */
22639 if (template_id_p)
22641 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
22642 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
22643 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
22645 error_at (type_start_token->location,
22646 "function template %qD redeclared as a class template", id);
22647 type = error_mark_node;
22649 else
22651 type = TREE_TYPE (id);
22652 type = maybe_process_partial_specialization (type);
22654 /* Check the scope while we still know whether or not we had a
22655 nested-name-specifier. */
22656 if (type != error_mark_node)
22657 check_unqualified_spec_or_inst (type, type_start_token->location);
22659 if (nested_name_specifier)
22660 pushed_scope = push_scope (nested_name_specifier);
22662 else if (nested_name_specifier)
22664 tree class_type;
22666 /* Given:
22668 template <typename T> struct S { struct T };
22669 template <typename T> struct S<T>::T { };
22671 we will get a TYPENAME_TYPE when processing the definition of
22672 `S::T'. We need to resolve it to the actual type before we
22673 try to define it. */
22674 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
22676 class_type = resolve_typename_type (TREE_TYPE (type),
22677 /*only_current_p=*/false);
22678 if (TREE_CODE (class_type) != TYPENAME_TYPE)
22679 type = TYPE_NAME (class_type);
22680 else
22682 cp_parser_error (parser, "could not resolve typename type");
22683 type = error_mark_node;
22687 if (maybe_process_partial_specialization (TREE_TYPE (type))
22688 == error_mark_node)
22690 type = NULL_TREE;
22691 goto done;
22694 class_type = current_class_type;
22695 /* Enter the scope indicated by the nested-name-specifier. */
22696 pushed_scope = push_scope (nested_name_specifier);
22697 /* Get the canonical version of this type. */
22698 type = TYPE_MAIN_DECL (TREE_TYPE (type));
22699 /* Call push_template_decl if it seems like we should be defining a
22700 template either from the template headers or the type we're
22701 defining, so that we diagnose both extra and missing headers. */
22702 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
22703 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
22704 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
22706 type = push_template_decl (type);
22707 if (type == error_mark_node)
22709 type = NULL_TREE;
22710 goto done;
22714 type = TREE_TYPE (type);
22715 *nested_name_specifier_p = true;
22717 else /* The name is not a nested name. */
22719 /* If the class was unnamed, create a dummy name. */
22720 if (!id)
22721 id = make_anon_name ();
22722 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
22723 parser->num_template_parameter_lists);
22726 /* Indicate whether this class was declared as a `class' or as a
22727 `struct'. */
22728 if (TREE_CODE (type) == RECORD_TYPE)
22729 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
22730 cp_parser_check_class_key (class_key, type);
22732 /* If this type was already complete, and we see another definition,
22733 that's an error. */
22734 if (type != error_mark_node && COMPLETE_TYPE_P (type))
22736 error_at (type_start_token->location, "redefinition of %q#T",
22737 type);
22738 inform (location_of (type), "previous definition of %q#T",
22739 type);
22740 type = NULL_TREE;
22741 goto done;
22743 else if (type == error_mark_node)
22744 type = NULL_TREE;
22746 if (type)
22748 /* Apply attributes now, before any use of the class as a template
22749 argument in its base list. */
22750 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
22751 fixup_attribute_variants (type);
22754 /* We will have entered the scope containing the class; the names of
22755 base classes should be looked up in that context. For example:
22757 struct A { struct B {}; struct C; };
22758 struct A::C : B {};
22760 is valid. */
22762 /* Get the list of base-classes, if there is one. */
22763 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22765 /* PR59482: enter the class scope so that base-specifiers are looked
22766 up correctly. */
22767 if (type)
22768 pushclass (type);
22769 bases = cp_parser_base_clause (parser);
22770 /* PR59482: get out of the previously pushed class scope so that the
22771 subsequent pops pop the right thing. */
22772 if (type)
22773 popclass ();
22775 else
22776 bases = NULL_TREE;
22778 /* If we're really defining a class, process the base classes.
22779 If they're invalid, fail. */
22780 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22781 xref_basetypes (type, bases);
22783 done:
22784 /* Leave the scope given by the nested-name-specifier. We will
22785 enter the class scope itself while processing the members. */
22786 if (pushed_scope)
22787 pop_scope (pushed_scope);
22789 if (invalid_explicit_specialization_p)
22791 end_specialization ();
22792 --parser->num_template_parameter_lists;
22795 if (type)
22796 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
22797 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
22798 CLASSTYPE_FINAL (type) = 1;
22799 out:
22800 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22801 return type;
22804 /* Parse a class-key.
22806 class-key:
22807 class
22808 struct
22809 union
22811 Returns the kind of class-key specified, or none_type to indicate
22812 error. */
22814 static enum tag_types
22815 cp_parser_class_key (cp_parser* parser)
22817 cp_token *token;
22818 enum tag_types tag_type;
22820 /* Look for the class-key. */
22821 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
22822 if (!token)
22823 return none_type;
22825 /* Check to see if the TOKEN is a class-key. */
22826 tag_type = cp_parser_token_is_class_key (token);
22827 if (!tag_type)
22828 cp_parser_error (parser, "expected class-key");
22829 return tag_type;
22832 /* Parse a type-parameter-key.
22834 type-parameter-key:
22835 class
22836 typename
22839 static void
22840 cp_parser_type_parameter_key (cp_parser* parser)
22842 /* Look for the type-parameter-key. */
22843 enum tag_types tag_type = none_type;
22844 cp_token *token = cp_lexer_peek_token (parser->lexer);
22845 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
22847 cp_lexer_consume_token (parser->lexer);
22848 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
22849 /* typename is not allowed in a template template parameter
22850 by the standard until C++1Z. */
22851 pedwarn (token->location, OPT_Wpedantic,
22852 "ISO C++ forbids typename key in template template parameter;"
22853 " use -std=c++1z or -std=gnu++1z");
22855 else
22856 cp_parser_error (parser, "expected %<class%> or %<typename%>");
22858 return;
22861 /* Parse an (optional) member-specification.
22863 member-specification:
22864 member-declaration member-specification [opt]
22865 access-specifier : member-specification [opt] */
22867 static void
22868 cp_parser_member_specification_opt (cp_parser* parser)
22870 while (true)
22872 cp_token *token;
22873 enum rid keyword;
22875 /* Peek at the next token. */
22876 token = cp_lexer_peek_token (parser->lexer);
22877 /* If it's a `}', or EOF then we've seen all the members. */
22878 if (token->type == CPP_CLOSE_BRACE
22879 || token->type == CPP_EOF
22880 || token->type == CPP_PRAGMA_EOL)
22881 break;
22883 /* See if this token is a keyword. */
22884 keyword = token->keyword;
22885 switch (keyword)
22887 case RID_PUBLIC:
22888 case RID_PROTECTED:
22889 case RID_PRIVATE:
22890 /* Consume the access-specifier. */
22891 cp_lexer_consume_token (parser->lexer);
22892 /* Remember which access-specifier is active. */
22893 current_access_specifier = token->u.value;
22894 /* Look for the `:'. */
22895 cp_parser_require (parser, CPP_COLON, RT_COLON);
22896 break;
22898 default:
22899 /* Accept #pragmas at class scope. */
22900 if (token->type == CPP_PRAGMA)
22902 cp_parser_pragma (parser, pragma_member, NULL);
22903 break;
22906 /* Otherwise, the next construction must be a
22907 member-declaration. */
22908 cp_parser_member_declaration (parser);
22913 /* Parse a member-declaration.
22915 member-declaration:
22916 decl-specifier-seq [opt] member-declarator-list [opt] ;
22917 function-definition ; [opt]
22918 :: [opt] nested-name-specifier template [opt] unqualified-id ;
22919 using-declaration
22920 template-declaration
22921 alias-declaration
22923 member-declarator-list:
22924 member-declarator
22925 member-declarator-list , member-declarator
22927 member-declarator:
22928 declarator pure-specifier [opt]
22929 declarator constant-initializer [opt]
22930 identifier [opt] : constant-expression
22932 GNU Extensions:
22934 member-declaration:
22935 __extension__ member-declaration
22937 member-declarator:
22938 declarator attributes [opt] pure-specifier [opt]
22939 declarator attributes [opt] constant-initializer [opt]
22940 identifier [opt] attributes [opt] : constant-expression
22942 C++0x Extensions:
22944 member-declaration:
22945 static_assert-declaration */
22947 static void
22948 cp_parser_member_declaration (cp_parser* parser)
22950 cp_decl_specifier_seq decl_specifiers;
22951 tree prefix_attributes;
22952 tree decl;
22953 int declares_class_or_enum;
22954 bool friend_p;
22955 cp_token *token = NULL;
22956 cp_token *decl_spec_token_start = NULL;
22957 cp_token *initializer_token_start = NULL;
22958 int saved_pedantic;
22959 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22961 /* Check for the `__extension__' keyword. */
22962 if (cp_parser_extension_opt (parser, &saved_pedantic))
22964 /* Recurse. */
22965 cp_parser_member_declaration (parser);
22966 /* Restore the old value of the PEDANTIC flag. */
22967 pedantic = saved_pedantic;
22969 return;
22972 /* Check for a template-declaration. */
22973 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22975 /* An explicit specialization here is an error condition, and we
22976 expect the specialization handler to detect and report this. */
22977 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
22978 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
22979 cp_parser_explicit_specialization (parser);
22980 else
22981 cp_parser_template_declaration (parser, /*member_p=*/true);
22983 return;
22985 /* Check for a template introduction. */
22986 else if (cp_parser_template_declaration_after_export (parser, true))
22987 return;
22989 /* Check for a using-declaration. */
22990 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
22992 if (cxx_dialect < cxx11)
22994 /* Parse the using-declaration. */
22995 cp_parser_using_declaration (parser,
22996 /*access_declaration_p=*/false);
22997 return;
22999 else
23001 tree decl;
23002 bool alias_decl_expected;
23003 cp_parser_parse_tentatively (parser);
23004 decl = cp_parser_alias_declaration (parser);
23005 /* Note that if we actually see the '=' token after the
23006 identifier, cp_parser_alias_declaration commits the
23007 tentative parse. In that case, we really expect an
23008 alias-declaration. Otherwise, we expect a using
23009 declaration. */
23010 alias_decl_expected =
23011 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23012 cp_parser_parse_definitely (parser);
23014 if (alias_decl_expected)
23015 finish_member_declaration (decl);
23016 else
23017 cp_parser_using_declaration (parser,
23018 /*access_declaration_p=*/false);
23019 return;
23023 /* Check for @defs. */
23024 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23026 tree ivar, member;
23027 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23028 ivar = ivar_chains;
23029 while (ivar)
23031 member = ivar;
23032 ivar = TREE_CHAIN (member);
23033 TREE_CHAIN (member) = NULL_TREE;
23034 finish_member_declaration (member);
23036 return;
23039 /* If the next token is `static_assert' we have a static assertion. */
23040 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23042 cp_parser_static_assert (parser, /*member_p=*/true);
23043 return;
23046 parser->colon_corrects_to_scope_p = false;
23048 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23049 goto out;
23051 /* Parse the decl-specifier-seq. */
23052 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23053 cp_parser_decl_specifier_seq (parser,
23054 CP_PARSER_FLAGS_OPTIONAL,
23055 &decl_specifiers,
23056 &declares_class_or_enum);
23057 /* Check for an invalid type-name. */
23058 if (!decl_specifiers.any_type_specifiers_p
23059 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23060 goto out;
23061 /* If there is no declarator, then the decl-specifier-seq should
23062 specify a type. */
23063 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23065 /* If there was no decl-specifier-seq, and the next token is a
23066 `;', then we have something like:
23068 struct S { ; };
23070 [class.mem]
23072 Each member-declaration shall declare at least one member
23073 name of the class. */
23074 if (!decl_specifiers.any_specifiers_p)
23076 cp_token *token = cp_lexer_peek_token (parser->lexer);
23077 if (!in_system_header_at (token->location))
23078 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
23080 else
23082 tree type;
23084 /* See if this declaration is a friend. */
23085 friend_p = cp_parser_friend_p (&decl_specifiers);
23086 /* If there were decl-specifiers, check to see if there was
23087 a class-declaration. */
23088 type = check_tag_decl (&decl_specifiers,
23089 /*explicit_type_instantiation_p=*/false);
23090 /* Nested classes have already been added to the class, but
23091 a `friend' needs to be explicitly registered. */
23092 if (friend_p)
23094 /* If the `friend' keyword was present, the friend must
23095 be introduced with a class-key. */
23096 if (!declares_class_or_enum && cxx_dialect < cxx11)
23097 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23098 "in C++03 a class-key must be used "
23099 "when declaring a friend");
23100 /* In this case:
23102 template <typename T> struct A {
23103 friend struct A<T>::B;
23106 A<T>::B will be represented by a TYPENAME_TYPE, and
23107 therefore not recognized by check_tag_decl. */
23108 if (!type)
23110 type = decl_specifiers.type;
23111 if (type && TREE_CODE (type) == TYPE_DECL)
23112 type = TREE_TYPE (type);
23114 if (!type || !TYPE_P (type))
23115 error_at (decl_spec_token_start->location,
23116 "friend declaration does not name a class or "
23117 "function");
23118 else
23119 make_friend_class (current_class_type, type,
23120 /*complain=*/true);
23122 /* If there is no TYPE, an error message will already have
23123 been issued. */
23124 else if (!type || type == error_mark_node)
23126 /* An anonymous aggregate has to be handled specially; such
23127 a declaration really declares a data member (with a
23128 particular type), as opposed to a nested class. */
23129 else if (ANON_AGGR_TYPE_P (type))
23131 /* C++11 9.5/6. */
23132 if (decl_specifiers.storage_class != sc_none)
23133 error_at (decl_spec_token_start->location,
23134 "a storage class on an anonymous aggregate "
23135 "in class scope is not allowed");
23137 /* Remove constructors and such from TYPE, now that we
23138 know it is an anonymous aggregate. */
23139 fixup_anonymous_aggr (type);
23140 /* And make the corresponding data member. */
23141 decl = build_decl (decl_spec_token_start->location,
23142 FIELD_DECL, NULL_TREE, type);
23143 /* Add it to the class. */
23144 finish_member_declaration (decl);
23146 else
23147 cp_parser_check_access_in_redeclaration
23148 (TYPE_NAME (type),
23149 decl_spec_token_start->location);
23152 else
23154 bool assume_semicolon = false;
23156 /* Clear attributes from the decl_specifiers but keep them
23157 around as prefix attributes that apply them to the entity
23158 being declared. */
23159 prefix_attributes = decl_specifiers.attributes;
23160 decl_specifiers.attributes = NULL_TREE;
23162 /* See if these declarations will be friends. */
23163 friend_p = cp_parser_friend_p (&decl_specifiers);
23165 /* Keep going until we hit the `;' at the end of the
23166 declaration. */
23167 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23169 tree attributes = NULL_TREE;
23170 tree first_attribute;
23172 /* Peek at the next token. */
23173 token = cp_lexer_peek_token (parser->lexer);
23175 /* Check for a bitfield declaration. */
23176 if (token->type == CPP_COLON
23177 || (token->type == CPP_NAME
23178 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
23179 == CPP_COLON))
23181 tree identifier;
23182 tree width;
23184 /* Get the name of the bitfield. Note that we cannot just
23185 check TOKEN here because it may have been invalidated by
23186 the call to cp_lexer_peek_nth_token above. */
23187 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
23188 identifier = cp_parser_identifier (parser);
23189 else
23190 identifier = NULL_TREE;
23192 /* Consume the `:' token. */
23193 cp_lexer_consume_token (parser->lexer);
23194 /* Get the width of the bitfield. */
23195 width
23196 = cp_parser_constant_expression (parser);
23198 /* Look for attributes that apply to the bitfield. */
23199 attributes = cp_parser_attributes_opt (parser);
23200 /* Remember which attributes are prefix attributes and
23201 which are not. */
23202 first_attribute = attributes;
23203 /* Combine the attributes. */
23204 attributes = chainon (prefix_attributes, attributes);
23206 /* Create the bitfield declaration. */
23207 decl = grokbitfield (identifier
23208 ? make_id_declarator (NULL_TREE,
23209 identifier,
23210 sfk_none)
23211 : NULL,
23212 &decl_specifiers,
23213 width,
23214 attributes);
23216 else
23218 cp_declarator *declarator;
23219 tree initializer;
23220 tree asm_specification;
23221 int ctor_dtor_or_conv_p;
23223 /* Parse the declarator. */
23224 declarator
23225 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23226 &ctor_dtor_or_conv_p,
23227 /*parenthesized_p=*/NULL,
23228 /*member_p=*/true,
23229 friend_p);
23231 /* If something went wrong parsing the declarator, make sure
23232 that we at least consume some tokens. */
23233 if (declarator == cp_error_declarator)
23235 /* Skip to the end of the statement. */
23236 cp_parser_skip_to_end_of_statement (parser);
23237 /* If the next token is not a semicolon, that is
23238 probably because we just skipped over the body of
23239 a function. So, we consume a semicolon if
23240 present, but do not issue an error message if it
23241 is not present. */
23242 if (cp_lexer_next_token_is (parser->lexer,
23243 CPP_SEMICOLON))
23244 cp_lexer_consume_token (parser->lexer);
23245 goto out;
23248 if (declares_class_or_enum & 2)
23249 cp_parser_check_for_definition_in_return_type
23250 (declarator, decl_specifiers.type,
23251 decl_specifiers.locations[ds_type_spec]);
23253 /* Look for an asm-specification. */
23254 asm_specification = cp_parser_asm_specification_opt (parser);
23255 /* Look for attributes that apply to the declaration. */
23256 attributes = cp_parser_attributes_opt (parser);
23257 /* Remember which attributes are prefix attributes and
23258 which are not. */
23259 first_attribute = attributes;
23260 /* Combine the attributes. */
23261 attributes = chainon (prefix_attributes, attributes);
23263 /* If it's an `=', then we have a constant-initializer or a
23264 pure-specifier. It is not correct to parse the
23265 initializer before registering the member declaration
23266 since the member declaration should be in scope while
23267 its initializer is processed. However, the rest of the
23268 front end does not yet provide an interface that allows
23269 us to handle this correctly. */
23270 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23272 /* In [class.mem]:
23274 A pure-specifier shall be used only in the declaration of
23275 a virtual function.
23277 A member-declarator can contain a constant-initializer
23278 only if it declares a static member of integral or
23279 enumeration type.
23281 Therefore, if the DECLARATOR is for a function, we look
23282 for a pure-specifier; otherwise, we look for a
23283 constant-initializer. When we call `grokfield', it will
23284 perform more stringent semantics checks. */
23285 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23286 if (function_declarator_p (declarator)
23287 || (decl_specifiers.type
23288 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23289 && declarator->kind == cdk_id
23290 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23291 == FUNCTION_TYPE)))
23292 initializer = cp_parser_pure_specifier (parser);
23293 else if (decl_specifiers.storage_class != sc_static)
23294 initializer = cp_parser_save_nsdmi (parser);
23295 else if (cxx_dialect >= cxx11)
23297 bool nonconst;
23298 /* Don't require a constant rvalue in C++11, since we
23299 might want a reference constant. We'll enforce
23300 constancy later. */
23301 cp_lexer_consume_token (parser->lexer);
23302 /* Parse the initializer. */
23303 initializer = cp_parser_initializer_clause (parser,
23304 &nonconst);
23306 else
23307 /* Parse the initializer. */
23308 initializer = cp_parser_constant_initializer (parser);
23310 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23311 && !function_declarator_p (declarator))
23313 bool x;
23314 if (decl_specifiers.storage_class != sc_static)
23315 initializer = cp_parser_save_nsdmi (parser);
23316 else
23317 initializer = cp_parser_initializer (parser, &x, &x);
23319 /* Otherwise, there is no initializer. */
23320 else
23321 initializer = NULL_TREE;
23323 /* See if we are probably looking at a function
23324 definition. We are certainly not looking at a
23325 member-declarator. Calling `grokfield' has
23326 side-effects, so we must not do it unless we are sure
23327 that we are looking at a member-declarator. */
23328 if (cp_parser_token_starts_function_definition_p
23329 (cp_lexer_peek_token (parser->lexer)))
23331 /* The grammar does not allow a pure-specifier to be
23332 used when a member function is defined. (It is
23333 possible that this fact is an oversight in the
23334 standard, since a pure function may be defined
23335 outside of the class-specifier. */
23336 if (initializer && initializer_token_start)
23337 error_at (initializer_token_start->location,
23338 "pure-specifier on function-definition");
23339 decl = cp_parser_save_member_function_body (parser,
23340 &decl_specifiers,
23341 declarator,
23342 attributes);
23343 if (parser->fully_implicit_function_template_p)
23344 decl = finish_fully_implicit_template (parser, decl);
23345 /* If the member was not a friend, declare it here. */
23346 if (!friend_p)
23347 finish_member_declaration (decl);
23348 /* Peek at the next token. */
23349 token = cp_lexer_peek_token (parser->lexer);
23350 /* If the next token is a semicolon, consume it. */
23351 if (token->type == CPP_SEMICOLON)
23352 cp_lexer_consume_token (parser->lexer);
23353 goto out;
23355 else
23356 if (declarator->kind == cdk_function)
23357 declarator->id_loc = token->location;
23358 /* Create the declaration. */
23359 decl = grokfield (declarator, &decl_specifiers,
23360 initializer, /*init_const_expr_p=*/true,
23361 asm_specification, attributes);
23362 if (parser->fully_implicit_function_template_p)
23364 if (friend_p)
23365 finish_fully_implicit_template (parser, 0);
23366 else
23367 decl = finish_fully_implicit_template (parser, decl);
23371 cp_finalize_omp_declare_simd (parser, decl);
23372 cp_finalize_oacc_routine (parser, decl, false);
23374 /* Reset PREFIX_ATTRIBUTES. */
23375 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23376 attributes = TREE_CHAIN (attributes);
23377 if (attributes)
23378 TREE_CHAIN (attributes) = NULL_TREE;
23380 /* If there is any qualification still in effect, clear it
23381 now; we will be starting fresh with the next declarator. */
23382 parser->scope = NULL_TREE;
23383 parser->qualifying_scope = NULL_TREE;
23384 parser->object_scope = NULL_TREE;
23385 /* If it's a `,', then there are more declarators. */
23386 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23388 cp_lexer_consume_token (parser->lexer);
23389 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23391 cp_token *token = cp_lexer_previous_token (parser->lexer);
23392 error_at (token->location,
23393 "stray %<,%> at end of member declaration");
23396 /* If the next token isn't a `;', then we have a parse error. */
23397 else if (cp_lexer_next_token_is_not (parser->lexer,
23398 CPP_SEMICOLON))
23400 /* The next token might be a ways away from where the
23401 actual semicolon is missing. Find the previous token
23402 and use that for our error position. */
23403 cp_token *token = cp_lexer_previous_token (parser->lexer);
23404 error_at (token->location,
23405 "expected %<;%> at end of member declaration");
23407 /* Assume that the user meant to provide a semicolon. If
23408 we were to cp_parser_skip_to_end_of_statement, we might
23409 skip to a semicolon inside a member function definition
23410 and issue nonsensical error messages. */
23411 assume_semicolon = true;
23414 if (decl)
23416 /* Add DECL to the list of members. */
23417 if (!friend_p
23418 /* Explicitly include, eg, NSDMIs, for better error
23419 recovery (c++/58650). */
23420 || !DECL_DECLARES_FUNCTION_P (decl))
23421 finish_member_declaration (decl);
23423 if (TREE_CODE (decl) == FUNCTION_DECL)
23424 cp_parser_save_default_args (parser, decl);
23425 else if (TREE_CODE (decl) == FIELD_DECL
23426 && !DECL_C_BIT_FIELD (decl)
23427 && DECL_INITIAL (decl))
23428 /* Add DECL to the queue of NSDMI to be parsed later. */
23429 vec_safe_push (unparsed_nsdmis, decl);
23432 if (assume_semicolon)
23433 goto out;
23437 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23438 out:
23439 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23442 /* Parse a pure-specifier.
23444 pure-specifier:
23447 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23448 Otherwise, ERROR_MARK_NODE is returned. */
23450 static tree
23451 cp_parser_pure_specifier (cp_parser* parser)
23453 cp_token *token;
23455 /* Look for the `=' token. */
23456 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23457 return error_mark_node;
23458 /* Look for the `0' token. */
23459 token = cp_lexer_peek_token (parser->lexer);
23461 if (token->type == CPP_EOF
23462 || token->type == CPP_PRAGMA_EOL)
23463 return error_mark_node;
23465 cp_lexer_consume_token (parser->lexer);
23467 /* Accept = default or = delete in c++0x mode. */
23468 if (token->keyword == RID_DEFAULT
23469 || token->keyword == RID_DELETE)
23471 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
23472 return token->u.value;
23475 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
23476 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
23478 cp_parser_error (parser,
23479 "invalid pure specifier (only %<= 0%> is allowed)");
23480 cp_parser_skip_to_end_of_statement (parser);
23481 return error_mark_node;
23483 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
23485 error_at (token->location, "templates may not be %<virtual%>");
23486 return error_mark_node;
23489 return integer_zero_node;
23492 /* Parse a constant-initializer.
23494 constant-initializer:
23495 = constant-expression
23497 Returns a representation of the constant-expression. */
23499 static tree
23500 cp_parser_constant_initializer (cp_parser* parser)
23502 /* Look for the `=' token. */
23503 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23504 return error_mark_node;
23506 /* It is invalid to write:
23508 struct S { static const int i = { 7 }; };
23511 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23513 cp_parser_error (parser,
23514 "a brace-enclosed initializer is not allowed here");
23515 /* Consume the opening brace. */
23516 cp_lexer_consume_token (parser->lexer);
23517 /* Skip the initializer. */
23518 cp_parser_skip_to_closing_brace (parser);
23519 /* Look for the trailing `}'. */
23520 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
23522 return error_mark_node;
23525 return cp_parser_constant_expression (parser);
23528 /* Derived classes [gram.class.derived] */
23530 /* Parse a base-clause.
23532 base-clause:
23533 : base-specifier-list
23535 base-specifier-list:
23536 base-specifier ... [opt]
23537 base-specifier-list , base-specifier ... [opt]
23539 Returns a TREE_LIST representing the base-classes, in the order in
23540 which they were declared. The representation of each node is as
23541 described by cp_parser_base_specifier.
23543 In the case that no bases are specified, this function will return
23544 NULL_TREE, not ERROR_MARK_NODE. */
23546 static tree
23547 cp_parser_base_clause (cp_parser* parser)
23549 tree bases = NULL_TREE;
23551 /* Look for the `:' that begins the list. */
23552 cp_parser_require (parser, CPP_COLON, RT_COLON);
23554 /* Scan the base-specifier-list. */
23555 while (true)
23557 cp_token *token;
23558 tree base;
23559 bool pack_expansion_p = false;
23561 /* Look for the base-specifier. */
23562 base = cp_parser_base_specifier (parser);
23563 /* Look for the (optional) ellipsis. */
23564 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23566 /* Consume the `...'. */
23567 cp_lexer_consume_token (parser->lexer);
23569 pack_expansion_p = true;
23572 /* Add BASE to the front of the list. */
23573 if (base && base != error_mark_node)
23575 if (pack_expansion_p)
23576 /* Make this a pack expansion type. */
23577 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
23579 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
23581 TREE_CHAIN (base) = bases;
23582 bases = base;
23585 /* Peek at the next token. */
23586 token = cp_lexer_peek_token (parser->lexer);
23587 /* If it's not a comma, then the list is complete. */
23588 if (token->type != CPP_COMMA)
23589 break;
23590 /* Consume the `,'. */
23591 cp_lexer_consume_token (parser->lexer);
23594 /* PARSER->SCOPE may still be non-NULL at this point, if the last
23595 base class had a qualified name. However, the next name that
23596 appears is certainly not qualified. */
23597 parser->scope = NULL_TREE;
23598 parser->qualifying_scope = NULL_TREE;
23599 parser->object_scope = NULL_TREE;
23601 return nreverse (bases);
23604 /* Parse a base-specifier.
23606 base-specifier:
23607 :: [opt] nested-name-specifier [opt] class-name
23608 virtual access-specifier [opt] :: [opt] nested-name-specifier
23609 [opt] class-name
23610 access-specifier virtual [opt] :: [opt] nested-name-specifier
23611 [opt] class-name
23613 Returns a TREE_LIST. The TREE_PURPOSE will be one of
23614 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
23615 indicate the specifiers provided. The TREE_VALUE will be a TYPE
23616 (or the ERROR_MARK_NODE) indicating the type that was specified. */
23618 static tree
23619 cp_parser_base_specifier (cp_parser* parser)
23621 cp_token *token;
23622 bool done = false;
23623 bool virtual_p = false;
23624 bool duplicate_virtual_error_issued_p = false;
23625 bool duplicate_access_error_issued_p = false;
23626 bool class_scope_p, template_p;
23627 tree access = access_default_node;
23628 tree type;
23630 /* Process the optional `virtual' and `access-specifier'. */
23631 while (!done)
23633 /* Peek at the next token. */
23634 token = cp_lexer_peek_token (parser->lexer);
23635 /* Process `virtual'. */
23636 switch (token->keyword)
23638 case RID_VIRTUAL:
23639 /* If `virtual' appears more than once, issue an error. */
23640 if (virtual_p && !duplicate_virtual_error_issued_p)
23642 cp_parser_error (parser,
23643 "%<virtual%> specified more than once in base-specified");
23644 duplicate_virtual_error_issued_p = true;
23647 virtual_p = true;
23649 /* Consume the `virtual' token. */
23650 cp_lexer_consume_token (parser->lexer);
23652 break;
23654 case RID_PUBLIC:
23655 case RID_PROTECTED:
23656 case RID_PRIVATE:
23657 /* If more than one access specifier appears, issue an
23658 error. */
23659 if (access != access_default_node
23660 && !duplicate_access_error_issued_p)
23662 cp_parser_error (parser,
23663 "more than one access specifier in base-specified");
23664 duplicate_access_error_issued_p = true;
23667 access = ridpointers[(int) token->keyword];
23669 /* Consume the access-specifier. */
23670 cp_lexer_consume_token (parser->lexer);
23672 break;
23674 default:
23675 done = true;
23676 break;
23679 /* It is not uncommon to see programs mechanically, erroneously, use
23680 the 'typename' keyword to denote (dependent) qualified types
23681 as base classes. */
23682 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
23684 token = cp_lexer_peek_token (parser->lexer);
23685 if (!processing_template_decl)
23686 error_at (token->location,
23687 "keyword %<typename%> not allowed outside of templates");
23688 else
23689 error_at (token->location,
23690 "keyword %<typename%> not allowed in this context "
23691 "(the base class is implicitly a type)");
23692 cp_lexer_consume_token (parser->lexer);
23695 /* Look for the optional `::' operator. */
23696 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
23697 /* Look for the nested-name-specifier. The simplest way to
23698 implement:
23700 [temp.res]
23702 The keyword `typename' is not permitted in a base-specifier or
23703 mem-initializer; in these contexts a qualified name that
23704 depends on a template-parameter is implicitly assumed to be a
23705 type name.
23707 is to pretend that we have seen the `typename' keyword at this
23708 point. */
23709 cp_parser_nested_name_specifier_opt (parser,
23710 /*typename_keyword_p=*/true,
23711 /*check_dependency_p=*/true,
23712 /*type_p=*/true,
23713 /*is_declaration=*/true);
23714 /* If the base class is given by a qualified name, assume that names
23715 we see are type names or templates, as appropriate. */
23716 class_scope_p = (parser->scope && TYPE_P (parser->scope));
23717 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
23719 if (!parser->scope
23720 && cp_lexer_next_token_is_decltype (parser->lexer))
23721 /* DR 950 allows decltype as a base-specifier. */
23722 type = cp_parser_decltype (parser);
23723 else
23725 /* Otherwise, look for the class-name. */
23726 type = cp_parser_class_name (parser,
23727 class_scope_p,
23728 template_p,
23729 typename_type,
23730 /*check_dependency_p=*/true,
23731 /*class_head_p=*/false,
23732 /*is_declaration=*/true);
23733 type = TREE_TYPE (type);
23736 if (type == error_mark_node)
23737 return error_mark_node;
23739 return finish_base_specifier (type, access, virtual_p);
23742 /* Exception handling [gram.exception] */
23744 /* Parse an (optional) noexcept-specification.
23746 noexcept-specification:
23747 noexcept ( constant-expression ) [opt]
23749 If no noexcept-specification is present, returns NULL_TREE.
23750 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
23751 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
23752 there are no parentheses. CONSUMED_EXPR will be set accordingly.
23753 Otherwise, returns a noexcept specification unless RETURN_COND is true,
23754 in which case a boolean condition is returned instead. */
23756 static tree
23757 cp_parser_noexcept_specification_opt (cp_parser* parser,
23758 bool require_constexpr,
23759 bool* consumed_expr,
23760 bool return_cond)
23762 cp_token *token;
23763 const char *saved_message;
23765 /* Peek at the next token. */
23766 token = cp_lexer_peek_token (parser->lexer);
23768 /* Is it a noexcept-specification? */
23769 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
23771 tree expr;
23772 cp_lexer_consume_token (parser->lexer);
23774 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
23776 cp_lexer_consume_token (parser->lexer);
23778 if (require_constexpr)
23780 /* Types may not be defined in an exception-specification. */
23781 saved_message = parser->type_definition_forbidden_message;
23782 parser->type_definition_forbidden_message
23783 = G_("types may not be defined in an exception-specification");
23785 expr = cp_parser_constant_expression (parser);
23787 /* Restore the saved message. */
23788 parser->type_definition_forbidden_message = saved_message;
23790 else
23792 expr = cp_parser_expression (parser);
23793 *consumed_expr = true;
23796 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23798 else
23800 expr = boolean_true_node;
23801 if (!require_constexpr)
23802 *consumed_expr = false;
23805 /* We cannot build a noexcept-spec right away because this will check
23806 that expr is a constexpr. */
23807 if (!return_cond)
23808 return build_noexcept_spec (expr, tf_warning_or_error);
23809 else
23810 return expr;
23812 else
23813 return NULL_TREE;
23816 /* Parse an (optional) exception-specification.
23818 exception-specification:
23819 throw ( type-id-list [opt] )
23821 Returns a TREE_LIST representing the exception-specification. The
23822 TREE_VALUE of each node is a type. */
23824 static tree
23825 cp_parser_exception_specification_opt (cp_parser* parser)
23827 cp_token *token;
23828 tree type_id_list;
23829 const char *saved_message;
23831 /* Peek at the next token. */
23832 token = cp_lexer_peek_token (parser->lexer);
23834 /* Is it a noexcept-specification? */
23835 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
23836 false);
23837 if (type_id_list != NULL_TREE)
23838 return type_id_list;
23840 /* If it's not `throw', then there's no exception-specification. */
23841 if (!cp_parser_is_keyword (token, RID_THROW))
23842 return NULL_TREE;
23844 location_t loc = token->location;
23846 /* Consume the `throw'. */
23847 cp_lexer_consume_token (parser->lexer);
23849 /* Look for the `('. */
23850 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23852 /* Peek at the next token. */
23853 token = cp_lexer_peek_token (parser->lexer);
23854 /* If it's not a `)', then there is a type-id-list. */
23855 if (token->type != CPP_CLOSE_PAREN)
23857 /* Types may not be defined in an exception-specification. */
23858 saved_message = parser->type_definition_forbidden_message;
23859 parser->type_definition_forbidden_message
23860 = G_("types may not be defined in an exception-specification");
23861 /* Parse the type-id-list. */
23862 type_id_list = cp_parser_type_id_list (parser);
23863 /* Restore the saved message. */
23864 parser->type_definition_forbidden_message = saved_message;
23866 if (cxx_dialect >= cxx1z)
23868 error_at (loc, "ISO C++1z does not allow dynamic exception "
23869 "specifications");
23870 type_id_list = NULL_TREE;
23872 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
23873 warning_at (loc, OPT_Wdeprecated,
23874 "dynamic exception specifications are deprecated in C++11;"
23875 " use %<noexcept%> instead");
23877 /* In C++17, throw() is equivalent to noexcept (true). throw()
23878 is deprecated in C++11 and above as well, but is still widely used,
23879 so don't warn about it yet. */
23880 else if (cxx_dialect >= cxx1z)
23881 type_id_list = noexcept_true_spec;
23882 else
23883 type_id_list = empty_except_spec;
23885 /* Look for the `)'. */
23886 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23888 return type_id_list;
23891 /* Parse an (optional) type-id-list.
23893 type-id-list:
23894 type-id ... [opt]
23895 type-id-list , type-id ... [opt]
23897 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
23898 in the order that the types were presented. */
23900 static tree
23901 cp_parser_type_id_list (cp_parser* parser)
23903 tree types = NULL_TREE;
23905 while (true)
23907 cp_token *token;
23908 tree type;
23910 token = cp_lexer_peek_token (parser->lexer);
23912 /* Get the next type-id. */
23913 type = cp_parser_type_id (parser);
23914 /* Check for invalid 'auto'. */
23915 if (flag_concepts && type_uses_auto (type))
23917 error_at (token->location,
23918 "invalid use of %<auto%> in exception-specification");
23919 type = error_mark_node;
23921 /* Parse the optional ellipsis. */
23922 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23924 /* Consume the `...'. */
23925 cp_lexer_consume_token (parser->lexer);
23927 /* Turn the type into a pack expansion expression. */
23928 type = make_pack_expansion (type);
23930 /* Add it to the list. */
23931 types = add_exception_specifier (types, type, /*complain=*/1);
23932 /* Peek at the next token. */
23933 token = cp_lexer_peek_token (parser->lexer);
23934 /* If it is not a `,', we are done. */
23935 if (token->type != CPP_COMMA)
23936 break;
23937 /* Consume the `,'. */
23938 cp_lexer_consume_token (parser->lexer);
23941 return nreverse (types);
23944 /* Parse a try-block.
23946 try-block:
23947 try compound-statement handler-seq */
23949 static tree
23950 cp_parser_try_block (cp_parser* parser)
23952 tree try_block;
23954 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
23955 if (parser->in_function_body
23956 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
23957 error ("%<try%> in %<constexpr%> function");
23959 try_block = begin_try_block ();
23960 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
23961 finish_try_block (try_block);
23962 cp_parser_handler_seq (parser);
23963 finish_handler_sequence (try_block);
23965 return try_block;
23968 /* Parse a function-try-block.
23970 function-try-block:
23971 try ctor-initializer [opt] function-body handler-seq */
23973 static bool
23974 cp_parser_function_try_block (cp_parser* parser)
23976 tree compound_stmt;
23977 tree try_block;
23978 bool ctor_initializer_p;
23980 /* Look for the `try' keyword. */
23981 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
23982 return false;
23983 /* Let the rest of the front end know where we are. */
23984 try_block = begin_function_try_block (&compound_stmt);
23985 /* Parse the function-body. */
23986 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23987 (parser, /*in_function_try_block=*/true);
23988 /* We're done with the `try' part. */
23989 finish_function_try_block (try_block);
23990 /* Parse the handlers. */
23991 cp_parser_handler_seq (parser);
23992 /* We're done with the handlers. */
23993 finish_function_handler_sequence (try_block, compound_stmt);
23995 return ctor_initializer_p;
23998 /* Parse a handler-seq.
24000 handler-seq:
24001 handler handler-seq [opt] */
24003 static void
24004 cp_parser_handler_seq (cp_parser* parser)
24006 while (true)
24008 cp_token *token;
24010 /* Parse the handler. */
24011 cp_parser_handler (parser);
24012 /* Peek at the next token. */
24013 token = cp_lexer_peek_token (parser->lexer);
24014 /* If it's not `catch' then there are no more handlers. */
24015 if (!cp_parser_is_keyword (token, RID_CATCH))
24016 break;
24020 /* Parse a handler.
24022 handler:
24023 catch ( exception-declaration ) compound-statement */
24025 static void
24026 cp_parser_handler (cp_parser* parser)
24028 tree handler;
24029 tree declaration;
24031 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24032 handler = begin_handler ();
24033 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24034 declaration = cp_parser_exception_declaration (parser);
24035 finish_handler_parms (declaration, handler);
24036 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24037 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24038 finish_handler (handler);
24041 /* Parse an exception-declaration.
24043 exception-declaration:
24044 type-specifier-seq declarator
24045 type-specifier-seq abstract-declarator
24046 type-specifier-seq
24049 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24050 ellipsis variant is used. */
24052 static tree
24053 cp_parser_exception_declaration (cp_parser* parser)
24055 cp_decl_specifier_seq type_specifiers;
24056 cp_declarator *declarator;
24057 const char *saved_message;
24059 /* If it's an ellipsis, it's easy to handle. */
24060 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24062 /* Consume the `...' token. */
24063 cp_lexer_consume_token (parser->lexer);
24064 return NULL_TREE;
24067 /* Types may not be defined in exception-declarations. */
24068 saved_message = parser->type_definition_forbidden_message;
24069 parser->type_definition_forbidden_message
24070 = G_("types may not be defined in exception-declarations");
24072 /* Parse the type-specifier-seq. */
24073 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24074 /*is_trailing_return=*/false,
24075 &type_specifiers);
24076 /* If it's a `)', then there is no declarator. */
24077 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24078 declarator = NULL;
24079 else
24080 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24081 /*ctor_dtor_or_conv_p=*/NULL,
24082 /*parenthesized_p=*/NULL,
24083 /*member_p=*/false,
24084 /*friend_p=*/false);
24086 /* Restore the saved message. */
24087 parser->type_definition_forbidden_message = saved_message;
24089 if (!type_specifiers.any_specifiers_p)
24090 return error_mark_node;
24092 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24095 /* Parse a throw-expression.
24097 throw-expression:
24098 throw assignment-expression [opt]
24100 Returns a THROW_EXPR representing the throw-expression. */
24102 static tree
24103 cp_parser_throw_expression (cp_parser* parser)
24105 tree expression;
24106 cp_token* token;
24108 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24109 token = cp_lexer_peek_token (parser->lexer);
24110 /* Figure out whether or not there is an assignment-expression
24111 following the "throw" keyword. */
24112 if (token->type == CPP_COMMA
24113 || token->type == CPP_SEMICOLON
24114 || token->type == CPP_CLOSE_PAREN
24115 || token->type == CPP_CLOSE_SQUARE
24116 || token->type == CPP_CLOSE_BRACE
24117 || token->type == CPP_COLON)
24118 expression = NULL_TREE;
24119 else
24120 expression = cp_parser_assignment_expression (parser);
24122 return build_throw (expression);
24125 /* GNU Extensions */
24127 /* Parse an (optional) asm-specification.
24129 asm-specification:
24130 asm ( string-literal )
24132 If the asm-specification is present, returns a STRING_CST
24133 corresponding to the string-literal. Otherwise, returns
24134 NULL_TREE. */
24136 static tree
24137 cp_parser_asm_specification_opt (cp_parser* parser)
24139 cp_token *token;
24140 tree asm_specification;
24142 /* Peek at the next token. */
24143 token = cp_lexer_peek_token (parser->lexer);
24144 /* If the next token isn't the `asm' keyword, then there's no
24145 asm-specification. */
24146 if (!cp_parser_is_keyword (token, RID_ASM))
24147 return NULL_TREE;
24149 /* Consume the `asm' token. */
24150 cp_lexer_consume_token (parser->lexer);
24151 /* Look for the `('. */
24152 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24154 /* Look for the string-literal. */
24155 asm_specification = cp_parser_string_literal (parser, false, false);
24157 /* Look for the `)'. */
24158 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24160 return asm_specification;
24163 /* Parse an asm-operand-list.
24165 asm-operand-list:
24166 asm-operand
24167 asm-operand-list , asm-operand
24169 asm-operand:
24170 string-literal ( expression )
24171 [ string-literal ] string-literal ( expression )
24173 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24174 each node is the expression. The TREE_PURPOSE is itself a
24175 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24176 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24177 is a STRING_CST for the string literal before the parenthesis. Returns
24178 ERROR_MARK_NODE if any of the operands are invalid. */
24180 static tree
24181 cp_parser_asm_operand_list (cp_parser* parser)
24183 tree asm_operands = NULL_TREE;
24184 bool invalid_operands = false;
24186 while (true)
24188 tree string_literal;
24189 tree expression;
24190 tree name;
24192 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24194 /* Consume the `[' token. */
24195 cp_lexer_consume_token (parser->lexer);
24196 /* Read the operand name. */
24197 name = cp_parser_identifier (parser);
24198 if (name != error_mark_node)
24199 name = build_string (IDENTIFIER_LENGTH (name),
24200 IDENTIFIER_POINTER (name));
24201 /* Look for the closing `]'. */
24202 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24204 else
24205 name = NULL_TREE;
24206 /* Look for the string-literal. */
24207 string_literal = cp_parser_string_literal (parser, false, false);
24209 /* Look for the `('. */
24210 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24211 /* Parse the expression. */
24212 expression = cp_parser_expression (parser);
24213 /* Look for the `)'. */
24214 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24216 if (name == error_mark_node
24217 || string_literal == error_mark_node
24218 || expression == error_mark_node)
24219 invalid_operands = true;
24221 /* Add this operand to the list. */
24222 asm_operands = tree_cons (build_tree_list (name, string_literal),
24223 expression,
24224 asm_operands);
24225 /* If the next token is not a `,', there are no more
24226 operands. */
24227 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24228 break;
24229 /* Consume the `,'. */
24230 cp_lexer_consume_token (parser->lexer);
24233 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24236 /* Parse an asm-clobber-list.
24238 asm-clobber-list:
24239 string-literal
24240 asm-clobber-list , string-literal
24242 Returns a TREE_LIST, indicating the clobbers in the order that they
24243 appeared. The TREE_VALUE of each node is a STRING_CST. */
24245 static tree
24246 cp_parser_asm_clobber_list (cp_parser* parser)
24248 tree clobbers = NULL_TREE;
24250 while (true)
24252 tree string_literal;
24254 /* Look for the string literal. */
24255 string_literal = cp_parser_string_literal (parser, false, false);
24256 /* Add it to the list. */
24257 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24258 /* If the next token is not a `,', then the list is
24259 complete. */
24260 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24261 break;
24262 /* Consume the `,' token. */
24263 cp_lexer_consume_token (parser->lexer);
24266 return clobbers;
24269 /* Parse an asm-label-list.
24271 asm-label-list:
24272 identifier
24273 asm-label-list , identifier
24275 Returns a TREE_LIST, indicating the labels in the order that they
24276 appeared. The TREE_VALUE of each node is a label. */
24278 static tree
24279 cp_parser_asm_label_list (cp_parser* parser)
24281 tree labels = NULL_TREE;
24283 while (true)
24285 tree identifier, label, name;
24287 /* Look for the identifier. */
24288 identifier = cp_parser_identifier (parser);
24289 if (!error_operand_p (identifier))
24291 label = lookup_label (identifier);
24292 if (TREE_CODE (label) == LABEL_DECL)
24294 TREE_USED (label) = 1;
24295 check_goto (label);
24296 name = build_string (IDENTIFIER_LENGTH (identifier),
24297 IDENTIFIER_POINTER (identifier));
24298 labels = tree_cons (name, label, labels);
24301 /* If the next token is not a `,', then the list is
24302 complete. */
24303 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24304 break;
24305 /* Consume the `,' token. */
24306 cp_lexer_consume_token (parser->lexer);
24309 return nreverse (labels);
24312 /* Return TRUE iff the next tokens in the stream are possibly the
24313 beginning of a GNU extension attribute. */
24315 static bool
24316 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24318 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24321 /* Return TRUE iff the next tokens in the stream are possibly the
24322 beginning of a standard C++-11 attribute specifier. */
24324 static bool
24325 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24327 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24330 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24331 beginning of a standard C++-11 attribute specifier. */
24333 static bool
24334 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24336 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24338 return (cxx_dialect >= cxx11
24339 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24340 || (token->type == CPP_OPEN_SQUARE
24341 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24342 && token->type == CPP_OPEN_SQUARE)));
24345 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24346 beginning of a GNU extension attribute. */
24348 static bool
24349 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24351 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24353 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24356 /* Return true iff the next tokens can be the beginning of either a
24357 GNU attribute list, or a standard C++11 attribute sequence. */
24359 static bool
24360 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
24362 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
24363 || cp_next_tokens_can_be_std_attribute_p (parser));
24366 /* Return true iff the next Nth tokens can be the beginning of either
24367 a GNU attribute list, or a standard C++11 attribute sequence. */
24369 static bool
24370 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
24372 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
24373 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
24376 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24377 of GNU attributes, or return NULL. */
24379 static tree
24380 cp_parser_attributes_opt (cp_parser *parser)
24382 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
24383 return cp_parser_gnu_attributes_opt (parser);
24384 return cp_parser_std_attribute_spec_seq (parser);
24387 #define CILK_SIMD_FN_CLAUSE_MASK \
24388 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
24389 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
24390 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
24391 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
24392 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
24394 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
24395 vector [(<clauses>)] */
24397 static void
24398 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
24400 bool first_p = parser->cilk_simd_fn_info == NULL;
24401 cp_token *token = v_token;
24402 if (first_p)
24404 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
24405 parser->cilk_simd_fn_info->error_seen = false;
24406 parser->cilk_simd_fn_info->fndecl_seen = false;
24407 parser->cilk_simd_fn_info->tokens = vNULL;
24408 parser->cilk_simd_fn_info->clauses = NULL_TREE;
24410 int paren_scope = 0;
24411 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24413 cp_lexer_consume_token (parser->lexer);
24414 v_token = cp_lexer_peek_token (parser->lexer);
24415 paren_scope++;
24417 while (paren_scope > 0)
24419 token = cp_lexer_peek_token (parser->lexer);
24420 if (token->type == CPP_OPEN_PAREN)
24421 paren_scope++;
24422 else if (token->type == CPP_CLOSE_PAREN)
24423 paren_scope--;
24424 /* Do not push the last ')' */
24425 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
24426 cp_lexer_consume_token (parser->lexer);
24429 token->type = CPP_PRAGMA_EOL;
24430 parser->lexer->next_token = token;
24431 cp_lexer_consume_token (parser->lexer);
24433 struct cp_token_cache *cp
24434 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
24435 parser->cilk_simd_fn_info->tokens.safe_push (cp);
24438 /* Parse an (optional) series of attributes.
24440 attributes:
24441 attributes attribute
24443 attribute:
24444 __attribute__ (( attribute-list [opt] ))
24446 The return value is as for cp_parser_gnu_attribute_list. */
24448 static tree
24449 cp_parser_gnu_attributes_opt (cp_parser* parser)
24451 tree attributes = NULL_TREE;
24453 while (true)
24455 cp_token *token;
24456 tree attribute_list;
24457 bool ok = true;
24459 /* Peek at the next token. */
24460 token = cp_lexer_peek_token (parser->lexer);
24461 /* If it's not `__attribute__', then we're done. */
24462 if (token->keyword != RID_ATTRIBUTE)
24463 break;
24465 /* Consume the `__attribute__' keyword. */
24466 cp_lexer_consume_token (parser->lexer);
24467 /* Look for the two `(' tokens. */
24468 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24469 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24471 /* Peek at the next token. */
24472 token = cp_lexer_peek_token (parser->lexer);
24473 if (token->type != CPP_CLOSE_PAREN)
24474 /* Parse the attribute-list. */
24475 attribute_list = cp_parser_gnu_attribute_list (parser);
24476 else
24477 /* If the next token is a `)', then there is no attribute
24478 list. */
24479 attribute_list = NULL;
24481 /* Look for the two `)' tokens. */
24482 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24483 ok = false;
24484 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24485 ok = false;
24486 if (!ok)
24487 cp_parser_skip_to_end_of_statement (parser);
24489 /* Add these new attributes to the list. */
24490 attributes = chainon (attributes, attribute_list);
24493 return attributes;
24496 /* Parse a GNU attribute-list.
24498 attribute-list:
24499 attribute
24500 attribute-list , attribute
24502 attribute:
24503 identifier
24504 identifier ( identifier )
24505 identifier ( identifier , expression-list )
24506 identifier ( expression-list )
24508 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
24509 to an attribute. The TREE_PURPOSE of each node is the identifier
24510 indicating which attribute is in use. The TREE_VALUE represents
24511 the arguments, if any. */
24513 static tree
24514 cp_parser_gnu_attribute_list (cp_parser* parser)
24516 tree attribute_list = NULL_TREE;
24517 bool save_translate_strings_p = parser->translate_strings_p;
24519 parser->translate_strings_p = false;
24520 while (true)
24522 cp_token *token;
24523 tree identifier;
24524 tree attribute;
24526 /* Look for the identifier. We also allow keywords here; for
24527 example `__attribute__ ((const))' is legal. */
24528 token = cp_lexer_peek_token (parser->lexer);
24529 if (token->type == CPP_NAME
24530 || token->type == CPP_KEYWORD)
24532 tree arguments = NULL_TREE;
24534 /* Consume the token, but save it since we need it for the
24535 SIMD enabled function parsing. */
24536 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
24538 /* Save away the identifier that indicates which attribute
24539 this is. */
24540 identifier = (token->type == CPP_KEYWORD)
24541 /* For keywords, use the canonical spelling, not the
24542 parsed identifier. */
24543 ? ridpointers[(int) token->keyword]
24544 : id_token->u.value;
24546 attribute = build_tree_list (identifier, NULL_TREE);
24548 /* Peek at the next token. */
24549 token = cp_lexer_peek_token (parser->lexer);
24550 /* If it's an `(', then parse the attribute arguments. */
24551 if (token->type == CPP_OPEN_PAREN)
24553 vec<tree, va_gc> *vec;
24554 int attr_flag = (attribute_takes_identifier_p (identifier)
24555 ? id_attr : normal_attr);
24556 if (is_cilkplus_vector_p (identifier))
24558 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24559 continue;
24561 else
24562 vec = cp_parser_parenthesized_expression_list
24563 (parser, attr_flag, /*cast_p=*/false,
24564 /*allow_expansion_p=*/false,
24565 /*non_constant_p=*/NULL);
24566 if (vec == NULL)
24567 arguments = error_mark_node;
24568 else
24570 arguments = build_tree_list_vec (vec);
24571 release_tree_vector (vec);
24573 /* Save the arguments away. */
24574 TREE_VALUE (attribute) = arguments;
24576 else if (is_cilkplus_vector_p (identifier))
24578 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24579 continue;
24582 if (arguments != error_mark_node)
24584 /* Add this attribute to the list. */
24585 TREE_CHAIN (attribute) = attribute_list;
24586 attribute_list = attribute;
24589 token = cp_lexer_peek_token (parser->lexer);
24591 /* Now, look for more attributes. If the next token isn't a
24592 `,', we're done. */
24593 if (token->type != CPP_COMMA)
24594 break;
24596 /* Consume the comma and keep going. */
24597 cp_lexer_consume_token (parser->lexer);
24599 parser->translate_strings_p = save_translate_strings_p;
24601 /* We built up the list in reverse order. */
24602 return nreverse (attribute_list);
24605 /* Parse a standard C++11 attribute.
24607 The returned representation is a TREE_LIST which TREE_PURPOSE is
24608 the scoped name of the attribute, and the TREE_VALUE is its
24609 arguments list.
24611 Note that the scoped name of the attribute is itself a TREE_LIST
24612 which TREE_PURPOSE is the namespace of the attribute, and
24613 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
24614 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
24615 and which TREE_PURPOSE is directly the attribute name.
24617 Clients of the attribute code should use get_attribute_namespace
24618 and get_attribute_name to get the actual namespace and name of
24619 attributes, regardless of their being GNU or C++11 attributes.
24621 attribute:
24622 attribute-token attribute-argument-clause [opt]
24624 attribute-token:
24625 identifier
24626 attribute-scoped-token
24628 attribute-scoped-token:
24629 attribute-namespace :: identifier
24631 attribute-namespace:
24632 identifier
24634 attribute-argument-clause:
24635 ( balanced-token-seq )
24637 balanced-token-seq:
24638 balanced-token [opt]
24639 balanced-token-seq balanced-token
24641 balanced-token:
24642 ( balanced-token-seq )
24643 [ balanced-token-seq ]
24644 { balanced-token-seq }. */
24646 static tree
24647 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
24649 tree attribute, attr_id = NULL_TREE, arguments;
24650 cp_token *token;
24652 /* First, parse name of the attribute, a.k.a attribute-token. */
24654 token = cp_lexer_peek_token (parser->lexer);
24655 if (token->type == CPP_NAME)
24656 attr_id = token->u.value;
24657 else if (token->type == CPP_KEYWORD)
24658 attr_id = ridpointers[(int) token->keyword];
24659 else if (token->flags & NAMED_OP)
24660 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
24662 if (attr_id == NULL_TREE)
24663 return NULL_TREE;
24665 cp_lexer_consume_token (parser->lexer);
24667 token = cp_lexer_peek_token (parser->lexer);
24668 if (token->type == CPP_SCOPE)
24670 /* We are seeing a scoped attribute token. */
24672 cp_lexer_consume_token (parser->lexer);
24673 if (attr_ns)
24674 error_at (token->location, "attribute using prefix used together "
24675 "with scoped attribute token");
24676 attr_ns = attr_id;
24678 token = cp_lexer_consume_token (parser->lexer);
24679 if (token->type == CPP_NAME)
24680 attr_id = token->u.value;
24681 else if (token->type == CPP_KEYWORD)
24682 attr_id = ridpointers[(int) token->keyword];
24683 else if (token->flags & NAMED_OP)
24684 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
24685 else
24687 error_at (token->location,
24688 "expected an identifier for the attribute name");
24689 return error_mark_node;
24691 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
24692 NULL_TREE);
24693 token = cp_lexer_peek_token (parser->lexer);
24695 else if (attr_ns)
24696 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
24697 NULL_TREE);
24698 else
24700 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
24701 NULL_TREE);
24702 /* C++11 noreturn attribute is equivalent to GNU's. */
24703 if (is_attribute_p ("noreturn", attr_id))
24704 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24705 /* C++14 deprecated attribute is equivalent to GNU's. */
24706 else if (is_attribute_p ("deprecated", attr_id))
24708 if (cxx_dialect == cxx11)
24709 pedwarn (token->location, OPT_Wpedantic,
24710 "%<deprecated%> is a C++14 feature;"
24711 " use %<gnu::deprecated%>");
24712 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24714 /* C++17 fallthrough attribute is equivalent to GNU's. */
24715 else if (is_attribute_p ("fallthrough", attr_id))
24717 if (cxx_dialect < cxx1z)
24718 pedwarn (token->location, OPT_Wpedantic,
24719 "%<fallthrough%> is a C++17 feature;"
24720 " use %<gnu::fallthrough%>");
24721 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24723 /* Transactional Memory TS optimize_for_synchronized attribute is
24724 equivalent to GNU transaction_callable. */
24725 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
24726 TREE_PURPOSE (attribute)
24727 = get_identifier ("transaction_callable");
24728 /* Transactional Memory attributes are GNU attributes. */
24729 else if (tm_attr_to_mask (attr_id))
24730 TREE_PURPOSE (attribute) = attr_id;
24733 /* Now parse the optional argument clause of the attribute. */
24735 if (token->type != CPP_OPEN_PAREN)
24736 return attribute;
24739 vec<tree, va_gc> *vec;
24740 int attr_flag = normal_attr;
24742 if (attr_ns == get_identifier ("gnu")
24743 && attribute_takes_identifier_p (attr_id))
24744 /* A GNU attribute that takes an identifier in parameter. */
24745 attr_flag = id_attr;
24747 vec = cp_parser_parenthesized_expression_list
24748 (parser, attr_flag, /*cast_p=*/false,
24749 /*allow_expansion_p=*/true,
24750 /*non_constant_p=*/NULL);
24751 if (vec == NULL)
24752 arguments = error_mark_node;
24753 else
24755 arguments = build_tree_list_vec (vec);
24756 release_tree_vector (vec);
24759 if (arguments == error_mark_node)
24760 attribute = error_mark_node;
24761 else
24762 TREE_VALUE (attribute) = arguments;
24765 return attribute;
24768 /* Check that the attribute ATTRIBUTE appears at most once in the
24769 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
24770 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
24771 isn't implemented yet in GCC. */
24773 static void
24774 cp_parser_check_std_attribute (tree attributes, tree attribute)
24776 if (attributes)
24778 tree name = get_attribute_name (attribute);
24779 if (is_attribute_p ("noreturn", name)
24780 && lookup_attribute ("noreturn", attributes))
24781 error ("attribute %<noreturn%> can appear at most once "
24782 "in an attribute-list");
24783 else if (is_attribute_p ("deprecated", name)
24784 && lookup_attribute ("deprecated", attributes))
24785 error ("attribute %<deprecated%> can appear at most once "
24786 "in an attribute-list");
24790 /* Parse a list of standard C++-11 attributes.
24792 attribute-list:
24793 attribute [opt]
24794 attribute-list , attribute[opt]
24795 attribute ...
24796 attribute-list , attribute ...
24799 static tree
24800 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
24802 tree attributes = NULL_TREE, attribute = NULL_TREE;
24803 cp_token *token = NULL;
24805 while (true)
24807 attribute = cp_parser_std_attribute (parser, attr_ns);
24808 if (attribute == error_mark_node)
24809 break;
24810 if (attribute != NULL_TREE)
24812 cp_parser_check_std_attribute (attributes, attribute);
24813 TREE_CHAIN (attribute) = attributes;
24814 attributes = attribute;
24816 token = cp_lexer_peek_token (parser->lexer);
24817 if (token->type == CPP_ELLIPSIS)
24819 cp_lexer_consume_token (parser->lexer);
24820 if (attribute == NULL_TREE)
24821 error_at (token->location,
24822 "expected attribute before %<...%>");
24823 else
24824 TREE_VALUE (attribute)
24825 = make_pack_expansion (TREE_VALUE (attribute));
24826 token = cp_lexer_peek_token (parser->lexer);
24828 if (token->type != CPP_COMMA)
24829 break;
24830 cp_lexer_consume_token (parser->lexer);
24832 attributes = nreverse (attributes);
24833 return attributes;
24836 /* Parse a standard C++-11 attribute specifier.
24838 attribute-specifier:
24839 [ [ attribute-using-prefix [opt] attribute-list ] ]
24840 alignment-specifier
24842 attribute-using-prefix:
24843 using attribute-namespace :
24845 alignment-specifier:
24846 alignas ( type-id ... [opt] )
24847 alignas ( alignment-expression ... [opt] ). */
24849 static tree
24850 cp_parser_std_attribute_spec (cp_parser *parser)
24852 tree attributes = NULL_TREE;
24853 cp_token *token = cp_lexer_peek_token (parser->lexer);
24855 if (token->type == CPP_OPEN_SQUARE
24856 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
24858 tree attr_ns = NULL_TREE;
24860 cp_lexer_consume_token (parser->lexer);
24861 cp_lexer_consume_token (parser->lexer);
24863 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
24865 token = cp_lexer_peek_nth_token (parser->lexer, 2);
24866 if (token->type == CPP_NAME)
24867 attr_ns = token->u.value;
24868 else if (token->type == CPP_KEYWORD)
24869 attr_ns = ridpointers[(int) token->keyword];
24870 else if (token->flags & NAMED_OP)
24871 attr_ns = get_identifier (cpp_type2name (token->type,
24872 token->flags));
24873 if (attr_ns
24874 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
24876 if (cxx_dialect < cxx1z
24877 && !in_system_header_at (input_location))
24878 pedwarn (input_location, 0,
24879 "attribute using prefix only available "
24880 "with -std=c++1z or -std=gnu++1z");
24882 cp_lexer_consume_token (parser->lexer);
24883 cp_lexer_consume_token (parser->lexer);
24884 cp_lexer_consume_token (parser->lexer);
24886 else
24887 attr_ns = NULL_TREE;
24890 attributes = cp_parser_std_attribute_list (parser, attr_ns);
24892 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
24893 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
24894 cp_parser_skip_to_end_of_statement (parser);
24895 else
24896 /* Warn about parsing c++11 attribute in non-c++1 mode, only
24897 when we are sure that we have actually parsed them. */
24898 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24900 else
24902 tree alignas_expr;
24904 /* Look for an alignment-specifier. */
24906 token = cp_lexer_peek_token (parser->lexer);
24908 if (token->type != CPP_KEYWORD
24909 || token->keyword != RID_ALIGNAS)
24910 return NULL_TREE;
24912 cp_lexer_consume_token (parser->lexer);
24913 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24915 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
24917 cp_parser_error (parser, "expected %<(%>");
24918 return error_mark_node;
24921 cp_parser_parse_tentatively (parser);
24922 alignas_expr = cp_parser_type_id (parser);
24924 if (!cp_parser_parse_definitely (parser))
24926 alignas_expr = cp_parser_assignment_expression (parser);
24927 if (alignas_expr == error_mark_node)
24928 cp_parser_skip_to_end_of_statement (parser);
24929 if (alignas_expr == NULL_TREE
24930 || alignas_expr == error_mark_node)
24931 return alignas_expr;
24934 alignas_expr = cxx_alignas_expr (alignas_expr);
24935 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
24937 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24939 cp_lexer_consume_token (parser->lexer);
24940 alignas_expr = make_pack_expansion (alignas_expr);
24943 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
24945 cp_parser_error (parser, "expected %<)%>");
24946 return error_mark_node;
24949 /* Build the C++-11 representation of an 'aligned'
24950 attribute. */
24951 attributes =
24952 build_tree_list (build_tree_list (get_identifier ("gnu"),
24953 get_identifier ("aligned")),
24954 alignas_expr);
24957 return attributes;
24960 /* Parse a standard C++-11 attribute-specifier-seq.
24962 attribute-specifier-seq:
24963 attribute-specifier-seq [opt] attribute-specifier
24966 static tree
24967 cp_parser_std_attribute_spec_seq (cp_parser *parser)
24969 tree attr_specs = NULL_TREE;
24970 tree attr_last = NULL_TREE;
24972 while (true)
24974 tree attr_spec = cp_parser_std_attribute_spec (parser);
24975 if (attr_spec == NULL_TREE)
24976 break;
24977 if (attr_spec == error_mark_node)
24978 return error_mark_node;
24980 if (attr_last)
24981 TREE_CHAIN (attr_last) = attr_spec;
24982 else
24983 attr_specs = attr_last = attr_spec;
24984 attr_last = tree_last (attr_last);
24987 return attr_specs;
24990 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
24991 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
24992 current value of the PEDANTIC flag, regardless of whether or not
24993 the `__extension__' keyword is present. The caller is responsible
24994 for restoring the value of the PEDANTIC flag. */
24996 static bool
24997 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
24999 /* Save the old value of the PEDANTIC flag. */
25000 *saved_pedantic = pedantic;
25002 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25004 /* Consume the `__extension__' token. */
25005 cp_lexer_consume_token (parser->lexer);
25006 /* We're not being pedantic while the `__extension__' keyword is
25007 in effect. */
25008 pedantic = 0;
25010 return true;
25013 return false;
25016 /* Parse a label declaration.
25018 label-declaration:
25019 __label__ label-declarator-seq ;
25021 label-declarator-seq:
25022 identifier , label-declarator-seq
25023 identifier */
25025 static void
25026 cp_parser_label_declaration (cp_parser* parser)
25028 /* Look for the `__label__' keyword. */
25029 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25031 while (true)
25033 tree identifier;
25035 /* Look for an identifier. */
25036 identifier = cp_parser_identifier (parser);
25037 /* If we failed, stop. */
25038 if (identifier == error_mark_node)
25039 break;
25040 /* Declare it as a label. */
25041 finish_label_decl (identifier);
25042 /* If the next token is a `;', stop. */
25043 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25044 break;
25045 /* Look for the `,' separating the label declarations. */
25046 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25049 /* Look for the final `;'. */
25050 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25053 // -------------------------------------------------------------------------- //
25054 // Requires Clause
25056 // Parse a requires clause.
25058 // requires-clause:
25059 // 'requires' logical-or-expression
25061 // The required logical-or-expression must be a constant expression. Note
25062 // that we don't check that the expression is constepxr here. We defer until
25063 // we analyze constraints and then, we only check atomic constraints.
25064 static tree
25065 cp_parser_requires_clause (cp_parser *parser)
25067 // Parse the requires clause so that it is not automatically folded.
25068 ++processing_template_decl;
25069 tree expr = cp_parser_binary_expression (parser, false, false,
25070 PREC_NOT_OPERATOR, NULL);
25071 if (check_for_bare_parameter_packs (expr))
25072 expr = error_mark_node;
25073 --processing_template_decl;
25074 return expr;
25077 // Optionally parse a requires clause:
25078 static tree
25079 cp_parser_requires_clause_opt (cp_parser *parser)
25081 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25082 if (tok->keyword != RID_REQUIRES)
25084 if (!flag_concepts && tok->type == CPP_NAME
25085 && tok->u.value == ridpointers[RID_REQUIRES])
25087 error_at (cp_lexer_peek_token (parser->lexer)->location,
25088 "%<requires%> only available with -fconcepts");
25089 /* Parse and discard the requires-clause. */
25090 cp_lexer_consume_token (parser->lexer);
25091 cp_parser_requires_clause (parser);
25093 return NULL_TREE;
25095 cp_lexer_consume_token (parser->lexer);
25096 return cp_parser_requires_clause (parser);
25100 /*---------------------------------------------------------------------------
25101 Requires expressions
25102 ---------------------------------------------------------------------------*/
25104 /* Parse a requires expression
25106 requirement-expression:
25107 'requires' requirement-parameter-list [opt] requirement-body */
25108 static tree
25109 cp_parser_requires_expression (cp_parser *parser)
25111 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25112 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25114 /* A requires-expression shall appear only within a concept
25115 definition or a requires-clause.
25117 TODO: Implement this diagnostic correctly. */
25118 if (!processing_template_decl)
25120 error_at (loc, "a requires expression cannot appear outside a template");
25121 cp_parser_skip_to_end_of_statement (parser);
25122 return error_mark_node;
25125 tree parms, reqs;
25127 /* Local parameters are delared as variables within the scope
25128 of the expression. They are not visible past the end of
25129 the expression. Expressions within the requires-expression
25130 are unevaluated. */
25131 struct scope_sentinel
25133 scope_sentinel ()
25135 ++cp_unevaluated_operand;
25136 begin_scope (sk_block, NULL_TREE);
25139 ~scope_sentinel ()
25141 pop_bindings_and_leave_scope ();
25142 --cp_unevaluated_operand;
25144 } s;
25146 /* Parse the optional parameter list. */
25147 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25149 parms = cp_parser_requirement_parameter_list (parser);
25150 if (parms == error_mark_node)
25151 return error_mark_node;
25153 else
25154 parms = NULL_TREE;
25156 /* Parse the requirement body. */
25157 reqs = cp_parser_requirement_body (parser);
25158 if (reqs == error_mark_node)
25159 return error_mark_node;
25162 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25163 the parm chain. */
25164 grokparms (parms, &parms);
25165 return finish_requires_expr (parms, reqs);
25168 /* Parse a parameterized requirement.
25170 requirement-parameter-list:
25171 '(' parameter-declaration-clause ')' */
25172 static tree
25173 cp_parser_requirement_parameter_list (cp_parser *parser)
25175 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25176 return error_mark_node;
25178 tree parms = cp_parser_parameter_declaration_clause (parser);
25180 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25181 return error_mark_node;
25183 return parms;
25186 /* Parse the body of a requirement.
25188 requirement-body:
25189 '{' requirement-list '}' */
25190 static tree
25191 cp_parser_requirement_body (cp_parser *parser)
25193 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
25194 return error_mark_node;
25196 tree reqs = cp_parser_requirement_list (parser);
25198 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25199 return error_mark_node;
25201 return reqs;
25204 /* Parse a list of requirements.
25206 requirement-list:
25207 requirement
25208 requirement-list ';' requirement[opt] */
25209 static tree
25210 cp_parser_requirement_list (cp_parser *parser)
25212 tree result = NULL_TREE;
25213 while (true)
25215 tree req = cp_parser_requirement (parser);
25216 if (req == error_mark_node)
25217 return error_mark_node;
25219 result = tree_cons (NULL_TREE, req, result);
25221 /* If we see a semi-colon, consume it. */
25222 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25223 cp_lexer_consume_token (parser->lexer);
25225 /* Stop processing at the end of the list. */
25226 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25227 break;
25230 /* Reverse the order of requirements so they are analyzed in
25231 declaration order. */
25232 return nreverse (result);
25235 /* Parse a syntactic requirement or type requirement.
25237 requirement:
25238 simple-requirement
25239 compound-requirement
25240 type-requirement
25241 nested-requirement */
25242 static tree
25243 cp_parser_requirement (cp_parser *parser)
25245 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25246 return cp_parser_compound_requirement (parser);
25247 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25248 return cp_parser_type_requirement (parser);
25249 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25250 return cp_parser_nested_requirement (parser);
25251 else
25252 return cp_parser_simple_requirement (parser);
25255 /* Parse a simple requirement.
25257 simple-requirement:
25258 expression ';' */
25259 static tree
25260 cp_parser_simple_requirement (cp_parser *parser)
25262 tree expr = cp_parser_expression (parser, NULL, false, false);
25263 if (!expr || expr == error_mark_node)
25264 return error_mark_node;
25266 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25267 return error_mark_node;
25269 return finish_simple_requirement (expr);
25272 /* Parse a type requirement
25274 type-requirement
25275 nested-name-specifier [opt] required-type-name ';'
25277 required-type-name:
25278 type-name
25279 'template' [opt] simple-template-id */
25280 static tree
25281 cp_parser_type_requirement (cp_parser *parser)
25283 cp_lexer_consume_token (parser->lexer);
25285 // Save the scope before parsing name specifiers.
25286 tree saved_scope = parser->scope;
25287 tree saved_object_scope = parser->object_scope;
25288 tree saved_qualifying_scope = parser->qualifying_scope;
25289 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25290 cp_parser_nested_name_specifier_opt (parser,
25291 /*typename_keyword_p=*/true,
25292 /*check_dependency_p=*/false,
25293 /*type_p=*/true,
25294 /*is_declaration=*/false);
25296 tree type;
25297 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25299 cp_lexer_consume_token (parser->lexer);
25300 type = cp_parser_template_id (parser,
25301 /*template_keyword_p=*/true,
25302 /*check_dependency=*/false,
25303 /*tag_type=*/none_type,
25304 /*is_declaration=*/false);
25305 type = make_typename_type (parser->scope, type, typename_type,
25306 /*complain=*/tf_error);
25308 else
25309 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
25311 if (TREE_CODE (type) == TYPE_DECL)
25312 type = TREE_TYPE (type);
25314 parser->scope = saved_scope;
25315 parser->object_scope = saved_object_scope;
25316 parser->qualifying_scope = saved_qualifying_scope;
25318 if (type == error_mark_node)
25319 cp_parser_skip_to_end_of_statement (parser);
25321 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25322 return error_mark_node;
25323 if (type == error_mark_node)
25324 return error_mark_node;
25326 return finish_type_requirement (type);
25329 /* Parse a compound requirement
25331 compound-requirement:
25332 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25333 static tree
25334 cp_parser_compound_requirement (cp_parser *parser)
25336 /* Parse an expression enclosed in '{ }'s. */
25337 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
25338 return error_mark_node;
25340 tree expr = cp_parser_expression (parser, NULL, false, false);
25341 if (!expr || expr == error_mark_node)
25342 return error_mark_node;
25344 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25345 return error_mark_node;
25347 /* Parse the optional noexcept. */
25348 bool noexcept_p = false;
25349 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
25351 cp_lexer_consume_token (parser->lexer);
25352 noexcept_p = true;
25355 /* Parse the optional trailing return type. */
25356 tree type = NULL_TREE;
25357 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
25359 cp_lexer_consume_token (parser->lexer);
25360 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
25361 parser->in_result_type_constraint_p = true;
25362 type = cp_parser_trailing_type_id (parser);
25363 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
25364 if (type == error_mark_node)
25365 return error_mark_node;
25368 return finish_compound_requirement (expr, type, noexcept_p);
25371 /* Parse a nested requirement. This is the same as a requires clause.
25373 nested-requirement:
25374 requires-clause */
25375 static tree
25376 cp_parser_nested_requirement (cp_parser *parser)
25378 cp_lexer_consume_token (parser->lexer);
25379 tree req = cp_parser_requires_clause (parser);
25380 if (req == error_mark_node)
25381 return error_mark_node;
25382 return finish_nested_requirement (req);
25385 /* Support Functions */
25387 /* Return the appropriate prefer_type argument for lookup_name_real based on
25388 tag_type and template_mem_access. */
25390 static inline int
25391 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
25393 /* DR 141: When looking in the current enclosing context for a template-name
25394 after -> or ., only consider class templates. */
25395 if (template_mem_access)
25396 return 2;
25397 switch (tag_type)
25399 case none_type: return 0; // No preference.
25400 case scope_type: return 1; // Type or namespace.
25401 default: return 2; // Type only.
25405 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25406 NAME should have one of the representations used for an
25407 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25408 is returned. If PARSER->SCOPE is a dependent type, then a
25409 SCOPE_REF is returned.
25411 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
25412 returned; the name was already resolved when the TEMPLATE_ID_EXPR
25413 was formed. Abstractly, such entities should not be passed to this
25414 function, because they do not need to be looked up, but it is
25415 simpler to check for this special case here, rather than at the
25416 call-sites.
25418 In cases not explicitly covered above, this function returns a
25419 DECL, OVERLOAD, or baselink representing the result of the lookup.
25420 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
25421 is returned.
25423 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
25424 (e.g., "struct") that was used. In that case bindings that do not
25425 refer to types are ignored.
25427 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
25428 ignored.
25430 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
25431 are ignored.
25433 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
25434 types.
25436 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
25437 TREE_LIST of candidates if name-lookup results in an ambiguity, and
25438 NULL_TREE otherwise. */
25440 static cp_expr
25441 cp_parser_lookup_name (cp_parser *parser, tree name,
25442 enum tag_types tag_type,
25443 bool is_template,
25444 bool is_namespace,
25445 bool check_dependency,
25446 tree *ambiguous_decls,
25447 location_t name_location)
25449 tree decl;
25450 tree object_type = parser->context->object_type;
25452 /* Assume that the lookup will be unambiguous. */
25453 if (ambiguous_decls)
25454 *ambiguous_decls = NULL_TREE;
25456 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
25457 no longer valid. Note that if we are parsing tentatively, and
25458 the parse fails, OBJECT_TYPE will be automatically restored. */
25459 parser->context->object_type = NULL_TREE;
25461 if (name == error_mark_node)
25462 return error_mark_node;
25464 /* A template-id has already been resolved; there is no lookup to
25465 do. */
25466 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
25467 return name;
25468 if (BASELINK_P (name))
25470 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
25471 == TEMPLATE_ID_EXPR);
25472 return name;
25475 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
25476 it should already have been checked to make sure that the name
25477 used matches the type being destroyed. */
25478 if (TREE_CODE (name) == BIT_NOT_EXPR)
25480 tree type;
25482 /* Figure out to which type this destructor applies. */
25483 if (parser->scope)
25484 type = parser->scope;
25485 else if (object_type)
25486 type = object_type;
25487 else
25488 type = current_class_type;
25489 /* If that's not a class type, there is no destructor. */
25490 if (!type || !CLASS_TYPE_P (type))
25491 return error_mark_node;
25492 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
25493 lazily_declare_fn (sfk_destructor, type);
25494 if (!CLASSTYPE_DESTRUCTORS (type))
25495 return error_mark_node;
25496 /* If it was a class type, return the destructor. */
25497 return CLASSTYPE_DESTRUCTORS (type);
25500 /* By this point, the NAME should be an ordinary identifier. If
25501 the id-expression was a qualified name, the qualifying scope is
25502 stored in PARSER->SCOPE at this point. */
25503 gcc_assert (identifier_p (name));
25505 /* Perform the lookup. */
25506 if (parser->scope)
25508 bool dependent_p;
25510 if (parser->scope == error_mark_node)
25511 return error_mark_node;
25513 /* If the SCOPE is dependent, the lookup must be deferred until
25514 the template is instantiated -- unless we are explicitly
25515 looking up names in uninstantiated templates. Even then, we
25516 cannot look up the name if the scope is not a class type; it
25517 might, for example, be a template type parameter. */
25518 dependent_p = (TYPE_P (parser->scope)
25519 && dependent_scope_p (parser->scope));
25520 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
25521 && dependent_p)
25522 /* Defer lookup. */
25523 decl = error_mark_node;
25524 else
25526 tree pushed_scope = NULL_TREE;
25528 /* If PARSER->SCOPE is a dependent type, then it must be a
25529 class type, and we must not be checking dependencies;
25530 otherwise, we would have processed this lookup above. So
25531 that PARSER->SCOPE is not considered a dependent base by
25532 lookup_member, we must enter the scope here. */
25533 if (dependent_p)
25534 pushed_scope = push_scope (parser->scope);
25536 /* If the PARSER->SCOPE is a template specialization, it
25537 may be instantiated during name lookup. In that case,
25538 errors may be issued. Even if we rollback the current
25539 tentative parse, those errors are valid. */
25540 decl = lookup_qualified_name (parser->scope, name,
25541 prefer_type_arg (tag_type),
25542 /*complain=*/true);
25544 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
25545 lookup result and the nested-name-specifier nominates a class C:
25546 * if the name specified after the nested-name-specifier, when
25547 looked up in C, is the injected-class-name of C (Clause 9), or
25548 * if the name specified after the nested-name-specifier is the
25549 same as the identifier or the simple-template-id's template-
25550 name in the last component of the nested-name-specifier,
25551 the name is instead considered to name the constructor of
25552 class C. [ Note: for example, the constructor is not an
25553 acceptable lookup result in an elaborated-type-specifier so
25554 the constructor would not be used in place of the
25555 injected-class-name. --end note ] Such a constructor name
25556 shall be used only in the declarator-id of a declaration that
25557 names a constructor or in a using-declaration. */
25558 if (tag_type == none_type
25559 && DECL_SELF_REFERENCE_P (decl)
25560 && same_type_p (DECL_CONTEXT (decl), parser->scope))
25561 decl = lookup_qualified_name (parser->scope, ctor_identifier,
25562 prefer_type_arg (tag_type),
25563 /*complain=*/true);
25565 /* If we have a single function from a using decl, pull it out. */
25566 if (TREE_CODE (decl) == OVERLOAD
25567 && !really_overloaded_fn (decl))
25568 decl = OVL_FUNCTION (decl);
25570 if (pushed_scope)
25571 pop_scope (pushed_scope);
25574 /* If the scope is a dependent type and either we deferred lookup or
25575 we did lookup but didn't find the name, rememeber the name. */
25576 if (decl == error_mark_node && TYPE_P (parser->scope)
25577 && dependent_type_p (parser->scope))
25579 if (tag_type)
25581 tree type;
25583 /* The resolution to Core Issue 180 says that `struct
25584 A::B' should be considered a type-name, even if `A'
25585 is dependent. */
25586 type = make_typename_type (parser->scope, name, tag_type,
25587 /*complain=*/tf_error);
25588 if (type != error_mark_node)
25589 decl = TYPE_NAME (type);
25591 else if (is_template
25592 && (cp_parser_next_token_ends_template_argument_p (parser)
25593 || cp_lexer_next_token_is (parser->lexer,
25594 CPP_CLOSE_PAREN)))
25595 decl = make_unbound_class_template (parser->scope,
25596 name, NULL_TREE,
25597 /*complain=*/tf_error);
25598 else
25599 decl = build_qualified_name (/*type=*/NULL_TREE,
25600 parser->scope, name,
25601 is_template);
25603 parser->qualifying_scope = parser->scope;
25604 parser->object_scope = NULL_TREE;
25606 else if (object_type)
25608 /* Look up the name in the scope of the OBJECT_TYPE, unless the
25609 OBJECT_TYPE is not a class. */
25610 if (CLASS_TYPE_P (object_type))
25611 /* If the OBJECT_TYPE is a template specialization, it may
25612 be instantiated during name lookup. In that case, errors
25613 may be issued. Even if we rollback the current tentative
25614 parse, those errors are valid. */
25615 decl = lookup_member (object_type,
25616 name,
25617 /*protect=*/0,
25618 prefer_type_arg (tag_type),
25619 tf_warning_or_error);
25620 else
25621 decl = NULL_TREE;
25623 if (!decl)
25624 /* Look it up in the enclosing context. DR 141: When looking for a
25625 template-name after -> or ., only consider class templates. */
25626 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
25627 /*nonclass=*/0,
25628 /*block_p=*/true, is_namespace, 0);
25629 if (object_type == unknown_type_node)
25630 /* The object is type-dependent, so we can't look anything up; we used
25631 this to get the DR 141 behavior. */
25632 object_type = NULL_TREE;
25633 parser->object_scope = object_type;
25634 parser->qualifying_scope = NULL_TREE;
25636 else
25638 decl = lookup_name_real (name, prefer_type_arg (tag_type),
25639 /*nonclass=*/0,
25640 /*block_p=*/true, is_namespace, 0);
25641 parser->qualifying_scope = NULL_TREE;
25642 parser->object_scope = NULL_TREE;
25645 /* If the lookup failed, let our caller know. */
25646 if (!decl || decl == error_mark_node)
25647 return error_mark_node;
25649 /* Pull out the template from an injected-class-name (or multiple). */
25650 if (is_template)
25651 decl = maybe_get_template_decl_from_type_decl (decl);
25653 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
25654 if (TREE_CODE (decl) == TREE_LIST)
25656 if (ambiguous_decls)
25657 *ambiguous_decls = decl;
25658 /* The error message we have to print is too complicated for
25659 cp_parser_error, so we incorporate its actions directly. */
25660 if (!cp_parser_simulate_error (parser))
25662 error_at (name_location, "reference to %qD is ambiguous",
25663 name);
25664 print_candidates (decl);
25666 return error_mark_node;
25669 gcc_assert (DECL_P (decl)
25670 || TREE_CODE (decl) == OVERLOAD
25671 || TREE_CODE (decl) == SCOPE_REF
25672 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
25673 || BASELINK_P (decl));
25675 /* If we have resolved the name of a member declaration, check to
25676 see if the declaration is accessible. When the name resolves to
25677 set of overloaded functions, accessibility is checked when
25678 overload resolution is done.
25680 During an explicit instantiation, access is not checked at all,
25681 as per [temp.explicit]. */
25682 if (DECL_P (decl))
25683 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
25685 maybe_record_typedef_use (decl);
25687 return cp_expr (decl, name_location);
25690 /* Like cp_parser_lookup_name, but for use in the typical case where
25691 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
25692 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
25694 static tree
25695 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
25697 return cp_parser_lookup_name (parser, name,
25698 none_type,
25699 /*is_template=*/false,
25700 /*is_namespace=*/false,
25701 /*check_dependency=*/true,
25702 /*ambiguous_decls=*/NULL,
25703 location);
25706 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
25707 the current context, return the TYPE_DECL. If TAG_NAME_P is
25708 true, the DECL indicates the class being defined in a class-head,
25709 or declared in an elaborated-type-specifier.
25711 Otherwise, return DECL. */
25713 static tree
25714 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
25716 /* If the TEMPLATE_DECL is being declared as part of a class-head,
25717 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
25719 struct A {
25720 template <typename T> struct B;
25723 template <typename T> struct A::B {};
25725 Similarly, in an elaborated-type-specifier:
25727 namespace N { struct X{}; }
25729 struct A {
25730 template <typename T> friend struct N::X;
25733 However, if the DECL refers to a class type, and we are in
25734 the scope of the class, then the name lookup automatically
25735 finds the TYPE_DECL created by build_self_reference rather
25736 than a TEMPLATE_DECL. For example, in:
25738 template <class T> struct S {
25739 S s;
25742 there is no need to handle such case. */
25744 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
25745 return DECL_TEMPLATE_RESULT (decl);
25747 return decl;
25750 /* If too many, or too few, template-parameter lists apply to the
25751 declarator, issue an error message. Returns TRUE if all went well,
25752 and FALSE otherwise. */
25754 static bool
25755 cp_parser_check_declarator_template_parameters (cp_parser* parser,
25756 cp_declarator *declarator,
25757 location_t declarator_location)
25759 switch (declarator->kind)
25761 case cdk_id:
25763 unsigned num_templates = 0;
25764 tree scope = declarator->u.id.qualifying_scope;
25766 if (scope)
25767 num_templates = num_template_headers_for_class (scope);
25768 else if (TREE_CODE (declarator->u.id.unqualified_name)
25769 == TEMPLATE_ID_EXPR)
25770 /* If the DECLARATOR has the form `X<y>' then it uses one
25771 additional level of template parameters. */
25772 ++num_templates;
25774 return cp_parser_check_template_parameters
25775 (parser, num_templates, declarator_location, declarator);
25778 case cdk_function:
25779 case cdk_array:
25780 case cdk_pointer:
25781 case cdk_reference:
25782 case cdk_ptrmem:
25783 return (cp_parser_check_declarator_template_parameters
25784 (parser, declarator->declarator, declarator_location));
25786 case cdk_decomp:
25787 case cdk_error:
25788 return true;
25790 default:
25791 gcc_unreachable ();
25793 return false;
25796 /* NUM_TEMPLATES were used in the current declaration. If that is
25797 invalid, return FALSE and issue an error messages. Otherwise,
25798 return TRUE. If DECLARATOR is non-NULL, then we are checking a
25799 declarator and we can print more accurate diagnostics. */
25801 static bool
25802 cp_parser_check_template_parameters (cp_parser* parser,
25803 unsigned num_templates,
25804 location_t location,
25805 cp_declarator *declarator)
25807 /* If there are the same number of template classes and parameter
25808 lists, that's OK. */
25809 if (parser->num_template_parameter_lists == num_templates)
25810 return true;
25811 /* If there are more, but only one more, then we are referring to a
25812 member template. That's OK too. */
25813 if (parser->num_template_parameter_lists == num_templates + 1)
25814 return true;
25815 /* If there are more template classes than parameter lists, we have
25816 something like:
25818 template <class T> void S<T>::R<T>::f (); */
25819 if (parser->num_template_parameter_lists < num_templates)
25821 if (declarator && !current_function_decl)
25822 error_at (location, "specializing member %<%T::%E%> "
25823 "requires %<template<>%> syntax",
25824 declarator->u.id.qualifying_scope,
25825 declarator->u.id.unqualified_name);
25826 else if (declarator)
25827 error_at (location, "invalid declaration of %<%T::%E%>",
25828 declarator->u.id.qualifying_scope,
25829 declarator->u.id.unqualified_name);
25830 else
25831 error_at (location, "too few template-parameter-lists");
25832 return false;
25834 /* Otherwise, there are too many template parameter lists. We have
25835 something like:
25837 template <class T> template <class U> void S::f(); */
25838 error_at (location, "too many template-parameter-lists");
25839 return false;
25842 /* Parse an optional `::' token indicating that the following name is
25843 from the global namespace. If so, PARSER->SCOPE is set to the
25844 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
25845 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
25846 Returns the new value of PARSER->SCOPE, if the `::' token is
25847 present, and NULL_TREE otherwise. */
25849 static tree
25850 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
25852 cp_token *token;
25854 /* Peek at the next token. */
25855 token = cp_lexer_peek_token (parser->lexer);
25856 /* If we're looking at a `::' token then we're starting from the
25857 global namespace, not our current location. */
25858 if (token->type == CPP_SCOPE)
25860 /* Consume the `::' token. */
25861 cp_lexer_consume_token (parser->lexer);
25862 /* Set the SCOPE so that we know where to start the lookup. */
25863 parser->scope = global_namespace;
25864 parser->qualifying_scope = global_namespace;
25865 parser->object_scope = NULL_TREE;
25867 return parser->scope;
25869 else if (!current_scope_valid_p)
25871 parser->scope = NULL_TREE;
25872 parser->qualifying_scope = NULL_TREE;
25873 parser->object_scope = NULL_TREE;
25876 return NULL_TREE;
25879 /* Returns TRUE if the upcoming token sequence is the start of a
25880 constructor declarator. If FRIEND_P is true, the declarator is
25881 preceded by the `friend' specifier. */
25883 static bool
25884 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
25886 bool constructor_p;
25887 bool outside_class_specifier_p;
25888 tree nested_name_specifier;
25889 cp_token *next_token;
25891 /* The common case is that this is not a constructor declarator, so
25892 try to avoid doing lots of work if at all possible. It's not
25893 valid declare a constructor at function scope. */
25894 if (parser->in_function_body)
25895 return false;
25896 /* And only certain tokens can begin a constructor declarator. */
25897 next_token = cp_lexer_peek_token (parser->lexer);
25898 if (next_token->type != CPP_NAME
25899 && next_token->type != CPP_SCOPE
25900 && next_token->type != CPP_NESTED_NAME_SPECIFIER
25901 && next_token->type != CPP_TEMPLATE_ID)
25902 return false;
25904 /* Parse tentatively; we are going to roll back all of the tokens
25905 consumed here. */
25906 cp_parser_parse_tentatively (parser);
25907 /* Assume that we are looking at a constructor declarator. */
25908 constructor_p = true;
25910 /* Look for the optional `::' operator. */
25911 cp_parser_global_scope_opt (parser,
25912 /*current_scope_valid_p=*/false);
25913 /* Look for the nested-name-specifier. */
25914 nested_name_specifier
25915 = (cp_parser_nested_name_specifier_opt (parser,
25916 /*typename_keyword_p=*/false,
25917 /*check_dependency_p=*/false,
25918 /*type_p=*/false,
25919 /*is_declaration=*/false));
25921 outside_class_specifier_p = (!at_class_scope_p ()
25922 || !TYPE_BEING_DEFINED (current_class_type)
25923 || friend_p);
25925 /* Outside of a class-specifier, there must be a
25926 nested-name-specifier. */
25927 if (!nested_name_specifier && outside_class_specifier_p)
25928 constructor_p = false;
25929 else if (nested_name_specifier == error_mark_node)
25930 constructor_p = false;
25932 /* If we have a class scope, this is easy; DR 147 says that S::S always
25933 names the constructor, and no other qualified name could. */
25934 if (constructor_p && nested_name_specifier
25935 && CLASS_TYPE_P (nested_name_specifier))
25937 tree id = cp_parser_unqualified_id (parser,
25938 /*template_keyword_p=*/false,
25939 /*check_dependency_p=*/false,
25940 /*declarator_p=*/true,
25941 /*optional_p=*/false);
25942 if (is_overloaded_fn (id))
25943 id = DECL_NAME (get_first_fn (id));
25944 if (!constructor_name_p (id, nested_name_specifier))
25945 constructor_p = false;
25947 /* If we still think that this might be a constructor-declarator,
25948 look for a class-name. */
25949 else if (constructor_p)
25951 /* If we have:
25953 template <typename T> struct S {
25954 S();
25957 we must recognize that the nested `S' names a class. */
25958 tree type_decl;
25959 type_decl = cp_parser_class_name (parser,
25960 /*typename_keyword_p=*/false,
25961 /*template_keyword_p=*/false,
25962 none_type,
25963 /*check_dependency_p=*/false,
25964 /*class_head_p=*/false,
25965 /*is_declaration=*/false);
25966 /* If there was no class-name, then this is not a constructor.
25967 Otherwise, if we are in a class-specifier and we aren't
25968 handling a friend declaration, check that its type matches
25969 current_class_type (c++/38313). Note: error_mark_node
25970 is left alone for error recovery purposes. */
25971 constructor_p = (!cp_parser_error_occurred (parser)
25972 && (outside_class_specifier_p
25973 || type_decl == error_mark_node
25974 || same_type_p (current_class_type,
25975 TREE_TYPE (type_decl))));
25977 /* If we're still considering a constructor, we have to see a `(',
25978 to begin the parameter-declaration-clause, followed by either a
25979 `)', an `...', or a decl-specifier. We need to check for a
25980 type-specifier to avoid being fooled into thinking that:
25982 S (f) (int);
25984 is a constructor. (It is actually a function named `f' that
25985 takes one parameter (of type `int') and returns a value of type
25986 `S'. */
25987 if (constructor_p
25988 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25989 constructor_p = false;
25991 if (constructor_p
25992 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
25993 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
25994 /* A parameter declaration begins with a decl-specifier,
25995 which is either the "attribute" keyword, a storage class
25996 specifier, or (usually) a type-specifier. */
25997 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
25999 tree type;
26000 tree pushed_scope = NULL_TREE;
26001 unsigned saved_num_template_parameter_lists;
26003 /* Names appearing in the type-specifier should be looked up
26004 in the scope of the class. */
26005 if (current_class_type)
26006 type = NULL_TREE;
26007 else
26009 type = TREE_TYPE (type_decl);
26010 if (TREE_CODE (type) == TYPENAME_TYPE)
26012 type = resolve_typename_type (type,
26013 /*only_current_p=*/false);
26014 if (TREE_CODE (type) == TYPENAME_TYPE)
26016 cp_parser_abort_tentative_parse (parser);
26017 return false;
26020 pushed_scope = push_scope (type);
26023 /* Inside the constructor parameter list, surrounding
26024 template-parameter-lists do not apply. */
26025 saved_num_template_parameter_lists
26026 = parser->num_template_parameter_lists;
26027 parser->num_template_parameter_lists = 0;
26029 /* Look for the type-specifier. */
26030 cp_parser_type_specifier (parser,
26031 CP_PARSER_FLAGS_NONE,
26032 /*decl_specs=*/NULL,
26033 /*is_declarator=*/true,
26034 /*declares_class_or_enum=*/NULL,
26035 /*is_cv_qualifier=*/NULL);
26037 parser->num_template_parameter_lists
26038 = saved_num_template_parameter_lists;
26040 /* Leave the scope of the class. */
26041 if (pushed_scope)
26042 pop_scope (pushed_scope);
26044 constructor_p = !cp_parser_error_occurred (parser);
26048 /* We did not really want to consume any tokens. */
26049 cp_parser_abort_tentative_parse (parser);
26051 return constructor_p;
26054 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26055 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26056 they must be performed once we are in the scope of the function.
26058 Returns the function defined. */
26060 static tree
26061 cp_parser_function_definition_from_specifiers_and_declarator
26062 (cp_parser* parser,
26063 cp_decl_specifier_seq *decl_specifiers,
26064 tree attributes,
26065 const cp_declarator *declarator)
26067 tree fn;
26068 bool success_p;
26070 /* Begin the function-definition. */
26071 success_p = start_function (decl_specifiers, declarator, attributes);
26073 /* The things we're about to see are not directly qualified by any
26074 template headers we've seen thus far. */
26075 reset_specialization ();
26077 /* If there were names looked up in the decl-specifier-seq that we
26078 did not check, check them now. We must wait until we are in the
26079 scope of the function to perform the checks, since the function
26080 might be a friend. */
26081 perform_deferred_access_checks (tf_warning_or_error);
26083 if (success_p)
26085 cp_finalize_omp_declare_simd (parser, current_function_decl);
26086 parser->omp_declare_simd = NULL;
26087 cp_finalize_oacc_routine (parser, current_function_decl, true);
26088 parser->oacc_routine = NULL;
26091 if (!success_p)
26093 /* Skip the entire function. */
26094 cp_parser_skip_to_end_of_block_or_statement (parser);
26095 fn = error_mark_node;
26097 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26099 /* Seen already, skip it. An error message has already been output. */
26100 cp_parser_skip_to_end_of_block_or_statement (parser);
26101 fn = current_function_decl;
26102 current_function_decl = NULL_TREE;
26103 /* If this is a function from a class, pop the nested class. */
26104 if (current_class_name)
26105 pop_nested_class ();
26107 else
26109 timevar_id_t tv;
26110 if (DECL_DECLARED_INLINE_P (current_function_decl))
26111 tv = TV_PARSE_INLINE;
26112 else
26113 tv = TV_PARSE_FUNC;
26114 timevar_push (tv);
26115 fn = cp_parser_function_definition_after_declarator (parser,
26116 /*inline_p=*/false);
26117 timevar_pop (tv);
26120 return fn;
26123 /* Parse the part of a function-definition that follows the
26124 declarator. INLINE_P is TRUE iff this function is an inline
26125 function defined within a class-specifier.
26127 Returns the function defined. */
26129 static tree
26130 cp_parser_function_definition_after_declarator (cp_parser* parser,
26131 bool inline_p)
26133 tree fn;
26134 bool ctor_initializer_p = false;
26135 bool saved_in_unbraced_linkage_specification_p;
26136 bool saved_in_function_body;
26137 unsigned saved_num_template_parameter_lists;
26138 cp_token *token;
26139 bool fully_implicit_function_template_p
26140 = parser->fully_implicit_function_template_p;
26141 parser->fully_implicit_function_template_p = false;
26142 tree implicit_template_parms
26143 = parser->implicit_template_parms;
26144 parser->implicit_template_parms = 0;
26145 cp_binding_level* implicit_template_scope
26146 = parser->implicit_template_scope;
26147 parser->implicit_template_scope = 0;
26149 saved_in_function_body = parser->in_function_body;
26150 parser->in_function_body = true;
26151 /* If the next token is `return', then the code may be trying to
26152 make use of the "named return value" extension that G++ used to
26153 support. */
26154 token = cp_lexer_peek_token (parser->lexer);
26155 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26157 /* Consume the `return' keyword. */
26158 cp_lexer_consume_token (parser->lexer);
26159 /* Look for the identifier that indicates what value is to be
26160 returned. */
26161 cp_parser_identifier (parser);
26162 /* Issue an error message. */
26163 error_at (token->location,
26164 "named return values are no longer supported");
26165 /* Skip tokens until we reach the start of the function body. */
26166 while (true)
26168 cp_token *token = cp_lexer_peek_token (parser->lexer);
26169 if (token->type == CPP_OPEN_BRACE
26170 || token->type == CPP_EOF
26171 || token->type == CPP_PRAGMA_EOL)
26172 break;
26173 cp_lexer_consume_token (parser->lexer);
26176 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26177 anything declared inside `f'. */
26178 saved_in_unbraced_linkage_specification_p
26179 = parser->in_unbraced_linkage_specification_p;
26180 parser->in_unbraced_linkage_specification_p = false;
26181 /* Inside the function, surrounding template-parameter-lists do not
26182 apply. */
26183 saved_num_template_parameter_lists
26184 = parser->num_template_parameter_lists;
26185 parser->num_template_parameter_lists = 0;
26187 start_lambda_scope (current_function_decl);
26189 /* If the next token is `try', `__transaction_atomic', or
26190 `__transaction_relaxed`, then we are looking at either function-try-block
26191 or function-transaction-block. Note that all of these include the
26192 function-body. */
26193 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26194 ctor_initializer_p = cp_parser_function_transaction (parser,
26195 RID_TRANSACTION_ATOMIC);
26196 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26197 RID_TRANSACTION_RELAXED))
26198 ctor_initializer_p = cp_parser_function_transaction (parser,
26199 RID_TRANSACTION_RELAXED);
26200 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26201 ctor_initializer_p = cp_parser_function_try_block (parser);
26202 else
26203 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
26204 (parser, /*in_function_try_block=*/false);
26206 finish_lambda_scope ();
26208 /* Finish the function. */
26209 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
26210 (inline_p ? 2 : 0));
26211 /* Generate code for it, if necessary. */
26212 expand_or_defer_fn (fn);
26213 /* Restore the saved values. */
26214 parser->in_unbraced_linkage_specification_p
26215 = saved_in_unbraced_linkage_specification_p;
26216 parser->num_template_parameter_lists
26217 = saved_num_template_parameter_lists;
26218 parser->in_function_body = saved_in_function_body;
26220 parser->fully_implicit_function_template_p
26221 = fully_implicit_function_template_p;
26222 parser->implicit_template_parms
26223 = implicit_template_parms;
26224 parser->implicit_template_scope
26225 = implicit_template_scope;
26227 if (parser->fully_implicit_function_template_p)
26228 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26230 return fn;
26233 /* Parse a template-declaration body (following argument list). */
26235 static void
26236 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26237 tree parameter_list,
26238 bool member_p)
26240 tree decl = NULL_TREE;
26241 bool friend_p = false;
26243 /* We just processed one more parameter list. */
26244 ++parser->num_template_parameter_lists;
26246 /* Get the deferred access checks from the parameter list. These
26247 will be checked once we know what is being declared, as for a
26248 member template the checks must be performed in the scope of the
26249 class containing the member. */
26250 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26252 /* Tentatively parse for a new template parameter list, which can either be
26253 the template keyword or a template introduction. */
26254 if (cp_parser_template_declaration_after_export (parser, member_p))
26255 /* OK */;
26256 else if (cxx_dialect >= cxx11
26257 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26258 decl = cp_parser_alias_declaration (parser);
26259 else
26261 /* There are no access checks when parsing a template, as we do not
26262 know if a specialization will be a friend. */
26263 push_deferring_access_checks (dk_no_check);
26264 cp_token *token = cp_lexer_peek_token (parser->lexer);
26265 decl = cp_parser_single_declaration (parser,
26266 checks,
26267 member_p,
26268 /*explicit_specialization_p=*/false,
26269 &friend_p);
26270 pop_deferring_access_checks ();
26272 /* If this is a member template declaration, let the front
26273 end know. */
26274 if (member_p && !friend_p && decl)
26276 if (TREE_CODE (decl) == TYPE_DECL)
26277 cp_parser_check_access_in_redeclaration (decl, token->location);
26279 decl = finish_member_template_decl (decl);
26281 else if (friend_p && decl
26282 && DECL_DECLARES_TYPE_P (decl))
26283 make_friend_class (current_class_type, TREE_TYPE (decl),
26284 /*complain=*/true);
26286 /* We are done with the current parameter list. */
26287 --parser->num_template_parameter_lists;
26289 pop_deferring_access_checks ();
26291 /* Finish up. */
26292 finish_template_decl (parameter_list);
26294 /* Check the template arguments for a literal operator template. */
26295 if (decl
26296 && DECL_DECLARES_FUNCTION_P (decl)
26297 && UDLIT_OPER_P (DECL_NAME (decl)))
26299 bool ok = true;
26300 if (parameter_list == NULL_TREE)
26301 ok = false;
26302 else
26304 int num_parms = TREE_VEC_LENGTH (parameter_list);
26305 if (num_parms == 1)
26307 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
26308 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26309 if (TREE_TYPE (parm) != char_type_node
26310 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26311 ok = false;
26313 else if (num_parms == 2 && cxx_dialect >= cxx14)
26315 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
26316 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
26317 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
26318 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26319 if (parm == error_mark_node
26320 || TREE_TYPE (parm) != TREE_TYPE (type)
26321 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26322 ok = false;
26324 else
26325 ok = false;
26327 if (!ok)
26329 if (cxx_dialect >= cxx14)
26330 error ("literal operator template %qD has invalid parameter list."
26331 " Expected non-type template argument pack <char...>"
26332 " or <typename CharT, CharT...>",
26333 decl);
26334 else
26335 error ("literal operator template %qD has invalid parameter list."
26336 " Expected non-type template argument pack <char...>",
26337 decl);
26341 /* Register member declarations. */
26342 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
26343 finish_member_declaration (decl);
26344 /* If DECL is a function template, we must return to parse it later.
26345 (Even though there is no definition, there might be default
26346 arguments that need handling.) */
26347 if (member_p && decl
26348 && DECL_DECLARES_FUNCTION_P (decl))
26349 vec_safe_push (unparsed_funs_with_definitions, decl);
26352 /* Parse a template introduction header for a template-declaration. Returns
26353 false if tentative parse fails. */
26355 static bool
26356 cp_parser_template_introduction (cp_parser* parser, bool member_p)
26358 cp_parser_parse_tentatively (parser);
26360 tree saved_scope = parser->scope;
26361 tree saved_object_scope = parser->object_scope;
26362 tree saved_qualifying_scope = parser->qualifying_scope;
26364 /* Look for the optional `::' operator. */
26365 cp_parser_global_scope_opt (parser,
26366 /*current_scope_valid_p=*/false);
26367 /* Look for the nested-name-specifier. */
26368 cp_parser_nested_name_specifier_opt (parser,
26369 /*typename_keyword_p=*/false,
26370 /*check_dependency_p=*/true,
26371 /*type_p=*/false,
26372 /*is_declaration=*/false);
26374 cp_token *token = cp_lexer_peek_token (parser->lexer);
26375 tree concept_name = cp_parser_identifier (parser);
26377 /* Look up the concept for which we will be matching
26378 template parameters. */
26379 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
26380 token->location);
26381 parser->scope = saved_scope;
26382 parser->object_scope = saved_object_scope;
26383 parser->qualifying_scope = saved_qualifying_scope;
26385 if (concept_name == error_mark_node)
26386 cp_parser_simulate_error (parser);
26388 /* Look for opening brace for introduction. */
26389 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
26391 if (!cp_parser_parse_definitely (parser))
26392 return false;
26394 push_deferring_access_checks (dk_deferred);
26396 /* Build vector of placeholder parameters and grab
26397 matching identifiers. */
26398 tree introduction_list = cp_parser_introduction_list (parser);
26400 /* The introduction-list shall not be empty. */
26401 int nargs = TREE_VEC_LENGTH (introduction_list);
26402 if (nargs == 0)
26404 error ("empty introduction-list");
26405 return true;
26408 /* Look for closing brace for introduction. */
26409 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
26410 return true;
26412 if (tmpl_decl == error_mark_node)
26414 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
26415 token->location);
26416 return true;
26419 /* Build and associate the constraint. */
26420 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
26421 if (parms && parms != error_mark_node)
26423 cp_parser_template_declaration_after_parameters (parser, parms,
26424 member_p);
26425 return true;
26428 error_at (token->location, "no matching concept for template-introduction");
26429 return true;
26432 /* Parse a normal template-declaration following the template keyword. */
26434 static void
26435 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
26437 tree parameter_list;
26438 bool need_lang_pop;
26439 location_t location = input_location;
26441 /* Look for the `<' token. */
26442 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
26443 return;
26444 if (at_class_scope_p () && current_function_decl)
26446 /* 14.5.2.2 [temp.mem]
26448 A local class shall not have member templates. */
26449 error_at (location,
26450 "invalid declaration of member template in local class");
26451 cp_parser_skip_to_end_of_block_or_statement (parser);
26452 return;
26454 /* [temp]
26456 A template ... shall not have C linkage. */
26457 if (current_lang_name == lang_name_c)
26459 error_at (location, "template with C linkage");
26460 /* Give it C++ linkage to avoid confusing other parts of the
26461 front end. */
26462 push_lang_context (lang_name_cplusplus);
26463 need_lang_pop = true;
26465 else
26466 need_lang_pop = false;
26468 /* We cannot perform access checks on the template parameter
26469 declarations until we know what is being declared, just as we
26470 cannot check the decl-specifier list. */
26471 push_deferring_access_checks (dk_deferred);
26473 /* If the next token is `>', then we have an invalid
26474 specialization. Rather than complain about an invalid template
26475 parameter, issue an error message here. */
26476 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
26478 cp_parser_error (parser, "invalid explicit specialization");
26479 begin_specialization ();
26480 parameter_list = NULL_TREE;
26482 else
26484 /* Parse the template parameters. */
26485 parameter_list = cp_parser_template_parameter_list (parser);
26488 /* Look for the `>'. */
26489 cp_parser_skip_to_end_of_template_parameter_list (parser);
26491 /* Manage template requirements */
26492 if (flag_concepts)
26494 tree reqs = get_shorthand_constraints (current_template_parms);
26495 if (tree r = cp_parser_requires_clause_opt (parser))
26496 reqs = conjoin_constraints (reqs, normalize_expression (r));
26497 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
26500 cp_parser_template_declaration_after_parameters (parser, parameter_list,
26501 member_p);
26503 /* For the erroneous case of a template with C linkage, we pushed an
26504 implicit C++ linkage scope; exit that scope now. */
26505 if (need_lang_pop)
26506 pop_lang_context ();
26509 /* Parse a template-declaration, assuming that the `export' (and
26510 `extern') keywords, if present, has already been scanned. MEMBER_P
26511 is as for cp_parser_template_declaration. */
26513 static bool
26514 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
26516 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26518 cp_lexer_consume_token (parser->lexer);
26519 cp_parser_explicit_template_declaration (parser, member_p);
26520 return true;
26522 else if (flag_concepts)
26523 return cp_parser_template_introduction (parser, member_p);
26525 return false;
26528 /* Perform the deferred access checks from a template-parameter-list.
26529 CHECKS is a TREE_LIST of access checks, as returned by
26530 get_deferred_access_checks. */
26532 static void
26533 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
26535 ++processing_template_parmlist;
26536 perform_access_checks (checks, tf_warning_or_error);
26537 --processing_template_parmlist;
26540 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
26541 `function-definition' sequence that follows a template header.
26542 If MEMBER_P is true, this declaration appears in a class scope.
26544 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
26545 *FRIEND_P is set to TRUE iff the declaration is a friend. */
26547 static tree
26548 cp_parser_single_declaration (cp_parser* parser,
26549 vec<deferred_access_check, va_gc> *checks,
26550 bool member_p,
26551 bool explicit_specialization_p,
26552 bool* friend_p)
26554 int declares_class_or_enum;
26555 tree decl = NULL_TREE;
26556 cp_decl_specifier_seq decl_specifiers;
26557 bool function_definition_p = false;
26558 cp_token *decl_spec_token_start;
26560 /* This function is only used when processing a template
26561 declaration. */
26562 gcc_assert (innermost_scope_kind () == sk_template_parms
26563 || innermost_scope_kind () == sk_template_spec);
26565 /* Defer access checks until we know what is being declared. */
26566 push_deferring_access_checks (dk_deferred);
26568 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
26569 alternative. */
26570 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
26571 cp_parser_decl_specifier_seq (parser,
26572 CP_PARSER_FLAGS_OPTIONAL,
26573 &decl_specifiers,
26574 &declares_class_or_enum);
26575 if (friend_p)
26576 *friend_p = cp_parser_friend_p (&decl_specifiers);
26578 /* There are no template typedefs. */
26579 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
26581 error_at (decl_spec_token_start->location,
26582 "template declaration of %<typedef%>");
26583 decl = error_mark_node;
26586 /* Gather up the access checks that occurred the
26587 decl-specifier-seq. */
26588 stop_deferring_access_checks ();
26590 /* Check for the declaration of a template class. */
26591 if (declares_class_or_enum)
26593 if (cp_parser_declares_only_class_p (parser)
26594 || (declares_class_or_enum & 2))
26596 // If this is a declaration, but not a definition, associate
26597 // any constraints with the type declaration. Constraints
26598 // are associated with definitions in cp_parser_class_specifier.
26599 if (declares_class_or_enum == 1)
26600 associate_classtype_constraints (decl_specifiers.type);
26602 decl = shadow_tag (&decl_specifiers);
26604 /* In this case:
26606 struct C {
26607 friend template <typename T> struct A<T>::B;
26610 A<T>::B will be represented by a TYPENAME_TYPE, and
26611 therefore not recognized by shadow_tag. */
26612 if (friend_p && *friend_p
26613 && !decl
26614 && decl_specifiers.type
26615 && TYPE_P (decl_specifiers.type))
26616 decl = decl_specifiers.type;
26618 if (decl && decl != error_mark_node)
26619 decl = TYPE_NAME (decl);
26620 else
26621 decl = error_mark_node;
26623 /* Perform access checks for template parameters. */
26624 cp_parser_perform_template_parameter_access_checks (checks);
26626 /* Give a helpful diagnostic for
26627 template <class T> struct A { } a;
26628 if we aren't already recovering from an error. */
26629 if (!cp_parser_declares_only_class_p (parser)
26630 && !seen_error ())
26632 error_at (cp_lexer_peek_token (parser->lexer)->location,
26633 "a class template declaration must not declare "
26634 "anything else");
26635 cp_parser_skip_to_end_of_block_or_statement (parser);
26636 goto out;
26641 /* Complain about missing 'typename' or other invalid type names. */
26642 if (!decl_specifiers.any_type_specifiers_p
26643 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
26645 /* cp_parser_parse_and_diagnose_invalid_type_name calls
26646 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
26647 the rest of this declaration. */
26648 decl = error_mark_node;
26649 goto out;
26652 /* If it's not a template class, try for a template function. If
26653 the next token is a `;', then this declaration does not declare
26654 anything. But, if there were errors in the decl-specifiers, then
26655 the error might well have come from an attempted class-specifier.
26656 In that case, there's no need to warn about a missing declarator. */
26657 if (!decl
26658 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
26659 || decl_specifiers.type != error_mark_node))
26661 decl = cp_parser_init_declarator (parser,
26662 &decl_specifiers,
26663 checks,
26664 /*function_definition_allowed_p=*/true,
26665 member_p,
26666 declares_class_or_enum,
26667 &function_definition_p,
26668 NULL, NULL, NULL);
26670 /* 7.1.1-1 [dcl.stc]
26672 A storage-class-specifier shall not be specified in an explicit
26673 specialization... */
26674 if (decl
26675 && explicit_specialization_p
26676 && decl_specifiers.storage_class != sc_none)
26678 error_at (decl_spec_token_start->location,
26679 "explicit template specialization cannot have a storage class");
26680 decl = error_mark_node;
26683 if (decl && VAR_P (decl))
26684 check_template_variable (decl);
26687 /* Look for a trailing `;' after the declaration. */
26688 if (!function_definition_p
26689 && (decl == error_mark_node
26690 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
26691 cp_parser_skip_to_end_of_block_or_statement (parser);
26693 out:
26694 pop_deferring_access_checks ();
26696 /* Clear any current qualification; whatever comes next is the start
26697 of something new. */
26698 parser->scope = NULL_TREE;
26699 parser->qualifying_scope = NULL_TREE;
26700 parser->object_scope = NULL_TREE;
26702 return decl;
26705 /* Parse a cast-expression that is not the operand of a unary "&". */
26707 static cp_expr
26708 cp_parser_simple_cast_expression (cp_parser *parser)
26710 return cp_parser_cast_expression (parser, /*address_p=*/false,
26711 /*cast_p=*/false, /*decltype*/false, NULL);
26714 /* Parse a functional cast to TYPE. Returns an expression
26715 representing the cast. */
26717 static cp_expr
26718 cp_parser_functional_cast (cp_parser* parser, tree type)
26720 vec<tree, va_gc> *vec;
26721 tree expression_list;
26722 cp_expr cast;
26723 bool nonconst_p;
26725 location_t start_loc = input_location;
26727 if (!type)
26728 type = error_mark_node;
26730 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26732 cp_lexer_set_source_position (parser->lexer);
26733 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26734 expression_list = cp_parser_braced_list (parser, &nonconst_p);
26735 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
26736 if (TREE_CODE (type) == TYPE_DECL)
26737 type = TREE_TYPE (type);
26739 cast = finish_compound_literal (type, expression_list,
26740 tf_warning_or_error);
26741 /* Create a location of the form:
26742 type_name{i, f}
26743 ^~~~~~~~~~~~~~~
26744 with caret == start at the start of the type name,
26745 finishing at the closing brace. */
26746 location_t finish_loc
26747 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26748 location_t combined_loc = make_location (start_loc, start_loc,
26749 finish_loc);
26750 cast.set_location (combined_loc);
26751 return cast;
26755 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
26756 /*cast_p=*/true,
26757 /*allow_expansion_p=*/true,
26758 /*non_constant_p=*/NULL);
26759 if (vec == NULL)
26760 expression_list = error_mark_node;
26761 else
26763 expression_list = build_tree_list_vec (vec);
26764 release_tree_vector (vec);
26767 cast = build_functional_cast (type, expression_list,
26768 tf_warning_or_error);
26769 /* [expr.const]/1: In an integral constant expression "only type
26770 conversions to integral or enumeration type can be used". */
26771 if (TREE_CODE (type) == TYPE_DECL)
26772 type = TREE_TYPE (type);
26773 if (cast != error_mark_node
26774 && !cast_valid_in_integral_constant_expression_p (type)
26775 && cp_parser_non_integral_constant_expression (parser,
26776 NIC_CONSTRUCTOR))
26777 return error_mark_node;
26779 /* Create a location of the form:
26780 float(i)
26781 ^~~~~~~~
26782 with caret == start at the start of the type name,
26783 finishing at the closing paren. */
26784 location_t finish_loc
26785 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26786 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
26787 cast.set_location (combined_loc);
26788 return cast;
26791 /* Save the tokens that make up the body of a member function defined
26792 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
26793 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
26794 specifiers applied to the declaration. Returns the FUNCTION_DECL
26795 for the member function. */
26797 static tree
26798 cp_parser_save_member_function_body (cp_parser* parser,
26799 cp_decl_specifier_seq *decl_specifiers,
26800 cp_declarator *declarator,
26801 tree attributes)
26803 cp_token *first;
26804 cp_token *last;
26805 tree fn;
26806 bool function_try_block = false;
26808 /* Create the FUNCTION_DECL. */
26809 fn = grokmethod (decl_specifiers, declarator, attributes);
26810 cp_finalize_omp_declare_simd (parser, fn);
26811 cp_finalize_oacc_routine (parser, fn, true);
26812 /* If something went badly wrong, bail out now. */
26813 if (fn == error_mark_node)
26815 /* If there's a function-body, skip it. */
26816 if (cp_parser_token_starts_function_definition_p
26817 (cp_lexer_peek_token (parser->lexer)))
26818 cp_parser_skip_to_end_of_block_or_statement (parser);
26819 return error_mark_node;
26822 /* Remember it, if there default args to post process. */
26823 cp_parser_save_default_args (parser, fn);
26825 /* Save away the tokens that make up the body of the
26826 function. */
26827 first = parser->lexer->next_token;
26829 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
26830 cp_lexer_consume_token (parser->lexer);
26831 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26832 RID_TRANSACTION_ATOMIC))
26834 cp_lexer_consume_token (parser->lexer);
26835 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
26836 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
26837 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
26838 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
26839 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
26840 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
26841 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
26843 cp_lexer_consume_token (parser->lexer);
26844 cp_lexer_consume_token (parser->lexer);
26845 cp_lexer_consume_token (parser->lexer);
26846 cp_lexer_consume_token (parser->lexer);
26847 cp_lexer_consume_token (parser->lexer);
26849 else
26850 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
26851 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
26853 cp_lexer_consume_token (parser->lexer);
26854 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26855 break;
26859 /* Handle function try blocks. */
26860 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26862 cp_lexer_consume_token (parser->lexer);
26863 function_try_block = true;
26865 /* We can have braced-init-list mem-initializers before the fn body. */
26866 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26868 cp_lexer_consume_token (parser->lexer);
26869 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
26871 /* cache_group will stop after an un-nested { } pair, too. */
26872 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26873 break;
26875 /* variadic mem-inits have ... after the ')'. */
26876 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26877 cp_lexer_consume_token (parser->lexer);
26880 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26881 /* Handle function try blocks. */
26882 if (function_try_block)
26883 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
26884 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26885 last = parser->lexer->next_token;
26887 /* Save away the inline definition; we will process it when the
26888 class is complete. */
26889 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
26890 DECL_PENDING_INLINE_P (fn) = 1;
26892 /* We need to know that this was defined in the class, so that
26893 friend templates are handled correctly. */
26894 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
26896 /* Add FN to the queue of functions to be parsed later. */
26897 vec_safe_push (unparsed_funs_with_definitions, fn);
26899 return fn;
26902 /* Save the tokens that make up the in-class initializer for a non-static
26903 data member. Returns a DEFAULT_ARG. */
26905 static tree
26906 cp_parser_save_nsdmi (cp_parser* parser)
26908 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
26911 /* Parse a template-argument-list, as well as the trailing ">" (but
26912 not the opening "<"). See cp_parser_template_argument_list for the
26913 return value. */
26915 static tree
26916 cp_parser_enclosed_template_argument_list (cp_parser* parser)
26918 tree arguments;
26919 tree saved_scope;
26920 tree saved_qualifying_scope;
26921 tree saved_object_scope;
26922 bool saved_greater_than_is_operator_p;
26923 int saved_unevaluated_operand;
26924 int saved_inhibit_evaluation_warnings;
26926 /* [temp.names]
26928 When parsing a template-id, the first non-nested `>' is taken as
26929 the end of the template-argument-list rather than a greater-than
26930 operator. */
26931 saved_greater_than_is_operator_p
26932 = parser->greater_than_is_operator_p;
26933 parser->greater_than_is_operator_p = false;
26934 /* Parsing the argument list may modify SCOPE, so we save it
26935 here. */
26936 saved_scope = parser->scope;
26937 saved_qualifying_scope = parser->qualifying_scope;
26938 saved_object_scope = parser->object_scope;
26939 /* We need to evaluate the template arguments, even though this
26940 template-id may be nested within a "sizeof". */
26941 saved_unevaluated_operand = cp_unevaluated_operand;
26942 cp_unevaluated_operand = 0;
26943 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
26944 c_inhibit_evaluation_warnings = 0;
26945 /* Parse the template-argument-list itself. */
26946 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
26947 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26948 arguments = NULL_TREE;
26949 else
26950 arguments = cp_parser_template_argument_list (parser);
26951 /* Look for the `>' that ends the template-argument-list. If we find
26952 a '>>' instead, it's probably just a typo. */
26953 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26955 if (cxx_dialect != cxx98)
26957 /* In C++0x, a `>>' in a template argument list or cast
26958 expression is considered to be two separate `>'
26959 tokens. So, change the current token to a `>', but don't
26960 consume it: it will be consumed later when the outer
26961 template argument list (or cast expression) is parsed.
26962 Note that this replacement of `>' for `>>' is necessary
26963 even if we are parsing tentatively: in the tentative
26964 case, after calling
26965 cp_parser_enclosed_template_argument_list we will always
26966 throw away all of the template arguments and the first
26967 closing `>', either because the template argument list
26968 was erroneous or because we are replacing those tokens
26969 with a CPP_TEMPLATE_ID token. The second `>' (which will
26970 not have been thrown away) is needed either to close an
26971 outer template argument list or to complete a new-style
26972 cast. */
26973 cp_token *token = cp_lexer_peek_token (parser->lexer);
26974 token->type = CPP_GREATER;
26976 else if (!saved_greater_than_is_operator_p)
26978 /* If we're in a nested template argument list, the '>>' has
26979 to be a typo for '> >'. We emit the error message, but we
26980 continue parsing and we push a '>' as next token, so that
26981 the argument list will be parsed correctly. Note that the
26982 global source location is still on the token before the
26983 '>>', so we need to say explicitly where we want it. */
26984 cp_token *token = cp_lexer_peek_token (parser->lexer);
26985 gcc_rich_location richloc (token->location);
26986 richloc.add_fixit_replace ("> >");
26987 error_at_rich_loc (&richloc, "%<>>%> should be %<> >%> "
26988 "within a nested template argument list");
26990 token->type = CPP_GREATER;
26992 else
26994 /* If this is not a nested template argument list, the '>>'
26995 is a typo for '>'. Emit an error message and continue.
26996 Same deal about the token location, but here we can get it
26997 right by consuming the '>>' before issuing the diagnostic. */
26998 cp_token *token = cp_lexer_consume_token (parser->lexer);
26999 error_at (token->location,
27000 "spurious %<>>%>, use %<>%> to terminate "
27001 "a template argument list");
27004 else
27005 cp_parser_skip_to_end_of_template_parameter_list (parser);
27006 /* The `>' token might be a greater-than operator again now. */
27007 parser->greater_than_is_operator_p
27008 = saved_greater_than_is_operator_p;
27009 /* Restore the SAVED_SCOPE. */
27010 parser->scope = saved_scope;
27011 parser->qualifying_scope = saved_qualifying_scope;
27012 parser->object_scope = saved_object_scope;
27013 cp_unevaluated_operand = saved_unevaluated_operand;
27014 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27016 return arguments;
27019 /* MEMBER_FUNCTION is a member function, or a friend. If default
27020 arguments, or the body of the function have not yet been parsed,
27021 parse them now. */
27023 static void
27024 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27026 timevar_push (TV_PARSE_INMETH);
27027 /* If this member is a template, get the underlying
27028 FUNCTION_DECL. */
27029 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27030 member_function = DECL_TEMPLATE_RESULT (member_function);
27032 /* There should not be any class definitions in progress at this
27033 point; the bodies of members are only parsed outside of all class
27034 definitions. */
27035 gcc_assert (parser->num_classes_being_defined == 0);
27036 /* While we're parsing the member functions we might encounter more
27037 classes. We want to handle them right away, but we don't want
27038 them getting mixed up with functions that are currently in the
27039 queue. */
27040 push_unparsed_function_queues (parser);
27042 /* Make sure that any template parameters are in scope. */
27043 maybe_begin_member_template_processing (member_function);
27045 /* If the body of the function has not yet been parsed, parse it
27046 now. */
27047 if (DECL_PENDING_INLINE_P (member_function))
27049 tree function_scope;
27050 cp_token_cache *tokens;
27052 /* The function is no longer pending; we are processing it. */
27053 tokens = DECL_PENDING_INLINE_INFO (member_function);
27054 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27055 DECL_PENDING_INLINE_P (member_function) = 0;
27057 /* If this is a local class, enter the scope of the containing
27058 function. */
27059 function_scope = current_function_decl;
27060 if (function_scope)
27061 push_function_context ();
27063 /* Push the body of the function onto the lexer stack. */
27064 cp_parser_push_lexer_for_tokens (parser, tokens);
27066 /* Let the front end know that we going to be defining this
27067 function. */
27068 start_preparsed_function (member_function, NULL_TREE,
27069 SF_PRE_PARSED | SF_INCLASS_INLINE);
27071 /* Don't do access checking if it is a templated function. */
27072 if (processing_template_decl)
27073 push_deferring_access_checks (dk_no_check);
27075 /* #pragma omp declare reduction needs special parsing. */
27076 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27078 parser->lexer->in_pragma = true;
27079 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27080 finish_function (/*inline*/2);
27081 cp_check_omp_declare_reduction (member_function);
27083 else
27084 /* Now, parse the body of the function. */
27085 cp_parser_function_definition_after_declarator (parser,
27086 /*inline_p=*/true);
27088 if (processing_template_decl)
27089 pop_deferring_access_checks ();
27091 /* Leave the scope of the containing function. */
27092 if (function_scope)
27093 pop_function_context ();
27094 cp_parser_pop_lexer (parser);
27097 /* Remove any template parameters from the symbol table. */
27098 maybe_end_member_template_processing ();
27100 /* Restore the queue. */
27101 pop_unparsed_function_queues (parser);
27102 timevar_pop (TV_PARSE_INMETH);
27105 /* If DECL contains any default args, remember it on the unparsed
27106 functions queue. */
27108 static void
27109 cp_parser_save_default_args (cp_parser* parser, tree decl)
27111 tree probe;
27113 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27114 probe;
27115 probe = TREE_CHAIN (probe))
27116 if (TREE_PURPOSE (probe))
27118 cp_default_arg_entry entry = {current_class_type, decl};
27119 vec_safe_push (unparsed_funs_with_default_args, entry);
27120 break;
27124 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27125 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27126 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27127 from the parameter-type-list. */
27129 static tree
27130 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27131 tree default_arg, tree parmtype)
27133 cp_token_cache *tokens;
27134 tree parsed_arg;
27135 bool dummy;
27137 if (default_arg == error_mark_node)
27138 return error_mark_node;
27140 /* Push the saved tokens for the default argument onto the parser's
27141 lexer stack. */
27142 tokens = DEFARG_TOKENS (default_arg);
27143 cp_parser_push_lexer_for_tokens (parser, tokens);
27145 start_lambda_scope (decl);
27147 /* Parse the default argument. */
27148 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27149 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27150 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27152 finish_lambda_scope ();
27154 if (parsed_arg == error_mark_node)
27155 cp_parser_skip_to_end_of_statement (parser);
27157 if (!processing_template_decl)
27159 /* In a non-template class, check conversions now. In a template,
27160 we'll wait and instantiate these as needed. */
27161 if (TREE_CODE (decl) == PARM_DECL)
27162 parsed_arg = check_default_argument (parmtype, parsed_arg,
27163 tf_warning_or_error);
27164 else
27165 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
27168 /* If the token stream has not been completely used up, then
27169 there was extra junk after the end of the default
27170 argument. */
27171 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27173 if (TREE_CODE (decl) == PARM_DECL)
27174 cp_parser_error (parser, "expected %<,%>");
27175 else
27176 cp_parser_error (parser, "expected %<;%>");
27179 /* Revert to the main lexer. */
27180 cp_parser_pop_lexer (parser);
27182 return parsed_arg;
27185 /* FIELD is a non-static data member with an initializer which we saved for
27186 later; parse it now. */
27188 static void
27189 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27191 tree def;
27193 maybe_begin_member_template_processing (field);
27195 push_unparsed_function_queues (parser);
27196 def = cp_parser_late_parse_one_default_arg (parser, field,
27197 DECL_INITIAL (field),
27198 NULL_TREE);
27199 pop_unparsed_function_queues (parser);
27201 maybe_end_member_template_processing ();
27203 DECL_INITIAL (field) = def;
27206 /* FN is a FUNCTION_DECL which may contains a parameter with an
27207 unparsed DEFAULT_ARG. Parse the default args now. This function
27208 assumes that the current scope is the scope in which the default
27209 argument should be processed. */
27211 static void
27212 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27214 bool saved_local_variables_forbidden_p;
27215 tree parm, parmdecl;
27217 /* While we're parsing the default args, we might (due to the
27218 statement expression extension) encounter more classes. We want
27219 to handle them right away, but we don't want them getting mixed
27220 up with default args that are currently in the queue. */
27221 push_unparsed_function_queues (parser);
27223 /* Local variable names (and the `this' keyword) may not appear
27224 in a default argument. */
27225 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27226 parser->local_variables_forbidden_p = true;
27228 push_defarg_context (fn);
27230 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27231 parmdecl = DECL_ARGUMENTS (fn);
27232 parm && parm != void_list_node;
27233 parm = TREE_CHAIN (parm),
27234 parmdecl = DECL_CHAIN (parmdecl))
27236 tree default_arg = TREE_PURPOSE (parm);
27237 tree parsed_arg;
27238 vec<tree, va_gc> *insts;
27239 tree copy;
27240 unsigned ix;
27242 if (!default_arg)
27243 continue;
27245 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27246 /* This can happen for a friend declaration for a function
27247 already declared with default arguments. */
27248 continue;
27250 parsed_arg
27251 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27252 default_arg,
27253 TREE_VALUE (parm));
27254 if (parsed_arg == error_mark_node)
27256 continue;
27259 TREE_PURPOSE (parm) = parsed_arg;
27261 /* Update any instantiations we've already created. */
27262 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27263 vec_safe_iterate (insts, ix, &copy); ix++)
27264 TREE_PURPOSE (copy) = parsed_arg;
27267 pop_defarg_context ();
27269 /* Make sure no default arg is missing. */
27270 check_default_args (fn);
27272 /* Restore the state of local_variables_forbidden_p. */
27273 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27275 /* Restore the queue. */
27276 pop_unparsed_function_queues (parser);
27279 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27281 sizeof ... ( identifier )
27283 where the 'sizeof' token has already been consumed. */
27285 static tree
27286 cp_parser_sizeof_pack (cp_parser *parser)
27288 /* Consume the `...'. */
27289 cp_lexer_consume_token (parser->lexer);
27290 maybe_warn_variadic_templates ();
27292 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
27293 if (paren)
27294 cp_lexer_consume_token (parser->lexer);
27295 else
27296 permerror (cp_lexer_peek_token (parser->lexer)->location,
27297 "%<sizeof...%> argument must be surrounded by parentheses");
27299 cp_token *token = cp_lexer_peek_token (parser->lexer);
27300 tree name = cp_parser_identifier (parser);
27301 if (name == error_mark_node)
27302 return error_mark_node;
27303 /* The name is not qualified. */
27304 parser->scope = NULL_TREE;
27305 parser->qualifying_scope = NULL_TREE;
27306 parser->object_scope = NULL_TREE;
27307 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
27308 if (expr == error_mark_node)
27309 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
27310 token->location);
27311 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
27312 expr = TREE_TYPE (expr);
27313 else if (TREE_CODE (expr) == CONST_DECL)
27314 expr = DECL_INITIAL (expr);
27315 expr = make_pack_expansion (expr);
27316 PACK_EXPANSION_SIZEOF_P (expr) = true;
27318 if (paren)
27319 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27321 return expr;
27324 /* Parse the operand of `sizeof' (or a similar operator). Returns
27325 either a TYPE or an expression, depending on the form of the
27326 input. The KEYWORD indicates which kind of expression we have
27327 encountered. */
27329 static tree
27330 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
27332 tree expr = NULL_TREE;
27333 const char *saved_message;
27334 char *tmp;
27335 bool saved_integral_constant_expression_p;
27336 bool saved_non_integral_constant_expression_p;
27338 /* If it's a `...', then we are computing the length of a parameter
27339 pack. */
27340 if (keyword == RID_SIZEOF
27341 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27342 return cp_parser_sizeof_pack (parser);
27344 /* Types cannot be defined in a `sizeof' expression. Save away the
27345 old message. */
27346 saved_message = parser->type_definition_forbidden_message;
27347 /* And create the new one. */
27348 tmp = concat ("types may not be defined in %<",
27349 IDENTIFIER_POINTER (ridpointers[keyword]),
27350 "%> expressions", NULL);
27351 parser->type_definition_forbidden_message = tmp;
27353 /* The restrictions on constant-expressions do not apply inside
27354 sizeof expressions. */
27355 saved_integral_constant_expression_p
27356 = parser->integral_constant_expression_p;
27357 saved_non_integral_constant_expression_p
27358 = parser->non_integral_constant_expression_p;
27359 parser->integral_constant_expression_p = false;
27361 /* Do not actually evaluate the expression. */
27362 ++cp_unevaluated_operand;
27363 ++c_inhibit_evaluation_warnings;
27364 /* If it's a `(', then we might be looking at the type-id
27365 construction. */
27366 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27368 tree type = NULL_TREE;
27370 /* We can't be sure yet whether we're looking at a type-id or an
27371 expression. */
27372 cp_parser_parse_tentatively (parser);
27373 /* Note: as a GNU Extension, compound literals are considered
27374 postfix-expressions as they are in C99, so they are valid
27375 arguments to sizeof. See comment in cp_parser_cast_expression
27376 for details. */
27377 if (cp_parser_compound_literal_p (parser))
27378 cp_parser_simulate_error (parser);
27379 else
27381 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
27382 parser->in_type_id_in_expr_p = true;
27383 /* Look for the type-id. */
27384 type = cp_parser_type_id (parser);
27385 /* Look for the closing `)'. */
27386 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27387 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
27390 /* If all went well, then we're done. */
27391 if (cp_parser_parse_definitely (parser))
27393 cp_decl_specifier_seq decl_specs;
27395 /* Build a trivial decl-specifier-seq. */
27396 clear_decl_specs (&decl_specs);
27397 decl_specs.type = type;
27399 /* Call grokdeclarator to figure out what type this is. */
27400 expr = grokdeclarator (NULL,
27401 &decl_specs,
27402 TYPENAME,
27403 /*initialized=*/0,
27404 /*attrlist=*/NULL);
27408 /* If the type-id production did not work out, then we must be
27409 looking at the unary-expression production. */
27410 if (!expr)
27411 expr = cp_parser_unary_expression (parser);
27413 /* Go back to evaluating expressions. */
27414 --cp_unevaluated_operand;
27415 --c_inhibit_evaluation_warnings;
27417 /* Free the message we created. */
27418 free (tmp);
27419 /* And restore the old one. */
27420 parser->type_definition_forbidden_message = saved_message;
27421 parser->integral_constant_expression_p
27422 = saved_integral_constant_expression_p;
27423 parser->non_integral_constant_expression_p
27424 = saved_non_integral_constant_expression_p;
27426 return expr;
27429 /* If the current declaration has no declarator, return true. */
27431 static bool
27432 cp_parser_declares_only_class_p (cp_parser *parser)
27434 /* If the next token is a `;' or a `,' then there is no
27435 declarator. */
27436 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27437 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
27440 /* Update the DECL_SPECS to reflect the storage class indicated by
27441 KEYWORD. */
27443 static void
27444 cp_parser_set_storage_class (cp_parser *parser,
27445 cp_decl_specifier_seq *decl_specs,
27446 enum rid keyword,
27447 cp_token *token)
27449 cp_storage_class storage_class;
27451 if (parser->in_unbraced_linkage_specification_p)
27453 error_at (token->location, "invalid use of %qD in linkage specification",
27454 ridpointers[keyword]);
27455 return;
27457 else if (decl_specs->storage_class != sc_none)
27459 decl_specs->conflicting_specifiers_p = true;
27460 return;
27463 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
27464 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
27465 && decl_specs->gnu_thread_keyword_p)
27467 pedwarn (decl_specs->locations[ds_thread], 0,
27468 "%<__thread%> before %qD", ridpointers[keyword]);
27471 switch (keyword)
27473 case RID_AUTO:
27474 storage_class = sc_auto;
27475 break;
27476 case RID_REGISTER:
27477 storage_class = sc_register;
27478 break;
27479 case RID_STATIC:
27480 storage_class = sc_static;
27481 break;
27482 case RID_EXTERN:
27483 storage_class = sc_extern;
27484 break;
27485 case RID_MUTABLE:
27486 storage_class = sc_mutable;
27487 break;
27488 default:
27489 gcc_unreachable ();
27491 decl_specs->storage_class = storage_class;
27492 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
27494 /* A storage class specifier cannot be applied alongside a typedef
27495 specifier. If there is a typedef specifier present then set
27496 conflicting_specifiers_p which will trigger an error later
27497 on in grokdeclarator. */
27498 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
27499 decl_specs->conflicting_specifiers_p = true;
27502 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
27503 is true, the type is a class or enum definition. */
27505 static void
27506 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
27507 tree type_spec,
27508 cp_token *token,
27509 bool type_definition_p)
27511 decl_specs->any_specifiers_p = true;
27513 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
27514 (with, for example, in "typedef int wchar_t;") we remember that
27515 this is what happened. In system headers, we ignore these
27516 declarations so that G++ can work with system headers that are not
27517 C++-safe. */
27518 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
27519 && !type_definition_p
27520 && (type_spec == boolean_type_node
27521 || type_spec == char16_type_node
27522 || type_spec == char32_type_node
27523 || type_spec == wchar_type_node)
27524 && (decl_specs->type
27525 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
27526 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
27527 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
27528 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
27530 decl_specs->redefined_builtin_type = type_spec;
27531 set_and_check_decl_spec_loc (decl_specs,
27532 ds_redefined_builtin_type_spec,
27533 token);
27534 if (!decl_specs->type)
27536 decl_specs->type = type_spec;
27537 decl_specs->type_definition_p = false;
27538 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
27541 else if (decl_specs->type)
27542 decl_specs->multiple_types_p = true;
27543 else
27545 decl_specs->type = type_spec;
27546 decl_specs->type_definition_p = type_definition_p;
27547 decl_specs->redefined_builtin_type = NULL_TREE;
27548 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
27552 /* True iff TOKEN is the GNU keyword __thread. */
27554 static bool
27555 token_is__thread (cp_token *token)
27557 gcc_assert (token->keyword == RID_THREAD);
27558 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
27561 /* Set the location for a declarator specifier and check if it is
27562 duplicated.
27564 DECL_SPECS is the sequence of declarator specifiers onto which to
27565 set the location.
27567 DS is the single declarator specifier to set which location is to
27568 be set onto the existing sequence of declarators.
27570 LOCATION is the location for the declarator specifier to
27571 consider. */
27573 static void
27574 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
27575 cp_decl_spec ds, cp_token *token)
27577 gcc_assert (ds < ds_last);
27579 if (decl_specs == NULL)
27580 return;
27582 source_location location = token->location;
27584 if (decl_specs->locations[ds] == 0)
27586 decl_specs->locations[ds] = location;
27587 if (ds == ds_thread)
27588 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
27590 else
27592 if (ds == ds_long)
27594 if (decl_specs->locations[ds_long_long] != 0)
27595 error_at (location,
27596 "%<long long long%> is too long for GCC");
27597 else
27599 decl_specs->locations[ds_long_long] = location;
27600 pedwarn_cxx98 (location,
27601 OPT_Wlong_long,
27602 "ISO C++ 1998 does not support %<long long%>");
27605 else if (ds == ds_thread)
27607 bool gnu = token_is__thread (token);
27608 if (gnu != decl_specs->gnu_thread_keyword_p)
27609 error_at (location,
27610 "both %<__thread%> and %<thread_local%> specified");
27611 else
27612 error_at (location, "duplicate %qD", token->u.value);
27614 else
27616 static const char *const decl_spec_names[] = {
27617 "signed",
27618 "unsigned",
27619 "short",
27620 "long",
27621 "const",
27622 "volatile",
27623 "restrict",
27624 "inline",
27625 "virtual",
27626 "explicit",
27627 "friend",
27628 "typedef",
27629 "using",
27630 "constexpr",
27631 "__complex"
27633 error_at (location,
27634 "duplicate %qs", decl_spec_names[ds]);
27639 /* Return true iff the declarator specifier DS is present in the
27640 sequence of declarator specifiers DECL_SPECS. */
27642 bool
27643 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
27644 cp_decl_spec ds)
27646 gcc_assert (ds < ds_last);
27648 if (decl_specs == NULL)
27649 return false;
27651 return decl_specs->locations[ds] != 0;
27654 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
27655 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
27657 static bool
27658 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
27660 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
27663 /* Issue an error message indicating that TOKEN_DESC was expected.
27664 If KEYWORD is true, it indicated this function is called by
27665 cp_parser_require_keword and the required token can only be
27666 a indicated keyword. */
27668 static void
27669 cp_parser_required_error (cp_parser *parser,
27670 required_token token_desc,
27671 bool keyword)
27673 switch (token_desc)
27675 case RT_NEW:
27676 cp_parser_error (parser, "expected %<new%>");
27677 return;
27678 case RT_DELETE:
27679 cp_parser_error (parser, "expected %<delete%>");
27680 return;
27681 case RT_RETURN:
27682 cp_parser_error (parser, "expected %<return%>");
27683 return;
27684 case RT_WHILE:
27685 cp_parser_error (parser, "expected %<while%>");
27686 return;
27687 case RT_EXTERN:
27688 cp_parser_error (parser, "expected %<extern%>");
27689 return;
27690 case RT_STATIC_ASSERT:
27691 cp_parser_error (parser, "expected %<static_assert%>");
27692 return;
27693 case RT_DECLTYPE:
27694 cp_parser_error (parser, "expected %<decltype%>");
27695 return;
27696 case RT_OPERATOR:
27697 cp_parser_error (parser, "expected %<operator%>");
27698 return;
27699 case RT_CLASS:
27700 cp_parser_error (parser, "expected %<class%>");
27701 return;
27702 case RT_TEMPLATE:
27703 cp_parser_error (parser, "expected %<template%>");
27704 return;
27705 case RT_NAMESPACE:
27706 cp_parser_error (parser, "expected %<namespace%>");
27707 return;
27708 case RT_USING:
27709 cp_parser_error (parser, "expected %<using%>");
27710 return;
27711 case RT_ASM:
27712 cp_parser_error (parser, "expected %<asm%>");
27713 return;
27714 case RT_TRY:
27715 cp_parser_error (parser, "expected %<try%>");
27716 return;
27717 case RT_CATCH:
27718 cp_parser_error (parser, "expected %<catch%>");
27719 return;
27720 case RT_THROW:
27721 cp_parser_error (parser, "expected %<throw%>");
27722 return;
27723 case RT_LABEL:
27724 cp_parser_error (parser, "expected %<__label__%>");
27725 return;
27726 case RT_AT_TRY:
27727 cp_parser_error (parser, "expected %<@try%>");
27728 return;
27729 case RT_AT_SYNCHRONIZED:
27730 cp_parser_error (parser, "expected %<@synchronized%>");
27731 return;
27732 case RT_AT_THROW:
27733 cp_parser_error (parser, "expected %<@throw%>");
27734 return;
27735 case RT_TRANSACTION_ATOMIC:
27736 cp_parser_error (parser, "expected %<__transaction_atomic%>");
27737 return;
27738 case RT_TRANSACTION_RELAXED:
27739 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
27740 return;
27741 default:
27742 break;
27744 if (!keyword)
27746 switch (token_desc)
27748 case RT_SEMICOLON:
27749 cp_parser_error (parser, "expected %<;%>");
27750 return;
27751 case RT_OPEN_PAREN:
27752 cp_parser_error (parser, "expected %<(%>");
27753 return;
27754 case RT_CLOSE_BRACE:
27755 cp_parser_error (parser, "expected %<}%>");
27756 return;
27757 case RT_OPEN_BRACE:
27758 cp_parser_error (parser, "expected %<{%>");
27759 return;
27760 case RT_CLOSE_SQUARE:
27761 cp_parser_error (parser, "expected %<]%>");
27762 return;
27763 case RT_OPEN_SQUARE:
27764 cp_parser_error (parser, "expected %<[%>");
27765 return;
27766 case RT_COMMA:
27767 cp_parser_error (parser, "expected %<,%>");
27768 return;
27769 case RT_SCOPE:
27770 cp_parser_error (parser, "expected %<::%>");
27771 return;
27772 case RT_LESS:
27773 cp_parser_error (parser, "expected %<<%>");
27774 return;
27775 case RT_GREATER:
27776 cp_parser_error (parser, "expected %<>%>");
27777 return;
27778 case RT_EQ:
27779 cp_parser_error (parser, "expected %<=%>");
27780 return;
27781 case RT_ELLIPSIS:
27782 cp_parser_error (parser, "expected %<...%>");
27783 return;
27784 case RT_MULT:
27785 cp_parser_error (parser, "expected %<*%>");
27786 return;
27787 case RT_COMPL:
27788 cp_parser_error (parser, "expected %<~%>");
27789 return;
27790 case RT_COLON:
27791 cp_parser_error (parser, "expected %<:%>");
27792 return;
27793 case RT_COLON_SCOPE:
27794 cp_parser_error (parser, "expected %<:%> or %<::%>");
27795 return;
27796 case RT_CLOSE_PAREN:
27797 cp_parser_error (parser, "expected %<)%>");
27798 return;
27799 case RT_COMMA_CLOSE_PAREN:
27800 cp_parser_error (parser, "expected %<,%> or %<)%>");
27801 return;
27802 case RT_PRAGMA_EOL:
27803 cp_parser_error (parser, "expected end of line");
27804 return;
27805 case RT_NAME:
27806 cp_parser_error (parser, "expected identifier");
27807 return;
27808 case RT_SELECT:
27809 cp_parser_error (parser, "expected selection-statement");
27810 return;
27811 case RT_INTERATION:
27812 cp_parser_error (parser, "expected iteration-statement");
27813 return;
27814 case RT_JUMP:
27815 cp_parser_error (parser, "expected jump-statement");
27816 return;
27817 case RT_CLASS_KEY:
27818 cp_parser_error (parser, "expected class-key");
27819 return;
27820 case RT_CLASS_TYPENAME_TEMPLATE:
27821 cp_parser_error (parser,
27822 "expected %<class%>, %<typename%>, or %<template%>");
27823 return;
27824 default:
27825 gcc_unreachable ();
27828 else
27829 gcc_unreachable ();
27834 /* If the next token is of the indicated TYPE, consume it. Otherwise,
27835 issue an error message indicating that TOKEN_DESC was expected.
27837 Returns the token consumed, if the token had the appropriate type.
27838 Otherwise, returns NULL. */
27840 static cp_token *
27841 cp_parser_require (cp_parser* parser,
27842 enum cpp_ttype type,
27843 required_token token_desc)
27845 if (cp_lexer_next_token_is (parser->lexer, type))
27846 return cp_lexer_consume_token (parser->lexer);
27847 else
27849 /* Output the MESSAGE -- unless we're parsing tentatively. */
27850 if (!cp_parser_simulate_error (parser))
27851 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
27852 return NULL;
27856 /* An error message is produced if the next token is not '>'.
27857 All further tokens are skipped until the desired token is
27858 found or '{', '}', ';' or an unbalanced ')' or ']'. */
27860 static void
27861 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
27863 /* Current level of '< ... >'. */
27864 unsigned level = 0;
27865 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
27866 unsigned nesting_depth = 0;
27868 /* Are we ready, yet? If not, issue error message. */
27869 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
27870 return;
27872 /* Skip tokens until the desired token is found. */
27873 while (true)
27875 /* Peek at the next token. */
27876 switch (cp_lexer_peek_token (parser->lexer)->type)
27878 case CPP_LESS:
27879 if (!nesting_depth)
27880 ++level;
27881 break;
27883 case CPP_RSHIFT:
27884 if (cxx_dialect == cxx98)
27885 /* C++0x views the `>>' operator as two `>' tokens, but
27886 C++98 does not. */
27887 break;
27888 else if (!nesting_depth && level-- == 0)
27890 /* We've hit a `>>' where the first `>' closes the
27891 template argument list, and the second `>' is
27892 spurious. Just consume the `>>' and stop; we've
27893 already produced at least one error. */
27894 cp_lexer_consume_token (parser->lexer);
27895 return;
27897 /* Fall through for C++0x, so we handle the second `>' in
27898 the `>>'. */
27899 gcc_fallthrough ();
27901 case CPP_GREATER:
27902 if (!nesting_depth && level-- == 0)
27904 /* We've reached the token we want, consume it and stop. */
27905 cp_lexer_consume_token (parser->lexer);
27906 return;
27908 break;
27910 case CPP_OPEN_PAREN:
27911 case CPP_OPEN_SQUARE:
27912 ++nesting_depth;
27913 break;
27915 case CPP_CLOSE_PAREN:
27916 case CPP_CLOSE_SQUARE:
27917 if (nesting_depth-- == 0)
27918 return;
27919 break;
27921 case CPP_EOF:
27922 case CPP_PRAGMA_EOL:
27923 case CPP_SEMICOLON:
27924 case CPP_OPEN_BRACE:
27925 case CPP_CLOSE_BRACE:
27926 /* The '>' was probably forgotten, don't look further. */
27927 return;
27929 default:
27930 break;
27933 /* Consume this token. */
27934 cp_lexer_consume_token (parser->lexer);
27938 /* If the next token is the indicated keyword, consume it. Otherwise,
27939 issue an error message indicating that TOKEN_DESC was expected.
27941 Returns the token consumed, if the token had the appropriate type.
27942 Otherwise, returns NULL. */
27944 static cp_token *
27945 cp_parser_require_keyword (cp_parser* parser,
27946 enum rid keyword,
27947 required_token token_desc)
27949 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
27951 if (token && token->keyword != keyword)
27953 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
27954 return NULL;
27957 return token;
27960 /* Returns TRUE iff TOKEN is a token that can begin the body of a
27961 function-definition. */
27963 static bool
27964 cp_parser_token_starts_function_definition_p (cp_token* token)
27966 return (/* An ordinary function-body begins with an `{'. */
27967 token->type == CPP_OPEN_BRACE
27968 /* A ctor-initializer begins with a `:'. */
27969 || token->type == CPP_COLON
27970 /* A function-try-block begins with `try'. */
27971 || token->keyword == RID_TRY
27972 /* A function-transaction-block begins with `__transaction_atomic'
27973 or `__transaction_relaxed'. */
27974 || token->keyword == RID_TRANSACTION_ATOMIC
27975 || token->keyword == RID_TRANSACTION_RELAXED
27976 /* The named return value extension begins with `return'. */
27977 || token->keyword == RID_RETURN);
27980 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
27981 definition. */
27983 static bool
27984 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
27986 cp_token *token;
27988 token = cp_lexer_peek_token (parser->lexer);
27989 return (token->type == CPP_OPEN_BRACE
27990 || (token->type == CPP_COLON
27991 && !parser->colon_doesnt_start_class_def_p));
27994 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
27995 C++0x) ending a template-argument. */
27997 static bool
27998 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28000 cp_token *token;
28002 token = cp_lexer_peek_token (parser->lexer);
28003 return (token->type == CPP_COMMA
28004 || token->type == CPP_GREATER
28005 || token->type == CPP_ELLIPSIS
28006 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28009 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28010 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28012 static bool
28013 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28014 size_t n)
28016 cp_token *token;
28018 token = cp_lexer_peek_nth_token (parser->lexer, n);
28019 if (token->type == CPP_LESS)
28020 return true;
28021 /* Check for the sequence `<::' in the original code. It would be lexed as
28022 `[:', where `[' is a digraph, and there is no whitespace before
28023 `:'. */
28024 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28026 cp_token *token2;
28027 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28028 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28029 return true;
28031 return false;
28034 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28035 or none_type otherwise. */
28037 static enum tag_types
28038 cp_parser_token_is_class_key (cp_token* token)
28040 switch (token->keyword)
28042 case RID_CLASS:
28043 return class_type;
28044 case RID_STRUCT:
28045 return record_type;
28046 case RID_UNION:
28047 return union_type;
28049 default:
28050 return none_type;
28054 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28055 or none_type otherwise or if the token is null. */
28057 static enum tag_types
28058 cp_parser_token_is_type_parameter_key (cp_token* token)
28060 if (!token)
28061 return none_type;
28063 switch (token->keyword)
28065 case RID_CLASS:
28066 return class_type;
28067 case RID_TYPENAME:
28068 return typename_type;
28070 default:
28071 return none_type;
28075 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28077 static void
28078 cp_parser_check_class_key (enum tag_types class_key, tree type)
28080 if (type == error_mark_node)
28081 return;
28082 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28084 if (permerror (input_location, "%qs tag used in naming %q#T",
28085 class_key == union_type ? "union"
28086 : class_key == record_type ? "struct" : "class",
28087 type))
28088 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28089 "%q#T was previously declared here", type);
28093 /* Issue an error message if DECL is redeclared with different
28094 access than its original declaration [class.access.spec/3].
28095 This applies to nested classes, nested class templates and
28096 enumerations [class.mem/1]. */
28098 static void
28099 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28101 if (!decl
28102 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28103 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28104 return;
28106 if ((TREE_PRIVATE (decl)
28107 != (current_access_specifier == access_private_node))
28108 || (TREE_PROTECTED (decl)
28109 != (current_access_specifier == access_protected_node)))
28110 error_at (location, "%qD redeclared with different access", decl);
28113 /* Look for the `template' keyword, as a syntactic disambiguator.
28114 Return TRUE iff it is present, in which case it will be
28115 consumed. */
28117 static bool
28118 cp_parser_optional_template_keyword (cp_parser *parser)
28120 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28122 /* In C++98 the `template' keyword can only be used within templates;
28123 outside templates the parser can always figure out what is a
28124 template and what is not. In C++11, per the resolution of DR 468,
28125 `template' is allowed in cases where it is not strictly necessary. */
28126 if (!processing_template_decl
28127 && pedantic && cxx_dialect == cxx98)
28129 cp_token *token = cp_lexer_peek_token (parser->lexer);
28130 pedwarn (token->location, OPT_Wpedantic,
28131 "in C++98 %<template%> (as a disambiguator) is only "
28132 "allowed within templates");
28133 /* If this part of the token stream is rescanned, the same
28134 error message would be generated. So, we purge the token
28135 from the stream. */
28136 cp_lexer_purge_token (parser->lexer);
28137 return false;
28139 else
28141 /* Consume the `template' keyword. */
28142 cp_lexer_consume_token (parser->lexer);
28143 return true;
28146 return false;
28149 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28150 set PARSER->SCOPE, and perform other related actions. */
28152 static void
28153 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28155 struct tree_check *check_value;
28157 /* Get the stored value. */
28158 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28159 /* Set the scope from the stored value. */
28160 parser->scope = saved_checks_value (check_value);
28161 parser->qualifying_scope = check_value->qualifying_scope;
28162 parser->object_scope = NULL_TREE;
28165 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28166 encounter the end of a block before what we were looking for. */
28168 static bool
28169 cp_parser_cache_group (cp_parser *parser,
28170 enum cpp_ttype end,
28171 unsigned depth)
28173 while (true)
28175 cp_token *token = cp_lexer_peek_token (parser->lexer);
28177 /* Abort a parenthesized expression if we encounter a semicolon. */
28178 if ((end == CPP_CLOSE_PAREN || depth == 0)
28179 && token->type == CPP_SEMICOLON)
28180 return true;
28181 /* If we've reached the end of the file, stop. */
28182 if (token->type == CPP_EOF
28183 || (end != CPP_PRAGMA_EOL
28184 && token->type == CPP_PRAGMA_EOL))
28185 return true;
28186 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28187 /* We've hit the end of an enclosing block, so there's been some
28188 kind of syntax error. */
28189 return true;
28191 /* Consume the token. */
28192 cp_lexer_consume_token (parser->lexer);
28193 /* See if it starts a new group. */
28194 if (token->type == CPP_OPEN_BRACE)
28196 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28197 /* In theory this should probably check end == '}', but
28198 cp_parser_save_member_function_body needs it to exit
28199 after either '}' or ')' when called with ')'. */
28200 if (depth == 0)
28201 return false;
28203 else if (token->type == CPP_OPEN_PAREN)
28205 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28206 if (depth == 0 && end == CPP_CLOSE_PAREN)
28207 return false;
28209 else if (token->type == CPP_PRAGMA)
28210 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28211 else if (token->type == end)
28212 return false;
28216 /* Like above, for caching a default argument or NSDMI. Both of these are
28217 terminated by a non-nested comma, but it can be unclear whether or not a
28218 comma is nested in a template argument list unless we do more parsing.
28219 In order to handle this ambiguity, when we encounter a ',' after a '<'
28220 we try to parse what follows as a parameter-declaration-list (in the
28221 case of a default argument) or a member-declarator (in the case of an
28222 NSDMI). If that succeeds, then we stop caching. */
28224 static tree
28225 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28227 unsigned depth = 0;
28228 int maybe_template_id = 0;
28229 cp_token *first_token;
28230 cp_token *token;
28231 tree default_argument;
28233 /* Add tokens until we have processed the entire default
28234 argument. We add the range [first_token, token). */
28235 first_token = cp_lexer_peek_token (parser->lexer);
28236 if (first_token->type == CPP_OPEN_BRACE)
28238 /* For list-initialization, this is straightforward. */
28239 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28240 token = cp_lexer_peek_token (parser->lexer);
28242 else while (true)
28244 bool done = false;
28246 /* Peek at the next token. */
28247 token = cp_lexer_peek_token (parser->lexer);
28248 /* What we do depends on what token we have. */
28249 switch (token->type)
28251 /* In valid code, a default argument must be
28252 immediately followed by a `,' `)', or `...'. */
28253 case CPP_COMMA:
28254 if (depth == 0 && maybe_template_id)
28256 /* If we've seen a '<', we might be in a
28257 template-argument-list. Until Core issue 325 is
28258 resolved, we don't know how this situation ought
28259 to be handled, so try to DTRT. We check whether
28260 what comes after the comma is a valid parameter
28261 declaration list. If it is, then the comma ends
28262 the default argument; otherwise the default
28263 argument continues. */
28264 bool error = false;
28265 cp_token *peek;
28267 /* Set ITALP so cp_parser_parameter_declaration_list
28268 doesn't decide to commit to this parse. */
28269 bool saved_italp = parser->in_template_argument_list_p;
28270 parser->in_template_argument_list_p = true;
28272 cp_parser_parse_tentatively (parser);
28274 if (nsdmi)
28276 /* Parse declarators until we reach a non-comma or
28277 somthing that cannot be an initializer.
28278 Just checking whether we're looking at a single
28279 declarator is insufficient. Consider:
28280 int var = tuple<T,U>::x;
28281 The template parameter 'U' looks exactly like a
28282 declarator. */
28285 int ctor_dtor_or_conv_p;
28286 cp_lexer_consume_token (parser->lexer);
28287 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28288 &ctor_dtor_or_conv_p,
28289 /*parenthesized_p=*/NULL,
28290 /*member_p=*/true,
28291 /*friend_p=*/false);
28292 peek = cp_lexer_peek_token (parser->lexer);
28293 if (cp_parser_error_occurred (parser))
28294 break;
28296 while (peek->type == CPP_COMMA);
28297 /* If we met an '=' or ';' then the original comma
28298 was the end of the NSDMI. Otherwise assume
28299 we're still in the NSDMI. */
28300 error = (peek->type != CPP_EQ
28301 && peek->type != CPP_SEMICOLON);
28303 else
28305 cp_lexer_consume_token (parser->lexer);
28306 begin_scope (sk_function_parms, NULL_TREE);
28307 cp_parser_parameter_declaration_list (parser, &error);
28308 pop_bindings_and_leave_scope ();
28310 if (!cp_parser_error_occurred (parser) && !error)
28311 done = true;
28312 cp_parser_abort_tentative_parse (parser);
28314 parser->in_template_argument_list_p = saved_italp;
28315 break;
28317 /* FALLTHRU */
28318 case CPP_CLOSE_PAREN:
28319 case CPP_ELLIPSIS:
28320 /* If we run into a non-nested `;', `}', or `]',
28321 then the code is invalid -- but the default
28322 argument is certainly over. */
28323 case CPP_SEMICOLON:
28324 case CPP_CLOSE_BRACE:
28325 case CPP_CLOSE_SQUARE:
28326 if (depth == 0
28327 /* Handle correctly int n = sizeof ... ( p ); */
28328 && token->type != CPP_ELLIPSIS)
28329 done = true;
28330 /* Update DEPTH, if necessary. */
28331 else if (token->type == CPP_CLOSE_PAREN
28332 || token->type == CPP_CLOSE_BRACE
28333 || token->type == CPP_CLOSE_SQUARE)
28334 --depth;
28335 break;
28337 case CPP_OPEN_PAREN:
28338 case CPP_OPEN_SQUARE:
28339 case CPP_OPEN_BRACE:
28340 ++depth;
28341 break;
28343 case CPP_LESS:
28344 if (depth == 0)
28345 /* This might be the comparison operator, or it might
28346 start a template argument list. */
28347 ++maybe_template_id;
28348 break;
28350 case CPP_RSHIFT:
28351 if (cxx_dialect == cxx98)
28352 break;
28353 /* Fall through for C++0x, which treats the `>>'
28354 operator like two `>' tokens in certain
28355 cases. */
28356 gcc_fallthrough ();
28358 case CPP_GREATER:
28359 if (depth == 0)
28361 /* This might be an operator, or it might close a
28362 template argument list. But if a previous '<'
28363 started a template argument list, this will have
28364 closed it, so we can't be in one anymore. */
28365 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
28366 if (maybe_template_id < 0)
28367 maybe_template_id = 0;
28369 break;
28371 /* If we run out of tokens, issue an error message. */
28372 case CPP_EOF:
28373 case CPP_PRAGMA_EOL:
28374 error_at (token->location, "file ends in default argument");
28375 return error_mark_node;
28377 case CPP_NAME:
28378 case CPP_SCOPE:
28379 /* In these cases, we should look for template-ids.
28380 For example, if the default argument is
28381 `X<int, double>()', we need to do name lookup to
28382 figure out whether or not `X' is a template; if
28383 so, the `,' does not end the default argument.
28385 That is not yet done. */
28386 break;
28388 default:
28389 break;
28392 /* If we've reached the end, stop. */
28393 if (done)
28394 break;
28396 /* Add the token to the token block. */
28397 token = cp_lexer_consume_token (parser->lexer);
28400 /* Create a DEFAULT_ARG to represent the unparsed default
28401 argument. */
28402 default_argument = make_node (DEFAULT_ARG);
28403 DEFARG_TOKENS (default_argument)
28404 = cp_token_cache_new (first_token, token);
28405 DEFARG_INSTANTIATIONS (default_argument) = NULL;
28407 return default_argument;
28410 /* Begin parsing tentatively. We always save tokens while parsing
28411 tentatively so that if the tentative parsing fails we can restore the
28412 tokens. */
28414 static void
28415 cp_parser_parse_tentatively (cp_parser* parser)
28417 /* Enter a new parsing context. */
28418 parser->context = cp_parser_context_new (parser->context);
28419 /* Begin saving tokens. */
28420 cp_lexer_save_tokens (parser->lexer);
28421 /* In order to avoid repetitive access control error messages,
28422 access checks are queued up until we are no longer parsing
28423 tentatively. */
28424 push_deferring_access_checks (dk_deferred);
28427 /* Commit to the currently active tentative parse. */
28429 static void
28430 cp_parser_commit_to_tentative_parse (cp_parser* parser)
28432 cp_parser_context *context;
28433 cp_lexer *lexer;
28435 /* Mark all of the levels as committed. */
28436 lexer = parser->lexer;
28437 for (context = parser->context; context->next; context = context->next)
28439 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28440 break;
28441 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28442 while (!cp_lexer_saving_tokens (lexer))
28443 lexer = lexer->next;
28444 cp_lexer_commit_tokens (lexer);
28448 /* Commit to the topmost currently active tentative parse.
28450 Note that this function shouldn't be called when there are
28451 irreversible side-effects while in a tentative state. For
28452 example, we shouldn't create a permanent entry in the symbol
28453 table, or issue an error message that might not apply if the
28454 tentative parse is aborted. */
28456 static void
28457 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
28459 cp_parser_context *context = parser->context;
28460 cp_lexer *lexer = parser->lexer;
28462 if (context)
28464 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28465 return;
28466 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28468 while (!cp_lexer_saving_tokens (lexer))
28469 lexer = lexer->next;
28470 cp_lexer_commit_tokens (lexer);
28474 /* Abort the currently active tentative parse. All consumed tokens
28475 will be rolled back, and no diagnostics will be issued. */
28477 static void
28478 cp_parser_abort_tentative_parse (cp_parser* parser)
28480 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
28481 || errorcount > 0);
28482 cp_parser_simulate_error (parser);
28483 /* Now, pretend that we want to see if the construct was
28484 successfully parsed. */
28485 cp_parser_parse_definitely (parser);
28488 /* Stop parsing tentatively. If a parse error has occurred, restore the
28489 token stream. Otherwise, commit to the tokens we have consumed.
28490 Returns true if no error occurred; false otherwise. */
28492 static bool
28493 cp_parser_parse_definitely (cp_parser* parser)
28495 bool error_occurred;
28496 cp_parser_context *context;
28498 /* Remember whether or not an error occurred, since we are about to
28499 destroy that information. */
28500 error_occurred = cp_parser_error_occurred (parser);
28501 /* Remove the topmost context from the stack. */
28502 context = parser->context;
28503 parser->context = context->next;
28504 /* If no parse errors occurred, commit to the tentative parse. */
28505 if (!error_occurred)
28507 /* Commit to the tokens read tentatively, unless that was
28508 already done. */
28509 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
28510 cp_lexer_commit_tokens (parser->lexer);
28512 pop_to_parent_deferring_access_checks ();
28514 /* Otherwise, if errors occurred, roll back our state so that things
28515 are just as they were before we began the tentative parse. */
28516 else
28518 cp_lexer_rollback_tokens (parser->lexer);
28519 pop_deferring_access_checks ();
28521 /* Add the context to the front of the free list. */
28522 context->next = cp_parser_context_free_list;
28523 cp_parser_context_free_list = context;
28525 return !error_occurred;
28528 /* Returns true if we are parsing tentatively and are not committed to
28529 this tentative parse. */
28531 static bool
28532 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
28534 return (cp_parser_parsing_tentatively (parser)
28535 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
28538 /* Returns nonzero iff an error has occurred during the most recent
28539 tentative parse. */
28541 static bool
28542 cp_parser_error_occurred (cp_parser* parser)
28544 return (cp_parser_parsing_tentatively (parser)
28545 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
28548 /* Returns nonzero if GNU extensions are allowed. */
28550 static bool
28551 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
28553 return parser->allow_gnu_extensions_p;
28556 /* Objective-C++ Productions */
28559 /* Parse an Objective-C expression, which feeds into a primary-expression
28560 above.
28562 objc-expression:
28563 objc-message-expression
28564 objc-string-literal
28565 objc-encode-expression
28566 objc-protocol-expression
28567 objc-selector-expression
28569 Returns a tree representation of the expression. */
28571 static cp_expr
28572 cp_parser_objc_expression (cp_parser* parser)
28574 /* Try to figure out what kind of declaration is present. */
28575 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
28577 switch (kwd->type)
28579 case CPP_OPEN_SQUARE:
28580 return cp_parser_objc_message_expression (parser);
28582 case CPP_OBJC_STRING:
28583 kwd = cp_lexer_consume_token (parser->lexer);
28584 return objc_build_string_object (kwd->u.value);
28586 case CPP_KEYWORD:
28587 switch (kwd->keyword)
28589 case RID_AT_ENCODE:
28590 return cp_parser_objc_encode_expression (parser);
28592 case RID_AT_PROTOCOL:
28593 return cp_parser_objc_protocol_expression (parser);
28595 case RID_AT_SELECTOR:
28596 return cp_parser_objc_selector_expression (parser);
28598 default:
28599 break;
28601 default:
28602 error_at (kwd->location,
28603 "misplaced %<@%D%> Objective-C++ construct",
28604 kwd->u.value);
28605 cp_parser_skip_to_end_of_block_or_statement (parser);
28608 return error_mark_node;
28611 /* Parse an Objective-C message expression.
28613 objc-message-expression:
28614 [ objc-message-receiver objc-message-args ]
28616 Returns a representation of an Objective-C message. */
28618 static tree
28619 cp_parser_objc_message_expression (cp_parser* parser)
28621 tree receiver, messageargs;
28623 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28624 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
28625 receiver = cp_parser_objc_message_receiver (parser);
28626 messageargs = cp_parser_objc_message_args (parser);
28627 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
28628 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
28630 tree result = objc_build_message_expr (receiver, messageargs);
28632 /* Construct a location e.g.
28633 [self func1:5]
28634 ^~~~~~~~~~~~~~
28635 ranging from the '[' to the ']', with the caret at the start. */
28636 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
28637 protected_set_expr_location (result, combined_loc);
28639 return result;
28642 /* Parse an objc-message-receiver.
28644 objc-message-receiver:
28645 expression
28646 simple-type-specifier
28648 Returns a representation of the type or expression. */
28650 static tree
28651 cp_parser_objc_message_receiver (cp_parser* parser)
28653 tree rcv;
28655 /* An Objective-C message receiver may be either (1) a type
28656 or (2) an expression. */
28657 cp_parser_parse_tentatively (parser);
28658 rcv = cp_parser_expression (parser);
28660 /* If that worked out, fine. */
28661 if (cp_parser_parse_definitely (parser))
28662 return rcv;
28664 cp_parser_parse_tentatively (parser);
28665 rcv = cp_parser_simple_type_specifier (parser,
28666 /*decl_specs=*/NULL,
28667 CP_PARSER_FLAGS_NONE);
28669 if (cp_parser_parse_definitely (parser))
28670 return objc_get_class_reference (rcv);
28672 cp_parser_error (parser, "objective-c++ message receiver expected");
28673 return error_mark_node;
28676 /* Parse the arguments and selectors comprising an Objective-C message.
28678 objc-message-args:
28679 objc-selector
28680 objc-selector-args
28681 objc-selector-args , objc-comma-args
28683 objc-selector-args:
28684 objc-selector [opt] : assignment-expression
28685 objc-selector-args objc-selector [opt] : assignment-expression
28687 objc-comma-args:
28688 assignment-expression
28689 objc-comma-args , assignment-expression
28691 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
28692 selector arguments and TREE_VALUE containing a list of comma
28693 arguments. */
28695 static tree
28696 cp_parser_objc_message_args (cp_parser* parser)
28698 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
28699 bool maybe_unary_selector_p = true;
28700 cp_token *token = cp_lexer_peek_token (parser->lexer);
28702 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
28704 tree selector = NULL_TREE, arg;
28706 if (token->type != CPP_COLON)
28707 selector = cp_parser_objc_selector (parser);
28709 /* Detect if we have a unary selector. */
28710 if (maybe_unary_selector_p
28711 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28712 return build_tree_list (selector, NULL_TREE);
28714 maybe_unary_selector_p = false;
28715 cp_parser_require (parser, CPP_COLON, RT_COLON);
28716 arg = cp_parser_assignment_expression (parser);
28718 sel_args
28719 = chainon (sel_args,
28720 build_tree_list (selector, arg));
28722 token = cp_lexer_peek_token (parser->lexer);
28725 /* Handle non-selector arguments, if any. */
28726 while (token->type == CPP_COMMA)
28728 tree arg;
28730 cp_lexer_consume_token (parser->lexer);
28731 arg = cp_parser_assignment_expression (parser);
28733 addl_args
28734 = chainon (addl_args,
28735 build_tree_list (NULL_TREE, arg));
28737 token = cp_lexer_peek_token (parser->lexer);
28740 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
28742 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
28743 return build_tree_list (error_mark_node, error_mark_node);
28746 return build_tree_list (sel_args, addl_args);
28749 /* Parse an Objective-C encode expression.
28751 objc-encode-expression:
28752 @encode objc-typename
28754 Returns an encoded representation of the type argument. */
28756 static cp_expr
28757 cp_parser_objc_encode_expression (cp_parser* parser)
28759 tree type;
28760 cp_token *token;
28761 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28763 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
28764 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28765 token = cp_lexer_peek_token (parser->lexer);
28766 type = complete_type (cp_parser_type_id (parser));
28767 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28769 if (!type)
28771 error_at (token->location,
28772 "%<@encode%> must specify a type as an argument");
28773 return error_mark_node;
28776 /* This happens if we find @encode(T) (where T is a template
28777 typename or something dependent on a template typename) when
28778 parsing a template. In that case, we can't compile it
28779 immediately, but we rather create an AT_ENCODE_EXPR which will
28780 need to be instantiated when the template is used.
28782 if (dependent_type_p (type))
28784 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
28785 TREE_READONLY (value) = 1;
28786 return value;
28790 /* Build a location of the form:
28791 @encode(int)
28792 ^~~~~~~~~~~~
28793 with caret==start at the @ token, finishing at the close paren. */
28794 location_t combined_loc
28795 = make_location (start_loc, start_loc,
28796 cp_lexer_previous_token (parser->lexer)->location);
28798 return cp_expr (objc_build_encode_expr (type), combined_loc);
28801 /* Parse an Objective-C @defs expression. */
28803 static tree
28804 cp_parser_objc_defs_expression (cp_parser *parser)
28806 tree name;
28808 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
28809 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28810 name = cp_parser_identifier (parser);
28811 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28813 return objc_get_class_ivars (name);
28816 /* Parse an Objective-C protocol expression.
28818 objc-protocol-expression:
28819 @protocol ( identifier )
28821 Returns a representation of the protocol expression. */
28823 static tree
28824 cp_parser_objc_protocol_expression (cp_parser* parser)
28826 tree proto;
28827 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28829 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
28830 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28831 proto = cp_parser_identifier (parser);
28832 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28834 /* Build a location of the form:
28835 @protocol(prot)
28836 ^~~~~~~~~~~~~~~
28837 with caret==start at the @ token, finishing at the close paren. */
28838 location_t combined_loc
28839 = make_location (start_loc, start_loc,
28840 cp_lexer_previous_token (parser->lexer)->location);
28841 tree result = objc_build_protocol_expr (proto);
28842 protected_set_expr_location (result, combined_loc);
28843 return result;
28846 /* Parse an Objective-C selector expression.
28848 objc-selector-expression:
28849 @selector ( objc-method-signature )
28851 objc-method-signature:
28852 objc-selector
28853 objc-selector-seq
28855 objc-selector-seq:
28856 objc-selector :
28857 objc-selector-seq objc-selector :
28859 Returns a representation of the method selector. */
28861 static tree
28862 cp_parser_objc_selector_expression (cp_parser* parser)
28864 tree sel_seq = NULL_TREE;
28865 bool maybe_unary_selector_p = true;
28866 cp_token *token;
28867 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28869 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
28870 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28871 token = cp_lexer_peek_token (parser->lexer);
28873 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
28874 || token->type == CPP_SCOPE)
28876 tree selector = NULL_TREE;
28878 if (token->type != CPP_COLON
28879 || token->type == CPP_SCOPE)
28880 selector = cp_parser_objc_selector (parser);
28882 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
28883 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
28885 /* Detect if we have a unary selector. */
28886 if (maybe_unary_selector_p)
28888 sel_seq = selector;
28889 goto finish_selector;
28891 else
28893 cp_parser_error (parser, "expected %<:%>");
28896 maybe_unary_selector_p = false;
28897 token = cp_lexer_consume_token (parser->lexer);
28899 if (token->type == CPP_SCOPE)
28901 sel_seq
28902 = chainon (sel_seq,
28903 build_tree_list (selector, NULL_TREE));
28904 sel_seq
28905 = chainon (sel_seq,
28906 build_tree_list (NULL_TREE, NULL_TREE));
28908 else
28909 sel_seq
28910 = chainon (sel_seq,
28911 build_tree_list (selector, NULL_TREE));
28913 token = cp_lexer_peek_token (parser->lexer);
28916 finish_selector:
28917 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28920 /* Build a location of the form:
28921 @selector(func)
28922 ^~~~~~~~~~~~~~~
28923 with caret==start at the @ token, finishing at the close paren. */
28924 location_t combined_loc
28925 = make_location (loc, loc,
28926 cp_lexer_previous_token (parser->lexer)->location);
28927 tree result = objc_build_selector_expr (combined_loc, sel_seq);
28928 /* TODO: objc_build_selector_expr doesn't always honor the location. */
28929 protected_set_expr_location (result, combined_loc);
28930 return result;
28933 /* Parse a list of identifiers.
28935 objc-identifier-list:
28936 identifier
28937 objc-identifier-list , identifier
28939 Returns a TREE_LIST of identifier nodes. */
28941 static tree
28942 cp_parser_objc_identifier_list (cp_parser* parser)
28944 tree identifier;
28945 tree list;
28946 cp_token *sep;
28948 identifier = cp_parser_identifier (parser);
28949 if (identifier == error_mark_node)
28950 return error_mark_node;
28952 list = build_tree_list (NULL_TREE, identifier);
28953 sep = cp_lexer_peek_token (parser->lexer);
28955 while (sep->type == CPP_COMMA)
28957 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28958 identifier = cp_parser_identifier (parser);
28959 if (identifier == error_mark_node)
28960 return list;
28962 list = chainon (list, build_tree_list (NULL_TREE,
28963 identifier));
28964 sep = cp_lexer_peek_token (parser->lexer);
28967 return list;
28970 /* Parse an Objective-C alias declaration.
28972 objc-alias-declaration:
28973 @compatibility_alias identifier identifier ;
28975 This function registers the alias mapping with the Objective-C front end.
28976 It returns nothing. */
28978 static void
28979 cp_parser_objc_alias_declaration (cp_parser* parser)
28981 tree alias, orig;
28983 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
28984 alias = cp_parser_identifier (parser);
28985 orig = cp_parser_identifier (parser);
28986 objc_declare_alias (alias, orig);
28987 cp_parser_consume_semicolon_at_end_of_statement (parser);
28990 /* Parse an Objective-C class forward-declaration.
28992 objc-class-declaration:
28993 @class objc-identifier-list ;
28995 The function registers the forward declarations with the Objective-C
28996 front end. It returns nothing. */
28998 static void
28999 cp_parser_objc_class_declaration (cp_parser* parser)
29001 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29002 while (true)
29004 tree id;
29006 id = cp_parser_identifier (parser);
29007 if (id == error_mark_node)
29008 break;
29010 objc_declare_class (id);
29012 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29013 cp_lexer_consume_token (parser->lexer);
29014 else
29015 break;
29017 cp_parser_consume_semicolon_at_end_of_statement (parser);
29020 /* Parse a list of Objective-C protocol references.
29022 objc-protocol-refs-opt:
29023 objc-protocol-refs [opt]
29025 objc-protocol-refs:
29026 < objc-identifier-list >
29028 Returns a TREE_LIST of identifiers, if any. */
29030 static tree
29031 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29033 tree protorefs = NULL_TREE;
29035 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29037 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29038 protorefs = cp_parser_objc_identifier_list (parser);
29039 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29042 return protorefs;
29045 /* Parse a Objective-C visibility specification. */
29047 static void
29048 cp_parser_objc_visibility_spec (cp_parser* parser)
29050 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29052 switch (vis->keyword)
29054 case RID_AT_PRIVATE:
29055 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29056 break;
29057 case RID_AT_PROTECTED:
29058 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29059 break;
29060 case RID_AT_PUBLIC:
29061 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29062 break;
29063 case RID_AT_PACKAGE:
29064 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29065 break;
29066 default:
29067 return;
29070 /* Eat '@private'/'@protected'/'@public'. */
29071 cp_lexer_consume_token (parser->lexer);
29074 /* Parse an Objective-C method type. Return 'true' if it is a class
29075 (+) method, and 'false' if it is an instance (-) method. */
29077 static inline bool
29078 cp_parser_objc_method_type (cp_parser* parser)
29080 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29081 return true;
29082 else
29083 return false;
29086 /* Parse an Objective-C protocol qualifier. */
29088 static tree
29089 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29091 tree quals = NULL_TREE, node;
29092 cp_token *token = cp_lexer_peek_token (parser->lexer);
29094 node = token->u.value;
29096 while (node && identifier_p (node)
29097 && (node == ridpointers [(int) RID_IN]
29098 || node == ridpointers [(int) RID_OUT]
29099 || node == ridpointers [(int) RID_INOUT]
29100 || node == ridpointers [(int) RID_BYCOPY]
29101 || node == ridpointers [(int) RID_BYREF]
29102 || node == ridpointers [(int) RID_ONEWAY]))
29104 quals = tree_cons (NULL_TREE, node, quals);
29105 cp_lexer_consume_token (parser->lexer);
29106 token = cp_lexer_peek_token (parser->lexer);
29107 node = token->u.value;
29110 return quals;
29113 /* Parse an Objective-C typename. */
29115 static tree
29116 cp_parser_objc_typename (cp_parser* parser)
29118 tree type_name = NULL_TREE;
29120 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29122 tree proto_quals, cp_type = NULL_TREE;
29124 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
29125 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29127 /* An ObjC type name may consist of just protocol qualifiers, in which
29128 case the type shall default to 'id'. */
29129 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29131 cp_type = cp_parser_type_id (parser);
29133 /* If the type could not be parsed, an error has already
29134 been produced. For error recovery, behave as if it had
29135 not been specified, which will use the default type
29136 'id'. */
29137 if (cp_type == error_mark_node)
29139 cp_type = NULL_TREE;
29140 /* We need to skip to the closing parenthesis as
29141 cp_parser_type_id() does not seem to do it for
29142 us. */
29143 cp_parser_skip_to_closing_parenthesis (parser,
29144 /*recovering=*/true,
29145 /*or_comma=*/false,
29146 /*consume_paren=*/false);
29150 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29151 type_name = build_tree_list (proto_quals, cp_type);
29154 return type_name;
29157 /* Check to see if TYPE refers to an Objective-C selector name. */
29159 static bool
29160 cp_parser_objc_selector_p (enum cpp_ttype type)
29162 return (type == CPP_NAME || type == CPP_KEYWORD
29163 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29164 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29165 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29166 || type == CPP_XOR || type == CPP_XOR_EQ);
29169 /* Parse an Objective-C selector. */
29171 static tree
29172 cp_parser_objc_selector (cp_parser* parser)
29174 cp_token *token = cp_lexer_consume_token (parser->lexer);
29176 if (!cp_parser_objc_selector_p (token->type))
29178 error_at (token->location, "invalid Objective-C++ selector name");
29179 return error_mark_node;
29182 /* C++ operator names are allowed to appear in ObjC selectors. */
29183 switch (token->type)
29185 case CPP_AND_AND: return get_identifier ("and");
29186 case CPP_AND_EQ: return get_identifier ("and_eq");
29187 case CPP_AND: return get_identifier ("bitand");
29188 case CPP_OR: return get_identifier ("bitor");
29189 case CPP_COMPL: return get_identifier ("compl");
29190 case CPP_NOT: return get_identifier ("not");
29191 case CPP_NOT_EQ: return get_identifier ("not_eq");
29192 case CPP_OR_OR: return get_identifier ("or");
29193 case CPP_OR_EQ: return get_identifier ("or_eq");
29194 case CPP_XOR: return get_identifier ("xor");
29195 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29196 default: return token->u.value;
29200 /* Parse an Objective-C params list. */
29202 static tree
29203 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29205 tree params = NULL_TREE;
29206 bool maybe_unary_selector_p = true;
29207 cp_token *token = cp_lexer_peek_token (parser->lexer);
29209 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29211 tree selector = NULL_TREE, type_name, identifier;
29212 tree parm_attr = NULL_TREE;
29214 if (token->keyword == RID_ATTRIBUTE)
29215 break;
29217 if (token->type != CPP_COLON)
29218 selector = cp_parser_objc_selector (parser);
29220 /* Detect if we have a unary selector. */
29221 if (maybe_unary_selector_p
29222 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29224 params = selector; /* Might be followed by attributes. */
29225 break;
29228 maybe_unary_selector_p = false;
29229 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29231 /* Something went quite wrong. There should be a colon
29232 here, but there is not. Stop parsing parameters. */
29233 break;
29235 type_name = cp_parser_objc_typename (parser);
29236 /* New ObjC allows attributes on parameters too. */
29237 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
29238 parm_attr = cp_parser_attributes_opt (parser);
29239 identifier = cp_parser_identifier (parser);
29241 params
29242 = chainon (params,
29243 objc_build_keyword_decl (selector,
29244 type_name,
29245 identifier,
29246 parm_attr));
29248 token = cp_lexer_peek_token (parser->lexer);
29251 if (params == NULL_TREE)
29253 cp_parser_error (parser, "objective-c++ method declaration is expected");
29254 return error_mark_node;
29257 /* We allow tail attributes for the method. */
29258 if (token->keyword == RID_ATTRIBUTE)
29260 *attributes = cp_parser_attributes_opt (parser);
29261 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29262 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29263 return params;
29264 cp_parser_error (parser,
29265 "method attributes must be specified at the end");
29266 return error_mark_node;
29269 if (params == NULL_TREE)
29271 cp_parser_error (parser, "objective-c++ method declaration is expected");
29272 return error_mark_node;
29274 return params;
29277 /* Parse the non-keyword Objective-C params. */
29279 static tree
29280 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
29281 tree* attributes)
29283 tree params = make_node (TREE_LIST);
29284 cp_token *token = cp_lexer_peek_token (parser->lexer);
29285 *ellipsisp = false; /* Initially, assume no ellipsis. */
29287 while (token->type == CPP_COMMA)
29289 cp_parameter_declarator *parmdecl;
29290 tree parm;
29292 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29293 token = cp_lexer_peek_token (parser->lexer);
29295 if (token->type == CPP_ELLIPSIS)
29297 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
29298 *ellipsisp = true;
29299 token = cp_lexer_peek_token (parser->lexer);
29300 break;
29303 /* TODO: parse attributes for tail parameters. */
29304 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
29305 parm = grokdeclarator (parmdecl->declarator,
29306 &parmdecl->decl_specifiers,
29307 PARM, /*initialized=*/0,
29308 /*attrlist=*/NULL);
29310 chainon (params, build_tree_list (NULL_TREE, parm));
29311 token = cp_lexer_peek_token (parser->lexer);
29314 /* We allow tail attributes for the method. */
29315 if (token->keyword == RID_ATTRIBUTE)
29317 if (*attributes == NULL_TREE)
29319 *attributes = cp_parser_attributes_opt (parser);
29320 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29321 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29322 return params;
29324 else
29325 /* We have an error, but parse the attributes, so that we can
29326 carry on. */
29327 *attributes = cp_parser_attributes_opt (parser);
29329 cp_parser_error (parser,
29330 "method attributes must be specified at the end");
29331 return error_mark_node;
29334 return params;
29337 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29339 static void
29340 cp_parser_objc_interstitial_code (cp_parser* parser)
29342 cp_token *token = cp_lexer_peek_token (parser->lexer);
29344 /* If the next token is `extern' and the following token is a string
29345 literal, then we have a linkage specification. */
29346 if (token->keyword == RID_EXTERN
29347 && cp_parser_is_pure_string_literal
29348 (cp_lexer_peek_nth_token (parser->lexer, 2)))
29349 cp_parser_linkage_specification (parser);
29350 /* Handle #pragma, if any. */
29351 else if (token->type == CPP_PRAGMA)
29352 cp_parser_pragma (parser, pragma_objc_icode, NULL);
29353 /* Allow stray semicolons. */
29354 else if (token->type == CPP_SEMICOLON)
29355 cp_lexer_consume_token (parser->lexer);
29356 /* Mark methods as optional or required, when building protocols. */
29357 else if (token->keyword == RID_AT_OPTIONAL)
29359 cp_lexer_consume_token (parser->lexer);
29360 objc_set_method_opt (true);
29362 else if (token->keyword == RID_AT_REQUIRED)
29364 cp_lexer_consume_token (parser->lexer);
29365 objc_set_method_opt (false);
29367 else if (token->keyword == RID_NAMESPACE)
29368 cp_parser_namespace_definition (parser);
29369 /* Other stray characters must generate errors. */
29370 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
29372 cp_lexer_consume_token (parser->lexer);
29373 error ("stray %qs between Objective-C++ methods",
29374 token->type == CPP_OPEN_BRACE ? "{" : "}");
29376 /* Finally, try to parse a block-declaration, or a function-definition. */
29377 else
29378 cp_parser_block_declaration (parser, /*statement_p=*/false);
29381 /* Parse a method signature. */
29383 static tree
29384 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
29386 tree rettype, kwdparms, optparms;
29387 bool ellipsis = false;
29388 bool is_class_method;
29390 is_class_method = cp_parser_objc_method_type (parser);
29391 rettype = cp_parser_objc_typename (parser);
29392 *attributes = NULL_TREE;
29393 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
29394 if (kwdparms == error_mark_node)
29395 return error_mark_node;
29396 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
29397 if (optparms == error_mark_node)
29398 return error_mark_node;
29400 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
29403 static bool
29404 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
29406 tree tattr;
29407 cp_lexer_save_tokens (parser->lexer);
29408 tattr = cp_parser_attributes_opt (parser);
29409 gcc_assert (tattr) ;
29411 /* If the attributes are followed by a method introducer, this is not allowed.
29412 Dump the attributes and flag the situation. */
29413 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
29414 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
29415 return true;
29417 /* Otherwise, the attributes introduce some interstitial code, possibly so
29418 rewind to allow that check. */
29419 cp_lexer_rollback_tokens (parser->lexer);
29420 return false;
29423 /* Parse an Objective-C method prototype list. */
29425 static void
29426 cp_parser_objc_method_prototype_list (cp_parser* parser)
29428 cp_token *token = cp_lexer_peek_token (parser->lexer);
29430 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29432 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29434 tree attributes, sig;
29435 bool is_class_method;
29436 if (token->type == CPP_PLUS)
29437 is_class_method = true;
29438 else
29439 is_class_method = false;
29440 sig = cp_parser_objc_method_signature (parser, &attributes);
29441 if (sig == error_mark_node)
29443 cp_parser_skip_to_end_of_block_or_statement (parser);
29444 token = cp_lexer_peek_token (parser->lexer);
29445 continue;
29447 objc_add_method_declaration (is_class_method, sig, attributes);
29448 cp_parser_consume_semicolon_at_end_of_statement (parser);
29450 else if (token->keyword == RID_AT_PROPERTY)
29451 cp_parser_objc_at_property_declaration (parser);
29452 else if (token->keyword == RID_ATTRIBUTE
29453 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29454 warning_at (cp_lexer_peek_token (parser->lexer)->location,
29455 OPT_Wattributes,
29456 "prefix attributes are ignored for methods");
29457 else
29458 /* Allow for interspersed non-ObjC++ code. */
29459 cp_parser_objc_interstitial_code (parser);
29461 token = cp_lexer_peek_token (parser->lexer);
29464 if (token->type != CPP_EOF)
29465 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29466 else
29467 cp_parser_error (parser, "expected %<@end%>");
29469 objc_finish_interface ();
29472 /* Parse an Objective-C method definition list. */
29474 static void
29475 cp_parser_objc_method_definition_list (cp_parser* parser)
29477 cp_token *token = cp_lexer_peek_token (parser->lexer);
29479 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29481 tree meth;
29483 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29485 cp_token *ptk;
29486 tree sig, attribute;
29487 bool is_class_method;
29488 if (token->type == CPP_PLUS)
29489 is_class_method = true;
29490 else
29491 is_class_method = false;
29492 push_deferring_access_checks (dk_deferred);
29493 sig = cp_parser_objc_method_signature (parser, &attribute);
29494 if (sig == error_mark_node)
29496 cp_parser_skip_to_end_of_block_or_statement (parser);
29497 token = cp_lexer_peek_token (parser->lexer);
29498 continue;
29500 objc_start_method_definition (is_class_method, sig, attribute,
29501 NULL_TREE);
29503 /* For historical reasons, we accept an optional semicolon. */
29504 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29505 cp_lexer_consume_token (parser->lexer);
29507 ptk = cp_lexer_peek_token (parser->lexer);
29508 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
29509 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
29511 perform_deferred_access_checks (tf_warning_or_error);
29512 stop_deferring_access_checks ();
29513 meth = cp_parser_function_definition_after_declarator (parser,
29514 false);
29515 pop_deferring_access_checks ();
29516 objc_finish_method_definition (meth);
29519 /* The following case will be removed once @synthesize is
29520 completely implemented. */
29521 else if (token->keyword == RID_AT_PROPERTY)
29522 cp_parser_objc_at_property_declaration (parser);
29523 else if (token->keyword == RID_AT_SYNTHESIZE)
29524 cp_parser_objc_at_synthesize_declaration (parser);
29525 else if (token->keyword == RID_AT_DYNAMIC)
29526 cp_parser_objc_at_dynamic_declaration (parser);
29527 else if (token->keyword == RID_ATTRIBUTE
29528 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29529 warning_at (token->location, OPT_Wattributes,
29530 "prefix attributes are ignored for methods");
29531 else
29532 /* Allow for interspersed non-ObjC++ code. */
29533 cp_parser_objc_interstitial_code (parser);
29535 token = cp_lexer_peek_token (parser->lexer);
29538 if (token->type != CPP_EOF)
29539 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29540 else
29541 cp_parser_error (parser, "expected %<@end%>");
29543 objc_finish_implementation ();
29546 /* Parse Objective-C ivars. */
29548 static void
29549 cp_parser_objc_class_ivars (cp_parser* parser)
29551 cp_token *token = cp_lexer_peek_token (parser->lexer);
29553 if (token->type != CPP_OPEN_BRACE)
29554 return; /* No ivars specified. */
29556 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
29557 token = cp_lexer_peek_token (parser->lexer);
29559 while (token->type != CPP_CLOSE_BRACE
29560 && token->keyword != RID_AT_END && token->type != CPP_EOF)
29562 cp_decl_specifier_seq declspecs;
29563 int decl_class_or_enum_p;
29564 tree prefix_attributes;
29566 cp_parser_objc_visibility_spec (parser);
29568 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29569 break;
29571 cp_parser_decl_specifier_seq (parser,
29572 CP_PARSER_FLAGS_OPTIONAL,
29573 &declspecs,
29574 &decl_class_or_enum_p);
29576 /* auto, register, static, extern, mutable. */
29577 if (declspecs.storage_class != sc_none)
29579 cp_parser_error (parser, "invalid type for instance variable");
29580 declspecs.storage_class = sc_none;
29583 /* thread_local. */
29584 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
29586 cp_parser_error (parser, "invalid type for instance variable");
29587 declspecs.locations[ds_thread] = 0;
29590 /* typedef. */
29591 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
29593 cp_parser_error (parser, "invalid type for instance variable");
29594 declspecs.locations[ds_typedef] = 0;
29597 prefix_attributes = declspecs.attributes;
29598 declspecs.attributes = NULL_TREE;
29600 /* Keep going until we hit the `;' at the end of the
29601 declaration. */
29602 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29604 tree width = NULL_TREE, attributes, first_attribute, decl;
29605 cp_declarator *declarator = NULL;
29606 int ctor_dtor_or_conv_p;
29608 /* Check for a (possibly unnamed) bitfield declaration. */
29609 token = cp_lexer_peek_token (parser->lexer);
29610 if (token->type == CPP_COLON)
29611 goto eat_colon;
29613 if (token->type == CPP_NAME
29614 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
29615 == CPP_COLON))
29617 /* Get the name of the bitfield. */
29618 declarator = make_id_declarator (NULL_TREE,
29619 cp_parser_identifier (parser),
29620 sfk_none);
29622 eat_colon:
29623 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
29624 /* Get the width of the bitfield. */
29625 width
29626 = cp_parser_constant_expression (parser);
29628 else
29630 /* Parse the declarator. */
29631 declarator
29632 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29633 &ctor_dtor_or_conv_p,
29634 /*parenthesized_p=*/NULL,
29635 /*member_p=*/false,
29636 /*friend_p=*/false);
29639 /* Look for attributes that apply to the ivar. */
29640 attributes = cp_parser_attributes_opt (parser);
29641 /* Remember which attributes are prefix attributes and
29642 which are not. */
29643 first_attribute = attributes;
29644 /* Combine the attributes. */
29645 attributes = chainon (prefix_attributes, attributes);
29647 if (width)
29648 /* Create the bitfield declaration. */
29649 decl = grokbitfield (declarator, &declspecs,
29650 width,
29651 attributes);
29652 else
29653 decl = grokfield (declarator, &declspecs,
29654 NULL_TREE, /*init_const_expr_p=*/false,
29655 NULL_TREE, attributes);
29657 /* Add the instance variable. */
29658 if (decl != error_mark_node && decl != NULL_TREE)
29659 objc_add_instance_variable (decl);
29661 /* Reset PREFIX_ATTRIBUTES. */
29662 while (attributes && TREE_CHAIN (attributes) != first_attribute)
29663 attributes = TREE_CHAIN (attributes);
29664 if (attributes)
29665 TREE_CHAIN (attributes) = NULL_TREE;
29667 token = cp_lexer_peek_token (parser->lexer);
29669 if (token->type == CPP_COMMA)
29671 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29672 continue;
29674 break;
29677 cp_parser_consume_semicolon_at_end_of_statement (parser);
29678 token = cp_lexer_peek_token (parser->lexer);
29681 if (token->keyword == RID_AT_END)
29682 cp_parser_error (parser, "expected %<}%>");
29684 /* Do not consume the RID_AT_END, so it will be read again as terminating
29685 the @interface of @implementation. */
29686 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
29687 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
29689 /* For historical reasons, we accept an optional semicolon. */
29690 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29691 cp_lexer_consume_token (parser->lexer);
29694 /* Parse an Objective-C protocol declaration. */
29696 static void
29697 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
29699 tree proto, protorefs;
29700 cp_token *tok;
29702 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29703 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
29705 tok = cp_lexer_peek_token (parser->lexer);
29706 error_at (tok->location, "identifier expected after %<@protocol%>");
29707 cp_parser_consume_semicolon_at_end_of_statement (parser);
29708 return;
29711 /* See if we have a forward declaration or a definition. */
29712 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
29714 /* Try a forward declaration first. */
29715 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
29717 while (true)
29719 tree id;
29721 id = cp_parser_identifier (parser);
29722 if (id == error_mark_node)
29723 break;
29725 objc_declare_protocol (id, attributes);
29727 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29728 cp_lexer_consume_token (parser->lexer);
29729 else
29730 break;
29732 cp_parser_consume_semicolon_at_end_of_statement (parser);
29735 /* Ok, we got a full-fledged definition (or at least should). */
29736 else
29738 proto = cp_parser_identifier (parser);
29739 protorefs = cp_parser_objc_protocol_refs_opt (parser);
29740 objc_start_protocol (proto, protorefs, attributes);
29741 cp_parser_objc_method_prototype_list (parser);
29745 /* Parse an Objective-C superclass or category. */
29747 static void
29748 cp_parser_objc_superclass_or_category (cp_parser *parser,
29749 bool iface_p,
29750 tree *super,
29751 tree *categ, bool *is_class_extension)
29753 cp_token *next = cp_lexer_peek_token (parser->lexer);
29755 *super = *categ = NULL_TREE;
29756 *is_class_extension = false;
29757 if (next->type == CPP_COLON)
29759 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
29760 *super = cp_parser_identifier (parser);
29762 else if (next->type == CPP_OPEN_PAREN)
29764 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
29766 /* If there is no category name, and this is an @interface, we
29767 have a class extension. */
29768 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29770 *categ = NULL_TREE;
29771 *is_class_extension = true;
29773 else
29774 *categ = cp_parser_identifier (parser);
29776 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29780 /* Parse an Objective-C class interface. */
29782 static void
29783 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
29785 tree name, super, categ, protos;
29786 bool is_class_extension;
29788 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
29789 name = cp_parser_identifier (parser);
29790 if (name == error_mark_node)
29792 /* It's hard to recover because even if valid @interface stuff
29793 is to follow, we can't compile it (or validate it) if we
29794 don't even know which class it refers to. Let's assume this
29795 was a stray '@interface' token in the stream and skip it.
29797 return;
29799 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
29800 &is_class_extension);
29801 protos = cp_parser_objc_protocol_refs_opt (parser);
29803 /* We have either a class or a category on our hands. */
29804 if (categ || is_class_extension)
29805 objc_start_category_interface (name, categ, protos, attributes);
29806 else
29808 objc_start_class_interface (name, super, protos, attributes);
29809 /* Handle instance variable declarations, if any. */
29810 cp_parser_objc_class_ivars (parser);
29811 objc_continue_interface ();
29814 cp_parser_objc_method_prototype_list (parser);
29817 /* Parse an Objective-C class implementation. */
29819 static void
29820 cp_parser_objc_class_implementation (cp_parser* parser)
29822 tree name, super, categ;
29823 bool is_class_extension;
29825 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
29826 name = cp_parser_identifier (parser);
29827 if (name == error_mark_node)
29829 /* It's hard to recover because even if valid @implementation
29830 stuff is to follow, we can't compile it (or validate it) if
29831 we don't even know which class it refers to. Let's assume
29832 this was a stray '@implementation' token in the stream and
29833 skip it.
29835 return;
29837 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
29838 &is_class_extension);
29840 /* We have either a class or a category on our hands. */
29841 if (categ)
29842 objc_start_category_implementation (name, categ);
29843 else
29845 objc_start_class_implementation (name, super);
29846 /* Handle instance variable declarations, if any. */
29847 cp_parser_objc_class_ivars (parser);
29848 objc_continue_implementation ();
29851 cp_parser_objc_method_definition_list (parser);
29854 /* Consume the @end token and finish off the implementation. */
29856 static void
29857 cp_parser_objc_end_implementation (cp_parser* parser)
29859 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29860 objc_finish_implementation ();
29863 /* Parse an Objective-C declaration. */
29865 static void
29866 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
29868 /* Try to figure out what kind of declaration is present. */
29869 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29871 if (attributes)
29872 switch (kwd->keyword)
29874 case RID_AT_ALIAS:
29875 case RID_AT_CLASS:
29876 case RID_AT_END:
29877 error_at (kwd->location, "attributes may not be specified before"
29878 " the %<@%D%> Objective-C++ keyword",
29879 kwd->u.value);
29880 attributes = NULL;
29881 break;
29882 case RID_AT_IMPLEMENTATION:
29883 warning_at (kwd->location, OPT_Wattributes,
29884 "prefix attributes are ignored before %<@%D%>",
29885 kwd->u.value);
29886 attributes = NULL;
29887 default:
29888 break;
29891 switch (kwd->keyword)
29893 case RID_AT_ALIAS:
29894 cp_parser_objc_alias_declaration (parser);
29895 break;
29896 case RID_AT_CLASS:
29897 cp_parser_objc_class_declaration (parser);
29898 break;
29899 case RID_AT_PROTOCOL:
29900 cp_parser_objc_protocol_declaration (parser, attributes);
29901 break;
29902 case RID_AT_INTERFACE:
29903 cp_parser_objc_class_interface (parser, attributes);
29904 break;
29905 case RID_AT_IMPLEMENTATION:
29906 cp_parser_objc_class_implementation (parser);
29907 break;
29908 case RID_AT_END:
29909 cp_parser_objc_end_implementation (parser);
29910 break;
29911 default:
29912 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
29913 kwd->u.value);
29914 cp_parser_skip_to_end_of_block_or_statement (parser);
29918 /* Parse an Objective-C try-catch-finally statement.
29920 objc-try-catch-finally-stmt:
29921 @try compound-statement objc-catch-clause-seq [opt]
29922 objc-finally-clause [opt]
29924 objc-catch-clause-seq:
29925 objc-catch-clause objc-catch-clause-seq [opt]
29927 objc-catch-clause:
29928 @catch ( objc-exception-declaration ) compound-statement
29930 objc-finally-clause:
29931 @finally compound-statement
29933 objc-exception-declaration:
29934 parameter-declaration
29935 '...'
29937 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
29939 Returns NULL_TREE.
29941 PS: This function is identical to c_parser_objc_try_catch_finally_statement
29942 for C. Keep them in sync. */
29944 static tree
29945 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
29947 location_t location;
29948 tree stmt;
29950 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
29951 location = cp_lexer_peek_token (parser->lexer)->location;
29952 objc_maybe_warn_exceptions (location);
29953 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
29954 node, lest it get absorbed into the surrounding block. */
29955 stmt = push_stmt_list ();
29956 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29957 objc_begin_try_stmt (location, pop_stmt_list (stmt));
29959 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
29961 cp_parameter_declarator *parm;
29962 tree parameter_declaration = error_mark_node;
29963 bool seen_open_paren = false;
29965 cp_lexer_consume_token (parser->lexer);
29966 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29967 seen_open_paren = true;
29968 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29970 /* We have "@catch (...)" (where the '...' are literally
29971 what is in the code). Skip the '...'.
29972 parameter_declaration is set to NULL_TREE, and
29973 objc_being_catch_clauses() knows that that means
29974 '...'. */
29975 cp_lexer_consume_token (parser->lexer);
29976 parameter_declaration = NULL_TREE;
29978 else
29980 /* We have "@catch (NSException *exception)" or something
29981 like that. Parse the parameter declaration. */
29982 parm = cp_parser_parameter_declaration (parser, false, NULL);
29983 if (parm == NULL)
29984 parameter_declaration = error_mark_node;
29985 else
29986 parameter_declaration = grokdeclarator (parm->declarator,
29987 &parm->decl_specifiers,
29988 PARM, /*initialized=*/0,
29989 /*attrlist=*/NULL);
29991 if (seen_open_paren)
29992 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29993 else
29995 /* If there was no open parenthesis, we are recovering from
29996 an error, and we are trying to figure out what mistake
29997 the user has made. */
29999 /* If there is an immediate closing parenthesis, the user
30000 probably forgot the opening one (ie, they typed "@catch
30001 NSException *e)". Parse the closing parenthesis and keep
30002 going. */
30003 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30004 cp_lexer_consume_token (parser->lexer);
30006 /* If these is no immediate closing parenthesis, the user
30007 probably doesn't know that parenthesis are required at
30008 all (ie, they typed "@catch NSException *e"). So, just
30009 forget about the closing parenthesis and keep going. */
30011 objc_begin_catch_clause (parameter_declaration);
30012 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30013 objc_finish_catch_clause ();
30015 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30017 cp_lexer_consume_token (parser->lexer);
30018 location = cp_lexer_peek_token (parser->lexer)->location;
30019 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30020 node, lest it get absorbed into the surrounding block. */
30021 stmt = push_stmt_list ();
30022 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30023 objc_build_finally_clause (location, pop_stmt_list (stmt));
30026 return objc_finish_try_stmt ();
30029 /* Parse an Objective-C synchronized statement.
30031 objc-synchronized-stmt:
30032 @synchronized ( expression ) compound-statement
30034 Returns NULL_TREE. */
30036 static tree
30037 cp_parser_objc_synchronized_statement (cp_parser *parser)
30039 location_t location;
30040 tree lock, stmt;
30042 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30044 location = cp_lexer_peek_token (parser->lexer)->location;
30045 objc_maybe_warn_exceptions (location);
30046 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
30047 lock = cp_parser_expression (parser);
30048 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
30050 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30051 node, lest it get absorbed into the surrounding block. */
30052 stmt = push_stmt_list ();
30053 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30055 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30058 /* Parse an Objective-C throw statement.
30060 objc-throw-stmt:
30061 @throw assignment-expression [opt] ;
30063 Returns a constructed '@throw' statement. */
30065 static tree
30066 cp_parser_objc_throw_statement (cp_parser *parser)
30068 tree expr = NULL_TREE;
30069 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30071 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30073 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30074 expr = cp_parser_expression (parser);
30076 cp_parser_consume_semicolon_at_end_of_statement (parser);
30078 return objc_build_throw_stmt (loc, expr);
30081 /* Parse an Objective-C statement. */
30083 static tree
30084 cp_parser_objc_statement (cp_parser * parser)
30086 /* Try to figure out what kind of declaration is present. */
30087 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30089 switch (kwd->keyword)
30091 case RID_AT_TRY:
30092 return cp_parser_objc_try_catch_finally_statement (parser);
30093 case RID_AT_SYNCHRONIZED:
30094 return cp_parser_objc_synchronized_statement (parser);
30095 case RID_AT_THROW:
30096 return cp_parser_objc_throw_statement (parser);
30097 default:
30098 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30099 kwd->u.value);
30100 cp_parser_skip_to_end_of_block_or_statement (parser);
30103 return error_mark_node;
30106 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30107 look ahead to see if an objc keyword follows the attributes. This
30108 is to detect the use of prefix attributes on ObjC @interface and
30109 @protocol. */
30111 static bool
30112 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30114 cp_lexer_save_tokens (parser->lexer);
30115 *attrib = cp_parser_attributes_opt (parser);
30116 gcc_assert (*attrib);
30117 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30119 cp_lexer_commit_tokens (parser->lexer);
30120 return true;
30122 cp_lexer_rollback_tokens (parser->lexer);
30123 return false;
30126 /* This routine is a minimal replacement for
30127 c_parser_struct_declaration () used when parsing the list of
30128 types/names or ObjC++ properties. For example, when parsing the
30129 code
30131 @property (readonly) int a, b, c;
30133 this function is responsible for parsing "int a, int b, int c" and
30134 returning the declarations as CHAIN of DECLs.
30136 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30137 similar parsing. */
30138 static tree
30139 cp_parser_objc_struct_declaration (cp_parser *parser)
30141 tree decls = NULL_TREE;
30142 cp_decl_specifier_seq declspecs;
30143 int decl_class_or_enum_p;
30144 tree prefix_attributes;
30146 cp_parser_decl_specifier_seq (parser,
30147 CP_PARSER_FLAGS_NONE,
30148 &declspecs,
30149 &decl_class_or_enum_p);
30151 if (declspecs.type == error_mark_node)
30152 return error_mark_node;
30154 /* auto, register, static, extern, mutable. */
30155 if (declspecs.storage_class != sc_none)
30157 cp_parser_error (parser, "invalid type for property");
30158 declspecs.storage_class = sc_none;
30161 /* thread_local. */
30162 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30164 cp_parser_error (parser, "invalid type for property");
30165 declspecs.locations[ds_thread] = 0;
30168 /* typedef. */
30169 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30171 cp_parser_error (parser, "invalid type for property");
30172 declspecs.locations[ds_typedef] = 0;
30175 prefix_attributes = declspecs.attributes;
30176 declspecs.attributes = NULL_TREE;
30178 /* Keep going until we hit the `;' at the end of the declaration. */
30179 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30181 tree attributes, first_attribute, decl;
30182 cp_declarator *declarator;
30183 cp_token *token;
30185 /* Parse the declarator. */
30186 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30187 NULL, NULL, false, false);
30189 /* Look for attributes that apply to the ivar. */
30190 attributes = cp_parser_attributes_opt (parser);
30191 /* Remember which attributes are prefix attributes and
30192 which are not. */
30193 first_attribute = attributes;
30194 /* Combine the attributes. */
30195 attributes = chainon (prefix_attributes, attributes);
30197 decl = grokfield (declarator, &declspecs,
30198 NULL_TREE, /*init_const_expr_p=*/false,
30199 NULL_TREE, attributes);
30201 if (decl == error_mark_node || decl == NULL_TREE)
30202 return error_mark_node;
30204 /* Reset PREFIX_ATTRIBUTES. */
30205 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30206 attributes = TREE_CHAIN (attributes);
30207 if (attributes)
30208 TREE_CHAIN (attributes) = NULL_TREE;
30210 DECL_CHAIN (decl) = decls;
30211 decls = decl;
30213 token = cp_lexer_peek_token (parser->lexer);
30214 if (token->type == CPP_COMMA)
30216 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30217 continue;
30219 else
30220 break;
30222 return decls;
30225 /* Parse an Objective-C @property declaration. The syntax is:
30227 objc-property-declaration:
30228 '@property' objc-property-attributes[opt] struct-declaration ;
30230 objc-property-attributes:
30231 '(' objc-property-attribute-list ')'
30233 objc-property-attribute-list:
30234 objc-property-attribute
30235 objc-property-attribute-list, objc-property-attribute
30237 objc-property-attribute
30238 'getter' = identifier
30239 'setter' = identifier
30240 'readonly'
30241 'readwrite'
30242 'assign'
30243 'retain'
30244 'copy'
30245 'nonatomic'
30247 For example:
30248 @property NSString *name;
30249 @property (readonly) id object;
30250 @property (retain, nonatomic, getter=getTheName) id name;
30251 @property int a, b, c;
30253 PS: This function is identical to
30254 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30255 static void
30256 cp_parser_objc_at_property_declaration (cp_parser *parser)
30258 /* The following variables hold the attributes of the properties as
30259 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30260 seen. When we see an attribute, we set them to 'true' (if they
30261 are boolean properties) or to the identifier (if they have an
30262 argument, ie, for getter and setter). Note that here we only
30263 parse the list of attributes, check the syntax and accumulate the
30264 attributes that we find. objc_add_property_declaration() will
30265 then process the information. */
30266 bool property_assign = false;
30267 bool property_copy = false;
30268 tree property_getter_ident = NULL_TREE;
30269 bool property_nonatomic = false;
30270 bool property_readonly = false;
30271 bool property_readwrite = false;
30272 bool property_retain = false;
30273 tree property_setter_ident = NULL_TREE;
30275 /* 'properties' is the list of properties that we read. Usually a
30276 single one, but maybe more (eg, in "@property int a, b, c;" there
30277 are three). */
30278 tree properties;
30279 location_t loc;
30281 loc = cp_lexer_peek_token (parser->lexer)->location;
30283 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
30285 /* Parse the optional attribute list... */
30286 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30288 /* Eat the '('. */
30289 cp_lexer_consume_token (parser->lexer);
30291 while (true)
30293 bool syntax_error = false;
30294 cp_token *token = cp_lexer_peek_token (parser->lexer);
30295 enum rid keyword;
30297 if (token->type != CPP_NAME)
30299 cp_parser_error (parser, "expected identifier");
30300 break;
30302 keyword = C_RID_CODE (token->u.value);
30303 cp_lexer_consume_token (parser->lexer);
30304 switch (keyword)
30306 case RID_ASSIGN: property_assign = true; break;
30307 case RID_COPY: property_copy = true; break;
30308 case RID_NONATOMIC: property_nonatomic = true; break;
30309 case RID_READONLY: property_readonly = true; break;
30310 case RID_READWRITE: property_readwrite = true; break;
30311 case RID_RETAIN: property_retain = true; break;
30313 case RID_GETTER:
30314 case RID_SETTER:
30315 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30317 if (keyword == RID_GETTER)
30318 cp_parser_error (parser,
30319 "missing %<=%> (after %<getter%> attribute)");
30320 else
30321 cp_parser_error (parser,
30322 "missing %<=%> (after %<setter%> attribute)");
30323 syntax_error = true;
30324 break;
30326 cp_lexer_consume_token (parser->lexer); /* eat the = */
30327 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
30329 cp_parser_error (parser, "expected identifier");
30330 syntax_error = true;
30331 break;
30333 if (keyword == RID_SETTER)
30335 if (property_setter_ident != NULL_TREE)
30337 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
30338 cp_lexer_consume_token (parser->lexer);
30340 else
30341 property_setter_ident = cp_parser_objc_selector (parser);
30342 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30343 cp_parser_error (parser, "setter name must terminate with %<:%>");
30344 else
30345 cp_lexer_consume_token (parser->lexer);
30347 else
30349 if (property_getter_ident != NULL_TREE)
30351 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
30352 cp_lexer_consume_token (parser->lexer);
30354 else
30355 property_getter_ident = cp_parser_objc_selector (parser);
30357 break;
30358 default:
30359 cp_parser_error (parser, "unknown property attribute");
30360 syntax_error = true;
30361 break;
30364 if (syntax_error)
30365 break;
30367 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30368 cp_lexer_consume_token (parser->lexer);
30369 else
30370 break;
30373 /* FIXME: "@property (setter, assign);" will generate a spurious
30374 "error: expected ‘)’ before ‘,’ token". This is because
30375 cp_parser_require, unlike the C counterpart, will produce an
30376 error even if we are in error recovery. */
30377 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30379 cp_parser_skip_to_closing_parenthesis (parser,
30380 /*recovering=*/true,
30381 /*or_comma=*/false,
30382 /*consume_paren=*/true);
30386 /* ... and the property declaration(s). */
30387 properties = cp_parser_objc_struct_declaration (parser);
30389 if (properties == error_mark_node)
30391 cp_parser_skip_to_end_of_statement (parser);
30392 /* If the next token is now a `;', consume it. */
30393 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30394 cp_lexer_consume_token (parser->lexer);
30395 return;
30398 if (properties == NULL_TREE)
30399 cp_parser_error (parser, "expected identifier");
30400 else
30402 /* Comma-separated properties are chained together in
30403 reverse order; add them one by one. */
30404 properties = nreverse (properties);
30406 for (; properties; properties = TREE_CHAIN (properties))
30407 objc_add_property_declaration (loc, copy_node (properties),
30408 property_readonly, property_readwrite,
30409 property_assign, property_retain,
30410 property_copy, property_nonatomic,
30411 property_getter_ident, property_setter_ident);
30414 cp_parser_consume_semicolon_at_end_of_statement (parser);
30417 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
30419 objc-synthesize-declaration:
30420 @synthesize objc-synthesize-identifier-list ;
30422 objc-synthesize-identifier-list:
30423 objc-synthesize-identifier
30424 objc-synthesize-identifier-list, objc-synthesize-identifier
30426 objc-synthesize-identifier
30427 identifier
30428 identifier = identifier
30430 For example:
30431 @synthesize MyProperty;
30432 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
30434 PS: This function is identical to c_parser_objc_at_synthesize_declaration
30435 for C. Keep them in sync.
30437 static void
30438 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
30440 tree list = NULL_TREE;
30441 location_t loc;
30442 loc = cp_lexer_peek_token (parser->lexer)->location;
30444 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
30445 while (true)
30447 tree property, ivar;
30448 property = cp_parser_identifier (parser);
30449 if (property == error_mark_node)
30451 cp_parser_consume_semicolon_at_end_of_statement (parser);
30452 return;
30454 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
30456 cp_lexer_consume_token (parser->lexer);
30457 ivar = cp_parser_identifier (parser);
30458 if (ivar == error_mark_node)
30460 cp_parser_consume_semicolon_at_end_of_statement (parser);
30461 return;
30464 else
30465 ivar = NULL_TREE;
30466 list = chainon (list, build_tree_list (ivar, property));
30467 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30468 cp_lexer_consume_token (parser->lexer);
30469 else
30470 break;
30472 cp_parser_consume_semicolon_at_end_of_statement (parser);
30473 objc_add_synthesize_declaration (loc, list);
30476 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
30478 objc-dynamic-declaration:
30479 @dynamic identifier-list ;
30481 For example:
30482 @dynamic MyProperty;
30483 @dynamic MyProperty, AnotherProperty;
30485 PS: This function is identical to c_parser_objc_at_dynamic_declaration
30486 for C. Keep them in sync.
30488 static void
30489 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
30491 tree list = NULL_TREE;
30492 location_t loc;
30493 loc = cp_lexer_peek_token (parser->lexer)->location;
30495 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
30496 while (true)
30498 tree property;
30499 property = cp_parser_identifier (parser);
30500 if (property == error_mark_node)
30502 cp_parser_consume_semicolon_at_end_of_statement (parser);
30503 return;
30505 list = chainon (list, build_tree_list (NULL, property));
30506 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30507 cp_lexer_consume_token (parser->lexer);
30508 else
30509 break;
30511 cp_parser_consume_semicolon_at_end_of_statement (parser);
30512 objc_add_dynamic_declaration (loc, list);
30516 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
30518 /* Returns name of the next clause.
30519 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
30520 the token is not consumed. Otherwise appropriate pragma_omp_clause is
30521 returned and the token is consumed. */
30523 static pragma_omp_clause
30524 cp_parser_omp_clause_name (cp_parser *parser)
30526 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
30528 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
30529 result = PRAGMA_OACC_CLAUSE_AUTO;
30530 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
30531 result = PRAGMA_OMP_CLAUSE_IF;
30532 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
30533 result = PRAGMA_OMP_CLAUSE_DEFAULT;
30534 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
30535 result = PRAGMA_OACC_CLAUSE_DELETE;
30536 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
30537 result = PRAGMA_OMP_CLAUSE_PRIVATE;
30538 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30539 result = PRAGMA_OMP_CLAUSE_FOR;
30540 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30542 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30543 const char *p = IDENTIFIER_POINTER (id);
30545 switch (p[0])
30547 case 'a':
30548 if (!strcmp ("aligned", p))
30549 result = PRAGMA_OMP_CLAUSE_ALIGNED;
30550 else if (!strcmp ("async", p))
30551 result = PRAGMA_OACC_CLAUSE_ASYNC;
30552 break;
30553 case 'c':
30554 if (!strcmp ("collapse", p))
30555 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
30556 else if (!strcmp ("copy", p))
30557 result = PRAGMA_OACC_CLAUSE_COPY;
30558 else if (!strcmp ("copyin", p))
30559 result = PRAGMA_OMP_CLAUSE_COPYIN;
30560 else if (!strcmp ("copyout", p))
30561 result = PRAGMA_OACC_CLAUSE_COPYOUT;
30562 else if (!strcmp ("copyprivate", p))
30563 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
30564 else if (!strcmp ("create", p))
30565 result = PRAGMA_OACC_CLAUSE_CREATE;
30566 break;
30567 case 'd':
30568 if (!strcmp ("defaultmap", p))
30569 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
30570 else if (!strcmp ("depend", p))
30571 result = PRAGMA_OMP_CLAUSE_DEPEND;
30572 else if (!strcmp ("device", p))
30573 result = PRAGMA_OMP_CLAUSE_DEVICE;
30574 else if (!strcmp ("deviceptr", p))
30575 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
30576 else if (!strcmp ("device_resident", p))
30577 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
30578 else if (!strcmp ("dist_schedule", p))
30579 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
30580 break;
30581 case 'f':
30582 if (!strcmp ("final", p))
30583 result = PRAGMA_OMP_CLAUSE_FINAL;
30584 else if (!strcmp ("firstprivate", p))
30585 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
30586 else if (!strcmp ("from", p))
30587 result = PRAGMA_OMP_CLAUSE_FROM;
30588 break;
30589 case 'g':
30590 if (!strcmp ("gang", p))
30591 result = PRAGMA_OACC_CLAUSE_GANG;
30592 else if (!strcmp ("grainsize", p))
30593 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
30594 break;
30595 case 'h':
30596 if (!strcmp ("hint", p))
30597 result = PRAGMA_OMP_CLAUSE_HINT;
30598 else if (!strcmp ("host", p))
30599 result = PRAGMA_OACC_CLAUSE_HOST;
30600 break;
30601 case 'i':
30602 if (!strcmp ("inbranch", p))
30603 result = PRAGMA_OMP_CLAUSE_INBRANCH;
30604 else if (!strcmp ("independent", p))
30605 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
30606 else if (!strcmp ("is_device_ptr", p))
30607 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
30608 break;
30609 case 'l':
30610 if (!strcmp ("lastprivate", p))
30611 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
30612 else if (!strcmp ("linear", p))
30613 result = PRAGMA_OMP_CLAUSE_LINEAR;
30614 else if (!strcmp ("link", p))
30615 result = PRAGMA_OMP_CLAUSE_LINK;
30616 break;
30617 case 'm':
30618 if (!strcmp ("map", p))
30619 result = PRAGMA_OMP_CLAUSE_MAP;
30620 else if (!strcmp ("mergeable", p))
30621 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
30622 else if (flag_cilkplus && !strcmp ("mask", p))
30623 result = PRAGMA_CILK_CLAUSE_MASK;
30624 break;
30625 case 'n':
30626 if (!strcmp ("nogroup", p))
30627 result = PRAGMA_OMP_CLAUSE_NOGROUP;
30628 else if (!strcmp ("notinbranch", p))
30629 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
30630 else if (!strcmp ("nowait", p))
30631 result = PRAGMA_OMP_CLAUSE_NOWAIT;
30632 else if (flag_cilkplus && !strcmp ("nomask", p))
30633 result = PRAGMA_CILK_CLAUSE_NOMASK;
30634 else if (!strcmp ("num_gangs", p))
30635 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
30636 else if (!strcmp ("num_tasks", p))
30637 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
30638 else if (!strcmp ("num_teams", p))
30639 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
30640 else if (!strcmp ("num_threads", p))
30641 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
30642 else if (!strcmp ("num_workers", p))
30643 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
30644 break;
30645 case 'o':
30646 if (!strcmp ("ordered", p))
30647 result = PRAGMA_OMP_CLAUSE_ORDERED;
30648 break;
30649 case 'p':
30650 if (!strcmp ("parallel", p))
30651 result = PRAGMA_OMP_CLAUSE_PARALLEL;
30652 else if (!strcmp ("present", p))
30653 result = PRAGMA_OACC_CLAUSE_PRESENT;
30654 else if (!strcmp ("present_or_copy", p)
30655 || !strcmp ("pcopy", p))
30656 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
30657 else if (!strcmp ("present_or_copyin", p)
30658 || !strcmp ("pcopyin", p))
30659 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
30660 else if (!strcmp ("present_or_copyout", p)
30661 || !strcmp ("pcopyout", p))
30662 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
30663 else if (!strcmp ("present_or_create", p)
30664 || !strcmp ("pcreate", p))
30665 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
30666 else if (!strcmp ("priority", p))
30667 result = PRAGMA_OMP_CLAUSE_PRIORITY;
30668 else if (!strcmp ("proc_bind", p))
30669 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
30670 break;
30671 case 'r':
30672 if (!strcmp ("reduction", p))
30673 result = PRAGMA_OMP_CLAUSE_REDUCTION;
30674 break;
30675 case 's':
30676 if (!strcmp ("safelen", p))
30677 result = PRAGMA_OMP_CLAUSE_SAFELEN;
30678 else if (!strcmp ("schedule", p))
30679 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
30680 else if (!strcmp ("sections", p))
30681 result = PRAGMA_OMP_CLAUSE_SECTIONS;
30682 else if (!strcmp ("self", p))
30683 result = PRAGMA_OACC_CLAUSE_SELF;
30684 else if (!strcmp ("seq", p))
30685 result = PRAGMA_OACC_CLAUSE_SEQ;
30686 else if (!strcmp ("shared", p))
30687 result = PRAGMA_OMP_CLAUSE_SHARED;
30688 else if (!strcmp ("simd", p))
30689 result = PRAGMA_OMP_CLAUSE_SIMD;
30690 else if (!strcmp ("simdlen", p))
30691 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
30692 break;
30693 case 't':
30694 if (!strcmp ("taskgroup", p))
30695 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
30696 else if (!strcmp ("thread_limit", p))
30697 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
30698 else if (!strcmp ("threads", p))
30699 result = PRAGMA_OMP_CLAUSE_THREADS;
30700 else if (!strcmp ("tile", p))
30701 result = PRAGMA_OACC_CLAUSE_TILE;
30702 else if (!strcmp ("to", p))
30703 result = PRAGMA_OMP_CLAUSE_TO;
30704 break;
30705 case 'u':
30706 if (!strcmp ("uniform", p))
30707 result = PRAGMA_OMP_CLAUSE_UNIFORM;
30708 else if (!strcmp ("untied", p))
30709 result = PRAGMA_OMP_CLAUSE_UNTIED;
30710 else if (!strcmp ("use_device", p))
30711 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
30712 else if (!strcmp ("use_device_ptr", p))
30713 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
30714 break;
30715 case 'v':
30716 if (!strcmp ("vector", p))
30717 result = PRAGMA_OACC_CLAUSE_VECTOR;
30718 else if (!strcmp ("vector_length", p))
30719 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
30720 else if (flag_cilkplus && !strcmp ("vectorlength", p))
30721 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
30722 break;
30723 case 'w':
30724 if (!strcmp ("wait", p))
30725 result = PRAGMA_OACC_CLAUSE_WAIT;
30726 else if (!strcmp ("worker", p))
30727 result = PRAGMA_OACC_CLAUSE_WORKER;
30728 break;
30732 if (result != PRAGMA_OMP_CLAUSE_NONE)
30733 cp_lexer_consume_token (parser->lexer);
30735 return result;
30738 /* Validate that a clause of the given type does not already exist. */
30740 static void
30741 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
30742 const char *name, location_t location)
30744 tree c;
30746 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
30747 if (OMP_CLAUSE_CODE (c) == code)
30749 error_at (location, "too many %qs clauses", name);
30750 break;
30754 /* OpenMP 2.5:
30755 variable-list:
30756 identifier
30757 variable-list , identifier
30759 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
30760 colon). An opening parenthesis will have been consumed by the caller.
30762 If KIND is nonzero, create the appropriate node and install the decl
30763 in OMP_CLAUSE_DECL and add the node to the head of the list.
30765 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
30766 return the list created.
30768 COLON can be NULL if only closing parenthesis should end the list,
30769 or pointer to bool which will receive false if the list is terminated
30770 by closing parenthesis or true if the list is terminated by colon. */
30772 static tree
30773 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
30774 tree list, bool *colon)
30776 cp_token *token;
30777 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
30778 if (colon)
30780 parser->colon_corrects_to_scope_p = false;
30781 *colon = false;
30783 while (1)
30785 tree name, decl;
30787 token = cp_lexer_peek_token (parser->lexer);
30788 if (kind != 0
30789 && current_class_ptr
30790 && cp_parser_is_keyword (token, RID_THIS))
30792 decl = finish_this_expr ();
30793 if (TREE_CODE (decl) == NON_LVALUE_EXPR
30794 || CONVERT_EXPR_P (decl))
30795 decl = TREE_OPERAND (decl, 0);
30796 cp_lexer_consume_token (parser->lexer);
30798 else
30800 name = cp_parser_id_expression (parser, /*template_p=*/false,
30801 /*check_dependency_p=*/true,
30802 /*template_p=*/NULL,
30803 /*declarator_p=*/false,
30804 /*optional_p=*/false);
30805 if (name == error_mark_node)
30806 goto skip_comma;
30808 decl = cp_parser_lookup_name_simple (parser, name, token->location);
30809 if (decl == error_mark_node)
30810 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
30811 token->location);
30813 if (decl == error_mark_node)
30815 else if (kind != 0)
30817 switch (kind)
30819 case OMP_CLAUSE__CACHE_:
30820 /* The OpenACC cache directive explicitly only allows "array
30821 elements or subarrays". */
30822 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
30824 error_at (token->location, "expected %<[%>");
30825 decl = error_mark_node;
30826 break;
30828 /* FALLTHROUGH. */
30829 case OMP_CLAUSE_MAP:
30830 case OMP_CLAUSE_FROM:
30831 case OMP_CLAUSE_TO:
30832 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
30834 location_t loc
30835 = cp_lexer_peek_token (parser->lexer)->location;
30836 cp_id_kind idk = CP_ID_KIND_NONE;
30837 cp_lexer_consume_token (parser->lexer);
30838 decl = convert_from_reference (decl);
30839 decl
30840 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
30841 decl, false,
30842 &idk, loc);
30844 /* FALLTHROUGH. */
30845 case OMP_CLAUSE_DEPEND:
30846 case OMP_CLAUSE_REDUCTION:
30847 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
30849 tree low_bound = NULL_TREE, length = NULL_TREE;
30851 parser->colon_corrects_to_scope_p = false;
30852 cp_lexer_consume_token (parser->lexer);
30853 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30854 low_bound = cp_parser_expression (parser);
30855 if (!colon)
30856 parser->colon_corrects_to_scope_p
30857 = saved_colon_corrects_to_scope_p;
30858 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
30859 length = integer_one_node;
30860 else
30862 /* Look for `:'. */
30863 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30864 goto skip_comma;
30865 if (!cp_lexer_next_token_is (parser->lexer,
30866 CPP_CLOSE_SQUARE))
30867 length = cp_parser_expression (parser);
30869 /* Look for the closing `]'. */
30870 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
30871 RT_CLOSE_SQUARE))
30872 goto skip_comma;
30874 decl = tree_cons (low_bound, length, decl);
30876 break;
30877 default:
30878 break;
30881 tree u = build_omp_clause (token->location, kind);
30882 OMP_CLAUSE_DECL (u) = decl;
30883 OMP_CLAUSE_CHAIN (u) = list;
30884 list = u;
30886 else
30887 list = tree_cons (decl, NULL_TREE, list);
30889 get_comma:
30890 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
30891 break;
30892 cp_lexer_consume_token (parser->lexer);
30895 if (colon)
30896 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30898 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30900 *colon = true;
30901 cp_parser_require (parser, CPP_COLON, RT_COLON);
30902 return list;
30905 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30907 int ending;
30909 /* Try to resync to an unnested comma. Copied from
30910 cp_parser_parenthesized_expression_list. */
30911 skip_comma:
30912 if (colon)
30913 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30914 ending = cp_parser_skip_to_closing_parenthesis (parser,
30915 /*recovering=*/true,
30916 /*or_comma=*/true,
30917 /*consume_paren=*/true);
30918 if (ending < 0)
30919 goto get_comma;
30922 return list;
30925 /* Similarly, but expect leading and trailing parenthesis. This is a very
30926 common case for omp clauses. */
30928 static tree
30929 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
30931 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30932 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
30933 return list;
30936 /* OpenACC 2.0:
30937 copy ( variable-list )
30938 copyin ( variable-list )
30939 copyout ( variable-list )
30940 create ( variable-list )
30941 delete ( variable-list )
30942 present ( variable-list )
30943 present_or_copy ( variable-list )
30944 pcopy ( variable-list )
30945 present_or_copyin ( variable-list )
30946 pcopyin ( variable-list )
30947 present_or_copyout ( variable-list )
30948 pcopyout ( variable-list )
30949 present_or_create ( variable-list )
30950 pcreate ( variable-list ) */
30952 static tree
30953 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
30954 tree list)
30956 enum gomp_map_kind kind;
30957 switch (c_kind)
30959 case PRAGMA_OACC_CLAUSE_COPY:
30960 kind = GOMP_MAP_FORCE_TOFROM;
30961 break;
30962 case PRAGMA_OACC_CLAUSE_COPYIN:
30963 kind = GOMP_MAP_FORCE_TO;
30964 break;
30965 case PRAGMA_OACC_CLAUSE_COPYOUT:
30966 kind = GOMP_MAP_FORCE_FROM;
30967 break;
30968 case PRAGMA_OACC_CLAUSE_CREATE:
30969 kind = GOMP_MAP_FORCE_ALLOC;
30970 break;
30971 case PRAGMA_OACC_CLAUSE_DELETE:
30972 kind = GOMP_MAP_DELETE;
30973 break;
30974 case PRAGMA_OACC_CLAUSE_DEVICE:
30975 kind = GOMP_MAP_FORCE_TO;
30976 break;
30977 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
30978 kind = GOMP_MAP_DEVICE_RESIDENT;
30979 break;
30980 case PRAGMA_OACC_CLAUSE_HOST:
30981 case PRAGMA_OACC_CLAUSE_SELF:
30982 kind = GOMP_MAP_FORCE_FROM;
30983 break;
30984 case PRAGMA_OACC_CLAUSE_LINK:
30985 kind = GOMP_MAP_LINK;
30986 break;
30987 case PRAGMA_OACC_CLAUSE_PRESENT:
30988 kind = GOMP_MAP_FORCE_PRESENT;
30989 break;
30990 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
30991 kind = GOMP_MAP_TOFROM;
30992 break;
30993 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
30994 kind = GOMP_MAP_TO;
30995 break;
30996 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
30997 kind = GOMP_MAP_FROM;
30998 break;
30999 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31000 kind = GOMP_MAP_ALLOC;
31001 break;
31002 default:
31003 gcc_unreachable ();
31005 tree nl, c;
31006 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31008 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31009 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31011 return nl;
31014 /* OpenACC 2.0:
31015 deviceptr ( variable-list ) */
31017 static tree
31018 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31020 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31021 tree vars, t;
31023 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31024 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31025 variable-list must only allow for pointer variables. */
31026 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31027 for (t = vars; t; t = TREE_CHAIN (t))
31029 tree v = TREE_PURPOSE (t);
31030 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31031 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31032 OMP_CLAUSE_DECL (u) = v;
31033 OMP_CLAUSE_CHAIN (u) = list;
31034 list = u;
31037 return list;
31040 /* OpenACC 2.0:
31041 auto
31042 independent
31043 nohost
31044 seq */
31046 static tree
31047 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31048 enum omp_clause_code code,
31049 tree list, location_t location)
31051 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31052 tree c = build_omp_clause (location, code);
31053 OMP_CLAUSE_CHAIN (c) = list;
31054 return c;
31057 /* OpenACC:
31058 num_gangs ( expression )
31059 num_workers ( expression )
31060 vector_length ( expression ) */
31062 static tree
31063 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31064 const char *str, tree list)
31066 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31068 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31069 return list;
31071 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31073 if (t == error_mark_node
31074 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31076 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31077 /*or_comma=*/false,
31078 /*consume_paren=*/true);
31079 return list;
31082 check_no_duplicate_clause (list, code, str, loc);
31084 tree c = build_omp_clause (loc, code);
31085 OMP_CLAUSE_OPERAND (c, 0) = t;
31086 OMP_CLAUSE_CHAIN (c) = list;
31087 return c;
31090 /* OpenACC:
31092 gang [( gang-arg-list )]
31093 worker [( [num:] int-expr )]
31094 vector [( [length:] int-expr )]
31096 where gang-arg is one of:
31098 [num:] int-expr
31099 static: size-expr
31101 and size-expr may be:
31104 int-expr
31107 static tree
31108 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31109 const char *str, tree list)
31111 const char *id = "num";
31112 cp_lexer *lexer = parser->lexer;
31113 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31114 location_t loc = cp_lexer_peek_token (lexer)->location;
31116 if (kind == OMP_CLAUSE_VECTOR)
31117 id = "length";
31119 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31121 cp_lexer_consume_token (lexer);
31125 cp_token *next = cp_lexer_peek_token (lexer);
31126 int idx = 0;
31128 /* Gang static argument. */
31129 if (kind == OMP_CLAUSE_GANG
31130 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31132 cp_lexer_consume_token (lexer);
31134 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31135 goto cleanup_error;
31137 idx = 1;
31138 if (ops[idx] != NULL)
31140 cp_parser_error (parser, "too many %<static%> arguments");
31141 goto cleanup_error;
31144 /* Check for the '*' argument. */
31145 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31146 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31147 || cp_lexer_nth_token_is (parser->lexer, 2,
31148 CPP_CLOSE_PAREN)))
31150 cp_lexer_consume_token (lexer);
31151 ops[idx] = integer_minus_one_node;
31153 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31155 cp_lexer_consume_token (lexer);
31156 continue;
31158 else break;
31161 /* Worker num: argument and vector length: arguments. */
31162 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31163 && strcmp (id, IDENTIFIER_POINTER (next->u.value)) == 0
31164 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31166 cp_lexer_consume_token (lexer); /* id */
31167 cp_lexer_consume_token (lexer); /* ':' */
31170 /* Now collect the actual argument. */
31171 if (ops[idx] != NULL_TREE)
31173 cp_parser_error (parser, "unexpected argument");
31174 goto cleanup_error;
31177 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31178 false);
31179 if (expr == error_mark_node)
31180 goto cleanup_error;
31182 mark_exp_read (expr);
31183 ops[idx] = expr;
31185 if (kind == OMP_CLAUSE_GANG
31186 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31188 cp_lexer_consume_token (lexer);
31189 continue;
31191 break;
31193 while (1);
31195 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31196 goto cleanup_error;
31199 check_no_duplicate_clause (list, kind, str, loc);
31201 c = build_omp_clause (loc, kind);
31203 if (ops[1])
31204 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31206 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31207 OMP_CLAUSE_CHAIN (c) = list;
31209 return c;
31211 cleanup_error:
31212 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31213 return list;
31216 /* OpenACC 2.0:
31217 tile ( size-expr-list ) */
31219 static tree
31220 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31222 tree c, expr = error_mark_node;
31223 tree tile = NULL_TREE;
31225 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
31227 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31228 return list;
31232 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
31233 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31234 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
31236 cp_lexer_consume_token (parser->lexer);
31237 expr = integer_minus_one_node;
31239 else
31240 expr = cp_parser_assignment_expression (parser, NULL, false, false);
31242 if (expr == error_mark_node)
31243 return list;
31245 tile = tree_cons (NULL_TREE, expr, tile);
31247 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31248 cp_lexer_consume_token (parser->lexer);
31250 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
31252 /* Consume the trailing ')'. */
31253 cp_lexer_consume_token (parser->lexer);
31255 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
31256 tile = nreverse (tile);
31257 OMP_CLAUSE_TILE_LIST (c) = tile;
31258 OMP_CLAUSE_CHAIN (c) = list;
31259 return c;
31262 /* OpenACC 2.0
31263 Parse wait clause or directive parameters. */
31265 static tree
31266 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
31268 vec<tree, va_gc> *args;
31269 tree t, args_tree;
31271 args = cp_parser_parenthesized_expression_list (parser, non_attr,
31272 /*cast_p=*/false,
31273 /*allow_expansion_p=*/true,
31274 /*non_constant_p=*/NULL);
31276 if (args == NULL || args->length () == 0)
31278 cp_parser_error (parser, "expected integer expression before ')'");
31279 if (args != NULL)
31280 release_tree_vector (args);
31281 return list;
31284 args_tree = build_tree_list_vec (args);
31286 release_tree_vector (args);
31288 for (t = args_tree; t; t = TREE_CHAIN (t))
31290 tree targ = TREE_VALUE (t);
31292 if (targ != error_mark_node)
31294 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
31295 error ("%<wait%> expression must be integral");
31296 else
31298 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
31300 mark_rvalue_use (targ);
31301 OMP_CLAUSE_DECL (c) = targ;
31302 OMP_CLAUSE_CHAIN (c) = list;
31303 list = c;
31308 return list;
31311 /* OpenACC:
31312 wait ( int-expr-list ) */
31314 static tree
31315 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
31317 location_t location = cp_lexer_peek_token (parser->lexer)->location;
31319 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
31320 return list;
31322 list = cp_parser_oacc_wait_list (parser, location, list);
31324 return list;
31327 /* OpenMP 3.0:
31328 collapse ( constant-expression ) */
31330 static tree
31331 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
31333 tree c, num;
31334 location_t loc;
31335 HOST_WIDE_INT n;
31337 loc = cp_lexer_peek_token (parser->lexer)->location;
31338 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31339 return list;
31341 num = cp_parser_constant_expression (parser);
31343 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31344 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31345 /*or_comma=*/false,
31346 /*consume_paren=*/true);
31348 if (num == error_mark_node)
31349 return list;
31350 num = fold_non_dependent_expr (num);
31351 if (!tree_fits_shwi_p (num)
31352 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31353 || (n = tree_to_shwi (num)) <= 0
31354 || (int) n != n)
31356 error_at (loc, "collapse argument needs positive constant integer expression");
31357 return list;
31360 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
31361 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
31362 OMP_CLAUSE_CHAIN (c) = list;
31363 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
31365 return c;
31368 /* OpenMP 2.5:
31369 default ( shared | none )
31371 OpenACC 2.0
31372 default (none) */
31374 static tree
31375 cp_parser_omp_clause_default (cp_parser *parser, tree list,
31376 location_t location, bool is_oacc)
31378 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
31379 tree c;
31381 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31382 return list;
31383 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31385 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31386 const char *p = IDENTIFIER_POINTER (id);
31388 switch (p[0])
31390 case 'n':
31391 if (strcmp ("none", p) != 0)
31392 goto invalid_kind;
31393 kind = OMP_CLAUSE_DEFAULT_NONE;
31394 break;
31396 case 's':
31397 if (strcmp ("shared", p) != 0 || is_oacc)
31398 goto invalid_kind;
31399 kind = OMP_CLAUSE_DEFAULT_SHARED;
31400 break;
31402 default:
31403 goto invalid_kind;
31406 cp_lexer_consume_token (parser->lexer);
31408 else
31410 invalid_kind:
31411 if (is_oacc)
31412 cp_parser_error (parser, "expected %<none%>");
31413 else
31414 cp_parser_error (parser, "expected %<none%> or %<shared%>");
31417 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31418 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31419 /*or_comma=*/false,
31420 /*consume_paren=*/true);
31422 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
31423 return list;
31425 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
31426 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
31427 OMP_CLAUSE_CHAIN (c) = list;
31428 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
31430 return c;
31433 /* OpenMP 3.1:
31434 final ( expression ) */
31436 static tree
31437 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
31439 tree t, c;
31441 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31442 return list;
31444 t = cp_parser_condition (parser);
31446 if (t == error_mark_node
31447 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31448 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31449 /*or_comma=*/false,
31450 /*consume_paren=*/true);
31452 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
31454 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
31455 OMP_CLAUSE_FINAL_EXPR (c) = t;
31456 OMP_CLAUSE_CHAIN (c) = list;
31458 return c;
31461 /* OpenMP 2.5:
31462 if ( expression )
31464 OpenMP 4.5:
31465 if ( directive-name-modifier : expression )
31467 directive-name-modifier:
31468 parallel | task | taskloop | target data | target | target update
31469 | target enter data | target exit data */
31471 static tree
31472 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
31473 bool is_omp)
31475 tree t, c;
31476 enum tree_code if_modifier = ERROR_MARK;
31478 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31479 return list;
31481 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31483 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31484 const char *p = IDENTIFIER_POINTER (id);
31485 int n = 2;
31487 if (strcmp ("parallel", p) == 0)
31488 if_modifier = OMP_PARALLEL;
31489 else if (strcmp ("task", p) == 0)
31490 if_modifier = OMP_TASK;
31491 else if (strcmp ("taskloop", p) == 0)
31492 if_modifier = OMP_TASKLOOP;
31493 else if (strcmp ("target", p) == 0)
31495 if_modifier = OMP_TARGET;
31496 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
31498 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
31499 p = IDENTIFIER_POINTER (id);
31500 if (strcmp ("data", p) == 0)
31501 if_modifier = OMP_TARGET_DATA;
31502 else if (strcmp ("update", p) == 0)
31503 if_modifier = OMP_TARGET_UPDATE;
31504 else if (strcmp ("enter", p) == 0)
31505 if_modifier = OMP_TARGET_ENTER_DATA;
31506 else if (strcmp ("exit", p) == 0)
31507 if_modifier = OMP_TARGET_EXIT_DATA;
31508 if (if_modifier != OMP_TARGET)
31509 n = 3;
31510 else
31512 location_t loc
31513 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
31514 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
31515 "or %<exit%>");
31516 if_modifier = ERROR_MARK;
31518 if (if_modifier == OMP_TARGET_ENTER_DATA
31519 || if_modifier == OMP_TARGET_EXIT_DATA)
31521 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
31523 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
31524 p = IDENTIFIER_POINTER (id);
31525 if (strcmp ("data", p) == 0)
31526 n = 4;
31528 if (n != 4)
31530 location_t loc
31531 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
31532 error_at (loc, "expected %<data%>");
31533 if_modifier = ERROR_MARK;
31538 if (if_modifier != ERROR_MARK)
31540 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
31542 while (n-- > 0)
31543 cp_lexer_consume_token (parser->lexer);
31545 else
31547 if (n > 2)
31549 location_t loc
31550 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
31551 error_at (loc, "expected %<:%>");
31553 if_modifier = ERROR_MARK;
31558 t = cp_parser_condition (parser);
31560 if (t == error_mark_node
31561 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31562 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31563 /*or_comma=*/false,
31564 /*consume_paren=*/true);
31566 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
31567 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
31569 if (if_modifier != ERROR_MARK
31570 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
31572 const char *p = NULL;
31573 switch (if_modifier)
31575 case OMP_PARALLEL: p = "parallel"; break;
31576 case OMP_TASK: p = "task"; break;
31577 case OMP_TASKLOOP: p = "taskloop"; break;
31578 case OMP_TARGET_DATA: p = "target data"; break;
31579 case OMP_TARGET: p = "target"; break;
31580 case OMP_TARGET_UPDATE: p = "target update"; break;
31581 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
31582 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
31583 default: gcc_unreachable ();
31585 error_at (location, "too many %<if%> clauses with %qs modifier",
31587 return list;
31589 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
31591 if (!is_omp)
31592 error_at (location, "too many %<if%> clauses");
31593 else
31594 error_at (location, "too many %<if%> clauses without modifier");
31595 return list;
31597 else if (if_modifier == ERROR_MARK
31598 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
31600 error_at (location, "if any %<if%> clause has modifier, then all "
31601 "%<if%> clauses have to use modifier");
31602 return list;
31606 c = build_omp_clause (location, OMP_CLAUSE_IF);
31607 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
31608 OMP_CLAUSE_IF_EXPR (c) = t;
31609 OMP_CLAUSE_CHAIN (c) = list;
31611 return c;
31614 /* OpenMP 3.1:
31615 mergeable */
31617 static tree
31618 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
31619 tree list, location_t location)
31621 tree c;
31623 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
31624 location);
31626 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
31627 OMP_CLAUSE_CHAIN (c) = list;
31628 return c;
31631 /* OpenMP 2.5:
31632 nowait */
31634 static tree
31635 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
31636 tree list, location_t location)
31638 tree c;
31640 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
31642 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
31643 OMP_CLAUSE_CHAIN (c) = list;
31644 return c;
31647 /* OpenMP 2.5:
31648 num_threads ( expression ) */
31650 static tree
31651 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
31652 location_t location)
31654 tree t, c;
31656 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31657 return list;
31659 t = cp_parser_expression (parser);
31661 if (t == error_mark_node
31662 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31663 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31664 /*or_comma=*/false,
31665 /*consume_paren=*/true);
31667 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
31668 "num_threads", location);
31670 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
31671 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
31672 OMP_CLAUSE_CHAIN (c) = list;
31674 return c;
31677 /* OpenMP 4.5:
31678 num_tasks ( expression ) */
31680 static tree
31681 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
31682 location_t location)
31684 tree t, c;
31686 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31687 return list;
31689 t = cp_parser_expression (parser);
31691 if (t == error_mark_node
31692 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31693 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31694 /*or_comma=*/false,
31695 /*consume_paren=*/true);
31697 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
31698 "num_tasks", location);
31700 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
31701 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
31702 OMP_CLAUSE_CHAIN (c) = list;
31704 return c;
31707 /* OpenMP 4.5:
31708 grainsize ( expression ) */
31710 static tree
31711 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
31712 location_t location)
31714 tree t, c;
31716 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31717 return list;
31719 t = cp_parser_expression (parser);
31721 if (t == error_mark_node
31722 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31723 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31724 /*or_comma=*/false,
31725 /*consume_paren=*/true);
31727 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
31728 "grainsize", location);
31730 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
31731 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
31732 OMP_CLAUSE_CHAIN (c) = list;
31734 return c;
31737 /* OpenMP 4.5:
31738 priority ( expression ) */
31740 static tree
31741 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
31742 location_t location)
31744 tree t, c;
31746 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31747 return list;
31749 t = cp_parser_expression (parser);
31751 if (t == error_mark_node
31752 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31753 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31754 /*or_comma=*/false,
31755 /*consume_paren=*/true);
31757 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
31758 "priority", location);
31760 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
31761 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
31762 OMP_CLAUSE_CHAIN (c) = list;
31764 return c;
31767 /* OpenMP 4.5:
31768 hint ( expression ) */
31770 static tree
31771 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
31772 location_t location)
31774 tree t, c;
31776 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31777 return list;
31779 t = cp_parser_expression (parser);
31781 if (t == error_mark_node
31782 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31783 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31784 /*or_comma=*/false,
31785 /*consume_paren=*/true);
31787 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
31789 c = build_omp_clause (location, OMP_CLAUSE_HINT);
31790 OMP_CLAUSE_HINT_EXPR (c) = t;
31791 OMP_CLAUSE_CHAIN (c) = list;
31793 return c;
31796 /* OpenMP 4.5:
31797 defaultmap ( tofrom : scalar ) */
31799 static tree
31800 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
31801 location_t location)
31803 tree c, id;
31804 const char *p;
31806 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31807 return list;
31809 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31811 cp_parser_error (parser, "expected %<tofrom%>");
31812 goto out_err;
31814 id = cp_lexer_peek_token (parser->lexer)->u.value;
31815 p = IDENTIFIER_POINTER (id);
31816 if (strcmp (p, "tofrom") != 0)
31818 cp_parser_error (parser, "expected %<tofrom%>");
31819 goto out_err;
31821 cp_lexer_consume_token (parser->lexer);
31822 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31823 goto out_err;
31825 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31827 cp_parser_error (parser, "expected %<scalar%>");
31828 goto out_err;
31830 id = cp_lexer_peek_token (parser->lexer)->u.value;
31831 p = IDENTIFIER_POINTER (id);
31832 if (strcmp (p, "scalar") != 0)
31834 cp_parser_error (parser, "expected %<scalar%>");
31835 goto out_err;
31837 cp_lexer_consume_token (parser->lexer);
31838 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31839 goto out_err;
31841 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
31842 location);
31844 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
31845 OMP_CLAUSE_CHAIN (c) = list;
31846 return c;
31848 out_err:
31849 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31850 /*or_comma=*/false,
31851 /*consume_paren=*/true);
31852 return list;
31855 /* OpenMP 2.5:
31856 ordered
31858 OpenMP 4.5:
31859 ordered ( constant-expression ) */
31861 static tree
31862 cp_parser_omp_clause_ordered (cp_parser *parser,
31863 tree list, location_t location)
31865 tree c, num = NULL_TREE;
31866 HOST_WIDE_INT n;
31868 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
31869 "ordered", location);
31871 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31873 cp_lexer_consume_token (parser->lexer);
31875 num = cp_parser_constant_expression (parser);
31877 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31878 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31879 /*or_comma=*/false,
31880 /*consume_paren=*/true);
31882 if (num == error_mark_node)
31883 return list;
31884 num = fold_non_dependent_expr (num);
31885 if (!tree_fits_shwi_p (num)
31886 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31887 || (n = tree_to_shwi (num)) <= 0
31888 || (int) n != n)
31890 error_at (location,
31891 "ordered argument needs positive constant integer "
31892 "expression");
31893 return list;
31897 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
31898 OMP_CLAUSE_ORDERED_EXPR (c) = num;
31899 OMP_CLAUSE_CHAIN (c) = list;
31900 return c;
31903 /* OpenMP 2.5:
31904 reduction ( reduction-operator : variable-list )
31906 reduction-operator:
31907 One of: + * - & ^ | && ||
31909 OpenMP 3.1:
31911 reduction-operator:
31912 One of: + * - & ^ | && || min max
31914 OpenMP 4.0:
31916 reduction-operator:
31917 One of: + * - & ^ | && ||
31918 id-expression */
31920 static tree
31921 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
31923 enum tree_code code = ERROR_MARK;
31924 tree nlist, c, id = NULL_TREE;
31926 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31927 return list;
31929 switch (cp_lexer_peek_token (parser->lexer)->type)
31931 case CPP_PLUS: code = PLUS_EXPR; break;
31932 case CPP_MULT: code = MULT_EXPR; break;
31933 case CPP_MINUS: code = MINUS_EXPR; break;
31934 case CPP_AND: code = BIT_AND_EXPR; break;
31935 case CPP_XOR: code = BIT_XOR_EXPR; break;
31936 case CPP_OR: code = BIT_IOR_EXPR; break;
31937 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
31938 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
31939 default: break;
31942 if (code != ERROR_MARK)
31943 cp_lexer_consume_token (parser->lexer);
31944 else
31946 bool saved_colon_corrects_to_scope_p;
31947 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31948 parser->colon_corrects_to_scope_p = false;
31949 id = cp_parser_id_expression (parser, /*template_p=*/false,
31950 /*check_dependency_p=*/true,
31951 /*template_p=*/NULL,
31952 /*declarator_p=*/false,
31953 /*optional_p=*/false);
31954 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31955 if (identifier_p (id))
31957 const char *p = IDENTIFIER_POINTER (id);
31959 if (strcmp (p, "min") == 0)
31960 code = MIN_EXPR;
31961 else if (strcmp (p, "max") == 0)
31962 code = MAX_EXPR;
31963 else if (id == ansi_opname (PLUS_EXPR))
31964 code = PLUS_EXPR;
31965 else if (id == ansi_opname (MULT_EXPR))
31966 code = MULT_EXPR;
31967 else if (id == ansi_opname (MINUS_EXPR))
31968 code = MINUS_EXPR;
31969 else if (id == ansi_opname (BIT_AND_EXPR))
31970 code = BIT_AND_EXPR;
31971 else if (id == ansi_opname (BIT_IOR_EXPR))
31972 code = BIT_IOR_EXPR;
31973 else if (id == ansi_opname (BIT_XOR_EXPR))
31974 code = BIT_XOR_EXPR;
31975 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
31976 code = TRUTH_ANDIF_EXPR;
31977 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
31978 code = TRUTH_ORIF_EXPR;
31979 id = omp_reduction_id (code, id, NULL_TREE);
31980 tree scope = parser->scope;
31981 if (scope)
31982 id = build_qualified_name (NULL_TREE, scope, id, false);
31983 parser->scope = NULL_TREE;
31984 parser->qualifying_scope = NULL_TREE;
31985 parser->object_scope = NULL_TREE;
31987 else
31989 error ("invalid reduction-identifier");
31990 resync_fail:
31991 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31992 /*or_comma=*/false,
31993 /*consume_paren=*/true);
31994 return list;
31998 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31999 goto resync_fail;
32001 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32002 NULL);
32003 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32005 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32006 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32009 return nlist;
32012 /* OpenMP 2.5:
32013 schedule ( schedule-kind )
32014 schedule ( schedule-kind , expression )
32016 schedule-kind:
32017 static | dynamic | guided | runtime | auto
32019 OpenMP 4.5:
32020 schedule ( schedule-modifier : schedule-kind )
32021 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32023 schedule-modifier:
32024 simd
32025 monotonic
32026 nonmonotonic */
32028 static tree
32029 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32031 tree c, t;
32032 int modifiers = 0, nmodifiers = 0;
32034 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32035 return list;
32037 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32039 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32041 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32042 const char *p = IDENTIFIER_POINTER (id);
32043 if (strcmp ("simd", p) == 0)
32044 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32045 else if (strcmp ("monotonic", p) == 0)
32046 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32047 else if (strcmp ("nonmonotonic", p) == 0)
32048 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32049 else
32050 break;
32051 cp_lexer_consume_token (parser->lexer);
32052 if (nmodifiers++ == 0
32053 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32054 cp_lexer_consume_token (parser->lexer);
32055 else
32057 cp_parser_require (parser, CPP_COLON, RT_COLON);
32058 break;
32062 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32064 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32065 const char *p = IDENTIFIER_POINTER (id);
32067 switch (p[0])
32069 case 'd':
32070 if (strcmp ("dynamic", p) != 0)
32071 goto invalid_kind;
32072 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32073 break;
32075 case 'g':
32076 if (strcmp ("guided", p) != 0)
32077 goto invalid_kind;
32078 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32079 break;
32081 case 'r':
32082 if (strcmp ("runtime", p) != 0)
32083 goto invalid_kind;
32084 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32085 break;
32087 default:
32088 goto invalid_kind;
32091 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32092 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32093 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32094 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32095 else
32096 goto invalid_kind;
32097 cp_lexer_consume_token (parser->lexer);
32099 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32100 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32101 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32102 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32104 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32105 "specified");
32106 modifiers = 0;
32109 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32111 cp_token *token;
32112 cp_lexer_consume_token (parser->lexer);
32114 token = cp_lexer_peek_token (parser->lexer);
32115 t = cp_parser_assignment_expression (parser);
32117 if (t == error_mark_node)
32118 goto resync_fail;
32119 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32120 error_at (token->location, "schedule %<runtime%> does not take "
32121 "a %<chunk_size%> parameter");
32122 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32123 error_at (token->location, "schedule %<auto%> does not take "
32124 "a %<chunk_size%> parameter");
32125 else
32126 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32128 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32129 goto resync_fail;
32131 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32132 goto resync_fail;
32134 OMP_CLAUSE_SCHEDULE_KIND (c)
32135 = (enum omp_clause_schedule_kind)
32136 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32138 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32139 OMP_CLAUSE_CHAIN (c) = list;
32140 return c;
32142 invalid_kind:
32143 cp_parser_error (parser, "invalid schedule kind");
32144 resync_fail:
32145 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32146 /*or_comma=*/false,
32147 /*consume_paren=*/true);
32148 return list;
32151 /* OpenMP 3.0:
32152 untied */
32154 static tree
32155 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32156 tree list, location_t location)
32158 tree c;
32160 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32162 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32163 OMP_CLAUSE_CHAIN (c) = list;
32164 return c;
32167 /* OpenMP 4.0:
32168 inbranch
32169 notinbranch */
32171 static tree
32172 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32173 tree list, location_t location)
32175 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32176 tree c = build_omp_clause (location, code);
32177 OMP_CLAUSE_CHAIN (c) = list;
32178 return c;
32181 /* OpenMP 4.0:
32182 parallel
32184 sections
32185 taskgroup */
32187 static tree
32188 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32189 enum omp_clause_code code,
32190 tree list, location_t location)
32192 tree c = build_omp_clause (location, code);
32193 OMP_CLAUSE_CHAIN (c) = list;
32194 return c;
32197 /* OpenMP 4.5:
32198 nogroup */
32200 static tree
32201 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
32202 tree list, location_t location)
32204 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
32205 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
32206 OMP_CLAUSE_CHAIN (c) = list;
32207 return c;
32210 /* OpenMP 4.5:
32211 simd
32212 threads */
32214 static tree
32215 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
32216 enum omp_clause_code code,
32217 tree list, location_t location)
32219 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32220 tree c = build_omp_clause (location, code);
32221 OMP_CLAUSE_CHAIN (c) = list;
32222 return c;
32225 /* OpenMP 4.0:
32226 num_teams ( expression ) */
32228 static tree
32229 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
32230 location_t location)
32232 tree t, c;
32234 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32235 return list;
32237 t = cp_parser_expression (parser);
32239 if (t == error_mark_node
32240 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32241 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32242 /*or_comma=*/false,
32243 /*consume_paren=*/true);
32245 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
32246 "num_teams", location);
32248 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
32249 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
32250 OMP_CLAUSE_CHAIN (c) = list;
32252 return c;
32255 /* OpenMP 4.0:
32256 thread_limit ( expression ) */
32258 static tree
32259 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
32260 location_t location)
32262 tree t, c;
32264 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32265 return list;
32267 t = cp_parser_expression (parser);
32269 if (t == error_mark_node
32270 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32271 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32272 /*or_comma=*/false,
32273 /*consume_paren=*/true);
32275 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
32276 "thread_limit", location);
32278 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
32279 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
32280 OMP_CLAUSE_CHAIN (c) = list;
32282 return c;
32285 /* OpenMP 4.0:
32286 aligned ( variable-list )
32287 aligned ( variable-list : constant-expression ) */
32289 static tree
32290 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
32292 tree nlist, c, alignment = NULL_TREE;
32293 bool colon;
32295 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32296 return list;
32298 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
32299 &colon);
32301 if (colon)
32303 alignment = cp_parser_constant_expression (parser);
32305 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32306 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32307 /*or_comma=*/false,
32308 /*consume_paren=*/true);
32310 if (alignment == error_mark_node)
32311 alignment = NULL_TREE;
32314 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32315 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
32317 return nlist;
32320 /* OpenMP 4.0:
32321 linear ( variable-list )
32322 linear ( variable-list : expression )
32324 OpenMP 4.5:
32325 linear ( modifier ( variable-list ) )
32326 linear ( modifier ( variable-list ) : expression ) */
32328 static tree
32329 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
32330 bool is_cilk_simd_fn, bool declare_simd)
32332 tree nlist, c, step = integer_one_node;
32333 bool colon;
32334 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
32336 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32337 return list;
32339 if (!is_cilk_simd_fn
32340 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32342 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32343 const char *p = IDENTIFIER_POINTER (id);
32345 if (strcmp ("ref", p) == 0)
32346 kind = OMP_CLAUSE_LINEAR_REF;
32347 else if (strcmp ("val", p) == 0)
32348 kind = OMP_CLAUSE_LINEAR_VAL;
32349 else if (strcmp ("uval", p) == 0)
32350 kind = OMP_CLAUSE_LINEAR_UVAL;
32351 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
32352 cp_lexer_consume_token (parser->lexer);
32353 else
32354 kind = OMP_CLAUSE_LINEAR_DEFAULT;
32357 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
32358 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
32359 &colon);
32360 else
32362 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
32363 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
32364 if (colon)
32365 cp_parser_require (parser, CPP_COLON, RT_COLON);
32366 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32367 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32368 /*or_comma=*/false,
32369 /*consume_paren=*/true);
32372 if (colon)
32374 step = NULL_TREE;
32375 if (declare_simd
32376 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32377 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
32379 cp_token *token = cp_lexer_peek_token (parser->lexer);
32380 cp_parser_parse_tentatively (parser);
32381 step = cp_parser_id_expression (parser, /*template_p=*/false,
32382 /*check_dependency_p=*/true,
32383 /*template_p=*/NULL,
32384 /*declarator_p=*/false,
32385 /*optional_p=*/false);
32386 if (step != error_mark_node)
32387 step = cp_parser_lookup_name_simple (parser, step, token->location);
32388 if (step == error_mark_node)
32390 step = NULL_TREE;
32391 cp_parser_abort_tentative_parse (parser);
32393 else if (!cp_parser_parse_definitely (parser))
32394 step = NULL_TREE;
32396 if (!step)
32397 step = cp_parser_expression (parser);
32399 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
32401 sorry ("using parameters for %<linear%> step is not supported yet");
32402 step = integer_one_node;
32404 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32405 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32406 /*or_comma=*/false,
32407 /*consume_paren=*/true);
32409 if (step == error_mark_node)
32410 return list;
32413 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32415 OMP_CLAUSE_LINEAR_STEP (c) = step;
32416 OMP_CLAUSE_LINEAR_KIND (c) = kind;
32419 return nlist;
32422 /* OpenMP 4.0:
32423 safelen ( constant-expression ) */
32425 static tree
32426 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
32427 location_t location)
32429 tree t, c;
32431 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32432 return list;
32434 t = cp_parser_constant_expression (parser);
32436 if (t == error_mark_node
32437 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32438 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32439 /*or_comma=*/false,
32440 /*consume_paren=*/true);
32442 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
32444 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
32445 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
32446 OMP_CLAUSE_CHAIN (c) = list;
32448 return c;
32451 /* OpenMP 4.0:
32452 simdlen ( constant-expression ) */
32454 static tree
32455 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
32456 location_t location)
32458 tree t, c;
32460 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32461 return list;
32463 t = cp_parser_constant_expression (parser);
32465 if (t == error_mark_node
32466 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32467 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32468 /*or_comma=*/false,
32469 /*consume_paren=*/true);
32471 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
32473 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
32474 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
32475 OMP_CLAUSE_CHAIN (c) = list;
32477 return c;
32480 /* OpenMP 4.5:
32481 vec:
32482 identifier [+/- integer]
32483 vec , identifier [+/- integer]
32486 static tree
32487 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
32488 tree list)
32490 tree vec = NULL;
32492 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32494 cp_parser_error (parser, "expected identifier");
32495 return list;
32498 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32500 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
32501 tree t, identifier = cp_parser_identifier (parser);
32502 tree addend = NULL;
32504 if (identifier == error_mark_node)
32505 t = error_mark_node;
32506 else
32508 t = cp_parser_lookup_name_simple
32509 (parser, identifier,
32510 cp_lexer_peek_token (parser->lexer)->location);
32511 if (t == error_mark_node)
32512 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
32513 id_loc);
32516 bool neg = false;
32517 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
32518 neg = true;
32519 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
32521 addend = integer_zero_node;
32522 goto add_to_vector;
32524 cp_lexer_consume_token (parser->lexer);
32526 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
32528 cp_parser_error (parser, "expected integer");
32529 return list;
32532 addend = cp_lexer_peek_token (parser->lexer)->u.value;
32533 if (TREE_CODE (addend) != INTEGER_CST)
32535 cp_parser_error (parser, "expected integer");
32536 return list;
32538 cp_lexer_consume_token (parser->lexer);
32540 add_to_vector:
32541 if (t != error_mark_node)
32543 vec = tree_cons (addend, t, vec);
32544 if (neg)
32545 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
32548 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
32549 break;
32551 cp_lexer_consume_token (parser->lexer);
32554 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
32556 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
32557 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
32558 OMP_CLAUSE_DECL (u) = nreverse (vec);
32559 OMP_CLAUSE_CHAIN (u) = list;
32560 return u;
32562 return list;
32565 /* OpenMP 4.0:
32566 depend ( depend-kind : variable-list )
32568 depend-kind:
32569 in | out | inout
32571 OpenMP 4.5:
32572 depend ( source )
32574 depend ( sink : vec ) */
32576 static tree
32577 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
32579 tree nlist, c;
32580 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
32582 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32583 return list;
32585 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32587 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32588 const char *p = IDENTIFIER_POINTER (id);
32590 if (strcmp ("in", p) == 0)
32591 kind = OMP_CLAUSE_DEPEND_IN;
32592 else if (strcmp ("inout", p) == 0)
32593 kind = OMP_CLAUSE_DEPEND_INOUT;
32594 else if (strcmp ("out", p) == 0)
32595 kind = OMP_CLAUSE_DEPEND_OUT;
32596 else if (strcmp ("source", p) == 0)
32597 kind = OMP_CLAUSE_DEPEND_SOURCE;
32598 else if (strcmp ("sink", p) == 0)
32599 kind = OMP_CLAUSE_DEPEND_SINK;
32600 else
32601 goto invalid_kind;
32603 else
32604 goto invalid_kind;
32606 cp_lexer_consume_token (parser->lexer);
32608 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
32610 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
32611 OMP_CLAUSE_DEPEND_KIND (c) = kind;
32612 OMP_CLAUSE_DECL (c) = NULL_TREE;
32613 OMP_CLAUSE_CHAIN (c) = list;
32614 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32615 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32616 /*or_comma=*/false,
32617 /*consume_paren=*/true);
32618 return c;
32621 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32622 goto resync_fail;
32624 if (kind == OMP_CLAUSE_DEPEND_SINK)
32625 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
32626 else
32628 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
32629 list, NULL);
32631 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32632 OMP_CLAUSE_DEPEND_KIND (c) = kind;
32634 return nlist;
32636 invalid_kind:
32637 cp_parser_error (parser, "invalid depend kind");
32638 resync_fail:
32639 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32640 /*or_comma=*/false,
32641 /*consume_paren=*/true);
32642 return list;
32645 /* OpenMP 4.0:
32646 map ( map-kind : variable-list )
32647 map ( variable-list )
32649 map-kind:
32650 alloc | to | from | tofrom
32652 OpenMP 4.5:
32653 map-kind:
32654 alloc | to | from | tofrom | release | delete
32656 map ( always [,] map-kind: variable-list ) */
32658 static tree
32659 cp_parser_omp_clause_map (cp_parser *parser, tree list)
32661 tree nlist, c;
32662 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
32663 bool always = false;
32665 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32666 return list;
32668 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32670 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32671 const char *p = IDENTIFIER_POINTER (id);
32673 if (strcmp ("always", p) == 0)
32675 int nth = 2;
32676 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
32677 nth++;
32678 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
32679 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
32680 == RID_DELETE))
32681 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
32682 == CPP_COLON))
32684 always = true;
32685 cp_lexer_consume_token (parser->lexer);
32686 if (nth == 3)
32687 cp_lexer_consume_token (parser->lexer);
32692 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32693 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
32695 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32696 const char *p = IDENTIFIER_POINTER (id);
32698 if (strcmp ("alloc", p) == 0)
32699 kind = GOMP_MAP_ALLOC;
32700 else if (strcmp ("to", p) == 0)
32701 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
32702 else if (strcmp ("from", p) == 0)
32703 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
32704 else if (strcmp ("tofrom", p) == 0)
32705 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
32706 else if (strcmp ("release", p) == 0)
32707 kind = GOMP_MAP_RELEASE;
32708 else
32710 cp_parser_error (parser, "invalid map kind");
32711 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32712 /*or_comma=*/false,
32713 /*consume_paren=*/true);
32714 return list;
32716 cp_lexer_consume_token (parser->lexer);
32717 cp_lexer_consume_token (parser->lexer);
32719 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
32720 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
32722 kind = GOMP_MAP_DELETE;
32723 cp_lexer_consume_token (parser->lexer);
32724 cp_lexer_consume_token (parser->lexer);
32727 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
32728 NULL);
32730 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32731 OMP_CLAUSE_SET_MAP_KIND (c, kind);
32733 return nlist;
32736 /* OpenMP 4.0:
32737 device ( expression ) */
32739 static tree
32740 cp_parser_omp_clause_device (cp_parser *parser, tree list,
32741 location_t location)
32743 tree t, c;
32745 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32746 return list;
32748 t = cp_parser_expression (parser);
32750 if (t == error_mark_node
32751 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32752 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32753 /*or_comma=*/false,
32754 /*consume_paren=*/true);
32756 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
32757 "device", location);
32759 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
32760 OMP_CLAUSE_DEVICE_ID (c) = t;
32761 OMP_CLAUSE_CHAIN (c) = list;
32763 return c;
32766 /* OpenMP 4.0:
32767 dist_schedule ( static )
32768 dist_schedule ( static , expression ) */
32770 static tree
32771 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
32772 location_t location)
32774 tree c, t;
32776 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32777 return list;
32779 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
32781 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32782 goto invalid_kind;
32783 cp_lexer_consume_token (parser->lexer);
32785 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32787 cp_lexer_consume_token (parser->lexer);
32789 t = cp_parser_assignment_expression (parser);
32791 if (t == error_mark_node)
32792 goto resync_fail;
32793 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
32795 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32796 goto resync_fail;
32798 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32799 goto resync_fail;
32801 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
32802 location);
32803 OMP_CLAUSE_CHAIN (c) = list;
32804 return c;
32806 invalid_kind:
32807 cp_parser_error (parser, "invalid dist_schedule kind");
32808 resync_fail:
32809 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32810 /*or_comma=*/false,
32811 /*consume_paren=*/true);
32812 return list;
32815 /* OpenMP 4.0:
32816 proc_bind ( proc-bind-kind )
32818 proc-bind-kind:
32819 master | close | spread */
32821 static tree
32822 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
32823 location_t location)
32825 tree c;
32826 enum omp_clause_proc_bind_kind kind;
32828 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32829 return list;
32831 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32833 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32834 const char *p = IDENTIFIER_POINTER (id);
32836 if (strcmp ("master", p) == 0)
32837 kind = OMP_CLAUSE_PROC_BIND_MASTER;
32838 else if (strcmp ("close", p) == 0)
32839 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
32840 else if (strcmp ("spread", p) == 0)
32841 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
32842 else
32843 goto invalid_kind;
32845 else
32846 goto invalid_kind;
32848 cp_lexer_consume_token (parser->lexer);
32849 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32850 goto resync_fail;
32852 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
32853 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
32854 location);
32855 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
32856 OMP_CLAUSE_CHAIN (c) = list;
32857 return c;
32859 invalid_kind:
32860 cp_parser_error (parser, "invalid depend kind");
32861 resync_fail:
32862 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32863 /*or_comma=*/false,
32864 /*consume_paren=*/true);
32865 return list;
32868 /* OpenACC:
32869 async [( int-expr )] */
32871 static tree
32872 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
32874 tree c, t;
32875 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32877 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
32879 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32881 cp_lexer_consume_token (parser->lexer);
32883 t = cp_parser_expression (parser);
32884 if (t == error_mark_node
32885 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32886 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32887 /*or_comma=*/false,
32888 /*consume_paren=*/true);
32891 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
32893 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
32894 OMP_CLAUSE_ASYNC_EXPR (c) = t;
32895 OMP_CLAUSE_CHAIN (c) = list;
32896 list = c;
32898 return list;
32901 /* Parse all OpenACC clauses. The set clauses allowed by the directive
32902 is a bitmask in MASK. Return the list of clauses found. */
32904 static tree
32905 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
32906 const char *where, cp_token *pragma_tok,
32907 bool finish_p = true)
32909 tree clauses = NULL;
32910 bool first = true;
32912 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32914 location_t here;
32915 pragma_omp_clause c_kind;
32916 omp_clause_code code;
32917 const char *c_name;
32918 tree prev = clauses;
32920 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32921 cp_lexer_consume_token (parser->lexer);
32923 here = cp_lexer_peek_token (parser->lexer)->location;
32924 c_kind = cp_parser_omp_clause_name (parser);
32926 switch (c_kind)
32928 case PRAGMA_OACC_CLAUSE_ASYNC:
32929 clauses = cp_parser_oacc_clause_async (parser, clauses);
32930 c_name = "async";
32931 break;
32932 case PRAGMA_OACC_CLAUSE_AUTO:
32933 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
32934 clauses, here);
32935 c_name = "auto";
32936 break;
32937 case PRAGMA_OACC_CLAUSE_COLLAPSE:
32938 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
32939 c_name = "collapse";
32940 break;
32941 case PRAGMA_OACC_CLAUSE_COPY:
32942 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32943 c_name = "copy";
32944 break;
32945 case PRAGMA_OACC_CLAUSE_COPYIN:
32946 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32947 c_name = "copyin";
32948 break;
32949 case PRAGMA_OACC_CLAUSE_COPYOUT:
32950 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32951 c_name = "copyout";
32952 break;
32953 case PRAGMA_OACC_CLAUSE_CREATE:
32954 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32955 c_name = "create";
32956 break;
32957 case PRAGMA_OACC_CLAUSE_DELETE:
32958 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32959 c_name = "delete";
32960 break;
32961 case PRAGMA_OMP_CLAUSE_DEFAULT:
32962 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
32963 c_name = "default";
32964 break;
32965 case PRAGMA_OACC_CLAUSE_DEVICE:
32966 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32967 c_name = "device";
32968 break;
32969 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
32970 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
32971 c_name = "deviceptr";
32972 break;
32973 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
32974 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32975 c_name = "device_resident";
32976 break;
32977 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
32978 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32979 clauses);
32980 c_name = "firstprivate";
32981 break;
32982 case PRAGMA_OACC_CLAUSE_GANG:
32983 c_name = "gang";
32984 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
32985 c_name, clauses);
32986 break;
32987 case PRAGMA_OACC_CLAUSE_HOST:
32988 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32989 c_name = "host";
32990 break;
32991 case PRAGMA_OACC_CLAUSE_IF:
32992 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
32993 c_name = "if";
32994 break;
32995 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
32996 clauses = cp_parser_oacc_simple_clause (parser,
32997 OMP_CLAUSE_INDEPENDENT,
32998 clauses, here);
32999 c_name = "independent";
33000 break;
33001 case PRAGMA_OACC_CLAUSE_LINK:
33002 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33003 c_name = "link";
33004 break;
33005 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33006 code = OMP_CLAUSE_NUM_GANGS;
33007 c_name = "num_gangs";
33008 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33009 clauses);
33010 break;
33011 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33012 c_name = "num_workers";
33013 code = OMP_CLAUSE_NUM_WORKERS;
33014 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33015 clauses);
33016 break;
33017 case PRAGMA_OACC_CLAUSE_PRESENT:
33018 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33019 c_name = "present";
33020 break;
33021 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33022 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33023 c_name = "present_or_copy";
33024 break;
33025 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33026 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33027 c_name = "present_or_copyin";
33028 break;
33029 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33030 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33031 c_name = "present_or_copyout";
33032 break;
33033 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33034 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33035 c_name = "present_or_create";
33036 break;
33037 case PRAGMA_OACC_CLAUSE_PRIVATE:
33038 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33039 clauses);
33040 c_name = "private";
33041 break;
33042 case PRAGMA_OACC_CLAUSE_REDUCTION:
33043 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33044 c_name = "reduction";
33045 break;
33046 case PRAGMA_OACC_CLAUSE_SELF:
33047 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33048 c_name = "self";
33049 break;
33050 case PRAGMA_OACC_CLAUSE_SEQ:
33051 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33052 clauses, here);
33053 c_name = "seq";
33054 break;
33055 case PRAGMA_OACC_CLAUSE_TILE:
33056 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33057 c_name = "tile";
33058 break;
33059 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33060 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33061 clauses);
33062 c_name = "use_device";
33063 break;
33064 case PRAGMA_OACC_CLAUSE_VECTOR:
33065 c_name = "vector";
33066 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33067 c_name, clauses);
33068 break;
33069 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33070 c_name = "vector_length";
33071 code = OMP_CLAUSE_VECTOR_LENGTH;
33072 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33073 clauses);
33074 break;
33075 case PRAGMA_OACC_CLAUSE_WAIT:
33076 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33077 c_name = "wait";
33078 break;
33079 case PRAGMA_OACC_CLAUSE_WORKER:
33080 c_name = "worker";
33081 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33082 c_name, clauses);
33083 break;
33084 default:
33085 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33086 goto saw_error;
33089 first = false;
33091 if (((mask >> c_kind) & 1) == 0)
33093 /* Remove the invalid clause(s) from the list to avoid
33094 confusing the rest of the compiler. */
33095 clauses = prev;
33096 error_at (here, "%qs is not valid for %qs", c_name, where);
33100 saw_error:
33101 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33103 if (finish_p)
33104 return finish_omp_clauses (clauses, C_ORT_ACC);
33106 return clauses;
33109 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33110 is a bitmask in MASK. Return the list of clauses found; the result
33111 of clause default goes in *pdefault. */
33113 static tree
33114 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33115 const char *where, cp_token *pragma_tok,
33116 bool finish_p = true)
33118 tree clauses = NULL;
33119 bool first = true;
33120 cp_token *token = NULL;
33122 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33124 pragma_omp_clause c_kind;
33125 const char *c_name;
33126 tree prev = clauses;
33128 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33129 cp_lexer_consume_token (parser->lexer);
33131 token = cp_lexer_peek_token (parser->lexer);
33132 c_kind = cp_parser_omp_clause_name (parser);
33134 switch (c_kind)
33136 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33137 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33138 token->location);
33139 c_name = "collapse";
33140 break;
33141 case PRAGMA_OMP_CLAUSE_COPYIN:
33142 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33143 c_name = "copyin";
33144 break;
33145 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33146 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33147 clauses);
33148 c_name = "copyprivate";
33149 break;
33150 case PRAGMA_OMP_CLAUSE_DEFAULT:
33151 clauses = cp_parser_omp_clause_default (parser, clauses,
33152 token->location, false);
33153 c_name = "default";
33154 break;
33155 case PRAGMA_OMP_CLAUSE_FINAL:
33156 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33157 c_name = "final";
33158 break;
33159 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33160 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33161 clauses);
33162 c_name = "firstprivate";
33163 break;
33164 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33165 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33166 token->location);
33167 c_name = "grainsize";
33168 break;
33169 case PRAGMA_OMP_CLAUSE_HINT:
33170 clauses = cp_parser_omp_clause_hint (parser, clauses,
33171 token->location);
33172 c_name = "hint";
33173 break;
33174 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33175 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33176 token->location);
33177 c_name = "defaultmap";
33178 break;
33179 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33180 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33181 clauses);
33182 c_name = "use_device_ptr";
33183 break;
33184 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33185 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33186 clauses);
33187 c_name = "is_device_ptr";
33188 break;
33189 case PRAGMA_OMP_CLAUSE_IF:
33190 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33191 true);
33192 c_name = "if";
33193 break;
33194 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33195 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33196 clauses);
33197 c_name = "lastprivate";
33198 break;
33199 case PRAGMA_OMP_CLAUSE_MERGEABLE:
33200 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
33201 token->location);
33202 c_name = "mergeable";
33203 break;
33204 case PRAGMA_OMP_CLAUSE_NOWAIT:
33205 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
33206 c_name = "nowait";
33207 break;
33208 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
33209 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
33210 token->location);
33211 c_name = "num_tasks";
33212 break;
33213 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
33214 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
33215 token->location);
33216 c_name = "num_threads";
33217 break;
33218 case PRAGMA_OMP_CLAUSE_ORDERED:
33219 clauses = cp_parser_omp_clause_ordered (parser, clauses,
33220 token->location);
33221 c_name = "ordered";
33222 break;
33223 case PRAGMA_OMP_CLAUSE_PRIORITY:
33224 clauses = cp_parser_omp_clause_priority (parser, clauses,
33225 token->location);
33226 c_name = "priority";
33227 break;
33228 case PRAGMA_OMP_CLAUSE_PRIVATE:
33229 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33230 clauses);
33231 c_name = "private";
33232 break;
33233 case PRAGMA_OMP_CLAUSE_REDUCTION:
33234 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33235 c_name = "reduction";
33236 break;
33237 case PRAGMA_OMP_CLAUSE_SCHEDULE:
33238 clauses = cp_parser_omp_clause_schedule (parser, clauses,
33239 token->location);
33240 c_name = "schedule";
33241 break;
33242 case PRAGMA_OMP_CLAUSE_SHARED:
33243 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
33244 clauses);
33245 c_name = "shared";
33246 break;
33247 case PRAGMA_OMP_CLAUSE_UNTIED:
33248 clauses = cp_parser_omp_clause_untied (parser, clauses,
33249 token->location);
33250 c_name = "untied";
33251 break;
33252 case PRAGMA_OMP_CLAUSE_INBRANCH:
33253 case PRAGMA_CILK_CLAUSE_MASK:
33254 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
33255 clauses, token->location);
33256 c_name = "inbranch";
33257 break;
33258 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
33259 case PRAGMA_CILK_CLAUSE_NOMASK:
33260 clauses = cp_parser_omp_clause_branch (parser,
33261 OMP_CLAUSE_NOTINBRANCH,
33262 clauses, token->location);
33263 c_name = "notinbranch";
33264 break;
33265 case PRAGMA_OMP_CLAUSE_PARALLEL:
33266 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
33267 clauses, token->location);
33268 c_name = "parallel";
33269 if (!first)
33271 clause_not_first:
33272 error_at (token->location, "%qs must be the first clause of %qs",
33273 c_name, where);
33274 clauses = prev;
33276 break;
33277 case PRAGMA_OMP_CLAUSE_FOR:
33278 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
33279 clauses, token->location);
33280 c_name = "for";
33281 if (!first)
33282 goto clause_not_first;
33283 break;
33284 case PRAGMA_OMP_CLAUSE_SECTIONS:
33285 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
33286 clauses, token->location);
33287 c_name = "sections";
33288 if (!first)
33289 goto clause_not_first;
33290 break;
33291 case PRAGMA_OMP_CLAUSE_TASKGROUP:
33292 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
33293 clauses, token->location);
33294 c_name = "taskgroup";
33295 if (!first)
33296 goto clause_not_first;
33297 break;
33298 case PRAGMA_OMP_CLAUSE_LINK:
33299 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
33300 c_name = "to";
33301 break;
33302 case PRAGMA_OMP_CLAUSE_TO:
33303 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
33304 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
33305 clauses);
33306 else
33307 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
33308 c_name = "to";
33309 break;
33310 case PRAGMA_OMP_CLAUSE_FROM:
33311 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
33312 c_name = "from";
33313 break;
33314 case PRAGMA_OMP_CLAUSE_UNIFORM:
33315 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
33316 clauses);
33317 c_name = "uniform";
33318 break;
33319 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
33320 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
33321 token->location);
33322 c_name = "num_teams";
33323 break;
33324 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
33325 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
33326 token->location);
33327 c_name = "thread_limit";
33328 break;
33329 case PRAGMA_OMP_CLAUSE_ALIGNED:
33330 clauses = cp_parser_omp_clause_aligned (parser, clauses);
33331 c_name = "aligned";
33332 break;
33333 case PRAGMA_OMP_CLAUSE_LINEAR:
33335 bool cilk_simd_fn = false, declare_simd = false;
33336 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
33337 cilk_simd_fn = true;
33338 else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
33339 declare_simd = true;
33340 clauses = cp_parser_omp_clause_linear (parser, clauses,
33341 cilk_simd_fn, declare_simd);
33343 c_name = "linear";
33344 break;
33345 case PRAGMA_OMP_CLAUSE_DEPEND:
33346 clauses = cp_parser_omp_clause_depend (parser, clauses,
33347 token->location);
33348 c_name = "depend";
33349 break;
33350 case PRAGMA_OMP_CLAUSE_MAP:
33351 clauses = cp_parser_omp_clause_map (parser, clauses);
33352 c_name = "map";
33353 break;
33354 case PRAGMA_OMP_CLAUSE_DEVICE:
33355 clauses = cp_parser_omp_clause_device (parser, clauses,
33356 token->location);
33357 c_name = "device";
33358 break;
33359 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
33360 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
33361 token->location);
33362 c_name = "dist_schedule";
33363 break;
33364 case PRAGMA_OMP_CLAUSE_PROC_BIND:
33365 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
33366 token->location);
33367 c_name = "proc_bind";
33368 break;
33369 case PRAGMA_OMP_CLAUSE_SAFELEN:
33370 clauses = cp_parser_omp_clause_safelen (parser, clauses,
33371 token->location);
33372 c_name = "safelen";
33373 break;
33374 case PRAGMA_OMP_CLAUSE_SIMDLEN:
33375 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
33376 token->location);
33377 c_name = "simdlen";
33378 break;
33379 case PRAGMA_OMP_CLAUSE_NOGROUP:
33380 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
33381 token->location);
33382 c_name = "nogroup";
33383 break;
33384 case PRAGMA_OMP_CLAUSE_THREADS:
33385 clauses
33386 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
33387 clauses, token->location);
33388 c_name = "threads";
33389 break;
33390 case PRAGMA_OMP_CLAUSE_SIMD:
33391 clauses
33392 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
33393 clauses, token->location);
33394 c_name = "simd";
33395 break;
33396 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
33397 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
33398 c_name = "simdlen";
33399 break;
33400 default:
33401 cp_parser_error (parser, "expected %<#pragma omp%> clause");
33402 goto saw_error;
33405 first = false;
33407 if (((mask >> c_kind) & 1) == 0)
33409 /* Remove the invalid clause(s) from the list to avoid
33410 confusing the rest of the compiler. */
33411 clauses = prev;
33412 error_at (token->location, "%qs is not valid for %qs", c_name, where);
33415 saw_error:
33416 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
33417 no reason to skip to the end. */
33418 if (!(flag_cilkplus && pragma_tok == NULL))
33419 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33420 if (finish_p)
33422 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
33423 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
33424 else
33425 return finish_omp_clauses (clauses, C_ORT_OMP);
33427 return clauses;
33430 /* OpenMP 2.5:
33431 structured-block:
33432 statement
33434 In practice, we're also interested in adding the statement to an
33435 outer node. So it is convenient if we work around the fact that
33436 cp_parser_statement calls add_stmt. */
33438 static unsigned
33439 cp_parser_begin_omp_structured_block (cp_parser *parser)
33441 unsigned save = parser->in_statement;
33443 /* Only move the values to IN_OMP_BLOCK if they weren't false.
33444 This preserves the "not within loop or switch" style error messages
33445 for nonsense cases like
33446 void foo() {
33447 #pragma omp single
33448 break;
33451 if (parser->in_statement)
33452 parser->in_statement = IN_OMP_BLOCK;
33454 return save;
33457 static void
33458 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
33460 parser->in_statement = save;
33463 static tree
33464 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
33466 tree stmt = begin_omp_structured_block ();
33467 unsigned int save = cp_parser_begin_omp_structured_block (parser);
33469 cp_parser_statement (parser, NULL_TREE, false, if_p);
33471 cp_parser_end_omp_structured_block (parser, save);
33472 return finish_omp_structured_block (stmt);
33475 /* OpenMP 2.5:
33476 # pragma omp atomic new-line
33477 expression-stmt
33479 expression-stmt:
33480 x binop= expr | x++ | ++x | x-- | --x
33481 binop:
33482 +, *, -, /, &, ^, |, <<, >>
33484 where x is an lvalue expression with scalar type.
33486 OpenMP 3.1:
33487 # pragma omp atomic new-line
33488 update-stmt
33490 # pragma omp atomic read new-line
33491 read-stmt
33493 # pragma omp atomic write new-line
33494 write-stmt
33496 # pragma omp atomic update new-line
33497 update-stmt
33499 # pragma omp atomic capture new-line
33500 capture-stmt
33502 # pragma omp atomic capture new-line
33503 capture-block
33505 read-stmt:
33506 v = x
33507 write-stmt:
33508 x = expr
33509 update-stmt:
33510 expression-stmt | x = x binop expr
33511 capture-stmt:
33512 v = expression-stmt
33513 capture-block:
33514 { v = x; update-stmt; } | { update-stmt; v = x; }
33516 OpenMP 4.0:
33517 update-stmt:
33518 expression-stmt | x = x binop expr | x = expr binop x
33519 capture-stmt:
33520 v = update-stmt
33521 capture-block:
33522 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
33524 where x and v are lvalue expressions with scalar type. */
33526 static void
33527 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
33529 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
33530 tree rhs1 = NULL_TREE, orig_lhs;
33531 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
33532 bool structured_block = false;
33533 bool seq_cst = false;
33535 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33537 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33538 const char *p = IDENTIFIER_POINTER (id);
33540 if (!strcmp (p, "seq_cst"))
33542 seq_cst = true;
33543 cp_lexer_consume_token (parser->lexer);
33544 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33545 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33546 cp_lexer_consume_token (parser->lexer);
33549 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33551 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33552 const char *p = IDENTIFIER_POINTER (id);
33554 if (!strcmp (p, "read"))
33555 code = OMP_ATOMIC_READ;
33556 else if (!strcmp (p, "write"))
33557 code = NOP_EXPR;
33558 else if (!strcmp (p, "update"))
33559 code = OMP_ATOMIC;
33560 else if (!strcmp (p, "capture"))
33561 code = OMP_ATOMIC_CAPTURE_NEW;
33562 else
33563 p = NULL;
33564 if (p)
33565 cp_lexer_consume_token (parser->lexer);
33567 if (!seq_cst)
33569 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33570 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33571 cp_lexer_consume_token (parser->lexer);
33573 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33575 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33576 const char *p = IDENTIFIER_POINTER (id);
33578 if (!strcmp (p, "seq_cst"))
33580 seq_cst = true;
33581 cp_lexer_consume_token (parser->lexer);
33585 cp_parser_require_pragma_eol (parser, pragma_tok);
33587 switch (code)
33589 case OMP_ATOMIC_READ:
33590 case NOP_EXPR: /* atomic write */
33591 v = cp_parser_unary_expression (parser);
33592 if (v == error_mark_node)
33593 goto saw_error;
33594 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33595 goto saw_error;
33596 if (code == NOP_EXPR)
33597 lhs = cp_parser_expression (parser);
33598 else
33599 lhs = cp_parser_unary_expression (parser);
33600 if (lhs == error_mark_node)
33601 goto saw_error;
33602 if (code == NOP_EXPR)
33604 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
33605 opcode. */
33606 code = OMP_ATOMIC;
33607 rhs = lhs;
33608 lhs = v;
33609 v = NULL_TREE;
33611 goto done;
33612 case OMP_ATOMIC_CAPTURE_NEW:
33613 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33615 cp_lexer_consume_token (parser->lexer);
33616 structured_block = true;
33618 else
33620 v = cp_parser_unary_expression (parser);
33621 if (v == error_mark_node)
33622 goto saw_error;
33623 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33624 goto saw_error;
33626 default:
33627 break;
33630 restart:
33631 lhs = cp_parser_unary_expression (parser);
33632 orig_lhs = lhs;
33633 switch (TREE_CODE (lhs))
33635 case ERROR_MARK:
33636 goto saw_error;
33638 case POSTINCREMENT_EXPR:
33639 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
33640 code = OMP_ATOMIC_CAPTURE_OLD;
33641 /* FALLTHROUGH */
33642 case PREINCREMENT_EXPR:
33643 lhs = TREE_OPERAND (lhs, 0);
33644 opcode = PLUS_EXPR;
33645 rhs = integer_one_node;
33646 break;
33648 case POSTDECREMENT_EXPR:
33649 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
33650 code = OMP_ATOMIC_CAPTURE_OLD;
33651 /* FALLTHROUGH */
33652 case PREDECREMENT_EXPR:
33653 lhs = TREE_OPERAND (lhs, 0);
33654 opcode = MINUS_EXPR;
33655 rhs = integer_one_node;
33656 break;
33658 case COMPOUND_EXPR:
33659 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
33660 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
33661 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
33662 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
33663 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
33664 (TREE_OPERAND (lhs, 1), 0), 0)))
33665 == BOOLEAN_TYPE)
33666 /* Undo effects of boolean_increment for post {in,de}crement. */
33667 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
33668 /* FALLTHRU */
33669 case MODIFY_EXPR:
33670 if (TREE_CODE (lhs) == MODIFY_EXPR
33671 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
33673 /* Undo effects of boolean_increment. */
33674 if (integer_onep (TREE_OPERAND (lhs, 1)))
33676 /* This is pre or post increment. */
33677 rhs = TREE_OPERAND (lhs, 1);
33678 lhs = TREE_OPERAND (lhs, 0);
33679 opcode = NOP_EXPR;
33680 if (code == OMP_ATOMIC_CAPTURE_NEW
33681 && !structured_block
33682 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
33683 code = OMP_ATOMIC_CAPTURE_OLD;
33684 break;
33687 /* FALLTHRU */
33688 default:
33689 switch (cp_lexer_peek_token (parser->lexer)->type)
33691 case CPP_MULT_EQ:
33692 opcode = MULT_EXPR;
33693 break;
33694 case CPP_DIV_EQ:
33695 opcode = TRUNC_DIV_EXPR;
33696 break;
33697 case CPP_PLUS_EQ:
33698 opcode = PLUS_EXPR;
33699 break;
33700 case CPP_MINUS_EQ:
33701 opcode = MINUS_EXPR;
33702 break;
33703 case CPP_LSHIFT_EQ:
33704 opcode = LSHIFT_EXPR;
33705 break;
33706 case CPP_RSHIFT_EQ:
33707 opcode = RSHIFT_EXPR;
33708 break;
33709 case CPP_AND_EQ:
33710 opcode = BIT_AND_EXPR;
33711 break;
33712 case CPP_OR_EQ:
33713 opcode = BIT_IOR_EXPR;
33714 break;
33715 case CPP_XOR_EQ:
33716 opcode = BIT_XOR_EXPR;
33717 break;
33718 case CPP_EQ:
33719 enum cp_parser_prec oprec;
33720 cp_token *token;
33721 cp_lexer_consume_token (parser->lexer);
33722 cp_parser_parse_tentatively (parser);
33723 rhs1 = cp_parser_simple_cast_expression (parser);
33724 if (rhs1 == error_mark_node)
33726 cp_parser_abort_tentative_parse (parser);
33727 cp_parser_simple_cast_expression (parser);
33728 goto saw_error;
33730 token = cp_lexer_peek_token (parser->lexer);
33731 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
33733 cp_parser_abort_tentative_parse (parser);
33734 cp_parser_parse_tentatively (parser);
33735 rhs = cp_parser_binary_expression (parser, false, true,
33736 PREC_NOT_OPERATOR, NULL);
33737 if (rhs == error_mark_node)
33739 cp_parser_abort_tentative_parse (parser);
33740 cp_parser_binary_expression (parser, false, true,
33741 PREC_NOT_OPERATOR, NULL);
33742 goto saw_error;
33744 switch (TREE_CODE (rhs))
33746 case MULT_EXPR:
33747 case TRUNC_DIV_EXPR:
33748 case RDIV_EXPR:
33749 case PLUS_EXPR:
33750 case MINUS_EXPR:
33751 case LSHIFT_EXPR:
33752 case RSHIFT_EXPR:
33753 case BIT_AND_EXPR:
33754 case BIT_IOR_EXPR:
33755 case BIT_XOR_EXPR:
33756 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
33758 if (cp_parser_parse_definitely (parser))
33760 opcode = TREE_CODE (rhs);
33761 rhs1 = TREE_OPERAND (rhs, 0);
33762 rhs = TREE_OPERAND (rhs, 1);
33763 goto stmt_done;
33765 else
33766 goto saw_error;
33768 break;
33769 default:
33770 break;
33772 cp_parser_abort_tentative_parse (parser);
33773 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
33775 rhs = cp_parser_expression (parser);
33776 if (rhs == error_mark_node)
33777 goto saw_error;
33778 opcode = NOP_EXPR;
33779 rhs1 = NULL_TREE;
33780 goto stmt_done;
33782 cp_parser_error (parser,
33783 "invalid form of %<#pragma omp atomic%>");
33784 goto saw_error;
33786 if (!cp_parser_parse_definitely (parser))
33787 goto saw_error;
33788 switch (token->type)
33790 case CPP_SEMICOLON:
33791 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33793 code = OMP_ATOMIC_CAPTURE_OLD;
33794 v = lhs;
33795 lhs = NULL_TREE;
33796 lhs1 = rhs1;
33797 rhs1 = NULL_TREE;
33798 cp_lexer_consume_token (parser->lexer);
33799 goto restart;
33801 else if (structured_block)
33803 opcode = NOP_EXPR;
33804 rhs = rhs1;
33805 rhs1 = NULL_TREE;
33806 goto stmt_done;
33808 cp_parser_error (parser,
33809 "invalid form of %<#pragma omp atomic%>");
33810 goto saw_error;
33811 case CPP_MULT:
33812 opcode = MULT_EXPR;
33813 break;
33814 case CPP_DIV:
33815 opcode = TRUNC_DIV_EXPR;
33816 break;
33817 case CPP_PLUS:
33818 opcode = PLUS_EXPR;
33819 break;
33820 case CPP_MINUS:
33821 opcode = MINUS_EXPR;
33822 break;
33823 case CPP_LSHIFT:
33824 opcode = LSHIFT_EXPR;
33825 break;
33826 case CPP_RSHIFT:
33827 opcode = RSHIFT_EXPR;
33828 break;
33829 case CPP_AND:
33830 opcode = BIT_AND_EXPR;
33831 break;
33832 case CPP_OR:
33833 opcode = BIT_IOR_EXPR;
33834 break;
33835 case CPP_XOR:
33836 opcode = BIT_XOR_EXPR;
33837 break;
33838 default:
33839 cp_parser_error (parser,
33840 "invalid operator for %<#pragma omp atomic%>");
33841 goto saw_error;
33843 oprec = TOKEN_PRECEDENCE (token);
33844 gcc_assert (oprec != PREC_NOT_OPERATOR);
33845 if (commutative_tree_code (opcode))
33846 oprec = (enum cp_parser_prec) (oprec - 1);
33847 cp_lexer_consume_token (parser->lexer);
33848 rhs = cp_parser_binary_expression (parser, false, false,
33849 oprec, NULL);
33850 if (rhs == error_mark_node)
33851 goto saw_error;
33852 goto stmt_done;
33853 /* FALLTHROUGH */
33854 default:
33855 cp_parser_error (parser,
33856 "invalid operator for %<#pragma omp atomic%>");
33857 goto saw_error;
33859 cp_lexer_consume_token (parser->lexer);
33861 rhs = cp_parser_expression (parser);
33862 if (rhs == error_mark_node)
33863 goto saw_error;
33864 break;
33866 stmt_done:
33867 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33869 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
33870 goto saw_error;
33871 v = cp_parser_unary_expression (parser);
33872 if (v == error_mark_node)
33873 goto saw_error;
33874 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33875 goto saw_error;
33876 lhs1 = cp_parser_unary_expression (parser);
33877 if (lhs1 == error_mark_node)
33878 goto saw_error;
33880 if (structured_block)
33882 cp_parser_consume_semicolon_at_end_of_statement (parser);
33883 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
33885 done:
33886 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
33887 if (!structured_block)
33888 cp_parser_consume_semicolon_at_end_of_statement (parser);
33889 return;
33891 saw_error:
33892 cp_parser_skip_to_end_of_block_or_statement (parser);
33893 if (structured_block)
33895 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33896 cp_lexer_consume_token (parser->lexer);
33897 else if (code == OMP_ATOMIC_CAPTURE_NEW)
33899 cp_parser_skip_to_end_of_block_or_statement (parser);
33900 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33901 cp_lexer_consume_token (parser->lexer);
33907 /* OpenMP 2.5:
33908 # pragma omp barrier new-line */
33910 static void
33911 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
33913 cp_parser_require_pragma_eol (parser, pragma_tok);
33914 finish_omp_barrier ();
33917 /* OpenMP 2.5:
33918 # pragma omp critical [(name)] new-line
33919 structured-block
33921 OpenMP 4.5:
33922 # pragma omp critical [(name) [hint(expression)]] new-line
33923 structured-block */
33925 #define OMP_CRITICAL_CLAUSE_MASK \
33926 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
33928 static tree
33929 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
33931 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
33933 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33935 cp_lexer_consume_token (parser->lexer);
33937 name = cp_parser_identifier (parser);
33939 if (name == error_mark_node
33940 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33941 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33942 /*or_comma=*/false,
33943 /*consume_paren=*/true);
33944 if (name == error_mark_node)
33945 name = NULL;
33947 clauses = cp_parser_omp_all_clauses (parser,
33948 OMP_CRITICAL_CLAUSE_MASK,
33949 "#pragma omp critical", pragma_tok);
33951 else
33952 cp_parser_require_pragma_eol (parser, pragma_tok);
33954 stmt = cp_parser_omp_structured_block (parser, if_p);
33955 return c_finish_omp_critical (input_location, stmt, name, clauses);
33958 /* OpenMP 2.5:
33959 # pragma omp flush flush-vars[opt] new-line
33961 flush-vars:
33962 ( variable-list ) */
33964 static void
33965 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
33967 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33968 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
33969 cp_parser_require_pragma_eol (parser, pragma_tok);
33971 finish_omp_flush ();
33974 /* Helper function, to parse omp for increment expression. */
33976 static tree
33977 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
33979 tree cond = cp_parser_binary_expression (parser, false, true,
33980 PREC_NOT_OPERATOR, NULL);
33981 if (cond == error_mark_node
33982 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33984 cp_parser_skip_to_end_of_statement (parser);
33985 return error_mark_node;
33988 switch (TREE_CODE (cond))
33990 case GT_EXPR:
33991 case GE_EXPR:
33992 case LT_EXPR:
33993 case LE_EXPR:
33994 break;
33995 case NE_EXPR:
33996 if (code == CILK_SIMD || code == CILK_FOR)
33997 break;
33998 /* Fall through: OpenMP disallows NE_EXPR. */
33999 gcc_fallthrough ();
34000 default:
34001 return error_mark_node;
34004 /* If decl is an iterator, preserve LHS and RHS of the relational
34005 expr until finish_omp_for. */
34006 if (decl
34007 && (type_dependent_expression_p (decl)
34008 || CLASS_TYPE_P (TREE_TYPE (decl))))
34009 return cond;
34011 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34012 TREE_CODE (cond),
34013 TREE_OPERAND (cond, 0), ERROR_MARK,
34014 TREE_OPERAND (cond, 1), ERROR_MARK,
34015 /*overload=*/NULL, tf_warning_or_error);
34018 /* Helper function, to parse omp for increment expression. */
34020 static tree
34021 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34023 cp_token *token = cp_lexer_peek_token (parser->lexer);
34024 enum tree_code op;
34025 tree lhs, rhs;
34026 cp_id_kind idk;
34027 bool decl_first;
34029 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34031 op = (token->type == CPP_PLUS_PLUS
34032 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34033 cp_lexer_consume_token (parser->lexer);
34034 lhs = cp_parser_simple_cast_expression (parser);
34035 if (lhs != decl
34036 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34037 return error_mark_node;
34038 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34041 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34042 if (lhs != decl
34043 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34044 return error_mark_node;
34046 token = cp_lexer_peek_token (parser->lexer);
34047 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34049 op = (token->type == CPP_PLUS_PLUS
34050 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34051 cp_lexer_consume_token (parser->lexer);
34052 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34055 op = cp_parser_assignment_operator_opt (parser);
34056 if (op == ERROR_MARK)
34057 return error_mark_node;
34059 if (op != NOP_EXPR)
34061 rhs = cp_parser_assignment_expression (parser);
34062 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34063 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34066 lhs = cp_parser_binary_expression (parser, false, false,
34067 PREC_ADDITIVE_EXPRESSION, NULL);
34068 token = cp_lexer_peek_token (parser->lexer);
34069 decl_first = (lhs == decl
34070 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34071 if (decl_first)
34072 lhs = NULL_TREE;
34073 if (token->type != CPP_PLUS
34074 && token->type != CPP_MINUS)
34075 return error_mark_node;
34079 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34080 cp_lexer_consume_token (parser->lexer);
34081 rhs = cp_parser_binary_expression (parser, false, false,
34082 PREC_ADDITIVE_EXPRESSION, NULL);
34083 token = cp_lexer_peek_token (parser->lexer);
34084 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34086 if (lhs == NULL_TREE)
34088 if (op == PLUS_EXPR)
34089 lhs = rhs;
34090 else
34091 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34092 tf_warning_or_error);
34094 else
34095 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34096 ERROR_MARK, NULL, tf_warning_or_error);
34099 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34101 if (!decl_first)
34103 if ((rhs != decl
34104 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34105 || op == MINUS_EXPR)
34106 return error_mark_node;
34107 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34109 else
34110 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34112 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34115 /* Parse the initialization statement of either an OpenMP for loop or
34116 a Cilk Plus for loop.
34118 Return true if the resulting construct should have an
34119 OMP_CLAUSE_PRIVATE added to it. */
34121 static tree
34122 cp_parser_omp_for_loop_init (cp_parser *parser,
34123 enum tree_code code,
34124 tree &this_pre_body,
34125 vec<tree, va_gc> *for_block,
34126 tree &init,
34127 tree &orig_init,
34128 tree &decl,
34129 tree &real_decl)
34131 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34132 return NULL_TREE;
34134 tree add_private_clause = NULL_TREE;
34136 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34138 init-expr:
34139 var = lb
34140 integer-type var = lb
34141 random-access-iterator-type var = lb
34142 pointer-type var = lb
34144 cp_decl_specifier_seq type_specifiers;
34146 /* First, try to parse as an initialized declaration. See
34147 cp_parser_condition, from whence the bulk of this is copied. */
34149 cp_parser_parse_tentatively (parser);
34150 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34151 /*is_trailing_return=*/false,
34152 &type_specifiers);
34153 if (cp_parser_parse_definitely (parser))
34155 /* If parsing a type specifier seq succeeded, then this
34156 MUST be a initialized declaration. */
34157 tree asm_specification, attributes;
34158 cp_declarator *declarator;
34160 declarator = cp_parser_declarator (parser,
34161 CP_PARSER_DECLARATOR_NAMED,
34162 /*ctor_dtor_or_conv_p=*/NULL,
34163 /*parenthesized_p=*/NULL,
34164 /*member_p=*/false,
34165 /*friend_p=*/false);
34166 attributes = cp_parser_attributes_opt (parser);
34167 asm_specification = cp_parser_asm_specification_opt (parser);
34169 if (declarator == cp_error_declarator)
34170 cp_parser_skip_to_end_of_statement (parser);
34172 else
34174 tree pushed_scope, auto_node;
34176 decl = start_decl (declarator, &type_specifiers,
34177 SD_INITIALIZED, attributes,
34178 /*prefix_attributes=*/NULL_TREE,
34179 &pushed_scope);
34181 auto_node = type_uses_auto (TREE_TYPE (decl));
34182 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34184 if (cp_lexer_next_token_is (parser->lexer,
34185 CPP_OPEN_PAREN))
34187 if (code != CILK_SIMD && code != CILK_FOR)
34188 error ("parenthesized initialization is not allowed in "
34189 "OpenMP %<for%> loop");
34190 else
34191 error ("parenthesized initialization is "
34192 "not allowed in for-loop");
34194 else
34195 /* Trigger an error. */
34196 cp_parser_require (parser, CPP_EQ, RT_EQ);
34198 init = error_mark_node;
34199 cp_parser_skip_to_end_of_statement (parser);
34201 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34202 || type_dependent_expression_p (decl)
34203 || auto_node)
34205 bool is_direct_init, is_non_constant_init;
34207 init = cp_parser_initializer (parser,
34208 &is_direct_init,
34209 &is_non_constant_init);
34211 if (auto_node)
34213 TREE_TYPE (decl)
34214 = do_auto_deduction (TREE_TYPE (decl), init,
34215 auto_node);
34217 if (!CLASS_TYPE_P (TREE_TYPE (decl))
34218 && !type_dependent_expression_p (decl))
34219 goto non_class;
34222 cp_finish_decl (decl, init, !is_non_constant_init,
34223 asm_specification,
34224 LOOKUP_ONLYCONVERTING);
34225 orig_init = init;
34226 if (CLASS_TYPE_P (TREE_TYPE (decl)))
34228 vec_safe_push (for_block, this_pre_body);
34229 init = NULL_TREE;
34231 else
34233 init = pop_stmt_list (this_pre_body);
34234 if (init && TREE_CODE (init) == STATEMENT_LIST)
34236 tree_stmt_iterator i = tsi_start (init);
34237 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34238 while (!tsi_end_p (i))
34240 tree t = tsi_stmt (i);
34241 if (TREE_CODE (t) == DECL_EXPR
34242 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
34244 tsi_delink (&i);
34245 vec_safe_push (for_block, t);
34246 continue;
34248 break;
34250 if (tsi_one_before_end_p (i))
34252 tree t = tsi_stmt (i);
34253 tsi_delink (&i);
34254 free_stmt_list (init);
34255 init = t;
34259 this_pre_body = NULL_TREE;
34261 else
34263 /* Consume '='. */
34264 cp_lexer_consume_token (parser->lexer);
34265 init = cp_parser_assignment_expression (parser);
34267 non_class:
34268 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
34269 init = error_mark_node;
34270 else
34271 cp_finish_decl (decl, NULL_TREE,
34272 /*init_const_expr_p=*/false,
34273 asm_specification,
34274 LOOKUP_ONLYCONVERTING);
34277 if (pushed_scope)
34278 pop_scope (pushed_scope);
34281 else
34283 cp_id_kind idk;
34284 /* If parsing a type specifier sequence failed, then
34285 this MUST be a simple expression. */
34286 if (code == CILK_FOR)
34287 error ("%<_Cilk_for%> allows expression instead of declaration only "
34288 "in C, not in C++");
34289 cp_parser_parse_tentatively (parser);
34290 decl = cp_parser_primary_expression (parser, false, false,
34291 false, &idk);
34292 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
34293 if (!cp_parser_error_occurred (parser)
34294 && decl
34295 && (TREE_CODE (decl) == COMPONENT_REF
34296 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
34298 cp_parser_abort_tentative_parse (parser);
34299 cp_parser_parse_tentatively (parser);
34300 cp_token *token = cp_lexer_peek_token (parser->lexer);
34301 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
34302 /*check_dependency_p=*/true,
34303 /*template_p=*/NULL,
34304 /*declarator_p=*/false,
34305 /*optional_p=*/false);
34306 if (name != error_mark_node
34307 && last_tok == cp_lexer_peek_token (parser->lexer))
34309 decl = cp_parser_lookup_name_simple (parser, name,
34310 token->location);
34311 if (TREE_CODE (decl) == FIELD_DECL)
34312 add_private_clause = omp_privatize_field (decl, false);
34314 cp_parser_abort_tentative_parse (parser);
34315 cp_parser_parse_tentatively (parser);
34316 decl = cp_parser_primary_expression (parser, false, false,
34317 false, &idk);
34319 if (!cp_parser_error_occurred (parser)
34320 && decl
34321 && DECL_P (decl)
34322 && CLASS_TYPE_P (TREE_TYPE (decl)))
34324 tree rhs;
34326 cp_parser_parse_definitely (parser);
34327 cp_parser_require (parser, CPP_EQ, RT_EQ);
34328 rhs = cp_parser_assignment_expression (parser);
34329 orig_init = rhs;
34330 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
34331 decl, NOP_EXPR,
34332 rhs,
34333 tf_warning_or_error));
34334 if (!add_private_clause)
34335 add_private_clause = decl;
34337 else
34339 decl = NULL;
34340 cp_parser_abort_tentative_parse (parser);
34341 init = cp_parser_expression (parser);
34342 if (init)
34344 if (TREE_CODE (init) == MODIFY_EXPR
34345 || TREE_CODE (init) == MODOP_EXPR)
34346 real_decl = TREE_OPERAND (init, 0);
34350 return add_private_clause;
34353 /* Parse the restricted form of the for statement allowed by OpenMP. */
34355 static tree
34356 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
34357 tree *cclauses, bool *if_p)
34359 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
34360 tree real_decl, initv, condv, incrv, declv;
34361 tree this_pre_body, cl, ordered_cl = NULL_TREE;
34362 location_t loc_first;
34363 bool collapse_err = false;
34364 int i, collapse = 1, ordered = 0, count, nbraces = 0;
34365 vec<tree, va_gc> *for_block = make_tree_vector ();
34366 auto_vec<tree, 4> orig_inits;
34368 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
34369 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
34370 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
34371 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
34372 && OMP_CLAUSE_ORDERED_EXPR (cl))
34374 ordered_cl = cl;
34375 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
34378 if (ordered && ordered < collapse)
34380 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
34381 "%<ordered%> clause parameter is less than %<collapse%>");
34382 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
34383 = build_int_cst (NULL_TREE, collapse);
34384 ordered = collapse;
34386 if (ordered)
34388 for (tree *pc = &clauses; *pc; )
34389 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
34391 error_at (OMP_CLAUSE_LOCATION (*pc),
34392 "%<linear%> clause may not be specified together "
34393 "with %<ordered%> clause with a parameter");
34394 *pc = OMP_CLAUSE_CHAIN (*pc);
34396 else
34397 pc = &OMP_CLAUSE_CHAIN (*pc);
34400 gcc_assert (collapse >= 1 && ordered >= 0);
34401 count = ordered ? ordered : collapse;
34403 declv = make_tree_vec (count);
34404 initv = make_tree_vec (count);
34405 condv = make_tree_vec (count);
34406 incrv = make_tree_vec (count);
34408 loc_first = cp_lexer_peek_token (parser->lexer)->location;
34410 for (i = 0; i < count; i++)
34412 int bracecount = 0;
34413 tree add_private_clause = NULL_TREE;
34414 location_t loc;
34416 if (code != CILK_FOR
34417 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34419 cp_parser_error (parser, "for statement expected");
34420 return NULL;
34422 if (code == CILK_FOR
34423 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
34425 cp_parser_error (parser, "_Cilk_for statement expected");
34426 return NULL;
34428 loc = cp_lexer_consume_token (parser->lexer)->location;
34430 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34431 return NULL;
34433 init = orig_init = decl = real_decl = NULL;
34434 this_pre_body = push_stmt_list ();
34436 add_private_clause
34437 = cp_parser_omp_for_loop_init (parser, code,
34438 this_pre_body, for_block,
34439 init, orig_init, decl, real_decl);
34441 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34442 if (this_pre_body)
34444 this_pre_body = pop_stmt_list (this_pre_body);
34445 if (pre_body)
34447 tree t = pre_body;
34448 pre_body = push_stmt_list ();
34449 add_stmt (t);
34450 add_stmt (this_pre_body);
34451 pre_body = pop_stmt_list (pre_body);
34453 else
34454 pre_body = this_pre_body;
34457 if (decl)
34458 real_decl = decl;
34459 if (cclauses != NULL
34460 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
34461 && real_decl != NULL_TREE)
34463 tree *c;
34464 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
34465 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
34466 && OMP_CLAUSE_DECL (*c) == real_decl)
34468 error_at (loc, "iteration variable %qD"
34469 " should not be firstprivate", real_decl);
34470 *c = OMP_CLAUSE_CHAIN (*c);
34472 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
34473 && OMP_CLAUSE_DECL (*c) == real_decl)
34475 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
34476 tree l = *c;
34477 *c = OMP_CLAUSE_CHAIN (*c);
34478 if (code == OMP_SIMD)
34480 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34481 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
34483 else
34485 OMP_CLAUSE_CHAIN (l) = clauses;
34486 clauses = l;
34488 add_private_clause = NULL_TREE;
34490 else
34492 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
34493 && OMP_CLAUSE_DECL (*c) == real_decl)
34494 add_private_clause = NULL_TREE;
34495 c = &OMP_CLAUSE_CHAIN (*c);
34499 if (add_private_clause)
34501 tree c;
34502 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
34504 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
34505 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
34506 && OMP_CLAUSE_DECL (c) == decl)
34507 break;
34508 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
34509 && OMP_CLAUSE_DECL (c) == decl)
34510 error_at (loc, "iteration variable %qD "
34511 "should not be firstprivate",
34512 decl);
34513 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
34514 && OMP_CLAUSE_DECL (c) == decl)
34515 error_at (loc, "iteration variable %qD should not be reduction",
34516 decl);
34518 if (c == NULL)
34520 if (code != OMP_SIMD)
34521 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
34522 else if (collapse == 1)
34523 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
34524 else
34525 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
34526 OMP_CLAUSE_DECL (c) = add_private_clause;
34527 c = finish_omp_clauses (c, C_ORT_OMP);
34528 if (c)
34530 OMP_CLAUSE_CHAIN (c) = clauses;
34531 clauses = c;
34532 /* For linear, signal that we need to fill up
34533 the so far unknown linear step. */
34534 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
34535 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
34540 cond = NULL;
34541 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34542 cond = cp_parser_omp_for_cond (parser, decl, code);
34543 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34545 incr = NULL;
34546 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
34548 /* If decl is an iterator, preserve the operator on decl
34549 until finish_omp_for. */
34550 if (real_decl
34551 && ((processing_template_decl
34552 && (TREE_TYPE (real_decl) == NULL_TREE
34553 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
34554 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
34555 incr = cp_parser_omp_for_incr (parser, real_decl);
34556 else
34557 incr = cp_parser_expression (parser);
34558 if (!EXPR_HAS_LOCATION (incr))
34559 protected_set_expr_location (incr, input_location);
34562 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
34563 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34564 /*or_comma=*/false,
34565 /*consume_paren=*/true);
34567 TREE_VEC_ELT (declv, i) = decl;
34568 TREE_VEC_ELT (initv, i) = init;
34569 TREE_VEC_ELT (condv, i) = cond;
34570 TREE_VEC_ELT (incrv, i) = incr;
34571 if (orig_init)
34573 orig_inits.safe_grow_cleared (i + 1);
34574 orig_inits[i] = orig_init;
34577 if (i == count - 1)
34578 break;
34580 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
34581 in between the collapsed for loops to be still considered perfectly
34582 nested. Hopefully the final version clarifies this.
34583 For now handle (multiple) {'s and empty statements. */
34584 cp_parser_parse_tentatively (parser);
34587 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34588 break;
34589 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34591 cp_lexer_consume_token (parser->lexer);
34592 bracecount++;
34594 else if (bracecount
34595 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34596 cp_lexer_consume_token (parser->lexer);
34597 else
34599 loc = cp_lexer_peek_token (parser->lexer)->location;
34600 error_at (loc, "not enough collapsed for loops");
34601 collapse_err = true;
34602 cp_parser_abort_tentative_parse (parser);
34603 declv = NULL_TREE;
34604 break;
34607 while (1);
34609 if (declv)
34611 cp_parser_parse_definitely (parser);
34612 nbraces += bracecount;
34616 if (nbraces)
34617 if_p = NULL;
34619 /* Note that we saved the original contents of this flag when we entered
34620 the structured block, and so we don't need to re-save it here. */
34621 if (code == CILK_SIMD || code == CILK_FOR)
34622 parser->in_statement = IN_CILK_SIMD_FOR;
34623 else
34624 parser->in_statement = IN_OMP_FOR;
34626 /* Note that the grammar doesn't call for a structured block here,
34627 though the loop as a whole is a structured block. */
34628 body = push_stmt_list ();
34629 cp_parser_statement (parser, NULL_TREE, false, if_p);
34630 body = pop_stmt_list (body);
34632 if (declv == NULL_TREE)
34633 ret = NULL_TREE;
34634 else
34635 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
34636 body, pre_body, &orig_inits, clauses);
34638 while (nbraces)
34640 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34642 cp_lexer_consume_token (parser->lexer);
34643 nbraces--;
34645 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34646 cp_lexer_consume_token (parser->lexer);
34647 else
34649 if (!collapse_err)
34651 error_at (cp_lexer_peek_token (parser->lexer)->location,
34652 "collapsed loops not perfectly nested");
34654 collapse_err = true;
34655 cp_parser_statement_seq_opt (parser, NULL);
34656 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
34657 break;
34661 while (!for_block->is_empty ())
34663 tree t = for_block->pop ();
34664 if (TREE_CODE (t) == STATEMENT_LIST)
34665 add_stmt (pop_stmt_list (t));
34666 else
34667 add_stmt (t);
34669 release_tree_vector (for_block);
34671 return ret;
34674 /* Helper function for OpenMP parsing, split clauses and call
34675 finish_omp_clauses on each of the set of clauses afterwards. */
34677 static void
34678 cp_omp_split_clauses (location_t loc, enum tree_code code,
34679 omp_clause_mask mask, tree clauses, tree *cclauses)
34681 int i;
34682 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
34683 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
34684 if (cclauses[i])
34685 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
34688 /* OpenMP 4.0:
34689 #pragma omp simd simd-clause[optseq] new-line
34690 for-loop */
34692 #define OMP_SIMD_CLAUSE_MASK \
34693 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
34694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
34695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
34697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34702 static tree
34703 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
34704 char *p_name, omp_clause_mask mask, tree *cclauses,
34705 bool *if_p)
34707 tree clauses, sb, ret;
34708 unsigned int save;
34709 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34711 strcat (p_name, " simd");
34712 mask |= OMP_SIMD_CLAUSE_MASK;
34714 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34715 cclauses == NULL);
34716 if (cclauses)
34718 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
34719 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
34720 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
34721 OMP_CLAUSE_ORDERED);
34722 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
34724 error_at (OMP_CLAUSE_LOCATION (c),
34725 "%<ordered%> clause with parameter may not be specified "
34726 "on %qs construct", p_name);
34727 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
34731 sb = begin_omp_structured_block ();
34732 save = cp_parser_begin_omp_structured_block (parser);
34734 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
34736 cp_parser_end_omp_structured_block (parser, save);
34737 add_stmt (finish_omp_structured_block (sb));
34739 return ret;
34742 /* OpenMP 2.5:
34743 #pragma omp for for-clause[optseq] new-line
34744 for-loop
34746 OpenMP 4.0:
34747 #pragma omp for simd for-simd-clause[optseq] new-line
34748 for-loop */
34750 #define OMP_FOR_CLAUSE_MASK \
34751 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
34757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
34758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
34759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34761 static tree
34762 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
34763 char *p_name, omp_clause_mask mask, tree *cclauses,
34764 bool *if_p)
34766 tree clauses, sb, ret;
34767 unsigned int save;
34768 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34770 strcat (p_name, " for");
34771 mask |= OMP_FOR_CLAUSE_MASK;
34772 /* parallel for{, simd} disallows nowait clause, but for
34773 target {teams distribute ,}parallel for{, simd} it should be accepted. */
34774 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
34775 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
34776 /* Composite distribute parallel for{, simd} disallows ordered clause. */
34777 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34778 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
34780 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34782 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34783 const char *p = IDENTIFIER_POINTER (id);
34785 if (strcmp (p, "simd") == 0)
34787 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34788 if (cclauses == NULL)
34789 cclauses = cclauses_buf;
34791 cp_lexer_consume_token (parser->lexer);
34792 if (!flag_openmp) /* flag_openmp_simd */
34793 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34794 cclauses, if_p);
34795 sb = begin_omp_structured_block ();
34796 save = cp_parser_begin_omp_structured_block (parser);
34797 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34798 cclauses, if_p);
34799 cp_parser_end_omp_structured_block (parser, save);
34800 tree body = finish_omp_structured_block (sb);
34801 if (ret == NULL)
34802 return ret;
34803 ret = make_node (OMP_FOR);
34804 TREE_TYPE (ret) = void_type_node;
34805 OMP_FOR_BODY (ret) = body;
34806 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34807 SET_EXPR_LOCATION (ret, loc);
34808 add_stmt (ret);
34809 return ret;
34812 if (!flag_openmp) /* flag_openmp_simd */
34814 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34815 return NULL_TREE;
34818 /* Composite distribute parallel for disallows linear clause. */
34819 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34820 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
34822 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34823 cclauses == NULL);
34824 if (cclauses)
34826 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
34827 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34830 sb = begin_omp_structured_block ();
34831 save = cp_parser_begin_omp_structured_block (parser);
34833 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
34835 cp_parser_end_omp_structured_block (parser, save);
34836 add_stmt (finish_omp_structured_block (sb));
34838 return ret;
34841 /* OpenMP 2.5:
34842 # pragma omp master new-line
34843 structured-block */
34845 static tree
34846 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34848 cp_parser_require_pragma_eol (parser, pragma_tok);
34849 return c_finish_omp_master (input_location,
34850 cp_parser_omp_structured_block (parser, if_p));
34853 /* OpenMP 2.5:
34854 # pragma omp ordered new-line
34855 structured-block
34857 OpenMP 4.5:
34858 # pragma omp ordered ordered-clauses new-line
34859 structured-block */
34861 #define OMP_ORDERED_CLAUSE_MASK \
34862 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
34863 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
34865 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
34866 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
34868 static bool
34869 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
34870 enum pragma_context context, bool *if_p)
34872 location_t loc = pragma_tok->location;
34874 if (context != pragma_stmt && context != pragma_compound)
34876 cp_parser_error (parser, "expected declaration specifiers");
34877 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34878 return false;
34881 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34883 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34884 const char *p = IDENTIFIER_POINTER (id);
34886 if (strcmp (p, "depend") == 0)
34888 if (context == pragma_stmt)
34890 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
34891 "%<depend%> clause may only be used in compound "
34892 "statements");
34893 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34894 return false;
34896 tree clauses
34897 = cp_parser_omp_all_clauses (parser,
34898 OMP_ORDERED_DEPEND_CLAUSE_MASK,
34899 "#pragma omp ordered", pragma_tok);
34900 c_finish_omp_ordered (loc, clauses, NULL_TREE);
34901 return false;
34905 tree clauses
34906 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
34907 "#pragma omp ordered", pragma_tok);
34908 c_finish_omp_ordered (loc, clauses,
34909 cp_parser_omp_structured_block (parser, if_p));
34910 return true;
34913 /* OpenMP 2.5:
34915 section-scope:
34916 { section-sequence }
34918 section-sequence:
34919 section-directive[opt] structured-block
34920 section-sequence section-directive structured-block */
34922 static tree
34923 cp_parser_omp_sections_scope (cp_parser *parser)
34925 tree stmt, substmt;
34926 bool error_suppress = false;
34927 cp_token *tok;
34929 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
34930 return NULL_TREE;
34932 stmt = push_stmt_list ();
34934 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
34935 != PRAGMA_OMP_SECTION)
34937 substmt = cp_parser_omp_structured_block (parser, NULL);
34938 substmt = build1 (OMP_SECTION, void_type_node, substmt);
34939 add_stmt (substmt);
34942 while (1)
34944 tok = cp_lexer_peek_token (parser->lexer);
34945 if (tok->type == CPP_CLOSE_BRACE)
34946 break;
34947 if (tok->type == CPP_EOF)
34948 break;
34950 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
34952 cp_lexer_consume_token (parser->lexer);
34953 cp_parser_require_pragma_eol (parser, tok);
34954 error_suppress = false;
34956 else if (!error_suppress)
34958 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
34959 error_suppress = true;
34962 substmt = cp_parser_omp_structured_block (parser, NULL);
34963 substmt = build1 (OMP_SECTION, void_type_node, substmt);
34964 add_stmt (substmt);
34966 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34968 substmt = pop_stmt_list (stmt);
34970 stmt = make_node (OMP_SECTIONS);
34971 TREE_TYPE (stmt) = void_type_node;
34972 OMP_SECTIONS_BODY (stmt) = substmt;
34974 add_stmt (stmt);
34975 return stmt;
34978 /* OpenMP 2.5:
34979 # pragma omp sections sections-clause[optseq] newline
34980 sections-scope */
34982 #define OMP_SECTIONS_CLAUSE_MASK \
34983 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34984 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34989 static tree
34990 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
34991 char *p_name, omp_clause_mask mask, tree *cclauses)
34993 tree clauses, ret;
34994 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34996 strcat (p_name, " sections");
34997 mask |= OMP_SECTIONS_CLAUSE_MASK;
34998 if (cclauses)
34999 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35001 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35002 cclauses == NULL);
35003 if (cclauses)
35005 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35006 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35009 ret = cp_parser_omp_sections_scope (parser);
35010 if (ret)
35011 OMP_SECTIONS_CLAUSES (ret) = clauses;
35013 return ret;
35016 /* OpenMP 2.5:
35017 # pragma omp parallel parallel-clause[optseq] new-line
35018 structured-block
35019 # pragma omp parallel for parallel-for-clause[optseq] new-line
35020 structured-block
35021 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35022 structured-block
35024 OpenMP 4.0:
35025 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35026 structured-block */
35028 #define OMP_PARALLEL_CLAUSE_MASK \
35029 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35031 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35032 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35033 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35034 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35039 static tree
35040 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35041 char *p_name, omp_clause_mask mask, tree *cclauses,
35042 bool *if_p)
35044 tree stmt, clauses, block;
35045 unsigned int save;
35046 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35048 strcat (p_name, " parallel");
35049 mask |= OMP_PARALLEL_CLAUSE_MASK;
35050 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35051 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35052 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35053 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35055 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35057 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35058 if (cclauses == NULL)
35059 cclauses = cclauses_buf;
35061 cp_lexer_consume_token (parser->lexer);
35062 if (!flag_openmp) /* flag_openmp_simd */
35063 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35064 if_p);
35065 block = begin_omp_parallel ();
35066 save = cp_parser_begin_omp_structured_block (parser);
35067 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35068 if_p);
35069 cp_parser_end_omp_structured_block (parser, save);
35070 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35071 block);
35072 if (ret == NULL_TREE)
35073 return ret;
35074 OMP_PARALLEL_COMBINED (stmt) = 1;
35075 return stmt;
35077 /* When combined with distribute, parallel has to be followed by for.
35078 #pragma omp target parallel is allowed though. */
35079 else if (cclauses
35080 && (mask & (OMP_CLAUSE_MASK_1
35081 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35083 error_at (loc, "expected %<for%> after %qs", p_name);
35084 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35085 return NULL_TREE;
35087 else if (!flag_openmp) /* flag_openmp_simd */
35089 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35090 return NULL_TREE;
35092 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35094 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35095 const char *p = IDENTIFIER_POINTER (id);
35096 if (strcmp (p, "sections") == 0)
35098 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35099 cclauses = cclauses_buf;
35101 cp_lexer_consume_token (parser->lexer);
35102 block = begin_omp_parallel ();
35103 save = cp_parser_begin_omp_structured_block (parser);
35104 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35105 cp_parser_end_omp_structured_block (parser, save);
35106 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35107 block);
35108 OMP_PARALLEL_COMBINED (stmt) = 1;
35109 return stmt;
35113 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35114 cclauses == NULL);
35115 if (cclauses)
35117 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35118 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35121 block = begin_omp_parallel ();
35122 save = cp_parser_begin_omp_structured_block (parser);
35123 cp_parser_statement (parser, NULL_TREE, false, if_p);
35124 cp_parser_end_omp_structured_block (parser, save);
35125 stmt = finish_omp_parallel (clauses, block);
35126 return stmt;
35129 /* OpenMP 2.5:
35130 # pragma omp single single-clause[optseq] new-line
35131 structured-block */
35133 #define OMP_SINGLE_CLAUSE_MASK \
35134 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35139 static tree
35140 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35142 tree stmt = make_node (OMP_SINGLE);
35143 TREE_TYPE (stmt) = void_type_node;
35145 OMP_SINGLE_CLAUSES (stmt)
35146 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35147 "#pragma omp single", pragma_tok);
35148 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35150 return add_stmt (stmt);
35153 /* OpenMP 3.0:
35154 # pragma omp task task-clause[optseq] new-line
35155 structured-block */
35157 #define OMP_TASK_CLAUSE_MASK \
35158 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35164 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35165 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35166 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35167 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35169 static tree
35170 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35172 tree clauses, block;
35173 unsigned int save;
35175 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35176 "#pragma omp task", pragma_tok);
35177 block = begin_omp_task ();
35178 save = cp_parser_begin_omp_structured_block (parser);
35179 cp_parser_statement (parser, NULL_TREE, false, if_p);
35180 cp_parser_end_omp_structured_block (parser, save);
35181 return finish_omp_task (clauses, block);
35184 /* OpenMP 3.0:
35185 # pragma omp taskwait new-line */
35187 static void
35188 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35190 cp_parser_require_pragma_eol (parser, pragma_tok);
35191 finish_omp_taskwait ();
35194 /* OpenMP 3.1:
35195 # pragma omp taskyield new-line */
35197 static void
35198 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35200 cp_parser_require_pragma_eol (parser, pragma_tok);
35201 finish_omp_taskyield ();
35204 /* OpenMP 4.0:
35205 # pragma omp taskgroup new-line
35206 structured-block */
35208 static tree
35209 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35211 cp_parser_require_pragma_eol (parser, pragma_tok);
35212 return c_finish_omp_taskgroup (input_location,
35213 cp_parser_omp_structured_block (parser,
35214 if_p));
35218 /* OpenMP 2.5:
35219 # pragma omp threadprivate (variable-list) */
35221 static void
35222 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
35224 tree vars;
35226 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35227 cp_parser_require_pragma_eol (parser, pragma_tok);
35229 finish_omp_threadprivate (vars);
35232 /* OpenMP 4.0:
35233 # pragma omp cancel cancel-clause[optseq] new-line */
35235 #define OMP_CANCEL_CLAUSE_MASK \
35236 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35237 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35238 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35240 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35242 static void
35243 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
35245 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
35246 "#pragma omp cancel", pragma_tok);
35247 finish_omp_cancel (clauses);
35250 /* OpenMP 4.0:
35251 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35253 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35254 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35255 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35256 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35257 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35259 static void
35260 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
35261 enum pragma_context context)
35263 tree clauses;
35264 bool point_seen = false;
35266 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35268 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35269 const char *p = IDENTIFIER_POINTER (id);
35271 if (strcmp (p, "point") == 0)
35273 cp_lexer_consume_token (parser->lexer);
35274 point_seen = true;
35277 if (!point_seen)
35279 cp_parser_error (parser, "expected %<point%>");
35280 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35281 return;
35284 if (context != pragma_compound)
35286 if (context == pragma_stmt)
35287 error_at (pragma_tok->location,
35288 "%<#pragma omp cancellation point%> may only be used in"
35289 " compound statements");
35290 else
35291 cp_parser_error (parser, "expected declaration specifiers");
35292 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35293 return;
35296 clauses = cp_parser_omp_all_clauses (parser,
35297 OMP_CANCELLATION_POINT_CLAUSE_MASK,
35298 "#pragma omp cancellation point",
35299 pragma_tok);
35300 finish_omp_cancellation_point (clauses);
35303 /* OpenMP 4.0:
35304 #pragma omp distribute distribute-clause[optseq] new-line
35305 for-loop */
35307 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35308 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35309 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35310 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35311 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35312 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35314 static tree
35315 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
35316 char *p_name, omp_clause_mask mask, tree *cclauses,
35317 bool *if_p)
35319 tree clauses, sb, ret;
35320 unsigned int save;
35321 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35323 strcat (p_name, " distribute");
35324 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
35326 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35328 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35329 const char *p = IDENTIFIER_POINTER (id);
35330 bool simd = false;
35331 bool parallel = false;
35333 if (strcmp (p, "simd") == 0)
35334 simd = true;
35335 else
35336 parallel = strcmp (p, "parallel") == 0;
35337 if (parallel || simd)
35339 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35340 if (cclauses == NULL)
35341 cclauses = cclauses_buf;
35342 cp_lexer_consume_token (parser->lexer);
35343 if (!flag_openmp) /* flag_openmp_simd */
35345 if (simd)
35346 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35347 cclauses, if_p);
35348 else
35349 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35350 cclauses, if_p);
35352 sb = begin_omp_structured_block ();
35353 save = cp_parser_begin_omp_structured_block (parser);
35354 if (simd)
35355 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35356 cclauses, if_p);
35357 else
35358 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35359 cclauses, if_p);
35360 cp_parser_end_omp_structured_block (parser, save);
35361 tree body = finish_omp_structured_block (sb);
35362 if (ret == NULL)
35363 return ret;
35364 ret = make_node (OMP_DISTRIBUTE);
35365 TREE_TYPE (ret) = void_type_node;
35366 OMP_FOR_BODY (ret) = body;
35367 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35368 SET_EXPR_LOCATION (ret, loc);
35369 add_stmt (ret);
35370 return ret;
35373 if (!flag_openmp) /* flag_openmp_simd */
35375 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35376 return NULL_TREE;
35379 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35380 cclauses == NULL);
35381 if (cclauses)
35383 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
35384 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35387 sb = begin_omp_structured_block ();
35388 save = cp_parser_begin_omp_structured_block (parser);
35390 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
35392 cp_parser_end_omp_structured_block (parser, save);
35393 add_stmt (finish_omp_structured_block (sb));
35395 return ret;
35398 /* OpenMP 4.0:
35399 # pragma omp teams teams-clause[optseq] new-line
35400 structured-block */
35402 #define OMP_TEAMS_CLAUSE_MASK \
35403 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
35408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
35409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
35411 static tree
35412 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
35413 char *p_name, omp_clause_mask mask, tree *cclauses,
35414 bool *if_p)
35416 tree clauses, sb, ret;
35417 unsigned int save;
35418 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35420 strcat (p_name, " teams");
35421 mask |= OMP_TEAMS_CLAUSE_MASK;
35423 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35425 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35426 const char *p = IDENTIFIER_POINTER (id);
35427 if (strcmp (p, "distribute") == 0)
35429 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35430 if (cclauses == NULL)
35431 cclauses = cclauses_buf;
35433 cp_lexer_consume_token (parser->lexer);
35434 if (!flag_openmp) /* flag_openmp_simd */
35435 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35436 cclauses, if_p);
35437 sb = begin_omp_structured_block ();
35438 save = cp_parser_begin_omp_structured_block (parser);
35439 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35440 cclauses, if_p);
35441 cp_parser_end_omp_structured_block (parser, save);
35442 tree body = finish_omp_structured_block (sb);
35443 if (ret == NULL)
35444 return ret;
35445 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35446 ret = make_node (OMP_TEAMS);
35447 TREE_TYPE (ret) = void_type_node;
35448 OMP_TEAMS_CLAUSES (ret) = clauses;
35449 OMP_TEAMS_BODY (ret) = body;
35450 OMP_TEAMS_COMBINED (ret) = 1;
35451 return add_stmt (ret);
35454 if (!flag_openmp) /* flag_openmp_simd */
35456 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35457 return NULL_TREE;
35460 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35461 cclauses == NULL);
35462 if (cclauses)
35464 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
35465 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35468 tree stmt = make_node (OMP_TEAMS);
35469 TREE_TYPE (stmt) = void_type_node;
35470 OMP_TEAMS_CLAUSES (stmt) = clauses;
35471 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35473 return add_stmt (stmt);
35476 /* OpenMP 4.0:
35477 # pragma omp target data target-data-clause[optseq] new-line
35478 structured-block */
35480 #define OMP_TARGET_DATA_CLAUSE_MASK \
35481 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35482 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35483 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35484 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
35486 static tree
35487 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35489 tree clauses
35490 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
35491 "#pragma omp target data", pragma_tok);
35492 int map_seen = 0;
35493 for (tree *pc = &clauses; *pc;)
35495 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35496 switch (OMP_CLAUSE_MAP_KIND (*pc))
35498 case GOMP_MAP_TO:
35499 case GOMP_MAP_ALWAYS_TO:
35500 case GOMP_MAP_FROM:
35501 case GOMP_MAP_ALWAYS_FROM:
35502 case GOMP_MAP_TOFROM:
35503 case GOMP_MAP_ALWAYS_TOFROM:
35504 case GOMP_MAP_ALLOC:
35505 map_seen = 3;
35506 break;
35507 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35508 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35509 case GOMP_MAP_ALWAYS_POINTER:
35510 break;
35511 default:
35512 map_seen |= 1;
35513 error_at (OMP_CLAUSE_LOCATION (*pc),
35514 "%<#pragma omp target data%> with map-type other "
35515 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35516 "on %<map%> clause");
35517 *pc = OMP_CLAUSE_CHAIN (*pc);
35518 continue;
35520 pc = &OMP_CLAUSE_CHAIN (*pc);
35523 if (map_seen != 3)
35525 if (map_seen == 0)
35526 error_at (pragma_tok->location,
35527 "%<#pragma omp target data%> must contain at least "
35528 "one %<map%> clause");
35529 return NULL_TREE;
35532 tree stmt = make_node (OMP_TARGET_DATA);
35533 TREE_TYPE (stmt) = void_type_node;
35534 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
35536 keep_next_level (true);
35537 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35539 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35540 return add_stmt (stmt);
35543 /* OpenMP 4.5:
35544 # pragma omp target enter data target-enter-data-clause[optseq] new-line
35545 structured-block */
35547 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
35548 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35549 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35554 static tree
35555 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
35556 enum pragma_context context)
35558 bool data_seen = false;
35559 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35561 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35562 const char *p = IDENTIFIER_POINTER (id);
35564 if (strcmp (p, "data") == 0)
35566 cp_lexer_consume_token (parser->lexer);
35567 data_seen = true;
35570 if (!data_seen)
35572 cp_parser_error (parser, "expected %<data%>");
35573 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35574 return NULL_TREE;
35577 if (context == pragma_stmt)
35579 error_at (pragma_tok->location,
35580 "%<#pragma omp target enter data%> may only be "
35581 "used in compound statements");
35582 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35583 return NULL_TREE;
35586 tree clauses
35587 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
35588 "#pragma omp target enter data", pragma_tok);
35589 int map_seen = 0;
35590 for (tree *pc = &clauses; *pc;)
35592 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35593 switch (OMP_CLAUSE_MAP_KIND (*pc))
35595 case GOMP_MAP_TO:
35596 case GOMP_MAP_ALWAYS_TO:
35597 case GOMP_MAP_ALLOC:
35598 map_seen = 3;
35599 break;
35600 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35601 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35602 case GOMP_MAP_ALWAYS_POINTER:
35603 break;
35604 default:
35605 map_seen |= 1;
35606 error_at (OMP_CLAUSE_LOCATION (*pc),
35607 "%<#pragma omp target enter data%> with map-type other "
35608 "than %<to%> or %<alloc%> on %<map%> clause");
35609 *pc = OMP_CLAUSE_CHAIN (*pc);
35610 continue;
35612 pc = &OMP_CLAUSE_CHAIN (*pc);
35615 if (map_seen != 3)
35617 if (map_seen == 0)
35618 error_at (pragma_tok->location,
35619 "%<#pragma omp target enter data%> must contain at least "
35620 "one %<map%> clause");
35621 return NULL_TREE;
35624 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
35625 TREE_TYPE (stmt) = void_type_node;
35626 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
35627 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35628 return add_stmt (stmt);
35631 /* OpenMP 4.5:
35632 # pragma omp target exit data target-enter-data-clause[optseq] new-line
35633 structured-block */
35635 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
35636 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35640 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35642 static tree
35643 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
35644 enum pragma_context context)
35646 bool data_seen = false;
35647 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35649 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35650 const char *p = IDENTIFIER_POINTER (id);
35652 if (strcmp (p, "data") == 0)
35654 cp_lexer_consume_token (parser->lexer);
35655 data_seen = true;
35658 if (!data_seen)
35660 cp_parser_error (parser, "expected %<data%>");
35661 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35662 return NULL_TREE;
35665 if (context == pragma_stmt)
35667 error_at (pragma_tok->location,
35668 "%<#pragma omp target exit data%> may only be "
35669 "used in compound statements");
35670 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35671 return NULL_TREE;
35674 tree clauses
35675 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
35676 "#pragma omp target exit data", pragma_tok);
35677 int map_seen = 0;
35678 for (tree *pc = &clauses; *pc;)
35680 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35681 switch (OMP_CLAUSE_MAP_KIND (*pc))
35683 case GOMP_MAP_FROM:
35684 case GOMP_MAP_ALWAYS_FROM:
35685 case GOMP_MAP_RELEASE:
35686 case GOMP_MAP_DELETE:
35687 map_seen = 3;
35688 break;
35689 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35690 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35691 case GOMP_MAP_ALWAYS_POINTER:
35692 break;
35693 default:
35694 map_seen |= 1;
35695 error_at (OMP_CLAUSE_LOCATION (*pc),
35696 "%<#pragma omp target exit data%> with map-type other "
35697 "than %<from%>, %<release%> or %<delete%> on %<map%>"
35698 " clause");
35699 *pc = OMP_CLAUSE_CHAIN (*pc);
35700 continue;
35702 pc = &OMP_CLAUSE_CHAIN (*pc);
35705 if (map_seen != 3)
35707 if (map_seen == 0)
35708 error_at (pragma_tok->location,
35709 "%<#pragma omp target exit data%> must contain at least "
35710 "one %<map%> clause");
35711 return NULL_TREE;
35714 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
35715 TREE_TYPE (stmt) = void_type_node;
35716 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
35717 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35718 return add_stmt (stmt);
35721 /* OpenMP 4.0:
35722 # pragma omp target update target-update-clause[optseq] new-line */
35724 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
35725 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
35726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
35727 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35728 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35729 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35730 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35732 static bool
35733 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
35734 enum pragma_context context)
35736 if (context == pragma_stmt)
35738 error_at (pragma_tok->location,
35739 "%<#pragma omp target update%> may only be "
35740 "used in compound statements");
35741 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35742 return false;
35745 tree clauses
35746 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
35747 "#pragma omp target update", pragma_tok);
35748 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
35749 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
35751 error_at (pragma_tok->location,
35752 "%<#pragma omp target update%> must contain at least one "
35753 "%<from%> or %<to%> clauses");
35754 return false;
35757 tree stmt = make_node (OMP_TARGET_UPDATE);
35758 TREE_TYPE (stmt) = void_type_node;
35759 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
35760 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35761 add_stmt (stmt);
35762 return false;
35765 /* OpenMP 4.0:
35766 # pragma omp target target-clause[optseq] new-line
35767 structured-block */
35769 #define OMP_TARGET_CLAUSE_MASK \
35770 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35771 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35772 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35773 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35774 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
35778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
35780 static bool
35781 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
35782 enum pragma_context context, bool *if_p)
35784 tree *pc = NULL, stmt;
35786 if (context != pragma_stmt && context != pragma_compound)
35788 cp_parser_error (parser, "expected declaration specifiers");
35789 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35790 return false;
35793 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35795 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35796 const char *p = IDENTIFIER_POINTER (id);
35797 enum tree_code ccode = ERROR_MARK;
35799 if (strcmp (p, "teams") == 0)
35800 ccode = OMP_TEAMS;
35801 else if (strcmp (p, "parallel") == 0)
35802 ccode = OMP_PARALLEL;
35803 else if (strcmp (p, "simd") == 0)
35804 ccode = OMP_SIMD;
35805 if (ccode != ERROR_MARK)
35807 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
35808 char p_name[sizeof ("#pragma omp target teams distribute "
35809 "parallel for simd")];
35811 cp_lexer_consume_token (parser->lexer);
35812 strcpy (p_name, "#pragma omp target");
35813 if (!flag_openmp) /* flag_openmp_simd */
35815 tree stmt;
35816 switch (ccode)
35818 case OMP_TEAMS:
35819 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
35820 OMP_TARGET_CLAUSE_MASK,
35821 cclauses, if_p);
35822 break;
35823 case OMP_PARALLEL:
35824 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
35825 OMP_TARGET_CLAUSE_MASK,
35826 cclauses, if_p);
35827 break;
35828 case OMP_SIMD:
35829 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
35830 OMP_TARGET_CLAUSE_MASK,
35831 cclauses, if_p);
35832 break;
35833 default:
35834 gcc_unreachable ();
35836 return stmt != NULL_TREE;
35838 keep_next_level (true);
35839 tree sb = begin_omp_structured_block (), ret;
35840 unsigned save = cp_parser_begin_omp_structured_block (parser);
35841 switch (ccode)
35843 case OMP_TEAMS:
35844 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
35845 OMP_TARGET_CLAUSE_MASK, cclauses,
35846 if_p);
35847 break;
35848 case OMP_PARALLEL:
35849 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
35850 OMP_TARGET_CLAUSE_MASK, cclauses,
35851 if_p);
35852 break;
35853 case OMP_SIMD:
35854 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
35855 OMP_TARGET_CLAUSE_MASK, cclauses,
35856 if_p);
35857 break;
35858 default:
35859 gcc_unreachable ();
35861 cp_parser_end_omp_structured_block (parser, save);
35862 tree body = finish_omp_structured_block (sb);
35863 if (ret == NULL_TREE)
35864 return false;
35865 if (ccode == OMP_TEAMS && !processing_template_decl)
35867 /* For combined target teams, ensure the num_teams and
35868 thread_limit clause expressions are evaluated on the host,
35869 before entering the target construct. */
35870 tree c;
35871 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35872 c; c = OMP_CLAUSE_CHAIN (c))
35873 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
35874 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
35875 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
35877 tree expr = OMP_CLAUSE_OPERAND (c, 0);
35878 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
35879 if (expr == error_mark_node)
35880 continue;
35881 tree tmp = TARGET_EXPR_SLOT (expr);
35882 add_stmt (expr);
35883 OMP_CLAUSE_OPERAND (c, 0) = expr;
35884 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
35885 OMP_CLAUSE_FIRSTPRIVATE);
35886 OMP_CLAUSE_DECL (tc) = tmp;
35887 OMP_CLAUSE_CHAIN (tc)
35888 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35889 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
35892 tree stmt = make_node (OMP_TARGET);
35893 TREE_TYPE (stmt) = void_type_node;
35894 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35895 OMP_TARGET_BODY (stmt) = body;
35896 OMP_TARGET_COMBINED (stmt) = 1;
35897 add_stmt (stmt);
35898 pc = &OMP_TARGET_CLAUSES (stmt);
35899 goto check_clauses;
35901 else if (!flag_openmp) /* flag_openmp_simd */
35903 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35904 return false;
35906 else if (strcmp (p, "data") == 0)
35908 cp_lexer_consume_token (parser->lexer);
35909 cp_parser_omp_target_data (parser, pragma_tok, if_p);
35910 return true;
35912 else if (strcmp (p, "enter") == 0)
35914 cp_lexer_consume_token (parser->lexer);
35915 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
35916 return false;
35918 else if (strcmp (p, "exit") == 0)
35920 cp_lexer_consume_token (parser->lexer);
35921 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
35922 return false;
35924 else if (strcmp (p, "update") == 0)
35926 cp_lexer_consume_token (parser->lexer);
35927 return cp_parser_omp_target_update (parser, pragma_tok, context);
35931 stmt = make_node (OMP_TARGET);
35932 TREE_TYPE (stmt) = void_type_node;
35934 OMP_TARGET_CLAUSES (stmt)
35935 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
35936 "#pragma omp target", pragma_tok);
35937 pc = &OMP_TARGET_CLAUSES (stmt);
35938 keep_next_level (true);
35939 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35941 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35942 add_stmt (stmt);
35944 check_clauses:
35945 while (*pc)
35947 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35948 switch (OMP_CLAUSE_MAP_KIND (*pc))
35950 case GOMP_MAP_TO:
35951 case GOMP_MAP_ALWAYS_TO:
35952 case GOMP_MAP_FROM:
35953 case GOMP_MAP_ALWAYS_FROM:
35954 case GOMP_MAP_TOFROM:
35955 case GOMP_MAP_ALWAYS_TOFROM:
35956 case GOMP_MAP_ALLOC:
35957 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35958 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35959 case GOMP_MAP_ALWAYS_POINTER:
35960 break;
35961 default:
35962 error_at (OMP_CLAUSE_LOCATION (*pc),
35963 "%<#pragma omp target%> with map-type other "
35964 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35965 "on %<map%> clause");
35966 *pc = OMP_CLAUSE_CHAIN (*pc);
35967 continue;
35969 pc = &OMP_CLAUSE_CHAIN (*pc);
35971 return true;
35974 /* OpenACC 2.0:
35975 # pragma acc cache (variable-list) new-line
35978 static tree
35979 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
35981 tree stmt, clauses;
35983 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
35984 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
35986 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
35988 stmt = make_node (OACC_CACHE);
35989 TREE_TYPE (stmt) = void_type_node;
35990 OACC_CACHE_CLAUSES (stmt) = clauses;
35991 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35992 add_stmt (stmt);
35994 return stmt;
35997 /* OpenACC 2.0:
35998 # pragma acc data oacc-data-clause[optseq] new-line
35999 structured-block */
36001 #define OACC_DATA_CLAUSE_MASK \
36002 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36003 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36004 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36005 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36008 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36009 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36014 static tree
36015 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36017 tree stmt, clauses, block;
36018 unsigned int save;
36020 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36021 "#pragma acc data", pragma_tok);
36023 block = begin_omp_parallel ();
36024 save = cp_parser_begin_omp_structured_block (parser);
36025 cp_parser_statement (parser, NULL_TREE, false, if_p);
36026 cp_parser_end_omp_structured_block (parser, save);
36027 stmt = finish_oacc_data (clauses, block);
36028 return stmt;
36031 /* OpenACC 2.0:
36032 # pragma acc host_data <clauses> new-line
36033 structured-block */
36035 #define OACC_HOST_DATA_CLAUSE_MASK \
36036 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36038 static tree
36039 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36041 tree stmt, clauses, block;
36042 unsigned int save;
36044 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36045 "#pragma acc host_data", pragma_tok);
36047 block = begin_omp_parallel ();
36048 save = cp_parser_begin_omp_structured_block (parser);
36049 cp_parser_statement (parser, NULL_TREE, false, if_p);
36050 cp_parser_end_omp_structured_block (parser, save);
36051 stmt = finish_oacc_host_data (clauses, block);
36052 return stmt;
36055 /* OpenACC 2.0:
36056 # pragma acc declare oacc-data-clause[optseq] new-line
36059 #define OACC_DECLARE_CLAUSE_MASK \
36060 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36073 static tree
36074 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36076 tree clauses, stmt;
36077 bool error = false;
36079 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36080 "#pragma acc declare", pragma_tok, true);
36083 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36085 error_at (pragma_tok->location,
36086 "no valid clauses specified in %<#pragma acc declare%>");
36087 return NULL_TREE;
36090 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36092 location_t loc = OMP_CLAUSE_LOCATION (t);
36093 tree decl = OMP_CLAUSE_DECL (t);
36094 if (!DECL_P (decl))
36096 error_at (loc, "array section in %<#pragma acc declare%>");
36097 error = true;
36098 continue;
36100 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36101 switch (OMP_CLAUSE_MAP_KIND (t))
36103 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36104 case GOMP_MAP_FORCE_ALLOC:
36105 case GOMP_MAP_FORCE_TO:
36106 case GOMP_MAP_FORCE_DEVICEPTR:
36107 case GOMP_MAP_DEVICE_RESIDENT:
36108 break;
36110 case GOMP_MAP_LINK:
36111 if (!global_bindings_p ()
36112 && (TREE_STATIC (decl)
36113 || !DECL_EXTERNAL (decl)))
36115 error_at (loc,
36116 "%qD must be a global variable in"
36117 "%<#pragma acc declare link%>",
36118 decl);
36119 error = true;
36120 continue;
36122 break;
36124 default:
36125 if (global_bindings_p ())
36127 error_at (loc, "invalid OpenACC clause at file scope");
36128 error = true;
36129 continue;
36131 if (DECL_EXTERNAL (decl))
36133 error_at (loc,
36134 "invalid use of %<extern%> variable %qD "
36135 "in %<#pragma acc declare%>", decl);
36136 error = true;
36137 continue;
36139 else if (TREE_PUBLIC (decl))
36141 error_at (loc,
36142 "invalid use of %<global%> variable %qD "
36143 "in %<#pragma acc declare%>", decl);
36144 error = true;
36145 continue;
36147 break;
36150 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36151 || lookup_attribute ("omp declare target link",
36152 DECL_ATTRIBUTES (decl)))
36154 error_at (loc, "variable %qD used more than once with "
36155 "%<#pragma acc declare%>", decl);
36156 error = true;
36157 continue;
36160 if (!error)
36162 tree id;
36164 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36165 id = get_identifier ("omp declare target link");
36166 else
36167 id = get_identifier ("omp declare target");
36169 DECL_ATTRIBUTES (decl)
36170 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36171 if (global_bindings_p ())
36173 symtab_node *node = symtab_node::get (decl);
36174 if (node != NULL)
36176 node->offloadable = 1;
36177 if (ENABLE_OFFLOADING)
36179 g->have_offload = true;
36180 if (is_a <varpool_node *> (node))
36181 vec_safe_push (offload_vars, decl);
36188 if (error || global_bindings_p ())
36189 return NULL_TREE;
36191 stmt = make_node (OACC_DECLARE);
36192 TREE_TYPE (stmt) = void_type_node;
36193 OACC_DECLARE_CLAUSES (stmt) = clauses;
36194 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36196 add_stmt (stmt);
36198 return NULL_TREE;
36201 /* OpenACC 2.0:
36202 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36206 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36208 LOC is the location of the #pragma token.
36211 #define OACC_ENTER_DATA_CLAUSE_MASK \
36212 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36220 #define OACC_EXIT_DATA_CLAUSE_MASK \
36221 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36227 static tree
36228 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
36229 bool enter)
36231 location_t loc = pragma_tok->location;
36232 tree stmt, clauses;
36233 const char *p = "";
36235 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36236 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36238 if (strcmp (p, "data") != 0)
36240 error_at (loc, enter
36241 ? "expected %<data%> after %<#pragma acc enter%>"
36242 : "expected %<data%> after %<#pragma acc exit%>");
36243 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36244 return NULL_TREE;
36247 cp_lexer_consume_token (parser->lexer);
36249 if (enter)
36250 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
36251 "#pragma acc enter data", pragma_tok);
36252 else
36253 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
36254 "#pragma acc exit data", pragma_tok);
36256 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36258 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
36259 enter ? "enter" : "exit");
36260 return NULL_TREE;
36263 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
36264 TREE_TYPE (stmt) = void_type_node;
36265 OMP_STANDALONE_CLAUSES (stmt) = clauses;
36266 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36267 add_stmt (stmt);
36268 return stmt;
36271 /* OpenACC 2.0:
36272 # pragma acc loop oacc-loop-clause[optseq] new-line
36273 structured-block */
36275 #define OACC_LOOP_CLAUSE_MASK \
36276 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36285 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36287 static tree
36288 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
36289 omp_clause_mask mask, tree *cclauses, bool *if_p)
36291 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
36293 strcat (p_name, " loop");
36294 mask |= OACC_LOOP_CLAUSE_MASK;
36296 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
36297 cclauses == NULL);
36298 if (cclauses)
36300 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
36301 if (*cclauses)
36302 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
36303 if (clauses)
36304 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36307 tree block = begin_omp_structured_block ();
36308 int save = cp_parser_begin_omp_structured_block (parser);
36309 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
36310 cp_parser_end_omp_structured_block (parser, save);
36311 add_stmt (finish_omp_structured_block (block));
36313 return stmt;
36316 /* OpenACC 2.0:
36317 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36318 structured-block
36322 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36323 structured-block
36326 #define OACC_KERNELS_CLAUSE_MASK \
36327 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36328 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36329 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36338 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36339 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36342 #define OACC_PARALLEL_CLAUSE_MASK \
36343 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36349 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36350 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
36351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36353 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36354 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36355 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36358 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36359 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36360 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36361 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36362 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36364 static tree
36365 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
36366 char *p_name, bool *if_p)
36368 omp_clause_mask mask;
36369 enum tree_code code;
36370 switch (cp_parser_pragma_kind (pragma_tok))
36372 case PRAGMA_OACC_KERNELS:
36373 strcat (p_name, " kernels");
36374 mask = OACC_KERNELS_CLAUSE_MASK;
36375 code = OACC_KERNELS;
36376 break;
36377 case PRAGMA_OACC_PARALLEL:
36378 strcat (p_name, " parallel");
36379 mask = OACC_PARALLEL_CLAUSE_MASK;
36380 code = OACC_PARALLEL;
36381 break;
36382 default:
36383 gcc_unreachable ();
36386 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36388 const char *p
36389 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36390 if (strcmp (p, "loop") == 0)
36392 cp_lexer_consume_token (parser->lexer);
36393 tree block = begin_omp_parallel ();
36394 tree clauses;
36395 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
36396 if_p);
36397 return finish_omp_construct (code, block, clauses);
36401 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
36403 tree block = begin_omp_parallel ();
36404 unsigned int save = cp_parser_begin_omp_structured_block (parser);
36405 cp_parser_statement (parser, NULL_TREE, false, if_p);
36406 cp_parser_end_omp_structured_block (parser, save);
36407 return finish_omp_construct (code, block, clauses);
36410 /* OpenACC 2.0:
36411 # pragma acc update oacc-update-clause[optseq] new-line
36414 #define OACC_UPDATE_CLAUSE_MASK \
36415 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
36417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
36418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
36420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
36422 static tree
36423 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
36425 tree stmt, clauses;
36427 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
36428 "#pragma acc update", pragma_tok);
36430 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36432 error_at (pragma_tok->location,
36433 "%<#pragma acc update%> must contain at least one "
36434 "%<device%> or %<host%> or %<self%> clause");
36435 return NULL_TREE;
36438 stmt = make_node (OACC_UPDATE);
36439 TREE_TYPE (stmt) = void_type_node;
36440 OACC_UPDATE_CLAUSES (stmt) = clauses;
36441 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36442 add_stmt (stmt);
36443 return stmt;
36446 /* OpenACC 2.0:
36447 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
36449 LOC is the location of the #pragma token.
36452 #define OACC_WAIT_CLAUSE_MASK \
36453 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
36455 static tree
36456 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
36458 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
36459 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36461 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36462 list = cp_parser_oacc_wait_list (parser, loc, list);
36464 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
36465 "#pragma acc wait", pragma_tok);
36467 stmt = c_finish_oacc_wait (loc, list, clauses);
36468 stmt = finish_expr_stmt (stmt);
36470 return stmt;
36473 /* OpenMP 4.0:
36474 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
36476 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
36477 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36478 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36479 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36480 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
36481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
36482 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
36484 static void
36485 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
36486 enum pragma_context context)
36488 bool first_p = parser->omp_declare_simd == NULL;
36489 cp_omp_declare_simd_data data;
36490 if (first_p)
36492 data.error_seen = false;
36493 data.fndecl_seen = false;
36494 data.tokens = vNULL;
36495 data.clauses = NULL_TREE;
36496 /* It is safe to take the address of a local variable; it will only be
36497 used while this scope is live. */
36498 parser->omp_declare_simd = &data;
36501 /* Store away all pragma tokens. */
36502 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36503 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36504 cp_lexer_consume_token (parser->lexer);
36505 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36506 parser->omp_declare_simd->error_seen = true;
36507 cp_parser_require_pragma_eol (parser, pragma_tok);
36508 struct cp_token_cache *cp
36509 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
36510 parser->omp_declare_simd->tokens.safe_push (cp);
36512 if (first_p)
36514 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
36515 cp_parser_pragma (parser, context, NULL);
36516 switch (context)
36518 case pragma_external:
36519 cp_parser_declaration (parser);
36520 break;
36521 case pragma_member:
36522 cp_parser_member_declaration (parser);
36523 break;
36524 case pragma_objc_icode:
36525 cp_parser_block_declaration (parser, /*statement_p=*/false);
36526 break;
36527 default:
36528 cp_parser_declaration_statement (parser);
36529 break;
36531 if (parser->omp_declare_simd
36532 && !parser->omp_declare_simd->error_seen
36533 && !parser->omp_declare_simd->fndecl_seen)
36534 error_at (pragma_tok->location,
36535 "%<#pragma omp declare simd%> not immediately followed by "
36536 "function declaration or definition");
36537 data.tokens.release ();
36538 parser->omp_declare_simd = NULL;
36542 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
36543 This function is modelled similar to the late parsing of omp declare
36544 simd. */
36546 static tree
36547 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
36549 struct cp_token_cache *ce;
36550 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
36551 int ii = 0;
36553 if (parser->omp_declare_simd != NULL
36554 || lookup_attribute ("simd", attrs))
36556 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
36557 "used in the same function marked as a Cilk Plus SIMD-enabled "
36558 "function");
36559 parser->cilk_simd_fn_info->tokens.release ();
36560 XDELETE (parser->cilk_simd_fn_info);
36561 parser->cilk_simd_fn_info = NULL;
36562 return attrs;
36564 if (!info->error_seen && info->fndecl_seen)
36566 error ("vector attribute not immediately followed by a single function"
36567 " declaration or definition");
36568 info->error_seen = true;
36570 if (info->error_seen)
36571 return attrs;
36573 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
36575 tree c, cl;
36577 cp_parser_push_lexer_for_tokens (parser, ce);
36578 parser->lexer->in_pragma = true;
36579 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
36580 "SIMD-enabled functions attribute",
36581 NULL);
36582 cp_parser_pop_lexer (parser);
36583 if (cl)
36584 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
36586 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
36587 TREE_CHAIN (c) = attrs;
36588 attrs = c;
36590 c = build_tree_list (get_identifier ("omp declare simd"), cl);
36591 TREE_CHAIN (c) = attrs;
36592 if (processing_template_decl)
36593 ATTR_IS_DEPENDENT (c) = 1;
36594 attrs = c;
36596 info->fndecl_seen = true;
36597 parser->cilk_simd_fn_info->tokens.release ();
36598 XDELETE (parser->cilk_simd_fn_info);
36599 parser->cilk_simd_fn_info = NULL;
36600 return attrs;
36603 /* Finalize #pragma omp declare simd clauses after direct declarator has
36604 been parsed, and put that into "omp declare simd" attribute. */
36606 static tree
36607 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
36609 struct cp_token_cache *ce;
36610 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
36611 int i;
36613 if (!data->error_seen && data->fndecl_seen)
36615 error ("%<#pragma omp declare simd%> not immediately followed by "
36616 "a single function declaration or definition");
36617 data->error_seen = true;
36619 if (data->error_seen)
36620 return attrs;
36622 FOR_EACH_VEC_ELT (data->tokens, i, ce)
36624 tree c, cl;
36626 cp_parser_push_lexer_for_tokens (parser, ce);
36627 parser->lexer->in_pragma = true;
36628 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
36629 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
36630 cp_lexer_consume_token (parser->lexer);
36631 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
36632 "#pragma omp declare simd", pragma_tok);
36633 cp_parser_pop_lexer (parser);
36634 if (cl)
36635 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
36636 c = build_tree_list (get_identifier ("omp declare simd"), cl);
36637 TREE_CHAIN (c) = attrs;
36638 if (processing_template_decl)
36639 ATTR_IS_DEPENDENT (c) = 1;
36640 attrs = c;
36643 data->fndecl_seen = true;
36644 return attrs;
36648 /* OpenMP 4.0:
36649 # pragma omp declare target new-line
36650 declarations and definitions
36651 # pragma omp end declare target new-line
36653 OpenMP 4.5:
36654 # pragma omp declare target ( extended-list ) new-line
36656 # pragma omp declare target declare-target-clauses[seq] new-line */
36658 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
36659 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
36662 static void
36663 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
36665 tree clauses = NULL_TREE;
36666 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36667 clauses
36668 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
36669 "#pragma omp declare target", pragma_tok);
36670 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36672 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
36673 clauses);
36674 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
36675 cp_parser_require_pragma_eol (parser, pragma_tok);
36677 else
36679 cp_parser_require_pragma_eol (parser, pragma_tok);
36680 scope_chain->omp_declare_target_attribute++;
36681 return;
36683 if (scope_chain->omp_declare_target_attribute)
36684 error_at (pragma_tok->location,
36685 "%<#pragma omp declare target%> with clauses in between "
36686 "%<#pragma omp declare target%> without clauses and "
36687 "%<#pragma omp end declare target%>");
36688 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
36690 tree t = OMP_CLAUSE_DECL (c), id;
36691 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
36692 tree at2 = lookup_attribute ("omp declare target link",
36693 DECL_ATTRIBUTES (t));
36694 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
36696 id = get_identifier ("omp declare target link");
36697 std::swap (at1, at2);
36699 else
36700 id = get_identifier ("omp declare target");
36701 if (at2)
36703 error_at (OMP_CLAUSE_LOCATION (c),
36704 "%qD specified both in declare target %<link%> and %<to%>"
36705 " clauses", t);
36706 continue;
36708 if (!at1)
36710 symtab_node *node = symtab_node::get (t);
36711 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
36712 if (node != NULL)
36714 node->offloadable = 1;
36715 if (ENABLE_OFFLOADING)
36717 g->have_offload = true;
36718 if (is_a <varpool_node *> (node))
36719 vec_safe_push (offload_vars, t);
36726 static void
36727 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
36729 const char *p = "";
36730 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36732 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36733 p = IDENTIFIER_POINTER (id);
36735 if (strcmp (p, "declare") == 0)
36737 cp_lexer_consume_token (parser->lexer);
36738 p = "";
36739 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36741 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36742 p = IDENTIFIER_POINTER (id);
36744 if (strcmp (p, "target") == 0)
36745 cp_lexer_consume_token (parser->lexer);
36746 else
36748 cp_parser_error (parser, "expected %<target%>");
36749 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36750 return;
36753 else
36755 cp_parser_error (parser, "expected %<declare%>");
36756 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36757 return;
36759 cp_parser_require_pragma_eol (parser, pragma_tok);
36760 if (!scope_chain->omp_declare_target_attribute)
36761 error_at (pragma_tok->location,
36762 "%<#pragma omp end declare target%> without corresponding "
36763 "%<#pragma omp declare target%>");
36764 else
36765 scope_chain->omp_declare_target_attribute--;
36768 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
36769 expression and optional initializer clause of
36770 #pragma omp declare reduction. We store the expression(s) as
36771 either 3, 6 or 7 special statements inside of the artificial function's
36772 body. The first two statements are DECL_EXPRs for the artificial
36773 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
36774 expression that uses those variables.
36775 If there was any INITIALIZER clause, this is followed by further statements,
36776 the fourth and fifth statements are DECL_EXPRs for the artificial
36777 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
36778 constructor variant (first token after open paren is not omp_priv),
36779 then the sixth statement is a statement with the function call expression
36780 that uses the OMP_PRIV and optionally OMP_ORIG variable.
36781 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
36782 to initialize the OMP_PRIV artificial variable and there is seventh
36783 statement, a DECL_EXPR of the OMP_PRIV statement again. */
36785 static bool
36786 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
36788 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
36789 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
36790 type = TREE_TYPE (type);
36791 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
36792 DECL_ARTIFICIAL (omp_out) = 1;
36793 pushdecl (omp_out);
36794 add_decl_expr (omp_out);
36795 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
36796 DECL_ARTIFICIAL (omp_in) = 1;
36797 pushdecl (omp_in);
36798 add_decl_expr (omp_in);
36799 tree combiner;
36800 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
36802 keep_next_level (true);
36803 tree block = begin_omp_structured_block ();
36804 combiner = cp_parser_expression (parser);
36805 finish_expr_stmt (combiner);
36806 block = finish_omp_structured_block (block);
36807 add_stmt (block);
36809 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36810 return false;
36812 const char *p = "";
36813 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36815 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36816 p = IDENTIFIER_POINTER (id);
36819 if (strcmp (p, "initializer") == 0)
36821 cp_lexer_consume_token (parser->lexer);
36822 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36823 return false;
36825 p = "";
36826 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36828 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36829 p = IDENTIFIER_POINTER (id);
36832 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
36833 DECL_ARTIFICIAL (omp_priv) = 1;
36834 pushdecl (omp_priv);
36835 add_decl_expr (omp_priv);
36836 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
36837 DECL_ARTIFICIAL (omp_orig) = 1;
36838 pushdecl (omp_orig);
36839 add_decl_expr (omp_orig);
36841 keep_next_level (true);
36842 block = begin_omp_structured_block ();
36844 bool ctor = false;
36845 if (strcmp (p, "omp_priv") == 0)
36847 bool is_direct_init, is_non_constant_init;
36848 ctor = true;
36849 cp_lexer_consume_token (parser->lexer);
36850 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
36851 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
36852 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36853 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
36854 == CPP_CLOSE_PAREN
36855 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
36856 == CPP_CLOSE_PAREN))
36858 finish_omp_structured_block (block);
36859 error ("invalid initializer clause");
36860 return false;
36862 initializer = cp_parser_initializer (parser, &is_direct_init,
36863 &is_non_constant_init);
36864 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
36865 NULL_TREE, LOOKUP_ONLYCONVERTING);
36867 else
36869 cp_parser_parse_tentatively (parser);
36870 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
36871 /*check_dependency_p=*/true,
36872 /*template_p=*/NULL,
36873 /*declarator_p=*/false,
36874 /*optional_p=*/false);
36875 vec<tree, va_gc> *args;
36876 if (fn_name == error_mark_node
36877 || cp_parser_error_occurred (parser)
36878 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36879 || ((args = cp_parser_parenthesized_expression_list
36880 (parser, non_attr, /*cast_p=*/false,
36881 /*allow_expansion_p=*/true,
36882 /*non_constant_p=*/NULL)),
36883 cp_parser_error_occurred (parser)))
36885 finish_omp_structured_block (block);
36886 cp_parser_abort_tentative_parse (parser);
36887 cp_parser_error (parser, "expected id-expression (arguments)");
36888 return false;
36890 unsigned int i;
36891 tree arg;
36892 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
36893 if (arg == omp_priv
36894 || (TREE_CODE (arg) == ADDR_EXPR
36895 && TREE_OPERAND (arg, 0) == omp_priv))
36896 break;
36897 cp_parser_abort_tentative_parse (parser);
36898 if (arg == NULL_TREE)
36899 error ("one of the initializer call arguments should be %<omp_priv%>"
36900 " or %<&omp_priv%>");
36901 initializer = cp_parser_postfix_expression (parser, false, false, false,
36902 false, NULL);
36903 finish_expr_stmt (initializer);
36906 block = finish_omp_structured_block (block);
36907 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
36908 add_stmt (block);
36910 if (ctor)
36911 add_decl_expr (omp_orig);
36913 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36914 return false;
36917 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
36918 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
36920 return true;
36923 /* OpenMP 4.0
36924 #pragma omp declare reduction (reduction-id : typename-list : expression) \
36925 initializer-clause[opt] new-line
36927 initializer-clause:
36928 initializer (omp_priv initializer)
36929 initializer (function-name (argument-list)) */
36931 static void
36932 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
36933 enum pragma_context)
36935 auto_vec<tree> types;
36936 enum tree_code reduc_code = ERROR_MARK;
36937 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
36938 unsigned int i;
36939 cp_token *first_token;
36940 cp_token_cache *cp;
36941 int errs;
36942 void *p;
36944 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
36945 p = obstack_alloc (&declarator_obstack, 0);
36947 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36948 goto fail;
36950 switch (cp_lexer_peek_token (parser->lexer)->type)
36952 case CPP_PLUS:
36953 reduc_code = PLUS_EXPR;
36954 break;
36955 case CPP_MULT:
36956 reduc_code = MULT_EXPR;
36957 break;
36958 case CPP_MINUS:
36959 reduc_code = MINUS_EXPR;
36960 break;
36961 case CPP_AND:
36962 reduc_code = BIT_AND_EXPR;
36963 break;
36964 case CPP_XOR:
36965 reduc_code = BIT_XOR_EXPR;
36966 break;
36967 case CPP_OR:
36968 reduc_code = BIT_IOR_EXPR;
36969 break;
36970 case CPP_AND_AND:
36971 reduc_code = TRUTH_ANDIF_EXPR;
36972 break;
36973 case CPP_OR_OR:
36974 reduc_code = TRUTH_ORIF_EXPR;
36975 break;
36976 case CPP_NAME:
36977 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
36978 break;
36979 default:
36980 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
36981 "%<|%>, %<&&%>, %<||%> or identifier");
36982 goto fail;
36985 if (reduc_code != ERROR_MARK)
36986 cp_lexer_consume_token (parser->lexer);
36988 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
36989 if (reduc_id == error_mark_node)
36990 goto fail;
36992 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36993 goto fail;
36995 /* Types may not be defined in declare reduction type list. */
36996 const char *saved_message;
36997 saved_message = parser->type_definition_forbidden_message;
36998 parser->type_definition_forbidden_message
36999 = G_("types may not be defined in declare reduction type list");
37000 bool saved_colon_corrects_to_scope_p;
37001 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37002 parser->colon_corrects_to_scope_p = false;
37003 bool saved_colon_doesnt_start_class_def_p;
37004 saved_colon_doesnt_start_class_def_p
37005 = parser->colon_doesnt_start_class_def_p;
37006 parser->colon_doesnt_start_class_def_p = true;
37008 while (true)
37010 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37011 type = cp_parser_type_id (parser);
37012 if (type == error_mark_node)
37014 else if (ARITHMETIC_TYPE_P (type)
37015 && (orig_reduc_id == NULL_TREE
37016 || (TREE_CODE (type) != COMPLEX_TYPE
37017 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
37018 "min") == 0
37019 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
37020 "max") == 0))))
37021 error_at (loc, "predeclared arithmetic type %qT in "
37022 "%<#pragma omp declare reduction%>", type);
37023 else if (TREE_CODE (type) == FUNCTION_TYPE
37024 || TREE_CODE (type) == METHOD_TYPE
37025 || TREE_CODE (type) == ARRAY_TYPE)
37026 error_at (loc, "function or array type %qT in "
37027 "%<#pragma omp declare reduction%>", type);
37028 else if (TREE_CODE (type) == REFERENCE_TYPE)
37029 error_at (loc, "reference type %qT in "
37030 "%<#pragma omp declare reduction%>", type);
37031 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37032 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37033 "%<#pragma omp declare reduction%>", type);
37034 else
37035 types.safe_push (type);
37037 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37038 cp_lexer_consume_token (parser->lexer);
37039 else
37040 break;
37043 /* Restore the saved message. */
37044 parser->type_definition_forbidden_message = saved_message;
37045 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37046 parser->colon_doesnt_start_class_def_p
37047 = saved_colon_doesnt_start_class_def_p;
37049 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37050 || types.is_empty ())
37052 fail:
37053 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37054 goto done;
37057 first_token = cp_lexer_peek_token (parser->lexer);
37058 cp = NULL;
37059 errs = errorcount;
37060 FOR_EACH_VEC_ELT (types, i, type)
37062 tree fntype
37063 = build_function_type_list (void_type_node,
37064 cp_build_reference_type (type, false),
37065 NULL_TREE);
37066 tree this_reduc_id = reduc_id;
37067 if (!dependent_type_p (type))
37068 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37069 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37070 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37071 DECL_ARTIFICIAL (fndecl) = 1;
37072 DECL_EXTERNAL (fndecl) = 1;
37073 DECL_DECLARED_INLINE_P (fndecl) = 1;
37074 DECL_IGNORED_P (fndecl) = 1;
37075 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37076 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37077 DECL_ATTRIBUTES (fndecl)
37078 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37079 DECL_ATTRIBUTES (fndecl));
37080 if (processing_template_decl)
37081 fndecl = push_template_decl (fndecl);
37082 bool block_scope = false;
37083 tree block = NULL_TREE;
37084 if (current_function_decl)
37086 block_scope = true;
37087 DECL_CONTEXT (fndecl) = global_namespace;
37088 if (!processing_template_decl)
37089 pushdecl (fndecl);
37091 else if (current_class_type)
37093 if (cp == NULL)
37095 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37096 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37097 cp_lexer_consume_token (parser->lexer);
37098 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37099 goto fail;
37100 cp = cp_token_cache_new (first_token,
37101 cp_lexer_peek_nth_token (parser->lexer,
37102 2));
37104 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37105 finish_member_declaration (fndecl);
37106 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37107 DECL_PENDING_INLINE_P (fndecl) = 1;
37108 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37109 continue;
37111 else
37113 DECL_CONTEXT (fndecl) = current_namespace;
37114 pushdecl (fndecl);
37116 if (!block_scope)
37117 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37118 else
37119 block = begin_omp_structured_block ();
37120 if (cp)
37122 cp_parser_push_lexer_for_tokens (parser, cp);
37123 parser->lexer->in_pragma = true;
37125 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37127 if (!block_scope)
37128 finish_function (0);
37129 else
37130 DECL_CONTEXT (fndecl) = current_function_decl;
37131 if (cp)
37132 cp_parser_pop_lexer (parser);
37133 goto fail;
37135 if (cp)
37136 cp_parser_pop_lexer (parser);
37137 if (!block_scope)
37138 finish_function (0);
37139 else
37141 DECL_CONTEXT (fndecl) = current_function_decl;
37142 block = finish_omp_structured_block (block);
37143 if (TREE_CODE (block) == BIND_EXPR)
37144 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37145 else if (TREE_CODE (block) == STATEMENT_LIST)
37146 DECL_SAVED_TREE (fndecl) = block;
37147 if (processing_template_decl)
37148 add_decl_expr (fndecl);
37150 cp_check_omp_declare_reduction (fndecl);
37151 if (cp == NULL && types.length () > 1)
37152 cp = cp_token_cache_new (first_token,
37153 cp_lexer_peek_nth_token (parser->lexer, 2));
37154 if (errs != errorcount)
37155 break;
37158 cp_parser_require_pragma_eol (parser, pragma_tok);
37160 done:
37161 /* Free any declarators allocated. */
37162 obstack_free (&declarator_obstack, p);
37165 /* OpenMP 4.0
37166 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37167 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37168 initializer-clause[opt] new-line
37169 #pragma omp declare target new-line */
37171 static void
37172 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37173 enum pragma_context context)
37175 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37177 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37178 const char *p = IDENTIFIER_POINTER (id);
37180 if (strcmp (p, "simd") == 0)
37182 cp_lexer_consume_token (parser->lexer);
37183 cp_parser_omp_declare_simd (parser, pragma_tok,
37184 context);
37185 return;
37187 cp_ensure_no_omp_declare_simd (parser);
37188 if (strcmp (p, "reduction") == 0)
37190 cp_lexer_consume_token (parser->lexer);
37191 cp_parser_omp_declare_reduction (parser, pragma_tok,
37192 context);
37193 return;
37195 if (!flag_openmp) /* flag_openmp_simd */
37197 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37198 return;
37200 if (strcmp (p, "target") == 0)
37202 cp_lexer_consume_token (parser->lexer);
37203 cp_parser_omp_declare_target (parser, pragma_tok);
37204 return;
37207 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37208 "or %<target%>");
37209 cp_parser_require_pragma_eol (parser, pragma_tok);
37212 /* OpenMP 4.5:
37213 #pragma omp taskloop taskloop-clause[optseq] new-line
37214 for-loop
37216 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37217 for-loop */
37219 #define OMP_TASKLOOP_CLAUSE_MASK \
37220 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37226 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37227 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37228 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37229 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37230 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37231 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37232 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37233 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37235 static tree
37236 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37237 char *p_name, omp_clause_mask mask, tree *cclauses,
37238 bool *if_p)
37240 tree clauses, sb, ret;
37241 unsigned int save;
37242 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37244 strcat (p_name, " taskloop");
37245 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37247 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37249 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37250 const char *p = IDENTIFIER_POINTER (id);
37252 if (strcmp (p, "simd") == 0)
37254 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37255 if (cclauses == NULL)
37256 cclauses = cclauses_buf;
37258 cp_lexer_consume_token (parser->lexer);
37259 if (!flag_openmp) /* flag_openmp_simd */
37260 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37261 cclauses, if_p);
37262 sb = begin_omp_structured_block ();
37263 save = cp_parser_begin_omp_structured_block (parser);
37264 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37265 cclauses, if_p);
37266 cp_parser_end_omp_structured_block (parser, save);
37267 tree body = finish_omp_structured_block (sb);
37268 if (ret == NULL)
37269 return ret;
37270 ret = make_node (OMP_TASKLOOP);
37271 TREE_TYPE (ret) = void_type_node;
37272 OMP_FOR_BODY (ret) = body;
37273 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37274 SET_EXPR_LOCATION (ret, loc);
37275 add_stmt (ret);
37276 return ret;
37279 if (!flag_openmp) /* flag_openmp_simd */
37281 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37282 return NULL_TREE;
37285 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37286 cclauses == NULL);
37287 if (cclauses)
37289 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
37290 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37293 sb = begin_omp_structured_block ();
37294 save = cp_parser_begin_omp_structured_block (parser);
37296 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
37297 if_p);
37299 cp_parser_end_omp_structured_block (parser, save);
37300 add_stmt (finish_omp_structured_block (sb));
37302 return ret;
37306 /* OpenACC 2.0:
37307 # pragma acc routine oacc-routine-clause[optseq] new-line
37308 function-definition
37310 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37313 #define OACC_ROUTINE_CLAUSE_MASK \
37314 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37316 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37317 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37320 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37321 component, which must resolve to a declared namespace-scope
37322 function. The clauses are either processed directly (for a named
37323 function), or defered until the immediatley following declaration
37324 is parsed. */
37326 static void
37327 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
37328 enum pragma_context context)
37330 gcc_checking_assert (context == pragma_external);
37331 /* The checking for "another pragma following this one" in the "no optional
37332 '( name )'" case makes sure that we dont re-enter. */
37333 gcc_checking_assert (parser->oacc_routine == NULL);
37335 cp_oacc_routine_data data;
37336 data.error_seen = false;
37337 data.fndecl_seen = false;
37338 data.tokens = vNULL;
37339 data.clauses = NULL_TREE;
37340 data.loc = pragma_tok->location;
37341 /* It is safe to take the address of a local variable; it will only be
37342 used while this scope is live. */
37343 parser->oacc_routine = &data;
37345 /* Look for optional '( name )'. */
37346 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37348 cp_lexer_consume_token (parser->lexer); /* '(' */
37350 /* We parse the name as an id-expression. If it resolves to
37351 anything other than a non-overloaded function at namespace
37352 scope, it's an error. */
37353 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
37354 tree name = cp_parser_id_expression (parser,
37355 /*template_keyword_p=*/false,
37356 /*check_dependency_p=*/false,
37357 /*template_p=*/NULL,
37358 /*declarator_p=*/false,
37359 /*optional_p=*/false);
37360 tree decl = cp_parser_lookup_name_simple (parser, name, name_loc);
37361 if (name != error_mark_node && decl == error_mark_node)
37362 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
37364 if (decl == error_mark_node
37365 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37367 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37368 parser->oacc_routine = NULL;
37369 return;
37372 data.clauses
37373 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37374 "#pragma acc routine",
37375 cp_lexer_peek_token (parser->lexer));
37377 if (decl && is_overloaded_fn (decl)
37378 && (TREE_CODE (decl) != FUNCTION_DECL
37379 || DECL_FUNCTION_TEMPLATE_P (decl)))
37381 error_at (name_loc,
37382 "%<#pragma acc routine%> names a set of overloads");
37383 parser->oacc_routine = NULL;
37384 return;
37387 /* Perhaps we should use the same rule as declarations in different
37388 namespaces? */
37389 if (!DECL_NAMESPACE_SCOPE_P (decl))
37391 error_at (name_loc,
37392 "%qD does not refer to a namespace scope function", decl);
37393 parser->oacc_routine = NULL;
37394 return;
37397 if (TREE_CODE (decl) != FUNCTION_DECL)
37399 error_at (name_loc, "%qD does not refer to a function", decl);
37400 parser->oacc_routine = NULL;
37401 return;
37404 cp_finalize_oacc_routine (parser, decl, false);
37405 parser->oacc_routine = NULL;
37407 else /* No optional '( name )'. */
37409 /* Store away all pragma tokens. */
37410 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37411 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37412 cp_lexer_consume_token (parser->lexer);
37413 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37414 parser->oacc_routine->error_seen = true;
37415 cp_parser_require_pragma_eol (parser, pragma_tok);
37416 struct cp_token_cache *cp
37417 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37418 parser->oacc_routine->tokens.safe_push (cp);
37420 /* Emit a helpful diagnostic if there's another pragma following this
37421 one. */
37422 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37424 cp_ensure_no_oacc_routine (parser);
37425 data.tokens.release ();
37426 /* ..., and then just keep going. */
37427 return;
37430 /* We only have to consider the pragma_external case here. */
37431 cp_parser_declaration (parser);
37432 if (parser->oacc_routine
37433 && !parser->oacc_routine->fndecl_seen)
37434 cp_ensure_no_oacc_routine (parser);
37435 else
37436 parser->oacc_routine = NULL;
37437 data.tokens.release ();
37441 /* Finalize #pragma acc routine clauses after direct declarator has
37442 been parsed. */
37444 static tree
37445 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
37447 struct cp_token_cache *ce;
37448 cp_oacc_routine_data *data = parser->oacc_routine;
37450 if (!data->error_seen && data->fndecl_seen)
37452 error_at (data->loc,
37453 "%<#pragma acc routine%> not immediately followed by "
37454 "a single function declaration or definition");
37455 data->error_seen = true;
37457 if (data->error_seen)
37458 return attrs;
37460 gcc_checking_assert (data->tokens.length () == 1);
37461 ce = data->tokens[0];
37463 cp_parser_push_lexer_for_tokens (parser, ce);
37464 parser->lexer->in_pragma = true;
37465 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37467 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37468 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
37469 parser->oacc_routine->clauses
37470 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37471 "#pragma acc routine", pragma_tok);
37472 cp_parser_pop_lexer (parser);
37473 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
37474 fndecl_seen. */
37476 return attrs;
37479 /* Apply any saved OpenACC routine clauses to a just-parsed
37480 declaration. */
37482 static void
37483 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
37485 if (__builtin_expect (parser->oacc_routine != NULL, 0))
37487 /* Keep going if we're in error reporting mode. */
37488 if (parser->oacc_routine->error_seen
37489 || fndecl == error_mark_node)
37490 return;
37492 if (parser->oacc_routine->fndecl_seen)
37494 error_at (parser->oacc_routine->loc,
37495 "%<#pragma acc routine%> not immediately followed by"
37496 " a single function declaration or definition");
37497 parser->oacc_routine = NULL;
37498 return;
37500 if (TREE_CODE (fndecl) != FUNCTION_DECL)
37502 cp_ensure_no_oacc_routine (parser);
37503 return;
37506 if (oacc_get_fn_attrib (fndecl))
37508 error_at (parser->oacc_routine->loc,
37509 "%<#pragma acc routine%> already applied to %qD", fndecl);
37510 parser->oacc_routine = NULL;
37511 return;
37514 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
37516 error_at (parser->oacc_routine->loc,
37517 "%<#pragma acc routine%> must be applied before %s",
37518 TREE_USED (fndecl) ? "use" : "definition");
37519 parser->oacc_routine = NULL;
37520 return;
37523 /* Process the routine's dimension clauses. */
37524 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
37525 oacc_replace_fn_attrib (fndecl, dims);
37527 /* Add an "omp declare target" attribute. */
37528 DECL_ATTRIBUTES (fndecl)
37529 = tree_cons (get_identifier ("omp declare target"),
37530 NULL_TREE, DECL_ATTRIBUTES (fndecl));
37532 /* Don't unset parser->oacc_routine here: we may still need it to
37533 diagnose wrong usage. But, remember that we've used this "#pragma acc
37534 routine". */
37535 parser->oacc_routine->fndecl_seen = true;
37539 /* Main entry point to OpenMP statement pragmas. */
37541 static void
37542 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37544 tree stmt;
37545 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
37546 omp_clause_mask mask (0);
37548 switch (cp_parser_pragma_kind (pragma_tok))
37550 case PRAGMA_OACC_ATOMIC:
37551 cp_parser_omp_atomic (parser, pragma_tok);
37552 return;
37553 case PRAGMA_OACC_CACHE:
37554 stmt = cp_parser_oacc_cache (parser, pragma_tok);
37555 break;
37556 case PRAGMA_OACC_DATA:
37557 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
37558 break;
37559 case PRAGMA_OACC_ENTER_DATA:
37560 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
37561 break;
37562 case PRAGMA_OACC_EXIT_DATA:
37563 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
37564 break;
37565 case PRAGMA_OACC_HOST_DATA:
37566 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
37567 break;
37568 case PRAGMA_OACC_KERNELS:
37569 case PRAGMA_OACC_PARALLEL:
37570 strcpy (p_name, "#pragma acc");
37571 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
37572 if_p);
37573 break;
37574 case PRAGMA_OACC_LOOP:
37575 strcpy (p_name, "#pragma acc");
37576 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
37577 if_p);
37578 break;
37579 case PRAGMA_OACC_UPDATE:
37580 stmt = cp_parser_oacc_update (parser, pragma_tok);
37581 break;
37582 case PRAGMA_OACC_WAIT:
37583 stmt = cp_parser_oacc_wait (parser, pragma_tok);
37584 break;
37585 case PRAGMA_OMP_ATOMIC:
37586 cp_parser_omp_atomic (parser, pragma_tok);
37587 return;
37588 case PRAGMA_OMP_CRITICAL:
37589 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
37590 break;
37591 case PRAGMA_OMP_DISTRIBUTE:
37592 strcpy (p_name, "#pragma omp");
37593 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
37594 if_p);
37595 break;
37596 case PRAGMA_OMP_FOR:
37597 strcpy (p_name, "#pragma omp");
37598 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
37599 if_p);
37600 break;
37601 case PRAGMA_OMP_MASTER:
37602 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
37603 break;
37604 case PRAGMA_OMP_PARALLEL:
37605 strcpy (p_name, "#pragma omp");
37606 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
37607 if_p);
37608 break;
37609 case PRAGMA_OMP_SECTIONS:
37610 strcpy (p_name, "#pragma omp");
37611 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
37612 break;
37613 case PRAGMA_OMP_SIMD:
37614 strcpy (p_name, "#pragma omp");
37615 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
37616 if_p);
37617 break;
37618 case PRAGMA_OMP_SINGLE:
37619 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
37620 break;
37621 case PRAGMA_OMP_TASK:
37622 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
37623 break;
37624 case PRAGMA_OMP_TASKGROUP:
37625 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
37626 break;
37627 case PRAGMA_OMP_TASKLOOP:
37628 strcpy (p_name, "#pragma omp");
37629 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
37630 if_p);
37631 break;
37632 case PRAGMA_OMP_TEAMS:
37633 strcpy (p_name, "#pragma omp");
37634 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
37635 if_p);
37636 break;
37637 default:
37638 gcc_unreachable ();
37641 protected_set_expr_location (stmt, pragma_tok->location);
37644 /* Transactional Memory parsing routines. */
37646 /* Parse a transaction attribute.
37648 txn-attribute:
37649 attribute
37650 [ [ identifier ] ]
37652 We use this instead of cp_parser_attributes_opt for transactions to avoid
37653 the pedwarn in C++98 mode. */
37655 static tree
37656 cp_parser_txn_attribute_opt (cp_parser *parser)
37658 cp_token *token;
37659 tree attr_name, attr = NULL;
37661 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
37662 return cp_parser_attributes_opt (parser);
37664 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
37665 return NULL_TREE;
37666 cp_lexer_consume_token (parser->lexer);
37667 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
37668 goto error1;
37670 token = cp_lexer_peek_token (parser->lexer);
37671 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
37673 token = cp_lexer_consume_token (parser->lexer);
37675 attr_name = (token->type == CPP_KEYWORD
37676 /* For keywords, use the canonical spelling,
37677 not the parsed identifier. */
37678 ? ridpointers[(int) token->keyword]
37679 : token->u.value);
37680 attr = build_tree_list (attr_name, NULL_TREE);
37682 else
37683 cp_parser_error (parser, "expected identifier");
37685 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
37686 error1:
37687 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
37688 return attr;
37691 /* Parse a __transaction_atomic or __transaction_relaxed statement.
37693 transaction-statement:
37694 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
37695 compound-statement
37696 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
37699 static tree
37700 cp_parser_transaction (cp_parser *parser, cp_token *token)
37702 unsigned char old_in = parser->in_transaction;
37703 unsigned char this_in = 1, new_in;
37704 enum rid keyword = token->keyword;
37705 tree stmt, attrs, noex;
37707 cp_lexer_consume_token (parser->lexer);
37709 if (keyword == RID_TRANSACTION_RELAXED
37710 || keyword == RID_SYNCHRONIZED)
37711 this_in |= TM_STMT_ATTR_RELAXED;
37712 else
37714 attrs = cp_parser_txn_attribute_opt (parser);
37715 if (attrs)
37716 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37719 /* Parse a noexcept specification. */
37720 if (keyword == RID_ATOMIC_NOEXCEPT)
37721 noex = boolean_true_node;
37722 else if (keyword == RID_ATOMIC_CANCEL)
37724 /* cancel-and-throw is unimplemented. */
37725 sorry ("atomic_cancel");
37726 noex = NULL_TREE;
37728 else
37729 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
37731 /* Keep track if we're in the lexical scope of an outer transaction. */
37732 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
37734 stmt = begin_transaction_stmt (token->location, NULL, this_in);
37736 parser->in_transaction = new_in;
37737 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
37738 parser->in_transaction = old_in;
37740 finish_transaction_stmt (stmt, NULL, this_in, noex);
37742 return stmt;
37745 /* Parse a __transaction_atomic or __transaction_relaxed expression.
37747 transaction-expression:
37748 __transaction_atomic txn-noexcept-spec[opt] ( expression )
37749 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
37752 static tree
37753 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
37755 unsigned char old_in = parser->in_transaction;
37756 unsigned char this_in = 1;
37757 cp_token *token;
37758 tree expr, noex;
37759 bool noex_expr;
37760 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37762 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
37763 || keyword == RID_TRANSACTION_RELAXED);
37765 if (!flag_tm)
37766 error_at (loc,
37767 keyword == RID_TRANSACTION_RELAXED
37768 ? G_("%<__transaction_relaxed%> without transactional memory "
37769 "support enabled")
37770 : G_("%<__transaction_atomic%> without transactional memory "
37771 "support enabled"));
37773 token = cp_parser_require_keyword (parser, keyword,
37774 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37775 : RT_TRANSACTION_RELAXED));
37776 gcc_assert (token != NULL);
37778 if (keyword == RID_TRANSACTION_RELAXED)
37779 this_in |= TM_STMT_ATTR_RELAXED;
37781 /* Set this early. This might mean that we allow transaction_cancel in
37782 an expression that we find out later actually has to be a constexpr.
37783 However, we expect that cxx_constant_value will be able to deal with
37784 this; also, if the noexcept has no constexpr, then what we parse next
37785 really is a transaction's body. */
37786 parser->in_transaction = this_in;
37788 /* Parse a noexcept specification. */
37789 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
37790 true);
37792 if (!noex || !noex_expr
37793 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37795 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
37797 expr = cp_parser_expression (parser);
37798 expr = finish_parenthesized_expr (expr);
37800 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
37802 else
37804 /* The only expression that is available got parsed for the noexcept
37805 already. noexcept is true then. */
37806 expr = noex;
37807 noex = boolean_true_node;
37810 expr = build_transaction_expr (token->location, expr, this_in, noex);
37811 parser->in_transaction = old_in;
37813 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
37814 return error_mark_node;
37816 return (flag_tm ? expr : error_mark_node);
37819 /* Parse a function-transaction-block.
37821 function-transaction-block:
37822 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
37823 function-body
37824 __transaction_atomic txn-attribute[opt] function-try-block
37825 __transaction_relaxed ctor-initializer[opt] function-body
37826 __transaction_relaxed function-try-block
37829 static bool
37830 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
37832 unsigned char old_in = parser->in_transaction;
37833 unsigned char new_in = 1;
37834 tree compound_stmt, stmt, attrs;
37835 bool ctor_initializer_p;
37836 cp_token *token;
37838 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
37839 || keyword == RID_TRANSACTION_RELAXED);
37840 token = cp_parser_require_keyword (parser, keyword,
37841 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37842 : RT_TRANSACTION_RELAXED));
37843 gcc_assert (token != NULL);
37845 if (keyword == RID_TRANSACTION_RELAXED)
37846 new_in |= TM_STMT_ATTR_RELAXED;
37847 else
37849 attrs = cp_parser_txn_attribute_opt (parser);
37850 if (attrs)
37851 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37854 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
37856 parser->in_transaction = new_in;
37858 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
37859 ctor_initializer_p = cp_parser_function_try_block (parser);
37860 else
37861 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
37862 (parser, /*in_function_try_block=*/false);
37864 parser->in_transaction = old_in;
37866 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
37868 return ctor_initializer_p;
37871 /* Parse a __transaction_cancel statement.
37873 cancel-statement:
37874 __transaction_cancel txn-attribute[opt] ;
37875 __transaction_cancel txn-attribute[opt] throw-expression ;
37877 ??? Cancel and throw is not yet implemented. */
37879 static tree
37880 cp_parser_transaction_cancel (cp_parser *parser)
37882 cp_token *token;
37883 bool is_outer = false;
37884 tree stmt, attrs;
37886 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
37887 RT_TRANSACTION_CANCEL);
37888 gcc_assert (token != NULL);
37890 attrs = cp_parser_txn_attribute_opt (parser);
37891 if (attrs)
37892 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
37894 /* ??? Parse cancel-and-throw here. */
37896 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
37898 if (!flag_tm)
37900 error_at (token->location, "%<__transaction_cancel%> without "
37901 "transactional memory support enabled");
37902 return error_mark_node;
37904 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
37906 error_at (token->location, "%<__transaction_cancel%> within a "
37907 "%<__transaction_relaxed%>");
37908 return error_mark_node;
37910 else if (is_outer)
37912 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
37913 && !is_tm_may_cancel_outer (current_function_decl))
37915 error_at (token->location, "outer %<__transaction_cancel%> not "
37916 "within outer %<__transaction_atomic%>");
37917 error_at (token->location,
37918 " or a %<transaction_may_cancel_outer%> function");
37919 return error_mark_node;
37922 else if (parser->in_transaction == 0)
37924 error_at (token->location, "%<__transaction_cancel%> not within "
37925 "%<__transaction_atomic%>");
37926 return error_mark_node;
37929 stmt = build_tm_abort_call (token->location, is_outer);
37930 add_stmt (stmt);
37932 return stmt;
37935 /* The parser. */
37937 static GTY (()) cp_parser *the_parser;
37940 /* Special handling for the first token or line in the file. The first
37941 thing in the file might be #pragma GCC pch_preprocess, which loads a
37942 PCH file, which is a GC collection point. So we need to handle this
37943 first pragma without benefit of an existing lexer structure.
37945 Always returns one token to the caller in *FIRST_TOKEN. This is
37946 either the true first token of the file, or the first token after
37947 the initial pragma. */
37949 static void
37950 cp_parser_initial_pragma (cp_token *first_token)
37952 tree name = NULL;
37954 cp_lexer_get_preprocessor_token (NULL, first_token);
37955 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
37956 return;
37958 cp_lexer_get_preprocessor_token (NULL, first_token);
37959 if (first_token->type == CPP_STRING)
37961 name = first_token->u.value;
37963 cp_lexer_get_preprocessor_token (NULL, first_token);
37964 if (first_token->type != CPP_PRAGMA_EOL)
37965 error_at (first_token->location,
37966 "junk at end of %<#pragma GCC pch_preprocess%>");
37968 else
37969 error_at (first_token->location, "expected string literal");
37971 /* Skip to the end of the pragma. */
37972 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
37973 cp_lexer_get_preprocessor_token (NULL, first_token);
37975 /* Now actually load the PCH file. */
37976 if (name)
37977 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
37979 /* Read one more token to return to our caller. We have to do this
37980 after reading the PCH file in, since its pointers have to be
37981 live. */
37982 cp_lexer_get_preprocessor_token (NULL, first_token);
37985 /* Parses the grainsize pragma for the _Cilk_for statement.
37986 Syntax:
37987 #pragma cilk grainsize = <VALUE>. */
37989 static void
37990 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37992 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
37994 tree exp = cp_parser_binary_expression (parser, false, false,
37995 PREC_NOT_OPERATOR, NULL);
37996 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37997 if (!exp || exp == error_mark_node)
37999 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
38000 return;
38003 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
38004 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
38005 cp_parser_cilk_for (parser, exp, if_p);
38006 else
38007 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
38008 "%<#pragma cilk grainsize%> is not followed by "
38009 "%<_Cilk_for%>");
38010 return;
38012 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38015 /* Normal parsing of a pragma token. Here we can (and must) use the
38016 regular lexer. */
38018 static bool
38019 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38021 cp_token *pragma_tok;
38022 unsigned int id;
38023 tree stmt;
38024 bool ret;
38026 pragma_tok = cp_lexer_consume_token (parser->lexer);
38027 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38028 parser->lexer->in_pragma = true;
38030 id = cp_parser_pragma_kind (pragma_tok);
38031 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38032 cp_ensure_no_omp_declare_simd (parser);
38033 switch (id)
38035 case PRAGMA_GCC_PCH_PREPROCESS:
38036 error_at (pragma_tok->location,
38037 "%<#pragma GCC pch_preprocess%> must be first");
38038 break;
38040 case PRAGMA_OMP_BARRIER:
38041 switch (context)
38043 case pragma_compound:
38044 cp_parser_omp_barrier (parser, pragma_tok);
38045 return false;
38046 case pragma_stmt:
38047 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
38048 "used in compound statements");
38049 break;
38050 default:
38051 goto bad_stmt;
38053 break;
38055 case PRAGMA_OMP_FLUSH:
38056 switch (context)
38058 case pragma_compound:
38059 cp_parser_omp_flush (parser, pragma_tok);
38060 return false;
38061 case pragma_stmt:
38062 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
38063 "used in compound statements");
38064 break;
38065 default:
38066 goto bad_stmt;
38068 break;
38070 case PRAGMA_OMP_TASKWAIT:
38071 switch (context)
38073 case pragma_compound:
38074 cp_parser_omp_taskwait (parser, pragma_tok);
38075 return false;
38076 case pragma_stmt:
38077 error_at (pragma_tok->location,
38078 "%<#pragma omp taskwait%> may only be "
38079 "used in compound statements");
38080 break;
38081 default:
38082 goto bad_stmt;
38084 break;
38086 case PRAGMA_OMP_TASKYIELD:
38087 switch (context)
38089 case pragma_compound:
38090 cp_parser_omp_taskyield (parser, pragma_tok);
38091 return false;
38092 case pragma_stmt:
38093 error_at (pragma_tok->location,
38094 "%<#pragma omp taskyield%> may only be "
38095 "used in compound statements");
38096 break;
38097 default:
38098 goto bad_stmt;
38100 break;
38102 case PRAGMA_OMP_CANCEL:
38103 switch (context)
38105 case pragma_compound:
38106 cp_parser_omp_cancel (parser, pragma_tok);
38107 return false;
38108 case pragma_stmt:
38109 error_at (pragma_tok->location,
38110 "%<#pragma omp cancel%> may only be "
38111 "used in compound statements");
38112 break;
38113 default:
38114 goto bad_stmt;
38116 break;
38118 case PRAGMA_OMP_CANCELLATION_POINT:
38119 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38120 return false;
38122 case PRAGMA_OMP_THREADPRIVATE:
38123 cp_parser_omp_threadprivate (parser, pragma_tok);
38124 return false;
38126 case PRAGMA_OMP_DECLARE:
38127 cp_parser_omp_declare (parser, pragma_tok, context);
38128 return false;
38130 case PRAGMA_OACC_DECLARE:
38131 cp_parser_oacc_declare (parser, pragma_tok);
38132 return false;
38134 case PRAGMA_OACC_ENTER_DATA:
38135 if (context == pragma_stmt)
38137 cp_parser_error (parser, "%<#pragma acc enter data%> may only be "
38138 "used in compound statements");
38139 break;
38141 else if (context != pragma_compound)
38142 goto bad_stmt;
38143 cp_parser_omp_construct (parser, pragma_tok, if_p);
38144 return true;
38146 case PRAGMA_OACC_EXIT_DATA:
38147 if (context == pragma_stmt)
38149 cp_parser_error (parser, "%<#pragma acc exit data%> may only be "
38150 "used in compound statements");
38151 break;
38153 else if (context != pragma_compound)
38154 goto bad_stmt;
38155 cp_parser_omp_construct (parser, pragma_tok, if_p);
38156 return true;
38158 case PRAGMA_OACC_ROUTINE:
38159 if (context != pragma_external)
38161 error_at (pragma_tok->location,
38162 "%<#pragma acc routine%> must be at file scope");
38163 break;
38165 cp_parser_oacc_routine (parser, pragma_tok, context);
38166 return false;
38168 case PRAGMA_OACC_UPDATE:
38169 if (context == pragma_stmt)
38171 cp_parser_error (parser, "%<#pragma acc update%> may only be "
38172 "used in compound statements");
38173 break;
38175 else if (context != pragma_compound)
38176 goto bad_stmt;
38177 cp_parser_omp_construct (parser, pragma_tok, if_p);
38178 return true;
38180 case PRAGMA_OACC_WAIT:
38181 if (context == pragma_stmt)
38183 cp_parser_error (parser, "%<#pragma acc wait%> may only be "
38184 "used in compound statements");
38185 break;
38187 else if (context != pragma_compound)
38188 goto bad_stmt;
38189 cp_parser_omp_construct (parser, pragma_tok, if_p);
38190 return true;
38192 case PRAGMA_OACC_ATOMIC:
38193 case PRAGMA_OACC_CACHE:
38194 case PRAGMA_OACC_DATA:
38195 case PRAGMA_OACC_HOST_DATA:
38196 case PRAGMA_OACC_KERNELS:
38197 case PRAGMA_OACC_PARALLEL:
38198 case PRAGMA_OACC_LOOP:
38199 case PRAGMA_OMP_ATOMIC:
38200 case PRAGMA_OMP_CRITICAL:
38201 case PRAGMA_OMP_DISTRIBUTE:
38202 case PRAGMA_OMP_FOR:
38203 case PRAGMA_OMP_MASTER:
38204 case PRAGMA_OMP_PARALLEL:
38205 case PRAGMA_OMP_SECTIONS:
38206 case PRAGMA_OMP_SIMD:
38207 case PRAGMA_OMP_SINGLE:
38208 case PRAGMA_OMP_TASK:
38209 case PRAGMA_OMP_TASKGROUP:
38210 case PRAGMA_OMP_TASKLOOP:
38211 case PRAGMA_OMP_TEAMS:
38212 if (context != pragma_stmt && context != pragma_compound)
38213 goto bad_stmt;
38214 stmt = push_omp_privatization_clauses (false);
38215 cp_parser_omp_construct (parser, pragma_tok, if_p);
38216 pop_omp_privatization_clauses (stmt);
38217 return true;
38219 case PRAGMA_OMP_ORDERED:
38220 stmt = push_omp_privatization_clauses (false);
38221 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38222 pop_omp_privatization_clauses (stmt);
38223 return ret;
38225 case PRAGMA_OMP_TARGET:
38226 stmt = push_omp_privatization_clauses (false);
38227 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38228 pop_omp_privatization_clauses (stmt);
38229 return ret;
38231 case PRAGMA_OMP_END_DECLARE_TARGET:
38232 cp_parser_omp_end_declare_target (parser, pragma_tok);
38233 return false;
38235 case PRAGMA_OMP_SECTION:
38236 error_at (pragma_tok->location,
38237 "%<#pragma omp section%> may only be used in "
38238 "%<#pragma omp sections%> construct");
38239 break;
38241 case PRAGMA_IVDEP:
38243 if (context == pragma_external)
38245 error_at (pragma_tok->location,
38246 "%<#pragma GCC ivdep%> must be inside a function");
38247 break;
38249 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38250 cp_token *tok;
38251 tok = cp_lexer_peek_token (the_parser->lexer);
38252 if (tok->type != CPP_KEYWORD
38253 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
38254 && tok->keyword != RID_DO))
38256 cp_parser_error (parser, "for, while or do statement expected");
38257 return false;
38259 cp_parser_iteration_statement (parser, if_p, true);
38260 return true;
38263 case PRAGMA_CILK_SIMD:
38264 if (context == pragma_external)
38266 error_at (pragma_tok->location,
38267 "%<#pragma simd%> must be inside a function");
38268 break;
38270 stmt = push_omp_privatization_clauses (false);
38271 cp_parser_cilk_simd (parser, pragma_tok, if_p);
38272 pop_omp_privatization_clauses (stmt);
38273 return true;
38275 case PRAGMA_CILK_GRAINSIZE:
38276 if (context == pragma_external)
38278 error_at (pragma_tok->location,
38279 "%<#pragma cilk grainsize%> must be inside a function");
38280 break;
38283 /* Ignore the pragma if Cilk Plus is not enabled. */
38284 if (flag_cilkplus)
38286 cp_parser_cilk_grainsize (parser, pragma_tok, if_p);
38287 return true;
38289 else
38291 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
38292 "%<#pragma cilk grainsize%>");
38293 break;
38296 default:
38297 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
38298 c_invoke_pragma_handler (id);
38299 break;
38301 bad_stmt:
38302 cp_parser_error (parser, "expected declaration specifiers");
38303 break;
38306 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38307 return false;
38310 /* The interface the pragma parsers have to the lexer. */
38312 enum cpp_ttype
38313 pragma_lex (tree *value, location_t *loc)
38315 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38316 enum cpp_ttype ret = tok->type;
38318 *value = tok->u.value;
38319 if (loc)
38320 *loc = tok->location;
38322 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
38323 ret = CPP_EOF;
38324 else if (ret == CPP_STRING)
38325 *value = cp_parser_string_literal (the_parser, false, false);
38326 else
38328 if (ret == CPP_KEYWORD)
38329 ret = CPP_NAME;
38330 cp_lexer_consume_token (the_parser->lexer);
38333 return ret;
38337 /* External interface. */
38339 /* Parse one entire translation unit. */
38341 void
38342 c_parse_file (void)
38344 static bool already_called = false;
38346 if (already_called)
38347 fatal_error (input_location,
38348 "inter-module optimizations not implemented for C++");
38349 already_called = true;
38351 the_parser = cp_parser_new ();
38352 push_deferring_access_checks (flag_access_control
38353 ? dk_no_deferred : dk_no_check);
38354 cp_parser_translation_unit (the_parser);
38355 the_parser = NULL;
38358 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
38359 vectorlength clause:
38360 Syntax:
38361 vectorlength ( constant-expression ) */
38363 static tree
38364 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
38365 bool is_simd_fn)
38367 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38368 tree expr;
38369 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
38370 safelen clause. Thus, vectorlength is represented as OMP 4.0
38371 safelen. For SIMD-enabled function it is represented by OMP 4.0
38372 simdlen. */
38373 if (!is_simd_fn)
38374 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
38375 loc);
38376 else
38377 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
38378 loc);
38380 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38381 return error_mark_node;
38383 expr = cp_parser_constant_expression (parser);
38384 expr = maybe_constant_value (expr);
38386 /* If expr == error_mark_node, then don't emit any errors nor
38387 create a clause. if any of the above functions returns
38388 error mark node then they would have emitted an error message. */
38389 if (expr == error_mark_node)
38391 else if (!TREE_TYPE (expr)
38392 || !TREE_CONSTANT (expr)
38393 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
38394 error_at (loc, "vectorlength must be an integer constant");
38395 else if (TREE_CONSTANT (expr)
38396 && !pow2p_hwi (TREE_INT_CST_LOW (expr)))
38397 error_at (loc, "vectorlength must be a power of 2");
38398 else
38400 tree c;
38401 if (!is_simd_fn)
38403 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
38404 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
38405 OMP_CLAUSE_CHAIN (c) = clauses;
38406 clauses = c;
38408 else
38410 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
38411 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
38412 OMP_CLAUSE_CHAIN (c) = clauses;
38413 clauses = c;
38417 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
38418 return error_mark_node;
38419 return clauses;
38422 /* Handles the Cilk Plus #pragma simd linear clause.
38423 Syntax:
38424 linear ( simd-linear-variable-list )
38426 simd-linear-variable-list:
38427 simd-linear-variable
38428 simd-linear-variable-list , simd-linear-variable
38430 simd-linear-variable:
38431 id-expression
38432 id-expression : simd-linear-step
38434 simd-linear-step:
38435 conditional-expression */
38437 static tree
38438 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
38440 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38442 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38443 return clauses;
38444 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38446 cp_parser_error (parser, "expected identifier");
38447 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38448 return error_mark_node;
38451 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38452 parser->colon_corrects_to_scope_p = false;
38453 while (1)
38455 cp_token *token = cp_lexer_peek_token (parser->lexer);
38456 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38458 cp_parser_error (parser, "expected variable-name");
38459 clauses = error_mark_node;
38460 break;
38463 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
38464 false, false);
38465 tree decl = cp_parser_lookup_name_simple (parser, var_name,
38466 token->location);
38467 if (decl == error_mark_node)
38469 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
38470 token->location);
38471 clauses = error_mark_node;
38473 else
38475 tree e = NULL_TREE;
38476 tree step_size = integer_one_node;
38478 /* If present, parse the linear step. Otherwise, assume the default
38479 value of 1. */
38480 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
38482 cp_lexer_consume_token (parser->lexer);
38484 e = cp_parser_assignment_expression (parser);
38485 e = maybe_constant_value (e);
38487 if (e == error_mark_node)
38489 /* If an error has occurred, then the whole pragma is
38490 considered ill-formed. Thus, no reason to keep
38491 parsing. */
38492 clauses = error_mark_node;
38493 break;
38495 else if (type_dependent_expression_p (e)
38496 || value_dependent_expression_p (e)
38497 || (TREE_TYPE (e)
38498 && INTEGRAL_TYPE_P (TREE_TYPE (e))
38499 && (TREE_CONSTANT (e)
38500 || DECL_P (e))))
38501 step_size = e;
38502 else
38503 cp_parser_error (parser,
38504 "step size must be an integer constant "
38505 "expression or an integer variable");
38508 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
38509 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
38510 OMP_CLAUSE_DECL (l) = decl;
38511 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
38512 OMP_CLAUSE_CHAIN (l) = clauses;
38513 clauses = l;
38515 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38516 cp_lexer_consume_token (parser->lexer);
38517 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
38518 break;
38519 else
38521 error_at (cp_lexer_peek_token (parser->lexer)->location,
38522 "expected %<,%> or %<)%> after %qE", decl);
38523 clauses = error_mark_node;
38524 break;
38527 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38528 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38529 return clauses;
38532 /* Returns the name of the next clause. If the clause is not
38533 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
38534 token is not consumed. Otherwise, the appropriate enum from the
38535 pragma_simd_clause is returned and the token is consumed. */
38537 static pragma_omp_clause
38538 cp_parser_cilk_simd_clause_name (cp_parser *parser)
38540 pragma_omp_clause clause_type;
38541 cp_token *token = cp_lexer_peek_token (parser->lexer);
38543 if (token->keyword == RID_PRIVATE)
38544 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
38545 else if (!token->u.value || token->type != CPP_NAME)
38546 return PRAGMA_CILK_CLAUSE_NONE;
38547 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
38548 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
38549 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
38550 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
38551 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
38552 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
38553 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
38554 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
38555 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
38556 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
38557 else
38558 return PRAGMA_CILK_CLAUSE_NONE;
38560 cp_lexer_consume_token (parser->lexer);
38561 return clause_type;
38564 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
38566 static tree
38567 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
38569 tree clauses = NULL_TREE;
38571 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38572 && clauses != error_mark_node)
38574 pragma_omp_clause c_kind;
38575 c_kind = cp_parser_cilk_simd_clause_name (parser);
38576 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
38577 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
38578 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
38579 clauses = cp_parser_cilk_simd_linear (parser, clauses);
38580 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
38581 /* Use the OpenMP 4.0 equivalent function. */
38582 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
38583 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
38584 /* Use the OpenMP 4.0 equivalent function. */
38585 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
38586 clauses);
38587 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
38588 /* Use the OMP 4.0 equivalent function. */
38589 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
38590 clauses);
38591 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
38592 /* Use the OMP 4.0 equivalent function. */
38593 clauses = cp_parser_omp_clause_reduction (parser, clauses);
38594 else
38596 clauses = error_mark_node;
38597 cp_parser_error (parser, "expected %<#pragma simd%> clause");
38598 break;
38602 cp_parser_skip_to_pragma_eol (parser, pragma_token);
38604 if (clauses == error_mark_node)
38605 return error_mark_node;
38606 else
38607 return finish_omp_clauses (clauses, C_ORT_CILK);
38610 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
38612 static void
38613 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token, bool *if_p)
38615 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
38617 if (clauses == error_mark_node)
38618 return;
38620 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
38622 error_at (cp_lexer_peek_token (parser->lexer)->location,
38623 "for statement expected");
38624 return;
38627 tree sb = begin_omp_structured_block ();
38628 int save = cp_parser_begin_omp_structured_block (parser);
38629 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL, if_p);
38630 if (ret)
38631 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
38632 cp_parser_end_omp_structured_block (parser, save);
38633 add_stmt (finish_omp_structured_block (sb));
38636 /* Main entry-point for parsing Cilk Plus _Cilk_for
38637 loops. The return value is error_mark_node
38638 when errors happen and CILK_FOR tree on success. */
38640 static tree
38641 cp_parser_cilk_for (cp_parser *parser, tree grain, bool *if_p)
38643 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
38644 gcc_unreachable ();
38646 tree sb = begin_omp_structured_block ();
38647 int save = cp_parser_begin_omp_structured_block (parser);
38649 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
38650 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
38651 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
38652 clauses = finish_omp_clauses (clauses, C_ORT_CILK);
38654 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL, if_p);
38655 if (ret)
38656 cpp_validate_cilk_plus_loop (ret);
38657 else
38658 ret = error_mark_node;
38660 cp_parser_end_omp_structured_block (parser, save);
38661 add_stmt (finish_omp_structured_block (sb));
38662 return ret;
38665 /* Create an identifier for a generic parameter type (a synthesized
38666 template parameter implied by `auto' or a concept identifier). */
38668 static GTY(()) int generic_parm_count;
38669 static tree
38670 make_generic_type_name ()
38672 char buf[32];
38673 sprintf (buf, "auto:%d", ++generic_parm_count);
38674 return get_identifier (buf);
38677 /* Predicate that behaves as is_auto_or_concept but matches the parent
38678 node of the generic type rather than the generic type itself. This
38679 allows for type transformation in add_implicit_template_parms. */
38681 static inline bool
38682 tree_type_is_auto_or_concept (const_tree t)
38684 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
38687 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
38688 (creating a new template parameter list if necessary). Returns the newly
38689 created template type parm. */
38691 static tree
38692 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
38694 gcc_assert (current_binding_level->kind == sk_function_parms);
38696 /* Before committing to modifying any scope, if we're in an
38697 implicit template scope, and we're trying to synthesize a
38698 constrained parameter, try to find a previous parameter with
38699 the same name. This is the same-type rule for abbreviated
38700 function templates.
38702 NOTE: We can generate implicit parameters when tentatively
38703 parsing a nested name specifier, only to reject that parse
38704 later. However, matching the same template-id as part of a
38705 direct-declarator should generate an identical template
38706 parameter, so this rule will merge them. */
38707 if (parser->implicit_template_scope && constr)
38709 tree t = parser->implicit_template_parms;
38710 while (t)
38712 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
38714 tree d = TREE_VALUE (t);
38715 if (TREE_CODE (d) == PARM_DECL)
38716 /* Return the TEMPLATE_PARM_INDEX. */
38717 d = DECL_INITIAL (d);
38718 return d;
38720 t = TREE_CHAIN (t);
38724 /* We are either continuing a function template that already contains implicit
38725 template parameters, creating a new fully-implicit function template, or
38726 extending an existing explicit function template with implicit template
38727 parameters. */
38729 cp_binding_level *const entry_scope = current_binding_level;
38731 bool become_template = false;
38732 cp_binding_level *parent_scope = 0;
38734 if (parser->implicit_template_scope)
38736 gcc_assert (parser->implicit_template_parms);
38738 current_binding_level = parser->implicit_template_scope;
38740 else
38742 /* Roll back to the existing template parameter scope (in the case of
38743 extending an explicit function template) or introduce a new template
38744 parameter scope ahead of the function parameter scope (or class scope
38745 in the case of out-of-line member definitions). The function scope is
38746 added back after template parameter synthesis below. */
38748 cp_binding_level *scope = entry_scope;
38750 while (scope->kind == sk_function_parms)
38752 parent_scope = scope;
38753 scope = scope->level_chain;
38755 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
38757 /* If not defining a class, then any class scope is a scope level in
38758 an out-of-line member definition. In this case simply wind back
38759 beyond the first such scope to inject the template parameter list.
38760 Otherwise wind back to the class being defined. The latter can
38761 occur in class member friend declarations such as:
38763 class A {
38764 void foo (auto);
38766 class B {
38767 friend void A::foo (auto);
38770 The template parameter list synthesized for the friend declaration
38771 must be injected in the scope of 'B'. This can also occur in
38772 erroneous cases such as:
38774 struct A {
38775 struct B {
38776 void foo (auto);
38778 void B::foo (auto) {}
38781 Here the attempted definition of 'B::foo' within 'A' is ill-formed
38782 but, nevertheless, the template parameter list synthesized for the
38783 declarator should be injected into the scope of 'A' as if the
38784 ill-formed template was specified explicitly. */
38786 while (scope->kind == sk_class && !scope->defining_class_p)
38788 parent_scope = scope;
38789 scope = scope->level_chain;
38793 current_binding_level = scope;
38795 if (scope->kind != sk_template_parms
38796 || !function_being_declared_is_template_p (parser))
38798 /* Introduce a new template parameter list for implicit template
38799 parameters. */
38801 become_template = true;
38803 parser->implicit_template_scope
38804 = begin_scope (sk_template_parms, NULL);
38806 ++processing_template_decl;
38808 parser->fully_implicit_function_template_p = true;
38809 ++parser->num_template_parameter_lists;
38811 else
38813 /* Synthesize implicit template parameters at the end of the explicit
38814 template parameter list. */
38816 gcc_assert (current_template_parms);
38818 parser->implicit_template_scope = scope;
38820 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38821 parser->implicit_template_parms
38822 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
38826 /* Synthesize a new template parameter and track the current template
38827 parameter chain with implicit_template_parms. */
38829 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
38830 tree synth_id = make_generic_type_name ();
38831 tree synth_tmpl_parm;
38832 bool non_type = false;
38834 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
38835 synth_tmpl_parm
38836 = finish_template_type_parm (class_type_node, synth_id);
38837 else if (TREE_CODE (proto) == TEMPLATE_DECL)
38838 synth_tmpl_parm
38839 = finish_constrained_template_template_parm (proto, synth_id);
38840 else
38842 synth_tmpl_parm = copy_decl (proto);
38843 DECL_NAME (synth_tmpl_parm) = synth_id;
38844 non_type = true;
38847 // Attach the constraint to the parm before processing.
38848 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
38849 TREE_TYPE (node) = constr;
38850 tree new_parm
38851 = process_template_parm (parser->implicit_template_parms,
38852 input_location,
38853 node,
38854 /*non_type=*/non_type,
38855 /*param_pack=*/false);
38857 // Chain the new parameter to the list of implicit parameters.
38858 if (parser->implicit_template_parms)
38859 parser->implicit_template_parms
38860 = TREE_CHAIN (parser->implicit_template_parms);
38861 else
38862 parser->implicit_template_parms = new_parm;
38864 tree new_decl = getdecls ();
38865 if (non_type)
38866 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
38867 new_decl = DECL_INITIAL (new_decl);
38869 /* If creating a fully implicit function template, start the new implicit
38870 template parameter list with this synthesized type, otherwise grow the
38871 current template parameter list. */
38873 if (become_template)
38875 parent_scope->level_chain = current_binding_level;
38877 tree new_parms = make_tree_vec (1);
38878 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
38879 current_template_parms = tree_cons (size_int (processing_template_decl),
38880 new_parms, current_template_parms);
38882 else
38884 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38885 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
38886 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
38887 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
38890 // If the new parameter was constrained, we need to add that to the
38891 // constraints in the template parameter list.
38892 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
38894 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
38895 reqs = conjoin_constraints (reqs, req);
38896 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
38899 current_binding_level = entry_scope;
38901 return new_decl;
38904 /* Finish the declaration of a fully implicit function template. Such a
38905 template has no explicit template parameter list so has not been through the
38906 normal template head and tail processing. synthesize_implicit_template_parm
38907 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
38908 provided if the declaration is a class member such that its template
38909 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
38910 form is returned. Otherwise NULL_TREE is returned. */
38912 static tree
38913 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
38915 gcc_assert (parser->fully_implicit_function_template_p);
38917 if (member_decl_opt && member_decl_opt != error_mark_node
38918 && DECL_VIRTUAL_P (member_decl_opt))
38920 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
38921 "implicit templates may not be %<virtual%>");
38922 DECL_VIRTUAL_P (member_decl_opt) = false;
38925 if (member_decl_opt)
38926 member_decl_opt = finish_member_template_decl (member_decl_opt);
38927 end_template_decl ();
38929 parser->fully_implicit_function_template_p = false;
38930 --parser->num_template_parameter_lists;
38932 return member_decl_opt;
38935 #include "gt-cp-parser.h"