* sv.po: Update.
[official-gcc.git] / gcc / cp / parser.c
blob07d182156d6375230fbeca6f5a6023fbd68f87de
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2016 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "cp-tree.h"
25 #include "c-family/c-common.h"
26 #include "timevar.h"
27 #include "stringpool.h"
28 #include "cgraph.h"
29 #include "print-tree.h"
30 #include "attribs.h"
31 #include "trans-mem.h"
32 #include "intl.h"
33 #include "decl.h"
34 #include "c-family/c-objc.h"
35 #include "plugin.h"
36 #include "tree-pretty-print.h"
37 #include "parser.h"
38 #include "omp-low.h"
39 #include "gomp-constants.h"
40 #include "c-family/c-indentation.h"
41 #include "context.h"
44 /* The lexer. */
46 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
47 and c-lex.c) and the C++ parser. */
49 static cp_token eof_token =
51 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
54 /* The various kinds of non integral constant we encounter. */
55 enum non_integral_constant {
56 NIC_NONE,
57 /* floating-point literal */
58 NIC_FLOAT,
59 /* %<this%> */
60 NIC_THIS,
61 /* %<__FUNCTION__%> */
62 NIC_FUNC_NAME,
63 /* %<__PRETTY_FUNCTION__%> */
64 NIC_PRETTY_FUNC,
65 /* %<__func__%> */
66 NIC_C99_FUNC,
67 /* "%<va_arg%> */
68 NIC_VA_ARG,
69 /* a cast */
70 NIC_CAST,
71 /* %<typeid%> operator */
72 NIC_TYPEID,
73 /* non-constant compound literals */
74 NIC_NCC,
75 /* a function call */
76 NIC_FUNC_CALL,
77 /* an increment */
78 NIC_INC,
79 /* an decrement */
80 NIC_DEC,
81 /* an array reference */
82 NIC_ARRAY_REF,
83 /* %<->%> */
84 NIC_ARROW,
85 /* %<.%> */
86 NIC_POINT,
87 /* the address of a label */
88 NIC_ADDR_LABEL,
89 /* %<*%> */
90 NIC_STAR,
91 /* %<&%> */
92 NIC_ADDR,
93 /* %<++%> */
94 NIC_PREINCREMENT,
95 /* %<--%> */
96 NIC_PREDECREMENT,
97 /* %<new%> */
98 NIC_NEW,
99 /* %<delete%> */
100 NIC_DEL,
101 /* calls to overloaded operators */
102 NIC_OVERLOADED,
103 /* an assignment */
104 NIC_ASSIGNMENT,
105 /* a comma operator */
106 NIC_COMMA,
107 /* a call to a constructor */
108 NIC_CONSTRUCTOR,
109 /* a transaction expression */
110 NIC_TRANSACTION
113 /* The various kinds of errors about name-lookup failing. */
114 enum name_lookup_error {
115 /* NULL */
116 NLE_NULL,
117 /* is not a type */
118 NLE_TYPE,
119 /* is not a class or namespace */
120 NLE_CXX98,
121 /* is not a class, namespace, or enumeration */
122 NLE_NOT_CXX98
125 /* The various kinds of required token */
126 enum required_token {
127 RT_NONE,
128 RT_SEMICOLON, /* ';' */
129 RT_OPEN_PAREN, /* '(' */
130 RT_CLOSE_BRACE, /* '}' */
131 RT_OPEN_BRACE, /* '{' */
132 RT_CLOSE_SQUARE, /* ']' */
133 RT_OPEN_SQUARE, /* '[' */
134 RT_COMMA, /* ',' */
135 RT_SCOPE, /* '::' */
136 RT_LESS, /* '<' */
137 RT_GREATER, /* '>' */
138 RT_EQ, /* '=' */
139 RT_ELLIPSIS, /* '...' */
140 RT_MULT, /* '*' */
141 RT_COMPL, /* '~' */
142 RT_COLON, /* ':' */
143 RT_COLON_SCOPE, /* ':' or '::' */
144 RT_CLOSE_PAREN, /* ')' */
145 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
146 RT_PRAGMA_EOL, /* end of line */
147 RT_NAME, /* identifier */
149 /* The type is CPP_KEYWORD */
150 RT_NEW, /* new */
151 RT_DELETE, /* delete */
152 RT_RETURN, /* return */
153 RT_WHILE, /* while */
154 RT_EXTERN, /* extern */
155 RT_STATIC_ASSERT, /* static_assert */
156 RT_DECLTYPE, /* decltype */
157 RT_OPERATOR, /* operator */
158 RT_CLASS, /* class */
159 RT_TEMPLATE, /* template */
160 RT_NAMESPACE, /* namespace */
161 RT_USING, /* using */
162 RT_ASM, /* asm */
163 RT_TRY, /* try */
164 RT_CATCH, /* catch */
165 RT_THROW, /* throw */
166 RT_LABEL, /* __label__ */
167 RT_AT_TRY, /* @try */
168 RT_AT_SYNCHRONIZED, /* @synchronized */
169 RT_AT_THROW, /* @throw */
171 RT_SELECT, /* selection-statement */
172 RT_INTERATION, /* iteration-statement */
173 RT_JUMP, /* jump-statement */
174 RT_CLASS_KEY, /* class-key */
175 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
176 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
177 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
178 RT_TRANSACTION_CANCEL /* __transaction_cancel */
181 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
182 reverting it on destruction. */
184 class type_id_in_expr_sentinel
186 cp_parser *parser;
187 bool saved;
188 public:
189 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
190 : parser (parser),
191 saved (parser->in_type_id_in_expr_p)
192 { parser->in_type_id_in_expr_p = set; }
193 ~type_id_in_expr_sentinel ()
194 { parser->in_type_id_in_expr_p = saved; }
197 /* Prototypes. */
199 static cp_lexer *cp_lexer_new_main
200 (void);
201 static cp_lexer *cp_lexer_new_from_tokens
202 (cp_token_cache *tokens);
203 static void cp_lexer_destroy
204 (cp_lexer *);
205 static int cp_lexer_saving_tokens
206 (const cp_lexer *);
207 static cp_token *cp_lexer_token_at
208 (cp_lexer *, cp_token_position);
209 static void cp_lexer_get_preprocessor_token
210 (cp_lexer *, cp_token *);
211 static inline cp_token *cp_lexer_peek_token
212 (cp_lexer *);
213 static cp_token *cp_lexer_peek_nth_token
214 (cp_lexer *, size_t);
215 static inline bool cp_lexer_next_token_is
216 (cp_lexer *, enum cpp_ttype);
217 static bool cp_lexer_next_token_is_not
218 (cp_lexer *, enum cpp_ttype);
219 static bool cp_lexer_next_token_is_keyword
220 (cp_lexer *, enum rid);
221 static cp_token *cp_lexer_consume_token
222 (cp_lexer *);
223 static void cp_lexer_purge_token
224 (cp_lexer *);
225 static void cp_lexer_purge_tokens_after
226 (cp_lexer *, cp_token_position);
227 static void cp_lexer_save_tokens
228 (cp_lexer *);
229 static void cp_lexer_commit_tokens
230 (cp_lexer *);
231 static void cp_lexer_rollback_tokens
232 (cp_lexer *);
233 static void cp_lexer_print_token
234 (FILE *, cp_token *);
235 static inline bool cp_lexer_debugging_p
236 (cp_lexer *);
237 static void cp_lexer_start_debugging
238 (cp_lexer *) ATTRIBUTE_UNUSED;
239 static void cp_lexer_stop_debugging
240 (cp_lexer *) ATTRIBUTE_UNUSED;
242 static cp_token_cache *cp_token_cache_new
243 (cp_token *, cp_token *);
245 static void cp_parser_initial_pragma
246 (cp_token *);
248 static tree cp_literal_operator_id
249 (const char *);
251 static void cp_parser_cilk_simd
252 (cp_parser *, cp_token *);
253 static tree cp_parser_cilk_for
254 (cp_parser *, tree);
255 static bool cp_parser_omp_declare_reduction_exprs
256 (tree, cp_parser *);
257 static tree cp_parser_cilk_simd_vectorlength
258 (cp_parser *, tree, bool);
259 static void cp_finalize_oacc_routine
260 (cp_parser *, tree, bool);
262 /* Manifest constants. */
263 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
264 #define CP_SAVED_TOKEN_STACK 5
266 /* Variables. */
268 /* The stream to which debugging output should be written. */
269 static FILE *cp_lexer_debug_stream;
271 /* Nonzero if we are parsing an unevaluated operand: an operand to
272 sizeof, typeof, or alignof. */
273 int cp_unevaluated_operand;
275 /* Dump up to NUM tokens in BUFFER to FILE starting with token
276 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
277 first token in BUFFER. If NUM is 0, dump all the tokens. If
278 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
279 highlighted by surrounding it in [[ ]]. */
281 static void
282 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
283 cp_token *start_token, unsigned num,
284 cp_token *curr_token)
286 unsigned i, nprinted;
287 cp_token *token;
288 bool do_print;
290 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
292 if (buffer == NULL)
293 return;
295 if (num == 0)
296 num = buffer->length ();
298 if (start_token == NULL)
299 start_token = buffer->address ();
301 if (start_token > buffer->address ())
303 cp_lexer_print_token (file, &(*buffer)[0]);
304 fprintf (file, " ... ");
307 do_print = false;
308 nprinted = 0;
309 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
311 if (token == start_token)
312 do_print = true;
314 if (!do_print)
315 continue;
317 nprinted++;
318 if (token == curr_token)
319 fprintf (file, "[[");
321 cp_lexer_print_token (file, token);
323 if (token == curr_token)
324 fprintf (file, "]]");
326 switch (token->type)
328 case CPP_SEMICOLON:
329 case CPP_OPEN_BRACE:
330 case CPP_CLOSE_BRACE:
331 case CPP_EOF:
332 fputc ('\n', file);
333 break;
335 default:
336 fputc (' ', file);
340 if (i == num && i < buffer->length ())
342 fprintf (file, " ... ");
343 cp_lexer_print_token (file, &buffer->last ());
346 fprintf (file, "\n");
350 /* Dump all tokens in BUFFER to stderr. */
352 void
353 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
355 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
358 DEBUG_FUNCTION void
359 debug (vec<cp_token, va_gc> &ref)
361 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
364 DEBUG_FUNCTION void
365 debug (vec<cp_token, va_gc> *ptr)
367 if (ptr)
368 debug (*ptr);
369 else
370 fprintf (stderr, "<nil>\n");
374 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
375 description for T. */
377 static void
378 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
380 if (t)
382 fprintf (file, "%s: ", desc);
383 print_node_brief (file, "", t, 0);
388 /* Dump parser context C to FILE. */
390 static void
391 cp_debug_print_context (FILE *file, cp_parser_context *c)
393 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
394 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
395 print_node_brief (file, "", c->object_type, 0);
396 fprintf (file, "}\n");
400 /* Print the stack of parsing contexts to FILE starting with FIRST. */
402 static void
403 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
405 unsigned i;
406 cp_parser_context *c;
408 fprintf (file, "Parsing context stack:\n");
409 for (i = 0, c = first; c; c = c->next, i++)
411 fprintf (file, "\t#%u: ", i);
412 cp_debug_print_context (file, c);
417 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
419 static void
420 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
422 if (flag)
423 fprintf (file, "%s: true\n", desc);
427 /* Print an unparsed function entry UF to FILE. */
429 static void
430 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
432 unsigned i;
433 cp_default_arg_entry *default_arg_fn;
434 tree fn;
436 fprintf (file, "\tFunctions with default args:\n");
437 for (i = 0;
438 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
439 i++)
441 fprintf (file, "\t\tClass type: ");
442 print_node_brief (file, "", default_arg_fn->class_type, 0);
443 fprintf (file, "\t\tDeclaration: ");
444 print_node_brief (file, "", default_arg_fn->decl, 0);
445 fprintf (file, "\n");
448 fprintf (file, "\n\tFunctions with definitions that require "
449 "post-processing\n\t\t");
450 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
452 print_node_brief (file, "", fn, 0);
453 fprintf (file, " ");
455 fprintf (file, "\n");
457 fprintf (file, "\n\tNon-static data members with initializers that require "
458 "post-processing\n\t\t");
459 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
461 print_node_brief (file, "", fn, 0);
462 fprintf (file, " ");
464 fprintf (file, "\n");
468 /* Print the stack of unparsed member functions S to FILE. */
470 static void
471 cp_debug_print_unparsed_queues (FILE *file,
472 vec<cp_unparsed_functions_entry, va_gc> *s)
474 unsigned i;
475 cp_unparsed_functions_entry *uf;
477 fprintf (file, "Unparsed functions\n");
478 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
480 fprintf (file, "#%u:\n", i);
481 cp_debug_print_unparsed_function (file, uf);
486 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
487 the given PARSER. If FILE is NULL, the output is printed on stderr. */
489 static void
490 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
492 cp_token *next_token, *first_token, *start_token;
494 if (file == NULL)
495 file = stderr;
497 next_token = parser->lexer->next_token;
498 first_token = parser->lexer->buffer->address ();
499 start_token = (next_token > first_token + window_size / 2)
500 ? next_token - window_size / 2
501 : first_token;
502 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
503 next_token);
507 /* Dump debugging information for the given PARSER. If FILE is NULL,
508 the output is printed on stderr. */
510 void
511 cp_debug_parser (FILE *file, cp_parser *parser)
513 const size_t window_size = 20;
514 cp_token *token;
515 expanded_location eloc;
517 if (file == NULL)
518 file = stderr;
520 fprintf (file, "Parser state\n\n");
521 fprintf (file, "Number of tokens: %u\n",
522 vec_safe_length (parser->lexer->buffer));
523 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
524 cp_debug_print_tree_if_set (file, "Object scope",
525 parser->object_scope);
526 cp_debug_print_tree_if_set (file, "Qualifying scope",
527 parser->qualifying_scope);
528 cp_debug_print_context_stack (file, parser->context);
529 cp_debug_print_flag (file, "Allow GNU extensions",
530 parser->allow_gnu_extensions_p);
531 cp_debug_print_flag (file, "'>' token is greater-than",
532 parser->greater_than_is_operator_p);
533 cp_debug_print_flag (file, "Default args allowed in current "
534 "parameter list", parser->default_arg_ok_p);
535 cp_debug_print_flag (file, "Parsing integral constant-expression",
536 parser->integral_constant_expression_p);
537 cp_debug_print_flag (file, "Allow non-constant expression in current "
538 "constant-expression",
539 parser->allow_non_integral_constant_expression_p);
540 cp_debug_print_flag (file, "Seen non-constant expression",
541 parser->non_integral_constant_expression_p);
542 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
543 "current context",
544 parser->local_variables_forbidden_p);
545 cp_debug_print_flag (file, "In unbraced linkage specification",
546 parser->in_unbraced_linkage_specification_p);
547 cp_debug_print_flag (file, "Parsing a declarator",
548 parser->in_declarator_p);
549 cp_debug_print_flag (file, "In template argument list",
550 parser->in_template_argument_list_p);
551 cp_debug_print_flag (file, "Parsing an iteration statement",
552 parser->in_statement & IN_ITERATION_STMT);
553 cp_debug_print_flag (file, "Parsing a switch statement",
554 parser->in_statement & IN_SWITCH_STMT);
555 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
556 parser->in_statement & IN_OMP_BLOCK);
557 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
558 parser->in_statement & IN_CILK_SIMD_FOR);
559 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
560 parser->in_statement & IN_OMP_FOR);
561 cp_debug_print_flag (file, "Parsing an if statement",
562 parser->in_statement & IN_IF_STMT);
563 cp_debug_print_flag (file, "Parsing a type-id in an expression "
564 "context", parser->in_type_id_in_expr_p);
565 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
566 parser->implicit_extern_c);
567 cp_debug_print_flag (file, "String expressions should be translated "
568 "to execution character set",
569 parser->translate_strings_p);
570 cp_debug_print_flag (file, "Parsing function body outside of a "
571 "local class", parser->in_function_body);
572 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
573 parser->colon_corrects_to_scope_p);
574 cp_debug_print_flag (file, "Colon doesn't start a class definition",
575 parser->colon_doesnt_start_class_def_p);
576 if (parser->type_definition_forbidden_message)
577 fprintf (file, "Error message for forbidden type definitions: %s\n",
578 parser->type_definition_forbidden_message);
579 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
580 fprintf (file, "Number of class definitions in progress: %u\n",
581 parser->num_classes_being_defined);
582 fprintf (file, "Number of template parameter lists for the current "
583 "declaration: %u\n", parser->num_template_parameter_lists);
584 cp_debug_parser_tokens (file, parser, window_size);
585 token = parser->lexer->next_token;
586 fprintf (file, "Next token to parse:\n");
587 fprintf (file, "\tToken: ");
588 cp_lexer_print_token (file, token);
589 eloc = expand_location (token->location);
590 fprintf (file, "\n\tFile: %s\n", eloc.file);
591 fprintf (file, "\tLine: %d\n", eloc.line);
592 fprintf (file, "\tColumn: %d\n", eloc.column);
595 DEBUG_FUNCTION void
596 debug (cp_parser &ref)
598 cp_debug_parser (stderr, &ref);
601 DEBUG_FUNCTION void
602 debug (cp_parser *ptr)
604 if (ptr)
605 debug (*ptr);
606 else
607 fprintf (stderr, "<nil>\n");
610 /* Allocate memory for a new lexer object and return it. */
612 static cp_lexer *
613 cp_lexer_alloc (void)
615 cp_lexer *lexer;
617 c_common_no_more_pch ();
619 /* Allocate the memory. */
620 lexer = ggc_cleared_alloc<cp_lexer> ();
622 /* Initially we are not debugging. */
623 lexer->debugging_p = false;
625 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
627 /* Create the buffer. */
628 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
630 return lexer;
634 /* Create a new main C++ lexer, the lexer that gets tokens from the
635 preprocessor. */
637 static cp_lexer *
638 cp_lexer_new_main (void)
640 cp_lexer *lexer;
641 cp_token token;
643 /* It's possible that parsing the first pragma will load a PCH file,
644 which is a GC collection point. So we have to do that before
645 allocating any memory. */
646 cp_parser_initial_pragma (&token);
648 lexer = cp_lexer_alloc ();
650 /* Put the first token in the buffer. */
651 lexer->buffer->quick_push (token);
653 /* Get the remaining tokens from the preprocessor. */
654 while (token.type != CPP_EOF)
656 cp_lexer_get_preprocessor_token (lexer, &token);
657 vec_safe_push (lexer->buffer, token);
660 lexer->last_token = lexer->buffer->address ()
661 + lexer->buffer->length ()
662 - 1;
663 lexer->next_token = lexer->buffer->length ()
664 ? lexer->buffer->address ()
665 : &eof_token;
667 /* Subsequent preprocessor diagnostics should use compiler
668 diagnostic functions to get the compiler source location. */
669 done_lexing = true;
671 gcc_assert (!lexer->next_token->purged_p);
672 return lexer;
675 /* Create a new lexer whose token stream is primed with the tokens in
676 CACHE. When these tokens are exhausted, no new tokens will be read. */
678 static cp_lexer *
679 cp_lexer_new_from_tokens (cp_token_cache *cache)
681 cp_token *first = cache->first;
682 cp_token *last = cache->last;
683 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
685 /* We do not own the buffer. */
686 lexer->buffer = NULL;
687 lexer->next_token = first == last ? &eof_token : first;
688 lexer->last_token = last;
690 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
692 /* Initially we are not debugging. */
693 lexer->debugging_p = false;
695 gcc_assert (!lexer->next_token->purged_p);
696 return lexer;
699 /* Frees all resources associated with LEXER. */
701 static void
702 cp_lexer_destroy (cp_lexer *lexer)
704 vec_free (lexer->buffer);
705 lexer->saved_tokens.release ();
706 ggc_free (lexer);
709 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
710 be used. The point of this flag is to help the compiler to fold away calls
711 to cp_lexer_debugging_p within this source file at compile time, when the
712 lexer is not being debugged. */
714 #define LEXER_DEBUGGING_ENABLED_P false
716 /* Returns nonzero if debugging information should be output. */
718 static inline bool
719 cp_lexer_debugging_p (cp_lexer *lexer)
721 if (!LEXER_DEBUGGING_ENABLED_P)
722 return false;
724 return lexer->debugging_p;
728 static inline cp_token_position
729 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
731 gcc_assert (!previous_p || lexer->next_token != &eof_token);
733 return lexer->next_token - previous_p;
736 static inline cp_token *
737 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
739 return pos;
742 static inline void
743 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
745 lexer->next_token = cp_lexer_token_at (lexer, pos);
748 static inline cp_token_position
749 cp_lexer_previous_token_position (cp_lexer *lexer)
751 if (lexer->next_token == &eof_token)
752 return lexer->last_token - 1;
753 else
754 return cp_lexer_token_position (lexer, true);
757 static inline cp_token *
758 cp_lexer_previous_token (cp_lexer *lexer)
760 cp_token_position tp = cp_lexer_previous_token_position (lexer);
762 /* Skip past purged tokens. */
763 while (tp->purged_p)
765 gcc_assert (tp != lexer->buffer->address ());
766 tp--;
769 return cp_lexer_token_at (lexer, tp);
772 /* nonzero if we are presently saving tokens. */
774 static inline int
775 cp_lexer_saving_tokens (const cp_lexer* lexer)
777 return lexer->saved_tokens.length () != 0;
780 /* Store the next token from the preprocessor in *TOKEN. Return true
781 if we reach EOF. If LEXER is NULL, assume we are handling an
782 initial #pragma pch_preprocess, and thus want the lexer to return
783 processed strings. */
785 static void
786 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
788 static int is_extern_c = 0;
790 /* Get a new token from the preprocessor. */
791 token->type
792 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
793 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
794 token->keyword = RID_MAX;
795 token->purged_p = false;
796 token->error_reported = false;
798 /* On some systems, some header files are surrounded by an
799 implicit extern "C" block. Set a flag in the token if it
800 comes from such a header. */
801 is_extern_c += pending_lang_change;
802 pending_lang_change = 0;
803 token->implicit_extern_c = is_extern_c > 0;
805 /* Check to see if this token is a keyword. */
806 if (token->type == CPP_NAME)
808 if (C_IS_RESERVED_WORD (token->u.value))
810 /* Mark this token as a keyword. */
811 token->type = CPP_KEYWORD;
812 /* Record which keyword. */
813 token->keyword = C_RID_CODE (token->u.value);
815 else
817 if (warn_cxx11_compat
818 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
819 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
821 /* Warn about the C++0x keyword (but still treat it as
822 an identifier). */
823 warning (OPT_Wc__11_compat,
824 "identifier %qE is a keyword in C++11",
825 token->u.value);
827 /* Clear out the C_RID_CODE so we don't warn about this
828 particular identifier-turned-keyword again. */
829 C_SET_RID_CODE (token->u.value, RID_MAX);
832 token->keyword = RID_MAX;
835 else if (token->type == CPP_AT_NAME)
837 /* This only happens in Objective-C++; it must be a keyword. */
838 token->type = CPP_KEYWORD;
839 switch (C_RID_CODE (token->u.value))
841 /* Replace 'class' with '@class', 'private' with '@private',
842 etc. This prevents confusion with the C++ keyword
843 'class', and makes the tokens consistent with other
844 Objective-C 'AT' keywords. For example '@class' is
845 reported as RID_AT_CLASS which is consistent with
846 '@synchronized', which is reported as
847 RID_AT_SYNCHRONIZED.
849 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
850 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
851 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
852 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
853 case RID_THROW: token->keyword = RID_AT_THROW; break;
854 case RID_TRY: token->keyword = RID_AT_TRY; break;
855 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
856 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
857 default: token->keyword = C_RID_CODE (token->u.value);
862 /* Update the globals input_location and the input file stack from TOKEN. */
863 static inline void
864 cp_lexer_set_source_position_from_token (cp_token *token)
866 if (token->type != CPP_EOF)
868 input_location = token->location;
872 /* Update the globals input_location and the input file stack from LEXER. */
873 static inline void
874 cp_lexer_set_source_position (cp_lexer *lexer)
876 cp_token *token = cp_lexer_peek_token (lexer);
877 cp_lexer_set_source_position_from_token (token);
880 /* Return a pointer to the next token in the token stream, but do not
881 consume it. */
883 static inline cp_token *
884 cp_lexer_peek_token (cp_lexer *lexer)
886 if (cp_lexer_debugging_p (lexer))
888 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
889 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
890 putc ('\n', cp_lexer_debug_stream);
892 return lexer->next_token;
895 /* Return true if the next token has the indicated TYPE. */
897 static inline bool
898 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
900 return cp_lexer_peek_token (lexer)->type == type;
903 /* Return true if the next token does not have the indicated TYPE. */
905 static inline bool
906 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
908 return !cp_lexer_next_token_is (lexer, type);
911 /* Return true if the next token is the indicated KEYWORD. */
913 static inline bool
914 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
916 return cp_lexer_peek_token (lexer)->keyword == keyword;
919 static inline bool
920 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
922 return cp_lexer_peek_nth_token (lexer, n)->type == type;
925 static inline bool
926 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
928 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
931 /* Return true if the next token is not the indicated KEYWORD. */
933 static inline bool
934 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
936 return cp_lexer_peek_token (lexer)->keyword != keyword;
939 /* Return true if the next token is a keyword for a decl-specifier. */
941 static bool
942 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
944 cp_token *token;
946 token = cp_lexer_peek_token (lexer);
947 switch (token->keyword)
949 /* auto specifier: storage-class-specifier in C++,
950 simple-type-specifier in C++0x. */
951 case RID_AUTO:
952 /* Storage classes. */
953 case RID_REGISTER:
954 case RID_STATIC:
955 case RID_EXTERN:
956 case RID_MUTABLE:
957 case RID_THREAD:
958 /* Elaborated type specifiers. */
959 case RID_ENUM:
960 case RID_CLASS:
961 case RID_STRUCT:
962 case RID_UNION:
963 case RID_TYPENAME:
964 /* Simple type specifiers. */
965 case RID_CHAR:
966 case RID_CHAR16:
967 case RID_CHAR32:
968 case RID_WCHAR:
969 case RID_BOOL:
970 case RID_SHORT:
971 case RID_INT:
972 case RID_LONG:
973 case RID_SIGNED:
974 case RID_UNSIGNED:
975 case RID_FLOAT:
976 case RID_DOUBLE:
977 case RID_VOID:
978 /* GNU extensions. */
979 case RID_ATTRIBUTE:
980 case RID_TYPEOF:
981 /* C++0x extensions. */
982 case RID_DECLTYPE:
983 case RID_UNDERLYING_TYPE:
984 return true;
986 default:
987 if (token->keyword >= RID_FIRST_INT_N
988 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
989 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
990 return true;
991 return false;
995 /* Returns TRUE iff the token T begins a decltype type. */
997 static bool
998 token_is_decltype (cp_token *t)
1000 return (t->keyword == RID_DECLTYPE
1001 || t->type == CPP_DECLTYPE);
1004 /* Returns TRUE iff the next token begins a decltype type. */
1006 static bool
1007 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1009 cp_token *t = cp_lexer_peek_token (lexer);
1010 return token_is_decltype (t);
1013 /* Called when processing a token with tree_check_value; perform or defer the
1014 associated checks and return the value. */
1016 static tree
1017 saved_checks_value (struct tree_check *check_value)
1019 /* Perform any access checks that were deferred. */
1020 vec<deferred_access_check, va_gc> *checks;
1021 deferred_access_check *chk;
1022 checks = check_value->checks;
1023 if (checks)
1025 int i;
1026 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1027 perform_or_defer_access_check (chk->binfo,
1028 chk->decl,
1029 chk->diag_decl, tf_warning_or_error);
1031 /* Return the stored value. */
1032 return check_value->value;
1035 /* Return a pointer to the Nth token in the token stream. If N is 1,
1036 then this is precisely equivalent to cp_lexer_peek_token (except
1037 that it is not inline). One would like to disallow that case, but
1038 there is one case (cp_parser_nth_token_starts_template_id) where
1039 the caller passes a variable for N and it might be 1. */
1041 static cp_token *
1042 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1044 cp_token *token;
1046 /* N is 1-based, not zero-based. */
1047 gcc_assert (n > 0);
1049 if (cp_lexer_debugging_p (lexer))
1050 fprintf (cp_lexer_debug_stream,
1051 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1053 --n;
1054 token = lexer->next_token;
1055 gcc_assert (!n || token != &eof_token);
1056 while (n != 0)
1058 ++token;
1059 if (token == lexer->last_token)
1061 token = &eof_token;
1062 break;
1065 if (!token->purged_p)
1066 --n;
1069 if (cp_lexer_debugging_p (lexer))
1071 cp_lexer_print_token (cp_lexer_debug_stream, token);
1072 putc ('\n', cp_lexer_debug_stream);
1075 return token;
1078 /* Return the next token, and advance the lexer's next_token pointer
1079 to point to the next non-purged token. */
1081 static cp_token *
1082 cp_lexer_consume_token (cp_lexer* lexer)
1084 cp_token *token = lexer->next_token;
1086 gcc_assert (token != &eof_token);
1087 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1091 lexer->next_token++;
1092 if (lexer->next_token == lexer->last_token)
1094 lexer->next_token = &eof_token;
1095 break;
1099 while (lexer->next_token->purged_p);
1101 cp_lexer_set_source_position_from_token (token);
1103 /* Provide debugging output. */
1104 if (cp_lexer_debugging_p (lexer))
1106 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1107 cp_lexer_print_token (cp_lexer_debug_stream, token);
1108 putc ('\n', cp_lexer_debug_stream);
1111 return token;
1114 /* Permanently remove the next token from the token stream, and
1115 advance the next_token pointer to refer to the next non-purged
1116 token. */
1118 static void
1119 cp_lexer_purge_token (cp_lexer *lexer)
1121 cp_token *tok = lexer->next_token;
1123 gcc_assert (tok != &eof_token);
1124 tok->purged_p = true;
1125 tok->location = UNKNOWN_LOCATION;
1126 tok->u.value = NULL_TREE;
1127 tok->keyword = RID_MAX;
1131 tok++;
1132 if (tok == lexer->last_token)
1134 tok = &eof_token;
1135 break;
1138 while (tok->purged_p);
1139 lexer->next_token = tok;
1142 /* Permanently remove all tokens after TOK, up to, but not
1143 including, the token that will be returned next by
1144 cp_lexer_peek_token. */
1146 static void
1147 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1149 cp_token *peek = lexer->next_token;
1151 if (peek == &eof_token)
1152 peek = lexer->last_token;
1154 gcc_assert (tok < peek);
1156 for ( tok += 1; tok != peek; tok += 1)
1158 tok->purged_p = true;
1159 tok->location = UNKNOWN_LOCATION;
1160 tok->u.value = NULL_TREE;
1161 tok->keyword = RID_MAX;
1165 /* Begin saving tokens. All tokens consumed after this point will be
1166 preserved. */
1168 static void
1169 cp_lexer_save_tokens (cp_lexer* lexer)
1171 /* Provide debugging output. */
1172 if (cp_lexer_debugging_p (lexer))
1173 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1175 lexer->saved_tokens.safe_push (lexer->next_token);
1178 /* Commit to the portion of the token stream most recently saved. */
1180 static void
1181 cp_lexer_commit_tokens (cp_lexer* lexer)
1183 /* Provide debugging output. */
1184 if (cp_lexer_debugging_p (lexer))
1185 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1187 lexer->saved_tokens.pop ();
1190 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1191 to the token stream. Stop saving tokens. */
1193 static void
1194 cp_lexer_rollback_tokens (cp_lexer* lexer)
1196 /* Provide debugging output. */
1197 if (cp_lexer_debugging_p (lexer))
1198 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1200 lexer->next_token = lexer->saved_tokens.pop ();
1203 /* RAII wrapper around the above functions, with sanity checking. Creating
1204 a variable saves tokens, which are committed when the variable is
1205 destroyed unless they are explicitly rolled back by calling the rollback
1206 member function. */
1208 struct saved_token_sentinel
1210 cp_lexer *lexer;
1211 unsigned len;
1212 bool commit;
1213 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1215 len = lexer->saved_tokens.length ();
1216 cp_lexer_save_tokens (lexer);
1218 void rollback ()
1220 cp_lexer_rollback_tokens (lexer);
1221 commit = false;
1223 ~saved_token_sentinel()
1225 if (commit)
1226 cp_lexer_commit_tokens (lexer);
1227 gcc_assert (lexer->saved_tokens.length () == len);
1231 /* Print a representation of the TOKEN on the STREAM. */
1233 static void
1234 cp_lexer_print_token (FILE * stream, cp_token *token)
1236 /* We don't use cpp_type2name here because the parser defines
1237 a few tokens of its own. */
1238 static const char *const token_names[] = {
1239 /* cpplib-defined token types */
1240 #define OP(e, s) #e,
1241 #define TK(e, s) #e,
1242 TTYPE_TABLE
1243 #undef OP
1244 #undef TK
1245 /* C++ parser token types - see "Manifest constants", above. */
1246 "KEYWORD",
1247 "TEMPLATE_ID",
1248 "NESTED_NAME_SPECIFIER",
1251 /* For some tokens, print the associated data. */
1252 switch (token->type)
1254 case CPP_KEYWORD:
1255 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1256 For example, `struct' is mapped to an INTEGER_CST. */
1257 if (!identifier_p (token->u.value))
1258 break;
1259 /* else fall through */
1260 case CPP_NAME:
1261 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1262 break;
1264 case CPP_STRING:
1265 case CPP_STRING16:
1266 case CPP_STRING32:
1267 case CPP_WSTRING:
1268 case CPP_UTF8STRING:
1269 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1270 break;
1272 case CPP_NUMBER:
1273 print_generic_expr (stream, token->u.value, 0);
1274 break;
1276 default:
1277 /* If we have a name for the token, print it out. Otherwise, we
1278 simply give the numeric code. */
1279 if (token->type < ARRAY_SIZE(token_names))
1280 fputs (token_names[token->type], stream);
1281 else
1282 fprintf (stream, "[%d]", token->type);
1283 break;
1287 DEBUG_FUNCTION void
1288 debug (cp_token &ref)
1290 cp_lexer_print_token (stderr, &ref);
1291 fprintf (stderr, "\n");
1294 DEBUG_FUNCTION void
1295 debug (cp_token *ptr)
1297 if (ptr)
1298 debug (*ptr);
1299 else
1300 fprintf (stderr, "<nil>\n");
1304 /* Start emitting debugging information. */
1306 static void
1307 cp_lexer_start_debugging (cp_lexer* lexer)
1309 if (!LEXER_DEBUGGING_ENABLED_P)
1310 fatal_error (input_location,
1311 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1313 lexer->debugging_p = true;
1314 cp_lexer_debug_stream = stderr;
1317 /* Stop emitting debugging information. */
1319 static void
1320 cp_lexer_stop_debugging (cp_lexer* lexer)
1322 if (!LEXER_DEBUGGING_ENABLED_P)
1323 fatal_error (input_location,
1324 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1326 lexer->debugging_p = false;
1327 cp_lexer_debug_stream = NULL;
1330 /* Create a new cp_token_cache, representing a range of tokens. */
1332 static cp_token_cache *
1333 cp_token_cache_new (cp_token *first, cp_token *last)
1335 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1336 cache->first = first;
1337 cache->last = last;
1338 return cache;
1341 /* Diagnose if #pragma omp declare simd isn't followed immediately
1342 by function declaration or definition. */
1344 static inline void
1345 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1347 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1349 error ("%<#pragma omp declare simd%> not immediately followed by "
1350 "function declaration or definition");
1351 parser->omp_declare_simd = NULL;
1355 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1356 and put that into "omp declare simd" attribute. */
1358 static inline void
1359 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1361 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1363 if (fndecl == error_mark_node)
1365 parser->omp_declare_simd = NULL;
1366 return;
1368 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1370 cp_ensure_no_omp_declare_simd (parser);
1371 return;
1376 /* Diagnose if #pragma acc routine isn't followed immediately by function
1377 declaration or definition. */
1379 static inline void
1380 cp_ensure_no_oacc_routine (cp_parser *parser)
1382 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1384 tree clauses = parser->oacc_routine->clauses;
1385 location_t loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE (clauses));
1387 error_at (loc, "%<#pragma acc routine%> not followed by a function "
1388 "declaration or definition");
1389 parser->oacc_routine = NULL;
1393 /* Decl-specifiers. */
1395 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1397 static void
1398 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1400 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1403 /* Declarators. */
1405 /* Nothing other than the parser should be creating declarators;
1406 declarators are a semi-syntactic representation of C++ entities.
1407 Other parts of the front end that need to create entities (like
1408 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1410 static cp_declarator *make_call_declarator
1411 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1412 static cp_declarator *make_array_declarator
1413 (cp_declarator *, tree);
1414 static cp_declarator *make_pointer_declarator
1415 (cp_cv_quals, cp_declarator *, tree);
1416 static cp_declarator *make_reference_declarator
1417 (cp_cv_quals, cp_declarator *, bool, tree);
1418 static cp_declarator *make_ptrmem_declarator
1419 (cp_cv_quals, tree, cp_declarator *, tree);
1421 /* An erroneous declarator. */
1422 static cp_declarator *cp_error_declarator;
1424 /* The obstack on which declarators and related data structures are
1425 allocated. */
1426 static struct obstack declarator_obstack;
1428 /* Alloc BYTES from the declarator memory pool. */
1430 static inline void *
1431 alloc_declarator (size_t bytes)
1433 return obstack_alloc (&declarator_obstack, bytes);
1436 /* Allocate a declarator of the indicated KIND. Clear fields that are
1437 common to all declarators. */
1439 static cp_declarator *
1440 make_declarator (cp_declarator_kind kind)
1442 cp_declarator *declarator;
1444 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1445 declarator->kind = kind;
1446 declarator->attributes = NULL_TREE;
1447 declarator->std_attributes = NULL_TREE;
1448 declarator->declarator = NULL;
1449 declarator->parameter_pack_p = false;
1450 declarator->id_loc = UNKNOWN_LOCATION;
1452 return declarator;
1455 /* Make a declarator for a generalized identifier. If
1456 QUALIFYING_SCOPE is non-NULL, the identifier is
1457 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1458 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1459 is, if any. */
1461 static cp_declarator *
1462 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1463 special_function_kind sfk)
1465 cp_declarator *declarator;
1467 /* It is valid to write:
1469 class C { void f(); };
1470 typedef C D;
1471 void D::f();
1473 The standard is not clear about whether `typedef const C D' is
1474 legal; as of 2002-09-15 the committee is considering that
1475 question. EDG 3.0 allows that syntax. Therefore, we do as
1476 well. */
1477 if (qualifying_scope && TYPE_P (qualifying_scope))
1478 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1480 gcc_assert (identifier_p (unqualified_name)
1481 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1482 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1484 declarator = make_declarator (cdk_id);
1485 declarator->u.id.qualifying_scope = qualifying_scope;
1486 declarator->u.id.unqualified_name = unqualified_name;
1487 declarator->u.id.sfk = sfk;
1489 return declarator;
1492 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1493 of modifiers such as const or volatile to apply to the pointer
1494 type, represented as identifiers. ATTRIBUTES represent the attributes that
1495 appertain to the pointer or reference. */
1497 cp_declarator *
1498 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1499 tree attributes)
1501 cp_declarator *declarator;
1503 declarator = make_declarator (cdk_pointer);
1504 declarator->declarator = target;
1505 declarator->u.pointer.qualifiers = cv_qualifiers;
1506 declarator->u.pointer.class_type = NULL_TREE;
1507 if (target)
1509 declarator->id_loc = target->id_loc;
1510 declarator->parameter_pack_p = target->parameter_pack_p;
1511 target->parameter_pack_p = false;
1513 else
1514 declarator->parameter_pack_p = false;
1516 declarator->std_attributes = attributes;
1518 return declarator;
1521 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1522 represent the attributes that appertain to the pointer or
1523 reference. */
1525 cp_declarator *
1526 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1527 bool rvalue_ref, tree attributes)
1529 cp_declarator *declarator;
1531 declarator = make_declarator (cdk_reference);
1532 declarator->declarator = target;
1533 declarator->u.reference.qualifiers = cv_qualifiers;
1534 declarator->u.reference.rvalue_ref = rvalue_ref;
1535 if (target)
1537 declarator->id_loc = target->id_loc;
1538 declarator->parameter_pack_p = target->parameter_pack_p;
1539 target->parameter_pack_p = false;
1541 else
1542 declarator->parameter_pack_p = false;
1544 declarator->std_attributes = attributes;
1546 return declarator;
1549 /* Like make_pointer_declarator -- but for a pointer to a non-static
1550 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1551 appertain to the pointer or reference. */
1553 cp_declarator *
1554 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1555 cp_declarator *pointee,
1556 tree attributes)
1558 cp_declarator *declarator;
1560 declarator = make_declarator (cdk_ptrmem);
1561 declarator->declarator = pointee;
1562 declarator->u.pointer.qualifiers = cv_qualifiers;
1563 declarator->u.pointer.class_type = class_type;
1565 if (pointee)
1567 declarator->parameter_pack_p = pointee->parameter_pack_p;
1568 pointee->parameter_pack_p = false;
1570 else
1571 declarator->parameter_pack_p = false;
1573 declarator->std_attributes = attributes;
1575 return declarator;
1578 /* Make a declarator for the function given by TARGET, with the
1579 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1580 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1581 indicates what exceptions can be thrown. */
1583 cp_declarator *
1584 make_call_declarator (cp_declarator *target,
1585 tree parms,
1586 cp_cv_quals cv_qualifiers,
1587 cp_virt_specifiers virt_specifiers,
1588 cp_ref_qualifier ref_qualifier,
1589 tree tx_qualifier,
1590 tree exception_specification,
1591 tree late_return_type,
1592 tree requires_clause)
1594 cp_declarator *declarator;
1596 declarator = make_declarator (cdk_function);
1597 declarator->declarator = target;
1598 declarator->u.function.parameters = parms;
1599 declarator->u.function.qualifiers = cv_qualifiers;
1600 declarator->u.function.virt_specifiers = virt_specifiers;
1601 declarator->u.function.ref_qualifier = ref_qualifier;
1602 declarator->u.function.tx_qualifier = tx_qualifier;
1603 declarator->u.function.exception_specification = exception_specification;
1604 declarator->u.function.late_return_type = late_return_type;
1605 declarator->u.function.requires_clause = requires_clause;
1606 if (target)
1608 declarator->id_loc = target->id_loc;
1609 declarator->parameter_pack_p = target->parameter_pack_p;
1610 target->parameter_pack_p = false;
1612 else
1613 declarator->parameter_pack_p = false;
1615 return declarator;
1618 /* Make a declarator for an array of BOUNDS elements, each of which is
1619 defined by ELEMENT. */
1621 cp_declarator *
1622 make_array_declarator (cp_declarator *element, tree bounds)
1624 cp_declarator *declarator;
1626 declarator = make_declarator (cdk_array);
1627 declarator->declarator = element;
1628 declarator->u.array.bounds = bounds;
1629 if (element)
1631 declarator->id_loc = element->id_loc;
1632 declarator->parameter_pack_p = element->parameter_pack_p;
1633 element->parameter_pack_p = false;
1635 else
1636 declarator->parameter_pack_p = false;
1638 return declarator;
1641 /* Determine whether the declarator we've seen so far can be a
1642 parameter pack, when followed by an ellipsis. */
1643 static bool
1644 declarator_can_be_parameter_pack (cp_declarator *declarator)
1646 if (declarator && declarator->parameter_pack_p)
1647 /* We already saw an ellipsis. */
1648 return false;
1650 /* Search for a declarator name, or any other declarator that goes
1651 after the point where the ellipsis could appear in a parameter
1652 pack. If we find any of these, then this declarator can not be
1653 made into a parameter pack. */
1654 bool found = false;
1655 while (declarator && !found)
1657 switch ((int)declarator->kind)
1659 case cdk_id:
1660 case cdk_array:
1661 found = true;
1662 break;
1664 case cdk_error:
1665 return true;
1667 default:
1668 declarator = declarator->declarator;
1669 break;
1673 return !found;
1676 cp_parameter_declarator *no_parameters;
1678 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1679 DECLARATOR and DEFAULT_ARGUMENT. */
1681 cp_parameter_declarator *
1682 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1683 cp_declarator *declarator,
1684 tree default_argument,
1685 bool template_parameter_pack_p = false)
1687 cp_parameter_declarator *parameter;
1689 parameter = ((cp_parameter_declarator *)
1690 alloc_declarator (sizeof (cp_parameter_declarator)));
1691 parameter->next = NULL;
1692 if (decl_specifiers)
1693 parameter->decl_specifiers = *decl_specifiers;
1694 else
1695 clear_decl_specs (&parameter->decl_specifiers);
1696 parameter->declarator = declarator;
1697 parameter->default_argument = default_argument;
1698 parameter->template_parameter_pack_p = template_parameter_pack_p;
1700 return parameter;
1703 /* Returns true iff DECLARATOR is a declaration for a function. */
1705 static bool
1706 function_declarator_p (const cp_declarator *declarator)
1708 while (declarator)
1710 if (declarator->kind == cdk_function
1711 && declarator->declarator->kind == cdk_id)
1712 return true;
1713 if (declarator->kind == cdk_id
1714 || declarator->kind == cdk_error)
1715 return false;
1716 declarator = declarator->declarator;
1718 return false;
1721 /* The parser. */
1723 /* Overview
1724 --------
1726 A cp_parser parses the token stream as specified by the C++
1727 grammar. Its job is purely parsing, not semantic analysis. For
1728 example, the parser breaks the token stream into declarators,
1729 expressions, statements, and other similar syntactic constructs.
1730 It does not check that the types of the expressions on either side
1731 of an assignment-statement are compatible, or that a function is
1732 not declared with a parameter of type `void'.
1734 The parser invokes routines elsewhere in the compiler to perform
1735 semantic analysis and to build up the abstract syntax tree for the
1736 code processed.
1738 The parser (and the template instantiation code, which is, in a
1739 way, a close relative of parsing) are the only parts of the
1740 compiler that should be calling push_scope and pop_scope, or
1741 related functions. The parser (and template instantiation code)
1742 keeps track of what scope is presently active; everything else
1743 should simply honor that. (The code that generates static
1744 initializers may also need to set the scope, in order to check
1745 access control correctly when emitting the initializers.)
1747 Methodology
1748 -----------
1750 The parser is of the standard recursive-descent variety. Upcoming
1751 tokens in the token stream are examined in order to determine which
1752 production to use when parsing a non-terminal. Some C++ constructs
1753 require arbitrary look ahead to disambiguate. For example, it is
1754 impossible, in the general case, to tell whether a statement is an
1755 expression or declaration without scanning the entire statement.
1756 Therefore, the parser is capable of "parsing tentatively." When the
1757 parser is not sure what construct comes next, it enters this mode.
1758 Then, while we attempt to parse the construct, the parser queues up
1759 error messages, rather than issuing them immediately, and saves the
1760 tokens it consumes. If the construct is parsed successfully, the
1761 parser "commits", i.e., it issues any queued error messages and
1762 the tokens that were being preserved are permanently discarded.
1763 If, however, the construct is not parsed successfully, the parser
1764 rolls back its state completely so that it can resume parsing using
1765 a different alternative.
1767 Future Improvements
1768 -------------------
1770 The performance of the parser could probably be improved substantially.
1771 We could often eliminate the need to parse tentatively by looking ahead
1772 a little bit. In some places, this approach might not entirely eliminate
1773 the need to parse tentatively, but it might still speed up the average
1774 case. */
1776 /* Flags that are passed to some parsing functions. These values can
1777 be bitwise-ored together. */
1779 enum
1781 /* No flags. */
1782 CP_PARSER_FLAGS_NONE = 0x0,
1783 /* The construct is optional. If it is not present, then no error
1784 should be issued. */
1785 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1786 /* When parsing a type-specifier, treat user-defined type-names
1787 as non-type identifiers. */
1788 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1789 /* When parsing a type-specifier, do not try to parse a class-specifier
1790 or enum-specifier. */
1791 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1792 /* When parsing a decl-specifier-seq, only allow type-specifier or
1793 constexpr. */
1794 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1797 /* This type is used for parameters and variables which hold
1798 combinations of the above flags. */
1799 typedef int cp_parser_flags;
1801 /* The different kinds of declarators we want to parse. */
1803 enum cp_parser_declarator_kind
1805 /* We want an abstract declarator. */
1806 CP_PARSER_DECLARATOR_ABSTRACT,
1807 /* We want a named declarator. */
1808 CP_PARSER_DECLARATOR_NAMED,
1809 /* We don't mind, but the name must be an unqualified-id. */
1810 CP_PARSER_DECLARATOR_EITHER
1813 /* The precedence values used to parse binary expressions. The minimum value
1814 of PREC must be 1, because zero is reserved to quickly discriminate
1815 binary operators from other tokens. */
1817 enum cp_parser_prec
1819 PREC_NOT_OPERATOR,
1820 PREC_LOGICAL_OR_EXPRESSION,
1821 PREC_LOGICAL_AND_EXPRESSION,
1822 PREC_INCLUSIVE_OR_EXPRESSION,
1823 PREC_EXCLUSIVE_OR_EXPRESSION,
1824 PREC_AND_EXPRESSION,
1825 PREC_EQUALITY_EXPRESSION,
1826 PREC_RELATIONAL_EXPRESSION,
1827 PREC_SHIFT_EXPRESSION,
1828 PREC_ADDITIVE_EXPRESSION,
1829 PREC_MULTIPLICATIVE_EXPRESSION,
1830 PREC_PM_EXPRESSION,
1831 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1834 /* A mapping from a token type to a corresponding tree node type, with a
1835 precedence value. */
1837 struct cp_parser_binary_operations_map_node
1839 /* The token type. */
1840 enum cpp_ttype token_type;
1841 /* The corresponding tree code. */
1842 enum tree_code tree_type;
1843 /* The precedence of this operator. */
1844 enum cp_parser_prec prec;
1847 struct cp_parser_expression_stack_entry
1849 /* Left hand side of the binary operation we are currently
1850 parsing. */
1851 cp_expr lhs;
1852 /* Original tree code for left hand side, if it was a binary
1853 expression itself (used for -Wparentheses). */
1854 enum tree_code lhs_type;
1855 /* Tree code for the binary operation we are parsing. */
1856 enum tree_code tree_type;
1857 /* Precedence of the binary operation we are parsing. */
1858 enum cp_parser_prec prec;
1859 /* Location of the binary operation we are parsing. */
1860 location_t loc;
1863 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1864 entries because precedence levels on the stack are monotonically
1865 increasing. */
1866 typedef struct cp_parser_expression_stack_entry
1867 cp_parser_expression_stack[NUM_PREC_VALUES];
1869 /* Prototypes. */
1871 /* Constructors and destructors. */
1873 static cp_parser_context *cp_parser_context_new
1874 (cp_parser_context *);
1876 /* Class variables. */
1878 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1880 /* The operator-precedence table used by cp_parser_binary_expression.
1881 Transformed into an associative array (binops_by_token) by
1882 cp_parser_new. */
1884 static const cp_parser_binary_operations_map_node binops[] = {
1885 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1886 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1888 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1889 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1890 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1892 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1893 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1895 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1896 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1898 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1899 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1900 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1901 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1903 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1904 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1906 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1908 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1910 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1912 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1914 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1917 /* The same as binops, but initialized by cp_parser_new so that
1918 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1919 for speed. */
1920 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1922 /* Constructors and destructors. */
1924 /* Construct a new context. The context below this one on the stack
1925 is given by NEXT. */
1927 static cp_parser_context *
1928 cp_parser_context_new (cp_parser_context* next)
1930 cp_parser_context *context;
1932 /* Allocate the storage. */
1933 if (cp_parser_context_free_list != NULL)
1935 /* Pull the first entry from the free list. */
1936 context = cp_parser_context_free_list;
1937 cp_parser_context_free_list = context->next;
1938 memset (context, 0, sizeof (*context));
1940 else
1941 context = ggc_cleared_alloc<cp_parser_context> ();
1943 /* No errors have occurred yet in this context. */
1944 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1945 /* If this is not the bottommost context, copy information that we
1946 need from the previous context. */
1947 if (next)
1949 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1950 expression, then we are parsing one in this context, too. */
1951 context->object_type = next->object_type;
1952 /* Thread the stack. */
1953 context->next = next;
1956 return context;
1959 /* Managing the unparsed function queues. */
1961 #define unparsed_funs_with_default_args \
1962 parser->unparsed_queues->last ().funs_with_default_args
1963 #define unparsed_funs_with_definitions \
1964 parser->unparsed_queues->last ().funs_with_definitions
1965 #define unparsed_nsdmis \
1966 parser->unparsed_queues->last ().nsdmis
1967 #define unparsed_classes \
1968 parser->unparsed_queues->last ().classes
1970 static void
1971 push_unparsed_function_queues (cp_parser *parser)
1973 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1974 vec_safe_push (parser->unparsed_queues, e);
1977 static void
1978 pop_unparsed_function_queues (cp_parser *parser)
1980 release_tree_vector (unparsed_funs_with_definitions);
1981 parser->unparsed_queues->pop ();
1984 /* Prototypes. */
1986 /* Constructors and destructors. */
1988 static cp_parser *cp_parser_new
1989 (void);
1991 /* Routines to parse various constructs.
1993 Those that return `tree' will return the error_mark_node (rather
1994 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1995 Sometimes, they will return an ordinary node if error-recovery was
1996 attempted, even though a parse error occurred. So, to check
1997 whether or not a parse error occurred, you should always use
1998 cp_parser_error_occurred. If the construct is optional (indicated
1999 either by an `_opt' in the name of the function that does the
2000 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2001 the construct is not present. */
2003 /* Lexical conventions [gram.lex] */
2005 static cp_expr cp_parser_identifier
2006 (cp_parser *);
2007 static cp_expr cp_parser_string_literal
2008 (cp_parser *, bool, bool, bool);
2009 static cp_expr cp_parser_userdef_char_literal
2010 (cp_parser *);
2011 static tree cp_parser_userdef_string_literal
2012 (tree);
2013 static cp_expr cp_parser_userdef_numeric_literal
2014 (cp_parser *);
2016 /* Basic concepts [gram.basic] */
2018 static bool cp_parser_translation_unit
2019 (cp_parser *);
2021 /* Expressions [gram.expr] */
2023 static cp_expr cp_parser_primary_expression
2024 (cp_parser *, bool, bool, bool, cp_id_kind *);
2025 static cp_expr cp_parser_id_expression
2026 (cp_parser *, bool, bool, bool *, bool, bool);
2027 static cp_expr cp_parser_unqualified_id
2028 (cp_parser *, bool, bool, bool, bool);
2029 static tree cp_parser_nested_name_specifier_opt
2030 (cp_parser *, bool, bool, bool, bool);
2031 static tree cp_parser_nested_name_specifier
2032 (cp_parser *, bool, bool, bool, bool);
2033 static tree cp_parser_qualifying_entity
2034 (cp_parser *, bool, bool, bool, bool, bool);
2035 static cp_expr cp_parser_postfix_expression
2036 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2037 static tree cp_parser_postfix_open_square_expression
2038 (cp_parser *, tree, bool, bool);
2039 static tree cp_parser_postfix_dot_deref_expression
2040 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2041 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2042 (cp_parser *, int, bool, bool, bool *, location_t * = NULL);
2043 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2044 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2045 static void cp_parser_pseudo_destructor_name
2046 (cp_parser *, tree, tree *, tree *);
2047 static cp_expr cp_parser_unary_expression
2048 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2049 static enum tree_code cp_parser_unary_operator
2050 (cp_token *);
2051 static tree cp_parser_new_expression
2052 (cp_parser *);
2053 static vec<tree, va_gc> *cp_parser_new_placement
2054 (cp_parser *);
2055 static tree cp_parser_new_type_id
2056 (cp_parser *, tree *);
2057 static cp_declarator *cp_parser_new_declarator_opt
2058 (cp_parser *);
2059 static cp_declarator *cp_parser_direct_new_declarator
2060 (cp_parser *);
2061 static vec<tree, va_gc> *cp_parser_new_initializer
2062 (cp_parser *);
2063 static tree cp_parser_delete_expression
2064 (cp_parser *);
2065 static cp_expr cp_parser_cast_expression
2066 (cp_parser *, bool, bool, bool, cp_id_kind *);
2067 static cp_expr cp_parser_binary_expression
2068 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2069 static tree cp_parser_question_colon_clause
2070 (cp_parser *, cp_expr);
2071 static cp_expr cp_parser_assignment_expression
2072 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2073 static enum tree_code cp_parser_assignment_operator_opt
2074 (cp_parser *);
2075 static cp_expr cp_parser_expression
2076 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2077 static cp_expr cp_parser_constant_expression
2078 (cp_parser *, bool = false, bool * = NULL);
2079 static cp_expr cp_parser_builtin_offsetof
2080 (cp_parser *);
2081 static cp_expr cp_parser_lambda_expression
2082 (cp_parser *);
2083 static void cp_parser_lambda_introducer
2084 (cp_parser *, tree);
2085 static bool cp_parser_lambda_declarator_opt
2086 (cp_parser *, tree);
2087 static void cp_parser_lambda_body
2088 (cp_parser *, tree);
2090 /* Statements [gram.stmt.stmt] */
2092 static void cp_parser_statement
2093 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL);
2094 static void cp_parser_label_for_labeled_statement
2095 (cp_parser *, tree);
2096 static tree cp_parser_expression_statement
2097 (cp_parser *, tree);
2098 static tree cp_parser_compound_statement
2099 (cp_parser *, tree, int, bool);
2100 static void cp_parser_statement_seq_opt
2101 (cp_parser *, tree);
2102 static tree cp_parser_selection_statement
2103 (cp_parser *, bool *, vec<tree> *);
2104 static tree cp_parser_condition
2105 (cp_parser *);
2106 static tree cp_parser_iteration_statement
2107 (cp_parser *, bool);
2108 static bool cp_parser_for_init_statement
2109 (cp_parser *, tree *decl);
2110 static tree cp_parser_for
2111 (cp_parser *, bool);
2112 static tree cp_parser_c_for
2113 (cp_parser *, tree, tree, bool);
2114 static tree cp_parser_range_for
2115 (cp_parser *, tree, tree, tree, bool);
2116 static void do_range_for_auto_deduction
2117 (tree, tree);
2118 static tree cp_parser_perform_range_for_lookup
2119 (tree, tree *, tree *);
2120 static tree cp_parser_range_for_member_function
2121 (tree, tree);
2122 static tree cp_parser_jump_statement
2123 (cp_parser *);
2124 static void cp_parser_declaration_statement
2125 (cp_parser *);
2127 static tree cp_parser_implicitly_scoped_statement
2128 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2129 static void cp_parser_already_scoped_statement
2130 (cp_parser *, const token_indent_info &);
2132 /* Declarations [gram.dcl.dcl] */
2134 static void cp_parser_declaration_seq_opt
2135 (cp_parser *);
2136 static void cp_parser_declaration
2137 (cp_parser *);
2138 static void cp_parser_block_declaration
2139 (cp_parser *, bool);
2140 static void cp_parser_simple_declaration
2141 (cp_parser *, bool, tree *);
2142 static void cp_parser_decl_specifier_seq
2143 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2144 static tree cp_parser_storage_class_specifier_opt
2145 (cp_parser *);
2146 static tree cp_parser_function_specifier_opt
2147 (cp_parser *, cp_decl_specifier_seq *);
2148 static tree cp_parser_type_specifier
2149 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2150 int *, bool *);
2151 static tree cp_parser_simple_type_specifier
2152 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2153 static tree cp_parser_type_name
2154 (cp_parser *, bool);
2155 static tree cp_parser_type_name
2156 (cp_parser *);
2157 static tree cp_parser_nonclass_name
2158 (cp_parser* parser);
2159 static tree cp_parser_elaborated_type_specifier
2160 (cp_parser *, bool, bool);
2161 static tree cp_parser_enum_specifier
2162 (cp_parser *);
2163 static void cp_parser_enumerator_list
2164 (cp_parser *, tree);
2165 static void cp_parser_enumerator_definition
2166 (cp_parser *, tree);
2167 static tree cp_parser_namespace_name
2168 (cp_parser *);
2169 static void cp_parser_namespace_definition
2170 (cp_parser *);
2171 static void cp_parser_namespace_body
2172 (cp_parser *);
2173 static tree cp_parser_qualified_namespace_specifier
2174 (cp_parser *);
2175 static void cp_parser_namespace_alias_definition
2176 (cp_parser *);
2177 static bool cp_parser_using_declaration
2178 (cp_parser *, bool);
2179 static void cp_parser_using_directive
2180 (cp_parser *);
2181 static tree cp_parser_alias_declaration
2182 (cp_parser *);
2183 static void cp_parser_asm_definition
2184 (cp_parser *);
2185 static void cp_parser_linkage_specification
2186 (cp_parser *);
2187 static void cp_parser_static_assert
2188 (cp_parser *, bool);
2189 static tree cp_parser_decltype
2190 (cp_parser *);
2192 /* Declarators [gram.dcl.decl] */
2194 static tree cp_parser_init_declarator
2195 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2196 bool, bool, int, bool *, tree *, location_t *);
2197 static cp_declarator *cp_parser_declarator
2198 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2199 static cp_declarator *cp_parser_direct_declarator
2200 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2201 static enum tree_code cp_parser_ptr_operator
2202 (cp_parser *, tree *, cp_cv_quals *, tree *);
2203 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2204 (cp_parser *);
2205 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2206 (cp_parser *);
2207 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2208 (cp_parser *);
2209 static tree cp_parser_tx_qualifier_opt
2210 (cp_parser *);
2211 static tree cp_parser_late_return_type_opt
2212 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2213 static tree cp_parser_declarator_id
2214 (cp_parser *, bool);
2215 static tree cp_parser_type_id
2216 (cp_parser *);
2217 static tree cp_parser_template_type_arg
2218 (cp_parser *);
2219 static tree cp_parser_trailing_type_id (cp_parser *);
2220 static tree cp_parser_type_id_1
2221 (cp_parser *, bool, bool);
2222 static void cp_parser_type_specifier_seq
2223 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2224 static tree cp_parser_parameter_declaration_clause
2225 (cp_parser *);
2226 static tree cp_parser_parameter_declaration_list
2227 (cp_parser *, bool *);
2228 static cp_parameter_declarator *cp_parser_parameter_declaration
2229 (cp_parser *, bool, bool *);
2230 static tree cp_parser_default_argument
2231 (cp_parser *, bool);
2232 static void cp_parser_function_body
2233 (cp_parser *, bool);
2234 static tree cp_parser_initializer
2235 (cp_parser *, bool *, bool *);
2236 static cp_expr cp_parser_initializer_clause
2237 (cp_parser *, bool *);
2238 static cp_expr cp_parser_braced_list
2239 (cp_parser*, bool*);
2240 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2241 (cp_parser *, bool *);
2243 static bool cp_parser_ctor_initializer_opt_and_function_body
2244 (cp_parser *, bool);
2246 static tree cp_parser_late_parsing_omp_declare_simd
2247 (cp_parser *, tree);
2249 static tree cp_parser_late_parsing_cilk_simd_fn_info
2250 (cp_parser *, tree);
2252 static tree cp_parser_late_parsing_oacc_routine
2253 (cp_parser *, tree);
2255 static tree synthesize_implicit_template_parm
2256 (cp_parser *, tree);
2257 static tree finish_fully_implicit_template
2258 (cp_parser *, tree);
2260 /* Classes [gram.class] */
2262 static tree cp_parser_class_name
2263 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2264 static tree cp_parser_class_specifier
2265 (cp_parser *);
2266 static tree cp_parser_class_head
2267 (cp_parser *, bool *);
2268 static enum tag_types cp_parser_class_key
2269 (cp_parser *);
2270 static void cp_parser_type_parameter_key
2271 (cp_parser* parser);
2272 static void cp_parser_member_specification_opt
2273 (cp_parser *);
2274 static void cp_parser_member_declaration
2275 (cp_parser *);
2276 static tree cp_parser_pure_specifier
2277 (cp_parser *);
2278 static tree cp_parser_constant_initializer
2279 (cp_parser *);
2281 /* Derived classes [gram.class.derived] */
2283 static tree cp_parser_base_clause
2284 (cp_parser *);
2285 static tree cp_parser_base_specifier
2286 (cp_parser *);
2288 /* Special member functions [gram.special] */
2290 static tree cp_parser_conversion_function_id
2291 (cp_parser *);
2292 static tree cp_parser_conversion_type_id
2293 (cp_parser *);
2294 static cp_declarator *cp_parser_conversion_declarator_opt
2295 (cp_parser *);
2296 static bool cp_parser_ctor_initializer_opt
2297 (cp_parser *);
2298 static void cp_parser_mem_initializer_list
2299 (cp_parser *);
2300 static tree cp_parser_mem_initializer
2301 (cp_parser *);
2302 static tree cp_parser_mem_initializer_id
2303 (cp_parser *);
2305 /* Overloading [gram.over] */
2307 static cp_expr cp_parser_operator_function_id
2308 (cp_parser *);
2309 static cp_expr cp_parser_operator
2310 (cp_parser *);
2312 /* Templates [gram.temp] */
2314 static void cp_parser_template_declaration
2315 (cp_parser *, bool);
2316 static tree cp_parser_template_parameter_list
2317 (cp_parser *);
2318 static tree cp_parser_template_parameter
2319 (cp_parser *, bool *, bool *);
2320 static tree cp_parser_type_parameter
2321 (cp_parser *, bool *);
2322 static tree cp_parser_template_id
2323 (cp_parser *, bool, bool, enum tag_types, bool);
2324 static tree cp_parser_template_name
2325 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2326 static tree cp_parser_template_argument_list
2327 (cp_parser *);
2328 static tree cp_parser_template_argument
2329 (cp_parser *);
2330 static void cp_parser_explicit_instantiation
2331 (cp_parser *);
2332 static void cp_parser_explicit_specialization
2333 (cp_parser *);
2335 /* Exception handling [gram.exception] */
2337 static tree cp_parser_try_block
2338 (cp_parser *);
2339 static bool cp_parser_function_try_block
2340 (cp_parser *);
2341 static void cp_parser_handler_seq
2342 (cp_parser *);
2343 static void cp_parser_handler
2344 (cp_parser *);
2345 static tree cp_parser_exception_declaration
2346 (cp_parser *);
2347 static tree cp_parser_throw_expression
2348 (cp_parser *);
2349 static tree cp_parser_exception_specification_opt
2350 (cp_parser *);
2351 static tree cp_parser_type_id_list
2352 (cp_parser *);
2354 /* GNU Extensions */
2356 static tree cp_parser_asm_specification_opt
2357 (cp_parser *);
2358 static tree cp_parser_asm_operand_list
2359 (cp_parser *);
2360 static tree cp_parser_asm_clobber_list
2361 (cp_parser *);
2362 static tree cp_parser_asm_label_list
2363 (cp_parser *);
2364 static bool cp_next_tokens_can_be_attribute_p
2365 (cp_parser *);
2366 static bool cp_next_tokens_can_be_gnu_attribute_p
2367 (cp_parser *);
2368 static bool cp_next_tokens_can_be_std_attribute_p
2369 (cp_parser *);
2370 static bool cp_nth_tokens_can_be_std_attribute_p
2371 (cp_parser *, size_t);
2372 static bool cp_nth_tokens_can_be_gnu_attribute_p
2373 (cp_parser *, size_t);
2374 static bool cp_nth_tokens_can_be_attribute_p
2375 (cp_parser *, size_t);
2376 static tree cp_parser_attributes_opt
2377 (cp_parser *);
2378 static tree cp_parser_gnu_attributes_opt
2379 (cp_parser *);
2380 static tree cp_parser_gnu_attribute_list
2381 (cp_parser *);
2382 static tree cp_parser_std_attribute
2383 (cp_parser *);
2384 static tree cp_parser_std_attribute_spec
2385 (cp_parser *);
2386 static tree cp_parser_std_attribute_spec_seq
2387 (cp_parser *);
2388 static bool cp_parser_extension_opt
2389 (cp_parser *, int *);
2390 static void cp_parser_label_declaration
2391 (cp_parser *);
2393 /* Concept Extensions */
2395 static tree cp_parser_requires_clause
2396 (cp_parser *);
2397 static tree cp_parser_requires_clause_opt
2398 (cp_parser *);
2399 static tree cp_parser_requires_expression
2400 (cp_parser *);
2401 static tree cp_parser_requirement_parameter_list
2402 (cp_parser *);
2403 static tree cp_parser_requirement_body
2404 (cp_parser *);
2405 static tree cp_parser_requirement_list
2406 (cp_parser *);
2407 static tree cp_parser_requirement
2408 (cp_parser *);
2409 static tree cp_parser_simple_requirement
2410 (cp_parser *);
2411 static tree cp_parser_compound_requirement
2412 (cp_parser *);
2413 static tree cp_parser_type_requirement
2414 (cp_parser *);
2415 static tree cp_parser_nested_requirement
2416 (cp_parser *);
2418 /* Transactional Memory Extensions */
2420 static tree cp_parser_transaction
2421 (cp_parser *, cp_token *);
2422 static tree cp_parser_transaction_expression
2423 (cp_parser *, enum rid);
2424 static bool cp_parser_function_transaction
2425 (cp_parser *, enum rid);
2426 static tree cp_parser_transaction_cancel
2427 (cp_parser *);
2429 enum pragma_context {
2430 pragma_external,
2431 pragma_member,
2432 pragma_objc_icode,
2433 pragma_stmt,
2434 pragma_compound
2436 static bool cp_parser_pragma
2437 (cp_parser *, enum pragma_context);
2439 /* Objective-C++ Productions */
2441 static tree cp_parser_objc_message_receiver
2442 (cp_parser *);
2443 static tree cp_parser_objc_message_args
2444 (cp_parser *);
2445 static tree cp_parser_objc_message_expression
2446 (cp_parser *);
2447 static cp_expr cp_parser_objc_encode_expression
2448 (cp_parser *);
2449 static tree cp_parser_objc_defs_expression
2450 (cp_parser *);
2451 static tree cp_parser_objc_protocol_expression
2452 (cp_parser *);
2453 static tree cp_parser_objc_selector_expression
2454 (cp_parser *);
2455 static cp_expr cp_parser_objc_expression
2456 (cp_parser *);
2457 static bool cp_parser_objc_selector_p
2458 (enum cpp_ttype);
2459 static tree cp_parser_objc_selector
2460 (cp_parser *);
2461 static tree cp_parser_objc_protocol_refs_opt
2462 (cp_parser *);
2463 static void cp_parser_objc_declaration
2464 (cp_parser *, tree);
2465 static tree cp_parser_objc_statement
2466 (cp_parser *);
2467 static bool cp_parser_objc_valid_prefix_attributes
2468 (cp_parser *, tree *);
2469 static void cp_parser_objc_at_property_declaration
2470 (cp_parser *) ;
2471 static void cp_parser_objc_at_synthesize_declaration
2472 (cp_parser *) ;
2473 static void cp_parser_objc_at_dynamic_declaration
2474 (cp_parser *) ;
2475 static tree cp_parser_objc_struct_declaration
2476 (cp_parser *) ;
2478 /* Utility Routines */
2480 static cp_expr cp_parser_lookup_name
2481 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2482 static tree cp_parser_lookup_name_simple
2483 (cp_parser *, tree, location_t);
2484 static tree cp_parser_maybe_treat_template_as_class
2485 (tree, bool);
2486 static bool cp_parser_check_declarator_template_parameters
2487 (cp_parser *, cp_declarator *, location_t);
2488 static bool cp_parser_check_template_parameters
2489 (cp_parser *, unsigned, location_t, cp_declarator *);
2490 static cp_expr cp_parser_simple_cast_expression
2491 (cp_parser *);
2492 static tree cp_parser_global_scope_opt
2493 (cp_parser *, bool);
2494 static bool cp_parser_constructor_declarator_p
2495 (cp_parser *, bool);
2496 static tree cp_parser_function_definition_from_specifiers_and_declarator
2497 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2498 static tree cp_parser_function_definition_after_declarator
2499 (cp_parser *, bool);
2500 static bool cp_parser_template_declaration_after_export
2501 (cp_parser *, bool);
2502 static void cp_parser_perform_template_parameter_access_checks
2503 (vec<deferred_access_check, va_gc> *);
2504 static tree cp_parser_single_declaration
2505 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2506 static cp_expr cp_parser_functional_cast
2507 (cp_parser *, tree);
2508 static tree cp_parser_save_member_function_body
2509 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2510 static tree cp_parser_save_nsdmi
2511 (cp_parser *);
2512 static tree cp_parser_enclosed_template_argument_list
2513 (cp_parser *);
2514 static void cp_parser_save_default_args
2515 (cp_parser *, tree);
2516 static void cp_parser_late_parsing_for_member
2517 (cp_parser *, tree);
2518 static tree cp_parser_late_parse_one_default_arg
2519 (cp_parser *, tree, tree, tree);
2520 static void cp_parser_late_parsing_nsdmi
2521 (cp_parser *, tree);
2522 static void cp_parser_late_parsing_default_args
2523 (cp_parser *, tree);
2524 static tree cp_parser_sizeof_operand
2525 (cp_parser *, enum rid);
2526 static tree cp_parser_trait_expr
2527 (cp_parser *, enum rid);
2528 static bool cp_parser_declares_only_class_p
2529 (cp_parser *);
2530 static void cp_parser_set_storage_class
2531 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2532 static void cp_parser_set_decl_spec_type
2533 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2534 static void set_and_check_decl_spec_loc
2535 (cp_decl_specifier_seq *decl_specs,
2536 cp_decl_spec ds, cp_token *);
2537 static bool cp_parser_friend_p
2538 (const cp_decl_specifier_seq *);
2539 static void cp_parser_required_error
2540 (cp_parser *, required_token, bool);
2541 static cp_token *cp_parser_require
2542 (cp_parser *, enum cpp_ttype, required_token);
2543 static cp_token *cp_parser_require_keyword
2544 (cp_parser *, enum rid, required_token);
2545 static bool cp_parser_token_starts_function_definition_p
2546 (cp_token *);
2547 static bool cp_parser_next_token_starts_class_definition_p
2548 (cp_parser *);
2549 static bool cp_parser_next_token_ends_template_argument_p
2550 (cp_parser *);
2551 static bool cp_parser_nth_token_starts_template_argument_list_p
2552 (cp_parser *, size_t);
2553 static enum tag_types cp_parser_token_is_class_key
2554 (cp_token *);
2555 static enum tag_types cp_parser_token_is_type_parameter_key
2556 (cp_token *);
2557 static void cp_parser_check_class_key
2558 (enum tag_types, tree type);
2559 static void cp_parser_check_access_in_redeclaration
2560 (tree type, location_t location);
2561 static bool cp_parser_optional_template_keyword
2562 (cp_parser *);
2563 static void cp_parser_pre_parsed_nested_name_specifier
2564 (cp_parser *);
2565 static bool cp_parser_cache_group
2566 (cp_parser *, enum cpp_ttype, unsigned);
2567 static tree cp_parser_cache_defarg
2568 (cp_parser *parser, bool nsdmi);
2569 static void cp_parser_parse_tentatively
2570 (cp_parser *);
2571 static void cp_parser_commit_to_tentative_parse
2572 (cp_parser *);
2573 static void cp_parser_commit_to_topmost_tentative_parse
2574 (cp_parser *);
2575 static void cp_parser_abort_tentative_parse
2576 (cp_parser *);
2577 static bool cp_parser_parse_definitely
2578 (cp_parser *);
2579 static inline bool cp_parser_parsing_tentatively
2580 (cp_parser *);
2581 static bool cp_parser_uncommitted_to_tentative_parse_p
2582 (cp_parser *);
2583 static void cp_parser_error
2584 (cp_parser *, const char *);
2585 static void cp_parser_name_lookup_error
2586 (cp_parser *, tree, tree, name_lookup_error, location_t);
2587 static bool cp_parser_simulate_error
2588 (cp_parser *);
2589 static bool cp_parser_check_type_definition
2590 (cp_parser *);
2591 static void cp_parser_check_for_definition_in_return_type
2592 (cp_declarator *, tree, location_t type_location);
2593 static void cp_parser_check_for_invalid_template_id
2594 (cp_parser *, tree, enum tag_types, location_t location);
2595 static bool cp_parser_non_integral_constant_expression
2596 (cp_parser *, non_integral_constant);
2597 static void cp_parser_diagnose_invalid_type_name
2598 (cp_parser *, tree, location_t);
2599 static bool cp_parser_parse_and_diagnose_invalid_type_name
2600 (cp_parser *);
2601 static int cp_parser_skip_to_closing_parenthesis
2602 (cp_parser *, bool, bool, bool);
2603 static void cp_parser_skip_to_end_of_statement
2604 (cp_parser *);
2605 static void cp_parser_consume_semicolon_at_end_of_statement
2606 (cp_parser *);
2607 static void cp_parser_skip_to_end_of_block_or_statement
2608 (cp_parser *);
2609 static bool cp_parser_skip_to_closing_brace
2610 (cp_parser *);
2611 static void cp_parser_skip_to_end_of_template_parameter_list
2612 (cp_parser *);
2613 static void cp_parser_skip_to_pragma_eol
2614 (cp_parser*, cp_token *);
2615 static bool cp_parser_error_occurred
2616 (cp_parser *);
2617 static bool cp_parser_allow_gnu_extensions_p
2618 (cp_parser *);
2619 static bool cp_parser_is_pure_string_literal
2620 (cp_token *);
2621 static bool cp_parser_is_string_literal
2622 (cp_token *);
2623 static bool cp_parser_is_keyword
2624 (cp_token *, enum rid);
2625 static tree cp_parser_make_typename_type
2626 (cp_parser *, tree, location_t location);
2627 static cp_declarator * cp_parser_make_indirect_declarator
2628 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2629 static bool cp_parser_compound_literal_p
2630 (cp_parser *);
2631 static bool cp_parser_array_designator_p
2632 (cp_parser *);
2633 static bool cp_parser_skip_to_closing_square_bracket
2634 (cp_parser *);
2636 /* Concept-related syntactic transformations */
2638 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2639 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2641 // -------------------------------------------------------------------------- //
2642 // Unevaluated Operand Guard
2644 // Implementation of an RAII helper for unevaluated operand parsing.
2645 cp_unevaluated::cp_unevaluated ()
2647 ++cp_unevaluated_operand;
2648 ++c_inhibit_evaluation_warnings;
2651 cp_unevaluated::~cp_unevaluated ()
2653 --c_inhibit_evaluation_warnings;
2654 --cp_unevaluated_operand;
2657 // -------------------------------------------------------------------------- //
2658 // Tentative Parsing
2660 /* Returns nonzero if we are parsing tentatively. */
2662 static inline bool
2663 cp_parser_parsing_tentatively (cp_parser* parser)
2665 return parser->context->next != NULL;
2668 /* Returns nonzero if TOKEN is a string literal. */
2670 static bool
2671 cp_parser_is_pure_string_literal (cp_token* token)
2673 return (token->type == CPP_STRING ||
2674 token->type == CPP_STRING16 ||
2675 token->type == CPP_STRING32 ||
2676 token->type == CPP_WSTRING ||
2677 token->type == CPP_UTF8STRING);
2680 /* Returns nonzero if TOKEN is a string literal
2681 of a user-defined string literal. */
2683 static bool
2684 cp_parser_is_string_literal (cp_token* token)
2686 return (cp_parser_is_pure_string_literal (token) ||
2687 token->type == CPP_STRING_USERDEF ||
2688 token->type == CPP_STRING16_USERDEF ||
2689 token->type == CPP_STRING32_USERDEF ||
2690 token->type == CPP_WSTRING_USERDEF ||
2691 token->type == CPP_UTF8STRING_USERDEF);
2694 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2696 static bool
2697 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2699 return token->keyword == keyword;
2702 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2703 PRAGMA_NONE. */
2705 static enum pragma_kind
2706 cp_parser_pragma_kind (cp_token *token)
2708 if (token->type != CPP_PRAGMA)
2709 return PRAGMA_NONE;
2710 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2711 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2714 /* Helper function for cp_parser_error.
2715 Having peeked a token of kind TOK1_KIND that might signify
2716 a conflict marker, peek successor tokens to determine
2717 if we actually do have a conflict marker.
2718 Specifically, we consider a run of 7 '<', '=' or '>' characters
2719 at the start of a line as a conflict marker.
2720 These come through the lexer as three pairs and a single,
2721 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2722 If it returns true, *OUT_LOC is written to with the location/range
2723 of the marker. */
2725 static bool
2726 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2727 location_t *out_loc)
2729 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2730 if (token2->type != tok1_kind)
2731 return false;
2732 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2733 if (token3->type != tok1_kind)
2734 return false;
2735 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2736 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2737 return false;
2739 /* It must be at the start of the line. */
2740 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2741 if (LOCATION_COLUMN (start_loc) != 1)
2742 return false;
2744 /* We have a conflict marker. Construct a location of the form:
2745 <<<<<<<
2746 ^~~~~~~
2747 with start == caret, finishing at the end of the marker. */
2748 location_t finish_loc = get_finish (token4->location);
2749 *out_loc = make_location (start_loc, start_loc, finish_loc);
2751 return true;
2754 /* If not parsing tentatively, issue a diagnostic of the form
2755 FILE:LINE: MESSAGE before TOKEN
2756 where TOKEN is the next token in the input stream. MESSAGE
2757 (specified by the caller) is usually of the form "expected
2758 OTHER-TOKEN". */
2760 static void
2761 cp_parser_error (cp_parser* parser, const char* gmsgid)
2763 if (!cp_parser_simulate_error (parser))
2765 cp_token *token = cp_lexer_peek_token (parser->lexer);
2766 /* This diagnostic makes more sense if it is tagged to the line
2767 of the token we just peeked at. */
2768 cp_lexer_set_source_position_from_token (token);
2770 if (token->type == CPP_PRAGMA)
2772 error_at (token->location,
2773 "%<#pragma%> is not allowed here");
2774 cp_parser_skip_to_pragma_eol (parser, token);
2775 return;
2778 /* If this is actually a conflict marker, report it as such. */
2779 if (token->type == CPP_LSHIFT
2780 || token->type == CPP_RSHIFT
2781 || token->type == CPP_EQ_EQ)
2783 location_t loc;
2784 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2786 error_at (loc, "version control conflict marker in file");
2787 return;
2791 c_parse_error (gmsgid,
2792 /* Because c_parser_error does not understand
2793 CPP_KEYWORD, keywords are treated like
2794 identifiers. */
2795 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2796 token->u.value, token->flags);
2800 /* Issue an error about name-lookup failing. NAME is the
2801 IDENTIFIER_NODE DECL is the result of
2802 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2803 the thing that we hoped to find. */
2805 static void
2806 cp_parser_name_lookup_error (cp_parser* parser,
2807 tree name,
2808 tree decl,
2809 name_lookup_error desired,
2810 location_t location)
2812 /* If name lookup completely failed, tell the user that NAME was not
2813 declared. */
2814 if (decl == error_mark_node)
2816 if (parser->scope && parser->scope != global_namespace)
2817 error_at (location, "%<%E::%E%> has not been declared",
2818 parser->scope, name);
2819 else if (parser->scope == global_namespace)
2820 error_at (location, "%<::%E%> has not been declared", name);
2821 else if (parser->object_scope
2822 && !CLASS_TYPE_P (parser->object_scope))
2823 error_at (location, "request for member %qE in non-class type %qT",
2824 name, parser->object_scope);
2825 else if (parser->object_scope)
2826 error_at (location, "%<%T::%E%> has not been declared",
2827 parser->object_scope, name);
2828 else
2829 error_at (location, "%qE has not been declared", name);
2831 else if (parser->scope && parser->scope != global_namespace)
2833 switch (desired)
2835 case NLE_TYPE:
2836 error_at (location, "%<%E::%E%> is not a type",
2837 parser->scope, name);
2838 break;
2839 case NLE_CXX98:
2840 error_at (location, "%<%E::%E%> is not a class or namespace",
2841 parser->scope, name);
2842 break;
2843 case NLE_NOT_CXX98:
2844 error_at (location,
2845 "%<%E::%E%> is not a class, namespace, or enumeration",
2846 parser->scope, name);
2847 break;
2848 default:
2849 gcc_unreachable ();
2853 else if (parser->scope == global_namespace)
2855 switch (desired)
2857 case NLE_TYPE:
2858 error_at (location, "%<::%E%> is not a type", name);
2859 break;
2860 case NLE_CXX98:
2861 error_at (location, "%<::%E%> is not a class or namespace", name);
2862 break;
2863 case NLE_NOT_CXX98:
2864 error_at (location,
2865 "%<::%E%> is not a class, namespace, or enumeration",
2866 name);
2867 break;
2868 default:
2869 gcc_unreachable ();
2872 else
2874 switch (desired)
2876 case NLE_TYPE:
2877 error_at (location, "%qE is not a type", name);
2878 break;
2879 case NLE_CXX98:
2880 error_at (location, "%qE is not a class or namespace", name);
2881 break;
2882 case NLE_NOT_CXX98:
2883 error_at (location,
2884 "%qE is not a class, namespace, or enumeration", name);
2885 break;
2886 default:
2887 gcc_unreachable ();
2892 /* If we are parsing tentatively, remember that an error has occurred
2893 during this tentative parse. Returns true if the error was
2894 simulated; false if a message should be issued by the caller. */
2896 static bool
2897 cp_parser_simulate_error (cp_parser* parser)
2899 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2901 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2902 return true;
2904 return false;
2907 /* This function is called when a type is defined. If type
2908 definitions are forbidden at this point, an error message is
2909 issued. */
2911 static bool
2912 cp_parser_check_type_definition (cp_parser* parser)
2914 /* If types are forbidden here, issue a message. */
2915 if (parser->type_definition_forbidden_message)
2917 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2918 in the message need to be interpreted. */
2919 error (parser->type_definition_forbidden_message);
2920 return false;
2922 return true;
2925 /* This function is called when the DECLARATOR is processed. The TYPE
2926 was a type defined in the decl-specifiers. If it is invalid to
2927 define a type in the decl-specifiers for DECLARATOR, an error is
2928 issued. TYPE_LOCATION is the location of TYPE and is used
2929 for error reporting. */
2931 static void
2932 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2933 tree type, location_t type_location)
2935 /* [dcl.fct] forbids type definitions in return types.
2936 Unfortunately, it's not easy to know whether or not we are
2937 processing a return type until after the fact. */
2938 while (declarator
2939 && (declarator->kind == cdk_pointer
2940 || declarator->kind == cdk_reference
2941 || declarator->kind == cdk_ptrmem))
2942 declarator = declarator->declarator;
2943 if (declarator
2944 && declarator->kind == cdk_function)
2946 error_at (type_location,
2947 "new types may not be defined in a return type");
2948 inform (type_location,
2949 "(perhaps a semicolon is missing after the definition of %qT)",
2950 type);
2954 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2955 "<" in any valid C++ program. If the next token is indeed "<",
2956 issue a message warning the user about what appears to be an
2957 invalid attempt to form a template-id. LOCATION is the location
2958 of the type-specifier (TYPE) */
2960 static void
2961 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2962 tree type,
2963 enum tag_types tag_type,
2964 location_t location)
2966 cp_token_position start = 0;
2968 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2970 if (TYPE_P (type))
2971 error_at (location, "%qT is not a template", type);
2972 else if (identifier_p (type))
2974 if (tag_type != none_type)
2975 error_at (location, "%qE is not a class template", type);
2976 else
2977 error_at (location, "%qE is not a template", type);
2979 else
2980 error_at (location, "invalid template-id");
2981 /* Remember the location of the invalid "<". */
2982 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2983 start = cp_lexer_token_position (parser->lexer, true);
2984 /* Consume the "<". */
2985 cp_lexer_consume_token (parser->lexer);
2986 /* Parse the template arguments. */
2987 cp_parser_enclosed_template_argument_list (parser);
2988 /* Permanently remove the invalid template arguments so that
2989 this error message is not issued again. */
2990 if (start)
2991 cp_lexer_purge_tokens_after (parser->lexer, start);
2995 /* If parsing an integral constant-expression, issue an error message
2996 about the fact that THING appeared and return true. Otherwise,
2997 return false. In either case, set
2998 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3000 static bool
3001 cp_parser_non_integral_constant_expression (cp_parser *parser,
3002 non_integral_constant thing)
3004 parser->non_integral_constant_expression_p = true;
3005 if (parser->integral_constant_expression_p)
3007 if (!parser->allow_non_integral_constant_expression_p)
3009 const char *msg = NULL;
3010 switch (thing)
3012 case NIC_FLOAT:
3013 error ("floating-point literal "
3014 "cannot appear in a constant-expression");
3015 return true;
3016 case NIC_CAST:
3017 error ("a cast to a type other than an integral or "
3018 "enumeration type cannot appear in a "
3019 "constant-expression");
3020 return true;
3021 case NIC_TYPEID:
3022 error ("%<typeid%> operator "
3023 "cannot appear in a constant-expression");
3024 return true;
3025 case NIC_NCC:
3026 error ("non-constant compound literals "
3027 "cannot appear in a constant-expression");
3028 return true;
3029 case NIC_FUNC_CALL:
3030 error ("a function call "
3031 "cannot appear in a constant-expression");
3032 return true;
3033 case NIC_INC:
3034 error ("an increment "
3035 "cannot appear in a constant-expression");
3036 return true;
3037 case NIC_DEC:
3038 error ("an decrement "
3039 "cannot appear in a constant-expression");
3040 return true;
3041 case NIC_ARRAY_REF:
3042 error ("an array reference "
3043 "cannot appear in a constant-expression");
3044 return true;
3045 case NIC_ADDR_LABEL:
3046 error ("the address of a label "
3047 "cannot appear in a constant-expression");
3048 return true;
3049 case NIC_OVERLOADED:
3050 error ("calls to overloaded operators "
3051 "cannot appear in a constant-expression");
3052 return true;
3053 case NIC_ASSIGNMENT:
3054 error ("an assignment cannot appear in a constant-expression");
3055 return true;
3056 case NIC_COMMA:
3057 error ("a comma operator "
3058 "cannot appear in a constant-expression");
3059 return true;
3060 case NIC_CONSTRUCTOR:
3061 error ("a call to a constructor "
3062 "cannot appear in a constant-expression");
3063 return true;
3064 case NIC_TRANSACTION:
3065 error ("a transaction expression "
3066 "cannot appear in a constant-expression");
3067 return true;
3068 case NIC_THIS:
3069 msg = "this";
3070 break;
3071 case NIC_FUNC_NAME:
3072 msg = "__FUNCTION__";
3073 break;
3074 case NIC_PRETTY_FUNC:
3075 msg = "__PRETTY_FUNCTION__";
3076 break;
3077 case NIC_C99_FUNC:
3078 msg = "__func__";
3079 break;
3080 case NIC_VA_ARG:
3081 msg = "va_arg";
3082 break;
3083 case NIC_ARROW:
3084 msg = "->";
3085 break;
3086 case NIC_POINT:
3087 msg = ".";
3088 break;
3089 case NIC_STAR:
3090 msg = "*";
3091 break;
3092 case NIC_ADDR:
3093 msg = "&";
3094 break;
3095 case NIC_PREINCREMENT:
3096 msg = "++";
3097 break;
3098 case NIC_PREDECREMENT:
3099 msg = "--";
3100 break;
3101 case NIC_NEW:
3102 msg = "new";
3103 break;
3104 case NIC_DEL:
3105 msg = "delete";
3106 break;
3107 default:
3108 gcc_unreachable ();
3110 if (msg)
3111 error ("%qs cannot appear in a constant-expression", msg);
3112 return true;
3115 return false;
3118 /* Emit a diagnostic for an invalid type name. This function commits
3119 to the current active tentative parse, if any. (Otherwise, the
3120 problematic construct might be encountered again later, resulting
3121 in duplicate error messages.) LOCATION is the location of ID. */
3123 static void
3124 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3125 location_t location)
3127 tree decl, ambiguous_decls;
3128 cp_parser_commit_to_tentative_parse (parser);
3129 /* Try to lookup the identifier. */
3130 decl = cp_parser_lookup_name (parser, id, none_type,
3131 /*is_template=*/false,
3132 /*is_namespace=*/false,
3133 /*check_dependency=*/true,
3134 &ambiguous_decls, location);
3135 if (ambiguous_decls)
3136 /* If the lookup was ambiguous, an error will already have
3137 been issued. */
3138 return;
3139 /* If the lookup found a template-name, it means that the user forgot
3140 to specify an argument list. Emit a useful error message. */
3141 if (DECL_TYPE_TEMPLATE_P (decl))
3143 error_at (location,
3144 "invalid use of template-name %qE without an argument list",
3145 decl);
3146 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3148 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3149 error_at (location, "invalid use of destructor %qD as a type", id);
3150 else if (TREE_CODE (decl) == TYPE_DECL)
3151 /* Something like 'unsigned A a;' */
3152 error_at (location, "invalid combination of multiple type-specifiers");
3153 else if (!parser->scope)
3155 /* Issue an error message. */
3156 error_at (location, "%qE does not name a type", id);
3157 /* If we're in a template class, it's possible that the user was
3158 referring to a type from a base class. For example:
3160 template <typename T> struct A { typedef T X; };
3161 template <typename T> struct B : public A<T> { X x; };
3163 The user should have said "typename A<T>::X". */
3164 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3165 inform (location, "C++11 %<constexpr%> only available with "
3166 "-std=c++11 or -std=gnu++11");
3167 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3168 inform (location, "C++11 %<noexcept%> only available with "
3169 "-std=c++11 or -std=gnu++11");
3170 else if (cxx_dialect < cxx11
3171 && TREE_CODE (id) == IDENTIFIER_NODE
3172 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
3173 inform (location, "C++11 %<thread_local%> only available with "
3174 "-std=c++11 or -std=gnu++11");
3175 else if (processing_template_decl && current_class_type
3176 && TYPE_BINFO (current_class_type))
3178 tree b;
3180 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3182 b = TREE_CHAIN (b))
3184 tree base_type = BINFO_TYPE (b);
3185 if (CLASS_TYPE_P (base_type)
3186 && dependent_type_p (base_type))
3188 tree field;
3189 /* Go from a particular instantiation of the
3190 template (which will have an empty TYPE_FIELDs),
3191 to the main version. */
3192 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3193 for (field = TYPE_FIELDS (base_type);
3194 field;
3195 field = DECL_CHAIN (field))
3196 if (TREE_CODE (field) == TYPE_DECL
3197 && DECL_NAME (field) == id)
3199 inform (location,
3200 "(perhaps %<typename %T::%E%> was intended)",
3201 BINFO_TYPE (b), id);
3202 break;
3204 if (field)
3205 break;
3210 /* Here we diagnose qualified-ids where the scope is actually correct,
3211 but the identifier does not resolve to a valid type name. */
3212 else if (parser->scope != error_mark_node)
3214 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3216 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3217 error_at (location_of (id),
3218 "%qE in namespace %qE does not name a template type",
3219 id, parser->scope);
3220 else
3221 error_at (location_of (id),
3222 "%qE in namespace %qE does not name a type",
3223 id, parser->scope);
3224 if (DECL_P (decl))
3225 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3227 else if (CLASS_TYPE_P (parser->scope)
3228 && constructor_name_p (id, parser->scope))
3230 /* A<T>::A<T>() */
3231 error_at (location, "%<%T::%E%> names the constructor, not"
3232 " the type", parser->scope, id);
3233 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3234 error_at (location, "and %qT has no template constructors",
3235 parser->scope);
3237 else if (TYPE_P (parser->scope)
3238 && dependent_scope_p (parser->scope))
3239 error_at (location, "need %<typename%> before %<%T::%E%> because "
3240 "%qT is a dependent scope",
3241 parser->scope, id, parser->scope);
3242 else if (TYPE_P (parser->scope))
3244 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3245 error_at (location_of (id),
3246 "%qE in %q#T does not name a template type",
3247 id, parser->scope);
3248 else
3249 error_at (location_of (id),
3250 "%qE in %q#T does not name a type",
3251 id, parser->scope);
3252 if (DECL_P (decl))
3253 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3255 else
3256 gcc_unreachable ();
3260 /* Check for a common situation where a type-name should be present,
3261 but is not, and issue a sensible error message. Returns true if an
3262 invalid type-name was detected.
3264 The situation handled by this function are variable declarations of the
3265 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3266 Usually, `ID' should name a type, but if we got here it means that it
3267 does not. We try to emit the best possible error message depending on
3268 how exactly the id-expression looks like. */
3270 static bool
3271 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3273 tree id;
3274 cp_token *token = cp_lexer_peek_token (parser->lexer);
3276 /* Avoid duplicate error about ambiguous lookup. */
3277 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3279 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3280 if (next->type == CPP_NAME && next->error_reported)
3281 goto out;
3284 cp_parser_parse_tentatively (parser);
3285 id = cp_parser_id_expression (parser,
3286 /*template_keyword_p=*/false,
3287 /*check_dependency_p=*/true,
3288 /*template_p=*/NULL,
3289 /*declarator_p=*/true,
3290 /*optional_p=*/false);
3291 /* If the next token is a (, this is a function with no explicit return
3292 type, i.e. constructor, destructor or conversion op. */
3293 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3294 || TREE_CODE (id) == TYPE_DECL)
3296 cp_parser_abort_tentative_parse (parser);
3297 return false;
3299 if (!cp_parser_parse_definitely (parser))
3300 return false;
3302 /* Emit a diagnostic for the invalid type. */
3303 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3304 out:
3305 /* If we aren't in the middle of a declarator (i.e. in a
3306 parameter-declaration-clause), skip to the end of the declaration;
3307 there's no point in trying to process it. */
3308 if (!parser->in_declarator_p)
3309 cp_parser_skip_to_end_of_block_or_statement (parser);
3310 return true;
3313 /* Consume tokens up to, and including, the next non-nested closing `)'.
3314 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3315 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3316 found an unnested token of that type. */
3318 static int
3319 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3320 bool recovering,
3321 cpp_ttype or_ttype,
3322 bool consume_paren)
3324 unsigned paren_depth = 0;
3325 unsigned brace_depth = 0;
3326 unsigned square_depth = 0;
3328 if (recovering && or_ttype == CPP_EOF
3329 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3330 return 0;
3332 while (true)
3334 cp_token * token = cp_lexer_peek_token (parser->lexer);
3336 /* Have we found what we're looking for before the closing paren? */
3337 if (token->type == or_ttype && or_ttype != CPP_EOF
3338 && !brace_depth && !paren_depth && !square_depth)
3339 return -1;
3341 switch (token->type)
3343 case CPP_EOF:
3344 case CPP_PRAGMA_EOL:
3345 /* If we've run out of tokens, then there is no closing `)'. */
3346 return 0;
3348 /* This is good for lambda expression capture-lists. */
3349 case CPP_OPEN_SQUARE:
3350 ++square_depth;
3351 break;
3352 case CPP_CLOSE_SQUARE:
3353 if (!square_depth--)
3354 return 0;
3355 break;
3357 case CPP_SEMICOLON:
3358 /* This matches the processing in skip_to_end_of_statement. */
3359 if (!brace_depth)
3360 return 0;
3361 break;
3363 case CPP_OPEN_BRACE:
3364 ++brace_depth;
3365 break;
3366 case CPP_CLOSE_BRACE:
3367 if (!brace_depth--)
3368 return 0;
3369 break;
3371 case CPP_OPEN_PAREN:
3372 if (!brace_depth)
3373 ++paren_depth;
3374 break;
3376 case CPP_CLOSE_PAREN:
3377 if (!brace_depth && !paren_depth--)
3379 if (consume_paren)
3380 cp_lexer_consume_token (parser->lexer);
3381 return 1;
3383 break;
3385 default:
3386 break;
3389 /* Consume the token. */
3390 cp_lexer_consume_token (parser->lexer);
3394 /* Consume tokens up to, and including, the next non-nested closing `)'.
3395 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3396 are doing error recovery. Returns -1 if OR_COMMA is true and we
3397 found an unnested token of that type. */
3399 static int
3400 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3401 bool recovering,
3402 bool or_comma,
3403 bool consume_paren)
3405 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3406 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3407 ttype, consume_paren);
3410 /* Consume tokens until we reach the end of the current statement.
3411 Normally, that will be just before consuming a `;'. However, if a
3412 non-nested `}' comes first, then we stop before consuming that. */
3414 static void
3415 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3417 unsigned nesting_depth = 0;
3419 /* Unwind generic function template scope if necessary. */
3420 if (parser->fully_implicit_function_template_p)
3421 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3423 while (true)
3425 cp_token *token = cp_lexer_peek_token (parser->lexer);
3427 switch (token->type)
3429 case CPP_EOF:
3430 case CPP_PRAGMA_EOL:
3431 /* If we've run out of tokens, stop. */
3432 return;
3434 case CPP_SEMICOLON:
3435 /* If the next token is a `;', we have reached the end of the
3436 statement. */
3437 if (!nesting_depth)
3438 return;
3439 break;
3441 case CPP_CLOSE_BRACE:
3442 /* If this is a non-nested '}', stop before consuming it.
3443 That way, when confronted with something like:
3445 { 3 + }
3447 we stop before consuming the closing '}', even though we
3448 have not yet reached a `;'. */
3449 if (nesting_depth == 0)
3450 return;
3452 /* If it is the closing '}' for a block that we have
3453 scanned, stop -- but only after consuming the token.
3454 That way given:
3456 void f g () { ... }
3457 typedef int I;
3459 we will stop after the body of the erroneously declared
3460 function, but before consuming the following `typedef'
3461 declaration. */
3462 if (--nesting_depth == 0)
3464 cp_lexer_consume_token (parser->lexer);
3465 return;
3468 case CPP_OPEN_BRACE:
3469 ++nesting_depth;
3470 break;
3472 default:
3473 break;
3476 /* Consume the token. */
3477 cp_lexer_consume_token (parser->lexer);
3481 /* This function is called at the end of a statement or declaration.
3482 If the next token is a semicolon, it is consumed; otherwise, error
3483 recovery is attempted. */
3485 static void
3486 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3488 /* Look for the trailing `;'. */
3489 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3491 /* If there is additional (erroneous) input, skip to the end of
3492 the statement. */
3493 cp_parser_skip_to_end_of_statement (parser);
3494 /* If the next token is now a `;', consume it. */
3495 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3496 cp_lexer_consume_token (parser->lexer);
3500 /* Skip tokens until we have consumed an entire block, or until we
3501 have consumed a non-nested `;'. */
3503 static void
3504 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3506 int nesting_depth = 0;
3508 /* Unwind generic function template scope if necessary. */
3509 if (parser->fully_implicit_function_template_p)
3510 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3512 while (nesting_depth >= 0)
3514 cp_token *token = cp_lexer_peek_token (parser->lexer);
3516 switch (token->type)
3518 case CPP_EOF:
3519 case CPP_PRAGMA_EOL:
3520 /* If we've run out of tokens, stop. */
3521 return;
3523 case CPP_SEMICOLON:
3524 /* Stop if this is an unnested ';'. */
3525 if (!nesting_depth)
3526 nesting_depth = -1;
3527 break;
3529 case CPP_CLOSE_BRACE:
3530 /* Stop if this is an unnested '}', or closes the outermost
3531 nesting level. */
3532 nesting_depth--;
3533 if (nesting_depth < 0)
3534 return;
3535 if (!nesting_depth)
3536 nesting_depth = -1;
3537 break;
3539 case CPP_OPEN_BRACE:
3540 /* Nest. */
3541 nesting_depth++;
3542 break;
3544 default:
3545 break;
3548 /* Consume the token. */
3549 cp_lexer_consume_token (parser->lexer);
3553 /* Skip tokens until a non-nested closing curly brace is the next
3554 token, or there are no more tokens. Return true in the first case,
3555 false otherwise. */
3557 static bool
3558 cp_parser_skip_to_closing_brace (cp_parser *parser)
3560 unsigned nesting_depth = 0;
3562 while (true)
3564 cp_token *token = cp_lexer_peek_token (parser->lexer);
3566 switch (token->type)
3568 case CPP_EOF:
3569 case CPP_PRAGMA_EOL:
3570 /* If we've run out of tokens, stop. */
3571 return false;
3573 case CPP_CLOSE_BRACE:
3574 /* If the next token is a non-nested `}', then we have reached
3575 the end of the current block. */
3576 if (nesting_depth-- == 0)
3577 return true;
3578 break;
3580 case CPP_OPEN_BRACE:
3581 /* If it the next token is a `{', then we are entering a new
3582 block. Consume the entire block. */
3583 ++nesting_depth;
3584 break;
3586 default:
3587 break;
3590 /* Consume the token. */
3591 cp_lexer_consume_token (parser->lexer);
3595 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3596 parameter is the PRAGMA token, allowing us to purge the entire pragma
3597 sequence. */
3599 static void
3600 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3602 cp_token *token;
3604 parser->lexer->in_pragma = false;
3607 token = cp_lexer_consume_token (parser->lexer);
3608 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3610 /* Ensure that the pragma is not parsed again. */
3611 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3614 /* Require pragma end of line, resyncing with it as necessary. The
3615 arguments are as for cp_parser_skip_to_pragma_eol. */
3617 static void
3618 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3620 parser->lexer->in_pragma = false;
3621 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3622 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3625 /* This is a simple wrapper around make_typename_type. When the id is
3626 an unresolved identifier node, we can provide a superior diagnostic
3627 using cp_parser_diagnose_invalid_type_name. */
3629 static tree
3630 cp_parser_make_typename_type (cp_parser *parser, tree id,
3631 location_t id_location)
3633 tree result;
3634 if (identifier_p (id))
3636 result = make_typename_type (parser->scope, id, typename_type,
3637 /*complain=*/tf_none);
3638 if (result == error_mark_node)
3639 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3640 return result;
3642 return make_typename_type (parser->scope, id, typename_type, tf_error);
3645 /* This is a wrapper around the
3646 make_{pointer,ptrmem,reference}_declarator functions that decides
3647 which one to call based on the CODE and CLASS_TYPE arguments. The
3648 CODE argument should be one of the values returned by
3649 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3650 appertain to the pointer or reference. */
3652 static cp_declarator *
3653 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3654 cp_cv_quals cv_qualifiers,
3655 cp_declarator *target,
3656 tree attributes)
3658 if (code == ERROR_MARK)
3659 return cp_error_declarator;
3661 if (code == INDIRECT_REF)
3662 if (class_type == NULL_TREE)
3663 return make_pointer_declarator (cv_qualifiers, target, attributes);
3664 else
3665 return make_ptrmem_declarator (cv_qualifiers, class_type,
3666 target, attributes);
3667 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3668 return make_reference_declarator (cv_qualifiers, target,
3669 false, attributes);
3670 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3671 return make_reference_declarator (cv_qualifiers, target,
3672 true, attributes);
3673 gcc_unreachable ();
3676 /* Create a new C++ parser. */
3678 static cp_parser *
3679 cp_parser_new (void)
3681 cp_parser *parser;
3682 cp_lexer *lexer;
3683 unsigned i;
3685 /* cp_lexer_new_main is called before doing GC allocation because
3686 cp_lexer_new_main might load a PCH file. */
3687 lexer = cp_lexer_new_main ();
3689 /* Initialize the binops_by_token so that we can get the tree
3690 directly from the token. */
3691 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3692 binops_by_token[binops[i].token_type] = binops[i];
3694 parser = ggc_cleared_alloc<cp_parser> ();
3695 parser->lexer = lexer;
3696 parser->context = cp_parser_context_new (NULL);
3698 /* For now, we always accept GNU extensions. */
3699 parser->allow_gnu_extensions_p = 1;
3701 /* The `>' token is a greater-than operator, not the end of a
3702 template-id. */
3703 parser->greater_than_is_operator_p = true;
3705 parser->default_arg_ok_p = true;
3707 /* We are not parsing a constant-expression. */
3708 parser->integral_constant_expression_p = false;
3709 parser->allow_non_integral_constant_expression_p = false;
3710 parser->non_integral_constant_expression_p = false;
3712 /* Local variable names are not forbidden. */
3713 parser->local_variables_forbidden_p = false;
3715 /* We are not processing an `extern "C"' declaration. */
3716 parser->in_unbraced_linkage_specification_p = false;
3718 /* We are not processing a declarator. */
3719 parser->in_declarator_p = false;
3721 /* We are not processing a template-argument-list. */
3722 parser->in_template_argument_list_p = false;
3724 /* We are not in an iteration statement. */
3725 parser->in_statement = 0;
3727 /* We are not in a switch statement. */
3728 parser->in_switch_statement_p = false;
3730 /* We are not parsing a type-id inside an expression. */
3731 parser->in_type_id_in_expr_p = false;
3733 /* Declarations aren't implicitly extern "C". */
3734 parser->implicit_extern_c = false;
3736 /* String literals should be translated to the execution character set. */
3737 parser->translate_strings_p = true;
3739 /* We are not parsing a function body. */
3740 parser->in_function_body = false;
3742 /* We can correct until told otherwise. */
3743 parser->colon_corrects_to_scope_p = true;
3745 /* The unparsed function queue is empty. */
3746 push_unparsed_function_queues (parser);
3748 /* There are no classes being defined. */
3749 parser->num_classes_being_defined = 0;
3751 /* No template parameters apply. */
3752 parser->num_template_parameter_lists = 0;
3754 /* Not declaring an implicit function template. */
3755 parser->auto_is_implicit_function_template_parm_p = false;
3756 parser->fully_implicit_function_template_p = false;
3757 parser->implicit_template_parms = 0;
3758 parser->implicit_template_scope = 0;
3760 /* Active OpenACC routine clauses. */
3761 parser->oacc_routine = NULL;
3763 /* Allow constrained-type-specifiers. */
3764 parser->prevent_constrained_type_specifiers = 0;
3766 return parser;
3769 /* Create a cp_lexer structure which will emit the tokens in CACHE
3770 and push it onto the parser's lexer stack. This is used for delayed
3771 parsing of in-class method bodies and default arguments, and should
3772 not be confused with tentative parsing. */
3773 static void
3774 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3776 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3777 lexer->next = parser->lexer;
3778 parser->lexer = lexer;
3780 /* Move the current source position to that of the first token in the
3781 new lexer. */
3782 cp_lexer_set_source_position_from_token (lexer->next_token);
3785 /* Pop the top lexer off the parser stack. This is never used for the
3786 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3787 static void
3788 cp_parser_pop_lexer (cp_parser *parser)
3790 cp_lexer *lexer = parser->lexer;
3791 parser->lexer = lexer->next;
3792 cp_lexer_destroy (lexer);
3794 /* Put the current source position back where it was before this
3795 lexer was pushed. */
3796 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3799 /* Lexical conventions [gram.lex] */
3801 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3802 identifier. */
3804 static cp_expr
3805 cp_parser_identifier (cp_parser* parser)
3807 cp_token *token;
3809 /* Look for the identifier. */
3810 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3811 /* Return the value. */
3812 if (token)
3813 return cp_expr (token->u.value, token->location);
3814 else
3815 return error_mark_node;
3818 /* Parse a sequence of adjacent string constants. Returns a
3819 TREE_STRING representing the combined, nul-terminated string
3820 constant. If TRANSLATE is true, translate the string to the
3821 execution character set. If WIDE_OK is true, a wide string is
3822 invalid here.
3824 C++98 [lex.string] says that if a narrow string literal token is
3825 adjacent to a wide string literal token, the behavior is undefined.
3826 However, C99 6.4.5p4 says that this results in a wide string literal.
3827 We follow C99 here, for consistency with the C front end.
3829 This code is largely lifted from lex_string() in c-lex.c.
3831 FUTURE: ObjC++ will need to handle @-strings here. */
3832 static cp_expr
3833 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3834 bool lookup_udlit = true)
3836 tree value;
3837 size_t count;
3838 struct obstack str_ob;
3839 cpp_string str, istr, *strs;
3840 cp_token *tok;
3841 enum cpp_ttype type, curr_type;
3842 int have_suffix_p = 0;
3843 tree string_tree;
3844 tree suffix_id = NULL_TREE;
3845 bool curr_tok_is_userdef_p = false;
3847 tok = cp_lexer_peek_token (parser->lexer);
3848 if (!cp_parser_is_string_literal (tok))
3850 cp_parser_error (parser, "expected string-literal");
3851 return error_mark_node;
3854 location_t loc = tok->location;
3856 if (cpp_userdef_string_p (tok->type))
3858 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3859 curr_type = cpp_userdef_string_remove_type (tok->type);
3860 curr_tok_is_userdef_p = true;
3862 else
3864 string_tree = tok->u.value;
3865 curr_type = tok->type;
3867 type = curr_type;
3869 /* Try to avoid the overhead of creating and destroying an obstack
3870 for the common case of just one string. */
3871 if (!cp_parser_is_string_literal
3872 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3874 cp_lexer_consume_token (parser->lexer);
3876 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3877 str.len = TREE_STRING_LENGTH (string_tree);
3878 count = 1;
3880 if (curr_tok_is_userdef_p)
3882 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3883 have_suffix_p = 1;
3884 curr_type = cpp_userdef_string_remove_type (tok->type);
3886 else
3887 curr_type = tok->type;
3889 strs = &str;
3891 else
3893 location_t last_tok_loc;
3894 gcc_obstack_init (&str_ob);
3895 count = 0;
3899 last_tok_loc = tok->location;
3900 cp_lexer_consume_token (parser->lexer);
3901 count++;
3902 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3903 str.len = TREE_STRING_LENGTH (string_tree);
3905 if (curr_tok_is_userdef_p)
3907 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3908 if (have_suffix_p == 0)
3910 suffix_id = curr_suffix_id;
3911 have_suffix_p = 1;
3913 else if (have_suffix_p == 1
3914 && curr_suffix_id != suffix_id)
3916 error ("inconsistent user-defined literal suffixes"
3917 " %qD and %qD in string literal",
3918 suffix_id, curr_suffix_id);
3919 have_suffix_p = -1;
3921 curr_type = cpp_userdef_string_remove_type (tok->type);
3923 else
3924 curr_type = tok->type;
3926 if (type != curr_type)
3928 if (type == CPP_STRING)
3929 type = curr_type;
3930 else if (curr_type != CPP_STRING)
3931 error_at (tok->location,
3932 "unsupported non-standard concatenation "
3933 "of string literals");
3936 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3938 tok = cp_lexer_peek_token (parser->lexer);
3939 if (cpp_userdef_string_p (tok->type))
3941 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3942 curr_type = cpp_userdef_string_remove_type (tok->type);
3943 curr_tok_is_userdef_p = true;
3945 else
3947 string_tree = tok->u.value;
3948 curr_type = tok->type;
3949 curr_tok_is_userdef_p = false;
3952 while (cp_parser_is_string_literal (tok));
3954 /* A string literal built by concatenation has its caret=start at
3955 the start of the initial string, and its finish at the finish of
3956 the final string literal. */
3957 loc = make_location (loc, loc, get_finish (last_tok_loc));
3959 strs = (cpp_string *) obstack_finish (&str_ob);
3962 if (type != CPP_STRING && !wide_ok)
3964 cp_parser_error (parser, "a wide string is invalid in this context");
3965 type = CPP_STRING;
3968 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3969 (parse_in, strs, count, &istr, type))
3971 value = build_string (istr.len, (const char *)istr.text);
3972 free (CONST_CAST (unsigned char *, istr.text));
3974 switch (type)
3976 default:
3977 case CPP_STRING:
3978 case CPP_UTF8STRING:
3979 TREE_TYPE (value) = char_array_type_node;
3980 break;
3981 case CPP_STRING16:
3982 TREE_TYPE (value) = char16_array_type_node;
3983 break;
3984 case CPP_STRING32:
3985 TREE_TYPE (value) = char32_array_type_node;
3986 break;
3987 case CPP_WSTRING:
3988 TREE_TYPE (value) = wchar_array_type_node;
3989 break;
3992 value = fix_string_type (value);
3994 if (have_suffix_p)
3996 tree literal = build_userdef_literal (suffix_id, value,
3997 OT_NONE, NULL_TREE);
3998 if (lookup_udlit)
3999 value = cp_parser_userdef_string_literal (literal);
4000 else
4001 value = literal;
4004 else
4005 /* cpp_interpret_string has issued an error. */
4006 value = error_mark_node;
4008 if (count > 1)
4009 obstack_free (&str_ob, 0);
4011 return cp_expr (value, loc);
4014 /* Look up a literal operator with the name and the exact arguments. */
4016 static tree
4017 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4019 tree decl, fns;
4020 decl = lookup_name (name);
4021 if (!decl || !is_overloaded_fn (decl))
4022 return error_mark_node;
4024 for (fns = decl; fns; fns = OVL_NEXT (fns))
4026 unsigned int ix;
4027 bool found = true;
4028 tree fn = OVL_CURRENT (fns);
4029 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4030 if (parmtypes != NULL_TREE)
4032 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4033 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4035 tree tparm = TREE_VALUE (parmtypes);
4036 tree targ = TREE_TYPE ((*args)[ix]);
4037 bool ptr = TYPE_PTR_P (tparm);
4038 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4039 if ((ptr || arr || !same_type_p (tparm, targ))
4040 && (!ptr || !arr
4041 || !same_type_p (TREE_TYPE (tparm),
4042 TREE_TYPE (targ))))
4043 found = false;
4045 if (found
4046 && ix == vec_safe_length (args)
4047 /* May be this should be sufficient_parms_p instead,
4048 depending on how exactly should user-defined literals
4049 work in presence of default arguments on the literal
4050 operator parameters. */
4051 && parmtypes == void_list_node)
4052 return decl;
4056 return error_mark_node;
4059 /* Parse a user-defined char constant. Returns a call to a user-defined
4060 literal operator taking the character as an argument. */
4062 static cp_expr
4063 cp_parser_userdef_char_literal (cp_parser *parser)
4065 cp_token *token = cp_lexer_consume_token (parser->lexer);
4066 tree literal = token->u.value;
4067 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4068 tree value = USERDEF_LITERAL_VALUE (literal);
4069 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4070 tree decl, result;
4072 /* Build up a call to the user-defined operator */
4073 /* Lookup the name we got back from the id-expression. */
4074 vec<tree, va_gc> *args = make_tree_vector ();
4075 vec_safe_push (args, value);
4076 decl = lookup_literal_operator (name, args);
4077 if (!decl || decl == error_mark_node)
4079 error ("unable to find character literal operator %qD with %qT argument",
4080 name, TREE_TYPE (value));
4081 release_tree_vector (args);
4082 return error_mark_node;
4084 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4085 release_tree_vector (args);
4086 return result;
4089 /* A subroutine of cp_parser_userdef_numeric_literal to
4090 create a char... template parameter pack from a string node. */
4092 static tree
4093 make_char_string_pack (tree value)
4095 tree charvec;
4096 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4097 const char *str = TREE_STRING_POINTER (value);
4098 int i, len = TREE_STRING_LENGTH (value) - 1;
4099 tree argvec = make_tree_vec (1);
4101 /* Fill in CHARVEC with all of the parameters. */
4102 charvec = make_tree_vec (len);
4103 for (i = 0; i < len; ++i)
4104 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4106 /* Build the argument packs. */
4107 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4108 TREE_TYPE (argpack) = char_type_node;
4110 TREE_VEC_ELT (argvec, 0) = argpack;
4112 return argvec;
4115 /* A subroutine of cp_parser_userdef_numeric_literal to
4116 create a char... template parameter pack from a string node. */
4118 static tree
4119 make_string_pack (tree value)
4121 tree charvec;
4122 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4123 const unsigned char *str
4124 = (const unsigned char *) TREE_STRING_POINTER (value);
4125 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4126 int len = TREE_STRING_LENGTH (value) / sz - 1;
4127 tree argvec = make_tree_vec (2);
4129 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4130 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4132 /* First template parm is character type. */
4133 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4135 /* Fill in CHARVEC with all of the parameters. */
4136 charvec = make_tree_vec (len);
4137 for (int i = 0; i < len; ++i)
4138 TREE_VEC_ELT (charvec, i)
4139 = double_int_to_tree (str_char_type_node,
4140 double_int::from_buffer (str + i * sz, sz));
4142 /* Build the argument packs. */
4143 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4144 TREE_TYPE (argpack) = str_char_type_node;
4146 TREE_VEC_ELT (argvec, 1) = argpack;
4148 return argvec;
4151 /* Parse a user-defined numeric constant. returns a call to a user-defined
4152 literal operator. */
4154 static cp_expr
4155 cp_parser_userdef_numeric_literal (cp_parser *parser)
4157 cp_token *token = cp_lexer_consume_token (parser->lexer);
4158 tree literal = token->u.value;
4159 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4160 tree value = USERDEF_LITERAL_VALUE (literal);
4161 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4162 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4163 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4164 tree decl, result;
4165 vec<tree, va_gc> *args;
4167 /* Look for a literal operator taking the exact type of numeric argument
4168 as the literal value. */
4169 args = make_tree_vector ();
4170 vec_safe_push (args, value);
4171 decl = lookup_literal_operator (name, args);
4172 if (decl && decl != error_mark_node)
4174 result = finish_call_expr (decl, &args, false, true,
4175 tf_warning_or_error);
4177 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4179 warning_at (token->location, OPT_Woverflow,
4180 "integer literal exceeds range of %qT type",
4181 long_long_unsigned_type_node);
4183 else
4185 if (overflow > 0)
4186 warning_at (token->location, OPT_Woverflow,
4187 "floating literal exceeds range of %qT type",
4188 long_double_type_node);
4189 else if (overflow < 0)
4190 warning_at (token->location, OPT_Woverflow,
4191 "floating literal truncated to zero");
4194 release_tree_vector (args);
4195 return result;
4197 release_tree_vector (args);
4199 /* If the numeric argument didn't work, look for a raw literal
4200 operator taking a const char* argument consisting of the number
4201 in string format. */
4202 args = make_tree_vector ();
4203 vec_safe_push (args, num_string);
4204 decl = lookup_literal_operator (name, args);
4205 if (decl && decl != error_mark_node)
4207 result = finish_call_expr (decl, &args, false, true,
4208 tf_warning_or_error);
4209 release_tree_vector (args);
4210 return result;
4212 release_tree_vector (args);
4214 /* If the raw literal didn't work, look for a non-type template
4215 function with parameter pack char.... Call the function with
4216 template parameter characters representing the number. */
4217 args = make_tree_vector ();
4218 decl = lookup_literal_operator (name, args);
4219 if (decl && decl != error_mark_node)
4221 tree tmpl_args = make_char_string_pack (num_string);
4222 decl = lookup_template_function (decl, tmpl_args);
4223 result = finish_call_expr (decl, &args, false, true,
4224 tf_warning_or_error);
4225 release_tree_vector (args);
4226 return result;
4229 release_tree_vector (args);
4231 error ("unable to find numeric literal operator %qD", name);
4232 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4233 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4234 "to enable more built-in suffixes");
4235 return error_mark_node;
4238 /* Parse a user-defined string constant. Returns a call to a user-defined
4239 literal operator taking a character pointer and the length of the string
4240 as arguments. */
4242 static tree
4243 cp_parser_userdef_string_literal (tree literal)
4245 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4246 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4247 tree value = USERDEF_LITERAL_VALUE (literal);
4248 int len = TREE_STRING_LENGTH (value)
4249 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4250 tree decl, result;
4251 vec<tree, va_gc> *args;
4253 /* Build up a call to the user-defined operator. */
4254 /* Lookup the name we got back from the id-expression. */
4255 args = make_tree_vector ();
4256 vec_safe_push (args, value);
4257 vec_safe_push (args, build_int_cst (size_type_node, len));
4258 decl = lookup_literal_operator (name, args);
4260 if (decl && decl != error_mark_node)
4262 result = finish_call_expr (decl, &args, false, true,
4263 tf_warning_or_error);
4264 release_tree_vector (args);
4265 return result;
4267 release_tree_vector (args);
4269 /* Look for a template function with typename parameter CharT
4270 and parameter pack CharT... Call the function with
4271 template parameter characters representing the string. */
4272 args = make_tree_vector ();
4273 decl = lookup_literal_operator (name, args);
4274 if (decl && decl != error_mark_node)
4276 tree tmpl_args = make_string_pack (value);
4277 decl = lookup_template_function (decl, tmpl_args);
4278 result = finish_call_expr (decl, &args, false, true,
4279 tf_warning_or_error);
4280 release_tree_vector (args);
4281 return result;
4283 release_tree_vector (args);
4285 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4286 name, TREE_TYPE (value), size_type_node);
4287 return error_mark_node;
4291 /* Basic concepts [gram.basic] */
4293 /* Parse a translation-unit.
4295 translation-unit:
4296 declaration-seq [opt]
4298 Returns TRUE if all went well. */
4300 static bool
4301 cp_parser_translation_unit (cp_parser* parser)
4303 /* The address of the first non-permanent object on the declarator
4304 obstack. */
4305 static void *declarator_obstack_base;
4307 bool success;
4309 /* Create the declarator obstack, if necessary. */
4310 if (!cp_error_declarator)
4312 gcc_obstack_init (&declarator_obstack);
4313 /* Create the error declarator. */
4314 cp_error_declarator = make_declarator (cdk_error);
4315 /* Create the empty parameter list. */
4316 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4317 /* Remember where the base of the declarator obstack lies. */
4318 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4321 cp_parser_declaration_seq_opt (parser);
4323 /* If there are no tokens left then all went well. */
4324 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4326 /* Get rid of the token array; we don't need it any more. */
4327 cp_lexer_destroy (parser->lexer);
4328 parser->lexer = NULL;
4330 /* This file might have been a context that's implicitly extern
4331 "C". If so, pop the lang context. (Only relevant for PCH.) */
4332 if (parser->implicit_extern_c)
4334 pop_lang_context ();
4335 parser->implicit_extern_c = false;
4338 /* Finish up. */
4339 finish_translation_unit ();
4341 success = true;
4343 else
4345 cp_parser_error (parser, "expected declaration");
4346 success = false;
4349 /* Make sure the declarator obstack was fully cleaned up. */
4350 gcc_assert (obstack_next_free (&declarator_obstack)
4351 == declarator_obstack_base);
4353 /* All went well. */
4354 return success;
4357 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4358 decltype context. */
4360 static inline tsubst_flags_t
4361 complain_flags (bool decltype_p)
4363 tsubst_flags_t complain = tf_warning_or_error;
4364 if (decltype_p)
4365 complain |= tf_decltype;
4366 return complain;
4369 /* We're about to parse a collection of statements. If we're currently
4370 parsing tentatively, set up a firewall so that any nested
4371 cp_parser_commit_to_tentative_parse won't affect the current context. */
4373 static cp_token_position
4374 cp_parser_start_tentative_firewall (cp_parser *parser)
4376 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4377 return 0;
4379 cp_parser_parse_tentatively (parser);
4380 cp_parser_commit_to_topmost_tentative_parse (parser);
4381 return cp_lexer_token_position (parser->lexer, false);
4384 /* We've finished parsing the collection of statements. Wrap up the
4385 firewall and replace the relevant tokens with the parsed form. */
4387 static void
4388 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4389 tree expr)
4391 if (!start)
4392 return;
4394 /* Finish the firewall level. */
4395 cp_parser_parse_definitely (parser);
4396 /* And remember the result of the parse for when we try again. */
4397 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4398 token->type = CPP_PREPARSED_EXPR;
4399 token->u.value = expr;
4400 token->keyword = RID_MAX;
4401 cp_lexer_purge_tokens_after (parser->lexer, start);
4404 /* Like the above functions, but let the user modify the tokens. Used by
4405 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4406 later parses, so it makes sense to localize the effects of
4407 cp_parser_commit_to_tentative_parse. */
4409 struct tentative_firewall
4411 cp_parser *parser;
4412 bool set;
4414 tentative_firewall (cp_parser *p): parser(p)
4416 /* If we're currently parsing tentatively, start a committed level as a
4417 firewall and then an inner tentative parse. */
4418 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4420 cp_parser_parse_tentatively (parser);
4421 cp_parser_commit_to_topmost_tentative_parse (parser);
4422 cp_parser_parse_tentatively (parser);
4426 ~tentative_firewall()
4428 if (set)
4430 /* Finish the inner tentative parse and the firewall, propagating any
4431 uncommitted error state to the outer tentative parse. */
4432 bool err = cp_parser_error_occurred (parser);
4433 cp_parser_parse_definitely (parser);
4434 cp_parser_parse_definitely (parser);
4435 if (err)
4436 cp_parser_simulate_error (parser);
4441 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4442 enclosing parentheses. */
4444 static cp_expr
4445 cp_parser_statement_expr (cp_parser *parser)
4447 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4449 /* Consume the '('. */
4450 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4451 cp_lexer_consume_token (parser->lexer);
4452 /* Start the statement-expression. */
4453 tree expr = begin_stmt_expr ();
4454 /* Parse the compound-statement. */
4455 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4456 /* Finish up. */
4457 expr = finish_stmt_expr (expr, false);
4458 /* Consume the ')'. */
4459 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4460 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4461 cp_parser_skip_to_end_of_statement (parser);
4463 cp_parser_end_tentative_firewall (parser, start, expr);
4464 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4465 return cp_expr (expr, combined_loc);
4468 /* Expressions [gram.expr] */
4470 /* Parse a fold-operator.
4472 fold-operator:
4473 - * / % ^ & | = < > << >>
4474 = -= *= /= %= ^= &= |= <<= >>=
4475 == != <= >= && || , .* ->*
4477 This returns the tree code corresponding to the matched operator
4478 as an int. When the current token matches a compound assignment
4479 opertor, the resulting tree code is the negative value of the
4480 non-assignment operator. */
4482 static int
4483 cp_parser_fold_operator (cp_token *token)
4485 switch (token->type)
4487 case CPP_PLUS: return PLUS_EXPR;
4488 case CPP_MINUS: return MINUS_EXPR;
4489 case CPP_MULT: return MULT_EXPR;
4490 case CPP_DIV: return TRUNC_DIV_EXPR;
4491 case CPP_MOD: return TRUNC_MOD_EXPR;
4492 case CPP_XOR: return BIT_XOR_EXPR;
4493 case CPP_AND: return BIT_AND_EXPR;
4494 case CPP_OR: return BIT_IOR_EXPR;
4495 case CPP_LSHIFT: return LSHIFT_EXPR;
4496 case CPP_RSHIFT: return RSHIFT_EXPR;
4498 case CPP_EQ: return -NOP_EXPR;
4499 case CPP_PLUS_EQ: return -PLUS_EXPR;
4500 case CPP_MINUS_EQ: return -MINUS_EXPR;
4501 case CPP_MULT_EQ: return -MULT_EXPR;
4502 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4503 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4504 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4505 case CPP_AND_EQ: return -BIT_AND_EXPR;
4506 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4507 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4508 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4510 case CPP_EQ_EQ: return EQ_EXPR;
4511 case CPP_NOT_EQ: return NE_EXPR;
4512 case CPP_LESS: return LT_EXPR;
4513 case CPP_GREATER: return GT_EXPR;
4514 case CPP_LESS_EQ: return LE_EXPR;
4515 case CPP_GREATER_EQ: return GE_EXPR;
4517 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4518 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4520 case CPP_COMMA: return COMPOUND_EXPR;
4522 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4523 case CPP_DEREF_STAR: return MEMBER_REF;
4525 default: return ERROR_MARK;
4529 /* Returns true if CODE indicates a binary expression, which is not allowed in
4530 the LHS of a fold-expression. More codes will need to be added to use this
4531 function in other contexts. */
4533 static bool
4534 is_binary_op (tree_code code)
4536 switch (code)
4538 case PLUS_EXPR:
4539 case POINTER_PLUS_EXPR:
4540 case MINUS_EXPR:
4541 case MULT_EXPR:
4542 case TRUNC_DIV_EXPR:
4543 case TRUNC_MOD_EXPR:
4544 case BIT_XOR_EXPR:
4545 case BIT_AND_EXPR:
4546 case BIT_IOR_EXPR:
4547 case LSHIFT_EXPR:
4548 case RSHIFT_EXPR:
4550 case MODOP_EXPR:
4552 case EQ_EXPR:
4553 case NE_EXPR:
4554 case LE_EXPR:
4555 case GE_EXPR:
4556 case LT_EXPR:
4557 case GT_EXPR:
4559 case TRUTH_ANDIF_EXPR:
4560 case TRUTH_ORIF_EXPR:
4562 case COMPOUND_EXPR:
4564 case DOTSTAR_EXPR:
4565 case MEMBER_REF:
4566 return true;
4568 default:
4569 return false;
4573 /* If the next token is a suitable fold operator, consume it and return as
4574 the function above. */
4576 static int
4577 cp_parser_fold_operator (cp_parser *parser)
4579 cp_token* token = cp_lexer_peek_token (parser->lexer);
4580 int code = cp_parser_fold_operator (token);
4581 if (code != ERROR_MARK)
4582 cp_lexer_consume_token (parser->lexer);
4583 return code;
4586 /* Parse a fold-expression.
4588 fold-expression:
4589 ( ... folding-operator cast-expression)
4590 ( cast-expression folding-operator ... )
4591 ( cast-expression folding operator ... folding-operator cast-expression)
4593 Note that the '(' and ')' are matched in primary expression. */
4595 static cp_expr
4596 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4598 cp_id_kind pidk;
4600 // Left fold.
4601 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4603 cp_lexer_consume_token (parser->lexer);
4604 int op = cp_parser_fold_operator (parser);
4605 if (op == ERROR_MARK)
4607 cp_parser_error (parser, "expected binary operator");
4608 return error_mark_node;
4611 tree expr = cp_parser_cast_expression (parser, false, false,
4612 false, &pidk);
4613 if (expr == error_mark_node)
4614 return error_mark_node;
4615 return finish_left_unary_fold_expr (expr, op);
4618 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4619 int op = cp_parser_fold_operator (parser);
4620 if (op == ERROR_MARK)
4622 cp_parser_error (parser, "expected binary operator");
4623 return error_mark_node;
4626 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4628 cp_parser_error (parser, "expected ...");
4629 return error_mark_node;
4631 cp_lexer_consume_token (parser->lexer);
4633 /* The operands of a fold-expression are cast-expressions, so binary or
4634 conditional expressions are not allowed. We check this here to avoid
4635 tentative parsing. */
4636 if (is_binary_op (TREE_CODE (expr1)))
4637 error_at (location_of (expr1),
4638 "binary expression in operand of fold-expression");
4639 else if (TREE_CODE (expr1) == COND_EXPR)
4640 error_at (location_of (expr1),
4641 "conditional expression in operand of fold-expression");
4643 // Right fold.
4644 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4645 return finish_right_unary_fold_expr (expr1, op);
4647 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4649 cp_parser_error (parser, "mismatched operator in fold-expression");
4650 return error_mark_node;
4652 cp_lexer_consume_token (parser->lexer);
4654 // Binary left or right fold.
4655 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4656 if (expr2 == error_mark_node)
4657 return error_mark_node;
4658 return finish_binary_fold_expr (expr1, expr2, op);
4661 /* Parse a primary-expression.
4663 primary-expression:
4664 literal
4665 this
4666 ( expression )
4667 id-expression
4668 lambda-expression (C++11)
4670 GNU Extensions:
4672 primary-expression:
4673 ( compound-statement )
4674 __builtin_va_arg ( assignment-expression , type-id )
4675 __builtin_offsetof ( type-id , offsetof-expression )
4677 C++ Extensions:
4678 __has_nothrow_assign ( type-id )
4679 __has_nothrow_constructor ( type-id )
4680 __has_nothrow_copy ( type-id )
4681 __has_trivial_assign ( type-id )
4682 __has_trivial_constructor ( type-id )
4683 __has_trivial_copy ( type-id )
4684 __has_trivial_destructor ( type-id )
4685 __has_virtual_destructor ( type-id )
4686 __is_abstract ( type-id )
4687 __is_base_of ( type-id , type-id )
4688 __is_class ( type-id )
4689 __is_empty ( type-id )
4690 __is_enum ( type-id )
4691 __is_final ( type-id )
4692 __is_literal_type ( type-id )
4693 __is_pod ( type-id )
4694 __is_polymorphic ( type-id )
4695 __is_std_layout ( type-id )
4696 __is_trivial ( type-id )
4697 __is_union ( type-id )
4699 Objective-C++ Extension:
4701 primary-expression:
4702 objc-expression
4704 literal:
4705 __null
4707 ADDRESS_P is true iff this expression was immediately preceded by
4708 "&" and therefore might denote a pointer-to-member. CAST_P is true
4709 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4710 true iff this expression is a template argument.
4712 Returns a representation of the expression. Upon return, *IDK
4713 indicates what kind of id-expression (if any) was present. */
4715 static cp_expr
4716 cp_parser_primary_expression (cp_parser *parser,
4717 bool address_p,
4718 bool cast_p,
4719 bool template_arg_p,
4720 bool decltype_p,
4721 cp_id_kind *idk)
4723 cp_token *token = NULL;
4725 /* Assume the primary expression is not an id-expression. */
4726 *idk = CP_ID_KIND_NONE;
4728 /* Peek at the next token. */
4729 token = cp_lexer_peek_token (parser->lexer);
4730 switch ((int) token->type)
4732 /* literal:
4733 integer-literal
4734 character-literal
4735 floating-literal
4736 string-literal
4737 boolean-literal
4738 pointer-literal
4739 user-defined-literal */
4740 case CPP_CHAR:
4741 case CPP_CHAR16:
4742 case CPP_CHAR32:
4743 case CPP_WCHAR:
4744 case CPP_UTF8CHAR:
4745 case CPP_NUMBER:
4746 case CPP_PREPARSED_EXPR:
4747 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4748 return cp_parser_userdef_numeric_literal (parser);
4749 token = cp_lexer_consume_token (parser->lexer);
4750 if (TREE_CODE (token->u.value) == FIXED_CST)
4752 error_at (token->location,
4753 "fixed-point types not supported in C++");
4754 return error_mark_node;
4756 /* Floating-point literals are only allowed in an integral
4757 constant expression if they are cast to an integral or
4758 enumeration type. */
4759 if (TREE_CODE (token->u.value) == REAL_CST
4760 && parser->integral_constant_expression_p
4761 && pedantic)
4763 /* CAST_P will be set even in invalid code like "int(2.7 +
4764 ...)". Therefore, we have to check that the next token
4765 is sure to end the cast. */
4766 if (cast_p)
4768 cp_token *next_token;
4770 next_token = cp_lexer_peek_token (parser->lexer);
4771 if (/* The comma at the end of an
4772 enumerator-definition. */
4773 next_token->type != CPP_COMMA
4774 /* The curly brace at the end of an enum-specifier. */
4775 && next_token->type != CPP_CLOSE_BRACE
4776 /* The end of a statement. */
4777 && next_token->type != CPP_SEMICOLON
4778 /* The end of the cast-expression. */
4779 && next_token->type != CPP_CLOSE_PAREN
4780 /* The end of an array bound. */
4781 && next_token->type != CPP_CLOSE_SQUARE
4782 /* The closing ">" in a template-argument-list. */
4783 && (next_token->type != CPP_GREATER
4784 || parser->greater_than_is_operator_p)
4785 /* C++0x only: A ">>" treated like two ">" tokens,
4786 in a template-argument-list. */
4787 && (next_token->type != CPP_RSHIFT
4788 || (cxx_dialect == cxx98)
4789 || parser->greater_than_is_operator_p))
4790 cast_p = false;
4793 /* If we are within a cast, then the constraint that the
4794 cast is to an integral or enumeration type will be
4795 checked at that point. If we are not within a cast, then
4796 this code is invalid. */
4797 if (!cast_p)
4798 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4800 return cp_expr (token->u.value, token->location);
4802 case CPP_CHAR_USERDEF:
4803 case CPP_CHAR16_USERDEF:
4804 case CPP_CHAR32_USERDEF:
4805 case CPP_WCHAR_USERDEF:
4806 case CPP_UTF8CHAR_USERDEF:
4807 return cp_parser_userdef_char_literal (parser);
4809 case CPP_STRING:
4810 case CPP_STRING16:
4811 case CPP_STRING32:
4812 case CPP_WSTRING:
4813 case CPP_UTF8STRING:
4814 case CPP_STRING_USERDEF:
4815 case CPP_STRING16_USERDEF:
4816 case CPP_STRING32_USERDEF:
4817 case CPP_WSTRING_USERDEF:
4818 case CPP_UTF8STRING_USERDEF:
4819 /* ??? Should wide strings be allowed when parser->translate_strings_p
4820 is false (i.e. in attributes)? If not, we can kill the third
4821 argument to cp_parser_string_literal. */
4822 return cp_parser_string_literal (parser,
4823 parser->translate_strings_p,
4824 true);
4826 case CPP_OPEN_PAREN:
4827 /* If we see `( { ' then we are looking at the beginning of
4828 a GNU statement-expression. */
4829 if (cp_parser_allow_gnu_extensions_p (parser)
4830 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4832 /* Statement-expressions are not allowed by the standard. */
4833 pedwarn (token->location, OPT_Wpedantic,
4834 "ISO C++ forbids braced-groups within expressions");
4836 /* And they're not allowed outside of a function-body; you
4837 cannot, for example, write:
4839 int i = ({ int j = 3; j + 1; });
4841 at class or namespace scope. */
4842 if (!parser->in_function_body
4843 || parser->in_template_argument_list_p)
4845 error_at (token->location,
4846 "statement-expressions are not allowed outside "
4847 "functions nor in template-argument lists");
4848 cp_parser_skip_to_end_of_block_or_statement (parser);
4849 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4850 cp_lexer_consume_token (parser->lexer);
4851 return error_mark_node;
4853 else
4854 return cp_parser_statement_expr (parser);
4856 /* Otherwise it's a normal parenthesized expression. */
4858 cp_expr expr;
4859 bool saved_greater_than_is_operator_p;
4861 location_t open_paren_loc = token->location;
4863 /* Consume the `('. */
4864 cp_lexer_consume_token (parser->lexer);
4865 /* Within a parenthesized expression, a `>' token is always
4866 the greater-than operator. */
4867 saved_greater_than_is_operator_p
4868 = parser->greater_than_is_operator_p;
4869 parser->greater_than_is_operator_p = true;
4871 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4872 /* Left fold expression. */
4873 expr = NULL_TREE;
4874 else
4875 /* Parse the parenthesized expression. */
4876 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4878 token = cp_lexer_peek_token (parser->lexer);
4879 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
4881 expr = cp_parser_fold_expression (parser, expr);
4882 if (expr != error_mark_node
4883 && cxx_dialect < cxx1z
4884 && !in_system_header_at (input_location))
4885 pedwarn (input_location, 0, "fold-expressions only available "
4886 "with -std=c++1z or -std=gnu++1z");
4888 else
4889 /* Let the front end know that this expression was
4890 enclosed in parentheses. This matters in case, for
4891 example, the expression is of the form `A::B', since
4892 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4893 not. */
4894 expr = finish_parenthesized_expr (expr);
4896 /* DR 705: Wrapping an unqualified name in parentheses
4897 suppresses arg-dependent lookup. We want to pass back
4898 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4899 (c++/37862), but none of the others. */
4900 if (*idk != CP_ID_KIND_QUALIFIED)
4901 *idk = CP_ID_KIND_NONE;
4903 /* The `>' token might be the end of a template-id or
4904 template-parameter-list now. */
4905 parser->greater_than_is_operator_p
4906 = saved_greater_than_is_operator_p;
4908 /* Consume the `)'. */
4909 token = cp_lexer_peek_token (parser->lexer);
4910 location_t close_paren_loc = token->location;
4911 expr.set_range (open_paren_loc, close_paren_loc);
4912 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)
4913 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4914 cp_parser_skip_to_end_of_statement (parser);
4916 return expr;
4919 case CPP_OPEN_SQUARE:
4921 if (c_dialect_objc ())
4923 /* We might have an Objective-C++ message. */
4924 cp_parser_parse_tentatively (parser);
4925 tree msg = cp_parser_objc_message_expression (parser);
4926 /* If that works out, we're done ... */
4927 if (cp_parser_parse_definitely (parser))
4928 return msg;
4929 /* ... else, fall though to see if it's a lambda. */
4931 cp_expr lam = cp_parser_lambda_expression (parser);
4932 /* Don't warn about a failed tentative parse. */
4933 if (cp_parser_error_occurred (parser))
4934 return error_mark_node;
4935 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4936 return lam;
4939 case CPP_OBJC_STRING:
4940 if (c_dialect_objc ())
4941 /* We have an Objective-C++ string literal. */
4942 return cp_parser_objc_expression (parser);
4943 cp_parser_error (parser, "expected primary-expression");
4944 return error_mark_node;
4946 case CPP_KEYWORD:
4947 switch (token->keyword)
4949 /* These two are the boolean literals. */
4950 case RID_TRUE:
4951 cp_lexer_consume_token (parser->lexer);
4952 return cp_expr (boolean_true_node, token->location);
4953 case RID_FALSE:
4954 cp_lexer_consume_token (parser->lexer);
4955 return cp_expr (boolean_false_node, token->location);
4957 /* The `__null' literal. */
4958 case RID_NULL:
4959 cp_lexer_consume_token (parser->lexer);
4960 return cp_expr (null_node, token->location);
4962 /* The `nullptr' literal. */
4963 case RID_NULLPTR:
4964 cp_lexer_consume_token (parser->lexer);
4965 return cp_expr (nullptr_node, token->location);
4967 /* Recognize the `this' keyword. */
4968 case RID_THIS:
4969 cp_lexer_consume_token (parser->lexer);
4970 if (parser->local_variables_forbidden_p)
4972 error_at (token->location,
4973 "%<this%> may not be used in this context");
4974 return error_mark_node;
4976 /* Pointers cannot appear in constant-expressions. */
4977 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4978 return error_mark_node;
4979 return cp_expr (finish_this_expr (), token->location);
4981 /* The `operator' keyword can be the beginning of an
4982 id-expression. */
4983 case RID_OPERATOR:
4984 goto id_expression;
4986 case RID_FUNCTION_NAME:
4987 case RID_PRETTY_FUNCTION_NAME:
4988 case RID_C99_FUNCTION_NAME:
4990 non_integral_constant name;
4992 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4993 __func__ are the names of variables -- but they are
4994 treated specially. Therefore, they are handled here,
4995 rather than relying on the generic id-expression logic
4996 below. Grammatically, these names are id-expressions.
4998 Consume the token. */
4999 token = cp_lexer_consume_token (parser->lexer);
5001 switch (token->keyword)
5003 case RID_FUNCTION_NAME:
5004 name = NIC_FUNC_NAME;
5005 break;
5006 case RID_PRETTY_FUNCTION_NAME:
5007 name = NIC_PRETTY_FUNC;
5008 break;
5009 case RID_C99_FUNCTION_NAME:
5010 name = NIC_C99_FUNC;
5011 break;
5012 default:
5013 gcc_unreachable ();
5016 if (cp_parser_non_integral_constant_expression (parser, name))
5017 return error_mark_node;
5019 /* Look up the name. */
5020 return finish_fname (token->u.value);
5023 case RID_VA_ARG:
5025 tree expression;
5026 tree type;
5027 source_location type_location;
5028 location_t start_loc
5029 = cp_lexer_peek_token (parser->lexer)->location;
5030 /* The `__builtin_va_arg' construct is used to handle
5031 `va_arg'. Consume the `__builtin_va_arg' token. */
5032 cp_lexer_consume_token (parser->lexer);
5033 /* Look for the opening `('. */
5034 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5035 /* Now, parse the assignment-expression. */
5036 expression = cp_parser_assignment_expression (parser);
5037 /* Look for the `,'. */
5038 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5039 type_location = cp_lexer_peek_token (parser->lexer)->location;
5040 /* Parse the type-id. */
5042 type_id_in_expr_sentinel s (parser);
5043 type = cp_parser_type_id (parser);
5045 /* Look for the closing `)'. */
5046 location_t finish_loc
5047 = cp_lexer_peek_token (parser->lexer)->location;
5048 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5049 /* Using `va_arg' in a constant-expression is not
5050 allowed. */
5051 if (cp_parser_non_integral_constant_expression (parser,
5052 NIC_VA_ARG))
5053 return error_mark_node;
5054 /* Construct a location of the form:
5055 __builtin_va_arg (v, int)
5056 ~~~~~~~~~~~~~~~~~~~~~^~~~
5057 with the caret at the type, ranging from the start of the
5058 "__builtin_va_arg" token to the close paren. */
5059 location_t combined_loc
5060 = make_location (type_location, start_loc, finish_loc);
5061 return build_x_va_arg (combined_loc, expression, type);
5064 case RID_OFFSETOF:
5065 return cp_parser_builtin_offsetof (parser);
5067 case RID_HAS_NOTHROW_ASSIGN:
5068 case RID_HAS_NOTHROW_CONSTRUCTOR:
5069 case RID_HAS_NOTHROW_COPY:
5070 case RID_HAS_TRIVIAL_ASSIGN:
5071 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5072 case RID_HAS_TRIVIAL_COPY:
5073 case RID_HAS_TRIVIAL_DESTRUCTOR:
5074 case RID_HAS_VIRTUAL_DESTRUCTOR:
5075 case RID_IS_ABSTRACT:
5076 case RID_IS_BASE_OF:
5077 case RID_IS_CLASS:
5078 case RID_IS_EMPTY:
5079 case RID_IS_ENUM:
5080 case RID_IS_FINAL:
5081 case RID_IS_LITERAL_TYPE:
5082 case RID_IS_POD:
5083 case RID_IS_POLYMORPHIC:
5084 case RID_IS_SAME_AS:
5085 case RID_IS_STD_LAYOUT:
5086 case RID_IS_TRIVIAL:
5087 case RID_IS_TRIVIALLY_ASSIGNABLE:
5088 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5089 case RID_IS_TRIVIALLY_COPYABLE:
5090 case RID_IS_UNION:
5091 return cp_parser_trait_expr (parser, token->keyword);
5093 // C++ concepts
5094 case RID_REQUIRES:
5095 return cp_parser_requires_expression (parser);
5097 /* Objective-C++ expressions. */
5098 case RID_AT_ENCODE:
5099 case RID_AT_PROTOCOL:
5100 case RID_AT_SELECTOR:
5101 return cp_parser_objc_expression (parser);
5103 case RID_TEMPLATE:
5104 if (parser->in_function_body
5105 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5106 == CPP_LESS))
5108 error_at (token->location,
5109 "a template declaration cannot appear at block scope");
5110 cp_parser_skip_to_end_of_block_or_statement (parser);
5111 return error_mark_node;
5113 default:
5114 cp_parser_error (parser, "expected primary-expression");
5115 return error_mark_node;
5118 /* An id-expression can start with either an identifier, a
5119 `::' as the beginning of a qualified-id, or the "operator"
5120 keyword. */
5121 case CPP_NAME:
5122 case CPP_SCOPE:
5123 case CPP_TEMPLATE_ID:
5124 case CPP_NESTED_NAME_SPECIFIER:
5126 id_expression:
5127 cp_expr id_expression;
5128 cp_expr decl;
5129 const char *error_msg;
5130 bool template_p;
5131 bool done;
5132 cp_token *id_expr_token;
5134 /* Parse the id-expression. */
5135 id_expression
5136 = cp_parser_id_expression (parser,
5137 /*template_keyword_p=*/false,
5138 /*check_dependency_p=*/true,
5139 &template_p,
5140 /*declarator_p=*/false,
5141 /*optional_p=*/false);
5142 if (id_expression == error_mark_node)
5143 return error_mark_node;
5144 id_expr_token = token;
5145 token = cp_lexer_peek_token (parser->lexer);
5146 done = (token->type != CPP_OPEN_SQUARE
5147 && token->type != CPP_OPEN_PAREN
5148 && token->type != CPP_DOT
5149 && token->type != CPP_DEREF
5150 && token->type != CPP_PLUS_PLUS
5151 && token->type != CPP_MINUS_MINUS);
5152 /* If we have a template-id, then no further lookup is
5153 required. If the template-id was for a template-class, we
5154 will sometimes have a TYPE_DECL at this point. */
5155 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5156 || TREE_CODE (id_expression) == TYPE_DECL)
5157 decl = id_expression;
5158 /* Look up the name. */
5159 else
5161 tree ambiguous_decls;
5163 /* If we already know that this lookup is ambiguous, then
5164 we've already issued an error message; there's no reason
5165 to check again. */
5166 if (id_expr_token->type == CPP_NAME
5167 && id_expr_token->error_reported)
5169 cp_parser_simulate_error (parser);
5170 return error_mark_node;
5173 decl = cp_parser_lookup_name (parser, id_expression,
5174 none_type,
5175 template_p,
5176 /*is_namespace=*/false,
5177 /*check_dependency=*/true,
5178 &ambiguous_decls,
5179 id_expr_token->location);
5180 /* If the lookup was ambiguous, an error will already have
5181 been issued. */
5182 if (ambiguous_decls)
5183 return error_mark_node;
5185 /* In Objective-C++, we may have an Objective-C 2.0
5186 dot-syntax for classes here. */
5187 if (c_dialect_objc ()
5188 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5189 && TREE_CODE (decl) == TYPE_DECL
5190 && objc_is_class_name (decl))
5192 tree component;
5193 cp_lexer_consume_token (parser->lexer);
5194 component = cp_parser_identifier (parser);
5195 if (component == error_mark_node)
5196 return error_mark_node;
5198 tree result = objc_build_class_component_ref (id_expression,
5199 component);
5200 /* Build a location of the form:
5201 expr.component
5202 ~~~~~^~~~~~~~~
5203 with caret at the start of the component name (at
5204 input_location), ranging from the start of the id_expression
5205 to the end of the component name. */
5206 location_t combined_loc
5207 = make_location (input_location, id_expression.get_start (),
5208 get_finish (input_location));
5209 protected_set_expr_location (result, combined_loc);
5210 return result;
5213 /* In Objective-C++, an instance variable (ivar) may be preferred
5214 to whatever cp_parser_lookup_name() found.
5215 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5216 rest of c-family, we have to do a little extra work to preserve
5217 any location information in cp_expr "decl". Given that
5218 objc_lookup_ivar is implemented in "c-family" and "objc", we
5219 have a trip through the pure "tree" type, rather than cp_expr.
5220 Naively copying it back to "decl" would implicitly give the
5221 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5222 store an EXPR_LOCATION. Hence we only update "decl" (and
5223 hence its location_t) if we get back a different tree node. */
5224 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5225 id_expression);
5226 if (decl_tree != decl.get_value ())
5227 decl = cp_expr (decl_tree);
5229 /* If name lookup gives us a SCOPE_REF, then the
5230 qualifying scope was dependent. */
5231 if (TREE_CODE (decl) == SCOPE_REF)
5233 /* At this point, we do not know if DECL is a valid
5234 integral constant expression. We assume that it is
5235 in fact such an expression, so that code like:
5237 template <int N> struct A {
5238 int a[B<N>::i];
5241 is accepted. At template-instantiation time, we
5242 will check that B<N>::i is actually a constant. */
5243 return decl;
5245 /* Check to see if DECL is a local variable in a context
5246 where that is forbidden. */
5247 if (parser->local_variables_forbidden_p
5248 && local_variable_p (decl))
5250 /* It might be that we only found DECL because we are
5251 trying to be generous with pre-ISO scoping rules.
5252 For example, consider:
5254 int i;
5255 void g() {
5256 for (int i = 0; i < 10; ++i) {}
5257 extern void f(int j = i);
5260 Here, name look up will originally find the out
5261 of scope `i'. We need to issue a warning message,
5262 but then use the global `i'. */
5263 decl = check_for_out_of_scope_variable (decl);
5264 if (local_variable_p (decl))
5266 error_at (id_expr_token->location,
5267 "local variable %qD may not appear in this context",
5268 decl.get_value ());
5269 return error_mark_node;
5274 decl = (finish_id_expression
5275 (id_expression, decl, parser->scope,
5276 idk,
5277 parser->integral_constant_expression_p,
5278 parser->allow_non_integral_constant_expression_p,
5279 &parser->non_integral_constant_expression_p,
5280 template_p, done, address_p,
5281 template_arg_p,
5282 &error_msg,
5283 id_expr_token->location));
5284 if (error_msg)
5285 cp_parser_error (parser, error_msg);
5286 decl.set_location (id_expr_token->location);
5287 return decl;
5290 /* Anything else is an error. */
5291 default:
5292 cp_parser_error (parser, "expected primary-expression");
5293 return error_mark_node;
5297 static inline cp_expr
5298 cp_parser_primary_expression (cp_parser *parser,
5299 bool address_p,
5300 bool cast_p,
5301 bool template_arg_p,
5302 cp_id_kind *idk)
5304 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5305 /*decltype*/false, idk);
5308 /* Parse an id-expression.
5310 id-expression:
5311 unqualified-id
5312 qualified-id
5314 qualified-id:
5315 :: [opt] nested-name-specifier template [opt] unqualified-id
5316 :: identifier
5317 :: operator-function-id
5318 :: template-id
5320 Return a representation of the unqualified portion of the
5321 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5322 a `::' or nested-name-specifier.
5324 Often, if the id-expression was a qualified-id, the caller will
5325 want to make a SCOPE_REF to represent the qualified-id. This
5326 function does not do this in order to avoid wastefully creating
5327 SCOPE_REFs when they are not required.
5329 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5330 `template' keyword.
5332 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5333 uninstantiated templates.
5335 If *TEMPLATE_P is non-NULL, it is set to true iff the
5336 `template' keyword is used to explicitly indicate that the entity
5337 named is a template.
5339 If DECLARATOR_P is true, the id-expression is appearing as part of
5340 a declarator, rather than as part of an expression. */
5342 static cp_expr
5343 cp_parser_id_expression (cp_parser *parser,
5344 bool template_keyword_p,
5345 bool check_dependency_p,
5346 bool *template_p,
5347 bool declarator_p,
5348 bool optional_p)
5350 bool global_scope_p;
5351 bool nested_name_specifier_p;
5353 /* Assume the `template' keyword was not used. */
5354 if (template_p)
5355 *template_p = template_keyword_p;
5357 /* Look for the optional `::' operator. */
5358 global_scope_p
5359 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
5360 != NULL_TREE);
5361 /* Look for the optional nested-name-specifier. */
5362 nested_name_specifier_p
5363 = (cp_parser_nested_name_specifier_opt (parser,
5364 /*typename_keyword_p=*/false,
5365 check_dependency_p,
5366 /*type_p=*/false,
5367 declarator_p)
5368 != NULL_TREE);
5369 /* If there is a nested-name-specifier, then we are looking at
5370 the first qualified-id production. */
5371 if (nested_name_specifier_p)
5373 tree saved_scope;
5374 tree saved_object_scope;
5375 tree saved_qualifying_scope;
5376 tree unqualified_id;
5377 bool is_template;
5379 /* See if the next token is the `template' keyword. */
5380 if (!template_p)
5381 template_p = &is_template;
5382 *template_p = cp_parser_optional_template_keyword (parser);
5383 /* Name lookup we do during the processing of the
5384 unqualified-id might obliterate SCOPE. */
5385 saved_scope = parser->scope;
5386 saved_object_scope = parser->object_scope;
5387 saved_qualifying_scope = parser->qualifying_scope;
5388 /* Process the final unqualified-id. */
5389 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5390 check_dependency_p,
5391 declarator_p,
5392 /*optional_p=*/false);
5393 /* Restore the SAVED_SCOPE for our caller. */
5394 parser->scope = saved_scope;
5395 parser->object_scope = saved_object_scope;
5396 parser->qualifying_scope = saved_qualifying_scope;
5398 return unqualified_id;
5400 /* Otherwise, if we are in global scope, then we are looking at one
5401 of the other qualified-id productions. */
5402 else if (global_scope_p)
5404 cp_token *token;
5405 tree id;
5407 /* Peek at the next token. */
5408 token = cp_lexer_peek_token (parser->lexer);
5410 /* If it's an identifier, and the next token is not a "<", then
5411 we can avoid the template-id case. This is an optimization
5412 for this common case. */
5413 if (token->type == CPP_NAME
5414 && !cp_parser_nth_token_starts_template_argument_list_p
5415 (parser, 2))
5416 return cp_parser_identifier (parser);
5418 cp_parser_parse_tentatively (parser);
5419 /* Try a template-id. */
5420 id = cp_parser_template_id (parser,
5421 /*template_keyword_p=*/false,
5422 /*check_dependency_p=*/true,
5423 none_type,
5424 declarator_p);
5425 /* If that worked, we're done. */
5426 if (cp_parser_parse_definitely (parser))
5427 return id;
5429 /* Peek at the next token. (Changes in the token buffer may
5430 have invalidated the pointer obtained above.) */
5431 token = cp_lexer_peek_token (parser->lexer);
5433 switch (token->type)
5435 case CPP_NAME:
5436 return cp_parser_identifier (parser);
5438 case CPP_KEYWORD:
5439 if (token->keyword == RID_OPERATOR)
5440 return cp_parser_operator_function_id (parser);
5441 /* Fall through. */
5443 default:
5444 cp_parser_error (parser, "expected id-expression");
5445 return error_mark_node;
5448 else
5449 return cp_parser_unqualified_id (parser, template_keyword_p,
5450 /*check_dependency_p=*/true,
5451 declarator_p,
5452 optional_p);
5455 /* Parse an unqualified-id.
5457 unqualified-id:
5458 identifier
5459 operator-function-id
5460 conversion-function-id
5461 ~ class-name
5462 template-id
5464 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5465 keyword, in a construct like `A::template ...'.
5467 Returns a representation of unqualified-id. For the `identifier'
5468 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5469 production a BIT_NOT_EXPR is returned; the operand of the
5470 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5471 other productions, see the documentation accompanying the
5472 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5473 names are looked up in uninstantiated templates. If DECLARATOR_P
5474 is true, the unqualified-id is appearing as part of a declarator,
5475 rather than as part of an expression. */
5477 static cp_expr
5478 cp_parser_unqualified_id (cp_parser* parser,
5479 bool template_keyword_p,
5480 bool check_dependency_p,
5481 bool declarator_p,
5482 bool optional_p)
5484 cp_token *token;
5486 /* Peek at the next token. */
5487 token = cp_lexer_peek_token (parser->lexer);
5489 switch ((int) token->type)
5491 case CPP_NAME:
5493 tree id;
5495 /* We don't know yet whether or not this will be a
5496 template-id. */
5497 cp_parser_parse_tentatively (parser);
5498 /* Try a template-id. */
5499 id = cp_parser_template_id (parser, template_keyword_p,
5500 check_dependency_p,
5501 none_type,
5502 declarator_p);
5503 /* If it worked, we're done. */
5504 if (cp_parser_parse_definitely (parser))
5505 return id;
5506 /* Otherwise, it's an ordinary identifier. */
5507 return cp_parser_identifier (parser);
5510 case CPP_TEMPLATE_ID:
5511 return cp_parser_template_id (parser, template_keyword_p,
5512 check_dependency_p,
5513 none_type,
5514 declarator_p);
5516 case CPP_COMPL:
5518 tree type_decl;
5519 tree qualifying_scope;
5520 tree object_scope;
5521 tree scope;
5522 bool done;
5524 /* Consume the `~' token. */
5525 cp_lexer_consume_token (parser->lexer);
5526 /* Parse the class-name. The standard, as written, seems to
5527 say that:
5529 template <typename T> struct S { ~S (); };
5530 template <typename T> S<T>::~S() {}
5532 is invalid, since `~' must be followed by a class-name, but
5533 `S<T>' is dependent, and so not known to be a class.
5534 That's not right; we need to look in uninstantiated
5535 templates. A further complication arises from:
5537 template <typename T> void f(T t) {
5538 t.T::~T();
5541 Here, it is not possible to look up `T' in the scope of `T'
5542 itself. We must look in both the current scope, and the
5543 scope of the containing complete expression.
5545 Yet another issue is:
5547 struct S {
5548 int S;
5549 ~S();
5552 S::~S() {}
5554 The standard does not seem to say that the `S' in `~S'
5555 should refer to the type `S' and not the data member
5556 `S::S'. */
5558 /* DR 244 says that we look up the name after the "~" in the
5559 same scope as we looked up the qualifying name. That idea
5560 isn't fully worked out; it's more complicated than that. */
5561 scope = parser->scope;
5562 object_scope = parser->object_scope;
5563 qualifying_scope = parser->qualifying_scope;
5565 /* Check for invalid scopes. */
5566 if (scope == error_mark_node)
5568 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5569 cp_lexer_consume_token (parser->lexer);
5570 return error_mark_node;
5572 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5574 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5575 error_at (token->location,
5576 "scope %qT before %<~%> is not a class-name",
5577 scope);
5578 cp_parser_simulate_error (parser);
5579 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5580 cp_lexer_consume_token (parser->lexer);
5581 return error_mark_node;
5583 gcc_assert (!scope || TYPE_P (scope));
5585 /* If the name is of the form "X::~X" it's OK even if X is a
5586 typedef. */
5587 token = cp_lexer_peek_token (parser->lexer);
5588 if (scope
5589 && token->type == CPP_NAME
5590 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5591 != CPP_LESS)
5592 && (token->u.value == TYPE_IDENTIFIER (scope)
5593 || (CLASS_TYPE_P (scope)
5594 && constructor_name_p (token->u.value, scope))))
5596 cp_lexer_consume_token (parser->lexer);
5597 return build_nt (BIT_NOT_EXPR, scope);
5600 /* ~auto means the destructor of whatever the object is. */
5601 if (cp_parser_is_keyword (token, RID_AUTO))
5603 if (cxx_dialect < cxx14)
5604 pedwarn (input_location, 0,
5605 "%<~auto%> only available with "
5606 "-std=c++14 or -std=gnu++14");
5607 cp_lexer_consume_token (parser->lexer);
5608 return build_nt (BIT_NOT_EXPR, make_auto ());
5611 /* If there was an explicit qualification (S::~T), first look
5612 in the scope given by the qualification (i.e., S).
5614 Note: in the calls to cp_parser_class_name below we pass
5615 typename_type so that lookup finds the injected-class-name
5616 rather than the constructor. */
5617 done = false;
5618 type_decl = NULL_TREE;
5619 if (scope)
5621 cp_parser_parse_tentatively (parser);
5622 type_decl = cp_parser_class_name (parser,
5623 /*typename_keyword_p=*/false,
5624 /*template_keyword_p=*/false,
5625 typename_type,
5626 /*check_dependency=*/false,
5627 /*class_head_p=*/false,
5628 declarator_p);
5629 if (cp_parser_parse_definitely (parser))
5630 done = true;
5632 /* In "N::S::~S", look in "N" as well. */
5633 if (!done && scope && qualifying_scope)
5635 cp_parser_parse_tentatively (parser);
5636 parser->scope = qualifying_scope;
5637 parser->object_scope = NULL_TREE;
5638 parser->qualifying_scope = NULL_TREE;
5639 type_decl
5640 = cp_parser_class_name (parser,
5641 /*typename_keyword_p=*/false,
5642 /*template_keyword_p=*/false,
5643 typename_type,
5644 /*check_dependency=*/false,
5645 /*class_head_p=*/false,
5646 declarator_p);
5647 if (cp_parser_parse_definitely (parser))
5648 done = true;
5650 /* In "p->S::~T", look in the scope given by "*p" as well. */
5651 else if (!done && object_scope)
5653 cp_parser_parse_tentatively (parser);
5654 parser->scope = object_scope;
5655 parser->object_scope = NULL_TREE;
5656 parser->qualifying_scope = NULL_TREE;
5657 type_decl
5658 = cp_parser_class_name (parser,
5659 /*typename_keyword_p=*/false,
5660 /*template_keyword_p=*/false,
5661 typename_type,
5662 /*check_dependency=*/false,
5663 /*class_head_p=*/false,
5664 declarator_p);
5665 if (cp_parser_parse_definitely (parser))
5666 done = true;
5668 /* Look in the surrounding context. */
5669 if (!done)
5671 parser->scope = NULL_TREE;
5672 parser->object_scope = NULL_TREE;
5673 parser->qualifying_scope = NULL_TREE;
5674 if (processing_template_decl)
5675 cp_parser_parse_tentatively (parser);
5676 type_decl
5677 = cp_parser_class_name (parser,
5678 /*typename_keyword_p=*/false,
5679 /*template_keyword_p=*/false,
5680 typename_type,
5681 /*check_dependency=*/false,
5682 /*class_head_p=*/false,
5683 declarator_p);
5684 if (processing_template_decl
5685 && ! cp_parser_parse_definitely (parser))
5687 /* We couldn't find a type with this name. If we're parsing
5688 tentatively, fail and try something else. */
5689 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5691 cp_parser_simulate_error (parser);
5692 return error_mark_node;
5694 /* Otherwise, accept it and check for a match at instantiation
5695 time. */
5696 type_decl = cp_parser_identifier (parser);
5697 if (type_decl != error_mark_node)
5698 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5699 return type_decl;
5702 /* If an error occurred, assume that the name of the
5703 destructor is the same as the name of the qualifying
5704 class. That allows us to keep parsing after running
5705 into ill-formed destructor names. */
5706 if (type_decl == error_mark_node && scope)
5707 return build_nt (BIT_NOT_EXPR, scope);
5708 else if (type_decl == error_mark_node)
5709 return error_mark_node;
5711 /* Check that destructor name and scope match. */
5712 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5714 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5715 error_at (token->location,
5716 "declaration of %<~%T%> as member of %qT",
5717 type_decl, scope);
5718 cp_parser_simulate_error (parser);
5719 return error_mark_node;
5722 /* [class.dtor]
5724 A typedef-name that names a class shall not be used as the
5725 identifier in the declarator for a destructor declaration. */
5726 if (declarator_p
5727 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5728 && !DECL_SELF_REFERENCE_P (type_decl)
5729 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5730 error_at (token->location,
5731 "typedef-name %qD used as destructor declarator",
5732 type_decl);
5734 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5737 case CPP_KEYWORD:
5738 if (token->keyword == RID_OPERATOR)
5740 cp_expr id;
5742 /* This could be a template-id, so we try that first. */
5743 cp_parser_parse_tentatively (parser);
5744 /* Try a template-id. */
5745 id = cp_parser_template_id (parser, template_keyword_p,
5746 /*check_dependency_p=*/true,
5747 none_type,
5748 declarator_p);
5749 /* If that worked, we're done. */
5750 if (cp_parser_parse_definitely (parser))
5751 return id;
5752 /* We still don't know whether we're looking at an
5753 operator-function-id or a conversion-function-id. */
5754 cp_parser_parse_tentatively (parser);
5755 /* Try an operator-function-id. */
5756 id = cp_parser_operator_function_id (parser);
5757 /* If that didn't work, try a conversion-function-id. */
5758 if (!cp_parser_parse_definitely (parser))
5759 id = cp_parser_conversion_function_id (parser);
5760 else if (UDLIT_OPER_P (id))
5762 /* 17.6.3.3.5 */
5763 const char *name = UDLIT_OP_SUFFIX (id);
5764 if (name[0] != '_' && !in_system_header_at (input_location)
5765 && declarator_p)
5766 warning (0, "literal operator suffixes not preceded by %<_%>"
5767 " are reserved for future standardization");
5770 return id;
5772 /* Fall through. */
5774 default:
5775 if (optional_p)
5776 return NULL_TREE;
5777 cp_parser_error (parser, "expected unqualified-id");
5778 return error_mark_node;
5782 /* Parse an (optional) nested-name-specifier.
5784 nested-name-specifier: [C++98]
5785 class-or-namespace-name :: nested-name-specifier [opt]
5786 class-or-namespace-name :: template nested-name-specifier [opt]
5788 nested-name-specifier: [C++0x]
5789 type-name ::
5790 namespace-name ::
5791 nested-name-specifier identifier ::
5792 nested-name-specifier template [opt] simple-template-id ::
5794 PARSER->SCOPE should be set appropriately before this function is
5795 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5796 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5797 in name lookups.
5799 Sets PARSER->SCOPE to the class (TYPE) or namespace
5800 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5801 it unchanged if there is no nested-name-specifier. Returns the new
5802 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5804 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5805 part of a declaration and/or decl-specifier. */
5807 static tree
5808 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5809 bool typename_keyword_p,
5810 bool check_dependency_p,
5811 bool type_p,
5812 bool is_declaration)
5814 bool success = false;
5815 cp_token_position start = 0;
5816 cp_token *token;
5818 /* Remember where the nested-name-specifier starts. */
5819 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5821 start = cp_lexer_token_position (parser->lexer, false);
5822 push_deferring_access_checks (dk_deferred);
5825 while (true)
5827 tree new_scope;
5828 tree old_scope;
5829 tree saved_qualifying_scope;
5830 bool template_keyword_p;
5832 /* Spot cases that cannot be the beginning of a
5833 nested-name-specifier. */
5834 token = cp_lexer_peek_token (parser->lexer);
5836 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5837 the already parsed nested-name-specifier. */
5838 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5840 /* Grab the nested-name-specifier and continue the loop. */
5841 cp_parser_pre_parsed_nested_name_specifier (parser);
5842 /* If we originally encountered this nested-name-specifier
5843 with IS_DECLARATION set to false, we will not have
5844 resolved TYPENAME_TYPEs, so we must do so here. */
5845 if (is_declaration
5846 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5848 new_scope = resolve_typename_type (parser->scope,
5849 /*only_current_p=*/false);
5850 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5851 parser->scope = new_scope;
5853 success = true;
5854 continue;
5857 /* Spot cases that cannot be the beginning of a
5858 nested-name-specifier. On the second and subsequent times
5859 through the loop, we look for the `template' keyword. */
5860 if (success && token->keyword == RID_TEMPLATE)
5862 /* A template-id can start a nested-name-specifier. */
5863 else if (token->type == CPP_TEMPLATE_ID)
5865 /* DR 743: decltype can be used in a nested-name-specifier. */
5866 else if (token_is_decltype (token))
5868 else
5870 /* If the next token is not an identifier, then it is
5871 definitely not a type-name or namespace-name. */
5872 if (token->type != CPP_NAME)
5873 break;
5874 /* If the following token is neither a `<' (to begin a
5875 template-id), nor a `::', then we are not looking at a
5876 nested-name-specifier. */
5877 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5879 if (token->type == CPP_COLON
5880 && parser->colon_corrects_to_scope_p
5881 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5883 error_at (token->location,
5884 "found %<:%> in nested-name-specifier, expected %<::%>");
5885 token->type = CPP_SCOPE;
5888 if (token->type != CPP_SCOPE
5889 && !cp_parser_nth_token_starts_template_argument_list_p
5890 (parser, 2))
5891 break;
5894 /* The nested-name-specifier is optional, so we parse
5895 tentatively. */
5896 cp_parser_parse_tentatively (parser);
5898 /* Look for the optional `template' keyword, if this isn't the
5899 first time through the loop. */
5900 if (success)
5901 template_keyword_p = cp_parser_optional_template_keyword (parser);
5902 else
5903 template_keyword_p = false;
5905 /* Save the old scope since the name lookup we are about to do
5906 might destroy it. */
5907 old_scope = parser->scope;
5908 saved_qualifying_scope = parser->qualifying_scope;
5909 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5910 look up names in "X<T>::I" in order to determine that "Y" is
5911 a template. So, if we have a typename at this point, we make
5912 an effort to look through it. */
5913 if (is_declaration
5914 && !typename_keyword_p
5915 && parser->scope
5916 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5917 parser->scope = resolve_typename_type (parser->scope,
5918 /*only_current_p=*/false);
5919 /* Parse the qualifying entity. */
5920 new_scope
5921 = cp_parser_qualifying_entity (parser,
5922 typename_keyword_p,
5923 template_keyword_p,
5924 check_dependency_p,
5925 type_p,
5926 is_declaration);
5927 /* Look for the `::' token. */
5928 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5930 /* If we found what we wanted, we keep going; otherwise, we're
5931 done. */
5932 if (!cp_parser_parse_definitely (parser))
5934 bool error_p = false;
5936 /* Restore the OLD_SCOPE since it was valid before the
5937 failed attempt at finding the last
5938 class-or-namespace-name. */
5939 parser->scope = old_scope;
5940 parser->qualifying_scope = saved_qualifying_scope;
5942 /* If the next token is a decltype, and the one after that is a
5943 `::', then the decltype has failed to resolve to a class or
5944 enumeration type. Give this error even when parsing
5945 tentatively since it can't possibly be valid--and we're going
5946 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5947 won't get another chance.*/
5948 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5949 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5950 == CPP_SCOPE))
5952 token = cp_lexer_consume_token (parser->lexer);
5953 error_at (token->location, "decltype evaluates to %qT, "
5954 "which is not a class or enumeration type",
5955 token->u.tree_check_value->value);
5956 parser->scope = error_mark_node;
5957 error_p = true;
5958 /* As below. */
5959 success = true;
5960 cp_lexer_consume_token (parser->lexer);
5963 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
5964 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
5966 /* If we have a non-type template-id followed by ::, it can't
5967 possibly be valid. */
5968 token = cp_lexer_peek_token (parser->lexer);
5969 tree tid = token->u.tree_check_value->value;
5970 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
5971 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
5973 tree tmpl = NULL_TREE;
5974 if (is_overloaded_fn (tid))
5976 tree fns = get_fns (tid);
5977 if (!OVL_CHAIN (fns))
5978 tmpl = OVL_CURRENT (fns);
5979 error_at (token->location, "function template-id %qD "
5980 "in nested-name-specifier", tid);
5982 else
5984 /* Variable template. */
5985 tmpl = TREE_OPERAND (tid, 0);
5986 gcc_assert (variable_template_p (tmpl));
5987 error_at (token->location, "variable template-id %qD "
5988 "in nested-name-specifier", tid);
5990 if (tmpl)
5991 inform (DECL_SOURCE_LOCATION (tmpl),
5992 "%qD declared here", tmpl);
5994 parser->scope = error_mark_node;
5995 error_p = true;
5996 /* As below. */
5997 success = true;
5998 cp_lexer_consume_token (parser->lexer);
5999 cp_lexer_consume_token (parser->lexer);
6003 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6004 break;
6005 /* If the next token is an identifier, and the one after
6006 that is a `::', then any valid interpretation would have
6007 found a class-or-namespace-name. */
6008 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6009 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6010 == CPP_SCOPE)
6011 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6012 != CPP_COMPL))
6014 token = cp_lexer_consume_token (parser->lexer);
6015 if (!error_p)
6017 if (!token->error_reported)
6019 tree decl;
6020 tree ambiguous_decls;
6022 decl = cp_parser_lookup_name (parser, token->u.value,
6023 none_type,
6024 /*is_template=*/false,
6025 /*is_namespace=*/false,
6026 /*check_dependency=*/true,
6027 &ambiguous_decls,
6028 token->location);
6029 if (TREE_CODE (decl) == TEMPLATE_DECL)
6030 error_at (token->location,
6031 "%qD used without template parameters",
6032 decl);
6033 else if (ambiguous_decls)
6035 // cp_parser_lookup_name has the same diagnostic,
6036 // thus make sure to emit it at most once.
6037 if (cp_parser_uncommitted_to_tentative_parse_p
6038 (parser))
6040 error_at (token->location,
6041 "reference to %qD is ambiguous",
6042 token->u.value);
6043 print_candidates (ambiguous_decls);
6045 decl = error_mark_node;
6047 else
6049 if (cxx_dialect != cxx98)
6050 cp_parser_name_lookup_error
6051 (parser, token->u.value, decl, NLE_NOT_CXX98,
6052 token->location);
6053 else
6054 cp_parser_name_lookup_error
6055 (parser, token->u.value, decl, NLE_CXX98,
6056 token->location);
6059 parser->scope = error_mark_node;
6060 error_p = true;
6061 /* Treat this as a successful nested-name-specifier
6062 due to:
6064 [basic.lookup.qual]
6066 If the name found is not a class-name (clause
6067 _class_) or namespace-name (_namespace.def_), the
6068 program is ill-formed. */
6069 success = true;
6071 cp_lexer_consume_token (parser->lexer);
6073 break;
6075 /* We've found one valid nested-name-specifier. */
6076 success = true;
6077 /* Name lookup always gives us a DECL. */
6078 if (TREE_CODE (new_scope) == TYPE_DECL)
6079 new_scope = TREE_TYPE (new_scope);
6080 /* Uses of "template" must be followed by actual templates. */
6081 if (template_keyword_p
6082 && !(CLASS_TYPE_P (new_scope)
6083 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6084 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6085 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6086 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6087 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6088 == TEMPLATE_ID_EXPR)))
6089 permerror (input_location, TYPE_P (new_scope)
6090 ? G_("%qT is not a template")
6091 : G_("%qD is not a template"),
6092 new_scope);
6093 /* If it is a class scope, try to complete it; we are about to
6094 be looking up names inside the class. */
6095 if (TYPE_P (new_scope)
6096 /* Since checking types for dependency can be expensive,
6097 avoid doing it if the type is already complete. */
6098 && !COMPLETE_TYPE_P (new_scope)
6099 /* Do not try to complete dependent types. */
6100 && !dependent_type_p (new_scope))
6102 new_scope = complete_type (new_scope);
6103 /* If it is a typedef to current class, use the current
6104 class instead, as the typedef won't have any names inside
6105 it yet. */
6106 if (!COMPLETE_TYPE_P (new_scope)
6107 && currently_open_class (new_scope))
6108 new_scope = TYPE_MAIN_VARIANT (new_scope);
6110 /* Make sure we look in the right scope the next time through
6111 the loop. */
6112 parser->scope = new_scope;
6115 /* If parsing tentatively, replace the sequence of tokens that makes
6116 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6117 token. That way, should we re-parse the token stream, we will
6118 not have to repeat the effort required to do the parse, nor will
6119 we issue duplicate error messages. */
6120 if (success && start)
6122 cp_token *token;
6124 token = cp_lexer_token_at (parser->lexer, start);
6125 /* Reset the contents of the START token. */
6126 token->type = CPP_NESTED_NAME_SPECIFIER;
6127 /* Retrieve any deferred checks. Do not pop this access checks yet
6128 so the memory will not be reclaimed during token replacing below. */
6129 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6130 token->u.tree_check_value->value = parser->scope;
6131 token->u.tree_check_value->checks = get_deferred_access_checks ();
6132 token->u.tree_check_value->qualifying_scope =
6133 parser->qualifying_scope;
6134 token->keyword = RID_MAX;
6136 /* Purge all subsequent tokens. */
6137 cp_lexer_purge_tokens_after (parser->lexer, start);
6140 if (start)
6141 pop_to_parent_deferring_access_checks ();
6143 return success ? parser->scope : NULL_TREE;
6146 /* Parse a nested-name-specifier. See
6147 cp_parser_nested_name_specifier_opt for details. This function
6148 behaves identically, except that it will an issue an error if no
6149 nested-name-specifier is present. */
6151 static tree
6152 cp_parser_nested_name_specifier (cp_parser *parser,
6153 bool typename_keyword_p,
6154 bool check_dependency_p,
6155 bool type_p,
6156 bool is_declaration)
6158 tree scope;
6160 /* Look for the nested-name-specifier. */
6161 scope = cp_parser_nested_name_specifier_opt (parser,
6162 typename_keyword_p,
6163 check_dependency_p,
6164 type_p,
6165 is_declaration);
6166 /* If it was not present, issue an error message. */
6167 if (!scope)
6169 cp_parser_error (parser, "expected nested-name-specifier");
6170 parser->scope = NULL_TREE;
6173 return scope;
6176 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6177 this is either a class-name or a namespace-name (which corresponds
6178 to the class-or-namespace-name production in the grammar). For
6179 C++0x, it can also be a type-name that refers to an enumeration
6180 type or a simple-template-id.
6182 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6183 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6184 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6185 TYPE_P is TRUE iff the next name should be taken as a class-name,
6186 even the same name is declared to be another entity in the same
6187 scope.
6189 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6190 specified by the class-or-namespace-name. If neither is found the
6191 ERROR_MARK_NODE is returned. */
6193 static tree
6194 cp_parser_qualifying_entity (cp_parser *parser,
6195 bool typename_keyword_p,
6196 bool template_keyword_p,
6197 bool check_dependency_p,
6198 bool type_p,
6199 bool is_declaration)
6201 tree saved_scope;
6202 tree saved_qualifying_scope;
6203 tree saved_object_scope;
6204 tree scope;
6205 bool only_class_p;
6206 bool successful_parse_p;
6208 /* DR 743: decltype can appear in a nested-name-specifier. */
6209 if (cp_lexer_next_token_is_decltype (parser->lexer))
6211 scope = cp_parser_decltype (parser);
6212 if (TREE_CODE (scope) != ENUMERAL_TYPE
6213 && !MAYBE_CLASS_TYPE_P (scope))
6215 cp_parser_simulate_error (parser);
6216 return error_mark_node;
6218 if (TYPE_NAME (scope))
6219 scope = TYPE_NAME (scope);
6220 return scope;
6223 /* Before we try to parse the class-name, we must save away the
6224 current PARSER->SCOPE since cp_parser_class_name will destroy
6225 it. */
6226 saved_scope = parser->scope;
6227 saved_qualifying_scope = parser->qualifying_scope;
6228 saved_object_scope = parser->object_scope;
6229 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6230 there is no need to look for a namespace-name. */
6231 only_class_p = template_keyword_p
6232 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6233 if (!only_class_p)
6234 cp_parser_parse_tentatively (parser);
6235 scope = cp_parser_class_name (parser,
6236 typename_keyword_p,
6237 template_keyword_p,
6238 type_p ? class_type : none_type,
6239 check_dependency_p,
6240 /*class_head_p=*/false,
6241 is_declaration,
6242 /*enum_ok=*/cxx_dialect > cxx98);
6243 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6244 /* If that didn't work, try for a namespace-name. */
6245 if (!only_class_p && !successful_parse_p)
6247 /* Restore the saved scope. */
6248 parser->scope = saved_scope;
6249 parser->qualifying_scope = saved_qualifying_scope;
6250 parser->object_scope = saved_object_scope;
6251 /* If we are not looking at an identifier followed by the scope
6252 resolution operator, then this is not part of a
6253 nested-name-specifier. (Note that this function is only used
6254 to parse the components of a nested-name-specifier.) */
6255 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6256 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6257 return error_mark_node;
6258 scope = cp_parser_namespace_name (parser);
6261 return scope;
6264 /* Return true if we are looking at a compound-literal, false otherwise. */
6266 static bool
6267 cp_parser_compound_literal_p (cp_parser *parser)
6269 /* Consume the `('. */
6270 cp_lexer_consume_token (parser->lexer);
6272 cp_lexer_save_tokens (parser->lexer);
6274 /* Skip tokens until the next token is a closing parenthesis.
6275 If we find the closing `)', and the next token is a `{', then
6276 we are looking at a compound-literal. */
6277 bool compound_literal_p
6278 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6279 /*consume_paren=*/true)
6280 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6282 /* Roll back the tokens we skipped. */
6283 cp_lexer_rollback_tokens (parser->lexer);
6285 return compound_literal_p;
6288 /* Parse a postfix-expression.
6290 postfix-expression:
6291 primary-expression
6292 postfix-expression [ expression ]
6293 postfix-expression ( expression-list [opt] )
6294 simple-type-specifier ( expression-list [opt] )
6295 typename :: [opt] nested-name-specifier identifier
6296 ( expression-list [opt] )
6297 typename :: [opt] nested-name-specifier template [opt] template-id
6298 ( expression-list [opt] )
6299 postfix-expression . template [opt] id-expression
6300 postfix-expression -> template [opt] id-expression
6301 postfix-expression . pseudo-destructor-name
6302 postfix-expression -> pseudo-destructor-name
6303 postfix-expression ++
6304 postfix-expression --
6305 dynamic_cast < type-id > ( expression )
6306 static_cast < type-id > ( expression )
6307 reinterpret_cast < type-id > ( expression )
6308 const_cast < type-id > ( expression )
6309 typeid ( expression )
6310 typeid ( type-id )
6312 GNU Extension:
6314 postfix-expression:
6315 ( type-id ) { initializer-list , [opt] }
6317 This extension is a GNU version of the C99 compound-literal
6318 construct. (The C99 grammar uses `type-name' instead of `type-id',
6319 but they are essentially the same concept.)
6321 If ADDRESS_P is true, the postfix expression is the operand of the
6322 `&' operator. CAST_P is true if this expression is the target of a
6323 cast.
6325 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6326 class member access expressions [expr.ref].
6328 Returns a representation of the expression. */
6330 static cp_expr
6331 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6332 bool member_access_only_p, bool decltype_p,
6333 cp_id_kind * pidk_return)
6335 cp_token *token;
6336 location_t loc;
6337 enum rid keyword;
6338 cp_id_kind idk = CP_ID_KIND_NONE;
6339 cp_expr postfix_expression = NULL_TREE;
6340 bool is_member_access = false;
6341 int saved_in_statement = -1;
6343 /* Peek at the next token. */
6344 token = cp_lexer_peek_token (parser->lexer);
6345 loc = token->location;
6346 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6348 /* Some of the productions are determined by keywords. */
6349 keyword = token->keyword;
6350 switch (keyword)
6352 case RID_DYNCAST:
6353 case RID_STATCAST:
6354 case RID_REINTCAST:
6355 case RID_CONSTCAST:
6357 tree type;
6358 cp_expr expression;
6359 const char *saved_message;
6360 bool saved_in_type_id_in_expr_p;
6362 /* All of these can be handled in the same way from the point
6363 of view of parsing. Begin by consuming the token
6364 identifying the cast. */
6365 cp_lexer_consume_token (parser->lexer);
6367 /* New types cannot be defined in the cast. */
6368 saved_message = parser->type_definition_forbidden_message;
6369 parser->type_definition_forbidden_message
6370 = G_("types may not be defined in casts");
6372 /* Look for the opening `<'. */
6373 cp_parser_require (parser, CPP_LESS, RT_LESS);
6374 /* Parse the type to which we are casting. */
6375 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6376 parser->in_type_id_in_expr_p = true;
6377 type = cp_parser_type_id (parser);
6378 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6379 /* Look for the closing `>'. */
6380 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6381 /* Restore the old message. */
6382 parser->type_definition_forbidden_message = saved_message;
6384 bool saved_greater_than_is_operator_p
6385 = parser->greater_than_is_operator_p;
6386 parser->greater_than_is_operator_p = true;
6388 /* And the expression which is being cast. */
6389 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6390 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6391 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6392 RT_CLOSE_PAREN);
6393 location_t end_loc = close_paren ?
6394 close_paren->location : UNKNOWN_LOCATION;
6396 parser->greater_than_is_operator_p
6397 = saved_greater_than_is_operator_p;
6399 /* Only type conversions to integral or enumeration types
6400 can be used in constant-expressions. */
6401 if (!cast_valid_in_integral_constant_expression_p (type)
6402 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6403 return error_mark_node;
6405 switch (keyword)
6407 case RID_DYNCAST:
6408 postfix_expression
6409 = build_dynamic_cast (type, expression, tf_warning_or_error);
6410 break;
6411 case RID_STATCAST:
6412 postfix_expression
6413 = build_static_cast (type, expression, tf_warning_or_error);
6414 break;
6415 case RID_REINTCAST:
6416 postfix_expression
6417 = build_reinterpret_cast (type, expression,
6418 tf_warning_or_error);
6419 break;
6420 case RID_CONSTCAST:
6421 postfix_expression
6422 = build_const_cast (type, expression, tf_warning_or_error);
6423 break;
6424 default:
6425 gcc_unreachable ();
6428 /* Construct a location e.g. :
6429 reinterpret_cast <int *> (expr)
6430 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6431 ranging from the start of the "*_cast" token to the final closing
6432 paren, with the caret at the start. */
6433 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6434 postfix_expression.set_location (cp_cast_loc);
6436 break;
6438 case RID_TYPEID:
6440 tree type;
6441 const char *saved_message;
6442 bool saved_in_type_id_in_expr_p;
6444 /* Consume the `typeid' token. */
6445 cp_lexer_consume_token (parser->lexer);
6446 /* Look for the `(' token. */
6447 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6448 /* Types cannot be defined in a `typeid' expression. */
6449 saved_message = parser->type_definition_forbidden_message;
6450 parser->type_definition_forbidden_message
6451 = G_("types may not be defined in a %<typeid%> expression");
6452 /* We can't be sure yet whether we're looking at a type-id or an
6453 expression. */
6454 cp_parser_parse_tentatively (parser);
6455 /* Try a type-id first. */
6456 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6457 parser->in_type_id_in_expr_p = true;
6458 type = cp_parser_type_id (parser);
6459 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6460 /* Look for the `)' token. Otherwise, we can't be sure that
6461 we're not looking at an expression: consider `typeid (int
6462 (3))', for example. */
6463 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6464 /* If all went well, simply lookup the type-id. */
6465 if (cp_parser_parse_definitely (parser))
6466 postfix_expression = get_typeid (type, tf_warning_or_error);
6467 /* Otherwise, fall back to the expression variant. */
6468 else
6470 tree expression;
6472 /* Look for an expression. */
6473 expression = cp_parser_expression (parser, & idk);
6474 /* Compute its typeid. */
6475 postfix_expression = build_typeid (expression, tf_warning_or_error);
6476 /* Look for the `)' token. */
6477 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6479 /* Restore the saved message. */
6480 parser->type_definition_forbidden_message = saved_message;
6481 /* `typeid' may not appear in an integral constant expression. */
6482 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6483 return error_mark_node;
6485 break;
6487 case RID_TYPENAME:
6489 tree type;
6490 /* The syntax permitted here is the same permitted for an
6491 elaborated-type-specifier. */
6492 ++parser->prevent_constrained_type_specifiers;
6493 type = cp_parser_elaborated_type_specifier (parser,
6494 /*is_friend=*/false,
6495 /*is_declaration=*/false);
6496 --parser->prevent_constrained_type_specifiers;
6497 postfix_expression = cp_parser_functional_cast (parser, type);
6499 break;
6501 case RID_CILK_SPAWN:
6503 location_t cilk_spawn_loc
6504 = cp_lexer_peek_token (parser->lexer)->location;
6505 cp_lexer_consume_token (parser->lexer);
6506 token = cp_lexer_peek_token (parser->lexer);
6507 if (token->type == CPP_SEMICOLON)
6509 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6510 "an expression");
6511 postfix_expression = error_mark_node;
6512 break;
6514 else if (!current_function_decl)
6516 error_at (token->location, "%<_Cilk_spawn%> may only be used "
6517 "inside a function");
6518 postfix_expression = error_mark_node;
6519 break;
6521 else
6523 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6524 saved_in_statement = parser->in_statement;
6525 parser->in_statement |= IN_CILK_SPAWN;
6527 cfun->calls_cilk_spawn = 1;
6528 postfix_expression =
6529 cp_parser_postfix_expression (parser, false, false,
6530 false, false, &idk);
6531 if (!flag_cilkplus)
6533 error_at (token->location, "-fcilkplus must be enabled to use"
6534 " %<_Cilk_spawn%>");
6535 cfun->calls_cilk_spawn = 0;
6537 else if (saved_in_statement & IN_CILK_SPAWN)
6539 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6540 "are not permitted");
6541 postfix_expression = error_mark_node;
6542 cfun->calls_cilk_spawn = 0;
6544 else
6546 location_t loc = postfix_expression.get_location ();
6547 postfix_expression = build_cilk_spawn (token->location,
6548 postfix_expression);
6549 /* Build a location of the form:
6550 _Cilk_spawn expr
6551 ~~~~~~~~~~~~^~~~
6552 with caret at the expr, ranging from the start of the
6553 _Cilk_spawn token to the end of the expression. */
6554 location_t combined_loc =
6555 make_location (loc, cilk_spawn_loc, get_finish (loc));
6556 postfix_expression.set_location (combined_loc);
6557 if (postfix_expression != error_mark_node)
6558 SET_EXPR_LOCATION (postfix_expression, input_location);
6559 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6561 break;
6564 case RID_BUILTIN_SHUFFLE:
6566 vec<tree, va_gc> *vec;
6567 unsigned int i;
6568 tree p;
6570 cp_lexer_consume_token (parser->lexer);
6571 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6572 /*cast_p=*/false, /*allow_expansion_p=*/true,
6573 /*non_constant_p=*/NULL);
6574 if (vec == NULL)
6575 return error_mark_node;
6577 FOR_EACH_VEC_ELT (*vec, i, p)
6578 mark_exp_read (p);
6580 if (vec->length () == 2)
6581 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
6582 tf_warning_or_error);
6583 else if (vec->length () == 3)
6584 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6585 tf_warning_or_error);
6586 else
6588 error_at (loc, "wrong number of arguments to "
6589 "%<__builtin_shuffle%>");
6590 return error_mark_node;
6592 break;
6595 default:
6597 tree type;
6599 /* If the next thing is a simple-type-specifier, we may be
6600 looking at a functional cast. We could also be looking at
6601 an id-expression. So, we try the functional cast, and if
6602 that doesn't work we fall back to the primary-expression. */
6603 cp_parser_parse_tentatively (parser);
6604 /* Look for the simple-type-specifier. */
6605 ++parser->prevent_constrained_type_specifiers;
6606 type = cp_parser_simple_type_specifier (parser,
6607 /*decl_specs=*/NULL,
6608 CP_PARSER_FLAGS_NONE);
6609 --parser->prevent_constrained_type_specifiers;
6610 /* Parse the cast itself. */
6611 if (!cp_parser_error_occurred (parser))
6612 postfix_expression
6613 = cp_parser_functional_cast (parser, type);
6614 /* If that worked, we're done. */
6615 if (cp_parser_parse_definitely (parser))
6616 break;
6618 /* If the functional-cast didn't work out, try a
6619 compound-literal. */
6620 if (cp_parser_allow_gnu_extensions_p (parser)
6621 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6623 cp_expr initializer = NULL_TREE;
6625 cp_parser_parse_tentatively (parser);
6627 /* Avoid calling cp_parser_type_id pointlessly, see comment
6628 in cp_parser_cast_expression about c++/29234. */
6629 if (!cp_parser_compound_literal_p (parser))
6630 cp_parser_simulate_error (parser);
6631 else
6633 /* Parse the type. */
6634 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6635 parser->in_type_id_in_expr_p = true;
6636 type = cp_parser_type_id (parser);
6637 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6638 /* Look for the `)'. */
6639 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6642 /* If things aren't going well, there's no need to
6643 keep going. */
6644 if (!cp_parser_error_occurred (parser))
6646 bool non_constant_p;
6647 /* Parse the brace-enclosed initializer list. */
6648 initializer = cp_parser_braced_list (parser,
6649 &non_constant_p);
6651 /* If that worked, we're definitely looking at a
6652 compound-literal expression. */
6653 if (cp_parser_parse_definitely (parser))
6655 /* Warn the user that a compound literal is not
6656 allowed in standard C++. */
6657 pedwarn (input_location, OPT_Wpedantic,
6658 "ISO C++ forbids compound-literals");
6659 /* For simplicity, we disallow compound literals in
6660 constant-expressions. We could
6661 allow compound literals of integer type, whose
6662 initializer was a constant, in constant
6663 expressions. Permitting that usage, as a further
6664 extension, would not change the meaning of any
6665 currently accepted programs. (Of course, as
6666 compound literals are not part of ISO C++, the
6667 standard has nothing to say.) */
6668 if (cp_parser_non_integral_constant_expression (parser,
6669 NIC_NCC))
6671 postfix_expression = error_mark_node;
6672 break;
6674 /* Form the representation of the compound-literal. */
6675 postfix_expression
6676 = finish_compound_literal (type, initializer,
6677 tf_warning_or_error);
6678 postfix_expression.set_location (initializer.get_location ());
6679 break;
6683 /* It must be a primary-expression. */
6684 postfix_expression
6685 = cp_parser_primary_expression (parser, address_p, cast_p,
6686 /*template_arg_p=*/false,
6687 decltype_p,
6688 &idk);
6690 break;
6693 /* Note that we don't need to worry about calling build_cplus_new on a
6694 class-valued CALL_EXPR in decltype when it isn't the end of the
6695 postfix-expression; unary_complex_lvalue will take care of that for
6696 all these cases. */
6698 /* Keep looping until the postfix-expression is complete. */
6699 while (true)
6701 if (idk == CP_ID_KIND_UNQUALIFIED
6702 && identifier_p (postfix_expression)
6703 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6704 /* It is not a Koenig lookup function call. */
6705 postfix_expression
6706 = unqualified_name_lookup_error (postfix_expression);
6708 /* Peek at the next token. */
6709 token = cp_lexer_peek_token (parser->lexer);
6711 switch (token->type)
6713 case CPP_OPEN_SQUARE:
6714 if (cp_next_tokens_can_be_std_attribute_p (parser))
6716 cp_parser_error (parser,
6717 "two consecutive %<[%> shall "
6718 "only introduce an attribute");
6719 return error_mark_node;
6721 postfix_expression
6722 = cp_parser_postfix_open_square_expression (parser,
6723 postfix_expression,
6724 false,
6725 decltype_p);
6726 postfix_expression.set_range (start_loc,
6727 postfix_expression.get_location ());
6729 idk = CP_ID_KIND_NONE;
6730 is_member_access = false;
6731 break;
6733 case CPP_OPEN_PAREN:
6734 /* postfix-expression ( expression-list [opt] ) */
6736 bool koenig_p;
6737 bool is_builtin_constant_p;
6738 bool saved_integral_constant_expression_p = false;
6739 bool saved_non_integral_constant_expression_p = false;
6740 tsubst_flags_t complain = complain_flags (decltype_p);
6741 vec<tree, va_gc> *args;
6742 location_t close_paren_loc = UNKNOWN_LOCATION;
6744 is_member_access = false;
6746 is_builtin_constant_p
6747 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6748 if (is_builtin_constant_p)
6750 /* The whole point of __builtin_constant_p is to allow
6751 non-constant expressions to appear as arguments. */
6752 saved_integral_constant_expression_p
6753 = parser->integral_constant_expression_p;
6754 saved_non_integral_constant_expression_p
6755 = parser->non_integral_constant_expression_p;
6756 parser->integral_constant_expression_p = false;
6758 args = (cp_parser_parenthesized_expression_list
6759 (parser, non_attr,
6760 /*cast_p=*/false, /*allow_expansion_p=*/true,
6761 /*non_constant_p=*/NULL,
6762 /*close_paren_loc=*/&close_paren_loc));
6763 if (is_builtin_constant_p)
6765 parser->integral_constant_expression_p
6766 = saved_integral_constant_expression_p;
6767 parser->non_integral_constant_expression_p
6768 = saved_non_integral_constant_expression_p;
6771 if (args == NULL)
6773 postfix_expression = error_mark_node;
6774 break;
6777 /* Function calls are not permitted in
6778 constant-expressions. */
6779 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6780 && cp_parser_non_integral_constant_expression (parser,
6781 NIC_FUNC_CALL))
6783 postfix_expression = error_mark_node;
6784 release_tree_vector (args);
6785 break;
6788 koenig_p = false;
6789 if (idk == CP_ID_KIND_UNQUALIFIED
6790 || idk == CP_ID_KIND_TEMPLATE_ID)
6792 if (identifier_p (postfix_expression))
6794 if (!args->is_empty ())
6796 koenig_p = true;
6797 if (!any_type_dependent_arguments_p (args))
6798 postfix_expression
6799 = perform_koenig_lookup (postfix_expression, args,
6800 complain);
6802 else
6803 postfix_expression
6804 = unqualified_fn_lookup_error (postfix_expression);
6806 /* We do not perform argument-dependent lookup if
6807 normal lookup finds a non-function, in accordance
6808 with the expected resolution of DR 218. */
6809 else if (!args->is_empty ()
6810 && is_overloaded_fn (postfix_expression))
6812 tree fn = get_first_fn (postfix_expression);
6813 fn = STRIP_TEMPLATE (fn);
6815 /* Do not do argument dependent lookup if regular
6816 lookup finds a member function or a block-scope
6817 function declaration. [basic.lookup.argdep]/3 */
6818 if (!DECL_FUNCTION_MEMBER_P (fn)
6819 && !DECL_LOCAL_FUNCTION_P (fn))
6821 koenig_p = true;
6822 if (!any_type_dependent_arguments_p (args))
6823 postfix_expression
6824 = perform_koenig_lookup (postfix_expression, args,
6825 complain);
6830 if (warn_memset_transposed_args)
6832 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6833 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6834 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6835 && vec_safe_length (args) == 3
6836 && TREE_CODE ((*args)[2]) == INTEGER_CST
6837 && integer_zerop ((*args)[2])
6838 && !(TREE_CODE ((*args)[1]) == INTEGER_CST
6839 && integer_zerop ((*args)[1])))
6840 warning (OPT_Wmemset_transposed_args,
6841 "%<memset%> used with constant zero length "
6842 "parameter; this could be due to transposed "
6843 "parameters");
6846 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6848 tree instance = TREE_OPERAND (postfix_expression, 0);
6849 tree fn = TREE_OPERAND (postfix_expression, 1);
6851 if (processing_template_decl
6852 && (type_dependent_expression_p (instance)
6853 || (!BASELINK_P (fn)
6854 && TREE_CODE (fn) != FIELD_DECL)
6855 || type_dependent_expression_p (fn)
6856 || any_type_dependent_arguments_p (args)))
6858 postfix_expression
6859 = build_nt_call_vec (postfix_expression, args);
6860 release_tree_vector (args);
6861 break;
6864 if (BASELINK_P (fn))
6866 postfix_expression
6867 = (build_new_method_call
6868 (instance, fn, &args, NULL_TREE,
6869 (idk == CP_ID_KIND_QUALIFIED
6870 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6871 : LOOKUP_NORMAL),
6872 /*fn_p=*/NULL,
6873 complain));
6875 else
6876 postfix_expression
6877 = finish_call_expr (postfix_expression, &args,
6878 /*disallow_virtual=*/false,
6879 /*koenig_p=*/false,
6880 complain);
6882 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6883 || TREE_CODE (postfix_expression) == MEMBER_REF
6884 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6885 postfix_expression = (build_offset_ref_call_from_tree
6886 (postfix_expression, &args,
6887 complain));
6888 else if (idk == CP_ID_KIND_QUALIFIED)
6889 /* A call to a static class member, or a namespace-scope
6890 function. */
6891 postfix_expression
6892 = finish_call_expr (postfix_expression, &args,
6893 /*disallow_virtual=*/true,
6894 koenig_p,
6895 complain);
6896 else
6897 /* All other function calls. */
6898 postfix_expression
6899 = finish_call_expr (postfix_expression, &args,
6900 /*disallow_virtual=*/false,
6901 koenig_p,
6902 complain);
6904 if (close_paren_loc != UNKNOWN_LOCATION)
6906 location_t combined_loc = make_location (token->location,
6907 start_loc,
6908 close_paren_loc);
6909 postfix_expression.set_location (combined_loc);
6912 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6913 idk = CP_ID_KIND_NONE;
6915 release_tree_vector (args);
6917 break;
6919 case CPP_DOT:
6920 case CPP_DEREF:
6921 /* postfix-expression . template [opt] id-expression
6922 postfix-expression . pseudo-destructor-name
6923 postfix-expression -> template [opt] id-expression
6924 postfix-expression -> pseudo-destructor-name */
6926 /* Consume the `.' or `->' operator. */
6927 cp_lexer_consume_token (parser->lexer);
6929 postfix_expression
6930 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6931 postfix_expression,
6932 false, &idk, loc);
6934 is_member_access = true;
6935 break;
6937 case CPP_PLUS_PLUS:
6938 /* postfix-expression ++ */
6939 /* Consume the `++' token. */
6940 cp_lexer_consume_token (parser->lexer);
6941 /* Generate a representation for the complete expression. */
6942 postfix_expression
6943 = finish_increment_expr (postfix_expression,
6944 POSTINCREMENT_EXPR);
6945 /* Increments may not appear in constant-expressions. */
6946 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6947 postfix_expression = error_mark_node;
6948 idk = CP_ID_KIND_NONE;
6949 is_member_access = false;
6950 break;
6952 case CPP_MINUS_MINUS:
6953 /* postfix-expression -- */
6954 /* Consume the `--' token. */
6955 cp_lexer_consume_token (parser->lexer);
6956 /* Generate a representation for the complete expression. */
6957 postfix_expression
6958 = finish_increment_expr (postfix_expression,
6959 POSTDECREMENT_EXPR);
6960 /* Decrements may not appear in constant-expressions. */
6961 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6962 postfix_expression = error_mark_node;
6963 idk = CP_ID_KIND_NONE;
6964 is_member_access = false;
6965 break;
6967 default:
6968 if (pidk_return != NULL)
6969 * pidk_return = idk;
6970 if (member_access_only_p)
6971 return is_member_access
6972 ? postfix_expression
6973 : cp_expr (error_mark_node);
6974 else
6975 return postfix_expression;
6979 /* We should never get here. */
6980 gcc_unreachable ();
6981 return error_mark_node;
6984 /* This function parses Cilk Plus array notations. If a normal array expr. is
6985 parsed then the array index is passed back to the caller through *INIT_INDEX
6986 and the function returns a NULL_TREE. If array notation expr. is parsed,
6987 then *INIT_INDEX is ignored by the caller and the function returns
6988 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6989 error_mark_node. */
6991 static tree
6992 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6993 tree array_value)
6995 cp_token *token = NULL;
6996 tree length_index, stride = NULL_TREE, value_tree, array_type;
6997 if (!array_value || array_value == error_mark_node)
6999 cp_parser_skip_to_end_of_statement (parser);
7000 return error_mark_node;
7003 array_type = TREE_TYPE (array_value);
7005 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
7006 parser->colon_corrects_to_scope_p = false;
7007 token = cp_lexer_peek_token (parser->lexer);
7009 if (!token)
7011 cp_parser_error (parser, "expected %<:%> or numeral");
7012 return error_mark_node;
7014 else if (token->type == CPP_COLON)
7016 /* Consume the ':'. */
7017 cp_lexer_consume_token (parser->lexer);
7019 /* If we are here, then we have a case like this A[:]. */
7020 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
7022 cp_parser_error (parser, "expected %<]%>");
7023 cp_parser_skip_to_end_of_statement (parser);
7024 return error_mark_node;
7026 *init_index = NULL_TREE;
7027 stride = NULL_TREE;
7028 length_index = NULL_TREE;
7030 else
7032 /* If we are here, then there are three valid possibilities:
7033 1. ARRAY [ EXP ]
7034 2. ARRAY [ EXP : EXP ]
7035 3. ARRAY [ EXP : EXP : EXP ] */
7037 *init_index = cp_parser_expression (parser);
7038 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
7040 /* This indicates that we have a normal array expression. */
7041 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7042 return NULL_TREE;
7045 /* Consume the ':'. */
7046 cp_lexer_consume_token (parser->lexer);
7047 length_index = cp_parser_expression (parser);
7048 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7050 cp_lexer_consume_token (parser->lexer);
7051 stride = cp_parser_expression (parser);
7054 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7056 if (*init_index == error_mark_node || length_index == error_mark_node
7057 || stride == error_mark_node || array_type == error_mark_node)
7059 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
7060 cp_lexer_consume_token (parser->lexer);
7061 return error_mark_node;
7063 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7065 value_tree = build_array_notation_ref (loc, array_value, *init_index,
7066 length_index, stride, array_type);
7067 return value_tree;
7070 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7071 by cp_parser_builtin_offsetof. We're looking for
7073 postfix-expression [ expression ]
7074 postfix-expression [ braced-init-list ] (C++11)
7076 FOR_OFFSETOF is set if we're being called in that context, which
7077 changes how we deal with integer constant expressions. */
7079 static tree
7080 cp_parser_postfix_open_square_expression (cp_parser *parser,
7081 tree postfix_expression,
7082 bool for_offsetof,
7083 bool decltype_p)
7085 tree index = NULL_TREE;
7086 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7087 bool saved_greater_than_is_operator_p;
7089 /* Consume the `[' token. */
7090 cp_lexer_consume_token (parser->lexer);
7092 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7093 parser->greater_than_is_operator_p = true;
7095 /* Parse the index expression. */
7096 /* ??? For offsetof, there is a question of what to allow here. If
7097 offsetof is not being used in an integral constant expression context,
7098 then we *could* get the right answer by computing the value at runtime.
7099 If we are in an integral constant expression context, then we might
7100 could accept any constant expression; hard to say without analysis.
7101 Rather than open the barn door too wide right away, allow only integer
7102 constant expressions here. */
7103 if (for_offsetof)
7104 index = cp_parser_constant_expression (parser);
7105 else
7107 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7109 bool expr_nonconst_p;
7110 cp_lexer_set_source_position (parser->lexer);
7111 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7112 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7113 if (flag_cilkplus
7114 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7116 error_at (cp_lexer_peek_token (parser->lexer)->location,
7117 "braced list index is not allowed with array "
7118 "notation");
7119 cp_parser_skip_to_end_of_statement (parser);
7120 return error_mark_node;
7123 else if (flag_cilkplus)
7125 /* Here are have these two options:
7126 ARRAY[EXP : EXP] - Array notation expr with default
7127 stride of 1.
7128 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7129 stride. */
7130 tree an_exp = cp_parser_array_notation (loc, parser, &index,
7131 postfix_expression);
7132 if (an_exp)
7133 return an_exp;
7135 else
7136 index = cp_parser_expression (parser);
7139 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7141 /* Look for the closing `]'. */
7142 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7144 /* Build the ARRAY_REF. */
7145 postfix_expression = grok_array_decl (loc, postfix_expression,
7146 index, decltype_p);
7148 /* When not doing offsetof, array references are not permitted in
7149 constant-expressions. */
7150 if (!for_offsetof
7151 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7152 postfix_expression = error_mark_node;
7154 return postfix_expression;
7157 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7158 by cp_parser_builtin_offsetof. We're looking for
7160 postfix-expression . template [opt] id-expression
7161 postfix-expression . pseudo-destructor-name
7162 postfix-expression -> template [opt] id-expression
7163 postfix-expression -> pseudo-destructor-name
7165 FOR_OFFSETOF is set if we're being called in that context. That sorta
7166 limits what of the above we'll actually accept, but nevermind.
7167 TOKEN_TYPE is the "." or "->" token, which will already have been
7168 removed from the stream. */
7170 static tree
7171 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7172 enum cpp_ttype token_type,
7173 cp_expr postfix_expression,
7174 bool for_offsetof, cp_id_kind *idk,
7175 location_t location)
7177 tree name;
7178 bool dependent_p;
7179 bool pseudo_destructor_p;
7180 tree scope = NULL_TREE;
7181 location_t start_loc = postfix_expression.get_start ();
7183 /* If this is a `->' operator, dereference the pointer. */
7184 if (token_type == CPP_DEREF)
7185 postfix_expression = build_x_arrow (location, postfix_expression,
7186 tf_warning_or_error);
7187 /* According to the standard, no expression should ever have
7188 reference type. Unfortunately, we do not currently match
7189 the standard in this respect in that our internal representation
7190 of an expression may have reference type even when the standard
7191 says it does not. Therefore, we have to manually obtain the
7192 underlying type here. */
7193 scope = non_reference (TREE_TYPE (postfix_expression));
7194 /* Check to see whether or not the expression is type-dependent and
7195 not the current instantiation. */
7196 dependent_p = !scope || dependent_scope_p (scope);
7197 /* The identifier following the `->' or `.' is not qualified. */
7198 parser->scope = NULL_TREE;
7199 parser->qualifying_scope = NULL_TREE;
7200 parser->object_scope = NULL_TREE;
7201 *idk = CP_ID_KIND_NONE;
7203 /* Enter the scope corresponding to the type of the object
7204 given by the POSTFIX_EXPRESSION. */
7205 if (!dependent_p)
7207 /* The type of the POSTFIX_EXPRESSION must be complete. */
7208 if (scope == unknown_type_node)
7210 error_at (location, "%qE does not have class type",
7211 postfix_expression.get_value ());
7212 scope = NULL_TREE;
7214 /* Unlike the object expression in other contexts, *this is not
7215 required to be of complete type for purposes of class member
7216 access (5.2.5) outside the member function body. */
7217 else if (postfix_expression != current_class_ref
7218 && !(processing_template_decl
7219 && current_class_type
7220 && (same_type_ignoring_top_level_qualifiers_p
7221 (scope, current_class_type))))
7222 scope = complete_type_or_else (scope, NULL_TREE);
7223 /* Let the name lookup machinery know that we are processing a
7224 class member access expression. */
7225 parser->context->object_type = scope;
7226 /* If something went wrong, we want to be able to discern that case,
7227 as opposed to the case where there was no SCOPE due to the type
7228 of expression being dependent. */
7229 if (!scope)
7230 scope = error_mark_node;
7231 /* If the SCOPE was erroneous, make the various semantic analysis
7232 functions exit quickly -- and without issuing additional error
7233 messages. */
7234 if (scope == error_mark_node)
7235 postfix_expression = error_mark_node;
7237 else
7238 /* Tell cp_parser_lookup_name that there was an object, even though it's
7239 type-dependent. */
7240 parser->context->object_type = unknown_type_node;
7242 /* Assume this expression is not a pseudo-destructor access. */
7243 pseudo_destructor_p = false;
7245 /* If the SCOPE is a scalar type, then, if this is a valid program,
7246 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7247 is type dependent, it can be pseudo-destructor-name or something else.
7248 Try to parse it as pseudo-destructor-name first. */
7249 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7251 tree s;
7252 tree type;
7254 cp_parser_parse_tentatively (parser);
7255 /* Parse the pseudo-destructor-name. */
7256 s = NULL_TREE;
7257 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7258 &s, &type);
7259 if (dependent_p
7260 && (cp_parser_error_occurred (parser)
7261 || !SCALAR_TYPE_P (type)))
7262 cp_parser_abort_tentative_parse (parser);
7263 else if (cp_parser_parse_definitely (parser))
7265 pseudo_destructor_p = true;
7266 postfix_expression
7267 = finish_pseudo_destructor_expr (postfix_expression,
7268 s, type, location);
7272 if (!pseudo_destructor_p)
7274 /* If the SCOPE is not a scalar type, we are looking at an
7275 ordinary class member access expression, rather than a
7276 pseudo-destructor-name. */
7277 bool template_p;
7278 cp_token *token = cp_lexer_peek_token (parser->lexer);
7279 /* Parse the id-expression. */
7280 name = (cp_parser_id_expression
7281 (parser,
7282 cp_parser_optional_template_keyword (parser),
7283 /*check_dependency_p=*/true,
7284 &template_p,
7285 /*declarator_p=*/false,
7286 /*optional_p=*/false));
7287 /* In general, build a SCOPE_REF if the member name is qualified.
7288 However, if the name was not dependent and has already been
7289 resolved; there is no need to build the SCOPE_REF. For example;
7291 struct X { void f(); };
7292 template <typename T> void f(T* t) { t->X::f(); }
7294 Even though "t" is dependent, "X::f" is not and has been resolved
7295 to a BASELINK; there is no need to include scope information. */
7297 /* But we do need to remember that there was an explicit scope for
7298 virtual function calls. */
7299 if (parser->scope)
7300 *idk = CP_ID_KIND_QUALIFIED;
7302 /* If the name is a template-id that names a type, we will get a
7303 TYPE_DECL here. That is invalid code. */
7304 if (TREE_CODE (name) == TYPE_DECL)
7306 error_at (token->location, "invalid use of %qD", name);
7307 postfix_expression = error_mark_node;
7309 else
7311 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7313 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7315 error_at (token->location, "%<%D::%D%> is not a class member",
7316 parser->scope, name);
7317 postfix_expression = error_mark_node;
7319 else
7320 name = build_qualified_name (/*type=*/NULL_TREE,
7321 parser->scope,
7322 name,
7323 template_p);
7324 parser->scope = NULL_TREE;
7325 parser->qualifying_scope = NULL_TREE;
7326 parser->object_scope = NULL_TREE;
7328 if (parser->scope && name && BASELINK_P (name))
7329 adjust_result_of_qualified_name_lookup
7330 (name, parser->scope, scope);
7331 postfix_expression
7332 = finish_class_member_access_expr (postfix_expression, name,
7333 template_p,
7334 tf_warning_or_error);
7335 /* Build a location e.g.:
7336 ptr->access_expr
7337 ~~~^~~~~~~~~~~~~
7338 where the caret is at the deref token, ranging from
7339 the start of postfix_expression to the end of the access expr. */
7340 location_t end_loc
7341 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7342 location_t combined_loc
7343 = make_location (input_location, start_loc, end_loc);
7344 protected_set_expr_location (postfix_expression, combined_loc);
7348 /* We no longer need to look up names in the scope of the object on
7349 the left-hand side of the `.' or `->' operator. */
7350 parser->context->object_type = NULL_TREE;
7352 /* Outside of offsetof, these operators may not appear in
7353 constant-expressions. */
7354 if (!for_offsetof
7355 && (cp_parser_non_integral_constant_expression
7356 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7357 postfix_expression = error_mark_node;
7359 return postfix_expression;
7362 /* Parse a parenthesized expression-list.
7364 expression-list:
7365 assignment-expression
7366 expression-list, assignment-expression
7368 attribute-list:
7369 expression-list
7370 identifier
7371 identifier, expression-list
7373 CAST_P is true if this expression is the target of a cast.
7375 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7376 argument pack.
7378 Returns a vector of trees. Each element is a representation of an
7379 assignment-expression. NULL is returned if the ( and or ) are
7380 missing. An empty, but allocated, vector is returned on no
7381 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7382 if we are parsing an attribute list for an attribute that wants a
7383 plain identifier argument, normal_attr for an attribute that wants
7384 an expression, or non_attr if we aren't parsing an attribute list. If
7385 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7386 not all of the expressions in the list were constant.
7387 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7388 will be written to with the location of the closing parenthesis. If
7389 an error occurs, it may or may not be written to. */
7391 static vec<tree, va_gc> *
7392 cp_parser_parenthesized_expression_list (cp_parser* parser,
7393 int is_attribute_list,
7394 bool cast_p,
7395 bool allow_expansion_p,
7396 bool *non_constant_p,
7397 location_t *close_paren_loc)
7399 vec<tree, va_gc> *expression_list;
7400 bool fold_expr_p = is_attribute_list != non_attr;
7401 tree identifier = NULL_TREE;
7402 bool saved_greater_than_is_operator_p;
7404 /* Assume all the expressions will be constant. */
7405 if (non_constant_p)
7406 *non_constant_p = false;
7408 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
7409 return NULL;
7411 expression_list = make_tree_vector ();
7413 /* Within a parenthesized expression, a `>' token is always
7414 the greater-than operator. */
7415 saved_greater_than_is_operator_p
7416 = parser->greater_than_is_operator_p;
7417 parser->greater_than_is_operator_p = true;
7419 /* Consume expressions until there are no more. */
7420 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7421 while (true)
7423 tree expr;
7425 /* At the beginning of attribute lists, check to see if the
7426 next token is an identifier. */
7427 if (is_attribute_list == id_attr
7428 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7430 cp_token *token;
7432 /* Consume the identifier. */
7433 token = cp_lexer_consume_token (parser->lexer);
7434 /* Save the identifier. */
7435 identifier = token->u.value;
7437 else
7439 bool expr_non_constant_p;
7441 /* Parse the next assignment-expression. */
7442 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7444 /* A braced-init-list. */
7445 cp_lexer_set_source_position (parser->lexer);
7446 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7447 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7448 if (non_constant_p && expr_non_constant_p)
7449 *non_constant_p = true;
7451 else if (non_constant_p)
7453 expr = (cp_parser_constant_expression
7454 (parser, /*allow_non_constant_p=*/true,
7455 &expr_non_constant_p));
7456 if (expr_non_constant_p)
7457 *non_constant_p = true;
7459 else
7460 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7461 cast_p);
7463 if (fold_expr_p)
7464 expr = instantiate_non_dependent_expr (expr);
7466 /* If we have an ellipsis, then this is an expression
7467 expansion. */
7468 if (allow_expansion_p
7469 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7471 /* Consume the `...'. */
7472 cp_lexer_consume_token (parser->lexer);
7474 /* Build the argument pack. */
7475 expr = make_pack_expansion (expr);
7478 /* Add it to the list. We add error_mark_node
7479 expressions to the list, so that we can still tell if
7480 the correct form for a parenthesized expression-list
7481 is found. That gives better errors. */
7482 vec_safe_push (expression_list, expr);
7484 if (expr == error_mark_node)
7485 goto skip_comma;
7488 /* After the first item, attribute lists look the same as
7489 expression lists. */
7490 is_attribute_list = non_attr;
7492 get_comma:;
7493 /* If the next token isn't a `,', then we are done. */
7494 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7495 break;
7497 /* Otherwise, consume the `,' and keep going. */
7498 cp_lexer_consume_token (parser->lexer);
7501 if (close_paren_loc)
7502 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7504 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7506 int ending;
7508 skip_comma:;
7509 /* We try and resync to an unnested comma, as that will give the
7510 user better diagnostics. */
7511 ending = cp_parser_skip_to_closing_parenthesis (parser,
7512 /*recovering=*/true,
7513 /*or_comma=*/true,
7514 /*consume_paren=*/true);
7515 if (ending < 0)
7516 goto get_comma;
7517 if (!ending)
7519 parser->greater_than_is_operator_p
7520 = saved_greater_than_is_operator_p;
7521 return NULL;
7525 parser->greater_than_is_operator_p
7526 = saved_greater_than_is_operator_p;
7528 if (identifier)
7529 vec_safe_insert (expression_list, 0, identifier);
7531 return expression_list;
7534 /* Parse a pseudo-destructor-name.
7536 pseudo-destructor-name:
7537 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7538 :: [opt] nested-name-specifier template template-id :: ~ type-name
7539 :: [opt] nested-name-specifier [opt] ~ type-name
7541 If either of the first two productions is used, sets *SCOPE to the
7542 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7543 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7544 or ERROR_MARK_NODE if the parse fails. */
7546 static void
7547 cp_parser_pseudo_destructor_name (cp_parser* parser,
7548 tree object,
7549 tree* scope,
7550 tree* type)
7552 bool nested_name_specifier_p;
7554 /* Handle ~auto. */
7555 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7556 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7557 && !type_dependent_expression_p (object))
7559 if (cxx_dialect < cxx14)
7560 pedwarn (input_location, 0,
7561 "%<~auto%> only available with "
7562 "-std=c++14 or -std=gnu++14");
7563 cp_lexer_consume_token (parser->lexer);
7564 cp_lexer_consume_token (parser->lexer);
7565 *scope = NULL_TREE;
7566 *type = TREE_TYPE (object);
7567 return;
7570 /* Assume that things will not work out. */
7571 *type = error_mark_node;
7573 /* Look for the optional `::' operator. */
7574 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7575 /* Look for the optional nested-name-specifier. */
7576 nested_name_specifier_p
7577 = (cp_parser_nested_name_specifier_opt (parser,
7578 /*typename_keyword_p=*/false,
7579 /*check_dependency_p=*/true,
7580 /*type_p=*/false,
7581 /*is_declaration=*/false)
7582 != NULL_TREE);
7583 /* Now, if we saw a nested-name-specifier, we might be doing the
7584 second production. */
7585 if (nested_name_specifier_p
7586 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7588 /* Consume the `template' keyword. */
7589 cp_lexer_consume_token (parser->lexer);
7590 /* Parse the template-id. */
7591 cp_parser_template_id (parser,
7592 /*template_keyword_p=*/true,
7593 /*check_dependency_p=*/false,
7594 class_type,
7595 /*is_declaration=*/true);
7596 /* Look for the `::' token. */
7597 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7599 /* If the next token is not a `~', then there might be some
7600 additional qualification. */
7601 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7603 /* At this point, we're looking for "type-name :: ~". The type-name
7604 must not be a class-name, since this is a pseudo-destructor. So,
7605 it must be either an enum-name, or a typedef-name -- both of which
7606 are just identifiers. So, we peek ahead to check that the "::"
7607 and "~" tokens are present; if they are not, then we can avoid
7608 calling type_name. */
7609 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7610 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7611 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7613 cp_parser_error (parser, "non-scalar type");
7614 return;
7617 /* Look for the type-name. */
7618 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7619 if (*scope == error_mark_node)
7620 return;
7622 /* Look for the `::' token. */
7623 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7625 else
7626 *scope = NULL_TREE;
7628 /* Look for the `~'. */
7629 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7631 /* Once we see the ~, this has to be a pseudo-destructor. */
7632 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7633 cp_parser_commit_to_topmost_tentative_parse (parser);
7635 /* Look for the type-name again. We are not responsible for
7636 checking that it matches the first type-name. */
7637 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7640 /* Parse a unary-expression.
7642 unary-expression:
7643 postfix-expression
7644 ++ cast-expression
7645 -- cast-expression
7646 unary-operator cast-expression
7647 sizeof unary-expression
7648 sizeof ( type-id )
7649 alignof ( type-id ) [C++0x]
7650 new-expression
7651 delete-expression
7653 GNU Extensions:
7655 unary-expression:
7656 __extension__ cast-expression
7657 __alignof__ unary-expression
7658 __alignof__ ( type-id )
7659 alignof unary-expression [C++0x]
7660 __real__ cast-expression
7661 __imag__ cast-expression
7662 && identifier
7663 sizeof ( type-id ) { initializer-list , [opt] }
7664 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7665 __alignof__ ( type-id ) { initializer-list , [opt] }
7667 ADDRESS_P is true iff the unary-expression is appearing as the
7668 operand of the `&' operator. CAST_P is true if this expression is
7669 the target of a cast.
7671 Returns a representation of the expression. */
7673 static cp_expr
7674 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7675 bool address_p, bool cast_p, bool decltype_p)
7677 cp_token *token;
7678 enum tree_code unary_operator;
7680 /* Peek at the next token. */
7681 token = cp_lexer_peek_token (parser->lexer);
7682 /* Some keywords give away the kind of expression. */
7683 if (token->type == CPP_KEYWORD)
7685 enum rid keyword = token->keyword;
7687 switch (keyword)
7689 case RID_ALIGNOF:
7690 case RID_SIZEOF:
7692 tree operand, ret;
7693 enum tree_code op;
7694 location_t first_loc;
7696 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7697 /* Consume the token. */
7698 cp_lexer_consume_token (parser->lexer);
7699 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7700 /* Parse the operand. */
7701 operand = cp_parser_sizeof_operand (parser, keyword);
7703 if (TYPE_P (operand))
7704 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7705 else
7707 /* ISO C++ defines alignof only with types, not with
7708 expressions. So pedwarn if alignof is used with a non-
7709 type expression. However, __alignof__ is ok. */
7710 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7711 pedwarn (token->location, OPT_Wpedantic,
7712 "ISO C++ does not allow %<alignof%> "
7713 "with a non-type");
7715 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7717 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7718 SIZEOF_EXPR with the original operand. */
7719 if (op == SIZEOF_EXPR && ret != error_mark_node)
7721 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7723 if (!processing_template_decl && TYPE_P (operand))
7725 ret = build_min (SIZEOF_EXPR, size_type_node,
7726 build1 (NOP_EXPR, operand,
7727 error_mark_node));
7728 SIZEOF_EXPR_TYPE_P (ret) = 1;
7730 else
7731 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7732 TREE_SIDE_EFFECTS (ret) = 0;
7733 TREE_READONLY (ret) = 1;
7735 SET_EXPR_LOCATION (ret, first_loc);
7737 return ret;
7740 case RID_NEW:
7741 return cp_parser_new_expression (parser);
7743 case RID_DELETE:
7744 return cp_parser_delete_expression (parser);
7746 case RID_EXTENSION:
7748 /* The saved value of the PEDANTIC flag. */
7749 int saved_pedantic;
7750 tree expr;
7752 /* Save away the PEDANTIC flag. */
7753 cp_parser_extension_opt (parser, &saved_pedantic);
7754 /* Parse the cast-expression. */
7755 expr = cp_parser_simple_cast_expression (parser);
7756 /* Restore the PEDANTIC flag. */
7757 pedantic = saved_pedantic;
7759 return expr;
7762 case RID_REALPART:
7763 case RID_IMAGPART:
7765 tree expression;
7767 /* Consume the `__real__' or `__imag__' token. */
7768 cp_lexer_consume_token (parser->lexer);
7769 /* Parse the cast-expression. */
7770 expression = cp_parser_simple_cast_expression (parser);
7771 /* Create the complete representation. */
7772 return build_x_unary_op (token->location,
7773 (keyword == RID_REALPART
7774 ? REALPART_EXPR : IMAGPART_EXPR),
7775 expression,
7776 tf_warning_or_error);
7778 break;
7780 case RID_TRANSACTION_ATOMIC:
7781 case RID_TRANSACTION_RELAXED:
7782 return cp_parser_transaction_expression (parser, keyword);
7784 case RID_NOEXCEPT:
7786 tree expr;
7787 const char *saved_message;
7788 bool saved_integral_constant_expression_p;
7789 bool saved_non_integral_constant_expression_p;
7790 bool saved_greater_than_is_operator_p;
7792 cp_lexer_consume_token (parser->lexer);
7793 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7795 saved_message = parser->type_definition_forbidden_message;
7796 parser->type_definition_forbidden_message
7797 = G_("types may not be defined in %<noexcept%> expressions");
7799 saved_integral_constant_expression_p
7800 = parser->integral_constant_expression_p;
7801 saved_non_integral_constant_expression_p
7802 = parser->non_integral_constant_expression_p;
7803 parser->integral_constant_expression_p = false;
7805 saved_greater_than_is_operator_p
7806 = parser->greater_than_is_operator_p;
7807 parser->greater_than_is_operator_p = true;
7809 ++cp_unevaluated_operand;
7810 ++c_inhibit_evaluation_warnings;
7811 ++cp_noexcept_operand;
7812 expr = cp_parser_expression (parser);
7813 --cp_noexcept_operand;
7814 --c_inhibit_evaluation_warnings;
7815 --cp_unevaluated_operand;
7817 parser->greater_than_is_operator_p
7818 = saved_greater_than_is_operator_p;
7820 parser->integral_constant_expression_p
7821 = saved_integral_constant_expression_p;
7822 parser->non_integral_constant_expression_p
7823 = saved_non_integral_constant_expression_p;
7825 parser->type_definition_forbidden_message = saved_message;
7827 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7828 return finish_noexcept_expr (expr, tf_warning_or_error);
7831 default:
7832 break;
7836 /* Look for the `:: new' and `:: delete', which also signal the
7837 beginning of a new-expression, or delete-expression,
7838 respectively. If the next token is `::', then it might be one of
7839 these. */
7840 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7842 enum rid keyword;
7844 /* See if the token after the `::' is one of the keywords in
7845 which we're interested. */
7846 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7847 /* If it's `new', we have a new-expression. */
7848 if (keyword == RID_NEW)
7849 return cp_parser_new_expression (parser);
7850 /* Similarly, for `delete'. */
7851 else if (keyword == RID_DELETE)
7852 return cp_parser_delete_expression (parser);
7855 /* Look for a unary operator. */
7856 unary_operator = cp_parser_unary_operator (token);
7857 /* The `++' and `--' operators can be handled similarly, even though
7858 they are not technically unary-operators in the grammar. */
7859 if (unary_operator == ERROR_MARK)
7861 if (token->type == CPP_PLUS_PLUS)
7862 unary_operator = PREINCREMENT_EXPR;
7863 else if (token->type == CPP_MINUS_MINUS)
7864 unary_operator = PREDECREMENT_EXPR;
7865 /* Handle the GNU address-of-label extension. */
7866 else if (cp_parser_allow_gnu_extensions_p (parser)
7867 && token->type == CPP_AND_AND)
7869 tree identifier;
7870 tree expression;
7871 location_t start_loc = token->location;
7873 /* Consume the '&&' token. */
7874 cp_lexer_consume_token (parser->lexer);
7875 /* Look for the identifier. */
7876 location_t finish_loc
7877 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
7878 identifier = cp_parser_identifier (parser);
7879 /* Construct a location of the form:
7880 &&label
7881 ^~~~~~~
7882 with caret==start at the "&&", finish at the end of the label. */
7883 location_t combined_loc
7884 = make_location (start_loc, start_loc, finish_loc);
7885 /* Create an expression representing the address. */
7886 expression = finish_label_address_expr (identifier, combined_loc);
7887 if (cp_parser_non_integral_constant_expression (parser,
7888 NIC_ADDR_LABEL))
7889 expression = error_mark_node;
7890 return expression;
7893 if (unary_operator != ERROR_MARK)
7895 cp_expr cast_expression;
7896 cp_expr expression = error_mark_node;
7897 non_integral_constant non_constant_p = NIC_NONE;
7898 location_t loc = token->location;
7899 tsubst_flags_t complain = complain_flags (decltype_p);
7901 /* Consume the operator token. */
7902 token = cp_lexer_consume_token (parser->lexer);
7903 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
7905 /* Parse the cast-expression. */
7906 cast_expression
7907 = cp_parser_cast_expression (parser,
7908 unary_operator == ADDR_EXPR,
7909 /*cast_p=*/false,
7910 /*decltype*/false,
7911 pidk);
7913 /* Make a location:
7914 OP_TOKEN CAST_EXPRESSION
7915 ^~~~~~~~~~~~~~~~~~~~~~~~~
7916 with start==caret at the operator token, and
7917 extending to the end of the cast_expression. */
7918 loc = make_location (loc, loc, cast_expression.get_finish ());
7920 /* Now, build an appropriate representation. */
7921 switch (unary_operator)
7923 case INDIRECT_REF:
7924 non_constant_p = NIC_STAR;
7925 expression = build_x_indirect_ref (loc, cast_expression,
7926 RO_UNARY_STAR,
7927 complain);
7928 /* TODO: build_x_indirect_ref does not always honor the
7929 location, so ensure it is set. */
7930 expression.set_location (loc);
7931 break;
7933 case ADDR_EXPR:
7934 non_constant_p = NIC_ADDR;
7935 /* Fall through. */
7936 case BIT_NOT_EXPR:
7937 expression = build_x_unary_op (loc, unary_operator,
7938 cast_expression,
7939 complain);
7940 /* TODO: build_x_unary_op does not always honor the location,
7941 so ensure it is set. */
7942 expression.set_location (loc);
7943 break;
7945 case PREINCREMENT_EXPR:
7946 case PREDECREMENT_EXPR:
7947 non_constant_p = unary_operator == PREINCREMENT_EXPR
7948 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7949 /* Fall through. */
7950 case NEGATE_EXPR:
7951 /* Immediately fold negation of a constant, unless the constant is 0
7952 (since -0 == 0) or it would overflow. */
7953 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
7954 && CONSTANT_CLASS_P (cast_expression)
7955 && !integer_zerop (cast_expression)
7956 && !TREE_OVERFLOW (cast_expression))
7958 tree folded = fold_build1 (unary_operator,
7959 TREE_TYPE (cast_expression),
7960 cast_expression);
7961 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
7963 expression = cp_expr (folded, loc);
7964 break;
7967 /* Fall through. */
7968 case UNARY_PLUS_EXPR:
7969 case TRUTH_NOT_EXPR:
7970 expression = finish_unary_op_expr (loc, unary_operator,
7971 cast_expression, complain);
7972 break;
7974 default:
7975 gcc_unreachable ();
7978 if (non_constant_p != NIC_NONE
7979 && cp_parser_non_integral_constant_expression (parser,
7980 non_constant_p))
7981 expression = error_mark_node;
7983 return expression;
7986 return cp_parser_postfix_expression (parser, address_p, cast_p,
7987 /*member_access_only_p=*/false,
7988 decltype_p,
7989 pidk);
7992 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7993 unary-operator, the corresponding tree code is returned. */
7995 static enum tree_code
7996 cp_parser_unary_operator (cp_token* token)
7998 switch (token->type)
8000 case CPP_MULT:
8001 return INDIRECT_REF;
8003 case CPP_AND:
8004 return ADDR_EXPR;
8006 case CPP_PLUS:
8007 return UNARY_PLUS_EXPR;
8009 case CPP_MINUS:
8010 return NEGATE_EXPR;
8012 case CPP_NOT:
8013 return TRUTH_NOT_EXPR;
8015 case CPP_COMPL:
8016 return BIT_NOT_EXPR;
8018 default:
8019 return ERROR_MARK;
8023 /* Parse a new-expression.
8025 new-expression:
8026 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8027 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8029 Returns a representation of the expression. */
8031 static tree
8032 cp_parser_new_expression (cp_parser* parser)
8034 bool global_scope_p;
8035 vec<tree, va_gc> *placement;
8036 tree type;
8037 vec<tree, va_gc> *initializer;
8038 tree nelts = NULL_TREE;
8039 tree ret;
8041 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8043 /* Look for the optional `::' operator. */
8044 global_scope_p
8045 = (cp_parser_global_scope_opt (parser,
8046 /*current_scope_valid_p=*/false)
8047 != NULL_TREE);
8048 /* Look for the `new' operator. */
8049 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8050 /* There's no easy way to tell a new-placement from the
8051 `( type-id )' construct. */
8052 cp_parser_parse_tentatively (parser);
8053 /* Look for a new-placement. */
8054 placement = cp_parser_new_placement (parser);
8055 /* If that didn't work out, there's no new-placement. */
8056 if (!cp_parser_parse_definitely (parser))
8058 if (placement != NULL)
8059 release_tree_vector (placement);
8060 placement = NULL;
8063 /* If the next token is a `(', then we have a parenthesized
8064 type-id. */
8065 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8067 cp_token *token;
8068 const char *saved_message = parser->type_definition_forbidden_message;
8070 /* Consume the `('. */
8071 cp_lexer_consume_token (parser->lexer);
8073 /* Parse the type-id. */
8074 parser->type_definition_forbidden_message
8075 = G_("types may not be defined in a new-expression");
8077 type_id_in_expr_sentinel s (parser);
8078 type = cp_parser_type_id (parser);
8080 parser->type_definition_forbidden_message = saved_message;
8082 /* Look for the closing `)'. */
8083 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8084 token = cp_lexer_peek_token (parser->lexer);
8085 /* There should not be a direct-new-declarator in this production,
8086 but GCC used to allowed this, so we check and emit a sensible error
8087 message for this case. */
8088 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8090 error_at (token->location,
8091 "array bound forbidden after parenthesized type-id");
8092 inform (token->location,
8093 "try removing the parentheses around the type-id");
8094 cp_parser_direct_new_declarator (parser);
8097 /* Otherwise, there must be a new-type-id. */
8098 else
8099 type = cp_parser_new_type_id (parser, &nelts);
8101 /* If the next token is a `(' or '{', then we have a new-initializer. */
8102 cp_token *token = cp_lexer_peek_token (parser->lexer);
8103 if (token->type == CPP_OPEN_PAREN
8104 || token->type == CPP_OPEN_BRACE)
8105 initializer = cp_parser_new_initializer (parser);
8106 else
8107 initializer = NULL;
8109 /* A new-expression may not appear in an integral constant
8110 expression. */
8111 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8112 ret = error_mark_node;
8113 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8114 of a new-type-id or type-id of a new-expression, the new-expression shall
8115 contain a new-initializer of the form ( assignment-expression )".
8116 Additionally, consistently with the spirit of DR 1467, we want to accept
8117 'new auto { 2 }' too. */
8118 else if (type_uses_auto (type)
8119 && (vec_safe_length (initializer) != 1
8120 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8121 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8123 error_at (token->location,
8124 "initialization of new-expression for type %<auto%> "
8125 "requires exactly one element");
8126 ret = error_mark_node;
8128 else
8130 /* Construct a location e.g.:
8131 ptr = new int[100]
8132 ^~~~~~~~~~~~
8133 with caret == start at the start of the "new" token, and the end
8134 at the end of the final token we consumed. */
8135 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8136 location_t end_loc = get_finish (end_tok->location);
8137 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8139 /* Create a representation of the new-expression. */
8140 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8141 tf_warning_or_error);
8142 protected_set_expr_location (ret, combined_loc);
8145 if (placement != NULL)
8146 release_tree_vector (placement);
8147 if (initializer != NULL)
8148 release_tree_vector (initializer);
8150 return ret;
8153 /* Parse a new-placement.
8155 new-placement:
8156 ( expression-list )
8158 Returns the same representation as for an expression-list. */
8160 static vec<tree, va_gc> *
8161 cp_parser_new_placement (cp_parser* parser)
8163 vec<tree, va_gc> *expression_list;
8165 /* Parse the expression-list. */
8166 expression_list = (cp_parser_parenthesized_expression_list
8167 (parser, non_attr, /*cast_p=*/false,
8168 /*allow_expansion_p=*/true,
8169 /*non_constant_p=*/NULL));
8171 if (expression_list && expression_list->is_empty ())
8172 error ("expected expression-list or type-id");
8174 return expression_list;
8177 /* Parse a new-type-id.
8179 new-type-id:
8180 type-specifier-seq new-declarator [opt]
8182 Returns the TYPE allocated. If the new-type-id indicates an array
8183 type, *NELTS is set to the number of elements in the last array
8184 bound; the TYPE will not include the last array bound. */
8186 static tree
8187 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8189 cp_decl_specifier_seq type_specifier_seq;
8190 cp_declarator *new_declarator;
8191 cp_declarator *declarator;
8192 cp_declarator *outer_declarator;
8193 const char *saved_message;
8195 /* The type-specifier sequence must not contain type definitions.
8196 (It cannot contain declarations of new types either, but if they
8197 are not definitions we will catch that because they are not
8198 complete.) */
8199 saved_message = parser->type_definition_forbidden_message;
8200 parser->type_definition_forbidden_message
8201 = G_("types may not be defined in a new-type-id");
8202 /* Parse the type-specifier-seq. */
8203 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8204 /*is_trailing_return=*/false,
8205 &type_specifier_seq);
8206 /* Restore the old message. */
8207 parser->type_definition_forbidden_message = saved_message;
8209 if (type_specifier_seq.type == error_mark_node)
8210 return error_mark_node;
8212 /* Parse the new-declarator. */
8213 new_declarator = cp_parser_new_declarator_opt (parser);
8215 /* Determine the number of elements in the last array dimension, if
8216 any. */
8217 *nelts = NULL_TREE;
8218 /* Skip down to the last array dimension. */
8219 declarator = new_declarator;
8220 outer_declarator = NULL;
8221 while (declarator && (declarator->kind == cdk_pointer
8222 || declarator->kind == cdk_ptrmem))
8224 outer_declarator = declarator;
8225 declarator = declarator->declarator;
8227 while (declarator
8228 && declarator->kind == cdk_array
8229 && declarator->declarator
8230 && declarator->declarator->kind == cdk_array)
8232 outer_declarator = declarator;
8233 declarator = declarator->declarator;
8236 if (declarator && declarator->kind == cdk_array)
8238 *nelts = declarator->u.array.bounds;
8239 if (*nelts == error_mark_node)
8240 *nelts = integer_one_node;
8242 if (outer_declarator)
8243 outer_declarator->declarator = declarator->declarator;
8244 else
8245 new_declarator = NULL;
8248 return groktypename (&type_specifier_seq, new_declarator, false);
8251 /* Parse an (optional) new-declarator.
8253 new-declarator:
8254 ptr-operator new-declarator [opt]
8255 direct-new-declarator
8257 Returns the declarator. */
8259 static cp_declarator *
8260 cp_parser_new_declarator_opt (cp_parser* parser)
8262 enum tree_code code;
8263 tree type, std_attributes = NULL_TREE;
8264 cp_cv_quals cv_quals;
8266 /* We don't know if there's a ptr-operator next, or not. */
8267 cp_parser_parse_tentatively (parser);
8268 /* Look for a ptr-operator. */
8269 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8270 /* If that worked, look for more new-declarators. */
8271 if (cp_parser_parse_definitely (parser))
8273 cp_declarator *declarator;
8275 /* Parse another optional declarator. */
8276 declarator = cp_parser_new_declarator_opt (parser);
8278 declarator = cp_parser_make_indirect_declarator
8279 (code, type, cv_quals, declarator, std_attributes);
8281 return declarator;
8284 /* If the next token is a `[', there is a direct-new-declarator. */
8285 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8286 return cp_parser_direct_new_declarator (parser);
8288 return NULL;
8291 /* Parse a direct-new-declarator.
8293 direct-new-declarator:
8294 [ expression ]
8295 direct-new-declarator [constant-expression]
8299 static cp_declarator *
8300 cp_parser_direct_new_declarator (cp_parser* parser)
8302 cp_declarator *declarator = NULL;
8304 while (true)
8306 tree expression;
8307 cp_token *token;
8309 /* Look for the opening `['. */
8310 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8312 token = cp_lexer_peek_token (parser->lexer);
8313 expression = cp_parser_expression (parser);
8314 /* The standard requires that the expression have integral
8315 type. DR 74 adds enumeration types. We believe that the
8316 real intent is that these expressions be handled like the
8317 expression in a `switch' condition, which also allows
8318 classes with a single conversion to integral or
8319 enumeration type. */
8320 if (!processing_template_decl)
8322 expression
8323 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8324 expression,
8325 /*complain=*/true);
8326 if (!expression)
8328 error_at (token->location,
8329 "expression in new-declarator must have integral "
8330 "or enumeration type");
8331 expression = error_mark_node;
8335 /* Look for the closing `]'. */
8336 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8338 /* Add this bound to the declarator. */
8339 declarator = make_array_declarator (declarator, expression);
8341 /* If the next token is not a `[', then there are no more
8342 bounds. */
8343 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8344 break;
8347 return declarator;
8350 /* Parse a new-initializer.
8352 new-initializer:
8353 ( expression-list [opt] )
8354 braced-init-list
8356 Returns a representation of the expression-list. */
8358 static vec<tree, va_gc> *
8359 cp_parser_new_initializer (cp_parser* parser)
8361 vec<tree, va_gc> *expression_list;
8363 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8365 tree t;
8366 bool expr_non_constant_p;
8367 cp_lexer_set_source_position (parser->lexer);
8368 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8369 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8370 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8371 expression_list = make_tree_vector_single (t);
8373 else
8374 expression_list = (cp_parser_parenthesized_expression_list
8375 (parser, non_attr, /*cast_p=*/false,
8376 /*allow_expansion_p=*/true,
8377 /*non_constant_p=*/NULL));
8379 return expression_list;
8382 /* Parse a delete-expression.
8384 delete-expression:
8385 :: [opt] delete cast-expression
8386 :: [opt] delete [ ] cast-expression
8388 Returns a representation of the expression. */
8390 static tree
8391 cp_parser_delete_expression (cp_parser* parser)
8393 bool global_scope_p;
8394 bool array_p;
8395 tree expression;
8397 /* Look for the optional `::' operator. */
8398 global_scope_p
8399 = (cp_parser_global_scope_opt (parser,
8400 /*current_scope_valid_p=*/false)
8401 != NULL_TREE);
8402 /* Look for the `delete' keyword. */
8403 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8404 /* See if the array syntax is in use. */
8405 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8407 /* Consume the `[' token. */
8408 cp_lexer_consume_token (parser->lexer);
8409 /* Look for the `]' token. */
8410 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8411 /* Remember that this is the `[]' construct. */
8412 array_p = true;
8414 else
8415 array_p = false;
8417 /* Parse the cast-expression. */
8418 expression = cp_parser_simple_cast_expression (parser);
8420 /* A delete-expression may not appear in an integral constant
8421 expression. */
8422 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8423 return error_mark_node;
8425 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8426 tf_warning_or_error);
8429 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8430 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8431 0 otherwise. */
8433 static int
8434 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8436 cp_token *token = cp_lexer_peek_token (parser->lexer);
8437 switch (token->type)
8439 case CPP_COMMA:
8440 case CPP_SEMICOLON:
8441 case CPP_QUERY:
8442 case CPP_COLON:
8443 case CPP_CLOSE_SQUARE:
8444 case CPP_CLOSE_PAREN:
8445 case CPP_CLOSE_BRACE:
8446 case CPP_OPEN_BRACE:
8447 case CPP_DOT:
8448 case CPP_DOT_STAR:
8449 case CPP_DEREF:
8450 case CPP_DEREF_STAR:
8451 case CPP_DIV:
8452 case CPP_MOD:
8453 case CPP_LSHIFT:
8454 case CPP_RSHIFT:
8455 case CPP_LESS:
8456 case CPP_GREATER:
8457 case CPP_LESS_EQ:
8458 case CPP_GREATER_EQ:
8459 case CPP_EQ_EQ:
8460 case CPP_NOT_EQ:
8461 case CPP_EQ:
8462 case CPP_MULT_EQ:
8463 case CPP_DIV_EQ:
8464 case CPP_MOD_EQ:
8465 case CPP_PLUS_EQ:
8466 case CPP_MINUS_EQ:
8467 case CPP_RSHIFT_EQ:
8468 case CPP_LSHIFT_EQ:
8469 case CPP_AND_EQ:
8470 case CPP_XOR_EQ:
8471 case CPP_OR_EQ:
8472 case CPP_XOR:
8473 case CPP_OR:
8474 case CPP_OR_OR:
8475 case CPP_EOF:
8476 case CPP_ELLIPSIS:
8477 return 0;
8479 case CPP_OPEN_PAREN:
8480 /* In ((type ()) () the last () isn't a valid cast-expression,
8481 so the whole must be parsed as postfix-expression. */
8482 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8483 != CPP_CLOSE_PAREN;
8485 case CPP_OPEN_SQUARE:
8486 /* '[' may start a primary-expression in obj-c++ and in C++11,
8487 as a lambda-expression, eg, '(void)[]{}'. */
8488 if (cxx_dialect >= cxx11)
8489 return -1;
8490 return c_dialect_objc ();
8492 case CPP_PLUS_PLUS:
8493 case CPP_MINUS_MINUS:
8494 /* '++' and '--' may or may not start a cast-expression:
8496 struct T { void operator++(int); };
8497 void f() { (T())++; }
8501 int a;
8502 (int)++a; */
8503 return -1;
8505 default:
8506 return 1;
8510 /* Parse a cast-expression.
8512 cast-expression:
8513 unary-expression
8514 ( type-id ) cast-expression
8516 ADDRESS_P is true iff the unary-expression is appearing as the
8517 operand of the `&' operator. CAST_P is true if this expression is
8518 the target of a cast.
8520 Returns a representation of the expression. */
8522 static cp_expr
8523 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8524 bool decltype_p, cp_id_kind * pidk)
8526 /* If it's a `(', then we might be looking at a cast. */
8527 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8529 tree type = NULL_TREE;
8530 cp_expr expr (NULL_TREE);
8531 int cast_expression = 0;
8532 const char *saved_message;
8534 /* There's no way to know yet whether or not this is a cast.
8535 For example, `(int (3))' is a unary-expression, while `(int)
8536 3' is a cast. So, we resort to parsing tentatively. */
8537 cp_parser_parse_tentatively (parser);
8538 /* Types may not be defined in a cast. */
8539 saved_message = parser->type_definition_forbidden_message;
8540 parser->type_definition_forbidden_message
8541 = G_("types may not be defined in casts");
8542 /* Consume the `('. */
8543 cp_token *open_paren = cp_lexer_consume_token (parser->lexer);
8544 location_t open_paren_loc = open_paren->location;
8546 /* A very tricky bit is that `(struct S) { 3 }' is a
8547 compound-literal (which we permit in C++ as an extension).
8548 But, that construct is not a cast-expression -- it is a
8549 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8550 is legal; if the compound-literal were a cast-expression,
8551 you'd need an extra set of parentheses.) But, if we parse
8552 the type-id, and it happens to be a class-specifier, then we
8553 will commit to the parse at that point, because we cannot
8554 undo the action that is done when creating a new class. So,
8555 then we cannot back up and do a postfix-expression.
8557 Another tricky case is the following (c++/29234):
8559 struct S { void operator () (); };
8561 void foo ()
8563 ( S()() );
8566 As a type-id we parse the parenthesized S()() as a function
8567 returning a function, groktypename complains and we cannot
8568 back up in this case either.
8570 Therefore, we scan ahead to the closing `)', and check to see
8571 if the tokens after the `)' can start a cast-expression. Otherwise
8572 we are dealing with an unary-expression, a postfix-expression
8573 or something else.
8575 Yet another tricky case, in C++11, is the following (c++/54891):
8577 (void)[]{};
8579 The issue is that usually, besides the case of lambda-expressions,
8580 the parenthesized type-id cannot be followed by '[', and, eg, we
8581 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8582 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8583 we don't commit, we try a cast-expression, then an unary-expression.
8585 Save tokens so that we can put them back. */
8586 cp_lexer_save_tokens (parser->lexer);
8588 /* We may be looking at a cast-expression. */
8589 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8590 /*consume_paren=*/true))
8591 cast_expression
8592 = cp_parser_tokens_start_cast_expression (parser);
8594 /* Roll back the tokens we skipped. */
8595 cp_lexer_rollback_tokens (parser->lexer);
8596 /* If we aren't looking at a cast-expression, simulate an error so
8597 that the call to cp_parser_error_occurred below returns true. */
8598 if (!cast_expression)
8599 cp_parser_simulate_error (parser);
8600 else
8602 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8603 parser->in_type_id_in_expr_p = true;
8604 /* Look for the type-id. */
8605 type = cp_parser_type_id (parser);
8606 /* Look for the closing `)'. */
8607 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8608 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8611 /* Restore the saved message. */
8612 parser->type_definition_forbidden_message = saved_message;
8614 /* At this point this can only be either a cast or a
8615 parenthesized ctor such as `(T ())' that looks like a cast to
8616 function returning T. */
8617 if (!cp_parser_error_occurred (parser))
8619 /* Only commit if the cast-expression doesn't start with
8620 '++', '--', or '[' in C++11. */
8621 if (cast_expression > 0)
8622 cp_parser_commit_to_topmost_tentative_parse (parser);
8624 expr = cp_parser_cast_expression (parser,
8625 /*address_p=*/false,
8626 /*cast_p=*/true,
8627 /*decltype_p=*/false,
8628 pidk);
8630 if (cp_parser_parse_definitely (parser))
8632 /* Warn about old-style casts, if so requested. */
8633 if (warn_old_style_cast
8634 && !in_system_header_at (input_location)
8635 && !VOID_TYPE_P (type)
8636 && current_lang_name != lang_name_c)
8637 warning (OPT_Wold_style_cast, "use of old-style cast");
8639 /* Only type conversions to integral or enumeration types
8640 can be used in constant-expressions. */
8641 if (!cast_valid_in_integral_constant_expression_p (type)
8642 && cp_parser_non_integral_constant_expression (parser,
8643 NIC_CAST))
8644 return error_mark_node;
8646 /* Perform the cast. */
8647 /* Make a location:
8648 (TYPE) EXPR
8649 ^~~~~~~~~~~
8650 with start==caret at the open paren, extending to the
8651 end of "expr". */
8652 location_t cast_loc = make_location (open_paren_loc,
8653 open_paren_loc,
8654 expr.get_finish ());
8655 expr = build_c_cast (cast_loc, type, expr);
8656 return expr;
8659 else
8660 cp_parser_abort_tentative_parse (parser);
8663 /* If we get here, then it's not a cast, so it must be a
8664 unary-expression. */
8665 return cp_parser_unary_expression (parser, pidk, address_p,
8666 cast_p, decltype_p);
8669 /* Parse a binary expression of the general form:
8671 pm-expression:
8672 cast-expression
8673 pm-expression .* cast-expression
8674 pm-expression ->* cast-expression
8676 multiplicative-expression:
8677 pm-expression
8678 multiplicative-expression * pm-expression
8679 multiplicative-expression / pm-expression
8680 multiplicative-expression % pm-expression
8682 additive-expression:
8683 multiplicative-expression
8684 additive-expression + multiplicative-expression
8685 additive-expression - multiplicative-expression
8687 shift-expression:
8688 additive-expression
8689 shift-expression << additive-expression
8690 shift-expression >> additive-expression
8692 relational-expression:
8693 shift-expression
8694 relational-expression < shift-expression
8695 relational-expression > shift-expression
8696 relational-expression <= shift-expression
8697 relational-expression >= shift-expression
8699 GNU Extension:
8701 relational-expression:
8702 relational-expression <? shift-expression
8703 relational-expression >? shift-expression
8705 equality-expression:
8706 relational-expression
8707 equality-expression == relational-expression
8708 equality-expression != relational-expression
8710 and-expression:
8711 equality-expression
8712 and-expression & equality-expression
8714 exclusive-or-expression:
8715 and-expression
8716 exclusive-or-expression ^ and-expression
8718 inclusive-or-expression:
8719 exclusive-or-expression
8720 inclusive-or-expression | exclusive-or-expression
8722 logical-and-expression:
8723 inclusive-or-expression
8724 logical-and-expression && inclusive-or-expression
8726 logical-or-expression:
8727 logical-and-expression
8728 logical-or-expression || logical-and-expression
8730 All these are implemented with a single function like:
8732 binary-expression:
8733 simple-cast-expression
8734 binary-expression <token> binary-expression
8736 CAST_P is true if this expression is the target of a cast.
8738 The binops_by_token map is used to get the tree codes for each <token> type.
8739 binary-expressions are associated according to a precedence table. */
8741 #define TOKEN_PRECEDENCE(token) \
8742 (((token->type == CPP_GREATER \
8743 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8744 && !parser->greater_than_is_operator_p) \
8745 ? PREC_NOT_OPERATOR \
8746 : binops_by_token[token->type].prec)
8748 static cp_expr
8749 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8750 bool no_toplevel_fold_p,
8751 bool decltype_p,
8752 enum cp_parser_prec prec,
8753 cp_id_kind * pidk)
8755 cp_parser_expression_stack stack;
8756 cp_parser_expression_stack_entry *sp = &stack[0];
8757 cp_parser_expression_stack_entry current;
8758 cp_expr rhs;
8759 cp_token *token;
8760 enum tree_code rhs_type;
8761 enum cp_parser_prec new_prec, lookahead_prec;
8762 tree overload;
8764 /* Parse the first expression. */
8765 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8766 ? TRUTH_NOT_EXPR : ERROR_MARK);
8767 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8768 cast_p, decltype_p, pidk);
8769 current.prec = prec;
8771 if (cp_parser_error_occurred (parser))
8772 return error_mark_node;
8774 for (;;)
8776 /* Get an operator token. */
8777 token = cp_lexer_peek_token (parser->lexer);
8779 if (warn_cxx11_compat
8780 && token->type == CPP_RSHIFT
8781 && !parser->greater_than_is_operator_p)
8783 if (warning_at (token->location, OPT_Wc__11_compat,
8784 "%<>>%> operator is treated"
8785 " as two right angle brackets in C++11"))
8786 inform (token->location,
8787 "suggest parentheses around %<>>%> expression");
8790 new_prec = TOKEN_PRECEDENCE (token);
8791 if (new_prec != PREC_NOT_OPERATOR
8792 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8793 /* This is a fold-expression; handle it later. */
8794 new_prec = PREC_NOT_OPERATOR;
8796 /* Popping an entry off the stack means we completed a subexpression:
8797 - either we found a token which is not an operator (`>' where it is not
8798 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8799 will happen repeatedly;
8800 - or, we found an operator which has lower priority. This is the case
8801 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8802 parsing `3 * 4'. */
8803 if (new_prec <= current.prec)
8805 if (sp == stack)
8806 break;
8807 else
8808 goto pop;
8811 get_rhs:
8812 current.tree_type = binops_by_token[token->type].tree_type;
8813 current.loc = token->location;
8815 /* We used the operator token. */
8816 cp_lexer_consume_token (parser->lexer);
8818 /* For "false && x" or "true || x", x will never be executed;
8819 disable warnings while evaluating it. */
8820 if (current.tree_type == TRUTH_ANDIF_EXPR)
8821 c_inhibit_evaluation_warnings +=
8822 cp_fully_fold (current.lhs) == truthvalue_false_node;
8823 else if (current.tree_type == TRUTH_ORIF_EXPR)
8824 c_inhibit_evaluation_warnings +=
8825 cp_fully_fold (current.lhs) == truthvalue_true_node;
8827 /* Extract another operand. It may be the RHS of this expression
8828 or the LHS of a new, higher priority expression. */
8829 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8830 ? TRUTH_NOT_EXPR : ERROR_MARK);
8831 rhs = cp_parser_simple_cast_expression (parser);
8833 /* Get another operator token. Look up its precedence to avoid
8834 building a useless (immediately popped) stack entry for common
8835 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8836 token = cp_lexer_peek_token (parser->lexer);
8837 lookahead_prec = TOKEN_PRECEDENCE (token);
8838 if (lookahead_prec != PREC_NOT_OPERATOR
8839 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8840 lookahead_prec = PREC_NOT_OPERATOR;
8841 if (lookahead_prec > new_prec)
8843 /* ... and prepare to parse the RHS of the new, higher priority
8844 expression. Since precedence levels on the stack are
8845 monotonically increasing, we do not have to care about
8846 stack overflows. */
8847 *sp = current;
8848 ++sp;
8849 current.lhs = rhs;
8850 current.lhs_type = rhs_type;
8851 current.prec = new_prec;
8852 new_prec = lookahead_prec;
8853 goto get_rhs;
8855 pop:
8856 lookahead_prec = new_prec;
8857 /* If the stack is not empty, we have parsed into LHS the right side
8858 (`4' in the example above) of an expression we had suspended.
8859 We can use the information on the stack to recover the LHS (`3')
8860 from the stack together with the tree code (`MULT_EXPR'), and
8861 the precedence of the higher level subexpression
8862 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8863 which will be used to actually build the additive expression. */
8864 rhs = current.lhs;
8865 rhs_type = current.lhs_type;
8866 --sp;
8867 current = *sp;
8870 /* Undo the disabling of warnings done above. */
8871 if (current.tree_type == TRUTH_ANDIF_EXPR)
8872 c_inhibit_evaluation_warnings -=
8873 cp_fully_fold (current.lhs) == truthvalue_false_node;
8874 else if (current.tree_type == TRUTH_ORIF_EXPR)
8875 c_inhibit_evaluation_warnings -=
8876 cp_fully_fold (current.lhs) == truthvalue_true_node;
8878 if (warn_logical_not_paren
8879 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
8880 && current.lhs_type == TRUTH_NOT_EXPR
8881 /* Avoid warning for !!x == y. */
8882 && (TREE_CODE (current.lhs) != NE_EXPR
8883 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
8884 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
8885 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
8886 /* Avoid warning for !b == y where b is boolean. */
8887 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
8888 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
8889 != BOOLEAN_TYPE))))
8890 /* Avoid warning for !!b == y where b is boolean. */
8891 && (!DECL_P (current.lhs)
8892 || TREE_TYPE (current.lhs) == NULL_TREE
8893 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
8894 warn_logical_not_parentheses (current.loc, current.tree_type,
8895 maybe_constant_value (rhs));
8897 overload = NULL;
8899 location_t combined_loc = make_location (current.loc,
8900 current.lhs.get_start (),
8901 rhs.get_finish ());
8903 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8904 ERROR_MARK for everything that is not a binary expression.
8905 This makes warn_about_parentheses miss some warnings that
8906 involve unary operators. For unary expressions we should
8907 pass the correct tree_code unless the unary expression was
8908 surrounded by parentheses.
8910 if (no_toplevel_fold_p
8911 && lookahead_prec <= current.prec
8912 && sp == stack)
8913 current.lhs = build2_loc (combined_loc,
8914 current.tree_type,
8915 TREE_CODE_CLASS (current.tree_type)
8916 == tcc_comparison
8917 ? boolean_type_node : TREE_TYPE (current.lhs),
8918 current.lhs, rhs);
8919 else
8921 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
8922 current.lhs, current.lhs_type,
8923 rhs, rhs_type, &overload,
8924 complain_flags (decltype_p));
8925 /* TODO: build_x_binary_op doesn't always honor the location. */
8926 current.lhs.set_location (combined_loc);
8928 current.lhs_type = current.tree_type;
8930 /* If the binary operator required the use of an overloaded operator,
8931 then this expression cannot be an integral constant-expression.
8932 An overloaded operator can be used even if both operands are
8933 otherwise permissible in an integral constant-expression if at
8934 least one of the operands is of enumeration type. */
8936 if (overload
8937 && cp_parser_non_integral_constant_expression (parser,
8938 NIC_OVERLOADED))
8939 return error_mark_node;
8942 return current.lhs;
8945 static cp_expr
8946 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8947 bool no_toplevel_fold_p,
8948 enum cp_parser_prec prec,
8949 cp_id_kind * pidk)
8951 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8952 /*decltype*/false, prec, pidk);
8955 /* Parse the `? expression : assignment-expression' part of a
8956 conditional-expression. The LOGICAL_OR_EXPR is the
8957 logical-or-expression that started the conditional-expression.
8958 Returns a representation of the entire conditional-expression.
8960 This routine is used by cp_parser_assignment_expression.
8962 ? expression : assignment-expression
8964 GNU Extensions:
8966 ? : assignment-expression */
8968 static tree
8969 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
8971 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
8972 cp_expr assignment_expr;
8973 struct cp_token *token;
8974 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8976 /* Consume the `?' token. */
8977 cp_lexer_consume_token (parser->lexer);
8978 token = cp_lexer_peek_token (parser->lexer);
8979 if (cp_parser_allow_gnu_extensions_p (parser)
8980 && token->type == CPP_COLON)
8982 pedwarn (token->location, OPT_Wpedantic,
8983 "ISO C++ does not allow ?: with omitted middle operand");
8984 /* Implicit true clause. */
8985 expr = NULL_TREE;
8986 c_inhibit_evaluation_warnings +=
8987 folded_logical_or_expr == truthvalue_true_node;
8988 warn_for_omitted_condop (token->location, logical_or_expr);
8990 else
8992 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8993 parser->colon_corrects_to_scope_p = false;
8994 /* Parse the expression. */
8995 c_inhibit_evaluation_warnings +=
8996 folded_logical_or_expr == truthvalue_false_node;
8997 expr = cp_parser_expression (parser);
8998 c_inhibit_evaluation_warnings +=
8999 ((folded_logical_or_expr == truthvalue_true_node)
9000 - (folded_logical_or_expr == truthvalue_false_node));
9001 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9004 /* The next token should be a `:'. */
9005 cp_parser_require (parser, CPP_COLON, RT_COLON);
9006 /* Parse the assignment-expression. */
9007 assignment_expr = cp_parser_assignment_expression (parser);
9008 c_inhibit_evaluation_warnings -=
9009 folded_logical_or_expr == truthvalue_true_node;
9011 /* Make a location:
9012 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9013 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9014 with the caret at the "?", ranging from the start of
9015 the logical_or_expr to the end of the assignment_expr. */
9016 loc = make_location (loc,
9017 logical_or_expr.get_start (),
9018 assignment_expr.get_finish ());
9020 /* Build the conditional-expression. */
9021 return build_x_conditional_expr (loc, logical_or_expr,
9022 expr,
9023 assignment_expr,
9024 tf_warning_or_error);
9027 /* Parse an assignment-expression.
9029 assignment-expression:
9030 conditional-expression
9031 logical-or-expression assignment-operator assignment_expression
9032 throw-expression
9034 CAST_P is true if this expression is the target of a cast.
9035 DECLTYPE_P is true if this expression is the operand of decltype.
9037 Returns a representation for the expression. */
9039 static cp_expr
9040 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9041 bool cast_p, bool decltype_p)
9043 cp_expr expr;
9045 /* If the next token is the `throw' keyword, then we're looking at
9046 a throw-expression. */
9047 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9048 expr = cp_parser_throw_expression (parser);
9049 /* Otherwise, it must be that we are looking at a
9050 logical-or-expression. */
9051 else
9053 /* Parse the binary expressions (logical-or-expression). */
9054 expr = cp_parser_binary_expression (parser, cast_p, false,
9055 decltype_p,
9056 PREC_NOT_OPERATOR, pidk);
9057 /* If the next token is a `?' then we're actually looking at a
9058 conditional-expression. */
9059 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9060 return cp_parser_question_colon_clause (parser, expr);
9061 else
9063 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9065 /* If it's an assignment-operator, we're using the second
9066 production. */
9067 enum tree_code assignment_operator
9068 = cp_parser_assignment_operator_opt (parser);
9069 if (assignment_operator != ERROR_MARK)
9071 bool non_constant_p;
9073 /* Parse the right-hand side of the assignment. */
9074 cp_expr rhs = cp_parser_initializer_clause (parser,
9075 &non_constant_p);
9077 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9078 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9080 /* An assignment may not appear in a
9081 constant-expression. */
9082 if (cp_parser_non_integral_constant_expression (parser,
9083 NIC_ASSIGNMENT))
9084 return error_mark_node;
9085 /* Build the assignment expression. Its default
9086 location:
9087 LHS = RHS
9088 ~~~~^~~~~
9089 is the location of the '=' token as the
9090 caret, ranging from the start of the lhs to the
9091 end of the rhs. */
9092 loc = make_location (loc,
9093 expr.get_start (),
9094 rhs.get_finish ());
9095 expr = build_x_modify_expr (loc, expr,
9096 assignment_operator,
9097 rhs,
9098 complain_flags (decltype_p));
9099 /* TODO: build_x_modify_expr doesn't honor the location,
9100 so we must set it here. */
9101 expr.set_location (loc);
9106 return expr;
9109 /* Parse an (optional) assignment-operator.
9111 assignment-operator: one of
9112 = *= /= %= += -= >>= <<= &= ^= |=
9114 GNU Extension:
9116 assignment-operator: one of
9117 <?= >?=
9119 If the next token is an assignment operator, the corresponding tree
9120 code is returned, and the token is consumed. For example, for
9121 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9122 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9123 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9124 operator, ERROR_MARK is returned. */
9126 static enum tree_code
9127 cp_parser_assignment_operator_opt (cp_parser* parser)
9129 enum tree_code op;
9130 cp_token *token;
9132 /* Peek at the next token. */
9133 token = cp_lexer_peek_token (parser->lexer);
9135 switch (token->type)
9137 case CPP_EQ:
9138 op = NOP_EXPR;
9139 break;
9141 case CPP_MULT_EQ:
9142 op = MULT_EXPR;
9143 break;
9145 case CPP_DIV_EQ:
9146 op = TRUNC_DIV_EXPR;
9147 break;
9149 case CPP_MOD_EQ:
9150 op = TRUNC_MOD_EXPR;
9151 break;
9153 case CPP_PLUS_EQ:
9154 op = PLUS_EXPR;
9155 break;
9157 case CPP_MINUS_EQ:
9158 op = MINUS_EXPR;
9159 break;
9161 case CPP_RSHIFT_EQ:
9162 op = RSHIFT_EXPR;
9163 break;
9165 case CPP_LSHIFT_EQ:
9166 op = LSHIFT_EXPR;
9167 break;
9169 case CPP_AND_EQ:
9170 op = BIT_AND_EXPR;
9171 break;
9173 case CPP_XOR_EQ:
9174 op = BIT_XOR_EXPR;
9175 break;
9177 case CPP_OR_EQ:
9178 op = BIT_IOR_EXPR;
9179 break;
9181 default:
9182 /* Nothing else is an assignment operator. */
9183 op = ERROR_MARK;
9186 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9187 if (op != ERROR_MARK
9188 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9189 op = ERROR_MARK;
9191 /* If it was an assignment operator, consume it. */
9192 if (op != ERROR_MARK)
9193 cp_lexer_consume_token (parser->lexer);
9195 return op;
9198 /* Parse an expression.
9200 expression:
9201 assignment-expression
9202 expression , assignment-expression
9204 CAST_P is true if this expression is the target of a cast.
9205 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9206 except possibly parenthesized or on the RHS of a comma (N3276).
9208 Returns a representation of the expression. */
9210 static cp_expr
9211 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9212 bool cast_p, bool decltype_p)
9214 cp_expr expression = NULL_TREE;
9215 location_t loc = UNKNOWN_LOCATION;
9217 while (true)
9219 cp_expr assignment_expression;
9221 /* Parse the next assignment-expression. */
9222 assignment_expression
9223 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9225 /* We don't create a temporary for a call that is the immediate operand
9226 of decltype or on the RHS of a comma. But when we see a comma, we
9227 need to create a temporary for a call on the LHS. */
9228 if (decltype_p && !processing_template_decl
9229 && TREE_CODE (assignment_expression) == CALL_EXPR
9230 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9231 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9232 assignment_expression
9233 = build_cplus_new (TREE_TYPE (assignment_expression),
9234 assignment_expression, tf_warning_or_error);
9236 /* If this is the first assignment-expression, we can just
9237 save it away. */
9238 if (!expression)
9239 expression = assignment_expression;
9240 else
9242 /* Create a location with caret at the comma, ranging
9243 from the start of the LHS to the end of the RHS. */
9244 loc = make_location (loc,
9245 expression.get_start (),
9246 assignment_expression.get_finish ());
9247 expression = build_x_compound_expr (loc, expression,
9248 assignment_expression,
9249 complain_flags (decltype_p));
9250 expression.set_location (loc);
9252 /* If the next token is not a comma, or we're in a fold-expression, then
9253 we are done with the expression. */
9254 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9255 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9256 break;
9257 /* Consume the `,'. */
9258 loc = cp_lexer_peek_token (parser->lexer)->location;
9259 cp_lexer_consume_token (parser->lexer);
9260 /* A comma operator cannot appear in a constant-expression. */
9261 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9262 expression = error_mark_node;
9265 return expression;
9268 /* Parse a constant-expression.
9270 constant-expression:
9271 conditional-expression
9273 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9274 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9275 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9276 is false, NON_CONSTANT_P should be NULL. */
9278 static cp_expr
9279 cp_parser_constant_expression (cp_parser* parser,
9280 bool allow_non_constant_p,
9281 bool *non_constant_p)
9283 bool saved_integral_constant_expression_p;
9284 bool saved_allow_non_integral_constant_expression_p;
9285 bool saved_non_integral_constant_expression_p;
9286 cp_expr expression;
9288 /* It might seem that we could simply parse the
9289 conditional-expression, and then check to see if it were
9290 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9291 one that the compiler can figure out is constant, possibly after
9292 doing some simplifications or optimizations. The standard has a
9293 precise definition of constant-expression, and we must honor
9294 that, even though it is somewhat more restrictive.
9296 For example:
9298 int i[(2, 3)];
9300 is not a legal declaration, because `(2, 3)' is not a
9301 constant-expression. The `,' operator is forbidden in a
9302 constant-expression. However, GCC's constant-folding machinery
9303 will fold this operation to an INTEGER_CST for `3'. */
9305 /* Save the old settings. */
9306 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9307 saved_allow_non_integral_constant_expression_p
9308 = parser->allow_non_integral_constant_expression_p;
9309 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9310 /* We are now parsing a constant-expression. */
9311 parser->integral_constant_expression_p = true;
9312 parser->allow_non_integral_constant_expression_p
9313 = (allow_non_constant_p || cxx_dialect >= cxx11);
9314 parser->non_integral_constant_expression_p = false;
9315 /* Although the grammar says "conditional-expression", we parse an
9316 "assignment-expression", which also permits "throw-expression"
9317 and the use of assignment operators. In the case that
9318 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9319 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9320 actually essential that we look for an assignment-expression.
9321 For example, cp_parser_initializer_clauses uses this function to
9322 determine whether a particular assignment-expression is in fact
9323 constant. */
9324 expression = cp_parser_assignment_expression (parser);
9325 /* Restore the old settings. */
9326 parser->integral_constant_expression_p
9327 = saved_integral_constant_expression_p;
9328 parser->allow_non_integral_constant_expression_p
9329 = saved_allow_non_integral_constant_expression_p;
9330 if (cxx_dialect >= cxx11)
9332 /* Require an rvalue constant expression here; that's what our
9333 callers expect. Reference constant expressions are handled
9334 separately in e.g. cp_parser_template_argument. */
9335 bool is_const = potential_rvalue_constant_expression (expression);
9336 parser->non_integral_constant_expression_p = !is_const;
9337 if (!is_const && !allow_non_constant_p)
9338 require_potential_rvalue_constant_expression (expression);
9340 if (allow_non_constant_p)
9341 *non_constant_p = parser->non_integral_constant_expression_p;
9342 parser->non_integral_constant_expression_p
9343 = saved_non_integral_constant_expression_p;
9345 return expression;
9348 /* Parse __builtin_offsetof.
9350 offsetof-expression:
9351 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9353 offsetof-member-designator:
9354 id-expression
9355 | offsetof-member-designator "." id-expression
9356 | offsetof-member-designator "[" expression "]"
9357 | offsetof-member-designator "->" id-expression */
9359 static cp_expr
9360 cp_parser_builtin_offsetof (cp_parser *parser)
9362 int save_ice_p, save_non_ice_p;
9363 tree type;
9364 cp_expr expr;
9365 cp_id_kind dummy;
9366 cp_token *token;
9367 location_t finish_loc;
9369 /* We're about to accept non-integral-constant things, but will
9370 definitely yield an integral constant expression. Save and
9371 restore these values around our local parsing. */
9372 save_ice_p = parser->integral_constant_expression_p;
9373 save_non_ice_p = parser->non_integral_constant_expression_p;
9375 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9377 /* Consume the "__builtin_offsetof" token. */
9378 cp_lexer_consume_token (parser->lexer);
9379 /* Consume the opening `('. */
9380 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9381 /* Parse the type-id. */
9382 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9383 type = cp_parser_type_id (parser);
9384 /* Look for the `,'. */
9385 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9386 token = cp_lexer_peek_token (parser->lexer);
9388 /* Build the (type *)null that begins the traditional offsetof macro. */
9389 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
9390 tf_warning_or_error);
9392 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9393 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
9394 true, &dummy, token->location);
9395 while (true)
9397 token = cp_lexer_peek_token (parser->lexer);
9398 switch (token->type)
9400 case CPP_OPEN_SQUARE:
9401 /* offsetof-member-designator "[" expression "]" */
9402 expr = cp_parser_postfix_open_square_expression (parser, expr,
9403 true, false);
9404 break;
9406 case CPP_DEREF:
9407 /* offsetof-member-designator "->" identifier */
9408 expr = grok_array_decl (token->location, expr,
9409 integer_zero_node, false);
9410 /* FALLTHRU */
9412 case CPP_DOT:
9413 /* offsetof-member-designator "." identifier */
9414 cp_lexer_consume_token (parser->lexer);
9415 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9416 expr, true, &dummy,
9417 token->location);
9418 break;
9420 case CPP_CLOSE_PAREN:
9421 /* Consume the ")" token. */
9422 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9423 cp_lexer_consume_token (parser->lexer);
9424 goto success;
9426 default:
9427 /* Error. We know the following require will fail, but
9428 that gives the proper error message. */
9429 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9430 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9431 expr = error_mark_node;
9432 goto failure;
9436 success:
9437 /* Make a location of the form:
9438 __builtin_offsetof (struct s, f)
9439 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9440 with caret at the type-id, ranging from the start of the
9441 "_builtin_offsetof" token to the close paren. */
9442 loc = make_location (loc, start_loc, finish_loc);
9443 /* The result will be an INTEGER_CST, so we need to explicitly
9444 preserve the location. */
9445 expr = cp_expr (finish_offsetof (expr, loc), loc);
9447 failure:
9448 parser->integral_constant_expression_p = save_ice_p;
9449 parser->non_integral_constant_expression_p = save_non_ice_p;
9451 return expr;
9454 /* Parse a trait expression.
9456 Returns a representation of the expression, the underlying type
9457 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9459 static tree
9460 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9462 cp_trait_kind kind;
9463 tree type1, type2 = NULL_TREE;
9464 bool binary = false;
9465 bool variadic = false;
9467 switch (keyword)
9469 case RID_HAS_NOTHROW_ASSIGN:
9470 kind = CPTK_HAS_NOTHROW_ASSIGN;
9471 break;
9472 case RID_HAS_NOTHROW_CONSTRUCTOR:
9473 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9474 break;
9475 case RID_HAS_NOTHROW_COPY:
9476 kind = CPTK_HAS_NOTHROW_COPY;
9477 break;
9478 case RID_HAS_TRIVIAL_ASSIGN:
9479 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9480 break;
9481 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9482 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9483 break;
9484 case RID_HAS_TRIVIAL_COPY:
9485 kind = CPTK_HAS_TRIVIAL_COPY;
9486 break;
9487 case RID_HAS_TRIVIAL_DESTRUCTOR:
9488 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9489 break;
9490 case RID_HAS_VIRTUAL_DESTRUCTOR:
9491 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9492 break;
9493 case RID_IS_ABSTRACT:
9494 kind = CPTK_IS_ABSTRACT;
9495 break;
9496 case RID_IS_BASE_OF:
9497 kind = CPTK_IS_BASE_OF;
9498 binary = true;
9499 break;
9500 case RID_IS_CLASS:
9501 kind = CPTK_IS_CLASS;
9502 break;
9503 case RID_IS_EMPTY:
9504 kind = CPTK_IS_EMPTY;
9505 break;
9506 case RID_IS_ENUM:
9507 kind = CPTK_IS_ENUM;
9508 break;
9509 case RID_IS_FINAL:
9510 kind = CPTK_IS_FINAL;
9511 break;
9512 case RID_IS_LITERAL_TYPE:
9513 kind = CPTK_IS_LITERAL_TYPE;
9514 break;
9515 case RID_IS_POD:
9516 kind = CPTK_IS_POD;
9517 break;
9518 case RID_IS_POLYMORPHIC:
9519 kind = CPTK_IS_POLYMORPHIC;
9520 break;
9521 case RID_IS_SAME_AS:
9522 kind = CPTK_IS_SAME_AS;
9523 binary = true;
9524 break;
9525 case RID_IS_STD_LAYOUT:
9526 kind = CPTK_IS_STD_LAYOUT;
9527 break;
9528 case RID_IS_TRIVIAL:
9529 kind = CPTK_IS_TRIVIAL;
9530 break;
9531 case RID_IS_TRIVIALLY_ASSIGNABLE:
9532 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9533 binary = true;
9534 break;
9535 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9536 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9537 variadic = true;
9538 break;
9539 case RID_IS_TRIVIALLY_COPYABLE:
9540 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9541 break;
9542 case RID_IS_UNION:
9543 kind = CPTK_IS_UNION;
9544 break;
9545 case RID_UNDERLYING_TYPE:
9546 kind = CPTK_UNDERLYING_TYPE;
9547 break;
9548 case RID_BASES:
9549 kind = CPTK_BASES;
9550 break;
9551 case RID_DIRECT_BASES:
9552 kind = CPTK_DIRECT_BASES;
9553 break;
9554 default:
9555 gcc_unreachable ();
9558 /* Consume the token. */
9559 cp_lexer_consume_token (parser->lexer);
9561 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9564 type_id_in_expr_sentinel s (parser);
9565 type1 = cp_parser_type_id (parser);
9568 if (type1 == error_mark_node)
9569 return error_mark_node;
9571 if (binary)
9573 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9576 type_id_in_expr_sentinel s (parser);
9577 type2 = cp_parser_type_id (parser);
9580 if (type2 == error_mark_node)
9581 return error_mark_node;
9583 else if (variadic)
9585 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9587 cp_lexer_consume_token (parser->lexer);
9588 tree elt = cp_parser_type_id (parser);
9589 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9591 cp_lexer_consume_token (parser->lexer);
9592 elt = make_pack_expansion (elt);
9594 if (elt == error_mark_node)
9595 return error_mark_node;
9596 type2 = tree_cons (NULL_TREE, elt, type2);
9600 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9602 /* Complete the trait expression, which may mean either processing
9603 the trait expr now or saving it for template instantiation. */
9604 switch(kind)
9606 case CPTK_UNDERLYING_TYPE:
9607 return finish_underlying_type (type1);
9608 case CPTK_BASES:
9609 return finish_bases (type1, false);
9610 case CPTK_DIRECT_BASES:
9611 return finish_bases (type1, true);
9612 default:
9613 return finish_trait_expr (kind, type1, type2);
9617 /* Lambdas that appear in variable initializer or default argument scope
9618 get that in their mangling, so we need to record it. We might as well
9619 use the count for function and namespace scopes as well. */
9620 static GTY(()) tree lambda_scope;
9621 static GTY(()) int lambda_count;
9622 struct GTY(()) tree_int
9624 tree t;
9625 int i;
9627 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
9629 static void
9630 start_lambda_scope (tree decl)
9632 tree_int ti;
9633 gcc_assert (decl);
9634 /* Once we're inside a function, we ignore other scopes and just push
9635 the function again so that popping works properly. */
9636 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
9637 decl = current_function_decl;
9638 ti.t = lambda_scope;
9639 ti.i = lambda_count;
9640 vec_safe_push (lambda_scope_stack, ti);
9641 if (lambda_scope != decl)
9643 /* Don't reset the count if we're still in the same function. */
9644 lambda_scope = decl;
9645 lambda_count = 0;
9649 static void
9650 record_lambda_scope (tree lambda)
9652 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
9653 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
9656 static void
9657 finish_lambda_scope (void)
9659 tree_int *p = &lambda_scope_stack->last ();
9660 if (lambda_scope != p->t)
9662 lambda_scope = p->t;
9663 lambda_count = p->i;
9665 lambda_scope_stack->pop ();
9668 /* Parse a lambda expression.
9670 lambda-expression:
9671 lambda-introducer lambda-declarator [opt] compound-statement
9673 Returns a representation of the expression. */
9675 static cp_expr
9676 cp_parser_lambda_expression (cp_parser* parser)
9678 tree lambda_expr = build_lambda_expr ();
9679 tree type;
9680 bool ok = true;
9681 cp_token *token = cp_lexer_peek_token (parser->lexer);
9682 cp_token_position start = 0;
9684 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9686 if (cp_unevaluated_operand)
9688 if (!token->error_reported)
9690 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9691 "lambda-expression in unevaluated context");
9692 token->error_reported = true;
9694 ok = false;
9696 else if (parser->in_template_argument_list_p)
9698 if (!token->error_reported)
9700 error_at (token->location, "lambda-expression in template-argument");
9701 token->error_reported = true;
9703 ok = false;
9706 /* We may be in the middle of deferred access check. Disable
9707 it now. */
9708 push_deferring_access_checks (dk_no_deferred);
9710 cp_parser_lambda_introducer (parser, lambda_expr);
9712 type = begin_lambda_type (lambda_expr);
9713 if (type == error_mark_node)
9714 return error_mark_node;
9716 record_lambda_scope (lambda_expr);
9718 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9719 determine_visibility (TYPE_NAME (type));
9721 /* Now that we've started the type, add the capture fields for any
9722 explicit captures. */
9723 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9726 /* Inside the class, surrounding template-parameter-lists do not apply. */
9727 unsigned int saved_num_template_parameter_lists
9728 = parser->num_template_parameter_lists;
9729 unsigned char in_statement = parser->in_statement;
9730 bool in_switch_statement_p = parser->in_switch_statement_p;
9731 bool fully_implicit_function_template_p
9732 = parser->fully_implicit_function_template_p;
9733 tree implicit_template_parms = parser->implicit_template_parms;
9734 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9735 bool auto_is_implicit_function_template_parm_p
9736 = parser->auto_is_implicit_function_template_parm_p;
9738 parser->num_template_parameter_lists = 0;
9739 parser->in_statement = 0;
9740 parser->in_switch_statement_p = false;
9741 parser->fully_implicit_function_template_p = false;
9742 parser->implicit_template_parms = 0;
9743 parser->implicit_template_scope = 0;
9744 parser->auto_is_implicit_function_template_parm_p = false;
9746 /* By virtue of defining a local class, a lambda expression has access to
9747 the private variables of enclosing classes. */
9749 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9751 if (ok)
9753 if (!cp_parser_error_occurred (parser)
9754 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9755 && cp_parser_start_tentative_firewall (parser))
9756 start = token;
9757 cp_parser_lambda_body (parser, lambda_expr);
9759 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9761 if (cp_parser_skip_to_closing_brace (parser))
9762 cp_lexer_consume_token (parser->lexer);
9765 /* The capture list was built up in reverse order; fix that now. */
9766 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9767 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9769 if (ok)
9770 maybe_add_lambda_conv_op (type);
9772 type = finish_struct (type, /*attributes=*/NULL_TREE);
9774 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9775 parser->in_statement = in_statement;
9776 parser->in_switch_statement_p = in_switch_statement_p;
9777 parser->fully_implicit_function_template_p
9778 = fully_implicit_function_template_p;
9779 parser->implicit_template_parms = implicit_template_parms;
9780 parser->implicit_template_scope = implicit_template_scope;
9781 parser->auto_is_implicit_function_template_parm_p
9782 = auto_is_implicit_function_template_parm_p;
9785 pop_deferring_access_checks ();
9787 /* This field is only used during parsing of the lambda. */
9788 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9790 /* This lambda shouldn't have any proxies left at this point. */
9791 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9792 /* And now that we're done, push proxies for an enclosing lambda. */
9793 insert_pending_capture_proxies ();
9795 if (ok)
9796 lambda_expr = build_lambda_object (lambda_expr);
9797 else
9798 lambda_expr = error_mark_node;
9800 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9802 return lambda_expr;
9805 /* Parse the beginning of a lambda expression.
9807 lambda-introducer:
9808 [ lambda-capture [opt] ]
9810 LAMBDA_EXPR is the current representation of the lambda expression. */
9812 static void
9813 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9815 /* Need commas after the first capture. */
9816 bool first = true;
9818 /* Eat the leading `['. */
9819 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9821 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9822 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9823 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9824 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9825 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9826 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9828 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9830 cp_lexer_consume_token (parser->lexer);
9831 first = false;
9834 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9836 cp_token* capture_token;
9837 tree capture_id;
9838 tree capture_init_expr;
9839 cp_id_kind idk = CP_ID_KIND_NONE;
9840 bool explicit_init_p = false;
9842 enum capture_kind_type
9844 BY_COPY,
9845 BY_REFERENCE
9847 enum capture_kind_type capture_kind = BY_COPY;
9849 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9851 error ("expected end of capture-list");
9852 return;
9855 if (first)
9856 first = false;
9857 else
9858 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9860 /* Possibly capture `this'. */
9861 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9863 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9864 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9865 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9866 "with by-copy capture default");
9867 cp_lexer_consume_token (parser->lexer);
9868 add_capture (lambda_expr,
9869 /*id=*/this_identifier,
9870 /*initializer=*/finish_this_expr(),
9871 /*by_reference_p=*/false,
9872 explicit_init_p);
9873 continue;
9876 /* Remember whether we want to capture as a reference or not. */
9877 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9879 capture_kind = BY_REFERENCE;
9880 cp_lexer_consume_token (parser->lexer);
9883 /* Get the identifier. */
9884 capture_token = cp_lexer_peek_token (parser->lexer);
9885 capture_id = cp_parser_identifier (parser);
9887 if (capture_id == error_mark_node)
9888 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9889 delimiters, but I modified this to stop on unnested ']' as well. It
9890 was already changed to stop on unnested '}', so the
9891 "closing_parenthesis" name is no more misleading with my change. */
9893 cp_parser_skip_to_closing_parenthesis (parser,
9894 /*recovering=*/true,
9895 /*or_comma=*/true,
9896 /*consume_paren=*/true);
9897 break;
9900 /* Find the initializer for this capture. */
9901 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9902 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9903 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9905 bool direct, non_constant;
9906 /* An explicit initializer exists. */
9907 if (cxx_dialect < cxx14)
9908 pedwarn (input_location, 0,
9909 "lambda capture initializers "
9910 "only available with -std=c++14 or -std=gnu++14");
9911 capture_init_expr = cp_parser_initializer (parser, &direct,
9912 &non_constant);
9913 explicit_init_p = true;
9914 if (capture_init_expr == NULL_TREE)
9916 error ("empty initializer for lambda init-capture");
9917 capture_init_expr = error_mark_node;
9920 else
9922 const char* error_msg;
9924 /* Turn the identifier into an id-expression. */
9925 capture_init_expr
9926 = cp_parser_lookup_name_simple (parser, capture_id,
9927 capture_token->location);
9929 if (capture_init_expr == error_mark_node)
9931 unqualified_name_lookup_error (capture_id);
9932 continue;
9934 else if (DECL_P (capture_init_expr)
9935 && (!VAR_P (capture_init_expr)
9936 && TREE_CODE (capture_init_expr) != PARM_DECL))
9938 error_at (capture_token->location,
9939 "capture of non-variable %qD ",
9940 capture_init_expr);
9941 inform (DECL_SOURCE_LOCATION (capture_init_expr),
9942 "%q#D declared here", capture_init_expr);
9943 continue;
9945 if (VAR_P (capture_init_expr)
9946 && decl_storage_duration (capture_init_expr) != dk_auto)
9948 if (pedwarn (capture_token->location, 0, "capture of variable "
9949 "%qD with non-automatic storage duration",
9950 capture_init_expr))
9951 inform (DECL_SOURCE_LOCATION (capture_init_expr),
9952 "%q#D declared here", capture_init_expr);
9953 continue;
9956 capture_init_expr
9957 = finish_id_expression
9958 (capture_id,
9959 capture_init_expr,
9960 parser->scope,
9961 &idk,
9962 /*integral_constant_expression_p=*/false,
9963 /*allow_non_integral_constant_expression_p=*/false,
9964 /*non_integral_constant_expression_p=*/NULL,
9965 /*template_p=*/false,
9966 /*done=*/true,
9967 /*address_p=*/false,
9968 /*template_arg_p=*/false,
9969 &error_msg,
9970 capture_token->location);
9972 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9974 cp_lexer_consume_token (parser->lexer);
9975 capture_init_expr = make_pack_expansion (capture_init_expr);
9977 else
9978 check_for_bare_parameter_packs (capture_init_expr);
9981 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9982 && !explicit_init_p)
9984 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9985 && capture_kind == BY_COPY)
9986 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9987 "of %qD redundant with by-copy capture default",
9988 capture_id);
9989 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9990 && capture_kind == BY_REFERENCE)
9991 pedwarn (capture_token->location, 0, "explicit by-reference "
9992 "capture of %qD redundant with by-reference capture "
9993 "default", capture_id);
9996 add_capture (lambda_expr,
9997 capture_id,
9998 capture_init_expr,
9999 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10000 explicit_init_p);
10003 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10006 /* Parse the (optional) middle of a lambda expression.
10008 lambda-declarator:
10009 < template-parameter-list [opt] >
10010 ( parameter-declaration-clause [opt] )
10011 attribute-specifier [opt]
10012 mutable [opt]
10013 exception-specification [opt]
10014 lambda-return-type-clause [opt]
10016 LAMBDA_EXPR is the current representation of the lambda expression. */
10018 static bool
10019 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10021 /* 5.1.1.4 of the standard says:
10022 If a lambda-expression does not include a lambda-declarator, it is as if
10023 the lambda-declarator were ().
10024 This means an empty parameter list, no attributes, and no exception
10025 specification. */
10026 tree param_list = void_list_node;
10027 tree attributes = NULL_TREE;
10028 tree exception_spec = NULL_TREE;
10029 tree template_param_list = NULL_TREE;
10030 tree tx_qual = NULL_TREE;
10032 /* The template-parameter-list is optional, but must begin with
10033 an opening angle if present. */
10034 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10036 if (cxx_dialect < cxx14)
10037 pedwarn (parser->lexer->next_token->location, 0,
10038 "lambda templates are only available with "
10039 "-std=c++14 or -std=gnu++14");
10041 cp_lexer_consume_token (parser->lexer);
10043 template_param_list = cp_parser_template_parameter_list (parser);
10045 cp_parser_skip_to_end_of_template_parameter_list (parser);
10047 /* We just processed one more parameter list. */
10048 ++parser->num_template_parameter_lists;
10051 /* The parameter-declaration-clause is optional (unless
10052 template-parameter-list was given), but must begin with an
10053 opening parenthesis if present. */
10054 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10056 cp_lexer_consume_token (parser->lexer);
10058 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10060 /* Parse parameters. */
10061 param_list = cp_parser_parameter_declaration_clause (parser);
10063 /* Default arguments shall not be specified in the
10064 parameter-declaration-clause of a lambda-declarator. */
10065 for (tree t = param_list; t; t = TREE_CHAIN (t))
10066 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
10067 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10068 "default argument specified for lambda parameter");
10070 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10072 attributes = cp_parser_attributes_opt (parser);
10074 /* Parse optional `mutable' keyword. */
10075 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
10077 cp_lexer_consume_token (parser->lexer);
10078 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10081 tx_qual = cp_parser_tx_qualifier_opt (parser);
10083 /* Parse optional exception specification. */
10084 exception_spec = cp_parser_exception_specification_opt (parser);
10086 /* Parse optional trailing return type. */
10087 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10089 cp_lexer_consume_token (parser->lexer);
10090 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10091 = cp_parser_trailing_type_id (parser);
10094 /* The function parameters must be in scope all the way until after the
10095 trailing-return-type in case of decltype. */
10096 pop_bindings_and_leave_scope ();
10098 else if (template_param_list != NULL_TREE) // generate diagnostic
10099 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10101 /* Create the function call operator.
10103 Messing with declarators like this is no uglier than building up the
10104 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10105 other code. */
10107 cp_decl_specifier_seq return_type_specs;
10108 cp_declarator* declarator;
10109 tree fco;
10110 int quals;
10111 void *p;
10113 clear_decl_specs (&return_type_specs);
10114 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10115 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
10116 else
10117 /* Maybe we will deduce the return type later. */
10118 return_type_specs.type = make_auto ();
10120 p = obstack_alloc (&declarator_obstack, 0);
10122 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
10123 sfk_none);
10125 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10126 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10127 declarator = make_call_declarator (declarator, param_list, quals,
10128 VIRT_SPEC_UNSPECIFIED,
10129 REF_QUAL_NONE,
10130 tx_qual,
10131 exception_spec,
10132 /*late_return_type=*/NULL_TREE,
10133 /*requires_clause*/NULL_TREE);
10134 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10136 fco = grokmethod (&return_type_specs,
10137 declarator,
10138 attributes);
10139 if (fco != error_mark_node)
10141 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10142 DECL_ARTIFICIAL (fco) = 1;
10143 /* Give the object parameter a different name. */
10144 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10145 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10146 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10148 if (template_param_list)
10150 fco = finish_member_template_decl (fco);
10151 finish_template_decl (template_param_list);
10152 --parser->num_template_parameter_lists;
10154 else if (parser->fully_implicit_function_template_p)
10155 fco = finish_fully_implicit_template (parser, fco);
10157 finish_member_declaration (fco);
10159 obstack_free (&declarator_obstack, p);
10161 return (fco != error_mark_node);
10165 /* Parse the body of a lambda expression, which is simply
10167 compound-statement
10169 but which requires special handling.
10170 LAMBDA_EXPR is the current representation of the lambda expression. */
10172 static void
10173 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10175 bool nested = (current_function_decl != NULL_TREE);
10176 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10177 if (nested)
10178 push_function_context ();
10179 else
10180 /* Still increment function_depth so that we don't GC in the
10181 middle of an expression. */
10182 ++function_depth;
10183 vec<tree> omp_privatization_save;
10184 save_omp_privatization_clauses (omp_privatization_save);
10185 /* Clear this in case we're in the middle of a default argument. */
10186 parser->local_variables_forbidden_p = false;
10188 /* Finish the function call operator
10189 - class_specifier
10190 + late_parsing_for_member
10191 + function_definition_after_declarator
10192 + ctor_initializer_opt_and_function_body */
10194 tree fco = lambda_function (lambda_expr);
10195 tree body;
10196 bool done = false;
10197 tree compound_stmt;
10198 tree cap;
10200 /* Let the front end know that we are going to be defining this
10201 function. */
10202 start_preparsed_function (fco,
10203 NULL_TREE,
10204 SF_PRE_PARSED | SF_INCLASS_INLINE);
10206 start_lambda_scope (fco);
10207 body = begin_function_body ();
10209 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10210 goto out;
10212 /* Push the proxies for any explicit captures. */
10213 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
10214 cap = TREE_CHAIN (cap))
10215 build_capture_proxy (TREE_PURPOSE (cap));
10217 compound_stmt = begin_compound_stmt (0);
10219 /* 5.1.1.4 of the standard says:
10220 If a lambda-expression does not include a trailing-return-type, it
10221 is as if the trailing-return-type denotes the following type:
10222 * if the compound-statement is of the form
10223 { return attribute-specifier [opt] expression ; }
10224 the type of the returned expression after lvalue-to-rvalue
10225 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10226 (_conv.array_ 4.2), and function-to-pointer conversion
10227 (_conv.func_ 4.3);
10228 * otherwise, void. */
10230 /* In a lambda that has neither a lambda-return-type-clause
10231 nor a deducible form, errors should be reported for return statements
10232 in the body. Since we used void as the placeholder return type, parsing
10233 the body as usual will give such desired behavior. */
10234 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10235 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
10236 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
10238 tree expr = NULL_TREE;
10239 cp_id_kind idk = CP_ID_KIND_NONE;
10241 /* Parse tentatively in case there's more after the initial return
10242 statement. */
10243 cp_parser_parse_tentatively (parser);
10245 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
10247 expr = cp_parser_expression (parser, &idk);
10249 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10250 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10252 if (cp_parser_parse_definitely (parser))
10254 if (!processing_template_decl)
10256 tree type = lambda_return_type (expr);
10257 apply_deduced_return_type (fco, type);
10258 if (type == error_mark_node)
10259 expr = error_mark_node;
10262 /* Will get error here if type not deduced yet. */
10263 finish_return_stmt (expr);
10265 done = true;
10269 if (!done)
10271 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10272 cp_parser_label_declaration (parser);
10273 cp_parser_statement_seq_opt (parser, NULL_TREE);
10274 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10277 finish_compound_stmt (compound_stmt);
10279 out:
10280 finish_function_body (body);
10281 finish_lambda_scope ();
10283 /* Finish the function and generate code for it if necessary. */
10284 tree fn = finish_function (/*inline*/2);
10286 /* Only expand if the call op is not a template. */
10287 if (!DECL_TEMPLATE_INFO (fco))
10288 expand_or_defer_fn (fn);
10291 restore_omp_privatization_clauses (omp_privatization_save);
10292 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10293 if (nested)
10294 pop_function_context();
10295 else
10296 --function_depth;
10299 /* Statements [gram.stmt.stmt] */
10301 /* Parse a statement.
10303 statement:
10304 labeled-statement
10305 expression-statement
10306 compound-statement
10307 selection-statement
10308 iteration-statement
10309 jump-statement
10310 declaration-statement
10311 try-block
10313 C++11:
10315 statement:
10316 labeled-statement
10317 attribute-specifier-seq (opt) expression-statement
10318 attribute-specifier-seq (opt) compound-statement
10319 attribute-specifier-seq (opt) selection-statement
10320 attribute-specifier-seq (opt) iteration-statement
10321 attribute-specifier-seq (opt) jump-statement
10322 declaration-statement
10323 attribute-specifier-seq (opt) try-block
10325 TM Extension:
10327 statement:
10328 atomic-statement
10330 IN_COMPOUND is true when the statement is nested inside a
10331 cp_parser_compound_statement; this matters for certain pragmas.
10333 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10334 is a (possibly labeled) if statement which is not enclosed in braces
10335 and has an else clause. This is used to implement -Wparentheses.
10337 CHAIN is a vector of if-else-if conditions. */
10339 static void
10340 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10341 bool in_compound, bool *if_p, vec<tree> *chain)
10343 tree statement, std_attrs = NULL_TREE;
10344 cp_token *token;
10345 location_t statement_location, attrs_location;
10347 restart:
10348 if (if_p != NULL)
10349 *if_p = false;
10350 /* There is no statement yet. */
10351 statement = NULL_TREE;
10353 saved_token_sentinel saved_tokens (parser->lexer);
10354 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10355 if (c_dialect_objc ())
10356 /* In obj-c++, seeing '[[' might be the either the beginning of
10357 c++11 attributes, or a nested objc-message-expression. So
10358 let's parse the c++11 attributes tentatively. */
10359 cp_parser_parse_tentatively (parser);
10360 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10361 if (c_dialect_objc ())
10363 if (!cp_parser_parse_definitely (parser))
10364 std_attrs = NULL_TREE;
10367 /* Peek at the next token. */
10368 token = cp_lexer_peek_token (parser->lexer);
10369 /* Remember the location of the first token in the statement. */
10370 statement_location = token->location;
10371 /* If this is a keyword, then that will often determine what kind of
10372 statement we have. */
10373 if (token->type == CPP_KEYWORD)
10375 enum rid keyword = token->keyword;
10377 switch (keyword)
10379 case RID_CASE:
10380 case RID_DEFAULT:
10381 /* Looks like a labeled-statement with a case label.
10382 Parse the label, and then use tail recursion to parse
10383 the statement. */
10384 cp_parser_label_for_labeled_statement (parser, std_attrs);
10385 in_compound = false;
10386 goto restart;
10388 case RID_IF:
10389 case RID_SWITCH:
10390 statement = cp_parser_selection_statement (parser, if_p, chain);
10391 break;
10393 case RID_WHILE:
10394 case RID_DO:
10395 case RID_FOR:
10396 statement = cp_parser_iteration_statement (parser, false);
10397 break;
10399 case RID_CILK_FOR:
10400 if (!flag_cilkplus)
10402 error_at (cp_lexer_peek_token (parser->lexer)->location,
10403 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10404 cp_lexer_consume_token (parser->lexer);
10405 statement = error_mark_node;
10407 else
10408 statement = cp_parser_cilk_for (parser, integer_zero_node);
10409 break;
10411 case RID_BREAK:
10412 case RID_CONTINUE:
10413 case RID_RETURN:
10414 case RID_GOTO:
10415 statement = cp_parser_jump_statement (parser);
10416 break;
10418 case RID_CILK_SYNC:
10419 cp_lexer_consume_token (parser->lexer);
10420 if (flag_cilkplus)
10422 tree sync_expr = build_cilk_sync ();
10423 SET_EXPR_LOCATION (sync_expr,
10424 token->location);
10425 statement = finish_expr_stmt (sync_expr);
10427 else
10429 error_at (token->location, "-fcilkplus must be enabled to use"
10430 " %<_Cilk_sync%>");
10431 statement = error_mark_node;
10433 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10434 break;
10436 /* Objective-C++ exception-handling constructs. */
10437 case RID_AT_TRY:
10438 case RID_AT_CATCH:
10439 case RID_AT_FINALLY:
10440 case RID_AT_SYNCHRONIZED:
10441 case RID_AT_THROW:
10442 statement = cp_parser_objc_statement (parser);
10443 break;
10445 case RID_TRY:
10446 statement = cp_parser_try_block (parser);
10447 break;
10449 case RID_NAMESPACE:
10450 /* This must be a namespace alias definition. */
10451 cp_parser_declaration_statement (parser);
10452 return;
10454 case RID_TRANSACTION_ATOMIC:
10455 case RID_TRANSACTION_RELAXED:
10456 case RID_SYNCHRONIZED:
10457 case RID_ATOMIC_NOEXCEPT:
10458 case RID_ATOMIC_CANCEL:
10459 statement = cp_parser_transaction (parser, token);
10460 break;
10461 case RID_TRANSACTION_CANCEL:
10462 statement = cp_parser_transaction_cancel (parser);
10463 break;
10465 default:
10466 /* It might be a keyword like `int' that can start a
10467 declaration-statement. */
10468 break;
10471 else if (token->type == CPP_NAME)
10473 /* If the next token is a `:', then we are looking at a
10474 labeled-statement. */
10475 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10476 if (token->type == CPP_COLON)
10478 /* Looks like a labeled-statement with an ordinary label.
10479 Parse the label, and then use tail recursion to parse
10480 the statement. */
10482 cp_parser_label_for_labeled_statement (parser, std_attrs);
10483 in_compound = false;
10484 goto restart;
10487 /* Anything that starts with a `{' must be a compound-statement. */
10488 else if (token->type == CPP_OPEN_BRACE)
10489 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10490 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10491 a statement all its own. */
10492 else if (token->type == CPP_PRAGMA)
10494 /* Only certain OpenMP pragmas are attached to statements, and thus
10495 are considered statements themselves. All others are not. In
10496 the context of a compound, accept the pragma as a "statement" and
10497 return so that we can check for a close brace. Otherwise we
10498 require a real statement and must go back and read one. */
10499 if (in_compound)
10500 cp_parser_pragma (parser, pragma_compound);
10501 else if (!cp_parser_pragma (parser, pragma_stmt))
10502 goto restart;
10503 return;
10505 else if (token->type == CPP_EOF)
10507 cp_parser_error (parser, "expected statement");
10508 return;
10511 /* Everything else must be a declaration-statement or an
10512 expression-statement. Try for the declaration-statement
10513 first, unless we are looking at a `;', in which case we know that
10514 we have an expression-statement. */
10515 if (!statement)
10517 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10519 if (std_attrs != NULL_TREE)
10521 /* Attributes should be parsed as part of the the
10522 declaration, so let's un-parse them. */
10523 saved_tokens.rollback();
10524 std_attrs = NULL_TREE;
10527 cp_parser_parse_tentatively (parser);
10528 /* Try to parse the declaration-statement. */
10529 cp_parser_declaration_statement (parser);
10530 /* If that worked, we're done. */
10531 if (cp_parser_parse_definitely (parser))
10532 return;
10534 /* Look for an expression-statement instead. */
10535 statement = cp_parser_expression_statement (parser, in_statement_expr);
10538 /* Set the line number for the statement. */
10539 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10540 SET_EXPR_LOCATION (statement, statement_location);
10542 /* Note that for now, we don't do anything with c++11 statements
10543 parsed at this level. */
10544 if (std_attrs != NULL_TREE)
10545 warning_at (attrs_location,
10546 OPT_Wattributes,
10547 "attributes at the beginning of statement are ignored");
10550 /* Parse the label for a labeled-statement, i.e.
10552 identifier :
10553 case constant-expression :
10554 default :
10556 GNU Extension:
10557 case constant-expression ... constant-expression : statement
10559 When a label is parsed without errors, the label is added to the
10560 parse tree by the finish_* functions, so this function doesn't
10561 have to return the label. */
10563 static void
10564 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10566 cp_token *token;
10567 tree label = NULL_TREE;
10568 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10570 /* The next token should be an identifier. */
10571 token = cp_lexer_peek_token (parser->lexer);
10572 if (token->type != CPP_NAME
10573 && token->type != CPP_KEYWORD)
10575 cp_parser_error (parser, "expected labeled-statement");
10576 return;
10579 parser->colon_corrects_to_scope_p = false;
10580 switch (token->keyword)
10582 case RID_CASE:
10584 tree expr, expr_hi;
10585 cp_token *ellipsis;
10587 /* Consume the `case' token. */
10588 cp_lexer_consume_token (parser->lexer);
10589 /* Parse the constant-expression. */
10590 expr = cp_parser_constant_expression (parser);
10591 if (check_for_bare_parameter_packs (expr))
10592 expr = error_mark_node;
10594 ellipsis = cp_lexer_peek_token (parser->lexer);
10595 if (ellipsis->type == CPP_ELLIPSIS)
10597 /* Consume the `...' token. */
10598 cp_lexer_consume_token (parser->lexer);
10599 expr_hi = cp_parser_constant_expression (parser);
10600 if (check_for_bare_parameter_packs (expr_hi))
10601 expr_hi = error_mark_node;
10603 /* We don't need to emit warnings here, as the common code
10604 will do this for us. */
10606 else
10607 expr_hi = NULL_TREE;
10609 if (parser->in_switch_statement_p)
10610 finish_case_label (token->location, expr, expr_hi);
10611 else
10612 error_at (token->location,
10613 "case label %qE not within a switch statement",
10614 expr);
10616 break;
10618 case RID_DEFAULT:
10619 /* Consume the `default' token. */
10620 cp_lexer_consume_token (parser->lexer);
10622 if (parser->in_switch_statement_p)
10623 finish_case_label (token->location, NULL_TREE, NULL_TREE);
10624 else
10625 error_at (token->location, "case label not within a switch statement");
10626 break;
10628 default:
10629 /* Anything else must be an ordinary label. */
10630 label = finish_label_stmt (cp_parser_identifier (parser));
10631 break;
10634 /* Require the `:' token. */
10635 cp_parser_require (parser, CPP_COLON, RT_COLON);
10637 /* An ordinary label may optionally be followed by attributes.
10638 However, this is only permitted if the attributes are then
10639 followed by a semicolon. This is because, for backward
10640 compatibility, when parsing
10641 lab: __attribute__ ((unused)) int i;
10642 we want the attribute to attach to "i", not "lab". */
10643 if (label != NULL_TREE
10644 && cp_next_tokens_can_be_gnu_attribute_p (parser))
10646 tree attrs;
10647 cp_parser_parse_tentatively (parser);
10648 attrs = cp_parser_gnu_attributes_opt (parser);
10649 if (attrs == NULL_TREE
10650 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10651 cp_parser_abort_tentative_parse (parser);
10652 else if (!cp_parser_parse_definitely (parser))
10654 else
10655 attributes = chainon (attributes, attrs);
10658 if (attributes != NULL_TREE)
10659 cplus_decl_attributes (&label, attributes, 0);
10661 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10664 /* Parse an expression-statement.
10666 expression-statement:
10667 expression [opt] ;
10669 Returns the new EXPR_STMT -- or NULL_TREE if the expression
10670 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
10671 indicates whether this expression-statement is part of an
10672 expression statement. */
10674 static tree
10675 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
10677 tree statement = NULL_TREE;
10678 cp_token *token = cp_lexer_peek_token (parser->lexer);
10680 /* If the next token is a ';', then there is no expression
10681 statement. */
10682 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10684 statement = cp_parser_expression (parser);
10685 if (statement == error_mark_node
10686 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10688 cp_parser_skip_to_end_of_block_or_statement (parser);
10689 return error_mark_node;
10693 /* Give a helpful message for "A<T>::type t;" and the like. */
10694 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
10695 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10697 if (TREE_CODE (statement) == SCOPE_REF)
10698 error_at (token->location, "need %<typename%> before %qE because "
10699 "%qT is a dependent scope",
10700 statement, TREE_OPERAND (statement, 0));
10701 else if (is_overloaded_fn (statement)
10702 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
10704 /* A::A a; */
10705 tree fn = get_first_fn (statement);
10706 error_at (token->location,
10707 "%<%T::%D%> names the constructor, not the type",
10708 DECL_CONTEXT (fn), DECL_NAME (fn));
10712 /* Consume the final `;'. */
10713 cp_parser_consume_semicolon_at_end_of_statement (parser);
10715 if (in_statement_expr
10716 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10717 /* This is the final expression statement of a statement
10718 expression. */
10719 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10720 else if (statement)
10721 statement = finish_expr_stmt (statement);
10723 return statement;
10726 /* Parse a compound-statement.
10728 compound-statement:
10729 { statement-seq [opt] }
10731 GNU extension:
10733 compound-statement:
10734 { label-declaration-seq [opt] statement-seq [opt] }
10736 label-declaration-seq:
10737 label-declaration
10738 label-declaration-seq label-declaration
10740 Returns a tree representing the statement. */
10742 static tree
10743 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10744 int bcs_flags, bool function_body)
10746 tree compound_stmt;
10748 /* Consume the `{'. */
10749 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10750 return error_mark_node;
10751 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10752 && !function_body && cxx_dialect < cxx14)
10753 pedwarn (input_location, OPT_Wpedantic,
10754 "compound-statement in constexpr function");
10755 /* Begin the compound-statement. */
10756 compound_stmt = begin_compound_stmt (bcs_flags);
10757 /* If the next keyword is `__label__' we have a label declaration. */
10758 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10759 cp_parser_label_declaration (parser);
10760 /* Parse an (optional) statement-seq. */
10761 cp_parser_statement_seq_opt (parser, in_statement_expr);
10762 /* Finish the compound-statement. */
10763 finish_compound_stmt (compound_stmt);
10764 /* Consume the `}'. */
10765 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10767 return compound_stmt;
10770 /* Parse an (optional) statement-seq.
10772 statement-seq:
10773 statement
10774 statement-seq [opt] statement */
10776 static void
10777 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10779 /* Scan statements until there aren't any more. */
10780 while (true)
10782 cp_token *token = cp_lexer_peek_token (parser->lexer);
10784 /* If we are looking at a `}', then we have run out of
10785 statements; the same is true if we have reached the end
10786 of file, or have stumbled upon a stray '@end'. */
10787 if (token->type == CPP_CLOSE_BRACE
10788 || token->type == CPP_EOF
10789 || token->type == CPP_PRAGMA_EOL
10790 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10791 break;
10793 /* If we are in a compound statement and find 'else' then
10794 something went wrong. */
10795 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10797 if (parser->in_statement & IN_IF_STMT)
10798 break;
10799 else
10801 token = cp_lexer_consume_token (parser->lexer);
10802 error_at (token->location, "%<else%> without a previous %<if%>");
10806 /* Parse the statement. */
10807 cp_parser_statement (parser, in_statement_expr, true, NULL);
10811 /* Parse a selection-statement.
10813 selection-statement:
10814 if ( condition ) statement
10815 if ( condition ) statement else statement
10816 switch ( condition ) statement
10818 Returns the new IF_STMT or SWITCH_STMT.
10820 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10821 is a (possibly labeled) if statement which is not enclosed in
10822 braces and has an else clause. This is used to implement
10823 -Wparentheses.
10825 CHAIN is a vector of if-else-if conditions. This is used to implement
10826 -Wduplicated-cond. */
10828 static tree
10829 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
10830 vec<tree> *chain)
10832 cp_token *token;
10833 enum rid keyword;
10834 token_indent_info guard_tinfo;
10836 if (if_p != NULL)
10837 *if_p = false;
10839 /* Peek at the next token. */
10840 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10841 guard_tinfo = get_token_indent_info (token);
10843 /* See what kind of keyword it is. */
10844 keyword = token->keyword;
10845 switch (keyword)
10847 case RID_IF:
10848 case RID_SWITCH:
10850 tree statement;
10851 tree condition;
10853 /* Look for the `('. */
10854 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10856 cp_parser_skip_to_end_of_statement (parser);
10857 return error_mark_node;
10860 /* Begin the selection-statement. */
10861 if (keyword == RID_IF)
10862 statement = begin_if_stmt ();
10863 else
10864 statement = begin_switch_stmt ();
10866 /* Parse the condition. */
10867 condition = cp_parser_condition (parser);
10868 /* Look for the `)'. */
10869 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10870 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10871 /*consume_paren=*/true);
10873 if (keyword == RID_IF)
10875 bool nested_if;
10876 unsigned char in_statement;
10878 /* Add the condition. */
10879 finish_if_stmt_cond (condition, statement);
10881 if (warn_duplicated_cond)
10882 warn_duplicated_cond_add_or_warn (token->location, condition,
10883 &chain);
10885 /* Parse the then-clause. */
10886 in_statement = parser->in_statement;
10887 parser->in_statement |= IN_IF_STMT;
10888 cp_parser_implicitly_scoped_statement (parser, &nested_if,
10889 guard_tinfo);
10890 parser->in_statement = in_statement;
10892 finish_then_clause (statement);
10894 /* If the next token is `else', parse the else-clause. */
10895 if (cp_lexer_next_token_is_keyword (parser->lexer,
10896 RID_ELSE))
10898 guard_tinfo
10899 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
10900 /* Consume the `else' keyword. */
10901 cp_lexer_consume_token (parser->lexer);
10902 if (warn_duplicated_cond)
10904 if (cp_lexer_next_token_is_keyword (parser->lexer,
10905 RID_IF)
10906 && chain == NULL)
10908 /* We've got "if (COND) else if (COND2)". Start
10909 the condition chain and add COND as the first
10910 element. */
10911 chain = new vec<tree> ();
10912 if (!CONSTANT_CLASS_P (condition)
10913 && !TREE_SIDE_EFFECTS (condition))
10915 /* Wrap it in a NOP_EXPR so that we can set the
10916 location of the condition. */
10917 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
10918 condition);
10919 SET_EXPR_LOCATION (e, token->location);
10920 chain->safe_push (e);
10923 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
10924 RID_IF))
10926 /* This is if-else without subsequent if. Zap the
10927 condition chain; we would have already warned at
10928 this point. */
10929 delete chain;
10930 chain = NULL;
10933 begin_else_clause (statement);
10934 /* Parse the else-clause. */
10935 cp_parser_implicitly_scoped_statement (parser, NULL,
10936 guard_tinfo, chain);
10938 finish_else_clause (statement);
10940 /* If we are currently parsing a then-clause, then
10941 IF_P will not be NULL. We set it to true to
10942 indicate that this if statement has an else clause.
10943 This may trigger the Wparentheses warning below
10944 when we get back up to the parent if statement. */
10945 if (if_p != NULL)
10946 *if_p = true;
10948 else
10950 /* This if statement does not have an else clause. If
10951 NESTED_IF is true, then the then-clause is an if
10952 statement which does have an else clause. We warn
10953 about the potential ambiguity. */
10954 if (nested_if)
10955 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10956 "suggest explicit braces to avoid ambiguous"
10957 " %<else%>");
10958 if (warn_duplicated_cond)
10960 /* We don't need the condition chain anymore. */
10961 delete chain;
10962 chain = NULL;
10966 /* Now we're all done with the if-statement. */
10967 finish_if_stmt (statement);
10969 else
10971 bool in_switch_statement_p;
10972 unsigned char in_statement;
10974 /* Add the condition. */
10975 finish_switch_cond (condition, statement);
10977 /* Parse the body of the switch-statement. */
10978 in_switch_statement_p = parser->in_switch_statement_p;
10979 in_statement = parser->in_statement;
10980 parser->in_switch_statement_p = true;
10981 parser->in_statement |= IN_SWITCH_STMT;
10982 cp_parser_implicitly_scoped_statement (parser, NULL,
10983 guard_tinfo);
10984 parser->in_switch_statement_p = in_switch_statement_p;
10985 parser->in_statement = in_statement;
10987 /* Now we're all done with the switch-statement. */
10988 finish_switch_stmt (statement);
10991 return statement;
10993 break;
10995 default:
10996 cp_parser_error (parser, "expected selection-statement");
10997 return error_mark_node;
11001 /* Parse a condition.
11003 condition:
11004 expression
11005 type-specifier-seq declarator = initializer-clause
11006 type-specifier-seq declarator braced-init-list
11008 GNU Extension:
11010 condition:
11011 type-specifier-seq declarator asm-specification [opt]
11012 attributes [opt] = assignment-expression
11014 Returns the expression that should be tested. */
11016 static tree
11017 cp_parser_condition (cp_parser* parser)
11019 cp_decl_specifier_seq type_specifiers;
11020 const char *saved_message;
11021 int declares_class_or_enum;
11023 /* Try the declaration first. */
11024 cp_parser_parse_tentatively (parser);
11025 /* New types are not allowed in the type-specifier-seq for a
11026 condition. */
11027 saved_message = parser->type_definition_forbidden_message;
11028 parser->type_definition_forbidden_message
11029 = G_("types may not be defined in conditions");
11030 /* Parse the type-specifier-seq. */
11031 cp_parser_decl_specifier_seq (parser,
11032 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11033 &type_specifiers,
11034 &declares_class_or_enum);
11035 /* Restore the saved message. */
11036 parser->type_definition_forbidden_message = saved_message;
11037 /* If all is well, we might be looking at a declaration. */
11038 if (!cp_parser_error_occurred (parser))
11040 tree decl;
11041 tree asm_specification;
11042 tree attributes;
11043 cp_declarator *declarator;
11044 tree initializer = NULL_TREE;
11046 /* Parse the declarator. */
11047 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11048 /*ctor_dtor_or_conv_p=*/NULL,
11049 /*parenthesized_p=*/NULL,
11050 /*member_p=*/false,
11051 /*friend_p=*/false);
11052 /* Parse the attributes. */
11053 attributes = cp_parser_attributes_opt (parser);
11054 /* Parse the asm-specification. */
11055 asm_specification = cp_parser_asm_specification_opt (parser);
11056 /* If the next token is not an `=' or '{', then we might still be
11057 looking at an expression. For example:
11059 if (A(a).x)
11061 looks like a decl-specifier-seq and a declarator -- but then
11062 there is no `=', so this is an expression. */
11063 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11064 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11065 cp_parser_simulate_error (parser);
11067 /* If we did see an `=' or '{', then we are looking at a declaration
11068 for sure. */
11069 if (cp_parser_parse_definitely (parser))
11071 tree pushed_scope;
11072 bool non_constant_p;
11073 bool flags = LOOKUP_ONLYCONVERTING;
11075 /* Create the declaration. */
11076 decl = start_decl (declarator, &type_specifiers,
11077 /*initialized_p=*/true,
11078 attributes, /*prefix_attributes=*/NULL_TREE,
11079 &pushed_scope);
11081 /* Parse the initializer. */
11082 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11084 initializer = cp_parser_braced_list (parser, &non_constant_p);
11085 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11086 flags = 0;
11088 else
11090 /* Consume the `='. */
11091 cp_parser_require (parser, CPP_EQ, RT_EQ);
11092 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11094 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11095 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11097 /* Process the initializer. */
11098 cp_finish_decl (decl,
11099 initializer, !non_constant_p,
11100 asm_specification,
11101 flags);
11103 if (pushed_scope)
11104 pop_scope (pushed_scope);
11106 return convert_from_reference (decl);
11109 /* If we didn't even get past the declarator successfully, we are
11110 definitely not looking at a declaration. */
11111 else
11112 cp_parser_abort_tentative_parse (parser);
11114 /* Otherwise, we are looking at an expression. */
11115 return cp_parser_expression (parser);
11118 /* Parses a for-statement or range-for-statement until the closing ')',
11119 not included. */
11121 static tree
11122 cp_parser_for (cp_parser *parser, bool ivdep)
11124 tree init, scope, decl;
11125 bool is_range_for;
11127 /* Begin the for-statement. */
11128 scope = begin_for_scope (&init);
11130 /* Parse the initialization. */
11131 is_range_for = cp_parser_for_init_statement (parser, &decl);
11133 if (is_range_for)
11134 return cp_parser_range_for (parser, scope, init, decl, ivdep);
11135 else
11136 return cp_parser_c_for (parser, scope, init, ivdep);
11139 static tree
11140 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11142 /* Normal for loop */
11143 tree condition = NULL_TREE;
11144 tree expression = NULL_TREE;
11145 tree stmt;
11147 stmt = begin_for_stmt (scope, init);
11148 /* The for-init-statement has already been parsed in
11149 cp_parser_for_init_statement, so no work is needed here. */
11150 finish_for_init_stmt (stmt);
11152 /* If there's a condition, process it. */
11153 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11154 condition = cp_parser_condition (parser);
11155 else if (ivdep)
11157 cp_parser_error (parser, "missing loop condition in loop with "
11158 "%<GCC ivdep%> pragma");
11159 condition = error_mark_node;
11161 finish_for_cond (condition, stmt, ivdep);
11162 /* Look for the `;'. */
11163 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11165 /* If there's an expression, process it. */
11166 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11167 expression = cp_parser_expression (parser);
11168 finish_for_expr (expression, stmt);
11170 return stmt;
11173 /* Tries to parse a range-based for-statement:
11175 range-based-for:
11176 decl-specifier-seq declarator : expression
11178 The decl-specifier-seq declarator and the `:' are already parsed by
11179 cp_parser_for_init_statement. If processing_template_decl it returns a
11180 newly created RANGE_FOR_STMT; if not, it is converted to a
11181 regular FOR_STMT. */
11183 static tree
11184 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11185 bool ivdep)
11187 tree stmt, range_expr;
11189 /* Get the range declaration momentarily out of the way so that
11190 the range expression doesn't clash with it. */
11191 if (range_decl != error_mark_node)
11192 pop_binding (DECL_NAME (range_decl), range_decl);
11194 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11196 bool expr_non_constant_p;
11197 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11199 else
11200 range_expr = cp_parser_expression (parser);
11202 /* Put the range declaration back into scope. */
11203 if (range_decl != error_mark_node)
11204 push_binding (DECL_NAME (range_decl), range_decl, current_binding_level);
11206 /* If in template, STMT is converted to a normal for-statement
11207 at instantiation. If not, it is done just ahead. */
11208 if (processing_template_decl)
11210 if (check_for_bare_parameter_packs (range_expr))
11211 range_expr = error_mark_node;
11212 stmt = begin_range_for_stmt (scope, init);
11213 if (ivdep)
11214 RANGE_FOR_IVDEP (stmt) = 1;
11215 finish_range_for_decl (stmt, range_decl, range_expr);
11216 if (!type_dependent_expression_p (range_expr)
11217 /* do_auto_deduction doesn't mess with template init-lists. */
11218 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11219 do_range_for_auto_deduction (range_decl, range_expr);
11221 else
11223 stmt = begin_for_stmt (scope, init);
11224 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
11226 return stmt;
11229 /* Subroutine of cp_convert_range_for: given the initializer expression,
11230 builds up the range temporary. */
11232 static tree
11233 build_range_temp (tree range_expr)
11235 tree range_type, range_temp;
11237 /* Find out the type deduced by the declaration
11238 `auto &&__range = range_expr'. */
11239 range_type = cp_build_reference_type (make_auto (), true);
11240 range_type = do_auto_deduction (range_type, range_expr,
11241 type_uses_auto (range_type));
11243 /* Create the __range variable. */
11244 range_temp = build_decl (input_location, VAR_DECL,
11245 get_identifier ("__for_range"), range_type);
11246 TREE_USED (range_temp) = 1;
11247 DECL_ARTIFICIAL (range_temp) = 1;
11249 return range_temp;
11252 /* Used by cp_parser_range_for in template context: we aren't going to
11253 do a full conversion yet, but we still need to resolve auto in the
11254 type of the for-range-declaration if present. This is basically
11255 a shortcut version of cp_convert_range_for. */
11257 static void
11258 do_range_for_auto_deduction (tree decl, tree range_expr)
11260 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11261 if (auto_node)
11263 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11264 range_temp = convert_from_reference (build_range_temp (range_expr));
11265 iter_type = (cp_parser_perform_range_for_lookup
11266 (range_temp, &begin_dummy, &end_dummy));
11267 if (iter_type)
11269 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11270 iter_type);
11271 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
11272 tf_warning_or_error);
11273 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11274 iter_decl, auto_node);
11279 /* Converts a range-based for-statement into a normal
11280 for-statement, as per the definition.
11282 for (RANGE_DECL : RANGE_EXPR)
11283 BLOCK
11285 should be equivalent to:
11288 auto &&__range = RANGE_EXPR;
11289 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11290 __begin != __end;
11291 ++__begin)
11293 RANGE_DECL = *__begin;
11294 BLOCK
11298 If RANGE_EXPR is an array:
11299 BEGIN_EXPR = __range
11300 END_EXPR = __range + ARRAY_SIZE(__range)
11301 Else if RANGE_EXPR has a member 'begin' or 'end':
11302 BEGIN_EXPR = __range.begin()
11303 END_EXPR = __range.end()
11304 Else:
11305 BEGIN_EXPR = begin(__range)
11306 END_EXPR = end(__range);
11308 If __range has a member 'begin' but not 'end', or vice versa, we must
11309 still use the second alternative (it will surely fail, however).
11310 When calling begin()/end() in the third alternative we must use
11311 argument dependent lookup, but always considering 'std' as an associated
11312 namespace. */
11314 tree
11315 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11316 bool ivdep)
11318 tree begin, end;
11319 tree iter_type, begin_expr, end_expr;
11320 tree condition, expression;
11322 if (range_decl == error_mark_node || range_expr == error_mark_node)
11323 /* If an error happened previously do nothing or else a lot of
11324 unhelpful errors would be issued. */
11325 begin_expr = end_expr = iter_type = error_mark_node;
11326 else
11328 tree range_temp;
11330 if (VAR_P (range_expr)
11331 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11332 /* Can't bind a reference to an array of runtime bound. */
11333 range_temp = range_expr;
11334 else
11336 range_temp = build_range_temp (range_expr);
11337 pushdecl (range_temp);
11338 cp_finish_decl (range_temp, range_expr,
11339 /*is_constant_init*/false, NULL_TREE,
11340 LOOKUP_ONLYCONVERTING);
11341 range_temp = convert_from_reference (range_temp);
11343 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11344 &begin_expr, &end_expr);
11347 /* The new for initialization statement. */
11348 begin = build_decl (input_location, VAR_DECL,
11349 get_identifier ("__for_begin"), iter_type);
11350 TREE_USED (begin) = 1;
11351 DECL_ARTIFICIAL (begin) = 1;
11352 pushdecl (begin);
11353 cp_finish_decl (begin, begin_expr,
11354 /*is_constant_init*/false, NULL_TREE,
11355 LOOKUP_ONLYCONVERTING);
11357 end = build_decl (input_location, VAR_DECL,
11358 get_identifier ("__for_end"), iter_type);
11359 TREE_USED (end) = 1;
11360 DECL_ARTIFICIAL (end) = 1;
11361 pushdecl (end);
11362 cp_finish_decl (end, end_expr,
11363 /*is_constant_init*/false, NULL_TREE,
11364 LOOKUP_ONLYCONVERTING);
11366 finish_for_init_stmt (statement);
11368 /* The new for condition. */
11369 condition = build_x_binary_op (input_location, NE_EXPR,
11370 begin, ERROR_MARK,
11371 end, ERROR_MARK,
11372 NULL, tf_warning_or_error);
11373 finish_for_cond (condition, statement, ivdep);
11375 /* The new increment expression. */
11376 expression = finish_unary_op_expr (input_location,
11377 PREINCREMENT_EXPR, begin,
11378 tf_warning_or_error);
11379 finish_for_expr (expression, statement);
11381 /* The declaration is initialized with *__begin inside the loop body. */
11382 cp_finish_decl (range_decl,
11383 build_x_indirect_ref (input_location, begin, RO_NULL,
11384 tf_warning_or_error),
11385 /*is_constant_init*/false, NULL_TREE,
11386 LOOKUP_ONLYCONVERTING);
11388 return statement;
11391 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11392 We need to solve both at the same time because the method used
11393 depends on the existence of members begin or end.
11394 Returns the type deduced for the iterator expression. */
11396 static tree
11397 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11399 if (error_operand_p (range))
11401 *begin = *end = error_mark_node;
11402 return error_mark_node;
11405 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11407 error ("range-based %<for%> expression of type %qT "
11408 "has incomplete type", TREE_TYPE (range));
11409 *begin = *end = error_mark_node;
11410 return error_mark_node;
11412 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11414 /* If RANGE is an array, we will use pointer arithmetic. */
11415 *begin = range;
11416 *end = build_binary_op (input_location, PLUS_EXPR,
11417 range,
11418 array_type_nelts_top (TREE_TYPE (range)),
11420 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
11422 else
11424 /* If it is not an array, we must do a bit of magic. */
11425 tree id_begin, id_end;
11426 tree member_begin, member_end;
11428 *begin = *end = error_mark_node;
11430 id_begin = get_identifier ("begin");
11431 id_end = get_identifier ("end");
11432 member_begin = lookup_member (TREE_TYPE (range), id_begin,
11433 /*protect=*/2, /*want_type=*/false,
11434 tf_warning_or_error);
11435 member_end = lookup_member (TREE_TYPE (range), id_end,
11436 /*protect=*/2, /*want_type=*/false,
11437 tf_warning_or_error);
11439 if (member_begin != NULL_TREE || member_end != NULL_TREE)
11441 /* Use the member functions. */
11442 if (member_begin != NULL_TREE)
11443 *begin = cp_parser_range_for_member_function (range, id_begin);
11444 else
11445 error ("range-based %<for%> expression of type %qT has an "
11446 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11448 if (member_end != NULL_TREE)
11449 *end = cp_parser_range_for_member_function (range, id_end);
11450 else
11451 error ("range-based %<for%> expression of type %qT has a "
11452 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
11454 else
11456 /* Use global functions with ADL. */
11457 vec<tree, va_gc> *vec;
11458 vec = make_tree_vector ();
11460 vec_safe_push (vec, range);
11462 member_begin = perform_koenig_lookup (id_begin, vec,
11463 tf_warning_or_error);
11464 *begin = finish_call_expr (member_begin, &vec, false, true,
11465 tf_warning_or_error);
11466 member_end = perform_koenig_lookup (id_end, vec,
11467 tf_warning_or_error);
11468 *end = finish_call_expr (member_end, &vec, false, true,
11469 tf_warning_or_error);
11471 release_tree_vector (vec);
11474 /* Last common checks. */
11475 if (*begin == error_mark_node || *end == error_mark_node)
11477 /* If one of the expressions is an error do no more checks. */
11478 *begin = *end = error_mark_node;
11479 return error_mark_node;
11481 else if (type_dependent_expression_p (*begin)
11482 || type_dependent_expression_p (*end))
11483 /* Can happen, when, eg, in a template context, Koenig lookup
11484 can't resolve begin/end (c++/58503). */
11485 return NULL_TREE;
11486 else
11488 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
11489 /* The unqualified type of the __begin and __end temporaries should
11490 be the same, as required by the multiple auto declaration. */
11491 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
11492 error ("inconsistent begin/end types in range-based %<for%> "
11493 "statement: %qT and %qT",
11494 TREE_TYPE (*begin), TREE_TYPE (*end));
11495 return iter_type;
11500 /* Helper function for cp_parser_perform_range_for_lookup.
11501 Builds a tree for RANGE.IDENTIFIER(). */
11503 static tree
11504 cp_parser_range_for_member_function (tree range, tree identifier)
11506 tree member, res;
11507 vec<tree, va_gc> *vec;
11509 member = finish_class_member_access_expr (range, identifier,
11510 false, tf_warning_or_error);
11511 if (member == error_mark_node)
11512 return error_mark_node;
11514 vec = make_tree_vector ();
11515 res = finish_call_expr (member, &vec,
11516 /*disallow_virtual=*/false,
11517 /*koenig_p=*/false,
11518 tf_warning_or_error);
11519 release_tree_vector (vec);
11520 return res;
11523 /* Parse an iteration-statement.
11525 iteration-statement:
11526 while ( condition ) statement
11527 do statement while ( expression ) ;
11528 for ( for-init-statement condition [opt] ; expression [opt] )
11529 statement
11531 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
11533 static tree
11534 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
11536 cp_token *token;
11537 enum rid keyword;
11538 tree statement;
11539 unsigned char in_statement;
11540 token_indent_info guard_tinfo;
11542 /* Peek at the next token. */
11543 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
11544 if (!token)
11545 return error_mark_node;
11547 guard_tinfo = get_token_indent_info (token);
11549 /* Remember whether or not we are already within an iteration
11550 statement. */
11551 in_statement = parser->in_statement;
11553 /* See what kind of keyword it is. */
11554 keyword = token->keyword;
11555 switch (keyword)
11557 case RID_WHILE:
11559 tree condition;
11561 /* Begin the while-statement. */
11562 statement = begin_while_stmt ();
11563 /* Look for the `('. */
11564 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11565 /* Parse the condition. */
11566 condition = cp_parser_condition (parser);
11567 finish_while_stmt_cond (condition, statement, ivdep);
11568 /* Look for the `)'. */
11569 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11570 /* Parse the dependent statement. */
11571 parser->in_statement = IN_ITERATION_STMT;
11572 cp_parser_already_scoped_statement (parser, guard_tinfo);
11573 parser->in_statement = in_statement;
11574 /* We're done with the while-statement. */
11575 finish_while_stmt (statement);
11577 break;
11579 case RID_DO:
11581 tree expression;
11583 /* Begin the do-statement. */
11584 statement = begin_do_stmt ();
11585 /* Parse the body of the do-statement. */
11586 parser->in_statement = IN_ITERATION_STMT;
11587 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
11588 parser->in_statement = in_statement;
11589 finish_do_body (statement);
11590 /* Look for the `while' keyword. */
11591 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
11592 /* Look for the `('. */
11593 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11594 /* Parse the expression. */
11595 expression = cp_parser_expression (parser);
11596 /* We're done with the do-statement. */
11597 finish_do_stmt (expression, statement, ivdep);
11598 /* Look for the `)'. */
11599 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11600 /* Look for the `;'. */
11601 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11603 break;
11605 case RID_FOR:
11607 /* Look for the `('. */
11608 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11610 statement = cp_parser_for (parser, ivdep);
11612 /* Look for the `)'. */
11613 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11615 /* Parse the body of the for-statement. */
11616 parser->in_statement = IN_ITERATION_STMT;
11617 cp_parser_already_scoped_statement (parser, guard_tinfo);
11618 parser->in_statement = in_statement;
11620 /* We're done with the for-statement. */
11621 finish_for_stmt (statement);
11623 break;
11625 default:
11626 cp_parser_error (parser, "expected iteration-statement");
11627 statement = error_mark_node;
11628 break;
11631 return statement;
11634 /* Parse a for-init-statement or the declarator of a range-based-for.
11635 Returns true if a range-based-for declaration is seen.
11637 for-init-statement:
11638 expression-statement
11639 simple-declaration */
11641 static bool
11642 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
11644 /* If the next token is a `;', then we have an empty
11645 expression-statement. Grammatically, this is also a
11646 simple-declaration, but an invalid one, because it does not
11647 declare anything. Therefore, if we did not handle this case
11648 specially, we would issue an error message about an invalid
11649 declaration. */
11650 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11652 bool is_range_for = false;
11653 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11655 /* A colon is used in range-based for. */
11656 parser->colon_corrects_to_scope_p = false;
11658 /* We're going to speculatively look for a declaration, falling back
11659 to an expression, if necessary. */
11660 cp_parser_parse_tentatively (parser);
11661 /* Parse the declaration. */
11662 cp_parser_simple_declaration (parser,
11663 /*function_definition_allowed_p=*/false,
11664 decl);
11665 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11666 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11668 /* It is a range-for, consume the ':' */
11669 cp_lexer_consume_token (parser->lexer);
11670 is_range_for = true;
11671 if (cxx_dialect < cxx11)
11673 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11674 "range-based %<for%> loops only available with "
11675 "-std=c++11 or -std=gnu++11");
11676 *decl = error_mark_node;
11679 else
11680 /* The ';' is not consumed yet because we told
11681 cp_parser_simple_declaration not to. */
11682 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11684 if (cp_parser_parse_definitely (parser))
11685 return is_range_for;
11686 /* If the tentative parse failed, then we shall need to look for an
11687 expression-statement. */
11689 /* If we are here, it is an expression-statement. */
11690 cp_parser_expression_statement (parser, NULL_TREE);
11691 return false;
11694 /* Parse a jump-statement.
11696 jump-statement:
11697 break ;
11698 continue ;
11699 return expression [opt] ;
11700 return braced-init-list ;
11701 goto identifier ;
11703 GNU extension:
11705 jump-statement:
11706 goto * expression ;
11708 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
11710 static tree
11711 cp_parser_jump_statement (cp_parser* parser)
11713 tree statement = error_mark_node;
11714 cp_token *token;
11715 enum rid keyword;
11716 unsigned char in_statement;
11718 /* Peek at the next token. */
11719 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
11720 if (!token)
11721 return error_mark_node;
11723 /* See what kind of keyword it is. */
11724 keyword = token->keyword;
11725 switch (keyword)
11727 case RID_BREAK:
11728 in_statement = parser->in_statement & ~IN_IF_STMT;
11729 switch (in_statement)
11731 case 0:
11732 error_at (token->location, "break statement not within loop or switch");
11733 break;
11734 default:
11735 gcc_assert ((in_statement & IN_SWITCH_STMT)
11736 || in_statement == IN_ITERATION_STMT);
11737 statement = finish_break_stmt ();
11738 if (in_statement == IN_ITERATION_STMT)
11739 break_maybe_infinite_loop ();
11740 break;
11741 case IN_OMP_BLOCK:
11742 error_at (token->location, "invalid exit from OpenMP structured block");
11743 break;
11744 case IN_OMP_FOR:
11745 error_at (token->location, "break statement used with OpenMP for loop");
11746 break;
11747 case IN_CILK_SIMD_FOR:
11748 error_at (token->location, "break statement used with Cilk Plus for loop");
11749 break;
11751 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11752 break;
11754 case RID_CONTINUE:
11755 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
11757 case 0:
11758 error_at (token->location, "continue statement not within a loop");
11759 break;
11760 case IN_CILK_SIMD_FOR:
11761 error_at (token->location,
11762 "continue statement within %<#pragma simd%> loop body");
11763 /* Fall through. */
11764 case IN_ITERATION_STMT:
11765 case IN_OMP_FOR:
11766 statement = finish_continue_stmt ();
11767 break;
11768 case IN_OMP_BLOCK:
11769 error_at (token->location, "invalid exit from OpenMP structured block");
11770 break;
11771 default:
11772 gcc_unreachable ();
11774 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11775 break;
11777 case RID_RETURN:
11779 tree expr;
11780 bool expr_non_constant_p;
11782 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11784 cp_lexer_set_source_position (parser->lexer);
11785 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11786 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11788 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11789 expr = cp_parser_expression (parser);
11790 else
11791 /* If the next token is a `;', then there is no
11792 expression. */
11793 expr = NULL_TREE;
11794 /* Build the return-statement. */
11795 statement = finish_return_stmt (expr);
11796 /* Look for the final `;'. */
11797 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11799 break;
11801 case RID_GOTO:
11802 if (parser->in_function_body
11803 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11805 error ("%<goto%> in %<constexpr%> function");
11806 cp_function_chain->invalid_constexpr = true;
11809 /* Create the goto-statement. */
11810 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11812 /* Issue a warning about this use of a GNU extension. */
11813 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11814 /* Consume the '*' token. */
11815 cp_lexer_consume_token (parser->lexer);
11816 /* Parse the dependent expression. */
11817 finish_goto_stmt (cp_parser_expression (parser));
11819 else
11820 finish_goto_stmt (cp_parser_identifier (parser));
11821 /* Look for the final `;'. */
11822 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11823 break;
11825 default:
11826 cp_parser_error (parser, "expected jump-statement");
11827 break;
11830 return statement;
11833 /* Parse a declaration-statement.
11835 declaration-statement:
11836 block-declaration */
11838 static void
11839 cp_parser_declaration_statement (cp_parser* parser)
11841 void *p;
11843 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11844 p = obstack_alloc (&declarator_obstack, 0);
11846 /* Parse the block-declaration. */
11847 cp_parser_block_declaration (parser, /*statement_p=*/true);
11849 /* Free any declarators allocated. */
11850 obstack_free (&declarator_obstack, p);
11853 /* Some dependent statements (like `if (cond) statement'), are
11854 implicitly in their own scope. In other words, if the statement is
11855 a single statement (as opposed to a compound-statement), it is
11856 none-the-less treated as if it were enclosed in braces. Any
11857 declarations appearing in the dependent statement are out of scope
11858 after control passes that point. This function parses a statement,
11859 but ensures that is in its own scope, even if it is not a
11860 compound-statement.
11862 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11863 is a (possibly labeled) if statement which is not enclosed in
11864 braces and has an else clause. This is used to implement
11865 -Wparentheses.
11867 CHAIN is a vector of if-else-if conditions. This is used to implement
11868 -Wduplicated-cond.
11870 Returns the new statement. */
11872 static tree
11873 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
11874 const token_indent_info &guard_tinfo,
11875 vec<tree> *chain)
11877 tree statement;
11878 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
11879 token_indent_info body_tinfo
11880 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11882 if (if_p != NULL)
11883 *if_p = false;
11885 /* Mark if () ; with a special NOP_EXPR. */
11886 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11888 cp_lexer_consume_token (parser->lexer);
11889 statement = add_stmt (build_empty_stmt (body_loc));
11891 if (guard_tinfo.keyword == RID_IF
11892 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
11893 warning_at (body_loc, OPT_Wempty_body,
11894 "suggest braces around empty body in an %<if%> statement");
11895 else if (guard_tinfo.keyword == RID_ELSE)
11896 warning_at (body_loc, OPT_Wempty_body,
11897 "suggest braces around empty body in an %<else%> statement");
11899 /* if a compound is opened, we simply parse the statement directly. */
11900 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11901 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
11902 /* If the token is not a `{', then we must take special action. */
11903 else
11905 /* Create a compound-statement. */
11906 statement = begin_compound_stmt (0);
11907 /* Parse the dependent-statement. */
11908 cp_parser_statement (parser, NULL_TREE, false, if_p, chain);
11909 /* Finish the dummy compound-statement. */
11910 finish_compound_stmt (statement);
11913 token_indent_info next_tinfo
11914 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11915 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
11917 /* Return the statement. */
11918 return statement;
11921 /* For some dependent statements (like `while (cond) statement'), we
11922 have already created a scope. Therefore, even if the dependent
11923 statement is a compound-statement, we do not want to create another
11924 scope. */
11926 static void
11927 cp_parser_already_scoped_statement (cp_parser* parser,
11928 const token_indent_info &guard_tinfo)
11930 /* If the token is a `{', then we must take special action. */
11931 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11933 token_indent_info body_tinfo
11934 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11936 cp_parser_statement (parser, NULL_TREE, false, NULL);
11937 token_indent_info next_tinfo
11938 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11939 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
11941 else
11943 /* Avoid calling cp_parser_compound_statement, so that we
11944 don't create a new scope. Do everything else by hand. */
11945 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11946 /* If the next keyword is `__label__' we have a label declaration. */
11947 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11948 cp_parser_label_declaration (parser);
11949 /* Parse an (optional) statement-seq. */
11950 cp_parser_statement_seq_opt (parser, NULL_TREE);
11951 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11955 /* Declarations [gram.dcl.dcl] */
11957 /* Parse an optional declaration-sequence.
11959 declaration-seq:
11960 declaration
11961 declaration-seq declaration */
11963 static void
11964 cp_parser_declaration_seq_opt (cp_parser* parser)
11966 while (true)
11968 cp_token *token;
11970 token = cp_lexer_peek_token (parser->lexer);
11972 if (token->type == CPP_CLOSE_BRACE
11973 || token->type == CPP_EOF
11974 || token->type == CPP_PRAGMA_EOL)
11975 break;
11977 if (token->type == CPP_SEMICOLON)
11979 /* A declaration consisting of a single semicolon is
11980 invalid. Allow it unless we're being pedantic. */
11981 cp_lexer_consume_token (parser->lexer);
11982 if (!in_system_header_at (input_location))
11983 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11984 continue;
11987 /* If we're entering or exiting a region that's implicitly
11988 extern "C", modify the lang context appropriately. */
11989 if (!parser->implicit_extern_c && token->implicit_extern_c)
11991 push_lang_context (lang_name_c);
11992 parser->implicit_extern_c = true;
11994 else if (parser->implicit_extern_c && !token->implicit_extern_c)
11996 pop_lang_context ();
11997 parser->implicit_extern_c = false;
12000 if (token->type == CPP_PRAGMA)
12002 /* A top-level declaration can consist solely of a #pragma.
12003 A nested declaration cannot, so this is done here and not
12004 in cp_parser_declaration. (A #pragma at block scope is
12005 handled in cp_parser_statement.) */
12006 cp_parser_pragma (parser, pragma_external);
12007 continue;
12010 /* Parse the declaration itself. */
12011 cp_parser_declaration (parser);
12015 /* Parse a declaration.
12017 declaration:
12018 block-declaration
12019 function-definition
12020 template-declaration
12021 explicit-instantiation
12022 explicit-specialization
12023 linkage-specification
12024 namespace-definition
12026 GNU extension:
12028 declaration:
12029 __extension__ declaration */
12031 static void
12032 cp_parser_declaration (cp_parser* parser)
12034 cp_token token1;
12035 cp_token token2;
12036 int saved_pedantic;
12037 void *p;
12038 tree attributes = NULL_TREE;
12040 /* Check for the `__extension__' keyword. */
12041 if (cp_parser_extension_opt (parser, &saved_pedantic))
12043 /* Parse the qualified declaration. */
12044 cp_parser_declaration (parser);
12045 /* Restore the PEDANTIC flag. */
12046 pedantic = saved_pedantic;
12048 return;
12051 /* Try to figure out what kind of declaration is present. */
12052 token1 = *cp_lexer_peek_token (parser->lexer);
12054 if (token1.type != CPP_EOF)
12055 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12056 else
12058 token2.type = CPP_EOF;
12059 token2.keyword = RID_MAX;
12062 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12063 p = obstack_alloc (&declarator_obstack, 0);
12065 /* If the next token is `extern' and the following token is a string
12066 literal, then we have a linkage specification. */
12067 if (token1.keyword == RID_EXTERN
12068 && cp_parser_is_pure_string_literal (&token2))
12069 cp_parser_linkage_specification (parser);
12070 /* If the next token is `template', then we have either a template
12071 declaration, an explicit instantiation, or an explicit
12072 specialization. */
12073 else if (token1.keyword == RID_TEMPLATE)
12075 /* `template <>' indicates a template specialization. */
12076 if (token2.type == CPP_LESS
12077 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12078 cp_parser_explicit_specialization (parser);
12079 /* `template <' indicates a template declaration. */
12080 else if (token2.type == CPP_LESS)
12081 cp_parser_template_declaration (parser, /*member_p=*/false);
12082 /* Anything else must be an explicit instantiation. */
12083 else
12084 cp_parser_explicit_instantiation (parser);
12086 /* If the next token is `export', then we have a template
12087 declaration. */
12088 else if (token1.keyword == RID_EXPORT)
12089 cp_parser_template_declaration (parser, /*member_p=*/false);
12090 /* If the next token is `extern', 'static' or 'inline' and the one
12091 after that is `template', we have a GNU extended explicit
12092 instantiation directive. */
12093 else if (cp_parser_allow_gnu_extensions_p (parser)
12094 && (token1.keyword == RID_EXTERN
12095 || token1.keyword == RID_STATIC
12096 || token1.keyword == RID_INLINE)
12097 && token2.keyword == RID_TEMPLATE)
12098 cp_parser_explicit_instantiation (parser);
12099 /* If the next token is `namespace', check for a named or unnamed
12100 namespace definition. */
12101 else if (token1.keyword == RID_NAMESPACE
12102 && (/* A named namespace definition. */
12103 (token2.type == CPP_NAME
12104 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12105 != CPP_EQ))
12106 || (token2.type == CPP_OPEN_SQUARE
12107 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12108 == CPP_OPEN_SQUARE)
12109 /* An unnamed namespace definition. */
12110 || token2.type == CPP_OPEN_BRACE
12111 || token2.keyword == RID_ATTRIBUTE))
12112 cp_parser_namespace_definition (parser);
12113 /* An inline (associated) namespace definition. */
12114 else if (token1.keyword == RID_INLINE
12115 && token2.keyword == RID_NAMESPACE)
12116 cp_parser_namespace_definition (parser);
12117 /* Objective-C++ declaration/definition. */
12118 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12119 cp_parser_objc_declaration (parser, NULL_TREE);
12120 else if (c_dialect_objc ()
12121 && token1.keyword == RID_ATTRIBUTE
12122 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12123 cp_parser_objc_declaration (parser, attributes);
12124 /* At this point we may have a template declared by a concept
12125 introduction. */
12126 else if (flag_concepts
12127 && cp_parser_template_declaration_after_export (parser,
12128 /*member_p=*/false))
12129 /* We did. */;
12130 else
12131 /* Try to parse a block-declaration, or a function-definition. */
12132 cp_parser_block_declaration (parser, /*statement_p=*/false);
12134 /* Free any declarators allocated. */
12135 obstack_free (&declarator_obstack, p);
12138 /* Parse a block-declaration.
12140 block-declaration:
12141 simple-declaration
12142 asm-definition
12143 namespace-alias-definition
12144 using-declaration
12145 using-directive
12147 GNU Extension:
12149 block-declaration:
12150 __extension__ block-declaration
12152 C++0x Extension:
12154 block-declaration:
12155 static_assert-declaration
12157 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12158 part of a declaration-statement. */
12160 static void
12161 cp_parser_block_declaration (cp_parser *parser,
12162 bool statement_p)
12164 cp_token *token1;
12165 int saved_pedantic;
12167 /* Check for the `__extension__' keyword. */
12168 if (cp_parser_extension_opt (parser, &saved_pedantic))
12170 /* Parse the qualified declaration. */
12171 cp_parser_block_declaration (parser, statement_p);
12172 /* Restore the PEDANTIC flag. */
12173 pedantic = saved_pedantic;
12175 return;
12178 /* Peek at the next token to figure out which kind of declaration is
12179 present. */
12180 token1 = cp_lexer_peek_token (parser->lexer);
12182 /* If the next keyword is `asm', we have an asm-definition. */
12183 if (token1->keyword == RID_ASM)
12185 if (statement_p)
12186 cp_parser_commit_to_tentative_parse (parser);
12187 cp_parser_asm_definition (parser);
12189 /* If the next keyword is `namespace', we have a
12190 namespace-alias-definition. */
12191 else if (token1->keyword == RID_NAMESPACE)
12192 cp_parser_namespace_alias_definition (parser);
12193 /* If the next keyword is `using', we have a
12194 using-declaration, a using-directive, or an alias-declaration. */
12195 else if (token1->keyword == RID_USING)
12197 cp_token *token2;
12199 if (statement_p)
12200 cp_parser_commit_to_tentative_parse (parser);
12201 /* If the token after `using' is `namespace', then we have a
12202 using-directive. */
12203 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12204 if (token2->keyword == RID_NAMESPACE)
12205 cp_parser_using_directive (parser);
12206 /* If the second token after 'using' is '=', then we have an
12207 alias-declaration. */
12208 else if (cxx_dialect >= cxx11
12209 && token2->type == CPP_NAME
12210 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12211 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12212 cp_parser_alias_declaration (parser);
12213 /* Otherwise, it's a using-declaration. */
12214 else
12215 cp_parser_using_declaration (parser,
12216 /*access_declaration_p=*/false);
12218 /* If the next keyword is `__label__' we have a misplaced label
12219 declaration. */
12220 else if (token1->keyword == RID_LABEL)
12222 cp_lexer_consume_token (parser->lexer);
12223 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12224 cp_parser_skip_to_end_of_statement (parser);
12225 /* If the next token is now a `;', consume it. */
12226 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12227 cp_lexer_consume_token (parser->lexer);
12229 /* If the next token is `static_assert' we have a static assertion. */
12230 else if (token1->keyword == RID_STATIC_ASSERT)
12231 cp_parser_static_assert (parser, /*member_p=*/false);
12232 /* Anything else must be a simple-declaration. */
12233 else
12234 cp_parser_simple_declaration (parser, !statement_p,
12235 /*maybe_range_for_decl*/NULL);
12238 /* Parse a simple-declaration.
12240 simple-declaration:
12241 decl-specifier-seq [opt] init-declarator-list [opt] ;
12243 init-declarator-list:
12244 init-declarator
12245 init-declarator-list , init-declarator
12247 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12248 function-definition as a simple-declaration.
12250 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12251 parsed declaration if it is an uninitialized single declarator not followed
12252 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12253 if present, will not be consumed. */
12255 static void
12256 cp_parser_simple_declaration (cp_parser* parser,
12257 bool function_definition_allowed_p,
12258 tree *maybe_range_for_decl)
12260 cp_decl_specifier_seq decl_specifiers;
12261 int declares_class_or_enum;
12262 bool saw_declarator;
12263 location_t comma_loc = UNKNOWN_LOCATION;
12264 location_t init_loc = UNKNOWN_LOCATION;
12266 if (maybe_range_for_decl)
12267 *maybe_range_for_decl = NULL_TREE;
12269 /* Defer access checks until we know what is being declared; the
12270 checks for names appearing in the decl-specifier-seq should be
12271 done as if we were in the scope of the thing being declared. */
12272 push_deferring_access_checks (dk_deferred);
12274 /* Parse the decl-specifier-seq. We have to keep track of whether
12275 or not the decl-specifier-seq declares a named class or
12276 enumeration type, since that is the only case in which the
12277 init-declarator-list is allowed to be empty.
12279 [dcl.dcl]
12281 In a simple-declaration, the optional init-declarator-list can be
12282 omitted only when declaring a class or enumeration, that is when
12283 the decl-specifier-seq contains either a class-specifier, an
12284 elaborated-type-specifier, or an enum-specifier. */
12285 cp_parser_decl_specifier_seq (parser,
12286 CP_PARSER_FLAGS_OPTIONAL,
12287 &decl_specifiers,
12288 &declares_class_or_enum);
12289 /* We no longer need to defer access checks. */
12290 stop_deferring_access_checks ();
12292 /* In a block scope, a valid declaration must always have a
12293 decl-specifier-seq. By not trying to parse declarators, we can
12294 resolve the declaration/expression ambiguity more quickly. */
12295 if (!function_definition_allowed_p
12296 && !decl_specifiers.any_specifiers_p)
12298 cp_parser_error (parser, "expected declaration");
12299 goto done;
12302 /* If the next two tokens are both identifiers, the code is
12303 erroneous. The usual cause of this situation is code like:
12305 T t;
12307 where "T" should name a type -- but does not. */
12308 if (!decl_specifiers.any_type_specifiers_p
12309 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12311 /* If parsing tentatively, we should commit; we really are
12312 looking at a declaration. */
12313 cp_parser_commit_to_tentative_parse (parser);
12314 /* Give up. */
12315 goto done;
12318 /* If we have seen at least one decl-specifier, and the next token
12319 is not a parenthesis, then we must be looking at a declaration.
12320 (After "int (" we might be looking at a functional cast.) */
12321 if (decl_specifiers.any_specifiers_p
12322 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12323 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12324 && !cp_parser_error_occurred (parser))
12325 cp_parser_commit_to_tentative_parse (parser);
12327 tree last_type, auto_node;
12329 last_type = NULL_TREE;
12330 auto_node = type_uses_auto (decl_specifiers.type);
12332 /* Keep going until we hit the `;' at the end of the simple
12333 declaration. */
12334 saw_declarator = false;
12335 while (cp_lexer_next_token_is_not (parser->lexer,
12336 CPP_SEMICOLON))
12338 cp_token *token;
12339 bool function_definition_p;
12340 tree decl;
12342 if (saw_declarator)
12344 /* If we are processing next declarator, comma is expected */
12345 token = cp_lexer_peek_token (parser->lexer);
12346 gcc_assert (token->type == CPP_COMMA);
12347 cp_lexer_consume_token (parser->lexer);
12348 if (maybe_range_for_decl)
12350 *maybe_range_for_decl = error_mark_node;
12351 if (comma_loc == UNKNOWN_LOCATION)
12352 comma_loc = token->location;
12355 else
12356 saw_declarator = true;
12358 /* Parse the init-declarator. */
12359 decl = cp_parser_init_declarator (parser, &decl_specifiers,
12360 /*checks=*/NULL,
12361 function_definition_allowed_p,
12362 /*member_p=*/false,
12363 declares_class_or_enum,
12364 &function_definition_p,
12365 maybe_range_for_decl,
12366 &init_loc);
12367 /* If an error occurred while parsing tentatively, exit quickly.
12368 (That usually happens when in the body of a function; each
12369 statement is treated as a declaration-statement until proven
12370 otherwise.) */
12371 if (cp_parser_error_occurred (parser))
12372 goto done;
12374 if (auto_node)
12376 tree type = TREE_TYPE (decl);
12377 if (last_type && !same_type_p (type, last_type))
12379 /* If the list of declarators contains more than one declarator,
12380 the type of each declared variable is determined as described
12381 above. If the type deduced for the template parameter U is not
12382 the same in each deduction, the program is ill-formed. */
12383 error_at (decl_specifiers.locations[ds_type_spec],
12384 "inconsistent deduction for %qT: %qT and then %qT",
12385 decl_specifiers.type, last_type, type);
12386 auto_node = NULL_TREE;
12388 last_type = type;
12391 /* Handle function definitions specially. */
12392 if (function_definition_p)
12394 /* If the next token is a `,', then we are probably
12395 processing something like:
12397 void f() {}, *p;
12399 which is erroneous. */
12400 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12402 cp_token *token = cp_lexer_peek_token (parser->lexer);
12403 error_at (token->location,
12404 "mixing"
12405 " declarations and function-definitions is forbidden");
12407 /* Otherwise, we're done with the list of declarators. */
12408 else
12410 pop_deferring_access_checks ();
12411 return;
12414 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
12415 *maybe_range_for_decl = decl;
12416 /* The next token should be either a `,' or a `;'. */
12417 token = cp_lexer_peek_token (parser->lexer);
12418 /* If it's a `,', there are more declarators to come. */
12419 if (token->type == CPP_COMMA)
12420 /* will be consumed next time around */;
12421 /* If it's a `;', we are done. */
12422 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12423 break;
12424 /* Anything else is an error. */
12425 else
12427 /* If we have already issued an error message we don't need
12428 to issue another one. */
12429 if ((decl != error_mark_node
12430 && DECL_INITIAL (decl) != error_mark_node)
12431 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12432 cp_parser_error (parser, "expected %<,%> or %<;%>");
12433 /* Skip tokens until we reach the end of the statement. */
12434 cp_parser_skip_to_end_of_statement (parser);
12435 /* If the next token is now a `;', consume it. */
12436 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12437 cp_lexer_consume_token (parser->lexer);
12438 goto done;
12440 /* After the first time around, a function-definition is not
12441 allowed -- even if it was OK at first. For example:
12443 int i, f() {}
12445 is not valid. */
12446 function_definition_allowed_p = false;
12449 /* Issue an error message if no declarators are present, and the
12450 decl-specifier-seq does not itself declare a class or
12451 enumeration: [dcl.dcl]/3. */
12452 if (!saw_declarator)
12454 if (cp_parser_declares_only_class_p (parser))
12456 if (!declares_class_or_enum
12457 && decl_specifiers.type
12458 && OVERLOAD_TYPE_P (decl_specifiers.type))
12459 /* Ensure an error is issued anyway when finish_decltype_type,
12460 called via cp_parser_decl_specifier_seq, returns a class or
12461 an enumeration (c++/51786). */
12462 decl_specifiers.type = NULL_TREE;
12463 shadow_tag (&decl_specifiers);
12465 /* Perform any deferred access checks. */
12466 perform_deferred_access_checks (tf_warning_or_error);
12469 /* Consume the `;'. */
12470 if (!maybe_range_for_decl)
12471 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12472 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12474 if (init_loc != UNKNOWN_LOCATION)
12475 error_at (init_loc, "initializer in range-based %<for%> loop");
12476 if (comma_loc != UNKNOWN_LOCATION)
12477 error_at (comma_loc,
12478 "multiple declarations in range-based %<for%> loop");
12481 done:
12482 pop_deferring_access_checks ();
12485 /* Parse a decl-specifier-seq.
12487 decl-specifier-seq:
12488 decl-specifier-seq [opt] decl-specifier
12489 decl-specifier attribute-specifier-seq [opt] (C++11)
12491 decl-specifier:
12492 storage-class-specifier
12493 type-specifier
12494 function-specifier
12495 friend
12496 typedef
12498 GNU Extension:
12500 decl-specifier:
12501 attributes
12503 Concepts Extension:
12505 decl-specifier:
12506 concept
12508 Set *DECL_SPECS to a representation of the decl-specifier-seq.
12510 The parser flags FLAGS is used to control type-specifier parsing.
12512 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
12513 flags:
12515 1: one of the decl-specifiers is an elaborated-type-specifier
12516 (i.e., a type declaration)
12517 2: one of the decl-specifiers is an enum-specifier or a
12518 class-specifier (i.e., a type definition)
12522 static void
12523 cp_parser_decl_specifier_seq (cp_parser* parser,
12524 cp_parser_flags flags,
12525 cp_decl_specifier_seq *decl_specs,
12526 int* declares_class_or_enum)
12528 bool constructor_possible_p = !parser->in_declarator_p;
12529 bool found_decl_spec = false;
12530 cp_token *start_token = NULL;
12531 cp_decl_spec ds;
12533 /* Clear DECL_SPECS. */
12534 clear_decl_specs (decl_specs);
12536 /* Assume no class or enumeration type is declared. */
12537 *declares_class_or_enum = 0;
12539 /* Keep reading specifiers until there are no more to read. */
12540 while (true)
12542 bool constructor_p;
12543 cp_token *token;
12544 ds = ds_last;
12546 /* Peek at the next token. */
12547 token = cp_lexer_peek_token (parser->lexer);
12549 /* Save the first token of the decl spec list for error
12550 reporting. */
12551 if (!start_token)
12552 start_token = token;
12553 /* Handle attributes. */
12554 if (cp_next_tokens_can_be_attribute_p (parser))
12556 /* Parse the attributes. */
12557 tree attrs = cp_parser_attributes_opt (parser);
12559 /* In a sequence of declaration specifiers, c++11 attributes
12560 appertain to the type that precede them. In that case
12561 [dcl.spec]/1 says:
12563 The attribute-specifier-seq affects the type only for
12564 the declaration it appears in, not other declarations
12565 involving the same type.
12567 But for now let's force the user to position the
12568 attribute either at the beginning of the declaration or
12569 after the declarator-id, which would clearly mean that it
12570 applies to the declarator. */
12571 if (cxx11_attribute_p (attrs))
12573 if (!found_decl_spec)
12574 /* The c++11 attribute is at the beginning of the
12575 declaration. It appertains to the entity being
12576 declared. */;
12577 else
12579 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
12581 /* This is an attribute following a
12582 class-specifier. */
12583 if (decl_specs->type_definition_p)
12584 warn_misplaced_attr_for_class_type (token->location,
12585 decl_specs->type);
12586 attrs = NULL_TREE;
12588 else
12590 decl_specs->std_attributes
12591 = chainon (decl_specs->std_attributes,
12592 attrs);
12593 if (decl_specs->locations[ds_std_attribute] == 0)
12594 decl_specs->locations[ds_std_attribute] = token->location;
12596 continue;
12600 decl_specs->attributes
12601 = chainon (decl_specs->attributes,
12602 attrs);
12603 if (decl_specs->locations[ds_attribute] == 0)
12604 decl_specs->locations[ds_attribute] = token->location;
12605 continue;
12607 /* Assume we will find a decl-specifier keyword. */
12608 found_decl_spec = true;
12609 /* If the next token is an appropriate keyword, we can simply
12610 add it to the list. */
12611 switch (token->keyword)
12613 /* decl-specifier:
12614 friend
12615 constexpr */
12616 case RID_FRIEND:
12617 if (!at_class_scope_p ())
12619 error_at (token->location, "%<friend%> used outside of class");
12620 cp_lexer_purge_token (parser->lexer);
12622 else
12624 ds = ds_friend;
12625 /* Consume the token. */
12626 cp_lexer_consume_token (parser->lexer);
12628 break;
12630 case RID_CONSTEXPR:
12631 ds = ds_constexpr;
12632 cp_lexer_consume_token (parser->lexer);
12633 break;
12635 case RID_CONCEPT:
12636 ds = ds_concept;
12637 cp_lexer_consume_token (parser->lexer);
12638 break;
12640 /* function-specifier:
12641 inline
12642 virtual
12643 explicit */
12644 case RID_INLINE:
12645 case RID_VIRTUAL:
12646 case RID_EXPLICIT:
12647 cp_parser_function_specifier_opt (parser, decl_specs);
12648 break;
12650 /* decl-specifier:
12651 typedef */
12652 case RID_TYPEDEF:
12653 ds = ds_typedef;
12654 /* Consume the token. */
12655 cp_lexer_consume_token (parser->lexer);
12656 /* A constructor declarator cannot appear in a typedef. */
12657 constructor_possible_p = false;
12658 /* The "typedef" keyword can only occur in a declaration; we
12659 may as well commit at this point. */
12660 cp_parser_commit_to_tentative_parse (parser);
12662 if (decl_specs->storage_class != sc_none)
12663 decl_specs->conflicting_specifiers_p = true;
12664 break;
12666 /* storage-class-specifier:
12667 auto
12668 register
12669 static
12670 extern
12671 mutable
12673 GNU Extension:
12674 thread */
12675 case RID_AUTO:
12676 if (cxx_dialect == cxx98)
12678 /* Consume the token. */
12679 cp_lexer_consume_token (parser->lexer);
12681 /* Complain about `auto' as a storage specifier, if
12682 we're complaining about C++0x compatibility. */
12683 warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
12684 " changes meaning in C++11; please remove it");
12686 /* Set the storage class anyway. */
12687 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
12688 token);
12690 else
12691 /* C++0x auto type-specifier. */
12692 found_decl_spec = false;
12693 break;
12695 case RID_REGISTER:
12696 case RID_STATIC:
12697 case RID_EXTERN:
12698 case RID_MUTABLE:
12699 /* Consume the token. */
12700 cp_lexer_consume_token (parser->lexer);
12701 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
12702 token);
12703 break;
12704 case RID_THREAD:
12705 /* Consume the token. */
12706 ds = ds_thread;
12707 cp_lexer_consume_token (parser->lexer);
12708 break;
12710 default:
12711 /* We did not yet find a decl-specifier yet. */
12712 found_decl_spec = false;
12713 break;
12716 if (found_decl_spec
12717 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
12718 && token->keyword != RID_CONSTEXPR)
12719 error ("decl-specifier invalid in condition");
12721 if (ds != ds_last)
12722 set_and_check_decl_spec_loc (decl_specs, ds, token);
12724 /* Constructors are a special case. The `S' in `S()' is not a
12725 decl-specifier; it is the beginning of the declarator. */
12726 constructor_p
12727 = (!found_decl_spec
12728 && constructor_possible_p
12729 && (cp_parser_constructor_declarator_p
12730 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
12732 /* If we don't have a DECL_SPEC yet, then we must be looking at
12733 a type-specifier. */
12734 if (!found_decl_spec && !constructor_p)
12736 int decl_spec_declares_class_or_enum;
12737 bool is_cv_qualifier;
12738 tree type_spec;
12740 type_spec
12741 = cp_parser_type_specifier (parser, flags,
12742 decl_specs,
12743 /*is_declaration=*/true,
12744 &decl_spec_declares_class_or_enum,
12745 &is_cv_qualifier);
12746 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
12748 /* If this type-specifier referenced a user-defined type
12749 (a typedef, class-name, etc.), then we can't allow any
12750 more such type-specifiers henceforth.
12752 [dcl.spec]
12754 The longest sequence of decl-specifiers that could
12755 possibly be a type name is taken as the
12756 decl-specifier-seq of a declaration. The sequence shall
12757 be self-consistent as described below.
12759 [dcl.type]
12761 As a general rule, at most one type-specifier is allowed
12762 in the complete decl-specifier-seq of a declaration. The
12763 only exceptions are the following:
12765 -- const or volatile can be combined with any other
12766 type-specifier.
12768 -- signed or unsigned can be combined with char, long,
12769 short, or int.
12771 -- ..
12773 Example:
12775 typedef char* Pc;
12776 void g (const int Pc);
12778 Here, Pc is *not* part of the decl-specifier seq; it's
12779 the declarator. Therefore, once we see a type-specifier
12780 (other than a cv-qualifier), we forbid any additional
12781 user-defined types. We *do* still allow things like `int
12782 int' to be considered a decl-specifier-seq, and issue the
12783 error message later. */
12784 if (type_spec && !is_cv_qualifier)
12785 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12786 /* A constructor declarator cannot follow a type-specifier. */
12787 if (type_spec)
12789 constructor_possible_p = false;
12790 found_decl_spec = true;
12791 if (!is_cv_qualifier)
12792 decl_specs->any_type_specifiers_p = true;
12796 /* If we still do not have a DECL_SPEC, then there are no more
12797 decl-specifiers. */
12798 if (!found_decl_spec)
12799 break;
12801 decl_specs->any_specifiers_p = true;
12802 /* After we see one decl-specifier, further decl-specifiers are
12803 always optional. */
12804 flags |= CP_PARSER_FLAGS_OPTIONAL;
12807 /* Don't allow a friend specifier with a class definition. */
12808 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
12809 && (*declares_class_or_enum & 2))
12810 error_at (decl_specs->locations[ds_friend],
12811 "class definition may not be declared a friend");
12814 /* Parse an (optional) storage-class-specifier.
12816 storage-class-specifier:
12817 auto
12818 register
12819 static
12820 extern
12821 mutable
12823 GNU Extension:
12825 storage-class-specifier:
12826 thread
12828 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
12830 static tree
12831 cp_parser_storage_class_specifier_opt (cp_parser* parser)
12833 switch (cp_lexer_peek_token (parser->lexer)->keyword)
12835 case RID_AUTO:
12836 if (cxx_dialect != cxx98)
12837 return NULL_TREE;
12838 /* Fall through for C++98. */
12840 case RID_REGISTER:
12841 case RID_STATIC:
12842 case RID_EXTERN:
12843 case RID_MUTABLE:
12844 case RID_THREAD:
12845 /* Consume the token. */
12846 return cp_lexer_consume_token (parser->lexer)->u.value;
12848 default:
12849 return NULL_TREE;
12853 /* Parse an (optional) function-specifier.
12855 function-specifier:
12856 inline
12857 virtual
12858 explicit
12860 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12861 Updates DECL_SPECS, if it is non-NULL. */
12863 static tree
12864 cp_parser_function_specifier_opt (cp_parser* parser,
12865 cp_decl_specifier_seq *decl_specs)
12867 cp_token *token = cp_lexer_peek_token (parser->lexer);
12868 switch (token->keyword)
12870 case RID_INLINE:
12871 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12872 break;
12874 case RID_VIRTUAL:
12875 /* 14.5.2.3 [temp.mem]
12877 A member function template shall not be virtual. */
12878 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12879 error_at (token->location, "templates may not be %<virtual%>");
12880 else
12881 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12882 break;
12884 case RID_EXPLICIT:
12885 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12886 break;
12888 default:
12889 return NULL_TREE;
12892 /* Consume the token. */
12893 return cp_lexer_consume_token (parser->lexer)->u.value;
12896 /* Parse a linkage-specification.
12898 linkage-specification:
12899 extern string-literal { declaration-seq [opt] }
12900 extern string-literal declaration */
12902 static void
12903 cp_parser_linkage_specification (cp_parser* parser)
12905 tree linkage;
12907 /* Look for the `extern' keyword. */
12908 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12910 /* Look for the string-literal. */
12911 linkage = cp_parser_string_literal (parser, false, false);
12913 /* Transform the literal into an identifier. If the literal is a
12914 wide-character string, or contains embedded NULs, then we can't
12915 handle it as the user wants. */
12916 if (strlen (TREE_STRING_POINTER (linkage))
12917 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12919 cp_parser_error (parser, "invalid linkage-specification");
12920 /* Assume C++ linkage. */
12921 linkage = lang_name_cplusplus;
12923 else
12924 linkage = get_identifier (TREE_STRING_POINTER (linkage));
12926 /* We're now using the new linkage. */
12927 push_lang_context (linkage);
12929 /* If the next token is a `{', then we're using the first
12930 production. */
12931 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12933 cp_ensure_no_omp_declare_simd (parser);
12934 cp_ensure_no_oacc_routine (parser);
12936 /* Consume the `{' token. */
12937 cp_lexer_consume_token (parser->lexer);
12938 /* Parse the declarations. */
12939 cp_parser_declaration_seq_opt (parser);
12940 /* Look for the closing `}'. */
12941 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12943 /* Otherwise, there's just one declaration. */
12944 else
12946 bool saved_in_unbraced_linkage_specification_p;
12948 saved_in_unbraced_linkage_specification_p
12949 = parser->in_unbraced_linkage_specification_p;
12950 parser->in_unbraced_linkage_specification_p = true;
12951 cp_parser_declaration (parser);
12952 parser->in_unbraced_linkage_specification_p
12953 = saved_in_unbraced_linkage_specification_p;
12956 /* We're done with the linkage-specification. */
12957 pop_lang_context ();
12960 /* Parse a static_assert-declaration.
12962 static_assert-declaration:
12963 static_assert ( constant-expression , string-literal ) ;
12964 static_assert ( constant-expression ) ; (C++1Z)
12966 If MEMBER_P, this static_assert is a class member. */
12968 static void
12969 cp_parser_static_assert(cp_parser *parser, bool member_p)
12971 tree condition;
12972 tree message;
12973 cp_token *token;
12974 location_t saved_loc;
12975 bool dummy;
12977 /* Peek at the `static_assert' token so we can keep track of exactly
12978 where the static assertion started. */
12979 token = cp_lexer_peek_token (parser->lexer);
12980 saved_loc = token->location;
12982 /* Look for the `static_assert' keyword. */
12983 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
12984 RT_STATIC_ASSERT))
12985 return;
12987 /* We know we are in a static assertion; commit to any tentative
12988 parse. */
12989 if (cp_parser_parsing_tentatively (parser))
12990 cp_parser_commit_to_tentative_parse (parser);
12992 /* Parse the `(' starting the static assertion condition. */
12993 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12995 /* Parse the constant-expression. Allow a non-constant expression
12996 here in order to give better diagnostics in finish_static_assert. */
12997 condition =
12998 cp_parser_constant_expression (parser,
12999 /*allow_non_constant_p=*/true,
13000 /*non_constant_p=*/&dummy);
13002 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13004 if (cxx_dialect < cxx1z)
13005 pedwarn (input_location, OPT_Wpedantic,
13006 "static_assert without a message "
13007 "only available with -std=c++1z or -std=gnu++1z");
13008 /* Eat the ')' */
13009 cp_lexer_consume_token (parser->lexer);
13010 message = build_string (1, "");
13011 TREE_TYPE (message) = char_array_type_node;
13012 fix_string_type (message);
13014 else
13016 /* Parse the separating `,'. */
13017 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13019 /* Parse the string-literal message. */
13020 message = cp_parser_string_literal (parser,
13021 /*translate=*/false,
13022 /*wide_ok=*/true);
13024 /* A `)' completes the static assertion. */
13025 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13026 cp_parser_skip_to_closing_parenthesis (parser,
13027 /*recovering=*/true,
13028 /*or_comma=*/false,
13029 /*consume_paren=*/true);
13032 /* A semicolon terminates the declaration. */
13033 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13035 /* Complete the static assertion, which may mean either processing
13036 the static assert now or saving it for template instantiation. */
13037 finish_static_assert (condition, message, saved_loc, member_p);
13040 /* Parse the expression in decltype ( expression ). */
13042 static tree
13043 cp_parser_decltype_expr (cp_parser *parser,
13044 bool &id_expression_or_member_access_p)
13046 cp_token *id_expr_start_token;
13047 tree expr;
13049 /* Since we're going to preserve any side-effects from this parse, set up a
13050 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13051 in the expression. */
13052 tentative_firewall firewall (parser);
13054 /* First, try parsing an id-expression. */
13055 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13056 cp_parser_parse_tentatively (parser);
13057 expr = cp_parser_id_expression (parser,
13058 /*template_keyword_p=*/false,
13059 /*check_dependency_p=*/true,
13060 /*template_p=*/NULL,
13061 /*declarator_p=*/false,
13062 /*optional_p=*/false);
13064 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13066 bool non_integral_constant_expression_p = false;
13067 tree id_expression = expr;
13068 cp_id_kind idk;
13069 const char *error_msg;
13071 if (identifier_p (expr))
13072 /* Lookup the name we got back from the id-expression. */
13073 expr = cp_parser_lookup_name_simple (parser, expr,
13074 id_expr_start_token->location);
13076 if (expr
13077 && expr != error_mark_node
13078 && TREE_CODE (expr) != TYPE_DECL
13079 && (TREE_CODE (expr) != BIT_NOT_EXPR
13080 || !TYPE_P (TREE_OPERAND (expr, 0)))
13081 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13083 /* Complete lookup of the id-expression. */
13084 expr = (finish_id_expression
13085 (id_expression, expr, parser->scope, &idk,
13086 /*integral_constant_expression_p=*/false,
13087 /*allow_non_integral_constant_expression_p=*/true,
13088 &non_integral_constant_expression_p,
13089 /*template_p=*/false,
13090 /*done=*/true,
13091 /*address_p=*/false,
13092 /*template_arg_p=*/false,
13093 &error_msg,
13094 id_expr_start_token->location));
13096 if (expr == error_mark_node)
13097 /* We found an id-expression, but it was something that we
13098 should not have found. This is an error, not something
13099 we can recover from, so note that we found an
13100 id-expression and we'll recover as gracefully as
13101 possible. */
13102 id_expression_or_member_access_p = true;
13105 if (expr
13106 && expr != error_mark_node
13107 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13108 /* We have an id-expression. */
13109 id_expression_or_member_access_p = true;
13112 if (!id_expression_or_member_access_p)
13114 /* Abort the id-expression parse. */
13115 cp_parser_abort_tentative_parse (parser);
13117 /* Parsing tentatively, again. */
13118 cp_parser_parse_tentatively (parser);
13120 /* Parse a class member access. */
13121 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13122 /*cast_p=*/false, /*decltype*/true,
13123 /*member_access_only_p=*/true, NULL);
13125 if (expr
13126 && expr != error_mark_node
13127 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13128 /* We have an id-expression. */
13129 id_expression_or_member_access_p = true;
13132 if (id_expression_or_member_access_p)
13133 /* We have parsed the complete id-expression or member access. */
13134 cp_parser_parse_definitely (parser);
13135 else
13137 /* Abort our attempt to parse an id-expression or member access
13138 expression. */
13139 cp_parser_abort_tentative_parse (parser);
13141 /* Parse a full expression. */
13142 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
13143 /*decltype_p=*/true);
13146 return expr;
13149 /* Parse a `decltype' type. Returns the type.
13151 simple-type-specifier:
13152 decltype ( expression )
13153 C++14 proposal:
13154 decltype ( auto ) */
13156 static tree
13157 cp_parser_decltype (cp_parser *parser)
13159 tree expr;
13160 bool id_expression_or_member_access_p = false;
13161 const char *saved_message;
13162 bool saved_integral_constant_expression_p;
13163 bool saved_non_integral_constant_expression_p;
13164 bool saved_greater_than_is_operator_p;
13165 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13167 if (start_token->type == CPP_DECLTYPE)
13169 /* Already parsed. */
13170 cp_lexer_consume_token (parser->lexer);
13171 return saved_checks_value (start_token->u.tree_check_value);
13174 /* Look for the `decltype' token. */
13175 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
13176 return error_mark_node;
13178 /* Parse the opening `('. */
13179 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13180 return error_mark_node;
13182 /* decltype (auto) */
13183 if (cxx_dialect >= cxx14
13184 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
13186 cp_lexer_consume_token (parser->lexer);
13187 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13188 return error_mark_node;
13189 expr = make_decltype_auto ();
13190 AUTO_IS_DECLTYPE (expr) = true;
13191 goto rewrite;
13194 /* Types cannot be defined in a `decltype' expression. Save away the
13195 old message. */
13196 saved_message = parser->type_definition_forbidden_message;
13198 /* And create the new one. */
13199 parser->type_definition_forbidden_message
13200 = G_("types may not be defined in %<decltype%> expressions");
13202 /* The restrictions on constant-expressions do not apply inside
13203 decltype expressions. */
13204 saved_integral_constant_expression_p
13205 = parser->integral_constant_expression_p;
13206 saved_non_integral_constant_expression_p
13207 = parser->non_integral_constant_expression_p;
13208 parser->integral_constant_expression_p = false;
13210 /* Within a parenthesized expression, a `>' token is always
13211 the greater-than operator. */
13212 saved_greater_than_is_operator_p
13213 = parser->greater_than_is_operator_p;
13214 parser->greater_than_is_operator_p = true;
13216 /* Do not actually evaluate the expression. */
13217 ++cp_unevaluated_operand;
13219 /* Do not warn about problems with the expression. */
13220 ++c_inhibit_evaluation_warnings;
13222 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
13224 /* Go back to evaluating expressions. */
13225 --cp_unevaluated_operand;
13226 --c_inhibit_evaluation_warnings;
13228 /* The `>' token might be the end of a template-id or
13229 template-parameter-list now. */
13230 parser->greater_than_is_operator_p
13231 = saved_greater_than_is_operator_p;
13233 /* Restore the old message and the integral constant expression
13234 flags. */
13235 parser->type_definition_forbidden_message = saved_message;
13236 parser->integral_constant_expression_p
13237 = saved_integral_constant_expression_p;
13238 parser->non_integral_constant_expression_p
13239 = saved_non_integral_constant_expression_p;
13241 /* Parse to the closing `)'. */
13242 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13244 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13245 /*consume_paren=*/true);
13246 return error_mark_node;
13249 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
13250 tf_warning_or_error);
13252 rewrite:
13253 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
13254 it again. */
13255 start_token->type = CPP_DECLTYPE;
13256 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13257 start_token->u.tree_check_value->value = expr;
13258 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
13259 start_token->keyword = RID_MAX;
13260 cp_lexer_purge_tokens_after (parser->lexer, start_token);
13262 return expr;
13265 /* Special member functions [gram.special] */
13267 /* Parse a conversion-function-id.
13269 conversion-function-id:
13270 operator conversion-type-id
13272 Returns an IDENTIFIER_NODE representing the operator. */
13274 static tree
13275 cp_parser_conversion_function_id (cp_parser* parser)
13277 tree type;
13278 tree saved_scope;
13279 tree saved_qualifying_scope;
13280 tree saved_object_scope;
13281 tree pushed_scope = NULL_TREE;
13283 /* Look for the `operator' token. */
13284 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13285 return error_mark_node;
13286 /* When we parse the conversion-type-id, the current scope will be
13287 reset. However, we need that information in able to look up the
13288 conversion function later, so we save it here. */
13289 saved_scope = parser->scope;
13290 saved_qualifying_scope = parser->qualifying_scope;
13291 saved_object_scope = parser->object_scope;
13292 /* We must enter the scope of the class so that the names of
13293 entities declared within the class are available in the
13294 conversion-type-id. For example, consider:
13296 struct S {
13297 typedef int I;
13298 operator I();
13301 S::operator I() { ... }
13303 In order to see that `I' is a type-name in the definition, we
13304 must be in the scope of `S'. */
13305 if (saved_scope)
13306 pushed_scope = push_scope (saved_scope);
13307 /* Parse the conversion-type-id. */
13308 type = cp_parser_conversion_type_id (parser);
13309 /* Leave the scope of the class, if any. */
13310 if (pushed_scope)
13311 pop_scope (pushed_scope);
13312 /* Restore the saved scope. */
13313 parser->scope = saved_scope;
13314 parser->qualifying_scope = saved_qualifying_scope;
13315 parser->object_scope = saved_object_scope;
13316 /* If the TYPE is invalid, indicate failure. */
13317 if (type == error_mark_node)
13318 return error_mark_node;
13319 return mangle_conv_op_name_for_type (type);
13322 /* Parse a conversion-type-id:
13324 conversion-type-id:
13325 type-specifier-seq conversion-declarator [opt]
13327 Returns the TYPE specified. */
13329 static tree
13330 cp_parser_conversion_type_id (cp_parser* parser)
13332 tree attributes;
13333 cp_decl_specifier_seq type_specifiers;
13334 cp_declarator *declarator;
13335 tree type_specified;
13336 const char *saved_message;
13338 /* Parse the attributes. */
13339 attributes = cp_parser_attributes_opt (parser);
13341 saved_message = parser->type_definition_forbidden_message;
13342 parser->type_definition_forbidden_message
13343 = G_("types may not be defined in a conversion-type-id");
13345 /* Parse the type-specifiers. */
13346 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13347 /*is_trailing_return=*/false,
13348 &type_specifiers);
13350 parser->type_definition_forbidden_message = saved_message;
13352 /* If that didn't work, stop. */
13353 if (type_specifiers.type == error_mark_node)
13354 return error_mark_node;
13355 /* Parse the conversion-declarator. */
13356 declarator = cp_parser_conversion_declarator_opt (parser);
13358 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
13359 /*initialized=*/0, &attributes);
13360 if (attributes)
13361 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
13363 /* Don't give this error when parsing tentatively. This happens to
13364 work because we always parse this definitively once. */
13365 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
13366 && type_uses_auto (type_specified))
13368 if (cxx_dialect < cxx14)
13370 error ("invalid use of %<auto%> in conversion operator");
13371 return error_mark_node;
13373 else if (template_parm_scope_p ())
13374 warning (0, "use of %<auto%> in member template "
13375 "conversion operator can never be deduced");
13378 return type_specified;
13381 /* Parse an (optional) conversion-declarator.
13383 conversion-declarator:
13384 ptr-operator conversion-declarator [opt]
13388 static cp_declarator *
13389 cp_parser_conversion_declarator_opt (cp_parser* parser)
13391 enum tree_code code;
13392 tree class_type, std_attributes = NULL_TREE;
13393 cp_cv_quals cv_quals;
13395 /* We don't know if there's a ptr-operator next, or not. */
13396 cp_parser_parse_tentatively (parser);
13397 /* Try the ptr-operator. */
13398 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
13399 &std_attributes);
13400 /* If it worked, look for more conversion-declarators. */
13401 if (cp_parser_parse_definitely (parser))
13403 cp_declarator *declarator;
13405 /* Parse another optional declarator. */
13406 declarator = cp_parser_conversion_declarator_opt (parser);
13408 declarator = cp_parser_make_indirect_declarator
13409 (code, class_type, cv_quals, declarator, std_attributes);
13411 return declarator;
13414 return NULL;
13417 /* Parse an (optional) ctor-initializer.
13419 ctor-initializer:
13420 : mem-initializer-list
13422 Returns TRUE iff the ctor-initializer was actually present. */
13424 static bool
13425 cp_parser_ctor_initializer_opt (cp_parser* parser)
13427 /* If the next token is not a `:', then there is no
13428 ctor-initializer. */
13429 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13431 /* Do default initialization of any bases and members. */
13432 if (DECL_CONSTRUCTOR_P (current_function_decl))
13433 finish_mem_initializers (NULL_TREE);
13435 return false;
13438 /* Consume the `:' token. */
13439 cp_lexer_consume_token (parser->lexer);
13440 /* And the mem-initializer-list. */
13441 cp_parser_mem_initializer_list (parser);
13443 return true;
13446 /* Parse a mem-initializer-list.
13448 mem-initializer-list:
13449 mem-initializer ... [opt]
13450 mem-initializer ... [opt] , mem-initializer-list */
13452 static void
13453 cp_parser_mem_initializer_list (cp_parser* parser)
13455 tree mem_initializer_list = NULL_TREE;
13456 tree target_ctor = error_mark_node;
13457 cp_token *token = cp_lexer_peek_token (parser->lexer);
13459 /* Let the semantic analysis code know that we are starting the
13460 mem-initializer-list. */
13461 if (!DECL_CONSTRUCTOR_P (current_function_decl))
13462 error_at (token->location,
13463 "only constructors take member initializers");
13465 /* Loop through the list. */
13466 while (true)
13468 tree mem_initializer;
13470 token = cp_lexer_peek_token (parser->lexer);
13471 /* Parse the mem-initializer. */
13472 mem_initializer = cp_parser_mem_initializer (parser);
13473 /* If the next token is a `...', we're expanding member initializers. */
13474 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13476 /* Consume the `...'. */
13477 cp_lexer_consume_token (parser->lexer);
13479 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
13480 can be expanded but members cannot. */
13481 if (mem_initializer != error_mark_node
13482 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
13484 error_at (token->location,
13485 "cannot expand initializer for member %<%D%>",
13486 TREE_PURPOSE (mem_initializer));
13487 mem_initializer = error_mark_node;
13490 /* Construct the pack expansion type. */
13491 if (mem_initializer != error_mark_node)
13492 mem_initializer = make_pack_expansion (mem_initializer);
13494 if (target_ctor != error_mark_node
13495 && mem_initializer != error_mark_node)
13497 error ("mem-initializer for %qD follows constructor delegation",
13498 TREE_PURPOSE (mem_initializer));
13499 mem_initializer = error_mark_node;
13501 /* Look for a target constructor. */
13502 if (mem_initializer != error_mark_node
13503 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
13504 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
13506 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
13507 if (mem_initializer_list)
13509 error ("constructor delegation follows mem-initializer for %qD",
13510 TREE_PURPOSE (mem_initializer_list));
13511 mem_initializer = error_mark_node;
13513 target_ctor = mem_initializer;
13515 /* Add it to the list, unless it was erroneous. */
13516 if (mem_initializer != error_mark_node)
13518 TREE_CHAIN (mem_initializer) = mem_initializer_list;
13519 mem_initializer_list = mem_initializer;
13521 /* If the next token is not a `,', we're done. */
13522 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13523 break;
13524 /* Consume the `,' token. */
13525 cp_lexer_consume_token (parser->lexer);
13528 /* Perform semantic analysis. */
13529 if (DECL_CONSTRUCTOR_P (current_function_decl))
13530 finish_mem_initializers (mem_initializer_list);
13533 /* Parse a mem-initializer.
13535 mem-initializer:
13536 mem-initializer-id ( expression-list [opt] )
13537 mem-initializer-id braced-init-list
13539 GNU extension:
13541 mem-initializer:
13542 ( expression-list [opt] )
13544 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
13545 class) or FIELD_DECL (for a non-static data member) to initialize;
13546 the TREE_VALUE is the expression-list. An empty initialization
13547 list is represented by void_list_node. */
13549 static tree
13550 cp_parser_mem_initializer (cp_parser* parser)
13552 tree mem_initializer_id;
13553 tree expression_list;
13554 tree member;
13555 cp_token *token = cp_lexer_peek_token (parser->lexer);
13557 /* Find out what is being initialized. */
13558 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
13560 permerror (token->location,
13561 "anachronistic old-style base class initializer");
13562 mem_initializer_id = NULL_TREE;
13564 else
13566 mem_initializer_id = cp_parser_mem_initializer_id (parser);
13567 if (mem_initializer_id == error_mark_node)
13568 return mem_initializer_id;
13570 member = expand_member_init (mem_initializer_id);
13571 if (member && !DECL_P (member))
13572 in_base_initializer = 1;
13574 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13576 bool expr_non_constant_p;
13577 cp_lexer_set_source_position (parser->lexer);
13578 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13579 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
13580 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
13581 expression_list = build_tree_list (NULL_TREE, expression_list);
13583 else
13585 vec<tree, va_gc> *vec;
13586 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
13587 /*cast_p=*/false,
13588 /*allow_expansion_p=*/true,
13589 /*non_constant_p=*/NULL);
13590 if (vec == NULL)
13591 return error_mark_node;
13592 expression_list = build_tree_list_vec (vec);
13593 release_tree_vector (vec);
13596 if (expression_list == error_mark_node)
13597 return error_mark_node;
13598 if (!expression_list)
13599 expression_list = void_type_node;
13601 in_base_initializer = 0;
13603 return member ? build_tree_list (member, expression_list) : error_mark_node;
13606 /* Parse a mem-initializer-id.
13608 mem-initializer-id:
13609 :: [opt] nested-name-specifier [opt] class-name
13610 decltype-specifier (C++11)
13611 identifier
13613 Returns a TYPE indicating the class to be initialized for the first
13614 production (and the second in C++11). Returns an IDENTIFIER_NODE
13615 indicating the data member to be initialized for the last production. */
13617 static tree
13618 cp_parser_mem_initializer_id (cp_parser* parser)
13620 bool global_scope_p;
13621 bool nested_name_specifier_p;
13622 bool template_p = false;
13623 tree id;
13625 cp_token *token = cp_lexer_peek_token (parser->lexer);
13627 /* `typename' is not allowed in this context ([temp.res]). */
13628 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13630 error_at (token->location,
13631 "keyword %<typename%> not allowed in this context (a qualified "
13632 "member initializer is implicitly a type)");
13633 cp_lexer_consume_token (parser->lexer);
13635 /* Look for the optional `::' operator. */
13636 global_scope_p
13637 = (cp_parser_global_scope_opt (parser,
13638 /*current_scope_valid_p=*/false)
13639 != NULL_TREE);
13640 /* Look for the optional nested-name-specifier. The simplest way to
13641 implement:
13643 [temp.res]
13645 The keyword `typename' is not permitted in a base-specifier or
13646 mem-initializer; in these contexts a qualified name that
13647 depends on a template-parameter is implicitly assumed to be a
13648 type name.
13650 is to assume that we have seen the `typename' keyword at this
13651 point. */
13652 nested_name_specifier_p
13653 = (cp_parser_nested_name_specifier_opt (parser,
13654 /*typename_keyword_p=*/true,
13655 /*check_dependency_p=*/true,
13656 /*type_p=*/true,
13657 /*is_declaration=*/true)
13658 != NULL_TREE);
13659 if (nested_name_specifier_p)
13660 template_p = cp_parser_optional_template_keyword (parser);
13661 /* If there is a `::' operator or a nested-name-specifier, then we
13662 are definitely looking for a class-name. */
13663 if (global_scope_p || nested_name_specifier_p)
13664 return cp_parser_class_name (parser,
13665 /*typename_keyword_p=*/true,
13666 /*template_keyword_p=*/template_p,
13667 typename_type,
13668 /*check_dependency_p=*/true,
13669 /*class_head_p=*/false,
13670 /*is_declaration=*/true);
13671 /* Otherwise, we could also be looking for an ordinary identifier. */
13672 cp_parser_parse_tentatively (parser);
13673 if (cp_lexer_next_token_is_decltype (parser->lexer))
13674 /* Try a decltype-specifier. */
13675 id = cp_parser_decltype (parser);
13676 else
13677 /* Otherwise, try a class-name. */
13678 id = cp_parser_class_name (parser,
13679 /*typename_keyword_p=*/true,
13680 /*template_keyword_p=*/false,
13681 none_type,
13682 /*check_dependency_p=*/true,
13683 /*class_head_p=*/false,
13684 /*is_declaration=*/true);
13685 /* If we found one, we're done. */
13686 if (cp_parser_parse_definitely (parser))
13687 return id;
13688 /* Otherwise, look for an ordinary identifier. */
13689 return cp_parser_identifier (parser);
13692 /* Overloading [gram.over] */
13694 /* Parse an operator-function-id.
13696 operator-function-id:
13697 operator operator
13699 Returns an IDENTIFIER_NODE for the operator which is a
13700 human-readable spelling of the identifier, e.g., `operator +'. */
13702 static cp_expr
13703 cp_parser_operator_function_id (cp_parser* parser)
13705 /* Look for the `operator' keyword. */
13706 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13707 return error_mark_node;
13708 /* And then the name of the operator itself. */
13709 return cp_parser_operator (parser);
13712 /* Return an identifier node for a user-defined literal operator.
13713 The suffix identifier is chained to the operator name identifier. */
13715 static tree
13716 cp_literal_operator_id (const char* name)
13718 tree identifier;
13719 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
13720 + strlen (name) + 10);
13721 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
13722 identifier = get_identifier (buffer);
13724 return identifier;
13727 /* Parse an operator.
13729 operator:
13730 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
13731 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
13732 || ++ -- , ->* -> () []
13734 GNU Extensions:
13736 operator:
13737 <? >? <?= >?=
13739 Returns an IDENTIFIER_NODE for the operator which is a
13740 human-readable spelling of the identifier, e.g., `operator +'. */
13742 static cp_expr
13743 cp_parser_operator (cp_parser* parser)
13745 tree id = NULL_TREE;
13746 cp_token *token;
13747 bool utf8 = false;
13749 /* Peek at the next token. */
13750 token = cp_lexer_peek_token (parser->lexer);
13752 location_t start_loc = token->location;
13754 /* Figure out which operator we have. */
13755 switch (token->type)
13757 case CPP_KEYWORD:
13759 enum tree_code op;
13761 /* The keyword should be either `new' or `delete'. */
13762 if (token->keyword == RID_NEW)
13763 op = NEW_EXPR;
13764 else if (token->keyword == RID_DELETE)
13765 op = DELETE_EXPR;
13766 else
13767 break;
13769 /* Consume the `new' or `delete' token. */
13770 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
13772 /* Peek at the next token. */
13773 token = cp_lexer_peek_token (parser->lexer);
13774 /* If it's a `[' token then this is the array variant of the
13775 operator. */
13776 if (token->type == CPP_OPEN_SQUARE)
13778 /* Consume the `[' token. */
13779 cp_lexer_consume_token (parser->lexer);
13780 /* Look for the `]' token. */
13781 end_loc = cp_parser_require (parser, CPP_CLOSE_SQUARE,
13782 RT_CLOSE_SQUARE)->location;
13783 id = ansi_opname (op == NEW_EXPR
13784 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
13786 /* Otherwise, we have the non-array variant. */
13787 else
13788 id = ansi_opname (op);
13790 location_t loc = make_location (start_loc, start_loc, end_loc);
13792 return cp_expr (id, loc);
13795 case CPP_PLUS:
13796 id = ansi_opname (PLUS_EXPR);
13797 break;
13799 case CPP_MINUS:
13800 id = ansi_opname (MINUS_EXPR);
13801 break;
13803 case CPP_MULT:
13804 id = ansi_opname (MULT_EXPR);
13805 break;
13807 case CPP_DIV:
13808 id = ansi_opname (TRUNC_DIV_EXPR);
13809 break;
13811 case CPP_MOD:
13812 id = ansi_opname (TRUNC_MOD_EXPR);
13813 break;
13815 case CPP_XOR:
13816 id = ansi_opname (BIT_XOR_EXPR);
13817 break;
13819 case CPP_AND:
13820 id = ansi_opname (BIT_AND_EXPR);
13821 break;
13823 case CPP_OR:
13824 id = ansi_opname (BIT_IOR_EXPR);
13825 break;
13827 case CPP_COMPL:
13828 id = ansi_opname (BIT_NOT_EXPR);
13829 break;
13831 case CPP_NOT:
13832 id = ansi_opname (TRUTH_NOT_EXPR);
13833 break;
13835 case CPP_EQ:
13836 id = ansi_assopname (NOP_EXPR);
13837 break;
13839 case CPP_LESS:
13840 id = ansi_opname (LT_EXPR);
13841 break;
13843 case CPP_GREATER:
13844 id = ansi_opname (GT_EXPR);
13845 break;
13847 case CPP_PLUS_EQ:
13848 id = ansi_assopname (PLUS_EXPR);
13849 break;
13851 case CPP_MINUS_EQ:
13852 id = ansi_assopname (MINUS_EXPR);
13853 break;
13855 case CPP_MULT_EQ:
13856 id = ansi_assopname (MULT_EXPR);
13857 break;
13859 case CPP_DIV_EQ:
13860 id = ansi_assopname (TRUNC_DIV_EXPR);
13861 break;
13863 case CPP_MOD_EQ:
13864 id = ansi_assopname (TRUNC_MOD_EXPR);
13865 break;
13867 case CPP_XOR_EQ:
13868 id = ansi_assopname (BIT_XOR_EXPR);
13869 break;
13871 case CPP_AND_EQ:
13872 id = ansi_assopname (BIT_AND_EXPR);
13873 break;
13875 case CPP_OR_EQ:
13876 id = ansi_assopname (BIT_IOR_EXPR);
13877 break;
13879 case CPP_LSHIFT:
13880 id = ansi_opname (LSHIFT_EXPR);
13881 break;
13883 case CPP_RSHIFT:
13884 id = ansi_opname (RSHIFT_EXPR);
13885 break;
13887 case CPP_LSHIFT_EQ:
13888 id = ansi_assopname (LSHIFT_EXPR);
13889 break;
13891 case CPP_RSHIFT_EQ:
13892 id = ansi_assopname (RSHIFT_EXPR);
13893 break;
13895 case CPP_EQ_EQ:
13896 id = ansi_opname (EQ_EXPR);
13897 break;
13899 case CPP_NOT_EQ:
13900 id = ansi_opname (NE_EXPR);
13901 break;
13903 case CPP_LESS_EQ:
13904 id = ansi_opname (LE_EXPR);
13905 break;
13907 case CPP_GREATER_EQ:
13908 id = ansi_opname (GE_EXPR);
13909 break;
13911 case CPP_AND_AND:
13912 id = ansi_opname (TRUTH_ANDIF_EXPR);
13913 break;
13915 case CPP_OR_OR:
13916 id = ansi_opname (TRUTH_ORIF_EXPR);
13917 break;
13919 case CPP_PLUS_PLUS:
13920 id = ansi_opname (POSTINCREMENT_EXPR);
13921 break;
13923 case CPP_MINUS_MINUS:
13924 id = ansi_opname (PREDECREMENT_EXPR);
13925 break;
13927 case CPP_COMMA:
13928 id = ansi_opname (COMPOUND_EXPR);
13929 break;
13931 case CPP_DEREF_STAR:
13932 id = ansi_opname (MEMBER_REF);
13933 break;
13935 case CPP_DEREF:
13936 id = ansi_opname (COMPONENT_REF);
13937 break;
13939 case CPP_OPEN_PAREN:
13940 /* Consume the `('. */
13941 cp_lexer_consume_token (parser->lexer);
13942 /* Look for the matching `)'. */
13943 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13944 return ansi_opname (CALL_EXPR);
13946 case CPP_OPEN_SQUARE:
13947 /* Consume the `['. */
13948 cp_lexer_consume_token (parser->lexer);
13949 /* Look for the matching `]'. */
13950 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13951 return ansi_opname (ARRAY_REF);
13953 case CPP_UTF8STRING:
13954 case CPP_UTF8STRING_USERDEF:
13955 utf8 = true;
13956 case CPP_STRING:
13957 case CPP_WSTRING:
13958 case CPP_STRING16:
13959 case CPP_STRING32:
13960 case CPP_STRING_USERDEF:
13961 case CPP_WSTRING_USERDEF:
13962 case CPP_STRING16_USERDEF:
13963 case CPP_STRING32_USERDEF:
13965 tree str, string_tree;
13966 int sz, len;
13968 if (cxx_dialect == cxx98)
13969 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
13971 /* Consume the string. */
13972 str = cp_parser_string_literal (parser, /*translate=*/true,
13973 /*wide_ok=*/true, /*lookup_udlit=*/false);
13974 if (str == error_mark_node)
13975 return error_mark_node;
13976 else if (TREE_CODE (str) == USERDEF_LITERAL)
13978 string_tree = USERDEF_LITERAL_VALUE (str);
13979 id = USERDEF_LITERAL_SUFFIX_ID (str);
13981 else
13983 string_tree = str;
13984 /* Look for the suffix identifier. */
13985 token = cp_lexer_peek_token (parser->lexer);
13986 if (token->type == CPP_NAME)
13987 id = cp_parser_identifier (parser);
13988 else if (token->type == CPP_KEYWORD)
13990 error ("unexpected keyword;"
13991 " remove space between quotes and suffix identifier");
13992 return error_mark_node;
13994 else
13996 error ("expected suffix identifier");
13997 return error_mark_node;
14000 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14001 (TREE_TYPE (TREE_TYPE (string_tree))));
14002 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14003 if (len != 0)
14005 error ("expected empty string after %<operator%> keyword");
14006 return error_mark_node;
14008 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14009 != char_type_node)
14011 error ("invalid encoding prefix in literal operator");
14012 return error_mark_node;
14014 if (id != error_mark_node)
14016 const char *name = IDENTIFIER_POINTER (id);
14017 id = cp_literal_operator_id (name);
14019 return id;
14022 default:
14023 /* Anything else is an error. */
14024 break;
14027 /* If we have selected an identifier, we need to consume the
14028 operator token. */
14029 if (id)
14030 cp_lexer_consume_token (parser->lexer);
14031 /* Otherwise, no valid operator name was present. */
14032 else
14034 cp_parser_error (parser, "expected operator");
14035 id = error_mark_node;
14038 return cp_expr (id, start_loc);
14041 /* Parse a template-declaration.
14043 template-declaration:
14044 export [opt] template < template-parameter-list > declaration
14046 If MEMBER_P is TRUE, this template-declaration occurs within a
14047 class-specifier.
14049 The grammar rule given by the standard isn't correct. What
14050 is really meant is:
14052 template-declaration:
14053 export [opt] template-parameter-list-seq
14054 decl-specifier-seq [opt] init-declarator [opt] ;
14055 export [opt] template-parameter-list-seq
14056 function-definition
14058 template-parameter-list-seq:
14059 template-parameter-list-seq [opt]
14060 template < template-parameter-list >
14062 Concept Extensions:
14064 template-parameter-list-seq:
14065 template < template-parameter-list > requires-clause [opt]
14067 requires-clause:
14068 requires logical-or-expression */
14070 static void
14071 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14073 /* Check for `export'. */
14074 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14076 /* Consume the `export' token. */
14077 cp_lexer_consume_token (parser->lexer);
14078 /* Warn that we do not support `export'. */
14079 warning (0, "keyword %<export%> not implemented, and will be ignored");
14082 cp_parser_template_declaration_after_export (parser, member_p);
14085 /* Parse a template-parameter-list.
14087 template-parameter-list:
14088 template-parameter
14089 template-parameter-list , template-parameter
14091 Returns a TREE_LIST. Each node represents a template parameter.
14092 The nodes are connected via their TREE_CHAINs. */
14094 static tree
14095 cp_parser_template_parameter_list (cp_parser* parser)
14097 tree parameter_list = NULL_TREE;
14099 begin_template_parm_list ();
14101 /* The loop below parses the template parms. We first need to know
14102 the total number of template parms to be able to compute proper
14103 canonical types of each dependent type. So after the loop, when
14104 we know the total number of template parms,
14105 end_template_parm_list computes the proper canonical types and
14106 fixes up the dependent types accordingly. */
14107 while (true)
14109 tree parameter;
14110 bool is_non_type;
14111 bool is_parameter_pack;
14112 location_t parm_loc;
14114 /* Parse the template-parameter. */
14115 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
14116 parameter = cp_parser_template_parameter (parser,
14117 &is_non_type,
14118 &is_parameter_pack);
14119 /* Add it to the list. */
14120 if (parameter != error_mark_node)
14121 parameter_list = process_template_parm (parameter_list,
14122 parm_loc,
14123 parameter,
14124 is_non_type,
14125 is_parameter_pack);
14126 else
14128 tree err_parm = build_tree_list (parameter, parameter);
14129 parameter_list = chainon (parameter_list, err_parm);
14132 /* If the next token is not a `,', we're done. */
14133 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14134 break;
14135 /* Otherwise, consume the `,' token. */
14136 cp_lexer_consume_token (parser->lexer);
14139 return end_template_parm_list (parameter_list);
14142 /* Parse a introduction-list.
14144 introduction-list:
14145 introduced-parameter
14146 introduction-list , introduced-parameter
14148 introduced-parameter:
14149 ...[opt] identifier
14151 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
14152 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
14153 WILDCARD_DECL will also have DECL_NAME set and token location in
14154 DECL_SOURCE_LOCATION. */
14156 static tree
14157 cp_parser_introduction_list (cp_parser *parser)
14159 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
14161 while (true)
14163 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14164 if (is_pack)
14165 cp_lexer_consume_token (parser->lexer);
14167 /* Build placeholder. */
14168 tree parm = build_nt (WILDCARD_DECL);
14169 DECL_SOURCE_LOCATION (parm)
14170 = cp_lexer_peek_token (parser->lexer)->location;
14171 DECL_NAME (parm) = cp_parser_identifier (parser);
14172 WILDCARD_PACK_P (parm) = is_pack;
14173 vec_safe_push (introduction_vec, parm);
14175 /* If the next token is not a `,', we're done. */
14176 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14177 break;
14178 /* Otherwise, consume the `,' token. */
14179 cp_lexer_consume_token (parser->lexer);
14182 /* Convert the vec into a TREE_VEC. */
14183 tree introduction_list = make_tree_vec (introduction_vec->length ());
14184 unsigned int n;
14185 tree parm;
14186 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
14187 TREE_VEC_ELT (introduction_list, n) = parm;
14189 release_tree_vector (introduction_vec);
14190 return introduction_list;
14193 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
14194 is an abstract declarator. */
14196 static inline cp_declarator*
14197 get_id_declarator (cp_declarator *declarator)
14199 cp_declarator *d = declarator;
14200 while (d && d->kind != cdk_id)
14201 d = d->declarator;
14202 return d;
14205 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
14206 is an abstract declarator. */
14208 static inline tree
14209 get_unqualified_id (cp_declarator *declarator)
14211 declarator = get_id_declarator (declarator);
14212 if (declarator)
14213 return declarator->u.id.unqualified_name;
14214 else
14215 return NULL_TREE;
14218 /* Returns true if DECL represents a constrained-parameter. */
14220 static inline bool
14221 is_constrained_parameter (tree decl)
14223 return (decl
14224 && TREE_CODE (decl) == TYPE_DECL
14225 && CONSTRAINED_PARM_CONCEPT (decl)
14226 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
14229 /* Returns true if PARM declares a constrained-parameter. */
14231 static inline bool
14232 is_constrained_parameter (cp_parameter_declarator *parm)
14234 return is_constrained_parameter (parm->decl_specifiers.type);
14237 /* Check that the type parameter is only a declarator-id, and that its
14238 type is not cv-qualified. */
14240 bool
14241 cp_parser_check_constrained_type_parm (cp_parser *parser,
14242 cp_parameter_declarator *parm)
14244 if (!parm->declarator)
14245 return true;
14247 if (parm->declarator->kind != cdk_id)
14249 cp_parser_error (parser, "invalid constrained type parameter");
14250 return false;
14253 /* Don't allow cv-qualified type parameters. */
14254 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
14255 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
14257 cp_parser_error (parser, "cv-qualified type parameter");
14258 return false;
14261 return true;
14264 /* Finish parsing/processing a template type parameter and checking
14265 various restrictions. */
14267 static inline tree
14268 cp_parser_constrained_type_template_parm (cp_parser *parser,
14269 tree id,
14270 cp_parameter_declarator* parmdecl)
14272 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
14273 return finish_template_type_parm (class_type_node, id);
14274 else
14275 return error_mark_node;
14278 static tree
14279 finish_constrained_template_template_parm (tree proto, tree id)
14281 /* FIXME: This should probably be copied, and we may need to adjust
14282 the template parameter depths. */
14283 tree saved_parms = current_template_parms;
14284 begin_template_parm_list ();
14285 current_template_parms = DECL_TEMPLATE_PARMS (proto);
14286 end_template_parm_list ();
14288 tree parm = finish_template_template_parm (class_type_node, id);
14289 current_template_parms = saved_parms;
14291 return parm;
14294 /* Finish parsing/processing a template template parameter by borrowing
14295 the template parameter list from the prototype parameter. */
14297 static tree
14298 cp_parser_constrained_template_template_parm (cp_parser *parser,
14299 tree proto,
14300 tree id,
14301 cp_parameter_declarator *parmdecl)
14303 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
14304 return error_mark_node;
14305 return finish_constrained_template_template_parm (proto, id);
14308 /* Create a new non-type template parameter from the given PARM
14309 declarator. */
14311 static tree
14312 constrained_non_type_template_parm (bool *is_non_type,
14313 cp_parameter_declarator *parm)
14315 *is_non_type = true;
14316 cp_declarator *decl = parm->declarator;
14317 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
14318 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
14319 return grokdeclarator (decl, specs, TPARM, 0, NULL);
14322 /* Build a constrained template parameter based on the PARMDECL
14323 declarator. The type of PARMDECL is the constrained type, which
14324 refers to the prototype template parameter that ultimately
14325 specifies the type of the declared parameter. */
14327 static tree
14328 finish_constrained_parameter (cp_parser *parser,
14329 cp_parameter_declarator *parmdecl,
14330 bool *is_non_type,
14331 bool *is_parameter_pack)
14333 tree decl = parmdecl->decl_specifiers.type;
14334 tree id = get_unqualified_id (parmdecl->declarator);
14335 tree def = parmdecl->default_argument;
14336 tree proto = DECL_INITIAL (decl);
14338 /* A template parameter constrained by a variadic concept shall also
14339 be declared as a template parameter pack. */
14340 bool is_variadic = template_parameter_pack_p (proto);
14341 if (is_variadic && !*is_parameter_pack)
14342 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
14344 /* Build the parameter. Return an error if the declarator was invalid. */
14345 tree parm;
14346 if (TREE_CODE (proto) == TYPE_DECL)
14347 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
14348 else if (TREE_CODE (proto) == TEMPLATE_DECL)
14349 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
14350 parmdecl);
14351 else
14352 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
14353 if (parm == error_mark_node)
14354 return error_mark_node;
14356 /* Finish the parameter decl and create a node attaching the
14357 default argument and constraint. */
14358 parm = build_tree_list (def, parm);
14359 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
14361 return parm;
14364 /* Returns true if the parsed type actually represents the declaration
14365 of a type template-parameter. */
14367 static inline bool
14368 declares_constrained_type_template_parameter (tree type)
14370 return (is_constrained_parameter (type)
14371 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
14375 /* Returns true if the parsed type actually represents the declaration of
14376 a template template-parameter. */
14378 static bool
14379 declares_constrained_template_template_parameter (tree type)
14381 return (is_constrained_parameter (type)
14382 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
14385 /* Parse a default argument for a type template-parameter.
14386 Note that diagnostics are handled in cp_parser_template_parameter. */
14388 static tree
14389 cp_parser_default_type_template_argument (cp_parser *parser)
14391 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14393 /* Consume the `=' token. */
14394 cp_lexer_consume_token (parser->lexer);
14396 cp_token *token = cp_lexer_peek_token (parser->lexer);
14398 /* Parse the default-argument. */
14399 push_deferring_access_checks (dk_no_deferred);
14400 tree default_argument = cp_parser_type_id (parser);
14401 pop_deferring_access_checks ();
14403 if (flag_concepts && type_uses_auto (default_argument))
14405 error_at (token->location,
14406 "invalid use of %<auto%> in default template argument");
14407 return error_mark_node;
14410 return default_argument;
14413 /* Parse a default argument for a template template-parameter. */
14415 static tree
14416 cp_parser_default_template_template_argument (cp_parser *parser)
14418 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14420 bool is_template;
14422 /* Consume the `='. */
14423 cp_lexer_consume_token (parser->lexer);
14424 /* Parse the id-expression. */
14425 push_deferring_access_checks (dk_no_deferred);
14426 /* save token before parsing the id-expression, for error
14427 reporting */
14428 const cp_token* token = cp_lexer_peek_token (parser->lexer);
14429 tree default_argument
14430 = cp_parser_id_expression (parser,
14431 /*template_keyword_p=*/false,
14432 /*check_dependency_p=*/true,
14433 /*template_p=*/&is_template,
14434 /*declarator_p=*/false,
14435 /*optional_p=*/false);
14436 if (TREE_CODE (default_argument) == TYPE_DECL)
14437 /* If the id-expression was a template-id that refers to
14438 a template-class, we already have the declaration here,
14439 so no further lookup is needed. */
14441 else
14442 /* Look up the name. */
14443 default_argument
14444 = cp_parser_lookup_name (parser, default_argument,
14445 none_type,
14446 /*is_template=*/is_template,
14447 /*is_namespace=*/false,
14448 /*check_dependency=*/true,
14449 /*ambiguous_decls=*/NULL,
14450 token->location);
14451 /* See if the default argument is valid. */
14452 default_argument = check_template_template_default_arg (default_argument);
14453 pop_deferring_access_checks ();
14454 return default_argument;
14457 /* Parse a template-parameter.
14459 template-parameter:
14460 type-parameter
14461 parameter-declaration
14463 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
14464 the parameter. The TREE_PURPOSE is the default value, if any.
14465 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
14466 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
14467 set to true iff this parameter is a parameter pack. */
14469 static tree
14470 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
14471 bool *is_parameter_pack)
14473 cp_token *token;
14474 cp_parameter_declarator *parameter_declarator;
14475 tree parm;
14477 /* Assume it is a type parameter or a template parameter. */
14478 *is_non_type = false;
14479 /* Assume it not a parameter pack. */
14480 *is_parameter_pack = false;
14481 /* Peek at the next token. */
14482 token = cp_lexer_peek_token (parser->lexer);
14483 /* If it is `class' or `template', we have a type-parameter. */
14484 if (token->keyword == RID_TEMPLATE)
14485 return cp_parser_type_parameter (parser, is_parameter_pack);
14486 /* If it is `class' or `typename' we do not know yet whether it is a
14487 type parameter or a non-type parameter. Consider:
14489 template <typename T, typename T::X X> ...
14493 template <class C, class D*> ...
14495 Here, the first parameter is a type parameter, and the second is
14496 a non-type parameter. We can tell by looking at the token after
14497 the identifier -- if it is a `,', `=', or `>' then we have a type
14498 parameter. */
14499 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
14501 /* Peek at the token after `class' or `typename'. */
14502 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14503 /* If it's an ellipsis, we have a template type parameter
14504 pack. */
14505 if (token->type == CPP_ELLIPSIS)
14506 return cp_parser_type_parameter (parser, is_parameter_pack);
14507 /* If it's an identifier, skip it. */
14508 if (token->type == CPP_NAME)
14509 token = cp_lexer_peek_nth_token (parser->lexer, 3);
14510 /* Now, see if the token looks like the end of a template
14511 parameter. */
14512 if (token->type == CPP_COMMA
14513 || token->type == CPP_EQ
14514 || token->type == CPP_GREATER)
14515 return cp_parser_type_parameter (parser, is_parameter_pack);
14518 /* Otherwise, it is a non-type parameter or a constrained parameter.
14520 [temp.param]
14522 When parsing a default template-argument for a non-type
14523 template-parameter, the first non-nested `>' is taken as the end
14524 of the template parameter-list rather than a greater-than
14525 operator. */
14526 parameter_declarator
14527 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
14528 /*parenthesized_p=*/NULL);
14530 if (!parameter_declarator)
14531 return error_mark_node;
14533 /* If the parameter declaration is marked as a parameter pack, set
14534 *IS_PARAMETER_PACK to notify the caller. */
14535 if (parameter_declarator->template_parameter_pack_p)
14536 *is_parameter_pack = true;
14538 if (parameter_declarator->default_argument)
14540 /* Can happen in some cases of erroneous input (c++/34892). */
14541 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14542 /* Consume the `...' for better error recovery. */
14543 cp_lexer_consume_token (parser->lexer);
14546 // The parameter may have been constrained.
14547 if (is_constrained_parameter (parameter_declarator))
14548 return finish_constrained_parameter (parser,
14549 parameter_declarator,
14550 is_non_type,
14551 is_parameter_pack);
14553 // Now we're sure that the parameter is a non-type parameter.
14554 *is_non_type = true;
14556 parm = grokdeclarator (parameter_declarator->declarator,
14557 &parameter_declarator->decl_specifiers,
14558 TPARM, /*initialized=*/0,
14559 /*attrlist=*/NULL);
14560 if (parm == error_mark_node)
14561 return error_mark_node;
14563 return build_tree_list (parameter_declarator->default_argument, parm);
14566 /* Parse a type-parameter.
14568 type-parameter:
14569 class identifier [opt]
14570 class identifier [opt] = type-id
14571 typename identifier [opt]
14572 typename identifier [opt] = type-id
14573 template < template-parameter-list > class identifier [opt]
14574 template < template-parameter-list > class identifier [opt]
14575 = id-expression
14577 GNU Extension (variadic templates):
14579 type-parameter:
14580 class ... identifier [opt]
14581 typename ... identifier [opt]
14583 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
14584 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
14585 the declaration of the parameter.
14587 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
14589 static tree
14590 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
14592 cp_token *token;
14593 tree parameter;
14595 /* Look for a keyword to tell us what kind of parameter this is. */
14596 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
14597 if (!token)
14598 return error_mark_node;
14600 switch (token->keyword)
14602 case RID_CLASS:
14603 case RID_TYPENAME:
14605 tree identifier;
14606 tree default_argument;
14608 /* If the next token is an ellipsis, we have a template
14609 argument pack. */
14610 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14612 /* Consume the `...' token. */
14613 cp_lexer_consume_token (parser->lexer);
14614 maybe_warn_variadic_templates ();
14616 *is_parameter_pack = true;
14619 /* If the next token is an identifier, then it names the
14620 parameter. */
14621 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14622 identifier = cp_parser_identifier (parser);
14623 else
14624 identifier = NULL_TREE;
14626 /* Create the parameter. */
14627 parameter = finish_template_type_parm (class_type_node, identifier);
14629 /* If the next token is an `=', we have a default argument. */
14630 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14632 default_argument
14633 = cp_parser_default_type_template_argument (parser);
14635 /* Template parameter packs cannot have default
14636 arguments. */
14637 if (*is_parameter_pack)
14639 if (identifier)
14640 error_at (token->location,
14641 "template parameter pack %qD cannot have a "
14642 "default argument", identifier);
14643 else
14644 error_at (token->location,
14645 "template parameter packs cannot have "
14646 "default arguments");
14647 default_argument = NULL_TREE;
14649 else if (check_for_bare_parameter_packs (default_argument))
14650 default_argument = error_mark_node;
14652 else
14653 default_argument = NULL_TREE;
14655 /* Create the combined representation of the parameter and the
14656 default argument. */
14657 parameter = build_tree_list (default_argument, parameter);
14659 break;
14661 case RID_TEMPLATE:
14663 tree identifier;
14664 tree default_argument;
14666 /* Look for the `<'. */
14667 cp_parser_require (parser, CPP_LESS, RT_LESS);
14668 /* Parse the template-parameter-list. */
14669 cp_parser_template_parameter_list (parser);
14670 /* Look for the `>'. */
14671 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14673 // If template requirements are present, parse them.
14674 if (flag_concepts)
14676 tree reqs = get_shorthand_constraints (current_template_parms);
14677 if (tree r = cp_parser_requires_clause_opt (parser))
14678 reqs = conjoin_constraints (reqs, make_predicate_constraint (r));
14679 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
14682 /* Look for the `class' or 'typename' keywords. */
14683 cp_parser_type_parameter_key (parser);
14684 /* If the next token is an ellipsis, we have a template
14685 argument pack. */
14686 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14688 /* Consume the `...' token. */
14689 cp_lexer_consume_token (parser->lexer);
14690 maybe_warn_variadic_templates ();
14692 *is_parameter_pack = true;
14694 /* If the next token is an `=', then there is a
14695 default-argument. If the next token is a `>', we are at
14696 the end of the parameter-list. If the next token is a `,',
14697 then we are at the end of this parameter. */
14698 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
14699 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
14700 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14702 identifier = cp_parser_identifier (parser);
14703 /* Treat invalid names as if the parameter were nameless. */
14704 if (identifier == error_mark_node)
14705 identifier = NULL_TREE;
14707 else
14708 identifier = NULL_TREE;
14710 /* Create the template parameter. */
14711 parameter = finish_template_template_parm (class_type_node,
14712 identifier);
14714 /* If the next token is an `=', then there is a
14715 default-argument. */
14716 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14718 default_argument
14719 = cp_parser_default_template_template_argument (parser);
14721 /* Template parameter packs cannot have default
14722 arguments. */
14723 if (*is_parameter_pack)
14725 if (identifier)
14726 error_at (token->location,
14727 "template parameter pack %qD cannot "
14728 "have a default argument",
14729 identifier);
14730 else
14731 error_at (token->location, "template parameter packs cannot "
14732 "have default arguments");
14733 default_argument = NULL_TREE;
14736 else
14737 default_argument = NULL_TREE;
14739 /* Create the combined representation of the parameter and the
14740 default argument. */
14741 parameter = build_tree_list (default_argument, parameter);
14743 break;
14745 default:
14746 gcc_unreachable ();
14747 break;
14750 return parameter;
14753 /* Parse a template-id.
14755 template-id:
14756 template-name < template-argument-list [opt] >
14758 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
14759 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
14760 returned. Otherwise, if the template-name names a function, or set
14761 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
14762 names a class, returns a TYPE_DECL for the specialization.
14764 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
14765 uninstantiated templates. */
14767 static tree
14768 cp_parser_template_id (cp_parser *parser,
14769 bool template_keyword_p,
14770 bool check_dependency_p,
14771 enum tag_types tag_type,
14772 bool is_declaration)
14774 tree templ;
14775 tree arguments;
14776 tree template_id;
14777 cp_token_position start_of_id = 0;
14778 cp_token *next_token = NULL, *next_token_2 = NULL;
14779 bool is_identifier;
14781 /* If the next token corresponds to a template-id, there is no need
14782 to reparse it. */
14783 next_token = cp_lexer_peek_token (parser->lexer);
14784 if (next_token->type == CPP_TEMPLATE_ID)
14786 cp_lexer_consume_token (parser->lexer);
14787 return saved_checks_value (next_token->u.tree_check_value);
14790 /* Avoid performing name lookup if there is no possibility of
14791 finding a template-id. */
14792 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
14793 || (next_token->type == CPP_NAME
14794 && !cp_parser_nth_token_starts_template_argument_list_p
14795 (parser, 2)))
14797 cp_parser_error (parser, "expected template-id");
14798 return error_mark_node;
14801 /* Remember where the template-id starts. */
14802 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
14803 start_of_id = cp_lexer_token_position (parser->lexer, false);
14805 push_deferring_access_checks (dk_deferred);
14807 /* Parse the template-name. */
14808 is_identifier = false;
14809 templ = cp_parser_template_name (parser, template_keyword_p,
14810 check_dependency_p,
14811 is_declaration,
14812 tag_type,
14813 &is_identifier);
14814 if (templ == error_mark_node || is_identifier)
14816 pop_deferring_access_checks ();
14817 return templ;
14820 /* Since we're going to preserve any side-effects from this parse, set up a
14821 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14822 in the template arguments. */
14823 tentative_firewall firewall (parser);
14825 /* If we find the sequence `[:' after a template-name, it's probably
14826 a digraph-typo for `< ::'. Substitute the tokens and check if we can
14827 parse correctly the argument list. */
14828 next_token = cp_lexer_peek_token (parser->lexer);
14829 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14830 if (next_token->type == CPP_OPEN_SQUARE
14831 && next_token->flags & DIGRAPH
14832 && next_token_2->type == CPP_COLON
14833 && !(next_token_2->flags & PREV_WHITE))
14835 cp_parser_parse_tentatively (parser);
14836 /* Change `:' into `::'. */
14837 next_token_2->type = CPP_SCOPE;
14838 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
14839 CPP_LESS. */
14840 cp_lexer_consume_token (parser->lexer);
14842 /* Parse the arguments. */
14843 arguments = cp_parser_enclosed_template_argument_list (parser);
14844 if (!cp_parser_parse_definitely (parser))
14846 /* If we couldn't parse an argument list, then we revert our changes
14847 and return simply an error. Maybe this is not a template-id
14848 after all. */
14849 next_token_2->type = CPP_COLON;
14850 cp_parser_error (parser, "expected %<<%>");
14851 pop_deferring_access_checks ();
14852 return error_mark_node;
14854 /* Otherwise, emit an error about the invalid digraph, but continue
14855 parsing because we got our argument list. */
14856 if (permerror (next_token->location,
14857 "%<<::%> cannot begin a template-argument list"))
14859 static bool hint = false;
14860 inform (next_token->location,
14861 "%<<:%> is an alternate spelling for %<[%>."
14862 " Insert whitespace between %<<%> and %<::%>");
14863 if (!hint && !flag_permissive)
14865 inform (next_token->location, "(if you use %<-fpermissive%> "
14866 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
14867 "accept your code)");
14868 hint = true;
14872 else
14874 /* Look for the `<' that starts the template-argument-list. */
14875 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
14877 pop_deferring_access_checks ();
14878 return error_mark_node;
14880 /* Parse the arguments. */
14881 arguments = cp_parser_enclosed_template_argument_list (parser);
14884 /* Build a representation of the specialization. */
14885 if (identifier_p (templ))
14886 template_id = build_min_nt_loc (next_token->location,
14887 TEMPLATE_ID_EXPR,
14888 templ, arguments);
14889 else if (DECL_TYPE_TEMPLATE_P (templ)
14890 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
14892 bool entering_scope;
14893 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
14894 template (rather than some instantiation thereof) only if
14895 is not nested within some other construct. For example, in
14896 "template <typename T> void f(T) { A<T>::", A<T> is just an
14897 instantiation of A. */
14898 entering_scope = (template_parm_scope_p ()
14899 && cp_lexer_next_token_is (parser->lexer,
14900 CPP_SCOPE));
14901 template_id
14902 = finish_template_type (templ, arguments, entering_scope);
14904 /* A template-like identifier may be a partial concept id. */
14905 else if (flag_concepts
14906 && (template_id = (cp_parser_maybe_partial_concept_id
14907 (parser, templ, arguments))))
14908 return template_id;
14909 else if (variable_template_p (templ))
14911 template_id = lookup_template_variable (templ, arguments);
14912 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
14913 SET_EXPR_LOCATION (template_id, next_token->location);
14915 else
14917 /* If it's not a class-template or a template-template, it should be
14918 a function-template. */
14919 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
14920 || TREE_CODE (templ) == OVERLOAD
14921 || BASELINK_P (templ)));
14923 template_id = lookup_template_function (templ, arguments);
14924 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
14925 SET_EXPR_LOCATION (template_id, next_token->location);
14928 /* If parsing tentatively, replace the sequence of tokens that makes
14929 up the template-id with a CPP_TEMPLATE_ID token. That way,
14930 should we re-parse the token stream, we will not have to repeat
14931 the effort required to do the parse, nor will we issue duplicate
14932 error messages about problems during instantiation of the
14933 template. */
14934 if (start_of_id
14935 /* Don't do this if we had a parse error in a declarator; re-parsing
14936 might succeed if a name changes meaning (60361). */
14937 && !(cp_parser_error_occurred (parser)
14938 && cp_parser_parsing_tentatively (parser)
14939 && parser->in_declarator_p))
14941 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
14943 /* Reset the contents of the START_OF_ID token. */
14944 token->type = CPP_TEMPLATE_ID;
14946 /* Update the location to be of the form:
14947 template-name < template-argument-list [opt] >
14948 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14949 with caret == start at the start of the template-name,
14950 ranging until the closing '>'. */
14951 location_t finish_loc
14952 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
14953 location_t combined_loc
14954 = make_location (token->location, token->location, finish_loc);
14955 token->location = combined_loc;
14957 /* Retrieve any deferred checks. Do not pop this access checks yet
14958 so the memory will not be reclaimed during token replacing below. */
14959 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14960 token->u.tree_check_value->value = template_id;
14961 token->u.tree_check_value->checks = get_deferred_access_checks ();
14962 token->keyword = RID_MAX;
14964 /* Purge all subsequent tokens. */
14965 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
14967 /* ??? Can we actually assume that, if template_id ==
14968 error_mark_node, we will have issued a diagnostic to the
14969 user, as opposed to simply marking the tentative parse as
14970 failed? */
14971 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
14972 error_at (token->location, "parse error in template argument list");
14975 pop_to_parent_deferring_access_checks ();
14976 return template_id;
14979 /* Parse a template-name.
14981 template-name:
14982 identifier
14984 The standard should actually say:
14986 template-name:
14987 identifier
14988 operator-function-id
14990 A defect report has been filed about this issue.
14992 A conversion-function-id cannot be a template name because they cannot
14993 be part of a template-id. In fact, looking at this code:
14995 a.operator K<int>()
14997 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
14998 It is impossible to call a templated conversion-function-id with an
14999 explicit argument list, since the only allowed template parameter is
15000 the type to which it is converting.
15002 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15003 `template' keyword, in a construction like:
15005 T::template f<3>()
15007 In that case `f' is taken to be a template-name, even though there
15008 is no way of knowing for sure.
15010 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15011 name refers to a set of overloaded functions, at least one of which
15012 is a template, or an IDENTIFIER_NODE with the name of the template,
15013 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15014 names are looked up inside uninstantiated templates. */
15016 static tree
15017 cp_parser_template_name (cp_parser* parser,
15018 bool template_keyword_p,
15019 bool check_dependency_p,
15020 bool is_declaration,
15021 enum tag_types tag_type,
15022 bool *is_identifier)
15024 tree identifier;
15025 tree decl;
15026 tree fns;
15027 cp_token *token = cp_lexer_peek_token (parser->lexer);
15029 /* If the next token is `operator', then we have either an
15030 operator-function-id or a conversion-function-id. */
15031 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15033 /* We don't know whether we're looking at an
15034 operator-function-id or a conversion-function-id. */
15035 cp_parser_parse_tentatively (parser);
15036 /* Try an operator-function-id. */
15037 identifier = cp_parser_operator_function_id (parser);
15038 /* If that didn't work, try a conversion-function-id. */
15039 if (!cp_parser_parse_definitely (parser))
15041 cp_parser_error (parser, "expected template-name");
15042 return error_mark_node;
15045 /* Look for the identifier. */
15046 else
15047 identifier = cp_parser_identifier (parser);
15049 /* If we didn't find an identifier, we don't have a template-id. */
15050 if (identifier == error_mark_node)
15051 return error_mark_node;
15053 /* If the name immediately followed the `template' keyword, then it
15054 is a template-name. However, if the next token is not `<', then
15055 we do not treat it as a template-name, since it is not being used
15056 as part of a template-id. This enables us to handle constructs
15057 like:
15059 template <typename T> struct S { S(); };
15060 template <typename T> S<T>::S();
15062 correctly. We would treat `S' as a template -- if it were `S<T>'
15063 -- but we do not if there is no `<'. */
15065 if (processing_template_decl
15066 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15068 /* In a declaration, in a dependent context, we pretend that the
15069 "template" keyword was present in order to improve error
15070 recovery. For example, given:
15072 template <typename T> void f(T::X<int>);
15074 we want to treat "X<int>" as a template-id. */
15075 if (is_declaration
15076 && !template_keyword_p
15077 && parser->scope && TYPE_P (parser->scope)
15078 && check_dependency_p
15079 && dependent_scope_p (parser->scope)
15080 /* Do not do this for dtors (or ctors), since they never
15081 need the template keyword before their name. */
15082 && !constructor_name_p (identifier, parser->scope))
15084 cp_token_position start = 0;
15086 /* Explain what went wrong. */
15087 error_at (token->location, "non-template %qD used as template",
15088 identifier);
15089 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15090 parser->scope, identifier);
15091 /* If parsing tentatively, find the location of the "<" token. */
15092 if (cp_parser_simulate_error (parser))
15093 start = cp_lexer_token_position (parser->lexer, true);
15094 /* Parse the template arguments so that we can issue error
15095 messages about them. */
15096 cp_lexer_consume_token (parser->lexer);
15097 cp_parser_enclosed_template_argument_list (parser);
15098 /* Skip tokens until we find a good place from which to
15099 continue parsing. */
15100 cp_parser_skip_to_closing_parenthesis (parser,
15101 /*recovering=*/true,
15102 /*or_comma=*/true,
15103 /*consume_paren=*/false);
15104 /* If parsing tentatively, permanently remove the
15105 template argument list. That will prevent duplicate
15106 error messages from being issued about the missing
15107 "template" keyword. */
15108 if (start)
15109 cp_lexer_purge_tokens_after (parser->lexer, start);
15110 if (is_identifier)
15111 *is_identifier = true;
15112 return identifier;
15115 /* If the "template" keyword is present, then there is generally
15116 no point in doing name-lookup, so we just return IDENTIFIER.
15117 But, if the qualifying scope is non-dependent then we can
15118 (and must) do name-lookup normally. */
15119 if (template_keyword_p
15120 && (!parser->scope
15121 || (TYPE_P (parser->scope)
15122 && dependent_type_p (parser->scope))))
15123 return identifier;
15126 /* Look up the name. */
15127 decl = cp_parser_lookup_name (parser, identifier,
15128 tag_type,
15129 /*is_template=*/true,
15130 /*is_namespace=*/false,
15131 check_dependency_p,
15132 /*ambiguous_decls=*/NULL,
15133 token->location);
15135 decl = strip_using_decl (decl);
15137 /* If DECL is a template, then the name was a template-name. */
15138 if (TREE_CODE (decl) == TEMPLATE_DECL)
15140 if (TREE_DEPRECATED (decl)
15141 && deprecated_state != DEPRECATED_SUPPRESS)
15142 warn_deprecated_use (decl, NULL_TREE);
15144 else
15146 tree fn = NULL_TREE;
15148 /* The standard does not explicitly indicate whether a name that
15149 names a set of overloaded declarations, some of which are
15150 templates, is a template-name. However, such a name should
15151 be a template-name; otherwise, there is no way to form a
15152 template-id for the overloaded templates. */
15153 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
15154 if (TREE_CODE (fns) == OVERLOAD)
15155 for (fn = fns; fn; fn = OVL_NEXT (fn))
15156 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
15157 break;
15159 if (!fn)
15161 /* The name does not name a template. */
15162 cp_parser_error (parser, "expected template-name");
15163 return error_mark_node;
15167 /* If DECL is dependent, and refers to a function, then just return
15168 its name; we will look it up again during template instantiation. */
15169 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
15171 tree scope = ovl_scope (decl);
15172 if (TYPE_P (scope) && dependent_type_p (scope))
15173 return identifier;
15176 return decl;
15179 /* Parse a template-argument-list.
15181 template-argument-list:
15182 template-argument ... [opt]
15183 template-argument-list , template-argument ... [opt]
15185 Returns a TREE_VEC containing the arguments. */
15187 static tree
15188 cp_parser_template_argument_list (cp_parser* parser)
15190 tree fixed_args[10];
15191 unsigned n_args = 0;
15192 unsigned alloced = 10;
15193 tree *arg_ary = fixed_args;
15194 tree vec;
15195 bool saved_in_template_argument_list_p;
15196 bool saved_ice_p;
15197 bool saved_non_ice_p;
15199 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
15200 parser->in_template_argument_list_p = true;
15201 /* Even if the template-id appears in an integral
15202 constant-expression, the contents of the argument list do
15203 not. */
15204 saved_ice_p = parser->integral_constant_expression_p;
15205 parser->integral_constant_expression_p = false;
15206 saved_non_ice_p = parser->non_integral_constant_expression_p;
15207 parser->non_integral_constant_expression_p = false;
15209 /* Parse the arguments. */
15212 tree argument;
15214 if (n_args)
15215 /* Consume the comma. */
15216 cp_lexer_consume_token (parser->lexer);
15218 /* Parse the template-argument. */
15219 argument = cp_parser_template_argument (parser);
15221 /* If the next token is an ellipsis, we're expanding a template
15222 argument pack. */
15223 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15225 if (argument == error_mark_node)
15227 cp_token *token = cp_lexer_peek_token (parser->lexer);
15228 error_at (token->location,
15229 "expected parameter pack before %<...%>");
15231 /* Consume the `...' token. */
15232 cp_lexer_consume_token (parser->lexer);
15234 /* Make the argument into a TYPE_PACK_EXPANSION or
15235 EXPR_PACK_EXPANSION. */
15236 argument = make_pack_expansion (argument);
15239 if (n_args == alloced)
15241 alloced *= 2;
15243 if (arg_ary == fixed_args)
15245 arg_ary = XNEWVEC (tree, alloced);
15246 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
15248 else
15249 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
15251 arg_ary[n_args++] = argument;
15253 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15255 vec = make_tree_vec (n_args);
15257 while (n_args--)
15258 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
15260 if (arg_ary != fixed_args)
15261 free (arg_ary);
15262 parser->non_integral_constant_expression_p = saved_non_ice_p;
15263 parser->integral_constant_expression_p = saved_ice_p;
15264 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
15265 if (CHECKING_P)
15266 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
15267 return vec;
15270 /* Parse a template-argument.
15272 template-argument:
15273 assignment-expression
15274 type-id
15275 id-expression
15277 The representation is that of an assignment-expression, type-id, or
15278 id-expression -- except that the qualified id-expression is
15279 evaluated, so that the value returned is either a DECL or an
15280 OVERLOAD.
15282 Although the standard says "assignment-expression", it forbids
15283 throw-expressions or assignments in the template argument.
15284 Therefore, we use "conditional-expression" instead. */
15286 static tree
15287 cp_parser_template_argument (cp_parser* parser)
15289 tree argument;
15290 bool template_p;
15291 bool address_p;
15292 bool maybe_type_id = false;
15293 cp_token *token = NULL, *argument_start_token = NULL;
15294 location_t loc = 0;
15295 cp_id_kind idk;
15297 /* There's really no way to know what we're looking at, so we just
15298 try each alternative in order.
15300 [temp.arg]
15302 In a template-argument, an ambiguity between a type-id and an
15303 expression is resolved to a type-id, regardless of the form of
15304 the corresponding template-parameter.
15306 Therefore, we try a type-id first. */
15307 cp_parser_parse_tentatively (parser);
15308 argument = cp_parser_template_type_arg (parser);
15309 /* If there was no error parsing the type-id but the next token is a
15310 '>>', our behavior depends on which dialect of C++ we're
15311 parsing. In C++98, we probably found a typo for '> >'. But there
15312 are type-id which are also valid expressions. For instance:
15314 struct X { int operator >> (int); };
15315 template <int V> struct Foo {};
15316 Foo<X () >> 5> r;
15318 Here 'X()' is a valid type-id of a function type, but the user just
15319 wanted to write the expression "X() >> 5". Thus, we remember that we
15320 found a valid type-id, but we still try to parse the argument as an
15321 expression to see what happens.
15323 In C++0x, the '>>' will be considered two separate '>'
15324 tokens. */
15325 if (!cp_parser_error_occurred (parser)
15326 && cxx_dialect == cxx98
15327 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15329 maybe_type_id = true;
15330 cp_parser_abort_tentative_parse (parser);
15332 else
15334 /* If the next token isn't a `,' or a `>', then this argument wasn't
15335 really finished. This means that the argument is not a valid
15336 type-id. */
15337 if (!cp_parser_next_token_ends_template_argument_p (parser))
15338 cp_parser_error (parser, "expected template-argument");
15339 /* If that worked, we're done. */
15340 if (cp_parser_parse_definitely (parser))
15341 return argument;
15343 /* We're still not sure what the argument will be. */
15344 cp_parser_parse_tentatively (parser);
15345 /* Try a template. */
15346 argument_start_token = cp_lexer_peek_token (parser->lexer);
15347 argument = cp_parser_id_expression (parser,
15348 /*template_keyword_p=*/false,
15349 /*check_dependency_p=*/true,
15350 &template_p,
15351 /*declarator_p=*/false,
15352 /*optional_p=*/false);
15353 /* If the next token isn't a `,' or a `>', then this argument wasn't
15354 really finished. */
15355 if (!cp_parser_next_token_ends_template_argument_p (parser))
15356 cp_parser_error (parser, "expected template-argument");
15357 if (!cp_parser_error_occurred (parser))
15359 /* Figure out what is being referred to. If the id-expression
15360 was for a class template specialization, then we will have a
15361 TYPE_DECL at this point. There is no need to do name lookup
15362 at this point in that case. */
15363 if (TREE_CODE (argument) != TYPE_DECL)
15364 argument = cp_parser_lookup_name (parser, argument,
15365 none_type,
15366 /*is_template=*/template_p,
15367 /*is_namespace=*/false,
15368 /*check_dependency=*/true,
15369 /*ambiguous_decls=*/NULL,
15370 argument_start_token->location);
15371 /* Handle a constrained-type-specifier for a non-type template
15372 parameter. */
15373 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
15374 argument = decl;
15375 else if (TREE_CODE (argument) != TEMPLATE_DECL
15376 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
15377 cp_parser_error (parser, "expected template-name");
15379 if (cp_parser_parse_definitely (parser))
15381 if (TREE_DEPRECATED (argument))
15382 warn_deprecated_use (argument, NULL_TREE);
15383 return argument;
15385 /* It must be a non-type argument. In C++17 any constant-expression is
15386 allowed. */
15387 if (cxx_dialect > cxx14)
15388 goto general_expr;
15390 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
15392 -- an integral constant-expression of integral or enumeration
15393 type; or
15395 -- the name of a non-type template-parameter; or
15397 -- the name of an object or function with external linkage...
15399 -- the address of an object or function with external linkage...
15401 -- a pointer to member... */
15402 /* Look for a non-type template parameter. */
15403 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15405 cp_parser_parse_tentatively (parser);
15406 argument = cp_parser_primary_expression (parser,
15407 /*address_p=*/false,
15408 /*cast_p=*/false,
15409 /*template_arg_p=*/true,
15410 &idk);
15411 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
15412 || !cp_parser_next_token_ends_template_argument_p (parser))
15413 cp_parser_simulate_error (parser);
15414 if (cp_parser_parse_definitely (parser))
15415 return argument;
15418 /* If the next token is "&", the argument must be the address of an
15419 object or function with external linkage. */
15420 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
15421 if (address_p)
15423 loc = cp_lexer_peek_token (parser->lexer)->location;
15424 cp_lexer_consume_token (parser->lexer);
15426 /* See if we might have an id-expression. */
15427 token = cp_lexer_peek_token (parser->lexer);
15428 if (token->type == CPP_NAME
15429 || token->keyword == RID_OPERATOR
15430 || token->type == CPP_SCOPE
15431 || token->type == CPP_TEMPLATE_ID
15432 || token->type == CPP_NESTED_NAME_SPECIFIER)
15434 cp_parser_parse_tentatively (parser);
15435 argument = cp_parser_primary_expression (parser,
15436 address_p,
15437 /*cast_p=*/false,
15438 /*template_arg_p=*/true,
15439 &idk);
15440 if (cp_parser_error_occurred (parser)
15441 || !cp_parser_next_token_ends_template_argument_p (parser))
15442 cp_parser_abort_tentative_parse (parser);
15443 else
15445 tree probe;
15447 if (INDIRECT_REF_P (argument))
15449 /* Strip the dereference temporarily. */
15450 gcc_assert (REFERENCE_REF_P (argument));
15451 argument = TREE_OPERAND (argument, 0);
15454 /* If we're in a template, we represent a qualified-id referring
15455 to a static data member as a SCOPE_REF even if the scope isn't
15456 dependent so that we can check access control later. */
15457 probe = argument;
15458 if (TREE_CODE (probe) == SCOPE_REF)
15459 probe = TREE_OPERAND (probe, 1);
15460 if (VAR_P (probe))
15462 /* A variable without external linkage might still be a
15463 valid constant-expression, so no error is issued here
15464 if the external-linkage check fails. */
15465 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
15466 cp_parser_simulate_error (parser);
15468 else if (is_overloaded_fn (argument))
15469 /* All overloaded functions are allowed; if the external
15470 linkage test does not pass, an error will be issued
15471 later. */
15473 else if (address_p
15474 && (TREE_CODE (argument) == OFFSET_REF
15475 || TREE_CODE (argument) == SCOPE_REF))
15476 /* A pointer-to-member. */
15478 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
15480 else
15481 cp_parser_simulate_error (parser);
15483 if (cp_parser_parse_definitely (parser))
15485 if (address_p)
15486 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
15487 tf_warning_or_error);
15488 else
15489 argument = convert_from_reference (argument);
15490 return argument;
15494 /* If the argument started with "&", there are no other valid
15495 alternatives at this point. */
15496 if (address_p)
15498 cp_parser_error (parser, "invalid non-type template argument");
15499 return error_mark_node;
15502 general_expr:
15503 /* If the argument wasn't successfully parsed as a type-id followed
15504 by '>>', the argument can only be a constant expression now.
15505 Otherwise, we try parsing the constant-expression tentatively,
15506 because the argument could really be a type-id. */
15507 if (maybe_type_id)
15508 cp_parser_parse_tentatively (parser);
15510 if (cxx_dialect <= cxx14)
15511 argument = cp_parser_constant_expression (parser);
15512 else
15514 /* With C++17 generalized non-type template arguments we need to handle
15515 lvalue constant expressions, too. */
15516 argument = cp_parser_assignment_expression (parser);
15517 require_potential_constant_expression (argument);
15520 if (!maybe_type_id)
15521 return argument;
15522 if (!cp_parser_next_token_ends_template_argument_p (parser))
15523 cp_parser_error (parser, "expected template-argument");
15524 if (cp_parser_parse_definitely (parser))
15525 return argument;
15526 /* We did our best to parse the argument as a non type-id, but that
15527 was the only alternative that matched (albeit with a '>' after
15528 it). We can assume it's just a typo from the user, and a
15529 diagnostic will then be issued. */
15530 return cp_parser_template_type_arg (parser);
15533 /* Parse an explicit-instantiation.
15535 explicit-instantiation:
15536 template declaration
15538 Although the standard says `declaration', what it really means is:
15540 explicit-instantiation:
15541 template decl-specifier-seq [opt] declarator [opt] ;
15543 Things like `template int S<int>::i = 5, int S<double>::j;' are not
15544 supposed to be allowed. A defect report has been filed about this
15545 issue.
15547 GNU Extension:
15549 explicit-instantiation:
15550 storage-class-specifier template
15551 decl-specifier-seq [opt] declarator [opt] ;
15552 function-specifier template
15553 decl-specifier-seq [opt] declarator [opt] ; */
15555 static void
15556 cp_parser_explicit_instantiation (cp_parser* parser)
15558 int declares_class_or_enum;
15559 cp_decl_specifier_seq decl_specifiers;
15560 tree extension_specifier = NULL_TREE;
15562 timevar_push (TV_TEMPLATE_INST);
15564 /* Look for an (optional) storage-class-specifier or
15565 function-specifier. */
15566 if (cp_parser_allow_gnu_extensions_p (parser))
15568 extension_specifier
15569 = cp_parser_storage_class_specifier_opt (parser);
15570 if (!extension_specifier)
15571 extension_specifier
15572 = cp_parser_function_specifier_opt (parser,
15573 /*decl_specs=*/NULL);
15576 /* Look for the `template' keyword. */
15577 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15578 /* Let the front end know that we are processing an explicit
15579 instantiation. */
15580 begin_explicit_instantiation ();
15581 /* [temp.explicit] says that we are supposed to ignore access
15582 control while processing explicit instantiation directives. */
15583 push_deferring_access_checks (dk_no_check);
15584 /* Parse a decl-specifier-seq. */
15585 cp_parser_decl_specifier_seq (parser,
15586 CP_PARSER_FLAGS_OPTIONAL,
15587 &decl_specifiers,
15588 &declares_class_or_enum);
15589 /* If there was exactly one decl-specifier, and it declared a class,
15590 and there's no declarator, then we have an explicit type
15591 instantiation. */
15592 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
15594 tree type;
15596 type = check_tag_decl (&decl_specifiers,
15597 /*explicit_type_instantiation_p=*/true);
15598 /* Turn access control back on for names used during
15599 template instantiation. */
15600 pop_deferring_access_checks ();
15601 if (type)
15602 do_type_instantiation (type, extension_specifier,
15603 /*complain=*/tf_error);
15605 else
15607 cp_declarator *declarator;
15608 tree decl;
15610 /* Parse the declarator. */
15611 declarator
15612 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15613 /*ctor_dtor_or_conv_p=*/NULL,
15614 /*parenthesized_p=*/NULL,
15615 /*member_p=*/false,
15616 /*friend_p=*/false);
15617 if (declares_class_or_enum & 2)
15618 cp_parser_check_for_definition_in_return_type (declarator,
15619 decl_specifiers.type,
15620 decl_specifiers.locations[ds_type_spec]);
15621 if (declarator != cp_error_declarator)
15623 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
15624 permerror (decl_specifiers.locations[ds_inline],
15625 "explicit instantiation shall not use"
15626 " %<inline%> specifier");
15627 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
15628 permerror (decl_specifiers.locations[ds_constexpr],
15629 "explicit instantiation shall not use"
15630 " %<constexpr%> specifier");
15632 decl = grokdeclarator (declarator, &decl_specifiers,
15633 NORMAL, 0, &decl_specifiers.attributes);
15634 /* Turn access control back on for names used during
15635 template instantiation. */
15636 pop_deferring_access_checks ();
15637 /* Do the explicit instantiation. */
15638 do_decl_instantiation (decl, extension_specifier);
15640 else
15642 pop_deferring_access_checks ();
15643 /* Skip the body of the explicit instantiation. */
15644 cp_parser_skip_to_end_of_statement (parser);
15647 /* We're done with the instantiation. */
15648 end_explicit_instantiation ();
15650 cp_parser_consume_semicolon_at_end_of_statement (parser);
15652 timevar_pop (TV_TEMPLATE_INST);
15655 /* Parse an explicit-specialization.
15657 explicit-specialization:
15658 template < > declaration
15660 Although the standard says `declaration', what it really means is:
15662 explicit-specialization:
15663 template <> decl-specifier [opt] init-declarator [opt] ;
15664 template <> function-definition
15665 template <> explicit-specialization
15666 template <> template-declaration */
15668 static void
15669 cp_parser_explicit_specialization (cp_parser* parser)
15671 bool need_lang_pop;
15672 cp_token *token = cp_lexer_peek_token (parser->lexer);
15674 /* Look for the `template' keyword. */
15675 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15676 /* Look for the `<'. */
15677 cp_parser_require (parser, CPP_LESS, RT_LESS);
15678 /* Look for the `>'. */
15679 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15680 /* We have processed another parameter list. */
15681 ++parser->num_template_parameter_lists;
15682 /* [temp]
15684 A template ... explicit specialization ... shall not have C
15685 linkage. */
15686 if (current_lang_name == lang_name_c)
15688 error_at (token->location, "template specialization with C linkage");
15689 /* Give it C++ linkage to avoid confusing other parts of the
15690 front end. */
15691 push_lang_context (lang_name_cplusplus);
15692 need_lang_pop = true;
15694 else
15695 need_lang_pop = false;
15696 /* Let the front end know that we are beginning a specialization. */
15697 if (!begin_specialization ())
15699 end_specialization ();
15700 return;
15703 /* If the next keyword is `template', we need to figure out whether
15704 or not we're looking a template-declaration. */
15705 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15707 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15708 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
15709 cp_parser_template_declaration_after_export (parser,
15710 /*member_p=*/false);
15711 else
15712 cp_parser_explicit_specialization (parser);
15714 else
15715 /* Parse the dependent declaration. */
15716 cp_parser_single_declaration (parser,
15717 /*checks=*/NULL,
15718 /*member_p=*/false,
15719 /*explicit_specialization_p=*/true,
15720 /*friend_p=*/NULL);
15721 /* We're done with the specialization. */
15722 end_specialization ();
15723 /* For the erroneous case of a template with C linkage, we pushed an
15724 implicit C++ linkage scope; exit that scope now. */
15725 if (need_lang_pop)
15726 pop_lang_context ();
15727 /* We're done with this parameter list. */
15728 --parser->num_template_parameter_lists;
15731 /* Parse a type-specifier.
15733 type-specifier:
15734 simple-type-specifier
15735 class-specifier
15736 enum-specifier
15737 elaborated-type-specifier
15738 cv-qualifier
15740 GNU Extension:
15742 type-specifier:
15743 __complex__
15745 Returns a representation of the type-specifier. For a
15746 class-specifier, enum-specifier, or elaborated-type-specifier, a
15747 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
15749 The parser flags FLAGS is used to control type-specifier parsing.
15751 If IS_DECLARATION is TRUE, then this type-specifier is appearing
15752 in a decl-specifier-seq.
15754 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
15755 class-specifier, enum-specifier, or elaborated-type-specifier, then
15756 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
15757 if a type is declared; 2 if it is defined. Otherwise, it is set to
15758 zero.
15760 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
15761 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
15762 is set to FALSE. */
15764 static tree
15765 cp_parser_type_specifier (cp_parser* parser,
15766 cp_parser_flags flags,
15767 cp_decl_specifier_seq *decl_specs,
15768 bool is_declaration,
15769 int* declares_class_or_enum,
15770 bool* is_cv_qualifier)
15772 tree type_spec = NULL_TREE;
15773 cp_token *token;
15774 enum rid keyword;
15775 cp_decl_spec ds = ds_last;
15777 /* Assume this type-specifier does not declare a new type. */
15778 if (declares_class_or_enum)
15779 *declares_class_or_enum = 0;
15780 /* And that it does not specify a cv-qualifier. */
15781 if (is_cv_qualifier)
15782 *is_cv_qualifier = false;
15783 /* Peek at the next token. */
15784 token = cp_lexer_peek_token (parser->lexer);
15786 /* If we're looking at a keyword, we can use that to guide the
15787 production we choose. */
15788 keyword = token->keyword;
15789 switch (keyword)
15791 case RID_ENUM:
15792 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15793 goto elaborated_type_specifier;
15795 /* Look for the enum-specifier. */
15796 type_spec = cp_parser_enum_specifier (parser);
15797 /* If that worked, we're done. */
15798 if (type_spec)
15800 if (declares_class_or_enum)
15801 *declares_class_or_enum = 2;
15802 if (decl_specs)
15803 cp_parser_set_decl_spec_type (decl_specs,
15804 type_spec,
15805 token,
15806 /*type_definition_p=*/true);
15807 return type_spec;
15809 else
15810 goto elaborated_type_specifier;
15812 /* Any of these indicate either a class-specifier, or an
15813 elaborated-type-specifier. */
15814 case RID_CLASS:
15815 case RID_STRUCT:
15816 case RID_UNION:
15817 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15818 goto elaborated_type_specifier;
15820 /* Parse tentatively so that we can back up if we don't find a
15821 class-specifier. */
15822 cp_parser_parse_tentatively (parser);
15823 /* Look for the class-specifier. */
15824 type_spec = cp_parser_class_specifier (parser);
15825 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
15826 /* If that worked, we're done. */
15827 if (cp_parser_parse_definitely (parser))
15829 if (declares_class_or_enum)
15830 *declares_class_or_enum = 2;
15831 if (decl_specs)
15832 cp_parser_set_decl_spec_type (decl_specs,
15833 type_spec,
15834 token,
15835 /*type_definition_p=*/true);
15836 return type_spec;
15839 /* Fall through. */
15840 elaborated_type_specifier:
15841 /* We're declaring (not defining) a class or enum. */
15842 if (declares_class_or_enum)
15843 *declares_class_or_enum = 1;
15845 /* Fall through. */
15846 case RID_TYPENAME:
15847 /* Look for an elaborated-type-specifier. */
15848 type_spec
15849 = (cp_parser_elaborated_type_specifier
15850 (parser,
15851 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
15852 is_declaration));
15853 if (decl_specs)
15854 cp_parser_set_decl_spec_type (decl_specs,
15855 type_spec,
15856 token,
15857 /*type_definition_p=*/false);
15858 return type_spec;
15860 case RID_CONST:
15861 ds = ds_const;
15862 if (is_cv_qualifier)
15863 *is_cv_qualifier = true;
15864 break;
15866 case RID_VOLATILE:
15867 ds = ds_volatile;
15868 if (is_cv_qualifier)
15869 *is_cv_qualifier = true;
15870 break;
15872 case RID_RESTRICT:
15873 ds = ds_restrict;
15874 if (is_cv_qualifier)
15875 *is_cv_qualifier = true;
15876 break;
15878 case RID_COMPLEX:
15879 /* The `__complex__' keyword is a GNU extension. */
15880 ds = ds_complex;
15881 break;
15883 default:
15884 break;
15887 /* Handle simple keywords. */
15888 if (ds != ds_last)
15890 if (decl_specs)
15892 set_and_check_decl_spec_loc (decl_specs, ds, token);
15893 decl_specs->any_specifiers_p = true;
15895 return cp_lexer_consume_token (parser->lexer)->u.value;
15898 /* If we do not already have a type-specifier, assume we are looking
15899 at a simple-type-specifier. */
15900 type_spec = cp_parser_simple_type_specifier (parser,
15901 decl_specs,
15902 flags);
15904 /* If we didn't find a type-specifier, and a type-specifier was not
15905 optional in this context, issue an error message. */
15906 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15908 cp_parser_error (parser, "expected type specifier");
15909 return error_mark_node;
15912 return type_spec;
15915 /* Parse a simple-type-specifier.
15917 simple-type-specifier:
15918 :: [opt] nested-name-specifier [opt] type-name
15919 :: [opt] nested-name-specifier template template-id
15920 char
15921 wchar_t
15922 bool
15923 short
15925 long
15926 signed
15927 unsigned
15928 float
15929 double
15930 void
15932 C++0x Extension:
15934 simple-type-specifier:
15935 auto
15936 decltype ( expression )
15937 char16_t
15938 char32_t
15939 __underlying_type ( type-id )
15941 GNU Extension:
15943 simple-type-specifier:
15944 __int128
15945 __typeof__ unary-expression
15946 __typeof__ ( type-id )
15947 __typeof__ ( type-id ) { initializer-list , [opt] }
15949 Concepts Extension:
15951 simple-type-specifier:
15952 constrained-type-specifier
15954 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
15955 appropriately updated. */
15957 static tree
15958 cp_parser_simple_type_specifier (cp_parser* parser,
15959 cp_decl_specifier_seq *decl_specs,
15960 cp_parser_flags flags)
15962 tree type = NULL_TREE;
15963 cp_token *token;
15964 int idx;
15966 /* Peek at the next token. */
15967 token = cp_lexer_peek_token (parser->lexer);
15969 /* If we're looking at a keyword, things are easy. */
15970 switch (token->keyword)
15972 case RID_CHAR:
15973 if (decl_specs)
15974 decl_specs->explicit_char_p = true;
15975 type = char_type_node;
15976 break;
15977 case RID_CHAR16:
15978 type = char16_type_node;
15979 break;
15980 case RID_CHAR32:
15981 type = char32_type_node;
15982 break;
15983 case RID_WCHAR:
15984 type = wchar_type_node;
15985 break;
15986 case RID_BOOL:
15987 type = boolean_type_node;
15988 break;
15989 case RID_SHORT:
15990 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
15991 type = short_integer_type_node;
15992 break;
15993 case RID_INT:
15994 if (decl_specs)
15995 decl_specs->explicit_int_p = true;
15996 type = integer_type_node;
15997 break;
15998 case RID_INT_N_0:
15999 case RID_INT_N_1:
16000 case RID_INT_N_2:
16001 case RID_INT_N_3:
16002 idx = token->keyword - RID_INT_N_0;
16003 if (! int_n_enabled_p [idx])
16004 break;
16005 if (decl_specs)
16007 decl_specs->explicit_intN_p = true;
16008 decl_specs->int_n_idx = idx;
16010 type = int_n_trees [idx].signed_type;
16011 break;
16012 case RID_LONG:
16013 if (decl_specs)
16014 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16015 type = long_integer_type_node;
16016 break;
16017 case RID_SIGNED:
16018 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16019 type = integer_type_node;
16020 break;
16021 case RID_UNSIGNED:
16022 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16023 type = unsigned_type_node;
16024 break;
16025 case RID_FLOAT:
16026 type = float_type_node;
16027 break;
16028 case RID_DOUBLE:
16029 type = double_type_node;
16030 break;
16031 case RID_VOID:
16032 type = void_type_node;
16033 break;
16035 case RID_AUTO:
16036 maybe_warn_cpp0x (CPP0X_AUTO);
16037 if (parser->auto_is_implicit_function_template_parm_p)
16039 /* The 'auto' might be the placeholder return type for a function decl
16040 with trailing return type. */
16041 bool have_trailing_return_fn_decl = false;
16043 cp_parser_parse_tentatively (parser);
16044 cp_lexer_consume_token (parser->lexer);
16045 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16046 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16047 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16048 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16050 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16052 cp_lexer_consume_token (parser->lexer);
16053 cp_parser_skip_to_closing_parenthesis (parser,
16054 /*recovering*/false,
16055 /*or_comma*/false,
16056 /*consume_paren*/true);
16057 continue;
16060 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16062 have_trailing_return_fn_decl = true;
16063 break;
16066 cp_lexer_consume_token (parser->lexer);
16068 cp_parser_abort_tentative_parse (parser);
16070 if (have_trailing_return_fn_decl)
16072 type = make_auto ();
16073 break;
16076 if (cxx_dialect >= cxx14)
16078 type = synthesize_implicit_template_parm (parser, NULL_TREE);
16079 type = TREE_TYPE (type);
16081 else
16082 type = error_mark_node;
16084 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16086 if (cxx_dialect < cxx14)
16087 error_at (token->location,
16088 "use of %<auto%> in lambda parameter declaration "
16089 "only available with "
16090 "-std=c++14 or -std=gnu++14");
16092 else if (cxx_dialect < cxx14)
16093 error_at (token->location,
16094 "use of %<auto%> in parameter declaration "
16095 "only available with "
16096 "-std=c++14 or -std=gnu++14");
16097 else if (!flag_concepts)
16098 pedwarn (token->location, OPT_Wpedantic,
16099 "ISO C++ forbids use of %<auto%> in parameter "
16100 "declaration");
16102 else
16103 type = make_auto ();
16104 break;
16106 case RID_DECLTYPE:
16107 /* Since DR 743, decltype can either be a simple-type-specifier by
16108 itself or begin a nested-name-specifier. Parsing it will replace
16109 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16110 handling below decide what to do. */
16111 cp_parser_decltype (parser);
16112 cp_lexer_set_token_position (parser->lexer, token);
16113 break;
16115 case RID_TYPEOF:
16116 /* Consume the `typeof' token. */
16117 cp_lexer_consume_token (parser->lexer);
16118 /* Parse the operand to `typeof'. */
16119 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
16120 /* If it is not already a TYPE, take its type. */
16121 if (!TYPE_P (type))
16122 type = finish_typeof (type);
16124 if (decl_specs)
16125 cp_parser_set_decl_spec_type (decl_specs, type,
16126 token,
16127 /*type_definition_p=*/false);
16129 return type;
16131 case RID_UNDERLYING_TYPE:
16132 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
16133 if (decl_specs)
16134 cp_parser_set_decl_spec_type (decl_specs, type,
16135 token,
16136 /*type_definition_p=*/false);
16138 return type;
16140 case RID_BASES:
16141 case RID_DIRECT_BASES:
16142 type = cp_parser_trait_expr (parser, token->keyword);
16143 if (decl_specs)
16144 cp_parser_set_decl_spec_type (decl_specs, type,
16145 token,
16146 /*type_definition_p=*/false);
16147 return type;
16148 default:
16149 break;
16152 /* If token is an already-parsed decltype not followed by ::,
16153 it's a simple-type-specifier. */
16154 if (token->type == CPP_DECLTYPE
16155 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
16157 type = saved_checks_value (token->u.tree_check_value);
16158 if (decl_specs)
16160 cp_parser_set_decl_spec_type (decl_specs, type,
16161 token,
16162 /*type_definition_p=*/false);
16163 /* Remember that we are handling a decltype in order to
16164 implement the resolution of DR 1510 when the argument
16165 isn't instantiation dependent. */
16166 decl_specs->decltype_p = true;
16168 cp_lexer_consume_token (parser->lexer);
16169 return type;
16172 /* If the type-specifier was for a built-in type, we're done. */
16173 if (type)
16175 /* Record the type. */
16176 if (decl_specs
16177 && (token->keyword != RID_SIGNED
16178 && token->keyword != RID_UNSIGNED
16179 && token->keyword != RID_SHORT
16180 && token->keyword != RID_LONG))
16181 cp_parser_set_decl_spec_type (decl_specs,
16182 type,
16183 token,
16184 /*type_definition_p=*/false);
16185 if (decl_specs)
16186 decl_specs->any_specifiers_p = true;
16188 /* Consume the token. */
16189 cp_lexer_consume_token (parser->lexer);
16191 if (type == error_mark_node)
16192 return error_mark_node;
16194 /* There is no valid C++ program where a non-template type is
16195 followed by a "<". That usually indicates that the user thought
16196 that the type was a template. */
16197 cp_parser_check_for_invalid_template_id (parser, type, none_type,
16198 token->location);
16200 return TYPE_NAME (type);
16203 /* The type-specifier must be a user-defined type. */
16204 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
16206 bool qualified_p;
16207 bool global_p;
16209 /* Don't gobble tokens or issue error messages if this is an
16210 optional type-specifier. */
16211 if (flags & CP_PARSER_FLAGS_OPTIONAL)
16212 cp_parser_parse_tentatively (parser);
16214 /* Look for the optional `::' operator. */
16215 global_p
16216 = (cp_parser_global_scope_opt (parser,
16217 /*current_scope_valid_p=*/false)
16218 != NULL_TREE);
16219 /* Look for the nested-name specifier. */
16220 qualified_p
16221 = (cp_parser_nested_name_specifier_opt (parser,
16222 /*typename_keyword_p=*/false,
16223 /*check_dependency_p=*/true,
16224 /*type_p=*/false,
16225 /*is_declaration=*/false)
16226 != NULL_TREE);
16227 token = cp_lexer_peek_token (parser->lexer);
16228 /* If we have seen a nested-name-specifier, and the next token
16229 is `template', then we are using the template-id production. */
16230 if (parser->scope
16231 && cp_parser_optional_template_keyword (parser))
16233 /* Look for the template-id. */
16234 type = cp_parser_template_id (parser,
16235 /*template_keyword_p=*/true,
16236 /*check_dependency_p=*/true,
16237 none_type,
16238 /*is_declaration=*/false);
16239 /* If the template-id did not name a type, we are out of
16240 luck. */
16241 if (TREE_CODE (type) != TYPE_DECL)
16243 cp_parser_error (parser, "expected template-id for type");
16244 type = NULL_TREE;
16247 /* Otherwise, look for a type-name. */
16248 else
16249 type = cp_parser_type_name (parser);
16250 /* Keep track of all name-lookups performed in class scopes. */
16251 if (type
16252 && !global_p
16253 && !qualified_p
16254 && TREE_CODE (type) == TYPE_DECL
16255 && identifier_p (DECL_NAME (type)))
16256 maybe_note_name_used_in_class (DECL_NAME (type), type);
16257 /* If it didn't work out, we don't have a TYPE. */
16258 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
16259 && !cp_parser_parse_definitely (parser))
16260 type = NULL_TREE;
16261 if (type && decl_specs)
16262 cp_parser_set_decl_spec_type (decl_specs, type,
16263 token,
16264 /*type_definition_p=*/false);
16267 /* If we didn't get a type-name, issue an error message. */
16268 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16270 cp_parser_error (parser, "expected type-name");
16271 return error_mark_node;
16274 if (type && type != error_mark_node)
16276 /* See if TYPE is an Objective-C type, and if so, parse and
16277 accept any protocol references following it. Do this before
16278 the cp_parser_check_for_invalid_template_id() call, because
16279 Objective-C types can be followed by '<...>' which would
16280 enclose protocol names rather than template arguments, and so
16281 everything is fine. */
16282 if (c_dialect_objc () && !parser->scope
16283 && (objc_is_id (type) || objc_is_class_name (type)))
16285 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16286 tree qual_type = objc_get_protocol_qualified_type (type, protos);
16288 /* Clobber the "unqualified" type previously entered into
16289 DECL_SPECS with the new, improved protocol-qualified version. */
16290 if (decl_specs)
16291 decl_specs->type = qual_type;
16293 return qual_type;
16296 /* There is no valid C++ program where a non-template type is
16297 followed by a "<". That usually indicates that the user
16298 thought that the type was a template. */
16299 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
16300 none_type,
16301 token->location);
16304 return type;
16307 /* Parse a type-name.
16309 type-name:
16310 class-name
16311 enum-name
16312 typedef-name
16313 simple-template-id [in c++0x]
16315 enum-name:
16316 identifier
16318 typedef-name:
16319 identifier
16321 Concepts:
16323 type-name:
16324 concept-name
16325 partial-concept-id
16327 concept-name:
16328 identifier
16330 Returns a TYPE_DECL for the type. */
16332 static tree
16333 cp_parser_type_name (cp_parser* parser)
16335 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
16338 /* See above. */
16339 static tree
16340 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
16342 tree type_decl;
16344 /* We can't know yet whether it is a class-name or not. */
16345 cp_parser_parse_tentatively (parser);
16346 /* Try a class-name. */
16347 type_decl = cp_parser_class_name (parser,
16348 typename_keyword_p,
16349 /*template_keyword_p=*/false,
16350 none_type,
16351 /*check_dependency_p=*/true,
16352 /*class_head_p=*/false,
16353 /*is_declaration=*/false);
16354 /* If it's not a class-name, keep looking. */
16355 if (!cp_parser_parse_definitely (parser))
16357 if (cxx_dialect < cxx11)
16358 /* It must be a typedef-name or an enum-name. */
16359 return cp_parser_nonclass_name (parser);
16361 cp_parser_parse_tentatively (parser);
16362 /* It is either a simple-template-id representing an
16363 instantiation of an alias template... */
16364 type_decl = cp_parser_template_id (parser,
16365 /*template_keyword_p=*/false,
16366 /*check_dependency_p=*/true,
16367 none_type,
16368 /*is_declaration=*/false);
16369 /* Note that this must be an instantiation of an alias template
16370 because [temp.names]/6 says:
16372 A template-id that names an alias template specialization
16373 is a type-name.
16375 Whereas [temp.names]/7 says:
16377 A simple-template-id that names a class template
16378 specialization is a class-name.
16380 With concepts, this could also be a partial-concept-id that
16381 declares a non-type template parameter. */
16382 if (type_decl != NULL_TREE
16383 && TREE_CODE (type_decl) == TYPE_DECL
16384 && TYPE_DECL_ALIAS_P (type_decl))
16385 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
16386 else if (is_constrained_parameter (type_decl))
16387 /* Don't do anything. */ ;
16388 else
16389 cp_parser_simulate_error (parser);
16391 if (!cp_parser_parse_definitely (parser))
16392 /* ... Or a typedef-name or an enum-name. */
16393 return cp_parser_nonclass_name (parser);
16396 return type_decl;
16399 /* Check if DECL and ARGS can form a constrained-type-specifier.
16400 If ARGS is non-null, we try to form a concept check of the
16401 form DECL<?, ARGS> where ? is a wildcard that matches any
16402 kind of template argument. If ARGS is NULL, then we try to
16403 form a concept check of the form DECL<?>. */
16405 static tree
16406 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
16407 tree decl, tree args)
16409 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
16411 /* If we a constrained-type-specifier cannot be deduced. */
16412 if (parser->prevent_constrained_type_specifiers)
16413 return NULL_TREE;
16415 /* A constrained type specifier can only be found in an
16416 overload set or as a reference to a template declaration.
16418 FIXME: This might be masking a bug. It's possible that
16419 that the deduction below is causing template specializations
16420 to be formed with the wildcard as an argument. */
16421 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
16422 return NULL_TREE;
16424 /* Try to build a call expression that evaluates the
16425 concept. This can fail if the overload set refers
16426 only to non-templates. */
16427 tree placeholder = build_nt (WILDCARD_DECL);
16428 tree check = build_concept_check (decl, placeholder, args);
16429 if (check == error_mark_node)
16430 return NULL_TREE;
16432 /* Deduce the checked constraint and the prototype parameter.
16434 FIXME: In certain cases, failure to deduce should be a
16435 diagnosable error. */
16436 tree conc;
16437 tree proto;
16438 if (!deduce_constrained_parameter (check, conc, proto))
16439 return NULL_TREE;
16441 /* In template parameter scope, this results in a constrained
16442 parameter. Return a descriptor of that parm. */
16443 if (processing_template_parmlist)
16444 return build_constrained_parameter (conc, proto, args);
16446 /* In a parameter-declaration-clause, constrained-type
16447 specifiers result in invented template parameters. */
16448 if (parser->auto_is_implicit_function_template_parm_p)
16450 tree x = build_constrained_parameter (conc, proto, args);
16451 return synthesize_implicit_template_parm (parser, x);
16453 else
16455 /* Otherwise, we're in a context where the constrained
16456 type name is deduced and the constraint applies
16457 after deduction. */
16458 return make_constrained_auto (conc, args);
16461 return NULL_TREE;
16464 /* If DECL refers to a concept, return a TYPE_DECL representing
16465 the result of using the constrained type specifier in the
16466 current context. DECL refers to a concept if
16468 - it is an overload set containing a function concept taking a single
16469 type argument, or
16471 - it is a variable concept taking a single type argument. */
16473 static tree
16474 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
16476 if (flag_concepts
16477 && (TREE_CODE (decl) == OVERLOAD
16478 || BASELINK_P (decl)
16479 || variable_concept_p (decl)))
16480 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
16481 else
16482 return NULL_TREE;
16485 /* Check if DECL and ARGS form a partial-concept-id. If so,
16486 assign ID to the resulting constrained placeholder.
16488 Returns true if the partial-concept-id designates a placeholder
16489 and false otherwise. Note that *id is set to NULL_TREE in
16490 this case. */
16492 static tree
16493 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
16495 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
16498 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
16499 or a concept-name.
16501 enum-name:
16502 identifier
16504 typedef-name:
16505 identifier
16507 concept-name:
16508 identifier
16510 Returns a TYPE_DECL for the type. */
16512 static tree
16513 cp_parser_nonclass_name (cp_parser* parser)
16515 tree type_decl;
16516 tree identifier;
16518 cp_token *token = cp_lexer_peek_token (parser->lexer);
16519 identifier = cp_parser_identifier (parser);
16520 if (identifier == error_mark_node)
16521 return error_mark_node;
16523 /* Look up the type-name. */
16524 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
16526 type_decl = strip_using_decl (type_decl);
16528 /* If we found an overload set, then it may refer to a concept-name. */
16529 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
16530 type_decl = decl;
16532 if (TREE_CODE (type_decl) != TYPE_DECL
16533 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
16535 /* See if this is an Objective-C type. */
16536 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16537 tree type = objc_get_protocol_qualified_type (identifier, protos);
16538 if (type)
16539 type_decl = TYPE_NAME (type);
16542 /* Issue an error if we did not find a type-name. */
16543 if (TREE_CODE (type_decl) != TYPE_DECL
16544 /* In Objective-C, we have the complication that class names are
16545 normally type names and start declarations (eg, the
16546 "NSObject" in "NSObject *object;"), but can be used in an
16547 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
16548 is an expression. So, a classname followed by a dot is not a
16549 valid type-name. */
16550 || (objc_is_class_name (TREE_TYPE (type_decl))
16551 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
16553 if (!cp_parser_simulate_error (parser))
16554 cp_parser_name_lookup_error (parser, identifier, type_decl,
16555 NLE_TYPE, token->location);
16556 return error_mark_node;
16558 /* Remember that the name was used in the definition of the
16559 current class so that we can check later to see if the
16560 meaning would have been different after the class was
16561 entirely defined. */
16562 else if (type_decl != error_mark_node
16563 && !parser->scope)
16564 maybe_note_name_used_in_class (identifier, type_decl);
16566 return type_decl;
16569 /* Parse an elaborated-type-specifier. Note that the grammar given
16570 here incorporates the resolution to DR68.
16572 elaborated-type-specifier:
16573 class-key :: [opt] nested-name-specifier [opt] identifier
16574 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
16575 enum-key :: [opt] nested-name-specifier [opt] identifier
16576 typename :: [opt] nested-name-specifier identifier
16577 typename :: [opt] nested-name-specifier template [opt]
16578 template-id
16580 GNU extension:
16582 elaborated-type-specifier:
16583 class-key attributes :: [opt] nested-name-specifier [opt] identifier
16584 class-key attributes :: [opt] nested-name-specifier [opt]
16585 template [opt] template-id
16586 enum attributes :: [opt] nested-name-specifier [opt] identifier
16588 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
16589 declared `friend'. If IS_DECLARATION is TRUE, then this
16590 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
16591 something is being declared.
16593 Returns the TYPE specified. */
16595 static tree
16596 cp_parser_elaborated_type_specifier (cp_parser* parser,
16597 bool is_friend,
16598 bool is_declaration)
16600 enum tag_types tag_type;
16601 tree identifier;
16602 tree type = NULL_TREE;
16603 tree attributes = NULL_TREE;
16604 tree globalscope;
16605 cp_token *token = NULL;
16607 /* See if we're looking at the `enum' keyword. */
16608 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
16610 /* Consume the `enum' token. */
16611 cp_lexer_consume_token (parser->lexer);
16612 /* Remember that it's an enumeration type. */
16613 tag_type = enum_type;
16614 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
16615 enums) is used here. */
16616 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
16617 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
16619 pedwarn (input_location, 0, "elaborated-type-specifier "
16620 "for a scoped enum must not use the %<%D%> keyword",
16621 cp_lexer_peek_token (parser->lexer)->u.value);
16622 /* Consume the `struct' or `class' and parse it anyway. */
16623 cp_lexer_consume_token (parser->lexer);
16625 /* Parse the attributes. */
16626 attributes = cp_parser_attributes_opt (parser);
16628 /* Or, it might be `typename'. */
16629 else if (cp_lexer_next_token_is_keyword (parser->lexer,
16630 RID_TYPENAME))
16632 /* Consume the `typename' token. */
16633 cp_lexer_consume_token (parser->lexer);
16634 /* Remember that it's a `typename' type. */
16635 tag_type = typename_type;
16637 /* Otherwise it must be a class-key. */
16638 else
16640 tag_type = cp_parser_class_key (parser);
16641 if (tag_type == none_type)
16642 return error_mark_node;
16643 /* Parse the attributes. */
16644 attributes = cp_parser_attributes_opt (parser);
16647 /* Look for the `::' operator. */
16648 globalscope = cp_parser_global_scope_opt (parser,
16649 /*current_scope_valid_p=*/false);
16650 /* Look for the nested-name-specifier. */
16651 if (tag_type == typename_type && !globalscope)
16653 if (!cp_parser_nested_name_specifier (parser,
16654 /*typename_keyword_p=*/true,
16655 /*check_dependency_p=*/true,
16656 /*type_p=*/true,
16657 is_declaration))
16658 return error_mark_node;
16660 else
16661 /* Even though `typename' is not present, the proposed resolution
16662 to Core Issue 180 says that in `class A<T>::B', `B' should be
16663 considered a type-name, even if `A<T>' is dependent. */
16664 cp_parser_nested_name_specifier_opt (parser,
16665 /*typename_keyword_p=*/true,
16666 /*check_dependency_p=*/true,
16667 /*type_p=*/true,
16668 is_declaration);
16669 /* For everything but enumeration types, consider a template-id.
16670 For an enumeration type, consider only a plain identifier. */
16671 if (tag_type != enum_type)
16673 bool template_p = false;
16674 tree decl;
16676 /* Allow the `template' keyword. */
16677 template_p = cp_parser_optional_template_keyword (parser);
16678 /* If we didn't see `template', we don't know if there's a
16679 template-id or not. */
16680 if (!template_p)
16681 cp_parser_parse_tentatively (parser);
16682 /* Parse the template-id. */
16683 token = cp_lexer_peek_token (parser->lexer);
16684 decl = cp_parser_template_id (parser, template_p,
16685 /*check_dependency_p=*/true,
16686 tag_type,
16687 is_declaration);
16688 /* If we didn't find a template-id, look for an ordinary
16689 identifier. */
16690 if (!template_p && !cp_parser_parse_definitely (parser))
16692 /* We can get here when cp_parser_template_id, called by
16693 cp_parser_class_name with tag_type == none_type, succeeds
16694 and caches a BASELINK. Then, when called again here,
16695 instead of failing and returning an error_mark_node
16696 returns it (see template/typename17.C in C++11).
16697 ??? Could we diagnose this earlier? */
16698 else if (tag_type == typename_type && BASELINK_P (decl))
16700 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
16701 type = error_mark_node;
16703 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
16704 in effect, then we must assume that, upon instantiation, the
16705 template will correspond to a class. */
16706 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16707 && tag_type == typename_type)
16708 type = make_typename_type (parser->scope, decl,
16709 typename_type,
16710 /*complain=*/tf_error);
16711 /* If the `typename' keyword is in effect and DECL is not a type
16712 decl, then type is non existent. */
16713 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
16715 else if (TREE_CODE (decl) == TYPE_DECL)
16716 type = check_elaborated_type_specifier (tag_type, decl,
16717 /*allow_template_p=*/true);
16718 else if (decl == error_mark_node)
16719 type = error_mark_node;
16722 if (!type)
16724 token = cp_lexer_peek_token (parser->lexer);
16725 identifier = cp_parser_identifier (parser);
16727 if (identifier == error_mark_node)
16729 parser->scope = NULL_TREE;
16730 return error_mark_node;
16733 /* For a `typename', we needn't call xref_tag. */
16734 if (tag_type == typename_type
16735 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
16736 return cp_parser_make_typename_type (parser, identifier,
16737 token->location);
16739 /* Template parameter lists apply only if we are not within a
16740 function parameter list. */
16741 bool template_parm_lists_apply
16742 = parser->num_template_parameter_lists;
16743 if (template_parm_lists_apply)
16744 for (cp_binding_level *s = current_binding_level;
16745 s && s->kind != sk_template_parms;
16746 s = s->level_chain)
16747 if (s->kind == sk_function_parms)
16748 template_parm_lists_apply = false;
16750 /* Look up a qualified name in the usual way. */
16751 if (parser->scope)
16753 tree decl;
16754 tree ambiguous_decls;
16756 decl = cp_parser_lookup_name (parser, identifier,
16757 tag_type,
16758 /*is_template=*/false,
16759 /*is_namespace=*/false,
16760 /*check_dependency=*/true,
16761 &ambiguous_decls,
16762 token->location);
16764 /* If the lookup was ambiguous, an error will already have been
16765 issued. */
16766 if (ambiguous_decls)
16767 return error_mark_node;
16769 /* If we are parsing friend declaration, DECL may be a
16770 TEMPLATE_DECL tree node here. However, we need to check
16771 whether this TEMPLATE_DECL results in valid code. Consider
16772 the following example:
16774 namespace N {
16775 template <class T> class C {};
16777 class X {
16778 template <class T> friend class N::C; // #1, valid code
16780 template <class T> class Y {
16781 friend class N::C; // #2, invalid code
16784 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
16785 name lookup of `N::C'. We see that friend declaration must
16786 be template for the code to be valid. Note that
16787 processing_template_decl does not work here since it is
16788 always 1 for the above two cases. */
16790 decl = (cp_parser_maybe_treat_template_as_class
16791 (decl, /*tag_name_p=*/is_friend
16792 && template_parm_lists_apply));
16794 if (TREE_CODE (decl) != TYPE_DECL)
16796 cp_parser_diagnose_invalid_type_name (parser,
16797 identifier,
16798 token->location);
16799 return error_mark_node;
16802 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
16804 bool allow_template = (template_parm_lists_apply
16805 || DECL_SELF_REFERENCE_P (decl));
16806 type = check_elaborated_type_specifier (tag_type, decl,
16807 allow_template);
16809 if (type == error_mark_node)
16810 return error_mark_node;
16813 /* Forward declarations of nested types, such as
16815 class C1::C2;
16816 class C1::C2::C3;
16818 are invalid unless all components preceding the final '::'
16819 are complete. If all enclosing types are complete, these
16820 declarations become merely pointless.
16822 Invalid forward declarations of nested types are errors
16823 caught elsewhere in parsing. Those that are pointless arrive
16824 here. */
16826 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16827 && !is_friend && !processing_explicit_instantiation)
16828 warning (0, "declaration %qD does not declare anything", decl);
16830 type = TREE_TYPE (decl);
16832 else
16834 /* An elaborated-type-specifier sometimes introduces a new type and
16835 sometimes names an existing type. Normally, the rule is that it
16836 introduces a new type only if there is not an existing type of
16837 the same name already in scope. For example, given:
16839 struct S {};
16840 void f() { struct S s; }
16842 the `struct S' in the body of `f' is the same `struct S' as in
16843 the global scope; the existing definition is used. However, if
16844 there were no global declaration, this would introduce a new
16845 local class named `S'.
16847 An exception to this rule applies to the following code:
16849 namespace N { struct S; }
16851 Here, the elaborated-type-specifier names a new type
16852 unconditionally; even if there is already an `S' in the
16853 containing scope this declaration names a new type.
16854 This exception only applies if the elaborated-type-specifier
16855 forms the complete declaration:
16857 [class.name]
16859 A declaration consisting solely of `class-key identifier ;' is
16860 either a redeclaration of the name in the current scope or a
16861 forward declaration of the identifier as a class name. It
16862 introduces the name into the current scope.
16864 We are in this situation precisely when the next token is a `;'.
16866 An exception to the exception is that a `friend' declaration does
16867 *not* name a new type; i.e., given:
16869 struct S { friend struct T; };
16871 `T' is not a new type in the scope of `S'.
16873 Also, `new struct S' or `sizeof (struct S)' never results in the
16874 definition of a new type; a new type can only be declared in a
16875 declaration context. */
16877 tag_scope ts;
16878 bool template_p;
16880 if (is_friend)
16881 /* Friends have special name lookup rules. */
16882 ts = ts_within_enclosing_non_class;
16883 else if (is_declaration
16884 && cp_lexer_next_token_is (parser->lexer,
16885 CPP_SEMICOLON))
16886 /* This is a `class-key identifier ;' */
16887 ts = ts_current;
16888 else
16889 ts = ts_global;
16891 template_p =
16892 (template_parm_lists_apply
16893 && (cp_parser_next_token_starts_class_definition_p (parser)
16894 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
16895 /* An unqualified name was used to reference this type, so
16896 there were no qualifying templates. */
16897 if (template_parm_lists_apply
16898 && !cp_parser_check_template_parameters (parser,
16899 /*num_templates=*/0,
16900 token->location,
16901 /*declarator=*/NULL))
16902 return error_mark_node;
16903 type = xref_tag (tag_type, identifier, ts, template_p);
16907 if (type == error_mark_node)
16908 return error_mark_node;
16910 /* Allow attributes on forward declarations of classes. */
16911 if (attributes)
16913 if (TREE_CODE (type) == TYPENAME_TYPE)
16914 warning (OPT_Wattributes,
16915 "attributes ignored on uninstantiated type");
16916 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
16917 && ! processing_explicit_instantiation)
16918 warning (OPT_Wattributes,
16919 "attributes ignored on template instantiation");
16920 else if (is_declaration && cp_parser_declares_only_class_p (parser))
16921 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
16922 else
16923 warning (OPT_Wattributes,
16924 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
16927 if (tag_type != enum_type)
16929 /* Indicate whether this class was declared as a `class' or as a
16930 `struct'. */
16931 if (CLASS_TYPE_P (type))
16932 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
16933 cp_parser_check_class_key (tag_type, type);
16936 /* A "<" cannot follow an elaborated type specifier. If that
16937 happens, the user was probably trying to form a template-id. */
16938 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
16939 token->location);
16941 return type;
16944 /* Parse an enum-specifier.
16946 enum-specifier:
16947 enum-head { enumerator-list [opt] }
16948 enum-head { enumerator-list , } [C++0x]
16950 enum-head:
16951 enum-key identifier [opt] enum-base [opt]
16952 enum-key nested-name-specifier identifier enum-base [opt]
16954 enum-key:
16955 enum
16956 enum class [C++0x]
16957 enum struct [C++0x]
16959 enum-base: [C++0x]
16960 : type-specifier-seq
16962 opaque-enum-specifier:
16963 enum-key identifier enum-base [opt] ;
16965 GNU Extensions:
16966 enum-key attributes[opt] identifier [opt] enum-base [opt]
16967 { enumerator-list [opt] }attributes[opt]
16968 enum-key attributes[opt] identifier [opt] enum-base [opt]
16969 { enumerator-list, }attributes[opt] [C++0x]
16971 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
16972 if the token stream isn't an enum-specifier after all. */
16974 static tree
16975 cp_parser_enum_specifier (cp_parser* parser)
16977 tree identifier;
16978 tree type = NULL_TREE;
16979 tree prev_scope;
16980 tree nested_name_specifier = NULL_TREE;
16981 tree attributes;
16982 bool scoped_enum_p = false;
16983 bool has_underlying_type = false;
16984 bool nested_being_defined = false;
16985 bool new_value_list = false;
16986 bool is_new_type = false;
16987 bool is_anonymous = false;
16988 tree underlying_type = NULL_TREE;
16989 cp_token *type_start_token = NULL;
16990 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
16992 parser->colon_corrects_to_scope_p = false;
16994 /* Parse tentatively so that we can back up if we don't find a
16995 enum-specifier. */
16996 cp_parser_parse_tentatively (parser);
16998 /* Caller guarantees that the current token is 'enum', an identifier
16999 possibly follows, and the token after that is an opening brace.
17000 If we don't have an identifier, fabricate an anonymous name for
17001 the enumeration being defined. */
17002 cp_lexer_consume_token (parser->lexer);
17004 /* Parse the "class" or "struct", which indicates a scoped
17005 enumeration type in C++0x. */
17006 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17007 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17009 if (cxx_dialect < cxx11)
17010 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17012 /* Consume the `struct' or `class' token. */
17013 cp_lexer_consume_token (parser->lexer);
17015 scoped_enum_p = true;
17018 attributes = cp_parser_attributes_opt (parser);
17020 /* Clear the qualification. */
17021 parser->scope = NULL_TREE;
17022 parser->qualifying_scope = NULL_TREE;
17023 parser->object_scope = NULL_TREE;
17025 /* Figure out in what scope the declaration is being placed. */
17026 prev_scope = current_scope ();
17028 type_start_token = cp_lexer_peek_token (parser->lexer);
17030 push_deferring_access_checks (dk_no_check);
17031 nested_name_specifier
17032 = cp_parser_nested_name_specifier_opt (parser,
17033 /*typename_keyword_p=*/true,
17034 /*check_dependency_p=*/false,
17035 /*type_p=*/false,
17036 /*is_declaration=*/false);
17038 if (nested_name_specifier)
17040 tree name;
17042 identifier = cp_parser_identifier (parser);
17043 name = cp_parser_lookup_name (parser, identifier,
17044 enum_type,
17045 /*is_template=*/false,
17046 /*is_namespace=*/false,
17047 /*check_dependency=*/true,
17048 /*ambiguous_decls=*/NULL,
17049 input_location);
17050 if (name && name != error_mark_node)
17052 type = TREE_TYPE (name);
17053 if (TREE_CODE (type) == TYPENAME_TYPE)
17055 /* Are template enums allowed in ISO? */
17056 if (template_parm_scope_p ())
17057 pedwarn (type_start_token->location, OPT_Wpedantic,
17058 "%qD is an enumeration template", name);
17059 /* ignore a typename reference, for it will be solved by name
17060 in start_enum. */
17061 type = NULL_TREE;
17064 else if (nested_name_specifier == error_mark_node)
17065 /* We already issued an error. */;
17066 else
17068 error_at (type_start_token->location,
17069 "%qD does not name an enumeration in %qT",
17070 identifier, nested_name_specifier);
17071 nested_name_specifier = error_mark_node;
17074 else
17076 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17077 identifier = cp_parser_identifier (parser);
17078 else
17080 identifier = make_anon_name ();
17081 is_anonymous = true;
17082 if (scoped_enum_p)
17083 error_at (type_start_token->location,
17084 "anonymous scoped enum is not allowed");
17087 pop_deferring_access_checks ();
17089 /* Check for the `:' that denotes a specified underlying type in C++0x.
17090 Note that a ':' could also indicate a bitfield width, however. */
17091 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17093 cp_decl_specifier_seq type_specifiers;
17095 /* Consume the `:'. */
17096 cp_lexer_consume_token (parser->lexer);
17098 /* Parse the type-specifier-seq. */
17099 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17100 /*is_trailing_return=*/false,
17101 &type_specifiers);
17103 /* At this point this is surely not elaborated type specifier. */
17104 if (!cp_parser_parse_definitely (parser))
17105 return NULL_TREE;
17107 if (cxx_dialect < cxx11)
17108 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17110 has_underlying_type = true;
17112 /* If that didn't work, stop. */
17113 if (type_specifiers.type != error_mark_node)
17115 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
17116 /*initialized=*/0, NULL);
17117 if (underlying_type == error_mark_node
17118 || check_for_bare_parameter_packs (underlying_type))
17119 underlying_type = NULL_TREE;
17123 /* Look for the `{' but don't consume it yet. */
17124 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17126 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
17128 cp_parser_error (parser, "expected %<{%>");
17129 if (has_underlying_type)
17131 type = NULL_TREE;
17132 goto out;
17135 /* An opaque-enum-specifier must have a ';' here. */
17136 if ((scoped_enum_p || underlying_type)
17137 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17139 cp_parser_error (parser, "expected %<;%> or %<{%>");
17140 if (has_underlying_type)
17142 type = NULL_TREE;
17143 goto out;
17148 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
17149 return NULL_TREE;
17151 if (nested_name_specifier)
17153 if (CLASS_TYPE_P (nested_name_specifier))
17155 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
17156 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
17157 push_scope (nested_name_specifier);
17159 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17161 push_nested_namespace (nested_name_specifier);
17165 /* Issue an error message if type-definitions are forbidden here. */
17166 if (!cp_parser_check_type_definition (parser))
17167 type = error_mark_node;
17168 else
17169 /* Create the new type. We do this before consuming the opening
17170 brace so the enum will be recorded as being on the line of its
17171 tag (or the 'enum' keyword, if there is no tag). */
17172 type = start_enum (identifier, type, underlying_type,
17173 attributes, scoped_enum_p, &is_new_type);
17175 /* If the next token is not '{' it is an opaque-enum-specifier or an
17176 elaborated-type-specifier. */
17177 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17179 timevar_push (TV_PARSE_ENUM);
17180 if (nested_name_specifier
17181 && nested_name_specifier != error_mark_node)
17183 /* The following catches invalid code such as:
17184 enum class S<int>::E { A, B, C }; */
17185 if (!processing_specialization
17186 && CLASS_TYPE_P (nested_name_specifier)
17187 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
17188 error_at (type_start_token->location, "cannot add an enumerator "
17189 "list to a template instantiation");
17191 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
17193 error_at (type_start_token->location,
17194 "%<%T::%E%> has not been declared",
17195 TYPE_CONTEXT (nested_name_specifier),
17196 nested_name_specifier);
17197 type = error_mark_node;
17199 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
17200 && !CLASS_TYPE_P (nested_name_specifier))
17202 error_at (type_start_token->location, "nested name specifier "
17203 "%qT for enum declaration does not name a class "
17204 "or namespace", nested_name_specifier);
17205 type = error_mark_node;
17207 /* If that scope does not contain the scope in which the
17208 class was originally declared, the program is invalid. */
17209 else if (prev_scope && !is_ancestor (prev_scope,
17210 nested_name_specifier))
17212 if (at_namespace_scope_p ())
17213 error_at (type_start_token->location,
17214 "declaration of %qD in namespace %qD which does not "
17215 "enclose %qD",
17216 type, prev_scope, nested_name_specifier);
17217 else
17218 error_at (type_start_token->location,
17219 "declaration of %qD in %qD which does not "
17220 "enclose %qD",
17221 type, prev_scope, nested_name_specifier);
17222 type = error_mark_node;
17226 if (scoped_enum_p)
17227 begin_scope (sk_scoped_enum, type);
17229 /* Consume the opening brace. */
17230 cp_lexer_consume_token (parser->lexer);
17232 if (type == error_mark_node)
17233 ; /* Nothing to add */
17234 else if (OPAQUE_ENUM_P (type)
17235 || (cxx_dialect > cxx98 && processing_specialization))
17237 new_value_list = true;
17238 SET_OPAQUE_ENUM_P (type, false);
17239 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17241 else
17243 error_at (type_start_token->location,
17244 "multiple definition of %q#T", type);
17245 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
17246 "previous definition here");
17247 type = error_mark_node;
17250 if (type == error_mark_node)
17251 cp_parser_skip_to_end_of_block_or_statement (parser);
17252 /* If the next token is not '}', then there are some enumerators. */
17253 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17255 if (is_anonymous && !scoped_enum_p)
17256 pedwarn (type_start_token->location, OPT_Wpedantic,
17257 "ISO C++ forbids empty anonymous enum");
17259 else
17260 cp_parser_enumerator_list (parser, type);
17262 /* Consume the final '}'. */
17263 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17265 if (scoped_enum_p)
17266 finish_scope ();
17267 timevar_pop (TV_PARSE_ENUM);
17269 else
17271 /* If a ';' follows, then it is an opaque-enum-specifier
17272 and additional restrictions apply. */
17273 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17275 if (is_anonymous)
17276 error_at (type_start_token->location,
17277 "opaque-enum-specifier without name");
17278 else if (nested_name_specifier)
17279 error_at (type_start_token->location,
17280 "opaque-enum-specifier must use a simple identifier");
17284 /* Look for trailing attributes to apply to this enumeration, and
17285 apply them if appropriate. */
17286 if (cp_parser_allow_gnu_extensions_p (parser))
17288 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
17289 cplus_decl_attributes (&type,
17290 trailing_attr,
17291 (int) ATTR_FLAG_TYPE_IN_PLACE);
17294 /* Finish up the enumeration. */
17295 if (type != error_mark_node)
17297 if (new_value_list)
17298 finish_enum_value_list (type);
17299 if (is_new_type)
17300 finish_enum (type);
17303 if (nested_name_specifier)
17305 if (CLASS_TYPE_P (nested_name_specifier))
17307 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
17308 pop_scope (nested_name_specifier);
17310 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17312 pop_nested_namespace (nested_name_specifier);
17315 out:
17316 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17317 return type;
17320 /* Parse an enumerator-list. The enumerators all have the indicated
17321 TYPE.
17323 enumerator-list:
17324 enumerator-definition
17325 enumerator-list , enumerator-definition */
17327 static void
17328 cp_parser_enumerator_list (cp_parser* parser, tree type)
17330 while (true)
17332 /* Parse an enumerator-definition. */
17333 cp_parser_enumerator_definition (parser, type);
17335 /* If the next token is not a ',', we've reached the end of
17336 the list. */
17337 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17338 break;
17339 /* Otherwise, consume the `,' and keep going. */
17340 cp_lexer_consume_token (parser->lexer);
17341 /* If the next token is a `}', there is a trailing comma. */
17342 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17344 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
17345 pedwarn (input_location, OPT_Wpedantic,
17346 "comma at end of enumerator list");
17347 break;
17352 /* Parse an enumerator-definition. The enumerator has the indicated
17353 TYPE.
17355 enumerator-definition:
17356 enumerator
17357 enumerator = constant-expression
17359 enumerator:
17360 identifier
17362 GNU Extensions:
17364 enumerator-definition:
17365 enumerator attributes [opt]
17366 enumerator attributes [opt] = constant-expression */
17368 static void
17369 cp_parser_enumerator_definition (cp_parser* parser, tree type)
17371 tree identifier;
17372 tree value;
17373 location_t loc;
17375 /* Save the input location because we are interested in the location
17376 of the identifier and not the location of the explicit value. */
17377 loc = cp_lexer_peek_token (parser->lexer)->location;
17379 /* Look for the identifier. */
17380 identifier = cp_parser_identifier (parser);
17381 if (identifier == error_mark_node)
17382 return;
17384 /* Parse any specified attributes. */
17385 tree attrs = cp_parser_attributes_opt (parser);
17387 /* If the next token is an '=', then there is an explicit value. */
17388 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17390 /* Consume the `=' token. */
17391 cp_lexer_consume_token (parser->lexer);
17392 /* Parse the value. */
17393 value = cp_parser_constant_expression (parser);
17395 else
17396 value = NULL_TREE;
17398 /* If we are processing a template, make sure the initializer of the
17399 enumerator doesn't contain any bare template parameter pack. */
17400 if (check_for_bare_parameter_packs (value))
17401 value = error_mark_node;
17403 /* Create the enumerator. */
17404 build_enumerator (identifier, value, type, attrs, loc);
17407 /* Parse a namespace-name.
17409 namespace-name:
17410 original-namespace-name
17411 namespace-alias
17413 Returns the NAMESPACE_DECL for the namespace. */
17415 static tree
17416 cp_parser_namespace_name (cp_parser* parser)
17418 tree identifier;
17419 tree namespace_decl;
17421 cp_token *token = cp_lexer_peek_token (parser->lexer);
17423 /* Get the name of the namespace. */
17424 identifier = cp_parser_identifier (parser);
17425 if (identifier == error_mark_node)
17426 return error_mark_node;
17428 /* Look up the identifier in the currently active scope. Look only
17429 for namespaces, due to:
17431 [basic.lookup.udir]
17433 When looking up a namespace-name in a using-directive or alias
17434 definition, only namespace names are considered.
17436 And:
17438 [basic.lookup.qual]
17440 During the lookup of a name preceding the :: scope resolution
17441 operator, object, function, and enumerator names are ignored.
17443 (Note that cp_parser_qualifying_entity only calls this
17444 function if the token after the name is the scope resolution
17445 operator.) */
17446 namespace_decl = cp_parser_lookup_name (parser, identifier,
17447 none_type,
17448 /*is_template=*/false,
17449 /*is_namespace=*/true,
17450 /*check_dependency=*/true,
17451 /*ambiguous_decls=*/NULL,
17452 token->location);
17453 /* If it's not a namespace, issue an error. */
17454 if (namespace_decl == error_mark_node
17455 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
17457 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17458 error_at (token->location, "%qD is not a namespace-name", identifier);
17459 cp_parser_error (parser, "expected namespace-name");
17460 namespace_decl = error_mark_node;
17463 return namespace_decl;
17466 /* Parse a namespace-definition.
17468 namespace-definition:
17469 named-namespace-definition
17470 unnamed-namespace-definition
17472 named-namespace-definition:
17473 original-namespace-definition
17474 extension-namespace-definition
17476 original-namespace-definition:
17477 namespace identifier { namespace-body }
17479 extension-namespace-definition:
17480 namespace original-namespace-name { namespace-body }
17482 unnamed-namespace-definition:
17483 namespace { namespace-body } */
17485 static void
17486 cp_parser_namespace_definition (cp_parser* parser)
17488 tree identifier, attribs;
17489 bool has_visibility;
17490 bool is_inline;
17491 cp_token* token;
17492 int nested_definition_count = 0;
17494 cp_ensure_no_omp_declare_simd (parser);
17495 cp_ensure_no_oacc_routine (parser);
17496 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
17498 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
17499 is_inline = true;
17500 cp_lexer_consume_token (parser->lexer);
17502 else
17503 is_inline = false;
17505 /* Look for the `namespace' keyword. */
17506 token = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17508 /* Parse any specified attributes before the identifier. */
17509 attribs = cp_parser_attributes_opt (parser);
17511 /* Get the name of the namespace. We do not attempt to distinguish
17512 between an original-namespace-definition and an
17513 extension-namespace-definition at this point. The semantic
17514 analysis routines are responsible for that. */
17515 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17516 identifier = cp_parser_identifier (parser);
17517 else
17518 identifier = NULL_TREE;
17520 /* Parse any specified attributes after the identifier. */
17521 tree post_ident_attribs = cp_parser_attributes_opt (parser);
17522 if (post_ident_attribs)
17524 if (attribs)
17525 attribs = chainon (attribs, post_ident_attribs);
17526 else
17527 attribs = post_ident_attribs;
17530 /* Start the namespace. */
17531 push_namespace (identifier);
17533 /* Parse any nested namespace definition. */
17534 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17536 if (attribs)
17537 error_at (token->location, "a nested namespace definition cannot have attributes");
17538 if (cxx_dialect < cxx1z)
17539 pedwarn (input_location, OPT_Wpedantic,
17540 "nested namespace definitions only available with "
17541 "-std=c++1z or -std=gnu++1z");
17542 if (is_inline)
17543 error_at (token->location, "a nested namespace definition cannot be inline");
17544 while (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17546 cp_lexer_consume_token (parser->lexer);
17547 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17548 identifier = cp_parser_identifier (parser);
17549 else
17551 cp_parser_error (parser, "nested identifier required");
17552 break;
17554 ++nested_definition_count;
17555 push_namespace (identifier);
17559 /* Look for the `{' to validate starting the namespace. */
17560 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
17562 /* "inline namespace" is equivalent to a stub namespace definition
17563 followed by a strong using directive. */
17564 if (is_inline)
17566 tree name_space = current_namespace;
17567 /* Set up namespace association. */
17568 DECL_NAMESPACE_ASSOCIATIONS (name_space)
17569 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
17570 DECL_NAMESPACE_ASSOCIATIONS (name_space));
17571 /* Import the contents of the inline namespace. */
17572 pop_namespace ();
17573 do_using_directive (name_space);
17574 push_namespace (identifier);
17577 has_visibility = handle_namespace_attrs (current_namespace, attribs);
17579 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
17581 /* Parse the body of the namespace. */
17582 cp_parser_namespace_body (parser);
17584 if (has_visibility)
17585 pop_visibility (1);
17587 /* Finish the nested namespace definitions. */
17588 while (nested_definition_count--)
17589 pop_namespace ();
17591 /* Finish the namespace. */
17592 pop_namespace ();
17593 /* Look for the final `}'. */
17594 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17597 /* Parse a namespace-body.
17599 namespace-body:
17600 declaration-seq [opt] */
17602 static void
17603 cp_parser_namespace_body (cp_parser* parser)
17605 cp_parser_declaration_seq_opt (parser);
17608 /* Parse a namespace-alias-definition.
17610 namespace-alias-definition:
17611 namespace identifier = qualified-namespace-specifier ; */
17613 static void
17614 cp_parser_namespace_alias_definition (cp_parser* parser)
17616 tree identifier;
17617 tree namespace_specifier;
17619 cp_token *token = cp_lexer_peek_token (parser->lexer);
17621 /* Look for the `namespace' keyword. */
17622 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17623 /* Look for the identifier. */
17624 identifier = cp_parser_identifier (parser);
17625 if (identifier == error_mark_node)
17626 return;
17627 /* Look for the `=' token. */
17628 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
17629 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17631 error_at (token->location, "%<namespace%> definition is not allowed here");
17632 /* Skip the definition. */
17633 cp_lexer_consume_token (parser->lexer);
17634 if (cp_parser_skip_to_closing_brace (parser))
17635 cp_lexer_consume_token (parser->lexer);
17636 return;
17638 cp_parser_require (parser, CPP_EQ, RT_EQ);
17639 /* Look for the qualified-namespace-specifier. */
17640 namespace_specifier
17641 = cp_parser_qualified_namespace_specifier (parser);
17642 /* Look for the `;' token. */
17643 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17645 /* Register the alias in the symbol table. */
17646 do_namespace_alias (identifier, namespace_specifier);
17649 /* Parse a qualified-namespace-specifier.
17651 qualified-namespace-specifier:
17652 :: [opt] nested-name-specifier [opt] namespace-name
17654 Returns a NAMESPACE_DECL corresponding to the specified
17655 namespace. */
17657 static tree
17658 cp_parser_qualified_namespace_specifier (cp_parser* parser)
17660 /* Look for the optional `::'. */
17661 cp_parser_global_scope_opt (parser,
17662 /*current_scope_valid_p=*/false);
17664 /* Look for the optional nested-name-specifier. */
17665 cp_parser_nested_name_specifier_opt (parser,
17666 /*typename_keyword_p=*/false,
17667 /*check_dependency_p=*/true,
17668 /*type_p=*/false,
17669 /*is_declaration=*/true);
17671 return cp_parser_namespace_name (parser);
17674 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
17675 access declaration.
17677 using-declaration:
17678 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
17679 using :: unqualified-id ;
17681 access-declaration:
17682 qualified-id ;
17686 static bool
17687 cp_parser_using_declaration (cp_parser* parser,
17688 bool access_declaration_p)
17690 cp_token *token;
17691 bool typename_p = false;
17692 bool global_scope_p;
17693 tree decl;
17694 tree identifier;
17695 tree qscope;
17696 int oldcount = errorcount;
17697 cp_token *diag_token = NULL;
17699 if (access_declaration_p)
17701 diag_token = cp_lexer_peek_token (parser->lexer);
17702 cp_parser_parse_tentatively (parser);
17704 else
17706 /* Look for the `using' keyword. */
17707 cp_parser_require_keyword (parser, RID_USING, RT_USING);
17709 /* Peek at the next token. */
17710 token = cp_lexer_peek_token (parser->lexer);
17711 /* See if it's `typename'. */
17712 if (token->keyword == RID_TYPENAME)
17714 /* Remember that we've seen it. */
17715 typename_p = true;
17716 /* Consume the `typename' token. */
17717 cp_lexer_consume_token (parser->lexer);
17721 /* Look for the optional global scope qualification. */
17722 global_scope_p
17723 = (cp_parser_global_scope_opt (parser,
17724 /*current_scope_valid_p=*/false)
17725 != NULL_TREE);
17727 /* If we saw `typename', or didn't see `::', then there must be a
17728 nested-name-specifier present. */
17729 if (typename_p || !global_scope_p)
17731 qscope = cp_parser_nested_name_specifier (parser, typename_p,
17732 /*check_dependency_p=*/true,
17733 /*type_p=*/false,
17734 /*is_declaration=*/true);
17735 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
17737 cp_parser_skip_to_end_of_block_or_statement (parser);
17738 return false;
17741 /* Otherwise, we could be in either of the two productions. In that
17742 case, treat the nested-name-specifier as optional. */
17743 else
17744 qscope = cp_parser_nested_name_specifier_opt (parser,
17745 /*typename_keyword_p=*/false,
17746 /*check_dependency_p=*/true,
17747 /*type_p=*/false,
17748 /*is_declaration=*/true);
17749 if (!qscope)
17750 qscope = global_namespace;
17751 else if (UNSCOPED_ENUM_P (qscope))
17752 qscope = CP_TYPE_CONTEXT (qscope);
17754 if (access_declaration_p && cp_parser_error_occurred (parser))
17755 /* Something has already gone wrong; there's no need to parse
17756 further. Since an error has occurred, the return value of
17757 cp_parser_parse_definitely will be false, as required. */
17758 return cp_parser_parse_definitely (parser);
17760 token = cp_lexer_peek_token (parser->lexer);
17761 /* Parse the unqualified-id. */
17762 identifier = cp_parser_unqualified_id (parser,
17763 /*template_keyword_p=*/false,
17764 /*check_dependency_p=*/true,
17765 /*declarator_p=*/true,
17766 /*optional_p=*/false);
17768 if (access_declaration_p)
17770 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17771 cp_parser_simulate_error (parser);
17772 if (!cp_parser_parse_definitely (parser))
17773 return false;
17776 /* The function we call to handle a using-declaration is different
17777 depending on what scope we are in. */
17778 if (qscope == error_mark_node || identifier == error_mark_node)
17780 else if (!identifier_p (identifier)
17781 && TREE_CODE (identifier) != BIT_NOT_EXPR)
17782 /* [namespace.udecl]
17784 A using declaration shall not name a template-id. */
17785 error_at (token->location,
17786 "a template-id may not appear in a using-declaration");
17787 else
17789 if (at_class_scope_p ())
17791 /* Create the USING_DECL. */
17792 decl = do_class_using_decl (parser->scope, identifier);
17794 if (decl && typename_p)
17795 USING_DECL_TYPENAME_P (decl) = 1;
17797 if (check_for_bare_parameter_packs (decl))
17799 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17800 return false;
17802 else
17803 /* Add it to the list of members in this class. */
17804 finish_member_declaration (decl);
17806 else
17808 decl = cp_parser_lookup_name_simple (parser,
17809 identifier,
17810 token->location);
17811 if (decl == error_mark_node)
17812 cp_parser_name_lookup_error (parser, identifier,
17813 decl, NLE_NULL,
17814 token->location);
17815 else if (check_for_bare_parameter_packs (decl))
17817 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17818 return false;
17820 else if (!at_namespace_scope_p ())
17821 do_local_using_decl (decl, qscope, identifier);
17822 else
17823 do_toplevel_using_decl (decl, qscope, identifier);
17827 /* Look for the final `;'. */
17828 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17830 if (access_declaration_p && errorcount == oldcount)
17831 warning_at (diag_token->location, OPT_Wdeprecated,
17832 "access declarations are deprecated "
17833 "in favour of using-declarations; "
17834 "suggestion: add the %<using%> keyword");
17836 return true;
17839 /* Parse an alias-declaration.
17841 alias-declaration:
17842 using identifier attribute-specifier-seq [opt] = type-id */
17844 static tree
17845 cp_parser_alias_declaration (cp_parser* parser)
17847 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
17848 location_t id_location;
17849 cp_declarator *declarator;
17850 cp_decl_specifier_seq decl_specs;
17851 bool member_p;
17852 const char *saved_message = NULL;
17854 /* Look for the `using' keyword. */
17855 cp_token *using_token
17856 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
17857 if (using_token == NULL)
17858 return error_mark_node;
17860 id_location = cp_lexer_peek_token (parser->lexer)->location;
17861 id = cp_parser_identifier (parser);
17862 if (id == error_mark_node)
17863 return error_mark_node;
17865 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
17866 attributes = cp_parser_attributes_opt (parser);
17867 if (attributes == error_mark_node)
17868 return error_mark_node;
17870 cp_parser_require (parser, CPP_EQ, RT_EQ);
17872 if (cp_parser_error_occurred (parser))
17873 return error_mark_node;
17875 cp_parser_commit_to_tentative_parse (parser);
17877 /* Now we are going to parse the type-id of the declaration. */
17880 [dcl.type]/3 says:
17882 "A type-specifier-seq shall not define a class or enumeration
17883 unless it appears in the type-id of an alias-declaration (7.1.3) that
17884 is not the declaration of a template-declaration."
17886 In other words, if we currently are in an alias template, the
17887 type-id should not define a type.
17889 So let's set parser->type_definition_forbidden_message in that
17890 case; cp_parser_check_type_definition (called by
17891 cp_parser_class_specifier) will then emit an error if a type is
17892 defined in the type-id. */
17893 if (parser->num_template_parameter_lists)
17895 saved_message = parser->type_definition_forbidden_message;
17896 parser->type_definition_forbidden_message =
17897 G_("types may not be defined in alias template declarations");
17900 type = cp_parser_type_id (parser);
17902 /* Restore the error message if need be. */
17903 if (parser->num_template_parameter_lists)
17904 parser->type_definition_forbidden_message = saved_message;
17906 if (type == error_mark_node
17907 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
17909 cp_parser_skip_to_end_of_block_or_statement (parser);
17910 return error_mark_node;
17913 /* A typedef-name can also be introduced by an alias-declaration. The
17914 identifier following the using keyword becomes a typedef-name. It has
17915 the same semantics as if it were introduced by the typedef
17916 specifier. In particular, it does not define a new type and it shall
17917 not appear in the type-id. */
17919 clear_decl_specs (&decl_specs);
17920 decl_specs.type = type;
17921 if (attributes != NULL_TREE)
17923 decl_specs.attributes = attributes;
17924 set_and_check_decl_spec_loc (&decl_specs,
17925 ds_attribute,
17926 attrs_token);
17928 set_and_check_decl_spec_loc (&decl_specs,
17929 ds_typedef,
17930 using_token);
17931 set_and_check_decl_spec_loc (&decl_specs,
17932 ds_alias,
17933 using_token);
17935 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
17936 declarator->id_loc = id_location;
17938 member_p = at_class_scope_p ();
17939 if (member_p)
17940 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
17941 NULL_TREE, attributes);
17942 else
17943 decl = start_decl (declarator, &decl_specs, 0,
17944 attributes, NULL_TREE, &pushed_scope);
17945 if (decl == error_mark_node)
17946 return decl;
17948 // Attach constraints to the alias declaration.
17949 if (flag_concepts && current_template_parms)
17951 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
17952 tree constr = build_constraints (reqs, NULL_TREE);
17953 set_constraints (decl, constr);
17956 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
17958 if (pushed_scope)
17959 pop_scope (pushed_scope);
17961 /* If decl is a template, return its TEMPLATE_DECL so that it gets
17962 added into the symbol table; otherwise, return the TYPE_DECL. */
17963 if (DECL_LANG_SPECIFIC (decl)
17964 && DECL_TEMPLATE_INFO (decl)
17965 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
17967 decl = DECL_TI_TEMPLATE (decl);
17968 if (member_p)
17969 check_member_template (decl);
17972 return decl;
17975 /* Parse a using-directive.
17977 using-directive:
17978 using namespace :: [opt] nested-name-specifier [opt]
17979 namespace-name ; */
17981 static void
17982 cp_parser_using_directive (cp_parser* parser)
17984 tree namespace_decl;
17985 tree attribs;
17987 /* Look for the `using' keyword. */
17988 cp_parser_require_keyword (parser, RID_USING, RT_USING);
17989 /* And the `namespace' keyword. */
17990 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17991 /* Look for the optional `::' operator. */
17992 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17993 /* And the optional nested-name-specifier. */
17994 cp_parser_nested_name_specifier_opt (parser,
17995 /*typename_keyword_p=*/false,
17996 /*check_dependency_p=*/true,
17997 /*type_p=*/false,
17998 /*is_declaration=*/true);
17999 /* Get the namespace being used. */
18000 namespace_decl = cp_parser_namespace_name (parser);
18001 /* And any specified attributes. */
18002 attribs = cp_parser_attributes_opt (parser);
18003 /* Update the symbol table. */
18004 parse_using_directive (namespace_decl, attribs);
18005 /* Look for the final `;'. */
18006 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18009 /* Parse an asm-definition.
18011 asm-definition:
18012 asm ( string-literal ) ;
18014 GNU Extension:
18016 asm-definition:
18017 asm volatile [opt] ( string-literal ) ;
18018 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18019 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18020 : asm-operand-list [opt] ) ;
18021 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18022 : asm-operand-list [opt]
18023 : asm-clobber-list [opt] ) ;
18024 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18025 : asm-clobber-list [opt]
18026 : asm-goto-list ) ; */
18028 static void
18029 cp_parser_asm_definition (cp_parser* parser)
18031 tree string;
18032 tree outputs = NULL_TREE;
18033 tree inputs = NULL_TREE;
18034 tree clobbers = NULL_TREE;
18035 tree labels = NULL_TREE;
18036 tree asm_stmt;
18037 bool volatile_p = false;
18038 bool extended_p = false;
18039 bool invalid_inputs_p = false;
18040 bool invalid_outputs_p = false;
18041 bool goto_p = false;
18042 required_token missing = RT_NONE;
18044 /* Look for the `asm' keyword. */
18045 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
18047 if (parser->in_function_body
18048 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
18050 error ("%<asm%> in %<constexpr%> function");
18051 cp_function_chain->invalid_constexpr = true;
18054 /* See if the next token is `volatile'. */
18055 if (cp_parser_allow_gnu_extensions_p (parser)
18056 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
18058 /* Remember that we saw the `volatile' keyword. */
18059 volatile_p = true;
18060 /* Consume the token. */
18061 cp_lexer_consume_token (parser->lexer);
18063 if (cp_parser_allow_gnu_extensions_p (parser)
18064 && parser->in_function_body
18065 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
18067 /* Remember that we saw the `goto' keyword. */
18068 goto_p = true;
18069 /* Consume the token. */
18070 cp_lexer_consume_token (parser->lexer);
18072 /* Look for the opening `('. */
18073 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18074 return;
18075 /* Look for the string. */
18076 string = cp_parser_string_literal (parser, false, false);
18077 if (string == error_mark_node)
18079 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18080 /*consume_paren=*/true);
18081 return;
18084 /* If we're allowing GNU extensions, check for the extended assembly
18085 syntax. Unfortunately, the `:' tokens need not be separated by
18086 a space in C, and so, for compatibility, we tolerate that here
18087 too. Doing that means that we have to treat the `::' operator as
18088 two `:' tokens. */
18089 if (cp_parser_allow_gnu_extensions_p (parser)
18090 && parser->in_function_body
18091 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
18092 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
18094 bool inputs_p = false;
18095 bool clobbers_p = false;
18096 bool labels_p = false;
18098 /* The extended syntax was used. */
18099 extended_p = true;
18101 /* Look for outputs. */
18102 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18104 /* Consume the `:'. */
18105 cp_lexer_consume_token (parser->lexer);
18106 /* Parse the output-operands. */
18107 if (cp_lexer_next_token_is_not (parser->lexer,
18108 CPP_COLON)
18109 && cp_lexer_next_token_is_not (parser->lexer,
18110 CPP_SCOPE)
18111 && cp_lexer_next_token_is_not (parser->lexer,
18112 CPP_CLOSE_PAREN)
18113 && !goto_p)
18115 outputs = cp_parser_asm_operand_list (parser);
18116 if (outputs == error_mark_node)
18117 invalid_outputs_p = true;
18120 /* If the next token is `::', there are no outputs, and the
18121 next token is the beginning of the inputs. */
18122 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18123 /* The inputs are coming next. */
18124 inputs_p = true;
18126 /* Look for inputs. */
18127 if (inputs_p
18128 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18130 /* Consume the `:' or `::'. */
18131 cp_lexer_consume_token (parser->lexer);
18132 /* Parse the output-operands. */
18133 if (cp_lexer_next_token_is_not (parser->lexer,
18134 CPP_COLON)
18135 && cp_lexer_next_token_is_not (parser->lexer,
18136 CPP_SCOPE)
18137 && cp_lexer_next_token_is_not (parser->lexer,
18138 CPP_CLOSE_PAREN))
18140 inputs = cp_parser_asm_operand_list (parser);
18141 if (inputs == error_mark_node)
18142 invalid_inputs_p = true;
18145 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18146 /* The clobbers are coming next. */
18147 clobbers_p = true;
18149 /* Look for clobbers. */
18150 if (clobbers_p
18151 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18153 clobbers_p = true;
18154 /* Consume the `:' or `::'. */
18155 cp_lexer_consume_token (parser->lexer);
18156 /* Parse the clobbers. */
18157 if (cp_lexer_next_token_is_not (parser->lexer,
18158 CPP_COLON)
18159 && cp_lexer_next_token_is_not (parser->lexer,
18160 CPP_CLOSE_PAREN))
18161 clobbers = cp_parser_asm_clobber_list (parser);
18163 else if (goto_p
18164 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18165 /* The labels are coming next. */
18166 labels_p = true;
18168 /* Look for labels. */
18169 if (labels_p
18170 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
18172 labels_p = true;
18173 /* Consume the `:' or `::'. */
18174 cp_lexer_consume_token (parser->lexer);
18175 /* Parse the labels. */
18176 labels = cp_parser_asm_label_list (parser);
18179 if (goto_p && !labels_p)
18180 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
18182 else if (goto_p)
18183 missing = RT_COLON_SCOPE;
18185 /* Look for the closing `)'. */
18186 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
18187 missing ? missing : RT_CLOSE_PAREN))
18188 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18189 /*consume_paren=*/true);
18190 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18192 if (!invalid_inputs_p && !invalid_outputs_p)
18194 /* Create the ASM_EXPR. */
18195 if (parser->in_function_body)
18197 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
18198 inputs, clobbers, labels);
18199 /* If the extended syntax was not used, mark the ASM_EXPR. */
18200 if (!extended_p)
18202 tree temp = asm_stmt;
18203 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
18204 temp = TREE_OPERAND (temp, 0);
18206 ASM_INPUT_P (temp) = 1;
18209 else
18210 symtab->finalize_toplevel_asm (string);
18214 /* Declarators [gram.dcl.decl] */
18216 /* Parse an init-declarator.
18218 init-declarator:
18219 declarator initializer [opt]
18221 GNU Extension:
18223 init-declarator:
18224 declarator asm-specification [opt] attributes [opt] initializer [opt]
18226 function-definition:
18227 decl-specifier-seq [opt] declarator ctor-initializer [opt]
18228 function-body
18229 decl-specifier-seq [opt] declarator function-try-block
18231 GNU Extension:
18233 function-definition:
18234 __extension__ function-definition
18236 TM Extension:
18238 function-definition:
18239 decl-specifier-seq [opt] declarator function-transaction-block
18241 The DECL_SPECIFIERS apply to this declarator. Returns a
18242 representation of the entity declared. If MEMBER_P is TRUE, then
18243 this declarator appears in a class scope. The new DECL created by
18244 this declarator is returned.
18246 The CHECKS are access checks that should be performed once we know
18247 what entity is being declared (and, therefore, what classes have
18248 befriended it).
18250 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
18251 for a function-definition here as well. If the declarator is a
18252 declarator for a function-definition, *FUNCTION_DEFINITION_P will
18253 be TRUE upon return. By that point, the function-definition will
18254 have been completely parsed.
18256 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
18257 is FALSE.
18259 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
18260 parsed declaration if it is an uninitialized single declarator not followed
18261 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
18262 if present, will not be consumed. If returned, this declarator will be
18263 created with SD_INITIALIZED but will not call cp_finish_decl.
18265 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
18266 and there is an initializer, the pointed location_t is set to the
18267 location of the '=' or `(', or '{' in C++11 token introducing the
18268 initializer. */
18270 static tree
18271 cp_parser_init_declarator (cp_parser* parser,
18272 cp_decl_specifier_seq *decl_specifiers,
18273 vec<deferred_access_check, va_gc> *checks,
18274 bool function_definition_allowed_p,
18275 bool member_p,
18276 int declares_class_or_enum,
18277 bool* function_definition_p,
18278 tree* maybe_range_for_decl,
18279 location_t* init_loc)
18281 cp_token *token = NULL, *asm_spec_start_token = NULL,
18282 *attributes_start_token = NULL;
18283 cp_declarator *declarator;
18284 tree prefix_attributes;
18285 tree attributes = NULL;
18286 tree asm_specification;
18287 tree initializer;
18288 tree decl = NULL_TREE;
18289 tree scope;
18290 int is_initialized;
18291 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
18292 initialized with "= ..", CPP_OPEN_PAREN if initialized with
18293 "(...)". */
18294 enum cpp_ttype initialization_kind;
18295 bool is_direct_init = false;
18296 bool is_non_constant_init;
18297 int ctor_dtor_or_conv_p;
18298 bool friend_p = cp_parser_friend_p (decl_specifiers);
18299 tree pushed_scope = NULL_TREE;
18300 bool range_for_decl_p = false;
18301 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18302 location_t tmp_init_loc = UNKNOWN_LOCATION;
18304 /* Gather the attributes that were provided with the
18305 decl-specifiers. */
18306 prefix_attributes = decl_specifiers->attributes;
18308 /* Assume that this is not the declarator for a function
18309 definition. */
18310 if (function_definition_p)
18311 *function_definition_p = false;
18313 /* Default arguments are only permitted for function parameters. */
18314 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
18315 parser->default_arg_ok_p = false;
18317 /* Defer access checks while parsing the declarator; we cannot know
18318 what names are accessible until we know what is being
18319 declared. */
18320 resume_deferring_access_checks ();
18322 /* Parse the declarator. */
18323 token = cp_lexer_peek_token (parser->lexer);
18324 declarator
18325 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18326 &ctor_dtor_or_conv_p,
18327 /*parenthesized_p=*/NULL,
18328 member_p, friend_p);
18329 /* Gather up the deferred checks. */
18330 stop_deferring_access_checks ();
18332 parser->default_arg_ok_p = saved_default_arg_ok_p;
18334 /* If the DECLARATOR was erroneous, there's no need to go
18335 further. */
18336 if (declarator == cp_error_declarator)
18337 return error_mark_node;
18339 /* Check that the number of template-parameter-lists is OK. */
18340 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
18341 token->location))
18342 return error_mark_node;
18344 if (declares_class_or_enum & 2)
18345 cp_parser_check_for_definition_in_return_type (declarator,
18346 decl_specifiers->type,
18347 decl_specifiers->locations[ds_type_spec]);
18349 /* Figure out what scope the entity declared by the DECLARATOR is
18350 located in. `grokdeclarator' sometimes changes the scope, so
18351 we compute it now. */
18352 scope = get_scope_of_declarator (declarator);
18354 /* Perform any lookups in the declared type which were thought to be
18355 dependent, but are not in the scope of the declarator. */
18356 decl_specifiers->type
18357 = maybe_update_decl_type (decl_specifiers->type, scope);
18359 /* If we're allowing GNU extensions, look for an
18360 asm-specification. */
18361 if (cp_parser_allow_gnu_extensions_p (parser))
18363 /* Look for an asm-specification. */
18364 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
18365 asm_specification = cp_parser_asm_specification_opt (parser);
18367 else
18368 asm_specification = NULL_TREE;
18370 /* Look for attributes. */
18371 attributes_start_token = cp_lexer_peek_token (parser->lexer);
18372 attributes = cp_parser_attributes_opt (parser);
18374 /* Peek at the next token. */
18375 token = cp_lexer_peek_token (parser->lexer);
18377 bool bogus_implicit_tmpl = false;
18379 if (function_declarator_p (declarator))
18381 /* Check to see if the token indicates the start of a
18382 function-definition. */
18383 if (cp_parser_token_starts_function_definition_p (token))
18385 if (!function_definition_allowed_p)
18387 /* If a function-definition should not appear here, issue an
18388 error message. */
18389 cp_parser_error (parser,
18390 "a function-definition is not allowed here");
18391 return error_mark_node;
18394 location_t func_brace_location
18395 = cp_lexer_peek_token (parser->lexer)->location;
18397 /* Neither attributes nor an asm-specification are allowed
18398 on a function-definition. */
18399 if (asm_specification)
18400 error_at (asm_spec_start_token->location,
18401 "an asm-specification is not allowed "
18402 "on a function-definition");
18403 if (attributes)
18404 error_at (attributes_start_token->location,
18405 "attributes are not allowed "
18406 "on a function-definition");
18407 /* This is a function-definition. */
18408 *function_definition_p = true;
18410 /* Parse the function definition. */
18411 if (member_p)
18412 decl = cp_parser_save_member_function_body (parser,
18413 decl_specifiers,
18414 declarator,
18415 prefix_attributes);
18416 else
18417 decl =
18418 (cp_parser_function_definition_from_specifiers_and_declarator
18419 (parser, decl_specifiers, prefix_attributes, declarator));
18421 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
18423 /* This is where the prologue starts... */
18424 DECL_STRUCT_FUNCTION (decl)->function_start_locus
18425 = func_brace_location;
18428 return decl;
18431 else if (parser->fully_implicit_function_template_p)
18433 /* A non-template declaration involving a function parameter list
18434 containing an implicit template parameter will be made into a
18435 template. If the resulting declaration is not going to be an
18436 actual function then finish the template scope here to prevent it.
18437 An error message will be issued once we have a decl to talk about.
18439 FIXME probably we should do type deduction rather than create an
18440 implicit template, but the standard currently doesn't allow it. */
18441 bogus_implicit_tmpl = true;
18442 finish_fully_implicit_template (parser, NULL_TREE);
18445 /* [dcl.dcl]
18447 Only in function declarations for constructors, destructors, and
18448 type conversions can the decl-specifier-seq be omitted.
18450 We explicitly postpone this check past the point where we handle
18451 function-definitions because we tolerate function-definitions
18452 that are missing their return types in some modes. */
18453 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
18455 cp_parser_error (parser,
18456 "expected constructor, destructor, or type conversion");
18457 return error_mark_node;
18460 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
18461 if (token->type == CPP_EQ
18462 || token->type == CPP_OPEN_PAREN
18463 || token->type == CPP_OPEN_BRACE)
18465 is_initialized = SD_INITIALIZED;
18466 initialization_kind = token->type;
18467 if (maybe_range_for_decl)
18468 *maybe_range_for_decl = error_mark_node;
18469 tmp_init_loc = token->location;
18470 if (init_loc && *init_loc == UNKNOWN_LOCATION)
18471 *init_loc = tmp_init_loc;
18473 if (token->type == CPP_EQ
18474 && function_declarator_p (declarator))
18476 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
18477 if (t2->keyword == RID_DEFAULT)
18478 is_initialized = SD_DEFAULTED;
18479 else if (t2->keyword == RID_DELETE)
18480 is_initialized = SD_DELETED;
18483 else
18485 /* If the init-declarator isn't initialized and isn't followed by a
18486 `,' or `;', it's not a valid init-declarator. */
18487 if (token->type != CPP_COMMA
18488 && token->type != CPP_SEMICOLON)
18490 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
18491 range_for_decl_p = true;
18492 else
18494 if (!maybe_range_for_decl)
18495 cp_parser_error (parser, "expected initializer");
18496 return error_mark_node;
18499 is_initialized = SD_UNINITIALIZED;
18500 initialization_kind = CPP_EOF;
18503 /* Because start_decl has side-effects, we should only call it if we
18504 know we're going ahead. By this point, we know that we cannot
18505 possibly be looking at any other construct. */
18506 cp_parser_commit_to_tentative_parse (parser);
18508 /* Enter the newly declared entry in the symbol table. If we're
18509 processing a declaration in a class-specifier, we wait until
18510 after processing the initializer. */
18511 if (!member_p)
18513 if (parser->in_unbraced_linkage_specification_p)
18514 decl_specifiers->storage_class = sc_extern;
18515 decl = start_decl (declarator, decl_specifiers,
18516 range_for_decl_p? SD_INITIALIZED : is_initialized,
18517 attributes, prefix_attributes, &pushed_scope);
18518 cp_finalize_omp_declare_simd (parser, decl);
18519 cp_finalize_oacc_routine (parser, decl, false);
18520 /* Adjust location of decl if declarator->id_loc is more appropriate:
18521 set, and decl wasn't merged with another decl, in which case its
18522 location would be different from input_location, and more accurate. */
18523 if (DECL_P (decl)
18524 && declarator->id_loc != UNKNOWN_LOCATION
18525 && DECL_SOURCE_LOCATION (decl) == input_location)
18526 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
18528 else if (scope)
18529 /* Enter the SCOPE. That way unqualified names appearing in the
18530 initializer will be looked up in SCOPE. */
18531 pushed_scope = push_scope (scope);
18533 /* Perform deferred access control checks, now that we know in which
18534 SCOPE the declared entity resides. */
18535 if (!member_p && decl)
18537 tree saved_current_function_decl = NULL_TREE;
18539 /* If the entity being declared is a function, pretend that we
18540 are in its scope. If it is a `friend', it may have access to
18541 things that would not otherwise be accessible. */
18542 if (TREE_CODE (decl) == FUNCTION_DECL)
18544 saved_current_function_decl = current_function_decl;
18545 current_function_decl = decl;
18548 /* Perform access checks for template parameters. */
18549 cp_parser_perform_template_parameter_access_checks (checks);
18551 /* Perform the access control checks for the declarator and the
18552 decl-specifiers. */
18553 perform_deferred_access_checks (tf_warning_or_error);
18555 /* Restore the saved value. */
18556 if (TREE_CODE (decl) == FUNCTION_DECL)
18557 current_function_decl = saved_current_function_decl;
18560 /* Parse the initializer. */
18561 initializer = NULL_TREE;
18562 is_direct_init = false;
18563 is_non_constant_init = true;
18564 if (is_initialized)
18566 if (function_declarator_p (declarator))
18568 if (initialization_kind == CPP_EQ)
18569 initializer = cp_parser_pure_specifier (parser);
18570 else
18572 /* If the declaration was erroneous, we don't really
18573 know what the user intended, so just silently
18574 consume the initializer. */
18575 if (decl != error_mark_node)
18576 error_at (tmp_init_loc, "initializer provided for function");
18577 cp_parser_skip_to_closing_parenthesis (parser,
18578 /*recovering=*/true,
18579 /*or_comma=*/false,
18580 /*consume_paren=*/true);
18583 else
18585 /* We want to record the extra mangling scope for in-class
18586 initializers of class members and initializers of static data
18587 member templates. The former involves deferring
18588 parsing of the initializer until end of class as with default
18589 arguments. So right here we only handle the latter. */
18590 if (!member_p && processing_template_decl)
18591 start_lambda_scope (decl);
18592 initializer = cp_parser_initializer (parser,
18593 &is_direct_init,
18594 &is_non_constant_init);
18595 if (!member_p && processing_template_decl)
18596 finish_lambda_scope ();
18597 if (initializer == error_mark_node)
18598 cp_parser_skip_to_end_of_statement (parser);
18602 /* The old parser allows attributes to appear after a parenthesized
18603 initializer. Mark Mitchell proposed removing this functionality
18604 on the GCC mailing lists on 2002-08-13. This parser accepts the
18605 attributes -- but ignores them. */
18606 if (cp_parser_allow_gnu_extensions_p (parser)
18607 && initialization_kind == CPP_OPEN_PAREN)
18608 if (cp_parser_attributes_opt (parser))
18609 warning (OPT_Wattributes,
18610 "attributes after parenthesized initializer ignored");
18612 /* And now complain about a non-function implicit template. */
18613 if (bogus_implicit_tmpl && decl != error_mark_node)
18614 error_at (DECL_SOURCE_LOCATION (decl),
18615 "non-function %qD declared as implicit template", decl);
18617 /* For an in-class declaration, use `grokfield' to create the
18618 declaration. */
18619 if (member_p)
18621 if (pushed_scope)
18623 pop_scope (pushed_scope);
18624 pushed_scope = NULL_TREE;
18626 decl = grokfield (declarator, decl_specifiers,
18627 initializer, !is_non_constant_init,
18628 /*asmspec=*/NULL_TREE,
18629 chainon (attributes, prefix_attributes));
18630 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
18631 cp_parser_save_default_args (parser, decl);
18632 cp_finalize_omp_declare_simd (parser, decl);
18633 cp_finalize_oacc_routine (parser, decl, false);
18636 /* Finish processing the declaration. But, skip member
18637 declarations. */
18638 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
18640 cp_finish_decl (decl,
18641 initializer, !is_non_constant_init,
18642 asm_specification,
18643 /* If the initializer is in parentheses, then this is
18644 a direct-initialization, which means that an
18645 `explicit' constructor is OK. Otherwise, an
18646 `explicit' constructor cannot be used. */
18647 ((is_direct_init || !is_initialized)
18648 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
18650 else if ((cxx_dialect != cxx98) && friend_p
18651 && decl && TREE_CODE (decl) == FUNCTION_DECL)
18652 /* Core issue #226 (C++0x only): A default template-argument
18653 shall not be specified in a friend class template
18654 declaration. */
18655 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
18656 /*is_partial=*/false, /*is_friend_decl=*/1);
18658 if (!friend_p && pushed_scope)
18659 pop_scope (pushed_scope);
18661 if (function_declarator_p (declarator)
18662 && parser->fully_implicit_function_template_p)
18664 if (member_p)
18665 decl = finish_fully_implicit_template (parser, decl);
18666 else
18667 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
18670 return decl;
18673 /* Parse a declarator.
18675 declarator:
18676 direct-declarator
18677 ptr-operator declarator
18679 abstract-declarator:
18680 ptr-operator abstract-declarator [opt]
18681 direct-abstract-declarator
18683 GNU Extensions:
18685 declarator:
18686 attributes [opt] direct-declarator
18687 attributes [opt] ptr-operator declarator
18689 abstract-declarator:
18690 attributes [opt] ptr-operator abstract-declarator [opt]
18691 attributes [opt] direct-abstract-declarator
18693 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
18694 detect constructor, destructor or conversion operators. It is set
18695 to -1 if the declarator is a name, and +1 if it is a
18696 function. Otherwise it is set to zero. Usually you just want to
18697 test for >0, but internally the negative value is used.
18699 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
18700 a decl-specifier-seq unless it declares a constructor, destructor,
18701 or conversion. It might seem that we could check this condition in
18702 semantic analysis, rather than parsing, but that makes it difficult
18703 to handle something like `f()'. We want to notice that there are
18704 no decl-specifiers, and therefore realize that this is an
18705 expression, not a declaration.)
18707 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
18708 the declarator is a direct-declarator of the form "(...)".
18710 MEMBER_P is true iff this declarator is a member-declarator.
18712 FRIEND_P is true iff this declarator is a friend. */
18714 static cp_declarator *
18715 cp_parser_declarator (cp_parser* parser,
18716 cp_parser_declarator_kind dcl_kind,
18717 int* ctor_dtor_or_conv_p,
18718 bool* parenthesized_p,
18719 bool member_p, bool friend_p)
18721 cp_declarator *declarator;
18722 enum tree_code code;
18723 cp_cv_quals cv_quals;
18724 tree class_type;
18725 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
18727 /* Assume this is not a constructor, destructor, or type-conversion
18728 operator. */
18729 if (ctor_dtor_or_conv_p)
18730 *ctor_dtor_or_conv_p = 0;
18732 if (cp_parser_allow_gnu_extensions_p (parser))
18733 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
18735 /* Check for the ptr-operator production. */
18736 cp_parser_parse_tentatively (parser);
18737 /* Parse the ptr-operator. */
18738 code = cp_parser_ptr_operator (parser,
18739 &class_type,
18740 &cv_quals,
18741 &std_attributes);
18743 /* If that worked, then we have a ptr-operator. */
18744 if (cp_parser_parse_definitely (parser))
18746 /* If a ptr-operator was found, then this declarator was not
18747 parenthesized. */
18748 if (parenthesized_p)
18749 *parenthesized_p = true;
18750 /* The dependent declarator is optional if we are parsing an
18751 abstract-declarator. */
18752 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18753 cp_parser_parse_tentatively (parser);
18755 /* Parse the dependent declarator. */
18756 declarator = cp_parser_declarator (parser, dcl_kind,
18757 /*ctor_dtor_or_conv_p=*/NULL,
18758 /*parenthesized_p=*/NULL,
18759 /*member_p=*/false,
18760 friend_p);
18762 /* If we are parsing an abstract-declarator, we must handle the
18763 case where the dependent declarator is absent. */
18764 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
18765 && !cp_parser_parse_definitely (parser))
18766 declarator = NULL;
18768 declarator = cp_parser_make_indirect_declarator
18769 (code, class_type, cv_quals, declarator, std_attributes);
18771 /* Everything else is a direct-declarator. */
18772 else
18774 if (parenthesized_p)
18775 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
18776 CPP_OPEN_PAREN);
18777 declarator = cp_parser_direct_declarator (parser, dcl_kind,
18778 ctor_dtor_or_conv_p,
18779 member_p, friend_p);
18782 if (gnu_attributes && declarator && declarator != cp_error_declarator)
18783 declarator->attributes = gnu_attributes;
18784 return declarator;
18787 /* Parse a direct-declarator or direct-abstract-declarator.
18789 direct-declarator:
18790 declarator-id
18791 direct-declarator ( parameter-declaration-clause )
18792 cv-qualifier-seq [opt]
18793 ref-qualifier [opt]
18794 exception-specification [opt]
18795 direct-declarator [ constant-expression [opt] ]
18796 ( declarator )
18798 direct-abstract-declarator:
18799 direct-abstract-declarator [opt]
18800 ( parameter-declaration-clause )
18801 cv-qualifier-seq [opt]
18802 ref-qualifier [opt]
18803 exception-specification [opt]
18804 direct-abstract-declarator [opt] [ constant-expression [opt] ]
18805 ( abstract-declarator )
18807 Returns a representation of the declarator. DCL_KIND is
18808 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
18809 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
18810 we are parsing a direct-declarator. It is
18811 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
18812 of ambiguity we prefer an abstract declarator, as per
18813 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
18814 as for cp_parser_declarator. */
18816 static cp_declarator *
18817 cp_parser_direct_declarator (cp_parser* parser,
18818 cp_parser_declarator_kind dcl_kind,
18819 int* ctor_dtor_or_conv_p,
18820 bool member_p, bool friend_p)
18822 cp_token *token;
18823 cp_declarator *declarator = NULL;
18824 tree scope = NULL_TREE;
18825 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18826 bool saved_in_declarator_p = parser->in_declarator_p;
18827 bool first = true;
18828 tree pushed_scope = NULL_TREE;
18830 while (true)
18832 /* Peek at the next token. */
18833 token = cp_lexer_peek_token (parser->lexer);
18834 if (token->type == CPP_OPEN_PAREN)
18836 /* This is either a parameter-declaration-clause, or a
18837 parenthesized declarator. When we know we are parsing a
18838 named declarator, it must be a parenthesized declarator
18839 if FIRST is true. For instance, `(int)' is a
18840 parameter-declaration-clause, with an omitted
18841 direct-abstract-declarator. But `((*))', is a
18842 parenthesized abstract declarator. Finally, when T is a
18843 template parameter `(T)' is a
18844 parameter-declaration-clause, and not a parenthesized
18845 named declarator.
18847 We first try and parse a parameter-declaration-clause,
18848 and then try a nested declarator (if FIRST is true).
18850 It is not an error for it not to be a
18851 parameter-declaration-clause, even when FIRST is
18852 false. Consider,
18854 int i (int);
18855 int i (3);
18857 The first is the declaration of a function while the
18858 second is the definition of a variable, including its
18859 initializer.
18861 Having seen only the parenthesis, we cannot know which of
18862 these two alternatives should be selected. Even more
18863 complex are examples like:
18865 int i (int (a));
18866 int i (int (3));
18868 The former is a function-declaration; the latter is a
18869 variable initialization.
18871 Thus again, we try a parameter-declaration-clause, and if
18872 that fails, we back out and return. */
18874 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18876 tree params;
18877 bool is_declarator = false;
18879 /* In a member-declarator, the only valid interpretation
18880 of a parenthesis is the start of a
18881 parameter-declaration-clause. (It is invalid to
18882 initialize a static data member with a parenthesized
18883 initializer; only the "=" form of initialization is
18884 permitted.) */
18885 if (!member_p)
18886 cp_parser_parse_tentatively (parser);
18888 /* Consume the `('. */
18889 cp_lexer_consume_token (parser->lexer);
18890 if (first)
18892 /* If this is going to be an abstract declarator, we're
18893 in a declarator and we can't have default args. */
18894 parser->default_arg_ok_p = false;
18895 parser->in_declarator_p = true;
18898 begin_scope (sk_function_parms, NULL_TREE);
18900 /* Parse the parameter-declaration-clause. */
18901 params = cp_parser_parameter_declaration_clause (parser);
18903 /* Consume the `)'. */
18904 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18906 /* If all went well, parse the cv-qualifier-seq,
18907 ref-qualifier and the exception-specification. */
18908 if (member_p || cp_parser_parse_definitely (parser))
18910 cp_cv_quals cv_quals;
18911 cp_virt_specifiers virt_specifiers;
18912 cp_ref_qualifier ref_qual;
18913 tree exception_specification;
18914 tree late_return;
18915 tree attrs;
18916 bool memfn = (member_p || (pushed_scope
18917 && CLASS_TYPE_P (pushed_scope)));
18919 is_declarator = true;
18921 if (ctor_dtor_or_conv_p)
18922 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
18923 first = false;
18925 /* Parse the cv-qualifier-seq. */
18926 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18927 /* Parse the ref-qualifier. */
18928 ref_qual = cp_parser_ref_qualifier_opt (parser);
18929 /* Parse the tx-qualifier. */
18930 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
18931 /* And the exception-specification. */
18932 exception_specification
18933 = cp_parser_exception_specification_opt (parser);
18935 attrs = cp_parser_std_attribute_spec_seq (parser);
18937 /* In here, we handle cases where attribute is used after
18938 the function declaration. For example:
18939 void func (int x) __attribute__((vector(..))); */
18940 tree gnu_attrs = NULL_TREE;
18941 if (flag_cilkplus
18942 && cp_next_tokens_can_be_gnu_attribute_p (parser))
18944 cp_parser_parse_tentatively (parser);
18945 tree attr = cp_parser_gnu_attributes_opt (parser);
18946 if (cp_lexer_next_token_is_not (parser->lexer,
18947 CPP_SEMICOLON)
18948 && cp_lexer_next_token_is_not (parser->lexer,
18949 CPP_OPEN_BRACE))
18950 cp_parser_abort_tentative_parse (parser);
18951 else if (!cp_parser_parse_definitely (parser))
18953 else
18954 gnu_attrs = attr;
18956 tree requires_clause = NULL_TREE;
18957 late_return = (cp_parser_late_return_type_opt
18958 (parser, declarator, requires_clause,
18959 memfn ? cv_quals : -1));
18961 /* Parse the virt-specifier-seq. */
18962 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18964 /* Create the function-declarator. */
18965 declarator = make_call_declarator (declarator,
18966 params,
18967 cv_quals,
18968 virt_specifiers,
18969 ref_qual,
18970 tx_qual,
18971 exception_specification,
18972 late_return,
18973 requires_clause);
18974 declarator->std_attributes = attrs;
18975 declarator->attributes = gnu_attrs;
18976 /* Any subsequent parameter lists are to do with
18977 return type, so are not those of the declared
18978 function. */
18979 parser->default_arg_ok_p = false;
18982 /* Remove the function parms from scope. */
18983 pop_bindings_and_leave_scope ();
18985 if (is_declarator)
18986 /* Repeat the main loop. */
18987 continue;
18990 /* If this is the first, we can try a parenthesized
18991 declarator. */
18992 if (first)
18994 bool saved_in_type_id_in_expr_p;
18996 parser->default_arg_ok_p = saved_default_arg_ok_p;
18997 parser->in_declarator_p = saved_in_declarator_p;
18999 /* Consume the `('. */
19000 cp_lexer_consume_token (parser->lexer);
19001 /* Parse the nested declarator. */
19002 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19003 parser->in_type_id_in_expr_p = true;
19004 declarator
19005 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
19006 /*parenthesized_p=*/NULL,
19007 member_p, friend_p);
19008 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19009 first = false;
19010 /* Expect a `)'. */
19011 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
19012 declarator = cp_error_declarator;
19013 if (declarator == cp_error_declarator)
19014 break;
19016 goto handle_declarator;
19018 /* Otherwise, we must be done. */
19019 else
19020 break;
19022 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19023 && token->type == CPP_OPEN_SQUARE
19024 && !cp_next_tokens_can_be_attribute_p (parser))
19026 /* Parse an array-declarator. */
19027 tree bounds, attrs;
19029 if (ctor_dtor_or_conv_p)
19030 *ctor_dtor_or_conv_p = 0;
19032 first = false;
19033 parser->default_arg_ok_p = false;
19034 parser->in_declarator_p = true;
19035 /* Consume the `['. */
19036 cp_lexer_consume_token (parser->lexer);
19037 /* Peek at the next token. */
19038 token = cp_lexer_peek_token (parser->lexer);
19039 /* If the next token is `]', then there is no
19040 constant-expression. */
19041 if (token->type != CPP_CLOSE_SQUARE)
19043 bool non_constant_p;
19044 bounds
19045 = cp_parser_constant_expression (parser,
19046 /*allow_non_constant=*/true,
19047 &non_constant_p);
19048 if (!non_constant_p)
19049 /* OK */;
19050 else if (error_operand_p (bounds))
19051 /* Already gave an error. */;
19052 else if (!parser->in_function_body
19053 || current_binding_level->kind == sk_function_parms)
19055 /* Normally, the array bound must be an integral constant
19056 expression. However, as an extension, we allow VLAs
19057 in function scopes as long as they aren't part of a
19058 parameter declaration. */
19059 cp_parser_error (parser,
19060 "array bound is not an integer constant");
19061 bounds = error_mark_node;
19063 else if (processing_template_decl
19064 && !type_dependent_expression_p (bounds))
19066 /* Remember this wasn't a constant-expression. */
19067 bounds = build_nop (TREE_TYPE (bounds), bounds);
19068 TREE_SIDE_EFFECTS (bounds) = 1;
19071 else
19072 bounds = NULL_TREE;
19073 /* Look for the closing `]'. */
19074 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
19076 declarator = cp_error_declarator;
19077 break;
19080 attrs = cp_parser_std_attribute_spec_seq (parser);
19081 declarator = make_array_declarator (declarator, bounds);
19082 declarator->std_attributes = attrs;
19084 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
19087 tree qualifying_scope;
19088 tree unqualified_name;
19089 tree attrs;
19090 special_function_kind sfk;
19091 bool abstract_ok;
19092 bool pack_expansion_p = false;
19093 cp_token *declarator_id_start_token;
19095 /* Parse a declarator-id */
19096 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
19097 if (abstract_ok)
19099 cp_parser_parse_tentatively (parser);
19101 /* If we see an ellipsis, we should be looking at a
19102 parameter pack. */
19103 if (token->type == CPP_ELLIPSIS)
19105 /* Consume the `...' */
19106 cp_lexer_consume_token (parser->lexer);
19108 pack_expansion_p = true;
19112 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
19113 unqualified_name
19114 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
19115 qualifying_scope = parser->scope;
19116 if (abstract_ok)
19118 bool okay = false;
19120 if (!unqualified_name && pack_expansion_p)
19122 /* Check whether an error occurred. */
19123 okay = !cp_parser_error_occurred (parser);
19125 /* We already consumed the ellipsis to mark a
19126 parameter pack, but we have no way to report it,
19127 so abort the tentative parse. We will be exiting
19128 immediately anyway. */
19129 cp_parser_abort_tentative_parse (parser);
19131 else
19132 okay = cp_parser_parse_definitely (parser);
19134 if (!okay)
19135 unqualified_name = error_mark_node;
19136 else if (unqualified_name
19137 && (qualifying_scope
19138 || (!identifier_p (unqualified_name))))
19140 cp_parser_error (parser, "expected unqualified-id");
19141 unqualified_name = error_mark_node;
19145 if (!unqualified_name)
19146 return NULL;
19147 if (unqualified_name == error_mark_node)
19149 declarator = cp_error_declarator;
19150 pack_expansion_p = false;
19151 declarator->parameter_pack_p = false;
19152 break;
19155 attrs = cp_parser_std_attribute_spec_seq (parser);
19157 if (qualifying_scope && at_namespace_scope_p ()
19158 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
19160 /* In the declaration of a member of a template class
19161 outside of the class itself, the SCOPE will sometimes
19162 be a TYPENAME_TYPE. For example, given:
19164 template <typename T>
19165 int S<T>::R::i = 3;
19167 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
19168 this context, we must resolve S<T>::R to an ordinary
19169 type, rather than a typename type.
19171 The reason we normally avoid resolving TYPENAME_TYPEs
19172 is that a specialization of `S' might render
19173 `S<T>::R' not a type. However, if `S' is
19174 specialized, then this `i' will not be used, so there
19175 is no harm in resolving the types here. */
19176 tree type;
19178 /* Resolve the TYPENAME_TYPE. */
19179 type = resolve_typename_type (qualifying_scope,
19180 /*only_current_p=*/false);
19181 /* If that failed, the declarator is invalid. */
19182 if (TREE_CODE (type) == TYPENAME_TYPE)
19184 if (typedef_variant_p (type))
19185 error_at (declarator_id_start_token->location,
19186 "cannot define member of dependent typedef "
19187 "%qT", type);
19188 else
19189 error_at (declarator_id_start_token->location,
19190 "%<%T::%E%> is not a type",
19191 TYPE_CONTEXT (qualifying_scope),
19192 TYPE_IDENTIFIER (qualifying_scope));
19194 qualifying_scope = type;
19197 sfk = sfk_none;
19199 if (unqualified_name)
19201 tree class_type;
19203 if (qualifying_scope
19204 && CLASS_TYPE_P (qualifying_scope))
19205 class_type = qualifying_scope;
19206 else
19207 class_type = current_class_type;
19209 if (TREE_CODE (unqualified_name) == TYPE_DECL)
19211 tree name_type = TREE_TYPE (unqualified_name);
19212 if (class_type && same_type_p (name_type, class_type))
19214 if (qualifying_scope
19215 && CLASSTYPE_USE_TEMPLATE (name_type))
19217 error_at (declarator_id_start_token->location,
19218 "invalid use of constructor as a template");
19219 inform (declarator_id_start_token->location,
19220 "use %<%T::%D%> instead of %<%T::%D%> to "
19221 "name the constructor in a qualified name",
19222 class_type,
19223 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
19224 class_type, name_type);
19225 declarator = cp_error_declarator;
19226 break;
19228 else
19229 unqualified_name = constructor_name (class_type);
19231 else
19233 /* We do not attempt to print the declarator
19234 here because we do not have enough
19235 information about its original syntactic
19236 form. */
19237 cp_parser_error (parser, "invalid declarator");
19238 declarator = cp_error_declarator;
19239 break;
19243 if (class_type)
19245 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
19246 sfk = sfk_destructor;
19247 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
19248 sfk = sfk_conversion;
19249 else if (/* There's no way to declare a constructor
19250 for an anonymous type, even if the type
19251 got a name for linkage purposes. */
19252 !TYPE_WAS_ANONYMOUS (class_type)
19253 /* Handle correctly (c++/19200):
19255 struct S {
19256 struct T{};
19257 friend void S(T);
19260 and also:
19262 namespace N {
19263 void S();
19266 struct S {
19267 friend void N::S();
19268 }; */
19269 && !(friend_p
19270 && class_type != qualifying_scope)
19271 && constructor_name_p (unqualified_name,
19272 class_type))
19274 unqualified_name = constructor_name (class_type);
19275 sfk = sfk_constructor;
19277 else if (is_overloaded_fn (unqualified_name)
19278 && DECL_CONSTRUCTOR_P (get_first_fn
19279 (unqualified_name)))
19280 sfk = sfk_constructor;
19282 if (ctor_dtor_or_conv_p && sfk != sfk_none)
19283 *ctor_dtor_or_conv_p = -1;
19286 declarator = make_id_declarator (qualifying_scope,
19287 unqualified_name,
19288 sfk);
19289 declarator->std_attributes = attrs;
19290 declarator->id_loc = token->location;
19291 declarator->parameter_pack_p = pack_expansion_p;
19293 if (pack_expansion_p)
19294 maybe_warn_variadic_templates ();
19297 handle_declarator:;
19298 scope = get_scope_of_declarator (declarator);
19299 if (scope)
19301 /* Any names that appear after the declarator-id for a
19302 member are looked up in the containing scope. */
19303 if (at_function_scope_p ())
19305 /* But declarations with qualified-ids can't appear in a
19306 function. */
19307 cp_parser_error (parser, "qualified-id in declaration");
19308 declarator = cp_error_declarator;
19309 break;
19311 pushed_scope = push_scope (scope);
19313 parser->in_declarator_p = true;
19314 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
19315 || (declarator && declarator->kind == cdk_id))
19316 /* Default args are only allowed on function
19317 declarations. */
19318 parser->default_arg_ok_p = saved_default_arg_ok_p;
19319 else
19320 parser->default_arg_ok_p = false;
19322 first = false;
19324 /* We're done. */
19325 else
19326 break;
19329 /* For an abstract declarator, we might wind up with nothing at this
19330 point. That's an error; the declarator is not optional. */
19331 if (!declarator)
19332 cp_parser_error (parser, "expected declarator");
19334 /* If we entered a scope, we must exit it now. */
19335 if (pushed_scope)
19336 pop_scope (pushed_scope);
19338 parser->default_arg_ok_p = saved_default_arg_ok_p;
19339 parser->in_declarator_p = saved_in_declarator_p;
19341 return declarator;
19344 /* Parse a ptr-operator.
19346 ptr-operator:
19347 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
19348 * cv-qualifier-seq [opt]
19350 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
19351 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
19353 GNU Extension:
19355 ptr-operator:
19356 & cv-qualifier-seq [opt]
19358 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
19359 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
19360 an rvalue reference. In the case of a pointer-to-member, *TYPE is
19361 filled in with the TYPE containing the member. *CV_QUALS is
19362 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
19363 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
19364 Note that the tree codes returned by this function have nothing
19365 to do with the types of trees that will be eventually be created
19366 to represent the pointer or reference type being parsed. They are
19367 just constants with suggestive names. */
19368 static enum tree_code
19369 cp_parser_ptr_operator (cp_parser* parser,
19370 tree* type,
19371 cp_cv_quals *cv_quals,
19372 tree *attributes)
19374 enum tree_code code = ERROR_MARK;
19375 cp_token *token;
19376 tree attrs = NULL_TREE;
19378 /* Assume that it's not a pointer-to-member. */
19379 *type = NULL_TREE;
19380 /* And that there are no cv-qualifiers. */
19381 *cv_quals = TYPE_UNQUALIFIED;
19383 /* Peek at the next token. */
19384 token = cp_lexer_peek_token (parser->lexer);
19386 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
19387 if (token->type == CPP_MULT)
19388 code = INDIRECT_REF;
19389 else if (token->type == CPP_AND)
19390 code = ADDR_EXPR;
19391 else if ((cxx_dialect != cxx98) &&
19392 token->type == CPP_AND_AND) /* C++0x only */
19393 code = NON_LVALUE_EXPR;
19395 if (code != ERROR_MARK)
19397 /* Consume the `*', `&' or `&&'. */
19398 cp_lexer_consume_token (parser->lexer);
19400 /* A `*' can be followed by a cv-qualifier-seq, and so can a
19401 `&', if we are allowing GNU extensions. (The only qualifier
19402 that can legally appear after `&' is `restrict', but that is
19403 enforced during semantic analysis. */
19404 if (code == INDIRECT_REF
19405 || cp_parser_allow_gnu_extensions_p (parser))
19406 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19408 attrs = cp_parser_std_attribute_spec_seq (parser);
19409 if (attributes != NULL)
19410 *attributes = attrs;
19412 else
19414 /* Try the pointer-to-member case. */
19415 cp_parser_parse_tentatively (parser);
19416 /* Look for the optional `::' operator. */
19417 cp_parser_global_scope_opt (parser,
19418 /*current_scope_valid_p=*/false);
19419 /* Look for the nested-name specifier. */
19420 token = cp_lexer_peek_token (parser->lexer);
19421 cp_parser_nested_name_specifier (parser,
19422 /*typename_keyword_p=*/false,
19423 /*check_dependency_p=*/true,
19424 /*type_p=*/false,
19425 /*is_declaration=*/false);
19426 /* If we found it, and the next token is a `*', then we are
19427 indeed looking at a pointer-to-member operator. */
19428 if (!cp_parser_error_occurred (parser)
19429 && cp_parser_require (parser, CPP_MULT, RT_MULT))
19431 /* Indicate that the `*' operator was used. */
19432 code = INDIRECT_REF;
19434 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
19435 error_at (token->location, "%qD is a namespace", parser->scope);
19436 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
19437 error_at (token->location, "cannot form pointer to member of "
19438 "non-class %q#T", parser->scope);
19439 else
19441 /* The type of which the member is a member is given by the
19442 current SCOPE. */
19443 *type = parser->scope;
19444 /* The next name will not be qualified. */
19445 parser->scope = NULL_TREE;
19446 parser->qualifying_scope = NULL_TREE;
19447 parser->object_scope = NULL_TREE;
19448 /* Look for optional c++11 attributes. */
19449 attrs = cp_parser_std_attribute_spec_seq (parser);
19450 if (attributes != NULL)
19451 *attributes = attrs;
19452 /* Look for the optional cv-qualifier-seq. */
19453 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19456 /* If that didn't work we don't have a ptr-operator. */
19457 if (!cp_parser_parse_definitely (parser))
19458 cp_parser_error (parser, "expected ptr-operator");
19461 return code;
19464 /* Parse an (optional) cv-qualifier-seq.
19466 cv-qualifier-seq:
19467 cv-qualifier cv-qualifier-seq [opt]
19469 cv-qualifier:
19470 const
19471 volatile
19473 GNU Extension:
19475 cv-qualifier:
19476 __restrict__
19478 Returns a bitmask representing the cv-qualifiers. */
19480 static cp_cv_quals
19481 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
19483 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
19485 while (true)
19487 cp_token *token;
19488 cp_cv_quals cv_qualifier;
19490 /* Peek at the next token. */
19491 token = cp_lexer_peek_token (parser->lexer);
19492 /* See if it's a cv-qualifier. */
19493 switch (token->keyword)
19495 case RID_CONST:
19496 cv_qualifier = TYPE_QUAL_CONST;
19497 break;
19499 case RID_VOLATILE:
19500 cv_qualifier = TYPE_QUAL_VOLATILE;
19501 break;
19503 case RID_RESTRICT:
19504 cv_qualifier = TYPE_QUAL_RESTRICT;
19505 break;
19507 default:
19508 cv_qualifier = TYPE_UNQUALIFIED;
19509 break;
19512 if (!cv_qualifier)
19513 break;
19515 if (cv_quals & cv_qualifier)
19517 error_at (token->location, "duplicate cv-qualifier");
19518 cp_lexer_purge_token (parser->lexer);
19520 else
19522 cp_lexer_consume_token (parser->lexer);
19523 cv_quals |= cv_qualifier;
19527 return cv_quals;
19530 /* Parse an (optional) ref-qualifier
19532 ref-qualifier:
19536 Returns cp_ref_qualifier representing ref-qualifier. */
19538 static cp_ref_qualifier
19539 cp_parser_ref_qualifier_opt (cp_parser* parser)
19541 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
19543 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
19544 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
19545 return ref_qual;
19547 while (true)
19549 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
19550 cp_token *token = cp_lexer_peek_token (parser->lexer);
19552 switch (token->type)
19554 case CPP_AND:
19555 curr_ref_qual = REF_QUAL_LVALUE;
19556 break;
19558 case CPP_AND_AND:
19559 curr_ref_qual = REF_QUAL_RVALUE;
19560 break;
19562 default:
19563 curr_ref_qual = REF_QUAL_NONE;
19564 break;
19567 if (!curr_ref_qual)
19568 break;
19569 else if (ref_qual)
19571 error_at (token->location, "multiple ref-qualifiers");
19572 cp_lexer_purge_token (parser->lexer);
19574 else
19576 ref_qual = curr_ref_qual;
19577 cp_lexer_consume_token (parser->lexer);
19581 return ref_qual;
19584 /* Parse an optional tx-qualifier.
19586 tx-qualifier:
19587 transaction_safe
19588 transaction_safe_dynamic */
19590 static tree
19591 cp_parser_tx_qualifier_opt (cp_parser *parser)
19593 cp_token *token = cp_lexer_peek_token (parser->lexer);
19594 if (token->type == CPP_NAME)
19596 tree name = token->u.value;
19597 const char *p = IDENTIFIER_POINTER (name);
19598 const int len = strlen ("transaction_safe");
19599 if (!strncmp (p, "transaction_safe", len))
19601 p += len;
19602 if (*p == '\0'
19603 || !strcmp (p, "_dynamic"))
19605 cp_lexer_consume_token (parser->lexer);
19606 if (!flag_tm)
19608 error ("%E requires %<-fgnu-tm%>", name);
19609 return NULL_TREE;
19611 else
19612 return name;
19616 return NULL_TREE;
19619 /* Parse an (optional) virt-specifier-seq.
19621 virt-specifier-seq:
19622 virt-specifier virt-specifier-seq [opt]
19624 virt-specifier:
19625 override
19626 final
19628 Returns a bitmask representing the virt-specifiers. */
19630 static cp_virt_specifiers
19631 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
19633 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19635 while (true)
19637 cp_token *token;
19638 cp_virt_specifiers virt_specifier;
19640 /* Peek at the next token. */
19641 token = cp_lexer_peek_token (parser->lexer);
19642 /* See if it's a virt-specifier-qualifier. */
19643 if (token->type != CPP_NAME)
19644 break;
19645 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
19647 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19648 virt_specifier = VIRT_SPEC_OVERRIDE;
19650 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
19652 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19653 virt_specifier = VIRT_SPEC_FINAL;
19655 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
19657 virt_specifier = VIRT_SPEC_FINAL;
19659 else
19660 break;
19662 if (virt_specifiers & virt_specifier)
19664 error_at (token->location, "duplicate virt-specifier");
19665 cp_lexer_purge_token (parser->lexer);
19667 else
19669 cp_lexer_consume_token (parser->lexer);
19670 virt_specifiers |= virt_specifier;
19673 return virt_specifiers;
19676 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
19677 is in scope even though it isn't real. */
19679 void
19680 inject_this_parameter (tree ctype, cp_cv_quals quals)
19682 tree this_parm;
19684 if (current_class_ptr)
19686 /* We don't clear this between NSDMIs. Is it already what we want? */
19687 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
19688 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
19689 && cp_type_quals (type) == quals)
19690 return;
19693 this_parm = build_this_parm (ctype, quals);
19694 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
19695 current_class_ptr = NULL_TREE;
19696 current_class_ref
19697 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
19698 current_class_ptr = this_parm;
19701 /* Return true iff our current scope is a non-static data member
19702 initializer. */
19704 bool
19705 parsing_nsdmi (void)
19707 /* We recognize NSDMI context by the context-less 'this' pointer set up
19708 by the function above. */
19709 if (current_class_ptr
19710 && TREE_CODE (current_class_ptr) == PARM_DECL
19711 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
19712 return true;
19713 return false;
19716 /* Parse a late-specified return type, if any. This is not a separate
19717 non-terminal, but part of a function declarator, which looks like
19719 -> trailing-type-specifier-seq abstract-declarator(opt)
19721 Returns the type indicated by the type-id.
19723 In addition to this, parse any queued up omp declare simd
19724 clauses and Cilk Plus SIMD-enabled function's vector attributes.
19726 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
19727 function. */
19729 static tree
19730 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
19731 tree& requires_clause, cp_cv_quals quals)
19733 cp_token *token;
19734 tree type = NULL_TREE;
19735 bool declare_simd_p = (parser->omp_declare_simd
19736 && declarator
19737 && declarator->kind == cdk_id);
19739 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
19740 && declarator && declarator->kind == cdk_id);
19742 bool oacc_routine_p = (parser->oacc_routine
19743 && declarator
19744 && declarator->kind == cdk_id);
19746 /* Peek at the next token. */
19747 token = cp_lexer_peek_token (parser->lexer);
19748 /* A late-specified return type is indicated by an initial '->'. */
19749 if (token->type != CPP_DEREF
19750 && token->keyword != RID_REQUIRES
19751 && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
19752 return NULL_TREE;
19754 tree save_ccp = current_class_ptr;
19755 tree save_ccr = current_class_ref;
19756 if (quals >= 0)
19758 /* DR 1207: 'this' is in scope in the trailing return type. */
19759 inject_this_parameter (current_class_type, quals);
19762 if (token->type == CPP_DEREF)
19764 /* Consume the ->. */
19765 cp_lexer_consume_token (parser->lexer);
19767 type = cp_parser_trailing_type_id (parser);
19770 /* Function declarations may be followed by a trailing
19771 requires-clause. */
19772 requires_clause = cp_parser_requires_clause_opt (parser);
19774 if (cilk_simd_fn_vector_p)
19775 declarator->attributes
19776 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
19777 declarator->attributes);
19778 if (declare_simd_p)
19779 declarator->attributes
19780 = cp_parser_late_parsing_omp_declare_simd (parser,
19781 declarator->attributes);
19782 if (oacc_routine_p)
19783 declarator->attributes
19784 = cp_parser_late_parsing_oacc_routine (parser,
19785 declarator->attributes);
19787 if (quals >= 0)
19789 current_class_ptr = save_ccp;
19790 current_class_ref = save_ccr;
19793 return type;
19796 /* Parse a declarator-id.
19798 declarator-id:
19799 id-expression
19800 :: [opt] nested-name-specifier [opt] type-name
19802 In the `id-expression' case, the value returned is as for
19803 cp_parser_id_expression if the id-expression was an unqualified-id.
19804 If the id-expression was a qualified-id, then a SCOPE_REF is
19805 returned. The first operand is the scope (either a NAMESPACE_DECL
19806 or TREE_TYPE), but the second is still just a representation of an
19807 unqualified-id. */
19809 static tree
19810 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
19812 tree id;
19813 /* The expression must be an id-expression. Assume that qualified
19814 names are the names of types so that:
19816 template <class T>
19817 int S<T>::R::i = 3;
19819 will work; we must treat `S<T>::R' as the name of a type.
19820 Similarly, assume that qualified names are templates, where
19821 required, so that:
19823 template <class T>
19824 int S<T>::R<T>::i = 3;
19826 will work, too. */
19827 id = cp_parser_id_expression (parser,
19828 /*template_keyword_p=*/false,
19829 /*check_dependency_p=*/false,
19830 /*template_p=*/NULL,
19831 /*declarator_p=*/true,
19832 optional_p);
19833 if (id && BASELINK_P (id))
19834 id = BASELINK_FUNCTIONS (id);
19835 return id;
19838 /* Parse a type-id.
19840 type-id:
19841 type-specifier-seq abstract-declarator [opt]
19843 Returns the TYPE specified. */
19845 static tree
19846 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
19847 bool is_trailing_return)
19849 cp_decl_specifier_seq type_specifier_seq;
19850 cp_declarator *abstract_declarator;
19852 /* Parse the type-specifier-seq. */
19853 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
19854 is_trailing_return,
19855 &type_specifier_seq);
19856 if (type_specifier_seq.type == error_mark_node)
19857 return error_mark_node;
19859 /* There might or might not be an abstract declarator. */
19860 cp_parser_parse_tentatively (parser);
19861 /* Look for the declarator. */
19862 abstract_declarator
19863 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
19864 /*parenthesized_p=*/NULL,
19865 /*member_p=*/false,
19866 /*friend_p=*/false);
19867 /* Check to see if there really was a declarator. */
19868 if (!cp_parser_parse_definitely (parser))
19869 abstract_declarator = NULL;
19871 if (type_specifier_seq.type
19872 /* The concepts TS allows 'auto' as a type-id. */
19873 && (!flag_concepts || parser->in_type_id_in_expr_p)
19874 /* None of the valid uses of 'auto' in C++14 involve the type-id
19875 nonterminal, but it is valid in a trailing-return-type. */
19876 && !(cxx_dialect >= cxx14 && is_trailing_return)
19877 && type_uses_auto (type_specifier_seq.type))
19879 /* A type-id with type 'auto' is only ok if the abstract declarator
19880 is a function declarator with a late-specified return type.
19882 A type-id with 'auto' is also valid in a trailing-return-type
19883 in a compound-requirement. */
19884 if (abstract_declarator
19885 && abstract_declarator->kind == cdk_function
19886 && abstract_declarator->u.function.late_return_type)
19887 /* OK */;
19888 else if (parser->in_result_type_constraint_p)
19889 /* OK */;
19890 else
19892 error ("invalid use of %<auto%>");
19893 return error_mark_node;
19897 return groktypename (&type_specifier_seq, abstract_declarator,
19898 is_template_arg);
19901 static tree
19902 cp_parser_type_id (cp_parser *parser)
19904 return cp_parser_type_id_1 (parser, false, false);
19907 static tree
19908 cp_parser_template_type_arg (cp_parser *parser)
19910 tree r;
19911 const char *saved_message = parser->type_definition_forbidden_message;
19912 parser->type_definition_forbidden_message
19913 = G_("types may not be defined in template arguments");
19914 r = cp_parser_type_id_1 (parser, true, false);
19915 parser->type_definition_forbidden_message = saved_message;
19916 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
19918 error ("invalid use of %<auto%> in template argument");
19919 r = error_mark_node;
19921 return r;
19924 static tree
19925 cp_parser_trailing_type_id (cp_parser *parser)
19927 return cp_parser_type_id_1 (parser, false, true);
19930 /* Parse a type-specifier-seq.
19932 type-specifier-seq:
19933 type-specifier type-specifier-seq [opt]
19935 GNU extension:
19937 type-specifier-seq:
19938 attributes type-specifier-seq [opt]
19940 If IS_DECLARATION is true, we are at the start of a "condition" or
19941 exception-declaration, so we might be followed by a declarator-id.
19943 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
19944 i.e. we've just seen "->".
19946 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
19948 static void
19949 cp_parser_type_specifier_seq (cp_parser* parser,
19950 bool is_declaration,
19951 bool is_trailing_return,
19952 cp_decl_specifier_seq *type_specifier_seq)
19954 bool seen_type_specifier = false;
19955 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
19956 cp_token *start_token = NULL;
19958 /* Clear the TYPE_SPECIFIER_SEQ. */
19959 clear_decl_specs (type_specifier_seq);
19961 /* In the context of a trailing return type, enum E { } is an
19962 elaborated-type-specifier followed by a function-body, not an
19963 enum-specifier. */
19964 if (is_trailing_return)
19965 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
19967 /* Parse the type-specifiers and attributes. */
19968 while (true)
19970 tree type_specifier;
19971 bool is_cv_qualifier;
19973 /* Check for attributes first. */
19974 if (cp_next_tokens_can_be_attribute_p (parser))
19976 type_specifier_seq->attributes =
19977 chainon (type_specifier_seq->attributes,
19978 cp_parser_attributes_opt (parser));
19979 continue;
19982 /* record the token of the beginning of the type specifier seq,
19983 for error reporting purposes*/
19984 if (!start_token)
19985 start_token = cp_lexer_peek_token (parser->lexer);
19987 /* Look for the type-specifier. */
19988 type_specifier = cp_parser_type_specifier (parser,
19989 flags,
19990 type_specifier_seq,
19991 /*is_declaration=*/false,
19992 NULL,
19993 &is_cv_qualifier);
19994 if (!type_specifier)
19996 /* If the first type-specifier could not be found, this is not a
19997 type-specifier-seq at all. */
19998 if (!seen_type_specifier)
20000 /* Set in_declarator_p to avoid skipping to the semicolon. */
20001 int in_decl = parser->in_declarator_p;
20002 parser->in_declarator_p = true;
20004 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
20005 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
20006 cp_parser_error (parser, "expected type-specifier");
20008 parser->in_declarator_p = in_decl;
20010 type_specifier_seq->type = error_mark_node;
20011 return;
20013 /* If subsequent type-specifiers could not be found, the
20014 type-specifier-seq is complete. */
20015 break;
20018 seen_type_specifier = true;
20019 /* The standard says that a condition can be:
20021 type-specifier-seq declarator = assignment-expression
20023 However, given:
20025 struct S {};
20026 if (int S = ...)
20028 we should treat the "S" as a declarator, not as a
20029 type-specifier. The standard doesn't say that explicitly for
20030 type-specifier-seq, but it does say that for
20031 decl-specifier-seq in an ordinary declaration. Perhaps it
20032 would be clearer just to allow a decl-specifier-seq here, and
20033 then add a semantic restriction that if any decl-specifiers
20034 that are not type-specifiers appear, the program is invalid. */
20035 if (is_declaration && !is_cv_qualifier)
20036 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
20040 /* Return whether the function currently being declared has an associated
20041 template parameter list. */
20043 static bool
20044 function_being_declared_is_template_p (cp_parser* parser)
20046 if (!current_template_parms || processing_template_parmlist)
20047 return false;
20049 if (parser->implicit_template_scope)
20050 return true;
20052 if (at_class_scope_p ()
20053 && TYPE_BEING_DEFINED (current_class_type))
20054 return parser->num_template_parameter_lists != 0;
20056 return ((int) parser->num_template_parameter_lists > template_class_depth
20057 (current_class_type));
20060 /* Parse a parameter-declaration-clause.
20062 parameter-declaration-clause:
20063 parameter-declaration-list [opt] ... [opt]
20064 parameter-declaration-list , ...
20066 Returns a representation for the parameter declarations. A return
20067 value of NULL indicates a parameter-declaration-clause consisting
20068 only of an ellipsis. */
20070 static tree
20071 cp_parser_parameter_declaration_clause (cp_parser* parser)
20073 tree parameters;
20074 cp_token *token;
20075 bool ellipsis_p;
20076 bool is_error;
20078 struct cleanup {
20079 cp_parser* parser;
20080 int auto_is_implicit_function_template_parm_p;
20081 ~cleanup() {
20082 parser->auto_is_implicit_function_template_parm_p
20083 = auto_is_implicit_function_template_parm_p;
20085 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
20087 (void) cleanup;
20089 if (!processing_specialization
20090 && !processing_template_parmlist
20091 && !processing_explicit_instantiation)
20092 if (!current_function_decl
20093 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
20094 parser->auto_is_implicit_function_template_parm_p = true;
20096 /* Peek at the next token. */
20097 token = cp_lexer_peek_token (parser->lexer);
20098 /* Check for trivial parameter-declaration-clauses. */
20099 if (token->type == CPP_ELLIPSIS)
20101 /* Consume the `...' token. */
20102 cp_lexer_consume_token (parser->lexer);
20103 return NULL_TREE;
20105 else if (token->type == CPP_CLOSE_PAREN)
20106 /* There are no parameters. */
20108 #ifndef NO_IMPLICIT_EXTERN_C
20109 if (in_system_header_at (input_location)
20110 && current_class_type == NULL
20111 && current_lang_name == lang_name_c)
20112 return NULL_TREE;
20113 else
20114 #endif
20115 return void_list_node;
20117 /* Check for `(void)', too, which is a special case. */
20118 else if (token->keyword == RID_VOID
20119 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20120 == CPP_CLOSE_PAREN))
20122 /* Consume the `void' token. */
20123 cp_lexer_consume_token (parser->lexer);
20124 /* There are no parameters. */
20125 return void_list_node;
20128 /* Parse the parameter-declaration-list. */
20129 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
20130 /* If a parse error occurred while parsing the
20131 parameter-declaration-list, then the entire
20132 parameter-declaration-clause is erroneous. */
20133 if (is_error)
20134 return NULL;
20136 /* Peek at the next token. */
20137 token = cp_lexer_peek_token (parser->lexer);
20138 /* If it's a `,', the clause should terminate with an ellipsis. */
20139 if (token->type == CPP_COMMA)
20141 /* Consume the `,'. */
20142 cp_lexer_consume_token (parser->lexer);
20143 /* Expect an ellipsis. */
20144 ellipsis_p
20145 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
20147 /* It might also be `...' if the optional trailing `,' was
20148 omitted. */
20149 else if (token->type == CPP_ELLIPSIS)
20151 /* Consume the `...' token. */
20152 cp_lexer_consume_token (parser->lexer);
20153 /* And remember that we saw it. */
20154 ellipsis_p = true;
20156 else
20157 ellipsis_p = false;
20159 /* Finish the parameter list. */
20160 if (!ellipsis_p)
20161 parameters = chainon (parameters, void_list_node);
20163 return parameters;
20166 /* Parse a parameter-declaration-list.
20168 parameter-declaration-list:
20169 parameter-declaration
20170 parameter-declaration-list , parameter-declaration
20172 Returns a representation of the parameter-declaration-list, as for
20173 cp_parser_parameter_declaration_clause. However, the
20174 `void_list_node' is never appended to the list. Upon return,
20175 *IS_ERROR will be true iff an error occurred. */
20177 static tree
20178 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
20180 tree parameters = NULL_TREE;
20181 tree *tail = &parameters;
20182 bool saved_in_unbraced_linkage_specification_p;
20183 int index = 0;
20185 /* Assume all will go well. */
20186 *is_error = false;
20187 /* The special considerations that apply to a function within an
20188 unbraced linkage specifications do not apply to the parameters
20189 to the function. */
20190 saved_in_unbraced_linkage_specification_p
20191 = parser->in_unbraced_linkage_specification_p;
20192 parser->in_unbraced_linkage_specification_p = false;
20194 /* Look for more parameters. */
20195 while (true)
20197 cp_parameter_declarator *parameter;
20198 tree decl = error_mark_node;
20199 bool parenthesized_p = false;
20200 int template_parm_idx = (function_being_declared_is_template_p (parser)?
20201 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
20202 (current_template_parms)) : 0);
20204 /* Parse the parameter. */
20205 parameter
20206 = cp_parser_parameter_declaration (parser,
20207 /*template_parm_p=*/false,
20208 &parenthesized_p);
20210 /* We don't know yet if the enclosing context is deprecated, so wait
20211 and warn in grokparms if appropriate. */
20212 deprecated_state = DEPRECATED_SUPPRESS;
20214 if (parameter)
20216 /* If a function parameter pack was specified and an implicit template
20217 parameter was introduced during cp_parser_parameter_declaration,
20218 change any implicit parameters introduced into packs. */
20219 if (parser->implicit_template_parms
20220 && parameter->declarator
20221 && parameter->declarator->parameter_pack_p)
20223 int latest_template_parm_idx = TREE_VEC_LENGTH
20224 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
20226 if (latest_template_parm_idx != template_parm_idx)
20227 parameter->decl_specifiers.type = convert_generic_types_to_packs
20228 (parameter->decl_specifiers.type,
20229 template_parm_idx, latest_template_parm_idx);
20232 decl = grokdeclarator (parameter->declarator,
20233 &parameter->decl_specifiers,
20234 PARM,
20235 parameter->default_argument != NULL_TREE,
20236 &parameter->decl_specifiers.attributes);
20239 deprecated_state = DEPRECATED_NORMAL;
20241 /* If a parse error occurred parsing the parameter declaration,
20242 then the entire parameter-declaration-list is erroneous. */
20243 if (decl == error_mark_node)
20245 *is_error = true;
20246 parameters = error_mark_node;
20247 break;
20250 if (parameter->decl_specifiers.attributes)
20251 cplus_decl_attributes (&decl,
20252 parameter->decl_specifiers.attributes,
20254 if (DECL_NAME (decl))
20255 decl = pushdecl (decl);
20257 if (decl != error_mark_node)
20259 retrofit_lang_decl (decl);
20260 DECL_PARM_INDEX (decl) = ++index;
20261 DECL_PARM_LEVEL (decl) = function_parm_depth ();
20264 /* Add the new parameter to the list. */
20265 *tail = build_tree_list (parameter->default_argument, decl);
20266 tail = &TREE_CHAIN (*tail);
20268 /* Peek at the next token. */
20269 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
20270 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
20271 /* These are for Objective-C++ */
20272 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20273 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20274 /* The parameter-declaration-list is complete. */
20275 break;
20276 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20278 cp_token *token;
20280 /* Peek at the next token. */
20281 token = cp_lexer_peek_nth_token (parser->lexer, 2);
20282 /* If it's an ellipsis, then the list is complete. */
20283 if (token->type == CPP_ELLIPSIS)
20284 break;
20285 /* Otherwise, there must be more parameters. Consume the
20286 `,'. */
20287 cp_lexer_consume_token (parser->lexer);
20288 /* When parsing something like:
20290 int i(float f, double d)
20292 we can tell after seeing the declaration for "f" that we
20293 are not looking at an initialization of a variable "i",
20294 but rather at the declaration of a function "i".
20296 Due to the fact that the parsing of template arguments
20297 (as specified to a template-id) requires backtracking we
20298 cannot use this technique when inside a template argument
20299 list. */
20300 if (!parser->in_template_argument_list_p
20301 && !parser->in_type_id_in_expr_p
20302 && cp_parser_uncommitted_to_tentative_parse_p (parser)
20303 /* However, a parameter-declaration of the form
20304 "float(f)" (which is a valid declaration of a
20305 parameter "f") can also be interpreted as an
20306 expression (the conversion of "f" to "float"). */
20307 && !parenthesized_p)
20308 cp_parser_commit_to_tentative_parse (parser);
20310 else
20312 cp_parser_error (parser, "expected %<,%> or %<...%>");
20313 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
20314 cp_parser_skip_to_closing_parenthesis (parser,
20315 /*recovering=*/true,
20316 /*or_comma=*/false,
20317 /*consume_paren=*/false);
20318 break;
20322 parser->in_unbraced_linkage_specification_p
20323 = saved_in_unbraced_linkage_specification_p;
20325 /* Reset implicit_template_scope if we are about to leave the function
20326 parameter list that introduced it. Note that for out-of-line member
20327 definitions, there will be one or more class scopes before we get to
20328 the template parameter scope. */
20330 if (cp_binding_level *its = parser->implicit_template_scope)
20331 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
20333 while (maybe_its->kind == sk_class)
20334 maybe_its = maybe_its->level_chain;
20335 if (maybe_its == its)
20337 parser->implicit_template_parms = 0;
20338 parser->implicit_template_scope = 0;
20342 return parameters;
20345 /* Parse a parameter declaration.
20347 parameter-declaration:
20348 decl-specifier-seq ... [opt] declarator
20349 decl-specifier-seq declarator = assignment-expression
20350 decl-specifier-seq ... [opt] abstract-declarator [opt]
20351 decl-specifier-seq abstract-declarator [opt] = assignment-expression
20353 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
20354 declares a template parameter. (In that case, a non-nested `>'
20355 token encountered during the parsing of the assignment-expression
20356 is not interpreted as a greater-than operator.)
20358 Returns a representation of the parameter, or NULL if an error
20359 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
20360 true iff the declarator is of the form "(p)". */
20362 static cp_parameter_declarator *
20363 cp_parser_parameter_declaration (cp_parser *parser,
20364 bool template_parm_p,
20365 bool *parenthesized_p)
20367 int declares_class_or_enum;
20368 cp_decl_specifier_seq decl_specifiers;
20369 cp_declarator *declarator;
20370 tree default_argument;
20371 cp_token *token = NULL, *declarator_token_start = NULL;
20372 const char *saved_message;
20373 bool template_parameter_pack_p = false;
20375 /* In a template parameter, `>' is not an operator.
20377 [temp.param]
20379 When parsing a default template-argument for a non-type
20380 template-parameter, the first non-nested `>' is taken as the end
20381 of the template parameter-list rather than a greater-than
20382 operator. */
20384 /* Type definitions may not appear in parameter types. */
20385 saved_message = parser->type_definition_forbidden_message;
20386 parser->type_definition_forbidden_message
20387 = G_("types may not be defined in parameter types");
20389 /* Parse the declaration-specifiers. */
20390 cp_parser_decl_specifier_seq (parser,
20391 CP_PARSER_FLAGS_NONE,
20392 &decl_specifiers,
20393 &declares_class_or_enum);
20395 /* Complain about missing 'typename' or other invalid type names. */
20396 if (!decl_specifiers.any_type_specifiers_p
20397 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20398 decl_specifiers.type = error_mark_node;
20400 /* If an error occurred, there's no reason to attempt to parse the
20401 rest of the declaration. */
20402 if (cp_parser_error_occurred (parser))
20404 parser->type_definition_forbidden_message = saved_message;
20405 return NULL;
20408 /* Peek at the next token. */
20409 token = cp_lexer_peek_token (parser->lexer);
20411 /* If the next token is a `)', `,', `=', `>', or `...', then there
20412 is no declarator. However, when variadic templates are enabled,
20413 there may be a declarator following `...'. */
20414 if (token->type == CPP_CLOSE_PAREN
20415 || token->type == CPP_COMMA
20416 || token->type == CPP_EQ
20417 || token->type == CPP_GREATER)
20419 declarator = NULL;
20420 if (parenthesized_p)
20421 *parenthesized_p = false;
20423 /* Otherwise, there should be a declarator. */
20424 else
20426 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20427 parser->default_arg_ok_p = false;
20429 /* After seeing a decl-specifier-seq, if the next token is not a
20430 "(", there is no possibility that the code is a valid
20431 expression. Therefore, if parsing tentatively, we commit at
20432 this point. */
20433 if (!parser->in_template_argument_list_p
20434 /* In an expression context, having seen:
20436 (int((char ...
20438 we cannot be sure whether we are looking at a
20439 function-type (taking a "char" as a parameter) or a cast
20440 of some object of type "char" to "int". */
20441 && !parser->in_type_id_in_expr_p
20442 && cp_parser_uncommitted_to_tentative_parse_p (parser)
20443 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20444 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
20445 cp_parser_commit_to_tentative_parse (parser);
20446 /* Parse the declarator. */
20447 declarator_token_start = token;
20448 declarator = cp_parser_declarator (parser,
20449 CP_PARSER_DECLARATOR_EITHER,
20450 /*ctor_dtor_or_conv_p=*/NULL,
20451 parenthesized_p,
20452 /*member_p=*/false,
20453 /*friend_p=*/false);
20454 parser->default_arg_ok_p = saved_default_arg_ok_p;
20455 /* After the declarator, allow more attributes. */
20456 decl_specifiers.attributes
20457 = chainon (decl_specifiers.attributes,
20458 cp_parser_attributes_opt (parser));
20460 /* If the declarator is a template parameter pack, remember that and
20461 clear the flag in the declarator itself so we don't get errors
20462 from grokdeclarator. */
20463 if (template_parm_p && declarator && declarator->parameter_pack_p)
20465 declarator->parameter_pack_p = false;
20466 template_parameter_pack_p = true;
20470 /* If the next token is an ellipsis, and we have not seen a declarator
20471 name, and if either the type of the declarator contains parameter
20472 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
20473 for, eg, abbreviated integral type names), then we actually have a
20474 parameter pack expansion expression. Otherwise, leave the ellipsis
20475 for a C-style variadic function. */
20476 token = cp_lexer_peek_token (parser->lexer);
20477 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20479 tree type = decl_specifiers.type;
20481 if (type && DECL_P (type))
20482 type = TREE_TYPE (type);
20484 if (((type
20485 && TREE_CODE (type) != TYPE_PACK_EXPANSION
20486 && (template_parm_p || uses_parameter_packs (type)))
20487 || (!type && template_parm_p))
20488 && declarator_can_be_parameter_pack (declarator))
20490 /* Consume the `...'. */
20491 cp_lexer_consume_token (parser->lexer);
20492 maybe_warn_variadic_templates ();
20494 /* Build a pack expansion type */
20495 if (template_parm_p)
20496 template_parameter_pack_p = true;
20497 else if (declarator)
20498 declarator->parameter_pack_p = true;
20499 else
20500 decl_specifiers.type = make_pack_expansion (type);
20504 /* The restriction on defining new types applies only to the type
20505 of the parameter, not to the default argument. */
20506 parser->type_definition_forbidden_message = saved_message;
20508 /* If the next token is `=', then process a default argument. */
20509 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20511 tree type = decl_specifiers.type;
20512 token = cp_lexer_peek_token (parser->lexer);
20513 /* If we are defining a class, then the tokens that make up the
20514 default argument must be saved and processed later. */
20515 if (!template_parm_p && at_class_scope_p ()
20516 && TYPE_BEING_DEFINED (current_class_type)
20517 && !LAMBDA_TYPE_P (current_class_type))
20518 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
20520 // A constrained-type-specifier may declare a type template-parameter.
20521 else if (declares_constrained_type_template_parameter (type))
20522 default_argument
20523 = cp_parser_default_type_template_argument (parser);
20525 // A constrained-type-specifier may declare a template-template-parameter.
20526 else if (declares_constrained_template_template_parameter (type))
20527 default_argument
20528 = cp_parser_default_template_template_argument (parser);
20530 /* Outside of a class definition, we can just parse the
20531 assignment-expression. */
20532 else
20533 default_argument
20534 = cp_parser_default_argument (parser, template_parm_p);
20536 if (!parser->default_arg_ok_p)
20538 permerror (token->location,
20539 "default arguments are only "
20540 "permitted for function parameters");
20542 else if ((declarator && declarator->parameter_pack_p)
20543 || template_parameter_pack_p
20544 || (decl_specifiers.type
20545 && PACK_EXPANSION_P (decl_specifiers.type)))
20547 /* Find the name of the parameter pack. */
20548 cp_declarator *id_declarator = declarator;
20549 while (id_declarator && id_declarator->kind != cdk_id)
20550 id_declarator = id_declarator->declarator;
20552 if (id_declarator && id_declarator->kind == cdk_id)
20553 error_at (declarator_token_start->location,
20554 template_parm_p
20555 ? G_("template parameter pack %qD "
20556 "cannot have a default argument")
20557 : G_("parameter pack %qD cannot have "
20558 "a default argument"),
20559 id_declarator->u.id.unqualified_name);
20560 else
20561 error_at (declarator_token_start->location,
20562 template_parm_p
20563 ? G_("template parameter pack cannot have "
20564 "a default argument")
20565 : G_("parameter pack cannot have a "
20566 "default argument"));
20568 default_argument = NULL_TREE;
20571 else
20572 default_argument = NULL_TREE;
20574 return make_parameter_declarator (&decl_specifiers,
20575 declarator,
20576 default_argument,
20577 template_parameter_pack_p);
20580 /* Parse a default argument and return it.
20582 TEMPLATE_PARM_P is true if this is a default argument for a
20583 non-type template parameter. */
20584 static tree
20585 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
20587 tree default_argument = NULL_TREE;
20588 bool saved_greater_than_is_operator_p;
20589 bool saved_local_variables_forbidden_p;
20590 bool non_constant_p, is_direct_init;
20592 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
20593 set correctly. */
20594 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
20595 parser->greater_than_is_operator_p = !template_parm_p;
20596 /* Local variable names (and the `this' keyword) may not
20597 appear in a default argument. */
20598 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20599 parser->local_variables_forbidden_p = true;
20600 /* Parse the assignment-expression. */
20601 if (template_parm_p)
20602 push_deferring_access_checks (dk_no_deferred);
20603 tree saved_class_ptr = NULL_TREE;
20604 tree saved_class_ref = NULL_TREE;
20605 /* The "this" pointer is not valid in a default argument. */
20606 if (cfun)
20608 saved_class_ptr = current_class_ptr;
20609 cp_function_chain->x_current_class_ptr = NULL_TREE;
20610 saved_class_ref = current_class_ref;
20611 cp_function_chain->x_current_class_ref = NULL_TREE;
20613 default_argument
20614 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
20615 /* Restore the "this" pointer. */
20616 if (cfun)
20618 cp_function_chain->x_current_class_ptr = saved_class_ptr;
20619 cp_function_chain->x_current_class_ref = saved_class_ref;
20621 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
20622 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20623 if (template_parm_p)
20624 pop_deferring_access_checks ();
20625 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
20626 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20628 return default_argument;
20631 /* Parse a function-body.
20633 function-body:
20634 compound_statement */
20636 static void
20637 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
20639 cp_parser_compound_statement (parser, NULL, (in_function_try_block
20640 ? BCS_TRY_BLOCK : BCS_NORMAL),
20641 true);
20644 /* Parse a ctor-initializer-opt followed by a function-body. Return
20645 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
20646 is true we are parsing a function-try-block. */
20648 static bool
20649 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
20650 bool in_function_try_block)
20652 tree body, list;
20653 bool ctor_initializer_p;
20654 const bool check_body_p =
20655 DECL_CONSTRUCTOR_P (current_function_decl)
20656 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
20657 tree last = NULL;
20659 /* Begin the function body. */
20660 body = begin_function_body ();
20661 /* Parse the optional ctor-initializer. */
20662 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
20664 /* If we're parsing a constexpr constructor definition, we need
20665 to check that the constructor body is indeed empty. However,
20666 before we get to cp_parser_function_body lot of junk has been
20667 generated, so we can't just check that we have an empty block.
20668 Rather we take a snapshot of the outermost block, and check whether
20669 cp_parser_function_body changed its state. */
20670 if (check_body_p)
20672 list = cur_stmt_list;
20673 if (STATEMENT_LIST_TAIL (list))
20674 last = STATEMENT_LIST_TAIL (list)->stmt;
20676 /* Parse the function-body. */
20677 cp_parser_function_body (parser, in_function_try_block);
20678 if (check_body_p)
20679 check_constexpr_ctor_body (last, list, /*complain=*/true);
20680 /* Finish the function body. */
20681 finish_function_body (body);
20683 return ctor_initializer_p;
20686 /* Parse an initializer.
20688 initializer:
20689 = initializer-clause
20690 ( expression-list )
20692 Returns an expression representing the initializer. If no
20693 initializer is present, NULL_TREE is returned.
20695 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
20696 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
20697 set to TRUE if there is no initializer present. If there is an
20698 initializer, and it is not a constant-expression, *NON_CONSTANT_P
20699 is set to true; otherwise it is set to false. */
20701 static tree
20702 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
20703 bool* non_constant_p)
20705 cp_token *token;
20706 tree init;
20708 /* Peek at the next token. */
20709 token = cp_lexer_peek_token (parser->lexer);
20711 /* Let our caller know whether or not this initializer was
20712 parenthesized. */
20713 *is_direct_init = (token->type != CPP_EQ);
20714 /* Assume that the initializer is constant. */
20715 *non_constant_p = false;
20717 if (token->type == CPP_EQ)
20719 /* Consume the `='. */
20720 cp_lexer_consume_token (parser->lexer);
20721 /* Parse the initializer-clause. */
20722 init = cp_parser_initializer_clause (parser, non_constant_p);
20724 else if (token->type == CPP_OPEN_PAREN)
20726 vec<tree, va_gc> *vec;
20727 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20728 /*cast_p=*/false,
20729 /*allow_expansion_p=*/true,
20730 non_constant_p);
20731 if (vec == NULL)
20732 return error_mark_node;
20733 init = build_tree_list_vec (vec);
20734 release_tree_vector (vec);
20736 else if (token->type == CPP_OPEN_BRACE)
20738 cp_lexer_set_source_position (parser->lexer);
20739 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20740 init = cp_parser_braced_list (parser, non_constant_p);
20741 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
20743 else
20745 /* Anything else is an error. */
20746 cp_parser_error (parser, "expected initializer");
20747 init = error_mark_node;
20750 return init;
20753 /* Parse an initializer-clause.
20755 initializer-clause:
20756 assignment-expression
20757 braced-init-list
20759 Returns an expression representing the initializer.
20761 If the `assignment-expression' production is used the value
20762 returned is simply a representation for the expression.
20764 Otherwise, calls cp_parser_braced_list. */
20766 static cp_expr
20767 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
20769 cp_expr initializer;
20771 /* Assume the expression is constant. */
20772 *non_constant_p = false;
20774 /* If it is not a `{', then we are looking at an
20775 assignment-expression. */
20776 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
20778 initializer
20779 = cp_parser_constant_expression (parser,
20780 /*allow_non_constant_p=*/true,
20781 non_constant_p);
20783 else
20784 initializer = cp_parser_braced_list (parser, non_constant_p);
20786 return initializer;
20789 /* Parse a brace-enclosed initializer list.
20791 braced-init-list:
20792 { initializer-list , [opt] }
20795 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
20796 the elements of the initializer-list (or NULL, if the last
20797 production is used). The TREE_TYPE for the CONSTRUCTOR will be
20798 NULL_TREE. There is no way to detect whether or not the optional
20799 trailing `,' was provided. NON_CONSTANT_P is as for
20800 cp_parser_initializer. */
20802 static cp_expr
20803 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
20805 tree initializer;
20806 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
20808 /* Consume the `{' token. */
20809 cp_lexer_consume_token (parser->lexer);
20810 /* Create a CONSTRUCTOR to represent the braced-initializer. */
20811 initializer = make_node (CONSTRUCTOR);
20812 /* If it's not a `}', then there is a non-trivial initializer. */
20813 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
20815 /* Parse the initializer list. */
20816 CONSTRUCTOR_ELTS (initializer)
20817 = cp_parser_initializer_list (parser, non_constant_p);
20818 /* A trailing `,' token is allowed. */
20819 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20820 cp_lexer_consume_token (parser->lexer);
20822 else
20823 *non_constant_p = false;
20824 /* Now, there should be a trailing `}'. */
20825 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
20826 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20827 TREE_TYPE (initializer) = init_list_type_node;
20829 cp_expr result (initializer);
20830 /* Build a location of the form:
20831 { ... }
20832 ^~~~~~~
20833 with caret==start at the open brace, finish at the close brace. */
20834 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
20835 result.set_location (combined_loc);
20836 return result;
20839 /* Consume tokens up to, and including, the next non-nested closing `]'.
20840 Returns true iff we found a closing `]'. */
20842 static bool
20843 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
20845 unsigned square_depth = 0;
20847 while (true)
20849 cp_token * token = cp_lexer_peek_token (parser->lexer);
20851 switch (token->type)
20853 case CPP_EOF:
20854 case CPP_PRAGMA_EOL:
20855 /* If we've run out of tokens, then there is no closing `]'. */
20856 return false;
20858 case CPP_OPEN_SQUARE:
20859 ++square_depth;
20860 break;
20862 case CPP_CLOSE_SQUARE:
20863 if (!square_depth--)
20865 cp_lexer_consume_token (parser->lexer);
20866 return true;
20868 break;
20870 default:
20871 break;
20874 /* Consume the token. */
20875 cp_lexer_consume_token (parser->lexer);
20879 /* Return true if we are looking at an array-designator, false otherwise. */
20881 static bool
20882 cp_parser_array_designator_p (cp_parser *parser)
20884 /* Consume the `['. */
20885 cp_lexer_consume_token (parser->lexer);
20887 cp_lexer_save_tokens (parser->lexer);
20889 /* Skip tokens until the next token is a closing square bracket.
20890 If we find the closing `]', and the next token is a `=', then
20891 we are looking at an array designator. */
20892 bool array_designator_p
20893 = (cp_parser_skip_to_closing_square_bracket (parser)
20894 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
20896 /* Roll back the tokens we skipped. */
20897 cp_lexer_rollback_tokens (parser->lexer);
20899 return array_designator_p;
20902 /* Parse an initializer-list.
20904 initializer-list:
20905 initializer-clause ... [opt]
20906 initializer-list , initializer-clause ... [opt]
20908 GNU Extension:
20910 initializer-list:
20911 designation initializer-clause ...[opt]
20912 initializer-list , designation initializer-clause ...[opt]
20914 designation:
20915 . identifier =
20916 identifier :
20917 [ constant-expression ] =
20919 Returns a vec of constructor_elt. The VALUE of each elt is an expression
20920 for the initializer. If the INDEX of the elt is non-NULL, it is the
20921 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
20922 as for cp_parser_initializer. */
20924 static vec<constructor_elt, va_gc> *
20925 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
20927 vec<constructor_elt, va_gc> *v = NULL;
20929 /* Assume all of the expressions are constant. */
20930 *non_constant_p = false;
20932 /* Parse the rest of the list. */
20933 while (true)
20935 cp_token *token;
20936 tree designator;
20937 tree initializer;
20938 bool clause_non_constant_p;
20940 /* If the next token is an identifier and the following one is a
20941 colon, we are looking at the GNU designated-initializer
20942 syntax. */
20943 if (cp_parser_allow_gnu_extensions_p (parser)
20944 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
20945 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
20947 /* Warn the user that they are using an extension. */
20948 pedwarn (input_location, OPT_Wpedantic,
20949 "ISO C++ does not allow designated initializers");
20950 /* Consume the identifier. */
20951 designator = cp_lexer_consume_token (parser->lexer)->u.value;
20952 /* Consume the `:'. */
20953 cp_lexer_consume_token (parser->lexer);
20955 /* Also handle the C99 syntax, '. id ='. */
20956 else if (cp_parser_allow_gnu_extensions_p (parser)
20957 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
20958 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
20959 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
20961 /* Warn the user that they are using an extension. */
20962 pedwarn (input_location, OPT_Wpedantic,
20963 "ISO C++ does not allow C99 designated initializers");
20964 /* Consume the `.'. */
20965 cp_lexer_consume_token (parser->lexer);
20966 /* Consume the identifier. */
20967 designator = cp_lexer_consume_token (parser->lexer)->u.value;
20968 /* Consume the `='. */
20969 cp_lexer_consume_token (parser->lexer);
20971 /* Also handle C99 array designators, '[ const ] ='. */
20972 else if (cp_parser_allow_gnu_extensions_p (parser)
20973 && !c_dialect_objc ()
20974 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
20976 /* In C++11, [ could start a lambda-introducer. */
20977 bool non_const = false;
20979 cp_parser_parse_tentatively (parser);
20981 if (!cp_parser_array_designator_p (parser))
20983 cp_parser_simulate_error (parser);
20984 designator = NULL_TREE;
20986 else
20988 designator = cp_parser_constant_expression (parser, true,
20989 &non_const);
20990 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
20991 cp_parser_require (parser, CPP_EQ, RT_EQ);
20994 if (!cp_parser_parse_definitely (parser))
20995 designator = NULL_TREE;
20996 else if (non_const)
20997 require_potential_rvalue_constant_expression (designator);
20999 else
21000 designator = NULL_TREE;
21002 /* Parse the initializer. */
21003 initializer = cp_parser_initializer_clause (parser,
21004 &clause_non_constant_p);
21005 /* If any clause is non-constant, so is the entire initializer. */
21006 if (clause_non_constant_p)
21007 *non_constant_p = true;
21009 /* If we have an ellipsis, this is an initializer pack
21010 expansion. */
21011 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21013 /* Consume the `...'. */
21014 cp_lexer_consume_token (parser->lexer);
21016 /* Turn the initializer into an initializer expansion. */
21017 initializer = make_pack_expansion (initializer);
21020 /* Add it to the vector. */
21021 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
21023 /* If the next token is not a comma, we have reached the end of
21024 the list. */
21025 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21026 break;
21028 /* Peek at the next token. */
21029 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21030 /* If the next token is a `}', then we're still done. An
21031 initializer-clause can have a trailing `,' after the
21032 initializer-list and before the closing `}'. */
21033 if (token->type == CPP_CLOSE_BRACE)
21034 break;
21036 /* Consume the `,' token. */
21037 cp_lexer_consume_token (parser->lexer);
21040 return v;
21043 /* Classes [gram.class] */
21045 /* Parse a class-name.
21047 class-name:
21048 identifier
21049 template-id
21051 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
21052 to indicate that names looked up in dependent types should be
21053 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
21054 keyword has been used to indicate that the name that appears next
21055 is a template. TAG_TYPE indicates the explicit tag given before
21056 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
21057 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
21058 is the class being defined in a class-head. If ENUM_OK is TRUE,
21059 enum-names are also accepted.
21061 Returns the TYPE_DECL representing the class. */
21063 static tree
21064 cp_parser_class_name (cp_parser *parser,
21065 bool typename_keyword_p,
21066 bool template_keyword_p,
21067 enum tag_types tag_type,
21068 bool check_dependency_p,
21069 bool class_head_p,
21070 bool is_declaration,
21071 bool enum_ok)
21073 tree decl;
21074 tree scope;
21075 bool typename_p;
21076 cp_token *token;
21077 tree identifier = NULL_TREE;
21079 /* All class-names start with an identifier. */
21080 token = cp_lexer_peek_token (parser->lexer);
21081 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
21083 cp_parser_error (parser, "expected class-name");
21084 return error_mark_node;
21087 /* PARSER->SCOPE can be cleared when parsing the template-arguments
21088 to a template-id, so we save it here. */
21089 scope = parser->scope;
21090 if (scope == error_mark_node)
21091 return error_mark_node;
21093 /* Any name names a type if we're following the `typename' keyword
21094 in a qualified name where the enclosing scope is type-dependent. */
21095 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
21096 && dependent_type_p (scope));
21097 /* Handle the common case (an identifier, but not a template-id)
21098 efficiently. */
21099 if (token->type == CPP_NAME
21100 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
21102 cp_token *identifier_token;
21103 bool ambiguous_p;
21105 /* Look for the identifier. */
21106 identifier_token = cp_lexer_peek_token (parser->lexer);
21107 ambiguous_p = identifier_token->error_reported;
21108 identifier = cp_parser_identifier (parser);
21109 /* If the next token isn't an identifier, we are certainly not
21110 looking at a class-name. */
21111 if (identifier == error_mark_node)
21112 decl = error_mark_node;
21113 /* If we know this is a type-name, there's no need to look it
21114 up. */
21115 else if (typename_p)
21116 decl = identifier;
21117 else
21119 tree ambiguous_decls;
21120 /* If we already know that this lookup is ambiguous, then
21121 we've already issued an error message; there's no reason
21122 to check again. */
21123 if (ambiguous_p)
21125 cp_parser_simulate_error (parser);
21126 return error_mark_node;
21128 /* If the next token is a `::', then the name must be a type
21129 name.
21131 [basic.lookup.qual]
21133 During the lookup for a name preceding the :: scope
21134 resolution operator, object, function, and enumerator
21135 names are ignored. */
21136 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21137 tag_type = typename_type;
21138 /* Look up the name. */
21139 decl = cp_parser_lookup_name (parser, identifier,
21140 tag_type,
21141 /*is_template=*/false,
21142 /*is_namespace=*/false,
21143 check_dependency_p,
21144 &ambiguous_decls,
21145 identifier_token->location);
21146 if (ambiguous_decls)
21148 if (cp_parser_parsing_tentatively (parser))
21149 cp_parser_simulate_error (parser);
21150 return error_mark_node;
21154 else
21156 /* Try a template-id. */
21157 decl = cp_parser_template_id (parser, template_keyword_p,
21158 check_dependency_p,
21159 tag_type,
21160 is_declaration);
21161 if (decl == error_mark_node)
21162 return error_mark_node;
21165 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
21167 /* If this is a typename, create a TYPENAME_TYPE. */
21168 if (typename_p && decl != error_mark_node)
21170 decl = make_typename_type (scope, decl, typename_type,
21171 /*complain=*/tf_error);
21172 if (decl != error_mark_node)
21173 decl = TYPE_NAME (decl);
21176 decl = strip_using_decl (decl);
21178 /* Check to see that it is really the name of a class. */
21179 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
21180 && identifier_p (TREE_OPERAND (decl, 0))
21181 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21182 /* Situations like this:
21184 template <typename T> struct A {
21185 typename T::template X<int>::I i;
21188 are problematic. Is `T::template X<int>' a class-name? The
21189 standard does not seem to be definitive, but there is no other
21190 valid interpretation of the following `::'. Therefore, those
21191 names are considered class-names. */
21193 decl = make_typename_type (scope, decl, tag_type, tf_error);
21194 if (decl != error_mark_node)
21195 decl = TYPE_NAME (decl);
21197 else if (TREE_CODE (decl) != TYPE_DECL
21198 || TREE_TYPE (decl) == error_mark_node
21199 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
21200 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
21201 /* In Objective-C 2.0, a classname followed by '.' starts a
21202 dot-syntax expression, and it's not a type-name. */
21203 || (c_dialect_objc ()
21204 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
21205 && objc_is_class_name (decl)))
21206 decl = error_mark_node;
21208 if (decl == error_mark_node)
21209 cp_parser_error (parser, "expected class-name");
21210 else if (identifier && !parser->scope)
21211 maybe_note_name_used_in_class (identifier, decl);
21213 return decl;
21216 /* Parse a class-specifier.
21218 class-specifier:
21219 class-head { member-specification [opt] }
21221 Returns the TREE_TYPE representing the class. */
21223 static tree
21224 cp_parser_class_specifier_1 (cp_parser* parser)
21226 tree type;
21227 tree attributes = NULL_TREE;
21228 bool nested_name_specifier_p;
21229 unsigned saved_num_template_parameter_lists;
21230 bool saved_in_function_body;
21231 unsigned char in_statement;
21232 bool in_switch_statement_p;
21233 bool saved_in_unbraced_linkage_specification_p;
21234 tree old_scope = NULL_TREE;
21235 tree scope = NULL_TREE;
21236 cp_token *closing_brace;
21238 push_deferring_access_checks (dk_no_deferred);
21240 /* Parse the class-head. */
21241 type = cp_parser_class_head (parser,
21242 &nested_name_specifier_p);
21243 /* If the class-head was a semantic disaster, skip the entire body
21244 of the class. */
21245 if (!type)
21247 cp_parser_skip_to_end_of_block_or_statement (parser);
21248 pop_deferring_access_checks ();
21249 return error_mark_node;
21252 /* Look for the `{'. */
21253 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
21255 pop_deferring_access_checks ();
21256 return error_mark_node;
21259 cp_ensure_no_omp_declare_simd (parser);
21260 cp_ensure_no_oacc_routine (parser);
21262 /* Issue an error message if type-definitions are forbidden here. */
21263 cp_parser_check_type_definition (parser);
21264 /* Remember that we are defining one more class. */
21265 ++parser->num_classes_being_defined;
21266 /* Inside the class, surrounding template-parameter-lists do not
21267 apply. */
21268 saved_num_template_parameter_lists
21269 = parser->num_template_parameter_lists;
21270 parser->num_template_parameter_lists = 0;
21271 /* We are not in a function body. */
21272 saved_in_function_body = parser->in_function_body;
21273 parser->in_function_body = false;
21274 /* Or in a loop. */
21275 in_statement = parser->in_statement;
21276 parser->in_statement = 0;
21277 /* Or in a switch. */
21278 in_switch_statement_p = parser->in_switch_statement_p;
21279 parser->in_switch_statement_p = false;
21280 /* We are not immediately inside an extern "lang" block. */
21281 saved_in_unbraced_linkage_specification_p
21282 = parser->in_unbraced_linkage_specification_p;
21283 parser->in_unbraced_linkage_specification_p = false;
21285 // Associate constraints with the type.
21286 if (flag_concepts)
21287 type = associate_classtype_constraints (type);
21289 /* Start the class. */
21290 if (nested_name_specifier_p)
21292 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
21293 old_scope = push_inner_scope (scope);
21295 type = begin_class_definition (type);
21297 if (type == error_mark_node)
21298 /* If the type is erroneous, skip the entire body of the class. */
21299 cp_parser_skip_to_closing_brace (parser);
21300 else
21301 /* Parse the member-specification. */
21302 cp_parser_member_specification_opt (parser);
21304 /* Look for the trailing `}'. */
21305 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21306 /* Look for trailing attributes to apply to this class. */
21307 if (cp_parser_allow_gnu_extensions_p (parser))
21308 attributes = cp_parser_gnu_attributes_opt (parser);
21309 if (type != error_mark_node)
21310 type = finish_struct (type, attributes);
21311 if (nested_name_specifier_p)
21312 pop_inner_scope (old_scope, scope);
21314 /* We've finished a type definition. Check for the common syntax
21315 error of forgetting a semicolon after the definition. We need to
21316 be careful, as we can't just check for not-a-semicolon and be done
21317 with it; the user might have typed:
21319 class X { } c = ...;
21320 class X { } *p = ...;
21322 and so forth. Instead, enumerate all the possible tokens that
21323 might follow this production; if we don't see one of them, then
21324 complain and silently insert the semicolon. */
21326 cp_token *token = cp_lexer_peek_token (parser->lexer);
21327 bool want_semicolon = true;
21329 if (cp_next_tokens_can_be_std_attribute_p (parser))
21330 /* Don't try to parse c++11 attributes here. As per the
21331 grammar, that should be a task for
21332 cp_parser_decl_specifier_seq. */
21333 want_semicolon = false;
21335 switch (token->type)
21337 case CPP_NAME:
21338 case CPP_SEMICOLON:
21339 case CPP_MULT:
21340 case CPP_AND:
21341 case CPP_OPEN_PAREN:
21342 case CPP_CLOSE_PAREN:
21343 case CPP_COMMA:
21344 want_semicolon = false;
21345 break;
21347 /* While it's legal for type qualifiers and storage class
21348 specifiers to follow type definitions in the grammar, only
21349 compiler testsuites contain code like that. Assume that if
21350 we see such code, then what we're really seeing is a case
21351 like:
21353 class X { }
21354 const <type> var = ...;
21358 class Y { }
21359 static <type> func (...) ...
21361 i.e. the qualifier or specifier applies to the next
21362 declaration. To do so, however, we need to look ahead one
21363 more token to see if *that* token is a type specifier.
21365 This code could be improved to handle:
21367 class Z { }
21368 static const <type> var = ...; */
21369 case CPP_KEYWORD:
21370 if (keyword_is_decl_specifier (token->keyword))
21372 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
21374 /* Handling user-defined types here would be nice, but very
21375 tricky. */
21376 want_semicolon
21377 = (lookahead->type == CPP_KEYWORD
21378 && keyword_begins_type_specifier (lookahead->keyword));
21380 break;
21381 default:
21382 break;
21385 /* If we don't have a type, then something is very wrong and we
21386 shouldn't try to do anything clever. Likewise for not seeing the
21387 closing brace. */
21388 if (closing_brace && TYPE_P (type) && want_semicolon)
21390 cp_token_position prev
21391 = cp_lexer_previous_token_position (parser->lexer);
21392 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
21393 location_t loc = prev_token->location;
21395 if (CLASSTYPE_DECLARED_CLASS (type))
21396 error_at (loc, "expected %<;%> after class definition");
21397 else if (TREE_CODE (type) == RECORD_TYPE)
21398 error_at (loc, "expected %<;%> after struct definition");
21399 else if (TREE_CODE (type) == UNION_TYPE)
21400 error_at (loc, "expected %<;%> after union definition");
21401 else
21402 gcc_unreachable ();
21404 /* Unget one token and smash it to look as though we encountered
21405 a semicolon in the input stream. */
21406 cp_lexer_set_token_position (parser->lexer, prev);
21407 token = cp_lexer_peek_token (parser->lexer);
21408 token->type = CPP_SEMICOLON;
21409 token->keyword = RID_MAX;
21413 /* If this class is not itself within the scope of another class,
21414 then we need to parse the bodies of all of the queued function
21415 definitions. Note that the queued functions defined in a class
21416 are not always processed immediately following the
21417 class-specifier for that class. Consider:
21419 struct A {
21420 struct B { void f() { sizeof (A); } };
21423 If `f' were processed before the processing of `A' were
21424 completed, there would be no way to compute the size of `A'.
21425 Note that the nesting we are interested in here is lexical --
21426 not the semantic nesting given by TYPE_CONTEXT. In particular,
21427 for:
21429 struct A { struct B; };
21430 struct A::B { void f() { } };
21432 there is no need to delay the parsing of `A::B::f'. */
21433 if (--parser->num_classes_being_defined == 0)
21435 tree decl;
21436 tree class_type = NULL_TREE;
21437 tree pushed_scope = NULL_TREE;
21438 unsigned ix;
21439 cp_default_arg_entry *e;
21440 tree save_ccp, save_ccr;
21442 /* In a first pass, parse default arguments to the functions.
21443 Then, in a second pass, parse the bodies of the functions.
21444 This two-phased approach handles cases like:
21446 struct S {
21447 void f() { g(); }
21448 void g(int i = 3);
21452 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
21454 decl = e->decl;
21455 /* If there are default arguments that have not yet been processed,
21456 take care of them now. */
21457 if (class_type != e->class_type)
21459 if (pushed_scope)
21460 pop_scope (pushed_scope);
21461 class_type = e->class_type;
21462 pushed_scope = push_scope (class_type);
21464 /* Make sure that any template parameters are in scope. */
21465 maybe_begin_member_template_processing (decl);
21466 /* Parse the default argument expressions. */
21467 cp_parser_late_parsing_default_args (parser, decl);
21468 /* Remove any template parameters from the symbol table. */
21469 maybe_end_member_template_processing ();
21471 vec_safe_truncate (unparsed_funs_with_default_args, 0);
21472 /* Now parse any NSDMIs. */
21473 save_ccp = current_class_ptr;
21474 save_ccr = current_class_ref;
21475 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
21477 if (class_type != DECL_CONTEXT (decl))
21479 if (pushed_scope)
21480 pop_scope (pushed_scope);
21481 class_type = DECL_CONTEXT (decl);
21482 pushed_scope = push_scope (class_type);
21484 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
21485 cp_parser_late_parsing_nsdmi (parser, decl);
21487 vec_safe_truncate (unparsed_nsdmis, 0);
21488 current_class_ptr = save_ccp;
21489 current_class_ref = save_ccr;
21490 if (pushed_scope)
21491 pop_scope (pushed_scope);
21493 /* Now do some post-NSDMI bookkeeping. */
21494 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
21495 after_nsdmi_defaulted_late_checks (class_type);
21496 vec_safe_truncate (unparsed_classes, 0);
21497 after_nsdmi_defaulted_late_checks (type);
21499 /* Now parse the body of the functions. */
21500 if (flag_openmp)
21502 /* OpenMP UDRs need to be parsed before all other functions. */
21503 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21504 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
21505 cp_parser_late_parsing_for_member (parser, decl);
21506 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21507 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
21508 cp_parser_late_parsing_for_member (parser, decl);
21510 else
21511 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21512 cp_parser_late_parsing_for_member (parser, decl);
21513 vec_safe_truncate (unparsed_funs_with_definitions, 0);
21515 else
21516 vec_safe_push (unparsed_classes, type);
21518 /* Put back any saved access checks. */
21519 pop_deferring_access_checks ();
21521 /* Restore saved state. */
21522 parser->in_switch_statement_p = in_switch_statement_p;
21523 parser->in_statement = in_statement;
21524 parser->in_function_body = saved_in_function_body;
21525 parser->num_template_parameter_lists
21526 = saved_num_template_parameter_lists;
21527 parser->in_unbraced_linkage_specification_p
21528 = saved_in_unbraced_linkage_specification_p;
21530 return type;
21533 static tree
21534 cp_parser_class_specifier (cp_parser* parser)
21536 tree ret;
21537 timevar_push (TV_PARSE_STRUCT);
21538 ret = cp_parser_class_specifier_1 (parser);
21539 timevar_pop (TV_PARSE_STRUCT);
21540 return ret;
21543 /* Parse a class-head.
21545 class-head:
21546 class-key identifier [opt] base-clause [opt]
21547 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
21548 class-key nested-name-specifier [opt] template-id
21549 base-clause [opt]
21551 class-virt-specifier:
21552 final
21554 GNU Extensions:
21555 class-key attributes identifier [opt] base-clause [opt]
21556 class-key attributes nested-name-specifier identifier base-clause [opt]
21557 class-key attributes nested-name-specifier [opt] template-id
21558 base-clause [opt]
21560 Upon return BASES is initialized to the list of base classes (or
21561 NULL, if there are none) in the same form returned by
21562 cp_parser_base_clause.
21564 Returns the TYPE of the indicated class. Sets
21565 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
21566 involving a nested-name-specifier was used, and FALSE otherwise.
21568 Returns error_mark_node if this is not a class-head.
21570 Returns NULL_TREE if the class-head is syntactically valid, but
21571 semantically invalid in a way that means we should skip the entire
21572 body of the class. */
21574 static tree
21575 cp_parser_class_head (cp_parser* parser,
21576 bool* nested_name_specifier_p)
21578 tree nested_name_specifier;
21579 enum tag_types class_key;
21580 tree id = NULL_TREE;
21581 tree type = NULL_TREE;
21582 tree attributes;
21583 tree bases;
21584 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21585 bool template_id_p = false;
21586 bool qualified_p = false;
21587 bool invalid_nested_name_p = false;
21588 bool invalid_explicit_specialization_p = false;
21589 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
21590 tree pushed_scope = NULL_TREE;
21591 unsigned num_templates;
21592 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
21593 /* Assume no nested-name-specifier will be present. */
21594 *nested_name_specifier_p = false;
21595 /* Assume no template parameter lists will be used in defining the
21596 type. */
21597 num_templates = 0;
21598 parser->colon_corrects_to_scope_p = false;
21600 /* Look for the class-key. */
21601 class_key = cp_parser_class_key (parser);
21602 if (class_key == none_type)
21603 return error_mark_node;
21605 /* Parse the attributes. */
21606 attributes = cp_parser_attributes_opt (parser);
21608 /* If the next token is `::', that is invalid -- but sometimes
21609 people do try to write:
21611 struct ::S {};
21613 Handle this gracefully by accepting the extra qualifier, and then
21614 issuing an error about it later if this really is a
21615 class-head. If it turns out just to be an elaborated type
21616 specifier, remain silent. */
21617 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
21618 qualified_p = true;
21620 push_deferring_access_checks (dk_no_check);
21622 /* Determine the name of the class. Begin by looking for an
21623 optional nested-name-specifier. */
21624 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
21625 nested_name_specifier
21626 = cp_parser_nested_name_specifier_opt (parser,
21627 /*typename_keyword_p=*/false,
21628 /*check_dependency_p=*/false,
21629 /*type_p=*/true,
21630 /*is_declaration=*/false);
21631 /* If there was a nested-name-specifier, then there *must* be an
21632 identifier. */
21633 if (nested_name_specifier)
21635 type_start_token = cp_lexer_peek_token (parser->lexer);
21636 /* Although the grammar says `identifier', it really means
21637 `class-name' or `template-name'. You are only allowed to
21638 define a class that has already been declared with this
21639 syntax.
21641 The proposed resolution for Core Issue 180 says that wherever
21642 you see `class T::X' you should treat `X' as a type-name.
21644 It is OK to define an inaccessible class; for example:
21646 class A { class B; };
21647 class A::B {};
21649 We do not know if we will see a class-name, or a
21650 template-name. We look for a class-name first, in case the
21651 class-name is a template-id; if we looked for the
21652 template-name first we would stop after the template-name. */
21653 cp_parser_parse_tentatively (parser);
21654 type = cp_parser_class_name (parser,
21655 /*typename_keyword_p=*/false,
21656 /*template_keyword_p=*/false,
21657 class_type,
21658 /*check_dependency_p=*/false,
21659 /*class_head_p=*/true,
21660 /*is_declaration=*/false);
21661 /* If that didn't work, ignore the nested-name-specifier. */
21662 if (!cp_parser_parse_definitely (parser))
21664 invalid_nested_name_p = true;
21665 type_start_token = cp_lexer_peek_token (parser->lexer);
21666 id = cp_parser_identifier (parser);
21667 if (id == error_mark_node)
21668 id = NULL_TREE;
21670 /* If we could not find a corresponding TYPE, treat this
21671 declaration like an unqualified declaration. */
21672 if (type == error_mark_node)
21673 nested_name_specifier = NULL_TREE;
21674 /* Otherwise, count the number of templates used in TYPE and its
21675 containing scopes. */
21676 else
21678 tree scope;
21680 for (scope = TREE_TYPE (type);
21681 scope && TREE_CODE (scope) != NAMESPACE_DECL;
21682 scope = get_containing_scope (scope))
21683 if (TYPE_P (scope)
21684 && CLASS_TYPE_P (scope)
21685 && CLASSTYPE_TEMPLATE_INFO (scope)
21686 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
21687 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
21688 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
21689 ++num_templates;
21692 /* Otherwise, the identifier is optional. */
21693 else
21695 /* We don't know whether what comes next is a template-id,
21696 an identifier, or nothing at all. */
21697 cp_parser_parse_tentatively (parser);
21698 /* Check for a template-id. */
21699 type_start_token = cp_lexer_peek_token (parser->lexer);
21700 id = cp_parser_template_id (parser,
21701 /*template_keyword_p=*/false,
21702 /*check_dependency_p=*/true,
21703 class_key,
21704 /*is_declaration=*/true);
21705 /* If that didn't work, it could still be an identifier. */
21706 if (!cp_parser_parse_definitely (parser))
21708 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21710 type_start_token = cp_lexer_peek_token (parser->lexer);
21711 id = cp_parser_identifier (parser);
21713 else
21714 id = NULL_TREE;
21716 else
21718 template_id_p = true;
21719 ++num_templates;
21723 pop_deferring_access_checks ();
21725 if (id)
21727 cp_parser_check_for_invalid_template_id (parser, id,
21728 class_key,
21729 type_start_token->location);
21731 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
21733 /* If it's not a `:' or a `{' then we can't really be looking at a
21734 class-head, since a class-head only appears as part of a
21735 class-specifier. We have to detect this situation before calling
21736 xref_tag, since that has irreversible side-effects. */
21737 if (!cp_parser_next_token_starts_class_definition_p (parser))
21739 cp_parser_error (parser, "expected %<{%> or %<:%>");
21740 type = error_mark_node;
21741 goto out;
21744 /* At this point, we're going ahead with the class-specifier, even
21745 if some other problem occurs. */
21746 cp_parser_commit_to_tentative_parse (parser);
21747 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
21749 cp_parser_error (parser,
21750 "cannot specify %<override%> for a class");
21751 type = error_mark_node;
21752 goto out;
21754 /* Issue the error about the overly-qualified name now. */
21755 if (qualified_p)
21757 cp_parser_error (parser,
21758 "global qualification of class name is invalid");
21759 type = error_mark_node;
21760 goto out;
21762 else if (invalid_nested_name_p)
21764 cp_parser_error (parser,
21765 "qualified name does not name a class");
21766 type = error_mark_node;
21767 goto out;
21769 else if (nested_name_specifier)
21771 tree scope;
21773 /* Reject typedef-names in class heads. */
21774 if (!DECL_IMPLICIT_TYPEDEF_P (type))
21776 error_at (type_start_token->location,
21777 "invalid class name in declaration of %qD",
21778 type);
21779 type = NULL_TREE;
21780 goto done;
21783 /* Figure out in what scope the declaration is being placed. */
21784 scope = current_scope ();
21785 /* If that scope does not contain the scope in which the
21786 class was originally declared, the program is invalid. */
21787 if (scope && !is_ancestor (scope, nested_name_specifier))
21789 if (at_namespace_scope_p ())
21790 error_at (type_start_token->location,
21791 "declaration of %qD in namespace %qD which does not "
21792 "enclose %qD",
21793 type, scope, nested_name_specifier);
21794 else
21795 error_at (type_start_token->location,
21796 "declaration of %qD in %qD which does not enclose %qD",
21797 type, scope, nested_name_specifier);
21798 type = NULL_TREE;
21799 goto done;
21801 /* [dcl.meaning]
21803 A declarator-id shall not be qualified except for the
21804 definition of a ... nested class outside of its class
21805 ... [or] the definition or explicit instantiation of a
21806 class member of a namespace outside of its namespace. */
21807 if (scope == nested_name_specifier)
21809 permerror (nested_name_specifier_token_start->location,
21810 "extra qualification not allowed");
21811 nested_name_specifier = NULL_TREE;
21812 num_templates = 0;
21815 /* An explicit-specialization must be preceded by "template <>". If
21816 it is not, try to recover gracefully. */
21817 if (at_namespace_scope_p ()
21818 && parser->num_template_parameter_lists == 0
21819 && template_id_p)
21821 error_at (type_start_token->location,
21822 "an explicit specialization must be preceded by %<template <>%>");
21823 invalid_explicit_specialization_p = true;
21824 /* Take the same action that would have been taken by
21825 cp_parser_explicit_specialization. */
21826 ++parser->num_template_parameter_lists;
21827 begin_specialization ();
21829 /* There must be no "return" statements between this point and the
21830 end of this function; set "type "to the correct return value and
21831 use "goto done;" to return. */
21832 /* Make sure that the right number of template parameters were
21833 present. */
21834 if (!cp_parser_check_template_parameters (parser, num_templates,
21835 type_start_token->location,
21836 /*declarator=*/NULL))
21838 /* If something went wrong, there is no point in even trying to
21839 process the class-definition. */
21840 type = NULL_TREE;
21841 goto done;
21844 /* Look up the type. */
21845 if (template_id_p)
21847 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
21848 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
21849 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
21851 error_at (type_start_token->location,
21852 "function template %qD redeclared as a class template", id);
21853 type = error_mark_node;
21855 else
21857 type = TREE_TYPE (id);
21858 type = maybe_process_partial_specialization (type);
21860 if (nested_name_specifier)
21861 pushed_scope = push_scope (nested_name_specifier);
21863 else if (nested_name_specifier)
21865 tree class_type;
21867 /* Given:
21869 template <typename T> struct S { struct T };
21870 template <typename T> struct S<T>::T { };
21872 we will get a TYPENAME_TYPE when processing the definition of
21873 `S::T'. We need to resolve it to the actual type before we
21874 try to define it. */
21875 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
21877 class_type = resolve_typename_type (TREE_TYPE (type),
21878 /*only_current_p=*/false);
21879 if (TREE_CODE (class_type) != TYPENAME_TYPE)
21880 type = TYPE_NAME (class_type);
21881 else
21883 cp_parser_error (parser, "could not resolve typename type");
21884 type = error_mark_node;
21888 if (maybe_process_partial_specialization (TREE_TYPE (type))
21889 == error_mark_node)
21891 type = NULL_TREE;
21892 goto done;
21895 class_type = current_class_type;
21896 /* Enter the scope indicated by the nested-name-specifier. */
21897 pushed_scope = push_scope (nested_name_specifier);
21898 /* Get the canonical version of this type. */
21899 type = TYPE_MAIN_DECL (TREE_TYPE (type));
21900 /* Call push_template_decl if it seems like we should be defining a
21901 template either from the template headers or the type we're
21902 defining, so that we diagnose both extra and missing headers. */
21903 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
21904 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
21905 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
21907 type = push_template_decl (type);
21908 if (type == error_mark_node)
21910 type = NULL_TREE;
21911 goto done;
21915 type = TREE_TYPE (type);
21916 *nested_name_specifier_p = true;
21918 else /* The name is not a nested name. */
21920 /* If the class was unnamed, create a dummy name. */
21921 if (!id)
21922 id = make_anon_name ();
21923 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
21924 parser->num_template_parameter_lists);
21927 /* Indicate whether this class was declared as a `class' or as a
21928 `struct'. */
21929 if (TREE_CODE (type) == RECORD_TYPE)
21930 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
21931 cp_parser_check_class_key (class_key, type);
21933 /* If this type was already complete, and we see another definition,
21934 that's an error. */
21935 if (type != error_mark_node && COMPLETE_TYPE_P (type))
21937 error_at (type_start_token->location, "redefinition of %q#T",
21938 type);
21939 error_at (type_start_token->location, "previous definition of %q+#T",
21940 type);
21941 type = NULL_TREE;
21942 goto done;
21944 else if (type == error_mark_node)
21945 type = NULL_TREE;
21947 if (type)
21949 /* Apply attributes now, before any use of the class as a template
21950 argument in its base list. */
21951 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
21952 fixup_attribute_variants (type);
21955 /* We will have entered the scope containing the class; the names of
21956 base classes should be looked up in that context. For example:
21958 struct A { struct B {}; struct C; };
21959 struct A::C : B {};
21961 is valid. */
21963 /* Get the list of base-classes, if there is one. */
21964 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21966 /* PR59482: enter the class scope so that base-specifiers are looked
21967 up correctly. */
21968 if (type)
21969 pushclass (type);
21970 bases = cp_parser_base_clause (parser);
21971 /* PR59482: get out of the previously pushed class scope so that the
21972 subsequent pops pop the right thing. */
21973 if (type)
21974 popclass ();
21976 else
21977 bases = NULL_TREE;
21979 /* If we're really defining a class, process the base classes.
21980 If they're invalid, fail. */
21981 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
21982 && !xref_basetypes (type, bases))
21983 type = NULL_TREE;
21985 done:
21986 /* Leave the scope given by the nested-name-specifier. We will
21987 enter the class scope itself while processing the members. */
21988 if (pushed_scope)
21989 pop_scope (pushed_scope);
21991 if (invalid_explicit_specialization_p)
21993 end_specialization ();
21994 --parser->num_template_parameter_lists;
21997 if (type)
21998 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
21999 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
22000 CLASSTYPE_FINAL (type) = 1;
22001 out:
22002 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22003 return type;
22006 /* Parse a class-key.
22008 class-key:
22009 class
22010 struct
22011 union
22013 Returns the kind of class-key specified, or none_type to indicate
22014 error. */
22016 static enum tag_types
22017 cp_parser_class_key (cp_parser* parser)
22019 cp_token *token;
22020 enum tag_types tag_type;
22022 /* Look for the class-key. */
22023 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
22024 if (!token)
22025 return none_type;
22027 /* Check to see if the TOKEN is a class-key. */
22028 tag_type = cp_parser_token_is_class_key (token);
22029 if (!tag_type)
22030 cp_parser_error (parser, "expected class-key");
22031 return tag_type;
22034 /* Parse a type-parameter-key.
22036 type-parameter-key:
22037 class
22038 typename
22041 static void
22042 cp_parser_type_parameter_key (cp_parser* parser)
22044 /* Look for the type-parameter-key. */
22045 enum tag_types tag_type = none_type;
22046 cp_token *token = cp_lexer_peek_token (parser->lexer);
22047 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
22049 cp_lexer_consume_token (parser->lexer);
22050 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
22051 /* typename is not allowed in a template template parameter
22052 by the standard until C++1Z. */
22053 pedwarn (token->location, OPT_Wpedantic,
22054 "ISO C++ forbids typename key in template template parameter;"
22055 " use -std=c++1z or -std=gnu++1z");
22057 else
22058 cp_parser_error (parser, "expected %<class%> or %<typename%>");
22060 return;
22063 /* Parse an (optional) member-specification.
22065 member-specification:
22066 member-declaration member-specification [opt]
22067 access-specifier : member-specification [opt] */
22069 static void
22070 cp_parser_member_specification_opt (cp_parser* parser)
22072 while (true)
22074 cp_token *token;
22075 enum rid keyword;
22077 /* Peek at the next token. */
22078 token = cp_lexer_peek_token (parser->lexer);
22079 /* If it's a `}', or EOF then we've seen all the members. */
22080 if (token->type == CPP_CLOSE_BRACE
22081 || token->type == CPP_EOF
22082 || token->type == CPP_PRAGMA_EOL)
22083 break;
22085 /* See if this token is a keyword. */
22086 keyword = token->keyword;
22087 switch (keyword)
22089 case RID_PUBLIC:
22090 case RID_PROTECTED:
22091 case RID_PRIVATE:
22092 /* Consume the access-specifier. */
22093 cp_lexer_consume_token (parser->lexer);
22094 /* Remember which access-specifier is active. */
22095 current_access_specifier = token->u.value;
22096 /* Look for the `:'. */
22097 cp_parser_require (parser, CPP_COLON, RT_COLON);
22098 break;
22100 default:
22101 /* Accept #pragmas at class scope. */
22102 if (token->type == CPP_PRAGMA)
22104 cp_parser_pragma (parser, pragma_member);
22105 break;
22108 /* Otherwise, the next construction must be a
22109 member-declaration. */
22110 cp_parser_member_declaration (parser);
22115 /* Parse a member-declaration.
22117 member-declaration:
22118 decl-specifier-seq [opt] member-declarator-list [opt] ;
22119 function-definition ; [opt]
22120 :: [opt] nested-name-specifier template [opt] unqualified-id ;
22121 using-declaration
22122 template-declaration
22123 alias-declaration
22125 member-declarator-list:
22126 member-declarator
22127 member-declarator-list , member-declarator
22129 member-declarator:
22130 declarator pure-specifier [opt]
22131 declarator constant-initializer [opt]
22132 identifier [opt] : constant-expression
22134 GNU Extensions:
22136 member-declaration:
22137 __extension__ member-declaration
22139 member-declarator:
22140 declarator attributes [opt] pure-specifier [opt]
22141 declarator attributes [opt] constant-initializer [opt]
22142 identifier [opt] attributes [opt] : constant-expression
22144 C++0x Extensions:
22146 member-declaration:
22147 static_assert-declaration */
22149 static void
22150 cp_parser_member_declaration (cp_parser* parser)
22152 cp_decl_specifier_seq decl_specifiers;
22153 tree prefix_attributes;
22154 tree decl;
22155 int declares_class_or_enum;
22156 bool friend_p;
22157 cp_token *token = NULL;
22158 cp_token *decl_spec_token_start = NULL;
22159 cp_token *initializer_token_start = NULL;
22160 int saved_pedantic;
22161 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22163 /* Check for the `__extension__' keyword. */
22164 if (cp_parser_extension_opt (parser, &saved_pedantic))
22166 /* Recurse. */
22167 cp_parser_member_declaration (parser);
22168 /* Restore the old value of the PEDANTIC flag. */
22169 pedantic = saved_pedantic;
22171 return;
22174 /* Check for a template-declaration. */
22175 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22177 /* An explicit specialization here is an error condition, and we
22178 expect the specialization handler to detect and report this. */
22179 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
22180 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
22181 cp_parser_explicit_specialization (parser);
22182 else
22183 cp_parser_template_declaration (parser, /*member_p=*/true);
22185 return;
22187 /* Check for a template introduction. */
22188 else if (cp_parser_template_declaration_after_export (parser, true))
22189 return;
22191 /* Check for a using-declaration. */
22192 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
22194 if (cxx_dialect < cxx11)
22196 /* Parse the using-declaration. */
22197 cp_parser_using_declaration (parser,
22198 /*access_declaration_p=*/false);
22199 return;
22201 else
22203 tree decl;
22204 bool alias_decl_expected;
22205 cp_parser_parse_tentatively (parser);
22206 decl = cp_parser_alias_declaration (parser);
22207 /* Note that if we actually see the '=' token after the
22208 identifier, cp_parser_alias_declaration commits the
22209 tentative parse. In that case, we really expect an
22210 alias-declaration. Otherwise, we expect a using
22211 declaration. */
22212 alias_decl_expected =
22213 !cp_parser_uncommitted_to_tentative_parse_p (parser);
22214 cp_parser_parse_definitely (parser);
22216 if (alias_decl_expected)
22217 finish_member_declaration (decl);
22218 else
22219 cp_parser_using_declaration (parser,
22220 /*access_declaration_p=*/false);
22221 return;
22225 /* Check for @defs. */
22226 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
22228 tree ivar, member;
22229 tree ivar_chains = cp_parser_objc_defs_expression (parser);
22230 ivar = ivar_chains;
22231 while (ivar)
22233 member = ivar;
22234 ivar = TREE_CHAIN (member);
22235 TREE_CHAIN (member) = NULL_TREE;
22236 finish_member_declaration (member);
22238 return;
22241 /* If the next token is `static_assert' we have a static assertion. */
22242 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
22244 cp_parser_static_assert (parser, /*member_p=*/true);
22245 return;
22248 parser->colon_corrects_to_scope_p = false;
22250 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
22251 goto out;
22253 /* Parse the decl-specifier-seq. */
22254 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22255 cp_parser_decl_specifier_seq (parser,
22256 CP_PARSER_FLAGS_OPTIONAL,
22257 &decl_specifiers,
22258 &declares_class_or_enum);
22259 /* Check for an invalid type-name. */
22260 if (!decl_specifiers.any_type_specifiers_p
22261 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22262 goto out;
22263 /* If there is no declarator, then the decl-specifier-seq should
22264 specify a type. */
22265 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22267 /* If there was no decl-specifier-seq, and the next token is a
22268 `;', then we have something like:
22270 struct S { ; };
22272 [class.mem]
22274 Each member-declaration shall declare at least one member
22275 name of the class. */
22276 if (!decl_specifiers.any_specifiers_p)
22278 cp_token *token = cp_lexer_peek_token (parser->lexer);
22279 if (!in_system_header_at (token->location))
22280 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
22282 else
22284 tree type;
22286 /* See if this declaration is a friend. */
22287 friend_p = cp_parser_friend_p (&decl_specifiers);
22288 /* If there were decl-specifiers, check to see if there was
22289 a class-declaration. */
22290 type = check_tag_decl (&decl_specifiers,
22291 /*explicit_type_instantiation_p=*/false);
22292 /* Nested classes have already been added to the class, but
22293 a `friend' needs to be explicitly registered. */
22294 if (friend_p)
22296 /* If the `friend' keyword was present, the friend must
22297 be introduced with a class-key. */
22298 if (!declares_class_or_enum && cxx_dialect < cxx11)
22299 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
22300 "in C++03 a class-key must be used "
22301 "when declaring a friend");
22302 /* In this case:
22304 template <typename T> struct A {
22305 friend struct A<T>::B;
22308 A<T>::B will be represented by a TYPENAME_TYPE, and
22309 therefore not recognized by check_tag_decl. */
22310 if (!type)
22312 type = decl_specifiers.type;
22313 if (type && TREE_CODE (type) == TYPE_DECL)
22314 type = TREE_TYPE (type);
22316 if (!type || !TYPE_P (type))
22317 error_at (decl_spec_token_start->location,
22318 "friend declaration does not name a class or "
22319 "function");
22320 else
22321 make_friend_class (current_class_type, type,
22322 /*complain=*/true);
22324 /* If there is no TYPE, an error message will already have
22325 been issued. */
22326 else if (!type || type == error_mark_node)
22328 /* An anonymous aggregate has to be handled specially; such
22329 a declaration really declares a data member (with a
22330 particular type), as opposed to a nested class. */
22331 else if (ANON_AGGR_TYPE_P (type))
22333 /* C++11 9.5/6. */
22334 if (decl_specifiers.storage_class != sc_none)
22335 error_at (decl_spec_token_start->location,
22336 "a storage class on an anonymous aggregate "
22337 "in class scope is not allowed");
22339 /* Remove constructors and such from TYPE, now that we
22340 know it is an anonymous aggregate. */
22341 fixup_anonymous_aggr (type);
22342 /* And make the corresponding data member. */
22343 decl = build_decl (decl_spec_token_start->location,
22344 FIELD_DECL, NULL_TREE, type);
22345 /* Add it to the class. */
22346 finish_member_declaration (decl);
22348 else
22349 cp_parser_check_access_in_redeclaration
22350 (TYPE_NAME (type),
22351 decl_spec_token_start->location);
22354 else
22356 bool assume_semicolon = false;
22358 /* Clear attributes from the decl_specifiers but keep them
22359 around as prefix attributes that apply them to the entity
22360 being declared. */
22361 prefix_attributes = decl_specifiers.attributes;
22362 decl_specifiers.attributes = NULL_TREE;
22364 /* See if these declarations will be friends. */
22365 friend_p = cp_parser_friend_p (&decl_specifiers);
22367 /* Keep going until we hit the `;' at the end of the
22368 declaration. */
22369 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22371 tree attributes = NULL_TREE;
22372 tree first_attribute;
22374 /* Peek at the next token. */
22375 token = cp_lexer_peek_token (parser->lexer);
22377 /* Check for a bitfield declaration. */
22378 if (token->type == CPP_COLON
22379 || (token->type == CPP_NAME
22380 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
22381 == CPP_COLON))
22383 tree identifier;
22384 tree width;
22386 /* Get the name of the bitfield. Note that we cannot just
22387 check TOKEN here because it may have been invalidated by
22388 the call to cp_lexer_peek_nth_token above. */
22389 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
22390 identifier = cp_parser_identifier (parser);
22391 else
22392 identifier = NULL_TREE;
22394 /* Consume the `:' token. */
22395 cp_lexer_consume_token (parser->lexer);
22396 /* Get the width of the bitfield. */
22397 width
22398 = cp_parser_constant_expression (parser);
22400 /* Look for attributes that apply to the bitfield. */
22401 attributes = cp_parser_attributes_opt (parser);
22402 /* Remember which attributes are prefix attributes and
22403 which are not. */
22404 first_attribute = attributes;
22405 /* Combine the attributes. */
22406 attributes = chainon (prefix_attributes, attributes);
22408 /* Create the bitfield declaration. */
22409 decl = grokbitfield (identifier
22410 ? make_id_declarator (NULL_TREE,
22411 identifier,
22412 sfk_none)
22413 : NULL,
22414 &decl_specifiers,
22415 width,
22416 attributes);
22418 else
22420 cp_declarator *declarator;
22421 tree initializer;
22422 tree asm_specification;
22423 int ctor_dtor_or_conv_p;
22425 /* Parse the declarator. */
22426 declarator
22427 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22428 &ctor_dtor_or_conv_p,
22429 /*parenthesized_p=*/NULL,
22430 /*member_p=*/true,
22431 friend_p);
22433 /* If something went wrong parsing the declarator, make sure
22434 that we at least consume some tokens. */
22435 if (declarator == cp_error_declarator)
22437 /* Skip to the end of the statement. */
22438 cp_parser_skip_to_end_of_statement (parser);
22439 /* If the next token is not a semicolon, that is
22440 probably because we just skipped over the body of
22441 a function. So, we consume a semicolon if
22442 present, but do not issue an error message if it
22443 is not present. */
22444 if (cp_lexer_next_token_is (parser->lexer,
22445 CPP_SEMICOLON))
22446 cp_lexer_consume_token (parser->lexer);
22447 goto out;
22450 if (declares_class_or_enum & 2)
22451 cp_parser_check_for_definition_in_return_type
22452 (declarator, decl_specifiers.type,
22453 decl_specifiers.locations[ds_type_spec]);
22455 /* Look for an asm-specification. */
22456 asm_specification = cp_parser_asm_specification_opt (parser);
22457 /* Look for attributes that apply to the declaration. */
22458 attributes = cp_parser_attributes_opt (parser);
22459 /* Remember which attributes are prefix attributes and
22460 which are not. */
22461 first_attribute = attributes;
22462 /* Combine the attributes. */
22463 attributes = chainon (prefix_attributes, attributes);
22465 /* If it's an `=', then we have a constant-initializer or a
22466 pure-specifier. It is not correct to parse the
22467 initializer before registering the member declaration
22468 since the member declaration should be in scope while
22469 its initializer is processed. However, the rest of the
22470 front end does not yet provide an interface that allows
22471 us to handle this correctly. */
22472 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22474 /* In [class.mem]:
22476 A pure-specifier shall be used only in the declaration of
22477 a virtual function.
22479 A member-declarator can contain a constant-initializer
22480 only if it declares a static member of integral or
22481 enumeration type.
22483 Therefore, if the DECLARATOR is for a function, we look
22484 for a pure-specifier; otherwise, we look for a
22485 constant-initializer. When we call `grokfield', it will
22486 perform more stringent semantics checks. */
22487 initializer_token_start = cp_lexer_peek_token (parser->lexer);
22488 if (function_declarator_p (declarator)
22489 || (decl_specifiers.type
22490 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
22491 && declarator->kind == cdk_id
22492 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
22493 == FUNCTION_TYPE)))
22494 initializer = cp_parser_pure_specifier (parser);
22495 else if (decl_specifiers.storage_class != sc_static)
22496 initializer = cp_parser_save_nsdmi (parser);
22497 else if (cxx_dialect >= cxx11)
22499 bool nonconst;
22500 /* Don't require a constant rvalue in C++11, since we
22501 might want a reference constant. We'll enforce
22502 constancy later. */
22503 cp_lexer_consume_token (parser->lexer);
22504 /* Parse the initializer. */
22505 initializer = cp_parser_initializer_clause (parser,
22506 &nonconst);
22508 else
22509 /* Parse the initializer. */
22510 initializer = cp_parser_constant_initializer (parser);
22512 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
22513 && !function_declarator_p (declarator))
22515 bool x;
22516 if (decl_specifiers.storage_class != sc_static)
22517 initializer = cp_parser_save_nsdmi (parser);
22518 else
22519 initializer = cp_parser_initializer (parser, &x, &x);
22521 /* Otherwise, there is no initializer. */
22522 else
22523 initializer = NULL_TREE;
22525 /* See if we are probably looking at a function
22526 definition. We are certainly not looking at a
22527 member-declarator. Calling `grokfield' has
22528 side-effects, so we must not do it unless we are sure
22529 that we are looking at a member-declarator. */
22530 if (cp_parser_token_starts_function_definition_p
22531 (cp_lexer_peek_token (parser->lexer)))
22533 /* The grammar does not allow a pure-specifier to be
22534 used when a member function is defined. (It is
22535 possible that this fact is an oversight in the
22536 standard, since a pure function may be defined
22537 outside of the class-specifier. */
22538 if (initializer && initializer_token_start)
22539 error_at (initializer_token_start->location,
22540 "pure-specifier on function-definition");
22541 decl = cp_parser_save_member_function_body (parser,
22542 &decl_specifiers,
22543 declarator,
22544 attributes);
22545 if (parser->fully_implicit_function_template_p)
22546 decl = finish_fully_implicit_template (parser, decl);
22547 /* If the member was not a friend, declare it here. */
22548 if (!friend_p)
22549 finish_member_declaration (decl);
22550 /* Peek at the next token. */
22551 token = cp_lexer_peek_token (parser->lexer);
22552 /* If the next token is a semicolon, consume it. */
22553 if (token->type == CPP_SEMICOLON)
22554 cp_lexer_consume_token (parser->lexer);
22555 goto out;
22557 else
22558 if (declarator->kind == cdk_function)
22559 declarator->id_loc = token->location;
22560 /* Create the declaration. */
22561 decl = grokfield (declarator, &decl_specifiers,
22562 initializer, /*init_const_expr_p=*/true,
22563 asm_specification, attributes);
22564 if (parser->fully_implicit_function_template_p)
22566 if (friend_p)
22567 finish_fully_implicit_template (parser, 0);
22568 else
22569 decl = finish_fully_implicit_template (parser, decl);
22573 cp_finalize_omp_declare_simd (parser, decl);
22574 cp_finalize_oacc_routine (parser, decl, false);
22576 /* Reset PREFIX_ATTRIBUTES. */
22577 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22578 attributes = TREE_CHAIN (attributes);
22579 if (attributes)
22580 TREE_CHAIN (attributes) = NULL_TREE;
22582 /* If there is any qualification still in effect, clear it
22583 now; we will be starting fresh with the next declarator. */
22584 parser->scope = NULL_TREE;
22585 parser->qualifying_scope = NULL_TREE;
22586 parser->object_scope = NULL_TREE;
22587 /* If it's a `,', then there are more declarators. */
22588 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22590 cp_lexer_consume_token (parser->lexer);
22591 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22593 cp_token *token = cp_lexer_previous_token (parser->lexer);
22594 error_at (token->location,
22595 "stray %<,%> at end of member declaration");
22598 /* If the next token isn't a `;', then we have a parse error. */
22599 else if (cp_lexer_next_token_is_not (parser->lexer,
22600 CPP_SEMICOLON))
22602 /* The next token might be a ways away from where the
22603 actual semicolon is missing. Find the previous token
22604 and use that for our error position. */
22605 cp_token *token = cp_lexer_previous_token (parser->lexer);
22606 error_at (token->location,
22607 "expected %<;%> at end of member declaration");
22609 /* Assume that the user meant to provide a semicolon. If
22610 we were to cp_parser_skip_to_end_of_statement, we might
22611 skip to a semicolon inside a member function definition
22612 and issue nonsensical error messages. */
22613 assume_semicolon = true;
22616 if (decl)
22618 /* Add DECL to the list of members. */
22619 if (!friend_p
22620 /* Explicitly include, eg, NSDMIs, for better error
22621 recovery (c++/58650). */
22622 || !DECL_DECLARES_FUNCTION_P (decl))
22623 finish_member_declaration (decl);
22625 if (TREE_CODE (decl) == FUNCTION_DECL)
22626 cp_parser_save_default_args (parser, decl);
22627 else if (TREE_CODE (decl) == FIELD_DECL
22628 && !DECL_C_BIT_FIELD (decl)
22629 && DECL_INITIAL (decl))
22630 /* Add DECL to the queue of NSDMI to be parsed later. */
22631 vec_safe_push (unparsed_nsdmis, decl);
22634 if (assume_semicolon)
22635 goto out;
22639 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22640 out:
22641 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22644 /* Parse a pure-specifier.
22646 pure-specifier:
22649 Returns INTEGER_ZERO_NODE if a pure specifier is found.
22650 Otherwise, ERROR_MARK_NODE is returned. */
22652 static tree
22653 cp_parser_pure_specifier (cp_parser* parser)
22655 cp_token *token;
22657 /* Look for the `=' token. */
22658 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
22659 return error_mark_node;
22660 /* Look for the `0' token. */
22661 token = cp_lexer_peek_token (parser->lexer);
22663 if (token->type == CPP_EOF
22664 || token->type == CPP_PRAGMA_EOL)
22665 return error_mark_node;
22667 cp_lexer_consume_token (parser->lexer);
22669 /* Accept = default or = delete in c++0x mode. */
22670 if (token->keyword == RID_DEFAULT
22671 || token->keyword == RID_DELETE)
22673 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
22674 return token->u.value;
22677 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
22678 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
22680 cp_parser_error (parser,
22681 "invalid pure specifier (only %<= 0%> is allowed)");
22682 cp_parser_skip_to_end_of_statement (parser);
22683 return error_mark_node;
22685 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
22687 error_at (token->location, "templates may not be %<virtual%>");
22688 return error_mark_node;
22691 return integer_zero_node;
22694 /* Parse a constant-initializer.
22696 constant-initializer:
22697 = constant-expression
22699 Returns a representation of the constant-expression. */
22701 static tree
22702 cp_parser_constant_initializer (cp_parser* parser)
22704 /* Look for the `=' token. */
22705 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
22706 return error_mark_node;
22708 /* It is invalid to write:
22710 struct S { static const int i = { 7 }; };
22713 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22715 cp_parser_error (parser,
22716 "a brace-enclosed initializer is not allowed here");
22717 /* Consume the opening brace. */
22718 cp_lexer_consume_token (parser->lexer);
22719 /* Skip the initializer. */
22720 cp_parser_skip_to_closing_brace (parser);
22721 /* Look for the trailing `}'. */
22722 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
22724 return error_mark_node;
22727 return cp_parser_constant_expression (parser);
22730 /* Derived classes [gram.class.derived] */
22732 /* Parse a base-clause.
22734 base-clause:
22735 : base-specifier-list
22737 base-specifier-list:
22738 base-specifier ... [opt]
22739 base-specifier-list , base-specifier ... [opt]
22741 Returns a TREE_LIST representing the base-classes, in the order in
22742 which they were declared. The representation of each node is as
22743 described by cp_parser_base_specifier.
22745 In the case that no bases are specified, this function will return
22746 NULL_TREE, not ERROR_MARK_NODE. */
22748 static tree
22749 cp_parser_base_clause (cp_parser* parser)
22751 tree bases = NULL_TREE;
22753 /* Look for the `:' that begins the list. */
22754 cp_parser_require (parser, CPP_COLON, RT_COLON);
22756 /* Scan the base-specifier-list. */
22757 while (true)
22759 cp_token *token;
22760 tree base;
22761 bool pack_expansion_p = false;
22763 /* Look for the base-specifier. */
22764 base = cp_parser_base_specifier (parser);
22765 /* Look for the (optional) ellipsis. */
22766 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22768 /* Consume the `...'. */
22769 cp_lexer_consume_token (parser->lexer);
22771 pack_expansion_p = true;
22774 /* Add BASE to the front of the list. */
22775 if (base && base != error_mark_node)
22777 if (pack_expansion_p)
22778 /* Make this a pack expansion type. */
22779 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
22781 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
22783 TREE_CHAIN (base) = bases;
22784 bases = base;
22787 /* Peek at the next token. */
22788 token = cp_lexer_peek_token (parser->lexer);
22789 /* If it's not a comma, then the list is complete. */
22790 if (token->type != CPP_COMMA)
22791 break;
22792 /* Consume the `,'. */
22793 cp_lexer_consume_token (parser->lexer);
22796 /* PARSER->SCOPE may still be non-NULL at this point, if the last
22797 base class had a qualified name. However, the next name that
22798 appears is certainly not qualified. */
22799 parser->scope = NULL_TREE;
22800 parser->qualifying_scope = NULL_TREE;
22801 parser->object_scope = NULL_TREE;
22803 return nreverse (bases);
22806 /* Parse a base-specifier.
22808 base-specifier:
22809 :: [opt] nested-name-specifier [opt] class-name
22810 virtual access-specifier [opt] :: [opt] nested-name-specifier
22811 [opt] class-name
22812 access-specifier virtual [opt] :: [opt] nested-name-specifier
22813 [opt] class-name
22815 Returns a TREE_LIST. The TREE_PURPOSE will be one of
22816 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
22817 indicate the specifiers provided. The TREE_VALUE will be a TYPE
22818 (or the ERROR_MARK_NODE) indicating the type that was specified. */
22820 static tree
22821 cp_parser_base_specifier (cp_parser* parser)
22823 cp_token *token;
22824 bool done = false;
22825 bool virtual_p = false;
22826 bool duplicate_virtual_error_issued_p = false;
22827 bool duplicate_access_error_issued_p = false;
22828 bool class_scope_p, template_p;
22829 tree access = access_default_node;
22830 tree type;
22832 /* Process the optional `virtual' and `access-specifier'. */
22833 while (!done)
22835 /* Peek at the next token. */
22836 token = cp_lexer_peek_token (parser->lexer);
22837 /* Process `virtual'. */
22838 switch (token->keyword)
22840 case RID_VIRTUAL:
22841 /* If `virtual' appears more than once, issue an error. */
22842 if (virtual_p && !duplicate_virtual_error_issued_p)
22844 cp_parser_error (parser,
22845 "%<virtual%> specified more than once in base-specified");
22846 duplicate_virtual_error_issued_p = true;
22849 virtual_p = true;
22851 /* Consume the `virtual' token. */
22852 cp_lexer_consume_token (parser->lexer);
22854 break;
22856 case RID_PUBLIC:
22857 case RID_PROTECTED:
22858 case RID_PRIVATE:
22859 /* If more than one access specifier appears, issue an
22860 error. */
22861 if (access != access_default_node
22862 && !duplicate_access_error_issued_p)
22864 cp_parser_error (parser,
22865 "more than one access specifier in base-specified");
22866 duplicate_access_error_issued_p = true;
22869 access = ridpointers[(int) token->keyword];
22871 /* Consume the access-specifier. */
22872 cp_lexer_consume_token (parser->lexer);
22874 break;
22876 default:
22877 done = true;
22878 break;
22881 /* It is not uncommon to see programs mechanically, erroneously, use
22882 the 'typename' keyword to denote (dependent) qualified types
22883 as base classes. */
22884 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
22886 token = cp_lexer_peek_token (parser->lexer);
22887 if (!processing_template_decl)
22888 error_at (token->location,
22889 "keyword %<typename%> not allowed outside of templates");
22890 else
22891 error_at (token->location,
22892 "keyword %<typename%> not allowed in this context "
22893 "(the base class is implicitly a type)");
22894 cp_lexer_consume_token (parser->lexer);
22897 /* Look for the optional `::' operator. */
22898 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
22899 /* Look for the nested-name-specifier. The simplest way to
22900 implement:
22902 [temp.res]
22904 The keyword `typename' is not permitted in a base-specifier or
22905 mem-initializer; in these contexts a qualified name that
22906 depends on a template-parameter is implicitly assumed to be a
22907 type name.
22909 is to pretend that we have seen the `typename' keyword at this
22910 point. */
22911 cp_parser_nested_name_specifier_opt (parser,
22912 /*typename_keyword_p=*/true,
22913 /*check_dependency_p=*/true,
22914 typename_type,
22915 /*is_declaration=*/true);
22916 /* If the base class is given by a qualified name, assume that names
22917 we see are type names or templates, as appropriate. */
22918 class_scope_p = (parser->scope && TYPE_P (parser->scope));
22919 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
22921 if (!parser->scope
22922 && cp_lexer_next_token_is_decltype (parser->lexer))
22923 /* DR 950 allows decltype as a base-specifier. */
22924 type = cp_parser_decltype (parser);
22925 else
22927 /* Otherwise, look for the class-name. */
22928 type = cp_parser_class_name (parser,
22929 class_scope_p,
22930 template_p,
22931 typename_type,
22932 /*check_dependency_p=*/true,
22933 /*class_head_p=*/false,
22934 /*is_declaration=*/true);
22935 type = TREE_TYPE (type);
22938 if (type == error_mark_node)
22939 return error_mark_node;
22941 return finish_base_specifier (type, access, virtual_p);
22944 /* Exception handling [gram.exception] */
22946 /* Parse an (optional) noexcept-specification.
22948 noexcept-specification:
22949 noexcept ( constant-expression ) [opt]
22951 If no noexcept-specification is present, returns NULL_TREE.
22952 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
22953 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
22954 there are no parentheses. CONSUMED_EXPR will be set accordingly.
22955 Otherwise, returns a noexcept specification unless RETURN_COND is true,
22956 in which case a boolean condition is returned instead. */
22958 static tree
22959 cp_parser_noexcept_specification_opt (cp_parser* parser,
22960 bool require_constexpr,
22961 bool* consumed_expr,
22962 bool return_cond)
22964 cp_token *token;
22965 const char *saved_message;
22967 /* Peek at the next token. */
22968 token = cp_lexer_peek_token (parser->lexer);
22970 /* Is it a noexcept-specification? */
22971 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
22973 tree expr;
22974 cp_lexer_consume_token (parser->lexer);
22976 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
22978 cp_lexer_consume_token (parser->lexer);
22980 if (require_constexpr)
22982 /* Types may not be defined in an exception-specification. */
22983 saved_message = parser->type_definition_forbidden_message;
22984 parser->type_definition_forbidden_message
22985 = G_("types may not be defined in an exception-specification");
22987 expr = cp_parser_constant_expression (parser);
22989 /* Restore the saved message. */
22990 parser->type_definition_forbidden_message = saved_message;
22992 else
22994 expr = cp_parser_expression (parser);
22995 *consumed_expr = true;
22998 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23000 else
23002 expr = boolean_true_node;
23003 if (!require_constexpr)
23004 *consumed_expr = false;
23007 /* We cannot build a noexcept-spec right away because this will check
23008 that expr is a constexpr. */
23009 if (!return_cond)
23010 return build_noexcept_spec (expr, tf_warning_or_error);
23011 else
23012 return expr;
23014 else
23015 return NULL_TREE;
23018 /* Parse an (optional) exception-specification.
23020 exception-specification:
23021 throw ( type-id-list [opt] )
23023 Returns a TREE_LIST representing the exception-specification. The
23024 TREE_VALUE of each node is a type. */
23026 static tree
23027 cp_parser_exception_specification_opt (cp_parser* parser)
23029 cp_token *token;
23030 tree type_id_list;
23031 const char *saved_message;
23033 /* Peek at the next token. */
23034 token = cp_lexer_peek_token (parser->lexer);
23036 /* Is it a noexcept-specification? */
23037 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
23038 false);
23039 if (type_id_list != NULL_TREE)
23040 return type_id_list;
23042 /* If it's not `throw', then there's no exception-specification. */
23043 if (!cp_parser_is_keyword (token, RID_THROW))
23044 return NULL_TREE;
23046 #if 0
23047 /* Enable this once a lot of code has transitioned to noexcept? */
23048 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
23049 warning (OPT_Wdeprecated, "dynamic exception specifications are "
23050 "deprecated in C++0x; use %<noexcept%> instead");
23051 #endif
23053 /* Consume the `throw'. */
23054 cp_lexer_consume_token (parser->lexer);
23056 /* Look for the `('. */
23057 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23059 /* Peek at the next token. */
23060 token = cp_lexer_peek_token (parser->lexer);
23061 /* If it's not a `)', then there is a type-id-list. */
23062 if (token->type != CPP_CLOSE_PAREN)
23064 /* Types may not be defined in an exception-specification. */
23065 saved_message = parser->type_definition_forbidden_message;
23066 parser->type_definition_forbidden_message
23067 = G_("types may not be defined in an exception-specification");
23068 /* Parse the type-id-list. */
23069 type_id_list = cp_parser_type_id_list (parser);
23070 /* Restore the saved message. */
23071 parser->type_definition_forbidden_message = saved_message;
23073 else
23074 type_id_list = empty_except_spec;
23076 /* Look for the `)'. */
23077 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23079 return type_id_list;
23082 /* Parse an (optional) type-id-list.
23084 type-id-list:
23085 type-id ... [opt]
23086 type-id-list , type-id ... [opt]
23088 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
23089 in the order that the types were presented. */
23091 static tree
23092 cp_parser_type_id_list (cp_parser* parser)
23094 tree types = NULL_TREE;
23096 while (true)
23098 cp_token *token;
23099 tree type;
23101 token = cp_lexer_peek_token (parser->lexer);
23103 /* Get the next type-id. */
23104 type = cp_parser_type_id (parser);
23105 /* Check for invalid 'auto'. */
23106 if (flag_concepts && type_uses_auto (type))
23108 error_at (token->location,
23109 "invalid use of %<auto%> in exception-specification");
23110 type = error_mark_node;
23112 /* Parse the optional ellipsis. */
23113 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23115 /* Consume the `...'. */
23116 cp_lexer_consume_token (parser->lexer);
23118 /* Turn the type into a pack expansion expression. */
23119 type = make_pack_expansion (type);
23121 /* Add it to the list. */
23122 types = add_exception_specifier (types, type, /*complain=*/1);
23123 /* Peek at the next token. */
23124 token = cp_lexer_peek_token (parser->lexer);
23125 /* If it is not a `,', we are done. */
23126 if (token->type != CPP_COMMA)
23127 break;
23128 /* Consume the `,'. */
23129 cp_lexer_consume_token (parser->lexer);
23132 return nreverse (types);
23135 /* Parse a try-block.
23137 try-block:
23138 try compound-statement handler-seq */
23140 static tree
23141 cp_parser_try_block (cp_parser* parser)
23143 tree try_block;
23145 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
23146 if (parser->in_function_body
23147 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
23148 error ("%<try%> in %<constexpr%> function");
23150 try_block = begin_try_block ();
23151 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
23152 finish_try_block (try_block);
23153 cp_parser_handler_seq (parser);
23154 finish_handler_sequence (try_block);
23156 return try_block;
23159 /* Parse a function-try-block.
23161 function-try-block:
23162 try ctor-initializer [opt] function-body handler-seq */
23164 static bool
23165 cp_parser_function_try_block (cp_parser* parser)
23167 tree compound_stmt;
23168 tree try_block;
23169 bool ctor_initializer_p;
23171 /* Look for the `try' keyword. */
23172 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
23173 return false;
23174 /* Let the rest of the front end know where we are. */
23175 try_block = begin_function_try_block (&compound_stmt);
23176 /* Parse the function-body. */
23177 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23178 (parser, /*in_function_try_block=*/true);
23179 /* We're done with the `try' part. */
23180 finish_function_try_block (try_block);
23181 /* Parse the handlers. */
23182 cp_parser_handler_seq (parser);
23183 /* We're done with the handlers. */
23184 finish_function_handler_sequence (try_block, compound_stmt);
23186 return ctor_initializer_p;
23189 /* Parse a handler-seq.
23191 handler-seq:
23192 handler handler-seq [opt] */
23194 static void
23195 cp_parser_handler_seq (cp_parser* parser)
23197 while (true)
23199 cp_token *token;
23201 /* Parse the handler. */
23202 cp_parser_handler (parser);
23203 /* Peek at the next token. */
23204 token = cp_lexer_peek_token (parser->lexer);
23205 /* If it's not `catch' then there are no more handlers. */
23206 if (!cp_parser_is_keyword (token, RID_CATCH))
23207 break;
23211 /* Parse a handler.
23213 handler:
23214 catch ( exception-declaration ) compound-statement */
23216 static void
23217 cp_parser_handler (cp_parser* parser)
23219 tree handler;
23220 tree declaration;
23222 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
23223 handler = begin_handler ();
23224 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23225 declaration = cp_parser_exception_declaration (parser);
23226 finish_handler_parms (declaration, handler);
23227 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23228 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
23229 finish_handler (handler);
23232 /* Parse an exception-declaration.
23234 exception-declaration:
23235 type-specifier-seq declarator
23236 type-specifier-seq abstract-declarator
23237 type-specifier-seq
23240 Returns a VAR_DECL for the declaration, or NULL_TREE if the
23241 ellipsis variant is used. */
23243 static tree
23244 cp_parser_exception_declaration (cp_parser* parser)
23246 cp_decl_specifier_seq type_specifiers;
23247 cp_declarator *declarator;
23248 const char *saved_message;
23250 /* If it's an ellipsis, it's easy to handle. */
23251 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23253 /* Consume the `...' token. */
23254 cp_lexer_consume_token (parser->lexer);
23255 return NULL_TREE;
23258 /* Types may not be defined in exception-declarations. */
23259 saved_message = parser->type_definition_forbidden_message;
23260 parser->type_definition_forbidden_message
23261 = G_("types may not be defined in exception-declarations");
23263 /* Parse the type-specifier-seq. */
23264 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
23265 /*is_trailing_return=*/false,
23266 &type_specifiers);
23267 /* If it's a `)', then there is no declarator. */
23268 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
23269 declarator = NULL;
23270 else
23271 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
23272 /*ctor_dtor_or_conv_p=*/NULL,
23273 /*parenthesized_p=*/NULL,
23274 /*member_p=*/false,
23275 /*friend_p=*/false);
23277 /* Restore the saved message. */
23278 parser->type_definition_forbidden_message = saved_message;
23280 if (!type_specifiers.any_specifiers_p)
23281 return error_mark_node;
23283 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
23286 /* Parse a throw-expression.
23288 throw-expression:
23289 throw assignment-expression [opt]
23291 Returns a THROW_EXPR representing the throw-expression. */
23293 static tree
23294 cp_parser_throw_expression (cp_parser* parser)
23296 tree expression;
23297 cp_token* token;
23299 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
23300 token = cp_lexer_peek_token (parser->lexer);
23301 /* Figure out whether or not there is an assignment-expression
23302 following the "throw" keyword. */
23303 if (token->type == CPP_COMMA
23304 || token->type == CPP_SEMICOLON
23305 || token->type == CPP_CLOSE_PAREN
23306 || token->type == CPP_CLOSE_SQUARE
23307 || token->type == CPP_CLOSE_BRACE
23308 || token->type == CPP_COLON)
23309 expression = NULL_TREE;
23310 else
23311 expression = cp_parser_assignment_expression (parser);
23313 return build_throw (expression);
23316 /* GNU Extensions */
23318 /* Parse an (optional) asm-specification.
23320 asm-specification:
23321 asm ( string-literal )
23323 If the asm-specification is present, returns a STRING_CST
23324 corresponding to the string-literal. Otherwise, returns
23325 NULL_TREE. */
23327 static tree
23328 cp_parser_asm_specification_opt (cp_parser* parser)
23330 cp_token *token;
23331 tree asm_specification;
23333 /* Peek at the next token. */
23334 token = cp_lexer_peek_token (parser->lexer);
23335 /* If the next token isn't the `asm' keyword, then there's no
23336 asm-specification. */
23337 if (!cp_parser_is_keyword (token, RID_ASM))
23338 return NULL_TREE;
23340 /* Consume the `asm' token. */
23341 cp_lexer_consume_token (parser->lexer);
23342 /* Look for the `('. */
23343 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23345 /* Look for the string-literal. */
23346 asm_specification = cp_parser_string_literal (parser, false, false);
23348 /* Look for the `)'. */
23349 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23351 return asm_specification;
23354 /* Parse an asm-operand-list.
23356 asm-operand-list:
23357 asm-operand
23358 asm-operand-list , asm-operand
23360 asm-operand:
23361 string-literal ( expression )
23362 [ string-literal ] string-literal ( expression )
23364 Returns a TREE_LIST representing the operands. The TREE_VALUE of
23365 each node is the expression. The TREE_PURPOSE is itself a
23366 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
23367 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
23368 is a STRING_CST for the string literal before the parenthesis. Returns
23369 ERROR_MARK_NODE if any of the operands are invalid. */
23371 static tree
23372 cp_parser_asm_operand_list (cp_parser* parser)
23374 tree asm_operands = NULL_TREE;
23375 bool invalid_operands = false;
23377 while (true)
23379 tree string_literal;
23380 tree expression;
23381 tree name;
23383 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
23385 /* Consume the `[' token. */
23386 cp_lexer_consume_token (parser->lexer);
23387 /* Read the operand name. */
23388 name = cp_parser_identifier (parser);
23389 if (name != error_mark_node)
23390 name = build_string (IDENTIFIER_LENGTH (name),
23391 IDENTIFIER_POINTER (name));
23392 /* Look for the closing `]'. */
23393 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23395 else
23396 name = NULL_TREE;
23397 /* Look for the string-literal. */
23398 string_literal = cp_parser_string_literal (parser, false, false);
23400 /* Look for the `('. */
23401 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23402 /* Parse the expression. */
23403 expression = cp_parser_expression (parser);
23404 /* Look for the `)'. */
23405 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23407 if (name == error_mark_node
23408 || string_literal == error_mark_node
23409 || expression == error_mark_node)
23410 invalid_operands = true;
23412 /* Add this operand to the list. */
23413 asm_operands = tree_cons (build_tree_list (name, string_literal),
23414 expression,
23415 asm_operands);
23416 /* If the next token is not a `,', there are no more
23417 operands. */
23418 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23419 break;
23420 /* Consume the `,'. */
23421 cp_lexer_consume_token (parser->lexer);
23424 return invalid_operands ? error_mark_node : nreverse (asm_operands);
23427 /* Parse an asm-clobber-list.
23429 asm-clobber-list:
23430 string-literal
23431 asm-clobber-list , string-literal
23433 Returns a TREE_LIST, indicating the clobbers in the order that they
23434 appeared. The TREE_VALUE of each node is a STRING_CST. */
23436 static tree
23437 cp_parser_asm_clobber_list (cp_parser* parser)
23439 tree clobbers = NULL_TREE;
23441 while (true)
23443 tree string_literal;
23445 /* Look for the string literal. */
23446 string_literal = cp_parser_string_literal (parser, false, false);
23447 /* Add it to the list. */
23448 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
23449 /* If the next token is not a `,', then the list is
23450 complete. */
23451 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23452 break;
23453 /* Consume the `,' token. */
23454 cp_lexer_consume_token (parser->lexer);
23457 return clobbers;
23460 /* Parse an asm-label-list.
23462 asm-label-list:
23463 identifier
23464 asm-label-list , identifier
23466 Returns a TREE_LIST, indicating the labels in the order that they
23467 appeared. The TREE_VALUE of each node is a label. */
23469 static tree
23470 cp_parser_asm_label_list (cp_parser* parser)
23472 tree labels = NULL_TREE;
23474 while (true)
23476 tree identifier, label, name;
23478 /* Look for the identifier. */
23479 identifier = cp_parser_identifier (parser);
23480 if (!error_operand_p (identifier))
23482 label = lookup_label (identifier);
23483 if (TREE_CODE (label) == LABEL_DECL)
23485 TREE_USED (label) = 1;
23486 check_goto (label);
23487 name = build_string (IDENTIFIER_LENGTH (identifier),
23488 IDENTIFIER_POINTER (identifier));
23489 labels = tree_cons (name, label, labels);
23492 /* If the next token is not a `,', then the list is
23493 complete. */
23494 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23495 break;
23496 /* Consume the `,' token. */
23497 cp_lexer_consume_token (parser->lexer);
23500 return nreverse (labels);
23503 /* Return TRUE iff the next tokens in the stream are possibly the
23504 beginning of a GNU extension attribute. */
23506 static bool
23507 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
23509 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
23512 /* Return TRUE iff the next tokens in the stream are possibly the
23513 beginning of a standard C++-11 attribute specifier. */
23515 static bool
23516 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
23518 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
23521 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23522 beginning of a standard C++-11 attribute specifier. */
23524 static bool
23525 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
23527 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23529 return (cxx_dialect >= cxx11
23530 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
23531 || (token->type == CPP_OPEN_SQUARE
23532 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
23533 && token->type == CPP_OPEN_SQUARE)));
23536 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23537 beginning of a GNU extension attribute. */
23539 static bool
23540 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
23542 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23544 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
23547 /* Return true iff the next tokens can be the beginning of either a
23548 GNU attribute list, or a standard C++11 attribute sequence. */
23550 static bool
23551 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
23553 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
23554 || cp_next_tokens_can_be_std_attribute_p (parser));
23557 /* Return true iff the next Nth tokens can be the beginning of either
23558 a GNU attribute list, or a standard C++11 attribute sequence. */
23560 static bool
23561 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
23563 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
23564 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
23567 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
23568 of GNU attributes, or return NULL. */
23570 static tree
23571 cp_parser_attributes_opt (cp_parser *parser)
23573 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
23574 return cp_parser_gnu_attributes_opt (parser);
23575 return cp_parser_std_attribute_spec_seq (parser);
23578 #define CILK_SIMD_FN_CLAUSE_MASK \
23579 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
23580 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
23581 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
23582 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
23583 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
23585 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
23586 vector [(<clauses>)] */
23588 static void
23589 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
23591 bool first_p = parser->cilk_simd_fn_info == NULL;
23592 cp_token *token = v_token;
23593 if (first_p)
23595 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
23596 parser->cilk_simd_fn_info->error_seen = false;
23597 parser->cilk_simd_fn_info->fndecl_seen = false;
23598 parser->cilk_simd_fn_info->tokens = vNULL;
23600 int paren_scope = 0;
23601 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23603 cp_lexer_consume_token (parser->lexer);
23604 v_token = cp_lexer_peek_token (parser->lexer);
23605 paren_scope++;
23607 while (paren_scope > 0)
23609 token = cp_lexer_peek_token (parser->lexer);
23610 if (token->type == CPP_OPEN_PAREN)
23611 paren_scope++;
23612 else if (token->type == CPP_CLOSE_PAREN)
23613 paren_scope--;
23614 /* Do not push the last ')' */
23615 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
23616 cp_lexer_consume_token (parser->lexer);
23619 token->type = CPP_PRAGMA_EOL;
23620 parser->lexer->next_token = token;
23621 cp_lexer_consume_token (parser->lexer);
23623 struct cp_token_cache *cp
23624 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
23625 parser->cilk_simd_fn_info->tokens.safe_push (cp);
23628 /* Parse an (optional) series of attributes.
23630 attributes:
23631 attributes attribute
23633 attribute:
23634 __attribute__ (( attribute-list [opt] ))
23636 The return value is as for cp_parser_gnu_attribute_list. */
23638 static tree
23639 cp_parser_gnu_attributes_opt (cp_parser* parser)
23641 tree attributes = NULL_TREE;
23643 while (true)
23645 cp_token *token;
23646 tree attribute_list;
23647 bool ok = true;
23649 /* Peek at the next token. */
23650 token = cp_lexer_peek_token (parser->lexer);
23651 /* If it's not `__attribute__', then we're done. */
23652 if (token->keyword != RID_ATTRIBUTE)
23653 break;
23655 /* Consume the `__attribute__' keyword. */
23656 cp_lexer_consume_token (parser->lexer);
23657 /* Look for the two `(' tokens. */
23658 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23659 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23661 /* Peek at the next token. */
23662 token = cp_lexer_peek_token (parser->lexer);
23663 if (token->type != CPP_CLOSE_PAREN)
23664 /* Parse the attribute-list. */
23665 attribute_list = cp_parser_gnu_attribute_list (parser);
23666 else
23667 /* If the next token is a `)', then there is no attribute
23668 list. */
23669 attribute_list = NULL;
23671 /* Look for the two `)' tokens. */
23672 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23673 ok = false;
23674 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23675 ok = false;
23676 if (!ok)
23677 cp_parser_skip_to_end_of_statement (parser);
23679 /* Add these new attributes to the list. */
23680 attributes = chainon (attributes, attribute_list);
23683 return attributes;
23686 /* Parse a GNU attribute-list.
23688 attribute-list:
23689 attribute
23690 attribute-list , attribute
23692 attribute:
23693 identifier
23694 identifier ( identifier )
23695 identifier ( identifier , expression-list )
23696 identifier ( expression-list )
23698 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
23699 to an attribute. The TREE_PURPOSE of each node is the identifier
23700 indicating which attribute is in use. The TREE_VALUE represents
23701 the arguments, if any. */
23703 static tree
23704 cp_parser_gnu_attribute_list (cp_parser* parser)
23706 tree attribute_list = NULL_TREE;
23707 bool save_translate_strings_p = parser->translate_strings_p;
23709 parser->translate_strings_p = false;
23710 while (true)
23712 cp_token *token;
23713 tree identifier;
23714 tree attribute;
23716 /* Look for the identifier. We also allow keywords here; for
23717 example `__attribute__ ((const))' is legal. */
23718 token = cp_lexer_peek_token (parser->lexer);
23719 if (token->type == CPP_NAME
23720 || token->type == CPP_KEYWORD)
23722 tree arguments = NULL_TREE;
23724 /* Consume the token, but save it since we need it for the
23725 SIMD enabled function parsing. */
23726 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
23728 /* Save away the identifier that indicates which attribute
23729 this is. */
23730 identifier = (token->type == CPP_KEYWORD)
23731 /* For keywords, use the canonical spelling, not the
23732 parsed identifier. */
23733 ? ridpointers[(int) token->keyword]
23734 : id_token->u.value;
23736 attribute = build_tree_list (identifier, NULL_TREE);
23738 /* Peek at the next token. */
23739 token = cp_lexer_peek_token (parser->lexer);
23740 /* If it's an `(', then parse the attribute arguments. */
23741 if (token->type == CPP_OPEN_PAREN)
23743 vec<tree, va_gc> *vec;
23744 int attr_flag = (attribute_takes_identifier_p (identifier)
23745 ? id_attr : normal_attr);
23746 if (is_cilkplus_vector_p (identifier))
23748 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
23749 continue;
23751 else
23752 vec = cp_parser_parenthesized_expression_list
23753 (parser, attr_flag, /*cast_p=*/false,
23754 /*allow_expansion_p=*/false,
23755 /*non_constant_p=*/NULL);
23756 if (vec == NULL)
23757 arguments = error_mark_node;
23758 else
23760 arguments = build_tree_list_vec (vec);
23761 release_tree_vector (vec);
23763 /* Save the arguments away. */
23764 TREE_VALUE (attribute) = arguments;
23766 else if (is_cilkplus_vector_p (identifier))
23768 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
23769 continue;
23772 if (arguments != error_mark_node)
23774 /* Add this attribute to the list. */
23775 TREE_CHAIN (attribute) = attribute_list;
23776 attribute_list = attribute;
23779 token = cp_lexer_peek_token (parser->lexer);
23781 /* Now, look for more attributes. If the next token isn't a
23782 `,', we're done. */
23783 if (token->type != CPP_COMMA)
23784 break;
23786 /* Consume the comma and keep going. */
23787 cp_lexer_consume_token (parser->lexer);
23789 parser->translate_strings_p = save_translate_strings_p;
23791 /* We built up the list in reverse order. */
23792 return nreverse (attribute_list);
23795 /* Parse a standard C++11 attribute.
23797 The returned representation is a TREE_LIST which TREE_PURPOSE is
23798 the scoped name of the attribute, and the TREE_VALUE is its
23799 arguments list.
23801 Note that the scoped name of the attribute is itself a TREE_LIST
23802 which TREE_PURPOSE is the namespace of the attribute, and
23803 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
23804 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
23805 and which TREE_PURPOSE is directly the attribute name.
23807 Clients of the attribute code should use get_attribute_namespace
23808 and get_attribute_name to get the actual namespace and name of
23809 attributes, regardless of their being GNU or C++11 attributes.
23811 attribute:
23812 attribute-token attribute-argument-clause [opt]
23814 attribute-token:
23815 identifier
23816 attribute-scoped-token
23818 attribute-scoped-token:
23819 attribute-namespace :: identifier
23821 attribute-namespace:
23822 identifier
23824 attribute-argument-clause:
23825 ( balanced-token-seq )
23827 balanced-token-seq:
23828 balanced-token [opt]
23829 balanced-token-seq balanced-token
23831 balanced-token:
23832 ( balanced-token-seq )
23833 [ balanced-token-seq ]
23834 { balanced-token-seq }. */
23836 static tree
23837 cp_parser_std_attribute (cp_parser *parser)
23839 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
23840 cp_token *token;
23842 /* First, parse name of the attribute, a.k.a attribute-token. */
23844 token = cp_lexer_peek_token (parser->lexer);
23845 if (token->type == CPP_NAME)
23846 attr_id = token->u.value;
23847 else if (token->type == CPP_KEYWORD)
23848 attr_id = ridpointers[(int) token->keyword];
23849 else if (token->flags & NAMED_OP)
23850 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
23852 if (attr_id == NULL_TREE)
23853 return NULL_TREE;
23855 cp_lexer_consume_token (parser->lexer);
23857 token = cp_lexer_peek_token (parser->lexer);
23858 if (token->type == CPP_SCOPE)
23860 /* We are seeing a scoped attribute token. */
23862 cp_lexer_consume_token (parser->lexer);
23863 attr_ns = attr_id;
23865 token = cp_lexer_consume_token (parser->lexer);
23866 if (token->type == CPP_NAME)
23867 attr_id = token->u.value;
23868 else if (token->type == CPP_KEYWORD)
23869 attr_id = ridpointers[(int) token->keyword];
23870 else
23872 error_at (token->location,
23873 "expected an identifier for the attribute name");
23874 return error_mark_node;
23876 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
23877 NULL_TREE);
23878 token = cp_lexer_peek_token (parser->lexer);
23880 else
23882 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
23883 NULL_TREE);
23884 /* C++11 noreturn attribute is equivalent to GNU's. */
23885 if (is_attribute_p ("noreturn", attr_id))
23886 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
23887 /* C++14 deprecated attribute is equivalent to GNU's. */
23888 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
23890 if (cxx_dialect == cxx11)
23891 pedwarn (token->location, OPT_Wpedantic,
23892 "%<deprecated%> is a C++14 feature;"
23893 " use %<gnu::deprecated%>");
23894 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
23896 /* Transactional Memory TS optimize_for_synchronized attribute is
23897 equivalent to GNU transaction_callable. */
23898 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
23899 TREE_PURPOSE (attribute)
23900 = get_identifier ("transaction_callable");
23901 /* Transactional Memory attributes are GNU attributes. */
23902 else if (tm_attr_to_mask (attr_id))
23903 TREE_PURPOSE (attribute) = attr_id;
23906 /* Now parse the optional argument clause of the attribute. */
23908 if (token->type != CPP_OPEN_PAREN)
23909 return attribute;
23912 vec<tree, va_gc> *vec;
23913 int attr_flag = normal_attr;
23915 if (attr_ns == get_identifier ("gnu")
23916 && attribute_takes_identifier_p (attr_id))
23917 /* A GNU attribute that takes an identifier in parameter. */
23918 attr_flag = id_attr;
23920 vec = cp_parser_parenthesized_expression_list
23921 (parser, attr_flag, /*cast_p=*/false,
23922 /*allow_expansion_p=*/true,
23923 /*non_constant_p=*/NULL);
23924 if (vec == NULL)
23925 arguments = error_mark_node;
23926 else
23928 arguments = build_tree_list_vec (vec);
23929 release_tree_vector (vec);
23932 if (arguments == error_mark_node)
23933 attribute = error_mark_node;
23934 else
23935 TREE_VALUE (attribute) = arguments;
23938 return attribute;
23941 /* Check that the attribute ATTRIBUTE appears at most once in the
23942 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
23943 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
23944 isn't implemented yet in GCC. */
23946 static void
23947 cp_parser_check_std_attribute (tree attributes, tree attribute)
23949 if (attributes)
23951 tree name = get_attribute_name (attribute);
23952 if (is_attribute_p ("noreturn", name)
23953 && lookup_attribute ("noreturn", attributes))
23954 error ("attribute noreturn can appear at most once "
23955 "in an attribute-list");
23956 else if (is_attribute_p ("deprecated", name)
23957 && lookup_attribute ("deprecated", attributes))
23958 error ("attribute deprecated can appear at most once "
23959 "in an attribute-list");
23963 /* Parse a list of standard C++-11 attributes.
23965 attribute-list:
23966 attribute [opt]
23967 attribute-list , attribute[opt]
23968 attribute ...
23969 attribute-list , attribute ...
23972 static tree
23973 cp_parser_std_attribute_list (cp_parser *parser)
23975 tree attributes = NULL_TREE, attribute = NULL_TREE;
23976 cp_token *token = NULL;
23978 while (true)
23980 attribute = cp_parser_std_attribute (parser);
23981 if (attribute == error_mark_node)
23982 break;
23983 if (attribute != NULL_TREE)
23985 cp_parser_check_std_attribute (attributes, attribute);
23986 TREE_CHAIN (attribute) = attributes;
23987 attributes = attribute;
23989 token = cp_lexer_peek_token (parser->lexer);
23990 if (token->type == CPP_ELLIPSIS)
23992 cp_lexer_consume_token (parser->lexer);
23993 TREE_VALUE (attribute)
23994 = make_pack_expansion (TREE_VALUE (attribute));
23995 token = cp_lexer_peek_token (parser->lexer);
23997 if (token->type != CPP_COMMA)
23998 break;
23999 cp_lexer_consume_token (parser->lexer);
24001 attributes = nreverse (attributes);
24002 return attributes;
24005 /* Parse a standard C++-11 attribute specifier.
24007 attribute-specifier:
24008 [ [ attribute-list ] ]
24009 alignment-specifier
24011 alignment-specifier:
24012 alignas ( type-id ... [opt] )
24013 alignas ( alignment-expression ... [opt] ). */
24015 static tree
24016 cp_parser_std_attribute_spec (cp_parser *parser)
24018 tree attributes = NULL_TREE;
24019 cp_token *token = cp_lexer_peek_token (parser->lexer);
24021 if (token->type == CPP_OPEN_SQUARE
24022 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
24024 cp_lexer_consume_token (parser->lexer);
24025 cp_lexer_consume_token (parser->lexer);
24027 attributes = cp_parser_std_attribute_list (parser);
24029 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
24030 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
24031 cp_parser_skip_to_end_of_statement (parser);
24032 else
24033 /* Warn about parsing c++11 attribute in non-c++1 mode, only
24034 when we are sure that we have actually parsed them. */
24035 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24037 else
24039 tree alignas_expr;
24041 /* Look for an alignment-specifier. */
24043 token = cp_lexer_peek_token (parser->lexer);
24045 if (token->type != CPP_KEYWORD
24046 || token->keyword != RID_ALIGNAS)
24047 return NULL_TREE;
24049 cp_lexer_consume_token (parser->lexer);
24050 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24052 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
24054 cp_parser_error (parser, "expected %<(%>");
24055 return error_mark_node;
24058 cp_parser_parse_tentatively (parser);
24059 alignas_expr = cp_parser_type_id (parser);
24061 if (!cp_parser_parse_definitely (parser))
24063 gcc_assert (alignas_expr == error_mark_node
24064 || alignas_expr == NULL_TREE);
24066 alignas_expr =
24067 cp_parser_assignment_expression (parser);
24068 if (alignas_expr == error_mark_node)
24069 cp_parser_skip_to_end_of_statement (parser);
24070 if (alignas_expr == NULL_TREE
24071 || alignas_expr == error_mark_node)
24072 return alignas_expr;
24075 alignas_expr = cxx_alignas_expr (alignas_expr);
24076 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
24078 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24080 cp_lexer_consume_token (parser->lexer);
24081 alignas_expr = make_pack_expansion (alignas_expr);
24084 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
24086 cp_parser_error (parser, "expected %<)%>");
24087 return error_mark_node;
24090 /* Build the C++-11 representation of an 'aligned'
24091 attribute. */
24092 attributes =
24093 build_tree_list (build_tree_list (get_identifier ("gnu"),
24094 get_identifier ("aligned")),
24095 alignas_expr);
24098 return attributes;
24101 /* Parse a standard C++-11 attribute-specifier-seq.
24103 attribute-specifier-seq:
24104 attribute-specifier-seq [opt] attribute-specifier
24107 static tree
24108 cp_parser_std_attribute_spec_seq (cp_parser *parser)
24110 tree attr_specs = NULL;
24112 while (true)
24114 tree attr_spec = cp_parser_std_attribute_spec (parser);
24115 if (attr_spec == NULL_TREE)
24116 break;
24117 if (attr_spec == error_mark_node)
24118 return error_mark_node;
24120 TREE_CHAIN (attr_spec) = attr_specs;
24121 attr_specs = attr_spec;
24124 attr_specs = nreverse (attr_specs);
24125 return attr_specs;
24128 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
24129 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
24130 current value of the PEDANTIC flag, regardless of whether or not
24131 the `__extension__' keyword is present. The caller is responsible
24132 for restoring the value of the PEDANTIC flag. */
24134 static bool
24135 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
24137 /* Save the old value of the PEDANTIC flag. */
24138 *saved_pedantic = pedantic;
24140 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
24142 /* Consume the `__extension__' token. */
24143 cp_lexer_consume_token (parser->lexer);
24144 /* We're not being pedantic while the `__extension__' keyword is
24145 in effect. */
24146 pedantic = 0;
24148 return true;
24151 return false;
24154 /* Parse a label declaration.
24156 label-declaration:
24157 __label__ label-declarator-seq ;
24159 label-declarator-seq:
24160 identifier , label-declarator-seq
24161 identifier */
24163 static void
24164 cp_parser_label_declaration (cp_parser* parser)
24166 /* Look for the `__label__' keyword. */
24167 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
24169 while (true)
24171 tree identifier;
24173 /* Look for an identifier. */
24174 identifier = cp_parser_identifier (parser);
24175 /* If we failed, stop. */
24176 if (identifier == error_mark_node)
24177 break;
24178 /* Declare it as a label. */
24179 finish_label_decl (identifier);
24180 /* If the next token is a `;', stop. */
24181 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24182 break;
24183 /* Look for the `,' separating the label declarations. */
24184 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
24187 /* Look for the final `;'. */
24188 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24191 // -------------------------------------------------------------------------- //
24192 // Requires Clause
24194 // Parse a requires clause.
24196 // requires-clause:
24197 // 'requires' logical-or-expression
24199 // The required logical-or-expression must be a constant expression. Note
24200 // that we don't check that the expression is constepxr here. We defer until
24201 // we analyze constraints and then, we only check atomic constraints.
24202 static tree
24203 cp_parser_requires_clause (cp_parser *parser)
24205 // Parse the requires clause so that it is not automatically folded.
24206 ++processing_template_decl;
24207 tree expr = cp_parser_binary_expression (parser, false, false,
24208 PREC_NOT_OPERATOR, NULL);
24209 --processing_template_decl;
24210 return expr;
24213 // Optionally parse a requires clause:
24214 static tree
24215 cp_parser_requires_clause_opt (cp_parser *parser)
24217 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
24218 return NULL_TREE;
24219 cp_lexer_consume_token (parser->lexer);
24220 return cp_parser_requires_clause (parser);
24224 /*---------------------------------------------------------------------------
24225 Requires expressions
24226 ---------------------------------------------------------------------------*/
24228 /* Parse a requires expression
24230 requirement-expression:
24231 'requires' requirement-parameter-list [opt] requirement-body */
24232 static tree
24233 cp_parser_requires_expression (cp_parser *parser)
24235 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
24236 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
24238 /* A requires-expression shall appear only within a concept
24239 definition or a requires-clause.
24241 TODO: Implement this diagnostic correctly. */
24242 if (!processing_template_decl)
24244 error_at (loc, "a requires expression cannot appear outside a template");
24245 cp_parser_skip_to_end_of_statement (parser);
24246 return error_mark_node;
24249 tree parms, reqs;
24251 /* Local parameters are delared as variables within the scope
24252 of the expression. They are not visible past the end of
24253 the expression. Expressions within the requires-expression
24254 are unevaluated. */
24255 struct scope_sentinel
24257 scope_sentinel ()
24259 ++cp_unevaluated_operand;
24260 begin_scope (sk_block, NULL_TREE);
24263 ~scope_sentinel ()
24265 pop_bindings_and_leave_scope ();
24266 --cp_unevaluated_operand;
24268 } s;
24270 /* Parse the optional parameter list. */
24271 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24273 parms = cp_parser_requirement_parameter_list (parser);
24274 if (parms == error_mark_node)
24275 return error_mark_node;
24277 else
24278 parms = NULL_TREE;
24280 /* Parse the requirement body. */
24281 reqs = cp_parser_requirement_body (parser);
24282 if (reqs == error_mark_node)
24283 return error_mark_node;
24286 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
24287 the parm chain. */
24288 grokparms (parms, &parms);
24289 return finish_requires_expr (parms, reqs);
24292 /* Parse a parameterized requirement.
24294 requirement-parameter-list:
24295 '(' parameter-declaration-clause ')' */
24296 static tree
24297 cp_parser_requirement_parameter_list (cp_parser *parser)
24299 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24300 return error_mark_node;
24302 tree parms = cp_parser_parameter_declaration_clause (parser);
24304 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24305 return error_mark_node;
24307 return parms;
24310 /* Parse the body of a requirement.
24312 requirement-body:
24313 '{' requirement-list '}' */
24314 static tree
24315 cp_parser_requirement_body (cp_parser *parser)
24317 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24318 return error_mark_node;
24320 tree reqs = cp_parser_requirement_list (parser);
24322 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24323 return error_mark_node;
24325 return reqs;
24328 /* Parse a list of requirements.
24330 requirement-list:
24331 requirement
24332 requirement-list ';' requirement[opt] */
24333 static tree
24334 cp_parser_requirement_list (cp_parser *parser)
24336 tree result = NULL_TREE;
24337 while (true)
24339 tree req = cp_parser_requirement (parser);
24340 if (req == error_mark_node)
24341 return error_mark_node;
24343 result = tree_cons (NULL_TREE, req, result);
24345 /* If we see a semi-colon, consume it. */
24346 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24347 cp_lexer_consume_token (parser->lexer);
24349 /* Stop processing at the end of the list. */
24350 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24351 break;
24354 /* Reverse the order of requirements so they are analyzed in
24355 declaration order. */
24356 return nreverse (result);
24359 /* Parse a syntactic requirement or type requirement.
24361 requirement:
24362 simple-requirement
24363 compound-requirement
24364 type-requirement
24365 nested-requirement */
24366 static tree
24367 cp_parser_requirement (cp_parser *parser)
24369 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24370 return cp_parser_compound_requirement (parser);
24371 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24372 return cp_parser_type_requirement (parser);
24373 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
24374 return cp_parser_nested_requirement (parser);
24375 else
24376 return cp_parser_simple_requirement (parser);
24379 /* Parse a simple requirement.
24381 simple-requirement:
24382 expression ';' */
24383 static tree
24384 cp_parser_simple_requirement (cp_parser *parser)
24386 tree expr = cp_parser_expression (parser, NULL, false, false);
24387 if (!expr || expr == error_mark_node)
24388 return error_mark_node;
24390 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
24391 return error_mark_node;
24393 return finish_simple_requirement (expr);
24396 /* Parse a type requirement
24398 type-requirement
24399 nested-name-specifier [opt] required-type-name ';'
24401 required-type-name:
24402 type-name
24403 'template' [opt] simple-template-id */
24404 static tree
24405 cp_parser_type_requirement (cp_parser *parser)
24407 cp_lexer_consume_token (parser->lexer);
24409 // Save the scope before parsing name specifiers.
24410 tree saved_scope = parser->scope;
24411 tree saved_object_scope = parser->object_scope;
24412 tree saved_qualifying_scope = parser->qualifying_scope;
24413 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
24414 cp_parser_nested_name_specifier_opt (parser,
24415 /*typename_keyword_p=*/true,
24416 /*check_dependency_p=*/false,
24417 /*type_p=*/true,
24418 /*is_declaration=*/false);
24420 tree type;
24421 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24423 cp_lexer_consume_token (parser->lexer);
24424 type = cp_parser_template_id (parser,
24425 /*template_keyword_p=*/true,
24426 /*check_dependency=*/false,
24427 /*tag_type=*/none_type,
24428 /*is_declaration=*/false);
24429 type = make_typename_type (parser->scope, type, typename_type,
24430 /*complain=*/tf_error);
24432 else
24433 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
24435 if (TREE_CODE (type) == TYPE_DECL)
24436 type = TREE_TYPE (type);
24438 parser->scope = saved_scope;
24439 parser->object_scope = saved_object_scope;
24440 parser->qualifying_scope = saved_qualifying_scope;
24442 if (type == error_mark_node)
24443 cp_parser_skip_to_end_of_statement (parser);
24445 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
24446 return error_mark_node;
24447 if (type == error_mark_node)
24448 return error_mark_node;
24450 return finish_type_requirement (type);
24453 /* Parse a compound requirement
24455 compound-requirement:
24456 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
24457 static tree
24458 cp_parser_compound_requirement (cp_parser *parser)
24460 /* Parse an expression enclosed in '{ }'s. */
24461 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24462 return error_mark_node;
24464 tree expr = cp_parser_expression (parser, NULL, false, false);
24465 if (!expr || expr == error_mark_node)
24466 return error_mark_node;
24468 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24469 return error_mark_node;
24471 /* Parse the optional noexcept. */
24472 bool noexcept_p = false;
24473 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
24475 cp_lexer_consume_token (parser->lexer);
24476 noexcept_p = true;
24479 /* Parse the optional trailing return type. */
24480 tree type = NULL_TREE;
24481 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
24483 cp_lexer_consume_token (parser->lexer);
24484 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
24485 parser->in_result_type_constraint_p = true;
24486 type = cp_parser_trailing_type_id (parser);
24487 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
24488 if (type == error_mark_node)
24489 return error_mark_node;
24492 return finish_compound_requirement (expr, type, noexcept_p);
24495 /* Parse a nested requirement. This is the same as a requires clause.
24497 nested-requirement:
24498 requires-clause */
24499 static tree
24500 cp_parser_nested_requirement (cp_parser *parser)
24502 cp_lexer_consume_token (parser->lexer);
24503 tree req = cp_parser_requires_clause (parser);
24504 if (req == error_mark_node)
24505 return error_mark_node;
24506 return finish_nested_requirement (req);
24509 /* Support Functions */
24511 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
24512 NAME should have one of the representations used for an
24513 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
24514 is returned. If PARSER->SCOPE is a dependent type, then a
24515 SCOPE_REF is returned.
24517 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
24518 returned; the name was already resolved when the TEMPLATE_ID_EXPR
24519 was formed. Abstractly, such entities should not be passed to this
24520 function, because they do not need to be looked up, but it is
24521 simpler to check for this special case here, rather than at the
24522 call-sites.
24524 In cases not explicitly covered above, this function returns a
24525 DECL, OVERLOAD, or baselink representing the result of the lookup.
24526 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
24527 is returned.
24529 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
24530 (e.g., "struct") that was used. In that case bindings that do not
24531 refer to types are ignored.
24533 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
24534 ignored.
24536 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
24537 are ignored.
24539 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
24540 types.
24542 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
24543 TREE_LIST of candidates if name-lookup results in an ambiguity, and
24544 NULL_TREE otherwise. */
24546 static cp_expr
24547 cp_parser_lookup_name (cp_parser *parser, tree name,
24548 enum tag_types tag_type,
24549 bool is_template,
24550 bool is_namespace,
24551 bool check_dependency,
24552 tree *ambiguous_decls,
24553 location_t name_location)
24555 tree decl;
24556 tree object_type = parser->context->object_type;
24558 /* Assume that the lookup will be unambiguous. */
24559 if (ambiguous_decls)
24560 *ambiguous_decls = NULL_TREE;
24562 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
24563 no longer valid. Note that if we are parsing tentatively, and
24564 the parse fails, OBJECT_TYPE will be automatically restored. */
24565 parser->context->object_type = NULL_TREE;
24567 if (name == error_mark_node)
24568 return error_mark_node;
24570 /* A template-id has already been resolved; there is no lookup to
24571 do. */
24572 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
24573 return name;
24574 if (BASELINK_P (name))
24576 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
24577 == TEMPLATE_ID_EXPR);
24578 return name;
24581 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
24582 it should already have been checked to make sure that the name
24583 used matches the type being destroyed. */
24584 if (TREE_CODE (name) == BIT_NOT_EXPR)
24586 tree type;
24588 /* Figure out to which type this destructor applies. */
24589 if (parser->scope)
24590 type = parser->scope;
24591 else if (object_type)
24592 type = object_type;
24593 else
24594 type = current_class_type;
24595 /* If that's not a class type, there is no destructor. */
24596 if (!type || !CLASS_TYPE_P (type))
24597 return error_mark_node;
24598 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
24599 lazily_declare_fn (sfk_destructor, type);
24600 if (!CLASSTYPE_DESTRUCTORS (type))
24601 return error_mark_node;
24602 /* If it was a class type, return the destructor. */
24603 return CLASSTYPE_DESTRUCTORS (type);
24606 /* By this point, the NAME should be an ordinary identifier. If
24607 the id-expression was a qualified name, the qualifying scope is
24608 stored in PARSER->SCOPE at this point. */
24609 gcc_assert (identifier_p (name));
24611 /* Perform the lookup. */
24612 if (parser->scope)
24614 bool dependent_p;
24616 if (parser->scope == error_mark_node)
24617 return error_mark_node;
24619 /* If the SCOPE is dependent, the lookup must be deferred until
24620 the template is instantiated -- unless we are explicitly
24621 looking up names in uninstantiated templates. Even then, we
24622 cannot look up the name if the scope is not a class type; it
24623 might, for example, be a template type parameter. */
24624 dependent_p = (TYPE_P (parser->scope)
24625 && dependent_scope_p (parser->scope));
24626 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
24627 && dependent_p)
24628 /* Defer lookup. */
24629 decl = error_mark_node;
24630 else
24632 tree pushed_scope = NULL_TREE;
24634 /* If PARSER->SCOPE is a dependent type, then it must be a
24635 class type, and we must not be checking dependencies;
24636 otherwise, we would have processed this lookup above. So
24637 that PARSER->SCOPE is not considered a dependent base by
24638 lookup_member, we must enter the scope here. */
24639 if (dependent_p)
24640 pushed_scope = push_scope (parser->scope);
24642 /* If the PARSER->SCOPE is a template specialization, it
24643 may be instantiated during name lookup. In that case,
24644 errors may be issued. Even if we rollback the current
24645 tentative parse, those errors are valid. */
24646 decl = lookup_qualified_name (parser->scope, name,
24647 tag_type != none_type,
24648 /*complain=*/true);
24650 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
24651 lookup result and the nested-name-specifier nominates a class C:
24652 * if the name specified after the nested-name-specifier, when
24653 looked up in C, is the injected-class-name of C (Clause 9), or
24654 * if the name specified after the nested-name-specifier is the
24655 same as the identifier or the simple-template-id's template-
24656 name in the last component of the nested-name-specifier,
24657 the name is instead considered to name the constructor of
24658 class C. [ Note: for example, the constructor is not an
24659 acceptable lookup result in an elaborated-type-specifier so
24660 the constructor would not be used in place of the
24661 injected-class-name. --end note ] Such a constructor name
24662 shall be used only in the declarator-id of a declaration that
24663 names a constructor or in a using-declaration. */
24664 if (tag_type == none_type
24665 && DECL_SELF_REFERENCE_P (decl)
24666 && same_type_p (DECL_CONTEXT (decl), parser->scope))
24667 decl = lookup_qualified_name (parser->scope, ctor_identifier,
24668 tag_type != none_type,
24669 /*complain=*/true);
24671 /* If we have a single function from a using decl, pull it out. */
24672 if (TREE_CODE (decl) == OVERLOAD
24673 && !really_overloaded_fn (decl))
24674 decl = OVL_FUNCTION (decl);
24676 if (pushed_scope)
24677 pop_scope (pushed_scope);
24680 /* If the scope is a dependent type and either we deferred lookup or
24681 we did lookup but didn't find the name, rememeber the name. */
24682 if (decl == error_mark_node && TYPE_P (parser->scope)
24683 && dependent_type_p (parser->scope))
24685 if (tag_type)
24687 tree type;
24689 /* The resolution to Core Issue 180 says that `struct
24690 A::B' should be considered a type-name, even if `A'
24691 is dependent. */
24692 type = make_typename_type (parser->scope, name, tag_type,
24693 /*complain=*/tf_error);
24694 if (type != error_mark_node)
24695 decl = TYPE_NAME (type);
24697 else if (is_template
24698 && (cp_parser_next_token_ends_template_argument_p (parser)
24699 || cp_lexer_next_token_is (parser->lexer,
24700 CPP_CLOSE_PAREN)))
24701 decl = make_unbound_class_template (parser->scope,
24702 name, NULL_TREE,
24703 /*complain=*/tf_error);
24704 else
24705 decl = build_qualified_name (/*type=*/NULL_TREE,
24706 parser->scope, name,
24707 is_template);
24709 parser->qualifying_scope = parser->scope;
24710 parser->object_scope = NULL_TREE;
24712 else if (object_type)
24714 /* Look up the name in the scope of the OBJECT_TYPE, unless the
24715 OBJECT_TYPE is not a class. */
24716 if (CLASS_TYPE_P (object_type))
24717 /* If the OBJECT_TYPE is a template specialization, it may
24718 be instantiated during name lookup. In that case, errors
24719 may be issued. Even if we rollback the current tentative
24720 parse, those errors are valid. */
24721 decl = lookup_member (object_type,
24722 name,
24723 /*protect=*/0,
24724 tag_type != none_type,
24725 tf_warning_or_error);
24726 else
24727 decl = NULL_TREE;
24729 if (!decl)
24730 /* Look it up in the enclosing context. DR 141: When looking for a
24731 template-name after -> or ., only consider class templates. */
24732 decl = lookup_name_real (name, tag_type != none_type || is_template,
24733 /*nonclass=*/0,
24734 /*block_p=*/true, is_namespace, 0);
24735 if (object_type == unknown_type_node)
24736 /* The object is type-dependent, so we can't look anything up; we used
24737 this to get the DR 141 behavior. */
24738 object_type = NULL_TREE;
24739 parser->object_scope = object_type;
24740 parser->qualifying_scope = NULL_TREE;
24742 else
24744 decl = lookup_name_real (name, tag_type != none_type,
24745 /*nonclass=*/0,
24746 /*block_p=*/true, is_namespace, 0);
24747 parser->qualifying_scope = NULL_TREE;
24748 parser->object_scope = NULL_TREE;
24751 /* If the lookup failed, let our caller know. */
24752 if (!decl || decl == error_mark_node)
24753 return error_mark_node;
24755 /* Pull out the template from an injected-class-name (or multiple). */
24756 if (is_template)
24757 decl = maybe_get_template_decl_from_type_decl (decl);
24759 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
24760 if (TREE_CODE (decl) == TREE_LIST)
24762 if (ambiguous_decls)
24763 *ambiguous_decls = decl;
24764 /* The error message we have to print is too complicated for
24765 cp_parser_error, so we incorporate its actions directly. */
24766 if (!cp_parser_simulate_error (parser))
24768 error_at (name_location, "reference to %qD is ambiguous",
24769 name);
24770 print_candidates (decl);
24772 return error_mark_node;
24775 gcc_assert (DECL_P (decl)
24776 || TREE_CODE (decl) == OVERLOAD
24777 || TREE_CODE (decl) == SCOPE_REF
24778 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
24779 || BASELINK_P (decl));
24781 /* If we have resolved the name of a member declaration, check to
24782 see if the declaration is accessible. When the name resolves to
24783 set of overloaded functions, accessibility is checked when
24784 overload resolution is done.
24786 During an explicit instantiation, access is not checked at all,
24787 as per [temp.explicit]. */
24788 if (DECL_P (decl))
24789 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
24791 maybe_record_typedef_use (decl);
24793 return cp_expr (decl, name_location);
24796 /* Like cp_parser_lookup_name, but for use in the typical case where
24797 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
24798 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
24800 static tree
24801 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
24803 return cp_parser_lookup_name (parser, name,
24804 none_type,
24805 /*is_template=*/false,
24806 /*is_namespace=*/false,
24807 /*check_dependency=*/true,
24808 /*ambiguous_decls=*/NULL,
24809 location);
24812 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
24813 the current context, return the TYPE_DECL. If TAG_NAME_P is
24814 true, the DECL indicates the class being defined in a class-head,
24815 or declared in an elaborated-type-specifier.
24817 Otherwise, return DECL. */
24819 static tree
24820 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
24822 /* If the TEMPLATE_DECL is being declared as part of a class-head,
24823 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
24825 struct A {
24826 template <typename T> struct B;
24829 template <typename T> struct A::B {};
24831 Similarly, in an elaborated-type-specifier:
24833 namespace N { struct X{}; }
24835 struct A {
24836 template <typename T> friend struct N::X;
24839 However, if the DECL refers to a class type, and we are in
24840 the scope of the class, then the name lookup automatically
24841 finds the TYPE_DECL created by build_self_reference rather
24842 than a TEMPLATE_DECL. For example, in:
24844 template <class T> struct S {
24845 S s;
24848 there is no need to handle such case. */
24850 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
24851 return DECL_TEMPLATE_RESULT (decl);
24853 return decl;
24856 /* If too many, or too few, template-parameter lists apply to the
24857 declarator, issue an error message. Returns TRUE if all went well,
24858 and FALSE otherwise. */
24860 static bool
24861 cp_parser_check_declarator_template_parameters (cp_parser* parser,
24862 cp_declarator *declarator,
24863 location_t declarator_location)
24865 switch (declarator->kind)
24867 case cdk_id:
24869 unsigned num_templates = 0;
24870 tree scope = declarator->u.id.qualifying_scope;
24872 if (scope)
24873 num_templates = num_template_headers_for_class (scope);
24874 else if (TREE_CODE (declarator->u.id.unqualified_name)
24875 == TEMPLATE_ID_EXPR)
24876 /* If the DECLARATOR has the form `X<y>' then it uses one
24877 additional level of template parameters. */
24878 ++num_templates;
24880 return cp_parser_check_template_parameters
24881 (parser, num_templates, declarator_location, declarator);
24884 case cdk_function:
24885 case cdk_array:
24886 case cdk_pointer:
24887 case cdk_reference:
24888 case cdk_ptrmem:
24889 return (cp_parser_check_declarator_template_parameters
24890 (parser, declarator->declarator, declarator_location));
24892 case cdk_error:
24893 return true;
24895 default:
24896 gcc_unreachable ();
24898 return false;
24901 /* NUM_TEMPLATES were used in the current declaration. If that is
24902 invalid, return FALSE and issue an error messages. Otherwise,
24903 return TRUE. If DECLARATOR is non-NULL, then we are checking a
24904 declarator and we can print more accurate diagnostics. */
24906 static bool
24907 cp_parser_check_template_parameters (cp_parser* parser,
24908 unsigned num_templates,
24909 location_t location,
24910 cp_declarator *declarator)
24912 /* If there are the same number of template classes and parameter
24913 lists, that's OK. */
24914 if (parser->num_template_parameter_lists == num_templates)
24915 return true;
24916 /* If there are more, but only one more, then we are referring to a
24917 member template. That's OK too. */
24918 if (parser->num_template_parameter_lists == num_templates + 1)
24919 return true;
24920 /* If there are more template classes than parameter lists, we have
24921 something like:
24923 template <class T> void S<T>::R<T>::f (); */
24924 if (parser->num_template_parameter_lists < num_templates)
24926 if (declarator && !current_function_decl)
24927 error_at (location, "specializing member %<%T::%E%> "
24928 "requires %<template<>%> syntax",
24929 declarator->u.id.qualifying_scope,
24930 declarator->u.id.unqualified_name);
24931 else if (declarator)
24932 error_at (location, "invalid declaration of %<%T::%E%>",
24933 declarator->u.id.qualifying_scope,
24934 declarator->u.id.unqualified_name);
24935 else
24936 error_at (location, "too few template-parameter-lists");
24937 return false;
24939 /* Otherwise, there are too many template parameter lists. We have
24940 something like:
24942 template <class T> template <class U> void S::f(); */
24943 error_at (location, "too many template-parameter-lists");
24944 return false;
24947 /* Parse an optional `::' token indicating that the following name is
24948 from the global namespace. If so, PARSER->SCOPE is set to the
24949 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
24950 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
24951 Returns the new value of PARSER->SCOPE, if the `::' token is
24952 present, and NULL_TREE otherwise. */
24954 static tree
24955 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
24957 cp_token *token;
24959 /* Peek at the next token. */
24960 token = cp_lexer_peek_token (parser->lexer);
24961 /* If we're looking at a `::' token then we're starting from the
24962 global namespace, not our current location. */
24963 if (token->type == CPP_SCOPE)
24965 /* Consume the `::' token. */
24966 cp_lexer_consume_token (parser->lexer);
24967 /* Set the SCOPE so that we know where to start the lookup. */
24968 parser->scope = global_namespace;
24969 parser->qualifying_scope = global_namespace;
24970 parser->object_scope = NULL_TREE;
24972 return parser->scope;
24974 else if (!current_scope_valid_p)
24976 parser->scope = NULL_TREE;
24977 parser->qualifying_scope = NULL_TREE;
24978 parser->object_scope = NULL_TREE;
24981 return NULL_TREE;
24984 /* Returns TRUE if the upcoming token sequence is the start of a
24985 constructor declarator. If FRIEND_P is true, the declarator is
24986 preceded by the `friend' specifier. */
24988 static bool
24989 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
24991 bool constructor_p;
24992 bool outside_class_specifier_p;
24993 tree nested_name_specifier;
24994 cp_token *next_token;
24996 /* The common case is that this is not a constructor declarator, so
24997 try to avoid doing lots of work if at all possible. It's not
24998 valid declare a constructor at function scope. */
24999 if (parser->in_function_body)
25000 return false;
25001 /* And only certain tokens can begin a constructor declarator. */
25002 next_token = cp_lexer_peek_token (parser->lexer);
25003 if (next_token->type != CPP_NAME
25004 && next_token->type != CPP_SCOPE
25005 && next_token->type != CPP_NESTED_NAME_SPECIFIER
25006 && next_token->type != CPP_TEMPLATE_ID)
25007 return false;
25009 /* Parse tentatively; we are going to roll back all of the tokens
25010 consumed here. */
25011 cp_parser_parse_tentatively (parser);
25012 /* Assume that we are looking at a constructor declarator. */
25013 constructor_p = true;
25015 /* Look for the optional `::' operator. */
25016 cp_parser_global_scope_opt (parser,
25017 /*current_scope_valid_p=*/false);
25018 /* Look for the nested-name-specifier. */
25019 nested_name_specifier
25020 = (cp_parser_nested_name_specifier_opt (parser,
25021 /*typename_keyword_p=*/false,
25022 /*check_dependency_p=*/false,
25023 /*type_p=*/false,
25024 /*is_declaration=*/false));
25026 outside_class_specifier_p = (!at_class_scope_p ()
25027 || !TYPE_BEING_DEFINED (current_class_type)
25028 || friend_p);
25030 /* Outside of a class-specifier, there must be a
25031 nested-name-specifier. */
25032 if (!nested_name_specifier && outside_class_specifier_p)
25033 constructor_p = false;
25034 else if (nested_name_specifier == error_mark_node)
25035 constructor_p = false;
25037 /* If we have a class scope, this is easy; DR 147 says that S::S always
25038 names the constructor, and no other qualified name could. */
25039 if (constructor_p && nested_name_specifier
25040 && CLASS_TYPE_P (nested_name_specifier))
25042 tree id = cp_parser_unqualified_id (parser,
25043 /*template_keyword_p=*/false,
25044 /*check_dependency_p=*/false,
25045 /*declarator_p=*/true,
25046 /*optional_p=*/false);
25047 if (is_overloaded_fn (id))
25048 id = DECL_NAME (get_first_fn (id));
25049 if (!constructor_name_p (id, nested_name_specifier))
25050 constructor_p = false;
25052 /* If we still think that this might be a constructor-declarator,
25053 look for a class-name. */
25054 else if (constructor_p)
25056 /* If we have:
25058 template <typename T> struct S {
25059 S();
25062 we must recognize that the nested `S' names a class. */
25063 tree type_decl;
25064 type_decl = cp_parser_class_name (parser,
25065 /*typename_keyword_p=*/false,
25066 /*template_keyword_p=*/false,
25067 none_type,
25068 /*check_dependency_p=*/false,
25069 /*class_head_p=*/false,
25070 /*is_declaration=*/false);
25071 /* If there was no class-name, then this is not a constructor.
25072 Otherwise, if we are in a class-specifier and we aren't
25073 handling a friend declaration, check that its type matches
25074 current_class_type (c++/38313). Note: error_mark_node
25075 is left alone for error recovery purposes. */
25076 constructor_p = (!cp_parser_error_occurred (parser)
25077 && (outside_class_specifier_p
25078 || type_decl == error_mark_node
25079 || same_type_p (current_class_type,
25080 TREE_TYPE (type_decl))));
25082 /* If we're still considering a constructor, we have to see a `(',
25083 to begin the parameter-declaration-clause, followed by either a
25084 `)', an `...', or a decl-specifier. We need to check for a
25085 type-specifier to avoid being fooled into thinking that:
25087 S (f) (int);
25089 is a constructor. (It is actually a function named `f' that
25090 takes one parameter (of type `int') and returns a value of type
25091 `S'. */
25092 if (constructor_p
25093 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25094 constructor_p = false;
25096 if (constructor_p
25097 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
25098 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
25099 /* A parameter declaration begins with a decl-specifier,
25100 which is either the "attribute" keyword, a storage class
25101 specifier, or (usually) a type-specifier. */
25102 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
25104 tree type;
25105 tree pushed_scope = NULL_TREE;
25106 unsigned saved_num_template_parameter_lists;
25108 /* Names appearing in the type-specifier should be looked up
25109 in the scope of the class. */
25110 if (current_class_type)
25111 type = NULL_TREE;
25112 else
25114 type = TREE_TYPE (type_decl);
25115 if (TREE_CODE (type) == TYPENAME_TYPE)
25117 type = resolve_typename_type (type,
25118 /*only_current_p=*/false);
25119 if (TREE_CODE (type) == TYPENAME_TYPE)
25121 cp_parser_abort_tentative_parse (parser);
25122 return false;
25125 pushed_scope = push_scope (type);
25128 /* Inside the constructor parameter list, surrounding
25129 template-parameter-lists do not apply. */
25130 saved_num_template_parameter_lists
25131 = parser->num_template_parameter_lists;
25132 parser->num_template_parameter_lists = 0;
25134 /* Look for the type-specifier. */
25135 cp_parser_type_specifier (parser,
25136 CP_PARSER_FLAGS_NONE,
25137 /*decl_specs=*/NULL,
25138 /*is_declarator=*/true,
25139 /*declares_class_or_enum=*/NULL,
25140 /*is_cv_qualifier=*/NULL);
25142 parser->num_template_parameter_lists
25143 = saved_num_template_parameter_lists;
25145 /* Leave the scope of the class. */
25146 if (pushed_scope)
25147 pop_scope (pushed_scope);
25149 constructor_p = !cp_parser_error_occurred (parser);
25153 /* We did not really want to consume any tokens. */
25154 cp_parser_abort_tentative_parse (parser);
25156 return constructor_p;
25159 /* Parse the definition of the function given by the DECL_SPECIFIERS,
25160 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
25161 they must be performed once we are in the scope of the function.
25163 Returns the function defined. */
25165 static tree
25166 cp_parser_function_definition_from_specifiers_and_declarator
25167 (cp_parser* parser,
25168 cp_decl_specifier_seq *decl_specifiers,
25169 tree attributes,
25170 const cp_declarator *declarator)
25172 tree fn;
25173 bool success_p;
25175 /* Begin the function-definition. */
25176 success_p = start_function (decl_specifiers, declarator, attributes);
25178 /* The things we're about to see are not directly qualified by any
25179 template headers we've seen thus far. */
25180 reset_specialization ();
25182 /* If there were names looked up in the decl-specifier-seq that we
25183 did not check, check them now. We must wait until we are in the
25184 scope of the function to perform the checks, since the function
25185 might be a friend. */
25186 perform_deferred_access_checks (tf_warning_or_error);
25188 if (success_p)
25190 cp_finalize_omp_declare_simd (parser, current_function_decl);
25191 parser->omp_declare_simd = NULL;
25192 cp_finalize_oacc_routine (parser, current_function_decl, true);
25193 parser->oacc_routine = NULL;
25196 if (!success_p)
25198 /* Skip the entire function. */
25199 cp_parser_skip_to_end_of_block_or_statement (parser);
25200 fn = error_mark_node;
25202 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
25204 /* Seen already, skip it. An error message has already been output. */
25205 cp_parser_skip_to_end_of_block_or_statement (parser);
25206 fn = current_function_decl;
25207 current_function_decl = NULL_TREE;
25208 /* If this is a function from a class, pop the nested class. */
25209 if (current_class_name)
25210 pop_nested_class ();
25212 else
25214 timevar_id_t tv;
25215 if (DECL_DECLARED_INLINE_P (current_function_decl))
25216 tv = TV_PARSE_INLINE;
25217 else
25218 tv = TV_PARSE_FUNC;
25219 timevar_push (tv);
25220 fn = cp_parser_function_definition_after_declarator (parser,
25221 /*inline_p=*/false);
25222 timevar_pop (tv);
25225 return fn;
25228 /* Parse the part of a function-definition that follows the
25229 declarator. INLINE_P is TRUE iff this function is an inline
25230 function defined within a class-specifier.
25232 Returns the function defined. */
25234 static tree
25235 cp_parser_function_definition_after_declarator (cp_parser* parser,
25236 bool inline_p)
25238 tree fn;
25239 bool ctor_initializer_p = false;
25240 bool saved_in_unbraced_linkage_specification_p;
25241 bool saved_in_function_body;
25242 unsigned saved_num_template_parameter_lists;
25243 cp_token *token;
25244 bool fully_implicit_function_template_p
25245 = parser->fully_implicit_function_template_p;
25246 parser->fully_implicit_function_template_p = false;
25247 tree implicit_template_parms
25248 = parser->implicit_template_parms;
25249 parser->implicit_template_parms = 0;
25250 cp_binding_level* implicit_template_scope
25251 = parser->implicit_template_scope;
25252 parser->implicit_template_scope = 0;
25254 saved_in_function_body = parser->in_function_body;
25255 parser->in_function_body = true;
25256 /* If the next token is `return', then the code may be trying to
25257 make use of the "named return value" extension that G++ used to
25258 support. */
25259 token = cp_lexer_peek_token (parser->lexer);
25260 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
25262 /* Consume the `return' keyword. */
25263 cp_lexer_consume_token (parser->lexer);
25264 /* Look for the identifier that indicates what value is to be
25265 returned. */
25266 cp_parser_identifier (parser);
25267 /* Issue an error message. */
25268 error_at (token->location,
25269 "named return values are no longer supported");
25270 /* Skip tokens until we reach the start of the function body. */
25271 while (true)
25273 cp_token *token = cp_lexer_peek_token (parser->lexer);
25274 if (token->type == CPP_OPEN_BRACE
25275 || token->type == CPP_EOF
25276 || token->type == CPP_PRAGMA_EOL)
25277 break;
25278 cp_lexer_consume_token (parser->lexer);
25281 /* The `extern' in `extern "C" void f () { ... }' does not apply to
25282 anything declared inside `f'. */
25283 saved_in_unbraced_linkage_specification_p
25284 = parser->in_unbraced_linkage_specification_p;
25285 parser->in_unbraced_linkage_specification_p = false;
25286 /* Inside the function, surrounding template-parameter-lists do not
25287 apply. */
25288 saved_num_template_parameter_lists
25289 = parser->num_template_parameter_lists;
25290 parser->num_template_parameter_lists = 0;
25292 start_lambda_scope (current_function_decl);
25294 /* If the next token is `try', `__transaction_atomic', or
25295 `__transaction_relaxed`, then we are looking at either function-try-block
25296 or function-transaction-block. Note that all of these include the
25297 function-body. */
25298 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
25299 ctor_initializer_p = cp_parser_function_transaction (parser,
25300 RID_TRANSACTION_ATOMIC);
25301 else if (cp_lexer_next_token_is_keyword (parser->lexer,
25302 RID_TRANSACTION_RELAXED))
25303 ctor_initializer_p = cp_parser_function_transaction (parser,
25304 RID_TRANSACTION_RELAXED);
25305 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
25306 ctor_initializer_p = cp_parser_function_try_block (parser);
25307 else
25308 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
25309 (parser, /*in_function_try_block=*/false);
25311 finish_lambda_scope ();
25313 /* Finish the function. */
25314 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
25315 (inline_p ? 2 : 0));
25316 /* Generate code for it, if necessary. */
25317 expand_or_defer_fn (fn);
25318 /* Restore the saved values. */
25319 parser->in_unbraced_linkage_specification_p
25320 = saved_in_unbraced_linkage_specification_p;
25321 parser->num_template_parameter_lists
25322 = saved_num_template_parameter_lists;
25323 parser->in_function_body = saved_in_function_body;
25325 parser->fully_implicit_function_template_p
25326 = fully_implicit_function_template_p;
25327 parser->implicit_template_parms
25328 = implicit_template_parms;
25329 parser->implicit_template_scope
25330 = implicit_template_scope;
25332 if (parser->fully_implicit_function_template_p)
25333 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
25335 return fn;
25338 /* Parse a template-declaration body (following argument list). */
25340 static void
25341 cp_parser_template_declaration_after_parameters (cp_parser* parser,
25342 tree parameter_list,
25343 bool member_p)
25345 tree decl = NULL_TREE;
25346 bool friend_p = false;
25348 /* We just processed one more parameter list. */
25349 ++parser->num_template_parameter_lists;
25351 /* Get the deferred access checks from the parameter list. These
25352 will be checked once we know what is being declared, as for a
25353 member template the checks must be performed in the scope of the
25354 class containing the member. */
25355 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
25357 /* Tentatively parse for a new template parameter list, which can either be
25358 the template keyword or a template introduction. */
25359 if (cp_parser_template_declaration_after_export (parser, member_p))
25360 /* OK */;
25361 else if (cxx_dialect >= cxx11
25362 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25363 decl = cp_parser_alias_declaration (parser);
25364 else
25366 /* There are no access checks when parsing a template, as we do not
25367 know if a specialization will be a friend. */
25368 push_deferring_access_checks (dk_no_check);
25369 cp_token *token = cp_lexer_peek_token (parser->lexer);
25370 decl = cp_parser_single_declaration (parser,
25371 checks,
25372 member_p,
25373 /*explicit_specialization_p=*/false,
25374 &friend_p);
25375 pop_deferring_access_checks ();
25377 /* If this is a member template declaration, let the front
25378 end know. */
25379 if (member_p && !friend_p && decl)
25381 if (TREE_CODE (decl) == TYPE_DECL)
25382 cp_parser_check_access_in_redeclaration (decl, token->location);
25384 decl = finish_member_template_decl (decl);
25386 else if (friend_p && decl
25387 && DECL_DECLARES_TYPE_P (decl))
25388 make_friend_class (current_class_type, TREE_TYPE (decl),
25389 /*complain=*/true);
25391 /* We are done with the current parameter list. */
25392 --parser->num_template_parameter_lists;
25394 pop_deferring_access_checks ();
25396 /* Finish up. */
25397 finish_template_decl (parameter_list);
25399 /* Check the template arguments for a literal operator template. */
25400 if (decl
25401 && DECL_DECLARES_FUNCTION_P (decl)
25402 && UDLIT_OPER_P (DECL_NAME (decl)))
25404 bool ok = true;
25405 if (parameter_list == NULL_TREE)
25406 ok = false;
25407 else
25409 int num_parms = TREE_VEC_LENGTH (parameter_list);
25410 if (num_parms == 1)
25412 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
25413 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
25414 if (TREE_TYPE (parm) != char_type_node
25415 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
25416 ok = false;
25418 else if (num_parms == 2 && cxx_dialect >= cxx14)
25420 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
25421 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
25422 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
25423 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
25424 if (TREE_TYPE (parm) != TREE_TYPE (type)
25425 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
25426 ok = false;
25428 else
25429 ok = false;
25431 if (!ok)
25433 if (cxx_dialect >= cxx14)
25434 error ("literal operator template %qD has invalid parameter list."
25435 " Expected non-type template argument pack <char...>"
25436 " or <typename CharT, CharT...>",
25437 decl);
25438 else
25439 error ("literal operator template %qD has invalid parameter list."
25440 " Expected non-type template argument pack <char...>",
25441 decl);
25445 /* Register member declarations. */
25446 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
25447 finish_member_declaration (decl);
25448 /* If DECL is a function template, we must return to parse it later.
25449 (Even though there is no definition, there might be default
25450 arguments that need handling.) */
25451 if (member_p && decl
25452 && DECL_DECLARES_FUNCTION_P (decl))
25453 vec_safe_push (unparsed_funs_with_definitions, decl);
25456 /* Parse a template introduction header for a template-declaration. Returns
25457 false if tentative parse fails. */
25459 static bool
25460 cp_parser_template_introduction (cp_parser* parser, bool member_p)
25462 cp_parser_parse_tentatively (parser);
25464 tree saved_scope = parser->scope;
25465 tree saved_object_scope = parser->object_scope;
25466 tree saved_qualifying_scope = parser->qualifying_scope;
25468 /* Look for the optional `::' operator. */
25469 cp_parser_global_scope_opt (parser,
25470 /*current_scope_valid_p=*/false);
25471 /* Look for the nested-name-specifier. */
25472 cp_parser_nested_name_specifier_opt (parser,
25473 /*typename_keyword_p=*/false,
25474 /*check_dependency_p=*/true,
25475 /*type_p=*/false,
25476 /*is_declaration=*/false);
25478 cp_token *token = cp_lexer_peek_token (parser->lexer);
25479 tree concept_name = cp_parser_identifier (parser);
25481 /* Look up the concept for which we will be matching
25482 template parameters. */
25483 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
25484 token->location);
25485 parser->scope = saved_scope;
25486 parser->object_scope = saved_object_scope;
25487 parser->qualifying_scope = saved_qualifying_scope;
25489 if (concept_name == error_mark_node)
25490 cp_parser_simulate_error (parser);
25492 /* Look for opening brace for introduction. */
25493 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
25495 if (!cp_parser_parse_definitely (parser))
25496 return false;
25498 push_deferring_access_checks (dk_deferred);
25500 /* Build vector of placeholder parameters and grab
25501 matching identifiers. */
25502 tree introduction_list = cp_parser_introduction_list (parser);
25504 /* The introduction-list shall not be empty. */
25505 int nargs = TREE_VEC_LENGTH (introduction_list);
25506 if (nargs == 0)
25508 error ("empty introduction-list");
25509 return true;
25512 /* Look for closing brace for introduction. */
25513 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25514 return true;
25516 if (tmpl_decl == error_mark_node)
25518 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
25519 token->location);
25520 return true;
25523 /* Build and associate the constraint. */
25524 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
25525 if (parms && parms != error_mark_node)
25527 cp_parser_template_declaration_after_parameters (parser, parms,
25528 member_p);
25529 return true;
25532 error_at (token->location, "no matching concept for template-introduction");
25533 return true;
25536 /* Parse a normal template-declaration following the template keyword. */
25538 static void
25539 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
25541 tree parameter_list;
25542 bool need_lang_pop;
25543 location_t location = input_location;
25545 /* Look for the `<' token. */
25546 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
25547 return;
25548 if (at_class_scope_p () && current_function_decl)
25550 /* 14.5.2.2 [temp.mem]
25552 A local class shall not have member templates. */
25553 error_at (location,
25554 "invalid declaration of member template in local class");
25555 cp_parser_skip_to_end_of_block_or_statement (parser);
25556 return;
25558 /* [temp]
25560 A template ... shall not have C linkage. */
25561 if (current_lang_name == lang_name_c)
25563 error_at (location, "template with C linkage");
25564 /* Give it C++ linkage to avoid confusing other parts of the
25565 front end. */
25566 push_lang_context (lang_name_cplusplus);
25567 need_lang_pop = true;
25569 else
25570 need_lang_pop = false;
25572 /* We cannot perform access checks on the template parameter
25573 declarations until we know what is being declared, just as we
25574 cannot check the decl-specifier list. */
25575 push_deferring_access_checks (dk_deferred);
25577 /* If the next token is `>', then we have an invalid
25578 specialization. Rather than complain about an invalid template
25579 parameter, issue an error message here. */
25580 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
25582 cp_parser_error (parser, "invalid explicit specialization");
25583 begin_specialization ();
25584 parameter_list = NULL_TREE;
25586 else
25588 /* Parse the template parameters. */
25589 parameter_list = cp_parser_template_parameter_list (parser);
25592 /* Look for the `>'. */
25593 cp_parser_skip_to_end_of_template_parameter_list (parser);
25595 /* Manage template requirements */
25596 if (flag_concepts)
25598 tree reqs = get_shorthand_constraints (current_template_parms);
25599 if (tree r = cp_parser_requires_clause_opt (parser))
25600 reqs = conjoin_constraints (reqs, make_predicate_constraint (r));
25601 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
25604 cp_parser_template_declaration_after_parameters (parser, parameter_list,
25605 member_p);
25607 /* For the erroneous case of a template with C linkage, we pushed an
25608 implicit C++ linkage scope; exit that scope now. */
25609 if (need_lang_pop)
25610 pop_lang_context ();
25613 /* Parse a template-declaration, assuming that the `export' (and
25614 `extern') keywords, if present, has already been scanned. MEMBER_P
25615 is as for cp_parser_template_declaration. */
25617 static bool
25618 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
25620 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25622 cp_lexer_consume_token (parser->lexer);
25623 cp_parser_explicit_template_declaration (parser, member_p);
25624 return true;
25626 else if (flag_concepts)
25627 return cp_parser_template_introduction (parser, member_p);
25629 return false;
25632 /* Perform the deferred access checks from a template-parameter-list.
25633 CHECKS is a TREE_LIST of access checks, as returned by
25634 get_deferred_access_checks. */
25636 static void
25637 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
25639 ++processing_template_parmlist;
25640 perform_access_checks (checks, tf_warning_or_error);
25641 --processing_template_parmlist;
25644 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
25645 `function-definition' sequence that follows a template header.
25646 If MEMBER_P is true, this declaration appears in a class scope.
25648 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
25649 *FRIEND_P is set to TRUE iff the declaration is a friend. */
25651 static tree
25652 cp_parser_single_declaration (cp_parser* parser,
25653 vec<deferred_access_check, va_gc> *checks,
25654 bool member_p,
25655 bool explicit_specialization_p,
25656 bool* friend_p)
25658 int declares_class_or_enum;
25659 tree decl = NULL_TREE;
25660 cp_decl_specifier_seq decl_specifiers;
25661 bool function_definition_p = false;
25662 cp_token *decl_spec_token_start;
25664 /* This function is only used when processing a template
25665 declaration. */
25666 gcc_assert (innermost_scope_kind () == sk_template_parms
25667 || innermost_scope_kind () == sk_template_spec);
25669 /* Defer access checks until we know what is being declared. */
25670 push_deferring_access_checks (dk_deferred);
25672 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
25673 alternative. */
25674 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
25675 cp_parser_decl_specifier_seq (parser,
25676 CP_PARSER_FLAGS_OPTIONAL,
25677 &decl_specifiers,
25678 &declares_class_or_enum);
25679 if (friend_p)
25680 *friend_p = cp_parser_friend_p (&decl_specifiers);
25682 /* There are no template typedefs. */
25683 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
25685 error_at (decl_spec_token_start->location,
25686 "template declaration of %<typedef%>");
25687 decl = error_mark_node;
25690 /* Gather up the access checks that occurred the
25691 decl-specifier-seq. */
25692 stop_deferring_access_checks ();
25694 /* Check for the declaration of a template class. */
25695 if (declares_class_or_enum)
25697 if (cp_parser_declares_only_class_p (parser)
25698 || (declares_class_or_enum & 2))
25700 // If this is a declaration, but not a definition, associate
25701 // any constraints with the type declaration. Constraints
25702 // are associated with definitions in cp_parser_class_specifier.
25703 if (declares_class_or_enum == 1)
25704 associate_classtype_constraints (decl_specifiers.type);
25706 decl = shadow_tag (&decl_specifiers);
25708 /* In this case:
25710 struct C {
25711 friend template <typename T> struct A<T>::B;
25714 A<T>::B will be represented by a TYPENAME_TYPE, and
25715 therefore not recognized by shadow_tag. */
25716 if (friend_p && *friend_p
25717 && !decl
25718 && decl_specifiers.type
25719 && TYPE_P (decl_specifiers.type))
25720 decl = decl_specifiers.type;
25722 if (decl && decl != error_mark_node)
25723 decl = TYPE_NAME (decl);
25724 else
25725 decl = error_mark_node;
25727 /* Perform access checks for template parameters. */
25728 cp_parser_perform_template_parameter_access_checks (checks);
25730 /* Give a helpful diagnostic for
25731 template <class T> struct A { } a;
25732 if we aren't already recovering from an error. */
25733 if (!cp_parser_declares_only_class_p (parser)
25734 && !seen_error ())
25736 error_at (cp_lexer_peek_token (parser->lexer)->location,
25737 "a class template declaration must not declare "
25738 "anything else");
25739 cp_parser_skip_to_end_of_block_or_statement (parser);
25740 goto out;
25745 /* Complain about missing 'typename' or other invalid type names. */
25746 if (!decl_specifiers.any_type_specifiers_p
25747 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
25749 /* cp_parser_parse_and_diagnose_invalid_type_name calls
25750 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
25751 the rest of this declaration. */
25752 decl = error_mark_node;
25753 goto out;
25756 /* If it's not a template class, try for a template function. If
25757 the next token is a `;', then this declaration does not declare
25758 anything. But, if there were errors in the decl-specifiers, then
25759 the error might well have come from an attempted class-specifier.
25760 In that case, there's no need to warn about a missing declarator. */
25761 if (!decl
25762 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
25763 || decl_specifiers.type != error_mark_node))
25765 decl = cp_parser_init_declarator (parser,
25766 &decl_specifiers,
25767 checks,
25768 /*function_definition_allowed_p=*/true,
25769 member_p,
25770 declares_class_or_enum,
25771 &function_definition_p,
25772 NULL, NULL);
25774 /* 7.1.1-1 [dcl.stc]
25776 A storage-class-specifier shall not be specified in an explicit
25777 specialization... */
25778 if (decl
25779 && explicit_specialization_p
25780 && decl_specifiers.storage_class != sc_none)
25782 error_at (decl_spec_token_start->location,
25783 "explicit template specialization cannot have a storage class");
25784 decl = error_mark_node;
25787 if (decl && VAR_P (decl))
25788 check_template_variable (decl);
25791 /* Look for a trailing `;' after the declaration. */
25792 if (!function_definition_p
25793 && (decl == error_mark_node
25794 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
25795 cp_parser_skip_to_end_of_block_or_statement (parser);
25797 out:
25798 pop_deferring_access_checks ();
25800 /* Clear any current qualification; whatever comes next is the start
25801 of something new. */
25802 parser->scope = NULL_TREE;
25803 parser->qualifying_scope = NULL_TREE;
25804 parser->object_scope = NULL_TREE;
25806 return decl;
25809 /* Parse a cast-expression that is not the operand of a unary "&". */
25811 static cp_expr
25812 cp_parser_simple_cast_expression (cp_parser *parser)
25814 return cp_parser_cast_expression (parser, /*address_p=*/false,
25815 /*cast_p=*/false, /*decltype*/false, NULL);
25818 /* Parse a functional cast to TYPE. Returns an expression
25819 representing the cast. */
25821 static cp_expr
25822 cp_parser_functional_cast (cp_parser* parser, tree type)
25824 vec<tree, va_gc> *vec;
25825 tree expression_list;
25826 cp_expr cast;
25827 bool nonconst_p;
25829 location_t start_loc = input_location;
25831 if (!type)
25832 type = error_mark_node;
25834 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25836 cp_lexer_set_source_position (parser->lexer);
25837 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25838 expression_list = cp_parser_braced_list (parser, &nonconst_p);
25839 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
25840 if (TREE_CODE (type) == TYPE_DECL)
25841 type = TREE_TYPE (type);
25843 cast = finish_compound_literal (type, expression_list,
25844 tf_warning_or_error);
25845 /* Create a location of the form:
25846 type_name{i, f}
25847 ^~~~~~~~~~~~~~~
25848 with caret == start at the start of the type name,
25849 finishing at the closing brace. */
25850 location_t finish_loc
25851 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
25852 location_t combined_loc = make_location (start_loc, start_loc,
25853 finish_loc);
25854 cast.set_location (combined_loc);
25855 return cast;
25859 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
25860 /*cast_p=*/true,
25861 /*allow_expansion_p=*/true,
25862 /*non_constant_p=*/NULL);
25863 if (vec == NULL)
25864 expression_list = error_mark_node;
25865 else
25867 expression_list = build_tree_list_vec (vec);
25868 release_tree_vector (vec);
25871 cast = build_functional_cast (type, expression_list,
25872 tf_warning_or_error);
25873 /* [expr.const]/1: In an integral constant expression "only type
25874 conversions to integral or enumeration type can be used". */
25875 if (TREE_CODE (type) == TYPE_DECL)
25876 type = TREE_TYPE (type);
25877 if (cast != error_mark_node
25878 && !cast_valid_in_integral_constant_expression_p (type)
25879 && cp_parser_non_integral_constant_expression (parser,
25880 NIC_CONSTRUCTOR))
25881 return error_mark_node;
25883 /* Create a location of the form:
25884 float(i)
25885 ^~~~~~~~
25886 with caret == start at the start of the type name,
25887 finishing at the closing paren. */
25888 location_t finish_loc
25889 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
25890 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
25891 cast.set_location (combined_loc);
25892 return cast;
25895 /* Save the tokens that make up the body of a member function defined
25896 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
25897 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
25898 specifiers applied to the declaration. Returns the FUNCTION_DECL
25899 for the member function. */
25901 static tree
25902 cp_parser_save_member_function_body (cp_parser* parser,
25903 cp_decl_specifier_seq *decl_specifiers,
25904 cp_declarator *declarator,
25905 tree attributes)
25907 cp_token *first;
25908 cp_token *last;
25909 tree fn;
25911 /* Create the FUNCTION_DECL. */
25912 fn = grokmethod (decl_specifiers, declarator, attributes);
25913 cp_finalize_omp_declare_simd (parser, fn);
25914 cp_finalize_oacc_routine (parser, fn, true);
25915 /* If something went badly wrong, bail out now. */
25916 if (fn == error_mark_node)
25918 /* If there's a function-body, skip it. */
25919 if (cp_parser_token_starts_function_definition_p
25920 (cp_lexer_peek_token (parser->lexer)))
25921 cp_parser_skip_to_end_of_block_or_statement (parser);
25922 return error_mark_node;
25925 /* Remember it, if there default args to post process. */
25926 cp_parser_save_default_args (parser, fn);
25928 /* Save away the tokens that make up the body of the
25929 function. */
25930 first = parser->lexer->next_token;
25931 /* Handle function try blocks. */
25932 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
25933 cp_lexer_consume_token (parser->lexer);
25934 /* We can have braced-init-list mem-initializers before the fn body. */
25935 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
25937 cp_lexer_consume_token (parser->lexer);
25938 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
25940 /* cache_group will stop after an un-nested { } pair, too. */
25941 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
25942 break;
25944 /* variadic mem-inits have ... after the ')'. */
25945 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25946 cp_lexer_consume_token (parser->lexer);
25949 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25950 /* Handle function try blocks. */
25951 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
25952 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25953 last = parser->lexer->next_token;
25955 /* Save away the inline definition; we will process it when the
25956 class is complete. */
25957 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
25958 DECL_PENDING_INLINE_P (fn) = 1;
25960 /* We need to know that this was defined in the class, so that
25961 friend templates are handled correctly. */
25962 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
25964 /* Add FN to the queue of functions to be parsed later. */
25965 vec_safe_push (unparsed_funs_with_definitions, fn);
25967 return fn;
25970 /* Save the tokens that make up the in-class initializer for a non-static
25971 data member. Returns a DEFAULT_ARG. */
25973 static tree
25974 cp_parser_save_nsdmi (cp_parser* parser)
25976 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
25979 /* Parse a template-argument-list, as well as the trailing ">" (but
25980 not the opening "<"). See cp_parser_template_argument_list for the
25981 return value. */
25983 static tree
25984 cp_parser_enclosed_template_argument_list (cp_parser* parser)
25986 tree arguments;
25987 tree saved_scope;
25988 tree saved_qualifying_scope;
25989 tree saved_object_scope;
25990 bool saved_greater_than_is_operator_p;
25991 int saved_unevaluated_operand;
25992 int saved_inhibit_evaluation_warnings;
25994 /* [temp.names]
25996 When parsing a template-id, the first non-nested `>' is taken as
25997 the end of the template-argument-list rather than a greater-than
25998 operator. */
25999 saved_greater_than_is_operator_p
26000 = parser->greater_than_is_operator_p;
26001 parser->greater_than_is_operator_p = false;
26002 /* Parsing the argument list may modify SCOPE, so we save it
26003 here. */
26004 saved_scope = parser->scope;
26005 saved_qualifying_scope = parser->qualifying_scope;
26006 saved_object_scope = parser->object_scope;
26007 /* We need to evaluate the template arguments, even though this
26008 template-id may be nested within a "sizeof". */
26009 saved_unevaluated_operand = cp_unevaluated_operand;
26010 cp_unevaluated_operand = 0;
26011 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
26012 c_inhibit_evaluation_warnings = 0;
26013 /* Parse the template-argument-list itself. */
26014 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
26015 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26016 arguments = NULL_TREE;
26017 else
26018 arguments = cp_parser_template_argument_list (parser);
26019 /* Look for the `>' that ends the template-argument-list. If we find
26020 a '>>' instead, it's probably just a typo. */
26021 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26023 if (cxx_dialect != cxx98)
26025 /* In C++0x, a `>>' in a template argument list or cast
26026 expression is considered to be two separate `>'
26027 tokens. So, change the current token to a `>', but don't
26028 consume it: it will be consumed later when the outer
26029 template argument list (or cast expression) is parsed.
26030 Note that this replacement of `>' for `>>' is necessary
26031 even if we are parsing tentatively: in the tentative
26032 case, after calling
26033 cp_parser_enclosed_template_argument_list we will always
26034 throw away all of the template arguments and the first
26035 closing `>', either because the template argument list
26036 was erroneous or because we are replacing those tokens
26037 with a CPP_TEMPLATE_ID token. The second `>' (which will
26038 not have been thrown away) is needed either to close an
26039 outer template argument list or to complete a new-style
26040 cast. */
26041 cp_token *token = cp_lexer_peek_token (parser->lexer);
26042 token->type = CPP_GREATER;
26044 else if (!saved_greater_than_is_operator_p)
26046 /* If we're in a nested template argument list, the '>>' has
26047 to be a typo for '> >'. We emit the error message, but we
26048 continue parsing and we push a '>' as next token, so that
26049 the argument list will be parsed correctly. Note that the
26050 global source location is still on the token before the
26051 '>>', so we need to say explicitly where we want it. */
26052 cp_token *token = cp_lexer_peek_token (parser->lexer);
26053 error_at (token->location, "%<>>%> should be %<> >%> "
26054 "within a nested template argument list");
26056 token->type = CPP_GREATER;
26058 else
26060 /* If this is not a nested template argument list, the '>>'
26061 is a typo for '>'. Emit an error message and continue.
26062 Same deal about the token location, but here we can get it
26063 right by consuming the '>>' before issuing the diagnostic. */
26064 cp_token *token = cp_lexer_consume_token (parser->lexer);
26065 error_at (token->location,
26066 "spurious %<>>%>, use %<>%> to terminate "
26067 "a template argument list");
26070 else
26071 cp_parser_skip_to_end_of_template_parameter_list (parser);
26072 /* The `>' token might be a greater-than operator again now. */
26073 parser->greater_than_is_operator_p
26074 = saved_greater_than_is_operator_p;
26075 /* Restore the SAVED_SCOPE. */
26076 parser->scope = saved_scope;
26077 parser->qualifying_scope = saved_qualifying_scope;
26078 parser->object_scope = saved_object_scope;
26079 cp_unevaluated_operand = saved_unevaluated_operand;
26080 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
26082 return arguments;
26085 /* MEMBER_FUNCTION is a member function, or a friend. If default
26086 arguments, or the body of the function have not yet been parsed,
26087 parse them now. */
26089 static void
26090 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
26092 timevar_push (TV_PARSE_INMETH);
26093 /* If this member is a template, get the underlying
26094 FUNCTION_DECL. */
26095 if (DECL_FUNCTION_TEMPLATE_P (member_function))
26096 member_function = DECL_TEMPLATE_RESULT (member_function);
26098 /* There should not be any class definitions in progress at this
26099 point; the bodies of members are only parsed outside of all class
26100 definitions. */
26101 gcc_assert (parser->num_classes_being_defined == 0);
26102 /* While we're parsing the member functions we might encounter more
26103 classes. We want to handle them right away, but we don't want
26104 them getting mixed up with functions that are currently in the
26105 queue. */
26106 push_unparsed_function_queues (parser);
26108 /* Make sure that any template parameters are in scope. */
26109 maybe_begin_member_template_processing (member_function);
26111 /* If the body of the function has not yet been parsed, parse it
26112 now. */
26113 if (DECL_PENDING_INLINE_P (member_function))
26115 tree function_scope;
26116 cp_token_cache *tokens;
26118 /* The function is no longer pending; we are processing it. */
26119 tokens = DECL_PENDING_INLINE_INFO (member_function);
26120 DECL_PENDING_INLINE_INFO (member_function) = NULL;
26121 DECL_PENDING_INLINE_P (member_function) = 0;
26123 /* If this is a local class, enter the scope of the containing
26124 function. */
26125 function_scope = current_function_decl;
26126 if (function_scope)
26127 push_function_context ();
26129 /* Push the body of the function onto the lexer stack. */
26130 cp_parser_push_lexer_for_tokens (parser, tokens);
26132 /* Let the front end know that we going to be defining this
26133 function. */
26134 start_preparsed_function (member_function, NULL_TREE,
26135 SF_PRE_PARSED | SF_INCLASS_INLINE);
26137 /* Don't do access checking if it is a templated function. */
26138 if (processing_template_decl)
26139 push_deferring_access_checks (dk_no_check);
26141 /* #pragma omp declare reduction needs special parsing. */
26142 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
26144 parser->lexer->in_pragma = true;
26145 cp_parser_omp_declare_reduction_exprs (member_function, parser);
26146 finish_function (/*inline*/2);
26147 cp_check_omp_declare_reduction (member_function);
26149 else
26150 /* Now, parse the body of the function. */
26151 cp_parser_function_definition_after_declarator (parser,
26152 /*inline_p=*/true);
26154 if (processing_template_decl)
26155 pop_deferring_access_checks ();
26157 /* Leave the scope of the containing function. */
26158 if (function_scope)
26159 pop_function_context ();
26160 cp_parser_pop_lexer (parser);
26163 /* Remove any template parameters from the symbol table. */
26164 maybe_end_member_template_processing ();
26166 /* Restore the queue. */
26167 pop_unparsed_function_queues (parser);
26168 timevar_pop (TV_PARSE_INMETH);
26171 /* If DECL contains any default args, remember it on the unparsed
26172 functions queue. */
26174 static void
26175 cp_parser_save_default_args (cp_parser* parser, tree decl)
26177 tree probe;
26179 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
26180 probe;
26181 probe = TREE_CHAIN (probe))
26182 if (TREE_PURPOSE (probe))
26184 cp_default_arg_entry entry = {current_class_type, decl};
26185 vec_safe_push (unparsed_funs_with_default_args, entry);
26186 break;
26190 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
26191 which is either a FIELD_DECL or PARM_DECL. Parse it and return
26192 the result. For a PARM_DECL, PARMTYPE is the corresponding type
26193 from the parameter-type-list. */
26195 static tree
26196 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
26197 tree default_arg, tree parmtype)
26199 cp_token_cache *tokens;
26200 tree parsed_arg;
26201 bool dummy;
26203 if (default_arg == error_mark_node)
26204 return error_mark_node;
26206 /* Push the saved tokens for the default argument onto the parser's
26207 lexer stack. */
26208 tokens = DEFARG_TOKENS (default_arg);
26209 cp_parser_push_lexer_for_tokens (parser, tokens);
26211 start_lambda_scope (decl);
26213 /* Parse the default argument. */
26214 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
26215 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
26216 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26218 finish_lambda_scope ();
26220 if (parsed_arg == error_mark_node)
26221 cp_parser_skip_to_end_of_statement (parser);
26223 if (!processing_template_decl)
26225 /* In a non-template class, check conversions now. In a template,
26226 we'll wait and instantiate these as needed. */
26227 if (TREE_CODE (decl) == PARM_DECL)
26228 parsed_arg = check_default_argument (parmtype, parsed_arg,
26229 tf_warning_or_error);
26230 else
26231 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
26234 /* If the token stream has not been completely used up, then
26235 there was extra junk after the end of the default
26236 argument. */
26237 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26239 if (TREE_CODE (decl) == PARM_DECL)
26240 cp_parser_error (parser, "expected %<,%>");
26241 else
26242 cp_parser_error (parser, "expected %<;%>");
26245 /* Revert to the main lexer. */
26246 cp_parser_pop_lexer (parser);
26248 return parsed_arg;
26251 /* FIELD is a non-static data member with an initializer which we saved for
26252 later; parse it now. */
26254 static void
26255 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
26257 tree def;
26259 maybe_begin_member_template_processing (field);
26261 push_unparsed_function_queues (parser);
26262 def = cp_parser_late_parse_one_default_arg (parser, field,
26263 DECL_INITIAL (field),
26264 NULL_TREE);
26265 pop_unparsed_function_queues (parser);
26267 maybe_end_member_template_processing ();
26269 DECL_INITIAL (field) = def;
26272 /* FN is a FUNCTION_DECL which may contains a parameter with an
26273 unparsed DEFAULT_ARG. Parse the default args now. This function
26274 assumes that the current scope is the scope in which the default
26275 argument should be processed. */
26277 static void
26278 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
26280 bool saved_local_variables_forbidden_p;
26281 tree parm, parmdecl;
26283 /* While we're parsing the default args, we might (due to the
26284 statement expression extension) encounter more classes. We want
26285 to handle them right away, but we don't want them getting mixed
26286 up with default args that are currently in the queue. */
26287 push_unparsed_function_queues (parser);
26289 /* Local variable names (and the `this' keyword) may not appear
26290 in a default argument. */
26291 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
26292 parser->local_variables_forbidden_p = true;
26294 push_defarg_context (fn);
26296 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
26297 parmdecl = DECL_ARGUMENTS (fn);
26298 parm && parm != void_list_node;
26299 parm = TREE_CHAIN (parm),
26300 parmdecl = DECL_CHAIN (parmdecl))
26302 tree default_arg = TREE_PURPOSE (parm);
26303 tree parsed_arg;
26304 vec<tree, va_gc> *insts;
26305 tree copy;
26306 unsigned ix;
26308 if (!default_arg)
26309 continue;
26311 if (TREE_CODE (default_arg) != DEFAULT_ARG)
26312 /* This can happen for a friend declaration for a function
26313 already declared with default arguments. */
26314 continue;
26316 parsed_arg
26317 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
26318 default_arg,
26319 TREE_VALUE (parm));
26320 if (parsed_arg == error_mark_node)
26322 continue;
26325 TREE_PURPOSE (parm) = parsed_arg;
26327 /* Update any instantiations we've already created. */
26328 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
26329 vec_safe_iterate (insts, ix, &copy); ix++)
26330 TREE_PURPOSE (copy) = parsed_arg;
26333 pop_defarg_context ();
26335 /* Make sure no default arg is missing. */
26336 check_default_args (fn);
26338 /* Restore the state of local_variables_forbidden_p. */
26339 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
26341 /* Restore the queue. */
26342 pop_unparsed_function_queues (parser);
26345 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
26347 sizeof ... ( identifier )
26349 where the 'sizeof' token has already been consumed. */
26351 static tree
26352 cp_parser_sizeof_pack (cp_parser *parser)
26354 /* Consume the `...'. */
26355 cp_lexer_consume_token (parser->lexer);
26356 maybe_warn_variadic_templates ();
26358 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
26359 if (paren)
26360 cp_lexer_consume_token (parser->lexer);
26361 else
26362 permerror (cp_lexer_peek_token (parser->lexer)->location,
26363 "%<sizeof...%> argument must be surrounded by parentheses");
26365 cp_token *token = cp_lexer_peek_token (parser->lexer);
26366 tree name = cp_parser_identifier (parser);
26367 if (name == error_mark_node)
26368 return error_mark_node;
26369 /* The name is not qualified. */
26370 parser->scope = NULL_TREE;
26371 parser->qualifying_scope = NULL_TREE;
26372 parser->object_scope = NULL_TREE;
26373 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
26374 if (expr == error_mark_node)
26375 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
26376 token->location);
26377 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
26378 expr = TREE_TYPE (expr);
26379 else if (TREE_CODE (expr) == CONST_DECL)
26380 expr = DECL_INITIAL (expr);
26381 expr = make_pack_expansion (expr);
26382 PACK_EXPANSION_SIZEOF_P (expr) = true;
26384 if (paren)
26385 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26387 return expr;
26390 /* Parse the operand of `sizeof' (or a similar operator). Returns
26391 either a TYPE or an expression, depending on the form of the
26392 input. The KEYWORD indicates which kind of expression we have
26393 encountered. */
26395 static tree
26396 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
26398 tree expr = NULL_TREE;
26399 const char *saved_message;
26400 char *tmp;
26401 bool saved_integral_constant_expression_p;
26402 bool saved_non_integral_constant_expression_p;
26404 /* If it's a `...', then we are computing the length of a parameter
26405 pack. */
26406 if (keyword == RID_SIZEOF
26407 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26408 return cp_parser_sizeof_pack (parser);
26410 /* Types cannot be defined in a `sizeof' expression. Save away the
26411 old message. */
26412 saved_message = parser->type_definition_forbidden_message;
26413 /* And create the new one. */
26414 tmp = concat ("types may not be defined in %<",
26415 IDENTIFIER_POINTER (ridpointers[keyword]),
26416 "%> expressions", NULL);
26417 parser->type_definition_forbidden_message = tmp;
26419 /* The restrictions on constant-expressions do not apply inside
26420 sizeof expressions. */
26421 saved_integral_constant_expression_p
26422 = parser->integral_constant_expression_p;
26423 saved_non_integral_constant_expression_p
26424 = parser->non_integral_constant_expression_p;
26425 parser->integral_constant_expression_p = false;
26427 /* Do not actually evaluate the expression. */
26428 ++cp_unevaluated_operand;
26429 ++c_inhibit_evaluation_warnings;
26430 /* If it's a `(', then we might be looking at the type-id
26431 construction. */
26432 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26434 tree type = NULL_TREE;
26436 /* We can't be sure yet whether we're looking at a type-id or an
26437 expression. */
26438 cp_parser_parse_tentatively (parser);
26439 /* Note: as a GNU Extension, compound literals are considered
26440 postfix-expressions as they are in C99, so they are valid
26441 arguments to sizeof. See comment in cp_parser_cast_expression
26442 for details. */
26443 if (cp_parser_compound_literal_p (parser))
26444 cp_parser_simulate_error (parser);
26445 else
26447 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
26448 parser->in_type_id_in_expr_p = true;
26449 /* Look for the type-id. */
26450 type = cp_parser_type_id (parser);
26451 /* Look for the closing `)'. */
26452 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26453 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
26456 /* If all went well, then we're done. */
26457 if (cp_parser_parse_definitely (parser))
26459 cp_decl_specifier_seq decl_specs;
26461 /* Build a trivial decl-specifier-seq. */
26462 clear_decl_specs (&decl_specs);
26463 decl_specs.type = type;
26465 /* Call grokdeclarator to figure out what type this is. */
26466 expr = grokdeclarator (NULL,
26467 &decl_specs,
26468 TYPENAME,
26469 /*initialized=*/0,
26470 /*attrlist=*/NULL);
26474 /* If the type-id production did not work out, then we must be
26475 looking at the unary-expression production. */
26476 if (!expr)
26477 expr = cp_parser_unary_expression (parser);
26479 /* Go back to evaluating expressions. */
26480 --cp_unevaluated_operand;
26481 --c_inhibit_evaluation_warnings;
26483 /* Free the message we created. */
26484 free (tmp);
26485 /* And restore the old one. */
26486 parser->type_definition_forbidden_message = saved_message;
26487 parser->integral_constant_expression_p
26488 = saved_integral_constant_expression_p;
26489 parser->non_integral_constant_expression_p
26490 = saved_non_integral_constant_expression_p;
26492 return expr;
26495 /* If the current declaration has no declarator, return true. */
26497 static bool
26498 cp_parser_declares_only_class_p (cp_parser *parser)
26500 /* If the next token is a `;' or a `,' then there is no
26501 declarator. */
26502 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26503 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
26506 /* Update the DECL_SPECS to reflect the storage class indicated by
26507 KEYWORD. */
26509 static void
26510 cp_parser_set_storage_class (cp_parser *parser,
26511 cp_decl_specifier_seq *decl_specs,
26512 enum rid keyword,
26513 cp_token *token)
26515 cp_storage_class storage_class;
26517 if (parser->in_unbraced_linkage_specification_p)
26519 error_at (token->location, "invalid use of %qD in linkage specification",
26520 ridpointers[keyword]);
26521 return;
26523 else if (decl_specs->storage_class != sc_none)
26525 decl_specs->conflicting_specifiers_p = true;
26526 return;
26529 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
26530 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
26531 && decl_specs->gnu_thread_keyword_p)
26533 pedwarn (decl_specs->locations[ds_thread], 0,
26534 "%<__thread%> before %qD", ridpointers[keyword]);
26537 switch (keyword)
26539 case RID_AUTO:
26540 storage_class = sc_auto;
26541 break;
26542 case RID_REGISTER:
26543 storage_class = sc_register;
26544 break;
26545 case RID_STATIC:
26546 storage_class = sc_static;
26547 break;
26548 case RID_EXTERN:
26549 storage_class = sc_extern;
26550 break;
26551 case RID_MUTABLE:
26552 storage_class = sc_mutable;
26553 break;
26554 default:
26555 gcc_unreachable ();
26557 decl_specs->storage_class = storage_class;
26558 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
26560 /* A storage class specifier cannot be applied alongside a typedef
26561 specifier. If there is a typedef specifier present then set
26562 conflicting_specifiers_p which will trigger an error later
26563 on in grokdeclarator. */
26564 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
26565 decl_specs->conflicting_specifiers_p = true;
26568 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
26569 is true, the type is a class or enum definition. */
26571 static void
26572 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
26573 tree type_spec,
26574 cp_token *token,
26575 bool type_definition_p)
26577 decl_specs->any_specifiers_p = true;
26579 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
26580 (with, for example, in "typedef int wchar_t;") we remember that
26581 this is what happened. In system headers, we ignore these
26582 declarations so that G++ can work with system headers that are not
26583 C++-safe. */
26584 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
26585 && !type_definition_p
26586 && (type_spec == boolean_type_node
26587 || type_spec == char16_type_node
26588 || type_spec == char32_type_node
26589 || type_spec == wchar_type_node)
26590 && (decl_specs->type
26591 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
26592 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
26593 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
26594 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
26596 decl_specs->redefined_builtin_type = type_spec;
26597 set_and_check_decl_spec_loc (decl_specs,
26598 ds_redefined_builtin_type_spec,
26599 token);
26600 if (!decl_specs->type)
26602 decl_specs->type = type_spec;
26603 decl_specs->type_definition_p = false;
26604 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
26607 else if (decl_specs->type)
26608 decl_specs->multiple_types_p = true;
26609 else
26611 decl_specs->type = type_spec;
26612 decl_specs->type_definition_p = type_definition_p;
26613 decl_specs->redefined_builtin_type = NULL_TREE;
26614 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
26618 /* True iff TOKEN is the GNU keyword __thread. */
26620 static bool
26621 token_is__thread (cp_token *token)
26623 gcc_assert (token->keyword == RID_THREAD);
26624 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
26627 /* Set the location for a declarator specifier and check if it is
26628 duplicated.
26630 DECL_SPECS is the sequence of declarator specifiers onto which to
26631 set the location.
26633 DS is the single declarator specifier to set which location is to
26634 be set onto the existing sequence of declarators.
26636 LOCATION is the location for the declarator specifier to
26637 consider. */
26639 static void
26640 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
26641 cp_decl_spec ds, cp_token *token)
26643 gcc_assert (ds < ds_last);
26645 if (decl_specs == NULL)
26646 return;
26648 source_location location = token->location;
26650 if (decl_specs->locations[ds] == 0)
26652 decl_specs->locations[ds] = location;
26653 if (ds == ds_thread)
26654 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
26656 else
26658 if (ds == ds_long)
26660 if (decl_specs->locations[ds_long_long] != 0)
26661 error_at (location,
26662 "%<long long long%> is too long for GCC");
26663 else
26665 decl_specs->locations[ds_long_long] = location;
26666 pedwarn_cxx98 (location,
26667 OPT_Wlong_long,
26668 "ISO C++ 1998 does not support %<long long%>");
26671 else if (ds == ds_thread)
26673 bool gnu = token_is__thread (token);
26674 if (gnu != decl_specs->gnu_thread_keyword_p)
26675 error_at (location,
26676 "both %<__thread%> and %<thread_local%> specified");
26677 else
26678 error_at (location, "duplicate %qD", token->u.value);
26680 else
26682 static const char *const decl_spec_names[] = {
26683 "signed",
26684 "unsigned",
26685 "short",
26686 "long",
26687 "const",
26688 "volatile",
26689 "restrict",
26690 "inline",
26691 "virtual",
26692 "explicit",
26693 "friend",
26694 "typedef",
26695 "using",
26696 "constexpr",
26697 "__complex"
26699 error_at (location,
26700 "duplicate %qs", decl_spec_names[ds]);
26705 /* Return true iff the declarator specifier DS is present in the
26706 sequence of declarator specifiers DECL_SPECS. */
26708 bool
26709 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
26710 cp_decl_spec ds)
26712 gcc_assert (ds < ds_last);
26714 if (decl_specs == NULL)
26715 return false;
26717 return decl_specs->locations[ds] != 0;
26720 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
26721 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
26723 static bool
26724 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
26726 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
26729 /* Issue an error message indicating that TOKEN_DESC was expected.
26730 If KEYWORD is true, it indicated this function is called by
26731 cp_parser_require_keword and the required token can only be
26732 a indicated keyword. */
26734 static void
26735 cp_parser_required_error (cp_parser *parser,
26736 required_token token_desc,
26737 bool keyword)
26739 switch (token_desc)
26741 case RT_NEW:
26742 cp_parser_error (parser, "expected %<new%>");
26743 return;
26744 case RT_DELETE:
26745 cp_parser_error (parser, "expected %<delete%>");
26746 return;
26747 case RT_RETURN:
26748 cp_parser_error (parser, "expected %<return%>");
26749 return;
26750 case RT_WHILE:
26751 cp_parser_error (parser, "expected %<while%>");
26752 return;
26753 case RT_EXTERN:
26754 cp_parser_error (parser, "expected %<extern%>");
26755 return;
26756 case RT_STATIC_ASSERT:
26757 cp_parser_error (parser, "expected %<static_assert%>");
26758 return;
26759 case RT_DECLTYPE:
26760 cp_parser_error (parser, "expected %<decltype%>");
26761 return;
26762 case RT_OPERATOR:
26763 cp_parser_error (parser, "expected %<operator%>");
26764 return;
26765 case RT_CLASS:
26766 cp_parser_error (parser, "expected %<class%>");
26767 return;
26768 case RT_TEMPLATE:
26769 cp_parser_error (parser, "expected %<template%>");
26770 return;
26771 case RT_NAMESPACE:
26772 cp_parser_error (parser, "expected %<namespace%>");
26773 return;
26774 case RT_USING:
26775 cp_parser_error (parser, "expected %<using%>");
26776 return;
26777 case RT_ASM:
26778 cp_parser_error (parser, "expected %<asm%>");
26779 return;
26780 case RT_TRY:
26781 cp_parser_error (parser, "expected %<try%>");
26782 return;
26783 case RT_CATCH:
26784 cp_parser_error (parser, "expected %<catch%>");
26785 return;
26786 case RT_THROW:
26787 cp_parser_error (parser, "expected %<throw%>");
26788 return;
26789 case RT_LABEL:
26790 cp_parser_error (parser, "expected %<__label__%>");
26791 return;
26792 case RT_AT_TRY:
26793 cp_parser_error (parser, "expected %<@try%>");
26794 return;
26795 case RT_AT_SYNCHRONIZED:
26796 cp_parser_error (parser, "expected %<@synchronized%>");
26797 return;
26798 case RT_AT_THROW:
26799 cp_parser_error (parser, "expected %<@throw%>");
26800 return;
26801 case RT_TRANSACTION_ATOMIC:
26802 cp_parser_error (parser, "expected %<__transaction_atomic%>");
26803 return;
26804 case RT_TRANSACTION_RELAXED:
26805 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
26806 return;
26807 default:
26808 break;
26810 if (!keyword)
26812 switch (token_desc)
26814 case RT_SEMICOLON:
26815 cp_parser_error (parser, "expected %<;%>");
26816 return;
26817 case RT_OPEN_PAREN:
26818 cp_parser_error (parser, "expected %<(%>");
26819 return;
26820 case RT_CLOSE_BRACE:
26821 cp_parser_error (parser, "expected %<}%>");
26822 return;
26823 case RT_OPEN_BRACE:
26824 cp_parser_error (parser, "expected %<{%>");
26825 return;
26826 case RT_CLOSE_SQUARE:
26827 cp_parser_error (parser, "expected %<]%>");
26828 return;
26829 case RT_OPEN_SQUARE:
26830 cp_parser_error (parser, "expected %<[%>");
26831 return;
26832 case RT_COMMA:
26833 cp_parser_error (parser, "expected %<,%>");
26834 return;
26835 case RT_SCOPE:
26836 cp_parser_error (parser, "expected %<::%>");
26837 return;
26838 case RT_LESS:
26839 cp_parser_error (parser, "expected %<<%>");
26840 return;
26841 case RT_GREATER:
26842 cp_parser_error (parser, "expected %<>%>");
26843 return;
26844 case RT_EQ:
26845 cp_parser_error (parser, "expected %<=%>");
26846 return;
26847 case RT_ELLIPSIS:
26848 cp_parser_error (parser, "expected %<...%>");
26849 return;
26850 case RT_MULT:
26851 cp_parser_error (parser, "expected %<*%>");
26852 return;
26853 case RT_COMPL:
26854 cp_parser_error (parser, "expected %<~%>");
26855 return;
26856 case RT_COLON:
26857 cp_parser_error (parser, "expected %<:%>");
26858 return;
26859 case RT_COLON_SCOPE:
26860 cp_parser_error (parser, "expected %<:%> or %<::%>");
26861 return;
26862 case RT_CLOSE_PAREN:
26863 cp_parser_error (parser, "expected %<)%>");
26864 return;
26865 case RT_COMMA_CLOSE_PAREN:
26866 cp_parser_error (parser, "expected %<,%> or %<)%>");
26867 return;
26868 case RT_PRAGMA_EOL:
26869 cp_parser_error (parser, "expected end of line");
26870 return;
26871 case RT_NAME:
26872 cp_parser_error (parser, "expected identifier");
26873 return;
26874 case RT_SELECT:
26875 cp_parser_error (parser, "expected selection-statement");
26876 return;
26877 case RT_INTERATION:
26878 cp_parser_error (parser, "expected iteration-statement");
26879 return;
26880 case RT_JUMP:
26881 cp_parser_error (parser, "expected jump-statement");
26882 return;
26883 case RT_CLASS_KEY:
26884 cp_parser_error (parser, "expected class-key");
26885 return;
26886 case RT_CLASS_TYPENAME_TEMPLATE:
26887 cp_parser_error (parser,
26888 "expected %<class%>, %<typename%>, or %<template%>");
26889 return;
26890 default:
26891 gcc_unreachable ();
26894 else
26895 gcc_unreachable ();
26900 /* If the next token is of the indicated TYPE, consume it. Otherwise,
26901 issue an error message indicating that TOKEN_DESC was expected.
26903 Returns the token consumed, if the token had the appropriate type.
26904 Otherwise, returns NULL. */
26906 static cp_token *
26907 cp_parser_require (cp_parser* parser,
26908 enum cpp_ttype type,
26909 required_token token_desc)
26911 if (cp_lexer_next_token_is (parser->lexer, type))
26912 return cp_lexer_consume_token (parser->lexer);
26913 else
26915 /* Output the MESSAGE -- unless we're parsing tentatively. */
26916 if (!cp_parser_simulate_error (parser))
26917 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
26918 return NULL;
26922 /* An error message is produced if the next token is not '>'.
26923 All further tokens are skipped until the desired token is
26924 found or '{', '}', ';' or an unbalanced ')' or ']'. */
26926 static void
26927 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
26929 /* Current level of '< ... >'. */
26930 unsigned level = 0;
26931 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
26932 unsigned nesting_depth = 0;
26934 /* Are we ready, yet? If not, issue error message. */
26935 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
26936 return;
26938 /* Skip tokens until the desired token is found. */
26939 while (true)
26941 /* Peek at the next token. */
26942 switch (cp_lexer_peek_token (parser->lexer)->type)
26944 case CPP_LESS:
26945 if (!nesting_depth)
26946 ++level;
26947 break;
26949 case CPP_RSHIFT:
26950 if (cxx_dialect == cxx98)
26951 /* C++0x views the `>>' operator as two `>' tokens, but
26952 C++98 does not. */
26953 break;
26954 else if (!nesting_depth && level-- == 0)
26956 /* We've hit a `>>' where the first `>' closes the
26957 template argument list, and the second `>' is
26958 spurious. Just consume the `>>' and stop; we've
26959 already produced at least one error. */
26960 cp_lexer_consume_token (parser->lexer);
26961 return;
26963 /* Fall through for C++0x, so we handle the second `>' in
26964 the `>>'. */
26966 case CPP_GREATER:
26967 if (!nesting_depth && level-- == 0)
26969 /* We've reached the token we want, consume it and stop. */
26970 cp_lexer_consume_token (parser->lexer);
26971 return;
26973 break;
26975 case CPP_OPEN_PAREN:
26976 case CPP_OPEN_SQUARE:
26977 ++nesting_depth;
26978 break;
26980 case CPP_CLOSE_PAREN:
26981 case CPP_CLOSE_SQUARE:
26982 if (nesting_depth-- == 0)
26983 return;
26984 break;
26986 case CPP_EOF:
26987 case CPP_PRAGMA_EOL:
26988 case CPP_SEMICOLON:
26989 case CPP_OPEN_BRACE:
26990 case CPP_CLOSE_BRACE:
26991 /* The '>' was probably forgotten, don't look further. */
26992 return;
26994 default:
26995 break;
26998 /* Consume this token. */
26999 cp_lexer_consume_token (parser->lexer);
27003 /* If the next token is the indicated keyword, consume it. Otherwise,
27004 issue an error message indicating that TOKEN_DESC was expected.
27006 Returns the token consumed, if the token had the appropriate type.
27007 Otherwise, returns NULL. */
27009 static cp_token *
27010 cp_parser_require_keyword (cp_parser* parser,
27011 enum rid keyword,
27012 required_token token_desc)
27014 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
27016 if (token && token->keyword != keyword)
27018 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
27019 return NULL;
27022 return token;
27025 /* Returns TRUE iff TOKEN is a token that can begin the body of a
27026 function-definition. */
27028 static bool
27029 cp_parser_token_starts_function_definition_p (cp_token* token)
27031 return (/* An ordinary function-body begins with an `{'. */
27032 token->type == CPP_OPEN_BRACE
27033 /* A ctor-initializer begins with a `:'. */
27034 || token->type == CPP_COLON
27035 /* A function-try-block begins with `try'. */
27036 || token->keyword == RID_TRY
27037 /* A function-transaction-block begins with `__transaction_atomic'
27038 or `__transaction_relaxed'. */
27039 || token->keyword == RID_TRANSACTION_ATOMIC
27040 || token->keyword == RID_TRANSACTION_RELAXED
27041 /* The named return value extension begins with `return'. */
27042 || token->keyword == RID_RETURN);
27045 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
27046 definition. */
27048 static bool
27049 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
27051 cp_token *token;
27053 token = cp_lexer_peek_token (parser->lexer);
27054 return (token->type == CPP_OPEN_BRACE
27055 || (token->type == CPP_COLON
27056 && !parser->colon_doesnt_start_class_def_p));
27059 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
27060 C++0x) ending a template-argument. */
27062 static bool
27063 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
27065 cp_token *token;
27067 token = cp_lexer_peek_token (parser->lexer);
27068 return (token->type == CPP_COMMA
27069 || token->type == CPP_GREATER
27070 || token->type == CPP_ELLIPSIS
27071 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
27074 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
27075 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
27077 static bool
27078 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
27079 size_t n)
27081 cp_token *token;
27083 token = cp_lexer_peek_nth_token (parser->lexer, n);
27084 if (token->type == CPP_LESS)
27085 return true;
27086 /* Check for the sequence `<::' in the original code. It would be lexed as
27087 `[:', where `[' is a digraph, and there is no whitespace before
27088 `:'. */
27089 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
27091 cp_token *token2;
27092 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
27093 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
27094 return true;
27096 return false;
27099 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
27100 or none_type otherwise. */
27102 static enum tag_types
27103 cp_parser_token_is_class_key (cp_token* token)
27105 switch (token->keyword)
27107 case RID_CLASS:
27108 return class_type;
27109 case RID_STRUCT:
27110 return record_type;
27111 case RID_UNION:
27112 return union_type;
27114 default:
27115 return none_type;
27119 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
27120 or none_type otherwise or if the token is null. */
27122 static enum tag_types
27123 cp_parser_token_is_type_parameter_key (cp_token* token)
27125 if (!token)
27126 return none_type;
27128 switch (token->keyword)
27130 case RID_CLASS:
27131 return class_type;
27132 case RID_TYPENAME:
27133 return typename_type;
27135 default:
27136 return none_type;
27140 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
27142 static void
27143 cp_parser_check_class_key (enum tag_types class_key, tree type)
27145 if (type == error_mark_node)
27146 return;
27147 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
27149 if (permerror (input_location, "%qs tag used in naming %q#T",
27150 class_key == union_type ? "union"
27151 : class_key == record_type ? "struct" : "class",
27152 type))
27153 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
27154 "%q#T was previously declared here", type);
27158 /* Issue an error message if DECL is redeclared with different
27159 access than its original declaration [class.access.spec/3].
27160 This applies to nested classes and nested class templates.
27161 [class.mem/1]. */
27163 static void
27164 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
27166 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
27167 return;
27169 if ((TREE_PRIVATE (decl)
27170 != (current_access_specifier == access_private_node))
27171 || (TREE_PROTECTED (decl)
27172 != (current_access_specifier == access_protected_node)))
27173 error_at (location, "%qD redeclared with different access", decl);
27176 /* Look for the `template' keyword, as a syntactic disambiguator.
27177 Return TRUE iff it is present, in which case it will be
27178 consumed. */
27180 static bool
27181 cp_parser_optional_template_keyword (cp_parser *parser)
27183 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27185 /* In C++98 the `template' keyword can only be used within templates;
27186 outside templates the parser can always figure out what is a
27187 template and what is not. In C++11, per the resolution of DR 468,
27188 `template' is allowed in cases where it is not strictly necessary. */
27189 if (!processing_template_decl
27190 && pedantic && cxx_dialect == cxx98)
27192 cp_token *token = cp_lexer_peek_token (parser->lexer);
27193 pedwarn (token->location, OPT_Wpedantic,
27194 "in C++98 %<template%> (as a disambiguator) is only "
27195 "allowed within templates");
27196 /* If this part of the token stream is rescanned, the same
27197 error message would be generated. So, we purge the token
27198 from the stream. */
27199 cp_lexer_purge_token (parser->lexer);
27200 return false;
27202 else
27204 /* Consume the `template' keyword. */
27205 cp_lexer_consume_token (parser->lexer);
27206 return true;
27209 return false;
27212 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
27213 set PARSER->SCOPE, and perform other related actions. */
27215 static void
27216 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
27218 struct tree_check *check_value;
27220 /* Get the stored value. */
27221 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
27222 /* Set the scope from the stored value. */
27223 parser->scope = saved_checks_value (check_value);
27224 parser->qualifying_scope = check_value->qualifying_scope;
27225 parser->object_scope = NULL_TREE;
27228 /* Consume tokens up through a non-nested END token. Returns TRUE if we
27229 encounter the end of a block before what we were looking for. */
27231 static bool
27232 cp_parser_cache_group (cp_parser *parser,
27233 enum cpp_ttype end,
27234 unsigned depth)
27236 while (true)
27238 cp_token *token = cp_lexer_peek_token (parser->lexer);
27240 /* Abort a parenthesized expression if we encounter a semicolon. */
27241 if ((end == CPP_CLOSE_PAREN || depth == 0)
27242 && token->type == CPP_SEMICOLON)
27243 return true;
27244 /* If we've reached the end of the file, stop. */
27245 if (token->type == CPP_EOF
27246 || (end != CPP_PRAGMA_EOL
27247 && token->type == CPP_PRAGMA_EOL))
27248 return true;
27249 if (token->type == CPP_CLOSE_BRACE && depth == 0)
27250 /* We've hit the end of an enclosing block, so there's been some
27251 kind of syntax error. */
27252 return true;
27254 /* Consume the token. */
27255 cp_lexer_consume_token (parser->lexer);
27256 /* See if it starts a new group. */
27257 if (token->type == CPP_OPEN_BRACE)
27259 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
27260 /* In theory this should probably check end == '}', but
27261 cp_parser_save_member_function_body needs it to exit
27262 after either '}' or ')' when called with ')'. */
27263 if (depth == 0)
27264 return false;
27266 else if (token->type == CPP_OPEN_PAREN)
27268 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
27269 if (depth == 0 && end == CPP_CLOSE_PAREN)
27270 return false;
27272 else if (token->type == CPP_PRAGMA)
27273 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
27274 else if (token->type == end)
27275 return false;
27279 /* Like above, for caching a default argument or NSDMI. Both of these are
27280 terminated by a non-nested comma, but it can be unclear whether or not a
27281 comma is nested in a template argument list unless we do more parsing.
27282 In order to handle this ambiguity, when we encounter a ',' after a '<'
27283 we try to parse what follows as a parameter-declaration-list (in the
27284 case of a default argument) or a member-declarator (in the case of an
27285 NSDMI). If that succeeds, then we stop caching. */
27287 static tree
27288 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
27290 unsigned depth = 0;
27291 int maybe_template_id = 0;
27292 cp_token *first_token;
27293 cp_token *token;
27294 tree default_argument;
27296 /* Add tokens until we have processed the entire default
27297 argument. We add the range [first_token, token). */
27298 first_token = cp_lexer_peek_token (parser->lexer);
27299 if (first_token->type == CPP_OPEN_BRACE)
27301 /* For list-initialization, this is straightforward. */
27302 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27303 token = cp_lexer_peek_token (parser->lexer);
27305 else while (true)
27307 bool done = false;
27309 /* Peek at the next token. */
27310 token = cp_lexer_peek_token (parser->lexer);
27311 /* What we do depends on what token we have. */
27312 switch (token->type)
27314 /* In valid code, a default argument must be
27315 immediately followed by a `,' `)', or `...'. */
27316 case CPP_COMMA:
27317 if (depth == 0 && maybe_template_id)
27319 /* If we've seen a '<', we might be in a
27320 template-argument-list. Until Core issue 325 is
27321 resolved, we don't know how this situation ought
27322 to be handled, so try to DTRT. We check whether
27323 what comes after the comma is a valid parameter
27324 declaration list. If it is, then the comma ends
27325 the default argument; otherwise the default
27326 argument continues. */
27327 bool error = false;
27328 cp_token *peek;
27330 /* Set ITALP so cp_parser_parameter_declaration_list
27331 doesn't decide to commit to this parse. */
27332 bool saved_italp = parser->in_template_argument_list_p;
27333 parser->in_template_argument_list_p = true;
27335 cp_parser_parse_tentatively (parser);
27337 if (nsdmi)
27339 /* Parse declarators until we reach a non-comma or
27340 somthing that cannot be an initializer.
27341 Just checking whether we're looking at a single
27342 declarator is insufficient. Consider:
27343 int var = tuple<T,U>::x;
27344 The template parameter 'U' looks exactly like a
27345 declarator. */
27348 int ctor_dtor_or_conv_p;
27349 cp_lexer_consume_token (parser->lexer);
27350 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27351 &ctor_dtor_or_conv_p,
27352 /*parenthesized_p=*/NULL,
27353 /*member_p=*/true,
27354 /*friend_p=*/false);
27355 peek = cp_lexer_peek_token (parser->lexer);
27356 if (cp_parser_error_occurred (parser))
27357 break;
27359 while (peek->type == CPP_COMMA);
27360 /* If we met an '=' or ';' then the original comma
27361 was the end of the NSDMI. Otherwise assume
27362 we're still in the NSDMI. */
27363 error = (peek->type != CPP_EQ
27364 && peek->type != CPP_SEMICOLON);
27366 else
27368 cp_lexer_consume_token (parser->lexer);
27369 begin_scope (sk_function_parms, NULL_TREE);
27370 cp_parser_parameter_declaration_list (parser, &error);
27371 pop_bindings_and_leave_scope ();
27373 if (!cp_parser_error_occurred (parser) && !error)
27374 done = true;
27375 cp_parser_abort_tentative_parse (parser);
27377 parser->in_template_argument_list_p = saved_italp;
27378 break;
27380 case CPP_CLOSE_PAREN:
27381 case CPP_ELLIPSIS:
27382 /* If we run into a non-nested `;', `}', or `]',
27383 then the code is invalid -- but the default
27384 argument is certainly over. */
27385 case CPP_SEMICOLON:
27386 case CPP_CLOSE_BRACE:
27387 case CPP_CLOSE_SQUARE:
27388 if (depth == 0
27389 /* Handle correctly int n = sizeof ... ( p ); */
27390 && token->type != CPP_ELLIPSIS)
27391 done = true;
27392 /* Update DEPTH, if necessary. */
27393 else if (token->type == CPP_CLOSE_PAREN
27394 || token->type == CPP_CLOSE_BRACE
27395 || token->type == CPP_CLOSE_SQUARE)
27396 --depth;
27397 break;
27399 case CPP_OPEN_PAREN:
27400 case CPP_OPEN_SQUARE:
27401 case CPP_OPEN_BRACE:
27402 ++depth;
27403 break;
27405 case CPP_LESS:
27406 if (depth == 0)
27407 /* This might be the comparison operator, or it might
27408 start a template argument list. */
27409 ++maybe_template_id;
27410 break;
27412 case CPP_RSHIFT:
27413 if (cxx_dialect == cxx98)
27414 break;
27415 /* Fall through for C++0x, which treats the `>>'
27416 operator like two `>' tokens in certain
27417 cases. */
27419 case CPP_GREATER:
27420 if (depth == 0)
27422 /* This might be an operator, or it might close a
27423 template argument list. But if a previous '<'
27424 started a template argument list, this will have
27425 closed it, so we can't be in one anymore. */
27426 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
27427 if (maybe_template_id < 0)
27428 maybe_template_id = 0;
27430 break;
27432 /* If we run out of tokens, issue an error message. */
27433 case CPP_EOF:
27434 case CPP_PRAGMA_EOL:
27435 error_at (token->location, "file ends in default argument");
27436 done = true;
27437 break;
27439 case CPP_NAME:
27440 case CPP_SCOPE:
27441 /* In these cases, we should look for template-ids.
27442 For example, if the default argument is
27443 `X<int, double>()', we need to do name lookup to
27444 figure out whether or not `X' is a template; if
27445 so, the `,' does not end the default argument.
27447 That is not yet done. */
27448 break;
27450 default:
27451 break;
27454 /* If we've reached the end, stop. */
27455 if (done)
27456 break;
27458 /* Add the token to the token block. */
27459 token = cp_lexer_consume_token (parser->lexer);
27462 /* Create a DEFAULT_ARG to represent the unparsed default
27463 argument. */
27464 default_argument = make_node (DEFAULT_ARG);
27465 DEFARG_TOKENS (default_argument)
27466 = cp_token_cache_new (first_token, token);
27467 DEFARG_INSTANTIATIONS (default_argument) = NULL;
27469 return default_argument;
27472 /* Begin parsing tentatively. We always save tokens while parsing
27473 tentatively so that if the tentative parsing fails we can restore the
27474 tokens. */
27476 static void
27477 cp_parser_parse_tentatively (cp_parser* parser)
27479 /* Enter a new parsing context. */
27480 parser->context = cp_parser_context_new (parser->context);
27481 /* Begin saving tokens. */
27482 cp_lexer_save_tokens (parser->lexer);
27483 /* In order to avoid repetitive access control error messages,
27484 access checks are queued up until we are no longer parsing
27485 tentatively. */
27486 push_deferring_access_checks (dk_deferred);
27489 /* Commit to the currently active tentative parse. */
27491 static void
27492 cp_parser_commit_to_tentative_parse (cp_parser* parser)
27494 cp_parser_context *context;
27495 cp_lexer *lexer;
27497 /* Mark all of the levels as committed. */
27498 lexer = parser->lexer;
27499 for (context = parser->context; context->next; context = context->next)
27501 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
27502 break;
27503 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
27504 while (!cp_lexer_saving_tokens (lexer))
27505 lexer = lexer->next;
27506 cp_lexer_commit_tokens (lexer);
27510 /* Commit to the topmost currently active tentative parse.
27512 Note that this function shouldn't be called when there are
27513 irreversible side-effects while in a tentative state. For
27514 example, we shouldn't create a permanent entry in the symbol
27515 table, or issue an error message that might not apply if the
27516 tentative parse is aborted. */
27518 static void
27519 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
27521 cp_parser_context *context = parser->context;
27522 cp_lexer *lexer = parser->lexer;
27524 if (context)
27526 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
27527 return;
27528 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
27530 while (!cp_lexer_saving_tokens (lexer))
27531 lexer = lexer->next;
27532 cp_lexer_commit_tokens (lexer);
27536 /* Abort the currently active tentative parse. All consumed tokens
27537 will be rolled back, and no diagnostics will be issued. */
27539 static void
27540 cp_parser_abort_tentative_parse (cp_parser* parser)
27542 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
27543 || errorcount > 0);
27544 cp_parser_simulate_error (parser);
27545 /* Now, pretend that we want to see if the construct was
27546 successfully parsed. */
27547 cp_parser_parse_definitely (parser);
27550 /* Stop parsing tentatively. If a parse error has occurred, restore the
27551 token stream. Otherwise, commit to the tokens we have consumed.
27552 Returns true if no error occurred; false otherwise. */
27554 static bool
27555 cp_parser_parse_definitely (cp_parser* parser)
27557 bool error_occurred;
27558 cp_parser_context *context;
27560 /* Remember whether or not an error occurred, since we are about to
27561 destroy that information. */
27562 error_occurred = cp_parser_error_occurred (parser);
27563 /* Remove the topmost context from the stack. */
27564 context = parser->context;
27565 parser->context = context->next;
27566 /* If no parse errors occurred, commit to the tentative parse. */
27567 if (!error_occurred)
27569 /* Commit to the tokens read tentatively, unless that was
27570 already done. */
27571 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
27572 cp_lexer_commit_tokens (parser->lexer);
27574 pop_to_parent_deferring_access_checks ();
27576 /* Otherwise, if errors occurred, roll back our state so that things
27577 are just as they were before we began the tentative parse. */
27578 else
27580 cp_lexer_rollback_tokens (parser->lexer);
27581 pop_deferring_access_checks ();
27583 /* Add the context to the front of the free list. */
27584 context->next = cp_parser_context_free_list;
27585 cp_parser_context_free_list = context;
27587 return !error_occurred;
27590 /* Returns true if we are parsing tentatively and are not committed to
27591 this tentative parse. */
27593 static bool
27594 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
27596 return (cp_parser_parsing_tentatively (parser)
27597 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
27600 /* Returns nonzero iff an error has occurred during the most recent
27601 tentative parse. */
27603 static bool
27604 cp_parser_error_occurred (cp_parser* parser)
27606 return (cp_parser_parsing_tentatively (parser)
27607 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
27610 /* Returns nonzero if GNU extensions are allowed. */
27612 static bool
27613 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
27615 return parser->allow_gnu_extensions_p;
27618 /* Objective-C++ Productions */
27621 /* Parse an Objective-C expression, which feeds into a primary-expression
27622 above.
27624 objc-expression:
27625 objc-message-expression
27626 objc-string-literal
27627 objc-encode-expression
27628 objc-protocol-expression
27629 objc-selector-expression
27631 Returns a tree representation of the expression. */
27633 static cp_expr
27634 cp_parser_objc_expression (cp_parser* parser)
27636 /* Try to figure out what kind of declaration is present. */
27637 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27639 switch (kwd->type)
27641 case CPP_OPEN_SQUARE:
27642 return cp_parser_objc_message_expression (parser);
27644 case CPP_OBJC_STRING:
27645 kwd = cp_lexer_consume_token (parser->lexer);
27646 return objc_build_string_object (kwd->u.value);
27648 case CPP_KEYWORD:
27649 switch (kwd->keyword)
27651 case RID_AT_ENCODE:
27652 return cp_parser_objc_encode_expression (parser);
27654 case RID_AT_PROTOCOL:
27655 return cp_parser_objc_protocol_expression (parser);
27657 case RID_AT_SELECTOR:
27658 return cp_parser_objc_selector_expression (parser);
27660 default:
27661 break;
27663 default:
27664 error_at (kwd->location,
27665 "misplaced %<@%D%> Objective-C++ construct",
27666 kwd->u.value);
27667 cp_parser_skip_to_end_of_block_or_statement (parser);
27670 return error_mark_node;
27673 /* Parse an Objective-C message expression.
27675 objc-message-expression:
27676 [ objc-message-receiver objc-message-args ]
27678 Returns a representation of an Objective-C message. */
27680 static tree
27681 cp_parser_objc_message_expression (cp_parser* parser)
27683 tree receiver, messageargs;
27685 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
27686 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
27687 receiver = cp_parser_objc_message_receiver (parser);
27688 messageargs = cp_parser_objc_message_args (parser);
27689 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
27690 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27692 tree result = objc_build_message_expr (receiver, messageargs);
27694 /* Construct a location e.g.
27695 [self func1:5]
27696 ^~~~~~~~~~~~~~
27697 ranging from the '[' to the ']', with the caret at the start. */
27698 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
27699 protected_set_expr_location (result, combined_loc);
27701 return result;
27704 /* Parse an objc-message-receiver.
27706 objc-message-receiver:
27707 expression
27708 simple-type-specifier
27710 Returns a representation of the type or expression. */
27712 static tree
27713 cp_parser_objc_message_receiver (cp_parser* parser)
27715 tree rcv;
27717 /* An Objective-C message receiver may be either (1) a type
27718 or (2) an expression. */
27719 cp_parser_parse_tentatively (parser);
27720 rcv = cp_parser_expression (parser);
27722 /* If that worked out, fine. */
27723 if (cp_parser_parse_definitely (parser))
27724 return rcv;
27726 cp_parser_parse_tentatively (parser);
27727 rcv = cp_parser_simple_type_specifier (parser,
27728 /*decl_specs=*/NULL,
27729 CP_PARSER_FLAGS_NONE);
27731 if (cp_parser_parse_definitely (parser))
27732 return objc_get_class_reference (rcv);
27734 cp_parser_error (parser, "objective-c++ message receiver expected");
27735 return error_mark_node;
27738 /* Parse the arguments and selectors comprising an Objective-C message.
27740 objc-message-args:
27741 objc-selector
27742 objc-selector-args
27743 objc-selector-args , objc-comma-args
27745 objc-selector-args:
27746 objc-selector [opt] : assignment-expression
27747 objc-selector-args objc-selector [opt] : assignment-expression
27749 objc-comma-args:
27750 assignment-expression
27751 objc-comma-args , assignment-expression
27753 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
27754 selector arguments and TREE_VALUE containing a list of comma
27755 arguments. */
27757 static tree
27758 cp_parser_objc_message_args (cp_parser* parser)
27760 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
27761 bool maybe_unary_selector_p = true;
27762 cp_token *token = cp_lexer_peek_token (parser->lexer);
27764 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
27766 tree selector = NULL_TREE, arg;
27768 if (token->type != CPP_COLON)
27769 selector = cp_parser_objc_selector (parser);
27771 /* Detect if we have a unary selector. */
27772 if (maybe_unary_selector_p
27773 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27774 return build_tree_list (selector, NULL_TREE);
27776 maybe_unary_selector_p = false;
27777 cp_parser_require (parser, CPP_COLON, RT_COLON);
27778 arg = cp_parser_assignment_expression (parser);
27780 sel_args
27781 = chainon (sel_args,
27782 build_tree_list (selector, arg));
27784 token = cp_lexer_peek_token (parser->lexer);
27787 /* Handle non-selector arguments, if any. */
27788 while (token->type == CPP_COMMA)
27790 tree arg;
27792 cp_lexer_consume_token (parser->lexer);
27793 arg = cp_parser_assignment_expression (parser);
27795 addl_args
27796 = chainon (addl_args,
27797 build_tree_list (NULL_TREE, arg));
27799 token = cp_lexer_peek_token (parser->lexer);
27802 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
27804 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
27805 return build_tree_list (error_mark_node, error_mark_node);
27808 return build_tree_list (sel_args, addl_args);
27811 /* Parse an Objective-C encode expression.
27813 objc-encode-expression:
27814 @encode objc-typename
27816 Returns an encoded representation of the type argument. */
27818 static cp_expr
27819 cp_parser_objc_encode_expression (cp_parser* parser)
27821 tree type;
27822 cp_token *token;
27823 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
27825 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
27826 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27827 token = cp_lexer_peek_token (parser->lexer);
27828 type = complete_type (cp_parser_type_id (parser));
27829 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27831 if (!type)
27833 error_at (token->location,
27834 "%<@encode%> must specify a type as an argument");
27835 return error_mark_node;
27838 /* This happens if we find @encode(T) (where T is a template
27839 typename or something dependent on a template typename) when
27840 parsing a template. In that case, we can't compile it
27841 immediately, but we rather create an AT_ENCODE_EXPR which will
27842 need to be instantiated when the template is used.
27844 if (dependent_type_p (type))
27846 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
27847 TREE_READONLY (value) = 1;
27848 return value;
27852 /* Build a location of the form:
27853 @encode(int)
27854 ^~~~~~~~~~~~
27855 with caret==start at the @ token, finishing at the close paren. */
27856 location_t combined_loc
27857 = make_location (start_loc, start_loc,
27858 cp_lexer_previous_token (parser->lexer)->location);
27860 return cp_expr (objc_build_encode_expr (type), combined_loc);
27863 /* Parse an Objective-C @defs expression. */
27865 static tree
27866 cp_parser_objc_defs_expression (cp_parser *parser)
27868 tree name;
27870 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
27871 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27872 name = cp_parser_identifier (parser);
27873 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27875 return objc_get_class_ivars (name);
27878 /* Parse an Objective-C protocol expression.
27880 objc-protocol-expression:
27881 @protocol ( identifier )
27883 Returns a representation of the protocol expression. */
27885 static tree
27886 cp_parser_objc_protocol_expression (cp_parser* parser)
27888 tree proto;
27889 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
27891 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
27892 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27893 proto = cp_parser_identifier (parser);
27894 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27896 /* Build a location of the form:
27897 @protocol(prot)
27898 ^~~~~~~~~~~~~~~
27899 with caret==start at the @ token, finishing at the close paren. */
27900 location_t combined_loc
27901 = make_location (start_loc, start_loc,
27902 cp_lexer_previous_token (parser->lexer)->location);
27903 tree result = objc_build_protocol_expr (proto);
27904 protected_set_expr_location (result, combined_loc);
27905 return result;
27908 /* Parse an Objective-C selector expression.
27910 objc-selector-expression:
27911 @selector ( objc-method-signature )
27913 objc-method-signature:
27914 objc-selector
27915 objc-selector-seq
27917 objc-selector-seq:
27918 objc-selector :
27919 objc-selector-seq objc-selector :
27921 Returns a representation of the method selector. */
27923 static tree
27924 cp_parser_objc_selector_expression (cp_parser* parser)
27926 tree sel_seq = NULL_TREE;
27927 bool maybe_unary_selector_p = true;
27928 cp_token *token;
27929 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27931 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
27932 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27933 token = cp_lexer_peek_token (parser->lexer);
27935 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
27936 || token->type == CPP_SCOPE)
27938 tree selector = NULL_TREE;
27940 if (token->type != CPP_COLON
27941 || token->type == CPP_SCOPE)
27942 selector = cp_parser_objc_selector (parser);
27944 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
27945 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
27947 /* Detect if we have a unary selector. */
27948 if (maybe_unary_selector_p)
27950 sel_seq = selector;
27951 goto finish_selector;
27953 else
27955 cp_parser_error (parser, "expected %<:%>");
27958 maybe_unary_selector_p = false;
27959 token = cp_lexer_consume_token (parser->lexer);
27961 if (token->type == CPP_SCOPE)
27963 sel_seq
27964 = chainon (sel_seq,
27965 build_tree_list (selector, NULL_TREE));
27966 sel_seq
27967 = chainon (sel_seq,
27968 build_tree_list (NULL_TREE, NULL_TREE));
27970 else
27971 sel_seq
27972 = chainon (sel_seq,
27973 build_tree_list (selector, NULL_TREE));
27975 token = cp_lexer_peek_token (parser->lexer);
27978 finish_selector:
27979 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27982 /* Build a location of the form:
27983 @selector(func)
27984 ^~~~~~~~~~~~~~~
27985 with caret==start at the @ token, finishing at the close paren. */
27986 location_t combined_loc
27987 = make_location (loc, loc,
27988 cp_lexer_previous_token (parser->lexer)->location);
27989 tree result = objc_build_selector_expr (combined_loc, sel_seq);
27990 /* TODO: objc_build_selector_expr doesn't always honor the location. */
27991 protected_set_expr_location (result, combined_loc);
27992 return result;
27995 /* Parse a list of identifiers.
27997 objc-identifier-list:
27998 identifier
27999 objc-identifier-list , identifier
28001 Returns a TREE_LIST of identifier nodes. */
28003 static tree
28004 cp_parser_objc_identifier_list (cp_parser* parser)
28006 tree identifier;
28007 tree list;
28008 cp_token *sep;
28010 identifier = cp_parser_identifier (parser);
28011 if (identifier == error_mark_node)
28012 return error_mark_node;
28014 list = build_tree_list (NULL_TREE, identifier);
28015 sep = cp_lexer_peek_token (parser->lexer);
28017 while (sep->type == CPP_COMMA)
28019 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28020 identifier = cp_parser_identifier (parser);
28021 if (identifier == error_mark_node)
28022 return list;
28024 list = chainon (list, build_tree_list (NULL_TREE,
28025 identifier));
28026 sep = cp_lexer_peek_token (parser->lexer);
28029 return list;
28032 /* Parse an Objective-C alias declaration.
28034 objc-alias-declaration:
28035 @compatibility_alias identifier identifier ;
28037 This function registers the alias mapping with the Objective-C front end.
28038 It returns nothing. */
28040 static void
28041 cp_parser_objc_alias_declaration (cp_parser* parser)
28043 tree alias, orig;
28045 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
28046 alias = cp_parser_identifier (parser);
28047 orig = cp_parser_identifier (parser);
28048 objc_declare_alias (alias, orig);
28049 cp_parser_consume_semicolon_at_end_of_statement (parser);
28052 /* Parse an Objective-C class forward-declaration.
28054 objc-class-declaration:
28055 @class objc-identifier-list ;
28057 The function registers the forward declarations with the Objective-C
28058 front end. It returns nothing. */
28060 static void
28061 cp_parser_objc_class_declaration (cp_parser* parser)
28063 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
28064 while (true)
28066 tree id;
28068 id = cp_parser_identifier (parser);
28069 if (id == error_mark_node)
28070 break;
28072 objc_declare_class (id);
28074 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28075 cp_lexer_consume_token (parser->lexer);
28076 else
28077 break;
28079 cp_parser_consume_semicolon_at_end_of_statement (parser);
28082 /* Parse a list of Objective-C protocol references.
28084 objc-protocol-refs-opt:
28085 objc-protocol-refs [opt]
28087 objc-protocol-refs:
28088 < objc-identifier-list >
28090 Returns a TREE_LIST of identifiers, if any. */
28092 static tree
28093 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
28095 tree protorefs = NULL_TREE;
28097 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
28099 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
28100 protorefs = cp_parser_objc_identifier_list (parser);
28101 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
28104 return protorefs;
28107 /* Parse a Objective-C visibility specification. */
28109 static void
28110 cp_parser_objc_visibility_spec (cp_parser* parser)
28112 cp_token *vis = cp_lexer_peek_token (parser->lexer);
28114 switch (vis->keyword)
28116 case RID_AT_PRIVATE:
28117 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
28118 break;
28119 case RID_AT_PROTECTED:
28120 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
28121 break;
28122 case RID_AT_PUBLIC:
28123 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
28124 break;
28125 case RID_AT_PACKAGE:
28126 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
28127 break;
28128 default:
28129 return;
28132 /* Eat '@private'/'@protected'/'@public'. */
28133 cp_lexer_consume_token (parser->lexer);
28136 /* Parse an Objective-C method type. Return 'true' if it is a class
28137 (+) method, and 'false' if it is an instance (-) method. */
28139 static inline bool
28140 cp_parser_objc_method_type (cp_parser* parser)
28142 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
28143 return true;
28144 else
28145 return false;
28148 /* Parse an Objective-C protocol qualifier. */
28150 static tree
28151 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
28153 tree quals = NULL_TREE, node;
28154 cp_token *token = cp_lexer_peek_token (parser->lexer);
28156 node = token->u.value;
28158 while (node && identifier_p (node)
28159 && (node == ridpointers [(int) RID_IN]
28160 || node == ridpointers [(int) RID_OUT]
28161 || node == ridpointers [(int) RID_INOUT]
28162 || node == ridpointers [(int) RID_BYCOPY]
28163 || node == ridpointers [(int) RID_BYREF]
28164 || node == ridpointers [(int) RID_ONEWAY]))
28166 quals = tree_cons (NULL_TREE, node, quals);
28167 cp_lexer_consume_token (parser->lexer);
28168 token = cp_lexer_peek_token (parser->lexer);
28169 node = token->u.value;
28172 return quals;
28175 /* Parse an Objective-C typename. */
28177 static tree
28178 cp_parser_objc_typename (cp_parser* parser)
28180 tree type_name = NULL_TREE;
28182 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28184 tree proto_quals, cp_type = NULL_TREE;
28186 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
28187 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
28189 /* An ObjC type name may consist of just protocol qualifiers, in which
28190 case the type shall default to 'id'. */
28191 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
28193 cp_type = cp_parser_type_id (parser);
28195 /* If the type could not be parsed, an error has already
28196 been produced. For error recovery, behave as if it had
28197 not been specified, which will use the default type
28198 'id'. */
28199 if (cp_type == error_mark_node)
28201 cp_type = NULL_TREE;
28202 /* We need to skip to the closing parenthesis as
28203 cp_parser_type_id() does not seem to do it for
28204 us. */
28205 cp_parser_skip_to_closing_parenthesis (parser,
28206 /*recovering=*/true,
28207 /*or_comma=*/false,
28208 /*consume_paren=*/false);
28212 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28213 type_name = build_tree_list (proto_quals, cp_type);
28216 return type_name;
28219 /* Check to see if TYPE refers to an Objective-C selector name. */
28221 static bool
28222 cp_parser_objc_selector_p (enum cpp_ttype type)
28224 return (type == CPP_NAME || type == CPP_KEYWORD
28225 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
28226 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
28227 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
28228 || type == CPP_XOR || type == CPP_XOR_EQ);
28231 /* Parse an Objective-C selector. */
28233 static tree
28234 cp_parser_objc_selector (cp_parser* parser)
28236 cp_token *token = cp_lexer_consume_token (parser->lexer);
28238 if (!cp_parser_objc_selector_p (token->type))
28240 error_at (token->location, "invalid Objective-C++ selector name");
28241 return error_mark_node;
28244 /* C++ operator names are allowed to appear in ObjC selectors. */
28245 switch (token->type)
28247 case CPP_AND_AND: return get_identifier ("and");
28248 case CPP_AND_EQ: return get_identifier ("and_eq");
28249 case CPP_AND: return get_identifier ("bitand");
28250 case CPP_OR: return get_identifier ("bitor");
28251 case CPP_COMPL: return get_identifier ("compl");
28252 case CPP_NOT: return get_identifier ("not");
28253 case CPP_NOT_EQ: return get_identifier ("not_eq");
28254 case CPP_OR_OR: return get_identifier ("or");
28255 case CPP_OR_EQ: return get_identifier ("or_eq");
28256 case CPP_XOR: return get_identifier ("xor");
28257 case CPP_XOR_EQ: return get_identifier ("xor_eq");
28258 default: return token->u.value;
28262 /* Parse an Objective-C params list. */
28264 static tree
28265 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
28267 tree params = NULL_TREE;
28268 bool maybe_unary_selector_p = true;
28269 cp_token *token = cp_lexer_peek_token (parser->lexer);
28271 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
28273 tree selector = NULL_TREE, type_name, identifier;
28274 tree parm_attr = NULL_TREE;
28276 if (token->keyword == RID_ATTRIBUTE)
28277 break;
28279 if (token->type != CPP_COLON)
28280 selector = cp_parser_objc_selector (parser);
28282 /* Detect if we have a unary selector. */
28283 if (maybe_unary_selector_p
28284 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28286 params = selector; /* Might be followed by attributes. */
28287 break;
28290 maybe_unary_selector_p = false;
28291 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28293 /* Something went quite wrong. There should be a colon
28294 here, but there is not. Stop parsing parameters. */
28295 break;
28297 type_name = cp_parser_objc_typename (parser);
28298 /* New ObjC allows attributes on parameters too. */
28299 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
28300 parm_attr = cp_parser_attributes_opt (parser);
28301 identifier = cp_parser_identifier (parser);
28303 params
28304 = chainon (params,
28305 objc_build_keyword_decl (selector,
28306 type_name,
28307 identifier,
28308 parm_attr));
28310 token = cp_lexer_peek_token (parser->lexer);
28313 if (params == NULL_TREE)
28315 cp_parser_error (parser, "objective-c++ method declaration is expected");
28316 return error_mark_node;
28319 /* We allow tail attributes for the method. */
28320 if (token->keyword == RID_ATTRIBUTE)
28322 *attributes = cp_parser_attributes_opt (parser);
28323 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28324 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28325 return params;
28326 cp_parser_error (parser,
28327 "method attributes must be specified at the end");
28328 return error_mark_node;
28331 if (params == NULL_TREE)
28333 cp_parser_error (parser, "objective-c++ method declaration is expected");
28334 return error_mark_node;
28336 return params;
28339 /* Parse the non-keyword Objective-C params. */
28341 static tree
28342 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
28343 tree* attributes)
28345 tree params = make_node (TREE_LIST);
28346 cp_token *token = cp_lexer_peek_token (parser->lexer);
28347 *ellipsisp = false; /* Initially, assume no ellipsis. */
28349 while (token->type == CPP_COMMA)
28351 cp_parameter_declarator *parmdecl;
28352 tree parm;
28354 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28355 token = cp_lexer_peek_token (parser->lexer);
28357 if (token->type == CPP_ELLIPSIS)
28359 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
28360 *ellipsisp = true;
28361 token = cp_lexer_peek_token (parser->lexer);
28362 break;
28365 /* TODO: parse attributes for tail parameters. */
28366 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
28367 parm = grokdeclarator (parmdecl->declarator,
28368 &parmdecl->decl_specifiers,
28369 PARM, /*initialized=*/0,
28370 /*attrlist=*/NULL);
28372 chainon (params, build_tree_list (NULL_TREE, parm));
28373 token = cp_lexer_peek_token (parser->lexer);
28376 /* We allow tail attributes for the method. */
28377 if (token->keyword == RID_ATTRIBUTE)
28379 if (*attributes == NULL_TREE)
28381 *attributes = cp_parser_attributes_opt (parser);
28382 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28383 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28384 return params;
28386 else
28387 /* We have an error, but parse the attributes, so that we can
28388 carry on. */
28389 *attributes = cp_parser_attributes_opt (parser);
28391 cp_parser_error (parser,
28392 "method attributes must be specified at the end");
28393 return error_mark_node;
28396 return params;
28399 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
28401 static void
28402 cp_parser_objc_interstitial_code (cp_parser* parser)
28404 cp_token *token = cp_lexer_peek_token (parser->lexer);
28406 /* If the next token is `extern' and the following token is a string
28407 literal, then we have a linkage specification. */
28408 if (token->keyword == RID_EXTERN
28409 && cp_parser_is_pure_string_literal
28410 (cp_lexer_peek_nth_token (parser->lexer, 2)))
28411 cp_parser_linkage_specification (parser);
28412 /* Handle #pragma, if any. */
28413 else if (token->type == CPP_PRAGMA)
28414 cp_parser_pragma (parser, pragma_objc_icode);
28415 /* Allow stray semicolons. */
28416 else if (token->type == CPP_SEMICOLON)
28417 cp_lexer_consume_token (parser->lexer);
28418 /* Mark methods as optional or required, when building protocols. */
28419 else if (token->keyword == RID_AT_OPTIONAL)
28421 cp_lexer_consume_token (parser->lexer);
28422 objc_set_method_opt (true);
28424 else if (token->keyword == RID_AT_REQUIRED)
28426 cp_lexer_consume_token (parser->lexer);
28427 objc_set_method_opt (false);
28429 else if (token->keyword == RID_NAMESPACE)
28430 cp_parser_namespace_definition (parser);
28431 /* Other stray characters must generate errors. */
28432 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
28434 cp_lexer_consume_token (parser->lexer);
28435 error ("stray %qs between Objective-C++ methods",
28436 token->type == CPP_OPEN_BRACE ? "{" : "}");
28438 /* Finally, try to parse a block-declaration, or a function-definition. */
28439 else
28440 cp_parser_block_declaration (parser, /*statement_p=*/false);
28443 /* Parse a method signature. */
28445 static tree
28446 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
28448 tree rettype, kwdparms, optparms;
28449 bool ellipsis = false;
28450 bool is_class_method;
28452 is_class_method = cp_parser_objc_method_type (parser);
28453 rettype = cp_parser_objc_typename (parser);
28454 *attributes = NULL_TREE;
28455 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
28456 if (kwdparms == error_mark_node)
28457 return error_mark_node;
28458 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
28459 if (optparms == error_mark_node)
28460 return error_mark_node;
28462 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
28465 static bool
28466 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
28468 tree tattr;
28469 cp_lexer_save_tokens (parser->lexer);
28470 tattr = cp_parser_attributes_opt (parser);
28471 gcc_assert (tattr) ;
28473 /* If the attributes are followed by a method introducer, this is not allowed.
28474 Dump the attributes and flag the situation. */
28475 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
28476 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
28477 return true;
28479 /* Otherwise, the attributes introduce some interstitial code, possibly so
28480 rewind to allow that check. */
28481 cp_lexer_rollback_tokens (parser->lexer);
28482 return false;
28485 /* Parse an Objective-C method prototype list. */
28487 static void
28488 cp_parser_objc_method_prototype_list (cp_parser* parser)
28490 cp_token *token = cp_lexer_peek_token (parser->lexer);
28492 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
28494 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
28496 tree attributes, sig;
28497 bool is_class_method;
28498 if (token->type == CPP_PLUS)
28499 is_class_method = true;
28500 else
28501 is_class_method = false;
28502 sig = cp_parser_objc_method_signature (parser, &attributes);
28503 if (sig == error_mark_node)
28505 cp_parser_skip_to_end_of_block_or_statement (parser);
28506 token = cp_lexer_peek_token (parser->lexer);
28507 continue;
28509 objc_add_method_declaration (is_class_method, sig, attributes);
28510 cp_parser_consume_semicolon_at_end_of_statement (parser);
28512 else if (token->keyword == RID_AT_PROPERTY)
28513 cp_parser_objc_at_property_declaration (parser);
28514 else if (token->keyword == RID_ATTRIBUTE
28515 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
28516 warning_at (cp_lexer_peek_token (parser->lexer)->location,
28517 OPT_Wattributes,
28518 "prefix attributes are ignored for methods");
28519 else
28520 /* Allow for interspersed non-ObjC++ code. */
28521 cp_parser_objc_interstitial_code (parser);
28523 token = cp_lexer_peek_token (parser->lexer);
28526 if (token->type != CPP_EOF)
28527 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
28528 else
28529 cp_parser_error (parser, "expected %<@end%>");
28531 objc_finish_interface ();
28534 /* Parse an Objective-C method definition list. */
28536 static void
28537 cp_parser_objc_method_definition_list (cp_parser* parser)
28539 cp_token *token = cp_lexer_peek_token (parser->lexer);
28541 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
28543 tree meth;
28545 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
28547 cp_token *ptk;
28548 tree sig, attribute;
28549 bool is_class_method;
28550 if (token->type == CPP_PLUS)
28551 is_class_method = true;
28552 else
28553 is_class_method = false;
28554 push_deferring_access_checks (dk_deferred);
28555 sig = cp_parser_objc_method_signature (parser, &attribute);
28556 if (sig == error_mark_node)
28558 cp_parser_skip_to_end_of_block_or_statement (parser);
28559 token = cp_lexer_peek_token (parser->lexer);
28560 continue;
28562 objc_start_method_definition (is_class_method, sig, attribute,
28563 NULL_TREE);
28565 /* For historical reasons, we accept an optional semicolon. */
28566 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28567 cp_lexer_consume_token (parser->lexer);
28569 ptk = cp_lexer_peek_token (parser->lexer);
28570 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
28571 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
28573 perform_deferred_access_checks (tf_warning_or_error);
28574 stop_deferring_access_checks ();
28575 meth = cp_parser_function_definition_after_declarator (parser,
28576 false);
28577 pop_deferring_access_checks ();
28578 objc_finish_method_definition (meth);
28581 /* The following case will be removed once @synthesize is
28582 completely implemented. */
28583 else if (token->keyword == RID_AT_PROPERTY)
28584 cp_parser_objc_at_property_declaration (parser);
28585 else if (token->keyword == RID_AT_SYNTHESIZE)
28586 cp_parser_objc_at_synthesize_declaration (parser);
28587 else if (token->keyword == RID_AT_DYNAMIC)
28588 cp_parser_objc_at_dynamic_declaration (parser);
28589 else if (token->keyword == RID_ATTRIBUTE
28590 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
28591 warning_at (token->location, OPT_Wattributes,
28592 "prefix attributes are ignored for methods");
28593 else
28594 /* Allow for interspersed non-ObjC++ code. */
28595 cp_parser_objc_interstitial_code (parser);
28597 token = cp_lexer_peek_token (parser->lexer);
28600 if (token->type != CPP_EOF)
28601 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
28602 else
28603 cp_parser_error (parser, "expected %<@end%>");
28605 objc_finish_implementation ();
28608 /* Parse Objective-C ivars. */
28610 static void
28611 cp_parser_objc_class_ivars (cp_parser* parser)
28613 cp_token *token = cp_lexer_peek_token (parser->lexer);
28615 if (token->type != CPP_OPEN_BRACE)
28616 return; /* No ivars specified. */
28618 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
28619 token = cp_lexer_peek_token (parser->lexer);
28621 while (token->type != CPP_CLOSE_BRACE
28622 && token->keyword != RID_AT_END && token->type != CPP_EOF)
28624 cp_decl_specifier_seq declspecs;
28625 int decl_class_or_enum_p;
28626 tree prefix_attributes;
28628 cp_parser_objc_visibility_spec (parser);
28630 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28631 break;
28633 cp_parser_decl_specifier_seq (parser,
28634 CP_PARSER_FLAGS_OPTIONAL,
28635 &declspecs,
28636 &decl_class_or_enum_p);
28638 /* auto, register, static, extern, mutable. */
28639 if (declspecs.storage_class != sc_none)
28641 cp_parser_error (parser, "invalid type for instance variable");
28642 declspecs.storage_class = sc_none;
28645 /* thread_local. */
28646 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
28648 cp_parser_error (parser, "invalid type for instance variable");
28649 declspecs.locations[ds_thread] = 0;
28652 /* typedef. */
28653 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
28655 cp_parser_error (parser, "invalid type for instance variable");
28656 declspecs.locations[ds_typedef] = 0;
28659 prefix_attributes = declspecs.attributes;
28660 declspecs.attributes = NULL_TREE;
28662 /* Keep going until we hit the `;' at the end of the
28663 declaration. */
28664 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28666 tree width = NULL_TREE, attributes, first_attribute, decl;
28667 cp_declarator *declarator = NULL;
28668 int ctor_dtor_or_conv_p;
28670 /* Check for a (possibly unnamed) bitfield declaration. */
28671 token = cp_lexer_peek_token (parser->lexer);
28672 if (token->type == CPP_COLON)
28673 goto eat_colon;
28675 if (token->type == CPP_NAME
28676 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
28677 == CPP_COLON))
28679 /* Get the name of the bitfield. */
28680 declarator = make_id_declarator (NULL_TREE,
28681 cp_parser_identifier (parser),
28682 sfk_none);
28684 eat_colon:
28685 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
28686 /* Get the width of the bitfield. */
28687 width
28688 = cp_parser_constant_expression (parser);
28690 else
28692 /* Parse the declarator. */
28693 declarator
28694 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28695 &ctor_dtor_or_conv_p,
28696 /*parenthesized_p=*/NULL,
28697 /*member_p=*/false,
28698 /*friend_p=*/false);
28701 /* Look for attributes that apply to the ivar. */
28702 attributes = cp_parser_attributes_opt (parser);
28703 /* Remember which attributes are prefix attributes and
28704 which are not. */
28705 first_attribute = attributes;
28706 /* Combine the attributes. */
28707 attributes = chainon (prefix_attributes, attributes);
28709 if (width)
28710 /* Create the bitfield declaration. */
28711 decl = grokbitfield (declarator, &declspecs,
28712 width,
28713 attributes);
28714 else
28715 decl = grokfield (declarator, &declspecs,
28716 NULL_TREE, /*init_const_expr_p=*/false,
28717 NULL_TREE, attributes);
28719 /* Add the instance variable. */
28720 if (decl != error_mark_node && decl != NULL_TREE)
28721 objc_add_instance_variable (decl);
28723 /* Reset PREFIX_ATTRIBUTES. */
28724 while (attributes && TREE_CHAIN (attributes) != first_attribute)
28725 attributes = TREE_CHAIN (attributes);
28726 if (attributes)
28727 TREE_CHAIN (attributes) = NULL_TREE;
28729 token = cp_lexer_peek_token (parser->lexer);
28731 if (token->type == CPP_COMMA)
28733 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28734 continue;
28736 break;
28739 cp_parser_consume_semicolon_at_end_of_statement (parser);
28740 token = cp_lexer_peek_token (parser->lexer);
28743 if (token->keyword == RID_AT_END)
28744 cp_parser_error (parser, "expected %<}%>");
28746 /* Do not consume the RID_AT_END, so it will be read again as terminating
28747 the @interface of @implementation. */
28748 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
28749 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
28751 /* For historical reasons, we accept an optional semicolon. */
28752 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28753 cp_lexer_consume_token (parser->lexer);
28756 /* Parse an Objective-C protocol declaration. */
28758 static void
28759 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
28761 tree proto, protorefs;
28762 cp_token *tok;
28764 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
28765 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
28767 tok = cp_lexer_peek_token (parser->lexer);
28768 error_at (tok->location, "identifier expected after %<@protocol%>");
28769 cp_parser_consume_semicolon_at_end_of_statement (parser);
28770 return;
28773 /* See if we have a forward declaration or a definition. */
28774 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
28776 /* Try a forward declaration first. */
28777 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
28779 while (true)
28781 tree id;
28783 id = cp_parser_identifier (parser);
28784 if (id == error_mark_node)
28785 break;
28787 objc_declare_protocol (id, attributes);
28789 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28790 cp_lexer_consume_token (parser->lexer);
28791 else
28792 break;
28794 cp_parser_consume_semicolon_at_end_of_statement (parser);
28797 /* Ok, we got a full-fledged definition (or at least should). */
28798 else
28800 proto = cp_parser_identifier (parser);
28801 protorefs = cp_parser_objc_protocol_refs_opt (parser);
28802 objc_start_protocol (proto, protorefs, attributes);
28803 cp_parser_objc_method_prototype_list (parser);
28807 /* Parse an Objective-C superclass or category. */
28809 static void
28810 cp_parser_objc_superclass_or_category (cp_parser *parser,
28811 bool iface_p,
28812 tree *super,
28813 tree *categ, bool *is_class_extension)
28815 cp_token *next = cp_lexer_peek_token (parser->lexer);
28817 *super = *categ = NULL_TREE;
28818 *is_class_extension = false;
28819 if (next->type == CPP_COLON)
28821 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
28822 *super = cp_parser_identifier (parser);
28824 else if (next->type == CPP_OPEN_PAREN)
28826 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
28828 /* If there is no category name, and this is an @interface, we
28829 have a class extension. */
28830 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
28832 *categ = NULL_TREE;
28833 *is_class_extension = true;
28835 else
28836 *categ = cp_parser_identifier (parser);
28838 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28842 /* Parse an Objective-C class interface. */
28844 static void
28845 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
28847 tree name, super, categ, protos;
28848 bool is_class_extension;
28850 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
28851 name = cp_parser_identifier (parser);
28852 if (name == error_mark_node)
28854 /* It's hard to recover because even if valid @interface stuff
28855 is to follow, we can't compile it (or validate it) if we
28856 don't even know which class it refers to. Let's assume this
28857 was a stray '@interface' token in the stream and skip it.
28859 return;
28861 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
28862 &is_class_extension);
28863 protos = cp_parser_objc_protocol_refs_opt (parser);
28865 /* We have either a class or a category on our hands. */
28866 if (categ || is_class_extension)
28867 objc_start_category_interface (name, categ, protos, attributes);
28868 else
28870 objc_start_class_interface (name, super, protos, attributes);
28871 /* Handle instance variable declarations, if any. */
28872 cp_parser_objc_class_ivars (parser);
28873 objc_continue_interface ();
28876 cp_parser_objc_method_prototype_list (parser);
28879 /* Parse an Objective-C class implementation. */
28881 static void
28882 cp_parser_objc_class_implementation (cp_parser* parser)
28884 tree name, super, categ;
28885 bool is_class_extension;
28887 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
28888 name = cp_parser_identifier (parser);
28889 if (name == error_mark_node)
28891 /* It's hard to recover because even if valid @implementation
28892 stuff is to follow, we can't compile it (or validate it) if
28893 we don't even know which class it refers to. Let's assume
28894 this was a stray '@implementation' token in the stream and
28895 skip it.
28897 return;
28899 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
28900 &is_class_extension);
28902 /* We have either a class or a category on our hands. */
28903 if (categ)
28904 objc_start_category_implementation (name, categ);
28905 else
28907 objc_start_class_implementation (name, super);
28908 /* Handle instance variable declarations, if any. */
28909 cp_parser_objc_class_ivars (parser);
28910 objc_continue_implementation ();
28913 cp_parser_objc_method_definition_list (parser);
28916 /* Consume the @end token and finish off the implementation. */
28918 static void
28919 cp_parser_objc_end_implementation (cp_parser* parser)
28921 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
28922 objc_finish_implementation ();
28925 /* Parse an Objective-C declaration. */
28927 static void
28928 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
28930 /* Try to figure out what kind of declaration is present. */
28931 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
28933 if (attributes)
28934 switch (kwd->keyword)
28936 case RID_AT_ALIAS:
28937 case RID_AT_CLASS:
28938 case RID_AT_END:
28939 error_at (kwd->location, "attributes may not be specified before"
28940 " the %<@%D%> Objective-C++ keyword",
28941 kwd->u.value);
28942 attributes = NULL;
28943 break;
28944 case RID_AT_IMPLEMENTATION:
28945 warning_at (kwd->location, OPT_Wattributes,
28946 "prefix attributes are ignored before %<@%D%>",
28947 kwd->u.value);
28948 attributes = NULL;
28949 default:
28950 break;
28953 switch (kwd->keyword)
28955 case RID_AT_ALIAS:
28956 cp_parser_objc_alias_declaration (parser);
28957 break;
28958 case RID_AT_CLASS:
28959 cp_parser_objc_class_declaration (parser);
28960 break;
28961 case RID_AT_PROTOCOL:
28962 cp_parser_objc_protocol_declaration (parser, attributes);
28963 break;
28964 case RID_AT_INTERFACE:
28965 cp_parser_objc_class_interface (parser, attributes);
28966 break;
28967 case RID_AT_IMPLEMENTATION:
28968 cp_parser_objc_class_implementation (parser);
28969 break;
28970 case RID_AT_END:
28971 cp_parser_objc_end_implementation (parser);
28972 break;
28973 default:
28974 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
28975 kwd->u.value);
28976 cp_parser_skip_to_end_of_block_or_statement (parser);
28980 /* Parse an Objective-C try-catch-finally statement.
28982 objc-try-catch-finally-stmt:
28983 @try compound-statement objc-catch-clause-seq [opt]
28984 objc-finally-clause [opt]
28986 objc-catch-clause-seq:
28987 objc-catch-clause objc-catch-clause-seq [opt]
28989 objc-catch-clause:
28990 @catch ( objc-exception-declaration ) compound-statement
28992 objc-finally-clause:
28993 @finally compound-statement
28995 objc-exception-declaration:
28996 parameter-declaration
28997 '...'
28999 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
29001 Returns NULL_TREE.
29003 PS: This function is identical to c_parser_objc_try_catch_finally_statement
29004 for C. Keep them in sync. */
29006 static tree
29007 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
29009 location_t location;
29010 tree stmt;
29012 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
29013 location = cp_lexer_peek_token (parser->lexer)->location;
29014 objc_maybe_warn_exceptions (location);
29015 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
29016 node, lest it get absorbed into the surrounding block. */
29017 stmt = push_stmt_list ();
29018 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29019 objc_begin_try_stmt (location, pop_stmt_list (stmt));
29021 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
29023 cp_parameter_declarator *parm;
29024 tree parameter_declaration = error_mark_node;
29025 bool seen_open_paren = false;
29027 cp_lexer_consume_token (parser->lexer);
29028 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29029 seen_open_paren = true;
29030 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29032 /* We have "@catch (...)" (where the '...' are literally
29033 what is in the code). Skip the '...'.
29034 parameter_declaration is set to NULL_TREE, and
29035 objc_being_catch_clauses() knows that that means
29036 '...'. */
29037 cp_lexer_consume_token (parser->lexer);
29038 parameter_declaration = NULL_TREE;
29040 else
29042 /* We have "@catch (NSException *exception)" or something
29043 like that. Parse the parameter declaration. */
29044 parm = cp_parser_parameter_declaration (parser, false, NULL);
29045 if (parm == NULL)
29046 parameter_declaration = error_mark_node;
29047 else
29048 parameter_declaration = grokdeclarator (parm->declarator,
29049 &parm->decl_specifiers,
29050 PARM, /*initialized=*/0,
29051 /*attrlist=*/NULL);
29053 if (seen_open_paren)
29054 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29055 else
29057 /* If there was no open parenthesis, we are recovering from
29058 an error, and we are trying to figure out what mistake
29059 the user has made. */
29061 /* If there is an immediate closing parenthesis, the user
29062 probably forgot the opening one (ie, they typed "@catch
29063 NSException *e)". Parse the closing parenthesis and keep
29064 going. */
29065 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29066 cp_lexer_consume_token (parser->lexer);
29068 /* If these is no immediate closing parenthesis, the user
29069 probably doesn't know that parenthesis are required at
29070 all (ie, they typed "@catch NSException *e"). So, just
29071 forget about the closing parenthesis and keep going. */
29073 objc_begin_catch_clause (parameter_declaration);
29074 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29075 objc_finish_catch_clause ();
29077 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
29079 cp_lexer_consume_token (parser->lexer);
29080 location = cp_lexer_peek_token (parser->lexer)->location;
29081 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
29082 node, lest it get absorbed into the surrounding block. */
29083 stmt = push_stmt_list ();
29084 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29085 objc_build_finally_clause (location, pop_stmt_list (stmt));
29088 return objc_finish_try_stmt ();
29091 /* Parse an Objective-C synchronized statement.
29093 objc-synchronized-stmt:
29094 @synchronized ( expression ) compound-statement
29096 Returns NULL_TREE. */
29098 static tree
29099 cp_parser_objc_synchronized_statement (cp_parser *parser)
29101 location_t location;
29102 tree lock, stmt;
29104 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
29106 location = cp_lexer_peek_token (parser->lexer)->location;
29107 objc_maybe_warn_exceptions (location);
29108 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
29109 lock = cp_parser_expression (parser);
29110 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29112 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
29113 node, lest it get absorbed into the surrounding block. */
29114 stmt = push_stmt_list ();
29115 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29117 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
29120 /* Parse an Objective-C throw statement.
29122 objc-throw-stmt:
29123 @throw assignment-expression [opt] ;
29125 Returns a constructed '@throw' statement. */
29127 static tree
29128 cp_parser_objc_throw_statement (cp_parser *parser)
29130 tree expr = NULL_TREE;
29131 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29133 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
29135 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29136 expr = cp_parser_expression (parser);
29138 cp_parser_consume_semicolon_at_end_of_statement (parser);
29140 return objc_build_throw_stmt (loc, expr);
29143 /* Parse an Objective-C statement. */
29145 static tree
29146 cp_parser_objc_statement (cp_parser * parser)
29148 /* Try to figure out what kind of declaration is present. */
29149 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29151 switch (kwd->keyword)
29153 case RID_AT_TRY:
29154 return cp_parser_objc_try_catch_finally_statement (parser);
29155 case RID_AT_SYNCHRONIZED:
29156 return cp_parser_objc_synchronized_statement (parser);
29157 case RID_AT_THROW:
29158 return cp_parser_objc_throw_statement (parser);
29159 default:
29160 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
29161 kwd->u.value);
29162 cp_parser_skip_to_end_of_block_or_statement (parser);
29165 return error_mark_node;
29168 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
29169 look ahead to see if an objc keyword follows the attributes. This
29170 is to detect the use of prefix attributes on ObjC @interface and
29171 @protocol. */
29173 static bool
29174 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
29176 cp_lexer_save_tokens (parser->lexer);
29177 *attrib = cp_parser_attributes_opt (parser);
29178 gcc_assert (*attrib);
29179 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
29181 cp_lexer_commit_tokens (parser->lexer);
29182 return true;
29184 cp_lexer_rollback_tokens (parser->lexer);
29185 return false;
29188 /* This routine is a minimal replacement for
29189 c_parser_struct_declaration () used when parsing the list of
29190 types/names or ObjC++ properties. For example, when parsing the
29191 code
29193 @property (readonly) int a, b, c;
29195 this function is responsible for parsing "int a, int b, int c" and
29196 returning the declarations as CHAIN of DECLs.
29198 TODO: Share this code with cp_parser_objc_class_ivars. It's very
29199 similar parsing. */
29200 static tree
29201 cp_parser_objc_struct_declaration (cp_parser *parser)
29203 tree decls = NULL_TREE;
29204 cp_decl_specifier_seq declspecs;
29205 int decl_class_or_enum_p;
29206 tree prefix_attributes;
29208 cp_parser_decl_specifier_seq (parser,
29209 CP_PARSER_FLAGS_NONE,
29210 &declspecs,
29211 &decl_class_or_enum_p);
29213 if (declspecs.type == error_mark_node)
29214 return error_mark_node;
29216 /* auto, register, static, extern, mutable. */
29217 if (declspecs.storage_class != sc_none)
29219 cp_parser_error (parser, "invalid type for property");
29220 declspecs.storage_class = sc_none;
29223 /* thread_local. */
29224 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
29226 cp_parser_error (parser, "invalid type for property");
29227 declspecs.locations[ds_thread] = 0;
29230 /* typedef. */
29231 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
29233 cp_parser_error (parser, "invalid type for property");
29234 declspecs.locations[ds_typedef] = 0;
29237 prefix_attributes = declspecs.attributes;
29238 declspecs.attributes = NULL_TREE;
29240 /* Keep going until we hit the `;' at the end of the declaration. */
29241 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29243 tree attributes, first_attribute, decl;
29244 cp_declarator *declarator;
29245 cp_token *token;
29247 /* Parse the declarator. */
29248 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29249 NULL, NULL, false, false);
29251 /* Look for attributes that apply to the ivar. */
29252 attributes = cp_parser_attributes_opt (parser);
29253 /* Remember which attributes are prefix attributes and
29254 which are not. */
29255 first_attribute = attributes;
29256 /* Combine the attributes. */
29257 attributes = chainon (prefix_attributes, attributes);
29259 decl = grokfield (declarator, &declspecs,
29260 NULL_TREE, /*init_const_expr_p=*/false,
29261 NULL_TREE, attributes);
29263 if (decl == error_mark_node || decl == NULL_TREE)
29264 return error_mark_node;
29266 /* Reset PREFIX_ATTRIBUTES. */
29267 while (attributes && TREE_CHAIN (attributes) != first_attribute)
29268 attributes = TREE_CHAIN (attributes);
29269 if (attributes)
29270 TREE_CHAIN (attributes) = NULL_TREE;
29272 DECL_CHAIN (decl) = decls;
29273 decls = decl;
29275 token = cp_lexer_peek_token (parser->lexer);
29276 if (token->type == CPP_COMMA)
29278 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29279 continue;
29281 else
29282 break;
29284 return decls;
29287 /* Parse an Objective-C @property declaration. The syntax is:
29289 objc-property-declaration:
29290 '@property' objc-property-attributes[opt] struct-declaration ;
29292 objc-property-attributes:
29293 '(' objc-property-attribute-list ')'
29295 objc-property-attribute-list:
29296 objc-property-attribute
29297 objc-property-attribute-list, objc-property-attribute
29299 objc-property-attribute
29300 'getter' = identifier
29301 'setter' = identifier
29302 'readonly'
29303 'readwrite'
29304 'assign'
29305 'retain'
29306 'copy'
29307 'nonatomic'
29309 For example:
29310 @property NSString *name;
29311 @property (readonly) id object;
29312 @property (retain, nonatomic, getter=getTheName) id name;
29313 @property int a, b, c;
29315 PS: This function is identical to
29316 c_parser_objc_at_property_declaration for C. Keep them in sync. */
29317 static void
29318 cp_parser_objc_at_property_declaration (cp_parser *parser)
29320 /* The following variables hold the attributes of the properties as
29321 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
29322 seen. When we see an attribute, we set them to 'true' (if they
29323 are boolean properties) or to the identifier (if they have an
29324 argument, ie, for getter and setter). Note that here we only
29325 parse the list of attributes, check the syntax and accumulate the
29326 attributes that we find. objc_add_property_declaration() will
29327 then process the information. */
29328 bool property_assign = false;
29329 bool property_copy = false;
29330 tree property_getter_ident = NULL_TREE;
29331 bool property_nonatomic = false;
29332 bool property_readonly = false;
29333 bool property_readwrite = false;
29334 bool property_retain = false;
29335 tree property_setter_ident = NULL_TREE;
29337 /* 'properties' is the list of properties that we read. Usually a
29338 single one, but maybe more (eg, in "@property int a, b, c;" there
29339 are three). */
29340 tree properties;
29341 location_t loc;
29343 loc = cp_lexer_peek_token (parser->lexer)->location;
29345 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
29347 /* Parse the optional attribute list... */
29348 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29350 /* Eat the '('. */
29351 cp_lexer_consume_token (parser->lexer);
29353 while (true)
29355 bool syntax_error = false;
29356 cp_token *token = cp_lexer_peek_token (parser->lexer);
29357 enum rid keyword;
29359 if (token->type != CPP_NAME)
29361 cp_parser_error (parser, "expected identifier");
29362 break;
29364 keyword = C_RID_CODE (token->u.value);
29365 cp_lexer_consume_token (parser->lexer);
29366 switch (keyword)
29368 case RID_ASSIGN: property_assign = true; break;
29369 case RID_COPY: property_copy = true; break;
29370 case RID_NONATOMIC: property_nonatomic = true; break;
29371 case RID_READONLY: property_readonly = true; break;
29372 case RID_READWRITE: property_readwrite = true; break;
29373 case RID_RETAIN: property_retain = true; break;
29375 case RID_GETTER:
29376 case RID_SETTER:
29377 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29379 if (keyword == RID_GETTER)
29380 cp_parser_error (parser,
29381 "missing %<=%> (after %<getter%> attribute)");
29382 else
29383 cp_parser_error (parser,
29384 "missing %<=%> (after %<setter%> attribute)");
29385 syntax_error = true;
29386 break;
29388 cp_lexer_consume_token (parser->lexer); /* eat the = */
29389 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
29391 cp_parser_error (parser, "expected identifier");
29392 syntax_error = true;
29393 break;
29395 if (keyword == RID_SETTER)
29397 if (property_setter_ident != NULL_TREE)
29399 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
29400 cp_lexer_consume_token (parser->lexer);
29402 else
29403 property_setter_ident = cp_parser_objc_selector (parser);
29404 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29405 cp_parser_error (parser, "setter name must terminate with %<:%>");
29406 else
29407 cp_lexer_consume_token (parser->lexer);
29409 else
29411 if (property_getter_ident != NULL_TREE)
29413 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
29414 cp_lexer_consume_token (parser->lexer);
29416 else
29417 property_getter_ident = cp_parser_objc_selector (parser);
29419 break;
29420 default:
29421 cp_parser_error (parser, "unknown property attribute");
29422 syntax_error = true;
29423 break;
29426 if (syntax_error)
29427 break;
29429 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29430 cp_lexer_consume_token (parser->lexer);
29431 else
29432 break;
29435 /* FIXME: "@property (setter, assign);" will generate a spurious
29436 "error: expected ‘)’ before ‘,’ token". This is because
29437 cp_parser_require, unlike the C counterpart, will produce an
29438 error even if we are in error recovery. */
29439 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29441 cp_parser_skip_to_closing_parenthesis (parser,
29442 /*recovering=*/true,
29443 /*or_comma=*/false,
29444 /*consume_paren=*/true);
29448 /* ... and the property declaration(s). */
29449 properties = cp_parser_objc_struct_declaration (parser);
29451 if (properties == error_mark_node)
29453 cp_parser_skip_to_end_of_statement (parser);
29454 /* If the next token is now a `;', consume it. */
29455 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29456 cp_lexer_consume_token (parser->lexer);
29457 return;
29460 if (properties == NULL_TREE)
29461 cp_parser_error (parser, "expected identifier");
29462 else
29464 /* Comma-separated properties are chained together in
29465 reverse order; add them one by one. */
29466 properties = nreverse (properties);
29468 for (; properties; properties = TREE_CHAIN (properties))
29469 objc_add_property_declaration (loc, copy_node (properties),
29470 property_readonly, property_readwrite,
29471 property_assign, property_retain,
29472 property_copy, property_nonatomic,
29473 property_getter_ident, property_setter_ident);
29476 cp_parser_consume_semicolon_at_end_of_statement (parser);
29479 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
29481 objc-synthesize-declaration:
29482 @synthesize objc-synthesize-identifier-list ;
29484 objc-synthesize-identifier-list:
29485 objc-synthesize-identifier
29486 objc-synthesize-identifier-list, objc-synthesize-identifier
29488 objc-synthesize-identifier
29489 identifier
29490 identifier = identifier
29492 For example:
29493 @synthesize MyProperty;
29494 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
29496 PS: This function is identical to c_parser_objc_at_synthesize_declaration
29497 for C. Keep them in sync.
29499 static void
29500 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
29502 tree list = NULL_TREE;
29503 location_t loc;
29504 loc = cp_lexer_peek_token (parser->lexer)->location;
29506 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
29507 while (true)
29509 tree property, ivar;
29510 property = cp_parser_identifier (parser);
29511 if (property == error_mark_node)
29513 cp_parser_consume_semicolon_at_end_of_statement (parser);
29514 return;
29516 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
29518 cp_lexer_consume_token (parser->lexer);
29519 ivar = cp_parser_identifier (parser);
29520 if (ivar == error_mark_node)
29522 cp_parser_consume_semicolon_at_end_of_statement (parser);
29523 return;
29526 else
29527 ivar = NULL_TREE;
29528 list = chainon (list, build_tree_list (ivar, property));
29529 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29530 cp_lexer_consume_token (parser->lexer);
29531 else
29532 break;
29534 cp_parser_consume_semicolon_at_end_of_statement (parser);
29535 objc_add_synthesize_declaration (loc, list);
29538 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
29540 objc-dynamic-declaration:
29541 @dynamic identifier-list ;
29543 For example:
29544 @dynamic MyProperty;
29545 @dynamic MyProperty, AnotherProperty;
29547 PS: This function is identical to c_parser_objc_at_dynamic_declaration
29548 for C. Keep them in sync.
29550 static void
29551 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
29553 tree list = NULL_TREE;
29554 location_t loc;
29555 loc = cp_lexer_peek_token (parser->lexer)->location;
29557 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
29558 while (true)
29560 tree property;
29561 property = cp_parser_identifier (parser);
29562 if (property == error_mark_node)
29564 cp_parser_consume_semicolon_at_end_of_statement (parser);
29565 return;
29567 list = chainon (list, build_tree_list (NULL, property));
29568 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29569 cp_lexer_consume_token (parser->lexer);
29570 else
29571 break;
29573 cp_parser_consume_semicolon_at_end_of_statement (parser);
29574 objc_add_dynamic_declaration (loc, list);
29578 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
29580 /* Returns name of the next clause.
29581 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
29582 the token is not consumed. Otherwise appropriate pragma_omp_clause is
29583 returned and the token is consumed. */
29585 static pragma_omp_clause
29586 cp_parser_omp_clause_name (cp_parser *parser)
29588 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
29590 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
29591 result = PRAGMA_OACC_CLAUSE_AUTO;
29592 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
29593 result = PRAGMA_OMP_CLAUSE_IF;
29594 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
29595 result = PRAGMA_OMP_CLAUSE_DEFAULT;
29596 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
29597 result = PRAGMA_OACC_CLAUSE_DELETE;
29598 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
29599 result = PRAGMA_OMP_CLAUSE_PRIVATE;
29600 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29601 result = PRAGMA_OMP_CLAUSE_FOR;
29602 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29604 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29605 const char *p = IDENTIFIER_POINTER (id);
29607 switch (p[0])
29609 case 'a':
29610 if (!strcmp ("aligned", p))
29611 result = PRAGMA_OMP_CLAUSE_ALIGNED;
29612 else if (!strcmp ("async", p))
29613 result = PRAGMA_OACC_CLAUSE_ASYNC;
29614 break;
29615 case 'c':
29616 if (!strcmp ("collapse", p))
29617 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
29618 else if (!strcmp ("copy", p))
29619 result = PRAGMA_OACC_CLAUSE_COPY;
29620 else if (!strcmp ("copyin", p))
29621 result = PRAGMA_OMP_CLAUSE_COPYIN;
29622 else if (!strcmp ("copyout", p))
29623 result = PRAGMA_OACC_CLAUSE_COPYOUT;
29624 else if (!strcmp ("copyprivate", p))
29625 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
29626 else if (!strcmp ("create", p))
29627 result = PRAGMA_OACC_CLAUSE_CREATE;
29628 break;
29629 case 'd':
29630 if (!strcmp ("defaultmap", p))
29631 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
29632 else if (!strcmp ("depend", p))
29633 result = PRAGMA_OMP_CLAUSE_DEPEND;
29634 else if (!strcmp ("device", p))
29635 result = PRAGMA_OMP_CLAUSE_DEVICE;
29636 else if (!strcmp ("deviceptr", p))
29637 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
29638 else if (!strcmp ("device_resident", p))
29639 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
29640 else if (!strcmp ("dist_schedule", p))
29641 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
29642 break;
29643 case 'f':
29644 if (!strcmp ("final", p))
29645 result = PRAGMA_OMP_CLAUSE_FINAL;
29646 else if (!strcmp ("firstprivate", p))
29647 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
29648 else if (!strcmp ("from", p))
29649 result = PRAGMA_OMP_CLAUSE_FROM;
29650 break;
29651 case 'g':
29652 if (!strcmp ("gang", p))
29653 result = PRAGMA_OACC_CLAUSE_GANG;
29654 else if (!strcmp ("grainsize", p))
29655 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
29656 break;
29657 case 'h':
29658 if (!strcmp ("hint", p))
29659 result = PRAGMA_OMP_CLAUSE_HINT;
29660 else if (!strcmp ("host", p))
29661 result = PRAGMA_OACC_CLAUSE_HOST;
29662 break;
29663 case 'i':
29664 if (!strcmp ("inbranch", p))
29665 result = PRAGMA_OMP_CLAUSE_INBRANCH;
29666 else if (!strcmp ("independent", p))
29667 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
29668 else if (!strcmp ("is_device_ptr", p))
29669 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
29670 break;
29671 case 'l':
29672 if (!strcmp ("lastprivate", p))
29673 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
29674 else if (!strcmp ("linear", p))
29675 result = PRAGMA_OMP_CLAUSE_LINEAR;
29676 else if (!strcmp ("link", p))
29677 result = PRAGMA_OMP_CLAUSE_LINK;
29678 break;
29679 case 'm':
29680 if (!strcmp ("map", p))
29681 result = PRAGMA_OMP_CLAUSE_MAP;
29682 else if (!strcmp ("mergeable", p))
29683 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
29684 else if (flag_cilkplus && !strcmp ("mask", p))
29685 result = PRAGMA_CILK_CLAUSE_MASK;
29686 break;
29687 case 'n':
29688 if (!strcmp ("nogroup", p))
29689 result = PRAGMA_OMP_CLAUSE_NOGROUP;
29690 else if (!strcmp ("notinbranch", p))
29691 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
29692 else if (!strcmp ("nowait", p))
29693 result = PRAGMA_OMP_CLAUSE_NOWAIT;
29694 else if (flag_cilkplus && !strcmp ("nomask", p))
29695 result = PRAGMA_CILK_CLAUSE_NOMASK;
29696 else if (!strcmp ("num_gangs", p))
29697 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
29698 else if (!strcmp ("num_tasks", p))
29699 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
29700 else if (!strcmp ("num_teams", p))
29701 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
29702 else if (!strcmp ("num_threads", p))
29703 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
29704 else if (!strcmp ("num_workers", p))
29705 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
29706 break;
29707 case 'o':
29708 if (!strcmp ("ordered", p))
29709 result = PRAGMA_OMP_CLAUSE_ORDERED;
29710 break;
29711 case 'p':
29712 if (!strcmp ("parallel", p))
29713 result = PRAGMA_OMP_CLAUSE_PARALLEL;
29714 else if (!strcmp ("present", p))
29715 result = PRAGMA_OACC_CLAUSE_PRESENT;
29716 else if (!strcmp ("present_or_copy", p)
29717 || !strcmp ("pcopy", p))
29718 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
29719 else if (!strcmp ("present_or_copyin", p)
29720 || !strcmp ("pcopyin", p))
29721 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
29722 else if (!strcmp ("present_or_copyout", p)
29723 || !strcmp ("pcopyout", p))
29724 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
29725 else if (!strcmp ("present_or_create", p)
29726 || !strcmp ("pcreate", p))
29727 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
29728 else if (!strcmp ("priority", p))
29729 result = PRAGMA_OMP_CLAUSE_PRIORITY;
29730 else if (!strcmp ("proc_bind", p))
29731 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
29732 break;
29733 case 'r':
29734 if (!strcmp ("reduction", p))
29735 result = PRAGMA_OMP_CLAUSE_REDUCTION;
29736 break;
29737 case 's':
29738 if (!strcmp ("safelen", p))
29739 result = PRAGMA_OMP_CLAUSE_SAFELEN;
29740 else if (!strcmp ("schedule", p))
29741 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
29742 else if (!strcmp ("sections", p))
29743 result = PRAGMA_OMP_CLAUSE_SECTIONS;
29744 else if (!strcmp ("self", p))
29745 result = PRAGMA_OACC_CLAUSE_SELF;
29746 else if (!strcmp ("seq", p))
29747 result = PRAGMA_OACC_CLAUSE_SEQ;
29748 else if (!strcmp ("shared", p))
29749 result = PRAGMA_OMP_CLAUSE_SHARED;
29750 else if (!strcmp ("simd", p))
29751 result = PRAGMA_OMP_CLAUSE_SIMD;
29752 else if (!strcmp ("simdlen", p))
29753 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
29754 break;
29755 case 't':
29756 if (!strcmp ("taskgroup", p))
29757 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
29758 else if (!strcmp ("thread_limit", p))
29759 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
29760 else if (!strcmp ("threads", p))
29761 result = PRAGMA_OMP_CLAUSE_THREADS;
29762 else if (!strcmp ("tile", p))
29763 result = PRAGMA_OACC_CLAUSE_TILE;
29764 else if (!strcmp ("to", p))
29765 result = PRAGMA_OMP_CLAUSE_TO;
29766 break;
29767 case 'u':
29768 if (!strcmp ("uniform", p))
29769 result = PRAGMA_OMP_CLAUSE_UNIFORM;
29770 else if (!strcmp ("untied", p))
29771 result = PRAGMA_OMP_CLAUSE_UNTIED;
29772 else if (!strcmp ("use_device", p))
29773 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
29774 else if (!strcmp ("use_device_ptr", p))
29775 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
29776 break;
29777 case 'v':
29778 if (!strcmp ("vector", p))
29779 result = PRAGMA_OACC_CLAUSE_VECTOR;
29780 else if (!strcmp ("vector_length", p))
29781 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
29782 else if (flag_cilkplus && !strcmp ("vectorlength", p))
29783 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
29784 break;
29785 case 'w':
29786 if (!strcmp ("wait", p))
29787 result = PRAGMA_OACC_CLAUSE_WAIT;
29788 else if (!strcmp ("worker", p))
29789 result = PRAGMA_OACC_CLAUSE_WORKER;
29790 break;
29794 if (result != PRAGMA_OMP_CLAUSE_NONE)
29795 cp_lexer_consume_token (parser->lexer);
29797 return result;
29800 /* Validate that a clause of the given type does not already exist. */
29802 static void
29803 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
29804 const char *name, location_t location)
29806 tree c;
29808 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29809 if (OMP_CLAUSE_CODE (c) == code)
29811 error_at (location, "too many %qs clauses", name);
29812 break;
29816 /* OpenMP 2.5:
29817 variable-list:
29818 identifier
29819 variable-list , identifier
29821 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
29822 colon). An opening parenthesis will have been consumed by the caller.
29824 If KIND is nonzero, create the appropriate node and install the decl
29825 in OMP_CLAUSE_DECL and add the node to the head of the list.
29827 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
29828 return the list created.
29830 COLON can be NULL if only closing parenthesis should end the list,
29831 or pointer to bool which will receive false if the list is terminated
29832 by closing parenthesis or true if the list is terminated by colon. */
29834 static tree
29835 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
29836 tree list, bool *colon)
29838 cp_token *token;
29839 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
29840 if (colon)
29842 parser->colon_corrects_to_scope_p = false;
29843 *colon = false;
29845 while (1)
29847 tree name, decl;
29849 token = cp_lexer_peek_token (parser->lexer);
29850 if (kind != 0
29851 && current_class_ptr
29852 && cp_parser_is_keyword (token, RID_THIS))
29854 decl = finish_this_expr ();
29855 if (TREE_CODE (decl) == NON_LVALUE_EXPR
29856 || CONVERT_EXPR_P (decl))
29857 decl = TREE_OPERAND (decl, 0);
29858 cp_lexer_consume_token (parser->lexer);
29860 else
29862 name = cp_parser_id_expression (parser, /*template_p=*/false,
29863 /*check_dependency_p=*/true,
29864 /*template_p=*/NULL,
29865 /*declarator_p=*/false,
29866 /*optional_p=*/false);
29867 if (name == error_mark_node)
29868 goto skip_comma;
29870 decl = cp_parser_lookup_name_simple (parser, name, token->location);
29871 if (decl == error_mark_node)
29872 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
29873 token->location);
29875 if (decl == error_mark_node)
29877 else if (kind != 0)
29879 switch (kind)
29881 case OMP_CLAUSE__CACHE_:
29882 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
29884 error_at (token->location, "expected %<[%>");
29885 decl = error_mark_node;
29886 break;
29888 /* FALLTHROUGH. */
29889 case OMP_CLAUSE_MAP:
29890 case OMP_CLAUSE_FROM:
29891 case OMP_CLAUSE_TO:
29892 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
29894 location_t loc
29895 = cp_lexer_peek_token (parser->lexer)->location;
29896 cp_id_kind idk = CP_ID_KIND_NONE;
29897 cp_lexer_consume_token (parser->lexer);
29898 decl
29899 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
29900 decl, false,
29901 &idk, loc);
29903 /* FALLTHROUGH. */
29904 case OMP_CLAUSE_DEPEND:
29905 case OMP_CLAUSE_REDUCTION:
29906 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
29908 tree low_bound = NULL_TREE, length = NULL_TREE;
29910 parser->colon_corrects_to_scope_p = false;
29911 cp_lexer_consume_token (parser->lexer);
29912 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
29913 low_bound = cp_parser_expression (parser);
29914 if (!colon)
29915 parser->colon_corrects_to_scope_p
29916 = saved_colon_corrects_to_scope_p;
29917 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
29918 length = integer_one_node;
29919 else
29921 /* Look for `:'. */
29922 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29923 goto skip_comma;
29924 if (!cp_lexer_next_token_is (parser->lexer,
29925 CPP_CLOSE_SQUARE))
29926 length = cp_parser_expression (parser);
29928 /* Look for the closing `]'. */
29929 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
29930 RT_CLOSE_SQUARE))
29931 goto skip_comma;
29933 if (kind == OMP_CLAUSE__CACHE_)
29935 if (TREE_CODE (low_bound) != INTEGER_CST
29936 && !TREE_READONLY (low_bound))
29938 error_at (token->location,
29939 "%qD is not a constant", low_bound);
29940 decl = error_mark_node;
29943 if (TREE_CODE (length) != INTEGER_CST
29944 && !TREE_READONLY (length))
29946 error_at (token->location,
29947 "%qD is not a constant", length);
29948 decl = error_mark_node;
29952 decl = tree_cons (low_bound, length, decl);
29954 break;
29955 default:
29956 break;
29959 tree u = build_omp_clause (token->location, kind);
29960 OMP_CLAUSE_DECL (u) = decl;
29961 OMP_CLAUSE_CHAIN (u) = list;
29962 list = u;
29964 else
29965 list = tree_cons (decl, NULL_TREE, list);
29967 get_comma:
29968 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29969 break;
29970 cp_lexer_consume_token (parser->lexer);
29973 if (colon)
29974 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
29976 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
29978 *colon = true;
29979 cp_parser_require (parser, CPP_COLON, RT_COLON);
29980 return list;
29983 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29985 int ending;
29987 /* Try to resync to an unnested comma. Copied from
29988 cp_parser_parenthesized_expression_list. */
29989 skip_comma:
29990 if (colon)
29991 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
29992 ending = cp_parser_skip_to_closing_parenthesis (parser,
29993 /*recovering=*/true,
29994 /*or_comma=*/true,
29995 /*consume_paren=*/true);
29996 if (ending < 0)
29997 goto get_comma;
30000 return list;
30003 /* Similarly, but expect leading and trailing parenthesis. This is a very
30004 common case for omp clauses. */
30006 static tree
30007 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
30009 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30010 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
30011 return list;
30014 /* OpenACC 2.0:
30015 copy ( variable-list )
30016 copyin ( variable-list )
30017 copyout ( variable-list )
30018 create ( variable-list )
30019 delete ( variable-list )
30020 present ( variable-list )
30021 present_or_copy ( variable-list )
30022 pcopy ( variable-list )
30023 present_or_copyin ( variable-list )
30024 pcopyin ( variable-list )
30025 present_or_copyout ( variable-list )
30026 pcopyout ( variable-list )
30027 present_or_create ( variable-list )
30028 pcreate ( variable-list ) */
30030 static tree
30031 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
30032 tree list)
30034 enum gomp_map_kind kind;
30035 switch (c_kind)
30037 case PRAGMA_OACC_CLAUSE_COPY:
30038 kind = GOMP_MAP_FORCE_TOFROM;
30039 break;
30040 case PRAGMA_OACC_CLAUSE_COPYIN:
30041 kind = GOMP_MAP_FORCE_TO;
30042 break;
30043 case PRAGMA_OACC_CLAUSE_COPYOUT:
30044 kind = GOMP_MAP_FORCE_FROM;
30045 break;
30046 case PRAGMA_OACC_CLAUSE_CREATE:
30047 kind = GOMP_MAP_FORCE_ALLOC;
30048 break;
30049 case PRAGMA_OACC_CLAUSE_DELETE:
30050 kind = GOMP_MAP_FORCE_DEALLOC;
30051 break;
30052 case PRAGMA_OACC_CLAUSE_DEVICE:
30053 kind = GOMP_MAP_FORCE_TO;
30054 break;
30055 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
30056 kind = GOMP_MAP_DEVICE_RESIDENT;
30057 break;
30058 case PRAGMA_OACC_CLAUSE_HOST:
30059 case PRAGMA_OACC_CLAUSE_SELF:
30060 kind = GOMP_MAP_FORCE_FROM;
30061 break;
30062 case PRAGMA_OACC_CLAUSE_LINK:
30063 kind = GOMP_MAP_LINK;
30064 break;
30065 case PRAGMA_OACC_CLAUSE_PRESENT:
30066 kind = GOMP_MAP_FORCE_PRESENT;
30067 break;
30068 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
30069 kind = GOMP_MAP_TOFROM;
30070 break;
30071 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
30072 kind = GOMP_MAP_TO;
30073 break;
30074 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
30075 kind = GOMP_MAP_FROM;
30076 break;
30077 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
30078 kind = GOMP_MAP_ALLOC;
30079 break;
30080 default:
30081 gcc_unreachable ();
30083 tree nl, c;
30084 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
30086 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
30087 OMP_CLAUSE_SET_MAP_KIND (c, kind);
30089 return nl;
30092 /* OpenACC 2.0:
30093 deviceptr ( variable-list ) */
30095 static tree
30096 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
30098 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30099 tree vars, t;
30101 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
30102 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
30103 variable-list must only allow for pointer variables. */
30104 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30105 for (t = vars; t; t = TREE_CHAIN (t))
30107 tree v = TREE_PURPOSE (t);
30109 /* FIXME diagnostics: Ideally we should keep individual
30110 locations for all the variables in the var list to make the
30111 following errors more precise. Perhaps
30112 c_parser_omp_var_list_parens should construct a list of
30113 locations to go along with the var list. */
30115 if (!VAR_P (v))
30116 error_at (loc, "%qD is not a variable", v);
30117 else if (TREE_TYPE (v) == error_mark_node)
30119 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
30120 error_at (loc, "%qD is not a pointer variable", v);
30122 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
30123 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
30124 OMP_CLAUSE_DECL (u) = v;
30125 OMP_CLAUSE_CHAIN (u) = list;
30126 list = u;
30129 return list;
30132 /* OpenACC 2.0:
30133 auto
30134 independent
30135 nohost
30136 seq */
30138 static tree
30139 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
30140 enum omp_clause_code code,
30141 tree list, location_t location)
30143 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
30144 tree c = build_omp_clause (location, code);
30145 OMP_CLAUSE_CHAIN (c) = list;
30146 return c;
30149 /* OpenACC:
30150 num_gangs ( expression )
30151 num_workers ( expression )
30152 vector_length ( expression ) */
30154 static tree
30155 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
30156 const char *str, tree list)
30158 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30160 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30161 return list;
30163 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
30165 if (t == error_mark_node
30166 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30168 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30169 /*or_comma=*/false,
30170 /*consume_paren=*/true);
30171 return list;
30174 check_no_duplicate_clause (list, code, str, loc);
30176 tree c = build_omp_clause (loc, code);
30177 OMP_CLAUSE_OPERAND (c, 0) = t;
30178 OMP_CLAUSE_CHAIN (c) = list;
30179 return c;
30182 /* OpenACC:
30184 gang [( gang-arg-list )]
30185 worker [( [num:] int-expr )]
30186 vector [( [length:] int-expr )]
30188 where gang-arg is one of:
30190 [num:] int-expr
30191 static: size-expr
30193 and size-expr may be:
30196 int-expr
30199 static tree
30200 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
30201 const char *str, tree list)
30203 const char *id = "num";
30204 cp_lexer *lexer = parser->lexer;
30205 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
30206 location_t loc = cp_lexer_peek_token (lexer)->location;
30208 if (kind == OMP_CLAUSE_VECTOR)
30209 id = "length";
30211 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
30213 cp_lexer_consume_token (lexer);
30217 cp_token *next = cp_lexer_peek_token (lexer);
30218 int idx = 0;
30220 /* Gang static argument. */
30221 if (kind == OMP_CLAUSE_GANG
30222 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
30224 cp_lexer_consume_token (lexer);
30226 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30227 goto cleanup_error;
30229 idx = 1;
30230 if (ops[idx] != NULL)
30232 cp_parser_error (parser, "too many %<static%> arguments");
30233 goto cleanup_error;
30236 /* Check for the '*' argument. */
30237 if (cp_lexer_next_token_is (lexer, CPP_MULT)
30238 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
30239 || cp_lexer_nth_token_is (parser->lexer, 2,
30240 CPP_CLOSE_PAREN)))
30242 cp_lexer_consume_token (lexer);
30243 ops[idx] = integer_minus_one_node;
30245 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
30247 cp_lexer_consume_token (lexer);
30248 continue;
30250 else break;
30253 /* Worker num: argument and vector length: arguments. */
30254 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
30255 && strcmp (id, IDENTIFIER_POINTER (next->u.value)) == 0
30256 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
30258 cp_lexer_consume_token (lexer); /* id */
30259 cp_lexer_consume_token (lexer); /* ':' */
30262 /* Now collect the actual argument. */
30263 if (ops[idx] != NULL_TREE)
30265 cp_parser_error (parser, "unexpected argument");
30266 goto cleanup_error;
30269 tree expr = cp_parser_assignment_expression (parser, NULL, false,
30270 false);
30271 if (expr == error_mark_node)
30272 goto cleanup_error;
30274 mark_exp_read (expr);
30275 ops[idx] = expr;
30277 if (kind == OMP_CLAUSE_GANG
30278 && cp_lexer_next_token_is (lexer, CPP_COMMA))
30280 cp_lexer_consume_token (lexer);
30281 continue;
30283 break;
30285 while (1);
30287 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30288 goto cleanup_error;
30291 check_no_duplicate_clause (list, kind, str, loc);
30293 c = build_omp_clause (loc, kind);
30295 if (ops[1])
30296 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
30298 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
30299 OMP_CLAUSE_CHAIN (c) = list;
30301 return c;
30303 cleanup_error:
30304 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
30305 return list;
30308 /* OpenACC 2.0:
30309 tile ( size-expr-list ) */
30311 static tree
30312 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
30314 tree c, expr = error_mark_node;
30315 tree tile = NULL_TREE;
30317 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
30319 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30320 return list;
30324 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
30325 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
30326 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
30328 cp_lexer_consume_token (parser->lexer);
30329 expr = integer_minus_one_node;
30331 else
30332 expr = cp_parser_assignment_expression (parser, NULL, false, false);
30334 if (expr == error_mark_node)
30335 return list;
30337 tile = tree_cons (NULL_TREE, expr, tile);
30339 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30340 cp_lexer_consume_token (parser->lexer);
30342 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
30344 /* Consume the trailing ')'. */
30345 cp_lexer_consume_token (parser->lexer);
30347 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
30348 tile = nreverse (tile);
30349 OMP_CLAUSE_TILE_LIST (c) = tile;
30350 OMP_CLAUSE_CHAIN (c) = list;
30351 return c;
30354 /* OpenACC 2.0
30355 Parse wait clause or directive parameters. */
30357 static tree
30358 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
30360 vec<tree, va_gc> *args;
30361 tree t, args_tree;
30363 args = cp_parser_parenthesized_expression_list (parser, non_attr,
30364 /*cast_p=*/false,
30365 /*allow_expansion_p=*/true,
30366 /*non_constant_p=*/NULL);
30368 if (args == NULL || args->length () == 0)
30370 cp_parser_error (parser, "expected integer expression before ')'");
30371 if (args != NULL)
30372 release_tree_vector (args);
30373 return list;
30376 args_tree = build_tree_list_vec (args);
30378 release_tree_vector (args);
30380 for (t = args_tree; t; t = TREE_CHAIN (t))
30382 tree targ = TREE_VALUE (t);
30384 if (targ != error_mark_node)
30386 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
30387 error ("%<wait%> expression must be integral");
30388 else
30390 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
30392 mark_rvalue_use (targ);
30393 OMP_CLAUSE_DECL (c) = targ;
30394 OMP_CLAUSE_CHAIN (c) = list;
30395 list = c;
30400 return list;
30403 /* OpenACC:
30404 wait ( int-expr-list ) */
30406 static tree
30407 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
30409 location_t location = cp_lexer_peek_token (parser->lexer)->location;
30411 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
30412 return list;
30414 list = cp_parser_oacc_wait_list (parser, location, list);
30416 return list;
30419 /* OpenMP 3.0:
30420 collapse ( constant-expression ) */
30422 static tree
30423 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
30425 tree c, num;
30426 location_t loc;
30427 HOST_WIDE_INT n;
30429 loc = cp_lexer_peek_token (parser->lexer)->location;
30430 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30431 return list;
30433 num = cp_parser_constant_expression (parser);
30435 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30436 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30437 /*or_comma=*/false,
30438 /*consume_paren=*/true);
30440 if (num == error_mark_node)
30441 return list;
30442 num = fold_non_dependent_expr (num);
30443 if (!tree_fits_shwi_p (num)
30444 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
30445 || (n = tree_to_shwi (num)) <= 0
30446 || (int) n != n)
30448 error_at (loc, "collapse argument needs positive constant integer expression");
30449 return list;
30452 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
30453 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
30454 OMP_CLAUSE_CHAIN (c) = list;
30455 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
30457 return c;
30460 /* OpenMP 2.5:
30461 default ( shared | none )
30463 OpenACC 2.0
30464 default (none) */
30466 static tree
30467 cp_parser_omp_clause_default (cp_parser *parser, tree list,
30468 location_t location, bool is_oacc)
30470 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
30471 tree c;
30473 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30474 return list;
30475 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30477 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30478 const char *p = IDENTIFIER_POINTER (id);
30480 switch (p[0])
30482 case 'n':
30483 if (strcmp ("none", p) != 0)
30484 goto invalid_kind;
30485 kind = OMP_CLAUSE_DEFAULT_NONE;
30486 break;
30488 case 's':
30489 if (strcmp ("shared", p) != 0 || is_oacc)
30490 goto invalid_kind;
30491 kind = OMP_CLAUSE_DEFAULT_SHARED;
30492 break;
30494 default:
30495 goto invalid_kind;
30498 cp_lexer_consume_token (parser->lexer);
30500 else
30502 invalid_kind:
30503 if (is_oacc)
30504 cp_parser_error (parser, "expected %<none%>");
30505 else
30506 cp_parser_error (parser, "expected %<none%> or %<shared%>");
30509 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30510 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30511 /*or_comma=*/false,
30512 /*consume_paren=*/true);
30514 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
30515 return list;
30517 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
30518 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
30519 OMP_CLAUSE_CHAIN (c) = list;
30520 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
30522 return c;
30525 /* OpenMP 3.1:
30526 final ( expression ) */
30528 static tree
30529 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
30531 tree t, c;
30533 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30534 return list;
30536 t = cp_parser_condition (parser);
30538 if (t == error_mark_node
30539 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30540 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30541 /*or_comma=*/false,
30542 /*consume_paren=*/true);
30544 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
30546 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
30547 OMP_CLAUSE_FINAL_EXPR (c) = t;
30548 OMP_CLAUSE_CHAIN (c) = list;
30550 return c;
30553 /* OpenMP 2.5:
30554 if ( expression )
30556 OpenMP 4.5:
30557 if ( directive-name-modifier : expression )
30559 directive-name-modifier:
30560 parallel | task | taskloop | target data | target | target update
30561 | target enter data | target exit data */
30563 static tree
30564 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
30565 bool is_omp)
30567 tree t, c;
30568 enum tree_code if_modifier = ERROR_MARK;
30570 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30571 return list;
30573 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30575 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30576 const char *p = IDENTIFIER_POINTER (id);
30577 int n = 2;
30579 if (strcmp ("parallel", p) == 0)
30580 if_modifier = OMP_PARALLEL;
30581 else if (strcmp ("task", p) == 0)
30582 if_modifier = OMP_TASK;
30583 else if (strcmp ("taskloop", p) == 0)
30584 if_modifier = OMP_TASKLOOP;
30585 else if (strcmp ("target", p) == 0)
30587 if_modifier = OMP_TARGET;
30588 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
30590 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
30591 p = IDENTIFIER_POINTER (id);
30592 if (strcmp ("data", p) == 0)
30593 if_modifier = OMP_TARGET_DATA;
30594 else if (strcmp ("update", p) == 0)
30595 if_modifier = OMP_TARGET_UPDATE;
30596 else if (strcmp ("enter", p) == 0)
30597 if_modifier = OMP_TARGET_ENTER_DATA;
30598 else if (strcmp ("exit", p) == 0)
30599 if_modifier = OMP_TARGET_EXIT_DATA;
30600 if (if_modifier != OMP_TARGET)
30601 n = 3;
30602 else
30604 location_t loc
30605 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
30606 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
30607 "or %<exit%>");
30608 if_modifier = ERROR_MARK;
30610 if (if_modifier == OMP_TARGET_ENTER_DATA
30611 || if_modifier == OMP_TARGET_EXIT_DATA)
30613 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
30615 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
30616 p = IDENTIFIER_POINTER (id);
30617 if (strcmp ("data", p) == 0)
30618 n = 4;
30620 if (n != 4)
30622 location_t loc
30623 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
30624 error_at (loc, "expected %<data%>");
30625 if_modifier = ERROR_MARK;
30630 if (if_modifier != ERROR_MARK)
30632 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
30634 while (n-- > 0)
30635 cp_lexer_consume_token (parser->lexer);
30637 else
30639 if (n > 2)
30641 location_t loc
30642 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
30643 error_at (loc, "expected %<:%>");
30645 if_modifier = ERROR_MARK;
30650 t = cp_parser_condition (parser);
30652 if (t == error_mark_node
30653 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30654 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30655 /*or_comma=*/false,
30656 /*consume_paren=*/true);
30658 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
30659 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
30661 if (if_modifier != ERROR_MARK
30662 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
30664 const char *p = NULL;
30665 switch (if_modifier)
30667 case OMP_PARALLEL: p = "parallel"; break;
30668 case OMP_TASK: p = "task"; break;
30669 case OMP_TASKLOOP: p = "taskloop"; break;
30670 case OMP_TARGET_DATA: p = "target data"; break;
30671 case OMP_TARGET: p = "target"; break;
30672 case OMP_TARGET_UPDATE: p = "target update"; break;
30673 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
30674 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
30675 default: gcc_unreachable ();
30677 error_at (location, "too many %<if%> clauses with %qs modifier",
30679 return list;
30681 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
30683 if (!is_omp)
30684 error_at (location, "too many %<if%> clauses");
30685 else
30686 error_at (location, "too many %<if%> clauses without modifier");
30687 return list;
30689 else if (if_modifier == ERROR_MARK
30690 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
30692 error_at (location, "if any %<if%> clause has modifier, then all "
30693 "%<if%> clauses have to use modifier");
30694 return list;
30698 c = build_omp_clause (location, OMP_CLAUSE_IF);
30699 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
30700 OMP_CLAUSE_IF_EXPR (c) = t;
30701 OMP_CLAUSE_CHAIN (c) = list;
30703 return c;
30706 /* OpenMP 3.1:
30707 mergeable */
30709 static tree
30710 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
30711 tree list, location_t location)
30713 tree c;
30715 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
30716 location);
30718 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
30719 OMP_CLAUSE_CHAIN (c) = list;
30720 return c;
30723 /* OpenMP 2.5:
30724 nowait */
30726 static tree
30727 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
30728 tree list, location_t location)
30730 tree c;
30732 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
30734 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
30735 OMP_CLAUSE_CHAIN (c) = list;
30736 return c;
30739 /* OpenMP 2.5:
30740 num_threads ( expression ) */
30742 static tree
30743 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
30744 location_t location)
30746 tree t, c;
30748 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30749 return list;
30751 t = cp_parser_expression (parser);
30753 if (t == error_mark_node
30754 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30755 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30756 /*or_comma=*/false,
30757 /*consume_paren=*/true);
30759 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
30760 "num_threads", location);
30762 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
30763 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
30764 OMP_CLAUSE_CHAIN (c) = list;
30766 return c;
30769 /* OpenMP 4.5:
30770 num_tasks ( expression ) */
30772 static tree
30773 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
30774 location_t location)
30776 tree t, c;
30778 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30779 return list;
30781 t = cp_parser_expression (parser);
30783 if (t == error_mark_node
30784 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30785 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30786 /*or_comma=*/false,
30787 /*consume_paren=*/true);
30789 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
30790 "num_tasks", location);
30792 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
30793 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
30794 OMP_CLAUSE_CHAIN (c) = list;
30796 return c;
30799 /* OpenMP 4.5:
30800 grainsize ( expression ) */
30802 static tree
30803 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
30804 location_t location)
30806 tree t, c;
30808 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30809 return list;
30811 t = cp_parser_expression (parser);
30813 if (t == error_mark_node
30814 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30815 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30816 /*or_comma=*/false,
30817 /*consume_paren=*/true);
30819 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
30820 "grainsize", location);
30822 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
30823 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
30824 OMP_CLAUSE_CHAIN (c) = list;
30826 return c;
30829 /* OpenMP 4.5:
30830 priority ( expression ) */
30832 static tree
30833 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
30834 location_t location)
30836 tree t, c;
30838 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30839 return list;
30841 t = cp_parser_expression (parser);
30843 if (t == error_mark_node
30844 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30845 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30846 /*or_comma=*/false,
30847 /*consume_paren=*/true);
30849 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
30850 "priority", location);
30852 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
30853 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
30854 OMP_CLAUSE_CHAIN (c) = list;
30856 return c;
30859 /* OpenMP 4.5:
30860 hint ( expression ) */
30862 static tree
30863 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
30864 location_t location)
30866 tree t, c;
30868 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30869 return list;
30871 t = cp_parser_expression (parser);
30873 if (t == error_mark_node
30874 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30875 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30876 /*or_comma=*/false,
30877 /*consume_paren=*/true);
30879 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
30881 c = build_omp_clause (location, OMP_CLAUSE_HINT);
30882 OMP_CLAUSE_HINT_EXPR (c) = t;
30883 OMP_CLAUSE_CHAIN (c) = list;
30885 return c;
30888 /* OpenMP 4.5:
30889 defaultmap ( tofrom : scalar ) */
30891 static tree
30892 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
30893 location_t location)
30895 tree c, id;
30896 const char *p;
30898 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30899 return list;
30901 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30903 cp_parser_error (parser, "expected %<tofrom%>");
30904 goto out_err;
30906 id = cp_lexer_peek_token (parser->lexer)->u.value;
30907 p = IDENTIFIER_POINTER (id);
30908 if (strcmp (p, "tofrom") != 0)
30910 cp_parser_error (parser, "expected %<tofrom%>");
30911 goto out_err;
30913 cp_lexer_consume_token (parser->lexer);
30914 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30915 goto out_err;
30917 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30919 cp_parser_error (parser, "expected %<scalar%>");
30920 goto out_err;
30922 id = cp_lexer_peek_token (parser->lexer)->u.value;
30923 p = IDENTIFIER_POINTER (id);
30924 if (strcmp (p, "scalar") != 0)
30926 cp_parser_error (parser, "expected %<scalar%>");
30927 goto out_err;
30929 cp_lexer_consume_token (parser->lexer);
30930 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30931 goto out_err;
30933 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
30934 location);
30936 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
30937 OMP_CLAUSE_CHAIN (c) = list;
30938 return c;
30940 out_err:
30941 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30942 /*or_comma=*/false,
30943 /*consume_paren=*/true);
30944 return list;
30947 /* OpenMP 2.5:
30948 ordered
30950 OpenMP 4.5:
30951 ordered ( constant-expression ) */
30953 static tree
30954 cp_parser_omp_clause_ordered (cp_parser *parser,
30955 tree list, location_t location)
30957 tree c, num = NULL_TREE;
30958 HOST_WIDE_INT n;
30960 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
30961 "ordered", location);
30963 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30965 cp_lexer_consume_token (parser->lexer);
30967 num = cp_parser_constant_expression (parser);
30969 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30970 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30971 /*or_comma=*/false,
30972 /*consume_paren=*/true);
30974 if (num == error_mark_node)
30975 return list;
30976 num = fold_non_dependent_expr (num);
30977 if (!tree_fits_shwi_p (num)
30978 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
30979 || (n = tree_to_shwi (num)) <= 0
30980 || (int) n != n)
30982 error_at (location,
30983 "ordered argument needs positive constant integer "
30984 "expression");
30985 return list;
30989 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
30990 OMP_CLAUSE_ORDERED_EXPR (c) = num;
30991 OMP_CLAUSE_CHAIN (c) = list;
30992 return c;
30995 /* OpenMP 2.5:
30996 reduction ( reduction-operator : variable-list )
30998 reduction-operator:
30999 One of: + * - & ^ | && ||
31001 OpenMP 3.1:
31003 reduction-operator:
31004 One of: + * - & ^ | && || min max
31006 OpenMP 4.0:
31008 reduction-operator:
31009 One of: + * - & ^ | && ||
31010 id-expression */
31012 static tree
31013 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
31015 enum tree_code code = ERROR_MARK;
31016 tree nlist, c, id = NULL_TREE;
31018 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31019 return list;
31021 switch (cp_lexer_peek_token (parser->lexer)->type)
31023 case CPP_PLUS: code = PLUS_EXPR; break;
31024 case CPP_MULT: code = MULT_EXPR; break;
31025 case CPP_MINUS: code = MINUS_EXPR; break;
31026 case CPP_AND: code = BIT_AND_EXPR; break;
31027 case CPP_XOR: code = BIT_XOR_EXPR; break;
31028 case CPP_OR: code = BIT_IOR_EXPR; break;
31029 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
31030 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
31031 default: break;
31034 if (code != ERROR_MARK)
31035 cp_lexer_consume_token (parser->lexer);
31036 else
31038 bool saved_colon_corrects_to_scope_p;
31039 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31040 parser->colon_corrects_to_scope_p = false;
31041 id = cp_parser_id_expression (parser, /*template_p=*/false,
31042 /*check_dependency_p=*/true,
31043 /*template_p=*/NULL,
31044 /*declarator_p=*/false,
31045 /*optional_p=*/false);
31046 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31047 if (identifier_p (id))
31049 const char *p = IDENTIFIER_POINTER (id);
31051 if (strcmp (p, "min") == 0)
31052 code = MIN_EXPR;
31053 else if (strcmp (p, "max") == 0)
31054 code = MAX_EXPR;
31055 else if (id == ansi_opname (PLUS_EXPR))
31056 code = PLUS_EXPR;
31057 else if (id == ansi_opname (MULT_EXPR))
31058 code = MULT_EXPR;
31059 else if (id == ansi_opname (MINUS_EXPR))
31060 code = MINUS_EXPR;
31061 else if (id == ansi_opname (BIT_AND_EXPR))
31062 code = BIT_AND_EXPR;
31063 else if (id == ansi_opname (BIT_IOR_EXPR))
31064 code = BIT_IOR_EXPR;
31065 else if (id == ansi_opname (BIT_XOR_EXPR))
31066 code = BIT_XOR_EXPR;
31067 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
31068 code = TRUTH_ANDIF_EXPR;
31069 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
31070 code = TRUTH_ORIF_EXPR;
31071 id = omp_reduction_id (code, id, NULL_TREE);
31072 tree scope = parser->scope;
31073 if (scope)
31074 id = build_qualified_name (NULL_TREE, scope, id, false);
31075 parser->scope = NULL_TREE;
31076 parser->qualifying_scope = NULL_TREE;
31077 parser->object_scope = NULL_TREE;
31079 else
31081 error ("invalid reduction-identifier");
31082 resync_fail:
31083 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31084 /*or_comma=*/false,
31085 /*consume_paren=*/true);
31086 return list;
31090 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31091 goto resync_fail;
31093 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
31094 NULL);
31095 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31097 OMP_CLAUSE_REDUCTION_CODE (c) = code;
31098 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
31101 return nlist;
31104 /* OpenMP 2.5:
31105 schedule ( schedule-kind )
31106 schedule ( schedule-kind , expression )
31108 schedule-kind:
31109 static | dynamic | guided | runtime | auto
31111 OpenMP 4.5:
31112 schedule ( schedule-modifier : schedule-kind )
31113 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
31115 schedule-modifier:
31116 simd
31117 monotonic
31118 nonmonotonic */
31120 static tree
31121 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
31123 tree c, t;
31124 int modifiers = 0, nmodifiers = 0;
31126 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31127 return list;
31129 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
31131 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31133 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31134 const char *p = IDENTIFIER_POINTER (id);
31135 if (strcmp ("simd", p) == 0)
31136 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
31137 else if (strcmp ("monotonic", p) == 0)
31138 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
31139 else if (strcmp ("nonmonotonic", p) == 0)
31140 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
31141 else
31142 break;
31143 cp_lexer_consume_token (parser->lexer);
31144 if (nmodifiers++ == 0
31145 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31146 cp_lexer_consume_token (parser->lexer);
31147 else
31149 cp_parser_require (parser, CPP_COLON, RT_COLON);
31150 break;
31154 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31156 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31157 const char *p = IDENTIFIER_POINTER (id);
31159 switch (p[0])
31161 case 'd':
31162 if (strcmp ("dynamic", p) != 0)
31163 goto invalid_kind;
31164 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
31165 break;
31167 case 'g':
31168 if (strcmp ("guided", p) != 0)
31169 goto invalid_kind;
31170 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
31171 break;
31173 case 'r':
31174 if (strcmp ("runtime", p) != 0)
31175 goto invalid_kind;
31176 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
31177 break;
31179 default:
31180 goto invalid_kind;
31183 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
31184 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
31185 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31186 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
31187 else
31188 goto invalid_kind;
31189 cp_lexer_consume_token (parser->lexer);
31191 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
31192 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
31193 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
31194 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
31196 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
31197 "specified");
31198 modifiers = 0;
31201 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31203 cp_token *token;
31204 cp_lexer_consume_token (parser->lexer);
31206 token = cp_lexer_peek_token (parser->lexer);
31207 t = cp_parser_assignment_expression (parser);
31209 if (t == error_mark_node)
31210 goto resync_fail;
31211 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
31212 error_at (token->location, "schedule %<runtime%> does not take "
31213 "a %<chunk_size%> parameter");
31214 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
31215 error_at (token->location, "schedule %<auto%> does not take "
31216 "a %<chunk_size%> parameter");
31217 else
31218 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
31220 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31221 goto resync_fail;
31223 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
31224 goto resync_fail;
31226 OMP_CLAUSE_SCHEDULE_KIND (c)
31227 = (enum omp_clause_schedule_kind)
31228 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
31230 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
31231 OMP_CLAUSE_CHAIN (c) = list;
31232 return c;
31234 invalid_kind:
31235 cp_parser_error (parser, "invalid schedule kind");
31236 resync_fail:
31237 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31238 /*or_comma=*/false,
31239 /*consume_paren=*/true);
31240 return list;
31243 /* OpenMP 3.0:
31244 untied */
31246 static tree
31247 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
31248 tree list, location_t location)
31250 tree c;
31252 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
31254 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
31255 OMP_CLAUSE_CHAIN (c) = list;
31256 return c;
31259 /* OpenMP 4.0:
31260 inbranch
31261 notinbranch */
31263 static tree
31264 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
31265 tree list, location_t location)
31267 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31268 tree c = build_omp_clause (location, code);
31269 OMP_CLAUSE_CHAIN (c) = list;
31270 return c;
31273 /* OpenMP 4.0:
31274 parallel
31276 sections
31277 taskgroup */
31279 static tree
31280 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
31281 enum omp_clause_code code,
31282 tree list, location_t location)
31284 tree c = build_omp_clause (location, code);
31285 OMP_CLAUSE_CHAIN (c) = list;
31286 return c;
31289 /* OpenMP 4.5:
31290 nogroup */
31292 static tree
31293 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
31294 tree list, location_t location)
31296 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
31297 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
31298 OMP_CLAUSE_CHAIN (c) = list;
31299 return c;
31302 /* OpenMP 4.5:
31303 simd
31304 threads */
31306 static tree
31307 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
31308 enum omp_clause_code code,
31309 tree list, location_t location)
31311 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31312 tree c = build_omp_clause (location, code);
31313 OMP_CLAUSE_CHAIN (c) = list;
31314 return c;
31317 /* OpenMP 4.0:
31318 num_teams ( expression ) */
31320 static tree
31321 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
31322 location_t location)
31324 tree t, c;
31326 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31327 return list;
31329 t = cp_parser_expression (parser);
31331 if (t == error_mark_node
31332 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31333 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31334 /*or_comma=*/false,
31335 /*consume_paren=*/true);
31337 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
31338 "num_teams", location);
31340 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
31341 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
31342 OMP_CLAUSE_CHAIN (c) = list;
31344 return c;
31347 /* OpenMP 4.0:
31348 thread_limit ( expression ) */
31350 static tree
31351 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
31352 location_t location)
31354 tree t, c;
31356 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31357 return list;
31359 t = cp_parser_expression (parser);
31361 if (t == error_mark_node
31362 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31363 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31364 /*or_comma=*/false,
31365 /*consume_paren=*/true);
31367 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
31368 "thread_limit", location);
31370 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
31371 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
31372 OMP_CLAUSE_CHAIN (c) = list;
31374 return c;
31377 /* OpenMP 4.0:
31378 aligned ( variable-list )
31379 aligned ( variable-list : constant-expression ) */
31381 static tree
31382 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
31384 tree nlist, c, alignment = NULL_TREE;
31385 bool colon;
31387 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31388 return list;
31390 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
31391 &colon);
31393 if (colon)
31395 alignment = cp_parser_constant_expression (parser);
31397 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31398 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31399 /*or_comma=*/false,
31400 /*consume_paren=*/true);
31402 if (alignment == error_mark_node)
31403 alignment = NULL_TREE;
31406 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31407 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
31409 return nlist;
31412 /* OpenMP 4.0:
31413 linear ( variable-list )
31414 linear ( variable-list : expression )
31416 OpenMP 4.5:
31417 linear ( modifier ( variable-list ) )
31418 linear ( modifier ( variable-list ) : expression ) */
31420 static tree
31421 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
31422 bool is_cilk_simd_fn, bool declare_simd)
31424 tree nlist, c, step = integer_one_node;
31425 bool colon;
31426 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
31428 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31429 return list;
31431 if (!is_cilk_simd_fn
31432 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31434 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31435 const char *p = IDENTIFIER_POINTER (id);
31437 if (strcmp ("ref", p) == 0)
31438 kind = OMP_CLAUSE_LINEAR_REF;
31439 else if (strcmp ("val", p) == 0)
31440 kind = OMP_CLAUSE_LINEAR_VAL;
31441 else if (strcmp ("uval", p) == 0)
31442 kind = OMP_CLAUSE_LINEAR_UVAL;
31443 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
31444 cp_lexer_consume_token (parser->lexer);
31445 else
31446 kind = OMP_CLAUSE_LINEAR_DEFAULT;
31449 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
31450 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
31451 &colon);
31452 else
31454 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
31455 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
31456 if (colon)
31457 cp_parser_require (parser, CPP_COLON, RT_COLON);
31458 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31459 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31460 /*or_comma=*/false,
31461 /*consume_paren=*/true);
31464 if (colon)
31466 step = NULL_TREE;
31467 if (declare_simd
31468 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
31469 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
31471 cp_token *token = cp_lexer_peek_token (parser->lexer);
31472 cp_parser_parse_tentatively (parser);
31473 step = cp_parser_id_expression (parser, /*template_p=*/false,
31474 /*check_dependency_p=*/true,
31475 /*template_p=*/NULL,
31476 /*declarator_p=*/false,
31477 /*optional_p=*/false);
31478 if (step != error_mark_node)
31479 step = cp_parser_lookup_name_simple (parser, step, token->location);
31480 if (step == error_mark_node)
31482 step = NULL_TREE;
31483 cp_parser_abort_tentative_parse (parser);
31485 else if (!cp_parser_parse_definitely (parser))
31486 step = NULL_TREE;
31488 if (!step)
31489 step = cp_parser_expression (parser);
31491 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
31493 sorry ("using parameters for %<linear%> step is not supported yet");
31494 step = integer_one_node;
31496 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31497 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31498 /*or_comma=*/false,
31499 /*consume_paren=*/true);
31501 if (step == error_mark_node)
31502 return list;
31505 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31507 OMP_CLAUSE_LINEAR_STEP (c) = step;
31508 OMP_CLAUSE_LINEAR_KIND (c) = kind;
31511 return nlist;
31514 /* OpenMP 4.0:
31515 safelen ( constant-expression ) */
31517 static tree
31518 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
31519 location_t location)
31521 tree t, c;
31523 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31524 return list;
31526 t = cp_parser_constant_expression (parser);
31528 if (t == error_mark_node
31529 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31530 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31531 /*or_comma=*/false,
31532 /*consume_paren=*/true);
31534 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
31536 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
31537 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
31538 OMP_CLAUSE_CHAIN (c) = list;
31540 return c;
31543 /* OpenMP 4.0:
31544 simdlen ( constant-expression ) */
31546 static tree
31547 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
31548 location_t location)
31550 tree t, c;
31552 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31553 return list;
31555 t = cp_parser_constant_expression (parser);
31557 if (t == error_mark_node
31558 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31559 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31560 /*or_comma=*/false,
31561 /*consume_paren=*/true);
31563 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
31565 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
31566 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
31567 OMP_CLAUSE_CHAIN (c) = list;
31569 return c;
31572 /* OpenMP 4.5:
31573 vec:
31574 identifier [+/- integer]
31575 vec , identifier [+/- integer]
31578 static tree
31579 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
31580 tree list)
31582 tree vec = NULL;
31584 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31586 cp_parser_error (parser, "expected identifier");
31587 return list;
31590 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31592 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
31593 tree t, identifier = cp_parser_identifier (parser);
31594 tree addend = NULL;
31596 if (identifier == error_mark_node)
31597 t = error_mark_node;
31598 else
31600 t = cp_parser_lookup_name_simple
31601 (parser, identifier,
31602 cp_lexer_peek_token (parser->lexer)->location);
31603 if (t == error_mark_node)
31604 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
31605 id_loc);
31608 bool neg = false;
31609 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
31610 neg = true;
31611 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
31613 addend = integer_zero_node;
31614 goto add_to_vector;
31616 cp_lexer_consume_token (parser->lexer);
31618 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
31620 cp_parser_error (parser, "expected integer");
31621 return list;
31624 addend = cp_lexer_peek_token (parser->lexer)->u.value;
31625 if (TREE_CODE (addend) != INTEGER_CST)
31627 cp_parser_error (parser, "expected integer");
31628 return list;
31630 cp_lexer_consume_token (parser->lexer);
31632 add_to_vector:
31633 if (t != error_mark_node)
31635 vec = tree_cons (addend, t, vec);
31636 if (neg)
31637 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
31640 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31641 break;
31643 cp_lexer_consume_token (parser->lexer);
31646 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
31648 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
31649 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
31650 OMP_CLAUSE_DECL (u) = nreverse (vec);
31651 OMP_CLAUSE_CHAIN (u) = list;
31652 return u;
31654 return list;
31657 /* OpenMP 4.0:
31658 depend ( depend-kind : variable-list )
31660 depend-kind:
31661 in | out | inout
31663 OpenMP 4.5:
31664 depend ( source )
31666 depend ( sink : vec ) */
31668 static tree
31669 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
31671 tree nlist, c;
31672 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
31674 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31675 return list;
31677 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31679 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31680 const char *p = IDENTIFIER_POINTER (id);
31682 if (strcmp ("in", p) == 0)
31683 kind = OMP_CLAUSE_DEPEND_IN;
31684 else if (strcmp ("inout", p) == 0)
31685 kind = OMP_CLAUSE_DEPEND_INOUT;
31686 else if (strcmp ("out", p) == 0)
31687 kind = OMP_CLAUSE_DEPEND_OUT;
31688 else if (strcmp ("source", p) == 0)
31689 kind = OMP_CLAUSE_DEPEND_SOURCE;
31690 else if (strcmp ("sink", p) == 0)
31691 kind = OMP_CLAUSE_DEPEND_SINK;
31692 else
31693 goto invalid_kind;
31695 else
31696 goto invalid_kind;
31698 cp_lexer_consume_token (parser->lexer);
31700 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
31702 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
31703 OMP_CLAUSE_DEPEND_KIND (c) = kind;
31704 OMP_CLAUSE_DECL (c) = NULL_TREE;
31705 OMP_CLAUSE_CHAIN (c) = list;
31706 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31707 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31708 /*or_comma=*/false,
31709 /*consume_paren=*/true);
31710 return c;
31713 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31714 goto resync_fail;
31716 if (kind == OMP_CLAUSE_DEPEND_SINK)
31717 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
31718 else
31720 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
31721 list, NULL);
31723 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31724 OMP_CLAUSE_DEPEND_KIND (c) = kind;
31726 return nlist;
31728 invalid_kind:
31729 cp_parser_error (parser, "invalid depend kind");
31730 resync_fail:
31731 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31732 /*or_comma=*/false,
31733 /*consume_paren=*/true);
31734 return list;
31737 /* OpenMP 4.0:
31738 map ( map-kind : variable-list )
31739 map ( variable-list )
31741 map-kind:
31742 alloc | to | from | tofrom
31744 OpenMP 4.5:
31745 map-kind:
31746 alloc | to | from | tofrom | release | delete
31748 map ( always [,] map-kind: variable-list ) */
31750 static tree
31751 cp_parser_omp_clause_map (cp_parser *parser, tree list)
31753 tree nlist, c;
31754 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
31755 bool always = false;
31757 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31758 return list;
31760 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31762 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31763 const char *p = IDENTIFIER_POINTER (id);
31765 if (strcmp ("always", p) == 0)
31767 int nth = 2;
31768 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
31769 nth++;
31770 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
31771 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
31772 == RID_DELETE))
31773 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
31774 == CPP_COLON))
31776 always = true;
31777 cp_lexer_consume_token (parser->lexer);
31778 if (nth == 3)
31779 cp_lexer_consume_token (parser->lexer);
31784 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
31785 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
31787 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31788 const char *p = IDENTIFIER_POINTER (id);
31790 if (strcmp ("alloc", p) == 0)
31791 kind = GOMP_MAP_ALLOC;
31792 else if (strcmp ("to", p) == 0)
31793 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
31794 else if (strcmp ("from", p) == 0)
31795 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
31796 else if (strcmp ("tofrom", p) == 0)
31797 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
31798 else if (strcmp ("release", p) == 0)
31799 kind = GOMP_MAP_RELEASE;
31800 else
31802 cp_parser_error (parser, "invalid map kind");
31803 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31804 /*or_comma=*/false,
31805 /*consume_paren=*/true);
31806 return list;
31808 cp_lexer_consume_token (parser->lexer);
31809 cp_lexer_consume_token (parser->lexer);
31811 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
31812 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
31814 kind = GOMP_MAP_DELETE;
31815 cp_lexer_consume_token (parser->lexer);
31816 cp_lexer_consume_token (parser->lexer);
31819 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
31820 NULL);
31822 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31823 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31825 return nlist;
31828 /* OpenMP 4.0:
31829 device ( expression ) */
31831 static tree
31832 cp_parser_omp_clause_device (cp_parser *parser, tree list,
31833 location_t location)
31835 tree t, c;
31837 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31838 return list;
31840 t = cp_parser_expression (parser);
31842 if (t == error_mark_node
31843 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31844 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31845 /*or_comma=*/false,
31846 /*consume_paren=*/true);
31848 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
31849 "device", location);
31851 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
31852 OMP_CLAUSE_DEVICE_ID (c) = t;
31853 OMP_CLAUSE_CHAIN (c) = list;
31855 return c;
31858 /* OpenMP 4.0:
31859 dist_schedule ( static )
31860 dist_schedule ( static , expression ) */
31862 static tree
31863 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
31864 location_t location)
31866 tree c, t;
31868 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31869 return list;
31871 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
31873 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
31874 goto invalid_kind;
31875 cp_lexer_consume_token (parser->lexer);
31877 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31879 cp_lexer_consume_token (parser->lexer);
31881 t = cp_parser_assignment_expression (parser);
31883 if (t == error_mark_node)
31884 goto resync_fail;
31885 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
31887 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31888 goto resync_fail;
31890 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
31891 goto resync_fail;
31893 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
31894 location);
31895 OMP_CLAUSE_CHAIN (c) = list;
31896 return c;
31898 invalid_kind:
31899 cp_parser_error (parser, "invalid dist_schedule kind");
31900 resync_fail:
31901 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31902 /*or_comma=*/false,
31903 /*consume_paren=*/true);
31904 return list;
31907 /* OpenMP 4.0:
31908 proc_bind ( proc-bind-kind )
31910 proc-bind-kind:
31911 master | close | spread */
31913 static tree
31914 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
31915 location_t location)
31917 tree c;
31918 enum omp_clause_proc_bind_kind kind;
31920 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31921 return list;
31923 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31925 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31926 const char *p = IDENTIFIER_POINTER (id);
31928 if (strcmp ("master", p) == 0)
31929 kind = OMP_CLAUSE_PROC_BIND_MASTER;
31930 else if (strcmp ("close", p) == 0)
31931 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
31932 else if (strcmp ("spread", p) == 0)
31933 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
31934 else
31935 goto invalid_kind;
31937 else
31938 goto invalid_kind;
31940 cp_lexer_consume_token (parser->lexer);
31941 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
31942 goto resync_fail;
31944 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
31945 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
31946 location);
31947 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
31948 OMP_CLAUSE_CHAIN (c) = list;
31949 return c;
31951 invalid_kind:
31952 cp_parser_error (parser, "invalid depend kind");
31953 resync_fail:
31954 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31955 /*or_comma=*/false,
31956 /*consume_paren=*/true);
31957 return list;
31960 /* OpenACC:
31961 async [( int-expr )] */
31963 static tree
31964 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
31966 tree c, t;
31967 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31969 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
31971 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31973 cp_lexer_consume_token (parser->lexer);
31975 t = cp_parser_expression (parser);
31976 if (t == error_mark_node
31977 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31978 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31979 /*or_comma=*/false,
31980 /*consume_paren=*/true);
31983 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
31985 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
31986 OMP_CLAUSE_ASYNC_EXPR (c) = t;
31987 OMP_CLAUSE_CHAIN (c) = list;
31988 list = c;
31990 return list;
31993 /* Parse all OpenACC clauses. The set clauses allowed by the directive
31994 is a bitmask in MASK. Return the list of clauses found. */
31996 static tree
31997 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
31998 const char *where, cp_token *pragma_tok,
31999 bool finish_p = true)
32001 tree clauses = NULL;
32002 bool first = true;
32004 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32006 location_t here;
32007 pragma_omp_clause c_kind;
32008 omp_clause_code code;
32009 const char *c_name;
32010 tree prev = clauses;
32012 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32013 cp_lexer_consume_token (parser->lexer);
32015 here = cp_lexer_peek_token (parser->lexer)->location;
32016 c_kind = cp_parser_omp_clause_name (parser);
32018 switch (c_kind)
32020 case PRAGMA_OACC_CLAUSE_ASYNC:
32021 clauses = cp_parser_oacc_clause_async (parser, clauses);
32022 c_name = "async";
32023 break;
32024 case PRAGMA_OACC_CLAUSE_AUTO:
32025 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
32026 clauses, here);
32027 c_name = "auto";
32028 break;
32029 case PRAGMA_OACC_CLAUSE_COLLAPSE:
32030 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
32031 c_name = "collapse";
32032 break;
32033 case PRAGMA_OACC_CLAUSE_COPY:
32034 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32035 c_name = "copy";
32036 break;
32037 case PRAGMA_OACC_CLAUSE_COPYIN:
32038 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32039 c_name = "copyin";
32040 break;
32041 case PRAGMA_OACC_CLAUSE_COPYOUT:
32042 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32043 c_name = "copyout";
32044 break;
32045 case PRAGMA_OACC_CLAUSE_CREATE:
32046 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32047 c_name = "create";
32048 break;
32049 case PRAGMA_OACC_CLAUSE_DELETE:
32050 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32051 c_name = "delete";
32052 break;
32053 case PRAGMA_OMP_CLAUSE_DEFAULT:
32054 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
32055 c_name = "default";
32056 break;
32057 case PRAGMA_OACC_CLAUSE_DEVICE:
32058 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32059 c_name = "device";
32060 break;
32061 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
32062 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
32063 c_name = "deviceptr";
32064 break;
32065 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
32066 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32067 c_name = "device_resident";
32068 break;
32069 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
32070 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32071 clauses);
32072 c_name = "firstprivate";
32073 break;
32074 case PRAGMA_OACC_CLAUSE_GANG:
32075 c_name = "gang";
32076 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
32077 c_name, clauses);
32078 break;
32079 case PRAGMA_OACC_CLAUSE_HOST:
32080 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32081 c_name = "host";
32082 break;
32083 case PRAGMA_OACC_CLAUSE_IF:
32084 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
32085 c_name = "if";
32086 break;
32087 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
32088 clauses = cp_parser_oacc_simple_clause (parser,
32089 OMP_CLAUSE_INDEPENDENT,
32090 clauses, here);
32091 c_name = "independent";
32092 break;
32093 case PRAGMA_OACC_CLAUSE_LINK:
32094 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32095 c_name = "link";
32096 break;
32097 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
32098 code = OMP_CLAUSE_NUM_GANGS;
32099 c_name = "num_gangs";
32100 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32101 clauses);
32102 break;
32103 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
32104 c_name = "num_workers";
32105 code = OMP_CLAUSE_NUM_WORKERS;
32106 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32107 clauses);
32108 break;
32109 case PRAGMA_OACC_CLAUSE_PRESENT:
32110 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32111 c_name = "present";
32112 break;
32113 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
32114 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32115 c_name = "present_or_copy";
32116 break;
32117 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
32118 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32119 c_name = "present_or_copyin";
32120 break;
32121 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
32122 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32123 c_name = "present_or_copyout";
32124 break;
32125 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
32126 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32127 c_name = "present_or_create";
32128 break;
32129 case PRAGMA_OACC_CLAUSE_PRIVATE:
32130 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
32131 clauses);
32132 c_name = "private";
32133 break;
32134 case PRAGMA_OACC_CLAUSE_REDUCTION:
32135 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32136 c_name = "reduction";
32137 break;
32138 case PRAGMA_OACC_CLAUSE_SELF:
32139 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32140 c_name = "self";
32141 break;
32142 case PRAGMA_OACC_CLAUSE_SEQ:
32143 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
32144 clauses, here);
32145 c_name = "seq";
32146 break;
32147 case PRAGMA_OACC_CLAUSE_TILE:
32148 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
32149 c_name = "tile";
32150 break;
32151 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
32152 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
32153 clauses);
32154 c_name = "use_device";
32155 break;
32156 case PRAGMA_OACC_CLAUSE_VECTOR:
32157 c_name = "vector";
32158 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
32159 c_name, clauses);
32160 break;
32161 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
32162 c_name = "vector_length";
32163 code = OMP_CLAUSE_VECTOR_LENGTH;
32164 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32165 clauses);
32166 break;
32167 case PRAGMA_OACC_CLAUSE_WAIT:
32168 clauses = cp_parser_oacc_clause_wait (parser, clauses);
32169 c_name = "wait";
32170 break;
32171 case PRAGMA_OACC_CLAUSE_WORKER:
32172 c_name = "worker";
32173 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
32174 c_name, clauses);
32175 break;
32176 default:
32177 cp_parser_error (parser, "expected %<#pragma acc%> clause");
32178 goto saw_error;
32181 first = false;
32183 if (((mask >> c_kind) & 1) == 0)
32185 /* Remove the invalid clause(s) from the list to avoid
32186 confusing the rest of the compiler. */
32187 clauses = prev;
32188 error_at (here, "%qs is not valid for %qs", c_name, where);
32192 saw_error:
32193 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32195 if (finish_p)
32196 return finish_omp_clauses (clauses, false);
32198 return clauses;
32201 /* Parse all OpenMP clauses. The set clauses allowed by the directive
32202 is a bitmask in MASK. Return the list of clauses found; the result
32203 of clause default goes in *pdefault. */
32205 static tree
32206 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
32207 const char *where, cp_token *pragma_tok,
32208 bool finish_p = true)
32210 tree clauses = NULL;
32211 bool first = true;
32212 cp_token *token = NULL;
32214 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32216 pragma_omp_clause c_kind;
32217 const char *c_name;
32218 tree prev = clauses;
32220 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32221 cp_lexer_consume_token (parser->lexer);
32223 token = cp_lexer_peek_token (parser->lexer);
32224 c_kind = cp_parser_omp_clause_name (parser);
32226 switch (c_kind)
32228 case PRAGMA_OMP_CLAUSE_COLLAPSE:
32229 clauses = cp_parser_omp_clause_collapse (parser, clauses,
32230 token->location);
32231 c_name = "collapse";
32232 break;
32233 case PRAGMA_OMP_CLAUSE_COPYIN:
32234 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
32235 c_name = "copyin";
32236 break;
32237 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
32238 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
32239 clauses);
32240 c_name = "copyprivate";
32241 break;
32242 case PRAGMA_OMP_CLAUSE_DEFAULT:
32243 clauses = cp_parser_omp_clause_default (parser, clauses,
32244 token->location, false);
32245 c_name = "default";
32246 break;
32247 case PRAGMA_OMP_CLAUSE_FINAL:
32248 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
32249 c_name = "final";
32250 break;
32251 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
32252 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32253 clauses);
32254 c_name = "firstprivate";
32255 break;
32256 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
32257 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
32258 token->location);
32259 c_name = "grainsize";
32260 break;
32261 case PRAGMA_OMP_CLAUSE_HINT:
32262 clauses = cp_parser_omp_clause_hint (parser, clauses,
32263 token->location);
32264 c_name = "hint";
32265 break;
32266 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
32267 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
32268 token->location);
32269 c_name = "defaultmap";
32270 break;
32271 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
32272 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
32273 clauses);
32274 c_name = "use_device_ptr";
32275 break;
32276 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
32277 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
32278 clauses);
32279 c_name = "is_device_ptr";
32280 break;
32281 case PRAGMA_OMP_CLAUSE_IF:
32282 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
32283 true);
32284 c_name = "if";
32285 break;
32286 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
32287 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
32288 clauses);
32289 c_name = "lastprivate";
32290 break;
32291 case PRAGMA_OMP_CLAUSE_MERGEABLE:
32292 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
32293 token->location);
32294 c_name = "mergeable";
32295 break;
32296 case PRAGMA_OMP_CLAUSE_NOWAIT:
32297 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
32298 c_name = "nowait";
32299 break;
32300 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
32301 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
32302 token->location);
32303 c_name = "num_tasks";
32304 break;
32305 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
32306 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
32307 token->location);
32308 c_name = "num_threads";
32309 break;
32310 case PRAGMA_OMP_CLAUSE_ORDERED:
32311 clauses = cp_parser_omp_clause_ordered (parser, clauses,
32312 token->location);
32313 c_name = "ordered";
32314 break;
32315 case PRAGMA_OMP_CLAUSE_PRIORITY:
32316 clauses = cp_parser_omp_clause_priority (parser, clauses,
32317 token->location);
32318 c_name = "priority";
32319 break;
32320 case PRAGMA_OMP_CLAUSE_PRIVATE:
32321 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
32322 clauses);
32323 c_name = "private";
32324 break;
32325 case PRAGMA_OMP_CLAUSE_REDUCTION:
32326 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32327 c_name = "reduction";
32328 break;
32329 case PRAGMA_OMP_CLAUSE_SCHEDULE:
32330 clauses = cp_parser_omp_clause_schedule (parser, clauses,
32331 token->location);
32332 c_name = "schedule";
32333 break;
32334 case PRAGMA_OMP_CLAUSE_SHARED:
32335 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
32336 clauses);
32337 c_name = "shared";
32338 break;
32339 case PRAGMA_OMP_CLAUSE_UNTIED:
32340 clauses = cp_parser_omp_clause_untied (parser, clauses,
32341 token->location);
32342 c_name = "untied";
32343 break;
32344 case PRAGMA_OMP_CLAUSE_INBRANCH:
32345 case PRAGMA_CILK_CLAUSE_MASK:
32346 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
32347 clauses, token->location);
32348 c_name = "inbranch";
32349 break;
32350 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
32351 case PRAGMA_CILK_CLAUSE_NOMASK:
32352 clauses = cp_parser_omp_clause_branch (parser,
32353 OMP_CLAUSE_NOTINBRANCH,
32354 clauses, token->location);
32355 c_name = "notinbranch";
32356 break;
32357 case PRAGMA_OMP_CLAUSE_PARALLEL:
32358 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
32359 clauses, token->location);
32360 c_name = "parallel";
32361 if (!first)
32363 clause_not_first:
32364 error_at (token->location, "%qs must be the first clause of %qs",
32365 c_name, where);
32366 clauses = prev;
32368 break;
32369 case PRAGMA_OMP_CLAUSE_FOR:
32370 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
32371 clauses, token->location);
32372 c_name = "for";
32373 if (!first)
32374 goto clause_not_first;
32375 break;
32376 case PRAGMA_OMP_CLAUSE_SECTIONS:
32377 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
32378 clauses, token->location);
32379 c_name = "sections";
32380 if (!first)
32381 goto clause_not_first;
32382 break;
32383 case PRAGMA_OMP_CLAUSE_TASKGROUP:
32384 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
32385 clauses, token->location);
32386 c_name = "taskgroup";
32387 if (!first)
32388 goto clause_not_first;
32389 break;
32390 case PRAGMA_OMP_CLAUSE_LINK:
32391 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
32392 c_name = "to";
32393 break;
32394 case PRAGMA_OMP_CLAUSE_TO:
32395 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
32396 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
32397 clauses);
32398 else
32399 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
32400 c_name = "to";
32401 break;
32402 case PRAGMA_OMP_CLAUSE_FROM:
32403 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
32404 c_name = "from";
32405 break;
32406 case PRAGMA_OMP_CLAUSE_UNIFORM:
32407 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
32408 clauses);
32409 c_name = "uniform";
32410 break;
32411 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
32412 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
32413 token->location);
32414 c_name = "num_teams";
32415 break;
32416 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
32417 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
32418 token->location);
32419 c_name = "thread_limit";
32420 break;
32421 case PRAGMA_OMP_CLAUSE_ALIGNED:
32422 clauses = cp_parser_omp_clause_aligned (parser, clauses);
32423 c_name = "aligned";
32424 break;
32425 case PRAGMA_OMP_CLAUSE_LINEAR:
32427 bool cilk_simd_fn = false, declare_simd = false;
32428 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
32429 cilk_simd_fn = true;
32430 else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
32431 declare_simd = true;
32432 clauses = cp_parser_omp_clause_linear (parser, clauses,
32433 cilk_simd_fn, declare_simd);
32435 c_name = "linear";
32436 break;
32437 case PRAGMA_OMP_CLAUSE_DEPEND:
32438 clauses = cp_parser_omp_clause_depend (parser, clauses,
32439 token->location);
32440 c_name = "depend";
32441 break;
32442 case PRAGMA_OMP_CLAUSE_MAP:
32443 clauses = cp_parser_omp_clause_map (parser, clauses);
32444 c_name = "map";
32445 break;
32446 case PRAGMA_OMP_CLAUSE_DEVICE:
32447 clauses = cp_parser_omp_clause_device (parser, clauses,
32448 token->location);
32449 c_name = "device";
32450 break;
32451 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
32452 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
32453 token->location);
32454 c_name = "dist_schedule";
32455 break;
32456 case PRAGMA_OMP_CLAUSE_PROC_BIND:
32457 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
32458 token->location);
32459 c_name = "proc_bind";
32460 break;
32461 case PRAGMA_OMP_CLAUSE_SAFELEN:
32462 clauses = cp_parser_omp_clause_safelen (parser, clauses,
32463 token->location);
32464 c_name = "safelen";
32465 break;
32466 case PRAGMA_OMP_CLAUSE_SIMDLEN:
32467 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
32468 token->location);
32469 c_name = "simdlen";
32470 break;
32471 case PRAGMA_OMP_CLAUSE_NOGROUP:
32472 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
32473 token->location);
32474 c_name = "nogroup";
32475 break;
32476 case PRAGMA_OMP_CLAUSE_THREADS:
32477 clauses
32478 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
32479 clauses, token->location);
32480 c_name = "threads";
32481 break;
32482 case PRAGMA_OMP_CLAUSE_SIMD:
32483 clauses
32484 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
32485 clauses, token->location);
32486 c_name = "simd";
32487 break;
32488 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
32489 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
32490 c_name = "simdlen";
32491 break;
32492 default:
32493 cp_parser_error (parser, "expected %<#pragma omp%> clause");
32494 goto saw_error;
32497 first = false;
32499 if (((mask >> c_kind) & 1) == 0)
32501 /* Remove the invalid clause(s) from the list to avoid
32502 confusing the rest of the compiler. */
32503 clauses = prev;
32504 error_at (token->location, "%qs is not valid for %qs", c_name, where);
32507 saw_error:
32508 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
32509 no reason to skip to the end. */
32510 if (!(flag_cilkplus && pragma_tok == NULL))
32511 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32512 if (finish_p)
32514 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
32515 return finish_omp_clauses (clauses, false, true);
32516 else
32517 return finish_omp_clauses (clauses, true);
32519 return clauses;
32522 /* OpenMP 2.5:
32523 structured-block:
32524 statement
32526 In practice, we're also interested in adding the statement to an
32527 outer node. So it is convenient if we work around the fact that
32528 cp_parser_statement calls add_stmt. */
32530 static unsigned
32531 cp_parser_begin_omp_structured_block (cp_parser *parser)
32533 unsigned save = parser->in_statement;
32535 /* Only move the values to IN_OMP_BLOCK if they weren't false.
32536 This preserves the "not within loop or switch" style error messages
32537 for nonsense cases like
32538 void foo() {
32539 #pragma omp single
32540 break;
32543 if (parser->in_statement)
32544 parser->in_statement = IN_OMP_BLOCK;
32546 return save;
32549 static void
32550 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
32552 parser->in_statement = save;
32555 static tree
32556 cp_parser_omp_structured_block (cp_parser *parser)
32558 tree stmt = begin_omp_structured_block ();
32559 unsigned int save = cp_parser_begin_omp_structured_block (parser);
32561 cp_parser_statement (parser, NULL_TREE, false, NULL);
32563 cp_parser_end_omp_structured_block (parser, save);
32564 return finish_omp_structured_block (stmt);
32567 /* OpenMP 2.5:
32568 # pragma omp atomic new-line
32569 expression-stmt
32571 expression-stmt:
32572 x binop= expr | x++ | ++x | x-- | --x
32573 binop:
32574 +, *, -, /, &, ^, |, <<, >>
32576 where x is an lvalue expression with scalar type.
32578 OpenMP 3.1:
32579 # pragma omp atomic new-line
32580 update-stmt
32582 # pragma omp atomic read new-line
32583 read-stmt
32585 # pragma omp atomic write new-line
32586 write-stmt
32588 # pragma omp atomic update new-line
32589 update-stmt
32591 # pragma omp atomic capture new-line
32592 capture-stmt
32594 # pragma omp atomic capture new-line
32595 capture-block
32597 read-stmt:
32598 v = x
32599 write-stmt:
32600 x = expr
32601 update-stmt:
32602 expression-stmt | x = x binop expr
32603 capture-stmt:
32604 v = expression-stmt
32605 capture-block:
32606 { v = x; update-stmt; } | { update-stmt; v = x; }
32608 OpenMP 4.0:
32609 update-stmt:
32610 expression-stmt | x = x binop expr | x = expr binop x
32611 capture-stmt:
32612 v = update-stmt
32613 capture-block:
32614 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
32616 where x and v are lvalue expressions with scalar type. */
32618 static void
32619 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
32621 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
32622 tree rhs1 = NULL_TREE, orig_lhs;
32623 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
32624 bool structured_block = false;
32625 bool seq_cst = false;
32627 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32629 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32630 const char *p = IDENTIFIER_POINTER (id);
32632 if (!strcmp (p, "seq_cst"))
32634 seq_cst = true;
32635 cp_lexer_consume_token (parser->lexer);
32636 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
32637 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
32638 cp_lexer_consume_token (parser->lexer);
32641 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32643 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32644 const char *p = IDENTIFIER_POINTER (id);
32646 if (!strcmp (p, "read"))
32647 code = OMP_ATOMIC_READ;
32648 else if (!strcmp (p, "write"))
32649 code = NOP_EXPR;
32650 else if (!strcmp (p, "update"))
32651 code = OMP_ATOMIC;
32652 else if (!strcmp (p, "capture"))
32653 code = OMP_ATOMIC_CAPTURE_NEW;
32654 else
32655 p = NULL;
32656 if (p)
32657 cp_lexer_consume_token (parser->lexer);
32659 if (!seq_cst)
32661 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
32662 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
32663 cp_lexer_consume_token (parser->lexer);
32665 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32667 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32668 const char *p = IDENTIFIER_POINTER (id);
32670 if (!strcmp (p, "seq_cst"))
32672 seq_cst = true;
32673 cp_lexer_consume_token (parser->lexer);
32677 cp_parser_require_pragma_eol (parser, pragma_tok);
32679 switch (code)
32681 case OMP_ATOMIC_READ:
32682 case NOP_EXPR: /* atomic write */
32683 v = cp_parser_unary_expression (parser);
32684 if (v == error_mark_node)
32685 goto saw_error;
32686 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32687 goto saw_error;
32688 if (code == NOP_EXPR)
32689 lhs = cp_parser_expression (parser);
32690 else
32691 lhs = cp_parser_unary_expression (parser);
32692 if (lhs == error_mark_node)
32693 goto saw_error;
32694 if (code == NOP_EXPR)
32696 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
32697 opcode. */
32698 code = OMP_ATOMIC;
32699 rhs = lhs;
32700 lhs = v;
32701 v = NULL_TREE;
32703 goto done;
32704 case OMP_ATOMIC_CAPTURE_NEW:
32705 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
32707 cp_lexer_consume_token (parser->lexer);
32708 structured_block = true;
32710 else
32712 v = cp_parser_unary_expression (parser);
32713 if (v == error_mark_node)
32714 goto saw_error;
32715 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32716 goto saw_error;
32718 default:
32719 break;
32722 restart:
32723 lhs = cp_parser_unary_expression (parser);
32724 orig_lhs = lhs;
32725 switch (TREE_CODE (lhs))
32727 case ERROR_MARK:
32728 goto saw_error;
32730 case POSTINCREMENT_EXPR:
32731 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
32732 code = OMP_ATOMIC_CAPTURE_OLD;
32733 /* FALLTHROUGH */
32734 case PREINCREMENT_EXPR:
32735 lhs = TREE_OPERAND (lhs, 0);
32736 opcode = PLUS_EXPR;
32737 rhs = integer_one_node;
32738 break;
32740 case POSTDECREMENT_EXPR:
32741 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
32742 code = OMP_ATOMIC_CAPTURE_OLD;
32743 /* FALLTHROUGH */
32744 case PREDECREMENT_EXPR:
32745 lhs = TREE_OPERAND (lhs, 0);
32746 opcode = MINUS_EXPR;
32747 rhs = integer_one_node;
32748 break;
32750 case COMPOUND_EXPR:
32751 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
32752 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
32753 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
32754 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
32755 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
32756 (TREE_OPERAND (lhs, 1), 0), 0)))
32757 == BOOLEAN_TYPE)
32758 /* Undo effects of boolean_increment for post {in,de}crement. */
32759 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
32760 /* FALLTHRU */
32761 case MODIFY_EXPR:
32762 if (TREE_CODE (lhs) == MODIFY_EXPR
32763 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
32765 /* Undo effects of boolean_increment. */
32766 if (integer_onep (TREE_OPERAND (lhs, 1)))
32768 /* This is pre or post increment. */
32769 rhs = TREE_OPERAND (lhs, 1);
32770 lhs = TREE_OPERAND (lhs, 0);
32771 opcode = NOP_EXPR;
32772 if (code == OMP_ATOMIC_CAPTURE_NEW
32773 && !structured_block
32774 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
32775 code = OMP_ATOMIC_CAPTURE_OLD;
32776 break;
32779 /* FALLTHRU */
32780 default:
32781 switch (cp_lexer_peek_token (parser->lexer)->type)
32783 case CPP_MULT_EQ:
32784 opcode = MULT_EXPR;
32785 break;
32786 case CPP_DIV_EQ:
32787 opcode = TRUNC_DIV_EXPR;
32788 break;
32789 case CPP_PLUS_EQ:
32790 opcode = PLUS_EXPR;
32791 break;
32792 case CPP_MINUS_EQ:
32793 opcode = MINUS_EXPR;
32794 break;
32795 case CPP_LSHIFT_EQ:
32796 opcode = LSHIFT_EXPR;
32797 break;
32798 case CPP_RSHIFT_EQ:
32799 opcode = RSHIFT_EXPR;
32800 break;
32801 case CPP_AND_EQ:
32802 opcode = BIT_AND_EXPR;
32803 break;
32804 case CPP_OR_EQ:
32805 opcode = BIT_IOR_EXPR;
32806 break;
32807 case CPP_XOR_EQ:
32808 opcode = BIT_XOR_EXPR;
32809 break;
32810 case CPP_EQ:
32811 enum cp_parser_prec oprec;
32812 cp_token *token;
32813 cp_lexer_consume_token (parser->lexer);
32814 cp_parser_parse_tentatively (parser);
32815 rhs1 = cp_parser_simple_cast_expression (parser);
32816 if (rhs1 == error_mark_node)
32818 cp_parser_abort_tentative_parse (parser);
32819 cp_parser_simple_cast_expression (parser);
32820 goto saw_error;
32822 token = cp_lexer_peek_token (parser->lexer);
32823 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
32825 cp_parser_abort_tentative_parse (parser);
32826 cp_parser_parse_tentatively (parser);
32827 rhs = cp_parser_binary_expression (parser, false, true,
32828 PREC_NOT_OPERATOR, NULL);
32829 if (rhs == error_mark_node)
32831 cp_parser_abort_tentative_parse (parser);
32832 cp_parser_binary_expression (parser, false, true,
32833 PREC_NOT_OPERATOR, NULL);
32834 goto saw_error;
32836 switch (TREE_CODE (rhs))
32838 case MULT_EXPR:
32839 case TRUNC_DIV_EXPR:
32840 case RDIV_EXPR:
32841 case PLUS_EXPR:
32842 case MINUS_EXPR:
32843 case LSHIFT_EXPR:
32844 case RSHIFT_EXPR:
32845 case BIT_AND_EXPR:
32846 case BIT_IOR_EXPR:
32847 case BIT_XOR_EXPR:
32848 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
32850 if (cp_parser_parse_definitely (parser))
32852 opcode = TREE_CODE (rhs);
32853 rhs1 = TREE_OPERAND (rhs, 0);
32854 rhs = TREE_OPERAND (rhs, 1);
32855 goto stmt_done;
32857 else
32858 goto saw_error;
32860 break;
32861 default:
32862 break;
32864 cp_parser_abort_tentative_parse (parser);
32865 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
32867 rhs = cp_parser_expression (parser);
32868 if (rhs == error_mark_node)
32869 goto saw_error;
32870 opcode = NOP_EXPR;
32871 rhs1 = NULL_TREE;
32872 goto stmt_done;
32874 cp_parser_error (parser,
32875 "invalid form of %<#pragma omp atomic%>");
32876 goto saw_error;
32878 if (!cp_parser_parse_definitely (parser))
32879 goto saw_error;
32880 switch (token->type)
32882 case CPP_SEMICOLON:
32883 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
32885 code = OMP_ATOMIC_CAPTURE_OLD;
32886 v = lhs;
32887 lhs = NULL_TREE;
32888 lhs1 = rhs1;
32889 rhs1 = NULL_TREE;
32890 cp_lexer_consume_token (parser->lexer);
32891 goto restart;
32893 else if (structured_block)
32895 opcode = NOP_EXPR;
32896 rhs = rhs1;
32897 rhs1 = NULL_TREE;
32898 goto stmt_done;
32900 cp_parser_error (parser,
32901 "invalid form of %<#pragma omp atomic%>");
32902 goto saw_error;
32903 case CPP_MULT:
32904 opcode = MULT_EXPR;
32905 break;
32906 case CPP_DIV:
32907 opcode = TRUNC_DIV_EXPR;
32908 break;
32909 case CPP_PLUS:
32910 opcode = PLUS_EXPR;
32911 break;
32912 case CPP_MINUS:
32913 opcode = MINUS_EXPR;
32914 break;
32915 case CPP_LSHIFT:
32916 opcode = LSHIFT_EXPR;
32917 break;
32918 case CPP_RSHIFT:
32919 opcode = RSHIFT_EXPR;
32920 break;
32921 case CPP_AND:
32922 opcode = BIT_AND_EXPR;
32923 break;
32924 case CPP_OR:
32925 opcode = BIT_IOR_EXPR;
32926 break;
32927 case CPP_XOR:
32928 opcode = BIT_XOR_EXPR;
32929 break;
32930 default:
32931 cp_parser_error (parser,
32932 "invalid operator for %<#pragma omp atomic%>");
32933 goto saw_error;
32935 oprec = TOKEN_PRECEDENCE (token);
32936 gcc_assert (oprec != PREC_NOT_OPERATOR);
32937 if (commutative_tree_code (opcode))
32938 oprec = (enum cp_parser_prec) (oprec - 1);
32939 cp_lexer_consume_token (parser->lexer);
32940 rhs = cp_parser_binary_expression (parser, false, false,
32941 oprec, NULL);
32942 if (rhs == error_mark_node)
32943 goto saw_error;
32944 goto stmt_done;
32945 /* FALLTHROUGH */
32946 default:
32947 cp_parser_error (parser,
32948 "invalid operator for %<#pragma omp atomic%>");
32949 goto saw_error;
32951 cp_lexer_consume_token (parser->lexer);
32953 rhs = cp_parser_expression (parser);
32954 if (rhs == error_mark_node)
32955 goto saw_error;
32956 break;
32958 stmt_done:
32959 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
32961 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
32962 goto saw_error;
32963 v = cp_parser_unary_expression (parser);
32964 if (v == error_mark_node)
32965 goto saw_error;
32966 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32967 goto saw_error;
32968 lhs1 = cp_parser_unary_expression (parser);
32969 if (lhs1 == error_mark_node)
32970 goto saw_error;
32972 if (structured_block)
32974 cp_parser_consume_semicolon_at_end_of_statement (parser);
32975 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
32977 done:
32978 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
32979 if (!structured_block)
32980 cp_parser_consume_semicolon_at_end_of_statement (parser);
32981 return;
32983 saw_error:
32984 cp_parser_skip_to_end_of_block_or_statement (parser);
32985 if (structured_block)
32987 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
32988 cp_lexer_consume_token (parser->lexer);
32989 else if (code == OMP_ATOMIC_CAPTURE_NEW)
32991 cp_parser_skip_to_end_of_block_or_statement (parser);
32992 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
32993 cp_lexer_consume_token (parser->lexer);
32999 /* OpenMP 2.5:
33000 # pragma omp barrier new-line */
33002 static void
33003 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
33005 cp_parser_require_pragma_eol (parser, pragma_tok);
33006 finish_omp_barrier ();
33009 /* OpenMP 2.5:
33010 # pragma omp critical [(name)] new-line
33011 structured-block
33013 OpenMP 4.5:
33014 # pragma omp critical [(name) [hint(expression)]] new-line
33015 structured-block */
33017 #define OMP_CRITICAL_CLAUSE_MASK \
33018 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
33020 static tree
33021 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
33023 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
33025 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33027 cp_lexer_consume_token (parser->lexer);
33029 name = cp_parser_identifier (parser);
33031 if (name == error_mark_node
33032 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33033 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33034 /*or_comma=*/false,
33035 /*consume_paren=*/true);
33036 if (name == error_mark_node)
33037 name = NULL;
33039 clauses = cp_parser_omp_all_clauses (parser,
33040 OMP_CRITICAL_CLAUSE_MASK,
33041 "#pragma omp critical", pragma_tok);
33043 else
33044 cp_parser_require_pragma_eol (parser, pragma_tok);
33046 stmt = cp_parser_omp_structured_block (parser);
33047 return c_finish_omp_critical (input_location, stmt, name, clauses);
33050 /* OpenMP 2.5:
33051 # pragma omp flush flush-vars[opt] new-line
33053 flush-vars:
33054 ( variable-list ) */
33056 static void
33057 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
33059 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33060 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
33061 cp_parser_require_pragma_eol (parser, pragma_tok);
33063 finish_omp_flush ();
33066 /* Helper function, to parse omp for increment expression. */
33068 static tree
33069 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
33071 tree cond = cp_parser_binary_expression (parser, false, true,
33072 PREC_NOT_OPERATOR, NULL);
33073 if (cond == error_mark_node
33074 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33076 cp_parser_skip_to_end_of_statement (parser);
33077 return error_mark_node;
33080 switch (TREE_CODE (cond))
33082 case GT_EXPR:
33083 case GE_EXPR:
33084 case LT_EXPR:
33085 case LE_EXPR:
33086 break;
33087 case NE_EXPR:
33088 if (code == CILK_SIMD || code == CILK_FOR)
33089 break;
33090 /* Fall through: OpenMP disallows NE_EXPR. */
33091 default:
33092 return error_mark_node;
33095 /* If decl is an iterator, preserve LHS and RHS of the relational
33096 expr until finish_omp_for. */
33097 if (decl
33098 && (type_dependent_expression_p (decl)
33099 || CLASS_TYPE_P (TREE_TYPE (decl))))
33100 return cond;
33102 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
33103 TREE_CODE (cond),
33104 TREE_OPERAND (cond, 0), ERROR_MARK,
33105 TREE_OPERAND (cond, 1), ERROR_MARK,
33106 /*overload=*/NULL, tf_warning_or_error);
33109 /* Helper function, to parse omp for increment expression. */
33111 static tree
33112 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
33114 cp_token *token = cp_lexer_peek_token (parser->lexer);
33115 enum tree_code op;
33116 tree lhs, rhs;
33117 cp_id_kind idk;
33118 bool decl_first;
33120 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
33122 op = (token->type == CPP_PLUS_PLUS
33123 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
33124 cp_lexer_consume_token (parser->lexer);
33125 lhs = cp_parser_simple_cast_expression (parser);
33126 if (lhs != decl
33127 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
33128 return error_mark_node;
33129 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
33132 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
33133 if (lhs != decl
33134 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
33135 return error_mark_node;
33137 token = cp_lexer_peek_token (parser->lexer);
33138 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
33140 op = (token->type == CPP_PLUS_PLUS
33141 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
33142 cp_lexer_consume_token (parser->lexer);
33143 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
33146 op = cp_parser_assignment_operator_opt (parser);
33147 if (op == ERROR_MARK)
33148 return error_mark_node;
33150 if (op != NOP_EXPR)
33152 rhs = cp_parser_assignment_expression (parser);
33153 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
33154 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
33157 lhs = cp_parser_binary_expression (parser, false, false,
33158 PREC_ADDITIVE_EXPRESSION, NULL);
33159 token = cp_lexer_peek_token (parser->lexer);
33160 decl_first = (lhs == decl
33161 || (processing_template_decl && cp_tree_equal (lhs, decl)));
33162 if (decl_first)
33163 lhs = NULL_TREE;
33164 if (token->type != CPP_PLUS
33165 && token->type != CPP_MINUS)
33166 return error_mark_node;
33170 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
33171 cp_lexer_consume_token (parser->lexer);
33172 rhs = cp_parser_binary_expression (parser, false, false,
33173 PREC_ADDITIVE_EXPRESSION, NULL);
33174 token = cp_lexer_peek_token (parser->lexer);
33175 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
33177 if (lhs == NULL_TREE)
33179 if (op == PLUS_EXPR)
33180 lhs = rhs;
33181 else
33182 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
33183 tf_warning_or_error);
33185 else
33186 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
33187 ERROR_MARK, NULL, tf_warning_or_error);
33190 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
33192 if (!decl_first)
33194 if ((rhs != decl
33195 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
33196 || op == MINUS_EXPR)
33197 return error_mark_node;
33198 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
33200 else
33201 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
33203 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
33206 /* Parse the initialization statement of either an OpenMP for loop or
33207 a Cilk Plus for loop.
33209 Return true if the resulting construct should have an
33210 OMP_CLAUSE_PRIVATE added to it. */
33212 static tree
33213 cp_parser_omp_for_loop_init (cp_parser *parser,
33214 enum tree_code code,
33215 tree &this_pre_body,
33216 vec<tree, va_gc> *for_block,
33217 tree &init,
33218 tree &orig_init,
33219 tree &decl,
33220 tree &real_decl)
33222 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33223 return NULL_TREE;
33225 tree add_private_clause = NULL_TREE;
33227 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
33229 init-expr:
33230 var = lb
33231 integer-type var = lb
33232 random-access-iterator-type var = lb
33233 pointer-type var = lb
33235 cp_decl_specifier_seq type_specifiers;
33237 /* First, try to parse as an initialized declaration. See
33238 cp_parser_condition, from whence the bulk of this is copied. */
33240 cp_parser_parse_tentatively (parser);
33241 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
33242 /*is_trailing_return=*/false,
33243 &type_specifiers);
33244 if (cp_parser_parse_definitely (parser))
33246 /* If parsing a type specifier seq succeeded, then this
33247 MUST be a initialized declaration. */
33248 tree asm_specification, attributes;
33249 cp_declarator *declarator;
33251 declarator = cp_parser_declarator (parser,
33252 CP_PARSER_DECLARATOR_NAMED,
33253 /*ctor_dtor_or_conv_p=*/NULL,
33254 /*parenthesized_p=*/NULL,
33255 /*member_p=*/false,
33256 /*friend_p=*/false);
33257 attributes = cp_parser_attributes_opt (parser);
33258 asm_specification = cp_parser_asm_specification_opt (parser);
33260 if (declarator == cp_error_declarator)
33261 cp_parser_skip_to_end_of_statement (parser);
33263 else
33265 tree pushed_scope, auto_node;
33267 decl = start_decl (declarator, &type_specifiers,
33268 SD_INITIALIZED, attributes,
33269 /*prefix_attributes=*/NULL_TREE,
33270 &pushed_scope);
33272 auto_node = type_uses_auto (TREE_TYPE (decl));
33273 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
33275 if (cp_lexer_next_token_is (parser->lexer,
33276 CPP_OPEN_PAREN))
33278 if (code != CILK_SIMD && code != CILK_FOR)
33279 error ("parenthesized initialization is not allowed in "
33280 "OpenMP %<for%> loop");
33281 else
33282 error ("parenthesized initialization is "
33283 "not allowed in for-loop");
33285 else
33286 /* Trigger an error. */
33287 cp_parser_require (parser, CPP_EQ, RT_EQ);
33289 init = error_mark_node;
33290 cp_parser_skip_to_end_of_statement (parser);
33292 else if (CLASS_TYPE_P (TREE_TYPE (decl))
33293 || type_dependent_expression_p (decl)
33294 || auto_node)
33296 bool is_direct_init, is_non_constant_init;
33298 init = cp_parser_initializer (parser,
33299 &is_direct_init,
33300 &is_non_constant_init);
33302 if (auto_node)
33304 TREE_TYPE (decl)
33305 = do_auto_deduction (TREE_TYPE (decl), init,
33306 auto_node);
33308 if (!CLASS_TYPE_P (TREE_TYPE (decl))
33309 && !type_dependent_expression_p (decl))
33310 goto non_class;
33313 cp_finish_decl (decl, init, !is_non_constant_init,
33314 asm_specification,
33315 LOOKUP_ONLYCONVERTING);
33316 orig_init = init;
33317 if (CLASS_TYPE_P (TREE_TYPE (decl)))
33319 vec_safe_push (for_block, this_pre_body);
33320 init = NULL_TREE;
33322 else
33323 init = pop_stmt_list (this_pre_body);
33324 this_pre_body = NULL_TREE;
33326 else
33328 /* Consume '='. */
33329 cp_lexer_consume_token (parser->lexer);
33330 init = cp_parser_assignment_expression (parser);
33332 non_class:
33333 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
33334 init = error_mark_node;
33335 else
33336 cp_finish_decl (decl, NULL_TREE,
33337 /*init_const_expr_p=*/false,
33338 asm_specification,
33339 LOOKUP_ONLYCONVERTING);
33342 if (pushed_scope)
33343 pop_scope (pushed_scope);
33346 else
33348 cp_id_kind idk;
33349 /* If parsing a type specifier sequence failed, then
33350 this MUST be a simple expression. */
33351 if (code == CILK_FOR)
33352 error ("%<_Cilk_for%> allows expression instead of declaration only "
33353 "in C, not in C++");
33354 cp_parser_parse_tentatively (parser);
33355 decl = cp_parser_primary_expression (parser, false, false,
33356 false, &idk);
33357 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
33358 if (!cp_parser_error_occurred (parser)
33359 && decl
33360 && (TREE_CODE (decl) == COMPONENT_REF
33361 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
33363 cp_parser_abort_tentative_parse (parser);
33364 cp_parser_parse_tentatively (parser);
33365 cp_token *token = cp_lexer_peek_token (parser->lexer);
33366 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
33367 /*check_dependency_p=*/true,
33368 /*template_p=*/NULL,
33369 /*declarator_p=*/false,
33370 /*optional_p=*/false);
33371 if (name != error_mark_node
33372 && last_tok == cp_lexer_peek_token (parser->lexer))
33374 decl = cp_parser_lookup_name_simple (parser, name,
33375 token->location);
33376 if (TREE_CODE (decl) == FIELD_DECL)
33377 add_private_clause = omp_privatize_field (decl, false);
33379 cp_parser_abort_tentative_parse (parser);
33380 cp_parser_parse_tentatively (parser);
33381 decl = cp_parser_primary_expression (parser, false, false,
33382 false, &idk);
33384 if (!cp_parser_error_occurred (parser)
33385 && decl
33386 && DECL_P (decl)
33387 && CLASS_TYPE_P (TREE_TYPE (decl)))
33389 tree rhs;
33391 cp_parser_parse_definitely (parser);
33392 cp_parser_require (parser, CPP_EQ, RT_EQ);
33393 rhs = cp_parser_assignment_expression (parser);
33394 orig_init = rhs;
33395 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
33396 decl, NOP_EXPR,
33397 rhs,
33398 tf_warning_or_error));
33399 if (!add_private_clause)
33400 add_private_clause = decl;
33402 else
33404 decl = NULL;
33405 cp_parser_abort_tentative_parse (parser);
33406 init = cp_parser_expression (parser);
33407 if (init)
33409 if (TREE_CODE (init) == MODIFY_EXPR
33410 || TREE_CODE (init) == MODOP_EXPR)
33411 real_decl = TREE_OPERAND (init, 0);
33415 return add_private_clause;
33418 /* Parse the restricted form of the for statement allowed by OpenMP. */
33420 static tree
33421 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
33422 tree *cclauses)
33424 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
33425 tree real_decl, initv, condv, incrv, declv;
33426 tree this_pre_body, cl, ordered_cl = NULL_TREE;
33427 location_t loc_first;
33428 bool collapse_err = false;
33429 int i, collapse = 1, ordered = 0, count, nbraces = 0;
33430 vec<tree, va_gc> *for_block = make_tree_vector ();
33431 auto_vec<tree, 4> orig_inits;
33433 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
33434 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
33435 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
33436 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
33437 && OMP_CLAUSE_ORDERED_EXPR (cl))
33439 ordered_cl = cl;
33440 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
33443 if (ordered && ordered < collapse)
33445 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
33446 "%<ordered%> clause parameter is less than %<collapse%>");
33447 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
33448 = build_int_cst (NULL_TREE, collapse);
33449 ordered = collapse;
33451 if (ordered)
33453 for (tree *pc = &clauses; *pc; )
33454 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
33456 error_at (OMP_CLAUSE_LOCATION (*pc),
33457 "%<linear%> clause may not be specified together "
33458 "with %<ordered%> clause with a parameter");
33459 *pc = OMP_CLAUSE_CHAIN (*pc);
33461 else
33462 pc = &OMP_CLAUSE_CHAIN (*pc);
33465 gcc_assert (collapse >= 1 && ordered >= 0);
33466 count = ordered ? ordered : collapse;
33468 declv = make_tree_vec (count);
33469 initv = make_tree_vec (count);
33470 condv = make_tree_vec (count);
33471 incrv = make_tree_vec (count);
33473 loc_first = cp_lexer_peek_token (parser->lexer)->location;
33475 for (i = 0; i < count; i++)
33477 int bracecount = 0;
33478 tree add_private_clause = NULL_TREE;
33479 location_t loc;
33481 if (code != CILK_FOR
33482 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
33484 cp_parser_error (parser, "for statement expected");
33485 return NULL;
33487 if (code == CILK_FOR
33488 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
33490 cp_parser_error (parser, "_Cilk_for statement expected");
33491 return NULL;
33493 loc = cp_lexer_consume_token (parser->lexer)->location;
33495 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33496 return NULL;
33498 init = orig_init = decl = real_decl = NULL;
33499 this_pre_body = push_stmt_list ();
33501 add_private_clause
33502 = cp_parser_omp_for_loop_init (parser, code,
33503 this_pre_body, for_block,
33504 init, orig_init, decl, real_decl);
33506 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
33507 if (this_pre_body)
33509 this_pre_body = pop_stmt_list (this_pre_body);
33510 if (pre_body)
33512 tree t = pre_body;
33513 pre_body = push_stmt_list ();
33514 add_stmt (t);
33515 add_stmt (this_pre_body);
33516 pre_body = pop_stmt_list (pre_body);
33518 else
33519 pre_body = this_pre_body;
33522 if (decl)
33523 real_decl = decl;
33524 if (cclauses != NULL
33525 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
33526 && real_decl != NULL_TREE)
33528 tree *c;
33529 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
33530 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
33531 && OMP_CLAUSE_DECL (*c) == real_decl)
33533 error_at (loc, "iteration variable %qD"
33534 " should not be firstprivate", real_decl);
33535 *c = OMP_CLAUSE_CHAIN (*c);
33537 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
33538 && OMP_CLAUSE_DECL (*c) == real_decl)
33540 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
33541 tree l = *c;
33542 *c = OMP_CLAUSE_CHAIN (*c);
33543 if (code == OMP_SIMD)
33545 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33546 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
33548 else
33550 OMP_CLAUSE_CHAIN (l) = clauses;
33551 clauses = l;
33553 add_private_clause = NULL_TREE;
33555 else
33557 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
33558 && OMP_CLAUSE_DECL (*c) == real_decl)
33559 add_private_clause = NULL_TREE;
33560 c = &OMP_CLAUSE_CHAIN (*c);
33564 if (add_private_clause)
33566 tree c;
33567 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
33569 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
33570 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
33571 && OMP_CLAUSE_DECL (c) == decl)
33572 break;
33573 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
33574 && OMP_CLAUSE_DECL (c) == decl)
33575 error_at (loc, "iteration variable %qD "
33576 "should not be firstprivate",
33577 decl);
33578 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
33579 && OMP_CLAUSE_DECL (c) == decl)
33580 error_at (loc, "iteration variable %qD should not be reduction",
33581 decl);
33583 if (c == NULL)
33585 if (code != OMP_SIMD)
33586 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
33587 else if (collapse == 1)
33588 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
33589 else
33590 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
33591 OMP_CLAUSE_DECL (c) = add_private_clause;
33592 c = finish_omp_clauses (c, true);
33593 if (c)
33595 OMP_CLAUSE_CHAIN (c) = clauses;
33596 clauses = c;
33597 /* For linear, signal that we need to fill up
33598 the so far unknown linear step. */
33599 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
33600 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
33605 cond = NULL;
33606 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33607 cond = cp_parser_omp_for_cond (parser, decl, code);
33608 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
33610 incr = NULL;
33611 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
33613 /* If decl is an iterator, preserve the operator on decl
33614 until finish_omp_for. */
33615 if (real_decl
33616 && ((processing_template_decl
33617 && (TREE_TYPE (real_decl) == NULL_TREE
33618 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
33619 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
33620 incr = cp_parser_omp_for_incr (parser, real_decl);
33621 else
33622 incr = cp_parser_expression (parser);
33623 if (!EXPR_HAS_LOCATION (incr))
33624 protected_set_expr_location (incr, input_location);
33627 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33628 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33629 /*or_comma=*/false,
33630 /*consume_paren=*/true);
33632 TREE_VEC_ELT (declv, i) = decl;
33633 TREE_VEC_ELT (initv, i) = init;
33634 TREE_VEC_ELT (condv, i) = cond;
33635 TREE_VEC_ELT (incrv, i) = incr;
33636 if (orig_init)
33638 orig_inits.safe_grow_cleared (i + 1);
33639 orig_inits[i] = orig_init;
33642 if (i == count - 1)
33643 break;
33645 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
33646 in between the collapsed for loops to be still considered perfectly
33647 nested. Hopefully the final version clarifies this.
33648 For now handle (multiple) {'s and empty statements. */
33649 cp_parser_parse_tentatively (parser);
33652 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
33653 break;
33654 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33656 cp_lexer_consume_token (parser->lexer);
33657 bracecount++;
33659 else if (bracecount
33660 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33661 cp_lexer_consume_token (parser->lexer);
33662 else
33664 loc = cp_lexer_peek_token (parser->lexer)->location;
33665 error_at (loc, "not enough collapsed for loops");
33666 collapse_err = true;
33667 cp_parser_abort_tentative_parse (parser);
33668 declv = NULL_TREE;
33669 break;
33672 while (1);
33674 if (declv)
33676 cp_parser_parse_definitely (parser);
33677 nbraces += bracecount;
33681 /* Note that we saved the original contents of this flag when we entered
33682 the structured block, and so we don't need to re-save it here. */
33683 if (code == CILK_SIMD || code == CILK_FOR)
33684 parser->in_statement = IN_CILK_SIMD_FOR;
33685 else
33686 parser->in_statement = IN_OMP_FOR;
33688 /* Note that the grammar doesn't call for a structured block here,
33689 though the loop as a whole is a structured block. */
33690 body = push_stmt_list ();
33691 cp_parser_statement (parser, NULL_TREE, false, NULL);
33692 body = pop_stmt_list (body);
33694 if (declv == NULL_TREE)
33695 ret = NULL_TREE;
33696 else
33697 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
33698 body, pre_body, &orig_inits, clauses);
33700 while (nbraces)
33702 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33704 cp_lexer_consume_token (parser->lexer);
33705 nbraces--;
33707 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33708 cp_lexer_consume_token (parser->lexer);
33709 else
33711 if (!collapse_err)
33713 error_at (cp_lexer_peek_token (parser->lexer)->location,
33714 "collapsed loops not perfectly nested");
33716 collapse_err = true;
33717 cp_parser_statement_seq_opt (parser, NULL);
33718 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
33719 break;
33723 while (!for_block->is_empty ())
33724 add_stmt (pop_stmt_list (for_block->pop ()));
33725 release_tree_vector (for_block);
33727 return ret;
33730 /* Helper function for OpenMP parsing, split clauses and call
33731 finish_omp_clauses on each of the set of clauses afterwards. */
33733 static void
33734 cp_omp_split_clauses (location_t loc, enum tree_code code,
33735 omp_clause_mask mask, tree clauses, tree *cclauses)
33737 int i;
33738 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
33739 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
33740 if (cclauses[i])
33741 cclauses[i] = finish_omp_clauses (cclauses[i], true);
33744 /* OpenMP 4.0:
33745 #pragma omp simd simd-clause[optseq] new-line
33746 for-loop */
33748 #define OMP_SIMD_CLAUSE_MASK \
33749 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
33750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
33751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
33752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
33753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33758 static tree
33759 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
33760 char *p_name, omp_clause_mask mask, tree *cclauses)
33762 tree clauses, sb, ret;
33763 unsigned int save;
33764 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33766 strcat (p_name, " simd");
33767 mask |= OMP_SIMD_CLAUSE_MASK;
33769 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33770 cclauses == NULL);
33771 if (cclauses)
33773 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
33774 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
33775 tree c = find_omp_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
33776 OMP_CLAUSE_ORDERED);
33777 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
33779 error_at (OMP_CLAUSE_LOCATION (c),
33780 "%<ordered%> clause with parameter may not be specified "
33781 "on %qs construct", p_name);
33782 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
33786 sb = begin_omp_structured_block ();
33787 save = cp_parser_begin_omp_structured_block (parser);
33789 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
33791 cp_parser_end_omp_structured_block (parser, save);
33792 add_stmt (finish_omp_structured_block (sb));
33794 return ret;
33797 /* OpenMP 2.5:
33798 #pragma omp for for-clause[optseq] new-line
33799 for-loop
33801 OpenMP 4.0:
33802 #pragma omp for simd for-simd-clause[optseq] new-line
33803 for-loop */
33805 #define OMP_FOR_CLAUSE_MASK \
33806 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
33810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
33812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
33813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
33814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33816 static tree
33817 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
33818 char *p_name, omp_clause_mask mask, tree *cclauses)
33820 tree clauses, sb, ret;
33821 unsigned int save;
33822 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33824 strcat (p_name, " for");
33825 mask |= OMP_FOR_CLAUSE_MASK;
33826 if (cclauses)
33827 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
33828 /* Composite distribute parallel for{, simd} disallows ordered clause. */
33829 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
33830 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
33832 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33834 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33835 const char *p = IDENTIFIER_POINTER (id);
33837 if (strcmp (p, "simd") == 0)
33839 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
33840 if (cclauses == NULL)
33841 cclauses = cclauses_buf;
33843 cp_lexer_consume_token (parser->lexer);
33844 if (!flag_openmp) /* flag_openmp_simd */
33845 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33846 cclauses);
33847 sb = begin_omp_structured_block ();
33848 save = cp_parser_begin_omp_structured_block (parser);
33849 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33850 cclauses);
33851 cp_parser_end_omp_structured_block (parser, save);
33852 tree body = finish_omp_structured_block (sb);
33853 if (ret == NULL)
33854 return ret;
33855 ret = make_node (OMP_FOR);
33856 TREE_TYPE (ret) = void_type_node;
33857 OMP_FOR_BODY (ret) = body;
33858 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33859 SET_EXPR_LOCATION (ret, loc);
33860 add_stmt (ret);
33861 return ret;
33864 if (!flag_openmp) /* flag_openmp_simd */
33866 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33867 return NULL_TREE;
33870 /* Composite distribute parallel for disallows linear clause. */
33871 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
33872 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
33874 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33875 cclauses == NULL);
33876 if (cclauses)
33878 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
33879 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33882 sb = begin_omp_structured_block ();
33883 save = cp_parser_begin_omp_structured_block (parser);
33885 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
33887 cp_parser_end_omp_structured_block (parser, save);
33888 add_stmt (finish_omp_structured_block (sb));
33890 return ret;
33893 /* OpenMP 2.5:
33894 # pragma omp master new-line
33895 structured-block */
33897 static tree
33898 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
33900 cp_parser_require_pragma_eol (parser, pragma_tok);
33901 return c_finish_omp_master (input_location,
33902 cp_parser_omp_structured_block (parser));
33905 /* OpenMP 2.5:
33906 # pragma omp ordered new-line
33907 structured-block
33909 OpenMP 4.5:
33910 # pragma omp ordered ordered-clauses new-line
33911 structured-block */
33913 #define OMP_ORDERED_CLAUSE_MASK \
33914 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
33915 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
33917 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
33918 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
33920 static bool
33921 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
33922 enum pragma_context context)
33924 location_t loc = pragma_tok->location;
33926 if (context != pragma_stmt && context != pragma_compound)
33928 cp_parser_error (parser, "expected declaration specifiers");
33929 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33930 return false;
33933 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33935 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33936 const char *p = IDENTIFIER_POINTER (id);
33938 if (strcmp (p, "depend") == 0)
33940 if (context == pragma_stmt)
33942 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
33943 "%<depend%> clause may only be used in compound "
33944 "statements");
33945 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33946 return false;
33948 tree clauses
33949 = cp_parser_omp_all_clauses (parser,
33950 OMP_ORDERED_DEPEND_CLAUSE_MASK,
33951 "#pragma omp ordered", pragma_tok);
33952 c_finish_omp_ordered (loc, clauses, NULL_TREE);
33953 return false;
33957 tree clauses
33958 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
33959 "#pragma omp ordered", pragma_tok);
33960 c_finish_omp_ordered (loc, clauses,
33961 cp_parser_omp_structured_block (parser));
33962 return true;
33965 /* OpenMP 2.5:
33967 section-scope:
33968 { section-sequence }
33970 section-sequence:
33971 section-directive[opt] structured-block
33972 section-sequence section-directive structured-block */
33974 static tree
33975 cp_parser_omp_sections_scope (cp_parser *parser)
33977 tree stmt, substmt;
33978 bool error_suppress = false;
33979 cp_token *tok;
33981 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
33982 return NULL_TREE;
33984 stmt = push_stmt_list ();
33986 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
33987 != PRAGMA_OMP_SECTION)
33989 substmt = cp_parser_omp_structured_block (parser);
33990 substmt = build1 (OMP_SECTION, void_type_node, substmt);
33991 add_stmt (substmt);
33994 while (1)
33996 tok = cp_lexer_peek_token (parser->lexer);
33997 if (tok->type == CPP_CLOSE_BRACE)
33998 break;
33999 if (tok->type == CPP_EOF)
34000 break;
34002 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
34004 cp_lexer_consume_token (parser->lexer);
34005 cp_parser_require_pragma_eol (parser, tok);
34006 error_suppress = false;
34008 else if (!error_suppress)
34010 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
34011 error_suppress = true;
34014 substmt = cp_parser_omp_structured_block (parser);
34015 substmt = build1 (OMP_SECTION, void_type_node, substmt);
34016 add_stmt (substmt);
34018 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34020 substmt = pop_stmt_list (stmt);
34022 stmt = make_node (OMP_SECTIONS);
34023 TREE_TYPE (stmt) = void_type_node;
34024 OMP_SECTIONS_BODY (stmt) = substmt;
34026 add_stmt (stmt);
34027 return stmt;
34030 /* OpenMP 2.5:
34031 # pragma omp sections sections-clause[optseq] newline
34032 sections-scope */
34034 #define OMP_SECTIONS_CLAUSE_MASK \
34035 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34041 static tree
34042 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
34043 char *p_name, omp_clause_mask mask, tree *cclauses)
34045 tree clauses, ret;
34046 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34048 strcat (p_name, " sections");
34049 mask |= OMP_SECTIONS_CLAUSE_MASK;
34050 if (cclauses)
34051 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
34053 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34054 cclauses == NULL);
34055 if (cclauses)
34057 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
34058 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
34061 ret = cp_parser_omp_sections_scope (parser);
34062 if (ret)
34063 OMP_SECTIONS_CLAUSES (ret) = clauses;
34065 return ret;
34068 /* OpenMP 2.5:
34069 # pragma omp parallel parallel-clause[optseq] new-line
34070 structured-block
34071 # pragma omp parallel for parallel-for-clause[optseq] new-line
34072 structured-block
34073 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
34074 structured-block
34076 OpenMP 4.0:
34077 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
34078 structured-block */
34080 #define OMP_PARALLEL_CLAUSE_MASK \
34081 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34084 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
34085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
34087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
34089 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
34091 static tree
34092 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
34093 char *p_name, omp_clause_mask mask, tree *cclauses)
34095 tree stmt, clauses, block;
34096 unsigned int save;
34097 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34099 strcat (p_name, " parallel");
34100 mask |= OMP_PARALLEL_CLAUSE_MASK;
34101 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
34102 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
34103 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
34104 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
34106 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34108 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34109 if (cclauses == NULL)
34110 cclauses = cclauses_buf;
34112 cp_lexer_consume_token (parser->lexer);
34113 if (!flag_openmp) /* flag_openmp_simd */
34114 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
34115 block = begin_omp_parallel ();
34116 save = cp_parser_begin_omp_structured_block (parser);
34117 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
34118 cp_parser_end_omp_structured_block (parser, save);
34119 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
34120 block);
34121 if (ret == NULL_TREE)
34122 return ret;
34123 OMP_PARALLEL_COMBINED (stmt) = 1;
34124 return stmt;
34126 /* When combined with distribute, parallel has to be followed by for.
34127 #pragma omp target parallel is allowed though. */
34128 else if (cclauses
34129 && (mask & (OMP_CLAUSE_MASK_1
34130 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34132 error_at (loc, "expected %<for%> after %qs", p_name);
34133 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34134 return NULL_TREE;
34136 else if (!flag_openmp) /* flag_openmp_simd */
34138 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34139 return NULL_TREE;
34141 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34143 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34144 const char *p = IDENTIFIER_POINTER (id);
34145 if (strcmp (p, "sections") == 0)
34147 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34148 cclauses = cclauses_buf;
34150 cp_lexer_consume_token (parser->lexer);
34151 block = begin_omp_parallel ();
34152 save = cp_parser_begin_omp_structured_block (parser);
34153 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
34154 cp_parser_end_omp_structured_block (parser, save);
34155 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
34156 block);
34157 OMP_PARALLEL_COMBINED (stmt) = 1;
34158 return stmt;
34162 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
34163 if (cclauses)
34165 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
34166 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
34169 block = begin_omp_parallel ();
34170 save = cp_parser_begin_omp_structured_block (parser);
34171 cp_parser_statement (parser, NULL_TREE, false, NULL);
34172 cp_parser_end_omp_structured_block (parser, save);
34173 stmt = finish_omp_parallel (clauses, block);
34174 return stmt;
34177 /* OpenMP 2.5:
34178 # pragma omp single single-clause[optseq] new-line
34179 structured-block */
34181 #define OMP_SINGLE_CLAUSE_MASK \
34182 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34183 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34184 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
34185 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34187 static tree
34188 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
34190 tree stmt = make_node (OMP_SINGLE);
34191 TREE_TYPE (stmt) = void_type_node;
34193 OMP_SINGLE_CLAUSES (stmt)
34194 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
34195 "#pragma omp single", pragma_tok);
34196 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
34198 return add_stmt (stmt);
34201 /* OpenMP 3.0:
34202 # pragma omp task task-clause[optseq] new-line
34203 structured-block */
34205 #define OMP_TASK_CLAUSE_MASK \
34206 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34207 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
34208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
34209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34210 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34211 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34212 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
34213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
34214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
34217 static tree
34218 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
34220 tree clauses, block;
34221 unsigned int save;
34223 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
34224 "#pragma omp task", pragma_tok);
34225 block = begin_omp_task ();
34226 save = cp_parser_begin_omp_structured_block (parser);
34227 cp_parser_statement (parser, NULL_TREE, false, NULL);
34228 cp_parser_end_omp_structured_block (parser, save);
34229 return finish_omp_task (clauses, block);
34232 /* OpenMP 3.0:
34233 # pragma omp taskwait new-line */
34235 static void
34236 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
34238 cp_parser_require_pragma_eol (parser, pragma_tok);
34239 finish_omp_taskwait ();
34242 /* OpenMP 3.1:
34243 # pragma omp taskyield new-line */
34245 static void
34246 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
34248 cp_parser_require_pragma_eol (parser, pragma_tok);
34249 finish_omp_taskyield ();
34252 /* OpenMP 4.0:
34253 # pragma omp taskgroup new-line
34254 structured-block */
34256 static tree
34257 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
34259 cp_parser_require_pragma_eol (parser, pragma_tok);
34260 return c_finish_omp_taskgroup (input_location,
34261 cp_parser_omp_structured_block (parser));
34265 /* OpenMP 2.5:
34266 # pragma omp threadprivate (variable-list) */
34268 static void
34269 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
34271 tree vars;
34273 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34274 cp_parser_require_pragma_eol (parser, pragma_tok);
34276 finish_omp_threadprivate (vars);
34279 /* OpenMP 4.0:
34280 # pragma omp cancel cancel-clause[optseq] new-line */
34282 #define OMP_CANCEL_CLAUSE_MASK \
34283 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
34284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
34285 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
34286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
34287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
34289 static void
34290 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
34292 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
34293 "#pragma omp cancel", pragma_tok);
34294 finish_omp_cancel (clauses);
34297 /* OpenMP 4.0:
34298 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
34300 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
34301 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
34302 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
34303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
34304 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
34306 static void
34307 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
34309 tree clauses;
34310 bool point_seen = false;
34312 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34314 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34315 const char *p = IDENTIFIER_POINTER (id);
34317 if (strcmp (p, "point") == 0)
34319 cp_lexer_consume_token (parser->lexer);
34320 point_seen = true;
34323 if (!point_seen)
34325 cp_parser_error (parser, "expected %<point%>");
34326 cp_parser_require_pragma_eol (parser, pragma_tok);
34327 return;
34330 clauses = cp_parser_omp_all_clauses (parser,
34331 OMP_CANCELLATION_POINT_CLAUSE_MASK,
34332 "#pragma omp cancellation point",
34333 pragma_tok);
34334 finish_omp_cancellation_point (clauses);
34337 /* OpenMP 4.0:
34338 #pragma omp distribute distribute-clause[optseq] new-line
34339 for-loop */
34341 #define OMP_DISTRIBUTE_CLAUSE_MASK \
34342 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
34346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34348 static tree
34349 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
34350 char *p_name, omp_clause_mask mask, tree *cclauses)
34352 tree clauses, sb, ret;
34353 unsigned int save;
34354 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34356 strcat (p_name, " distribute");
34357 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
34359 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34361 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34362 const char *p = IDENTIFIER_POINTER (id);
34363 bool simd = false;
34364 bool parallel = false;
34366 if (strcmp (p, "simd") == 0)
34367 simd = true;
34368 else
34369 parallel = strcmp (p, "parallel") == 0;
34370 if (parallel || simd)
34372 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34373 if (cclauses == NULL)
34374 cclauses = cclauses_buf;
34375 cp_lexer_consume_token (parser->lexer);
34376 if (!flag_openmp) /* flag_openmp_simd */
34378 if (simd)
34379 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34380 cclauses);
34381 else
34382 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
34383 cclauses);
34385 sb = begin_omp_structured_block ();
34386 save = cp_parser_begin_omp_structured_block (parser);
34387 if (simd)
34388 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34389 cclauses);
34390 else
34391 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
34392 cclauses);
34393 cp_parser_end_omp_structured_block (parser, save);
34394 tree body = finish_omp_structured_block (sb);
34395 if (ret == NULL)
34396 return ret;
34397 ret = make_node (OMP_DISTRIBUTE);
34398 TREE_TYPE (ret) = void_type_node;
34399 OMP_FOR_BODY (ret) = body;
34400 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
34401 SET_EXPR_LOCATION (ret, loc);
34402 add_stmt (ret);
34403 return ret;
34406 if (!flag_openmp) /* flag_openmp_simd */
34408 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34409 return NULL_TREE;
34412 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34413 cclauses == NULL);
34414 if (cclauses)
34416 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
34417 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
34420 sb = begin_omp_structured_block ();
34421 save = cp_parser_begin_omp_structured_block (parser);
34423 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
34425 cp_parser_end_omp_structured_block (parser, save);
34426 add_stmt (finish_omp_structured_block (sb));
34428 return ret;
34431 /* OpenMP 4.0:
34432 # pragma omp teams teams-clause[optseq] new-line
34433 structured-block */
34435 #define OMP_TEAMS_CLAUSE_MASK \
34436 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34437 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34438 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34439 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
34441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
34442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
34444 static tree
34445 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
34446 char *p_name, omp_clause_mask mask, tree *cclauses)
34448 tree clauses, sb, ret;
34449 unsigned int save;
34450 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34452 strcat (p_name, " teams");
34453 mask |= OMP_TEAMS_CLAUSE_MASK;
34455 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34457 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34458 const char *p = IDENTIFIER_POINTER (id);
34459 if (strcmp (p, "distribute") == 0)
34461 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34462 if (cclauses == NULL)
34463 cclauses = cclauses_buf;
34465 cp_lexer_consume_token (parser->lexer);
34466 if (!flag_openmp) /* flag_openmp_simd */
34467 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
34468 cclauses);
34469 sb = begin_omp_structured_block ();
34470 save = cp_parser_begin_omp_structured_block (parser);
34471 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
34472 cclauses);
34473 cp_parser_end_omp_structured_block (parser, save);
34474 tree body = finish_omp_structured_block (sb);
34475 if (ret == NULL)
34476 return ret;
34477 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34478 ret = make_node (OMP_TEAMS);
34479 TREE_TYPE (ret) = void_type_node;
34480 OMP_TEAMS_CLAUSES (ret) = clauses;
34481 OMP_TEAMS_BODY (ret) = body;
34482 OMP_TEAMS_COMBINED (ret) = 1;
34483 return add_stmt (ret);
34486 if (!flag_openmp) /* flag_openmp_simd */
34488 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34489 return NULL_TREE;
34492 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34493 cclauses == NULL);
34494 if (cclauses)
34496 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
34497 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34500 tree stmt = make_node (OMP_TEAMS);
34501 TREE_TYPE (stmt) = void_type_node;
34502 OMP_TEAMS_CLAUSES (stmt) = clauses;
34503 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
34505 return add_stmt (stmt);
34508 /* OpenMP 4.0:
34509 # pragma omp target data target-data-clause[optseq] new-line
34510 structured-block */
34512 #define OMP_TARGET_DATA_CLAUSE_MASK \
34513 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34514 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34515 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
34518 static tree
34519 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
34521 tree clauses
34522 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
34523 "#pragma omp target data", pragma_tok);
34524 int map_seen = 0;
34525 for (tree *pc = &clauses; *pc;)
34527 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34528 switch (OMP_CLAUSE_MAP_KIND (*pc))
34530 case GOMP_MAP_TO:
34531 case GOMP_MAP_ALWAYS_TO:
34532 case GOMP_MAP_FROM:
34533 case GOMP_MAP_ALWAYS_FROM:
34534 case GOMP_MAP_TOFROM:
34535 case GOMP_MAP_ALWAYS_TOFROM:
34536 case GOMP_MAP_ALLOC:
34537 map_seen = 3;
34538 break;
34539 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34540 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34541 case GOMP_MAP_ALWAYS_POINTER:
34542 break;
34543 default:
34544 map_seen |= 1;
34545 error_at (OMP_CLAUSE_LOCATION (*pc),
34546 "%<#pragma omp target data%> with map-type other "
34547 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
34548 "on %<map%> clause");
34549 *pc = OMP_CLAUSE_CHAIN (*pc);
34550 continue;
34552 pc = &OMP_CLAUSE_CHAIN (*pc);
34555 if (map_seen != 3)
34557 if (map_seen == 0)
34558 error_at (pragma_tok->location,
34559 "%<#pragma omp target data%> must contain at least "
34560 "one %<map%> clause");
34561 return NULL_TREE;
34564 tree stmt = make_node (OMP_TARGET_DATA);
34565 TREE_TYPE (stmt) = void_type_node;
34566 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
34568 keep_next_level (true);
34569 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
34571 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34572 return add_stmt (stmt);
34575 /* OpenMP 4.5:
34576 # pragma omp target enter data target-enter-data-clause[optseq] new-line
34577 structured-block */
34579 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
34580 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34581 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34582 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34586 static tree
34587 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
34588 enum pragma_context context)
34590 bool data_seen = false;
34591 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34593 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34594 const char *p = IDENTIFIER_POINTER (id);
34596 if (strcmp (p, "data") == 0)
34598 cp_lexer_consume_token (parser->lexer);
34599 data_seen = true;
34602 if (!data_seen)
34604 cp_parser_error (parser, "expected %<data%>");
34605 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34606 return NULL_TREE;
34609 if (context == pragma_stmt)
34611 error_at (pragma_tok->location,
34612 "%<#pragma omp target enter data%> may only be "
34613 "used in compound statements");
34614 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34615 return NULL_TREE;
34618 tree clauses
34619 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
34620 "#pragma omp target enter data", pragma_tok);
34621 int map_seen = 0;
34622 for (tree *pc = &clauses; *pc;)
34624 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34625 switch (OMP_CLAUSE_MAP_KIND (*pc))
34627 case GOMP_MAP_TO:
34628 case GOMP_MAP_ALWAYS_TO:
34629 case GOMP_MAP_ALLOC:
34630 map_seen = 3;
34631 break;
34632 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34633 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34634 case GOMP_MAP_ALWAYS_POINTER:
34635 break;
34636 default:
34637 map_seen |= 1;
34638 error_at (OMP_CLAUSE_LOCATION (*pc),
34639 "%<#pragma omp target enter data%> with map-type other "
34640 "than %<to%> or %<alloc%> on %<map%> clause");
34641 *pc = OMP_CLAUSE_CHAIN (*pc);
34642 continue;
34644 pc = &OMP_CLAUSE_CHAIN (*pc);
34647 if (map_seen != 3)
34649 if (map_seen == 0)
34650 error_at (pragma_tok->location,
34651 "%<#pragma omp target enter data%> must contain at least "
34652 "one %<map%> clause");
34653 return NULL_TREE;
34656 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
34657 TREE_TYPE (stmt) = void_type_node;
34658 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
34659 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34660 return add_stmt (stmt);
34663 /* OpenMP 4.5:
34664 # pragma omp target exit data target-enter-data-clause[optseq] new-line
34665 structured-block */
34667 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
34668 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34671 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34672 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34674 static tree
34675 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
34676 enum pragma_context context)
34678 bool data_seen = false;
34679 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34681 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34682 const char *p = IDENTIFIER_POINTER (id);
34684 if (strcmp (p, "data") == 0)
34686 cp_lexer_consume_token (parser->lexer);
34687 data_seen = true;
34690 if (!data_seen)
34692 cp_parser_error (parser, "expected %<data%>");
34693 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34694 return NULL_TREE;
34697 if (context == pragma_stmt)
34699 error_at (pragma_tok->location,
34700 "%<#pragma omp target exit data%> may only be "
34701 "used in compound statements");
34702 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34703 return NULL_TREE;
34706 tree clauses
34707 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
34708 "#pragma omp target exit data", pragma_tok);
34709 int map_seen = 0;
34710 for (tree *pc = &clauses; *pc;)
34712 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34713 switch (OMP_CLAUSE_MAP_KIND (*pc))
34715 case GOMP_MAP_FROM:
34716 case GOMP_MAP_ALWAYS_FROM:
34717 case GOMP_MAP_RELEASE:
34718 case GOMP_MAP_DELETE:
34719 map_seen = 3;
34720 break;
34721 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34722 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34723 case GOMP_MAP_ALWAYS_POINTER:
34724 break;
34725 default:
34726 map_seen |= 1;
34727 error_at (OMP_CLAUSE_LOCATION (*pc),
34728 "%<#pragma omp target exit data%> with map-type other "
34729 "than %<from%>, %<release%> or %<delete%> on %<map%>"
34730 " clause");
34731 *pc = OMP_CLAUSE_CHAIN (*pc);
34732 continue;
34734 pc = &OMP_CLAUSE_CHAIN (*pc);
34737 if (map_seen != 3)
34739 if (map_seen == 0)
34740 error_at (pragma_tok->location,
34741 "%<#pragma omp target exit data%> must contain at least "
34742 "one %<map%> clause");
34743 return NULL_TREE;
34746 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
34747 TREE_TYPE (stmt) = void_type_node;
34748 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
34749 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34750 return add_stmt (stmt);
34753 /* OpenMP 4.0:
34754 # pragma omp target update target-update-clause[optseq] new-line */
34756 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
34757 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
34758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
34759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34760 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34764 static bool
34765 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
34766 enum pragma_context context)
34768 if (context == pragma_stmt)
34770 error_at (pragma_tok->location,
34771 "%<#pragma omp target update%> may only be "
34772 "used in compound statements");
34773 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34774 return false;
34777 tree clauses
34778 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
34779 "#pragma omp target update", pragma_tok);
34780 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
34781 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
34783 error_at (pragma_tok->location,
34784 "%<#pragma omp target update%> must contain at least one "
34785 "%<from%> or %<to%> clauses");
34786 return false;
34789 tree stmt = make_node (OMP_TARGET_UPDATE);
34790 TREE_TYPE (stmt) = void_type_node;
34791 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
34792 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34793 add_stmt (stmt);
34794 return false;
34797 /* OpenMP 4.0:
34798 # pragma omp target target-clause[optseq] new-line
34799 structured-block */
34801 #define OMP_TARGET_CLAUSE_MASK \
34802 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
34807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
34810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
34812 static bool
34813 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
34814 enum pragma_context context)
34816 tree *pc = NULL, stmt;
34818 if (context != pragma_stmt && context != pragma_compound)
34820 cp_parser_error (parser, "expected declaration specifiers");
34821 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34822 return false;
34825 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34827 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34828 const char *p = IDENTIFIER_POINTER (id);
34829 enum tree_code ccode = ERROR_MARK;
34831 if (strcmp (p, "teams") == 0)
34832 ccode = OMP_TEAMS;
34833 else if (strcmp (p, "parallel") == 0)
34834 ccode = OMP_PARALLEL;
34835 else if (strcmp (p, "simd") == 0)
34836 ccode = OMP_SIMD;
34837 if (ccode != ERROR_MARK)
34839 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
34840 char p_name[sizeof ("#pragma omp target teams distribute "
34841 "parallel for simd")];
34843 cp_lexer_consume_token (parser->lexer);
34844 strcpy (p_name, "#pragma omp target");
34845 if (!flag_openmp) /* flag_openmp_simd */
34847 tree stmt;
34848 switch (ccode)
34850 case OMP_TEAMS:
34851 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
34852 OMP_TARGET_CLAUSE_MASK,
34853 cclauses);
34854 break;
34855 case OMP_PARALLEL:
34856 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
34857 OMP_TARGET_CLAUSE_MASK,
34858 cclauses);
34859 break;
34860 case OMP_SIMD:
34861 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
34862 OMP_TARGET_CLAUSE_MASK,
34863 cclauses);
34864 break;
34865 default:
34866 gcc_unreachable ();
34868 return stmt != NULL_TREE;
34870 keep_next_level (true);
34871 tree sb = begin_omp_structured_block (), ret;
34872 unsigned save = cp_parser_begin_omp_structured_block (parser);
34873 switch (ccode)
34875 case OMP_TEAMS:
34876 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
34877 OMP_TARGET_CLAUSE_MASK, cclauses);
34878 break;
34879 case OMP_PARALLEL:
34880 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
34881 OMP_TARGET_CLAUSE_MASK, cclauses);
34882 break;
34883 case OMP_SIMD:
34884 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
34885 OMP_TARGET_CLAUSE_MASK, cclauses);
34886 break;
34887 default:
34888 gcc_unreachable ();
34890 cp_parser_end_omp_structured_block (parser, save);
34891 tree body = finish_omp_structured_block (sb);
34892 if (ret == NULL_TREE)
34893 return false;
34894 if (ccode == OMP_TEAMS && !processing_template_decl)
34896 /* For combined target teams, ensure the num_teams and
34897 thread_limit clause expressions are evaluated on the host,
34898 before entering the target construct. */
34899 tree c;
34900 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34901 c; c = OMP_CLAUSE_CHAIN (c))
34902 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
34903 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
34904 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
34906 tree expr = OMP_CLAUSE_OPERAND (c, 0);
34907 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
34908 if (expr == error_mark_node)
34909 continue;
34910 tree tmp = TARGET_EXPR_SLOT (expr);
34911 add_stmt (expr);
34912 OMP_CLAUSE_OPERAND (c, 0) = expr;
34913 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
34914 OMP_CLAUSE_FIRSTPRIVATE);
34915 OMP_CLAUSE_DECL (tc) = tmp;
34916 OMP_CLAUSE_CHAIN (tc)
34917 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
34918 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
34921 tree stmt = make_node (OMP_TARGET);
34922 TREE_TYPE (stmt) = void_type_node;
34923 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
34924 OMP_TARGET_BODY (stmt) = body;
34925 OMP_TARGET_COMBINED (stmt) = 1;
34926 add_stmt (stmt);
34927 pc = &OMP_TARGET_CLAUSES (stmt);
34928 goto check_clauses;
34930 else if (!flag_openmp) /* flag_openmp_simd */
34932 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34933 return false;
34935 else if (strcmp (p, "data") == 0)
34937 cp_lexer_consume_token (parser->lexer);
34938 cp_parser_omp_target_data (parser, pragma_tok);
34939 return true;
34941 else if (strcmp (p, "enter") == 0)
34943 cp_lexer_consume_token (parser->lexer);
34944 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
34945 return false;
34947 else if (strcmp (p, "exit") == 0)
34949 cp_lexer_consume_token (parser->lexer);
34950 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
34951 return false;
34953 else if (strcmp (p, "update") == 0)
34955 cp_lexer_consume_token (parser->lexer);
34956 return cp_parser_omp_target_update (parser, pragma_tok, context);
34960 stmt = make_node (OMP_TARGET);
34961 TREE_TYPE (stmt) = void_type_node;
34963 OMP_TARGET_CLAUSES (stmt)
34964 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
34965 "#pragma omp target", pragma_tok);
34966 pc = &OMP_TARGET_CLAUSES (stmt);
34967 keep_next_level (true);
34968 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
34970 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34971 add_stmt (stmt);
34973 check_clauses:
34974 while (*pc)
34976 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34977 switch (OMP_CLAUSE_MAP_KIND (*pc))
34979 case GOMP_MAP_TO:
34980 case GOMP_MAP_ALWAYS_TO:
34981 case GOMP_MAP_FROM:
34982 case GOMP_MAP_ALWAYS_FROM:
34983 case GOMP_MAP_TOFROM:
34984 case GOMP_MAP_ALWAYS_TOFROM:
34985 case GOMP_MAP_ALLOC:
34986 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34987 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34988 case GOMP_MAP_ALWAYS_POINTER:
34989 break;
34990 default:
34991 error_at (OMP_CLAUSE_LOCATION (*pc),
34992 "%<#pragma omp target%> with map-type other "
34993 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
34994 "on %<map%> clause");
34995 *pc = OMP_CLAUSE_CHAIN (*pc);
34996 continue;
34998 pc = &OMP_CLAUSE_CHAIN (*pc);
35000 return true;
35003 /* OpenACC 2.0:
35004 # pragma acc cache (variable-list) new-line
35007 static tree
35008 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
35010 tree stmt, clauses;
35012 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
35013 clauses = finish_omp_clauses (clauses, false);
35015 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
35017 stmt = make_node (OACC_CACHE);
35018 TREE_TYPE (stmt) = void_type_node;
35019 OACC_CACHE_CLAUSES (stmt) = clauses;
35020 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35021 add_stmt (stmt);
35023 return stmt;
35026 /* OpenACC 2.0:
35027 # pragma acc data oacc-data-clause[optseq] new-line
35028 structured-block */
35030 #define OACC_DATA_CLAUSE_MASK \
35031 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35032 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35033 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35034 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
35043 static tree
35044 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok)
35046 tree stmt, clauses, block;
35047 unsigned int save;
35049 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
35050 "#pragma acc data", pragma_tok);
35052 block = begin_omp_parallel ();
35053 save = cp_parser_begin_omp_structured_block (parser);
35054 cp_parser_statement (parser, NULL_TREE, false, NULL);
35055 cp_parser_end_omp_structured_block (parser, save);
35056 stmt = finish_oacc_data (clauses, block);
35057 return stmt;
35060 /* OpenACC 2.0:
35061 # pragma acc host_data <clauses> new-line
35062 structured-block */
35064 #define OACC_HOST_DATA_CLAUSE_MASK \
35065 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
35067 static tree
35068 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok)
35070 tree stmt, clauses, block;
35071 unsigned int save;
35073 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
35074 "#pragma acc host_data", pragma_tok);
35076 block = begin_omp_parallel ();
35077 save = cp_parser_begin_omp_structured_block (parser);
35078 cp_parser_statement (parser, NULL_TREE, false, NULL);
35079 cp_parser_end_omp_structured_block (parser, save);
35080 stmt = finish_oacc_host_data (clauses, block);
35081 return stmt;
35084 /* OpenACC 2.0:
35085 # pragma acc declare oacc-data-clause[optseq] new-line
35088 #define OACC_DECLARE_CLAUSE_MASK \
35089 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35094 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
35095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
35096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
35102 static tree
35103 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
35105 tree clauses, stmt;
35106 bool error = false;
35108 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
35109 "#pragma acc declare", pragma_tok, true);
35112 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35114 error_at (pragma_tok->location,
35115 "no valid clauses specified in %<#pragma acc declare%>");
35116 return NULL_TREE;
35119 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
35121 location_t loc = OMP_CLAUSE_LOCATION (t);
35122 tree decl = OMP_CLAUSE_DECL (t);
35123 if (!DECL_P (decl))
35125 error_at (loc, "array section in %<#pragma acc declare%>");
35126 error = true;
35127 continue;
35129 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
35130 switch (OMP_CLAUSE_MAP_KIND (t))
35132 case GOMP_MAP_FORCE_ALLOC:
35133 case GOMP_MAP_FORCE_TO:
35134 case GOMP_MAP_FORCE_DEVICEPTR:
35135 case GOMP_MAP_DEVICE_RESIDENT:
35136 break;
35138 case GOMP_MAP_POINTER:
35139 /* Generated by c_finish_omp_clauses from array sections;
35140 avoid spurious diagnostics. */
35141 break;
35143 case GOMP_MAP_LINK:
35144 if (!global_bindings_p ()
35145 && (TREE_STATIC (decl)
35146 || !DECL_EXTERNAL (decl)))
35148 error_at (loc,
35149 "%qD must be a global variable in"
35150 "%<#pragma acc declare link%>",
35151 decl);
35152 error = true;
35153 continue;
35155 break;
35157 default:
35158 if (global_bindings_p ())
35160 error_at (loc, "invalid OpenACC clause at file scope");
35161 error = true;
35162 continue;
35164 if (DECL_EXTERNAL (decl))
35166 error_at (loc,
35167 "invalid use of %<extern%> variable %qD "
35168 "in %<#pragma acc declare%>", decl);
35169 error = true;
35170 continue;
35172 else if (TREE_PUBLIC (decl))
35174 error_at (loc,
35175 "invalid use of %<global%> variable %qD "
35176 "in %<#pragma acc declare%>", decl);
35177 error = true;
35178 continue;
35180 break;
35183 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
35184 || lookup_attribute ("omp declare target link",
35185 DECL_ATTRIBUTES (decl)))
35187 error_at (loc, "variable %qD used more than once with "
35188 "%<#pragma acc declare%>", decl);
35189 error = true;
35190 continue;
35193 if (!error)
35195 tree id;
35197 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
35198 id = get_identifier ("omp declare target link");
35199 else
35200 id = get_identifier ("omp declare target");
35202 DECL_ATTRIBUTES (decl)
35203 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
35204 if (global_bindings_p ())
35206 symtab_node *node = symtab_node::get (decl);
35207 if (node != NULL)
35209 node->offloadable = 1;
35210 if (ENABLE_OFFLOADING)
35212 g->have_offload = true;
35213 if (is_a <varpool_node *> (node))
35214 vec_safe_push (offload_vars, decl);
35221 if (error || global_bindings_p ())
35222 return NULL_TREE;
35224 stmt = make_node (OACC_DECLARE);
35225 TREE_TYPE (stmt) = void_type_node;
35226 OACC_DECLARE_CLAUSES (stmt) = clauses;
35227 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35229 add_stmt (stmt);
35231 return NULL_TREE;
35234 /* OpenACC 2.0:
35235 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
35239 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
35241 LOC is the location of the #pragma token.
35244 #define OACC_ENTER_DATA_CLAUSE_MASK \
35245 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35246 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35247 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35248 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35249 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35250 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35251 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35253 #define OACC_EXIT_DATA_CLAUSE_MASK \
35254 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35255 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35256 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35257 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
35258 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35260 static tree
35261 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
35262 bool enter)
35264 tree stmt, clauses;
35266 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
35267 || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
35269 cp_parser_error (parser, enter
35270 ? "expected %<data%> in %<#pragma acc enter data%>"
35271 : "expected %<data%> in %<#pragma acc exit data%>");
35272 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35273 return NULL_TREE;
35276 const char *p =
35277 IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
35278 if (strcmp (p, "data") != 0)
35280 cp_parser_error (parser, "invalid pragma");
35281 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35282 return NULL_TREE;
35285 cp_lexer_consume_token (parser->lexer);
35287 if (enter)
35288 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
35289 "#pragma acc enter data", pragma_tok);
35290 else
35291 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
35292 "#pragma acc exit data", pragma_tok);
35294 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35296 error_at (pragma_tok->location,
35297 "%<#pragma acc enter data%> has no data movement clause");
35298 return NULL_TREE;
35301 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
35302 TREE_TYPE (stmt) = void_type_node;
35303 OMP_STANDALONE_CLAUSES (stmt) = clauses;
35304 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35305 add_stmt (stmt);
35306 return stmt;
35309 /* OpenACC 2.0:
35310 # pragma acc loop oacc-loop-clause[optseq] new-line
35311 structured-block */
35313 #define OACC_LOOP_CLAUSE_MASK \
35314 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
35315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
35316 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
35317 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
35318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
35319 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
35320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
35321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
35322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
35323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
35325 static tree
35326 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
35327 omp_clause_mask mask, tree *cclauses)
35329 strcat (p_name, " loop");
35330 mask |= OACC_LOOP_CLAUSE_MASK;
35332 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
35333 cclauses == NULL);
35334 if (cclauses)
35336 clauses = c_oacc_split_loop_clauses (clauses, cclauses);
35337 if (*cclauses)
35338 finish_omp_clauses (*cclauses, false);
35339 if (clauses)
35340 finish_omp_clauses (clauses, false);
35343 tree block = begin_omp_structured_block ();
35344 int save = cp_parser_begin_omp_structured_block (parser);
35345 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL);
35346 cp_parser_end_omp_structured_block (parser, save);
35347 add_stmt (finish_omp_structured_block (block));
35349 return stmt;
35352 /* OpenACC 2.0:
35353 # pragma acc kernels oacc-kernels-clause[optseq] new-line
35354 structured-block
35358 # pragma acc parallel oacc-parallel-clause[optseq] new-line
35359 structured-block
35362 #define OACC_KERNELS_CLAUSE_MASK \
35363 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35364 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35365 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35368 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
35369 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35370 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35371 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35372 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35373 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35374 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35375 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35376 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35378 #define OACC_PARALLEL_CLAUSE_MASK \
35379 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35382 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35383 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
35385 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35386 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
35387 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
35389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
35390 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
35396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
35397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
35398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35400 static tree
35401 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
35402 char *p_name)
35404 omp_clause_mask mask;
35405 enum tree_code code;
35406 switch (cp_parser_pragma_kind (pragma_tok))
35408 case PRAGMA_OACC_KERNELS:
35409 strcat (p_name, " kernels");
35410 mask = OACC_KERNELS_CLAUSE_MASK;
35411 code = OACC_KERNELS;
35412 break;
35413 case PRAGMA_OACC_PARALLEL:
35414 strcat (p_name, " parallel");
35415 mask = OACC_PARALLEL_CLAUSE_MASK;
35416 code = OACC_PARALLEL;
35417 break;
35418 default:
35419 gcc_unreachable ();
35422 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35424 const char *p
35425 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
35426 if (strcmp (p, "loop") == 0)
35428 cp_lexer_consume_token (parser->lexer);
35429 mask |= OACC_LOOP_CLAUSE_MASK;
35431 tree block = begin_omp_parallel ();
35432 tree clauses;
35433 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses);
35434 return finish_omp_construct (code, block, clauses);
35438 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
35440 tree block = begin_omp_parallel ();
35441 unsigned int save = cp_parser_begin_omp_structured_block (parser);
35442 cp_parser_statement (parser, NULL_TREE, false, NULL);
35443 cp_parser_end_omp_structured_block (parser, save);
35444 return finish_omp_construct (code, block, clauses);
35447 /* OpenACC 2.0:
35448 # pragma acc update oacc-update-clause[optseq] new-line
35451 #define OACC_UPDATE_CLAUSE_MASK \
35452 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35453 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
35454 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
35455 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
35457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
35459 static tree
35460 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
35462 tree stmt, clauses;
35464 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
35465 "#pragma acc update", pragma_tok);
35467 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35469 error_at (pragma_tok->location,
35470 "%<#pragma acc update%> must contain at least one "
35471 "%<device%> or %<host%> or %<self%> clause");
35472 return NULL_TREE;
35475 stmt = make_node (OACC_UPDATE);
35476 TREE_TYPE (stmt) = void_type_node;
35477 OACC_UPDATE_CLAUSES (stmt) = clauses;
35478 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35479 add_stmt (stmt);
35480 return stmt;
35483 /* OpenACC 2.0:
35484 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
35486 LOC is the location of the #pragma token.
35489 #define OACC_WAIT_CLAUSE_MASK \
35490 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
35492 static tree
35493 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
35495 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
35496 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35498 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
35499 list = cp_parser_oacc_wait_list (parser, loc, list);
35501 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
35502 "#pragma acc wait", pragma_tok);
35504 stmt = c_finish_oacc_wait (loc, list, clauses);
35505 stmt = finish_expr_stmt (stmt);
35507 return stmt;
35510 /* OpenMP 4.0:
35511 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
35513 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
35514 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35515 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35517 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
35518 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
35519 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
35521 static void
35522 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
35523 enum pragma_context context)
35525 bool first_p = parser->omp_declare_simd == NULL;
35526 cp_omp_declare_simd_data data;
35527 if (first_p)
35529 data.error_seen = false;
35530 data.fndecl_seen = false;
35531 data.tokens = vNULL;
35532 parser->omp_declare_simd = &data;
35534 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
35535 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
35536 cp_lexer_consume_token (parser->lexer);
35537 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35538 parser->omp_declare_simd->error_seen = true;
35539 cp_parser_require_pragma_eol (parser, pragma_tok);
35540 struct cp_token_cache *cp
35541 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
35542 parser->omp_declare_simd->tokens.safe_push (cp);
35543 if (first_p)
35545 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
35546 cp_parser_pragma (parser, context);
35547 switch (context)
35549 case pragma_external:
35550 cp_parser_declaration (parser);
35551 break;
35552 case pragma_member:
35553 cp_parser_member_declaration (parser);
35554 break;
35555 case pragma_objc_icode:
35556 cp_parser_block_declaration (parser, /*statement_p=*/false);
35557 break;
35558 default:
35559 cp_parser_declaration_statement (parser);
35560 break;
35562 if (parser->omp_declare_simd
35563 && !parser->omp_declare_simd->error_seen
35564 && !parser->omp_declare_simd->fndecl_seen)
35565 error_at (pragma_tok->location,
35566 "%<#pragma omp declare simd%> not immediately followed by "
35567 "function declaration or definition");
35568 data.tokens.release ();
35569 parser->omp_declare_simd = NULL;
35573 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
35574 This function is modelled similar to the late parsing of omp declare
35575 simd. */
35577 static tree
35578 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
35580 struct cp_token_cache *ce;
35581 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
35582 int ii = 0;
35584 if (parser->omp_declare_simd != NULL
35585 || lookup_attribute ("simd", attrs))
35587 error ("%<#pragma omp declare simd%> of %<simd%> attribute cannot be "
35588 "used in the same function marked as a Cilk Plus SIMD-enabled "
35589 " function");
35590 parser->cilk_simd_fn_info->tokens.release ();
35591 XDELETE (parser->cilk_simd_fn_info);
35592 parser->cilk_simd_fn_info = NULL;
35593 return attrs;
35595 if (!info->error_seen && info->fndecl_seen)
35597 error ("vector attribute not immediately followed by a single function"
35598 " declaration or definition");
35599 info->error_seen = true;
35601 if (info->error_seen)
35602 return attrs;
35604 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
35606 tree c, cl;
35608 cp_parser_push_lexer_for_tokens (parser, ce);
35609 parser->lexer->in_pragma = true;
35610 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
35611 "SIMD-enabled functions attribute",
35612 NULL);
35613 cp_parser_pop_lexer (parser);
35614 if (cl)
35615 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
35617 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
35618 TREE_CHAIN (c) = attrs;
35619 attrs = c;
35621 c = build_tree_list (get_identifier ("omp declare simd"), cl);
35622 TREE_CHAIN (c) = attrs;
35623 if (processing_template_decl)
35624 ATTR_IS_DEPENDENT (c) = 1;
35625 attrs = c;
35627 info->fndecl_seen = true;
35628 parser->cilk_simd_fn_info->tokens.release ();
35629 XDELETE (parser->cilk_simd_fn_info);
35630 parser->cilk_simd_fn_info = NULL;
35631 return attrs;
35634 /* Finalize #pragma omp declare simd clauses after direct declarator has
35635 been parsed, and put that into "omp declare simd" attribute. */
35637 static tree
35638 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
35640 struct cp_token_cache *ce;
35641 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
35642 int i;
35644 if (!data->error_seen && data->fndecl_seen)
35646 error ("%<#pragma omp declare simd%> not immediately followed by "
35647 "a single function declaration or definition");
35648 data->error_seen = true;
35649 return attrs;
35651 if (data->error_seen)
35652 return attrs;
35654 FOR_EACH_VEC_ELT (data->tokens, i, ce)
35656 tree c, cl;
35658 cp_parser_push_lexer_for_tokens (parser, ce);
35659 parser->lexer->in_pragma = true;
35660 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
35661 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
35662 cp_lexer_consume_token (parser->lexer);
35663 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
35664 "#pragma omp declare simd", pragma_tok);
35665 cp_parser_pop_lexer (parser);
35666 if (cl)
35667 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
35668 c = build_tree_list (get_identifier ("omp declare simd"), cl);
35669 TREE_CHAIN (c) = attrs;
35670 if (processing_template_decl)
35671 ATTR_IS_DEPENDENT (c) = 1;
35672 attrs = c;
35675 data->fndecl_seen = true;
35676 return attrs;
35680 /* OpenMP 4.0:
35681 # pragma omp declare target new-line
35682 declarations and definitions
35683 # pragma omp end declare target new-line
35685 OpenMP 4.5:
35686 # pragma omp declare target ( extended-list ) new-line
35688 # pragma omp declare target declare-target-clauses[seq] new-line */
35690 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
35691 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
35692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
35694 static void
35695 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
35697 tree clauses = NULL_TREE;
35698 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35699 clauses
35700 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
35701 "#pragma omp declare target", pragma_tok);
35702 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35704 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
35705 clauses);
35706 clauses = finish_omp_clauses (clauses, true);
35707 cp_parser_require_pragma_eol (parser, pragma_tok);
35709 else
35711 cp_parser_require_pragma_eol (parser, pragma_tok);
35712 scope_chain->omp_declare_target_attribute++;
35713 return;
35715 if (scope_chain->omp_declare_target_attribute)
35716 error_at (pragma_tok->location,
35717 "%<#pragma omp declare target%> with clauses in between "
35718 "%<#pragma omp declare target%> without clauses and "
35719 "%<#pragma omp end declare target%>");
35720 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
35722 tree t = OMP_CLAUSE_DECL (c), id;
35723 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
35724 tree at2 = lookup_attribute ("omp declare target link",
35725 DECL_ATTRIBUTES (t));
35726 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
35728 id = get_identifier ("omp declare target link");
35729 std::swap (at1, at2);
35731 else
35732 id = get_identifier ("omp declare target");
35733 if (at2)
35735 error_at (OMP_CLAUSE_LOCATION (c),
35736 "%qD specified both in declare target %<link%> and %<to%>"
35737 " clauses", t);
35738 continue;
35740 if (!at1)
35742 symtab_node *node = symtab_node::get (t);
35743 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
35744 if (node != NULL)
35746 node->offloadable = 1;
35747 if (ENABLE_OFFLOADING)
35749 g->have_offload = true;
35750 if (is_a <varpool_node *> (node))
35751 vec_safe_push (offload_vars, t);
35758 static void
35759 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
35761 const char *p = "";
35762 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35764 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35765 p = IDENTIFIER_POINTER (id);
35767 if (strcmp (p, "declare") == 0)
35769 cp_lexer_consume_token (parser->lexer);
35770 p = "";
35771 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35773 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35774 p = IDENTIFIER_POINTER (id);
35776 if (strcmp (p, "target") == 0)
35777 cp_lexer_consume_token (parser->lexer);
35778 else
35780 cp_parser_error (parser, "expected %<target%>");
35781 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35782 return;
35785 else
35787 cp_parser_error (parser, "expected %<declare%>");
35788 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35789 return;
35791 cp_parser_require_pragma_eol (parser, pragma_tok);
35792 if (!scope_chain->omp_declare_target_attribute)
35793 error_at (pragma_tok->location,
35794 "%<#pragma omp end declare target%> without corresponding "
35795 "%<#pragma omp declare target%>");
35796 else
35797 scope_chain->omp_declare_target_attribute--;
35800 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
35801 expression and optional initializer clause of
35802 #pragma omp declare reduction. We store the expression(s) as
35803 either 3, 6 or 7 special statements inside of the artificial function's
35804 body. The first two statements are DECL_EXPRs for the artificial
35805 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
35806 expression that uses those variables.
35807 If there was any INITIALIZER clause, this is followed by further statements,
35808 the fourth and fifth statements are DECL_EXPRs for the artificial
35809 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
35810 constructor variant (first token after open paren is not omp_priv),
35811 then the sixth statement is a statement with the function call expression
35812 that uses the OMP_PRIV and optionally OMP_ORIG variable.
35813 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
35814 to initialize the OMP_PRIV artificial variable and there is seventh
35815 statement, a DECL_EXPR of the OMP_PRIV statement again. */
35817 static bool
35818 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
35820 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
35821 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
35822 type = TREE_TYPE (type);
35823 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
35824 DECL_ARTIFICIAL (omp_out) = 1;
35825 pushdecl (omp_out);
35826 add_decl_expr (omp_out);
35827 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
35828 DECL_ARTIFICIAL (omp_in) = 1;
35829 pushdecl (omp_in);
35830 add_decl_expr (omp_in);
35831 tree combiner;
35832 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
35834 keep_next_level (true);
35835 tree block = begin_omp_structured_block ();
35836 combiner = cp_parser_expression (parser);
35837 finish_expr_stmt (combiner);
35838 block = finish_omp_structured_block (block);
35839 add_stmt (block);
35841 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
35842 return false;
35844 const char *p = "";
35845 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35847 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35848 p = IDENTIFIER_POINTER (id);
35851 if (strcmp (p, "initializer") == 0)
35853 cp_lexer_consume_token (parser->lexer);
35854 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35855 return false;
35857 p = "";
35858 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35860 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35861 p = IDENTIFIER_POINTER (id);
35864 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
35865 DECL_ARTIFICIAL (omp_priv) = 1;
35866 pushdecl (omp_priv);
35867 add_decl_expr (omp_priv);
35868 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
35869 DECL_ARTIFICIAL (omp_orig) = 1;
35870 pushdecl (omp_orig);
35871 add_decl_expr (omp_orig);
35873 keep_next_level (true);
35874 block = begin_omp_structured_block ();
35876 bool ctor = false;
35877 if (strcmp (p, "omp_priv") == 0)
35879 bool is_direct_init, is_non_constant_init;
35880 ctor = true;
35881 cp_lexer_consume_token (parser->lexer);
35882 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
35883 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
35884 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
35885 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
35886 == CPP_CLOSE_PAREN
35887 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
35888 == CPP_CLOSE_PAREN))
35890 finish_omp_structured_block (block);
35891 error ("invalid initializer clause");
35892 return false;
35894 initializer = cp_parser_initializer (parser, &is_direct_init,
35895 &is_non_constant_init);
35896 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
35897 NULL_TREE, LOOKUP_ONLYCONVERTING);
35899 else
35901 cp_parser_parse_tentatively (parser);
35902 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
35903 /*check_dependency_p=*/true,
35904 /*template_p=*/NULL,
35905 /*declarator_p=*/false,
35906 /*optional_p=*/false);
35907 vec<tree, va_gc> *args;
35908 if (fn_name == error_mark_node
35909 || cp_parser_error_occurred (parser)
35910 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
35911 || ((args = cp_parser_parenthesized_expression_list
35912 (parser, non_attr, /*cast_p=*/false,
35913 /*allow_expansion_p=*/true,
35914 /*non_constant_p=*/NULL)),
35915 cp_parser_error_occurred (parser)))
35917 finish_omp_structured_block (block);
35918 cp_parser_abort_tentative_parse (parser);
35919 cp_parser_error (parser, "expected id-expression (arguments)");
35920 return false;
35922 unsigned int i;
35923 tree arg;
35924 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
35925 if (arg == omp_priv
35926 || (TREE_CODE (arg) == ADDR_EXPR
35927 && TREE_OPERAND (arg, 0) == omp_priv))
35928 break;
35929 cp_parser_abort_tentative_parse (parser);
35930 if (arg == NULL_TREE)
35931 error ("one of the initializer call arguments should be %<omp_priv%>"
35932 " or %<&omp_priv%>");
35933 initializer = cp_parser_postfix_expression (parser, false, false, false,
35934 false, NULL);
35935 finish_expr_stmt (initializer);
35938 block = finish_omp_structured_block (block);
35939 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
35940 add_stmt (block);
35942 if (ctor)
35943 add_decl_expr (omp_orig);
35945 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
35946 return false;
35949 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
35950 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
35952 return true;
35955 /* OpenMP 4.0
35956 #pragma omp declare reduction (reduction-id : typename-list : expression) \
35957 initializer-clause[opt] new-line
35959 initializer-clause:
35960 initializer (omp_priv initializer)
35961 initializer (function-name (argument-list)) */
35963 static void
35964 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
35965 enum pragma_context)
35967 auto_vec<tree> types;
35968 enum tree_code reduc_code = ERROR_MARK;
35969 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
35970 unsigned int i;
35971 cp_token *first_token;
35972 cp_token_cache *cp;
35973 int errs;
35974 void *p;
35976 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
35977 p = obstack_alloc (&declarator_obstack, 0);
35979 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35980 goto fail;
35982 switch (cp_lexer_peek_token (parser->lexer)->type)
35984 case CPP_PLUS:
35985 reduc_code = PLUS_EXPR;
35986 break;
35987 case CPP_MULT:
35988 reduc_code = MULT_EXPR;
35989 break;
35990 case CPP_MINUS:
35991 reduc_code = MINUS_EXPR;
35992 break;
35993 case CPP_AND:
35994 reduc_code = BIT_AND_EXPR;
35995 break;
35996 case CPP_XOR:
35997 reduc_code = BIT_XOR_EXPR;
35998 break;
35999 case CPP_OR:
36000 reduc_code = BIT_IOR_EXPR;
36001 break;
36002 case CPP_AND_AND:
36003 reduc_code = TRUTH_ANDIF_EXPR;
36004 break;
36005 case CPP_OR_OR:
36006 reduc_code = TRUTH_ORIF_EXPR;
36007 break;
36008 case CPP_NAME:
36009 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
36010 break;
36011 default:
36012 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
36013 "%<|%>, %<&&%>, %<||%> or identifier");
36014 goto fail;
36017 if (reduc_code != ERROR_MARK)
36018 cp_lexer_consume_token (parser->lexer);
36020 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
36021 if (reduc_id == error_mark_node)
36022 goto fail;
36024 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36025 goto fail;
36027 /* Types may not be defined in declare reduction type list. */
36028 const char *saved_message;
36029 saved_message = parser->type_definition_forbidden_message;
36030 parser->type_definition_forbidden_message
36031 = G_("types may not be defined in declare reduction type list");
36032 bool saved_colon_corrects_to_scope_p;
36033 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
36034 parser->colon_corrects_to_scope_p = false;
36035 bool saved_colon_doesnt_start_class_def_p;
36036 saved_colon_doesnt_start_class_def_p
36037 = parser->colon_doesnt_start_class_def_p;
36038 parser->colon_doesnt_start_class_def_p = true;
36040 while (true)
36042 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36043 type = cp_parser_type_id (parser);
36044 if (type == error_mark_node)
36046 else if (ARITHMETIC_TYPE_P (type)
36047 && (orig_reduc_id == NULL_TREE
36048 || (TREE_CODE (type) != COMPLEX_TYPE
36049 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
36050 "min") == 0
36051 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
36052 "max") == 0))))
36053 error_at (loc, "predeclared arithmetic type %qT in "
36054 "%<#pragma omp declare reduction%>", type);
36055 else if (TREE_CODE (type) == FUNCTION_TYPE
36056 || TREE_CODE (type) == METHOD_TYPE
36057 || TREE_CODE (type) == ARRAY_TYPE)
36058 error_at (loc, "function or array type %qT in "
36059 "%<#pragma omp declare reduction%>", type);
36060 else if (TREE_CODE (type) == REFERENCE_TYPE)
36061 error_at (loc, "reference type %qT in "
36062 "%<#pragma omp declare reduction%>", type);
36063 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
36064 error_at (loc, "const, volatile or __restrict qualified type %qT in "
36065 "%<#pragma omp declare reduction%>", type);
36066 else
36067 types.safe_push (type);
36069 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36070 cp_lexer_consume_token (parser->lexer);
36071 else
36072 break;
36075 /* Restore the saved message. */
36076 parser->type_definition_forbidden_message = saved_message;
36077 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36078 parser->colon_doesnt_start_class_def_p
36079 = saved_colon_doesnt_start_class_def_p;
36081 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
36082 || types.is_empty ())
36084 fail:
36085 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36086 goto done;
36089 first_token = cp_lexer_peek_token (parser->lexer);
36090 cp = NULL;
36091 errs = errorcount;
36092 FOR_EACH_VEC_ELT (types, i, type)
36094 tree fntype
36095 = build_function_type_list (void_type_node,
36096 cp_build_reference_type (type, false),
36097 NULL_TREE);
36098 tree this_reduc_id = reduc_id;
36099 if (!dependent_type_p (type))
36100 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
36101 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
36102 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
36103 DECL_ARTIFICIAL (fndecl) = 1;
36104 DECL_EXTERNAL (fndecl) = 1;
36105 DECL_DECLARED_INLINE_P (fndecl) = 1;
36106 DECL_IGNORED_P (fndecl) = 1;
36107 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
36108 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
36109 DECL_ATTRIBUTES (fndecl)
36110 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
36111 DECL_ATTRIBUTES (fndecl));
36112 if (processing_template_decl)
36113 fndecl = push_template_decl (fndecl);
36114 bool block_scope = false;
36115 tree block = NULL_TREE;
36116 if (current_function_decl)
36118 block_scope = true;
36119 DECL_CONTEXT (fndecl) = global_namespace;
36120 if (!processing_template_decl)
36121 pushdecl (fndecl);
36123 else if (current_class_type)
36125 if (cp == NULL)
36127 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36128 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36129 cp_lexer_consume_token (parser->lexer);
36130 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36131 goto fail;
36132 cp = cp_token_cache_new (first_token,
36133 cp_lexer_peek_nth_token (parser->lexer,
36134 2));
36136 DECL_STATIC_FUNCTION_P (fndecl) = 1;
36137 finish_member_declaration (fndecl);
36138 DECL_PENDING_INLINE_INFO (fndecl) = cp;
36139 DECL_PENDING_INLINE_P (fndecl) = 1;
36140 vec_safe_push (unparsed_funs_with_definitions, fndecl);
36141 continue;
36143 else
36145 DECL_CONTEXT (fndecl) = current_namespace;
36146 pushdecl (fndecl);
36148 if (!block_scope)
36149 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
36150 else
36151 block = begin_omp_structured_block ();
36152 if (cp)
36154 cp_parser_push_lexer_for_tokens (parser, cp);
36155 parser->lexer->in_pragma = true;
36157 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
36159 if (!block_scope)
36160 finish_function (0);
36161 else
36162 DECL_CONTEXT (fndecl) = current_function_decl;
36163 if (cp)
36164 cp_parser_pop_lexer (parser);
36165 goto fail;
36167 if (cp)
36168 cp_parser_pop_lexer (parser);
36169 if (!block_scope)
36170 finish_function (0);
36171 else
36173 DECL_CONTEXT (fndecl) = current_function_decl;
36174 block = finish_omp_structured_block (block);
36175 if (TREE_CODE (block) == BIND_EXPR)
36176 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
36177 else if (TREE_CODE (block) == STATEMENT_LIST)
36178 DECL_SAVED_TREE (fndecl) = block;
36179 if (processing_template_decl)
36180 add_decl_expr (fndecl);
36182 cp_check_omp_declare_reduction (fndecl);
36183 if (cp == NULL && types.length () > 1)
36184 cp = cp_token_cache_new (first_token,
36185 cp_lexer_peek_nth_token (parser->lexer, 2));
36186 if (errs != errorcount)
36187 break;
36190 cp_parser_require_pragma_eol (parser, pragma_tok);
36192 done:
36193 /* Free any declarators allocated. */
36194 obstack_free (&declarator_obstack, p);
36197 /* OpenMP 4.0
36198 #pragma omp declare simd declare-simd-clauses[optseq] new-line
36199 #pragma omp declare reduction (reduction-id : typename-list : expression) \
36200 initializer-clause[opt] new-line
36201 #pragma omp declare target new-line */
36203 static void
36204 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
36205 enum pragma_context context)
36207 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36209 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36210 const char *p = IDENTIFIER_POINTER (id);
36212 if (strcmp (p, "simd") == 0)
36214 cp_lexer_consume_token (parser->lexer);
36215 cp_parser_omp_declare_simd (parser, pragma_tok,
36216 context);
36217 return;
36219 cp_ensure_no_omp_declare_simd (parser);
36220 if (strcmp (p, "reduction") == 0)
36222 cp_lexer_consume_token (parser->lexer);
36223 cp_parser_omp_declare_reduction (parser, pragma_tok,
36224 context);
36225 return;
36227 if (!flag_openmp) /* flag_openmp_simd */
36229 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36230 return;
36232 if (strcmp (p, "target") == 0)
36234 cp_lexer_consume_token (parser->lexer);
36235 cp_parser_omp_declare_target (parser, pragma_tok);
36236 return;
36239 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
36240 "or %<target%>");
36241 cp_parser_require_pragma_eol (parser, pragma_tok);
36244 /* OpenMP 4.5:
36245 #pragma omp taskloop taskloop-clause[optseq] new-line
36246 for-loop
36248 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
36249 for-loop */
36251 #define OMP_TASKLOOP_CLAUSE_MASK \
36252 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36253 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36254 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36255 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36256 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
36257 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
36258 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
36259 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
36260 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
36261 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36262 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
36263 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
36264 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
36265 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
36267 static tree
36268 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
36269 char *p_name, omp_clause_mask mask, tree *cclauses)
36271 tree clauses, sb, ret;
36272 unsigned int save;
36273 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36275 strcat (p_name, " taskloop");
36276 mask |= OMP_TASKLOOP_CLAUSE_MASK;
36278 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36280 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36281 const char *p = IDENTIFIER_POINTER (id);
36283 if (strcmp (p, "simd") == 0)
36285 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36286 if (cclauses == NULL)
36287 cclauses = cclauses_buf;
36289 cp_lexer_consume_token (parser->lexer);
36290 if (!flag_openmp) /* flag_openmp_simd */
36291 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36292 cclauses);
36293 sb = begin_omp_structured_block ();
36294 save = cp_parser_begin_omp_structured_block (parser);
36295 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36296 cclauses);
36297 cp_parser_end_omp_structured_block (parser, save);
36298 tree body = finish_omp_structured_block (sb);
36299 if (ret == NULL)
36300 return ret;
36301 ret = make_node (OMP_TASKLOOP);
36302 TREE_TYPE (ret) = void_type_node;
36303 OMP_FOR_BODY (ret) = body;
36304 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
36305 SET_EXPR_LOCATION (ret, loc);
36306 add_stmt (ret);
36307 return ret;
36310 if (!flag_openmp) /* flag_openmp_simd */
36312 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36313 return NULL_TREE;
36316 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36317 cclauses == NULL);
36318 if (cclauses)
36320 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
36321 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
36324 sb = begin_omp_structured_block ();
36325 save = cp_parser_begin_omp_structured_block (parser);
36327 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses);
36329 cp_parser_end_omp_structured_block (parser, save);
36330 add_stmt (finish_omp_structured_block (sb));
36332 return ret;
36336 /* OpenACC 2.0:
36337 # pragma acc routine oacc-routine-clause[optseq] new-line
36338 function-definition
36340 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
36343 #define OACC_ROUTINE_CLAUSE_MASK \
36344 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
36350 /* Parse the OpenACC routine pragma. This has an optional '( name )'
36351 component, which must resolve to a declared namespace-scope
36352 function. The clauses are either processed directly (for a named
36353 function), or defered until the immediatley following declaration
36354 is parsed. */
36356 static void
36357 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
36358 enum pragma_context context)
36360 bool first_p = parser->oacc_routine == NULL;
36361 location_t loc = pragma_tok->location;
36362 cp_omp_declare_simd_data data;
36363 if (first_p)
36365 data.error_seen = false;
36366 data.fndecl_seen = false;
36367 data.tokens = vNULL;
36368 data.clauses = NULL_TREE;
36369 parser->oacc_routine = &data;
36372 tree decl = NULL_TREE;
36373 /* Create a dummy claue, to record location. */
36374 tree c_head = build_omp_clause (pragma_tok->location, OMP_CLAUSE_SEQ);
36376 if (context != pragma_external)
36378 cp_parser_error (parser, "%<#pragma acc routine%> not at file scope");
36379 parser->oacc_routine->error_seen = true;
36380 parser->oacc_routine = NULL;
36381 return;
36384 /* Look for optional '( name )'. */
36385 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36387 if (!first_p)
36389 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36390 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36391 cp_lexer_consume_token (parser->lexer);
36392 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36393 parser->oacc_routine->error_seen = true;
36394 cp_parser_require_pragma_eol (parser, pragma_tok);
36396 error_at (OMP_CLAUSE_LOCATION (parser->oacc_routine->clauses),
36397 "%<#pragma acc routine%> not followed by a "
36398 "function declaration or definition");
36400 parser->oacc_routine->error_seen = true;
36401 return;
36404 cp_lexer_consume_token (parser->lexer);
36405 cp_token *token = cp_lexer_peek_token (parser->lexer);
36407 /* We parse the name as an id-expression. If it resolves to
36408 anything other than a non-overloaded function at namespace
36409 scope, it's an error. */
36410 tree id = cp_parser_id_expression (parser,
36411 /*template_keyword_p=*/false,
36412 /*check_dependency_p=*/false,
36413 /*template_p=*/NULL,
36414 /*declarator_p=*/false,
36415 /*optional_p=*/false);
36416 decl = cp_parser_lookup_name_simple (parser, id, token->location);
36417 if (id != error_mark_node && decl == error_mark_node)
36418 cp_parser_name_lookup_error (parser, id, decl, NLE_NULL,
36419 token->location);
36421 if (decl == error_mark_node
36422 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36424 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36425 parser->oacc_routine = NULL;
36426 return;
36429 /* Build a chain of clauses. */
36430 parser->lexer->in_pragma = true;
36431 tree clauses = NULL_TREE;
36432 clauses = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
36433 "#pragma acc routine",
36434 cp_lexer_peek_token
36435 (parser->lexer));
36437 /* Force clauses to be non-null, by attaching context to it. */
36438 clauses = tree_cons (c_head, clauses, NULL_TREE);
36440 if (decl && is_overloaded_fn (decl)
36441 && (TREE_CODE (decl) != FUNCTION_DECL
36442 || DECL_FUNCTION_TEMPLATE_P (decl)))
36444 error_at (loc, "%<#pragma acc routine%> names a set of overloads");
36445 parser->oacc_routine = NULL;
36446 return;
36449 /* Perhaps we should use the same rule as declarations in different
36450 namespaces? */
36451 if (!DECL_NAMESPACE_SCOPE_P (decl))
36453 error_at (loc, "%<#pragma acc routine%> does not refer to a "
36454 "namespace scope function");
36455 parser->oacc_routine = NULL;
36456 return;
36459 if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
36461 error_at (loc,
36462 "%<#pragma acc routine%> does not refer to a function");
36463 parser->oacc_routine = NULL;
36464 return;
36467 data.clauses = clauses;
36469 cp_finalize_oacc_routine (parser, decl, false);
36470 data.tokens.release ();
36471 parser->oacc_routine = NULL;
36473 else
36475 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36476 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36477 cp_lexer_consume_token (parser->lexer);
36478 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36479 parser->oacc_routine->error_seen = true;
36480 cp_parser_require_pragma_eol (parser, pragma_tok);
36482 struct cp_token_cache *cp
36483 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
36484 parser->oacc_routine->tokens.safe_push (cp);
36486 if (first_p)
36487 parser->oacc_routine->clauses = c_head;
36489 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
36490 cp_parser_pragma (parser, context);
36492 if (first_p)
36494 /* Create an empty list of clauses. */
36495 parser->oacc_routine->clauses = tree_cons (c_head, NULL_TREE,
36496 NULL_TREE);
36497 cp_parser_declaration (parser);
36499 if (parser->oacc_routine
36500 && !parser->oacc_routine->error_seen
36501 && !parser->oacc_routine->fndecl_seen)
36502 error_at (loc, "%<#pragma acc routine%> not followed by a "
36503 "function declaration or definition");
36505 data.tokens.release ();
36506 parser->oacc_routine = NULL;
36511 /* Finalize #pragma acc routine clauses after direct declarator has
36512 been parsed, and put that into "oacc function" attribute. */
36514 static tree
36515 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
36517 struct cp_token_cache *ce;
36518 cp_omp_declare_simd_data *data = parser->oacc_routine;
36519 tree cl, clauses = parser->oacc_routine->clauses;
36520 location_t loc;
36522 loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE(clauses));
36524 if ((!data->error_seen && data->fndecl_seen)
36525 || data->tokens.length () != 1)
36527 error_at (loc, "%<#pragma acc routine%> not followed by a "
36528 "function declaration or definition");
36529 data->error_seen = true;
36530 return attrs;
36532 if (data->error_seen)
36533 return attrs;
36535 ce = data->tokens[0];
36537 cp_parser_push_lexer_for_tokens (parser, ce);
36538 parser->lexer->in_pragma = true;
36539 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
36541 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
36542 cl = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
36543 "#pragma acc routine", pragma_tok);
36544 cp_parser_pop_lexer (parser);
36546 tree c_head = build_omp_clause (loc, OMP_CLAUSE_SEQ);
36548 /* Force clauses to be non-null, by attaching context to it. */
36549 parser->oacc_routine->clauses = tree_cons (c_head, cl, NULL_TREE);
36551 data->fndecl_seen = true;
36552 return attrs;
36555 /* Apply any saved OpenACC routine clauses to a just-parsed
36556 declaration. */
36558 static void
36559 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
36561 if (__builtin_expect (parser->oacc_routine != NULL, 0))
36563 tree clauses = parser->oacc_routine->clauses;
36564 location_t loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE(clauses));
36566 if (parser->oacc_routine->error_seen)
36567 return;
36569 if (fndecl == error_mark_node)
36571 parser->oacc_routine = NULL;
36572 return;
36575 if (TREE_CODE (fndecl) != FUNCTION_DECL)
36577 cp_ensure_no_oacc_routine (parser);
36578 return;
36581 if (!fndecl || TREE_CODE (fndecl) != FUNCTION_DECL)
36583 error_at (loc,
36584 "%<#pragma acc routine%> not followed by a function "
36585 "declaration or definition");
36586 parser->oacc_routine = NULL;
36589 if (get_oacc_fn_attrib (fndecl))
36591 error_at (loc, "%<#pragma acc routine%> already applied to %D",
36592 fndecl);
36593 parser->oacc_routine = NULL;
36596 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
36598 error_at (loc, "%<#pragma acc routine%> must be applied before %s",
36599 TREE_USED (fndecl) ? "use" : "definition");
36600 parser->oacc_routine = NULL;
36603 /* Process for function attrib */
36604 tree dims = build_oacc_routine_dims (TREE_VALUE (clauses));
36605 replace_oacc_fn_attrib (fndecl, dims);
36607 /* Add an "omp target" attribute. */
36608 DECL_ATTRIBUTES (fndecl)
36609 = tree_cons (get_identifier ("omp declare target"),
36610 NULL_TREE, DECL_ATTRIBUTES (fndecl));
36614 /* Main entry point to OpenMP statement pragmas. */
36616 static void
36617 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
36619 tree stmt;
36620 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
36621 omp_clause_mask mask (0);
36623 switch (cp_parser_pragma_kind (pragma_tok))
36625 case PRAGMA_OACC_ATOMIC:
36626 cp_parser_omp_atomic (parser, pragma_tok);
36627 return;
36628 case PRAGMA_OACC_CACHE:
36629 stmt = cp_parser_oacc_cache (parser, pragma_tok);
36630 break;
36631 case PRAGMA_OACC_DATA:
36632 stmt = cp_parser_oacc_data (parser, pragma_tok);
36633 break;
36634 case PRAGMA_OACC_ENTER_DATA:
36635 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
36636 break;
36637 case PRAGMA_OACC_EXIT_DATA:
36638 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
36639 break;
36640 case PRAGMA_OACC_HOST_DATA:
36641 stmt = cp_parser_oacc_host_data (parser, pragma_tok);
36642 break;
36643 case PRAGMA_OACC_KERNELS:
36644 case PRAGMA_OACC_PARALLEL:
36645 strcpy (p_name, "#pragma acc");
36646 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name);
36647 break;
36648 case PRAGMA_OACC_LOOP:
36649 strcpy (p_name, "#pragma acc");
36650 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL);
36651 break;
36652 case PRAGMA_OACC_UPDATE:
36653 stmt = cp_parser_oacc_update (parser, pragma_tok);
36654 break;
36655 case PRAGMA_OACC_WAIT:
36656 stmt = cp_parser_oacc_wait (parser, pragma_tok);
36657 break;
36658 case PRAGMA_OMP_ATOMIC:
36659 cp_parser_omp_atomic (parser, pragma_tok);
36660 return;
36661 case PRAGMA_OMP_CRITICAL:
36662 stmt = cp_parser_omp_critical (parser, pragma_tok);
36663 break;
36664 case PRAGMA_OMP_DISTRIBUTE:
36665 strcpy (p_name, "#pragma omp");
36666 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
36667 break;
36668 case PRAGMA_OMP_FOR:
36669 strcpy (p_name, "#pragma omp");
36670 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
36671 break;
36672 case PRAGMA_OMP_MASTER:
36673 stmt = cp_parser_omp_master (parser, pragma_tok);
36674 break;
36675 case PRAGMA_OMP_PARALLEL:
36676 strcpy (p_name, "#pragma omp");
36677 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
36678 break;
36679 case PRAGMA_OMP_SECTIONS:
36680 strcpy (p_name, "#pragma omp");
36681 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
36682 break;
36683 case PRAGMA_OMP_SIMD:
36684 strcpy (p_name, "#pragma omp");
36685 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
36686 break;
36687 case PRAGMA_OMP_SINGLE:
36688 stmt = cp_parser_omp_single (parser, pragma_tok);
36689 break;
36690 case PRAGMA_OMP_TASK:
36691 stmt = cp_parser_omp_task (parser, pragma_tok);
36692 break;
36693 case PRAGMA_OMP_TASKGROUP:
36694 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
36695 break;
36696 case PRAGMA_OMP_TASKLOOP:
36697 strcpy (p_name, "#pragma omp");
36698 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL);
36699 break;
36700 case PRAGMA_OMP_TEAMS:
36701 strcpy (p_name, "#pragma omp");
36702 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
36703 break;
36704 default:
36705 gcc_unreachable ();
36708 protected_set_expr_location (stmt, pragma_tok->location);
36711 /* Transactional Memory parsing routines. */
36713 /* Parse a transaction attribute.
36715 txn-attribute:
36716 attribute
36717 [ [ identifier ] ]
36719 We use this instead of cp_parser_attributes_opt for transactions to avoid
36720 the pedwarn in C++98 mode. */
36722 static tree
36723 cp_parser_txn_attribute_opt (cp_parser *parser)
36725 cp_token *token;
36726 tree attr_name, attr = NULL;
36728 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
36729 return cp_parser_attributes_opt (parser);
36731 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
36732 return NULL_TREE;
36733 cp_lexer_consume_token (parser->lexer);
36734 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
36735 goto error1;
36737 token = cp_lexer_peek_token (parser->lexer);
36738 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
36740 token = cp_lexer_consume_token (parser->lexer);
36742 attr_name = (token->type == CPP_KEYWORD
36743 /* For keywords, use the canonical spelling,
36744 not the parsed identifier. */
36745 ? ridpointers[(int) token->keyword]
36746 : token->u.value);
36747 attr = build_tree_list (attr_name, NULL_TREE);
36749 else
36750 cp_parser_error (parser, "expected identifier");
36752 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
36753 error1:
36754 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
36755 return attr;
36758 /* Parse a __transaction_atomic or __transaction_relaxed statement.
36760 transaction-statement:
36761 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
36762 compound-statement
36763 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
36766 static tree
36767 cp_parser_transaction (cp_parser *parser, cp_token *token)
36769 unsigned char old_in = parser->in_transaction;
36770 unsigned char this_in = 1, new_in;
36771 enum rid keyword = token->keyword;
36772 tree stmt, attrs, noex;
36774 cp_lexer_consume_token (parser->lexer);
36776 if (keyword == RID_TRANSACTION_RELAXED
36777 || keyword == RID_SYNCHRONIZED)
36778 this_in |= TM_STMT_ATTR_RELAXED;
36779 else
36781 attrs = cp_parser_txn_attribute_opt (parser);
36782 if (attrs)
36783 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
36786 /* Parse a noexcept specification. */
36787 if (keyword == RID_ATOMIC_NOEXCEPT)
36788 noex = boolean_true_node;
36789 else if (keyword == RID_ATOMIC_CANCEL)
36791 /* cancel-and-throw is unimplemented. */
36792 sorry ("atomic_cancel");
36793 noex = NULL_TREE;
36795 else
36796 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
36798 /* Keep track if we're in the lexical scope of an outer transaction. */
36799 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
36801 stmt = begin_transaction_stmt (token->location, NULL, this_in);
36803 parser->in_transaction = new_in;
36804 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
36805 parser->in_transaction = old_in;
36807 finish_transaction_stmt (stmt, NULL, this_in, noex);
36809 return stmt;
36812 /* Parse a __transaction_atomic or __transaction_relaxed expression.
36814 transaction-expression:
36815 __transaction_atomic txn-noexcept-spec[opt] ( expression )
36816 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
36819 static tree
36820 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
36822 unsigned char old_in = parser->in_transaction;
36823 unsigned char this_in = 1;
36824 cp_token *token;
36825 tree expr, noex;
36826 bool noex_expr;
36827 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36829 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
36830 || keyword == RID_TRANSACTION_RELAXED);
36832 if (!flag_tm)
36833 error_at (loc,
36834 keyword == RID_TRANSACTION_RELAXED
36835 ? G_("%<__transaction_relaxed%> without transactional memory "
36836 "support enabled")
36837 : G_("%<__transaction_atomic%> without transactional memory "
36838 "support enabled"));
36840 token = cp_parser_require_keyword (parser, keyword,
36841 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
36842 : RT_TRANSACTION_RELAXED));
36843 gcc_assert (token != NULL);
36845 if (keyword == RID_TRANSACTION_RELAXED)
36846 this_in |= TM_STMT_ATTR_RELAXED;
36848 /* Set this early. This might mean that we allow transaction_cancel in
36849 an expression that we find out later actually has to be a constexpr.
36850 However, we expect that cxx_constant_value will be able to deal with
36851 this; also, if the noexcept has no constexpr, then what we parse next
36852 really is a transaction's body. */
36853 parser->in_transaction = this_in;
36855 /* Parse a noexcept specification. */
36856 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
36857 true);
36859 if (!noex || !noex_expr
36860 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36862 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
36864 expr = cp_parser_expression (parser);
36865 expr = finish_parenthesized_expr (expr);
36867 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
36869 else
36871 /* The only expression that is available got parsed for the noexcept
36872 already. noexcept is true then. */
36873 expr = noex;
36874 noex = boolean_true_node;
36877 expr = build_transaction_expr (token->location, expr, this_in, noex);
36878 parser->in_transaction = old_in;
36880 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
36881 return error_mark_node;
36883 return (flag_tm ? expr : error_mark_node);
36886 /* Parse a function-transaction-block.
36888 function-transaction-block:
36889 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
36890 function-body
36891 __transaction_atomic txn-attribute[opt] function-try-block
36892 __transaction_relaxed ctor-initializer[opt] function-body
36893 __transaction_relaxed function-try-block
36896 static bool
36897 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
36899 unsigned char old_in = parser->in_transaction;
36900 unsigned char new_in = 1;
36901 tree compound_stmt, stmt, attrs;
36902 bool ctor_initializer_p;
36903 cp_token *token;
36905 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
36906 || keyword == RID_TRANSACTION_RELAXED);
36907 token = cp_parser_require_keyword (parser, keyword,
36908 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
36909 : RT_TRANSACTION_RELAXED));
36910 gcc_assert (token != NULL);
36912 if (keyword == RID_TRANSACTION_RELAXED)
36913 new_in |= TM_STMT_ATTR_RELAXED;
36914 else
36916 attrs = cp_parser_txn_attribute_opt (parser);
36917 if (attrs)
36918 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
36921 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
36923 parser->in_transaction = new_in;
36925 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
36926 ctor_initializer_p = cp_parser_function_try_block (parser);
36927 else
36928 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
36929 (parser, /*in_function_try_block=*/false);
36931 parser->in_transaction = old_in;
36933 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
36935 return ctor_initializer_p;
36938 /* Parse a __transaction_cancel statement.
36940 cancel-statement:
36941 __transaction_cancel txn-attribute[opt] ;
36942 __transaction_cancel txn-attribute[opt] throw-expression ;
36944 ??? Cancel and throw is not yet implemented. */
36946 static tree
36947 cp_parser_transaction_cancel (cp_parser *parser)
36949 cp_token *token;
36950 bool is_outer = false;
36951 tree stmt, attrs;
36953 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
36954 RT_TRANSACTION_CANCEL);
36955 gcc_assert (token != NULL);
36957 attrs = cp_parser_txn_attribute_opt (parser);
36958 if (attrs)
36959 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
36961 /* ??? Parse cancel-and-throw here. */
36963 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
36965 if (!flag_tm)
36967 error_at (token->location, "%<__transaction_cancel%> without "
36968 "transactional memory support enabled");
36969 return error_mark_node;
36971 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
36973 error_at (token->location, "%<__transaction_cancel%> within a "
36974 "%<__transaction_relaxed%>");
36975 return error_mark_node;
36977 else if (is_outer)
36979 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
36980 && !is_tm_may_cancel_outer (current_function_decl))
36982 error_at (token->location, "outer %<__transaction_cancel%> not "
36983 "within outer %<__transaction_atomic%>");
36984 error_at (token->location,
36985 " or a %<transaction_may_cancel_outer%> function");
36986 return error_mark_node;
36989 else if (parser->in_transaction == 0)
36991 error_at (token->location, "%<__transaction_cancel%> not within "
36992 "%<__transaction_atomic%>");
36993 return error_mark_node;
36996 stmt = build_tm_abort_call (token->location, is_outer);
36997 add_stmt (stmt);
36999 return stmt;
37002 /* The parser. */
37004 static GTY (()) cp_parser *the_parser;
37007 /* Special handling for the first token or line in the file. The first
37008 thing in the file might be #pragma GCC pch_preprocess, which loads a
37009 PCH file, which is a GC collection point. So we need to handle this
37010 first pragma without benefit of an existing lexer structure.
37012 Always returns one token to the caller in *FIRST_TOKEN. This is
37013 either the true first token of the file, or the first token after
37014 the initial pragma. */
37016 static void
37017 cp_parser_initial_pragma (cp_token *first_token)
37019 tree name = NULL;
37021 cp_lexer_get_preprocessor_token (NULL, first_token);
37022 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
37023 return;
37025 cp_lexer_get_preprocessor_token (NULL, first_token);
37026 if (first_token->type == CPP_STRING)
37028 name = first_token->u.value;
37030 cp_lexer_get_preprocessor_token (NULL, first_token);
37031 if (first_token->type != CPP_PRAGMA_EOL)
37032 error_at (first_token->location,
37033 "junk at end of %<#pragma GCC pch_preprocess%>");
37035 else
37036 error_at (first_token->location, "expected string literal");
37038 /* Skip to the end of the pragma. */
37039 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
37040 cp_lexer_get_preprocessor_token (NULL, first_token);
37042 /* Now actually load the PCH file. */
37043 if (name)
37044 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
37046 /* Read one more token to return to our caller. We have to do this
37047 after reading the PCH file in, since its pointers have to be
37048 live. */
37049 cp_lexer_get_preprocessor_token (NULL, first_token);
37052 /* Parses the grainsize pragma for the _Cilk_for statement.
37053 Syntax:
37054 #pragma cilk grainsize = <VALUE>. */
37056 static void
37057 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok)
37059 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
37061 tree exp = cp_parser_binary_expression (parser, false, false,
37062 PREC_NOT_OPERATOR, NULL);
37063 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37064 if (!exp || exp == error_mark_node)
37066 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
37067 return;
37070 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
37071 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
37072 cp_parser_cilk_for (parser, exp);
37073 else
37074 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
37075 "%<#pragma cilk grainsize%> is not followed by "
37076 "%<_Cilk_for%>");
37077 return;
37079 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37082 /* Normal parsing of a pragma token. Here we can (and must) use the
37083 regular lexer. */
37085 static bool
37086 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
37088 cp_token *pragma_tok;
37089 unsigned int id;
37090 tree stmt;
37091 bool ret;
37093 pragma_tok = cp_lexer_consume_token (parser->lexer);
37094 gcc_assert (pragma_tok->type == CPP_PRAGMA);
37095 parser->lexer->in_pragma = true;
37097 id = cp_parser_pragma_kind (pragma_tok);
37098 if (id != PRAGMA_OMP_DECLARE_REDUCTION && id != PRAGMA_OACC_ROUTINE)
37099 cp_ensure_no_omp_declare_simd (parser);
37100 switch (id)
37102 case PRAGMA_GCC_PCH_PREPROCESS:
37103 error_at (pragma_tok->location,
37104 "%<#pragma GCC pch_preprocess%> must be first");
37105 break;
37107 case PRAGMA_OMP_BARRIER:
37108 switch (context)
37110 case pragma_compound:
37111 cp_parser_omp_barrier (parser, pragma_tok);
37112 return false;
37113 case pragma_stmt:
37114 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
37115 "used in compound statements");
37116 break;
37117 default:
37118 goto bad_stmt;
37120 break;
37122 case PRAGMA_OMP_FLUSH:
37123 switch (context)
37125 case pragma_compound:
37126 cp_parser_omp_flush (parser, pragma_tok);
37127 return false;
37128 case pragma_stmt:
37129 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
37130 "used in compound statements");
37131 break;
37132 default:
37133 goto bad_stmt;
37135 break;
37137 case PRAGMA_OMP_TASKWAIT:
37138 switch (context)
37140 case pragma_compound:
37141 cp_parser_omp_taskwait (parser, pragma_tok);
37142 return false;
37143 case pragma_stmt:
37144 error_at (pragma_tok->location,
37145 "%<#pragma omp taskwait%> may only be "
37146 "used in compound statements");
37147 break;
37148 default:
37149 goto bad_stmt;
37151 break;
37153 case PRAGMA_OMP_TASKYIELD:
37154 switch (context)
37156 case pragma_compound:
37157 cp_parser_omp_taskyield (parser, pragma_tok);
37158 return false;
37159 case pragma_stmt:
37160 error_at (pragma_tok->location,
37161 "%<#pragma omp taskyield%> may only be "
37162 "used in compound statements");
37163 break;
37164 default:
37165 goto bad_stmt;
37167 break;
37169 case PRAGMA_OMP_CANCEL:
37170 switch (context)
37172 case pragma_compound:
37173 cp_parser_omp_cancel (parser, pragma_tok);
37174 return false;
37175 case pragma_stmt:
37176 error_at (pragma_tok->location,
37177 "%<#pragma omp cancel%> may only be "
37178 "used in compound statements");
37179 break;
37180 default:
37181 goto bad_stmt;
37183 break;
37185 case PRAGMA_OMP_CANCELLATION_POINT:
37186 switch (context)
37188 case pragma_compound:
37189 cp_parser_omp_cancellation_point (parser, pragma_tok);
37190 return false;
37191 case pragma_stmt:
37192 error_at (pragma_tok->location,
37193 "%<#pragma omp cancellation point%> may only be "
37194 "used in compound statements");
37195 break;
37196 default:
37197 goto bad_stmt;
37199 break;
37201 case PRAGMA_OMP_THREADPRIVATE:
37202 cp_parser_omp_threadprivate (parser, pragma_tok);
37203 return false;
37205 case PRAGMA_OMP_DECLARE_REDUCTION:
37206 cp_parser_omp_declare (parser, pragma_tok, context);
37207 return false;
37209 case PRAGMA_OACC_DECLARE:
37210 cp_parser_oacc_declare (parser, pragma_tok);
37211 return false;
37213 case PRAGMA_OACC_ROUTINE:
37214 cp_parser_oacc_routine (parser, pragma_tok, context);
37215 return false;
37217 case PRAGMA_OACC_ATOMIC:
37218 case PRAGMA_OACC_CACHE:
37219 case PRAGMA_OACC_DATA:
37220 case PRAGMA_OACC_ENTER_DATA:
37221 case PRAGMA_OACC_EXIT_DATA:
37222 case PRAGMA_OACC_HOST_DATA:
37223 case PRAGMA_OACC_KERNELS:
37224 case PRAGMA_OACC_PARALLEL:
37225 case PRAGMA_OACC_LOOP:
37226 case PRAGMA_OACC_UPDATE:
37227 case PRAGMA_OACC_WAIT:
37228 case PRAGMA_OMP_ATOMIC:
37229 case PRAGMA_OMP_CRITICAL:
37230 case PRAGMA_OMP_DISTRIBUTE:
37231 case PRAGMA_OMP_FOR:
37232 case PRAGMA_OMP_MASTER:
37233 case PRAGMA_OMP_PARALLEL:
37234 case PRAGMA_OMP_SECTIONS:
37235 case PRAGMA_OMP_SIMD:
37236 case PRAGMA_OMP_SINGLE:
37237 case PRAGMA_OMP_TASK:
37238 case PRAGMA_OMP_TASKGROUP:
37239 case PRAGMA_OMP_TASKLOOP:
37240 case PRAGMA_OMP_TEAMS:
37241 if (context != pragma_stmt && context != pragma_compound)
37242 goto bad_stmt;
37243 stmt = push_omp_privatization_clauses (false);
37244 cp_parser_omp_construct (parser, pragma_tok);
37245 pop_omp_privatization_clauses (stmt);
37246 return true;
37248 case PRAGMA_OMP_ORDERED:
37249 stmt = push_omp_privatization_clauses (false);
37250 ret = cp_parser_omp_ordered (parser, pragma_tok, context);
37251 pop_omp_privatization_clauses (stmt);
37252 return ret;
37254 case PRAGMA_OMP_TARGET:
37255 stmt = push_omp_privatization_clauses (false);
37256 ret = cp_parser_omp_target (parser, pragma_tok, context);
37257 pop_omp_privatization_clauses (stmt);
37258 return ret;
37260 case PRAGMA_OMP_END_DECLARE_TARGET:
37261 cp_parser_omp_end_declare_target (parser, pragma_tok);
37262 return false;
37264 case PRAGMA_OMP_SECTION:
37265 error_at (pragma_tok->location,
37266 "%<#pragma omp section%> may only be used in "
37267 "%<#pragma omp sections%> construct");
37268 break;
37270 case PRAGMA_IVDEP:
37272 if (context == pragma_external)
37274 error_at (pragma_tok->location,
37275 "%<#pragma GCC ivdep%> must be inside a function");
37276 break;
37278 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37279 cp_token *tok;
37280 tok = cp_lexer_peek_token (the_parser->lexer);
37281 if (tok->type != CPP_KEYWORD
37282 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
37283 && tok->keyword != RID_DO))
37285 cp_parser_error (parser, "for, while or do statement expected");
37286 return false;
37288 cp_parser_iteration_statement (parser, true);
37289 return true;
37292 case PRAGMA_CILK_SIMD:
37293 if (context == pragma_external)
37295 error_at (pragma_tok->location,
37296 "%<#pragma simd%> must be inside a function");
37297 break;
37299 stmt = push_omp_privatization_clauses (false);
37300 cp_parser_cilk_simd (parser, pragma_tok);
37301 pop_omp_privatization_clauses (stmt);
37302 return true;
37304 case PRAGMA_CILK_GRAINSIZE:
37305 if (context == pragma_external)
37307 error_at (pragma_tok->location,
37308 "%<#pragma cilk grainsize%> must be inside a function");
37309 break;
37312 /* Ignore the pragma if Cilk Plus is not enabled. */
37313 if (flag_cilkplus)
37315 cp_parser_cilk_grainsize (parser, pragma_tok);
37316 return true;
37318 else
37320 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
37321 "%<#pragma cilk grainsize%>");
37322 break;
37325 default:
37326 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
37327 c_invoke_pragma_handler (id);
37328 break;
37330 bad_stmt:
37331 cp_parser_error (parser, "expected declaration specifiers");
37332 break;
37335 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37336 return false;
37339 /* The interface the pragma parsers have to the lexer. */
37341 enum cpp_ttype
37342 pragma_lex (tree *value, location_t *loc)
37344 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
37345 enum cpp_ttype ret = tok->type;
37347 *value = tok->u.value;
37348 if (loc)
37349 *loc = tok->location;
37351 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
37352 ret = CPP_EOF;
37353 else if (ret == CPP_STRING)
37354 *value = cp_parser_string_literal (the_parser, false, false);
37355 else
37357 if (ret == CPP_KEYWORD)
37358 ret = CPP_NAME;
37359 cp_lexer_consume_token (the_parser->lexer);
37362 return ret;
37366 /* External interface. */
37368 /* Parse one entire translation unit. */
37370 void
37371 c_parse_file (void)
37373 static bool already_called = false;
37375 if (already_called)
37376 fatal_error (input_location,
37377 "inter-module optimizations not implemented for C++");
37378 already_called = true;
37380 the_parser = cp_parser_new ();
37381 push_deferring_access_checks (flag_access_control
37382 ? dk_no_deferred : dk_no_check);
37383 cp_parser_translation_unit (the_parser);
37384 the_parser = NULL;
37387 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
37388 vectorlength clause:
37389 Syntax:
37390 vectorlength ( constant-expression ) */
37392 static tree
37393 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
37394 bool is_simd_fn)
37396 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37397 tree expr;
37398 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
37399 safelen clause. Thus, vectorlength is represented as OMP 4.0
37400 safelen. For SIMD-enabled function it is represented by OMP 4.0
37401 simdlen. */
37402 if (!is_simd_fn)
37403 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
37404 loc);
37405 else
37406 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
37407 loc);
37409 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37410 return error_mark_node;
37412 expr = cp_parser_constant_expression (parser);
37413 expr = maybe_constant_value (expr);
37415 /* If expr == error_mark_node, then don't emit any errors nor
37416 create a clause. if any of the above functions returns
37417 error mark node then they would have emitted an error message. */
37418 if (expr == error_mark_node)
37420 else if (!TREE_TYPE (expr)
37421 || !TREE_CONSTANT (expr)
37422 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
37423 error_at (loc, "vectorlength must be an integer constant");
37424 else if (TREE_CONSTANT (expr)
37425 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
37426 error_at (loc, "vectorlength must be a power of 2");
37427 else
37429 tree c;
37430 if (!is_simd_fn)
37432 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
37433 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
37434 OMP_CLAUSE_CHAIN (c) = clauses;
37435 clauses = c;
37437 else
37439 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
37440 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
37441 OMP_CLAUSE_CHAIN (c) = clauses;
37442 clauses = c;
37446 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37447 return error_mark_node;
37448 return clauses;
37451 /* Handles the Cilk Plus #pragma simd linear clause.
37452 Syntax:
37453 linear ( simd-linear-variable-list )
37455 simd-linear-variable-list:
37456 simd-linear-variable
37457 simd-linear-variable-list , simd-linear-variable
37459 simd-linear-variable:
37460 id-expression
37461 id-expression : simd-linear-step
37463 simd-linear-step:
37464 conditional-expression */
37466 static tree
37467 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
37469 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37471 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37472 return clauses;
37473 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37475 cp_parser_error (parser, "expected identifier");
37476 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
37477 return error_mark_node;
37480 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37481 parser->colon_corrects_to_scope_p = false;
37482 while (1)
37484 cp_token *token = cp_lexer_peek_token (parser->lexer);
37485 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37487 cp_parser_error (parser, "expected variable-name");
37488 clauses = error_mark_node;
37489 break;
37492 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
37493 false, false);
37494 tree decl = cp_parser_lookup_name_simple (parser, var_name,
37495 token->location);
37496 if (decl == error_mark_node)
37498 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
37499 token->location);
37500 clauses = error_mark_node;
37502 else
37504 tree e = NULL_TREE;
37505 tree step_size = integer_one_node;
37507 /* If present, parse the linear step. Otherwise, assume the default
37508 value of 1. */
37509 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
37511 cp_lexer_consume_token (parser->lexer);
37513 e = cp_parser_assignment_expression (parser);
37514 e = maybe_constant_value (e);
37516 if (e == error_mark_node)
37518 /* If an error has occurred, then the whole pragma is
37519 considered ill-formed. Thus, no reason to keep
37520 parsing. */
37521 clauses = error_mark_node;
37522 break;
37524 else if (type_dependent_expression_p (e)
37525 || value_dependent_expression_p (e)
37526 || (TREE_TYPE (e)
37527 && INTEGRAL_TYPE_P (TREE_TYPE (e))
37528 && (TREE_CONSTANT (e)
37529 || DECL_P (e))))
37530 step_size = e;
37531 else
37532 cp_parser_error (parser,
37533 "step size must be an integer constant "
37534 "expression or an integer variable");
37537 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
37538 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
37539 OMP_CLAUSE_DECL (l) = decl;
37540 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
37541 OMP_CLAUSE_CHAIN (l) = clauses;
37542 clauses = l;
37544 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37545 cp_lexer_consume_token (parser->lexer);
37546 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37547 break;
37548 else
37550 error_at (cp_lexer_peek_token (parser->lexer)->location,
37551 "expected %<,%> or %<)%> after %qE", decl);
37552 clauses = error_mark_node;
37553 break;
37556 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37557 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
37558 return clauses;
37561 /* Returns the name of the next clause. If the clause is not
37562 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
37563 token is not consumed. Otherwise, the appropriate enum from the
37564 pragma_simd_clause is returned and the token is consumed. */
37566 static pragma_omp_clause
37567 cp_parser_cilk_simd_clause_name (cp_parser *parser)
37569 pragma_omp_clause clause_type;
37570 cp_token *token = cp_lexer_peek_token (parser->lexer);
37572 if (token->keyword == RID_PRIVATE)
37573 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
37574 else if (!token->u.value || token->type != CPP_NAME)
37575 return PRAGMA_CILK_CLAUSE_NONE;
37576 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
37577 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
37578 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
37579 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
37580 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
37581 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
37582 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
37583 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
37584 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
37585 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
37586 else
37587 return PRAGMA_CILK_CLAUSE_NONE;
37589 cp_lexer_consume_token (parser->lexer);
37590 return clause_type;
37593 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
37595 static tree
37596 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
37598 tree clauses = NULL_TREE;
37600 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37601 && clauses != error_mark_node)
37603 pragma_omp_clause c_kind;
37604 c_kind = cp_parser_cilk_simd_clause_name (parser);
37605 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
37606 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
37607 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
37608 clauses = cp_parser_cilk_simd_linear (parser, clauses);
37609 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
37610 /* Use the OpenMP 4.0 equivalent function. */
37611 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
37612 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
37613 /* Use the OpenMP 4.0 equivalent function. */
37614 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
37615 clauses);
37616 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
37617 /* Use the OMP 4.0 equivalent function. */
37618 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
37619 clauses);
37620 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
37621 /* Use the OMP 4.0 equivalent function. */
37622 clauses = cp_parser_omp_clause_reduction (parser, clauses);
37623 else
37625 clauses = error_mark_node;
37626 cp_parser_error (parser, "expected %<#pragma simd%> clause");
37627 break;
37631 cp_parser_skip_to_pragma_eol (parser, pragma_token);
37633 if (clauses == error_mark_node)
37634 return error_mark_node;
37635 else
37636 return c_finish_cilk_clauses (clauses);
37639 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
37641 static void
37642 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
37644 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
37646 if (clauses == error_mark_node)
37647 return;
37649 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
37651 error_at (cp_lexer_peek_token (parser->lexer)->location,
37652 "for statement expected");
37653 return;
37656 tree sb = begin_omp_structured_block ();
37657 int save = cp_parser_begin_omp_structured_block (parser);
37658 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
37659 if (ret)
37660 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
37661 cp_parser_end_omp_structured_block (parser, save);
37662 add_stmt (finish_omp_structured_block (sb));
37665 /* Main entry-point for parsing Cilk Plus _Cilk_for
37666 loops. The return value is error_mark_node
37667 when errors happen and CILK_FOR tree on success. */
37669 static tree
37670 cp_parser_cilk_for (cp_parser *parser, tree grain)
37672 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
37673 gcc_unreachable ();
37675 tree sb = begin_omp_structured_block ();
37676 int save = cp_parser_begin_omp_structured_block (parser);
37678 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
37679 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
37680 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
37681 clauses = finish_omp_clauses (clauses, false);
37683 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL);
37684 if (ret)
37685 cpp_validate_cilk_plus_loop (ret);
37686 else
37687 ret = error_mark_node;
37689 cp_parser_end_omp_structured_block (parser, save);
37690 add_stmt (finish_omp_structured_block (sb));
37691 return ret;
37694 /* Create an identifier for a generic parameter type (a synthesized
37695 template parameter implied by `auto' or a concept identifier). */
37697 static GTY(()) int generic_parm_count;
37698 static tree
37699 make_generic_type_name ()
37701 char buf[32];
37702 sprintf (buf, "auto:%d", ++generic_parm_count);
37703 return get_identifier (buf);
37706 /* Predicate that behaves as is_auto_or_concept but matches the parent
37707 node of the generic type rather than the generic type itself. This
37708 allows for type transformation in add_implicit_template_parms. */
37710 static inline bool
37711 tree_type_is_auto_or_concept (const_tree t)
37713 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
37716 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
37717 (creating a new template parameter list if necessary). Returns the newly
37718 created template type parm. */
37720 static tree
37721 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
37723 gcc_assert (current_binding_level->kind == sk_function_parms);
37725 /* Before committing to modifying any scope, if we're in an
37726 implicit template scope, and we're trying to synthesize a
37727 constrained parameter, try to find a previous parameter with
37728 the same name. This is the same-type rule for abbreviated
37729 function templates. */
37730 if (parser->implicit_template_scope && constr)
37732 tree t = parser->implicit_template_parms;
37733 while (t)
37735 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
37737 tree d = TREE_VALUE (t);
37738 if (TREE_CODE (d) == PARM_DECL)
37739 /* Return the TEMPLATE_PARM_INDEX. */
37740 d = DECL_INITIAL (d);
37741 return d;
37743 t = TREE_CHAIN (t);
37747 /* We are either continuing a function template that already contains implicit
37748 template parameters, creating a new fully-implicit function template, or
37749 extending an existing explicit function template with implicit template
37750 parameters. */
37752 cp_binding_level *const entry_scope = current_binding_level;
37754 bool become_template = false;
37755 cp_binding_level *parent_scope = 0;
37757 if (parser->implicit_template_scope)
37759 gcc_assert (parser->implicit_template_parms);
37761 current_binding_level = parser->implicit_template_scope;
37763 else
37765 /* Roll back to the existing template parameter scope (in the case of
37766 extending an explicit function template) or introduce a new template
37767 parameter scope ahead of the function parameter scope (or class scope
37768 in the case of out-of-line member definitions). The function scope is
37769 added back after template parameter synthesis below. */
37771 cp_binding_level *scope = entry_scope;
37773 while (scope->kind == sk_function_parms)
37775 parent_scope = scope;
37776 scope = scope->level_chain;
37778 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
37780 /* If not defining a class, then any class scope is a scope level in
37781 an out-of-line member definition. In this case simply wind back
37782 beyond the first such scope to inject the template parameter list.
37783 Otherwise wind back to the class being defined. The latter can
37784 occur in class member friend declarations such as:
37786 class A {
37787 void foo (auto);
37789 class B {
37790 friend void A::foo (auto);
37793 The template parameter list synthesized for the friend declaration
37794 must be injected in the scope of 'B'. This can also occur in
37795 erroneous cases such as:
37797 struct A {
37798 struct B {
37799 void foo (auto);
37801 void B::foo (auto) {}
37804 Here the attempted definition of 'B::foo' within 'A' is ill-formed
37805 but, nevertheless, the template parameter list synthesized for the
37806 declarator should be injected into the scope of 'A' as if the
37807 ill-formed template was specified explicitly. */
37809 while (scope->kind == sk_class && !scope->defining_class_p)
37811 parent_scope = scope;
37812 scope = scope->level_chain;
37816 current_binding_level = scope;
37818 if (scope->kind != sk_template_parms
37819 || !function_being_declared_is_template_p (parser))
37821 /* Introduce a new template parameter list for implicit template
37822 parameters. */
37824 become_template = true;
37826 parser->implicit_template_scope
37827 = begin_scope (sk_template_parms, NULL);
37829 ++processing_template_decl;
37831 parser->fully_implicit_function_template_p = true;
37832 ++parser->num_template_parameter_lists;
37834 else
37836 /* Synthesize implicit template parameters at the end of the explicit
37837 template parameter list. */
37839 gcc_assert (current_template_parms);
37841 parser->implicit_template_scope = scope;
37843 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
37844 parser->implicit_template_parms
37845 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
37849 /* Synthesize a new template parameter and track the current template
37850 parameter chain with implicit_template_parms. */
37852 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
37853 tree synth_id = make_generic_type_name ();
37854 tree synth_tmpl_parm;
37855 bool non_type = false;
37857 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
37858 synth_tmpl_parm
37859 = finish_template_type_parm (class_type_node, synth_id);
37860 else if (TREE_CODE (proto) == TEMPLATE_DECL)
37861 synth_tmpl_parm
37862 = finish_constrained_template_template_parm (proto, synth_id);
37863 else
37865 synth_tmpl_parm = copy_decl (proto);
37866 DECL_NAME (synth_tmpl_parm) = synth_id;
37867 non_type = true;
37870 // Attach the constraint to the parm before processing.
37871 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
37872 TREE_TYPE (node) = constr;
37873 tree new_parm
37874 = process_template_parm (parser->implicit_template_parms,
37875 input_location,
37876 node,
37877 /*non_type=*/non_type,
37878 /*param_pack=*/false);
37880 // Chain the new parameter to the list of implicit parameters.
37881 if (parser->implicit_template_parms)
37882 parser->implicit_template_parms
37883 = TREE_CHAIN (parser->implicit_template_parms);
37884 else
37885 parser->implicit_template_parms = new_parm;
37887 tree new_decl = getdecls ();
37888 if (non_type)
37889 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
37890 new_decl = DECL_INITIAL (new_decl);
37892 /* If creating a fully implicit function template, start the new implicit
37893 template parameter list with this synthesized type, otherwise grow the
37894 current template parameter list. */
37896 if (become_template)
37898 parent_scope->level_chain = current_binding_level;
37900 tree new_parms = make_tree_vec (1);
37901 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
37902 current_template_parms = tree_cons (size_int (processing_template_decl),
37903 new_parms, current_template_parms);
37905 else
37907 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
37908 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
37909 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
37910 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
37913 // If the new parameter was constrained, we need to add that to the
37914 // constraints in the template parameter list.
37915 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
37917 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
37918 reqs = conjoin_constraints (reqs, req);
37919 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
37922 current_binding_level = entry_scope;
37924 return new_decl;
37927 /* Finish the declaration of a fully implicit function template. Such a
37928 template has no explicit template parameter list so has not been through the
37929 normal template head and tail processing. synthesize_implicit_template_parm
37930 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
37931 provided if the declaration is a class member such that its template
37932 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
37933 form is returned. Otherwise NULL_TREE is returned. */
37935 static tree
37936 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
37938 gcc_assert (parser->fully_implicit_function_template_p);
37940 if (member_decl_opt && member_decl_opt != error_mark_node
37941 && DECL_VIRTUAL_P (member_decl_opt))
37943 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
37944 "implicit templates may not be %<virtual%>");
37945 DECL_VIRTUAL_P (member_decl_opt) = false;
37948 if (member_decl_opt)
37949 member_decl_opt = finish_member_template_decl (member_decl_opt);
37950 end_template_decl ();
37952 parser->fully_implicit_function_template_p = false;
37953 --parser->num_template_parameter_lists;
37955 return member_decl_opt;
37958 #include "gt-cp-parser.h"